[PATCH v3 1/7] mm: memremap: register/unregister altmap region to a separate resource

Li Zhijian
2024-03-06T10:29:16Z

The elfcorehdr descirbes the dumpable region in PT_LOADs.

Generally, an iomem resource registered with flags
(IORESOURCE_SYSTEM_RAM | IORESOUCE_BUSY) will be added to PT_LOADs by
kexe_file_load(2). An iomem resource with name prefix "System RAM" will
be added to PT_LOADs in kexec-tools by calling kexe_load(2).

So a simple way to make the altmap dumpable is to register altmap region
as a separate resource with the proper name and resource flags.

Here naming it as "Device Backed Vmemmap" plus resource flags
(IORESOURCE_DEVICE_BACKED_VMEMMAP and IORESOUCE_BUSY) to make it work first.

A /proc/iomem example is as following:
$ sudo cat /proc/iomem
...
fffc0000-ffffffff : Reserved
100000000-13fffffff : Persistent Memory
100000000-10fffffff : namespace0.0
100000000-1005fffff : Device Backed Vmemmap # fsdax
a80000000-b7fffffff : CXL Window 0
a80000000-affffffff : Persistent Memory
a80000000-affffffff : region1
a80000000-a811fffff : namespace1.0
a80000000-a811fffff : Device Backed Vmemmap # devdax
a81200000-abfffffff : dax1.0
b80000000-c7fffffff : CXL Window 1
c80000000-147fffffff : PCI Bus 0000:00
c80000000-c801fffff : PCI Bus 0000:01
...

CC: Andrew Morton <akpm@linux-foundation.org>
CC: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
CC: Baoquan He <bhe@redhat.com>
CC: Dan Williams <dan.j.williams@intel.com>
CC: linux-mm@kvack.org
Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
---
include/linux/ioport.h | 1 +
include/linux/memremap.h | 3 +++
mm/memremap.c | 23 ++++++++++++++++++++++-
3 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index db7fe25f3370..3b59e924f531 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -69,6 +69,7 @@ struct resource {
#define IORESOURCE_UNSET 0x20000000 /* No address assigned yet */
#define IORESOURCE_AUTO 0x40000000
#define IORESOURCE_BUSY 0x80000000 /* Driver has marked this resource busy */
+#define IORESOURCE_DEVICE_BACKED_VMEMMAP 0xa0000000 /* device backed vmemmap resource */

/* I/O resource extended types */
#define IORESOURCE_SYSTEM_RAM (IORESOURCE_MEM|IORESOURCE_SYSRAM)
diff --git a/include/linux/memremap.h b/include/linux/memremap.h
index 744c830f4b13..ca1f12353008 100644
--- a/include/linux/memremap.h
+++ b/include/linux/memremap.h
@@ -17,6 +17,8 @@ struct device;
* @free: free pages set aside in the mapping for memmap storage
* @align: pages reserved to meet allocation alignments
* @alloc: track pages consumed, private to vmemmap_populate()
+ * @parent: the parent resource that altmap region belongs to
+ * @res: altmap region resource
*/
struct vmem_altmap {
unsigned long base_pfn;
@@ -25,6 +27,7 @@ struct vmem_altmap {
unsigned long free;
unsigned long align;
unsigned long alloc;
+ struct resource *parent, *res;
};

/*
diff --git a/mm/memremap.c b/mm/memremap.c
index 9e9fb1972fff..78047157b0ee 100644
--- a/mm/memremap.c
+++ b/mm/memremap.c
@@ -157,7 +157,17 @@ EXPORT_SYMBOL_GPL(memunmap_pages);

static void devm_memremap_pages_release(void *data)
{
- memunmap_pages(data);
+ struct dev_pagemap *pgmap = data;
+
+ if (pgmap->flags & PGMAP_ALTMAP_VALID && pgmap->altmap.res) {
+ resource_size_t start = pgmap->altmap.res->start;
+ resource_size_t size = pgmap->altmap.res->end -
+ pgmap->altmap.res->start + 1;
+
+ __release_region(pgmap->altmap.parent, start, size);
+ }
+
+ memunmap_pages(pgmap);
}

static void dev_pagemap_percpu_release(struct percpu_ref *ref)
@@ -404,11 +414,22 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
{
int error;
void *ret;
+ struct vmem_altmap *altmap = &pgmap->altmap;

ret = memremap_pages(pgmap, dev_to_node(dev));
if (IS_ERR(ret))
return ret;

+ if (pgmap->flags & PGMAP_ALTMAP_VALID && altmap->parent) {
+ unsigned long start = altmap->base_pfn << PAGE_SHIFT;
+ unsigned long size = vmem_altmap_offset(altmap) << PAGE_SHIFT;
+ int flags = IORESOURCE_DEVICE_BACKED_VMEMMAP | IORESOURCE_BUSY;
+
+ altmap->res = __request_region(altmap->parent, start, size,
+ "Device Backed Vmemmap", flags);
+ pr_debug("Insert a separate resource for altmap, %lx-%lx\n",
+ start, start + size);
+ }
error = devm_add_action_or_reset(dev, devm_memremap_pages_release,
pgmap);
if (error)
--
2.29.2