]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
powercap: fix race condition in register_control_type()
authorSumeet Pawnikar <sumeet4linux@gmail.com>
Fri, 5 Dec 2025 19:02:16 +0000 (00:32 +0530)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 19 Jan 2026 12:10:19 +0000 (13:10 +0100)
[ Upstream commit 7bda1910c4bccd4b8d4726620bb3d6bbfb62286e ]

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>
drivers/powercap/powercap_sys.c

index fd475e463d1fa9c5b7e63bcdb6a44be31d428d49..d7dadcaa3736b0c9bab9b2353f71eb5b86910f08 100644 (file)
@@ -624,17 +624,23 @@ struct powercap_control_type *powercap_register_control_type(
        INIT_LIST_HEAD(&control_type->node);
        control_type->dev.class = &powercap_class;
        dev_set_name(&control_type->dev, "%s", name);
-       result = device_register(&control_type->dev);
-       if (result) {
-               put_device(&control_type->dev);
-               return ERR_PTR(result);
-       }
        idr_init(&control_type->idr);
 
        mutex_lock(&powercap_cntrl_list_lock);
        list_add_tail(&control_type->node, &powercap_cntrl_list);
        mutex_unlock(&powercap_cntrl_list_lock);
 
+       result = device_register(&control_type->dev);
+       if (result) {
+               mutex_lock(&powercap_cntrl_list_lock);
+               list_del(&control_type->node);
+               mutex_unlock(&powercap_cntrl_list_lock);
+
+               idr_destroy(&control_type->idr);
+               put_device(&control_type->dev);
+               return ERR_PTR(result);
+       }
+
        return control_type;
 }
 EXPORT_SYMBOL_GPL(powercap_register_control_type);