Skip to contentSkip to conversation

scripts/coccinelle/generation: add example generation script

PATCH In review Luis Chamberlain 3 participants 5 messages Apr 3, 2025 – Apr 6, 2025 Coccinelle/Semantic Patches (SmPL)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts/coccinelle/generation: add example generation script

To do complicated tasks it is sometimes a bit difficult to write a coccinelle rule. Such is the case when you want to use the iterator, which can be used to help check for nested set of calls.

An example use case of this is when we want to check if a routine may call an atomic context, or if a routine is calling any known existing sleep routines.

This adds some initial examples we can go and try to enhance over time with some very specific specialized tasks. What we see is it is even hard to have code generate code (cocci python code) which we can easily maintain.

Fortunately AI does *grok* this. So no shame, *different* gen AI agents helped me get these to where they are. I suspect we'll need gen AIs agents to continue to maintain them as well and enhance them.

What we should do is once we have high confidence is start using it for tests to ensure we respect some golden rules. These all take a long time to run because of the nested stuff. Its complicated.

I wrote these try to help with a bug we're trying to fix upstream [0] on determining if __find_get_block_slow() really can't block and ... determining exactly *why*.

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

---

Posting this in case this is useful to others. I'll be merging it to kdevops now, but long term if we find value, we can move to Linux too. It has low confidence for now and so needs much more work and a bit more of AI & human love.

 .../generation/check_for_atomic_calls.py      | 443 ++++++++++++
 .../generation/check_for_sleepy_calls.py      | 678 ++++++++++++++++++
 2 files changed, 1121 insertions(+)
 create mode 100755 scripts/coccinelle/generation/check_for_atomic_calls.py
 create mode 100755 scripts/coccinelle/generation/check_for_sleepy_calls.py
