Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1 | /* |
| 2 | * zsmalloc memory allocator |
| 3 | * |
| 4 | * Copyright (C) 2011 Nitin Gupta |
Minchan Kim | 31fc00b | 2014-01-30 15:45:55 -0800 | [diff] [blame] | 5 | * Copyright (C) 2012, 2013 Minchan Kim |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 6 | * |
| 7 | * This code is released using a dual license strategy: BSD/GPL |
| 8 | * You can choose the license that better fits your requirements. |
| 9 | * |
| 10 | * Released under the terms of 3-clause BSD License |
| 11 | * Released under the terms of GNU General Public License Version 2.0 |
| 12 | */ |
| 13 | |
Nitin Gupta | 2db51da | 2012-06-09 17:41:14 -0700 | [diff] [blame] | 14 | /* |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 15 | * This allocator is designed for use with zram. Thus, the allocator is |
| 16 | * supposed to work well under low memory conditions. In particular, it |
| 17 | * never attempts higher order page allocation which is very likely to |
| 18 | * fail under memory pressure. On the other hand, if we just use single |
| 19 | * (0-order) pages, it would suffer from very high fragmentation -- |
| 20 | * any object of size PAGE_SIZE/2 or larger would occupy an entire page. |
| 21 | * This was one of the major issues with its predecessor (xvmalloc). |
Nitin Gupta | 2db51da | 2012-06-09 17:41:14 -0700 | [diff] [blame] | 22 | * |
| 23 | * To overcome these issues, zsmalloc allocates a bunch of 0-order pages |
| 24 | * and links them together using various 'struct page' fields. These linked |
| 25 | * pages act as a single higher-order page i.e. an object can span 0-order |
| 26 | * page boundaries. The code refers to these linked pages as a single entity |
| 27 | * called zspage. |
| 28 | * |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 29 | * For simplicity, zsmalloc can only allocate objects of size up to PAGE_SIZE |
| 30 | * since this satisfies the requirements of all its current users (in the |
| 31 | * worst case, page is incompressible and is thus stored "as-is" i.e. in |
| 32 | * uncompressed form). For allocation requests larger than this size, failure |
| 33 | * is returned (see zs_malloc). |
| 34 | * |
| 35 | * Additionally, zs_malloc() does not return a dereferenceable pointer. |
| 36 | * Instead, it returns an opaque handle (unsigned long) which encodes actual |
| 37 | * location of the allocated object. The reason for this indirection is that |
| 38 | * zsmalloc does not keep zspages permanently mapped since that would cause |
| 39 | * issues on 32-bit systems where the VA region for kernel space mappings |
| 40 | * is very small. So, before using the allocating memory, the object has to |
| 41 | * be mapped using zs_map_object() to get a usable pointer and subsequently |
| 42 | * unmapped using zs_unmap_object(). |
| 43 | * |
Nitin Gupta | 2db51da | 2012-06-09 17:41:14 -0700 | [diff] [blame] | 44 | * Following is how we use various fields and flags of underlying |
| 45 | * struct page(s) to form a zspage. |
| 46 | * |
| 47 | * Usage of struct page fields: |
| 48 | * page->first_page: points to the first component (0-order) page |
| 49 | * page->index (union with page->freelist): offset of the first object |
| 50 | * starting in this page. For the first page, this is |
| 51 | * always 0, so we use this field (aka freelist) to point |
| 52 | * to the first free object in zspage. |
| 53 | * page->lru: links together all component pages (except the first page) |
| 54 | * of a zspage |
| 55 | * |
| 56 | * For _first_ page only: |
| 57 | * |
| 58 | * page->private (union with page->first_page): refers to the |
| 59 | * component page after the first page |
| 60 | * page->freelist: points to the first free object in zspage. |
| 61 | * Free objects are linked together using in-place |
| 62 | * metadata. |
| 63 | * page->objects: maximum number of objects we can store in this |
| 64 | * zspage (class->zspage_order * PAGE_SIZE / class->size) |
| 65 | * page->lru: links together first pages of various zspages. |
| 66 | * Basically forming list of zspages in a fullness group. |
| 67 | * page->mapping: class index and fullness group of the zspage |
| 68 | * |
| 69 | * Usage of struct page flags: |
| 70 | * PG_private: identifies the first component page |
| 71 | * PG_private2: identifies the last component page |
| 72 | * |
| 73 | */ |
| 74 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 75 | #ifdef CONFIG_ZSMALLOC_DEBUG |
| 76 | #define DEBUG |
| 77 | #endif |
| 78 | |
| 79 | #include <linux/module.h> |
| 80 | #include <linux/kernel.h> |
| 81 | #include <linux/bitops.h> |
| 82 | #include <linux/errno.h> |
| 83 | #include <linux/highmem.h> |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 84 | #include <linux/string.h> |
| 85 | #include <linux/slab.h> |
| 86 | #include <asm/tlbflush.h> |
| 87 | #include <asm/pgtable.h> |
| 88 | #include <linux/cpumask.h> |
| 89 | #include <linux/cpu.h> |
Seth Jennings | 0cbb613 | 2012-02-13 08:47:49 -0600 | [diff] [blame] | 90 | #include <linux/vmalloc.h> |
Seth Jennings | c60369f | 2012-07-18 11:55:55 -0500 | [diff] [blame] | 91 | #include <linux/hardirq.h> |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 92 | #include <linux/spinlock.h> |
| 93 | #include <linux/types.h> |
Minchan Kim | bcf1647 | 2014-01-30 15:45:50 -0800 | [diff] [blame] | 94 | #include <linux/zsmalloc.h> |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * This must be power of 2 and greater than of equal to sizeof(link_free). |
| 98 | * These two conditions ensure that any 'struct link_free' itself doesn't |
| 99 | * span more than 1 page which avoids complex case of mapping 2 pages simply |
| 100 | * to restore link_free pointer values. |
| 101 | */ |
| 102 | #define ZS_ALIGN 8 |
| 103 | |
| 104 | /* |
| 105 | * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single) |
| 106 | * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N. |
| 107 | */ |
| 108 | #define ZS_MAX_ZSPAGE_ORDER 2 |
| 109 | #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER) |
| 110 | |
| 111 | /* |
| 112 | * Object location (<PFN>, <obj_idx>) is encoded as |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 113 | * as single (unsigned long) handle value. |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 114 | * |
| 115 | * Note that object index <obj_idx> is relative to system |
| 116 | * page <PFN> it is stored in, so for each sub-page belonging |
| 117 | * to a zspage, obj_idx starts with 0. |
| 118 | * |
| 119 | * This is made more complicated by various memory models and PAE. |
| 120 | */ |
| 121 | |
| 122 | #ifndef MAX_PHYSMEM_BITS |
| 123 | #ifdef CONFIG_HIGHMEM64G |
| 124 | #define MAX_PHYSMEM_BITS 36 |
| 125 | #else /* !CONFIG_HIGHMEM64G */ |
| 126 | /* |
| 127 | * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just |
| 128 | * be PAGE_SHIFT |
| 129 | */ |
| 130 | #define MAX_PHYSMEM_BITS BITS_PER_LONG |
| 131 | #endif |
| 132 | #endif |
| 133 | #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT) |
| 134 | #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS) |
| 135 | #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1) |
| 136 | |
| 137 | #define MAX(a, b) ((a) >= (b) ? (a) : (b)) |
| 138 | /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */ |
| 139 | #define ZS_MIN_ALLOC_SIZE \ |
| 140 | MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS)) |
| 141 | #define ZS_MAX_ALLOC_SIZE PAGE_SIZE |
| 142 | |
| 143 | /* |
Weijie Yang | 7eb5251 | 2014-06-04 16:11:08 -0700 | [diff] [blame] | 144 | * On systems with 4K page size, this gives 255 size classes! There is a |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 145 | * trader-off here: |
| 146 | * - Large number of size classes is potentially wasteful as free page are |
| 147 | * spread across these classes |
| 148 | * - Small number of size classes causes large internal fragmentation |
| 149 | * - Probably its better to use specific size classes (empirically |
| 150 | * determined). NOTE: all those class sizes must be set as multiple of |
| 151 | * ZS_ALIGN to make sure link_free itself never has to span 2 pages. |
| 152 | * |
| 153 | * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN |
| 154 | * (reason above) |
| 155 | */ |
Seth Jennings | d662b8e | 2013-01-25 11:46:18 -0600 | [diff] [blame] | 156 | #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8) |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 157 | #define ZS_SIZE_CLASSES ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / \ |
| 158 | ZS_SIZE_CLASS_DELTA + 1) |
| 159 | |
| 160 | /* |
| 161 | * We do not maintain any list for completely empty or full pages |
| 162 | */ |
| 163 | enum fullness_group { |
| 164 | ZS_ALMOST_FULL, |
| 165 | ZS_ALMOST_EMPTY, |
| 166 | _ZS_NR_FULLNESS_GROUPS, |
| 167 | |
| 168 | ZS_EMPTY, |
| 169 | ZS_FULL |
| 170 | }; |
| 171 | |
| 172 | /* |
| 173 | * We assign a page to ZS_ALMOST_EMPTY fullness group when: |
| 174 | * n <= N / f, where |
| 175 | * n = number of allocated objects |
| 176 | * N = total number of objects zspage can store |
| 177 | * f = 1/fullness_threshold_frac |
| 178 | * |
| 179 | * Similarly, we assign zspage to: |
| 180 | * ZS_ALMOST_FULL when n > N / f |
| 181 | * ZS_EMPTY when n == 0 |
| 182 | * ZS_FULL when n == N |
| 183 | * |
| 184 | * (see: fix_fullness_group()) |
| 185 | */ |
| 186 | static const int fullness_threshold_frac = 4; |
| 187 | |
| 188 | struct size_class { |
| 189 | /* |
| 190 | * Size of objects stored in this class. Must be multiple |
| 191 | * of ZS_ALIGN. |
| 192 | */ |
| 193 | int size; |
| 194 | unsigned int index; |
| 195 | |
| 196 | /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */ |
| 197 | int pages_per_zspage; |
| 198 | |
| 199 | spinlock_t lock; |
| 200 | |
| 201 | /* stats */ |
| 202 | u64 pages_allocated; |
| 203 | |
| 204 | struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS]; |
| 205 | }; |
| 206 | |
| 207 | /* |
| 208 | * Placed within free objects to form a singly linked list. |
| 209 | * For every zspage, first_page->freelist gives head of this list. |
| 210 | * |
| 211 | * This must be power of 2 and less than or equal to ZS_ALIGN |
| 212 | */ |
| 213 | struct link_free { |
| 214 | /* Handle of next free chunk (encodes <PFN, obj_idx>) */ |
| 215 | void *next; |
| 216 | }; |
| 217 | |
| 218 | struct zs_pool { |
| 219 | struct size_class size_class[ZS_SIZE_CLASSES]; |
| 220 | |
| 221 | gfp_t flags; /* allocation flags used when growing pool */ |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 222 | }; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 223 | |
| 224 | /* |
| 225 | * A zspage's class index and fullness group |
| 226 | * are encoded in its (first)page->mapping |
| 227 | */ |
| 228 | #define CLASS_IDX_BITS 28 |
| 229 | #define FULLNESS_BITS 4 |
| 230 | #define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1) |
| 231 | #define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1) |
| 232 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 233 | struct mapping_area { |
Minchan Kim | 1b945ae | 2013-12-11 11:04:36 +0900 | [diff] [blame] | 234 | #ifdef CONFIG_PGTABLE_MAPPING |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 235 | struct vm_struct *vm; /* vm area for mapping object that span pages */ |
| 236 | #else |
| 237 | char *vm_buf; /* copy buffer for objects that span pages */ |
| 238 | #endif |
| 239 | char *vm_addr; /* address of kmap_atomic()'ed pages */ |
| 240 | enum zs_mapmode vm_mm; /* mapping mode */ |
| 241 | }; |
| 242 | |
| 243 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 244 | /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */ |
| 245 | static DEFINE_PER_CPU(struct mapping_area, zs_map_area); |
| 246 | |
| 247 | static int is_first_page(struct page *page) |
| 248 | { |
Minchan Kim | a27545bf | 2012-04-25 15:23:09 +0900 | [diff] [blame] | 249 | return PagePrivate(page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static int is_last_page(struct page *page) |
| 253 | { |
Minchan Kim | a27545bf | 2012-04-25 15:23:09 +0900 | [diff] [blame] | 254 | return PagePrivate2(page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static void get_zspage_mapping(struct page *page, unsigned int *class_idx, |
| 258 | enum fullness_group *fullness) |
| 259 | { |
| 260 | unsigned long m; |
| 261 | BUG_ON(!is_first_page(page)); |
| 262 | |
| 263 | m = (unsigned long)page->mapping; |
| 264 | *fullness = m & FULLNESS_MASK; |
| 265 | *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK; |
| 266 | } |
| 267 | |
| 268 | static void set_zspage_mapping(struct page *page, unsigned int class_idx, |
| 269 | enum fullness_group fullness) |
| 270 | { |
| 271 | unsigned long m; |
| 272 | BUG_ON(!is_first_page(page)); |
| 273 | |
| 274 | m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) | |
| 275 | (fullness & FULLNESS_MASK); |
| 276 | page->mapping = (struct address_space *)m; |
| 277 | } |
| 278 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 279 | /* |
| 280 | * zsmalloc divides the pool into various size classes where each |
| 281 | * class maintains a list of zspages where each zspage is divided |
| 282 | * into equal sized chunks. Each allocation falls into one of these |
| 283 | * classes depending on its size. This function returns index of the |
| 284 | * size class which has chunk size big enough to hold the give size. |
| 285 | */ |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 286 | static int get_size_class_index(int size) |
| 287 | { |
| 288 | int idx = 0; |
| 289 | |
| 290 | if (likely(size > ZS_MIN_ALLOC_SIZE)) |
| 291 | idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE, |
| 292 | ZS_SIZE_CLASS_DELTA); |
| 293 | |
| 294 | return idx; |
| 295 | } |
| 296 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 297 | /* |
| 298 | * For each size class, zspages are divided into different groups |
| 299 | * depending on how "full" they are. This was done so that we could |
| 300 | * easily find empty or nearly empty zspages when we try to shrink |
| 301 | * the pool (not yet implemented). This function returns fullness |
| 302 | * status of the given page. |
| 303 | */ |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 304 | static enum fullness_group get_fullness_group(struct page *page) |
| 305 | { |
| 306 | int inuse, max_objects; |
| 307 | enum fullness_group fg; |
| 308 | BUG_ON(!is_first_page(page)); |
| 309 | |
| 310 | inuse = page->inuse; |
| 311 | max_objects = page->objects; |
| 312 | |
| 313 | if (inuse == 0) |
| 314 | fg = ZS_EMPTY; |
| 315 | else if (inuse == max_objects) |
| 316 | fg = ZS_FULL; |
| 317 | else if (inuse <= max_objects / fullness_threshold_frac) |
| 318 | fg = ZS_ALMOST_EMPTY; |
| 319 | else |
| 320 | fg = ZS_ALMOST_FULL; |
| 321 | |
| 322 | return fg; |
| 323 | } |
| 324 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 325 | /* |
| 326 | * Each size class maintains various freelists and zspages are assigned |
| 327 | * to one of these freelists based on the number of live objects they |
| 328 | * have. This functions inserts the given zspage into the freelist |
| 329 | * identified by <class, fullness_group>. |
| 330 | */ |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 331 | static void insert_zspage(struct page *page, struct size_class *class, |
| 332 | enum fullness_group fullness) |
| 333 | { |
| 334 | struct page **head; |
| 335 | |
| 336 | BUG_ON(!is_first_page(page)); |
| 337 | |
| 338 | if (fullness >= _ZS_NR_FULLNESS_GROUPS) |
| 339 | return; |
| 340 | |
| 341 | head = &class->fullness_list[fullness]; |
| 342 | if (*head) |
| 343 | list_add_tail(&page->lru, &(*head)->lru); |
| 344 | |
| 345 | *head = page; |
| 346 | } |
| 347 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 348 | /* |
| 349 | * This function removes the given zspage from the freelist identified |
| 350 | * by <class, fullness_group>. |
| 351 | */ |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 352 | static void remove_zspage(struct page *page, struct size_class *class, |
| 353 | enum fullness_group fullness) |
| 354 | { |
| 355 | struct page **head; |
| 356 | |
| 357 | BUG_ON(!is_first_page(page)); |
| 358 | |
| 359 | if (fullness >= _ZS_NR_FULLNESS_GROUPS) |
| 360 | return; |
| 361 | |
| 362 | head = &class->fullness_list[fullness]; |
| 363 | BUG_ON(!*head); |
| 364 | if (list_empty(&(*head)->lru)) |
| 365 | *head = NULL; |
| 366 | else if (*head == page) |
| 367 | *head = (struct page *)list_entry((*head)->lru.next, |
| 368 | struct page, lru); |
| 369 | |
| 370 | list_del_init(&page->lru); |
| 371 | } |
| 372 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 373 | /* |
| 374 | * Each size class maintains zspages in different fullness groups depending |
| 375 | * on the number of live objects they contain. When allocating or freeing |
| 376 | * objects, the fullness status of the page can change, say, from ALMOST_FULL |
| 377 | * to ALMOST_EMPTY when freeing an object. This function checks if such |
| 378 | * a status change has occurred for the given page and accordingly moves the |
| 379 | * page from the freelist of the old fullness group to that of the new |
| 380 | * fullness group. |
| 381 | */ |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 382 | static enum fullness_group fix_fullness_group(struct zs_pool *pool, |
| 383 | struct page *page) |
| 384 | { |
| 385 | int class_idx; |
| 386 | struct size_class *class; |
| 387 | enum fullness_group currfg, newfg; |
| 388 | |
| 389 | BUG_ON(!is_first_page(page)); |
| 390 | |
| 391 | get_zspage_mapping(page, &class_idx, &currfg); |
| 392 | newfg = get_fullness_group(page); |
| 393 | if (newfg == currfg) |
| 394 | goto out; |
| 395 | |
| 396 | class = &pool->size_class[class_idx]; |
| 397 | remove_zspage(page, class, currfg); |
| 398 | insert_zspage(page, class, newfg); |
| 399 | set_zspage_mapping(page, class_idx, newfg); |
| 400 | |
| 401 | out: |
| 402 | return newfg; |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * We have to decide on how many pages to link together |
| 407 | * to form a zspage for each size class. This is important |
| 408 | * to reduce wastage due to unusable space left at end of |
| 409 | * each zspage which is given as: |
| 410 | * wastage = Zp - Zp % size_class |
| 411 | * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ... |
| 412 | * |
| 413 | * For example, for size class of 3/8 * PAGE_SIZE, we should |
| 414 | * link together 3 PAGE_SIZE sized pages to form a zspage |
| 415 | * since then we can perfectly fit in 8 such objects. |
| 416 | */ |
Minchan Kim | 2e3b615 | 2012-05-03 15:40:39 +0900 | [diff] [blame] | 417 | static int get_pages_per_zspage(int class_size) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 418 | { |
| 419 | int i, max_usedpc = 0; |
| 420 | /* zspage order which gives maximum used size per KB */ |
| 421 | int max_usedpc_order = 1; |
| 422 | |
Seth Jennings | 84d4faa | 2012-03-05 11:33:21 -0600 | [diff] [blame] | 423 | for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) { |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 424 | int zspage_size; |
| 425 | int waste, usedpc; |
| 426 | |
| 427 | zspage_size = i * PAGE_SIZE; |
| 428 | waste = zspage_size % class_size; |
| 429 | usedpc = (zspage_size - waste) * 100 / zspage_size; |
| 430 | |
| 431 | if (usedpc > max_usedpc) { |
| 432 | max_usedpc = usedpc; |
| 433 | max_usedpc_order = i; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | return max_usedpc_order; |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * A single 'zspage' is composed of many system pages which are |
| 442 | * linked together using fields in struct page. This function finds |
| 443 | * the first/head page, given any component page of a zspage. |
| 444 | */ |
| 445 | static struct page *get_first_page(struct page *page) |
| 446 | { |
| 447 | if (is_first_page(page)) |
| 448 | return page; |
| 449 | else |
| 450 | return page->first_page; |
| 451 | } |
| 452 | |
| 453 | static struct page *get_next_page(struct page *page) |
| 454 | { |
| 455 | struct page *next; |
| 456 | |
| 457 | if (is_last_page(page)) |
| 458 | next = NULL; |
| 459 | else if (is_first_page(page)) |
Sunghan Suh | e842b97 | 2013-07-12 16:08:13 +0900 | [diff] [blame] | 460 | next = (struct page *)page_private(page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 461 | else |
| 462 | next = list_entry(page->lru.next, struct page, lru); |
| 463 | |
| 464 | return next; |
| 465 | } |
| 466 | |
Olav Haugan | 6729687 | 2013-11-22 09:30:41 -0800 | [diff] [blame] | 467 | /* |
| 468 | * Encode <page, obj_idx> as a single handle value. |
| 469 | * On hardware platforms with physical memory starting at 0x0 the pfn |
| 470 | * could be 0 so we ensure that the handle will never be 0 by adjusting the |
| 471 | * encoded obj_idx value before encoding. |
| 472 | */ |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 473 | static void *obj_location_to_handle(struct page *page, unsigned long obj_idx) |
| 474 | { |
| 475 | unsigned long handle; |
| 476 | |
| 477 | if (!page) { |
| 478 | BUG_ON(obj_idx); |
| 479 | return NULL; |
| 480 | } |
| 481 | |
| 482 | handle = page_to_pfn(page) << OBJ_INDEX_BITS; |
Olav Haugan | 6729687 | 2013-11-22 09:30:41 -0800 | [diff] [blame] | 483 | handle |= ((obj_idx + 1) & OBJ_INDEX_MASK); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 484 | |
| 485 | return (void *)handle; |
| 486 | } |
| 487 | |
Olav Haugan | 6729687 | 2013-11-22 09:30:41 -0800 | [diff] [blame] | 488 | /* |
| 489 | * Decode <page, obj_idx> pair from the given object handle. We adjust the |
| 490 | * decoded obj_idx back to its original value since it was adjusted in |
| 491 | * obj_location_to_handle(). |
| 492 | */ |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 493 | static void obj_handle_to_location(unsigned long handle, struct page **page, |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 494 | unsigned long *obj_idx) |
| 495 | { |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 496 | *page = pfn_to_page(handle >> OBJ_INDEX_BITS); |
Olav Haugan | 6729687 | 2013-11-22 09:30:41 -0800 | [diff] [blame] | 497 | *obj_idx = (handle & OBJ_INDEX_MASK) - 1; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static unsigned long obj_idx_to_offset(struct page *page, |
| 501 | unsigned long obj_idx, int class_size) |
| 502 | { |
| 503 | unsigned long off = 0; |
| 504 | |
| 505 | if (!is_first_page(page)) |
| 506 | off = page->index; |
| 507 | |
| 508 | return off + obj_idx * class_size; |
| 509 | } |
| 510 | |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 511 | static void reset_page(struct page *page) |
| 512 | { |
| 513 | clear_bit(PG_private, &page->flags); |
| 514 | clear_bit(PG_private_2, &page->flags); |
| 515 | set_page_private(page, 0); |
| 516 | page->mapping = NULL; |
| 517 | page->freelist = NULL; |
Mel Gorman | 22b751c | 2013-02-22 16:34:59 -0800 | [diff] [blame] | 518 | page_mapcount_reset(page); |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 519 | } |
| 520 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 521 | static void free_zspage(struct page *first_page) |
| 522 | { |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 523 | struct page *nextp, *tmp, *head_extra; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 524 | |
| 525 | BUG_ON(!is_first_page(first_page)); |
| 526 | BUG_ON(first_page->inuse); |
| 527 | |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 528 | head_extra = (struct page *)page_private(first_page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 529 | |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 530 | reset_page(first_page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 531 | __free_page(first_page); |
| 532 | |
| 533 | /* zspage with only 1 system page */ |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 534 | if (!head_extra) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 535 | return; |
| 536 | |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 537 | list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) { |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 538 | list_del(&nextp->lru); |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 539 | reset_page(nextp); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 540 | __free_page(nextp); |
| 541 | } |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 542 | reset_page(head_extra); |
| 543 | __free_page(head_extra); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* Initialize a newly allocated zspage */ |
| 547 | static void init_zspage(struct page *first_page, struct size_class *class) |
| 548 | { |
| 549 | unsigned long off = 0; |
| 550 | struct page *page = first_page; |
| 551 | |
| 552 | BUG_ON(!is_first_page(first_page)); |
| 553 | while (page) { |
| 554 | struct page *next_page; |
| 555 | struct link_free *link; |
| 556 | unsigned int i, objs_on_page; |
| 557 | |
| 558 | /* |
| 559 | * page->index stores offset of first object starting |
| 560 | * in the page. For the first page, this is always 0, |
| 561 | * so we use first_page->index (aka ->freelist) to store |
| 562 | * head of corresponding zspage's freelist. |
| 563 | */ |
| 564 | if (page != first_page) |
| 565 | page->index = off; |
| 566 | |
| 567 | link = (struct link_free *)kmap_atomic(page) + |
| 568 | off / sizeof(*link); |
| 569 | objs_on_page = (PAGE_SIZE - off) / class->size; |
| 570 | |
| 571 | for (i = 1; i <= objs_on_page; i++) { |
| 572 | off += class->size; |
| 573 | if (off < PAGE_SIZE) { |
| 574 | link->next = obj_location_to_handle(page, i); |
| 575 | link += class->size / sizeof(*link); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * We now come to the last (full or partial) object on this |
| 581 | * page, which must point to the first object on the next |
| 582 | * page (if present) |
| 583 | */ |
| 584 | next_page = get_next_page(page); |
| 585 | link->next = obj_location_to_handle(next_page, 0); |
| 586 | kunmap_atomic(link); |
| 587 | page = next_page; |
| 588 | off = (off + class->size) % PAGE_SIZE; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /* |
| 593 | * Allocate a zspage for the given size class |
| 594 | */ |
| 595 | static struct page *alloc_zspage(struct size_class *class, gfp_t flags) |
| 596 | { |
| 597 | int i, error; |
Seth Jennings | b4b700c | 2012-06-13 16:03:42 -0500 | [diff] [blame] | 598 | struct page *first_page = NULL, *uninitialized_var(prev_page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 599 | |
| 600 | /* |
| 601 | * Allocate individual pages and link them together as: |
| 602 | * 1. first page->private = first sub-page |
| 603 | * 2. all sub-pages are linked together using page->lru |
| 604 | * 3. each sub-page is linked to the first page using page->first_page |
| 605 | * |
| 606 | * For each size class, First/Head pages are linked together using |
| 607 | * page->lru. Also, we set PG_private to identify the first page |
| 608 | * (i.e. no other sub-page has this flag set) and PG_private_2 to |
| 609 | * identify the last page. |
| 610 | */ |
| 611 | error = -ENOMEM; |
Minchan Kim | 2e3b615 | 2012-05-03 15:40:39 +0900 | [diff] [blame] | 612 | for (i = 0; i < class->pages_per_zspage; i++) { |
Seth Jennings | b4b700c | 2012-06-13 16:03:42 -0500 | [diff] [blame] | 613 | struct page *page; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 614 | |
| 615 | page = alloc_page(flags); |
| 616 | if (!page) |
| 617 | goto cleanup; |
| 618 | |
| 619 | INIT_LIST_HEAD(&page->lru); |
| 620 | if (i == 0) { /* first page */ |
Minchan Kim | a27545bf | 2012-04-25 15:23:09 +0900 | [diff] [blame] | 621 | SetPagePrivate(page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 622 | set_page_private(page, 0); |
| 623 | first_page = page; |
| 624 | first_page->inuse = 0; |
| 625 | } |
| 626 | if (i == 1) |
Sunghan Suh | e842b97 | 2013-07-12 16:08:13 +0900 | [diff] [blame] | 627 | set_page_private(first_page, (unsigned long)page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 628 | if (i >= 1) |
| 629 | page->first_page = first_page; |
| 630 | if (i >= 2) |
| 631 | list_add(&page->lru, &prev_page->lru); |
Minchan Kim | 2e3b615 | 2012-05-03 15:40:39 +0900 | [diff] [blame] | 632 | if (i == class->pages_per_zspage - 1) /* last page */ |
Minchan Kim | a27545bf | 2012-04-25 15:23:09 +0900 | [diff] [blame] | 633 | SetPagePrivate2(page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 634 | prev_page = page; |
| 635 | } |
| 636 | |
| 637 | init_zspage(first_page, class); |
| 638 | |
| 639 | first_page->freelist = obj_location_to_handle(first_page, 0); |
| 640 | /* Maximum number of objects we can store in this zspage */ |
Minchan Kim | 2e3b615 | 2012-05-03 15:40:39 +0900 | [diff] [blame] | 641 | first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 642 | |
| 643 | error = 0; /* Success */ |
| 644 | |
| 645 | cleanup: |
| 646 | if (unlikely(error) && first_page) { |
| 647 | free_zspage(first_page); |
| 648 | first_page = NULL; |
| 649 | } |
| 650 | |
| 651 | return first_page; |
| 652 | } |
| 653 | |
| 654 | static struct page *find_get_zspage(struct size_class *class) |
| 655 | { |
| 656 | int i; |
| 657 | struct page *page; |
| 658 | |
| 659 | for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) { |
| 660 | page = class->fullness_list[i]; |
| 661 | if (page) |
| 662 | break; |
| 663 | } |
| 664 | |
| 665 | return page; |
| 666 | } |
| 667 | |
Minchan Kim | 1b945ae | 2013-12-11 11:04:36 +0900 | [diff] [blame] | 668 | #ifdef CONFIG_PGTABLE_MAPPING |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 669 | static inline int __zs_cpu_up(struct mapping_area *area) |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 670 | { |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 671 | /* |
| 672 | * Make sure we don't leak memory if a cpu UP notification |
| 673 | * and zs_init() race and both call zs_cpu_up() on the same cpu |
| 674 | */ |
| 675 | if (area->vm) |
| 676 | return 0; |
| 677 | area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL); |
| 678 | if (!area->vm) |
| 679 | return -ENOMEM; |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | static inline void __zs_cpu_down(struct mapping_area *area) |
| 684 | { |
| 685 | if (area->vm) |
| 686 | free_vm_area(area->vm); |
| 687 | area->vm = NULL; |
| 688 | } |
| 689 | |
| 690 | static inline void *__zs_map_object(struct mapping_area *area, |
| 691 | struct page *pages[2], int off, int size) |
| 692 | { |
WANG Chao | f6f8ed4 | 2014-08-06 16:06:58 -0700 | [diff] [blame^] | 693 | BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages)); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 694 | area->vm_addr = area->vm->addr; |
| 695 | return area->vm_addr + off; |
| 696 | } |
| 697 | |
| 698 | static inline void __zs_unmap_object(struct mapping_area *area, |
| 699 | struct page *pages[2], int off, int size) |
| 700 | { |
| 701 | unsigned long addr = (unsigned long)area->vm_addr; |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 702 | |
Joerg Roedel | d95abbb | 2013-03-27 01:43:14 +0100 | [diff] [blame] | 703 | unmap_kernel_range(addr, PAGE_SIZE * 2); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 704 | } |
| 705 | |
Minchan Kim | 1b945ae | 2013-12-11 11:04:36 +0900 | [diff] [blame] | 706 | #else /* CONFIG_PGTABLE_MAPPING */ |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 707 | |
| 708 | static inline int __zs_cpu_up(struct mapping_area *area) |
| 709 | { |
| 710 | /* |
| 711 | * Make sure we don't leak memory if a cpu UP notification |
| 712 | * and zs_init() race and both call zs_cpu_up() on the same cpu |
| 713 | */ |
| 714 | if (area->vm_buf) |
| 715 | return 0; |
| 716 | area->vm_buf = (char *)__get_free_page(GFP_KERNEL); |
| 717 | if (!area->vm_buf) |
| 718 | return -ENOMEM; |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static inline void __zs_cpu_down(struct mapping_area *area) |
| 723 | { |
| 724 | if (area->vm_buf) |
| 725 | free_page((unsigned long)area->vm_buf); |
| 726 | area->vm_buf = NULL; |
| 727 | } |
| 728 | |
| 729 | static void *__zs_map_object(struct mapping_area *area, |
| 730 | struct page *pages[2], int off, int size) |
| 731 | { |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 732 | int sizes[2]; |
| 733 | void *addr; |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 734 | char *buf = area->vm_buf; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 735 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 736 | /* disable page faults to match kmap_atomic() return conditions */ |
| 737 | pagefault_disable(); |
| 738 | |
| 739 | /* no read fastpath */ |
| 740 | if (area->vm_mm == ZS_MM_WO) |
| 741 | goto out; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 742 | |
| 743 | sizes[0] = PAGE_SIZE - off; |
| 744 | sizes[1] = size - sizes[0]; |
| 745 | |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 746 | /* copy object to per-cpu buffer */ |
| 747 | addr = kmap_atomic(pages[0]); |
| 748 | memcpy(buf, addr + off, sizes[0]); |
| 749 | kunmap_atomic(addr); |
| 750 | addr = kmap_atomic(pages[1]); |
| 751 | memcpy(buf + sizes[0], addr, sizes[1]); |
| 752 | kunmap_atomic(addr); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 753 | out: |
| 754 | return area->vm_buf; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 755 | } |
| 756 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 757 | static void __zs_unmap_object(struct mapping_area *area, |
| 758 | struct page *pages[2], int off, int size) |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 759 | { |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 760 | int sizes[2]; |
| 761 | void *addr; |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 762 | char *buf = area->vm_buf; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 763 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 764 | /* no write fastpath */ |
| 765 | if (area->vm_mm == ZS_MM_RO) |
| 766 | goto out; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 767 | |
| 768 | sizes[0] = PAGE_SIZE - off; |
| 769 | sizes[1] = size - sizes[0]; |
| 770 | |
| 771 | /* copy per-cpu buffer to object */ |
| 772 | addr = kmap_atomic(pages[0]); |
| 773 | memcpy(addr + off, buf, sizes[0]); |
| 774 | kunmap_atomic(addr); |
| 775 | addr = kmap_atomic(pages[1]); |
| 776 | memcpy(addr, buf + sizes[0], sizes[1]); |
| 777 | kunmap_atomic(addr); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 778 | |
| 779 | out: |
| 780 | /* enable page faults to match kunmap_atomic() return conditions */ |
| 781 | pagefault_enable(); |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 782 | } |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 783 | |
Minchan Kim | 1b945ae | 2013-12-11 11:04:36 +0900 | [diff] [blame] | 784 | #endif /* CONFIG_PGTABLE_MAPPING */ |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 785 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 786 | static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action, |
| 787 | void *pcpu) |
| 788 | { |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 789 | int ret, cpu = (long)pcpu; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 790 | struct mapping_area *area; |
| 791 | |
| 792 | switch (action) { |
| 793 | case CPU_UP_PREPARE: |
| 794 | area = &per_cpu(zs_map_area, cpu); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 795 | ret = __zs_cpu_up(area); |
| 796 | if (ret) |
| 797 | return notifier_from_errno(ret); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 798 | break; |
| 799 | case CPU_DEAD: |
| 800 | case CPU_UP_CANCELED: |
| 801 | area = &per_cpu(zs_map_area, cpu); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 802 | __zs_cpu_down(area); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 803 | break; |
| 804 | } |
| 805 | |
| 806 | return NOTIFY_OK; |
| 807 | } |
| 808 | |
| 809 | static struct notifier_block zs_cpu_nb = { |
| 810 | .notifier_call = zs_cpu_notifier |
| 811 | }; |
| 812 | |
| 813 | static void zs_exit(void) |
| 814 | { |
| 815 | int cpu; |
| 816 | |
Srivatsa S. Bhat | f0e71fc | 2014-03-11 02:09:59 +0530 | [diff] [blame] | 817 | cpu_notifier_register_begin(); |
| 818 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 819 | for_each_online_cpu(cpu) |
| 820 | zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu); |
Srivatsa S. Bhat | f0e71fc | 2014-03-11 02:09:59 +0530 | [diff] [blame] | 821 | __unregister_cpu_notifier(&zs_cpu_nb); |
| 822 | |
| 823 | cpu_notifier_register_done(); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | static int zs_init(void) |
| 827 | { |
| 828 | int cpu, ret; |
| 829 | |
Srivatsa S. Bhat | f0e71fc | 2014-03-11 02:09:59 +0530 | [diff] [blame] | 830 | cpu_notifier_register_begin(); |
| 831 | |
| 832 | __register_cpu_notifier(&zs_cpu_nb); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 833 | for_each_online_cpu(cpu) { |
| 834 | ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu); |
Srivatsa S. Bhat | f0e71fc | 2014-03-11 02:09:59 +0530 | [diff] [blame] | 835 | if (notifier_to_errno(ret)) { |
| 836 | cpu_notifier_register_done(); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 837 | goto fail; |
Srivatsa S. Bhat | f0e71fc | 2014-03-11 02:09:59 +0530 | [diff] [blame] | 838 | } |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 839 | } |
Srivatsa S. Bhat | f0e71fc | 2014-03-11 02:09:59 +0530 | [diff] [blame] | 840 | |
| 841 | cpu_notifier_register_done(); |
| 842 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 843 | return 0; |
| 844 | fail: |
| 845 | zs_exit(); |
| 846 | return notifier_to_errno(ret); |
| 847 | } |
| 848 | |
Davidlohr Bueso | 4bbc0bc | 2013-01-04 12:14:00 -0800 | [diff] [blame] | 849 | /** |
| 850 | * zs_create_pool - Creates an allocation pool to work from. |
Seth Jennings | 0d145a5 | 2013-01-30 09:36:52 -0600 | [diff] [blame] | 851 | * @flags: allocation flags used to allocate pool metadata |
Davidlohr Bueso | 4bbc0bc | 2013-01-04 12:14:00 -0800 | [diff] [blame] | 852 | * |
| 853 | * This function must be called before anything when using |
| 854 | * the zsmalloc allocator. |
| 855 | * |
| 856 | * On success, a pointer to the newly created pool is returned, |
| 857 | * otherwise NULL. |
| 858 | */ |
Seth Jennings | 0d145a5 | 2013-01-30 09:36:52 -0600 | [diff] [blame] | 859 | struct zs_pool *zs_create_pool(gfp_t flags) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 860 | { |
Ben Hutchings | 069f101 | 2012-06-20 02:31:11 +0100 | [diff] [blame] | 861 | int i, ovhd_size; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 862 | struct zs_pool *pool; |
| 863 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 864 | ovhd_size = roundup(sizeof(*pool), PAGE_SIZE); |
| 865 | pool = kzalloc(ovhd_size, GFP_KERNEL); |
| 866 | if (!pool) |
| 867 | return NULL; |
| 868 | |
| 869 | for (i = 0; i < ZS_SIZE_CLASSES; i++) { |
| 870 | int size; |
| 871 | struct size_class *class; |
| 872 | |
| 873 | size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA; |
| 874 | if (size > ZS_MAX_ALLOC_SIZE) |
| 875 | size = ZS_MAX_ALLOC_SIZE; |
| 876 | |
| 877 | class = &pool->size_class[i]; |
| 878 | class->size = size; |
| 879 | class->index = i; |
| 880 | spin_lock_init(&class->lock); |
Minchan Kim | 2e3b615 | 2012-05-03 15:40:39 +0900 | [diff] [blame] | 881 | class->pages_per_zspage = get_pages_per_zspage(size); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 882 | |
| 883 | } |
| 884 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 885 | pool->flags = flags; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 886 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 887 | return pool; |
| 888 | } |
| 889 | EXPORT_SYMBOL_GPL(zs_create_pool); |
| 890 | |
| 891 | void zs_destroy_pool(struct zs_pool *pool) |
| 892 | { |
| 893 | int i; |
| 894 | |
| 895 | for (i = 0; i < ZS_SIZE_CLASSES; i++) { |
| 896 | int fg; |
| 897 | struct size_class *class = &pool->size_class[i]; |
| 898 | |
| 899 | for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) { |
| 900 | if (class->fullness_list[fg]) { |
Marlies Ruck | 93ad5ab | 2013-05-15 16:56:49 -0400 | [diff] [blame] | 901 | pr_info("Freeing non-empty class with size %db, fullness group %d\n", |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 902 | class->size, fg); |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | kfree(pool); |
| 907 | } |
| 908 | EXPORT_SYMBOL_GPL(zs_destroy_pool); |
| 909 | |
| 910 | /** |
| 911 | * zs_malloc - Allocate block of given size from pool. |
| 912 | * @pool: pool to allocate from |
| 913 | * @size: size of block to allocate |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 914 | * |
Minchan Kim | 00a61d8 | 2012-05-03 15:40:40 +0900 | [diff] [blame] | 915 | * On success, handle to the allocated object is returned, |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 916 | * otherwise 0. |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 917 | * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail. |
| 918 | */ |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 919 | unsigned long zs_malloc(struct zs_pool *pool, size_t size) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 920 | { |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 921 | unsigned long obj; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 922 | struct link_free *link; |
| 923 | int class_idx; |
| 924 | struct size_class *class; |
| 925 | |
| 926 | struct page *first_page, *m_page; |
| 927 | unsigned long m_objidx, m_offset; |
| 928 | |
| 929 | if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE)) |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 930 | return 0; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 931 | |
| 932 | class_idx = get_size_class_index(size); |
| 933 | class = &pool->size_class[class_idx]; |
| 934 | BUG_ON(class_idx != class->index); |
| 935 | |
| 936 | spin_lock(&class->lock); |
| 937 | first_page = find_get_zspage(class); |
| 938 | |
| 939 | if (!first_page) { |
| 940 | spin_unlock(&class->lock); |
| 941 | first_page = alloc_zspage(class, pool->flags); |
| 942 | if (unlikely(!first_page)) |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 943 | return 0; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 944 | |
| 945 | set_zspage_mapping(first_page, class->index, ZS_EMPTY); |
| 946 | spin_lock(&class->lock); |
Minchan Kim | 2e3b615 | 2012-05-03 15:40:39 +0900 | [diff] [blame] | 947 | class->pages_allocated += class->pages_per_zspage; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 948 | } |
| 949 | |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 950 | obj = (unsigned long)first_page->freelist; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 951 | obj_handle_to_location(obj, &m_page, &m_objidx); |
| 952 | m_offset = obj_idx_to_offset(m_page, m_objidx, class->size); |
| 953 | |
| 954 | link = (struct link_free *)kmap_atomic(m_page) + |
| 955 | m_offset / sizeof(*link); |
| 956 | first_page->freelist = link->next; |
| 957 | memset(link, POISON_INUSE, sizeof(*link)); |
| 958 | kunmap_atomic(link); |
| 959 | |
| 960 | first_page->inuse++; |
| 961 | /* Now move the zspage to another fullness group, if required */ |
| 962 | fix_fullness_group(pool, first_page); |
| 963 | spin_unlock(&class->lock); |
| 964 | |
| 965 | return obj; |
| 966 | } |
| 967 | EXPORT_SYMBOL_GPL(zs_malloc); |
| 968 | |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 969 | void zs_free(struct zs_pool *pool, unsigned long obj) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 970 | { |
| 971 | struct link_free *link; |
| 972 | struct page *first_page, *f_page; |
| 973 | unsigned long f_objidx, f_offset; |
| 974 | |
| 975 | int class_idx; |
| 976 | struct size_class *class; |
| 977 | enum fullness_group fullness; |
| 978 | |
| 979 | if (unlikely(!obj)) |
| 980 | return; |
| 981 | |
| 982 | obj_handle_to_location(obj, &f_page, &f_objidx); |
| 983 | first_page = get_first_page(f_page); |
| 984 | |
| 985 | get_zspage_mapping(first_page, &class_idx, &fullness); |
| 986 | class = &pool->size_class[class_idx]; |
| 987 | f_offset = obj_idx_to_offset(f_page, f_objidx, class->size); |
| 988 | |
| 989 | spin_lock(&class->lock); |
| 990 | |
| 991 | /* Insert this object in containing zspage's freelist */ |
| 992 | link = (struct link_free *)((unsigned char *)kmap_atomic(f_page) |
| 993 | + f_offset); |
| 994 | link->next = first_page->freelist; |
| 995 | kunmap_atomic(link); |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 996 | first_page->freelist = (void *)obj; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 997 | |
| 998 | first_page->inuse--; |
| 999 | fullness = fix_fullness_group(pool, first_page); |
| 1000 | |
| 1001 | if (fullness == ZS_EMPTY) |
Minchan Kim | 2e3b615 | 2012-05-03 15:40:39 +0900 | [diff] [blame] | 1002 | class->pages_allocated -= class->pages_per_zspage; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1003 | |
| 1004 | spin_unlock(&class->lock); |
| 1005 | |
| 1006 | if (fullness == ZS_EMPTY) |
| 1007 | free_zspage(first_page); |
| 1008 | } |
| 1009 | EXPORT_SYMBOL_GPL(zs_free); |
| 1010 | |
Minchan Kim | 00a61d8 | 2012-05-03 15:40:40 +0900 | [diff] [blame] | 1011 | /** |
| 1012 | * zs_map_object - get address of allocated object from handle. |
| 1013 | * @pool: pool from which the object was allocated |
| 1014 | * @handle: handle returned from zs_malloc |
| 1015 | * |
| 1016 | * Before using an object allocated from zs_malloc, it must be mapped using |
| 1017 | * this function. When done with the object, it must be unmapped using |
Seth Jennings | 166cfda | 2012-07-02 16:15:51 -0500 | [diff] [blame] | 1018 | * zs_unmap_object. |
| 1019 | * |
| 1020 | * Only one object can be mapped per cpu at a time. There is no protection |
| 1021 | * against nested mappings. |
| 1022 | * |
| 1023 | * This function returns with preemption and page faults disabled. |
Sara Bird | 396b7fd | 2013-05-20 15:18:14 -0400 | [diff] [blame] | 1024 | */ |
Seth Jennings | b741851 | 2012-07-02 16:15:52 -0500 | [diff] [blame] | 1025 | void *zs_map_object(struct zs_pool *pool, unsigned long handle, |
| 1026 | enum zs_mapmode mm) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1027 | { |
| 1028 | struct page *page; |
| 1029 | unsigned long obj_idx, off; |
| 1030 | |
| 1031 | unsigned int class_idx; |
| 1032 | enum fullness_group fg; |
| 1033 | struct size_class *class; |
| 1034 | struct mapping_area *area; |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1035 | struct page *pages[2]; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1036 | |
| 1037 | BUG_ON(!handle); |
| 1038 | |
Seth Jennings | c60369f | 2012-07-18 11:55:55 -0500 | [diff] [blame] | 1039 | /* |
| 1040 | * Because we use per-cpu mapping areas shared among the |
| 1041 | * pools/users, we can't allow mapping in interrupt context |
| 1042 | * because it can corrupt another users mappings. |
| 1043 | */ |
| 1044 | BUG_ON(in_interrupt()); |
| 1045 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1046 | obj_handle_to_location(handle, &page, &obj_idx); |
| 1047 | get_zspage_mapping(get_first_page(page), &class_idx, &fg); |
| 1048 | class = &pool->size_class[class_idx]; |
| 1049 | off = obj_idx_to_offset(page, obj_idx, class->size); |
| 1050 | |
| 1051 | area = &get_cpu_var(zs_map_area); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1052 | area->vm_mm = mm; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1053 | if (off + class->size <= PAGE_SIZE) { |
| 1054 | /* this object is contained entirely within a page */ |
| 1055 | area->vm_addr = kmap_atomic(page); |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 1056 | return area->vm_addr + off; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1057 | } |
| 1058 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1059 | /* this object spans two pages */ |
| 1060 | pages[0] = page; |
| 1061 | pages[1] = get_next_page(page); |
| 1062 | BUG_ON(!pages[1]); |
Seth Jennings | b741851 | 2012-07-02 16:15:52 -0500 | [diff] [blame] | 1063 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1064 | return __zs_map_object(area, pages, off, class->size); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1065 | } |
| 1066 | EXPORT_SYMBOL_GPL(zs_map_object); |
| 1067 | |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 1068 | void zs_unmap_object(struct zs_pool *pool, unsigned long handle) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1069 | { |
| 1070 | struct page *page; |
| 1071 | unsigned long obj_idx, off; |
| 1072 | |
| 1073 | unsigned int class_idx; |
| 1074 | enum fullness_group fg; |
| 1075 | struct size_class *class; |
| 1076 | struct mapping_area *area; |
| 1077 | |
| 1078 | BUG_ON(!handle); |
| 1079 | |
| 1080 | obj_handle_to_location(handle, &page, &obj_idx); |
| 1081 | get_zspage_mapping(get_first_page(page), &class_idx, &fg); |
| 1082 | class = &pool->size_class[class_idx]; |
| 1083 | off = obj_idx_to_offset(page, obj_idx, class->size); |
| 1084 | |
Christoph Lameter | 7c8e018 | 2014-06-04 16:07:56 -0700 | [diff] [blame] | 1085 | area = this_cpu_ptr(&zs_map_area); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1086 | if (off + class->size <= PAGE_SIZE) |
| 1087 | kunmap_atomic(area->vm_addr); |
| 1088 | else { |
| 1089 | struct page *pages[2]; |
Seth Jennings | b741851 | 2012-07-02 16:15:52 -0500 | [diff] [blame] | 1090 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1091 | pages[0] = page; |
| 1092 | pages[1] = get_next_page(page); |
| 1093 | BUG_ON(!pages[1]); |
| 1094 | |
| 1095 | __zs_unmap_object(area, pages, off, class->size); |
| 1096 | } |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1097 | put_cpu_var(zs_map_area); |
| 1098 | } |
| 1099 | EXPORT_SYMBOL_GPL(zs_unmap_object); |
| 1100 | |
| 1101 | u64 zs_get_total_size_bytes(struct zs_pool *pool) |
| 1102 | { |
| 1103 | int i; |
| 1104 | u64 npages = 0; |
| 1105 | |
| 1106 | for (i = 0; i < ZS_SIZE_CLASSES; i++) |
| 1107 | npages += pool->size_class[i].pages_allocated; |
| 1108 | |
| 1109 | return npages << PAGE_SHIFT; |
| 1110 | } |
| 1111 | EXPORT_SYMBOL_GPL(zs_get_total_size_bytes); |
Ben Hutchings | 069f101 | 2012-06-20 02:31:11 +0100 | [diff] [blame] | 1112 | |
| 1113 | module_init(zs_init); |
| 1114 | module_exit(zs_exit); |
| 1115 | |
| 1116 | MODULE_LICENSE("Dual BSD/GPL"); |
| 1117 | MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); |