Skip to contentSkip to conversation

add a not device managed memremap_pages v2

PATCH In review Christoph Hellwig 5 participants 14 messages Aug 16, 2019 – Aug 18, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Hi Dan and Jason,

Bharata has been working on secure page management for kvmppc guests, and one I thing I noticed is that he had to fake up a struct device just so that it could be passed to the devm_memremap_pages instrastructure for device private memory.

This series adds non-device managed versions of the devm_request_free_mem_region and devm_memremap_pages functions for his use case.

Changes since v1:
- don't overload devm_request_free_mem_region
- export the memremap_pages and munmap_pages as kvmppc can be a module

#1 lore raw

PATCH 1/4 resource: add a not device managed request_free_mem_region variant

Just add a simple macro that passes a NULL dev argument to dev_request_free_mem_region, and call request_mem_region in the function for that particular case.

---
 include/linux/ioport.h |  2 ++
 kernel/resource.c      | 45 +++++++++++++++++++++++++++++-------------
 2 files changed, 33 insertions(+), 14 deletions(-)
include/linux/ioport.h +2 -0
index 5b6a7121c9f0..7bddddfc76d6 100644
@@ -297,6 +297,8 @@ static inline bool resource_overlaps(struct resource *r1, struct resource *r2)
 
 struct resource *devm_request_free_mem_region(struct device *dev,
 		struct resource *base, unsigned long size);
+struct resource *request_free_mem_region(struct resource *base,
+		unsigned long size, const char *name);
 
 #endif /* __ASSEMBLY__ */
 #endif	/* _LINUX_IOPORT_H */
kernel/resource.c +31 -14

64 lines not on this page · raw message

Signature · 1 line
2.20.1
maintainer
#2 lore raw

Re: [PATCH 1/4] resource: add a not device managed request_free_mem_region variant

Quoted 3 lines · On Fri, 16 Aug 2019 08:54:31 +0200 Christoph Hellwig <[email protected]> wrote:

Just add a simple macro that passes a NULL dev argument to dev_request_free_mem_region, and call request_mem_region in the function for that particular case.

Nit:

include/linux/ioport.h static inline bool resource_overlaps(struct resource *r1, struct resource *r2) · line 301

Quoted 2 lines
include/linux/ioport.h +2 -0
+struct resource *request_free_mem_region(struct resource *base,
+		unsigned long size, const char *name);

This isn't a macro ;)

#3 lore raw

Re: [PATCH 1/4] resource: add a not device managed request_free_mem_region variant

Quoted 6 lines · Andrew Morton
Quoted 3 lines · On Fri, 16 Aug 2019 08:54:31 +0200 Christoph Hellwig <[email protected]> wrote:

Just add a simple macro that passes a NULL dev argument to dev_request_free_mem_region, and call request_mem_region in the function for that particular case.

Quoted 6 lines

Nit:

include/linux/ioport.h static inline bool resource_overlaps(struct resource *r1, struct resource *r2) · line 301

Quoted 2 lines
include/linux/ioport.h +2 -0
+struct resource *request_free_mem_region(struct resource *base,
+		unsigned long size, const char *name);

This isn't a macro ;)

Oops, the changelog needs updating vs the first version of course.

#4 lore raw

PATCH 2/4 memremap: remove the dev field in struct dev_pagemap

In reply to Christoph Hellwig

The dev field in struct dev_pagemap is only used to print dev_name in two places, which are at best nice to have. Just remove the field and thus the name in those two messages.

---
 include/linux/memremap.h | 1 -
 mm/memremap.c            | 6 +-----
 mm/page_alloc.c          | 2 +-
 3 files changed, 2 insertions(+), 7 deletions(-)
include/linux/memremap.h +0 -1
index f8a5b2a19945..8f0013e18e14 100644
@@ -109,7 +109,6 @@ struct dev_pagemap {
 	struct percpu_ref *ref;
 	struct percpu_ref internal_ref;
 	struct completion done;
-	struct device *dev;
 	enum memory_type type;
 	unsigned int flags;
 	u64 pci_p2pdma_bus_offset;
mm/memremap.c +1 -5
index 86432650f829..416b4129acbb 100644
@@ -102,7 +102,6 @@ static void dev_pagemap_cleanup(struct dev_pagemap *pgmap)
 static void devm_memremap_pages_release(void *data)
 {
 	struct dev_pagemap *pgmap = data;
-	struct device *dev = pgmap->dev;
 	struct resource *res = &pgmap->res;
 	unsigned long pfn;
 	int nid;
@@ -129,8 +128,7 @@ static void devm_memremap_pages_release(void *data)
 
 	untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
 	pgmap_array_delete(res);
-	dev_WARN_ONCE(dev, pgmap->altmap.alloc,
-		      "%s: failed to free all reserved pages\n", __func__);
+	WARN_ONCE(pgmap->altmap.alloc, "failed to free all reserved pages\n");
 }
 
 static void dev_pagemap_percpu_release(struct percpu_ref *ref)
@@ -251,8 +249,6 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
 		goto err_array;
 	}
 
