From: Greg Thelen <gthelen@google.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>, Hao Luo <haoluo@google.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Song Liu <songliubraving@fb.com>,
Namhyung Kim <namhyung@kernel.org>,
Blake Jones <blakejones@google.com>, bpf <bpf@vger.kernel.org>,
open list <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf-next] bpf: Cache the last valid build_id.
Date: Wed, 23 Feb 2022 15:16:06 -0800 [thread overview]
Message-ID: <xr9335k918eh.fsf@gthelen2.svl.corp.google.com> (raw)
In-Reply-To: <CAEf4BzbjxwEukaZfW9qCLwXeyS32WeNQ_8MvUqRd-JA7cZzuGw@mail.gmail.com>
Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
> On Wed, Feb 23, 2022 at 2:20 PM Hao Luo <haoluo@google.com> wrote:
>>
>> For binaries that are statically linked, consecutive stack frames are
>> likely to be in the same VMA and therefore have the same build id.
>> As an optimization for this case, we can cache the previous frame's
>> VMA, if the new frame has the same VMA as the previous one, reuse the
>> previous one's build id. We are holding the MM locks as reader across
>> the entire loop, so we don't need to worry about VMA going away.
>>
>> Tested through "stacktrace_build_id" and "stacktrace_build_id_nmi" in
>> test_progs.
>>
>> Suggested-by: Greg Thelen <gthelen@google.com>
>> Signed-off-by: Hao Luo <haoluo@google.com>
>> ---
>> kernel/bpf/stackmap.c | 11 ++++++++++-
>> 1 file changed, 10 insertions(+), 1 deletion(-)
>>
>> diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
>> index 22c8ae94e4c1..280b9198af27 100644
>> --- a/kernel/bpf/stackmap.c
>> +++ b/kernel/bpf/stackmap.c
>> @@ -132,7 +132,8 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
>> int i;
>> struct mmap_unlock_irq_work *work = NULL;
>> bool irq_work_busy = bpf_mmap_unlock_get_irq_work(&work);
>> - struct vm_area_struct *vma;
>> + struct vm_area_struct *vma, *prev_vma = NULL;
>> + const char *prev_build_id;
>>
>> /* If the irq_work is in use, fall back to report ips. Same
>> * fallback is used for kernel stack (!user) on a stackmap with
>> @@ -151,6 +152,11 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
>>
>> for (i = 0; i < trace_nr; i++) {
>> vma = find_vma(current->mm, ips[i]);
>
> as a further optimization, shouldn't we first check if ips[i] is
> within prev_vma and avoid rbtree walk altogether? Would this work:
>
> if (prev_vma && range_in_vma(prev_vma, ips[i])) {
> /* reuse build_id */
> }
> vma = find_vma(current->mm, ips[i]);
>
>
> ?
Yes, that's a nice addition. Good idea.
>> + if (vma && vma == prev_vma) {
>> + memcpy(id_offs[i].build_id, prev_build_id,
>> + BUILD_ID_SIZE_MAX);
>> + goto build_id_valid;
>> + }
>> if (!vma || build_id_parse(vma, id_offs[i].build_id, NULL)) {
>> /* per entry fall back to ips */
>> id_offs[i].status = BPF_STACK_BUILD_ID_IP;
>> @@ -158,9 +164,12 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
>> memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX);
>> continue;
>> }
>> +build_id_valid:
>> id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i]
>> - vma->vm_start;
>> id_offs[i].status = BPF_STACK_BUILD_ID_VALID;
>> + prev_vma = vma;
>> + prev_build_id = id_offs[i].build_id;
>> }
>> bpf_mmap_unlock_mm(work, current->mm);
>> }
>> --
>> 2.35.1.473.g83b2b277ed-goog
>>
next prev parent reply other threads:[~2022-02-23 23:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-23 22:20 Hao Luo
2022-02-23 22:52 ` Andrii Nakryiko
2022-02-23 23:16 ` Greg Thelen [this message]
2022-02-23 23:40 ` Hao Luo
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=xr9335k918eh.fsf@gthelen2.svl.corp.google.com \
--to=gthelen@google.com \
--cc=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=blakejones@google.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=namhyung@kernel.org \
--cc=songliubraving@fb.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
all inboxes | Powered by JetHome®