Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bitmap: remove explicit newline handling using scnprintf format string

bitmap_print_to_pagebuf uses scnprintf to copy the cpumask/list to page
buffer. It handles the newline and trailing null character explicitly.

It's unnecessary and also partially duplicated as scnprintf already adds
trailing null character. The newline can be passed through format
string to scnprintf. This patch does that simplification.

However theoretically there's one behavior difference: when the buffer
is too small, the original code would still output '\n' at the end while
the new code(with this patch) would just continue to print the formatted
string. Since this function is dealing with only page buffers, it's
highly unlikely to hit that corner case.

This patch will help in auditing the users of bitmap_print_to_pagebuf to
verify that the buffer passed is large enough and get rid of it
completely by replacing them with direct scnprintf()

[akpm@linux-foundation.org: tweak comment]
Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
Suggested-by: Pawel Moll <Pawel.Moll@arm.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: "Peter Zijlstra (Intel)" <peterz@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Sudeep Holla and committed by
Linus Torvalds
9cf79d11 ca96ab85

+8 -7
+8 -7
lib/bitmap.c
··· 462 462 * Output format is a comma-separated list of decimal numbers and 463 463 * ranges if list is specified or hex digits grouped into comma-separated 464 464 * sets of 8 digits/set. Returns the number of characters written to buf. 465 + * 466 + * It is assumed that @buf is a pointer into a PAGE_SIZE area and that 467 + * sufficient storage remains at @buf to accommodate the 468 + * bitmap_print_to_pagebuf() output. 465 469 */ 466 470 int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp, 467 471 int nmaskbits) 468 472 { 469 - ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf - 2; 473 + ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf; 470 474 int n = 0; 471 475 472 - if (len > 1) { 473 - n = list ? scnprintf(buf, len, "%*pbl", nmaskbits, maskp) : 474 - scnprintf(buf, len, "%*pb", nmaskbits, maskp); 475 - buf[n++] = '\n'; 476 - buf[n] = '\0'; 477 - } 476 + if (len > 1) 477 + n = list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) : 478 + scnprintf(buf, len, "%*pb\n", nmaskbits, maskp); 478 479 return n; 479 480 } 480 481 EXPORT_SYMBOL(bitmap_print_to_pagebuf);