From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B8D1B382369 for ; Mon, 20 Apr 2026 06:56:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=209.85.218.48 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776668206; cv=pass; b=F/86biSHV2jiPzhEdaS8fIXihYhGIPolX7BnTTcjWmnL3H2JaFvQj4LvKvL1pNjVcmXNaTlNyFlpqxzD2/hXrFN94s09hFG/EpVG9LqzHHggna8hMALm1mEzy3aKJmmsiTDfU9HLbb41QNznXB8CU5R8twK8sFc6HVnY1VnR/H8= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776668206; c=relaxed/simple; bh=q2h2S+ybPyUuORfyPrVUgIx6M1XZ4AInwFwtr0lmqIU=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=bu5TAv/M1jEJ4WRn6ssSz3FtnHeeam3wjuMJQRKLT7Lx151hwQMtfAKGM3r7FozI1pLnhjTqENqYZcuoJ3CGYF7+S9N9Q2GdAoh/Xb7Cu3D2PDV8GLEf/4SGre3D3VQS3z1F69Jtxn8Rhdi1NZ+6t0CPgGDPY+IZB9LhGfaVEbo= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Qna4vtBj; arc=pass smtp.client-ip=209.85.218.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Qna4vtBj" Received: by mail-ej1-f48.google.com with SMTP id a640c23a62f3a-ba25fd27571so386189566b.0 for ; Sun, 19 Apr 2026 23:56:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1776668203; cv=none; d=google.com; s=arc-20240605; b=UWW4BLrf7B+i1JBNvqZUkFB3H/XCQ+KhbJuZ77rAgpQ7Q0oNNTwyJvF/dkVgph7FnK imobta3AeMA9OX6IIQF6MjdMOyPrhh0rFrbNi1NBsPnyQ/tAe1ZoYaG9bN049Rz0h12e +rWsqZt33gslLKqhIlKCq0CzYSMePJ3pFRdmmSavkGg4BNgSEL2MsoOg8JyFvyJKcHM3 +AYEETAHAloTxhqmZCBKJUV4OIC04bYLRQ3CB2C/1DBQn2Z7VUDLF1/ZYME/uuTUZ4To Gb5hbXfP+0R+nXQoV1z5M01uSYVSKCb5037NQRLfhBnVleYqi0mE9JzYHivJSmH+U7xH 8vhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=bVaZtqMT64CADYWqi8aDaSmUgUoGBxSGLKSrX44BRFQ=; fh=yZIbMrBNNvK31FCFXEk/Gg6ViI1Sg0D3xBkY34I8Unc=; b=RZL/HZ4z2DF8pxzr96b22H3Fjy0LFoqMJXEatuBT/ghwi3AeaWC7zIvZZCZDm6tHMm gyb/HnTrvBtIyyrIArDu03K2+p8wtNj9ywsD0u/wyGf9DQY5Zwg+EhLaT1Ri6k6nyKfQ 4uis2GZaOHzEIfhxbQWX6V4G6aVlMM94LX+rnRet8gcW09cwaaTZQlEWn3a9bkgv+1gu F5wXegPLLLxu3/ZvHl3h+USaL4KOgZaJr/HBTAAN2N8ZpX4NK7CU4ge7nwJmwTQ9Bx65 EODDntmn9q5AdZ0zCRRJQTPH6CswE1ZJLIYfml2MwAKX1Mm+W4m6IKmitIqHsmBLF5gq Uk4Q==; darn=vger.kernel.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776668203; x=1777273003; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=bVaZtqMT64CADYWqi8aDaSmUgUoGBxSGLKSrX44BRFQ=; b=Qna4vtBjfGS7sTxzR8ymtlzx4QPNFBz5P3mM8jM3E1pOUvueMr/RT9o/vDxb8Lu6fG tTxSC3/YZmqcbb1Q+w3JyOeM7PZFJfFOdF47wlDHsHY0Cc5q8vMX/SqLQzUwAP5wgqJP 4vDd39XaA45+A87GZkxdyMbA9rBJpUOjhqb06carxWwiZ2Viam3ZOv4gfSzl/H3SOnzX SGWJhTbRsQuek7RIVtx496QP5bCWZu/ppUiVYi/Fa0VM0UjLtVl2P+hG9/hzyOVc1yRR JvY7cppG+l33iquD1z4FyzWUKbNCIA3jtVeE4ns200nPkOi+p9tsQX8xRbHDJbTcFvg1 T9qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776668203; x=1777273003; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=bVaZtqMT64CADYWqi8aDaSmUgUoGBxSGLKSrX44BRFQ=; b=idzAR6XLbcOApWLnmQFJF4CbUFXNIXjlBxHOPwaKuiPece6yNeYx0hmqtTauXv1sRr Kce6oCby57R/pJJx88kHSkWkXQqo5LX+ihB7XfIqh0UCgc/7uCz7pNryiThI9Gbxh9EK WIGCIF0s1Ed0cVkT6mkMpJYBAh2oZUAmwDsEpy7CYvI8u1ORVZH1ode/b52cv5FJvGH2 mZD4T/F7tQISS2OMs6CiTzEUnsye2AF5NRmGjRU6oYuO2GIUK7iNB1wPe0OFkBaBHBxy El5FJJyRU3cNxLtGMunzurrxKgUGdU+rit/WOQlkZNKCUiJ0uF7C7+ytk7t7OU9mQfWO vEWw== X-Forwarded-Encrypted: i=1; AFNElJ+rXJZFYHChAnjJGz5d1KO7PJUtrf/V9Fs8xHykgBzZWdJfiOXyumg8LB2ArfMsmWYenrWYRz2nGuoNkC0=@vger.kernel.org X-Gm-Message-State: AOJu0Yyqmgxz9YpnhGmwWu5Loedf//0eXWuPALOpLbJKjmku/iZWBa3O 2eg97wH4fc8fI9/AGNL+MaiQ8j38ED31fMPXzLv+dvaTA+VzkCYXjHxob3oDs6BhnMMNCV8hHFk pbs21/jarG8IXHVANroS8e8Tt9+06GIo= X-Gm-Gg: AeBDiesyoEgZub5z1CQbcMFyPYtsavvVjfAcI7Pn2OwENRlSwUXgTW1YXInWfxTbWzx 4WcYgYdjwVnsb5eHCI6DoS1GibhIYduCGwhvwPpe7fZbwRM/RaErXAaBdZ1yZkAnwhFsAmux/tQ M+B0p7xbf09+/XjX/aKayg1LMsLyBPCTMH6EjorXKmtePGHvfCty8CxdblUkWUzGEZQrvW/oklf TG9Sd8qxbR2eXUs1vfHnuJJqHurgTiEUXgScnGYYIRI4rlWG6HaVnis+gj3lBZcSKiYKL8hr+H2 fseRkT/ciNhoHGMTJ7yapeBPm0CNpoZ3K28PDmasyGfE6x/ErtkG0qwcgQNG+oDTsfXRS8utV3K 1cbN2wVxwxdXNw/RNpg== X-Received: by 2002:a17:906:4fc5:b0:b97:aea3:86dc with SMTP id a640c23a62f3a-ba419074424mr620683866b.15.1776668202651; Sun, 19 Apr 2026 23:56:42 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20260407143306.78809-1-shuvampandey1@gmail.com> <20260419153111.90635-1-shuvampandey1@gmail.com> In-Reply-To: <20260419153111.90635-1-shuvampandey1@gmail.com> From: Andy Shevchenko Date: Mon, 20 Apr 2026 09:56:05 +0300 X-Gm-Features: AQROBzBB7sr9V9y3776Xm-nQgbVWK8ePbaEINiPI1wlUAH7f5fhqOgGYD5-wWmI Message-ID: Subject: Re: [PATCH v2] lib/string_helpers: renormalize string_get_size() after rounding carry To: Shuvam Pandey , James Bottomley Cc: Kees Cook , Andrew Morton , Andy Shevchenko , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Please, keep the author of the original change in the loop. James, what do you think? On Sun, Apr 19, 2026 at 6:31=E2=80=AFPM 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 > helper still prints the old unit and produces strings like "1000 kB" > and "1024 KiB". > > That misformatted carry case comes from the arithmetic rounding added by > commit 564b026fbd0d ("string_helpers: fix precision loss for some > inputs"). The helper already rounds those values up numerically; it > just fails to renormalize them into the next unit before formatting. > > Renormalize the carried result into the next named unit so those cases > print as "1.00 MB" and "1.00 MiB" instead. Only do that when another > named unit exists, so the existing "UNK" fallback is preserved at the > top end. Extend the KUnit coverage around the first decimal and binary > boundaries, including adjacent values and non-byte block sizes that hit > the same carry path. > > Fixes: 564b026fbd0d ("string_helpers: fix precision loss for some inputs"= ) > Signed-off-by: Shuvam Pandey > --- > Changes in v2: > - correct the Fixes tag to 564b026fbd0d > - renormalize only when another named unit exists > - preserve the top-end UNK fallback > - add decimal and binary boundary/neighbor KUnit coverage > - add non-byte block-size cases that hit the same carry path > > lib/string_helpers.c | 12 ++++++++++++ > lib/tests/string_helpers_kunit.c | 25 +++++++++++++++++++++++++ > 2 files changed, 37 insertions(+) > > diff --git a/lib/string_helpers.c b/lib/string_helpers.c > index 169eaf5834949..bcabb24fe0dbf 100644 > --- a/lib/string_helpers.c > +++ b/lib/string_helpers.c > @@ -121,6 +121,18 @@ int string_get_size(u64 size, u64 blk_size, const en= um string_size_units units, > size +=3D 1; > } > > + /* > + * Renormalize into the next named unit, but preserve the top-end > + * UNK fallback. After promotion the value is exactly 1 of the ne= xt > + * unit, so keep two fractional digits for the usual 1.00 formatt= ing. > + */ > + if (size >=3D divisor[units_base] && i + 1 < ARRAY_SIZE(units_2))= { > + size =3D 1; > + remainder =3D 0; > + i++; > + j =3D 2; > + } > + > if (j) { > snprintf(tmp, sizeof(tmp), ".%03u", remainder); > tmp[j+1] =3D '\0'; > diff --git a/lib/tests/string_helpers_kunit.c b/lib/tests/string_helpers_= kunit.c > index c853046183d24..67822cb48fd15 100644 > --- a/lib/tests/string_helpers_kunit.c > +++ b/lib/tests/string_helpers_kunit.c > @@ -558,6 +558,31 @@ static void test_get_size(struct kunit *test) > /* weird block sizes */ > test_string_get_size_one(3000, 1900, "5.70 MB", "5.44 MiB"); > > + /* rounding carry into the next unit at the first decimal boundar= y */ > + test_string_get_size_one(999499, 1, "999 kB", "976 KiB"); > + test_string_get_size_one(999500, 1, "1.00 MB", "976 KiB"); > + test_string_get_size_one(999999, 1, "1.00 MB", "977 KiB"); > + test_string_get_size_one(1000000, 1, "1.00 MB", "977 KiB"); > + test_string_get_size_one(1000001, 1, "1.00 MB", "977 KiB"); > + > + /* rounding carry into the next unit at the first binary boundary= */ > + test_string_get_size_one(1048063, 1, "1.05 MB", "1023 KiB"); > + test_string_get_size_one(1048064, 1, "1.05 MB", "1.00 MiB"); > + test_string_get_size_one(1048575, 1, "1.05 MB", "1.00 MiB"); > + test_string_get_size_one(1048576, 1, "1.05 MB", "1.00 MiB"); > + test_string_get_size_one(1048577, 1, "1.05 MB", "1.00 MiB"); > + > + /* values already in the next binary unit stay unchanged */ > + test_string_get_size_one(2097151, 1, "2.10 MB", "2.00 MiB"); > + test_string_get_size_one(2097152, 1, "2.10 MB", "2.00 MiB"); > + test_string_get_size_one(2097153, 1, "2.10 MB", "2.00 MiB"); > + > + /* non-byte block sizes hit the same carry path */ > + test_string_get_size_one(4997, 200, "999 kB", "976 KiB"); > + test_string_get_size_one(4998, 200, "1.00 MB", "976 KiB"); > + test_string_get_size_one(9981, 105, "1.05 MB", "1023 KiB"); > + test_string_get_size_one(9982, 105, "1.05 MB", "1.00 MiB"); > + > /* huge values */ > test_string_get_size_one(U64_MAX, 4096, "75.6 ZB", "64.0 ZiB"); > test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB"); > -- > 2.50.1 (Apple Git-155) > --=20 With Best Regards, Andy Shevchenko