Skip to content

Commit

Permalink
Merge branch 'misc' of git://git.kernel.org/pub/scm/linux/kernel/git/…
Browse files Browse the repository at this point in the history
…mmarek/kbuild

* 'misc' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild:
  scripts/coccinelle: improve the coverage of some semantic patches
  coccinelle: semantic patches related to devm_ functions (part 2)
  coccinelle: semantic patches related to devm_ functions (part 1)
  coccinelle.txt: update documentation to include M= option
  coccicheck: add M= option to control which dir is processed
  ctags: remove struct forward declarations
  scripts/tags.sh: Add Page flag function magic
  • Loading branch information
torvalds committed Jan 16, 2012
2 parents 287b901 + 29a36d4 commit 408e057
Show file tree
Hide file tree
Showing 16 changed files with 595 additions and 85 deletions.
10 changes: 8 additions & 2 deletions Documentation/coccinelle.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,15 @@ or
make coccicheck COCCI=<my_SP.cocci> MODE=report


Using Coccinelle on (modified) files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Controlling Which Files are Processed by Coccinelle
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
By default the entire kernel source tree is checked.

To apply Coccinelle to a specific directory, M= can be used.
For example, to check drivers/net/wireless/ one may write:

make coccicheck M=drivers/net/wireless/

To apply Coccinelle on a file basis, instead of a directory basis, the
following command may be used:

Expand Down
19 changes: 14 additions & 5 deletions scripts/coccicheck
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ if [ "$C" = "1" -o "$C" = "2" ]; then
# FLAGS="-ignore_unknown_options -very_quiet"
# OPTIONS=$*

