Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

power: supply: core: mark attribute arrays as ro_after_init

The attribute arrays are only modified during the __init phase.
To protect them against intentional or accidental modification,
mark them as __ro_after_init.

To make sure no modifications are introduced, also mark the return
values of the accessors as const.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Link: https://lore.kernel.org/r/20241005-power-supply-cleanups-v1-3-45303b2d0a4d@weissschuh.net
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>

authored by

Thomas Weißschuh and committed by
Sebastian Reichel
3120b5f2 cf70da29

+7 -7
+1 -1
drivers/power/supply/power_supply.h
··· 18 18 19 19 #ifdef CONFIG_SYSFS 20 20 21 - extern void power_supply_init_attrs(void); 21 + extern void __init power_supply_init_attrs(void); 22 22 extern int power_supply_uevent(const struct device *dev, struct kobj_uevent_env *env); 23 23 extern const struct attribute_group *power_supply_attr_groups[]; 24 24
+6 -6
drivers/power/supply/power_supply_sysfs.c
··· 142 142 [POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE] = "force-discharge", 143 143 }; 144 144 145 - static struct power_supply_attr power_supply_attrs[] = { 145 + static struct power_supply_attr power_supply_attrs[] __ro_after_init = { 146 146 /* Properties of type `int' */ 147 147 POWER_SUPPLY_ENUM_ATTR(STATUS), 148 148 POWER_SUPPLY_ENUM_ATTR(CHARGE_TYPE), ··· 225 225 #define POWER_SUPPLY_ATTR_CNT ARRAY_SIZE(power_supply_attrs) 226 226 227 227 static struct attribute * 228 - __power_supply_attrs[POWER_SUPPLY_ATTR_CNT + 1]; 228 + __power_supply_attrs[POWER_SUPPLY_ATTR_CNT + 1] __ro_after_init; 229 229 230 - static struct power_supply_attr *to_ps_attr(struct device_attribute *attr) 230 + static const struct power_supply_attr *to_ps_attr(struct device_attribute *attr) 231 231 { 232 232 return container_of(attr, struct power_supply_attr, dev_attr); 233 233 } ··· 273 273 char *buf) { 274 274 ssize_t ret; 275 275 struct power_supply *psy = dev_get_drvdata(dev); 276 - struct power_supply_attr *ps_attr = to_ps_attr(attr); 276 + const struct power_supply_attr *ps_attr = to_ps_attr(attr); 277 277 enum power_supply_property psp = dev_attr_psp(attr); 278 278 union power_supply_propval value; 279 279 ··· 326 326 const char *buf, size_t count) { 327 327 ssize_t ret; 328 328 struct power_supply *psy = dev_get_drvdata(dev); 329 - struct power_supply_attr *ps_attr = to_ps_attr(attr); 329 + const struct power_supply_attr *ps_attr = to_ps_attr(attr); 330 330 enum power_supply_property psp = dev_attr_psp(attr); 331 331 union power_supply_propval value; 332 332 ··· 401 401 NULL 402 402 }; 403 403 404 - void power_supply_init_attrs(void) 404 + void __init power_supply_init_attrs(void) 405 405 { 406 406 int i; 407 407