kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* Why do we need READ_ONCE() in list_empty()?
@ 2022-04-28 15:00 Karim Manaouil
  2022-05-01 11:43 ` Valentin Vidić
  0 siblings, 1 reply; 2+ messages in thread
From: Karim Manaouil @ 2022-04-28 15:00 UTC (permalink / raw)
  To: kernelnewbies


[-- Attachment #1.1: Type: text/plain, Size: 795 bytes --]

Hi,

list_empty() uses READ_ONCE() to read head->next [1]. AFAIK, READ_ONCE() is
a compile-time memory
barrier to prevent the compiler from reordering the read with the previous
or the following memory accesses.

I checked commit id 1658d35ead5d8dd from its committer Paul E. McKenney
when such a thing was added
to the kernel, to seek for an explanation, but the commit description is
not very insightful. It only says that this
macro might be accessed without the protection of a lock and thus the need
for READ_ONCE.

I still don't understand why READ_ONCE() is needed in list_empty(). Is
there a concrete example where not
wrapping it inside READ_ONCE() could lead to inconsistency/incorrectness?

[1]
https://elixir.bootlin.com/linux/v5.14.11/source/include/linux/list.h#L282

Cheers
Karim

[-- Attachment #1.2: Type: text/html, Size: 1084 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Why do we need READ_ONCE() in list_empty()?
  2022-04-28 15:00 Why do we need READ_ONCE() in list_empty()? Karim Manaouil
@ 2022-05-01 11:43 ` Valentin Vidić
  0 siblings, 0 replies; 2+ messages in thread
From: Valentin Vidić @ 2022-05-01 11:43 UTC (permalink / raw)
  To: kernelnewbies

On Thu, Apr 28, 2022 at 04:00:15PM +0100, Karim Manaouil wrote:
> list_empty() uses READ_ONCE() to read head->next [1]. AFAIK, READ_ONCE() is
> a compile-time memory
> barrier to prevent the compiler from reordering the read with the previous
> or the following memory accesses.
> 
> I checked commit id 1658d35ead5d8dd from its committer Paul E. McKenney
> when such a thing was added
> to the kernel, to seek for an explanation, but the commit description is
> not very insightful. It only says that this
> macro might be accessed without the protection of a lock and thus the need
> for READ_ONCE.
> 
> I still don't understand why READ_ONCE() is needed in list_empty(). Is
> there a concrete example where not
> wrapping it inside READ_ONCE() could lead to inconsistency/incorrectness?

list_empty is inline so it will be included in many different functions
in the kernel and it is possible that cases like this might happen:

https://lkml.iu.edu/hypermail/linux/kernel/1602.3/03297.html

Also see https://www.kernel.org/doc/Documentation/memory-barriers.txt
and
https://mirrors.edge.kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook.html
for more examples of READ_ONCE.

If this does not help you can always CC the patch author and bug
reporter for more info :)

-- 
Valentin

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-05-01 11:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-04-28 15:00 Why do we need READ_ONCE() in list_empty()? Karim Manaouil
2022-05-01 11:43 ` Valentin Vidić

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).