Skip to content

Commit 44cee85

Browse files
committed
Merge branch 'misc' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
Pull misc kbuild updates from Michal Marek: - coccicheck script improvements by Luis Rodriguez and Deepa Dinamani - new coccinelle patches by Yann Droneaud and Vaishali Thakkar - debian packaging fixes by Wilfried Klaebe, Henning Schild and Marcin Mielniczuk * 'misc' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild: Fix the Debian packaging script on systems with no codename builddeb: fix file permissions before packaging scripts/coccinelle: require coccinelle >= 1.0.4 on device_node_continue.cocci coccicheck: refer to Documentation/coccinelle.txt and wiki coccicheck: add support for requring a coccinelle version scripts: add Linux .cocciconfig for coccinelle coccicheck: replace --very-quiet with --quiet when debugging coccicheck: add support for DEBUG_FILE coccicheck: enable parmap support coccicheck: make SPFLAGS more useful coccicheck: move spatch binary check up builddeb: really include objtool binary in headers package coccinelle: catch krealloc() on devm_*() allocated memory coccinelle: recognize more devm_* memory allocation functions coccinelle: also catch kzfree() issues coccicheck: Allow for overriding spatch flags Coccinelle: noderef: Add new rules and correct the old rule
2 parents f716a85 + 4c58606 commit 44cee85

File tree

11 files changed

+313
-21
lines changed

11 files changed

+313
-21
lines changed

.cocciconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[spatch]
2+
options = --timeout 200
3+
options = --use-gitgrep

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Module.symvers
6767
#
6868
!.gitignore
6969
!.mailmap
70+
!.cocciconfig
7071

7172
#
7273
# Generated include files

Documentation/coccinelle.txt

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ as a regular user, and install it with
3838

3939
sudo make install
4040

41+
Supplemental documentation
42+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43+
44+
For supplemental documentation refer to the wiki:
45+
46+
https://bottest.wiki.kernel.org/coccicheck
47+
48+
The wiki documentation always refers to the linux-next version of the script.
49+
4150
Using Coccinelle on the Linux kernel
4251
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4352

@@ -94,11 +103,26 @@ To enable verbose messages set the V= variable, for example:
94103

95104
make coccicheck MODE=report V=1
96105

106+
Coccinelle parallelization
107+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108+
97109
By default, coccicheck tries to run as parallel as possible. To change
98110
the parallelism, set the J= variable. For example, to run across 4 CPUs:
99111

100112
make coccicheck MODE=report J=4
101113

114+
As of Coccinelle 1.0.2 Coccinelle uses Ocaml parmap for parallelization,
115+
if support for this is detected you will benefit from parmap parallelization.
116+
117+
When parmap is enabled coccicheck will enable dynamic load balancing by using
118+
'--chunksize 1' argument, this ensures we keep feeding threads with work
119+
one by one, so that we avoid the situation where most work gets done by only
120+
a few threads. With dynamic load balancing, if a thread finishes early we keep
121+
feeding it more work.
122+
123+
When parmap is enabled, if an error occurs in Coccinelle, this error
124+
value is propagated back, the return value of the 'make coccicheck'
125+
captures this return value.
102126

103127
Using Coccinelle with a single semantic patch
104128
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -142,15 +166,118 @@ semantic patch as shown in the previous section.
142166
The "report" mode is the default. You can select another one with the
143167
MODE variable explained above.
144168

