tech-kern archive

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

__{read,write}_once



There are cases in the kernel where we read/write global memory locklessly,
and accept the races either because it is part of the design (eg low-level
scheduling) or we simply don't care (eg global stats).

In these cases, we want to access the memory only once, and need to ensure
the compiler does not split that access in several pieces, some of which
may be changed by concurrent accesses. There is a Linux article [1] about
this, and also [2]. I'd like to introduce the following macros:

	__read_once(x)
	__write_once(x, val)

These macros:

 - Are supposed to ensure that reads/writes result in only one access. We
   basically just cast to volatile for now.

 - Serve as markers for KCSAN, to say "yes there is a race, but it's
   expected and you don't need to care".

 - Help understanding the code, ie the reader will easily see that the area
   can legitimately race.

In short, the main role they play is markers, to inform the reader and
sanitizers.

I've made a patch [3], with several changes already to use these macros.

Feel free to comment

[1] https://lwn.net/Articles/508991/
[2] https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE
[3] https://m00nbsd.net/garbage/kcsan/access-once.diff


Home | Main Index | Thread Index | Old Index