Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/percpu.c - percpu memory allocator |
| 3 | * |
| 4 | * Copyright (C) 2009 SUSE Linux Products GmbH |
| 5 | * Copyright (C) 2009 Tejun Heo <tj@kernel.org> |
| 6 | * |
| 7 | * This file is released under the GPLv2. |
| 8 | * |
| 9 | * This is percpu allocator which can handle both static and dynamic |
| 10 | * areas. Percpu areas are allocated in chunks in vmalloc area. Each |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 11 | * chunk is consisted of boot-time determined number of units and the |
| 12 | * first chunk is used for static percpu variables in the kernel image |
| 13 | * (special boot time alloc/init handling necessary as these areas |
| 14 | * need to be brought up before allocation services are running). |
| 15 | * Unit grows as necessary and all units grow or shrink in unison. |
| 16 | * When a chunk is filled up, another chunk is allocated. ie. in |
| 17 | * vmalloc area |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 18 | * |
| 19 | * c0 c1 c2 |
| 20 | * ------------------- ------------------- ------------ |
| 21 | * | u0 | u1 | u2 | u3 | | u0 | u1 | u2 | u3 | | u0 | u1 | u |
| 22 | * ------------------- ...... ------------------- .... ------------ |
| 23 | * |
| 24 | * Allocation is done in offset-size areas of single unit space. Ie, |
| 25 | * an area of 512 bytes at 6k in c1 occupies 512 bytes at 6k of c1:u0, |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 26 | * c1:u1, c1:u2 and c1:u3. On UMA, units corresponds directly to |
| 27 | * cpus. On NUMA, the mapping can be non-linear and even sparse. |
| 28 | * Percpu access can be done by configuring percpu base registers |
| 29 | * according to cpu to unit mapping and pcpu_unit_size. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 30 | * |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 31 | * There are usually many small percpu allocations many of them being |
| 32 | * as small as 4 bytes. The allocator organizes chunks into lists |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 33 | * according to free size and tries to allocate from the fullest one. |
| 34 | * Each chunk keeps the maximum contiguous area size hint which is |
| 35 | * guaranteed to be eqaul to or larger than the maximum contiguous |
| 36 | * area in the chunk. This helps the allocator not to iterate the |
| 37 | * chunk maps unnecessarily. |
| 38 | * |
| 39 | * Allocation state in each chunk is kept using an array of integers |
| 40 | * on chunk->map. A positive value in the map represents a free |
| 41 | * region and negative allocated. Allocation inside a chunk is done |
| 42 | * by scanning this map sequentially and serving the first matching |
| 43 | * entry. This is mostly copied from the percpu_modalloc() allocator. |
Christoph Lameter | e1b9aa3 | 2009-04-02 13:21:44 +0900 | [diff] [blame] | 44 | * Chunks can be determined from the address using the index field |
| 45 | * in the page struct. The index field contains a pointer to the chunk. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 46 | * |
| 47 | * To use this allocator, arch code should do the followings. |
| 48 | * |
Tejun Heo | e74e396 | 2009-03-30 19:07:44 +0900 | [diff] [blame] | 49 | * - drop CONFIG_HAVE_LEGACY_PER_CPU_AREA |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 50 | * |
| 51 | * - define __addr_to_pcpu_ptr() and __pcpu_ptr_to_addr() to translate |
Tejun Heo | e010098 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 52 | * regular address to percpu pointer and back if they need to be |
| 53 | * different from the default |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 54 | * |
Tejun Heo | 8d408b4 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 55 | * - use pcpu_setup_first_chunk() during percpu area initialization to |
| 56 | * setup the first chunk containing the kernel static percpu area |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 57 | */ |
| 58 | |
| 59 | #include <linux/bitmap.h> |
| 60 | #include <linux/bootmem.h> |
| 61 | #include <linux/list.h> |
| 62 | #include <linux/mm.h> |
| 63 | #include <linux/module.h> |
| 64 | #include <linux/mutex.h> |
| 65 | #include <linux/percpu.h> |
| 66 | #include <linux/pfn.h> |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 67 | #include <linux/slab.h> |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 68 | #include <linux/spinlock.h> |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 69 | #include <linux/vmalloc.h> |
Tejun Heo | a56dbdd | 2009-03-07 00:44:11 +0900 | [diff] [blame] | 70 | #include <linux/workqueue.h> |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 71 | |
| 72 | #include <asm/cacheflush.h> |
Tejun Heo | e010098 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 73 | #include <asm/sections.h> |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 74 | #include <asm/tlbflush.h> |
| 75 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 76 | #define PCPU_SLOT_BASE_SHIFT 5 /* 1-31 shares the same slot */ |
| 77 | #define PCPU_DFL_MAP_ALLOC 16 /* start a map with 16 ents */ |
| 78 | |
Tejun Heo | e010098 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 79 | /* default addr <-> pcpu_ptr mapping, override in asm/percpu.h if necessary */ |
| 80 | #ifndef __addr_to_pcpu_ptr |
| 81 | #define __addr_to_pcpu_ptr(addr) \ |
| 82 | (void *)((unsigned long)(addr) - (unsigned long)pcpu_base_addr \ |
| 83 | + (unsigned long)__per_cpu_start) |
| 84 | #endif |
| 85 | #ifndef __pcpu_ptr_to_addr |
| 86 | #define __pcpu_ptr_to_addr(ptr) \ |
| 87 | (void *)((unsigned long)(ptr) + (unsigned long)pcpu_base_addr \ |
| 88 | - (unsigned long)__per_cpu_start) |
| 89 | #endif |
| 90 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 91 | struct pcpu_chunk { |
| 92 | struct list_head list; /* linked to pcpu_slot lists */ |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 93 | int free_size; /* free bytes in the chunk */ |
| 94 | int contig_hint; /* max contiguous size hint */ |
| 95 | struct vm_struct *vm; /* mapped vmalloc region */ |
| 96 | int map_used; /* # of map entries used */ |
| 97 | int map_alloc; /* # of map entries allocated */ |
| 98 | int *map; /* allocation map */ |
Tejun Heo | 8d408b4 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 99 | bool immutable; /* no [de]population allowed */ |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 100 | unsigned long populated[]; /* populated bitmap */ |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 101 | }; |
| 102 | |
Tejun Heo | 40150d3 | 2009-02-24 12:32:28 +0900 | [diff] [blame] | 103 | static int pcpu_unit_pages __read_mostly; |
| 104 | static int pcpu_unit_size __read_mostly; |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 105 | static int pcpu_nr_units __read_mostly; |
Tejun Heo | 40150d3 | 2009-02-24 12:32:28 +0900 | [diff] [blame] | 106 | static int pcpu_chunk_size __read_mostly; |
| 107 | static int pcpu_nr_slots __read_mostly; |
| 108 | static size_t pcpu_chunk_struct_size __read_mostly; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 109 | |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 110 | /* cpus with the lowest and highest unit numbers */ |
| 111 | static unsigned int pcpu_first_unit_cpu __read_mostly; |
| 112 | static unsigned int pcpu_last_unit_cpu __read_mostly; |
| 113 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 114 | /* the address of the first chunk which starts with the kernel static area */ |
Tejun Heo | 40150d3 | 2009-02-24 12:32:28 +0900 | [diff] [blame] | 115 | void *pcpu_base_addr __read_mostly; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 116 | EXPORT_SYMBOL_GPL(pcpu_base_addr); |
| 117 | |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 118 | /* cpu -> unit map */ |
| 119 | const int *pcpu_unit_map __read_mostly; |
| 120 | |
Tejun Heo | ae9e6bc9 | 2009-04-02 13:19:54 +0900 | [diff] [blame] | 121 | /* |
| 122 | * The first chunk which always exists. Note that unlike other |
| 123 | * chunks, this one can be allocated and mapped in several different |
| 124 | * ways and thus often doesn't live in the vmalloc area. |
| 125 | */ |
| 126 | static struct pcpu_chunk *pcpu_first_chunk; |
| 127 | |
| 128 | /* |
| 129 | * Optional reserved chunk. This chunk reserves part of the first |
| 130 | * chunk and serves it for reserved allocations. The amount of |
| 131 | * reserved offset is in pcpu_reserved_chunk_limit. When reserved |
| 132 | * area doesn't exist, the following variables contain NULL and 0 |
| 133 | * respectively. |
| 134 | */ |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 135 | static struct pcpu_chunk *pcpu_reserved_chunk; |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 136 | static int pcpu_reserved_chunk_limit; |
| 137 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 138 | /* |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 139 | * Synchronization rules. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 140 | * |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 141 | * There are two locks - pcpu_alloc_mutex and pcpu_lock. The former |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 142 | * protects allocation/reclaim paths, chunks, populated bitmap and |
| 143 | * vmalloc mapping. The latter is a spinlock and protects the index |
| 144 | * data structures - chunk slots, chunks and area maps in chunks. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 145 | * |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 146 | * During allocation, pcpu_alloc_mutex is kept locked all the time and |
| 147 | * pcpu_lock is grabbed and released as necessary. All actual memory |
| 148 | * allocations are done using GFP_KERNEL with pcpu_lock released. |
| 149 | * |
| 150 | * Free path accesses and alters only the index data structures, so it |
| 151 | * can be safely called from atomic context. When memory needs to be |
| 152 | * returned to the system, free path schedules reclaim_work which |
| 153 | * grabs both pcpu_alloc_mutex and pcpu_lock, unlinks chunks to be |
| 154 | * reclaimed, release both locks and frees the chunks. Note that it's |
| 155 | * necessary to grab both locks to remove a chunk from circulation as |
| 156 | * allocation path might be referencing the chunk with only |
| 157 | * pcpu_alloc_mutex locked. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 158 | */ |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 159 | static DEFINE_MUTEX(pcpu_alloc_mutex); /* protects whole alloc and reclaim */ |
| 160 | static DEFINE_SPINLOCK(pcpu_lock); /* protects index data structures */ |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 161 | |
Tejun Heo | 40150d3 | 2009-02-24 12:32:28 +0900 | [diff] [blame] | 162 | static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */ |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 163 | |
Tejun Heo | a56dbdd | 2009-03-07 00:44:11 +0900 | [diff] [blame] | 164 | /* reclaim work to release fully free chunks, scheduled from free path */ |
| 165 | static void pcpu_reclaim(struct work_struct *work); |
| 166 | static DECLARE_WORK(pcpu_reclaim_work, pcpu_reclaim); |
| 167 | |
Tejun Heo | d9b55ee | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 168 | static int __pcpu_size_to_slot(int size) |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 169 | { |
Tejun Heo | cae3aeb | 2009-02-21 16:56:23 +0900 | [diff] [blame] | 170 | int highbit = fls(size); /* size is in bytes */ |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 171 | return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1); |
| 172 | } |
| 173 | |
Tejun Heo | d9b55ee | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 174 | static int pcpu_size_to_slot(int size) |
| 175 | { |
| 176 | if (size == pcpu_unit_size) |
| 177 | return pcpu_nr_slots - 1; |
| 178 | return __pcpu_size_to_slot(size); |
| 179 | } |
| 180 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 181 | static int pcpu_chunk_slot(const struct pcpu_chunk *chunk) |
| 182 | { |
| 183 | if (chunk->free_size < sizeof(int) || chunk->contig_hint < sizeof(int)) |
| 184 | return 0; |
| 185 | |
| 186 | return pcpu_size_to_slot(chunk->free_size); |
| 187 | } |
| 188 | |
| 189 | static int pcpu_page_idx(unsigned int cpu, int page_idx) |
| 190 | { |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 191 | return pcpu_unit_map[cpu] * pcpu_unit_pages + page_idx; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 192 | } |
| 193 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 194 | static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk, |
| 195 | unsigned int cpu, int page_idx) |
| 196 | { |
| 197 | return (unsigned long)chunk->vm->addr + |
| 198 | (pcpu_page_idx(cpu, page_idx) << PAGE_SHIFT); |
| 199 | } |
| 200 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 201 | static struct page *pcpu_chunk_page(struct pcpu_chunk *chunk, |
| 202 | unsigned int cpu, int page_idx) |
Tejun Heo | c8a51be | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 203 | { |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 204 | /* must not be used on pre-mapped chunk */ |
| 205 | WARN_ON(chunk->immutable); |
Tejun Heo | c8a51be | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 206 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 207 | return vmalloc_to_page((void *)pcpu_chunk_addr(chunk, cpu, page_idx)); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 208 | } |
| 209 | |
Christoph Lameter | e1b9aa3 | 2009-04-02 13:21:44 +0900 | [diff] [blame] | 210 | /* set the pointer to a chunk in a page struct */ |
| 211 | static void pcpu_set_page_chunk(struct page *page, struct pcpu_chunk *pcpu) |
| 212 | { |
| 213 | page->index = (unsigned long)pcpu; |
| 214 | } |
| 215 | |
| 216 | /* obtain pointer to a chunk from a page struct */ |
| 217 | static struct pcpu_chunk *pcpu_get_page_chunk(struct page *page) |
| 218 | { |
| 219 | return (struct pcpu_chunk *)page->index; |
| 220 | } |
| 221 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 222 | static void pcpu_next_unpop(struct pcpu_chunk *chunk, int *rs, int *re, int end) |
| 223 | { |
| 224 | *rs = find_next_zero_bit(chunk->populated, end, *rs); |
| 225 | *re = find_next_bit(chunk->populated, end, *rs + 1); |
| 226 | } |
| 227 | |
| 228 | static void pcpu_next_pop(struct pcpu_chunk *chunk, int *rs, int *re, int end) |
| 229 | { |
| 230 | *rs = find_next_bit(chunk->populated, end, *rs); |
| 231 | *re = find_next_zero_bit(chunk->populated, end, *rs + 1); |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * (Un)populated page region iterators. Iterate over (un)populated |
| 236 | * page regions betwen @start and @end in @chunk. @rs and @re should |
| 237 | * be integer variables and will be set to start and end page index of |
| 238 | * the current region. |
| 239 | */ |
| 240 | #define pcpu_for_each_unpop_region(chunk, rs, re, start, end) \ |
| 241 | for ((rs) = (start), pcpu_next_unpop((chunk), &(rs), &(re), (end)); \ |
| 242 | (rs) < (re); \ |
| 243 | (rs) = (re) + 1, pcpu_next_unpop((chunk), &(rs), &(re), (end))) |
| 244 | |
| 245 | #define pcpu_for_each_pop_region(chunk, rs, re, start, end) \ |
| 246 | for ((rs) = (start), pcpu_next_pop((chunk), &(rs), &(re), (end)); \ |
| 247 | (rs) < (re); \ |
| 248 | (rs) = (re) + 1, pcpu_next_pop((chunk), &(rs), &(re), (end))) |
| 249 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 250 | /** |
Tejun Heo | 1880d93 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 251 | * pcpu_mem_alloc - allocate memory |
| 252 | * @size: bytes to allocate |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 253 | * |
Tejun Heo | 1880d93 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 254 | * Allocate @size bytes. If @size is smaller than PAGE_SIZE, |
| 255 | * kzalloc() is used; otherwise, vmalloc() is used. The returned |
| 256 | * memory is always zeroed. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 257 | * |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 258 | * CONTEXT: |
| 259 | * Does GFP_KERNEL allocation. |
| 260 | * |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 261 | * RETURNS: |
Tejun Heo | 1880d93 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 262 | * Pointer to the allocated area on success, NULL on failure. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 263 | */ |
Tejun Heo | 1880d93 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 264 | static void *pcpu_mem_alloc(size_t size) |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 265 | { |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 266 | if (size <= PAGE_SIZE) |
Tejun Heo | 1880d93 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 267 | return kzalloc(size, GFP_KERNEL); |
| 268 | else { |
| 269 | void *ptr = vmalloc(size); |
| 270 | if (ptr) |
| 271 | memset(ptr, 0, size); |
| 272 | return ptr; |
| 273 | } |
| 274 | } |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 275 | |
Tejun Heo | 1880d93 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 276 | /** |
| 277 | * pcpu_mem_free - free memory |
| 278 | * @ptr: memory to free |
| 279 | * @size: size of the area |
| 280 | * |
| 281 | * Free @ptr. @ptr should have been allocated using pcpu_mem_alloc(). |
| 282 | */ |
| 283 | static void pcpu_mem_free(void *ptr, size_t size) |
| 284 | { |
| 285 | if (size <= PAGE_SIZE) |
| 286 | kfree(ptr); |
| 287 | else |
| 288 | vfree(ptr); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /** |
| 292 | * pcpu_chunk_relocate - put chunk in the appropriate chunk slot |
| 293 | * @chunk: chunk of interest |
| 294 | * @oslot: the previous slot it was on |
| 295 | * |
| 296 | * This function is called after an allocation or free changed @chunk. |
| 297 | * New slot according to the changed state is determined and @chunk is |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 298 | * moved to the slot. Note that the reserved chunk is never put on |
| 299 | * chunk slots. |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 300 | * |
| 301 | * CONTEXT: |
| 302 | * pcpu_lock. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 303 | */ |
| 304 | static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot) |
| 305 | { |
| 306 | int nslot = pcpu_chunk_slot(chunk); |
| 307 | |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 308 | if (chunk != pcpu_reserved_chunk && oslot != nslot) { |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 309 | if (oslot < nslot) |
| 310 | list_move(&chunk->list, &pcpu_slot[nslot]); |
| 311 | else |
| 312 | list_move_tail(&chunk->list, &pcpu_slot[nslot]); |
| 313 | } |
| 314 | } |
| 315 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 316 | /** |
Christoph Lameter | e1b9aa3 | 2009-04-02 13:21:44 +0900 | [diff] [blame] | 317 | * pcpu_chunk_addr_search - determine chunk containing specified address |
| 318 | * @addr: address for which the chunk needs to be determined. |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 319 | * |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 320 | * RETURNS: |
| 321 | * The address of the found chunk. |
| 322 | */ |
| 323 | static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr) |
| 324 | { |
Tejun Heo | ae9e6bc9 | 2009-04-02 13:19:54 +0900 | [diff] [blame] | 325 | void *first_start = pcpu_first_chunk->vm->addr; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 326 | |
Tejun Heo | ae9e6bc9 | 2009-04-02 13:19:54 +0900 | [diff] [blame] | 327 | /* is it in the first chunk? */ |
Tejun Heo | 79ba6ac | 2009-07-04 08:10:58 +0900 | [diff] [blame] | 328 | if (addr >= first_start && addr < first_start + pcpu_unit_size) { |
Tejun Heo | ae9e6bc9 | 2009-04-02 13:19:54 +0900 | [diff] [blame] | 329 | /* is it in the reserved area? */ |
| 330 | if (addr < first_start + pcpu_reserved_chunk_limit) |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 331 | return pcpu_reserved_chunk; |
Tejun Heo | ae9e6bc9 | 2009-04-02 13:19:54 +0900 | [diff] [blame] | 332 | return pcpu_first_chunk; |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 333 | } |
| 334 | |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 335 | /* |
| 336 | * The address is relative to unit0 which might be unused and |
| 337 | * thus unmapped. Offset the address to the unit space of the |
| 338 | * current processor before looking it up in the vmalloc |
| 339 | * space. Note that any possible cpu id can be used here, so |
| 340 | * there's no need to worry about preemption or cpu hotplug. |
| 341 | */ |
| 342 | addr += pcpu_unit_map[smp_processor_id()] * pcpu_unit_size; |
Christoph Lameter | e1b9aa3 | 2009-04-02 13:21:44 +0900 | [diff] [blame] | 343 | return pcpu_get_page_chunk(vmalloc_to_page(addr)); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /** |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 347 | * pcpu_extend_area_map - extend area map for allocation |
| 348 | * @chunk: target chunk |
| 349 | * |
| 350 | * Extend area map of @chunk so that it can accomodate an allocation. |
| 351 | * A single allocation can split an area into three areas, so this |
| 352 | * function makes sure that @chunk->map has at least two extra slots. |
| 353 | * |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 354 | * CONTEXT: |
| 355 | * pcpu_alloc_mutex, pcpu_lock. pcpu_lock is released and reacquired |
| 356 | * if area map is extended. |
| 357 | * |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 358 | * RETURNS: |
| 359 | * 0 if noop, 1 if successfully extended, -errno on failure. |
| 360 | */ |
| 361 | static int pcpu_extend_area_map(struct pcpu_chunk *chunk) |
| 362 | { |
| 363 | int new_alloc; |
| 364 | int *new; |
| 365 | size_t size; |
| 366 | |
| 367 | /* has enough? */ |
| 368 | if (chunk->map_alloc >= chunk->map_used + 2) |
| 369 | return 0; |
| 370 | |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 371 | spin_unlock_irq(&pcpu_lock); |
| 372 | |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 373 | new_alloc = PCPU_DFL_MAP_ALLOC; |
| 374 | while (new_alloc < chunk->map_used + 2) |
| 375 | new_alloc *= 2; |
| 376 | |
| 377 | new = pcpu_mem_alloc(new_alloc * sizeof(new[0])); |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 378 | if (!new) { |
| 379 | spin_lock_irq(&pcpu_lock); |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 380 | return -ENOMEM; |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Acquire pcpu_lock and switch to new area map. Only free |
| 385 | * could have happened inbetween, so map_used couldn't have |
| 386 | * grown. |
| 387 | */ |
| 388 | spin_lock_irq(&pcpu_lock); |
| 389 | BUG_ON(new_alloc < chunk->map_used + 2); |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 390 | |
| 391 | size = chunk->map_alloc * sizeof(chunk->map[0]); |
| 392 | memcpy(new, chunk->map, size); |
| 393 | |
| 394 | /* |
| 395 | * map_alloc < PCPU_DFL_MAP_ALLOC indicates that the chunk is |
| 396 | * one of the first chunks and still using static map. |
| 397 | */ |
| 398 | if (chunk->map_alloc >= PCPU_DFL_MAP_ALLOC) |
| 399 | pcpu_mem_free(chunk->map, size); |
| 400 | |
| 401 | chunk->map_alloc = new_alloc; |
| 402 | chunk->map = new; |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | /** |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 407 | * pcpu_split_block - split a map block |
| 408 | * @chunk: chunk of interest |
| 409 | * @i: index of map block to split |
Tejun Heo | cae3aeb | 2009-02-21 16:56:23 +0900 | [diff] [blame] | 410 | * @head: head size in bytes (can be 0) |
| 411 | * @tail: tail size in bytes (can be 0) |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 412 | * |
| 413 | * Split the @i'th map block into two or three blocks. If @head is |
| 414 | * non-zero, @head bytes block is inserted before block @i moving it |
| 415 | * to @i+1 and reducing its size by @head bytes. |
| 416 | * |
| 417 | * If @tail is non-zero, the target block, which can be @i or @i+1 |
| 418 | * depending on @head, is reduced by @tail bytes and @tail byte block |
| 419 | * is inserted after the target block. |
| 420 | * |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 421 | * @chunk->map must have enough free slots to accomodate the split. |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 422 | * |
| 423 | * CONTEXT: |
| 424 | * pcpu_lock. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 425 | */ |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 426 | static void pcpu_split_block(struct pcpu_chunk *chunk, int i, |
| 427 | int head, int tail) |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 428 | { |
| 429 | int nr_extra = !!head + !!tail; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 430 | |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 431 | BUG_ON(chunk->map_alloc < chunk->map_used + nr_extra); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 432 | |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 433 | /* insert new subblocks */ |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 434 | memmove(&chunk->map[i + nr_extra], &chunk->map[i], |
| 435 | sizeof(chunk->map[0]) * (chunk->map_used - i)); |
| 436 | chunk->map_used += nr_extra; |
| 437 | |
| 438 | if (head) { |
| 439 | chunk->map[i + 1] = chunk->map[i] - head; |
| 440 | chunk->map[i++] = head; |
| 441 | } |
| 442 | if (tail) { |
| 443 | chunk->map[i++] -= tail; |
| 444 | chunk->map[i] = tail; |
| 445 | } |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /** |
| 449 | * pcpu_alloc_area - allocate area from a pcpu_chunk |
| 450 | * @chunk: chunk of interest |
Tejun Heo | cae3aeb | 2009-02-21 16:56:23 +0900 | [diff] [blame] | 451 | * @size: wanted size in bytes |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 452 | * @align: wanted align |
| 453 | * |
| 454 | * Try to allocate @size bytes area aligned at @align from @chunk. |
| 455 | * Note that this function only allocates the offset. It doesn't |
| 456 | * populate or map the area. |
| 457 | * |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 458 | * @chunk->map must have at least two free slots. |
| 459 | * |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 460 | * CONTEXT: |
| 461 | * pcpu_lock. |
| 462 | * |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 463 | * RETURNS: |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 464 | * Allocated offset in @chunk on success, -1 if no matching area is |
| 465 | * found. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 466 | */ |
| 467 | static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align) |
| 468 | { |
| 469 | int oslot = pcpu_chunk_slot(chunk); |
| 470 | int max_contig = 0; |
| 471 | int i, off; |
| 472 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 473 | for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) { |
| 474 | bool is_last = i + 1 == chunk->map_used; |
| 475 | int head, tail; |
| 476 | |
| 477 | /* extra for alignment requirement */ |
| 478 | head = ALIGN(off, align) - off; |
| 479 | BUG_ON(i == 0 && head != 0); |
| 480 | |
| 481 | if (chunk->map[i] < 0) |
| 482 | continue; |
| 483 | if (chunk->map[i] < head + size) { |
| 484 | max_contig = max(chunk->map[i], max_contig); |
| 485 | continue; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * If head is small or the previous block is free, |
| 490 | * merge'em. Note that 'small' is defined as smaller |
| 491 | * than sizeof(int), which is very small but isn't too |
| 492 | * uncommon for percpu allocations. |
| 493 | */ |
| 494 | if (head && (head < sizeof(int) || chunk->map[i - 1] > 0)) { |
| 495 | if (chunk->map[i - 1] > 0) |
| 496 | chunk->map[i - 1] += head; |
| 497 | else { |
| 498 | chunk->map[i - 1] -= head; |
| 499 | chunk->free_size -= head; |
| 500 | } |
| 501 | chunk->map[i] -= head; |
| 502 | off += head; |
| 503 | head = 0; |
| 504 | } |
| 505 | |
| 506 | /* if tail is small, just keep it around */ |
| 507 | tail = chunk->map[i] - head - size; |
| 508 | if (tail < sizeof(int)) |
| 509 | tail = 0; |
| 510 | |
| 511 | /* split if warranted */ |
| 512 | if (head || tail) { |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 513 | pcpu_split_block(chunk, i, head, tail); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 514 | if (head) { |
| 515 | i++; |
| 516 | off += head; |
| 517 | max_contig = max(chunk->map[i - 1], max_contig); |
| 518 | } |
| 519 | if (tail) |
| 520 | max_contig = max(chunk->map[i + 1], max_contig); |
| 521 | } |
| 522 | |
| 523 | /* update hint and mark allocated */ |
| 524 | if (is_last) |
| 525 | chunk->contig_hint = max_contig; /* fully scanned */ |
| 526 | else |
| 527 | chunk->contig_hint = max(chunk->contig_hint, |
| 528 | max_contig); |
| 529 | |
| 530 | chunk->free_size -= chunk->map[i]; |
| 531 | chunk->map[i] = -chunk->map[i]; |
| 532 | |
| 533 | pcpu_chunk_relocate(chunk, oslot); |
| 534 | return off; |
| 535 | } |
| 536 | |
| 537 | chunk->contig_hint = max_contig; /* fully scanned */ |
| 538 | pcpu_chunk_relocate(chunk, oslot); |
| 539 | |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 540 | /* tell the upper layer that this chunk has no matching area */ |
| 541 | return -1; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
| 545 | * pcpu_free_area - free area to a pcpu_chunk |
| 546 | * @chunk: chunk of interest |
| 547 | * @freeme: offset of area to free |
| 548 | * |
| 549 | * Free area starting from @freeme to @chunk. Note that this function |
| 550 | * only modifies the allocation map. It doesn't depopulate or unmap |
| 551 | * the area. |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 552 | * |
| 553 | * CONTEXT: |
| 554 | * pcpu_lock. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 555 | */ |
| 556 | static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme) |
| 557 | { |
| 558 | int oslot = pcpu_chunk_slot(chunk); |
| 559 | int i, off; |
| 560 | |
| 561 | for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) |
| 562 | if (off == freeme) |
| 563 | break; |
| 564 | BUG_ON(off != freeme); |
| 565 | BUG_ON(chunk->map[i] > 0); |
| 566 | |
| 567 | chunk->map[i] = -chunk->map[i]; |
| 568 | chunk->free_size += chunk->map[i]; |
| 569 | |
| 570 | /* merge with previous? */ |
| 571 | if (i > 0 && chunk->map[i - 1] >= 0) { |
| 572 | chunk->map[i - 1] += chunk->map[i]; |
| 573 | chunk->map_used--; |
| 574 | memmove(&chunk->map[i], &chunk->map[i + 1], |
| 575 | (chunk->map_used - i) * sizeof(chunk->map[0])); |
| 576 | i--; |
| 577 | } |
| 578 | /* merge with next? */ |
| 579 | if (i + 1 < chunk->map_used && chunk->map[i + 1] >= 0) { |
| 580 | chunk->map[i] += chunk->map[i + 1]; |
| 581 | chunk->map_used--; |
| 582 | memmove(&chunk->map[i + 1], &chunk->map[i + 2], |
| 583 | (chunk->map_used - (i + 1)) * sizeof(chunk->map[0])); |
| 584 | } |
| 585 | |
| 586 | chunk->contig_hint = max(chunk->map[i], chunk->contig_hint); |
| 587 | pcpu_chunk_relocate(chunk, oslot); |
| 588 | } |
| 589 | |
| 590 | /** |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 591 | * pcpu_get_pages_and_bitmap - get temp pages array and bitmap |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 592 | * @chunk: chunk of interest |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 593 | * @bitmapp: output parameter for bitmap |
| 594 | * @may_alloc: may allocate the array |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 595 | * |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 596 | * Returns pointer to array of pointers to struct page and bitmap, |
| 597 | * both of which can be indexed with pcpu_page_idx(). The returned |
| 598 | * array is cleared to zero and *@bitmapp is copied from |
| 599 | * @chunk->populated. Note that there is only one array and bitmap |
| 600 | * and access exclusion is the caller's responsibility. |
| 601 | * |
| 602 | * CONTEXT: |
| 603 | * pcpu_alloc_mutex and does GFP_KERNEL allocation if @may_alloc. |
| 604 | * Otherwise, don't care. |
| 605 | * |
| 606 | * RETURNS: |
| 607 | * Pointer to temp pages array on success, NULL on failure. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 608 | */ |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 609 | static struct page **pcpu_get_pages_and_bitmap(struct pcpu_chunk *chunk, |
| 610 | unsigned long **bitmapp, |
| 611 | bool may_alloc) |
| 612 | { |
| 613 | static struct page **pages; |
| 614 | static unsigned long *bitmap; |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 615 | size_t pages_size = pcpu_nr_units * pcpu_unit_pages * sizeof(pages[0]); |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 616 | size_t bitmap_size = BITS_TO_LONGS(pcpu_unit_pages) * |
| 617 | sizeof(unsigned long); |
| 618 | |
| 619 | if (!pages || !bitmap) { |
| 620 | if (may_alloc && !pages) |
| 621 | pages = pcpu_mem_alloc(pages_size); |
| 622 | if (may_alloc && !bitmap) |
| 623 | bitmap = pcpu_mem_alloc(bitmap_size); |
| 624 | if (!pages || !bitmap) |
| 625 | return NULL; |
| 626 | } |
| 627 | |
| 628 | memset(pages, 0, pages_size); |
| 629 | bitmap_copy(bitmap, chunk->populated, pcpu_unit_pages); |
| 630 | |
| 631 | *bitmapp = bitmap; |
| 632 | return pages; |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * pcpu_free_pages - free pages which were allocated for @chunk |
| 637 | * @chunk: chunk pages were allocated for |
| 638 | * @pages: array of pages to be freed, indexed by pcpu_page_idx() |
| 639 | * @populated: populated bitmap |
| 640 | * @page_start: page index of the first page to be freed |
| 641 | * @page_end: page index of the last page to be freed + 1 |
| 642 | * |
| 643 | * Free pages [@page_start and @page_end) in @pages for all units. |
| 644 | * The pages were allocated for @chunk. |
| 645 | */ |
| 646 | static void pcpu_free_pages(struct pcpu_chunk *chunk, |
| 647 | struct page **pages, unsigned long *populated, |
| 648 | int page_start, int page_end) |
| 649 | { |
| 650 | unsigned int cpu; |
| 651 | int i; |
| 652 | |
| 653 | for_each_possible_cpu(cpu) { |
| 654 | for (i = page_start; i < page_end; i++) { |
| 655 | struct page *page = pages[pcpu_page_idx(cpu, i)]; |
| 656 | |
| 657 | if (page) |
| 658 | __free_page(page); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * pcpu_alloc_pages - allocates pages for @chunk |
| 665 | * @chunk: target chunk |
| 666 | * @pages: array to put the allocated pages into, indexed by pcpu_page_idx() |
| 667 | * @populated: populated bitmap |
| 668 | * @page_start: page index of the first page to be allocated |
| 669 | * @page_end: page index of the last page to be allocated + 1 |
| 670 | * |
| 671 | * Allocate pages [@page_start,@page_end) into @pages for all units. |
| 672 | * The allocation is for @chunk. Percpu core doesn't care about the |
| 673 | * content of @pages and will pass it verbatim to pcpu_map_pages(). |
| 674 | */ |
| 675 | static int pcpu_alloc_pages(struct pcpu_chunk *chunk, |
| 676 | struct page **pages, unsigned long *populated, |
| 677 | int page_start, int page_end) |
| 678 | { |
| 679 | const gfp_t gfp = GFP_KERNEL | __GFP_HIGHMEM | __GFP_COLD; |
| 680 | unsigned int cpu; |
| 681 | int i; |
| 682 | |
| 683 | for_each_possible_cpu(cpu) { |
| 684 | for (i = page_start; i < page_end; i++) { |
| 685 | struct page **pagep = &pages[pcpu_page_idx(cpu, i)]; |
| 686 | |
| 687 | *pagep = alloc_pages_node(cpu_to_node(cpu), gfp, 0); |
| 688 | if (!*pagep) { |
| 689 | pcpu_free_pages(chunk, pages, populated, |
| 690 | page_start, page_end); |
| 691 | return -ENOMEM; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * pcpu_pre_unmap_flush - flush cache prior to unmapping |
| 700 | * @chunk: chunk the regions to be flushed belongs to |
| 701 | * @page_start: page index of the first page to be flushed |
| 702 | * @page_end: page index of the last page to be flushed + 1 |
| 703 | * |
| 704 | * Pages in [@page_start,@page_end) of @chunk are about to be |
| 705 | * unmapped. Flush cache. As each flushing trial can be very |
| 706 | * expensive, issue flush on the whole region at once rather than |
| 707 | * doing it for each cpu. This could be an overkill but is more |
| 708 | * scalable. |
| 709 | */ |
| 710 | static void pcpu_pre_unmap_flush(struct pcpu_chunk *chunk, |
| 711 | int page_start, int page_end) |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 712 | { |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 713 | flush_cache_vunmap( |
| 714 | pcpu_chunk_addr(chunk, pcpu_first_unit_cpu, page_start), |
| 715 | pcpu_chunk_addr(chunk, pcpu_last_unit_cpu, page_end)); |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 716 | } |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 717 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 718 | static void __pcpu_unmap_pages(unsigned long addr, int nr_pages) |
| 719 | { |
| 720 | unmap_kernel_range_noflush(addr, nr_pages << PAGE_SHIFT); |
| 721 | } |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 722 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 723 | /** |
| 724 | * pcpu_unmap_pages - unmap pages out of a pcpu_chunk |
| 725 | * @chunk: chunk of interest |
| 726 | * @pages: pages array which can be used to pass information to free |
| 727 | * @populated: populated bitmap |
| 728 | * @page_start: page index of the first page to unmap |
| 729 | * @page_end: page index of the last page to unmap + 1 |
| 730 | * |
| 731 | * For each cpu, unmap pages [@page_start,@page_end) out of @chunk. |
| 732 | * Corresponding elements in @pages were cleared by the caller and can |
| 733 | * be used to carry information to pcpu_free_pages() which will be |
| 734 | * called after all unmaps are finished. The caller should call |
| 735 | * proper pre/post flush functions. |
| 736 | */ |
| 737 | static void pcpu_unmap_pages(struct pcpu_chunk *chunk, |
| 738 | struct page **pages, unsigned long *populated, |
| 739 | int page_start, int page_end) |
| 740 | { |
| 741 | unsigned int cpu; |
| 742 | int i; |
| 743 | |
| 744 | for_each_possible_cpu(cpu) { |
| 745 | for (i = page_start; i < page_end; i++) { |
| 746 | struct page *page; |
| 747 | |
| 748 | page = pcpu_chunk_page(chunk, cpu, i); |
| 749 | WARN_ON(!page); |
| 750 | pages[pcpu_page_idx(cpu, i)] = page; |
| 751 | } |
| 752 | __pcpu_unmap_pages(pcpu_chunk_addr(chunk, cpu, page_start), |
| 753 | page_end - page_start); |
| 754 | } |
| 755 | |
| 756 | for (i = page_start; i < page_end; i++) |
| 757 | __clear_bit(i, populated); |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * pcpu_post_unmap_tlb_flush - flush TLB after unmapping |
| 762 | * @chunk: pcpu_chunk the regions to be flushed belong to |
| 763 | * @page_start: page index of the first page to be flushed |
| 764 | * @page_end: page index of the last page to be flushed + 1 |
| 765 | * |
| 766 | * Pages [@page_start,@page_end) of @chunk have been unmapped. Flush |
| 767 | * TLB for the regions. This can be skipped if the area is to be |
| 768 | * returned to vmalloc as vmalloc will handle TLB flushing lazily. |
| 769 | * |
| 770 | * As with pcpu_pre_unmap_flush(), TLB flushing also is done at once |
| 771 | * for the whole region. |
| 772 | */ |
| 773 | static void pcpu_post_unmap_tlb_flush(struct pcpu_chunk *chunk, |
| 774 | int page_start, int page_end) |
| 775 | { |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 776 | flush_tlb_kernel_range( |
| 777 | pcpu_chunk_addr(chunk, pcpu_first_unit_cpu, page_start), |
| 778 | pcpu_chunk_addr(chunk, pcpu_last_unit_cpu, page_end)); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 779 | } |
| 780 | |
Tejun Heo | c8a51be | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 781 | static int __pcpu_map_pages(unsigned long addr, struct page **pages, |
| 782 | int nr_pages) |
| 783 | { |
| 784 | return map_kernel_range_noflush(addr, nr_pages << PAGE_SHIFT, |
| 785 | PAGE_KERNEL, pages); |
| 786 | } |
| 787 | |
| 788 | /** |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 789 | * pcpu_map_pages - map pages into a pcpu_chunk |
Tejun Heo | c8a51be | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 790 | * @chunk: chunk of interest |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 791 | * @pages: pages array containing pages to be mapped |
| 792 | * @populated: populated bitmap |
Tejun Heo | c8a51be | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 793 | * @page_start: page index of the first page to map |
| 794 | * @page_end: page index of the last page to map + 1 |
| 795 | * |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 796 | * For each cpu, map pages [@page_start,@page_end) into @chunk. The |
| 797 | * caller is responsible for calling pcpu_post_map_flush() after all |
| 798 | * mappings are complete. |
| 799 | * |
| 800 | * This function is responsible for setting corresponding bits in |
| 801 | * @chunk->populated bitmap and whatever is necessary for reverse |
| 802 | * lookup (addr -> chunk). |
Tejun Heo | c8a51be | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 803 | */ |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 804 | static int pcpu_map_pages(struct pcpu_chunk *chunk, |
| 805 | struct page **pages, unsigned long *populated, |
| 806 | int page_start, int page_end) |
Tejun Heo | c8a51be | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 807 | { |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 808 | unsigned int cpu, tcpu; |
| 809 | int i, err; |
Tejun Heo | c8a51be | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 810 | |
| 811 | for_each_possible_cpu(cpu) { |
| 812 | err = __pcpu_map_pages(pcpu_chunk_addr(chunk, cpu, page_start), |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 813 | &pages[pcpu_page_idx(cpu, page_start)], |
Tejun Heo | c8a51be | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 814 | page_end - page_start); |
| 815 | if (err < 0) |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 816 | goto err; |
Tejun Heo | c8a51be | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 817 | } |
| 818 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 819 | /* mapping successful, link chunk and mark populated */ |
| 820 | for (i = page_start; i < page_end; i++) { |
| 821 | for_each_possible_cpu(cpu) |
| 822 | pcpu_set_page_chunk(pages[pcpu_page_idx(cpu, i)], |
| 823 | chunk); |
| 824 | __set_bit(i, populated); |
| 825 | } |
| 826 | |
| 827 | return 0; |
| 828 | |
| 829 | err: |
| 830 | for_each_possible_cpu(tcpu) { |
| 831 | if (tcpu == cpu) |
| 832 | break; |
| 833 | __pcpu_unmap_pages(pcpu_chunk_addr(chunk, tcpu, page_start), |
| 834 | page_end - page_start); |
| 835 | } |
| 836 | return err; |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * pcpu_post_map_flush - flush cache after mapping |
| 841 | * @chunk: pcpu_chunk the regions to be flushed belong to |
| 842 | * @page_start: page index of the first page to be flushed |
| 843 | * @page_end: page index of the last page to be flushed + 1 |
| 844 | * |
| 845 | * Pages [@page_start,@page_end) of @chunk have been mapped. Flush |
| 846 | * cache. |
| 847 | * |
| 848 | * As with pcpu_pre_unmap_flush(), TLB flushing also is done at once |
| 849 | * for the whole region. |
| 850 | */ |
| 851 | static void pcpu_post_map_flush(struct pcpu_chunk *chunk, |
| 852 | int page_start, int page_end) |
| 853 | { |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 854 | flush_cache_vmap( |
| 855 | pcpu_chunk_addr(chunk, pcpu_first_unit_cpu, page_start), |
| 856 | pcpu_chunk_addr(chunk, pcpu_last_unit_cpu, page_end)); |
Tejun Heo | c8a51be | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 857 | } |
| 858 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 859 | /** |
| 860 | * pcpu_depopulate_chunk - depopulate and unmap an area of a pcpu_chunk |
| 861 | * @chunk: chunk to depopulate |
| 862 | * @off: offset to the area to depopulate |
Tejun Heo | cae3aeb | 2009-02-21 16:56:23 +0900 | [diff] [blame] | 863 | * @size: size of the area to depopulate in bytes |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 864 | * @flush: whether to flush cache and tlb or not |
| 865 | * |
| 866 | * For each cpu, depopulate and unmap pages [@page_start,@page_end) |
| 867 | * from @chunk. If @flush is true, vcache is flushed before unmapping |
| 868 | * and tlb after. |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 869 | * |
| 870 | * CONTEXT: |
| 871 | * pcpu_alloc_mutex. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 872 | */ |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 873 | static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size) |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 874 | { |
| 875 | int page_start = PFN_DOWN(off); |
| 876 | int page_end = PFN_UP(off + size); |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 877 | struct page **pages; |
| 878 | unsigned long *populated; |
| 879 | int rs, re; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 880 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 881 | /* quick path, check whether it's empty already */ |
| 882 | pcpu_for_each_unpop_region(chunk, rs, re, page_start, page_end) { |
| 883 | if (rs == page_start && re == page_end) |
| 884 | return; |
| 885 | break; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 886 | } |
| 887 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 888 | /* immutable chunks can't be depopulated */ |
| 889 | WARN_ON(chunk->immutable); |
| 890 | |
| 891 | /* |
| 892 | * If control reaches here, there must have been at least one |
| 893 | * successful population attempt so the temp pages array must |
| 894 | * be available now. |
| 895 | */ |
| 896 | pages = pcpu_get_pages_and_bitmap(chunk, &populated, false); |
| 897 | BUG_ON(!pages); |
| 898 | |
| 899 | /* unmap and free */ |
| 900 | pcpu_pre_unmap_flush(chunk, page_start, page_end); |
| 901 | |
| 902 | pcpu_for_each_pop_region(chunk, rs, re, page_start, page_end) |
| 903 | pcpu_unmap_pages(chunk, pages, populated, rs, re); |
| 904 | |
| 905 | /* no need to flush tlb, vmalloc will handle it lazily */ |
| 906 | |
| 907 | pcpu_for_each_pop_region(chunk, rs, re, page_start, page_end) |
| 908 | pcpu_free_pages(chunk, pages, populated, rs, re); |
| 909 | |
| 910 | /* commit new bitmap */ |
| 911 | bitmap_copy(chunk->populated, populated, pcpu_unit_pages); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 912 | } |
| 913 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 914 | /** |
| 915 | * pcpu_populate_chunk - populate and map an area of a pcpu_chunk |
| 916 | * @chunk: chunk of interest |
| 917 | * @off: offset to the area to populate |
Tejun Heo | cae3aeb | 2009-02-21 16:56:23 +0900 | [diff] [blame] | 918 | * @size: size of the area to populate in bytes |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 919 | * |
| 920 | * For each cpu, populate and map pages [@page_start,@page_end) into |
| 921 | * @chunk. The area is cleared on return. |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 922 | * |
| 923 | * CONTEXT: |
| 924 | * pcpu_alloc_mutex, does GFP_KERNEL allocation. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 925 | */ |
| 926 | static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size) |
| 927 | { |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 928 | int page_start = PFN_DOWN(off); |
| 929 | int page_end = PFN_UP(off + size); |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 930 | int free_end = page_start, unmap_end = page_start; |
| 931 | struct page **pages; |
| 932 | unsigned long *populated; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 933 | unsigned int cpu; |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 934 | int rs, re, rc; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 935 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 936 | /* quick path, check whether all pages are already there */ |
| 937 | pcpu_for_each_pop_region(chunk, rs, re, page_start, page_end) { |
| 938 | if (rs == page_start && re == page_end) |
| 939 | goto clear; |
| 940 | break; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 941 | } |
| 942 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 943 | /* need to allocate and map pages, this chunk can't be immutable */ |
| 944 | WARN_ON(chunk->immutable); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 945 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 946 | pages = pcpu_get_pages_and_bitmap(chunk, &populated, true); |
| 947 | if (!pages) |
| 948 | return -ENOMEM; |
| 949 | |
| 950 | /* alloc and map */ |
| 951 | pcpu_for_each_unpop_region(chunk, rs, re, page_start, page_end) { |
| 952 | rc = pcpu_alloc_pages(chunk, pages, populated, rs, re); |
| 953 | if (rc) |
| 954 | goto err_free; |
| 955 | free_end = re; |
| 956 | } |
| 957 | |
| 958 | pcpu_for_each_unpop_region(chunk, rs, re, page_start, page_end) { |
| 959 | rc = pcpu_map_pages(chunk, pages, populated, rs, re); |
| 960 | if (rc) |
| 961 | goto err_unmap; |
| 962 | unmap_end = re; |
| 963 | } |
| 964 | pcpu_post_map_flush(chunk, page_start, page_end); |
| 965 | |
| 966 | /* commit new bitmap */ |
| 967 | bitmap_copy(chunk->populated, populated, pcpu_unit_pages); |
| 968 | clear: |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 969 | for_each_possible_cpu(cpu) |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 970 | memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 971 | return 0; |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 972 | |
| 973 | err_unmap: |
| 974 | pcpu_pre_unmap_flush(chunk, page_start, unmap_end); |
| 975 | pcpu_for_each_unpop_region(chunk, rs, re, page_start, unmap_end) |
| 976 | pcpu_unmap_pages(chunk, pages, populated, rs, re); |
| 977 | pcpu_post_unmap_tlb_flush(chunk, page_start, unmap_end); |
| 978 | err_free: |
| 979 | pcpu_for_each_unpop_region(chunk, rs, re, page_start, free_end) |
| 980 | pcpu_free_pages(chunk, pages, populated, rs, re); |
| 981 | return rc; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | static void free_pcpu_chunk(struct pcpu_chunk *chunk) |
| 985 | { |
| 986 | if (!chunk) |
| 987 | return; |
| 988 | if (chunk->vm) |
| 989 | free_vm_area(chunk->vm); |
Tejun Heo | 1880d93 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 990 | pcpu_mem_free(chunk->map, chunk->map_alloc * sizeof(chunk->map[0])); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 991 | kfree(chunk); |
| 992 | } |
| 993 | |
| 994 | static struct pcpu_chunk *alloc_pcpu_chunk(void) |
| 995 | { |
| 996 | struct pcpu_chunk *chunk; |
| 997 | |
| 998 | chunk = kzalloc(pcpu_chunk_struct_size, GFP_KERNEL); |
| 999 | if (!chunk) |
| 1000 | return NULL; |
| 1001 | |
Tejun Heo | 1880d93 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 1002 | chunk->map = pcpu_mem_alloc(PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0])); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1003 | chunk->map_alloc = PCPU_DFL_MAP_ALLOC; |
| 1004 | chunk->map[chunk->map_used++] = pcpu_unit_size; |
| 1005 | |
| 1006 | chunk->vm = get_vm_area(pcpu_chunk_size, GFP_KERNEL); |
| 1007 | if (!chunk->vm) { |
| 1008 | free_pcpu_chunk(chunk); |
| 1009 | return NULL; |
| 1010 | } |
| 1011 | |
| 1012 | INIT_LIST_HEAD(&chunk->list); |
| 1013 | chunk->free_size = pcpu_unit_size; |
| 1014 | chunk->contig_hint = pcpu_unit_size; |
| 1015 | |
| 1016 | return chunk; |
| 1017 | } |
| 1018 | |
| 1019 | /** |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1020 | * pcpu_alloc - the percpu allocator |
Tejun Heo | cae3aeb | 2009-02-21 16:56:23 +0900 | [diff] [blame] | 1021 | * @size: size of area to allocate in bytes |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1022 | * @align: alignment of area (max PAGE_SIZE) |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1023 | * @reserved: allocate from the reserved chunk if available |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1024 | * |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1025 | * Allocate percpu area of @size bytes aligned at @align. |
| 1026 | * |
| 1027 | * CONTEXT: |
| 1028 | * Does GFP_KERNEL allocation. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1029 | * |
| 1030 | * RETURNS: |
| 1031 | * Percpu pointer to the allocated area on success, NULL on failure. |
| 1032 | */ |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1033 | static void *pcpu_alloc(size_t size, size_t align, bool reserved) |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1034 | { |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1035 | struct pcpu_chunk *chunk; |
| 1036 | int slot, off; |
| 1037 | |
Tejun Heo | 8d408b4 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1038 | if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE)) { |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1039 | WARN(true, "illegal size (%zu) or align (%zu) for " |
| 1040 | "percpu allocation\n", size, align); |
| 1041 | return NULL; |
| 1042 | } |
| 1043 | |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1044 | mutex_lock(&pcpu_alloc_mutex); |
| 1045 | spin_lock_irq(&pcpu_lock); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1046 | |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1047 | /* serve reserved allocations from the reserved chunk if available */ |
| 1048 | if (reserved && pcpu_reserved_chunk) { |
| 1049 | chunk = pcpu_reserved_chunk; |
Tejun Heo | 9f7dcf2 | 2009-03-07 00:44:09 +0900 | [diff] [blame] | 1050 | if (size > chunk->contig_hint || |
| 1051 | pcpu_extend_area_map(chunk) < 0) |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1052 | goto fail_unlock; |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1053 | off = pcpu_alloc_area(chunk, size, align); |
| 1054 | if (off >= 0) |
| 1055 | goto area_found; |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1056 | goto fail_unlock; |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1057 | } |
| 1058 | |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1059 | restart: |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1060 | /* search through normal chunks */ |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1061 | for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) { |
| 1062 | list_for_each_entry(chunk, &pcpu_slot[slot], list) { |
| 1063 | if (size > chunk->contig_hint) |
| 1064 | continue; |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1065 | |
| 1066 | switch (pcpu_extend_area_map(chunk)) { |
| 1067 | case 0: |
| 1068 | break; |
| 1069 | case 1: |
| 1070 | goto restart; /* pcpu_lock dropped, restart */ |
| 1071 | default: |
| 1072 | goto fail_unlock; |
| 1073 | } |
| 1074 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1075 | off = pcpu_alloc_area(chunk, size, align); |
| 1076 | if (off >= 0) |
| 1077 | goto area_found; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /* hmmm... no space left, create a new chunk */ |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1082 | spin_unlock_irq(&pcpu_lock); |
| 1083 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1084 | chunk = alloc_pcpu_chunk(); |
| 1085 | if (!chunk) |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1086 | goto fail_unlock_mutex; |
| 1087 | |
| 1088 | spin_lock_irq(&pcpu_lock); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1089 | pcpu_chunk_relocate(chunk, -1); |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1090 | goto restart; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1091 | |
| 1092 | area_found: |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1093 | spin_unlock_irq(&pcpu_lock); |
| 1094 | |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1095 | /* populate, map and clear the area */ |
| 1096 | if (pcpu_populate_chunk(chunk, off, size)) { |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1097 | spin_lock_irq(&pcpu_lock); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1098 | pcpu_free_area(chunk, off); |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1099 | goto fail_unlock; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1100 | } |
| 1101 | |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1102 | mutex_unlock(&pcpu_alloc_mutex); |
| 1103 | |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 1104 | /* return address relative to unit0 */ |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1105 | return __addr_to_pcpu_ptr(chunk->vm->addr + off); |
| 1106 | |
| 1107 | fail_unlock: |
| 1108 | spin_unlock_irq(&pcpu_lock); |
| 1109 | fail_unlock_mutex: |
| 1110 | mutex_unlock(&pcpu_alloc_mutex); |
| 1111 | return NULL; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1112 | } |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1113 | |
| 1114 | /** |
| 1115 | * __alloc_percpu - allocate dynamic percpu area |
| 1116 | * @size: size of area to allocate in bytes |
| 1117 | * @align: alignment of area (max PAGE_SIZE) |
| 1118 | * |
| 1119 | * Allocate percpu area of @size bytes aligned at @align. Might |
| 1120 | * sleep. Might trigger writeouts. |
| 1121 | * |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1122 | * CONTEXT: |
| 1123 | * Does GFP_KERNEL allocation. |
| 1124 | * |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1125 | * RETURNS: |
| 1126 | * Percpu pointer to the allocated area on success, NULL on failure. |
| 1127 | */ |
| 1128 | void *__alloc_percpu(size_t size, size_t align) |
| 1129 | { |
| 1130 | return pcpu_alloc(size, align, false); |
| 1131 | } |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1132 | EXPORT_SYMBOL_GPL(__alloc_percpu); |
| 1133 | |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1134 | /** |
| 1135 | * __alloc_reserved_percpu - allocate reserved percpu area |
| 1136 | * @size: size of area to allocate in bytes |
| 1137 | * @align: alignment of area (max PAGE_SIZE) |
| 1138 | * |
| 1139 | * Allocate percpu area of @size bytes aligned at @align from reserved |
| 1140 | * percpu area if arch has set it up; otherwise, allocation is served |
| 1141 | * from the same dynamic area. Might sleep. Might trigger writeouts. |
| 1142 | * |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1143 | * CONTEXT: |
| 1144 | * Does GFP_KERNEL allocation. |
| 1145 | * |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1146 | * RETURNS: |
| 1147 | * Percpu pointer to the allocated area on success, NULL on failure. |
| 1148 | */ |
| 1149 | void *__alloc_reserved_percpu(size_t size, size_t align) |
| 1150 | { |
| 1151 | return pcpu_alloc(size, align, true); |
| 1152 | } |
| 1153 | |
Tejun Heo | a56dbdd | 2009-03-07 00:44:11 +0900 | [diff] [blame] | 1154 | /** |
| 1155 | * pcpu_reclaim - reclaim fully free chunks, workqueue function |
| 1156 | * @work: unused |
| 1157 | * |
| 1158 | * Reclaim all fully free chunks except for the first one. |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1159 | * |
| 1160 | * CONTEXT: |
| 1161 | * workqueue context. |
Tejun Heo | a56dbdd | 2009-03-07 00:44:11 +0900 | [diff] [blame] | 1162 | */ |
| 1163 | static void pcpu_reclaim(struct work_struct *work) |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1164 | { |
Tejun Heo | a56dbdd | 2009-03-07 00:44:11 +0900 | [diff] [blame] | 1165 | LIST_HEAD(todo); |
| 1166 | struct list_head *head = &pcpu_slot[pcpu_nr_slots - 1]; |
| 1167 | struct pcpu_chunk *chunk, *next; |
| 1168 | |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1169 | mutex_lock(&pcpu_alloc_mutex); |
| 1170 | spin_lock_irq(&pcpu_lock); |
Tejun Heo | a56dbdd | 2009-03-07 00:44:11 +0900 | [diff] [blame] | 1171 | |
| 1172 | list_for_each_entry_safe(chunk, next, head, list) { |
| 1173 | WARN_ON(chunk->immutable); |
| 1174 | |
| 1175 | /* spare the first one */ |
| 1176 | if (chunk == list_first_entry(head, struct pcpu_chunk, list)) |
| 1177 | continue; |
| 1178 | |
Tejun Heo | a56dbdd | 2009-03-07 00:44:11 +0900 | [diff] [blame] | 1179 | list_move(&chunk->list, &todo); |
| 1180 | } |
| 1181 | |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1182 | spin_unlock_irq(&pcpu_lock); |
| 1183 | mutex_unlock(&pcpu_alloc_mutex); |
Tejun Heo | a56dbdd | 2009-03-07 00:44:11 +0900 | [diff] [blame] | 1184 | |
| 1185 | list_for_each_entry_safe(chunk, next, &todo, list) { |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1186 | pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size); |
Tejun Heo | a56dbdd | 2009-03-07 00:44:11 +0900 | [diff] [blame] | 1187 | free_pcpu_chunk(chunk); |
| 1188 | } |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | /** |
| 1192 | * free_percpu - free percpu area |
| 1193 | * @ptr: pointer to area to free |
| 1194 | * |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1195 | * Free percpu area @ptr. |
| 1196 | * |
| 1197 | * CONTEXT: |
| 1198 | * Can be called from atomic context. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1199 | */ |
| 1200 | void free_percpu(void *ptr) |
| 1201 | { |
| 1202 | void *addr = __pcpu_ptr_to_addr(ptr); |
| 1203 | struct pcpu_chunk *chunk; |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1204 | unsigned long flags; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1205 | int off; |
| 1206 | |
| 1207 | if (!ptr) |
| 1208 | return; |
| 1209 | |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1210 | spin_lock_irqsave(&pcpu_lock, flags); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1211 | |
| 1212 | chunk = pcpu_chunk_addr_search(addr); |
| 1213 | off = addr - chunk->vm->addr; |
| 1214 | |
| 1215 | pcpu_free_area(chunk, off); |
| 1216 | |
Tejun Heo | a56dbdd | 2009-03-07 00:44:11 +0900 | [diff] [blame] | 1217 | /* if there are more than one fully free chunks, wake up grim reaper */ |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1218 | if (chunk->free_size == pcpu_unit_size) { |
| 1219 | struct pcpu_chunk *pos; |
| 1220 | |
Tejun Heo | a56dbdd | 2009-03-07 00:44:11 +0900 | [diff] [blame] | 1221 | list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list) |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1222 | if (pos != chunk) { |
Tejun Heo | a56dbdd | 2009-03-07 00:44:11 +0900 | [diff] [blame] | 1223 | schedule_work(&pcpu_reclaim_work); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1224 | break; |
| 1225 | } |
| 1226 | } |
| 1227 | |
Tejun Heo | ccea34b | 2009-03-07 00:44:13 +0900 | [diff] [blame] | 1228 | spin_unlock_irqrestore(&pcpu_lock, flags); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1229 | } |
| 1230 | EXPORT_SYMBOL_GPL(free_percpu); |
| 1231 | |
| 1232 | /** |
Tejun Heo | 8d408b4 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1233 | * pcpu_setup_first_chunk - initialize the first percpu chunk |
Tejun Heo | 8d408b4 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1234 | * @static_size: the size of static percpu area in bytes |
Tejun Heo | 38a6be5 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1235 | * @reserved_size: the size of reserved percpu area in bytes, 0 for none |
Tejun Heo | cafe881 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1236 | * @dyn_size: free size for dynamic allocation in bytes, -1 for auto |
Tejun Heo | 38a6be5 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1237 | * @unit_size: unit size in bytes, must be multiple of PAGE_SIZE |
| 1238 | * @base_addr: mapped address |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 1239 | * @unit_map: cpu -> unit map, NULL for sequential mapping |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1240 | * |
Tejun Heo | 8d408b4 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1241 | * Initialize the first percpu chunk which contains the kernel static |
| 1242 | * perpcu area. This function is to be called from arch percpu area |
Tejun Heo | 38a6be5 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1243 | * setup path. |
Tejun Heo | 8d408b4 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1244 | * |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1245 | * @reserved_size, if non-zero, specifies the amount of bytes to |
| 1246 | * reserve after the static area in the first chunk. This reserves |
| 1247 | * the first chunk such that it's available only through reserved |
| 1248 | * percpu allocation. This is primarily used to serve module percpu |
| 1249 | * static areas on architectures where the addressing model has |
| 1250 | * limited offset range for symbol relocations to guarantee module |
| 1251 | * percpu symbols fall inside the relocatable range. |
| 1252 | * |
Tejun Heo | 6074d5b | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1253 | * @dyn_size, if non-negative, determines the number of bytes |
| 1254 | * available for dynamic allocation in the first chunk. Specifying |
| 1255 | * non-negative value makes percpu leave alone the area beyond |
| 1256 | * @static_size + @reserved_size + @dyn_size. |
| 1257 | * |
Tejun Heo | 38a6be5 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1258 | * @unit_size specifies unit size and must be aligned to PAGE_SIZE and |
| 1259 | * equal to or larger than @static_size + @reserved_size + if |
| 1260 | * non-negative, @dyn_size. |
Tejun Heo | 8d408b4 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1261 | * |
Tejun Heo | 38a6be5 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1262 | * The caller should have mapped the first chunk at @base_addr and |
| 1263 | * copied static data to each unit. |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1264 | * |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1265 | * If the first chunk ends up with both reserved and dynamic areas, it |
| 1266 | * is served by two chunks - one to serve the core static and reserved |
| 1267 | * areas and the other for the dynamic area. They share the same vm |
| 1268 | * and page map but uses different area allocation map to stay away |
| 1269 | * from each other. The latter chunk is circulated in the chunk slots |
| 1270 | * and available for dynamic allocation like any other chunks. |
| 1271 | * |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1272 | * RETURNS: |
| 1273 | * The determined pcpu_unit_size which can be used to initialize |
| 1274 | * percpu access. |
| 1275 | */ |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1276 | size_t __init pcpu_setup_first_chunk(size_t static_size, size_t reserved_size, |
Tejun Heo | 38a6be5 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1277 | ssize_t dyn_size, size_t unit_size, |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 1278 | void *base_addr, const int *unit_map) |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1279 | { |
Tejun Heo | 2441d15 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1280 | static struct vm_struct first_vm; |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1281 | static int smap[2], dmap[2]; |
Tejun Heo | 6074d5b | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1282 | size_t size_sum = static_size + reserved_size + |
| 1283 | (dyn_size >= 0 ? dyn_size : 0); |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1284 | struct pcpu_chunk *schunk, *dchunk = NULL; |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 1285 | unsigned int cpu, tcpu; |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1286 | int i; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1287 | |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 1288 | /* sanity checks */ |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1289 | BUILD_BUG_ON(ARRAY_SIZE(smap) >= PCPU_DFL_MAP_ALLOC || |
| 1290 | ARRAY_SIZE(dmap) >= PCPU_DFL_MAP_ALLOC); |
Tejun Heo | 8d408b4 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1291 | BUG_ON(!static_size); |
Tejun Heo | 38a6be5 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1292 | BUG_ON(!base_addr); |
| 1293 | BUG_ON(unit_size < size_sum); |
| 1294 | BUG_ON(unit_size & ~PAGE_MASK); |
| 1295 | BUG_ON(unit_size < PCPU_MIN_UNIT_SIZE); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1296 | |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 1297 | /* determine number of units and verify and initialize pcpu_unit_map */ |
| 1298 | if (unit_map) { |
| 1299 | int first_unit = INT_MAX, last_unit = INT_MIN; |
| 1300 | |
| 1301 | for_each_possible_cpu(cpu) { |
| 1302 | int unit = unit_map[cpu]; |
| 1303 | |
| 1304 | BUG_ON(unit < 0); |
| 1305 | for_each_possible_cpu(tcpu) { |
| 1306 | if (tcpu == cpu) |
| 1307 | break; |
| 1308 | /* the mapping should be one-to-one */ |
| 1309 | BUG_ON(unit_map[tcpu] == unit); |
| 1310 | } |
| 1311 | |
| 1312 | if (unit < first_unit) { |
| 1313 | pcpu_first_unit_cpu = cpu; |
| 1314 | first_unit = unit; |
| 1315 | } |
| 1316 | if (unit > last_unit) { |
| 1317 | pcpu_last_unit_cpu = cpu; |
| 1318 | last_unit = unit; |
| 1319 | } |
| 1320 | } |
| 1321 | pcpu_nr_units = last_unit + 1; |
| 1322 | pcpu_unit_map = unit_map; |
| 1323 | } else { |
| 1324 | int *identity_map; |
| 1325 | |
| 1326 | /* #units == #cpus, identity mapped */ |
| 1327 | identity_map = alloc_bootmem(num_possible_cpus() * |
| 1328 | sizeof(identity_map[0])); |
| 1329 | |
| 1330 | for_each_possible_cpu(cpu) |
| 1331 | identity_map[cpu] = cpu; |
| 1332 | |
| 1333 | pcpu_first_unit_cpu = 0; |
| 1334 | pcpu_last_unit_cpu = pcpu_nr_units - 1; |
| 1335 | pcpu_nr_units = num_possible_cpus(); |
| 1336 | pcpu_unit_map = identity_map; |
| 1337 | } |
| 1338 | |
| 1339 | /* determine basic parameters */ |
Tejun Heo | 38a6be5 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1340 | pcpu_unit_pages = unit_size >> PAGE_SHIFT; |
Tejun Heo | d9b55ee | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1341 | pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT; |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 1342 | pcpu_chunk_size = pcpu_nr_units * pcpu_unit_size; |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1343 | pcpu_chunk_struct_size = sizeof(struct pcpu_chunk) + |
| 1344 | BITS_TO_LONGS(pcpu_unit_pages) * sizeof(unsigned long); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1345 | |
Tejun Heo | cafe881 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1346 | if (dyn_size < 0) |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1347 | dyn_size = pcpu_unit_size - static_size - reserved_size; |
Tejun Heo | cafe881 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1348 | |
Tejun Heo | 38a6be5 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1349 | first_vm.flags = VM_ALLOC; |
| 1350 | first_vm.size = pcpu_chunk_size; |
| 1351 | first_vm.addr = base_addr; |
| 1352 | |
Tejun Heo | d9b55ee | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1353 | /* |
| 1354 | * Allocate chunk slots. The additional last slot is for |
| 1355 | * empty chunks. |
| 1356 | */ |
| 1357 | pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1358 | pcpu_slot = alloc_bootmem(pcpu_nr_slots * sizeof(pcpu_slot[0])); |
| 1359 | for (i = 0; i < pcpu_nr_slots; i++) |
| 1360 | INIT_LIST_HEAD(&pcpu_slot[i]); |
| 1361 | |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1362 | /* |
| 1363 | * Initialize static chunk. If reserved_size is zero, the |
| 1364 | * static chunk covers static area + dynamic allocation area |
| 1365 | * in the first chunk. If reserved_size is not zero, it |
| 1366 | * covers static area + reserved area (mostly used for module |
| 1367 | * static percpu allocation). |
| 1368 | */ |
Tejun Heo | 2441d15 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1369 | schunk = alloc_bootmem(pcpu_chunk_struct_size); |
| 1370 | INIT_LIST_HEAD(&schunk->list); |
| 1371 | schunk->vm = &first_vm; |
Tejun Heo | 61ace7f | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1372 | schunk->map = smap; |
| 1373 | schunk->map_alloc = ARRAY_SIZE(smap); |
Tejun Heo | 38a6be5 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1374 | schunk->immutable = true; |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1375 | bitmap_fill(schunk->populated, pcpu_unit_pages); |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1376 | |
| 1377 | if (reserved_size) { |
| 1378 | schunk->free_size = reserved_size; |
Tejun Heo | ae9e6bc9 | 2009-04-02 13:19:54 +0900 | [diff] [blame] | 1379 | pcpu_reserved_chunk = schunk; |
| 1380 | pcpu_reserved_chunk_limit = static_size + reserved_size; |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1381 | } else { |
| 1382 | schunk->free_size = dyn_size; |
| 1383 | dyn_size = 0; /* dynamic area covered */ |
| 1384 | } |
Tejun Heo | 2441d15 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1385 | schunk->contig_hint = schunk->free_size; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1386 | |
Tejun Heo | 61ace7f | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1387 | schunk->map[schunk->map_used++] = -static_size; |
| 1388 | if (schunk->free_size) |
| 1389 | schunk->map[schunk->map_used++] = schunk->free_size; |
| 1390 | |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1391 | /* init dynamic chunk if necessary */ |
| 1392 | if (dyn_size) { |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1393 | dchunk = alloc_bootmem(pcpu_chunk_struct_size); |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1394 | INIT_LIST_HEAD(&dchunk->list); |
| 1395 | dchunk->vm = &first_vm; |
| 1396 | dchunk->map = dmap; |
| 1397 | dchunk->map_alloc = ARRAY_SIZE(dmap); |
Tejun Heo | 38a6be5 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1398 | dchunk->immutable = true; |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1399 | bitmap_fill(dchunk->populated, pcpu_unit_pages); |
Tejun Heo | edcb463 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1400 | |
| 1401 | dchunk->contig_hint = dchunk->free_size = dyn_size; |
| 1402 | dchunk->map[dchunk->map_used++] = -pcpu_reserved_chunk_limit; |
| 1403 | dchunk->map[dchunk->map_used++] = dchunk->free_size; |
| 1404 | } |
| 1405 | |
Tejun Heo | 2441d15 | 2009-03-06 14:33:59 +0900 | [diff] [blame] | 1406 | /* link the first chunk in */ |
Tejun Heo | ae9e6bc9 | 2009-04-02 13:19:54 +0900 | [diff] [blame] | 1407 | pcpu_first_chunk = dchunk ?: schunk; |
| 1408 | pcpu_chunk_relocate(pcpu_first_chunk, -1); |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1409 | |
| 1410 | /* we're done */ |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 1411 | pcpu_base_addr = schunk->vm->addr; |
Tejun Heo | fbf59bc | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1412 | return pcpu_unit_size; |
| 1413 | } |
Tejun Heo | 66c3a75 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1414 | |
Tejun Heo | 8c4bfc6 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1415 | static size_t pcpu_calc_fc_sizes(size_t static_size, size_t reserved_size, |
| 1416 | ssize_t *dyn_sizep) |
| 1417 | { |
| 1418 | size_t size_sum; |
| 1419 | |
| 1420 | size_sum = PFN_ALIGN(static_size + reserved_size + |
| 1421 | (*dyn_sizep >= 0 ? *dyn_sizep : 0)); |
| 1422 | if (*dyn_sizep != 0) |
| 1423 | *dyn_sizep = size_sum - static_size - reserved_size; |
| 1424 | |
| 1425 | return size_sum; |
| 1426 | } |
| 1427 | |
Tejun Heo | 66c3a75 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1428 | /** |
| 1429 | * pcpu_embed_first_chunk - embed the first percpu chunk into bootmem |
| 1430 | * @static_size: the size of static percpu area in bytes |
| 1431 | * @reserved_size: the size of reserved percpu area in bytes |
| 1432 | * @dyn_size: free size for dynamic allocation in bytes, -1 for auto |
Tejun Heo | 66c3a75 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1433 | * |
| 1434 | * This is a helper to ease setting up embedded first percpu chunk and |
| 1435 | * can be called where pcpu_setup_first_chunk() is expected. |
| 1436 | * |
| 1437 | * If this function is used to setup the first chunk, it is allocated |
| 1438 | * as a contiguous area using bootmem allocator and used as-is without |
| 1439 | * being mapped into vmalloc area. This enables the first chunk to |
| 1440 | * piggy back on the linear physical mapping which often uses larger |
| 1441 | * page size. |
| 1442 | * |
| 1443 | * When @dyn_size is positive, dynamic area might be larger than |
Tejun Heo | 788e5ab | 2009-07-04 08:10:58 +0900 | [diff] [blame] | 1444 | * specified to fill page alignment. When @dyn_size is auto, |
| 1445 | * @dyn_size is just big enough to fill page alignment after static |
| 1446 | * and reserved areas. |
Tejun Heo | 66c3a75 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1447 | * |
| 1448 | * If the needed size is smaller than the minimum or specified unit |
| 1449 | * size, the leftover is returned to the bootmem allocator. |
| 1450 | * |
| 1451 | * RETURNS: |
| 1452 | * The determined pcpu_unit_size which can be used to initialize |
| 1453 | * percpu access on success, -errno on failure. |
| 1454 | */ |
| 1455 | ssize_t __init pcpu_embed_first_chunk(size_t static_size, size_t reserved_size, |
Tejun Heo | 788e5ab | 2009-07-04 08:10:58 +0900 | [diff] [blame] | 1456 | ssize_t dyn_size) |
Tejun Heo | 66c3a75 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1457 | { |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1458 | size_t size_sum, unit_size, chunk_size; |
| 1459 | void *base; |
Tejun Heo | 66c3a75 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1460 | unsigned int cpu; |
| 1461 | |
| 1462 | /* determine parameters and allocate */ |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1463 | size_sum = pcpu_calc_fc_sizes(static_size, reserved_size, &dyn_size); |
Tejun Heo | 66c3a75 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1464 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1465 | unit_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE); |
| 1466 | chunk_size = unit_size * num_possible_cpus(); |
Tejun Heo | fa8a709 | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 1467 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1468 | base = __alloc_bootmem_nopanic(chunk_size, PAGE_SIZE, |
| 1469 | __pa(MAX_DMA_ADDRESS)); |
| 1470 | if (!base) { |
Tejun Heo | fa8a709 | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 1471 | pr_warning("PERCPU: failed to allocate %zu bytes for " |
| 1472 | "embedding\n", chunk_size); |
Tejun Heo | 66c3a75 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1473 | return -ENOMEM; |
Tejun Heo | fa8a709 | 2009-06-22 11:56:24 +0900 | [diff] [blame] | 1474 | } |
Tejun Heo | 66c3a75 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1475 | |
| 1476 | /* return the leftover and copy */ |
| 1477 | for_each_possible_cpu(cpu) { |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1478 | void *ptr = base + cpu * unit_size; |
Tejun Heo | 66c3a75 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1479 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1480 | free_bootmem(__pa(ptr + size_sum), unit_size - size_sum); |
Tejun Heo | 66c3a75 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1481 | memcpy(ptr, __per_cpu_load, static_size); |
| 1482 | } |
| 1483 | |
| 1484 | /* we're ready, commit */ |
| 1485 | pr_info("PERCPU: Embedded %zu pages at %p, static data %zu bytes\n", |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1486 | size_sum >> PAGE_SHIFT, base, static_size); |
Tejun Heo | 66c3a75 | 2009-03-10 16:27:48 +0900 | [diff] [blame] | 1487 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1488 | return pcpu_setup_first_chunk(static_size, reserved_size, dyn_size, |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 1489 | unit_size, base, NULL); |
Tejun Heo | d4b95f8 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | /** |
| 1493 | * pcpu_4k_first_chunk - map the first chunk using PAGE_SIZE pages |
| 1494 | * @static_size: the size of static percpu area in bytes |
| 1495 | * @reserved_size: the size of reserved percpu area in bytes |
| 1496 | * @alloc_fn: function to allocate percpu page, always called with PAGE_SIZE |
| 1497 | * @free_fn: funtion to free percpu page, always called with PAGE_SIZE |
| 1498 | * @populate_pte_fn: function to populate pte |
| 1499 | * |
| 1500 | * This is a helper to ease setting up embedded first percpu chunk and |
| 1501 | * can be called where pcpu_setup_first_chunk() is expected. |
| 1502 | * |
| 1503 | * This is the basic allocator. Static percpu area is allocated |
| 1504 | * page-by-page into vmalloc area. |
| 1505 | * |
| 1506 | * RETURNS: |
| 1507 | * The determined pcpu_unit_size which can be used to initialize |
| 1508 | * percpu access on success, -errno on failure. |
| 1509 | */ |
| 1510 | ssize_t __init pcpu_4k_first_chunk(size_t static_size, size_t reserved_size, |
| 1511 | pcpu_fc_alloc_fn_t alloc_fn, |
| 1512 | pcpu_fc_free_fn_t free_fn, |
| 1513 | pcpu_fc_populate_pte_fn_t populate_pte_fn) |
| 1514 | { |
Tejun Heo | 8f05a6a | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1515 | static struct vm_struct vm; |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1516 | int unit_pages; |
Tejun Heo | d4b95f8 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1517 | size_t pages_size; |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1518 | struct page **pages; |
Tejun Heo | d4b95f8 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1519 | unsigned int cpu; |
| 1520 | int i, j; |
| 1521 | ssize_t ret; |
| 1522 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1523 | unit_pages = PFN_UP(max_t(size_t, static_size + reserved_size, |
| 1524 | PCPU_MIN_UNIT_SIZE)); |
Tejun Heo | d4b95f8 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1525 | |
| 1526 | /* unaligned allocations can't be freed, round up to page size */ |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1527 | pages_size = PFN_ALIGN(unit_pages * num_possible_cpus() * |
| 1528 | sizeof(pages[0])); |
| 1529 | pages = alloc_bootmem(pages_size); |
Tejun Heo | d4b95f8 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1530 | |
Tejun Heo | 8f05a6a | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1531 | /* allocate pages */ |
Tejun Heo | d4b95f8 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1532 | j = 0; |
| 1533 | for_each_possible_cpu(cpu) |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1534 | for (i = 0; i < unit_pages; i++) { |
Tejun Heo | d4b95f8 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1535 | void *ptr; |
| 1536 | |
| 1537 | ptr = alloc_fn(cpu, PAGE_SIZE); |
| 1538 | if (!ptr) { |
| 1539 | pr_warning("PERCPU: failed to allocate " |
| 1540 | "4k page for cpu%u\n", cpu); |
| 1541 | goto enomem; |
| 1542 | } |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1543 | pages[j++] = virt_to_page(ptr); |
Tejun Heo | d4b95f8 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1544 | } |
| 1545 | |
Tejun Heo | 8f05a6a | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1546 | /* allocate vm area, map the pages and copy static data */ |
| 1547 | vm.flags = VM_ALLOC; |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1548 | vm.size = num_possible_cpus() * unit_pages << PAGE_SHIFT; |
Tejun Heo | 8f05a6a | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1549 | vm_area_register_early(&vm, PAGE_SIZE); |
| 1550 | |
| 1551 | for_each_possible_cpu(cpu) { |
| 1552 | unsigned long unit_addr = (unsigned long)vm.addr + |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1553 | (cpu * unit_pages << PAGE_SHIFT); |
Tejun Heo | 8f05a6a | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1554 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1555 | for (i = 0; i < unit_pages; i++) |
Tejun Heo | 8f05a6a | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1556 | populate_pte_fn(unit_addr + (i << PAGE_SHIFT)); |
| 1557 | |
| 1558 | /* pte already populated, the following shouldn't fail */ |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1559 | ret = __pcpu_map_pages(unit_addr, &pages[cpu * unit_pages], |
| 1560 | unit_pages); |
Tejun Heo | 8f05a6a | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1561 | if (ret < 0) |
| 1562 | panic("failed to map percpu area, err=%zd\n", ret); |
| 1563 | |
| 1564 | /* |
| 1565 | * FIXME: Archs with virtual cache should flush local |
| 1566 | * cache for the linear mapping here - something |
| 1567 | * equivalent to flush_cache_vmap() on the local cpu. |
| 1568 | * flush_cache_vmap() can't be used as most supporting |
| 1569 | * data structures are not set up yet. |
| 1570 | */ |
| 1571 | |
| 1572 | /* copy static data */ |
| 1573 | memcpy((void *)unit_addr, __per_cpu_load, static_size); |
| 1574 | } |
| 1575 | |
Tejun Heo | d4b95f8 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1576 | /* we're ready, commit */ |
Tejun Heo | 8f05a6a | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1577 | pr_info("PERCPU: %d 4k pages per cpu, static data %zu bytes\n", |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1578 | unit_pages, static_size); |
Tejun Heo | d4b95f8 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1579 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1580 | ret = pcpu_setup_first_chunk(static_size, reserved_size, -1, |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 1581 | unit_pages << PAGE_SHIFT, vm.addr, NULL); |
Tejun Heo | d4b95f8 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1582 | goto out_free_ar; |
| 1583 | |
| 1584 | enomem: |
| 1585 | while (--j >= 0) |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1586 | free_fn(page_address(pages[j]), PAGE_SIZE); |
Tejun Heo | d4b95f8 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1587 | ret = -ENOMEM; |
| 1588 | out_free_ar: |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1589 | free_bootmem(__pa(pages), pages_size); |
Tejun Heo | d4b95f8 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1590 | return ret; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
Tejun Heo | 8c4bfc6 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1594 | * Large page remapping first chunk setup helper |
| 1595 | */ |
| 1596 | #ifdef CONFIG_NEED_MULTIPLE_NODES |
| 1597 | struct pcpul_ent { |
| 1598 | unsigned int cpu; |
| 1599 | void *ptr; |
| 1600 | }; |
| 1601 | |
| 1602 | static size_t pcpul_size; |
| 1603 | static size_t pcpul_unit_size; |
| 1604 | static struct pcpul_ent *pcpul_map; |
| 1605 | static struct vm_struct pcpul_vm; |
| 1606 | |
Tejun Heo | 8c4bfc6 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1607 | /** |
| 1608 | * pcpu_lpage_first_chunk - remap the first percpu chunk using large page |
| 1609 | * @static_size: the size of static percpu area in bytes |
| 1610 | * @reserved_size: the size of reserved percpu area in bytes |
| 1611 | * @dyn_size: free size for dynamic allocation in bytes, -1 for auto |
| 1612 | * @lpage_size: the size of a large page |
| 1613 | * @alloc_fn: function to allocate percpu lpage, always called with lpage_size |
| 1614 | * @free_fn: function to free percpu memory, @size <= lpage_size |
| 1615 | * @map_fn: function to map percpu lpage, always called with lpage_size |
| 1616 | * |
| 1617 | * This allocator uses large page as unit. A large page is allocated |
| 1618 | * for each cpu and each is remapped into vmalloc area using large |
| 1619 | * page mapping. As large page can be quite large, only part of it is |
| 1620 | * used for the first chunk. Unused part is returned to the bootmem |
| 1621 | * allocator. |
| 1622 | * |
| 1623 | * So, the large pages are mapped twice - once to the physical mapping |
| 1624 | * and to the vmalloc area for the first percpu chunk. The double |
| 1625 | * mapping does add one more large TLB entry pressure but still is |
| 1626 | * much better than only using 4k mappings while still being NUMA |
| 1627 | * friendly. |
| 1628 | * |
| 1629 | * RETURNS: |
| 1630 | * The determined pcpu_unit_size which can be used to initialize |
| 1631 | * percpu access on success, -errno on failure. |
| 1632 | */ |
| 1633 | ssize_t __init pcpu_lpage_first_chunk(size_t static_size, size_t reserved_size, |
| 1634 | ssize_t dyn_size, size_t lpage_size, |
| 1635 | pcpu_fc_alloc_fn_t alloc_fn, |
| 1636 | pcpu_fc_free_fn_t free_fn, |
| 1637 | pcpu_fc_map_fn_t map_fn) |
| 1638 | { |
| 1639 | size_t size_sum; |
| 1640 | size_t map_size; |
| 1641 | unsigned int cpu; |
| 1642 | int i, j; |
| 1643 | ssize_t ret; |
| 1644 | |
| 1645 | /* |
| 1646 | * Currently supports only single page. Supporting multiple |
| 1647 | * pages won't be too difficult if it ever becomes necessary. |
| 1648 | */ |
| 1649 | size_sum = pcpu_calc_fc_sizes(static_size, reserved_size, &dyn_size); |
| 1650 | |
| 1651 | pcpul_unit_size = lpage_size; |
| 1652 | pcpul_size = max_t(size_t, size_sum, PCPU_MIN_UNIT_SIZE); |
| 1653 | if (pcpul_size > pcpul_unit_size) { |
| 1654 | pr_warning("PERCPU: static data is larger than large page, " |
| 1655 | "can't use large page\n"); |
| 1656 | return -EINVAL; |
| 1657 | } |
| 1658 | |
| 1659 | /* allocate pointer array and alloc large pages */ |
| 1660 | map_size = PFN_ALIGN(num_possible_cpus() * sizeof(pcpul_map[0])); |
| 1661 | pcpul_map = alloc_bootmem(map_size); |
| 1662 | |
| 1663 | for_each_possible_cpu(cpu) { |
| 1664 | void *ptr; |
| 1665 | |
| 1666 | ptr = alloc_fn(cpu, lpage_size); |
| 1667 | if (!ptr) { |
| 1668 | pr_warning("PERCPU: failed to allocate large page " |
| 1669 | "for cpu%u\n", cpu); |
| 1670 | goto enomem; |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | * Only use pcpul_size bytes and give back the rest. |
| 1675 | * |
| 1676 | * Ingo: The lpage_size up-rounding bootmem is needed |
| 1677 | * to make sure the partial lpage is still fully RAM - |
| 1678 | * it's not well-specified to have a incompatible area |
| 1679 | * (unmapped RAM, device memory, etc.) in that hole. |
| 1680 | */ |
| 1681 | free_fn(ptr + pcpul_size, lpage_size - pcpul_size); |
| 1682 | |
| 1683 | pcpul_map[cpu].cpu = cpu; |
| 1684 | pcpul_map[cpu].ptr = ptr; |
| 1685 | |
| 1686 | memcpy(ptr, __per_cpu_load, static_size); |
| 1687 | } |
| 1688 | |
| 1689 | /* allocate address and map */ |
| 1690 | pcpul_vm.flags = VM_ALLOC; |
| 1691 | pcpul_vm.size = num_possible_cpus() * pcpul_unit_size; |
| 1692 | vm_area_register_early(&pcpul_vm, pcpul_unit_size); |
| 1693 | |
| 1694 | for_each_possible_cpu(cpu) |
| 1695 | map_fn(pcpul_map[cpu].ptr, pcpul_unit_size, |
| 1696 | pcpul_vm.addr + cpu * pcpul_unit_size); |
| 1697 | |
| 1698 | /* we're ready, commit */ |
| 1699 | pr_info("PERCPU: Remapped at %p with large pages, static data " |
| 1700 | "%zu bytes\n", pcpul_vm.addr, static_size); |
| 1701 | |
Tejun Heo | ce3141a | 2009-07-04 08:11:00 +0900 | [diff] [blame] | 1702 | ret = pcpu_setup_first_chunk(static_size, reserved_size, dyn_size, |
Tejun Heo | 2f39e63 | 2009-07-04 08:11:00 +0900 | [diff] [blame^] | 1703 | pcpul_unit_size, pcpul_vm.addr, NULL); |
Tejun Heo | 8c4bfc6 | 2009-07-04 08:10:59 +0900 | [diff] [blame] | 1704 | |
| 1705 | /* sort pcpul_map array for pcpu_lpage_remapped() */ |
| 1706 | for (i = 0; i < num_possible_cpus() - 1; i++) |
| 1707 | for (j = i + 1; j < num_possible_cpus(); j++) |
| 1708 | if (pcpul_map[i].ptr > pcpul_map[j].ptr) { |
| 1709 | struct pcpul_ent tmp = pcpul_map[i]; |
| 1710 | pcpul_map[i] = pcpul_map[j]; |
| 1711 | pcpul_map[j] = tmp; |
| 1712 | } |
| 1713 | |
| 1714 | return ret; |
| 1715 | |
| 1716 | enomem: |
| 1717 | for_each_possible_cpu(cpu) |
| 1718 | if (pcpul_map[cpu].ptr) |
| 1719 | free_fn(pcpul_map[cpu].ptr, pcpul_size); |
| 1720 | free_bootmem(__pa(pcpul_map), map_size); |
| 1721 | return -ENOMEM; |
| 1722 | } |
| 1723 | |
| 1724 | /** |
| 1725 | * pcpu_lpage_remapped - determine whether a kaddr is in pcpul recycled area |
| 1726 | * @kaddr: the kernel address in question |
| 1727 | * |
| 1728 | * Determine whether @kaddr falls in the pcpul recycled area. This is |
| 1729 | * used by pageattr to detect VM aliases and break up the pcpu large |
| 1730 | * page mapping such that the same physical page is not mapped under |
| 1731 | * different attributes. |
| 1732 | * |
| 1733 | * The recycled area is always at the tail of a partially used large |
| 1734 | * page. |
| 1735 | * |
| 1736 | * RETURNS: |
| 1737 | * Address of corresponding remapped pcpu address if match is found; |
| 1738 | * otherwise, NULL. |
| 1739 | */ |
| 1740 | void *pcpu_lpage_remapped(void *kaddr) |
| 1741 | { |
| 1742 | unsigned long unit_mask = pcpul_unit_size - 1; |
| 1743 | void *lpage_addr = (void *)((unsigned long)kaddr & ~unit_mask); |
| 1744 | unsigned long offset = (unsigned long)kaddr & unit_mask; |
| 1745 | int left = 0, right = num_possible_cpus() - 1; |
| 1746 | int pos; |
| 1747 | |
| 1748 | /* pcpul in use at all? */ |
| 1749 | if (!pcpul_map) |
| 1750 | return NULL; |
| 1751 | |
| 1752 | /* okay, perform binary search */ |
| 1753 | while (left <= right) { |
| 1754 | pos = (left + right) / 2; |
| 1755 | |
| 1756 | if (pcpul_map[pos].ptr < lpage_addr) |
| 1757 | left = pos + 1; |
| 1758 | else if (pcpul_map[pos].ptr > lpage_addr) |
| 1759 | right = pos - 1; |
| 1760 | else { |
| 1761 | /* it shouldn't be in the area for the first chunk */ |
| 1762 | WARN_ON(offset < pcpul_size); |
| 1763 | |
| 1764 | return pcpul_vm.addr + |
| 1765 | pcpul_map[pos].cpu * pcpul_unit_size + offset; |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | return NULL; |
| 1770 | } |
| 1771 | #endif |
| 1772 | |
| 1773 | /* |
Tejun Heo | e74e396 | 2009-03-30 19:07:44 +0900 | [diff] [blame] | 1774 | * Generic percpu area setup. |
| 1775 | * |
| 1776 | * The embedding helper is used because its behavior closely resembles |
| 1777 | * the original non-dynamic generic percpu area setup. This is |
| 1778 | * important because many archs have addressing restrictions and might |
| 1779 | * fail if the percpu area is located far away from the previous |
| 1780 | * location. As an added bonus, in non-NUMA cases, embedding is |
| 1781 | * generally a good idea TLB-wise because percpu area can piggy back |
| 1782 | * on the physical linear memory mapping which uses large page |
| 1783 | * mappings on applicable archs. |
| 1784 | */ |
| 1785 | #ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA |
| 1786 | unsigned long __per_cpu_offset[NR_CPUS] __read_mostly; |
| 1787 | EXPORT_SYMBOL(__per_cpu_offset); |
| 1788 | |
| 1789 | void __init setup_per_cpu_areas(void) |
| 1790 | { |
| 1791 | size_t static_size = __per_cpu_end - __per_cpu_start; |
| 1792 | ssize_t unit_size; |
| 1793 | unsigned long delta; |
| 1794 | unsigned int cpu; |
| 1795 | |
| 1796 | /* |
| 1797 | * Always reserve area for module percpu variables. That's |
| 1798 | * what the legacy allocator did. |
| 1799 | */ |
| 1800 | unit_size = pcpu_embed_first_chunk(static_size, PERCPU_MODULE_RESERVE, |
Tejun Heo | 788e5ab | 2009-07-04 08:10:58 +0900 | [diff] [blame] | 1801 | PERCPU_DYNAMIC_RESERVE); |
Tejun Heo | e74e396 | 2009-03-30 19:07:44 +0900 | [diff] [blame] | 1802 | if (unit_size < 0) |
| 1803 | panic("Failed to initialized percpu areas."); |
| 1804 | |
| 1805 | delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start; |
| 1806 | for_each_possible_cpu(cpu) |
| 1807 | __per_cpu_offset[cpu] = delta + cpu * unit_size; |
| 1808 | } |
| 1809 | #endif /* CONFIG_HAVE_SETUP_PER_CPU_AREA */ |