Skip to content

Commit ae86cbf

Browse files
committed
libnvdimm, pfn: Pad pfn namespaces relative to other regions
Commit cfe30b8 "libnvdimm, pmem: adjust for section collisions with 'System RAM'" enabled Linux to workaround occasions where platform firmware arranges for "System RAM" and "Persistent Memory" to collide within a single section boundary. Unfortunately, as reported in this issue [1], platform firmware can inflict the same collision between persistent memory regions. The approach of interrogating iomem_resource does not work in this case because platform firmware may merge multiple regions into a single iomem_resource range. Instead provide a method to interrogate regions that share the same parent bus. This is a stop-gap until the core-MM can grow support for hotplug on sub-section boundaries. [1]: pmem/ndctl#76 Fixes: cfe30b8 ("libnvdimm, pmem: adjust for section collisions with...") Cc: <stable@vger.kernel.org> Reported-by: Patrick Geary <patrickg@supermicro.com> Tested-by: Patrick Geary <patrickg@supermicro.com> Reviewed-by: Vishal Verma <vishal.l.verma@intel.com> Signed-off-by: Dan Williams <dan.j.williams@intel.com>
1 parent e3f5df7 commit ae86cbf

File tree

3 files changed

+80
-27
lines changed

3 files changed

+80
-27
lines changed

drivers/nvdimm/nd-core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
111111
struct nd_mapping *nd_mapping, resource_size_t *overlap);
112112
resource_size_t nd_blk_available_dpa(struct nd_region *nd_region);
113113
resource_size_t nd_region_available_dpa(struct nd_region *nd_region);
114+
int nd_region_conflict(struct nd_region *nd_region, resource_size_t start,
115+
resource_size_t size);
114116
resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
115117
struct nd_label_id *label_id);
116118
int alias_dpa_busy(struct device *dev, void *data);

drivers/nvdimm/pfn_devs.c

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -649,14 +649,47 @@ static u64 phys_pmem_align_down(struct nd_pfn *nd_pfn, u64 phys)
649649
ALIGN_DOWN(phys, nd_pfn->align));
650650
}
651651

