genalloc.c 26 KB

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