dynamic_printk.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 0;
  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 {
  253. if (elem) {
  254. if (value && (elem->enable == 0)) {
  255. dynamic_printk_enabled |=
  256. (1LL << elem->hash1);
  257. dynamic_printk_enabled2 |=
  258. (1LL << elem->hash2);
  259. elem->enable = 1;
  260. num_enabled++;
  261. dynamic_enabled = DYNAMIC_ENABLED_SOME;
  262. err = 0;
  263. printk(KERN_DEBUG
  264. "debugging enabled for module %s",
  265. elem->name);
  266. } else if (!value && (elem->enable == 1)) {
  267. elem->enable = 0;
  268. num_enabled--;
  269. if (disabled_hash(elem->hash1, true))
  270. dynamic_printk_enabled &=
  271. ~(1LL << elem->hash1);
  272. if (disabled_hash(elem->hash2, false))
  273. dynamic_printk_enabled2 &=
  274. ~(1LL << elem->hash2);
  275. if (num_enabled)
  276. dynamic_enabled =
  277. DYNAMIC_ENABLED_SOME;
  278. else
  279. dynamic_enabled =
  280. DYNAMIC_ENABLED_NONE;
  281. err = 0;
  282. printk(KERN_DEBUG
  283. "debugging disabled for module "
  284. "%s", elem->name);
  285. }
  286. }
  287. }
  288. }
  289. if (!err)
  290. err = length;
  291. out_up:
  292. up(&debug_list_mutex);
  293. out:
  294. free_page((unsigned long)buffer);
  295. return err;
  296. }
  297. static void *pr_debug_seq_start(struct seq_file *f, loff_t *pos)
  298. {
  299. return (*pos < DEBUG_HASH_TABLE_SIZE) ? pos : NULL;
  300. }
  301. static void *pr_debug_seq_next(struct seq_file *s, void *v, loff_t *pos)
  302. {
  303. (*pos)++;
  304. if (*pos >= DEBUG_HASH_TABLE_SIZE)
  305. return NULL;
  306. return pos;
  307. }
  308. static void pr_debug_seq_stop(struct seq_file *s, void *v)
  309. {
  310. /* Nothing to do */
  311. }
  312. static int pr_debug_seq_show(struct seq_file *s, void *v)
  313. {
  314. struct hlist_head *head;
  315. struct hlist_node *node;
  316. struct debug_name *elem;
  317. unsigned int i = *(loff_t *) v;
  318. rcu_read_lock();
  319. head = &module_table[i];
  320. hlist_for_each_entry_rcu(elem, node, head, hlist) {
  321. seq_printf(s, "%s enabled=%d", elem->name, elem->enable);
  322. seq_printf(s, "\n");
  323. }
  324. rcu_read_unlock();
  325. return 0;
  326. }
  327. static struct seq_operations pr_debug_seq_ops = {
  328. .start = pr_debug_seq_start,
  329. .next = pr_debug_seq_next,
  330. .stop = pr_debug_seq_stop,
  331. .show = pr_debug_seq_show
  332. };
  333. static int pr_debug_open(struct inode *inode, struct file *filp)
  334. {
  335. return seq_open(filp, &pr_debug_seq_ops);
  336. }
  337. static const struct file_operations pr_debug_operations = {
  338. .open = pr_debug_open,
  339. .read = seq_read,
  340. .write = pr_debug_write,
  341. .llseek = seq_lseek,
  342. .release = seq_release,
  343. };
  344. static int __init dynamic_printk_init(void)
  345. {
  346. struct dentry *dir, *file;
  347. struct mod_debug *iter;
  348. unsigned long value;
  349. dir = debugfs_create_dir("dynamic_printk", NULL);
  350. if (!dir)
  351. return -ENOMEM;
  352. file = debugfs_create_file("modules", 0644, dir, NULL,
  353. &pr_debug_operations);
  354. if (!file) {
  355. debugfs_remove(dir);
  356. return -ENOMEM;
  357. }
  358. for (value = (unsigned long)__start___verbose;
  359. value < (unsigned long)__stop___verbose;
  360. value += sizeof(struct mod_debug)) {
  361. iter = (struct mod_debug *)value;
  362. register_dynamic_debug_module(iter->modname,
  363. iter->type,
  364. iter->logical_modname,
  365. iter->flag_names, iter->hash, iter->hash2);
  366. }
  367. if (dynamic_enabled == DYNAMIC_ENABLED_ALL)
  368. set_all(true);
  369. return 0;
  370. }
  371. module_init(dynamic_printk_init);
  372. /* may want to move this earlier so we can get traces as early as possible */
  373. static int __init dynamic_printk_setup(char *str)
  374. {
  375. if (str)
  376. return -ENOENT;
  377. dynamic_enabled = DYNAMIC_ENABLED_ALL;
  378. return 0;
  379. }
  380. /* Use early_param(), so we can get debug output as early as possible */
  381. early_param("dynamic_printk", dynamic_printk_setup);