All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] math: rational: avoid overflow when comparing the semi-convergent term
@ 2026-12-01 12:46 Joel Cruz Cabrera
  2026-12-01 12:46 ` [PATCH 2/2] math: rational: add a KUnit case for a semi-convergent term above LONG_MAX Joel Cruz Cabrera
  0 siblings, 1 reply; 3+ messages in thread
From: Joel Cruz Cabrera @ 2026-12-01 12:46 UTC (permalink / raw)
  To: akpm; +Cc: tpiepho, linux-kernel

rational_best_approximation() chooses between the previous convergent
and the best semi-convergent with "2u * t > a".  When t is
2^(BITS_PER_LONG - 1) or more, the product wraps, the test fails, and
the previous convergent is returned even though the semi-convergent is
much closer.  The result still respects max_numerator and
max_denominator; it is just not the best approximation.

t is bounded by max_numerator and max_denominator, so this needs a
bound of at least 2^(BITS_PER_LONG - 1).  For example, on a 32-bit
kernel

  rational_best_approximation(1, U32_MAX, U32_MAX, U32_MAX - 1, &n, &d)

returns 0/1 instead of 1/(U32_MAX - 1), and 2147484020/2147484019 with
max_denominator = 2147484017 gives 1/1 instead of
2147484018/2147484017.  64-bit kernels behave the same with bounds of
2^63 or more.  No in-tree caller is known to pass bounds that large.

Compare t with a / 2 instead.  This is equivalent and cannot overflow:
2 * t > a if and only if t > a / 2, and 2 * t == a if and only if a is
even and t == a / 2.

The other products in the function cannot overflow.  The Euclidean
algorithm gives d1 * d + d2 * dp == given_denominator and
n1 * d + n2 * dp == given_numerator, and d0 <= d2, so d0 * dp, d1 * d,
d2 and n2 never exceed the inputs.

The problem was found with the help of an AI assistant (Claude, by
Anthropic) while checking the lib/math helpers.  The assistant also
wrote the user-space test harness, proposed this change and drafted
this changelog; I reviewed all of it.  Testing: the unmodified function,
compiled in user space with 64-bit and with 32-bit unsigned long, was
compared with a brute-force and an exact 128-bit reference: exhaustively
for all inputs and bounds up to 120, on 4 * 10^7 random large inputs and
on targeted cases.  It is wrong only in the case described above; with
this change there are no mismatches.  The KUnit case added in the next
patch fails without this change and passes with it (kunit.py, arm64
and 32-bit arm under QEMU).

Fixes: 323dd2c3ed06 ("lib/math/rational.c: fix possible incorrect result from rational fractions helper")




Signed-off-by: Joel Cruz Cabrera <tf.joel.work@gmail.com>
---
 lib/math/rational.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lib/math/rational.c b/lib/math/rational.c
index d2c34e629..bf22f7f4c 100644
--- a/lib/math/rational.c
+++ b/lib/math/rational.c
@@ -90,8 +90,11 @@ void rational_best_approximation(
 			/* This tests if the semi-convergent is closer than the previous
 			 * convergent.  If d1 is zero there is no previous convergent as this
 			 * is the 1st iteration, so always choose the semi-convergent.
+			 * Compare t with a / 2 rather than 2 * t with a: t can be as large
+			 * as the bounds, and 2 * t would overflow.
 			 */
-			if (!d1 || 2u * t > a || (2u * t == a && d0 * dp > d1 * d)) {
+			if (!d1 || t > a / 2 ||
+			    (t == a / 2 && !(a & 1) && d0 * dp > d1 * d)) {
 				n1 = n0 + t * n1;
 				d1 = d0 + t * d1;
 			}

base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
-- 
2.50.1 (Apple Git-155)


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] math: rational: add a KUnit case for a semi-convergent term above LONG_MAX
  2026-12-01 12:46 [PATCH 1/2] math: rational: avoid overflow when comparing the semi-convergent term Joel Cruz Cabrera
@ 2026-12-01 12:46 ` Joel Cruz Cabrera
  2026-12-01 18:54   ` Trent Piepho
  0 siblings, 1 reply; 3+ messages in thread
From: Joel Cruz Cabrera @ 2026-12-01 12:46 UTC (permalink / raw)
  To: akpm; +Cc: tpiepho, linux-kernel

Add a case where the best semi-convergent term is ULONG_MAX - 1, which
made "2u * t" overflow before the previous patch: for 1/ULONG_MAX with
max_denominator = ULONG_MAX - 1, the closest fraction is
1/(ULONG_MAX - 1), not 0/1.  The case is written with ULONG_MAX so that
it covers both 32-bit and 64-bit kernels.

This test was written with the help of an AI assistant (Claude, by
Anthropic); I reviewed it.




Signed-off-by: Joel Cruz Cabrera <tf.joel.work@gmail.com>
---
 lib/math/tests/rational_kunit.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lib/math/tests/rational_kunit.c b/lib/math/tests/rational_kunit.c
index 47486a95f..c8dac1904 100644
--- a/lib/math/tests/rational_kunit.c
+++ b/lib/math/tests/rational_kunit.c
@@ -2,6 +2,7 @@
 
 #include <kunit/test.h>
 
+#include <linux/limits.h>
 #include <linux/rational.h>
 
 struct rational_test_param {
@@ -21,6 +22,8 @@ static const struct rational_test_param test_parameters[] = {
 	{ 1155, 7735,	255, 255,	33, 221,   "Exact answer" },
 	{ 87, 32,	70, 32,		68, 25,    "Semiconvergent, numerator limit" },
 	{ 14533, 4626,	15000, 2400,	7433, 2366, "Semiconvergent, denominator limit" },
+	{ 1, ULONG_MAX,	ULONG_MAX, ULONG_MAX - 1,	1, ULONG_MAX - 1,
+	  "Semiconvergent, term above LONG_MAX" },
 };
 
 static void get_desc(const struct rational_test_param *param, char *desc)
-- 
2.50.1 (Apple Git-155)


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 2/2] math: rational: add a KUnit case for a semi-convergent term above LONG_MAX
  2026-12-01 12:46 ` [PATCH 2/2] math: rational: add a KUnit case for a semi-convergent term above LONG_MAX Joel Cruz Cabrera
