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

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/clk/linux

Pull clk fixes from Stephen Boyd:
"A couple clk driver fixes, a build fix, and a deadlock fix:

- Mediatek mt7988 has broken PCIe because the wrong parent is used

- Mediatek clk drivers may deadlock when registering their clks
because the clk provider device is repeatedly runtime PM resumed
and suspended during probe and clk registration.

Resuming the clk provider device deadlocks with an ABBA deadlock
due to genpd_lock and the clk prepare_lock. The fix is to keep the
device runtime resumed while registering clks.

- Another runtime PM related deadlock, this time with disabling
unused clks during late init.

We get an ABBA deadlock where a device is runtime PM resuming (or
suspending) while the disabling of unused clks is happening in
parallel. That runtime PM action calls into the clk framework and
tries to grab the clk prepare_lock while the disabling of unused
clks holds the prepare_lock and is waiting for that runtime PM
action to complete.

The fix is to runtime resume all the clk provider devices before
grabbing the clk prepare_lock during disable unused.

- A build fix to provide an empty devm_clk_rate_exclusive_get()
function when CONFIG_COMMON_CLK=n"

* tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/clk/linux:
clk: mediatek: mt7988-infracfg: fix clocks for 2nd PCIe port
clk: mediatek: Do a runtime PM get on controllers during probe
clk: Get runtime PM before walking tree for clk_summary
clk: Get runtime PM before walking tree during disable_unused
clk: Initialize struct clk_core kref earlier
clk: Don't hold prepare_lock when calling kref_put()
clk: Remove prepare_lock hold assertion in __clk_release()
clk: Provide !COMMON_CLK dummy for devm_clk_rate_exclusive_get()

