[PATCH 2/4] Add scripts/smpl/drop_kmalloc_cast.cocci

Previous thread: [PATCH] - New round-robin rotor for SLAB allocations by Jack Steiner on Monday, April 26, 2010 - 2:00 pm. (4 messages)

Next thread: Positive results with Transparent Hugepages at VMware by Philip Langdale on Monday, April 26, 2010 - 2:17 pm. (1 message)
From: Nicolas Palix
Date: Monday, April 26, 2010 - 2:11 pm

New targets are added (coccicheck-<mode>) to call the spatch front-end
in the 'scripts' directory with the <mode> argument.

Four modes are defined: report, patch, context, and org.

'report' mode generates a list in the following format:
  file:line:column-column: message

'patch' mode proposes a generic fix, when possible.

'context' mode highlights lines of interest and their context
in a diff-like style.

'org' mode generates a report in the Org mode format of Emacs.

Three semantic patches, with a low rate of false positives, are also added.
Other semantic patches will be propose later.

Nicolas Palix (4):
  Add targets to use the Coccinelle checker
  Add scripts/smpl/drop_kmalloc_cast.cocci
  Add scripts/smpl/kzalloc-simple.cocci
  Add scripts/smpl/resource_size.cocci

 MAINTAINERS                          |   10 +++
 Makefile                             |    9 +++
 scripts/smpl/drop_kmalloc_cast.cocci |   74 +++++++++++++++++++++++++
 scripts/smpl/kzalloc-simple.cocci    |   88 +++++++++++++++++++++++++++++
 scripts/smpl/resource_size.cocci     |  101 ++++++++++++++++++++++++++++++++++
 scripts/spatch.sh                    |   14 +++++
 6 files changed, 296 insertions(+), 0 deletions(-)
 create mode 100644 scripts/smpl/drop_kmalloc_cast.cocci
 create mode 100644 scripts/smpl/kzalloc-simple.cocci
 create mode 100644 scripts/smpl/resource_size.cocci
 create mode 100755 scripts/spatch.sh

--

From: Nicolas Palix
Date: Monday, April 26, 2010 - 2:11 pm

Four targets are added. Each one generates a different
output kind: context, patch, org, report.
Every SmPL file in 'scripts/smpl' is given to the spatch frontend
(located in the 'scripts' directory), and applied to the entire
source tree.

Signed-off-by: Nicolas Palix <npalix@diku.dk>
---
 MAINTAINERS       |   10 ++++++++++
 Makefile          |    9 +++++++++
 scripts/spatch.sh |   14 ++++++++++++++
 3 files changed, 33 insertions(+), 0 deletions(-)
 create mode 100755 scripts/spatch.sh

diff --git a/MAINTAINERS b/MAINTAINERS
index 3d29fa3..2aab763 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1533,6 +1533,16 @@ L:	platform-driver-x86@vger.kernel.org
 S:	Supported
 F:	drivers/platform/x86/classmate-laptop.c
 
+COCCINELLE
+M:	Julia Lawall <julia@diku.dk>
+M:	Gilles Muller <Gilles.Muller@lip6.fr>
+M:	Nicolas Palix <npalix@diku.dk>
+L:	cocci@diku.dk
+W:	http://coccinelle.lip6.fr/
+S:	Supported
+F:	scripts/smpl/
+F:	scripts/spatch.sh
+
 CODA FILE SYSTEM
 M:	Jan Harkes <jaharkes@cs.cmu.edu>
 M:	coda@cs.cmu.edu
diff --git a/Makefile b/Makefile
index 67c1001..293c88b 100644
--- a/Makefile
+++ b/Makefile
@@ -325,6 +325,7 @@ INSTALLKERNEL  := installkernel
 DEPMOD		= /sbin/depmod
 KALLSYMS	= scripts/kallsyms
 PERL		= perl
+SPATCH          = spatch
 CHECK		= sparse
 
 CHECKFLAGS     := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ \
@@ -1424,6 +1425,14 @@ versioncheck:
 		-name '*.[hcS]' -type f -print | sort \
 		| xargs $(PERL) -w $(srctree)/scripts/checkversion.pl
 
+coccicheck-context coccicheck-patch coccicheck-org coccicheck-report:
+	@echo "\nPlease, check for false positive in the output before submitting a patch.\n\n"\
+		"Take particularly attention when using the \"patch\" mode\n"\
+		"and carefully review the patch YOU are about to submit.\n"
+	@find $(srctree)/scripts/smpl/                        \
+		-name '*.cocci' -type f                       \
+		-exec $(srctree)/scripts/spatch.sh $(SPATCH) $(@:coccicheck-%=%) \{} $(srctree) \;
+
 ...
