genalloc.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. The genalloc/genpool subsystem
  2. ==============================
  3. There are a number of memory-allocation subsystems in the kernel, each
  4. aimed at a specific need. Sometimes, however, a kernel developer needs to
  5. implement a new allocator for a specific range of special-purpose memory;
  6. often that memory is located on a device somewhere. The author of the
  7. driver for that device can certainly write a little allocator to get the
  8. job done, but that is the way to fill the kernel with dozens of poorly
  9. tested allocators. Back in 2005, Jes Sorensen lifted one of those
  10. allocators from the sym53c8xx_2 driver and posted_ it as a generic module
  11. for the creation of ad hoc memory allocators. This code was merged
  12. for the 2.6.13 release; it has been modified considerably since then.
  13. .. _posted: https://lwn.net/Articles/125842/
  14. Code using this allocator should include <linux/genalloc.h>. The action
  15. begins with the creation of a pool using one of:
  16. .. kernel-doc:: lib/genalloc.c
  17. :functions: gen_pool_create
  18. .. kernel-doc:: lib/genalloc.c
  19. :functions: devm_gen_pool_create
  20. A call to gen_pool_create() will create a pool. The granularity of
  21. allocations is set with min_alloc_order; it is a log-base-2 number like
  22. those used by the page allocator, but it refers to bytes rather than pages.
  23. So, if min_alloc_order is passed as 3, then all allocations will be a
  24. multiple of eight bytes. Increasing min_alloc_order decreases the memory
  25. required to track the memory in the pool. The nid parameter specifies
  26. which NUMA node should be used for the allocation of the housekeeping
  27. structures; it can be -1 if the caller doesn't care.
  28. The "managed" interface devm_gen_pool_create() ties the pool to a
  29. specific device. Among other things, it will automatically clean up the
  30. pool when the given device is destroyed.
  31. A pool is shut down with:
  32. .. kernel-doc:: lib/genalloc.c
  33. :functions: gen_pool_destroy
  34. It's worth noting that, if there are still allocations outstanding from the
  35. given pool, this function will take the rather extreme step of invoking
  36. BUG(), crashing the entire system. You have been warned.
  37. A freshly created pool has no memory to allocate. It is fairly useless in
  38. that state, so one of the first orders of business is usually to add memory
  39. to the pool. That can be done with one of:
  40. .. kernel-doc:: include/linux/genalloc.h
  41. :functions: gen_pool_add
  42. .. kernel-doc:: lib/genalloc.c
  43. :functions: gen_pool_add_owner
  44. A call to gen_pool_add() will place the size bytes of memory
  45. starting at addr (in the kernel's virtual address space) into the given
  46. pool, once again using nid as the node ID for ancillary memory allocations.
  47. The gen_pool_add_virt() variant associates an explicit physical
  48. address with the memory; this is only necessary if the pool will be used
  49. for DMA allocations.
  50. The functions for allocating memory from the pool (and putting it back)
  51. are:
  52. .. kernel-doc:: include/linux/genalloc.h
  53. :functions: gen_pool_alloc
  54. .. kernel-doc:: lib/genalloc.c
  55. :functions: gen_pool_dma_alloc
  56. .. kernel-doc:: lib/genalloc.c
  57. :functions: gen_pool_free_owner
  58. As one would expect, gen_pool_alloc() will allocate size< bytes
  59. from the given pool. The gen_pool_dma_alloc() variant allocates
  60. memory for use with DMA operations, returning the associated physical
  61. address in the space pointed to by dma. This will only work if the memory
  62. was added with gen_pool_add_virt(). Note that this function
  63. departs from the usual genpool pattern of using unsigned long values to
  64. represent kernel addresses; it returns a void * instead.
  65. That all seems relatively simple; indeed, some developers clearly found it
  66. to be too simple. After all, the interface above provides no control over
  67. how the allocation functions choose which specific piece of memory to
  68. return. If that sort of control is needed, the following functions will be
  69. of interest:
  70. .. kernel-doc:: lib/genalloc.c
  71. :functions: gen_pool_alloc_algo_owner
  72. .. kernel-doc:: lib/genalloc.c
  73. :functions: gen_pool_set_algo
  74. Allocations with gen_pool_alloc_algo() specify an algorithm to be
  75. used to choose the memory to be allocated; the default algorithm can be set
  76. with gen_pool_set_algo(). The data value is passed to the
  77. algorithm; most ignore it, but it is occasionally needed. One can,
  78. naturally, write a special-purpose algorithm, but there is a fair set
  79. already available:
  80. - gen_pool_first_fit is a simple first-fit allocator; this is the default
  81. algorithm if none other has been specified.
  82. - gen_pool_first_fit_align forces the allocation to have a specific
  83. alignment (passed via data in a genpool_data_align structure).
  84. - gen_pool_first_fit_order_align aligns the allocation to the order of the
  85. size. A 60-byte allocation will thus be 64-byte aligned, for example.
  86. - gen_pool_best_fit, as one would expect, is a simple best-fit allocator.
  87. - gen_pool_fixed_alloc allocates at a specific offset (passed in a
  88. genpool_data_fixed structure via the data parameter) within the pool.
  89. If the indicated memory is not available the allocation fails.
  90. There is a handful of other functions, mostly for purposes like querying
  91. the space available in the pool or iterating through chunks of memory.
  92. Most users, however, should not need much beyond what has been described
  93. above. With luck, wider awareness of this module will help to prevent the
  94. writing of special-purpose memory allocators in the future.
  95. .. kernel-doc:: lib/genalloc.c
  96. :functions: gen_pool_virt_to_phys
  97. .. kernel-doc:: lib/genalloc.c
  98. :functions: gen_pool_for_each_chunk
  99. .. kernel-doc:: lib/genalloc.c
  100. :functions: gen_pool_has_addr
  101. .. kernel-doc:: lib/genalloc.c
  102. :functions: gen_pool_avail
  103. .. kernel-doc:: lib/genalloc.c
  104. :functions: gen_pool_size
  105. .. kernel-doc:: lib/genalloc.c
  106. :functions: gen_pool_get
  107. .. kernel-doc:: lib/genalloc.c
  108. :functions: of_gen_pool_get