LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] vsprintf and docs: Add X to %ph for upper case output
@ 2021-08-22  2:59 Joe Perches
  2021-08-22  8:31 ` Andy Shevchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Joe Perches @ 2021-08-22  2:59 UTC (permalink / raw)
  To: Petr Mladek, Steven Rostedt, Sergey Senozhatsky, Andy Shevchenko,
	Rasmus Villemoes, Jonathan Corbet
  Cc: linux-doc, linux-kernel

Uppercase hex output of small char arrays is moderately frequently used.
Add a mechanism to support the %*ph output as uppercase using 'X'.

Signed-off-by: Joe Perches <joe@perches.com>
---
 Documentation/core-api/printk-formats.rst |  7 ++++-
 lib/vsprintf.c                            | 48 ++++++++++++++++++++-----------
 2 files changed, 37 insertions(+), 18 deletions(-)

diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst
index e08bbe9b0cbf3..01944b0c8adf3 100644
--- a/Documentation/core-api/printk-formats.rst
+++ b/Documentation/core-api/printk-formats.rst
@@ -285,12 +285,17 @@ Raw buffer as a hex string
 ::
 
 	%*ph	00 01 02  ...  3f
+	%*phX	00 01 02  ...  3F
 	%*phC	00:01:02: ... :3f
+	%*phCX	00:01:02: ... :3F
 	%*phD	00-01-02- ... -3f
+	%*phDX	00-01-02- ... -3F
 	%*phN	000102 ... 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
+certain separator. Typical output is lowercase, formats with X are uppercase.
+For larger buffers consider using
 :c:func:`print_hex_dump`.
 
 MAC/FDDI addresses
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 134216c45980e..6aca95d5a99f0 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;
+	bool ucase = false;
 
 	if (spec.field_width == 0)
 		/* nothing to print */
@@ -1156,30 +1159,41 @@ 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':
+			ucase = true;
+			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)
+		if (buf < end) {
 			*buf = hex_asc_hi(addr[i]);
+			if (ucase)
+				*buf = toupper(*buf);
+		}
 		++buf;
-		if (buf < end)
+		if (buf < end) {
 			*buf = hex_asc_lo(addr[i]);
+			if (ucase)
+				*buf = toupper(*buf);
+		}
 		++buf;
 
 		if (separator && i != len - 1) {



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vsprintf and docs: Add X to %ph for upper case output
  2021-08-22  2:59 [PATCH] vsprintf and docs: Add X to %ph for upper case output Joe Perches
@ 2021-08-22  8:31 ` Andy Shevchenko
  2021-08-22  8:45   ` Joe Perches
  0 siblings, 1 reply; 6+ messages in thread
From: Andy Shevchenko @ 2021-08-22  8:31 UTC (permalink / raw)
  To: Joe Perches
  Cc: Petr Mladek, Steven Rostedt, Sergey Senozhatsky, Andy Shevchenko,
	Rasmus Villemoes, Jonathan Corbet, Linux Documentation List,
	Linux Kernel Mailing List

On Sun, Aug 22, 2021 at 6:00 AM Joe Perches <joe@perches.com> wrote:
>
> Uppercase hex output of small char arrays is moderately frequently used.
> Add a mechanism to support the %*ph output as uppercase using 'X'.

Besides the fact of existing hex_asc_upper_*(), what ABI (!) uses
this? If none, I dunno we need this.
And show at least a few users where we gain something after conversion.

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vsprintf and docs: Add X to %ph for upper case output
  2021-08-22  8:31 ` Andy Shevchenko
@ 2021-08-22  8:45   ` Joe Perches
  2021-08-22  8:53     ` Andy Shevchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Joe Perches @ 2021-08-22  8:45 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Petr Mladek, Steven Rostedt, Sergey Senozhatsky, Andy Shevchenko,
	Rasmus Villemoes, Jonathan Corbet, Linux Documentation List,
	Linux Kernel Mailing List

