[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <87v85bow1f.fsf@yhuang6-desk2.ccr.corp.intel.com>
Date: Mon, 25 Mar 2024 11:00:28 +0800
From: "Huang, Ying" <ying.huang@...el.com>
To: Akira Yokosawa <akiyks@...il.com>
Cc: linux-kernel@...r.kernel.org, linux-mm@...ck.org, "Paul E. McKenney"
<paulmck@...nel.org>, ryan.roberts@....com, chrisl@...nel.org
Subject: Re: Can you help us on memory barrier usage? (was Re: [PATCH v4
4/6] mm: swap: Allow storage of all mTHP orders)
Akira Yokosawa <akiyks@...il.com> writes:
> [Use Paul's reachable address in CC;
> trimmed CC list, keeping only those who have responded so far.]
Thanks a lot!
> Hello Huang,
> Let me chime in.
>
> On Fri, 22 Mar 2024 06:19:52 -0700, Huang, Ying wrote:
>> Hi, Paul,
>>
>> Can you help us on WRITE_ONCE()/READ_ONCE()/barrier() usage as follows?
>> For some example kernel code as follows,
>>
>> "
>> unsigned char x[16];
>>
>> void writer(void)
>> {
>> memset(x, 1, sizeof(x));
>> /* To make memset() take effect ASAP */
>> barrier();
>> }
>>
>> unsigned char reader(int n)
>> {
>> return READ_ONCE(x[n]);
>> }
>> "
>>
>> where, writer() and reader() may be called on 2 CPUs without any lock.
>> It's acceptable for reader() to read the written value a little later.
>> Our questions are,
>>
>> 1. because it's impossible for accessing "unsigned char" to cause
>> tearing. So, WRITE_ONCE()/READ_ONCE()/barrier() isn't necessary for
>> correctness, right?
>>
>> 2. we use barrier() and READ_ONCE() in writer() and reader(), because we
>> want to make writing take effect ASAP. Is it a good practice? Or it's
>> a micro-optimization that should be avoided?
>
> Why don't you consult Documentation/memory-barriers.txt, especially
> the section titled "COMPILER BARRIER"?
>
> TL;DR:
>
> barrier(), WRITE_ONCE(), and READ_ONCE() are compiler barriers, not
> memory barriers. They just restrict compiler optimizations and don't
> have any effect with regard to "make writing take effect ASAP".
Yes. In theory, this is absolutely correct.
My question is, in practice, will compiler barriers make CPU runs (or
sees) memory read/write instructions a little earlier via avoiding to
reorder the operations after read/write (e.g., becomes before
read/write)?
> If you have further questions, please don't hesitate to ask.
--
Best Regards,
Huang, Ying
Powered by blists - more mailing lists