Skip to contentSkip to conversation

DAX poison recovery

PATCH Superseded Jane Chu 4 participants 35 messages Jan 28, 2022 – Feb 6, 2022

Subsystems (10)
x86 VFS Device mapper x86 MM +6 more subsystems

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Jane Chu

author
#0 lore raw

v5 cover DAX poison recovery

In this series, dax recovery code path is independent of that of normal write. Competing dax recovery threads are serialized, racing read threads are guaranteed not overlapping with the recovery process.

In this phase, the recovery granularity is page, future patch will explore recovery in finer granularity.

Change from v4:
Fixed build errors reported by kernel test robot
Change from v3:
Rebased to v5.17-rc1-81-g0280e3c58f92

v4:
https://lore.kernel.org/lkml/[email protected]/T/
v3:
https://lkml.org/lkml/2022/1/11/900
v2:
https://lore.kernel.org/all/[email protected]/
Disussions about marking poisoned page as 'np':
https://lore.kernel.org/all/CAPcyv4hrXPb1tASBZUg-GgdVs0OOFKXMXLiHmktg_kFi7YBMyQ@mail.gmail.com/

Jane Chu (7):
mce: fix set_mce_nospec to always unmap the whole page
dax: introduce dax device flag DAXDEV_RECOVERY
dm: make dm aware of target's DAXDEV_RECOVERY capability
dax: add dax_recovery_write to dax_op and dm target type
pmem: add pmem_recovery_write() dax op
dax: add recovery_write to dax_iomap_iter in failure path
pmem: fix pmem_do_write() avoid writing to 'np' page

 arch/x86/include/asm/set_memory.h | 17 ++----
 arch/x86/kernel/cpu/mce/core.c    |  6 +-
 arch/x86/mm/pat/set_memory.c      |  8 ++-
 drivers/dax/super.c               | 41 +++++++++++++
 drivers/md/dm-linear.c            | 12 ++++
 drivers/md/dm-log-writes.c        | 12 ++++
 drivers/md/dm-stripe.c            | 13 ++++
 drivers/md/dm-table.c             | 33 +++++++++++
 drivers/md/dm.c                   | 27 +++++++++
 drivers/nvdimm/pmem.c             | 99 ++++++++++++++++++++++++++++---
 drivers/nvdimm/pmem.h             |  1 +
 fs/dax.c                          | 23 ++++++-
 include/linux/dax.h               | 30 ++++++++++
 include/linux/device-mapper.h     |  9 +++
 include/linux/set_memory.h        |  2 +-
 15 files changed, 306 insertions(+), 27 deletions(-)
Signature · 1 line
2.18.4
author
#1 lore raw

PATCH v5 1/7 mce: fix set_mce_nospec to always unmap the whole page

Mark poisoned page as not present, and to reverse the 'np' effect, restate the _PAGE_PRESENT bit. Please refer to discussions here for reason behind the decision.
https://lore.kernel.org/all/CAPcyv4hrXPb1tASBZUg-GgdVs0OOFKXMXLiHmktg_kFi7YBMyQ@mail.gmail.com/

Fixes 284ce4011ba6 ("x86/memory_failure: Introduce {set, clear}_mce_nospec()")
---
 arch/x86/include/asm/set_memory.h | 17 +++++------------
 arch/x86/kernel/cpu/mce/core.c    |  6 +++---
 arch/x86/mm/pat/set_memory.c      |  8 +++++++-
 include/linux/set_memory.h        |  2 +-
 4 files changed, 16 insertions(+), 17 deletions(-)
arch/x86/include/asm/set_memory.h +5 -12
index ff0f2d90338a..aef6677da291 100644
@@ -50,6 +50,7 @@ int set_memory_decrypted(unsigned long addr, int numpages);
 int set_memory_np_noalias(unsigned long addr, int numpages);
 int set_memory_nonglobal(unsigned long addr, int numpages);
 int set_memory_global(unsigned long addr, int numpages);
+int _set_memory_present(unsigned long addr, int numpages);
 
 int set_pages_array_uc(struct page **pages, int addrinarray);
 int set_pages_array_wc(struct page **pages, int addrinarray);
@@ -89,13 +90,8 @@ void notify_range_enc_status_changed(unsigned long vaddr, int npages, bool enc);
 extern int kernel_set_to_readonly;
 
 #ifdef CONFIG_X86_64
-/*
- * Prevent speculative access to the page by either unmapping
- * it (if we do not require access to any part of the page) or
- * marking it uncacheable (if we want to try to retrieve data
- * from non-poisoned lines in the page).
- */
-static inline int set_mce_nospec(unsigned long pfn, bool unmap)
+/* Prevent speculative access to a page by marking it not-present */
+static inline int set_mce_nospec(unsigned long pfn)
 {
 	unsigned long decoy_addr;
 	int rc;
@@ -117,10 +113,7 @@ static inline int set_mce_nospec(unsigned long pfn, bool unmap)
 	 */
 	decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63));
 
-	if (unmap)
-		rc = set_memory_np(decoy_addr, 1);
-	else
-		rc = set_memory_uc(decoy_addr, 1);
+	rc = set_memory_np(decoy_addr, 1);
 	if (rc)
 		pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn);
 	return rc;
@@ -130,7 +123,7 @@ static inline int set_mce_nospec(unsigned long pfn, bool unmap)
 /* Restore full speculative operation to the pfn. */
 static inline int clear_mce_nospec(unsigned long pfn)
 {
-	return set_memory_wb((unsigned long) pfn_to_kaddr(pfn), 1);
+	return _set_memory_present((unsigned long) pfn_to_kaddr(pfn), 1);
 }
 #define clear_mce_nospec clear_mce_nospec
 #else
arch/x86/kernel/cpu/mce/core.c +3 -3
index 5818b837fd4d..8d12739f283d 100644
@@ -613,7 +613,7 @@ static int uc_decode_notifier(struct notifier_block *nb, unsigned long val,
 
 	pfn = mce->addr >> PAGE_SHIFT;
 	if (!memory_failure(pfn, 0)) {
-		set_mce_nospec(pfn, whole_page(mce));
+		set_mce_nospec(pfn);
 		mce->kflags |= MCE_HANDLED_UC;
 	}
 
@@ -1297,7 +1297,7 @@ static void kill_me_maybe(struct callback_head *cb)
 
 	ret = memory_failure(p->mce_addr >> PAGE_SHIFT, flags);
 	if (!ret) {
-		set_mce_nospec(p->mce_addr >> PAGE_SHIFT, p->mce_whole_page);
+		set_mce_nospec(p->mce_addr >> PAGE_SHIFT);
 		sync_core();
 		return;
 	}
@@ -1321,7 +1321,7 @@ static void kill_me_never(struct callback_head *cb)
 	p->mce_count = 0;
 	pr_err("Kernel accessed poison in user space at %llx\n", p->mce_addr);
 	if (!memory_failure(p->mce_addr >> PAGE_SHIFT, 0))
-		set_mce_nospec(p->mce_addr >> PAGE_SHIFT, p->mce_whole_page);
+		set_mce_nospec(p->mce_addr >> PAGE_SHIFT);
 }
 
 static void queue_task_work(struct mce *m, char *msg, void (*func)(struct callback_head *))
arch/x86/mm/pat/set_memory.c +7 -1
index b4072115c8ef..68d84c8bd977 100644
@@ -1816,7 +1816,7 @@ static inline int cpa_clear_pages_array(struct page **pages, int numpages,
 }
 
 /*
- * _set_memory_prot is an internal helper for callers that have been passed
+ * __set_memory_prot is an internal helper for callers that have been passed
  * a pgprot_t value from upper layers and a reservation has already been taken.
  * If you want to set the pgprot to a specific page protocol, use the
  * set_memory_xx() functions.
@@ -1983,6 +1983,12 @@ int set_memory_global(unsigned long addr, int numpages)
 				    __pgprot(_PAGE_GLOBAL), 0);
 }
 
+int _set_memory_present(unsigned long addr, int numpages)
+{
+	return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
+}
+EXPORT_SYMBOL_GPL(_set_memory_present);
+
 /*
  * __set_memory_enc_pgtable() is used for the hypervisors that get
  * informed about "encryption" status via page tables.
include/linux/set_memory.h +1 -1
index f36be5166c19..9ad898d40e7e 100644
@@ -43,7 +43,7 @@ static inline bool can_set_direct_map(void)
 #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */
 
 #ifndef set_mce_nospec
-static inline int set_mce_nospec(unsigned long pfn, bool unmap)
+static inline int set_mce_nospec(unsigned long pfn)
 {
 	return 0;
 }
Signature · 1 line
2.18.4
#2 lore raw

Re: [PATCH v5 1/7] mce: fix set_mce_nospec to always unmap the whole page

Quoted 17 lines
arch/x86/include/asm/set_memory.h +2 -4
+static inline int set_mce_nospec(unsigned long pfn)
 {
 	unsigned long decoy_addr;
 	int rc;
@@ -117,10 +113,7 @@ static inline int set_mce_nospec(unsigned long pfn, bool unmap)
 	 */
 	decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63));
 
-	if (unmap)
-		rc = set_memory_np(decoy_addr, 1);
-	else
-		rc = set_memory_uc(decoy_addr, 1);
+	rc = set_memory_np(decoy_addr, 1);
 	if (rc)
 		pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn);
 	return rc;

arch/x86/include/asm/set_memory.h static inline int set_mce_nospec(unsigned long pfn, bool unmap) · line 127

Quoted 6 lines
arch/x86/include/asm/set_memory.h +1 -1
@@ -130,7 +123,7 @@ static inline int set_mce_nospec(unsigned long pfn, bool unmap)
 /* Restore full speculative operation to the pfn. */
 static inline int clear_mce_nospec(unsigned long pfn)
 {
-	return set_memory_wb((unsigned long) pfn_to_kaddr(pfn), 1);
+	return _set_memory_present((unsigned long) pfn_to_kaddr(pfn), 1);
 }

Wouldn't it make more sense to move these helpers out of line rather than exporting _set_memory_present?

arch/x86/mm/pat/set_memory.c static inline int cpa_clear_pages_array(struct page **pages, int numpages, · line 1819

Quoted 3 lines
arch/x86/mm/pat/set_memory.c +1 -1
 /*
- * _set_memory_prot is an internal helper for callers that have been passed
+ * __set_memory_prot is an internal helper for callers that have been passed

This looks unrelated to the patch.

author
#3 lore raw

Re: [PATCH v5 1/7] mce: fix set_mce_nospec to always unmap the whole page

Quoted 18 lines · Christoph Hellwig
Quoted 18 lines
arch/x86/include/asm/set_memory.h +2 -4
+static inline int set_mce_nospec(unsigned long pfn)
  {
  	unsigned long decoy_addr;
  	int rc;
@@ -117,10 +113,7 @@ static inline int set_mce_nospec(unsigned long pfn, bool unmap)
  	 */
  	decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63));
 
-	if (unmap)
-		rc = set_memory_np(decoy_addr, 1);
-	else
-		rc = set_memory_uc(decoy_addr, 1);
+	rc = set_memory_np(decoy_addr, 1);
  	if (rc)
  		pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn);
  	return rc;
@@ -130,7 +123,7 @@ static inline int set_mce_nospec(unsigned long pfn, bool unmap)
  /* Restore full speculative operation to the pfn. */
Quoted 8 lines

arch/x86/include/asm/set_memory.h static inline int set_mce_nospec(unsigned long pfn, bool unmap) · line 127

Quoted 5 lines
arch/x86/include/asm/set_memory.h +1 -1
  static inline int clear_mce_nospec(unsigned long pfn)
  {
-	return set_memory_wb((unsigned long) pfn_to_kaddr(pfn), 1);
+	return _set_memory_present((unsigned long) pfn_to_kaddr(pfn), 1);
  }

