Commit 7cc7867f authored by Dan Williams's avatar Dan Williams Committed by Linus Torvalds

mm/devm_memremap_pages: enable sub-section remap

Teach devm_memremap_pages() about the new sub-section capabilities of
arch_{add,remove}_memory().  Effectively, just replace all usage of
align_start, align_end, and align_size with res->start, res->end, and
resource_size(res).  The existing sanity check will still make sure that
the two separate remap attempts do not collide within a sub-section (2MB
on x86).

Link: http://lkml.kernel.org/r/156092355542.979959.10060071713397030576.stgit@dwillia2-desk3.amr.corp.intel.comSigned-off-by: default avatarDan Williams <dan.j.williams@intel.com>
Tested-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>	[ppc64]
Cc: Michal Hocko <mhocko@suse.com>
Cc: Toshi Kani <toshi.kani@hpe.com>
Cc: Jérôme Glisse <jglisse@redhat.com>
Cc: Logan Gunthorpe <logang@deltatee.com>
Cc: Oscar Salvador <osalvador@suse.de>
Cc: Pavel Tatashin <pasha.tatashin@soleen.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Jane Chu <jane.chu@oracle.com>
Cc: Jeff Moyer <jmoyer@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Mike Rapoport <rppt@linux.ibm.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Wei Yang <richardw.yang@linux.intel.com>
Cc: Jason Gunthorpe <jgg@mellanox.com>
Cc: Christoph Hellwig <hch@lst.de>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent a0653406
...@@ -54,7 +54,7 @@ static void pgmap_array_delete(struct resource *res) ...@@ -54,7 +54,7 @@ static void pgmap_array_delete(struct resource *res)
static unsigned long pfn_first(struct dev_pagemap *pgmap) static unsigned long pfn_first(struct dev_pagemap *pgmap)
{ {
return (pgmap->res.start >> PAGE_SHIFT) + return PHYS_PFN(pgmap->res.start) +
vmem_altmap_offset(pgmap_altmap(pgmap)); vmem_altmap_offset(pgmap_altmap(pgmap));
} }
...@@ -98,7 +98,6 @@ static void devm_memremap_pages_release(void *data) ...@@ -98,7 +98,6 @@ static void devm_memremap_pages_release(void *data)
struct dev_pagemap *pgmap = data; struct dev_pagemap *pgmap = data;
struct device *dev = pgmap->dev; struct device *dev = pgmap->dev;
struct resource *res = &pgmap->res; struct resource *res = &pgmap->res;
resource_size_t align_start, align_size;
unsigned long pfn; unsigned long pfn;
int nid; int nid;
...@@ -108,25 +107,21 @@ static void devm_memremap_pages_release(void *data) ...@@ -108,25 +107,21 @@ static void devm_memremap_pages_release(void *data)
dev_pagemap_cleanup(pgmap); dev_pagemap_cleanup(pgmap);
/* pages are dead and unused, undo the arch mapping */ /* pages are dead and unused, undo the arch mapping */
align_start = res->start & ~(SECTION_SIZE - 1); nid = page_to_nid(pfn_to_page(PHYS_PFN(res->start)));
align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
- align_start;
nid = page_to_nid(pfn_to_page(align_start >> PAGE_SHIFT));
mem_hotplug_begin(); mem_hotplug_begin();
if (pgmap->type == MEMORY_DEVICE_PRIVATE) { if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
pfn = align_start >> PAGE_SHIFT; pfn = PHYS_PFN(res->start);
__remove_pages(page_zone(pfn_to_page(pfn)), pfn, __remove_pages(page_zone(pfn_to_page(pfn)), pfn,
align_size >> PAGE_SHIFT, NULL); PHYS_PFN(resource_size(res)), NULL);
} else { } else {
arch_remove_memory(nid, align_start, align_size, arch_remove_memory(nid, res->start, resource_size(res),
pgmap_altmap(pgmap)); pgmap_altmap(pgmap));
kasan_remove_zero_shadow(__va(align_start), align_size); kasan_remove_zero_shadow(__va(res->start), resource_size(res));
} }
mem_hotplug_done(); mem_hotplug_done();
untrack_pfn(NULL, PHYS_PFN(align_start), align_size); untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
pgmap_array_delete(res); pgmap_array_delete(res);
dev_WARN_ONCE(dev, pgmap->altmap.alloc, dev_WARN_ONCE(dev, pgmap->altmap.alloc,
"%s: failed to free all reserved pages\n", __func__); "%s: failed to free all reserved pages\n", __func__);
...@@ -162,13 +157,12 @@ static void dev_pagemap_percpu_release(struct percpu_ref *ref) ...@@ -162,13 +157,12 @@ static void dev_pagemap_percpu_release(struct percpu_ref *ref)
*/ */
void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
{ {
resource_size_t align_start, align_size, align_end;
struct resource *res = &pgmap->res; struct resource *res = &pgmap->res;
struct dev_pagemap *conflict_pgmap; struct dev_pagemap *conflict_pgmap;
struct mhp_restrictions restrictions = { struct mhp_restrictions restrictions = {
/* /*
* We do not want any optional features only our own memmap * We do not want any optional features only our own memmap
*/ */
.altmap = pgmap_altmap(pgmap), .altmap = pgmap_altmap(pgmap),
}; };
pgprot_t pgprot = PAGE_KERNEL; pgprot_t pgprot = PAGE_KERNEL;
...@@ -225,12 +219,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -225,12 +219,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
return ERR_PTR(error); return ERR_PTR(error);
} }
align_start = res->start & ~(SECTION_SIZE - 1); conflict_pgmap = get_dev_pagemap(PHYS_PFN(res->start), NULL);
align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
- align_start;
align_end = align_start + align_size - 1;
conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_start), NULL);
if (conflict_pgmap) { if (conflict_pgmap) {
dev_WARN(dev, "Conflicting mapping in same section\n"); dev_WARN(dev, "Conflicting mapping in same section\n");
put_dev_pagemap(conflict_pgmap); put_dev_pagemap(conflict_pgmap);
...@@ -238,7 +227,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -238,7 +227,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
goto err_array; goto err_array;
} }
conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_end), NULL); conflict_pgmap = get_dev_pagemap(PHYS_PFN(res->end), NULL);
if (conflict_pgmap) { if (conflict_pgmap) {
dev_WARN(dev, "Conflicting mapping in same section\n"); dev_WARN(dev, "Conflicting mapping in same section\n");
put_dev_pagemap(conflict_pgmap); put_dev_pagemap(conflict_pgmap);
...@@ -246,7 +235,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -246,7 +235,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
goto err_array; goto err_array;
} }
is_ram = region_intersects(align_start, align_size, is_ram = region_intersects(res->start, resource_size(res),
IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE); IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
if (is_ram != REGION_DISJOINT) { if (is_ram != REGION_DISJOINT) {
...@@ -267,8 +256,8 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -267,8 +256,8 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
if (nid < 0) if (nid < 0)
nid = numa_mem_id(); nid = numa_mem_id();
error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(align_start), 0, error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(res->start), 0,
align_size); resource_size(res));
if (error) if (error)
goto err_pfn_remap; goto err_pfn_remap;
...@@ -286,16 +275,16 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -286,16 +275,16 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
* arch_add_memory(). * arch_add_memory().
*/ */
if (pgmap->type == MEMORY_DEVICE_PRIVATE) { if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
error = add_pages(nid, align_start >> PAGE_SHIFT, error = add_pages(nid, PHYS_PFN(res->start),
align_size >> PAGE_SHIFT, &restrictions); PHYS_PFN(resource_size(res)), &restrictions);
} else { } else {
error = kasan_add_zero_shadow(__va(align_start), align_size); error = kasan_add_zero_shadow(__va(res->start), resource_size(res));
if (error) { if (error) {
mem_hotplug_done(); mem_hotplug_done();
goto err_kasan; goto err_kasan;
} }
error = arch_add_memory(nid, align_start, align_size, error = arch_add_memory(nid, res->start, resource_size(res),
&restrictions); &restrictions);
} }
...@@ -303,8 +292,8 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -303,8 +292,8 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
struct zone *zone; struct zone *zone;
zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE]; zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
move_pfn_range_to_zone(zone, align_start >> PAGE_SHIFT, move_pfn_range_to_zone(zone, PHYS_PFN(res->start),
align_size >> PAGE_SHIFT, pgmap_altmap(pgmap)); PHYS_PFN(resource_size(res)), restrictions.altmap);
} }
mem_hotplug_done(); mem_hotplug_done();
...@@ -316,8 +305,8 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -316,8 +305,8 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
* to allow us to do the work while not holding the hotplug lock. * to allow us to do the work while not holding the hotplug lock.
*/ */
memmap_init_zone_device(&NODE_DATA(nid)->node_zones[ZONE_DEVICE], memmap_init_zone_device(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
align_start >> PAGE_SHIFT, PHYS_PFN(res->start),
align_size >> PAGE_SHIFT, pgmap); PHYS_PFN(resource_size(res)), pgmap);
percpu_ref_get_many(pgmap->ref, pfn_end(pgmap) - pfn_first(pgmap)); percpu_ref_get_many(pgmap->ref, pfn_end(pgmap) - pfn_first(pgmap));
error = devm_add_action_or_reset(dev, devm_memremap_pages_release, error = devm_add_action_or_reset(dev, devm_memremap_pages_release,
...@@ -328,9 +317,9 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -328,9 +317,9 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
return __va(res->start); return __va(res->start);
err_add_memory: err_add_memory:
kasan_remove_zero_shadow(__va(align_start), align_size); kasan_remove_zero_shadow(__va(res->start), resource_size(res));
err_kasan: err_kasan:
untrack_pfn(NULL, PHYS_PFN(align_start), align_size); untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
err_pfn_remap: err_pfn_remap:
pgmap_array_delete(res); pgmap_array_delete(res);
err_array: err_array:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment