* Re: [PATCH] coccinelle: api: add kvmalloc script
[not found] <cd186837-3753-436a-b1f6-7e1dfe5ff5e0@web.de>
@ 2020-08-03 13:21 ` Julia Lawall
0 siblings, 0 replies; 2+ messages in thread
From: Julia Lawall @ 2020-08-03 13:21 UTC (permalink / raw)
To: Markus Elfring
Cc: Denis Efremov, Gilles Muller, Julia Lawall, Masahiro Yamada,
Michal Marek, Nicolas Palix, Coccinelle, linux-kernel,
kernel-janitors
[-- Attachment #1: Type: text/plain, Size: 1990 bytes --]
On Mon, 3 Aug 2020, Markus Elfring wrote:
> …
> > +++ b/scripts/coccinelle/api/kvmalloc.cocci
> …
> > +@opportunity depends on !patch@
> > +expression E, E1, size;
> > +position p;
> > +@@
> > +
> > +(
> …
> > +|
> > +* E = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\|
> > +* kmalloc_array\|kmalloc_array_node\|kcalloc_node\)(..., size, ...)
> > + ... when != E = E1
> > + when != size = E1
> > + when any
> > +* if (\(!E\|E == NULL\))@p {
Actually, you shouldn't need both E == NULL and !E. E == NULL should be
sufficient.
julia
> …
>
> I suggest to extend the SmPL code exclusion specification so that
> pointer dereferences will be filtered out before the shown null pointer detection.
>
>
> > +@depends on patch@
> > +expression E, E1, flags, size, node;
> > +identifier x;
> > +type T;
> > +@@
> > +
> > +(
> > +- if (\(size <= E1\|size < E1\|size == E1\|size > E1\))
>
> This condition check is repeated a few times.
> Thus I imagine that the usage of another metavariable with a SmPL constraint
> can eventually be helpful.
>
> +binary operator bo = {<=, <, ==, >};
> …
> +-if (size bo E1)
>
>
>
> > +- E = kmalloc(size, flags);
> > +- else
> > +- E = vmalloc(size);
>
> Will it be interesting to support also the use of conditional operators
> by another part of a SmPL disjunction?
>
> -E = (…) ? kmalloc(size, flags) : vmalloc(size);
>
>
> > ++ E = kvmalloc(size, flags);
> > +|
> > +- E = kmalloc(size, flags | __GFP_NOWARN);
> > +- if (\(!E\|E == NULL\))
> > +- E = vmalloc(size);
> > ++ E = kvmalloc(size, flags);
>
> This source code replacement line is repeated. Thus I imagine
> that it would be nice if such SmPL code duplication could be avoided
> by the application of another SmPL disjunction.
> Unfortunately, the software “Coccinelle 1.0.8-00146-g04f36d53” presents
> the error message “15: no available token to attach to” then.
> Would you like to adjust anything in this area?
>
> Regards,
> Markus
>
^ permalink raw reply [flat|nested] 2+ messages in thread
* [PATCH] coccinelle: api: add kvmalloc script
@ 2020-08-03 10:56 Denis Efremov
0 siblings, 0 replies; 2+ messages in thread
From: Denis Efremov @ 2020-08-03 10:56 UTC (permalink / raw)
To: Julia Lawall; +Cc: Denis Efremov, cocci, linux-kernel
Suggest kvmalloc instead of opencoded kmalloc && vmalloc condition.
Signed-off-by: Denis Efremov <efremov@linux.com>
---
If coccinelle fails with "Segmentation fault" during analysis, then
one needs to increase stack limit, e.g. ulimit -s 32767.
Current, I've sent only one patch for this rule and will send the rest
after the merge window. https://lkml.org/lkml/2020/7/31/986
scripts/coccinelle/api/kvmalloc.cocci | 127 ++++++++++++++++++++++++++
1 file changed, 127 insertions(+)
create mode 100644 scripts/coccinelle/api/kvmalloc.cocci
diff --git a/scripts/coccinelle/api/kvmalloc.cocci b/scripts/coccinelle/api/kvmalloc.cocci
new file mode 100644
index 000000000000..76d6aeab7c09
--- /dev/null
+++ b/scripts/coccinelle/api/kvmalloc.cocci
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Find conditions in code for kmalloc/vmalloc calls.
+/// Suggest to use kvmalloc instead.
+///
+// Confidence: High
+// Copyright: (C) 2020 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+//
+
+virtual patch
+virtual report
+virtual org
+virtual context
+
+@opportunity depends on !patch@
+expression E, E1, size;
+position p;
+@@
+
+(
+* if (\(size <= E1\|size < E1\|size == E1\|size > E1\) || ...)@p {
+ ...
+* E = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\|
+* kmalloc_array\|kmalloc_array_node\|kcalloc_node\)(..., size, ...)
+ ...
+ } else {
+ ...
+* E = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...)
+ ...
+ }
+|
+* E = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\|
+* kmalloc_array\|kmalloc_array_node\|kcalloc_node\)(..., size, ...)
+ ... when != E = E1
+ when != size = E1
+ when any
+* if (\(!E\|E == NULL\))@p {
+ ...
+* E = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...)
+ ...
+ }
+)
+
+@depends on patch@
+expression E, E1, flags, size, node;
+identifier x;
+type T;
+@@
+
+(
+- if (\(size <= E1\|size < E1\|size == E1\|size > E1\))
+- E = kmalloc(size, flags);
+- else
+- E = vmalloc(size);
++ E = kvmalloc(size, flags);
+|
+- E = kmalloc(size, flags | __GFP_NOWARN);
+- if (\(!E\|E == NULL\))
+- E = vmalloc(size);
++ E = kvmalloc(size, flags);
+|
+- T x = kmalloc(size, flags | __GFP_NOWARN);
+- if (\(!x\|x == NULL\))
+- x = vmalloc(size);
++ T x = kvmalloc(size, flags);
+|
+- if (\(size <= E1\|size < E1\|size == E1\|size > E1\))
+- E = kzalloc(size, flags);
+- else
+- E = vzalloc(size);
++ E = kvzalloc(size, flags);
+|
+- E = kzalloc(size, flags | __GFP_NOWARN);
+- if (\(!E\|E == NULL\))
+- E = vzalloc(size);
++ E = kvzalloc(size, flags);
+|
+- T x = kzalloc(size, flags | __GFP_NOWARN);
+- if (\(!x\|x == NULL\))
+- x = vzalloc(size);
++ T x = kvzalloc(size, flags);
+|
+- if (\(size <= E1\|size < E1\|size == E1\|size > E1\))
+- E = kmalloc_node(size, flags, node);
+- else
+- E = vmalloc_node(size, node);
++ E = kvmalloc_node(size, flags, node);
+|
+- E = kmalloc_node(size, flags | __GFP_NOWARN, node);
+- if (\(!E\|E == NULL\))
+- E = vmalloc_node(size, node);
++ E = kvmalloc_node(size, flags, node);
+|
+- T x = kmalloc_node(size, flags | __GFP_NOWARN, node);
+- if (\(!x\|x == NULL\))
+- x = vmalloc_node(size, node);
++ T x = kvmalloc_node(size, flags, node);
+|
+- if (\(size <= E1\|size < E1\|size == E1\|size > E1\))
+- E = kvzalloc_node(size, flags, node);
+- else
+- E = vzalloc_node(size, node);
++ E = kvzalloc_node(size, flags, node);
+|
+- E = kvzalloc_node(size, flags | __GFP_NOWARN, node);
+- if (\(!E\|E == NULL\))
+- E = vzalloc_node(size, node);
++ E = kvzalloc_node(size, flags, node);
+|
+- T x = kvzalloc_node(size, flags | __GFP_NOWARN, node);
+- if (\(!x\|x == NULL\))
+- x = vzalloc_node(size, node);
++ T x = kvzalloc_node(size, flags, node);
+)
+
+@script: python depends on report@
+p << opportunity.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING: opportunity for kvmalloc")
+
+@script: python depends on org@
+p << opportunity.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING: opportunity for kvmalloc")
--
2.26.2
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-08-03 13:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <cd186837-3753-436a-b1f6-7e1dfe5ff5e0@web.de>
2020-08-03 13:21 ` [PATCH] coccinelle: api: add kvmalloc script Julia Lawall
2020-08-03 10:56 Denis Efremov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®