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 38C80C742A8 for ; Fri, 12 Jul 2019 06:03:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0E49F21019 for ; Fri, 12 Jul 2019 06:03:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562911403; bh=JlKl/AFRP//Q2Qa46FgNw9uvAElTFd/VfufhbbTwfdI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:List-ID:From; b=z5msypYMaMZQ6501m/qwxJeFr+9ZZLjvggWgPinmmqM/CByd++dWw03UZ9yy9trsI nROEHYkqxCFXq01sdVv2EzhSDoupe9UgOBgHdpdahKDLnVnBaHCwBEbP0Z08T0jL6b UDCCckneX8PiHUZVWeGXzKzl4kslyM3ykb7sSXd0= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725784AbfGLGDW (ORCPT ); Fri, 12 Jul 2019 02:03:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:51194 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725267AbfGLGDW (ORCPT ); Fri, 12 Jul 2019 02:03:22 -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 0281F20863; Fri, 12 Jul 2019 06:03:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562911401; bh=JlKl/AFRP//Q2Qa46FgNw9uvAElTFd/VfufhbbTwfdI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=BnfgcN9nVmcY9PH9Cl9qhoBkq49uwEcUa6TmqsLNDwOGZBjtRldYbAKT2meWMmezN WHIy6BQVAiAOS2zPc4hlIsZHtzIq3fLJESCeJbDxRIVah5JHtJ/Y6bfwLBbRxKzv1y hoS0EtoH9mXP3sLt6ebyEWy5AytF7MLcxUuHkoFk= Date: Fri, 12 Jul 2019 09:03:09 +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: <20190712060309.GM23598@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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190711173110.GN25821@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 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. Thanks > > Jason