All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Shubhrajyoti Datta" <shubhrajyoti.datta@amd.com>,
	linux-clk@vger.kernel.org
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v4 7/8] clk: clocking-wizard: Fix division by zero and unbounded register write
Date: Tue, 24 Nov 2026 04:18:05 +0000	[thread overview]
Message-ID: <sashiko-outbox-230500@kernel.org> (raw)
In-Reply-To: <20261124035613.1968260-8-shubhrajyoti.datta@amd.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] clk: clocking-wizard: u8 truncation of div in
  clk_wzrd_determine_rate()
- [Medium] clk: clocking-wizard: missing zero rate checks in
  multi-output rate callbacks
- [Low] clk: clocking-wizard: missing Fixes trailers for bug fixes
--

commit 7829b1929b443e35dccbe8d3e7ecd82961ac4d4c
Author: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>

> clk: clocking-wizard: Fix division by zero and unbounded register write
>
> 1. clk_wzrd_determine_rate(): DIV_ROUND_CLOSEST() can return 0 when
>    the requested rate greatly exceeds the parent rate, causing a
>    division by zero on the subsequent parent_rate / div. Clamp div
>    to a minimum of 1.
>
> 2. clk_wzrd_dynamic_reconfig(): min_t() result was not assigned back
>    to value, so the cap to WZRD_DR_MAX_INT_DIV_VALUE was never applied
>    before the register write.
>
> 3. clk_wzrd_get_divisors() and clk_wzrd_get_divisors_ver(): If rate is
>    0, DIV_ROUND_CLOSEST_ULL divides by zero. Return -EINVAL early.
>
> Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>

[Severity: Low]
Should this include Fixes: tags for the commits that introduced these bugs?

  Fixes: 5a853722eb32 ("staging: clocking-wizard: Add support for dynamic reconfiguration")
  Fixes: 7681f64e6404 ("clk: clocking-wizard: calculate dividers fractional parts")
  Fixes: e0a94c6bb5b4 ("clk: xilinx: Optimize divisor search in clk_wzrd_get_divisors_ver()")

