Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp3558716ima; Mon, 4 Feb 2019 01:03:43 -0800 (PST) X-Google-Smtp-Source: ALg8bN7NhScG+msFfLDwD6c+U6UXs/fyAea4BI0HRmkV02BnOOkd2hVh5QZa9TpzTON/rdF0D7Nh X-Received: by 2002:a17:902:bf03:: with SMTP id bi3mr50817637plb.83.1549271023160; Mon, 04 Feb 2019 01:03:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549271023; cv=none; d=google.com; s=arc-20160816; b=dOjJgjROcRTG0Qu1DFBAy+2zCXTojc7Xvybz1iV7SKU40ErYv0hzREtzg2AO2fpfsI 3RuWhxIjg63hYY9cZFIcQ0h/MobRFV9IF8UDTjotD+19SVbf18ropzfmRNLz/okxDzrc 9Yn+9gG5lXUIsE7CRnOEjuohwHOVc/usGkT8SlAysWgIAie4hDna6tqA/7m5l8orltw8 gL+f7nAATvTOZAn5eZjDxZ89szfYSKG5zxAzfOJHIn5JMFshIwBlVdrxW19KhuEcaYJ5 oikx0llXS7hPsmFZEOctwmU2fNwr97PhsdPXFRcQFinkAYRmhJEr6EnOEmCsLxSQ9x0H TV0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date; bh=5PquTjiPlcgS0MlOAKSIdYJ2UEIM0hnCV4jSGy2N+vM=; b=yfE09SZln+fuuciT5lXOgzZD8tPP51GLWjMmZNE2s7hJKudC6xCd3pM9n230rvxcsf vdSrBEboF65enm8VKxcMbwr/1VcIlDEjSBpkNhiAcTAtTAVdfrWJTYFDDwFSXnjzEkiG aRPEYKVt3noBtgoMkqhEfOeh5ZhqG/rdht0KDzWAY9BFAdj9McAfD/braVrSUCYhZ0ux YPf9LUjkOelG2U223a7DuIDhMF1S9a2rSxjTf3VbbHURmuztRVIFVlYcwfi7lcmjMbjL Z34ZlMi5dpq2DRYjqqYEwonG+baPHXgSSXTeDvD5bnff45jgACFxwpm7P0ovrCJahY29 78Eg== ARC-Authentication-Results: i=1; mx.google.com; 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 c81si15344351pfc.82.2019.02.04.01.03.27; Mon, 04 Feb 2019 01:03:43 -0800 (PST) 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; 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 S1728883AbfBDJC7 (ORCPT + 99 others); Mon, 4 Feb 2019 04:02:59 -0500 Received: from terminus.zytor.com ([198.137.202.136]:50933 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726320AbfBDJC7 (ORCPT ); Mon, 4 Feb 2019 04:02:59 -0500 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id x1492dlc358960 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Mon, 4 Feb 2019 01:02:39 -0800 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id x1492c22358957; Mon, 4 Feb 2019 01:02:38 -0800 Date: Mon, 4 Feb 2019 01:02:38 -0800 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Andrea Parri Message-ID: Cc: paulmck@linux.ibm.com, mingo@kernel.org, torvalds@linux-foundation.org, tglx@linutronix.de, efault@gmx.de, linux-kernel@vger.kernel.org, stern@rowland.harvard.edu, andrea.parri@amarulasolutions.com, hpa@zytor.com, will.deacon@arm.com, peterz@infradead.org Reply-To: tglx@linutronix.de, efault@gmx.de, paulmck@linux.ibm.com, mingo@kernel.org, torvalds@linux-foundation.org, will.deacon@arm.com, peterz@infradead.org, andrea.parri@amarulasolutions.com, linux-kernel@vger.kernel.org, stern@rowland.harvard.edu, hpa@zytor.com In-Reply-To: <20190121155240.27173-1-andrea.parri@amarulasolutions.com> References: <20190121155240.27173-1-andrea.parri@amarulasolutions.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:sched/core] sched/core: Use READ_ONCE()/WRITE_ONCE() in move_queued_task()/task_rq_lock() Git-Commit-ID: c546951d9c9300065bad253ecdf1ac59ce9d06c8 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,BAYES_00, FREEMAIL_FORGED_REPLYTO,T_DATE_IN_FUTURE_96_Q autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: c546951d9c9300065bad253ecdf1ac59ce9d06c8 Gitweb: https://git.kernel.org/tip/c546951d9c9300065bad253ecdf1ac59ce9d06c8 Author: Andrea Parri AuthorDate: Mon, 21 Jan 2019 16:52:40 +0100 Committer: Ingo Molnar CommitDate: Mon, 4 Feb 2019 09:13:21 +0100 sched/core: Use READ_ONCE()/WRITE_ONCE() in move_queued_task()/task_rq_lock() move_queued_task() synchronizes with task_rq_lock() as follows: move_queued_task() task_rq_lock() [S] ->on_rq = MIGRATING [L] rq = task_rq() WMB (__set_task_cpu()) ACQUIRE (rq->lock); [S] ->cpu = new_cpu [L] ->on_rq where "[L] rq = task_rq()" is ordered before "ACQUIRE (rq->lock)" by an address dependency and, in turn, "ACQUIRE (rq->lock)" is ordered before "[L] ->on_rq" by the ACQUIRE itself. Use READ_ONCE() to load ->cpu in task_rq() (c.f., task_cpu()) to honor this address dependency. Also, mark the accesses to ->cpu and ->on_rq with READ_ONCE()/WRITE_ONCE() to comply with the LKMM. Signed-off-by: Andrea Parri Signed-off-by: Peter Zijlstra (Intel) Cc: Alan Stern Cc: Linus Torvalds Cc: Mike Galbraith Cc: Paul E. McKenney Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Will Deacon Link: https://lkml.kernel.org/r/20190121155240.27173-1-andrea.parri@amarulasolutions.com Signed-off-by: Ingo Molnar --- include/linux/sched.h | 4 ++-- kernel/sched/core.c | 9 +++++---- kernel/sched/sched.h | 6 +++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 351c0fe64c85..4112639c2a85 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1745,9 +1745,9 @@ static __always_inline bool need_resched(void) static inline unsigned int task_cpu(const struct task_struct *p) { #ifdef CONFIG_THREAD_INFO_IN_TASK - return p->cpu; + return READ_ONCE(p->cpu); #else - return task_thread_info(p)->cpu; + return READ_ONCE(task_thread_info(p)->cpu); #endif } diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 32e06704565e..ec1b67a195cc 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -107,11 +107,12 @@ struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf) * [L] ->on_rq * RELEASE (rq->lock) * - * If we observe the old CPU in task_rq_lock, the acquire of + * If we observe the old CPU in task_rq_lock(), the acquire of * the old rq->lock will fully serialize against the stores. * - * If we observe the new CPU in task_rq_lock, the acquire will - * pair with the WMB to ensure we must then also see migrating. + * If we observe the new CPU in task_rq_lock(), the address + * dependency headed by '[L] rq = task_rq()' and the acquire + * will pair with the WMB to ensure we then also see migrating. */ if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) { rq_pin_lock(rq, rf); @@ -916,7 +917,7 @@ static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf, { lockdep_assert_held(&rq->lock); - p->on_rq = TASK_ON_RQ_MIGRATING; + WRITE_ONCE(p->on_rq, TASK_ON_RQ_MIGRATING); dequeue_task(rq, p, DEQUEUE_NOCLOCK); set_task_cpu(p, new_cpu); rq_unlock(rq, rf); diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 99e2a7772d16..c688ef5012e5 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1479,9 +1479,9 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) */ smp_wmb(); #ifdef CONFIG_THREAD_INFO_IN_TASK - p->cpu = cpu; + WRITE_ONCE(p->cpu, cpu); #else - task_thread_info(p)->cpu = cpu; + WRITE_ONCE(task_thread_info(p)->cpu, cpu); #endif p->wake_cpu = cpu; #endif @@ -1582,7 +1582,7 @@ static inline int task_on_rq_queued(struct task_struct *p) static inline int task_on_rq_migrating(struct task_struct *p) { - return p->on_rq == TASK_ON_RQ_MIGRATING; + return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING; } /*