public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 3/3] lib/uuid: avoid double traversal in __uuid_parse()
@ 2026-03-12 18:41 Josh Law
  2026-03-12 20:48 ` Andrew Morton
  2026-03-13  9:04 ` Andy Shevchenko
  0 siblings, 2 replies; 4+ messages in thread
From: Josh Law @ 2026-03-12 18:41 UTC (permalink / raw)
  To: Andrew Morton, Andy Shevchenko; +Cc: Josh Law, linux-kernel

__uuid_parse() calls uuid_is_valid() to walk all 36 characters for
format validation, then walks the string a second time to parse the
hex bytes.  Combine both passes into one: validate each hex digit
inline via hex_to_bin() return value and check the four dash positions
after the loop.

uuid_is_valid() remains exported unchanged for callers that only need
validation without parsing.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/uuid.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/lib/uuid.c b/lib/uuid.c
index 128a51f1879b..89608f82ca6b 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -108,16 +108,20 @@ static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16])
 	static const u8 si[16] = {0,2,4,6,9,11,14,16,19,21,24,26,28,30,32,34};
 	unsigned int i;
 
-	if (!uuid_is_valid(uuid))
-		return -EINVAL;
-
 	for (i = 0; i < 16; i++) {
 		int hi = hex_to_bin(uuid[si[i] + 0]);
 		int lo = hex_to_bin(uuid[si[i] + 1]);
 
+		if (hi < 0 || lo < 0)
+			return -EINVAL;
+
 		b[ei[i]] = (hi << 4) | lo;
 	}
 
+	if (uuid[8] != '-' || uuid[13] != '-' ||
+	    uuid[18] != '-' || uuid[23] != '-')
+		return -EINVAL;
+
 	return 0;
 }
 
-- 
2.34.1


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

* Re: [PATCH 3/3] lib/uuid: avoid double traversal in __uuid_parse()
  2026-03-12 18:41 [PATCH 3/3] lib/uuid: avoid double traversal in __uuid_parse() Josh Law
@ 2026-03-12 20:48 ` Andrew Morton
  2026-03-12 20:51   ` Josh Law
  2026-03-13  9:04 ` Andy Shevchenko
  1 sibling, 1 reply; 4+ messages in thread
From: Andrew Morton @ 2026-03-12 20:48 UTC (permalink / raw)
  To: Josh Law; +Cc: Andy Shevchenko, Josh Law, linux-kernel

On Thu, 12 Mar 2026 18:41:13 +0000 Josh Law <hlcj1234567@gmail.com> wrote:

> __uuid_parse() calls uuid_is_valid() to walk all 36 characters for
> format validation, then walks the string a second time to parse the
> hex bytes.  Combine both passes into one: validate each hex digit
> inline via hex_to_bin() return value and check the four dash positions
> after the loop.
> 
> uuid_is_valid() remains exported unchanged for callers that only need
> validation without parsing.
> 
> --- a/lib/uuid.c
> +++ b/lib/uuid.c
> @@ -108,16 +108,20 @@ static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16])
>  	static const u8 si[16] = {0,2,4,6,9,11,14,16,19,21,24,26,28,30,32,34};
>  	unsigned int i;
>  
> -	if (!uuid_is_valid(uuid))
> -		return -EINVAL;
> -
>  	for (i = 0; i < 16; i++) {
>  		int hi = hex_to_bin(uuid[si[i] + 0]);
>  		int lo = hex_to_bin(uuid[si[i] + 1]);
>  
> +		if (hi < 0 || lo < 0)
> +			return -EINVAL;
> +
>  		b[ei[i]] = (hi << 4) | lo;
>  	}
>  
> +	if (uuid[8] != '-' || uuid[13] != '-' ||
> +	    uuid[18] != '-' || uuid[23] != '-')
> +		return -EINVAL;
> +
>  	return 0;

This rather messifies the code, and for what?  Is this in any way a hot
path?


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

* Re: [PATCH 3/3] lib/uuid: avoid double traversal in __uuid_parse()
  2026-03-12 20:48 ` Andrew Morton
