Some platforms can customize the PTE uffd_wp bit and make it unavailable
even if the architecture allows providing the PTE resource.
This patch adds a macro API which allows architectures to define
their specific one for checking if the PTE uffd_wp bit is available.
Signed-off-by: Chunyan Zhang <zhangchunyan@iscas.ac.cn>
---
fs/userfaultfd.c | 25 +++++++++--------
include/asm-generic/pgtable_uffd.h | 12 ++++++++
include/linux/mm_inline.h | 6 ++--
include/linux/pgtable.h | 1 +
include/linux/userfaultfd_k.h | 44 +++++++++++++++++++-----------
mm/memory.c | 6 ++--
6 files changed, 63 insertions(+), 31 deletions(-)
diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
index 54c6cc7fe9c6..68e5006e5158 100644
--- a/fs/userfaultfd.c
+++ b/fs/userfaultfd.c
@@ -1270,9 +1270,10 @@ 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 (!IS_ENABLED(CONFIG_HAVE_ARCH_USERFAULTFD_WP) ||
+ !pte_uffd_wp_available())
+ goto out;
+
vm_flags |= VM_UFFD_WP;
}
if (uffdio_register.mode & UFFDIO_REGISTER_MODE_MINOR) {
@@ -1980,14 +1981,16 @@ 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 (!IS_ENABLED(CONFIG_HAVE_ARCH_USERFAULTFD_WP) ||
+ !pte_uffd_wp_available())
+ uffdio_api.features &= ~UFFD_FEATURE_PAGEFAULT_FLAG_WP;
+
+ if (!IS_ENABLED(CONFIG_PTE_MARKER_UFFD_WP) ||
+ !pte_uffd_wp_available()) {
+ 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..b86a5ff447da 100644
--- a/include/asm-generic/pgtable_uffd.h
+++ b/include/asm-generic/pgtable_uffd.h
@@ -61,6 +61,18 @@ static inline pmd_t pmd_swp_clear_uffd_wp(pmd_t pmd)
{
return pmd;
}
+#define pte_uffd_wp_available() (false)
+#else
+/*
+ * Some platforms can customize the PTE uffd_wp bit and make it unavailable
+ * even if the architecture allows providing the PTE resource.
+ * This allows architectures to define their own API for checking if
+ * the PTE uffd_wp bit is available.
+ */
+#ifndef pte_uffd_wp_available
+#define pte_uffd_wp_available() (true)
+#endif
+
#endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
#endif /* _ASM_GENERIC_PGTABLE_UFFD_H */
diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
index 89b518ff097e..a81055bb3f87 100644
--- a/include/linux/mm_inline.h
+++ b/include/linux/mm_inline.h
@@ -570,7 +570,9 @@ static inline bool
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
+ if (!IS_ENABLED(CONFIG_PTE_MARKER_UFFD_WP) || !pte_uffd_wp_available())
+ return false;
+
bool arm_uffd_pte = false;
/* The current status of the pte should be "cleared" before calling */
@@ -601,7 +603,7 @@ pte_install_uffd_wp_if_needed(struct vm_area_struct *vma, unsigned long addr,
make_pte_marker(PTE_MARKER_UFFD_WP));
return true;
}
-#endif
+
return false;
}
diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index 2a489647ac96..51f5b610c5ec 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -1564,6 +1564,7 @@ static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
}
#endif
#else /* !CONFIG_HAVE_ARCH_SOFT_DIRTY */
+#define pte_soft_dirty_available() (false)
static inline int pte_soft_dirty(pte_t pte)
{
return 0;
diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h
index c0e716aec26a..ec4a815286c8 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) ||
+ !pte_uffd_wp_available()) &&
+ (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 (pte_uffd_wp_available())
+ return is_pte_marker_entry(entry) &&
+ (pte_marker_get(entry) & PTE_MARKER_UFFD_WP);
+ else
+ return false;
#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
- swp_entry_t entry;
+ if (pte_uffd_wp_available()) {
+ swp_entry_t entry;
- if (!is_swap_pte(pte))
- return false;
+ if (!is_swap_pte(pte))
+ return false;
- entry = pte_to_swp_entry(pte);
+ entry = pte_to_swp_entry(pte);
+
+ return pte_marker_entry_uffd_wp(entry);
+ } else {
+ return false;
+ }
- return pte_marker_entry_uffd_wp(entry);
#else
return false;
#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
- if (!is_swap_pte(pte))
- return false;
+ if (pte_uffd_wp_available()) {
+ 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..1c61b2d7bd4d 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) || !pte_uffd_wp_available())
+ return was_installed;
+
/* Zap on anonymous always means dropping everything */
if (vma_is_anonymous(vma))
return false;
@@ -1482,7 +1484,7 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma,
pte++;
addr += PAGE_SIZE;
}
-#endif
+
return was_installed;
}
--
2.34.1
Hi Chunyan, kernel test robot noticed the following build errors: [auto build test ERROR on brauner-vfs/vfs.all] [also build test ERROR on linus/master v6.17-rc4] [cannot apply to akpm-mm/mm-everything arnd-asm-generic/master next-20250905] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Chunyan-Zhang/mm-softdirty-Add-pte_soft_dirty_available/20250905-184138 base: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all patch link: https://lore.kernel.org/r/20250905103651.489197-3-zhangchunyan%40iscas.ac.cn patch subject: [PATCH v9 2/5] mm: uffd_wp: Add pte_uffd_wp_available() config: m68k-allnoconfig (https://download.01.org/0day-ci/archive/20250906/202509062145.1ipU0q7y-lkp@intel.com/config) compiler: m68k-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250906/202509062145.1ipU0q7y-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202509062145.1ipU0q7y-lkp@intel.com/ All errors (new ones prefixed by >>): In file included from fs/../mm/internal.h:13, from fs/exec.c:82: include/linux/mm_inline.h: In function 'pte_install_uffd_wp_if_needed': >> include/linux/mm_inline.h:573:56: error: implicit declaration of function 'pte_uffd_wp_available'; did you mean 'pte_soft_dirty_available'? [-Wimplicit-function-declaration] 573 | if (!IS_ENABLED(CONFIG_PTE_MARKER_UFFD_WP) || !pte_uffd_wp_available()) | ^~~~~~~~~~~~~~~~~~~~~ | pte_soft_dirty_available In file included from include/asm-generic/bug.h:7, from arch/m68k/include/asm/bug.h:32, from include/linux/bug.h:5, from include/linux/thread_info.h:13, from include/asm-generic/preempt.h:5, from ./arch/m68k/include/generated/asm/preempt.h:1, from include/linux/preempt.h:79, from include/linux/spinlock.h:56, from include/linux/mmzone.h:8, from include/linux/gfp.h:7, from include/linux/slab.h:16, from fs/exec.c:27: >> include/linux/mm_inline.h:579:23: error: implicit declaration of function 'pte_none'; did you mean 'p4d_none'? [-Wimplicit-function-declaration] 579 | WARN_ON_ONCE(!pte_none(ptep_get(pte))); | ^~~~~~~~ include/linux/once_lite.h:28:41: note: in definition of macro 'DO_ONCE_LITE_IF' 28 | bool __ret_do_once = !!(condition); \ | ^~~~~~~~~ include/linux/mm_inline.h:579:9: note: in expansion of macro 'WARN_ON_ONCE' 579 | WARN_ON_ONCE(!pte_none(ptep_get(pte))); | ^~~~~~~~~~~~ >> include/linux/mm_inline.h:579:32: error: implicit declaration of function 'ptep_get' [-Wimplicit-function-declaration] 579 | WARN_ON_ONCE(!pte_none(ptep_get(pte))); | ^~~~~~~~ include/linux/once_lite.h:28:41: note: in definition of macro 'DO_ONCE_LITE_IF' 28 | bool __ret_do_once = !!(condition); \ | ^~~~~~~~~ include/linux/mm_inline.h:579:9: note: in expansion of macro 'WARN_ON_ONCE' 579 | WARN_ON_ONCE(!pte_none(ptep_get(pte))); | ^~~~~~~~~~~~ In file included from include/linux/file.h:9, from include/linux/kernel_read_file.h:5, from fs/exec.c:26: >> include/linux/mm_inline.h:591:22: error: implicit declaration of function 'pte_present'; did you mean 'p4d_present'? [-Wimplicit-function-declaration] 591 | if (unlikely(pte_present(pteval) && pte_uffd_wp(pteval))) | ^~~~~~~~~~~ include/linux/compiler.h:77:45: note: in definition of macro 'unlikely' 77 | # define unlikely(x) __builtin_expect(!!(x), 0) | ^ >> include/linux/mm_inline.h:591:45: error: implicit declaration of function 'pte_uffd_wp' [-Wimplicit-function-declaration] 591 | if (unlikely(pte_present(pteval) && pte_uffd_wp(pteval))) | ^~~~~~~~~~~ include/linux/compiler.h:77:45: note: in definition of macro 'unlikely' 77 | # define unlikely(x) __builtin_expect(!!(x), 0) | ^ >> include/linux/mm_inline.h:602:17: error: implicit declaration of function 'set_pte_at'; did you mean 'user_path_at'? [-Wimplicit-function-declaration] 602 | set_pte_at(vma->vm_mm, addr, pte, | ^~~~~~~~~~ | user_path_at >> include/linux/mm_inline.h:603:28: error: implicit declaration of function 'make_pte_marker' [-Wimplicit-function-declaration] 603 | make_pte_marker(PTE_MARKER_UFFD_WP)); | ^~~~~~~~~~~~~~~ >> include/linux/mm_inline.h:603:44: error: 'PTE_MARKER_UFFD_WP' undeclared (first use in this function) 603 | make_pte_marker(PTE_MARKER_UFFD_WP)); | ^~~~~~~~~~~~~~~~~~ include/linux/mm_inline.h:603:44: note: each undeclared identifier is reported only once for each function it appears in -- In file included from fs/splice.c:27: include/linux/mm_inline.h: In function 'pte_install_uffd_wp_if_needed': >> include/linux/mm_inline.h:573:56: error: implicit declaration of function 'pte_uffd_wp_available'; did you mean 'pte_soft_dirty_available'? [-Wimplicit-function-declaration] 573 | if (!IS_ENABLED(CONFIG_PTE_MARKER_UFFD_WP) || !pte_uffd_wp_available()) | ^~~~~~~~~~~~~~~~~~~~~ | pte_soft_dirty_available In file included from include/asm-generic/bug.h:7, from arch/m68k/include/asm/bug.h:32, from include/linux/bug.h:5, from include/linux/vfsdebug.h:5, from include/linux/fs.h:5, from include/linux/highmem.h:5, from include/linux/bvec.h:10, from fs/splice.c:21: >> include/linux/mm_inline.h:579:23: error: implicit declaration of function 'pte_none'; did you mean 'p4d_none'? [-Wimplicit-function-declaration] 579 | WARN_ON_ONCE(!pte_none(ptep_get(pte))); | ^~~~~~~~ include/linux/once_lite.h:28:41: note: in definition of macro 'DO_ONCE_LITE_IF' 28 | bool __ret_do_once = !!(condition); \ | ^~~~~~~~~ include/linux/mm_inline.h:579:9: note: in expansion of macro 'WARN_ON_ONCE' 579 | WARN_ON_ONCE(!pte_none(ptep_get(pte))); | ^~~~~~~~~~~~ >> include/linux/mm_inline.h:579:32: error: implicit declaration of function 'ptep_get' [-Wimplicit-function-declaration] 579 | WARN_ON_ONCE(!pte_none(ptep_get(pte))); | ^~~~~~~~ include/linux/once_lite.h:28:41: note: in definition of macro 'DO_ONCE_LITE_IF' 28 | bool __ret_do_once = !!(condition); \ | ^~~~~~~~~ include/linux/mm_inline.h:579:9: note: in expansion of macro 'WARN_ON_ONCE' 579 | WARN_ON_ONCE(!pte_none(ptep_get(pte))); | ^~~~~~~~~~~~ In file included from include/asm-generic/bug.h:5: >> include/linux/mm_inline.h:591:22: error: implicit declaration of function 'pte_present'; did you mean 'p4d_present'? [-Wimplicit-function-declaration] 591 | if (unlikely(pte_present(pteval) && pte_uffd_wp(pteval))) | ^~~~~~~~~~~ include/linux/compiler.h:77:45: note: in definition of macro 'unlikely' 77 | # define unlikely(x) __builtin_expect(!!(x), 0) | ^ >> include/linux/mm_inline.h:591:45: error: implicit declaration of function 'pte_uffd_wp' [-Wimplicit-function-declaration] 591 | if (unlikely(pte_present(pteval) && pte_uffd_wp(pteval))) | ^~~~~~~~~~~ include/linux/compiler.h:77:45: note: in definition of macro 'unlikely' 77 | # define unlikely(x) __builtin_expect(!!(x), 0) | ^ include/linux/mm_inline.h:602:17: error: implicit declaration of function 'set_pte_at' [-Wimplicit-function-declaration] 602 | set_pte_at(vma->vm_mm, addr, pte, | ^~~~~~~~~~ >> include/linux/mm_inline.h:603:28: error: implicit declaration of function 'make_pte_marker' [-Wimplicit-function-declaration] 603 | make_pte_marker(PTE_MARKER_UFFD_WP)); | ^~~~~~~~~~~~~~~ >> include/linux/mm_inline.h:603:44: error: 'PTE_MARKER_UFFD_WP' undeclared (first use in this function) 603 | make_pte_marker(PTE_MARKER_UFFD_WP)); | ^~~~~~~~~~~~~~~~~~ include/linux/mm_inline.h:603:44: note: each undeclared identifier is reported only once for each function it appears in vim +573 include/linux/mm_inline.h 556 557 /* 558 * If this pte is wr-protected by uffd-wp in any form, arm the special pte to 559 * replace a none pte. NOTE! This should only be called when *pte is already 560 * cleared so we will never accidentally replace something valuable. Meanwhile 561 * none pte also means we are not demoting the pte so tlb flushed is not needed. 562 * E.g., when pte cleared the caller should have taken care of the tlb flush. 563 * 564 * Must be called with pgtable lock held so that no thread will see the none 565 * pte, and if they see it, they'll fault and serialize at the pgtable lock. 566 * 567 * Returns true if an uffd-wp pte was installed, false otherwise. 568 */ 569 static inline bool 570 pte_install_uffd_wp_if_needed(struct vm_area_struct *vma, unsigned long addr, 571 pte_t *pte, pte_t pteval) 572 { > 573 if (!IS_ENABLED(CONFIG_PTE_MARKER_UFFD_WP) || !pte_uffd_wp_available()) 574 return false; 575 576 bool arm_uffd_pte = false; 577 578 /* The current status of the pte should be "cleared" before calling */ > 579 WARN_ON_ONCE(!pte_none(ptep_get(pte))); 580 581 /* 582 * NOTE: userfaultfd_wp_unpopulated() doesn't need this whole 583 * thing, because when zapping either it means it's dropping the 584 * page, or in TTU where the present pte will be quickly replaced 585 * with a swap pte. There's no way of leaking the bit. 586 */ 587 if (vma_is_anonymous(vma) || !userfaultfd_wp(vma)) 588 return false; 589 590 /* A uffd-wp wr-protected normal pte */ > 591 if (unlikely(pte_present(pteval) && pte_uffd_wp(pteval))) 592 arm_uffd_pte = true; 593 594 /* 595 * A uffd-wp wr-protected swap pte. Note: this should even cover an 596 * existing pte marker with uffd-wp bit set. 597 */ 598 if (unlikely(pte_swp_uffd_wp_any(pteval))) 599 arm_uffd_pte = true; 600 601 if (unlikely(arm_uffd_pte)) { > 602 set_pte_at(vma->vm_mm, addr, pte, > 603 make_pte_marker(PTE_MARKER_UFFD_WP)); 604 return true; 605 } 606 607 return false; 608 } 609 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
© 2016 - 2025 Red Hat, Inc.