genalloc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Basic general purpose allocator for managing special purpose
  3. * memory, for example, memory that is not managed by the regular
  4. * kmalloc/kfree interface. Uses for this includes on-device special
  5. * memory, uncached memory etc.
  6. *
  7. * It is safe to use the allocator in NMI handlers and other special
  8. * unblockable contexts that could otherwise deadlock on locks. This
  9. * is implemented by using atomic operations and retries on any
  10. * conflicts. The disadvantage is that there may be livelocks in
  11. * extreme cases. For better scalability, one allocator can be used
  12. * for each CPU.
  13. *
  14. * The lockless operation only works if there is enough memory
  15. * available. If new memory is added to the pool a lock has to be
  16. * still taken. So any user relying on locklessness has to ensure
  17. * that sufficient memory is preallocated.
  18. *
  19. * The basic atomic operation of this allocator is cmpxchg on long.
  20. * On architectures that don't have NMI-safe cmpxchg implementation,
  21. * the allocator can NOT be used in NMI handler. So code uses the
  22. * allocator in NMI handler should depend on
  23. * CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
  24. *
  25. * Copyright 2005 (C) Jes Sorensen <jes@trained-monkey.org>
  26. *
  27. * This source code is licensed under the GNU General Public License,
  28. * Version 2. See the file COPYING for more details.
  29. */
  30. #include <linux/slab.h>
  31. #include <linux/export.h>
  32. #include <linux/bitmap.h>
  33. #include <linux/rculist.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/genalloc.h>
  36. #include <linux/of_address.h>
  37. #include <linux/of_device.h>
  38. static int set_bits_ll(unsigned long *addr, unsigned long mask_to_set)
  39. {
  40. unsigned long val, nval;
  41. nval = *addr;
  42. do {
  43. val = nval;
  44. if (val & mask_to_set)
  45. return -EBUSY;
  46. cpu_relax();
  47. } while ((nval = cmpxchg(addr, val, val | mask_to_set)) != val);
  48. return 0;
  49. }
  50. static int clear_bits_ll(unsigned long *addr, unsigned long mask_to_clear)
  51. {
  52. unsigned long val, nval;
  53. nval = *addr;
  54. do {
  55. val = nval;
  56. if ((val & mask_to_clear) != mask_to_clear)
  57. return -EBUSY;
  58. cpu_relax();
  59. } while ((nval = cmpxchg(addr, val, val & ~mask_to_clear)) != val);
  60. return 0;
  61. }
  62. /*
  63. * bitmap_set_ll - set the specified number of bits at the specified position
  64. * @map: pointer to a bitmap
  65. * @start: a bit position in @map
  66. * @nr: number of bits to set
  67. *
  68. * Set @nr bits start from @start in @map lock-lessly. Several users
  69. * can set/clear the same bitmap simultaneously without lock. If two
  70. * users set the same bit, one user will return remain bits, otherwise
  71. * return 0.
  72. */
  73. static int bitmap_set_ll(unsigned long *map, int start, int nr)
  74. {
  75. unsigned long *p = map + BIT_WORD(start);
  76. const int size = start + nr;
  77. int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
  78. unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
  79. while (nr - bits_to_set >= 0) {
  80. if (set_bits_ll(p, mask_to_set))
  81. return nr;
  82. nr -= bits_to_set;
  83. bits_to_set = BITS_PER_LONG;
  84. mask_to_set = ~0UL;
  85. p++;
  86. }
  87. if (nr) {
  88. mask_to_set &= BITMAP_LAST_WORD_MASK(size);
  89. if (set_bits_ll(p, mask_to_set))
  90. return nr;
  91. }
  92. return 0;
  93. }
  94. /*
  95. * bitmap_clear_ll - clear the specified number of bits at the specified position
  96. * @map: pointer to a bitmap
  97. * @start: a bit position in @map
  98. * @nr: number of bits to set
  99. *
  100. * Clear @nr bits start from @start in @map lock-lessly. Several users
  101. * can set/clear the same bitmap simultaneously without lock. If two
  102. * users clear the same bit, one user will return remain bits,
  103. * otherwise return 0.
  104. */
  105. static int bitmap_clear_ll(unsigned long *map, int start, int nr)
  106. {
  107. unsigned long *p = map + BIT_WORD(start);
  108. const int size = start + nr;
  109. int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
  110. unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
  111. while (nr - bits_to_clear >= 0) {
  112. if (clear_bits_ll(p, mask_to_clear))
  113. return nr;
  114. nr -= bits_to_clear;
  115. bits_to_clear = BITS_PER_LONG;
  116. mask_to_clear = ~0UL;
  117. p++;
  118. }
  119. if (nr) {
  120. mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
  121. if (clear_bits_ll(p, mask_to_clear))
  122. return nr;
  123. }
  124. return 0;
  125. }
  126. /**
  127. * gen_pool_create - create a new special memory pool
  128. * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
  129. * @nid: node id of the node the pool structure should be allocated on, or -1
  130. *
  131. * Create a new special memory pool that can be used to manage special purpose
  132. * memory not managed by the regular kmalloc/kfree interface.
  133. */
  134. struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
  135. {
  136. struct gen_pool *pool;
  137. pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid);
  138. if (pool != NULL) {
  139. spin_lock_init(&pool->lock);
  140. INIT_LIST_HEAD(&pool->chunks);
  141. pool->min_alloc_order = min_alloc_order;
  142. pool->algo = gen_pool_first_fit;
  143. pool->data = NULL;
  144. }
  145. return pool;
  146. }
  147. EXPORT_SYMBOL(gen_pool_create);
  148. /**
  149. * gen_pool_add_virt - add a new chunk of special memory to the pool
  150. * @pool: pool to add new memory chunk to
  151. * @virt: virtual starting address of memory chunk to add to pool
  152. * @phys: physical starting address of memory chunk to add to pool
  153. * @size: size in bytes of the memory chunk to add to pool
  154. * @nid: node id of the node the chunk structure and bitmap should be
  155. * allocated on, or -1
  156. *
  157. * Add a new chunk of special memory to the specified pool.
  158. *
  159. * Returns 0 on success or a -ve errno on failure.
  160. */
  161. int gen_pool_add_virt(struct gen_pool *pool, unsigned long virt, phys_addr_t phys,
  162. size_t size, int nid)
  163. {
  164. struct gen_pool_chunk *chunk;
  165. int nbits = size >> pool->min_alloc_order;
  166. int nbytes = sizeof(struct gen_pool_chunk) +
  167. BITS_TO_LONGS(nbits) * sizeof(long);
  168. chunk = kmalloc_node(nbytes, GFP_KERNEL | __GFP_ZERO, nid);
  169. if (unlikely(chunk == NULL))
  170. return -ENOMEM;
  171. chunk->phys_addr = phys;
  172. chunk->start_addr = virt;
  173. chunk->end_addr = virt + size;
  174. atomic_set(&chunk->avail, size);
  175. spin_lock(&pool->lock);
  176. list_add_rcu(&chunk->next_chunk, &pool->chunks);
  177. spin_unlock(&pool->lock);
  178. return 0;
  179. }
  180. EXPORT_SYMBOL(gen_pool_add_virt);
  181. /**
  182. * gen_pool_virt_to_phys - return the physical address of memory
  183. * @pool: pool to allocate from
  184. * @addr: starting address of memory
  185. *
  186. * Returns the physical address on success, or -1 on error.
  187. */
  188. phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long addr)
  189. {
  190. struct gen_pool_chunk *chunk;
  191. phys_addr_t paddr = -1;
  192. rcu_read_lock();
  193. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  194. if (addr >= chunk->start_addr && addr < chunk->end_addr) {
  195. paddr = chunk->phys_addr + (addr - chunk->start_addr);
  196. break;
  197. }
  198. }
  199. rcu_read_unlock();
  200. return paddr;
  201. }
  202. EXPORT_SYMBOL(gen_pool_virt_to_phys);
  203. /**
  204. * gen_pool_destroy - destroy a special memory pool
  205. * @pool: pool to destroy
  206. *
  207. * Destroy the specified special memory pool. Verifies that there are no
  208. * outstanding allocations.
  209. */
  210. void gen_pool_destroy(struct gen_pool *pool)
  211. {
  212. struct list_head *_chunk, *_next_chunk;
  213. struct gen_pool_chunk *chunk;
  214. int order = pool->min_alloc_order;
  215. int bit, end_bit;
  216. list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
  217. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  218. list_del(&chunk->next_chunk);
  219. end_bit = (chunk->end_addr - chunk->start_addr) >> order;
  220. bit = find_next_bit(chunk->bits, end_bit, 0);
  221. BUG_ON(bit < end_bit);
  222. kfree(chunk);
  223. }
  224. kfree(pool);
  225. return;
  226. }
  227. EXPORT_SYMBOL(gen_pool_destroy);
  228. /**
  229. * gen_pool_alloc - allocate special memory from the pool
  230. * @pool: pool to allocate from
  231. * @size: number of bytes to allocate from the pool
  232. *
  233. * Allocate the requested number of bytes from the specified pool.
  234. * Uses the pool allocation function (with first-fit algorithm by default).
  235. * Can not be used in NMI handler on architectures without
  236. * NMI-safe cmpxchg implementation.
  237. */
  238. unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
  239. {
  240. struct gen_pool_chunk *chunk;
  241. unsigned long addr = 0;
  242. int order = pool->min_alloc_order;
  243. int nbits, start_bit = 0, end_bit, remain;
  244. #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  245. BUG_ON(in_nmi());
  246. #endif
  247. if (size == 0)
  248. return 0;
  249. nbits = (size + (1UL << order) - 1) >> order;
  250. rcu_read_lock();
  251. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  252. if (size > atomic_read(&chunk->avail))
  253. continue;
  254. end_bit = (chunk->end_addr - chunk->start_addr) >> order;
  255. retry:
  256. start_bit = pool->algo(chunk->bits, end_bit, start_bit, nbits,
  257. pool->data);
  258. if (start_bit >= end_bit)
  259. continue;
  260. remain = bitmap_set_ll(chunk->bits, start_bit, nbits);
  261. if (remain) {
  262. remain = bitmap_clear_ll(chunk->bits, start_bit,
  263. nbits - remain);
  264. BUG_ON(remain);
  265. goto retry;
  266. }
  267. addr = chunk->start_addr + ((unsigned long)start_bit << order);
  268. size = nbits << order;
  269. atomic_sub(size, &chunk->avail);
  270. break;
  271. }
  272. rcu_read_unlock();
  273. return addr;
  274. }
  275. EXPORT_SYMBOL(gen_pool_alloc);
  276. /**
  277. * gen_pool_free - free allocated special memory back to the pool
  278. * @pool: pool to free to
  279. * @addr: starting address of memory to free back to pool
  280. * @size: size in bytes of memory to free
  281. *
  282. * Free previously allocated special memory back to the specified
  283. * pool. Can not be used in NMI handler on architectures without
  284. * NMI-safe cmpxchg implementation.
  285. */
  286. void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
  287. {
  288. struct gen_pool_chunk *chunk;
  289. int order = pool->min_alloc_order;
  290. int start_bit, nbits, remain;
  291. #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  292. BUG_ON(in_nmi());
  293. #endif
  294. nbits = (size + (1UL << order) - 1) >> order;
  295. rcu_read_lock();
  296. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  297. if (addr >= chunk->start_addr && addr < chunk->end_addr) {
  298. BUG_ON(addr + size > chunk->end_addr);
  299. start_bit = (addr - chunk->start_addr) >> order;
  300. remain = bitmap_clear_ll(chunk->bits, start_bit, nbits);
  301. BUG_ON(remain);
  302. size = nbits << order;
  303. atomic_add(size, &chunk->avail);
  304. rcu_read_unlock();
  305. return;
  306. }
  307. }
  308. rcu_read_unlock();
  309. BUG();
  310. }
  311. EXPORT_SYMBOL(gen_pool_free);
  312. /**
  313. * gen_pool_for_each_chunk - call func for every chunk of generic memory pool
  314. * @pool: the generic memory pool
  315. * @func: func to call
  316. * @data: additional data used by @func
  317. *
  318. * Call @func for every chunk of generic memory pool. The @func is
  319. * called with rcu_read_lock held.
  320. */
  321. void gen_pool_for_each_chunk(struct gen_pool *pool,
  322. void (*func)(struct gen_pool *pool, struct gen_pool_chunk *chunk, void *data),
  323. void *data)
  324. {
  325. struct gen_pool_chunk *chunk;
  326. rcu_read_lock();
  327. list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk)
  328. func(pool, chunk, data);
  329. rcu_read_unlock();
  330. }
  331. EXPORT_SYMBOL(gen_pool_for_each_chunk);
  332. /**
  333. * gen_pool_avail - get available free space of the pool
  334. * @pool: pool to get available free space
  335. *
  336. * Return available free space of the specified pool.
  337. */
  338. size_t gen_pool_avail(struct gen_pool *pool)
  339. {
  340. struct gen_pool_chunk *chunk;
  341. size_t avail = 0;
  342. rcu_read_lock();
  343. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
  344. avail += atomic_read(&chunk->avail);
  345. rcu_read_unlock();
  346. return avail;
  347. }
  348. EXPORT_SYMBOL_GPL(gen_pool_avail);
  349. /**
  350. * gen_pool_size - get size in bytes of memory managed by the pool
  351. * @pool: pool to get size
  352. *
  353. * Return size in bytes of memory managed by the pool.
  354. */
  355. size_t gen_pool_size(struct gen_pool *pool)
  356. {
  357. struct gen_pool_chunk *chunk;
  358. size_t size = 0;
  359. rcu_read_lock();
  360. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
  361. size += chunk->end_addr - chunk->start_addr;
  362. rcu_read_unlock();
  363. return size;
  364. }
  365. EXPORT_SYMBOL_GPL(gen_pool_size);
  366. /**
  367. * gen_pool_set_algo - set the allocation algorithm
  368. * @pool: pool to change allocation algorithm
  369. * @algo: custom algorithm function
  370. * @data: additional data used by @algo
  371. *
  372. * Call @algo for each memory allocation in the pool.
  373. * If @algo is NULL use gen_pool_first_fit as default
  374. * memory allocation function.
  375. */
  376. void gen_pool_set_algo(struct gen_pool *pool, genpool_algo_t algo, void *data)
  377. {
  378. rcu_read_lock();
  379. pool->algo = algo;
  380. if (!pool->algo)
  381. pool->algo = gen_pool_first_fit;
  382. pool->data = data;
  383. rcu_read_unlock();
  384. }
  385. EXPORT_SYMBOL(gen_pool_set_algo);
  386. /**
  387. * gen_pool_first_fit - find the first available region
  388. * of memory matching the size requirement (no alignment constraint)
  389. * @map: The address to base the search on
  390. * @size: The bitmap size in bits
  391. * @start: The bitnumber to start searching at
  392. * @nr: The number of zeroed bits we're looking for
  393. * @data: additional data - unused
  394. */
  395. unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size,
  396. unsigned long start, unsigned int nr, void *data)
  397. {
  398. return bitmap_find_next_zero_area(map, size, start, nr, 0);
  399. }
  400. EXPORT_SYMBOL(gen_pool_first_fit);
  401. /**
  402. * gen_pool_best_fit - find the best fitting region of memory
  403. * macthing the size requirement (no alignment constraint)
  404. * @map: The address to base the search on
  405. * @size: The bitmap size in bits
  406. * @start: The bitnumber to start searching at
  407. * @nr: The number of zeroed bits we're looking for
  408. * @data: additional data - unused
  409. *
  410. * Iterate over the bitmap to find the smallest free region
  411. * which we can allocate the memory.
  412. */
  413. unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size,
  414. unsigned long start, unsigned int nr, void *data)
  415. {
  416. unsigned long start_bit = size;
  417. unsigned long len = size + 1;
  418. unsigned long index;
  419. index = bitmap_find_next_zero_area(map, size, start, nr, 0);
  420. while (index < size) {
  421. int next_bit = find_next_bit(map, size, index + nr);
  422. if ((next_bit - index) < len) {
  423. len = next_bit - index;
  424. start_bit = index;
  425. if (len == nr)
  426. return start_bit;
  427. }
  428. index = bitmap_find_next_zero_area(map, size,
  429. next_bit + 1, nr, 0);
  430. }
  431. return start_bit;
  432. }
  433. EXPORT_SYMBOL(gen_pool_best_fit);
  434. static void devm_gen_pool_release(struct device *dev, void *res)
  435. {
  436. gen_pool_destroy(*(struct gen_pool **)res);
  437. }
  438. /**
  439. * devm_gen_pool_create - managed gen_pool_create
  440. * @dev: device that provides the gen_pool
  441. * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
  442. * @nid: node id of the node the pool structure should be allocated on, or -1
  443. *
  444. * Create a new special memory pool that can be used to manage special purpose
  445. * memory not managed by the regular kmalloc/kfree interface. The pool will be
  446. * automatically destroyed by the device management code.
  447. */
  448. struct gen_pool *devm_gen_pool_create(struct device *dev, int min_alloc_order,
  449. int nid)
  450. {
  451. struct gen_pool **ptr, *pool;
  452. ptr = devres_alloc(devm_gen_pool_release, sizeof(*ptr), GFP_KERNEL);
  453. pool = gen_pool_create(min_alloc_order, nid);
  454. if (pool) {
  455. *ptr = pool;
  456. devres_add(dev, ptr);
  457. } else {
  458. devres_free(ptr);
  459. }
  460. return pool;
  461. }
  462. /**
  463. * dev_get_gen_pool - Obtain the gen_pool (if any) for a device
  464. * @dev: device to retrieve the gen_pool from
  465. * @name: Optional name for the gen_pool, usually NULL
  466. *
  467. * Returns the gen_pool for the device if one is present, or NULL.
  468. */
  469. struct gen_pool *dev_get_gen_pool(struct device *dev)
  470. {
  471. struct gen_pool **p = devres_find(dev, devm_gen_pool_release, NULL,
  472. NULL);
  473. if (!p)
  474. return NULL;
  475. return *p;
  476. }
  477. EXPORT_SYMBOL_GPL(dev_get_gen_pool);
  478. #ifdef CONFIG_OF
  479. /**
  480. * of_get_named_gen_pool - find a pool by phandle property
  481. * @np: device node
  482. * @propname: property name containing phandle(s)
  483. * @index: index into the phandle array
  484. *
  485. * Returns the pool that contains the chunk starting at the physical
  486. * address of the device tree node pointed at by the phandle property,
  487. * or NULL if not found.
  488. */
  489. struct gen_pool *of_get_named_gen_pool(struct device_node *np,
  490. const char *propname, int index)
  491. {
  492. struct platform_device *pdev;
  493. struct device_node *np_pool;
  494. np_pool = of_parse_phandle(np, propname, index);
  495. if (!np_pool)
  496. return NULL;
  497. pdev = of_find_device_by_node(np_pool);
  498. if (!pdev)
  499. return NULL;
  500. return dev_get_gen_pool(&pdev->dev);
  501. }
  502. EXPORT_SYMBOL_GPL(of_get_named_gen_pool);
  503. #endif /* CONFIG_OF */