hwpoison-inject.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* Inject a hwpoison memory failure on a arbitary pfn */
  2. #include <linux/module.h>
  3. #include <linux/debugfs.h>
  4. #include <linux/kernel.h>
  5. #include <linux/mm.h>
  6. static struct dentry *hwpoison_dir;
  7. static int hwpoison_inject(void *data, u64 val)
  8. {
  9. if (!capable(CAP_SYS_ADMIN))
  10. return -EPERM;
  11. printk(KERN_INFO "Injecting memory failure at pfn %Lx\n", val);
  12. return __memory_failure(val, 18, 0);
  13. }
  14. static int hwpoison_unpoison(void *data, u64 val)
  15. {
  16. if (!capable(CAP_SYS_ADMIN))
  17. return -EPERM;
  18. return unpoison_memory(val);
  19. }
  20. DEFINE_SIMPLE_ATTRIBUTE(hwpoison_fops, NULL, hwpoison_inject, "%lli\n");
  21. DEFINE_SIMPLE_ATTRIBUTE(unpoison_fops, NULL, hwpoison_unpoison, "%lli\n");
  22. static void pfn_inject_exit(void)
  23. {
  24. if (hwpoison_dir)
  25. debugfs_remove_recursive(hwpoison_dir);
  26. }
  27. static int pfn_inject_init(void)
  28. {
  29. struct dentry *dentry;
  30. hwpoison_dir = debugfs_create_dir("hwpoison", NULL);
  31. if (hwpoison_dir == NULL)
  32. return -ENOMEM;
  33. /*
  34. * Note that the below poison/unpoison interfaces do not involve
  35. * hardware status change, hence do not require hardware support.
  36. * They are mainly for testing hwpoison in software level.
  37. */
  38. dentry = debugfs_create_file("corrupt-pfn", 0600, hwpoison_dir,
  39. NULL, &hwpoison_fops);
  40. if (!dentry)
  41. goto fail;
  42. dentry = debugfs_create_file("unpoison-pfn", 0600, hwpoison_dir,
  43. NULL, &unpoison_fops);
  44. if (!dentry)
  45. goto fail;
  46. return 0;
  47. fail:
  48. pfn_inject_exit();
  49. return -ENOMEM;
  50. }
  51. module_init(pfn_inject_init);
  52. module_exit(pfn_inject_exit);
  53. MODULE_LICENSE("GPL");