ACCESS_ONCE() and compiler bugs

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mounter625

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值