Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1665080 > unrolled thread

[PATCH 1/2] power: supply: sbs-battery: Prevent CAPACITY_MODE races

Started byShawn Nematbakhsh <shawnn@chromium.org>
First post2017-06-13 20:00 +0200
Last post2017-06-15 18:50 +0200
Articles 3 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH 1/2] power: supply: sbs-battery: Prevent CAPACITY_MODE races Shawn Nematbakhsh <shawnn@chromium.org> - 2017-06-13 20:00 +0200
    Re: [PATCH 1/2] power: supply: sbs-battery: Prevent CAPACITY_MODE races Guenter Roeck <groeck@google.com> - 2017-06-13 20:20 +0200
    Re: [PATCH 1/2] power: supply: sbs-battery: Prevent CAPACITY_MODE  races Sebastian Reichel <sebastian.reichel@collabora.co.uk> - 2017-06-15 18:50 +0200

#1665080 — [PATCH 1/2] power: supply: sbs-battery: Prevent CAPACITY_MODE races

FromShawn Nematbakhsh <shawnn@chromium.org>
Date2017-06-13 20:00 +0200
Subject[PATCH 1/2] power: supply: sbs-battery: Prevent CAPACITY_MODE races
Message-ID<tRYgG-2x8-29@gated-at.bofh.it>
A subset of smart battery commands return charge or energy depending on
the CAPACITY_MODE bit setting of BatteryMode(). In order to
unambiguously read a charge or energy value, it is necessary to ensure
that CAPACITY_MODE is set as desired, and not changed for the duration
of the attribute read.

Signed-off-by: Shawn Nematbakhsh <shawnn@chromium.org>
---
 drivers/power/supply/sbs-battery.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/power/supply/sbs-battery.c b/drivers/power/supply/sbs-battery.c
index e3a114e60f1a..d7e68fd97099 100644
--- a/drivers/power/supply/sbs-battery.c
+++ b/drivers/power/supply/sbs-battery.c
@@ -171,6 +171,7 @@ struct sbs_info {
 	u32				i2c_retry_count;
 	u32				poll_retry_count;
 	struct delayed_work		work;
+	struct mutex			mode_lock;
 };
 
 static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
