mohitmishra786/low-level-dev-skills

kernel-concurrency

Kernel concurrency skill for Linux locking and synchronization.

Quelltext ansehen

Verwendung

  1. Installationsbefehl kopieren und das Quellpaket vor der Installation in der eigenen Agentenumgebung prüfen.
  2. Benötigte Werkzeuge, Zugangsdaten und Hosts im Original prüfen.
  3. Eingabe und gewünschtes Ergebnis beschreiben und die Ausgabe mit den Quellen abgleichen.
npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill kernel-concurrency

Vor dem Start

  • Die Installation umfasst weder Modellabo noch API-Kontingent oder Dateizugriffsrechte.
  • Sterne und Installationen zeigen Aktivität, keine unabhängige Qualitäts- oder Sicherheitsprüfung.
Originales Skill-Dokument

Aus dem Quell-Repository gerendert; Überschriften, Beispiele, Code, Tabellen, Links und Bilder bleiben erhalten.

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

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

Verwandte Aufgaben

aus demselben Repository

Weitere Skills

Alle Skills
mohitmishra786
Community

core-dumps

Core dump analysis skill for production crash triage. Use when loading core files in GDB or LLDB, enabling core dump generation on Linux/macOS, mapping symbols with debuginfo or debuginfod, or extracting backtraces from crashes without re-running the program. Activates on queries about core files, ulimit, coredumpctl, debuginfod, crash triage, or analyzing segfaults from production binaries.

Installationen
2
GitHub Stars
267
Aktualisiert
27. Juni
mohitmishra786
Community

custom-allocators

Custom allocator skill for memory allocation strategies. Use when implementing pool/slab/arena allocators, tuning jemalloc/mimalloc, writing Rust GlobalAlloc, or benchmarking allocator performance. Activates on queries about jemalloc, mimalloc, tcmalloc, arena allocator, GlobalAlloc, or fragmentation.

Installationen
2
GitHub Stars
267
Aktualisiert
27. Juni
mohitmishra786
Community

ebpf

eBPF skill for Linux observability and networking. Use when writing eBPF programs with libbpf or bpftrace, attaching kprobes/tracepoints/XDP hooks, debugging verifier errors, working with eBPF maps, or achieving CO-RE portability across kernel versions. Activates on queries about eBPF, bpftool, bpftrace, XDP programs, libbpf, verifier errors, eBPF maps, or kernel tracing with BPF.

Installationen
2
GitHub Stars
267
Aktualisiert
27. Juni
mohitmishra786
Community

flamegraphs

Flamegraph generation and interpretation skill. Use when converting perf, Valgrind Callgrind, or other profiler output into SVG flamegraphs using Brendan Gregg's FlameGraph tools, or when reading flamegraphs to identify performance bottlenecks. Activates on queries about flamegraphs, stackcollapse, flamegraph.svg, identifying hot frames, wide vs tall frames, or performance visualisation.

Installationen
2
GitHub Stars
267
Aktualisiert
27. Juni