blob: b1323fc881b1baa7c5bfd29979079b2b714c9ffb [file] [log] [blame]
Ankita Garg8bb31b92006-10-02 02:17:36 -07001/*
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 *
Simon Kagstrom0347af42010-03-05 13:42:49 -080029 * Debugfs support added by Simon Kagstrom <simon.kagstrom@netinsight.net>
Ankita Garg8bb31b92006-10-02 02:17:36 -070030 *
Simon Kagstrom0347af42010-03-05 13:42:49 -080031 * See Documentation/fault-injection/provoke-crashes.txt for instructions
Ankita Garg8bb31b92006-10-02 02:17:36 -070032 */
33
34#include <linux/kernel.h>
Randy Dunlap5d861d92006-11-02 22:07:06 -080035#include <linux/fs.h>
Ankita Garg8bb31b92006-10-02 02:17:36 -070036#include <linux/module.h>
Randy Dunlap5d861d92006-11-02 22:07:06 -080037#include <linux/buffer_head.h>
Ankita Garg8bb31b92006-10-02 02:17:36 -070038#include <linux/kprobes.h>
Randy Dunlap5d861d92006-11-02 22:07:06 -080039#include <linux/list.h>
Ankita Garg8bb31b92006-10-02 02:17:36 -070040#include <linux/init.h>
Ankita Garg8bb31b92006-10-02 02:17:36 -070041#include <linux/interrupt.h>
Randy Dunlap5d861d92006-11-02 22:07:06 -080042#include <linux/hrtimer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Ankita Garg8bb31b92006-10-02 02:17:36 -070044#include <scsi/scsi_cmnd.h>
Simon Kagstrom0347af42010-03-05 13:42:49 -080045#include <linux/debugfs.h>
Ankita Garg8bb31b92006-10-02 02:17:36 -070046
47#ifdef CONFIG_IDE
48#include <linux/ide.h>
49#endif
50
Ankita Garg8bb31b92006-10-02 02:17:36 -070051#define DEFAULT_COUNT 10
52#define REC_NUM_DEFAULT 10
53
54enum cname {
Namhyung Kim93e2f582010-10-26 14:22:40 -070055 CN_INVALID,
56 CN_INT_HARDWARE_ENTRY,
57 CN_INT_HW_IRQ_EN,
58 CN_INT_TASKLET_ENTRY,
59 CN_FS_DEVRW,
60 CN_MEM_SWAPOUT,
61 CN_TIMERADD,
62 CN_SCSI_DISPATCH_CMD,
63 CN_IDE_CORE_CP,
64 CN_DIRECT,
Ankita Garg8bb31b92006-10-02 02:17:36 -070065};
66
67enum ctype {
Namhyung Kim93e2f582010-10-26 14:22:40 -070068 CT_NONE,
69 CT_PANIC,
70 CT_BUG,
Kees Cook658927232013-07-08 10:01:31 -070071 CT_WARNING,
Namhyung Kim93e2f582010-10-26 14:22:40 -070072 CT_EXCEPTION,
73 CT_LOOP,
74 CT_OVERFLOW,
75 CT_CORRUPT_STACK,
76 CT_UNALIGNED_LOAD_STORE_WRITE,
77 CT_OVERWRITE_ALLOCATION,
78 CT_WRITE_AFTER_FREE,
79 CT_SOFTLOCKUP,
80 CT_HARDLOCKUP,
81 CT_HUNG_TASK,
Ankita Garg8bb31b92006-10-02 02:17:36 -070082};
83
84static char* cp_name[] = {
85 "INT_HARDWARE_ENTRY",
86 "INT_HW_IRQ_EN",
87 "INT_TASKLET_ENTRY",
88 "FS_DEVRW",
89 "MEM_SWAPOUT",
90 "TIMERADD",
91 "SCSI_DISPATCH_CMD",
Simon Kagstrom0347af42010-03-05 13:42:49 -080092 "IDE_CORE_CP",
93 "DIRECT",
Ankita Garg8bb31b92006-10-02 02:17:36 -070094};
95
96static char* cp_type[] = {
97 "PANIC",
98 "BUG",
Kees Cook658927232013-07-08 10:01:31 -070099 "WARNING",
Ankita Garg8bb31b92006-10-02 02:17:36 -0700100 "EXCEPTION",
101 "LOOP",
Simon Kagstrom0347af42010-03-05 13:42:49 -0800102 "OVERFLOW",
103 "CORRUPT_STACK",
104 "UNALIGNED_LOAD_STORE_WRITE",
105 "OVERWRITE_ALLOCATION",
106 "WRITE_AFTER_FREE",
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700107 "SOFTLOCKUP",
108 "HARDLOCKUP",
109 "HUNG_TASK",
Ankita Garg8bb31b92006-10-02 02:17:36 -0700110};
111
112static struct jprobe lkdtm;
113
114static int lkdtm_parse_commandline(void);
115static void lkdtm_handler(void);
116
Al Viroec1c6202007-02-09 16:05:17 +0000117static char* cpoint_name;
118static char* cpoint_type;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700119static int cpoint_count = DEFAULT_COUNT;
120static int recur_count = REC_NUM_DEFAULT;
121
Namhyung Kim93e2f582010-10-26 14:22:40 -0700122static enum cname cpoint = CN_INVALID;
123static enum ctype cptype = CT_NONE;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700124static int count = DEFAULT_COUNT;
Josh Huntaa2c96d2011-06-27 16:18:08 -0700125static DEFINE_SPINLOCK(count_lock);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700126
127module_param(recur_count, int, 0644);
Randy Dunlap5d861d92006-11-02 22:07:06 -0800128MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test, "\
129 "default is 10");
Rusty Russelldca41302010-08-11 23:04:21 -0600130module_param(cpoint_name, charp, 0444);
Randy Dunlap5d861d92006-11-02 22:07:06 -0800131MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
Rusty Russelldca41302010-08-11 23:04:21 -0600132module_param(cpoint_type, charp, 0444);
Randy Dunlap5d861d92006-11-02 22:07:06 -0800133MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
134 "hitting the crash point");
135module_param(cpoint_count, int, 0644);
136MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
137 "crash point is to be hit to trigger action");
Ankita Garg8bb31b92006-10-02 02:17:36 -0700138
Adrian Bunk21181162008-02-06 01:36:50 -0800139static unsigned int jp_do_irq(unsigned int irq)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700140{
141 lkdtm_handler();
142 jprobe_return();
143 return 0;
144}
145
Adrian Bunk21181162008-02-06 01:36:50 -0800146static irqreturn_t jp_handle_irq_event(unsigned int irq,
147 struct irqaction *action)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700148{
149 lkdtm_handler();
150 jprobe_return();
151 return 0;
152}
153
Adrian Bunk21181162008-02-06 01:36:50 -0800154static void jp_tasklet_action(struct softirq_action *a)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700155{
156 lkdtm_handler();
157 jprobe_return();
158}
159
Adrian Bunk21181162008-02-06 01:36:50 -0800160static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
Ankita Garg8bb31b92006-10-02 02:17:36 -0700161{
162 lkdtm_handler();
163 jprobe_return();
164}
165
166struct scan_control;
167
Adrian Bunk21181162008-02-06 01:36:50 -0800168static unsigned long jp_shrink_inactive_list(unsigned long max_scan,
169 struct zone *zone,
170 struct scan_control *sc)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700171{
172 lkdtm_handler();
173 jprobe_return();
174 return 0;
175}
176
Adrian Bunk21181162008-02-06 01:36:50 -0800177static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
178 const enum hrtimer_mode mode)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700179{
180 lkdtm_handler();
181 jprobe_return();
182 return 0;
183}
184
Adrian Bunk21181162008-02-06 01:36:50 -0800185static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700186{
187 lkdtm_handler();
188 jprobe_return();
189 return 0;
190}
191
192#ifdef CONFIG_IDE
193int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
194 struct block_device *bdev, unsigned int cmd,
195 unsigned long arg)
196{
197 lkdtm_handler();
198 jprobe_return();
199 return 0;
200}
201#endif
202
Simon Kagstrom0347af42010-03-05 13:42:49 -0800203/* Return the crashpoint number or NONE if the name is invalid */
204static enum ctype parse_cp_type(const char *what, size_t count)
205{
206 int i;
207
208 for (i = 0; i < ARRAY_SIZE(cp_type); i++) {
209 if (!strcmp(what, cp_type[i]))
210 return i + 1;
211 }
212
Namhyung Kim93e2f582010-10-26 14:22:40 -0700213 return CT_NONE;
Simon Kagstrom0347af42010-03-05 13:42:49 -0800214}
215
216static const char *cp_type_to_str(enum ctype type)
217{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700218 if (type == CT_NONE || type < 0 || type > ARRAY_SIZE(cp_type))
Simon Kagstrom0347af42010-03-05 13:42:49 -0800219 return "None";
220
221 return cp_type[type - 1];
222}
223
224static const char *cp_name_to_str(enum cname name)
225{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700226 if (name == CN_INVALID || name < 0 || name > ARRAY_SIZE(cp_name))
Simon Kagstrom0347af42010-03-05 13:42:49 -0800227 return "INVALID";
228
229 return cp_name[name - 1];
230}
231
232
Ankita Garg8bb31b92006-10-02 02:17:36 -0700233static int lkdtm_parse_commandline(void)
234{
235 int i;
Josh Huntaa2c96d2011-06-27 16:18:08 -0700236 unsigned long flags;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700237
Simon Kagstrom0347af42010-03-05 13:42:49 -0800238 if (cpoint_count < 1 || recur_count < 1)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700239 return -EINVAL;
240
Josh Huntaa2c96d2011-06-27 16:18:08 -0700241 spin_lock_irqsave(&count_lock, flags);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700242 count = cpoint_count;
Josh Huntaa2c96d2011-06-27 16:18:08 -0700243 spin_unlock_irqrestore(&count_lock, flags);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700244
Simon Kagstrom0347af42010-03-05 13:42:49 -0800245 /* No special parameters */
246 if (!cpoint_type && !cpoint_name)
247 return 0;
248
249 /* Neither or both of these need to be set */
250 if (!cpoint_type || !cpoint_name)
251 return -EINVAL;
252
253 cptype = parse_cp_type(cpoint_type, strlen(cpoint_type));
Namhyung Kim93e2f582010-10-26 14:22:40 -0700254 if (cptype == CT_NONE)
Simon Kagstrom0347af42010-03-05 13:42:49 -0800255 return -EINVAL;
256
257 for (i = 0; i < ARRAY_SIZE(cp_name); i++) {
258 if (!strcmp(cpoint_name, cp_name[i])) {
259 cpoint = i + 1;
260 return 0;
261 }
262 }
263
264 /* Could not find a valid crash point */
265 return -EINVAL;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700266}
267
268static int recursive_loop(int a)
269{
270 char buf[1024];
271
272 memset(buf,0xFF,1024);
273 recur_count--;
274 if (!recur_count)
275 return 0;
276 else
277 return recursive_loop(a);
278}
279
Simon Kagstrom0347af42010-03-05 13:42:49 -0800280static void lkdtm_do_action(enum ctype which)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700281{
Simon Kagstrom0347af42010-03-05 13:42:49 -0800282 switch (which) {
Namhyung Kim93e2f582010-10-26 14:22:40 -0700283 case CT_PANIC:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800284 panic("dumptest");
285 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700286 case CT_BUG:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800287 BUG();
288 break;
Kees Cook658927232013-07-08 10:01:31 -0700289 case CT_WARNING:
290 WARN_ON(1);
291 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700292 case CT_EXCEPTION:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800293 *((int *) 0) = 0;
294 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700295 case CT_LOOP:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800296 for (;;)
297 ;
298 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700299 case CT_OVERFLOW:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800300 (void) recursive_loop(0);
301 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700302 case CT_CORRUPT_STACK: {
Kees Cook4f198282013-07-08 10:01:30 -0700303 /* Make sure the compiler creates and uses an 8 char array. */
304 volatile char data[8];
Simon Kagstrom0347af42010-03-05 13:42:49 -0800305
Kees Cook4f198282013-07-08 10:01:30 -0700306 memset((void *)data, 0, 64);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800307 break;
308 }
Namhyung Kim93e2f582010-10-26 14:22:40 -0700309 case CT_UNALIGNED_LOAD_STORE_WRITE: {
Simon Kagstrom0347af42010-03-05 13:42:49 -0800310 static u8 data[5] __attribute__((aligned(4))) = {1, 2,
311 3, 4, 5};
312 u32 *p;
313 u32 val = 0x12345678;
314
315 p = (u32 *)(data + 1);
316 if (*p == 0)
317 val = 0x87654321;
318 *p = val;
319 break;
320 }
Namhyung Kim93e2f582010-10-26 14:22:40 -0700321 case CT_OVERWRITE_ALLOCATION: {
Simon Kagstrom0347af42010-03-05 13:42:49 -0800322 size_t len = 1020;
323 u32 *data = kmalloc(len, GFP_KERNEL);
324
325 data[1024 / sizeof(u32)] = 0x12345678;
326 kfree(data);
327 break;
328 }
Namhyung Kim93e2f582010-10-26 14:22:40 -0700329 case CT_WRITE_AFTER_FREE: {
Simon Kagstrom0347af42010-03-05 13:42:49 -0800330 size_t len = 1024;
331 u32 *data = kmalloc(len, GFP_KERNEL);
332
333 kfree(data);
334 schedule();
335 memset(data, 0x78, len);
336 break;
337 }
Namhyung Kim93e2f582010-10-26 14:22:40 -0700338 case CT_SOFTLOCKUP:
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700339 preempt_disable();
340 for (;;)
341 cpu_relax();
342 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700343 case CT_HARDLOCKUP:
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700344 local_irq_disable();
345 for (;;)
346 cpu_relax();
347 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700348 case CT_HUNG_TASK:
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700349 set_current_state(TASK_UNINTERRUPTIBLE);
350 schedule();
351 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700352 case CT_NONE:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800353 default:
354 break;
355 }
356
357}
358
359static void lkdtm_handler(void)
360{
Josh Huntaa2c96d2011-06-27 16:18:08 -0700361 unsigned long flags;
Cong Wang92618182012-02-03 15:37:15 -0800362 bool do_it = false;
Josh Huntaa2c96d2011-06-27 16:18:08 -0700363
364 spin_lock_irqsave(&count_lock, flags);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800365 count--;
366 printk(KERN_INFO "lkdtm: Crash point %s of type %s hit, trigger in %d rounds\n",
367 cp_name_to_str(cpoint), cp_type_to_str(cptype), count);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700368
369 if (count == 0) {
Cong Wang92618182012-02-03 15:37:15 -0800370 do_it = true;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700371 count = cpoint_count;
372 }
Josh Huntaa2c96d2011-06-27 16:18:08 -0700373 spin_unlock_irqrestore(&count_lock, flags);
Cong Wang92618182012-02-03 15:37:15 -0800374
375 if (do_it)
376 lkdtm_do_action(cptype);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700377}
378
Simon Kagstrom0347af42010-03-05 13:42:49 -0800379static int lkdtm_register_cpoint(enum cname which)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700380{
381 int ret;
382
Namhyung Kim93e2f582010-10-26 14:22:40 -0700383 cpoint = CN_INVALID;
Simon Kagstrom0347af42010-03-05 13:42:49 -0800384 if (lkdtm.entry != NULL)
385 unregister_jprobe(&lkdtm);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700386
Simon Kagstrom0347af42010-03-05 13:42:49 -0800387 switch (which) {
Namhyung Kim93e2f582010-10-26 14:22:40 -0700388 case CN_DIRECT:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800389 lkdtm_do_action(cptype);
390 return 0;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700391 case CN_INT_HARDWARE_ENTRY:
M. Mohan Kumarf58f2fa2009-09-22 16:43:29 -0700392 lkdtm.kp.symbol_name = "do_IRQ";
Ankita Garg8bb31b92006-10-02 02:17:36 -0700393 lkdtm.entry = (kprobe_opcode_t*) jp_do_irq;
394 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700395 case CN_INT_HW_IRQ_EN:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700396 lkdtm.kp.symbol_name = "handle_IRQ_event";
397 lkdtm.entry = (kprobe_opcode_t*) jp_handle_irq_event;
398 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700399 case CN_INT_TASKLET_ENTRY:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700400 lkdtm.kp.symbol_name = "tasklet_action";
401 lkdtm.entry = (kprobe_opcode_t*) jp_tasklet_action;
402 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700403 case CN_FS_DEVRW:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700404 lkdtm.kp.symbol_name = "ll_rw_block";
405 lkdtm.entry = (kprobe_opcode_t*) jp_ll_rw_block;
406 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700407 case CN_MEM_SWAPOUT:
Ankita Garg18a61e42006-11-05 23:52:07 -0800408 lkdtm.kp.symbol_name = "shrink_inactive_list";
409 lkdtm.entry = (kprobe_opcode_t*) jp_shrink_inactive_list;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700410 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700411 case CN_TIMERADD:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700412 lkdtm.kp.symbol_name = "hrtimer_start";
413 lkdtm.entry = (kprobe_opcode_t*) jp_hrtimer_start;
414 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700415 case CN_SCSI_DISPATCH_CMD:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700416 lkdtm.kp.symbol_name = "scsi_dispatch_cmd";
417 lkdtm.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd;
418 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700419 case CN_IDE_CORE_CP:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700420#ifdef CONFIG_IDE
421 lkdtm.kp.symbol_name = "generic_ide_ioctl";
422 lkdtm.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl;
423#else
Simon Kagstrom0347af42010-03-05 13:42:49 -0800424 printk(KERN_INFO "lkdtm: Crash point not available\n");
425 return -EINVAL;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700426#endif
427 break;
428 default:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800429 printk(KERN_INFO "lkdtm: Invalid Crash Point\n");
430 return -EINVAL;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700431 }
432
Simon Kagstrom0347af42010-03-05 13:42:49 -0800433 cpoint = which;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700434 if ((ret = register_jprobe(&lkdtm)) < 0) {
Simon Kagstrom0347af42010-03-05 13:42:49 -0800435 printk(KERN_INFO "lkdtm: Couldn't register jprobe\n");
Namhyung Kim93e2f582010-10-26 14:22:40 -0700436 cpoint = CN_INVALID;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700437 }
438
Simon Kagstrom0347af42010-03-05 13:42:49 -0800439 return ret;
440}
441
442static ssize_t do_register_entry(enum cname which, struct file *f,
443 const char __user *user_buf, size_t count, loff_t *off)
444{
445 char *buf;
446 int err;
447
448 if (count >= PAGE_SIZE)
449 return -EINVAL;
450
451 buf = (char *)__get_free_page(GFP_KERNEL);
452 if (!buf)
453 return -ENOMEM;
454 if (copy_from_user(buf, user_buf, count)) {
455 free_page((unsigned long) buf);
456 return -EFAULT;
457 }
458 /* NULL-terminate and remove enter */
459 buf[count] = '\0';
460 strim(buf);
461
462 cptype = parse_cp_type(buf, count);
463 free_page((unsigned long) buf);
464
Namhyung Kim93e2f582010-10-26 14:22:40 -0700465 if (cptype == CT_NONE)
Simon Kagstrom0347af42010-03-05 13:42:49 -0800466 return -EINVAL;
467
468 err = lkdtm_register_cpoint(which);
469 if (err < 0)
470 return err;
471
472 *off += count;
473
474 return count;
475}
476
477/* Generic read callback that just prints out the available crash types */
478static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
479 size_t count, loff_t *off)
480{
481 char *buf;
482 int i, n, out;
483
484 buf = (char *)__get_free_page(GFP_KERNEL);
Alan Cox086ff4b2012-07-30 14:43:24 -0700485 if (buf == NULL)
486 return -ENOMEM;
Simon Kagstrom0347af42010-03-05 13:42:49 -0800487
488 n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
489 for (i = 0; i < ARRAY_SIZE(cp_type); i++)
490 n += snprintf(buf + n, PAGE_SIZE - n, "%s\n", cp_type[i]);
491 buf[n] = '\0';
492
493 out = simple_read_from_buffer(user_buf, count, off,
494 buf, n);
495 free_page((unsigned long) buf);
496
497 return out;
498}
499
500static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
501{
Ankita Garg8bb31b92006-10-02 02:17:36 -0700502 return 0;
503}
504
Simon Kagstrom0347af42010-03-05 13:42:49 -0800505
506static ssize_t int_hardware_entry(struct file *f, const char __user *buf,
507 size_t count, loff_t *off)
508{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700509 return do_register_entry(CN_INT_HARDWARE_ENTRY, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800510}
511
512static ssize_t int_hw_irq_en(struct file *f, const char __user *buf,
513 size_t count, loff_t *off)
514{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700515 return do_register_entry(CN_INT_HW_IRQ_EN, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800516}
517
518static ssize_t int_tasklet_entry(struct file *f, const char __user *buf,
519 size_t count, loff_t *off)
520{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700521 return do_register_entry(CN_INT_TASKLET_ENTRY, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800522}
523
524static ssize_t fs_devrw_entry(struct file *f, const char __user *buf,
525 size_t count, loff_t *off)
526{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700527 return do_register_entry(CN_FS_DEVRW, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800528}
529
530static ssize_t mem_swapout_entry(struct file *f, const char __user *buf,
531 size_t count, loff_t *off)
532{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700533 return do_register_entry(CN_MEM_SWAPOUT, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800534}
535
536static ssize_t timeradd_entry(struct file *f, const char __user *buf,
537 size_t count, loff_t *off)
538{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700539 return do_register_entry(CN_TIMERADD, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800540}
541
542static ssize_t scsi_dispatch_cmd_entry(struct file *f,
543 const char __user *buf, size_t count, loff_t *off)
544{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700545 return do_register_entry(CN_SCSI_DISPATCH_CMD, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800546}
547
548static ssize_t ide_core_cp_entry(struct file *f, const char __user *buf,
549 size_t count, loff_t *off)
550{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700551 return do_register_entry(CN_IDE_CORE_CP, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800552}
553
554/* Special entry to just crash directly. Available without KPROBEs */
555static ssize_t direct_entry(struct file *f, const char __user *user_buf,
556 size_t count, loff_t *off)
557{
558 enum ctype type;
559 char *buf;
560
561 if (count >= PAGE_SIZE)
562 return -EINVAL;
563 if (count < 1)
564 return -EINVAL;
565
566 buf = (char *)__get_free_page(GFP_KERNEL);
567 if (!buf)
568 return -ENOMEM;
569 if (copy_from_user(buf, user_buf, count)) {
570 free_page((unsigned long) buf);
571 return -EFAULT;
572 }
573 /* NULL-terminate and remove enter */
574 buf[count] = '\0';
575 strim(buf);
576
577 type = parse_cp_type(buf, count);
578 free_page((unsigned long) buf);
Namhyung Kim93e2f582010-10-26 14:22:40 -0700579 if (type == CT_NONE)
Simon Kagstrom0347af42010-03-05 13:42:49 -0800580 return -EINVAL;
581
582 printk(KERN_INFO "lkdtm: Performing direct entry %s\n",
583 cp_type_to_str(type));
584 lkdtm_do_action(type);
585 *off += count;
586
587 return count;
588}
589
590struct crash_entry {
591 const char *name;
592 const struct file_operations fops;
593};
594
595static const struct crash_entry crash_entries[] = {
596 {"DIRECT", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200597 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800598 .open = lkdtm_debugfs_open,
599 .write = direct_entry} },
600 {"INT_HARDWARE_ENTRY", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200601 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800602 .open = lkdtm_debugfs_open,
603 .write = int_hardware_entry} },
604 {"INT_HW_IRQ_EN", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200605 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800606 .open = lkdtm_debugfs_open,
607 .write = int_hw_irq_en} },
608 {"INT_TASKLET_ENTRY", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200609 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800610 .open = lkdtm_debugfs_open,
611 .write = int_tasklet_entry} },
612 {"FS_DEVRW", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200613 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800614 .open = lkdtm_debugfs_open,
615 .write = fs_devrw_entry} },
616 {"MEM_SWAPOUT", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200617 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800618 .open = lkdtm_debugfs_open,
619 .write = mem_swapout_entry} },
620 {"TIMERADD", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200621 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800622 .open = lkdtm_debugfs_open,
623 .write = timeradd_entry} },
624 {"SCSI_DISPATCH_CMD", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200625 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800626 .open = lkdtm_debugfs_open,
627 .write = scsi_dispatch_cmd_entry} },
628 {"IDE_CORE_CP", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200629 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800630 .open = lkdtm_debugfs_open,
631 .write = ide_core_cp_entry} },
632};
633
634static struct dentry *lkdtm_debugfs_root;
635
636static int __init lkdtm_module_init(void)
637{
638 int ret = -EINVAL;
639 int n_debugfs_entries = 1; /* Assume only the direct entry */
640 int i;
641
642 /* Register debugfs interface */
643 lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
644 if (!lkdtm_debugfs_root) {
645 printk(KERN_ERR "lkdtm: creating root dir failed\n");
646 return -ENODEV;
647 }
648
649#ifdef CONFIG_KPROBES
650 n_debugfs_entries = ARRAY_SIZE(crash_entries);
651#endif
652
653 for (i = 0; i < n_debugfs_entries; i++) {
654 const struct crash_entry *cur = &crash_entries[i];
655 struct dentry *de;
656
657 de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
658 NULL, &cur->fops);
659 if (de == NULL) {
660 printk(KERN_ERR "lkdtm: could not create %s\n",
661 cur->name);
662 goto out_err;
663 }
664 }
665
666 if (lkdtm_parse_commandline() == -EINVAL) {
667 printk(KERN_INFO "lkdtm: Invalid command\n");
668 goto out_err;
669 }
670
Namhyung Kim93e2f582010-10-26 14:22:40 -0700671 if (cpoint != CN_INVALID && cptype != CT_NONE) {
Simon Kagstrom0347af42010-03-05 13:42:49 -0800672 ret = lkdtm_register_cpoint(cpoint);
673 if (ret < 0) {
674 printk(KERN_INFO "lkdtm: Invalid crash point %d\n",
675 cpoint);
676 goto out_err;
677 }
678 printk(KERN_INFO "lkdtm: Crash point %s of type %s registered\n",
679 cpoint_name, cpoint_type);
680 } else {
681 printk(KERN_INFO "lkdtm: No crash points registered, enable through debugfs\n");
682 }
683
684 return 0;
685
686out_err:
687 debugfs_remove_recursive(lkdtm_debugfs_root);
688 return ret;
689}
690
Adrian Bunk21181162008-02-06 01:36:50 -0800691static void __exit lkdtm_module_exit(void)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700692{
Simon Kagstrom0347af42010-03-05 13:42:49 -0800693 debugfs_remove_recursive(lkdtm_debugfs_root);
694
695 unregister_jprobe(&lkdtm);
696 printk(KERN_INFO "lkdtm: Crash point unregistered\n");
Ankita Garg8bb31b92006-10-02 02:17:36 -0700697}
698
699module_init(lkdtm_module_init);
700module_exit(lkdtm_module_exit);
701
702MODULE_LICENSE("GPL");