On Sun, 2021-08-22 at 11:31 +0300, Andy Shevchenko wrote:
> On Sun, Aug 22, 2021 at 6:00 AM Joe Perches <joe@perches.com> wrote:
> > 
> > Uppercase hex output of small char arrays is moderately frequently used.
> > Add a mechanism to support the %*ph output as uppercase using 'X'.
> 
> Besides the fact of existing hex_asc_upper_*(), what ABI (!) uses
> this? If none, I dunno we need this.
> And show at least a few users where we gain something after conversion.
> 

There are at least a few uses that could be converted.

For instance:

diff --git a/drivers/scsi/hpsa.c b/drivers/scsi/hpsa.c
index 3faa87fa296a2..c56871e8ce1b7 100644
--- a/drivers/scsi/hpsa.c
+++ b/drivers/scsi/hpsa.c
@@ -743,13 +743,7 @@ static ssize_t unique_id_show(struct device *dev,
 	}
 	memcpy(sn, hdev->device_id, sizeof(sn));
 	spin_unlock_irqrestore(&h->lock, flags);
-	return snprintf(buf, 16 * 2 + 2,
-			"%02X%02X%02X%02X%02X%02X%02X%02X"
-			"%02X%02X%02X%02X%02X%02X%02X%02X\n",
-			sn[0], sn[1], sn[2], sn[3],
-			sn[4], sn[5], sn[6], sn[7],
-			sn[8], sn[9], sn[10], sn[11],
-			sn[12], sn[13], sn[14], sn[15]);
+	return snprintf(buf, 16 * 2 + 2, "%16phNX\n", sn);
 }
 
 static ssize_t sas_address_show(struct device *dev,

and

diff --git a/drivers/scsi/smartpqi/smartpqi_init.c b/drivers/scsi/smartpqi/smartpqi_init.c
index ecb2af3f43ca3..eb39490b196cc 100644
--- a/drivers/scsi/smartpqi/smartpqi_init.c
+++ b/drivers/scsi/smartpqi/smartpqi_init.c
@@ -6674,13 +6674,7 @@ static ssize_t pqi_unique_id_show(struct device *dev,
 
 	spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
 
-	return scnprintf(buffer, PAGE_SIZE,
-		"%02X%02X%02X%02X%02X%02X%02X%02X"
-		"%02X%02X%02X%02X%02X%02X%02X%02X\n",
-		unique_id[0], unique_id[1], unique_id[2], unique_id[3],
-		unique_id[4], unique_id[5], unique_id[6], unique_id[7],
-		unique_id[8], unique_id[9], unique_id[10], unique_id[11],
-		unique_id[12], unique_id[13], unique_id[14], unique_id[15]);
+	return scnprintf(buffer, PAGE_SIZE, "%16phNX\n", unique_id);
 }



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vsprintf and docs: Add X to %ph for upper case output
  2021-08-22  8:45   ` Joe Perches
@ 2021-08-22  8:53     ` Andy Shevchenko
  2021-08-22  8:57       ` Joe Perches
  0 siblings, 1 reply; 6+ messages in thread
From: Andy Shevchenko @ 2021-08-22  8:53 UTC (permalink / raw)
  To: Joe Perches
  Cc: Petr Mladek, Steven Rostedt, Sergey Senozhatsky, Andy Shevchenko,
	Rasmus Villemoes, Jonathan Corbet, Linux Documentation List,
	Linux Kernel Mailing List

On Sun, Aug 22, 2021 at 11:45 AM Joe Perches <joe@perches.com> wrote:
> On Sun, 2021-08-22 at 11:31 +0300, Andy Shevchenko wrote:
> > On Sun, Aug 22, 2021 at 6:00 AM Joe Perches <joe@perches.com> wrote:
> > >
> > > Uppercase hex output of small char arrays is moderately frequently used.
> > > Add a mechanism to support the %*ph output as uppercase using 'X'.
> >
> > Besides the fact of existing hex_asc_upper_*(), what ABI (!) uses
> > this? If none, I dunno we need this.
> > And show at least a few users where we gain something after conversion.
> >
>
> There are at least a few uses that could be converted.

Provide a series then!

...

> +       return snprintf(buf, 16 * 2 + 2, "%16phNX\n", sn);

> +       return scnprintf(buffer, PAGE_SIZE, "%16phNX\n", unique_id);

I think you need to convert to sysfs_emit() in both cases.

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vsprintf and docs: Add X to %ph for upper case output
  2021-08-22  8:53     ` Andy Shevchenko
@ 2021-08-22  8:57       ` Joe Perches
  2021-08-22  9:00         ` Andy Shevchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Joe Perches @ 2021-08-22  8:57 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Petr Mladek, Steven Rostedt, Sergey Senozhatsky, Andy Shevchenko,
	Rasmus Villemoes, Jonathan Corbet, Linux Documentation List,
	Linux Kernel Mailing List

On Sun, 2021-08-22 at 11:53 +0300, Andy Shevchenko wrote:
> On Sun, Aug 22, 2021 at 11:45 AM Joe Perches <joe@perches.com> wrote:
> > On Sun, 2021-08-22 at 11:31 +0300, Andy Shevchenko wrote:
> > > On Sun, Aug 22, 2021 at 6:00 AM Joe Perches <joe@perches.com> wrote:
> > > > 
> > > > Uppercase hex output of small char arrays is moderately frequently used.
> > > > Add a mechanism to support the %*ph output as uppercase using 'X'.
> > > 
> > > Besides the fact of existing hex_asc_upper_*(), what ABI (!) uses
> > > this? If none, I dunno we need this.
> > > And show at least a few users where we gain something after conversion.
> > > 
> > 
> > There are at least a few uses that could be converted.
> 
> Provide a series then!
> 
> ...
> 
> > +       return snprintf(buf, 16 * 2 + 2, "%16phNX\n", sn);
> 
> > +       return scnprintf(buffer, PAGE_SIZE, "%16phNX\n", unique_id);
> 
> I think you need to convert to sysfs_emit() in both cases.

First things first...


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] vsprintf and docs: Add X to %ph for upper case output
  2021-08-22  8:57       ` Joe Perches
@ 2021-08-22  9:00         ` Andy Shevchenko
  0 siblings, 0 replies; 6+ messages in thread