Wouldn't it make more sense to move these helpers out of line rather than exporting _set_memory_present?

Do you mean to move
return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);
into clear_mce_nospec() for the x86 arch and get rid of _set_memory_present? If so, sure I'll do that.

Quoted 6 lines

arch/x86/mm/pat/set_memory.c static inline int cpa_clear_pages_array(struct page **pages, int numpages, · line 1819

Quoted 4 lines
arch/x86/mm/pat/set_memory.c +1 -1
  /*
- * _set_memory_prot is an internal helper for callers that have been passed
+ * __set_memory_prot is an internal helper for callers that have been passed

This looks unrelated to the patch.

My bad, will remove the remnant.

thanks!
-jane

author
#4 lore raw

Re: [PATCH v5 1/7] mce: fix set_mce_nospec to always unmap the whole page

Quoted 28 lines · Jane Chu
Quoted 18 lines · Christoph Hellwig
Quoted 18 lines
arch/x86/include/asm/set_memory.h +2 -4
+static inline int set_mce_nospec(unsigned long pfn)
   {
   	unsigned long decoy_addr;
   	int rc;
@@ -117,10 +113,7 @@ static inline int set_mce_nospec(unsigned long pfn, bool unmap)
   	 */
   	decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63));
 
-	if (unmap)
-		rc = set_memory_np(decoy_addr, 1);
-	else
-		rc = set_memory_uc(decoy_addr, 1);
+	rc = set_memory_np(decoy_addr, 1);
   	if (rc)
   		pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn);
   	return rc;
@@ -130,7 +123,7 @@ static inline int set_mce_nospec(unsigned long pfn, bool unmap)
   /* Restore full speculative operation to the pfn. */
Quoted 8 lines

arch/x86/include/asm/set_memory.h static inline int set_mce_nospec(unsigned long pfn, bool unmap) · line 127

Quoted 5 lines
arch/x86/include/asm/set_memory.h +1 -1
   static inline int clear_mce_nospec(unsigned long pfn)
   {
-	return set_memory_wb((unsigned long) pfn_to_kaddr(pfn), 1);
+	return _set_memory_present((unsigned long) pfn_to_kaddr(pfn), 1);
   }

Wouldn't it make more sense to move these helpers out of line rather than exporting _set_memory_present?

Quoted 4 lines

Do you mean to move

    return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);

into clear_mce_nospec() for the x86 arch and get rid of _set_memory_present? If so, sure I'll do that.

Looks like I can't do that. It's either exporting _set_memory_present(), or exporting change_page_attr_set(). Perhaps the former is more conventional?

-jane

#5 lore raw

Re: [PATCH v5 1/7] mce: fix set_mce_nospec to always unmap the whole page

Quoted 4 lines · Jane Chu
Quoted 3 lines · Jane Chu
Quoted 2 lines

Wouldn't it make more sense to move these helpers out of line rather than exporting _set_memory_present?

Quoted 8 lines
Quoted 4 lines

Do you mean to move

    return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);

into clear_mce_nospec() for the x86 arch and get rid of _set_memory_present? If so, sure I'll do that.

Looks like I can't do that. It's either exporting _set_memory_present(), or exporting change_page_attr_set(). Perhaps the former is more conventional?

These helpers above means set_mce_nospec and clear_mce_nospec. If they are moved to normal functions instead of inlines, there is no need to export the internals at all.

#6 lore raw

Re: [PATCH v5 1/7] mce: fix set_mce_nospec to always unmap the whole page

Quoted 11 lines · Christoph Hellwig
Quoted 9 lines · Jane Chu
Quoted 7 lines · Jane Chu
Quoted 2 lines

Wouldn't it make more sense to move these helpers out of line rather than exporting _set_memory_present?

Do you mean to move

    return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);

into clear_mce_nospec() for the x86 arch and get rid of _set_memory_present? If so, sure I'll do that.

Quoted 7 lines
Quoted 3 lines

Looks like I can't do that. It's either exporting _set_memory_present(), or exporting change_page_attr_set(). Perhaps the former is more conventional?

These helpers above means set_mce_nospec and clear_mce_nospec. If they are moved to normal functions instead of inlines, there is no need to export the internals at all.

Agree, {set,clear}_mce_nospec() can just move to arch/x86/mm/pat/set_memory.c.

author
#7 lore raw

Re: [PATCH v5 1/7] mce: fix set_mce_nospec to always unmap the whole page

Quoted 16 lines · Dan Williams
Quoted 15 lines · Christoph Hellwig
Quoted 12 lines · Jane Chu
Quoted 7 lines · Jane Chu
Quoted 2 lines

Wouldn't it make more sense to move these helpers out of line rather than exporting _set_memory_present?

Do you mean to move

     return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT), 0);

into clear_mce_nospec() for the x86 arch and get rid of _set_memory_present? If so, sure I'll do that.

Looks like I can't do that. It's either exporting _set_memory_present(), or exporting change_page_attr_set(). Perhaps the former is more conventional?

Quoted 5 lines
Quoted 3 lines

These helpers above means set_mce_nospec and clear_mce_nospec. If they are moved to normal functions instead of inlines, there is no need to export the internals at all.

Agree, {set,clear}_mce_nospec() can just move to arch/x86/mm/pat/set_memory.c.

Got it, will do.

thanks!
-jane

author
#8 lore raw

PATCH v5 2/7 dax: introduce dax device flag DAXDEV_RECOVERY

In reply to Jane Chu

Introduce dax device flag DAXDEV_RECOVERY to indicate a device that is capable of recoverying from media poison. For MD raid DAX devices, the capability is allowed for partial device as oppose to the entire device. And the final poison detection and repair rely on the provisioning base drivers.

---
 drivers/dax/super.c   | 24 ++++++++++++++++++++++++
 drivers/nvdimm/pmem.c |  1 +
 include/linux/dax.h   | 14 ++++++++++++++
 3 files changed, 39 insertions(+)
drivers/dax/super.c +24 -0
index e3029389d809..f4f607d9698b 100644
@@ -109,6 +109,8 @@ enum dax_device_flags {
 	DAXDEV_NOCACHE,
 	/* handle CPU fetch exceptions during reads */
 	DAXDEV_NOMC,
+	/* flag to indicate device capable of poison recovery */
+	DAXDEV_RECOVERY,
 };
 
 /**
@@ -311,6 +313,28 @@ static void dax_destroy_inode(struct inode *inode)
 			"kill_dax() must be called before final iput()\n");
 }
 
+void set_dax_recovery(struct dax_device *dax_dev)
+{
+	set_bit(DAXDEV_RECOVERY, &dax_dev->flags);
+}
+EXPORT_SYMBOL_GPL(set_dax_recovery);
+
+bool dax_recovery_capable(struct dax_device *dax_dev)
+{
+	return test_bit(DAXDEV_RECOVERY, &dax_dev->flags);
+}
+EXPORT_SYMBOL_GPL(dax_recovery_capable);
+
+int dax_prep_recovery(struct dax_device *dax_dev, void **kaddr)
+{
+	if (dax_recovery_capable(dax_dev)) {
+		set_bit(DAXDEV_RECOVERY, (unsigned long *)kaddr);
+		return 0;
+	}
+	return -EINVAL;
+}
+EXPORT_SYMBOL_GPL(dax_prep_recovery);
+
 static const struct super_operations dax_sops = {
 	.statfs = simple_statfs,
 	.alloc_inode = dax_alloc_inode,
drivers/nvdimm/pmem.c +1 -0
index 58d95242a836..e8823813a8df 100644
@@ -487,6 +487,7 @@ static int pmem_attach_disk(struct device *dev,
 	if (rc)
 		goto out_cleanup_dax;
 	dax_write_cache(dax_dev, nvdimm_has_cache(nd_region));
+	set_dax_recovery(dax_dev);
 	pmem->dax_dev = dax_dev;
 
 	rc = device_add_disk(dev, disk, pmem_attribute_groups);
include/linux/dax.h +14 -0
index 9fc5f99a0ae2..2fc776653c6e 100644
@@ -40,6 +40,9 @@ void dax_write_cache(struct dax_device *dax_dev, bool wc);
 bool dax_write_cache_enabled(struct dax_device *dax_dev);
 bool dax_synchronous(struct dax_device *dax_dev);
 void set_dax_synchronous(struct dax_device *dax_dev);
+void set_dax_recovery(struct dax_device *dax_dev);
+bool dax_recovery_capable(struct dax_device *dax_dev);
+int dax_prep_recovery(struct dax_device *dax_dev, void **kaddr);
 /*
  * Check if given mapping is supported by the file / underlying device.
  */
