The ACCESS_ONCE() macro is used throughout the kernel to ensure that code generated by the compiler will access the indicated variable once (and only once); see this article for details on how it works and when its use is necessary. When that article was written (2012), there were 200 invocations of ACCESS_ONCE() in the kernel; now there are over 700 of them. Like many low-level techniques for concurrency management, ACCESS_ONCE() relies on trickery that is best hidden from view. And, like such techniques, it may break if the compiler changes behavior or, as has been seen recently, contains a bug.
ACCESS_ONCE() 宏在整个内核中广泛使用,用于确保编译器生成的代码对指定变量的访问只发生一次(且仅一次);关于它的工作原理以及何时需要使用,可以参考这篇文章。撰写该文时(2012 年),内核中约有 200 处使用 ACCESS_ONCE(),而现在已经超过 700 处。像许多底层并发管理技术一样,ACCESS_ONCE() 依赖一些最好被隐藏的技巧。而且,和这类技术一样,如果编译器行为发生变化,或者像最近发现的情况那样出现了 bug,它也可能出问题。
Back in November, Christian Borntraeger posted a message regarding the interactions between ACCESS_ONCE() and an obscure GCC bug. To understand the problem, it is worth looking at the macro, which is defined simply in current kernels (in <linux/compiler.h>):
去年十一月,Christian Borntraeger 发布了一则消息,讨论了 ACCESS_ONCE() 与一个隐蔽的 GCC bug 之间的交互问题。为了理解这个问题,有必要看看当前内核中对该宏的定义(位于 <linux/compiler.h>):
#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
In short, ACCESS_ONCE() forces the variable to be treated as being a volatile type, even though it (like almost all variables in the kernel) is not declared that way. The problem reported by Christian