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
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.
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.
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.
22 * Copyright (C) IBM Corporation, 2006
24 * Author: Ankita Garg <ankita@in.ibm.com>
26 * It is adapted from the Linux Kernel Dump Test Tool by
27 * Fernando Luis Vazquez Cao <http://lkdtt.sourceforge.net>
29 * Debugfs support added by Simon Kagstrom <simon.kagstrom@netinsight.net>
31 * See Documentation/fault-injection/provoke-crashes.txt for instructions
33 #define pr_fmt(fmt) "lkdtm: " fmt
35 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/buffer_head.h>
39 #include <linux/kprobes.h>
40 #include <linux/list.h>
41 #include <linux/init.h>
42 #include <linux/interrupt.h>
43 #include <linux/hrtimer.h>
44 #include <linux/slab.h>
45 #include <scsi/scsi_cmnd.h>
46 #include <linux/debugfs.h>
49 #include <linux/ide.h>
56 CN_INT_HARDWARE_ENTRY,
76 CT_UNALIGNED_LOAD_STORE_WRITE,
77 CT_OVERWRITE_ALLOCATION,
80 CT_WRITE_BUDDY_AFTER_FREE,
81 CT_READ_BUDDY_AFTER_FREE,
94 CT_WRITE_RO_AFTER_INIT,
98 CT_USERCOPY_HEAP_SIZE_TO,
99 CT_USERCOPY_HEAP_SIZE_FROM,
100 CT_USERCOPY_HEAP_FLAG_TO,
101 CT_USERCOPY_HEAP_FLAG_FROM,
102 CT_USERCOPY_STACK_FRAME_TO,
103 CT_USERCOPY_STACK_FRAME_FROM,
104 CT_USERCOPY_STACK_BEYOND,
108 static char* cp_name[] = {
110 "INT_HARDWARE_ENTRY",
121 static char* cp_type[] = {
130 "UNALIGNED_LOAD_STORE_WRITE",
131 "OVERWRITE_ALLOCATION",
134 "WRITE_BUDDY_AFTER_FREE",
135 "READ_BUDDY_AFTER_FREE",
148 "WRITE_RO_AFTER_INIT",
152 "USERCOPY_HEAP_SIZE_TO",
153 "USERCOPY_HEAP_SIZE_FROM",
154 "USERCOPY_HEAP_FLAG_TO",
155 "USERCOPY_HEAP_FLAG_FROM",
156 "USERCOPY_STACK_FRAME_TO",
157 "USERCOPY_STACK_FRAME_FROM",
158 "USERCOPY_STACK_BEYOND",
162 static struct jprobe lkdtm_jprobe;
164 static int lkdtm_parse_commandline(void);
165 static void lkdtm_handler(void);
167 #define DEFAULT_COUNT 10
168 static char* cpoint_name;
169 static char* cpoint_type;
170 static int cpoint_count = DEFAULT_COUNT;
171 static int recur_count = -1;
172 static int crash_count = DEFAULT_COUNT;
173 static DEFINE_SPINLOCK(crash_count_lock);
175 static enum cname lkdtm_crashpoint = CN_INVALID;
176 static enum ctype lkdtm_crashtype = CT_NONE;
178 module_param(recur_count, int, 0644);
179 MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
180 module_param(cpoint_name, charp, 0444);
181 MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
182 module_param(cpoint_type, charp, 0444);
183 MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
184 "hitting the crash point");
185 module_param(cpoint_count, int, 0644);
186 MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
187 "crash point is to be hit to trigger action");
189 static unsigned int jp_do_irq(unsigned int irq)
196 static irqreturn_t jp_handle_irq_event(unsigned int irq,
197 struct irqaction *action)
204 static void jp_tasklet_action(struct softirq_action *a)
210 static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
218 static unsigned long jp_shrink_inactive_list(unsigned long max_scan,
220 struct scan_control *sc)
227 static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
228 const enum hrtimer_mode mode)
235 static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
243 static int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
244 struct block_device *bdev, unsigned int cmd,
253 /* Return the crashpoint number or NONE if the name is invalid */
254 static enum ctype parse_cp_type(const char *what, size_t count)
258 for (i = 0; i < ARRAY_SIZE(cp_type); i++) {
259 if (!strcmp(what, cp_type[i]))
266 static const char *cp_type_to_str(enum ctype type)
268 if (type == CT_NONE || type < 0 || type > ARRAY_SIZE(cp_type))
271 return cp_type[type];
274 static const char *cp_name_to_str(enum cname name)
276 if (name == CN_INVALID || name < 0 || name > ARRAY_SIZE(cp_name))
279 return cp_name[name];
283 static int lkdtm_parse_commandline(void)
288 if (cpoint_count < 1 || recur_count < 1)
291 spin_lock_irqsave(&crash_count_lock, flags);
292 crash_count = cpoint_count;
293 spin_unlock_irqrestore(&crash_count_lock, flags);
295 /* No special parameters */
296 if (!cpoint_type && !cpoint_name)
299 /* Neither or both of these need to be set */
300 if (!cpoint_type || !cpoint_name)
303 lkdtm_crashtype = parse_cp_type(cpoint_type, strlen(cpoint_type));
304 if (lkdtm_crashtype == CT_NONE)
307 /* Refuse INVALID as a selectable crashpoint name. */
308 if (!strcmp(cpoint_name, "INVALID"))
311 for (i = 0; i < ARRAY_SIZE(cp_name); i++) {
312 if (!strcmp(cpoint_name, cp_name[i])) {
313 lkdtm_crashpoint = i;
318 /* Could not find a valid crash point */
322 static void lkdtm_do_action(enum ctype which)
343 case CT_CORRUPT_STACK:
344 lkdtm_CORRUPT_STACK();
346 case CT_UNALIGNED_LOAD_STORE_WRITE:
347 lkdtm_UNALIGNED_LOAD_STORE_WRITE();
349 case CT_OVERWRITE_ALLOCATION:
350 lkdtm_OVERWRITE_ALLOCATION();
352 case CT_WRITE_AFTER_FREE:
353 lkdtm_WRITE_AFTER_FREE();
355 case CT_READ_AFTER_FREE:
356 lkdtm_READ_AFTER_FREE();
358 case CT_WRITE_BUDDY_AFTER_FREE:
359 lkdtm_WRITE_BUDDY_AFTER_FREE();
361 case CT_READ_BUDDY_AFTER_FREE:
362 lkdtm_READ_BUDDY_AFTER_FREE();
382 case CT_EXEC_KMALLOC:
383 lkdtm_EXEC_KMALLOC();
385 case CT_EXEC_VMALLOC:
386 lkdtm_EXEC_VMALLOC();
391 case CT_EXEC_USERSPACE:
392 lkdtm_EXEC_USERSPACE();
394 case CT_ACCESS_USERSPACE:
395 lkdtm_ACCESS_USERSPACE();
400 case CT_WRITE_RO_AFTER_INIT:
401 lkdtm_WRITE_RO_AFTER_INIT();
406 case CT_ATOMIC_UNDERFLOW:
407 lkdtm_ATOMIC_UNDERFLOW();
409 case CT_ATOMIC_OVERFLOW:
410 lkdtm_ATOMIC_OVERFLOW();
412 case CT_USERCOPY_HEAP_SIZE_TO:
413 lkdtm_USERCOPY_HEAP_SIZE_TO();
415 case CT_USERCOPY_HEAP_SIZE_FROM:
416 lkdtm_USERCOPY_HEAP_SIZE_FROM();
418 case CT_USERCOPY_HEAP_FLAG_TO:
419 lkdtm_USERCOPY_HEAP_FLAG_TO();
421 case CT_USERCOPY_HEAP_FLAG_FROM:
422 lkdtm_USERCOPY_HEAP_FLAG_FROM();
424 case CT_USERCOPY_STACK_FRAME_TO:
425 lkdtm_USERCOPY_STACK_FRAME_TO();
427 case CT_USERCOPY_STACK_FRAME_FROM:
428 lkdtm_USERCOPY_STACK_FRAME_FROM();
430 case CT_USERCOPY_STACK_BEYOND:
431 lkdtm_USERCOPY_STACK_BEYOND();
433 case CT_USERCOPY_KERNEL:
434 lkdtm_USERCOPY_KERNEL();
443 static void lkdtm_handler(void)
448 spin_lock_irqsave(&crash_count_lock, flags);
450 pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
451 cp_name_to_str(lkdtm_crashpoint),
452 cp_type_to_str(lkdtm_crashtype), crash_count);
454 if (crash_count == 0) {
456 crash_count = cpoint_count;
458 spin_unlock_irqrestore(&crash_count_lock, flags);
461 lkdtm_do_action(lkdtm_crashtype);
464 static int lkdtm_register_cpoint(enum cname which)
468 lkdtm_crashpoint = CN_INVALID;
469 if (lkdtm_jprobe.entry != NULL)
470 unregister_jprobe(&lkdtm_jprobe);
474 lkdtm_do_action(lkdtm_crashtype);
476 case CN_INT_HARDWARE_ENTRY:
477 lkdtm_jprobe.kp.symbol_name = "do_IRQ";
478 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_do_irq;
480 case CN_INT_HW_IRQ_EN:
481 lkdtm_jprobe.kp.symbol_name = "handle_IRQ_event";
482 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_handle_irq_event;
484 case CN_INT_TASKLET_ENTRY:
485 lkdtm_jprobe.kp.symbol_name = "tasklet_action";
486 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_tasklet_action;
489 lkdtm_jprobe.kp.symbol_name = "ll_rw_block";
490 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_ll_rw_block;
493 lkdtm_jprobe.kp.symbol_name = "shrink_inactive_list";
494 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_shrink_inactive_list;
497 lkdtm_jprobe.kp.symbol_name = "hrtimer_start";
498 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_hrtimer_start;
500 case CN_SCSI_DISPATCH_CMD:
501 lkdtm_jprobe.kp.symbol_name = "scsi_dispatch_cmd";
502 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd;
506 lkdtm_jprobe.kp.symbol_name = "generic_ide_ioctl";
507 lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl;
509 pr_info("Crash point not available\n");
514 pr_info("Invalid Crash Point\n");
518 lkdtm_crashpoint = which;
519 if ((ret = register_jprobe(&lkdtm_jprobe)) < 0) {
520 pr_info("Couldn't register jprobe\n");
521 lkdtm_crashpoint = CN_INVALID;
527 static ssize_t do_register_entry(enum cname which, struct file *f,
528 const char __user *user_buf, size_t count, loff_t *off)
533 if (count >= PAGE_SIZE)
536 buf = (char *)__get_free_page(GFP_KERNEL);
539 if (copy_from_user(buf, user_buf, count)) {
540 free_page((unsigned long) buf);
543 /* NULL-terminate and remove enter */
547 lkdtm_crashtype = parse_cp_type(buf, count);
548 free_page((unsigned long) buf);
550 if (lkdtm_crashtype == CT_NONE)
553 err = lkdtm_register_cpoint(which);
562 /* Generic read callback that just prints out the available crash types */
563 static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
564 size_t count, loff_t *off)
569 buf = (char *)__get_free_page(GFP_KERNEL);
573 n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
574 for (i = 0; i < ARRAY_SIZE(cp_type); i++)
575 n += snprintf(buf + n, PAGE_SIZE - n, "%s\n", cp_type[i]);
578 out = simple_read_from_buffer(user_buf, count, off,
580 free_page((unsigned long) buf);
585 static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
591 static ssize_t int_hardware_entry(struct file *f, const char __user *buf,
592 size_t count, loff_t *off)
594 return do_register_entry(CN_INT_HARDWARE_ENTRY, f, buf, count, off);
597 static ssize_t int_hw_irq_en(struct file *f, const char __user *buf,
598 size_t count, loff_t *off)
600 return do_register_entry(CN_INT_HW_IRQ_EN, f, buf, count, off);
603 static ssize_t int_tasklet_entry(struct file *f, const char __user *buf,
604 size_t count, loff_t *off)
606 return do_register_entry(CN_INT_TASKLET_ENTRY, f, buf, count, off);
609 static ssize_t fs_devrw_entry(struct file *f, const char __user *buf,
610 size_t count, loff_t *off)
612 return do_register_entry(CN_FS_DEVRW, f, buf, count, off);
615 static ssize_t mem_swapout_entry(struct file *f, const char __user *buf,
616 size_t count, loff_t *off)
618 return do_register_entry(CN_MEM_SWAPOUT, f, buf, count, off);
621 static ssize_t timeradd_entry(struct file *f, const char __user *buf,
622 size_t count, loff_t *off)
624 return do_register_entry(CN_TIMERADD, f, buf, count, off);
627 static ssize_t scsi_dispatch_cmd_entry(struct file *f,
628 const char __user *buf, size_t count, loff_t *off)
630 return do_register_entry(CN_SCSI_DISPATCH_CMD, f, buf, count, off);
633 static ssize_t ide_core_cp_entry(struct file *f, const char __user *buf,
634 size_t count, loff_t *off)
636 return do_register_entry(CN_IDE_CORE_CP, f, buf, count, off);
639 /* Special entry to just crash directly. Available without KPROBEs */
640 static ssize_t direct_entry(struct file *f, const char __user *user_buf,
641 size_t count, loff_t *off)
646 if (count >= PAGE_SIZE)
651 buf = (char *)__get_free_page(GFP_KERNEL);
654 if (copy_from_user(buf, user_buf, count)) {
655 free_page((unsigned long) buf);
658 /* NULL-terminate and remove enter */
662 type = parse_cp_type(buf, count);
663 free_page((unsigned long) buf);
667 pr_info("Performing direct entry %s\n", cp_type_to_str(type));
668 lkdtm_do_action(type);
676 const struct file_operations fops;
679 static const struct crash_entry crash_entries[] = {
680 {"DIRECT", {.read = lkdtm_debugfs_read,
681 .llseek = generic_file_llseek,
682 .open = lkdtm_debugfs_open,
683 .write = direct_entry} },
684 {"INT_HARDWARE_ENTRY", {.read = lkdtm_debugfs_read,
685 .llseek = generic_file_llseek,
686 .open = lkdtm_debugfs_open,
687 .write = int_hardware_entry} },
688 {"INT_HW_IRQ_EN", {.read = lkdtm_debugfs_read,
689 .llseek = generic_file_llseek,
690 .open = lkdtm_debugfs_open,
691 .write = int_hw_irq_en} },
692 {"INT_TASKLET_ENTRY", {.read = lkdtm_debugfs_read,
693 .llseek = generic_file_llseek,
694 .open = lkdtm_debugfs_open,
695 .write = int_tasklet_entry} },
696 {"FS_DEVRW", {.read = lkdtm_debugfs_read,
697 .llseek = generic_file_llseek,
698 .open = lkdtm_debugfs_open,
699 .write = fs_devrw_entry} },
700 {"MEM_SWAPOUT", {.read = lkdtm_debugfs_read,
701 .llseek = generic_file_llseek,
702 .open = lkdtm_debugfs_open,
703 .write = mem_swapout_entry} },
704 {"TIMERADD", {.read = lkdtm_debugfs_read,
705 .llseek = generic_file_llseek,
706 .open = lkdtm_debugfs_open,
707 .write = timeradd_entry} },
708 {"SCSI_DISPATCH_CMD", {.read = lkdtm_debugfs_read,
709 .llseek = generic_file_llseek,
710 .open = lkdtm_debugfs_open,
711 .write = scsi_dispatch_cmd_entry} },
712 {"IDE_CORE_CP", {.read = lkdtm_debugfs_read,
713 .llseek = generic_file_llseek,
714 .open = lkdtm_debugfs_open,
715 .write = ide_core_cp_entry} },
718 static struct dentry *lkdtm_debugfs_root;
720 static int __init lkdtm_module_init(void)
723 int n_debugfs_entries = 1; /* Assume only the direct entry */
726 /* Handle test-specific initialization. */
727 lkdtm_bugs_init(&recur_count);
729 lkdtm_usercopy_init();
731 /* Register debugfs interface */
732 lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
733 if (!lkdtm_debugfs_root) {
734 pr_err("creating root dir failed\n");
738 #ifdef CONFIG_KPROBES
739 n_debugfs_entries = ARRAY_SIZE(crash_entries);
742 for (i = 0; i < n_debugfs_entries; i++) {
743 const struct crash_entry *cur = &crash_entries[i];
746 de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
749 pr_err("could not create %s\n", cur->name);
754 if (lkdtm_parse_commandline() == -EINVAL) {
755 pr_info("Invalid command\n");
759 if (lkdtm_crashpoint != CN_INVALID && lkdtm_crashtype != CT_NONE) {
760 ret = lkdtm_register_cpoint(lkdtm_crashpoint);
762 pr_info("Invalid crash point %d\n", lkdtm_crashpoint);
765 pr_info("Crash point %s of type %s registered\n",
766 cpoint_name, cpoint_type);
768 pr_info("No crash points registered, enable through debugfs\n");
774 debugfs_remove_recursive(lkdtm_debugfs_root);
778 static void __exit lkdtm_module_exit(void)
780 debugfs_remove_recursive(lkdtm_debugfs_root);
782 /* Handle test-specific clean-up. */
783 lkdtm_usercopy_exit();
785 unregister_jprobe(&lkdtm_jprobe);
786 pr_info("Crash point unregistered\n");
789 module_init(lkdtm_module_init);
790 module_exit(lkdtm_module_exit);
792 MODULE_LICENSE("GPL");
793 MODULE_DESCRIPTION("Kprobe module for testing crash dumps");