From: Joe Perches
Date: Monday, April 26, 2010 - 2:37 pm

I like the concept and believe the kernel tree is a better
repository for these scripts than an external website.
 



you



--

From: Nicolas Palix
Date: Monday, April 26, 2010 - 3:20 pm

Thank you for your feedback and support.

From 98aba248f7cba64c771f3632c11b8188819c45a1 Mon Sep 17 00:00:00 2001
From: Nicolas Palix <npalix@diku.dk>
Date: Sun, 4 Apr 2010 15:42:57 +0200
Subject: [PATCH 1/4] Add targets to use the Coccinelle checker

Four targets are added. Each one generates a different
output kind: context, patch, org, report.
Every SmPL file in 'scripts/smpl' is given to the spatch frontend
(located in the 'scripts' directory), and applied to the entire
source tree.

Signed-off-by: Nicolas Palix <npalix@diku.dk>
---
 MAINTAINERS       |   10 ++++++++++
 Makefile          |    9 +++++++++
 scripts/spatch.sh |   14 ++++++++++++++
 3 files changed, 33 insertions(+), 0 deletions(-)
 create mode 100755 scripts/spatch.sh

diff --git a/MAINTAINERS b/MAINTAINERS
index 3d29fa3..2aab763 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1533,6 +1533,16 @@ L:	platform-driver-x86@vger.kernel.org
 S:	Supported
 F:	drivers/platform/x86/classmate-laptop.c
 
+COCCINELLE/Semantic Patches (SmPL)
+M:	Julia Lawall <julia@diku.dk>
+M:	Gilles Muller <Gilles.Muller@lip6.fr>
+M:	Nicolas Palix <npalix@diku.dk>
+L:	cocci@diku.dk
+W:	http://coccinelle.lip6.fr/
+S:	Supported
+F:	scripts/smpl/
+F:	scripts/spatch.sh
+
 CODA FILE SYSTEM
 M:	Jan Harkes <jaharkes@cs.cmu.edu>
 M:	coda@cs.cmu.edu
diff --git a/Makefile b/Makefile
index 67c1001..293c88b 100644
--- a/Makefile
+++ b/Makefile
@@ -325,6 +325,7 @@ INSTALLKERNEL  := installkernel
 DEPMOD		= /sbin/depmod
 KALLSYMS	= scripts/kallsyms
 PERL		= perl
+SPATCH          = spatch
 CHECK		= sparse
 
 CHECKFLAGS     := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ \
@@ -1424,6 +1425,14 @@ versioncheck:
 		-name '*.[hcS]' -type f -print | sort \
 		| xargs $(PERL) -w $(srctree)/scripts/checkversion.pl
 
+coccicheck-context coccicheck-patch coccicheck-org coccicheck-report:
+	@echo "\nPlease check for false positive in the output before submitting a patch.\n\n"\
+		"Take particularly attention when using the \"patch\" ...
From: Randy Dunlap
Date: Monday, April 26, 2010 - 3:23 pm

From: Roland Dreier
Date: Thursday, April 29, 2010 - 10:01 am

> > +		"Take particularly attention when using the \"patch\" mode\n"\
 > 
 > 		      particular

Actually, "take particular attention" is not particularly grammatical.
I might phrase this as

  Be especially careful when using the "patch" mode to review the patch
  you are about to submit.

 - R.
-- 
Roland Dreier <rolandd@cisco.com> || For corporate legal information go to:
http://www.cisco.com/web/about/doing_business/legal/cri/index.html
--

From: Randy Dunlap
Date: Friday, April 30, 2010 - 2:07 pm

Yes, or:

    When using "patch" mode, carefully review the patch before submitting it.


---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***
--

From: Michal Marek
Date: Tuesday, April 27, 2010 - 5:40 am

Hi Nicolas,



This should be echo -e in bash, but then it won't work in dash I guess.
So better use multiple echo commands. Also please add the targets to

Please use 'for file in $(srctree)/scripts/smpl/*.cocci; do ...', so
that the reports are in a defined order. Or do you plan to use

echo "The semantic patch that makes this change is available"
echo "in $FILE"

Then you don't need to add the same comment to each of the *.cocci
files. Also is it necessary to advertise

"More information about semantic patching is available at
 http://coccinelle.lip6.fr/"

