kernel-memory-management — Claude Code skill: Linux kernel allocators
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.
MIT 388 installs ★ 298 updated 2026-06-27
npx skills add mohitmishra786/low-level-dev-skills --skill kernel-memory-management -a claude-codeWhat kernel-memory-management does
Ask for an allocator and the agent works down a short decision tree. Memory that hardware must see as physically contiguous goes to dma_alloc_coherent() or a CMA pool. Requests up to roughly 128K fit in kmalloc. Larger buffers that can live in scattered pages land in vmalloc, and whole pages come from alloc_pages() at order n, meaning 2^n pages.
GFP flags get the same treatment: GFP_KERNEL where the caller may sleep, GFP_ATOMIC inside interrupt handlers and spinlocks, GFP_DMA for legacy 32-bit devices. Zones are covered too — ZONE_DMA, ZONE_NORMAL, ZONE_MOVABLE — along with NUMA, where kmalloc prefers the local node and alloc_pages_node() picks one outright.
Debugging leans on cat /proc/meminfo, cat /proc/slabinfo and dmesg | grep -i oom, with CONFIG_SLUB_DEBUG and CONFIG_KASAN for the uglier cases. SLUB is the default backend these days; SLAB was removed in Linux 6.12. One thing worth knowing up front: a GFP_KERNEL allocation inside an interrupt handler means sleeping in atomic context, and that is a bug.
What it helps with
- pick an allocator for a driver ring buffer
- trace why the OOM killer fired
- catch a slab use-after-free with KASAN
- read
/proc/meminfozone by zone - explain why
vmallocmemory cannot feed a DMA engine
Example prompts for the agent
- /kernel-memory-management pick an allocator for a 2 MiB ring buffer in process context
kmallocreturned NULL for a 4 MiB request, what should I use instead?- Walk through this dmesg output and tell me which cache keeps growing
How to install kernel-memory-management
1Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill kernel-memory-management -a claude-code2Manually: put the files in place
~/.claude/skills/kernel-memory-management/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/kernel-memory-management/
1Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill kernel-memory-management -a codex2Manually: put the files in place
.agents/skills/kernel-memory-management/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill kernel-memory-management -a cursor2Manually: put the files in place
.cursor/skills/kernel-memory-management/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install mohitmishra786/low-level-dev-skills skills/kernel-dev/kernel-memory-management2Manually: put the files in place
.github/skills/kernel-memory-management/SKILL.md1Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill kernel-memory-management -a gemini-cli2Platform CLI
gemini skills install https://github.com/mohitmishra786/low-level-dev-skills --path skills/kernel-dev/kernel-memory-managementManually: put the files in place
.devin/skills/kernel-memory-management/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill kernel-memory-management -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about kernel-memory-management
How do I install kernel-memory-management in Claude Code?
Run npx skills add mohitmishra786/low-level-dev-skills --skill kernel-memory-management -a claude-code. The skill file lands at ~/.claude/skills/kernel-memory-management/SKILL.md.
What does kernel-memory-management do?
It guides an agent through kernel allocation choices: kmalloc and SLUB, vmalloc, the buddy page allocator, memory zones and GFP flags. It also points at ways to investigate OOM kills and slab corruption.
When should I use vmalloc instead of kmalloc?
vmalloc returns a virtually contiguous buffer built from scattered physical pages, which suits big allocations. It will not work for DMA unless you go through dma_map_*.
Source text
SKILL.md · MIT
---
name: kernel-memory-management
description: Kernel memory management skill for Linux mm subsystem. Use when using kmalloc, vmalloc, page allocator, SLUB, or debugging OOM and memory zones. Activates on queries about buddy allocator, kmalloc sizes, vmalloc vs kmalloc, SLUB, memory zones, or kernel OOM.
---
# Kernel Memory Management
## Purpose
Guide agents through Linux kernel memory allocation: physical page allocator (buddy), SLUB kmalloc caches, vmalloc virtual mappings, zones (DMA/Normal), and safe allocation patterns in drivers — extending `skills/kernel/kernel-internals`.
## When to Use
- Choosing `kmalloc` vs `vmalloc` vs `get_free_pages`
- Debugging kernel OOM or slab corruption
- DMA buffer allocation requirements
- Understanding `/proc/meminfo` and slab stats
## Workflow
### 1. Allocator decision tree
```
Need physically contiguous memory for DMA?
├── Yes → dma_alloc_coherent() or CMA pool
└── No
├── Size ≤ ~128K (arch-dependent) → kmalloc / kzalloc
├── Large or non-contiguous OK → vmalloc / vzalloc
└── Whole pages → alloc_pages() / __get_free_pages()
```
### 2. kmalloc and SLUB
SLUB is the default general-purpose kmalloc backend (per [kernel memory allocation guide](https://docs.kernel.org/core-api/memory-allocation.html)); the legacy SLAB allocator was removed in Linux 6.12.
```c
void *buf = kmalloc(size, GFP_KERNEL);
if (!buf)
return -ENOMEM;
/* ... */
kfree(buf);
```
| GFP flag | Context |
|----------|---------|
| `GFP_KERNEL` | Process context, may sleep |
| `GFP_ATOMIC` | IRQ / holding spinlock — smaller pools |
| `GFP_DMA` | DMA zone (legacy 32-bit devices) |
Check `/proc/slabinfo` and `cat /sys/kernel/slab/*/object_size`.
### 3. vmalloc
```c
void *v = vmalloc(size); /* virtually contiguous, physically scattered */
vfree(v);
```
Use for large buffers where contiguous physical pages are unnecessary. Do not use for DMA without `dma_map_*`.
### 4. Buddy page allocator
```c
struct page *page = alloc_pages(GFP_KERNEL, order);
void *addr = page_address(page);
__free_pages(page, order);
```
`order` n allocates `2^n` pages. Zone selection: `ZONE_DMA`, `ZONE_NORMAL`, `ZONE_MOVABLE` (see `include/linux/mmzone.h`).
### 5. NUMA basics
On NUMA systems, `kmalloc` prefers local node; use `alloc_pages_node()` or `mbind` policies for HPC paths — see `skills/allocators/numa-programming`.
### 6. Debugging
```bash
cat /proc/meminfo
cat /proc/slabinfo
dmesg | grep -i oom
```
Kernel config: `CONFIG_SLUB_DEBUG`, `CONFIG_KASAN` for use-after-free.
### 7. Agent usage
```
/kernel-memory-management Choose allocator for 2 MiB driver ring buffer in process context
```
## Common Problems
| Symptom | Cause | Fix |
|---------|-------|-----|
| `kmalloc` fails large | Exceeds kmalloc limit | Use `vmalloc` or pages |
| DMA buffer wrong | Used `vmalloc` for HW DMA | `dma_alloc_coherent` |
| OOM killer | Unbounded cache growth | Limit pools; use shrinker |
| Slab corruption | Use-after-free | KASAN; audit `kfree` pairing |
| `GFP_KERNEL` in IRQ | Sleeping allocator in atomic | `GFP_ATOMIC` |
## Related Skills
- `skills/kernel/kernel-internals` — page cache, OOM killer
- `skills/kernel/device-drivers` — `dma_alloc_coherent`
- `skills/allocators/custom-allocators` — userspace allocator design
- `skills/allocators/numa-programming` — NUMA-aware allocation
- `skills/runtimes/sanitizers` — KASAN overlap
© mohitmishra786 · MIT · mohitmishra786/low-level-dev-skills