2019-05-19 12:08:20 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2011-03-31 01:57:33 +00:00
|
|
|
/* Inject a hwpoison memory failure on a arbitrary pfn */
|
2009-09-16 09:50:17 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/mm.h>
|
2009-12-16 11:19:59 +00:00
|
|
|
#include <linux/swap.h>
|
|
|
|
#include <linux/pagemap.h>
|
2010-05-28 00:29:22 +00:00
|
|
|
#include <linux/hugetlb.h>
|
2009-12-16 11:19:59 +00:00
|
|
|
#include "internal.h"
|
2009-09-16 09:50:17 +00:00
|
|
|
|
2009-12-16 11:19:58 +00:00
|
|
|
static struct dentry *hwpoison_dir;
|
2009-09-16 09:50:17 +00:00
|
|
|
|
|
|
|
static int hwpoison_inject(void *data, u64 val)
|
|
|
|
{
|
2009-12-16 11:19:59 +00:00
|
|
|
unsigned long pfn = val;
|
|
|
|
struct page *p;
|
2024-04-12 19:35:02 +00:00
|
|
|
struct folio *folio;
|
2009-12-16 11:19:59 +00:00
|
|
|
int err;
|
|
|
|
|
2009-09-16 09:50:17 +00:00
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EPERM;
|
2009-12-16 11:19:59 +00:00
|
|
|
|
|
|
|
if (!pfn_valid(pfn))
|
|
|
|
return -ENXIO;
|
|
|
|
|
|
|
|
p = pfn_to_page(pfn);
|
2024-04-12 19:35:02 +00:00
|
|
|
folio = page_folio(p);
|
2009-12-16 11:19:59 +00:00
|
|
|
|
2013-09-30 20:45:24 +00:00
|
|
|
if (!hwpoison_filter_enable)
|
|
|
|
goto inject;
|
|
|
|
|
2024-04-12 19:35:02 +00:00
|
|
|
shake_folio(folio);
|
2009-12-16 11:19:59 +00:00
|
|
|
/*
|
2022-03-22 21:44:35 +00:00
|
|
|
* This implies unable to support non-LRU pages except free page.
|
2009-12-16 11:19:59 +00:00
|
|
|
*/
|
2024-04-12 19:35:02 +00:00
|
|
|
if (!folio_test_lru(folio) && !folio_test_hugetlb(folio) &&
|
|
|
|
!is_free_buddy_page(p))
|
2020-10-16 03:06:46 +00:00
|
|
|
return 0;
|
2009-12-16 11:19:59 +00:00
|
|
|
|
|
|
|
/*
|
2020-10-16 03:06:46 +00:00
|
|
|
* do a racy check to make sure PG_hwpoison will only be set for
|
|
|
|
* the targeted owner (or on a free page).
|
2011-12-15 18:48:12 +00:00
|
|
|
* memory_failure() will redo the check reliably inside page lock.
|
2009-12-16 11:19:59 +00:00
|
|
|
*/
|
2024-04-12 19:35:02 +00:00
|
|
|
err = hwpoison_filter(&folio->page);
|
2009-12-16 11:19:59 +00:00
|
|
|
if (err)
|
2020-10-16 03:06:46 +00:00
|
|
|
return 0;
|
2009-12-16 11:19:59 +00:00
|
|
|
|
2009-12-16 11:20:01 +00:00
|
|
|
inject:
|
2014-01-21 23:50:56 +00:00
|
|
|
pr_info("Injecting memory failure at pfn %#lx\n", pfn);
|
2022-06-15 09:32:09 +00:00
|
|
|
err = memory_failure(pfn, MF_SW_SIMULATED);
|
2022-03-22 21:44:38 +00:00
|
|
|
return (err == -EOPNOTSUPP) ? 0 : err;
|
2009-09-16 09:50:17 +00:00
|
|
|
}
|
|
|
|
|
2009-12-16 11:19:58 +00:00
|
|
|
static int hwpoison_unpoison(void *data, u64 val)
|
|
|
|
{
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
return unpoison_memory(val);
|
|
|
|
}
|
|
|
|
|
2019-12-01 01:57:35 +00:00
|
|
|
DEFINE_DEBUGFS_ATTRIBUTE(hwpoison_fops, NULL, hwpoison_inject, "%lli\n");
|
|
|
|
DEFINE_DEBUGFS_ATTRIBUTE(unpoison_fops, NULL, hwpoison_unpoison, "%lli\n");
|
2009-09-16 09:50:17 +00:00
|
|
|
|
2022-09-06 09:35:30 +00:00
|
|
|
static void __exit pfn_inject_exit(void)
|
2009-09-16 09:50:17 +00:00
|
|
|
{
|
2022-05-13 03:23:10 +00:00
|
|
|
hwpoison_filter_enable = 0;
|
2014-08-06 23:06:41 +00:00
|
|
|
debugfs_remove_recursive(hwpoison_dir);
|
2009-09-16 09:50:17 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 09:35:30 +00:00
|
|
|
static int __init pfn_inject_init(void)
|
2009-09-16 09:50:17 +00:00
|
|
|
{
|
|
|
|
hwpoison_dir = debugfs_create_dir("hwpoison", NULL);
|
2009-12-16 11:19:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-01-22 15:21:10 +00:00
|
|
|
debugfs_create_file("corrupt-pfn", 0200, hwpoison_dir, NULL,
|
|
|
|
&hwpoison_fops);
|
|
|
|
|
|
|
|
debugfs_create_file("unpoison-pfn", 0200, hwpoison_dir, NULL,
|
|
|
|
&unpoison_fops);
|
|
|
|
|
|
|
|
debugfs_create_u32("corrupt-filter-enable", 0600, hwpoison_dir,
|
|
|
|
&hwpoison_filter_enable);
|
|
|
|
|
|
|
|
debugfs_create_u32("corrupt-filter-dev-major", 0600, hwpoison_dir,
|
|
|
|
&hwpoison_filter_dev_major);
|
|
|
|
|
|
|
|
debugfs_create_u32("corrupt-filter-dev-minor", 0600, hwpoison_dir,
|
|
|
|
&hwpoison_filter_dev_minor);
|
|
|
|
|
|
|
|
debugfs_create_u64("corrupt-filter-flags-mask", 0600, hwpoison_dir,
|
|
|
|
&hwpoison_filter_flags_mask);
|
|
|
|
|
|
|
|
debugfs_create_u64("corrupt-filter-flags-value", 0600, hwpoison_dir,
|
|
|
|
&hwpoison_filter_flags_value);
|
2009-12-16 11:19:59 +00:00
|
|
|
|
2015-09-09 22:35:31 +00:00
|
|
|
#ifdef CONFIG_MEMCG
|
2019-01-22 15:21:10 +00:00
|
|
|
debugfs_create_u64("corrupt-filter-memcg", 0600, hwpoison_dir,
|
|
|
|
&hwpoison_filter_memcg);
|
2009-12-16 11:19:59 +00:00
|
|
|
#endif
|
|
|
|
|
2009-09-16 09:50:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(pfn_inject_init);
|
|
|
|
module_exit(pfn_inject_exit);
|
2024-05-13 19:37:38 +00:00
|
|
|
MODULE_DESCRIPTION("HWPoison pages injector");
|
2009-09-16 09:50:17 +00:00
|
|
|
MODULE_LICENSE("GPL");
|