genalloc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. static int set_bits_ll(unsigned long *addr, unsigned long mask_to_set)
  37. {
  38. unsigned long val, nval;
  39. nval = *addr;
  40. do {
  41. val = nval;
  42. if (val & mask_to_set)
  43. return -EBUSY;
  44. cpu_relax();
  45. } while ((nval = cmpxchg(addr, val, val | mask_to_set)) != val);
  46. return 0;
  47. }
  48. static int clear_bits_ll(unsigned long *addr, unsigned long mask_to_clear)
  49. {
  50. unsigned long val, nval;
  51. nval = *addr;
  52. do {
  53. val = nval;
  54. if ((val & mask_to_clear) != mask_to_clear)
  55. return -EBUSY;
  56. cpu_relax();
  57. } while ((nval = cmpxchg(addr, val, val & ~mask_to_clear)) != val);
  58. return 0;
  59. }
  60. /*
  61. * bitmap_set_ll - set the specified number of bits at the specified position
  62. * @map: pointer to a bitmap
  63. * @start: a bit position in @map
  64. * @nr: number of bits to set
  65. *
  66. * Set @nr bits start from @start in @map lock-lessly. Several users
  67. * can set/clear the same bitmap simultaneously without lock. If two
  68. * users set the same bit, one user will return remain bits, otherwise
  69. * return 0.
  70. */
  71. static int bitmap_set_ll(unsigned long *map, int start, int nr)
  72. {
  73. unsigned long *p = map + BIT_WORD(start);
  74. const int size = start + nr;
  75. int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
  76. unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
  77. while (nr - bits_to_set >= 0) {
  78. if (set_bits_ll(p, mask_to_set))
  79. return nr;
  80. nr -= bits_to_set;
  81. bits_to_set = BITS_PER_LONG;
  82. mask_to_set = ~0UL;
  83. p++;
  84. }
  85. if (nr) {
  86. mask_to_set &= BITMAP_LAST_WORD_MASK(size);
  87. if (set_bits_ll(p, mask_to_set))
  88. return nr;
  89. }
  90. return 0;
  91. }
  92. /*
  93. * bitmap_clear_ll - clear the specified number of bits at the specified position
  94. * @map: pointer to a bitmap
  95. * @start: a bit position in @map
  96. * @nr: number of bits to set
  97. *
  98. * Clear @nr bits start from @start in @map lock-lessly. Several users
  99. * can set/clear the same bitmap simultaneously without lock. If two
  100. * users clear the same bit, one user will return remain bits,
  101. * otherwise return 0.
  102. */
  103. static int bitmap_clear_ll(unsigned long *map, int start, int nr)
  104. {
  105. unsigned long *p = map + BIT_WORD(start);
  106. const int size = start + nr;
  107. int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
  108. unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
  109. while (nr - bits_to_clear >= 0) {
  110. if (clear_bits_ll(p, mask_to_clear))
  111. return nr;
  112. nr -= bits_to_clear;
  113. bits_to_clear = BITS_PER_LONG;
  114. mask_to_clear = ~0UL;
  115. p++;
  116. }
  117. if (nr) {
  118. mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
  119. if (clear_bits_ll(p, mask_to_clear))
  120. return nr;
  121. }
  122. return 0;
  123. }
  124. /**
  125. * gen_pool_create - create a new special memory pool
  126. * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
  127. * @nid: node id of the node the pool structure should be allocated on, or -1
  128. *
  129. * Create a new special memory pool that can be used to manage special purpose
  130. * memory not managed by the regular kmalloc/kfree interface.
  131. */
  132. struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
  133. {
  134. struct gen_pool *pool;
  135. pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid);
  136. if (pool != NULL) {
  137. spin_lock_init(&pool->lock);
  138. INIT_LIST_HEAD(&pool->chunks);
  139. pool->min_alloc_order = min_alloc_order;
  140. pool->algo = gen_pool_first_fit;
  141. pool->data = NULL;
  142. }
  143. return pool;
  144. }
  145. EXPORT_SYMBOL(gen_pool_create);
  146. /**
  147. * gen_pool_add_virt - add a new chunk of special memory to the pool
  148. * @pool: pool to add new memory chunk to
  149. * @virt: virtual starting address of memory chunk to add to pool
  150. * @phys: physical starting address of memory chunk to add to pool
  151. * @size: size in bytes of the memory chunk to add to pool
  152. * @nid: node id of the node the chunk structure and bitmap should be
  153. * allocated on, or -1
  154. *
  155. * Add a new chunk of special memory to the specified pool.
  156. *
  157. * Returns 0 on success or a -ve errno on failure.
  158. */
  159. int gen_pool_add_virt(struct gen_pool *pool, unsigned long virt, phys_addr_t phys,
  160. size_t size, int nid)
  161. {
  162. struct gen_pool_chunk *chunk;
  163. int nbits = size >> pool->min_alloc_order;
  164. int nbytes = sizeof(struct gen_pool_chunk) +
  165. BITS_TO_LONGS(nbits) * sizeof(long);
  166. chunk = kmalloc_node(nbytes, GFP_KERNEL | __GFP_ZERO, nid);
  167. if (unlikely(chunk == NULL))
  168. return -ENOMEM;
  169. chunk->phys_addr = phys;
  170. chunk->start_addr = virt;
  171. chunk->end_addr = virt + size;
  172. atomic_set(&chunk->avail, size);
  173. spin_lock(&pool->lock);
  174. list_add_rcu(&chunk->next_chunk, &pool->chunks);
  175. spin_unlock(&pool->lock);
  176. return 0;
  177. }
  178. EXPORT_SYMBOL(gen_pool_add_virt);
  179. /**
  180. * gen_pool_virt_to_phys - return the physical address of memory
  181. * @pool: pool to allocate from
  182. * @addr: starting address of memory
  183. *
  184. * Returns the physical address on success, or -1 on error.
  185. */
  186. phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long addr)
  187. {
  188. struct gen_pool_chunk *chunk;
  189. phys_addr_t paddr = -1;
  190. rcu_read_lock();
  191. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  192. if (addr >= chunk->start_addr && addr < chunk->end_addr) {
  193. paddr = chunk->phys_addr + (addr - chunk->start_addr);
  194. break;
  195. }
  196. }
  197. rcu_read_unlock();
  198. return paddr;
  199. }
  200. EXPORT_SYMBOL(gen_pool_virt_to_phys);
  201. /**
  202. * gen_pool_destroy - destroy a special memory pool
  203. * @pool: pool to destroy
  204. *
  205. * Destroy the specified special memory pool. Verifies that there are no
  206. * outstanding allocations.
  207. */
  208. void gen_pool_destroy(struct gen_pool *pool)
  209. {
  210. struct list_head *_chunk, *_next_chunk;
  211. struct gen_pool_chunk *chunk;
  212. int order = pool->min_alloc_order;
  213. int bit, end_bit;
  214. list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
  215. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  216. list_del(&chunk->next_chunk);
  217. end_bit = (chunk->end_addr - chunk->start_addr) >> order;
  218. bit = find_next_bit(chunk->bits, end_bit, 0);
  219. BUG_ON(bit < end_bit);
  220. kfree(chunk);
  221. }
  222. kfree(pool);
  223. return;
  224. }
  225. EXPORT_SYMBOL(gen_pool_destroy);
  226. /**
  227. * gen_pool_alloc - allocate special memory from the pool
  228. * @pool: pool to allocate from
  229. * @size: number of bytes to allocate from the pool
  230. *
  231. * Allocate the requested number of bytes from the specified pool.
  232. * Uses the pool allocation function (with first-fit algorithm by default).
  233. * Can not be used in NMI handler on architectures without
  234. * NMI-safe cmpxchg implementation.
  235. */
  236. unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
  237. {
  238. struct gen_pool_chunk *chunk;
  239. unsigned long addr = 0;
  240. int order = pool->min_alloc_order;
  241. int nbits, start_bit = 0, end_bit, remain;
  242. #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  243. BUG_ON(in_nmi());
  244. #endif
  245. if (size == 0)
  246. return 0;
  247. nbits = (size + (1UL << order) - 1) >> order;
  248. rcu_read_lock();
  249. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  250. if (size > atomic_read(&chunk->avail))
  251. continue;
  252. end_bit = (chunk->end_addr - chunk->start_addr) >> order;
  253. retry:
  254. start_bit = pool->algo(chunk->bits, end_bit, start_bit, nbits,
  255. pool->data);
  256. if (start_bit >= end_bit)
  257. continue;
  258. remain = bitmap_set_ll(chunk->bits, start_bit, nbits);
  259. if (remain) {
  260. remain = bitmap_clear_ll(chunk->bits, start_bit,
  261. nbits - remain);
  262. BUG_ON(remain);
  263. goto retry;
  264. }
  265. addr = chunk->start_addr + ((unsigned long)start_bit << order);
  266. size = nbits << order;
  267. atomic_sub(size, &chunk->avail);
  268. break;
  269. }
  270. rcu_read_unlock();
  271. return addr;
  272. }
  273. EXPORT_SYMBOL(gen_pool_alloc);
  274. /**
  275. * gen_pool_free - free allocated special memory back to the pool
  276. * @pool: pool to free to
  277. * @addr: starting address of memory to free back to pool
  278. * @size: size in bytes of memory to free
  279. *
  280. * Free previously allocated special memory back to the specified
  281. * pool. Can not be used in NMI handler on architectures without
  282. * NMI-safe cmpxchg implementation.
  283. */
  284. void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
  285. {
  286. struct gen_pool_chunk *chunk;
  287. int order = pool->min_alloc_order;
  288. int start_bit, nbits, remain;
  289. #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  290. BUG_ON(in_nmi());
  291. #endif
  292. nbits = (size + (1UL << order) - 1) >> order;
  293. rcu_read_lock();
  294. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  295. if (addr >= chunk->start_addr && addr < chunk->end_addr) {
  296. BUG_ON(addr + size > chunk->end_addr);
  297. start_bit = (addr - chunk->start_addr) >> order;
  298. remain = bitmap_clear_ll(chunk->bits, start_bit, nbits);
  299. BUG_ON(remain);
  300. size = nbits << order;
  301. atomic_add(size, &chunk->avail);
  302. rcu_read_unlock();
  303. return;
  304. }
  305. }
  306. rcu_read_unlock();
  307. BUG();
  308. }
  309. EXPORT_SYMBOL(gen_pool_free);
  310. /**
  311. * gen_pool_for_each_chunk - call func for every chunk of generic memory pool
  312. * @pool: the generic memory pool
  313. * @func: func to call
  314. * @data: additional data used by @func
  315. *
  316. * Call @func for every chunk of generic memory pool. The @func is
  317. * called with rcu_read_lock held.
  318. */
  319. void gen_pool_for_each_chunk(struct gen_pool *pool,
  320. void (*func)(struct gen_pool *pool, struct gen_pool_chunk *chunk, void *data),
  321. void *data)
  322. {
  323. struct gen_pool_chunk *chunk;
  324. rcu_read_lock();
  325. list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk)
  326. func(pool, chunk, data);
  327. rcu_read_unlock();
  328. }
  329. EXPORT_SYMBOL(gen_pool_for_each_chunk);
  330. /**
  331. * gen_pool_avail - get available free space of the pool
  332. * @pool: pool to get available free space
  333. *
  334. * Return available free space of the specified pool.
  335. */
  336. size_t gen_pool_avail(struct gen_pool *pool)
  337. {
  338. struct gen_pool_chunk *chunk;
  339. size_t avail = 0;
  340. rcu_read_lock();
  341. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
  342. avail += atomic_read(&chunk->avail);
  343. rcu_read_unlock();
  344. return avail;
  345. }
  346. EXPORT_SYMBOL_GPL(gen_pool_avail);
  347. /**
  348. * gen_pool_size - get size in bytes of memory managed by the pool
  349. * @pool: pool to get size
  350. *
  351. * Return size in bytes of memory managed by the pool.
  352. */
  353. size_t gen_pool_size(struct gen_pool *pool)
  354. {
  355. struct gen_pool_chunk *chunk;
  356. size_t size = 0;
  357. rcu_read_lock();
  358. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
  359. size += chunk->end_addr - chunk->start_addr;
  360. rcu_read_unlock();
  361. return size;
  362. }
  363. EXPORT_SYMBOL_GPL(gen_pool_size);
  364. /**
  365. * gen_pool_set_algo - set the allocation algorithm
  366. * @pool: pool to change allocation algorithm
  367. * @algo: custom algorithm function
  368. * @data: additional data used by @algo
  369. *
  370. * Call @algo for each memory allocation in the pool.
  371. * If @algo is NULL use gen_pool_first_fit as default
  372. * memory allocation function.
  373. */
  374. void gen_pool_set_algo(struct gen_pool *pool, genpool_algo_t algo, void *data)
  375. {
  376. rcu_read_lock();
  377. pool->algo = algo;
  378. if (!pool->algo)
  379. pool->algo = gen_pool_first_fit;
  380. pool->data = data;
  381. rcu_read_unlock();
  382. }
  383. EXPORT_SYMBOL(gen_pool_set_algo);
  384. /**
  385. * gen_pool_first_fit - find the first available region
  386. * of memory matching the size requirement (no alignment constraint)
  387. * @map: The address to base the search on
  388. * @size: The bitmap size in bits
  389. * @start: The bitnumber to start searching at
  390. * @nr: The number of zeroed bits we're looking for
  391. * @data: additional data - unused
  392. */
  393. unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size,
  394. unsigned long start, unsigned int nr, void *data)
  395. {
  396. return bitmap_find_next_zero_area(map, size, start, nr, 0);
  397. }
  398. EXPORT_SYMBOL(gen_pool_first_fit);
  399. /**
  400. * gen_pool_best_fit - find the best fitting region of memory
  401. * macthing the size requirement (no alignment constraint)
  402. * @map: The address to base the search on
  403. * @size: The bitmap size in bits
  404. * @start: The bitnumber to start searching at
  405. * @nr: The number of zeroed bits we're looking for
  406. * @data: additional data - unused
  407. *
  408. * Iterate over the bitmap to find the smallest free region
  409. * which we can allocate the memory.
  410. */
  411. unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size,
  412. unsigned long start, unsigned int nr, void *data)
  413. {
  414. unsigned long start_bit = size;
  415. unsigned long len = size + 1;
  416. unsigned long index;
  417. index = bitmap_find_next_zero_area(map, size, start, nr, 0);
  418. while (index < size) {
  419. int next_bit = find_next_bit(map, size, index + nr);
  420. if ((next_bit - index) < len) {
  421. len = next_bit - index;
  422. start_bit = index;
  423. if (len == nr)
  424. return start_bit;
  425. }
  426. index = bitmap_find_next_zero_area(map, size,
  427. next_bit + 1, nr, 0);
  428. }
  429. return start_bit;
  430. }
  431. EXPORT_SYMBOL(gen_pool_best_fit);