652+
/*
653+
* Check if pmem collides with 'System RAM', or other regions when
654+
* section aligned. Trim it accordingly.
655+
*/
656+
static void trim_pfn_device(struct nd_pfn *nd_pfn, u32 *start_pad, u32 *end_trunc)
657+
{
658+
struct nd_namespace_common *ndns = nd_pfn->ndns;
659+
struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
660+
struct nd_region *nd_region = to_nd_region(nd_pfn->dev.parent);
661+
const resource_size_t start = nsio->res.start;
662+
const resource_size_t end = start + resource_size(&nsio->res);
663+
resource_size_t adjust, size;
664+
665+
*start_pad = 0;
666+
*end_trunc = 0;
667+
668+
adjust = start - PHYS_SECTION_ALIGN_DOWN(start);
669+
size = resource_size(&nsio->res) + adjust;
670+
if (region_intersects(start - adjust, size, IORESOURCE_SYSTEM_RAM,
671+
IORES_DESC_NONE) == REGION_MIXED
672+
|| nd_region_conflict(nd_region, start - adjust, size))
673+
*start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
674+
675+
/* Now check that end of the range does not collide. */
676+
adjust = PHYS_SECTION_ALIGN_UP(end) - end;
677+
size = resource_size(&nsio->res) + adjust;
678+
if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
679+
IORES_DESC_NONE) == REGION_MIXED
680+
|| !IS_ALIGNED(end, nd_pfn->align)
681+
|| nd_region_conflict(nd_region, start, size + adjust))
682+
*end_trunc = end - phys_pmem_align_down(nd_pfn, end);
683+
}
684+
652685
static int nd_pfn_init(struct nd_pfn *nd_pfn)
653686
{
654687
u32 dax_label_reserve = is_nd_dax(&nd_pfn->dev) ? SZ_128K : 0;
655688
struct nd_namespace_common *ndns = nd_pfn->ndns;
656-
u32 start_pad = 0, end_trunc = 0;
689+
struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
657690
resource_size_t start, size;
658-
struct nd_namespace_io *nsio;
659691
struct nd_region *nd_region;
692+
u32 start_pad, end_trunc;
660693
struct nd_pfn_sb *pfn_sb;
661694
unsigned long npfns;
662695
phys_addr_t offset;
@@ -688,30 +721,7 @@ static int nd_pfn_init(struct nd_pfn *nd_pfn)
688721

689722
memset(pfn_sb, 0, sizeof(*pfn_sb));
690723

691-
/*
692-
* Check if pmem collides with 'System RAM' when section aligned and
693-
* trim it accordingly
694-
*/
695-
nsio = to_nd_namespace_io(&ndns->dev);
696-
start = PHYS_SECTION_ALIGN_DOWN(nsio->res.start);
697-
size = resource_size(&nsio->res);
698-
if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
699-
IORES_DESC_NONE) == REGION_MIXED) {
700-
start = nsio->res.start;
701-
start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
702-
}
703-
704-
start = nsio->res.start;
705-
size = PHYS_SECTION_ALIGN_UP(start + size) - start;
706-
if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
707-
IORES_DESC_NONE) == REGION_MIXED
708-
|| !IS_ALIGNED(start + resource_size(&nsio->res),
709-
nd_pfn->align)) {
710-
size = resource_size(&nsio->res);
711-
end_trunc = start + size - phys_pmem_align_down(nd_pfn,
712-
start + size);
713-
}
714-
724+
trim_pfn_device(nd_pfn, &start_pad, &end_trunc);
715725
if (start_pad + end_trunc)
716726
dev_info(&nd_pfn->dev, "%s alignment collision, truncate %d bytes\n",
717727
dev_name(&ndns->dev), start_pad + end_trunc);
@@ -722,7 +732,7 @@ static int nd_pfn_init(struct nd_pfn *nd_pfn)
722732
* implementation will limit the pfns advertised through
723733
* ->direct_access() to those that are included in the memmap.
724734
*/
725-
start += start_pad;
735+
start = nsio->res.start + start_pad;
726736
size = resource_size(&nsio->res);
727737
npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K)
728738
/ PAGE_SIZE);

drivers/nvdimm/region_devs.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,47 @@ int nvdimm_has_cache(struct nd_region *nd_region)
11841184
}
11851185
EXPORT_SYMBOL_GPL(nvdimm_has_cache);
11861186

1187+
struct conflict_context {
1188+
struct nd_region *nd_region;
1189+
resource_size_t start, size;
1190+
};
1191+
1192+
static int region_conflict(struct device *dev, void *data)
1193+
{
1194+
struct nd_region *nd_region;
1195+
struct conflict_context *ctx = data;
1196+
resource_size_t res_end, region_end, region_start;
1197+
1198+
if (!is_memory(dev))
1199+
return 0;
1200+
1201+
nd_region = to_nd_region(dev);
1202+
if (nd_region == ctx->nd_region)
1203+
return 0;
1204+
1205+
res_end = ctx->start + ctx->size;
1206+
region_start = nd_region->ndr_start;
1207+
region_end = region_start + nd_region->ndr_size;
1208+
if (ctx->start >= region_start && ctx->start < region_end)
1209+
return -EBUSY;
1210+
if (res_end > region_start && res_end <= region_end)
1211+
return -EBUSY;
1212+
return 0;
1213+
}
1214+
1215+
int nd_region_conflict(struct nd_region *nd_region, resource_size_t start,
1216+
resource_size_t size)
1217+
{
1218+
struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
1219+
struct conflict_context ctx = {
1220+
.nd_region = nd_region,
1221+
.start = start,
1222+
.size = size,
1223+
};
1224+
1225+
return device_for_each_child(&nvdimm_bus->dev, &ctx, region_conflict);
1226+
}
1227+
11871228
void __exit nd_region_devs_exit(void)
11881229
{
11891230
ida_destroy(&region_ida);

0 commit comments

Comments
 (0)