[HMM v16 02/15] mm/ZONE_DEVICE/devmem_pages_remove: allow early removal of device memory v2

From: JÃrÃme Glisse
Date: Thu Jan 12 2017 - 10:30:06 EST


Some device driver manage multiple physical devices memory from a single
fake device driver. In that case the fake device might outlive the real
device and ZONE_DEVICE and its resource allocated for a real device would
waste resources in the meantime.

This patch allow early removal of ZONE_DEVICE and associated resource,
before device driver is tear down.

Changed since v1:
- s/devm_memremap_pages_remove/devm_memunmap_pages
- use ret value of devm_memremap_pages as arg for devm_memunmap_pages

Signed-off-by: JÃrÃme Glisse <jglisse@xxxxxxxxxx>
Cc: Dan Williams <dan.j.williams@xxxxxxxxx>
Cc: Ross Zwisler <ross.zwisler@xxxxxxxxxxxxxxx>
---
include/linux/memremap.h | 6 ++++++
kernel/memremap.c | 14 ++++++++++++++
2 files changed, 20 insertions(+)

diff --git a/include/linux/memremap.h b/include/linux/memremap.h
index f7e0609..aee8477 100644
--- a/include/linux/memremap.h
+++ b/include/linux/memremap.h
@@ -53,6 +53,7 @@ struct dev_pagemap {
void *devm_memremap_pages(struct device *dev, struct resource *res,
struct percpu_ref *ref, struct vmem_altmap *altmap);
struct dev_pagemap *find_dev_pagemap(resource_size_t phys);
+int devm_memunmap_pages(struct device *dev, void *start);

static inline bool dev_page_allow_migrate(const struct page *page)
{
@@ -78,6 +79,11 @@ static inline struct dev_pagemap *find_dev_pagemap(resource_size_t phys)
return NULL;
}

+static inline int devm_memunmap_pages(struct device *dev, void *start)
+{
+ return -EINVAL;
+}
+
static inline bool dev_page_allow_migrate(const struct page *page)
{
return false;
diff --git a/kernel/memremap.c b/kernel/memremap.c
index 07665eb..f0d4ea2 100644
--- a/kernel/memremap.c
+++ b/kernel/memremap.c
@@ -387,6 +387,20 @@ void *devm_memremap_pages(struct device *dev, struct resource *res,
}
EXPORT_SYMBOL(devm_memremap_pages);

+static int devm_page_map_match(struct device *dev, void *data, void *match_data)
+{
+ struct page_map *page_map = data;
+
+ return __va(page_map->res.start) == match_data;
+}
+
+int devm_memunmap_pages(struct device *dev, void *start)
+{
+ return devres_release(dev, &devm_memremap_pages_release,
+ &devm_page_map_match, start);
+}
+EXPORT_SYMBOL(devm_memunmap_pages);
+
unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
{
/* number of pfns from base where pfn_to_page() is valid */
--
2.4.3