Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3194639ybt; Sat, 4 Jul 2020 09:01:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzYkCxP1LU1v1QqzMIRWCL5ULBnDkzrmcPZ+R40OD/SY1Y7eXRBhoTWqR+Q/6tVg98X7qxc X-Received: by 2002:a17:906:1151:: with SMTP id i17mr38704816eja.535.1593878480325; Sat, 04 Jul 2020 09:01:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593878480; cv=none; d=google.com; s=arc-20160816; b=qLCstULUeiQEIdAP3TYof7uPM+VHAstkwDiVI5ldvPyAT5Z+mSVqk9uoz5UoT/IcVJ kQ1Haoi6ZwQf+kWVnYKRLZA969/93/ZLMszoRARNsAULKNC8LAPquOD5usTxPDENRjmr 0SaZORLNb2NM6Qw+X5Wh6/MoXKdPtJj5EbIOB1iL2cr1hORVLZrdF7BnjYdt4ujXkIWe 2/2BTpzH6e/wf5IWOsp+KlJnuiIste+g0/W/uwhmcDczEtQ13BBYiLTrjApxkPe+lfKE 4mwXV1t+RjKqL9bwkBolAzA6W1gCVQkqKbVqscxMfuTEsI364M/UxdRwY4UpIXWvRJNA 8iYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=MneLPuD5S3HvDGKfJhCT3Hqkhp0P853kQ3fB39pbxjY=; b=PiDcvI4jldwuHEDa72uuAFlAbVoS/bpyCpu93m/ZxBIzHyAJvgEnik53Bh8Sd3hZUN FNVkNcg2RjB+rcR+7mLskg0G/njE0xL1/cLSAm8QcMwTD+JooCAOe4HPnMp3764TowKY XwP0h+9N9+hhR2pk6ha60EPmXuemrVh7fDOMu5NmgW/pSz8cEaiHOAqwLdGAPfk6gI3F LKXyS9ILYXlwoc+xYQ6oP//TETFgULW8hCjh0XGr2uZ0M98RTrxnzueNj4s+ve6WlO7H jPn/0Z2L9qEGHbjMtQLVegtU+Z5Bx65oHEIzDnO3aZsyxgxSpbeHL4S0ZYWE+DGTIOkf vhRA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f20si10001619edc.220.2020.07.04.09.00.57; Sat, 04 Jul 2020 09:01:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726747AbgGDQAx (ORCPT + 99 others); Sat, 4 Jul 2020 12:00:53 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:57226 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726638AbgGDQAw (ORCPT ); Sat, 4 Jul 2020 12:00:52 -0400 Received: from ip5f5af08c.dynamic.kabel-deutschland.de ([95.90.240.140] helo=wittgenstein) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1jrkav-00008Q-2S; Sat, 04 Jul 2020 16:00:49 +0000 Date: Sat, 4 Jul 2020 18:00:48 +0200 From: Christian Brauner To: "Eric W. Biederman" Cc: linux-kernel@vger.kernel.org, David Miller , Greg Kroah-Hartman , Tetsuo Handa , Alexei Starovoitov , Kees Cook , Andrew Morton , Alexei Starovoitov , Al Viro , bpf , linux-fsdevel , Daniel Borkmann , Jakub Kicinski , Masahiro Yamada , Gary Lin , Bruno Meneguele , LSM List , Casey Schaufler , Luis Chamberlain , Linus Torvalds Subject: Re: [PATCH v3 13/16] exit: Factor thread_group_exited out of pidfd_poll Message-ID: <20200704160048.l2yvs3w5k4vclpdv@wittgenstein> References: <87y2o1swee.fsf_-_@x220.int.ebiederm.org> <20200702164140.4468-13-ebiederm@xmission.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20200702164140.4468-13-ebiederm@xmission.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 02, 2020 at 11:41:37AM -0500, Eric W. Biederman wrote: > Create an independent helper thread_group_exited report return true s/report return/which reports/ > 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 missing READ_ONCE on > the read of task->exit_state. I would prefer to have this comment dropped as this read_once() is not missing as you can see from the comments elsewhere in this thread. > > 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. > > Signed-off-by: "Eric W. Biederman" > --- Minus the typos above and below, this looks good and passes the pidfd and process test-suite. Acked-by: Christian Brauner Thanks! Christian > include/linux/sched/signal.h | 2 ++ > kernel/exit.c | 24 ++++++++++++++++++++++++ > kernel/fork.c | 6 +----- > 3 files changed, 27 insertions(+), 5 deletions(-) > > diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h > index 0ee5e696c5d8..1bad18a1d8ba 100644 > --- a/include/linux/sched/signal.h > +++ b/include/linux/sched/signal.h > @@ -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); > > diff --git a/kernel/exit.c b/kernel/exit.c > index d3294b611df1..a7f112feb0f6 100644 > --- a/kernel/exit.c > +++ b/kernel/exit.c > @@ -1713,6 +1713,30 @@ COMPAT_SYSCALL_DEFINE5(waitid, > } > #endif > > +/** > + * thread_group_exited - check that a thread group has exited > + * @pid: tgid of thread group to be checked. > + * > + * Test if thread group is has exited (all threads are zombies, dead s/is has exited/has exited/ > + * 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(); > diff --git a/kernel/fork.c b/kernel/fork.c > index 142b23645d82..bf215af7a904 100644 > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -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; > } > -- > 2.25.0 >