before processing each *.cocci file? If you want the banner, you could
append it to the "Please check for false positives..." text printed once

You can also print the URL here if the spatch command is not available.

Michal
--

From: Michal Marek
Date: Tuesday, April 27, 2010 - 6:01 am

The list should be marked as "moderated for non-subscribers".

Michal
--

From: Wolfram Sang
Date: Tuesday, April 27, 2010 - 5:53 am

I think the part containing "THISFILE" the patches should be generated here at

-- 
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
From: Sam Ravnborg
Date: Tuesday, April 27, 2010 - 1:24 pm

Hej Nicolas.


For outsiders smpl does not rellate to coccinelle.

I suggest to consistently use the name of the tool in all places.

So smpl directory to be named coccinelle
SPATCH named coccinelle
spatch.sh named coccinelle.sh
etc.

This way we use one name for one thing. And not today where you
use three names for the same thing.
The files may be named *.smpl - because their home in

    scripts/coccinelle/

will tell what they are used for.

[I regret that I used CHECK to name the sparse tool].

	Sam
--

From: Sam Ravnborg
Date: Tuesday, April 27, 2010 - 1:28 pm

Looking again they are named *.cocci. So I confused myself on the smpl thing.
The point above consistent naming still holds.

	Sam
--

From: Nicolas Palix
Date: Monday, April 26, 2010 - 2:11 pm

The purpose of this semantic patch is to remove
useless casts, as mentioned in the Linux documentation.
See Chapter 14 in Documentation/CodingStyle for more information.

Signed-off-by: Nicolas Palix <npalix@diku.dk>
---
 scripts/smpl/drop_kmalloc_cast.cocci |   74 ++++++++++++++++++++++++++++++++++
 1 files changed, 74 insertions(+), 0 deletions(-)
 create mode 100644 scripts/smpl/drop_kmalloc_cast.cocci

diff --git a/scripts/smpl/drop_kmalloc_cast.cocci b/scripts/smpl/drop_kmalloc_cast.cocci
new file mode 100644
index 0000000..fbd3950
--- /dev/null
+++ b/scripts/smpl/drop_kmalloc_cast.cocci
@@ -0,0 +1,74 @@
+///
+/// Casting (void *) value returned by kmalloc is useless
+/// as mentioned in Documentation/CodingStyle, Chap 14.
+///
+/// The semantic patch that makes this change is available
+/// in THISFILE.
+///
+/// More information about semantic patching is available at
+/// http://coccinelle.lip6.fr/
+///
+// Confidence: High
+// Copyright: 2009,2010 Nicolas Palix, DIKU.  GPLv2.
+// URL: http://coccinelle.lip6.fr/
+// Options: -no_includes -include_headers
+//
+// Keywords: kmalloc, kzalloc, kcalloc
+// Version min: < 2.6.12 kmalloc
+// Version min: < 2.6.12 kcalloc
+// Version min:   2.6.14 kzalloc
+// Version max: *
+//
+
+virtual context
+virtual patch
+virtual org
+virtual report
+
+//----------------------------------------------------------
+//  For context mode
+//----------------------------------------------------------
+
+@depends on context@
+type T;
+@@
+
+* (T *)
+  \(kmalloc\|kzalloc\|kcalloc\)(...)
+
+//----------------------------------------------------------
+//  For patch mode
+//----------------------------------------------------------
+
+@depends on patch@
+type T;
+@@
+
+- (T *)
+  \(kmalloc\|kzalloc\|kcalloc\)(...)
+
+//----------------------------------------------------------
+//  For org and report mode
+//----------------------------------------------------------
+
+@r depends on org || report@
+type ...
From: Nicolas Palix
Date: Monday, April 26, 2010 - 2:11 pm

This semantic patch replaces a pair of calls to kmalloc and memset
by a single call to kzalloc.

It only looks for simple cases to improve the confidence.

Signed-off-by: Nicolas Palix <npalix@diku.dk>
---
 scripts/smpl/kzalloc-simple.cocci |   88 +++++++++++++++++++++++++++++++++++++
 1 files changed, 88 insertions(+), 0 deletions(-)
 create mode 100644 scripts/smpl/kzalloc-simple.cocci

