| 1 | |
| 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
|
| 22 | struct module_sect_attrs {
|
| 23 | struct attribute_group grp;
|
| 24 | struct bin_attribute attrs[];
|
| 25 | };
|
| 26 |
|
| 27 | #define MODULE_SECT_READ_SIZE (3 + (BITS_PER_LONG / 4))
|
| 28 | static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
|
| 29 | const struct bin_attribute *battr,
|
| 30 | char *buf, loff_t pos, size_t count)
|
| 31 | {
|
| 32 | char bounce[MODULE_SECT_READ_SIZE + 1];
|
| 33 | size_t wrote;
|
| 34 |
|
| 35 | if (pos != 0)
|
| 36 | return -EINVAL;
|
| 37 |
|
| 38 | |
| 39 | * Since we're a binary read handler, we must account for the
|
| 40 | * trailing NUL byte that sprintf will write: if "buf" is
|
| 41 | * too small to hold the NUL, or the NUL is exactly the last
|
| 42 | * byte, the read will look like it got truncated by one byte.
|
| 43 | * Since there is no way to ask sprintf nicely to not write
|
| 44 | * the NUL, we have to use a bounce buffer.
|
| 45 |
|
| 46 | wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",
|
| 47 | kallsyms_show_value(file->f_cred)
|
| 48 | ? battr->private : NULL);
|
| 49 | count = min(count, wrote);
|
| 50 | memcpy(buf, bounce, count);
|
| 51 |
|
| 52 | return count;
|
| 53 | }
|
| 54 |
|
| 55 | static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
|
| 56 | {
|
| 57 | const struct bin_attribute *const *bin_attr;
|
| 58 |
|
| 59 | for (bin_attr = sect_attrs->grp.bin_attrs; *bin_attr; bin_attr++)
|
| 60 | kfree((*bin_attr)->attr.name);
|
| 61 | kfree(sect_attrs->grp.bin_attrs);
|
| 62 | kfree(sect_attrs);
|
| 63 | }
|
| 64 |
|
| 65 | static int add_sect_attrs(struct module *mod, const struct load_info *info)
|
| 66 | {
|
| 67 | struct module_sect_attrs *sect_attrs;
|
| 68 | const struct bin_attribute **gattr;
|
| 69 | struct bin_attribute *sattr;
|
| 70 | unsigned int nloaded = 0, i;
|
| 71 | int ret;
|
| 72 |
|
| 73 |
|
| 74 | for (i = 0; i < info->hdr->e_shnum; i++)
|
| 75 | if (!sect_empty(&info->sechdrs[i]))
|
| 76 | nloaded++;
|
| 77 | sect_attrs = kzalloc(struct_size(sect_attrs, attrs, nloaded), GFP_KERNEL);
|
| 78 | if (!sect_attrs)
|
| 79 | return -ENOMEM;
|
| 80 |
|
| 81 | gattr = kcalloc(nloaded + 1, sizeof(*gattr), GFP_KERNEL);
|
| 82 | if (!gattr) {
|
| 83 | kfree(sect_attrs);
|
| 84 | return -ENOMEM;
|
| 85 | }
|
| 86 |
|
| 87 |
|
| 88 | sect_attrs->grp.name = "sections";
|
| 89 | sect_attrs->grp.bin_attrs = gattr;
|
| 90 |
|
| 91 | sattr = §_attrs->attrs[0];
|
| 92 | for (i = 0; i < info->hdr->e_shnum; i++) {
|
| 93 | Elf_Shdr *sec = &info->sechdrs[i];
|
| 94 |
|
| 95 | if (sect_empty(sec))
|
| 96 | continue;
|
| 97 | sysfs_bin_attr_init(sattr);
|
| 98 | sattr->attr.name =
|
| 99 | kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
|
| 100 | if (!sattr->attr.name) {
|
| 101 | ret = -ENOMEM;
|
| 102 | goto out;
|
| 103 | }
|
| 104 | sattr->read = module_sect_read;
|
| 105 | sattr->private = (void *)sec->sh_addr;
|
| 106 | sattr->size = MODULE_SECT_READ_SIZE;
|
| 107 | sattr->attr.mode = 0400;
|
| 108 | *(gattr++) = sattr++;
|
| 109 | }
|
| 110 |
|
| 111 | ret = sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp);
|
| 112 | if (ret)
|
| 113 | goto out;
|
| 114 |
|
| 115 | mod->sect_attrs = sect_attrs;
|
| 116 | return 0;
|
| 117 | out:
|
| 118 | free_sect_attrs(sect_attrs);
|
| 119 | return ret;
|
| 120 | }
|
| 121 |
|
| 122 | static void remove_sect_attrs(struct module *mod)
|
| 123 | {
|
| 124 | if (mod->sect_attrs) {
|
| 125 | sysfs_remove_group(&mod->mkobj.kobj,
|
| 126 | &mod->sect_attrs->grp);
|
| 127 | |
| 128 | * We are positive that no one is using any sect attrs
|
| 129 | * at this point. Deallocate immediately.
|
| 130 |
|
| 131 | free_sect_attrs(mod->sect_attrs);
|
| 132 | mod->sect_attrs = NULL;
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | |
| 137 | * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
|
| 138 |
|
| 139 |
|
| 140 | struct module_notes_attrs {
|
| 141 | struct attribute_group grp;
|
| 142 | struct bin_attribute attrs[];
|
| 143 | };
|
| 144 |
|
| 145 | static void free_notes_attrs(struct module_notes_attrs *notes_attrs)
|
| 146 | {
|
| 147 | kfree(notes_attrs->grp.bin_attrs);
|
| 148 | kfree(notes_attrs);
|
| 149 | }
|
| 150 |
|
| 151 | static int add_notes_attrs(struct module *mod, const struct load_info *info)
|
| 152 | {
|
| 153 | unsigned int notes, loaded, i;
|
| 154 | struct module_notes_attrs *notes_attrs;
|
| 155 | const struct bin_attribute **gattr;
|
| 156 | struct bin_attribute *nattr;
|
| 157 | int ret;
|
| 158 |
|
| 159 |
|
| 160 | notes = 0;
|
| 161 | for (i = 0; i < info->hdr->e_shnum; i++)
|
| 162 | if (!sect_empty(&info->sechdrs[i]) &&
|
| 163 | info->sechdrs[i].sh_type == SHT_NOTE)
|
| 164 | ++notes;
|
| 165 |
|
| 166 | if (notes == 0)
|
| 167 | return 0;
|
| 168 |
|
| 169 | notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
|
| 170 | GFP_KERNEL);
|
| 171 | if (!notes_attrs)
|
| 172 | return -ENOMEM;
|
| 173 |
|
| 174 | gattr = kcalloc(notes + 1, sizeof(*gattr), GFP_KERNEL);
|
| 175 | if (!gattr) {
|
| 176 | kfree(notes_attrs);
|
| 177 | return -ENOMEM;
|
| 178 | }
|
| 179 |
|
| 180 | notes_attrs->grp.name = "notes";
|
| 181 | notes_attrs->grp.bin_attrs = gattr;
|
| 182 |
|
| 183 | nattr = ¬es_attrs->attrs[0];
|
| 184 | for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
|
| 185 | if (sect_empty(&info->sechdrs[i]))
|
| 186 | continue;
|
| 187 | if (info->sechdrs[i].sh_type == SHT_NOTE) {
|
| 188 | sysfs_bin_attr_init(nattr);
|
| 189 | nattr->attr.name = mod->sect_attrs->attrs[loaded].attr.name;
|
| 190 | nattr->attr.mode = 0444;
|
| 191 | nattr->size = info->sechdrs[i].sh_size;
|
| 192 | nattr->private = (void *)info->sechdrs[i].sh_addr;
|
| 193 | nattr->read = sysfs_bin_attr_simple_read;
|
| 194 | *(gattr++) = nattr++;
|
| 195 | }
|
| 196 | ++loaded;
|
| 197 | }
|
| 198 |
|
| 199 | ret = sysfs_create_group(&mod->mkobj.kobj, ¬es_attrs->grp);
|
| 200 | if (ret)
|
| 201 | goto out;
|
| 202 |
|
| 203 | mod->notes_attrs = notes_attrs;
|
| 204 | return 0;
|
| 205 |
|
| 206 | out:
|
| 207 | free_notes_attrs(notes_attrs);
|
| 208 | return ret;
|
| 209 | }
|
| 210 |
|
| 211 | static void remove_notes_attrs(struct module *mod)
|
| 212 | {
|
| 213 | if (mod->notes_attrs) {
|
| 214 | sysfs_remove_group(&mod->mkobj.kobj,
|
| 215 | &mod->notes_attrs->grp);
|
| 216 | |
| 217 | * We are positive that no one is using any notes attrs
|
| 218 | * at this point. Deallocate immediately.
|
| 219 |
|
| 220 | free_notes_attrs(mod->notes_attrs);
|
| 221 | mod->notes_attrs = NULL;
|
| 222 | }
|
| 223 | }
|
| 224 |
|
| 225 | #else
|
| 226 | static inline int add_sect_attrs(struct module *mod, const struct load_info *info)
|
| 227 | {
|
| 228 | return 0;
|
| 229 | }
|
| 230 | static inline void remove_sect_attrs(struct module *mod) { }
|
| 231 | static inline int add_notes_attrs(struct module *mod, const struct load_info *info)
|
| 232 | {
|
| 233 | return 0;
|
| 234 | }
|
| 235 | static inline void remove_notes_attrs(struct module *mod) { }
|
| 236 | #endif
|
| 237 |
|
| 238 | static void del_usage_links(struct module *mod)
|
| 239 | {
|
| 240 | #ifdef CONFIG_MODULE_UNLOAD
|
| 241 | struct module_use *use;
|
| 242 |
|
| 243 | mutex_lock(&module_mutex);
|
| 244 | list_for_each_entry(use, &mod->target_list, target_list)
|
| 245 | sysfs_remove_link(use->target->holders_dir, mod->name);
|
| 246 | mutex_unlock(&module_mutex);
|
| 247 | #endif
|
| 248 | }
|
| 249 |
|
| 250 | static int add_usage_links(struct module *mod)
|
| 251 | {
|
| 252 | int ret = 0;
|
| 253 | #ifdef CONFIG_MODULE_UNLOAD
|
| 254 | struct module_use *use;
|
| 255 |
|
| 256 | mutex_lock(&module_mutex);
|
| 257 | list_for_each_entry(use, &mod->target_list, target_list) {
|
| 258 | ret = sysfs_create_link(use->target->holders_dir,
|
| 259 | &mod->mkobj.kobj, mod->name);
|
| 260 | if (ret)
|
| 261 | break;
|
| 262 | }
|
| 263 | mutex_unlock(&module_mutex);
|
| 264 | if (ret)
|
| 265 | del_usage_links(mod);
|
| 266 | #endif
|
| 267 | return ret;
|
| 268 | }
|
| 269 |
|
| 270 | static void module_remove_modinfo_attrs(struct module *mod, int end)
|
| 271 | {
|
| 272 | const struct module_attribute *attr;
|
| 273 | int i;
|
| 274 |
|
| 275 | for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
|
| 276 | if (end >= 0 && i > end)
|
| 277 | break;
|
| 278 |
|
| 279 | if (!attr->attr.name)
|
| 280 | break;
|
| 281 | sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
|
| 282 | if (attr->free)
|
| 283 | attr->free(mod);
|
| 284 | }
|
| 285 | kfree(mod->modinfo_attrs);
|
| 286 | }
|
| 287 |
|
| 288 | static int module_add_modinfo_attrs(struct module *mod)
|
| 289 | {
|
| 290 | const struct module_attribute *attr;
|
| 291 | struct module_attribute *temp_attr;
|
| 292 | int error = 0;
|
| 293 | int i;
|
| 294 |
|
| 295 | mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
|
| 296 | (modinfo_attrs_count + 1)),
|
| 297 | GFP_KERNEL);
|
| 298 | if (!mod->modinfo_attrs)
|
| 299 | return -ENOMEM;
|
| 300 |
|
| 301 | temp_attr = mod->modinfo_attrs;
|
| 302 | for (i = 0; (attr = modinfo_attrs[i]); i++) {
|
| 303 | if (!attr->test || attr->test(mod)) {
|
| 304 | memcpy(temp_attr, attr, sizeof(*temp_attr));
|
| 305 | sysfs_attr_init(&temp_attr->attr);
|
| 306 | error = sysfs_create_file(&mod->mkobj.kobj,
|
| 307 | &temp_attr->attr);
|
| 308 | if (error)
|
| 309 | goto error_out;
|
| 310 | ++temp_attr;
|
| 311 | }
|
| 312 | }
|
| 313 |
|
| 314 | return 0;
|
| 315 |
|
| 316 | error_out:
|
| 317 | if (i > 0)
|
| 318 | module_remove_modinfo_attrs(mod, --i);
|
| 319 | else
|
| 320 | kfree(mod->modinfo_attrs);
|
| 321 | return error;
|
| 322 | }
|
| 323 |
|
| 324 | static void mod_kobject_put(struct module *mod)
|
| 325 | {
|
| 326 | DECLARE_COMPLETION_ONSTACK(c);
|
| 327 |
|
| 328 | mod->mkobj.kobj_completion = &c;
|
| 329 | kobject_put(&mod->mkobj.kobj);
|
| 330 | wait_for_completion(&c);
|
| 331 | }
|
| 332 |
|
| 333 | static int mod_sysfs_init(struct module *mod)
|
| 334 | {
|
| 335 | int err;
|
| 336 | struct kobject *kobj;
|
| 337 |
|
| 338 | if (!module_kset) {
|
| 339 | pr_err("%s: module sysfs not initialized\n", mod->name);
|
| 340 | err = -EINVAL;
|
| 341 | goto out;
|
| 342 | }
|
| 343 |
|
| 344 | kobj = kset_find_obj(module_kset, mod->name);
|
| 345 | if (kobj) {
|
| 346 | pr_err("%s: module is already loaded\n", mod->name);
|
| 347 | kobject_put(kobj);
|
| 348 | err = -EINVAL;
|
| 349 | goto out;
|
| 350 | }
|
| 351 |
|
| 352 | mod->mkobj.mod = mod;
|
| 353 |
|
| 354 | memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
|
| 355 | mod->mkobj.kobj.kset = module_kset;
|
| 356 | err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
|
| 357 | "%s", mod->name);
|
| 358 | if (err)
|
| 359 | mod_kobject_put(mod);
|
| 360 |
|
| 361 | out:
|
| 362 | return err;
|
| 363 | }
|
| 364 |
|
| 365 | int mod_sysfs_setup(struct module *mod,
|
| 366 | const struct load_info *info,
|
| 367 | struct kernel_param *kparam,
|
| 368 | unsigned int num_params)
|
| 369 | {
|
| 370 | int err;
|
| 371 |
|
| 372 | err = mod_sysfs_init(mod);
|
| 373 | if (err)
|
| 374 | goto out;
|
| 375 |
|
| 376 | mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
|
| 377 | if (!mod->holders_dir) {
|
| 378 | err = -ENOMEM;
|
| 379 | goto out_unreg;
|
| 380 | }
|
| 381 |
|
| 382 | err = module_param_sysfs_setup(mod, kparam, num_params);
|
| 383 | if (err)
|
| 384 | goto out_unreg_holders;
|
| 385 |
|
| 386 | err = module_add_modinfo_attrs(mod);
|
| 387 | if (err)
|
| 388 | goto out_unreg_param;
|
| 389 |
|
| 390 | err = add_usage_links(mod);
|
| 391 | if (err)
|
| 392 | goto out_unreg_modinfo_attrs;
|
| 393 |
|
| 394 | err = add_sect_attrs(mod, info);
|
| 395 | if (err)
|
| 396 | goto out_del_usage_links;
|
| 397 |
|
| 398 | err = add_notes_attrs(mod, info);
|
| 399 | if (err)
|
| 400 | goto out_unreg_sect_attrs;
|
| 401 |
|
| 402 | return 0;
|
| 403 |
|
| 404 | out_unreg_sect_attrs:
|
| 405 | remove_sect_attrs(mod);
|
| 406 | out_del_usage_links:
|
| 407 | del_usage_links(mod);
|
| 408 | out_unreg_modinfo_attrs:
|
| 409 | module_remove_modinfo_attrs(mod, -1);
|
| 410 | out_unreg_param:
|
| 411 | module_param_sysfs_remove(mod);
|
| 412 | out_unreg_holders:
|
| 413 | kobject_put(mod->holders_dir);
|
| 414 | out_unreg:
|
| 415 | mod_kobject_put(mod);
|
| 416 | out:
|
| 417 | return err;
|
| 418 | }
|
| 419 |
|
| 420 | static void mod_sysfs_fini(struct module *mod)
|
| 421 | {
|
| 422 | remove_notes_attrs(mod);
|
| 423 | remove_sect_attrs(mod);
|
| 424 | mod_kobject_put(mod);
|
| 425 | }
|
| 426 |
|
| 427 | void mod_sysfs_teardown(struct module *mod)
|
| 428 | {
|
| 429 | del_usage_links(mod);
|
| 430 | module_remove_modinfo_attrs(mod, -1);
|
| 431 | module_param_sysfs_remove(mod);
|
| 432 | kobject_put(mod->mkobj.drivers_dir);
|
| 433 | kobject_put(mod->holders_dir);
|
| 434 | mod_sysfs_fini(mod);
|
| 435 | }
|
| 436 |
|
| 437 | void init_param_lock(struct module *mod)
|
| 438 | {
|
| 439 | mutex_init(&mod->param_lock);
|
| 440 | }
|