genalloc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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/module.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. }
  141. return pool;
  142. }
  143. EXPORT_SYMBOL(gen_pool_create);
  144. /**
  145. * gen_pool_add_virt - add a new chunk of special memory to the pool
  146. * @pool: pool to add new memory chunk to
  147. * @virt: virtual starting address of memory chunk to add to pool
  148. * @phys: physical starting address of memory chunk to add to pool
  149. * @size: size in bytes of the memory chunk to add to pool
  150. * @nid: node id of the node the chunk structure and bitmap should be
  151. * allocated on, or -1
  152. *
  153. * Add a new chunk of special memory to the specified pool.
  154. *
  155. * Returns 0 on success or a -ve errno on failure.
  156. */
  157. int gen_pool_add_virt(struct gen_pool *pool, unsigned long virt, phys_addr_t phys,
  158. size_t size, int nid)
  159. {
  160. struct gen_pool_chunk *chunk;
  161. int nbits = size >> pool->min_alloc_order;
  162. int nbytes = sizeof(struct gen_pool_chunk) +
  163. (nbits + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
  164. chunk = kmalloc_node(nbytes, GFP_KERNEL | __GFP_ZERO, nid);
  165. if (unlikely(chunk == NULL))
  166. return -ENOMEM;
  167. chunk->phys_addr = phys;
  168. chunk->start_addr = virt;
  169. chunk->end_addr = virt + size;
  170. atomic_set(&chunk->avail, size);
  171. spin_lock(&pool->lock);
  172. list_add_rcu(&chunk->next_chunk, &pool->chunks);
  173. spin_unlock(&pool->lock);
  174. return 0;
  175. }
  176. EXPORT_SYMBOL(gen_pool_add_virt);
  177. /**
  178. * gen_pool_virt_to_phys - return the physical address of memory
  179. * @pool: pool to allocate from
  180. * @addr: starting address of memory
  181. *
  182. * Returns the physical address on success, or -1 on error.
  183. */
  184. phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long addr)
  185. {
  186. struct gen_pool_chunk *chunk;
  187. phys_addr_t paddr = -1;
  188. rcu_read_lock();
  189. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  190. if (addr >= chunk->start_addr && addr < chunk->end_addr) {
  191. paddr = chunk->phys_addr + (addr - chunk->start_addr);
  192. break;
  193. }
  194. }
  195. rcu_read_unlock();
  196. return paddr;
  197. }
  198. EXPORT_SYMBOL(gen_pool_virt_to_phys);
  199. /**
  200. * gen_pool_destroy - destroy a special memory pool
  201. * @pool: pool to destroy
  202. *
  203. * Destroy the specified special memory pool. Verifies that there are no
  204. * outstanding allocations.
  205. */
  206. void gen_pool_destroy(struct gen_pool *pool)
  207. {
  208. struct list_head *_chunk, *_next_chunk;
  209. struct gen_pool_chunk *chunk;
  210. int order = pool->min_alloc_order;
  211. int bit, end_bit;
  212. list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
  213. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  214. list_del(&chunk->next_chunk);
  215. end_bit = (chunk->end_addr - chunk->start_addr) >> order;
  216. bit = find_next_bit(chunk->bits, end_bit, 0);
  217. BUG_ON(bit < end_bit);
  218. kfree(chunk);
  219. }
  220. kfree(pool);
  221. return;
  222. }
  223. EXPORT_SYMBOL(gen_pool_destroy);
  224. /**
  225. * gen_pool_alloc - allocate special memory from the pool
  226. * @pool: pool to allocate from
  227. * @size: number of bytes to allocate from the pool
  228. *
  229. * Allocate the requested number of bytes from the specified pool.
  230. * Uses a first-fit algorithm. Can not be used in NMI handler on
  231. * architectures without NMI-safe cmpxchg implementation.
  232. */
  233. unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
  234. {
  235. struct gen_pool_chunk *chunk;
  236. unsigned long addr = 0;
  237. int order = pool->min_alloc_order;
  238. int nbits, start_bit = 0, end_bit, remain;
  239. #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  240. BUG_ON(in_nmi());
  241. #endif
  242. if (size == 0)
  243. return 0;
  244. nbits = (size + (1UL << order) - 1) >> order;
  245. rcu_read_lock();
  246. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  247. if (size > atomic_read(&chunk->avail))
  248. continue;
  249. end_bit = (chunk->end_addr - chunk->start_addr) >> order;
  250. retry:
  251. start_bit = bitmap_find_next_zero_area(chunk->bits, end_bit,
  252. start_bit, nbits, 0);
  253. if (start_bit >= end_bit)
  254. continue;
  255. remain = bitmap_set_ll(chunk->bits, start_bit, nbits);
  256. if (remain) {
  257. remain = bitmap_clear_ll(chunk->bits, start_bit,
  258. nbits - remain);
  259. BUG_ON(remain);
  260. goto retry;
  261. }
  262. addr = chunk->start_addr + ((unsigned long)start_bit << order);
  263. size = nbits << order;
  264. atomic_sub(size, &chunk->avail);
  265. break;
  266. }
  267. rcu_read_unlock();
  268. return addr;
  269. }
  270. EXPORT_SYMBOL(gen_pool_alloc);
  271. /**
  272. * gen_pool_free - free allocated special memory back to the pool
  273. * @pool: pool to free to
  274. * @addr: starting address of memory to free back to pool
  275. * @size: size in bytes of memory to free
  276. *
  277. * Free previously allocated special memory back to the specified
  278. * pool. Can not be used in NMI handler on architectures without
  279. * NMI-safe cmpxchg implementation.
  280. */
  281. void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
  282. {
  283. struct gen_pool_chunk *chunk;
  284. int order = pool->min_alloc_order;
  285. int start_bit, nbits, remain;
  286. #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
  287. BUG_ON(in_nmi());
  288. #endif
  289. nbits = (size + (1UL << order) - 1) >> order;
  290. rcu_read_lock();
  291. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
  292. if (addr >= chunk->start_addr && addr < chunk->end_addr) {
  293. BUG_ON(addr + size > chunk->end_addr);
  294. start_bit = (addr - chunk->start_addr) >> order;
  295. remain = bitmap_clear_ll(chunk->bits, start_bit, nbits);
  296. BUG_ON(remain);
  297. size = nbits << order;
  298. atomic_add(size, &chunk->avail);
  299. rcu_read_unlock();
  300. return;
  301. }
  302. }
  303. rcu_read_unlock();
  304. BUG();
  305. }
  306. EXPORT_SYMBOL(gen_pool_free);
  307. /**
  308. * gen_pool_for_each_chunk - call func for every chunk of generic memory pool
  309. * @pool: the generic memory pool
  310. * @func: func to call
  311. * @data: additional data used by @func
  312. *
  313. * Call @func for every chunk of generic memory pool. The @func is
  314. * called with rcu_read_lock held.
  315. */
  316. void gen_pool_for_each_chunk(struct gen_pool *pool,
  317. void (*func)(struct gen_pool *pool, struct gen_pool_chunk *chunk, void *data),
  318. void *data)
  319. {
  320. struct gen_pool_chunk *chunk;
  321. rcu_read_lock();
  322. list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk)
  323. func(pool, chunk, data);
  324. rcu_read_unlock();
  325. }
  326. EXPORT_SYMBOL(gen_pool_for_each_chunk);
  327. /**
  328. * gen_pool_avail - get available free space of the pool
  329. * @pool: pool to get available free space
  330. *
  331. * Return available free space of the specified pool.
  332. */
  333. size_t gen_pool_avail(struct gen_pool *pool)
  334. {
  335. struct gen_pool_chunk *chunk;
  336. size_t avail = 0;
  337. rcu_read_lock();
  338. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
  339. avail += atomic_read(&chunk->avail);
  340. rcu_read_unlock();
  341. return avail;
  342. }
  343. EXPORT_SYMBOL_GPL(gen_pool_avail);
  344. /**
  345. * gen_pool_size - get size in bytes of memory managed by the pool
  346. * @pool: pool to get size
  347. *
  348. * Return size in bytes of memory managed by the pool.
  349. */
  350. size_t gen_pool_size(struct gen_pool *pool)
  351. {
  352. struct gen_pool_chunk *chunk;
  353. size_t size = 0;
  354. rcu_read_lock();
  355. list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
  356. size += chunk->end_addr - chunk->start_addr;
  357. rcu_read_unlock();
  358. return size;
  359. }
  360. EXPORT_SYMBOL_GPL(gen_pool_size);