diff --git a/scripts/smpl/kzalloc-simple.cocci b/scripts/smpl/kzalloc-simple.cocci
new file mode 100644
index 0000000..d4e1b25
--- /dev/null
+++ b/scripts/smpl/kzalloc-simple.cocci
@@ -0,0 +1,88 @@
+///
+/// kzalloc should be used rather than kmalloc followed by memset 0
+///
+/// The semantic patch that makes this change is available
+/// in THISFILE.
+///
+/// More information about semantic patching is available at
+/// http://coccinelle.lip6.fr/
+///
+// Confidence: High
+// Copyright: (C) 2009-2010 Gilles Muller, Julia Lawall, Nicolas Palix, EMN, DIKU.  GPLv2.
+// URL: http://coccinelle.lip6.fr/rules/kzalloc.html
+// Options: -no_includes -include_headers
+//
+// Keywords: kmalloc, kzalloc
+// Version min: < 12 kmalloc
+// Version min:   14 kzalloc
+// Version max: *
+//
+
+virtual context
+virtual patch
+virtual org
+virtual report
+
+//----------------------------------------------------------
+//  For context mode
+//----------------------------------------------------------
+
+@depends on context@
+type T, T2;
+expression x;
+expression E1,E2;
+statement S;
+@@
+
+* x = (T)kmalloc(E1,E2);
+  if ((x==NULL) || ...) S
+* memset((T2)x,0,E1);
+
+//----------------------------------------------------------
+//  For patch mode
+//----------------------------------------------------------
+
+@depends on patch@
+type T, T2;
+expression x;
+expression E1,E2;
+statement S;
+@@
+
+- x = (T)kmalloc(E1,E2);
++ x = kzalloc(E1,E2);
+  if ((x==NULL) || ...) S
+- memset((T2)x,0,E1);
+
+//----------------------------------------------------------
+//  For org ...
From: Nicolas Palix
Date: Monday, April 26, 2010 - 2:11 pm

This semantic patch replaces explicit computations
of resource size by a call to resource_size.

Signed-off-by: Nicolas Palix <npalix@diku.dk>
---
 scripts/smpl/resource_size.cocci |  101 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 101 insertions(+), 0 deletions(-)
 create mode 100644 scripts/smpl/resource_size.cocci

diff --git a/scripts/smpl/resource_size.cocci b/scripts/smpl/resource_size.cocci
new file mode 100644
index 0000000..e5e24ac
--- /dev/null
+++ b/scripts/smpl/resource_size.cocci
@@ -0,0 +1,101 @@
+///
+/// Use resource_size function on resource object
+/// instead of explicit computation.
+///
+/// The semantic patch that makes this change is available
+/// in THISFILE.
+///
+/// More information about semantic patching is available at
+/// http://coccinelle.lip6.fr/
+///
+//  Confidence: High
+//  Copyright: (C) 2009, 2010 Nicolas Palix, DIKU.  GPLv2.
+//  Copyright: (C) 2009, 2010 Julia Lawall, DIKU.  GPLv2.
+//  Copyright: (C) 2009, 2010 Gilles Muller, EMN.  GPLv2.
+//  URL: http://coccinelle.lip6.fr/
+//  Options:
+//
+//  Keywords: resource_size
+//  Version min: 2.6.27 resource_size
+//  Version max: *
+//
+
+virtual context
+virtual patch
+virtual org
+virtual report
+
+//----------------------------------------------------------
+//  For context mode
+//----------------------------------------------------------
+
+@r_context depends on context && !patch && !org@
+struct resource *res;
+@@
+
+* (res->end - res->start) + 1
+
+//----------------------------------------------------------
+//  For patch mode
+//----------------------------------------------------------
+
+@r_patch depends on !context && patch && !org@
+struct resource *res;
+@@
+
+- (res->end - res->start) + 1
++ resource_size(res)
+
+//----------------------------------------------------------
+//  For org mode
+//----------------------------------------------------------
+
+
+@r_org depends on !context && !patch && (org || report)@
+struct ...
From: Wolfram Sang
Date: Tuesday, April 27, 2010 - 5:50 am

So, as I understood all semantic patches have to support 'org'?
I think this makes submitting slightly more complicated...

-- 
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
From: Julia Lawall
Date: Tuesday, April 27, 2010 - 5:53 am

No, this should not be the case.  It just won't do anything if you request 
org output and nothing is defined to produce org output.

julia
--

Previous thread: [PATCH] - New round-robin rotor for SLAB allocations by Jack Steiner on Monday, April 26, 2010 - 2:00 pm. (4 messages)

Next thread: Positive results with Transparent Hugepages at VMware by Philip Langdale on Monday, April 26, 2010 - 2:17 pm. (1 message)