# Workaround for Coccinelle < 0.2.3
FLAGS="-I $srctree/include -very_quiet"
shift $(( $# - 1 ))
OPTIONS=$1
if [ "$KBUILD_EXTMOD" = "" ] ; then
# Workaround for Coccinelle < 0.2.3
FLAGS="-I $srctree/include -very_quiet"
shift $(( $# - 1 ))
OPTIONS=$1
else
echo M= is not currently supported when C=1 or C=2
exit 1
fi
else
ONLINE=0
FLAGS="-very_quiet"
OPTIONS="-dir $srctree"
if [ "$KBUILD_EXTMOD" = "" ] ; then
OPTIONS="-dir $srctree"
else
OPTIONS="-dir $KBUILD_EXTMOD -patch $srctree -I $srctree/include -I $KBUILD_EXTMOD/include"
fi
fi

if [ ! -x "$SPATCH" ]; then
Expand Down
105 changes: 105 additions & 0 deletions scripts/coccinelle/api/devm_request_and_ioremap.cocci
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/// Reimplement a call to devm_request_mem_region followed by a call to ioremap
/// or ioremap_nocache by a call to devm_request_and_ioremap.
/// Devm_request_and_ioremap was introduced in
/// 72f8c0bfa0de64c68ee59f40eb9b2683bffffbb0. It makes the code much more
/// concise.
///
///
// Confidence: High
// Copyright: (C) 2011 Julia Lawall, INRIA/LIP6. GPLv2.
// Copyright: (C) 2011 Gilles Muller, INRIA/LiP6. GPLv2.
// URL: http://coccinelle.lip6.fr/
// Comments:
// Options: -no_includes -include_headers

virtual patch
virtual org
virtual report
virtual context

@nm@
expression myname;
identifier i;
@@

struct platform_driver i = { .driver = { .name = myname } };

@depends on patch@
expression dev,res,size;
@@

-if (!devm_request_mem_region(dev, res->start, size,
- \(res->name\|dev_name(dev)\))) {
- ...
- return ...;
-}
... when != res->start
(
-devm_ioremap(dev,res->start,size)
+devm_request_and_ioremap(dev,res)
|
-devm_ioremap_nocache(dev,res->start,size)
+devm_request_and_ioremap(dev,res)
)
... when any
when != res->start

// this rule is separate from the previous one, because a single file can
// have multiple values of myname
@depends on patch@
expression dev,res,size;
expression nm.myname;
@@

-if (!devm_request_mem_region(dev, res->start, size,myname)) {
- ...
- return ...;
-}
... when != res->start
(
-devm_ioremap(dev,res->start,size)
+devm_request_and_ioremap(dev,res)
|
-devm_ioremap_nocache(dev,res->start,size)
+devm_request_and_ioremap(dev,res)
)
... when any
when != res->start


@pb depends on org || report || context@
expression dev,res,size;
expression nm.myname;
position p1,p2;
@@

*if
(!devm_request_mem_region@p1(dev, res->start, size,
\(res->name\|dev_name(dev)\|myname\))) {
...
return ...;
}
... when != res->start
(
*devm_ioremap@p2(dev,res->start,size)
|
*devm_ioremap_nocache@p2(dev,res->start,size)
)
... when any
when != res->start

@script:python depends on org@
p1 << pb.p1;
p2 << pb.p2;
@@
cocci.print_main("INFO: replace by devm_request_and_ioremap",p1)
cocci.print_secs("",p2)
@script:python depends on report@
p1 << pb.p1;
p2 << pb.p2;
@@
msg = "INFO: devm_request_mem_region followed by ioremap on line %s can be replaced by devm_request_and_ioremap" % (p2[0].line)
coccilib.report.print_report(p1[0],msg)
75 changes: 70 additions & 5 deletions scripts/coccinelle/api/kstrdup.cocci
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
/// Use kstrdup rather than duplicating its implementation
///
// Confidence: High
// Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2.
// Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2.
// Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2.
// Copyright: (C) 2010-2012 Nicolas Palix. GPLv2.
// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2.
// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2.
// URL: http://coccinelle.lip6.fr/
// Comments:
// Options: -no_includes -include_headers

virtual patch
virtual context
virtual org
virtual report

@@
@depends on patch@
expression from,to;
expression flag,E1,E2;
statement S;
Expand All @@ -23,7 +26,7 @@ statement S;
... when != \(from = E2 \| to = E2 \)
- strcpy(to, from);

@@
@depends on patch@
expression x,from,to;
expression flag,E1,E2,E3;
statement S;
Expand All @@ -37,3 +40,65 @@ statement S;
if (to==NULL || ...) S
... when != \(x = E3 \| from = E3 \| to = E3 \)
- memcpy(to, from, x);

// ---------------------------------------------------------------------

@r1 depends on !patch exists@
expression from,to;
expression flag,E1,E2;
statement S;
position p1,p2;
@@

* to = kmalloc@p1(strlen(from) + 1,flag);
... when != \(from = E1 \| to = E1 \)
if (to==NULL || ...) S
... when != \(from = E2 \| to = E2 \)
* strcpy@p2(to, from);

@r2 depends on !patch exists@
expression x,from,to;
expression flag,E1,E2,E3;
statement S;
position p1,p2;
@@

* x = strlen(from) + 1;
... when != \( x = E1 \| from = E1 \)
* to = \(kmalloc@p1\|kzalloc@p2\)(x,flag);
... when != \(x = E2 \| from = E2 \| to = E2 \)
if (to==NULL || ...) S
... when != \(x = E3 \| from = E3 \| to = E3 \)
* memcpy@p2(to, from, x);

@script:python depends on org@
p1 << r1.p1;
p2 << r1.p2;
@@
cocci.print_main("WARNING opportunity for kstrdep",p1)
cocci.print_secs("strcpy",p2)
@script:python depends on org@
p1 << r2.p1;
p2 << r2.p2;
@@
cocci.print_main("WARNING opportunity for kstrdep",p1)
cocci.print_secs("memcpy",p2)
@script:python depends on report@
p1 << r1.p1;
p2 << r1.p2;
@@
msg = "WARNING opportunity for kstrdep (strcpy on line %s)" % (p2[0].line)
coccilib.report.print_report(p1[0], msg)
@script:python depends on report@
p1 << r2.p1;
p2 << r2.p2;
@@
msg = "WARNING opportunity for kstrdep (memcpy on line %s)" % (p2[0].line)
coccilib.report.print_report(p1[0], msg)
34 changes: 30 additions & 4 deletions scripts/coccinelle/api/memdup.cocci
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/// Use kmemdup rather than duplicating its implementation
///
// Confidence: High
// Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2.
// Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2.
// Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2.
// Copyright: (C) 2010-2012 Nicolas Palix. GPLv2.
// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2.
// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2.
// URL: http://coccinelle.lip6.fr/
// Comments:
// Options: -no_includes -include_headers

virtual patch
virtual context
virtual org
virtual report

@r1@
expression from,to;
Expand All @@ -28,7 +31,7 @@ position p;
... when != \( x = E1 \| from = E1 \)
to = \(kmalloc@p\|kzalloc@p\)(x,flag);

@@
@depends on patch@
expression from,to,size,flag;
position p != {r1.p,r2.p};
statement S;
Expand All @@ -38,3 +41,26 @@ statement S;
+ to = kmemdup(from,size,flag);
if (to==NULL || ...) S
- memcpy(to, from, size);

@r depends on !patch@
expression from,to,size,flag;
position p != {r1.p,r2.p};
statement S;
@@

* to = \(kmalloc@p\|kzalloc@p\)(size,flag);
to = kmemdup(from,size,flag);
if (to==NULL || ...) S
* memcpy(to, from, size);

@script:python depends on org@
p << r.p;
@@
coccilib.org.print_todo(p[0], "WARNING opportunity for kmemdep")
@script:python depends on report@
p << r.p;
@@
coccilib.report.print_report(p[0], "WARNING opportunity for kmemdep")
39 changes: 32 additions & 7 deletions scripts/coccinelle/api/memdup_user.cocci
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
/// Use kmemdup_user rather than duplicating its implementation
/// Use memdup_user rather than duplicating its implementation
/// This is a little bit restricted to reduce false positives
///
// Confidence: High
// Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2.
// Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2.
// Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2.
// Copyright: (C) 2010-2012 Nicolas Palix. GPLv2.
// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2.
// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2.
// URL: http://coccinelle.lip6.fr/
// Comments:
// Options: -no_includes -include_headers

virtual patch
virtual context
virtual org
virtual report

@@
@depends on patch@
expression from,to,size,flag;
position p;
identifier l1,l2;
@@

- to = \(kmalloc@p\|kzalloc@p\)(size,flag);
- to = \(kmalloc\|kzalloc\)(size,flag);
+ to = memdup_user(from,size);
if (
- to==NULL
Expand All @@ -33,3 +35,26 @@ identifier l1,l2;
- -EFAULT
- ...+>
- }

@r depends on !patch@
expression from,to,size,flag;
position p;
statement S1,S2;
@@

* to = \(kmalloc@p\|kzalloc@p\)(size,flag);
if (to==NULL || ...) S1
if (copy_from_user(to, from, size) != 0)
S2

@script:python depends on org@
p << r.p;
@@
coccilib.org.print_todo(p[0], "WARNING opportunity for memdep_user")
@script:python depends on report@
p << r.p;
@@
coccilib.report.print_report(p[0], "WARNING opportunity for memdep_user")
Loading

0 comments on commit 408e057

Please sign in to comment.