From: Matthew Wilcox <willy@infradead.org>
To: Suren Baghdasaryan <surenb@google.com>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>,
Peter Zijlstra <peterz@infradead.org>,
"Paul E. McKenney" <paulmck@kernel.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
mingo@kernel.org, andrii@kernel.org,
linux-kernel@vger.kernel.org, rostedt@goodmis.org,
oleg@redhat.com, jolsa@kernel.org, clm@meta.com,
bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH 00/10] perf/uprobe: Optimize uprobes
Date: Sat, 27 Jul 2024 04:45:53 +0100 [thread overview]
Message-ID: <ZqRtcZHWFfUf6dfi@casper.infradead.org> (raw)
In-Reply-To: <CAJuCfpELNoDrVyyNV+fuB7ju77pqyj0rD0gOkLVX+RHKTxXGCA@mail.gmail.com>
On Fri, Jul 26, 2024 at 06:29:44PM -0700, Suren Baghdasaryan wrote:
> On Fri, Jul 26, 2024 at 5:20 PM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
> >
> > On Mon, Jul 22, 2024 at 12:09 PM Suren Baghdasaryan <surenb@google.com> wrote:
> > >
> > > On Wed, Jul 10, 2024 at 2:40 AM Peter Zijlstra <peterz@infradead.org> wrote:
> > > >
> > > > On Wed, Jul 10, 2024 at 11:16:31AM +0200, Peter Zijlstra wrote:
> > > >
> > > > > If it were an actual sequence count, I could make it work, but sadly,
> > > > > not. Also, vma_end_write() seems to be missing :-( If anything it could
> > > > > be used to lockdep annotate the thing.
> > >
> > > Thanks Matthew for forwarding me this discussion!
> > >
> > > > >
> > > > > Mooo.. I need to stare more at this to see if perhaps it can be made to
> > > > > work, but so far, no joy :/
> > > >
> > > > See, this is what I want, except I can't close the race against VMA
> > > > modification because of that crazy locking scheme :/
> > >
> > > Happy to explain more about this crazy locking scheme. The catch is
> > > that we can write-lock a VMA only while holding mmap_lock for write
> > > and we unlock all write-locked VMAs together when we drop that
> > > mmap_lock:
> > >
> > > mmap_write_lock(mm);
> > > vma_start_write(vma1);
> > > vma_start_write(vma2);
> > > ...
> > > mmap_write_unlock(mm); -> vma_end_write_all(mm); // unlocks all locked vmas
> > >
> > > This is done because oftentimes we need to lock multiple VMAs when
> > > modifying the address space (vma merge/split) and unlocking them
> > > individually would be more expensive than unlocking them in bulk by
> > > incrementing mm->mm_lock_seq.
> > >
> > > >
> > > >
> > > > --- a/kernel/events/uprobes.c
> > > > +++ b/kernel/events/uprobes.c
> > > > @@ -2146,11 +2146,58 @@ static int is_trap_at_addr(struct mm_str
> > > > return is_trap_insn(&opcode);
> > > > }
> > > >
> > > > -static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
> > > > +#ifndef CONFIG_PER_VMA_LOCK
> > > > +static struct uprobe *__find_active_uprobe(unsigned long bp_vaddr)
> > > > +{
> > > > + return NULL;
> > > > +}
> > > > +#else
> > >
> > > IIUC your code below, you want to get vma->vm_file without locking the
> > > VMA. I think under RCU that would have been possible if vma->vm_file
> > > were RCU-safe, which it's not (we had discussions with Paul and
> > > Matthew about that in
> > > https://lore.kernel.org/all/CAJuCfpHW2=Zu+CHXL+5fjWxGk=CVix=C66ra+DmXgn6r3+fsXg@mail.gmail.com/).
> > > Otherwise you could store the value of vma->vm_lock_seq before
> > > comparing it with mm->mm_lock_seq, then do get_file(vma->file) and
> > > then compare your locally stored vm_lock_seq against vma->vm_lock_seq
> > > to see if VMA got locked for modification after we got the file. So,
> > > unless I miss some other race, I think the VMA locking sequence does
> > > not preclude you from implementing __find_active_uprobe() but
> > > accessing vma->vm_file would be unsafe without some kind of locking.
> >
> > Hey Suren!
> >
> > I've haven't yet dug properly into this, but from quick checking
> > around I think for the hot path (where this all matters), we really
> > only want to get vma's underlying inode. vm_file itself is just a
> > means to that end. If there is some clever way to do
> > vma->vm_file->f_inode under RCU and without mmap_read_lock, that would
> > be good enough, I think.
>
> Hi Andrii,
> Sorry, I'm not aware of any other way to get the inode from vma. Maybe
> Matthew with his FS background can find a way?
Hum. What if we added SLAB_TYPESAFE_BY_RCU to files_cachep? That way
we could do:
inode = NULL;
rcu_read_lock();
vma = find_vma(mm, address);
if (!vma)
goto unlock;
file = READ_ONCE(vma->vm_file);
if (!file)
goto unlock;
inode = file->f_inode;
if (file != READ_ONCE(vma->vm_file))
inode = NULL;
unlock:
rcu_read_unlock();
if (inode)
return inode;
mmap_read_lock();
vma = find_vma(mm, address);
...
I think this would be safe because 'vma' will not be reused while we
hold the read lock, and while 'file' might be reused, whatever f_inode
points to won't be used if vm_file is no longer what it once was.
On the other hand, it's quarter to midnight on Friday, and I have a
terrible virus that I'm struggling through, so not ideal circumstances
for me to be reasoning about RCU guarantees.
next prev parent reply other threads:[~2024-07-27 3:45 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20240708091241.544262971@infradead.org>
[not found] ` <20240709075651.122204f1358f9f78d1e64b62@kernel.org>
2024-07-09 0:25 ` [PATCH 00/10] perf/uprobe: Optimize uprobes Andrii Nakryiko
2024-07-09 9:01 ` Peter Zijlstra
2024-07-09 14:11 ` Paul E. McKenney
2024-07-09 14:29 ` Peter Zijlstra
2024-07-09 14:36 ` Paul E. McKenney
2024-07-09 15:31 ` Peter Zijlstra
2024-07-09 15:56 ` Paul E. McKenney
2024-07-09 16:10 ` Matthew Wilcox
2024-07-09 16:30 ` Matthew Wilcox
2024-07-09 16:57 ` Paul E. McKenney
2024-07-10 9:16 ` Peter Zijlstra
2024-07-10 9:40 ` Peter Zijlstra
2024-07-22 19:09 ` Suren Baghdasaryan
2024-07-27 0:20 ` Andrii Nakryiko
2024-07-27 1:29 ` Suren Baghdasaryan
2024-07-27 3:45 ` Matthew Wilcox [this message]
2024-07-30 3:18 ` Andrii Nakryiko
2024-07-30 13:10 ` Peter Zijlstra
2024-07-30 18:10 ` Suren Baghdasaryan
2024-08-03 5:47 ` Andrii Nakryiko
2024-08-03 8:53 ` Peter Zijlstra
2024-08-04 23:22 ` Andrii Nakryiko
2024-08-06 4:08 ` Andrii Nakryiko
2024-08-06 14:50 ` Suren Baghdasaryan
2024-08-06 17:40 ` Andrii Nakryiko
2024-08-06 17:44 ` Suren Baghdasaryan
2024-08-07 1:36 ` Suren Baghdasaryan
2024-08-07 5:13 ` Suren Baghdasaryan
2024-08-07 17:49 ` Andrii Nakryiko
2024-08-07 18:04 ` Suren Baghdasaryan
2024-08-07 18:30 ` Andrii Nakryiko
2024-08-07 18:33 ` Suren Baghdasaryan
2024-08-08 0:47 ` Andrii Nakryiko
2024-07-30 13:46 ` Peter Zijlstra
2024-07-30 18:16 ` Suren Baghdasaryan
2024-07-09 16:42 ` Andrii Nakryiko
2024-07-09 9:03 ` Peter Zijlstra
2024-07-09 10:01 ` Jiri Olsa
2024-07-09 10:16 ` Peter Zijlstra
2024-07-09 22:10 ` Masami Hiramatsu
2024-07-10 10:10 ` Peter Zijlstra
2024-07-10 14:56 ` Masami Hiramatsu
2024-07-10 18:40 ` Andrii Nakryiko
2024-07-11 8:51 ` Peter Zijlstra
2024-07-11 15:17 ` Masami Hiramatsu
2024-07-11 15:22 ` Peter Zijlstra
2024-07-11 17:47 ` Steven Rostedt
2024-07-11 23:59 ` Masami Hiramatsu
2024-07-10 0:55 ` Masami Hiramatsu
2024-07-09 21:47 ` Andrii Nakryiko
2024-07-10 10:12 ` Peter Zijlstra
2024-07-10 12:34 ` Peter Zijlstra
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=ZqRtcZHWFfUf6dfi@casper.infradead.org \
--to=willy@infradead.org \
--cc=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=clm@meta.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=mingo@kernel.org \
--cc=oleg@redhat.com \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=surenb@google.com \
/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