Received: by 10.192.165.148 with SMTP id m20csp3693848imm; Mon, 30 Apr 2018 04:57:03 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrbSNHJoAA3kbNqOnWzY7BiORCCUd/JUUN541p0Vi7NaJJir1CV0N0mU5AOnS4FwoX0D/Al X-Received: by 10.98.254.17 with SMTP id z17mr11731309pfh.105.1525089423036; Mon, 30 Apr 2018 04:57:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525089423; cv=none; d=google.com; s=arc-20160816; b=YWtKnKRyKU4F5LGOtUA84qUd8FccXkaoAzHiwws1VG2L5glJvHJ4FDHKr9s9JzwQDd Sbf68HIDySq+IlC/QlhGY+hKZeGJKHwBFsa4sllWiwD5dRG0H65cCnYHYteZyitvOibX d+rwoIktyHyGOhtJ5MdJJ/TqFdFwW19yS8Vtd+LYtM9H1EOiXRizBfQVux3s1g6sgYHL gRlZq4UTd9AZP57MVvAf31tXBPPaMjru/tFX8oG4Y2DhJv/lRSyDOViqV2Mk40IFHgBO fg+4XWImSzSt6bjo7chOTro5SIpqVBH64CsT6W5ZL+aiGptEUKUzGzckFm3Frn944sQf lpKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=s2izkYdEFx2RABW6lq43Nn3x9Qci0UKPuGTLE4eVBLo=; b=x5NsGPkFYOjB7qHcHKmGgSaV3thBFlnGPXXkMuPQmDAsbxI3qScJwJoI0dSOhquKLw tg55Zu5E/GdOvJjUZMNBIzWHVwhBCCj7lQCUsKFTp6oA2C7L4Jpb8ZeaJDzhjSlhg9Y/ hUT36RY9X3N7WNi1bh5DVzoIi+YDVBnWFvaKBNXIJ4guld+Es+DBez0r+83xwih5C1D3 ZtMz4CfpLkzRHFf/vAbfT2GAvA5oM6EX2sUtD60kixJtSlDWhjDQ/wgxmBQP0lFKBpgy pt3jz8GecAUIvea6QpwMdUzpY1xtGizlJLrpotJeHf3BTvzNvU0lWKgH7gzxrhWnLCiN fmZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=BOO8kMjZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b1-v6si7135166plc.403.2018.04.30.04.56.49; Mon, 30 Apr 2018 04:57:03 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=BOO8kMjZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752902AbeD3L43 (ORCPT + 99 others); Mon, 30 Apr 2018 07:56:29 -0400 Received: from merlin.infradead.org ([205.233.59.134]:33644 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751606AbeD3L41 (ORCPT ); Mon, 30 Apr 2018 07:56:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=s2izkYdEFx2RABW6lq43Nn3x9Qci0UKPuGTLE4eVBLo=; b=BOO8kMjZq45XH51EqZK+WdfHB nZR/zfk7cTI61byP5ls/OxXAG6jU5+RvIdt5/2y1fokG7JZ/ne5yrt19AwkxT1T87neHSaBPb32Pu wLmKwKziSXYbHSK11yuzwqRdUWgKJ8LGroWXmHQpsAUaDpw42G1Ko1dhd+K7AAsyS4deSOyBxtIt9 VLuzAAsbgY+bskudPuBb3hgCOptet7/2LHvI2rY5aBKs4Fl5mwiVHjrDu5OKPg1CFN2z3kd47fIye IJTsC73CrGcwHn9UHkCyNXlKZkWthIELstrc6qICO5+ot4ULShUfGfG+++B8s11O/1seknzm0HuAg mxLd8jbUw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1fD7Pj-000512-Uv; Mon, 30 Apr 2018 11:56:16 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 443A82029F873; Mon, 30 Apr 2018 13:56:12 +0200 (CEST) Date: Mon, 30 Apr 2018 13:56:12 +0200 From: Peter Zijlstra To: Oleg Nesterov Cc: Gaurav Kohli , tglx@linutronix.de, mpe@ellerman.id.au, mingo@kernel.org, bigeasy@linutronix.de, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Neeraj Upadhyay , Will Deacon Subject: Re: [PATCH v1] kthread/smpboot: Serialize kthread parking against wakeup Message-ID: <20180430115612.GX4129@hirez.programming.kicks-ass.net> References: <1524645199-5596-1-git-send-email-gkohli@codeaurora.org> <20180425200917.GZ4082@hirez.programming.kicks-ass.net> <20180426084131.GV4129@hirez.programming.kicks-ass.net> <20180426161820.GA15391@redhat.com> <20180430112029.GF4082@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180430112029.GF4082@hirez.programming.kicks-ass.net> User-Agent: Mutt/1.9.3 (2018-01-21) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 30, 2018 at 01:20:29PM +0200, Peter Zijlstra wrote: > On Thu, Apr 26, 2018 at 06:18:20PM +0200, Oleg Nesterov wrote: > > On 04/26, Peter Zijlstra wrote: > > > > > > For the others, I think we want to do something like the below. I still > > > need to look at TASK_TRACED, which I suspect is also special, > > > > ptrace_freeze_traced() and ptrace_unfreeze_traced() should be fine, but > > ptrace_stop() wants set_special_state() too, I think. > > Thanks for going through that, I'll update the patch. Something like so then... I'll go write me some Changelog. diff --git a/include/linux/sched.h b/include/linux/sched.h index b3d697f3b573..51f46704e566 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -112,17 +112,38 @@ struct task_group; #ifdef CONFIG_DEBUG_ATOMIC_SLEEP +/* + * Special states are those that do not use the normal wait-loop pattern. See + * the comment with set_special_state(). + */ +#define is_special_task_state(state) \ + ((state) == TASK_DEAD || \ + (state) == TASK_STOPPED || \ + (state) == TASK_TRACED) + #define __set_current_state(state_value) \ do { \ + WARN_ON_ONCE(is_special_task_state(state)); \ current->task_state_change = _THIS_IP_; \ current->state = (state_value); \ } while (0) + #define set_current_state(state_value) \ do { \ + WARN_ON_ONCE(is_special_task_state(state)); \ current->task_state_change = _THIS_IP_; \ smp_store_mb(current->state, (state_value)); \ } while (0) +#define set_special_state(state_value) \ + do { \ + unsigned long flags; /* may shadow */ \ + WARN_ON_ONCE(!is_special_task_state(state_value)); \ + raw_spin_lock_irqsave(¤t->pi_lock, flags); \ + current->task_state_change = _THIS_IP_; \ + current->state = (state_value); \ + raw_spin_unlock_irqrestore(¤t->pi_lock, flags); \ + } while (0) #else /* * set_current_state() includes a barrier so that the write of current->state @@ -144,8 +165,8 @@ struct task_group; * * The above is typically ordered against the wakeup, which does: * - * need_sleep = false; - * wake_up_state(p, TASK_UNINTERRUPTIBLE); + * need_sleep = false; + * wake_up_state(p, TASK_UNINTERRUPTIBLE); * * Where wake_up_state() (and all other wakeup primitives) imply enough * barriers to order the store of the variable against wakeup. @@ -154,12 +175,33 @@ struct task_group; * once it observes the TASK_UNINTERRUPTIBLE store the waking CPU can issue a * TASK_RUNNING store which can collide with __set_current_state(TASK_RUNNING). * - * This is obviously fine, since they both store the exact same value. + * However, with slightly different timing the wakeup TASK_RUNNING store can + * also collide with the TASK_UNINTERRUPTIBLE store. Loosing that store is not + * a problem either because that will result in one extra go around the loop + * and our @cond test will save the day. * * Also see the comments of try_to_wake_up(). */ -#define __set_current_state(state_value) do { current->state = (state_value); } while (0) -#define set_current_state(state_value) smp_store_mb(current->state, (state_value)) +#define __set_current_state(state_value) \ + current->state = (state_value) + +#define set_current_state(state_value) \ + smp_store_mb(current->state, (state_value)) + +/* + * set_special_state() should be used for those states when the blocking task + * can not use the regular condition based wait-loop. In that case we must + * serialize against wakeups such that any possible in-flight TASK_RUNNING stores + * will not collide with our state change. + */ +#define set_special_state(state_value) \ + do { \ + unsigned long flags; /* may shadow */ \ + raw_spin_lock_irqsave(¤t->pi_lock, flags); \ + current->state = (state_value); \ + raw_spin_unlock_irqrestore(¤t->pi_lock, flags); \ + } while (0) + #endif /* Task command name length: */ diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index a7ce74c74e49..113d1ad1ced7 100644 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -280,7 +280,7 @@ static inline void kernel_signal_stop(void) { spin_lock_irq(¤t->sighand->siglock); if (current->jobctl & JOBCTL_STOP_DEQUEUED) - __set_current_state(TASK_STOPPED); + set_special_state(TASK_STOPPED); spin_unlock_irq(¤t->sighand->siglock); schedule(); diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 5e10aaeebfcc..3898a8047c11 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3498,23 +3498,8 @@ static void __sched notrace __schedule(bool preempt) void __noreturn do_task_dead(void) { - /* - * The setting of TASK_RUNNING by try_to_wake_up() may be delayed - * when the following two conditions become true. - * - There is race condition of mmap_sem (It is acquired by - * exit_mm()), and - * - SMI occurs before setting TASK_RUNINNG. - * (or hypervisor of virtual machine switches to other guest) - * As a result, we may become TASK_RUNNING after becoming TASK_DEAD - * - * To avoid it, we have to wait for releasing tsk->pi_lock which - * is held by try_to_wake_up() - */ - raw_spin_lock_irq(¤t->pi_lock); - raw_spin_unlock_irq(¤t->pi_lock); - /* Causes final put_task_struct in finish_task_switch(): */ - __set_current_state(TASK_DEAD); + set_special_state(TASK_DEAD); /* Tell freezer to ignore us: */ current->flags |= PF_NOFREEZE; diff --git a/kernel/signal.c b/kernel/signal.c index d4ccea599692..564ee214e80a 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1968,7 +1968,7 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info) * atomic with respect to siglock and should be done after the arch * hook as siglock is released and regrabbed across it. */ - set_current_state(TASK_TRACED); + set_special_state(TASK_TRACED); current->last_siginfo = info; current->exit_code = exit_code; @@ -2176,7 +2176,7 @@ static bool do_signal_stop(int signr) if (task_participate_group_stop(current)) notify = CLD_STOPPED; - __set_current_state(TASK_STOPPED); + set_special_state(TASK_STOPPED); spin_unlock_irq(¤t->sighand->siglock); /*