Re: [PATCH v4 1/2] clk: divider: Add KUnit tests for clk_divider_bestdiv() ULONG_MAX handling

From: Brian Masney

Date: Mon Apr 27 2026 - 11:34:04 EST


Hi Lad,

On Mon, Apr 27, 2026 at 12:28:23PM +0100, Prabhakar wrote:
> From: Lad Prabhakar <prabhakar.mahadev-lad.rj@xxxxxxxxxxxxxx>
>
> 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=2 (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 = 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 <prabhakar.mahadev-lad.rj@xxxxxxxxxxxxxx>

[snip]

> + KUNIT_ASSERT_EQ(test, 0,
> + kunit_add_action_or_reset(test,
> + (kunit_action_t *)clk_hw_unregister_fixed_rate,
> + parent_hw));

You can use KUNIT_DEFINE_ACTION_WRAPPER() to avoid that cast. There's
some examples in drivers/clk/clk_kunit_helpers.c.

With that fixed:

Reviewed-by: Brian Masney <bmasney@xxxxxxxxxx>