169+
Debugging Coccinelle SmPL patches
170+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171+
172+
Using coccicheck is best as it provides in the spatch command line
173+
include options matching the options used when we compile the kernel.
174+
You can learn what these options are by using V=1, you could then
175+
manually run Coccinelle with debug options added.
176+
177+
Alternatively you can debug running Coccinelle against SmPL patches
178+
by asking for stderr to be redirected to stderr, by default stderr
179+
is redirected to /dev/null, if you'd like to capture stderr you
180+
can specify the DEBUG_FILE="file.txt" option to coccicheck. For
181+
instance:
182+
183+
rm -f cocci.err
184+
make coccicheck COCCI=scripts/coccinelle/free/kfree.cocci MODE=report DEBUG_FILE=cocci.err
185+
cat cocci.err
186+
187+
You can use SPFLAGS to add debugging flags, for instance you may want to
188+
add both --profile --show-trying to SPFLAGS when debugging. For instance
189+
you may want to use:
190+
191+
rm -f err.log
192+
export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci
193+
make coccicheck DEBUG_FILE="err.log" MODE=report SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c
194+
195+
err.log will now have the profiling information, while stdout will
196+
provide some progress information as Coccinelle moves forward with
197+
work.
198+
199+
DEBUG_FILE support is only supported when using coccinelle >= 1.2.
200+
201+
.cocciconfig support
202+
~~~~~~~~~~~~~~~~~~~~~~
203+
204+
Coccinelle supports reading .cocciconfig for default Coccinelle options that
205+
should be used every time spatch is spawned, the order of precedence for
206+
variables for .cocciconfig is as follows:
207+
208+
o Your current user's home directory is processed first
209+
o Your directory from which spatch is called is processed next
210+
o The directory provided with the --dir option is processed last, if used
211+
212+
Since coccicheck runs through make, it naturally runs from the kernel
213+
proper dir, as such the second rule above would be implied for picking up a
214+
.cocciconfig when using 'make coccicheck'.
215+
216+
'make coccicheck' also supports using M= targets.If you do not supply
217+
any M= target, it is assumed you want to target the entire kernel.
218+
The kernel coccicheck script has:
219+
220+
if [ "$KBUILD_EXTMOD" = "" ] ; then
221+
OPTIONS="--dir $srctree $COCCIINCLUDE"
222+
else
223+
OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
224+
fi
225+
226+
KBUILD_EXTMOD is set when an explicit target with M= is used. For both cases
227+
the spatch --dir argument is used, as such third rule applies when whether M=
228+
is used or not, and when M= is used the target directory can have its own
229+
.cocciconfig file. When M= is not passed as an argument to coccicheck the
230+
target directory is the same as the directory from where spatch was called.
231+
232+
If not using the kernel's coccicheck target, keep the above precedence
233+
order logic of .cocciconfig reading. If using the kernel's coccicheck target,
234+
override any of the kernel's .coccicheck's settings using SPFLAGS.
235+
236+
We help Coccinelle when used against Linux with a set of sensible defaults
237+
options for Linux with our own Linux .cocciconfig. This hints to coccinelle
238+
git can be used for 'git grep' queries over coccigrep. A timeout of 200
239+
seconds should suffice for now.
240+
241+
The options picked up by coccinelle when reading a .cocciconfig do not appear
242+
as arguments to spatch processes running on your system, to confirm what
243+
options will be used by Coccinelle run:
244+
245+
spatch --print-options-only
246+
247+
You can override with your own preferred index option by using SPFLAGS. Take
248+
note that when there are conflicting options Coccinelle takes precedence for
249+
the last options passed. Using .cocciconfig is possible to use idutils, however
250+
given the order of precedence followed by Coccinelle, since the kernel now
251+
carries its own .cocciconfig, you will need to use SPFLAGS to use idutils if
252+
desired. See below section "Additional flags" for more details on how to use
253+
idutils.
254+
145255
Additional flags
146256
~~~~~~~~~~~~~~~~~~
147257

148258
Additional flags can be passed to spatch through the SPFLAGS
149-
variable.
259+
variable. This works as Coccinelle respects the last flags
260+
given to it when options are in conflict.
150261

151262
make SPFLAGS=--use-glimpse coccicheck
263+
264+
Coccinelle supports idutils as well but requires coccinelle >= 1.0.6.
265+
When no ID file is specified coccinelle assumes your ID database file
266+
is in the file .id-utils.index on the top level of the kernel, coccinelle
267+
carries a script scripts/idutils_index.sh which creates the database with
268+
269+
mkid -i C --output .id-utils.index
270+
271+
If you have another database filename you can also just symlink with this
272+
name.
273+
152274
make SPFLAGS=--use-idutils coccicheck
153275

