module: Constify 'struct module_attribute'
cover module: Constify 'struct module_attribute'
These structs are never modified, move them to read-only memory. This makes the API clearer and also prepares for the constification of 'struct attribute' itself.
---
Thomas Weißschuh (3):
params: Prepare for 'const struct module_attribute *'
module: Handle 'struct module_version_attribute' as const
module: Constify 'struct module_attribute'
include/linux/module.h | 10 +++++----- kernel/module/internal.h | 4 ++-- kernel/module/main.c | 40 ++++++++++++++++++++-------------------- kernel/module/sysfs.c | 4 ++-- kernel/params.c | 22 +++++++++++----------- 5 files changed, 40 insertions(+), 40 deletions(-)
---
base-commit: 78d4f34e2115b517bcbfe7ec0d018bbbb6f9b0b8
change-id: 20241204-sysfs-const-attr-module-927afe76eda4
Best regards,
Signature · 1 line
Thomas Weißschuh <[email protected]>
PATCH 1/3 params: Prepare for 'const struct module_attribute *'
The 'struct module_attribute' sysfs callbacks are about to change to receive a 'const struct module_attribute *' parameter. Prepare for that by avoid casting away the constness through container_of() and using const pointers to 'struct param_attribute'.
--- kernel/params.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/params.c +3 -3
@@ -555,13 +555,13 @@ struct module_param_attrs };#ifdef CONFIG_SYSFS -#define to_param_attr (n) container_of(n, struct param_attribute, mattr) +#define to_param_attr (n) container_of_const(n, struct param_attribute, mattr)static ssize_t param_attr_show (struct module_attribute *mattr,struct module_kobject *mk,char *buf) {int count; -struct param_attribute *attribute =to_param_attr (mattr); +const struct param_attribute *attribute =to_param_attr (mattr);if (!attribute->param->ops->get)return -EPERM ;
@@ -578,7 +578,7 @@ static ssize_t param_attr_store(struct module_attribute *mattr, const char *buf, size_t len) { int err; -struct param_attribute *attribute =to_param_attr (mattr); +const struct param_attribute *attribute =to_param_attr (mattr);if (!attribute->param->ops->set)return -EPERM ;
Signature · 1 line
2.47.1
PATCH 2/3 module: Handle 'struct module_version_attribute' as const
In reply to Thomas WeißschuhThe structure is always read-only due to its placement in the read-only section __modver. Reflect this at its usage sites.
Also prepare for the const handling of 'struct module_attribute' itself.
--- include/linux/module.h | 2 +- kernel/params.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
include/linux/module.h +1 -1
@@ -275,7 +275,7 @@ extern typeof(name) __mod_device_table__##type##__##name \#else #define MODULE_VERSION (_version) \ MODULE_INFO(version, _version); \ - static struct module_version_attribute __modver_attr \ + static const struct module_version_attribute __modver_attr \ __used __section("__modver") \ __aligned(__alignof__(struct module_version_attribute)) \ = { \
kernel/params.c +2 -2
@@ -860,8 +860,8 @@ static void __init param_sysfs_builtin(void)ssize_t __modver_version_show (struct module_attribute *mattr,struct module_kobject *mk,char *buf) { -struct module_version_attribute *vattr = - container_of(mattr, struct module_version_attribute, mattr); +const struct module_version_attribute *vattr = + container_of_const(mattr, struct module_version_attribute, mattr);return scnprintf (buf,PAGE_SIZE ,"%s\n" , vattr->version); }
Signature · 1 line
2.47.1
PATCH 3/3 module: Constify 'struct module_attribute'
In reply to Thomas WeißschuhThese structs are never modified, move them to read-only memory. This makes the API clearer and also prepares for the constification of 'struct attribute' itself.
While at it, also constify 'modinfo_attrs_count'.
--- include/linux/module.h | 8 ++++---- kernel/module/internal.h | 4 ++-- kernel/module/main.c | 40 ++++++++++++++++++++-------------------- kernel/module/sysfs.c | 4 ++-- kernel/params.c | 12 ++++++------ 5 files changed, 34 insertions(+), 34 deletions(-)
include/linux/module.h +4 -4
@@ -52,9 +52,9 @@ struct module_kobject {struct module_attribute {struct attribute attr; -ssize_t (*show)(struct module_attribute *,struct module_kobject *, +ssize_t (*show)(const struct module_attribute *,struct module_kobject *,char *); -ssize_t (*store)(struct module_attribute *,struct module_kobject *, +ssize_t (*store)(const struct module_attribute *,struct module_kobject *,const char *,size_t count);void (*setup)(struct module *,const char *);int (*test)(struct module *);
@@ -67,10 +67,10 @@ struct module_version_attribute {const char *version; }; -extern ssize_t __modver_version_show (struct module_attribute *, +extern ssize_t __modver_version_show (const struct module_attribute *,struct module_kobject *,char *); -extern struct module_attribute module_uevent; +extern const struct module_attribute module_uevent;/* These are either module local, or the kernel's dummy ones. */ extern int init_module (void );
kernel/module/internal.h +2 -2
@@ -47,8 +47,8 @@ struct kernel_symbol {extern struct mutex module_mutex;extern struct list_head modules; -extern struct module_attribute *modinfo_attrs[]; -extern size_t modinfo_attrs_count; +extern const struct module_attribute *const modinfo_attrs[]; +extern const size_t modinfo_attrs_count;/* Provided by the linker */ extern const struct kernel_symbol __start___ksymtab[];
kernel/module/main.c +20 -20
@@ -538,7 +538,7 @@ static void setup_modinfo_##field(struct module *mod, const char *s) \ { \ mod->field =kstrdup (s,GFP_KERNEL ); \ } \ -static ssize_t show_modinfo_##field(struct module_attribute *mattr, \ +static ssize_t show_modinfo_##field(const struct module_attribute *mattr, \ struct module_kobject *mk, char *buffer) \ { \ return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
@@ -552,7 +552,7 @@ static void free_modinfo_##field(struct module *mod) \kfree (mod->field); \ mod->field =NULL ; \ } \ -static struct module_attribute modinfo_##field = { \ +static const struct module_attribute modinfo_##field = { \ .attr = { .name = __stringify(field), .mode = 0444 }, \ .show = show_modinfo_##field, \ .setup = setup_modinfo_##field, \
@@ -842,13 +842,13 @@ void symbol_put_addr(void *addr) }EXPORT_SYMBOL_GPL (symbol_put_addr); -static ssize_t show_refcnt (struct module_attribute *mattr, +static ssize_t show_refcnt (const struct module_attribute *mattr,struct module_kobject *mk,char *buffer) {return sprintf (buffer,"%i\n" ,module_refcount (mk->mod)); } -static struct module_attribute modinfo_refcnt = +static const struct module_attribute modinfo_refcnt =__ATTR (refcnt,0444 , show_refcnt,NULL );void __module_get (struct module *module)
@@ -917,7 +917,7 @@ size_t module_flags_taint(unsigned long taints, char *buf)return l; } -static ssize_t show_initstate (struct module_attribute *mattr, +static ssize_t show_initstate (const struct module_attribute *mattr,struct module_kobject *mk,char *buffer) {const char *state ="unknown" ;
@@ -938,10 +938,10 @@ static ssize_t show_initstate(struct module_attribute *mattr,return sprintf (buffer,"%s\n" , state); } -static struct module_attribute modinfo_initstate = +static const struct module_attribute modinfo_initstate =__ATTR (initstate,0444 , show_initstate,NULL ); -static ssize_t store_uevent (struct module_attribute *mattr, +static ssize_t store_uevent (const struct module_attribute *mattr,struct module_kobject *mk,const char *buffer,size_t count) {
@@ -951,10 +951,10 @@ static ssize_t store_uevent(struct module_attribute *mattr,return rc ? rc : count; } -struct module_attribute module_uevent = +const struct module_attribute module_uevent =__ATTR (uevent,0200 ,NULL , store_uevent); -static ssize_t show_coresize (struct module_attribute *mattr, +static ssize_t show_coresize (const struct module_attribute *mattr,struct module_kobject *mk,char *buffer) {unsigned int size = mk->mod->mem[MOD_TEXT ].size;
@@ -966,11 +966,11 @@ static ssize_t show_coresize(struct module_attribute *mattr,return sprintf (buffer,"%u\n" , size); } -static struct module_attribute modinfo_coresize = +static const struct module_attribute modinfo_coresize =__ATTR (coresize,0444 , show_coresize,NULL );#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC -static ssize_t show_datasize (struct module_attribute *mattr, +static ssize_t show_datasize (const struct module_attribute *mattr,struct module_kobject *mk,char *buffer) {unsigned int size =0 ;
@@ -980,11 +980,11 @@ static ssize_t show_datasize(struct module_attribute *mattr, return sprintf(buffer, "%u\n", size); } -static struct module_attribute modinfo_datasize = +static const struct module_attribute modinfo_datasize =__ATTR (datasize,0444 , show_datasize,NULL );#endif -static ssize_t show_initsize (struct module_attribute *mattr, +static ssize_t show_initsize (const struct module_attribute *mattr,struct module_kobject *mk,char *buffer) {unsigned int size =0 ;
@@ -994,10 +994,10 @@ static ssize_t show_initsize(struct module_attribute *mattr,return sprintf (buffer,"%u\n" , size); } -static struct module_attribute modinfo_initsize = +static const struct module_attribute modinfo_initsize =__ATTR (initsize,0444 , show_initsize,NULL ); -static ssize_t show_taint (struct module_attribute *mattr, +static ssize_t show_taint (const struct module_attribute *mattr,struct module_kobject *mk,char *buffer) {size_t l;
@@ -1007,10 +1007,10 @@ static ssize_t show_taint(struct module_attribute *mattr,return l; } -static struct module_attribute modinfo_taint = +static const struct module_attribute modinfo_taint =__ATTR (taint,0444 , show_taint,NULL ); -struct module_attribute *modinfo_attrs[] = { +const struct module_attribute *const modinfo_attrs[] = { &module_uevent, &modinfo_version, &modinfo_srcversion,
@@ -1027,7 +1027,7 @@ struct module_attribute *modinfo_attrs[] = { NULL, }; -size_t modinfo_attrs_count =ARRAY_SIZE (modinfo_attrs); +const size_t modinfo_attrs_count =ARRAY_SIZE (modinfo_attrs);static const char vermagic[] =VERMAGIC_STRING ;
@@ -1681,7 +1681,7 @@ static void module_license_taint_check(struct module *mod, const char *license)static void setup_modinfo (struct module *mod,struct load_info *info) { -struct module_attribute *attr; +const struct module_attribute *attr;int i;for (i =0 ; (attr = modinfo_attrs[i]); i++) {
@@ -1692,7 +1692,7 @@ static void setup_modinfo(struct module *mod, struct load_info *info)static void free_modinfo (struct module *mod) { -struct module_attribute *attr; +const struct module_attribute *attr;int i;for (i =0 ; (attr = modinfo_attrs[i]); i++) {
kernel/module/sysfs.c +2 -2
@@ -275,7 +275,7 @@ static int add_usage_links(struct module *mod)static void module_remove_modinfo_attrs (struct module *mod,int end) { -struct module_attribute *attr; +const struct module_attribute *attr;int i;for (i =0 ; (attr = &mod->modinfo_attrs[i]); i++) {
@@ -293,7 +293,7 @@ static void module_remove_modinfo_attrs(struct module *mod, int end)static int module_add_modinfo_attrs (struct module *mod) { -struct module_attribute *attr; +const struct module_attribute *attr;struct module_attribute *temp_attr;int error =0 ;int i;
kernel/params.c +6 -6
@@ -538,7 +538,7 @@ const struct kernel_param_ops param_ops_string = {EXPORT_SYMBOL (param_ops_string);/* sysfs output in /sys/modules/XYZ/parameters/ */ -#define to_module_attr (n) container_of(n, struct module_attribute, attr) +#define to_module_attr (n) container_of_const(n, struct module_attribute, attr)#define to_module_kobject (n) container_of(n, struct module_kobject, kobj)struct param_attribute
@@ -557,7 +557,7 @@ struct module_param_attrs#ifdef CONFIG_SYSFS #define to_param_attr (n) container_of_const(n, struct param_attribute, mattr) -static ssize_t param_attr_show (struct module_attribute *mattr, +static ssize_t param_attr_show (const struct module_attribute *mattr,struct module_kobject *mk,char *buf) {int count;
@@ -573,7 +573,7 @@ static ssize_t param_attr_show(struct module_attribute *mattr, }/* sysfs always hands a nul-terminated string in buf. We rely on that. */ -static ssize_t param_attr_store (struct module_attribute *mattr, +static ssize_t param_attr_store (const struct module_attribute *mattr,struct module_kobject *mk,const char *buf,size_t len) {
@@ -857,7 +857,7 @@ static void __init param_sysfs_builtin(void) } } -ssize_t __modver_version_show (struct module_attribute *mattr, +ssize_t __modver_version_show (const struct module_attribute *mattr,struct module_kobject *mk,char *buf) { const struct module_version_attribute *vattr =
@@ -892,7 +892,7 @@ static ssize_t module_attr_show(struct kobject *kobj,struct attribute *attr, char *buf) { - struct module_attribute *attribute; + const struct module_attribute *attribute;struct module_kobject *mk;int ret;
@@ -911,7 +911,7 @@ static ssize_t module_attr_store(struct kobject *kobj,struct attribute *attr, const char *buf, size_t len) { - struct module_attribute *attribute; + const struct module_attribute *attribute;struct module_kobject *mk;int ret;
Signature · 1 line
2.47.1
Quoted 17 lines · Thomas Weißschuh
These structs are never modified, move them to read-only memory. This makes the API clearer and also prepares for the constification of 'struct attribute' itself.
Signed-off-by Thomas Weißschuh <[email protected]>---
Thomas Weißschuh (3):
params: Prepare for 'const structmodule_attribute*'
module: Handle 'structmodule_version_attribute' as const
module: Constify 'structmodule_attribute'include/linux/module.h | 10 +++++----- kernel/module/internal.h | 4 ++-- kernel/module/main.c | 40 ++++++++++++++++++++-------------------- kernel/module/sysfs.c | 4 ++-- kernel/params.c | 22 +++++++++++----------- 5 files changed, 40 insertions(+), 40 deletions(-)
Quoted 3 lines
---
base-commit:78d4f34e2115b517bcbfe7ec0d018bbbb6f9b0b8
change-id: 20241204-sysfs-const-attr-module-927afe76eda4
I'm going to wait for a few days if others want to comment and then plan to queue this on the modules tree for the 6.14 merge window.
Signature · 2 lines
Thanks, Petr