From patchwork Sat Mar 8 22:58:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 871598 Delivered-To: patch@linaro.org Received: by 2002:a5d:64ce:0:b0:38f:210b:807b with SMTP id f14csp345472wri; Sat, 8 Mar 2025 14:59:50 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXIPutMwS0ilo4QbodyD/GrkMiGftphI8GrN0SQmH+ahyPSCEp/rcLGhEXGQF0yhTAYpHWQpQ==@linaro.org X-Google-Smtp-Source: AGHT+IFKDCPfZ9+uaNtsCTAoC4Xcx9/iTRTx+XjyjOnNHB47cYhrKBS1jA8CFL6YCVLk/EQTNScq X-Received: by 2002:a05:620a:1b8a:b0:7c0:b490:2c26 with SMTP id af79cd13be357-7c53c8705d2mr607539885a.12.1741474790218; Sat, 08 Mar 2025 14:59:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1741474790; cv=none; d=google.com; s=arc-20240605; b=FTGN9hpFzU5kcd0pn1HGR+872a0T2O6q5Yl13B+RUa/RpKZW3EXATzStXreLwrGxn8 jTIMEw98azmLrTwcGGc2tuoNQOFdr9lfTYKv5TmbCL1ccfJADxKSYyVYC9n7UHM6bUOW PSEJ6feoaiZzISjPf9M0pOxPCsop2e+vlZLSI1vZ7Nr4XRWQ5sF4RO7GF8yWfVD6NSVv bBYRzog9t4SSUMHIrtvCr1v9t9eICKMr/HvXfUAab0bHdMjFTBeW6OQNj8rmgWkusC4D sVbgikJKZkyQSrgLe7mNQbNXDeFnFUUSi7yAAwnNypXligT0py+VxoU0sjwF18LsOtPd Ee/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=nAl4pf8E8sE2qLvyFwJ/6sczfmHaPGMTc7HMlUw0GYQ=; fh=I56VROqnFHrV/Rf4dX+umpAXd/kTSqWX1noJJ1IeGII=; b=H0MQPCwyl0YQGl7jHJ5hQsmMwHFTGuTYrBeWYj7bND/pLOh2cbl+U3GOk6xxDcrVq0 YuS2RiPPdX3bqNpsTPgI3rd3jfsmQJJDDIytjD9lsQNidpUo92b0IGlIIrUcrgJ8/CJg tAQ8KMI68TYijiXNguAw0PusVkULhAyrq7vcjTY/9NO8Zx1lL97juH2CYe+vZv9AQKAK NyG8odGB5kDOpbIjgsuWE5IiRrjH8RUuxNIi8DAw0PIKaEm1tz7Va2Bx1ksZ/dwDibo1 9tlIbKA2md4KBhEhBLEBu7EfUKYxEwLHRPIIVdCtiu7hZPNwpm/Hw5RmaUjNFDBlfnR5 m+Pw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fdEOrYX0; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7c541dfcb72si177274185a.534.2025.03.08.14.59.50 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 08 Mar 2025 14:59:50 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fdEOrYX0; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tr38S-0004ZH-RF; Sat, 08 Mar 2025 17:59:12 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tr38O-0004WA-K0 for qemu-devel@nongnu.org; Sat, 08 Mar 2025 17:59:08 -0500 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tr38M-0005K6-9w for qemu-devel@nongnu.org; Sat, 08 Mar 2025 17:59:08 -0500 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-22185cddbffso74606635ad.1 for ; Sat, 08 Mar 2025 14:59:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1741474744; x=1742079544; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nAl4pf8E8sE2qLvyFwJ/6sczfmHaPGMTc7HMlUw0GYQ=; b=fdEOrYX0MYKLV5FGoNZXuwV5wzSAa1Y66tiHo8WeHruYGj1B8h1twH1JzoncPfVaF7 zNKrDaRibmLnvrN8D8dv6LpiESCuWP//FZNC2edvKwtT/DGYFfgnzoXW5dAXSphx9/la DVTBrsvnZFu3dvNsVxeWHwlBKSBpGygGz+rTkNkUxt6A/MAr/EktbthxEifa/Yyv+V9M wklk5a9vwMFWWx/oUbYK4yqP8Hd9bNwLbM+Q5ruvyxpdeeQ/fn8tAgMJ7AeyrJhyql+/ AYXz/WMV9gj532LgUPwZsbVXZ+GlBPBHEl+Z7FPbFz0DBpZ6Ay6XMRM4GHGhXhy1c0rc zhgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741474744; x=1742079544; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nAl4pf8E8sE2qLvyFwJ/6sczfmHaPGMTc7HMlUw0GYQ=; b=A3xlfwyB52ZKMJVA1ajEX9uDfoj5Jclkg6OccDz/fg1gmIhYR7dfcnE/2ujfxB8207 N/jLvAOzkunx6O1i1jPjohMWWxKX04J8hPoHuQ92egQwv/6kd1p3FeLaTJO9etkFJ+iM XjD1bLZXsKn948ot+luZWH2VR/u3FNWEXYWYccfbRPMC0tX07v0IYX5vPeiDERAJGtsY dP7pVfLEGQdojAXuLaBkdv9/91DqPhItDCawxGOxVz8HaHUUEMloyvAMiEVWbczQKbY9 iRyEUNdJKMI4k8YX5ck2D7/SasbiZT29HBabT1wGaNSVMqJ02glF8FTSBJeGB/kcIMIj jpmg== X-Gm-Message-State: AOJu0YzX/V7kApHdH0OaG+xKXLLp57tb43wf/n7j7pMRanCRjjE205kr qa9M+oB9+5FNO0l/h8p1DmIq//BX5JfCpHnDtKnmhH1Pp8ZX4P5wOO9f2DT3q5Pi1uZTy6pTnvU j X-Gm-Gg: ASbGncsC+m2+aoanjebb4k/iVfQhbam3OzX/wldD1VdECOjWshJUA/+AznrJjYYGYuf Eldz2gP64O8dIyqOH3Lyp67v1TiZgTbfUHQkdlliAF1lUNnV6lBUPQKc97KPeqKjlxd5EnOxkNa uRG3ksMdDOGSb3oTqaW5QIpLVH4jW6PQMcGakgaHFjIF6xa3c38Ldkk2Dm9STlVa9C287M/yRol Dbs0ZAwANqrZSlxmTyicpjcHpbkRfCOKeYbtNo9uGrqJW4hVzuYey0wb3sqC74Ijp7w9dqadKp4 eTE9NIJlgdiXdOR0z95mNGvfF0SrAUtiC1/Sv3nEXbTCr+TYNrOcy9+piVKJTN5I6MjuWqGU7Ub 5 X-Received: by 2002:a17:902:f708:b0:223:4d5e:76a6 with SMTP id d9443c01a7336-2244f057623mr79708035ad.1.1741474744602; Sat, 08 Mar 2025 14:59:04 -0800 (PST) Received: from stoup.. (174-21-74-48.tukw.qwest.net. [174.21.74.48]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22410a908a0sm51831175ad.162.2025.03.08.14.59.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Mar 2025 14:59:04 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, Andreas Schwab Subject: [PATCH v2] linux-user/riscv: Fix handling of cpu mask in riscv_hwprobe syscall Date: Sat, 8 Mar 2025 14:58:40 -0800 Message-ID: <20250308225902.1208237-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250308225902.1208237-1-richard.henderson@linaro.org> References: <20250308225902.1208237-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62e; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org The third argument of the syscall contains the size of the cpu mask in bytes, not bits. Nor is the size rounded up to a multiple of sizeof(abi_ulong). Cc: qemu-stable@nongnu.org Reported-by: Andreas Schwab Fixes: 9e1c7d982d7 ("linux-user/riscv: Add syscall riscv_hwprobe") Signed-off-by: Richard Henderson --- linux-user/syscall.c | 55 +++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 02ea4221c9..fcc77c094d 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -9118,35 +9118,38 @@ static void risc_hwprobe_fill_pairs(CPURISCVState *env, } } -static int cpu_set_valid(abi_long arg3, abi_long arg4) +/* + * If the cpumask_t of (target_cpus, cpusetsize) cannot be read: -EFAULT. + * If the cpumast_t has no bits set: -EINVAL. + * Otherwise the cpumask_t contains some bit set: 0. + * Unlike the kernel, we do not mask cpumask_t by the set of online cpus, + * nor bound the search by cpumask_size(). + */ +static int nonempty_cpu_set(abi_ulong cpusetsize, abi_ptr target_cpus) { - int ret, i, tmp; - size_t host_mask_size, target_mask_size; - unsigned long *host_mask; + unsigned char *p = lock_user(VERIFY_READ, target_cpus, cpusetsize, 1); + int ret = -TARGET_EFAULT; - /* - * cpu_set_t represent CPU masks as bit masks of type unsigned long *. - * arg3 contains the cpu count. - */ - tmp = (8 * sizeof(abi_ulong)); - target_mask_size = ((arg3 + tmp - 1) / tmp) * sizeof(abi_ulong); - host_mask_size = (target_mask_size + (sizeof(*host_mask) - 1)) & - ~(sizeof(*host_mask) - 1); - - host_mask = alloca(host_mask_size); - - ret = target_to_host_cpu_mask(host_mask, host_mask_size, - arg4, target_mask_size); - if (ret != 0) { - return ret; - } - - for (i = 0 ; i < host_mask_size / sizeof(*host_mask); i++) { - if (host_mask[i] != 0) { - return 0; + if (p) { + ret = -TARGET_EINVAL; + /* + * Since we only care about the empty/non-empty state of the cpumask_t + * not the individual bits, we do not need to repartition the bits + * from target abi_ulong to host unsigned long. + * + * Note that the kernel does not round up cpusetsize to a multiple of + * sizeof(abi_ulong). After bounding cpusetsize by cpumask_size(), + * it copies exactly cpusetsize bytes into a zeroed buffer. + */ + for (abi_ulong i = 0; i < cpusetsize; ++i) { + if (p[i]) { + ret = 0; + break; + } } + unlock_user(p, target_cpus, 0); } - return -TARGET_EINVAL; + return ret; } static abi_long do_riscv_hwprobe(CPUArchState *cpu_env, abi_long arg1, @@ -9163,7 +9166,7 @@ static abi_long do_riscv_hwprobe(CPUArchState *cpu_env, abi_long arg1, /* check cpu_set */ if (arg3 != 0) { - ret = cpu_set_valid(arg3, arg4); + ret = nonempty_cpu_set(arg3, arg4); if (ret != 0) { return ret; }