diff mbox series

net: dsa: ocelot: use div64_u64() instead of do_div()

Message ID 1644395942-4186-1-git-send-email-wangqing@vivo.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series net: dsa: ocelot: use div64_u64() instead of do_div() | expand

Checks

Context Check Description
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 10 of 10 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 8 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

王擎 Feb. 9, 2022, 8:39 a.m. UTC
From: Wang Qing <wangqing@vivo.com>

do_div() does a 64-by-32 division.
When the divisor is u64, do_div() truncates it to 32 bits, this means it
can test non-zero and be truncated to zero for division.

fix do_div.cocci warning:
do_div() does a 64-by-32 division, please consider using div64_u64 instead.

Signed-off-by: Wang Qing <wangqing@vivo.com>
---
 drivers/net/dsa/ocelot/felix_vsc9959.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Vladimir Oltean Feb. 9, 2022, 11:14 a.m. UTC | #1
Hi Wang,

On Wed, Feb 09, 2022 at 12:39:02AM -0800, Qing Wang wrote:
> From: Wang Qing <wangqing@vivo.com>
> 
> do_div() does a 64-by-32 division.
> When the divisor is u64, do_div() truncates it to 32 bits, this means it
> can test non-zero and be truncated to zero for division.
> 
> fix do_div.cocci warning:
> do_div() does a 64-by-32 division, please consider using div64_u64 instead.
> 
> Signed-off-by: Wang Qing <wangqing@vivo.com>
> ---
>  drivers/net/dsa/ocelot/felix_vsc9959.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/dsa/ocelot/felix_vsc9959.c b/drivers/net/dsa/ocelot/felix_vsc9959.c
> index bf8d382..5c2482f
> --- a/drivers/net/dsa/ocelot/felix_vsc9959.c
> +++ b/drivers/net/dsa/ocelot/felix_vsc9959.c
> @@ -1178,7 +1178,7 @@ static void vsc9959_new_base_time(struct ocelot *ocelot, ktime_t base_time,
>  	if (base_time < current_time) {
>  		u64 nr_of_cycles = current_time - base_time;
>  
> -		do_div(nr_of_cycles, cycle_time);
> +		div64_u64(nr_of_cycles, cycle_time);
>  		new_base_time += cycle_time * (nr_of_cycles + 1);
>  	}
>  
> -- 
> 2.7.4
>

I would prefer that you teach your scripts that, if a range check exists
for the divisor prior to the division, it gets taken into consideration.

vsc9959_qos_port_tas_set:

	if (taprio->cycle_time > NSEC_PER_SEC ||
	    taprio->cycle_time_extension >= NSEC_PER_SEC)
		return -EINVAL;

	vsc9959_new_base_time(ocelot, taprio->base_time,
			      taprio->cycle_time, &base_ts);

vsc9959_psfp_sgi_set:

	if (sgi->cycletime < VSC9959_PSFP_GATE_CYCLETIME_MIN ||
	    sgi->cycletime > NSEC_PER_SEC)
		return -EINVAL;

	vsc9959_new_base_time(ocelot, sgi->basetime, sgi->cycletime, &base_ts);

So all callers provide a cycle_time argument that is smaller than
NSEC_PER_SEC (1000000000L = 0x3B9ACA00 => fits on 32 bits).
diff mbox series

Patch

diff --git a/drivers/net/dsa/ocelot/felix_vsc9959.c b/drivers/net/dsa/ocelot/felix_vsc9959.c
index bf8d382..5c2482f
--- a/drivers/net/dsa/ocelot/felix_vsc9959.c
+++ b/drivers/net/dsa/ocelot/felix_vsc9959.c
@@ -1178,7 +1178,7 @@  static void vsc9959_new_base_time(struct ocelot *ocelot, ktime_t base_time,
 	if (base_time < current_time) {
 		u64 nr_of_cycles = current_time - base_time;
 
-		do_div(nr_of_cycles, cycle_time);
+		div64_u64(nr_of_cycles, cycle_time);
 		new_base_time += cycle_time * (nr_of_cycles + 1);
 	}