lib: Optimise hex_dump_to_buffer()
Subsystems (2)
PATCH v2 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.
(All callers currently pass 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.
Fix the return value (should be zero) when both len and linebuflen are zero.
All the updated tests in lib/test_hexdump.c pass.
Code size (x86-64) approximately halved.
---
Changes for v2:
- Fix typo in kerneldoc comment.
- Fix indendation of function prototype contunuation lines.
- Add un/likely() to the buffer overflow comparisons.
These are useful for the human readers and may improve code.
- Adjust buffer limit tests to reduce register pressure. (Removes a stack spill with gcc 12.2 for x86-84.)
- Change some types to 'unsigned long' to avoid explicit zero extension instructions when indexing arrays.
asc_hex[] is directly indexed to avoid the high nibble being masked. Possibly the helpers hex_pack_byte() (etc) could be changed now that 'char' is unsigned in all kernel builds.
I've also stopped gcc changing the loop that fills the pad before the ascii to a call to memset() - the count is small, often 1. An alternative option would be to explicitly memset the output buffer to spaces and then fill in the hex digits over the top. Definitely better if the output size is constant (unrolled writes).
gcc 12 and 13 (but not 14) manage to load the two characters into (eg) %al and %ah and then do a 16bit write.
However (I think to avoid false dependencies) the whole of %eax must be zeroed, this is done in a very stange way reading a zero from memory! See https://www.godbolt.org/z/1ds784hEq
Strangly this only happens with -mno-sse.
include/linux/printk.h | 6 +- lib/hexdump.c | 172 +++++++++++++++++++++-------------------- 2 files changed, 91 insertions(+), 87 deletions(-)
include/linux/printk.h +3 -3
@@ -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 +88 -84
206 lines not on this page · raw message
Signature · 1 line
2.39.5
Quoted 13 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.
(All callers currently pass 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.Fix the return value (should be zero) when both len and linebuflen are zero.
All the updated tests in
lib/test_hexdump.cpass.
Code size (x86-64) approximately halved.
...
include/linux/printk.h enum { · line 755
Quoted 4 lines
include/linux/printk.h +1
-3-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,
Why is extern still here?
include/linux/printk.h enum { · line 757
Quoted 2 lines
include/linux/printk.h +2
-0+ size_t groupsize, char *linebuf, + size_t linebuflen, bool ascii);
int - > size_t in the returned value is incorrect change. This is explained in the comments to the test cases patch series.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 15 lines · On Mon, 10 Mar 2025 11:05:13 +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.
(All callers currently pass 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.Quoted 4 lines
Fix the return value (should be zero) when both len and linebuflen are zero.
All the updated tests in
lib/test_hexdump.cpass.
Code size (x86-64) approximately halved.
Quoted 8 lines
...
include/linux/printk.h enum { · line 755
Quoted 4 lines
include/linux/printk.h +1
-3-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,Why is extern still here?
Because I didn't spot it ...
Quoted 6 lines
include/linux/printk.h enum { · line 757
Quoted 3 lines
include/linux/printk.h +2
-0+ size_t groupsize, char *linebuf, + size_t linebuflen, bool ascii);int - >
size_tin the returned value is incorrect change. This is explained in the comments to the test cases patch series.
I don't see you mentioning why.
The return value is 'the number of bytes that would be output if the buffer were large enough' - it is never negative.
Although given 'a large enough buffer' length is trivially calculable it would have been safer to return the actual number of bytes added (excluding the '\0').
There were no tests for 'len == 0 && linebuflen == 0', with !ascii the existing hex_dump_to_buffer() even manages to return -1.
(and the function than generates the 'test compare data' is also broken.)
Note that libc snprintf() has the same return type as fprintf() which can be -1, but any code the looks at is probably broken!
So an unsigned return type it better.
David
Quoted 3 lines · David Laight
Quoted 1 line · On Mon, 10 Mar 2025 11:05:13 +0200 Andy Shevchenko…
On Sat, Mar 08, 2025 at 09:34:21AM +0000, David Laight wrote:
...
Quoted 10 lines
Quoted 9 lines
include/linux/printk.h enum { · line 757
Quoted 6 lines
include/linux/printk.h +3
-3-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);int - >
size_tin the returned value is incorrect change. This is explained in the comments to the test cases patch series.
Quoted 3 lines
I don't see you mentioning why.
The return value is 'the number of bytes that would be output if the buffer were large enough' - it is never negative.
True...
Quoted 3 lines
Although given 'a large enough buffer' length is trivially calculable it would have been safer to return the actual number of bytes added (excluding the '\0').
...but the functions keep the snprintf() semantics, which returns an int. This makes it more-or-less 1:1 snprintf() substitute in cases where it can be done in general.
Quoted 3 lines
There were no tests for 'len == 0 && linebuflen == 0', with !ascii the existing
hex_dump_to_buffer()even manages to return -1.
(and the function than generates the 'test compare data' is also broken.)
Then you can start with fixes of those?
Quoted 4 lines
Note that libc
snprintf()has the same return type asfprintf()which can be -1, but any code the looks at is probably broken!So an unsigned return type it better.
Maybe, but this will deviate from the prototype and use cases.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 23 lines · On Wed, 12 Mar 2025 21:31:58 +0200 Andy Shevchenko…
Quoted 3 lines · David Laight
Quoted 1 line · On Mon, 10 Mar 2025 11:05:13 +0200 Andy Shevchenko…
On Sat, Mar 08, 2025 at 09:34:21AM +0000, David Laight wrote:
...
Quoted 10 lines
Quoted 9 lines
include/linux/printk.h enum { · line 757
Quoted 6 lines
include/linux/printk.h +3
-3-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);int - >
size_tin the returned value is incorrect change. This is explained in the comments to the test cases patch series.Quoted 3 lines
I don't see you mentioning why.
The return value is 'the number of bytes that would be output if the buffer were large enough' - it is never negative.True...
Quoted 7 lines
Quoted 3 lines
Although given 'a large enough buffer' length is trivially calculable it would have been safer to return the actual number of bytes added (excluding the '\0').
...but the functions keep the
snprintf()semantics, which returns an int. This makes it more-or-less 1:1snprintf()substitute in cases where it can be done in general.
And scnprintf() has been added because the return value of snprintf() isn't the one most code wanted.
I've looked through all the code that uses the result of hex_dump_to_buffer(). The only code that needs the 'overflow' result is the test code. Everything else will work just the same if it returns the number of characters added to the buffer.
The code in drivers/platform/chrome/wilco_ec/debugfs.c uses the return value without checking - hard to say whether the buffer is big enough (or whether the code has the required locking to allow for multiple readers.
Quoted 6 lines
Quoted 4 lines
There were no tests for 'len == 0 && linebuflen == 0', with !ascii the existing
hex_dump_to_buffer()even manages to return -1.
(and the function than generates the 'test compare data' is also broken.)Then you can start with fixes of those?
No one calls it like that.
I could split it into multiple patches, but they don't overlap and it just makes more work for everyone.
Quoted 7 lines
Quoted 5 lines
Note that libc
snprintf()has the same return type asfprintf()which can be -1, but any code the looks at is probably broken!So an unsigned return type it better.
Maybe, but this will deviate from the prototype and use cases.
The use cases all want a 'length' never an 'error'.
Having an unsigned return type makes it absolutely clear that -1 (or -errno) won't be returned.
It isn't the sort of function where you want to have to 'go through hoops' to write valid code.
David