* [PATCH -mm] kstrtox: delete division from inner parsing loop
@ 2026-07-03 10:25 Alexey Dobriyan
2026-07-03 10:34 ` Dmitry Antipov
0 siblings, 1 reply; 2+ messages in thread
From: Alexey Dobriyan @ 2026-07-03 10:25 UTC (permalink / raw)
To: akpm; +Cc: adobriyan, linux-kernel, dmantipov, andriy.shevchenko
Overflow conditions can be checked on the fly resulting in at most
2 very predictable branches and zero division instructions:
_parse_integer_limit:
...
mov rax, rdx
mul rsi
seto bl
add rax, rcx
mov rdx, rax
setb al
...
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
lib/kstrtox.c | 14 +++++---------
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/lib/kstrtox.c b/lib/kstrtox.c
index 97be2a39f537..efd55db0533a 100644
--- a/lib/kstrtox.c
+++ b/lib/kstrtox.c
@@ -71,15 +71,11 @@ unsigned int _parse_integer_limit(const char *s, unsigned int base, unsigned lon
if (val >= base)
break;
- /*
- * Check for overflow only if we are within range of
- * it in the max base we support (16)
- */
- if (unlikely(res & (~0ull << 60))) {
- if (res > div_u64(ULLONG_MAX - val, base))
- rv |= KSTRTOX_OVERFLOW;
- }
- res = res * base + val;
+
+ if (check_mul_overflow(res, base, &res))
+ rv |= KSTRTOX_OVERFLOW;
+ if (check_add_overflow(res, val, &res))
+ rv |= KSTRTOX_OVERFLOW;
rv++;
s++;
}
--
2.54.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH -mm] kstrtox: delete division from inner parsing loop
2026-07-03 10:25 [PATCH -mm] kstrtox: delete division from inner parsing loop Alexey Dobriyan
@ 2026-07-03 10:34 ` Dmitry Antipov
0 siblings, 0 replies; 2+ messages in thread
From: Dmitry Antipov @ 2026-07-03 10:34 UTC (permalink / raw)
To: Alexey Dobriyan, akpm; +Cc: linux-kernel, andriy.shevchenko
On 7/3/26 1:25 PM, Alexey Dobriyan wrote:
> Overflow conditions can be checked on the fly resulting in at most
> 2 very predictable branches and zero division instructions:
>
> _parse_integer_limit:
> ...
> mov rax, rdx
> mul rsi
> seto bl
> add rax, rcx
> mov rdx, rax
> setb al
Trust my word, there is a huge world beyond x86.
Dmitry
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-03 10:44 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-03 10:25 [PATCH -mm] kstrtox: delete division from inner parsing loop Alexey Dobriyan
2026-07-03 10:34 ` Dmitry Antipov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®