Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cpufreq: qcom-hw: drop devm_xxx() calls from init/exit hooks

Commit f17b3e44320b ("cpufreq: qcom-hw: Use
devm_platform_ioremap_resource() to simplify code") introduces
a regression on platforms using the driver, by failing to initialise
a policy, when one is created post hotplug.

When all the CPUs of a policy are hoptplugged out, the call to .exit()
and later to devm_iounmap() does not release the memory region that was
requested during devm_platform_ioremap_resource(). Therefore,
a subsequent call to .init() will result in the following error, which
will prevent a new policy to be initialised:

[ 3395.915416] CPU4: shutdown
[ 3395.938185] psci: CPU4 killed (polled 0 ms)
[ 3399.071424] CPU5: shutdown
[ 3399.094316] psci: CPU5 killed (polled 0 ms)
[ 3402.139358] CPU6: shutdown
[ 3402.161705] psci: CPU6 killed (polled 0 ms)
[ 3404.742939] CPU7: shutdown
[ 3404.765592] psci: CPU7 killed (polled 0 ms)
[ 3411.492274] Detected VIPT I-cache on CPU4
[ 3411.492337] GICv3: CPU4: found redistributor 400 region 0:0x0000000017ae0000
[ 3411.492448] CPU4: Booted secondary processor 0x0000000400 [0x516f802d]
[ 3411.503654] qcom-cpufreq-hw 17d43000.cpufreq: can't request region for resource [mem 0x17d45800-0x17d46bff]

With that being said, the original code was tricky and skipping memory
region request intentionally to hide this issue. The true cause is that
those devm_xxx() device managed functions shouldn't be used for cpufreq
init/exit hooks, because &pdev->dev is alive across the hooks and will
not trigger auto resource free-up. Let's drop the use of device managed
functions and manually allocate/free resources, so that the issue can be
fixed properly.

Cc: v5.10+ <stable@vger.kernel.org> # v5.10+
Fixes: f17b3e44320b ("cpufreq: qcom-hw: Use devm_platform_ioremap_resource() to simplify code")
Suggested-by: Bjorn Andersson <bjorn.andersson@linaro.org>
Signed-off-by: Shawn Guo <shawn.guo@linaro.org>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>

authored by

Shawn Guo and committed by
Viresh Kumar
67fc209b 7114ebff

+32 -8
+32 -8
drivers/cpufreq/qcom-cpufreq-hw.c
··· 32 32 33 33 struct qcom_cpufreq_data { 34 34 void __iomem *base; 35 + struct resource *res; 35 36 const struct qcom_cpufreq_soc_data *soc_data; 36 37 }; 37 38 ··· 281 280 struct of_phandle_args args; 282 281 struct device_node *cpu_np; 283 282 struct device *cpu_dev; 283 + struct resource *res; 284 284 void __iomem *base; 285 285 struct qcom_cpufreq_data *data; 286 286 int ret, index; ··· 305 303 306 304 index = args.args[0]; 307 305 308 - base = devm_platform_ioremap_resource(pdev, index); 309 - if (IS_ERR(base)) 310 - return PTR_ERR(base); 306 + res = platform_get_resource(pdev, IORESOURCE_MEM, index); 307 + if (!res) { 308 + dev_err(dev, "failed to get mem resource %d\n", index); 309 + return -ENODEV; 310 + } 311 311 312 - data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 312 + if (!request_mem_region(res->start, resource_size(res), res->name)) { 313 + dev_err(dev, "failed to request resource %pR\n", res); 314 + return -EBUSY; 315 + } 316 + 317 + base = ioremap(res->start, resource_size(res)); 318 + if (IS_ERR(base)) { 319 + dev_err(dev, "failed to map resource %pR\n", res); 320 + ret = PTR_ERR(base); 321 + goto release_region; 322 + } 323 + 324 + data = kzalloc(sizeof(*data), GFP_KERNEL); 313 325 if (!data) { 314 326 ret = -ENOMEM; 315 - goto error; 327 + goto unmap_base; 316 328 } 317 329 318 330 data->soc_data = of_device_get_match_data(&pdev->dev); 319 331 data->base = base; 332 + data->res = res; 320 333 321 334 /* HW should be in enabled state to proceed */ 322 335 if (!(readl_relaxed(base + data->soc_data->reg_enable) & 0x1)) { ··· 372 355 373 356 return 0; 374 357 error: 375 - devm_iounmap(dev, base); 358 + kfree(data); 359 + unmap_base: 360 + iounmap(data->base); 361 + release_region: 362 + release_mem_region(res->start, resource_size(res)); 376 363 return ret; 377 364 } 378 365 ··· 384 363 { 385 364 struct device *cpu_dev = get_cpu_device(policy->cpu); 386 365 struct qcom_cpufreq_data *data = policy->driver_data; 387 - struct platform_device *pdev = cpufreq_get_driver_data(); 366 + struct resource *res = data->res; 367 + void __iomem *base = data->base; 388 368 389 369 dev_pm_opp_remove_all_dynamic(cpu_dev); 390 370 dev_pm_opp_of_cpumask_remove_table(policy->related_cpus); 391 371 kfree(policy->freq_table); 392 - devm_iounmap(&pdev->dev, data->base); 372 + kfree(data); 373 + iounmap(base); 374 + release_mem_region(res->start, resource_size(res)); 393 375 394 376 return 0; 395 377 }