Skip to contentSkip to conversation

cpumask: Omit terminating null byte in cpumap_print_{list,bitmask}_to_buf

PATCH In review Tobias Klauser 6 participants 10 messages Sep 16, 2021 – Sep 30, 2021 Bitmap API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpumask: Omit terminating null byte in cpumap_print_{list,bitmask}_to_buf

The changes in the patch series [1] introduced a terminating null byte when reading from cpulist or cpumap sysfs files, for example:

  $ xxd /sys/devices/system/node/node0/cpulist
  00000000: 302d 310a 00                             0-1..

Before this change, the output looked as follows:

  $ xxd /sys/devices/system/node/node0/cpulist
  00000000: 302d 310a                                0-1.

Fix this regression by excluding the terminating null byte from the returned length in cpumap_print_list_to_buf and cpumap_print_bitmask_to_buf.

[1] https://lore.kernel.org/all/[email protected]/

Fixes 1fae562983ca ("cpumask: introduce cpumap_print_list/bitmask_to_buf to support large bitmask and list")
---
 include/linux/cpumask.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
include/linux/cpumask.h +4 -3
index 5d4d07a9e1ed..1e7399fc69c0 100644
@@ -996,14 +996,15 @@ cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask)
  * cpumask; Typically used by bin_attribute to export cpumask bitmask
  * ABI.
  *
- * Returns the length of how many bytes have been copied.
+ * Returns the length of how many bytes have been copied, excluding
+ * terminating '\0'.
  */
 static 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);
+				   nr_cpu_ids, off, count) - 1;
 }
 
 /**
@@ -1018,7 +1019,7 @@ 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);
+				   nr_cpu_ids, off, count) - 1;
 }
 
 #if NR_CPUS <= BITS_PER_LONG
Signature · 1 line
2.33.0
#1 lore raw
Quoted 17 lines

-----Original Message-----
From: Tobias Klauser [mailto:[email protected]]
Sent: Friday, September 17, 2021 10:27 AM
To: Greg Kroah-Hartman <[email protected]>; Jonathan Cameron <[email protected]>; tiantao (H) <[email protected]>; Song Bao Hua (Barry Song) <[email protected]>
Cc: Andrew Morton <[email protected]>; Andy Shevchenko <[email protected]>; Yury Norov <[email protected]>; Peter Zijlstra <[email protected]>; [email protected]
Subject: [PATCH] cpumask: Omit terminating null byte in cpumap_print_{list,bitmask}_to_buf

The changes in the patch series [1] introduced a terminating null byte when reading from cpulist or cpumap sysfs files, for example:

Quoted 7 lines
  $ xxd /sys/devices/system/node/node0/cpulist
  00000000: 302d 310a 00                             0-1..

Before this change, the output looked as follows:

  $ xxd /sys/devices/system/node/node0/cpulist
  00000000: 302d 310a                                0-1.

If we don't use xxd, I don't see any actual harm of this NULL byte by cat, lscpu, numactl etc. this doesn't break them at all.

if we only want to make sure the output is exactly same with before for every single character, this patch is right.

Quoted 48 lines

Fix this regression by excluding the terminating null byte from the returned length in cpumap_print_list_to_buf and cpumap_print_bitmask_to_buf.

[1]
https://lore.kernel.org/all/[email protected]
m/

Fixes 1fae562983ca ("cpumask: introduce cpumap_print_list/bitmask_to_buf to

support large bitmask and list")

---
 include/linux/cpumask.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
include/linux/cpumask.h +3 -2
index 5d4d07a9e1ed..1e7399fc69c0 100644
@@ -996,14 +996,15 @@ cpumap_print_to_pagebuf(bool list, char *buf, const struct
 cpumask *mask)
  * cpumask; Typically used by bin_attribute to export cpumask bitmask
  * ABI.
  *
- * Returns the length of how many bytes have been copied.
+ * Returns the length of how many bytes have been copied, excluding
+ * terminating '\0'.
  */
 static 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);
+				   nr_cpu_ids, off, count) - 1;
 }
 

/**

include/linux/cpumask.h +1 -1
@@ -1018,7 +1019,7 @@ 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);
+				   nr_cpu_ids, off, count) - 1;
 }
 
Quoted 3 lines

#if NR_CPUS <= BITS_PER_LONG
--
2.33.0

Thanks
Barry

maintainer Acked-by
#2 lore raw

[CC Greg KH <[email protected]>]

Quoted 20 lines · Song Bao Hua (Barry Song)
Quoted 20 lines

-----Original Message-----
From: Tobias Klauser [mailto:[email protected]]
Sent: Friday, September 17, 2021 10:27 AM
To: Greg Kroah-Hartman <[email protected]>; Jonathan Cameron <[email protected]>; tiantao (H) <[email protected]>; Song Bao Hua (Barry Song) <[email protected]>
Cc: Andrew Morton <[email protected]>; Andy Shevchenko <[email protected]>; Yury Norov <[email protected]>; Peter Zijlstra <[email protected]>; [email protected]
Subject: [PATCH] cpumask: Omit terminating null byte in cpumap_print_{list,bitmask}_to_buf

The changes in the patch series [1] introduced a terminating null byte when reading from cpulist or cpumap sysfs files, for example:

  $ xxd /sys/devices/system/node/node0/cpulist
  00000000: 302d 310a 00                             0-1..
Quoted 7 lines
Quoted 4 lines

Before this change, the output looked as follows:

  $ xxd /sys/devices/system/node/node0/cpulist
  00000000: 302d 310a                                0-1.

If we don't use xxd, I don't see any actual harm of this NULL byte by cat, lscpu, numactl etc. this doesn't break them at all.

Barry, Tobias' script that uses xxd is userspace. Linux kernel never breaks userspace.

Quoted 2 lines

if we only want to make sure the output is exactly same with before for every single character, this patch is right.

We don't want to make the output exactly the same. The "0,1" would also work for the example above. But garbage characters following \0 is a bug that should be fixed.

Greg, would you like to move this patch through your tree?

Quoted 53 lines
Quoted 47 lines

Fix this regression by excluding the terminating null byte from the returned length in cpumap_print_list_to_buf and cpumap_print_bitmask_to_buf.

[1]
https://lore.kernel.org/all/[email protected]
m/

Fixes 1fae562983ca ("cpumask: introduce cpumap_print_list/bitmask_to_buf to

support large bitmask and list")

---
 include/linux/cpumask.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
include/linux/cpumask.h +3 -2
index 5d4d07a9e1ed..1e7399fc69c0 100644
@@ -996,14 +996,15 @@ cpumap_print_to_pagebuf(bool list, char *buf, const struct
 cpumask *mask)
  * cpumask; Typically used by bin_attribute to export cpumask bitmask
  * ABI.
  *
- * Returns the length of how many bytes have been copied.
+ * Returns the length of how many bytes have been copied, excluding
+ * terminating '\0'.
  */
 static 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);
+				   nr_cpu_ids, off, count) - 1;
 }
 

