Skip to contentSkip to conversation

power: supply: core: Add power_supply_get/set_property_direct()

PATCH In review Armin Wolf 4 participants 10 messages Jun 27, 2025 – Jul 7, 2025

Subsystems (3)
lore
Share & tools
lore

Armin Wolf

author maintainer
#0 lore raw

PATCH 1/3 power: supply: core: Add power_supply_get/set_property_direct()

Power supply extensions might want to interact with the underlying power supply to retrieve data like serial numbers, charging status and more. However doing so causes psy->extensions_sem to be locked twice, possibly causing a deadlock.

Provide special variants of power_supply_get/set_property() that ignore any power supply extensions and thus do not touch the associated psy->extensions_sem lock.

---
 drivers/power/supply/power_supply_core.c | 82 ++++++++++++++++++++----
 include/linux/power_supply.h             |  8 +++
 2 files changed, 78 insertions(+), 12 deletions(-)
drivers/power/supply/power_supply_core.c +70 -12
index aedb20c1d276..e70ffedf1a80 100644
@@ -1241,9 +1241,8 @@ bool power_supply_has_property(struct power_supply *psy,
 	return false;
 }
 
-int power_supply_get_property(struct power_supply *psy,
-			    enum power_supply_property psp,
-			    union power_supply_propval *val)
+static int __power_supply_get_property(struct power_supply *psy, enum power_supply_property psp,
+				       union power_supply_propval *val, bool use_extensions)
 {
 	struct power_supply_ext_registration *reg;
 
@@ -1253,10 +1252,14 @@ int power_supply_get_property(struct power_supply *psy,
 		return -ENODEV;
 	}
 
-	scoped_guard(rwsem_read, &psy->extensions_sem) {
-		power_supply_for_each_extension(reg, psy) {
-			if (power_supply_ext_has_property(reg->ext, psp))
+	if (use_extensions) {
+		scoped_guard(rwsem_read, &psy->extensions_sem) {
+			power_supply_for_each_extension(reg, psy) {
+				if (!power_supply_ext_has_property(reg->ext, psp))
+					continue;
+
 				return reg->ext->get_property(psy, reg->ext, reg->data, psp, val);
+			}
 		}
 	}
 
@@ -1267,20 +1270,49 @@ int power_supply_get_property(struct power_supply *psy,
 	else
 		return -EINVAL;
 }
+
+int power_supply_get_property(struct power_supply *psy, enum power_supply_property psp,
+			      union power_supply_propval *val)
+{
+	return __power_supply_get_property(psy, psp, val, true);
+}
 EXPORT_SYMBOL_GPL(power_supply_get_property);
 
-int power_supply_set_property(struct power_supply *psy,
-			    enum power_supply_property psp,
-			    const union power_supply_propval *val)
+/**
+ * power_supply_get_property_direct - Read a power supply property without checking for extensions
+ * @psy: The power supply
+ * @psp: The power supply property to read
+ * @val: The resulting value of the power supply property
+ *
+ * Read a power supply property without taking into account any power supply extensions registered
+ * on the given power supply. This is mostly useful for power supply extensions that want to access
+ * their own power supply as using power_supply_get_property() directly will result in a potential
+ * deadlock.
+ *
+ * Return: 0 on success or negative error code on failure.
+ */
+int power_supply_get_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     union power_supply_propval *val)
+{
+        return __power_supply_get_property(psy, psp, val, false);
+}
+EXPORT_SYMBOL_GPL(power_supply_get_property_direct);
+
+
+static int __power_supply_set_property(struct power_supply *psy, enum power_supply_property psp,
+				       const union power_supply_propval *val, bool use_extensions)
 {
 	struct power_supply_ext_registration *reg;
 
 	if (atomic_read(&psy->use_cnt) <= 0)
 		return -ENODEV;
 
-	scoped_guard(rwsem_read, &psy->extensions_sem) {
-		power_supply_for_each_extension(reg, psy) {
-			if (power_supply_ext_has_property(reg->ext, psp)) {
+	if (use_extensions) {
+		scoped_guard(rwsem_read, &psy->extensions_sem) {
+			power_supply_for_each_extension(reg, psy) {
+				if (!power_supply_ext_has_property(reg->ext, psp))
+					continue;
+
 				if (reg->ext->set_property)
 					return reg->ext->set_property(psy, reg->ext, reg->data,
 								      psp, val);
@@ -1295,8 +1327,34 @@ int power_supply_set_property(struct power_supply *psy,
 
 	return psy->desc->set_property(psy, psp, val);
 }
+
+int power_supply_set_property(struct power_supply *psy, enum power_supply_property psp,
+			      const union power_supply_propval *val)
+{
+	return __power_supply_set_property(psy, psp, val, true);
+}
 EXPORT_SYMBOL_GPL(power_supply_set_property);
 
+/**
+ * power_supply_set_property_direct - Write a power supply property without checking for extensions
+ * @psy: The power supply
+ * @psp: The power supply property to write
+ * @val: The value to write to the power supply property
+ *
+ * Write a power supply property without taking into account any power supply extensions registered
+ * on the given power supply. This is mostly useful for power supply extensions that want to access
+ * their own power supply as using power_supply_set_property() directly will result in a potential
+ * deadlock.
+ *
+ * Return: 0 on success or negative error code on failure.
+ */
+int power_supply_set_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     const union power_supply_propval *val)
+{
+	return __power_supply_set_property(psy, psp, val, false);
+}
+EXPORT_SYMBOL_GPL(power_supply_set_property_direct);
+
 int power_supply_property_is_writeable(struct power_supply *psy,
 					enum power_supply_property psp)
 {
include/linux/power_supply.h +8 -0
index 45468959dd98..f21f806bfb38 100644
@@ -878,15 +878,23 @@ static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
 extern int power_supply_get_property(struct power_supply *psy,
 			    enum power_supply_property psp,
 			    union power_supply_propval *val);
+int power_supply_get_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     union power_supply_propval *val);
 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
 extern int power_supply_set_property(struct power_supply *psy,
 			    enum power_supply_property psp,
 			    const union power_supply_propval *val);
+int power_supply_set_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     const union power_supply_propval *val);
 #else
 static inline int power_supply_set_property(struct power_supply *psy,
 			    enum power_supply_property psp,
 			    const union power_supply_propval *val)
 { return 0; }
+static inline int power_supply_set_property_direct(struct power_supply *psy,
+						   enum power_supply_property psp,
+						   const union power_supply_propval *val)
+{ return 0; }
 #endif
 extern void power_supply_external_power_changed(struct power_supply *psy);
 
Signature · 1 line
2.39.5
author maintainer
#1 lore raw

PATCH 2/3 power: supply: test-power: Test access to extended power supply

Test that power supply extensions can access properties of their power supply using power_supply_get_property_direct(). This both ensures that the functionality works and serves as an example for future driver developers.

---
 drivers/power/supply/test_power.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/power/supply/test_power.c +4 -0
index 5bfdfcf6013b..2c0e9ad820c0 100644
@@ -259,6 +259,7 @@ static const struct power_supply_config test_power_configs[] = {
 static int test_power_battery_extmanufacture_year = 1234;
 static int test_power_battery_exttemp_max = 1000;
 static const enum power_supply_property test_power_battery_extprops[] = {
+	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
 	POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
 	POWER_SUPPLY_PROP_TEMP_MAX,
 };
@@ -270,6 +271,9 @@ static int test_power_battery_extget_property(struct power_supply *psy,
 					      union power_supply_propval *val)
 {
 	switch (psp) {
+	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
+		return power_supply_get_property_direct(psy, POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
+							val);
 	case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
 		val->intval = test_power_battery_extmanufacture_year;
 		break;
Signature · 1 line
2.39.5
Acked-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] power: supply: test-power: Test access to extended power supply

Hi,

Quoted 7 lines · Armin Wolf

Test that power supply extensions can access properties of their power supply using power_supply_get_property_direct(). This both ensures that the functionality works and serves as an example for future driver developers.

---

-- Sebastian

Quoted 29 lines
 drivers/power/supply/test_power.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/power/supply/test_power.c +4 -0
index 5bfdfcf6013b..2c0e9ad820c0 100644
@@ -259,6 +259,7 @@ static const struct power_supply_config test_power_configs[] = {
 static int test_power_battery_extmanufacture_year = 1234;
 static int test_power_battery_exttemp_max = 1000;
 static const enum power_supply_property test_power_battery_extprops[] = {
+	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
 	POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
 	POWER_SUPPLY_PROP_TEMP_MAX,
 };
@@ -270,6 +271,9 @@ static int test_power_battery_extget_property(struct power_supply *psy,
 					      union power_supply_propval *val)
 {
 	switch (psp) {
+	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
+		return power_supply_get_property_direct(psy, POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
+							val);
 	case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
 		val->intval = test_power_battery_extmanufacture_year;
 		break;
Signature · 1 line
2.39.5
author maintainer
#3 lore raw

PATCH 3/3 platform/x86: dell-ddv: Fix taking the psy->extensions_sem lock twice

In reply to Armin Wolf

Calling power_supply_get_property() inside dell_wmi_ddv_battery_translate() can cause a deadlock since this function is also being called from the power supply extension code, in which case psy->extensions_sem is already being held.

Fix this by using the new power_supply_get_property_direct() function to ignore any power supply extensions when retrieving the battery serial number.

Tested on a Dell Inspiron 3505.

Fixes 058de163a376 ("platform/x86: dell-ddv: Implement the battery matching algorithm")
---
 drivers/platform/x86/dell/dell-wmi-ddv.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/platform/x86/dell/dell-wmi-ddv.c +7 -3
index 67f3d7158403..62e3d060f038 100644
@@ -689,9 +689,13 @@ static int dell_wmi_ddv_battery_translate(struct dell_wmi_ddv_data *data,
 
 	dev_dbg(&data->wdev->dev, "Translation cache miss\n");
 
-	/* Perform a translation between a ACPI battery and a battery index */
-
-	ret = power_supply_get_property(battery, POWER_SUPPLY_PROP_SERIAL_NUMBER, &val);
+	/*
+	 * Perform a translation between a ACPI battery and a battery index.
+	 * We have to use power_supply_get_property_direct() here because this
+	 * function will also get called from the callbacks of the power supply
+	 * extension.
+	 */
+	ret = power_supply_get_property_direct(battery, POWER_SUPPLY_PROP_SERIAL_NUMBER, &val);
 	if (ret < 0)
 		return ret;
 
Signature · 1 line
2.39.5
maintainer Reviewed-by on v1 1/3
#4 lore raw
In reply to Armin Wolf

Hi Armin,

Quoted 11 lines · Armin Wolf

Power supply extensions might want to interact with the underlying power supply to retrieve data like serial numbers, charging status and more. However doing so causes psy->extensions_sem to be locked twice, possibly causing a deadlock.

Provide special variants of power_supply_get/set_property() that ignore any power supply extensions and thus do not touch the associated psy->extensions_sem lock.

Thank you for your work on this.

The entire series looks good to me:

for the series.

There is the question of how to merge this. I think it might be best for the entire series to go through the power-supply tree.

Ilpo would that work for you and if yes can we have your ack ?

Sebastian, IMHO this should be merged as fixed not as for-next material.

Regards,

Hans

Quoted 158 lines
---
 drivers/power/supply/power_supply_core.c | 82 ++++++++++++++++++++----
 include/linux/power_supply.h             |  8 +++
 2 files changed, 78 insertions(+), 12 deletions(-)
drivers/power/supply/power_supply_core.c +70 -12
index aedb20c1d276..e70ffedf1a80 100644
@@ -1241,9 +1241,8 @@ bool power_supply_has_property(struct power_supply *psy,
 	return false;
 }
 
-int power_supply_get_property(struct power_supply *psy,
-			    enum power_supply_property psp,
-			    union power_supply_propval *val)
+static int __power_supply_get_property(struct power_supply *psy, enum power_supply_property psp,
+				       union power_supply_propval *val, bool use_extensions)
 {
 	struct power_supply_ext_registration *reg;
 
@@ -1253,10 +1252,14 @@ int power_supply_get_property(struct power_supply *psy,
 		return -ENODEV;
 	}
 
-	scoped_guard(rwsem_read, &psy->extensions_sem) {
-		power_supply_for_each_extension(reg, psy) {
-			if (power_supply_ext_has_property(reg->ext, psp))
+	if (use_extensions) {
+		scoped_guard(rwsem_read, &psy->extensions_sem) {
+			power_supply_for_each_extension(reg, psy) {
+				if (!power_supply_ext_has_property(reg->ext, psp))
+					continue;
+
 				return reg->ext->get_property(psy, reg->ext, reg->data, psp, val);
+			}
 		}
 	}
 
@@ -1267,20 +1270,49 @@ int power_supply_get_property(struct power_supply *psy,
 	else
 		return -EINVAL;
 }
+
+int power_supply_get_property(struct power_supply *psy, enum power_supply_property psp,
+			      union power_supply_propval *val)
+{
+	return __power_supply_get_property(psy, psp, val, true);
+}
 EXPORT_SYMBOL_GPL(power_supply_get_property);
 
-int power_supply_set_property(struct power_supply *psy,
-			    enum power_supply_property psp,
-			    const union power_supply_propval *val)
+/**
+ * power_supply_get_property_direct - Read a power supply property without checking for extensions
+ * @psy: The power supply
+ * @psp: The power supply property to read
+ * @val: The resulting value of the power supply property
+ *
+ * Read a power supply property without taking into account any power supply extensions registered
+ * on the given power supply. This is mostly useful for power supply extensions that want to access
+ * their own power supply as using power_supply_get_property() directly will result in a potential
+ * deadlock.
+ *
+ * Return: 0 on success or negative error code on failure.
+ */
+int power_supply_get_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     union power_supply_propval *val)
+{
+        return __power_supply_get_property(psy, psp, val, false);
+}
+EXPORT_SYMBOL_GPL(power_supply_get_property_direct);
+
+
+static int __power_supply_set_property(struct power_supply *psy, enum power_supply_property psp,
+				       const union power_supply_propval *val, bool use_extensions)
 {
 	struct power_supply_ext_registration *reg;
 
 	if (atomic_read(&psy->use_cnt) <= 0)
 		return -ENODEV;
 
-	scoped_guard(rwsem_read, &psy->extensions_sem) {
-		power_supply_for_each_extension(reg, psy) {
-			if (power_supply_ext_has_property(reg->ext, psp)) {
+	if (use_extensions) {
+		scoped_guard(rwsem_read, &psy->extensions_sem) {
+			power_supply_for_each_extension(reg, psy) {
+				if (!power_supply_ext_has_property(reg->ext, psp))
+					continue;
+
 				if (reg->ext->set_property)
 					return reg->ext->set_property(psy, reg->ext, reg->data,
 								      psp, val);
@@ -1295,8 +1327,34 @@ int power_supply_set_property(struct power_supply *psy,
 
 	return psy->desc->set_property(psy, psp, val);
 }
+
+int power_supply_set_property(struct power_supply *psy, enum power_supply_property psp,
+			      const union power_supply_propval *val)
+{
+	return __power_supply_set_property(psy, psp, val, true);
+}
 EXPORT_SYMBOL_GPL(power_supply_set_property);
 
+/**
+ * power_supply_set_property_direct - Write a power supply property without checking for extensions
+ * @psy: The power supply
+ * @psp: The power supply property to write
+ * @val: The value to write to the power supply property
+ *
+ * Write a power supply property without taking into account any power supply extensions registered
+ * on the given power supply. This is mostly useful for power supply extensions that want to access
+ * their own power supply as using power_supply_set_property() directly will result in a potential
+ * deadlock.
+ *
+ * Return: 0 on success or negative error code on failure.
+ */
+int power_supply_set_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     const union power_supply_propval *val)
+{
+	return __power_supply_set_property(psy, psp, val, false);
+}
+EXPORT_SYMBOL_GPL(power_supply_set_property_direct);
+
 int power_supply_property_is_writeable(struct power_supply *psy,
 					enum power_supply_property psp)
 {
include/linux/power_supply.h +8 -0
index 45468959dd98..f21f806bfb38 100644
@@ -878,15 +878,23 @@ static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
 extern int power_supply_get_property(struct power_supply *psy,
 			    enum power_supply_property psp,
 			    union power_supply_propval *val);
+int power_supply_get_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     union power_supply_propval *val);
 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
 extern int power_supply_set_property(struct power_supply *psy,
 			    enum power_supply_property psp,
 			    const union power_supply_propval *val);
+int power_supply_set_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     const union power_supply_propval *val);
 #else
 static inline int power_supply_set_property(struct power_supply *psy,
 			    enum power_supply_property psp,
 			    const union power_supply_propval *val)
 { return 0; }
+static inline int power_supply_set_property_direct(struct power_supply *psy,
+						   enum power_supply_property psp,
+						   const union power_supply_propval *val)
+{ return 0; }
 #endif
 extern void power_supply_external_power_changed(struct power_supply *psy);
 
author maintainer
#5 lore raw

Am 28.06.25 um 11:25 schrieb Hans de Goede:

Quoted 26 lines

Hi Armin,

Quoted 11 lines · Armin Wolf

Power supply extensions might want to interact with the underlying power supply to retrieve data like serial numbers, charging status and more. However doing so causes psy->extensions_sem to be locked twice, possibly causing a deadlock.

Provide special variants of power_supply_get/set_property() that ignore any power supply extensions and thus do not touch the associated psy->extensions_sem lock.

Thank you for your work on this.

The entire series looks good to me:

for the series.

There is the question of how to merge this. I think it might be best for the entire series to go through the power-supply tree.

Quoted 8 lines

Ilpo would that work for you and if yes can we have your ack ?

Sebastian, IMHO this should be merged as fixed not as for-next material.

Regards,

Hans

Personally i would prefer to merge this through the pdx86 tree as the uniwill-laptop driver currently under review will also require this functionality.

Thanks,
Armin Wolf

Quoted 159 lines
Quoted 158 lines
---
  drivers/power/supply/power_supply_core.c | 82 ++++++++++++++++++++----
  include/linux/power_supply.h             |  8 +++
  2 files changed, 78 insertions(+), 12 deletions(-)
drivers/power/supply/power_supply_core.c +70 -12
index aedb20c1d276..e70ffedf1a80 100644
@@ -1241,9 +1241,8 @@ bool power_supply_has_property(struct power_supply *psy,
  	return false;
  }
 
-int power_supply_get_property(struct power_supply *psy,
-			    enum power_supply_property psp,
-			    union power_supply_propval *val)
+static int __power_supply_get_property(struct power_supply *psy, enum power_supply_property psp,
+				       union power_supply_propval *val, bool use_extensions)
  {
  	struct power_supply_ext_registration *reg;
 
@@ -1253,10 +1252,14 @@ int power_supply_get_property(struct power_supply *psy,
  		return -ENODEV;
  	}
 
-	scoped_guard(rwsem_read, &psy->extensions_sem) {
-		power_supply_for_each_extension(reg, psy) {
-			if (power_supply_ext_has_property(reg->ext, psp))
+	if (use_extensions) {
+		scoped_guard(rwsem_read, &psy->extensions_sem) {
+			power_supply_for_each_extension(reg, psy) {
+				if (!power_supply_ext_has_property(reg->ext, psp))
+					continue;
+
  				return reg->ext->get_property(psy, reg->ext, reg->data, psp, val);
+			}
  		}
  	}
 
@@ -1267,20 +1270,49 @@ int power_supply_get_property(struct power_supply *psy,
  	else
  		return -EINVAL;
  }
+
+int power_supply_get_property(struct power_supply *psy, enum power_supply_property psp,
+			      union power_supply_propval *val)
+{
+	return __power_supply_get_property(psy, psp, val, true);
+}
  EXPORT_SYMBOL_GPL(power_supply_get_property);
 
-int power_supply_set_property(struct power_supply *psy,
-			    enum power_supply_property psp,
-			    const union power_supply_propval *val)
+/**
+ * power_supply_get_property_direct - Read a power supply property without checking for extensions
+ * @psy: The power supply
+ * @psp: The power supply property to read
+ * @val: The resulting value of the power supply property
+ *
+ * Read a power supply property without taking into account any power supply extensions registered
+ * on the given power supply. This is mostly useful for power supply extensions that want to access
+ * their own power supply as using power_supply_get_property() directly will result in a potential
+ * deadlock.
+ *
+ * Return: 0 on success or negative error code on failure.
+ */
+int power_supply_get_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     union power_supply_propval *val)
+{
+        return __power_supply_get_property(psy, psp, val, false);
+}
+EXPORT_SYMBOL_GPL(power_supply_get_property_direct);
+
+
+static int __power_supply_set_property(struct power_supply *psy, enum power_supply_property psp,
+				       const union power_supply_propval *val, bool use_extensions)
  {
  	struct power_supply_ext_registration *reg;
 
  	if (atomic_read(&psy->use_cnt) <= 0)
  		return -ENODEV;
 
-	scoped_guard(rwsem_read, &psy->extensions_sem) {
-		power_supply_for_each_extension(reg, psy) {
-			if (power_supply_ext_has_property(reg->ext, psp)) {
+	if (use_extensions) {
+		scoped_guard(rwsem_read, &psy->extensions_sem) {
+			power_supply_for_each_extension(reg, psy) {
+				if (!power_supply_ext_has_property(reg->ext, psp))
+					continue;
+
  				if (reg->ext->set_property)
  					return reg->ext->set_property(psy, reg->ext, reg->data,
  								      psp, val);
@@ -1295,8 +1327,34 @@ int power_supply_set_property(struct power_supply *psy,
 
  	return psy->desc->set_property(psy, psp, val);
  }
+
+int power_supply_set_property(struct power_supply *psy, enum power_supply_property psp,
+			      const union power_supply_propval *val)
+{
+	return __power_supply_set_property(psy, psp, val, true);
+}
  EXPORT_SYMBOL_GPL(power_supply_set_property);
 
+/**
+ * power_supply_set_property_direct - Write a power supply property without checking for extensions
+ * @psy: The power supply
+ * @psp: The power supply property to write
+ * @val: The value to write to the power supply property
+ *
+ * Write a power supply property without taking into account any power supply extensions registered
+ * on the given power supply. This is mostly useful for power supply extensions that want to access
+ * their own power supply as using power_supply_set_property() directly will result in a potential
+ * deadlock.
+ *
+ * Return: 0 on success or negative error code on failure.
+ */
+int power_supply_set_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     const union power_supply_propval *val)
+{
+	return __power_supply_set_property(psy, psp, val, false);
+}
+EXPORT_SYMBOL_GPL(power_supply_set_property_direct);
+
  int power_supply_property_is_writeable(struct power_supply *psy,
  					enum power_supply_property psp)
  {
include/linux/power_supply.h +8 -0
index 45468959dd98..f21f806bfb38 100644
@@ -878,15 +878,23 @@ static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
  extern int power_supply_get_property(struct power_supply *psy,
  			    enum power_supply_property psp,
  			    union power_supply_propval *val);
+int power_supply_get_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     union power_supply_propval *val);
  #if IS_ENABLED(CONFIG_POWER_SUPPLY)
  extern int power_supply_set_property(struct power_supply *psy,
  			    enum power_supply_property psp,
  			    const union power_supply_propval *val);
+int power_supply_set_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     const union power_supply_propval *val);
  #else
  static inline int power_supply_set_property(struct power_supply *psy,
  			    enum power_supply_property psp,
  			    const union power_supply_propval *val)
  { return 0; }
+static inline int power_supply_set_property_direct(struct power_supply *psy,
+						   enum power_supply_property psp,
+						   const union power_supply_propval *val)
+{ return 0; }
  #endif
  extern void power_supply_external_power_changed(struct power_supply *psy);
 
maintainer
#6 lore raw
Quoted 33 lines · Armin Wolf

Am 28.06.25 um 11:25 schrieb Hans de Goede:

Quoted 31 lines

Hi Armin,

Quoted 11 lines · Armin Wolf

Power supply extensions might want to interact with the underlying power supply to retrieve data like serial numbers, charging status and more. However doing so causes psy->extensions_sem to be locked twice, possibly causing a deadlock.

Provide special variants of power_supply_get/set_property() that ignore any power supply extensions and thus do not touch the associated psy->extensions_sem lock.

Thank you for your work on this.

The entire series looks good to me:

for the series.

There is the question of how to merge this. I think it might be best for the entire series to go through the power-supply tree.

Ilpo would that work for you and if yes can we have your ack ?

Sebastian, IMHO this should be merged as fixed not as for-next material.

Quoted 7 lines
Quoted 3 lines

Regards,

Hans

Personally i would prefer to merge this through the pdx86 tree as the uniwill-laptop driver currently under review will also require this functionality.

Sebastian, are you okay if I take this through pdx86 fixes branch as requested by Armin? If yes, can I have your ack please.

Signature · 1 line
 i.
Quoted 184 lines
Quoted 183 lines
Quoted 182 lines
---
  drivers/power/supply/power_supply_core.c | 82 ++++++++++++++++++++----
  include/linux/power_supply.h             |  8 +++
  2 files changed, 78 insertions(+), 12 deletions(-)
drivers/power/supply/power_supply_core.c +2 -2
index aedb20c1d276..e70ffedf1a80 100644
@@ -1241,9 +1241,8 @@ bool power_supply_has_property(struct power_supply
 *psy,
  	return false;
  }
  -int power_supply_get_property(struct power_supply *psy,
-			    enum power_supply_property psp,
-			    union power_supply_propval *val)
+static int __power_supply_get_property(struct power_supply *psy, enum
 power_supply_property psp,
+				       union power_supply_propval *val, bool
 use_extensions)
  {
  	struct power_supply_ext_registration *reg;
  @@ -1253,10 +1252,14 @@ int power_supply_get_property(struct

power_supply *psy,
return -ENODEV;
}
- scoped_guard(rwsem_read, &psy->extensions_sem) {

drivers/power/supply/power_supply_core.c +4 -2
-		power_supply_for_each_extension(reg, psy) {
-			if (power_supply_ext_has_property(reg->ext, psp))
+	if (use_extensions) {
+		scoped_guard(rwsem_read, &psy->extensions_sem) {
+			power_supply_for_each_extension(reg, psy) {
+				if (!power_supply_ext_has_property(reg->ext,

psp))

drivers/power/supply/power_supply_core.c +2 -0
+					continue;
+
  				return reg->ext->get_property(psy, reg->ext,

reg->data, psp, val);

drivers/power/supply/power_supply_core.c +1 -0
+			}
  		}
  	}
  @@ -1267,20 +1270,49 @@ int power_supply_get_property(struct

power_supply *psy,
else
return -EINVAL;
}

drivers/power/supply/power_supply_core.c +2 -0
+
+int power_supply_get_property(struct power_supply *psy, enum

power_supply_property psp,

drivers/power/supply/power_supply_core.c +6 -2
+			      union power_supply_propval *val)
+{
+	return __power_supply_get_property(psy, psp, val, true);
+}
  EXPORT_SYMBOL_GPL(power_supply_get_property);
  -int power_supply_set_property(struct power_supply *psy,
-			    enum power_supply_property psp,
-			    const union power_supply_propval *val)
+/**
+ * power_supply_get_property_direct - Read a power supply property

without checking for extensions
+ * @psy: The power supply
+ * @psp: The power supply property to read
+ * @val: The resulting value of the power supply property
+ *
+ * Read a power supply property without taking into account any power supply extensions registered
+ * on the given power supply. This is mostly useful for power supply extensions that want to access
+ * their own power supply as using power_supply_get_property() directly will result in a potential
+ * deadlock.
+ *
+ * Return: 0 on success or negative error code on failure.
+ */
+int power_supply_get_property_direct(struct power_supply *psy, enum power_supply_property psp,

drivers/power/supply/power_supply_core.c +8 -0
+				     union power_supply_propval *val)
+{
+        return __power_supply_get_property(psy, psp, val, false);
+}
+EXPORT_SYMBOL_GPL(power_supply_get_property_direct);
+
+
+static int __power_supply_set_property(struct power_supply *psy, enum

power_supply_property psp,

drivers/power/supply/power_supply_core.c +1 -0
+				       const union power_supply_propval *val,

bool use_extensions)
{

  	struct power_supply_ext_registration *reg;
    	if (atomic_read(&psy->use_cnt) <= 0)

return -ENODEV;
- scoped_guard(rwsem_read, &psy->extensions_sem) {

drivers/power/supply/power_supply_core.c +4 -2
-		power_supply_for_each_extension(reg, psy) {
-			if (power_supply_ext_has_property(reg->ext, psp)) {
+	if (use_extensions) {
+		scoped_guard(rwsem_read, &psy->extensions_sem) {
+			power_supply_for_each_extension(reg, psy) {
+				if (!power_supply_ext_has_property(reg->ext,

psp))

drivers/power/supply/power_supply_core.c +2 -0
+					continue;
+
  				if (reg->ext->set_property)
  					return reg->ext->set_property(psy,

reg->ext, reg->data,
psp,
val);

drivers/power/supply/power_supply_core.c +7 -0
@@ -1295,8 +1327,34 @@ int power_supply_set_property(struct power_supply
 *psy,
    	return psy->desc->set_property(psy, psp, val);
  }
+
+int power_supply_set_property(struct power_supply *psy, enum
 power_supply_property psp,
+			      const union power_supply_propval *val)
+{
+	return __power_supply_set_property(psy, psp, val, true);
+}
  EXPORT_SYMBOL_GPL(power_supply_set_property);
  +/**
+ * power_supply_set_property_direct - Write a power supply property

without checking for extensions
+ * @psy: The power supply
+ * @psp: The power supply property to write
+ * @val: The value to write to the power supply property
+ *
+ * Write a power supply property without taking into account any power supply extensions registered
+ * on the given power supply. This is mostly useful for power supply extensions that want to access
+ * their own power supply as using power_supply_set_property() directly will result in a potential
+ * deadlock.
+ *
+ * Return: 0 on success or negative error code on failure.
+ */
+int power_supply_set_property_direct(struct power_supply *psy, enum power_supply_property psp,

drivers/power/supply/power_supply_core.c +6 -0
+				     const union power_supply_propval *val)
+{
+	return __power_supply_set_property(psy, psp, val, false);
+}
+EXPORT_SYMBOL_GPL(power_supply_set_property_direct);
+
  int power_supply_property_is_writeable(struct power_supply *psy,
  					enum power_supply_property psp)
  {
include/linux/power_supply.h +5 -0
index 45468959dd98..f21f806bfb38 100644
@@ -878,15 +878,23 @@ static inline int
 power_supply_is_system_supplied(void) { return -ENOSYS; }
  extern int power_supply_get_property(struct power_supply *psy,
  			    enum power_supply_property psp,
  			    union power_supply_propval *val);
+int power_supply_get_property_direct(struct power_supply *psy, enum
 power_supply_property psp,
+				     union power_supply_propval *val);
  #if IS_ENABLED(CONFIG_POWER_SUPPLY)
  extern int power_supply_set_property(struct power_supply *psy,
  			    enum power_supply_property psp,
  			    const union power_supply_propval *val);
+int power_supply_set_property_direct(struct power_supply *psy, enum
 power_supply_property psp,
+				     const union power_supply_propval *val);
  #else
  static inline int power_supply_set_property(struct power_supply *psy,
  			    enum power_supply_property psp,
  			    const union power_supply_propval *val)
  { return 0; }
+static inline int power_supply_set_property_direct(struct power_supply

*psy,

include/linux/power_supply.h +1 -0
+						   enum power_supply_property

psp,

include/linux/power_supply.h +1 -0
+						   const union

power_supply_propval *val)
+{ return 0; }
#endif
extern void power_supply_external_power_changed(struct power_supply
*psy);

#7 lore raw

Hi,

Quoted 37 lines · Ilpo Järvinen
Quoted 35 lines · Armin Wolf

Am 28.06.25 um 11:25 schrieb Hans de Goede:

Quoted 33 lines

Hi Armin,

Quoted 11 lines · Armin Wolf

Power supply extensions might want to interact with the underlying power supply to retrieve data like serial numbers, charging status and more. However doing so causes psy->extensions_sem to be locked twice, possibly causing a deadlock.

Provide special variants of power_supply_get/set_property() that ignore any power supply extensions and thus do not touch the associated psy->extensions_sem lock.

Thank you for your work on this.

The entire series looks good to me:

for the series.

There is the question of how to merge this. I think it might be best for the entire series to go through the power-supply tree.

Ilpo would that work for you and if yes can we have your ack ?

Sebastian, IMHO this should be merged as fixed not as for-next material.

Regards,

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Hans

Personally i would prefer to merge this through the pdx86 tree as the uniwill-laptop driver currently under review will also require this functionality.

Sebastian, are you okay if I take this through pdx86 fixes branch as requested by Armin? If yes, can I have your ack please.

Sorry, took me a bit to figure out if that works, since the core is also patched in my next branch. But I think git should figure everything out on its own. Feel free to merge the whole series.

Greetings,

-- Sebastian

Acked-by on v1 1/3
#8 lore raw
In reply to Armin Wolf

Hi,

Quoted 5 lines · Armin Wolf

Power supply extensions might want to interact with the underlying power supply to retrieve data like serial numbers, charging status and more. However doing so causes psy->extensions_sem to be locked twice, possibly causing a deadlock.

Quoted 7 lines

Provide special variants of power_supply_get/set_property() that ignore any power supply extensions and thus do not touch the associated psy->extensions_sem lock.

---

-- Sebastian

Quoted 160 lines
 drivers/power/supply/power_supply_core.c | 82 ++++++++++++++++++++----
 include/linux/power_supply.h             |  8 +++
 2 files changed, 78 insertions(+), 12 deletions(-)
drivers/power/supply/power_supply_core.c +70 -12
index aedb20c1d276..e70ffedf1a80 100644
@@ -1241,9 +1241,8 @@ bool power_supply_has_property(struct power_supply *psy,
 	return false;
 }
 
-int power_supply_get_property(struct power_supply *psy,
-			    enum power_supply_property psp,
-			    union power_supply_propval *val)
+static int __power_supply_get_property(struct power_supply *psy, enum power_supply_property psp,
+				       union power_supply_propval *val, bool use_extensions)
 {
 	struct power_supply_ext_registration *reg;
 
@@ -1253,10 +1252,14 @@ int power_supply_get_property(struct power_supply *psy,
 		return -ENODEV;
 	}
 
-	scoped_guard(rwsem_read, &psy->extensions_sem) {
-		power_supply_for_each_extension(reg, psy) {
-			if (power_supply_ext_has_property(reg->ext, psp))
+	if (use_extensions) {
+		scoped_guard(rwsem_read, &psy->extensions_sem) {
+			power_supply_for_each_extension(reg, psy) {
+				if (!power_supply_ext_has_property(reg->ext, psp))
+					continue;
+
 				return reg->ext->get_property(psy, reg->ext, reg->data, psp, val);
+			}
 		}
 	}
 
@@ -1267,20 +1270,49 @@ int power_supply_get_property(struct power_supply *psy,
 	else
 		return -EINVAL;
 }
+
+int power_supply_get_property(struct power_supply *psy, enum power_supply_property psp,
+			      union power_supply_propval *val)
+{
+	return __power_supply_get_property(psy, psp, val, true);
+}
 EXPORT_SYMBOL_GPL(power_supply_get_property);
 
-int power_supply_set_property(struct power_supply *psy,
-			    enum power_supply_property psp,
-			    const union power_supply_propval *val)
+/**
+ * power_supply_get_property_direct - Read a power supply property without checking for extensions
+ * @psy: The power supply
+ * @psp: The power supply property to read
+ * @val: The resulting value of the power supply property
+ *
+ * Read a power supply property without taking into account any power supply extensions registered
+ * on the given power supply. This is mostly useful for power supply extensions that want to access
+ * their own power supply as using power_supply_get_property() directly will result in a potential
+ * deadlock.
+ *
+ * Return: 0 on success or negative error code on failure.
+ */
+int power_supply_get_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     union power_supply_propval *val)
+{
+        return __power_supply_get_property(psy, psp, val, false);
+}
+EXPORT_SYMBOL_GPL(power_supply_get_property_direct);
+
+
+static int __power_supply_set_property(struct power_supply *psy, enum power_supply_property psp,
+				       const union power_supply_propval *val, bool use_extensions)
 {
 	struct power_supply_ext_registration *reg;
 
 	if (atomic_read(&psy->use_cnt) <= 0)
 		return -ENODEV;
 
-	scoped_guard(rwsem_read, &psy->extensions_sem) {
-		power_supply_for_each_extension(reg, psy) {
-			if (power_supply_ext_has_property(reg->ext, psp)) {
+	if (use_extensions) {
+		scoped_guard(rwsem_read, &psy->extensions_sem) {
+			power_supply_for_each_extension(reg, psy) {
+				if (!power_supply_ext_has_property(reg->ext, psp))
+					continue;
+
 				if (reg->ext->set_property)
 					return reg->ext->set_property(psy, reg->ext, reg->data,
 								      psp, val);
@@ -1295,8 +1327,34 @@ int power_supply_set_property(struct power_supply *psy,
 
 	return psy->desc->set_property(psy, psp, val);
 }
+
+int power_supply_set_property(struct power_supply *psy, enum power_supply_property psp,
+			      const union power_supply_propval *val)
+{
+	return __power_supply_set_property(psy, psp, val, true);
+}
 EXPORT_SYMBOL_GPL(power_supply_set_property);
 
+/**
+ * power_supply_set_property_direct - Write a power supply property without checking for extensions
+ * @psy: The power supply
+ * @psp: The power supply property to write
+ * @val: The value to write to the power supply property
+ *
+ * Write a power supply property without taking into account any power supply extensions registered
+ * on the given power supply. This is mostly useful for power supply extensions that want to access
+ * their own power supply as using power_supply_set_property() directly will result in a potential
+ * deadlock.
+ *
+ * Return: 0 on success or negative error code on failure.
+ */
+int power_supply_set_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     const union power_supply_propval *val)
+{
+	return __power_supply_set_property(psy, psp, val, false);
+}
+EXPORT_SYMBOL_GPL(power_supply_set_property_direct);
+
 int power_supply_property_is_writeable(struct power_supply *psy,
 					enum power_supply_property psp)
 {
include/linux/power_supply.h +8 -0
index 45468959dd98..f21f806bfb38 100644
@@ -878,15 +878,23 @@ static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
 extern int power_supply_get_property(struct power_supply *psy,
 			    enum power_supply_property psp,
 			    union power_supply_propval *val);
+int power_supply_get_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     union power_supply_propval *val);
 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
 extern int power_supply_set_property(struct power_supply *psy,
 			    enum power_supply_property psp,
 			    const union power_supply_propval *val);
+int power_supply_set_property_direct(struct power_supply *psy, enum power_supply_property psp,
+				     const union power_supply_propval *val);
 #else
 static inline int power_supply_set_property(struct power_supply *psy,
 			    enum power_supply_property psp,
 			    const union power_supply_propval *val)
 { return 0; }
+static inline int power_supply_set_property_direct(struct power_supply *psy,
+						   enum power_supply_property psp,
+						   const union power_supply_propval *val)
+{ return 0; }
 #endif
 extern void power_supply_external_power_changed(struct power_supply *psy);
 
Signature · 1 line
2.39.5
maintainer
#9 lore raw
In reply to Armin Wolf
Quoted 10 lines · Armin Wolf

Power supply extensions might want to interact with the underlying power supply to retrieve data like serial numbers, charging status and more. However doing so causes psy->extensions_sem to be locked twice, possibly causing a deadlock.

Provide special variants of power_supply_get/set_property() that ignore any power supply extensions and thus do not touch the associated psy->extensions_sem lock.

[...]

Thank you for your contribution, it has been applied to my local review-ilpo-fixes branch. Note it will show up in the public platform-drivers-x86/review-ilpo-fixes branch only once I've pushed my local branch there, which might take a while.

The list of commits applied:
[1/3] power: supply: core: Add power_supply_get/set_property_direct()

      commit: 3ebed2fddf6fac5729ffc8c471c87d111b641678
[2/3] power: supply: test-power: Test access to extended power supply
      commit: a5f354232118751fe43be6ac896f8d6e7d7418b5
[3/3] platform/x86: dell-ddv: Fix taking the psy->extensions_sem lock twice
      commit: d4e83784b2a9be58b938d55efb232d2751c4cab4

--
i.