From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Rodrigo Alencar <455.rodrigo.alencar@gmail.com>
Cc: rodrigo.alencar@analog.com, linux-kernel@vger.kernel.org,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-doc@vger.kernel.org, Jonathan Cameron <jic23@kernel.org>,
David Lechner <dlechner@baylibre.com>,
Andy Shevchenko <andy@kernel.org>,
Lars-Peter Clausen <lars@metafoo.de>,
Michael Hennerich <Michael.Hennerich@analog.com>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Jonathan Corbet <corbet@lwn.net>,
Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [PATCH v8 02/10] lib: kstrtox: add kstrntoull() helper
Date: Fri, 20 Mar 2026 16:17:57 +0200 [thread overview]
Message-ID: <ab1XFW6qsqKFdNXP@ashevche-desk.local> (raw)
In-Reply-To: <qsquhriyrepndxwexox35oi5i7v4klb64lajuk3j3pfozkjiew@fp55l4znck4o>
On Fri, Mar 20, 2026 at 12:41:57PM +0000, Rodrigo Alencar wrote:
> On 26/03/20 02:24PM, Andy Shevchenko wrote:
> > On Fri, Mar 20, 2026 at 12:08:41PM +0000, Rodrigo Alencar wrote:
> > > On 26/03/20 01:50PM, Andy Shevchenko wrote:
> > > > On Fri, Mar 20, 2026 at 11:16:32AM +0000, Rodrigo Alencar wrote:
> > > > > On 26/03/04 10:02AM, Rodrigo Alencar wrote:
...
> > > > > could you provide more feedback here? Thanks!
> > > >
> > > > I don't know what new I can add here.
> > > >
> > > > My suggestion was (and still is) to have something in *_strtoull() family
> > > > with additional checks added, but no limitations on the input string (i.e.
> > > > no max_chars). If you look at the printf() code the max_chars was added
> > > > solely for scanf() and has no use otherwise (yes, I know about and aware
> > > > of initramfs case).
> > >
> > > but is it include/linux/kstrtox.h the right place for this?
> >
> > Seems so, there simple_strto*() are declared.
> >
> > > *_strtoull familly... then can we just expose simple_strntoull(), which is
> > > private to lib/vsprintf.c, by changing its prototype to expose a error return?
> >
> > Why do you need that? I'm lost, sorry, I don't understand this big desire of
> > having that max_chars parameter.
> >
> > > In my case the limitation on the input string is useful for the truncation of
> > > decimal places when parsing the fixed point value. It would avoid a 64-bit
> > > division.
> >
> > How is it better than checking the returned end pointer? Just treat anything
> > that parses too many digits after dot as invalid input?
> >
> > ret = ..._strtoull(..., &end, &result);
>
> here I would want to pass max_chars as the precision of the fixed point parsing
>
> > if (ret)
> > return ret; // overflow!
> >
> > if (end - start > $YOUR_LIMIT)
> > return -EINVAL; // bad input
> >
> > ...process result...
>
> otherwise, here I would need to check the amount of parsed characters and
> perform a 64-bit division if it goes beyond the desired precision.
> Also, having max_chars allows for more flexible usage of the parsing function.
>
> this is the prototype of simple_strntoull() that would be thinking on expose:
>
> int simple_strntoull(const char *startp, char **endp,
> unsigned long long *res, unsigned int base,
> size_t max_chars)
>
> that would also allow to drop the existing FIXME in simple_strntoull().
That's fine, but the prototype should be rather
int simple_strntoull(const char *startp, char **endp,
unsigned int base, size_t max_chars, unsigned long long *res)
> > Some (stupid) thoughts loudly. IIUC even if we implement '%g' in scanf(), it
> > wont help you as you want to have more precise values. Do I get it correct?
>
> If I am parsing 3.14159265359 with 6 decimal precision I want to stop at:
>
> frac = 141592
> int = 3
This is different to what strto*() usually do. From my p.o.v. this is simply
an invalid input, and TBH, the parsing of this is not as trivial as flooring
the result. One might want ceiling it, and see the difference when it's about
signed value. So, if one wants lesser precision they need to set the rules,
and not the library, because there are several rules that the user may want
to adjust.
P.S.
Avoiding division in your case is copying a (sub)string and using kstrto*() on it.
I believe it's always a room for 20-30 bytes on the stack for that, and it will be
much faster than division, indeed.
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2026-03-20 14:18 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-03 13:27 [PATCH v8 00/10] ADF41513/ADF41510 PLL frequency synthesizers Rodrigo Alencar via B4 Relay
2026-03-03 13:27 ` [PATCH v8 01/10] dt-bindings: iio: frequency: add adf41513 Rodrigo Alencar via B4 Relay
2026-03-03 13:27 ` [PATCH v8 02/10] lib: kstrtox: add kstrntoull() helper Rodrigo Alencar via B4 Relay
2026-03-03 13:49 ` Andy Shevchenko
2026-03-03 14:16 ` Rodrigo Alencar
2026-03-04 10:02 ` Rodrigo Alencar
2026-03-20 11:16 ` Rodrigo Alencar
2026-03-20 11:50 ` Andy Shevchenko
2026-03-20 12:08 ` Rodrigo Alencar
2026-03-20 12:24 ` Andy Shevchenko
2026-03-20 12:41 ` Rodrigo Alencar
2026-03-20 14:17 ` Andy Shevchenko [this message]
2026-03-20 14:44 ` David Laight
2026-03-04 10:16 ` David Laight
2026-03-04 11:41 ` Rodrigo Alencar
2026-03-10 9:26 ` Rodrigo Alencar
2026-03-10 10:50 ` David Laight
2026-03-10 11:30 ` Rodrigo Alencar
2026-03-03 13:27 ` [PATCH v8 03/10] lib: test-kstrtox: add tests for kstrntoull() Rodrigo Alencar via B4 Relay
2026-03-03 13:27 ` [PATCH v8 04/10] iio: core: add fixed point parsing with 64-bit parts Rodrigo Alencar via B4 Relay
2026-03-03 13:27 ` [PATCH v8 05/10] iio: test: add kunit test for fixed-point parsing Rodrigo Alencar via B4 Relay
2026-03-03 13:27 ` [PATCH v8 06/10] iio: frequency: adf41513: driver implementation Rodrigo Alencar via B4 Relay
2026-03-03 13:27 ` [PATCH v8 07/10] iio: frequency: adf41513: handle LE synchronization feature Rodrigo Alencar via B4 Relay
2026-03-03 13:27 ` [PATCH v8 08/10] iio: frequency: adf41513: features on frequency change Rodrigo Alencar via B4 Relay
2026-03-03 13:27 ` [PATCH v8 09/10] docs: iio: add documentation for adf41513 driver Rodrigo Alencar via B4 Relay
2026-03-03 13:27 ` [PATCH v8 10/10] Documentation: ABI: testing: add common ABI file for iio/frequency Rodrigo Alencar via B4 Relay
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=ab1XFW6qsqKFdNXP@ashevche-desk.local \
--to=andriy.shevchenko@intel.com \
--cc=455.rodrigo.alencar@gmail.com \
--cc=Michael.Hennerich@analog.com \
--cc=akpm@linux-foundation.org \
--cc=andy@kernel.org \
--cc=conor+dt@kernel.org \
--cc=corbet@lwn.net \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-doc@vger.kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=robh@kernel.org \
--cc=rodrigo.alencar@analog.com \
/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®