Re: drivers/rtc/rtc-mxc_v2.c:361 mxc_rtc_probe() warn: 'pdata->clk' not released on lines: 341,354,361.

From: Alexandre Belloni
Date: Fri May 28 2021 - 18:22:51 EST


Hello,

On 25/05/2021 16:16:15+0300, Dan Carpenter wrote:
> Hi Alexandre,
>
> First bad commit (maybe != root cause):
>
> tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> head: a050a6d2b7e80ca52b2f4141eaf3420d201b72b3
> commit: 0020868f2a7037e87d6b3b196526de2fb885830d rtc: mxc{,_v2}: enable COMPILE_TEST
> config: microblaze-randconfig-m031-20210525 (attached as .config)
> compiler: microblaze-linux-gcc (GCC) 9.3.0
>
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kernel test robot <lkp@xxxxxxxxx>
> Reported-by: Dan Carpenter <dan.carpenter@xxxxxxxxxx>
>
> smatch warnings:
> drivers/rtc/rtc-mxc_v2.c:361 mxc_rtc_probe() warn: 'pdata->clk' not released on lines: 341,354,361.
>
> vim +361 drivers/rtc/rtc-mxc_v2.c
>
> 83c880f79e88cc Patrick Bruenn 2017-12-18 279 static int mxc_rtc_probe(struct platform_device *pdev)
> 83c880f79e88cc Patrick Bruenn 2017-12-18 280 {
> 83c880f79e88cc Patrick Bruenn 2017-12-18 281 struct mxc_rtc_data *pdata;
> 83c880f79e88cc Patrick Bruenn 2017-12-18 282 void __iomem *ioaddr;
> 83c880f79e88cc Patrick Bruenn 2017-12-18 283 int ret = 0;
> 83c880f79e88cc Patrick Bruenn 2017-12-18 284
> 83c880f79e88cc Patrick Bruenn 2017-12-18 285 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 286 if (!pdata)
> 83c880f79e88cc Patrick Bruenn 2017-12-18 287 return -ENOMEM;
> 83c880f79e88cc Patrick Bruenn 2017-12-18 288
> 874532cdeefefa Anson Huang 2019-07-17 289 pdata->ioaddr = devm_platform_ioremap_resource(pdev, 0);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 290 if (IS_ERR(pdata->ioaddr))
> 83c880f79e88cc Patrick Bruenn 2017-12-18 291 return PTR_ERR(pdata->ioaddr);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 292
> 83c880f79e88cc Patrick Bruenn 2017-12-18 293 ioaddr = pdata->ioaddr;
> 83c880f79e88cc Patrick Bruenn 2017-12-18 294
> 83c880f79e88cc Patrick Bruenn 2017-12-18 295 pdata->clk = devm_clk_get(&pdev->dev, NULL);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 296 if (IS_ERR(pdata->clk)) {
> 83c880f79e88cc Patrick Bruenn 2017-12-18 297 dev_err(&pdev->dev, "unable to get rtc clock!\n");
> 83c880f79e88cc Patrick Bruenn 2017-12-18 298 return PTR_ERR(pdata->clk);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 299 }
> 83c880f79e88cc Patrick Bruenn 2017-12-18 300
> 83c880f79e88cc Patrick Bruenn 2017-12-18 301 spin_lock_init(&pdata->lock);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 302 pdata->irq = platform_get_irq(pdev, 0);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 303 if (pdata->irq < 0)
> 83c880f79e88cc Patrick Bruenn 2017-12-18 304 return pdata->irq;
> 83c880f79e88cc Patrick Bruenn 2017-12-18 305
> 83c880f79e88cc Patrick Bruenn 2017-12-18 306 device_init_wakeup(&pdev->dev, 1);
> fbc5ee9a6955e6 Anson Huang 2019-04-11 307 ret = dev_pm_set_wake_irq(&pdev->dev, pdata->irq);
> fbc5ee9a6955e6 Anson Huang 2019-04-11 308 if (ret)
> fbc5ee9a6955e6 Anson Huang 2019-04-11 309 dev_err(&pdev->dev, "failed to enable irq wake\n");
> 83c880f79e88cc Patrick Bruenn 2017-12-18 310
> 83c880f79e88cc Patrick Bruenn 2017-12-18 311 ret = clk_prepare_enable(pdata->clk);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 312 if (ret)
> 83c880f79e88cc Patrick Bruenn 2017-12-18 313 return ret;
> 83c880f79e88cc Patrick Bruenn 2017-12-18 314 /* initialize glitch detect */
> 83c880f79e88cc Patrick Bruenn 2017-12-18 315 writel(SRTC_LPPDR_INIT, ioaddr + SRTC_LPPDR);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 316
> 83c880f79e88cc Patrick Bruenn 2017-12-18 317 /* clear lp interrupt status */
> 83c880f79e88cc Patrick Bruenn 2017-12-18 318 writel(0xFFFFFFFF, ioaddr + SRTC_LPSR);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 319
> 83c880f79e88cc Patrick Bruenn 2017-12-18 320 /* move out of init state */
> 83c880f79e88cc Patrick Bruenn 2017-12-18 321 writel((SRTC_LPCR_IE | SRTC_LPCR_NSA), ioaddr + SRTC_LPCR);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 322 ret = mxc_rtc_wait_for_flag(ioaddr + SRTC_LPSR, SRTC_LPSR_IES);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 323 if (ret) {
> 83c880f79e88cc Patrick Bruenn 2017-12-18 324 dev_err(&pdev->dev, "Timeout waiting for SRTC_LPSR_IES\n");
> 83c880f79e88cc Patrick Bruenn 2017-12-18 325 clk_disable_unprepare(pdata->clk);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 326 return ret;
> 83c880f79e88cc Patrick Bruenn 2017-12-18 327 }
> 83c880f79e88cc Patrick Bruenn 2017-12-18 328
> 83c880f79e88cc Patrick Bruenn 2017-12-18 329 /* move out of non-valid state */
> 83c880f79e88cc Patrick Bruenn 2017-12-18 330 writel((SRTC_LPCR_IE | SRTC_LPCR_NVE | SRTC_LPCR_NSA |
> 83c880f79e88cc Patrick Bruenn 2017-12-18 331 SRTC_LPCR_EN_LP), ioaddr + SRTC_LPCR);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 332 ret = mxc_rtc_wait_for_flag(ioaddr + SRTC_LPSR, SRTC_LPSR_NVES);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 333 if (ret) {
> 83c880f79e88cc Patrick Bruenn 2017-12-18 334 dev_err(&pdev->dev, "Timeout waiting for SRTC_LPSR_NVES\n");
> 83c880f79e88cc Patrick Bruenn 2017-12-18 335 clk_disable_unprepare(pdata->clk);
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> 83c880f79e88cc Patrick Bruenn 2017-12-18 336 return ret;
> 83c880f79e88cc Patrick Bruenn 2017-12-18 337 }
> 83c880f79e88cc Patrick Bruenn 2017-12-18 338
> 5490a1e018a4b4 Alexandre Belloni 2018-05-19 339 pdata->rtc = devm_rtc_allocate_device(&pdev->dev);
> 5490a1e018a4b4 Alexandre Belloni 2018-05-19 340 if (IS_ERR(pdata->rtc))
> 5490a1e018a4b4 Alexandre Belloni 2018-05-19 341 return PTR_ERR(pdata->rtc);
>
> clk_disable_unprepare(pdata->clk);

That one needs to be fixed.

>
> 5490a1e018a4b4 Alexandre Belloni 2018-05-19 342
> 5490a1e018a4b4 Alexandre Belloni 2018-05-19 343 pdata->rtc->ops = &mxc_rtc_ops;
> 95fbfa14b431d4 Alexandre Belloni 2018-05-19 344 pdata->rtc->range_max = U32_MAX;
> 5490a1e018a4b4 Alexandre Belloni 2018-05-19 345
> 83c880f79e88cc Patrick Bruenn 2017-12-18 346 clk_disable(pdata->clk);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 347 platform_set_drvdata(pdev, pdata);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 348 ret =
> 83c880f79e88cc Patrick Bruenn 2017-12-18 349 devm_request_irq(&pdev->dev, pdata->irq, mxc_rtc_interrupt, 0,
> 83c880f79e88cc Patrick Bruenn 2017-12-18 350 pdev->name, &pdev->dev);
> 83c880f79e88cc Patrick Bruenn 2017-12-18 351 if (ret < 0) {
> 83c880f79e88cc Patrick Bruenn 2017-12-18 352 dev_err(&pdev->dev, "interrupt not available.\n");
> 83c880f79e88cc Patrick Bruenn 2017-12-18 353 clk_unprepare(pdata->clk);
>
> Should these be clk_disable_unprepare()? I don't know. Please tell
> me the rules on this if the warning is wrong. I also haven't looked up
> exactly why this warning is displayed, I would have expected
> clk_unprepare() to silence it.

clk_unprepare here is right because there is an unconditional
clk_disable on line 346. I guess smatch needs to learn that clk_disable
followed by clk_unprepare is the same as clk_disable_unprepare and I
guess also for clk_prepare_enable/clk_prepare/clk_enable

>
> 83c880f79e88cc Patrick Bruenn 2017-12-18 354 return ret;
> 83c880f79e88cc Patrick Bruenn 2017-12-18 355 }
> 83c880f79e88cc Patrick Bruenn 2017-12-18 356
> fdcfd854333be5 Bartosz Golaszewski 2020-11-09 357 ret = devm_rtc_register_device(pdata->rtc);
> 5490a1e018a4b4 Alexandre Belloni 2018-05-19 358 if (ret < 0)
> 83c880f79e88cc Patrick Bruenn 2017-12-18 359 clk_unprepare(pdata->clk);
>
> Same.
>

Ditto

> 83c880f79e88cc Patrick Bruenn 2017-12-18 360
> 5490a1e018a4b4 Alexandre Belloni 2018-05-19 @361 return ret;
> 83c880f79e88cc Patrick Bruenn 2017-12-18 362 }
>

--
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com