From: Christian Brauner <brauner@kernel.org>
To: Oleg Nesterov <oleg@redhat.com>
Cc: Christian Brauner <brauner@kernel.org>,
linux-fsdevel@vger.kernel.org, Jeff Layton <jlayton@kernel.org>,
Lennart Poettering <lennart@poettering.net>,
Daan De Meyer <daan.j.demeyer@gmail.com>,
Mike Yuan <me@yhndnzj.com>,
linux-kernel@vger.kernel.org,
Peter Ziljstra <peterz@infradead.org>
Subject: [RFC PATCH] pidfs: ensure consistent ENOENT/ESRCH reporting
Date: Wed, 9 Apr 2025 20:18:58 +0200 [thread overview]
Message-ID: <20250409-rohstoff-ungnade-d1afa571f32c@brauner> (raw)
In-Reply-To: <20250409-sesshaft-absurd-35d97607142c@brauner>
In a prior patch series we tried to cleanly differentiate between:
(1) The task has already been reaped.
(2) The caller requested a pidfd for a thread-group leader but the pid
actually references a struct pid that isn't used as a thread-group
leader.
as this was causing issues for non-threaded workloads.
But there's cases where the current simple logic is wrong. Specifically,
if the pid was a leader pid and the check races with __unhash_process().
Stabilize this by using a sequence counter associated with tasklist_lock
and retry while we're in __unhash_process(). The seqcounter might be
useful independent of pidfs.
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
include/linux/pid.h | 1 +
kernel/exit.c | 11 +++++++++++
kernel/fork.c | 22 ++++++++++++----------
kernel/pid.c | 1 +
4 files changed, 25 insertions(+), 10 deletions(-)
diff --git a/include/linux/pid.h b/include/linux/pid.h
index 311ecebd7d56..b54a4c1ef602 100644
--- a/include/linux/pid.h
+++ b/include/linux/pid.h
@@ -65,6 +65,7 @@ struct pid
struct hlist_head inodes;
/* wait queue for pidfd notifications */
wait_queue_head_t wait_pidfd;
+ seqcount_rwlock_t pid_seq;
struct rcu_head rcu;
struct upid numbers[];
};
diff --git a/kernel/exit.c b/kernel/exit.c
index 1b51dc099f1e..8050572fe682 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -133,17 +133,28 @@ struct release_task_post {
static void __unhash_process(struct release_task_post *post, struct task_struct *p,
bool group_dead)
{
+ struct pid *pid;
+
+ lockdep_assert_held_write(&tasklist_lock);
+
nr_threads--;
+
+ pid = task_pid(p);
+ raw_write_seqcount_begin(&pid->pid_seq);
detach_pid(post->pids, p, PIDTYPE_PID);
if (group_dead) {
detach_pid(post->pids, p, PIDTYPE_TGID);
detach_pid(post->pids, p, PIDTYPE_PGID);
detach_pid(post->pids, p, PIDTYPE_SID);
+ }
+ raw_write_seqcount_end(&pid->pid_seq);
+ if (group_dead) {
list_del_rcu(&p->tasks);
list_del_init(&p->sibling);
__this_cpu_dec(process_counts);
}
+
list_del_rcu(&p->thread_node);
}
diff --git a/kernel/fork.c b/kernel/fork.c
index 4a2080b968c8..1480bf6f5f38 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -2109,24 +2109,26 @@ static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **re
int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
{
int err = 0;
+ unsigned int seq;
- if (!(flags & PIDFD_THREAD)) {
+ do {
+ seq = raw_seqcount_begin(&pid->pid_seq);
/*
* If this is struct pid isn't used as a thread-group
* leader pid but the caller requested to create a
* thread-group leader pidfd then report ENOENT to the
* caller as a hint.
*/
- if (!pid_has_task(pid, PIDTYPE_TGID))
+ if (!(flags & PIDFD_THREAD) && !pid_has_task(pid, PIDTYPE_TGID))
err = -ENOENT;
- }
-
- /*
- * If this wasn't a thread-group leader struct pid or the task
- * got reaped in the meantime report -ESRCH to userspace.
- */
- if (!pid_has_task(pid, PIDTYPE_PID))
- err = -ESRCH;
+ /*
+ * If this wasn't a thread-group leader struct pid or
+ * the task got reaped in the meantime report -ESRCH to
+ * userspace.
+ */
+ if (!pid_has_task(pid, PIDTYPE_PID))
+ err = -ESRCH;
+ } while (read_seqcount_retry(&pid->pid_seq, seq));
if (err)
return err;
diff --git a/kernel/pid.c b/kernel/pid.c
index 4ac2ce46817f..bbca61f62faa 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -271,6 +271,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid,
upid = pid->numbers + ns->level;
idr_preload(GFP_KERNEL);
spin_lock(&pidmap_lock);
+ seqcount_rwlock_init(&pid->pid_seq, &tasklist_lock);
if (!(ns->pid_allocated & PIDNS_ADDING))
goto out_unlock;
pidfs_add_pid(pid);
--
2.47.2
next prev parent reply other threads:[~2025-04-09 18:19 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-03 14:09 [PATCH RFC 0/4] pidfd: improve uapi when task isn't found Christian Brauner
2025-04-03 14:09 ` [PATCH RFC 1/4] selftests/pidfd: adapt to recent changes Christian Brauner
2025-04-03 14:09 ` [PATCH RFC 2/4] pidfd: remove unneeded NULL check from pidfd_prepare() Christian Brauner
2025-04-03 14:09 ` [PATCH RFC 3/4] pidfd: improve uapi when task isn't found Christian Brauner
2025-04-04 12:37 ` Oleg Nesterov
2025-04-04 13:38 ` Christian Brauner
2025-04-04 14:53 ` Oleg Nesterov
2025-04-09 15:38 ` Christian Brauner
2025-04-09 18:18 ` Christian Brauner [this message]
2025-04-09 18:40 ` [RFC PATCH] pidfs: ensure consistent ENOENT/ESRCH reporting Oleg Nesterov
2025-04-10 10:18 ` Oleg Nesterov
2025-04-10 10:43 ` Christian Brauner
2025-04-10 13:10 ` Oleg Nesterov
2025-04-10 20:05 ` Christian Brauner
2025-04-10 20:24 ` Christian Brauner
2025-04-11 11:08 ` Christian Brauner
2025-04-11 11:25 ` Oleg Nesterov
2025-04-11 11:41 ` Oleg Nesterov
2025-04-03 14:09 ` [PATCH RFC 4/4] selftest/pidfd: add test for thread-group leader pidfd open for thread Christian Brauner
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=20250409-rohstoff-ungnade-d1afa571f32c@brauner \
--to=brauner@kernel.org \
--cc=daan.j.demeyer@gmail.com \
--cc=jlayton@kernel.org \
--cc=lennart@poettering.net \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=me@yhndnzj.com \
--cc=oleg@redhat.com \
--cc=peterz@infradead.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
all inboxes | Powered by JetHome®