scripts/coccinelle/generation/check_for_atomic_calls.py new file +443 -0
new file mode 100755
index 000000000000..5849f9643de5
@@ -0,0 +1,443 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2025 Luis Chamberlain <[email protected]>
+# SPDX-License-Identifier: GPL-2.0-or-later OR copyleft-next-0.3.1
+
+# Generates a Coccinelle file which can be used to track down if a routine
+# can sleep up to --max-depth levels (defauilt is 1). You can either specify
+# the sleep routine you want to check for, or you can use our built-in db.
+# Huge work in progress, but its a start.
+#
+# This is work in progress.
+# Confidence: low
+
+import multiprocessing
+import argparse
+
+"""
+Generate a Coccinelle semantic patch that checks for atomic context
+in any transitive caller (up to N levels) of a target function.
+Example usage:
+    ./check_for_atomic_calls.py --levels 5 --target __find_get_block_slow --output atomic_check_find_block.cocci
+    make coccicheck MODE=report COCCI=find_block_check.cocci
+For an obvious atomic call:
+    ./check_for_atomic_calls.py --levels 5 --target netif_rx --output netif_rx.cocci
+    make coccicheck MODE=report COCCI=netif_rx.cocci
+"""
+parser = argparse.ArgumentParser(
+    description="Generate a Coccinelle checker for atomic context in transitive callers of a target function."
+)
+parser.add_argument(
+    "--levels", "-l",
+    type=int,
+    required=True,
+    help="Maximum number of transitive caller levels to follow (e.g., 5)"
+)
+parser.add_argument(
+    "--target", "-t",
+    type=str,
+    required=True,
+    help="Target function to trace (e.g., __find_get_block_slow)"
+)
+parser.add_argument(
+    "--output", "-o",
+    type=str,
+    required=True,
+    help="Output .cocci file to generate"
+)
+args = parser.parse_args()
+max_depth = args.levels
+target_func = args.target
+
+# Add a function to get the number of processors for parallel jobs
+def get_nprocs():
+    try:
+        return multiprocessing.cpu_count()
+    except:
+        return 1  # Default to 1 if can't determine
+
+outfile = args.output
+header = f"""// SPDX-License-Identifier: GPL-2.0
+/// Autogenerated by gen_atomic_context_chain.py
+/// Detect atomic context in ANY transitive caller (up to {max_depth} levels)
+/// of `{target_func}`
+// Options: --no-includes --include-headers
+virtual after_start
+virtual report
+@initialize:python@
+@@
+seen = set()
+seen_atomic = set()
+seen_irq_regions = set()
+seen_spinlock_regions = set()
+
+def register_caller(fn, file):
+    if fn not in seen:
+        seen.add(fn)
+        it = Iteration()
+        if file is not None:
+            it.set_files([file])
+        it.add_virtual_rule("after_start")
+        it.add_virtual_identifier("transitive_caller", fn)
+        it.register()
+
+// Look for direct calls to the target function
+@seed@
+identifier fn;
+position p;
+@@
+fn@p(...) {{
+  <+... {target_func}(...); ...+>
+}}
+
+@script:python depends on seed@
+fn << seed.fn;
+p  << seed.p;
+@@
+print(f"🌱 SEED HIT: {{fn}} calls {target_func} at {{p[0].file}}:{{p[0].line}}")
+register_caller(fn, p[0].file)
+
+// Special pattern for IRQ handler detection in macro definitions
+@irq_handler_def@
+identifier fn;
+@@
+(
+DEFINE_IRQ_HANDLER(fn, ...)
+|
+DECLARE_TASKLET(fn, ...)
+)
+
+@script:python depends on irq_handler_def@
+fn << irq_handler_def.fn;
+@@
+print(f"⚑ DEFINED IRQ HANDLER: {{fn}}")
+register_caller(fn, None)
+
+// Look for irq-related function names that haven't been caught yet
+@irq_func_names@
+identifier fn =~ "(_irq|_intr|_isr|_napi|_poll|_tasklet|_softirq|_bh)$";
+@@
+
+fn(...) {{ ... }}
+
+@script:python depends on irq_func_names@
+fn << irq_func_names.fn;
+@@
+print(f"⚑ NAMED IRQ HANDLER: {{fn}}")
+register_caller(fn, None)
+
+// Look for functions with interrupt prefix naming patterns
+@interrupt_prefixed@
+identifier fn =~ "^(irq_|intr_|isr_|napi_|poll_|do_softirq|tasklet_)";
+@@
+
+fn(...) {{ ... }}
+
+@script:python depends on interrupt_prefixed@
+fn << interrupt_prefixed.fn;
+@@
+print(f"⚑ PREFIXED IRQ HANDLER: {{fn}}")
+register_caller(fn, None)
+"""
+with open(outfile, "w") as f:
+    f.write(header)
+    
+    # Generate all the caller chain rules
+    for level in range(1, max_depth + 1):
+        f.write(f"""
+// Level {level} caller discovery
+@caller{level} depends on after_start exists@
+identifier virtual.transitive_caller;
+identifier fn;
+position p;
+@@
+fn@p(...) {{
+  <+... transitive_caller(...); ...+>
+}}
+
+@script:python depends on caller{level}@
+fn << caller{level}.fn;
+p << caller{level}.p;
+transitive_caller << virtual.transitive_caller;
+@@
+print(f"πŸ”„ Chain level {level}: {{fn}} calls {{transitive_caller}} at {{p[0].file}}:{{p[0].line}}")
+register_caller(fn, p[0].file)
+""")
+
+    # Check for atomic context in each caller in our chain
+    for level in range(1, max_depth + 1):
+        # First, check for common atomic primitives
+        f.write(f"""
+// Level {level} atomic context check - Common atomic primitives
+@atomiccheck{level} depends on after_start exists@
+identifier virtual.transitive_caller;
+position p1, p2;
+@@
+(
+spin_lock@p1(...)
+|
+spin_lock_irq@p1(...)
+|
+spin_lock_irqsave@p1(...)
+|
+spin_lock_bh@p1(...)
+|
+read_lock@p1(...)
+|
+read_lock_irq@p1(...)
+|
+read_lock_irqsave@p1(...)
+|
+read_lock_bh@p1(...)
+|
+write_lock@p1(...)
+|
+write_lock_irq@p1(...)
+|
+write_lock_irqsave@p1(...)
+|
+write_lock_bh@p1(...)
+|
+raw_spin_lock@p1(...)
+|
+raw_spin_lock_irq@p1(...)
+|
+raw_spin_lock_irqsave@p1(...)
+|
+raw_spin_lock_bh@p1(...)
+|
+local_irq_disable@p1()
+|
+local_irq_save@p1(...)
+|
+local_bh_disable@p1()
+|
+preempt_disable@p1()
+|
+in_atomic@p1()
+|
+in_atomic_preempt_off@p1()
+|
+in_interrupt@p1()
+|
+in_irq@p1()
+|
+in_serving_softirq@p1()
+|
+in_nmi@p1()
+|
+in_task@p1()
+|
+rcu_read_lock@p1()
+|
+irq_enter@p1()
+|
+napi_disable@p1(...)
+)
+...
+transitive_caller@p2(...)
+
+@script:python depends on atomiccheck{level}@
+p1 << atomiccheck{level}.p1;
+p2 << atomiccheck{level}.p2;
+transitive_caller << virtual.transitive_caller;
+@@
+key = (p1[0].file, p1[0].line, transitive_caller)
+if key not in seen_atomic:
+    seen_atomic.add(key)
+    print(f"⚠️  WARNING: atomic context at level {level}: {{p1[0].current_element}} at {{p1[0].file}}:{{p1[0].line}} may reach {{transitive_caller}}() β†’ eventually {target_func}()")
+""")
+
+        # Check for lock-related functions directly calling our chain
+        f.write(f"""
+// Level {level} atomic context check - Lock-related functions
+@atomic_fn_check{level} depends on after_start exists@
+identifier virtual.transitive_caller;
+identifier lock_fn;
+position p;
+@@
+// Common locking function patterns
+lock_fn(...) {{
+  ... when != unlock_irqrestore(...)
+      when != local_irq_restore(...)
+      when != spin_unlock(...)
+      when != rcu_read_unlock(...)
+  transitive_caller@p(...)
+  ...
+}}
+
+@script:python depends on atomic_fn_check{level}@
+lock_fn << atomic_fn_check{level}.lock_fn;
+p << atomic_fn_check{level}.p;
+transitive_caller << virtual.transitive_caller;
+@@
+# Only report functions with lock-related names
+atomic_keywords = ['lock', 'irq', 'atomic', 'bh', 'intr', 'preempt', 'disable', 'napi', 'rcu']
+if any(kw in lock_fn.lower() for kw in atomic_keywords):
+    print(f"⚠️  WARNING: potential atomic function at level {level}: {{lock_fn}} (name suggests lock handling) contains call to {{transitive_caller}}() at {{p[0].file}}:{{p[0].line}} β†’ eventually {target_func}()")
+""")
+
+        # Check for spinlock regions
+        f.write(f"""
+// Level {level} spinlock region check
+@spinlock_region{level} depends on after_start exists@
+identifier virtual.transitive_caller;
+expression E1, flags;
+position p1, p3;
+@@
+(
+spin_lock@p1(E1,...)
+|
+spin_lock_irq@p1(E1,...)
+|
+spin_lock_irqsave@p1(E1, flags)
+|
+spin_trylock@p1(E1)
+|
+raw_spin_lock@p1(E1,...)
+|
+raw_spin_lock_irq@p1(E1,...)
+|
+raw_spin_lock_irqsave@p1(E1, flags)
+|
+raw_spin_trylock@p1(E1)
+)
+... when != spin_unlock(E1,...)
+    when != spin_unlock_irq(E1,...)
+    when != spin_unlock_irqrestore(E1, flags)
+    when != raw_spin_unlock(E1,...)
+    when != raw_spin_unlock_irq(E1,...)
+    when != raw_spin_unlock_irqrestore(E1, flags)
+transitive_caller@p3(...)
+
+@script:python depends on spinlock_region{level}@
+p1 << spinlock_region{level}.p1;
+p3 << spinlock_region{level}.p3;
+transitive_caller << virtual.transitive_caller;
+@@
+key = (p1[0].file, p1[0].line, p3[0].line, transitive_caller)
+if key not in seen_spinlock_regions:
+    seen_spinlock_regions.add(key)
+    print(f"⚠️  WARNING: spinlock region at level {level}: {{p1[0].current_element}} at {{p1[0].file}}:{{p1[0].line}} contains call to {{transitive_caller}}() at line {{p3[0].line}} β†’ eventually {target_func}()")
+""")
+
+        # Look for functions that can't sleep
+        f.write(f"""
+// Level {level} check - Can't sleep contexts
+@cant_sleep{level} depends on after_start exists@
+identifier virtual.transitive_caller;
+position p1, p2;
+@@
+(
+GFP_ATOMIC@p1
+|
+cond_resched@p1()
+|
+__GFP_ATOMIC@p1
+|
+DECLARE_COMPLETION@p1(...)
+)
+...
+transitive_caller@p2(...)
+
+@script:python depends on cant_sleep{level}@
+p1 << cant_sleep{level}.p1;
+p2 << cant_sleep{level}.p2;
+transitive_caller << virtual.transitive_caller;
+@@
+print(f"⚠️  WARNING: Non-sleeping context at {{p1[0].file}}:{{p1[0].line}} but calls {{transitive_caller}}() at line {{p2[0].line}} β†’ eventually {target_func}()")
+""")
+
+        # Check for network driver contexts
+        f.write(f"""
+// Level {level} check - Network driver contexts (commonly atomic)
+@netdriver{level} depends on after_start exists@
+identifier virtual.transitive_caller;
+position p1, p2;
+@@
+(
+alloc_skb@p1(...)
+|
+netif_receive_skb@p1(...)
+|
+netdev_alloc_skb@p1(...)
+|
+napi_complete@p1(...)
+|
+napi_schedule@p1(...)
+|
+napi_gro_receive@p1(...)
+|
+skb_reserve@p1(...)
+|
+consume_skb@p1(...)
+|
+dev_consume_skb_any@p1(...)
+|
+skb_put@p1(...)
+|
+skb_push@p1(...)
+|
+netif_rx_ni@p1(...)
+)
+...
+transitive_caller@p2(...)
+
+@script:python depends on netdriver{level}@
+p1 << netdriver{level}.p1;
+p2 << netdriver{level}.p2;
+transitive_caller << virtual.transitive_caller;
+@@
+print(f"⚠️  WARNING: Network driver context at {{p1[0].file}}:{{p1[0].line}} but calls {{transitive_caller}}() at line {{p2[0].line}} β†’ eventually {target_func}()")
+""")
+
+        # Check for functions that might call from atomic context by name
+        f.write(f"""
+// Level {level} check - Function with name suggesting atomic context
+@atomic_name{level} depends on after_start exists@
+identifier virtual.transitive_caller;
+identifier atomic_fn =~ "(_irq|_intr|_isr|_napi|_poll|_bh|_softirq|_tasklet|_atomic)$";
+position p;
+@@
+atomic_fn(...) {{
+...
+transitive_caller@p(...)
+...
+}}
+
+@script:python depends on atomic_name{level}@
+atomic_fn << atomic_name{level}.atomic_fn;
+p << atomic_name{level}.p;
+transitive_caller << virtual.transitive_caller;
+@@
+print(f"⚠️  WARNING: Function with atomic-suggesting name {{atomic_fn}} calls {{transitive_caller}}() at {{p[0].file}}:{{p[0].line}} β†’ eventually {target_func}()")
+""")
+
+        # Check for sleep-incompatible contexts but target function might sleep
+        f.write(f"""
+// Level {level} check - Target function called in context where might_sleep is used
+@might_sleep_check{level} depends on after_start exists@
+identifier virtual.transitive_caller;
+position p1, p2;
+@@
+(
+might_sleep@p1()
+|
+might_sleep_if@p1(...)
+|
+sched_might_sleep@p1()
+) 
+...
+transitive_caller@p2(...)
+
+@script:python depends on might_sleep_check{level}@
+p1 << might_sleep_check{level}.p1;
+p2 << might_sleep_check{level}.p2;
+transitive_caller << virtual.transitive_caller;
+@@
+print(f"⚠️  WARNING: Function has might_sleep() at {{p1[0].file}}:{{p1[0].line}} but also calls {{transitive_caller}}() at line {{p2[0].line}} β†’ eventually {target_func}()")
+""")
+
+    f.write("\n")
+
+print(f"βœ… Generated {outfile} with enhanced atomic checks for `{target_func}` up to {max_depth} levels. Run with: make coccicheck MODE=report COCCI={outfile} J={get_nprocs()}")
scripts/coccinelle/generation/check_for_sleepy_calls.py new file +678 -0

