1*4882a593SmuzhiyunThe genalloc/genpool subsystem 2*4882a593Smuzhiyun============================== 3*4882a593Smuzhiyun 4*4882a593SmuzhiyunThere are a number of memory-allocation subsystems in the kernel, each 5*4882a593Smuzhiyunaimed at a specific need. Sometimes, however, a kernel developer needs to 6*4882a593Smuzhiyunimplement a new allocator for a specific range of special-purpose memory; 7*4882a593Smuzhiyunoften that memory is located on a device somewhere. The author of the 8*4882a593Smuzhiyundriver for that device can certainly write a little allocator to get the 9*4882a593Smuzhiyunjob done, but that is the way to fill the kernel with dozens of poorly 10*4882a593Smuzhiyuntested allocators. Back in 2005, Jes Sorensen lifted one of those 11*4882a593Smuzhiyunallocators from the sym53c8xx_2 driver and posted_ it as a generic module 12*4882a593Smuzhiyunfor the creation of ad hoc memory allocators. This code was merged 13*4882a593Smuzhiyunfor the 2.6.13 release; it has been modified considerably since then. 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun.. _posted: https://lwn.net/Articles/125842/ 16*4882a593Smuzhiyun 17*4882a593SmuzhiyunCode using this allocator should include <linux/genalloc.h>. The action 18*4882a593Smuzhiyunbegins with the creation of a pool using one of: 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 21*4882a593Smuzhiyun :functions: gen_pool_create 22*4882a593Smuzhiyun 23*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 24*4882a593Smuzhiyun :functions: devm_gen_pool_create 25*4882a593Smuzhiyun 26*4882a593SmuzhiyunA call to gen_pool_create() will create a pool. The granularity of 27*4882a593Smuzhiyunallocations is set with min_alloc_order; it is a log-base-2 number like 28*4882a593Smuzhiyunthose used by the page allocator, but it refers to bytes rather than pages. 29*4882a593SmuzhiyunSo, if min_alloc_order is passed as 3, then all allocations will be a 30*4882a593Smuzhiyunmultiple of eight bytes. Increasing min_alloc_order decreases the memory 31*4882a593Smuzhiyunrequired to track the memory in the pool. The nid parameter specifies 32*4882a593Smuzhiyunwhich NUMA node should be used for the allocation of the housekeeping 33*4882a593Smuzhiyunstructures; it can be -1 if the caller doesn't care. 34*4882a593Smuzhiyun 35*4882a593SmuzhiyunThe "managed" interface devm_gen_pool_create() ties the pool to a 36*4882a593Smuzhiyunspecific device. Among other things, it will automatically clean up the 37*4882a593Smuzhiyunpool when the given device is destroyed. 38*4882a593Smuzhiyun 39*4882a593SmuzhiyunA pool is shut down with: 40*4882a593Smuzhiyun 41*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 42*4882a593Smuzhiyun :functions: gen_pool_destroy 43*4882a593Smuzhiyun 44*4882a593SmuzhiyunIt's worth noting that, if there are still allocations outstanding from the 45*4882a593Smuzhiyungiven pool, this function will take the rather extreme step of invoking 46*4882a593SmuzhiyunBUG(), crashing the entire system. You have been warned. 47*4882a593Smuzhiyun 48*4882a593SmuzhiyunA freshly created pool has no memory to allocate. It is fairly useless in 49*4882a593Smuzhiyunthat state, so one of the first orders of business is usually to add memory 50*4882a593Smuzhiyunto the pool. That can be done with one of: 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun.. kernel-doc:: include/linux/genalloc.h 53*4882a593Smuzhiyun :functions: gen_pool_add 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 56*4882a593Smuzhiyun :functions: gen_pool_add_owner 57*4882a593Smuzhiyun 58*4882a593SmuzhiyunA call to gen_pool_add() will place the size bytes of memory 59*4882a593Smuzhiyunstarting at addr (in the kernel's virtual address space) into the given 60*4882a593Smuzhiyunpool, once again using nid as the node ID for ancillary memory allocations. 61*4882a593SmuzhiyunThe gen_pool_add_virt() variant associates an explicit physical 62*4882a593Smuzhiyunaddress with the memory; this is only necessary if the pool will be used 63*4882a593Smuzhiyunfor DMA allocations. 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunThe functions for allocating memory from the pool (and putting it back) 66*4882a593Smuzhiyunare: 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun.. kernel-doc:: include/linux/genalloc.h 69*4882a593Smuzhiyun :functions: gen_pool_alloc 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 72*4882a593Smuzhiyun :functions: gen_pool_dma_alloc 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 75*4882a593Smuzhiyun :functions: gen_pool_free_owner 76*4882a593Smuzhiyun 77*4882a593SmuzhiyunAs one would expect, gen_pool_alloc() will allocate size< bytes 78*4882a593Smuzhiyunfrom the given pool. The gen_pool_dma_alloc() variant allocates 79*4882a593Smuzhiyunmemory for use with DMA operations, returning the associated physical 80*4882a593Smuzhiyunaddress in the space pointed to by dma. This will only work if the memory 81*4882a593Smuzhiyunwas added with gen_pool_add_virt(). Note that this function 82*4882a593Smuzhiyundeparts from the usual genpool pattern of using unsigned long values to 83*4882a593Smuzhiyunrepresent kernel addresses; it returns a void * instead. 84*4882a593Smuzhiyun 85*4882a593SmuzhiyunThat all seems relatively simple; indeed, some developers clearly found it 86*4882a593Smuzhiyunto be too simple. After all, the interface above provides no control over 87*4882a593Smuzhiyunhow the allocation functions choose which specific piece of memory to 88*4882a593Smuzhiyunreturn. If that sort of control is needed, the following functions will be 89*4882a593Smuzhiyunof interest: 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 92*4882a593Smuzhiyun :functions: gen_pool_alloc_algo_owner 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 95*4882a593Smuzhiyun :functions: gen_pool_set_algo 96*4882a593Smuzhiyun 97*4882a593SmuzhiyunAllocations with gen_pool_alloc_algo() specify an algorithm to be 98*4882a593Smuzhiyunused to choose the memory to be allocated; the default algorithm can be set 99*4882a593Smuzhiyunwith gen_pool_set_algo(). The data value is passed to the 100*4882a593Smuzhiyunalgorithm; most ignore it, but it is occasionally needed. One can, 101*4882a593Smuzhiyunnaturally, write a special-purpose algorithm, but there is a fair set 102*4882a593Smuzhiyunalready available: 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun- gen_pool_first_fit is a simple first-fit allocator; this is the default 105*4882a593Smuzhiyun algorithm if none other has been specified. 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun- gen_pool_first_fit_align forces the allocation to have a specific 108*4882a593Smuzhiyun alignment (passed via data in a genpool_data_align structure). 109*4882a593Smuzhiyun 110*4882a593Smuzhiyun- gen_pool_first_fit_order_align aligns the allocation to the order of the 111*4882a593Smuzhiyun size. A 60-byte allocation will thus be 64-byte aligned, for example. 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun- gen_pool_best_fit, as one would expect, is a simple best-fit allocator. 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun- gen_pool_fixed_alloc allocates at a specific offset (passed in a 116*4882a593Smuzhiyun genpool_data_fixed structure via the data parameter) within the pool. 117*4882a593Smuzhiyun If the indicated memory is not available the allocation fails. 118*4882a593Smuzhiyun 119*4882a593SmuzhiyunThere is a handful of other functions, mostly for purposes like querying 120*4882a593Smuzhiyunthe space available in the pool or iterating through chunks of memory. 121*4882a593SmuzhiyunMost users, however, should not need much beyond what has been described 122*4882a593Smuzhiyunabove. With luck, wider awareness of this module will help to prevent the 123*4882a593Smuzhiyunwriting of special-purpose memory allocators in the future. 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 126*4882a593Smuzhiyun :functions: gen_pool_virt_to_phys 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 129*4882a593Smuzhiyun :functions: gen_pool_for_each_chunk 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 132*4882a593Smuzhiyun :functions: gen_pool_has_addr 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 135*4882a593Smuzhiyun :functions: gen_pool_avail 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 138*4882a593Smuzhiyun :functions: gen_pool_size 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 141*4882a593Smuzhiyun :functions: gen_pool_get 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun.. kernel-doc:: lib/genalloc.c 144*4882a593Smuzhiyun :functions: of_gen_pool_get 145