Skip to content

Commit c953cc9

Browse files
djbwdavejiang
authored andcommitted
libnvdimm, pmem: Restore page attributes when clearing errors
Use clear_mce_nospec() to restore WB mode for the kernel linear mapping of a pmem page that was marked 'HWPoison'. A page with 'HWPoison' set has also been marked UC in PAT (page attribute table) via set_mce_nospec() to prevent speculative retrievals of poison. The 'HWPoison' flag is only cleared when overwriting an entire page. Signed-off-by: Dan Williams <dan.j.williams@intel.com> Signed-off-by: Dave Jiang <dave.jiang@intel.com>
1 parent 284ce40 commit c953cc9

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

drivers/nvdimm/pmem.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/hdreg.h>
2121
#include <linux/init.h>
2222
#include <linux/platform_device.h>
23+
#include <linux/set_memory.h>
2324
#include <linux/module.h>
2425
#include <linux/moduleparam.h>
2526
#include <linux/badblocks.h>
@@ -51,6 +52,30 @@ static struct nd_region *to_region(struct pmem_device *pmem)
5152
return to_nd_region(to_dev(pmem)->parent);
5253
}
5354

55+
static void hwpoison_clear(struct pmem_device *pmem,
56+
phys_addr_t phys, unsigned int len)
57+
{
58+
unsigned long pfn_start, pfn_end, pfn;
59+
60+
/* only pmem in the linear map supports HWPoison */
61+
if (is_vmalloc_addr(pmem->virt_addr))
62+
return;
63+
64+
pfn_start = PHYS_PFN(phys);
65+
pfn_end = pfn_start + PHYS_PFN(len);
66+
for (pfn = pfn_start; pfn < pfn_end; pfn++) {
67+
struct page *page = pfn_to_page(pfn);
68+
69+
/*
70+
* Note, no need to hold a get_dev_pagemap() reference
71+
* here since we're in the driver I/O path and
72+
* outstanding I/O requests pin the dev_pagemap.
73+
*/
74+
if (test_and_clear_pmem_poison(page))
75+
clear_mce_nospec(pfn);
76+
}
77+
}
78+
5479
static blk_status_t pmem_clear_poison(struct pmem_device *pmem,
5580
phys_addr_t offset, unsigned int len)
5681
{
@@ -65,6 +90,7 @@ static blk_status_t pmem_clear_poison(struct pmem_device *pmem,
6590
if (cleared < len)
6691
rc = BLK_STS_IOERR;
6792
if (cleared > 0 && cleared / 512) {
93+
hwpoison_clear(pmem, pmem->phys_addr + offset, cleared);
6894
cleared /= 512;
6995
dev_dbg(dev, "%#llx clear %ld sector%s\n",
7096
(unsigned long long) sector, cleared,

drivers/nvdimm/pmem.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
#ifndef __NVDIMM_PMEM_H__
33
#define __NVDIMM_PMEM_H__
4+
#include <linux/page-flags.h>
45
#include <linux/badblocks.h>
56
#include <linux/types.h>
67
#include <linux/pfn_t.h>
@@ -27,4 +28,16 @@ struct pmem_device {
2728

2829
long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
2930
long nr_pages, void **kaddr, pfn_t *pfn);
31+
32+
#ifdef CONFIG_MEMORY_FAILURE
33+
static inline bool test_and_clear_pmem_poison(struct page *page)
34+
{
35+
return TestClearPageHWPoison(page);
36+
}
37+
#else
38+
static inline bool test_and_clear_pmem_poison(struct page *page)
39+
{
40+
return false;
41+
}
42+
#endif
3043
#endif /* __NVDIMM_PMEM_H__ */

0 commit comments

Comments
 (0)