> diff --git a/drivers/clk/xilinx/clk-xlnx-clock-wizard.c b/drivers/clk/xilinx/clk-xlnx-clock-wizard.c
> index 7fe8d0d4cfba2..046efa5c77c98 100644
> --- a/drivers/clk/xilinx/clk-xlnx-clock-wizard.c
> +++ b/drivers/clk/xilinx/clk-xlnx-clock-wizard.c
> @@ -303,7 +303,7 @@ static int clk_wzrd_dynamic_reconfig(struct clk_hw *hw, unsigned long rate,
>  	value = DIV_ROUND_CLOSEST(parent_rate, rate);
>  
>  	/* Cap the value to max */
> -	min_t(u32, value, WZRD_DR_MAX_INT_DIV_VALUE);
> +	value = min_t(u32, value, WZRD_DR_MAX_INT_DIV_VALUE);
>  
>  	/* Set divisor and clear phase offset */
>  	writel(value, div_addr);
> @@ -341,6 +341,8 @@ static int clk_wzrd_determine_rate(struct clk_hw *hw,
>  	 * achievable
>  	 */
>  	div = DIV_ROUND_CLOSEST(req->best_parent_rate, req->rate);
> +	if (!div)
> +		div = 1;
>  
>  	req->rate = req->best_parent_rate / div;

[Severity: Medium]
Can storing DIV_ROUND_CLOSEST() into u8 div in clk_wzrd_determine_rate()
truncate dividers above WZRD_DR_MAX_INT_DIV_VALUE (255) instead of clamping
them?

In clk_wzrd_determine_rate(), div is declared as u8.  When a low rate is
requested such that DIV_ROUND_CLOSEST(req->best_parent_rate, req->rate) is
256 or greater, the quotient wraps modulo 256.  If the quotient is a
multiple of 256 (for example, 256 MHz parent and 1 MHz requested rate),
div wraps to 0 and the new "if (!div) div = 1;" check forces req->rate to
req->best_parent_rate instead of the minimum frequency.  Because
clk_set_rate() passes the rounded req->rate from clk_wzrd_determine_rate()
into clk_wzrd_dynamic_reconfig(), the recomputed value in
clk_wzrd_dynamic_reconfig() is already wrapped to 1..255, bypassing the
min_t(u32, value, WZRD_DR_MAX_INT_DIV_VALUE) cap above.  Widen div to u32
(or unsigned long) and clamp it to [1, WZRD_DR_MAX_INT_DIV_VALUE] in
clk_wzrd_determine_rate().

> @@ -355,6 +357,9 @@ static int clk_wzrd_get_divisors_ver(struct clk_hw *hw, unsigned long rate,
>  	u32 m, d, o;
>  	u32 mmin, mmax, dmin, dmax, omin, omax;
>  
> +	if (!rate)
> +		return -EINVAL;
> +
>  	mmin = VER_WZRD_M_MIN;
>  	mmax = VER_WZRD_M_MAX;
>  	dmin = VER_WZRD_D_MIN;
> @@ -397,6 +402,9 @@ static int clk_wzrd_get_divisors(struct clk_hw *hw, unsigned long rate,
>  	u64 m, d, o;
>  	u64 mmin, mmax, dmin, dmax, omin, omax, mdmin, mdmax;
>  
> +	if (!rate)
> +		return -EINVAL;
> +
>  	mmin = WZRD_M_MIN << 3;
>  	mmax = WZRD_M_MAX << 3;
>  	dmin = WZRD_D_MIN;

[Severity: Medium]
Can a rate of 0 still trigger a division by zero in the multi-output rate
callbacks?

clk_wzrd_get_divisors() and clk_wzrd_get_divisors_ver() are only used when
nr_outputs == 1.  For multi-output instances, the driver does not set a
minimum rate via clk_hw_set_rate_range(), so req->rate == 0 can reach
clk_wzrd_determine_rate() and divide by zero in
DIV_ROUND_CLOSEST(req->best_parent_rate, req->rate) before the new !div
check.  Likewise, clk_wzrd_determine_rate_f() returns 0 without rejecting
req->rate == 0, allowing clk_wzrd_dynamic_reconfig_f() to divide by rate ==
0 in DIV_ROUND_DOWN_ULL() and DIV_ROUND_CLOSEST_ULL(), and
clk_wzrd_dynamic_reconfig() and clk_wzrd_ver_dynamic_reconfig() also divide
by rate without checking for 0.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124035613.1968260-1-shubhrajyoti.datta@amd.com?part=7

  reply	other threads:[~2026-11-24  4:18 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  3:55 [PATCH v4 0/8] clk: clocking-wizard: Add static-config clock provider support Shubhrajyoti Datta
2026-11-24  3:55 ` [PATCH v4 1/8] dt-bindings: clock: clocking-wizard: Add static-config mode support Shubhrajyoti Datta
2026-11-24  3:55 ` [PATCH v4 2/8] clk: clocking-wizard: Do not map the memory for static-config Shubhrajyoti Datta
2026-11-24  3:55 ` [PATCH v4 3/8] clk: clocking-wizard: Move clk_in1 acquisition before static-config check Shubhrajyoti Datta
2026-11-24  3:55 ` [PATCH v4 4/8] clk: clocking-wizard: Add static-config clock provider support Shubhrajyoti Datta
2026-11-24  4:14   ` sashiko-bot
2026-11-25 15:36   ` Brian Masney
2026-11-24  3:55 ` [PATCH v4 5/8] clk: clocking-wizard: Skip s_axi_aclk for static-config Shubhrajyoti Datta
2026-11-24  3:55 ` [PATCH v4 6/8] clk: clocking-wizard: Use dev_err_probe() when mapping registers Shubhrajyoti Datta
2026-11-24  4:03   ` sashiko-bot
2026-11-25 15:36   ` Brian Masney
2026-11-24  3:55 ` [PATCH v4 7/8] clk: clocking-wizard: Fix division by zero and unbounded register write Shubhrajyoti Datta
2026-11-24  4:18   ` sashiko-bot [this message]
2026-11-25 15:36   ` Brian Masney
2026-11-24  3:55 ` [PATCH v4 8/8] clk: clocking-wizard: Use separate notifier_block for each clock Shubhrajyoti Datta
2026-11-24  4:06   ` sashiko-bot
2026-11-25 15:36   ` Brian Masney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-230500@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shubhrajyoti.datta@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.