Andi Kleen | cae681f | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 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> |
Wu Fengguang | 31d3d34 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 6 | #include <linux/swap.h> |
| 7 | #include <linux/pagemap.h> |
Wu Fengguang | 7c116f2 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 8 | #include "internal.h" |
Andi Kleen | cae681f | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 9 | |
Wu Fengguang | 847ce40 | 2009-12-16 12:19:58 +0100 | [diff] [blame] | 10 | static struct dentry *hwpoison_dir; |
Andi Kleen | cae681f | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 11 | |
| 12 | static int hwpoison_inject(void *data, u64 val) |
| 13 | { |
Wu Fengguang | 31d3d34 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 14 | unsigned long pfn = val; |
| 15 | struct page *p; |
| 16 | int err; |
| 17 | |
Andi Kleen | cae681f | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 18 | if (!capable(CAP_SYS_ADMIN)) |
| 19 | return -EPERM; |
Wu Fengguang | 31d3d34 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 20 | |
Andi Kleen | 0d57eb8d | 2009-12-16 12:20:01 +0100 | [diff] [blame] | 21 | if (!hwpoison_filter_enable) |
| 22 | goto inject; |
Wu Fengguang | 31d3d34 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 23 | if (!pfn_valid(pfn)) |
| 24 | return -ENXIO; |
| 25 | |
| 26 | p = pfn_to_page(pfn); |
| 27 | /* |
| 28 | * This implies unable to support free buddy pages. |
| 29 | */ |
| 30 | if (!get_page_unless_zero(p)) |
| 31 | return 0; |
| 32 | |
| 33 | if (!PageLRU(p)) |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 34 | shake_page(p, 0); |
Wu Fengguang | 31d3d34 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 35 | /* |
| 36 | * This implies unable to support non-LRU pages. |
| 37 | */ |
| 38 | if (!PageLRU(p)) |
| 39 | return 0; |
| 40 | |
| 41 | /* |
| 42 | * do a racy check with elevated page count, to make sure PG_hwpoison |
| 43 | * will only be set for the targeted owner (or on a free page). |
| 44 | * We temporarily take page lock for try_get_mem_cgroup_from_page(). |
| 45 | * __memory_failure() will redo the check reliably inside page lock. |
| 46 | */ |
| 47 | lock_page(p); |
| 48 | err = hwpoison_filter(p); |
| 49 | unlock_page(p); |
| 50 | if (err) |
| 51 | return 0; |
| 52 | |
Andi Kleen | 0d57eb8d | 2009-12-16 12:20:01 +0100 | [diff] [blame] | 53 | inject: |
Wu Fengguang | 31d3d34 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 54 | printk(KERN_INFO "Injecting memory failure at pfn %lx\n", pfn); |
| 55 | return __memory_failure(pfn, 18, MF_COUNT_INCREASED); |
Andi Kleen | cae681f | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Wu Fengguang | 847ce40 | 2009-12-16 12:19:58 +0100 | [diff] [blame] | 58 | static int hwpoison_unpoison(void *data, u64 val) |
| 59 | { |
| 60 | if (!capable(CAP_SYS_ADMIN)) |
| 61 | return -EPERM; |
| 62 | |
| 63 | return unpoison_memory(val); |
| 64 | } |
| 65 | |
Andi Kleen | cae681f | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 66 | DEFINE_SIMPLE_ATTRIBUTE(hwpoison_fops, NULL, hwpoison_inject, "%lli\n"); |
Wu Fengguang | 847ce40 | 2009-12-16 12:19:58 +0100 | [diff] [blame] | 67 | DEFINE_SIMPLE_ATTRIBUTE(unpoison_fops, NULL, hwpoison_unpoison, "%lli\n"); |
Andi Kleen | cae681f | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 68 | |
| 69 | static void pfn_inject_exit(void) |
| 70 | { |
| 71 | if (hwpoison_dir) |
| 72 | debugfs_remove_recursive(hwpoison_dir); |
| 73 | } |
| 74 | |
| 75 | static int pfn_inject_init(void) |
| 76 | { |
Wu Fengguang | 847ce40 | 2009-12-16 12:19:58 +0100 | [diff] [blame] | 77 | struct dentry *dentry; |
| 78 | |
Andi Kleen | cae681f | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 79 | hwpoison_dir = debugfs_create_dir("hwpoison", NULL); |
| 80 | if (hwpoison_dir == NULL) |
| 81 | return -ENOMEM; |
Wu Fengguang | 847ce40 | 2009-12-16 12:19:58 +0100 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * Note that the below poison/unpoison interfaces do not involve |
| 85 | * hardware status change, hence do not require hardware support. |
| 86 | * They are mainly for testing hwpoison in software level. |
| 87 | */ |
| 88 | dentry = debugfs_create_file("corrupt-pfn", 0600, hwpoison_dir, |
Andi Kleen | cae681f | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 89 | NULL, &hwpoison_fops); |
Wu Fengguang | 847ce40 | 2009-12-16 12:19:58 +0100 | [diff] [blame] | 90 | if (!dentry) |
| 91 | goto fail; |
| 92 | |
| 93 | dentry = debugfs_create_file("unpoison-pfn", 0600, hwpoison_dir, |
| 94 | NULL, &unpoison_fops); |
| 95 | if (!dentry) |
| 96 | goto fail; |
| 97 | |
Haicheng Li | 1bfe5fe | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 98 | dentry = debugfs_create_u32("corrupt-filter-enable", 0600, |
| 99 | hwpoison_dir, &hwpoison_filter_enable); |
| 100 | if (!dentry) |
| 101 | goto fail; |
| 102 | |
Wu Fengguang | 7c116f2 | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 103 | dentry = debugfs_create_u32("corrupt-filter-dev-major", 0600, |
| 104 | hwpoison_dir, &hwpoison_filter_dev_major); |
| 105 | if (!dentry) |
| 106 | goto fail; |
| 107 | |
| 108 | dentry = debugfs_create_u32("corrupt-filter-dev-minor", 0600, |
| 109 | hwpoison_dir, &hwpoison_filter_dev_minor); |
| 110 | if (!dentry) |
| 111 | goto fail; |
| 112 | |
Wu Fengguang | 478c5ff | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 113 | dentry = debugfs_create_u64("corrupt-filter-flags-mask", 0600, |
| 114 | hwpoison_dir, &hwpoison_filter_flags_mask); |
| 115 | if (!dentry) |
| 116 | goto fail; |
| 117 | |
| 118 | dentry = debugfs_create_u64("corrupt-filter-flags-value", 0600, |
| 119 | hwpoison_dir, &hwpoison_filter_flags_value); |
| 120 | if (!dentry) |
| 121 | goto fail; |
| 122 | |
Andi Kleen | 4fd466e | 2009-12-16 12:19:59 +0100 | [diff] [blame] | 123 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 124 | dentry = debugfs_create_u64("corrupt-filter-memcg", 0600, |
| 125 | hwpoison_dir, &hwpoison_filter_memcg); |
| 126 | if (!dentry) |
| 127 | goto fail; |
| 128 | #endif |
| 129 | |
Andi Kleen | cae681f | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 130 | return 0; |
Wu Fengguang | 847ce40 | 2009-12-16 12:19:58 +0100 | [diff] [blame] | 131 | fail: |
| 132 | pfn_inject_exit(); |
| 133 | return -ENOMEM; |
Andi Kleen | cae681f | 2009-09-16 11:50:17 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | module_init(pfn_inject_init); |
| 137 | module_exit(pfn_inject_exit); |
| 138 | MODULE_LICENSE("GPL"); |