Commit 6869b7b2 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Jason Gunthorpe

memremap: provide a not device managed memremap_pages

The kvmppc ultravisor code wants a device private memory pool that is
system wide and not attached to a device.  Instead of faking up one
provide a low-level memremap_pages for it.

Link: https://lore.kernel.org/r/20190818090557.17853-5-hch@lst.deSigned-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarIra Weiny <ira.weiny@intel.com>
Reviewed-by: default avatarDan Williams <dan.j.williams@intel.com>
Tested-by: default avatarBharata B Rao <bharata@linux.ibm.com>
Reviewed-by: default avatarJason Gunthorpe <jgg@mellanox.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@mellanox.com>
parent 6f42193f
...@@ -123,6 +123,8 @@ static inline struct vmem_altmap *pgmap_altmap(struct dev_pagemap *pgmap) ...@@ -123,6 +123,8 @@ static inline struct vmem_altmap *pgmap_altmap(struct dev_pagemap *pgmap)
} }
#ifdef CONFIG_ZONE_DEVICE #ifdef CONFIG_ZONE_DEVICE
void *memremap_pages(struct dev_pagemap *pgmap, int nid);
void memunmap_pages(struct dev_pagemap *pgmap);
void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap); void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap);
void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap); void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap);
struct dev_pagemap *get_dev_pagemap(unsigned long pfn, struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
......
...@@ -96,9 +96,8 @@ static void dev_pagemap_cleanup(struct dev_pagemap *pgmap) ...@@ -96,9 +96,8 @@ static void dev_pagemap_cleanup(struct dev_pagemap *pgmap)
} }
} }
static void devm_memremap_pages_release(void *data) void memunmap_pages(struct dev_pagemap *pgmap)
{ {
struct dev_pagemap *pgmap = data;
struct resource *res = &pgmap->res; struct resource *res = &pgmap->res;
unsigned long pfn; unsigned long pfn;
int nid; int nid;
...@@ -128,6 +127,12 @@ static void devm_memremap_pages_release(void *data) ...@@ -128,6 +127,12 @@ static void devm_memremap_pages_release(void *data)
WARN_ONCE(pgmap->altmap.alloc, "failed to free all reserved pages\n"); WARN_ONCE(pgmap->altmap.alloc, "failed to free all reserved pages\n");
devmap_managed_enable_put(); devmap_managed_enable_put();
} }
EXPORT_SYMBOL_GPL(memunmap_pages);
static void devm_memremap_pages_release(void *data)
{
memunmap_pages(data);
}
static void dev_pagemap_percpu_release(struct percpu_ref *ref) static void dev_pagemap_percpu_release(struct percpu_ref *ref)
{ {
...@@ -137,27 +142,12 @@ static void dev_pagemap_percpu_release(struct percpu_ref *ref) ...@@ -137,27 +142,12 @@ static void dev_pagemap_percpu_release(struct percpu_ref *ref)
complete(&pgmap->done); complete(&pgmap->done);
} }
/** /*
* devm_memremap_pages - remap and provide memmap backing for the given resource * Not device managed version of dev_memremap_pages, undone by
* @dev: hosting device for @res * memunmap_pages(). Please use dev_memremap_pages if you have a struct
* @pgmap: pointer to a struct dev_pagemap * device available.
*
* Notes:
* 1/ At a minimum the res and type members of @pgmap must be initialized
* by the caller before passing it to this function
*
* 2/ The altmap field may optionally be initialized, in which case
* PGMAP_ALTMAP_VALID must be set in pgmap->flags.
*
* 3/ The ref field may optionally be provided, in which pgmap->ref must be
* 'live' on entry and will be killed and reaped at
* devm_memremap_pages_release() time, or if this routine fails.
*
* 4/ res is expected to be a host memory range that could feasibly be
* treated as a "System RAM" range, i.e. not a device mmio range, but
* this is not enforced.
*/ */
void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) void *memremap_pages(struct dev_pagemap *pgmap, int nid)
{ {
struct resource *res = &pgmap->res; struct resource *res = &pgmap->res;
struct dev_pagemap *conflict_pgmap; struct dev_pagemap *conflict_pgmap;
...@@ -168,7 +158,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -168,7 +158,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
.altmap = pgmap_altmap(pgmap), .altmap = pgmap_altmap(pgmap),
}; };
pgprot_t pgprot = PAGE_KERNEL; pgprot_t pgprot = PAGE_KERNEL;
int error, nid, is_ram; int error, is_ram;
bool need_devmap_managed = true; bool need_devmap_managed = true;
switch (pgmap->type) { switch (pgmap->type) {
...@@ -223,7 +213,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -223,7 +213,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
conflict_pgmap = get_dev_pagemap(PHYS_PFN(res->start), NULL); conflict_pgmap = get_dev_pagemap(PHYS_PFN(res->start), NULL);
if (conflict_pgmap) { if (conflict_pgmap) {
dev_WARN(dev, "Conflicting mapping in same section\n"); WARN(1, "Conflicting mapping in same section\n");
put_dev_pagemap(conflict_pgmap); put_dev_pagemap(conflict_pgmap);
error = -ENOMEM; error = -ENOMEM;
goto err_array; goto err_array;
...@@ -231,7 +221,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -231,7 +221,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
conflict_pgmap = get_dev_pagemap(PHYS_PFN(res->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"); WARN(1, "Conflicting mapping in same section\n");
put_dev_pagemap(conflict_pgmap); put_dev_pagemap(conflict_pgmap);
error = -ENOMEM; error = -ENOMEM;
goto err_array; goto err_array;
...@@ -252,7 +242,6 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -252,7 +242,6 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
if (error) if (error)
goto err_array; goto err_array;
nid = dev_to_node(dev);
if (nid < 0) if (nid < 0)
nid = numa_mem_id(); nid = numa_mem_id();
...@@ -308,12 +297,6 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -308,12 +297,6 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
PHYS_PFN(res->start), PHYS_PFN(res->start),
PHYS_PFN(resource_size(res)), 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,
pgmap);
if (error)
return ERR_PTR(error);
return __va(res->start); return __va(res->start);
err_add_memory: err_add_memory:
...@@ -328,6 +311,43 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) ...@@ -328,6 +311,43 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
devmap_managed_enable_put(); devmap_managed_enable_put();
return ERR_PTR(error); return ERR_PTR(error);
} }
EXPORT_SYMBOL_GPL(memremap_pages);
/**
* devm_memremap_pages - remap and provide memmap backing for the given resource
* @dev: hosting device for @res
* @pgmap: pointer to a struct dev_pagemap
*
* Notes:
* 1/ At a minimum the res and type members of @pgmap must be initialized
* by the caller before passing it to this function
*
* 2/ The altmap field may optionally be initialized, in which case
* PGMAP_ALTMAP_VALID must be set in pgmap->flags.
*
* 3/ The ref field may optionally be provided, in which pgmap->ref must be
* 'live' on entry and will be killed and reaped at
* devm_memremap_pages_release() time, or if this routine fails.
*
* 4/ res is expected to be a host memory range that could feasibly be
* treated as a "System RAM" range, i.e. not a device mmio range, but
* this is not enforced.
*/
void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
{
int error;
void *ret;
ret = memremap_pages(pgmap, dev_to_node(dev));
if (IS_ERR(ret))
return ret;
error = devm_add_action_or_reset(dev, devm_memremap_pages_release,
pgmap);
if (error)
return ERR_PTR(error);
return ret;
}
EXPORT_SYMBOL_GPL(devm_memremap_pages); EXPORT_SYMBOL_GPL(devm_memremap_pages);
void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap) void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap)
......
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