Skip to contentSkip to conversation

cpufreq: schedutil: Fix tunables_hook cleanup on init failure

PATCH In review Kaushlendra Kumar 2 participants 5 messages Nov 25, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpufreq: schedutil: Fix tunables_hook cleanup on init failure

If sugov_init() fails after registering the tunables_hook to the global list, the hook is not removed before freeing the tunables structure via kobject_put(). This leaves a dangling list entry pointing to freed memory.

Remove the hook from the list in the error path to prevent this.

---
 kernel/sched/cpufreq_schedutil.c | 1 +
 1 file changed, 1 insertion(+)
kernel/sched/cpufreq_schedutil.c +1 -0
index 9fdbc7a6063d..165c58a51a83 100644
@@ -800,6 +800,7 @@ static int sugov_init(struct cpufreq_policy *policy)
 	return 0;
 
 fail:
+	list_del(&sg_policy->tunables_hook);
 	kobject_put(&tunables->attr_set.kobj);
 	policy->governor_data = NULL;
 	sugov_clear_global_tunables();
Signature · 1 line
2.34.1
#1 lore raw
Quoted 5 lines · Kaushlendra Kumar

If sugov_init() fails after registering the tunables_hook to the global list, the hook is not removed before freeing the tunables structure via kobject_put(). This leaves a dangling list entry pointing to freed memory.

Except that sugov_init() cannot fail after calling gov_attr_set_get(), so how can it get to the error path with tunables_hook in the list in the first place?

Quoted 1 line

Remove the hook from the list in the error path to prevent this.

There is no bug to fix here AFAICS.

Quoted 10 lines
---
 kernel/sched/cpufreq_schedutil.c | 1 +
 1 file changed, 1 insertion(+)
kernel/sched/cpufreq_schedutil.c +0 -0
index 9fdbc7a6063d..165c58a51a83 100644

kernel/sched/cpufreq_schedutil.c static int sugov_init(struct cpufreq_policy *policy) · line 803

Quoted 4 lines
kernel/sched/cpufreq_schedutil.c +1 -0
@@ -800,6 +800,7 @@ static int sugov_init(struct cpufreq_policy *policy)
        return 0;
 
 fail:
+       list_del(&sg_policy->tunables_hook);

And this change is invalid.

Quoted 4 lines
kernel/sched/cpufreq_schedutil.c +0 -1
        kobject_put(&tunables->attr_set.kobj);
        policy->governor_data = NULL;
        sugov_clear_global_tunables();
--
#2 lore raw

Hi Rafael,

Quoted 8 lines · Rafael J. Wysocki
Quoted 5 lines · Kaushlendra Kumar

If sugov_init() fails after registering the tunables_hook to the global list, the hook is not removed before freeing the tunables structure via kobject_put(). This leaves a dangling list entry pointing to freed memory.

Quoted 8 lines

Except that sugov_init() cannot fail after calling gov_attr_set_get(), so how can it get to the error path with tunables_hook in the list in the first place?

Quoted 1 line

Remove the hook from the list in the error path to prevent this.

There is no bug to fix here AFAICS.

In function sugov_tunables_alloc() tunables_hook is getting added. tunables = sugov_tunables_alloc(sg_policy);

Thanks for the review.

Regards,
Kaushlendra

#3 lore raw
Quoted 17 lines · Kaushlendra

Hi Rafael,

Quoted 13 lines · Rafael J. Wysocki
Quoted 5 lines · Kaushlendra Kumar

If sugov_init() fails after registering the tunables_hook to the global list, the hook is not removed before freeing the tunables structure via kobject_put(). This leaves a dangling list entry pointing to freed memory.

Except that sugov_init() cannot fail after calling gov_attr_set_get(), so how can it get to the error path with tunables_hook in the list in the first place?

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Remove the hook from the list in the error path to prevent this.

There is no bug to fix here AFAICS.

In function sugov_tunables_alloc() tunables_hook is getting added. tunables = sugov_tunables_alloc(sg_policy);

Yes, which adds tunables_hook in sg_policy to attr_set in tunables. Both tunables and sg_policy are freed in the error path in question.

Where's the bug being fixed then?

#4 lore raw

Hi Rafael,

Quoted 10 lines · Rafael J. Wysocki
Quoted 3 lines · Kaushlendra

In function sugov_tunables_alloc() tunables_hook is getting added. tunables = sugov_tunables_alloc(sg_policy);

Yes, which adds tunables_hook in sg_policy to attr_set in tunables. Both tunables and sg_policy are freed in the error path in question.

Where's the bug being fixed then?

Agreed. Since both structures are freed, there's no dangling pointer issue. I misunderstood the lifecycle. There is no bug here.

Please disregard this patch.

Regards,
Kaushlendra