From: Oleg Nesterov <oleg@redhat.com>
To: Yonghong Song <yonghong.song@linux.dev>
Cc: Alexei Starovoitov <ast@kernel.org>,
Chuyi Zhou <zhouchuyi@bytedance.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Kui-Feng Lee <kuifeng@fb.com>,
linux-kernel@vger.kernel.org, bpf@vger.kernel.org
Subject: Re: [PATCH 1/3] bpf: task_group_seq_get_next: use __next_thread() rather than next_thread()
Date: Thu, 16 Nov 2023 10:34:28 +0100 [thread overview]
Message-ID: <20231116093428.GA18748@redhat.com> (raw)
In-Reply-To: <34440ea4-3780-45e4-9e7c-1b36b535171b@linux.dev>
On 11/15, Yonghong Song wrote:
>
> On 11/14/23 11:32 AM, Oleg Nesterov wrote:
> >@@ -70,15 +70,13 @@ static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_comm
> > return NULL;
> > retry:
> >- task = next_thread(task);
> >+ task = __next_thread(task);
> >+ if (!task)
> >+ return NULL;
> > next_tid = __task_pid_nr_ns(task, PIDTYPE_PID, common->ns);
> >- if (!next_tid || next_tid == common->pid) {
> >- /* Run out of tasks of a process. The tasks of a
> >- * thread_group are linked as circular linked list.
> >- */
> >- return NULL;
> >- }
> >+ if (!next_tid)
> >+ goto retry;
>
> Look at the code. Looks like next_tid should never be 0
...
> pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
> struct pid_namespace *ns)
> {
> pid_t nr = 0;
>
> rcu_read_lock();
> if (!ns)
> ns = task_active_pid_ns(current);
> nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns);
^^^^^^^^^^^^^^^^^^^^^^^^^
Please note that task_pid_ptr(task, type)) can return NULL if this
task has already exited and called detach_pid().
detach_pid() does __change_pid(task, type, NULL), please note the
*pid_ptr = new; // NULL in this case
assignment in __change_pid().
IOW. The problem is not that ns can change, the problem is that
task->thread_pid (and other pid links) can be NULL, and in this
case pid_nr_ns() returns zero.
This code should be rewritten from the very beginning, it should
not rely on pid_nr. If nothing else common->pid and/or pid_visiting
can be reused. But currently my only concern is next_thread().
> Other than above, the change looks good to me.
Thanks for review!
Oleg.
next prev parent reply other threads:[~2023-11-16 9:35 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-14 16:32 [PATCH 0/3] bpf: kernel/bpf/task_iter.c: don't abuse next_thread() Oleg Nesterov
2023-11-14 16:32 ` [PATCH 1/3] bpf: task_group_seq_get_next: use __next_thread() rather than next_thread() Oleg Nesterov
2023-11-16 3:31 ` Yonghong Song
2023-11-16 9:34 ` Oleg Nesterov [this message]
2023-11-16 11:46 ` Yonghong Song
2023-11-14 16:32 ` [PATCH 2/3] bpf: bpf_iter_task_next: " Oleg Nesterov
2023-11-16 3:34 ` Yonghong Song
2023-11-14 16:32 ` [PATCH 3/3] bpf: bpf_iter_task_next: use next_task(kit->task) rather than next_task(kit->pos) Oleg Nesterov
2023-11-16 5:16 ` Yonghong Song
2023-11-16 9:38 ` Oleg Nesterov
2023-11-16 3:13 ` [PATCH 0/3] bpf: kernel/bpf/task_iter.c: don't abuse next_thread() Yonghong Song
2023-11-16 9:54 ` Oleg Nesterov
2023-11-16 11:52 ` Yonghong Song
2023-11-19 20:00 ` patchwork-bot+netdevbpf
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=20231116093428.GA18748@redhat.com \
--to=oleg@redhat.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kuifeng@fb.com \
--cc=linux-kernel@vger.kernel.org \
--cc=yonghong.song@linux.dev \
--cc=zhouchuyi@bytedance.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