Skip to content

Commit f3227ff

Browse files
t-8chpetrpavlu
authored andcommitted
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. While at it, also constify 'modinfo_attrs_count'. Signed-off-by: Thomas Weißschuh <[email protected]> Reviewed-by: Petr Pavlu <[email protected]> Link: https://lore.kernel.org/r/20241216-sysfs-const-attr-module-v1-3-3790b53e0abf@weissschuh.net Signed-off-by: Petr Pavlu <[email protected]>
1 parent 38e3fe6 commit f3227ff

File tree

5 files changed

+34
-34
lines changed

5 files changed

+34
-34
lines changed

include/linux/module.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ struct module_kobject {
5252

5353
struct module_attribute {
5454
struct attribute attr;
55-
ssize_t (*show)(struct module_attribute *, struct module_kobject *,
55+
ssize_t (*show)(const struct module_attribute *, struct module_kobject *,
5656
char *);
57-
ssize_t (*store)(struct module_attribute *, struct module_kobject *,
57+
ssize_t (*store)(const struct module_attribute *, struct module_kobject *,
5858
const char *, size_t count);
5959
void (*setup)(struct module *, const char *);
6060
int (*test)(struct module *);
@@ -67,10 +67,10 @@ struct module_version_attribute {
6767
const char *version;
6868
};
6969

70-
extern ssize_t __modver_version_show(struct module_attribute *,
70+
extern ssize_t __modver_version_show(const struct module_attribute *,
7171
struct module_kobject *, char *);
7272

73-
extern struct module_attribute module_uevent;
73+
extern const struct module_attribute module_uevent;
7474

7575
/* These are either module local, or the kernel's dummy ones. */
7676
extern int init_module(void);

kernel/module/internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ struct kernel_symbol {
4747
extern struct mutex module_mutex;
4848
extern struct list_head modules;
4949

50-
extern struct module_attribute *modinfo_attrs[];
51-
extern size_t modinfo_attrs_count;
50+
extern const struct module_attribute *const modinfo_attrs[];
51+
extern const size_t modinfo_attrs_count;
5252

5353
/* Provided by the linker */
5454
extern const struct kernel_symbol __start___ksymtab[];

kernel/module/main.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ static void setup_modinfo_##field(struct module *mod, const char *s) \
538538
{ \
539539
mod->field = kstrdup(s, GFP_KERNEL); \
540540
} \
541-
static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
541+
static ssize_t show_modinfo_##field(const struct module_attribute *mattr, \
542542
struct module_kobject *mk, char *buffer) \
543543
{ \
544544
return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
@@ -552,7 +552,7 @@ static void free_modinfo_##field(struct module *mod) \
552552
kfree(mod->field); \
553553
mod->field = NULL; \
554554
} \
555-
static struct module_attribute modinfo_##field = { \
555+
static const struct module_attribute modinfo_##field = { \
556556
.attr = { .name = __stringify(field), .mode = 0444 }, \
557557
.show = show_modinfo_##field, \
558558
.setup = setup_modinfo_##field, \
@@ -842,13 +842,13 @@ void symbol_put_addr(void *addr)
842842
}
843843
EXPORT_SYMBOL_GPL(symbol_put_addr);
844844

845-
static ssize_t show_refcnt(struct module_attribute *mattr,
845+
static ssize_t show_refcnt(const struct module_attribute *mattr,
846846
struct module_kobject *mk, char *buffer)
847847
{
848848
return sprintf(buffer, "%i\n", module_refcount(mk->mod));
849849
}
850850

851-
static struct module_attribute modinfo_refcnt =
851+
static const struct module_attribute modinfo_refcnt =
852852
__ATTR(refcnt, 0444, show_refcnt, NULL);
853853

854854
void __module_get(struct module *module)
@@ -917,7 +917,7 @@ size_t module_flags_taint(unsigned long taints, char *buf)
917917
return l;
918918
}
919919

920-
static ssize_t show_initstate(struct module_attribute *mattr,
920+
static ssize_t show_initstate(const struct module_attribute *mattr,
921921
struct module_kobject *mk, char *buffer)
922922
{
923923
const char *state = "unknown";
@@ -938,10 +938,10 @@ static ssize_t show_initstate(struct module_attribute *mattr,
938938
return sprintf(buffer, "%s\n", state);
939939
}
940940

941-
static struct module_attribute modinfo_initstate =
941+
static const struct module_attribute modinfo_initstate =
942942
__ATTR(initstate, 0444, show_initstate, NULL);
943943

944-
static ssize_t store_uevent(struct module_attribute *mattr,
944+
static ssize_t store_uevent(const struct module_attribute *mattr,
945945
struct module_kobject *mk,
946946
const char *buffer, size_t count)
947947
{
@@ -951,10 +951,10 @@ static ssize_t store_uevent(struct module_attribute *mattr,
951951
return rc ? rc : count;
952952
}
953953

954-
struct module_attribute module_uevent =
954+
const struct module_attribute module_uevent =
955955
__ATTR(uevent, 0200, NULL, store_uevent);
956956

957-
static ssize_t show_coresize(struct module_attribute *mattr,
957+
static ssize_t show_coresize(const struct module_attribute *mattr,
958958
struct module_kobject *mk, char *buffer)
959959
{
960960
unsigned int size = mk->mod->mem[MOD_TEXT].size;
@@ -966,11 +966,11 @@ static ssize_t show_coresize(struct module_attribute *mattr,
966966
return sprintf(buffer, "%u\n", size);
967967
}
968968

969-
static struct module_attribute modinfo_coresize =
969+
static const struct module_attribute modinfo_coresize =
970970
__ATTR(coresize, 0444, show_coresize, NULL);
971971

972972
#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
973-
static ssize_t show_datasize(struct module_attribute *mattr,
973+
static ssize_t show_datasize(const struct module_attribute *mattr,
974974
struct module_kobject *mk, char *buffer)
975975
{
976976
unsigned int size = 0;
@@ -980,11 +980,11 @@ static ssize_t show_datasize(struct module_attribute *mattr,
980980
return sprintf(buffer, "%u\n", size);
981981
}
982982

983-
static struct module_attribute modinfo_datasize =
983+
static const struct module_attribute modinfo_datasize =
984984
__ATTR(datasize, 0444, show_datasize, NULL);
985985
#endif
986986

987-
static ssize_t show_initsize(struct module_attribute *mattr,
987+
static ssize_t show_initsize(const struct module_attribute *mattr,
988988
struct module_kobject *mk, char *buffer)
989989
{
990990
unsigned int size = 0;
@@ -994,10 +994,10 @@ static ssize_t show_initsize(struct module_attribute *mattr,
994994
return sprintf(buffer, "%u\n", size);
995995
}
996996

997-
static struct module_attribute modinfo_initsize =
997+
static const struct module_attribute modinfo_initsize =
998998
__ATTR(initsize, 0444, show_initsize, NULL);
999999

1000-
static ssize_t show_taint(struct module_attribute *mattr,
1000+
static ssize_t show_taint(const struct module_attribute *mattr,
10011001
struct module_kobject *mk, char *buffer)
10021002
{
10031003
size_t l;
@@ -1007,10 +1007,10 @@ static ssize_t show_taint(struct module_attribute *mattr,
10071007
return l;
10081008
}
10091009

1010-
static struct module_attribute modinfo_taint =
1010+
static const struct module_attribute modinfo_taint =
10111011
__ATTR(taint, 0444, show_taint, NULL);
10121012

1013-
struct module_attribute *modinfo_attrs[] = {
1013+
const struct module_attribute *const modinfo_attrs[] = {
10141014
&module_uevent,
10151015
&modinfo_version,
10161016
&modinfo_srcversion,
@@ -1027,7 +1027,7 @@ struct module_attribute *modinfo_attrs[] = {
10271027
NULL,
10281028
};
10291029

1030-
size_t modinfo_attrs_count = ARRAY_SIZE(modinfo_attrs);
1030+
const size_t modinfo_attrs_count = ARRAY_SIZE(modinfo_attrs);
10311031

10321032
static const char vermagic[] = VERMAGIC_STRING;
10331033

@@ -1681,7 +1681,7 @@ static void module_license_taint_check(struct module *mod, const char *license)
16811681

16821682
static void setup_modinfo(struct module *mod, struct load_info *info)
16831683
{
1684-
struct module_attribute *attr;
1684+
const struct module_attribute *attr;
16851685
int i;
16861686

16871687
for (i = 0; (attr = modinfo_attrs[i]); i++) {
@@ -1692,7 +1692,7 @@ static void setup_modinfo(struct module *mod, struct load_info *info)
16921692

16931693
static void free_modinfo(struct module *mod)
16941694
{
1695-
struct module_attribute *attr;
1695+
const struct module_attribute *attr;
16961696
int i;
16971697

16981698
for (i = 0; (attr = modinfo_attrs[i]); i++) {

kernel/module/sysfs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static int add_usage_links(struct module *mod)
275275

276276
static void module_remove_modinfo_attrs(struct module *mod, int end)
277277
{
278-
struct module_attribute *attr;
278+
const struct module_attribute *attr;
279279
int i;
280280

281281
for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
@@ -293,7 +293,7 @@ static void module_remove_modinfo_attrs(struct module *mod, int end)
293293

294294
static int module_add_modinfo_attrs(struct module *mod)
295295
{
296-
struct module_attribute *attr;
296+
const struct module_attribute *attr;
297297
struct module_attribute *temp_attr;
298298
int error = 0;
299299
int i;

kernel/params.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ const struct kernel_param_ops param_ops_string = {
538538
EXPORT_SYMBOL(param_ops_string);
539539

540540
/* sysfs output in /sys/modules/XYZ/parameters/ */
541-
#define to_module_attr(n) container_of(n, struct module_attribute, attr)
541+
#define to_module_attr(n) container_of_const(n, struct module_attribute, attr)
542542
#define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
543543

544544
struct param_attribute
@@ -557,7 +557,7 @@ struct module_param_attrs
557557
#ifdef CONFIG_SYSFS
558558
#define to_param_attr(n) container_of_const(n, struct param_attribute, mattr)
559559

560-
static ssize_t param_attr_show(struct module_attribute *mattr,
560+
static ssize_t param_attr_show(const struct module_attribute *mattr,
561561
struct module_kobject *mk, char *buf)
562562
{
563563
int count;
@@ -573,7 +573,7 @@ static ssize_t param_attr_show(struct module_attribute *mattr,
573573
}
574574

575575
/* sysfs always hands a nul-terminated string in buf. We rely on that. */
576-
static ssize_t param_attr_store(struct module_attribute *mattr,
576+
static ssize_t param_attr_store(const struct module_attribute *mattr,
577577
struct module_kobject *mk,
578578
const char *buf, size_t len)
579579
{
@@ -857,7 +857,7 @@ static void __init param_sysfs_builtin(void)
857857
}
858858
}
859859

860-
ssize_t __modver_version_show(struct module_attribute *mattr,
860+
ssize_t __modver_version_show(const struct module_attribute *mattr,
861861
struct module_kobject *mk, char *buf)
862862
{
863863
const struct module_version_attribute *vattr =
@@ -892,7 +892,7 @@ static ssize_t module_attr_show(struct kobject *kobj,
892892
struct attribute *attr,
893893
char *buf)
894894
{
895-
struct module_attribute *attribute;
895+
const struct module_attribute *attribute;
896896
struct module_kobject *mk;
897897
int ret;
898898

@@ -911,7 +911,7 @@ static ssize_t module_attr_store(struct kobject *kobj,
911911
struct attribute *attr,
912912
const char *buf, size_t len)
913913
{
914-
struct module_attribute *attribute;
914+
const struct module_attribute *attribute;
915915
struct module_kobject *mk;
916916
int ret;
917917

0 commit comments

Comments
 (0)