diff mbox series

[v8,2/4] userfaultfd: split mwriteprotect_range()

Message ID 20230124084323.1363825-3-usama.anjum@collabora.com (mailing list archive)
State New
Headers show
Series Implement IOCTL to get and/or the clear info about PTEs | expand

Commit Message

Muhammad Usama Anjum Jan. 24, 2023, 8:43 a.m. UTC
Split mwriteprotect_range() to create a unlocked version. This
will be used in the next patch to write protect a memory area.
Add a helper function, wp_range_async() as well.

Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
---
Changes in v7:
- Remove async being set in the PAGEMAP_IOCTL
---
 fs/userfaultfd.c              | 10 +++++++++
 include/linux/userfaultfd_k.h | 10 +++++++++
 mm/userfaultfd.c              | 40 ++++++++++++++++++++++-------------
 3 files changed, 45 insertions(+), 15 deletions(-)

Comments

kernel test robot Jan. 24, 2023, 10:23 a.m. UTC | #1
Hi Muhammad,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on shuah-kselftest/next]
[also build test ERROR on shuah-kselftest/fixes linus/master v6.2-rc5]
[cannot apply to next-20230124]
[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/Muhammad-Usama-Anjum/userfaultfd-Add-UFFD-WP-Async-support/20230124-164601
base:   https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next
patch link:    https://lore.kernel.org/r/20230124084323.1363825-3-usama.anjum%40collabora.com
patch subject: [PATCH v8 2/4] userfaultfd: split mwriteprotect_range()
config: i386-tinyconfig (https://download.01.org/0day-ci/archive/20230124/202301241831.AYRPKsH5-lkp@intel.com/config)
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0
reproduce (this is a W=1 build):
        # https://github.com/intel-lab-lkp/linux/commit/2806717805539421b82e971890ebbaf83b3deee4
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Muhammad-Usama-Anjum/userfaultfd-Add-UFFD-WP-Async-support/20230124-164601
        git checkout 2806717805539421b82e971890ebbaf83b3deee4
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 O=build_dir ARCH=i386 olddefconfig
        make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   ld: arch/x86/mm/init_32.o: in function `userfaultfd_wp_async':
   init_32.c:(.text+0x0): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: arch/x86/mm/init_32.o: in function `wp_range_async':
>> init_32.c:(.text+0x3): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: arch/x86/mm/fault.o: in function `userfaultfd_wp_async':
   fault.c:(.text+0x8cd): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: arch/x86/mm/fault.o: in function `wp_range_async':
   fault.c:(.text+0x8d0): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: arch/x86/mm/pgtable.o: in function `userfaultfd_wp_async':
   pgtable.c:(.text+0x0): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: arch/x86/mm/pgtable.o: in function `wp_range_async':
   pgtable.c:(.text+0x3): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: kernel/fork.o: in function `userfaultfd_wp_async':
   fork.c:(.text+0x5bb): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: kernel/fork.o: in function `wp_range_async':
   fork.c:(.text+0x5be): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: kernel/sysctl.o: in function `userfaultfd_wp_async':
   sysctl.c:(.text+0x0): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: kernel/sysctl.o: in function `wp_range_async':
   sysctl.c:(.text+0x3): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: kernel/sys.o: in function `userfaultfd_wp_async':
   sys.c:(.text+0xb5e): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: kernel/sys.o: in function `wp_range_async':
   sys.c:(.text+0xb61): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: kernel/events/core.o: in function `userfaultfd_wp_async':
   core.c:(.text+0x404c): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: kernel/events/core.o: in function `wp_range_async':
   core.c:(.text+0x404f): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/filemap.o: in function `userfaultfd_wp_async':
   filemap.c:(.text+0x7c5): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/filemap.o: in function `wp_range_async':
   filemap.c:(.text+0x7c8): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/page-writeback.o: in function `userfaultfd_wp_async':
   page-writeback.c:(.text+0xb69): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/page-writeback.o: in function `wp_range_async':
   page-writeback.c:(.text+0xb6c): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/folio-compat.o: in function `userfaultfd_wp_async':
   folio-compat.c:(.text+0xc): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/folio-compat.o: in function `wp_range_async':
   folio-compat.c:(.text+0xf): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/readahead.o: in function `userfaultfd_wp_async':
   readahead.c:(.text+0x128): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/readahead.o: in function `wp_range_async':
   readahead.c:(.text+0x12b): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/swap.o: in function `userfaultfd_wp_async':
   swap.c:(.text+0x6e5): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/swap.o: in function `wp_range_async':
   swap.c:(.text+0x6e8): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/vmscan.o: in function `userfaultfd_wp_async':
   vmscan.c:(.text+0xf96): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/vmscan.o: in function `wp_range_async':
   vmscan.c:(.text+0xf99): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/shmem.o: in function `userfaultfd_wp_async':
   shmem.c:(.text+0x91): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/shmem.o: in function `wp_range_async':
   shmem.c:(.text+0x94): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/util.o: in function `userfaultfd_wp_async':
   util.c:(.text+0x2b): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/util.o: in function `wp_range_async':
   util.c:(.text+0x2e): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/vmstat.o: in function `userfaultfd_wp_async':
   vmstat.c:(.text+0x0): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/vmstat.o: in function `wp_range_async':
   vmstat.c:(.text+0x3): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/compaction.o: in function `userfaultfd_wp_async':
   compaction.c:(.text+0x0): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/compaction.o: in function `wp_range_async':
   compaction.c:(.text+0x3): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/workingset.o: in function `userfaultfd_wp_async':
   workingset.c:(.text+0x181): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/workingset.o: in function `wp_range_async':
   workingset.c:(.text+0x184): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/debug.o: in function `userfaultfd_wp_async':
   debug.c:(.text+0xb9): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/debug.o: in function `wp_range_async':
   debug.c:(.text+0xbc): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/gup.o: in function `userfaultfd_wp_async':
   gup.c:(.text+0x2ae): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/gup.o: in function `wp_range_async':
   gup.c:(.text+0x2b1): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/memory.o: in function `userfaultfd_wp_async':
   memory.c:(.text+0x737): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/memory.o: in function `wp_range_async':
   memory.c:(.text+0x73a): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/mincore.o: in function `userfaultfd_wp_async':
   mincore.c:(.text+0x149): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/mincore.o: in function `wp_range_async':
   mincore.c:(.text+0x14c): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/mlock.o: in function `userfaultfd_wp_async':
   mlock.c:(.text+0x90e): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/mlock.o: in function `wp_range_async':
   mlock.c:(.text+0x911): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/mmap.o: in function `userfaultfd_wp_async':
   mmap.c:(.text+0x4d8): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/mmap.o: in function `wp_range_async':
   mmap.c:(.text+0x4db): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/mmu_gather.o: in function `userfaultfd_wp_async':
   mmu_gather.c:(.text+0x29): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/mmu_gather.o: in function `wp_range_async':
   mmu_gather.c:(.text+0x2c): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
   ld: mm/mprotect.o: in function `userfaultfd_wp_async':
   mprotect.c:(.text+0x45): multiple definition of `userfaultfd_wp_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x3): first defined here
   ld: mm/mprotect.o: in function `wp_range_async':
   mprotect.c:(.text+0x48): multiple definition of `wp_range_async'; arch/x86/kernel/setup.o:setup.c:(.text+0x6): first defined here
Peter Xu Jan. 27, 2023, 5:05 p.m. UTC | #2
On Tue, Jan 24, 2023 at 01:43:21PM +0500, Muhammad Usama Anjum wrote:
> Split mwriteprotect_range() to create a unlocked version. This
> will be used in the next patch to write protect a memory area.
> Add a helper function, wp_range_async() as well.
> 
> Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>

IIUC this patch is not needed.  You have a stable vma, so I think you can
directly use uffd_wp_range(), while most of the mwriteprotect_range() is
not needed.

There's one trivial detail of ignoring userfaultfd_ctx->mmap_changing when
it's set to true, but I don't think it applies here either because it was
used to resolve a problem in uffd non-cooperative mode on the predictable
behavior of events, here I don't think it matters a lot either.
Muhammad Usama Anjum Jan. 30, 2023, 9:10 a.m. UTC | #3
On 1/27/23 10:05 PM, Peter Xu wrote:
> On Tue, Jan 24, 2023 at 01:43:21PM +0500, Muhammad Usama Anjum wrote:
>> Split mwriteprotect_range() to create a unlocked version. This
>> will be used in the next patch to write protect a memory area.
>> Add a helper function, wp_range_async() as well.
>>
>> Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
> 
> IIUC this patch is not needed.  You have a stable vma, so I think you can
> directly use uffd_wp_range(), while most of the mwriteprotect_range() is
> not needed.
> 
> There's one trivial detail of ignoring userfaultfd_ctx->mmap_changing when
> it's set to true, but I don't think it applies here either because it was
> used to resolve a problem in uffd non-cooperative mode on the predictable
> behavior of events, here I don't think it matters a lot either.
> 
Thanks, I'll drop this patch and do direct wiring to uffd_wp_range().
diff mbox series

Patch

diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
index b82af02092ce..cd52c9ee8db9 100644
--- a/fs/userfaultfd.c
+++ b/fs/userfaultfd.c
@@ -1961,6 +1961,16 @@  int userfaultfd_wp_async(struct vm_area_struct *vma)
 	return (ctx && (ctx->features & UFFD_FEATURE_WP_ASYNC));
 }
 
