Skip to contentSkip to conversation

mm-khugepaged-skip-shmem-with-userfaultfd.patch removed from -mm tree

merged mm-stable Andrew Morton 1 participant 1 message Apr 18, 2023

lore
Share & tools
lore

Andrew Morton

author tree notice Applied
#0 lore raw

The quilt patch titled
Subject: mm/khugepaged: skip shmem with userfaultfd
has been removed from the -mm tree. Its filename was
mm-khugepaged-skip-shmem-with-userfaultfd.patch

This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: David Stevens <[email protected]>
Subject: mm/khugepaged: skip shmem with userfaultfd
Date: Tue, 4 Apr 2023 21:01:16 +0900

Make sure that collapse_file respects any userfaultfds registered with MODE_MISSING. If userspace has any such userfaultfds registered, then for any page which it knows to be missing, it may expect a UFFD_EVENT_PAGEFAULT. This means collapse_file needs to be careful when collapsing a shmem range would result in replacing an empty page with a THP, to avoid breaking userfaultfd.

Synchronization when checking for userfaultfds in collapse_file is tricky because the mmap locks can't be used to prevent races with the registration of new userfaultfds. Instead, we provide synchronization by ensuring that userspace cannot observe the fact that pages are missing before we check for userfaultfds. Although this allows registration of a userfaultfd to race with collapse_file, it ensures that userspace cannot observe any pages transition from missing to present after such a race occurs. This makes such a race indistinguishable to the collapse occurring immediately before the userfaultfd registration.

The first step to provide this synchronization is to stop filling gaps during the loop iterating over the target range, since the page cache lock can be dropped during that loop. The second step is to fill the gaps with XA_RETRY_ENTRY after the page cache lock is acquired the final time, to avoid races with accesses to the page cache that only take the RCU read lock.

The fact that we don't fill holes during the initial iteration means that collapse_file now has to handle faults occurring during the collapse. This is done by re-validating the number of missing pages after acquiring the page cache lock for the final time.

This fix is targeted at khugepaged, but the change also applies to MADV_COLLAPSE. MADV_COLLAPSE on a range with a userfaultfd will now return EBUSY if there are any missing pages (instead of succeeding on shmem and returning EINVAL on anonymous memory). There is also now a window during MADV_COLLAPSE where a fault on a missing page will cause the syscall to fail with EAGAIN.

The fact that intermediate page cache state can no longer be observed before the rollback of a failed collapse is also technically a userspace-visible change (via at least SEEK_DATA and SEEK_END), but it is exceedingly unlikely that anything relies on being able to observe that transient state.

---

 include/trace/events/huge_memory.h |    3 
 mm/khugepaged.c                    |   99 +++++++++++++++++++++------
 2 files changed, 81 insertions(+), 21 deletions(-)
include/trace/events/huge_memory.h renamed from include/trace/events/huge_memory.h~mm-khugepaged-skip-shmem-with-userfaultfd +2 -1
@@ -38,7 +38,8 @@
 	EM( SCAN_TRUNCATED,		"truncated")			\
 	EM( SCAN_PAGE_HAS_PRIVATE,	"page_has_private")		\
 	EM( SCAN_STORE_FAILED,		"store_failed")			\
-	EMe(SCAN_COPY_MC,		"copy_poisoned_page")
+	EM( SCAN_COPY_MC,		"copy_poisoned_page")		\
+	EMe(SCAN_PAGE_FILLED,		"page_filled")
 
 #undef EM
 #undef EMe
mm/khugepaged.c renamed from mm/khugepaged.c~mm-khugepaged-skip-shmem-with-userfaultfd +79 -20

155 lines not on this page · raw message

_

Patches currently in -mm which might be from [email protected] are

mm-shmem-fix-race-in-shmem_undo_range-w-thp.patch mm-khugepaged-maintain-page-cache-uptodate-flag.patch