[PATCH V8 37/44] memremap_pages: Set PKS PKey in PTEs if PGMAP_PROTECTIONS is requested

From: ira . weiny
Date: Thu Jan 27 2022 - 12:56:52 EST


From: Ira Weiny <ira.weiny@xxxxxxxxx>

When the user requests protections the dev_pagemap mappings need to have
a PKEY set.

Define devmap_protection_adjust_pgprot() to add the PKey to the page
protections. Call it when PGMAP_PROTECTIONS is requested when remapping
pages.

Signed-off-by: Ira Weiny <ira.weiny@xxxxxxxxx>
---
mm/memremap.c | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/mm/memremap.c b/mm/memremap.c
index a74d985a1908..d3e6f328a711 100644
--- a/mm/memremap.c
+++ b/mm/memremap.c
@@ -83,6 +83,14 @@ static void devmap_protection_enable(void)
static_branch_inc(&dev_pgmap_protection_static_key);
}

+static pgprot_t devmap_protection_adjust_pgprot(pgprot_t prot)
+{
+ pgprotval_t val;
+
+ val = pgprot_val(prot);
+ return __pgprot(val | _PAGE_PKEY(PKS_KEY_PGMAP_PROTECTION));
+}
+
static void devmap_protection_disable(void)
{
static_branch_dec(&dev_pgmap_protection_static_key);
@@ -99,6 +107,10 @@ EXPORT_SYMBOL_GPL(pgmap_protection_available);
static void devmap_protection_enable(void) { }
static void devmap_protection_disable(void) { }

+static pgprot_t devmap_protection_adjust_pgprot(pgprot_t prot)
+{
+ return prot;
+}
#endif /* CONFIG_DEVMAP_ACCESS_PROTECTION */

static void pgmap_array_delete(struct range *range)
@@ -353,6 +365,7 @@ void *memremap_pages(struct dev_pagemap *pgmap, int nid)
if (!pgmap_protection_available())
return ERR_PTR(-EINVAL);
devmap_protection_enable();
+ params.pgprot = devmap_protection_adjust_pgprot(params.pgprot);
}

switch (pgmap->type) {
--
2.31.1