Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 1 | /* |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 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. |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 24 | * |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 32 | #include <linux/export.h> |
Akinobu Mita | 243797f | 2009-12-15 16:48:31 -0800 | [diff] [blame] | 33 | #include <linux/bitmap.h> |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 34 | #include <linux/rculist.h> |
| 35 | #include <linux/interrupt.h> |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 36 | #include <linux/genalloc.h> |
Shubhraprakash Das | e461457 | 2012-08-14 00:25:43 -0700 | [diff] [blame] | 37 | #include <linux/vmalloc.h> |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 38 | |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 39 | static 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 | |
| 54 | static 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 | */ |
| 80 | static 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 | */ |
| 115 | static 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 Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 138 | |
Dean Nelson | a58cbd7 | 2006-10-02 02:17:01 -0700 | [diff] [blame] | 139 | /** |
| 140 | * gen_pool_create - create a new special memory pool |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 141 | * @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 Nelson | a58cbd7 | 2006-10-02 02:17:01 -0700 | [diff] [blame] | 143 | * |
| 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 Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 146 | */ |
| 147 | struct gen_pool *gen_pool_create(int min_alloc_order, int nid) |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 148 | { |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 149 | struct gen_pool *pool; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 150 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 151 | pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid); |
| 152 | if (pool != NULL) { |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 153 | spin_lock_init(&pool->lock); |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 154 | INIT_LIST_HEAD(&pool->chunks); |
| 155 | pool->min_alloc_order = min_alloc_order; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 156 | } |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 157 | return pool; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 158 | } |
| 159 | EXPORT_SYMBOL(gen_pool_create); |
| 160 | |
Dean Nelson | a58cbd7 | 2006-10-02 02:17:01 -0700 | [diff] [blame] | 161 | /** |
Jean-Christophe PLAGNIOL-VILLARD | 3c8f370 | 2011-05-24 17:13:34 -0700 | [diff] [blame] | 162 | * gen_pool_add_virt - add a new chunk of special memory to the pool |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 163 | * @pool: pool to add new memory chunk to |
Jean-Christophe PLAGNIOL-VILLARD | 3c8f370 | 2011-05-24 17:13:34 -0700 | [diff] [blame] | 164 | * @virt: virtual starting address of memory chunk to add to pool |
| 165 | * @phys: physical starting address of memory chunk to add to pool |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 166 | * @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 Nelson | a58cbd7 | 2006-10-02 02:17:01 -0700 | [diff] [blame] | 169 | * |
| 170 | * Add a new chunk of special memory to the specified pool. |
Jean-Christophe PLAGNIOL-VILLARD | 3c8f370 | 2011-05-24 17:13:34 -0700 | [diff] [blame] | 171 | * |
| 172 | * Returns 0 on success or a -ve errno on failure. |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 173 | */ |
Laura Abbott | db1db16 | 2013-04-08 14:43:14 -0700 | [diff] [blame] | 174 | int gen_pool_add_virt(struct gen_pool *pool, u64 virt, phys_addr_t phys, |
Jean-Christophe PLAGNIOL-VILLARD | 3c8f370 | 2011-05-24 17:13:34 -0700 | [diff] [blame] | 175 | size_t size, int nid) |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 176 | { |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 177 | 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 Cascardo | 1e0cac6 | 2012-10-25 13:37:51 -0700 | [diff] [blame] | 180 | BITS_TO_LONGS(nbits) * sizeof(long); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 181 | |
Shubhraprakash Das | e461457 | 2012-08-14 00:25:43 -0700 | [diff] [blame] | 182 | if (nbytes <= PAGE_SIZE) |
| 183 | chunk = kmalloc_node(nbytes, __GFP_ZERO, nid); |
| 184 | else |
| 185 | chunk = vmalloc(nbytes); |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 186 | if (unlikely(chunk == NULL)) |
Jean-Christophe PLAGNIOL-VILLARD | 3c8f370 | 2011-05-24 17:13:34 -0700 | [diff] [blame] | 187 | return -ENOMEM; |
Shubhraprakash Das | e461457 | 2012-08-14 00:25:43 -0700 | [diff] [blame] | 188 | if (nbytes > PAGE_SIZE) |
| 189 | memset(chunk, 0, nbytes); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 190 | |
Jean-Christophe PLAGNIOL-VILLARD | 3c8f370 | 2011-05-24 17:13:34 -0700 | [diff] [blame] | 191 | chunk->phys_addr = phys; |
| 192 | chunk->start_addr = virt; |
| 193 | chunk->end_addr = virt + size; |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 194 | atomic_set(&chunk->avail, size); |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 195 | |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 196 | spin_lock(&pool->lock); |
| 197 | list_add_rcu(&chunk->next_chunk, &pool->chunks); |
| 198 | spin_unlock(&pool->lock); |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 199 | |
| 200 | return 0; |
| 201 | } |
Jean-Christophe PLAGNIOL-VILLARD | 3c8f370 | 2011-05-24 17:13:34 -0700 | [diff] [blame] | 202 | EXPORT_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 Abbott | db1db16 | 2013-04-08 14:43:14 -0700 | [diff] [blame] | 211 | phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, u64 addr) |
Jean-Christophe PLAGNIOL-VILLARD | 3c8f370 | 2011-05-24 17:13:34 -0700 | [diff] [blame] | 212 | { |
Jean-Christophe PLAGNIOL-VILLARD | 3c8f370 | 2011-05-24 17:13:34 -0700 | [diff] [blame] | 213 | struct gen_pool_chunk *chunk; |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 214 | phys_addr_t paddr = -1; |
Jean-Christophe PLAGNIOL-VILLARD | 3c8f370 | 2011-05-24 17:13:34 -0700 | [diff] [blame] | 215 | |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 216 | 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-VILLARD | 3c8f370 | 2011-05-24 17:13:34 -0700 | [diff] [blame] | 222 | } |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 223 | rcu_read_unlock(); |
Jean-Christophe PLAGNIOL-VILLARD | 3c8f370 | 2011-05-24 17:13:34 -0700 | [diff] [blame] | 224 | |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 225 | return paddr; |
Jean-Christophe PLAGNIOL-VILLARD | 3c8f370 | 2011-05-24 17:13:34 -0700 | [diff] [blame] | 226 | } |
| 227 | EXPORT_SYMBOL(gen_pool_virt_to_phys); |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 228 | |
Dean Nelson | a58cbd7 | 2006-10-02 02:17:01 -0700 | [diff] [blame] | 229 | /** |
| 230 | * gen_pool_destroy - destroy a special memory pool |
Steve Wise | 322acc9 | 2006-10-02 02:17:00 -0700 | [diff] [blame] | 231 | * @pool: pool to destroy |
Dean Nelson | a58cbd7 | 2006-10-02 02:17:01 -0700 | [diff] [blame] | 232 | * |
| 233 | * Destroy the specified special memory pool. Verifies that there are no |
| 234 | * outstanding allocations. |
Steve Wise | 322acc9 | 2006-10-02 02:17:00 -0700 | [diff] [blame] | 235 | */ |
| 236 | void 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 Wise | 322acc9 | 2006-10-02 02:17:00 -0700 | [diff] [blame] | 243 | list_for_each_safe(_chunk, _next_chunk, &pool->chunks) { |
Shubhraprakash Das | e461457 | 2012-08-14 00:25:43 -0700 | [diff] [blame] | 244 | int nbytes; |
Steve Wise | 322acc9 | 2006-10-02 02:17:00 -0700 | [diff] [blame] | 245 | 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 Das | e461457 | 2012-08-14 00:25:43 -0700 | [diff] [blame] | 249 | nbytes = sizeof(struct gen_pool_chunk) + |
Sunil Khatri | f66edad | 2014-02-13 19:52:09 +0530 | [diff] [blame] | 250 | BITS_TO_LONGS(end_bit) * sizeof(long); |
Steve Wise | 322acc9 | 2006-10-02 02:17:00 -0700 | [diff] [blame] | 251 | bit = find_next_bit(chunk->bits, end_bit, 0); |
| 252 | BUG_ON(bit < end_bit); |
| 253 | |
Shubhraprakash Das | e461457 | 2012-08-14 00:25:43 -0700 | [diff] [blame] | 254 | if (nbytes <= PAGE_SIZE) |
| 255 | kfree(chunk); |
| 256 | else |
| 257 | vfree(chunk); |
Steve Wise | 322acc9 | 2006-10-02 02:17:00 -0700 | [diff] [blame] | 258 | } |
| 259 | kfree(pool); |
| 260 | return; |
| 261 | } |
| 262 | EXPORT_SYMBOL(gen_pool_destroy); |
| 263 | |
Dean Nelson | a58cbd7 | 2006-10-02 02:17:01 -0700 | [diff] [blame] | 264 | /** |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 265 | * gen_pool_alloc_aligned - allocate special memory from the pool |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 266 | * @pool: pool to allocate from |
| 267 | * @size: number of bytes to allocate from the pool |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 268 | * @alignment_order: Order the allocated space should be |
| 269 | * aligned to (eg. 20 means allocated space |
| 270 | * must be aligned to 1MiB). |
Dean Nelson | a58cbd7 | 2006-10-02 02:17:01 -0700 | [diff] [blame] | 271 | * |
| 272 | * Allocate the requested number of bytes from the specified pool. |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 273 | * Uses a first-fit algorithm. Can not be used in NMI handler on |
| 274 | * architectures without NMI-safe cmpxchg implementation. |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 275 | */ |
Laura Abbott | db1db16 | 2013-04-08 14:43:14 -0700 | [diff] [blame] | 276 | u64 gen_pool_alloc_aligned(struct gen_pool *pool, size_t size, |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 277 | unsigned alignment_order) |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 278 | { |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 279 | struct gen_pool_chunk *chunk; |
Laura Abbott | db1db16 | 2013-04-08 14:43:14 -0700 | [diff] [blame] | 280 | u64 addr = 0, align_mask = 0; |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 281 | int order = pool->min_alloc_order; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 282 | int nbits, start_bit = 0, remain; |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 283 | |
| 284 | #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG |
| 285 | BUG_ON(in_nmi()); |
| 286 | #endif |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 287 | |
| 288 | if (size == 0) |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 289 | return 0; |
| 290 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 291 | if (alignment_order > order) |
| 292 | align_mask = (1 << (alignment_order - order)) - 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 293 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 294 | nbits = (size + (1UL << order) - 1) >> order; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 295 | |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 296 | rcu_read_lock(); |
| 297 | list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 298 | unsigned long chunk_size; |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 299 | if (size > atomic_read(&chunk->avail)) |
| 300 | continue; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 301 | chunk_size = (chunk->end_addr - chunk->start_addr) >> order; |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 302 | |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 303 | retry: |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 304 | start_bit = bitmap_find_next_zero_area_off(chunk->bits, chunk_size, |
Laura Abbott | e8e5365 | 2012-08-30 17:15:02 -0700 | [diff] [blame] | 305 | 0, nbits, align_mask, |
| 306 | chunk->start_addr >> order); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 307 | if (start_bit >= chunk_size) |
Akinobu Mita | 243797f | 2009-12-15 16:48:31 -0800 | [diff] [blame] | 308 | continue; |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 309 | 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 Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 315 | } |
Akinobu Mita | 243797f | 2009-12-15 16:48:31 -0800 | [diff] [blame] | 316 | |
Laura Abbott | db1db16 | 2013-04-08 14:43:14 -0700 | [diff] [blame] | 317 | addr = chunk->start_addr + ((u64)start_bit << order); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 318 | size = nbits << pool->min_alloc_order; |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 319 | atomic_sub(size, &chunk->avail); |
| 320 | break; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 321 | } |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 322 | rcu_read_unlock(); |
| 323 | return addr; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 324 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 325 | EXPORT_SYMBOL(gen_pool_alloc_aligned); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 326 | |
Dean Nelson | a58cbd7 | 2006-10-02 02:17:01 -0700 | [diff] [blame] | 327 | /** |
| 328 | * gen_pool_free - free allocated special memory back to the pool |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 329 | * @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 Nelson | a58cbd7 | 2006-10-02 02:17:01 -0700 | [diff] [blame] | 332 | * |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 333 | * 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 Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 336 | */ |
Laura Abbott | db1db16 | 2013-04-08 14:43:14 -0700 | [diff] [blame] | 337 | void gen_pool_free(struct gen_pool *pool, u64 addr, size_t size) |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 338 | { |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 339 | struct gen_pool_chunk *chunk; |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 340 | int order = pool->min_alloc_order; |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 341 | int start_bit, nbits, remain; |
| 342 | |
| 343 | #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG |
| 344 | BUG_ON(in_nmi()); |
| 345 | #endif |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 346 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 347 | nbits = (size + (1UL << order) - 1) >> order; |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 348 | rcu_read_lock(); |
| 349 | list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) { |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame] | 350 | if (addr >= chunk->start_addr && addr < chunk->end_addr) { |
| 351 | BUG_ON(addr + size > chunk->end_addr); |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 352 | 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 Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 359 | } |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 360 | } |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 361 | rcu_read_unlock(); |
| 362 | BUG(); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 363 | } |
| 364 | EXPORT_SYMBOL(gen_pool_free); |
Huang Ying | 7f18427 | 2011-07-13 13:14:24 +0800 | [diff] [blame] | 365 | |
| 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 | */ |
| 375 | void 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 | } |
| 386 | EXPORT_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 | */ |
| 394 | size_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 | } |
| 405 | EXPORT_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 | */ |
| 413 | size_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 | } |
| 424 | EXPORT_SYMBOL_GPL(gen_pool_size); |