blob: aff4d27ec23528466af2443046fa72bb70c5dbcf [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002/* Inject a hwpoison memory failure on a arbitrary pfn */
Andi Kleencae681f2009-09-16 11:50:17 +02003#include <linux/module.h>
4#include <linux/debugfs.h>
5#include <linux/kernel.h>
6#include <linux/mm.h>
Wu Fengguang31d3d342009-12-16 12:19:59 +01007#include <linux/swap.h>
8#include <linux/pagemap.h>
Naoya Horiguchi43131e12010-05-28 09:29:22 +09009#include <linux/hugetlb.h>
Wu Fengguang7c116f22009-12-16 12:19:59 +010010#include "internal.h"
Andi Kleencae681f2009-09-16 11:50:17 +020011
Wu Fengguang847ce402009-12-16 12:19:58 +010012static struct dentry *hwpoison_dir;
Andi Kleencae681f2009-09-16 11:50:17 +020013
14static int hwpoison_inject(void *data, u64 val)
15{
Wu Fengguang31d3d342009-12-16 12:19:59 +010016 unsigned long pfn = val;
17 struct page *p;
Naoya Horiguchi43131e12010-05-28 09:29:22 +090018 struct page *hpage;
Wu Fengguang31d3d342009-12-16 12:19:59 +010019 int err;
20
Andi Kleencae681f2009-09-16 11:50:17 +020021 if (!capable(CAP_SYS_ADMIN))
22 return -EPERM;
Wu Fengguang31d3d342009-12-16 12:19:59 +010023
24 if (!pfn_valid(pfn))
25 return -ENXIO;
26
27 p = pfn_to_page(pfn);
Naoya Horiguchi43131e12010-05-28 09:29:22 +090028 hpage = compound_head(p);
Wu Fengguang31d3d342009-12-16 12:19:59 +010029
Wanpeng Lifb31ba32013-09-30 13:45:24 -070030 if (!hwpoison_filter_enable)
31 goto inject;
32
Yang Shid0505e92021-09-02 14:58:31 -070033 shake_page(hpage);
Wu Fengguang31d3d342009-12-16 12:19:59 +010034 /*
35 * This implies unable to support non-LRU pages.
36 */
Naoya Horiguchie386eed2015-05-05 16:23:52 -070037 if (!PageLRU(hpage) && !PageHuge(p))
Naoya Horiguchifd476722020-10-15 20:06:46 -070038 return 0;
Wu Fengguang31d3d342009-12-16 12:19:59 +010039
40 /*
Naoya Horiguchifd476722020-10-15 20:06:46 -070041 * do a racy check to make sure PG_hwpoison will only be set for
42 * the targeted owner (or on a free page).
Tony Luckcd42f4a2011-12-15 10:48:12 -080043 * memory_failure() will redo the check reliably inside page lock.
Wu Fengguang31d3d342009-12-16 12:19:59 +010044 */
Naoya Horiguchi43131e12010-05-28 09:29:22 +090045 err = hwpoison_filter(hpage);
Wu Fengguang31d3d342009-12-16 12:19:59 +010046 if (err)
Naoya Horiguchifd476722020-10-15 20:06:46 -070047 return 0;
Wu Fengguang31d3d342009-12-16 12:19:59 +010048
Andi Kleen0d57eb8d2009-12-16 12:20:01 +010049inject:
Wanpeng Li4883e992014-01-21 15:50:56 -080050 pr_info("Injecting memory failure at pfn %#lx\n", pfn);
Naoya Horiguchifd476722020-10-15 20:06:46 -070051 return memory_failure(pfn, 0);
Andi Kleencae681f2009-09-16 11:50:17 +020052}
53
Wu Fengguang847ce402009-12-16 12:19:58 +010054static int hwpoison_unpoison(void *data, u64 val)
55{
56 if (!capable(CAP_SYS_ADMIN))
57 return -EPERM;
58
59 return unpoison_memory(val);
60}
61
zhong jiang35e3d562019-11-30 17:57:35 -080062DEFINE_DEBUGFS_ATTRIBUTE(hwpoison_fops, NULL, hwpoison_inject, "%lli\n");
63DEFINE_DEBUGFS_ATTRIBUTE(unpoison_fops, NULL, hwpoison_unpoison, "%lli\n");
Andi Kleencae681f2009-09-16 11:50:17 +020064
65static void pfn_inject_exit(void)
66{
Fabian Frederickc2ea2182014-08-06 16:06:41 -070067 debugfs_remove_recursive(hwpoison_dir);
Andi Kleencae681f2009-09-16 11:50:17 +020068}
69
70static int pfn_inject_init(void)
71{
72 hwpoison_dir = debugfs_create_dir("hwpoison", NULL);
Wu Fengguang847ce402009-12-16 12:19:58 +010073
74 /*
75 * Note that the below poison/unpoison interfaces do not involve
76 * hardware status change, hence do not require hardware support.
77 * They are mainly for testing hwpoison in software level.
78 */
Greg Kroah-Hartman2fcc6e22019-01-22 16:21:10 +010079 debugfs_create_file("corrupt-pfn", 0200, hwpoison_dir, NULL,
80 &hwpoison_fops);
Wu Fengguang847ce402009-12-16 12:19:58 +010081
Greg Kroah-Hartman2fcc6e22019-01-22 16:21:10 +010082 debugfs_create_file("unpoison-pfn", 0200, hwpoison_dir, NULL,
83 &unpoison_fops);
Wu Fengguang847ce402009-12-16 12:19:58 +010084
Greg Kroah-Hartman2fcc6e22019-01-22 16:21:10 +010085 debugfs_create_u32("corrupt-filter-enable", 0600, hwpoison_dir,
86 &hwpoison_filter_enable);
Haicheng Li1bfe5fe2009-12-16 12:19:59 +010087
Greg Kroah-Hartman2fcc6e22019-01-22 16:21:10 +010088 debugfs_create_u32("corrupt-filter-dev-major", 0600, hwpoison_dir,
89 &hwpoison_filter_dev_major);
Wu Fengguang7c116f22009-12-16 12:19:59 +010090
Greg Kroah-Hartman2fcc6e22019-01-22 16:21:10 +010091 debugfs_create_u32("corrupt-filter-dev-minor", 0600, hwpoison_dir,
92 &hwpoison_filter_dev_minor);
Wu Fengguang7c116f22009-12-16 12:19:59 +010093
Greg Kroah-Hartman2fcc6e22019-01-22 16:21:10 +010094 debugfs_create_u64("corrupt-filter-flags-mask", 0600, hwpoison_dir,
95 &hwpoison_filter_flags_mask);
Wu Fengguang478c5ff2009-12-16 12:19:59 +010096
Greg Kroah-Hartman2fcc6e22019-01-22 16:21:10 +010097 debugfs_create_u64("corrupt-filter-flags-value", 0600, hwpoison_dir,
98 &hwpoison_filter_flags_value);
Wu Fengguang478c5ff2009-12-16 12:19:59 +010099
Vladimir Davydov94a59fb2015-09-09 15:35:31 -0700100#ifdef CONFIG_MEMCG
Greg Kroah-Hartman2fcc6e22019-01-22 16:21:10 +0100101 debugfs_create_u64("corrupt-filter-memcg", 0600, hwpoison_dir,
102 &hwpoison_filter_memcg);
Andi Kleen4fd466e2009-12-16 12:19:59 +0100103#endif
104
Andi Kleencae681f2009-09-16 11:50:17 +0200105 return 0;
Andi Kleencae681f2009-09-16 11:50:17 +0200106}
107
108module_init(pfn_inject_init);
109module_exit(pfn_inject_exit);
110MODULE_LICENSE("GPL");