Skip to contentSkip to conversation

pid: document that do_each_pid_task() needs tasklist_lock

PATCH Applied Oleg Nesterov 3 participants 3 messages Oct 11 – Oct 12

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pid: document that do_each_pid_task() needs tasklist_lock

do_each_pid_task() looks "obviously rcu-safe". This confuses people and we already have 2 buggy users: send_sigio() and send_sigurg().

The reasons why it is not are not obvious at all, add the comment.

---
 include/linux/pid.h | 9 +++++++++
 1 file changed, 9 insertions(+)
include/linux/pid.h +9 -0
index ddaef0bbc8ba..f5476d7140a3 100644
@@ -187,6 +187,15 @@ static inline pid_t pid_nr(struct pid *pid)
 pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns);
 pid_t pid_vnr(struct pid *pid);
 
+/*
+ * Needs tasklist_lock if type > PIDTYPE_TGID. A lockless reader can:
+ *
+ * - race with a change_pid() that moves the task to another hlist,
+ *   and continue to scan the wrong list.
+ *
+ * - race with a copy_process() that adds a new task to the start of hlist,
+ *   and miss the new child.
+ */
 #define do_each_pid_task(pid, type, task)				\
 	do {								\
 		if ((pid) != NULL)					\
Signature · 1 line
2.55.0
#1 lore raw
Quoted 4 lines · Oleg Nesterov

do_each_pid_task() looks "obviously rcu-safe". This confuses people and we already have 2 buggy users: send_sigio() and send_sigurg().

The reasons why it is not are not obvious at all, add the comment.

Applied to the kernel-7.4.signal branch of the vfs/vfs.git tree. Patches in the kernel-7.4.signal branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: kernel-7.4.signal

[1/1] pid: document that do_each_pid_task() needs tasklist_lock

      https://git.kernel.org/vfs/vfs/c/41e24dc80739
#2 lore raw
In reply to Oleg Nesterov
Quoted 15 lines · On Sun, 11 Oct 2026 16:16:51 +0200 Oleg Nesterov <[email protected]> wrote:

do_each_pid_task() looks "obviously rcu-safe". This confuses people and we already have 2 buggy users: send_sigio() and send_sigurg().

The reasons why it is not are not obvious at all, add the comment.

---
 include/linux/pid.h | 9 +++++++++
 1 file changed, 9 insertions(+)
include/linux/pid.h +0 -0
index ddaef0bbc8ba..f5476d7140a3 100644

include/linux/pid.h static inline pid_t pid_nr(struct pid *pid) · line 191

Quoted 5 lines
include/linux/pid.h +2 -0
@@ -187,6 +187,15 @@ static inline pid_t pid_nr(struct pid *pid)
 pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns);
 pid_t pid_vnr(struct pid *pid);
 
+/*
+ * Needs tasklist_lock if type > PIDTYPE_TGID. A lockless reader can:

Think I'd be more explicit, so:
... if type isn't PIDTYPE_PID or PIDTYPE_TGID.
David

Quoted 10 lines
include/linux/pid.h +7 -0
+ *
+ * - race with a change_pid() that moves the task to another hlist,
+ *   and continue to scan the wrong list.
+ *
+ * - race with a copy_process() that adds a new task to the start of hlist,
+ *   and miss the new child.
+ */
 #define do_each_pid_task(pid, type, task)				\
 	do {								\
 		if ((pid) != NULL)					\