Skip to contentSkip to conversation

cpuidle: Fail cpuidle device registration if there is one already

PATCH Superseded Rafael J. Wysocki 2 participants 3 messages Sep 18, 2025 – Sep 19, 2025 CPU idle time management framework

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Rafael J. Wysocki

author maintainer
#0 lore raw

PATCH 1/1 cpuidle: Fail cpuidle device registration if there is one already

From: Rafael J. Wysocki <[email protected]>

Refuse to register a cpuidle device if the given CPU has a cpuidle device already and print a message regarding it.

Without this, an attempt to register a new cpuidle device without unregistering the existing one leads to the removal of the existing cpuidle device without removing its sysfs interface.

---
 drivers/cpuidle/cpuidle.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/cpuidle/cpuidle.c +7 -1
@@ -635,11 +635,17 @@ static void __cpuidle_device_init(struct
 static int __cpuidle_register_device(struct cpuidle_device *dev)
 {
 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
+	unsigned int cpu = dev->cpu;
 	int i, ret;
 
 	if (!try_module_get(drv->owner))
 		return -EINVAL;
 
+	if (per_cpu(cpuidle_devices, cpu)) {
+		pr_info("CPU%d: cpuidle device already registered\n", cpu);
+		return -EEXIST;
+	}
+
 	for (i = 0; i < drv->state_count; i++) {
 		if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE)
 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
@@ -648,7 +654,7 @@ static int __cpuidle_register_device(str
 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER;
 	}
 
-	per_cpu(cpuidle_devices, dev->cpu) = dev;
+	per_cpu(cpuidle_devices, cpu) = dev;
 	list_add(&dev->device_list, &cpuidle_detected_devices);
 
 	ret = cpuidle_coupled_register_device(dev);
#1 lore raw

Hi Rafael,

Quoted 19 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Refuse to register a cpuidle device if the given CPU has a cpuidle device already and print a message regarding it.

Without this, an attempt to register a new cpuidle device without unregistering the existing one leads to the removal of the existing cpuidle device without removing its sysfs interface.

---
 drivers/cpuidle/cpuidle.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/cpuidle/cpuidle.c +0 -0
@@ -635,11 +635,17 @@ static void __cpuidle_device_init(struct
 static int __cpuidle_register_device(struct cpuidle_device *dev)
 {

drivers/cpuidle/cpuidle.c static void __cpuidle_device_init(struct · line 646

Quoted 10 lines
drivers/cpuidle/cpuidle.c +4 -0
 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
+	unsigned int cpu = dev->cpu;
 	int i, ret;
 
 	if (!try_module_get(drv->owner))
 		return -EINVAL;
 
+	if (per_cpu(cpuidle_devices, cpu)) {
+		pr_info("CPU%d: cpuidle device already registered\n", cpu);
+		return -EEXIST;

Here we return prematurely after a try_module_get right?
Do we need a module_put() similar to how you do it later by calling unregister_device function by checking ret = cpuidle_coupled_register_device ?

Quoted 18 lines
drivers/cpuidle/cpuidle.c +3 -1
+	}
+
 	for (i = 0; i < drv->state_count; i++) {
 		if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE)
 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
@@ -648,7 +654,7 @@ static int __cpuidle_register_device(str
 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER;
 	}
 
-	per_cpu(cpuidle_devices, dev->cpu) = dev;
+	per_cpu(cpuidle_devices, cpu) = dev;
 	list_add(&dev->device_list, &cpuidle_detected_devices);
 
 	ret = cpuidle_coupled_register_device(dev);
Signature · 3 lines
Best regards,
Dhruva Gole
Texas Instruments Incorporated
author maintainer
#2 lore raw

Hi,

Quoted 30 lines · Dhruva Gole

Hi Rafael,

Quoted 26 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Refuse to register a cpuidle device if the given CPU has a cpuidle device already and print a message regarding it.

Without this, an attempt to register a new cpuidle device without unregistering the existing one leads to the removal of the existing cpuidle device without removing its sysfs interface.

---
 drivers/cpuidle/cpuidle.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/cpuidle/cpuidle.c +1 -0
@@ -635,11 +635,17 @@ static void __cpuidle_device_init(struct
 static int __cpuidle_register_device(struct cpuidle_device *dev)
 {
      struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
+     unsigned int cpu = dev->cpu;
      int i, ret;
 
      if (!try_module_get(drv->owner))
              return -EINVAL;
 
Quoted 7 lines

drivers/cpuidle/cpuidle.c static void __cpuidle_device_init(struct · line 646

Quoted 3 lines
drivers/cpuidle/cpuidle.c +3 -0
+     if (per_cpu(cpuidle_devices, cpu)) {
+             pr_info("CPU%d: cpuidle device already registered\n", cpu);
+             return -EEXIST;

Here we return prematurely after a try_module_get right?
Do we need a module_put() similar to how you do it later by calling unregister_device function by checking ret = cpuidle_coupled_register_device ?

Good catch, thanks!

I need to move the new check above the driver module reference counting.