18c2ecf20Sopenharmony_ci.. Copyright 2004 Linus Torvalds
28c2ecf20Sopenharmony_ci.. Copyright 2004 Pavel Machek <pavel@ucw.cz>
38c2ecf20Sopenharmony_ci.. Copyright 2006 Bob Copeland <me@bobcopeland.com>
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciSparse
68c2ecf20Sopenharmony_ci======
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ciSparse is a semantic checker for C programs; it can be used to find a
98c2ecf20Sopenharmony_cinumber of potential problems with kernel code.  See
108c2ecf20Sopenharmony_cihttps://lwn.net/Articles/689907/ for an overview of sparse; this document
118c2ecf20Sopenharmony_cicontains some kernel-specific sparse information.
128c2ecf20Sopenharmony_ciMore information on sparse, mainly about its internals, can be found in
138c2ecf20Sopenharmony_ciits official pages at https://sparse.docs.kernel.org.
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciUsing sparse for typechecking
178c2ecf20Sopenharmony_ci-----------------------------
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci"__bitwise" is a type attribute, so you have to do something like this::
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci        typedef int __bitwise pm_request_t;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci        enum pm_request {
248c2ecf20Sopenharmony_ci                PM_SUSPEND = (__force pm_request_t) 1,
258c2ecf20Sopenharmony_ci                PM_RESUME = (__force pm_request_t) 2
268c2ecf20Sopenharmony_ci        };
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciwhich makes PM_SUSPEND and PM_RESUME "bitwise" integers (the "__force" is
298c2ecf20Sopenharmony_cithere because sparse will complain about casting to/from a bitwise type,
308c2ecf20Sopenharmony_cibut in this case we really _do_ want to force the conversion). And because
318c2ecf20Sopenharmony_cithe enum values are all the same type, now "enum pm_request" will be that
328c2ecf20Sopenharmony_citype too.
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ciAnd with gcc, all the "__bitwise"/"__force stuff" goes away, and it all
358c2ecf20Sopenharmony_ciends up looking just like integers to gcc.
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciQuite frankly, you don't need the enum there. The above all really just
388c2ecf20Sopenharmony_ciboils down to one special "int __bitwise" type.
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciSo the simpler way is to just do::
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci        typedef int __bitwise pm_request_t;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci        #define PM_SUSPEND ((__force pm_request_t) 1)
458c2ecf20Sopenharmony_ci        #define PM_RESUME ((__force pm_request_t) 2)
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciand you now have all the infrastructure needed for strict typechecking.
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciOne small note: the constant integer "0" is special. You can use a
508c2ecf20Sopenharmony_ciconstant zero as a bitwise integer type without sparse ever complaining.
518c2ecf20Sopenharmony_ciThis is because "bitwise" (as the name implies) was designed for making
528c2ecf20Sopenharmony_cisure that bitwise types don't get mixed up (little-endian vs big-endian
538c2ecf20Sopenharmony_civs cpu-endian vs whatever), and there the constant "0" really _is_
548c2ecf20Sopenharmony_cispecial.
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ciUsing sparse for lock checking
578c2ecf20Sopenharmony_ci------------------------------
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciThe following macros are undefined for gcc and defined during a sparse
608c2ecf20Sopenharmony_cirun to use the "context" tracking feature of sparse, applied to
618c2ecf20Sopenharmony_cilocking.  These annotations tell sparse when a lock is held, with
628c2ecf20Sopenharmony_ciregard to the annotated function's entry and exit.
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci__must_hold - The specified lock is held on function entry and exit.
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci__acquires - The specified lock is held on function exit, but not entry.
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci__releases - The specified lock is held on function entry, but not exit.
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ciIf the function enters and exits without the lock held, acquiring and
718c2ecf20Sopenharmony_cireleasing the lock inside the function in a balanced way, no
728c2ecf20Sopenharmony_ciannotation is needed.  The three annotations above are for cases where
738c2ecf20Sopenharmony_cisparse would otherwise report a context imbalance.
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ciGetting sparse
768c2ecf20Sopenharmony_ci--------------
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ciYou can get tarballs of the latest released versions from:
798c2ecf20Sopenharmony_cihttps://www.kernel.org/pub/software/devel/sparse/dist/
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ciAlternatively, you can get snapshots of the latest development version
828c2ecf20Sopenharmony_ciof sparse using git to clone::
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci        git://git.kernel.org/pub/scm/devel/sparse/sparse.git
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ciOnce you have it, just do::
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci        make
898c2ecf20Sopenharmony_ci        make install
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cias a regular user, and it will install sparse in your ~/bin directory.
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ciUsing sparse
948c2ecf20Sopenharmony_ci------------
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ciDo a kernel make with "make C=1" to run sparse on all the C files that get
978c2ecf20Sopenharmony_cirecompiled, or use "make C=2" to run sparse on the files whether they need to
988c2ecf20Sopenharmony_cibe recompiled or not.  The latter is a fast way to check the whole tree if you
998c2ecf20Sopenharmony_cihave already built it.
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciThe optional make variable CF can be used to pass arguments to sparse.  The
1028c2ecf20Sopenharmony_cibuild system passes -Wbitwise to sparse automatically.
103