From: Yonghong Song <yhs@fb.com>
To: Josef Bacik <josef@toxicpanda.com>, <bpf@vger.kernel.org>,
<netdev@vger.kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>, <kernel-team@fb.com>,
Andrii Nakryiko <andriin@fb.com>
Subject: Re: [PATCH bpf-next v2 1/2] bpf: avoid iterating duplicated files for task_file iterator
Date: Tue, 1 Sep 2020 10:38:13 -0700 [thread overview]
Message-ID: <31e47eed-0ac0-b221-16ba-5284c1ec6386@fb.com> (raw)
In-Reply-To: <01bc7a06-e294-7cfe-d284-1b7e834ba90f@toxicpanda.com>
On 9/1/20 10:18 AM, Josef Bacik wrote:
> On 8/28/20 1:38 AM, Yonghong Song wrote:
>> Currently, task_file iterator iterates all files from all tasks.
>> This may potentially visit a lot of duplicated files if there are
>> many tasks sharing the same files, e.g., typical pthreads
>> where these pthreads and the main thread are sharing the same files.
>>
>> This patch changed task_file iterator to skip a particular task
>> if that task shares the same files as its group_leader (the task
>> having the same tgid and also task->tgid == task->pid).
>> This will preserve the same result, visiting all files from all
>> tasks, and will reduce runtime cost significantl, e.g., if there are
>> a lot of pthreads and the process has a lot of open files.
>>
>> Suggested-by: Andrii Nakryiko <andriin@fb.com>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
>> kernel/bpf/task_iter.c | 14 +++++++++-----
>> 1 file changed, 9 insertions(+), 5 deletions(-)
>>
>> It would be good if somebody familar with sched code can help check
>> whether I missed anything or not (e.g., locks, etc.)
>> for the code change
>> task->files == task->group_leader->files
>>
>> Note the change in this patch might have conflicts with
>> e60572b8d4c3 ("bpf: Avoid visit same object multiple times")
>> which is merged into bpf/net sometimes back.
>>
>> diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c
>> index 232df29793e9..0c5c96bb6964 100644
>> --- a/kernel/bpf/task_iter.c
>> +++ b/kernel/bpf/task_iter.c
>> @@ -22,7 +22,8 @@ struct bpf_iter_seq_task_info {
>> };
>> static struct task_struct *task_seq_get_next(struct pid_namespace *ns,
>> - u32 *tid)
>> + u32 *tid,
>> + bool skip_if_dup_files)
>> {
>> struct task_struct *task = NULL;
>> struct pid *pid;
>> @@ -32,7 +33,10 @@ static struct task_struct *task_seq_get_next(struct
>> pid_namespace *ns,
>> pid = idr_get_next(&ns->idr, tid);
>> if (pid) {
>> task = get_pid_task(pid, PIDTYPE_PID);
>> - if (!task) {
>> + if (!task ||
>> + (skip_if_dup_files &&
>> + task->tgid != task->pid &&
>> + task->files == task->group_leader->files)) {
>> ++*tid;
>> goto retry;
>
> Sorry I only checked the task->files and task->group_leader thing, I
> forgot to actually pay attention to what the patch itself was doing.
>
> This will leak task structs, you need something like
>
> if (!task) {
> ++*tid;
> goto retry;
> }
> if (skip_if_dup_files && etc) {
> ++*tid;
> put_task_struct(task);
> goto retry;
> }
>
> otherwise you'll leak tasks. Thanks,
Or, yes, good catch. Thanks! I too focused on task->files ==
task->group_lead->files and did not think deep on leaking
tasks.
Will send v2.
>
> Josef
next prev parent reply other threads:[~2020-09-01 17:38 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-28 5:38 [PATCH bpf-next v2 0/2] bpf: avoid iterating duplicated files for task_file iterator Yonghong Song
2020-08-28 5:38 ` [PATCH bpf-next v2 1/2] " Yonghong Song
2020-09-01 16:17 ` Josef Bacik
2020-09-01 17:18 ` Josef Bacik
2020-09-01 17:38 ` Yonghong Song [this message]
2020-08-28 5:38 ` [PATCH bpf-next v2 2/2] selftests/bpf: test task_file iterator without visiting pthreads Yonghong Song
2020-09-02 0:41 ` Andrii Nakryiko
2020-09-02 2:18 ` 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=31e47eed-0ac0-b221-16ba-5284c1ec6386@fb.com \
--to=yhs@fb.com \
--cc=andriin@fb.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=josef@toxicpanda.com \
--cc=kernel-team@fb.com \
--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).