Re: [PATCH v2 1/2] clk: divider: Fix clk_divider_bestdiv() returning min rate for large rate requests

Brian Masney
2026-04-16T20:21:12Z

On Mon, Apr 13, 2026 at 01:49:11PM +0100, Prabhakar wrote:
> From: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
>
> clk_divider_bestdiv() clamps maxdiv using:
>
> maxdiv = 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.
>
> Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>

Reviewed-by: Brian Masney <bmasney@redhat.com>