procfs.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Module proc support
  4. *
  5. * Copyright (C) 2008 Alexey Dobriyan
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kallsyms.h>
  9. #include <linux/mutex.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/proc_fs.h>
  12. #include "internal.h"
  13. #ifdef CONFIG_MODULE_UNLOAD
  14. static inline void print_unload_info(struct seq_file *m, struct module *mod)
  15. {
  16. struct module_use *use;
  17. int printed_something = 0;
  18. seq_printf(m, " %i ", module_refcount(mod));
  19. /*
  20. * Always include a trailing , so userspace can differentiate
  21. * between this and the old multi-field proc format.
  22. */
  23. list_for_each_entry(use, &mod->source_list, source_list) {
  24. printed_something = 1;
  25. seq_printf(m, "%s,", use->source->name);
  26. }
  27. if (mod->init && !mod->exit) {
  28. printed_something = 1;
  29. seq_puts(m, "[permanent],");
  30. }
  31. if (!printed_something)
  32. seq_puts(m, "-");
  33. }
  34. #else /* !CONFIG_MODULE_UNLOAD */
  35. static inline void print_unload_info(struct seq_file *m, struct module *mod)
  36. {
  37. /* We don't know the usage count, or what modules are using. */
  38. seq_puts(m, " - -");
  39. }
  40. #endif /* CONFIG_MODULE_UNLOAD */
  41. /* Called by the /proc file system to return a list of modules. */
  42. static void *m_start(struct seq_file *m, loff_t *pos)
  43. {
  44. mutex_lock(&module_mutex);
  45. return seq_list_start(&modules, *pos);
  46. }
  47. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  48. {
  49. return seq_list_next(p, &modules, pos);
  50. }
  51. static void m_stop(struct seq_file *m, void *p)
  52. {
  53. mutex_unlock(&module_mutex);
  54. }
  55. static int m_show(struct seq_file *m, void *p)
  56. {
  57. struct module *mod = list_entry(p, struct module, list);
  58. char buf[MODULE_FLAGS_BUF_SIZE];
  59. void *value;
  60. unsigned int size;
  61. /* We always ignore unformed modules. */
  62. if (mod->state == MODULE_STATE_UNFORMED)
  63. return 0;
  64. size = mod->init_layout.size + mod->core_layout.size;
  65. #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  66. size += mod->data_layout.size;
  67. #endif
  68. seq_printf(m, "%s %u", mod->name, size);
  69. print_unload_info(m, mod);
  70. /* Informative for users. */
  71. seq_printf(m, " %s",
  72. mod->state == MODULE_STATE_GOING ? "Unloading" :
  73. mod->state == MODULE_STATE_COMING ? "Loading" :
  74. "Live");
  75. /* Used by oprofile and other similar tools. */
  76. value = m->private ? NULL : mod->core_layout.base;
  77. seq_printf(m, " 0x%px", value);
  78. /* Taints info */
  79. if (mod->taints)
  80. seq_printf(m, " %s", module_flags(mod, buf, true));
  81. seq_puts(m, "\n");
  82. return 0;
  83. }
  84. /*
  85. * Format: modulename size refcount deps address
  86. *
  87. * Where refcount is a number or -, and deps is a comma-separated list
  88. * of depends or -.
  89. */
  90. static const struct seq_operations modules_op = {
  91. .start = m_start,
  92. .next = m_next,
  93. .stop = m_stop,
  94. .show = m_show
  95. };
  96. /*
  97. * This also sets the "private" pointer to non-NULL if the
  98. * kernel pointers should be hidden (so you can just test
  99. * "m->private" to see if you should keep the values private).
  100. *
  101. * We use the same logic as for /proc/kallsyms.
  102. */
  103. static int modules_open(struct inode *inode, struct file *file)
  104. {
  105. int err = seq_open(file, &modules_op);
  106. if (!err) {
  107. struct seq_file *m = file->private_data;
  108. m->private = kallsyms_show_value(file->f_cred) ? NULL : (void *)8ul;
  109. }
  110. return err;
  111. }
  112. static const struct proc_ops modules_proc_ops = {
  113. .proc_flags = PROC_ENTRY_PERMANENT,
  114. .proc_open = modules_open,
  115. .proc_read = seq_read,
  116. .proc_lseek = seq_lseek,
  117. .proc_release = seq_release,
  118. };
  119. static int __init proc_modules_init(void)
  120. {
  121. proc_create("modules", 0, NULL, &modules_proc_ops);
  122. return 0;
  123. }
  124. module_init(proc_modules_init);