From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DBCC319E98D for ; Wed, 18 Dec 2024 10:30:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734517805; cv=none; b=TDmdagsU529AyUSccVBzslOGPDGbeW30tZ9uP/QMcN+LyzQy5zkfsAwnvAdFezxF69OpzN5TbJOA89Xda6ZYubTG0xaYBM9tKt49xZt5y3Vr4U2wBQO1mi3Je7lR1RplZgKt5ww7yTYbSRM4bPOaexvePDN2MqFUwa6i9nNl4io= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734517805; c=relaxed/simple; bh=Fq/bm5RjGpX5YYraIBq4lqN8jopLxWyWF6bQ0HHr0wg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=f0/bHKiKtdf6N0K54DtLyMKZGNc1paGXZrTfgniqE8FiWVlGF305Nf4Rghhz+P6aC9eOv0jIUf0qc3qPfxUYS35H0GoPXQ6Tij2tY0hoaVTwBhtLFDvIT4CuqMxluiLoR4cXYIF8S2w1ARTa/avjMWbxOFuDqgqDqA5Cntq+gB0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=au6l075P; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="au6l075P" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.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; bh=/b5MXvdqkBBO8O1Q8ZkH55C9OwiwQBk40mdO98cua2k=; b=au6l075PWrq6XcYJztsvdBr3SM fi54kZ5cIUL+q85mvTo/8tPI0q/XsFkbmHGjhyEfav+/1YmcQkw+Xf0Gi5ZasMOPZ5e+TqctFLQDs zocEyorW5oRoqUNdLtqYFY4+C0EA2FpmDz7QwvOCgJQS6d3Unmya95bBVb78jipFbsFgu+Rt58kW2 bOKx99gdS5XDsGvEeltAv8joAz0dp++PIUvxWOjFTQ1qgzkIAOyRrHCAV4emp4IhwXIwGf35Aiq7F MebO6svFIbcYPGpXo8BdeRYUkkendwoJv1I88Ky6OWaWHeDG9y8+O4VTvHKpa/t+2pyXnFi0Umapk tnKLux9w==; Received: from 77-249-17-89.cable.dynamic.v4.ziggo.nl ([77.249.17.89] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tNrJZ-0000000AdcI-0N7y; Wed, 18 Dec 2024 10:30:01 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id 5045E3005A5; Wed, 18 Dec 2024 11:30:00 +0100 (CET) Date: Wed, 18 Dec 2024 11:30:00 +0100 From: Peter Zijlstra To: Daniel Xu Cc: mingo@redhat.com, will@kernel.org, longman@redhat.com, boqun.feng@gmail.com, linux-kernel@vger.kernel.org, paulmck@kernel.org Subject: Re: [PATCH] seqlock: Use WRITE_ONCE() when updating sequence Message-ID: <20241218103000.GK11133@noisy.programming.kicks-ass.net> References: <0eaea03ecc9df536649763cfecda356fc38b6938.1734477414.git.dxu@dxuuu.xyz> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <0eaea03ecc9df536649763cfecda356fc38b6938.1734477414.git.dxu@dxuuu.xyz> On Tue, Dec 17, 2024 at 03:17:36PM -0800, Daniel Xu wrote: > `sequence` is a concurrently accessed shared variable on the reader > side. Therefore, it needs to be wrapped in WRITE_ONCE() in order to > prevent unwanted compiler optimizations like store tearing. > > Signed-off-by: Daniel Xu > --- > include/linux/seqlock.h | 14 +++++++------- > 1 file changed, 7 insertions(+), 7 deletions(-) > > diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h > index 5298765d6ca4..f4c6f2507742 100644 > --- a/include/linux/seqlock.h > +++ b/include/linux/seqlock.h > @@ -45,7 +45,7 @@ static inline void __seqcount_init(seqcount_t *s, const char *name, > * Make sure we are not reinitializing a held lock: > */ > lockdep_init_map(&s->dep_map, name, key, 0); > - s->sequence = 0; > + WRITE_ONCE(s->sequence, 0); > } > > #ifdef CONFIG_DEBUG_LOCK_ALLOC > @@ -405,7 +405,7 @@ do { \ > static inline void do_raw_write_seqcount_begin(seqcount_t *s) > { > kcsan_nestable_atomic_begin(); > - s->sequence++; > + WRITE_ONCE(s->sequence, READ_ONCE(s->sequence) + 1); > smp_wmb(); > } This results in significantly worse code-gen, it will change an inc to memory with a load,inc,store.