[PATCH] clk: disable unused clocks registered after boot

From: Alexandre Belloni

Date: Tue Aug 25 2026 - 16:54:06 EST


clk_disable_unused() only runs once as a late_initcall, so clocks
registered afterwards by loading a module are never checked and may be left
running needlessly.

So record when clk_disable_unused has run and afterwards, schedule a
delayed work to run the scan when new clocks are registered.

Signed-off-by: Alexandre Belloni <alexandre.belloni@xxxxxxxxxxx>
---
drivers/clk/clk.c | 50 +++++++++++++++++++++++++++++++++++------------
1 file changed, 37 insertions(+), 13 deletions(-)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 048adfa86a5d..817f916192ac 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -24,6 +24,7 @@
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/stringhash.h>
+#include <linux/workqueue.h>

#include "clk.h"

@@ -47,6 +48,10 @@ static LIST_HEAD(clk_notifier_list);
static HLIST_HEAD(clk_rpm_list);
static DEFINE_MUTEX(clk_rpm_list_lock);

+static bool clk_disable_unused_done;
+
+#define CLK_DISABLE_UNUSED_DELAY (HZ / 2)
+
static const struct hlist_head *all_lists[] = {
&clk_root_list,
&clk_orphan_list,
@@ -1442,7 +1447,7 @@ static void clk_core_disable_unprepare(struct clk_core *core)
clk_core_unprepare_lock(core);
}

-static void __init clk_unprepare_unused_subtree(struct clk_core *core)
+static void clk_unprepare_unused_subtree(struct clk_core *core)
{
struct clk_core *child;

@@ -1467,7 +1472,7 @@ static void __init clk_unprepare_unused_subtree(struct clk_core *core)
}
}

-static void __init clk_disable_unused_subtree(struct clk_core *core)
+static void clk_disable_unused_subtree(struct clk_core *core)
{
struct clk_core *child;
unsigned long flags;
@@ -1516,21 +1521,14 @@ static int __init clk_ignore_unused_setup(char *__unused)
}
__setup("clk_ignore_unused", clk_ignore_unused_setup);

-static int __init clk_disable_unused(void)
+static void clk_disable_unused(void)
{
struct clk_core *core;
int ret;

- if (clk_ignore_unused) {
- pr_warn("clk: Not disabling unused clocks\n");
- return 0;
- }
-
- pr_info("clk: Disabling unused clocks\n");
-
ret = clk_pm_runtime_get_all();
if (ret)
- return ret;
+ return;
/*
* Grab the prepare lock to keep the clk topology stable while iterating
* over clks.
@@ -1552,10 +1550,31 @@ static int __init clk_disable_unused(void)
clk_prepare_unlock();

clk_pm_runtime_put_all();
+}
+
+static void clk_disable_unused_workfn(struct work_struct *work)
+{
+ clk_disable_unused();
+}
+
+static DECLARE_DELAYED_WORK(clk_disable_unused_work, clk_disable_unused_workfn);
+
+static int __init clk_disable_unused_init(void)
+{
+ if (clk_ignore_unused) {
+ pr_warn("clk: Not disabling unused clocks\n");
+ return 0;
+ }
+
+ pr_info("clk: Disabling unused clocks\n");
+
+ clk_disable_unused();
+
+ WRITE_ONCE(clk_disable_unused_done, true);

return 0;
}
-late_initcall_sync(clk_disable_unused);
+late_initcall_sync(clk_disable_unused_init);

static int clk_core_determine_round_nolock(struct clk_core *core,
struct clk_rate_request *req)
@@ -4366,8 +4385,13 @@ __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
clk_core_link_consumer(core, hw->clk);

ret = __clk_core_init(core);
- if (!ret)
+ if (!ret) {
+ if (READ_ONCE(clk_disable_unused_done))
+ mod_delayed_work(system_power_efficient_wq,
+ &clk_disable_unused_work,
+ CLK_DISABLE_UNUSED_DELAY);
return hw->clk;
+ }

clk_prepare_lock();
clk_core_unlink_consumer(hw->clk);
--
2.55.0


--B1kxVD8HOxRY8rij--