-	pgmap->dev = dev;
-
 	error = xa_err(xa_store_range(&pgmap_array, PHYS_PFN(res->start),
 				PHYS_PFN(res->end), pgmap, GFP_KERNEL));
 	if (error)
mm/page_alloc.c +1 -1
index 272c6de1bf4e..b39baa2b1faf 100644
@@ -5982,7 +5982,7 @@ void __ref memmap_init_zone_device(struct zone *zone,
 		}
 	}
 
-	pr_info("%s initialised, %lu pages in %ums\n", dev_name(pgmap->dev),
+	pr_info("%s initialised %lu pages in %ums\n", __func__,
 		size, jiffies_to_msecs(jiffies - start));
 }
 
Signature · 1 line
2.20.1
#5 lore raw

PATCH 3/4 memremap: don't use a separate devm action for devmap_managed_enable_get

In reply to Christoph Hellwig

Just clean up for early failures and then piggy back on devm_memremap_pages_release. This helps with a pending not device managed version of devm_memremap_pages.

---
 mm/memremap.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
mm/memremap.c +10 -5
index 416b4129acbb..4e11da4ecab9 100644
@@ -21,13 +21,13 @@ DEFINE_STATIC_KEY_FALSE(devmap_managed_key);
 EXPORT_SYMBOL(devmap_managed_key);
 static atomic_t devmap_managed_enable;
 
-static void devmap_managed_enable_put(void *data)
+static void devmap_managed_enable_put(void)
 {
 	if (atomic_dec_and_test(&devmap_managed_enable))
 		static_branch_disable(&devmap_managed_key);
 }
 
-static int devmap_managed_enable_get(struct device *dev, struct dev_pagemap *pgmap)
+static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
 {
 	if (!pgmap->ops || !pgmap->ops->page_free) {
 		WARN(1, "Missing page_free method\n");
@@ -36,13 +36,16 @@ static int devmap_managed_enable_get(struct device *dev, struct dev_pagemap *pgm
 
 	if (atomic_inc_return(&devmap_managed_enable) == 1)
 		static_branch_enable(&devmap_managed_key);
-	return devm_add_action_or_reset(dev, devmap_managed_enable_put, NULL);
+	return 0;
 }
 #else
-static int devmap_managed_enable_get(struct device *dev, struct dev_pagemap *pgmap)
+static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
 {
 	return -EINVAL;
 }
+static void devmap_managed_enable_put(void)
+{
+}
 #endif /* CONFIG_DEV_PAGEMAP_OPS */
 
 static void pgmap_array_delete(struct resource *res)
@@ -129,6 +132,7 @@ static void devm_memremap_pages_release(void *data)
 	untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
 	pgmap_array_delete(res);
 	WARN_ONCE(pgmap->altmap.alloc, "failed to free all reserved pages\n");
+	devmap_managed_enable_put();
 }
 
 static void dev_pagemap_percpu_release(struct percpu_ref *ref)
@@ -218,7 +222,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
 	}
 
 	if (need_devmap_managed) {
-		error = devmap_managed_enable_get(dev, pgmap);
+		error = devmap_managed_enable_get(pgmap);
 		if (error)
 			return ERR_PTR(error);
 	}
@@ -327,6 +331,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
  err_array:
 	dev_pagemap_kill(pgmap);
 	dev_pagemap_cleanup(pgmap);
+	devmap_managed_enable_put();
 	return ERR_PTR(error);
 }
 EXPORT_SYMBOL_GPL(devm_memremap_pages);
Signature · 1 line
2.20.1
#6 lore raw

PATCH 4/4 memremap: provide a not device managed memremap_pages

In reply to Christoph Hellwig

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. Note that this function is not exported, and doesn't have a cleanup routine associated with it to discourage use from more driver like users.

---
 include/linux/memremap.h |  2 +
 mm/memremap.c            | 84 +++++++++++++++++++++++++---------------
 2 files changed, 54 insertions(+), 32 deletions(-)
