mcpField
K
kernel-concurrency

kernel-concurrency

github.com/mohitmishra786Development269311 installsUpdated 18w ago
Repository

About kernel-concurrency

An agent skill for synchronization in the Linux kernel, covering locks, RCU, completions, and memory barriers. It is for developers writing drivers or subsystem patches and diagnosing deadlocks or scheduling-in-atomic issues.

Key features

  1. 01Choose locks for execution context
  2. 02Protect IRQ-shared data with spinlocks
  3. 03Use RCU and seqlocks for concurrent reads
  4. 04Coordinate threads with completions
  5. 05Apply memory barriers and MMIO access rules

Use cases

  1. 01Protect data shared by IRQ handlers and processes
  2. 02Select synchronization for read-mostly structures
  3. 03Diagnose deadlocks or scheduling-in-atomic errors
  4. 04Coordinate kernel waits and notifications

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill kernel-concurrency

After installing, type/kernel-concurrencyin Claude Code to confirm it loaded.

Details

From the project README

Kernel Concurrency

Purpose

Guide agents through synchronization in the Linux kernel: spinlocks, mutexes, semaphores, RCU, seqlocks, completions, and memory ordering rules — critical for correct drivers and subsystem patches.

When to Use

  • IRQ handler shares data with process context
  • Read-mostly data structures needing RCU
  • Choosing lock type for probe vs ioctl paths
  • Debugging deadlocks or scheduling while atomic

Workflow

1. Lock selection tree
Context can sleep?
├── No (IRQ, spinlock held, preempt disabled)
│   └── spin_lock_irqsave() / atomic_t
└── Yes
    ├── Exclusive long-held → mutex
    ├── Reader/writer → rw_semaphore or RCU (read-mostly)
    └── One-shot signal → completion

Never sleep while holding a spinlock (kmalloc(GFP_KERNEL), mutex_lock).

2. Spinlock + IRQ
spinlock_t lock;
unsigned long flags;

spin_lock_irqsave(&lock, flags);
/* critical section — no blocking */
spin_unlock_irqrestore(&lock, flags);

Use spin_lock_bh when softirq/tasklet sharing is the concern.

3. Mutex in process context
struct mutex m;
mutex_lock(&m);
/* may allocate, may sleep */
mutex_unlock(&m);
4. RCU (read-copy update)
/* Readers — no lock */
rcu_read_lock();
p = rcu_dereference(ptr);
/* use p */
rcu_read_unlock();

/* Writer */
new = kmalloc(...);
rcu_assign_pointer(ptr, new);
synchronize_rcu();
kfree(old);

RCU readers must not block indefinitely. Grace period completes after all CPUs quiescent.

5. Seqlock (jiffies, timestamps)
unsigned seq;
do {
    seq = read_seqbegin(&seqlock);
    /* read shared data */
} while (read_seqretry(&seqlock, seq));

Writer uses write_seqlock / write_sequnlock.

6. Completions
DECLARE_COMPLETION(done);
/* waiter */
wait_for_completion(&done);
/* signaller */
complete(&done);
7. Memory barriers

Kernel provides smp_mb(), smp_wmb(), smp_rmb(). Device MMIO uses readl/writel (ordered on most arches). See skills/low-level-programming/memory-model for userspace analogies.

8. Agent usage
/kernel-concurrency Protect shared ring buffer between IRQ and read() syscall

Common Problems

SymptomCauseFix
scheduling while atomicSleep under spinlockUse GFP_ATOMIC or defer work
DeadlockAB-BA mutex orderGlobal lock ordering
RCU stallReader blocked too longrcu_read_lock section minimal
Lost wakecomplete before waitUse INIT_COMPLETION each cycle
Corrupt counterNon-atomic RMW in IRQatomic_t or lock

Related Skills

  • skills/kernel/device-drivers — threaded IRQ pattern
  • skills/low-level-programming/memory-model — C11 atomics vs kernel
  • skills/debuggers/concurrency-debugging — userspace TSan
  • skills/kernel/kernel-internals — scheduler preemption
  • skills/profilers/linux-perf — lock contention profiling

Comments

Markdown supported