背景
在分析Linux RCU lock的时候,发现每次在rcu_read_lock/rcu_read_unlock的时候,都会成对出现__acquire/release接口,这两个接口的定义都是__context,但是在内核中并没有找到出处。
备忘
查阅了相关资料,原来这个东西是给Linux代码静态检查工具使用的,为了保证lock/unlock必须成对出现使用的。
| 宏名称 | 宏定义 | 检查点 |
|---|---|---|
| __bitwise | attribute((bitwise)) | 确保变量是相同的位方式(比如 bit-endian, little-endiandeng) |
| __user | attribute((noderef, address_space(1))) | 指针地址必须在用户地址空间 |
| __kernel | attribute((noderef, address_space(0))) | 指针地址必须在内核地址空间 |
| __iomem | attribute((noderef, address_space(2))) | 指针地址必须在设备地址空间 |
| __safe | attribute((safe)) | 变量可以为空 |
| __force | attribute((force)) | 变量可以进行强制转换 |
| __nocast | attribute((nocast)) | 参数类型与实际参数类型必须一致 |
| __acquires(x) | attribute((context(x, 0, 1))) | 参数x 在执行前引用计数必须是0,执行后,引用计数必须为1 |
| __releases(x) | attribute((context(x, 1, 0))) | 与 __acquires(x) 相反 |
| __acquire(x) | context(x, 1) | 参数x 的引用计数 + 1 |
| __release(x) | context(x, -1) | 与 __acquire(x) 相反 |
| __cond_lock(x,c) | (© ? ({ __acquire(x); 1; }) : 0) | 参数c 不为0时,引用计数 + 1, 并返回1 |
其中 __acquires(x) 和 __releases(x), __acquire(x) 和 __release(x) 必须配对使用, 否则 Sparse 会给出警告
本文探讨了Linux内核中RCU锁的使用,特别是rcu_read_lock和rcu_read_unlock操作。这些操作与__acquire和__release宏配合,确保锁的正确配对。这些宏实际上是为Linux代码静态检查工具Sparse设计的,用于防止锁未正确配对的错误。__acquires和__releases宏确保引用计数的增减,而__acquire和__release则直接操作引用计数。如果未按对出现,Sparse工具将发出警告。
4448

被折叠的 条评论
为什么被折叠?



