Re: [PATCH 2/2 v3] mmc: dw_mmc: Fix the CTO overflow calculation for 32-bit systems

From: Andy Shevchenko
Date: Mon Feb 26 2018 - 13:30:50 EST


On Mon, Feb 26, 2018 at 7:14 PM, Evgeniy Didin
<Evgeniy.Didin@xxxxxxxxxxxx> wrote:
> On Mon, 2018-02-26 at 18:53 +0200, Andy Shevchenko wrote:
>> On Mon, Feb 26, 2018 at 5:14 PM, Evgeniy Didin
>> <Evgeniy.Didin@xxxxxxxxxxxx> wrote:
>> > On Mon, 2018-02-26 at 16:39 +0200, Andy Shevchenko wrote:
>> > > On Mon, Feb 26, 2018 at 4:34 PM, Evgeniy Didin
>> > > <Evgeniy.Didin@xxxxxxxxxxxx> wrote:
>> > > > In commit 4c2357f57dd5 ("mmc: dw_mmc: Fix the CTO timeout calculation")
>> > > > have been made changes which can cause multiply overflow for 32-bit systems.
>> > > > The value of cto_ms is lower the drto_ms, but nevertheless overflow can occur.
>> > > > Lets cast this multiply to u64 type which prevents overflow.
>> > > > - cto_ms = DIV_ROUND_UP(MSEC_PER_SEC * cto_clks * cto_div, host->bus_hz);
>> > > > +
>> > > > + cto_ms = DIV_ROUND_UP((u64)MSEC_PER_SEC * cto_clks * cto_div, host->bus_hz);
>> > >
>> > > IIRC, someone commented on this or similar, i.e.
>> > >
>> > > DIV_ROUND_UP_ULL() ?
>> >
>> > Switch DIV_ROUND_UP macro to DIV_ROUND_UP_ULL is not reasonable
>> > because overflow happens on multiply and DIV_ROUND_UP_ULL helps
>> > with sum overflow.
>>
>> Did you try to compile your code for 32-bit target?
> Yes, we have compiled code for 32-bit system.

> I am wondering why are you asking that?

Because I simple didn't believe you.

ERROR: "__udivdi3" [drivers/mmc/host/dw_mmc.ko] undefined!
...scripts/Makefile.modpost:92: recipe for target '__modpost' failed
make[2]: *** [__modpost] Error 1


+++ b/drivers/mmc/host/dw_mmc.c
@@ -409,7 +409,7 @@ static inline void dw_mci_set_cto(struct dw_mci *host)

- cto_ms = DIV_ROUND_UP(MSEC_PER_SEC * cto_clks * cto_div, host->bus_hz);
+ cto_ms = DIV_ROUND_UP((u64)MSEC_PER_SEC * cto_clks * cto_div,
host->bus_hz);

--
With Best Regards,
Andy Shevchenko