Skip to contentSkip to conversation

coccinelle: mini_lock: improve performance when searching loops

PATCH In review Sang-Heon Jeon 2 participants 2 messages Jul 27 – Aug 6 Coccinelle/Semantic Patches (SmPL)

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 coccinelle: mini_lock: improve performance when searching loops

The 'looped' rule collects the returns inside a for loop to prevent 'err' from reporting them. It searches every for loop in the file, and on files with large loop bodies the search explodes.

For example, kernel/bpf/verifier.c runs for over 200 seconds, almost entirely in 'looped' according to --profile. Since the kernel .cocciconfig sets a 200 second timeout, coccicheck silently skips the file.

To avoid this, collect the candidate returns first, so that 'looped' checks only those positions. 'err' then excludes what 'looped' found.

Every return that 'err' can report is also a candidate, so the same returns are excluded as before and the output does not change. A report-mode run over every .c file in the tree produces identical output.

So verifier.c now finishes well within the timeout, in a few seconds.

---
Changes from v1 [1]
- remove unnecessary depends keyword as Julia suggested
- add exists keyword to 'looped' rule

[1] https://lore.kernel.org/all/[email protected]/

---
 scripts/coccinelle/locks/mini_lock.cocci | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
scripts/coccinelle/locks/mini_lock.cocci +22 -2
index 71065d8a5d54..c65241c895ff 100644
@@ -53,11 +53,31 @@ spin_lock_irq@p1
 spin_lock_irqsave@p1
 ) (E1@p,...);
 
-@looped@
+@err_candidate exists@
+expression E1;
+position prelocked.p;
+position up != prelocked.p1;
+position rc;
+identifier lock,unlock;
+@@
+
+lock(E1@p,...);
+... when != E1
+    when any
+if (...) {
+  ... when != E1
+  return@rc ...;
+}
+... when != E1
+    when any
+unlock@up(E1,...);
+
+@looped exists@
+position err_candidate.rc;
 position r;
 @@
 
-for(...;...;...) { <+... return@r ...; ...+> }
+for(...;...;...) { <+... return@rc@r ...; ...+> }
 
 @err exists@
 expression E1;
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 13 lines · Sang-Heon Jeon

The 'looped' rule collects the returns inside a for loop to prevent 'err' from reporting them. It searches every for loop in the file, and on files with large loop bodies the search explodes.

For example, kernel/bpf/verifier.c runs for over 200 seconds, almost entirely in 'looped' according to --profile. Since the kernel .cocciconfig sets a 200 second timeout, coccicheck silently skips the file.

To avoid this, collect the candidate returns first, so that 'looped' checks only those positions. 'err' then excludes what 'looped' found.

Quoted 7 lines

Every return that 'err' can report is also a candidate, so the same returns are excluded as before and the output does not change. A report-mode run over every .c file in the tree produces identical output.

So verifier.c now finishes well within the timeout, in a few seconds.

Thanks for the fixes. Applied.

julia

Quoted 54 lines

---
Changes from v1 [1]
- remove unnecessary depends keyword as Julia suggested
- add exists keyword to 'looped' rule

[1] https://lore.kernel.org/all/[email protected]/

---
 scripts/coccinelle/locks/mini_lock.cocci | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
scripts/coccinelle/locks/mini_lock.cocci +22 -3
index 71065d8a5d54..c65241c895ff 100644
@@ -53,11 +53,31 @@ spin_lock_irq@p1
 spin_lock_irqsave@p1
 ) (E1@p,...);
 
-@looped@
+@err_candidate exists@
+expression E1;
+position prelocked.p;
+position up != prelocked.p1;
+position rc;
+identifier lock,unlock;
+@@
+
+lock(E1@p,...);
+... when != E1
+    when any
+if (...) {
+  ... when != E1
+  return@rc ...;
+}
+... when != E1
+    when any
+unlock@up(E1,...);
+
+@looped exists@
+position err_candidate.rc;
 position r;
 @@
 
-for(...;...;...) { <+... return@r ...; ...+> }
+for(...;...;...) { <+... return@rc@r ...; ...+> }
 
 @err exists@
 expression E1;
--

2.43.0