<div dir="ltr"><div dir="ltr"></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Apr 1, 2020 at 1:18 PM Santosh Sivaraj <<a href="mailto:santosh@fossix.org" target="_blank">santosh@fossix.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Subscribe to the MCE notification and add the physical address which<br>
generated a memory error to nvdimm bad range.<br>
<br>
Signed-off-by: Santosh Sivaraj <<a href="mailto:santosh@fossix.org" target="_blank">santosh@fossix.org</a>><br>
---<br></blockquote><div><br></div><div>Any comments on this?</div><div><br></div><div>Thanks,</div><div>Santosh</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
This patch depends on "powerpc/mce: Add MCE notification chain" [1].<br>
<br>
Unlike the previous series[2], the patch adds badblock registration only for<br>
pseries scm driver. Handling badblocks for baremetal (powernv) PMEM will be done<br>
later and if possible get the badblock handling as a common code.<br>
<br>
[1] <a href="https://lore.kernel.org/linuxppc-dev/20200330071219.12284-1-ganeshgr@linux.ibm.com/" rel="noreferrer" target="_blank">https://lore.kernel.org/linuxppc-dev/20200330071219.12284-1-ganeshgr@linux.ibm.com/</a><br>
[2] <a href="https://lore.kernel.org/linuxppc-dev/20190820023030.18232-1-santosh@fossix.org/" rel="noreferrer" target="_blank">https://lore.kernel.org/linuxppc-dev/20190820023030.18232-1-santosh@fossix.org/</a><br>
<br>
arch/powerpc/platforms/pseries/papr_scm.c | 96 ++++++++++++++++++++++-<br>
 1 file changed, 95 insertions(+), 1 deletion(-)<br>
<br>
diff --git a/arch/powerpc/platforms/pseries/papr_scm.c b/arch/powerpc/platforms/pseries/papr_scm.c<br>
index 0b4467e378e5..5012cbf4606e 100644<br>
--- a/arch/powerpc/platforms/pseries/papr_scm.c<br>
+++ b/arch/powerpc/platforms/pseries/papr_scm.c<br>
@@ -12,6 +12,8 @@<br>
 #include <linux/libnvdimm.h><br>
 #include <linux/platform_device.h><br>
 #include <linux/delay.h><br>
+#include <linux/nd.h><br>
+#include <asm/mce.h><br>
<br>
 #include <asm/plpar_wrappers.h><br>
<br>
@@ -39,8 +41,12 @@ struct papr_scm_priv {<br>
        struct resource res;<br>
        struct nd_region *region;<br>
        struct nd_interleave_set nd_set;<br>
+       struct list_head region_list;<br>
 };<br>
<br>
+LIST_HEAD(papr_nd_regions);<br>
+DEFINE_MUTEX(papr_ndr_lock);<br>
+<br>
 static int drc_pmem_bind(struct papr_scm_priv *p)<br>
 {<br>
        unsigned long ret[PLPAR_HCALL_BUFSIZE];<br>
@@ -372,6 +378,10 @@ static int papr_scm_nvdimm_init(struct papr_scm_priv *p)<br>
                dev_info(dev, "Region registered with target node %d and online node %d",<br>
                         target_nid, online_nid);<br>
<br>
+       mutex_lock(&papr_ndr_lock);<br>
+       list_add_tail(&p->region_list, &papr_nd_regions);<br>
+       mutex_unlock(&papr_ndr_lock);<br>
+<br>
        return 0;<br>
<br>
 err:   nvdimm_bus_unregister(p->bus);<br>
@@ -379,6 +389,68 @@ err:       nvdimm_bus_unregister(p->bus);<br>
        return -ENXIO;<br>
 }<br>
