From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 B247B13212A for ; Wed, 18 Dec 2024 16:10:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734538209; cv=none; b=NE2e1HGino8aftLbckO6NRQz1lDHrnUVJqvAy852Wgfn3F5VyhAa5QcbLOr9xjV7JfRjtbnUuFgckVH9EN2hKgOKd3uSQAyhGhCYmjJt4qDjUGWnqlcl7VKSZLls9CM6QK9oaILbmmIQH8ejeMFDLAvlK+EZcnTBehINKiJC8Wk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734538209; c=relaxed/simple; bh=VZcVTbnZEmVXHAFf2Y/mFG1R6VjecqLUDkgaAzdveGA=; h=From:Message-ID:Date:MIME-Version:Subject:To:Cc:References: In-Reply-To:Content-Type; b=lI+V55g+q6BXIHO5K0aufNCbTp2kWPPG5SlqbnCsYo2qYZxtYxKYnSC1BylxLYW2dldEJ1AWKn29s5ohuxc1m1qc/jBD44YcV1mWU4fSmjiegxMImLqrjaC3rmZqThPr0VNJlQBYbZHTLLdBKvmEXax/2fs5tld+979vs3H1B4w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=UnkxIMZv; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="UnkxIMZv" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734538206; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=G3Aq1ruLN6rVGPpI6BVUv091KcReTUk1KEX3lB5LuFc=; b=UnkxIMZvV1kVWEAnpV8ioU1pLaWeZCQLE3TRhkvqWMwrSBsLgVacMwLW2PKP3u5kNcD4ss feOBYYaL/Uz1l2aHtS531NVwQrkZWtCSXZNid9zkey5EdgtVDaEawS5yMuv1lIb4Rn/2ss b+gO+88bYDlfuwwgfNbaHn2FofNNPWo= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-196-suOOAIEZPNyejiwyeCZ8AA-1; Wed, 18 Dec 2024 11:10:05 -0500 X-MC-Unique: suOOAIEZPNyejiwyeCZ8AA-1 X-Mimecast-MFC-AGG-ID: suOOAIEZPNyejiwyeCZ8AA Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-7b6f943f5abso266882785a.3 for ; Wed, 18 Dec 2024 08:10:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734538204; x=1735143004; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:user-agent:mime-version:date:message-id:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=G3Aq1ruLN6rVGPpI6BVUv091KcReTUk1KEX3lB5LuFc=; b=hKqvFIWE2VamxZvOMKxCgBtzKeRjgZxMoQPjwxkf14IUt/fvbwCsRWyoMkZjCGIPTW ur3tlrjPuSMcABiks54QGKx0O4SkAXC0RT8ofMMQirP/goNomSKJhHLDKw/zDGQialxy BAPIAmtXtKN1NKuNymwrDIvDG8dcSmnlV2KzKGlagTZF7dMeLevJHvMMCYJz5pmupINu mTFYjNRaYvhekau0Ji5n/zPDI4bm/2dsEnDhUc1SOgptNa09Wn5ANc5CK1oOL4ZJ8lZA 5msdCKA+2htrIImyECSN3NgqsiHhXk81lTkxEU5BNvBw59ql4wRH3z3GlUCwlvwQxlRl H6lw== X-Forwarded-Encrypted: i=1; AJvYcCV5JGFAeGyEc2Nguipj8t8dwrpjLZiysybrZPmwEoOOAdC+XyL41YXBxKzdSimNodGMHcPnkmKDs9T8KKI=@vger.kernel.org X-Gm-Message-State: AOJu0YwANny0ggTYneWtJubF4YI6Mk1vFb79AZntZW7NdX5cDf3wC5D6 F2dvTM/XPvnDYoov9MtQJ3Ic7bAoBxlBIqaWgFmHReUpWMpn+q0N6JnO7hzcd6NVShkOkz0pqu3 CdVbb0dEnLoWYD2G0NQSvjwM8Vwe0IXqASOX//HPGgQKlkRT7pY7iMmBK/RT1c+3j2PKueA== X-Gm-Gg: ASbGncvsKl4pqihJ6tD0dReoeejSfZlfMPuPUU1b0/QiBnGjUNHzI5ziUYwxZ5UI+68 R1UESV83X0haK+mJKayqb4h40rc8Z0HE7wrKe++YSZWZOHA/Xlmhsc8F6sfss/tvcBEGOhZVKoq 7omM5bXkm8VhQsPhfVCrQv1+Xojlw6Y+7pzTQL5ygf8SWEKxJw8yVujOaDhk0/X06Z/hyjxVZRh rT9uLNMweMxfQ0n11vFKbzDXKiU25TSnLTiRx4+6FqrkQ+/5JFfJ+LCDFh5h6aImJGS7QxCYf69 EzM6PE3jkHYV0QtMV0nNyzlK X-Received: by 2002:a05:620a:4607:b0:7b1:3bf5:11f8 with SMTP id af79cd13be357-7b86375acc2mr552301485a.25.1734538204051; Wed, 18 Dec 2024 08:10:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IFzs/CI/6KkhGFeGlCGeGt9+hmGVyheT07z8Wbg5/sf7f8daFsjB8V1XG/6CWTwCc57RTLt8Q== X-Received: by 2002:a05:620a:4607:b0:7b1:3bf5:11f8 with SMTP id af79cd13be357-7b86375acc2mr552296785a.25.1734538203763; Wed, 18 Dec 2024 08:10:03 -0800 (PST) Received: from ?IPV6:2601:188:ca00:a00:f844:fad5:7984:7bd7? ([2601:188:ca00:a00:f844:fad5:7984:7bd7]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7b7047d4a2bsm439549785a.41.2024.12.18.08.10.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 18 Dec 2024 08:10:02 -0800 (PST) From: Waiman Long X-Google-Original-From: Waiman Long Message-ID: <8da9c55b-65fb-4c81-b6ea-f80892e3cff7@redhat.com> Date: Wed, 18 Dec 2024 11:10:01 -0500 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] seqlock: Use WRITE_ONCE() when updating sequence To: paulmck@kernel.org, Waiman Long Cc: Daniel Xu , mingo@redhat.com, will@kernel.org, peterz@infradead.org, boqun.feng@gmail.com, linux-kernel@vger.kernel.org References: <0eaea03ecc9df536649763cfecda356fc38b6938.1734477414.git.dxu@dxuuu.xyz> <6fbd26a5-615f-41bf-ad54-91114898cc2c@redhat.com> <21914c89-4f4a-4e09-97de-4d5b99a48243@paulmck-laptop> Content-Language: en-US In-Reply-To: <21914c89-4f4a-4e09-97de-4d5b99a48243@paulmck-laptop> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 12/18/24 10:45 AM, Paul E. McKenney wrote: > On Tue, Dec 17, 2024 at 10:30:39PM -0500, Waiman Long wrote: >> On 12/17/24 6:17 PM, 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); >>> } >> The init function certainly doesn't need to use WRITE_ONCE(). >>> #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(); >>> } >>> @@ -426,7 +426,7 @@ do { \ >>> static inline void do_raw_write_seqcount_end(seqcount_t *s) >>> { >>> smp_wmb(); >>> - s->sequence++; >>> + WRITE_ONCE(s->sequence, READ_ONCE(s->sequence) + 1); >>> kcsan_nestable_atomic_end(); >>> } >>> @@ -548,9 +548,9 @@ static inline void do_write_seqcount_end(seqcount_t *s) >>> static inline void do_raw_write_seqcount_barrier(seqcount_t *s) >>> { >>> kcsan_nestable_atomic_begin(); >>> - s->sequence++; >>> + WRITE_ONCE(s->sequence, READ_ONCE(s->sequence) + 1); >>> smp_wmb(); >>> - s->sequence++; >>> + WRITE_ONCE(s->sequence, READ_ONCE(s->sequence) + 1); >>> kcsan_nestable_atomic_end(); >>> } >>> @@ -569,7 +569,7 @@ static inline void do_write_seqcount_invalidate(seqcount_t *s) >>> { >>> smp_wmb(); >>> kcsan_nestable_atomic_begin(); >>> - s->sequence+=2; >>> + WRITE_ONCE(s->sequence, READ_ONCE(s->sequence) + 2); >>> kcsan_nestable_atomic_end(); >>> } >>> @@ -673,7 +673,7 @@ read_seqcount_latch_retry(const seqcount_latch_t *s, unsigned start) >>> static __always_inline void raw_write_seqcount_latch(seqcount_latch_t *s) >>> { >>> smp_wmb(); /* prior stores before incrementing "sequence" */ >>> - s->seqcount.sequence++; >>> + WRITE_ONCE(s->seqcount.sequence, READ_ONCE(s->seqcount.sequence) + 1); >>> smp_wmb(); /* increment "sequence" before following stores */ >>> } >> For seqcount, its actual value isn't important. What is important is whether >> the value changes and whether it is even or odd. So even if store tearing is >> happening, it shouldn't affect its operation. I doubt we need to use >> WRITE_ONCE() here. Could you come up with a scenario where store tearing >> will make it behave incorrectly? > But why expand the state space? I don't quite get what you mean by "state space". > > Also, there are potentially "optimizations" other than store tearing. > No, I haven't seen them yet, but then again, there were a great many > optimizations that were not being used back when I started coding C. All the inc's are bounded by smp_wmb()'s which should limit the kind of optimizations that can be done by the compiler. That is why I don't see a need to use WRITE_ONCE() here. Of course, there may be some corner cases that I may miss. So I ask for whether such a case exists. Cheers, Longman