Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5644733img; Wed, 27 Mar 2019 12:19:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqyzChbMOytgh8JzOaJZSRssWUkPQ+HRf7feFCXiUGXMicOebuuCBvdjDtV5UoKPkJxvrvh8 X-Received: by 2002:a62:1e06:: with SMTP id e6mr36857967pfe.168.1553714394574; Wed, 27 Mar 2019 12:19:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553714394; cv=none; d=google.com; s=arc-20160816; b=hdiXNUorZb84AUMAuvjye0AhK7xLW7EnhK1DydhVfNr21w8JRnlx/WxX5is7ojZalW aOpWbXaVx5FZ1CQB2s0E3vV4mfkF5M7WHahHONftQj4VcxWV+ibFEHVK4ntf7misL3i7 anoVkUhXqBv1/LFP+9fMrCyZA9n0ZlUl3iyNLJ02IK+W3KB1YdOy6U+ppDneLOcvq6Ga xLY1pe1W0ZjzDbfZbRblXwwBMWjT1fyMzzy3ZV2VbWJ452opkOFgadLURzPhuRXAtcP0 V8jezBYwAvjiAGjDc5LALDiAP2hYPGCFyKVlQdJAHpV06/K2HbZw0EMtUgDKm8VOR3q4 JOhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=xfp2NAFizyk70m9MszTMllIRSUeFI3pXrEN4JxTgWZI=; b=ivw0c5b3D20iPIHgWhKVmmAYY1nf9hRp/0mmmKRFlAYbcOfaPwhWU6pduHMLHcRUKJ gyDDr/NF27jB/KwEUotOY2aEb0tZHiV90XKbMpufqNsIzsBaiqVD4M6H1amhNFKSNl7k RAulWVbtBf3bNOV0JRLkgDUaYfB/iuC51YWd1Eq0O9piI2f1OyUnCYwklOoIKekiZui4 8P90Nxr0i/GBE+leT/hHNsw0BuRMUiqyfdHfSltf3lGbcB5ds00NYUhcZUj1pV+97In0 ulhe524XlJ5750qNKuVs3Qc/NZKw0YOsb4idaTfEttxlo87w3gteoEI3eMOLe9I3ILZR YECw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=B5rewxu5; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t18si18826063pgu.369.2019.03.27.12.19.39; Wed, 27 Mar 2019 12:19:54 -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=pass header.i=@kernel.org header.s=default header.b=B5rewxu5; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388398AbfC0TTG (ORCPT + 99 others); Wed, 27 Mar 2019 15:19:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:49908 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388270AbfC0SH7 (ORCPT ); Wed, 27 Mar 2019 14:07:59 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 22E9721734; Wed, 27 Mar 2019 18:07:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553710077; bh=E+cAm8yZUEoE7KUHNxaVBFmrPnH+eB+CGfQ2NtonmwE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B5rewxu5Q9Y+1qsuVOgQ8Qf7XAlqXZGCW0cUtNY0+Ls/FfYPQNLsBTkA7N4Q7Rxlp 1wuxEHDmHQYDCqupl+Lr023VSxTvWk47akoz1xJbf7YJeC7FDRXIYlPy+mmeFwozbO dZVqiHG1Aakrpdi8hum6QE3XQRwevN/LkU0tJX5I= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Andrea Parri , Peter Zijlstra , Alan Stern , Linus Torvalds , Mike Galbraith , "Paul E . McKenney" , Thomas Gleixner , Will Deacon , Ingo Molnar , Sasha Levin Subject: [PATCH AUTOSEL 5.0 187/262] sched/core: Use READ_ONCE()/WRITE_ONCE() in move_queued_task()/task_rq_lock() Date: Wed, 27 Mar 2019 14:00:42 -0400 Message-Id: <20190327180158.10245-187-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190327180158.10245-1-sashal@kernel.org> References: <20190327180158.10245-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrea Parri [ Upstream commit c546951d9c9300065bad253ecdf1ac59ce9d06c8 ] 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 Signed-off-by: Sasha Levin --- 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 f9b43c989577..9b35aff09f70 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1748,9 +1748,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 d8d76a65cfdd..01a2489de94e 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); @@ -928,7 +929,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 d04530bf251f..425a5589e5f6 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1460,9 +1460,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 @@ -1563,7 +1563,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; } /* -- 2.19.1