1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2008 Rusty Russell
8 #include <linux/module.h>
9 #include <linux/kernel.h>
11 #include <linux/sysfs.h>
12 #include <linux/slab.h>
13 #include <linux/kallsyms.h>
14 #include <linux/mutex.h>
18 * /sys/module/foo/sections stuff
21 #ifdef CONFIG_KALLSYMS
22 struct module_sect_attr {
23 struct bin_attribute battr;
24 unsigned long address;
27 struct module_sect_attrs {
28 struct attribute_group grp;
29 unsigned int nsections;
30 struct module_sect_attr attrs[];
33 #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
34 static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
35 struct bin_attribute *battr,
36 char *buf, loff_t pos, size_t count)
38 struct module_sect_attr *sattr =
39 container_of(battr, struct module_sect_attr, battr);
40 char bounce[MODULE_SECT_READ_SIZE + 1];
47 * Since we're a binary read handler, we must account for the
48 * trailing NUL byte that sprintf will write: if "buf" is
49 * too small to hold the NUL, or the NUL is exactly the last
50 * byte, the read will look like it got truncated by one byte.
51 * Since there is no way to ask sprintf nicely to not write
52 * the NUL, we have to use a bounce buffer.
54 wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",
55 kallsyms_show_value(file->f_cred)
56 ? (void *)sattr->address : NULL);
57 count = min(count, wrote);
58 memcpy(buf, bounce, count);
63 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
67 for (section = 0; section < sect_attrs->nsections; section++)
68 kfree(sect_attrs->attrs[section].battr.attr.name);
72 static void add_sect_attrs(struct module *mod, const struct load_info *info)
74 unsigned int nloaded = 0, i, size[2];
75 struct module_sect_attrs *sect_attrs;
76 struct module_sect_attr *sattr;
77 struct bin_attribute **gattr;
79 /* Count loaded sections and allocate structures */
80 for (i = 0; i < info->hdr->e_shnum; i++)
81 if (!sect_empty(&info->sechdrs[i]))
83 size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
84 sizeof(sect_attrs->grp.bin_attrs[0]));
85 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
86 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
90 /* Setup section attributes. */
91 sect_attrs->grp.name = "sections";
92 sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
94 sect_attrs->nsections = 0;
95 sattr = §_attrs->attrs[0];
96 gattr = §_attrs->grp.bin_attrs[0];
97 for (i = 0; i < info->hdr->e_shnum; i++) {
98 Elf_Shdr *sec = &info->sechdrs[i];
102 sysfs_bin_attr_init(&sattr->battr);
103 sattr->address = sec->sh_addr;
104 sattr->battr.attr.name =
105 kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
106 if (!sattr->battr.attr.name)
108 sect_attrs->nsections++;
109 sattr->battr.read = module_sect_read;
110 sattr->battr.size = MODULE_SECT_READ_SIZE;
111 sattr->battr.attr.mode = 0400;
112 *(gattr++) = &(sattr++)->battr;
116 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
119 mod->sect_attrs = sect_attrs;
122 free_sect_attrs(sect_attrs);
125 static void remove_sect_attrs(struct module *mod)
127 if (mod->sect_attrs) {
128 sysfs_remove_group(&mod->mkobj.kobj,
129 &mod->sect_attrs->grp);
131 * We are positive that no one is using any sect attrs
132 * at this point. Deallocate immediately.
134 free_sect_attrs(mod->sect_attrs);
135 mod->sect_attrs = NULL;
140 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
143 struct module_notes_attrs {
146 struct bin_attribute attrs[] __counted_by(notes);
149 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
152 if (notes_attrs->dir) {
154 sysfs_remove_bin_file(notes_attrs->dir,
155 ¬es_attrs->attrs[i]);
156 kobject_put(notes_attrs->dir);
161 static void add_notes_attrs(struct module *mod, const struct load_info *info)
163 unsigned int notes, loaded, i;
164 struct module_notes_attrs *notes_attrs;
165 struct bin_attribute *nattr;
167 /* failed to create section attributes, so can't create notes */
168 if (!mod->sect_attrs)
171 /* Count notes sections and allocate structures. */
173 for (i = 0; i < info->hdr->e_shnum; i++)
174 if (!sect_empty(&info->sechdrs[i]) &&
175 info->sechdrs[i].sh_type == SHT_NOTE)
181 notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
186 notes_attrs->notes = notes;
187 nattr = ¬es_attrs->attrs[0];
188 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
189 if (sect_empty(&info->sechdrs[i]))
191 if (info->sechdrs[i].sh_type == SHT_NOTE) {
192 sysfs_bin_attr_init(nattr);
193 nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name;
194 nattr->attr.mode = 0444;
195 nattr->size = info->sechdrs[i].sh_size;
196 nattr->private = (void *)info->sechdrs[i].sh_addr;
197 nattr->read = sysfs_bin_attr_simple_read;
203 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
204 if (!notes_attrs->dir)
207 for (i = 0; i < notes; ++i)
208 if (sysfs_create_bin_file(notes_attrs->dir,
209 ¬es_attrs->attrs[i]))
212 mod->notes_attrs = notes_attrs;
216 free_notes_attrs(notes_attrs, i);
219 static void remove_notes_attrs(struct module *mod)
221 if (mod->notes_attrs)
222 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
225 #else /* !CONFIG_KALLSYMS */
226 static inline void add_sect_attrs(struct module *mod, const struct load_info *info) { }
227 static inline void remove_sect_attrs(struct module *mod) { }
228 static inline void add_notes_attrs(struct module *mod, const struct load_info *info) { }
229 static inline void remove_notes_attrs(struct module *mod) { }
230 #endif /* CONFIG_KALLSYMS */
232 static void del_usage_links(struct module *mod)
234 #ifdef CONFIG_MODULE_UNLOAD
235 struct module_use *use;
237 mutex_lock(&module_mutex);
238 list_for_each_entry(use, &mod->target_list, target_list)
239 sysfs_remove_link(use->target->holders_dir, mod->name);
240 mutex_unlock(&module_mutex);
244 static int add_usage_links(struct module *mod)
247 #ifdef CONFIG_MODULE_UNLOAD
248 struct module_use *use;
250 mutex_lock(&module_mutex);
251 list_for_each_entry(use, &mod->target_list, target_list) {
252 ret = sysfs_create_link(use->target->holders_dir,
253 &mod->mkobj.kobj, mod->name);
257 mutex_unlock(&module_mutex);
259 del_usage_links(mod);
264 static void module_remove_modinfo_attrs(struct module *mod, int end)
266 struct module_attribute *attr;
269 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
270 if (end >= 0 && i > end)
272 /* pick a field to test for end of list */
273 if (!attr->attr.name)
275 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
279 kfree(mod->modinfo_attrs);
282 static int module_add_modinfo_attrs(struct module *mod)
284 struct module_attribute *attr;
285 struct module_attribute *temp_attr;
289 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
290 (modinfo_attrs_count + 1)),
292 if (!mod->modinfo_attrs)
295 temp_attr = mod->modinfo_attrs;
296 for (i = 0; (attr = modinfo_attrs[i]); i++) {
297 if (!attr->test || attr->test(mod)) {
298 memcpy(temp_attr, attr, sizeof(*temp_attr));
299 sysfs_attr_init(&temp_attr->attr);
300 error = sysfs_create_file(&mod->mkobj.kobj,
312 module_remove_modinfo_attrs(mod, --i);
314 kfree(mod->modinfo_attrs);
318 static void mod_kobject_put(struct module *mod)
320 DECLARE_COMPLETION_ONSTACK(c);
322 mod->mkobj.kobj_completion = &c;
323 kobject_put(&mod->mkobj.kobj);
324 wait_for_completion(&c);
327 static int mod_sysfs_init(struct module *mod)
330 struct kobject *kobj;
333 pr_err("%s: module sysfs not initialized\n", mod->name);
338 kobj = kset_find_obj(module_kset, mod->name);
340 pr_err("%s: module is already loaded\n", mod->name);
346 mod->mkobj.mod = mod;
348 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
349 mod->mkobj.kobj.kset = module_kset;
350 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
353 mod_kobject_put(mod);
359 int mod_sysfs_setup(struct module *mod,
360 const struct load_info *info,
361 struct kernel_param *kparam,
362 unsigned int num_params)
366 err = mod_sysfs_init(mod);
370 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
371 if (!mod->holders_dir) {
376 err = module_param_sysfs_setup(mod, kparam, num_params);
378 goto out_unreg_holders;
380 err = module_add_modinfo_attrs(mod);
382 goto out_unreg_param;
384 err = add_usage_links(mod);
386 goto out_unreg_modinfo_attrs;
388 add_sect_attrs(mod, info);
389 add_notes_attrs(mod, info);
393 out_unreg_modinfo_attrs:
394 module_remove_modinfo_attrs(mod, -1);
396 module_param_sysfs_remove(mod);
398 kobject_put(mod->holders_dir);
400 mod_kobject_put(mod);
405 static void mod_sysfs_fini(struct module *mod)
407 remove_notes_attrs(mod);
408 remove_sect_attrs(mod);
409 mod_kobject_put(mod);
412 void mod_sysfs_teardown(struct module *mod)
414 del_usage_links(mod);
415 module_remove_modinfo_attrs(mod, -1);
416 module_param_sysfs_remove(mod);
417 kobject_put(mod->mkobj.drivers_dir);
418 kobject_put(mod->holders_dir);
422 void init_param_lock(struct module *mod)
424 mutex_init(&mod->param_lock);