hwpoison-inject.c 3.3 KB
Newer Older
L
Lucas De Marchi 已提交
1
/* Inject a hwpoison memory failure on a arbitrary pfn */
2 3 4 5
#include <linux/module.h>
#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/mm.h>
6 7
#include <linux/swap.h>
#include <linux/pagemap.h>
8
#include <linux/hugetlb.h>
W
Wu Fengguang 已提交
9
#include "internal.h"
10

W
Wu Fengguang 已提交
11
static struct dentry *hwpoison_dir;
12 13 14

static int hwpoison_inject(void *data, u64 val)
{
15 16
	unsigned long pfn = val;
	struct page *p;
17
	struct page *hpage;
18 19
	int err;

20 21
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;
22 23 24 25 26

	if (!pfn_valid(pfn))
		return -ENXIO;

	p = pfn_to_page(pfn);
27
	hpage = compound_head(p);
28 29 30
	/*
	 * This implies unable to support free buddy pages.
	 */
31
	if (!get_page_unless_zero(hpage))
32 33
		return 0;

34 35 36
	if (!hwpoison_filter_enable)
		goto inject;

37 38
	if (!PageLRU(hpage) && !PageHuge(p))
		shake_page(hpage, 0);
39 40 41
	/*
	 * This implies unable to support non-LRU pages.
	 */
42
	if (!PageLRU(hpage) && !PageHuge(p))
43
		goto put_out;
44 45 46 47 48

	/*
	 * do a racy check with elevated page count, to make sure PG_hwpoison
	 * will only be set for the targeted owner (or on a free page).
	 * We temporarily take page lock for try_get_mem_cgroup_from_page().
49
	 * memory_failure() will redo the check reliably inside page lock.
50
	 */
51 52 53
	lock_page(hpage);
	err = hwpoison_filter(hpage);
	unlock_page(hpage);
54
	if (err)
55
		goto put_out;
56

57
inject:
58
	pr_info("Injecting memory failure at pfn %#lx\n", pfn);
59
	return memory_failure(pfn, 18, MF_COUNT_INCREASED);
60 61 62
put_out:
	put_page(hpage);
	return 0;
63 64
}

W
Wu Fengguang 已提交
65 66 67 68 69 70 71 72
static int hwpoison_unpoison(void *data, u64 val)
{
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	return unpoison_memory(val);
}

73
DEFINE_SIMPLE_ATTRIBUTE(hwpoison_fops, NULL, hwpoison_inject, "%lli\n");
W
Wu Fengguang 已提交
74
DEFINE_SIMPLE_ATTRIBUTE(unpoison_fops, NULL, hwpoison_unpoison, "%lli\n");
75 76 77

static void pfn_inject_exit(void)
{
78
	debugfs_remove_recursive(hwpoison_dir);
79 80 81 82
}

static int pfn_inject_init(void)
{
W
Wu Fengguang 已提交
83 84
	struct dentry *dentry;

85 86 87
	hwpoison_dir = debugfs_create_dir("hwpoison", NULL);
	if (hwpoison_dir == NULL)
		return -ENOMEM;
W
Wu Fengguang 已提交
88 89 90 91 92 93

	/*
	 * Note that the below poison/unpoison interfaces do not involve
	 * hardware status change, hence do not require hardware support.
	 * They are mainly for testing hwpoison in software level.
	 */
94
	dentry = debugfs_create_file("corrupt-pfn", 0200, hwpoison_dir,
95
					  NULL, &hwpoison_fops);
W
Wu Fengguang 已提交
96 97 98
	if (!dentry)
		goto fail;

99
	dentry = debugfs_create_file("unpoison-pfn", 0200, hwpoison_dir,
W
Wu Fengguang 已提交
100 101 102 103
				     NULL, &unpoison_fops);
	if (!dentry)
		goto fail;

104 105 106 107 108
	dentry = debugfs_create_u32("corrupt-filter-enable", 0600,
				    hwpoison_dir, &hwpoison_filter_enable);
	if (!dentry)
		goto fail;

W
Wu Fengguang 已提交
109 110 111 112 113 114 115 116 117 118
	dentry = debugfs_create_u32("corrupt-filter-dev-major", 0600,
				    hwpoison_dir, &hwpoison_filter_dev_major);
	if (!dentry)
		goto fail;

	dentry = debugfs_create_u32("corrupt-filter-dev-minor", 0600,
				    hwpoison_dir, &hwpoison_filter_dev_minor);
	if (!dentry)
		goto fail;

W
Wu Fengguang 已提交
119 120 121 122 123 124 125 126 127 128
	dentry = debugfs_create_u64("corrupt-filter-flags-mask", 0600,
				    hwpoison_dir, &hwpoison_filter_flags_mask);
	if (!dentry)
		goto fail;

	dentry = debugfs_create_u64("corrupt-filter-flags-value", 0600,
				    hwpoison_dir, &hwpoison_filter_flags_value);
	if (!dentry)
		goto fail;

A
Andrew Morton 已提交
129
#ifdef CONFIG_MEMCG_SWAP
A
Andi Kleen 已提交
130 131 132 133 134 135
	dentry = debugfs_create_u64("corrupt-filter-memcg", 0600,
				    hwpoison_dir, &hwpoison_filter_memcg);
	if (!dentry)
		goto fail;
#endif

136
	return 0;
W
Wu Fengguang 已提交
137 138 139
fail:
	pfn_inject_exit();
	return -ENOMEM;
140 141 142 143 144
}

module_init(pfn_inject_init);
module_exit(pfn_inject_exit);
MODULE_LICENSE("GPL");