Skip to content

Commit 4794d94

Browse files
djbwgregkh
authored andcommitted
libnvdimm, pfn: Pad pfn namespaces relative to other regions
commit ae86cbf upstream. 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: <[email protected]> Reported-by: Patrick Geary <[email protected]> Tested-by: Patrick Geary <[email protected]> Reviewed-by: Vishal Verma <[email protected]> Signed-off-by: Dan Williams <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent b919b48 commit 4794d94

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
@@ -105,6 +105,8 @@ resource_size_t nd_pmem_available_dpa(struct nd_region *nd_region,
105105
struct nd_mapping *nd_mapping, resource_size_t *overlap);
106106
resource_size_t nd_blk_available_dpa(struct nd_region *nd_region);
107107
resource_size_t nd_region_available_dpa(struct nd_region *nd_region);
108+
int nd_region_conflict(struct nd_region *nd_region, resource_size_t start,
109+
resource_size_t size);
108110
resource_size_t nvdimm_allocated_dpa(struct nvdimm_drvdata *ndd,
109111
struct nd_label_id *label_id);
110112
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
@@ -589,14 +589,47 @@ static u64 phys_pmem_align_down(struct nd_pfn *nd_pfn, u64 phys)
589589
ALIGN_DOWN(phys, nd_pfn->align));
590590
}
591591

592+
/*
593+
* Check if pmem collides with 'System RAM', or other regions when
594+
* section aligned. Trim it accordingly.
595+
*/
596+
static void trim_pfn_device(struct nd_pfn *nd_pfn, u32 *start_pad, u32 *end_trunc)
597+
{
598+
struct nd_namespace_common *ndns = nd_pfn->ndns;
599+
struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
600+
struct nd_region *nd_region = to_nd_region(nd_pfn->dev.parent);
601+
const resource_size_t start = nsio->res.start;
602+
const resource_size_t end = start + resource_size(&nsio->res);
603+
resource_size_t adjust, size;
604+
605+
*start_pad = 0;
606+
*end_trunc = 0;
607+
608+
adjust = start - PHYS_SECTION_ALIGN_DOWN(start);
609+
size = resource_size(&nsio->res) + adjust;
610+
if (region_intersects(start - adjust, size, IORESOURCE_SYSTEM_RAM,
611+
IORES_DESC_NONE) == REGION_MIXED
612+
|| nd_region_conflict(nd_region, start - adjust, size))
613+
*start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
614+
615+
/* Now check that end of the range does not collide. */
616+
adjust = PHYS_SECTION_ALIGN_UP(end) - end;
617+
size = resource_size(&nsio->res) + adjust;
618+
if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
619+
IORES_DESC_NONE) == REGION_MIXED
620+
|| !IS_ALIGNED(end, nd_pfn->align)
621+
|| nd_region_conflict(nd_region, start, size + adjust))
622+
*end_trunc = end - phys_pmem_align_down(nd_pfn, end);
623+
}
624+
592625
static int nd_pfn_init(struct nd_pfn *nd_pfn)
593626
{
594627
u32 dax_label_reserve = is_nd_dax(&nd_pfn->dev) ? SZ_128K : 0;
595628
struct nd_namespace_common *ndns = nd_pfn->ndns;
596-
u32 start_pad = 0, end_trunc = 0;
629+
struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
597630
resource_size_t start, size;
598-
struct nd_namespace_io *nsio;
599631
struct nd_region *nd_region;
632+
u32 start_pad, end_trunc;
600633
struct nd_pfn_sb *pfn_sb;
601634
unsigned long npfns;
602635
phys_addr_t offset;
@@ -628,30 +661,7 @@ static int nd_pfn_init(struct nd_pfn *nd_pfn)
628661

629662
memset(pfn_sb, 0, sizeof(*pfn_sb));
630663

631-
/*
632-
* Check if pmem collides with 'System RAM' when section aligned and
633-
* trim it accordingly
634-
*/
635-
nsio = to_nd_namespace_io(&ndns->dev);
636-
start = PHYS_SECTION_ALIGN_DOWN(nsio->res.start);
637-
size = resource_size(&nsio->res);
638-
if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
639-
IORES_DESC_NONE) == REGION_MIXED) {
640-
start = nsio->res.start;
641-
start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
642-
}
643-
644-
start = nsio->res.start;
645-
size = PHYS_SECTION_ALIGN_UP(start + size) - start;
646-
if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
647-
IORES_DESC_NONE) == REGION_MIXED
648-
|| !IS_ALIGNED(start + resource_size(&nsio->res),
649-
nd_pfn->align)) {
650-
size = resource_size(&nsio->res);
651-
end_trunc = start + size - phys_pmem_align_down(nd_pfn,
652-
start + size);
653-
}
654-
664+
trim_pfn_device(nd_pfn, &start_pad, &end_trunc);
655665
if (start_pad + end_trunc)
656666
dev_info(&nd_pfn->dev, "%s alignment collision, truncate %d bytes\n",
657667
dev_name(&ndns->dev), start_pad + end_trunc);
@@ -662,7 +672,7 @@ static int nd_pfn_init(struct nd_pfn *nd_pfn)
662672
* implementation will limit the pfns advertised through
663673
* ->direct_access() to those that are included in the memmap.
664674
*/
665-
start += start_pad;
675+
start = nsio->res.start + start_pad;
666676
size = resource_size(&nsio->res);
667677
npfns = PFN_SECTION_ALIGN_UP((size - start_pad - end_trunc - SZ_8K)
668678
/ PAGE_SIZE);

drivers/nvdimm/region_devs.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,47 @@ int nvdimm_has_cache(struct nd_region *nd_region)
11121112
}
11131113
EXPORT_SYMBOL_GPL(nvdimm_has_cache);
11141114

1115+
struct conflict_context {
1116+
struct nd_region *nd_region;
1117+
resource_size_t start, size;
1118+
};
1119+
1120+
static int region_conflict(struct device *dev, void *data)
1121+
{
1122+
struct nd_region *nd_region;
1123+
struct conflict_context *ctx = data;
1124+
resource_size_t res_end, region_end, region_start;
1125+
1126+
if (!is_memory(dev))
1127+
return 0;
1128+
1129+
nd_region = to_nd_region(dev);
1130+
if (nd_region == ctx->nd_region)
1131+
return 0;
1132+
1133+
res_end = ctx->start + ctx->size;
1134+
region_start = nd_region->ndr_start;
1135+
region_end = region_start + nd_region->ndr_size;
1136+
if (ctx->start >= region_start && ctx->start < region_end)
1137+
return -EBUSY;
1138+
if (res_end > region_start && res_end <= region_end)
1139+
return -EBUSY;
1140+
return 0;
1141+
}
1142+
1143+
int nd_region_conflict(struct nd_region *nd_region, resource_size_t start,
1144+
resource_size_t size)
1145+
{
1146+
struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
1147+
struct conflict_context ctx = {
1148+
.nd_region = nd_region,
1149+
.start = start,
1150+
.size = size,
1151+
};
1152+
1153+
return device_for_each_child(&nvdimm_bus->dev, &ctx, region_conflict);
1154+
}
1155+
11151156
void __exit nd_region_devs_exit(void)
11161157
{
11171158
ida_destroy(&region_ida);

0 commit comments

Comments
 (0)