linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Mike Rapoport <rppt@kernel.org>
To: Sean Christopherson <seanjc@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Andrea Arcangeli <aarcange@redhat.com>,
	Andrei Vagin <avagin@google.com>,
	Axel Rasmussen <axelrasmussen@google.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	David Hildenbrand <david@kernel.org>,
	Harry Yoo <harry.yoo@oracle.com>, Hugh Dickins <hughd@google.com>,
	James Houghton <jthoughton@google.com>,
	"Liam R. Howlett" <Liam.Howlett@oracle.com>,
	"Lorenzo Stoakes (Oracle)" <ljs@kernel.org>,
	"Matthew Wilcox (Oracle)" <willy@infradead.org>,
	Michal Hocko <mhocko@suse.com>,
	Muchun Song <muchun.song@linux.dev>,
	Nikita Kalyazin <kalyazin@amazon.com>,
	Oscar Salvador <osalvador@suse.de>,
	Paolo Bonzini <pbonzini@redhat.com>, Peter Xu <peterx@redhat.com>,
	Shuah Khan <shuah@kernel.org>,
	Suren Baghdasaryan <surenb@google.com>,
	Vlastimil Babka <vbabka@suse.cz>,
	kvm@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-mm@kvack.org
Subject: Re: [PATCH v4 13/15] KVM: guest_memfd: implement userfaultfd operations
Date: Thu, 9 Apr 2026 14:50:09 +0300	[thread overview]
Message-ID: <adeScX-HVYeYufCm@kernel.org> (raw)
In-Reply-To: <ac7oE_1j6gxf8OKT@google.com>

Hi Sean,

