hwpoison-inject.c 3.22 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
Lucas De Marchi's avatar
Lucas De Marchi committed
2
/* Inject a hwpoison memory failure on a arbitrary pfn */
3 4 5 6
#include <linux/module.h>
#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/mm.h>
7 8
#include <linux/swap.h>
#include <linux/pagemap.h>
9
#include <linux/hugetlb.h>
10
#include "internal.h"
11

12
static struct dentry *hwpoison_dir;
13 14 15

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

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

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

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

35 36 37
	if (!hwpoison_filter_enable)
		goto inject;

38
	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

	/*
	 * 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).
48
	 * memory_failure() will redo the check reliably inside page lock.
49
	 */
50
	err = hwpoison_filter(hpage);
51
	if (err)
52
		goto put_out;
53

54
inject:
55
	pr_info("Injecting memory failure at pfn %#lx\n", pfn);
56
	return memory_failure(pfn, MF_COUNT_INCREASED);
57
put_out:
58
	put_hwpoison_page(p);
59
	return 0;
60 61
}

62 63 64 65 66 67 68 69
static int hwpoison_unpoison(void *data, u64 val)
{
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	return unpoison_memory(val);
}

70
DEFINE_SIMPLE_ATTRIBUTE(hwpoison_fops, NULL, hwpoison_inject, "%lli\n");
71
DEFINE_SIMPLE_ATTRIBUTE(unpoison_fops, NULL, hwpoison_unpoison, "%lli\n");
72 73 74

static void pfn_inject_exit(void)
{
75
	debugfs_remove_recursive(hwpoison_dir);
76 77 78 79
}

static int pfn_inject_init(void)
{
80 81
	struct dentry *dentry;

82 83 84
	hwpoison_dir = debugfs_create_dir("hwpoison", NULL);
	if (hwpoison_dir == NULL)
		return -ENOMEM;
85 86 87 88 89 90

	/*
	 * 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.
	 */
91
	dentry = debugfs_create_file("corrupt-pfn", 0200, hwpoison_dir,
92
					  NULL, &hwpoison_fops);
93 94 95
	if (!dentry)
		goto fail;

96
	dentry = debugfs_create_file("unpoison-pfn", 0200, hwpoison_dir,
97 98 99 100
				     NULL, &unpoison_fops);
	if (!dentry)
		goto fail;

101 102 103 104 105
	dentry = debugfs_create_u32("corrupt-filter-enable", 0600,
				    hwpoison_dir, &hwpoison_filter_enable);
	if (!dentry)
		goto fail;

106 107 108 109 110 111 112 113 114 115
	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;

116 117 118 119 120 121 122 123 124 125
	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;

126
#ifdef CONFIG_MEMCG
127 128 129 130 131 132
	dentry = debugfs_create_u64("corrupt-filter-memcg", 0600,
				    hwpoison_dir, &hwpoison_filter_memcg);
	if (!dentry)
		goto fail;
#endif

133
	return 0;
134 135 136
fail:
	pfn_inject_exit();
	return -ENOMEM;
137 138 139 140 141
}

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