xref: /OK3568_Linux_fs/kernel/Documentation/dev-tools/sparse.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. Copyright 2004 Linus Torvalds
2*4882a593Smuzhiyun.. Copyright 2004 Pavel Machek <pavel@ucw.cz>
3*4882a593Smuzhiyun.. Copyright 2006 Bob Copeland <me@bobcopeland.com>
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunSparse
6*4882a593Smuzhiyun======
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunSparse is a semantic checker for C programs; it can be used to find a
9*4882a593Smuzhiyunnumber of potential problems with kernel code.  See
10*4882a593Smuzhiyunhttps://lwn.net/Articles/689907/ for an overview of sparse; this document
11*4882a593Smuzhiyuncontains some kernel-specific sparse information.
12*4882a593SmuzhiyunMore information on sparse, mainly about its internals, can be found in
13*4882a593Smuzhiyunits official pages at https://sparse.docs.kernel.org.
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun
16*4882a593SmuzhiyunUsing sparse for typechecking
17*4882a593Smuzhiyun-----------------------------
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun"__bitwise" is a type attribute, so you have to do something like this::
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun        typedef int __bitwise pm_request_t;
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun        enum pm_request {
24*4882a593Smuzhiyun                PM_SUSPEND = (__force pm_request_t) 1,
25*4882a593Smuzhiyun                PM_RESUME = (__force pm_request_t) 2
26*4882a593Smuzhiyun        };
27*4882a593Smuzhiyun
28*4882a593Smuzhiyunwhich makes PM_SUSPEND and PM_RESUME "bitwise" integers (the "__force" is
29*4882a593Smuzhiyunthere because sparse will complain about casting to/from a bitwise type,
30*4882a593Smuzhiyunbut in this case we really _do_ want to force the conversion). And because
31*4882a593Smuzhiyunthe enum values are all the same type, now "enum pm_request" will be that
32*4882a593Smuzhiyuntype too.
33*4882a593Smuzhiyun
34*4882a593SmuzhiyunAnd with gcc, all the "__bitwise"/"__force stuff" goes away, and it all
35*4882a593Smuzhiyunends up looking just like integers to gcc.
36*4882a593Smuzhiyun
37*4882a593SmuzhiyunQuite frankly, you don't need the enum there. The above all really just
38*4882a593Smuzhiyunboils down to one special "int __bitwise" type.
39*4882a593Smuzhiyun
40*4882a593SmuzhiyunSo the simpler way is to just do::
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun        typedef int __bitwise pm_request_t;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun        #define PM_SUSPEND ((__force pm_request_t) 1)
45*4882a593Smuzhiyun        #define PM_RESUME ((__force pm_request_t) 2)
46*4882a593Smuzhiyun
47*4882a593Smuzhiyunand you now have all the infrastructure needed for strict typechecking.
48*4882a593Smuzhiyun
49*4882a593SmuzhiyunOne small note: the constant integer "0" is special. You can use a
50*4882a593Smuzhiyunconstant zero as a bitwise integer type without sparse ever complaining.
51*4882a593SmuzhiyunThis is because "bitwise" (as the name implies) was designed for making
52*4882a593Smuzhiyunsure that bitwise types don't get mixed up (little-endian vs big-endian
53*4882a593Smuzhiyunvs cpu-endian vs whatever), and there the constant "0" really _is_
54*4882a593Smuzhiyunspecial.
55*4882a593Smuzhiyun
56*4882a593SmuzhiyunUsing sparse for lock checking
57*4882a593Smuzhiyun------------------------------
58*4882a593Smuzhiyun
59*4882a593SmuzhiyunThe following macros are undefined for gcc and defined during a sparse
60*4882a593Smuzhiyunrun to use the "context" tracking feature of sparse, applied to
61*4882a593Smuzhiyunlocking.  These annotations tell sparse when a lock is held, with
62*4882a593Smuzhiyunregard to the annotated function's entry and exit.
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun__must_hold - The specified lock is held on function entry and exit.
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun__acquires - The specified lock is held on function exit, but not entry.
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun__releases - The specified lock is held on function entry, but not exit.
69*4882a593Smuzhiyun
70*4882a593SmuzhiyunIf the function enters and exits without the lock held, acquiring and
71*4882a593Smuzhiyunreleasing the lock inside the function in a balanced way, no
72*4882a593Smuzhiyunannotation is needed.  The three annotations above are for cases where
73*4882a593Smuzhiyunsparse would otherwise report a context imbalance.
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunGetting sparse
76*4882a593Smuzhiyun--------------
77*4882a593Smuzhiyun
78*4882a593SmuzhiyunYou can get tarballs of the latest released versions from:
79*4882a593Smuzhiyunhttps://www.kernel.org/pub/software/devel/sparse/dist/
80*4882a593Smuzhiyun
81*4882a593SmuzhiyunAlternatively, you can get snapshots of the latest development version
82*4882a593Smuzhiyunof sparse using git to clone::
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun        git://git.kernel.org/pub/scm/devel/sparse/sparse.git
85*4882a593Smuzhiyun
86*4882a593SmuzhiyunOnce you have it, just do::
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun        make
89*4882a593Smuzhiyun        make install
90*4882a593Smuzhiyun
91*4882a593Smuzhiyunas a regular user, and it will install sparse in your ~/bin directory.
92*4882a593Smuzhiyun
93*4882a593SmuzhiyunUsing sparse
94*4882a593Smuzhiyun------------
95*4882a593Smuzhiyun
96*4882a593SmuzhiyunDo a kernel make with "make C=1" to run sparse on all the C files that get
97*4882a593Smuzhiyunrecompiled, or use "make C=2" to run sparse on the files whether they need to
98*4882a593Smuzhiyunbe recompiled or not.  The latter is a fast way to check the whole tree if you
99*4882a593Smuzhiyunhave already built it.
100*4882a593Smuzhiyun
101*4882a593SmuzhiyunThe optional make variable CF can be used to pass arguments to sparse.  The
102*4882a593Smuzhiyunbuild system passes -Wbitwise to sparse automatically.
103