[RFC v4][PATCH part-2 11/13] mm/dpt: Add decorated page-table remap function

From: Alexandre Chartre
Date: Mon May 04 2020 - 11:03:20 EST


Add a function to remap an already mapped buffer with a new address in
a decorated page-table: the already mapped buffer is unmapped, and a
new mapping is added for the specified new address.

This is useful to track and remap a buffer which can be freed and
then reallocated.

Signed-off-by: Alexandre Chartre <alexandre.chartre@xxxxxxxxxx>
---
arch/x86/include/asm/dpt.h | 1 +
arch/x86/mm/dpt.c | 25 +++++++++++++++++++++++++
2 files changed, 26 insertions(+)

diff --git a/arch/x86/include/asm/dpt.h b/arch/x86/include/asm/dpt.h
index fd8c1b84ffe2..3234ba968d80 100644
--- a/arch/x86/include/asm/dpt.h
+++ b/arch/x86/include/asm/dpt.h
@@ -57,6 +57,7 @@ extern int dpt_map_range(struct dpt *dpt, void *ptr, size_t size,
enum page_table_level level);
extern int dpt_map(struct dpt *dpt, void *ptr, unsigned long size);
extern void dpt_unmap(struct dpt *dpt, void *ptr);
+extern int dpt_remap(struct dpt *dpt, void **mapping, void *ptr, size_t size);

static inline int dpt_map_module(struct dpt *dpt, char *module_name)
{
diff --git a/arch/x86/mm/dpt.c b/arch/x86/mm/dpt.c
index adc59f9ed876..9517e3081716 100644
--- a/arch/x86/mm/dpt.c
+++ b/arch/x86/mm/dpt.c
@@ -809,6 +809,31 @@ int dpt_map_percpu(struct dpt *dpt, void *percpu_ptr, size_t size)
}
EXPORT_SYMBOL(dpt_map_percpu);

+int dpt_remap(struct dpt *dpt, void **current_ptrp, void *new_ptr, size_t size)
+{
+ void *current_ptr = *current_ptrp;
+ int err;
+
+ if (current_ptr == new_ptr) {
+ /* no change, already mapped */
+ return 0;
+ }
+
+ if (current_ptr) {
+ dpt_unmap(dpt, current_ptr);
+ *current_ptrp = NULL;
+ }
+
+ err = dpt_map(dpt, new_ptr, size);
+ if (err)
+ return err;
+
+ *current_ptrp = new_ptr;
+
+ return 0;
+}
+EXPORT_SYMBOL(dpt_remap);
+
/*
* dpt_create - allocate a page-table and create a corresponding
* decorated page-table. The page-table is allocated and aligned
--
2.18.2