@ 2026-03-12 20:51   ` Josh Law
  0 siblings, 0 replies; 4+ messages in thread
From: Josh Law @ 2026-03-12 20:51 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Andy Shevchenko, Josh Law, linux-kernel

12 Mar 2026 20:48:59 Andrew Morton <akpm@linux-foundation.org>:

> On Thu, 12 Mar 2026 18:41:13 +0000 Josh Law <hlcj1234567@gmail.com> wrote:
>
>> __uuid_parse() calls uuid_is_valid() to walk all 36 characters for
>> format validation, then walks the string a second time to parse the
>> hex bytes.  Combine both passes into one: validate each hex digit
>> inline via hex_to_bin() return value and check the four dash positions
>> after the loop.
>>
>> uuid_is_valid() remains exported unchanged for callers that only need
>> validation without parsing.
>>
>> --- a/lib/uuid.c
>> +++ b/lib/uuid.c
>> @@ -108,16 +108,20 @@ static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16])
>>     static const u8 si[16] = {0,2,4,6,9,11,14,16,19,21,24,26,28,30,32,34};
>>     unsigned int i;
>>
>> -   if (!uuid_is_valid(uuid))
>> -       return -EINVAL;
>> -
>>     for (i = 0; i < 16; i++) {
>>         int hi = hex_to_bin(uuid[si[i] + 0]);
>>         int lo = hex_to_bin(uuid[si[i] + 1]);
>>
>> +       if (hi < 0 || lo < 0)
>> +           return -EINVAL;
>> +
>>         b[ei[i]] = (hi << 4) | lo;
>>     }
>>
>> +   if (uuid[8] != '-' || uuid[13] != '-' ||
>> +       uuid[18] != '-' || uuid[23] != '-')
>> +       return -EINVAL;
>> +
>>     return 0;
>
> This rather messifies the code, and for what?  Is this in any way a hot
> path?


ehhhh. If I had to say, yes it is a hot path, it runs quite a lot

perf increases ever so slightly, so that's your call Andrew.

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

* Re: [PATCH 3/3] lib/uuid: avoid double traversal in __uuid_parse()
  2026-03-12 18:41 [PATCH 3/3] lib/uuid: avoid double traversal in __uuid_parse() Josh Law
  2026-03-12 20:48 ` Andrew Morton
@ 2026-03-13  9:04 ` Andy Shevchenko
  1 sibling, 0 replies; 4+ messages in thread
From: Andy Shevchenko @ 2026-03-13  9:04 UTC (permalink / raw)
  To: Josh Law; +Cc: Andrew Morton, Josh Law, linux-kernel

On Thu, Mar 12, 2026 at 06:41:13PM +0000, Josh Law wrote:
> __uuid_parse() calls uuid_is_valid() to walk all 36 characters for
> format validation, then walks the string a second time to parse the
> hex bytes.  Combine both passes into one: validate each hex digit
> inline via hex_to_bin() return value and check the four dash positions
> after the loop.
> 
> uuid_is_valid() remains exported unchanged for callers that only need
> validation without parsing.

...

> -	if (!uuid_is_valid(uuid))
> -		return -EINVAL;

In case if valid string it heats up the caches, no?

>  	for (i = 0; i < 16; i++) {
>  		int hi = hex_to_bin(uuid[si[i] + 0]);
>  		int lo = hex_to_bin(uuid[si[i] + 1]);

> +		if (hi < 0 || lo < 0)
> +			return -EINVAL;

Here we add a branch (or two, depending on the code generation)

>  		b[ei[i]] = (hi << 4) | lo;
>  	}

> +	if (uuid[8] != '-' || uuid[13] != '-' ||
> +	    uuid[18] != '-' || uuid[23] != '-')
> +		return -EINVAL;

And one more here.

>  	return 0;
>  }

If you a really into performance, please, provide the tests and the results to
show the benefit. Numbers will tell for themselves.

-- 
With Best Regards,
Andy Shevchenko



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

end of thread, other threads:[~2026-03-13  9:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-12 18:41 [PATCH 3/3] lib/uuid: avoid double traversal in __uuid_parse() Josh Law
2026-03-12 20:48 ` Andrew Morton
2026-03-12 20:51   ` Josh Law
2026-03-13  9:04 ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox