From: Alexey Dobriyan <adobriyan@gmail.com>
To: akpm@linux-foundation.org
Cc: adobriyan@gmail.com, linux-kernel@vger.kernel.org,
dmantipov@yandex.ru, andriy.shevchenko@intel.com
Subject: [PATCH -mm] kstrtox: delete division from inner parsing loop
Date: Fri, 3 Jul 2026 13:25:50 +0300 [thread overview]
Message-ID: <20260703102550.501490-1-adobriyan@gmail.com> (raw)
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
next reply other threads:[~2026-07-03 10:22 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-03 10:25 Alexey Dobriyan [this message]
2026-07-03 10:34 ` Dmitry Antipov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260703102550.501490-1-adobriyan@gmail.com \
--to=adobriyan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@intel.com \
--cc=dmantipov@yandex.ru \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®