@@ -87,6 +90,17 @@ static inline bool daxdev_mapping_supported(struct vm_area_struct *vma,
 {
 	return !(vma->vm_flags & VM_SYNC);
 }
+static inline void set_dax_recovery(struct dax_device *dax_dev)
+{
+}
+static inline bool dax_recovery_capable(struct dax_device *dax_dev)
+{
+	return false;
+}
+static inline int dax_prep_recovery(struct dax_device *dax_dev, void **kaddr)
+{
+	return -EINVAL;
+}
 #endif
 
 void set_dax_nocache(struct dax_device *dax_dev);
Signature · 1 line
2.18.4
#9 lore raw

Re: [PATCH v5 2/7] dax: introduce dax device flag DAXDEV_RECOVERY

drivers/dax/super.c static void dax_destroy_inode(struct inode *inode) · line 334

Quoted 7 lines · Jane Chu
drivers/dax/super.c +7 -0
+int dax_prep_recovery(struct dax_device *dax_dev, void **kaddr)
+{
+	if (dax_recovery_capable(dax_dev)) {
+		set_bit(DAXDEV_RECOVERY, (unsigned long *)kaddr);
+		return 0;
+	}
+	return -EINVAL;

Setting a random bit on a passed in memory address looks a little dangerous to me.

Also I'd return early for the EINVAL case to make the flow a little more clear.

author
#10 lore raw

Re: [PATCH v5 2/7] dax: introduce dax device flag DAXDEV_RECOVERY

Quoted 11 lines · Christoph Hellwig

drivers/dax/super.c static void dax_destroy_inode(struct inode *inode) · line 334

Quoted 7 lines · Jane Chu
drivers/dax/super.c +7 -0
+int dax_prep_recovery(struct dax_device *dax_dev, void **kaddr)
+{
+	if (dax_recovery_capable(dax_dev)) {
+		set_bit(DAXDEV_RECOVERY, (unsigned long *)kaddr);
+		return 0;
+	}
+	return -EINVAL;

Setting a random bit on a passed in memory address looks a little dangerous to me.

Yeah, I see. Would you suggest a way to pass the indication from dax_iomap_iter to dax_direct_access that the caller intends the callee to ignore poison in the range because the caller intends to do recovery_write? We tried adding a flag to dax_direct_access, and that wasn't liked if I recall.

Quoted 3 lines

Also I'd return early for the EINVAL case to make the flow a little more clear.

Agreed, will do.

thanks!
-jane

#11 lore raw

Re: [PATCH v5 2/7] dax: introduce dax device flag DAXDEV_RECOVERY

Quoted 5 lines · Jane Chu

Yeah, I see. Would you suggest a way to pass the indication from dax_iomap_iter to dax_direct_access that the caller intends the callee to ignore poison in the range because the caller intends to do recovery_write? We tried adding a flag to dax_direct_access, and that wasn't liked if I recall.

To me a flag seems cleaner than this magic, but let's wait for Dan to chime in.

#12 lore raw

Re: [PATCH v5 2/7] dax: introduce dax device flag DAXDEV_RECOVERY

Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Jane Chu

Yeah, I see. Would you suggest a way to pass the indication from dax_iomap_iter to dax_direct_access that the caller intends the callee to ignore poison in the range because the caller intends to do recovery_write? We tried adding a flag to dax_direct_access, and that wasn't liked if I recall.

To me a flag seems cleaner than this magic, but let's wait for Dan to chime in.

So back in November I suggested modifying the kaddr, mainly to avoid touching all the dax_direct_access() call sites [1]. However, now seeing the code and Chrisoph's comment I think this either wants type safety (e.g. 'dax_addr_t *'), or just add a new flag. Given both of those options involve touching all dax_direct_access() call sites and a @flags operation is more extensible if any other scenarios arrive lets go ahead and plumb a flag and skip the magic.

#13 lore raw

Re: [PATCH v5 2/7] dax: introduce dax device flag DAXDEV_RECOVERY

Quoted 13 lines · Dan Williams
Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Jane Chu

Yeah, I see. Would you suggest a way to pass the indication from dax_iomap_iter to dax_direct_access that the caller intends the callee to ignore poison in the range because the caller intends to do recovery_write? We tried adding a flag to dax_direct_access, and that wasn't liked if I recall.

To me a flag seems cleaner than this magic, but let's wait for Dan to chime in.

Quoted 7 lines

So back in November I suggested modifying the kaddr, mainly to avoid touching all the dax_direct_access() call sites [1]. However, now seeing the code and Chrisoph's comment I think this either wants type safety (e.g. 'dax_addr_t *'), or just add a new flag. Given both of those options involve touching all dax_direct_access() call sites and a @flags operation is more extensible if any other scenarios arrive lets go ahead and plumb a flag and skip the magic.

Just to be clear we are talking about a flow like:

        flags = 0;
        rc = dax_direct_access(..., &kaddr, flags, ...);
        if (unlikely(rc)) {
                flags |= DAX_RECOVERY;
                dax_direct_access(..., &kaddr, flags, ...);
                return dax_recovery_{read,write}(..., kaddr, ...);
        }
        return copy_{mc_to_iter,from_iter_flushcache}(...);
author
#14 lore raw

Re: [PATCH v5 2/7] dax: introduce dax device flag DAXDEV_RECOVERY

Quoted 24 lines · Dan Williams
Quoted 13 lines · Dan Williams
Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Jane Chu

Yeah, I see. Would you suggest a way to pass the indication from dax_iomap_iter to dax_direct_access that the caller intends the callee to ignore poison in the range because the caller intends to do recovery_write? We tried adding a flag to dax_direct_access, and that wasn't liked if I recall.

To me a flag seems cleaner than this magic, but let's wait for Dan to chime in.

Quoted 7 lines

So back in November I suggested modifying the kaddr, mainly to avoid touching all the dax_direct_access() call sites [1]. However, now seeing the code and Chrisoph's comment I think this either wants type safety (e.g. 'dax_addr_t *'), or just add a new flag. Given both of those options involve touching all dax_direct_access() call sites and a @flags operation is more extensible if any other scenarios arrive lets go ahead and plumb a flag and skip the magic.

Just to be clear we are talking about a flow like:

Quoted 8 lines
         flags = 0;
         rc = dax_direct_access(..., &kaddr, flags, ...);
         if (unlikely(rc)) {
                 flags |= DAX_RECOVERY;
                 dax_direct_access(..., &kaddr, flags, ...);
                 return dax_recovery_{read,write}(..., kaddr, ...);
         }
         return copy_{mc_to_iter,from_iter_flushcache}(...);

Okay, will go with a flag.

thanks!
-jane

author
#15 lore raw

PATCH v5 3/7 dm: make dm aware of target's DAXDEV_RECOVERY capability

In reply to Jane Chu

If one of the MD raid participating target dax device supports DAXDEV_RECOVERY, then it'll be declared on the whole that the MD device is capable of DAXDEV_RECOVERY.
And only when the recovery process reaches to the target driver, it becomes deterministic whether a certain dax address range maybe recovered, or not.

---
 drivers/md/dm-table.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
drivers/md/dm-table.c +33 -0
index e43096cfe9e2..8af8a81b6172 100644
@@ -844,6 +844,36 @@ static bool dm_table_supports_dax(struct dm_table *t,
 	return true;
 }
 
+/* Check whether device is capable of dax poison recovery */
+static int device_poison_recovery_capable(struct dm_target *ti,
+	struct dm_dev *dev, sector_t start, sector_t len, void *data)
+{
+	if (!dev->dax_dev)
+		return false;
+	return dax_recovery_capable(dev->dax_dev);
+}
+
+static bool dm_table_supports_poison_recovery(struct dm_table *t,
+	iterate_devices_callout_fn func)
+{
+	struct dm_target *ti;
+	unsigned int i;
+
+	/* Check if any DAX target supports poison recovery */
+	for (i = 0; i < dm_table_get_num_targets(t); i++) {
+		ti = dm_table_get_target(t, i);
+
+		if (!ti->type->direct_access)
+			return false;
+
+		if (ti->type->iterate_devices &&
+		    ti->type->iterate_devices(ti, func, NULL))
+			return true;
+	}
+
+	return false;
+}
+
 static int device_is_rq_stackable(struct dm_target *ti, struct dm_dev *dev,
 				  sector_t start, sector_t len, void *data)
 {
@@ -2014,6 +2044,9 @@ int dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
 		blk_queue_flag_set(QUEUE_FLAG_DAX, q);
 		if (dm_table_supports_dax(t, device_not_dax_synchronous_capable))
 			set_dax_synchronous(t->md->dax_dev);
+		if (dm_table_supports_poison_recovery(t,
+					device_poison_recovery_capable))
+			set_dax_recovery(t->md->dax_dev);
 	}
 	else
 		blk_queue_flag_clear(QUEUE_FLAG_DAX, q);
Signature · 1 line
2.18.4
#16 lore raw

Re: [PATCH v5 3/7] dm: make dm aware of target's DAXDEV_RECOVERY capability

Quoted 18 lines · Jane Chu

If one of the MD raid participating target dax device supports DAXDEV_RECOVERY, then it'll be declared on the whole that the MD device is capable of DAXDEV_RECOVERY.
And only when the recovery process reaches to the target driver, it becomes deterministic whether a certain dax address range maybe recovered, or not.

---
 drivers/md/dm-table.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
drivers/md/dm-table.c +0 -0
index e43096cfe9e2..8af8a81b6172 100644

drivers/md/dm-table.c static bool dm_table_supports_dax(struct dm_table *t, · line 853

Quoted 10 lines
drivers/md/dm-table.c +7 -0
@@ -844,6 +844,36 @@ static bool dm_table_supports_dax(struct dm_table *t,
        return true;
 }
 
+/* Check whether device is capable of dax poison recovery */
+static int device_poison_recovery_capable(struct dm_target *ti,
+       struct dm_dev *dev, sector_t start, sector_t len, void *data)
+{
+       if (!dev->dax_dev)
+               return false;
+       return dax_recovery_capable(dev->dax_dev);

Hmm it's not clear to me that dax_recovery_capable is necessary. If a dax_dev does not support recovery it can simply fail the dax_direct_access() call with the DAX_RECOVERY flag set.

So all DM needs to worry about is passing the new @flags parameter through the stack.

author
#17 lore raw

Re: [PATCH v5 3/7] dm: make dm aware of target's DAXDEV_RECOVERY capability

Quoted 34 lines · Dan Williams
Quoted 18 lines · Jane Chu

If one of the MD raid participating target dax device supports DAXDEV_RECOVERY, then it'll be declared on the whole that the MD device is capable of DAXDEV_RECOVERY.
And only when the recovery process reaches to the target driver, it becomes deterministic whether a certain dax address range maybe recovered, or not.

---
  drivers/md/dm-table.c | 33 +++++++++++++++++++++++++++++++++
  1 file changed, 33 insertions(+)
drivers/md/dm-table.c +0 -0
index e43096cfe9e2..8af8a81b6172 100644

drivers/md/dm-table.c static bool dm_table_supports_dax(struct dm_table *t, · line 853

Quoted 10 lines
drivers/md/dm-table.c +7 -0
@@ -844,6 +844,36 @@ static bool dm_table_supports_dax(struct dm_table *t,
         return true;
  }
 
+/* Check whether device is capable of dax poison recovery */
+static int device_poison_recovery_capable(struct dm_target *ti,
+       struct dm_dev *dev, sector_t start, sector_t len, void *data)
+{
+       if (!dev->dax_dev)
+               return false;
+       return dax_recovery_capable(dev->dax_dev);

Hmm it's not clear to me that dax_recovery_capable is necessary. If a dax_dev does not support recovery it can simply fail the dax_direct_access() call with the DAX_RECOVERY flag set.

Quoted 2 lines

So all DM needs to worry about is passing the new @flags parameter through the stack.

Yeah, given your idea about adding the .recovery_write to pgmap_ops, it wouldn't be needed.

thanks,
-jane

author
#18 lore raw

PATCH v5 4/7 dax: add dax_recovery_write to dax_op and dm target type

In reply to Jane Chu

dax_recovery_write() dax op is only required for DAX device that export DAXDEV_RECOVERY indicating its capability to recover from poisons.

DM may be nested, if part of the base dax devices forming a DM device support dax recovery, the DM device is marked with such capability.

---
 drivers/dax/super.c           | 17 +++++++++++++++++
 drivers/md/dm-linear.c        | 12 ++++++++++++
 drivers/md/dm-log-writes.c    | 12 ++++++++++++
 drivers/md/dm-stripe.c        | 13 +++++++++++++
 drivers/md/dm.c               | 27 +++++++++++++++++++++++++++
 drivers/nvdimm/pmem.c         |  7 +++++++
 include/linux/dax.h           | 16 ++++++++++++++++
 include/linux/device-mapper.h |  9 +++++++++
 8 files changed, 113 insertions(+)
drivers/dax/super.c +17 -0
index f4f607d9698b..a136fa6b3e36 100644
@@ -195,6 +195,23 @@ int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
 }
 EXPORT_SYMBOL_GPL(dax_zero_page_range);
 
+size_t dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
+			void *addr, size_t bytes, struct iov_iter *i)
+{
+	if (!dax_recovery_capable(dax_dev) || !dax_dev->ops->recovery_write)
+		return (size_t)-EOPNOTSUPP;
+	return dax_dev->ops->recovery_write(dax_dev, pgoff, addr, bytes, i);
+}
+EXPORT_SYMBOL_GPL(dax_recovery_write);
+
+bool dax_recovery_started(struct dax_device *dax_dev, void **kaddr)
+{
+	if (!kaddr || !dax_recovery_capable(dax_dev))
+		return false;
+	return test_bit(DAXDEV_RECOVERY, (unsigned long *)kaddr);
+}
+EXPORT_SYMBOL_GPL(dax_recovery_started);
+
 #ifdef CONFIG_ARCH_HAS_PMEM_API
 void arch_wb_cache_pmem(void *addr, size_t size);
 void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
drivers/md/dm-linear.c +12 -0
index 1b97a11d7151..831c565bf024 100644
@@ -188,9 +188,20 @@ static int linear_dax_zero_page_range(struct dm_target *ti, pgoff_t pgoff,
 	return dax_zero_page_range(dax_dev, pgoff, nr_pages);
 }
 
+static size_t linear_dax_recovery_write(struct dm_target *ti, pgoff_t pgoff,
+	void *addr, size_t bytes, struct iov_iter *i)
+{
+	struct dax_device *dax_dev = linear_dax_pgoff(ti, &pgoff);
+
+	if (!dax_recovery_capable(dax_dev))
+		return (size_t) -EOPNOTSUPP;
+	return dax_recovery_write(dax_dev, pgoff, addr, bytes, i);
+};
+
 #else
 #define linear_dax_direct_access NULL
 #define linear_dax_zero_page_range NULL
+#define	linear_dax_recovery_write NULL
 #endif
 
 static struct target_type linear_target = {
@@ -208,6 +219,7 @@ static struct target_type linear_target = {
 	.iterate_devices = linear_iterate_devices,
 	.direct_access = linear_dax_direct_access,
 	.dax_zero_page_range = linear_dax_zero_page_range,
+	.dax_recovery_write = linear_dax_recovery_write,
 };
 
 int __init dm_linear_init(void)
drivers/md/dm-log-writes.c +12 -0
index 139b09b06eda..22c2d64ef963 100644
@@ -927,9 +927,20 @@ static int log_writes_dax_zero_page_range(struct dm_target *ti, pgoff_t pgoff,
 	return dax_zero_page_range(dax_dev, pgoff, nr_pages << PAGE_SHIFT);
 }
 
+static size_t log_writes_dax_recovery_write(struct dm_target *ti,
+	pgoff_t pgoff, void *addr, size_t bytes, struct iov_iter *i)
+{
+	struct dax_device *dax_dev = log_writes_dax_pgoff(ti, &pgoff);
+
+	if (!dax_recovery_capable(dax_dev))
+		return (size_t) -EOPNOTSUPP;
+	return dax_recovery_write(dax_dev, pgoff, addr, bytes, i);
+}
+
 #else
 #define log_writes_dax_direct_access NULL
 #define log_writes_dax_zero_page_range NULL
+#define	log_writes_dax_recovery_write NULL
 #endif
 
 static struct target_type log_writes_target = {
@@ -947,6 +958,7 @@ static struct target_type log_writes_target = {
 	.io_hints = log_writes_io_hints,
 	.direct_access = log_writes_dax_direct_access,
 	.dax_zero_page_range = log_writes_dax_zero_page_range,
+	.dax_recovery_write = log_writes_dax_recovery_write,
 };
 
 static int __init dm_log_writes_init(void)
drivers/md/dm-stripe.c +13 -0
index e566115ec0bb..78c52c8865ef 100644
@@ -332,9 +332,21 @@ static int stripe_dax_zero_page_range(struct dm_target *ti, pgoff_t pgoff,
 	return dax_zero_page_range(dax_dev, pgoff, nr_pages);
 }
 
+static size_t stripe_dax_recovery_write(struct dm_target *ti, pgoff_t pgoff,
+		void *addr, size_t bytes, struct iov_iter *i)
+{
+	struct dax_device *dax_dev = stripe_dax_pgoff(ti, &pgoff);
+
+	if (!dax_recovery_capable(dax_dev))
+		return (size_t) -EOPNOTSUPP;
+
+	return dax_recovery_write(dax_dev, pgoff, addr, bytes, i);
+}
+
 #else
 #define stripe_dax_direct_access NULL
 #define stripe_dax_zero_page_range NULL
+#define	stripe_dax_recovery_write NULL
 #endif
 
 /*
@@ -471,6 +483,7 @@ static struct target_type stripe_target = {
 	.io_hints = stripe_io_hints,
 	.direct_access = stripe_dax_direct_access,
 	.dax_zero_page_range = stripe_dax_zero_page_range,
+	.dax_recovery_write = stripe_dax_recovery_write,
 };
 
 int __init dm_stripe_init(void)
drivers/md/dm.c +27 -0
index c0ae8087c602..bdc142258ace 100644
@@ -1054,6 +1054,32 @@ static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
 	return ret;
 }
 
+static size_t dm_dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
+		void *addr, size_t bytes, struct iov_iter *i)
+{
+	struct mapped_device *md = dax_get_private(dax_dev);
+	sector_t sector = pgoff * PAGE_SECTORS;
+	struct dm_target *ti;
+	long ret = 0;
+	int srcu_idx;
+
+	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
+
+	if (!ti)
+		goto out;
+
+	if (!ti->type->dax_recovery_write) {
+		ret = (size_t)-EOPNOTSUPP;
+		goto out;
+	}
+
+	ret = ti->type->dax_recovery_write(ti, pgoff, addr, bytes, i);
+out:
+	dm_put_live_table(md, srcu_idx);
+
+	return ret;
+}
+
 /*
  * A target may call dm_accept_partial_bio only from the map routine.  It is
  * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_* zone management
@@ -2980,6 +3006,7 @@ static const struct block_device_operations dm_rq_blk_dops = {
 static const struct dax_operations dm_dax_ops = {
 	.direct_access = dm_dax_direct_access,
 	.zero_page_range = dm_dax_zero_page_range,
+	.recovery_write = dm_dax_recovery_write,
 };
 
 /*
drivers/nvdimm/pmem.c +7 -0
index e8823813a8df..638e64681db9 100644
@@ -301,9 +301,16 @@ static long pmem_dax_direct_access(struct dax_device *dax_dev,
 	return __pmem_direct_access(pmem, pgoff, nr_pages, kaddr, pfn);
 }
 
+static size_t pmem_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
+		void *addr, size_t bytes, struct iov_iter *i)
+{
+	return 0;
+}
+
 static const struct dax_operations pmem_dax_ops = {
 	.direct_access = pmem_dax_direct_access,
 	.zero_page_range = pmem_dax_zero_page_range,
+	.recovery_write = pmem_recovery_write,
 };
 
 static ssize_t write_cache_show(struct device *dev,
include/linux/dax.h +16 -0
index 2fc776653c6e..1b3d6ebf3e49 100644
@@ -30,6 +30,9 @@ struct dax_operations {
 			sector_t, sector_t);
 	/* zero_page_range: required operation. Zero page range   */
 	int (*zero_page_range)(struct dax_device *, pgoff_t, size_t);
+	/* recovery_write: optional operation. */
+	size_t (*recovery_write)(struct dax_device *, pgoff_t, void *, size_t,
+				struct iov_iter *);
 };
 
 #if IS_ENABLED(CONFIG_DAX)
@@ -43,6 +46,9 @@ void set_dax_synchronous(struct dax_device *dax_dev);
 void set_dax_recovery(struct dax_device *dax_dev);
 bool dax_recovery_capable(struct dax_device *dax_dev);
 int dax_prep_recovery(struct dax_device *dax_dev, void **kaddr);
+bool dax_recovery_started(struct dax_device *dax_dev, void **kaddr);
+size_t dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
+		size_t bytes, struct iov_iter *i);
 /*
  * Check if given mapping is supported by the file / underlying device.
  */
@@ -101,6 +107,16 @@ static inline int dax_prep_recovery(struct dax_device *dax_dev, void **kaddr)
 {
 	return -EINVAL;
 }
+static inline bool dax_recovery_started(struct dax_device *dax_dev,
+			void **kaddr)
+{
+	return false;
+}
+static inline size_t dax_recovery_write(struct dax_device *dax_dev,
+		pgoff_t pgoff, void *addr, size_t bytes, struct iov_iter *i)
+{
+	return 0;
+}
 #endif
 
 void set_dax_nocache(struct dax_device *dax_dev);
include/linux/device-mapper.h +9 -0
index b26fecf6c8e8..4f134ba63d3c 100644
@@ -150,6 +150,14 @@ typedef long (*dm_dax_direct_access_fn) (struct dm_target *ti, pgoff_t pgoff,
 typedef int (*dm_dax_zero_page_range_fn)(struct dm_target *ti, pgoff_t pgoff,
 		size_t nr_pages);
 
+/*
+ * Returns:
+ *   != 0 : number of bytes transferred, or size_t casted negative error code
+ *   0    : failure
+ */
+typedef size_t (*dm_dax_recovery_write_fn)(struct dm_target *ti, pgoff_t pgoff,
+		void *addr, size_t bytes, struct iov_iter *i);
+
 void dm_error(const char *message);
 
 struct dm_dev {
@@ -199,6 +207,7 @@ struct target_type {
 	dm_io_hints_fn io_hints;
 	dm_dax_direct_access_fn direct_access;
 	dm_dax_zero_page_range_fn dax_zero_page_range;
+	dm_dax_recovery_write_fn dax_recovery_write;
 
 	/* For internal device-mapper use. */
 	struct list_head list;
Signature · 1 line
2.18.4
#19 lore raw

Re: [PATCH v5 4/7] dax: add dax_recovery_write to dax_op and dm target type

Quoted 7 lines · Jane Chu

dax_recovery_write() dax op is only required for DAX device that export DAXDEV_RECOVERY indicating its capability to recover from poisons.

DM may be nested, if part of the base dax devices forming a DM device support dax recovery, the DM device is marked with such capability.

I'd fold this into the previous 2 patches as the flag and method are clearly very tightly coupled.

drivers/md/dm-linear.c static int linear_dax_zero_page_range(struct dm_target *ti, pgoff_t pgoff, · line 192

Quoted 2 lines
drivers/md/dm-linear.c +2 -0
+static size_t linear_dax_recovery_write(struct dm_target *ti, pgoff_t pgoff,
+	void *addr, size_t bytes, struct iov_iter *i)

Function line continuations use two tab indentations or alignment after the opening brace.

drivers/md/dm-linear.c static int linear_dax_zero_page_range(struct dm_target *ti, pgoff_t pgoff, · line 197

Quoted 5 lines
drivers/md/dm-linear.c +5 -0
+{
+	struct dax_device *dax_dev = linear_dax_pgoff(ti, &pgoff);
+
+	if (!dax_recovery_capable(dax_dev))
+		return (size_t) -EOPNOTSUPP;

Returning a negativ errno through an unsigned argument looks dangerous.

include/linux/dax.h struct dax_operations { · line 33

Quoted 1 line
include/linux/dax.h +1 -0
+	/* recovery_write: optional operation. */

And explanation of what the method is use for might be more useful than mentioning that is is optional.

include/linux/dax.h struct dax_operations { · line 35

Quoted 2 lines
include/linux/dax.h +2 -0
+	size_t (*recovery_write)(struct dax_device *, pgoff_t, void *, size_t,
+				struct iov_iter *);

Spelling out the arguments tends to help readability, but then again none of the existing methods does it.

author
#20 lore raw

Re: [PATCH v5 4/7] dax: add dax_recovery_write to dax_op and dm target type

Quoted 11 lines · Christoph Hellwig
Quoted 7 lines · Jane Chu

dax_recovery_write() dax op is only required for DAX device that export DAXDEV_RECOVERY indicating its capability to recover from poisons.

DM may be nested, if part of the base dax devices forming a DM device support dax recovery, the DM device is marked with such capability.

I'd fold this into the previous 2 patches as the flag and method are clearly very tightly coupled.

Make sense, will do.

Quoted 6 lines

drivers/md/dm-linear.c static int linear_dax_zero_page_range(struct dm_target *ti, pgoff_t pgoff, · line 192

Quoted 3 lines
drivers/md/dm-linear.c +2 -0
+static size_t linear_dax_recovery_write(struct dm_target *ti, pgoff_t pgoff,
+	void *addr, size_t bytes, struct iov_iter *i)

Function line continuations use two tab indentations or alignment after the opening brace.

Okay.

Quoted 8 lines

drivers/md/dm-linear.c static int linear_dax_zero_page_range(struct dm_target *ti, pgoff_t pgoff, · line 197

Quoted 6 lines
drivers/md/dm-linear.c +5 -0
+{
+	struct dax_device *dax_dev = linear_dax_pgoff(ti, &pgoff);
+
+	if (!dax_recovery_capable(dax_dev))
+		return (size_t) -EOPNOTSUPP;

Returning a negativ errno through an unsigned argument looks dangerous.

Sorry, should be (ssize_t) there.

Quoted 5 lines

include/linux/dax.h struct dax_operations { · line 33

Quoted 2 lines
include/linux/dax.h +1 -0
+	/* recovery_write: optional operation. */

And explanation of what the method is use for might be more useful than mentioning that is is optional.

Will add substance to comments.

Quoted 6 lines

include/linux/dax.h struct dax_operations { · line 35

Quoted 3 lines
include/linux/dax.h +2 -0
+	size_t (*recovery_write)(struct dax_device *, pgoff_t, void *, size_t,
+				struct iov_iter *);

Spelling out the arguments tends to help readability, but then again none of the existing methods does it.

Thanks!
-jane

#21 lore raw

Re: [PATCH v5 4/7] dax: add dax_recovery_write to dax_op and dm target type

In reply to Jane Chu
Quoted 10 lines · Jane Chu

dax_recovery_write() dax op is only required for DAX device that export DAXDEV_RECOVERY indicating its capability to recover from poisons.

DM may be nested, if part of the base dax devices forming a DM device support dax recovery, the DM device is marked with such capability.

[..]

include/linux/dax.h struct dax_operations { · line 35

Quoted 11 lines
include/linux/dax.h +3 -0
index 2fc776653c6e..1b3d6ebf3e49 100644
@@ -30,6 +30,9 @@ struct dax_operations {
                        sector_t, sector_t);
        /* zero_page_range: required operation. Zero page range   */
        int (*zero_page_range)(struct dax_device *, pgoff_t, size_t);
+       /* recovery_write: optional operation. */
+       size_t (*recovery_write)(struct dax_device *, pgoff_t, void *, size_t,
+                               struct iov_iter *);

The removal of the ->copy_{to,from}_iter() operations set the precedent that dax ops should not be needed when the operation can be carried out generically. The only need to call back to the pmem driver is so that it can call nvdimm_clear_poison(). nvdimm_clear_poison() in turn only needs the 'struct device' hosting the pmem and the physical address to be cleared. The physical address is already returned by dax_direct_access(). The device is something that could be added to dax_device, and the pgmap could host the callback that pmem fills in. Something like:

drivers/nvdimm/pfn_devs.c +0 -0
index 58eda16f5c53..36486ba4753a 100644
@@ -694,6 +694,7 @@ static int __nvdimm_setup_pfn(struct nd_pfn

*nd_pfn, struct dev_pagemap *pgmap)

                .end = nsio->res.end - end_trunc,
        };
        pgmap->nr_range = 1;
+       pgmap->owner = &nd_pfn->dev;
        if (nd_pfn->mode == PFN_MODE_RAM) {
                if (offset < reserve)
                        return -EINVAL;
drivers/nvdimm/pmem.c +1 -0
index 58d95242a836..95e1b6326f88 100644
@@ -481,6 +481,7 @@ static int pmem_attach_disk(struct device *dev,
        }
        set_dax_nocache(dax_dev);
        set_dax_nomc(dax_dev);
+       set_dax_pgmap(dax_dev, &pmem->pgmap);
        if (is_nvdimm_sync(nd_region))
                set_dax_synchronous(dax_dev);
        rc = dax_add_host(dax_dev, disk);
include/linux/memremap.h +6 -0
index 1fafcc38acba..8cb59b5df38b 100644
@@ -81,6 +81,11 @@ struct dev_pagemap_ops {
 
 #define PGMAP_ALTMAP_VALID     (1 << 0)
 
+struct dev_pagemap_operations {
+       size_t (*recovery_write)(struct dev_pagemap *pgmap, void *, size_t,
+                                struct iov_iter *);
+};
+
 /**
  * struct dev_pagemap - metadata for ZONE_DEVICE mappings
  * @altmap: pre-allocated/reserved memory for vmemmap allocations
@@ -111,12 +116,15 @@ struct dev_pagemap {
        const struct dev_pagemap_ops *ops;
        void *owner;
        int nr_range;
+       struct dev_pagemap_operations ops;
        union {
                struct range range;
                struct range ranges[0];
        };
 };
 

...then DM does not need to be involved in the recovery path, fs/dax.c just does dax_direct_access(..., DAX_RECOVERY, ...) and then looks up the pgmap to generically coordinate the recovery_write(). The pmem driver would be responsible for setting pgmap->recovery_write() to a function that calls nvdimm_clear_poison().

This arch works for anything that can be described by a pgmap, and supports error clearing, it need not be limited to the pmem block driver.

author
#22 lore raw

Re: [PATCH v5 4/7] dax: add dax_recovery_write to dax_op and dm target type

Quoted 93 lines · Dan Williams
Quoted 10 lines · Jane Chu

dax_recovery_write() dax op is only required for DAX device that export DAXDEV_RECOVERY indicating its capability to recover from poisons.

DM may be nested, if part of the base dax devices forming a DM device support dax recovery, the DM device is marked with such capability.

[..]

include/linux/dax.h struct dax_operations { · line 35

Quoted 11 lines
include/linux/dax.h +3 -0
index 2fc776653c6e..1b3d6ebf3e49 100644
@@ -30,6 +30,9 @@ struct dax_operations {
                         sector_t, sector_t);
         /* zero_page_range: required operation. Zero page range   */
         int (*zero_page_range)(struct dax_device *, pgoff_t, size_t);
+       /* recovery_write: optional operation. */
+       size_t (*recovery_write)(struct dax_device *, pgoff_t, void *, size_t,
+                               struct iov_iter *);

The removal of the ->copy_{to,from}_iter() operations set the precedent that dax ops should not be needed when the operation can be carried out generically. The only need to call back to the pmem driver is so that it can call nvdimm_clear_poison(). nvdimm_clear_poison() in turn only needs the 'struct device' hosting the pmem and the physical address to be cleared. The physical address is already returned by dax_direct_access(). The device is something that could be added to dax_device, and the pgmap could host the callback that pmem fills in. Something like:

drivers/nvdimm/pfn_devs.c +1 -0
index 58eda16f5c53..36486ba4753a 100644
@@ -694,6 +694,7 @@ static int __nvdimm_setup_pfn(struct nd_pfn
 *nd_pfn, struct dev_pagemap *pgmap)
                 .end = nsio->res.end - end_trunc,
         };
         pgmap->nr_range = 1;
+       pgmap->owner = &nd_pfn->dev;
         if (nd_pfn->mode == PFN_MODE_RAM) {
                 if (offset < reserve)
                         return -EINVAL;
drivers/nvdimm/pmem.c +1 -0
index 58d95242a836..95e1b6326f88 100644
@@ -481,6 +481,7 @@ static int pmem_attach_disk(struct device *dev,
         }
         set_dax_nocache(dax_dev);
         set_dax_nomc(dax_dev);
+       set_dax_pgmap(dax_dev, &pmem->pgmap);
         if (is_nvdimm_sync(nd_region))
                 set_dax_synchronous(dax_dev);
         rc = dax_add_host(dax_dev, disk);
include/linux/memremap.h +6 -0
index 1fafcc38acba..8cb59b5df38b 100644
@@ -81,6 +81,11 @@ struct dev_pagemap_ops {
 
  #define PGMAP_ALTMAP_VALID     (1 << 0)
 
+struct dev_pagemap_operations {
+       size_t (*recovery_write)(struct dev_pagemap *pgmap, void *, size_t,
+                                struct iov_iter *);
+};
+
  /**
   * struct dev_pagemap - metadata for ZONE_DEVICE mappings
   * @altmap: pre-allocated/reserved memory for vmemmap allocations
@@ -111,12 +116,15 @@ struct dev_pagemap {
         const struct dev_pagemap_ops *ops;
         void *owner;
         int nr_range;
+       struct dev_pagemap_operations ops;
         union {
                 struct range range;
                 struct range ranges[0];
         };
  };
 

...then DM does not need to be involved in the recovery path, fs/dax.c just does dax_direct_access(..., DAX_RECOVERY, ...) and then looks up the pgmap to generically coordinate the recovery_write(). The pmem driver would be responsible for setting pgmap->recovery_write() to a function that calls nvdimm_clear_poison().

Quoted 3 lines

This arch works for anything that can be described by a pgmap, and supports error clearing, it need not be limited to the pmem block driver.

This is an interesting idea, let me give it a try and get back to you.

Thanks!
-jane

author
#23 lore raw

PATCH v5 5/7 pmem: add pmem_recovery_write() dax op

In reply to Jane Chu

pmem_recovery_write() consists of clearing poison via DSM, clearing page HWPoison bit, re-state _PAGE_PRESENT bit, cacheflush, write, and finally clearing bad-block record.

A competing pread thread is held off during recovery write by the presence of bad-block record. A competing recovery_write thread is serialized by a lock.

---
 drivers/nvdimm/pmem.c | 82 +++++++++++++++++++++++++++++++++++++++----
 drivers/nvdimm/pmem.h |  1 +
 2 files changed, 77 insertions(+), 6 deletions(-)
drivers/nvdimm/pmem.c +76 -6
index 638e64681db9..f2d6b34d48de 100644
@@ -69,6 +69,14 @@ static void hwpoison_clear(struct pmem_device *pmem,
 	}
 }
 
+static void pmem_clear_badblocks(struct pmem_device *pmem, sector_t sector,
+				long cleared_blks)
+{
+	badblocks_clear(&pmem->bb, sector, cleared_blks);
+	if (pmem->bb_state)
+		sysfs_notify_dirent(pmem->bb_state);
+}
+
 static blk_status_t pmem_clear_poison(struct pmem_device *pmem,
 		phys_addr_t offset, unsigned int len)
 {
@@ -88,9 +96,7 @@ static blk_status_t pmem_clear_poison(struct pmem_device *pmem,
 		dev_dbg(dev, "%#llx clear %ld sector%s\n",
 				(unsigned long long) sector, cleared,
 				cleared > 1 ? "s" : "");
-		badblocks_clear(&pmem->bb, sector, cleared);
-		if (pmem->bb_state)
-			sysfs_notify_dirent(pmem->bb_state);
+		pmem_clear_badblocks(pmem, sector, cleared);
 	}
 
 	arch_invalidate_pmem(pmem->virt_addr + offset, len);
@@ -257,10 +263,15 @@ static int pmem_rw_page(struct block_device *bdev, sector_t sector,
 __weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
 		long nr_pages, void **kaddr, pfn_t *pfn)
 {
+	bool bad_pmem;
+	bool do_recovery = false;
 	resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
 
-	if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
-					PFN_PHYS(nr_pages))))
+	bad_pmem = is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
+				PFN_PHYS(nr_pages));
+	if (bad_pmem && kaddr)
+		do_recovery = dax_recovery_started(pmem->dax_dev, kaddr);
+	if (bad_pmem && !do_recovery)
 		return -EIO;
 
 	if (kaddr)
@@ -301,10 +312,68 @@ static long pmem_dax_direct_access(struct dax_device *dax_dev,
 	return __pmem_direct_access(pmem, pgoff, nr_pages, kaddr, pfn);
 }
 
+/*
+ * The recovery write thread started out as a normal pwrite thread and
+ * when the filesystem was told about potential media error in the
+ * range, filesystem turns the normal pwrite to a dax_recovery_write.
+ *
+ * The recovery write consists of clearing poison via DSM, clearing page
+ * HWPoison bit, reenable page-wide read-write permission, flush the
+ * caches and finally write.  A competing pread thread needs to be held
+ * off during the recovery process since data read back might not be valid.
+ * And that's achieved by placing the badblock records clearing after
+ * the completion of the recovery write.
+ *
+ * Any competing recovery write thread needs to be serialized, and this is
+ * done via pmem device level lock .recovery_lock.
+ */
 static size_t pmem_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
 		void *addr, size_t bytes, struct iov_iter *i)
 {
-	return 0;
+	size_t rc, len, off;
+	phys_addr_t pmem_off;
+	struct pmem_device *pmem = dax_get_private(dax_dev);
+	struct device *dev = pmem->bb.dev;
+	sector_t sector;
+	long cleared, cleared_blk;
+
+	mutex_lock(&pmem->recovery_lock);
+
+	/* If no poison found in the range, go ahead with write */
+	off = (unsigned long)addr & ~PAGE_MASK;
+	len = PFN_PHYS(PFN_UP(off + bytes));
+	if (!is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512, len)) {
+		rc = _copy_from_iter_flushcache(addr, bytes, i);
+		goto write_done;
+	}
+
+	/* Not page-aligned range cannot be recovered */
+	if (off || !(PAGE_ALIGNED(bytes))) {
+		dev_warn(dev, "Found poison, but addr(%p) or bytes(%#lx) not page aligned\n",
+			addr, bytes);
+		rc = (size_t) -EIO;
+		goto write_done;
+	}
+
+	pmem_off = PFN_PHYS(pgoff) + pmem->data_offset;
+	sector = (pmem_off - pmem->data_offset) / 512;
+	cleared = nvdimm_clear_poison(dev, pmem->phys_addr + pmem_off, len);
+	cleared_blk = cleared / 512;
+	if (cleared_blk > 0) {
+		hwpoison_clear(pmem, pmem->phys_addr + pmem_off, cleared);
+	} else {
+		dev_warn(dev, "pmem_recovery_write: cleared_blk: %ld\n",
+			cleared_blk);
+		rc = (size_t) -EIO;
+		goto write_done;
+	}
+	arch_invalidate_pmem(pmem->virt_addr + pmem_off, bytes);
+	rc = _copy_from_iter_flushcache(addr, bytes, i);
+	pmem_clear_badblocks(pmem, sector, cleared_blk);
+
+write_done:
+	mutex_unlock(&pmem->recovery_lock);
+	return rc;
 }
 
 static const struct dax_operations pmem_dax_ops = {
@@ -495,6 +564,7 @@ static int pmem_attach_disk(struct device *dev,
 		goto out_cleanup_dax;
 	dax_write_cache(dax_dev, nvdimm_has_cache(nd_region));
 	set_dax_recovery(dax_dev);
+	mutex_init(&pmem->recovery_lock);
 	pmem->dax_dev = dax_dev;
 
 	rc = device_add_disk(dev, disk, pmem_attribute_groups);
drivers/nvdimm/pmem.h +1 -0
index 59cfe13ea8a8..971bff7552d6 100644
@@ -24,6 +24,7 @@ struct pmem_device {
 	struct dax_device	*dax_dev;
 	struct gendisk		*disk;
 	struct dev_pagemap	pgmap;
+	struct mutex		recovery_lock;
 };
 
 long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
Signature · 1 line
2.18.4
#24 lore raw

Re: [PATCH v5 5/7] pmem: add pmem_recovery_write() dax op

Thank you for the patch! Perhaps something to improve:

Hi Jane,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on nvdimm/libnvdimm-for-next]
[also build test WARNING on linus/master v5.17-rc1 next-20220128] [cannot apply to device-mapper-dm/for-next tip/x86/core]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Jane-Chu/DAX-poison-recovery/20220129-053412
base:   https://git.kernel.org/pub/scm/linux/kernel/git/nvdimm/nvdimm.git libnvdimm-for-next
config: mips-randconfig-r026-20220127 (https://download.01.org/0day-ci/archive/20220129/202201291550.PbNwGb5W-lkp(a)intel.com/config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 33b45ee44b1f32ffdbc995e6fec806271b4b3ba4)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install mips cross compiling tool for clang build
# apt-get install binutils-mips-linux-gnu
# https://github.com/0day-ci/linux/commit/7190481d22a37d39ed1ae4268805d6cb0335849f
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Jane-Chu/DAX-poison-recovery/20220129-053412
git checkout 7190481d22a37d39ed1ae4268805d6cb0335849f
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=mips SHELL=/bin/bash drivers/nvdimm/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> drivers/nvdimm/pmem.c:353:10: warning: format specifies type 'unsigned long' but the argument has type 'size_t' (aka 'unsigned int') [-Wformat]
                           addr, bytes);
                                 ^~~~~
   include/linux/dev_printk.h:146:70: note: expanded from macro 'dev_warn'
           dev_printk_index_wrap(_dev_warn, KERN_WARNING, dev, dev_fmt(fmt), ##__VA_ARGS__)
                                                                       ~~~     ^~~~~~~~~~~
   include/linux/dev_printk.h:110:23: note: expanded from macro 'dev_printk_index_wrap'
                   _p_func(dev, fmt, ##__VA_ARGS__);                       \
                                ~~~    ^~~~~~~~~~~

1 warning generated.

vim +353 drivers/nvdimm/pmem.c

314
315 /*
316 * The recovery write thread started out as a normal pwrite thread and
317 * when the filesystem was told about potential media error in the
318 * range, filesystem turns the normal pwrite to a dax_recovery_write.
319 *
320 * The recovery write consists of clearing poison via DSM, clearing page
321 * HWPoison bit, reenable page-wide read-write permission, flush the
322 * caches and finally write. A competing pread thread needs to be held
323 * off during the recovery process since data read back might not be valid.
324 * And that's achieved by placing the badblock records clearing after
325 * the completion of the recovery write.
326 *
327 * Any competing recovery write thread needs to be serialized, and this is
328 * done via pmem device level lock .recovery_lock.
329 */
330 static size_t pmem_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
331 void *addr, size_t bytes, struct iov_iter *i)
332 {
333 size_t rc, len, off;
334 phys_addr_t pmem_off;
335 struct pmem_device *pmem = dax_get_private(dax_dev);
336 struct device *dev = pmem->bb.dev;
337 sector_t sector;
338 long cleared, cleared_blk;
339
340 mutex_lock(&pmem->recovery_lock);
341
342 /* If no poison found in the range, go ahead with write */
343 off = (unsigned long)addr & ~PAGE_MASK;
344 len = PFN_PHYS(PFN_UP(off + bytes));
345 if (!is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512, len)) {
346 rc = _copy_from_iter_flushcache(addr, bytes, i);
347 goto write_done;
348 }
349
350 /* Not page-aligned range cannot be recovered */
351 if (off || !(PAGE_ALIGNED(bytes))) {
352 dev_warn(dev, "Found poison, but addr(%p) or bytes(%#lx) not page aligned\n",

 > 353				addr, bytes);

354 rc = (size_t) -EIO;
355 goto write_done;
356 }
357
358 pmem_off = PFN_PHYS(pgoff) + pmem->data_offset;
359 sector = (pmem_off - pmem->data_offset) / 512;
360 cleared = nvdimm_clear_poison(dev, pmem->phys_addr + pmem_off, len);
361 cleared_blk = cleared / 512;
362 if (cleared_blk > 0) {
363 hwpoison_clear(pmem, pmem->phys_addr + pmem_off, cleared);
364 } else {
365 dev_warn(dev, "pmem_recovery_write: cleared_blk: %ld\n",
366 cleared_blk);
367 rc = (size_t) -EIO;
368 goto write_done;
369 }
370 arch_invalidate_pmem(pmem->virt_addr + pmem_off, bytes);
371 rc = _copy_from_iter_flushcache(addr, bytes, i);
372 pmem_clear_badblocks(pmem, sector, cleared_blk);
373
374 write_done:
375 mutex_unlock(&pmem->recovery_lock);
376 return rc;
377 }
378

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#25 lore raw

Re: [PATCH v5 5/7] pmem: add pmem_recovery_write() dax op

In reply to Jane Chu
Quoted 6 lines
drivers/nvdimm/pmem.c +2 -0
@@ -257,10 +263,15 @@ static int pmem_rw_page(struct block_device *bdev, sector_t sector,
 __weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
 		long nr_pages, void **kaddr, pfn_t *pfn)
 {
+	bool bad_pmem;
+	bool do_recovery = false;

drivers/nvdimm/pmem.c static int pmem_rw_page(struct block_device *bdev, sector_t sector, · line 275

Quoted 10 lines
drivers/nvdimm/pmem.c +5 -2
 	resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
 
-	if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
-					PFN_PHYS(nr_pages))))
+	bad_pmem = is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
+				PFN_PHYS(nr_pages));
+	if (bad_pmem && kaddr)
+		do_recovery = dax_recovery_started(pmem->dax_dev, kaddr);
+	if (bad_pmem && !do_recovery)
 		return -EIO;

I find the passing of the recovery flag through the address very cumbersome. I remember there was some kind of discussion, but this looks pretty ugly.

Also no need for the bad_pmem variable:

	if (is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512, PFN_PHYS(nr_pages)) &&
	    (!kaddr | !dax_recovery_started(pmem->dax_dev, kaddr)))
		return -EIO;

Also: the !kaddr check could go into dax_recovery_started. That way even if we stick with the overloading kaddr could also be used just for the flag if needed.

author
#26 lore raw

Re: [PATCH v5 5/7] pmem: add pmem_recovery_write() dax op

Quoted 21 lines · Christoph Hellwig
Quoted 6 lines
drivers/nvdimm/pmem.c +2 -0
@@ -257,10 +263,15 @@ static int pmem_rw_page(struct block_device *bdev, sector_t sector,
  __weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
  		long nr_pages, void **kaddr, pfn_t *pfn)
  {
+	bool bad_pmem;
+	bool do_recovery = false;

drivers/nvdimm/pmem.c static int pmem_rw_page(struct block_device *bdev, sector_t sector, · line 275

Quoted 10 lines
drivers/nvdimm/pmem.c +5 -2
  	resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
 
-	if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
-					PFN_PHYS(nr_pages))))
+	bad_pmem = is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
+				PFN_PHYS(nr_pages));
+	if (bad_pmem && kaddr)
+		do_recovery = dax_recovery_started(pmem->dax_dev, kaddr);
+	if (bad_pmem && !do_recovery)
  		return -EIO;

I find the passing of the recovery flag through the address very cumbersome. I remember there was some kind of discussion, but this looks pretty ugly.

Quoted 5 lines

Also no need for the bad_pmem variable:

	if (is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512, PFN_PHYS(nr_pages)) &&
	    (!kaddr | !dax_recovery_started(pmem->dax_dev, kaddr)))
		return -EIO;

Okay.

Quoted 4 lines

Also: the !kaddr check could go into dax_recovery_started. That way even if we stick with the overloading kaddr could also be used just for the flag if needed.

The !kaddr check is in dax_recovery_started(), and that reminded me the check should be in dax_prep_recovery() too.

Thanks!
-jane

#27 lore raw

Re: [PATCH v5 5/7] pmem: add pmem_recovery_write() dax op

In reply to Jane Chu
Quoted 92 lines · Jane Chu

pmem_recovery_write() consists of clearing poison via DSM, clearing page HWPoison bit, re-state _PAGE_PRESENT bit, cacheflush, write, and finally clearing bad-block record.

A competing pread thread is held off during recovery write by the presence of bad-block record. A competing recovery_write thread is serialized by a lock.

---
 drivers/nvdimm/pmem.c | 82 +++++++++++++++++++++++++++++++++++++++----
 drivers/nvdimm/pmem.h |  1 +
 2 files changed, 77 insertions(+), 6 deletions(-)
drivers/nvdimm/pmem.c +37 -6
index 638e64681db9..f2d6b34d48de 100644
@@ -69,6 +69,14 @@ static void hwpoison_clear(struct pmem_device *pmem,
        }
 }
 
+static void pmem_clear_badblocks(struct pmem_device *pmem, sector_t sector,
+                               long cleared_blks)
+{
+       badblocks_clear(&pmem->bb, sector, cleared_blks);
+       if (pmem->bb_state)
+               sysfs_notify_dirent(pmem->bb_state);
+}
+
 static blk_status_t pmem_clear_poison(struct pmem_device *pmem,
                phys_addr_t offset, unsigned int len)
 {
@@ -88,9 +96,7 @@ static blk_status_t pmem_clear_poison(struct pmem_device *pmem,
                dev_dbg(dev, "%#llx clear %ld sector%s\n",
                                (unsigned long long) sector, cleared,
                                cleared > 1 ? "s" : "");
-               badblocks_clear(&pmem->bb, sector, cleared);
-               if (pmem->bb_state)
-                       sysfs_notify_dirent(pmem->bb_state);
+               pmem_clear_badblocks(pmem, sector, cleared);
        }
 
        arch_invalidate_pmem(pmem->virt_addr + offset, len);
@@ -257,10 +263,15 @@ static int pmem_rw_page(struct block_device *bdev, sector_t sector,
 __weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
                long nr_pages, void **kaddr, pfn_t *pfn)
 {
+       bool bad_pmem;
+       bool do_recovery = false;
        resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
 
-       if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
-                                       PFN_PHYS(nr_pages))))
+       bad_pmem = is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
+                               PFN_PHYS(nr_pages));
+       if (bad_pmem && kaddr)
+               do_recovery = dax_recovery_started(pmem->dax_dev, kaddr);
+       if (bad_pmem && !do_recovery)
                return -EIO;
 
        if (kaddr)
@@ -301,10 +312,68 @@ static long pmem_dax_direct_access(struct dax_device *dax_dev,
        return __pmem_direct_access(pmem, pgoff, nr_pages, kaddr, pfn);
 }
 
+/*
+ * The recovery write thread started out as a normal pwrite thread and
+ * when the filesystem was told about potential media error in the
+ * range, filesystem turns the normal pwrite to a dax_recovery_write.
+ *
+ * The recovery write consists of clearing poison via DSM, clearing page
+ * HWPoison bit, reenable page-wide read-write permission, flush the
+ * caches and finally write.  A competing pread thread needs to be held
+ * off during the recovery process since data read back might not be valid.
+ * And that's achieved by placing the badblock records clearing after
+ * the completion of the recovery write.
+ *
+ * Any competing recovery write thread needs to be serialized, and this is
+ * done via pmem device level lock .recovery_lock.
+ */
 static size_t pmem_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
                void *addr, size_t bytes, struct iov_iter *i)
 {
-       return 0;
+       size_t rc, len, off;
+       phys_addr_t pmem_off;
+       struct pmem_device *pmem = dax_get_private(dax_dev);
+       struct device *dev = pmem->bb.dev;
+       sector_t sector;
+       long cleared, cleared_blk;

drivers/nvdimm/pmem.c static long pmem_dax_direct_access(struct dax_device *dax_dev, · line 348

Quoted 10 lines
drivers/nvdimm/pmem.c +10 -0
+
+       mutex_lock(&pmem->recovery_lock);
+
+       /* If no poison found in the range, go ahead with write */
+       off = (unsigned long)addr & ~PAGE_MASK;
+       len = PFN_PHYS(PFN_UP(off + bytes));
+       if (!is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512, len)) {
+               rc = _copy_from_iter_flushcache(addr, bytes, i);
+               goto write_done;
+       }

is_bad_pmem() takes a seqlock so it should be safe to move the recovery_lock below this point.

drivers/nvdimm/pmem.c static long pmem_dax_direct_access(struct dax_device *dax_dev, · line 353

Quoted 5 lines
drivers/nvdimm/pmem.c +5 -0
+
+       /* Not page-aligned range cannot be recovered */
+       if (off || !(PAGE_ALIGNED(bytes))) {
+               dev_warn(dev, "Found poison, but addr(%p) or bytes(%#lx) not page aligned\n",
+                       addr, bytes);

fs/dax.c will prevent this from happening, right? It seems like an upper layer bug if we get this far into the recovery process without checking if a full page is being overwritten.

Quoted 9 lines
drivers/nvdimm/pmem.c +9 -0
+               rc = (size_t) -EIO;
+               goto write_done;
+       }
+
+       pmem_off = PFN_PHYS(pgoff) + pmem->data_offset;
+       sector = (pmem_off - pmem->data_offset) / 512;
+       cleared = nvdimm_clear_poison(dev, pmem->phys_addr + pmem_off, len);
+       cleared_blk = cleared / 512;
+       if (cleared_blk > 0) {

drivers/nvdimm/pmem.c static long pmem_dax_direct_access(struct dax_device *dax_dev, · line 372

Quoted 10 lines
drivers/nvdimm/pmem.c +10 -0
+               hwpoison_clear(pmem, pmem->phys_addr + pmem_off, cleared);
+       } else {
+               dev_warn(dev, "pmem_recovery_write: cleared_blk: %ld\n",
+                       cleared_blk);
+               rc = (size_t) -EIO;
+               goto write_done;
+       }
+       arch_invalidate_pmem(pmem->virt_addr + pmem_off, bytes);
+       rc = _copy_from_iter_flushcache(addr, bytes, i);
+       pmem_clear_badblocks(pmem, sector, cleared_blk);

This duplicates pmem_clear_poison() can more code be shared between the 2 functions?

Quoted 30 lines
drivers/nvdimm/pmem.c +5 -0
+
+write_done:
+       mutex_unlock(&pmem->recovery_lock);
+       return rc;
 }
 
 static const struct dax_operations pmem_dax_ops = {
@@ -495,6 +564,7 @@ static int pmem_attach_disk(struct device *dev,
                goto out_cleanup_dax;
        dax_write_cache(dax_dev, nvdimm_has_cache(nd_region));
        set_dax_recovery(dax_dev);
+       mutex_init(&pmem->recovery_lock);
        pmem->dax_dev = dax_dev;
 
        rc = device_add_disk(dev, disk, pmem_attribute_groups);
drivers/nvdimm/pmem.h +1 -1
index 59cfe13ea8a8..971bff7552d6 100644
@@ -24,6 +24,7 @@ struct pmem_device {
        struct dax_device       *dax_dev;
        struct gendisk          *disk;
        struct dev_pagemap      pgmap;
+       struct mutex            recovery_lock;
 };
 
 long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
--

2.18.4

author
#28 lore raw

Re: [PATCH v5 5/7] pmem: add pmem_recovery_write() dax op

Quoted 98 lines · Dan Williams
Quoted 97 lines · Jane Chu

pmem_recovery_write() consists of clearing poison via DSM, clearing page HWPoison bit, re-state _PAGE_PRESENT bit, cacheflush, write, and finally clearing bad-block record.

A competing pread thread is held off during recovery write by the presence of bad-block record. A competing recovery_write thread is serialized by a lock.

---
  drivers/nvdimm/pmem.c | 82 +++++++++++++++++++++++++++++++++++++++----
  drivers/nvdimm/pmem.h |  1 +
  2 files changed, 77 insertions(+), 6 deletions(-)
drivers/nvdimm/pmem.c +42 -6
index 638e64681db9..f2d6b34d48de 100644
@@ -69,6 +69,14 @@ static void hwpoison_clear(struct pmem_device *pmem,
         }
  }
 
+static void pmem_clear_badblocks(struct pmem_device *pmem, sector_t sector,
+                               long cleared_blks)
+{
+       badblocks_clear(&pmem->bb, sector, cleared_blks);
+       if (pmem->bb_state)
+               sysfs_notify_dirent(pmem->bb_state);
+}
+
  static blk_status_t pmem_clear_poison(struct pmem_device *pmem,
                 phys_addr_t offset, unsigned int len)
  {
@@ -88,9 +96,7 @@ static blk_status_t pmem_clear_poison(struct pmem_device *pmem,
                 dev_dbg(dev, "%#llx clear %ld sector%s\n",
                                 (unsigned long long) sector, cleared,
                                 cleared > 1 ? "s" : "");
-               badblocks_clear(&pmem->bb, sector, cleared);
-               if (pmem->bb_state)
-                       sysfs_notify_dirent(pmem->bb_state);
+               pmem_clear_badblocks(pmem, sector, cleared);
         }
 
         arch_invalidate_pmem(pmem->virt_addr + offset, len);
@@ -257,10 +263,15 @@ static int pmem_rw_page(struct block_device *bdev, sector_t sector,
  __weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
                 long nr_pages, void **kaddr, pfn_t *pfn)
  {
+       bool bad_pmem;
+       bool do_recovery = false;
         resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
 
-       if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
-                                       PFN_PHYS(nr_pages))))
+       bad_pmem = is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
+                               PFN_PHYS(nr_pages));
+       if (bad_pmem && kaddr)
+               do_recovery = dax_recovery_started(pmem->dax_dev, kaddr);
+       if (bad_pmem && !do_recovery)
                 return -EIO;
 
         if (kaddr)
@@ -301,10 +312,68 @@ static long pmem_dax_direct_access(struct dax_device *dax_dev,
         return __pmem_direct_access(pmem, pgoff, nr_pages, kaddr, pfn);
  }
 
+/*
+ * The recovery write thread started out as a normal pwrite thread and
+ * when the filesystem was told about potential media error in the
+ * range, filesystem turns the normal pwrite to a dax_recovery_write.
+ *
+ * The recovery write consists of clearing poison via DSM, clearing page
+ * HWPoison bit, reenable page-wide read-write permission, flush the
+ * caches and finally write.  A competing pread thread needs to be held
+ * off during the recovery process since data read back might not be valid.
+ * And that's achieved by placing the badblock records clearing after
+ * the completion of the recovery write.
+ *
+ * Any competing recovery write thread needs to be serialized, and this is
+ * done via pmem device level lock .recovery_lock.
+ */
  static size_t pmem_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
                 void *addr, size_t bytes, struct iov_iter *i)
  {
-       return 0;
+       size_t rc, len, off;
+       phys_addr_t pmem_off;
+       struct pmem_device *pmem = dax_get_private(dax_dev);
+       struct device *dev = pmem->bb.dev;
+       sector_t sector;
+       long cleared, cleared_blk;
+
+       mutex_lock(&pmem->recovery_lock);
+
+       /* If no poison found in the range, go ahead with write */
+       off = (unsigned long)addr & ~PAGE_MASK;
Quoted 8 lines

drivers/nvdimm/pmem.c static long pmem_dax_direct_access(struct dax_device *dax_dev, · line 348

Quoted 5 lines
drivers/nvdimm/pmem.c +5 -0
+       len = PFN_PHYS(PFN_UP(off + bytes));
+       if (!is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512, len)) {
+               rc = _copy_from_iter_flushcache(addr, bytes, i);
+               goto write_done;
+       }

is_bad_pmem() takes a seqlock so it should be safe to move the recovery_lock below this point.

Okay, thanks!

Quoted 10 lines

drivers/nvdimm/pmem.c static long pmem_dax_direct_access(struct dax_device *dax_dev, · line 353

Quoted 6 lines
drivers/nvdimm/pmem.c +5 -0
+
+       /* Not page-aligned range cannot be recovered */
+       if (off || !(PAGE_ALIGNED(bytes))) {
+               dev_warn(dev, "Found poison, but addr(%p) or bytes(%#lx) not page aligned\n",
+                       addr, bytes);

fs/dax.c will prevent this from happening, right? It seems like an upper layer bug if we get this far into the recovery process without checking if a full page is being overwritten.

Yes, at the start of each dax_iomap_iter, the buffer is page aligned. However, the underlying dax_copy_from_iter is allowed to return partial results, causing the subsequent 'while' loop within dax_iomap_iter to continue at not page aligned address. I ran into the situation when I played around dax_copy_from_iter, not sure in reality, partial result is legitimate, just thought to issue a warning should the situation happen.

Quoted 21 lines
Quoted 10 lines
drivers/nvdimm/pmem.c +9 -0
+               rc = (size_t) -EIO;
+               goto write_done;
+       }
+
+       pmem_off = PFN_PHYS(pgoff) + pmem->data_offset;
+       sector = (pmem_off - pmem->data_offset) / 512;
+       cleared = nvdimm_clear_poison(dev, pmem->phys_addr + pmem_off, len);
+       cleared_blk = cleared / 512;
+       if (cleared_blk > 0) {

drivers/nvdimm/pmem.c static long pmem_dax_direct_access(struct dax_device *dax_dev, · line 372

Quoted 10 lines
drivers/nvdimm/pmem.c +10 -0
+               hwpoison_clear(pmem, pmem->phys_addr + pmem_off, cleared);
+       } else {
+               dev_warn(dev, "pmem_recovery_write: cleared_blk: %ld\n",
+                       cleared_blk);
+               rc = (size_t) -EIO;
+               goto write_done;
+       }
+       arch_invalidate_pmem(pmem->virt_addr + pmem_off, bytes);
+       rc = _copy_from_iter_flushcache(addr, bytes, i);
+       pmem_clear_badblocks(pmem, sector, cleared_blk);
Quoted 2 lines

This duplicates pmem_clear_poison() can more code be shared between the 2 functions?

I'll look into refactoring pmem_clear_poison().

Quoted 28 lines
Quoted 28 lines
drivers/nvdimm/pmem.c +5 -0
+
+write_done:
+       mutex_unlock(&pmem->recovery_lock);
+       return rc;
  }
 
  static const struct dax_operations pmem_dax_ops = {
@@ -495,6 +564,7 @@ static int pmem_attach_disk(struct device *dev,
                 goto out_cleanup_dax;
         dax_write_cache(dax_dev, nvdimm_has_cache(nd_region));
         set_dax_recovery(dax_dev);
+       mutex_init(&pmem->recovery_lock);
         pmem->dax_dev = dax_dev;
 
         rc = device_add_disk(dev, disk, pmem_attribute_groups);
drivers/nvdimm/pmem.h +1 -0
index 59cfe13ea8a8..971bff7552d6 100644
@@ -24,6 +24,7 @@ struct pmem_device {
         struct dax_device       *dax_dev;
         struct gendisk          *disk;
         struct dev_pagemap      pgmap;
+       struct mutex            recovery_lock;
  };
 
Quoted 4 lines
Quoted 4 lines
drivers/nvdimm/pmem.h +0 -1
  long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
--

2.18.4

thanks!
-jane

author
#29 lore raw

PATCH v5 6/7 dax: add recovery_write to dax_iomap_iter in failure path

In reply to Jane Chu

dax_iomap_iter() fails if the destination range contains poison. Add recovery_write to the failure code path.

---
 fs/dax.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
fs/dax.c +21 -2
index cd03485867a7..236675bd5946 100644
@@ -1199,6 +1199,8 @@ int dax_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
 }
 EXPORT_SYMBOL_GPL(dax_truncate_page);
 
+typedef size_t (*iter_func_t)(struct dax_device *dax_dev, pgoff_t pgoff,
+		void *addr, size_t bytes, struct iov_iter *i);
 static loff_t dax_iomap_iter(const struct iomap_iter *iomi,
 		struct iov_iter *iter)
 {
@@ -1210,6 +1212,7 @@ static loff_t dax_iomap_iter(const struct iomap_iter *iomi,
 	ssize_t ret = 0;
 	size_t xfer;
 	int id;
+	iter_func_t write_func = dax_copy_from_iter;
 
 	if (iov_iter_rw(iter) == READ) {
 		end = min(end, i_size_read(iomi->inode));
@@ -1249,6 +1252,17 @@ static loff_t dax_iomap_iter(const struct iomap_iter *iomi,
 
 		map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
 				&kaddr, NULL);
+		if ((map_len == -EIO) && (iov_iter_rw(iter) == WRITE)) {
+			if (dax_prep_recovery(dax_dev, &kaddr) < 0) {
+				ret = map_len;
+				break;
+			}
+			map_len = dax_direct_access(dax_dev, pgoff,
+					PHYS_PFN(size), &kaddr, NULL);
+			if (map_len > 0)
+				write_func = dax_recovery_write;
+		}
+
 		if (map_len < 0) {
 			ret = map_len;
 			break;
@@ -1261,12 +1275,17 @@ static loff_t dax_iomap_iter(const struct iomap_iter *iomi,
 			map_len = end - pos;
 
 		if (iov_iter_rw(iter) == WRITE)
-			xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
-					map_len, iter);
+			xfer = write_func(dax_dev, pgoff, kaddr, map_len, iter);
 		else
 			xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
 					map_len, iter);
 
+		if (xfer == (ssize_t) -EIO) {
+			pr_warn("dax_ioma_iter: write_func returns -EIO\n");
+			ret = -EIO;
+			break;
+		}
+
 		pos += xfer;
 		length -= xfer;
 		done += xfer;
Signature · 1 line
2.18.4
#30 lore raw

Re: [PATCH v5 6/7] dax: add recovery_write to dax_iomap_iter in failure path

fs/dax.c static loff_t dax_iomap_iter(const struct iomap_iter *iomi, · line 1215

Quoted 10 lines · Jane Chu
fs/dax.c +3 -0
+typedef size_t (*iter_func_t)(struct dax_device *dax_dev, pgoff_t pgoff,
+		void *addr, size_t bytes, struct iov_iter *i);
 static loff_t dax_iomap_iter(const struct iomap_iter *iomi,
 		struct iov_iter *iter)
 {
@@ -1210,6 +1212,7 @@ static loff_t dax_iomap_iter(const struct iomap_iter *iomi,
 	ssize_t ret = 0;
 	size_t xfer;
 	int id;
+	iter_func_t write_func = dax_copy_from_iter;

This use of a function pointer causes indirect call overhead. A simple "bool in_recovery" or do_recovery does the trick in a way that is both more readable and generates faster code.

fs/dax.c static loff_t dax_iomap_iter(const struct iomap_iter *iomi, · line 1255

Quoted 1 line
fs/dax.c +1 -0
+		if ((map_len == -EIO) && (iov_iter_rw(iter) == WRITE)) {

No need for the braces.

fs/dax.c static loff_t dax_iomap_iter(const struct iomap_iter *iomi, · line 1281

Quoted 7 lines
fs/dax.c +1 -2
 		if (iov_iter_rw(iter) == WRITE)
-			xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
-					map_len, iter);
+			xfer = write_func(dax_dev, pgoff, kaddr, map_len, iter);
fs/dax.c +0 -0
 		else
 			xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
 					map_len, iter);

i.e.

		if (iov_iter_rw(iter) == READ)
			xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
					map_len, iter);
		else if (unlikely(do_recovery))
			xfer = dax_recovery_write(dax_dev, pgoff, kaddr,
					map_len, iter);
		else
			xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
					map_len, iter);
author
#31 lore raw

Re: [PATCH v5 6/7] dax: add recovery_write to dax_iomap_iter in failure path

Quoted 15 lines · Christoph Hellwig

fs/dax.c static loff_t dax_iomap_iter(const struct iomap_iter *iomi, · line 1215

Quoted 10 lines · Jane Chu
fs/dax.c +3 -0
+typedef size_t (*iter_func_t)(struct dax_device *dax_dev, pgoff_t pgoff,
+		void *addr, size_t bytes, struct iov_iter *i);
  static loff_t dax_iomap_iter(const struct iomap_iter *iomi,
  		struct iov_iter *iter)
  {
@@ -1210,6 +1212,7 @@ static loff_t dax_iomap_iter(const struct iomap_iter *iomi,
  	ssize_t ret = 0;
  	size_t xfer;
  	int id;
+	iter_func_t write_func = dax_copy_from_iter;

This use of a function pointer causes indirect call overhead. A simple "bool in_recovery" or do_recovery does the trick in a way that is both more readable and generates faster code.

Good point, thanks!

Quoted 4 lines

fs/dax.c static loff_t dax_iomap_iter(const struct iomap_iter *iomi, · line 1255

Quoted 2 lines
fs/dax.c +1 -0
+		if ((map_len == -EIO) && (iov_iter_rw(iter) == WRITE)) {

No need for the braces.

Did you mean the outer "( )" ?

Quoted 11 lines
Quoted 8 lines
fs/dax.c +1 -2
  		if (iov_iter_rw(iter) == WRITE)
-			xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
-					map_len, iter);
+			xfer = write_func(dax_dev, pgoff, kaddr, map_len, iter);

else
xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
map_len, iter);

i.e.

Quoted 10 lines
		if (iov_iter_rw(iter) == READ)
			xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
					map_len, iter);
		else if (unlikely(do_recovery))
			xfer = dax_recovery_write(dax_dev, pgoff, kaddr,
					map_len, iter);
		else
			xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
					map_len, iter);

Will do.

Thanks a lot!

-jane

author
#32 lore raw

PATCH v5 7/7 pmem: fix pmem_do_write() avoid writing to 'np' page

In reply to Jane Chu

Since poisoned page is marked as not-present, the first of the two back-to-back write_pmem() calls can only be made when there is no poison in the range, otherwise kernel Oops.

---
 drivers/nvdimm/pmem.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/nvdimm/pmem.c +8 -3
index f2d6b34d48de..283890084d58 100644
@@ -187,10 +187,15 @@ static blk_status_t pmem_do_write(struct pmem_device *pmem,
 	 * after clear poison.
 	 */
 	flush_dcache_page(page);
-	write_pmem(pmem_addr, page, page_off, len);
-	if (unlikely(bad_pmem)) {
-		rc = pmem_clear_poison(pmem, pmem_off, len);
+	if (!bad_pmem) {
 		write_pmem(pmem_addr, page, page_off, len);
+	} else {
+		rc = pmem_clear_poison(pmem, pmem_off, len);
+		if (rc == BLK_STS_OK)
+			write_pmem(pmem_addr, page, page_off, len);
+		else
+			pr_warn("%s: failed to clear poison\n",
+				__func__);
 	}
 
 	return rc;
Signature · 1 line
2.18.4
#33 lore raw

Re: [PATCH v5 7/7] pmem: fix pmem_do_write() avoid writing to 'np' page

drivers/nvdimm/pmem.c static blk_status_t pmem_do_write(struct pmem_device *pmem, · line 198

Quoted 9 lines · Jane Chu
drivers/nvdimm/pmem.c +8 -0
+	if (!bad_pmem) {
 		write_pmem(pmem_addr, page, page_off, len);
+	} else {
+		rc = pmem_clear_poison(pmem, pmem_off, len);
+		if (rc == BLK_STS_OK)
+			write_pmem(pmem_addr, page, page_off, len);
+		else
+			pr_warn("%s: failed to clear poison\n",
+				__func__);

This warning probably needs ratelimiting.

Also this flow looks a little odd. I'd redo the whole function with a clear bad_mem case:

	if (unlikely(is_bad_pmem(&pmem->bb, sector, len))) {
		blk_status_t rc = pmem_clear_poison(pmem, pmem_off, len);

		if (rc != BLK_STS_OK) {
			pr_warn("%s: failed to clear poison\n", __func__);
			return rc;
		}
	}
	flush_dcache_page(page);
	write_pmem(pmem_addr, page, page_off, len);
	return BLK_STS_OK;
author
#34 lore raw

Re: [PATCH v5 7/7] pmem: fix pmem_do_write() avoid writing to 'np' page

Quoted 12 lines · Christoph Hellwig

drivers/nvdimm/pmem.c static blk_status_t pmem_do_write(struct pmem_device *pmem, · line 198

Quoted 9 lines · Jane Chu
drivers/nvdimm/pmem.c +8 -0
+	if (!bad_pmem) {
  		write_pmem(pmem_addr, page, page_off, len);
+	} else {
+		rc = pmem_clear_poison(pmem, pmem_off, len);
+		if (rc == BLK_STS_OK)
+			write_pmem(pmem_addr, page, page_off, len);
+		else
+			pr_warn("%s: failed to clear poison\n",
+				__func__);

This warning probably needs ratelimiting.

Will do, in case bad hardware is encountered, I can see lots of warnings.

Quoted 4 lines

Also this flow looks a little odd. I'd redo the whole function with a clear bad_mem case:

Quoted 13 lines
	if (unlikely(is_bad_pmem(&pmem->bb, sector, len))) {
		blk_status_t rc = pmem_clear_poison(pmem, pmem_off, len);

		if (rc != BLK_STS_OK) {
			pr_warn("%s: failed to clear poison\n", __func__);
			return rc;
		}
	}
	flush_dcache_page(page);
	write_pmem(pmem_addr, page, page_off, len);
	return BLK_STS_OK;

This is much better, thanks for the suggestion!

-jane