genalloc.c 12 KB

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