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 int 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;
80 /* Count loaded sections and allocate structures */
81 for (i = 0; i < info->hdr->e_shnum; i++)
82 if (!sect_empty(&info->sechdrs[i]))
84 size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
85 sizeof(sect_attrs->grp.bin_attrs[0]));
86 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
87 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
91 /* Setup section attributes. */
92 sect_attrs->grp.name = "sections";
93 sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
95 sect_attrs->nsections = 0;
96 sattr = §_attrs->attrs[0];
97 gattr = §_attrs->grp.bin_attrs[0];
98 for (i = 0; i < info->hdr->e_shnum; i++) {
99 Elf_Shdr *sec = &info->sechdrs[i];
103 sysfs_bin_attr_init(&sattr->battr);
104 sattr->address = sec->sh_addr;
105 sattr->battr.attr.name =
106 kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
107 if (!sattr->battr.attr.name) {
111 sect_attrs->nsections++;
112 sattr->battr.read = module_sect_read;
113 sattr->battr.size = MODULE_SECT_READ_SIZE;
114 sattr->battr.attr.mode = 0400;
115 *(gattr++) = &(sattr++)->battr;
119 ret = sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp);
123 mod->sect_attrs = sect_attrs;
126 free_sect_attrs(sect_attrs);
130 static void remove_sect_attrs(struct module *mod)
132 if (mod->sect_attrs) {
133 sysfs_remove_group(&mod->mkobj.kobj,
134 &mod->sect_attrs->grp);
136 * We are positive that no one is using any sect attrs
137 * at this point. Deallocate immediately.
139 free_sect_attrs(mod->sect_attrs);
140 mod->sect_attrs = NULL;
145 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
148 struct module_notes_attrs {
151 struct bin_attribute attrs[] __counted_by(notes);
154 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
157 if (notes_attrs->dir) {
159 sysfs_remove_bin_file(notes_attrs->dir,
160 ¬es_attrs->attrs[i]);
161 kobject_put(notes_attrs->dir);
166 static int add_notes_attrs(struct module *mod, const struct load_info *info)
168 unsigned int notes, loaded, i;
169 struct module_notes_attrs *notes_attrs;
170 struct bin_attribute *nattr;
173 /* Count notes sections and allocate structures. */
175 for (i = 0; i < info->hdr->e_shnum; i++)
176 if (!sect_empty(&info->sechdrs[i]) &&
177 info->sechdrs[i].sh_type == SHT_NOTE)
183 notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
188 notes_attrs->notes = notes;
189 nattr = ¬es_attrs->attrs[0];
190 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
191 if (sect_empty(&info->sechdrs[i]))
193 if (info->sechdrs[i].sh_type == SHT_NOTE) {
194 sysfs_bin_attr_init(nattr);
195 nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name;
196 nattr->attr.mode = 0444;
197 nattr->size = info->sechdrs[i].sh_size;
198 nattr->private = (void *)info->sechdrs[i].sh_addr;
199 nattr->read = sysfs_bin_attr_simple_read;
205 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
206 if (!notes_attrs->dir) {
211 for (i = 0; i < notes; ++i) {
212 ret = sysfs_create_bin_file(notes_attrs->dir, ¬es_attrs->attrs[i]);
217 mod->notes_attrs = notes_attrs;
221 free_notes_attrs(notes_attrs, i);
225 static void remove_notes_attrs(struct module *mod)
227 if (mod->notes_attrs)
228 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
231 #else /* !CONFIG_KALLSYMS */
232 static inline int add_sect_attrs(struct module *mod, const struct load_info *info)
236 static inline void remove_sect_attrs(struct module *mod) { }
237 static inline int add_notes_attrs(struct module *mod, const struct load_info *info)
241 static inline void remove_notes_attrs(struct module *mod) { }
242 #endif /* CONFIG_KALLSYMS */
244 static void del_usage_links(struct module *mod)
246 #ifdef CONFIG_MODULE_UNLOAD
247 struct module_use *use;
249 mutex_lock(&module_mutex);
250 list_for_each_entry(use, &mod->target_list, target_list)
251 sysfs_remove_link(use->target->holders_dir, mod->name);
252 mutex_unlock(&module_mutex);
256 static int add_usage_links(struct module *mod)
259 #ifdef CONFIG_MODULE_UNLOAD
260 struct module_use *use;
262 mutex_lock(&module_mutex);
263 list_for_each_entry(use, &mod->target_list, target_list) {
264 ret = sysfs_create_link(use->target->holders_dir,
265 &mod->mkobj.kobj, mod->name);
269 mutex_unlock(&module_mutex);
271 del_usage_links(mod);
276 static void module_remove_modinfo_attrs(struct module *mod, int end)
278 struct module_attribute *attr;
281 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
282 if (end >= 0 && i > end)
284 /* pick a field to test for end of list */
285 if (!attr->attr.name)
287 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
291 kfree(mod->modinfo_attrs);
294 static int module_add_modinfo_attrs(struct module *mod)
296 struct module_attribute *attr;
297 struct module_attribute *temp_attr;
301 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
302 (modinfo_attrs_count + 1)),
304 if (!mod->modinfo_attrs)
307 temp_attr = mod->modinfo_attrs;
308 for (i = 0; (attr = modinfo_attrs[i]); i++) {
309 if (!attr->test || attr->test(mod)) {
310 memcpy(temp_attr, attr, sizeof(*temp_attr));
311 sysfs_attr_init(&temp_attr->attr);
312 error = sysfs_create_file(&mod->mkobj.kobj,
324 module_remove_modinfo_attrs(mod, --i);
326 kfree(mod->modinfo_attrs);
330 static void mod_kobject_put(struct module *mod)
332 DECLARE_COMPLETION_ONSTACK(c);
334 mod->mkobj.kobj_completion = &c;
335 kobject_put(&mod->mkobj.kobj);
336 wait_for_completion(&c);
339 static int mod_sysfs_init(struct module *mod)
342 struct kobject *kobj;
345 pr_err("%s: module sysfs not initialized\n", mod->name);
350 kobj = kset_find_obj(module_kset, mod->name);
352 pr_err("%s: module is already loaded\n", mod->name);
358 mod->mkobj.mod = mod;
360 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
361 mod->mkobj.kobj.kset = module_kset;
362 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
365 mod_kobject_put(mod);
371 int mod_sysfs_setup(struct module *mod,
372 const struct load_info *info,
373 struct kernel_param *kparam,
374 unsigned int num_params)
378 err = mod_sysfs_init(mod);
382 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
383 if (!mod->holders_dir) {
388 err = module_param_sysfs_setup(mod, kparam, num_params);
390 goto out_unreg_holders;
392 err = module_add_modinfo_attrs(mod);
394 goto out_unreg_param;
396 err = add_usage_links(mod);
398 goto out_unreg_modinfo_attrs;
400 err = add_sect_attrs(mod, info);
402 goto out_del_usage_links;
404 err = add_notes_attrs(mod, info);
406 goto out_unreg_sect_attrs;
410 out_unreg_sect_attrs:
411 remove_sect_attrs(mod);
413 del_usage_links(mod);
414 out_unreg_modinfo_attrs:
415 module_remove_modinfo_attrs(mod, -1);
417 module_param_sysfs_remove(mod);
419 kobject_put(mod->holders_dir);
421 mod_kobject_put(mod);
426 static void mod_sysfs_fini(struct module *mod)
428 remove_notes_attrs(mod);
429 remove_sect_attrs(mod);
430 mod_kobject_put(mod);
433 void mod_sysfs_teardown(struct module *mod)
435 del_usage_links(mod);
436 module_remove_modinfo_attrs(mod, -1);
437 module_param_sysfs_remove(mod);
438 kobject_put(mod->mkobj.drivers_dir);
439 kobject_put(mod->holders_dir);
443 void init_param_lock(struct module *mod)
445 mutex_init(&mod->param_lock);