Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Module proc support
0004  *
0005  * Copyright (C) 2008 Alexey Dobriyan
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/kallsyms.h>
0010 #include <linux/mutex.h>
0011 #include <linux/seq_file.h>
0012 #include <linux/proc_fs.h>
0013 #include "internal.h"
0014 
0015 #ifdef CONFIG_MODULE_UNLOAD
0016 static inline void print_unload_info(struct seq_file *m, struct module *mod)
0017 {
0018     struct module_use *use;
0019     int printed_something = 0;
0020 
0021     seq_printf(m, " %i ", module_refcount(mod));
0022 
0023     /*
0024      * Always include a trailing , so userspace can differentiate
0025      * between this and the old multi-field proc format.
0026      */
0027     list_for_each_entry(use, &mod->source_list, source_list) {
0028         printed_something = 1;
0029         seq_printf(m, "%s,", use->source->name);
0030     }
0031 
0032     if (mod->init && !mod->exit) {
0033         printed_something = 1;
0034         seq_puts(m, "[permanent],");
0035     }
0036 
0037     if (!printed_something)
0038         seq_puts(m, "-");
0039 }
0040 #else /* !CONFIG_MODULE_UNLOAD */
0041 static inline void print_unload_info(struct seq_file *m, struct module *mod)
0042 {
0043     /* We don't know the usage count, or what modules are using. */
0044     seq_puts(m, " - -");
0045 }
0046 #endif /* CONFIG_MODULE_UNLOAD */
0047 
0048 /* Called by the /proc file system to return a list of modules. */
0049 static void *m_start(struct seq_file *m, loff_t *pos)
0050 {
0051     mutex_lock(&module_mutex);
0052     return seq_list_start(&modules, *pos);
0053 }
0054 
0055 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
0056 {
0057     return seq_list_next(p, &modules, pos);
0058 }
0059 
0060 static void m_stop(struct seq_file *m, void *p)
0061 {
0062     mutex_unlock(&module_mutex);
0063 }
0064 
0065 static int m_show(struct seq_file *m, void *p)
0066 {
0067     struct module *mod = list_entry(p, struct module, list);
0068     char buf[MODULE_FLAGS_BUF_SIZE];
0069     void *value;
0070     unsigned int size;
0071 
0072     /* We always ignore unformed modules. */
0073     if (mod->state == MODULE_STATE_UNFORMED)
0074         return 0;
0075 
0076     size = mod->init_layout.size + mod->core_layout.size;
0077 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
0078     size += mod->data_layout.size;
0079 #endif
0080     seq_printf(m, "%s %u", mod->name, size);
0081     print_unload_info(m, mod);
0082 
0083     /* Informative for users. */
0084     seq_printf(m, " %s",
0085            mod->state == MODULE_STATE_GOING ? "Unloading" :
0086            mod->state == MODULE_STATE_COMING ? "Loading" :
0087            "Live");
0088     /* Used by oprofile and other similar tools. */
0089     value = m->private ? NULL : mod->core_layout.base;
0090     seq_printf(m, " 0x%px", value);
0091 
0092     /* Taints info */
0093     if (mod->taints)
0094         seq_printf(m, " %s", module_flags(mod, buf, true));
0095 
0096     seq_puts(m, "\n");
0097     return 0;
0098 }
0099 
0100 /*
0101  * Format: modulename size refcount deps address
0102  *
0103  * Where refcount is a number or -, and deps is a comma-separated list
0104  * of depends or -.
0105  */
0106 static const struct seq_operations modules_op = {
0107     .start  = m_start,
0108     .next   = m_next,
0109     .stop   = m_stop,
0110     .show   = m_show
0111 };
0112 
0113 /*
0114  * This also sets the "private" pointer to non-NULL if the
0115  * kernel pointers should be hidden (so you can just test
0116  * "m->private" to see if you should keep the values private).
0117  *
0118  * We use the same logic as for /proc/kallsyms.
0119  */
0120 static int modules_open(struct inode *inode, struct file *file)
0121 {
0122     int err = seq_open(file, &modules_op);
0123 
0124     if (!err) {
0125         struct seq_file *m = file->private_data;
0126 
0127         m->private = kallsyms_show_value(file->f_cred) ? NULL : (void *)8ul;
0128     }
0129 
0130     return err;
0131 }
0132 
0133 static const struct proc_ops modules_proc_ops = {
0134     .proc_flags = PROC_ENTRY_PERMANENT,
0135     .proc_open  = modules_open,
0136     .proc_read  = seq_read,
0137     .proc_lseek = seq_lseek,
0138     .proc_release   = seq_release,
0139 };
0140 
0141 static int __init proc_modules_init(void)
0142 {
0143     proc_create("modules", 0, NULL, &modules_proc_ops);
0144     return 0;
0145 }
0146 module_init(proc_modules_init);