RE: [PATCH] media: qcom: use div64_u64() instead of do_div()

From: David Laight
Date: Fri Feb 11 2022 - 07:21:25 EST


From: Dmitry Baryshkov
> Sent: 10 February 2022 10:17
>
> On 09/02/2022 11:38, Qing Wang wrote:
> > From: Wang Qing <wangqing@xxxxxxxx>
> >
> > 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.
>
>
> NAK
>
> The div64_u64 is not equivalent to do_div. It returns the quotient
> rather than modifying the first arg. Moreover it is unoptimal on 32-bit
> arches.
>
> >
> > Signed-off-by: Wang Qing <wangqing@xxxxxxxx>
> > ---
> > drivers/media/platform/qcom/venus/vdec.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/media/platform/qcom/venus/vdec.c b/drivers/media/platform/qcom/venus/vdec.c
> > index 91da3f5..941a904
> > --- a/drivers/media/platform/qcom/venus/vdec.c
> > +++ b/drivers/media/platform/qcom/venus/vdec.c
> > @@ -435,7 +435,7 @@ static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
> > return -EINVAL;
> >
> > fps = (u64)USEC_PER_SEC;
> > - do_div(fps, us_per_frame);
> > + div64_u64(fps, us_per_frame);

That one is particularly insane to begin with.
Why was it even do_div()?
I guess because:
fps = USEC_PER_SEC / us_per_frame;
caused the compiler to call the 64bit divide function because the 'us_per_frame'
is 64bit - even though the code only makes sense if it is small.

The correct fix here is probably to change the type of us_per_frame to 32bit
or just do:
fps = USEC_PER_FRAM / (unsigned int)us_per_frame.

Oh, and fps definitely doesn't need to be 64bit either.

IMHO all (and I mean ALL) of these patches to change do_div() to div64_u64()
need dropping.
Maybe one or two are real problems, but the surrounding code needs
correctly examining.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)