[RFC PATCH 57/86] coccinelle: script to remove cond_resched()

From: Ankur Arora
Date: Tue Nov 07 2023 - 18:09:43 EST


Rudimentary script to remove the straight-forward subset of
cond_resched() and allies:

1) if (need_resched())
cond_resched()

2) expression*;
cond_resched(); /* or in the reverse order */

3) if (expression)
statement
cond_resched(); /* or in the reverse order */

The last two patterns depend on the control flow level to ensure
that the complex cond_resched() patterns (ex. conditioned ones)
are left alone and we only pick up ones which are only minimally
related the neighbouring code.

Cc: Julia Lawall <Julia.Lawall@xxxxxxxx>
Cc: Nicolas Palix <nicolas.palix@xxxxxxx>
Signed-off-by: Ankur Arora <ankur.a.arora@xxxxxxxxxx>
---
scripts/coccinelle/api/cond_resched.cocci | 53 +++++++++++++++++++++++
1 file changed, 53 insertions(+)
create mode 100644 scripts/coccinelle/api/cond_resched.cocci

diff --git a/scripts/coccinelle/api/cond_resched.cocci b/scripts/coccinelle/api/cond_resched.cocci
new file mode 100644
index 000000000000..bf43768a8f8c
--- /dev/null
+++ b/scripts/coccinelle/api/cond_resched.cocci
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// Remove naked cond_resched() statements
+///
+//# Remove cond_resched() statements when:
+//# - executing at the same control flow level as the previous or the
+//# next statement (this lets us avoid complicated conditionals in
+//# the neighbourhood.)
+//# - they are of the form "if (need_resched()) cond_resched()" which
+//# is always safe.
+//#
+//# Coccinelle generally takes care of comments in the immediate neighbourhood
+//# but might need to handle other comments alluding to rescheduling.
+//#
+virtual patch
+virtual context
+
+@ r1 @
+identifier r;
+@@
+
+(
+ r = cond_resched();
+|
+-if (need_resched())
+- cond_resched();
+)
+
+@ r2 @
+expression E;
+statement S,T;
+@@
+(
+ E;
+|
+ if (E) S
+|
+ if (E) S else T
+|
+)
+-cond_resched();
+
+@ r3 @
+expression E;
+statement S,T;
+@@
+-cond_resched();
+(
+ E;
+|
+ if (E) S
+|
+ if (E) S else T
+)
--
2.31.1