2024-02-24 20:15:03

by Ricardo B. Marliere

[permalink] [raw]
Subject: [PATCH v2] power: supply: core: constify the struct device_type usage

Since commit aed65af1cc2f ("drivers: make device_type const"), the driver
core can properly handle constant struct device_type. Move the
power_supply_dev_type variable to be a constant structure as well, placing
it into read-only memory which can not be modified at runtime.

In order to accomplish that, export power_supply_attr_group in
power_supply.h and use it with the macro __ATTRIBUTE_GROUPS when defining
power_supply_dev_type in power_supply_core.c. Therefore the attribute group
is no longer static. Lastly, because power_supply_attr_groups is no longer
dynamically associated to power_supply_dev_type in
power_supply_init_attrs(), make the function receive zero arguments.

Cc: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Ricardo B. Marliere <[email protected]>
---
Changes in v2:
- Exported attribute group and made use of __ATTRIBUTE_GROUPS macro.
- Properly put the struct back where it should be (power_supply_core.c).
- Cleaned power_supply_init_attrs()
- Link to v1: https://lore.kernel.org/r/[email protected]
---
drivers/power/supply/power_supply.h | 4 +++-
drivers/power/supply/power_supply_core.c | 8 ++++++--
drivers/power/supply/power_supply_sysfs.c | 11 ++---------
3 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/drivers/power/supply/power_supply.h b/drivers/power/supply/power_supply.h
index 645eee4d6b6a..232fdd8c1212 100644
--- a/drivers/power/supply/power_supply.h
+++ b/drivers/power/supply/power_supply.h
@@ -13,9 +13,11 @@ struct device;
struct device_type;
struct power_supply;

+extern const struct attribute_group power_supply_attr_group;
+
#ifdef CONFIG_SYSFS

-extern void power_supply_init_attrs(struct device_type *dev_type);
+extern void power_supply_init_attrs(void);
extern int power_supply_uevent(const struct device *dev, struct kobj_uevent_env *env);

#else
diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index ecef35ac3b7e..37dac7669090 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -31,7 +31,11 @@ EXPORT_SYMBOL_GPL(power_supply_class);

static BLOCKING_NOTIFIER_HEAD(power_supply_notifier);

-static struct device_type power_supply_dev_type;
+__ATTRIBUTE_GROUPS(power_supply_attr);
+static const struct device_type power_supply_dev_type = {
+ .name = "power_supply",
+ .groups = power_supply_attr_groups,
+};

#define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)

@@ -1623,7 +1627,7 @@ static int __init power_supply_class_init(void)
return PTR_ERR(power_supply_class);

power_supply_class->dev_uevent = power_supply_uevent;
- power_supply_init_attrs(&power_supply_dev_type);
+ power_supply_init_attrs();

return 0;
}
diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c
index 977611e16373..edb240450e38 100644
--- a/drivers/power/supply/power_supply_sysfs.c
+++ b/drivers/power/supply/power_supply_sysfs.c
@@ -389,22 +389,15 @@ static umode_t power_supply_attr_is_visible(struct kobject *kobj,
return 0;
}

-static const struct attribute_group power_supply_attr_group = {
+const struct attribute_group power_supply_attr_group = {
.attrs = __power_supply_attrs,
.is_visible = power_supply_attr_is_visible,
};

-static const struct attribute_group *power_supply_attr_groups[] = {
- &power_supply_attr_group,
- NULL,
-};
-
-void power_supply_init_attrs(struct device_type *dev_type)
+void power_supply_init_attrs(void)
{
int i;

- dev_type->groups = power_supply_attr_groups;
-
for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++) {
struct device_attribute *attr;


---
base-commit: a9b254892ce1a447b06c5019cbf0e9caeb48c138
change-id: 20240220-device_cleanup-power-037594022cb1

Best regards,
--
Ricardo B. Marliere <[email protected]>



2024-02-25 20:41:09

by Sebastian Reichel

[permalink] [raw]
Subject: Re: [PATCH v2] power: supply: core: constify the struct device_type usage


On Sat, 24 Feb 2024 17:15:41 -0300, Ricardo B. Marliere wrote:
> Since commit aed65af1cc2f ("drivers: make device_type const"), the driver
> core can properly handle constant struct device_type. Move the
> power_supply_dev_type variable to be a constant structure as well, placing
> it into read-only memory which can not be modified at runtime.
>
> In order to accomplish that, export power_supply_attr_group in
> power_supply.h and use it with the macro __ATTRIBUTE_GROUPS when defining
> power_supply_dev_type in power_supply_core.c. Therefore the attribute group
> is no longer static. Lastly, because power_supply_attr_groups is no longer
> dynamically associated to power_supply_dev_type in
> power_supply_init_attrs(), make the function receive zero arguments.
>
> [...]

Applied, thanks!

[1/1] power: supply: core: constify the struct device_type usage
commit: 7b46b60944d77e361a727cd8ce46aec31c146e26

Best regards,
--
Sebastian Reichel <[email protected]>