Skip to contentSkip to conversation

lib: Optimise hex_dump_to_buffer()

PATCH Superseded David Laight 3 participants 7 messages Feb 16, 2025 – Feb 19, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: Optimise hex_dump_to_buffer()

Fastpath the normal case of single byte output that fits in the buffer. Output byte groups (byteswapped on little-endian) without calling snprintf(). Remove the restriction that rowsize must be 16 or 32.
Remove the restriction that groupsize must be 8 or less.
If groupsize isn't a power of 2 or doesn't divide into both len and
rowsize it is set to 1 (otherwise byteswapping is hard).
Change the types of the rowsize and groupsize parameters to be unsigned types.

Tested in a userspace harness, code size (x86-64) halved to 723 bytes.

---
 include/linux/printk.h |   6 +-
 lib/hexdump.c          | 165 ++++++++++++++++++++---------------------
 2 files changed, 85 insertions(+), 86 deletions(-)
include/linux/printk.h +3 -3
index 4217a9f412b2..49e67f63277e 100644
@@ -752,9 +752,9 @@ enum {
 	DUMP_PREFIX_ADDRESS,
 	DUMP_PREFIX_OFFSET
 };
-extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
-			      int groupsize, char *linebuf, size_t linebuflen,
-			      bool ascii);
+extern size_t hex_dump_to_buffer(const void *buf, size_t len, size_t rowsize,
+			      size_t groupsize, char *linebuf,
+			      size_t linebuflen, bool ascii);
 #ifdef CONFIG_PRINTK
 extern void print_hex_dump(const char *level, const char *prefix_str,
 			   int prefix_type, int rowsize, int groupsize,
lib/hexdump.c +82 -83

200 lines not on this page · raw message

Signature · 1 line
2.39.5
#1 lore raw
Quoted 9 lines · David Laight

Fastpath the normal case of single byte output that fits in the buffer. Output byte groups (byteswapped on little-endian) without calling snprintf(). Remove the restriction that rowsize must be 16 or 32.
Remove the restriction that groupsize must be 8 or less.
If groupsize isn't a power of 2 or doesn't divide into both len and
rowsize it is set to 1 (otherwise byteswapping is hard).
Change the types of the rowsize and groupsize parameters to be unsigned types.

Tested in a userspace harness, code size (x86-64) halved to 723 bytes.

Does it imply running the respective test cases we have? Do you need to add more test cases? I believe so. Without test cases added it's a no go.

include/linux/printk.h enum { · line 757

Quoted 3 lines
include/linux/printk.h +3 -0
+extern size_t hex_dump_to_buffer(const void *buf, size_t len, size_t rowsize,
+			      size_t groupsize, char *linebuf,
+			      size_t linebuflen, bool ascii);

Looking at another thread where upper layer function wants to have unsigned long flags instead of bool ascii, I would also do the new API, that takes flags and leave the old one as a simple wrapper with all restrictions being applied.

And again, provide it together with a bunch of test cases.

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+			dst[0] = hex_asc_hi(ch);
+			dst[1] = hex_asc_lo(ch);

We have hex_pack_byte() or so

...

lib/hexdump.c EXPORT_SYMBOL(bin2hex); · line 179

Quoted 4 lines
lib/hexdump.c +4 -0
+			ch = ptr[j ^ byteswap];
+			dst[0] = hex_asc_hi(ch);
+			dst[1] = hex_asc_lo(ch);
+			dst += 2;

Ditto.

...

lib/hexdump.c EXPORT_SYMBOL(bin2hex); · line 211

Quoted 2 lines
lib/hexdump.c +1 -1
-		linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
+		*dst++ = ch >= ' ' && ch < 0x7f ? ch : '.';

Please also add a test case for this to make sure it has no changes.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 15 lines · On Sun, 16 Feb 2025 22:47:49 +0200 Andy Shevchenko…
Quoted 9 lines · David Laight

Fastpath the normal case of single byte output that fits in the buffer. Output byte groups (byteswapped on little-endian) without calling snprintf(). Remove the restriction that rowsize must be 16 or 32.
Remove the restriction that groupsize must be 8 or less.
If groupsize isn't a power of 2 or doesn't divide into both len and
rowsize it is set to 1 (otherwise byteswapping is hard).
Change the types of the rowsize and groupsize parameters to be unsigned types.

Tested in a userspace harness, code size (x86-64) halved to 723 bytes.

Does it imply running the respective test cases we have? Do you need to add more test cases? I believe so. Without test cases added it's a no go.

Quoted 7 lines

include/linux/printk.h enum { · line 757

Quoted 3 lines
include/linux/printk.h +3 -0
+extern size_t hex_dump_to_buffer(const void *buf, size_t len, size_t rowsize,
+			      size_t groupsize, char *linebuf,
+			      size_t linebuflen, bool ascii);  

Looking at another thread where upper layer function wants to have unsigned long flags instead of bool ascii, I would also do the new API, that takes flags and leave the old one as a simple wrapper with all restrictions being applied.

I can't imagine any code relying on the rowsize being converted to 16. And (elsewhere) I've definitely needed to do hexdumps with strange numbers of bytes/line.

Quoted 9 lines

And again, provide it together with a bunch of test cases.

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+			dst[0] = hex_asc_hi(ch);
+			dst[1] = hex_asc_lo(ch);  

We have hex_pack_byte() or so

At least some versions of gcc have generated better code if you don't use *ptr++ but do the increment afterwards.
It is also what the old version used.
Not to mention being another wrapper you need to look up to work out what the code is doing.

..

Quoted 6 lines

...

lib/hexdump.c EXPORT_SYMBOL(bin2hex); · line 211

Quoted 2 lines
lib/hexdump.c +1 -1
-		linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
+		*dst++ = ch >= ' ' && ch < 0x7f ? ch : '.';  

Please also add a test case for this to make sure it has no changes.

Well isascii() usually checks for the 0x80 bit being clear and isprint() rejects control characters and 'del' (0x7f).
I'm not sure what isascii() does for EBCDIC type charsets - but I don't expect Linux runs on any of those so who cares.
Oh, and isprint() seems to be based on a memory lookup in an _ctype[] array. Very 1970s.

David

Quoted 1 line
#3 lore raw
Quoted 3 lines · David Laight
Quoted 1 line · On Sun, 16 Feb 2025 22:47:49 +0200 Andy Shevchenko…

On Sun, Feb 16, 2025 at 08:19:01PM +0000, David Laight wrote:

...

Quoted 11 lines
Quoted 7 lines

include/linux/printk.h enum { · line 757

Quoted 3 lines
include/linux/printk.h +3 -0
+extern size_t hex_dump_to_buffer(const void *buf, size_t len, size_t rowsize,
+			      size_t groupsize, char *linebuf,
+			      size_t linebuflen, bool ascii);  

Looking at another thread where upper layer function wants to have unsigned long flags instead of bool ascii, I would also do the new API, that takes flags and leave the old one as a simple wrapper with all restrictions being applied.

I can't imagine any code relying on the rowsize being converted to 16. And (elsewhere) I've definitely needed to do hexdumps with strange numbers of bytes/line.

I didn't get how this is related to my comment.

...

Quoted 8 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+			dst[0] = hex_asc_hi(ch);
+			dst[1] = hex_asc_lo(ch);  

We have hex_pack_byte() or so

At least some versions of gcc have generated better code if you don't use *ptr++ but do the increment afterwards.
It is also what the old version used.

Do we really care? What versions, btw, are you talk about?

Quoted 2 lines

Not to mention being another wrapper you need to look up to work out what the code is doing.

That's not an issue. We use a lot of wrappers in the Linux kernel. Without a skill to quickly find this information it would be very hard to develop the kernel code without reinventing a wheel.

...

Quoted 9 lines
Quoted 4 lines

lib/hexdump.c EXPORT_SYMBOL(bin2hex); · line 211

Quoted 2 lines
lib/hexdump.c +1 -1
-		linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
+		*dst++ = ch >= ' ' && ch < 0x7f ? ch : '.';  

Please also add a test case for this to make sure it has no changes.

Well isascii() usually checks for the 0x80 bit being clear and isprint() rejects control characters and 'del' (0x7f).
I'm not sure what isascii() does for EBCDIC type charsets - but I don't expect Linux runs on any of those so who cares.

Still, it's good to have a test cases for this change.

Quoted 2 lines

Oh, and isprint() seems to be based on a memory lookup in an _ctype[] array. Very 1970s.

With enough CPU (data) caches it's quite effective.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
In reply to David Laight
Quoted 8 lines · David Laight

Fastpath the normal case of single byte output that fits in the buffer. Output byte groups (byteswapped on little-endian) without calling snprintf(). Remove the restriction that rowsize must be 16 or 32.
Remove the restriction that groupsize must be 8 or less.
If groupsize isn't a power of 2 or doesn't divide into both len and
rowsize it is set to 1 (otherwise byteswapping is hard).
Change the types of the rowsize and groupsize parameters to be unsigned types.

Thank you!

Quoted 16 lines

Tested in a userspace harness, code size (x86-64) halved to 723 bytes.

---
 include/linux/printk.h |   6 +-
 lib/hexdump.c          | 165 ++++++++++++++++++++---------------------
 2 files changed, 85 insertions(+), 86 deletions(-)
include/linux/printk.h +0 -0
index 4217a9f412b2..49e67f63277e 100644
@@ -752,9 +752,9 @@ enum {
 	DUMP_PREFIX_ADDRESS,
 	DUMP_PREFIX_OFFSET
 };

...

Quoted 3 lines

- * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
+ * If @groupsize isn't a power of 2 that divides into both @len and @rowsize
+ * the it is set to 1.

s/the/then/

Quoted 3 lines

+ *
+ * hex_dump_to_buffer() works on one "line" of output at a time, e.g.,
* 16 or 32 bytes of input data converted to hex + ASCII output.

...

include/linux/printk.h

Quoted 7 lines
include/linux/printk.h +5 -1
-		linebuf[lx++] = ' ';
+	if (!ascii) {
+		*dst = 0;
+		return out_len;
 	}
+ 
+	pad_len += 2;

So at a minimum there is 2 spaces before the ascii translation? when people allocate linebuf, what should they use to calculate the len?

Also side nit, this existed before this patch, the endian switch may occur on the hex dump but it doesn't on the ascii conversion:

[   20.172006][  T150] 706f6e6d6c6b6a696867666564636261  abcdefghijklmnop

include/linux/printk.h

Quoted 3 lines
include/linux/printk.h +3 -0
+	out_len += pad_len + len;
+	if (dst + pad_len >= dst_end)
+		pad_len = dst_end - dst - 1;

Why not jump to hex_truncate here? This feels like an error case and if I am understanding correctly, this will pad the rest of the buffer leaving no room for ascii.

Quoted 4 lines
include/linux/printk.h +4 -0
+	while (pad_len--)
+		*dst++ = ' ';
+
+	if (dst + len >= dst_end)

...

Quoted 2 lines
Signature · 1 line
2.39.5

I will like to also support a wrapper to a bitmap argument as Andy mentioned. Mostly for selfish reasons though: I would like an argument to be added to skip endian conversion, and just observe the bytes as they appear in memory (without having to use groupsize 1).

I had fun tracking some of these bitwise operations on power of 2 integers, I think I must've missed that day in school. Cool stuff :)

#5 lore raw
Quoted 39 lines · On Tue, 18 Feb 2025 16:52:38 -0600 Nick Child <[email protected]> wrote:
Quoted 8 lines · David Laight

Fastpath the normal case of single byte output that fits in the buffer. Output byte groups (byteswapped on little-endian) without calling snprintf(). Remove the restriction that rowsize must be 16 or 32.
Remove the restriction that groupsize must be 8 or less.
If groupsize isn't a power of 2 or doesn't divide into both len and
rowsize it is set to 1 (otherwise byteswapping is hard).
Change the types of the rowsize and groupsize parameters to be unsigned types.

Thank you!

Quoted 16 lines

Tested in a userspace harness, code size (x86-64) halved to 723 bytes.

---
 include/linux/printk.h |   6 +-
 lib/hexdump.c          | 165 ++++++++++++++++++++---------------------
 2 files changed, 85 insertions(+), 86 deletions(-)
include/linux/printk.h +0 -0
index 4217a9f412b2..49e67f63277e 100644
@@ -752,9 +752,9 @@ enum {
 	DUMP_PREFIX_ADDRESS,
 	DUMP_PREFIX_OFFSET
 };  

...

Quoted 3 lines

- * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
+ * If @groupsize isn't a power of 2 that divides into both @len and @rowsize
+ * the it is set to 1.

s/the/then/

Quoted 3 lines

+ *
+ * hex_dump_to_buffer() works on one "line" of output at a time, e.g.,
* 16 or 32 bytes of input data converted to hex + ASCII output.

...

Quoted 9 lines

include/linux/printk.h

Quoted 7 lines
include/linux/printk.h +5 -1
-		linebuf[lx++] = ' ';
+	if (!ascii) {
+		*dst = 0;
+		return out_len;
 	}
+ 
+	pad_len += 2;  

So at a minimum there is 2 spaces before the ascii translation?

That is the way it always was.
Does make sense that way.

Quoted 1 line

when people allocate linebuf, what should they use to calculate the len?

Enough :-)
Unchanged from before, 'rowsize * 4 + 2' is just enough.

Quoted 4 lines

Also side nit, this existed before this patch, the endian switch may occur on the hex dump but it doesn't on the ascii conversion:

[   20.172006][  T150] 706f6e6d6c6b6a696867666564636261  abcdefghijklmnop

Correct, that is what you want.
Consider { u32 x; char y[4]; } - you don't want the ascii byte reversed.

Indeed, if the data might not be aligned it is easier to process the hex bytes in address order than a little-endian value that is split between two 'words'.

Quoted 9 lines

include/linux/printk.h

Quoted 5 lines
include/linux/printk.h +3 -0
+	out_len += pad_len + len;
+	if (dst + pad_len >= dst_end)
+		pad_len = dst_end - dst - 1;  

Why not jump to hex_truncate here? This feels like an error case and if I am understanding correctly, this will pad the rest of the buffer leaving no room for ascii.

I missed that, can't remember what the old code did.

Quoted 6 lines
Quoted 5 lines
include/linux/printk.h +4 -0
+	while (pad_len--)
+		*dst++ = ' ';
+
+	if (dst + len >= dst_end)  

...

Quoted 7 lines
Quoted 2 lines
Signature · 1 line
2.39.5  

I will like to also support a wrapper to a bitmap argument as Andy mentioned. Mostly for selfish reasons though: I would like an argument to be added to skip endian conversion, and just observe the bytes as they appear in memory (without having to use groupsize 1).

More useful would be an option to keep address order with a space between the bytes, but add an extra space every 'n' bytes. So you get: "00 11 22 33 44 55 66 77 88..."

But that is an enhancement rather than a rewrite. Although it is all easier to do with my new 'slow path' loop.

Quoted 2 lines

I had fun tracking some of these bitwise operations on power of 2 integers, I think I must've missed that day in school. Cool stuff :)

It came the day after the rule for inverting boolean expressions.

David

#6 lore raw
In reply to Nick Child
Quoted 4 lines · On Tue, 18 Feb 2025 16:52:38 -0600 Nick Child <[email protected]> wrote:
Quoted 3 lines · David Laight

Fastpath the normal case of single byte output that fits in the buffer. Output byte groups (byteswapped on little-endian) without calling snprintf(). Remove the restriction that rowsize must be 16 or 32.

That lets dump_hex_to_buffer(buf, len, len, 1, output, size, true) be used to format hex+ascii without a gap between the hex and ascii.

Quoted 8 lines
Quoted 5 lines

Remove the restriction that groupsize must be 8 or less.
If groupsize isn't a power of 2 or doesn't divide into both len and
rowsize it is set to 1 (otherwise byteswapping is hard).
Change the types of the rowsize and groupsize parameters to be unsigned types.

Thank you!

....

Quoted 7 lines

lib/hexdump.c EXPORT_SYMBOL(bin2hex); · line 202

Quoted 3 lines
lib/hexdump.c +3 -0
+	out_len += pad_len + len;
+	if (dst + pad_len >= dst_end)
+		pad_len = dst_end - dst - 1;  

Why not jump to hex_truncate here? This feels like an error case and if I am understanding correctly, this will pad the rest of the buffer leaving no room for ascii.

I've had a look at the code again.
The truncating error path would be: dst[0] = 0; return out_len; But that would be confusing because it would be a truncated output that doesn't fill the buffer.
This is different from snprintf(), so I think it is best to pad to the end of the buffer.

David