Skip to main content
skill CX Codex

kernel-concurrency — Linux skill: spinlocks, mutexes, RCU

kernel-concurrency is a Claude Code skill that helps select a Linux kernel synchronization primitive: spinlock, mutex, RCU, seqlock, or completion.

MIT 391 installs ★ 298 updated 2026-06-27

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add mohitmishra786/low-level-dev-skills --skill kernel-concurrency -a claude-code
Open on GitHub

What kernel-concurrency does

Linux kernel synchronization demands precision: spinlocks cannot sleep, mutexes only work where sleep is safe, RCU trades writers' work for reader speed on read-heavy data. The skill offers a decision tree — first check if the current context can sleep, then decide whether writers need full exclusion or readers can run in parallel.

Includes ready-made code patterns: spin_lock_irqsave() with flags for IRQ safety, rcu_read_lock() / rcu_assign_pointer() for arrays and lists, seqlock for counters like jiffies, completion for one-shot signals. A troubleshooting table links failures like “scheduling while atomic” to their cause (slept under spinlock) and fix (switch to GFP_ATOMIC or defer the work).

Without the right synchronization, code either deadlocks or corrupts data through race conditions. The skill charts a clear path from problem to code.

What it helps with

  • choose spinlock or mutex for guarding shared state
  • synchronize a ring buffer between IRQ handler and syscall read()
  • apply RCU to read-heavy data structures
  • place memory barriers in the right order
  • debug “scheduling while atomic” errors

Example prompts for the agent

  • Protect the shared ring buffer between IRQ and read() syscall
  • Which lock should guard a data structure with 90% reads?
  • Why does probe code fail on “scheduling while atomic”?

How to install kernel-concurrency

1Skills CLI (Vercel)

npx skills add mohitmishra786/low-level-dev-skills --skill kernel-concurrency -a claude-code

2Manually: put the files in place

~/.claude/skills/kernel-concurrency/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/kernel-concurrency/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about kernel-concurrency

How do I install kernel-concurrency in Claude Code?

Run npx skills add mohitmishra786/low-level-dev-skills --skill kernel-concurrency -a claude-code; the skill will be at ~/.claude/skills/kernel-concurrency/SKILL.md.

What does kernel-concurrency do?

Guides you to the right synchronization primitive (spinlock, mutex, RCU, seqlock, completion) based on context — IRQ handler, process context, or read-mostly workload. Includes code patterns and a diagnostic table for common failures.

Spinlock or mutex?

Spinlock when the context cannot sleep (IRQ, spinlock already held, preemption off). Mutex in process context where sleep is allowed. Hold spinlocks briefly; mutexes as long as you need exclusion.

Source text

SKILL.md · MIT
---
name: kernel-concurrency
description: Kernel concurrency skill for Linux locking and synchronization. Use when choosing spinlocks vs mutexes, using RCU, seqlocks, completions, or applying memory barriers in kernel code. Activates on queries about kernel spinlock, mutex, RCU, seqlock, memory barrier, or PREEMPT_RT locking.
---

# 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

```c
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

```c
struct mutex m;
mutex_lock(&m);
/* may allocate, may sleep */
mutex_unlock(&m);
```

### 4. RCU (read-copy update)

```c
/* 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)

```c
unsigned seq;
do {
    seq = read_seqbegin(&seqlock);
    /* read shared data */
} while (read_seqretry(&seqlock, seq));
```

Writer uses `write_seqlock` / `write_sequnlock`.

### 6. Completions

```c
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

| Symptom | Cause | Fix |
|---------|-------|-----|
| `scheduling while atomic` | Sleep under spinlock | Use `GFP_ATOMIC` or defer work |
| Deadlock | AB-BA mutex order | Global lock ordering |
| RCU stall | Reader blocked too long | `rcu_read_lock` section minimal |
| Lost wake | `complete` before `wait` | Use `INIT_COMPLETION` each cycle |
| Corrupt counter | Non-atomic RMW in IRQ | `atomic_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

© mohitmishra786 · MIT · mohitmishra786/low-level-dev-skills

Similar skills

All skills for Programming languages →

From the same set140

The whole set →
mmio-and-bit-manipulation mmio-and-bit-manipulation — a skill for safe peripheral memory access: volatile semantics, read-modify-write patterns, bit masks, and alignment. Essential when writing bare-metal drivers without a HAL. CX Codex +1 392installs dma-baremetal dma-baremetal is a Claude Code skill that assists with register-level DMA setup for bare-metal microcontrollers, moving UART, SPI, or ADC data away from the CPU. CX Codex +1 392installs bus-drivers-i2c-spi bus-drivers-i2c-spi walks an agent through Linux client drivers for I2C and SPI sensors and PMICs: registration, regmap, device tree binding. CX Codex +1 392installs uart-serial-baremetal uart-serial-baremetal configures bare-metal UART and USART drivers for serial debug consoles, baud calculation, and interrupt I/O. CX Codex +1 391installs gpu-memory-model gpu-memory-model explains GPU execution and memory hierarchy under SIMT. Helps debug warp divergence, bank conflicts, and coalescing issues in compute kernels. CX Codex +1 391installs compiler-frontend A skill for building compiler frontends, from tokenization and parsing to type checks and LLVM IR generation. Useful for a language, DSL, or interpreter. CX Codex +1 390installs qemu-embedded-simulation The qemu-embedded-simulation skill guides QEMU runs of bare-metal ARM and RISC-V firmware and GDB sessions. Use it to test firmware without a board. CX Codex +1 389installs kernel-memory-management kernel-memory-management — a skill that walks an AI agent through Linux kernel allocation: kmalloc vs vmalloc vs page allocator, GFP flags, zones and OOM debugging. CX Codex +1 388installs

Project sponsors

Companies that keep this analytics open