From: Song Liu <songliubraving@fb.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, Networking <netdev@vger.kernel.org>,
"Alexei Starovoitov" <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
"Andrii Nakryiko" <andrii@kernel.org>,
john fastabend <john.fastabend@gmail.com>,
"KP Singh" <kpsingh@chromium.org>,
Kernel Team <Kernel-team@fb.com>
Subject: Re: [PATCH v2 bpf-next 1/4] bpf: introduce task_vma bpf_iter
Date: Thu, 17 Dec 2020 01:51:13 +0000 [thread overview]
Message-ID: <CE22B399-1CA1-460E-9A74-74B44CCF675E@fb.com> (raw)
In-Reply-To: <CAEf4BzYNAd7V=EevVYiz48+q=UjNnRBzfQFA4tTYCX8a9Wfu7A@mail.gmail.com>
> On Dec 16, 2020, at 4:34 PM, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
>
> On Tue, Dec 15, 2020 at 3:37 PM Song Liu <songliubraving@fb.com> wrote:
>>
>> Introduce task_vma bpf_iter to print memory information of a process. It
>> can be used to print customized information similar to /proc/<pid>/maps.
>>
>> task_vma iterator releases mmap_lock before calling the BPF program.
>> Therefore, we cannot pass vm_area_struct directly to the BPF program. A
>> new __vm_area_struct is introduced to keep key information of a vma. On
>> each iteration, task_vma gathers information in __vm_area_struct and
>> passes it to the BPF program.
>>
>> If the vma maps to a file, task_vma also holds a reference to the file
>> while calling the BPF program.
>>
>> Signed-off-by: Song Liu <songliubraving@fb.com>
>> ---
>> include/linux/bpf.h | 2 +-
>> kernel/bpf/task_iter.c | 205 ++++++++++++++++++++++++++++++++++++++++-
>> 2 files changed, 205 insertions(+), 2 deletions(-)
>>
>> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
>> index 07cb5d15e7439..49dd1e29c8118 100644
>> --- a/include/linux/bpf.h
>> +++ b/include/linux/bpf.h
>> @@ -1325,7 +1325,7 @@ enum bpf_iter_feature {
>> BPF_ITER_RESCHED = BIT(0),
>> };
>>
>> -#define BPF_ITER_CTX_ARG_MAX 2
>> +#define BPF_ITER_CTX_ARG_MAX 3
>> struct bpf_iter_reg {
>> const char *target;
>> bpf_iter_attach_target_t attach_target;
>> diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c
>> index 0458a40edf10a..15a066b442f75 100644
>> --- a/kernel/bpf/task_iter.c
>> +++ b/kernel/bpf/task_iter.c
>> @@ -304,9 +304,183 @@ static const struct seq_operations task_file_seq_ops = {
>> .show = task_file_seq_show,
>> };
>>
>> +/*
>> + * Key information from vm_area_struct. We need this because we cannot
>> + * assume the vm_area_struct is still valid after each iteration.
>> + */
>> +struct __vm_area_struct {
>> + __u64 start;
>> + __u64 end;
>> + __u64 flags;
>> + __u64 pgoff;
>
> I'd keep the original names of the fields (vm_start, vm_end, etc).
I thought about the names. Unlike the kernel fs/mm code, where there
are many different start/end/offset/flags, the prefix doesn't seem to
be helpful in the BPF programs. In fact, it is probably easier for
the developers to differentiate __vm_area_struct->start and
vm_area_struct->vm_start.
Also, we have bpf_iter__task_vma->file, which is the same as
vm_area_struct->vm_file. If we prefix __vm_area_struct members, it
will be a little weird to name it either "vm_file" or "file".
Given these reasons, I decided to not have vm_ prefix. Does this make
sense?
> But
> there are some more fields which seem useful, like vm_page_prot,
> vm_mm, etc.
vm_page_prot doesn't really provide extra information than vm_flags.
Please refer to mm/mmap.c vm_get_page_prot().
We have the vm_mm in task->mm, so no need to add it to __vm_area_struct.
>
> It's quite unfortunate, actually, that bpf_iter program doesn't get
> access to the real vm_area_struct, because it won't be able to do much
> beyond using fields that we pre-defined here. E.g., there could be
> interesting things to do with vm_mm, but unfortunately it won't be
> possible.
>
> Is there any way to still provide access to the original
> vm_area_struct and let BPF programs use BTF magic to follow all those
> pointers (like vm_mm) safely?
We hold a reference to task, and the task holds a reference to task->mm,
so we can safely probe_read information in mm_struct, like the page
table.
Thanks,
Song
next prev parent reply other threads:[~2020-12-17 1:52 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-15 23:36 [PATCH v2 bpf-next 0/4] introduce bpf_iter for task_vma Song Liu
2020-12-15 23:36 ` [PATCH v2 bpf-next 1/4] bpf: introduce task_vma bpf_iter Song Liu
2020-12-16 17:36 ` Yonghong Song
2020-12-16 19:41 ` Song Liu
2020-12-17 0:34 ` Andrii Nakryiko
2020-12-17 1:51 ` Song Liu [this message]
2020-12-17 19:03 ` Alexei Starovoitov
2020-12-17 22:08 ` Song Liu
2020-12-18 2:34 ` Alexei Starovoitov
2020-12-18 3:15 ` Yonghong Song
2020-12-18 4:33 ` Song Liu
2020-12-18 5:23 ` Alexei Starovoitov
2020-12-18 16:38 ` Yonghong Song
2020-12-18 17:23 ` Song Liu
2021-01-05 1:46 ` Alexei Starovoitov
2021-01-05 5:47 ` Song Liu
2021-01-05 16:27 ` Alexei Starovoitov
2021-01-05 17:10 ` Song Liu
2021-01-05 17:27 ` Alexei Starovoitov
2021-01-05 19:38 ` Song Liu
2021-01-05 19:46 ` Alexei Starovoitov
2021-01-05 19:51 ` Song Liu
2020-12-15 23:37 ` [PATCH v2 bpf-next 2/4] bpf: allow bpf_d_path in sleepable bpf_iter program Song Liu
2020-12-16 17:41 ` Yonghong Song
2020-12-16 18:15 ` KP Singh
2020-12-16 18:31 ` KP Singh
2021-01-25 12:52 ` Jiri Olsa
2020-12-15 23:37 ` [PATCH v2 bpf-next 3/4] libbpf: introduce section "iter.s/" for " Song Liu
2020-12-16 17:42 ` Yonghong Song
2020-12-16 18:00 ` KP Singh
2020-12-15 23:37 ` [PATCH v2 bpf-next 4/4] selftests/bpf: add test for bpf_iter_task_vma Song Liu
2020-12-16 18:18 ` Yonghong Song
2020-12-16 23:23 ` Song Liu
2020-12-16 17:00 ` [PATCH v2 bpf-next 0/4] introduce bpf_iter for task_vma Yonghong Song
2020-12-16 17:35 ` Song Liu
2020-12-16 18:31 ` Yonghong Song
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=CE22B399-1CA1-460E-9A74-74B44CCF675E@fb.com \
--to=songliubraving@fb.com \
--cc=Kernel-team@fb.com \
--cc=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=john.fastabend@gmail.com \
--cc=kpsingh@chromium.org \
--cc=netdev@vger.kernel.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).