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>
54 #define DEFAULT_COUNT 10
56 static int count = DEFAULT_COUNT;
57 static DEFINE_SPINLOCK(count_lock);
61 CN_INT_HARDWARE_ENTRY,
81 CT_UNALIGNED_LOAD_STORE_WRITE,
82 CT_OVERWRITE_ALLOCATION,
85 CT_WRITE_BUDDY_AFTER_FREE,
86 CT_READ_BUDDY_AFTER_FREE,
99 CT_WRITE_RO_AFTER_INIT,
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,
113 static char* cp_name[] = {
115 "INT_HARDWARE_ENTRY",
126 static char* cp_type[] = {
135 "UNALIGNED_LOAD_STORE_WRITE",
136 "OVERWRITE_ALLOCATION",
139 "WRITE_BUDDY_AFTER_FREE",
140 "READ_BUDDY_AFTER_FREE",
153 "WRITE_RO_AFTER_INIT",
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",
167 static struct jprobe lkdtm;
169 static int lkdtm_parse_commandline(void);
170 static void lkdtm_handler(void);
172 static char* cpoint_name;
173 static char* cpoint_type;
174 static int cpoint_count = DEFAULT_COUNT;
175 static int recur_count = -1;
177 static enum cname cpoint = CN_INVALID;
178 static enum ctype cptype = CT_NONE;
180 module_param(recur_count, int, 0644);
181 MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
182 module_param(cpoint_name, charp, 0444);
183 MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
184 module_param(cpoint_type, charp, 0444);
185 MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
186 "hitting the crash point");
187 module_param(cpoint_count, int, 0644);
188 MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
189 "crash point is to be hit to trigger action");
191 static unsigned int jp_do_irq(unsigned int irq)
198 static irqreturn_t jp_handle_irq_event(unsigned int irq,
199 struct irqaction *action)
206 static void jp_tasklet_action(struct softirq_action *a)
212 static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
220 static unsigned long jp_shrink_inactive_list(unsigned long max_scan,
222 struct scan_control *sc)
229 static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
230 const enum hrtimer_mode mode)
237 static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
245 static int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
246 struct block_device *bdev, unsigned int cmd,
255 /* Return the crashpoint number or NONE if the name is invalid */
256 static enum ctype parse_cp_type(const char *what, size_t count)
260 for (i = 0; i < ARRAY_SIZE(cp_type); i++) {
261 if (!strcmp(what, cp_type[i]))
268 static const char *cp_type_to_str(enum ctype type)
270 if (type == CT_NONE || type < 0 || type > ARRAY_SIZE(cp_type))
273 return cp_type[type];
276 static const char *cp_name_to_str(enum cname name)
278 if (name == CN_INVALID || name < 0 || name > ARRAY_SIZE(cp_name))
281 return cp_name[name];
285 static int lkdtm_parse_commandline(void)
290 if (cpoint_count < 1 || recur_count < 1)
293 spin_lock_irqsave(&count_lock, flags);
294 count = cpoint_count;
295 spin_unlock_irqrestore(&count_lock, flags);
297 /* No special parameters */
298 if (!cpoint_type && !cpoint_name)
301 /* Neither or both of these need to be set */
302 if (!cpoint_type || !cpoint_name)
305 cptype = parse_cp_type(cpoint_type, strlen(cpoint_type));
306 if (cptype == CT_NONE)
309 /* Refuse INVALID as a selectable crashpoint name. */
310 if (!strcmp(cpoint_name, "INVALID"))
313 for (i = 0; i < ARRAY_SIZE(cp_name); i++) {
314 if (!strcmp(cpoint_name, cp_name[i])) {
320 /* Could not find a valid crash point */
324 static void lkdtm_do_action(enum ctype which)
345 case CT_CORRUPT_STACK:
346 lkdtm_CORRUPT_STACK();
348 case CT_UNALIGNED_LOAD_STORE_WRITE:
349 lkdtm_UNALIGNED_LOAD_STORE_WRITE();
351 case CT_OVERWRITE_ALLOCATION:
352 lkdtm_OVERWRITE_ALLOCATION();
354 case CT_WRITE_AFTER_FREE:
355 lkdtm_WRITE_AFTER_FREE();
357 case CT_READ_AFTER_FREE:
358 lkdtm_READ_AFTER_FREE();
360 case CT_WRITE_BUDDY_AFTER_FREE:
361 lkdtm_WRITE_BUDDY_AFTER_FREE();
363 case CT_READ_BUDDY_AFTER_FREE:
364 lkdtm_READ_BUDDY_AFTER_FREE();
384 case CT_EXEC_KMALLOC:
385 lkdtm_EXEC_KMALLOC();
387 case CT_EXEC_VMALLOC:
388 lkdtm_EXEC_VMALLOC();
393 case CT_EXEC_USERSPACE:
394 lkdtm_EXEC_USERSPACE();
396 case CT_ACCESS_USERSPACE:
397 lkdtm_ACCESS_USERSPACE();
402 case CT_WRITE_RO_AFTER_INIT:
403 lkdtm_WRITE_RO_AFTER_INIT();
408 case CT_ATOMIC_UNDERFLOW:
409 lkdtm_ATOMIC_UNDERFLOW();
411 case CT_ATOMIC_OVERFLOW:
412 lkdtm_ATOMIC_OVERFLOW();
414 case CT_USERCOPY_HEAP_SIZE_TO:
415 lkdtm_USERCOPY_HEAP_SIZE_TO();
417 case CT_USERCOPY_HEAP_SIZE_FROM:
418 lkdtm_USERCOPY_HEAP_SIZE_FROM();
420 case CT_USERCOPY_HEAP_FLAG_TO:
421 lkdtm_USERCOPY_HEAP_FLAG_TO();
423 case CT_USERCOPY_HEAP_FLAG_FROM:
424 lkdtm_USERCOPY_HEAP_FLAG_FROM();
426 case CT_USERCOPY_STACK_FRAME_TO:
427 lkdtm_USERCOPY_STACK_FRAME_TO();
429 case CT_USERCOPY_STACK_FRAME_FROM:
430 lkdtm_USERCOPY_STACK_FRAME_FROM();
432 case CT_USERCOPY_STACK_BEYOND:
433 lkdtm_USERCOPY_STACK_BEYOND();
435 case CT_USERCOPY_KERNEL:
436 lkdtm_USERCOPY_KERNEL();
445 static void lkdtm_handler(void)
450 spin_lock_irqsave(&count_lock, flags);
452 pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
453 cp_name_to_str(cpoint), cp_type_to_str(cptype), count);
457 count = cpoint_count;
459 spin_unlock_irqrestore(&count_lock, flags);
462 lkdtm_do_action(cptype);
465 static int lkdtm_register_cpoint(enum cname which)
470 if (lkdtm.entry != NULL)
471 unregister_jprobe(&lkdtm);
475 lkdtm_do_action(cptype);
477 case CN_INT_HARDWARE_ENTRY:
478 lkdtm.kp.symbol_name = "do_IRQ";
479 lkdtm.entry = (kprobe_opcode_t*) jp_do_irq;
481 case CN_INT_HW_IRQ_EN:
482 lkdtm.kp.symbol_name = "handle_IRQ_event";
483 lkdtm.entry = (kprobe_opcode_t*) jp_handle_irq_event;
485 case CN_INT_TASKLET_ENTRY:
486 lkdtm.kp.symbol_name = "tasklet_action";
487 lkdtm.entry = (kprobe_opcode_t*) jp_tasklet_action;
490 lkdtm.kp.symbol_name = "ll_rw_block";
491 lkdtm.entry = (kprobe_opcode_t*) jp_ll_rw_block;
494 lkdtm.kp.symbol_name = "shrink_inactive_list";
495 lkdtm.entry = (kprobe_opcode_t*) jp_shrink_inactive_list;
498 lkdtm.kp.symbol_name = "hrtimer_start";
499 lkdtm.entry = (kprobe_opcode_t*) jp_hrtimer_start;
501 case CN_SCSI_DISPATCH_CMD:
502 lkdtm.kp.symbol_name = "scsi_dispatch_cmd";
503 lkdtm.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd;
507 lkdtm.kp.symbol_name = "generic_ide_ioctl";
508 lkdtm.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl;
510 pr_info("Crash point not available\n");
515 pr_info("Invalid Crash Point\n");
520 if ((ret = register_jprobe(&lkdtm)) < 0) {
521 pr_info("Couldn't register jprobe\n");
528 static ssize_t do_register_entry(enum cname which, struct file *f,
529 const char __user *user_buf, size_t count, loff_t *off)
534 if (count >= PAGE_SIZE)
537 buf = (char *)__get_free_page(GFP_KERNEL);
540 if (copy_from_user(buf, user_buf, count)) {
541 free_page((unsigned long) buf);
544 /* NULL-terminate and remove enter */
548 cptype = parse_cp_type(buf, count);
549 free_page((unsigned long) buf);
551 if (cptype == CT_NONE)
554 err = lkdtm_register_cpoint(which);
563 /* Generic read callback that just prints out the available crash types */
564 static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
565 size_t count, loff_t *off)
570 buf = (char *)__get_free_page(GFP_KERNEL);
574 n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
575 for (i = 0; i < ARRAY_SIZE(cp_type); i++)
576 n += snprintf(buf + n, PAGE_SIZE - n, "%s\n", cp_type[i]);
579 out = simple_read_from_buffer(user_buf, count, off,
581 free_page((unsigned long) buf);
586 static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
592 static ssize_t int_hardware_entry(struct file *f, const char __user *buf,
593 size_t count, loff_t *off)
595 return do_register_entry(CN_INT_HARDWARE_ENTRY, f, buf, count, off);
598 static ssize_t int_hw_irq_en(struct file *f, const char __user *buf,
599 size_t count, loff_t *off)
601 return do_register_entry(CN_INT_HW_IRQ_EN, f, buf, count, off);
604 static ssize_t int_tasklet_entry(struct file *f, const char __user *buf,
605 size_t count, loff_t *off)
607 return do_register_entry(CN_INT_TASKLET_ENTRY, f, buf, count, off);
610 static ssize_t fs_devrw_entry(struct file *f, const char __user *buf,
611 size_t count, loff_t *off)
613 return do_register_entry(CN_FS_DEVRW, f, buf, count, off);
616 static ssize_t mem_swapout_entry(struct file *f, const char __user *buf,
617 size_t count, loff_t *off)
619 return do_register_entry(CN_MEM_SWAPOUT, f, buf, count, off);
622 static ssize_t timeradd_entry(struct file *f, const char __user *buf,
623 size_t count, loff_t *off)
625 return do_register_entry(CN_TIMERADD, f, buf, count, off);
628 static ssize_t scsi_dispatch_cmd_entry(struct file *f,
629 const char __user *buf, size_t count, loff_t *off)
631 return do_register_entry(CN_SCSI_DISPATCH_CMD, f, buf, count, off);
634 static ssize_t ide_core_cp_entry(struct file *f, const char __user *buf,
635 size_t count, loff_t *off)
637 return do_register_entry(CN_IDE_CORE_CP, f, buf, count, off);
640 /* Special entry to just crash directly. Available without KPROBEs */
641 static ssize_t direct_entry(struct file *f, const char __user *user_buf,
642 size_t count, loff_t *off)
647 if (count >= PAGE_SIZE)
652 buf = (char *)__get_free_page(GFP_KERNEL);
655 if (copy_from_user(buf, user_buf, count)) {
656 free_page((unsigned long) buf);
659 /* NULL-terminate and remove enter */
663 type = parse_cp_type(buf, count);
664 free_page((unsigned long) buf);
668 pr_info("Performing direct entry %s\n", cp_type_to_str(type));
669 lkdtm_do_action(type);
677 const struct file_operations fops;
680 static const struct crash_entry crash_entries[] = {
681 {"DIRECT", {.read = lkdtm_debugfs_read,
682 .llseek = generic_file_llseek,
683 .open = lkdtm_debugfs_open,
684 .write = direct_entry} },
685 {"INT_HARDWARE_ENTRY", {.read = lkdtm_debugfs_read,
686 .llseek = generic_file_llseek,
687 .open = lkdtm_debugfs_open,
688 .write = int_hardware_entry} },
689 {"INT_HW_IRQ_EN", {.read = lkdtm_debugfs_read,
690 .llseek = generic_file_llseek,
691 .open = lkdtm_debugfs_open,
692 .write = int_hw_irq_en} },
693 {"INT_TASKLET_ENTRY", {.read = lkdtm_debugfs_read,
694 .llseek = generic_file_llseek,
695 .open = lkdtm_debugfs_open,
696 .write = int_tasklet_entry} },
697 {"FS_DEVRW", {.read = lkdtm_debugfs_read,
698 .llseek = generic_file_llseek,
699 .open = lkdtm_debugfs_open,
700 .write = fs_devrw_entry} },
701 {"MEM_SWAPOUT", {.read = lkdtm_debugfs_read,
702 .llseek = generic_file_llseek,
703 .open = lkdtm_debugfs_open,
704 .write = mem_swapout_entry} },
705 {"TIMERADD", {.read = lkdtm_debugfs_read,
706 .llseek = generic_file_llseek,
707 .open = lkdtm_debugfs_open,
708 .write = timeradd_entry} },
709 {"SCSI_DISPATCH_CMD", {.read = lkdtm_debugfs_read,
710 .llseek = generic_file_llseek,
711 .open = lkdtm_debugfs_open,
712 .write = scsi_dispatch_cmd_entry} },
713 {"IDE_CORE_CP", {.read = lkdtm_debugfs_read,
714 .llseek = generic_file_llseek,
715 .open = lkdtm_debugfs_open,
716 .write = ide_core_cp_entry} },
719 static struct dentry *lkdtm_debugfs_root;
721 static int __init lkdtm_module_init(void)
724 int n_debugfs_entries = 1; /* Assume only the direct entry */
727 /* Handle test-specific initialization. */
728 lkdtm_bugs_init(&recur_count);
730 lkdtm_usercopy_init();
732 /* Register debugfs interface */
733 lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
734 if (!lkdtm_debugfs_root) {
735 pr_err("creating root dir failed\n");
739 #ifdef CONFIG_KPROBES
740 n_debugfs_entries = ARRAY_SIZE(crash_entries);
743 for (i = 0; i < n_debugfs_entries; i++) {
744 const struct crash_entry *cur = &crash_entries[i];
747 de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
750 pr_err("could not create %s\n", cur->name);
755 if (lkdtm_parse_commandline() == -EINVAL) {
756 pr_info("Invalid command\n");
760 if (cpoint != CN_INVALID && cptype != CT_NONE) {
761 ret = lkdtm_register_cpoint(cpoint);
763 pr_info("Invalid crash point %d\n", cpoint);
766 pr_info("Crash point %s of type %s registered\n",
767 cpoint_name, cpoint_type);
769 pr_info("No crash points registered, enable through debugfs\n");
775 debugfs_remove_recursive(lkdtm_debugfs_root);
779 static void __exit lkdtm_module_exit(void)
781 debugfs_remove_recursive(lkdtm_debugfs_root);
783 /* Handle test-specific clean-up. */
784 lkdtm_usercopy_exit();
786 unregister_jprobe(&lkdtm);
787 pr_info("Crash point unregistered\n");
790 module_init(lkdtm_module_init);
791 module_exit(lkdtm_module_exit);
793 MODULE_LICENSE("GPL");
794 MODULE_DESCRIPTION("Kprobe module for testing crash dumps");