On Thu, Apr 02, 2026 at 03:05:07PM -0700, Sean Christopherson wrote:
> On Thu, Apr 02, 2026, Mike Rapoport wrote:
>
> > +#ifdef CONFIG_USERFAULTFD
> > +static bool kvm_gmem_can_userfault(struct vm_area_struct *vma, vm_flags_t vm_flags)
> > +{
> > +	struct inode *inode = file_inode(vma->vm_file);
> > +
> > +	/*
> > +	 * Only support userfaultfd for guest_memfd with INIT_SHARED flag.
> > +	 * This ensures the memory can be mapped to userspace.
> > +	 */
> > +	if (!(GMEM_I(inode)->flags & GUEST_MEMFD_FLAG_INIT_SHARED))
> > +		return false;
> 
> I'm not comfortable with this change.  It works for now, but it's going to be
> wildly wrong when in-place conversion comes along.  While I agree with the "Let's
> solve each problem in it's time :)"[*], the time for in-place conversion is now.
> In-place conversion isn't landing this cycle or next, but it's been in development
> for longer than UFFD support, and I'm not willing to punt solvable problems to
> that series, because it's plenty fat as is.

I'm not against solving it as a part of uffd support, but since we are very
close to the merge window, for now I asked Andrew to drop drop guest_memfd
patches from the set and only move forward with the refactoring of uffd and
shmem that has value on its own.
 
> Happily, IIUC, this is an easy problem to solve, and will have a nice side effect
> for the common UFFD code.
> 
> My objection to an early, global "can_userfault()" check is that it's guaranteed
> to cause TOCTOU issues.  E.g. for VM_UFFD_MISSING and VM_UFFD_MINOR, the check on
> whether or not a given address can be faulted in needs to happen in __do_userfault(),
> not broadly when VM_UFFD_MINOR is added to a VMA.  Conceptually, that also better
> aligns the code with the "normal" user fault path in kvm_gmem_fault_user_mapping().
>
> diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h
> index 6f33307c2780..8a2d0625ffa3 100644
> --- a/include/linux/userfaultfd_k.h
> +++ b/include/linux/userfaultfd_k.h
> @@ -82,8 +82,8 @@ extern vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason);
>  
>  /* VMA userfaultfd operations */
>  struct vm_uffd_ops {
> -       /* Checks if a VMA can support userfaultfd */
> -       bool (*can_userfault)(struct vm_area_struct *vma, vm_flags_t vm_flags);
> +       /* What UFFD flags/modes are supported. */
> +       const vm_flags_t supported_uffd_flags;

VMA maintainers really didn't like a fields flag in vm_uffd_ops when it was
proposed earlier, but an indirect call may work.

>         /*
>          * Called to resolve UFFDIO_CONTINUE request.
>          * Should return the folio found at pgoff in the VMA's pagecache if it
> 
> with usage like:
> 
> static const struct vm_uffd_ops shmem_uffd_ops = {
> 	.supported_uffd_flags	= __VM_UFFD_FLAGS,
> 	.get_folio_noalloc	= shmem_get_folio_noalloc,
> 	.alloc_folio		= shmem_mfill_folio_alloc,
> 	.filemap_add		= shmem_mfill_filemap_add,
> 	.filemap_remove		= shmem_mfill_filemap_remove,
> };
> 
> All in all, somelike like so (completely untested):
> 
> ---
>  include/linux/userfaultfd_k.h |  4 +-
>  mm/filemap.c                  |  1 +
>  mm/hugetlb.c                  |  8 +---
>  mm/shmem.c                    |  7 +--
>  mm/userfaultfd.c              |  6 +--
>  virt/kvm/guest_memfd.c        | 80 ++++++++++++++++++++++++++++++++++-
>  6 files changed, 87 insertions(+), 19 deletions(-)

Let's revisit after -rc1 :)

-- 
Sincerely yours,
Mike.


  parent reply	other threads:[~2026-04-09 11:50 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-02  4:11 [PATCH v4 00/15] mm, kvm: allow uffd support in guest_memfd Mike Rapoport
2026-04-02  4:11 ` [PATCH v4 01/15] userfaultfd: introduce mfill_copy_folio_locked() helper Mike Rapoport
2026-04-02  4:11 ` [PATCH v4 02/15] userfaultfd: introduce struct mfill_state Mike Rapoport
2026-04-03 11:19   ` Harry Yoo (Oracle)
2026-04-02  4:11 ` [PATCH v4 03/15] userfaultfd: introduce mfill_establish_pmd() helper Mike Rapoport
2026-04-02  4:11 ` [PATCH v4 04/15] userfaultfd: introduce mfill_get_vma() and mfill_put_vma() Mike Rapoport
2026-04-03 12:54   ` Harry Yoo (Oracle)
2026-04-02  4:11 ` [PATCH v4 05/15] userfaultfd: retry copying with locks dropped in mfill_atomic_pte_copy() Mike Rapoport
2026-04-06  1:54   ` Harry Yoo (Oracle)
2026-04-02  4:11 ` [PATCH v4 06/15] userfaultfd: move vma_can_userfault out of line Mike Rapoport
2026-04-02  4:11 ` [PATCH v4 07/15] userfaultfd: introduce vm_uffd_ops Mike Rapoport
2026-04-02 19:32   ` Tal Zussman
2026-04-09 17:18     ` Mike Rapoport
2026-04-02  4:11 ` [PATCH v4 08/15] shmem, userfaultfd: use a VMA callback to handle UFFDIO_CONTINUE Mike Rapoport
2026-04-02  4:11 ` [PATCH v4 09/15] userfaultfd: introduce vm_uffd_ops->alloc_folio() Mike Rapoport
2026-04-02  4:11 ` [PATCH v4 10/15] shmem, userfaultfd: implement shmem uffd operations using vm_uffd_ops Mike Rapoport
2026-04-02  4:11 ` [PATCH v4 11/15] userfaultfd: mfill_atomic(): remove retry logic Mike Rapoport
2026-04-02 13:47   ` Mike Rapoport
2026-04-02 18:23     ` Andrew Morton
2026-04-02  4:11 ` [PATCH v4 12/15] mm: generalize handling of userfaults in __do_fault() Mike Rapoport
2026-04-02  4:11 ` [PATCH v4 13/15] KVM: guest_memfd: implement userfaultfd operations Mike Rapoport
2026-04-02 22:05   ` Sean Christopherson
2026-04-07 15:01     ` Nikita Kalyazin
2026-04-09 11:50     ` Mike Rapoport [this message]
2026-04-02  4:11 ` [PATCH v4 14/15] KVM: selftests: test userfaultfd minor for guest_memfd Mike Rapoport
2026-04-02  4:11 ` [PATCH v4 15/15] KVM: selftests: test userfaultfd missing " Mike Rapoport
2026-04-02  4:35 ` [PATCH v4 00/15] mm, kvm: allow uffd support in guest_memfd Andrew Morton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=adeScX-HVYeYufCm@kernel.org \
    --to=rppt@kernel.org \
    --cc=Liam.Howlett@oracle.com \
    --cc=aarcange@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=avagin@google.com \
    --cc=axelrasmussen@google.com \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=harry.yoo@oracle.com \
    --cc=hughd@google.com \
    --cc=jthoughton@google.com \
    --cc=kalyazin@amazon.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=osalvador@suse.de \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=seanjc@google.com \
    --cc=shuah@kernel.org \
    --cc=surenb@google.com \
    --cc=vbabka@suse.cz \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox