All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@mellanox.com>
To: Leon Romanovsky <leon@kernel.org>
Cc: Doug Ledford <dledford@redhat.com>,
	RDMA mailing list <linux-rdma@vger.kernel.org>,
	Yamin Friedman <yaminf@mellanox.com>
Subject: Re: [PATCH rdma-next] lib/dim: Prevent overflow in calculation of ratio statistics
Date: Thu, 11 Jul 2019 17:31:14 +0000	[thread overview]
Message-ID: <20190711173110.GN25821@mellanox.com> (raw)
In-Reply-To: <20190711171922.GJ23598@mtr-leonro.mtl.com>

On Thu, Jul 11, 2019 at 08:19:22PM +0300, Leon Romanovsky wrote:
> On Thu, Jul 11, 2019 at 04:11:07PM +0000, Jason Gunthorpe wrote:
> > On Thu, Jul 11, 2019 at 06:47:34PM +0300, Leon Romanovsky wrote:
> > > > > diff --git a/lib/dim/dim.c b/lib/dim/dim.c
> > > > > index 439d641ec796..38045d6d0538 100644
> > > > > +++ b/lib/dim/dim.c
> > > > > @@ -74,8 +74,8 @@ void dim_calc_stats(struct dim_sample *start, struct dim_sample *end,
> > > > >  					delta_us);
> > > > >  	curr_stats->cpms = DIV_ROUND_UP(ncomps * USEC_PER_MSEC, delta_us);
> > > > >  	if (curr_stats->epms != 0)
> > > > > -		curr_stats->cpe_ratio =
> > > > > -				(curr_stats->cpms * 100) / curr_stats->epms;
> > > > > +		curr_stats->cpe_ratio = DIV_ROUND_DOWN_ULL(
> > > > > +			curr_stats->cpms * 100, curr_stats->epms);
> > > >
> > > > This will still potentially overfow the 'int' for cpe_ratio if epms <
> > > > 100 ?
> > >
> > > I assumed that assignment to "unsigned long long" will do the trick.
> > > https://elixir.bootlin.com/linux/latest/source/include/linux/kernel.h#L94
> >
> > That only protects the multiply, the result of DIV_ROUND_DOWN_ULL is
> > casted to int.
> 
> It is ok, the result is "int" and it will be small, 100 in multiply
> represents percentage.

Percentage would be divide by 100..

Like I said it will overflow if epms < 100 ...

Jason

  reply	other threads:[~2019-07-11 17:31 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-11 15:31 [PATCH rdma-next] lib/dim: Prevent overflow in calculation of ratio statistics Leon Romanovsky
2019-07-11 15:43 ` Jason Gunthorpe
2019-07-11 15:47   ` Leon Romanovsky
2019-07-11 16:11     ` Jason Gunthorpe
2019-07-11 17:19       ` Leon Romanovsky
2019-07-11 17:31         ` Jason Gunthorpe [this message]
2019-07-12  6:03           ` Leon Romanovsky
2019-07-12 15:23             ` Jason Gunthorpe
2019-07-14 10:54               ` Leon Romanovsky
2019-07-18 17:39                 ` Jason Gunthorpe
2019-07-19 12:38                   ` Leon Romanovsky

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=20190711173110.GN25821@mellanox.com \
    --to=jgg@mellanox.com \
    --cc=dledford@redhat.com \
    --cc=leon@kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=yaminf@mellanox.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.