2019-05-07 08:12:10

by Wen Yang

[permalink] [raw]
Subject: [PATCH] coccinelle: semantic patch for missing of_node_put

The call to of_parse_phandle()/of_find_node_by_name() ... returns a node
pointer with refcount incremented thus it must be explicitly decremented
after the last usage.

This SmPL is also looking for places where there is an of_node_put on
some path but not on others.

Suggested-by: Julia Lawall <[email protected]>
Signed-off-by: Wen Yang <[email protected]>
Cc: Julia Lawall <[email protected]>
Cc: Gilles Muller <[email protected]>
Cc: Nicolas Palix <[email protected]>
Cc: Michal Marek <[email protected]>
Cc: Masahiro Yamada <[email protected]>
Cc: [email protected]
---
scripts/coccinelle/free/of_node_put.cocci | 133 ++++++++++++++++++++++++++++++
1 file changed, 133 insertions(+)
create mode 100644 scripts/coccinelle/free/of_node_put.cocci

diff --git a/scripts/coccinelle/free/of_node_put.cocci b/scripts/coccinelle/free/of_node_put.cocci
new file mode 100644
index 0000000..304293c
--- /dev/null
+++ b/scripts/coccinelle/free/of_node_put.cocci
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0
+/// Find missing of_node_put
+///
+// Confidence: Moderate
+// Copyright: (C) 2018-2019 Wen Yang, ZTE.
+// Comments:
+// Options: --no-includes --include-headers
+
+virtual report
+virtual org
+
+@initialize:python@
+@@
+
+msg_prefix = "ERROR: missing of_node_put; acquired a node pointer with refcount incremented on line "
+msg_suffix = ", but without a corresponding object release within this function."
+
+seen = set()
+
+def add_if_not_present (p1, p2):
+ if (p1, p2) not in seen:
+ seen.add((p1, p2))
+ return True
+ return False
+
+@r1 exists@
+local idexpression struct device_node *x;
+expression e, e1;
+position p1, p2;
+identifier f;
+statement S;
+type T;
+@@
+
+(
+x = f@p1(...);
+... when != e = (T)x
+ when any
+ when != true x == NULL
+ when != of_node_put(x)
+ when != of_get_next_parent(x)
+ when != of_find_matching_node(x, ...)
+ when != if (x) { ... of_node_put(x) ... }
+ when != if (x) { ... return x; }
+ when != v4l2_async_notifier_add_fwnode_subdev(<...x...>)
+ when != e1 = of_fwnode_handle(x)
+(
+if (x) { ... when forall
+ of_node_put(x) ... }
+|
+return x;
+|
+return of_fwnode_handle(x);
+|
+return@p2 ...;
+)
+&
+x = f(...)
+...
+if (<+...x...+>) S
+...
+of_node_put(x);
+)
+
+@script:python depends on report && r1@
+p1 << r1.p1;
+p2 << r1.p2;
+@@
+
+if(add_if_not_present(p1[0].line, p2[0].line)):
+ coccilib.report.print_report(p2[0], msg_prefix + p1[0].line + msg_suffix)
+
+@script:python depends on org && r1@
+p1 << r1.p1;
+p2 << r1.p2;
+@@
+
+cocci.print_main("acquired a node pointer with refcount incremented", p1)
+cocci.print_secs("needed of_node_put", p2)
+
+@r2 exists@
+local idexpression struct device_node *x;
+expression e, e1;
+position p1, p2;
+statement S;
+type T;
+@@
+
+x = @p1\(of_find_compatible_node\|of_find_node_by_name\|of_parse_phandle\|
+ of_find_node_by_type\|of_find_node_by_name\|of_find_all_nodes\|
+ of_get_cpu_node\|of_get_parent\|of_get_next_parent\|
+ of_get_next_child\|of_get_next_available_child\|of_get_next_cpu_node\|
+ of_get_compatible_child\|of_get_child_by_name\|of_find_node_opts_by_path\|
+ of_find_node_with_property\|of_find_matching_node_and_match\|of_find_node_by_phandle\|
+ of_parse_phandle\)(...);
+...
+if (x == NULL || ...) S
+... when != e = (T)x
+ when any
+ when != true x == NULL
+ when != of_node_put(x)
+ when != of_get_next_parent(x)
+ when != of_find_matching_node(x, ...)
+ when != if (x) { ... of_node_put(x) ... }
+ when != if (x) { ... return x; }
+ when != v4l2_async_notifier_add_fwnode_subdev(<...x...>)
+ when != e1 = of_fwnode_handle(x)
+(
+if (x) { ... when forall
+ of_node_put(x) ... }
+|
+return x;
+|
+return of_fwnode_handle(x);
+|
+return@p2 ...;
+)
+
+@script:python depends on report && r2@
+p1 << r2.p1;
+p2 << r2.p2;
+@@
+
+if(add_if_not_present(p1[0].line, p2[0].line)):
+ coccilib.report.print_report(p2[0], msg_prefix + p1[0].line + msg_suffix)
+
+@script:python depends on org && r2@
+p1 << r2.p1;
+p2 << r2.p2;
+@@
+
+cocci.print_main("acquired a node pointer with refcount incremented", p1)
+cocci.print_secs("needed of_node_put", p2)
--
2.9.5


2019-05-07 15:29:09

by Markus Elfring

[permalink] [raw]
Subject: Re: [PATCH] coccinelle: semantic patch for missing of_node_put

> The call to of_parse_phandle()/of_find_node_by_name() ... returns a node
> pointer with refcount incremented thus it must be explicitly decremented
> after the last usage.
>
> This SmPL is also looking for places where there is an of_node_put on
> some path but not on others.

I suggest to improve this commit description.

* Possible wording:
There are functions which increment a reference counter for a device node.
These functions belong to a programming interface for the management
of information from device trees.
The counter must be decremented after the last usage of a device node.

This SmPL script looks also for places where a of_node_put() call is on
some paths but not on others.

* Will the word “patch” be replaced by “code search” in the commit subject
because the operation modes “report” and “org” are supported here?


> +@initialize:python@
> +@@

Such a SmPL rule would apply to every possible operation mode.
I have noticed then that the two Python variables from here will be needed
only in two SmPL rules which depend on the mode “report”.

* Thus I would prefer to adjust the dependency specification accordingly.

* Please replace these variables by a separate function like
the following.
def display1(p1 ,p2):
if add_if_not_present(p1[0].line, p2[0].line):
coccilib.report.print_report(p2[0],
"prefix"
+ p1[0].line
+ "suffix")


* Please move another bit of duplicate code to a separate function like
the following.
def display2(p1 ,p2):
cocci.print_main("Choose info 1", p1)
cocci.print_secs("Choose info 2", p2)


> +x = @p1\(of_find_compatible_node\|of_find_node_by_name\|of_parse_phandle\|

If you would like to insist to use such a SmPL disjunction, I would prefer
an other code formatting here.
How do you think about to put each function name on a separate line?

Can such a name list be ever automatically determined from an other
information source?
(Are there circumstances to consider under which the application of
a detailed regular expression would become interesting for a SmPL constraint?)

Will it be influenced by any sort criteria?


> + when != of_node_put(x)

> + when != if (x) { ... of_node_put(x) ... }

I find the second when constraint specification unnecessary because
the previous one should be sufficient to exclude such a function call.


Can the specification “when != \( of_node_put \| of_get_next_parent \) (x)”
be useful?


> +return x;
> +|
> +return of_fwnode_handle(x);

Can it be nicer to merge this bit of code into another SmPL disjunction?

+return \( x \| of_fwnode_handle(x) \);


Regards,
Markus