Slab Allocation

Functions and structures

folio_slab

folio_slab (folio)

Converts from folio to slab.

Parameters

folio

The folio.

Description

Currently struct slab is a different representation of a folio where folio_test_slab() is true.

Return

The slab which contains this folio.

slab_folio

slab_folio (s)

The folio allocated for a slab

Parameters

s

The slab.

Description

Slabs are allocated as folios that contain the individual objects and are using some fields in the first struct page of the folio - those fields are now accessed by struct slab. It is occasionally necessary to convert back to a folio in order to communicate with the rest of the mm. Please use this helper function instead of casting yourself, as the implementation may change in the future.

page_slab

page_slab (p)

Converts from first struct page to slab.

Parameters

p

The first (either head of compound or single) page of slab.

Description

A temporary wrapper to convert struct page to struct slab in situations where we know the page is the compound head, or single order-0 page.

Long-term ideally everything would work with struct slab directly or go through folio to struct slab.

Return

The slab which contains this page

slab_page

slab_page (s)

The first struct page allocated for a slab

Parameters

s

The slab.

Description

A convenience wrapper for converting slab to the first struct page of the underlying folio, to communicate with code not yet converted to folio or struct slab.

enum slab_flags

How the slab flags bits are used.

Constants

SL_locked

Is locked with slab_lock()

SL_partial

On the per-node partial list

SL_pfmemalloc

Was allocated from PF_MEMALLOC reserves

Description

The slab flags share space with the page flags but some bits have different interpretations. The high bits are used for information like zone/node/section.

void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)

Allocate an object on the specified node

Parameters

struct kmem_cache *s

The cache to allocate from.

gfp_t gfpflags

See kmalloc().

int node

node number of the target node.

Description

Identical to kmem_cache_alloc but it will allocate memory on the given node, which can improve the performance for cpu bound structures.

Fallback to other node is possible if __GFP_THISNODE is not set.

Return

pointer to the new object or NULL in case of error

void *kmalloc_nolock(size_t size, gfp_t gfp_flags, int node)

Allocate an object of given size from any context.

Parameters

size_t size

size to allocate

gfp_t gfp_flags

GFP flags. Only __GFP_ACCOUNT, __GFP_ZERO, __GFP_NO_OBJ_EXT allowed.

int node

node number of the target node.

Return

pointer to the new object or NULL in case of error. NULL does not mean EBUSY or EAGAIN. It means ENOMEM. There is no reason to call it again and expect !NULL.

void *krealloc_node_align(const void *p, size_t new_size, unsigned long align, gfp_t flags, int nid)

reallocate memory. The contents will remain unchanged.

Parameters

const void *p

object to reallocate memory for.

size_t new_size

how many bytes of memory are required.

unsigned long align

desired alignment.

gfp_t flags

the type of memory to allocate.

int nid

NUMA node or NUMA_NO_NODE

Description

If p is NULL, krealloc() behaves exactly like kmalloc(). If new_size is 0 and p is not a NULL pointer, the object pointed to is freed.

Only alignments up to those guaranteed by kmalloc() will be honored. Please see Memory Allocation Guide for more details.

If __GFP_ZERO logic is requested, callers must ensure that, starting with the initial memory allocation, every subsequent call to this API for the same memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that __GFP_ZERO is not fully honored by this API.

When slub_debug_orig_size() is off, krealloc() only knows about the bucket size of an allocation (but not the exact size it was allocated with) and hence implements the following semantics for shrinking and growing buffers with __GFP_ZERO:

        new             bucket
0       size             size
|--------|----------------|
|  keep  |      zero      |

Otherwise, the original allocation size ‘orig_size’ could be used to precisely clear the requested size, and the new size will also be stored as the new ‘orig_size’.

In any case, the contents of the object pointed to are preserved up to the lesser of the new and old sizes.

Return

pointer to the allocated memory or NULL in case of error

void *__kvmalloc_node(size, b, unsigned long align, gfp_t flags, int node)

attempt to allocate physically contiguous memory, but upon failure, fall back to non-contiguous (vmalloc) allocation.

Parameters

size

size of the request.

b

which set of kmalloc buckets to allocate from.

unsigned long align

desired alignment.

gfp_t flags

gfp mask for the allocation - must be compatible (superset) with GFP_KERNEL.

int node

numa node to allocate from

Description

Only alignments up to those guaranteed by kmalloc() will be honored. Please see Memory Allocation Guide for more details.

Uses kmalloc to get the memory but if the allocation fails then falls back to the vmalloc allocator. Use kvfree for freeing the memory.

GFP_NOWAIT and GFP_ATOMIC are not supported, neither is the __GFP_NORETRY modifier. __GFP_RETRY_MAYFAIL is supported, and it should be used only if kmalloc is preferable to the vmalloc fallback, due to visible performance drawbacks.

Return

pointer to the allocated memory of NULL in case of failure

void *kvrealloc_node_align(const void *p, size_t size, unsigned long align, gfp_t flags, int nid)

reallocate memory; contents remain unchanged

Parameters

const void *p

object to reallocate memory for

size_t size

the size to reallocate

unsigned long align

desired alignment

gfp_t flags

the flags for the page level allocator

int nid

NUMA node id

Description

If p is NULL, kvrealloc() behaves exactly like kvmalloc(). If size is 0 and p is not a NULL pointer, the object pointed to is freed.

Only alignments up to those guaranteed by kmalloc() will be honored. Please see Memory Allocation Guide for more details.

If __GFP_ZERO logic is requested, callers must ensure that, starting with the initial memory allocation, every subsequent call to this API for the same memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that __GFP_ZERO is not fully honored by this API.

In any case, the contents of the object pointed to are preserved up to the lesser of the new and old sizes.

This function must not be called concurrently with itself or kvfree() for the same memory allocation.

Return

pointer to the allocated memory or NULL in case of error