From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.181 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796129220; b=VPZ9xaTWOilTOAPwxRdo2JZf+DOFpxoFQoaWGN/Lf6ozbIN+y1xH/mwqITtU3EHviVnf Yy8/GYXj9qQuFwiBcg8YpxWs96rGyYvroqsjWeQT7pNqZPb51QAbb9bOyPlVXhvdU1jtv qhx7BmjFl/nr1jccuqfG6+uxNQbSABqgNd+Hp3+Zf36HeSEIYJAWfwyylMr9HlqPhbYVh fWiGTriofo1w0zl5suNLQRU0tvqIniEGInjquJ+EWwgOetFTOGgeo/YSC6Kv7HOdxyqzu r4MPDuumi1+mtjiWIeC76E4LCQI94X7TPPR4SbLReB3lbcMtUoTo4yMu4sg7uUfSPeg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796129220; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=b6ajarrV2jjCYpn9ZKYyK/eET8+tQ4iFKhySd38Sx0U=; b=uDwZRWc2BOHAYgWrulOJt5RWTGBef3ax5lBW4tFo1u7NJBBmMAf7G43ZZlr7U5BgUuZk A+SCi0nNxghp2zSRYO4RhctT0/cwTkYaMhfezC3y5dUv2Igw7yZw9HcPSUS2bWMzNN+W9 gwanW0zrMQzhyCaB80MyYCrTX9ZcefxobiV3b59DGL7GGoh+doxNff+47jgTIdKFr4RWl XN7Lxsi0Cn/3NCTOPZQsVx6Mw3ntM2IkLIAIIyhk24ChkQ3VNWGk5XH2kkqwfo6HCZjXk 9yK0e2XMqBa4oKr73QRLh6moKHhSYdZi+DRiMNvdNieaf7+KZML8K4+3XmwOkx4o2Pw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=EecmQaPQ; arc=none smtp.remote-ip=209.85.160.181 Received: from mail-qt1-f181.google.com (mail-qt1-f181.google.com [209.85.160.181]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9954C606A334 for ; Tue, 1 Dec 2026 12:46:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9954C606A334 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.a=rsa-sha256 header.s=20260707 header.b=EecmQaPQ Received: by mail-qt1-f181.google.com with SMTP id d75a77b69052e-5495d549924so122852911cf.1 for ; Tue, 01 Dec 2026 04:46:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796129200; x=1796734000; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=b6ajarrV2jjCYpn9ZKYyK/eET8+tQ4iFKhySd38Sx0U=; b=EecmQaPQfZhMiQLUL754mnz1rttuY9dYERmkP6o7lVJOZTsVgOxYlu7CqOukrxdYO0 t9vFJdQn5B5s/UMGtXR5c2pk3S/LnPccafmIK/QEA+R5ifoUtLowQNe2Sr0ov6xHV4Q7 8OvDDTwKOFLnorJUmrwtXs3lTaP9N1bbcvrrJVV9ZULSfmZXEr51fWRQkyWshxHaACRi ARzVFsMuCG7Zy/eKF4sjA2IVkESApjPkQKzkyr66o1oz7FzVc5LZ5cVBLzAdWKUh/qMa 7rFuypPsSPpmuJNfoYyzYcEBosH+ApQl3gGKbvYrqbO99QS0UYvqwM1oupbxUHG9pLnw wXuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796129200; x=1796734000; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=b6ajarrV2jjCYpn9ZKYyK/eET8+tQ4iFKhySd38Sx0U=; b=JGJxI48rjGok+0UDZS8PeFPKvapTGgqYxySnf71Vv/pc0BofOdaib7+xxrXcoPF4qZ lydpF+kRWMcYL6uojvt6M4FPFuGzV7mN/vlm/nu6j4m0x+pux4fjpx6vck1s6LBWtESN azzdzfnvRBsROYaLcc9bI/1znSIUCBzWT+d4BvfHRrk8EVg6ZH0irU7Ew+R4UCy9rDvv C24JoLdNK/ohIqQHP/Qw33Pp5M3idTQbVCA6aaUhCbr1wVFmb4YJmambWFkeIvi92zQN exAewMblLXTl2sVzlzVH8mwjZJS12vcQGjkPBLlHLQkFxgcXgZuh331XHZMlckQSANhz 0lnQ== X-Forwarded-Encrypted: i=1; AL+hErERfsrRu4JKOKuCUmvsOXxaUBFYUR4/7zXhfJvsWPG4qa1jTs5hk3mI4lzEdsLxptlUIA9e3dWQkEV60KI=@vger.kernel.org X-Gm-Message-State: APuU4Z9GwUsj5czr6i7k3rQkKJOdgZvQBNmdwN0q6pJBJ/arjX+MDREA SibkBAL3Btpo7QR9VfcaPiBWDSxQUTnCwhjqsup6IJ2iK+cc3Ysp0zWf X-Gm-Gg: AZFdTZfgo66b9qbwn/8sfAvp26p8WtVw0WdtvIOQd8HB2jPjm+tvQPCphX8OJDcLnbS Xdp755XDupWw8jFnAgVAiaTen/AifH0Pqq5ieCqj753sIHznqHzRm8OJkTSFcpunYgYfgtlaFuz 0NFh7tWS9sTmPGjeVFqIba3iwKrceGMw0TJV4MUBc5RtIlPh3XLckAPl5gHLTCV9q1mIKpgQ2ji 6IJMFSNPSXyUNmJZTx7TdMTFAeKynTu0Zsw0XC5Wkbc1y0YAviQQ2lw2iqwpCMZVoXeVOS85M62 5i8XJPv8BUyaTiUByjUsbIei2E97p70MsAKCADYcrBsKXOJtePTiyqWy+MkC6ot2CnHBHVSJB03 Zbp334FVcZTliSc3aqhtH1ci/RgRro0fcMpf9UHP6XBDzrCHgHw3BDTKCYW1gcuACgml0UPcHDb ZsSF9llHm6+UjgtXhUAlrpXjx2YQHUyiK1uO+KJN4aKMaA6pIIFjaKZnambJBAezlZDSoBrRoHW qqvyxWub2YeJr4Eu47F5aAHLVQO6djBxvGd+eovIkQsl/9fr4ypUxEo8pUFr6sLOYz7CGHGR1wk Ft+DG+oIFGsphMZG75spKszIIZhv5+AATrtOXy2wYfHRV13ApZOLt+K6vodSzhSYLy97Ibaumnl zAmM6NsFMo2BknU1KYkMeZNLA81CxQgrJ7PXrm2gQyVeCGmivx7TIt0bPXhhtMuM/FS0ISIyY9m tNnVedRZMwmnd5TUeGGGhq9xkJuSVLB470gDn+5cxkTo2nCjq1EFaeah2XePt6U9z5HJ/TCDgm5 +yUm5RWrV5t5b55+UCAiKf2Ip73YuJ8xRpjIy/vDxMtWYd3Q9MXPQucBhfc+wJM1Pjmj+tp9IBj 2FYb X-Received: by 2002:a05:622a:3d3:b0:54d:8cea:50bb with SMTP id d75a77b69052e-55e036c480fmr40779281cf.66.1796129200609; Tue, 01 Dec 2026 04:46:40 -0800 (PST) Received: from localhost.localdomain (static-23-234-123-69.cust.tzulo.com. [23.234.123.69]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-55d78e4fc12sm306148361cf.0.2026.12.01.04.46.38 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 01 Dec 2026 04:46:40 -0800 (PST) From: Joel Cruz Cabrera To: akpm@linux-foundation.org Cc: tpiepho@gmail.com, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] math: rational: avoid overflow when comparing the semi-convergent term Date: Tue, 1 Dec 2026 12:46:33 +0000 Message-ID: <20261201124634.48970-1-tf.joel.work@gmail.com> X-Mailer: git-send-email 2.50.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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)