Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Module sysfs support
0004  *
0005  * Copyright (C) 2008 Rusty Russell
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/kernel.h>
0010 #include <linux/fs.h>
0011 #include <linux/sysfs.h>
0012 #include <linux/slab.h>
0013 #include <linux/kallsyms.h>
0014 #include <linux/mutex.h>
0015 #include "internal.h"
0016 
0017 /*
0018  * /sys/module/foo/sections stuff
0019  * J. Corbet <corbet@lwn.net>
0020  */
0021 #ifdef CONFIG_KALLSYMS
0022 struct module_sect_attr {
0023     struct bin_attribute battr;
0024     unsigned long address;
0025 };
0026 
0027 struct module_sect_attrs {
0028     struct attribute_group grp;
0029     unsigned int nsections;
0030     struct module_sect_attr attrs[];
0031 };
0032 
0033 #define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
0034 static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
0035                 struct bin_attribute *battr,
0036                 char *buf, loff_t pos, size_t count)
0037 {
0038     struct module_sect_attr *sattr =
0039         container_of(battr, struct module_sect_attr, battr);
0040     char bounce[MODULE_SECT_READ_SIZE + 1];
0041     size_t wrote;
0042 
0043     if (pos != 0)
0044         return -EINVAL;
0045 
0046     /*
0047      * Since we're a binary read handler, we must account for the
0048      * trailing NUL byte that sprintf will write: if "buf" is
0049      * too small to hold the NUL, or the NUL is exactly the last
0050      * byte, the read will look like it got truncated by one byte.
0051      * Since there is no way to ask sprintf nicely to not write
0052      * the NUL, we have to use a bounce buffer.
0053      */
0054     wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",
0055               kallsyms_show_value(file->f_cred)
0056                 ? (void *)sattr->address : NULL);
0057     count = min(count, wrote);
0058     memcpy(buf, bounce, count);
0059 
0060     return count;
0061 }
0062 
0063 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
0064 {
0065     unsigned int section;
0066 
0067     for (section = 0; section < sect_attrs->nsections; section++)
0068         kfree(sect_attrs->attrs[section].battr.attr.name);
0069     kfree(sect_attrs);
0070 }
0071 
0072 static void add_sect_attrs(struct module *mod, const struct load_info *info)
0073 {
0074     unsigned int nloaded = 0, i, size[2];
0075     struct module_sect_attrs *sect_attrs;
0076     struct module_sect_attr *sattr;
0077     struct bin_attribute **gattr;
0078 
0079     /* Count loaded sections and allocate structures */
0080     for (i = 0; i < info->hdr->e_shnum; i++)
0081         if (!sect_empty(&info->sechdrs[i]))
0082             nloaded++;
0083     size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
0084             sizeof(sect_attrs->grp.bin_attrs[0]));
0085     size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
0086     sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
0087     if (!sect_attrs)
0088         return;
0089 
0090     /* Setup section attributes. */
0091     sect_attrs->grp.name = "sections";
0092     sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
0093 
0094     sect_attrs->nsections = 0;
0095     sattr = &sect_attrs->attrs[0];
0096     gattr = &sect_attrs->grp.bin_attrs[0];
0097     for (i = 0; i < info->hdr->e_shnum; i++) {
0098         Elf_Shdr *sec = &info->sechdrs[i];
0099 
0100         if (sect_empty(sec))
0101             continue;
0102         sysfs_bin_attr_init(&sattr->battr);
0103         sattr->address = sec->sh_addr;
0104         sattr->battr.attr.name =
0105             kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
0106         if (!sattr->battr.attr.name)
0107             goto out;
0108         sect_attrs->nsections++;
0109         sattr->battr.read = module_sect_read;
0110         sattr->battr.size = MODULE_SECT_READ_SIZE;
0111         sattr->battr.attr.mode = 0400;
0112         *(gattr++) = &(sattr++)->battr;
0113     }
0114     *gattr = NULL;
0115 
0116     if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
0117         goto out;
0118 
0119     mod->sect_attrs = sect_attrs;
0120     return;
0121 out:
0122     free_sect_attrs(sect_attrs);
0123 }
0124 
0125 static void remove_sect_attrs(struct module *mod)
0126 {
0127     if (mod->sect_attrs) {
0128         sysfs_remove_group(&mod->mkobj.kobj,
0129                    &mod->sect_attrs->grp);
0130         /*
0131          * We are positive that no one is using any sect attrs
0132          * at this point.  Deallocate immediately.
0133          */
0134         free_sect_attrs(mod->sect_attrs);
0135         mod->sect_attrs = NULL;
0136     }
0137 }
0138 
0139 /*
0140  * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
0141  */
0142 
0143 struct module_notes_attrs {
0144     struct kobject *dir;
0145     unsigned int notes;
0146     struct bin_attribute attrs[];
0147 };
0148 
0149 static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
0150                  struct bin_attribute *bin_attr,
0151                  char *buf, loff_t pos, size_t count)
0152 {
0153     /*
0154      * The caller checked the pos and count against our size.
0155      */
0156     memcpy(buf, bin_attr->private + pos, count);
0157     return count;
0158 }
0159 
0160 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
0161                  unsigned int i)
0162 {
0163     if (notes_attrs->dir) {
0164         while (i-- > 0)
0165             sysfs_remove_bin_file(notes_attrs->dir,
0166                           &notes_attrs->attrs[i]);
0167         kobject_put(notes_attrs->dir);
0168     }
0169     kfree(notes_attrs);
0170 }
0171 
0172 static void add_notes_attrs(struct module *mod, const struct load_info *info)
0173 {
0174     unsigned int notes, loaded, i;
0175     struct module_notes_attrs *notes_attrs;
0176     struct bin_attribute *nattr;
0177 
0178     /* failed to create section attributes, so can't create notes */
0179     if (!mod->sect_attrs)
0180         return;
0181 
0182     /* Count notes sections and allocate structures.  */
0183     notes = 0;
0184     for (i = 0; i < info->hdr->e_shnum; i++)
0185         if (!sect_empty(&info->sechdrs[i]) &&
0186             info->sechdrs[i].sh_type == SHT_NOTE)
0187             ++notes;
0188 
0189     if (notes == 0)
0190         return;
0191 
0192     notes_attrs = kzalloc(struct_size(notes_attrs, attrs, notes),
0193                   GFP_KERNEL);
0194     if (!notes_attrs)
0195         return;
0196 
0197     notes_attrs->notes = notes;
0198     nattr = &notes_attrs->attrs[0];
0199     for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
0200         if (sect_empty(&info->sechdrs[i]))
0201             continue;
0202         if (info->sechdrs[i].sh_type == SHT_NOTE) {
0203             sysfs_bin_attr_init(nattr);
0204             nattr->attr.name = mod->sect_attrs->attrs[loaded].battr.attr.name;
0205             nattr->attr.mode = 0444;
0206             nattr->size = info->sechdrs[i].sh_size;
0207             nattr->private = (void *)info->sechdrs[i].sh_addr;
0208             nattr->read = module_notes_read;
0209             ++nattr;
0210         }
0211         ++loaded;
0212     }
0213 
0214     notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
0215     if (!notes_attrs->dir)
0216         goto out;
0217 
0218     for (i = 0; i < notes; ++i)
0219         if (sysfs_create_bin_file(notes_attrs->dir,
0220                       &notes_attrs->attrs[i]))
0221             goto out;
0222 
0223     mod->notes_attrs = notes_attrs;
0224     return;
0225 
0226 out:
0227     free_notes_attrs(notes_attrs, i);
0228 }
0229 
0230 static void remove_notes_attrs(struct module *mod)
0231 {
0232     if (mod->notes_attrs)
0233         free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
0234 }
0235 
0236 #else /* !CONFIG_KALLSYMS */
0237 static inline void add_sect_attrs(struct module *mod, const struct load_info *info) { }
0238 static inline void remove_sect_attrs(struct module *mod) { }
0239 static inline void add_notes_attrs(struct module *mod, const struct load_info *info) { }
0240 static inline void remove_notes_attrs(struct module *mod) { }
0241 #endif /* CONFIG_KALLSYMS */
0242 
0243 static void del_usage_links(struct module *mod)
0244 {
0245 #ifdef CONFIG_MODULE_UNLOAD
0246     struct module_use *use;
0247 
0248     mutex_lock(&module_mutex);
0249     list_for_each_entry(use, &mod->target_list, target_list)
0250         sysfs_remove_link(use->target->holders_dir, mod->name);
0251     mutex_unlock(&module_mutex);
0252 #endif
0253 }
0254 
0255 static int add_usage_links(struct module *mod)
0256 {
0257     int ret = 0;
0258 #ifdef CONFIG_MODULE_UNLOAD
0259     struct module_use *use;
0260 
0261     mutex_lock(&module_mutex);
0262     list_for_each_entry(use, &mod->target_list, target_list) {
0263         ret = sysfs_create_link(use->target->holders_dir,
0264                     &mod->mkobj.kobj, mod->name);
0265         if (ret)
0266             break;
0267     }
0268     mutex_unlock(&module_mutex);
0269     if (ret)
0270         del_usage_links(mod);
0271 #endif
0272     return ret;
0273 }
0274 
0275 static void module_remove_modinfo_attrs(struct module *mod, int end)
0276 {
0277     struct module_attribute *attr;
0278     int i;
0279 
0280     for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
0281         if (end >= 0 && i > end)
0282             break;
0283         /* pick a field to test for end of list */
0284         if (!attr->attr.name)
0285             break;
0286         sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
0287         if (attr->free)
0288             attr->free(mod);
0289     }
0290     kfree(mod->modinfo_attrs);
0291 }
0292 
0293 static int module_add_modinfo_attrs(struct module *mod)
0294 {
0295     struct module_attribute *attr;
0296     struct module_attribute *temp_attr;
0297     int error = 0;
0298     int i;
0299 
0300     mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
0301                     (modinfo_attrs_count + 1)),
0302                     GFP_KERNEL);
0303     if (!mod->modinfo_attrs)
0304         return -ENOMEM;
0305 
0306     temp_attr = mod->modinfo_attrs;
0307     for (i = 0; (attr = modinfo_attrs[i]); i++) {
0308         if (!attr->test || attr->test(mod)) {
0309             memcpy(temp_attr, attr, sizeof(*temp_attr));
0310             sysfs_attr_init(&temp_attr->attr);
0311             error = sysfs_create_file(&mod->mkobj.kobj,
0312                           &temp_attr->attr);
0313             if (error)
0314                 goto error_out;
0315             ++temp_attr;
0316         }
0317     }
0318 
0319     return 0;
0320 
0321 error_out:
0322     if (i > 0)
0323         module_remove_modinfo_attrs(mod, --i);
0324     else
0325         kfree(mod->modinfo_attrs);
0326     return error;
0327 }
0328 
0329 static void mod_kobject_put(struct module *mod)
0330 {
0331     DECLARE_COMPLETION_ONSTACK(c);
0332 
0333     mod->mkobj.kobj_completion = &c;
0334     kobject_put(&mod->mkobj.kobj);
0335     wait_for_completion(&c);
0336 }
0337 
0338 static int mod_sysfs_init(struct module *mod)
0339 {
0340     int err;
0341     struct kobject *kobj;
0342 
0343     if (!module_sysfs_initialized) {
0344         pr_err("%s: module sysfs not initialized\n", mod->name);
0345         err = -EINVAL;
0346         goto out;
0347     }
0348 
0349     kobj = kset_find_obj(module_kset, mod->name);
0350     if (kobj) {
0351         pr_err("%s: module is already loaded\n", mod->name);
0352         kobject_put(kobj);
0353         err = -EINVAL;
0354         goto out;
0355     }
0356 
0357     mod->mkobj.mod = mod;
0358 
0359     memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
0360     mod->mkobj.kobj.kset = module_kset;
0361     err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
0362                    "%s", mod->name);
0363     if (err)
0364         mod_kobject_put(mod);
0365 
0366 out:
0367     return err;
0368 }
0369 
0370 int mod_sysfs_setup(struct module *mod,
0371             const struct load_info *info,
0372                struct kernel_param *kparam,
0373                unsigned int num_params)
0374 {
0375     int err;
0376 
0377     err = mod_sysfs_init(mod);
0378     if (err)
0379         goto out;
0380 
0381     mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
0382     if (!mod->holders_dir) {
0383         err = -ENOMEM;
0384         goto out_unreg;
0385     }
0386 
0387     err = module_param_sysfs_setup(mod, kparam, num_params);
0388     if (err)
0389         goto out_unreg_holders;
0390 
0391     err = module_add_modinfo_attrs(mod);
0392     if (err)
0393         goto out_unreg_param;
0394 
0395     err = add_usage_links(mod);
0396     if (err)
0397         goto out_unreg_modinfo_attrs;
0398 
0399     add_sect_attrs(mod, info);
0400     add_notes_attrs(mod, info);
0401 
0402     return 0;
0403 
0404 out_unreg_modinfo_attrs:
0405     module_remove_modinfo_attrs(mod, -1);
0406 out_unreg_param:
0407     module_param_sysfs_remove(mod);
0408 out_unreg_holders:
0409     kobject_put(mod->holders_dir);
0410 out_unreg:
0411     mod_kobject_put(mod);
0412 out:
0413     return err;
0414 }
0415 
0416 static void mod_sysfs_fini(struct module *mod)
0417 {
0418     remove_notes_attrs(mod);
0419     remove_sect_attrs(mod);
0420     mod_kobject_put(mod);
0421 }
0422 
0423 void mod_sysfs_teardown(struct module *mod)
0424 {
0425     del_usage_links(mod);
0426     module_remove_modinfo_attrs(mod, -1);
0427     module_param_sysfs_remove(mod);
0428     kobject_put(mod->mkobj.drivers_dir);
0429     kobject_put(mod->holders_dir);
0430     mod_sysfs_fini(mod);
0431 }
0432 
0433 void init_param_lock(struct module *mod)
0434 {
0435     mutex_init(&mod->param_lock);
0436 }