2020-04-03 20:21:18

by Michał Mirosław

[permalink] [raw]
Subject: [PATCH v3 02/11] power: charger-manager: don't write through desc->properties

psy_desc->properties will become pointer to const. Avoid writing
through the pointer to enable constification of the tables elsewhere.

Signed-off-by: Michał Mirosław <[email protected]>
---
v3: initial version
---
drivers/power/supply/charger-manager.c | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/drivers/power/supply/charger-manager.c b/drivers/power/supply/charger-manager.c
index 887f5bb879e5..7ecb82107efb 100644
--- a/drivers/power/supply/charger-manager.c
+++ b/drivers/power/supply/charger-manager.c
@@ -1425,15 +1425,18 @@ static int cm_init_thermal_data(struct charger_manager *cm,
struct power_supply *fuel_gauge)
{
struct charger_desc *desc = cm->desc;
+ enum power_supply_property *props;
union power_supply_propval val;
int ret;

+ props = (void *)cm->charger_psy_desc.properties;
+
/* Verify whether fuel gauge provides battery temperature */
ret = power_supply_get_property(fuel_gauge,
POWER_SUPPLY_PROP_TEMP, &val);

if (!ret) {
- cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
+ props[cm->charger_psy_desc.num_properties] =
POWER_SUPPLY_PROP_TEMP;
cm->charger_psy_desc.num_properties++;
cm->desc->measure_battery_temp = true;
@@ -1446,7 +1449,7 @@ static int cm_init_thermal_data(struct charger_manager *cm,
return PTR_ERR(cm->tzd_batt);

/* Use external thermometer */
- cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
+ props[cm->charger_psy_desc.num_properties] =
POWER_SUPPLY_PROP_TEMP_AMBIENT;
cm->charger_psy_desc.num_properties++;
cm->desc->measure_battery_temp = true;
@@ -1622,6 +1625,7 @@ static int charger_manager_probe(struct platform_device *pdev)
union power_supply_propval val;
struct power_supply *fuel_gauge;
struct power_supply_config psy_cfg = {};
+ enum power_supply_property *props;

if (IS_ERR(desc)) {
dev_err(&pdev->dev, "No platform data (desc) found\n");
@@ -1717,7 +1721,7 @@ static int charger_manager_probe(struct platform_device *pdev)
cm->charger_psy_desc.name = cm->psy_name_buf;

/* Allocate for psy properties because they may vary */
- cm->charger_psy_desc.properties =
+ cm->charger_psy_desc.properties = props =
devm_kcalloc(&pdev->dev,
ARRAY_SIZE(default_charger_props) +
NUM_CHARGER_PSY_OPTIONAL,
@@ -1725,7 +1729,7 @@ static int charger_manager_probe(struct platform_device *pdev)
if (!cm->charger_psy_desc.properties)
return -ENOMEM;

- memcpy(cm->charger_psy_desc.properties, default_charger_props,
+ memcpy(props, default_charger_props,
sizeof(enum power_supply_property) *
ARRAY_SIZE(default_charger_props));

@@ -1738,14 +1742,14 @@ static int charger_manager_probe(struct platform_device *pdev)
}
if (!power_supply_get_property(fuel_gauge,
POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
- cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
+ props[cm->charger_psy_desc.num_properties] =
POWER_SUPPLY_PROP_CHARGE_NOW;
cm->charger_psy_desc.num_properties++;
}
if (!power_supply_get_property(fuel_gauge,
POWER_SUPPLY_PROP_CURRENT_NOW,
&val)) {
- cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
+ props[cm->charger_psy_desc.num_properties] =
POWER_SUPPLY_PROP_CURRENT_NOW;
cm->charger_psy_desc.num_properties++;
}
--
2.20.1


2020-05-01 12:41:07

by Sebastian Reichel

[permalink] [raw]
Subject: Re: [PATCH v3 02/11] power: charger-manager: don't write through desc->properties

Hi,

On Fri, Apr 03, 2020 at 10:20:31PM +0200, Michał Mirosław wrote:
> psy_desc->properties will become pointer to const. Avoid writing
> through the pointer to enable constification of the tables elsewhere.
>
> Signed-off-by: Michał Mirosław <[email protected]>
> ---

For patches 1-3 I used my version, that I wrote in parallel while
reviewing a different patch series. It is slightly different, but
achieves the same goal.

-- Sebastian


Attachments:
(No filename) (467.00 B)
signature.asc (849.00 B)
Download all attachments

2020-05-01 13:33:07

by Michał Mirosław

[permalink] [raw]
Subject: Re: [PATCH v3 02/11] power: charger-manager: don't write through desc->properties

On Fri, May 01, 2020 at 02:38:49PM +0200, Sebastian Reichel wrote:
> Hi,
>
> On Fri, Apr 03, 2020 at 10:20:31PM +0200, Micha? Miros?aw wrote:
> > psy_desc->properties will become pointer to const. Avoid writing
> > through the pointer to enable constification of the tables elsewhere.
> >
> > Signed-off-by: Micha? Miros?aw <[email protected]>
> For patches 1-3 I used my version, that I wrote in parallel while
> reviewing a different patch series. It is slightly different, but
> achieves the same goal.

There is a bug in the tree now regarding use of num_properties
in charger-manager. Following patch should fix it.

Best Regards,
Micha? Miros?aw

diff --git a/drivers/power/supply/charger-manager.c b/drivers/power/supply/charger-manager.c
index a71e2ee81423..2ef53dc1f2fb 100644
--- a/drivers/power/supply/charger-manager.c
+++ b/drivers/power/supply/charger-manager.c
@@ -1740,14 +1740,14 @@ static int charger_manager_probe(struct platform_device *pdev)
}
if (!power_supply_get_property(fuel_gauge,
POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
- properties[cm->charger_psy_desc.num_properties] =
+ properties[num_properties] =
POWER_SUPPLY_PROP_CHARGE_NOW;
num_properties++;
}
if (!power_supply_get_property(fuel_gauge,
POWER_SUPPLY_PROP_CURRENT_NOW,
&val)) {
- properties[cm->charger_psy_desc.num_properties] =
+ properties[num_properties] =
POWER_SUPPLY_PROP_CURRENT_NOW;
num_properties++;
}

2020-05-01 13:41:44

by Michał Mirosław

[permalink] [raw]
Subject: [PATCH] power: charger-manager: fix adding of optional properties

Use num_properties to index added property.
This will prevent overwriting POWER_SUPPLY_PROP_CHARGE_NOW with
POWER_SUPPLY_PROP_CURRENT_NOW and leaving the latter entry
uninitialized.

For clarity, num_properties is initialized with length of the copied
array instead of relying on previously memcpy'd value.

Fixes: 0a46510addc7 ("power: supply: charger-manager: Prepare for const properties")
Signed-off-by: Michał Mirosław <[email protected]>
---
drivers/power/supply/charger-manager.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/power/supply/charger-manager.c b/drivers/power/supply/charger-manager.c
index a71e2ee81423..2ef53dc1f2fb 100644
--- a/drivers/power/supply/charger-manager.c
+++ b/drivers/power/supply/charger-manager.c
@@ -1729,7 +1729,7 @@ static int charger_manager_probe(struct platform_device *pdev)
memcpy(properties, default_charger_props,
sizeof(enum power_supply_property) *
ARRAY_SIZE(default_charger_props));
- num_properties = psy_default.num_properties;
+ num_properties = ARRAY_SIZE(default_charger_props);

/* Find which optional psy-properties are available */
fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
@@ -1740,14 +1740,14 @@ static int charger_manager_probe(struct platform_device *pdev)
}
if (!power_supply_get_property(fuel_gauge,
POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
- properties[cm->charger_psy_desc.num_properties] =
+ properties[num_properties] =
POWER_SUPPLY_PROP_CHARGE_NOW;
num_properties++;
}
if (!power_supply_get_property(fuel_gauge,
POWER_SUPPLY_PROP_CURRENT_NOW,
&val)) {
- properties[cm->charger_psy_desc.num_properties] =
+ properties[num_properties] =
POWER_SUPPLY_PROP_CURRENT_NOW;
num_properties++;
}
--
2.20.1

2020-05-01 13:52:11

by Sebastian Reichel

[permalink] [raw]
Subject: Re: [PATCH v3 02/11] power: charger-manager: don't write through desc->properties

Hi,

On Fri, May 01, 2020 at 03:30:08PM +0200, Michał Mirosław wrote:
> On Fri, May 01, 2020 at 02:38:49PM +0200, Sebastian Reichel wrote:
> > Hi,
> >
> > On Fri, Apr 03, 2020 at 10:20:31PM +0200, Michał Mirosław wrote:
> > > psy_desc->properties will become pointer to const. Avoid writing
> > > through the pointer to enable constification of the tables elsewhere.
> > >
> > > Signed-off-by: Michał Mirosław <[email protected]>
> > For patches 1-3 I used my version, that I wrote in parallel while
> > reviewing a different patch series. It is slightly different, but
> > achieves the same goal.
>
> There is a bug in the tree now regarding use of num_properties
> in charger-manager. Following patch should fix it.

Thanks, I folded this into the charger-manager patch.

-- Sebastian

> Best Regards,
> Michał Mirosław
>
> diff --git a/drivers/power/supply/charger-manager.c b/drivers/power/supply/charger-manager.c
> index a71e2ee81423..2ef53dc1f2fb 100644
> --- a/drivers/power/supply/charger-manager.c
> +++ b/drivers/power/supply/charger-manager.c
> @@ -1740,14 +1740,14 @@ static int charger_manager_probe(struct platform_device *pdev)
> }
> if (!power_supply_get_property(fuel_gauge,
> POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
> - properties[cm->charger_psy_desc.num_properties] =
> + properties[num_properties] =
> POWER_SUPPLY_PROP_CHARGE_NOW;
> num_properties++;
> }
> if (!power_supply_get_property(fuel_gauge,
> POWER_SUPPLY_PROP_CURRENT_NOW,
> &val)) {
> - properties[cm->charger_psy_desc.num_properties] =
> + properties[num_properties] =
> POWER_SUPPLY_PROP_CURRENT_NOW;
> num_properties++;
> }


Attachments:
(No filename) (1.69 kB)
signature.asc (849.00 B)
Download all attachments

2020-05-01 13:53:38

by Sebastian Reichel

[permalink] [raw]
Subject: Re: [PATCH] power: charger-manager: fix adding of optional properties

Hi,

On Fri, May 01, 2020 at 03:39:53PM +0200, Michał Mirosław wrote:
> Use num_properties to index added property.
> This will prevent overwriting POWER_SUPPLY_PROP_CHARGE_NOW with
> POWER_SUPPLY_PROP_CURRENT_NOW and leaving the latter entry
> uninitialized.
>
> For clarity, num_properties is initialized with length of the copied
> array instead of relying on previously memcpy'd value.
>
> Fixes: 0a46510addc7 ("power: supply: charger-manager: Prepare for const properties")
> Signed-off-by: Michał Mirosław <[email protected]>
> ---

I folded your fix directly into the charger-manager patch, which did
not yet reach linux-next. If you send the num_properties part as a
separate one, I will merge it.

-- Sebastian

> drivers/power/supply/charger-manager.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/power/supply/charger-manager.c b/drivers/power/supply/charger-manager.c
> index a71e2ee81423..2ef53dc1f2fb 100644
> --- a/drivers/power/supply/charger-manager.c
> +++ b/drivers/power/supply/charger-manager.c
> @@ -1729,7 +1729,7 @@ static int charger_manager_probe(struct platform_device *pdev)
> memcpy(properties, default_charger_props,
> sizeof(enum power_supply_property) *
> ARRAY_SIZE(default_charger_props));
> - num_properties = psy_default.num_properties;
> + num_properties = ARRAY_SIZE(default_charger_props);
>
> /* Find which optional psy-properties are available */
> fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
> @@ -1740,14 +1740,14 @@ static int charger_manager_probe(struct platform_device *pdev)
> }
> if (!power_supply_get_property(fuel_gauge,
> POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
> - properties[cm->charger_psy_desc.num_properties] =
> + properties[num_properties] =
> POWER_SUPPLY_PROP_CHARGE_NOW;
> num_properties++;
> }
> if (!power_supply_get_property(fuel_gauge,
> POWER_SUPPLY_PROP_CURRENT_NOW,
> &val)) {
> - properties[cm->charger_psy_desc.num_properties] =
> + properties[num_properties] =
> POWER_SUPPLY_PROP_CURRENT_NOW;
> num_properties++;
> }
> --
> 2.20.1
>


Attachments:
(No filename) (2.15 kB)
signature.asc (849.00 B)
Download all attachments

2020-05-01 14:18:44

by Michał Mirosław

[permalink] [raw]
Subject: Re: [PATCH] power: charger-manager: fix adding of optional properties

On Fri, May 01, 2020 at 03:51:09PM +0200, Sebastian Reichel wrote:
> Hi,
>
> On Fri, May 01, 2020 at 03:39:53PM +0200, Micha? Miros?aw wrote:
> > Use num_properties to index added property.
> > This will prevent overwriting POWER_SUPPLY_PROP_CHARGE_NOW with
> > POWER_SUPPLY_PROP_CURRENT_NOW and leaving the latter entry
> > uninitialized.
> >
> > For clarity, num_properties is initialized with length of the copied
> > array instead of relying on previously memcpy'd value.
> >
> > Fixes: 0a46510addc7 ("power: supply: charger-manager: Prepare for const properties")
> > Signed-off-by: Micha? Miros?aw <[email protected]>
> > ---
>
> I folded your fix directly into the charger-manager patch, which did
> not yet reach linux-next. If you send the num_properties part as a
> separate one, I will merge it.

Since your patch already changes the line, maybe you could squash first
hunk below into it?

> > drivers/power/supply/charger-manager.c | 6 +++---
> > 1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/power/supply/charger-manager.c b/drivers/power/supply/charger-manager.c
> > index a71e2ee81423..2ef53dc1f2fb 100644
> > --- a/drivers/power/supply/charger-manager.c
> > +++ b/drivers/power/supply/charger-manager.c
> > @@ -1729,7 +1729,7 @@ static int charger_manager_probe(struct platform_device *pdev)
> > memcpy(properties, default_charger_props,
> > sizeof(enum power_supply_property) *
> > ARRAY_SIZE(default_charger_props));
> > - num_properties = psy_default.num_properties;
> > + num_properties = ARRAY_SIZE(default_charger_props);
> >
> > /* Find which optional psy-properties are available */
> > fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
[...]

2020-05-01 14:32:31

by Michał Mirosław

[permalink] [raw]
Subject: [PATCH] power: charger-manager: clarify num_properties starting value

Initialize num_properties with length of the copied array instead
of relying on previously memcpy'd value. This makes it clear how
the array and the counter are related.

Signed-off-by: Michał Mirosław <[email protected]>
---
drivers/power/supply/charger-manager.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/power/supply/charger-manager.c b/drivers/power/supply/charger-manager.c
index 415a9efa6816..2ef53dc1f2fb 100644
--- a/drivers/power/supply/charger-manager.c
+++ b/drivers/power/supply/charger-manager.c
@@ -1729,7 +1729,7 @@ static int charger_manager_probe(struct platform_device *pdev)
memcpy(properties, default_charger_props,
sizeof(enum power_supply_property) *
ARRAY_SIZE(default_charger_props));
- num_properties = psy_default.num_properties;
+ num_properties = ARRAY_SIZE(default_charger_props);

/* Find which optional psy-properties are available */
fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
--
2.20.1

2020-05-01 15:00:33

by Sebastian Reichel

[permalink] [raw]
Subject: Re: [PATCH] power: charger-manager: clarify num_properties starting value

Hi,

On Fri, May 01, 2020 at 04:30:43PM +0200, Michał Mirosław wrote:
> Initialize num_properties with length of the copied array instead
> of relying on previously memcpy'd value. This makes it clear how
> the array and the counter are related.
>
> Signed-off-by: Michał Mirosław <[email protected]>
> ---

Thanks, queued.

-- Sebastian

> drivers/power/supply/charger-manager.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/power/supply/charger-manager.c b/drivers/power/supply/charger-manager.c
> index 415a9efa6816..2ef53dc1f2fb 100644
> --- a/drivers/power/supply/charger-manager.c
> +++ b/drivers/power/supply/charger-manager.c
> @@ -1729,7 +1729,7 @@ static int charger_manager_probe(struct platform_device *pdev)
> memcpy(properties, default_charger_props,
> sizeof(enum power_supply_property) *
> ARRAY_SIZE(default_charger_props));
> - num_properties = psy_default.num_properties;
> + num_properties = ARRAY_SIZE(default_charger_props);
>
> /* Find which optional psy-properties are available */
> fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
> --
> 2.20.1
>


Attachments:
(No filename) (1.15 kB)
signature.asc (849.00 B)
Download all attachments