]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
mm/uffd: PTE_MARKER_UFFD_WP
authorPeter Xu <peterx@redhat.com>
Thu, 14 Apr 2022 19:16:48 +0000 (12:16 -0700)
committerLiam R. Howlett <Liam.Howlett@oracle.com>
Thu, 14 Apr 2022 21:49:51 +0000 (17:49 -0400)
This patch introduces the 1st user of pte marker: the uffd-wp marker.

When the pte marker is installed with the uffd-wp bit set, it means this
pte was wr-protected by uffd.

We will use this special pte to arm the ptes that got either unmapped or
swapped out for a file-backed region that was previously wr-protected.
This special pte could trigger a page fault just like swap entries.

This idea is greatly inspired by Hugh and Andrea in the discussion, which
is referenced in the links below.

Some helpers are introduced to detect whether a swap pte is uffd
wr-protected.  After the pte marker introduced, one swap pte can be
wr-protected in two forms: either it is a normal swap pte and it has
_PAGE_SWP_UFFD_WP set, or it's a pte marker that has PTE_MARKER_UFFD_WP
set.

Link: https://lore.kernel.org/lkml/20201126222359.8120-1-peterx@redhat.com/
Link: https://lore.kernel.org/lkml/20201130230603.46187-1-peterx@redhat.com/
Link: https://lkml.kernel.org/r/20220405014838.14131-1-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
Suggested-by: Andrea Arcangeli <aarcange@redhat.com>
Suggested-by: Hugh Dickins <hughd@google.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Jerome Glisse <jglisse@redhat.com>
Cc: "Kirill A . Shutemov" <kirill@shutemov.name>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Mike Rapoport <rppt@linux.vnet.ibm.com>
Cc: Nadav Amit <nadav.amit@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
include/linux/swapops.h
include/linux/userfaultfd_k.h
mm/Kconfig

index a27e2a8056ee0009d938ee5e73280febd9ebb5f8..a291f210e7f8fd93bf09d9be8ab13619ad1334a9 100644 (file)
@@ -276,7 +276,8 @@ static inline int is_readable_migration_entry(swp_entry_t entry)
 
 typedef unsigned long pte_marker;
 
-#define  PTE_MARKER_MASK     (0)
+#define  PTE_MARKER_UFFD_WP  BIT(0)
+#define  PTE_MARKER_MASK     (PTE_MARKER_UFFD_WP)
 
 #ifdef CONFIG_PTE_MARKER
 
index e0b2ec2c20f28640a5c65caf184d73e79d0764ea..4d7e0c89889f5ffb5d7a1671e407ff06c3e84960 100644 (file)
@@ -15,6 +15,8 @@
 
 #include <linux/fcntl.h>
 #include <linux/mm.h>
+#include <linux/swap.h>
+#include <linux/swapops.h>
 #include <asm-generic/pgtable_uffd.h>
 
 /* The set of all possible UFFD-related VM flags. */
@@ -235,4 +237,45 @@ static inline void userfaultfd_unmap_complete(struct mm_struct *mm,
 
 #endif /* CONFIG_USERFAULTFD */
 
+static inline bool pte_marker_entry_uffd_wp(swp_entry_t entry)
+{
+       return is_pte_marker_entry(entry) &&
+           (pte_marker_get(entry) & PTE_MARKER_UFFD_WP);
+}
+
+static inline bool pte_marker_uffd_wp(pte_t pte)
+{
+#ifdef CONFIG_PTE_MARKER_UFFD_WP
+       swp_entry_t entry;
+
+       if (!is_swap_pte(pte))
+               return false;
+
+       entry = pte_to_swp_entry(pte);
+
+       return pte_marker_entry_uffd_wp(entry);
+#else
+       return false;
+#endif
+}
+
+/*
+ * Returns true if this is a swap pte and was uffd-wp wr-protected in either
+ * forms (pte marker or a normal swap pte), false otherwise.
+ */
+static inline bool pte_swp_uffd_wp_any(pte_t pte)
+{
+#ifdef CONFIG_PTE_MARKER_UFFD_WP
+       if (!is_swap_pte(pte))
+               return false;
+
+       if (pte_swp_uffd_wp(pte))
+               return true;
+
+       if (pte_marker_uffd_wp(pte))
+               return true;
+#endif
+       return false;
+}
+
 #endif /* _LINUX_USERFAULTFD_K_H */
index 2262f211b53e9d16d9dae7bd083b9fbb414f4a7a..71c653260a4455852c259a5f239fa99f66afd75b 100644 (file)
@@ -915,6 +915,15 @@ config PTE_MARKER
        help
          Allows to create marker PTEs for file-backed memory.
 
+config PTE_MARKER_UFFD_WP
+       bool "Marker PTEs support for userfaultfd write protection"
+       depends on PTE_MARKER && HAVE_ARCH_USERFAULTFD_WP
+
+       help
+         Allows to create marker PTEs for userfaultfd write protection
+         purposes.  It is required to enable userfaultfd write protection on
+         file-backed memory types like shmem and hugetlbfs.
+
 source "mm/damon/Kconfig"
 
 endmenu