[PATCH 0/5] vsprintf and uses: Add upper case output to %*ph extension

STALE1837d

12 messages, 4 authors, 2021-08-28 · open the first message on its own page

[PATCH 0/5] vsprintf and uses: Add upper case output to %*ph extension

From: Joe Perches <joe@perches.com>
Date: 2021-08-26 18:43:13

Several sysfs uses that could use %*ph are upper case hex output.
Add a flag to the short hex formatting routine in vsprintf to support them.
Add documentation too.

Joe Perches (5):
  vsprintf/Documentation: Add X to %*ph extension to output upper case hex
  scsi: aacraid: Use vsprintf %phNX extension
  scsi: hpsa: Use vsprintf %phNX extension
  scsi: smartpqi: Use vsprintf %phNX extension
  staging: r8188eu: Use vsprintf extension %phCX to format a copy_to_user string

 Documentation/core-api/printk-formats.rst    |  6 +++
 drivers/scsi/aacraid/linit.c                 |  7 +---
 drivers/scsi/hpsa.c                          |  8 +---
 drivers/scsi/smartpqi/smartpqi_init.c        |  8 +---
 drivers/staging/r8188eu/os_dep/ioctl_linux.c |  9 ++---
 lib/vsprintf.c                               | 42 ++++++++++++--------
 6 files changed, 37 insertions(+), 43 deletions(-)

-- 
2.30.0

[PATCH 1/5] vsprintf/Documentation: Add X to %*ph extension to output upper case hex

From: Joe Perches <joe@perches.com>
Date: 2021-08-26 18:43:15

A few sysfs output uses of hex arrays are uppercase and are nominally ABI.

Add a mechanism to the existing vsprintf %*ph hex output extension to
support upper case hex output.

Signed-off-by: Joe Perches <joe@perches.com>
---
 Documentation/core-api/printk-formats.rst |  6 ++++
 lib/vsprintf.c                            | 42 ++++++++++++++---------
 2 files changed, 31 insertions(+), 17 deletions(-)
diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst
index e08bbe9b0cbf3..ca750274992e6 100644
--- a/Documentation/core-api/printk-formats.rst
+++ b/Documentation/core-api/printk-formats.rst
@@ -284,10 +284,16 @@ Raw buffer as a hex string
 
 ::
 
+	The preferred output is lowercase
 	%*ph	00 01 02  ...  3f
 	%*phC	00:01:02: ... :3f
 	%*phD	00-01-02- ... -3f
 	%*phN	000102 ... 3f
