From c666d447e091be3a742588b49290e7733115769f Mon Sep 17 00:00:00 2001 From: "Matthew Wilcox (Oracle)" Date: Tue, 19 Oct 2021 15:26:17 +0100 Subject: test_printf: Make pft array const Instead of assigning ptf[i].value, leave the values in the on-stack array and then we can make the array const. Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: Yafang Shao Reviewed-by: Petr Mladek Reviewed-by: Anshuman Khandual Signed-off-by: Petr Mladek Link: https://lore.kernel.org/r/20211019142621.2810043-2-willy@infradead.org --- lib/test_printf.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/test_printf.c b/lib/test_printf.c index 55082432f37e..a52c1c3a55ba 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -586,22 +586,21 @@ struct page_flags_test { int width; int shift; int mask; - unsigned long value; const char *fmt; const char *name; }; -static struct page_flags_test pft[] = { +static const struct page_flags_test pft[] = { {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK, - 0, "%d", "section"}, + "%d", "section"}, {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK, - 0, "%d", "node"}, + "%d", "node"}, {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK, - 0, "%d", "zone"}, + "%d", "zone"}, {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK, - 0, "%#x", "lastcpupid"}, + "%#x", "lastcpupid"}, {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK, - 0, "%#x", "kasantag"}, + "%#x", "kasantag"}, }; static void __init @@ -627,10 +626,6 @@ page_flags_test(int section, int node, int zone, int last_cpupid, #endif } - /* Set the test value */ - for (i = 0; i < ARRAY_SIZE(pft); i++) - pft[i].value = values[i]; - for (i = 0; i < ARRAY_SIZE(pft); i++) { if (!pft[i].width) continue; @@ -640,11 +635,11 @@ page_flags_test(int section, int node, int zone, int last_cpupid, size = strlen(cmp_buf); } - page_flags |= (pft[i].value & pft[i].mask) << pft[i].shift; + page_flags |= (values[i] & pft[i].mask) << pft[i].shift; snprintf(cmp_buf + size, BUF_SIZE - size, "%s=", pft[i].name); size = strlen(cmp_buf); snprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt, - pft[i].value & pft[i].mask); + values[i] & pft[i].mask); size = strlen(cmp_buf); append = true; } -- cgit From a25a0854a2264a0c592ba1ea01a165101f8c1a6c Mon Sep 17 00:00:00 2001 From: "Matthew Wilcox (Oracle)" Date: Tue, 19 Oct 2021 15:26:18 +0100 Subject: test_printf: Remove separate page_flags variable Keep flags intact so that we also test what happens when unknown flags are passed to %pGp. Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: Yafang Shao Reviewed-by: Petr Mladek Signed-off-by: Petr Mladek Link: https://lore.kernel.org/r/20211019142621.2810043-3-willy@infradead.org --- lib/test_printf.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/test_printf.c b/lib/test_printf.c index a52c1c3a55ba..4531063afd45 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -605,17 +605,15 @@ static const struct page_flags_test pft[] = { static void __init page_flags_test(int section, int node, int zone, int last_cpupid, - int kasan_tag, int flags, const char *name, char *cmp_buf) + int kasan_tag, unsigned long flags, const char *name, + char *cmp_buf) { unsigned long values[] = {section, node, zone, last_cpupid, kasan_tag}; - unsigned long page_flags = 0; unsigned long size = 0; bool append = false; int i; - flags &= PAGEFLAGS_MASK; - if (flags) { - page_flags |= flags; + if (flags & PAGEFLAGS_MASK) { snprintf(cmp_buf + size, BUF_SIZE - size, "%s", name); size = strlen(cmp_buf); #if SECTIONS_WIDTH || NODES_WIDTH || ZONES_WIDTH || \ @@ -635,7 +633,7 @@ page_flags_test(int section, int node, int zone, int last_cpupid, size = strlen(cmp_buf); } - page_flags |= (values[i] & pft[i].mask) << pft[i].shift; + flags |= (values[i] & pft[i].mask) << pft[i].shift; snprintf(cmp_buf + size, BUF_SIZE - size, "%s=", pft[i].name); size = strlen(cmp_buf); snprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt, @@ -644,7 +642,7 @@ page_flags_test(int section, int node, int zone, int last_cpupid, append = true; } - test(cmp_buf, "%pGp", &page_flags); + test(cmp_buf, "%pGp", &flags); } static void __init -- cgit From 5b358b0de963f822226bfee916fb53c80bae4000 Mon Sep 17 00:00:00 2001 From: "Matthew Wilcox (Oracle)" Date: Tue, 19 Oct 2021 15:26:19 +0100 Subject: test_printf: Remove custom appending of '|' Instead of having an ifdef to decide whether to print a |, use the 'append' functionality of the main loop to print it. Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: Yafang Shao Reviewed-by: Petr Mladek Reviewed-by: Anshuman Khandual Signed-off-by: Petr Mladek Link: https://lore.kernel.org/r/20211019142621.2810043-4-willy@infradead.org --- lib/test_printf.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/test_printf.c b/lib/test_printf.c index 4531063afd45..ec584196cb99 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -616,12 +616,7 @@ page_flags_test(int section, int node, int zone, int last_cpupid, if (flags & PAGEFLAGS_MASK) { snprintf(cmp_buf + size, BUF_SIZE - size, "%s", name); size = strlen(cmp_buf); -#if SECTIONS_WIDTH || NODES_WIDTH || ZONES_WIDTH || \ - LAST_CPUPID_WIDTH || KASAN_TAG_WIDTH - /* Other information also included in page flags */ - snprintf(cmp_buf + size, BUF_SIZE - size, "|"); - size = strlen(cmp_buf); -#endif + append = true; } for (i = 0; i < ARRAY_SIZE(pft); i++) { -- cgit From 507f98603607d43cb76ed39c370c4dc1ed6a94f9 Mon Sep 17 00:00:00 2001 From: "Matthew Wilcox (Oracle)" Date: Tue, 19 Oct 2021 15:26:20 +0100 Subject: test_printf: Append strings more efficiently Use scnprintf instead of snprintf + strlen. Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: Petr Mladek Reviewed-by: Yafang Shao Signed-off-by: Petr Mladek Link: https://lore.kernel.org/r/20211019142621.2810043-5-willy@infradead.org --- lib/test_printf.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/test_printf.c b/lib/test_printf.c index ec584196cb99..d09993fca463 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -614,8 +614,7 @@ page_flags_test(int section, int node, int zone, int last_cpupid, int i; if (flags & PAGEFLAGS_MASK) { - snprintf(cmp_buf + size, BUF_SIZE - size, "%s", name); - size = strlen(cmp_buf); + size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name); append = true; } @@ -623,17 +622,14 @@ page_flags_test(int section, int node, int zone, int last_cpupid, if (!pft[i].width) continue; - if (append) { - snprintf(cmp_buf + size, BUF_SIZE - size, "|"); - size = strlen(cmp_buf); - } + if (append) + size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|"); flags |= (values[i] & pft[i].mask) << pft[i].shift; - snprintf(cmp_buf + size, BUF_SIZE - size, "%s=", pft[i].name); - size = strlen(cmp_buf); - snprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt, - values[i] & pft[i].mask); - size = strlen(cmp_buf); + size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=", + pft[i].name); + size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt, + values[i] & pft[i].mask); append = true; } -- cgit From 23efd0804c0a869dfb1e78470f80a27251317b7e Mon Sep 17 00:00:00 2001 From: "Matthew Wilcox (Oracle)" Date: Tue, 19 Oct 2021 15:26:21 +0100 Subject: vsprintf: Make %pGp print the hex value All existing users of %pGp want the hex value as well as the decoded flag names. This looks awkward (passing the same parameter to printf twice), so move that functionality into the core. If we want, we can make that optional with flag arguments to %pGp in the future. Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: Yafang Shao Reviewed-by: Petr Mladek Signed-off-by: Petr Mladek Link: https://lore.kernel.org/r/20211019142621.2810043-6-willy@infradead.org --- lib/test_printf.c | 9 +++++++-- lib/vsprintf.c | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/test_printf.c b/lib/test_printf.c index d09993fca463..07309c45f327 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -609,10 +609,14 @@ page_flags_test(int section, int node, int zone, int last_cpupid, char *cmp_buf) { unsigned long values[] = {section, node, zone, last_cpupid, kasan_tag}; - unsigned long size = 0; + unsigned long size; bool append = false; int i; + for (i = 0; i < ARRAY_SIZE(values); i++) + flags |= (values[i] & pft[i].mask) << pft[i].shift; + + size = scnprintf(cmp_buf, BUF_SIZE, "%#lx(", flags); if (flags & PAGEFLAGS_MASK) { size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name); append = true; @@ -625,7 +629,6 @@ page_flags_test(int section, int node, int zone, int last_cpupid, if (append) size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|"); - flags |= (values[i] & pft[i].mask) << pft[i].shift; size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=", pft[i].name); size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt, @@ -633,6 +636,8 @@ page_flags_test(int section, int node, int zone, int last_cpupid, append = true; } + snprintf(cmp_buf + size, BUF_SIZE - size, ")"); + test(cmp_buf, "%pGp", &flags); } diff --git a/lib/vsprintf.c b/lib/vsprintf.c index d7ad44f2c8f5..214098248610 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2023,6 +2023,11 @@ char *format_page_flags(char *buf, char *end, unsigned long flags) bool append = false; int i; + buf = number(buf, end, flags, default_flag_spec); + if (buf < end) + *buf = '('; + buf++; + /* Page flags from the main area. */ if (main_flags) { buf = format_flags(buf, end, main_flags, pageflag_names); @@ -2051,6 +2056,9 @@ char *format_page_flags(char *buf, char *end, unsigned long flags) append = true; } + if (buf < end) + *buf = ')'; + buf++; return buf; } -- cgit From 24a1dffbecafeb00d8830985eb7a318e37aabc4e Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Tue, 26 Oct 2021 17:03:56 +0300 Subject: lib/vsprintf.c: Amend static asserts for format specifier flags There are couple of improvements to static asserts against the format specifier flags: - new static assert for SIGN - fix static assert for SMALL SMALL is not equal to ASCII code of white space, it equals to the bit difference between capital and small letters (however the value is the same, semantically expression means different things). Signed-off-by: Andy Shevchenko Signed-off-by: Petr Mladek Link: https://lore.kernel.org/r/20211026140356.45610-1-andriy.shevchenko@linux.intel.com --- lib/vsprintf.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 26c83943748a..1173930ed9d3 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -408,8 +408,9 @@ int num_to_str(char *buf, int size, unsigned long long num, unsigned int width) #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ +static_assert(SIGN == 1); static_assert(ZEROPAD == ('0' - ' ')); -static_assert(SMALL == ' '); +static_assert(SMALL == ('a' ^ 'A')); enum format_type { FORMAT_TYPE_NONE, /* Just a string part */ -- cgit