lkdtm: reorganize module paramaters
[deliverable/linux.git] / drivers / misc / lkdtm_core.c
CommitLineData
8bb31b9d 1/*
426f3a53
KC
2 * Linux Kernel Dump Test Module for testing kernel crashes conditions:
3 * induces system failures at predefined crashpoints and under predefined
4 * operational conditions in order to evaluate the reliability of kernel
5 * sanity checking and crash dumps obtained using different dumping
6 * solutions.
8bb31b9d
AG
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 * Copyright (C) IBM Corporation, 2006
23 *
24 * Author: Ankita Garg <ankita@in.ibm.com>
25 *
8bb31b9d
AG
26 * It is adapted from the Linux Kernel Dump Test Tool by
27 * Fernando Luis Vazquez Cao <http://lkdtt.sourceforge.net>
28 *
0347af4e 29 * Debugfs support added by Simon Kagstrom <simon.kagstrom@netinsight.net>
8bb31b9d 30 *
0347af4e 31 * See Documentation/fault-injection/provoke-crashes.txt for instructions
8bb31b9d 32 */
426f3a53 33#define pr_fmt(fmt) "lkdtm: " fmt
8bb31b9d
AG
34
35#include <linux/kernel.h>
5d861d92 36#include <linux/fs.h>
8bb31b9d 37#include <linux/module.h>
5d861d92 38#include <linux/buffer_head.h>
8bb31b9d 39#include <linux/kprobes.h>
5d861d92 40#include <linux/list.h>
8bb31b9d 41#include <linux/init.h>
8bb31b9d 42#include <linux/interrupt.h>
5d861d92 43#include <linux/hrtimer.h>
5a0e3ad6 44#include <linux/slab.h>
8bb31b9d 45#include <scsi/scsi_cmnd.h>
0347af4e 46#include <linux/debugfs.h>
8bb31b9d
AG
47
48#ifdef CONFIG_IDE
49#include <linux/ide.h>
50#endif
51
9a49a528
KC
52#include "lkdtm.h"
53
d87c9788
KC
54#define DEFAULT_COUNT 10
55
56static int lkdtm_parse_commandline(void);
57static void lkdtm_handler(void);
58
8bb31b9d 59enum cname {
93e2f585
NK
60 CN_INVALID,
61 CN_INT_HARDWARE_ENTRY,
62 CN_INT_HW_IRQ_EN,
63 CN_INT_TASKLET_ENTRY,
64 CN_FS_DEVRW,
65 CN_MEM_SWAPOUT,
66 CN_TIMERADD,
67 CN_SCSI_DISPATCH_CMD,
68 CN_IDE_CORE_CP,
69 CN_DIRECT,
8bb31b9d
AG
70};
71
72enum ctype {
93e2f585
NK
73 CT_NONE,
74 CT_PANIC,
75 CT_BUG,
65892723 76 CT_WARNING,
93e2f585
NK
77 CT_EXCEPTION,
78 CT_LOOP,
79 CT_OVERFLOW,
80 CT_CORRUPT_STACK,
81 CT_UNALIGNED_LOAD_STORE_WRITE,
82 CT_OVERWRITE_ALLOCATION,
83 CT_WRITE_AFTER_FREE,
bc0b8cc6 84 CT_READ_AFTER_FREE,
920d451f
LA
85 CT_WRITE_BUDDY_AFTER_FREE,
86 CT_READ_BUDDY_AFTER_FREE,
93e2f585
NK
87 CT_SOFTLOCKUP,
88 CT_HARDLOCKUP,
274a5855 89 CT_SPINLOCKUP,
93e2f585 90 CT_HUNG_TASK,
cc33c537
KC
91 CT_EXEC_DATA,
92 CT_EXEC_STACK,
93 CT_EXEC_KMALLOC,
94 CT_EXEC_VMALLOC,
9a49a528 95 CT_EXEC_RODATA,
9ae113ce
KC
96 CT_EXEC_USERSPACE,
97 CT_ACCESS_USERSPACE,
98 CT_WRITE_RO,
7cca071c 99 CT_WRITE_RO_AFTER_INIT,
dc2b9e90 100 CT_WRITE_KERN,
b5484527
KC
101 CT_ATOMIC_UNDERFLOW,
102 CT_ATOMIC_OVERFLOW,
aa981a66
KC
103 CT_USERCOPY_HEAP_SIZE_TO,
104 CT_USERCOPY_HEAP_SIZE_FROM,
105 CT_USERCOPY_HEAP_FLAG_TO,
106 CT_USERCOPY_HEAP_FLAG_FROM,
107 CT_USERCOPY_STACK_FRAME_TO,
108 CT_USERCOPY_STACK_FRAME_FROM,
109 CT_USERCOPY_STACK_BEYOND,
6c352140 110 CT_USERCOPY_KERNEL,
8bb31b9d
AG
111};
112
113static char* cp_name[] = {
329d416c 114 "INVALID",
8bb31b9d
AG
115 "INT_HARDWARE_ENTRY",
116 "INT_HW_IRQ_EN",
117 "INT_TASKLET_ENTRY",
118 "FS_DEVRW",
119 "MEM_SWAPOUT",
120 "TIMERADD",
121 "SCSI_DISPATCH_CMD",
0347af4e
SK
122 "IDE_CORE_CP",
123 "DIRECT",
8bb31b9d
AG
124};
125
126static char* cp_type[] = {
329d416c 127 "NONE",
8bb31b9d
AG
128 "PANIC",
129 "BUG",
65892723 130 "WARNING",
8bb31b9d
AG
131 "EXCEPTION",
132 "LOOP",
0347af4e
SK
133 "OVERFLOW",
134 "CORRUPT_STACK",
135 "UNALIGNED_LOAD_STORE_WRITE",
136 "OVERWRITE_ALLOCATION",
137 "WRITE_AFTER_FREE",
bc0b8cc6 138 "READ_AFTER_FREE",
920d451f
LA
139 "WRITE_BUDDY_AFTER_FREE",
140 "READ_BUDDY_AFTER_FREE",
a48223f9
FW
141 "SOFTLOCKUP",
142 "HARDLOCKUP",
274a5855 143 "SPINLOCKUP",
a48223f9 144 "HUNG_TASK",
cc33c537
KC
145 "EXEC_DATA",
146 "EXEC_STACK",
147 "EXEC_KMALLOC",
148 "EXEC_VMALLOC",
9a49a528 149 "EXEC_RODATA",
9ae113ce
KC
150 "EXEC_USERSPACE",
151 "ACCESS_USERSPACE",
152 "WRITE_RO",
7cca071c 153 "WRITE_RO_AFTER_INIT",
dc2b9e90 154 "WRITE_KERN",
b5484527
KC
155 "ATOMIC_UNDERFLOW",
156 "ATOMIC_OVERFLOW",
aa981a66
KC
157 "USERCOPY_HEAP_SIZE_TO",
158 "USERCOPY_HEAP_SIZE_FROM",
159 "USERCOPY_HEAP_FLAG_TO",
160 "USERCOPY_HEAP_FLAG_FROM",
161 "USERCOPY_STACK_FRAME_TO",
162 "USERCOPY_STACK_FRAME_FROM",
163 "USERCOPY_STACK_BEYOND",
6c352140 164 "USERCOPY_KERNEL",
8bb31b9d
AG
165};
166
d87c9788 167/* Global jprobe entry and crashtype. */
38f95fe2 168static struct jprobe lkdtm_jprobe;
d87c9788
KC
169static enum cname lkdtm_crashpoint = CN_INVALID;
170static enum ctype lkdtm_crashtype = CT_NONE;
8bb31b9d 171
d87c9788 172/* Global crash counter and spinlock. */
76a10e2b
KC
173static int crash_count = DEFAULT_COUNT;
174static DEFINE_SPINLOCK(crash_count_lock);
8bb31b9d 175
d87c9788
KC
176/* Module parameters */
177static int recur_count = -1;
8bb31b9d 178module_param(recur_count, int, 0644);
7d196ac3 179MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
d87c9788
KC
180
181static char* cpoint_name;
dca41306 182module_param(cpoint_name, charp, 0444);
5d861d92 183MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
d87c9788
KC
184
185static char* cpoint_type;
dca41306 186module_param(cpoint_type, charp, 0444);
5d861d92
RD
187MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
188 "hitting the crash point");
d87c9788
KC
189
190static int cpoint_count = DEFAULT_COUNT;
5d861d92
RD
191module_param(cpoint_count, int, 0644);
192MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
193 "crash point is to be hit to trigger action");
8bb31b9d 194
2118116e 195static unsigned int jp_do_irq(unsigned int irq)
8bb31b9d
AG
196{
197 lkdtm_handler();
198 jprobe_return();
199 return 0;
200}
201
2118116e
AB
202static irqreturn_t jp_handle_irq_event(unsigned int irq,
203 struct irqaction *action)
8bb31b9d
AG
204{
205 lkdtm_handler();
206 jprobe_return();
207 return 0;
208}
209
2118116e 210static void jp_tasklet_action(struct softirq_action *a)
8bb31b9d
AG
211{
212 lkdtm_handler();
213 jprobe_return();
214}
215
2118116e 216static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
8bb31b9d
AG
217{
218 lkdtm_handler();
219 jprobe_return();
220}
221
222struct scan_control;
223
2118116e
AB
224static unsigned long jp_shrink_inactive_list(unsigned long max_scan,
225 struct zone *zone,
226 struct scan_control *sc)
8bb31b9d
AG
227{
228 lkdtm_handler();
229 jprobe_return();
230 return 0;
231}
232
2118116e
AB
233static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
234 const enum hrtimer_mode mode)
8bb31b9d
AG
235{
236 lkdtm_handler();
237 jprobe_return();
238 return 0;
239}
240
2118116e 241static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
8bb31b9d
AG
242{
243 lkdtm_handler();
244 jprobe_return();
245 return 0;
246}
247
248#ifdef CONFIG_IDE
44629432 249static int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
8bb31b9d
AG
250 struct block_device *bdev, unsigned int cmd,
251 unsigned long arg)
252{
253 lkdtm_handler();
254 jprobe_return();
255 return 0;
256}
257#endif
258
0347af4e
SK
259/* Return the crashpoint number or NONE if the name is invalid */
260static enum ctype parse_cp_type(const char *what, size_t count)
261{
262 int i;
263
264 for (i = 0; i < ARRAY_SIZE(cp_type); i++) {
265 if (!strcmp(what, cp_type[i]))
329d416c 266 return i;
0347af4e
SK
267 }
268
93e2f585 269 return CT_NONE;
0347af4e
SK
270}
271
272static const char *cp_type_to_str(enum ctype type)
273{
93e2f585 274 if (type == CT_NONE || type < 0 || type > ARRAY_SIZE(cp_type))
329d416c 275 return "NONE";
0347af4e 276
329d416c 277 return cp_type[type];
0347af4e
SK
278}
279
280static const char *cp_name_to_str(enum cname name)
281{
93e2f585 282 if (name == CN_INVALID || name < 0 || name > ARRAY_SIZE(cp_name))
0347af4e
SK
283 return "INVALID";
284
329d416c 285 return cp_name[name];
0347af4e
SK
286}
287
288
8bb31b9d
AG
289static int lkdtm_parse_commandline(void)
290{
291 int i;
aa2c96d6 292 unsigned long flags;
8bb31b9d 293
0347af4e 294 if (cpoint_count < 1 || recur_count < 1)
8bb31b9d
AG
295 return -EINVAL;
296
76a10e2b
KC
297 spin_lock_irqsave(&crash_count_lock, flags);
298 crash_count = cpoint_count;
299 spin_unlock_irqrestore(&crash_count_lock, flags);
0347af4e
SK
300
301 /* No special parameters */
302 if (!cpoint_type && !cpoint_name)
303 return 0;
304
305 /* Neither or both of these need to be set */
306 if (!cpoint_type || !cpoint_name)
307 return -EINVAL;
308
38f95fe2
KC
309 lkdtm_crashtype = parse_cp_type(cpoint_type, strlen(cpoint_type));
310 if (lkdtm_crashtype == CT_NONE)
0347af4e
SK
311 return -EINVAL;
312
329d416c
KC
313 /* Refuse INVALID as a selectable crashpoint name. */
314 if (!strcmp(cpoint_name, "INVALID"))
315 return -EINVAL;
316
0347af4e 317 for (i = 0; i < ARRAY_SIZE(cp_name); i++) {
8bb31b9d 318 if (!strcmp(cpoint_name, cp_name[i])) {
38f95fe2 319 lkdtm_crashpoint = i;
0347af4e 320 return 0;
8bb31b9d
AG
321 }
322 }
323
0347af4e
SK
324 /* Could not find a valid crash point */
325 return -EINVAL;
8bb31b9d
AG
326}
327
0347af4e 328static void lkdtm_do_action(enum ctype which)
8bb31b9d 329{
0347af4e 330 switch (which) {
93e2f585 331 case CT_PANIC:
00f496c4 332 lkdtm_PANIC();
0347af4e 333 break;
93e2f585 334 case CT_BUG:
00f496c4 335 lkdtm_BUG();
0347af4e 336 break;
65892723 337 case CT_WARNING:
00f496c4 338 lkdtm_WARNING();
65892723 339 break;
93e2f585 340 case CT_EXCEPTION:
00f496c4 341 lkdtm_EXCEPTION();
0347af4e 342 break;
93e2f585 343 case CT_LOOP:
00f496c4 344 lkdtm_LOOP();
0347af4e 345 break;
93e2f585 346 case CT_OVERFLOW:
00f496c4 347 lkdtm_OVERFLOW();
0347af4e 348 break;
629c66a2 349 case CT_CORRUPT_STACK:
00f496c4
KC
350 lkdtm_CORRUPT_STACK();
351 break;
352 case CT_UNALIGNED_LOAD_STORE_WRITE:
353 lkdtm_UNALIGNED_LOAD_STORE_WRITE();
354 break;
ffc514f3
KC
355 case CT_OVERWRITE_ALLOCATION:
356 lkdtm_OVERWRITE_ALLOCATION();
0347af4e 357 break;
ffc514f3
KC
358 case CT_WRITE_AFTER_FREE:
359 lkdtm_WRITE_AFTER_FREE();
0347af4e 360 break;
ffc514f3
KC
361 case CT_READ_AFTER_FREE:
362 lkdtm_READ_AFTER_FREE();
bc0b8cc6 363 break;
ffc514f3
KC
364 case CT_WRITE_BUDDY_AFTER_FREE:
365 lkdtm_WRITE_BUDDY_AFTER_FREE();
920d451f 366 break;
ffc514f3
KC
367 case CT_READ_BUDDY_AFTER_FREE:
368 lkdtm_READ_BUDDY_AFTER_FREE();
0347af4e 369 break;
93e2f585 370 case CT_SOFTLOCKUP:
00f496c4 371 lkdtm_SOFTLOCKUP();
a48223f9 372 break;
93e2f585 373 case CT_HARDLOCKUP:
00f496c4 374 lkdtm_HARDLOCKUP();
a48223f9 375 break;
274a5855 376 case CT_SPINLOCKUP:
00f496c4 377 lkdtm_SPINLOCKUP();
274a5855 378 break;
93e2f585 379 case CT_HUNG_TASK:
00f496c4 380 lkdtm_HUNG_TASK();
a48223f9 381 break;
cc33c537 382 case CT_EXEC_DATA:
0d9eb29b 383 lkdtm_EXEC_DATA();
cc33c537 384 break;
0d9eb29b
KC
385 case CT_EXEC_STACK:
386 lkdtm_EXEC_STACK();
cc33c537 387 break;
0d9eb29b
KC
388 case CT_EXEC_KMALLOC:
389 lkdtm_EXEC_KMALLOC();
cc33c537 390 break;
0d9eb29b
KC
391 case CT_EXEC_VMALLOC:
392 lkdtm_EXEC_VMALLOC();
cc33c537 393 break;
9a49a528 394 case CT_EXEC_RODATA:
0d9eb29b 395 lkdtm_EXEC_RODATA();
9a49a528 396 break;
0d9eb29b
KC
397 case CT_EXEC_USERSPACE:
398 lkdtm_EXEC_USERSPACE();
9ae113ce 399 break;
0d9eb29b
KC
400 case CT_ACCESS_USERSPACE:
401 lkdtm_ACCESS_USERSPACE();
9ae113ce 402 break;
0d9eb29b
KC
403 case CT_WRITE_RO:
404 lkdtm_WRITE_RO();
7cca071c 405 break;
0d9eb29b
KC
406 case CT_WRITE_RO_AFTER_INIT:
407 lkdtm_WRITE_RO_AFTER_INIT();
9ae113ce 408 break;
0d9eb29b
KC
409 case CT_WRITE_KERN:
410 lkdtm_WRITE_KERN();
dc2b9e90 411 break;
00f496c4
KC
412 case CT_ATOMIC_UNDERFLOW:
413 lkdtm_ATOMIC_UNDERFLOW();
414 break;
415 case CT_ATOMIC_OVERFLOW:
416 lkdtm_ATOMIC_OVERFLOW();
b5484527 417 break;
aa981a66 418 case CT_USERCOPY_HEAP_SIZE_TO:
a3dff71c 419 lkdtm_USERCOPY_HEAP_SIZE_TO();
aa981a66
KC
420 break;
421 case CT_USERCOPY_HEAP_SIZE_FROM:
a3dff71c 422 lkdtm_USERCOPY_HEAP_SIZE_FROM();
aa981a66
KC
423 break;
424 case CT_USERCOPY_HEAP_FLAG_TO:
a3dff71c 425 lkdtm_USERCOPY_HEAP_FLAG_TO();
aa981a66
KC
426 break;
427 case CT_USERCOPY_HEAP_FLAG_FROM:
a3dff71c 428 lkdtm_USERCOPY_HEAP_FLAG_FROM();
aa981a66
KC
429 break;
430 case CT_USERCOPY_STACK_FRAME_TO:
a3dff71c 431 lkdtm_USERCOPY_STACK_FRAME_TO();
aa981a66
KC
432 break;
433 case CT_USERCOPY_STACK_FRAME_FROM:
a3dff71c 434 lkdtm_USERCOPY_STACK_FRAME_FROM();
aa981a66
KC
435 break;
436 case CT_USERCOPY_STACK_BEYOND:
a3dff71c 437 lkdtm_USERCOPY_STACK_BEYOND();
aa981a66 438 break;
6c352140 439 case CT_USERCOPY_KERNEL:
a3dff71c 440 lkdtm_USERCOPY_KERNEL();
6c352140 441 break;
93e2f585 442 case CT_NONE:
0347af4e
SK
443 default:
444 break;
445 }
446
447}
448
449static void lkdtm_handler(void)
450{
aa2c96d6 451 unsigned long flags;
92618184 452 bool do_it = false;
aa2c96d6 453
76a10e2b
KC
454 spin_lock_irqsave(&crash_count_lock, flags);
455 crash_count--;
feac6e21 456 pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
38f95fe2
KC
457 cp_name_to_str(lkdtm_crashpoint),
458 cp_type_to_str(lkdtm_crashtype), crash_count);
8bb31b9d 459
76a10e2b 460 if (crash_count == 0) {
92618184 461 do_it = true;
76a10e2b 462 crash_count = cpoint_count;
8bb31b9d 463 }
76a10e2b 464 spin_unlock_irqrestore(&crash_count_lock, flags);
92618184
CW
465
466 if (do_it)
38f95fe2 467 lkdtm_do_action(lkdtm_crashtype);
8bb31b9d
AG
468}
469
0347af4e 470static int lkdtm_register_cpoint(enum cname which)
8bb31b9d
AG
471{
472 int ret;
473
38f95fe2
KC
474 lkdtm_crashpoint = CN_INVALID;
475 if (lkdtm_jprobe.entry != NULL)
476 unregister_jprobe(&lkdtm_jprobe);
8bb31b9d 477
0347af4e 478 switch (which) {
93e2f585 479 case CN_DIRECT:
38f95fe2 480 lkdtm_do_action(lkdtm_crashtype);
0347af4e 481 return 0;
93e2f585 482 case CN_INT_HARDWARE_ENTRY:
38f95fe2
KC
483 lkdtm_jprobe.kp.symbol_name = "do_IRQ";
484 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_do_irq;
8bb31b9d 485 break;
93e2f585 486 case CN_INT_HW_IRQ_EN:
38f95fe2
KC
487 lkdtm_jprobe.kp.symbol_name = "handle_IRQ_event";
488 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_handle_irq_event;
8bb31b9d 489 break;
93e2f585 490 case CN_INT_TASKLET_ENTRY:
38f95fe2
KC
491 lkdtm_jprobe.kp.symbol_name = "tasklet_action";
492 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_tasklet_action;
8bb31b9d 493 break;
93e2f585 494 case CN_FS_DEVRW:
38f95fe2
KC
495 lkdtm_jprobe.kp.symbol_name = "ll_rw_block";
496 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_ll_rw_block;
8bb31b9d 497 break;
93e2f585 498 case CN_MEM_SWAPOUT:
38f95fe2
KC
499 lkdtm_jprobe.kp.symbol_name = "shrink_inactive_list";
500 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_shrink_inactive_list;
8bb31b9d 501 break;
93e2f585 502 case CN_TIMERADD:
38f95fe2
KC
503 lkdtm_jprobe.kp.symbol_name = "hrtimer_start";
504 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_hrtimer_start;
8bb31b9d 505 break;
93e2f585 506 case CN_SCSI_DISPATCH_CMD:
38f95fe2
KC
507 lkdtm_jprobe.kp.symbol_name = "scsi_dispatch_cmd";
508 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd;
8bb31b9d 509 break;
93e2f585 510 case CN_IDE_CORE_CP:
8bb31b9d 511#ifdef CONFIG_IDE
38f95fe2
KC
512 lkdtm_jprobe.kp.symbol_name = "generic_ide_ioctl";
513 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl;
8bb31b9d 514#else
feac6e21 515 pr_info("Crash point not available\n");
0347af4e 516 return -EINVAL;
8bb31b9d
AG
517#endif
518 break;
519 default:
feac6e21 520 pr_info("Invalid Crash Point\n");
0347af4e 521 return -EINVAL;
8bb31b9d
AG
522 }
523
38f95fe2
KC
524 lkdtm_crashpoint = which;
525 if ((ret = register_jprobe(&lkdtm_jprobe)) < 0) {
feac6e21 526 pr_info("Couldn't register jprobe\n");
38f95fe2 527 lkdtm_crashpoint = CN_INVALID;
0347af4e
SK
528 }
529
530 return ret;
531}
532
533static ssize_t do_register_entry(enum cname which, struct file *f,
534 const char __user *user_buf, size_t count, loff_t *off)
535{
536 char *buf;
537 int err;
538
539 if (count >= PAGE_SIZE)
540 return -EINVAL;
541
542 buf = (char *)__get_free_page(GFP_KERNEL);
543 if (!buf)
544 return -ENOMEM;
545 if (copy_from_user(buf, user_buf, count)) {
546 free_page((unsigned long) buf);
547 return -EFAULT;
548 }
549 /* NULL-terminate and remove enter */
550 buf[count] = '\0';
551 strim(buf);
552
38f95fe2 553 lkdtm_crashtype = parse_cp_type(buf, count);
0347af4e
SK
554 free_page((unsigned long) buf);
555
38f95fe2 556 if (lkdtm_crashtype == CT_NONE)
0347af4e
SK
557 return -EINVAL;
558
559 err = lkdtm_register_cpoint(which);
560 if (err < 0)
561 return err;
562
563 *off += count;
564
565 return count;
566}
567
568/* Generic read callback that just prints out the available crash types */
569static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
570 size_t count, loff_t *off)
571{
572 char *buf;
573 int i, n, out;
574
575 buf = (char *)__get_free_page(GFP_KERNEL);
086ff4b3
AC
576 if (buf == NULL)
577 return -ENOMEM;
0347af4e
SK
578
579 n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
580 for (i = 0; i < ARRAY_SIZE(cp_type); i++)
581 n += snprintf(buf + n, PAGE_SIZE - n, "%s\n", cp_type[i]);
582 buf[n] = '\0';
583
584 out = simple_read_from_buffer(user_buf, count, off,
585 buf, n);
586 free_page((unsigned long) buf);
587
588 return out;
589}
590
591static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
592{
593 return 0;
594}
595
596
597static ssize_t int_hardware_entry(struct file *f, const char __user *buf,
598 size_t count, loff_t *off)
599{
93e2f585 600 return do_register_entry(CN_INT_HARDWARE_ENTRY, f, buf, count, off);
0347af4e
SK
601}
602
603static ssize_t int_hw_irq_en(struct file *f, const char __user *buf,
604 size_t count, loff_t *off)
605{
93e2f585 606 return do_register_entry(CN_INT_HW_IRQ_EN, f, buf, count, off);
0347af4e
SK
607}
608
609static ssize_t int_tasklet_entry(struct file *f, const char __user *buf,
610 size_t count, loff_t *off)
611{
93e2f585 612 return do_register_entry(CN_INT_TASKLET_ENTRY, f, buf, count, off);
0347af4e
SK
613}
614
615static ssize_t fs_devrw_entry(struct file *f, const char __user *buf,
616 size_t count, loff_t *off)
617{
93e2f585 618 return do_register_entry(CN_FS_DEVRW, f, buf, count, off);
0347af4e
SK
619}
620
621static ssize_t mem_swapout_entry(struct file *f, const char __user *buf,
622 size_t count, loff_t *off)
623{
93e2f585 624 return do_register_entry(CN_MEM_SWAPOUT, f, buf, count, off);
0347af4e
SK
625}
626
627static ssize_t timeradd_entry(struct file *f, const char __user *buf,
628 size_t count, loff_t *off)
629{
93e2f585 630 return do_register_entry(CN_TIMERADD, f, buf, count, off);
0347af4e
SK
631}
632
633static ssize_t scsi_dispatch_cmd_entry(struct file *f,
634 const char __user *buf, size_t count, loff_t *off)
635{
93e2f585 636 return do_register_entry(CN_SCSI_DISPATCH_CMD, f, buf, count, off);
0347af4e
SK
637}
638
639static ssize_t ide_core_cp_entry(struct file *f, const char __user *buf,
640 size_t count, loff_t *off)
641{
93e2f585 642 return do_register_entry(CN_IDE_CORE_CP, f, buf, count, off);
0347af4e
SK
643}
644
645/* Special entry to just crash directly. Available without KPROBEs */
646static ssize_t direct_entry(struct file *f, const char __user *user_buf,
647 size_t count, loff_t *off)
648{
649 enum ctype type;
650 char *buf;
651
652 if (count >= PAGE_SIZE)
653 return -EINVAL;
654 if (count < 1)
655 return -EINVAL;
656
657 buf = (char *)__get_free_page(GFP_KERNEL);
658 if (!buf)
659 return -ENOMEM;
660 if (copy_from_user(buf, user_buf, count)) {
661 free_page((unsigned long) buf);
662 return -EFAULT;
663 }
664 /* NULL-terminate and remove enter */
665 buf[count] = '\0';
666 strim(buf);
667
668 type = parse_cp_type(buf, count);
669 free_page((unsigned long) buf);
93e2f585 670 if (type == CT_NONE)
0347af4e
SK
671 return -EINVAL;
672
feac6e21 673 pr_info("Performing direct entry %s\n", cp_type_to_str(type));
0347af4e
SK
674 lkdtm_do_action(type);
675 *off += count;
676
677 return count;
678}
679
680struct crash_entry {
681 const char *name;
682 const struct file_operations fops;
683};
684
685static const struct crash_entry crash_entries[] = {
686 {"DIRECT", {.read = lkdtm_debugfs_read,
05271ec4 687 .llseek = generic_file_llseek,
0347af4e
SK
688 .open = lkdtm_debugfs_open,
689 .write = direct_entry} },
690 {"INT_HARDWARE_ENTRY", {.read = lkdtm_debugfs_read,
05271ec4 691 .llseek = generic_file_llseek,
0347af4e
SK
692 .open = lkdtm_debugfs_open,
693 .write = int_hardware_entry} },
694 {"INT_HW_IRQ_EN", {.read = lkdtm_debugfs_read,
05271ec4 695 .llseek = generic_file_llseek,
0347af4e
SK
696 .open = lkdtm_debugfs_open,
697 .write = int_hw_irq_en} },
698 {"INT_TASKLET_ENTRY", {.read = lkdtm_debugfs_read,
05271ec4 699 .llseek = generic_file_llseek,
0347af4e
SK
700 .open = lkdtm_debugfs_open,
701 .write = int_tasklet_entry} },
702 {"FS_DEVRW", {.read = lkdtm_debugfs_read,
05271ec4 703 .llseek = generic_file_llseek,
0347af4e
SK
704 .open = lkdtm_debugfs_open,
705 .write = fs_devrw_entry} },
706 {"MEM_SWAPOUT", {.read = lkdtm_debugfs_read,
05271ec4 707 .llseek = generic_file_llseek,
0347af4e
SK
708 .open = lkdtm_debugfs_open,
709 .write = mem_swapout_entry} },
710 {"TIMERADD", {.read = lkdtm_debugfs_read,
05271ec4 711 .llseek = generic_file_llseek,
0347af4e
SK
712 .open = lkdtm_debugfs_open,
713 .write = timeradd_entry} },
714 {"SCSI_DISPATCH_CMD", {.read = lkdtm_debugfs_read,
05271ec4 715 .llseek = generic_file_llseek,
0347af4e
SK
716 .open = lkdtm_debugfs_open,
717 .write = scsi_dispatch_cmd_entry} },
718 {"IDE_CORE_CP", {.read = lkdtm_debugfs_read,
05271ec4 719 .llseek = generic_file_llseek,
0347af4e
SK
720 .open = lkdtm_debugfs_open,
721 .write = ide_core_cp_entry} },
722};
723
724static struct dentry *lkdtm_debugfs_root;
725
726static int __init lkdtm_module_init(void)
727{
728 int ret = -EINVAL;
729 int n_debugfs_entries = 1; /* Assume only the direct entry */
730 int i;
731
a3dff71c 732 /* Handle test-specific initialization. */
00f496c4 733 lkdtm_bugs_init(&recur_count);
0d9eb29b 734 lkdtm_perms_init();
a3dff71c
KC
735 lkdtm_usercopy_init();
736
0347af4e
SK
737 /* Register debugfs interface */
738 lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
739 if (!lkdtm_debugfs_root) {
feac6e21 740 pr_err("creating root dir failed\n");
0347af4e
SK
741 return -ENODEV;
742 }
743
744#ifdef CONFIG_KPROBES
745 n_debugfs_entries = ARRAY_SIZE(crash_entries);
746#endif
747
748 for (i = 0; i < n_debugfs_entries; i++) {
749 const struct crash_entry *cur = &crash_entries[i];
750 struct dentry *de;
751
752 de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
753 NULL, &cur->fops);
754 if (de == NULL) {
feac6e21 755 pr_err("could not create %s\n", cur->name);
0347af4e
SK
756 goto out_err;
757 }
758 }
759
760 if (lkdtm_parse_commandline() == -EINVAL) {
feac6e21 761 pr_info("Invalid command\n");
0347af4e
SK
762 goto out_err;
763 }
764
38f95fe2
KC
765 if (lkdtm_crashpoint != CN_INVALID && lkdtm_crashtype != CT_NONE) {
766 ret = lkdtm_register_cpoint(lkdtm_crashpoint);
0347af4e 767 if (ret < 0) {
38f95fe2 768 pr_info("Invalid crash point %d\n", lkdtm_crashpoint);
0347af4e
SK
769 goto out_err;
770 }
feac6e21
KC
771 pr_info("Crash point %s of type %s registered\n",
772 cpoint_name, cpoint_type);
0347af4e 773 } else {
feac6e21 774 pr_info("No crash points registered, enable through debugfs\n");
8bb31b9d
AG
775 }
776
8bb31b9d 777 return 0;
0347af4e
SK
778
779out_err:
780 debugfs_remove_recursive(lkdtm_debugfs_root);
781 return ret;
8bb31b9d
AG
782}
783
2118116e 784static void __exit lkdtm_module_exit(void)
8bb31b9d 785{
0347af4e
SK
786 debugfs_remove_recursive(lkdtm_debugfs_root);
787
a3dff71c
KC
788 /* Handle test-specific clean-up. */
789 lkdtm_usercopy_exit();
aa981a66 790
38f95fe2 791 unregister_jprobe(&lkdtm_jprobe);
feac6e21 792 pr_info("Crash point unregistered\n");
8bb31b9d
AG
793}
794
795module_init(lkdtm_module_init);
796module_exit(lkdtm_module_exit);
797
798MODULE_LICENSE("GPL");
da86920f 799MODULE_DESCRIPTION("Kprobe module for testing crash dumps");
This page took 0.774643 seconds and 5 git commands to generate.