genalloc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Basic general purpose allocator for managing special purpose
  4. * memory, for example, memory that is not managed by the regular
  5. * kmalloc/kfree interface. Uses for this includes on-device special
  6. * memory, uncached memory etc.
  7. *
  8. * It is safe to use the allocator in NMI handlers and other special
  9. * unblockable contexts that could otherwise deadlock on locks. This
  10. * is implemented by using atomic operations and retries on any
  11. * conflicts. The disadvantage is that there may be livelocks in
  12. * extreme cases. For better scalability, one allocator can be used
  13. * for each CPU.
  14. *
  15. * The lockless operation only works if there is enough memory
  16. * available. If new memory is added to the pool a lock has to be
  17. * still taken. So any user relying on locklessness has to ensure
  18. * that sufficient memory is preallocated.
  19. *
  20. * The basic atomic operation of this allocator is cmpxchg on long.
  21. * On architectures that don't have NMI-safe cmpxchg implementation,
  22. * the allocator can NOT be used in NMI handler. So code uses the
  23. * allocator in NMI handler should depend on
  24. * CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
  25. *
  26. * Copyright 2005 (C) Jes Sorensen <[email protected]>
  27. */
  28. #include <linux/slab.h>
  29. #include <linux/export.h>
  30. #include <linux/bitmap.h>
  31. #include <linux/rculist.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/genalloc.h>
  34. #include <linux/of_device.h>
  35. #include <linux/vmalloc.h>
  36. static inline size_t chunk_size(const struct gen_pool_chunk *chunk)
  37. {
  38. return chunk->end_addr - chunk->start_addr + 1;
  39. }
  40. static int set_bits_ll(unsigned long *addr, unsigned long mask_to_set)
  41. {
  42. unsigned long val, nval;
  43. nval = *addr;
  44. do {
  45. val = nval;
  46. if (val & mask_to_set)
  47. return -EBUSY;
  48. cpu_relax();
  49. } while ((nval = cmpxchg(addr, val, val | mask_to_set)) != val);
  50. return 0;
  51. }
  52. static int clear_bits_ll(unsigned long *addr, unsigned long mask_to_clear)
  53. {
  54. unsigned long val, nval;
  55. nval = *addr;
  56. do {
  57. val = nval;
  58. if ((val & mask_to_clear) != mask_to_clear)
  59. return -EBUSY;
  60. cpu_relax();
  61. } while ((nval = cmpxchg(addr, val, val & ~mask_to_clear)) != val);
  62. return 0;
  63. }
  64. /*
  65. * bitmap_set_ll - set the specified number of bits at the specified position
  66. * @map: pointer to a bitmap
  67. * @start: a bit position in @map
  68. * @nr: number of bits to set
  69. *
  70. * Set @nr bits start from @start in @map lock-lessly. Several users
  71. * can set/clear the same bitmap simultaneously without lock. If two
  72. * users set the same bit, one user will return remain bits, otherwise
  73. * return 0.
  74. */
  75. static unsigned long
  76. bitmap_set_ll(unsigned long *map, unsigned long start, unsigned long nr)
  77. {
  78. unsigned long *p = map + BIT_WORD(start);
  79. const unsigned long size = start + nr;
  80. int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
  81. unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
  82. while (nr >= bits_to_set) {
  83. if (set_bits_ll(p, mask_to_set))
  84. return nr;
  85. nr -= bits_to_set;
  86. bits_to_set = BITS_PER_LONG;
  87. mask_to_set = ~0UL;
  88. p++;
  89. }
  90. if (nr) {
  91. mask_to_set &= BITMAP_LAST_WORD_MASK(size);
  92. if (set_bits_ll(p, mask_to_set))
  93. return nr;
  94. }
  95. return 0;
  96. }
  97. /*
  98. * bitmap_clear_ll - clear the specified number of bits at the specified position
  99. * @map: pointer to a bitmap
  100. * @start: a bit position in @map
  101. * @nr: number of bits to set
  102. *
  103. * Clear @nr bits start from @start in @map lock-lessly. Several users
  104. * can set/clear the same bitmap simultaneously without lock. If two
  105. * users clear the same bit, one user will return remain bits,
  106. * otherwise return 0.
  107. */
  108. static unsigned long
  109. bitmap_clear_ll(unsigned long *map, unsigned long start, unsigned long nr)
  110. {
  111. unsigned long *p = map + BIT_WORD(start);
  112. const unsigned long size = start + nr;
  113. int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
  114. unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
  115. while (nr >= bits_to_clear) {
  116. if (clear_bits_ll(p, mask_to_clear))
  117. return nr;
  118. nr -= bits_to_clear;
  119. bits_to_clear = BITS_PER_LONG;
  120. mask_to_clear = ~0UL;
  121. p++;
  122. }
  123. if (nr) {
  124. mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
  125. if (clear_bits_ll(p, mask_to_clear))
  126. return nr;
  127. }
  128. return 0;
  129. }
  130. /**
  131. * gen_pool_create - create a new special memory pool
  132. * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
  133. * @nid: node id of the node the pool structure should be allocated on, or -1
  134. *
  135. * Create a new special memory pool that can be used to manage special purpose
  136. * memory not managed by the regular kmalloc/kfree interface.
  137. */
  138. struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
  139. {
  140. struct gen_pool *pool;
  141. pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid);
  142. if (pool != NULL) {
  143. spin_lock_init(&pool->lock);
  144. INIT_LIST_HEAD(&pool->chunks);
  145. pool->min_alloc_order = min_alloc_order;
  146. pool->algo = gen_pool_first_fit;
  147. pool->data = NULL;
  148. pool->name = NULL;
  149. }
  150. return pool;
  151. }
  152. EXPORT_SYMBOL(gen_pool_create);
  153. /**
  154. * gen_pool_add_owner- add a new chunk of special memory to the pool
  155. * @pool: pool to add new memory chunk to
  156. * @virt: virtual starting address of memory chunk to add to pool
  157. * @phys: physical starting address of memory chunk to add to pool
  158. * @size: size in bytes of the memory chunk to add to pool
  159. * @nid: node id of the node the chunk structure and bitmap should be
  160. * allocated on, or -1
  161. * @owner: private data the publisher would like to recall at alloc time
  162. *
  163. * Add a new chunk of special memory to the specified pool.
  164. *
  165. * Returns 0 on success or a -ve errno on failure.
  166. */
  167. int gen_pool_add_owner(struct gen_pool *pool, unsigned long virt, phys_addr_t phys,
  168. size_t size, int nid, void *owner)
  169. {
  170. struct gen_pool_chunk *chunk;
  171. unsigned long nbits = size >> pool->min_alloc_order;
  172. unsigned long nbytes = sizeof(struct gen_pool_chunk) +
  173. BITS_TO_LONGS(nbits) * sizeof(long);
  174. chunk = vzalloc_node(nbytes, nid);
  175. if (unlikely(chunk == NULL))
  176. return -ENOMEM;
  177. chunk->phys_addr = phys;
  178. chunk->start_addr = virt;
  179. chunk->end_addr = virt + size - 1;
  180. chunk->owner = owner;
  181. atomic_long_set(&chunk->avail, size);
  182. spin_lock(&pool->lock);
  183. list_add_rcu(&chunk->next_chunk, &pool->chunks);
  184. spin_unlock(&pool->lock);
  185. return 0;
  186. }
  187. EXPORT_SYMBOL(gen_pool_add_owner);
  188. /**
  189. * gen_pool_virt_to_phys - return the physical address of memory
  190. * @pool: pool to allocate from
  191. * @addr: starting address of memory
  192. *
  193. * Returns the physical address on success, or -1 on error.
  194. */
  195. phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long addr)
  196. {
  197. struct gen_pool_chunk *chunk;
  198. phys_addr_t paddr = -1;
  199. rcu_read_lock();
  200. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  201. if (addr >= chunk->start_addr && addr <= chunk->end_addr) {
  202. paddr = chunk->phys_addr + (addr - chunk->start_addr);
  203. break;
  204. }
  205. }
  206. rcu_read_unlock();
  207. return paddr;
  208. }
  209. EXPORT_SYMBOL(gen_pool_virt_to_phys);
  210. /**
  211. * gen_pool_destroy - destroy a special memory pool
  212. * @pool: pool to destroy
  213. *
  214. * Destroy the specified special memory pool. Verifies that there are no
  215. * outstanding allocations.
  216. */
  217. void gen_pool_destroy(struct gen_pool *pool)
  218. {
  219. struct list_head *_chunk, *_next_chunk;
  220. struct gen_pool_chunk *chunk;
  221. int order = pool->min_alloc_order;
  222. unsigned long bit, end_bit;
  223. list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
  224. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  225. list_del(&chunk->next_chunk);
  226. end_bit = chunk_size(chunk) >> order;
  227. bit = find_first_bit(chunk->bits, end_bit);
  228. BUG_ON(bit < end_bit);
  229. vfree(chunk);
  230. }
  231. kfree_const(pool->name);
  232. kfree(pool);
  233. }
  234. EXPORT_SYMBOL(gen_pool_destroy);
  235. /**
  236. * gen_pool_alloc_algo_owner - allocate special memory from the pool
  237. * @pool: pool to allocate from
  238. * @size: number of bytes to allocate from the pool
  239. * @algo: algorithm passed from caller
  240. * @data: data passed to algorithm
  241. * @owner: optionally retrieve the chunk owner
  242. *
  243. * Allocate the requested number of bytes from the specified pool.
  244. * Uses the pool allocation function (with first-fit algorithm by default).
  245. * Can not be used in NMI handler on architectures without
  246. * NMI-safe cmpxchg implementation.
  247. */
  248. unsigned long gen_pool_alloc_algo_owner(struct gen_pool *pool, size_t size,
  249. genpool_algo_t algo, void *data, void **owner)
  250. {
  251. struct gen_pool_chunk *chunk;
  252. unsigned long addr = 0;
  253. int order = pool->min_alloc_order;
  254. unsigned long nbits, start_bit, end_bit, remain;
  255. #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  256. BUG_ON(in_nmi());
  257. #endif
  258. if (owner)
  259. *owner = NULL;
  260. if (size == 0)
  261. return 0;
  262. nbits = (size + (1UL << order) - 1) >> order;
  263. rcu_read_lock();
  264. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  265. if (size > atomic_long_read(&chunk->avail))
  266. continue;
  267. start_bit = 0;
  268. end_bit = chunk_size(chunk) >> order;
  269. retry:
  270. start_bit = algo(chunk->bits, end_bit, start_bit,
  271. nbits, data, pool, chunk->start_addr);
  272. if (start_bit >= end_bit)
  273. continue;
  274. remain = bitmap_set_ll(chunk->bits, start_bit, nbits);
  275. if (remain) {
  276. remain = bitmap_clear_ll(chunk->bits, start_bit,
  277. nbits - remain);
  278. BUG_ON(remain);
  279. goto retry;
  280. }
  281. addr = chunk->start_addr + ((unsigned long)start_bit << order);
  282. size = nbits << order;
  283. atomic_long_sub(size, &chunk->avail);
  284. if (owner)
  285. *owner = chunk->owner;
  286. break;
  287. }
  288. rcu_read_unlock();
  289. return addr;
  290. }
  291. EXPORT_SYMBOL(gen_pool_alloc_algo_owner);
  292. /**
  293. * gen_pool_dma_alloc - allocate special memory from the pool for DMA usage
  294. * @pool: pool to allocate from
  295. * @size: number of bytes to allocate from the pool
  296. * @dma: dma-view physical address return value. Use %NULL if unneeded.
  297. *
  298. * Allocate the requested number of bytes from the specified pool.
  299. * Uses the pool allocation function (with first-fit algorithm by default).
  300. * Can not be used in NMI handler on architectures without
  301. * NMI-safe cmpxchg implementation.
  302. *
  303. * Return: virtual address of the allocated memory, or %NULL on failure
  304. */
  305. void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
  306. {
  307. return gen_pool_dma_alloc_algo(pool, size, dma, pool->algo, pool->data);
  308. }
  309. EXPORT_SYMBOL(gen_pool_dma_alloc);
  310. /**
  311. * gen_pool_dma_alloc_algo - allocate special memory from the pool for DMA
  312. * usage with the given pool algorithm
  313. * @pool: pool to allocate from
  314. * @size: number of bytes to allocate from the pool
  315. * @dma: DMA-view physical address return value. Use %NULL if unneeded.
  316. * @algo: algorithm passed from caller
  317. * @data: data passed to algorithm
  318. *
  319. * Allocate the requested number of bytes from the specified pool. Uses the
  320. * given pool allocation function. Can not be used in NMI handler on
  321. * architectures without NMI-safe cmpxchg implementation.
  322. *
  323. * Return: virtual address of the allocated memory, or %NULL on failure
  324. */
  325. void *gen_pool_dma_alloc_algo(struct gen_pool *pool, size_t size,
  326. dma_addr_t *dma, genpool_algo_t algo, void *data)
  327. {
  328. unsigned long vaddr;
  329. if (!pool)
  330. return NULL;
  331. vaddr = gen_pool_alloc_algo(pool, size, algo, data);
  332. if (!vaddr)
  333. return NULL;
  334. if (dma)
  335. *dma = gen_pool_virt_to_phys(pool, vaddr);
  336. return (void *)vaddr;
  337. }
  338. EXPORT_SYMBOL(gen_pool_dma_alloc_algo);
  339. /**
  340. * gen_pool_dma_alloc_align - allocate special memory from the pool for DMA
  341. * usage with the given alignment
  342. * @pool: pool to allocate from
  343. * @size: number of bytes to allocate from the pool
  344. * @dma: DMA-view physical address return value. Use %NULL if unneeded.
  345. * @align: alignment in bytes for starting address
  346. *
  347. * Allocate the requested number bytes from the specified pool, with the given
  348. * alignment restriction. Can not be used in NMI handler on architectures
  349. * without NMI-safe cmpxchg implementation.
  350. *
  351. * Return: virtual address of the allocated memory, or %NULL on failure
  352. */
  353. void *gen_pool_dma_alloc_align(struct gen_pool *pool, size_t size,
  354. dma_addr_t *dma, int align)
  355. {
  356. struct genpool_data_align data = { .align = align };
  357. return gen_pool_dma_alloc_algo(pool, size, dma,
  358. gen_pool_first_fit_align, &data);
  359. }
  360. EXPORT_SYMBOL(gen_pool_dma_alloc_align);
  361. /**
  362. * gen_pool_dma_zalloc - allocate special zeroed memory from the pool for
  363. * DMA usage
  364. * @pool: pool to allocate from
  365. * @size: number of bytes to allocate from the pool
  366. * @dma: dma-view physical address return value. Use %NULL if unneeded.
  367. *
  368. * Allocate the requested number of zeroed bytes from the specified pool.
  369. * Uses the pool allocation function (with first-fit algorithm by default).
  370. * Can not be used in NMI handler on architectures without
  371. * NMI-safe cmpxchg implementation.
  372. *
  373. * Return: virtual address of the allocated zeroed memory, or %NULL on failure
  374. */
  375. void *gen_pool_dma_zalloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
  376. {
  377. return gen_pool_dma_zalloc_algo(pool, size, dma, pool->algo, pool->data);
  378. }
  379. EXPORT_SYMBOL(gen_pool_dma_zalloc);
  380. /**
  381. * gen_pool_dma_zalloc_algo - allocate special zeroed memory from the pool for
  382. * DMA usage with the given pool algorithm
  383. * @pool: pool to allocate from
  384. * @size: number of bytes to allocate from the pool
  385. * @dma: DMA-view physical address return value. Use %NULL if unneeded.
  386. * @algo: algorithm passed from caller
  387. * @data: data passed to algorithm
  388. *
  389. * Allocate the requested number of zeroed bytes from the specified pool. Uses
  390. * the given pool allocation function. Can not be used in NMI handler on
  391. * architectures without NMI-safe cmpxchg implementation.
  392. *
  393. * Return: virtual address of the allocated zeroed memory, or %NULL on failure
  394. */
  395. void *gen_pool_dma_zalloc_algo(struct gen_pool *pool, size_t size,
  396. dma_addr_t *dma, genpool_algo_t algo, void *data)
  397. {
  398. void *vaddr = gen_pool_dma_alloc_algo(pool, size, dma, algo, data);
  399. if (vaddr)
  400. memset(vaddr, 0, size);
  401. return vaddr;
  402. }
  403. EXPORT_SYMBOL(gen_pool_dma_zalloc_algo);
  404. /**
  405. * gen_pool_dma_zalloc_align - allocate special zeroed memory from the pool for
  406. * DMA usage with the given alignment
  407. * @pool: pool to allocate from
  408. * @size: number of bytes to allocate from the pool
  409. * @dma: DMA-view physical address return value. Use %NULL if unneeded.
  410. * @align: alignment in bytes for starting address
  411. *
  412. * Allocate the requested number of zeroed bytes from the specified pool,
  413. * with the given alignment restriction. Can not be used in NMI handler on
  414. * architectures without NMI-safe cmpxchg implementation.
  415. *
  416. * Return: virtual address of the allocated zeroed memory, or %NULL on failure
  417. */
  418. void *gen_pool_dma_zalloc_align(struct gen_pool *pool, size_t size,
  419. dma_addr_t *dma, int align)
  420. {
  421. struct genpool_data_align data = { .align = align };
  422. return gen_pool_dma_zalloc_algo(pool, size, dma,
  423. gen_pool_first_fit_align, &data);
  424. }
  425. EXPORT_SYMBOL(gen_pool_dma_zalloc_align);
  426. /**
  427. * gen_pool_free_owner - free allocated special memory back to the pool
  428. * @pool: pool to free to
  429. * @addr: starting address of memory to free back to pool
  430. * @size: size in bytes of memory to free
  431. * @owner: private data stashed at gen_pool_add() time
  432. *
  433. * Free previously allocated special memory back to the specified
  434. * pool. Can not be used in NMI handler on architectures without
  435. * NMI-safe cmpxchg implementation.
  436. */
  437. void gen_pool_free_owner(struct gen_pool *pool, unsigned long addr, size_t size,
  438. void **owner)
  439. {
  440. struct gen_pool_chunk *chunk;
  441. int order = pool->min_alloc_order;
  442. unsigned long start_bit, nbits, remain;
  443. #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  444. BUG_ON(in_nmi());
  445. #endif
  446. if (owner)
  447. *owner = NULL;
  448. nbits = (size + (1UL << order) - 1) >> order;
  449. rcu_read_lock();
  450. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  451. if (addr >= chunk->start_addr && addr <= chunk->end_addr) {
  452. BUG_ON(addr + size - 1 > chunk->end_addr);
  453. start_bit = (addr - chunk->start_addr) >> order;
  454. remain = bitmap_clear_ll(chunk->bits, start_bit, nbits);
  455. BUG_ON(remain);
  456. size = nbits << order;
  457. atomic_long_add(size, &chunk->avail);
  458. if (owner)
  459. *owner = chunk->owner;
  460. rcu_read_unlock();
  461. return;
  462. }
  463. }
  464. rcu_read_unlock();
  465. BUG();
  466. }
  467. EXPORT_SYMBOL(gen_pool_free_owner);
  468. /**
  469. * gen_pool_for_each_chunk - call func for every chunk of generic memory pool
  470. * @pool: the generic memory pool
  471. * @func: func to call
  472. * @data: additional data used by @func
  473. *
  474. * Call @func for every chunk of generic memory pool. The @func is
  475. * called with rcu_read_lock held.
  476. */
  477. void gen_pool_for_each_chunk(struct gen_pool *pool,
  478. void (*func)(struct gen_pool *pool, struct gen_pool_chunk *chunk, void *data),
  479. void *data)
  480. {
  481. struct gen_pool_chunk *chunk;
  482. rcu_read_lock();
  483. list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk)
  484. func(pool, chunk, data);
  485. rcu_read_unlock();
  486. }
  487. EXPORT_SYMBOL(gen_pool_for_each_chunk);
  488. /**
  489. * gen_pool_has_addr - checks if an address falls within the range of a pool
  490. * @pool: the generic memory pool
  491. * @start: start address
  492. * @size: size of the region
  493. *
  494. * Check if the range of addresses falls within the specified pool. Returns
  495. * true if the entire range is contained in the pool and false otherwise.
  496. */
  497. bool gen_pool_has_addr(struct gen_pool *pool, unsigned long start,
  498. size_t size)
  499. {
  500. bool found = false;
  501. unsigned long end = start + size - 1;
  502. struct gen_pool_chunk *chunk;
  503. rcu_read_lock();
  504. list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk) {
  505. if (start >= chunk->start_addr && start <= chunk->end_addr) {
  506. if (end <= chunk->end_addr) {
  507. found = true;
  508. break;
  509. }
  510. }
  511. }
  512. rcu_read_unlock();
  513. return found;
  514. }
  515. EXPORT_SYMBOL(gen_pool_has_addr);
  516. /**
  517. * gen_pool_avail - get available free space of the pool
  518. * @pool: pool to get available free space
  519. *
  520. * Return available free space of the specified pool.
  521. */
  522. size_t gen_pool_avail(struct gen_pool *pool)
  523. {
  524. struct gen_pool_chunk *chunk;
  525. size_t avail = 0;
  526. rcu_read_lock();
  527. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
  528. avail += atomic_long_read(&chunk->avail);
  529. rcu_read_unlock();
  530. return avail;
  531. }
  532. EXPORT_SYMBOL_GPL(gen_pool_avail);
  533. /**
  534. * gen_pool_size - get size in bytes of memory managed by the pool
  535. * @pool: pool to get size
  536. *
  537. * Return size in bytes of memory managed by the pool.
  538. */
  539. size_t gen_pool_size(struct gen_pool *pool)
  540. {
  541. struct gen_pool_chunk *chunk;
  542. size_t size = 0;
  543. rcu_read_lock();
  544. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
  545. size += chunk_size(chunk);
  546. rcu_read_unlock();
  547. return size;
  548. }
  549. EXPORT_SYMBOL_GPL(gen_pool_size);
  550. /**
  551. * gen_pool_set_algo - set the allocation algorithm
  552. * @pool: pool to change allocation algorithm
  553. * @algo: custom algorithm function
  554. * @data: additional data used by @algo
  555. *
  556. * Call @algo for each memory allocation in the pool.
  557. * If @algo is NULL use gen_pool_first_fit as default
  558. * memory allocation function.
  559. */
  560. void gen_pool_set_algo(struct gen_pool *pool, genpool_algo_t algo, void *data)
  561. {
  562. rcu_read_lock();
  563. pool->algo = algo;
  564. if (!pool->algo)
  565. pool->algo = gen_pool_first_fit;
  566. pool->data = data;
  567. rcu_read_unlock();
  568. }
  569. EXPORT_SYMBOL(gen_pool_set_algo);
  570. /**
  571. * gen_pool_first_fit - find the first available region
  572. * of memory matching the size requirement (no alignment constraint)
  573. * @map: The address to base the search on
  574. * @size: The bitmap size in bits
  575. * @start: The bitnumber to start searching at
  576. * @nr: The number of zeroed bits we're looking for
  577. * @data: additional data - unused
  578. * @pool: pool to find the fit region memory from
  579. * @start_addr: not used in this function
  580. */
  581. unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size,
  582. unsigned long start, unsigned int nr, void *data,
  583. struct gen_pool *pool, unsigned long start_addr)
  584. {
  585. return bitmap_find_next_zero_area(map, size, start, nr, 0);
  586. }
  587. EXPORT_SYMBOL(gen_pool_first_fit);
  588. /**
  589. * gen_pool_first_fit_align - find the first available region
  590. * of memory matching the size requirement (alignment constraint)
  591. * @map: The address to base the search on
  592. * @size: The bitmap size in bits
  593. * @start: The bitnumber to start searching at
  594. * @nr: The number of zeroed bits we're looking for
  595. * @data: data for alignment
  596. * @pool: pool to get order from
  597. * @start_addr: start addr of alloction chunk
  598. */
  599. unsigned long gen_pool_first_fit_align(unsigned long *map, unsigned long size,
  600. unsigned long start, unsigned int nr, void *data,
  601. struct gen_pool *pool, unsigned long start_addr)
  602. {
  603. struct genpool_data_align *alignment;
  604. unsigned long align_mask, align_off;
  605. int order;
  606. alignment = data;
  607. order = pool->min_alloc_order;
  608. align_mask = ((alignment->align + (1UL << order) - 1) >> order) - 1;
  609. align_off = (start_addr & (alignment->align - 1)) >> order;
  610. return bitmap_find_next_zero_area_off(map, size, start, nr,
  611. align_mask, align_off);
  612. }
  613. EXPORT_SYMBOL(gen_pool_first_fit_align);
  614. /**
  615. * gen_pool_fixed_alloc - reserve a specific region
  616. * @map: The address to base the search on
  617. * @size: The bitmap size in bits
  618. * @start: The bitnumber to start searching at
  619. * @nr: The number of zeroed bits we're looking for
  620. * @data: data for alignment
  621. * @pool: pool to get order from
  622. * @start_addr: not used in this function
  623. */
  624. unsigned long gen_pool_fixed_alloc(unsigned long *map, unsigned long size,
  625. unsigned long start, unsigned int nr, void *data,
  626. struct gen_pool *pool, unsigned long start_addr)
  627. {
  628. struct genpool_data_fixed *fixed_data;
  629. int order;
  630. unsigned long offset_bit;
  631. unsigned long start_bit;
  632. fixed_data = data;
  633. order = pool->min_alloc_order;
  634. offset_bit = fixed_data->offset >> order;
  635. if (WARN_ON(fixed_data->offset & ((1UL << order) - 1)))
  636. return size;
  637. start_bit = bitmap_find_next_zero_area(map, size,
  638. start + offset_bit, nr, 0);
  639. if (start_bit != offset_bit)
  640. start_bit = size;
  641. return start_bit;
  642. }
  643. EXPORT_SYMBOL(gen_pool_fixed_alloc);
  644. /**
  645. * gen_pool_first_fit_order_align - find the first available region
  646. * of memory matching the size requirement. The region will be aligned
  647. * to the order of the size specified.
  648. * @map: The address to base the search on
  649. * @size: The bitmap size in bits
  650. * @start: The bitnumber to start searching at
  651. * @nr: The number of zeroed bits we're looking for
  652. * @data: additional data - unused
  653. * @pool: pool to find the fit region memory from
  654. * @start_addr: not used in this function
  655. */
  656. unsigned long gen_pool_first_fit_order_align(unsigned long *map,
  657. unsigned long size, unsigned long start,
  658. unsigned int nr, void *data, struct gen_pool *pool,
  659. unsigned long start_addr)
  660. {
  661. unsigned long align_mask = roundup_pow_of_two(nr) - 1;
  662. return bitmap_find_next_zero_area(map, size, start, nr, align_mask);
  663. }
  664. EXPORT_SYMBOL(gen_pool_first_fit_order_align);
  665. /**
  666. * gen_pool_best_fit - find the best fitting region of memory
  667. * matching the size requirement (no alignment constraint)
  668. * @map: The address to base the search on
  669. * @size: The bitmap size in bits
  670. * @start: The bitnumber to start searching at
  671. * @nr: The number of zeroed bits we're looking for
  672. * @data: additional data - unused
  673. * @pool: pool to find the fit region memory from
  674. * @start_addr: not used in this function
  675. *
  676. * Iterate over the bitmap to find the smallest free region
  677. * which we can allocate the memory.
  678. */
  679. unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size,
  680. unsigned long start, unsigned int nr, void *data,
  681. struct gen_pool *pool, unsigned long start_addr)
  682. {
  683. unsigned long start_bit = size;
  684. unsigned long len = size + 1;
  685. unsigned long index;
  686. index = bitmap_find_next_zero_area(map, size, start, nr, 0);
  687. while (index < size) {
  688. unsigned long next_bit = find_next_bit(map, size, index + nr);
  689. if ((next_bit - index) < len) {
  690. len = next_bit - index;
  691. start_bit = index;
  692. if (len == nr)
  693. return start_bit;
  694. }
  695. index = bitmap_find_next_zero_area(map, size,
  696. next_bit + 1, nr, 0);
  697. }
  698. return start_bit;
  699. }
  700. EXPORT_SYMBOL(gen_pool_best_fit);
  701. static void devm_gen_pool_release(struct device *dev, void *res)
  702. {
  703. gen_pool_destroy(*(struct gen_pool **)res);
  704. }
  705. static int devm_gen_pool_match(struct device *dev, void *res, void *data)
  706. {
  707. struct gen_pool **p = res;
  708. /* NULL data matches only a pool without an assigned name */
  709. if (!data && !(*p)->name)
  710. return 1;
  711. if (!data || !(*p)->name)
  712. return 0;
  713. return !strcmp((*p)->name, data);
  714. }
  715. /**
  716. * gen_pool_get - Obtain the gen_pool (if any) for a device
  717. * @dev: device to retrieve the gen_pool from
  718. * @name: name of a gen_pool or NULL, identifies a particular gen_pool on device
  719. *
  720. * Returns the gen_pool for the device if one is present, or NULL.
  721. */
  722. struct gen_pool *gen_pool_get(struct device *dev, const char *name)
  723. {
  724. struct gen_pool **p;
  725. p = devres_find(dev, devm_gen_pool_release, devm_gen_pool_match,
  726. (void *)name);
  727. if (!p)
  728. return NULL;
  729. return *p;
  730. }
  731. EXPORT_SYMBOL_GPL(gen_pool_get);
  732. /**
  733. * devm_gen_pool_create - managed gen_pool_create
  734. * @dev: device that provides the gen_pool
  735. * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
  736. * @nid: node selector for allocated gen_pool, %NUMA_NO_NODE for all nodes
  737. * @name: name of a gen_pool or NULL, identifies a particular gen_pool on device
  738. *
  739. * Create a new special memory pool that can be used to manage special purpose
  740. * memory not managed by the regular kmalloc/kfree interface. The pool will be
  741. * automatically destroyed by the device management code.
  742. */
  743. struct gen_pool *devm_gen_pool_create(struct device *dev, int min_alloc_order,
  744. int nid, const char *name)
  745. {
  746. struct gen_pool **ptr, *pool;
  747. const char *pool_name = NULL;
  748. /* Check that genpool to be created is uniquely addressed on device */
  749. if (gen_pool_get(dev, name))
  750. return ERR_PTR(-EINVAL);
  751. if (name) {
  752. pool_name = kstrdup_const(name, GFP_KERNEL);
  753. if (!pool_name)
  754. return ERR_PTR(-ENOMEM);
  755. }
  756. ptr = devres_alloc(devm_gen_pool_release, sizeof(*ptr), GFP_KERNEL);
  757. if (!ptr)
  758. goto free_pool_name;
  759. pool = gen_pool_create(min_alloc_order, nid);
  760. if (!pool)
  761. goto free_devres;
  762. *ptr = pool;
  763. pool->name = pool_name;
  764. devres_add(dev, ptr);
  765. return pool;
  766. free_devres:
  767. devres_free(ptr);
  768. free_pool_name:
  769. kfree_const(pool_name);
  770. return ERR_PTR(-ENOMEM);
  771. }
  772. EXPORT_SYMBOL(devm_gen_pool_create);
  773. #ifdef CONFIG_OF
  774. /**
  775. * of_gen_pool_get - find a pool by phandle property
  776. * @np: device node
  777. * @propname: property name containing phandle(s)
  778. * @index: index into the phandle array
  779. *
  780. * Returns the pool that contains the chunk starting at the physical
  781. * address of the device tree node pointed at by the phandle property,
  782. * or NULL if not found.
  783. */
  784. struct gen_pool *of_gen_pool_get(struct device_node *np,
  785. const char *propname, int index)
  786. {
  787. struct platform_device *pdev;
  788. struct device_node *np_pool, *parent;
  789. const char *name = NULL;
  790. struct gen_pool *pool = NULL;
  791. np_pool = of_parse_phandle(np, propname, index);
  792. if (!np_pool)
  793. return NULL;
  794. pdev = of_find_device_by_node(np_pool);
  795. if (!pdev) {
  796. /* Check if named gen_pool is created by parent node device */
  797. parent = of_get_parent(np_pool);
  798. pdev = of_find_device_by_node(parent);
  799. of_node_put(parent);
  800. of_property_read_string(np_pool, "label", &name);
  801. if (!name)
  802. name = np_pool->name;
  803. }
  804. if (pdev)
  805. pool = gen_pool_get(&pdev->dev, name);
  806. of_node_put(np_pool);
  807. return pool;
  808. }
  809. EXPORT_SYMBOL_GPL(of_gen_pool_get);
  810. #endif /* CONFIG_OF */