include/linux/memremap.h +2 -0
index 8f0013e18e14..fb2a0bd826b9 100644
@@ -123,6 +123,8 @@ static inline struct vmem_altmap *pgmap_altmap(struct dev_pagemap *pgmap)
 }
 
 #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_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap);
 struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
mm/memremap.c +52 -32

140 lines not on this page · raw message

Signature · 1 line
2.20.1
maintainer
#7 lore raw

Re: [PATCH 4/4] memremap: provide a not device managed memremap_pages

Quoted 5 lines · On Fri, 16 Aug 2019 08:54:34 +0200 Christoph Hellwig <[email protected]> wrote:

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. Note that this function is not exported, and doesn't have a cleanup routine associated with it to discourage use from more driver like users.

Confused. Which function is "not exported"?

Quoted 3 lines
mm/memremap.c +2 -0
+EXPORT_SYMBOL_GPL(memunmap_pages);
+EXPORT_SYMBOL_GPL(memremap_pages);
 EXPORT_SYMBOL_GPL(devm_memremap_pages);
#8 lore raw

Re: [PATCH 4/4] memremap: provide a not device managed memremap_pages

Quoted 9 lines · Andrew Morton
Quoted 5 lines · On Fri, 16 Aug 2019 08:54:34 +0200 Christoph Hellwig <[email protected]> wrote:

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. Note that this function is not exported, and doesn't have a cleanup routine associated with it to discourage use from more driver like users.

Confused. Which function is "not exported"?

Leftover from v1 and dropped now.

#9 lore raw
In reply to Christoph Hellwig
Quoted 10 lines · Christoph Hellwig

Hi Dan and Jason,

Bharata has been working on secure page management for kvmppc guests, and one I thing I noticed is that he had to fake up a struct device just so that it could be passed to the devm_memremap_pages instrastructure for device private memory.

This series adds non-device managed versions of the devm_request_free_mem_region and devm_memremap_pages functions for his use case.

Tested this series with my patches that add secure page management for kvmppc guests. These patches along with migrate_vma-cleanup series are good-to-have to support secure guests on ultravisor enabled POWER platforms.

Regards,
Bharata.

#10 lore raw
In reply to Christoph Hellwig
Quoted 7 lines · Christoph Hellwig

Hi Dan and Jason,

Bharata has been working on secure page management for kvmppc guests, and one I thing I noticed is that he had to fake up a struct device just so that it could be passed to the devm_memremap_pages instrastructure for device private memory.

Quoted 7 lines

This series adds non-device managed versions of the devm_request_free_mem_region and devm_memremap_pages functions for his use case.

Changes since v1:
- don't overload devm_request_free_mem_region
- export the memremap_pages and munmap_pages as kvmppc can be a module

What tree do we want this to go through? Dan are you running a pgmap tree still? Do we know of any conflicts?

Thanks,
Jason

#11 lore raw
Quoted 6 lines
Quoted 3 lines

Changes since v1:
- don't overload devm_request_free_mem_region
- export the memremap_pages and munmap_pages as kvmppc can be a module

What tree do we want this to go through? Dan are you running a pgmap tree still? Do we know of any conflicts?

The last changes in this area went through the hmm tree. There are now known conflicts, and the kvmppc drivers that needs this already has a dependency on the hmm tree for the migrate_vma_* changes.

#12 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 6 lines
Quoted 3 lines

Changes since v1:
- don't overload devm_request_free_mem_region
- export the memremap_pages and munmap_pages as kvmppc can be a module

What tree do we want this to go through? Dan are you running a pgmap tree still? Do we know of any conflicts?

The last changes in this area went through the hmm tree. There are now known conflicts, and the kvmppc drivers that needs this already has a dependency on the hmm tree for the migrate_vma_* changes.

OK by me, Dan can you ack or review? Thanks

Jason

Reviewed-by
#13 lore raw
In reply to Christoph Hellwig
Quoted 7 lines · Christoph Hellwig

Hi Dan and Jason,

Bharata has been working on secure page management for kvmppc guests, and one I thing I noticed is that he had to fake up a struct device just so that it could be passed to the devm_memremap_pages instrastructure for device private memory.

Quoted 7 lines

This series adds non-device managed versions of the devm_request_free_mem_region and devm_memremap_pages functions for his use case.

Changes since v1:
- don't overload devm_request_free_mem_region
- export the memremap_pages and munmap_pages as kvmppc can be a module

Except for the questions from Andrew this does not look to change anything so:

Quoted 4 lines

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm