Skip to main content
skill CX Codex

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

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

What 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/meminfo zone by zone
  • explain why vmalloc memory 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
  • kmalloc returned 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-code

2Manually: put the files in place

~/.claude/skills/kernel-memory-management/SKILL.md

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

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

Similar skills

All skills for DevOps and infrastructure →

From the same set140

The whole set →
kernel-concurrency kernel-concurrency is a Claude Code skill that helps select a Linux kernel synchronization primitive: spinlock, mutex, RCU, seqlock, or completion. 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 cuda-debugging Walks the agent through CUDA debugging: cuda-gdb, Compute Sanitizer, GPU core dumps, error codes 700 and 702. Useful when a kernel crashes or returns wrong values. CX Codex +1 386installs llvm-ir-and-passes llvm-ir-and-passes teaches the agent to read LLVM IR, understand SSA form, and navigate optimization passes. Useful when tracing what -O2 changed or preparing to write a custom pass. CX Codex +1 384installs spi-i2c-baremetal The spi-i2c-baremetal skill guides bare-metal SPI and I2C driver work, from register reads to bus troubleshooting. Use it when bringing up sensors, flash, or displays. CX Codex +1 384installs protocol-analysis protocol-analysis is a Claude Code skill that decodes I2C, SPI, and UART captures with sigrok and PulseView, then checks them against driver code. It handles silent-sensor bus analysis. CX Codex +1 383installs

Project sponsors

Companies that keep this analytics open