+156 -39
+135 -38
drivers/clk/clk.c
··· 37 37 static HLIST_HEAD(clk_orphan_list); 38 38 static LIST_HEAD(clk_notifier_list); 39 39 40 + /* List of registered clks that use runtime PM */ 41 + static HLIST_HEAD(clk_rpm_list); 42 + static DEFINE_MUTEX(clk_rpm_list_lock); 43 + 40 44 static const struct hlist_head *all_lists[] = { 41 45 &clk_root_list, 42 46 &clk_orphan_list, ··· 63 59 struct clk_hw *hw; 64 60 struct module *owner; 65 61 struct device *dev; 62 + struct hlist_node rpm_node; 66 63 struct device_node *of_node; 67 64 struct clk_core *parent; 68 65 struct clk_parent_map *parents; ··· 125 120 return; 126 121 127 122 pm_runtime_put_sync(core->dev); 123 + } 124 + 125 + /** 126 + * clk_pm_runtime_get_all() - Runtime "get" all clk provider devices 127 + * 128 + * Call clk_pm_runtime_get() on all runtime PM enabled clks in the clk tree so 129 + * that disabling unused clks avoids a deadlock where a device is runtime PM 130 + * resuming/suspending and the runtime PM callback is trying to grab the 131 + * prepare_lock for something like clk_prepare_enable() while 132 + * clk_disable_unused_subtree() holds the prepare_lock and is trying to runtime 133 + * PM resume/suspend the device as well. 134 + * 135 + * Context: Acquires the 'clk_rpm_list_lock' and returns with the lock held on 136 + * success. Otherwise the lock is released on failure. 137 + * 138 + * Return: 0 on success, negative errno otherwise. 139 + */ 140 + static int clk_pm_runtime_get_all(void) 141 + { 142 + int ret; 143 + struct clk_core *core, *failed; 144 + 145 + /* 146 + * Grab the list lock to prevent any new clks from being registered 147 + * or unregistered until clk_pm_runtime_put_all(). 148 + */ 149 + mutex_lock(&clk_rpm_list_lock); 150 + 151 + /* 152 + * Runtime PM "get" all the devices that are needed for the clks 153 + * currently registered. Do this without holding the prepare_lock, to 154 + * avoid the deadlock. 155 + */ 156 + hlist_for_each_entry(core, &clk_rpm_list, rpm_node) { 157 + ret = clk_pm_runtime_get(core); 158 + if (ret) { 159 + failed = core; 160 + pr_err("clk: Failed to runtime PM get '%s' for clk '%s'\n", 161 + dev_name(failed->dev), failed->name); 162 + goto err; 163 + } 164 + } 165 + 166 + return 0; 167 + 168 + err: 169 + hlist_for_each_entry(core, &clk_rpm_list, rpm_node) { 170 + if (core == failed) 171 + break; 172 + 173 + clk_pm_runtime_put(core); 174 + } 175 + mutex_unlock(&clk_rpm_list_lock); 176 + 177 + return ret; 178 + } 179 + 180 + /** 181 + * clk_pm_runtime_put_all() - Runtime "put" all clk provider devices 182 + * 183 + * Put the runtime PM references taken in clk_pm_runtime_get_all() and release 184 + * the 'clk_rpm_list_lock'. 185 + */ 186 + static void clk_pm_runtime_put_all(void) 187 + { 188 + struct clk_core *core; 189 + 190 + hlist_for_each_entry(core, &clk_rpm_list, rpm_node) 191 + clk_pm_runtime_put(core); 192 + mutex_unlock(&clk_rpm_list_lock); 193 + } 194 + 195 + static void clk_pm_runtime_init(struct clk_core *core) 196 + { 197 + struct device *dev = core->dev; 198 + 199 + if (dev && pm_runtime_enabled(dev)) { 200 + core->rpm_enabled = true; 201 + 202 + mutex_lock(&clk_rpm_list_lock); 203 + hlist_add_head(&core->rpm_node, &clk_rpm_list); 204 + mutex_unlock(&clk_rpm_list_lock); 205 + } 128 206 } 129 207 130 208 /*** locking ***/ ··· 1469 1381 if (core->flags & CLK_IGNORE_UNUSED) 1470 1382 return; 1471 1383 1472 - if (clk_pm_runtime_get(core)) 1473 - return; 1474 - 1475 1384 if (clk_core_is_prepared(core)) { 1476 1385 trace_clk_unprepare(core); 1477 1386 if (core->ops->unprepare_unused) ··· 1477 1392 core->ops->unprepare(core->hw); 1478 1393 trace_clk_unprepare_complete(core); 1479 1394 } 1480 - 1481 - clk_pm_runtime_put(core); 1482 1395 } 1483 1396 1484 1397 static void __init clk_disable_unused_subtree(struct clk_core *core) ··· 1491 1408 1492 1409 if (core->flags & CLK_OPS_PARENT_ENABLE) 1493 1410 clk_core_prepare_enable(core->parent); 1494 - 1495 - if (clk_pm_runtime_get(core)) 1496 - goto unprepare_out; 1497 1411 1498 1412 flags = clk_enable_lock(); 1499 1413 ··· 1516 1436 1517 1437 unlock_out: 1518 1438 clk_enable_unlock(flags); 1519 - clk_pm_runtime_put(core); 1520 - unprepare_out: 1521 1439 if (core->flags & CLK_OPS_PARENT_ENABLE) 1522 1440 clk_core_disable_unprepare(core->parent); 1523 1441 } ··· 1531 1453 static int __init clk_disable_unused(void) 1532 1454 { 1533 1455 struct clk_core *core; 1456 + int ret; 1534 1457 1535 1458 if (clk_ignore_unused) { 1536 1459 pr_warn("clk: Not disabling unused clocks\n"); ··· 1540 1461 1541 1462 pr_info("clk: Disabling unused clocks\n"); 1542 1463 1464 + ret = clk_pm_runtime_get_all(); 1465 + if (ret) 1466 + return ret; 1467 + /* 1468 + * Grab the prepare lock to keep the clk topology stable while iterating 1469 + * over clks. 1470 + */ 1543 1471 clk_prepare_lock(); 1544 1472 1545 1473 hlist_for_each_entry(core, &clk_root_list, child_node) ··· 1562 1476 clk_unprepare_unused_subtree(core); 1563 1477 1564 1478 clk_prepare_unlock(); 1479 + 1480 + clk_pm_runtime_put_all(); 1565 1481 1566 1482 return 0; 1567 1483 } ··· 3340 3252 { 3341 3253 struct clk_core *child; 3342 3254 3343 - clk_pm_runtime_get(c); 3344 3255 clk_summary_show_one(s, c, level); 3345 - clk_pm_runtime_put(c); 3346 3256 3347 3257 hlist_for_each_entry(child, &c->children, child_node) 3348 3258 clk_summary_show_subtree(s, child, level + 1); ··· 3350 3264 { 3351 3265 struct clk_core *c; 3352 3266 struct hlist_head **lists = s->private; 3267 + int ret; 3353 3268 3354 3269 seq_puts(s, " enable prepare protect duty hardware connection\n"); 3355 3270 seq_puts(s, " clock count count count rate accuracy phase cycle enable consumer id\n"); 3356 3271 seq_puts(s, "---------------------------------------------------------------------------------------------------------------------------------------------\n"); 3357 3272 3273 + ret = clk_pm_runtime_get_all(); 3274 + if (ret) 3275 + return ret; 3358 3276 3359 3277 clk_prepare_lock(); 3360 3278 ··· 3367 3277 clk_summary_show_subtree(s, c, 0); 3368 3278 3369 3279 clk_prepare_unlock(); 3280 + clk_pm_runtime_put_all(); 3370 3281 3371 3282 return 0; 3372 3283 } ··· 3415 3324 struct clk_core *c; 3416 3325 bool first_node = true; 3417 3326 struct hlist_head **lists = s->private; 3327 + int ret; 3328 + 3329 + ret = clk_pm_runtime_get_all(); 3330 + if (ret) 3331 + return ret; 3418 3332 3419 3333 seq_putc(s, '{'); 3334 + 3420 3335 clk_prepare_lock(); 3421 3336 3422 3337 for (; *lists; lists++) { ··· 3435 3338 } 3436 3339 3437 3340 clk_prepare_unlock(); 3341 + clk_pm_runtime_put_all(); 3438 3342 3439 3343 seq_puts(s, "}\n"); 3440 3344 return 0; ··· 4079 3981 } 4080 3982 4081 3983 clk_core_reparent_orphans_nolock(); 4082 - 4083 - kref_init(&core->ref); 4084 3984 out: 4085 3985 clk_pm_runtime_put(core); 4086 3986 unlock: ··· 4307 4211 kfree(core->parents); 4308 4212 } 4309 4213 4214 + /* Free memory allocated for a struct clk_core */ 4215 + static void __clk_release(struct kref *ref) 4216 + { 4217 + struct clk_core *core = container_of(ref, struct clk_core, ref); 4218 + 4219 + if (core->rpm_enabled) { 4220 + mutex_lock(&clk_rpm_list_lock); 4221 + hlist_del(&core->rpm_node); 4222 + mutex_unlock(&clk_rpm_list_lock); 4223 + } 4224 + 4225 + clk_core_free_parent_map(core); 4226 + kfree_const(core->name); 4227 + kfree(core); 4228 + } 4229 + 4310 4230 static struct clk * 4311 4231 __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw) 4312 4232 { ··· 4343 4231 goto fail_out; 4344 4232 } 4345 4233 4234 + kref_init(&core->ref); 4235 + 4346 4236 core->name = kstrdup_const(init->name, GFP_KERNEL); 4347 4237 if (!core->name) { 4348 4238 ret = -ENOMEM; ··· 4357 4243 } 4358 4244 core->ops = init->ops; 4359 4245 4360 - if (dev && pm_runtime_enabled(dev)) 4361 - core->rpm_enabled = true; 4362 4246 core->dev = dev; 4247 + clk_pm_runtime_init(core); 4363 4248 core->of_node = np; 4364 4249 if (dev && dev->driver) 4365 4250 core->owner = dev->driver->owner; ··· 4398 4285 hw->clk = NULL; 4399 4286 4400 4287 fail_create_clk: 4401 - clk_core_free_parent_map(core); 4402 4288 fail_parents: 4403 4289 fail_ops: 4404 - kfree_const(core->name); 4405 4290 fail_name: 4406 - kfree(core); 4291 + kref_put(&core->ref, __clk_release); 4407 4292 fail_out: 4408 4293 return ERR_PTR(ret); 4409 4294 } ··· 4480 4369 return PTR_ERR_OR_ZERO(__clk_register(NULL, node, hw)); 4481 4370 } 4482 4371 EXPORT_SYMBOL_GPL(of_clk_hw_register); 4483 - 4484 - /* Free memory allocated for a clock. */ 4485 - static void __clk_release(struct kref *ref) 4486 - { 4487 - struct clk_core *core = container_of(ref, struct clk_core, ref); 4488 - 4489 - lockdep_assert_held(&prepare_lock); 4490 - 4491 - clk_core_free_parent_map(core); 4492 - kfree_const(core->name); 4493 - kfree(core); 4494 - } 4495 4372 4496 4373 /* 4497 4374 * Empty clk_ops for unregistered clocks. These are used temporarily ··· 4571 4472 if (ops == &clk_nodrv_ops) { 4572 4473 pr_err("%s: unregistered clock: %s\n", __func__, 4573 4474 clk->core->name); 4574 - goto unlock; 4475 + clk_prepare_unlock(); 4476 + return; 4575 4477 } 4576 4478 /* 4577 4479 * Assign empty clock ops for consumers that might still hold ··· 4606 4506 if (clk->core->protect_count) 4607 4507 pr_warn("%s: unregistering protected clock: %s\n", 4608 4508 __func__, clk->core->name); 4509 + clk_prepare_unlock(); 4609 4510 4610 4511 kref_put(&clk->core->ref, __clk_release); 4611 4512 free_clk(clk); 4612 - unlock: 4613 - clk_prepare_unlock(); 4614 4513 } 4615 4514 EXPORT_SYMBOL_GPL(clk_unregister); 4616 4515 ··· 4768 4669 if (clk->min_rate > 0 || clk->max_rate < ULONG_MAX) 4769 4670 clk_set_rate_range_nolock(clk, 0, ULONG_MAX); 4770 4671 4771 - owner = clk->core->owner; 4772 - kref_put(&clk->core->ref, __clk_release); 4773 - 4774 4672 clk_prepare_unlock(); 4775 4673 4674 + owner = clk->core->owner; 4675 + kref_put(&clk->core->ref, __clk_release); 4776 4676 module_put(owner); 4777 - 4778 4677 free_clk(clk); 4779 4678 } 4780 4679
+1 -1
drivers/clk/mediatek/clk-mt7988-infracfg.c
··· 156 156 GATE_INFRA0(CLK_INFRA_PCIE_PERI_26M_CK_P1, "infra_pcie_peri_ck_26m_ck_p1", 157 157 "csw_infra_f26m_sel", 8), 158 158 GATE_INFRA0(CLK_INFRA_PCIE_PERI_26M_CK_P2, "infra_pcie_peri_ck_26m_ck_p2", 159 - "csw_infra_f26m_sel", 9), 159 + "infra_pcie_peri_ck_26m_ck_p3", 9), 160 160 GATE_INFRA0(CLK_INFRA_PCIE_PERI_26M_CK_P3, "infra_pcie_peri_ck_26m_ck_p3", 161 161 "csw_infra_f26m_sel", 10), 162 162 /* INFRA1 */
+15
drivers/clk/mediatek/clk-mtk.c
··· 13 13 #include <linux/of.h> 14 14 #include <linux/of_address.h> 15 15 #include <linux/platform_device.h> 16 + #include <linux/pm_runtime.h> 16 17 #include <linux/slab.h> 17 18 18 19 #include "clk-mtk.h" ··· 495 494 return IS_ERR(base) ? PTR_ERR(base) : -ENOMEM; 496 495 } 497 496 497 + 498 + devm_pm_runtime_enable(&pdev->dev); 499 + /* 500 + * Do a pm_runtime_resume_and_get() to workaround a possible 501 + * deadlock between clk_register() and the genpd framework. 502 + */ 503 + r = pm_runtime_resume_and_get(&pdev->dev); 504 + if (r) 505 + return r; 506 + 498 507 /* Calculate how many clk_hw_onecell_data entries to allocate */ 499 508 num_clks = mcd->num_clks + mcd->num_composite_clks; 500 509 num_clks += mcd->num_fixed_clks + mcd->num_factor_clks; ··· 585 574 goto unregister_clks; 586 575 } 587 576 577 + pm_runtime_put(&pdev->dev); 578 + 588 579 return r; 589 580 590 581 unregister_clks: ··· 617 604 free_base: 618 605 if (mcd->shared_io && base) 619 606 iounmap(base); 607 + 608 + pm_runtime_put(&pdev->dev); 620 609 return r; 621 610 } 622 611
+5
include/linux/clk.h
··· 286 286 return 0; 287 287 } 288 288 289 + static inline int devm_clk_rate_exclusive_get(struct device *dev, struct clk *clk) 290 + { 291 + return 0; 292 + } 293 + 289 294 static inline void clk_rate_exclusive_put(struct clk *clk) {} 290 295 291 296 #endif