Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libnvdimm/namespace: Enforce memremap_compat_align()

The pmem driver on PowerPC crashes with the following signature when
instantiating misaligned namespaces that map their capacity via
memremap_pages().

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

With the assumption that only memremap_pages() has alignment
constraints, enforce memremap_compat_align() for
pmem_should_map_pages(), nd_pfn, and nd_dax cases. This includes
preventing the creation of namespaces where the base address is
misaligned and cases there infoblock padding parameters are invalid.

Reported-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Cc: Jeff Moyer <jmoyer@redhat.com>
Fixes: a3619190d62e ("libnvdimm/pfn: stop padding pmem namespaces to section alignment")
Reviewed-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>

+58 -3
+17
drivers/nvdimm/namespace_devs.c
··· 10 10 #include <linux/nd.h> 11 11 #include "nd-core.h" 12 12 #include "pmem.h" 13 + #include "pfn.h" 13 14 #include "nd.h" 14 15 15 16 static void namespace_io_release(struct device *dev) ··· 1738 1737 dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n", 1739 1738 &size, ND_MIN_NAMESPACE_SIZE); 1740 1739 return ERR_PTR(-ENODEV); 1740 + } 1741 + 1742 + /* 1743 + * Note, alignment validation for fsdax and devdax mode 1744 + * namespaces happens in nd_pfn_validate() where infoblock 1745 + * padding parameters can be applied. 1746 + */ 1747 + if (pmem_should_map_pages(dev)) { 1748 + struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); 1749 + struct resource *res = &nsio->res; 1750 + 1751 + if (!IS_ALIGNED(res->start | (res->end + 1), 1752 + memremap_compat_align())) { 1753 + dev_err(&ndns->dev, "%pr misaligned, unable to map\n", res); 1754 + return ERR_PTR(-EOPNOTSUPP); 1755 + } 1741 1756 } 1742 1757 1743 1758 if (is_namespace_pmem(&ndns->dev)) {
+12
drivers/nvdimm/pfn.h
··· 24 24 __le64 npfns; 25 25 __le32 mode; 26 26 /* minor-version-1 additions for section alignment */ 27 + /** 28 + * @start_pad: Deprecated attribute to pad start-misaligned namespaces 29 + * 30 + * start_pad is deprecated because the original definition did 31 + * not comprehend that dataoff is relative to the base address 32 + * of the namespace not the start_pad adjusted base. The result 33 + * is that the dax path is broken, but the block-I/O path is 34 + * not. The kernel will no longer create namespaces using start 35 + * padding, but it still supports block-I/O for legacy 36 + * configurations mainly to allow a backup, reconfigure the 37 + * namespace, and restore flow to repair dax operation. 38 + */ 27 39 __le32 start_pad; 28 40 __le32 end_trunc; 29 41 /* minor-version-2 record the base alignment of the mapping */
+29 -3
drivers/nvdimm/pfn_devs.c
··· 446 446 int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig) 447 447 { 448 448 u64 checksum, offset; 449 + struct resource *res; 449 450 enum nd_pfn_mode mode; 450 451 struct nd_namespace_io *nsio; 451 452 unsigned long align, start_pad; ··· 579 578 * established. 580 579 */ 581 580 nsio = to_nd_namespace_io(&ndns->dev); 582 - if (offset >= resource_size(&nsio->res)) { 581 + res = &nsio->res; 582 + if (offset >= resource_size(res)) { 583 583 dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n", 584 584 dev_name(&ndns->dev)); 585 585 return -EOPNOTSUPP; 586 586 } 587 587 588 - if ((align && !IS_ALIGNED(nsio->res.start + offset + start_pad, align)) 588 + if ((align && !IS_ALIGNED(res->start + offset + start_pad, align)) 589 589 || !IS_ALIGNED(offset, PAGE_SIZE)) { 590 590 dev_err(&nd_pfn->dev, 591 591 "bad offset: %#llx dax disabled align: %#lx\n", 592 592 offset, align); 593 + return -EOPNOTSUPP; 594 + } 595 + 596 + if (!IS_ALIGNED(res->start + le32_to_cpu(pfn_sb->start_pad), 597 + memremap_compat_align())) { 598 + dev_err(&nd_pfn->dev, "resource start misaligned\n"); 599 + return -EOPNOTSUPP; 600 + } 601 + 602 + if (!IS_ALIGNED(res->end + 1 - le32_to_cpu(pfn_sb->end_trunc), 603 + memremap_compat_align())) { 604 + dev_err(&nd_pfn->dev, "resource end misaligned\n"); 593 605 return -EOPNOTSUPP; 594 606 } 595 607 ··· 764 750 start = nsio->res.start; 765 751 size = resource_size(&nsio->res); 766 752 npfns = PHYS_PFN(size - SZ_8K); 767 - align = max(nd_pfn->align, SUBSECTION_SIZE); 753 + align = max(nd_pfn->align, memremap_compat_align()); 754 + 755 + /* 756 + * When @start is misaligned fail namespace creation. See 757 + * the 'struct nd_pfn_sb' commentary on why ->start_pad is not 758 + * an option. 759 + */ 760 + if (!IS_ALIGNED(start, memremap_compat_align())) { 761 + dev_err(&nd_pfn->dev, "%s: start %pa misaligned to %#lx\n", 762 + dev_name(&ndns->dev), &start, 763 + memremap_compat_align()); 764 + return -EINVAL; 765 + } 768 766 end_trunc = start + size - ALIGN_DOWN(start + size, align); 769 767 if (nd_pfn->mode == PFN_MODE_PMEM) { 770 768 /*