Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6080993imu; Mon, 21 Jan 2019 02:54:00 -0800 (PST) X-Google-Smtp-Source: ALg8bN5Ip8Ij6Xgfz4mIIMGzRkkPNhPpTC+V4RzfjPTovfjhzVEw11zDIOUbweQ+CL2J94caWjpI X-Received: by 2002:a17:902:584:: with SMTP id f4mr30499278plf.28.1548068040837; Mon, 21 Jan 2019 02:54:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548068040; cv=none; d=google.com; s=arc-20160816; b=BQnl+TRGvw5GGpUtl7x1ZpbcbbjtJh3X0Vwif41XfBmqW4eD7hKRaR7yBkXzbuMMyW EjRWIaDilxX4+kKDqDYwQUGu7UKoMohl8cw+vGFEzJgv3S3rKfzfeW6vftgcBmsh5Rm+ 6nHLwhaB620wW5am6orJkJBil42EUBR2O3mERZPcLRZV6I00oHD9iVJfRbokK9gdvoGz +yhbEeijBvshfpqS3Jet84pc8rAzTpHKOjDYBxfqyBfYJ/Zv1Eu8WmAIdR1nUas1aiz7 hZCZ4UY9MBYqJ5QdYoOmET2aKNff4VMmZlKimMIfUFzTUFJV5+DGWjZljFvjdvwbTs41 Y6uA== 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; bh=mJMHBdkQexsknFUastnGo4BYttKPi0+5BH/HVX9RkGY=; b=xvx5sxE3EBF1LYoGez6/IvYeTFXhnmg/49bEQRLqvqL2URvcFx0YhHNozFkz3MQeoy gAu5ImgRGDfflTQ/4tHwji2csOI2IKpiqWebtZ8DJRHo61vLKb7pA881izMcwJACj3Ip rUlhQNijvBQBHHqGN4s5vuVTBwZ4gMItpfNZn15O7aCvbQC1sxsFxpmxeyK53CBX25Mz NhAglBBHUDWXnhHs60JRZKq4BusfoowiLsAFCtrzo7+p8pyd82yV2KV//zhxoHhCSe1T CtB9SiXiXG2qs/WiS52yY3a86FcwypffqAOheJwNVzNb3/NBWQ9Z7Ojqz3JHhxHNaEYZ 7G1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=NZ5N+ZGS; 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 c2si12600635plb.152.2019.01.21.02.53.45; Mon, 21 Jan 2019 02:54:00 -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; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=NZ5N+ZGS; 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 S1727743AbfAUKvb (ORCPT + 99 others); Mon, 21 Jan 2019 05:51:31 -0500 Received: from mail-ed1-f66.google.com ([209.85.208.66]:41661 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727014AbfAUKvb (ORCPT ); Mon, 21 Jan 2019 05:51:31 -0500 Received: by mail-ed1-f66.google.com with SMTP id a20so16188971edc.8 for ; Mon, 21 Jan 2019 02:51:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=mJMHBdkQexsknFUastnGo4BYttKPi0+5BH/HVX9RkGY=; b=NZ5N+ZGSC/G8G33gWeqDEKiuQjE8QtuINx4uVZ0vbFp1Njnq8Ah6M6CtxoA4yK0n5+ vmhsLlb53r4PCAInYa6f0Or2U74th9Io4hwOIEVo3StaVpqH2PedJV5FtVTmfgw9p1rt mOq1iaYIm/8oZu1A4Yl7pMdB2BzxmLc9P7WM8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=mJMHBdkQexsknFUastnGo4BYttKPi0+5BH/HVX9RkGY=; b=kDGgVLkHWC3lrvlaRMvuONuJyXd89tEeKQz5sByTNEKcWhNWq0SmOU7+nQHpBnJj1v 3jf1MLCJ/Kvb7+RRsu3nEhaCgnrjD0M431+HiVNhhghpMBBBgHMeFQWDn/cGeXHzVCkE h+AdkFDLeY1glz788sk4VwkV+pQFpTmbDMXpibdT6Fp2cl+8PYrLZI0poJ4vyi1ta2El 0NhX912WJHCPLMO5LDw6lXnNdN8gXyf6WtL+7HtfvaVoPonYF75Pz0egaCTug6se7teX Tyy22tsuO91WTXazahj3T6kudebJd8zS80grdW0n+qTcx35o7fc4XRZimb/Zu0CURzHf Xieg== X-Gm-Message-State: AJcUukeDVmdXED/Gug/pDqdcDWTFV/mnxRmoYZUjrwJf3t1gWoO27hk+ tegg+VfTG4iXrrOjCKjX74IA71h5IuYVsQ== X-Received: by 2002:a05:6402:8d2:: with SMTP id d18mr26863573edz.119.1548067889188; Mon, 21 Jan 2019 02:51:29 -0800 (PST) Received: from andrea (85.100.broadband17.iol.cz. [109.80.100.85]) by smtp.gmail.com with ESMTPSA id n16-v6sm4781867eja.6.2019.01.21.02.51.28 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 21 Jan 2019 02:51:28 -0800 (PST) Date: Mon, 21 Jan 2019 11:51:21 +0100 From: Andrea Parri To: linux-kernel@vger.kernel.org Cc: Ingo Molnar , Peter Zijlstra , "Paul E. McKenney" , Alan Stern , Will Deacon Subject: Re: [PATCH] sched: Use READ_ONCE()/WRITE_ONCE() in task_cpu()/__set_task_cpu() Message-ID: <20190121104707.GA5883@andrea> References: <20190116184218.13757-1-andrea.parri@amarulasolutions.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190116184218.13757-1-andrea.parri@amarulasolutions.com> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 16, 2019 at 07:42:18PM +0100, Andrea Parri wrote: > The smp_wmb() in move_queued_task() (c.f., __set_task_cpu()) pairs with > the composition of the dependency and the ACQUIRE in task_rq_lock(): > > 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 honour > this address dependency between loads; also, mark the store to ->cpu in > __set_task_cpu() by using WRITE_ONCE() in order to tell the compiler to > not mess/tear this (synchronizing) memory access. In the light of the recent discussion about the integration of plain accesses in the LKMM (c.f., e.g., [1] and discussion thereof), I was considering even further changes to this in order to "reinforce" the above smp_wmb(). Here's two approaches (one of): 1) replace this smp_wmb()+WRITE_ONCE() with an smp_store_release(); 2) or keep this smp_wmb()+WRITE_ONCE(), but use {WRITE,READ}_ONCE() also for the accesses to ->on_rq. What do you think? (maybe I'm just being too paranoid?) Adding Will to the Cc: ((1) should be "painless" for x86, not sure about arm64...) Andrea [1] http://lkml.kernel.org/r/20190118155638.GA24442@andrea > > Signed-off-by: Andrea Parri > Cc: Ingo Molnar > Cc: Peter Zijlstra > Cc: "Paul E. McKenney" > Cc: Alan Stern > --- > include/linux/sched.h | 4 ++-- > kernel/sched/sched.h | 4 ++-- > 2 files changed, 4 insertions(+), 4 deletions(-) > > diff --git a/include/linux/sched.h b/include/linux/sched.h > index 224666226e87b..2bb02c9635bd8 100644 > --- a/include/linux/sched.h > +++ b/include/linux/sched.h > @@ -1753,9 +1753,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/sched.h b/kernel/sched/sched.h > index d04530bf251fe..270a3333589d2 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 > -- > 2.17.1 >