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