blob: fd699ca9443499f05f9bef9dd848a325653fab70 [file] [log] [blame]
Jes Sorensenf14f75b2005-06-21 17:15:02 -07001/*
Huang Ying7f184272011-07-13 13:14:24 +08002 * 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.
Jes Sorensenf14f75b2005-06-21 17:15:02 -070024 *
Jes Sorensenf14f75b2005-06-21 17:15:02 -070025 * 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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050032#include <linux/export.h>
Akinobu Mita243797f2009-12-15 16:48:31 -080033#include <linux/bitmap.h>
Huang Ying7f184272011-07-13 13:14:24 +080034#include <linux/rculist.h>
35#include <linux/interrupt.h>
Jes Sorensenf14f75b2005-06-21 17:15:02 -070036#include <linux/genalloc.h>
Shubhraprakash Dase4614572012-08-14 00:25:43 -070037#include <linux/vmalloc.h>
Jes Sorensenf14f75b2005-06-21 17:15:02 -070038
Huang Ying7f184272011-07-13 13:14:24 +080039static int set_bits_ll(unsigned long *addr, unsigned long mask_to_set)
40{
41 unsigned long val, nval;
42
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
51 return 0;
52}
53
54static int clear_bits_ll(unsigned long *addr, unsigned long mask_to_clear)
55{
56 unsigned long val, nval;
57
58 nval = *addr;
59 do {
60 val = nval;
61 if ((val & mask_to_clear) != mask_to_clear)
62 return -EBUSY;
63 cpu_relax();
64 } while ((nval = cmpxchg(addr, val, val & ~mask_to_clear)) != val);
65
66 return 0;
67}
68
69/*
70 * bitmap_set_ll - set the specified number of bits at the specified position
71 * @map: pointer to a bitmap
72 * @start: a bit position in @map
73 * @nr: number of bits to set
74 *
75 * Set @nr bits start from @start in @map lock-lessly. Several users
76 * can set/clear the same bitmap simultaneously without lock. If two
77 * users set the same bit, one user will return remain bits, otherwise
78 * return 0.
79 */
80static int bitmap_set_ll(unsigned long *map, int start, int nr)
81{
82 unsigned long *p = map + BIT_WORD(start);
83 const int size = start + nr;
84 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
85 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
86
87 while (nr - bits_to_set >= 0) {
88 if (set_bits_ll(p, mask_to_set))
89 return nr;
90 nr -= bits_to_set;
91 bits_to_set = BITS_PER_LONG;
92 mask_to_set = ~0UL;
93 p++;
94 }
95 if (nr) {
96 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
97 if (set_bits_ll(p, mask_to_set))
98 return nr;
99 }
100
101 return 0;
102}
103
104/*
105 * bitmap_clear_ll - clear the specified number of bits at the specified position
106 * @map: pointer to a bitmap
107 * @start: a bit position in @map
108 * @nr: number of bits to set
109 *
110 * Clear @nr bits start from @start in @map lock-lessly. Several users
111 * can set/clear the same bitmap simultaneously without lock. If two
112 * users clear the same bit, one user will return remain bits,
113 * otherwise return 0.
114 */
115static int bitmap_clear_ll(unsigned long *map, int start, int nr)
116{
117 unsigned long *p = map + BIT_WORD(start);
118 const int size = start + nr;
119 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
120 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
121
122 while (nr - bits_to_clear >= 0) {
123 if (clear_bits_ll(p, mask_to_clear))
124 return nr;
125 nr -= bits_to_clear;
126 bits_to_clear = BITS_PER_LONG;
127 mask_to_clear = ~0UL;
128 p++;
129 }
130 if (nr) {
131 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
132 if (clear_bits_ll(p, mask_to_clear))
133 return nr;
134 }
135
136 return 0;
137}
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700138
Dean Nelsona58cbd72006-10-02 02:17:01 -0700139/**
140 * gen_pool_create - create a new special memory pool
Dean Nelson929f9722006-06-23 02:03:21 -0700141 * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
142 * @nid: node id of the node the pool structure should be allocated on, or -1
Dean Nelsona58cbd72006-10-02 02:17:01 -0700143 *
144 * Create a new special memory pool that can be used to manage special purpose
145 * memory not managed by the regular kmalloc/kfree interface.
Dean Nelson929f9722006-06-23 02:03:21 -0700146 */
147struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700148{
Dean Nelson929f9722006-06-23 02:03:21 -0700149 struct gen_pool *pool;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700150
Dean Nelson929f9722006-06-23 02:03:21 -0700151 pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid);
152 if (pool != NULL) {
Huang Ying7f184272011-07-13 13:14:24 +0800153 spin_lock_init(&pool->lock);
Dean Nelson929f9722006-06-23 02:03:21 -0700154 INIT_LIST_HEAD(&pool->chunks);
155 pool->min_alloc_order = min_alloc_order;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700156 }
Dean Nelson929f9722006-06-23 02:03:21 -0700157 return pool;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700158}
159EXPORT_SYMBOL(gen_pool_create);
160
Dean Nelsona58cbd72006-10-02 02:17:01 -0700161/**
Jean-Christophe PLAGNIOL-VILLARD3c8f3702011-05-24 17:13:34 -0700162 * gen_pool_add_virt - add a new chunk of special memory to the pool
Dean Nelson929f9722006-06-23 02:03:21 -0700163 * @pool: pool to add new memory chunk to
Jean-Christophe PLAGNIOL-VILLARD3c8f3702011-05-24 17:13:34 -0700164 * @virt: virtual starting address of memory chunk to add to pool
165 * @phys: physical starting address of memory chunk to add to pool
Dean Nelson929f9722006-06-23 02:03:21 -0700166 * @size: size in bytes of the memory chunk to add to pool
167 * @nid: node id of the node the chunk structure and bitmap should be
168 * allocated on, or -1
Dean Nelsona58cbd72006-10-02 02:17:01 -0700169 *
170 * Add a new chunk of special memory to the specified pool.
Jean-Christophe PLAGNIOL-VILLARD3c8f3702011-05-24 17:13:34 -0700171 *
172 * Returns 0 on success or a -ve errno on failure.
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700173 */
Laura Abbottdb1db162013-04-08 14:43:14 -0700174int gen_pool_add_virt(struct gen_pool *pool, u64 virt, phys_addr_t phys,
Jean-Christophe PLAGNIOL-VILLARD3c8f3702011-05-24 17:13:34 -0700175 size_t size, int nid)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700176{
Dean Nelson929f9722006-06-23 02:03:21 -0700177 struct gen_pool_chunk *chunk;
178 int nbits = size >> pool->min_alloc_order;
179 int nbytes = sizeof(struct gen_pool_chunk) +
Thadeu Lima de Souza Cascardo1e0cac62012-10-25 13:37:51 -0700180 BITS_TO_LONGS(nbits) * sizeof(long);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700181
Shubhraprakash Dase4614572012-08-14 00:25:43 -0700182 if (nbytes <= PAGE_SIZE)
183 chunk = kmalloc_node(nbytes, __GFP_ZERO, nid);
184 else
185 chunk = vmalloc(nbytes);
Dean Nelson929f9722006-06-23 02:03:21 -0700186 if (unlikely(chunk == NULL))
Jean-Christophe PLAGNIOL-VILLARD3c8f3702011-05-24 17:13:34 -0700187 return -ENOMEM;
Shubhraprakash Dase4614572012-08-14 00:25:43 -0700188 if (nbytes > PAGE_SIZE)
189 memset(chunk, 0, nbytes);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700190
Jean-Christophe PLAGNIOL-VILLARD3c8f3702011-05-24 17:13:34 -0700191 chunk->phys_addr = phys;
192 chunk->start_addr = virt;
193 chunk->end_addr = virt + size;
Huang Ying7f184272011-07-13 13:14:24 +0800194 atomic_set(&chunk->avail, size);
Dean Nelson929f9722006-06-23 02:03:21 -0700195
Huang Ying7f184272011-07-13 13:14:24 +0800196 spin_lock(&pool->lock);
197 list_add_rcu(&chunk->next_chunk, &pool->chunks);
198 spin_unlock(&pool->lock);
Dean Nelson929f9722006-06-23 02:03:21 -0700199
200 return 0;
201}
Jean-Christophe PLAGNIOL-VILLARD3c8f3702011-05-24 17:13:34 -0700202EXPORT_SYMBOL(gen_pool_add_virt);
203
204/**
205 * gen_pool_virt_to_phys - return the physical address of memory
206 * @pool: pool to allocate from
207 * @addr: starting address of memory
208 *
209 * Returns the physical address on success, or -1 on error.
210 */
Laura Abbottdb1db162013-04-08 14:43:14 -0700211phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, u64 addr)
Jean-Christophe PLAGNIOL-VILLARD3c8f3702011-05-24 17:13:34 -0700212{
Jean-Christophe PLAGNIOL-VILLARD3c8f3702011-05-24 17:13:34 -0700213 struct gen_pool_chunk *chunk;
Huang Ying7f184272011-07-13 13:14:24 +0800214 phys_addr_t paddr = -1;
Jean-Christophe PLAGNIOL-VILLARD3c8f3702011-05-24 17:13:34 -0700215
Huang Ying7f184272011-07-13 13:14:24 +0800216 rcu_read_lock();
217 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
218 if (addr >= chunk->start_addr && addr < chunk->end_addr) {
219 paddr = chunk->phys_addr + (addr - chunk->start_addr);
220 break;
221 }
Jean-Christophe PLAGNIOL-VILLARD3c8f3702011-05-24 17:13:34 -0700222 }
Huang Ying7f184272011-07-13 13:14:24 +0800223 rcu_read_unlock();
Jean-Christophe PLAGNIOL-VILLARD3c8f3702011-05-24 17:13:34 -0700224
Huang Ying7f184272011-07-13 13:14:24 +0800225 return paddr;
Jean-Christophe PLAGNIOL-VILLARD3c8f3702011-05-24 17:13:34 -0700226}
227EXPORT_SYMBOL(gen_pool_virt_to_phys);
Dean Nelson929f9722006-06-23 02:03:21 -0700228
Dean Nelsona58cbd72006-10-02 02:17:01 -0700229/**
230 * gen_pool_destroy - destroy a special memory pool
Steve Wise322acc92006-10-02 02:17:00 -0700231 * @pool: pool to destroy
Dean Nelsona58cbd72006-10-02 02:17:01 -0700232 *
233 * Destroy the specified special memory pool. Verifies that there are no
234 * outstanding allocations.
Steve Wise322acc92006-10-02 02:17:00 -0700235 */
236void gen_pool_destroy(struct gen_pool *pool)
237{
238 struct list_head *_chunk, *_next_chunk;
239 struct gen_pool_chunk *chunk;
240 int order = pool->min_alloc_order;
241 int bit, end_bit;
242
Steve Wise322acc92006-10-02 02:17:00 -0700243 list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
Shubhraprakash Dase4614572012-08-14 00:25:43 -0700244 int nbytes;
Steve Wise322acc92006-10-02 02:17:00 -0700245 chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
246 list_del(&chunk->next_chunk);
247
248 end_bit = (chunk->end_addr - chunk->start_addr) >> order;
Shubhraprakash Dase4614572012-08-14 00:25:43 -0700249 nbytes = sizeof(struct gen_pool_chunk) +
Sunil Khatrif66edad2014-02-13 19:52:09 +0530250 BITS_TO_LONGS(end_bit) * sizeof(long);
Steve Wise322acc92006-10-02 02:17:00 -0700251 bit = find_next_bit(chunk->bits, end_bit, 0);
252 BUG_ON(bit < end_bit);
253
Shubhraprakash Dase4614572012-08-14 00:25:43 -0700254 if (nbytes <= PAGE_SIZE)
255 kfree(chunk);
256 else
257 vfree(chunk);
Steve Wise322acc92006-10-02 02:17:00 -0700258 }
259 kfree(pool);
260 return;
261}
262EXPORT_SYMBOL(gen_pool_destroy);
263
Dean Nelsona58cbd72006-10-02 02:17:01 -0700264/**
Steve Mucklef132c6c2012-06-06 18:30:57 -0700265 * gen_pool_alloc_aligned - allocate special memory from the pool
Dean Nelson929f9722006-06-23 02:03:21 -0700266 * @pool: pool to allocate from
267 * @size: number of bytes to allocate from the pool
Steve Mucklef132c6c2012-06-06 18:30:57 -0700268 * @alignment_order: Order the allocated space should be
269 * aligned to (eg. 20 means allocated space
270 * must be aligned to 1MiB).
Dean Nelsona58cbd72006-10-02 02:17:01 -0700271 *
272 * Allocate the requested number of bytes from the specified pool.
Huang Ying7f184272011-07-13 13:14:24 +0800273 * Uses a first-fit algorithm. Can not be used in NMI handler on
274 * architectures without NMI-safe cmpxchg implementation.
Dean Nelson929f9722006-06-23 02:03:21 -0700275 */
Laura Abbottdb1db162013-04-08 14:43:14 -0700276u64 gen_pool_alloc_aligned(struct gen_pool *pool, size_t size,
Steve Mucklef132c6c2012-06-06 18:30:57 -0700277 unsigned alignment_order)
Dean Nelson929f9722006-06-23 02:03:21 -0700278{
Dean Nelson929f9722006-06-23 02:03:21 -0700279 struct gen_pool_chunk *chunk;
Laura Abbottdb1db162013-04-08 14:43:14 -0700280 u64 addr = 0, align_mask = 0;
Dean Nelson929f9722006-06-23 02:03:21 -0700281 int order = pool->min_alloc_order;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700282 int nbits, start_bit = 0, remain;
Huang Ying7f184272011-07-13 13:14:24 +0800283
284#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
285 BUG_ON(in_nmi());
286#endif
Dean Nelson929f9722006-06-23 02:03:21 -0700287
288 if (size == 0)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700289 return 0;
290
Steve Mucklef132c6c2012-06-06 18:30:57 -0700291 if (alignment_order > order)
292 align_mask = (1 << (alignment_order - order)) - 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293
Dean Nelson929f9722006-06-23 02:03:21 -0700294 nbits = (size + (1UL << order) - 1) >> order;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700295
Huang Ying7f184272011-07-13 13:14:24 +0800296 rcu_read_lock();
297 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700298 unsigned long chunk_size;
Huang Ying7f184272011-07-13 13:14:24 +0800299 if (size > atomic_read(&chunk->avail))
300 continue;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700301 chunk_size = (chunk->end_addr - chunk->start_addr) >> order;
Dean Nelson929f9722006-06-23 02:03:21 -0700302
Huang Ying7f184272011-07-13 13:14:24 +0800303retry:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700304 start_bit = bitmap_find_next_zero_area_off(chunk->bits, chunk_size,
Laura Abbotte8e53652012-08-30 17:15:02 -0700305 0, nbits, align_mask,
306 chunk->start_addr >> order);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700307 if (start_bit >= chunk_size)
Akinobu Mita243797f2009-12-15 16:48:31 -0800308 continue;
Huang Ying7f184272011-07-13 13:14:24 +0800309 remain = bitmap_set_ll(chunk->bits, start_bit, nbits);
310 if (remain) {
311 remain = bitmap_clear_ll(chunk->bits, start_bit,
312 nbits - remain);
313 BUG_ON(remain);
314 goto retry;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700315 }
Akinobu Mita243797f2009-12-15 16:48:31 -0800316
Laura Abbottdb1db162013-04-08 14:43:14 -0700317 addr = chunk->start_addr + ((u64)start_bit << order);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700318 size = nbits << pool->min_alloc_order;
Huang Ying7f184272011-07-13 13:14:24 +0800319 atomic_sub(size, &chunk->avail);
320 break;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700321 }
Huang Ying7f184272011-07-13 13:14:24 +0800322 rcu_read_unlock();
323 return addr;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700324}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325EXPORT_SYMBOL(gen_pool_alloc_aligned);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700326
Dean Nelsona58cbd72006-10-02 02:17:01 -0700327/**
328 * gen_pool_free - free allocated special memory back to the pool
Dean Nelson929f9722006-06-23 02:03:21 -0700329 * @pool: pool to free to
330 * @addr: starting address of memory to free back to pool
331 * @size: size in bytes of memory to free
Dean Nelsona58cbd72006-10-02 02:17:01 -0700332 *
Huang Ying7f184272011-07-13 13:14:24 +0800333 * Free previously allocated special memory back to the specified
334 * pool. Can not be used in NMI handler on architectures without
335 * NMI-safe cmpxchg implementation.
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700336 */
Laura Abbottdb1db162013-04-08 14:43:14 -0700337void gen_pool_free(struct gen_pool *pool, u64 addr, size_t size)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700338{
Dean Nelson929f9722006-06-23 02:03:21 -0700339 struct gen_pool_chunk *chunk;
Dean Nelson929f9722006-06-23 02:03:21 -0700340 int order = pool->min_alloc_order;
Huang Ying7f184272011-07-13 13:14:24 +0800341 int start_bit, nbits, remain;
342
343#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
344 BUG_ON(in_nmi());
345#endif
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700346
Dean Nelson929f9722006-06-23 02:03:21 -0700347 nbits = (size + (1UL << order) - 1) >> order;
Huang Ying7f184272011-07-13 13:14:24 +0800348 rcu_read_lock();
349 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
Dean Nelson929f9722006-06-23 02:03:21 -0700350 if (addr >= chunk->start_addr && addr < chunk->end_addr) {
351 BUG_ON(addr + size > chunk->end_addr);
Huang Ying7f184272011-07-13 13:14:24 +0800352 start_bit = (addr - chunk->start_addr) >> order;
353 remain = bitmap_clear_ll(chunk->bits, start_bit, nbits);
354 BUG_ON(remain);
355 size = nbits << order;
356 atomic_add(size, &chunk->avail);
357 rcu_read_unlock();
358 return;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700359 }
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700360 }
Huang Ying7f184272011-07-13 13:14:24 +0800361 rcu_read_unlock();
362 BUG();
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700363}
364EXPORT_SYMBOL(gen_pool_free);
Huang Ying7f184272011-07-13 13:14:24 +0800365
366/**
367 * gen_pool_for_each_chunk - call func for every chunk of generic memory pool
368 * @pool: the generic memory pool
369 * @func: func to call
370 * @data: additional data used by @func
371 *
372 * Call @func for every chunk of generic memory pool. The @func is
373 * called with rcu_read_lock held.
374 */
375void gen_pool_for_each_chunk(struct gen_pool *pool,
376 void (*func)(struct gen_pool *pool, struct gen_pool_chunk *chunk, void *data),
377 void *data)
378{
379 struct gen_pool_chunk *chunk;
380
381 rcu_read_lock();
382 list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk)
383 func(pool, chunk, data);
384 rcu_read_unlock();
385}
386EXPORT_SYMBOL(gen_pool_for_each_chunk);
387
388/**
389 * gen_pool_avail - get available free space of the pool
390 * @pool: pool to get available free space
391 *
392 * Return available free space of the specified pool.
393 */
394size_t gen_pool_avail(struct gen_pool *pool)
395{
396 struct gen_pool_chunk *chunk;
397 size_t avail = 0;
398
399 rcu_read_lock();
400 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
401 avail += atomic_read(&chunk->avail);
402 rcu_read_unlock();
403 return avail;
404}
405EXPORT_SYMBOL_GPL(gen_pool_avail);
406
407/**
408 * gen_pool_size - get size in bytes of memory managed by the pool
409 * @pool: pool to get size
410 *
411 * Return size in bytes of memory managed by the pool.
412 */
413size_t gen_pool_size(struct gen_pool *pool)
414{
415 struct gen_pool_chunk *chunk;
416 size_t size = 0;
417
418 rcu_read_lock();
419 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
420 size += chunk->end_addr - chunk->start_addr;
421 rcu_read_unlock();
422 return size;
423}
424EXPORT_SYMBOL_GPL(gen_pool_size);