All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vinod Koul <vkoul@kernel.org>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-arm-msm@vger.kernel.org,
	Bjorn Andersson <bjorn.andersson@linaro.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] linux/kernel.h: fix overflow for DIV_ROUND_UP_ULL
Date: Wed, 26 Jun 2019 09:25:22 +0530	[thread overview]
Message-ID: <20190626035522.GN2962@vkoul-mobl> (raw)
In-Reply-To: <20190625153231.9b2d2fb8d8bf35c6acd5aafd@linux-foundation.org>

On 25-06-19, 15:32, Andrew Morton wrote:
> On Tue, 25 Jun 2019 15:29:38 -0700 Andrew Morton <akpm@linux-foundation.org> wrote:
> 
> > On Tue, 25 Jun 2019 15:35:18 +0530 Vinod Koul <vkoul@kernel.org> wrote:
> > 
> > > DIV_ROUND_UP_ULL adds the two arguments and then invokes
> > > DIV_ROUND_DOWN_ULL. But on a 32bit system the addition of two 32 bit
> > > values can overflow. DIV_ROUND_DOWN_ULL does it correctly and stashes
> > > the addition into a unsigned long long so cast the result to unsigned
> > > long long here to avoid the overflow condition.
> > >
> > > ...
> > >
> > > --- a/include/linux/kernel.h
> > > +++ b/include/linux/kernel.h
> > > @@ -93,7 +93,8 @@
> > >  #define DIV_ROUND_DOWN_ULL(ll, d) \
> > >  	({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
> > >  
> > > -#define DIV_ROUND_UP_ULL(ll, d)		DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
> > > +#define DIV_ROUND_UP_ULL(ll, d) \
> > > +	({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) })
> > >  
> > 
> > This clearly wasn't tested :(

Apologies for that, I did test and stash, but failed to amend the
commit. I should have noticed while sending but :(

Anyway I had the same conclusion as yous, so all is good.

Thanks for fixing this

Reviewed-by: Vinod Koul <vkoul@kernel.org>
Tested-by: Vinod Koul <vkoul@kernel.org>

> > 
> > fs/fs-writeback.c: In function wb_split_bdi_pages:
> > ./include/linux/kernel.h:97:65: error: expected ; before } token
> >   ({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) })
> >                                                                  ^
> > fs/fs-writeback.c:811:10: note: in expansion of macro DIV_ROUND_UP_ULL
> >    return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw);
> > 
> > 
> > From: Andrew Morton <akpm@linux-foundation.org>
> > Subject: linux-kernelh-fix-overflow-for-div_round_up_ull-fix
> > 
> > DIV_ROUND_UP_ULL must be an rval
> > 
> > Cc: Bjorn Andersson <bjorn.andersson@linaro.org>
> > Cc: Randy Dunlap <rdunlap@infradead.org>
> > Cc: Vinod Koul <vkoul@kernel.org>
> > Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> > ---
> > 
> >  include/linux/kernel.h |    6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > --- a/include/linux/kernel.h~linux-kernelh-fix-overflow-for-div_round_up_ull-fix
> > +++ a/include/linux/kernel.h
> > @@ -93,8 +93,10 @@
> >  #define DIV_ROUND_DOWN_ULL(ll, d) \
> >  	({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
> >  
> > -#define DIV_ROUND_UP_ULL(ll, d) \
> > -	({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) })
> > +#define DIV_ROUND_UP_ULL(ll, d) ({ \
> > +	unsigned long long _tmp; \
> > +	_tmp = DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)); \
> > +	_tmp; })
> 
> Simpler:
> 
> --- a/include/linux/kernel.h~linux-kernelh-fix-overflow-for-div_round_up_ull-fix
> +++ a/include/linux/kernel.h
> @@ -94,7 +94,7 @@
>  	({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
>  
>  #define DIV_ROUND_UP_ULL(ll, d) \
> -	({ DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) })
> +	DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
>  
>  #if BITS_PER_LONG == 32
>  # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
> _

-- 
~Vinod

      reply	other threads:[~2019-06-26  3:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-25 10:05 Vinod Koul
2019-06-25 22:29 ` Andrew Morton
2019-06-25 22:32   ` Andrew Morton
2019-06-26  3:55     ` Vinod Koul [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=20190626035522.GN2962@vkoul-mobl \
    --to=vkoul@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=bjorn.andersson@linaro.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rdunlap@infradead.org \
    --subject='Re: [PATCH] linux/kernel.h: fix overflow for DIV_ROUND_UP_ULL' \
    /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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.