w1: Constify w1_family_ops
Subsystems (2)
cover w1: Constify w1_family_ops
None of the current instances of struct w1_family_ops in the kernel is modified. Constify these to let the compiler put them in read-only memory.
The first patch changes the fops field in w1_family struct to a pointer to const and makes a local variable a pointer to const to avoid a compiler warning. This patch is a prerequisite for the second and third patches which constifies the static structs in drivers in w1 and power. These changes was done with coccinelle (details in the commit messages).
With these changes applied, all instances of struct w1_family_ops in the kernel are const.
Build-tested on x86 allmodconfig.
Rikard Falkeborn (3):
w1: Constify struct w1_family_ops
w1: Constify static w1_family_ops structs
power: supply: Constify static w1_family_ops structs
drivers/power/supply/bq27xxx_battery_hdq.c | 2 +- drivers/power/supply/ds2760_battery.c | 2 +- drivers/power/supply/max1721x_battery.c | 2 +- drivers/w1/slaves/w1_ds2405.c | 2 +- drivers/w1/slaves/w1_ds2406.c | 2 +- drivers/w1/slaves/w1_ds2408.c | 2 +- drivers/w1/slaves/w1_ds2413.c | 2 +- drivers/w1/slaves/w1_ds2423.c | 2 +- drivers/w1/slaves/w1_ds2430.c | 2 +- drivers/w1/slaves/w1_ds2431.c | 2 +- drivers/w1/slaves/w1_ds2433.c | 2 +- drivers/w1/slaves/w1_ds2438.c | 2 +- drivers/w1/slaves/w1_ds250x.c | 2 +- drivers/w1/slaves/w1_ds2780.c | 2 +- drivers/w1/slaves/w1_ds2781.c | 2 +- drivers/w1/slaves/w1_ds2805.c | 2 +- drivers/w1/slaves/w1_ds28e04.c | 2 +- drivers/w1/slaves/w1_ds28e17.c | 2 +- drivers/w1/slaves/w1_therm.c | 6 +++--- drivers/w1/w1.c | 4 ++-- include/linux/w1.h | 2 +- 21 files changed, 24 insertions(+), 24 deletions(-)
Signature · 1 line
2.28.0
PATCH 1/3 w1: Constify struct w1_family_ops
The fops field in the w1_family struct is never modified. Make it const to indicate that. Constifying the pointer makes it possible for drivers to declare static w1_family_ops structs const, which in turn will allow the compiler to put it in read-only memory.
--- drivers/w1/w1.c | 2 +- include/linux/w1.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/w1/w1.c +1 -1
@@ -613,7 +613,7 @@ static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)static int w1_family_notify (unsigned long action,struct w1_slave *sl) { -struct w1_family_ops *fops; +const struct w1_family_ops *fops;int err; fops = sl->family->fops;
include/linux/w1.h +1 -1
@@ -269,7 +269,7 @@ struct w1_family {struct list_head family_entry;u8 fid; -struct w1_family_ops *fops; +const struct w1_family_ops *fops;const struct of_device_id *of_match_table;
Signature · 1 line
2.28.0
Re: [PATCH 1/3] w1: Constify struct w1_family_ops
Hi,
Quoted 7 lines · Rikard Falkeborn
The fops field in the
w1_familystruct is never modified. Make it const to indicate that. Constifying the pointer makes it possible for drivers to declare staticw1_family_opsstructs const, which in turn will allow the compiler to put it in read-only memory.Signed-off-by Rikard Falkeborn <[email protected]>---
-- Sebastian
Quoted 33 lines
drivers/w1/w1.c | 2 +- include/linux/w1.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)drivers/w1/w1.c +1
-1@@ -613,7 +613,7 @@ static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)static int w1_family_notify (unsigned long action,struct w1_slave *sl) { -struct w1_family_ops *fops; +const struct w1_family_ops *fops;int err; fops = sl->family->fops;include/linux/w1.h +1
-1@@ -269,7 +269,7 @@ struct w1_family {struct list_head family_entry;u8 fid; -struct w1_family_ops *fops; +const struct w1_family_ops *fops;const struct of_device_id *of_match_table;Signature · 1 line
2.28.0
PATCH 2/3 w1: Constify static w1_family_ops structs
In reply to Rikard FalkebornThe only usage of these structs is to assign their address to the fops field in the w1_family struct, which is a const pointer. Make them const to allow the compiler to put them in read-only memory.
This was done with the following Coccinelle semantic patch
(http://coccinelle.lip6.fr/):
// <smpl>
@r1 disable optional_qualifier @
identifier i;
position p;
@@
static struct w1_family_ops i@p = {...};
@ok1@
identifier r1.i;
position p;
identifier s;
@@
static struct w1_family s = {
.fops=&i@p,
};@bad1@
position p!={r1.p,ok1.p};
identifier r1.i;
@@
i@p
@depends on !bad1 disable optional_qualifier@
identifier r1.i;
@@
static
+const
struct w1_family_ops i={};
// </smpl>
--- drivers/w1/slaves/w1_ds2405.c | 2 +- drivers/w1/slaves/w1_ds2406.c | 2 +- drivers/w1/slaves/w1_ds2408.c | 2 +- drivers/w1/slaves/w1_ds2413.c | 2 +- drivers/w1/slaves/w1_ds2423.c | 2 +- drivers/w1/slaves/w1_ds2430.c | 2 +- drivers/w1/slaves/w1_ds2431.c | 2 +- drivers/w1/slaves/w1_ds2433.c | 2 +- drivers/w1/slaves/w1_ds2438.c | 2 +- drivers/w1/slaves/w1_ds250x.c | 2 +- drivers/w1/slaves/w1_ds2780.c | 2 +- drivers/w1/slaves/w1_ds2781.c | 2 +- drivers/w1/slaves/w1_ds2805.c | 2 +- drivers/w1/slaves/w1_ds28e04.c | 2 +- drivers/w1/slaves/w1_ds28e17.c | 2 +- drivers/w1/slaves/w1_therm.c | 6 +++--- drivers/w1/w1.c | 2 +- 17 files changed, 19 insertions(+), 19 deletions(-)
drivers/w1/slaves/w1_ds2405.c +1 -1
@@ -206,7 +206,7 @@ static struct attribute *w1_ds2405_attrs[] = {ATTRIBUTE_GROUPS (w1_ds2405); -static struct w1_family_ops w1_ds2405_fops = { +static const struct w1_family_ops w1_ds2405_fops = { .groups = w1_ds2405_groups };
drivers/w1/slaves/w1_ds2406.c +1 -1
@@ -138,7 +138,7 @@ static void w1_f12_remove_slave(struct w1_slave *sl) &(w1_f12_sysfs_bin_files[i])); } -static struct w1_family_ops w1_f12_fops = { +static const struct w1_family_ops w1_f12_fops = { .add_slave = w1_f12_add_slave, .remove_slave = w1_f12_remove_slave, };
drivers/w1/slaves/w1_ds2408.c +1 -1
@@ -336,7 +336,7 @@ static const struct attribute_group *w1_f29_groups[] = { NULL, }; -static struct w1_family_ops w1_f29_fops = { +static const struct w1_family_ops w1_f29_fops = { .add_slave = w1_f29_disable_test_mode, .groups = w1_f29_groups, };
drivers/w1/slaves/w1_ds2413.c +1 -1
@@ -143,7 +143,7 @@ static const struct attribute_group *w1_f3a_groups[] = { NULL, }; -static struct w1_family_ops w1_f3a_fops = { +static const struct w1_family_ops w1_f3a_fops = { .groups = w1_f3a_groups, };
drivers/w1/slaves/w1_ds2423.c +1 -1
@@ -117,7 +117,7 @@ static struct attribute *w1_f1d_attrs[] = { };ATTRIBUTE_GROUPS (w1_f1d); -static struct w1_family_ops w1_f1d_fops = { +static const struct w1_family_ops w1_f1d_fops = { .groups = w1_f1d_groups, };
drivers/w1/slaves/w1_ds2430.c +1 -1
@@ -279,7 +279,7 @@ static const struct attribute_group *w1_f14_groups[] = { NULL, }; -static struct w1_family_ops w1_f14_fops = { +static const struct w1_family_ops w1_f14_fops = { .groups = w1_f14_groups, };
drivers/w1/slaves/w1_ds2431.c +1 -1
@@ -278,7 +278,7 @@ static const struct attribute_group *w1_f2d_groups[] = { NULL, }; -static struct w1_family_ops w1_f2d_fops = { +static const struct w1_family_ops w1_f2d_fops = { .groups = w1_f2d_groups, };
drivers/w1/slaves/w1_ds2433.c +1 -1
@@ -288,7 +288,7 @@ static void w1_f23_remove_slave(struct w1_slave *sl)#endif /* CONFIG_W1_SLAVE_DS2433_CRC */ } -static struct w1_family_ops w1_f23_fops = { +static const struct w1_family_ops w1_f23_fops = { .add_slave = w1_f23_add_slave, .remove_slave = w1_f23_remove_slave, .groups = w1_f23_groups,
drivers/w1/slaves/w1_ds2438.c +1 -1
@@ -412,7 +412,7 @@ static const struct attribute_group *w1_ds2438_groups[] = { NULL, }; -static struct w1_family_ops w1_ds2438_fops = { +static const struct w1_family_ops w1_ds2438_fops = { .groups = w1_ds2438_groups, };
drivers/w1/slaves/w1_ds250x.c +1 -1
@@ -215,7 +215,7 @@ static int w1_eprom_add_slave(struct w1_slave *sl)return PTR_ERR_OR_ZERO (nvmem); } -static struct w1_family_ops w1_eprom_fops = { +static const struct w1_family_ops w1_eprom_fops = { .add_slave = w1_eprom_add_slave, };
drivers/w1/slaves/w1_ds2780.c +1 -1
@@ -141,7 +141,7 @@ static void w1_ds2780_remove_slave(struct w1_slave *sl)platform_device_unregister (pdev); } -static struct w1_family_ops w1_ds2780_fops = { +static const struct w1_family_ops w1_ds2780_fops = { .add_slave = w1_ds2780_add_slave, .remove_slave = w1_ds2780_remove_slave, .groups = w1_ds2780_groups,
drivers/w1/slaves/w1_ds2781.c +1 -1
@@ -138,7 +138,7 @@ static void w1_ds2781_remove_slave(struct w1_slave *sl)platform_device_unregister (pdev); } -static struct w1_family_ops w1_ds2781_fops = { +static const struct w1_family_ops w1_ds2781_fops = { .add_slave = w1_ds2781_add_slave, .remove_slave = w1_ds2781_remove_slave, .groups = w1_ds2781_groups,
drivers/w1/slaves/w1_ds2805.c +1 -1
@@ -281,7 +281,7 @@ static void w1_f0d_remove_slave(struct w1_slave *sl)sysfs_remove_bin_file (&sl->dev.kobj, &w1_f0d_bin_attr); } -static struct w1_family_ops w1_f0d_fops = { +static const struct w1_family_ops w1_f0d_fops = { .add_slave = w1_f0d_add_slave, .remove_slave = w1_f0d_remove_slave, };
drivers/w1/slaves/w1_ds28e04.c +1 -1
@@ -410,7 +410,7 @@ static void w1_f1C_remove_slave(struct w1_slave *sl) sl->family_data =NULL ; } -static struct w1_family_ops w1_f1C_fops = { +static const struct w1_family_ops w1_f1C_fops = { .add_slave = w1_f1C_add_slave, .remove_slave = w1_f1C_remove_slave, .groups = w1_f1C_groups,
drivers/w1/slaves/w1_ds28e17.c +1 -1
@@ -741,7 +741,7 @@ static void w1_f19_remove_slave(struct w1_slave *sl)/* Declarations within the w1 subsystem. */ -static struct w1_family_ops w1_f19_fops = { +static const struct w1_family_ops w1_f19_fops = { .add_slave = w1_f19_add_slave, .remove_slave = w1_f19_remove_slave, .groups = w1_f19_groups,
drivers/w1/slaves/w1_therm.c +3 -3
@@ -409,21 +409,21 @@ static const struct hwmon_chip_info w1_chip_info = {/* Family operations */ -static struct w1_family_ops w1_therm_fops = { +static const struct w1_family_ops w1_therm_fops = { .add_slave = w1_therm_add_slave, .remove_slave = w1_therm_remove_slave, .groups = w1_therm_groups, .chip_info =W1_CHIPINFO , }; -static struct w1_family_ops w1_ds18s20_fops = { +static const struct w1_family_ops w1_ds18s20_fops = { .add_slave = w1_therm_add_slave, .remove_slave = w1_therm_remove_slave, .groups = w1_ds18s20_groups, .chip_info =W1_CHIPINFO , }; -static struct w1_family_ops w1_ds28ea00_fops = { +static const struct w1_family_ops w1_ds28ea00_fops = { .add_slave = w1_therm_add_slave, .remove_slave = w1_therm_remove_slave, .groups = w1_ds28ea00_groups,
drivers/w1/w1.c +1 -1
@@ -160,7 +160,7 @@ static const struct attribute_group *w1_slave_default_groups[] = { NULL, }; -static struct w1_family_ops w1_default_fops = { +static const struct w1_family_ops w1_default_fops = { .groups = w1_slave_default_groups, };
Signature · 1 line
2.28.0
PATCH 3/3 power: supply: Constify static w1_family_ops structs
In reply to Rikard FalkebornThe only usage of these structs is to assign their address to the fops field in the w1_family struct, which is a const pointer. Make them const to allow the compiler to put them in read-only memory.
This was done with the following Coccinelle semantic patch
(http://coccinelle.lip6.fr/):
// <smpl>
@r1 disable optional_qualifier @
identifier i;
position p;
@@
static struct w1_family_ops i@p = {...};
@ok1@
identifier r1.i;
position p;
identifier s;
@@
static struct w1_family s = {
.fops=&i@p,
};@bad1@
position p!={r1.p,ok1.p};
identifier r1.i;
@@
i@p
@depends on !bad1 disable optional_qualifier@
identifier r1.i;
@@
static
+const
struct w1_family_ops i={};
// </smpl>
--- drivers/power/supply/bq27xxx_battery_hdq.c | 2 +- drivers/power/supply/ds2760_battery.c | 2 +- drivers/power/supply/max1721x_battery.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
drivers/power/supply/bq27xxx_battery_hdq.c +1 -1
@@ -97,7 +97,7 @@ static void bq27xxx_battery_hdq_remove_slave(struct w1_slave *sl)bq27xxx_battery_teardown (di); } -static struct w1_family_ops bq27xxx_battery_hdq_fops = { +static const struct w1_family_ops bq27xxx_battery_hdq_fops = { .add_slave = bq27xxx_battery_hdq_add_slave, .remove_slave = bq27xxx_battery_hdq_remove_slave, };
drivers/power/supply/ds2760_battery.c +1 -1
@@ -795,7 +795,7 @@ static const struct of_device_id w1_ds2760_of_ids[] = { };#endif -static struct w1_family_ops w1_ds2760_fops = { +static const struct w1_family_ops w1_ds2760_fops = { .add_slave = w1_ds2760_add_slave, .remove_slave = w1_ds2760_remove_slave, .groups = w1_ds2760_groups,
drivers/power/supply/max1721x_battery.c +1 -1
@@ -431,7 +431,7 @@ static int devm_w1_max1721x_add_device(struct w1_slave *sl)return 0 ; } -static struct w1_family_ops w1_max1721x_fops = { +static const struct w1_family_ops w1_max1721x_fops = { .add_slave = devm_w1_max1721x_add_device, };
Signature · 1 line
2.28.0
Re: [PATCH 3/3] power: supply: Constify static w1_family_ops structs
Hi,
Quoted 37 lines · Rikard Falkeborn
The only usage of these structs is to assign their address to the fops field in the
w1_familystruct, which is a const pointer. Make them const to allow the compiler to put them in read-only memory.This was done with the following Coccinelle semantic patch
(http://coccinelle.lip6.fr/):// <smpl>
@r1 disableoptional_qualifier@
identifier i;
position p;
@@
static structw1_family_opsi@p = {...};@ok1@
identifier r1.i;
position p;
identifier s;
@@static struct w1_family s = { .fops=&i@p, };@bad1@
position p!={r1.p,ok1.p};
identifier r1.i;
@@
i@p@depends on !bad1 disable optional_qualifier@
identifier r1.i;
@@
static
+const
structw1_family_opsi={};
// </smpl>
Quoted 2 lines
Signed-off-by Rikard Falkeborn <[email protected]>---
I suggest that this simply goes through the w1 tree together with the other patches:
-- Sebastian
Quoted 47 lines
drivers/power/supply/bq27xxx_battery_hdq.c | 2 +- drivers/power/supply/ds2760_battery.c | 2 +- drivers/power/supply/max1721x_battery.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)drivers/power/supply/bq27xxx_battery_hdq.c +1
-1@@ -97,7 +97,7 @@ static void bq27xxx_battery_hdq_remove_slave(struct w1_slave *sl)bq27xxx_battery_teardown (di); } -static struct w1_family_ops bq27xxx_battery_hdq_fops = { +static const struct w1_family_ops bq27xxx_battery_hdq_fops = { .add_slave = bq27xxx_battery_hdq_add_slave, .remove_slave = bq27xxx_battery_hdq_remove_slave, };drivers/power/supply/ds2760_battery.c +1
-1@@ -795,7 +795,7 @@ static const struct of_device_id w1_ds2760_of_ids[] = { };#endif -static struct w1_family_ops w1_ds2760_fops = { +static const struct w1_family_ops w1_ds2760_fops = { .add_slave = w1_ds2760_add_slave, .remove_slave = w1_ds2760_remove_slave, .groups = w1_ds2760_groups,drivers/power/supply/max1721x_battery.c +1
-1@@ -431,7 +431,7 @@ static int devm_w1_max1721x_add_device(struct w1_slave *sl)return 0 ; } -static struct w1_family_ops w1_max1721x_fops = { +static const struct w1_family_ops w1_max1721x_fops = { .add_slave = devm_w1_max1721x_add_device, };Signature · 1 line
2.28.0