276+
Alternatively you can specify the database filename explicitly, for
277+
instance:
278+
279+
make SPFLAGS="--use-idutils /full-path/to/ID" coccicheck
280+
154281
See spatch --help to learn more about spatch options.
155282

156283
Note that the '--use-glimpse' and '--use-idutils' options
@@ -159,6 +286,25 @@ thus active by default. However, by indexing the code with
159286
one of these tools, and according to the cocci file used,
160287
spatch could proceed the entire code base more quickly.
161288

289+
SmPL patch specific options
290+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291+
292+
SmPL patches can have their own requirements for options passed
293+
to Coccinelle. SmPL patch specific options can be provided by
294+
providing them at the top of the SmPL patch, for instance:
295+
296+
// Options: --no-includes --include-headers
297+
298+
SmPL patch Coccinelle requirements
299+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
300+
301+
As Coccinelle features get added some more advanced SmPL patches
302+
may require newer versions of Coccinelle. If an SmPL patch requires
303+
at least a version of Coccinelle, this can be specified as follows,
304+
as an example if requiring at least Coccinelle >= 1.0.5:
305+
306+
// Requires: 1.0.5
307+
162308
Proposing new semantic patches
163309
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
164310

scripts/coccicheck

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
#!/bin/bash
2-
2+
# Linux kernel coccicheck
3+
#
4+
# Read Documentation/coccinelle.txt
35
#
46
# This script requires at least spatch
57
# version 1.0.0-rc11.
6-
#
78

9+
DIR="$(dirname $(readlink -f $0))/.."
810
SPATCH="`which ${SPATCH:=spatch}`"
911

10-
trap kill_running SIGTERM SIGINT
11-
declare -a SPATCH_PID
12+
if [ ! -x "$SPATCH" ]; then
13+
echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
14+
exit 1
15+
fi
16+
17+
SPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}')
18+
SPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh)
19+
20+
USE_JOBS="no"
21+
$SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes"
1222

1323
# The verbosity may be set by the environmental parameter V=
1424
# as for example with 'make V=1 coccicheck'
@@ -25,7 +35,28 @@ else
2535
NPROC="$J"
2636
fi
2737

28-
FLAGS="$SPFLAGS --very-quiet"
38+
FLAGS="--very-quiet"
39+
40+
# You can use SPFLAGS to append extra arguments to coccicheck or override any
41+
# heuristics done in this file as Coccinelle accepts the last options when
42+
# options conflict.
43+
#
44+
# A good example for use of SPFLAGS is if you want to debug your cocci script,
45+
# you can for instance use the following:
46+
#
47+
# $ export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci
48+
# $ make coccicheck MODE=report DEBUG_FILE="all.err" SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c
49+
#
50+
# "--show-trying" should show you what rule is being processed as it goes to
51+
# stdout, you do not need a debug file for that. The profile output will be
52+
# be sent to stdout, if you provide a DEBUG_FILE the profiling data can be
53+
# inspected there.
54+
#
55+
# --profile will not output if --very-quiet is used, so avoid it.
56+
echo $SPFLAGS | egrep -e "--profile|--show-trying" 2>&1 > /dev/null
57+
if [ $? -eq 0 ]; then
58+
FLAGS="--quiet"
59+
fi
2960

3061
# spatch only allows include directories with the syntax "-I include"
3162
# while gcc also allows "-Iinclude" and "-include include"
@@ -51,9 +82,14 @@ if [ "$KBUILD_EXTMOD" != "" ] ; then
5182
OPTIONS="--patch $srctree $OPTIONS"
5283
fi
5384

54-
if [ ! -x "$SPATCH" ]; then
55-
echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
56-
exit 1
85+
# You can override by using SPFLAGS
86+
if [ "$USE_JOBS" = "no" ]; then
87+
trap kill_running SIGTERM SIGINT
88+
declare -a SPATCH_PID
89+
elif [ "$NPROC" != "1" ]; then
90+
# Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on
91+
# https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c
92+
OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1"
5793
fi
5894

