From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: Yubin Ruan <ablacktshirt@gmail.com>
Cc: perfbook@vger.kernel.org, Akira Yokosawa <akiyks@gmail.com>
Subject: Re: synchronize with a non-atomic flag
Date: Sun, 8 Oct 2017 19:14:33 -0700 [thread overview]
Message-ID: <20171009021433.GB3521@linux.vnet.ibm.com> (raw)
In-Reply-To: <20171009084009.GA5758@HP>
On Mon, Oct 09, 2017 at 04:40:11PM +0800, Yubin Ruan wrote:
> On Sun, Oct 08, 2017 at 09:07:38AM -0700, Paul E. McKenney wrote:
> > On Sun, Oct 08, 2017 at 05:12:18PM +0800, Yubin Ruan wrote:
> > > 2017-10-06 13:52 GMT+08:00 Yubin Ruan <ablacktshirt@gmail.com>:
> > > > Hi,
> > > > I saw lots of discussions on the web about possible race when doing
> > > > synchronization between multiple threads/processes with lock or atomic
> > > > operations[1][2]. From my point of view most them are over-worrying.
> > > > But I want to point out some particular issue here to see whether
> > > > anyone have anything to say.
> > > >
> > > > Imagine two processes communicate using only a uint32_t variable in
> > > > shared memory, like this:
> > > >
> > > > // uint32_t variable in shared memory
> > > > uint32_t flag = 0;
> > > >
> > > > //process 1
> > > > while(1) {
> > > > if(READ_ONCE(flag) == 0) {
> > > > do_something();
> > > > WRITE_ONCE(flag, 1); // let another process to run
> > > > } else {
> > > > continue;
> > > > }
> > > > }
> > > >
> > > > //process 2
> > > > while(1) {
> > > > if(READ_ONCE(flag) == 1) {
> > > > printf("process 2 running...\n");
> > > > WRITE_ONCE(flag, 0); // let another process to run
> > > > } else {
> > > > continue;
> > > > }
> > > > }
> > > >
> > > > On X86 or X64, I expect this code to run correctly, that is, I will
> > > > got the two `printf' to printf one after one. That is because:
> > > >
> > > > 1) on X86/X64, load/store on 32-bits variable are atomic
> > >
> > > Ah...this assumption is wrong at the first place. Atomic access on
> > > 4-bytes integers is guaranteed only when these integer is aligned on a
> > > 4-bytes memory address boundary...
> >
> > Indeed, accesses crossing cachelines normally won't guarantee you
> > much of anything other than painful debugging sessions. ;-)
>
> I see similar interfaces in the Linux kernel source[1]:
>
> #define atomic_set(v, i) ((v)->counter = (i))
> #define atomic_read(v) ((v)->counter)
>
> which set and read 'atomically' from a atomic variable, and by `atomic', they
> simply mean:
>
> The setting is atomic in that the return values of the atomic operations by
> all threads are guaranteed to be correct reflecting either the value that
> has been set with this operation or set with another operation.
>
> The read is atomic in that the return value is guaranteed to be one of the
> values initialized or modified with the interface operations if a proper
> implicit or explicit memory barrier is used after possible runtime
> initialization by any other thread and the value is modified only with the
> interface operations.
> (but still, the compare-and-swap operations still involve lock)
>
> Are those operations atomic because the `atomic_t' is defined as a struct
>
> typedef struct { int counter; } atomic_t;
>
> and therefore proper alignment and atomic attribute is guaranteed by the
> compiler and the CPU?
Yes, unless you take explicit action to force unalignment, usually
by allocating a block of memory and constructing an unaligned pointer
to the middle of it, but this is almost never a good thing to do.
> If I do something like this:
>
> atomic_t v = ATOMIC_INIT(0); // globally visible
>
> atomic_set(&v, 1); //process 1
>
> atomic_set(&v, 2); //process 2
>
> int i = atomic_read(&v); // process 3
>
> will process 3 see any intermediate value between 1 and 2?
Given this code, process 3 should see only the values 0, 1, and 2.
Thanx, Paul
> Yubin
>
> [1]: Documentation/core-api/atomic_ops.rst
>
prev parent reply other threads:[~2017-10-09 2:14 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-06 5:52 synchronize with a non-atomic flag Yubin Ruan
2017-10-06 12:03 ` Akira Yokosawa
2017-10-06 12:35 ` Yubin Ruan
2017-10-06 19:12 ` Paul E. McKenney
2017-10-07 7:04 ` Yubin Ruan
2017-10-07 11:40 ` Akira Yokosawa
2017-10-07 13:43 ` Yubin Ruan
2017-10-07 14:36 ` Akira Yokosawa
2017-10-07 20:20 ` Paul E. McKenney
2017-10-08 9:12 ` Yubin Ruan
2017-10-08 16:07 ` Paul E. McKenney
2017-10-09 8:40 ` Yubin Ruan
2017-10-09 2:14 ` Paul E. McKenney [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171009021433.GB3521@linux.vnet.ibm.com \
--to=paulmck@linux.vnet.ibm.com \
--cc=ablacktshirt@gmail.com \
--cc=akiyks@gmail.com \
--cc=perfbook@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox