Skip to contentSkip to conversation

cpufreq: Skip scheduling when update work is pending

PATCH In review Kaushlendra Kumar 2 participants 3 messages Nov 18, 2025 CPU frequency scaling framework

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpufreq: Skip scheduling when update work is pending

Avoid scheduling cpufreq policy update work when it's already pending by checking work_pending() before schedule_work(). This reduces unnecessary work queue churn and scheduling overhead under frequent notifier events.

---
 drivers/cpufreq/cpufreq.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/cpufreq/cpufreq.c +5 -3
index d90dcffac953..49300cedd8d0 100644
@@ -1222,7 +1222,8 @@ static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq,
 {
 	struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min);
 
-	schedule_work(&policy->update);
+	if (!work_pending(&policy->update))
+		schedule_work(&policy->update);
 	return 0;
 }
 
@@ -1231,7 +1232,8 @@ static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq,
 {
 	struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max);
 
-	schedule_work(&policy->update);
+	if (!work_pending(&policy->update))
+		schedule_work(&policy->update);
 	return 0;
 }
 
@@ -1841,7 +1843,7 @@ static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, b
 			return policy->cur;
 
 		cpufreq_out_of_sync(policy, new_freq);
-		if (update)
+		if (update && !work_pending(&policy->update))
 			schedule_work(&policy->update);
 	}
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 16 lines · Kaushlendra Kumar

Avoid scheduling cpufreq policy update work when it's already pending by checking work_pending() before schedule_work(). This reduces unnecessary work queue churn and scheduling overhead under frequent notifier events.

---
 drivers/cpufreq/cpufreq.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/cpufreq/cpufreq.c +0 -0
index d90dcffac953..49300cedd8d0 100644

drivers/cpufreq/cpufreq.c static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq, · line 1226

Quoted 6 lines
drivers/cpufreq/cpufreq.c +2 -1
@@ -1222,7 +1222,8 @@ static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq,
 {
        struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min);
 
-       schedule_work(&policy->update);
+       if (!work_pending(&policy->update))
+               schedule_work(&policy->update);

This duplicates the test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) check in queue_work_on(). Why exactly?

drivers/cpufreq/cpufreq.c static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq, · line 1236

Quoted 10 lines
drivers/cpufreq/cpufreq.c +2 -1
        return 0;
 }
 
@@ -1231,7 +1232,8 @@ static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq,
 {
        struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max);
 
-       schedule_work(&policy->update);
+       if (!work_pending(&policy->update))
+               schedule_work(&policy->update);

And same here.

Quoted 13 lines
drivers/cpufreq/cpufreq.c +1 -2
        return 0;
 }
 
@@ -1841,7 +1843,7 @@ static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, b
                        return policy->cur;
 
                cpufreq_out_of_sync(policy, new_freq);
-               if (update)
+               if (update && !work_pending(&policy->update))
                        schedule_work(&policy->update);
        }
 
--
#2 lore raw
Quoted 2 lines

This duplicates the test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) check in queue_work_on(). Why exactly?

You're right — the work_pending() guards are redundant. schedule_work() / queue_work_on() already perform the atomic test‑and‑set internally.
Thanks for the review.

BR,
Kaushlendra