Re: Can you help us on memory barrier usage? (was Re: [PATCH v4 4/6] mm: swap: Allow storage of all mTHP orders)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Mar 23, 2024 at 11:11:09AM +0900, Akira Yokosawa wrote:
> [Use Paul's reachable address in CC;
>  trimmed CC list, keeping only those who have responded so far.]
> 
> 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.

What are your consistency requirements?  For but one example, if reader(3)
gives the new value, is it OK for a later call to reader(2) to give the
old value?

Until we know what your requirements are, it is hard to say whether the
above code meets those requirements.  In the meantime, I can imagine
requirements that it meets and others that it does not.

Also, Akira's points below are quite important.

							Thanx, Paul

> > 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".
> 
> If you have further questions, please don't hesitate to ask.
> 
> Regards,
> Akira (a LKMM Reveiwer).
> 
> > 
> > --
> > Best Regards,
> > Huang, Ying
> 




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux