lkdtm.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/fs.h>
  47. #include <linux/module.h>
  48. #include <linux/buffer_head.h>
  49. #include <linux/kprobes.h>
  50. #include <linux/list.h>
  51. #include <linux/init.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/hrtimer.h>
  54. #include <scsi/scsi_cmnd.h>
  55. #ifdef CONFIG_IDE
  56. #include <linux/ide.h>
  57. #endif
  58. #define NUM_CPOINTS 8
  59. #define NUM_CPOINT_TYPES 5
  60. #define DEFAULT_COUNT 10
  61. #define REC_NUM_DEFAULT 10
  62. enum cname {
  63. INVALID,
  64. INT_HARDWARE_ENTRY,
  65. INT_HW_IRQ_EN,
  66. INT_TASKLET_ENTRY,
  67. FS_DEVRW,
  68. MEM_SWAPOUT,
  69. TIMERADD,
  70. SCSI_DISPATCH_CMD,
  71. IDE_CORE_CP
  72. };
  73. enum ctype {
  74. NONE,
  75. PANIC,
  76. BUG,
  77. EXCEPTION,
  78. LOOP,
  79. OVERFLOW
  80. };
  81. static char* cp_name[] = {
  82. "INT_HARDWARE_ENTRY",
  83. "INT_HW_IRQ_EN",
  84. "INT_TASKLET_ENTRY",
  85. "FS_DEVRW",
  86. "MEM_SWAPOUT",
  87. "TIMERADD",
  88. "SCSI_DISPATCH_CMD",
  89. "IDE_CORE_CP"
  90. };
  91. static char* cp_type[] = {
  92. "PANIC",
  93. "BUG",
  94. "EXCEPTION",
  95. "LOOP",
  96. "OVERFLOW"
  97. };
  98. static struct jprobe lkdtm;
  99. static int lkdtm_parse_commandline(void);
  100. static void lkdtm_handler(void);
  101. static char* cpoint_name;
  102. static char* cpoint_type;
  103. static int cpoint_count = DEFAULT_COUNT;
  104. static int recur_count = REC_NUM_DEFAULT;
  105. static enum cname cpoint = INVALID;
  106. static enum ctype cptype = NONE;
  107. static int count = DEFAULT_COUNT;
  108. module_param(recur_count, int, 0644);
  109. MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test, "\
  110. "default is 10");
  111. module_param(cpoint_name, charp, 0644);
  112. MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
  113. module_param(cpoint_type, charp, 0644);
  114. MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
  115. "hitting the crash point");
  116. module_param(cpoint_count, int, 0644);
  117. MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
  118. "crash point is to be hit to trigger action");
  119. static unsigned int jp_do_irq(unsigned int irq)
  120. {
  121. lkdtm_handler();
  122. jprobe_return();
  123. return 0;
  124. }
  125. static irqreturn_t jp_handle_irq_event(unsigned int irq,
  126. struct irqaction *action)
  127. {
  128. lkdtm_handler();
  129. jprobe_return();
  130. return 0;
  131. }
  132. static void jp_tasklet_action(struct softirq_action *a)
  133. {
  134. lkdtm_handler();
  135. jprobe_return();
  136. }
  137. static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
  138. {
  139. lkdtm_handler();
  140. jprobe_return();
  141. }
  142. struct scan_control;
  143. static unsigned long jp_shrink_inactive_list(unsigned long max_scan,
  144. struct zone *zone,
  145. struct scan_control *sc)
  146. {
  147. lkdtm_handler();
  148. jprobe_return();
  149. return 0;
  150. }
  151. static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
  152. const enum hrtimer_mode mode)
  153. {
  154. lkdtm_handler();
  155. jprobe_return();
  156. return 0;
  157. }
  158. static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
  159. {
  160. lkdtm_handler();
  161. jprobe_return();
  162. return 0;
  163. }
  164. #ifdef CONFIG_IDE
  165. int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
  166. struct block_device *bdev, unsigned int cmd,
  167. unsigned long arg)
  168. {
  169. lkdtm_handler();
  170. jprobe_return();
  171. return 0;
  172. }
  173. #endif
  174. static int lkdtm_parse_commandline(void)
  175. {
  176. int i;
  177. if (cpoint_name == NULL || cpoint_type == NULL ||
  178. cpoint_count < 1 || recur_count < 1)
  179. return -EINVAL;
  180. for (i = 0; i < NUM_CPOINTS; ++i) {
  181. if (!strcmp(cpoint_name, cp_name[i])) {
  182. cpoint = i + 1;
  183. break;
  184. }
  185. }
  186. for (i = 0; i < NUM_CPOINT_TYPES; ++i) {
  187. if (!strcmp(cpoint_type, cp_type[i])) {
  188. cptype = i + 1;
  189. break;
  190. }
  191. }
  192. if (cpoint == INVALID || cptype == NONE)
  193. return -EINVAL;
  194. count = cpoint_count;
  195. return 0;
  196. }
  197. static int recursive_loop(int a)
  198. {
  199. char buf[1024];
  200. memset(buf,0xFF,1024);
  201. recur_count--;
  202. if (!recur_count)
  203. return 0;
  204. else
  205. return recursive_loop(a);
  206. }
  207. void lkdtm_handler(void)
  208. {
  209. printk(KERN_INFO "lkdtm : Crash point %s of type %s hit\n",
  210. cpoint_name, cpoint_type);
  211. --count;
  212. if (count == 0) {
  213. switch (cptype) {
  214. case NONE:
  215. break;
  216. case PANIC:
  217. printk(KERN_INFO "lkdtm : PANIC\n");
  218. panic("dumptest");
  219. break;
  220. case BUG:
  221. printk(KERN_INFO "lkdtm : BUG\n");
  222. BUG();
  223. break;
  224. case EXCEPTION:
  225. printk(KERN_INFO "lkdtm : EXCEPTION\n");
  226. *((int *) 0) = 0;
  227. break;
  228. case LOOP:
  229. printk(KERN_INFO "lkdtm : LOOP\n");
  230. for (;;);
  231. break;
  232. case OVERFLOW:
  233. printk(KERN_INFO "lkdtm : OVERFLOW\n");
  234. (void) recursive_loop(0);
  235. break;
  236. default:
  237. break;
  238. }
  239. count = cpoint_count;
  240. }
  241. }
  242. static int __init lkdtm_module_init(void)
  243. {
  244. int ret;
  245. if (lkdtm_parse_commandline() == -EINVAL) {
  246. printk(KERN_INFO "lkdtm : Invalid command\n");
  247. return -EINVAL;
  248. }
  249. switch (cpoint) {
  250. case INT_HARDWARE_ENTRY:
  251. lkdtm.kp.symbol_name = "__do_IRQ";
  252. lkdtm.entry = (kprobe_opcode_t*) jp_do_irq;
  253. break;
  254. case INT_HW_IRQ_EN:
  255. lkdtm.kp.symbol_name = "handle_IRQ_event";
  256. lkdtm.entry = (kprobe_opcode_t*) jp_handle_irq_event;
  257. break;
  258. case INT_TASKLET_ENTRY:
  259. lkdtm.kp.symbol_name = "tasklet_action";
  260. lkdtm.entry = (kprobe_opcode_t*) jp_tasklet_action;
  261. break;
  262. case FS_DEVRW:
  263. lkdtm.kp.symbol_name = "ll_rw_block";
  264. lkdtm.entry = (kprobe_opcode_t*) jp_ll_rw_block;
  265. break;
  266. case MEM_SWAPOUT:
  267. lkdtm.kp.symbol_name = "shrink_inactive_list";
  268. lkdtm.entry = (kprobe_opcode_t*) jp_shrink_inactive_list;
  269. break;
  270. case TIMERADD:
  271. lkdtm.kp.symbol_name = "hrtimer_start";
  272. lkdtm.entry = (kprobe_opcode_t*) jp_hrtimer_start;
  273. break;
  274. case SCSI_DISPATCH_CMD:
  275. lkdtm.kp.symbol_name = "scsi_dispatch_cmd";
  276. lkdtm.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd;
  277. break;
  278. case IDE_CORE_CP:
  279. #ifdef CONFIG_IDE
  280. lkdtm.kp.symbol_name = "generic_ide_ioctl";
  281. lkdtm.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl;
  282. #else
  283. printk(KERN_INFO "lkdtm : Crash point not available\n");
  284. #endif
  285. break;
  286. default:
  287. printk(KERN_INFO "lkdtm : Invalid Crash Point\n");
  288. break;
  289. }
  290. if ((ret = register_jprobe(&lkdtm)) < 0) {
  291. printk(KERN_INFO "lkdtm : Couldn't register jprobe\n");
  292. return ret;
  293. }
  294. printk(KERN_INFO "lkdtm : Crash point %s of type %s registered\n",
  295. cpoint_name, cpoint_type);
  296. return 0;
  297. }
  298. static void __exit lkdtm_module_exit(void)
  299. {
  300. unregister_jprobe(&lkdtm);
  301. printk(KERN_INFO "lkdtm : Crash point unregistered\n");
  302. }
  303. module_init(lkdtm_module_init);
  304. module_exit(lkdtm_module_exit);
  305. MODULE_LICENSE("GPL");