linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Suren Baghdasaryan <surenb@google.com>
Cc: "Liam R. Howlett" <Liam.Howlett@oracle.com>,
	Matthew Wilcox <willy@infradead.org>,
	 Andrii Nakryiko <andrii@kernel.org>,
	linux-fsdevel@vger.kernel.org, brauner@kernel.org,
	 viro@zeniv.linux.org.uk, akpm@linux-foundation.org,
	 linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
	gregkh@linuxfoundation.org,  linux-mm@kvack.org, rppt@kernel.org
Subject: Re: [PATCH v3 1/9] mm: add find_vma()-like API but RCU protected and taking VMA lock
Date: Thu, 6 Jun 2024 09:51:50 -0700	[thread overview]
Message-ID: <CAEf4Bzax2E1JS=MUm=sBJvcMb+CyWaPdxmr2mDuODs2cc3_mTg@mail.gmail.com> (raw)
In-Reply-To: <CAJuCfpER9qUSGbWBcHhT1=ssH41Xv8--XVA5BEPCM7uf=z_GLw@mail.gmail.com>

On Wed, Jun 5, 2024 at 4:22 PM Suren Baghdasaryan <surenb@google.com> wrote:
>
> On Wed, Jun 5, 2024 at 10:03 AM Liam R. Howlett <Liam.Howlett@oracle.com> wrote:
> >
> > * Andrii Nakryiko <andrii.nakryiko@gmail.com> [240605 12:27]:
> > > On Wed, Jun 5, 2024 at 9:24 AM Andrii Nakryiko
> > > <andrii.nakryiko@gmail.com> wrote:
> > > >
> > > > On Wed, Jun 5, 2024 at 9:13 AM Andrii Nakryiko
> > > > <andrii.nakryiko@gmail.com> wrote:
> > > > >
> > > > > On Wed, Jun 5, 2024 at 6:33 AM Liam R. Howlett <Liam.Howlett@oracle.com> wrote:
> > > > > >
> > > > > > * Matthew Wilcox <willy@infradead.org> [240604 20:57]:
> > > > > > > On Tue, Jun 04, 2024 at 05:24:46PM -0700, Andrii Nakryiko wrote:
> > > > > > > > +/*
> > > > > > > > + * find_and_lock_vma_rcu() - Find and lock the VMA for a given address, or the
> > > > > > > > + * next VMA. Search is done under RCU protection, without taking or assuming
> > > > > > > > + * mmap_lock. Returned VMA is guaranteed to be stable and not isolated.
> > > > > > >
> > > > > > > You know this is supposed to be the _short_ description, right?
> > > > > > > Three lines is way too long.  The full description goes between the
> > > > > > > arguments and the Return: line.
> > > > >
> > > > > Sure, I'll adjust.
> > > > >
> > > > > > >
> > > > > > > > + * @mm: The mm_struct to check
> > > > > > > > + * @addr: The address
> > > > > > > > + *
> > > > > > > > + * Returns: The VMA associated with addr, or the next VMA.
> > > > > > > > + * May return %NULL in the case of no VMA at addr or above.
> > > > > > > > + * If the VMA is being modified and can't be locked, -EBUSY is returned.
> > > > > > > > + */
> > > > > > > > +struct vm_area_struct *find_and_lock_vma_rcu(struct mm_struct *mm,
> > > > > > > > +                                        unsigned long address)
> > > > > > > > +{
> > > > > > > > +   MA_STATE(mas, &mm->mm_mt, address, address);
> > > > > > > > +   struct vm_area_struct *vma;
> > > > > > > > +   int err;
> > > > > > > > +
> > > > > > > > +   rcu_read_lock();
> > > > > > > > +retry:
> > > > > > > > +   vma = mas_find(&mas, ULONG_MAX);
> > > > > > > > +   if (!vma) {
> > > > > > > > +           err = 0; /* no VMA, return NULL */
> > > > > > > > +           goto inval;
> > > > > > > > +   }
> > > > > > > > +
> > > > > > > > +   if (!vma_start_read(vma)) {
> > > > > > > > +           err = -EBUSY;
> > > > > > > > +           goto inval;
> > > > > > > > +   }
> > > > > > > > +
> > > > > > > > +   /*
> > > > > > > > +    * Check since vm_start/vm_end might change before we lock the VMA.
> > > > > > > > +    * Note, unlike lock_vma_under_rcu() we are searching for VMA covering
> > > > > > > > +    * address or the next one, so we only make sure VMA wasn't updated to
> > > > > > > > +    * end before the address.
> > > > > > > > +    */
> > > > > > > > +   if (unlikely(vma->vm_end <= address)) {
> > > > > > > > +           err = -EBUSY;
> > > > > > > > +           goto inval_end_read;
> > > > > > > > +   }
> > > > > > > > +
> > > > > > > > +   /* Check if the VMA got isolated after we found it */
> > > > > > > > +   if (vma->detached) {
> > > > > > > > +           vma_end_read(vma);
> > > > > > > > +           count_vm_vma_lock_event(VMA_LOCK_MISS);
> > > > > > > > +           /* The area was replaced with another one */
> > > > > > >
> > > > > > > Surely you need to mas_reset() before you goto retry?
> > > > > >
> > > > > > Probably more than that.  We've found and may have adjusted the
> > > > > > index/last; we should reconfigure the maple state.  You should probably
> > > > > > use mas_set(), which will reset the maple state and set the index and
> > > > > > long to address.
> > > > >
> > > > > Yep, makes sense, thanks. As for the `unlikely(vma->vm_end <=
> > > > > address)` case, I presume we want to do the same, right? Basically, on
> > > > > each retry start from the `address` unconditionally, no matter what's
> > > > > the reason for retry.
> > > >
> > > > ah, never mind, we don't retry in that situation, I'll just put
> > > > `mas_set(&mas, address);` right before `goto retry;`. Unless we should
> > > > actually retry in the case when VMA got moved before the requested
> > > > address, not sure, let me know what you think. Presumably retrying
> > > > will allow us to get the correct VMA without the need to fall back to
> > > > mmap_lock?
> > >
> > > sorry, one more question as I look some more around this (unfamiliar
> > > to me) piece of code. I see that lock_vma_under_rcu counts
> > > VMA_LOCK_MISS on retry, but I see that there is actually a
> > > VMA_LOCK_RETRY stat as well. Any reason it's a MISS instead of RETRY?
> > > Should I use MISS as well, or actually count a RETRY?
> > >
> >
> > VMA_LOCK_MISS is used here because we missed the VMA due to a write
> > happening to move the vma (rather rare).  The VMA_LOCK missed the vma.
> >
> > VMA_LOCK_RETRY is used to indicate we need to retry under the mmap lock.
> > A retry is needed after the VMA_LOCK did not work under rcu locking.
>
> Originally lock_vma_under_rcu() was used only inside page fault path,
> so these counters helped us quantify how effective VMA locking is when
> handling page faults. With more users of that function these counters
> will be affected by other paths as well. I'm not sure but I think it
> makes sense to use them only inside page fault path, IOW we should
> probably move count_vm_vma_lock_event() calls outside of
> lock_vma_under_rcu() and add them only when handling page faults.

Alright, seems like I should then just drop count_vm_vma_lock_event()
from the API I'm adding.

>
> >
> > Thanks,
> > Liam


  reply	other threads:[~2024-06-06 16:52 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-05  0:24 [PATCH v3 0/9] ioctl()-based API to query VMAs from /proc/<pid>/maps Andrii Nakryiko
2024-06-05  0:24 ` [PATCH v3 1/9] mm: add find_vma()-like API but RCU protected and taking VMA lock Andrii Nakryiko
2024-06-05  0:57   ` Matthew Wilcox
2024-06-05 13:33     ` Liam R. Howlett
2024-06-05 16:13       ` Andrii Nakryiko
2024-06-05 16:24         ` Andrii Nakryiko
2024-06-05 16:27           ` Andrii Nakryiko
2024-06-05 17:03             ` Liam R. Howlett
2024-06-05 23:22               ` Suren Baghdasaryan
2024-06-06 16:51                 ` Andrii Nakryiko [this message]
2024-06-06 17:13                   ` Suren Baghdasaryan
2024-06-05  0:24 ` [PATCH v3 2/9] fs/procfs: extract logic for getting VMA name constituents Andrii Nakryiko
2024-06-05  0:24 ` [PATCH v3 3/9] fs/procfs: implement efficient VMA querying API for /proc/<pid>/maps Andrii Nakryiko
2024-06-07 22:31   ` Andrei Vagin
2024-06-10  8:17     ` Andrii Nakryiko
2024-06-12 17:48       ` Andrei Vagin
2024-06-05  0:24 ` [PATCH v3 4/9] fs/procfs: use per-VMA RCU-protected locking in PROCMAP_QUERY API Andrii Nakryiko
2024-06-05 23:15   ` Suren Baghdasaryan
2024-06-06 16:51     ` Andrii Nakryiko
2024-06-06 17:12       ` Suren Baghdasaryan
2024-06-06 18:03         ` Andrii Nakryiko
2024-06-06 17:15       ` Liam R. Howlett
2024-06-06 17:33         ` Suren Baghdasaryan
2024-06-06 18:07           ` Liam R. Howlett
2024-06-06 18:09         ` Andrii Nakryiko
2024-06-06 18:32           ` Liam R. Howlett
2024-06-05  0:24 ` [PATCH v3 5/9] fs/procfs: add build ID fetching to " Andrii Nakryiko
2024-06-05  0:24 ` [PATCH v3 6/9] docs/procfs: call out ioctl()-based PROCMAP_QUERY command existence Andrii Nakryiko
2024-06-05  0:24 ` [PATCH v3 7/9] tools: sync uapi/linux/fs.h header into tools subdir Andrii Nakryiko
2024-06-05  0:24 ` [PATCH v3 8/9] selftests/bpf: make use of PROCMAP_QUERY ioctl if available Andrii Nakryiko
2024-06-05  0:24 ` [PATCH v3 9/9] selftests/bpf: add simple benchmark tool for /proc/<pid>/maps APIs Andrii Nakryiko

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='CAEf4Bzax2E1JS=MUm=sBJvcMb+CyWaPdxmr2mDuODs2cc3_mTg@mail.gmail.com' \
    --to=andrii.nakryiko@gmail.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=rppt@kernel.org \
    --cc=surenb@google.com \
    --cc=viro@zeniv.linux.org.uk \
    --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