Commit | Line | Data |
---|---|---|
8bb31b9d AG |
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 | * | |
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 AG |
32 | */ |
33 | ||
34 | #include <linux/kernel.h> | |
5d861d92 | 35 | #include <linux/fs.h> |
8bb31b9d | 36 | #include <linux/module.h> |
5d861d92 | 37 | #include <linux/buffer_head.h> |
8bb31b9d | 38 | #include <linux/kprobes.h> |
5d861d92 | 39 | #include <linux/list.h> |
8bb31b9d | 40 | #include <linux/init.h> |
8bb31b9d | 41 | #include <linux/interrupt.h> |
5d861d92 | 42 | #include <linux/hrtimer.h> |
5a0e3ad6 | 43 | #include <linux/slab.h> |
8bb31b9d | 44 | #include <scsi/scsi_cmnd.h> |
0347af4e | 45 | #include <linux/debugfs.h> |
8bb31b9d AG |
46 | |
47 | #ifdef CONFIG_IDE | |
48 | #include <linux/ide.h> | |
49 | #endif | |
50 | ||
8bb31b9d AG |
51 | #define DEFAULT_COUNT 10 |
52 | #define REC_NUM_DEFAULT 10 | |
53 | ||
54 | enum cname { | |
93e2f585 NK |
55 | 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, | |
8bb31b9d AG |
65 | }; |
66 | ||
67 | enum ctype { | |
93e2f585 NK |
68 | CT_NONE, |
69 | CT_PANIC, | |
70 | CT_BUG, | |
65892723 | 71 | CT_WARNING, |
93e2f585 NK |
72 | 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, | |
8bb31b9d AG |
82 | }; |
83 | ||
84 | static 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", | |
0347af4e SK |
92 | "IDE_CORE_CP", |
93 | "DIRECT", | |
8bb31b9d AG |
94 | }; |
95 | ||
96 | static char* cp_type[] = { | |
97 | "PANIC", | |
98 | "BUG", | |
65892723 | 99 | "WARNING", |
8bb31b9d AG |
100 | "EXCEPTION", |
101 | "LOOP", | |
0347af4e SK |
102 | "OVERFLOW", |
103 | "CORRUPT_STACK", | |
104 | "UNALIGNED_LOAD_STORE_WRITE", | |
105 | "OVERWRITE_ALLOCATION", | |
106 | "WRITE_AFTER_FREE", | |
a48223f9 FW |
107 | "SOFTLOCKUP", |
108 | "HARDLOCKUP", | |
109 | "HUNG_TASK", | |
8bb31b9d AG |
110 | }; |
111 | ||
112 | static struct jprobe lkdtm; | |
113 | ||
114 | static int lkdtm_parse_commandline(void); | |
115 | static void lkdtm_handler(void); | |
116 | ||
ec1c620b AV |
117 | static char* cpoint_name; |
118 | static char* cpoint_type; | |
8bb31b9d AG |
119 | static int cpoint_count = DEFAULT_COUNT; |
120 | static int recur_count = REC_NUM_DEFAULT; | |
121 | ||
93e2f585 NK |
122 | static enum cname cpoint = CN_INVALID; |
123 | static enum ctype cptype = CT_NONE; | |
8bb31b9d | 124 | static int count = DEFAULT_COUNT; |
aa2c96d6 | 125 | static DEFINE_SPINLOCK(count_lock); |
8bb31b9d AG |
126 | |
127 | module_param(recur_count, int, 0644); | |
5d861d92 RD |
128 | MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test, "\ |
129 | "default is 10"); | |
dca41306 | 130 | module_param(cpoint_name, charp, 0444); |
5d861d92 | 131 | MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed"); |
dca41306 | 132 | module_param(cpoint_type, charp, 0444); |
5d861d92 RD |
133 | MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\ |
134 | "hitting the crash point"); | |
135 | module_param(cpoint_count, int, 0644); | |
136 | MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\ | |
137 | "crash point is to be hit to trigger action"); | |
8bb31b9d | 138 | |
2118116e | 139 | static unsigned int jp_do_irq(unsigned int irq) |
8bb31b9d AG |
140 | { |
141 | lkdtm_handler(); | |
142 | jprobe_return(); | |
143 | return 0; | |
144 | } | |
145 | ||
2118116e AB |
146 | static irqreturn_t jp_handle_irq_event(unsigned int irq, |
147 | struct irqaction *action) | |
8bb31b9d AG |
148 | { |
149 | lkdtm_handler(); | |
150 | jprobe_return(); | |
151 | return 0; | |
152 | } | |
153 | ||
2118116e | 154 | static void jp_tasklet_action(struct softirq_action *a) |
8bb31b9d AG |
155 | { |
156 | lkdtm_handler(); | |
157 | jprobe_return(); | |
158 | } | |
159 | ||
2118116e | 160 | static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[]) |
8bb31b9d AG |
161 | { |
162 | lkdtm_handler(); | |
163 | jprobe_return(); | |
164 | } | |
165 | ||
166 | struct scan_control; | |
167 | ||
2118116e AB |
168 | static unsigned long jp_shrink_inactive_list(unsigned long max_scan, |
169 | struct zone *zone, | |
170 | struct scan_control *sc) | |
8bb31b9d AG |
171 | { |
172 | lkdtm_handler(); | |
173 | jprobe_return(); | |
174 | return 0; | |
175 | } | |
176 | ||
2118116e AB |
177 | static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim, |
178 | const enum hrtimer_mode mode) | |
8bb31b9d AG |
179 | { |
180 | lkdtm_handler(); | |
181 | jprobe_return(); | |
182 | return 0; | |
183 | } | |
184 | ||
2118116e | 185 | static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd) |
8bb31b9d AG |
186 | { |
187 | lkdtm_handler(); | |
188 | jprobe_return(); | |
189 | return 0; | |
190 | } | |
191 | ||
192 | #ifdef CONFIG_IDE | |
193 | int 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 | ||
0347af4e SK |
203 | /* Return the crashpoint number or NONE if the name is invalid */ |
204 | static 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 | ||
93e2f585 | 213 | return CT_NONE; |
0347af4e SK |
214 | } |
215 | ||
216 | static const char *cp_type_to_str(enum ctype type) | |
217 | { | |
93e2f585 | 218 | if (type == CT_NONE || type < 0 || type > ARRAY_SIZE(cp_type)) |
0347af4e SK |
219 | return "None"; |
220 | ||
221 | return cp_type[type - 1]; | |
222 | } | |
223 | ||
224 | static const char *cp_name_to_str(enum cname name) | |
225 | { | |
93e2f585 | 226 | if (name == CN_INVALID || name < 0 || name > ARRAY_SIZE(cp_name)) |
0347af4e SK |
227 | return "INVALID"; |
228 | ||
229 | return cp_name[name - 1]; | |
230 | } | |
231 | ||
232 | ||
8bb31b9d AG |
233 | static int lkdtm_parse_commandline(void) |
234 | { | |
235 | int i; | |
aa2c96d6 | 236 | unsigned long flags; |
8bb31b9d | 237 | |
0347af4e | 238 | if (cpoint_count < 1 || recur_count < 1) |
8bb31b9d AG |
239 | return -EINVAL; |
240 | ||
aa2c96d6 | 241 | spin_lock_irqsave(&count_lock, flags); |
0347af4e | 242 | count = cpoint_count; |
aa2c96d6 | 243 | spin_unlock_irqrestore(&count_lock, flags); |
0347af4e SK |
244 | |
245 | /* 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)); | |
93e2f585 | 254 | if (cptype == CT_NONE) |
0347af4e SK |
255 | return -EINVAL; |
256 | ||
257 | for (i = 0; i < ARRAY_SIZE(cp_name); i++) { | |
8bb31b9d AG |
258 | if (!strcmp(cpoint_name, cp_name[i])) { |
259 | cpoint = i + 1; | |
0347af4e | 260 | return 0; |
8bb31b9d AG |
261 | } |
262 | } | |
263 | ||
0347af4e SK |
264 | /* Could not find a valid crash point */ |
265 | return -EINVAL; | |
8bb31b9d AG |
266 | } |
267 | ||
268 | static 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 | ||
0347af4e | 280 | static void lkdtm_do_action(enum ctype which) |
8bb31b9d | 281 | { |
0347af4e | 282 | switch (which) { |
93e2f585 | 283 | case CT_PANIC: |
0347af4e SK |
284 | panic("dumptest"); |
285 | break; | |
93e2f585 | 286 | case CT_BUG: |
0347af4e SK |
287 | BUG(); |
288 | break; | |
65892723 KC |
289 | case CT_WARNING: |
290 | WARN_ON(1); | |
291 | break; | |
93e2f585 | 292 | case CT_EXCEPTION: |
0347af4e SK |
293 | *((int *) 0) = 0; |
294 | break; | |
93e2f585 | 295 | case CT_LOOP: |
0347af4e SK |
296 | for (;;) |
297 | ; | |
298 | break; | |
93e2f585 | 299 | case CT_OVERFLOW: |
0347af4e SK |
300 | (void) recursive_loop(0); |
301 | break; | |
93e2f585 | 302 | case CT_CORRUPT_STACK: { |
4f198289 KC |
303 | /* Make sure the compiler creates and uses an 8 char array. */ |
304 | volatile char data[8]; | |
0347af4e | 305 | |
4f198289 | 306 | memset((void *)data, 0, 64); |
0347af4e SK |
307 | break; |
308 | } | |
93e2f585 | 309 | case CT_UNALIGNED_LOAD_STORE_WRITE: { |
0347af4e SK |
310 | 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 | } | |
93e2f585 | 321 | case CT_OVERWRITE_ALLOCATION: { |
0347af4e SK |
322 | size_t len = 1020; |
323 | u32 *data = kmalloc(len, GFP_KERNEL); | |
324 | ||
325 | data[1024 / sizeof(u32)] = 0x12345678; | |
326 | kfree(data); | |
327 | break; | |
328 | } | |
93e2f585 | 329 | case CT_WRITE_AFTER_FREE: { |
0347af4e SK |
330 | 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 | } | |
93e2f585 | 338 | case CT_SOFTLOCKUP: |
a48223f9 FW |
339 | preempt_disable(); |
340 | for (;;) | |
341 | cpu_relax(); | |
342 | break; | |
93e2f585 | 343 | case CT_HARDLOCKUP: |
a48223f9 FW |
344 | local_irq_disable(); |
345 | for (;;) | |
346 | cpu_relax(); | |
347 | break; | |
93e2f585 | 348 | case CT_HUNG_TASK: |
a48223f9 FW |
349 | set_current_state(TASK_UNINTERRUPTIBLE); |
350 | schedule(); | |
351 | break; | |
93e2f585 | 352 | case CT_NONE: |
0347af4e SK |
353 | default: |
354 | break; | |
355 | } | |
356 | ||
357 | } | |
358 | ||
359 | static void lkdtm_handler(void) | |
360 | { | |
aa2c96d6 | 361 | unsigned long flags; |
92618184 | 362 | bool do_it = false; |
aa2c96d6 JH |
363 | |
364 | spin_lock_irqsave(&count_lock, flags); | |
0347af4e SK |
365 | 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); | |
8bb31b9d AG |
368 | |
369 | if (count == 0) { | |
92618184 | 370 | do_it = true; |
8bb31b9d AG |
371 | count = cpoint_count; |
372 | } | |
aa2c96d6 | 373 | spin_unlock_irqrestore(&count_lock, flags); |
92618184 CW |
374 | |
375 | if (do_it) | |
376 | lkdtm_do_action(cptype); | |
8bb31b9d AG |
377 | } |
378 | ||
0347af4e | 379 | static int lkdtm_register_cpoint(enum cname which) |
8bb31b9d AG |
380 | { |
381 | int ret; | |
382 | ||
93e2f585 | 383 | cpoint = CN_INVALID; |
0347af4e SK |
384 | if (lkdtm.entry != NULL) |
385 | unregister_jprobe(&lkdtm); | |
8bb31b9d | 386 | |
0347af4e | 387 | switch (which) { |
93e2f585 | 388 | case CN_DIRECT: |
0347af4e SK |
389 | lkdtm_do_action(cptype); |
390 | return 0; | |
93e2f585 | 391 | case CN_INT_HARDWARE_ENTRY: |
f58f2fa9 | 392 | lkdtm.kp.symbol_name = "do_IRQ"; |
8bb31b9d AG |
393 | lkdtm.entry = (kprobe_opcode_t*) jp_do_irq; |
394 | break; | |
93e2f585 | 395 | case CN_INT_HW_IRQ_EN: |
8bb31b9d AG |
396 | lkdtm.kp.symbol_name = "handle_IRQ_event"; |
397 | lkdtm.entry = (kprobe_opcode_t*) jp_handle_irq_event; | |
398 | break; | |
93e2f585 | 399 | case CN_INT_TASKLET_ENTRY: |
8bb31b9d AG |
400 | lkdtm.kp.symbol_name = "tasklet_action"; |
401 | lkdtm.entry = (kprobe_opcode_t*) jp_tasklet_action; | |
402 | break; | |
93e2f585 | 403 | case CN_FS_DEVRW: |
8bb31b9d AG |
404 | lkdtm.kp.symbol_name = "ll_rw_block"; |
405 | lkdtm.entry = (kprobe_opcode_t*) jp_ll_rw_block; | |
406 | break; | |
93e2f585 | 407 | case CN_MEM_SWAPOUT: |
18a61e4a AG |
408 | lkdtm.kp.symbol_name = "shrink_inactive_list"; |
409 | lkdtm.entry = (kprobe_opcode_t*) jp_shrink_inactive_list; | |
8bb31b9d | 410 | break; |
93e2f585 | 411 | case CN_TIMERADD: |
8bb31b9d AG |
412 | lkdtm.kp.symbol_name = "hrtimer_start"; |
413 | lkdtm.entry = (kprobe_opcode_t*) jp_hrtimer_start; | |
414 | break; | |
93e2f585 | 415 | case CN_SCSI_DISPATCH_CMD: |
8bb31b9d AG |
416 | lkdtm.kp.symbol_name = "scsi_dispatch_cmd"; |
417 | lkdtm.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd; | |
418 | break; | |
93e2f585 | 419 | case CN_IDE_CORE_CP: |
8bb31b9d AG |
420 | #ifdef CONFIG_IDE |
421 | lkdtm.kp.symbol_name = "generic_ide_ioctl"; | |
422 | lkdtm.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl; | |
423 | #else | |
0347af4e SK |
424 | printk(KERN_INFO "lkdtm: Crash point not available\n"); |
425 | return -EINVAL; | |
8bb31b9d AG |
426 | #endif |
427 | break; | |
428 | default: | |
0347af4e SK |
429 | printk(KERN_INFO "lkdtm: Invalid Crash Point\n"); |
430 | return -EINVAL; | |
8bb31b9d AG |
431 | } |
432 | ||
0347af4e | 433 | cpoint = which; |
8bb31b9d | 434 | if ((ret = register_jprobe(&lkdtm)) < 0) { |
0347af4e | 435 | printk(KERN_INFO "lkdtm: Couldn't register jprobe\n"); |
93e2f585 | 436 | cpoint = CN_INVALID; |
0347af4e SK |
437 | } |
438 | ||
439 | return ret; | |
440 | } | |
441 | ||
442 | static 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 | ||
93e2f585 | 465 | if (cptype == CT_NONE) |
0347af4e SK |
466 | 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 */ | |
478 | static 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); | |
086ff4b3 AC |
485 | if (buf == NULL) |
486 | return -ENOMEM; | |
0347af4e SK |
487 | |
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 | ||
500 | static int lkdtm_debugfs_open(struct inode *inode, struct file *file) | |
501 | { | |
502 | return 0; | |
503 | } | |
504 | ||
505 | ||
506 | static ssize_t int_hardware_entry(struct file *f, const char __user *buf, | |
507 | size_t count, loff_t *off) | |
508 | { | |
93e2f585 | 509 | return do_register_entry(CN_INT_HARDWARE_ENTRY, f, buf, count, off); |
0347af4e SK |
510 | } |
511 | ||
512 | static ssize_t int_hw_irq_en(struct file *f, const char __user *buf, | |
513 | size_t count, loff_t *off) | |
514 | { | |
93e2f585 | 515 | return do_register_entry(CN_INT_HW_IRQ_EN, f, buf, count, off); |
0347af4e SK |
516 | } |
517 | ||
518 | static ssize_t int_tasklet_entry(struct file *f, const char __user *buf, | |
519 | size_t count, loff_t *off) | |
520 | { | |
93e2f585 | 521 | return do_register_entry(CN_INT_TASKLET_ENTRY, f, buf, count, off); |
0347af4e SK |
522 | } |
523 | ||
524 | static ssize_t fs_devrw_entry(struct file *f, const char __user *buf, | |
525 | size_t count, loff_t *off) | |
526 | { | |
93e2f585 | 527 | return do_register_entry(CN_FS_DEVRW, f, buf, count, off); |
0347af4e SK |
528 | } |
529 | ||
530 | static ssize_t mem_swapout_entry(struct file *f, const char __user *buf, | |
531 | size_t count, loff_t *off) | |
532 | { | |
93e2f585 | 533 | return do_register_entry(CN_MEM_SWAPOUT, f, buf, count, off); |
0347af4e SK |
534 | } |
535 | ||
536 | static ssize_t timeradd_entry(struct file *f, const char __user *buf, | |
537 | size_t count, loff_t *off) | |
538 | { | |
93e2f585 | 539 | return do_register_entry(CN_TIMERADD, f, buf, count, off); |
0347af4e SK |
540 | } |
541 | ||
542 | static ssize_t scsi_dispatch_cmd_entry(struct file *f, | |
543 | const char __user *buf, size_t count, loff_t *off) | |
544 | { | |
93e2f585 | 545 | return do_register_entry(CN_SCSI_DISPATCH_CMD, f, buf, count, off); |
0347af4e SK |
546 | } |
547 | ||
548 | static ssize_t ide_core_cp_entry(struct file *f, const char __user *buf, | |
549 | size_t count, loff_t *off) | |
550 | { | |
93e2f585 | 551 | return do_register_entry(CN_IDE_CORE_CP, f, buf, count, off); |
0347af4e SK |
552 | } |
553 | ||
554 | /* Special entry to just crash directly. Available without KPROBEs */ | |
555 | static 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); | |
93e2f585 | 579 | if (type == CT_NONE) |
0347af4e SK |
580 | 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 | ||
590 | struct crash_entry { | |
591 | const char *name; | |
592 | const struct file_operations fops; | |
593 | }; | |
594 | ||
595 | static const struct crash_entry crash_entries[] = { | |
596 | {"DIRECT", {.read = lkdtm_debugfs_read, | |
05271ec4 | 597 | .llseek = generic_file_llseek, |
0347af4e SK |
598 | .open = lkdtm_debugfs_open, |
599 | .write = direct_entry} }, | |
600 | {"INT_HARDWARE_ENTRY", {.read = lkdtm_debugfs_read, | |
05271ec4 | 601 | .llseek = generic_file_llseek, |
0347af4e SK |
602 | .open = lkdtm_debugfs_open, |
603 | .write = int_hardware_entry} }, | |
604 | {"INT_HW_IRQ_EN", {.read = lkdtm_debugfs_read, | |
05271ec4 | 605 | .llseek = generic_file_llseek, |
0347af4e SK |
606 | .open = lkdtm_debugfs_open, |
607 | .write = int_hw_irq_en} }, | |
608 | {"INT_TASKLET_ENTRY", {.read = lkdtm_debugfs_read, | |
05271ec4 | 609 | .llseek = generic_file_llseek, |
0347af4e SK |
610 | .open = lkdtm_debugfs_open, |
611 | .write = int_tasklet_entry} }, | |
612 | {"FS_DEVRW", {.read = lkdtm_debugfs_read, | |
05271ec4 | 613 | .llseek = generic_file_llseek, |
0347af4e SK |
614 | .open = lkdtm_debugfs_open, |
615 | .write = fs_devrw_entry} }, | |
616 | {"MEM_SWAPOUT", {.read = lkdtm_debugfs_read, | |
05271ec4 | 617 | .llseek = generic_file_llseek, |
0347af4e SK |
618 | .open = lkdtm_debugfs_open, |
619 | .write = mem_swapout_entry} }, | |
620 | {"TIMERADD", {.read = lkdtm_debugfs_read, | |
05271ec4 | 621 | .llseek = generic_file_llseek, |
0347af4e SK |
622 | .open = lkdtm_debugfs_open, |
623 | .write = timeradd_entry} }, | |
624 | {"SCSI_DISPATCH_CMD", {.read = lkdtm_debugfs_read, | |
05271ec4 | 625 | .llseek = generic_file_llseek, |
0347af4e SK |
626 | .open = lkdtm_debugfs_open, |
627 | .write = scsi_dispatch_cmd_entry} }, | |
628 | {"IDE_CORE_CP", {.read = lkdtm_debugfs_read, | |
05271ec4 | 629 | .llseek = generic_file_llseek, |
0347af4e SK |
630 | .open = lkdtm_debugfs_open, |
631 | .write = ide_core_cp_entry} }, | |
632 | }; | |
633 | ||
634 | static struct dentry *lkdtm_debugfs_root; | |
635 | ||
636 | static 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 | ||
93e2f585 | 671 | if (cpoint != CN_INVALID && cptype != CT_NONE) { |
0347af4e SK |
672 | 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"); | |
8bb31b9d AG |
682 | } |
683 | ||
8bb31b9d | 684 | return 0; |
0347af4e SK |
685 | |
686 | out_err: | |
687 | debugfs_remove_recursive(lkdtm_debugfs_root); | |
688 | return ret; | |
8bb31b9d AG |
689 | } |
690 | ||
2118116e | 691 | static void __exit lkdtm_module_exit(void) |
8bb31b9d | 692 | { |
0347af4e SK |
693 | debugfs_remove_recursive(lkdtm_debugfs_root); |
694 | ||
695 | unregister_jprobe(&lkdtm); | |
696 | printk(KERN_INFO "lkdtm: Crash point unregistered\n"); | |
8bb31b9d AG |
697 | } |
698 | ||
699 | module_init(lkdtm_module_init); | |
700 | module_exit(lkdtm_module_exit); | |
701 | ||
702 | MODULE_LICENSE("GPL"); |