Skip to content

Commit

Permalink
power: supply: core: mark attribute arrays as ro_after_init
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Thomas Weißschuh authored and Sebastian Reichel committed Oct 16, 2024
1 parent cf70da2 commit 3120b5f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion drivers/power/supply/power_supply.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern int power_supply_property_is_writeable(struct power_supply *psy,

#ifdef CONFIG_SYSFS

extern void power_supply_init_attrs(void);
extern void __init power_supply_init_attrs(void);
extern int power_supply_uevent(const struct device *dev, struct kobj_uevent_env *env);
extern const struct attribute_group *power_supply_attr_groups[];

Expand Down
12 changes: 6 additions & 6 deletions drivers/power/supply/power_supply_sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ static const char * const POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[] = {
[POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE] = "force-discharge",
};

static struct power_supply_attr power_supply_attrs[] = {
static struct power_supply_attr power_supply_attrs[] __ro_after_init = {
/* Properties of type `int' */
POWER_SUPPLY_ENUM_ATTR(STATUS),
POWER_SUPPLY_ENUM_ATTR(CHARGE_TYPE),
Expand Down Expand Up @@ -225,9 +225,9 @@ static struct power_supply_attr power_supply_attrs[] = {
#define POWER_SUPPLY_ATTR_CNT ARRAY_SIZE(power_supply_attrs)

static struct attribute *
__power_supply_attrs[POWER_SUPPLY_ATTR_CNT + 1];
__power_supply_attrs[POWER_SUPPLY_ATTR_CNT + 1] __ro_after_init;

static struct power_supply_attr *to_ps_attr(struct device_attribute *attr)
static const struct power_supply_attr *to_ps_attr(struct device_attribute *attr)
{
return container_of(attr, struct power_supply_attr, dev_attr);
}
Expand Down Expand Up @@ -273,7 +273,7 @@ static ssize_t power_supply_show_property(struct device *dev,
char *buf) {
ssize_t ret;
struct power_supply *psy = dev_get_drvdata(dev);
struct power_supply_attr *ps_attr = to_ps_attr(attr);
const struct power_supply_attr *ps_attr = to_ps_attr(attr);
enum power_supply_property psp = dev_attr_psp(attr);
union power_supply_propval value;

Expand Down Expand Up @@ -326,7 +326,7 @@ static ssize_t power_supply_store_property(struct device *dev,
const char *buf, size_t count) {
ssize_t ret;
struct power_supply *psy = dev_get_drvdata(dev);
struct power_supply_attr *ps_attr = to_ps_attr(attr);
const struct power_supply_attr *ps_attr = to_ps_attr(attr);
enum power_supply_property psp = dev_attr_psp(attr);
union power_supply_propval value;

Expand Down Expand Up @@ -401,7 +401,7 @@ const struct attribute_group *power_supply_attr_groups[] = {
NULL
};

void power_supply_init_attrs(void)
void __init power_supply_init_attrs(void)
{
int i;

Expand Down

0 comments on commit 3120b5f

Please sign in to comment.