blob: b6ac70616c32160d84ea5c3773b8fc23d2491d89 [file] [log] [blame]
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001/* Inject a hwpoison memory failure on a arbitrary pfn */
Andi Kleencae681f2009-09-16 11:50:17 +02002#include <linux/module.h>
3#include <linux/debugfs.h>
4#include <linux/kernel.h>
5#include <linux/mm.h>
Wu Fengguang31d3d342009-12-16 12:19:59 +01006#include <linux/swap.h>
7#include <linux/pagemap.h>
Naoya Horiguchi43131e12010-05-28 09:29:22 +09008#include <linux/hugetlb.h>
Wu Fengguang7c116f22009-12-16 12:19:59 +01009#include "internal.h"
Andi Kleencae681f2009-09-16 11:50:17 +020010
Wu Fengguang847ce402009-12-16 12:19:58 +010011static struct dentry *hwpoison_dir;
Andi Kleencae681f2009-09-16 11:50:17 +020012
13static int hwpoison_inject(void *data, u64 val)
14{
Wu Fengguang31d3d342009-12-16 12:19:59 +010015 unsigned long pfn = val;
16 struct page *p;
Naoya Horiguchi43131e12010-05-28 09:29:22 +090017 struct page *hpage;
Wu Fengguang31d3d342009-12-16 12:19:59 +010018 int err;
19
Andi Kleencae681f2009-09-16 11:50:17 +020020 if (!capable(CAP_SYS_ADMIN))
21 return -EPERM;
Wu Fengguang31d3d342009-12-16 12:19:59 +010022
23 if (!pfn_valid(pfn))
24 return -ENXIO;
25
26 p = pfn_to_page(pfn);
Naoya Horiguchi43131e12010-05-28 09:29:22 +090027 hpage = compound_head(p);
Wu Fengguang31d3d342009-12-16 12:19:59 +010028 /*
29 * This implies unable to support free buddy pages.
30 */
Naoya Horiguchiead07f62015-06-24 16:56:48 -070031 if (!get_hwpoison_page(p))
Wu Fengguang31d3d342009-12-16 12:19:59 +010032 return 0;
33
Wanpeng Lifb31ba32013-09-30 13:45:24 -070034 if (!hwpoison_filter_enable)
35 goto inject;
36
Naoya Horiguchi8bcb74d2017-05-03 14:56:19 -070037 shake_page(hpage, 0);
Wu Fengguang31d3d342009-12-16 12:19:59 +010038 /*
39 * This implies unable to support non-LRU pages.
40 */
Naoya Horiguchie386eed2015-05-05 16:23:52 -070041 if (!PageLRU(hpage) && !PageHuge(p))
Naoya Horiguchi7ea434a2015-05-05 16:23:49 -070042 goto put_out;
Wu Fengguang31d3d342009-12-16 12:19:59 +010043
44 /*
45 * do a racy check with elevated page count, to make sure PG_hwpoison
46 * will only be set for the targeted owner (or on a free page).
Tony Luckcd42f4a2011-12-15 10:48:12 -080047 * memory_failure() will redo the check reliably inside page lock.
Wu Fengguang31d3d342009-12-16 12:19:59 +010048 */
Naoya Horiguchi43131e12010-05-28 09:29:22 +090049 err = hwpoison_filter(hpage);
Wu Fengguang31d3d342009-12-16 12:19:59 +010050 if (err)
Naoya Horiguchi7ea434a2015-05-05 16:23:49 -070051 goto put_out;
Wu Fengguang31d3d342009-12-16 12:19:59 +010052
Andi Kleen0d57eb8d2009-12-16 12:20:01 +010053inject:
Wanpeng Li4883e992014-01-21 15:50:56 -080054 pr_info("Injecting memory failure at pfn %#lx\n", pfn);
Eric W. Biederman83b57532017-07-09 18:14:01 -050055 return memory_failure(pfn, MF_COUNT_INCREASED);
Naoya Horiguchi7ea434a2015-05-05 16:23:49 -070056put_out:
Wanpeng Libe917482015-09-08 15:03:18 -070057 put_hwpoison_page(p);
Naoya Horiguchi7ea434a2015-05-05 16:23:49 -070058 return 0;
Andi Kleencae681f2009-09-16 11:50:17 +020059}
60
Wu Fengguang847ce402009-12-16 12:19:58 +010061static int hwpoison_unpoison(void *data, u64 val)
62{
63 if (!capable(CAP_SYS_ADMIN))
64 return -EPERM;
65
66 return unpoison_memory(val);
67}
68
Andi Kleencae681f2009-09-16 11:50:17 +020069DEFINE_SIMPLE_ATTRIBUTE(hwpoison_fops, NULL, hwpoison_inject, "%lli\n");
Wu Fengguang847ce402009-12-16 12:19:58 +010070DEFINE_SIMPLE_ATTRIBUTE(unpoison_fops, NULL, hwpoison_unpoison, "%lli\n");
Andi Kleencae681f2009-09-16 11:50:17 +020071
72static void pfn_inject_exit(void)
73{
Fabian Frederickc2ea2182014-08-06 16:06:41 -070074 debugfs_remove_recursive(hwpoison_dir);
Andi Kleencae681f2009-09-16 11:50:17 +020075}
76
77static int pfn_inject_init(void)
78{
Wu Fengguang847ce402009-12-16 12:19:58 +010079 struct dentry *dentry;
80
Andi Kleencae681f2009-09-16 11:50:17 +020081 hwpoison_dir = debugfs_create_dir("hwpoison", NULL);
82 if (hwpoison_dir == NULL)
83 return -ENOMEM;
Wu Fengguang847ce402009-12-16 12:19:58 +010084
85 /*
86 * Note that the below poison/unpoison interfaces do not involve
87 * hardware status change, hence do not require hardware support.
88 * They are mainly for testing hwpoison in software level.
89 */
Wanpeng Li2d1e8b32013-09-11 14:23:00 -070090 dentry = debugfs_create_file("corrupt-pfn", 0200, hwpoison_dir,
Andi Kleencae681f2009-09-16 11:50:17 +020091 NULL, &hwpoison_fops);
Wu Fengguang847ce402009-12-16 12:19:58 +010092 if (!dentry)
93 goto fail;
94
Wanpeng Li2d1e8b32013-09-11 14:23:00 -070095 dentry = debugfs_create_file("unpoison-pfn", 0200, hwpoison_dir,
Wu Fengguang847ce402009-12-16 12:19:58 +010096 NULL, &unpoison_fops);
97 if (!dentry)
98 goto fail;
99
Haicheng Li1bfe5fe2009-12-16 12:19:59 +0100100 dentry = debugfs_create_u32("corrupt-filter-enable", 0600,
101 hwpoison_dir, &hwpoison_filter_enable);
102 if (!dentry)
103 goto fail;
104
Wu Fengguang7c116f22009-12-16 12:19:59 +0100105 dentry = debugfs_create_u32("corrupt-filter-dev-major", 0600,
106 hwpoison_dir, &hwpoison_filter_dev_major);
107 if (!dentry)
108 goto fail;
109
110 dentry = debugfs_create_u32("corrupt-filter-dev-minor", 0600,
111 hwpoison_dir, &hwpoison_filter_dev_minor);
112 if (!dentry)
113 goto fail;
114
Wu Fengguang478c5ff2009-12-16 12:19:59 +0100115 dentry = debugfs_create_u64("corrupt-filter-flags-mask", 0600,
116 hwpoison_dir, &hwpoison_filter_flags_mask);
117 if (!dentry)
118 goto fail;
119
120 dentry = debugfs_create_u64("corrupt-filter-flags-value", 0600,
121 hwpoison_dir, &hwpoison_filter_flags_value);
122 if (!dentry)
123 goto fail;
124
Vladimir Davydov94a59fb2015-09-09 15:35:31 -0700125#ifdef CONFIG_MEMCG
Andi Kleen4fd466e2009-12-16 12:19:59 +0100126 dentry = debugfs_create_u64("corrupt-filter-memcg", 0600,
127 hwpoison_dir, &hwpoison_filter_memcg);
128 if (!dentry)
129 goto fail;
130#endif
131
Andi Kleencae681f2009-09-16 11:50:17 +0200132 return 0;
Wu Fengguang847ce402009-12-16 12:19:58 +0100133fail:
134 pfn_inject_exit();
135 return -ENOMEM;
Andi Kleencae681f2009-09-16 11:50:17 +0200136}
137
138module_init(pfn_inject_init);
139module_exit(pfn_inject_exit);
140MODULE_LICENSE("GPL");