From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EAE1EC433F5 for ; Wed, 27 Apr 2022 22:35:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236362AbiD0Wig (ORCPT ); Wed, 27 Apr 2022 18:38:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229647AbiD0Wia (ORCPT ); Wed, 27 Apr 2022 18:38:30 -0400 Received: from out02.mta.xmission.com (out02.mta.xmission.com [166.70.13.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4479DA7; Wed, 27 Apr 2022 15:35:16 -0700 (PDT) Received: from in01.mta.xmission.com ([166.70.13.51]:39804) by out02.mta.xmission.com with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1njqFe-004CCe-1P; Wed, 27 Apr 2022 16:35:14 -0600 Received: from ip68-227-174-4.om.om.cox.net ([68.227.174.4]:35990 helo=email.froward.int.ebiederm.org.xmission.com) by in01.mta.xmission.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1njqFc-00Bd4H-Ux; Wed, 27 Apr 2022 16:35:13 -0600 From: "Eric W. Biederman" To: Oleg Nesterov Cc: linux-kernel@vger.kernel.org, rjw@rjwysocki.net, mingo@kernel.org, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, mgorman@suse.de, bigeasy@linutronix.de, Will Deacon , tj@kernel.org, linux-pm@vger.kernel.org, Peter Zijlstra , Richard Weinberger , Anton Ivanov , Johannes Berg , linux-um@lists.infradead.org, Chris Zankel , Max Filippov , inux-xtensa@linux-xtensa.org, Kees Cook , Jann Horn References: <878rrrh32q.fsf_-_@email.froward.int.ebiederm.org> <20220426225211.308418-9-ebiederm@xmission.com> <20220427154158.GG17421@redhat.com> Date: Wed, 27 Apr 2022 17:35:05 -0500 In-Reply-To: <20220427154158.GG17421@redhat.com> (Oleg Nesterov's message of "Wed, 27 Apr 2022 17:41:59 +0200") Message-ID: <87mtg62m06.fsf@email.froward.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1njqFc-00Bd4H-Ux;;;mid=<87mtg62m06.fsf@email.froward.int.ebiederm.org>;;;hst=in01.mta.xmission.com;;;ip=68.227.174.4;;;frm=ebiederm@xmission.com;;;spf=softfail X-XM-AID: U2FsdGVkX18S5RmCcBMZtZ5La6DbmgpHNqUhFhXjeT0= X-SA-Exim-Connect-IP: 68.227.174.4 X-SA-Exim-Mail-From: ebiederm@xmission.com Subject: Re: [PATCH 9/9] ptrace: Don't change __state X-SA-Exim-Version: 4.2.1 (built Sat, 08 Feb 2020 21:53:50 +0000) X-SA-Exim-Scanned: Yes (on in01.mta.xmission.com) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Oleg Nesterov writes: 2> On 04/26, Eric W. Biederman wrote: >> >> static void ptrace_unfreeze_traced(struct task_struct *task) >> { >> - if (READ_ONCE(task->__state) != __TASK_TRACED) >> + if (!(READ_ONCE(task->jobctl) & JOBCTL_DELAY_WAKEKILL)) >> return; >> >> WARN_ON(!task->ptrace || task->parent != current); >> @@ -213,11 +213,10 @@ static void ptrace_unfreeze_traced(struct task_struct *task) >> * Recheck state under the lock to close this race. >> */ >> spin_lock_irq(&task->sighand->siglock); > > Now that we do not check __state = __TASK_TRACED, we need lock_task_sighand(). > The tracee can be already woken up by ptrace_resume(), but it is possible that > it didn't clear DELAY_WAKEKILL yet. Yes. The subtle differences in when __TASK_TRACED and JOBCTL_DELAY_WAKEKILL are cleared are causing me some minor issues. This "WARN_ON(!task->ptrace || task->parent != current);" also now needs to be inside siglock, because the __TASK_TRACED is insufficient. > Now, before we take ->siglock, the tracee can exit and another thread can do > wait() and reap this task. > > Also, I think the comment above should be updated. I agree, it makes sense to > re-check JOBCTL_DELAY_WAKEKILL under siglock just for clarity, but we no longer > need to do this to close the race; jobctl &= ~JOBCTL_DELAY_WAKEKILL and > wake_up_state() are safe even if JOBCTL_DELAY_WAKEKILL was already > cleared. I think you are right about it being safe, but I am having a hard time convincing myself that is true. I want to be very careful sending __TASK_TRACED wake_ups as ptrace_stop fundamentally can't handle spurious wake_ups. So I think adding task_is_traced to the test to verify the task is still frozen. static void ptrace_unfreeze_traced(struct task_struct *task) { unsigned long flags; /* * Verify the task is still frozen before unfreezing it, * ptrace_resume could have unfrozen us. */ if (lock_task_sighand(task, &flags)) { if ((task->jobctl & JOBCTL_DELAY_WAKEKILL) && task_is_traced(task)) { task->jobctl &= ~JOBCTL_DELAY_WAKEKILL; if (__fatal_signal_pending(task)) wake_up_state(task, __TASK_TRACED); } unlock_task_sighand(task, &flags); } } >> @@ -2307,6 +2307,7 @@ static int ptrace_stop(int exit_code, int why, int clear_code, >> >> /* LISTENING can be set only during STOP traps, clear it */ >> current->jobctl &= ~JOBCTL_LISTENING; >> + current->jobctl &= ~JOBCTL_DELAY_WAKEKILL; > > minor, but > > current->jobctl &= ~(JOBCTL_LISTENING | JOBCTL_DELAY_WAKEKILL); > > looks better. Yes. Eric From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from out02.mta.xmission.com ([166.70.13.232]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1njqFg-003nCR-JI for linux-um@lists.infradead.org; Wed, 27 Apr 2022 22:35:18 +0000 From: "Eric W. Biederman" References: <878rrrh32q.fsf_-_@email.froward.int.ebiederm.org> <20220426225211.308418-9-ebiederm@xmission.com> <20220427154158.GG17421@redhat.com> Date: Wed, 27 Apr 2022 17:35:05 -0500 In-Reply-To: <20220427154158.GG17421@redhat.com> (Oleg Nesterov's message of "Wed, 27 Apr 2022 17:41:59 +0200") Message-ID: <87mtg62m06.fsf@email.froward.int.ebiederm.org> MIME-Version: 1.0 Subject: Re: [PATCH 9/9] ptrace: Don't change __state List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-um" Errors-To: linux-um-bounces+geert=linux-m68k.org@lists.infradead.org To: Oleg Nesterov Cc: linux-kernel@vger.kernel.org, rjw@rjwysocki.net, mingo@kernel.org, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, mgorman@suse.de, bigeasy@linutronix.de, Will Deacon , tj@kernel.org, linux-pm@vger.kernel.org, Peter Zijlstra , Richard Weinberger , Anton Ivanov , Johannes Berg , linux-um@lists.infradead.org, Chris Zankel , Max Filippov , inux-xtensa@linux-xtensa.org, Kees Cook , Jann Horn Oleg Nesterov writes: 2> On 04/26, Eric W. Biederman wrote: >> >> static void ptrace_unfreeze_traced(struct task_struct *task) >> { >> - if (READ_ONCE(task->__state) != __TASK_TRACED) >> + if (!(READ_ONCE(task->jobctl) & JOBCTL_DELAY_WAKEKILL)) >> return; >> >> WARN_ON(!task->ptrace || task->parent != current); >> @@ -213,11 +213,10 @@ static void ptrace_unfreeze_traced(struct task_struct *task) >> * Recheck state under the lock to close this race. >> */ >> spin_lock_irq(&task->sighand->siglock); > > Now that we do not check __state = __TASK_TRACED, we need lock_task_sighand(). > The tracee can be already woken up by ptrace_resume(), but it is possible that > it didn't clear DELAY_WAKEKILL yet. Yes. The subtle differences in when __TASK_TRACED and JOBCTL_DELAY_WAKEKILL are cleared are causing me some minor issues. This "WARN_ON(!task->ptrace || task->parent != current);" also now needs to be inside siglock, because the __TASK_TRACED is insufficient. > Now, before we take ->siglock, the tracee can exit and another thread can do > wait() and reap this task. > > Also, I think the comment above should be updated. I agree, it makes sense to > re-check JOBCTL_DELAY_WAKEKILL under siglock just for clarity, but we no longer > need to do this to close the race; jobctl &= ~JOBCTL_DELAY_WAKEKILL and > wake_up_state() are safe even if JOBCTL_DELAY_WAKEKILL was already > cleared. I think you are right about it being safe, but I am having a hard time convincing myself that is true. I want to be very careful sending __TASK_TRACED wake_ups as ptrace_stop fundamentally can't handle spurious wake_ups. So I think adding task_is_traced to the test to verify the task is still frozen. static void ptrace_unfreeze_traced(struct task_struct *task) { unsigned long flags; /* * Verify the task is still frozen before unfreezing it, * ptrace_resume could have unfrozen us. */ if (lock_task_sighand(task, &flags)) { if ((task->jobctl & JOBCTL_DELAY_WAKEKILL) && task_is_traced(task)) { task->jobctl &= ~JOBCTL_DELAY_WAKEKILL; if (__fatal_signal_pending(task)) wake_up_state(task, __TASK_TRACED); } unlock_task_sighand(task, &flags); } } >> @@ -2307,6 +2307,7 @@ static int ptrace_stop(int exit_code, int why, int clear_code, >> >> /* LISTENING can be set only during STOP traps, clear it */ >> current->jobctl &= ~JOBCTL_LISTENING; >> + current->jobctl &= ~JOBCTL_DELAY_WAKEKILL; > > minor, but > > current->jobctl &= ~(JOBCTL_LISTENING | JOBCTL_DELAY_WAKEKILL); > > looks better. Yes. Eric _______________________________________________ linux-um mailing list linux-um@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-um