Return-Path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682442436; x=1685034436; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=RL3yVFoMVMBJdUvHJ8nuXXERgfnOvLwFPUfUkW3k6H8=; b=CoQmI/4Pn5ihE+45XD93Aho4V0GM1/41dHL312UE8/jblbxe9WkZifVwklF4FVK5Ea UkMA7fo3fzOkAV4CWvsN6OiyMIyczROqawe/jNKxgcuZef2nZPzyUX/5803K1GxJ1oxw uVNIbVDmuAOud3i7aWE7q1JQhUdjZQKj83fJf9Kyt/3lgzHz5MZuy87K/ubKqwuDSlrh S8H3UPVZ5IpWiBy1T5DCRSt6OekaTXj8Tk28liNMQLL2dn3pRNI7gmzRVOqDHbXN9cmg 6mJ0SrOuHsxX/Qfd1O7bZIHpqUhFWa19XTjRag2nu2iPjMgHg5JB7woLUmQAxmQvn4Rf x3qA== Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.400.51.1.1\)) Subject: Re: Question: Why does the modification of seq in write_seqlock not require WRITE_ONCE? From: Alan Huang In-Reply-To: <25bd5e72-0d70-3268-afa4-7a6ba0febcfa@gmail.com> Date: Wed, 26 Apr 2023 01:06:57 +0800 Content-Transfer-Encoding: quoted-printable Message-Id: <6FDBABBC-7444-4189-A881-147F9D42B041@gmail.com> References: <77ACB1FE-73A2-4C0D-A4EE-67D108DA539C@gmail.com> <25bd5e72-0d70-3268-afa4-7a6ba0febcfa@gmail.com> To: Akira Yokosawa Cc: perfbook@vger.kernel.org, "Paul E. McKenney" List-ID: Hi Akira, > 2023=E5=B9=B44=E6=9C=8825=E6=97=A5 23:33=EF=BC=8CAkira Yokosawa = =E5=86=99=E9=81=93=EF=BC=9A >=20 > Hi Alan, >=20 > On Tue, 25 Apr 2023 21:52:48 +0800, Alan Huang wrote: >> Hi, >>=20 >> I noticed that the modifications of seq in write_seqlock and = write_sequnlock of Listing 9.10 use plain ++ operation. >> But as Chapter 4 (especially 4.3.4.4) says, there will be store = tearing since there are concurrent readers. >>=20 >> However, the kernel implementation of sequence lock also uses plain = ++ operation. >>=20 >> I=E2=80=99m somewhat confused. >>=20 >> Why does the modification of seq in write_seqlock not require = WRITE_ONCE? >=20 > Good question... >=20 > I don't have a straight answer, but the history of = include/linux/seqlock.h > says those plain increments predate the introduction of ACCESS_ONCE > (predecessor to READ_ONCE/WRITE_ONCE) to the Linux kernel. >=20 > The code at the time (pre v2.6.0) looked like this: >=20 > static inline void write_seqlock(seqlock_t *sl) > { > spin_lock(&sl->lock); > ++sl->sequence; > smp_wmb();=20 > }=20 >=20 > static inline void write_sequnlock(seqlock_t *sl)=20 > { > smp_wmb(); > sl->sequence++; > spin_unlock(&sl->lock); > } >=20 > static inline int write_tryseqlock(seqlock_t *sl) > { > int ret =3D spin_trylock(&sl->lock); >=20 > if (ret) { > ++sl->sequence; > smp_wmb();=20 > } > return ret; > } >=20 > The purpose of WRITE_ONCE() would be to suppress compiler optimization > of write accesses. In the code above, smp_wmb(), spin_lock(), > spin_unlock(), and spin_trylock() all imply compiler barriers. > So, there is not much room for compilers to optimize the store part > of increments. But unsigned long is at least 32 bits, here is the COMPILER BARRIER = section of Documentation/memory-barriers.txt: For example, given an architecture having 16-bit store = instructions with 7-bit immediate fields, the compiler might be tempted to use two 16-bit store-immediate instructions = to implement the following 32-bit store: p =3D 0x00010002; The kernel defines sequence as unsigned, which is at least 16 bits, I = don=E2=80=99t know if there exists an architecture having 16-bit store = instructions with 32-bit unsigned=E2=80=A6 Thanks, Alan >=20 > I think those plain-looking increments are safe as far as current > compilers are concerned. >=20 > Does this explanation help you? >=20 > Paul, please chime in if I'm missing something. >=20 > Thanks, Akira >=20 >>=20 >> Thanks, >> Alan