Linux clock framework development
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Romain TISSERAND <romain.tisserand@gmail.com>
Cc: Chen-Yu Tsai <wens@kernel.org>,
	Jernej Skrabec <jernej.skrabec@gmail.com>,
	Samuel Holland <samuel@sholland.org>,
	Stephen Boyd <sboyd@kernel.org>,
	Brian Masney <bmasney+clk@redhat.com>,
	Jerome Brunet <jbrunet+clk@baylibre.com>,
	Maxime Ripard <mripard@kernel.org>,
	Frank Oltmanns <frank@oltmanns.dev>,
	linux-clk@vger.kernel.org, linux-sunxi@lists.linux.dev,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, sashiko-reviews@lists.linux.dev
Subject: Re: [PATCH] clk: sunxi-ng: nkm: use div64_ul() for the rate divisions
Date: Wed, 2 Dec 2026 22:40:25 +0000	[thread overview]
Message-ID: <20261202224025.07df2dd1@pumpkin> (raw)
In-Reply-To: <20261118162237.1664352-1-romain.tisserand@gmail.com>

On Wed, 18 Nov 2026 17:22:37 +0100
Romain TISSERAND <romain.tisserand@gmail.com> wrote:

> ccu_nkm_calc_rate() and ccu_nkm_calc_parent_rate() divide their 64-bit
> products by an unsigned long (m, n * k) with do_div(), whose divisor is
> 32 bits: on 64-bit it would be truncated. The N, K and M factors keep
> it far below 2^32, so the results do not change, but use div64_ul(),
> which takes an unsigned long divisor, as coccinelle suggests. On 32-bit
> it is the same do_div() as before.

Exactly when does the truncation happen?
That is what actual values cause a truncation?
And if there is one, why does it only happen on 64bit.

David

> 
> Fixes: e9d9dae0d248 ("clk: sunxi-ng: nkm: avoid 32-bit overflow in rate calculations")
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202611151822.QXTOs54d-lkp@intel.com/
> Signed-off-by: Romain TISSERAND <romain.tisserand@gmail.com>
> ---
> Notes:
> - Follow-up to e9d9dae0d248 in sunxi/clk-for-7.5, for the coccinelle
>   report of the kernel test robot (do_div() with an unsigned long
>   divisor); squash it into that commit if you prefer.
> - No functional change: m and n * k stay far below 2^32 for every NKM
>   clock, so do_div() never saw a truncated divisor.
> - Testing: built with W=1 for arm (multi_v7_defconfig with the A31 and
>   A33 CCUs) and arm64 (defconfig), no warnings; on arm the divisions
>   still go through __do_div64 (no __aeabi_uldivmod). Not run on
>   hardware: the generated code for the rates the hardware uses is the
>   same division as before.
> - Tool disclosure: prepared with Claude Opus 5.5 (Anthropic) as a coding
>   assistant, from the robot report; I reviewed and understand the change.
> 
>  drivers/clk/sunxi-ng/ccu_nkm.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/clk/sunxi-ng/ccu_nkm.c b/drivers/clk/sunxi-ng/ccu_nkm.c
> index e5a8f22d50f5..a11d51dd0f2a 100644
> --- a/drivers/clk/sunxi-ng/ccu_nkm.c
> +++ b/drivers/clk/sunxi-ng/ccu_nkm.c
> @@ -35,9 +35,8 @@ static unsigned long ccu_nkm_calc_rate(unsigned long parent,
>  				       unsigned long n, unsigned long k,
>  				       unsigned long m)
>  {
> -	u64 rate = (u64)parent * n * k;
> +	u64 rate = div64_ul((u64)parent * n * k, m);
>  
> -	do_div(rate, m);
>  	return min_t(u64, rate, ULONG_MAX);
>  }
>  
> @@ -45,9 +44,8 @@ static unsigned long ccu_nkm_calc_parent_rate(unsigned long rate,
>  					      unsigned long n, unsigned long k,
>  					      unsigned long m)
>  {
> -	u64 parent = (u64)rate * m;
> +	u64 parent = div64_ul((u64)rate * m, n * k);
>  
> -	do_div(parent, n * k);
>  	return min_t(u64, parent, ULONG_MAX);
>  }
>  
> 
> base-commit: 3f8438336a3b340ffbda9e89480630436423782d


      parent reply	other threads:[~2026-12-02 22:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 16:22 [PATCH] clk: sunxi-ng: nkm: use div64_ul() for the rate divisions Romain TISSERAND
2026-12-02 21:40 ` Romain TISSERAND
2026-12-02 22:40 ` David Laight [this message]

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=20261202224025.07df2dd1@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=bmasney+clk@redhat.com \
    --cc=frank@oltmanns.dev \
    --cc=jbrunet+clk@baylibre.com \
    --cc=jernej.skrabec@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sunxi@lists.linux.dev \
    --cc=mripard@kernel.org \
    --cc=romain.tisserand@gmail.com \
    --cc=samuel@sholland.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sboyd@kernel.org \
    --cc=wens@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox