From patchwork Wed Mar 19 08:03:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Francis X-Patchwork-Id: 874719 Delivered-To: patch@linaro.org Received: by 2002:a5d:6a90:0:b0:38f:210b:807b with SMTP id s16csp264934wru; Wed, 19 Mar 2025 01:05:23 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWPok3v/nWDYVIMxRvk7npJLOObr0ZoKBSe6VnWqcNyf4ioU4odwxnjlK7JO11kozYOiqPBeA==@linaro.org X-Google-Smtp-Source: AGHT+IHfFS6+tNQYQ4k3vmTON1oDNVW13KAF75SYNDmRAsvEmQncBnwOibOaAGQp1UuZjTdo78Jn X-Received: by 2002:a05:600c:1f83:b0:43c:fe85:e4ba with SMTP id 5b1f17b1804b1-43d437a1ecdmr16033295e9.15.1742371523263; Wed, 19 Mar 2025 01:05:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1742371523; cv=none; d=google.com; s=arc-20240605; b=jSZLn/fj7Gkk2H5iZaQMHpoV35Y8vjp04aItXU8g19jeBAWY2DScK/aazFMvpOybIV LNmxdD2/6/H5ye6R40oIl0zx1LqWx2GWNgd+iMhfxDQSBZBYzitpvWC7AWJWF9FS0pgm wUrepwQtMBN6F0lgLd2v/HmdXsiRd/Sg/LeSPRNfxKW8vZqZMkuvvEDVlVQNJGdDGrGg EW4arauMheSeBwIahXh7AiPiVqopafoYixUdYy7oqtavLT51LoBJeMr9gJhN2Ip8n0PB ZnOA8tU7LxkemvCkztGtDUQvCrg4ofT7vSCkj3TQWrMwNKStdkDhdCjyGZGqT+XFhaTm 58wQ== 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=g8ym/X0tsdAFFXl/72oxrMtQ/+O/1A1epWPinRhhrVg=; fh=AhtJhYlvUB9oe+82BWHODdwxDXZAtOg8oNmXNsAR8vQ=; b=VjJfFkBcnziRu4P1JiFnQdFeEee9jVER6wlmChWsk+lDQ2cmhaTw01OviMuzFcy35c MeqcITTeWBFHAJ9Sa5gOVyoDoWgUc5WmAliGk/naCRNfat3TG0yUZo3V/PMUEWU7Y46u 0iOXXaivabmdZnUoUpGjrVOxzWIzYXhfI1LJImSD8yG5st8k3GDUNfQ6E45L5hbz5bK3 8FH3qlyy4Rc2uA7a0SjbVC7u/ZaStqCz1JsGbDZq2+R31OLjgxIkRlV01QhUGVtBKhg8 SHxSSH0zcLQvaRfofFXyJ4MyT0zYEkiaOWbe59H0zXt8MrP74xjO65pUQYax0u0N+LtQ bBwQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=I1HOstOr; 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=QUARANTINE dis=NONE) header.from=gmail.com; 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 ffacd0b85a97d-395cb316707si12359402f8f.818.2025.03.19.01.05.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 19 Mar 2025 01:05:23 -0700 (PDT) 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=@gmail.com header.s=20230601 header.b=I1HOstOr; 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=QUARANTINE dis=NONE) header.from=gmail.com; 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 1tuoPe-0000Cl-3L; Wed, 19 Mar 2025 04:04:30 -0400 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 1tuoP1-00084S-MU; Wed, 19 Mar 2025 04:03:52 -0400 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tuoOz-0004UL-Rp; Wed, 19 Mar 2025 04:03:51 -0400 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-224341bbc1dso124693895ad.3; Wed, 19 Mar 2025 01:03:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742371428; x=1742976228; 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=g8ym/X0tsdAFFXl/72oxrMtQ/+O/1A1epWPinRhhrVg=; b=I1HOstOr/2Vb9YpL1HeYn+fL/xbgEiSVdQhtO1ppedL0isSDsMMdRGSF/B11HMyW0x Bh+7MtjJQiYaEsfWMu9Q3J1l5Cw84yb6rEKiP4fyb5XV4d+xa2y9k1yXgbTMBIi+14Hz g/Eul/ENxdXVz4i6TVIdeQiP6/h/FO5r68VcPZGGLkFBBg16CtZTQjKZtPSkshUfoFdo rhfLpIO0rg4rNkOkVM2tkqtCKLVdPhARkr/XtKUoTFrvmEREW5YyR9FHSwl7tjXeis4+ GTApl9SiSGS3EVHwEJqgOSpgEf3LrUMpLLbu4XKujkLZrx6ZcJ/n72mPiUZr8SiIOuEG hYXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742371428; x=1742976228; 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=g8ym/X0tsdAFFXl/72oxrMtQ/+O/1A1epWPinRhhrVg=; b=qBuP07PIpr5ihep2nFOUObx7h2GCo23q/TENsJoeGw/12wv/Cvkp4DGqCXob9jqlRq hpP6EJBLRzrpwcexglKaCSpoLxaTFzLUG+jqdRAuWVlbs9vEXwxlEprTu5XbzYHvsB8n Ie7yXxwew/hfrsPBTLu0RGDoM+fj8q5W9Sn0FeU7vbnez59TgScSd9KCsVlOtdZUJvIe GrdlFClvTu78cus1r+UqtS+1xsYTIRiXrI8BtPETEzO6noe29EHlEyufbZ20hItq0kh9 oi7hc1r9I92hHp392VnoNw9VnaQZuSzeHtGg1fZtmLXcslIIOqaIRcR7mpIvHuY5QNCz A7tw== X-Forwarded-Encrypted: i=1; AJvYcCXxUNcpxssLGhReHIPAoN04szH4Ay0qNMiubOnqavX92rwxIP5QzYd7yQ/aR0+xCt+Pi6csXKX5y3z++Q==@nongnu.org X-Gm-Message-State: AOJu0YwjmLEfW+VHZuQcobehxL2YDxdGBWFKpy+N1kbINQu7FC8TmKIY qUwfiQ7v6s3aRaX9me4Xun7Sgtn99bseBufYCKG16W8crPm0ZRfVPjZR2g== X-Gm-Gg: ASbGncs5vAqmcJM9At/nq0bVroillWA1Qpw0DdEew1DxJOiw8UEE4T+WuEWB46Uu6hl 7wzD0eYZurR41HuDVUdduLnikgF9KQdXH2ePiQ5Of+m6pUgVaTEX67EuAsa0WpWKo6CqEtDQXtk sSVTE1BKFiD6SRWOz6pMK52uGKZVP5ZM3cN7OwGlAJLsUm90mDa30DMQVZH74tZAHm951CmUkOA J/NeJj/IENyYEHIUihJbW2t6mx9Rz8q7K+emR0WgYlfAUfCsJiVF9NCeAC/3dj1NX95j3cxK3OG 8WwwY8Pwa2dvk00Ns9ZCYKqn/CmNU9RE66b6L/0pYN7cQnpuoHOSOkgPwg1oib2jwfQnPemXKvc QAQi68C3mEd6QS0a40azEuDlPQZ15K9Aw5SC182jYu54a+Rrm X-Received: by 2002:a17:902:ea0a:b0:223:3396:15e8 with SMTP id d9443c01a7336-2264981d9bdmr28022065ad.22.1742371427814; Wed, 19 Mar 2025 01:03:47 -0700 (PDT) Received: from toolbox.alistair23.me (2403-580b-97e8-0-321-6fb2-58f1-a1b1.ip6.aussiebb.net. [2403:580b:97e8:0:321:6fb2:58f1:a1b1]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-225c6ba6c8dsm107725255ad.156.2025.03.19.01.03.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Mar 2025 01:03:47 -0700 (PDT) From: Alistair Francis X-Google-Original-From: Alistair Francis To: qemu-devel@nongnu.org Cc: alistair23@gmail.com, Richard Henderson , qemu-stable@nongnu.org, Andreas Schwab , Alistair Francis Subject: [PULL 09/10] linux-user/riscv: Fix handling of cpu mask in riscv_hwprobe syscall Date: Wed, 19 Mar 2025 18:03:07 +1000 Message-ID: <20250319080308.609520-10-alistair.francis@wdc.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250319080308.609520-1-alistair.francis@wdc.com> References: <20250319080308.609520-1-alistair.francis@wdc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62a; envelope-from=alistair23@gmail.com; helo=mail-pl1-x62a.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 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, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, 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 From: Richard Henderson 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 Reviewed-by: Alistair Francis Message-ID: <20250308225902.1208237-3-richard.henderson@linaro.org> Signed-off-by: Alistair Francis --- 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 b32de763f7..8bfe4912e1 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -9119,35 +9119,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; - - /* - * 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; - } + unsigned char *p = lock_user(VERIFY_READ, target_cpus, cpusetsize, 1); + int ret = -TARGET_EFAULT; - 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, @@ -9164,7 +9167,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; }