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> |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 94 | #include <linux/debugfs.h> |
Minchan Kim | bcf1647 | 2014-01-30 15:45:50 -0800 | [diff] [blame] | 95 | #include <linux/zsmalloc.h> |
Dan Streetman | c795779 | 2014-08-06 16:08:38 -0700 | [diff] [blame] | 96 | #include <linux/zpool.h> |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * This must be power of 2 and greater than of equal to sizeof(link_free). |
| 100 | * These two conditions ensure that any 'struct link_free' itself doesn't |
| 101 | * span more than 1 page which avoids complex case of mapping 2 pages simply |
| 102 | * to restore link_free pointer values. |
| 103 | */ |
| 104 | #define ZS_ALIGN 8 |
| 105 | |
| 106 | /* |
| 107 | * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single) |
| 108 | * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N. |
| 109 | */ |
| 110 | #define ZS_MAX_ZSPAGE_ORDER 2 |
| 111 | #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER) |
| 112 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 113 | #define ZS_HANDLE_SIZE (sizeof(unsigned long)) |
| 114 | |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 115 | /* |
| 116 | * Object location (<PFN>, <obj_idx>) is encoded as |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 117 | * as single (unsigned long) handle value. |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 118 | * |
| 119 | * Note that object index <obj_idx> is relative to system |
| 120 | * page <PFN> it is stored in, so for each sub-page belonging |
| 121 | * to a zspage, obj_idx starts with 0. |
| 122 | * |
| 123 | * This is made more complicated by various memory models and PAE. |
| 124 | */ |
| 125 | |
| 126 | #ifndef MAX_PHYSMEM_BITS |
| 127 | #ifdef CONFIG_HIGHMEM64G |
| 128 | #define MAX_PHYSMEM_BITS 36 |
| 129 | #else /* !CONFIG_HIGHMEM64G */ |
| 130 | /* |
| 131 | * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just |
| 132 | * be PAGE_SHIFT |
| 133 | */ |
| 134 | #define MAX_PHYSMEM_BITS BITS_PER_LONG |
| 135 | #endif |
| 136 | #endif |
| 137 | #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT) |
| 138 | #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS) |
| 139 | #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1) |
| 140 | |
| 141 | #define MAX(a, b) ((a) >= (b) ? (a) : (b)) |
| 142 | /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */ |
| 143 | #define ZS_MIN_ALLOC_SIZE \ |
| 144 | MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS)) |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 145 | /* each chunk includes extra space to keep handle */ |
| 146 | #define ZS_MAX_ALLOC_SIZE (PAGE_SIZE + ZS_HANDLE_SIZE) |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 147 | |
| 148 | /* |
Weijie Yang | 7eb5251 | 2014-06-04 16:11:08 -0700 | [diff] [blame] | 149 | * 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] | 150 | * trader-off here: |
| 151 | * - Large number of size classes is potentially wasteful as free page are |
| 152 | * spread across these classes |
| 153 | * - Small number of size classes causes large internal fragmentation |
| 154 | * - Probably its better to use specific size classes (empirically |
| 155 | * determined). NOTE: all those class sizes must be set as multiple of |
| 156 | * ZS_ALIGN to make sure link_free itself never has to span 2 pages. |
| 157 | * |
| 158 | * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN |
| 159 | * (reason above) |
| 160 | */ |
Seth Jennings | d662b8e | 2013-01-25 11:46:18 -0600 | [diff] [blame] | 161 | #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8) |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * We do not maintain any list for completely empty or full pages |
| 165 | */ |
| 166 | enum fullness_group { |
| 167 | ZS_ALMOST_FULL, |
| 168 | ZS_ALMOST_EMPTY, |
| 169 | _ZS_NR_FULLNESS_GROUPS, |
| 170 | |
| 171 | ZS_EMPTY, |
| 172 | ZS_FULL |
| 173 | }; |
| 174 | |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 175 | enum zs_stat_type { |
| 176 | OBJ_ALLOCATED, |
| 177 | OBJ_USED, |
| 178 | NR_ZS_STAT_TYPE, |
| 179 | }; |
| 180 | |
| 181 | #ifdef CONFIG_ZSMALLOC_STAT |
| 182 | |
| 183 | static struct dentry *zs_stat_root; |
| 184 | |
| 185 | struct zs_size_stat { |
| 186 | unsigned long objs[NR_ZS_STAT_TYPE]; |
| 187 | }; |
| 188 | |
| 189 | #endif |
| 190 | |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 191 | /* |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 192 | * number of size_classes |
| 193 | */ |
| 194 | static int zs_size_classes; |
| 195 | |
| 196 | /* |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 197 | * We assign a page to ZS_ALMOST_EMPTY fullness group when: |
| 198 | * n <= N / f, where |
| 199 | * n = number of allocated objects |
| 200 | * N = total number of objects zspage can store |
Wang Sheng-Hui | 6dd9737 | 2014-10-09 15:29:59 -0700 | [diff] [blame] | 201 | * f = fullness_threshold_frac |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 202 | * |
| 203 | * Similarly, we assign zspage to: |
| 204 | * ZS_ALMOST_FULL when n > N / f |
| 205 | * ZS_EMPTY when n == 0 |
| 206 | * ZS_FULL when n == N |
| 207 | * |
| 208 | * (see: fix_fullness_group()) |
| 209 | */ |
| 210 | static const int fullness_threshold_frac = 4; |
| 211 | |
| 212 | struct size_class { |
| 213 | /* |
| 214 | * Size of objects stored in this class. Must be multiple |
| 215 | * of ZS_ALIGN. |
| 216 | */ |
| 217 | int size; |
| 218 | unsigned int index; |
| 219 | |
| 220 | /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */ |
| 221 | int pages_per_zspage; |
| 222 | |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 223 | #ifdef CONFIG_ZSMALLOC_STAT |
| 224 | struct zs_size_stat stats; |
| 225 | #endif |
| 226 | |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 227 | spinlock_t lock; |
| 228 | |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 229 | struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS]; |
| 230 | }; |
| 231 | |
| 232 | /* |
| 233 | * Placed within free objects to form a singly linked list. |
| 234 | * For every zspage, first_page->freelist gives head of this list. |
| 235 | * |
| 236 | * This must be power of 2 and less than or equal to ZS_ALIGN |
| 237 | */ |
| 238 | struct link_free { |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 239 | union { |
| 240 | /* |
| 241 | * Position of next free chunk (encodes <PFN, obj_idx>) |
| 242 | * It's valid for non-allocated object |
| 243 | */ |
| 244 | void *next; |
| 245 | /* |
| 246 | * Handle of allocated object. |
| 247 | */ |
| 248 | unsigned long handle; |
| 249 | }; |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | struct zs_pool { |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 253 | char *name; |
| 254 | |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 255 | struct size_class **size_class; |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 256 | struct kmem_cache *handle_cachep; |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 257 | |
| 258 | gfp_t flags; /* allocation flags used when growing pool */ |
Minchan Kim | 13de893 | 2014-10-09 15:29:48 -0700 | [diff] [blame] | 259 | atomic_long_t pages_allocated; |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 260 | |
| 261 | #ifdef CONFIG_ZSMALLOC_STAT |
| 262 | struct dentry *stat_dentry; |
| 263 | #endif |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 264 | }; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 265 | |
| 266 | /* |
| 267 | * A zspage's class index and fullness group |
| 268 | * are encoded in its (first)page->mapping |
| 269 | */ |
| 270 | #define CLASS_IDX_BITS 28 |
| 271 | #define FULLNESS_BITS 4 |
| 272 | #define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1) |
| 273 | #define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1) |
| 274 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 275 | struct mapping_area { |
Minchan Kim | 1b945ae | 2013-12-11 11:04:36 +0900 | [diff] [blame] | 276 | #ifdef CONFIG_PGTABLE_MAPPING |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 277 | struct vm_struct *vm; /* vm area for mapping object that span pages */ |
| 278 | #else |
| 279 | char *vm_buf; /* copy buffer for objects that span pages */ |
| 280 | #endif |
| 281 | char *vm_addr; /* address of kmap_atomic()'ed pages */ |
| 282 | enum zs_mapmode vm_mm; /* mapping mode */ |
| 283 | }; |
| 284 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 285 | static int create_handle_cache(struct zs_pool *pool) |
| 286 | { |
| 287 | pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE, |
| 288 | 0, 0, NULL); |
| 289 | return pool->handle_cachep ? 0 : 1; |
| 290 | } |
| 291 | |
| 292 | static void destroy_handle_cache(struct zs_pool *pool) |
| 293 | { |
| 294 | kmem_cache_destroy(pool->handle_cachep); |
| 295 | } |
| 296 | |
| 297 | static unsigned long alloc_handle(struct zs_pool *pool) |
| 298 | { |
| 299 | return (unsigned long)kmem_cache_alloc(pool->handle_cachep, |
| 300 | pool->flags & ~__GFP_HIGHMEM); |
| 301 | } |
| 302 | |
| 303 | static void free_handle(struct zs_pool *pool, unsigned long handle) |
| 304 | { |
| 305 | kmem_cache_free(pool->handle_cachep, (void *)handle); |
| 306 | } |
| 307 | |
| 308 | static void record_obj(unsigned long handle, unsigned long obj) |
| 309 | { |
| 310 | *(unsigned long *)handle = obj; |
| 311 | } |
| 312 | |
Dan Streetman | c795779 | 2014-08-06 16:08:38 -0700 | [diff] [blame] | 313 | /* zpool driver */ |
| 314 | |
| 315 | #ifdef CONFIG_ZPOOL |
| 316 | |
Ganesh Mahendran | 3eba0c6 | 2015-02-12 15:00:51 -0800 | [diff] [blame] | 317 | static void *zs_zpool_create(char *name, gfp_t gfp, struct zpool_ops *zpool_ops) |
Dan Streetman | c795779 | 2014-08-06 16:08:38 -0700 | [diff] [blame] | 318 | { |
Ganesh Mahendran | 3eba0c6 | 2015-02-12 15:00:51 -0800 | [diff] [blame] | 319 | return zs_create_pool(name, gfp); |
Dan Streetman | c795779 | 2014-08-06 16:08:38 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void zs_zpool_destroy(void *pool) |
| 323 | { |
| 324 | zs_destroy_pool(pool); |
| 325 | } |
| 326 | |
| 327 | static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp, |
| 328 | unsigned long *handle) |
| 329 | { |
| 330 | *handle = zs_malloc(pool, size); |
| 331 | return *handle ? 0 : -1; |
| 332 | } |
| 333 | static void zs_zpool_free(void *pool, unsigned long handle) |
| 334 | { |
| 335 | zs_free(pool, handle); |
| 336 | } |
| 337 | |
| 338 | static int zs_zpool_shrink(void *pool, unsigned int pages, |
| 339 | unsigned int *reclaimed) |
| 340 | { |
| 341 | return -EINVAL; |
| 342 | } |
| 343 | |
| 344 | static void *zs_zpool_map(void *pool, unsigned long handle, |
| 345 | enum zpool_mapmode mm) |
| 346 | { |
| 347 | enum zs_mapmode zs_mm; |
| 348 | |
| 349 | switch (mm) { |
| 350 | case ZPOOL_MM_RO: |
| 351 | zs_mm = ZS_MM_RO; |
| 352 | break; |
| 353 | case ZPOOL_MM_WO: |
| 354 | zs_mm = ZS_MM_WO; |
| 355 | break; |
| 356 | case ZPOOL_MM_RW: /* fallthru */ |
| 357 | default: |
| 358 | zs_mm = ZS_MM_RW; |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | return zs_map_object(pool, handle, zs_mm); |
| 363 | } |
| 364 | static void zs_zpool_unmap(void *pool, unsigned long handle) |
| 365 | { |
| 366 | zs_unmap_object(pool, handle); |
| 367 | } |
| 368 | |
| 369 | static u64 zs_zpool_total_size(void *pool) |
| 370 | { |
Minchan Kim | 722cdc1 | 2014-10-09 15:29:50 -0700 | [diff] [blame] | 371 | return zs_get_total_pages(pool) << PAGE_SHIFT; |
Dan Streetman | c795779 | 2014-08-06 16:08:38 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static struct zpool_driver zs_zpool_driver = { |
| 375 | .type = "zsmalloc", |
| 376 | .owner = THIS_MODULE, |
| 377 | .create = zs_zpool_create, |
| 378 | .destroy = zs_zpool_destroy, |
| 379 | .malloc = zs_zpool_malloc, |
| 380 | .free = zs_zpool_free, |
| 381 | .shrink = zs_zpool_shrink, |
| 382 | .map = zs_zpool_map, |
| 383 | .unmap = zs_zpool_unmap, |
| 384 | .total_size = zs_zpool_total_size, |
| 385 | }; |
| 386 | |
Kees Cook | 137f8cf | 2014-08-29 15:18:40 -0700 | [diff] [blame] | 387 | MODULE_ALIAS("zpool-zsmalloc"); |
Dan Streetman | c795779 | 2014-08-06 16:08:38 -0700 | [diff] [blame] | 388 | #endif /* CONFIG_ZPOOL */ |
| 389 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 390 | /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */ |
| 391 | static DEFINE_PER_CPU(struct mapping_area, zs_map_area); |
| 392 | |
| 393 | static int is_first_page(struct page *page) |
| 394 | { |
Minchan Kim | a27545bf | 2012-04-25 15:23:09 +0900 | [diff] [blame] | 395 | return PagePrivate(page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static int is_last_page(struct page *page) |
| 399 | { |
Minchan Kim | a27545bf | 2012-04-25 15:23:09 +0900 | [diff] [blame] | 400 | return PagePrivate2(page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static void get_zspage_mapping(struct page *page, unsigned int *class_idx, |
| 404 | enum fullness_group *fullness) |
| 405 | { |
| 406 | unsigned long m; |
| 407 | BUG_ON(!is_first_page(page)); |
| 408 | |
| 409 | m = (unsigned long)page->mapping; |
| 410 | *fullness = m & FULLNESS_MASK; |
| 411 | *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK; |
| 412 | } |
| 413 | |
| 414 | static void set_zspage_mapping(struct page *page, unsigned int class_idx, |
| 415 | enum fullness_group fullness) |
| 416 | { |
| 417 | unsigned long m; |
| 418 | BUG_ON(!is_first_page(page)); |
| 419 | |
| 420 | m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) | |
| 421 | (fullness & FULLNESS_MASK); |
| 422 | page->mapping = (struct address_space *)m; |
| 423 | } |
| 424 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 425 | /* |
| 426 | * zsmalloc divides the pool into various size classes where each |
| 427 | * class maintains a list of zspages where each zspage is divided |
| 428 | * into equal sized chunks. Each allocation falls into one of these |
| 429 | * classes depending on its size. This function returns index of the |
| 430 | * size class which has chunk size big enough to hold the give size. |
| 431 | */ |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 432 | static int get_size_class_index(int size) |
| 433 | { |
| 434 | int idx = 0; |
| 435 | |
| 436 | if (likely(size > ZS_MIN_ALLOC_SIZE)) |
| 437 | idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE, |
| 438 | ZS_SIZE_CLASS_DELTA); |
| 439 | |
| 440 | return idx; |
| 441 | } |
| 442 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 443 | /* |
| 444 | * For each size class, zspages are divided into different groups |
| 445 | * depending on how "full" they are. This was done so that we could |
| 446 | * easily find empty or nearly empty zspages when we try to shrink |
| 447 | * the pool (not yet implemented). This function returns fullness |
| 448 | * status of the given page. |
| 449 | */ |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 450 | static enum fullness_group get_fullness_group(struct page *page) |
| 451 | { |
| 452 | int inuse, max_objects; |
| 453 | enum fullness_group fg; |
| 454 | BUG_ON(!is_first_page(page)); |
| 455 | |
| 456 | inuse = page->inuse; |
| 457 | max_objects = page->objects; |
| 458 | |
| 459 | if (inuse == 0) |
| 460 | fg = ZS_EMPTY; |
| 461 | else if (inuse == max_objects) |
| 462 | fg = ZS_FULL; |
| 463 | else if (inuse <= max_objects / fullness_threshold_frac) |
| 464 | fg = ZS_ALMOST_EMPTY; |
| 465 | else |
| 466 | fg = ZS_ALMOST_FULL; |
| 467 | |
| 468 | return fg; |
| 469 | } |
| 470 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 471 | /* |
| 472 | * Each size class maintains various freelists and zspages are assigned |
| 473 | * to one of these freelists based on the number of live objects they |
| 474 | * have. This functions inserts the given zspage into the freelist |
| 475 | * identified by <class, fullness_group>. |
| 476 | */ |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 477 | static void insert_zspage(struct page *page, struct size_class *class, |
| 478 | enum fullness_group fullness) |
| 479 | { |
| 480 | struct page **head; |
| 481 | |
| 482 | BUG_ON(!is_first_page(page)); |
| 483 | |
| 484 | if (fullness >= _ZS_NR_FULLNESS_GROUPS) |
| 485 | return; |
| 486 | |
| 487 | head = &class->fullness_list[fullness]; |
| 488 | if (*head) |
| 489 | list_add_tail(&page->lru, &(*head)->lru); |
| 490 | |
| 491 | *head = page; |
| 492 | } |
| 493 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 494 | /* |
| 495 | * This function removes the given zspage from the freelist identified |
| 496 | * by <class, fullness_group>. |
| 497 | */ |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 498 | static void remove_zspage(struct page *page, struct size_class *class, |
| 499 | enum fullness_group fullness) |
| 500 | { |
| 501 | struct page **head; |
| 502 | |
| 503 | BUG_ON(!is_first_page(page)); |
| 504 | |
| 505 | if (fullness >= _ZS_NR_FULLNESS_GROUPS) |
| 506 | return; |
| 507 | |
| 508 | head = &class->fullness_list[fullness]; |
| 509 | BUG_ON(!*head); |
| 510 | if (list_empty(&(*head)->lru)) |
| 511 | *head = NULL; |
| 512 | else if (*head == page) |
| 513 | *head = (struct page *)list_entry((*head)->lru.next, |
| 514 | struct page, lru); |
| 515 | |
| 516 | list_del_init(&page->lru); |
| 517 | } |
| 518 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 519 | /* |
| 520 | * Each size class maintains zspages in different fullness groups depending |
| 521 | * on the number of live objects they contain. When allocating or freeing |
| 522 | * objects, the fullness status of the page can change, say, from ALMOST_FULL |
| 523 | * to ALMOST_EMPTY when freeing an object. This function checks if such |
| 524 | * a status change has occurred for the given page and accordingly moves the |
| 525 | * page from the freelist of the old fullness group to that of the new |
| 526 | * fullness group. |
| 527 | */ |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame^] | 528 | static enum fullness_group fix_fullness_group(struct size_class *class, |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 529 | struct page *page) |
| 530 | { |
| 531 | int class_idx; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 532 | enum fullness_group currfg, newfg; |
| 533 | |
| 534 | BUG_ON(!is_first_page(page)); |
| 535 | |
| 536 | get_zspage_mapping(page, &class_idx, &currfg); |
| 537 | newfg = get_fullness_group(page); |
| 538 | if (newfg == currfg) |
| 539 | goto out; |
| 540 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 541 | remove_zspage(page, class, currfg); |
| 542 | insert_zspage(page, class, newfg); |
| 543 | set_zspage_mapping(page, class_idx, newfg); |
| 544 | |
| 545 | out: |
| 546 | return newfg; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * We have to decide on how many pages to link together |
| 551 | * to form a zspage for each size class. This is important |
| 552 | * to reduce wastage due to unusable space left at end of |
| 553 | * each zspage which is given as: |
| 554 | * wastage = Zp - Zp % size_class |
| 555 | * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ... |
| 556 | * |
| 557 | * For example, for size class of 3/8 * PAGE_SIZE, we should |
| 558 | * link together 3 PAGE_SIZE sized pages to form a zspage |
| 559 | * since then we can perfectly fit in 8 such objects. |
| 560 | */ |
Minchan Kim | 2e3b615 | 2012-05-03 15:40:39 +0900 | [diff] [blame] | 561 | static int get_pages_per_zspage(int class_size) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 562 | { |
| 563 | int i, max_usedpc = 0; |
| 564 | /* zspage order which gives maximum used size per KB */ |
| 565 | int max_usedpc_order = 1; |
| 566 | |
Seth Jennings | 84d4faa | 2012-03-05 11:33:21 -0600 | [diff] [blame] | 567 | for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) { |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 568 | int zspage_size; |
| 569 | int waste, usedpc; |
| 570 | |
| 571 | zspage_size = i * PAGE_SIZE; |
| 572 | waste = zspage_size % class_size; |
| 573 | usedpc = (zspage_size - waste) * 100 / zspage_size; |
| 574 | |
| 575 | if (usedpc > max_usedpc) { |
| 576 | max_usedpc = usedpc; |
| 577 | max_usedpc_order = i; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | return max_usedpc_order; |
| 582 | } |
| 583 | |
| 584 | /* |
| 585 | * A single 'zspage' is composed of many system pages which are |
| 586 | * linked together using fields in struct page. This function finds |
| 587 | * the first/head page, given any component page of a zspage. |
| 588 | */ |
| 589 | static struct page *get_first_page(struct page *page) |
| 590 | { |
| 591 | if (is_first_page(page)) |
| 592 | return page; |
| 593 | else |
| 594 | return page->first_page; |
| 595 | } |
| 596 | |
| 597 | static struct page *get_next_page(struct page *page) |
| 598 | { |
| 599 | struct page *next; |
| 600 | |
| 601 | if (is_last_page(page)) |
| 602 | next = NULL; |
| 603 | else if (is_first_page(page)) |
Sunghan Suh | e842b97 | 2013-07-12 16:08:13 +0900 | [diff] [blame] | 604 | next = (struct page *)page_private(page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 605 | else |
| 606 | next = list_entry(page->lru.next, struct page, lru); |
| 607 | |
| 608 | return next; |
| 609 | } |
| 610 | |
Olav Haugan | 6729687 | 2013-11-22 09:30:41 -0800 | [diff] [blame] | 611 | /* |
| 612 | * Encode <page, obj_idx> as a single handle value. |
| 613 | * On hardware platforms with physical memory starting at 0x0 the pfn |
| 614 | * could be 0 so we ensure that the handle will never be 0 by adjusting the |
| 615 | * encoded obj_idx value before encoding. |
| 616 | */ |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 617 | static void *obj_location_to_handle(struct page *page, unsigned long obj_idx) |
| 618 | { |
| 619 | unsigned long handle; |
| 620 | |
| 621 | if (!page) { |
| 622 | BUG_ON(obj_idx); |
| 623 | return NULL; |
| 624 | } |
| 625 | |
| 626 | handle = page_to_pfn(page) << OBJ_INDEX_BITS; |
Olav Haugan | 6729687 | 2013-11-22 09:30:41 -0800 | [diff] [blame] | 627 | handle |= ((obj_idx + 1) & OBJ_INDEX_MASK); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 628 | |
| 629 | return (void *)handle; |
| 630 | } |
| 631 | |
Olav Haugan | 6729687 | 2013-11-22 09:30:41 -0800 | [diff] [blame] | 632 | /* |
| 633 | * Decode <page, obj_idx> pair from the given object handle. We adjust the |
| 634 | * decoded obj_idx back to its original value since it was adjusted in |
| 635 | * obj_location_to_handle(). |
| 636 | */ |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 637 | static void obj_to_location(unsigned long handle, struct page **page, |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 638 | unsigned long *obj_idx) |
| 639 | { |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 640 | *page = pfn_to_page(handle >> OBJ_INDEX_BITS); |
Olav Haugan | 6729687 | 2013-11-22 09:30:41 -0800 | [diff] [blame] | 641 | *obj_idx = (handle & OBJ_INDEX_MASK) - 1; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 642 | } |
| 643 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 644 | static unsigned long handle_to_obj(unsigned long handle) |
| 645 | { |
| 646 | return *(unsigned long *)handle; |
| 647 | } |
| 648 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 649 | static unsigned long obj_idx_to_offset(struct page *page, |
| 650 | unsigned long obj_idx, int class_size) |
| 651 | { |
| 652 | unsigned long off = 0; |
| 653 | |
| 654 | if (!is_first_page(page)) |
| 655 | off = page->index; |
| 656 | |
| 657 | return off + obj_idx * class_size; |
| 658 | } |
| 659 | |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 660 | static void reset_page(struct page *page) |
| 661 | { |
| 662 | clear_bit(PG_private, &page->flags); |
| 663 | clear_bit(PG_private_2, &page->flags); |
| 664 | set_page_private(page, 0); |
| 665 | page->mapping = NULL; |
| 666 | page->freelist = NULL; |
Mel Gorman | 22b751c | 2013-02-22 16:34:59 -0800 | [diff] [blame] | 667 | page_mapcount_reset(page); |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 668 | } |
| 669 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 670 | static void free_zspage(struct page *first_page) |
| 671 | { |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 672 | struct page *nextp, *tmp, *head_extra; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 673 | |
| 674 | BUG_ON(!is_first_page(first_page)); |
| 675 | BUG_ON(first_page->inuse); |
| 676 | |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 677 | head_extra = (struct page *)page_private(first_page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 678 | |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 679 | reset_page(first_page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 680 | __free_page(first_page); |
| 681 | |
| 682 | /* zspage with only 1 system page */ |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 683 | if (!head_extra) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 684 | return; |
| 685 | |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 686 | list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) { |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 687 | list_del(&nextp->lru); |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 688 | reset_page(nextp); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 689 | __free_page(nextp); |
| 690 | } |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 691 | reset_page(head_extra); |
| 692 | __free_page(head_extra); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /* Initialize a newly allocated zspage */ |
| 696 | static void init_zspage(struct page *first_page, struct size_class *class) |
| 697 | { |
| 698 | unsigned long off = 0; |
| 699 | struct page *page = first_page; |
| 700 | |
| 701 | BUG_ON(!is_first_page(first_page)); |
| 702 | while (page) { |
| 703 | struct page *next_page; |
| 704 | struct link_free *link; |
Dan Streetman | 5538c56 | 2014-10-09 15:30:01 -0700 | [diff] [blame] | 705 | unsigned int i = 1; |
Minchan Kim | af4ee5e | 2014-12-12 16:56:58 -0800 | [diff] [blame] | 706 | void *vaddr; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 707 | |
| 708 | /* |
| 709 | * page->index stores offset of first object starting |
| 710 | * in the page. For the first page, this is always 0, |
| 711 | * so we use first_page->index (aka ->freelist) to store |
| 712 | * head of corresponding zspage's freelist. |
| 713 | */ |
| 714 | if (page != first_page) |
| 715 | page->index = off; |
| 716 | |
Minchan Kim | af4ee5e | 2014-12-12 16:56:58 -0800 | [diff] [blame] | 717 | vaddr = kmap_atomic(page); |
| 718 | link = (struct link_free *)vaddr + off / sizeof(*link); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 719 | |
Dan Streetman | 5538c56 | 2014-10-09 15:30:01 -0700 | [diff] [blame] | 720 | while ((off += class->size) < PAGE_SIZE) { |
| 721 | link->next = obj_location_to_handle(page, i++); |
| 722 | link += class->size / sizeof(*link); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | /* |
| 726 | * We now come to the last (full or partial) object on this |
| 727 | * page, which must point to the first object on the next |
| 728 | * page (if present) |
| 729 | */ |
| 730 | next_page = get_next_page(page); |
| 731 | link->next = obj_location_to_handle(next_page, 0); |
Minchan Kim | af4ee5e | 2014-12-12 16:56:58 -0800 | [diff] [blame] | 732 | kunmap_atomic(vaddr); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 733 | page = next_page; |
Dan Streetman | 5538c56 | 2014-10-09 15:30:01 -0700 | [diff] [blame] | 734 | off %= PAGE_SIZE; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Allocate a zspage for the given size class |
| 740 | */ |
| 741 | static struct page *alloc_zspage(struct size_class *class, gfp_t flags) |
| 742 | { |
| 743 | int i, error; |
Seth Jennings | b4b700c | 2012-06-13 16:03:42 -0500 | [diff] [blame] | 744 | struct page *first_page = NULL, *uninitialized_var(prev_page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 745 | |
| 746 | /* |
| 747 | * Allocate individual pages and link them together as: |
| 748 | * 1. first page->private = first sub-page |
| 749 | * 2. all sub-pages are linked together using page->lru |
| 750 | * 3. each sub-page is linked to the first page using page->first_page |
| 751 | * |
| 752 | * For each size class, First/Head pages are linked together using |
| 753 | * page->lru. Also, we set PG_private to identify the first page |
| 754 | * (i.e. no other sub-page has this flag set) and PG_private_2 to |
| 755 | * identify the last page. |
| 756 | */ |
| 757 | error = -ENOMEM; |
Minchan Kim | 2e3b615 | 2012-05-03 15:40:39 +0900 | [diff] [blame] | 758 | for (i = 0; i < class->pages_per_zspage; i++) { |
Seth Jennings | b4b700c | 2012-06-13 16:03:42 -0500 | [diff] [blame] | 759 | struct page *page; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 760 | |
| 761 | page = alloc_page(flags); |
| 762 | if (!page) |
| 763 | goto cleanup; |
| 764 | |
| 765 | INIT_LIST_HEAD(&page->lru); |
| 766 | if (i == 0) { /* first page */ |
Minchan Kim | a27545bf | 2012-04-25 15:23:09 +0900 | [diff] [blame] | 767 | SetPagePrivate(page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 768 | set_page_private(page, 0); |
| 769 | first_page = page; |
| 770 | first_page->inuse = 0; |
| 771 | } |
| 772 | if (i == 1) |
Sunghan Suh | e842b97 | 2013-07-12 16:08:13 +0900 | [diff] [blame] | 773 | set_page_private(first_page, (unsigned long)page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 774 | if (i >= 1) |
| 775 | page->first_page = first_page; |
| 776 | if (i >= 2) |
| 777 | list_add(&page->lru, &prev_page->lru); |
Minchan Kim | 2e3b615 | 2012-05-03 15:40:39 +0900 | [diff] [blame] | 778 | if (i == class->pages_per_zspage - 1) /* last page */ |
Minchan Kim | a27545bf | 2012-04-25 15:23:09 +0900 | [diff] [blame] | 779 | SetPagePrivate2(page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 780 | prev_page = page; |
| 781 | } |
| 782 | |
| 783 | init_zspage(first_page, class); |
| 784 | |
| 785 | first_page->freelist = obj_location_to_handle(first_page, 0); |
| 786 | /* Maximum number of objects we can store in this zspage */ |
Minchan Kim | 2e3b615 | 2012-05-03 15:40:39 +0900 | [diff] [blame] | 787 | first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 788 | |
| 789 | error = 0; /* Success */ |
| 790 | |
| 791 | cleanup: |
| 792 | if (unlikely(error) && first_page) { |
| 793 | free_zspage(first_page); |
| 794 | first_page = NULL; |
| 795 | } |
| 796 | |
| 797 | return first_page; |
| 798 | } |
| 799 | |
| 800 | static struct page *find_get_zspage(struct size_class *class) |
| 801 | { |
| 802 | int i; |
| 803 | struct page *page; |
| 804 | |
| 805 | for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) { |
| 806 | page = class->fullness_list[i]; |
| 807 | if (page) |
| 808 | break; |
| 809 | } |
| 810 | |
| 811 | return page; |
| 812 | } |
| 813 | |
Minchan Kim | 1b945ae | 2013-12-11 11:04:36 +0900 | [diff] [blame] | 814 | #ifdef CONFIG_PGTABLE_MAPPING |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 815 | static inline int __zs_cpu_up(struct mapping_area *area) |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 816 | { |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 817 | /* |
| 818 | * Make sure we don't leak memory if a cpu UP notification |
| 819 | * and zs_init() race and both call zs_cpu_up() on the same cpu |
| 820 | */ |
| 821 | if (area->vm) |
| 822 | return 0; |
| 823 | area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL); |
| 824 | if (!area->vm) |
| 825 | return -ENOMEM; |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | static inline void __zs_cpu_down(struct mapping_area *area) |
| 830 | { |
| 831 | if (area->vm) |
| 832 | free_vm_area(area->vm); |
| 833 | area->vm = NULL; |
| 834 | } |
| 835 | |
| 836 | static inline void *__zs_map_object(struct mapping_area *area, |
| 837 | struct page *pages[2], int off, int size) |
| 838 | { |
WANG Chao | f6f8ed4 | 2014-08-06 16:06:58 -0700 | [diff] [blame] | 839 | BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages)); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 840 | area->vm_addr = area->vm->addr; |
| 841 | return area->vm_addr + off; |
| 842 | } |
| 843 | |
| 844 | static inline void __zs_unmap_object(struct mapping_area *area, |
| 845 | struct page *pages[2], int off, int size) |
| 846 | { |
| 847 | unsigned long addr = (unsigned long)area->vm_addr; |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 848 | |
Joerg Roedel | d95abbb | 2013-03-27 01:43:14 +0100 | [diff] [blame] | 849 | unmap_kernel_range(addr, PAGE_SIZE * 2); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 850 | } |
| 851 | |
Minchan Kim | 1b945ae | 2013-12-11 11:04:36 +0900 | [diff] [blame] | 852 | #else /* CONFIG_PGTABLE_MAPPING */ |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 853 | |
| 854 | static inline int __zs_cpu_up(struct mapping_area *area) |
| 855 | { |
| 856 | /* |
| 857 | * Make sure we don't leak memory if a cpu UP notification |
| 858 | * and zs_init() race and both call zs_cpu_up() on the same cpu |
| 859 | */ |
| 860 | if (area->vm_buf) |
| 861 | return 0; |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 862 | area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 863 | if (!area->vm_buf) |
| 864 | return -ENOMEM; |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | static inline void __zs_cpu_down(struct mapping_area *area) |
| 869 | { |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 870 | kfree(area->vm_buf); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 871 | area->vm_buf = NULL; |
| 872 | } |
| 873 | |
| 874 | static void *__zs_map_object(struct mapping_area *area, |
| 875 | struct page *pages[2], int off, int size) |
| 876 | { |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 877 | int sizes[2]; |
| 878 | void *addr; |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 879 | char *buf = area->vm_buf; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 880 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 881 | /* disable page faults to match kmap_atomic() return conditions */ |
| 882 | pagefault_disable(); |
| 883 | |
| 884 | /* no read fastpath */ |
| 885 | if (area->vm_mm == ZS_MM_WO) |
| 886 | goto out; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 887 | |
| 888 | sizes[0] = PAGE_SIZE - off; |
| 889 | sizes[1] = size - sizes[0]; |
| 890 | |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 891 | /* copy object to per-cpu buffer */ |
| 892 | addr = kmap_atomic(pages[0]); |
| 893 | memcpy(buf, addr + off, sizes[0]); |
| 894 | kunmap_atomic(addr); |
| 895 | addr = kmap_atomic(pages[1]); |
| 896 | memcpy(buf + sizes[0], addr, sizes[1]); |
| 897 | kunmap_atomic(addr); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 898 | out: |
| 899 | return area->vm_buf; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 900 | } |
| 901 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 902 | static void __zs_unmap_object(struct mapping_area *area, |
| 903 | struct page *pages[2], int off, int size) |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 904 | { |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 905 | int sizes[2]; |
| 906 | void *addr; |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 907 | char *buf; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 908 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 909 | /* no write fastpath */ |
| 910 | if (area->vm_mm == ZS_MM_RO) |
| 911 | goto out; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 912 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 913 | buf = area->vm_buf + ZS_HANDLE_SIZE; |
| 914 | size -= ZS_HANDLE_SIZE; |
| 915 | off += ZS_HANDLE_SIZE; |
| 916 | |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 917 | sizes[0] = PAGE_SIZE - off; |
| 918 | sizes[1] = size - sizes[0]; |
| 919 | |
| 920 | /* copy per-cpu buffer to object */ |
| 921 | addr = kmap_atomic(pages[0]); |
| 922 | memcpy(addr + off, buf, sizes[0]); |
| 923 | kunmap_atomic(addr); |
| 924 | addr = kmap_atomic(pages[1]); |
| 925 | memcpy(addr, buf + sizes[0], sizes[1]); |
| 926 | kunmap_atomic(addr); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 927 | |
| 928 | out: |
| 929 | /* enable page faults to match kunmap_atomic() return conditions */ |
| 930 | pagefault_enable(); |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 931 | } |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 932 | |
Minchan Kim | 1b945ae | 2013-12-11 11:04:36 +0900 | [diff] [blame] | 933 | #endif /* CONFIG_PGTABLE_MAPPING */ |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 934 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 935 | static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action, |
| 936 | void *pcpu) |
| 937 | { |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 938 | int ret, cpu = (long)pcpu; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 939 | struct mapping_area *area; |
| 940 | |
| 941 | switch (action) { |
| 942 | case CPU_UP_PREPARE: |
| 943 | area = &per_cpu(zs_map_area, cpu); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 944 | ret = __zs_cpu_up(area); |
| 945 | if (ret) |
| 946 | return notifier_from_errno(ret); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 947 | break; |
| 948 | case CPU_DEAD: |
| 949 | case CPU_UP_CANCELED: |
| 950 | area = &per_cpu(zs_map_area, cpu); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 951 | __zs_cpu_down(area); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 952 | break; |
| 953 | } |
| 954 | |
| 955 | return NOTIFY_OK; |
| 956 | } |
| 957 | |
| 958 | static struct notifier_block zs_cpu_nb = { |
| 959 | .notifier_call = zs_cpu_notifier |
| 960 | }; |
| 961 | |
Sergey Senozhatsky | b1b00a5 | 2014-12-12 16:56:56 -0800 | [diff] [blame] | 962 | static int zs_register_cpu_notifier(void) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 963 | { |
Sergey Senozhatsky | b1b00a5 | 2014-12-12 16:56:56 -0800 | [diff] [blame] | 964 | int cpu, uninitialized_var(ret); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 965 | |
Srivatsa S. Bhat | f0e71fc | 2014-03-11 02:09:59 +0530 | [diff] [blame] | 966 | cpu_notifier_register_begin(); |
| 967 | |
| 968 | __register_cpu_notifier(&zs_cpu_nb); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 969 | for_each_online_cpu(cpu) { |
| 970 | ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu); |
Sergey Senozhatsky | b1b00a5 | 2014-12-12 16:56:56 -0800 | [diff] [blame] | 971 | if (notifier_to_errno(ret)) |
| 972 | break; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 973 | } |
Srivatsa S. Bhat | f0e71fc | 2014-03-11 02:09:59 +0530 | [diff] [blame] | 974 | |
| 975 | cpu_notifier_register_done(); |
Sergey Senozhatsky | b1b00a5 | 2014-12-12 16:56:56 -0800 | [diff] [blame] | 976 | return notifier_to_errno(ret); |
| 977 | } |
| 978 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 979 | static void zs_unregister_cpu_notifier(void) |
| 980 | { |
| 981 | int cpu; |
| 982 | |
| 983 | cpu_notifier_register_begin(); |
| 984 | |
| 985 | for_each_online_cpu(cpu) |
| 986 | zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu); |
| 987 | __unregister_cpu_notifier(&zs_cpu_nb); |
| 988 | |
| 989 | cpu_notifier_register_done(); |
| 990 | } |
| 991 | |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 992 | static void init_zs_size_classes(void) |
| 993 | { |
| 994 | int nr; |
| 995 | |
| 996 | nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1; |
| 997 | if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA) |
| 998 | nr += 1; |
| 999 | |
| 1000 | zs_size_classes = nr; |
| 1001 | } |
| 1002 | |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1003 | static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage) |
| 1004 | { |
| 1005 | return pages_per_zspage * PAGE_SIZE / size; |
| 1006 | } |
| 1007 | |
| 1008 | static bool can_merge(struct size_class *prev, int size, int pages_per_zspage) |
| 1009 | { |
| 1010 | if (prev->pages_per_zspage != pages_per_zspage) |
| 1011 | return false; |
| 1012 | |
| 1013 | if (get_maxobj_per_zspage(prev->size, prev->pages_per_zspage) |
| 1014 | != get_maxobj_per_zspage(size, pages_per_zspage)) |
| 1015 | return false; |
| 1016 | |
| 1017 | return true; |
| 1018 | } |
| 1019 | |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 1020 | #ifdef CONFIG_ZSMALLOC_STAT |
| 1021 | |
| 1022 | static inline void zs_stat_inc(struct size_class *class, |
| 1023 | enum zs_stat_type type, unsigned long cnt) |
| 1024 | { |
| 1025 | class->stats.objs[type] += cnt; |
| 1026 | } |
| 1027 | |
| 1028 | static inline void zs_stat_dec(struct size_class *class, |
| 1029 | enum zs_stat_type type, unsigned long cnt) |
| 1030 | { |
| 1031 | class->stats.objs[type] -= cnt; |
| 1032 | } |
| 1033 | |
| 1034 | static inline unsigned long zs_stat_get(struct size_class *class, |
| 1035 | enum zs_stat_type type) |
| 1036 | { |
| 1037 | return class->stats.objs[type]; |
| 1038 | } |
| 1039 | |
| 1040 | static int __init zs_stat_init(void) |
| 1041 | { |
| 1042 | if (!debugfs_initialized()) |
| 1043 | return -ENODEV; |
| 1044 | |
| 1045 | zs_stat_root = debugfs_create_dir("zsmalloc", NULL); |
| 1046 | if (!zs_stat_root) |
| 1047 | return -ENOMEM; |
| 1048 | |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | static void __exit zs_stat_exit(void) |
| 1053 | { |
| 1054 | debugfs_remove_recursive(zs_stat_root); |
| 1055 | } |
| 1056 | |
| 1057 | static int zs_stats_size_show(struct seq_file *s, void *v) |
| 1058 | { |
| 1059 | int i; |
| 1060 | struct zs_pool *pool = s->private; |
| 1061 | struct size_class *class; |
| 1062 | int objs_per_zspage; |
| 1063 | unsigned long obj_allocated, obj_used, pages_used; |
| 1064 | unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0; |
| 1065 | |
| 1066 | seq_printf(s, " %5s %5s %13s %10s %10s\n", "class", "size", |
| 1067 | "obj_allocated", "obj_used", "pages_used"); |
| 1068 | |
| 1069 | for (i = 0; i < zs_size_classes; i++) { |
| 1070 | class = pool->size_class[i]; |
| 1071 | |
| 1072 | if (class->index != i) |
| 1073 | continue; |
| 1074 | |
| 1075 | spin_lock(&class->lock); |
| 1076 | obj_allocated = zs_stat_get(class, OBJ_ALLOCATED); |
| 1077 | obj_used = zs_stat_get(class, OBJ_USED); |
| 1078 | spin_unlock(&class->lock); |
| 1079 | |
| 1080 | objs_per_zspage = get_maxobj_per_zspage(class->size, |
| 1081 | class->pages_per_zspage); |
| 1082 | pages_used = obj_allocated / objs_per_zspage * |
| 1083 | class->pages_per_zspage; |
| 1084 | |
| 1085 | seq_printf(s, " %5u %5u %10lu %10lu %10lu\n", i, |
| 1086 | class->size, obj_allocated, obj_used, pages_used); |
| 1087 | |
| 1088 | total_objs += obj_allocated; |
| 1089 | total_used_objs += obj_used; |
| 1090 | total_pages += pages_used; |
| 1091 | } |
| 1092 | |
| 1093 | seq_puts(s, "\n"); |
| 1094 | seq_printf(s, " %5s %5s %10lu %10lu %10lu\n", "Total", "", |
| 1095 | total_objs, total_used_objs, total_pages); |
| 1096 | |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | static int zs_stats_size_open(struct inode *inode, struct file *file) |
| 1101 | { |
| 1102 | return single_open(file, zs_stats_size_show, inode->i_private); |
| 1103 | } |
| 1104 | |
| 1105 | static const struct file_operations zs_stat_size_ops = { |
| 1106 | .open = zs_stats_size_open, |
| 1107 | .read = seq_read, |
| 1108 | .llseek = seq_lseek, |
| 1109 | .release = single_release, |
| 1110 | }; |
| 1111 | |
| 1112 | static int zs_pool_stat_create(char *name, struct zs_pool *pool) |
| 1113 | { |
| 1114 | struct dentry *entry; |
| 1115 | |
| 1116 | if (!zs_stat_root) |
| 1117 | return -ENODEV; |
| 1118 | |
| 1119 | entry = debugfs_create_dir(name, zs_stat_root); |
| 1120 | if (!entry) { |
| 1121 | pr_warn("debugfs dir <%s> creation failed\n", name); |
| 1122 | return -ENOMEM; |
| 1123 | } |
| 1124 | pool->stat_dentry = entry; |
| 1125 | |
| 1126 | entry = debugfs_create_file("obj_in_classes", S_IFREG | S_IRUGO, |
| 1127 | pool->stat_dentry, pool, &zs_stat_size_ops); |
| 1128 | if (!entry) { |
| 1129 | pr_warn("%s: debugfs file entry <%s> creation failed\n", |
| 1130 | name, "obj_in_classes"); |
| 1131 | return -ENOMEM; |
| 1132 | } |
| 1133 | |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | static void zs_pool_stat_destroy(struct zs_pool *pool) |
| 1138 | { |
| 1139 | debugfs_remove_recursive(pool->stat_dentry); |
| 1140 | } |
| 1141 | |
| 1142 | #else /* CONFIG_ZSMALLOC_STAT */ |
| 1143 | |
| 1144 | static inline void zs_stat_inc(struct size_class *class, |
| 1145 | enum zs_stat_type type, unsigned long cnt) |
| 1146 | { |
| 1147 | } |
| 1148 | |
| 1149 | static inline void zs_stat_dec(struct size_class *class, |
| 1150 | enum zs_stat_type type, unsigned long cnt) |
| 1151 | { |
| 1152 | } |
| 1153 | |
| 1154 | static inline unsigned long zs_stat_get(struct size_class *class, |
| 1155 | enum zs_stat_type type) |
| 1156 | { |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
| 1160 | static int __init zs_stat_init(void) |
| 1161 | { |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | static void __exit zs_stat_exit(void) |
| 1166 | { |
| 1167 | } |
| 1168 | |
| 1169 | static inline int zs_pool_stat_create(char *name, struct zs_pool *pool) |
| 1170 | { |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
| 1174 | static inline void zs_pool_stat_destroy(struct zs_pool *pool) |
| 1175 | { |
| 1176 | } |
| 1177 | |
| 1178 | #endif |
| 1179 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1180 | unsigned long zs_get_total_pages(struct zs_pool *pool) |
| 1181 | { |
| 1182 | return atomic_long_read(&pool->pages_allocated); |
| 1183 | } |
| 1184 | EXPORT_SYMBOL_GPL(zs_get_total_pages); |
| 1185 | |
| 1186 | /** |
| 1187 | * zs_map_object - get address of allocated object from handle. |
| 1188 | * @pool: pool from which the object was allocated |
| 1189 | * @handle: handle returned from zs_malloc |
| 1190 | * |
| 1191 | * Before using an object allocated from zs_malloc, it must be mapped using |
| 1192 | * this function. When done with the object, it must be unmapped using |
| 1193 | * zs_unmap_object. |
| 1194 | * |
| 1195 | * Only one object can be mapped per cpu at a time. There is no protection |
| 1196 | * against nested mappings. |
| 1197 | * |
| 1198 | * This function returns with preemption and page faults disabled. |
| 1199 | */ |
| 1200 | void *zs_map_object(struct zs_pool *pool, unsigned long handle, |
| 1201 | enum zs_mapmode mm) |
| 1202 | { |
| 1203 | struct page *page; |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1204 | unsigned long obj, obj_idx, off; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1205 | |
| 1206 | unsigned int class_idx; |
| 1207 | enum fullness_group fg; |
| 1208 | struct size_class *class; |
| 1209 | struct mapping_area *area; |
| 1210 | struct page *pages[2]; |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1211 | void *ret; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1212 | |
| 1213 | BUG_ON(!handle); |
| 1214 | |
| 1215 | /* |
| 1216 | * Because we use per-cpu mapping areas shared among the |
| 1217 | * pools/users, we can't allow mapping in interrupt context |
| 1218 | * because it can corrupt another users mappings. |
| 1219 | */ |
| 1220 | BUG_ON(in_interrupt()); |
| 1221 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1222 | obj = handle_to_obj(handle); |
| 1223 | obj_to_location(obj, &page, &obj_idx); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1224 | get_zspage_mapping(get_first_page(page), &class_idx, &fg); |
| 1225 | class = pool->size_class[class_idx]; |
| 1226 | off = obj_idx_to_offset(page, obj_idx, class->size); |
| 1227 | |
| 1228 | area = &get_cpu_var(zs_map_area); |
| 1229 | area->vm_mm = mm; |
| 1230 | if (off + class->size <= PAGE_SIZE) { |
| 1231 | /* this object is contained entirely within a page */ |
| 1232 | area->vm_addr = kmap_atomic(page); |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1233 | ret = area->vm_addr + off; |
| 1234 | goto out; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | /* this object spans two pages */ |
| 1238 | pages[0] = page; |
| 1239 | pages[1] = get_next_page(page); |
| 1240 | BUG_ON(!pages[1]); |
| 1241 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1242 | ret = __zs_map_object(area, pages, off, class->size); |
| 1243 | out: |
| 1244 | return ret + ZS_HANDLE_SIZE; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1245 | } |
| 1246 | EXPORT_SYMBOL_GPL(zs_map_object); |
| 1247 | |
| 1248 | void zs_unmap_object(struct zs_pool *pool, unsigned long handle) |
| 1249 | { |
| 1250 | struct page *page; |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1251 | unsigned long obj, obj_idx, off; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1252 | |
| 1253 | unsigned int class_idx; |
| 1254 | enum fullness_group fg; |
| 1255 | struct size_class *class; |
| 1256 | struct mapping_area *area; |
| 1257 | |
| 1258 | BUG_ON(!handle); |
| 1259 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1260 | obj = handle_to_obj(handle); |
| 1261 | obj_to_location(obj, &page, &obj_idx); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1262 | get_zspage_mapping(get_first_page(page), &class_idx, &fg); |
| 1263 | class = pool->size_class[class_idx]; |
| 1264 | off = obj_idx_to_offset(page, obj_idx, class->size); |
| 1265 | |
| 1266 | area = this_cpu_ptr(&zs_map_area); |
| 1267 | if (off + class->size <= PAGE_SIZE) |
| 1268 | kunmap_atomic(area->vm_addr); |
| 1269 | else { |
| 1270 | struct page *pages[2]; |
| 1271 | |
| 1272 | pages[0] = page; |
| 1273 | pages[1] = get_next_page(page); |
| 1274 | BUG_ON(!pages[1]); |
| 1275 | |
| 1276 | __zs_unmap_object(area, pages, off, class->size); |
| 1277 | } |
| 1278 | put_cpu_var(zs_map_area); |
| 1279 | } |
| 1280 | EXPORT_SYMBOL_GPL(zs_unmap_object); |
| 1281 | |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame^] | 1282 | static unsigned long obj_malloc(struct page *first_page, |
| 1283 | struct size_class *class, unsigned long handle) |
| 1284 | { |
| 1285 | unsigned long obj; |
| 1286 | struct link_free *link; |
| 1287 | |
| 1288 | struct page *m_page; |
| 1289 | unsigned long m_objidx, m_offset; |
| 1290 | void *vaddr; |
| 1291 | |
| 1292 | obj = (unsigned long)first_page->freelist; |
| 1293 | obj_to_location(obj, &m_page, &m_objidx); |
| 1294 | m_offset = obj_idx_to_offset(m_page, m_objidx, class->size); |
| 1295 | |
| 1296 | vaddr = kmap_atomic(m_page); |
| 1297 | link = (struct link_free *)vaddr + m_offset / sizeof(*link); |
| 1298 | first_page->freelist = link->next; |
| 1299 | /* record handle in the header of allocated chunk */ |
| 1300 | link->handle = handle; |
| 1301 | kunmap_atomic(vaddr); |
| 1302 | first_page->inuse++; |
| 1303 | zs_stat_inc(class, OBJ_USED, 1); |
| 1304 | |
| 1305 | return obj; |
| 1306 | } |
| 1307 | |
| 1308 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1309 | /** |
| 1310 | * zs_malloc - Allocate block of given size from pool. |
| 1311 | * @pool: pool to allocate from |
| 1312 | * @size: size of block to allocate |
| 1313 | * |
| 1314 | * On success, handle to the allocated object is returned, |
| 1315 | * otherwise 0. |
| 1316 | * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail. |
| 1317 | */ |
| 1318 | unsigned long zs_malloc(struct zs_pool *pool, size_t size) |
| 1319 | { |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1320 | unsigned long handle, obj; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1321 | struct size_class *class; |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame^] | 1322 | struct page *first_page; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1323 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1324 | if (unlikely(!size || (size + ZS_HANDLE_SIZE) > ZS_MAX_ALLOC_SIZE)) |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1325 | return 0; |
| 1326 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1327 | handle = alloc_handle(pool); |
| 1328 | if (!handle) |
| 1329 | return 0; |
| 1330 | |
| 1331 | /* extra space in chunk to keep the handle */ |
| 1332 | size += ZS_HANDLE_SIZE; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1333 | class = pool->size_class[get_size_class_index(size)]; |
| 1334 | |
| 1335 | spin_lock(&class->lock); |
| 1336 | first_page = find_get_zspage(class); |
| 1337 | |
| 1338 | if (!first_page) { |
| 1339 | spin_unlock(&class->lock); |
| 1340 | first_page = alloc_zspage(class, pool->flags); |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1341 | if (unlikely(!first_page)) { |
| 1342 | free_handle(pool, handle); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1343 | return 0; |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1344 | } |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1345 | |
| 1346 | set_zspage_mapping(first_page, class->index, ZS_EMPTY); |
| 1347 | atomic_long_add(class->pages_per_zspage, |
| 1348 | &pool->pages_allocated); |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 1349 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1350 | spin_lock(&class->lock); |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 1351 | zs_stat_inc(class, OBJ_ALLOCATED, get_maxobj_per_zspage( |
| 1352 | class->size, class->pages_per_zspage)); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1353 | } |
| 1354 | |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame^] | 1355 | obj = obj_malloc(first_page, class, handle); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1356 | /* Now move the zspage to another fullness group, if required */ |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame^] | 1357 | fix_fullness_group(class, first_page); |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1358 | record_obj(handle, obj); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1359 | spin_unlock(&class->lock); |
| 1360 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1361 | return handle; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1362 | } |
| 1363 | EXPORT_SYMBOL_GPL(zs_malloc); |
| 1364 | |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame^] | 1365 | static void obj_free(struct zs_pool *pool, struct size_class *class, |
| 1366 | unsigned long obj) |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1367 | { |
| 1368 | struct link_free *link; |
| 1369 | struct page *first_page, *f_page; |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame^] | 1370 | unsigned long f_objidx, f_offset; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1371 | void *vaddr; |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame^] | 1372 | int class_idx; |
| 1373 | enum fullness_group fullness; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1374 | |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame^] | 1375 | BUG_ON(!obj); |
| 1376 | |
| 1377 | obj_to_location(obj, &f_page, &f_objidx); |
| 1378 | first_page = get_first_page(f_page); |
| 1379 | |
| 1380 | get_zspage_mapping(first_page, &class_idx, &fullness); |
| 1381 | f_offset = obj_idx_to_offset(f_page, f_objidx, class->size); |
| 1382 | |
| 1383 | vaddr = kmap_atomic(f_page); |
| 1384 | |
| 1385 | /* Insert this object in containing zspage's freelist */ |
| 1386 | link = (struct link_free *)(vaddr + f_offset); |
| 1387 | link->next = first_page->freelist; |
| 1388 | kunmap_atomic(vaddr); |
| 1389 | first_page->freelist = (void *)obj; |
| 1390 | first_page->inuse--; |
| 1391 | zs_stat_dec(class, OBJ_USED, 1); |
| 1392 | } |
| 1393 | |
| 1394 | void zs_free(struct zs_pool *pool, unsigned long handle) |
| 1395 | { |
| 1396 | struct page *first_page, *f_page; |
| 1397 | unsigned long obj, f_objidx; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1398 | int class_idx; |
| 1399 | struct size_class *class; |
| 1400 | enum fullness_group fullness; |
| 1401 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1402 | if (unlikely(!handle)) |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1403 | return; |
| 1404 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1405 | obj = handle_to_obj(handle); |
| 1406 | free_handle(pool, handle); |
| 1407 | obj_to_location(obj, &f_page, &f_objidx); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1408 | first_page = get_first_page(f_page); |
| 1409 | |
| 1410 | get_zspage_mapping(first_page, &class_idx, &fullness); |
| 1411 | class = pool->size_class[class_idx]; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1412 | |
| 1413 | spin_lock(&class->lock); |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame^] | 1414 | obj_free(pool, class, obj); |
| 1415 | fullness = fix_fullness_group(class, first_page); |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 1416 | if (fullness == ZS_EMPTY) |
| 1417 | zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage( |
| 1418 | class->size, class->pages_per_zspage)); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1419 | spin_unlock(&class->lock); |
| 1420 | |
| 1421 | if (fullness == ZS_EMPTY) { |
| 1422 | atomic_long_sub(class->pages_per_zspage, |
| 1423 | &pool->pages_allocated); |
| 1424 | free_zspage(first_page); |
| 1425 | } |
| 1426 | } |
| 1427 | EXPORT_SYMBOL_GPL(zs_free); |
| 1428 | |
Davidlohr Bueso | 4bbc0bc | 2013-01-04 12:14:00 -0800 | [diff] [blame] | 1429 | /** |
| 1430 | * zs_create_pool - Creates an allocation pool to work from. |
Seth Jennings | 0d145a5 | 2013-01-30 09:36:52 -0600 | [diff] [blame] | 1431 | * @flags: allocation flags used to allocate pool metadata |
Davidlohr Bueso | 4bbc0bc | 2013-01-04 12:14:00 -0800 | [diff] [blame] | 1432 | * |
| 1433 | * This function must be called before anything when using |
| 1434 | * the zsmalloc allocator. |
| 1435 | * |
| 1436 | * On success, a pointer to the newly created pool is returned, |
| 1437 | * otherwise NULL. |
| 1438 | */ |
Ganesh Mahendran | 3eba0c6 | 2015-02-12 15:00:51 -0800 | [diff] [blame] | 1439 | struct zs_pool *zs_create_pool(char *name, gfp_t flags) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1440 | { |
Ganesh Mahendran | 1813665 | 2014-12-12 16:57:10 -0800 | [diff] [blame] | 1441 | int i; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1442 | struct zs_pool *pool; |
Ganesh Mahendran | df8b5bb | 2014-12-12 16:57:07 -0800 | [diff] [blame] | 1443 | struct size_class *prev_class = NULL; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1444 | |
Ganesh Mahendran | 1813665 | 2014-12-12 16:57:10 -0800 | [diff] [blame] | 1445 | pool = kzalloc(sizeof(*pool), GFP_KERNEL); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1446 | if (!pool) |
| 1447 | return NULL; |
| 1448 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1449 | pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *), |
| 1450 | GFP_KERNEL); |
| 1451 | if (!pool->size_class) { |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 1452 | kfree(pool); |
| 1453 | return NULL; |
| 1454 | } |
| 1455 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1456 | pool->name = kstrdup(name, GFP_KERNEL); |
| 1457 | if (!pool->name) |
| 1458 | goto err; |
| 1459 | |
| 1460 | if (create_handle_cache(pool)) |
| 1461 | goto err; |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 1462 | |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1463 | /* |
| 1464 | * Iterate reversly, because, size of size_class that we want to use |
| 1465 | * for merging should be larger or equal to current size. |
| 1466 | */ |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 1467 | for (i = zs_size_classes - 1; i >= 0; i--) { |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1468 | int size; |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1469 | int pages_per_zspage; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1470 | struct size_class *class; |
| 1471 | |
| 1472 | size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA; |
| 1473 | if (size > ZS_MAX_ALLOC_SIZE) |
| 1474 | size = ZS_MAX_ALLOC_SIZE; |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1475 | pages_per_zspage = get_pages_per_zspage(size); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1476 | |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1477 | /* |
| 1478 | * size_class is used for normal zsmalloc operation such |
| 1479 | * as alloc/free for that size. Although it is natural that we |
| 1480 | * have one size_class for each size, there is a chance that we |
| 1481 | * can get more memory utilization if we use one size_class for |
| 1482 | * many different sizes whose size_class have same |
| 1483 | * characteristics. So, we makes size_class point to |
| 1484 | * previous size_class if possible. |
| 1485 | */ |
Ganesh Mahendran | df8b5bb | 2014-12-12 16:57:07 -0800 | [diff] [blame] | 1486 | if (prev_class) { |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1487 | if (can_merge(prev_class, size, pages_per_zspage)) { |
| 1488 | pool->size_class[i] = prev_class; |
| 1489 | continue; |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | class = kzalloc(sizeof(struct size_class), GFP_KERNEL); |
| 1494 | if (!class) |
| 1495 | goto err; |
| 1496 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1497 | class->size = size; |
| 1498 | class->index = i; |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1499 | class->pages_per_zspage = pages_per_zspage; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1500 | spin_lock_init(&class->lock); |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1501 | pool->size_class[i] = class; |
Ganesh Mahendran | df8b5bb | 2014-12-12 16:57:07 -0800 | [diff] [blame] | 1502 | |
| 1503 | prev_class = class; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1504 | } |
| 1505 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1506 | pool->flags = flags; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1507 | |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 1508 | if (zs_pool_stat_create(name, pool)) |
| 1509 | goto err; |
| 1510 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1511 | return pool; |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1512 | |
| 1513 | err: |
| 1514 | zs_destroy_pool(pool); |
| 1515 | return NULL; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1516 | } |
| 1517 | EXPORT_SYMBOL_GPL(zs_create_pool); |
| 1518 | |
| 1519 | void zs_destroy_pool(struct zs_pool *pool) |
| 1520 | { |
| 1521 | int i; |
| 1522 | |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 1523 | zs_pool_stat_destroy(pool); |
| 1524 | |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 1525 | for (i = 0; i < zs_size_classes; i++) { |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1526 | int fg; |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1527 | struct size_class *class = pool->size_class[i]; |
| 1528 | |
| 1529 | if (!class) |
| 1530 | continue; |
| 1531 | |
| 1532 | if (class->index != i) |
| 1533 | continue; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1534 | |
| 1535 | for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) { |
| 1536 | if (class->fullness_list[fg]) { |
Marlies Ruck | 93ad5ab | 2013-05-15 16:56:49 -0400 | [diff] [blame] | 1537 | 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] | 1538 | class->size, fg); |
| 1539 | } |
| 1540 | } |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1541 | kfree(class); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1542 | } |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 1543 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1544 | destroy_handle_cache(pool); |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 1545 | kfree(pool->size_class); |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 1546 | kfree(pool->name); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1547 | kfree(pool); |
| 1548 | } |
| 1549 | EXPORT_SYMBOL_GPL(zs_destroy_pool); |
| 1550 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1551 | static int __init zs_init(void) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1552 | { |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1553 | int ret = zs_register_cpu_notifier(); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1554 | |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 1555 | if (ret) |
| 1556 | goto notifier_fail; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1557 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1558 | init_zs_size_classes(); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1559 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1560 | #ifdef CONFIG_ZPOOL |
| 1561 | zpool_register_driver(&zs_zpool_driver); |
| 1562 | #endif |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 1563 | |
| 1564 | ret = zs_stat_init(); |
| 1565 | if (ret) { |
| 1566 | pr_err("zs stat initialization failed\n"); |
| 1567 | goto stat_fail; |
| 1568 | } |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1569 | return 0; |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 1570 | |
| 1571 | stat_fail: |
| 1572 | #ifdef CONFIG_ZPOOL |
| 1573 | zpool_unregister_driver(&zs_zpool_driver); |
| 1574 | #endif |
| 1575 | notifier_fail: |
| 1576 | zs_unregister_cpu_notifier(); |
| 1577 | |
| 1578 | return ret; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1579 | } |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1580 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1581 | static void __exit zs_exit(void) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1582 | { |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1583 | #ifdef CONFIG_ZPOOL |
| 1584 | zpool_unregister_driver(&zs_zpool_driver); |
| 1585 | #endif |
| 1586 | zs_unregister_cpu_notifier(); |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 1587 | |
| 1588 | zs_stat_exit(); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1589 | } |
Ben Hutchings | 069f101 | 2012-06-20 02:31:11 +0100 | [diff] [blame] | 1590 | |
| 1591 | module_init(zs_init); |
| 1592 | module_exit(zs_exit); |
| 1593 | |
| 1594 | MODULE_LICENSE("Dual BSD/GPL"); |
| 1595 | MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); |