162306a36Sopenharmony_ciThe genalloc/genpool subsystem
262306a36Sopenharmony_ci==============================
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciThere are a number of memory-allocation subsystems in the kernel, each
562306a36Sopenharmony_ciaimed at a specific need.  Sometimes, however, a kernel developer needs to
662306a36Sopenharmony_ciimplement a new allocator for a specific range of special-purpose memory;
762306a36Sopenharmony_cioften that memory is located on a device somewhere.  The author of the
862306a36Sopenharmony_cidriver for that device can certainly write a little allocator to get the
962306a36Sopenharmony_cijob done, but that is the way to fill the kernel with dozens of poorly
1062306a36Sopenharmony_citested allocators.  Back in 2005, Jes Sorensen lifted one of those
1162306a36Sopenharmony_ciallocators from the sym53c8xx_2 driver and posted_ it as a generic module
1262306a36Sopenharmony_cifor the creation of ad hoc memory allocators.  This code was merged
1362306a36Sopenharmony_cifor the 2.6.13 release; it has been modified considerably since then.
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci.. _posted: https://lwn.net/Articles/125842/
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciCode using this allocator should include <linux/genalloc.h>.  The action
1862306a36Sopenharmony_cibegins with the creation of a pool using one of:
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
2162306a36Sopenharmony_ci   :functions: gen_pool_create		
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
2462306a36Sopenharmony_ci   :functions: devm_gen_pool_create
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ciA call to gen_pool_create() will create a pool.  The granularity of
2762306a36Sopenharmony_ciallocations is set with min_alloc_order; it is a log-base-2 number like
2862306a36Sopenharmony_cithose used by the page allocator, but it refers to bytes rather than pages.
2962306a36Sopenharmony_ciSo, if min_alloc_order is passed as 3, then all allocations will be a
3062306a36Sopenharmony_cimultiple of eight bytes.  Increasing min_alloc_order decreases the memory
3162306a36Sopenharmony_cirequired to track the memory in the pool.  The nid parameter specifies
3262306a36Sopenharmony_ciwhich NUMA node should be used for the allocation of the housekeeping
3362306a36Sopenharmony_cistructures; it can be -1 if the caller doesn't care.
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ciThe "managed" interface devm_gen_pool_create() ties the pool to a
3662306a36Sopenharmony_cispecific device.  Among other things, it will automatically clean up the
3762306a36Sopenharmony_cipool when the given device is destroyed.
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ciA pool is shut down with:
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
4262306a36Sopenharmony_ci   :functions: gen_pool_destroy
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ciIt's worth noting that, if there are still allocations outstanding from the
4562306a36Sopenharmony_cigiven pool, this function will take the rather extreme step of invoking
4662306a36Sopenharmony_ciBUG(), crashing the entire system.  You have been warned.
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ciA freshly created pool has no memory to allocate.  It is fairly useless in
4962306a36Sopenharmony_cithat state, so one of the first orders of business is usually to add memory
5062306a36Sopenharmony_cito the pool.  That can be done with one of:
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci.. kernel-doc:: include/linux/genalloc.h
5362306a36Sopenharmony_ci   :functions: gen_pool_add
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
5662306a36Sopenharmony_ci   :functions: gen_pool_add_owner
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ciA call to gen_pool_add() will place the size bytes of memory
5962306a36Sopenharmony_cistarting at addr (in the kernel's virtual address space) into the given
6062306a36Sopenharmony_cipool, once again using nid as the node ID for ancillary memory allocations.
6162306a36Sopenharmony_ciThe gen_pool_add_virt() variant associates an explicit physical
6262306a36Sopenharmony_ciaddress with the memory; this is only necessary if the pool will be used
6362306a36Sopenharmony_cifor DMA allocations.
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ciThe functions for allocating memory from the pool (and putting it back)
6662306a36Sopenharmony_ciare:
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci.. kernel-doc:: include/linux/genalloc.h
6962306a36Sopenharmony_ci   :functions: gen_pool_alloc
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
7262306a36Sopenharmony_ci   :functions: gen_pool_dma_alloc
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
7562306a36Sopenharmony_ci   :functions: gen_pool_free_owner
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciAs one would expect, gen_pool_alloc() will allocate size< bytes
7862306a36Sopenharmony_cifrom the given pool.  The gen_pool_dma_alloc() variant allocates
7962306a36Sopenharmony_cimemory for use with DMA operations, returning the associated physical
8062306a36Sopenharmony_ciaddress in the space pointed to by dma.  This will only work if the memory
8162306a36Sopenharmony_ciwas added with gen_pool_add_virt().  Note that this function
8262306a36Sopenharmony_cideparts from the usual genpool pattern of using unsigned long values to
8362306a36Sopenharmony_cirepresent kernel addresses; it returns a void * instead.
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ciThat all seems relatively simple; indeed, some developers clearly found it
8662306a36Sopenharmony_cito be too simple.  After all, the interface above provides no control over
8762306a36Sopenharmony_cihow the allocation functions choose which specific piece of memory to
8862306a36Sopenharmony_cireturn.  If that sort of control is needed, the following functions will be
8962306a36Sopenharmony_ciof interest:
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
9262306a36Sopenharmony_ci   :functions: gen_pool_alloc_algo_owner
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
9562306a36Sopenharmony_ci   :functions: gen_pool_set_algo
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciAllocations with gen_pool_alloc_algo() specify an algorithm to be
9862306a36Sopenharmony_ciused to choose the memory to be allocated; the default algorithm can be set
9962306a36Sopenharmony_ciwith gen_pool_set_algo().  The data value is passed to the
10062306a36Sopenharmony_cialgorithm; most ignore it, but it is occasionally needed.  One can,
10162306a36Sopenharmony_cinaturally, write a special-purpose algorithm, but there is a fair set
10262306a36Sopenharmony_cialready available:
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci- gen_pool_first_fit is a simple first-fit allocator; this is the default
10562306a36Sopenharmony_ci  algorithm if none other has been specified.
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci- gen_pool_first_fit_align forces the allocation to have a specific
10862306a36Sopenharmony_ci  alignment (passed via data in a genpool_data_align structure).
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci- gen_pool_first_fit_order_align aligns the allocation to the order of the
11162306a36Sopenharmony_ci  size.  A 60-byte allocation will thus be 64-byte aligned, for example.
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci- gen_pool_best_fit, as one would expect, is a simple best-fit allocator.
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci- gen_pool_fixed_alloc allocates at a specific offset (passed in a
11662306a36Sopenharmony_ci  genpool_data_fixed structure via the data parameter) within the pool.
11762306a36Sopenharmony_ci  If the indicated memory is not available the allocation fails.
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ciThere is a handful of other functions, mostly for purposes like querying
12062306a36Sopenharmony_cithe space available in the pool or iterating through chunks of memory.
12162306a36Sopenharmony_ciMost users, however, should not need much beyond what has been described
12262306a36Sopenharmony_ciabove.  With luck, wider awareness of this module will help to prevent the
12362306a36Sopenharmony_ciwriting of special-purpose memory allocators in the future.
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
12662306a36Sopenharmony_ci   :functions: gen_pool_virt_to_phys
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
12962306a36Sopenharmony_ci   :functions: gen_pool_for_each_chunk
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
13262306a36Sopenharmony_ci   :functions: gen_pool_has_addr
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
13562306a36Sopenharmony_ci   :functions: gen_pool_avail
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
13862306a36Sopenharmony_ci   :functions: gen_pool_size
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
14162306a36Sopenharmony_ci   :functions: gen_pool_get
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci.. kernel-doc:: lib/genalloc.c
14462306a36Sopenharmony_ci   :functions: of_gen_pool_get
145