678 lines not on this page Β· raw message

Signature Β· 1 line
2.47.2
#1 lore raw
Quoted 2 lines

To do complicated tasks it is sometimes a bit difficult to write a coccinelle rule.

Some data processing challenges can be usual.

Quoted 1 line

Such is the case when you want to use the iterator,

Would you like to trigger any improvements eventually also for the software documentation?

Quoted 1 line

which can be used to help check for nested set of calls.

Can the indicated nesting point any related functionality and requirements out?

Quoted 3 lines

An example use case of this is when we want to check if a routine may call an atomic context, or if a routine is calling any known existing sleep routines.

Can it be that your needs would include support for inter-procedural analyses?

…

Quoted 2 lines

+++ b/scripts/coccinelle/generation/check_for_atomic_calls.py

(quoted diff) +0 -0
@@ -0,0 +1,443 @@

…

Review

Quoted 2 lines
(quoted diff) +2 -0
+# Generates a Coccinelle file which can be used to track down if a routine
+# can sleep up to --max-depth levels (defauilt is 1).

default?

Quoted 2 lines

You can either specify
+# the sleep routine you want to check for,

Further configuration parameters?

Quoted 1 line

or you can use our built-in db.

I am curious how the support will evolve further also for databases.

Regards,
Markus

#2 lore raw
In reply to Luis Chamberlain

…

Quoted 2 lines

+++ b/scripts/coccinelle/generation/check_for_atomic_calls.py

(quoted diff) +0 -0
@@ -0,0 +1,443 @@

…

Quoted 1 line

+header = f"""// SPDX-License-Identifier: GPL-2.0

…

Quoted 1 line

+/// Detect atomic context in ANY transitive caller (up to {max_depth} levels)

…

scripts/coccinelle/generation/check_for_atomic_calls.py line 66

Quoted 2 lines
scripts/coccinelle/generation/check_for_atomic_calls.py new file +2 -0
+virtual after_start
+virtual report

Can any code refinements trigger desirable effects?

+virtual after_start, report

…

scripts/coccinelle/generation/check_for_atomic_calls.py line 108

Quoted 8 lines

+@irq_handler_def@

scripts/coccinelle/generation/check_for_atomic_calls.py new file +7 -0
+identifier fn;
+@@
+(
+DEFINE_IRQ_HANDLER(fn, ...)
+|
+DECLARE_TASKLET(fn, ...)
+)

+(DEFINE_IRQ_HANDLER
+|DECLARE_TASKLET
+)(fn, ...)

…

Quoted 3 lines

+@irq_func_names@
+identifier fn =~ "(_irq|_intr|_isr|_napi|_poll|_tasklet|_softirq|_bh)$"; +@@

+identifier fn =~ "_(?:i(?:rq|(?:nt|s)r)|napi|poll|tasklet|softirq|bh)$";

…

Quoted 3 lines

+@interrupt_prefixed@
+identifier fn =~ "^(irq_|intr_|isr_|napi_|poll_|do_softirq|tasklet_)"; +@@

+identifier fn =~ "^(?:i(?:rq|(?:nt|s)r)|napi|poll|tasklet)_|do_softirq)";

…

Review

Quoted 3 lines

+@atomic_name{level} depends on after_start exists@

(quoted diff) +2 -0
+identifier virtual.transitive_caller;
+identifier atomic_fn =~ "(_irq|_intr|_isr|_napi|_poll|_bh|_softirq|_tasklet|_atomic)$";

+identifier virtual.transitive_caller,
+ atomic_fn =~ "_(?:i(?:rq|(?:nt|s)r)|napi|poll|bh|softirq|tasklet|atomic)$";

…

Quoted 2 lines

+++ b/scripts/coccinelle/generation/check_for_sleepy_calls.py

(quoted diff) +0 -0
@@ -0,0 +1,678 @@

…

Quoted 1 line

+ f.write(f"""// SPDX-License-Identifier: GPL-2.0

