diff options
author | Joe Perches <joe@perches.com> | 2014-08-06 19:10:27 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-08-06 21:01:27 -0400 |
commit | ebfdc40969f24fc0cdd1349835d36e8ebae05374 (patch) | |
tree | d43881f65568d89560feb76177bf9e959df97113 /scripts | |
parent | 74e765319084bd2940a9612ada961f0f7385936c (diff) |
checkpatch: attempt to find unnecessary 'out of memory' messages
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>
Acked-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/checkpatch.pl | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 182be0f12407..ac1d6b0a6f09 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -4014,6 +4014,23 @@ sub process { | |||
4014 | } | 4014 | } |
4015 | } | 4015 | } |
4016 | 4016 | ||
4017 | # check for unnecessary "Out of Memory" messages | ||
4018 | if ($line =~ /^\+.*\b$logFunctions\s*\(/ && | ||
4019 | $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && | ||
4020 | (defined $1 || defined $3) && | ||
4021 | $linenr > 3) { | ||
4022 | my $testval = $2; | ||
4023 | my $testline = $lines[$linenr - 3]; | ||
4024 | |||
4025 | my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); | ||
4026 | # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); | ||
4027 | |||
4028 | if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) { | ||
4029 | WARN("OOM_MESSAGE", | ||
4030 | "Possible unnecessary 'out of memory' message\n" . $hereprev); | ||
4031 | } | ||
4032 | } | ||
4033 | |||
4017 | # check for bad placement of section $InitAttribute (e.g.: __initdata) | 4034 | # check for bad placement of section $InitAttribute (e.g.: __initdata) |
4018 | if ($line =~ /(\b$InitAttribute\b)/) { | 4035 | if ($line =~ /(\b$InitAttribute\b)/) { |
4019 | my $attr = $1; | 4036 | my $attr = $1; |