Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3187859ybt; Sat, 4 Jul 2020 08:51:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx3FbwdBszBpi9A0+d9akt1bya1ZUqGSGwfHffp5bYvw5EILurOZEYyN2Z0/5mUBsTShM1e X-Received: by 2002:a17:906:d116:: with SMTP id b22mr13233251ejz.250.1593877893493; Sat, 04 Jul 2020 08:51:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593877893; cv=none; d=google.com; s=arc-20160816; b=JkS/b8seIK5WkQwBbEVQmiEv/jjRrBXVebYQs+CsjZ47wVElDfVCDNPK5SAlwGTxR5 7rjlq8K+zHymdKfOMiDRck7Rv3fI79NeEhcpZf7AmzxcnCvZM/V4Iukkg9ZNhtkLM+ui UQ4zDyOeT2998+CYhoVhn2PH6/N4NUyLRVtzGLUiXvqFVt46q0qcq7jR/rYIXXe0IFF9 OK6gtGLwqOAuAsdVChvoBNJyE8Qf4nzV2pYAzH05NVYZsK9Qv8vAnGZOp9fMTuxXYDC5 oMiaNlAETMTfKFWhgaLJM4wfsSrF9V6N5yis4VgcESGnKKDRnI5T/LFQaCDMK06Tzfbm 7PsA== 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=r3w7RYit4ppLWjqg50OVQ/O6O3tXQipsA4v43DAt3sA=; b=md1qTqibl59frtqFHV+9dbEUV8gAuNVBLbcU0Ru5cWQuIBqg0+7mRLrMAtU8eVpM5P twVQAfHHQQ+tZdLlSPOYG72hlKWFrLeGtwwwLevQca3ekhsFdQ/9WLYDPwV9Ja8BtzF8 l3HDZoS3Yk2iP1Pwagph+jQT/KSdTx6TbAKy3QwBJm6pmIxQhFTN5uAdRU36mCK437MV zc6GEIaSbUuukDoFeLlD6BdaRBSyNr4RCxia237rBavz1PFXxeESMf3ymVTg15L2al2e 3einuoMZKs0EQNbCKIvb0kFDCQKZivBlb7lCQ6Dk7UMfjVy6wfUVRj1Y1iQqn6yP3eJc fWAw== 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 s20si9994665edy.519.2020.07.04.08.51.11; Sat, 04 Jul 2020 08:51:33 -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 S1726922AbgGDPvA (ORCPT + 99 others); Sat, 4 Jul 2020 11:51:00 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:57146 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726643AbgGDPvA (ORCPT ); Sat, 4 Jul 2020 11:51:00 -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 1jrkRJ-00087y-O9; Sat, 04 Jul 2020 15:50:53 +0000 Date: Sat, 4 Jul 2020 17:50:52 +0200 From: Christian Brauner To: "Eric W. Biederman" Cc: Alexei Starovoitov , linux-kernel@vger.kernel.org, David Miller , Greg Kroah-Hartman , Tetsuo Handa , 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: <20200704155052.kmrest5useyxcfnu@wittgenstein> References: <87y2o1swee.fsf_-_@x220.int.ebiederm.org> <20200702164140.4468-13-ebiederm@xmission.com> <20200703203021.paebx25miovmaxqt@ast-mbp.dhcp.thefacebook.com> <873668s2j8.fsf@x220.int.ebiederm.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <873668s2j8.fsf@x220.int.ebiederm.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 03, 2020 at 04:37:47PM -0500, Eric W. Biederman wrote: > Alexei Starovoitov writes: > > > On Thu, Jul 02, 2020 at 11:41:37AM -0500, Eric W. Biederman wrote: > >> Create an independent helper thread_group_exited report return 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 missing 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. > >> > >> Signed-off-by: "Eric W. Biederman" > >> --- > >> 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 > >> + * 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; > >> +} > > > > I'm not sure why you think READ_ONCE was missing. > > It's different in wait_consider_task() where READ_ONCE is needed because > > of multiple checks. Here it's done once. > > In practice it probably has no effect on the generated code. But > READ_ONCE is about telling the compiler not to be clever. Don't use > tearing loads or stores etc. When all of the other readers are using > READ_ONCE I just get nervous if we have a case that doesn't. That's not true. The only place where READ_ONCE(->exit_state) is used is in wait_consider_task() and nowhere else. We had that discussion a while ago where I or someone proposed to simply place a READ_ONCE() around all accesses to exit_state for the sake of kcsan and we agreed that it's unnecessary and not to do this. But it obviously doesn't hurt to have it. Christian