From: Andy Shevchenko @ 2021-08-22  9:00 UTC (permalink / raw)
  To: Joe Perches
  Cc: Petr Mladek, Steven Rostedt, Sergey Senozhatsky, Andy Shevchenko,
	Rasmus Villemoes, Jonathan Corbet, Linux Documentation List,
	Linux Kernel Mailing List

On Sun, Aug 22, 2021 at 11:57 AM Joe Perches <joe@perches.com> wrote:
> On Sun, 2021-08-22 at 11:53 +0300, Andy Shevchenko wrote:
> > On Sun, Aug 22, 2021 at 11:45 AM Joe Perches <joe@perches.com> wrote:
> > > On Sun, 2021-08-22 at 11:31 +0300, Andy Shevchenko wrote:

...

> > > +       return snprintf(buf, 16 * 2 + 2, "%16phNX\n", sn);
> >
> > > +       return scnprintf(buffer, PAGE_SIZE, "%16phNX\n", unique_id);
> >
> > I think you need to convert to sysfs_emit() in both cases.
>
> First things first...

Yep. and to avoid ping-pong style, first here is converting to
sysfs_emit*() since it's already in the kernel.

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-08-22  9:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-22  2:59 [PATCH] vsprintf and docs: Add X to %ph for upper case output Joe Perches
2021-08-22  8:31 ` Andy Shevchenko
2021-08-22  8:45   ` Joe Perches
2021-08-22  8:53     ` Andy Shevchenko
2021-08-22  8:57       ` Joe Perches
2021-08-22  9:00         ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).