From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.6 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B72B3C76188 for ; Fri, 19 Jul 2019 12:39:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 88D8C2184E for ; Fri, 19 Jul 2019 12:39:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563539953; bh=cesfhJHTCpYUDVj6l3RswxTaGpggZ7x2y6SD4p0RwRY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:List-ID:From; b=mb05Z3+Y3hg7n6gXsT1CgvSqXS6BvFbE18Xw5dIW1jBXmKYRMtiXOSLBqeKzpuWay GydOePAdOcFkttyBq4tiqpE7zdA8gk5sUVD77rkpHdQtLmsiwvfjZtXCL7lnKsrI1B eKrff6YQlOqaa57jVE8tFopIPb59UCWxCbkNp/2g= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727745AbfGSMjM (ORCPT ); Fri, 19 Jul 2019 08:39:12 -0400 Received: from mail.kernel.org ([198.145.29.99]:58004 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727559AbfGSMjM (ORCPT ); Fri, 19 Jul 2019 08:39:12 -0400 Received: from localhost (unknown [37.142.3.125]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0379821849; Fri, 19 Jul 2019 12:39:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563539951; bh=cesfhJHTCpYUDVj6l3RswxTaGpggZ7x2y6SD4p0RwRY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=i46UR2mv6GhnFefqHTQBf7O0HCaGMtn9/SrzKclZgrGwBFaGA2ThsVvfESaCIooRH D8CIcTxl5+BIyBDCkZM52r+Z8wqcb72HazxLz7sABNqBpM3WY7kUdCylYPh+trlDtm 4A8dfAhzu1t3F57WpA9voUqsJh8oEHOG1iFE6H24= Date: Fri, 19 Jul 2019 15:38:55 +0300 From: Leon Romanovsky To: Jason Gunthorpe Cc: Doug Ledford , RDMA mailing list , Yamin Friedman Subject: Re: [PATCH rdma-next] lib/dim: Prevent overflow in calculation of ratio statistics Message-ID: <20190719123855.GS10130@mtr-leonro.mtl.com> References: <20190711153118.14635-1-leon@kernel.org> <20190711154324.GK25821@mellanox.com> <20190711154734.GI23598@mtr-leonro.mtl.com> <20190711161103.GL25821@mellanox.com> <20190711171922.GJ23598@mtr-leonro.mtl.com> <20190711173110.GN25821@mellanox.com> <20190712060309.GM23598@mtr-leonro.mtl.com> <20190712152315.GD27526@mellanox.com> <20190714105459.GA6039@mtr-leonro.mtl.com> <20190718173943.GG1647@mellanox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190718173943.GG1647@mellanox.com> User-Agent: Mutt/1.12.0 (2019-05-25) Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org On Thu, Jul 18, 2019 at 05:39:47PM +0000, Jason Gunthorpe wrote: > On Sun, Jul 14, 2019 at 01:54:59PM +0300, Leon Romanovsky wrote: > > On Fri, Jul 12, 2019 at 03:23:20PM +0000, Jason Gunthorpe wrote: > > > On Fri, Jul 12, 2019 at 09:03:09AM +0300, Leon Romanovsky wrote: > > > > On Thu, Jul 11, 2019 at 05:31:14PM +0000, Jason Gunthorpe wrote: > > > > > 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 ... > > > > > > > > It is unlikely to happen because cpe_ratio is between 0 to 100 and cpms > > > > * 100 is not large at all. > > > > > > > > UBSAN error is "theoretical" overflow. > > > > > > ? UBSAN is not theoretical, it only triggers if something actually > > > happens. So in this case cpms*100 was very large and overflowed. > > > > > > Maybe it shouldn't be and that is the actual bug, but if we overflowed > > > with cpms*100, then epms must be > 100 or we still overflow the > > > divide. > > > > I think that the real bug is cpms became too big. > > So I'll drop the patch until someone figures out what is happening Thanks, Yamin is working to fix it. > > Jason