exit: Factor thread_group_exited out of pidfd_poll
authorEric W. Biederman <ebiederm@xmission.com>
Wed, 1 Jul 2020 12:30:06 +0000 (07:30 -0500)
committerEric W. Biederman <ebiederm@xmission.com>
Tue, 7 Jul 2020 16:58:17 +0000 (11:58 -0500)
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/20200702164140.4468-13-ebiederm@xmission.com
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Tested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
include/linux/sched/signal.h
kernel/exit.c
kernel/fork.c

index 0ee5e696c5d8d1bffa611fe28d56d37aac3e3cb7..1bad18a1d8ba71649a620f5124d3a262242ff542 100644 (file)
@@ -674,6 +674,8 @@ static inline int thread_group_empty(struct task_struct *p)
 #define delay_group_leader(p) \
                (thread_group_leader(p) && !thread_group_empty(p))
 
+extern bool thread_group_exited(struct pid *pid);
+
 extern struct sighand_struct *__lock_task_sighand(struct task_struct *task,
                                                        unsigned long *flags);
 
index d3294b611df1841e43b5f7e328259d15e8430af7..dee246c0866f56d7fb12bfdb6600e5806a2fba06 100644 (file)
@@ -1713,6 +1713,30 @@ Efault:
 }
 #endif
 
+/**
+ * thread_group_exited - check that a thread group has exited
+ * @pid: tgid of thread group to be checked.
+ *
+ * Test if the thread group represented by tgid has exited (all
+ * threads are zombies, dead or completely gone).
+ *
+ * Return: true if the thread group has exited. false otherwise.
+ */
+bool thread_group_exited(struct pid *pid)
+{
+       struct task_struct *task;
+       bool exited;
+
+       rcu_read_lock();
+       task = pid_task(pid, PIDTYPE_PID);
+       exited = !task ||
+               (READ_ONCE(task->exit_state) && thread_group_empty(task));
+       rcu_read_unlock();
+
+       return exited;
+}
+EXPORT_SYMBOL(thread_group_exited);
+
 __weak void abort(void)
 {
        BUG();
index 142b23645d82e76297298d0dd62fd0b11969685a..bf215af7a904033edcf4764a4af89e43b2b38bac 100644 (file)
@@ -1787,22 +1787,18 @@ static void pidfd_show_fdinfo(struct seq_file *m, struct file *f)
  */
 static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts)
 {
-       struct task_struct *task;
        struct pid *pid = file->private_data;
        __poll_t poll_flags = 0;
 
        poll_wait(file, &pid->wait_pidfd, pts);
 
-       rcu_read_lock();
-       task = pid_task(pid, PIDTYPE_PID);
        /*
         * Inform pollers only when the whole thread group exits.
         * If the thread group leader exits before all other threads in the
         * group, then poll(2) should block, similar to the wait(2) family.
         */
-       if (!task || (task->exit_state && thread_group_empty(task)))
+       if (thread_group_exited(pid))
                poll_flags = EPOLLIN | EPOLLRDNORM;
-       rcu_read_unlock();
 
        return poll_flags;
 }