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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD241EE3F33 for ; Tue, 12 Sep 2023 21:11:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230052AbjILVLj (ORCPT ); Tue, 12 Sep 2023 17:11:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230453AbjILVLi (ORCPT ); Tue, 12 Sep 2023 17:11:38 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4584E10CE; Tue, 12 Sep 2023 14:11:34 -0700 (PDT) X-IronPort-AV: E=McAfee;i="6600,9927,10831"; a="382301443" X-IronPort-AV: E=Sophos;i="6.02,141,1688454000"; d="scan'208";a="382301443" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Sep 2023 14:11:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10831"; a="743858277" X-IronPort-AV: E=Sophos;i="6.02,141,1688454000"; d="scan'208";a="743858277" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga002.jf.intel.com with ESMTP; 12 Sep 2023 14:11:02 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 037C81E5; Wed, 13 Sep 2023 00:11:00 +0300 (EEST) Date: Wed, 13 Sep 2023 00:11:00 +0300 From: Andy Shevchenko To: Biju Das Cc: Michael Turquette , Stephen Boyd , linux-clk@vger.kernel.org, Geert Uytterhoeven , Prabhakar Mahadev Lad , linux-renesas-soc@vger.kernel.org, Julia Lawall Subject: Re: [PATCH v5 2/4] clk: vc3: Fix 64 by 64 division Message-ID: References: <20230824104812.147775-1-biju.das.jz@bp.renesas.com> <20230824104812.147775-3-biju.das.jz@bp.renesas.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230824104812.147775-3-biju.das.jz@bp.renesas.com> Precedence: bulk List-ID: X-Mailing-List: linux-clk@vger.kernel.org On Thu, Aug 24, 2023 at 11:48:10AM +0100, Biju Das wrote: > Fix the below cocci warnings by replacing do_div()->div64_ul() and > bound the result with a max value of U16_MAX. > > cocci warnings: > drivers/clk/clk-versaclock3.c:404:2-8: WARNING: do_div() does a > 64-by-32 division, please consider using div64_ul instead. It's nice, but there is a room for a couple of improvements. See below. ... > /* Determine best fractional part, which is 16 bit wide */ > div_frc = rate % *parent_rate; > div_frc *= BIT(16) - 1; > - do_div(div_frc, *parent_rate); > > - vc3->div_frc = (u32)div_frc; > + vc3->div_frc = min_t(u64, div64_ul(div_frc, *parent_rate), U16_MAX); First of all, as Linus Torvalds pointed out [1] min_t() is often used as a shortcut for clamp(). Second one, the BIT(16) - 1 is specifically used as the value related to the bits in the hardware and u16 is a software type that coincidentially has the same maximum as the above mentioned bitfield. That said, here this should be clamped to the [0 .. BIT(16) - 1] range. Since the patch is applied perhaps you can cook a followup. To everyone the message is simple: try to not use typed version of min() and clamp() at all. > rate = (*parent_rate * > - (vc3->div_int * VC3_2_POW_16 + div_frc) / VC3_2_POW_16); > + (vc3->div_int * VC3_2_POW_16 + vc3->div_frc) / VC3_2_POW_16); [1]: https://lore.kernel.org/lkml/CAHk-=whwEAc22wm8h9FESPB5X+P4bLDgv0erBQMa1buTNQW7tA@mail.gmail.com/ -- With Best Regards, Andy Shevchenko