lkdtm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. *
  29. * Debugfs support added by Simon Kagstrom <simon.kagstrom@netinsight.net>
  30. *
  31. * See Documentation/fault-injection/provoke-crashes.txt for instructions
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/fs.h>
  35. #include <linux/module.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/kprobes.h>
  38. #include <linux/list.h>
  39. #include <linux/init.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/hrtimer.h>
  42. #include <linux/slab.h>
  43. #include <scsi/scsi_cmnd.h>
  44. #include <linux/debugfs.h>
  45. #include <linux/vmalloc.h>
  46. #include <linux/mman.h>
  47. #ifdef CONFIG_IDE
  48. #include <linux/ide.h>
  49. #endif
  50. /*
  51. * Make sure our attempts to over run the kernel stack doesn't trigger
  52. * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
  53. * recurse past the end of THREAD_SIZE by default.
  54. */
  55. #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
  56. #define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
  57. #else
  58. #define REC_STACK_SIZE (THREAD_SIZE / 8)
  59. #endif
  60. #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
  61. #define DEFAULT_COUNT 10
  62. #define EXEC_SIZE 64
  63. enum cname {
  64. CN_INVALID,
  65. CN_INT_HARDWARE_ENTRY,
  66. CN_INT_HW_IRQ_EN,
  67. CN_INT_TASKLET_ENTRY,
  68. CN_FS_DEVRW,
  69. CN_MEM_SWAPOUT,
  70. CN_TIMERADD,
  71. CN_SCSI_DISPATCH_CMD,
  72. CN_IDE_CORE_CP,
  73. CN_DIRECT,
  74. };
  75. enum ctype {
  76. CT_NONE,
  77. CT_PANIC,
  78. CT_BUG,
  79. CT_WARNING,
  80. CT_EXCEPTION,
  81. CT_LOOP,
  82. CT_OVERFLOW,
  83. CT_CORRUPT_STACK,
  84. CT_UNALIGNED_LOAD_STORE_WRITE,
  85. CT_OVERWRITE_ALLOCATION,
  86. CT_WRITE_AFTER_FREE,
  87. CT_SOFTLOCKUP,
  88. CT_HARDLOCKUP,
  89. CT_SPINLOCKUP,
  90. CT_HUNG_TASK,
  91. CT_EXEC_DATA,
  92. CT_EXEC_STACK,
  93. CT_EXEC_KMALLOC,
  94. CT_EXEC_VMALLOC,
  95. CT_EXEC_USERSPACE,
  96. CT_ACCESS_USERSPACE,
  97. CT_WRITE_RO,
  98. };
  99. static char* cp_name[] = {
  100. "INT_HARDWARE_ENTRY",
  101. "INT_HW_IRQ_EN",
  102. "INT_TASKLET_ENTRY",
  103. "FS_DEVRW",
  104. "MEM_SWAPOUT",
  105. "TIMERADD",
  106. "SCSI_DISPATCH_CMD",
  107. "IDE_CORE_CP",
  108. "DIRECT",
  109. };
  110. static char* cp_type[] = {
  111. "PANIC",
  112. "BUG",
  113. "WARNING",
  114. "EXCEPTION",
  115. "LOOP",
  116. "OVERFLOW",
  117. "CORRUPT_STACK",
  118. "UNALIGNED_LOAD_STORE_WRITE",
  119. "OVERWRITE_ALLOCATION",
  120. "WRITE_AFTER_FREE",
  121. "SOFTLOCKUP",
  122. "HARDLOCKUP",
  123. "SPINLOCKUP",
  124. "HUNG_TASK",
  125. "EXEC_DATA",
  126. "EXEC_STACK",
  127. "EXEC_KMALLOC",
  128. "EXEC_VMALLOC",
  129. "EXEC_USERSPACE",
  130. "ACCESS_USERSPACE",
  131. "WRITE_RO",
  132. };
  133. static struct jprobe lkdtm;
  134. static int lkdtm_parse_commandline(void);
  135. static void lkdtm_handler(void);
  136. static char* cpoint_name;
  137. static char* cpoint_type;
  138. static int cpoint_count = DEFAULT_COUNT;
  139. static int recur_count = REC_NUM_DEFAULT;
  140. static enum cname cpoint = CN_INVALID;
  141. static enum ctype cptype = CT_NONE;
  142. static int count = DEFAULT_COUNT;
  143. static DEFINE_SPINLOCK(count_lock);
  144. static DEFINE_SPINLOCK(lock_me_up);
  145. static u8 data_area[EXEC_SIZE];
  146. static const unsigned long rodata = 0xAA55AA55;
  147. module_param(recur_count, int, 0644);
  148. MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
  149. module_param(cpoint_name, charp, 0444);
  150. MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
  151. module_param(cpoint_type, charp, 0444);
  152. MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
  153. "hitting the crash point");
  154. module_param(cpoint_count, int, 0644);
  155. MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
  156. "crash point is to be hit to trigger action");
  157. static unsigned int jp_do_irq(unsigned int irq)
  158. {
  159. lkdtm_handler();
  160. jprobe_return();
  161. return 0;
  162. }
  163. static irqreturn_t jp_handle_irq_event(unsigned int irq,
  164. struct irqaction *action)
  165. {
  166. lkdtm_handler();
  167. jprobe_return();
  168. return 0;
  169. }
  170. static void jp_tasklet_action(struct softirq_action *a)
  171. {
  172. lkdtm_handler();
  173. jprobe_return();
  174. }
  175. static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
  176. {
  177. lkdtm_handler();
  178. jprobe_return();
  179. }
  180. struct scan_control;
  181. static unsigned long jp_shrink_inactive_list(unsigned long max_scan,
  182. struct zone *zone,
  183. struct scan_control *sc)
  184. {
  185. lkdtm_handler();
  186. jprobe_return();
  187. return 0;
  188. }
  189. static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
  190. const enum hrtimer_mode mode)
  191. {
  192. lkdtm_handler();
  193. jprobe_return();
  194. return 0;
  195. }
  196. static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
  197. {
  198. lkdtm_handler();
  199. jprobe_return();
  200. return 0;
  201. }
  202. #ifdef CONFIG_IDE
  203. static int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
  204. struct block_device *bdev, unsigned int cmd,
  205. unsigned long arg)
  206. {
  207. lkdtm_handler();
  208. jprobe_return();
  209. return 0;
  210. }
  211. #endif
  212. /* Return the crashpoint number or NONE if the name is invalid */
  213. static enum ctype parse_cp_type(const char *what, size_t count)
  214. {
  215. int i;
  216. for (i = 0; i < ARRAY_SIZE(cp_type); i++) {
  217. if (!strcmp(what, cp_type[i]))
  218. return i + 1;
  219. }
  220. return CT_NONE;
  221. }
  222. static const char *cp_type_to_str(enum ctype type)
  223. {
  224. if (type == CT_NONE || type < 0 || type > ARRAY_SIZE(cp_type))
  225. return "None";
  226. return cp_type[type - 1];
  227. }
  228. static const char *cp_name_to_str(enum cname name)
  229. {
  230. if (name == CN_INVALID || name < 0 || name > ARRAY_SIZE(cp_name))
  231. return "INVALID";
  232. return cp_name[name - 1];
  233. }
  234. static int lkdtm_parse_commandline(void)
  235. {
  236. int i;
  237. unsigned long flags;
  238. if (cpoint_count < 1 || recur_count < 1)
  239. return -EINVAL;
  240. spin_lock_irqsave(&count_lock, flags);
  241. count = cpoint_count;
  242. spin_unlock_irqrestore(&count_lock, flags);
  243. /* No special parameters */
  244. if (!cpoint_type && !cpoint_name)
  245. return 0;
  246. /* Neither or both of these need to be set */
  247. if (!cpoint_type || !cpoint_name)
  248. return -EINVAL;
  249. cptype = parse_cp_type(cpoint_type, strlen(cpoint_type));
  250. if (cptype == CT_NONE)
  251. return -EINVAL;
  252. for (i = 0; i < ARRAY_SIZE(cp_name); i++) {
  253. if (!strcmp(cpoint_name, cp_name[i])) {
  254. cpoint = i + 1;
  255. return 0;
  256. }
  257. }
  258. /* Could not find a valid crash point */
  259. return -EINVAL;
  260. }
  261. static int recursive_loop(int remaining)
  262. {
  263. char buf[REC_STACK_SIZE];
  264. /* Make sure compiler does not optimize this away. */
  265. memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
  266. if (!remaining)
  267. return 0;
  268. else
  269. return recursive_loop(remaining - 1);
  270. }
  271. static void do_nothing(void)
  272. {
  273. return;
  274. }
  275. static noinline void corrupt_stack(void)
  276. {
  277. /* Use default char array length that triggers stack protection. */
  278. char data[8];
  279. memset((void *)data, 0, 64);
  280. }
  281. static void execute_location(void *dst)
  282. {
  283. void (*func)(void) = dst;
  284. memcpy(dst, do_nothing, EXEC_SIZE);
  285. func();
  286. }
  287. static void execute_user_location(void *dst)
  288. {
  289. /* Intentionally crossing kernel/user memory boundary. */
  290. void (*func)(void) = dst;
  291. if (copy_to_user((void __user *)dst, do_nothing, EXEC_SIZE))
  292. return;
  293. func();
  294. }
  295. static void lkdtm_do_action(enum ctype which)
  296. {
  297. switch (which) {
  298. case CT_PANIC:
  299. panic("dumptest");
  300. break;
  301. case CT_BUG:
  302. BUG();
  303. break;
  304. case CT_WARNING:
  305. WARN_ON(1);
  306. break;
  307. case CT_EXCEPTION:
  308. *((int *) 0) = 0;
  309. break;
  310. case CT_LOOP:
  311. for (;;)
  312. ;
  313. break;
  314. case CT_OVERFLOW:
  315. (void) recursive_loop(recur_count);
  316. break;
  317. case CT_CORRUPT_STACK:
  318. corrupt_stack();
  319. break;
  320. case CT_UNALIGNED_LOAD_STORE_WRITE: {
  321. static u8 data[5] __attribute__((aligned(4))) = {1, 2,
  322. 3, 4, 5};
  323. u32 *p;
  324. u32 val = 0x12345678;
  325. p = (u32 *)(data + 1);
  326. if (*p == 0)
  327. val = 0x87654321;
  328. *p = val;
  329. break;
  330. }
  331. case CT_OVERWRITE_ALLOCATION: {
  332. size_t len = 1020;
  333. u32 *data = kmalloc(len, GFP_KERNEL);
  334. data[1024 / sizeof(u32)] = 0x12345678;
  335. kfree(data);
  336. break;
  337. }
  338. case CT_WRITE_AFTER_FREE: {
  339. size_t len = 1024;
  340. u32 *data = kmalloc(len, GFP_KERNEL);
  341. kfree(data);
  342. schedule();
  343. memset(data, 0x78, len);
  344. break;
  345. }
  346. case CT_SOFTLOCKUP:
  347. preempt_disable();
  348. for (;;)
  349. cpu_relax();
  350. break;
  351. case CT_HARDLOCKUP:
  352. local_irq_disable();
  353. for (;;)
  354. cpu_relax();
  355. break;
  356. case CT_SPINLOCKUP:
  357. /* Must be called twice to trigger. */
  358. spin_lock(&lock_me_up);
  359. /* Let sparse know we intended to exit holding the lock. */
  360. __release(&lock_me_up);
  361. break;
  362. case CT_HUNG_TASK:
  363. set_current_state(TASK_UNINTERRUPTIBLE);
  364. schedule();
  365. break;
  366. case CT_EXEC_DATA:
  367. execute_location(data_area);
  368. break;
  369. case CT_EXEC_STACK: {
  370. u8 stack_area[EXEC_SIZE];
  371. execute_location(stack_area);
  372. break;
  373. }
  374. case CT_EXEC_KMALLOC: {
  375. u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
  376. execute_location(kmalloc_area);
  377. kfree(kmalloc_area);
  378. break;
  379. }
  380. case CT_EXEC_VMALLOC: {
  381. u32 *vmalloc_area = vmalloc(EXEC_SIZE);
  382. execute_location(vmalloc_area);
  383. vfree(vmalloc_area);
  384. break;
  385. }
  386. case CT_EXEC_USERSPACE: {
  387. unsigned long user_addr;
  388. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  389. PROT_READ | PROT_WRITE | PROT_EXEC,
  390. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  391. if (user_addr >= TASK_SIZE) {
  392. pr_warn("Failed to allocate user memory\n");
  393. return;
  394. }
  395. execute_user_location((void *)user_addr);
  396. vm_munmap(user_addr, PAGE_SIZE);
  397. break;
  398. }
  399. case CT_ACCESS_USERSPACE: {
  400. unsigned long user_addr, tmp;
  401. unsigned long *ptr;
  402. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  403. PROT_READ | PROT_WRITE | PROT_EXEC,
  404. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  405. if (user_addr >= TASK_SIZE) {
  406. pr_warn("Failed to allocate user memory\n");
  407. return;
  408. }
  409. ptr = (unsigned long *)user_addr;
  410. tmp = *ptr;
  411. tmp += 0xc0dec0de;
  412. *ptr = tmp;
  413. vm_munmap(user_addr, PAGE_SIZE);
  414. break;
  415. }
  416. case CT_WRITE_RO: {
  417. unsigned long *ptr;
  418. ptr = (unsigned long *)&rodata;
  419. *ptr ^= 0xabcd1234;
  420. break;
  421. }
  422. case CT_NONE:
  423. default:
  424. break;
  425. }
  426. }
  427. static void lkdtm_handler(void)
  428. {
  429. unsigned long flags;
  430. bool do_it = false;
  431. spin_lock_irqsave(&count_lock, flags);
  432. count--;
  433. printk(KERN_INFO "lkdtm: Crash point %s of type %s hit, trigger in %d rounds\n",
  434. cp_name_to_str(cpoint), cp_type_to_str(cptype), count);
  435. if (count == 0) {
  436. do_it = true;
  437. count = cpoint_count;
  438. }
  439. spin_unlock_irqrestore(&count_lock, flags);
  440. if (do_it)
  441. lkdtm_do_action(cptype);
  442. }
  443. static int lkdtm_register_cpoint(enum cname which)
  444. {
  445. int ret;
  446. cpoint = CN_INVALID;
  447. if (lkdtm.entry != NULL)
  448. unregister_jprobe(&lkdtm);
  449. switch (which) {
  450. case CN_DIRECT:
  451. lkdtm_do_action(cptype);
  452. return 0;
  453. case CN_INT_HARDWARE_ENTRY:
  454. lkdtm.kp.symbol_name = "do_IRQ";
  455. lkdtm.entry = (kprobe_opcode_t*) jp_do_irq;
  456. break;
  457. case CN_INT_HW_IRQ_EN:
  458. lkdtm.kp.symbol_name = "handle_IRQ_event";
  459. lkdtm.entry = (kprobe_opcode_t*) jp_handle_irq_event;
  460. break;
  461. case CN_INT_TASKLET_ENTRY:
  462. lkdtm.kp.symbol_name = "tasklet_action";
  463. lkdtm.entry = (kprobe_opcode_t*) jp_tasklet_action;
  464. break;
  465. case CN_FS_DEVRW:
  466. lkdtm.kp.symbol_name = "ll_rw_block";
  467. lkdtm.entry = (kprobe_opcode_t*) jp_ll_rw_block;
  468. break;
  469. case CN_MEM_SWAPOUT:
  470. lkdtm.kp.symbol_name = "shrink_inactive_list";
  471. lkdtm.entry = (kprobe_opcode_t*) jp_shrink_inactive_list;
  472. break;
  473. case CN_TIMERADD:
  474. lkdtm.kp.symbol_name = "hrtimer_start";
  475. lkdtm.entry = (kprobe_opcode_t*) jp_hrtimer_start;
  476. break;
  477. case CN_SCSI_DISPATCH_CMD:
  478. lkdtm.kp.symbol_name = "scsi_dispatch_cmd";
  479. lkdtm.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd;
  480. break;
  481. case CN_IDE_CORE_CP:
  482. #ifdef CONFIG_IDE
  483. lkdtm.kp.symbol_name = "generic_ide_ioctl";
  484. lkdtm.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl;
  485. #else
  486. printk(KERN_INFO "lkdtm: Crash point not available\n");
  487. return -EINVAL;
  488. #endif
  489. break;
  490. default:
  491. printk(KERN_INFO "lkdtm: Invalid Crash Point\n");
  492. return -EINVAL;
  493. }
  494. cpoint = which;
  495. if ((ret = register_jprobe(&lkdtm)) < 0) {
  496. printk(KERN_INFO "lkdtm: Couldn't register jprobe\n");
  497. cpoint = CN_INVALID;
  498. }
  499. return ret;
  500. }
  501. static ssize_t do_register_entry(enum cname which, struct file *f,
  502. const char __user *user_buf, size_t count, loff_t *off)
  503. {
  504. char *buf;
  505. int err;
  506. if (count >= PAGE_SIZE)
  507. return -EINVAL;
  508. buf = (char *)__get_free_page(GFP_KERNEL);
  509. if (!buf)
  510. return -ENOMEM;
  511. if (copy_from_user(buf, user_buf, count)) {
  512. free_page((unsigned long) buf);
  513. return -EFAULT;
  514. }
  515. /* NULL-terminate and remove enter */
  516. buf[count] = '\0';
  517. strim(buf);
  518. cptype = parse_cp_type(buf, count);
  519. free_page((unsigned long) buf);
  520. if (cptype == CT_NONE)
  521. return -EINVAL;
  522. err = lkdtm_register_cpoint(which);
  523. if (err < 0)
  524. return err;
  525. *off += count;
  526. return count;
  527. }
  528. /* Generic read callback that just prints out the available crash types */
  529. static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
  530. size_t count, loff_t *off)
  531. {
  532. char *buf;
  533. int i, n, out;
  534. buf = (char *)__get_free_page(GFP_KERNEL);
  535. if (buf == NULL)
  536. return -ENOMEM;
  537. n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
  538. for (i = 0; i < ARRAY_SIZE(cp_type); i++)
  539. n += snprintf(buf + n, PAGE_SIZE - n, "%s\n", cp_type[i]);
  540. buf[n] = '\0';
  541. out = simple_read_from_buffer(user_buf, count, off,
  542. buf, n);
  543. free_page((unsigned long) buf);
  544. return out;
  545. }
  546. static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
  547. {
  548. return 0;
  549. }
  550. static ssize_t int_hardware_entry(struct file *f, const char __user *buf,
  551. size_t count, loff_t *off)
  552. {
  553. return do_register_entry(CN_INT_HARDWARE_ENTRY, f, buf, count, off);
  554. }
  555. static ssize_t int_hw_irq_en(struct file *f, const char __user *buf,
  556. size_t count, loff_t *off)
  557. {
  558. return do_register_entry(CN_INT_HW_IRQ_EN, f, buf, count, off);
  559. }
  560. static ssize_t int_tasklet_entry(struct file *f, const char __user *buf,
  561. size_t count, loff_t *off)
  562. {
  563. return do_register_entry(CN_INT_TASKLET_ENTRY, f, buf, count, off);
  564. }
  565. static ssize_t fs_devrw_entry(struct file *f, const char __user *buf,
  566. size_t count, loff_t *off)
  567. {
  568. return do_register_entry(CN_FS_DEVRW, f, buf, count, off);
  569. }
  570. static ssize_t mem_swapout_entry(struct file *f, const char __user *buf,
  571. size_t count, loff_t *off)
  572. {
  573. return do_register_entry(CN_MEM_SWAPOUT, f, buf, count, off);
  574. }
  575. static ssize_t timeradd_entry(struct file *f, const char __user *buf,
  576. size_t count, loff_t *off)
  577. {
  578. return do_register_entry(CN_TIMERADD, f, buf, count, off);
  579. }
  580. static ssize_t scsi_dispatch_cmd_entry(struct file *f,
  581. const char __user *buf, size_t count, loff_t *off)
  582. {
  583. return do_register_entry(CN_SCSI_DISPATCH_CMD, f, buf, count, off);
  584. }
  585. static ssize_t ide_core_cp_entry(struct file *f, const char __user *buf,
  586. size_t count, loff_t *off)
  587. {
  588. return do_register_entry(CN_IDE_CORE_CP, f, buf, count, off);
  589. }
  590. /* Special entry to just crash directly. Available without KPROBEs */
  591. static ssize_t direct_entry(struct file *f, const char __user *user_buf,
  592. size_t count, loff_t *off)
  593. {
  594. enum ctype type;
  595. char *buf;
  596. if (count >= PAGE_SIZE)
  597. return -EINVAL;
  598. if (count < 1)
  599. return -EINVAL;
  600. buf = (char *)__get_free_page(GFP_KERNEL);
  601. if (!buf)
  602. return -ENOMEM;
  603. if (copy_from_user(buf, user_buf, count)) {
  604. free_page((unsigned long) buf);
  605. return -EFAULT;
  606. }
  607. /* NULL-terminate and remove enter */
  608. buf[count] = '\0';
  609. strim(buf);
  610. type = parse_cp_type(buf, count);
  611. free_page((unsigned long) buf);
  612. if (type == CT_NONE)
  613. return -EINVAL;
  614. printk(KERN_INFO "lkdtm: Performing direct entry %s\n",
  615. cp_type_to_str(type));
  616. lkdtm_do_action(type);
  617. *off += count;
  618. return count;
  619. }
  620. struct crash_entry {
  621. const char *name;
  622. const struct file_operations fops;
  623. };
  624. static const struct crash_entry crash_entries[] = {
  625. {"DIRECT", {.read = lkdtm_debugfs_read,
  626. .llseek = generic_file_llseek,
  627. .open = lkdtm_debugfs_open,
  628. .write = direct_entry} },
  629. {"INT_HARDWARE_ENTRY", {.read = lkdtm_debugfs_read,
  630. .llseek = generic_file_llseek,
  631. .open = lkdtm_debugfs_open,
  632. .write = int_hardware_entry} },
  633. {"INT_HW_IRQ_EN", {.read = lkdtm_debugfs_read,
  634. .llseek = generic_file_llseek,
  635. .open = lkdtm_debugfs_open,
  636. .write = int_hw_irq_en} },
  637. {"INT_TASKLET_ENTRY", {.read = lkdtm_debugfs_read,
  638. .llseek = generic_file_llseek,
  639. .open = lkdtm_debugfs_open,
  640. .write = int_tasklet_entry} },
  641. {"FS_DEVRW", {.read = lkdtm_debugfs_read,
  642. .llseek = generic_file_llseek,
  643. .open = lkdtm_debugfs_open,
  644. .write = fs_devrw_entry} },
  645. {"MEM_SWAPOUT", {.read = lkdtm_debugfs_read,
  646. .llseek = generic_file_llseek,
  647. .open = lkdtm_debugfs_open,
  648. .write = mem_swapout_entry} },
  649. {"TIMERADD", {.read = lkdtm_debugfs_read,
  650. .llseek = generic_file_llseek,
  651. .open = lkdtm_debugfs_open,
  652. .write = timeradd_entry} },
  653. {"SCSI_DISPATCH_CMD", {.read = lkdtm_debugfs_read,
  654. .llseek = generic_file_llseek,
  655. .open = lkdtm_debugfs_open,
  656. .write = scsi_dispatch_cmd_entry} },
  657. {"IDE_CORE_CP", {.read = lkdtm_debugfs_read,
  658. .llseek = generic_file_llseek,
  659. .open = lkdtm_debugfs_open,
  660. .write = ide_core_cp_entry} },
  661. };
  662. static struct dentry *lkdtm_debugfs_root;
  663. static int __init lkdtm_module_init(void)
  664. {
  665. int ret = -EINVAL;
  666. int n_debugfs_entries = 1; /* Assume only the direct entry */
  667. int i;
  668. /* Register debugfs interface */
  669. lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
  670. if (!lkdtm_debugfs_root) {
  671. printk(KERN_ERR "lkdtm: creating root dir failed\n");
  672. return -ENODEV;
  673. }
  674. #ifdef CONFIG_KPROBES
  675. n_debugfs_entries = ARRAY_SIZE(crash_entries);
  676. #endif
  677. for (i = 0; i < n_debugfs_entries; i++) {
  678. const struct crash_entry *cur = &crash_entries[i];
  679. struct dentry *de;
  680. de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
  681. NULL, &cur->fops);
  682. if (de == NULL) {
  683. printk(KERN_ERR "lkdtm: could not create %s\n",
  684. cur->name);
  685. goto out_err;
  686. }
  687. }
  688. if (lkdtm_parse_commandline() == -EINVAL) {
  689. printk(KERN_INFO "lkdtm: Invalid command\n");
  690. goto out_err;
  691. }
  692. if (cpoint != CN_INVALID && cptype != CT_NONE) {
  693. ret = lkdtm_register_cpoint(cpoint);
  694. if (ret < 0) {
  695. printk(KERN_INFO "lkdtm: Invalid crash point %d\n",
  696. cpoint);
  697. goto out_err;
  698. }
  699. printk(KERN_INFO "lkdtm: Crash point %s of type %s registered\n",
  700. cpoint_name, cpoint_type);
  701. } else {
  702. printk(KERN_INFO "lkdtm: No crash points registered, enable through debugfs\n");
  703. }
  704. return 0;
  705. out_err:
  706. debugfs_remove_recursive(lkdtm_debugfs_root);
  707. return ret;
  708. }
  709. static void __exit lkdtm_module_exit(void)
  710. {
  711. debugfs_remove_recursive(lkdtm_debugfs_root);
  712. unregister_jprobe(&lkdtm);
  713. printk(KERN_INFO "lkdtm: Crash point unregistered\n");
  714. }
  715. module_init(lkdtm_module_init);
  716. module_exit(lkdtm_module_exit);
  717. MODULE_LICENSE("GPL");