Linux clock framework development
 help / color / mirror / Atom feed
From: Leo Yan <leo.yan@linaro.org>
To: Masahiro Yamada <yamada.masahiro@socionext.com>
Cc: "Wangtao (Kevin, Kirin)" <kevin.wangtao@hisilicon.com>,
	Wei Yongjun <weiyongjun1@huawei.com>,
	Michael Turquette <mturquette@baylibre.com>,
	Stephen Boyd <sboyd@codeaurora.org>,
	Kaihua Zhong <zhongkaihua@huawei.com>,
	Ruyi Wang <wangruyi@huawei.com>,
	Kai Zhao <zhaokai1@hisilicon.com>,
	linux-clk <linux-clk@vger.kernel.org>
Subject: Re: [PATCH -next] clk: hisilicon: hi3660:Fix potential NULL dereference in hi3660_stub_clk_probe()
Date: Mon, 8 Jan 2018 12:22:35 +0800	[thread overview]
Message-ID: <20180108042235.GA21618@leoy-ThinkPad-T440> (raw)
In-Reply-To: <CAK7LNAQcieqS0vrGKrtr3paQADpG0GEM_k9zPdLiKF8ykv5PaA@mail.gmail.com>

On Fri, Jan 05, 2018 at 06:50:21PM +0900, Masahiro Yamada wrote:
> 2018-01-05 18:47 GMT+09:00 Wangtao (Kevin, Kirin) <kevin.wangtao@hisilicon.com>:
> >
> >
> > 在 2018/1/5 16:40, Leo Yan 写道:
> >>
> >> On Fri, Jan 05, 2018 at 12:59:38PM +0900, Masahiro Yamada wrote:
> >>>
> >>> 2018-01-04 15:36 GMT+09:00 Wei Yongjun <weiyongjun1@huawei.com>:
> >>>>
> >>>> platform_get_resource() may return NULL, add proper check to
> >>>> avoid potential NULL dereferencing.
> >>>>
> >>>> This is detected by Coccinelle semantic patch.
> >>>>
> >>>> @@
> >>>> expression pdev, res, n, t, e, e1, e2;
> >>>> @@
> >>>>
> >>>> res = platform_get_resource(pdev, t, n);
> >>>> + if (!res)
> >>>> +   return -EINVAL;
> >>>> ... when != res == NULL
> >>>> e = devm_ioremap(e1, res->start, e2);
> >>>>
> >>>> Fixes: 4f16f7ff3bc0 ("clk: hisilicon: Add support for Hi3660 stub
> >>>> clocks")
> >>>> Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
> >>>> ---
> >>>>   drivers/clk/hisilicon/clk-hi3660-stub.c | 2 ++
> >>>>   1 file changed, 2 insertions(+)
> >>>>
> >>>> diff --git a/drivers/clk/hisilicon/clk-hi3660-stub.c
> >>>> b/drivers/clk/hisilicon/clk-hi3660-stub.c
> >>>> index 9b6c72b..e8b2c43 100644
> >>>> --- a/drivers/clk/hisilicon/clk-hi3660-stub.c
> >>>> +++ b/drivers/clk/hisilicon/clk-hi3660-stub.c
> >>>> @@ -149,6 +149,8 @@ static int hi3660_stub_clk_probe(struct
> >>>> platform_device *pdev)
> >>>>                  return PTR_ERR(stub_clk_chan.mbox);
> >>>>
> >>>>          res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> >>>> +       if (!res)
> >>>> +               return -EINVAL;
> >>>>          freq_reg = devm_ioremap(dev, res->start, resource_size(res));
> >>>>          if (!freq_reg)
> >>>>                  return -ENOMEM;
> >>>
> >>>
> >>> Probably, it is better to use devm_ioremap_resource().
> >>> This checks NULL input.
> >>
> >>
> >> Thanks for suggestion, Masahiro. This is good suggestion.
> >>
> >> Yongjun, could you spin new patch by using devm_ioremap_resource()
> >> to replace devm_ioremap()?
> >
> > clk-stub's memory region has intersection with mailbox, so we can not use
> > devm_ioremap_resource here, it will cause problem when mailbox driver be
> > accepted by mainline.
> 
> Hmm, that sounds odd.
> 
> syscon in this case?

I verified Yongjun patch v2 for using devm_ioremap_resource(), just as
Kevin mentioned after using devm_ioremap_resource() the kernel has
memory mapping conflict between stub clock driver and mailbox driver.

Following Masahiro suggestion, I changed the stub clock driver to use
syscon & regmap to access the memory, I can confirm this can work
well, you could review in below change.

But this change should note two things, one thing is it is dependent on
mailbox driver upstreaming, so I prefer we can commit one patch for this
change along with mailbox driver patch set; the second thing is we
need change DT binding document.

If you have any better solution for this, also very welcome.

Thanks,
Leo Yan

---8<---

diff --git a/drivers/clk/hisilicon/clk-hi3660-stub.c b/drivers/clk/hisilicon/clk-hi3660-stub.c
index e8dde4f..40ee63b 100644
--- a/drivers/clk/hisilicon/clk-hi3660-stub.c
+++ b/drivers/clk/hisilicon/clk-hi3660-stub.c
@@ -25,12 +25,14 @@
 #include <linux/err.h>
 #include <linux/init.h>
 #include <linux/mailbox_client.h>
+#include <linux/mfd/syscon.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/regmap.h>
 #include <dt-bindings/clock/hi3660-clock.h>
 
-#define HI3660_STUB_CLOCK_DATA		(0x70)
+#define HI3660_STUB_CLOCK_DATA		(0x570)
 #define MHZ				(1000 * 1000)
 
 #define DEFINE_CLK_STUB(_id, _cmd, _name)			\
@@ -60,7 +62,7 @@ struct hi3660_stub_clk {
 	unsigned int rate;
 };
 
-static void __iomem *freq_reg;
+static struct regmap *freq_reg;
 static struct hi3660_stub_clk_chan stub_clk_chan;
 
 static unsigned long hi3660_stub_clk_recalc_rate(struct clk_hw *hw,
@@ -72,7 +74,9 @@ static unsigned long hi3660_stub_clk_recalc_rate(struct clk_hw *hw,
 	 * LPM3 writes back the CPU frequency in shared SRAM so read
 	 * back the frequency.
 	 */
-	stub_clk->rate = readl(freq_reg + (stub_clk->id << 2)) * MHZ;
+	regmap_read(freq_reg, HI3660_STUB_CLOCK_DATA + (stub_clk->id << 2),
+		    &stub_clk->rate);
+	stub_clk->rate *= MHZ;
 	return stub_clk->rate;
 }
 
@@ -133,7 +137,7 @@ static struct clk_hw *hi3660_stub_clk_hw_get(struct of_phandle_args *clkspec,
 static int hi3660_stub_clk_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
-	struct resource *res;
+	struct device_node *np = pdev->dev.of_node;
 	unsigned int i;
 	int ret;
 
@@ -148,12 +152,12 @@ static int hi3660_stub_clk_probe(struct platform_device *pdev)
 	if (IS_ERR(stub_clk_chan.mbox))
 		return PTR_ERR(stub_clk_chan.mbox);
 
-	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-	freq_reg = devm_ioremap_resource(dev, res);
-	if (IS_ERR(freq_reg))
+	freq_reg = syscon_regmap_lookup_by_phandle(np,
+				"hisilicon,hi3660-clk-sram");
+	if (IS_ERR(freq_reg)) {
+		dev_err(dev, "failed to get sram regmap\n");
 		return PTR_ERR(freq_reg);
-
-	freq_reg += HI3660_STUB_CLOCK_DATA;
+	}
 
 	for (i = 0; i < HI3660_CLK_STUB_NUM; i++) {
 		ret = devm_clk_hw_register(&pdev->dev, &hi3660_stub_clks[i].hw);

  parent reply	other threads:[~2018-01-08  4:22 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-04  6:36 [PATCH -next] clk: hisilicon: hi3660:Fix potential NULL dereference in hi3660_stub_clk_probe() Wei Yongjun
2018-01-05  3:59 ` Masahiro Yamada
2018-01-05  8:40   ` Leo Yan
2018-01-05  8:54     ` weiyongjun (A)
2018-01-05  9:47     ` Wangtao (Kevin, Kirin)
2018-01-05  9:50       ` Masahiro Yamada
2018-01-05 10:47         ` Wangtao (Kevin, Kirin)
2018-01-08  4:22         ` Leo Yan [this message]
2018-01-10 22:48           ` Stephen Boyd
2018-01-17  7:58             ` Leo Yan
2018-01-05  9:04 ` [PATCH -next v2] clk: hisilicon: Using devm_ioremap_resource() instead of devm_ioremap() Wei Yongjun
2018-03-12 22:13 ` [PATCH -next] clk: hisilicon: hi3660:Fix potential NULL dereference in hi3660_stub_clk_probe() Stephen Boyd

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180108042235.GA21618@leoy-ThinkPad-T440 \
    --to=leo.yan@linaro.org \
    --cc=kevin.wangtao@hisilicon.com \
    --cc=linux-clk@vger.kernel.org \
    --cc=mturquette@baylibre.com \
    --cc=sboyd@codeaurora.org \
    --cc=wangruyi@huawei.com \
    --cc=weiyongjun1@huawei.com \
    --cc=yamada.masahiro@socionext.com \
    --cc=zhaokai1@hisilicon.com \
    --cc=zhongkaihua@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox