All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com>
To: dan.j.williams@intel.com, mpe@ellerman.id.au
Cc: linux-nvdimm@lists.01.org, linuxppc-dev@lists.ozlabs.org,
	"Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com>
Subject: [RFC PATCH 1/4] libnvdimm/namespace: Make namespace size validation arch dependent
Date: Mon, 28 Oct 2019 15:18:22 +0530	[thread overview]
Message-ID: <20191028094825.21448-1-aneesh.kumar@linux.ibm.com> (raw)

The page size used to map the namespace is arch dependent. For example
architectures like ppc64 use 16MB page size for direct-mapping. If the namespace
size is not aligned to the mapping page size, we can observe kernel crash
during namespace init and destroy.

This is due to kernel doing partial map/unmap of the resource range

BUG: Unable to handle kernel data access at 0xc001000406000000
Faulting instruction address: 0xc000000000090790
NIP [c000000000090790] arch_add_memory+0xc0/0x130
LR [c000000000090744] arch_add_memory+0x74/0x130
Call Trace:
 arch_add_memory+0x74/0x130 (unreliable)
 memremap_pages+0x74c/0xa30
 devm_memremap_pages+0x3c/0xa0
 pmem_attach_disk+0x188/0x770
 nvdimm_bus_probe+0xd8/0x470
 really_probe+0x148/0x570
 driver_probe_device+0x19c/0x1d0
 device_driver_attach+0xcc/0x100
 bind_store+0x134/0x1c0
 drv_attr_store+0x44/0x60
 sysfs_kf_write+0x74/0xc0
 kernfs_fop_write+0x1b4/0x290
 __vfs_write+0x3c/0x70
 vfs_write+0xd0/0x260
 ksys_write+0xdc/0x130
 system_call+0x5c/0x68

Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
---
 arch/arm64/mm/flush.c     | 11 +++++++++++
 arch/powerpc/lib/pmem.c   | 21 +++++++++++++++++++--
 arch/x86/mm/pageattr.c    | 12 ++++++++++++
 include/linux/libnvdimm.h |  1 +
 4 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/mm/flush.c b/arch/arm64/mm/flush.c
index ac485163a4a7..90c54c600023 100644
--- a/arch/arm64/mm/flush.c
+++ b/arch/arm64/mm/flush.c
@@ -91,4 +91,15 @@ void arch_invalidate_pmem(void *addr, size_t size)
 	__inval_dcache_area(addr, size);
 }
 EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
+
+unsigned long arch_validate_namespace_size(unsigned int ndr_mappings, unsigned long size)
+{
+	u32 remainder;
+
+	div_u64_rem(size, PAGE_SIZE * ndr_mappings, &remainder);
+	if (remainder)
+		return PAGE_SIZE * ndr_mappings;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(arch_validate_namespace_size);
 #endif
diff --git a/arch/powerpc/lib/pmem.c b/arch/powerpc/lib/pmem.c
index 377712e85605..2e661a08dae5 100644
--- a/arch/powerpc/lib/pmem.c
+++ b/arch/powerpc/lib/pmem.c
@@ -17,14 +17,31 @@ void arch_wb_cache_pmem(void *addr, size_t size)
 	unsigned long start = (unsigned long) addr;
 	flush_dcache_range(start, start + size);
 }
-EXPORT_SYMBOL(arch_wb_cache_pmem);
+EXPORT_SYMBOL_GPL(arch_wb_cache_pmem);
 
 void arch_invalidate_pmem(void *addr, size_t size)
 {
 	unsigned long start = (unsigned long) addr;
 	flush_dcache_range(start, start + size);
 }
-EXPORT_SYMBOL(arch_invalidate_pmem);
+EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
+
+unsigned long arch_validate_namespace_size(unsigned int ndr_mappings, unsigned long size)
+{
+	u32 remainder;
+	unsigned long linear_map_size;
+
+	if (radix_enabled())
+		linear_map_size = PAGE_SIZE;
+	else
+		linear_map_size = (1UL << mmu_psize_defs[mmu_linear_psize].shift);
+
+	div_u64_rem(size, linear_map_size * ndr_mappings, &remainder);
+	if (remainder)
+		return linear_map_size * ndr_mappings;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(arch_validate_namespace_size);
 
 /*
  * CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE symbols
diff --git a/arch/x86/mm/pageattr.c b/arch/x86/mm/pageattr.c
index 0d09cc5aad61..023329d7dfac 100644
--- a/arch/x86/mm/pageattr.c
+++ b/arch/x86/mm/pageattr.c
@@ -310,6 +310,18 @@ void arch_invalidate_pmem(void *addr, size_t size)
 }
 EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
 
+unsigned long arch_validate_namespace_size(unsigned int ndr_mappings, unsigned long size)
+{
+	u32 remainder;
+
+	div_u64_rem(size, PAGE_SIZE * ndr_mappings, &remainder);
+	if (remainder)
+		return PAGE_SIZE * ndr_mappings;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(arch_validate_namespace_size);
+
+
 static void __cpa_flush_all(void *arg)
 {
 	unsigned long cache = (unsigned long)arg;
diff --git a/include/linux/libnvdimm.h b/include/linux/libnvdimm.h
index b6eddf912568..e2f8387d9ef4 100644
--- a/include/linux/libnvdimm.h
+++ b/include/linux/libnvdimm.h
@@ -291,4 +291,5 @@ static inline void arch_invalidate_pmem(void *addr, size_t size)
 }
 #endif
 
+unsigned long arch_validate_namespace_size(unsigned int ndr_mappings, unsigned long size);
 #endif /* __LIBNVDIMM_H__ */
-- 
2.21.0
_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-leave@lists.01.org

WARNING: multiple messages have this Message-ID (diff)
From: "Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com>
To: dan.j.williams@intel.com, mpe@ellerman.id.au
Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com>,
	linuxppc-dev@lists.ozlabs.org, linux-nvdimm@lists.01.org
Subject: [RFC PATCH 1/4] libnvdimm/namespace: Make namespace size validation arch dependent
Date: Mon, 28 Oct 2019 15:18:22 +0530	[thread overview]
Message-ID: <20191028094825.21448-1-aneesh.kumar@linux.ibm.com> (raw)

The page size used to map the namespace is arch dependent. For example
architectures like ppc64 use 16MB page size for direct-mapping. If the namespace
size is not aligned to the mapping page size, we can observe kernel crash
during namespace init and destroy.

This is due to kernel doing partial map/unmap of the resource range

BUG: Unable to handle kernel data access at 0xc001000406000000
Faulting instruction address: 0xc000000000090790
NIP [c000000000090790] arch_add_memory+0xc0/0x130
LR [c000000000090744] arch_add_memory+0x74/0x130
Call Trace:
 arch_add_memory+0x74/0x130 (unreliable)
 memremap_pages+0x74c/0xa30
 devm_memremap_pages+0x3c/0xa0
 pmem_attach_disk+0x188/0x770
 nvdimm_bus_probe+0xd8/0x470
 really_probe+0x148/0x570
 driver_probe_device+0x19c/0x1d0
 device_driver_attach+0xcc/0x100
 bind_store+0x134/0x1c0
 drv_attr_store+0x44/0x60
 sysfs_kf_write+0x74/0xc0
 kernfs_fop_write+0x1b4/0x290
 __vfs_write+0x3c/0x70
 vfs_write+0xd0/0x260
 ksys_write+0xdc/0x130
 system_call+0x5c/0x68

Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
---
 arch/arm64/mm/flush.c     | 11 +++++++++++
 arch/powerpc/lib/pmem.c   | 21 +++++++++++++++++++--
 arch/x86/mm/pageattr.c    | 12 ++++++++++++
 include/linux/libnvdimm.h |  1 +
 4 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/mm/flush.c b/arch/arm64/mm/flush.c
index ac485163a4a7..90c54c600023 100644
--- a/arch/arm64/mm/flush.c
+++ b/arch/arm64/mm/flush.c
@@ -91,4 +91,15 @@ void arch_invalidate_pmem(void *addr, size_t size)
 	__inval_dcache_area(addr, size);
 }
 EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
+
+unsigned long arch_validate_namespace_size(unsigned int ndr_mappings, unsigned long size)
+{
+	u32 remainder;
+
+	div_u64_rem(size, PAGE_SIZE * ndr_mappings, &remainder);
+	if (remainder)
+		return PAGE_SIZE * ndr_mappings;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(arch_validate_namespace_size);
 #endif
diff --git a/arch/powerpc/lib/pmem.c b/arch/powerpc/lib/pmem.c
index 377712e85605..2e661a08dae5 100644
--- a/arch/powerpc/lib/pmem.c
+++ b/arch/powerpc/lib/pmem.c
@@ -17,14 +17,31 @@ void arch_wb_cache_pmem(void *addr, size_t size)
 	unsigned long start = (unsigned long) addr;
 	flush_dcache_range(start, start + size);
 }
-EXPORT_SYMBOL(arch_wb_cache_pmem);
+EXPORT_SYMBOL_GPL(arch_wb_cache_pmem);
 
 void arch_invalidate_pmem(void *addr, size_t size)
 {
 	unsigned long start = (unsigned long) addr;
 	flush_dcache_range(start, start + size);
 }
