dynamic_printk.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * lib/dynamic_printk.c
  3. *
  4. * make pr_debug()/dev_dbg() calls runtime configurable based upon their
  5. * their source module.
  6. *
  7. * Copyright (C) 2008 Red Hat, Inc., Jason Baron <jbaron@redhat.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/fs.h>
  15. extern struct mod_debug __start___verbose[];
  16. extern struct mod_debug __stop___verbose[];
  17. struct debug_name {
  18. struct hlist_node hlist;
  19. struct hlist_node hlist2;
  20. int hash1;
  21. int hash2;
  22. char *name;
  23. int enable;
  24. int type;
  25. };
  26. static int nr_entries;
  27. static int num_enabled;
  28. int dynamic_enabled = DYNAMIC_ENABLED_NONE;
  29. static struct hlist_head module_table[DEBUG_HASH_TABLE_SIZE] =
  30. { [0 ... DEBUG_HASH_TABLE_SIZE-1] = HLIST_HEAD_INIT };
  31. static struct hlist_head module_table2[DEBUG_HASH_TABLE_SIZE] =
  32. { [0 ... DEBUG_HASH_TABLE_SIZE-1] = HLIST_HEAD_INIT };
  33. static DECLARE_MUTEX(debug_list_mutex);
  34. /* dynamic_printk_enabled, and dynamic_printk_enabled2 are bitmasks in which
  35. * bit n is set to 1 if any modname hashes into the bucket n, 0 otherwise. They
  36. * use independent hash functions, to reduce the chance of false positives.
  37. */
  38. long long dynamic_printk_enabled;
  39. EXPORT_SYMBOL_GPL(dynamic_printk_enabled);
  40. long long dynamic_printk_enabled2;
  41. EXPORT_SYMBOL_GPL(dynamic_printk_enabled2);
  42. /* returns the debug module pointer. */
  43. static struct debug_name *find_debug_module(char *module_name)
  44. {
  45. int i;
  46. struct hlist_head *head;
  47. struct hlist_node *node;
  48. struct debug_name *element;
  49. element = NULL;
  50. for (i = 0; i < DEBUG_HASH_TABLE_SIZE; i++) {
  51. head = &module_table[i];
  52. hlist_for_each_entry_rcu(element, node, head, hlist)
  53. if (!strcmp(element->name, module_name))
  54. return element;
  55. }
  56. return NULL;
  57. }
  58. /* returns the debug module pointer. */
  59. static struct debug_name *find_debug_module_hash(char *module_name, int hash)
  60. {
  61. struct hlist_head *head;
  62. struct hlist_node *node;
  63. struct debug_name *element;
  64. element = NULL;
  65. head = &module_table[hash];
  66. hlist_for_each_entry_rcu(element, node, head, hlist)
  67. if (!strcmp(element->name, module_name))
  68. return element;
  69. return NULL;
  70. }
  71. /* caller must hold mutex*/
  72. static int __add_debug_module(char *mod_name, int hash, int hash2)
  73. {
  74. struct debug_name *new;
  75. char *module_name;
  76. int ret = 0;
  77. if (find_debug_module(mod_name)) {
  78. ret = -EINVAL;
  79. goto out;
  80. }
  81. module_name = kmalloc(strlen(mod_name) + 1, GFP_KERNEL);
  82. if (!module_name) {
  83. ret = -ENOMEM;
  84. goto out;
  85. }
  86. module_name = strcpy(module_name, mod_name);
  87. module_name[strlen(mod_name)] = '\0';
  88. new = kzalloc(sizeof(struct debug_name), GFP_KERNEL);
  89. if (!new) {
  90. kfree(module_name);
  91. ret = -ENOMEM;
  92. goto out;
  93. }
  94. INIT_HLIST_NODE(&new->hlist);
  95. INIT_HLIST_NODE(&new->hlist2);
  96. new->name = module_name;
  97. new->hash1 = hash;
  98. new->hash2 = hash2;
  99. hlist_add_head_rcu(&new->hlist, &module_table[hash]);
  100. hlist_add_head_rcu(&new->hlist2, &module_table2[hash2]);
  101. nr_entries++;
  102. out:
  103. return ret;
  104. }
  105. int unregister_dynamic_debug_module(char *mod_name)
  106. {
  107. struct debug_name *element;
  108. int ret = 0;
  109. down(&debug_list_mutex);
  110. element = find_debug_module(mod_name);
  111. if (!element) {
  112. ret = -EINVAL;
  113. goto out;
  114. }
  115. hlist_del_rcu(&element->hlist);
  116. hlist_del_rcu(&element->hlist2);
  117. synchronize_rcu();
  118. kfree(element->name);
  119. if (element->enable)
  120. num_enabled--;
  121. kfree(element);
  122. nr_entries--;
  123. out:
  124. up(&debug_list_mutex);
  125. return ret;
  126. }
  127. EXPORT_SYMBOL_GPL(unregister_dynamic_debug_module);
  128. int register_dynamic_debug_module(char *mod_name, int type, char *share_name,
  129. char *flags, int hash, int hash2)
  130. {
  131. struct debug_name *elem;
  132. int ret = 0;
  133. down(&debug_list_mutex);
  134. elem = find_debug_module(mod_name);
  135. if (!elem) {
  136. if (__add_debug_module(mod_name, hash, hash2))
  137. goto out;
  138. elem = find_debug_module(mod_name);
  139. if (dynamic_enabled == DYNAMIC_ENABLED_ALL &&
  140. !strcmp(mod_name, share_name)) {
  141. elem->enable = true;
  142. num_enabled++;
  143. }
  144. }
  145. elem->type |= type;
  146. out:
  147. up(&debug_list_mutex);
  148. return ret;
  149. }
  150. EXPORT_SYMBOL_GPL(register_dynamic_debug_module);
  151. int __dynamic_dbg_enabled_helper(char *mod_name, int type, int value, int hash)
  152. {
  153. struct debug_name *elem;
  154. int ret = 0;
  155. if (dynamic_enabled == DYNAMIC_ENABLED_ALL)
  156. return 1;
  157. rcu_read_lock();
  158. elem = find_debug_module_hash(mod_name, hash);
  159. if (elem && elem->enable)
  160. ret = 1;
  161. rcu_read_unlock();
  162. return ret;
  163. }
  164. EXPORT_SYMBOL_GPL(__dynamic_dbg_enabled_helper);
  165. static void set_all(bool enable)
  166. {
  167. struct debug_name *e;
  168. struct hlist_node *node;
  169. int i;
  170. long long enable_mask;
  171. for (i = 0; i < DEBUG_HASH_TABLE_SIZE; i++) {
  172. if (module_table[i].first != NULL) {
  173. hlist_for_each_entry(e, node, &module_table[i], hlist) {
  174. e->enable = enable;
  175. }
  176. }
  177. }
  178. if (enable)
  179. enable_mask = ULLONG_MAX;
  180. else
  181. enable_mask = 0;
  182. dynamic_printk_enabled = enable_mask;
  183. dynamic_printk_enabled2 = enable_mask;
  184. }
  185. static int disabled_hash(int i, bool first_table)
  186. {
  187. struct debug_name *e;
  188. struct hlist_node *node;
  189. if (first_table) {
  190. hlist_for_each_entry(e, node, &module_table[i], hlist) {
  191. if (e->enable)
  192. return 0;
  193. }
  194. } else {
  195. hlist_for_each_entry(e, node, &module_table2[i], hlist2) {
  196. if (e->enable)
  197. return 0;
  198. }
  199. }
  200. return 1;
  201. }
  202. static ssize_t pr_debug_write(struct file *file, const char __user *buf,
  203. size_t length, loff_t *ppos)
  204. {
  205. char *buffer, *s, *value_str, *setting_str;
  206. int err, value;
  207. struct debug_name *elem = NULL;
  208. int all = 0;
  209. if (length > PAGE_SIZE || length < 0)
  210. return -EINVAL;
  211. buffer = (char *)__get_free_page(GFP_KERNEL);
  212. if (!buffer)
  213. return -ENOMEM;
  214. err = -EFAULT;
  215. if (copy_from_user(buffer, buf, length))
  216. goto out;
  217. err = -EINVAL;
  218. if (length < PAGE_SIZE)
  219. buffer[length] = '\0';
  220. else if (buffer[PAGE_SIZE-1])
  221. goto out;
  222. err = -EINVAL;
  223. down(&debug_list_mutex);
  224. if (strncmp("set", buffer, 3))
  225. goto out_up;
  226. s = buffer + 3;
  227. setting_str = strsep(&s, "=");
  228. if (s == NULL)
  229. goto out_up;
  230. setting_str = strstrip(setting_str);
  231. value_str = strsep(&s, " ");
  232. if (s == NULL)
  233. goto out_up;
  234. s = strstrip(s);
  235. if (!strncmp(s, "all", 3))
  236. all = 1;
  237. else
  238. elem = find_debug_module(s);
  239. if (!strncmp(setting_str, "enable", 6)) {
  240. value = !!simple_strtol(value_str, NULL, 10);
  241. if (all) {
  242. if (value) {
  243. set_all(true);
  244. num_enabled = nr_entries;
  245. dynamic_enabled = DYNAMIC_ENABLED_ALL;
  246. } else {
  247. set_all(false);
  248. num_enabled = 0;
  249. dynamic_enabled = DYNAMIC_ENABLED_NONE;
  250. }
  251. err = 0;
  252. } else if (elem) {
  253. if (value && (elem->enable == 0)) {
  254. dynamic_printk_enabled |= (1LL << elem->hash1);
  255. dynamic_printk_enabled2 |= (1LL << elem->hash2);
  256. elem->enable = 1;
  257. num_enabled++;
  258. dynamic_enabled = DYNAMIC_ENABLED_SOME;
  259. err = 0;
  260. printk(KERN_DEBUG
  261. "debugging enabled for module %s\n",
  262. elem->name);
  263. } else if (!value && (elem->enable == 1)) {
  264. elem->enable = 0;
  265. num_enabled--;
  266. if (disabled_hash(elem->hash1, true))
  267. dynamic_printk_enabled &=
  268. ~(1LL << elem->hash1);
  269. if (disabled_hash(elem->hash2, false))
  270. dynamic_printk_enabled2 &=
  271. ~(1LL << elem->hash2);
  272. if (num_enabled)
  273. dynamic_enabled = DYNAMIC_ENABLED_SOME;
  274. else
  275. dynamic_enabled = DYNAMIC_ENABLED_NONE;
  276. err = 0;
  277. printk(KERN_DEBUG
  278. "debugging disabled for module %s\n",
  279. elem->name);
  280. }
  281. }
  282. }
  283. if (!err)
  284. err = length;
  285. out_up:
  286. up(&debug_list_mutex);
  287. out:
  288. free_page((unsigned long)buffer);
  289. return err;
  290. }
  291. static void *pr_debug_seq_start(struct seq_file *f, loff_t *pos)
  292. {
  293. return (*pos < DEBUG_HASH_TABLE_SIZE) ? pos : NULL;
  294. }
  295. static void *pr_debug_seq_next(struct seq_file *s, void *v, loff_t *pos)
  296. {
  297. (*pos)++;
  298. if (*pos >= DEBUG_HASH_TABLE_SIZE)
  299. return NULL;
  300. return pos;
  301. }
  302. static void pr_debug_seq_stop(struct seq_file *s, void *v)
  303. {
  304. /* Nothing to do */
  305. }
  306. static int pr_debug_seq_show(struct seq_file *s, void *v)
  307. {
  308. struct hlist_head *head;
  309. struct hlist_node *node;
  310. struct debug_name *elem;
  311. unsigned int i = *(loff_t *) v;
  312. rcu_read_lock();
  313. head = &module_table[i];
  314. hlist_for_each_entry_rcu(elem, node, head, hlist) {
  315. seq_printf(s, "%s enabled=%d", elem->name, elem->enable);
  316. seq_printf(s, "\n");
  317. }
  318. rcu_read_unlock();
  319. return 0;
  320. }
  321. static struct seq_operations pr_debug_seq_ops = {
  322. .start = pr_debug_seq_start,
  323. .next = pr_debug_seq_next,
  324. .stop = pr_debug_seq_stop,
  325. .show = pr_debug_seq_show
  326. };
  327. static int pr_debug_open(struct inode *inode, struct file *filp)
  328. {
  329. return seq_open(filp, &pr_debug_seq_ops);
  330. }
  331. static const struct file_operations pr_debug_operations = {
  332. .open = pr_debug_open,
  333. .read = seq_read,
  334. .write = pr_debug_write,
  335. .llseek = seq_lseek,
  336. .release = seq_release,
  337. };
  338. static int __init dynamic_printk_init(void)
  339. {
  340. struct dentry *dir, *file;
  341. struct mod_debug *iter;
  342. unsigned long value;
  343. dir = debugfs_create_dir("dynamic_printk", NULL);
  344. if (!dir)
  345. return -ENOMEM;
  346. file = debugfs_create_file("modules", 0644, dir, NULL,
  347. &pr_debug_operations);
  348. if (!file) {
  349. debugfs_remove(dir);
  350. return -ENOMEM;
  351. }
  352. for (value = (unsigned long)__start___verbose;
  353. value < (unsigned long)__stop___verbose;
  354. value += sizeof(struct mod_debug)) {
  355. iter = (struct mod_debug *)value;
  356. register_dynamic_debug_module(iter->modname,
  357. iter->type,
  358. iter->logical_modname,
  359. iter->flag_names, iter->hash, iter->hash2);
  360. }
  361. if (dynamic_enabled == DYNAMIC_ENABLED_ALL)
  362. set_all(true);
  363. return 0;
  364. }
  365. module_init(dynamic_printk_init);
  366. /* may want to move this earlier so we can get traces as early as possible */
  367. static int __init dynamic_printk_setup(char *str)
  368. {
  369. if (str)
  370. return -ENOENT;
  371. dynamic_enabled = DYNAMIC_ENABLED_ALL;
  372. return 0;
  373. }
  374. /* Use early_param(), so we can get debug output as early as possible */
  375. early_param("dynamic_printk", dynamic_printk_setup);