…

Quoted 1 line

+def register_sleep_point(caller_func, sleep_func, file, line, reason=""):

…

scripts/coccinelle/generation/check_for_sleepy_calls.py line 233

Quoted 10 lines
scripts/coccinelle/generation/check_for_sleepy_calls.py new file +10 -0
+        if reason:
+            message = f"{{symbol}} {'VERIFIED' if expected_to_sleep else 'WARNING'}: {{caller_func}}() might sleep at {{file}}:{{line}} - {{reason}} (via {{sleep_func}})"
+            path_message = f"   Call path: {{path_str}} β†’ {{sleep_func}}"
+            print(message)
+            print(path_message)
+        else:
+            message = f"{{symbol}} {'VERIFIED' if expected_to_sleep else 'WARNING'}: {{caller_func}}() might sleep at {{file}}:{{line}} (via {{sleep_func}})"
+            path_message = f"   Call path: {{path_str}} β†’ {{sleep_func}}"
+            print(message)
+            print(path_message)

+ if reason:
+ message = f"{{symbol}} {'VERIFIED' if expected_to_sleep else 'WARNING'}: {{caller_func}}() might sleep at {{file}}:{{line}} - {{reason}} (via {{sleep_func}})"
+ else:
+ message = f"{{symbol}} {'VERIFIED' if expected_to_sleep else 'WARNING'}: {{caller_func}}() might sleep at {{file}}:{{line}} (via {{sleep_func}})"
+
+ print(message)
+ print(f" Call path: {{path_str}} β†’ {{sleep_func}}")

