Why do we need READ_ONCE() in list_empty()?

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

 



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
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]

  Powered by Linux