Skip to contentSkip to conversation

docs: printk-formats: Fix hex printing of signed values

PATCH In review Andy Shevchenko 2 participants 7 messages Jul 3, 2023 – Jul 18, 2023

Subsystems (2)
lore
Share & tools
lore

Andy Shevchenko

author reviewer
#0 lore raw

PATCH 1/2 docs: printk-formats: Fix hex printing of signed values

The commit cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use of unnecessary %h[xudi] and %hh[xudi]") obviously missed the point of sign promotion for the signed values lesser than int. In such case %x prints not the same as %h[h]x. Restore back those specifiers for the signed hex cases.

Fixes cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use of unnecessary %h[xudi] and %hh[xudi]")
---
 Documentation/core-api/printk-formats.rst | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Documentation/core-api/printk-formats.rst +4 -4
index dfe7e75a71de..2f3a6e0b3fc4 100644
@@ -15,9 +15,9 @@ Integer types
 
 	If variable is of Type,		use printk format specifier:
 	------------------------------------------------------------
-		char			%d or %x
+		char			%d or %hhx
 		unsigned char		%u or %x
-		short int		%d or %x
+		short int		%d or %hx
 		unsigned short int	%u or %x
 		int			%d or %x
 		unsigned int		%u or %x
@@ -27,9 +27,9 @@ Integer types
 		unsigned long long	%llu or %llx
 		size_t			%zu or %zx
 		ssize_t			%zd or %zx
-		s8			%d or %x
+		s8			%d or %hhx
 		u8			%u or %x
-		s16			%d or %x
+		s16			%d or %hx
 		u16			%u or %x
 		s32			%d or %x
 		u32			%u or %x
Signature · 1 line
2.40.0.1.gaa8946217a0b
author reviewer
#1 lore raw

PATCH 2/2 docs: printk-formats: Treat char as always unsigned

The Linux kernel switched to have char be equivalent to usigned char. Reflect this in the printk specifiers.

Fixes 3bc753c06dd0 ("kbuild: treat char as always unsigned")
---
 Documentation/core-api/printk-formats.rst | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Documentation/core-api/printk-formats.rst +2 -1
index 2f3a6e0b3fc4..4451ef501936 100644
@@ -15,8 +15,9 @@ Integer types
 
 	If variable is of Type,		use printk format specifier:
 	------------------------------------------------------------
-		char			%d or %hhx
+		signed char		%d or %hhx
 		unsigned char		%u or %x
+		char			%u or %x
 		short int		%d or %hx
 		unsigned short int	%u or %x
 		int			%d or %x
Signature · 1 line
2.40.0.1.gaa8946217a0b
maintainer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH v1 2/2] docs: printk-formats: Treat char as always unsigned

Quoted 5 lines · Andy Shevchenko

The Linux kernel switched to have char be equivalent to usigned char. Reflect this in the printk specifiers.

Fixes 3bc753c06dd0 ("kbuild: treat char as always unsigned")

Makes sense.

Best Regards,
Petr

PS: I am going to queue both patches for 6.6. They are not
urgent to be added in the -rc phase.

author reviewer
#3 lore raw

Re: [PATCH v1 2/2] docs: printk-formats: Treat char as always unsigned

Quoted 1 line · Petr Mladek

On Mon 2023-07-03 17:58:39, Andy Shevchenko wrote:

...

Quoted 1 line

Thank you for the review!

Quoted 2 lines

PS: I am going to queue both patches for 6.6. They are not
urgent to be added in the -rc phase.

Sure, works for me.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#4 lore raw

Re: [PATCH v1 2/2] docs: printk-formats: Treat char as always unsigned

Quoted 7 lines · Andy Shevchenko
Quoted 4 lines · Petr Mladek

On Mon 2023-07-03 17:58:39, Andy Shevchenko wrote:

PS: I am going to queue both patches for 6.6. They are not
urgent to be added in the -rc phase.

Sure, works for me.

JFYI, both patches have been pushed into printk/linux.git, branch for-6.6-vsprintf-doc.

Best Regards,
Petr

author reviewer
#5 lore raw
In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko

The commit cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use of unnecessary %h[xudi] and %hh[xudi]") obviously missed the point of sign promotion for the signed values lesser than int. In such case %x prints not the same as %h[h]x. Restore back those specifiers for the signed hex cases.

FWIW, currently we luckily have only one ABI case drivers/power/supply/bq24190_charger.c:466 which luckily uses unsigned value for that specifier.

The rest is debug and error messages along with tracepoints. The tracepoints might have an undesired, but not critical, deviation. Nevertheless, promoting sign for hex values seems to me the wrong suggestion.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer Reviewed-by on v1 1/2
#6 lore raw
In reply to Andy Shevchenko
Quoted 8 lines · Andy Shevchenko

The commit cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use of unnecessary %h[xudi] and %hh[xudi]") obviously missed the point of sign promotion for the signed values lesser than int. In such case %x prints not the same as %h[h]x. Restore back those specifiers for the signed hex cases.

Fixes cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use of unnecessary %h[xudi] and %hh[xudi]")

Makes sense. %d would print the same (small) negative number even when casted do int. But %x would always add more "ff"s up to 4 bytes.

Best Regards,
Petr