/**

include/linux/cpumask.h +1 -1
@@ -1018,7 +1019,7 @@ 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);
+				   nr_cpu_ids, off, count) - 1;
 }
 
Quoted 3 lines

#if NR_CPUS <= BITS_PER_LONG
--
2.33.0

Thanks
Barry

#3 lore raw
Quoted 25 lines · Yury Norov

[CC Greg KH <[email protected]>]

Quoted 22 lines · Song Bao Hua (Barry Song)
Quoted 22 lines

-----Original Message-----
From: Tobias Klauser [mailto:[email protected]]
Sent: Friday, September 17, 2021 10:27 AM
To: Greg Kroah-Hartman <[email protected]>; Jonathan Cameron <[email protected]>; tiantao (H) <[email protected]>; Song Bao Hua (Barry Song) <[email protected]>
Cc: Andrew Morton <[email protected]>; Andy Shevchenko <[email protected]>; Yury Norov <[email protected]>; Peter Zijlstra <[email protected]>; [email protected]
Subject: [PATCH] cpumask: Omit terminating null byte in cpumap_print_{list,bitmask}_to_buf

The changes in the patch series [1] introduced a terminating null byte when reading from cpulist or cpumap sysfs files, for example:

  $ xxd /sys/devices/system/node/node0/cpulist
  00000000: 302d 310a 00                             0-1..

Before this change, the output looked as follows:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
  $ xxd /sys/devices/system/node/node0/cpulist
  00000000: 302d 310a                                0-1.

If we don't use xxd, I don't see any actual harm of this NULL byte by cat, lscpu, numactl etc. this doesn't break them at all.

Barry, Tobias' script that uses xxd is userspace. Linux kernel never breaks userspace.

FWIW, the example using xxd was just to illustrate the issue in a concise way for the commit message. This is breaking other userspace programs as well. Originally, I discovered this because Kubernetes' kubelet was crashing on a bpf-next kernel. See [1] and following comments for more information:

[1] https://github.com/cilium/cilium/pull/17394#issuecomment-920902042

Quoted 6 lines
Quoted 2 lines

if we only want to make sure the output is exactly same with before for every single character, this patch is right.

We don't want to make the output exactly the same. The "0,1" would also work for the example above. But garbage characters following \0 is a bug that should be fixed.

I think we also want to avoid the \0 itself, which is what this patch does and is in line with previous behavior. It also looks like all other sysfs files in that subtree expose the same content format (i.e. \n is the last character, not \0).

Thanks,
Tobias

Acked-by
#4 lore raw
Quoted 36 lines · Tobias Klauser
Quoted 25 lines · Yury Norov

[CC Greg KH <[email protected]>]

Quoted 22 lines · Song Bao Hua (Barry Song)
Quoted 22 lines

-----Original Message-----
From: Tobias Klauser [mailto:[email protected]]
Sent: Friday, September 17, 2021 10:27 AM
To: Greg Kroah-Hartman <[email protected]>; Jonathan Cameron <[email protected]>; tiantao (H) <[email protected]>; Song Bao Hua (Barry Song) <[email protected]>
Cc: Andrew Morton <[email protected]>; Andy Shevchenko <[email protected]>; Yury Norov <[email protected]>; Peter Zijlstra <[email protected]>; [email protected]
Subject: [PATCH] cpumask: Omit terminating null byte in cpumap_print_{list,bitmask}_to_buf

The changes in the patch series [1] introduced a terminating null byte when reading from cpulist or cpumap sysfs files, for example:

  $ xxd /sys/devices/system/node/node0/cpulist
  00000000: 302d 310a 00                             0-1..

Before this change, the output looked as follows:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
  $ xxd /sys/devices/system/node/node0/cpulist
  00000000: 302d 310a                                0-1.

If we don't use xxd, I don't see any actual harm of this NULL byte by cat, lscpu, numactl etc. this doesn't break them at all.

Barry, Tobias' script that uses xxd is userspace. Linux kernel never breaks userspace.

Quoted 8 lines

FWIW, the example using xxd was just to illustrate the issue in a concise way for the commit message. This is breaking other userspace programs as well. Originally, I discovered this because Kubernetes' kubelet was crashing on a bpf-next kernel. See [1] and following comments for more information:

[1] https://github.com/cilium/cilium/pull/17394#issuecomment-920902042

cat, lscpu, numactl tools were tested. the above was not in the test cases. Anyway, if some apps depend on the last character, this patch makes sense. we need this one. sorry for missing the test case.

Greg, can you please help merge this one into 5.15?

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

if we only want to make sure the output is exactly same with before for every single character, this patch is right.

We don't want to make the output exactly the same. The "0,1" would also work for the example above. But garbage characters following \0 is a bug that should be fixed.

Quoted 7 lines

I think we also want to avoid the \0 itself, which is what this patch does and is in line with previous behavior. It also looks like all other sysfs files in that subtree expose the same content format (i.e. \n is the last character, not \0).

Thanks,
Tobias

Thanks
barry

#5 lore raw
Quoted 46 lines · Barry Song
Quoted 36 lines · Tobias Klauser
Quoted 25 lines · Yury Norov

[CC Greg KH <[email protected]>]

Quoted 22 lines · Song Bao Hua (Barry Song)
Quoted 22 lines

-----Original Message-----
From: Tobias Klauser [mailto:[email protected]]
Sent: Friday, September 17, 2021 10:27 AM
To: Greg Kroah-Hartman <[email protected]>; Jonathan Cameron <[email protected]>; tiantao (H) <[email protected]>; Song Bao Hua (Barry Song) <[email protected]>
Cc: Andrew Morton <[email protected]>; Andy Shevchenko <[email protected]>; Yury Norov <[email protected]>; Peter Zijlstra <[email protected]>; [email protected]
Subject: [PATCH] cpumask: Omit terminating null byte in cpumap_print_{list,bitmask}_to_buf

The changes in the patch series [1] introduced a terminating null byte when reading from cpulist or cpumap sysfs files, for example:

  $ xxd /sys/devices/system/node/node0/cpulist
  00000000: 302d 310a 00                             0-1..

Before this change, the output looked as follows:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
  $ xxd /sys/devices/system/node/node0/cpulist
  00000000: 302d 310a                                0-1.

If we don't use xxd, I don't see any actual harm of this NULL byte by cat, lscpu, numactl etc. this doesn't break them at all.

Barry, Tobias' script that uses xxd is userspace. Linux kernel never breaks userspace.

Quoted 8 lines

FWIW, the example using xxd was just to illustrate the issue in a concise way for the commit message. This is breaking other userspace programs as well. Originally, I discovered this because Kubernetes' kubelet was crashing on a bpf-next kernel. See [1] and following comments for more information:

[1] https://github.com/cilium/cilium/pull/17394#issuecomment-920902042

Quoted 7 lines

cat, lscpu, numactl tools were tested. the above was not in the test cases. Anyway, if some apps depend on the last character, this patch makes sense. we need this one. sorry for missing the test case.

Greg, can you please help merge this one into 5.15?

Yes, will apply it to my tree soon.

thanks,

greg k-h

#6 lore raw
In reply to Tobias Klauser

An original function, bitmap_print_to_pagebuf() in lib/bitmap.c, returns the number of printed characters, excluding terminating null.

Commit 1fae5629, a cause of this regression, introduced new functions to lib/bitmap.c:

- bitmap_print_to_buf()
(return value doc missing)

- bitmap_print_bitmask_to_buf()
(return value doc not explicit about terminating null, but can be considered misleading)

- bitmap_print_list_to_buf()
(the same as above)

Unlike the original function, the return value of new functions include the terminating null.

As this behavior is clearly opposite to the original function, and functions that print to buffers in general, I would suggest fixing this problem by alignign these new functions with the original one:
excluding the terminating null. And documenting this behavior unambiguously.

The suggested change to cpumask_print_{bitmask,list}_to_buf() functions decrements possible errors (like -ENOMEM) returned by bitmap_print_to_buf(). This must not happen.

#7 lore raw
Quoted 26 lines · Antti Kervinen

An original function, bitmap_print_to_pagebuf() in lib/bitmap.c, returns the number of printed characters, excluding terminating null.

Commit 1fae5629, a cause of this regression, introduced new functions to lib/bitmap.c:

- bitmap_print_to_buf()
(return value doc missing)

- bitmap_print_bitmask_to_buf()
(return value doc not explicit about terminating null, but can be considered misleading)

- bitmap_print_list_to_buf()
(the same as above)

Unlike the original function, the return value of new functions include the terminating null.

As this behavior is clearly opposite to the original function, and functions that print to buffers in general, I would suggest fixing this problem by alignign these new functions with the original one:
excluding the terminating null. And documenting this behavior unambiguously.

Quoted 3 lines

The suggested change to cpumask_print_{bitmask,list}_to_buf() functions decrements possible errors (like -ENOMEM) returned by bitmap_print_to_buf(). This must not happen.

I already pointed you at

	https://lore.kernel.org/r/[email protected]

a few hours ago.

Why not test the patch there (and in linux-next) and let us know if it resolves the issue you see or not.

thanks,

greg k-h

#8 lore raw
Quoted 3 lines

Why not test the patch there (and in linux-next) and let us know if it resolves the issue you see or not.

Yes, I conform that the patch fixes this issue in my userspace. Too bad that I was obviously late with my code review notes.

Thanks!

Antti

#9 lore raw
In reply to Antti Kervinen
Quoted 5 lines · Antti Kervinen

An original function, bitmap_print_to_pagebuf() in lib/bitmap.c, returns the number of printed characters, excluding terminating null.

a patch has been in
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/?id=c86a2d9058c5a4a05d20ef89e699b7a6b2c89da6

I guess it is on the way to linus' tree.

Quoted 22 lines

Commit 1fae5629, a cause of this regression, introduced new functions to lib/bitmap.c:

- bitmap_print_to_buf()
(return value doc missing)

- bitmap_print_bitmask_to_buf()
(return value doc not explicit about terminating null, but can be considered misleading)

- bitmap_print_list_to_buf()
(the same as above)

Unlike the original function, the return value of new functions include the terminating null.

As this behavior is clearly opposite to the original function, and functions that print to buffers in general, I would suggest fixing this problem by alignign these new functions with the original one:
excluding the terminating null. And documenting this behavior unambiguously.

Quoted 3 lines

The suggested change to cpumask_print_{bitmask,list}_to_buf() functions decrements possible errors (like -ENOMEM) returned by bitmap_print_to_buf(). This must not happen.

Thanks
barry