Skip to content

mohitmishra786/low-level-dev-skills / skills/kernel-dev/kernel-memory-management

kernel-memory-management

Verified

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.

FileKindSize
SKILL.mdskill md3.4 KB
---
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`.

## Triggers

- 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

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

```text
/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
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall