From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-10 21:23:09
Changelog
=========
v4->v5:
- Typo fix in the documentation update.
- Removed comment in vma_can_userfault. The same information is better covered
in the documentation update, so the comment is unnecessary (and slightly
confusing as written).
- Reworded comment for MCOPY_ATOMIC_CONTINUE mode.
- For non-shared CONTINUE, only make the PTE(s) non-writable, don't change flags
on the VMA.
- In hugetlb_mcopy_atomic_pte, always unlock the page in MCOPY_ATOMIC_CONTINUE,
even if we don't have VM_SHARED.
- In hugetlb_mcopy_atomic_pte, introduce "bool is_continue" to make that kind of
mode check more terse.
- Merged two nested if()s into a single expression in __mcopy_atomic_hugetlb.
- Moved "return -EINVAL if MCOPY_CONTINUE isn't supported for this vma type" up
one level, into __mcopy_atomic.
- Rebased onto linux-next/akpm, instead of the latest 5.11 RC. Resolved
conflicts with Mike's recent hugetlb changes.
v3->v4:
- Relaxed restriction for minor registration to allow any hugetlb VMAs, not
just those with VM_SHARED. Fixed setting VM_WRITE flag in a CONTINUE ioctl
for non-VM_SHARED VMAs.
- Reordered if() branches in hugetlb_mcopy_atomic_pte, so the conditions are
simpler and easier to read.
- Reverted most of the mfill_atomic_pte change (the anon / shmem path). Just
return -EINVAL for CONTINUE, and set zeropage = (mode ==
MCOPY_ATOMIC_ZEROPAGE), so we can keep the delta small.
- Split out adding #ifdef CONFIG_USERFAULTFD to a separate patch (instead of
lumping it together with adding UFFDIO_CONTINUE).
- Fixed signature of hugetlb_mcopy_atomic_pte for !CONFIG_HUGETLB_PAGE
(signature must be the same in either case).
- Rebased onto a newer version of Peter's patches to disable huge PMD sharing.
v2->v3:
- Added #ifdef CONFIG_USERFAULTFD around hugetlb helper functions, to fix build
errors when building without CONFIG_USERFAULTFD set.
v1->v2:
- Fixed a bug in the hugetlb_mcopy_atomic_pte retry case. We now plumb in the
enum mcopy_atomic_mode, so we can differentiate between the three cases this
function needs to handle:
1) We're doing a COPY op, and need to allocate a page, add to cache, etc.
2) We're doing a COPY op, but allocation in this function failed previously;
we're in the retry path. The page was allocated, but not e.g. added to page
cache, so that still needs to be done.
3) We're doing a CONTINUE op, we need to look up an existing page instead of
allocating a new one.
- Rebased onto a newer version of Peter's patches to disable huge PMD sharing,
which fixes syzbot complaints on some non-x86 architectures.
- Moved __VM_UFFD_FLAGS into userfaultfd_k.h, so inline helpers can use it.
- Renamed UFFD_FEATURE_MINOR_FAULT_HUGETLBFS to UFFD_FEATURE_MINOR_HUGETLBFS,
for consistency with other existing feature flags.
- Moved the userfaultfd_minor hook in hugetlb.c into the else block, so we don't
have to explicitly check for !new_page.
RFC->v1:
- Rebased onto Peter Xu's patches for disabling huge PMD sharing for certain
userfaultfd-registered areas.
- Added commits which update documentation, and add a self test which exercises
the new feature.
- Fixed reporting CONTINUE as a supported ioctl even for non-MINOR ranges.
Overview
========
This series adds a new userfaultfd registration mode,
UFFDIO_REGISTER_MODE_MINOR. This allows userspace to intercept "minor" faults.
By "minor" fault, I mean the following situation:
Let there exist two mappings (i.e., VMAs) to the same page(s) (shared memory).
One of the mappings is registered with userfaultfd (in minor mode), and the
other is not. Via the non-UFFD mapping, the underlying pages have already been
allocated & filled with some contents. The UFFD mapping has not yet been
faulted in; when it is touched for the first time, this results in what I'm
calling a "minor" fault. As a concrete example, when working with hugetlbfs, we
have huge_pte_none(), but find_lock_page() finds an existing page.
We also add a new ioctl to resolve such faults: UFFDIO_CONTINUE. The idea is,
userspace resolves the fault by either a) doing nothing if the contents are
already correct, or b) updating the underlying contents using the second,
non-UFFD mapping (via memcpy/memset or similar, or something fancier like RDMA,
or etc...). In either case, userspace issues UFFDIO_CONTINUE to tell the kernel
"I have ensured the page contents are correct, carry on setting up the mapping".
Use Case
========
Consider the use case of VM live migration (e.g. under QEMU/KVM):
1. While a VM is still running, we copy the contents of its memory to a
target machine. The pages are populated on the target by writing to the
non-UFFD mapping, using the setup described above. The VM is still running
(and therefore its memory is likely changing), so this may be repeated
several times, until we decide the target is "up to date enough".
2. We pause the VM on the source, and start executing on the target machine.
During this gap, the VM's user(s) will *see* a pause, so it is desirable to
minimize this window.
3. Between the last time any page was copied from the source to the target, and
when the VM was paused, the contents of that page may have changed - and
therefore the copy we have on the target machine is out of date. Although we
can keep track of which pages are out of date, for VMs with large amounts of
memory, it is "slow" to transfer this information to the target machine. We
want to resume execution before such a transfer would complete.
4. So, the guest begins executing on the target machine. The first time it
touches its memory (via the UFFD-registered mapping), userspace wants to
intercept this fault. Userspace checks whether or not the page is up to date,
and if not, copies the updated page from the source machine, via the non-UFFD
mapping. Finally, whether a copy was performed or not, userspace issues a
UFFDIO_CONTINUE ioctl to tell the kernel "I have ensured the page contents
are correct, carry on setting up the mapping".
We don't have to do all of the final updates on-demand. The userfaultfd manager
can, in the background, also copy over updated pages once it receives the map of
which pages are up-to-date or not.
Interaction with Existing APIs
==============================
Because it's possible to combine registration modes (e.g. a single VMA can be
userfaultfd-registered MINOR | MISSING), and because it's up to userspace how to
resolve faults once they are received, I spent some time thinking through how
the existing API interacts with the new feature.
UFFDIO_CONTINUE cannot be used to resolve non-minor faults, as it does not
allocate a new page. If UFFDIO_CONTINUE is used on a non-minor fault:
- For non-shared memory or shmem, -EINVAL is returned.
- For hugetlb, -EFAULT is returned.
UFFDIO_COPY and UFFDIO_ZEROPAGE cannot be used to resolve minor faults. Without
modifications, the existing codepath assumes a new page needs to be allocated.
This is okay, since userspace must have a second non-UFFD-registered mapping
anyway, thus there isn't much reason to want to use these in any case (just
memcpy or memset or similar).
- If UFFDIO_COPY is used on a minor fault, -EEXIST is returned.
- If UFFDIO_ZEROPAGE is used on a minor fault, -EEXIST is returned (or -EINVAL
in the case of hugetlb, as UFFDIO_ZEROPAGE is unsupported in any case).
- UFFDIO_WRITEPROTECT simply doesn't work with shared memory, and returns
-ENOENT in that case (regardless of the kind of fault).
Dependencies
============
I've included 4 commits from Peter Xu's larger series
(https://lore.kernel.org/patchwork/cover/1366017/) in this series. My changes
depend on his work, to disable huge PMD sharing for MINOR registered userfaultfd
areas. I included the 4 commits directly because a) it lets this series just be
applied and work as-is, and b) they are fairly standalone, and could potentially
be merged even without the rest of the larger series Peter submitted. Thanks
Peter!
Also, although it doesn't affect minor fault handling, I did notice that the
userfaultfd self test sometimes experienced memory corruption
(https://lore.kernel.org/patchwork/cover/1356755/). For anyone testing this
series, it may be useful to apply that series first to fix the selftest
flakiness. That series doesn't have to be merged into mainline / maintaner
branches before mine, though.
Future Work
===========
Currently the patchset only supports hugetlbfs. There is no reason it can't work
with shmem, but I expect hugetlbfs to be much more commonly used since we're
talking about backing guest memory for VMs. I plan to implement shmem support in
a follow-up patch series.
Axel Rasmussen (6):
userfaultfd: add minor fault registration mode
userfaultfd: disable huge PMD sharing for MINOR registered VMAs
userfaultfd: hugetlbfs: only compile UFFD helpers if config enabled
userfaultfd: add UFFDIO_CONTINUE ioctl
userfaultfd: update documentation to describe minor fault handling
userfaultfd/selftests: add test exercising minor fault handling
Peter Xu (4):
hugetlb: Pass vma into huge_pte_alloc() and huge_pmd_share()
hugetlb/userfaultfd: Forbid huge pmd sharing when uffd enabled
mm/hugetlb: Move flush_hugetlb_tlb_range() into hugetlb.h
hugetlb/userfaultfd: Unshare all pmds for hugetlbfs when register wp
Documentation/admin-guide/mm/userfaultfd.rst | 107 +++++++----
arch/arm64/mm/hugetlbpage.c | 7 +-
arch/ia64/mm/hugetlbpage.c | 3 +-
arch/mips/mm/hugetlbpage.c | 4 +-
arch/parisc/mm/hugetlbpage.c | 2 +-
arch/powerpc/mm/hugetlbpage.c | 3 +-
arch/s390/mm/hugetlbpage.c | 2 +-
arch/sh/mm/hugetlbpage.c | 2 +-
arch/sparc/mm/hugetlbpage.c | 6 +-
fs/proc/task_mmu.c | 1 +
fs/userfaultfd.c | 186 +++++++++++++++----
include/linux/hugetlb.h | 22 ++-
include/linux/mm.h | 1 +
include/linux/mmu_notifier.h | 1 +
include/linux/userfaultfd_k.h | 49 ++++-
include/trace/events/mmflags.h | 1 +
include/uapi/linux/userfaultfd.h | 36 +++-
mm/hugetlb.c | 116 ++++++++----
mm/userfaultfd.c | 39 ++--
tools/testing/selftests/vm/userfaultfd.c | 147 ++++++++++++++-
20 files changed, 587 insertions(+), 148 deletions(-)
--
2.30.0.478.g8a0d178c01-goog
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-10 21:23:10
From: Peter Xu <peterx@redhat.com>
It is a preparation work to be able to behave differently in the per
architecture huge_pte_alloc() according to different VMA attributes.
Pass it deeper into huge_pmd_share() so that we can avoid the find_vma() call.
Suggested-by: Mike Kravetz <redacted>
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
arch/arm64/mm/hugetlbpage.c | 4 ++--
arch/ia64/mm/hugetlbpage.c | 3 ++-
arch/mips/mm/hugetlbpage.c | 4 ++--
arch/parisc/mm/hugetlbpage.c | 2 +-
arch/powerpc/mm/hugetlbpage.c | 3 ++-
arch/s390/mm/hugetlbpage.c | 2 +-
arch/sh/mm/hugetlbpage.c | 2 +-
arch/sparc/mm/hugetlbpage.c | 6 +-----
include/linux/hugetlb.h | 5 +++--
mm/hugetlb.c | 15 ++++++++-------
mm/userfaultfd.c | 2 +-
11 files changed, 24 insertions(+), 24 deletions(-)
From: Mike Kravetz <hidden> Date: 2021-02-12 00:01:11
On 2/10/21 1:21 PM, Axel Rasmussen wrote:
From: Peter Xu <peterx@redhat.com>
It is a preparation work to be able to behave differently in the per
architecture huge_pte_alloc() according to different VMA attributes.
Pass it deeper into huge_pmd_share() so that we can avoid the find_vma() call.
Suggested-by: Mike Kravetz <redacted>
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
arch/arm64/mm/hugetlbpage.c | 4 ++--
arch/ia64/mm/hugetlbpage.c | 3 ++-
arch/mips/mm/hugetlbpage.c | 4 ++--
arch/parisc/mm/hugetlbpage.c | 2 +-
arch/powerpc/mm/hugetlbpage.c | 3 ++-
arch/s390/mm/hugetlbpage.c | 2 +-
arch/sh/mm/hugetlbpage.c | 2 +-
arch/sparc/mm/hugetlbpage.c | 6 +-----
include/linux/hugetlb.h | 5 +++--
mm/hugetlb.c | 15 ++++++++-------
mm/userfaultfd.c | 2 +-
11 files changed, 24 insertions(+), 24 deletions(-)
Thanks, this will be needed for multiple features where pmd sharing must
be disabled. And, the need to disable sharing is based on information in
the vma.
Reviewed-by: Mike Kravetz <redacted>
--
Mike Kravetz
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-10 21:23:32
From: Peter Xu <peterx@redhat.com>
Huge pmd sharing could bring problem to userfaultfd. The thing is that
userfaultfd is running its logic based on the special bits on page table
entries, however the huge pmd sharing could potentially share page table
entries for different address ranges. That could cause issues on either:
- When sharing huge pmd page tables for an uffd write protected range, the
newly mapped huge pmd range will also be write protected unexpectedly, or,
- When we try to write protect a range of huge pmd shared range, we'll first
do huge_pmd_unshare() in hugetlb_change_protection(), however that also
means the UFFDIO_WRITEPROTECT could be silently skipped for the shared
region, which could lead to data loss.
Since at it, a few other things are done altogether:
- Move want_pmd_share() from mm/hugetlb.c into linux/hugetlb.h, because
that's definitely something that arch code would like to use too
- ARM64 currently directly check against CONFIG_ARCH_WANT_HUGE_PMD_SHARE when
trying to share huge pmd. Switch to the want_pmd_share() helper.
Since at it, move vma_shareable() from huge_pmd_share() into want_pmd_share().
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
arch/arm64/mm/hugetlbpage.c | 3 +--
include/linux/hugetlb.h | 2 ++
include/linux/userfaultfd_k.h | 9 +++++++++
mm/hugetlb.c | 20 ++++++++++++++------
4 files changed, 26 insertions(+), 8 deletions(-)
From: Mike Kravetz <hidden> Date: 2021-02-12 00:22:35
On 2/10/21 1:21 PM, Axel Rasmussen wrote:
quoted hunk
From: Peter Xu <peterx@redhat.com>
Huge pmd sharing could bring problem to userfaultfd. The thing is that
userfaultfd is running its logic based on the special bits on page table
entries, however the huge pmd sharing could potentially share page table
entries for different address ranges. That could cause issues on either:
- When sharing huge pmd page tables for an uffd write protected range, the
newly mapped huge pmd range will also be write protected unexpectedly, or,
- When we try to write protect a range of huge pmd shared range, we'll first
do huge_pmd_unshare() in hugetlb_change_protection(), however that also
means the UFFDIO_WRITEPROTECT could be silently skipped for the shared
region, which could lead to data loss.
Since at it, a few other things are done altogether:
- Move want_pmd_share() from mm/hugetlb.c into linux/hugetlb.h, because
that's definitely something that arch code would like to use too
- ARM64 currently directly check against CONFIG_ARCH_WANT_HUGE_PMD_SHARE when
trying to share huge pmd. Switch to the want_pmd_share() helper.
Since at it, move vma_shareable() from huge_pmd_share() into want_pmd_share().
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
arch/arm64/mm/hugetlbpage.c | 3 +--
include/linux/hugetlb.h | 2 ++
include/linux/userfaultfd_k.h | 9 +++++++++
mm/hugetlb.c | 20 ++++++++++++++------
4 files changed, 26 insertions(+), 8 deletions(-)
This code certainly does the right thing, however I wonder if it should
be structured a little differently.
want_pmd_share() is currently just a check for CONFIG_ARCH_WANT_HUGE_PMD_SHARE.
How about leaving that mostly as is, and adding the new vma checks to
vma_shareable(). vma_shareable() would then be something like:
if (!(vma->vm_flags & VM_MAYSHARE))
return false;
#ifdef CONFIG_USERFAULTFD
if (uffd_disable_huge_pmd_share(vma)
return false;
#endif
#ifdef /* XXX */
/* add other checks for things like uffd wp and soft dirty here */
#endif /* XXX */
if (range_in_vma(vma, base, end)
return true;
return false;
Of course, this would require we leave the call to vma_shareable() at the
beginning of huge_pmd_share. It also means that we are always making a
function call into huge_pmd_share to determine if sharing is possible.
That is not any different than today. If we do not want to make that extra
function call, then I would suggest putting all that code in want_pmd_share.
It just seems that all the vma checks for sharing should be in one place
if possible.
--
Mike Kravetz
quoted hunk
/*
* Determine if start,end range within vma could be mapped by shared pmd.
* If yes, adjust start and end to cover range associated with possible
From: Peter Xu <peterx@redhat.com> Date: 2021-02-12 20:42:05
On Thu, Feb 11, 2021 at 04:19:55PM -0800, Mike Kravetz wrote:
want_pmd_share() is currently just a check for CONFIG_ARCH_WANT_HUGE_PMD_SHARE.
How about leaving that mostly as is, and adding the new vma checks to
vma_shareable(). vma_shareable() would then be something like:
if (!(vma->vm_flags & VM_MAYSHARE))
return false;
#ifdef CONFIG_USERFAULTFD
if (uffd_disable_huge_pmd_share(vma)
return false;
#endif
#ifdef /* XXX */
/* add other checks for things like uffd wp and soft dirty here */
#endif /* XXX */
if (range_in_vma(vma, base, end)
return true;
return false;
Of course, this would require we leave the call to vma_shareable() at the
beginning of huge_pmd_share. It also means that we are always making a
function call into huge_pmd_share to determine if sharing is possible.
That is not any different than today. If we do not want to make that extra
function call, then I would suggest putting all that code in want_pmd_share.
It just seems that all the vma checks for sharing should be in one place
if possible.
I don't worry a lot on that since we've already got huge_pte_alloc() which
takes care of huge pmd sharing case, so I don't expect e.g. even most hugetlb
developers to use want_pmd_share() at all, because huge_pte_alloc() will be the
one that frequently got called.
But yeah we can definitely put the check logic into huge_pmd_share() too.
Looking at above code it looks still worth a helper like want_pmd_share() or
with some other name. Then... instead of making this complicated, how about I
mostly keep this patch but move want_pmd_share() call into huge_pmd_share()
instead?
Btw, Axel, it seems there will still be some respins on the pmd sharing
patches. Since it turns out it'll be shared by multiple tasks now, do you mind
I pick those out and send them separately? Then we can consolidate this part
to move on with either the rest of the tasks we've got on hand.
Thanks,
--
Peter Xu
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-12 20:48:51
On Fri, Feb 12, 2021 at 12:40 PM Peter Xu [off-list ref] wrote:
On Thu, Feb 11, 2021 at 04:19:55PM -0800, Mike Kravetz wrote:
quoted
want_pmd_share() is currently just a check for CONFIG_ARCH_WANT_HUGE_PMD_SHARE.
How about leaving that mostly as is, and adding the new vma checks to
vma_shareable(). vma_shareable() would then be something like:
if (!(vma->vm_flags & VM_MAYSHARE))
return false;
#ifdef CONFIG_USERFAULTFD
if (uffd_disable_huge_pmd_share(vma)
return false;
#endif
#ifdef /* XXX */
/* add other checks for things like uffd wp and soft dirty here */
#endif /* XXX */
if (range_in_vma(vma, base, end)
return true;
return false;
Of course, this would require we leave the call to vma_shareable() at the
beginning of huge_pmd_share. It also means that we are always making a
function call into huge_pmd_share to determine if sharing is possible.
That is not any different than today. If we do not want to make that extra
function call, then I would suggest putting all that code in want_pmd_share.
It just seems that all the vma checks for sharing should be in one place
if possible.
I don't worry a lot on that since we've already got huge_pte_alloc() which
takes care of huge pmd sharing case, so I don't expect e.g. even most hugetlb
developers to use want_pmd_share() at all, because huge_pte_alloc() will be the
one that frequently got called.
But yeah we can definitely put the check logic into huge_pmd_share() too.
Looking at above code it looks still worth a helper like want_pmd_share() or
with some other name. Then... instead of making this complicated, how about I
mostly keep this patch but move want_pmd_share() call into huge_pmd_share()
instead?
Btw, Axel, it seems there will still be some respins on the pmd sharing
patches. Since it turns out it'll be shared by multiple tasks now, do you mind
I pick those out and send them separately? Then we can consolidate this part
to move on with either the rest of the tasks we've got on hand.
Sounds good to me. :) Thanks Peter + Mike for working on this!
From: Mike Kravetz <hidden> Date: 2021-02-12 21:29:48
On 2/12/21 12:47 PM, Axel Rasmussen wrote:
On Fri, Feb 12, 2021 at 12:40 PM Peter Xu [off-list ref] wrote:
quoted
On Thu, Feb 11, 2021 at 04:19:55PM -0800, Mike Kravetz wrote:
quoted
want_pmd_share() is currently just a check for CONFIG_ARCH_WANT_HUGE_PMD_SHARE.
How about leaving that mostly as is, and adding the new vma checks to
vma_shareable(). vma_shareable() would then be something like:
if (!(vma->vm_flags & VM_MAYSHARE))
return false;
#ifdef CONFIG_USERFAULTFD
if (uffd_disable_huge_pmd_share(vma)
return false;
#endif
#ifdef /* XXX */
/* add other checks for things like uffd wp and soft dirty here */
#endif /* XXX */
if (range_in_vma(vma, base, end)
return true;
return false;
Of course, this would require we leave the call to vma_shareable() at the
beginning of huge_pmd_share. It also means that we are always making a
function call into huge_pmd_share to determine if sharing is possible.
That is not any different than today. If we do not want to make that extra
function call, then I would suggest putting all that code in want_pmd_share.
It just seems that all the vma checks for sharing should be in one place
if possible.
I don't worry a lot on that since we've already got huge_pte_alloc() which
takes care of huge pmd sharing case, so I don't expect e.g. even most hugetlb
developers to use want_pmd_share() at all, because huge_pte_alloc() will be the
one that frequently got called.
But yeah we can definitely put the check logic into huge_pmd_share() too.
Looking at above code it looks still worth a helper like want_pmd_share() or
with some other name. Then... instead of making this complicated, how about I
mostly keep this patch but move want_pmd_share() call into huge_pmd_share()
instead?
When looking at this again, all I was suggesting was a single routine to
check for the possibility of pmd sharing. That is what the version of
want_pmd_share in this patch does.
I have some patches for future optimizations that only take i_mmap_rwsem
in the fault path if sharing is possible. This is before huge_pte_alloc.
want_pmd_share as defined in this patch would work for that.
Sorry for the noise.
--
Mike Kravetz
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-10 21:24:00
From: Peter Xu <peterx@redhat.com>
Prepare for it to be called outside of mm/hugetlb.c.
Reviewed-by: Mike Kravetz <redacted>
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
include/linux/hugetlb.h | 8 ++++++++
mm/hugetlb.c | 8 --------
2 files changed, 8 insertions(+), 8 deletions(-)
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-10 21:24:04
From: Peter Xu <peterx@redhat.com>
Huge pmd sharing for hugetlbfs is racy with userfaultfd-wp because
userfaultfd-wp is always based on pgtable entries, so they cannot be shared.
Walk the hugetlb range and unshare all such mappings if there is, right before
UFFDIO_REGISTER will succeed and return to userspace.
This will pair with want_pmd_share() in hugetlb code so that huge pmd sharing
is completely disabled for userfaultfd-wp registered range.
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
fs/userfaultfd.c | 48 ++++++++++++++++++++++++++++++++++++
include/linux/mmu_notifier.h | 1 +
2 files changed, 49 insertions(+)
From: Mike Kravetz <hidden> Date: 2021-02-12 18:17:20
On 2/10/21 1:21 PM, Axel Rasmussen wrote:
quoted hunk
From: Peter Xu <peterx@redhat.com>
Huge pmd sharing for hugetlbfs is racy with userfaultfd-wp because
userfaultfd-wp is always based on pgtable entries, so they cannot be shared.
Walk the hugetlb range and unshare all such mappings if there is, right before
UFFDIO_REGISTER will succeed and return to userspace.
This will pair with want_pmd_share() in hugetlb code so that huge pmd sharing
is completely disabled for userfaultfd-wp registered range.
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
fs/userfaultfd.c | 48 ++++++++++++++++++++++++++++++++++++
include/linux/mmu_notifier.h | 1 +
2 files changed, 49 insertions(+)
This code will certainly work as intended. However, I wonder if we should
try to optimize and only flush and call huge_pmd_unshare for addresses where
sharing is possible. Consider this worst case example:
vm_start = 8G + 2M
vm_end = 11G - 2M
The vma is 'almost' 3G in size, yet only the range 9G to 10G is possibly
shared. This routine will potentially call lock/unlock ptl and call
huge_pmd_share for every huge page in the range. Ideally, we should only
make one call to huge_pmd_share with address 9G. If the unshare is
successful or not, we are done. The subtle manipulation of &address in
huge_pmd_unshare will result in only one call if the unshare is successful,
but if unsuccessful we will unnecessarily call huge_pmd_unshare for each
address in the range.
Maybe we start by rounding up vm_start by PUD_SIZE and rounding down
vm_end by PUD_SIZE.
I don't claim to know much about mmu notifiers. Currently, we use other
event notifiers such as MMU_NOTIFY_CLEAR. I guess we do 'clear' page table
entries if we unshare. More than happy to have a MMU_NOTIFY_HUGETLB_UNSHARE
event, but will consumers of the notifications know what this new event type
means? And, if we introduce this should we use this other places where
huge_pmd_unshare is called?
--
Mike Kravetz
From: Peter Xu <peterx@redhat.com> Date: 2021-02-12 21:20:53
On Fri, Feb 12, 2021 at 10:11:39AM -0800, Mike Kravetz wrote:
On 2/10/21 1:21 PM, Axel Rasmussen wrote:
quoted
From: Peter Xu <peterx@redhat.com>
Huge pmd sharing for hugetlbfs is racy with userfaultfd-wp because
userfaultfd-wp is always based on pgtable entries, so they cannot be shared.
Walk the hugetlb range and unshare all such mappings if there is, right before
UFFDIO_REGISTER will succeed and return to userspace.
This will pair with want_pmd_share() in hugetlb code so that huge pmd sharing
is completely disabled for userfaultfd-wp registered range.
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
fs/userfaultfd.c | 48 ++++++++++++++++++++++++++++++++++++
include/linux/mmu_notifier.h | 1 +
2 files changed, 49 insertions(+)
This code will certainly work as intended. However, I wonder if we should
try to optimize and only flush and call huge_pmd_unshare for addresses where
sharing is possible. Consider this worst case example:
vm_start = 8G + 2M
vm_end = 11G - 2M
The vma is 'almost' 3G in size, yet only the range 9G to 10G is possibly
shared. This routine will potentially call lock/unlock ptl and call
huge_pmd_share for every huge page in the range. Ideally, we should only
make one call to huge_pmd_share with address 9G. If the unshare is
successful or not, we are done. The subtle manipulation of &address in
huge_pmd_unshare will result in only one call if the unshare is successful,
but if unsuccessful we will unnecessarily call huge_pmd_unshare for each
address in the range.
Maybe we start by rounding up vm_start by PUD_SIZE and rounding down
vm_end by PUD_SIZE.
I didn't think that lot since it's slow path, but yeah if that's faster and
without extra logic, then why not. :)
I don't claim to know much about mmu notifiers. Currently, we use other
event notifiers such as MMU_NOTIFY_CLEAR. I guess we do 'clear' page table
entries if we unshare. More than happy to have a MMU_NOTIFY_HUGETLB_UNSHARE
event, but will consumers of the notifications know what this new event type
means? And, if we introduce this should we use this other places where
huge_pmd_unshare is called?
Yeah AFAICT that is a new feature to mmu notifiers and it's not really used a
lot by consumers yet. Hmm... is there really any consumer at all? I simply
grepped MMU_NOTIFY_UNMAP and see no hook took special care of that. So it's
some extra information that the upper layer would like to deliever to the
notifiers, it's just not vastly used so far.
So far I didn't worry too much on that either. MMU_NOTIFY_HUGETLB_UNSHARE is
introduced here simply because I tried to make it explicit, then it's easy to
be overwritten one day if we think huge pmd unshare is not worth a standalone
flag but reuse some other common one. But I think at least I owe one
documentation of that new enum. :)
I'm not extremely willing to touch the rest callers of huge pmd unshare yet,
unless I've a solid reason. E.g., one day maybe one mmu notifier hook would
start to monitor some events, then that's a better place imho to change them.
Otherwise any of my future change could be vague, imho.
For this patch - how about I simply go back to use MMU_NOTIFIER_CLEAR instead?
Thanks,
--
Peter Xu
I don't claim to know much about mmu notifiers. Currently, we use other
event notifiers such as MMU_NOTIFY_CLEAR. I guess we do 'clear' page table
entries if we unshare. More than happy to have a MMU_NOTIFY_HUGETLB_UNSHARE
event, but will consumers of the notifications know what this new event type
means? And, if we introduce this should we use this other places where
huge_pmd_unshare is called?
Yeah AFAICT that is a new feature to mmu notifiers and it's not really used a
lot by consumers yet. Hmm... is there really any consumer at all? I simply
grepped MMU_NOTIFY_UNMAP and see no hook took special care of that. So it's
some extra information that the upper layer would like to deliever to the
notifiers, it's just not vastly used so far.
So far I didn't worry too much on that either. MMU_NOTIFY_HUGETLB_UNSHARE is
introduced here simply because I tried to make it explicit, then it's easy to
be overwritten one day if we think huge pmd unshare is not worth a standalone
flag but reuse some other common one. But I think at least I owe one
documentation of that new enum. :)
I'm not extremely willing to touch the rest callers of huge pmd unshare yet,
unless I've a solid reason. E.g., one day maybe one mmu notifier hook would
start to monitor some events, then that's a better place imho to change them.
Otherwise any of my future change could be vague, imho.
For this patch - how about I simply go back to use MMU_NOTIFIER_CLEAR instead?
I'm good with the new MMU_NOTIFY_HUGETLB_UNSHARE and agree with your reasoning
for adding it. I really did not know enough about usage which caused me to
question.
--
Mike Kravetz
From: Peter Xu <peterx@redhat.com> Date: 2021-02-12 22:15:53
On Fri, Feb 12, 2021 at 01:34:03PM -0800, Mike Kravetz wrote:
I'm good with the new MMU_NOTIFY_HUGETLB_UNSHARE and agree with your reasoning
for adding it. I really did not know enough about usage which caused me to
question.
It actually is a good question.. Because after a 2nd thought I cannot think of
any mmu notifier usage to explicitly listen to huge pmd unshare event - it
could be just a too internal impl detail of hugetlbfs. I think any new flag
should justify itself when introduced. Before I could justify it, I think
MMU_NOTIFIER_CLEAR is indeed more suitable.
Thanks,
--
Peter Xu
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-10 21:24:27
This feature allows userspace to intercept "minor" faults. By "minor"
faults, I mean the following situation:
Let there exist two mappings (i.e., VMAs) to the same page(s). One of
the mappings is registered with userfaultfd (in minor mode), and the
other is not. Via the non-UFFD mapping, the underlying pages have
already been allocated & filled with some contents. The UFFD mapping
has not yet been faulted in; when it is touched for the first time,
this results in what I'm calling a "minor" fault. As a concrete
example, when working with hugetlbfs, we have huge_pte_none(), but
find_lock_page() finds an existing page.
This commit adds the new registration mode, and sets the relevant flag
on the VMAs being registered. In the hugetlb fault path, if we find
that we have huge_pte_none(), but find_lock_page() does indeed find an
existing page, then we have a "minor" fault, and if the VMA has the
userfaultfd registration flag, we call into userfaultfd to handle it.
Why add a new registration mode, as opposed to adding a feature to
MISSING registration, like UFFD_FEATURE_SIGBUS?
- The semantics are significantly different. UFFDIO_COPY or
UFFDIO_ZEROPAGE do not make sense for these minor faults; userspace
would instead just memset() or memcpy() or whatever via the non-UFFD
mapping. Unlike MISSING registration, MINOR registration only makes
sense for hugetlbfs (or, in the future, shmem), as this is the only
way to get two VMAs to a single set of underlying pages.
- Doing so would make handle_userfault()'s "reason" argument confusing.
We'd pass in "MISSING" even if the pages weren't really missing.
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
fs/proc/task_mmu.c | 1 +
fs/userfaultfd.c | 71 ++++++++++++++++++--------------
include/linux/mm.h | 1 +
include/linux/userfaultfd_k.h | 15 ++++++-
include/trace/events/mmflags.h | 1 +
include/uapi/linux/userfaultfd.h | 15 ++++++-
mm/hugetlb.c | 32 ++++++++++++++
7 files changed, 102 insertions(+), 34 deletions(-)
@@ -651,6 +651,7 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)[ilog2(VM_MTE)]="mt",[ilog2(VM_MTE_ALLOWED)]="",#endif+[ilog2(VM_UFFD_MINOR)]="ui",#ifdef CONFIG_ARCH_HAS_PKEYS/* These come out via ProtectionKey: */[ilog2(VM_PKEY_BIT0)]="",
@@ -401,8 +398,10 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason)BUG_ON(ctx->mm!=mm);-VM_BUG_ON(reason&~(VM_UFFD_MISSING|VM_UFFD_WP));-VM_BUG_ON(!(reason&VM_UFFD_MISSING)^!!(reason&VM_UFFD_WP));+/* Any unrecognized flag is a bug. */+VM_BUG_ON(reason&~__VM_UFFD_FLAGS);+/* 0 or > 1 flags set is a bug; we expect exactly 1. */+VM_BUG_ON(!reason||!!(reason&(reason-1)));if(ctx->features&UFFD_FEATURE_SIGBUS)gotoout;
@@ -17,6 +17,9 @@#include<linux/mm.h>#include<asm-generic/pgtable_uffd.h>+/* The set of all possible UFFD-related VM flags. */+#define __VM_UFFD_FLAGS (VM_UFFD_MISSING | VM_UFFD_WP | VM_UFFD_MINOR)+/**CAREFUL:Checkinclude/uapi/asm-generic/fcntl.hwhendefining*newflags,sincetheymightcollidewithO_*ones.Wewant
@@ -127,6 +131,7 @@ struct uffd_msg {/* flags for UFFD_EVENT_PAGEFAULT */#define UFFD_PAGEFAULT_FLAG_WRITE (1<<0) /* If this was a write fault */#define UFFD_PAGEFAULT_FLAG_WP (1<<1) /* If reason is VM_UFFD_WP */+#define UFFD_PAGEFAULT_FLAG_MINOR (1<<2) /* If reason is VM_UFFD_MINOR */structuffdio_api{/* userland asks for an API number and the features to enable */
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-11 19:29:49
On Wed, Feb 10, 2021 at 1:22 PM Axel Rasmussen [off-list ref] wrote:
quoted hunk
This feature allows userspace to intercept "minor" faults. By "minor"
faults, I mean the following situation:
Let there exist two mappings (i.e., VMAs) to the same page(s). One of
the mappings is registered with userfaultfd (in minor mode), and the
other is not. Via the non-UFFD mapping, the underlying pages have
already been allocated & filled with some contents. The UFFD mapping
has not yet been faulted in; when it is touched for the first time,
this results in what I'm calling a "minor" fault. As a concrete
example, when working with hugetlbfs, we have huge_pte_none(), but
find_lock_page() finds an existing page.
This commit adds the new registration mode, and sets the relevant flag
on the VMAs being registered. In the hugetlb fault path, if we find
that we have huge_pte_none(), but find_lock_page() does indeed find an
existing page, then we have a "minor" fault, and if the VMA has the
userfaultfd registration flag, we call into userfaultfd to handle it.
Why add a new registration mode, as opposed to adding a feature to
MISSING registration, like UFFD_FEATURE_SIGBUS?
- The semantics are significantly different. UFFDIO_COPY or
UFFDIO_ZEROPAGE do not make sense for these minor faults; userspace
would instead just memset() or memcpy() or whatever via the non-UFFD
mapping. Unlike MISSING registration, MINOR registration only makes
sense for hugetlbfs (or, in the future, shmem), as this is the only
way to get two VMAs to a single set of underlying pages.
- Doing so would make handle_userfault()'s "reason" argument confusing.
We'd pass in "MISSING" even if the pages weren't really missing.
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
fs/proc/task_mmu.c | 1 +
fs/userfaultfd.c | 71 ++++++++++++++++++--------------
include/linux/mm.h | 1 +
include/linux/userfaultfd_k.h | 15 ++++++-
include/trace/events/mmflags.h | 1 +
include/uapi/linux/userfaultfd.h | 15 ++++++-
mm/hugetlb.c | 32 ++++++++++++++
7 files changed, 102 insertions(+), 34 deletions(-)
@@ -651,6 +651,7 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)[ilog2(VM_MTE)]="mt",[ilog2(VM_MTE_ALLOWED)]="",#endif+[ilog2(VM_UFFD_MINOR)]="ui",#ifdef CONFIG_ARCH_HAS_PKEYS/* These come out via ProtectionKey: */[ilog2(VM_PKEY_BIT0)]="",
@@ -401,8 +398,10 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason)BUG_ON(ctx->mm!=mm);-VM_BUG_ON(reason&~(VM_UFFD_MISSING|VM_UFFD_WP));-VM_BUG_ON(!(reason&VM_UFFD_MISSING)^!!(reason&VM_UFFD_WP));+/* Any unrecognized flag is a bug. */+VM_BUG_ON(reason&~__VM_UFFD_FLAGS);+/* 0 or > 1 flags set is a bug; we expect exactly 1. */+VM_BUG_ON(!reason||!!(reason&(reason-1)));if(ctx->features&UFFD_FEATURE_SIGBUS)gotoout;
@@ -276,6 +276,7 @@ extern unsigned int kobjsize(const void *objp);#define VM_PFNMAP 0x00000400 /* Page-ranges managed without "struct page", just pure PFN */#define VM_DENYWRITE 0x00000800 /* ETXTBSY on write attempts.. */#define VM_UFFD_WP 0x00001000 /* wrprotect pages tracking */+#define VM_UFFD_MINOR 0x00002000 /* minor fault interception */
Ah, I had added this just after VM_UFFD_WP, without noticing that this
would be sharing a bit with VM_LOCKED. That seems like not such a
great idea.
I don't see another unused bit, and I don't see some other obvious
candidate to share with. So, the solution that comes to mind is
something like:
- Since it isn't feasible to have one VM_ flag per UFFD trigger type,
handle_userfault()'s "reason" argument should be some enumeration of
possible UFFD reasons instead.
- Introduce a path where handle_userfault() can return 0, meaning "you
called into me, but I am not meant to be handling this fault per the
userfaultfd_ctx, handle it normally instead".
- Use VM_UFFD_MISSING to decide whether or not to call
handle_userfault(), whether it was a missing or minor fault.
Unless there are objections or some simpler idea, I'll send a v6 with
this change.
quoted hunk
#define VM_LOCKED 0x00002000
#define VM_IO 0x00004000 /* Memory mapped I/O or similar */
@@ -17,6 +17,9 @@#include<linux/mm.h>#include<asm-generic/pgtable_uffd.h>+/* The set of all possible UFFD-related VM flags. */+#define __VM_UFFD_FLAGS (VM_UFFD_MISSING | VM_UFFD_WP | VM_UFFD_MINOR)+/**CAREFUL:Checkinclude/uapi/asm-generic/fcntl.hwhendefining*newflags,sincetheymightcollidewithO_*ones.Wewant
@@ -127,6 +131,7 @@ struct uffd_msg {/* flags for UFFD_EVENT_PAGEFAULT */#define UFFD_PAGEFAULT_FLAG_WRITE (1<<0) /* If this was a write fault */#define UFFD_PAGEFAULT_FLAG_WP (1<<1) /* If reason is VM_UFFD_WP */+#define UFFD_PAGEFAULT_FLAG_MINOR (1<<2) /* If reason is VM_UFFD_MINOR */structuffdio_api{/* userland asks for an API number and the features to enable */
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-11 20:59:56
On Thu, Feb 11, 2021 at 11:28 AM Axel Rasmussen
[off-list ref] wrote:
On Wed, Feb 10, 2021 at 1:22 PM Axel Rasmussen [off-list ref] wrote:
quoted
This feature allows userspace to intercept "minor" faults. By "minor"
faults, I mean the following situation:
Let there exist two mappings (i.e., VMAs) to the same page(s). One of
the mappings is registered with userfaultfd (in minor mode), and the
other is not. Via the non-UFFD mapping, the underlying pages have
already been allocated & filled with some contents. The UFFD mapping
has not yet been faulted in; when it is touched for the first time,
this results in what I'm calling a "minor" fault. As a concrete
example, when working with hugetlbfs, we have huge_pte_none(), but
find_lock_page() finds an existing page.
This commit adds the new registration mode, and sets the relevant flag
on the VMAs being registered. In the hugetlb fault path, if we find
that we have huge_pte_none(), but find_lock_page() does indeed find an
existing page, then we have a "minor" fault, and if the VMA has the
userfaultfd registration flag, we call into userfaultfd to handle it.
Why add a new registration mode, as opposed to adding a feature to
MISSING registration, like UFFD_FEATURE_SIGBUS?
- The semantics are significantly different. UFFDIO_COPY or
UFFDIO_ZEROPAGE do not make sense for these minor faults; userspace
would instead just memset() or memcpy() or whatever via the non-UFFD
mapping. Unlike MISSING registration, MINOR registration only makes
sense for hugetlbfs (or, in the future, shmem), as this is the only
way to get two VMAs to a single set of underlying pages.
- Doing so would make handle_userfault()'s "reason" argument confusing.
We'd pass in "MISSING" even if the pages weren't really missing.
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
fs/proc/task_mmu.c | 1 +
fs/userfaultfd.c | 71 ++++++++++++++++++--------------
include/linux/mm.h | 1 +
include/linux/userfaultfd_k.h | 15 ++++++-
include/trace/events/mmflags.h | 1 +
include/uapi/linux/userfaultfd.h | 15 ++++++-
mm/hugetlb.c | 32 ++++++++++++++
7 files changed, 102 insertions(+), 34 deletions(-)
@@ -651,6 +651,7 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)[ilog2(VM_MTE)]="mt",[ilog2(VM_MTE_ALLOWED)]="",#endif+[ilog2(VM_UFFD_MINOR)]="ui",#ifdef CONFIG_ARCH_HAS_PKEYS/* These come out via ProtectionKey: */[ilog2(VM_PKEY_BIT0)]="",
@@ -401,8 +398,10 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason)BUG_ON(ctx->mm!=mm);-VM_BUG_ON(reason&~(VM_UFFD_MISSING|VM_UFFD_WP));-VM_BUG_ON(!(reason&VM_UFFD_MISSING)^!!(reason&VM_UFFD_WP));+/* Any unrecognized flag is a bug. */+VM_BUG_ON(reason&~__VM_UFFD_FLAGS);+/* 0 or > 1 flags set is a bug; we expect exactly 1. */+VM_BUG_ON(!reason||!!(reason&(reason-1)));if(ctx->features&UFFD_FEATURE_SIGBUS)gotoout;
@@ -276,6 +276,7 @@ extern unsigned int kobjsize(const void *objp);#define VM_PFNMAP 0x00000400 /* Page-ranges managed without "struct page", just pure PFN */#define VM_DENYWRITE 0x00000800 /* ETXTBSY on write attempts.. */#define VM_UFFD_WP 0x00001000 /* wrprotect pages tracking */+#define VM_UFFD_MINOR 0x00002000 /* minor fault interception */
Ah, I had added this just after VM_UFFD_WP, without noticing that this
would be sharing a bit with VM_LOCKED. That seems like not such a
great idea.
I don't see another unused bit, and I don't see some other obvious
candidate to share with. So, the solution that comes to mind is
something like:
- Since it isn't feasible to have one VM_ flag per UFFD trigger type,
handle_userfault()'s "reason" argument should be some enumeration of
possible UFFD reasons instead.
- Introduce a path where handle_userfault() can return 0, meaning "you
called into me, but I am not meant to be handling this fault per the
userfaultfd_ctx, handle it normally instead".
Ah, yeah this doesn't work either. The context is per-fd, not
per-registration. We don't write down the requested registration mode
anywhere other than the VM flags. So then, in lieu of some larger
redesign I think minor fault handling has to be a per-fd option, not a
per-registration option -- basically, it has to be a UFFD_FEATURE_*
flag instead of a separate mode.
- Use VM_UFFD_MISSING to decide whether or not to call
handle_userfault(), whether it was a missing or minor fault.
Unless there are objections or some simpler idea, I'll send a v6 with
this change.
quoted
#define VM_LOCKED 0x00002000
#define VM_IO 0x00004000 /* Memory mapped I/O or similar */
@@ -17,6 +17,9 @@#include<linux/mm.h>#include<asm-generic/pgtable_uffd.h>+/* The set of all possible UFFD-related VM flags. */+#define __VM_UFFD_FLAGS (VM_UFFD_MISSING | VM_UFFD_WP | VM_UFFD_MINOR)+/**CAREFUL:Checkinclude/uapi/asm-generic/fcntl.hwhendefining*newflags,sincetheymightcollidewithO_*ones.Wewant
@@ -127,6 +131,7 @@ struct uffd_msg {/* flags for UFFD_EVENT_PAGEFAULT */#define UFFD_PAGEFAULT_FLAG_WRITE (1<<0) /* If this was a write fault */#define UFFD_PAGEFAULT_FLAG_WP (1<<1) /* If reason is VM_UFFD_WP */+#define UFFD_PAGEFAULT_FLAG_MINOR (1<<2) /* If reason is VM_UFFD_MINOR */structuffdio_api{/* userland asks for an API number and the features to enable */
From: Matthew Wilcox <willy@infradead.org> Date: 2021-02-12 22:23:24
On Thu, Feb 11, 2021 at 11:28:09AM -0800, Axel Rasmussen wrote:
Ah, I had added this just after VM_UFFD_WP, without noticing that this
would be sharing a bit with VM_LOCKED. That seems like not such a
great idea.
I don't see another unused bit, and I don't see some other obvious
candidate to share with. So, the solution that comes to mind is
it'd be even better if you didn't use the last unused bit for UFFD_WP.
not sure how feasible that is, but you can see we're really short on
bits here.
From: Peter Xu <peterx@redhat.com> Date: 2021-02-12 22:51:36
On Fri, Feb 12, 2021 at 10:21:45PM +0000, Matthew Wilcox wrote:
On Thu, Feb 11, 2021 at 11:28:09AM -0800, Axel Rasmussen wrote:
quoted
Ah, I had added this just after VM_UFFD_WP, without noticing that this
would be sharing a bit with VM_LOCKED. That seems like not such a
great idea.
I don't see another unused bit, and I don't see some other obvious
candidate to share with. So, the solution that comes to mind is
it'd be even better if you didn't use the last unused bit for UFFD_WP.
not sure how feasible that is, but you can see we're really short on
bits here.
UFFD_WP is used now for anonymouse already.. And the support for hugetlbfs and
shmem is in rfc stage on the list.
Is it possible to use CONFIG_ARCH_USES_HIGH_VMA_FLAGS here? So far uffd-wp is
only working for 64 bit x86 too due to enlarged pte space. Maybe we can also
let minor mode to only support 64 bit hosts.
Thanks,
--
Peter Xu
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-12 22:55:48
On Fri, Feb 12, 2021 at 2:44 PM Peter Xu [off-list ref] wrote:
On Fri, Feb 12, 2021 at 10:21:45PM +0000, Matthew Wilcox wrote:
quoted
On Thu, Feb 11, 2021 at 11:28:09AM -0800, Axel Rasmussen wrote:
quoted
Ah, I had added this just after VM_UFFD_WP, without noticing that this
would be sharing a bit with VM_LOCKED. That seems like not such a
great idea.
I don't see another unused bit, and I don't see some other obvious
candidate to share with. So, the solution that comes to mind is
it'd be even better if you didn't use the last unused bit for UFFD_WP.
not sure how feasible that is, but you can see we're really short on
bits here.
UFFD_WP is used now for anonymouse already.. And the support for hugetlbfs and
shmem is in rfc stage on the list.
Is it possible to use CONFIG_ARCH_USES_HIGH_VMA_FLAGS here? So far uffd-wp is
only working for 64 bit x86 too due to enlarged pte space. Maybe we can also
let minor mode to only support 64 bit hosts.
At least for my / Google's purposes, I don't care about 32-bit support
for this feature. I do care about both x86_64 and arm64, though. So
it's a possibility.
Alternatively, the "it's an API feature not a registration mode"
approach I sent in my v6 also works for me, although it has some
drawbacks.
Another option is, would it be terrible to add an extra u16 or u32 for
UFFD flags to vm_area_struct (say within vm_userfaultfd_ctx)?
Historically we've already added a pointer, so maybe an extra say 16
bits isn't so bad? This would avoid using *any* VM_* flags for UFFD,
even VM_UFFD_MISSING could be in this new flag field.
From: Peter Xu <peterx@redhat.com> Date: 2021-02-12 23:03:29
On Fri, Feb 12, 2021 at 02:51:17PM -0800, Axel Rasmussen wrote:
On Fri, Feb 12, 2021 at 2:44 PM Peter Xu [off-list ref] wrote:
quoted
On Fri, Feb 12, 2021 at 10:21:45PM +0000, Matthew Wilcox wrote:
quoted
On Thu, Feb 11, 2021 at 11:28:09AM -0800, Axel Rasmussen wrote:
quoted
Ah, I had added this just after VM_UFFD_WP, without noticing that this
would be sharing a bit with VM_LOCKED. That seems like not such a
great idea.
I don't see another unused bit, and I don't see some other obvious
candidate to share with. So, the solution that comes to mind is
it'd be even better if you didn't use the last unused bit for UFFD_WP.
not sure how feasible that is, but you can see we're really short on
bits here.
UFFD_WP is used now for anonymouse already.. And the support for hugetlbfs and
shmem is in rfc stage on the list.
Is it possible to use CONFIG_ARCH_USES_HIGH_VMA_FLAGS here? So far uffd-wp is
only working for 64 bit x86 too due to enlarged pte space. Maybe we can also
let minor mode to only support 64 bit hosts.
At least for my / Google's purposes, I don't care about 32-bit support
for this feature. I do care about both x86_64 and arm64, though. So
it's a possibility.
Alternatively, the "it's an API feature not a registration mode"
approach I sent in my v6 also works for me, although it has some
drawbacks.
Per-vma has finer granularity and logically more flexible. If it's low hanging
fruit, let's think about it more before giving up so quickly.
Sorry I commented late for this - I got diverged a bit in the past days. While
you worked on it so fast (which in many cases still a good thing :).
Another option is, would it be terrible to add an extra u16 or u32 for
UFFD flags to vm_area_struct (say within vm_userfaultfd_ctx)?
Historically we've already added a pointer, so maybe an extra say 16
bits isn't so bad? This would avoid using *any* VM_* flags for UFFD,
even VM_UFFD_MISSING could be in this new flag field.
For 64bit hosts there're still places for vm_flags. It's just 32bit, while
there's option to make it 64bit-only. Even if we'd add a new field, those bits
were still unused on 64bit hosts. IMHO we should try to use them before adding
new field which will actually impact all hosts.
Thanks,
--
Peter Xu
From: Mike Kravetz <hidden> Date: 2021-02-12 19:20:41
On 2/10/21 1:21 PM, Axel Rasmussen wrote:
This feature allows userspace to intercept "minor" faults. By "minor"
faults, I mean the following situation:
Let there exist two mappings (i.e., VMAs) to the same page(s). One of
the mappings is registered with userfaultfd (in minor mode), and the
other is not. Via the non-UFFD mapping, the underlying pages have
already been allocated & filled with some contents. The UFFD mapping
has not yet been faulted in; when it is touched for the first time,
this results in what I'm calling a "minor" fault. As a concrete
example, when working with hugetlbfs, we have huge_pte_none(), but
find_lock_page() finds an existing page.
Do we want to intercept the fault if it is for a private mapping that
will COW the page in the page cache? I think 'yes' but just want to
confirm. The code added to hugetlb_no_page will intercept these COW
accesses.
<snip>
@@ -4366,6 +4366,38 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm,VM_FAULT_SET_HINDEX(hstate_index(h));gotobackout_unlocked;}++/* Check for page in userfault range. */+if(userfaultfd_minor(vma)){+u32hash;+structvm_faultvmf={+.vma=vma,+.address=haddr,+.flags=flags,+/*+*Hardtodebugifitendsupbeingusedbya+*calleethatassumessomethingaboutthe+*otheruninitializedfields...sameasin+*memory.c+*/+};++unlock_page(page);++/*+*hugetlb_fault_mutexandi_mmap_rwsemmustbedropped+*beforehandlinguserfault.Reacquireafterhandling+*faulttomakecallingcodesimpler.+*/++hash=hugetlb_fault_mutex_hash(mapping,idx);+mutex_unlock(&hugetlb_fault_mutex_table[hash]);+i_mmap_unlock_read(mapping);
After dropping all the locks, we only hold a reference to the page in the
page cache. I 'think' someone else could hole punch the page and remove it
from the cache. IIUC, state changing while processing uffd faults is something
that users need to deal with? Just need to make sure there are no assumptions
in the kernel code.
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-12 19:35:15
On Fri, Feb 12, 2021 at 11:18 AM Mike Kravetz [off-list ref] wrote:
On 2/10/21 1:21 PM, Axel Rasmussen wrote:
quoted
This feature allows userspace to intercept "minor" faults. By "minor"
faults, I mean the following situation:
Let there exist two mappings (i.e., VMAs) to the same page(s). One of
the mappings is registered with userfaultfd (in minor mode), and the
other is not. Via the non-UFFD mapping, the underlying pages have
already been allocated & filled with some contents. The UFFD mapping
has not yet been faulted in; when it is touched for the first time,
this results in what I'm calling a "minor" fault. As a concrete
example, when working with hugetlbfs, we have huge_pte_none(), but
find_lock_page() finds an existing page.
Do we want to intercept the fault if it is for a private mapping that
will COW the page in the page cache? I think 'yes' but just want to
confirm. The code added to hugetlb_no_page will intercept these COW
accesses.
I can at least say this is intentional, although I admit I don't have
a precise use case in mind for the UFFD mapping being private. I
suppose it's something like, the UFFD poll thread is supposed to
(maybe) update the page contents, *before* I CoW it, and then once
it's been CoW-ed I don't want that poll thread to be able to see
whatever changes I've made?
Unless there's some different use case for this, I believe this is the
behavior we want.
@@ -4366,6 +4366,38 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm,VM_FAULT_SET_HINDEX(hstate_index(h));gotobackout_unlocked;}++/* Check for page in userfault range. */+if(userfaultfd_minor(vma)){+u32hash;+structvm_faultvmf={+.vma=vma,+.address=haddr,+.flags=flags,+/*+*Hardtodebugifitendsupbeingusedbya+*calleethatassumessomethingaboutthe+*otheruninitializedfields...sameasin+*memory.c+*/+};++unlock_page(page);++/*+*hugetlb_fault_mutexandi_mmap_rwsemmustbedropped+*beforehandlinguserfault.Reacquireafterhandling+*faulttomakecallingcodesimpler.+*/++hash=hugetlb_fault_mutex_hash(mapping,idx);+mutex_unlock(&hugetlb_fault_mutex_table[hash]);+i_mmap_unlock_read(mapping);
After dropping all the locks, we only hold a reference to the page in the
page cache. I 'think' someone else could hole punch the page and remove it
from the cache. IIUC, state changing while processing uffd faults is something
that users need to deal with? Just need to make sure there are no assumptions
in the kernel code.
Yeah, this seems possible. What I'd expect to happen in that case is
something like:
1. hugetlb_no_page() calls into handle_userfault().
2. Someone hole punches the page, removing it from the page cache.
3. The UFFD poll thread gets the fault event, and issues a
UFFDIO_CONTINUE. (Say we instead were going to write an update, and
*then* UFFDIO_CONTINUE: I think the hole punch by another thread could
also happen between those two events.)
4. This calls down into hugetlb_mcopy_atomic_pte, where we try to
find_lock_page(). This returns NULL, so we bail with -EFAULT.
5. Userspace detects and deals with this error - maybe by writing to
the non-UFFD mapping, thereby putting a page back in the page cache,
or by issuing a UFFDIO_COPY or such?
Which, as far as I can see is fine? But, I am by no means an expert
yet so please correct me if this seems problematic. :)
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-10 21:24:47
As the comment says: for the MINOR fault use case, although the page
might be present and populated in the other (non-UFFD-registered) half
of the mapping, it may be out of date, and we explicitly want userspace
to get a minor fault so it can check and potentially update the page's
contents.
Huge PMD sharing would prevent these faults from occurring for
suitably aligned areas, so disable it upon UFFD registration.
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
include/linux/userfaultfd_k.h | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-10 21:24:54
Fix a dormant bug in userfaultfd_events_test(), where we did
`return faulting_process(0)` instead of `exit(faulting_process(0))`.
This caused the forked process to keep running, trying to execute any
further test cases after the events test in parallel with the "real"
process.
Add a simple test case which exercises minor faults. In short, it does
the following:
1. "Sets up" an area (area_dst) and a second shared mapping to the same
underlying pages (area_dst_alias).
2. Register one of these areas with userfaultfd, in minor fault mode.
3. Start a second thread to handle any minor faults.
4. Populate the underlying pages with the non-UFFD-registered side of
the mapping. Basically, memset() each page with some arbitrary
contents.
5. Then, using the UFFD-registered mapping, read all of the page
contents, asserting that the contents match expectations (we expect
the minor fault handling thread can modify the page contents before
resolving the fault).
The minor fault handling thread, upon receiving an event, flips all the
bits (~) in that page, just to prove that it can modify it in some
arbitrary way. Then it issues a UFFDIO_CONTINUE ioctl, to setup the
mapping and resolve the fault. The reading thread should wake up and see
this modification.
Currently the minor fault test is only enabled in hugetlb_shared mode,
as this is the only configuration the kernel feature supports.
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
tools/testing/selftests/vm/userfaultfd.c | 147 ++++++++++++++++++++++-
1 file changed, 143 insertions(+), 4 deletions(-)
@@ -81,6 +81,8 @@ static volatile bool test_uffdio_copy_eexist = true;staticvolatilebooltest_uffdio_zeropage_eexist=true;/* Whether to test uffd write-protection */staticbooltest_uffdio_wp=false;+/* Whether to test uffd minor faults */+staticbooltest_uffdio_minor=false;staticboolmap_shared;staticinthuge_fd;
@@ -1112,7 +1160,7 @@ static int userfaultfd_events_test(void)}if(!pid)-returnfaulting_process(0);+exit(faulting_process(0));waitpid(pid,&err,0);if(err){
@@ -1215,6 +1263,95 @@ static int userfaultfd_sig_test(void)returnuserfaults!=0;}+staticintuserfaultfd_minor_test(void)+{+structuffdio_registeruffdio_register;+unsignedlongexpected_ioctls;+unsignedlongp;+pthread_tuffd_mon;+uint8_texpected_byte;+void*expected_page;+charc;+structuffd_statsstats={0};++if(!test_uffdio_minor)+return0;++printf("testing minor faults: ");+fflush(stdout);++if(uffd_test_ops->release_pages(area_dst))+return1;++if(userfaultfd_open(0))+return1;++uffdio_register.range.start=(unsignedlong)area_dst_alias;+uffdio_register.range.len=nr_pages*page_size;+uffdio_register.mode=UFFDIO_REGISTER_MODE_MINOR;+if(ioctl(uffd,UFFDIO_REGISTER,&uffdio_register)){+fprintf(stderr,"register failure\n");+exit(1);+}++expected_ioctls=uffd_test_ops->expected_ioctls;+expected_ioctls|=1<<_UFFDIO_CONTINUE;+if((uffdio_register.ioctls&expected_ioctls)!=expected_ioctls){+fprintf(stderr,"unexpected missing ioctl(s)\n");+exit(1);+}++/*+*AfterregisteringwithUFFD,populatethenon-UFFD-registeredsideof+*thesharedmapping.Thisshould*not*triggeranyUFFDminorfaults.+*/+for(p=0;p<nr_pages;++p){+memset(area_dst+(p*page_size),p%((uint8_t)-1),+page_size);+}++if(pthread_create(&uffd_mon,&attr,uffd_poll_thread,&stats)){+perror("uffd_poll_thread create");+exit(1);+}++/*+*ReadeachofthepagesbackusingtheUFFD-registeredmapping.We+*expectthatthefirsttimewetouchapage,itwillresultinaminor+*fault.uffd_poll_threadwillresolvethefaultbybit-flippingthe+*page'scontents,andthenissuingaCONTINUEioctl.+*/++if(posix_memalign(&expected_page,page_size,page_size)){+fprintf(stderr,"out of memory\n");+return1;+}++for(p=0;p<nr_pages;++p){+expected_byte=~((uint8_t)(p%((uint8_t)-1)));+memset(expected_page,expected_byte,page_size);+if(my_bcmp(expected_page,area_dst_alias+(p*page_size),+page_size)){+fprintf(stderr,+"unexpected page contents after minor fault\n");+exit(1);+}+}++if(write(pipefd[1],&c,sizeof(c))!=sizeof(c)){+perror("pipe write");+exit(1);+}+if(pthread_join(uffd_mon,NULL))+return1;++close(uffd);++uffd_stats_report(&stats,1);++returnstats.minor_faults!=nr_pages;+}+staticintuserfaultfd_stress(void){void*area;
@@ -1413,7 +1550,7 @@ static int userfaultfd_stress(void)close(uffd);returnuserfaultfd_zeropage_test()||userfaultfd_sig_test()-||userfaultfd_events_test();+||userfaultfd_events_test()||userfaultfd_minor_test();}/*
@@ -1454,6 +1591,8 @@ static void set_test_type(const char *type)map_shared=true;test_type=TEST_HUGETLB;uffd_test_ops=&hugetlb_uffd_test_ops;+/* Minor faults require shared hugetlb; only enable here. */+test_uffdio_minor=true;}elseif(!strcmp(type,"shmem")){map_shared=true;test_type=TEST_SHMEM;
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-10 21:24:57
Reword / reorganize things a little bit into "lists", so new features /
modes / ioctls can sort of just be appended.
Describe how UFFDIO_REGISTER_MODE_MINOR and UFFDIO_CONTINUE can be used
to intercept and resolve minor faults. Make it clear that COPY and
ZEROPAGE are used for MISSING faults, whereas CONTINUE is used for MINOR
faults.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
Documentation/admin-guide/mm/userfaultfd.rst | 107 ++++++++++++-------
1 file changed, 66 insertions(+), 41 deletions(-)
@@ -63,36 +63,36 @@ the generic ioctl available. The ``uffdio_api.features`` bitmask returned by the ``UFFDIO_API`` ioctl defines what memory types are supported by the ``userfaultfd`` and what-events, except page fault notifications, may be generated.--If the kernel supports registering ``userfaultfd`` ranges on hugetlbfs-virtual memory areas, ``UFFD_FEATURE_MISSING_HUGETLBFS`` will be set in-``uffdio_api.features``. Similarly, ``UFFD_FEATURE_MISSING_SHMEM`` will be-set if the kernel supports registering ``userfaultfd`` ranges on shared-memory (covering all shmem APIs, i.e. tmpfs, ``IPCSHM``, ``/dev/zero``,-``MAP_SHARED``, ``memfd_create``, etc).--The userland application that wants to use ``userfaultfd`` with hugetlbfs-or shared memory need to set the corresponding flag in-``uffdio_api.features`` to enable those features.--If the userland desires to receive notifications for events other than-page faults, it has to verify that ``uffdio_api.features`` has appropriate-``UFFD_FEATURE_EVENT_*`` bits set. These events are described in more-detail below in `Non-cooperative userfaultfd`_ section.--Once the ``userfaultfd`` has been enabled the ``UFFDIO_REGISTER`` ioctl should-be invoked (if present in the returned ``uffdio_api.ioctls`` bitmask) to-register a memory range in the ``userfaultfd`` by setting the+events, except page fault notifications, may be generated:++- The ``UFFD_FEATURE_EVENT_*`` flags indicate that various other events+ other than page faults are supported. These events are described in more+ detail below in the `Non-cooperative userfaultfd`_ section.++-``UFFD_FEATURE_MISSING_HUGETLBFS`` and ``UFFD_FEATURE_MISSING_SHMEM``+ indicate that the kernel supports ``UFFDIO_REGISTER_MODE_MISSING``+ registrations for hugetlbfs and shared memory (covering all shmem APIs,+ i.e. tmpfs, ``IPCSHM``, ``/dev/zero``, ``MAP_SHARED``, ``memfd_create``,+ etc) virtual memory areas, respectively.++-``UFFD_FEATURE_MINOR_HUGETLBFS`` indicates that the kernel supports+``UFFDIO_REGISTER_MODE_MINOR`` registration for hugetlbfs virtual memory+ areas.++The userland application should set the feature flags it intends to use+when invoking the ``UFFDIO_API`` ioctl, to request that those features be+enabled if supported.++Once the ``userfaultfd`` API has been enabled the ``UFFDIO_REGISTER``+ioctl should be invoked (if present in the returned ``uffdio_api.ioctls``+bitmask) to register a memory range in the ``userfaultfd`` by setting the uffdio_register structure accordingly. The ``uffdio_register.mode`` bitmask will specify to the kernel which kind of faults to track for-the range (``UFFDIO_REGISTER_MODE_MISSING`` would track missing-pages). The ``UFFDIO_REGISTER`` ioctl will return the+the range. The ``UFFDIO_REGISTER`` ioctl will return the``uffdio_register.ioctls`` bitmask of ioctls that are suitable to resolve userfaults on the range registered. Not all ioctls will necessarily be-supported for all memory types depending on the underlying virtual-memory backend (anonymous memory vs tmpfs vs real filebacked-mappings).+supported for all memory types (e.g. anonymous memory vs. shmem vs.+hugetlbfs), or all types of intercepted faults. Userland can use the ``uffdio_register.ioctls`` to manage the virtual address space in the background (to add or potentially also remove
@@ -100,21 +100,46 @@ memory from the ``userfaultfd`` registered range). This means a userfault could be triggering just before userland maps in the background the user-faulted page.-The primary ioctl to resolve userfaults is ``UFFDIO_COPY``. That-atomically copies a page into the userfault registered range and wakes-up the blocked userfaults-(unless ``uffdio_copy.mode & UFFDIO_COPY_MODE_DONTWAKE`` is set).-Other ioctl works similarly to ``UFFDIO_COPY``. They're atomic as in-guaranteeing that nothing can see an half copied page since it'll-keep userfaulting until the copy has finished.+Resolving Userfaults+--------------------++There are three basic ways to resolve userfaults:++-``UFFDIO_COPY`` atomically copies some existing page contents from+ userspace.++-``UFFDIO_ZEROPAGE`` atomically zeros the new page.++-``UFFDIO_CONTINUE`` maps an existing, previously-populated page.++These operations are atomic in the sense that they guarantee nothing can+see a half-populated page, since readers will keep userfaulting until the+operation has finished.++By default, these wake up userfaults blocked on the range in question.+They support a ``UFFDIO_*_MODE_DONTWAKE````mode`` flag, which indicates+that waking will be done separately at some later time.++Which ioctl to choose depends on the kind of page fault, and what we'd+like to do to resolve it:++- For ``UFFDIO_REGISTER_MODE_MISSING`` faults, the fault needs to be+ resolved by either providing a new page (``UFFDIO_COPY``), or mapping+ the zero page (``UFFDIO_ZEROPAGE``). By default, the kernel would map+ the zero page for a missing fault. With userfaultfd, userspace can+ decide what content to provide before the faulting thread continues.++- For ``UFFDIO_REGISTER_MODE_MINOR`` faults, there is an existing page (in+ the page cache). Userspace has the option of modifying the page's+ contents before resolving the fault. Once the contents are correct+ (modified or not), userspace asks the kernel to map the page and let the+ faulting thread continue with ``UFFDIO_CONTINUE``. Notes:-- If you requested ``UFFDIO_REGISTER_MODE_MISSING`` when registering then- you must provide some kind of page in your thread after reading from- the uffd. You must provide either ``UFFDIO_COPY`` or ``UFFDIO_ZEROPAGE``.- The normal behavior of the OS automatically providing a zero page on- an anonymous mmaping is not in place.+- You can tell which kind of fault occurred by examining+``pagefault.flags`` within the ``uffd_msg``, checking for the+``UFFD_PAGEFAULT_FLAG_*`` flags.- None of the page-delivering ioctls default to the range that you registered with. You must fill in all fields for the appropriate
@@ -122,9 +147,9 @@ Notes:- You get the address of the access that triggered the missing page event out of a struct uffd_msg that you read in the thread from the- uffd. You can supply as many pages as you want with ``UFFDIO_COPY`` or-``UFFDIO_ZEROPAGE``. Keep in mind that unless you used DONTWAKE then- the first of any of those IOCTLs wakes up the faulting thread.+ uffd. You can supply as many pages as you want with these IOCTLs.+ Keep in mind that unless you used DONTWAKE then the first of any of+ those IOCTLs wakes up the faulting thread.- Be sure to test for all errors including (``pollfd[0].revents & POLLERR``). This can happen, e.g. when ranges
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-10 21:25:32
For background, mm/userfaultfd.c provides a general mcopy_atomic
implementation. But some types of memory (i.e., hugetlb and shmem) need
a slightly different implementation, so they provide their own helpers
for this. In other words, userfaultfd is the only caller of these
functions.
This patch achieves two things:
1. Don't spend time compiling code which will end up never being
referenced anyway (a small build time optimization).
2. In patches later in this series, we extend the signature of these
helpers with UFFD-specific state (a mode enumeration). Once this
happens, we *have to* either not compile the helpers, or unconditionally
define the UFFD-only state (which seems messier to me). This includes
the declarations in the headers, as otherwise they'd yield warnings
about implicitly defining the type of those arguments.
Reviewed-by: Mike Kravetz <redacted>
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
include/linux/hugetlb.h | 4 ++++
mm/hugetlb.c | 2 ++
2 files changed, 6 insertions(+)
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-10 21:25:34
This ioctl is how userspace ought to resolve "minor" userfaults. The
idea is, userspace is notified that a minor fault has occurred. It might
change the contents of the page using its second non-UFFD mapping, or
not. Then, it calls UFFDIO_CONTINUE to tell the kernel "I have ensured
the page contents are correct, carry on setting up the mapping".
Note that it doesn't make much sense to use UFFDIO_{COPY,ZEROPAGE} for
MINOR registered VMAs. ZEROPAGE maps the VMA to the zero page; but in
the minor fault case, we already have some pre-existing underlying page.
Likewise, UFFDIO_COPY isn't useful if we have a second non-UFFD mapping.
We'd just use memcpy() or similar instead.
It turns out hugetlb_mcopy_atomic_pte() already does very close to what
we want, if an existing page is provided via `struct page **pagep`. We
already special-case the behavior a bit for the UFFDIO_ZEROPAGE case, so
just extend that design: add an enum for the three modes of operation,
and make the small adjustments needed for the MCOPY_ATOMIC_CONTINUE
case. (Basically, look up the existing page, and avoid adding the
existing page to the page cache or calling set_page_huge_active() on
it.)
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
fs/userfaultfd.c | 67 ++++++++++++++++++++++++++++++++
include/linux/hugetlb.h | 3 ++
include/linux/userfaultfd_k.h | 18 +++++++++
include/uapi/linux/userfaultfd.h | 21 +++++++++-
mm/hugetlb.c | 39 ++++++++++++-------
mm/userfaultfd.c | 37 +++++++++++-------
6 files changed, 156 insertions(+), 29 deletions(-)
@@ -1527,6 +1527,10 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,if(!(uffdio_register.mode&UFFDIO_REGISTER_MODE_WP))ioctls_out&=~((__u64)1<<_UFFDIO_WRITEPROTECT);+/* CONTINUE ioctl is only supported for MINOR ranges. */+if(!(uffdio_register.mode&UFFDIO_REGISTER_MODE_MINOR))+ioctls_out&=~((__u64)1<<_UFFDIO_CONTINUE);+/**Nowthatwescannedallvmaswecanalreadytell*userlandwhichioctlsmethodsareguaranteedto
@@ -1880,6 +1884,66 @@ static int userfaultfd_writeprotect(struct userfaultfd_ctx *ctx,returnret;}+staticintuserfaultfd_continue(structuserfaultfd_ctx*ctx,unsignedlongarg)+{+__s64ret;+structuffdio_continueuffdio_continue;+structuffdio_continue__user*user_uffdio_continue;+structuserfaultfd_wake_rangerange;++user_uffdio_continue=(structuffdio_continue__user*)arg;++ret=-EAGAIN;+if(READ_ONCE(ctx->mmap_changing))+gotoout;++ret=-EFAULT;+if(copy_from_user(&uffdio_continue,user_uffdio_continue,+/* don't copy the output fields */+sizeof(uffdio_continue)-(sizeof(__s64))))+gotoout;++ret=validate_range(ctx->mm,&uffdio_continue.range.start,+uffdio_continue.range.len);+if(ret)+gotoout;++ret=-EINVAL;+/* double check for wraparound just in case. */+if(uffdio_continue.range.start+uffdio_continue.range.len<=+uffdio_continue.range.start){+gotoout;+}+if(uffdio_continue.mode&~UFFDIO_CONTINUE_MODE_DONTWAKE)+gotoout;++if(mmget_not_zero(ctx->mm)){+ret=mcopy_continue(ctx->mm,uffdio_continue.range.start,+uffdio_continue.range.len,+&ctx->mmap_changing);+mmput(ctx->mm);+}else{+return-ESRCH;+}++if(unlikely(put_user(ret,&user_uffdio_continue->mapped)))+return-EFAULT;+if(ret<0)+gotoout;++/* len == 0 would wake all */+BUG_ON(!ret);+range.len=ret;+if(!(uffdio_continue.mode&UFFDIO_CONTINUE_MODE_DONTWAKE)){+range.start=uffdio_continue.range.start;+wake_userfault(ctx,&range);+}+ret=range.len==uffdio_continue.range.len?0:-EAGAIN;++out:+returnret;+}+staticinlineunsignedintuffd_ctx_features(__u64user_features){/*
@@ -37,6 +37,22 @@ extern int sysctl_unprivileged_userfaultfd;externvm_fault_thandle_userfault(structvm_fault*vmf,unsignedlongreason);+/*+*Themodeofoperationfor__mcopy_atomicanditshelpers.+*+*Thisisalmostanimplementationdetail(mcopy_atomicbelowdoesn'ttakethis+*asaparameter),butit'sexposedherebecausememory-kind-specific+*implementations(e.g.hugetlbfs)needtoknowthemodeofoperation.+*/+enummcopy_atomic_mode{+/* A normal copy_from_user into the destination range. */+MCOPY_ATOMIC_NORMAL,+/* Don't copy; map the destination range to the zero page. */+MCOPY_ATOMIC_ZEROPAGE,+/* Just install pte(s) with the existing page(s) in the page cache. */+MCOPY_ATOMIC_CONTINUE,+};+externssize_tmcopy_atomic(structmm_struct*dst_mm,unsignedlongdst_start,unsignedlongsrc_start,unsignedlonglen,bool*mmap_changing,__u64mode);
@@ -4648,8 +4647,10 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,structvm_area_struct*dst_vma,unsignedlongdst_addr,unsignedlongsrc_addr,+enummcopy_atomic_modemode,structpage**pagep){+boolis_continue=(mode==MCOPY_ATOMIC_CONTINUE);structaddress_space*mapping;pgoff_tidx;unsignedlongsize;
@@ -4659,8 +4660,18 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,spinlock_t*ptl;intret;structpage*page;+intwritable;-if(!*pagep){+mapping=dst_vma->vm_file->f_mapping;+idx=vma_hugecache_offset(h,dst_vma,dst_addr);++if(is_continue){+ret=-EFAULT;+page=find_lock_page(mapping,idx);+*pagep=NULL;+if(!page)+gotoout;+}elseif(!*pagep){ret=-ENOMEM;page=alloc_huge_page(dst_vma,dst_addr,0);if(IS_ERR(page))
@@ -4689,13 +4700,8 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,*/__SetPageUptodate(page);-mapping=dst_vma->vm_file->f_mapping;-idx=vma_hugecache_offset(h,dst_vma,dst_addr);--/*-*Ifshared,addtopagecache-*/-if(vm_shared){+/* Add shared, newly allocated pages to the page cache. */+if(vm_shared&&!is_continue){size=i_size_read(mapping->host)>>huge_page_shift(h);ret=-EFAULT;if(idx>=size)
@@ -4740,8 +4746,14 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,hugepage_add_new_anon_rmap(page,dst_vma,dst_addr);}-_dst_pte=make_huge_pte(dst_vma,page,dst_vma->vm_flags&VM_WRITE);-if(dst_vma->vm_flags&VM_WRITE)+/* For CONTINUE on a non-shared VMA, don't set VM_WRITE for CoW. */+if(is_continue&&!vm_shared)+writable=0;+else+writable=dst_vma->vm_flags&VM_WRITE;++_dst_pte=make_huge_pte(dst_vma,page,writable);+if(writable)_dst_pte=huge_pte_mkdirty(_dst_pte);_dst_pte=pte_mkyoung(_dst_pte);
@@ -4755,8 +4767,9 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,update_mmu_cache(dst_vma,dst_addr,dst_pte);spin_unlock(ptl);-SetHPageMigratable(page);-if(vm_shared)+if(!is_continue)+SetHPageMigratable(page);+if(vm_shared||is_continue)unlock_page(page);ret=0;out:
From: Axel Rasmussen <axelrasmussen@google.com> Date: 2021-02-10 22:23:04
"
On Wed, Feb 10, 2021 at 1:22 PM Axel Rasmussen [off-list ref] wrote:
quoted hunk
This ioctl is how userspace ought to resolve "minor" userfaults. The
idea is, userspace is notified that a minor fault has occurred. It might
change the contents of the page using its second non-UFFD mapping, or
not. Then, it calls UFFDIO_CONTINUE to tell the kernel "I have ensured
the page contents are correct, carry on setting up the mapping".
Note that it doesn't make much sense to use UFFDIO_{COPY,ZEROPAGE} for
MINOR registered VMAs. ZEROPAGE maps the VMA to the zero page; but in
the minor fault case, we already have some pre-existing underlying page.
Likewise, UFFDIO_COPY isn't useful if we have a second non-UFFD mapping.
We'd just use memcpy() or similar instead.
It turns out hugetlb_mcopy_atomic_pte() already does very close to what
we want, if an existing page is provided via `struct page **pagep`. We
already special-case the behavior a bit for the UFFDIO_ZEROPAGE case, so
just extend that design: add an enum for the three modes of operation,
and make the small adjustments needed for the MCOPY_ATOMIC_CONTINUE
case. (Basically, look up the existing page, and avoid adding the
existing page to the page cache or calling set_page_huge_active() on
it.)
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
fs/userfaultfd.c | 67 ++++++++++++++++++++++++++++++++
include/linux/hugetlb.h | 3 ++
include/linux/userfaultfd_k.h | 18 +++++++++
include/uapi/linux/userfaultfd.h | 21 +++++++++-
mm/hugetlb.c | 39 ++++++++++++-------
mm/userfaultfd.c | 37 +++++++++++-------
6 files changed, 156 insertions(+), 29 deletions(-)
@@ -1527,6 +1527,10 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,if(!(uffdio_register.mode&UFFDIO_REGISTER_MODE_WP))ioctls_out&=~((__u64)1<<_UFFDIO_WRITEPROTECT);+/* CONTINUE ioctl is only supported for MINOR ranges. */+if(!(uffdio_register.mode&UFFDIO_REGISTER_MODE_MINOR))+ioctls_out&=~((__u64)1<<_UFFDIO_CONTINUE);+/**Nowthatwescannedallvmaswecanalreadytell*userlandwhichioctlsmethodsareguaranteedto
@@ -1880,6 +1884,66 @@ static int userfaultfd_writeprotect(struct userfaultfd_ctx *ctx,returnret;}+staticintuserfaultfd_continue(structuserfaultfd_ctx*ctx,unsignedlongarg)+{+__s64ret;+structuffdio_continueuffdio_continue;+structuffdio_continue__user*user_uffdio_continue;+structuserfaultfd_wake_rangerange;++user_uffdio_continue=(structuffdio_continue__user*)arg;++ret=-EAGAIN;+if(READ_ONCE(ctx->mmap_changing))+gotoout;++ret=-EFAULT;+if(copy_from_user(&uffdio_continue,user_uffdio_continue,+/* don't copy the output fields */+sizeof(uffdio_continue)-(sizeof(__s64))))+gotoout;++ret=validate_range(ctx->mm,&uffdio_continue.range.start,+uffdio_continue.range.len);+if(ret)+gotoout;++ret=-EINVAL;+/* double check for wraparound just in case. */+if(uffdio_continue.range.start+uffdio_continue.range.len<=+uffdio_continue.range.start){+gotoout;+}+if(uffdio_continue.mode&~UFFDIO_CONTINUE_MODE_DONTWAKE)+gotoout;++if(mmget_not_zero(ctx->mm)){+ret=mcopy_continue(ctx->mm,uffdio_continue.range.start,+uffdio_continue.range.len,+&ctx->mmap_changing);+mmput(ctx->mm);+}else{+return-ESRCH;+}++if(unlikely(put_user(ret,&user_uffdio_continue->mapped)))+return-EFAULT;+if(ret<0)+gotoout;++/* len == 0 would wake all */+BUG_ON(!ret);+range.len=ret;+if(!(uffdio_continue.mode&UFFDIO_CONTINUE_MODE_DONTWAKE)){+range.start=uffdio_continue.range.start;+wake_userfault(ctx,&range);+}+ret=range.len==uffdio_continue.range.len?0:-EAGAIN;++out:+returnret;+}+staticinlineunsignedintuffd_ctx_features(__u64user_features){/*
@@ -37,6 +37,22 @@ extern int sysctl_unprivileged_userfaultfd;externvm_fault_thandle_userfault(structvm_fault*vmf,unsignedlongreason);+/*+*Themodeofoperationfor__mcopy_atomicanditshelpers.+*+*Thisisalmostanimplementationdetail(mcopy_atomicbelowdoesn'ttakethis+*asaparameter),butit'sexposedherebecausememory-kind-specific+*implementations(e.g.hugetlbfs)needtoknowthemodeofoperation.+*/+enummcopy_atomic_mode{+/* A normal copy_from_user into the destination range. */+MCOPY_ATOMIC_NORMAL,+/* Don't copy; map the destination range to the zero page. */+MCOPY_ATOMIC_ZEROPAGE,+/* Just install pte(s) with the existing page(s) in the page cache. */+MCOPY_ATOMIC_CONTINUE,+};+externssize_tmcopy_atomic(structmm_struct*dst_mm,unsignedlongdst_start,unsignedlongsrc_start,unsignedlonglen,bool*mmap_changing,__u64mode);
@@ -4648,8 +4647,10 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,structvm_area_struct*dst_vma,unsignedlongdst_addr,unsignedlongsrc_addr,+enummcopy_atomic_modemode,structpage**pagep){+boolis_continue=(mode==MCOPY_ATOMIC_CONTINUE);structaddress_space*mapping;pgoff_tidx;unsignedlongsize;
@@ -4659,8 +4660,18 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,spinlock_t*ptl;intret;structpage*page;+intwritable;-if(!*pagep){+mapping=dst_vma->vm_file->f_mapping;+idx=vma_hugecache_offset(h,dst_vma,dst_addr);++if(is_continue){+ret=-EFAULT;+page=find_lock_page(mapping,idx);+*pagep=NULL;+if(!page)+gotoout;+}elseif(!*pagep){ret=-ENOMEM;page=alloc_huge_page(dst_vma,dst_addr,0);if(IS_ERR(page))
@@ -4689,13 +4700,8 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,*/__SetPageUptodate(page);-mapping=dst_vma->vm_file->f_mapping;-idx=vma_hugecache_offset(h,dst_vma,dst_addr);--/*-*Ifshared,addtopagecache-*/-if(vm_shared){+/* Add shared, newly allocated pages to the page cache. */+if(vm_shared&&!is_continue){size=i_size_read(mapping->host)>>huge_page_shift(h);ret=-EFAULT;if(idx>=size)
@@ -4740,8 +4746,14 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,hugepage_add_new_anon_rmap(page,dst_vma,dst_addr);}-_dst_pte=make_huge_pte(dst_vma,page,dst_vma->vm_flags&VM_WRITE);-if(dst_vma->vm_flags&VM_WRITE)+/* For CONTINUE on a non-shared VMA, don't set VM_WRITE for CoW. */+if(is_continue&&!vm_shared)+writable=0;+else+writable=dst_vma->vm_flags&VM_WRITE;++_dst_pte=make_huge_pte(dst_vma,page,writable);+if(writable)_dst_pte=huge_pte_mkdirty(_dst_pte);_dst_pte=pte_mkyoung(_dst_pte);
@@ -4755,8 +4767,9 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,update_mmu_cache(dst_vma,dst_addr,dst_pte);spin_unlock(ptl);-SetHPageMigratable(page);-if(vm_shared)+if(!is_continue)+SetHPageMigratable(page);+if(vm_shared||is_continue)unlock_page(page);ret=0;out:
Apologies, I noticed a mistake here. Just like the
"unlock_page(page);" line above, I think we need to change to "if
(vm_shared || is_continue)" in the "out_release_unlock:" case as well.
I'll send this change in a v5.1 or a v6, depending on what other
comments this series gets in the next few days.