Re: [PATCH v11 2/5] mm: userfaultfd: Add pgtable_uffd_wp_supported()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 11.09.25 11:55, Chunyan Zhang wrote:
Some platforms can customize the PTE/PMD entry uffd-wp bit making
it unavailable even if the architecture provides the resource.
This patch adds a macro API that allows architectures to define their
specific implementations to check if the uffd-wp bit is available
on which device the kernel is running.

If you change the name of the sofdirty thingy, adjust that one here as well.


Signed-off-by: Chunyan Zhang <zhangchunyan@xxxxxxxxxxx>
---
  fs/userfaultfd.c                   | 23 ++++++++--------
  include/asm-generic/pgtable_uffd.h | 11 ++++++++
  include/linux/mm_inline.h          |  7 +++++
  include/linux/userfaultfd_k.h      | 44 +++++++++++++++++++-----------
  mm/memory.c                        |  6 ++--
  5 files changed, 62 insertions(+), 29 deletions(-)

diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
index 54c6cc7fe9c6..b549c327d7ad 100644
--- a/fs/userfaultfd.c
+++ b/fs/userfaultfd.c
@@ -1270,9 +1270,9 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
  	if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MISSING)
  		vm_flags |= VM_UFFD_MISSING;
  	if (uffdio_register.mode & UFFDIO_REGISTER_MODE_WP) {
-#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP
-		goto out;
-#endif
+		if (!pgtable_uffd_wp_supported())
+			goto out;
+
  		vm_flags |= VM_UFFD_WP;

I like that, similar to the softdirty thing we will simply not set the flag.

  	}
  	if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MINOR) {
@@ -1980,14 +1980,15 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx,
  	uffdio_api.features &=
  		~(UFFD_FEATURE_MINOR_HUGETLBFS | UFFD_FEATURE_MINOR_SHMEM);
  #endif
-#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP
-	uffdio_api.features &= ~UFFD_FEATURE_PAGEFAULT_FLAG_WP;
-#endif
-#ifndef CONFIG_PTE_MARKER_UFFD_WP
-	uffdio_api.features &= ~UFFD_FEATURE_WP_HUGETLBFS_SHMEM;
-	uffdio_api.features &= ~UFFD_FEATURE_WP_UNPOPULATED;
-	uffdio_api.features &= ~UFFD_FEATURE_WP_ASYNC;
-#endif
+	if (!pgtable_uffd_wp_supported())
+		uffdio_api.features &= ~UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+
+	if (!IS_ENABLED(CONFIG_PTE_MARKER_UFFD_WP) ||
+	    !pgtable_uffd_wp_supported()) {

I wonder if we would want to have a helper for that like

static inline bool uffd_supports_wp_marker(void)
{
	return pgtable_uffd_wp_supported() && IS_ENABLED(CONFIG_PTE_MARKER_UFFD_WP);
}

That should clean all of this futher up.

+		uffdio_api.features &= ~UFFD_FEATURE_WP_HUGETLBFS_SHMEM;
+		uffdio_api.features &= ~UFFD_FEATURE_WP_UNPOPULATED;
+		uffdio_api.features &= ~UFFD_FEATURE_WP_ASYNC;
+	}
ret = -EINVAL;
  	if (features & ~uffdio_api.features)
diff --git a/include/asm-generic/pgtable_uffd.h b/include/asm-generic/pgtable_uffd.h
index 828966d4c281..895d68ece0e7 100644
--- a/include/asm-generic/pgtable_uffd.h
+++ b/include/asm-generic/pgtable_uffd.h
@@ -1,6 +1,17 @@
  #ifndef _ASM_GENERIC_PGTABLE_UFFD_H
  #define _ASM_GENERIC_PGTABLE_UFFD_H
+/*
+ * Some platforms can customize the uffd-wp bit, making it unavailable
+ * even if the architecture provides the resource.
+ * Adding this API allows architectures to add their own checks for the
+ * devices on which the kernel is running.
+ * Note: When overiding it, please make sure the

s/overiding/overriding/

+ * CONFIG_HAVE_ARCH_USERFAULTFD_WP is part of this macro.
+ */
+#ifndef pgtable_uffd_wp_supported
+#define pgtable_uffd_wp_supported()	IS_ENABLED(CONFIG_HAVE_ARCH_USERFAULTFD_WP)
+#endif
  #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP
  static __always_inline int pte_uffd_wp(pte_t pte)
  {
diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
index 89b518ff097e..38845b8b79ff 100644
--- a/include/linux/mm_inline.h
+++ b/include/linux/mm_inline.h
@@ -571,6 +571,13 @@ pte_install_uffd_wp_if_needed(struct vm_area_struct *vma, unsigned long addr,
  			      pte_t *pte, pte_t pteval)
  {
  #ifdef CONFIG_PTE_MARKER_UFFD_WP
+	/*
+	 * Some platforms can customize the PTE uffd-wp bit, making it unavailable
+	 * even if the architecture allows providing the PTE resource.
+	 */
+	if (!pgtable_uffd_wp_supported())
+		return false;
+

Likely we could use the uffd_supports_wp_marker() wrapper here isntead and
remove the #ifdef.

  	bool arm_uffd_pte = false;
/* The current status of the pte should be "cleared" before calling */
diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h
index c0e716aec26a..6264b56ae961 100644
--- a/include/linux/userfaultfd_k.h
+++ b/include/linux/userfaultfd_k.h
@@ -228,15 +228,15 @@ static inline bool vma_can_userfault(struct vm_area_struct *vma,
  	if (wp_async && (vm_flags == VM_UFFD_WP))
  		return true;
-#ifndef CONFIG_PTE_MARKER_UFFD_WP
  	/*
  	 * If user requested uffd-wp but not enabled pte markers for
  	 * uffd-wp, then shmem & hugetlbfs are not supported but only
  	 * anonymous.
  	 */
-	if ((vm_flags & VM_UFFD_WP) && !vma_is_anonymous(vma))
+	if ((!IS_ENABLED(CONFIG_PTE_MARKER_UFFD_WP) ||
+	     !pgtable_uffd_wp_supported()) &&

This would also use the helper.

+	    (vm_flags & VM_UFFD_WP) && !vma_is_anonymous(vma))
  		return false;
-#endif
/* By default, allow any of anon|shmem|hugetlb */
  	return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
@@ -437,8 +437,11 @@ static inline bool userfaultfd_wp_use_markers(struct vm_area_struct *vma)
  static inline bool pte_marker_entry_uffd_wp(swp_entry_t entry)
  {
  #ifdef CONFIG_PTE_MARKER_UFFD_WP
-	return is_pte_marker_entry(entry) &&
-	    (pte_marker_get(entry) & PTE_MARKER_UFFD_WP);
+	if (pgtable_uffd_wp_supported())
+		return is_pte_marker_entry(entry) &&
+			(pte_marker_get(entry) & PTE_MARKER_UFFD_WP);
+	else
+		return false;

if (!uffd_supports_wp_marker())
	return false;
return is_pte_marker_entry(entry) &&
	(pte_marker_get(entry) & PTE_MARKER_UFFD_WP);

  #else
  	return false;
  #endif
@@ -447,14 +450,19 @@ static inline bool pte_marker_entry_uffd_wp(swp_entry_t entry)
  static inline bool pte_marker_uffd_wp(pte_t pte)
  {
  #ifdef CONFIG_PTE_MARKER_UFFD_WP


Simialrly here, just do a

if (!uffd_supports_wp_marker())
	return false;

and remove the ifdef

  #endif
@@ -467,14 +475,18 @@ static inline bool pte_marker_uffd_wp(pte_t pte)
  static inline bool pte_swp_uffd_wp_any(pte_t pte)
  {
  #ifdef CONFIG_PTE_MARKER_UFFD_WP

Same here.

-	if (!is_swap_pte(pte))
-		return false;
+	if (pgtable_uffd_wp_supported()) {
+		if (!is_swap_pte(pte))
+			return false;
- if (pte_swp_uffd_wp(pte))
-		return true;
+		if (pte_swp_uffd_wp(pte))
+			return true;
- if (pte_marker_uffd_wp(pte))
-		return true;
+		if (pte_marker_uffd_wp(pte))
+			return true;
+	} else {
+		return false;
+	}
  #endif
  	return false;
  }
diff --git a/mm/memory.c b/mm/memory.c
index 0ba4f6b71847..4eb05c5f487b 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1465,7 +1465,9 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma,
  {
  	bool was_installed = false;
-#ifdef CONFIG_PTE_MARKER_UFFD_WP
+	if (!IS_ENABLED(CONFIG_PTE_MARKER_UFFD_WP) || !pgtable_uffd_wp_supported())
+		return false;
+


Same here.


--
Cheers

David / dhildenb





[Index of Archives]     [Linux Ext4 Filesystem]     [Union Filesystem]     [Filesystem Testing]     [Ceph Users]     [Ecryptfs]     [NTFS 3]     [AutoFS]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux Cachefs]     [Reiser Filesystem]     [Linux RAID]     [NTFS 3]     [Samba]     [Device Mapper]     [CEPH Development]

  Powered by Linux