From: Lokesh Gidra <lokeshgidra@google.com>
To: Suren Baghdasaryan <surenb@google.com>
Cc: "Liam R. Howlett" <Liam.Howlett@oracle.com>,
akpm@linux-foundation.org, linux-fsdevel@vger.kernel.org,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
selinux@vger.kernel.org, kernel-team@android.com,
aarcange@redhat.com, peterx@redhat.com, david@redhat.com,
axelrasmussen@google.com, bgeffon@google.com,
willy@infradead.org, jannh@google.com, kaleshsingh@google.com,
ngeoffray@google.com, timmurray@google.com, rppt@kernel.org
Subject: Re: [PATCH v2 3/3] userfaultfd: use per-vma locks in userfaultfd operations
Date: Mon, 5 Feb 2024 13:54:43 -0800 [thread overview]
Message-ID: <CA+EESO7ri47BaecbesP8dZCjeAk60+=Fcdc8xc5mbeA4UrYmqQ@mail.gmail.com> (raw)
In-Reply-To: <CAJuCfpFB6Udm0pkTwJCOtvrn9+=g05oFgL-dUnEkEO0cGmyvOw@mail.gmail.com>
On Mon, Feb 5, 2024 at 1:47 PM Suren Baghdasaryan <surenb@google.com> wrote:
>
> On Wed, Jan 31, 2024 at 1:41 PM Liam R. Howlett <Liam.Howlett@oracle.com> wrote:
> >
> > * Lokesh Gidra <lokeshgidra@google.com> [240130 21:49]:
> > > On Mon, Jan 29, 2024 at 6:58 PM Liam R. Howlett <Liam.Howlett@oracle.com> wrote:
> > > >
> > > > * Lokesh Gidra <lokeshgidra@google.com> [240129 19:28]:
> > > > > On Mon, Jan 29, 2024 at 12:53 PM Suren Baghdasaryan <surenb@google.com> wrote:
> > > > > >
> > > >
> >
> > ...
> >
> > > >
> > > > > Your suggestion is definitely simpler and easier to follow, but due to
> > > > > the overflow situation that Suren pointed out, I would still need to
> > > > > keep the locking/boolean dance, no? IIUC, even if I were to return
> > > > > EAGAIN to the userspace, there is no guarantee that subsequent ioctls
> > > > > on the same vma will succeed due to the same overflow, until someone
> > > > > acquires and releases mmap_lock in write-mode.
> > > > > Also, sometimes it seems insufficient whether we managed to lock vma
> > > > > or not. For instance, lock_vma_under_rcu() checks if anon_vma (for
> > > > > anonymous vma) exists. If not then it bails out.
> > > > > So it seems to me that we have to provide some fall back in
> > > > > userfaultfd operations which executes with mmap_lock in read-mode.
> > > >
> > > > Fair enough, what if we didn't use the sequence number and just locked
> > > > the vma directly?
> > >
> > > Looks good to me, unless someone else has any objections.
> > > >
> > > > /* This will wait on the vma lock, so once we return it's locked */
> > > > void vma_aquire_read_lock(struct vm_area_struct *vma)
> > > > {
> > > > mmap_assert_locked(vma->vm_mm);
> > > > down_read(&vma->vm_lock->lock);
> > > > }
> > > >
> > > > struct vm_area_struct *lock_vma(struct mm_struct *mm,
> > > > unsigned long addr)) /* or some better name.. */
> > > > {
> > > > struct vm_area_struct *vma;
> > > >
> > > > vma = lock_vma_under_rcu(mm, addr);
> > > > if (vma)
> > > > return vma;
> > > >
> > > > mmap_read_lock(mm);
> > > > /* mm sequence cannot change, no mm writers anyways.
> > > > * find_mergeable_anon_vma is only a concern in the page fault
> > > > * path
> > > > * start/end won't change under the mmap_lock
> > > > * vma won't become detached as we have the mmap_lock in read
> > > > * We are now sure no writes will change the VMA
> > > > * So let's make sure no other context is isolating the vma
> > > > */
> > > > vma = lookup_vma(mm, addr);
> > > > if (vma)
> > > We can take care of anon_vma as well here right? I can take a bool
> > > parameter ('prepare_anon' or something) and then:
> > >
> > > if (vma) {
> > > if (prepare_anon && vma_is_anonymous(vma)) &&
> > > !anon_vma_prepare(vma)) {
> > > vma = ERR_PTR(-ENOMEM);
> > > goto out_unlock;
> > > }
> > > > vma_aquire_read_lock(vma);
> > > }
> > > out_unlock:
> > > > mmap_read_unlock(mm);
> > > > return vma;
> > > > }
> >
> > Do you need this? I didn't think this was happening in the code as
> > written? If you need it I would suggest making it happen always and
> > ditch the flag until a user needs this variant, but document what's
> > going on in here or even have a better name.
>
> I think yes, you do need this. I can see calls to anon_vma_prepare()
> under mmap_read_lock() protection in both mfill_atomic_hugetlb() and
> in mfill_atomic(). This means, just like in the pagefault path, we
> modify vma->anon_vma under mmap_read_lock protection which guarantees
> that adjacent VMAs won't change. This is important because
> __anon_vma_prepare() uses find_mergeable_anon_vma() that needs the
> neighboring VMAs to be stable. Per-VMA lock guarantees stability of
> the VMA we locked but not of its neighbors, therefore holding per-VMA
> lock while calling anon_vma_prepare() is not enough. The solution
> Lokesh suggests would call anon_vma_prepare() under mmap_read_lock and
> therefore would avoid the issue.
>
Thanks, Suren.
anon_vma_prepare() is also called in validate_move_areas() via move_pages().
>
> >
> > Thanks,
> > Liam
next prev parent reply other threads:[~2024-02-05 21:54 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-29 19:35 [PATCH v2 0/3] per-vma locks in userfaultfd Lokesh Gidra
2024-01-29 19:35 ` [PATCH v2 1/3] userfaultfd: move userfaultfd_ctx struct to header file Lokesh Gidra
2024-01-30 7:12 ` Mike Rapoport
2024-01-29 19:35 ` [PATCH v2 2/3] userfaultfd: protect mmap_changing with rw_sem in userfaulfd_ctx Lokesh Gidra
2024-01-29 21:00 ` Liam R. Howlett
2024-01-29 22:35 ` Lokesh Gidra
2024-01-30 3:46 ` Liam R. Howlett
2024-01-30 8:55 ` Mike Rapoport
2024-01-30 17:28 ` Liam R. Howlett
2024-01-31 2:24 ` Lokesh Gidra
2024-02-04 10:27 ` Mike Rapoport
2024-02-05 20:53 ` Lokesh Gidra
2024-02-07 15:27 ` Mike Rapoport
2024-02-07 20:24 ` Lokesh Gidra
2024-02-12 8:14 ` Mike Rapoport
2024-01-30 7:21 ` Mike Rapoport
2024-01-29 19:35 ` [PATCH v2 3/3] userfaultfd: use per-vma locks in userfaultfd operations Lokesh Gidra
2024-01-29 20:36 ` Liam R. Howlett
2024-01-29 20:52 ` Suren Baghdasaryan
2024-01-29 21:18 ` Liam R. Howlett
2024-01-30 0:28 ` Lokesh Gidra
2024-01-30 2:58 ` Liam R. Howlett
2024-01-31 2:49 ` Lokesh Gidra
2024-01-31 21:41 ` Liam R. Howlett
2024-02-05 21:46 ` Suren Baghdasaryan
2024-02-05 21:54 ` Lokesh Gidra [this message]
2024-02-05 22:00 ` Liam R. Howlett
2024-02-05 22:24 ` Lokesh Gidra
2024-02-06 14:35 ` Liam R. Howlett
2024-02-06 16:26 ` Lokesh Gidra
2024-02-06 17:07 ` Liam R. Howlett
2024-01-31 3:03 ` Suren Baghdasaryan
2024-01-31 21:43 ` Liam R. Howlett
2024-01-29 20:39 ` [PATCH v2 0/3] per-vma locks in userfaultfd Liam R. Howlett
2024-01-29 21:58 ` Lokesh Gidra
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='CA+EESO7ri47BaecbesP8dZCjeAk60+=Fcdc8xc5mbeA4UrYmqQ@mail.gmail.com' \
--to=lokeshgidra@google.com \
--cc=Liam.Howlett@oracle.com \
--cc=aarcange@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=axelrasmussen@google.com \
--cc=bgeffon@google.com \
--cc=david@redhat.com \
--cc=jannh@google.com \
--cc=kaleshsingh@google.com \
--cc=kernel-team@android.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ngeoffray@google.com \
--cc=peterx@redhat.com \
--cc=rppt@kernel.org \
--cc=selinux@vger.kernel.org \
--cc=surenb@google.com \
--cc=timmurray@google.com \
--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;
as well as URLs for NNTP newsgroup(s).