Message ID | 20230706100454.28998-1-shuijing.li@mediatek.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp2454185vqx; Thu, 6 Jul 2023 03:27:52 -0700 (PDT) X-Google-Smtp-Source: APBJJlFDtID0ZUXv0dA4F/Xj3rz9V/mEGuEDKWfgu9NR8b5FXQQqPUefuPM+/QkQc9AzkmgYXR0F X-Received: by 2002:a17:903:244e:b0:1b7:e355:d1ea with SMTP id l14-20020a170903244e00b001b7e355d1eamr1724234pls.24.1688639272491; Thu, 06 Jul 2023 03:27:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688639272; cv=none; d=google.com; s=arc-20160816; b=FfZyIcCa1lAF7boh2HL0f+Dqm117a8+6jMPfnwCqrn7jgwok0raA2qMddnjldD7kfe uTn5pVVIgyqvOn36F+ocyGBi0EqxVqkM0dIsS3yQ/IkRQCcHgkkaTnfC0TiWRON8aI4w 4PDcrvlasMjr8d0dODTD+IHPg8Wpvi5LSLiTqvHIK5f++zQb+yUMWb96tFWF/ZhvSrEq AR4HYhCce2ssFoBFp2RCILkZokEHtOBznP3oXvc2TQFGZIbf4M0hCMkKc/W3Qeo9CJ91 JJLpNrkTZ+Z6jpjOSdw6Qvh9PPpoWVt08LLUc+owvWuxCU3utCihSYsqgWCrhy9BGL2e PRsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=TS+0nfycyMyZboLtN8mtKKdv0Ar+eMXmyw7XCZMA2LM=; fh=kzXGkAFtq9dLL87WnlI+ums9383k7SV3dTSWaV3WGmw=; b=Eruv+gq8FXX8OC5ZTg4vBvbl1tJwXwgNcYahTZhEYFTtJt9Xf/2luh9Nk0xg1e+DAm 8nmVvvj6F9zwswFUkFgByDBKCUaMUmRxh0d5/+RFh4Q8ceoYYD2Jrzg6pdoNLQy2CoEI a6+KnuPGYN70S+8bHGRpbDKEpcJsxongw42MZwyS1KQRd2OpXLVaBMXSD8U7ek2OLsvd 0qUp/V/iNQwfr0r+tqCaEAEl/pbkq3q4Zt2CKyYWFuvfuzt/mpuH/OdPSloDoYrrKJ5n Hak0TMaDnoc4nIcKZMFSFqt+/2hNZzGGTDrc1Rnf2854GX1HGjYjcOhnCYbB9l4jZRcw /7ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=fJiHurZX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id la8-20020a170902fa0800b001b891de90dbsi1001858plb.72.2023.07.06.03.27.38; Thu, 06 Jul 2023 03:27:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=fJiHurZX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231552AbjGFKEv (ORCPT <rfc822;hadasmailinglist@gmail.com> + 99 others); Thu, 6 Jul 2023 06:04:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229508AbjGFKEt (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 6 Jul 2023 06:04:49 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC61DDD; Thu, 6 Jul 2023 03:04:43 -0700 (PDT) X-UUID: 844ca40e1be411ee9cb5633481061a41-20230706 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=TS+0nfycyMyZboLtN8mtKKdv0Ar+eMXmyw7XCZMA2LM=; b=fJiHurZXnnY+Lx6TfYtKU5N9yAVtg4kPU60MYMbHE+FqvFav2JzXuC7wRap1ytOAmVhpZigstnjRiFDC3Cer7+mgJ559SiDzZ0nVlIrMax4is+kLdeUeW2zgmjBxVRHIQM/mGQOTUeyGIAcD+XbpzQARdMJ44N1/w67IVy8cVJ0=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.27,REQID:dc2f369f-25e0-4dda-b1cc-f807240d32cb,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Release_Ham,ACTI ON:release,TS:70 X-CID-INFO: VERSION:1.1.27,REQID:dc2f369f-25e0-4dda-b1cc-f807240d32cb,IP:0,URL :0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Spam_GS981B3D,ACTI ON:quarantine,TS:70 X-CID-META: VersionHash:01c9525,CLOUDID:4396ee0d-c22b-45ab-8a43-3004e9216b56,B ulkID:230706180438HVKI64YI,BulkQuantity:0,Recheck:0,SF:19|48|38|29|28|17,T C:nil,Content:0,EDM:-3,IP:nil,URL:11|1,File:nil,Bulk:nil,QS:nil,BEC:nil,CO L:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_SDM,TF_CID_SPAM_ASC,TF_CID_SPAM_FAS, TF_CID_SPAM_FSD,TF_CID_SPAM_ULN X-UUID: 844ca40e1be411ee9cb5633481061a41-20230706 Received: from mtkmbs13n1.mediatek.inc [(172.21.101.193)] by mailgw01.mediatek.com (envelope-from <shuijing.li@mediatek.com>) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 986363233; Thu, 06 Jul 2023 18:04:38 +0800 Received: from mtkmbs13n1.mediatek.inc (172.21.101.193) by MTKMBS14N1.mediatek.inc (172.21.101.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Thu, 6 Jul 2023 18:04:36 +0800 Received: from mszsdhlt06.gcn.mediatek.inc (10.16.6.206) by mtkmbs13n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Thu, 6 Jul 2023 18:04:36 +0800 From: Shuijing Li <shuijing.li@mediatek.com> To: <thierry.reding@gmail.com>, <u.kleine-koenig@pengutronix.de>, <matthias.bgg@gmail.com>, <angelogioacchino.delregno@collabora.com> CC: <devicetree@vger.kernel.org>, <linux-pwm@vger.kernel.org>, <linux-kernel@vger.kernel.org>, <linux-arm-kernel@lists.infradead.org>, <linux-mediatek@lists.infradead.org>, <Project_Global_Chrome_Upstream_Group@mediatek.com>, <jitao.shi@mediatek.com>, Shuijing Li <shuijing.li@mediatek.com> Subject: [PATCH] pwm: mtk_disp: fix disp_pwm coverity issue Date: Thu, 6 Jul 2023 18:04:54 +0800 Message-ID: <20230706100454.28998-1-shuijing.li@mediatek.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-MTK: N X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS, T_SCC_BODY_TEXT_LINE,T_SPF_TEMPERROR,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770666614264704566?= X-GMAIL-MSGID: =?utf-8?q?1770666614264704566?= |
Series |
pwm: mtk_disp: fix disp_pwm coverity issue
|
|
Commit Message
Shuijing Li
July 6, 2023, 10:04 a.m. UTC
There is a coverity issue in the original mtk_disp_pwm_get_state()
function. In function call DIV64_U64_ROUND_UP, division by expression
Which may be zero has undefined behavior.
Fix this accordingly.
Signed-off-by: Shuijing Li <shuijing.li@mediatek.com>
---
drivers/pwm/pwm-mtk-disp.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
Comments
On 06/07/2023 12:04, Shuijing Li wrote: > There is a coverity issue in the original mtk_disp_pwm_get_state() > function. In function call DIV64_U64_ROUND_UP, division by expression > Which may be zero has undefined behavior. > Fix this accordingly. > > Signed-off-by: Shuijing Li <shuijing.li@mediatek.com> > --- > drivers/pwm/pwm-mtk-disp.c | 9 ++++++++- > 1 file changed, 8 insertions(+), 1 deletion(-) > > diff --git a/drivers/pwm/pwm-mtk-disp.c b/drivers/pwm/pwm-mtk-disp.c > index 79e321e96f56..ca00058a6ef4 100644 > --- a/drivers/pwm/pwm-mtk-disp.c > +++ b/drivers/pwm/pwm-mtk-disp.c > @@ -196,6 +196,14 @@ static int mtk_disp_pwm_get_state(struct pwm_chip *chip, > return err; > } > > + rate = clk_get_rate(mdp->clk_main); > + if (rate <= 0) { > + dev_err(chip->dev, "Can't get rate: %pe\n", ERR_PTR(rate)); > + clk_disable_unprepare(mdp->clk_mm); > + clk_disable_unprepare(mdp->clk_main); > + return err; > + } > + > /* > * Apply DISP_PWM_DEBUG settings to choose whether to enable or disable > * registers double buffer and manual commit to working register before > @@ -206,7 +214,6 @@ static int mtk_disp_pwm_get_state(struct pwm_chip *chip, > mdp->data->bls_debug_mask, > mdp->data->bls_debug_mask); > > - rate = clk_get_rate(mdp->clk_main); > con0 = readl(mdp->base + mdp->data->con0); > con1 = readl(mdp->base + mdp->data->con1); > pwm_en = readl(mdp->base + DISP_PWM_EN); IMHO, it should be done int the function `mtk_disp_pwm_apply` too. Do you agree ?
Il 06/07/23 14:29, Alexandre Mergnat ha scritto: > > > On 06/07/2023 12:04, Shuijing Li wrote: >> There is a coverity issue in the original mtk_disp_pwm_get_state() >> function. In function call DIV64_U64_ROUND_UP, division by expression >> Which may be zero has undefined behavior. >> Fix this accordingly. >> >> Signed-off-by: Shuijing Li <shuijing.li@mediatek.com> >> --- >> drivers/pwm/pwm-mtk-disp.c | 9 ++++++++- >> 1 file changed, 8 insertions(+), 1 deletion(-) >> >> diff --git a/drivers/pwm/pwm-mtk-disp.c b/drivers/pwm/pwm-mtk-disp.c >> index 79e321e96f56..ca00058a6ef4 100644 >> --- a/drivers/pwm/pwm-mtk-disp.c >> +++ b/drivers/pwm/pwm-mtk-disp.c >> @@ -196,6 +196,14 @@ static int mtk_disp_pwm_get_state(struct pwm_chip *chip, >> return err; >> } >> + rate = clk_get_rate(mdp->clk_main); >> + if (rate <= 0) { >> + dev_err(chip->dev, "Can't get rate: %pe\n", ERR_PTR(rate)); >> + clk_disable_unprepare(mdp->clk_mm); >> + clk_disable_unprepare(mdp->clk_main); >> + return err; >> + } >> + >> /* >> * Apply DISP_PWM_DEBUG settings to choose whether to enable or disable >> * registers double buffer and manual commit to working register before >> @@ -206,7 +214,6 @@ static int mtk_disp_pwm_get_state(struct pwm_chip *chip, >> mdp->data->bls_debug_mask, >> mdp->data->bls_debug_mask); >> - rate = clk_get_rate(mdp->clk_main); >> con0 = readl(mdp->base + mdp->data->con0); >> con1 = readl(mdp->base + mdp->data->con1); >> pwm_en = readl(mdp->base + DISP_PWM_EN); > > IMHO, it should be done int the function `mtk_disp_pwm_apply` too. > Do you agree ? > I think that realistically this will never happen. We're getting the clk_main clock's handle at probe time (or fail and get out), then the PWM clock never has the CLK_GET_RATE_NOCACHE flag, as there wouldn't be any reason to not cache the rates for this clock. But even if we had the CLK_GET_RATE_NOCACHE flag, it wouldn't change much, as our validation actually happens at probe time... This means that our call to clk_get_rate is guaranteed to have a not NULL pointer to this clock's `struct clk_core` and, unless the declaration of this clock in the clock controller driver was wrong, for it, or for its parent, the branch... if (!core->num_parents || core->parent) return core->rate; ...is always satisfied, so, in the end, this instance of clk_get_rate() can't really return zero. If you got such an issue, I suggest to check what the problem is, as it is likely to be outside of this driver. ...that, besides the fact that the proposed code is incorrect, as clk_get_rate() returns an `unsigned long`, so `rate` can never be less than zero, anyway. Cheers, Angelo
diff --git a/drivers/pwm/pwm-mtk-disp.c b/drivers/pwm/pwm-mtk-disp.c index 79e321e96f56..ca00058a6ef4 100644 --- a/drivers/pwm/pwm-mtk-disp.c +++ b/drivers/pwm/pwm-mtk-disp.c @@ -196,6 +196,14 @@ static int mtk_disp_pwm_get_state(struct pwm_chip *chip, return err; } + rate = clk_get_rate(mdp->clk_main); + if (rate <= 0) { + dev_err(chip->dev, "Can't get rate: %pe\n", ERR_PTR(rate)); + clk_disable_unprepare(mdp->clk_mm); + clk_disable_unprepare(mdp->clk_main); + return err; + } + /* * Apply DISP_PWM_DEBUG settings to choose whether to enable or disable * registers double buffer and manual commit to working register before @@ -206,7 +214,6 @@ static int mtk_disp_pwm_get_state(struct pwm_chip *chip, mdp->data->bls_debug_mask, mdp->data->bls_debug_mask); - rate = clk_get_rate(mdp->clk_main); con0 = readl(mdp->base + mdp->data->con0); con1 = readl(mdp->base + mdp->data->con1); pwm_en = readl(mdp->base + DISP_PWM_EN);