Skip to contentSkip to conversation

PM: domains: Fix return value of API dev_pm_get_subsys_data()

PATCH RESEND Superseded Zijun Hu 2 participants 4 messages Oct 28, 2024 – Nov 13, 2024

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Zijun Hu

author
#0 lore raw

PATCH RESEND 1/1 PM: domains: Fix return value of API dev_pm_get_subsys_data()

From: Zijun Hu <[email protected]>

dev_pm_get_subsys_data() has below 2 issues under condition (@dev->power.subsys_data != NULL):

- it will do unnecessary kzalloc() and kfree().
- it will return -ENOMEM if the kzalloc() fails, that is wrong since the kzalloc() is not needed.

Fixed by not doing kzalloc() and returning 0 for the condition.

Fixes ef27bed1870d ("PM: Reference counting of power.subsys_data")
---
 drivers/base/power/common.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/base/power/common.c +8 -0
index 8c34ae1cd8d5..13cb1f2a06e7 100644
@@ -26,6 +26,14 @@ int dev_pm_get_subsys_data(struct device *dev)
 {
 	struct pm_subsys_data *psd;
 
+	spin_lock_irq(&dev->power.lock);
+	if (dev->power.subsys_data) {
+		dev->power.subsys_data->refcount++;
+		spin_unlock_irq(&dev->power.lock);
+		return 0;
+	}
+	spin_unlock_irq(&dev->power.lock);
+
 	psd = kzalloc(sizeof(*psd), GFP_KERNEL);
 	if (!psd)
 		return -ENOMEM;

---
base-commit: 9852d85ec9d492ebef56dc5f229416c925758edc
change-id: 20241010-fix_dev_pm_get_subsys_data-2478bb200fde

Best regards,

Signature · 1 line
Zijun Hu <[email protected]>
#1 lore raw
Quoted 6 lines · Zijun Hu

From: Zijun Hu <[email protected]>

dev_pm_get_subsys_data() has below 2 issues under condition (@dev->power.subsys_data != NULL):

- it will do unnecessary kzalloc() and kfree().

But that's ok, everything still works, right?

Quoted 2 lines

- it will return -ENOMEM if the kzalloc() fails, that is wrong since the kzalloc() is not needed.

But it's ok to return the proper error if the system is that broken.

Quoted 5 lines

Fixed by not doing kzalloc() and returning 0 for the condition.

Fixes ef27bed1870d ("PM: Reference counting of power.subsys_data")

Why is this relevant for stable kernels?

thanks,

greg k-h

author
#2 lore raw
Quoted 9 lines · Greg Kroah-Hartman
Quoted 6 lines · Zijun Hu

From: Zijun Hu <[email protected]>

dev_pm_get_subsys_data() has below 2 issues under condition (@dev->power.subsys_data != NULL):

- it will do unnecessary kzalloc() and kfree().

But that's ok, everything still works, right?

yes.

Quoted 5 lines
Quoted 3 lines

- it will return -ENOMEM if the kzalloc() fails, that is wrong since the kzalloc() is not needed.

But it's ok to return the proper error if the system is that broken.

IMO, the API should return 0 (success) instead of -ENOMEM since it does not need to do kzalloc().

Different return value should impact caller's logic.

Quoted 8 lines
Quoted 6 lines

Fixed by not doing kzalloc() and returning 0 for the condition.

Fixes ef27bed1870d ("PM: Reference counting of power.subsys_data")

Why is this relevant for stable kernels?

you can remove both Fix and stable tag directly if you like this change.(^^)

Quoted 4 lines

thanks,

greg k-h

author
#3 lore raw
In reply to Greg Kroah-Hartman
Quoted 10 lines · Greg Kroah-Hartman
Quoted 6 lines · Zijun Hu

From: Zijun Hu <[email protected]>

dev_pm_get_subsys_data() has below 2 issues under condition (@dev->power.subsys_data != NULL):

- it will do unnecessary kzalloc() and kfree().

But that's ok, everything still works, right?

i don't think so, as explained below:

under condition (@dev->power.subsys_data != NULL), the API does not need to do kzalloc() and should always return 0 (success).

but it actually does *unnecessary* kzalloc() and have below impact shown:

if (kzalloc() is successfully)
it will degrade the API's performance
else
it changed the API's return value to -ENOMEM from 0, that will impact caller's logic.

Quoted 12 lines
Quoted 2 lines

- it will return -ENOMEM if the kzalloc() fails, that is wrong since the kzalloc() is not needed.

But it's ok to return the proper error if the system is that broken.

Quoted 5 lines
Quoted 1 line

Fixed by not doing kzalloc() and returning 0 for the condition.

Fixes ef27bed1870d ("PM: Reference counting of power.subsys_data")

Why is this relevant for stable kernels?

it has impact related to performance and logic as explained above.

Quoted 3 lines

thanks,

greg k-h