5995
if [ "$MODE" = "" ] ; then
@@ -72,7 +108,7 @@ if [ "$MODE" = "chain" ] ; then
72108
echo 'All available modes will be tried (in that order): patch, report, context, org'
73109
fi
74110
elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
75-
FLAGS="$FLAGS --no-show-diff"
111+
FLAGS="--no-show-diff $FLAGS"
76112
fi
77113

78114
if [ "$ONLINE" = "0" ] ; then
@@ -82,7 +118,26 @@ if [ "$ONLINE" = "0" ] ; then
82118
echo ''
83119
fi
84120

85-
run_cmd() {
121+
run_cmd_parmap() {
122+
if [ $VERBOSE -ne 0 ] ; then
123+
echo "Running ($NPROC in parallel): $@"
124+
fi
125+
if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then
126+
if [ -f $DEBUG_FILE ]; then
127+
echo "Debug file $DEBUG_FILE exists, bailing"
128+
exit
129+
fi
130+
else
131+
DEBUG_FILE="/dev/null"
132+
fi
133+
$@ 2>$DEBUG_FILE
134+
if [[ $? -ne 0 ]]; then
135+
echo "coccicheck failed"
136+
exit $?
137+
fi
138+
}
139+
140+
run_cmd_old() {
86141
local i
87142
if [ $VERBOSE -ne 0 ] ; then
88143
echo "Running ($NPROC in parallel): $@"
@@ -97,6 +152,14 @@ run_cmd() {
97152
wait
98153
}
99154

155+
run_cmd() {
156+
if [ "$USE_JOBS" = "yes" ]; then
157+
run_cmd_parmap $@
158+
else
159+
run_cmd_old $@
160+
fi
161+
}
162+
100163
kill_running() {
101164
for i in $(seq 0 $(( NPROC - 1 )) ); do
102165
if [ $VERBOSE -eq 2 ] ; then
@@ -106,10 +169,23 @@ kill_running() {
106169
done
107170
}
108171

172+
# You can override heuristics with SPFLAGS, these must always go last
173+
OPTIONS="$OPTIONS $SPFLAGS"
174+
109175
coccinelle () {
110176
COCCI="$1"
111177

112178
OPT=`grep "Option" $COCCI | cut -d':' -f2`
179+
REQ=`grep "Requires" $COCCI | cut -d':' -f2 | sed "s| ||"`
180+
REQ_NUM=$(echo $REQ | ${DIR}/scripts/ld-version.sh)
181+
if [ "$REQ_NUM" != "0" ] ; then
182+
if [ "$SPATCH_VERSION_NUM" -lt "$REQ_NUM" ] ; then
183+
echo "Skipping coccinele SmPL patch: $COCCI"
184+
echo "You have coccinelle: $SPATCH_VERSION"
185+
echo "This SmPL patch requires: $REQ"
186+
return
187+
fi
188+
fi
113189

114190
# The option '--parse-cocci' can be used to syntactically check the SmPL files.
115191
#

scripts/coccinelle/free/devm_free.cocci

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,23 @@ expression x;
2929
@@
3030

3131
(
32+
x = devm_kmalloc(...)
33+
|
34+
x = devm_kvasprintf(...)
35+
|
36+
x = devm_kasprintf(...)
37+
|
3238
x = devm_kzalloc(...)
39+
|
40+
x = devm_kmalloc_array(...)
41+
|
42+
x = devm_kcalloc(...)
43+
|
44+
x = devm_kstrdup(...)
45+
|
46+
x = devm_kmemdup(...)
47+
|
48+
x = devm_get_free_pages(...)
3349
|
3450
x = devm_request_irq(...)
3551
|
@@ -48,6 +64,16 @@ position p;
4864
(
4965
* kfree@p(x)
5066
|
67+
* kzfree@p(x)
68+
|
69+
* __krealloc@p(x, ...)
70+
|
71+
* krealloc@p(x, ...)
72+
|
73+
* free_pages@p(x, ...)
74+
|
75+
* free_page@p(x)
76+
|
5177
* free_irq@p(x)
5278
|
5379
* iounmap@p(x)

0 commit comments

Comments
 (0)