projects/linux·blob v6.11 · refs

425 lines · 9.9 KiB

1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Module sysfs support
4 *
5 * Copyright (C) 2008 Rusty Russell
6 */
7
8#include <linux/module.h>
9#include <linux/kernel.h>
10#include <linux/fs.h>
11#include <linux/sysfs.h>
12#include <linux/slab.h>
13#include <linux/kallsyms.h>
14#include <linux/mutex.h>
15#include "internal.h"
16
17/*
18 * /sys/module/foo/sections stuff
19 * J. Corbet <[email protected]>
20 */
21#ifdef CONFIG_KALLSYMS
22struct module_sect_attr {
23 struct bin_attribute battr;
24 unsigned long address;
25};
26
27struct module_sect_attrs {
28 struct attribute_group grp;
29 unsigned int nsections;
30 struct module_sect_attr attrs[];
31};
32
33#define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
34static 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)
37{
38 struct module_sect_attr *sattr =
39 container_of(battr, struct module_sect_attr, battr);
40 char bounce[MODULE_SECT_READ_SIZE + 1];
41 size_t wrote;
42
43 if (pos != 0)
44 return -EINVAL;
45
46 /*
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.
53 */
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);
59
60 return count;
61}
62
63static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
64{
65 unsigned int section;
66
67 for (section = 0; section < sect_attrs->nsections; section++)
68 kfree(sect_attrs->attrs[section].battr.attr.name);
69 kfree(sect_attrs);
70}
71
72static void add_sect_attrs(struct module *mod, const struct load_info *info)
73{
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;
78
79 /* Count loaded sections and allocate structures */
80 for (i = 0; i < info->hdr->e_shnum; i++)
81 if (!sect_empty(&info->sechdrs[i]))
82 nloaded++;
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);
87 if (!sect_attrs)
88 return;
89
90 /* Setup section attributes. */
91 sect_attrs->grp.name = "sections";
92 sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
93
94 sect_attrs->nsections = 0;
95 sattr = &sect_attrs->attrs[0];
96 gattr = &sect_attrs->grp.bin_attrs[0];
97 for (i = 0; i < info->hdr->e_shnum; i++) {
98 Elf_Shdr *sec = &info->sechdrs[i];
99
100 if (sect_empty(sec))
101 continue;
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)
107 goto out;
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;
113 }
114 *gattr = NULL;
115
116 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
117 goto out;
118
119 mod->sect_attrs = sect_attrs;
120 return;
121out:
122 free_sect_attrs(sect_attrs);
123}
124
125static void remove_sect_attrs(struct module *mod)
126{
127 if (mod->sect_attrs) {
128 sysfs_remove_group(&mod->mkobj.kobj,
129 &mod->sect_attrs->grp);
130 /*
131 * We are positive that no one is using any sect attrs
132 * at this point. Deallocate immediately.
133 */
134 free_sect_attrs(mod->sect_attrs);
135 mod->sect_attrs = NULL;
136 }
137}
138
139/*
140 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
141 */
142
143struct module_notes_attrs {
144 struct kobject *dir;
145 unsigned int notes;
146 struct bin_attribute attrs[] __counted_by(notes);
147};
148
149static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
150 unsigned int i)
151{
152 if (notes_attrs->dir) {
153 while (i-- > 0)
154 sysfs_remove_bin_file(notes_attrs->dir,
155 &notes_attrs->attrs[i]);
156 kobject_put(notes_attrs->dir);
157 }
158 kfree(notes_attrs);
159}
160
161static void add_notes_attrs(struct module *mod, const struct load_info *info)
162{
163 unsigned int notes, loaded, i;
164 struct module_notes_attrs *notes_attrs;
165 struct bin_attribute *nattr;
166
167 /* failed to create section attributes, so can't create notes */
168 if (!mod->sect_attrs)
169 return;
170
171 /* Count notes sections and allocate structures. */
172 notes = 0;
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)
176 ++notes;
177
178 if (notes == 0)
179 return;
180
181 notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
182 GFP_KERNEL);
183 if (!notes_attrs)
184 return;
185
186 notes_attrs->notes = notes;
187 nattr = &notes_attrs->attrs[0];
188 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
189 if (sect_empty(&info->sechdrs[i]))
190 continue;
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;
198 ++nattr;
199 }
200 ++loaded;
201 }
202
203 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
204 if (!notes_attrs->dir)
205 goto out;
206
207 for (i = 0; i < notes; ++i)
208 if (sysfs_create_bin_file(notes_attrs->dir,
209 &notes_attrs->attrs[i]))
210 goto out;
211
212 mod->notes_attrs = notes_attrs;
213 return;
214
215out:
216 free_notes_attrs(notes_attrs, i);
217}
218
219static void remove_notes_attrs(struct module *mod)
220{
221 if (mod->notes_attrs)
222 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
223}
224
225#else /* !CONFIG_KALLSYMS */
226static inline void add_sect_attrs(struct module *mod, const struct load_info *info) { }
227static inline void remove_sect_attrs(struct module *mod) { }
228static inline void add_notes_attrs(struct module *mod, const struct load_info *info) { }
229static inline void remove_notes_attrs(struct module *mod) { }
230#endif /* CONFIG_KALLSYMS */
231
232static void del_usage_links(struct module *mod)
233{
234#ifdef CONFIG_MODULE_UNLOAD
235 struct module_use *use;
236
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);
241#endif
242}
243
244static int add_usage_links(struct module *mod)
245{
246 int ret = 0;
247#ifdef CONFIG_MODULE_UNLOAD
248 struct module_use *use;
249
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);
254 if (ret)
255 break;
256 }
257 mutex_unlock(&module_mutex);
258 if (ret)
259 del_usage_links(mod);
260#endif
261 return ret;
262}
263
264static void module_remove_modinfo_attrs(struct module *mod, int end)
265{
266 struct module_attribute *attr;
267 int i;
268
269 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
270 if (end >= 0 && i > end)
271 break;
272 /* pick a field to test for end of list */
273 if (!attr->attr.name)
274 break;
275 sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
276 if (attr->free)
277 attr->free(mod);
278 }
279 kfree(mod->modinfo_attrs);
280}
281
282static int module_add_modinfo_attrs(struct module *mod)
283{
284 struct module_attribute *attr;
285 struct module_attribute *temp_attr;
286 int error = 0;
287 int i;
288
289 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
290 (modinfo_attrs_count + 1)),
291 GFP_KERNEL);
292 if (!mod->modinfo_attrs)
293 return -ENOMEM;
294
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,
301 &temp_attr->attr);
302 if (error)
303 goto error_out;
304 ++temp_attr;
305 }
306 }
307
308 return 0;
309
310error_out:
311 if (i > 0)
312 module_remove_modinfo_attrs(mod, --i);
313 else
314 kfree(mod->modinfo_attrs);
315 return error;
316}
317
318static void mod_kobject_put(struct module *mod)
319{
320 DECLARE_COMPLETION_ONSTACK(c);
321
322 mod->mkobj.kobj_completion = &c;
323 kobject_put(&mod->mkobj.kobj);
324 wait_for_completion(&c);
325}
326
327static int mod_sysfs_init(struct module *mod)
328{
329 int err;
330 struct kobject *kobj;
331
332 if (!module_kset) {
333 pr_err("%s: module sysfs not initialized\n", mod->name);
334 err = -EINVAL;
335 goto out;
336 }
337
338 kobj = kset_find_obj(module_kset, mod->name);
339 if (kobj) {
340 pr_err("%s: module is already loaded\n", mod->name);
341 kobject_put(kobj);
342 err = -EINVAL;
343 goto out;
344 }
345
346 mod->mkobj.mod = mod;
347
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,
351 "%s", mod->name);
352 if (err)
353 mod_kobject_put(mod);
354
355out:
356 return err;
357}
358
359int mod_sysfs_setup(struct module *mod,
360 const struct load_info *info,
361 struct kernel_param *kparam,
362 unsigned int num_params)
363{
364 int err;
365
366 err = mod_sysfs_init(mod);
367 if (err)
368 goto out;
369
370 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
371 if (!mod->holders_dir) {
372 err = -ENOMEM;
373 goto out_unreg;
374 }
375
376 err = module_param_sysfs_setup(mod, kparam, num_params);
377 if (err)
378 goto out_unreg_holders;
379
380 err = module_add_modinfo_attrs(mod);
381 if (err)
382 goto out_unreg_param;
383
384 err = add_usage_links(mod);
385 if (err)
386 goto out_unreg_modinfo_attrs;
387
388 add_sect_attrs(mod, info);
389 add_notes_attrs(mod, info);
390
391 return 0;
392
393out_unreg_modinfo_attrs:
394 module_remove_modinfo_attrs(mod, -1);
395out_unreg_param:
396 module_param_sysfs_remove(mod);
397out_unreg_holders:
398 kobject_put(mod->holders_dir);
399out_unreg:
400 mod_kobject_put(mod);
401out:
402 return err;
403}
404
405static void mod_sysfs_fini(struct module *mod)
406{
407 remove_notes_attrs(mod);
408 remove_sect_attrs(mod);
409 mod_kobject_put(mod);
410}
411
412void mod_sysfs_teardown(struct module *mod)
413{
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);
419 mod_sysfs_fini(mod);
420}
421
422void init_param_lock(struct module *mod)
423{
424 mutex_init(&mod->param_lock);
425}