0001 The genalloc/genpool subsystem
0002 ==============================
0003
0004 There are a number of memory-allocation subsystems in the kernel, each
0005 aimed at a specific need. Sometimes, however, a kernel developer needs to
0006 implement a new allocator for a specific range of special-purpose memory;
0007 often that memory is located on a device somewhere. The author of the
0008 driver for that device can certainly write a little allocator to get the
0009 job done, but that is the way to fill the kernel with dozens of poorly
0010 tested allocators. Back in 2005, Jes Sorensen lifted one of those
0011 allocators from the sym53c8xx_2 driver and posted_ it as a generic module
0012 for the creation of ad hoc memory allocators. This code was merged
0013 for the 2.6.13 release; it has been modified considerably since then.
0014
0015 .. _posted: https://lwn.net/Articles/125842/
0016
0017 Code using this allocator should include <linux/genalloc.h>. The action
0018 begins with the creation of a pool using one of:
0019
0020 .. kernel-doc:: lib/genalloc.c
0021 :functions: gen_pool_create
0022
0023 .. kernel-doc:: lib/genalloc.c
0024 :functions: devm_gen_pool_create
0025
0026 A call to gen_pool_create() will create a pool. The granularity of
0027 allocations is set with min_alloc_order; it is a log-base-2 number like
0028 those used by the page allocator, but it refers to bytes rather than pages.
0029 So, if min_alloc_order is passed as 3, then all allocations will be a
0030 multiple of eight bytes. Increasing min_alloc_order decreases the memory
0031 required to track the memory in the pool. The nid parameter specifies
0032 which NUMA node should be used for the allocation of the housekeeping
0033 structures; it can be -1 if the caller doesn't care.
0034
0035 The "managed" interface devm_gen_pool_create() ties the pool to a
0036 specific device. Among other things, it will automatically clean up the
0037 pool when the given device is destroyed.
0038
0039 A pool is shut down with:
0040
0041 .. kernel-doc:: lib/genalloc.c
0042 :functions: gen_pool_destroy
0043
0044 It's worth noting that, if there are still allocations outstanding from the
0045 given pool, this function will take the rather extreme step of invoking
0046 BUG(), crashing the entire system. You have been warned.
0047
0048 A freshly created pool has no memory to allocate. It is fairly useless in
0049 that state, so one of the first orders of business is usually to add memory
0050 to the pool. That can be done with one of:
0051
0052 .. kernel-doc:: include/linux/genalloc.h
0053 :functions: gen_pool_add
0054
0055 .. kernel-doc:: lib/genalloc.c
0056 :functions: gen_pool_add_owner
0057
0058 A call to gen_pool_add() will place the size bytes of memory
0059 starting at addr (in the kernel's virtual address space) into the given
0060 pool, once again using nid as the node ID for ancillary memory allocations.
0061 The gen_pool_add_virt() variant associates an explicit physical
0062 address with the memory; this is only necessary if the pool will be used
0063 for DMA allocations.
0064
0065 The functions for allocating memory from the pool (and putting it back)
0066 are:
0067
0068 .. kernel-doc:: include/linux/genalloc.h
0069 :functions: gen_pool_alloc
0070
0071 .. kernel-doc:: lib/genalloc.c
0072 :functions: gen_pool_dma_alloc
0073
0074 .. kernel-doc:: lib/genalloc.c
0075 :functions: gen_pool_free_owner
0076
0077 As one would expect, gen_pool_alloc() will allocate size< bytes
0078 from the given pool. The gen_pool_dma_alloc() variant allocates
0079 memory for use with DMA operations, returning the associated physical
0080 address in the space pointed to by dma. This will only work if the memory
0081 was added with gen_pool_add_virt(). Note that this function
0082 departs from the usual genpool pattern of using unsigned long values to
0083 represent kernel addresses; it returns a void * instead.
0084
0085 That all seems relatively simple; indeed, some developers clearly found it
0086 to be too simple. After all, the interface above provides no control over
0087 how the allocation functions choose which specific piece of memory to
0088 return. If that sort of control is needed, the following functions will be
0089 of interest:
0090
0091 .. kernel-doc:: lib/genalloc.c
0092 :functions: gen_pool_alloc_algo_owner
0093
0094 .. kernel-doc:: lib/genalloc.c
0095 :functions: gen_pool_set_algo
0096
0097 Allocations with gen_pool_alloc_algo() specify an algorithm to be
0098 used to choose the memory to be allocated; the default algorithm can be set
0099 with gen_pool_set_algo(). The data value is passed to the
0100 algorithm; most ignore it, but it is occasionally needed. One can,
0101 naturally, write a special-purpose algorithm, but there is a fair set
0102 already available:
0103
0104 - gen_pool_first_fit is a simple first-fit allocator; this is the default
0105 algorithm if none other has been specified.
0106
0107 - gen_pool_first_fit_align forces the allocation to have a specific
0108 alignment (passed via data in a genpool_data_align structure).
0109
0110 - gen_pool_first_fit_order_align aligns the allocation to the order of the
0111 size. A 60-byte allocation will thus be 64-byte aligned, for example.
0112
0113 - gen_pool_best_fit, as one would expect, is a simple best-fit allocator.
0114
0115 - gen_pool_fixed_alloc allocates at a specific offset (passed in a
0116 genpool_data_fixed structure via the data parameter) within the pool.
0117 If the indicated memory is not available the allocation fails.
0118
0119 There is a handful of other functions, mostly for purposes like querying
0120 the space available in the pool or iterating through chunks of memory.
0121 Most users, however, should not need much beyond what has been described
0122 above. With luck, wider awareness of this module will help to prevent the
0123 writing of special-purpose memory allocators in the future.
0124
0125 .. kernel-doc:: lib/genalloc.c
0126 :functions: gen_pool_virt_to_phys
0127
0128 .. kernel-doc:: lib/genalloc.c
0129 :functions: gen_pool_for_each_chunk
0130
0131 .. kernel-doc:: lib/genalloc.c
0132 :functions: gen_pool_has_addr
0133
0134 .. kernel-doc:: lib/genalloc.c
0135 :functions: gen_pool_avail
0136
0137 .. kernel-doc:: lib/genalloc.c
0138 :functions: gen_pool_size
0139
0140 .. kernel-doc:: lib/genalloc.c
0141 :functions: gen_pool_get
0142
0143 .. kernel-doc:: lib/genalloc.c
0144 :functions: of_gen_pool_get