Skip to content

Commit d33cc39

Browse files
sumeet4linuxgregkh
authored andcommitted
powercap: fix race condition in register_control_type()
[ Upstream commit 7bda191 ] The device becomes visible to userspace via device_register() even before it fully initialized by idr_init(). If userspace or another thread tries to register a zone immediately after device_register(), the control_type_valid() will fail because the control_type is not yet in the list. The IDR is not yet initialized, so this race condition causes zone registration failure. Move idr_init() and list addition before device_register() fix the race condition. Signed-off-by: Sumeet Pawnikar <sumeet4linux@gmail.com> [ rjw: Subject adjustment, empty line added ] Link: https://patch.msgid.link/20251205190216.5032-1-sumeet4linux@gmail.com Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent cbbf6c1 commit d33cc39

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

drivers/powercap/powercap_sys.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -625,17 +625,23 @@ struct powercap_control_type *powercap_register_control_type(
625625
INIT_LIST_HEAD(&control_type->node);
626626
control_type->dev.class = &powercap_class;
627627
dev_set_name(&control_type->dev, "%s", name);
628-
result = device_register(&control_type->dev);
629-
if (result) {
630-
put_device(&control_type->dev);
631-
return ERR_PTR(result);
632-
}
633628
idr_init(&control_type->idr);
634629

635630
mutex_lock(&powercap_cntrl_list_lock);
636631
list_add_tail(&control_type->node, &powercap_cntrl_list);
637632
mutex_unlock(&powercap_cntrl_list_lock);
638633

634+
result = device_register(&control_type->dev);
635+
if (result) {
636+
mutex_lock(&powercap_cntrl_list_lock);
637+
list_del(&control_type->node);
638+
mutex_unlock(&powercap_cntrl_list_lock);
639+
640+
idr_destroy(&control_type->idr);
641+
put_device(&control_type->dev);
642+
return ERR_PTR(result);
643+
}
644+
639645
return control_type;
640646
}
641647
EXPORT_SYMBOL_GPL(powercap_register_control_type);

0 commit comments

Comments
 (0)