lkdtm.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Kprobe module for testing crash dumps
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2006
  19. *
  20. * Author: Ankita Garg <ankita@in.ibm.com>
  21. *
  22. * This module induces system failures at predefined crashpoints to
  23. * evaluate the reliability of crash dumps obtained using different dumping
  24. * solutions.
  25. *
  26. * It is adapted from the Linux Kernel Dump Test Tool by
  27. * Fernando Luis Vazquez Cao <http://lkdtt.sourceforge.net>
  28. *
  29. * Usage : insmod lkdtm.ko [recur_count={>0}] cpoint_name=<> cpoint_type=<>
  30. * [cpoint_count={>0}]
  31. *
  32. * recur_count : Recursion level for the stack overflow test. Default is 10.
  33. *
  34. * cpoint_name : Crash point where the kernel is to be crashed. It can be
  35. * one of INT_HARDWARE_ENTRY, INT_HW_IRQ_EN, INT_TASKLET_ENTRY,
  36. * FS_DEVRW, MEM_SWAPOUT, TIMERADD, SCSI_DISPATCH_CMD,
  37. * IDE_CORE_CP
  38. *
  39. * cpoint_type : Indicates the action to be taken on hitting the crash point.
  40. * It can be one of PANIC, BUG, EXCEPTION, LOOP, OVERFLOW
  41. *
  42. * cpoint_count : Indicates the number of times the crash point is to be hit
  43. * to trigger an action. The default is 10.
  44. */
  45. #include <linux/kernel.h>
  46. #include <linux/module.h>
  47. #include <linux/kprobes.h>
  48. #include <linux/kallsyms.h>
  49. #include <linux/init.h>
  50. #include <linux/irq.h>
  51. #include <linux/interrupt.h>
  52. #include <scsi/scsi_cmnd.h>
  53. #ifdef CONFIG_IDE
  54. #include <linux/ide.h>
  55. #endif
  56. #define NUM_CPOINTS 8
  57. #define NUM_CPOINT_TYPES 5
  58. #define DEFAULT_COUNT 10
  59. #define REC_NUM_DEFAULT 10
  60. enum cname {
  61. INVALID,
  62. INT_HARDWARE_ENTRY,
  63. INT_HW_IRQ_EN,
  64. INT_TASKLET_ENTRY,
  65. FS_DEVRW,
  66. MEM_SWAPOUT,
  67. TIMERADD,
  68. SCSI_DISPATCH_CMD,
  69. IDE_CORE_CP
  70. };
  71. enum ctype {
  72. NONE,
  73. PANIC,
  74. BUG,
  75. EXCEPTION,
  76. LOOP,
  77. OVERFLOW
  78. };
  79. static char* cp_name[] = {
  80. "INT_HARDWARE_ENTRY",
  81. "INT_HW_IRQ_EN",
  82. "INT_TASKLET_ENTRY",
  83. "FS_DEVRW",
  84. "MEM_SWAPOUT",
  85. "TIMERADD",
  86. "SCSI_DISPATCH_CMD",
  87. "IDE_CORE_CP"
  88. };
  89. static char* cp_type[] = {
  90. "PANIC",
  91. "BUG",
  92. "EXCEPTION",
  93. "LOOP",
  94. "OVERFLOW"
  95. };
  96. static struct jprobe lkdtm;
  97. static int lkdtm_parse_commandline(void);
  98. static void lkdtm_handler(void);
  99. static char* cpoint_name = INVALID;
  100. static char* cpoint_type = NONE;
  101. static int cpoint_count = DEFAULT_COUNT;
  102. static int recur_count = REC_NUM_DEFAULT;
  103. static enum cname cpoint = INVALID;
  104. static enum ctype cptype = NONE;
  105. static int count = DEFAULT_COUNT;
  106. module_param(recur_count, int, 0644);
  107. MODULE_PARM_DESC(recur_count, "Recurcion level for the stack overflow test,\
  108. default is 10");
  109. module_param(cpoint_name, charp, 0644);
  110. MODULE_PARM_DESC(cpoint_name, "Crash Point, where kernel is to be crashed");
  111. module_param(cpoint_type, charp, 06444);
  112. MODULE_PARM_DESC(cpoint_type, "Crash Point Type, action to be taken on\
  113. hitting the crash point");
  114. module_param(cpoint_count, int, 06444);
  115. MODULE_PARM_DESC(cpoint_count, "Crash Point Count, number of times the \
  116. crash point is to be hit to trigger action");
  117. unsigned int jp_do_irq(unsigned int irq)
  118. {
  119. lkdtm_handler();
  120. jprobe_return();
  121. return 0;
  122. }
  123. irqreturn_t jp_handle_irq_event(unsigned int irq, struct irqaction *action)
  124. {
  125. lkdtm_handler();
  126. jprobe_return();
  127. return 0;
  128. }
  129. void jp_tasklet_action(struct softirq_action *a)
  130. {
  131. lkdtm_handler();
  132. jprobe_return();
  133. }
  134. void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
  135. {
  136. lkdtm_handler();
  137. jprobe_return();
  138. }
  139. struct scan_control;
  140. unsigned long jp_shrink_page_list(struct list_head *page_list,
  141. struct scan_control *sc)
  142. {
  143. lkdtm_handler();
  144. jprobe_return();
  145. return 0;
  146. }
  147. int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
  148. const enum hrtimer_mode mode)
  149. {
  150. lkdtm_handler();
  151. jprobe_return();
  152. return 0;
  153. }
  154. int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
  155. {
  156. lkdtm_handler();
  157. jprobe_return();
  158. return 0;
  159. }
  160. #ifdef CONFIG_IDE
  161. int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
  162. struct block_device *bdev, unsigned int cmd,
  163. unsigned long arg)
  164. {
  165. lkdtm_handler();
  166. jprobe_return();
  167. return 0;
  168. }
  169. #endif
  170. static int lkdtm_parse_commandline(void)
  171. {
  172. int i;
  173. if (cpoint_name == INVALID || cpoint_type == NONE ||
  174. cpoint_count < 1 || recur_count < 1)
  175. return -EINVAL;
  176. for (i = 0; i < NUM_CPOINTS; ++i) {
  177. if (!strcmp(cpoint_name, cp_name[i])) {
  178. cpoint = i + 1;
  179. break;
  180. }
  181. }
  182. for (i = 0; i < NUM_CPOINT_TYPES; ++i) {
  183. if (!strcmp(cpoint_type, cp_type[i])) {
  184. cptype = i + 1;
  185. break;
  186. }
  187. }
  188. if (cpoint == INVALID || cptype == NONE)
  189. return -EINVAL;
  190. count = cpoint_count;
  191. return 0;
  192. }
  193. static int recursive_loop(int a)
  194. {
  195. char buf[1024];
  196. memset(buf,0xFF,1024);
  197. recur_count--;
  198. if (!recur_count)
  199. return 0;
  200. else
  201. return recursive_loop(a);
  202. }
  203. void lkdtm_handler(void)
  204. {
  205. printk(KERN_INFO "lkdtm : Crash point %s of type %s hit\n",
  206. cpoint_name, cpoint_type);
  207. --count;
  208. if (count == 0) {
  209. switch (cptype) {
  210. case NONE:
  211. break;
  212. case PANIC:
  213. printk(KERN_INFO "lkdtm : PANIC\n");
  214. panic("dumptest");
  215. break;
  216. case BUG:
  217. printk(KERN_INFO "lkdtm : BUG\n");
  218. BUG();
  219. break;
  220. case EXCEPTION:
  221. printk(KERN_INFO "lkdtm : EXCEPTION\n");
  222. *((int *) 0) = 0;
  223. break;
  224. case LOOP:
  225. printk(KERN_INFO "lkdtm : LOOP\n");
  226. for (;;);
  227. break;
  228. case OVERFLOW:
  229. printk(KERN_INFO "lkdtm : OVERFLOW\n");
  230. (void) recursive_loop(0);
  231. break;
  232. default:
  233. break;
  234. }
  235. count = cpoint_count;
  236. }
  237. }
  238. int lkdtm_module_init(void)
  239. {
  240. int ret;
  241. if (lkdtm_parse_commandline() == -EINVAL) {
  242. printk(KERN_INFO "lkdtm : Invalid command\n");
  243. return -EINVAL;
  244. }
  245. switch (cpoint) {
  246. case INT_HARDWARE_ENTRY:
  247. lkdtm.kp.symbol_name = "__do_IRQ";
  248. lkdtm.entry = (kprobe_opcode_t*) jp_do_irq;
  249. break;
  250. case INT_HW_IRQ_EN:
  251. lkdtm.kp.symbol_name = "handle_IRQ_event";
  252. lkdtm.entry = (kprobe_opcode_t*) jp_handle_irq_event;
  253. break;
  254. case INT_TASKLET_ENTRY:
  255. lkdtm.kp.symbol_name = "tasklet_action";
  256. lkdtm.entry = (kprobe_opcode_t*) jp_tasklet_action;
  257. break;
  258. case FS_DEVRW:
  259. lkdtm.kp.symbol_name = "ll_rw_block";
  260. lkdtm.entry = (kprobe_opcode_t*) jp_ll_rw_block;
  261. break;
  262. case MEM_SWAPOUT:
  263. lkdtm.kp.symbol_name = "shrink_page_list";
  264. lkdtm.entry = (kprobe_opcode_t*) jp_shrink_page_list;
  265. break;
  266. case TIMERADD:
  267. lkdtm.kp.symbol_name = "hrtimer_start";
  268. lkdtm.entry = (kprobe_opcode_t*) jp_hrtimer_start;
  269. break;
  270. case SCSI_DISPATCH_CMD:
  271. lkdtm.kp.symbol_name = "scsi_dispatch_cmd";
  272. lkdtm.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd;
  273. break;
  274. case IDE_CORE_CP:
  275. #ifdef CONFIG_IDE
  276. lkdtm.kp.symbol_name = "generic_ide_ioctl";
  277. lkdtm.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl;
  278. #else
  279. printk(KERN_INFO "lkdtm : Crash point not available\n");
  280. #endif
  281. break;
  282. default:
  283. printk(KERN_INFO "lkdtm : Invalid Crash Point\n");
  284. break;
  285. }
  286. if ((ret = register_jprobe(&lkdtm)) < 0) {
  287. printk(KERN_INFO "lkdtm : Couldn't register jprobe\n");
  288. return ret;
  289. }
  290. printk(KERN_INFO "lkdtm : Crash point %s of type %s registered\n",
  291. cpoint_name, cpoint_type);
  292. return 0;
  293. }
  294. void lkdtm_module_exit(void)
  295. {
  296. unregister_jprobe(&lkdtm);
  297. printk(KERN_INFO "lkdtm : Crash point unregistered\n");
  298. }
  299. module_init(lkdtm_module_init);
  300. module_exit(lkdtm_module_exit);
  301. MODULE_LICENSE("GPL");