All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH] checkpatch: Attempt to find unnecessary 'out of memory' messages
@ 2014-06-10 16:55 Joe Perches
  2014-06-11  7:47 ` Dan Carpenter
  0 siblings, 1 reply; 17+ messages in thread
From: Joe Perches @ 2014-06-10 16:55 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Dan Carpenter, Andy Whitcroft, LKML, Julia Lawall

Logging messages that show some type of "out of memory" error
are generally unnecessary as there is a generic message and
a stack dump done by the memory subsystem.

These messages generally increase kernel size without much
added value.

Emit a warning on these types of messages.

This test looks for any inserted message function, then looks
at the previous line for an "if (!foo)" or "if (foo == NULL)"
test and then looks at the preceding statement for an allocation
function like "foo = kmalloc()"

ie: this code matches:

	foo = kmalloc();
	if (foo == NULL) {
		printk("Out of memory\n");
		return -ENOMEM;
	}

This test is very crude and incomplete.

This test can miss quite a lot of of OOM messages that do not
have this specific form.

ie: this code does not match:

	foo = kmalloc();
	if (!foo) {
		rtn = -ENOMEM;
		printk("Out of memory!\n");
		goto out;
	}

This test could also be a false positive when the logging
message itself does not specify anything about memory, but
I did not find any false positives in my limited testing.

spatch could be a better solution but correctness seems
non-trivial for that tool too.

Signed-off-by: Joe Perches <joe@perches.com>
---
 scripts/checkpatch.pl | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 010b18e..64bee42 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4009,6 +4009,23 @@ sub process {
 			}
 		}
 
+# check for unnecessary "Out of Memory" messages
+		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
+		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
+		    (defined $1 || defined $3) &&
+		    $linenr > 3) {
+			my $testval = $2;
+			my $testline = $lines[$linenr - 3];
+
+			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
+#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
+
+			if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
+				WARN("OOM_MESSAGE",
+				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
+			}
+		}
+
 # check for bad placement of section $InitAttribute (e.g.: __initdata)
 		if ($line =~ /(\b$InitAttribute\b)/) {
 			my $attr = $1;



^ permalink raw reply related	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2014-06-20 22:04 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-10 16:55 [RFC PATCH] checkpatch: Attempt to find unnecessary 'out of memory' messages Joe Perches
2014-06-11  7:47 ` Dan Carpenter
2014-06-11 16:01   ` Joe Perches
2014-06-14  7:40     ` Julia Lawall
2014-06-14  8:30       ` Joe Perches
2014-06-14  9:51       ` Julia Lawall
2014-06-18 19:33         ` Paul E. McKenney
2014-06-18 19:49           ` Joe Perches
2014-06-19 21:00             ` Paul E. McKenney
2014-06-19 23:24               ` [RFC PATCH] use snprintf instead of sprintf in rcu_torture_printk Pranith Kumar
2014-06-19 23:33                 ` Joe Perches
2014-06-20  0:11                   ` Pranith Kumar
2014-06-19 23:49                 ` Paul E. McKenney
2014-06-20  0:13                   ` Pranith Kumar
2014-06-20  4:54                     ` Paul E. McKenney
2014-06-20 22:04                       ` Joe Perches
2014-06-18 21:18           ` [RFC PATCH] checkpatch: Attempt to find unnecessary 'out of memory' messages Julia Lawall

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.