Skip to contentSkip to conversation

lib/test_printf: Mark big constant with ULL

PATCH Superseded Andy Shevchenko 1 participant 8 messages Feb 16, 2018

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Andy Shevchenko

author reviewer
#0 lore raw

PATCH 1/7 lib/test_printf: Mark big constant with ULL

Sparse complains that constant is so bit for unsigned long on 64-bit architecture.

lib/test_printf.c:217:54: warning: constant 0xffff0123456789ab is so big it is unsigned long lib/test_printf.c:246:54: warning: constant 0xffff0123456789ab is so big it is unsigned long

To satisfy everyone, mark the constant with ULL.

---
 lib/test_printf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/test_printf.c +1 -1
index 71ebfa43ad05..309cf8d7e6d4 100644
@@ -204,7 +204,7 @@ test_string(void)
 #if BITS_PER_LONG == 64
 
 #define PTR_WIDTH 16
-#define PTR ((void *)0xffff0123456789ab)
+#define PTR ((void *)0xffff0123456789abULL)
 #define PTR_STR "ffff0123456789ab"
 #define ZEROS "00000000"	/* hex 32 zero bits */
 
Signature · 1 line
2.15.1
author reviewer
#1 lore raw

PATCH 2/7 lib/vsprintf: Deduplicate pointer_string()

There is an exact code at the end of ptr_to_id().
Replace it by calling pointer_string() directly.

This is followup to the commit
ad67b74d2469 ("printk: hash addresses printed with %p").

Cc Tobin C. Harding <[email protected]>
---
 lib/vsprintf.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)
lib/vsprintf.c +2 -11
index d7a708f82559..b6d254723828 100644
@@ -1697,10 +1697,9 @@ early_initcall(initialize_ptr_random);
 static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
 {
 	unsigned long hashval;
-	const int default_width = 2 * sizeof(ptr);
 
 	if (unlikely(!have_filled_random_ptr_key)) {
-		spec.field_width = default_width;
+		spec.field_width = 2 * sizeof(ptr);
 		/* string length must be less than default_width */
 		return string(buf, end, "(ptrval)", spec);
 	}
@@ -1715,15 +1714,7 @@ static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
 #else
 	hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
 #endif
-
-	spec.flags |= SMALL;
-	if (spec.field_width == -1) {
-		spec.field_width = default_width;
-		spec.flags |= ZEROPAD;
-	}
-	spec.base = 16;
-
-	return number(buf, end, hashval, spec);
+	return pointer_string(buf, end, (const void *)hashval, spec);
 }
 
 /*
Signature · 1 line
2.15.1
author reviewer
#2 lore raw

PATCH 3/7 lib/vsprintf: Replace ' ' with '_' before crng is ready

In reply to Andy Shevchenko

From: Shunyong Yang <[email protected]>

Before crng is ready, output of "%p" composes of "(ptrval)" and left padding spaces for alignment as no random address can be generated. This seems a little strange when default string width is larger than strlen("(ptrval)").

For example, when irq domain names are built with "%p", the nodes under /sys/kernel/debug/irq/domains like this on AArch64 system,

[root@y irq]# ls domains/
default irqchip@ (ptrval)-2
irqchip@ (ptrval)-4 \_SB_.TCS0.QIC1 \_SB_.TCS0.QIC3
irqchip@ (ptrval) irqchip@ (ptrval)-3
\_SB_.TCS0.QIC0 \_SB_.TCS0.QIC2

The name "irqchip@ (ptrval)-2" is not so readable in console
output.

This patch replaces space with readable "=" when output needs padding. Following is the output after applying the patch,

[root@y domains]# ls
default irqchip@(____ptrval____)-2
irqchip@(____ptrval____)-4 \_SB_.TCS0.QIC1 \_SB_.TCS0.QIC3 irqchip@(____ptrval____) irqchip@(____ptrval____)-3 \_SB_.TCS0.QIC0 \_SB_.TCS0.QIC2

There is same problem in some subsystem's dmesg output. Moreover, someone may call "%p" in a similar case. In addition, the timing of crng initialization done may vary on different system. So, the change is made in vsprintf.c.

---
 lib/vsprintf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/vsprintf.c +2 -1
index b6d254723828..360e751305bf 100644
@@ -1696,12 +1696,13 @@ early_initcall(initialize_ptr_random);
 /* Maps a pointer to a 32 bit unique identifier. */
 static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
 {
+	const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
 	unsigned long hashval;
 
 	if (unlikely(!have_filled_random_ptr_key)) {
 		spec.field_width = 2 * sizeof(ptr);
 		/* string length must be less than default_width */
-		return string(buf, end, "(ptrval)", spec);
+		return string(buf, end, str, spec);
 	}
 
 #ifdef CONFIG_64BIT
Signature · 1 line
2.15.1
author reviewer
#3 lore raw

PATCH 4/7 lib/vsprintf: Remove useless NULL checks

In reply to Andy Shevchenko

The pointer can't be NULL since it's first what has been done in the pointer().

Remove useless checks.

Note we leave check for !CONFIG_HAVE_CLK to make compiler to optimize code away when possible.

---
 lib/vsprintf.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
lib/vsprintf.c +1 -12
index 360e751305bf..79f27ebdeb6e 100644
@@ -813,10 +813,6 @@ char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
 		/* nothing to print */
 		return buf;
 
-	if (ZERO_OR_NULL_PTR(addr))
-		/* NULL pointer */
-		return string(buf, end, NULL, spec);
-
 	switch (fmt[1]) {
 	case 'C':
 		separator = ':';
@@ -1255,10 +1251,6 @@ char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
 	if (spec.field_width == 0)
 		return buf;				/* nothing to print */
 
-	if (ZERO_OR_NULL_PTR(addr))
-		return string(buf, end, NULL, spec);	/* NULL pointer */
-
-
 	do {
 		switch (fmt[count++]) {
 		case 'a':
@@ -1442,7 +1434,7 @@ static noinline_for_stack
 char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
 	    const char *fmt)
 {
-	if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
+	if (!IS_ENABLED(CONFIG_HAVE_CLK))
 		return string(buf, end, NULL, spec);
 
 	switch (fmt[1]) {
@@ -1581,9 +1573,6 @@ char *device_node_string(char *buf, char *end, struct device_node *dn,
 	if (!IS_ENABLED(CONFIG_OF))
 		return string(buf, end, "(!OF)", spec);
 
-	if ((unsigned long)dn < PAGE_SIZE)
-		return string(buf, end, "(null)", spec);
-
 	/* simple case without anything any more format specifiers */
 	fmt++;
 	if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
Signature · 1 line
2.15.1
author reviewer
#4 lore raw

PATCH 5/7 lib/vsprintf: Make decspec global

In reply to Andy Shevchenko

There are places where default specification to print decimal numbers is in use.

Make it global and convert existing users.

---
 lib/vsprintf.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)
lib/vsprintf.c +9 -12
index 79f27ebdeb6e..3a02fcaf8ac8 100644
@@ -693,6 +693,11 @@ char *symbol_string(char *buf, char *end, void *ptr,
 #endif
 }
 
+static const struct printf_spec default_dec_spec = {
+	.base = 10,
+	.precision = -1,
+};
+
 static noinline_for_stack
 char *resource_string(char *buf, char *end, struct resource *res,
 		      struct printf_spec spec, const char *fmt)
@@ -722,11 +727,6 @@ char *resource_string(char *buf, char *end, struct resource *res,
 		.precision = -1,
 		.flags = SMALL | ZEROPAD,
 	};
-	static const struct printf_spec dec_spec = {
-		.base = 10,
-		.precision = -1,
-		.flags = 0,
-	};
 	static const struct printf_spec str_spec = {
 		.field_width = -1,
 		.precision = 10,
@@ -760,10 +760,10 @@ char *resource_string(char *buf, char *end, struct resource *res,
 		specp = &mem_spec;
 	} else if (res->flags & IORESOURCE_IRQ) {
 		p = string(p, pend, "irq ", str_spec);
-		specp = &dec_spec;
+		specp = &default_dec_spec;
 	} else if (res->flags & IORESOURCE_DMA) {
 		p = string(p, pend, "dma ", str_spec);
-		specp = &dec_spec;
+		specp = &default_dec_spec;
 	} else if (res->flags & IORESOURCE_BUS) {
 		p = string(p, pend, "bus ", str_spec);
 		specp = &bus_spec;
@@ -899,9 +899,6 @@ char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
 	int cur, rbot, rtop;
 	bool first = true;
 
-	/* reused to print numbers */
-	spec = (struct printf_spec){ .base = 10 };
-
 	rbot = cur = find_first_bit(bitmap, nr_bits);
 	while (cur < nr_bits) {
 		rtop = cur;
@@ -916,13 +913,13 @@ char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
 		}
 		first = false;
 
-		buf = number(buf, end, rbot, spec);
+		buf = number(buf, end, rbot, default_dec_spec);
 		if (rbot < rtop) {
 			if (buf < end)
 				*buf = '-';
 			buf++;
 
-			buf = number(buf, end, rtop, spec);
+			buf = number(buf, end, rtop, default_dec_spec);
 		}
 
 		rbot = cur;
Signature · 1 line
2.15.1
author reviewer
#5 lore raw

PATCH 6/7 lib/vsprintf: Make strspec global

In reply to Andy Shevchenko

There is at least one new user is coming where default specification to print strings is in use.

Make it global.

---
 lib/vsprintf.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
lib/vsprintf.c +6 -5
index 3a02fcaf8ac8..20c0ab9faba5 100644
@@ -693,6 +693,11 @@ char *symbol_string(char *buf, char *end, void *ptr,
 #endif
 }
 
+static const struct printf_spec default_str_spec = {
+	.field_width = -1,
+	.precision = -1,
+};
+
 static const struct printf_spec default_dec_spec = {
 	.base = 10,
 	.precision = -1,
@@ -1453,10 +1458,6 @@ char *format_flags(char *buf, char *end, unsigned long flags,
 					const struct trace_print_flags *names)
 {
 	unsigned long mask;
-	const struct printf_spec strspec = {
-		.field_width = -1,
-		.precision = -1,
-	};
 	const struct printf_spec numspec = {
 		.flags = SPECIAL|SMALL,
 		.field_width = -1,
@@ -1469,7 +1470,7 @@ char *format_flags(char *buf, char *end, unsigned long flags,
 		if ((flags & mask) != mask)
 			continue;
 
-		buf = string(buf, end, names->name, strspec);
+		buf = string(buf, end, names->name, default_str_spec);
 
 		flags &= ~mask;
 		if (flags) {
Signature · 1 line
2.15.1
author reviewer
#6 lore raw

PATCH 7/7 lib/vsprintf: Mark expected switch fall-through

In reply to Andy Shevchenko

In preparation to enabling -Wimplicit-fallthrough, mark switch cases where we are expecting to fall through.

---
 lib/vsprintf.c | 3 +++
 1 file changed, 3 insertions(+)
lib/vsprintf.c +3 -0
index 20c0ab9faba5..bf0c45788100 100644
@@ -2094,6 +2094,7 @@ int format_decode(const char *fmt, struct printf_spec *spec)
 
 	case 'x':
 		spec->flags |= SMALL;
+		/* fall through */
 
 	case 'X':
 		spec->base = 16;
@@ -3048,8 +3049,10 @@ int vsscanf(const char *buf, const char *fmt, va_list args)
 			break;
 		case 'i':
 			base = 0;
+			/* fall through */
 		case 'd':
 			is_sign = true;
+			/* fall through */
 		case 'u':
 			break;
 		case '%':
Signature · 1 line
2.15.1
author reviewer
#7 lore raw
In reply to Andy Shevchenko
Quoted 10 lines · Andy Shevchenko

Sparse complains that constant is so bit for unsigned long on 64-bit architecture.

lib/test_printf.c:217:54: warning: constant 0xffff0123456789ab is so big it is unsigned long
lib/test_printf.c:246:54: warning: constant 0xffff0123456789ab is so big it is unsigned long

To satisfy everyone, mark the constant with ULL.

Please, drop entire series.

I found more candidates inside the file which can utilize changes. Besides that, there is a typo in commit message of patch 3.

Quoted 18 lines
---
 lib/test_printf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/test_printf.c +1 -1
index 71ebfa43ad05..309cf8d7e6d4 100644
@@ -204,7 +204,7 @@ test_string(void)
 #if BITS_PER_LONG == 64
 
 #define PTR_WIDTH 16
-#define PTR ((void *)0xffff0123456789ab)
+#define PTR ((void *)0xffff0123456789abULL)
 #define PTR_STR "ffff0123456789ab"
 #define ZEROS "00000000"	/* hex 32 zero bits */
 
Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy