diff options
author | Boqun Feng <boqun.feng@gmail.com> | 2015-12-28 23:18:46 -0500 |
---|---|---|
committer | Paul E. McKenney <paulmck@linux.vnet.ibm.com> | 2016-02-23 22:59:53 -0500 |
commit | ad315455d396a1cbcb2f9fdd687b7e1b26b789e7 (patch) | |
tree | ec1d39c7dda0349ac5a29cbcb5c02a43532dd61d | |
parent | 1914aab54319ed70608078df4f18ac4767753977 (diff) |
sparse: Add __private to privatize members of structs
In C programming language, we don't have a easy way to privatize a
member of a structure. However in kernel, sometimes there is a need to
privatize a member in case of potential bugs or misuses.
Fortunately, the noderef attribute of sparse is a way to privatize a
member, as by defining a member as noderef, the address-of operator on
the member will produce a noderef pointer to that member, and if anyone
wants to dereference that kind of pointers to read or modify the member,
sparse will yell.
Based on this, __private modifier and related operation ACCESS_PRIVATE()
are introduced, which could help detect undesigned public uses of
private members of structs. Here is an example of sparse's output if it
detect an undersigned public use:
| kernel/rcu/tree.c:4453:25: warning: incorrect type in argument 1 (different modifiers)
| kernel/rcu/tree.c:4453:25: expected struct raw_spinlock [usertype] *lock
| kernel/rcu/tree.c:4453:25: got struct raw_spinlock [noderef] *<noident>
Also, this patch improves compiler.h a little bit by adding comments for
"#else" and "#endif".
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
-rw-r--r-- | include/linux/compiler.h | 12 | ||||
-rwxr-xr-x | scripts/checkpatch.pl | 3 |
2 files changed, 10 insertions, 5 deletions
diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 00b042c49ccd..c845356952bb 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h | |||
@@ -20,12 +20,14 @@ | |||
20 | # define __pmem __attribute__((noderef, address_space(5))) | 20 | # define __pmem __attribute__((noderef, address_space(5))) |
21 | #ifdef CONFIG_SPARSE_RCU_POINTER | 21 | #ifdef CONFIG_SPARSE_RCU_POINTER |
22 | # define __rcu __attribute__((noderef, address_space(4))) | 22 | # define __rcu __attribute__((noderef, address_space(4))) |
23 | #else | 23 | #else /* CONFIG_SPARSE_RCU_POINTER */ |
24 | # define __rcu | 24 | # define __rcu |
25 | #endif | 25 | #endif /* CONFIG_SPARSE_RCU_POINTER */ |
26 | # define __private __attribute__((noderef)) | ||
26 | extern void __chk_user_ptr(const volatile void __user *); | 27 | extern void __chk_user_ptr(const volatile void __user *); |
27 | extern void __chk_io_ptr(const volatile void __iomem *); | 28 | extern void __chk_io_ptr(const volatile void __iomem *); |
28 | #else | 29 | # define ACCESS_PRIVATE(p, member) (*((typeof((p)->member) __force *) &(p)->member)) |
30 | #else /* __CHECKER__ */ | ||
29 | # define __user | 31 | # define __user |
30 | # define __kernel | 32 | # define __kernel |
31 | # define __safe | 33 | # define __safe |
@@ -44,7 +46,9 @@ extern void __chk_io_ptr(const volatile void __iomem *); | |||
44 | # define __percpu | 46 | # define __percpu |
45 | # define __rcu | 47 | # define __rcu |
46 | # define __pmem | 48 | # define __pmem |
47 | #endif | 49 | # define __private |
50 | # define ACCESS_PRIVATE(p, member) ((p)->member) | ||
51 | #endif /* __CHECKER__ */ | ||
48 | 52 | ||
49 | /* Indirect macros required for expanded argument pasting, eg. __LINE__. */ | 53 | /* Indirect macros required for expanded argument pasting, eg. __LINE__. */ |
50 | #define ___PASTE(a,b) a##b | 54 | #define ___PASTE(a,b) a##b |
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 0147c91fa549..874132b26d23 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -269,7 +269,8 @@ our $Sparse = qr{ | |||
269 | __init_refok| | 269 | __init_refok| |
270 | __kprobes| | 270 | __kprobes| |
271 | __ref| | 271 | __ref| |
272 | __rcu | 272 | __rcu| |
273 | __private | ||
273 | }x; | 274 | }x; |
274 | our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; | 275 | our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; |
275 | our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; | 276 | our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; |