Skip to contentSkip to conversation

linux-user: fix handling of cpu mask in riscv_hwprobe syscall

PATCH In review Andreas Schwab 3 participants 3 messages Mar 4, 2025 – Mar 11, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: fix handling of cpu mask in riscv_hwprobe syscall

The third argument of the riscv_hwprobe syscall contains the size of the cpu mask in bytes, not bits.

---
 linux-user/syscall.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
linux-user/syscall.c +4 -4
index a22a5df8cc..4cc1a31d0d 100644
@@ -9120,16 +9120,16 @@ static void risc_hwprobe_fill_pairs(CPURISCVState *env,
 
 static int cpu_set_valid(abi_long arg3, abi_long arg4)
 {
-    int ret, i, tmp;
+    int ret, i;
     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.
+     * arg3 contains the size of the cpu mask.
      */
-    tmp = (8 * sizeof(abi_ulong));
-    target_mask_size = ((arg3 + tmp - 1) / tmp) * sizeof(abi_ulong);
+    target_mask_size = (arg3 + (sizeof(abi_ulong) - 1)) &
+                       ~(sizeof(abi_ulong) - 1);
     host_mask_size = (target_mask_size + (sizeof(*host_mask) - 1)) &
                      ~(sizeof(*host_mask) - 1);
 
Signature · 7 lines
2.48.1


-- 
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
#1 lore raw
Quoted 5 lines · Andreas Schwab

The third argument of the riscv_hwprobe syscall contains the size of the cpu mask in bytes, not bits.

Richard sent a v2 that I have applied:

https://patchew.org/QEMU/[email protected]/

Alistair

Quoted 38 lines
---
 linux-user/syscall.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
linux-user/syscall.c +4 -5
index a22a5df8cc..4cc1a31d0d 100644
@@ -9120,16 +9120,16 @@ static void risc_hwprobe_fill_pairs(CPURISCVState *env,
 
 static int cpu_set_valid(abi_long arg3, abi_long arg4)
 {
-    int ret, i, tmp;
+    int ret, i;
     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.
+     * arg3 contains the size of the cpu mask.
      */
-    tmp = (8 * sizeof(abi_ulong));
-    target_mask_size = ((arg3 + tmp - 1) / tmp) * sizeof(abi_ulong);
+    target_mask_size = (arg3 + (sizeof(abi_ulong) - 1)) &
+                       ~(sizeof(abi_ulong) - 1);
     host_mask_size = (target_mask_size + (sizeof(*host_mask) - 1)) &
                      ~(sizeof(*host_mask) - 1);
 
--

2.48.1

--
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7 "And now for something completely different."

Acked-by
#2 lore raw
Quoted 11 lines · Alistair Francis
Quoted 5 lines · Andreas Schwab

The third argument of the riscv_hwprobe syscall contains the size of the cpu mask in bytes, not bits.

Richard sent a v2 that I have applied:

https://patchew.org/QEMU/[email protected]/

Hey, thanks for fixing!

Quoted 41 lines

Alistair

Quoted 38 lines
---
 linux-user/syscall.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
linux-user/syscall.c +4 -5
index a22a5df8cc..4cc1a31d0d 100644
@@ -9120,16 +9120,16 @@ static void risc_hwprobe_fill_pairs(CPURISCVState *env,
 
 static int cpu_set_valid(abi_long arg3, abi_long arg4)
 {
-    int ret, i, tmp;
+    int ret, i;
     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.
+     * arg3 contains the size of the cpu mask.
      */
-    tmp = (8 * sizeof(abi_ulong));
-    target_mask_size = ((arg3 + tmp - 1) / tmp) * sizeof(abi_ulong);
+    target_mask_size = (arg3 + (sizeof(abi_ulong) - 1)) &
+                       ~(sizeof(abi_ulong) - 1);
     host_mask_size = (target_mask_size + (sizeof(*host_mask) - 1)) &
                      ~(sizeof(*host_mask) - 1);
 
--

2.48.1

--
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7 "And now for something completely different."