+	Formats with X are uppercase, used for backwards compatibility
+	%*phX	00 01 02  ...  3F
+	%*phCX	00:01:02: ... :3F
+	%*phDX	00-01-02- ... -3F
+	%*phNX	000102 ... 3F
 
 For printing small buffers (up to 64 bytes long) as a hex string with a
 certain separator. For larger buffers consider using
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 134216c45980e..5c22a07bbe3a7 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1147,7 +1147,10 @@ char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
 {
 	int i, len = 1;		/* if we pass '%ph[CDN]', field width remains
 				   negative value, fallback to the default */
-	char separator;
+	char separator = ' ';
+	int count = 1;
+	bool found = true;
+	char locase = 0x20;	/* ASCII OR'd for lower case see: number() */
 
 	if (spec.field_width == 0)
 		/* nothing to print */
@@ -1156,30 +1159,35 @@ char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
 	if (check_pointer(&buf, end, addr, spec))
 		return buf;
 
-	switch (fmt[1]) {
-	case 'C':
-		separator = ':';
-		break;
-	case 'D':
-		separator = '-';
-		break;
-	case 'N':
-		separator = 0;
-		break;
-	default:
-		separator = ' ';
-		break;
-	}
+	do {
+		switch (fmt[count++]) {
+		case 'C':
+			separator = ':';
+			break;
+		case 'D':
+			separator = '-';
+			break;
+		case 'N':
+			separator = 0;
+			break;
+		case 'X':
+			locase = 0;
+			break;
+		default:
+			found = false;
+			break;
+		}
+	} while (found);
 
 	if (spec.field_width > 0)
 		len = min_t(int, spec.field_width, 64);
 
 	for (i = 0; i < len; ++i) {
 		if (buf < end)
-			*buf = hex_asc_hi(addr[i]);
+			*buf = hex_asc_upper_hi(addr[i]) | locase;
 		++buf;
 		if (buf < end)
-			*buf = hex_asc_lo(addr[i]);
+			*buf = hex_asc_upper_lo(addr[i]) | locase;
 		++buf;
 
 		if (separator && i != len - 1) {
-- 
2.30.0

Re: [PATCH 1/5] vsprintf/Documentation: Add X to %*ph extension to output upper case hex

From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Date: 2021-08-27 07:48:47

On Thu, Aug 26, 2021 at 11:43:01AM -0700, Joe Perches wrote:
A few sysfs output uses of hex arrays are uppercase and are nominally ABI.

Add a mechanism to the existing vsprintf %*ph hex output extension to
support upper case hex output.
...
+	The preferred output is lowercase
 	%*ph	00 01 02  ...  3f
 	%*phC	00:01:02: ... :3f
 	%*phD	00-01-02- ... -3f
 	%*phN	000102 ... 3f
+	Formats with X are uppercase, used for backwards compatibility
+	%*phX	00 01 02  ...  3F
+	%*phCX	00:01:02: ... :3F
+	%*phDX	00-01-02- ... -3F
+	%*phNX	000102 ... 3F
Why not using %*pH...?

...
+	char locase = 0x20;	/* ASCII OR'd for lower case see: number() */
If you use h vs H, you may derive this from (fmt[...] & SMALL).

-- 
With Best Regards,
Andy Shevchenko

Re: [PATCH 0/5] vsprintf and uses: Add upper case output to %*ph extension

From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Date: 2021-08-27 07:52:10

On Thu, Aug 26, 2021 at 11:43:00AM -0700, Joe Perches wrote:
Several sysfs uses that could use %*ph are upper case hex output.
Add a flag to the short hex formatting routine in vsprintf to support them.
Add documentation too.
Thanks!

Unfortunately I have got only first patch and this cover letter. Can you,
please, Cc entire series?

-- 
With Best Regards,
Andy Shevchenko

Re: [PATCH 1/5] vsprintf/Documentation: Add X to %*ph extension to output upper case hex

From: Joe Perches <joe@perches.com>
Date: 2021-08-27 08:08:21

On Fri, 2021-08-27 at 10:48 +0300, Andy Shevchenko wrote:
On Thu, Aug 26, 2021 at 11:43:01AM -0700, Joe Perches wrote:
quoted
A few sysfs output uses of hex arrays are uppercase and are nominally ABI.

Add a mechanism to the existing vsprintf %*ph hex output extension to
support upper case hex output.
...
quoted
+	The preferred output is lowercase
 	%*ph	00 01 02  ...  3f
 	%*phC	00:01:02: ... :3f
 	%*phD	00-01-02- ... -3f
 	%*phN	000102 ... 3f
+	Formats with X are uppercase, used for backwards compatibility
+	%*phX	00 01 02  ...  3F
+	%*phCX	00:01:02: ... :3F
+	%*phDX	00-01-02- ... -3F
+	%*phNX	000102 ... 3F
Why not using %*pH...?
I find X more intelligible.
quoted
+	char locase = 0x20;	/* ASCII OR'd for lower case see: number() */
If you use h vs H, you may derive this from (fmt[...] & SMALL).
It's not necessary to use any more of the rather limited vsprintf
extension namespace.

Re: [PATCH 0/5] vsprintf and uses: Add upper case output to %*ph extension

From: Joe Perches <joe@perches.com>
Date: 2021-08-27 08:10:49

On Fri, 2021-08-27 at 10:51 +0300, Andy Shevchenko wrote:
On Thu, Aug 26, 2021 at 11:43:00AM -0700, Joe Perches wrote:
quoted
Several sysfs uses that could use %*ph are upper case hex output.
Add a flag to the short hex formatting routine in vsprintf to support them.
Add documentation too.
Thanks!

Unfortunately I have got only first patch and this cover letter. Can you,
please, Cc entire series?
It's on lore.

https://lore.kernel.org/lkml/cover.1630003183.git.joe@perches.com/T/#u
 

Re: [PATCH 0/5] vsprintf and uses: Add upper case output to %*ph extension

From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Date: 2021-08-27 08:46:33

On Fri, Aug 27, 2021 at 01:10:41AM -0700, Joe Perches wrote:
On Fri, 2021-08-27 at 10:51 +0300, Andy Shevchenko wrote:
quoted
On Thu, Aug 26, 2021 at 11:43:00AM -0700, Joe Perches wrote:
quoted
Several sysfs uses that could use %*ph are upper case hex output.
Add a flag to the short hex formatting routine in vsprintf to support them.
Add documentation too.
Thanks!

Unfortunately I have got only first patch and this cover letter. Can you,
please, Cc entire series?
It's on lore.

https://lore.kernel.org/lkml/cover.1630003183.git.joe@perches.com/T/#u
Thanks. So, you won't me to review them in a regular way :-)

TBH, I think those examples may pretty much be safe to use small
letters always.

-- 
With Best Regards,
Andy Shevchenko

Re: [PATCH 1/5] vsprintf/Documentation: Add X to %*ph extension to output upper case hex

From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Date: 2021-08-27 08:49:17

On Fri, Aug 27, 2021 at 01:08:10AM -0700, Joe Perches wrote:
On Fri, 2021-08-27 at 10:48 +0300, Andy Shevchenko wrote:
quoted
On Thu, Aug 26, 2021 at 11:43:01AM -0700, Joe Perches wrote:
quoted
A few sysfs output uses of hex arrays are uppercase and are nominally ABI.

Add a mechanism to the existing vsprintf %*ph hex output extension to
support upper case hex output.
...
quoted
+	The preferred output is lowercase
 	%*ph	00 01 02  ...  3f
 	%*phC	00:01:02: ... :3f
 	%*phD	00-01-02- ... -3f
 	%*phN	000102 ... 3f
+	Formats with X are uppercase, used for backwards compatibility
+	%*phX	00 01 02  ...  3F
+	%*phCX	00:01:02: ... :3F
+	%*phDX	00-01-02- ... -3F
+	%*phNX	000102 ... 3F
Why not using %*pH...?
I find X more intelligible.
quoted
quoted
+	char locase = 0x20;	/* ASCII OR'd for lower case see: number() */
If you use h vs H, you may derive this from (fmt[...] & SMALL).
It's not necessary to use any more of the rather limited vsprintf
extension namespace.
I understand your concern, but %*ph is quite widely used (I guess top 1 or 2
among all %p extensions), its performance degradation with your code may affect
a lot of other users and hence a kernel as a whole.

So, that's why my proposal stays.

Of course you may provide a benchmark (btw, where are the test cases for this?)
for yours and mine variant and we will see if it makes sense to optimize.

-- 
With Best Regards,
Andy Shevchenko

Re: [PATCH 0/5] vsprintf and uses: Add upper case output to %*ph extension

From: Greg KH <gregkh@linuxfoundation.org>
Date: 2021-08-27 10:23:57

On Fri, Aug 27, 2021 at 11:46:20AM +0300, Andy Shevchenko wrote:
On Fri, Aug 27, 2021 at 01:10:41AM -0700, Joe Perches wrote:
quoted
On Fri, 2021-08-27 at 10:51 +0300, Andy Shevchenko wrote:
quoted
On Thu, Aug 26, 2021 at 11:43:00AM -0700, Joe Perches wrote:
quoted
Several sysfs uses that could use %*ph are upper case hex output.
Add a flag to the short hex formatting routine in vsprintf to support them.
Add documentation too.
Thanks!

Unfortunately I have got only first patch and this cover letter. Can you,
please, Cc entire series?
It's on lore.

https://lore.kernel.org/lkml/cover.1630003183.git.joe@perches.com/T/#u
Thanks. So, you won't me to review them in a regular way :-)

TBH, I think those examples may pretty much be safe to use small
letters always.
I agree, let's just fix the users here to use small letters instead of
adding another modifier to the kernel.

thanks,

greg k-h

Re: [PATCH 1/5] vsprintf/Documentation: Add X to %*ph extension to output upper case hex

From: Petr Mladek <pmladek@suse.com>
Date: 2021-08-27 10:49:19

On Fri 2021-08-27 11:49:07, Andy Shevchenko wrote:
On Fri, Aug 27, 2021 at 01:08:10AM -0700, Joe Perches wrote:
quoted
On Fri, 2021-08-27 at 10:48 +0300, Andy Shevchenko wrote:
quoted
On Thu, Aug 26, 2021 at 11:43:01AM -0700, Joe Perches wrote:
quoted
A few sysfs output uses of hex arrays are uppercase and are nominally ABI.

Add a mechanism to the existing vsprintf %*ph hex output extension to
support upper case hex output.
...
quoted
+	The preferred output is lowercase
 	%*ph	00 01 02  ...  3f
 	%*phC	00:01:02: ... :3f
 	%*phD	00-01-02- ... -3f
 	%*phN	000102 ... 3f
+	Formats with X are uppercase, used for backwards compatibility
+	%*phX	00 01 02  ...  3F
+	%*phCX	00:01:02: ... :3F
+	%*phDX	00-01-02- ... -3F
+	%*phNX	000102 ... 3F
Why not using %*pH...?
I though about this as well.
quoted
I find X more intelligible.
I would slightly prefer %pH. I always have problems to parse long
sequences of modifiers. So, the shorter format the better.

Of course, it means that 'H' won't be usable for another purpose.
But it will happen one day anyway. Well, this is why I do not
have strong opinion.

I am more and more convinced that we will need another approach.
Mathew Wilcox has had an idea to add support for custom callbacks
that would be able to format the string, something like:

   vsprintf("Date: %pX(%p)\n", format_date, time_stamp);

I think that it might even be possible to do something like:

   vsprintf("Date: %pX\n", format_date(time));

, where the format_date() would be a macro that would create
a struct at stack a pass it as a pointer:

#define format_date(time)			   \
({						   \
	struct vsprintf_callback c = {		   \
		.func = vsprintf_format_date,	   \
		.arg1 = time,			   \
	}					   \
						   \
	&c;					   \
})

and vsprintf would internally do something like:

char *custom_format(char *buf, char *end, vsprintf_callback *c,
			 struct printf_spec spec, const char *fmt)
{
	return c->func(buf, end, c->arg1, spec);
}

It would allow to replace all the magic %pXYZ modifiers with
self-explanatory callbacks. While still keeping it easy to use.

Best Regards,
Petr

Re: [PATCH 0/5] vsprintf and uses: Add upper case output to %*ph extension

From: Joe Perches <joe@perches.com>
Date: 2021-08-27 16:09:45

On Fri, 2021-08-27 at 12:23 +0200, Greg KH wrote:
On Fri, Aug 27, 2021 at 11:46:20AM +0300, Andy Shevchenko wrote:
quoted
On Fri, Aug 27, 2021 at 01:10:41AM -0700, Joe Perches wrote:
quoted
On Fri, 2021-08-27 at 10:51 +0300, Andy Shevchenko wrote:
quoted
On Thu, Aug 26, 2021 at 11:43:00AM -0700, Joe Perches wrote:
quoted
Several sysfs uses that could use %*ph are upper case hex output.
Add a flag to the short hex formatting routine in vsprintf to support them.
Add documentation too.
Thanks!

Unfortunately I have got only first patch and this cover letter. Can you,
please, Cc entire series?
It's on lore.

https://lore.kernel.org/lkml/cover.1630003183.git.joe@perches.com/T/#u
Thanks. So, you won't me to review them in a regular way :-)

TBH, I think those examples may pretty much be safe to use small
letters always.
I agree, let's just fix the users here to use small letters instead of
adding another modifier to the kernel.
ABI _should_ mean stability for random parsers.

I don't use these so I don't care that much.

Re: [PATCH 1/5] vsprintf/Documentation: Add X to %*ph extension to output upper case hex

From: Joe Perches <joe@perches.com>
Date: 2021-08-28 02:49:33

On Fri, 2021-08-27 at 11:49 +0300, Andy Shevchenko wrote:
On Fri, Aug 27, 2021 at 01:08:10AM -0700, Joe Perches wrote:
quoted
On Fri, 2021-08-27 at 10:48 +0300, Andy Shevchenko wrote:
quoted
On Thu, Aug 26, 2021 at 11:43:01AM -0700, Joe Perches wrote:
quoted
A few sysfs output uses of hex arrays are uppercase and are nominally ABI.

Add a mechanism to the existing vsprintf %*ph hex output extension to
support upper case hex output.
...
quoted
+	The preferred output is lowercase
 	%*ph	00 01 02  ...  3f
 	%*phC	00:01:02: ... :3f
 	%*phD	00-01-02- ... -3f
 	%*phN	000102 ... 3f
+	Formats with X are uppercase, used for backwards compatibility
+	%*phX	00 01 02  ...  3F
+	%*phCX	00:01:02: ... :3F
+	%*phDX	00-01-02- ... -3F
+	%*phNX	000102 ... 3F
Why not using %*pH...?
I find X more intelligible.
quoted
quoted
+	char locase = 0x20;	/* ASCII OR'd for lower case see: number() */
If you use h vs H, you may derive this from (fmt[...] & SMALL).
It's not necessary to use any more of the rather limited vsprintf
extension namespace.
I understand your concern, but %*ph is quite widely used (I guess top 1 or 2
among all %p extensions),
Cumulatively 3rd after %pM and %pOF
its performance degradation with your code may affect
a lot of other users and hence a kernel as a whole.

So, that's why my proposal stays.
Knock yourself out.
Of course you may provide a benchmark (btw, where are the test cases for this?)
You are welcome to provide both test cases and benchmarks.
I find the whole thing rather dull.
for yours and mine variant and we will see if it makes sense to optimize.
It doesn't.  Anyone thinking there is a required printf/vsprintf
optimization in the kernel is decidedly barking up the wrong tree.

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help