@ 2026-12-01 18:54   ` Trent Piepho
  0 siblings, 0 replies; 3+ messages in thread
From: Trent Piepho @ 2026-12-01 18:54 UTC (permalink / raw)
  To: Joel Cruz Cabrera; +Cc: akpm, linux-kernel

On Tue, Dec 1, 2026 at 4:46 AM Joel Cruz Cabrera <tf.joel.work@gmail.com> wrote:
>
> Add a case where the best semi-convergent term is ULONG_MAX - 1, which
> made "2u * t" overflow before the previous patch: for 1/ULONG_MAX with
> max_denominator = ULONG_MAX - 1, the closest fraction is
> 1/(ULONG_MAX - 1), not 0/1.  The case is written with ULONG_MAX so that
> it covers both 32-bit and 64-bit kernels.
>
> This test was written with the help of an AI assistant (Claude, by
> Anthropic); I reviewed it.

Your AI is finding "bugs" that will never be triggered.

This function is used to find integer ratios for the PLLs that fit
into 6, 8, 10, etc. bits.  However many bits the PLL has.  There is
nowhere in the kernel that a max_denominator larger than 32 bits is
ever going to be used.  I doubt there's an existing callsite with a
larger than 16 bit value.  The code could be kept faster and smaller
by just documenting the max_denominator should be <= UINT_MAX, a
restriction that will affect no one.

Values larger than 63 bits, like these, are even more unrealistic.
The entire point of this function, which the AI totally missed, is to
*reduce* the number of bits it takes to represent a rational fraction.
If the input is 64 bits and the output is also 64 bits, then it's
pointless to even call it.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-01 18:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 12:46 [PATCH 1/2] math: rational: avoid overflow when comparing the semi-convergent term Joel Cruz Cabrera
2026-12-01 12:46 ` [PATCH 2/2] math: rational: add a KUnit case for a semi-convergent term above LONG_MAX Joel Cruz Cabrera
2026-12-01 18:54   ` Trent Piepho

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.