Skip to contentSkip to conversation

cpumask: Preserve short reads in cpumap printing helpers

PATCH In review Hungyu Lin 3 participants 3 messages Oct 20 – Oct 28 Bitmap API

lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH 1/1 cpumask: Preserve short reads in cpumap printing helpers

The cpumap printing helpers unconditionally subtract one from the bitmap helper's return value to exclude a terminating NUL. A partial read may not reach that NUL, so this also subtracts a data byte from the returned length. A one-byte read therefore returns zero even when data remains. For example, this command prints nothing despite the file being nonempty:

head -c 1 /sys/devices/system/cpu/cpu0/topology/core_cpus_list

Subtract one only when the copied range actually ends in NUL. This preserves short reads, EOF and negative error returns while continuing to exclude the terminator from complete sysfs reads.

Fixes c86a2d9058c5 ("cpumask: Omit terminating null byte in cpumap_print_{list,bitmask}_to_buf")
---
 include/linux/cpumask.h | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
include/linux/cpumask.h +16 -4
index 4c8bb6953107..2caeae982faf 100644
@@ -1334,8 +1334,14 @@ static __always_inline
 ssize_t cpumap_print_bitmask_to_buf(char *buf, const struct cpumask *mask,
 				    loff_t off, size_t count)
 {
-	return bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask),
-				   nr_cpu_ids, off, count) - 1;
+	ssize_t n;
+
+	n = bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask),
+					nr_cpu_ids, off, count);
+	if (n > 0 && buf[n - 1] == '\0')
+		n--;
+
+	return n;
 }
 
 /**
@@ -1356,8 +1362,14 @@ static __always_inline
 ssize_t cpumap_print_list_to_buf(char *buf, const struct cpumask *mask,
 				 loff_t off, size_t count)
 {
-	return bitmap_print_list_to_buf(buf, cpumask_bits(mask),
-				   nr_cpu_ids, off, count) - 1;
+	ssize_t n;
+
+	n = bitmap_print_list_to_buf(buf, cpumask_bits(mask),
+				     nr_cpu_ids, off, count);
+	if (n > 0 && buf[n - 1] == '\0')
+		n--;
+
+	return n;
 }
 
 #if NR_CPUS <= BITS_PER_LONG

base-commit: a73b4d1fa7d334a5f7de5bb0a14726530f66196e

Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Hungyu Lin

The cpumap printing helpers unconditionally subtract one from the bitmap helper's return value to exclude a terminating NUL. A partial read may not reach that NUL, so this also subtracts a data byte from the returned length. A one-byte read therefore returns zero even when data remains. For example, this command prints nothing despite the file being nonempty:

Quoted 8 lines

head -c 1 /sys/devices/system/cpu/cpu0/topology/core_cpus_list

Subtract one only when the copied range actually ends in NUL. This preserves short reads, EOF and negative error returns while continuing to exclude the terminator from complete sysfs reads.

Fixes c86a2d9058c5 ("cpumask: Omit terminating null byte in cpumap_print_{list,bitmask}_to_buf")

Thanks

#2 lore raw
In reply to Hungyu Lin
Quoted 6 lines · Hungyu Lin

The cpumap printing helpers unconditionally subtract one from the bitmap helper's return value to exclude a terminating NUL. A partial read may not reach that NUL, so this also subtracts a data byte from the returned length. A one-byte read therefore returns zero even when data remains. For example, this command prints nothing despite the file being nonempty:

Quoted 8 lines

head -c 1 /sys/devices/system/cpu/cpu0/topology/core_cpus_list

Subtract one only when the copied range actually ends in NUL. This preserves short reads, EOF and negative error returns while continuing to exclude the terminator from complete sysfs reads.

Fixes c86a2d9058c5 ("cpumask: Omit terminating null byte in cpumap_print_{list,bitmask}_to_buf")

Added in bitmap-for-next. Thanks!

Quoted 44 lines
---
 include/linux/cpumask.h | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
include/linux/cpumask.h +16 -4
index 4c8bb6953107..2caeae982faf 100644
@@ -1334,8 +1334,14 @@ static __always_inline
 ssize_t cpumap_print_bitmask_to_buf(char *buf, const struct cpumask *mask,
 				    loff_t off, size_t count)
 {
-	return bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask),
-				   nr_cpu_ids, off, count) - 1;
+	ssize_t n;
+
+	n = bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask),
+					nr_cpu_ids, off, count);
+	if (n > 0 && buf[n - 1] == '\0')
+		n--;
+
+	return n;
 }
 
 /**
@@ -1356,8 +1362,14 @@ static __always_inline
 ssize_t cpumap_print_list_to_buf(char *buf, const struct cpumask *mask,
 				 loff_t off, size_t count)
 {
-	return bitmap_print_list_to_buf(buf, cpumask_bits(mask),
-				   nr_cpu_ids, off, count) - 1;
+	ssize_t n;
+
+	n = bitmap_print_list_to_buf(buf, cpumask_bits(mask),
+				     nr_cpu_ids, off, count);
+	if (n > 0 && buf[n - 1] == '\0')
+		n--;
+
+	return n;
 }
 
 #if NR_CPUS <= BITS_PER_LONG

base-commit: a73b4d1fa7d334a5f7de5bb0a14726530f66196e