Skip to content

linux/mm/slab-common.c

Imported from _research/manual-study-linux/file-notes/linux__mm__slab_common.c.md.

File Notes: mm/slab_common.c

Status: reviewed.

Purpose

Implements shared slab-cache creation and destruction logic used by kernel object allocators. It validates cache parameters, handles alignment and flags, creates cache descriptors, merges aliases where allowed, and tears caches down only after deferred/freeing work is safe.

Key Types And Functions

  • kmem_cache_sanity_check(): cache name, context, size, duplicate-name checks.
  • calculate_alignment(): object alignment based on flags and size.
  • create_cache(): allocates and links a cache descriptor.
  • __kmem_cache_create_args(): public creation implementation behind wrapper APIs.
  • kmem_cache_destroy(): safe cache teardown.

Data Flow

Cache creation validates arguments under slab_mutex, normalizes unsupported debug flags, applies SLAB_NO_MERGE where cache-specific capacity requires it, validates hardened-usercopy ranges, searches for an alias cache, duplicates the name, calculates alignment, and calls create_cache().

create_cache() validates custom freelist pointer constraints, allocates a struct kmem_cache descriptor from the metadata cache, calls allocator-specific creation, sets the refcount, and links the cache into slab_caches.

Cache destruction waits for in-flight RCU/free activity, handles SLAB_TYPESAFE_BY_RCU, takes CPU and slab locks, decrements refcount, shuts down KASAN/allocator state, warns if objects remain, unlinks global/sysfs/debug state, waits for RCU if needed, and releases the descriptor.

Invariants And Safety Contracts

  • Cache creation cannot happen in interrupt context.
  • Duplicate names are warned because they confuse diagnostics.
  • Hardened usercopy ranges fail closed on invalid values.
  • kmem_cache_destroy() should only be called after allocated objects are freed; remaining objects trigger warnings.
  • SLAB_TYPESAFE_BY_RCU requires RCU grace-period care during teardown.

Rust Translation Guidance

Model slab caches as validated SlabCache<T> handles. Creation should encode object size/alignment, usercopy range, reclaim/accounting flags, and merge policy. Destruction should require a state proving no live typed objects remain or should be an unsafe operation with clear invariants.

AI-Native Systems Guidance

Agent runtimes need allocator classes for repeated objects: prompts, tool-call records, embeddings chunks, trace spans, and job descriptors. Cache creation should carry accounting and user-copy/export policy, not just object size.

Evidence

  • Cache sanity checks reject bad name/context/size and warn on duplicate names at mm/slab_common.c:91-115.
  • create_cache() validates custom free pointers, allocates the descriptor, calls allocator creation, sets refcount, and links the cache at mm/slab_common.c:232-264.
  • __kmem_cache_create_args() documents flags and context at mm/slab_common.c:292-317.
  • Creation validates flags/usercopy ranges, handles aliases, calculates alignment, and creates the cache under slab_mutex at mm/slab_common.c:318-402.
  • kmem_cache_destroy() handles deferred RCU/free work, refcounting, KASAN, shutdown, unlink, RCU barriers, and release at mm/slab_common.c:506-591.
  • Kmalloc slab creation computes alignment and marks caches unmergeable at mm/slab_common.c:700-724.