From nobody Wed Jun 17 03:00:20 2026 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 C5139365A17 for ; Tue, 21 Apr 2026 18:25:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776795946; cv=none; b=EpGR82/kKxN8MTQogFhmeGQDJeSebrm9y1GdWjn2UOQOBLC+CJlOoSEu67uiwzPEHeNQZVJ/pr7StQ07zS+XVGUxN2+ohUn1f7OIKTpOX3mhwgVkdAzEKo5x3Oa9hqocopSQM3LIOhetPbxbbXTmZ7C/fkPE7xbRBthjKR7LeXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776795946; c=relaxed/simple; bh=Re5gJZwAmHy8llKKOsNMx2M/78ANUl9lB5LH0gmbCWc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RppD9NUXE4km0dDhiC1iJ6JA11P+6BAMXXg4EXnW+DGREVZf0yc3WNiteiyQ417ibTajIV7fF7OkLVKl5pQS3BOGbIlbnJ3yXuz5//o0tYTlEhQrKQpk+32yt6NttEfsfKshElG4WAneGuAQH4Mbz8gLvGt5VOh/DAEOZZMzJis= ARC-Authentication-Results: i=1; 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=mhNLA2qt; arc=none smtp.client-ip=209.85.128.41 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="mhNLA2qt" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-488b150559bso36224715e9.1 for ; Tue, 21 Apr 2026 11:25:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776795934; x=1777400734; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=I4zFajylv2hEREauigciCo0bW3w7NfjVPW0rAl01j5Q=; b=mhNLA2qtaUpGF7vhK6RA7P2Xl9yEO5u5Q2juTZ9VXG9ldRXnNAE/zlx0rtyFzbTlc4 EJsn5WmUVp0GnVJjMV7l09ABRV3vUluAAuTGP4ik9l5V5QTNz2VCdctcFqQCBB3f/YS8 kx6D7tts5cMbcVJqryxIBdl7hizCl7/dK61OlMV5gibBmNoXty5k8Zo3AOhiOl8xxxTF xBMXEnwyVdAwG10pW0QKvfMwgq50y1rvDCqQfHcaeMMTiG81WtUXW6+L5ZrOC77vXL6N x9B2aM9aEAuiLeXfDPwsOgIcK+B+SsMOqrYzFWkmSfs7Vo+jFhoveY3XyRVkMVF8ljKC lMLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776795934; x=1777400734; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=I4zFajylv2hEREauigciCo0bW3w7NfjVPW0rAl01j5Q=; b=o9o+VMmCyNUPpOgXxv944J8FKnteDe/OT3Waf5Yxj7Mgyg3FGCwdgovGQkfg8yf3Ac 1KDr2fqtExzfghfkvBvNe5T6q4i9Jm5otMWu2RLga6WrgcqMLo8u819G1f8w4HiDE8DN ocAQIZtRIslGPr7a2+Qg0ioO3XEhObbcwHsg4G7b85wo8c8p0xaaNRi7Liy0k5tmSj9F EF+QhzV6fC6wdVFhVb4zFxRtdw/EXg1yGDeAY5trTSqY9Rx0RPI4fk89xQKaLDsVBiBm ugsHql0g6kKPcp+Fv4YPBFqoSQ2OOymE2UstIcV5v/T4ZAOzJTK65Up4+0mNeNDxbumq LqHQ== X-Gm-Message-State: AOJu0YxCymzBO8Qc6yr2ZiyhslDPoUd1UueWwOSi3LoTSR0JjP6oPR7Q 0ty9/a6CguC8xuzea5gSx0A1ISo6AgPHiH/q4Zlx88LWxzoDgSNuc7yE X-Gm-Gg: AeBDiet/LEekp0n6KpIkq8cZJkR2+GTJoYLiaBLhoN7gfYZyiPN7WGCONJ+AiZmM6g/ ZnY/LTaIcUZ3nr3w5SUl16eiicB9q3vweob2+CwEnp1i36l1HWjeBgm+4lhAI2bTy1jXyyV3VQv bKGe6m3LZvm4k/kZEw0F1VJi8YPBakywTRS2EHJMD6Mw6h7UmNRzaHpUyfoC6hikfiFQ087iDDL Q5wxubU2EFFemzvCZS1CBdHHedU7iDuFS/T29dcEsg69WdpVsC7+qFaHuMMyv02e66KIV1mLhwM Hya+ebWQ8Tjc42vGCSPq2zfWPD9rbgWZWf+gAbr0YXpkra5BMwKnG16AYcynTcZ2ejovevo82WW YPAGfmBS4OHfwpcCwnZnFXkfyIXPQRqaJM3ODfQswXnPkDrwx+AG4oUsMlxUtnjAv9SBPiNLGwc tauduOr0oblpzMF8WB8mA63dt0uuJODb51Vxo7UR22Gl6MVGz8JCvQfweCi9zbGXwPGMxpcsNEA 764mRqjZeuJU5OyoUVP4du26our32HyOuMWmA6Qw1maEq7O X-Received: by 2002:a05:600d:8408:b0:486:ff92:63e5 with SMTP id 5b1f17b1804b1-488fb73dc6fmr221016215e9.6.1776795933722; Tue, 21 Apr 2026 11:25:33 -0700 (PDT) Received: from iku.Home ([2a06:5906:61b:2d00:d3da:32d9:3f08:c6f3]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4891941e05bsm66621565e9.36.2026.04.21.11.25.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Apr 2026 11:25:33 -0700 (PDT) From: Prabhakar X-Google-Original-From: Prabhakar To: Michael Turquette , Stephen Boyd , Brian Masney , Geert Uytterhoeven Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, Prabhakar , Biju Das , Fabrizio Castro , Lad Prabhakar Subject: [PATCH v3 1/2] clk: divider: Add KUnit tests for clk_divider_bestdiv() ULONG_MAX handling Date: Tue, 21 Apr 2026 19:25:25 +0100 Message-ID: <20260421182529.3055114-2-prabhakar.mahadev-lad.rj@bp.renesas.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260421182529.3055114-1-prabhakar.mahadev-lad.rj@bp.renesas.com> References: <20260421182529.3055114-1-prabhakar.mahadev-lad.rj@bp.renesas.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Lad Prabhakar Add KUnit tests to verify the behaviour of clk_divider_bestdiv() when clk_round_rate() is called with ULONG_MAX, which is the canonical way to probe the maximum rate a clock can produce. Two test cases are introduced: - clk_divider_bestdiv_ulong_max_returns_max_rate: registers a 1 GHz fixed-rate parent driving a table-based divider whose smallest entry is div=3D2 (entries: 2, 4, 8). Calls clk_hw_round_rate(div_hw, ULONG_MAX) and checks the result. - clk_divider_bestdiv_mux_ulong_max_returns_max_rate: places a two-input mux (4 GHz and 2 GHz fixed-rate parents, CLK_SET_RATE_PARENT) ahead of the same table-based divider to verify correct parent selection under ULONG_MAX. Both tests use an explicit clk_div_table with a minimum divider of 2 so that the pre-loop maxdiv clamping in clk_divider_bestdiv(): maxdiv =3D min(ULONG_MAX / rate, maxdiv); clamps maxdiv to 1, causing _next_div() to return 2 on the first iteration and skip the loop body entirely. This makes bestdiv fall back to the maximum divider, returning the minimum rate rather than the maximum. The expected values intentionally reflect the buggy output: - test 1: PARENT_RATE_1GHZ / 8 (minimum rate, not maximum) - test 2: 0 (invalid, loop never populated bestdiv) These will be corrected to PARENT_RATE_1GHZ / 2 and PARENT_RATE_4GHZ / 2 respectively once the fix to clk_divider_bestdiv() is applied. Signed-off-by: Lad Prabhakar --- v2->v3: - Added false positive expected values - Updated the commit message - Added dependency on !S390 in Kconfig --- drivers/clk/Kconfig | 8 ++ drivers/clk/Makefile | 1 + drivers/clk/clk-divider_test.c | 158 +++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 drivers/clk/clk-divider_test.c diff --git a/drivers/clk/Kconfig b/drivers/clk/Kconfig index b2efbe9f6acb..fdeb87ff8fd9 100644 --- a/drivers/clk/Kconfig +++ b/drivers/clk/Kconfig @@ -573,4 +573,12 @@ config CLK_FD_KUNIT_TEST help Kunit test for the clk-fractional-divider type. =20 +config CLK_DIVIDER_KUNIT_TEST + tristate "KUnit tests for clk divider bestdiv" if !KUNIT_ALL_TESTS + depends on KUNIT + depends on !S390 + default KUNIT_ALL_TESTS + help + Kunit test for the clk-divider type. + endif diff --git a/drivers/clk/Makefile b/drivers/clk/Makefile index a3e2862ebd7e..dc653b458f56 100644 --- a/drivers/clk/Makefile +++ b/drivers/clk/Makefile @@ -21,6 +21,7 @@ clk-test-y :=3D clk_test.o \ kunit_clk_hw_get_dev_of_node.dtbo.o \ kunit_clk_parent_data_test.dtbo.o obj-$(CONFIG_COMMON_CLK) +=3D clk-divider.o +obj-$(CONFIG_CLK_DIVIDER_KUNIT_TEST) +=3D clk-divider_test.o obj-$(CONFIG_COMMON_CLK) +=3D clk-fixed-factor.o obj-$(CONFIG_COMMON_CLK) +=3D clk-fixed-rate.o obj-$(CONFIG_CLK_FIXED_RATE_KUNIT_TEST) +=3D clk-fixed-rate-test.o diff --git a/drivers/clk/clk-divider_test.c b/drivers/clk/clk-divider_test.c new file mode 100644 index 000000000000..3087c77fb157 --- /dev/null +++ b/drivers/clk/clk-divider_test.c @@ -0,0 +1,158 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for clk_divider_bestdiv() + */ +#include +#include +#include +#include +#include + +#define PARENT_RATE_1GHZ GIGA +#define PARENT_RATE_2GHZ (2 * GIGA) +#define PARENT_RATE_4GHZ (4 * GIGA) + +static const struct clk_div_table bestdiv_table[] =3D { + { .val =3D 0, .div =3D 2 }, + { .val =3D 1, .div =3D 4 }, + { .val =3D 2, .div =3D 8 }, + { /* sentinel */ } +}; + +static void unregister_fixed_rate(void *hw) +{ + clk_hw_unregister_fixed_rate(hw); +} + +static void unregister_divider(void *hw) +{ + clk_hw_unregister_divider(hw); +} + +static void unregister_mux(void *hw) +{ + clk_hw_unregister_mux(hw); +} + +/* + * Test that clk_round_rate(clk, ULONG_MAX) returns the maximum achievable + * rate for a divider clock. + */ +static void clk_divider_bestdiv_ulong_max_returns_max_rate(struct kunit *t= est) +{ + struct clk_hw *parent_hw, *div_hw; + u32 *fake_reg; + unsigned long rate; + + fake_reg =3D kunit_kzalloc(test, sizeof(*fake_reg), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fake_reg); + + parent_hw =3D clk_hw_register_fixed_rate(NULL, "bestdiv-parent", + NULL, 0, PARENT_RATE_1GHZ); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw); + KUNIT_ASSERT_EQ(test, 0, + kunit_add_action_or_reset(test, unregister_fixed_rate, + parent_hw)); + + div_hw =3D clk_hw_register_divider_table(NULL, "bestdiv-div", + "bestdiv-parent", + CLK_SET_RATE_PARENT, + (void __iomem __force *)fake_reg, + 0, 2, 0, bestdiv_table, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, div_hw); + KUNIT_ASSERT_EQ(test, 0, + kunit_add_action_or_reset(test, unregister_divider, div_hw)); + + /* + * ULONG_MAX is the canonical way to probe the maximum rate a clock + * can produce. + */ + rate =3D clk_hw_round_rate(div_hw, ULONG_MAX); + KUNIT_EXPECT_EQ(test, rate, PARENT_RATE_1GHZ / 8); +} + +/* + * Test that clk_round_rate(clk, ULONG_MAX) returns the correct maximum ra= te + * when a mux clock sits between a divider and its parent candidates. + * + * Topology: + * + * [fixed 4 GHz] --\ + * +--> [mux CLK_SET_RATE_PARENT] --> [div {2,4,8} CLK_= SET_RATE_PARENT] + * [fixed 2 GHz] --/ + * + */ +static void clk_divider_bestdiv_mux_ulong_max_returns_max_rate(struct kuni= t *test) +{ + static const char * const mux_parents[] =3D { + "bestdiv-mux-parent-a", + "bestdiv-mux-parent-b", + }; + struct clk_hw *parent_a_hw, *parent_b_hw, *mux_hw, *div_hw; + u32 *fake_reg_mux, *fake_reg_div; + unsigned long rate; + + fake_reg_mux =3D kunit_kzalloc(test, sizeof(*fake_reg_mux), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fake_reg_mux); + + fake_reg_div =3D kunit_kzalloc(test, sizeof(*fake_reg_div), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fake_reg_div); + + /* Higher-rate parent: the mux should select this for ULONG_MAX. */ + parent_a_hw =3D clk_hw_register_fixed_rate(NULL, "bestdiv-mux-parent-a", + NULL, 0, PARENT_RATE_4GHZ); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_a_hw); + KUNIT_ASSERT_EQ(test, 0, + kunit_add_action_or_reset(test, unregister_fixed_rate, + parent_a_hw)); + + /* Lower-rate parent: should not be selected. */ + parent_b_hw =3D clk_hw_register_fixed_rate(NULL, "bestdiv-mux-parent-b", + NULL, 0, PARENT_RATE_2GHZ); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_b_hw); + KUNIT_ASSERT_EQ(test, 0, + kunit_add_action_or_reset(test, unregister_fixed_rate, + parent_b_hw)); + + /* + * 1-bit mux register selects between the two parents. + * CLK_SET_RATE_PARENT allows the divider's rate request to + * propagate into clk_mux_determine_rate(). + */ + mux_hw =3D clk_hw_register_mux(NULL, "bestdiv-mux", + mux_parents, ARRAY_SIZE(mux_parents), + CLK_SET_RATE_PARENT, + (void __iomem __force *)fake_reg_mux, + 0, 1, 0, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mux_hw); + KUNIT_ASSERT_EQ(test, 0, + kunit_add_action_or_reset(test, unregister_mux, mux_hw)); + + div_hw =3D clk_hw_register_divider_table(NULL, "bestdiv-mux-div", + "bestdiv-mux", + CLK_SET_RATE_PARENT, + (void __iomem __force *)fake_reg_div, + 0, 2, 0, bestdiv_table, NULL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, div_hw); + KUNIT_ASSERT_EQ(test, 0, + kunit_add_action_or_reset(test, unregister_divider, div_hw)); + + rate =3D clk_hw_round_rate(div_hw, ULONG_MAX); + KUNIT_EXPECT_EQ(test, rate, 0); +} + +static struct kunit_case clk_divider_bestdiv_test_cases[] =3D { + KUNIT_CASE(clk_divider_bestdiv_ulong_max_returns_max_rate), + KUNIT_CASE(clk_divider_bestdiv_mux_ulong_max_returns_max_rate), + {} +}; + +static struct kunit_suite clk_divider_bestdiv_test_suite =3D { + .name =3D "clk_divider_bestdiv", + .test_cases =3D clk_divider_bestdiv_test_cases, +}; + +kunit_test_suite(clk_divider_bestdiv_test_suite); + +MODULE_DESCRIPTION("KUnit tests for clk_divider_bestdiv()"); +MODULE_LICENSE("GPL"); --=20 2.53.0 From nobody Wed Jun 17 03:00:20 2026 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 CC23E372ED7 for ; Tue, 21 Apr 2026 18:25:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776795943; cv=none; b=UczW3xvCSNVFSA12zRLsT0REbIMkR+GDrOy6KkL0vykgiOs2NNUIOxCu6+3a49PDPx6OKkqCUOAhEEF/rx+UVL/S144kuHEorP8EWhN3wqbCH1l/RS79Xm9YIn2F4luxLbdot2d+H5Lh1xpab2lAvNNiWGbGCF1BvLVBf+JxGb0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776795943; c=relaxed/simple; bh=PSar3N9Z2BHMP8vv/EuV+UcLe488tH3TScHmwENCS9s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LzWhWaXZV3CgALtaxiMsceug0AEkEvxat0R4OCK14cbnk3zJu2Etp/ST9GB9zo0KZ0q1SMCVsZxs2fiyDV5/EwgUv1At18bAmN1WWjFHx+G8iexBo7MqB1QoczG5F/ilONCLy+YAvFH5gjPbiQwDq39cMbohqLxtBFtafzLthFc= ARC-Authentication-Results: i=1; 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=DYTdS9K0; arc=none smtp.client-ip=209.85.128.44 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="DYTdS9K0" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-4890098abbaso31597315e9.0 for ; Tue, 21 Apr 2026 11:25:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776795935; x=1777400735; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=M1R6milmB9n/Q/nqo4lAWxKj72sYlq6LR8BMjq2RJUI=; b=DYTdS9K0X4qqwAz15Uh5roAkXlBV33kjVH66rS/pGSMzf8rBRZL5mefOhLiso8DRfY utBaNRNGaVYpSmyAJs8Up+a3hgsiSMt8GSzkyMJNIsgpj2dfljwijlnsfHt+rF64HDNP YmF1ffrAed3tFf+yJVAxb0Xjatp+95gvWgUeeRiAHx1xPICmgCNHNJBuLLrFXY8Ru6Dq YBrwtMaL8iaftEskbKQwmWGAtbtsEEqNzzuZBVMilTWa2G6ellXCZ1tEZi6uS+gbyns4 gc6ME9bG2/ua+TnDLtoz3gy2MqzDVZogVU6tBj85TmJ3sPkqRXXZi75y50xORv3pSCku luFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776795935; x=1777400735; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=M1R6milmB9n/Q/nqo4lAWxKj72sYlq6LR8BMjq2RJUI=; b=ayLsrls32wLRtYYhmkFS726QV/scucez20WdN89oTbYmwrunw+tsQtXu1Xar5fdjr6 KDSGMEIOvOR2YrjbH7T4YSJet9Zk5N/AbxzDAc4dST0aqt3Fp2n5GAAQCsHhmuK0KtQc /zcHJAHZeBYvHfClhPwcAtVrxclzVG2SVkS9DnzORvsDMbiRMypLW4pDh9HJLpOyeuUI BmL+TSPtIhyHpIQS2eG3KF50JDHFG/dmR01pbFICGRbaMZcqTRLY41DpJfEDZoEJlGtB Iuu/aaon0FYK6Gfijm6YckTnwdzxviiPObc8hmtVsmK2JHr8v0SNNyC7vAGY4ibFvtxk NHCg== X-Gm-Message-State: AOJu0YyMWf3/nPrF4Q9hzXFNcszszHPgJr+luFLSaGC/cSJ149ic4U6S eUMpxPFsbYhgYitK7dswi9H7W3ROfuF3d3sbWrNyJGY0vbe4BwkiY8kI X-Gm-Gg: AeBDieuLnWzVAdg4BI1ik0zPIcnNAcH2UmjCSy7L9gdbHYzSGrMCCv4vfpEbovjX4it 8miUg4iVOGEgRdprO4YTRNszULtWwG3XbY5DqBnlfHB8BEBEyyDZTA5Fwd3/9qVpNKNUgVgUIHC 4340yBJzAYDtLBY2MD9+qhKlEm597M1uPINGfVupuCF0TUSdNxFipjvi9Cqj4BBJzO7LMdrXu5K zV32P/nMh1s1bgklGs5C63thX2qrudOMHt8HSNPvEoWSRukGEPTkUQ+ZxgJruPGod00MAyYCyI3 H60BfQgq0RFxJJGHD46im2yEN6b1Rr2tK6z4eBP3Vn4HOqbNm2IAgCdAQ8R+7U3ecDXvelp6ypU dFt4FsXCxEZh5/SEJ3CbEOM+dDeysj32Os6vOgjVoHnoCWm24YQyDEQKmtJcrAt9jNaxdCwE5yA zGIxz2brOY0R0200G85dZzDkwAqz8nzGbm84u0JhFFDqCIhtSYegdIJQ1Q+la6V6iBkwspful/2 LQalSgntW91KK24XQuTLYM7ejg78qSvvO2jsAIsiCGBpkkK X-Received: by 2002:a05:600c:4fd1:b0:488:78f2:6b0 with SMTP id 5b1f17b1804b1-488fb78ede0mr278306405e9.29.1776795934482; Tue, 21 Apr 2026 11:25:34 -0700 (PDT) Received: from iku.Home ([2a06:5906:61b:2d00:d3da:32d9:3f08:c6f3]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4891941e05bsm66621565e9.36.2026.04.21.11.25.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Apr 2026 11:25:34 -0700 (PDT) From: Prabhakar X-Google-Original-From: Prabhakar To: Michael Turquette , Stephen Boyd , Brian Masney , Geert Uytterhoeven Cc: linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, Prabhakar , Biju Das , Fabrizio Castro , Lad Prabhakar Subject: [PATCH v3 2/2] clk: divider: Fix clk_divider_bestdiv() returning min rate for large rate requests Date: Tue, 21 Apr 2026 19:25:26 +0100 Message-ID: <20260421182529.3055114-3-prabhakar.mahadev-lad.rj@bp.renesas.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260421182529.3055114-1-prabhakar.mahadev-lad.rj@bp.renesas.com> References: <20260421182529.3055114-1-prabhakar.mahadev-lad.rj@bp.renesas.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Lad Prabhakar clk_divider_bestdiv() clamps maxdiv using: maxdiv =3D min(ULONG_MAX / rate, maxdiv); to avoid overflow in rate * i. However, requests like clk_round_rate(clk, ULONG_MAX), which are used to determine the maximum supported rate of a clock, result in maxdiv being clamped to 1. If no valid divider of 1 exists in the table the loop is never entered and bestdiv falls back to the maximum divider with the minimum parent rate, causing clk_round_rate(clk, ULONG_MAX) to incorrectly return the minimum supported rate instead of the maximum. Fix this by removing the pre-loop maxdiv clamping and replacing the unprotected rate * i multiplication with check_mul_overflow(). Guard the exact-match short-circuit with !overflow to prevent a clamped target_parent_rate of ULONG_MAX from falsely matching parent_rate_saved and causing premature loop exit. Break out of the loop after evaluating the first overflowing divider since clk_hw_round_rate(parent, ULONG_MAX) returns a constant for all subsequent iterations, meaning no better candidate can be found, and continuing would cause exponential recursive calls in chained divider clocks. Update the KUnit test expected values to reflect the corrected behaviour: - clk_divider_bestdiv_ulong_max_returns_max_rate: PARENT_RATE_1GHZ / 8 (minimum rate, pre-fix) -> PARENT_RATE_1GHZ / 2 (maximum rate) - clk_divider_bestdiv_mux_ulong_max_returns_max_rate: 0 (invalid, pre-fix) -> PARENT_RATE_4GHZ / 2 (maximum rate with mux selecting the 4 GHz parent and applying the smallest table divider of 2) Signed-off-by: Lad Prabhakar Reviewed-by: Brian Masney --- v2->v3: - Added Rb tag - Added the expected value for the tests - updated the commit message --- drivers/clk/clk-divider.c | 25 +++++++++++++++++-------- drivers/clk/clk-divider_test.c | 4 ++-- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c index c3804bbc06f9..36bfaf7200e2 100644 --- a/drivers/clk/clk-divider.c +++ b/drivers/clk/clk-divider.c @@ -15,6 +15,7 @@ #include #include #include +#include =20 /* * DOC: basic adjustable divider clock that cannot gate @@ -301,6 +302,7 @@ static int clk_divider_bestdiv(struct clk_hw *hw, struc= t clk_hw *parent, int i, bestdiv =3D 0; unsigned long parent_rate, best =3D 0, now, maxdiv; unsigned long parent_rate_saved =3D *best_parent_rate; + unsigned long target_parent_rate; =20 if (!rate) rate =3D 1; @@ -315,15 +317,11 @@ static int clk_divider_bestdiv(struct clk_hw *hw, str= uct clk_hw *parent, return bestdiv; } =20 - /* - * The maximum divider we can use without overflowing - * unsigned long in rate * i below - */ - maxdiv =3D min(ULONG_MAX / rate, maxdiv); - for (i =3D _next_div(table, 0, flags); i <=3D maxdiv; i =3D _next_div(table, i, flags)) { - if (rate * i =3D=3D parent_rate_saved) { + bool overflow =3D check_mul_overflow(rate, (unsigned long)i, &target_par= ent_rate); + + if (!overflow && target_parent_rate =3D=3D parent_rate_saved) { /* * It's the most ideal case if the requested rate can be * divided from parent clock without needing to change @@ -332,13 +330,24 @@ static int clk_divider_bestdiv(struct clk_hw *hw, str= uct clk_hw *parent, *best_parent_rate =3D parent_rate_saved; return i; } - parent_rate =3D clk_hw_round_rate(parent, rate * i); + /* + * Clamp target_parent_rate to ULONG_MAX on overflow. The true + * required parent rate exceeds what can be represented, so ask + * the parent for the highest rate it can produce. There is no + * point continuing the loop past this since larger dividers + * only move further from the requested rate. + */ + if (overflow) + target_parent_rate =3D ULONG_MAX; + parent_rate =3D clk_hw_round_rate(parent, target_parent_rate); now =3D DIV_ROUND_UP_ULL((u64)parent_rate, i); if (_is_best_div(rate, now, best, flags)) { bestdiv =3D i; best =3D now; *best_parent_rate =3D parent_rate; } + if (overflow) + break; } =20 if (!bestdiv) { diff --git a/drivers/clk/clk-divider_test.c b/drivers/clk/clk-divider_test.c index 3087c77fb157..300b8ba04279 100644 --- a/drivers/clk/clk-divider_test.c +++ b/drivers/clk/clk-divider_test.c @@ -68,7 +68,7 @@ static void clk_divider_bestdiv_ulong_max_returns_max_rat= e(struct kunit *test) * can produce. */ rate =3D clk_hw_round_rate(div_hw, ULONG_MAX); - KUNIT_EXPECT_EQ(test, rate, PARENT_RATE_1GHZ / 8); + KUNIT_EXPECT_EQ(test, rate, PARENT_RATE_1GHZ / 2); } =20 /* @@ -138,7 +138,7 @@ static void clk_divider_bestdiv_mux_ulong_max_returns_m= ax_rate(struct kunit *tes kunit_add_action_or_reset(test, unregister_divider, div_hw)); =20 rate =3D clk_hw_round_rate(div_hw, ULONG_MAX); - KUNIT_EXPECT_EQ(test, rate, 0); + KUNIT_EXPECT_EQ(test, rate, PARENT_RATE_4GHZ / 2); } =20 static struct kunit_case clk_divider_bestdiv_test_cases[] =3D { --=20 2.53.0