All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hungyu Lin <dennylin0707@gmail.com>
To: yury.norov@gmail.com
Cc: linux@rasmusvillemoes.dk, tklauser@distanz.ch,
	linux-kernel@vger.kernel.org, Hungyu Lin <dennylin0707@gmail.com>
Subject: [PATCH] cpumask: Preserve short reads in cpumap printing helpers
Date: Tue, 20 Oct 2026 17:01:26 +0000	[thread overview]
Message-ID: <20261020170126.15428-1-dennylin0707@gmail.com> (raw)

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")
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
 include/linux/cpumask.h | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index 4c8bb6953107..2caeae982faf 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -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

             reply	other threads:[~2026-10-20 17:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-20 17:01 Hungyu Lin [this message]
2026-10-21 14:42 ` [PATCH] cpumask: Preserve short reads in cpumap printing helpers Tobias Klauser
2026-10-28 15:25 ` Yury Norov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261020170126.15428-1-dennylin0707@gmail.com \
    --to=dennylin0707@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=tklauser@distanz.ch \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.