From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Shuvam Pandey <shuvampandey1@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Kees Cook <kees@kernel.org>,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH] lib/string_helpers: fix string_get_size() unit promotion after rounding
Date: Tue, 14 Apr 2026 10:19:20 +0300 [thread overview]
Message-ID: <ad3qeKiW1jIKHOMi@ashevche-desk.local> (raw)
In-Reply-To: <CANAAWHKZXbFAr7=DRCrS46GDsWi88Q=v883RQrgF4+EScksP=w@mail.gmail.com>
First of all, do not top-post!
Second, I just realised now that the discussion was done in private emails, do
not do this. Please, answer again with all what was discussed to the public
mailing lists as this message does.
On Fri, Apr 10, 2026 at 09:11:59PM +0545, Shuvam Pandey wrote:
> I think the open question is whether string_get_size() is intended to
> round across a unit boundary at all.
> There seem to be two possible interpretations:
>
> 1. Keep the current 3-significant figure rounding, and if that rounding
> carries to the unit divisor, renormalize into the next unit. That is
> what my patch does.
>
> 2. Treat the unit boundary as hard, so values below 1.00 MiB / 1.00 MB
> stay below that threshold and are not rounded across it.
>
> If (2) is the intended behavior, then this is broader than the
> carry-renormalization case and my patch is not the right fix.
>
> Which behavior should string_get_size() implement here, or am I
> missing a third intended behavior?
>
> On Thu, Apr 9, 2026 at 1:55 AM Shuvam Pandey <shuvampandey1@gmail.com>
> wrote:
>
> > I checked the additional base 10 values as well.
> >
> > For blk_size = 1, the current code gives:
> >
> > 999999:
> >
> > STRING_UNITS_10: before 1000 kB, after 1.00 MB
> >
> > 1000000:
> >
> > STRING_UNITS_10: before 1.00 MB, after 1.00 MB
> >
> > 1000001:
> >
> > STRING_UNITS_10: before 1.00 MB, after 1.00 MB
> >
> > 1048575:
> >
> > STRING_UNITS_2: before 1024 KiB, after 1.00 MiB
> >
> > 1048576:
> >
> > STRING_UNITS_2: before 1.00 MiB, after 1.00 MiB
> >
> > 1048577:
> >
> > STRING_UNITS_2: before 1.00 MiB, after 1.00 MiB
> >
> > So this patch only changes the carry-to-divisor cases, where the current
> > code rounds the value up but still prints it in the old unit.
> >
> > That said, I think your point about the broader semantics is valid.
> >
> > string_get_size() has long rounded to 3 significant figures, and my
> > change only makes that existing behavior consistent after a carry. If
> > intended behavior is instead that values like 1048575 should stay
> > below 1.00 MiB, then the real issue is not just the missing promotion
> > here, but the rounding policy itself.
> >
> > So I think the patch is correct for the current implementation, but it
> > may not be the right fix if the intended behavior near unit boundaries
> > is supposed to be different.
> >
> > I'll take another look at the expected behavior around these boundary
> > cases and follow up once I've re-evaluated that.
> >
> > On Thu, Apr 9, 2026 at 1:38 AM Andy Shevchenko <
> > andriy.shevchenko@intel.com> wrote:
> >
> >> On Tue, Apr 07, 2026 at 08:48:59PM +0545, Shuvam Pandey wrote:
> >> > For blk_size = 1:
> >> >
> >> > 1048575:
> >> > STRING_UNITS_10: before 1.05 MB, after 1.05 MB
> >> > STRING_UNITS_2: before 1024 KiB, after 1.00 MiB
> >> >
> >> > 1048576:
> >> > STRING_UNITS_10: before 1.05 MB, after 1.05 MB
> >> > STRING_UNITS_2: before 1.00 MiB, after 1.00 MiB
> >> >
> >> > 1048577:
> >> > STRING_UNITS_10: before 1.05 MB, after 1.05 MB
> >> > STRING_UNITS_2: before 1.00 MiB, after 1.00 MiB
> >> >
> >> > Likewise around 2 MiB:
> >> >
> >> > 2097151:
> >> > STRING_UNITS_10: before 2.10 MB, after 2.10 MB
> >> > STRING_UNITS_2: before 2.00 MiB, after 2.00 MiB
> >> >
> >> > 2097152:
> >> > STRING_UNITS_10: before 2.10 MB, after 2.10 MB
> >> > STRING_UNITS_2: before 2.00 MiB, after 2.00 MiB
> >> >
> >> > 2097153:
> >> > STRING_UNITS_10: before 2.10 MB, after 2.10 MB
> >> > STRING_UNITS_2: before 2.00 MiB, after 2.00 MiB
> >> >
> >> > So the change only affects the carry-to-divisor case itself; values that
> >> > are already in the next unit stay unchanged.
> >> >
> >> > I also rechecked the neighboring value 1048574, and it changes from 1024
> >> > KiB to 1.00 MiB as well. I can add coverage for 1048574..1048577 in v2
> >> if
> >> > you think that would be useful.
> >>
> >> Also with base 10 for 999999 1000000 1000001...
> >>
> >> I'm trying to understand the current behaviour better and the logic of
> >> your
> >> change. But looking at the above I think your patch doesn't improve the
> >> case.
> >> And might even be another bug in the current code. I mean, do we have to
> >> round
> >> the numbers? 1048575 and lower are not equal 1.00 MiB strictly speaking.
> >> It's rather 0.999 MiB.
> >>
> >> > On Tue, Apr 7, 2026 at 8:38 PM Andy Shevchenko <
> >> andriy.shevchenko@intel.com>
> >> > wrote:
> >> >
> >> > > On Tue, Apr 07, 2026 at 08:18:06PM +0545, Shuvam Pandey wrote:
> >> > > > string_get_size() rounds the fractional part before formatting the
> >> > > > result. If that carry pushes the integer part to the unit divisor,
> >> the
> >> > > > value is still printed in the old unit.
> >> > > >
> >> > > > This yields outputs like "1000 kB" for 999500 bytes and "1024 KiB"
> >> for
> >> > > > 1048064 bytes instead of promoting them to the next unit.
> >> > >
> >> > > What does it print for 1048575, 1048576, 1048577 respectively?
> >> > > (before and after your patch)
> >> > >
> >> > > All the same for numbers around 2MiB.
> >> > >
> >> > > > Renormalize the value after the carry so it is promoted before
> >> > > > formatting. Add KUnit coverage for the decimal and binary boundary
> >> > > > cases.
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2026-04-14 7:19 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-07 14:33 Shuvam Pandey
2026-04-07 14:53 ` Andy Shevchenko
[not found] ` <CANAAWH+ihOpBVB5_Q4gxLs0_zO=pHb-kF53UXE-pqg1U9E9OKw@mail.gmail.com>
[not found] ` <adayT_7xAtOLiVlq@ashevche-desk.local>
[not found] ` <CANAAWHKDBoi1x=MpAyU5bQ88=ZOOrkaJNziyhcK9Ei7c8oNSHg@mail.gmail.com>
[not found] ` <CANAAWHKZXbFAr7=DRCrS46GDsWi88Q=v883RQrgF4+EScksP=w@mail.gmail.com>
2026-04-14 7:19 ` Andy Shevchenko [this message]
2026-04-14 8:06 ` Shuvam Pandey
2026-04-19 15:31 ` [PATCH v2] lib/string_helpers: renormalize string_get_size() after rounding carry Shuvam Pandey
2026-04-20 6:56 ` Andy Shevchenko
2026-04-21 3:04 ` James Bottomley
2026-04-21 3:38 ` Shuvam Pandey
2026-04-21 4:53 ` Andy Shevchenko
2026-04-21 10:10 ` Shuvam Pandey
2026-04-21 12:16 ` James Bottomley
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=ad3qeKiW1jIKHOMi@ashevche-desk.local \
--to=andriy.shevchenko@intel.com \
--cc=akpm@linux-foundation.org \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=shuvampandey1@gmail.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®