drivers/gpu/drm/i915/i915_perf.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
As the comment explains, the if check ensures that the divisor oa_period
is a u32. Explicitly cast oa_period to u32 to remove the following
Coccinelle/coccicheck warning reported by do_div.cocci:
WARNING: do_div() does a 64-by-32 division, please consider using div64_u64 instead
Use the preferred div_u64() function instead of the do_div() macro and
remove the now unnecessary local variable tmp.
Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
---
Changes in v2:
- Use div_u64() instead of do_div() after feedback from Ville Syrjälä
- Link to v1: https://lore.kernel.org/linux-kernel/20240710074650.419902-2-thorsten.blum@toblux.com/
---
drivers/gpu/drm/i915/i915_perf.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_perf.c
index 0b1cd4c7a525..f65fbe13ab59 100644
--- a/drivers/gpu/drm/i915/i915_perf.c
+++ b/drivers/gpu/drm/i915/i915_perf.c
@@ -4096,15 +4096,13 @@ static int read_properties_unlocked(struct i915_perf *perf,
oa_period = oa_exponent_to_ns(perf, value);
/* This check is primarily to ensure that oa_period <=
- * UINT32_MAX (before passing to do_div which only
+ * UINT32_MAX (before passing it to div_u64 which only
* accepts a u32 denominator), but we can also skip
* checking anything < 1Hz which implicitly can't be
* limited via an integer oa_max_sample_rate.
*/
if (oa_period <= NSEC_PER_SEC) {
- u64 tmp = NSEC_PER_SEC;
- do_div(tmp, oa_period);
- oa_freq_hz = tmp;
+ oa_freq_hz = div_u64(NSEC_PER_SEC, (u32)oa_period);
} else
oa_freq_hz = 0;
--
2.45.2
-----Original Message-----
From: Intel-gfx <intel-gfx-bounces@lists.freedesktop.org> On Behalf Of Thorsten Blum
Sent: Friday, August 2, 2024 9:03 AM
To: jani.nikula@linux.intel.com; joonas.lahtinen@linux.intel.com; Vivi, Rodrigo <rodrigo.vivi@intel.com>; tursulin@ursulin.net; airlied@gmail.com; daniel@ffwll.ch
Cc: intel-gfx@lists.freedesktop.org; dri-devel@lists.freedesktop.org; linux-kernel@vger.kernel.org; Thorsten Blum <thorsten.blum@toblux.com>
Subject: [PATCH v2] drm/i915: Explicitly cast divisor and use div_u64()
>
> As the comment explains, the if check ensures that the divisor oa_period
> is a u32. Explicitly cast oa_period to u32 to remove the following
> Coccinelle/coccicheck warning reported by do_div.cocci:
>
> WARNING: do_div() does a 64-by-32 division, please consider using div64_u64 instead
>
> Use the preferred div_u64() function instead of the do_div() macro and
> remove the now unnecessary local variable tmp.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
> ---
> Changes in v2:
> - Use div_u64() instead of do_div() after feedback from Ville Syrjälä
> - Link to v1: https://lore.kernel.org/linux-kernel/20240710074650.419902-2-thorsten.blum@toblux.com/
> ---
> drivers/gpu/drm/i915/i915_perf.c | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/gpu/drm/i915/i915_perf.c b/drivers/gpu/drm/i915/i915_perf.c
> index 0b1cd4c7a525..f65fbe13ab59 100644
> --- a/drivers/gpu/drm/i915/i915_perf.c
> +++ b/drivers/gpu/drm/i915/i915_perf.c
> @@ -4096,15 +4096,13 @@ static int read_properties_unlocked(struct i915_perf *perf,
> oa_period = oa_exponent_to_ns(perf, value);
>
> /* This check is primarily to ensure that oa_period <=
> - * UINT32_MAX (before passing to do_div which only
> + * UINT32_MAX (before passing it to div_u64 which only
> * accepts a u32 denominator), but we can also skip
> * checking anything < 1Hz which implicitly can't be
> * limited via an integer oa_max_sample_rate.
> */
> if (oa_period <= NSEC_PER_SEC) {
> - u64 tmp = NSEC_PER_SEC;
> - do_div(tmp, oa_period);
> - oa_freq_hz = tmp;
> + oa_freq_hz = div_u64(NSEC_PER_SEC, (u32)oa_period);
> } else
> oa_freq_hz = 0;
Non-blocking suggestion: this looks like it can be inlined. And if the
inline route is taken, it might be best to invert the conditional check
like such:
oa_freq_hz = oa_period > NSEC_PER_SEC ? 0 :
div_u64(NSEC_PER_SEC, (u32)oa_period);
I think this is just a matter of preference, though. The explicit if-else
block is definitely clearer.
Reviewed-by: Jonathan Cavitt <jonathan.cavitt@intel.com>
-Jonathan Cavitt
>
> --
> 2.45.2
>
>
Hi Thorsten,
> > /* This check is primarily to ensure that oa_period <=
> > - * UINT32_MAX (before passing to do_div which only
> > + * UINT32_MAX (before passing it to div_u64 which only
> > * accepts a u32 denominator), but we can also skip
> > * checking anything < 1Hz which implicitly can't be
> > * limited via an integer oa_max_sample_rate.
> > */
> > if (oa_period <= NSEC_PER_SEC) {
> > - u64 tmp = NSEC_PER_SEC;
> > - do_div(tmp, oa_period);
> > - oa_freq_hz = tmp;
> > + oa_freq_hz = div_u64(NSEC_PER_SEC, (u32)oa_period);
> > } else
> > oa_freq_hz = 0;
>
> Non-blocking suggestion: this looks like it can be inlined. And if the
> inline route is taken, it might be best to invert the conditional check
> like such:
>
> oa_freq_hz = oa_period > NSEC_PER_SEC ? 0 :
> div_u64(NSEC_PER_SEC, (u32)oa_period);
>
> I think this is just a matter of preference, though. The explicit if-else
> block is definitely clearer.
It's also stylistically wrong given that now the if/else don't
need the brackets anymore, triggering a checkpatch error.
Thorsten do you mind resending it either following Jonathan's
suggestion (my favourite, as well) or fix the bracket issue
following the kernel style.
Andi
© 2016 - 2025 Red Hat, Inc.