lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <d1a535df-37e9-481e-b1c0-c809923b34ea@gmx.de>
Date: Fri, 27 Jun 2025 22:55:12 +0200
From: Armin Wolf <W_Armin@....de>
To: sre@...nel.org, hdegoede@...hat.com, ilpo.jarvinen@...ux.intel.com
Cc: linux-pm@...r.kernel.org, platform-driver-x86@...r.kernel.org,
 linux-kernel@...r.kernel.org, linux@...ssschuh.net
Subject: Re: [PATCH 1/3] power: supply: core: Add
 power_supply_get/set_property_direct()

Am 27.06.25 um 22:53 schrieb 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.

Sorry for sending the same patch series twice, it seems that git ignored my request
to append "RESEND" to the title of each patch :(.

> Suggested-by: Hans de Goede <hansg@...nel.org>
> Signed-off-by: Armin Wolf <W_Armin@....de>
> ---
>   drivers/power/supply/power_supply_core.c | 82 ++++++++++++++++++++----
>   include/linux/power_supply.h             |  8 +++
>   2 files changed, 78 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
> index aedb20c1d276..e70ffedf1a80 100644
> --- a/drivers/power/supply/power_supply_core.c
> +++ b/drivers/power/supply/power_supply_core.c
> @@ -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)
>   {
> diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
> index 45468959dd98..f21f806bfb38 100644
> --- a/include/linux/power_supply.h
> +++ b/include/linux/power_supply.h
> @@ -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);
>   

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