Skip to content

Commit 38fd525

Browse files
committed
exit: Factor thread_group_exited out of pidfd_poll
Create an independent helper thread_group_exited which returns true when all threads have passed exit_notify in do_exit. AKA all of the threads are at least zombies and might be dead or completely gone. Create this helper by taking the logic out of pidfd_poll where it is already tested, and adding a READ_ONCE on the read of task->exit_state. I will be changing the user mode driver code to use this same logic to know when a user mode driver needs to be restarted. Place the new helper thread_group_exited in kernel/exit.c and EXPORT it so it can be used by modules. Link: https://lkml.kernel.org/r/[email protected] Acked-by: Christian Brauner <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Tested-by: Alexei Starovoitov <[email protected]> Signed-off-by: "Eric W. Biederman" <[email protected]>
1 parent 1c340ea commit 38fd525

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

include/linux/sched/signal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ static inline int thread_group_empty(struct task_struct *p)
674674
#define delay_group_leader(p) \
675675
(thread_group_leader(p) && !thread_group_empty(p))
676676

677+
extern bool thread_group_exited(struct pid *pid);
678+
677679
extern struct sighand_struct *__lock_task_sighand(struct task_struct *task,
678680
unsigned long *flags);
679681

kernel/exit.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,30 @@ COMPAT_SYSCALL_DEFINE5(waitid,
17131713
}
17141714
#endif
17151715

1716+
/**
1717+
* thread_group_exited - check that a thread group has exited
1718+
* @pid: tgid of thread group to be checked.
1719+
*
1720+
* Test if the thread group represented by tgid has exited (all
1721+
* threads are zombies, dead or completely gone).
1722+
*
1723+
* Return: true if the thread group has exited. false otherwise.
1724+
*/
1725+
bool thread_group_exited(struct pid *pid)
1726+
{
1727+
struct task_struct *task;
1728+
bool exited;
1729+
1730+
rcu_read_lock();
1731+
task = pid_task(pid, PIDTYPE_PID);
1732+
exited = !task ||
1733+
(READ_ONCE(task->exit_state) && thread_group_empty(task));
1734+
rcu_read_unlock();
1735+
1736+
return exited;
1737+
}
1738+
EXPORT_SYMBOL(thread_group_exited);
1739+
17161740
__weak void abort(void)
17171741
{
17181742
BUG();

kernel/fork.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,22 +1787,18 @@ static void pidfd_show_fdinfo(struct seq_file *m, struct file *f)
17871787
*/
17881788
static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts)
17891789
{
1790-
struct task_struct *task;
17911790
struct pid *pid = file->private_data;
17921791
__poll_t poll_flags = 0;
17931792

17941793
poll_wait(file, &pid->wait_pidfd, pts);
17951794

1796-
rcu_read_lock();
1797-
task = pid_task(pid, PIDTYPE_PID);
17981795
/*
17991796
* Inform pollers only when the whole thread group exits.
18001797
* If the thread group leader exits before all other threads in the
18011798
* group, then poll(2) should block, similar to the wait(2) family.
18021799
*/
1803-
if (!task || (task->exit_state && thread_group_empty(task)))
1800+
if (thread_group_exited(pid))
18041801
poll_flags = EPOLLIN | EPOLLRDNORM;
1805-
rcu_read_unlock();
18061802

18071803
return poll_flags;
18081804
}

0 commit comments

Comments
 (0)