@@ -623,7 +624,13 @@ static int sbs_get_property(struct power_supply *psy,
 		if (ret < 0)
 			break;
 
+		/* sbs_get_battery_capacity() will change the battery mode
+		 * temporarily to read the requested attribute. Ensure we stay
+		 * in the desired mode for the duration of the attribute read.
+		 */
+		mutex_lock(&chip->mode_lock);
 		ret = sbs_get_battery_capacity(client, ret, psp, val);
+		mutex_unlock(&chip->mode_lock);
 		break;
 
 	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
@@ -808,6 +815,7 @@ static int sbs_probe(struct i2c_client *client,
 	psy_cfg.of_node = client->dev.of_node;
 	psy_cfg.drv_data = chip;
 	chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
+	mutex_init(&chip->mode_lock);
 
 	/* use pdata if available, fall back to DT properties,
 	 * or hardcoded defaults if not
-- 
2.13.1.508.gb3defc5cc-goog

[toc] | [next] | [standalone]


#1665096

FromGuenter Roeck <groeck@google.com>
Date2017-06-13 20:20 +0200
Message-ID<tRYA2-2SW-13@gated-at.bofh.it>
In reply to#1665080
On Tue, Jun 13, 2017 at 10:53 AM, Shawn Nematbakhsh <shawnn@chromium.org> wrote:
> A subset of smart battery commands return charge or energy depending on
> the CAPACITY_MODE bit setting of BatteryMode(). In order to
> unambiguously read a charge or energy value, it is necessary to ensure
> that CAPACITY_MODE is set as desired, and not changed for the duration
> of the attribute read.
>
> Signed-off-by: Shawn Nematbakhsh <shawnn@chromium.org>

Reviewed-by: Guenter Roeck <groeck@chromium.org>

> ---
>  drivers/power/supply/sbs-battery.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
>
> diff --git a/drivers/power/supply/sbs-battery.c b/drivers/power/supply/sbs-battery.c
> index e3a114e60f1a..d7e68fd97099 100644
> --- a/drivers/power/supply/sbs-battery.c
> +++ b/drivers/power/supply/sbs-battery.c
> @@ -171,6 +171,7 @@ struct sbs_info {
>         u32                             i2c_retry_count;
>         u32                             poll_retry_count;
>         struct delayed_work             work;
> +       struct mutex                    mode_lock;
>  };
>
>  static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
> @@ -623,7 +624,13 @@ static int sbs_get_property(struct power_supply *psy,
>                 if (ret < 0)
>                         break;
>
> +               /* sbs_get_battery_capacity() will change the battery mode
> +                * temporarily to read the requested attribute. Ensure we stay
> +                * in the desired mode for the duration of the attribute read.
> +                */
> +               mutex_lock(&chip->mode_lock);
>                 ret = sbs_get_battery_capacity(client, ret, psp, val);
> +               mutex_unlock(&chip->mode_lock);
>                 break;
>
>         case POWER_SUPPLY_PROP_SERIAL_NUMBER:
> @@ -808,6 +815,7 @@ static int sbs_probe(struct i2c_client *client,
>         psy_cfg.of_node = client->dev.of_node;
>         psy_cfg.drv_data = chip;
>         chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
> +       mutex_init(&chip->mode_lock);
>
>         /* use pdata if available, fall back to DT properties,
>          * or hardcoded defaults if not
> --
> 2.13.1.508.gb3defc5cc-goog
>

[toc] | [prev] | [next] | [standalone]


#1666897 — Re: [PATCH 1/2] power: supply: sbs-battery: Prevent CAPACITY_MODE races

FromSebastian Reichel <sebastian.reichel@collabora.co.uk>
Date2017-06-15 18:50 +0200
SubjectRe: [PATCH 1/2] power: supply: sbs-battery: Prevent CAPACITY_MODE races
Message-ID<tSG83-4QO-43@gated-at.bofh.it>
In reply to#1665080

[Multipart message — attachments visible in raw view] — view raw

Hi,

On Tue, Jun 13, 2017 at 10:53:25AM -0700, Shawn Nematbakhsh wrote:
> A subset of smart battery commands return charge or energy depending on
> the CAPACITY_MODE bit setting of BatteryMode(). In order to
> unambiguously read a charge or energy value, it is necessary to ensure
> that CAPACITY_MODE is set as desired, and not changed for the duration
> of the attribute read.
> 
> Signed-off-by: Shawn Nematbakhsh <shawnn@chromium.org>
> ---

Thanks, queued.

-- Sebastian

>  drivers/power/supply/sbs-battery.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/power/supply/sbs-battery.c b/drivers/power/supply/sbs-battery.c
> index e3a114e60f1a..d7e68fd97099 100644
> --- a/drivers/power/supply/sbs-battery.c
> +++ b/drivers/power/supply/sbs-battery.c
> @@ -171,6 +171,7 @@ struct sbs_info {
>  	u32				i2c_retry_count;
>  	u32				poll_retry_count;
>  	struct delayed_work		work;
> +	struct mutex			mode_lock;
>  };
>  
>  static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
> @@ -623,7 +624,13 @@ static int sbs_get_property(struct power_supply *psy,
>  		if (ret < 0)
>  			break;
>  
> +		/* sbs_get_battery_capacity() will change the battery mode
> +		 * temporarily to read the requested attribute. Ensure we stay
> +		 * in the desired mode for the duration of the attribute read.
> +		 */
> +		mutex_lock(&chip->mode_lock);
>  		ret = sbs_get_battery_capacity(client, ret, psp, val);
> +		mutex_unlock(&chip->mode_lock);
>  		break;
>  
>  	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
> @@ -808,6 +815,7 @@ static int sbs_probe(struct i2c_client *client,
>  	psy_cfg.of_node = client->dev.of_node;
>  	psy_cfg.drv_data = chip;
>  	chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
> +	mutex_init(&chip->mode_lock);
>  
>  	/* use pdata if available, fall back to DT properties,
>  	 * or hardcoded defaults if not
> -- 
> 2.13.1.508.gb3defc5cc-goog
> 

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web