<br>
+void papr_scm_add_badblock(struct nd_region *region, struct nvdimm_bus *bus,<br>
+                          u64 phys_addr)<br>
+{<br>
+       u64 aligned_addr = ALIGN_DOWN(phys_addr, L1_CACHE_BYTES);<br>
+<br>
+       if (nvdimm_bus_add_badrange(bus, aligned_addr, L1_CACHE_BYTES)) {<br>
+               pr_err("Bad block registration for 0x%llx failed\n", phys_addr);<br>
+               return;<br>
+       }<br>
+<br>
+       pr_debug("Add memory range (0x%llx - 0x%llx) as bad range\n",<br>
+                aligned_addr, aligned_addr + L1_CACHE_BYTES);<br>
+<br>
+       nvdimm_region_notify(region, NVDIMM_REVALIDATE_POISON);<br>
+}<br>
+<br>
+static int handle_mce_ue(struct notifier_block *nb, unsigned long val,<br>
+                        void *data)<br>
+{<br>
+       struct machine_check_event *evt = data;<br>
+       struct papr_scm_priv *p;<br>
+       u64 phys_addr;<br>
+       bool found = false;<br>
+<br>
+       if (evt->error_type != MCE_ERROR_TYPE_UE)<br>
+               return NOTIFY_DONE;<br>
+<br>
+       if (list_empty(&papr_nd_regions))<br>
+               return NOTIFY_DONE;<br>
+<br>
+       phys_addr = evt->u.ue_error.physical_address +<br>
+               (evt->u.ue_error.effective_address & ~PAGE_MASK);<br>
+<br>
+       if (!evt->u.ue_error.physical_address_provided ||<br>
+           !is_zone_device_page(pfn_to_page(phys_addr >> PAGE_SHIFT)))<br>
+               return NOTIFY_DONE;<br>
+<br>
+       /* mce notifier is called from a process context, so mutex is safe */<br>
+       mutex_lock(&papr_ndr_lock);<br>
+       list_for_each_entry(p, &papr_nd_regions, region_list) {<br>
+               struct resource res = p->res;<br>
+<br>
+               if (phys_addr >= res.start && phys_addr <= res.end) {<br>
+                       found = true;<br>
+                       break;<br>
+               }<br>
+       }<br>
+<br>
+       mutex_unlock(&papr_ndr_lock);<br>
+<br>
+       if (!found)<br>
+               return NOTIFY_DONE;<br>
+<br>
+       papr_scm_add_badblock(p->region, p->bus, phys_addr);<br>
+<br>
+       return NOTIFY_OK;<br>
+}<br>
+<br>
+static struct notifier_block mce_ue_nb = {<br>
+       .notifier_call = handle_mce_ue<br>
+};<br>
+<br>
 static int papr_scm_probe(struct platform_device *pdev)<br>
 {<br>
        struct device_node *dn = pdev->dev.of_node;<br>
@@ -476,6 +548,10 @@ static int papr_scm_remove(struct platform_device *pdev)<br>
 {<br>
        struct papr_scm_priv *p = platform_get_drvdata(pdev);<br>
<br>
+       mutex_lock(&papr_ndr_lock);<br>
+       list_del(&(p->region_list));<br>
+       mutex_unlock(&papr_ndr_lock);<br>
+<br>
        nvdimm_bus_unregister(p->bus);<br>
        drc_pmem_unbind(p);<br>
        kfree(p->bus_desc.provider_name);<br>
@@ -498,7 +574,25 @@ static struct platform_driver papr_scm_driver = {<br>
        },<br>
 };<br>
<br>
-module_platform_driver(papr_scm_driver);<br>
+static int __init papr_scm_init(void)<br>
+{<br>
+       int ret;<br>
+<br>
+       ret = platform_driver_register(&papr_scm_driver);<br>
+       if (!ret)<br>
+               mce_register_notifier(&mce_ue_nb);<br>
+<br>
+       return ret;<br>
+}<br>
+module_init(papr_scm_init);<br>
+<br>
+static void __exit papr_scm_exit(void)<br>
+{<br>
+       mce_unregister_notifier(&mce_ue_nb);<br>
+       platform_driver_unregister(&papr_scm_driver);<br>
+}<br>
+module_exit(papr_scm_exit);<br>
+<br>
 MODULE_DEVICE_TABLE(of, papr_scm_match);<br>
 MODULE_LICENSE("GPL");<br>
 MODULE_AUTHOR("IBM Corporation");<br>
-- <br>
2.25.1<br>
<br>
</blockquote></div><br></div>