Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1627954yba; Thu, 25 Apr 2019 03:05:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqyZ9L3lygzEjdbOgmJALHL/+Bbi67A0hYgobyugdF4MZIs7Q6CkLdTjL0pJj4IjyZcqgbdp X-Received: by 2002:a17:902:184:: with SMTP id b4mr38088319plb.26.1556186717923; Thu, 25 Apr 2019 03:05:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556186717; cv=none; d=google.com; s=arc-20160816; b=C9m+O3xqsE0jxe4cZsYkNWh52mGJVLD/zFkv8BFYyZse0Rp4fXmPlEXEIqD/odk+Rt 3+5ppYZcDIDK6Cz2Ge9Tgepk/F/DEjxYCGSpEo1DIVT6zvGCQkTw7pZAb4Qw7py8dC8D TF06ezWFJCXikgfEZfD3hYfriPnMLUqZ743LxUvhFdnpOXn1OVbhsAvAeQ5PgEaBA/+n 1N8V5KsBPD2BgWk0JvMTMPhKt3Dj24Ks8G8k30jKL0AA5ij81jxQA46XhsZZI+Amcz0M 7binYvKo5D9hHq+8nVNkwlF5isMJl1dI5NSATgUeu1K5pFvehaTRJnbHfiIHSFy8sAd1 EUEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:date:user-agent:message-id:to:subject :from:cc:references:in-reply-to:content-transfer-encoding :mime-version:dkim-signature; bh=hlX22Pjcl4LhF3cGF6a2IJw6gyfsk7nMlw6PoHV/pdQ=; b=SxsNAV9emQvMmYKK3ULJNXHVMeUoua863EobYnzQ3+FaOvThNW7kk/WiMqrZRK8lgl wKjdxscUylmsqDmDMmqc/zDiKEl+acEMYj3ZDBBS3xReozmM4JNEUc41M18VSWfJ089d 5bYE09gPdNTK3LzCLPhAI3ienMHL7tHg53dKmb7vPP+cKXWI2U29Te4PJJ+9ILKNSw4/ EGTp4uXKdXPdFl924olth7l8rxSF5ZfRPDZFu4sikJUCvbh3Z8KsjyGsXlCb1zPU1TT+ ZBg/v5YfUj5ZPncLVYyjLgGug4dYfR5SUqklqH2FiyqmPdbFP1RNxphEp+OPMK5fNhLb cUnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=rzaZXS9t; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r136si202416pfc.289.2019.04.25.03.05.01; Thu, 25 Apr 2019 03:05:17 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=rzaZXS9t; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387547AbfDXV1f (ORCPT + 99 others); Wed, 24 Apr 2019 17:27:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:39418 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731175AbfDXV1e (ORCPT ); Wed, 24 Apr 2019 17:27:34 -0400 Received: from localhost (unknown [104.132.0.74]) (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 990A32183E; Wed, 24 Apr 2019 21:27:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1556141253; bh=xUxFNsMZ8txNu7js12qDGeRYYEYc3jJ0GwudbvyOSd4=; h=In-Reply-To:References:Cc:From:Subject:To:Date:From; b=rzaZXS9tp0Wrwyxq4Sl6jD+5RE/ExNHeXPc/36SNn0Z69ZrmA7BV8FUgWkRwfBGfp 6hSFUqO9qrTqfhxEZr49uZcbwpb3ErbNK4oZv6N05aLJ7IUbMAOtbw8n5gaLqwXIdK Fx6L6fqUhFOdKJSE0x61mMqu7Iu2qX7h7l+MCGcc= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable In-Reply-To: References: <1553954090-65470-1-git-send-email-nixiaoming@huawei.com> <155605990288.15276.7089726827167650203@swboyd.mtv.corp.google.com> Cc: "linux-clk@vger.kernel.org" , "linux-kernel@vger.kernel.org" From: Stephen Boyd Subject: RE: [PATCH v3 1/2] clk:Fix divide-by-zero in divider_ro_round_rate_parent To: "jbrunet@baylibre.com" , "mojha@codeaurora.org" , "mturquette@baylibre.com" , "sboyd@codeaurora.org" , "soren.brinkmann@xilinx.com" , Nixiaoming Message-ID: <155614125279.15276.7728769195271989477@swboyd.mtv.corp.google.com> User-Agent: alot/0.8 Date: Wed, 24 Apr 2019 14:27:32 -0700 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Quoting Nixiaoming (2019-04-24 08:31:23) > On Wed, Apr 24, 2019 at 6:52 AM Stephen Boyd wrote: > >Quoting nixiaoming (2019-03-30 06:54:50) > >> In the function divider_recalc_rate() The judgment of the return value= of > >> _get_div() indicates that the return value of _get_div() can be 0. > > > >When does _get_div() return 0? It can't be CLK_DIVIDER_MAX_AT_ZERO or > >CLK_DIVIDER_POWER_OF_TWO. I suppose it could be CLK_DIVIDER_ONE_BASED if > >CLK_DIVIDER_ALLOW_ZERO is set? Or just CLK_DIVIDER_ALLOW_ZERO is set? Or > >a table that has 0 in it for some odd reason. > > > divider_ro_round_rate_parent() is an exported function. > There is no parameter check or return value check before=20 > and after calling _get_div(), which may result in a divide by zero error. >=20 > Case1: The "flags" contains CLK_DIVIDER_ONE_BASED, and "val" is 0. > Case2: The "flags" does not contain CLK_DIVIDER_ONE_BASED,=20 > CLK_DIVIDER_POWER_OF_TWO, CLK_DIVIDER_MAX_AT_ZERO, =20 > "table" is NULL. "val" is 0xffffffff > In both cases _get_div() returns 0 Alright, so this is all theoretical and not happening in practice? Best to do nothing then I think. >=20 > >> In order to avoid the divide-by-zero error, add check for return value > >> of _get_div() in the divider_ro_round_rate_parent() > >>=20 > >> Signed-off-by: nixiaoming > >> Reviewed-by: Mukesh Ojha > >> --- > >> drivers/clk/clk-divider.c | 3 +++ > >> 1 file changed, 3 insertions(+) > >>=20 > >> diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c > >> index e5a1726..f4bf7a4 100644 > >> --- a/drivers/clk/clk-divider.c > >> +++ b/drivers/clk/clk-divider.c > >> @@ -347,6 +347,9 @@ long divider_ro_round_rate_parent(struct clk_hw *h= w, struct clk_hw *parent, > >> int div; > >> =20 > >> div =3D _get_div(table, val, flags, width); > >> + /* avoid divide-by-zero */ > >> + if (!div) > >> + return -EINVAL; > > > >Can you please give more details on what's happening here? Who's the > >caller? What are the arguments being passed in? Shouldn't we check for > >CLK_DIVIDER_ALLOW_ZERO and then return prate as it comes in instead of > >returning an error? > > > I found that there may be a divide-by-zero error by code review, > for example: "flags" is CLK_DIVIDER_ONE_BASED and "val" is 0. > So simply add a return value check to avoid divide-by-zero >=20 > thanks for your suggestion,=20 > I will resend the patch later > refer to your advice and divider_recalc_rate() to add a check for CLK_DIV= IDER_ALLOW_ZERO >=20 Ok. I'm actually wondering why divider_ro_round_rate_parent() doesn't use clk_divider_bestdiv() instead of _get_div(). I think we did this to make things simpler. We could make clk_divider_bestdiv() take a 'val' argument to start the divider loop (and make that 0 for the "normal" case) and then look at the 'flags' for CLK_DIVIDER_READ_ONLY to restrict the 'maxdiv' and 'i' arguments there to be whatever the val is passed in to be. It would require passing the val into _get_maxdiv() too, and only caring about the val when the read only flag is set. Then I think we would naturally get div-by-zero avoidance code and we could collapse the divider code paths substantially into clk_divider_bestdiv(). It would be even better if that patch could come after moving the clk-divider ops to use .determine_rate() instead of .round_rate() clk_ops. If we did that then we could enhance the divider code to handle rate request clamping.