[PATCH V8 35/44] memremap_pages: Introduce devmap_protected()

ira.weiny
2022-01-27T17:56:28Z

From: Ira Weiny <ira.weiny@intel.com>

Users of protected dev_pagemaps can check the PGMAP_PROTECTION flag to
see if the devmap is protected. However, most callers operate on struct
page's not the pagemap directly.

Define devmap_protected() to determine if a page is part of a
dev_pagemap mapping and if so if the page is protected by the additional
protections.

Signed-off-by: Ira Weiny <ira.weiny@intel.com>
---
include/linux/mm.h | 17 +++++++++++++++++
1 file changed, 17 insertions(+)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 2ae99bee6e82..6e4a2758e3d3 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1145,6 +1145,23 @@ static inline bool is_pci_p2pdma_page(const struct page *page)

#ifdef CONFIG_DEVMAP_ACCESS_PROTECTION

+DECLARE_STATIC_KEY_FALSE(dev_pgmap_protection_static_key);
+
+/*
+ * devmap_protected() requires a reference on the page to ensure there is no
+ * races with dev_pagemap tear down.
+ */
+static inline bool devmap_protected(struct page *page)
+{
+ if (!static_branch_unlikely(&dev_pgmap_protection_static_key))
+ return false;
+ if (!is_zone_device_page(page))
+ return false;
+ if (page->pgmap->flags & PGMAP_PROTECTION)
+ return true;
+ return false;
+}
+
bool pgmap_protection_available(void);

#else
--
2.31.1