+int wp_range_async(struct vm_area_struct *vma, unsigned long start, unsigned long len)
+{
+	struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx;
+
+	if (!ctx)
+		return -1;
+
+	return __mwriteprotect_range(ctx->mm, start, len, true, &ctx->mmap_changing);
+}
+
 static inline unsigned int uffd_ctx_features(__u64 user_features)
 {
 	/*
diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h
index 5db51fccae1d..40d1d9e8f34d 100644
--- a/include/linux/userfaultfd_k.h
+++ b/include/linux/userfaultfd_k.h
@@ -73,6 +73,9 @@  extern ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long dst_start,
 extern int mwriteprotect_range(struct mm_struct *dst_mm,
 			       unsigned long start, unsigned long len,
 			       bool enable_wp, atomic_t *mmap_changing);
+extern int __mwriteprotect_range(struct mm_struct *dst_mm,
+				 unsigned long start, unsigned long len,
+				 bool enable_wp, atomic_t *mmap_changing);
 extern void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *vma,
 			  unsigned long start, unsigned long len, bool enable_wp);
 
@@ -180,6 +183,8 @@  extern int userfaultfd_unmap_prep(struct mm_struct *mm, unsigned long start,
 extern void userfaultfd_unmap_complete(struct mm_struct *mm,
 				       struct list_head *uf);
 extern int userfaultfd_wp_async(struct vm_area_struct *vma);
+extern int wp_range_async(struct vm_area_struct *vma, unsigned long start,
+			  unsigned long len);
 
 #else /* CONFIG_USERFAULTFD */
 
@@ -280,6 +285,11 @@  int userfaultfd_wp_async(struct vm_area_struct *vma)
 	return false;
 }
 
+int wp_range_async(struct vm_area_struct *vma, unsigned long start, unsigned long len)
+{
+	return -1;
+}
+
 #endif /* CONFIG_USERFAULTFD */
 
 static inline bool pte_marker_entry_uffd_wp(swp_entry_t entry)
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 65ad172add27..9d8a43faf764 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -734,25 +734,13 @@  void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma,
 	tlb_finish_mmu(&tlb);
 }
 
-int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
-			unsigned long len, bool enable_wp,
-			atomic_t *mmap_changing)
+int __mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
+			  unsigned long len, bool enable_wp,
+			  atomic_t *mmap_changing)
 {
 	struct vm_area_struct *dst_vma;
 	unsigned long page_mask;
 	int err;
-
-	/*
-	 * Sanitize the command parameters:
-	 */
-	BUG_ON(start & ~PAGE_MASK);
-	BUG_ON(len & ~PAGE_MASK);
-
-	/* Does the address range wrap, or is the span zero-sized? */
-	BUG_ON(start + len <= start);
-
-	mmap_read_lock(dst_mm);
-
 	/*
 	 * If memory mappings are changing because of non-cooperative
 	 * operation (e.g. mremap) running in parallel, bail out and
@@ -783,6 +771,28 @@  int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
 
 	err = 0;
 out_unlock:
+	return err;
+}
+
+int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
+			unsigned long len, bool enable_wp,
+			atomic_t *mmap_changing)
+{
+	int err;
+
+	/*
+	 * Sanitize the command parameters:
+	 */
+	BUG_ON(start & ~PAGE_MASK);
+	BUG_ON(len & ~PAGE_MASK);
+
+	/* Does the address range wrap, or is the span zero-sized? */
+	BUG_ON(start + len <= start);
+
+	mmap_read_lock(dst_mm);
+
+	err = __mwriteprotect_range(dst_mm, start, len, enable_wp, mmap_changing);
+
 	mmap_read_unlock(dst_mm);
 	return err;
 }