-EXPORT_SYMBOL(arch_invalidate_pmem);
+EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
+
+unsigned long arch_validate_namespace_size(unsigned int ndr_mappings, unsigned long size)
+{
+	u32 remainder;
+	unsigned long linear_map_size;
+
+	if (radix_enabled())
+		linear_map_size = PAGE_SIZE;
+	else
+		linear_map_size = (1UL << mmu_psize_defs[mmu_linear_psize].shift);
+
+	div_u64_rem(size, linear_map_size * ndr_mappings, &remainder);
+	if (remainder)
+		return linear_map_size * ndr_mappings;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(arch_validate_namespace_size);
 
 /*
  * CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE symbols
diff --git a/arch/x86/mm/pageattr.c b/arch/x86/mm/pageattr.c
index 0d09cc5aad61..023329d7dfac 100644
--- a/arch/x86/mm/pageattr.c
+++ b/arch/x86/mm/pageattr.c
@@ -310,6 +310,18 @@ void arch_invalidate_pmem(void *addr, size_t size)
 }
 EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
 
+unsigned long arch_validate_namespace_size(unsigned int ndr_mappings, unsigned long size)
+{
+	u32 remainder;
+
+	div_u64_rem(size, PAGE_SIZE * ndr_mappings, &remainder);
+	if (remainder)
+		return PAGE_SIZE * ndr_mappings;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(arch_validate_namespace_size);
+
+
 static void __cpa_flush_all(void *arg)
 {
 	unsigned long cache = (unsigned long)arg;
diff --git a/include/linux/libnvdimm.h b/include/linux/libnvdimm.h
index b6eddf912568..e2f8387d9ef4 100644
--- a/include/linux/libnvdimm.h
+++ b/include/linux/libnvdimm.h
@@ -291,4 +291,5 @@ static inline void arch_invalidate_pmem(void *addr, size_t size)
 }
 #endif
 
+unsigned long arch_validate_namespace_size(unsigned int ndr_mappings, unsigned long size);
 #endif /* __LIBNVDIMM_H__ */
-- 
2.21.0


             reply	other threads:[~2019-10-28  9:48 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-28  9:48 Aneesh Kumar K.V [this message]
2019-10-28  9:48 ` [RFC PATCH 1/4] libnvdimm/namespace: Make namespace size validation arch dependent Aneesh Kumar K.V
2019-10-28  9:48 ` [RFC PATCH 2/4] libnvdimm/namespace: Disable the region if the namespace size is not aligned correctly Aneesh Kumar K.V
2019-10-28  9:48   ` Aneesh Kumar K.V
2019-10-30  9:12   ` kbuild test robot
2019-10-28  9:48 ` [RFC PATCH 3/4] libnvdimm/namespace: Use direct-mapping page size to validate namespace size Aneesh Kumar K.V
2019-10-28  9:48   ` Aneesh Kumar K.V
2019-10-28  9:48 ` [RFC PATCH 4/4] libnvdimm/namespace: Add debug check while initializing namespace resource size Aneesh Kumar K.V
2019-10-28  9:48   ` Aneesh Kumar K.V
2019-10-28 21:21 ` [RFC PATCH 1/4] libnvdimm/namespace: Make namespace size validation arch dependent Ira Weiny
2019-10-28 21:21   ` Ira Weiny
2019-10-29  4:34   ` Aneesh Kumar K.V
2019-10-29  4:34     ` Aneesh Kumar K.V
2019-10-28 23:08 ` Dan Williams
2019-10-28 23:08   ` Dan Williams
2019-10-29  4:34   ` Aneesh Kumar K.V
2019-10-29  4:34     ` Aneesh Kumar K.V
2019-10-29  5:30     ` Dan Williams
2019-10-29  5:30       ` Dan Williams
2019-10-31  5:35       ` Aneesh Kumar K.V
2019-10-31  5:35         ` Aneesh Kumar K.V
2019-10-31  6:30         ` Dan Williams
2019-10-31  6:30           ` Dan Williams
2019-10-31  8:37           ` Aneesh Kumar K.V
2019-10-31  8:37             ` Aneesh Kumar K.V
2019-10-31 15:53             ` Dan Williams
2019-10-31 15:53               ` Dan Williams
2019-11-06 10:44           ` Aneesh Kumar K.V
2019-11-06 10:44             ` Aneesh Kumar K.V
2019-11-16 12:15             ` Aneesh Kumar K.V
2019-11-16 12:15               ` Aneesh Kumar K.V
2019-11-16 18:50               ` Dan Williams
2019-11-16 18:50                 ` Dan Williams
2019-11-18  9:51                 ` Aneesh Kumar K.V
2019-11-18  9:51                   ` Aneesh Kumar K.V
2019-11-19 17:58                   ` Dan Williams
2019-11-19 17:58                     ` Dan Williams
2019-11-20  3:19                     ` Aneesh Kumar K.V
2019-11-20  3:19                       ` Aneesh Kumar K.V
2019-11-20  3:46                       ` Dan Williams
2019-11-20  3:46                         ` Dan Williams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191028094825.21448-1-aneesh.kumar@linux.ibm.com \
    --to=aneesh.kumar@linux.ibm.com \
    --cc=dan.j.williams@intel.com \
    --cc=linux-nvdimm@lists.01.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=mpe@ellerman.id.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.