scripts/coccinelle/generation/check_for_sleepy_calls.py line 239

Quoted 5 lines
scripts/coccinelle/generation/check_for_sleepy_calls.py new file +5 -0
+        # Store this for our final stats
+        if path:
+            path_value = path_str + " β†’ " + sleep_func
+        else:
+            path_value = caller_func + " β†’ " + sleep_func

+ path_value = " β†’ ".join(((path_str if path else caller_func), sleep_func))

…

scripts/coccinelle/generation/check_for_sleepy_calls.py line 553

Quoted 4 lines

+@check_sleep_names@

scripts/coccinelle/generation/check_for_sleepy_calls.py new file +3 -0
+position p;
+identifier fn;
+identifier sleep_fn =~ "(_sleep|_timeout|_wait|_block|_sync|_lock|create_|alloc_|_kmalloc|_mutex)";

+identifier fn,
+ sleep_fn =~ "(?:_(sleep|timeout|wait|block|sync|lock|kmalloc|mutex)|(?:create|alloc)_)";

scripts/coccinelle/generation/check_for_sleepy_calls.py line 558

Quoted 5 lines

+@@

scripts/coccinelle/generation/check_for_sleepy_calls.py new file +4 -0
+fn(...) {{
+  <...
+  sleep_fn@p(...)
+  ...>

May the search pattern be really optional here?
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/fe67e5985c72582553ecde33cd7d6e6c2643ef00/docs/manual/cocci_syntax.tex#L802-806
https://github.com/coccinelle/coccinelle/blob/4093aad2da23ea5c86916de0a71f43032fc2bb2f/docs/manual/cocci_syntax.tex#L802-L806

…

scripts/coccinelle/generation/check_for_sleepy_calls.py line 661

Quoted 6 lines
scripts/coccinelle/generation/check_for_sleepy_calls.py new file +6 -0
+    if sleepy_func:
+        message = f"\n\n{{symbol}}: {{caller_func}}() can sleep at {{file}}:{{line}} - {{reason}} (via {{sleep_func}})"
+        print(message)
+    else:
+        message = f"\n\n{{symbol}}: {{caller_func}}() found to may sleep."
+        print(message)

+ if sleepy_func:
+ message = f"\n\n{{symbol}}: {{caller_func}}() can sleep at {{file}}:{{line}} - {{reason}} (via {{sleep_func}})"
+ else:
+ message = f"\n\n{{symbol}}: {{caller_func}}() found to may sleep."
+
+ print(message)

…

scripts/coccinelle/generation/check_for_sleepy_calls.py line 677

Quoted 4 lines
scripts/coccinelle/generation/check_for_sleepy_calls.py new file +4 -0
+msg = f"βœ… Generated {outfile} to check if '{target_func}' might sleep"
+if sleepy_func:
+    msg = f"βœ… Generated {outfile} to check if '{target_func}' calls '{sleepy_func}'"
+print(msg)

+if sleepy_func:
+ msg = f"βœ… Generated {outfile} to check if '{target_func}' calls '{sleepy_func}'"
+else:
+ msg = f"βœ… Generated {outfile} to check if '{target_func}' might sleep"
+
+print(msg)

…

Regards,
Markus

maintainer
#3 lore raw
Quoted 4 lines Β· Markus Elfring

…

Quoted 2 lines

+++ b/scripts/coccinelle/generation/check_for_atomic_calls.py

(quoted diff) +0 -0
@@ -0,0 +1,443 @@

…

Quoted 8 lines
Quoted 1 line

+header = f"""// SPDX-License-Identifier: GPL-2.0

…

Quoted 1 line

+/// Detect atomic context in ANY transitive caller (up to {max_depth} levels)

…

scripts/coccinelle/generation/check_for_atomic_calls.py line 66

Quoted 2 lines
scripts/coccinelle/generation/check_for_atomic_calls.py new file +2 -0
+virtual after_start
+virtual report

Can any code refinements trigger desirable effects?

How should I know? If you want these changes to be made, do some performance tests and show that this is beneficial in some way.

Quoted 13 lines

+virtual after_start, report

…

Quoted 8 lines

+@irq_handler_def@

scripts/coccinelle/generation/check_for_atomic_calls.py new file +7 -0
+identifier fn;
+@@
+(
+DEFINE_IRQ_HANDLER(fn, ...)
+|
+DECLARE_TASKLET(fn, ...)
+)

Review

Quoted 3 lines
(quoted diff) +3 -0
+(DEFINE_IRQ_HANDLER
+|DECLARE_TASKLET
+)(fn, ...)

Intuitively this is of no benefit, because Coccinelle will generate the first case internally.

Quoted 8 lines

…

Quoted 3 lines

+@irq_func_names@
+identifier fn =~ "(_irq|_intr|_isr|_napi|_poll|_tasklet|_softirq|_bh)$"; +@@

+identifier fn =~ "_(?:i(?:rq|(?:nt|s)r)|napi|poll|tasklet|softirq|bh)$";

I would imagine that a regular expression compiler would generate the same code in both cases.

julia

Quoted 112 lines

…

Quoted 3 lines

+@interrupt_prefixed@
+identifier fn =~ "^(irq_|intr_|isr_|napi_|poll_|do_softirq|tasklet_)"; +@@

+identifier fn =~ "^(?:i(?:rq|(?:nt|s)r)|napi|poll|tasklet)_|do_softirq)";

…

Quoted 3 lines

+@atomic_name{level} depends on after_start exists@

(quoted diff) +2 -0
+identifier virtual.transitive_caller;
+identifier atomic_fn =~ "(_irq|_intr|_isr|_napi|_poll|_bh|_softirq|_tasklet|_atomic)$";
(quoted diff) +2 -0
+identifier virtual.transitive_caller,
+           atomic_fn =~ "_(?:i(?:rq|(?:nt|s)r)|napi|poll|bh|softirq|tasklet|atomic)$";
 
 
…
Quoted 2 lines

+++ b/scripts/coccinelle/generation/check_for_sleepy_calls.py

(quoted diff) +0 -0
@@ -0,0 +1,678 @@

…

Quoted 1 line

+ f.write(f"""// SPDX-License-Identifier: GPL-2.0

…

Quoted 1 line

+def register_sleep_point(caller_func, sleep_func, file, line, reason=""):

…

Quoted 10 lines
scripts/coccinelle/generation/check_for_sleepy_calls.py new file +10 -0
+        if reason:
+            message = f"{{symbol}} {'VERIFIED' if expected_to_sleep else 'WARNING'}: {{caller_func}}() might sleep at {{file}}:{{line}} - {{reason}} (via {{sleep_func}})"
+            path_message = f"   Call path: {{path_str}} β†’ {{sleep_func}}"
+            print(message)
+            print(path_message)
+        else:
+            message = f"{{symbol}} {'VERIFIED' if expected_to_sleep else 'WARNING'}: {{caller_func}}() might sleep at {{file}}:{{line}} (via {{sleep_func}})"
+            path_message = f"   Call path: {{path_str}} β†’ {{sleep_func}}"
+            print(message)
+            print(path_message)
scripts/coccinelle/generation/check_for_sleepy_calls.py new file +7 -0
+        if reason:
+            message = f"{{symbol}} {'VERIFIED' if expected_to_sleep else 'WARNING'}: {{caller_func}}() might sleep at {{file}}:{{line}} - {{reason}} (via {{sleep_func}})"
+        else:
+            message = f"{{symbol}} {'VERIFIED' if expected_to_sleep else 'WARNING'}: {{caller_func}}() might sleep at {{file}}:{{line}} (via {{sleep_func}})"
+
+        print(message)
+        print(f"   Call path: {{path_str}} β†’ {{sleep_func}}")

scripts/coccinelle/generation/check_for_sleepy_calls.py line 239

Quoted 7 lines
scripts/coccinelle/generation/check_for_sleepy_calls.py new file +5 -0
+        # Store this for our final stats
+        if path:
+            path_value = path_str + " β†’ " + sleep_func
+        else:
+            path_value = caller_func + " β†’ " + sleep_func

+ path_value = " β†’ ".join(((path_str if path else caller_func), sleep_func))

…

Quoted 4 lines

+@check_sleep_names@

scripts/coccinelle/generation/check_for_sleepy_calls.py new file +3 -0
+position p;
+identifier fn;
+identifier sleep_fn =~ "(_sleep|_timeout|_wait|_block|_sync|_lock|create_|alloc_|_kmalloc|_mutex)";
(quoted diff) +2 -0
+identifier fn,
+           sleep_fn =~ "(?:_(sleep|timeout|wait|block|sync|lock|kmalloc|mutex)|(?:create|alloc)_)";

scripts/coccinelle/generation/check_for_sleepy_calls.py line 558

Quoted 7 lines

+@@

scripts/coccinelle/generation/check_for_sleepy_calls.py new file +4 -0
+fn(...) {{
+  <...
+  sleep_fn@p(...)
+  ...>

May the search pattern be really optional here?
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/fe67e5985c72582553ecde33cd7d6e6c2643ef00/docs/manual/cocci_syntax.tex#L802-806
https://github.com/coccinelle/coccinelle/blob/4093aad2da23ea5c86916de0a71f43032fc2bb2f/docs/manual/cocci_syntax.tex#L802-L806

…

Quoted 6 lines
scripts/coccinelle/generation/check_for_sleepy_calls.py new file +6 -0
+    if sleepy_func:
+        message = f"\n\n{{symbol}}: {{caller_func}}() can sleep at {{file}}:{{line}} - {{reason}} (via {{sleep_func}})"
+        print(message)
+    else:
+        message = f"\n\n{{symbol}}: {{caller_func}}() found to may sleep."
+        print(message)
scripts/coccinelle/generation/check_for_sleepy_calls.py new file +6 -0
+    if sleepy_func:
+        message = f"\n\n{{symbol}}: {{caller_func}}() can sleep at {{file}}:{{line}} - {{reason}} (via {{sleep_func}})"
+    else:
+        message = f"\n\n{{symbol}}: {{caller_func}}() found to may sleep."
+
+    print(message)
 
 
…
Quoted 4 lines
scripts/coccinelle/generation/check_for_sleepy_calls.py new file +4 -0
+msg = f"βœ… Generated {outfile} to check if '{target_func}' might sleep"
+if sleepy_func:
+    msg = f"βœ… Generated {outfile} to check if '{target_func}' calls '{sleepy_func}'"
+print(msg)
scripts/coccinelle/generation/check_for_sleepy_calls.py new file +6 -0
+if sleepy_func:
+    msg = f"βœ… Generated {outfile} to check if '{target_func}' calls '{sleepy_func}'"
+else:
+    msg = f"βœ… Generated {outfile} to check if '{target_func}' might sleep"
+
+print(msg)
 
…

Regards,
Markus

#4 lore raw
Quoted 6 lines
Quoted 6 lines

…

Quoted 2 lines

+++ b/scripts/coccinelle/generation/check_for_atomic_calls.py

(quoted diff) +0 -0
@@ -0,0 +1,443 @@

…

Quoted 1 line

+header = f"""// SPDX-License-Identifier: GPL-2.0

…

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

+/// Detect atomic context in ANY transitive caller (up to {max_depth} levels)

…

scripts/coccinelle/generation/check_for_atomic_calls.py line 66

Quoted 2 lines
scripts/coccinelle/generation/check_for_atomic_calls.py new file +2 -0
+virtual after_start
+virtual report

Can any code refinements trigger desirable effects?

How should I know?

I imagine that further experiences can be shared by developers and code reviewers.

Quoted 4 lines

If you want these changes to be made, do some
performance tests and show that this is beneficial in some way.

Quoted 1 line

+virtual after_start, report

Can the reduction of repeated key words be occasionally desirable (also because of an influence on code size)?

Quoted 16 lines

Review

Quoted 13 lines

…

Quoted 8 lines

+@irq_handler_def@

scripts/coccinelle/generation/check_for_atomic_calls.py new file +7 -0
+identifier fn;
+@@
+(
+DEFINE_IRQ_HANDLER(fn, ...)
+|
+DECLARE_TASKLET(fn, ...)
+)
(quoted diff) +3 -0
+(DEFINE_IRQ_HANDLER
+|DECLARE_TASKLET
+)(fn, ...)

Intuitively this is of no benefit, because Coccinelle will generate the first case internally.

Is the proposed SmPL code variant a bit more succinct?

Quoted 9 lines
Quoted 6 lines

…

Quoted 3 lines

+@irq_func_names@
+identifier fn =~ "(_irq|_intr|_isr|_napi|_poll|_tasklet|_softirq|_bh)$"; +@@

+identifier fn =~ "_(?:i(?:rq|(?:nt|s)r)|napi|poll|tasklet|softirq|bh)$";

I would imagine that a regular expression compiler would generate the same code in both cases.

I suggest to improve the expression precision at some places.

Regards,
Markus