1 /* 2 * zsmalloc memory allocator 3 * 4 * Copyright (C) 2011 Nitin Gupta 5 * Copyright (C) 2012, 2013 Minchan Kim 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 14 /* 15 * Following is how we use various fields and flags of underlying 16 * struct page(s) to form a zspage. 17 * 18 * Usage of struct page fields: 19 * page->private: points to zspage 20 * page->index: links together all component pages of a zspage 21 * For the huge page, this is always 0, so we use this field 22 * to store handle. 23 * page->page_type: PGTY_zsmalloc, lower 24 bits locate the first object 24 * offset in a subpage of a zspage 25 * 26 * Usage of struct page flags: 27 * PG_private: identifies the first component page 28 * PG_owner_priv_1: identifies the huge component page 29 * 30 */ 31 32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 33 34 /* 35 * lock ordering: 36 * page_lock 37 * pool->migrate_lock 38 * class->lock 39 * zspage->lock 40 */ 41 42 #include <linux/module.h> 43 #include <linux/kernel.h> 44 #include <linux/sched.h> 45 #include <linux/bitops.h> 46 #include <linux/errno.h> 47 #include <linux/highmem.h> 48 #include <linux/string.h> 49 #include <linux/slab.h> 50 #include <linux/pgtable.h> 51 #include <asm/tlbflush.h> 52 #include <linux/cpumask.h> 53 #include <linux/cpu.h> 54 #include <linux/vmalloc.h> 55 #include <linux/preempt.h> 56 #include <linux/spinlock.h> 57 #include <linux/shrinker.h> 58 #include <linux/types.h> 59 #include <linux/debugfs.h> 60 #include <linux/zsmalloc.h> 61 #include <linux/zpool.h> 62 #include <linux/migrate.h> 63 #include <linux/wait.h> 64 #include <linux/pagemap.h> 65 #include <linux/fs.h> 66 #include <linux/local_lock.h> 67 68 #define ZSPAGE_MAGIC 0x58 69 70 /* 71 * This must be power of 2 and greater than or equal to sizeof(link_free). 72 * These two conditions ensure that any 'struct link_free' itself doesn't 73 * span more than 1 page which avoids complex case of mapping 2 pages simply 74 * to restore link_free pointer values. 75 */ 76 #define ZS_ALIGN 8 77 78 #define ZS_HANDLE_SIZE (sizeof(unsigned long)) 79 80 /* 81 * Object location (<PFN>, <obj_idx>) is encoded as 82 * a single (unsigned long) handle value. 83 * 84 * Note that object index <obj_idx> starts from 0. 85 * 86 * This is made more complicated by various memory models and PAE. 87 */ 88 89 #ifndef MAX_POSSIBLE_PHYSMEM_BITS 90 #ifdef MAX_PHYSMEM_BITS 91 #define MAX_POSSIBLE_PHYSMEM_BITS MAX_PHYSMEM_BITS 92 #else 93 /* 94 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just 95 * be PAGE_SHIFT 96 */ 97 #define MAX_POSSIBLE_PHYSMEM_BITS BITS_PER_LONG 98 #endif 99 #endif 100 101 #define _PFN_BITS (MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT) 102 103 /* 104 * Head in allocated object should have OBJ_ALLOCATED_TAG 105 * to identify the object was allocated or not. 106 * It's okay to add the status bit in the least bit because 107 * header keeps handle which is 4byte-aligned address so we 108 * have room for two bit at least. 109 */ 110 #define OBJ_ALLOCATED_TAG 1 111 112 #define OBJ_TAG_BITS 1 113 #define OBJ_TAG_MASK OBJ_ALLOCATED_TAG 114 115 #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS) 116 #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1) 117 118 #define HUGE_BITS 1 119 #define FULLNESS_BITS 4 120 #define CLASS_BITS 8 121 #define MAGIC_VAL_BITS 8 122 123 #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(CONFIG_ZSMALLOC_CHAIN_SIZE, UL)) 124 125 /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */ 126 #define ZS_MIN_ALLOC_SIZE \ 127 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS)) 128 /* each chunk includes extra space to keep handle */ 129 #define ZS_MAX_ALLOC_SIZE PAGE_SIZE 130 131 /* 132 * On systems with 4K page size, this gives 255 size classes! There is a 133 * trader-off here: 134 * - Large number of size classes is potentially wasteful as free page are 135 * spread across these classes 136 * - Small number of size classes causes large internal fragmentation 137 * - Probably its better to use specific size classes (empirically 138 * determined). NOTE: all those class sizes must be set as multiple of 139 * ZS_ALIGN to make sure link_free itself never has to span 2 pages. 140 * 141 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN 142 * (reason above) 143 */ 144 #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS) 145 #define ZS_SIZE_CLASSES (DIV_ROUND_UP(ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE, \ 146 ZS_SIZE_CLASS_DELTA) + 1) 147 148 /* 149 * Pages are distinguished by the ratio of used memory (that is the ratio 150 * of ->inuse objects to all objects that page can store). For example, 151 * INUSE_RATIO_10 means that the ratio of used objects is > 0% and <= 10%. 152 * 153 * The number of fullness groups is not random. It allows us to keep 154 * difference between the least busy page in the group (minimum permitted 155 * number of ->inuse objects) and the most busy page (maximum permitted 156 * number of ->inuse objects) at a reasonable value. 157 */ 158 enum fullness_group { 159 ZS_INUSE_RATIO_0, 160 ZS_INUSE_RATIO_10, 161 /* NOTE: 8 more fullness groups here */ 162 ZS_INUSE_RATIO_99 = 10, 163 ZS_INUSE_RATIO_100, 164 NR_FULLNESS_GROUPS, 165 }; 166 167 enum class_stat_type { 168 /* NOTE: stats for 12 fullness groups here: from inuse 0 to 100 */ 169 ZS_OBJS_ALLOCATED = NR_FULLNESS_GROUPS, 170 ZS_OBJS_INUSE, 171 NR_CLASS_STAT_TYPES, 172 }; 173 174 struct zs_size_stat { 175 unsigned long objs[NR_CLASS_STAT_TYPES]; 176 }; 177 178 #ifdef CONFIG_ZSMALLOC_STAT 179 static struct dentry *zs_stat_root; 180 #endif 181 182 static size_t huge_class_size; 183 184 struct size_class { 185 spinlock_t lock; 186 struct list_head fullness_list[NR_FULLNESS_GROUPS]; 187 /* 188 * Size of objects stored in this class. Must be multiple 189 * of ZS_ALIGN. 190 */ 191 int size; 192 int objs_per_zspage; 193 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */ 194 int pages_per_zspage; 195 196 unsigned int index; 197 struct zs_size_stat stats; 198 }; 199 200 /* 201 * Placed within free objects to form a singly linked list. 202 * For every zspage, zspage->freeobj gives head of this list. 203 * 204 * This must be power of 2 and less than or equal to ZS_ALIGN 205 */ 206 struct link_free { 207 union { 208 /* 209 * Free object index; 210 * It's valid for non-allocated object 211 */ 212 unsigned long next; 213 /* 214 * Handle of allocated object. 215 */ 216 unsigned long handle; 217 }; 218 }; 219 220 struct zs_pool { 221 const char *name; 222 223 struct size_class *size_class[ZS_SIZE_CLASSES]; 224 struct kmem_cache *handle_cachep; 225 struct kmem_cache *zspage_cachep; 226 227 atomic_long_t pages_allocated; 228 229 struct zs_pool_stats stats; 230 231 /* Compact classes */ 232 struct shrinker *shrinker; 233 234 #ifdef CONFIG_ZSMALLOC_STAT 235 struct dentry *stat_dentry; 236 #endif 237 #ifdef CONFIG_COMPACTION 238 struct work_struct free_work; 239 #endif 240 /* protect page/zspage migration */ 241 rwlock_t migrate_lock; 242 atomic_t compaction_in_progress; 243 }; 244 245 struct zspage { 246 struct { 247 unsigned int huge:HUGE_BITS; 248 unsigned int fullness:FULLNESS_BITS; 249 unsigned int class:CLASS_BITS + 1; 250 unsigned int magic:MAGIC_VAL_BITS; 251 }; 252 unsigned int inuse; 253 unsigned int freeobj; 254 struct page *first_page; 255 struct list_head list; /* fullness list */ 256 struct zs_pool *pool; 257 rwlock_t lock; 258 }; 259 260 struct mapping_area { 261 local_lock_t lock; 262 char *vm_buf; /* copy buffer for objects that span pages */ 263 char *vm_addr; /* address of kmap_atomic()'ed pages */ 264 enum zs_mapmode vm_mm; /* mapping mode */ 265 }; 266 267 /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */ 268 static void SetZsHugePage(struct zspage *zspage) 269 { 270 zspage->huge = 1; 271 } 272 273 static bool ZsHugePage(struct zspage *zspage) 274 { 275 return zspage->huge; 276 } 277 278 static void migrate_lock_init(struct zspage *zspage); 279 static void migrate_read_lock(struct zspage *zspage); 280 static void migrate_read_unlock(struct zspage *zspage); 281 static void migrate_write_lock(struct zspage *zspage); 282 static void migrate_write_unlock(struct zspage *zspage); 283 284 #ifdef CONFIG_COMPACTION 285 static void kick_deferred_free(struct zs_pool *pool); 286 static void init_deferred_free(struct zs_pool *pool); 287 static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage); 288 #else 289 static void kick_deferred_free(struct zs_pool *pool) {} 290 static void init_deferred_free(struct zs_pool *pool) {} 291 static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {} 292 #endif 293 294 static int create_cache(struct zs_pool *pool) 295 { 296 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE, 297 0, 0, NULL); 298 if (!pool->handle_cachep) 299 return 1; 300 301 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage), 302 0, 0, NULL); 303 if (!pool->zspage_cachep) { 304 kmem_cache_destroy(pool->handle_cachep); 305 pool->handle_cachep = NULL; 306 return 1; 307 } 308 309 return 0; 310 } 311 312 static void destroy_cache(struct zs_pool *pool) 313 { 314 kmem_cache_destroy(pool->handle_cachep); 315 kmem_cache_destroy(pool->zspage_cachep); 316 } 317 318 static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp) 319 { 320 return (unsigned long)kmem_cache_alloc(pool->handle_cachep, 321 gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE)); 322 } 323 324 static void cache_free_handle(struct zs_pool *pool, unsigned long handle) 325 { 326 kmem_cache_free(pool->handle_cachep, (void *)handle); 327 } 328 329 static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags) 330 { 331 return kmem_cache_zalloc(pool->zspage_cachep, 332 flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE)); 333 } 334 335 static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage) 336 { 337 kmem_cache_free(pool->zspage_cachep, zspage); 338 } 339 340 /* class->lock(which owns the handle) synchronizes races */ 341 static void record_obj(unsigned long handle, unsigned long obj) 342 { 343 *(unsigned long *)handle = obj; 344 } 345 346 /* zpool driver */ 347 348 #ifdef CONFIG_ZPOOL 349 350 static void *zs_zpool_create(const char *name, gfp_t gfp) 351 { 352 /* 353 * Ignore global gfp flags: zs_malloc() may be invoked from 354 * different contexts and its caller must provide a valid 355 * gfp mask. 356 */ 357 return zs_create_pool(name); 358 } 359 360 static void zs_zpool_destroy(void *pool) 361 { 362 zs_destroy_pool(pool); 363 } 364 365 static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp, 366 unsigned long *handle) 367 { 368 *handle = zs_malloc(pool, size, gfp); 369 370 if (IS_ERR_VALUE(*handle)) 371 return PTR_ERR((void *)*handle); 372 return 0; 373 } 374 static void zs_zpool_free(void *pool, unsigned long handle) 375 { 376 zs_free(pool, handle); 377 } 378 379 static void *zs_zpool_map(void *pool, unsigned long handle, 380 enum zpool_mapmode mm) 381 { 382 enum zs_mapmode zs_mm; 383 384 switch (mm) { 385 case ZPOOL_MM_RO: 386 zs_mm = ZS_MM_RO; 387 break; 388 case ZPOOL_MM_WO: 389 zs_mm = ZS_MM_WO; 390 break; 391 case ZPOOL_MM_RW: 392 default: 393 zs_mm = ZS_MM_RW; 394 break; 395 } 396 397 return zs_map_object(pool, handle, zs_mm); 398 } 399 static void zs_zpool_unmap(void *pool, unsigned long handle) 400 { 401 zs_unmap_object(pool, handle); 402 } 403 404 static u64 zs_zpool_total_pages(void *pool) 405 { 406 return zs_get_total_pages(pool); 407 } 408 409 static struct zpool_driver zs_zpool_driver = { 410 .type = "zsmalloc", 411 .owner = THIS_MODULE, 412 .create = zs_zpool_create, 413 .destroy = zs_zpool_destroy, 414 .malloc_support_movable = true, 415 .malloc = zs_zpool_malloc, 416 .free = zs_zpool_free, 417 .map = zs_zpool_map, 418 .unmap = zs_zpool_unmap, 419 .total_pages = zs_zpool_total_pages, 420 }; 421 422 MODULE_ALIAS("zpool-zsmalloc"); 423 #endif /* CONFIG_ZPOOL */ 424 425 /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */ 426 static DEFINE_PER_CPU(struct mapping_area, zs_map_area) = { 427 .lock = INIT_LOCAL_LOCK(lock), 428 }; 429 430 static __maybe_unused int is_first_page(struct page *page) 431 { 432 return PagePrivate(page); 433 } 434 435 /* Protected by class->lock */ 436 static inline int get_zspage_inuse(struct zspage *zspage) 437 { 438 return zspage->inuse; 439 } 440 441 442 static inline void mod_zspage_inuse(struct zspage *zspage, int val) 443 { 444 zspage->inuse += val; 445 } 446 447 static inline struct page *get_first_page(struct zspage *zspage) 448 { 449 struct page *first_page = zspage->first_page; 450 451 VM_BUG_ON_PAGE(!is_first_page(first_page), first_page); 452 return first_page; 453 } 454 455 #define FIRST_OBJ_PAGE_TYPE_MASK 0xffffff 456 457 static inline unsigned int get_first_obj_offset(struct page *page) 458 { 459 VM_WARN_ON_ONCE(!PageZsmalloc(page)); 460 return page->page_type & FIRST_OBJ_PAGE_TYPE_MASK; 461 } 462 463 static inline void set_first_obj_offset(struct page *page, unsigned int offset) 464 { 465 /* With 24 bits available, we can support offsets into 16 MiB pages. */ 466 BUILD_BUG_ON(PAGE_SIZE > SZ_16M); 467 VM_WARN_ON_ONCE(!PageZsmalloc(page)); 468 VM_WARN_ON_ONCE(offset & ~FIRST_OBJ_PAGE_TYPE_MASK); 469 page->page_type &= ~FIRST_OBJ_PAGE_TYPE_MASK; 470 page->page_type |= offset & FIRST_OBJ_PAGE_TYPE_MASK; 471 } 472 473 static inline unsigned int get_freeobj(struct zspage *zspage) 474 { 475 return zspage->freeobj; 476 } 477 478 static inline void set_freeobj(struct zspage *zspage, unsigned int obj) 479 { 480 zspage->freeobj = obj; 481 } 482 483 static struct size_class *zspage_class(struct zs_pool *pool, 484 struct zspage *zspage) 485 { 486 return pool->size_class[zspage->class]; 487 } 488 489 /* 490 * zsmalloc divides the pool into various size classes where each 491 * class maintains a list of zspages where each zspage is divided 492 * into equal sized chunks. Each allocation falls into one of these 493 * classes depending on its size. This function returns index of the 494 * size class which has chunk size big enough to hold the given size. 495 */ 496 static int get_size_class_index(int size) 497 { 498 int idx = 0; 499 500 if (likely(size > ZS_MIN_ALLOC_SIZE)) 501 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE, 502 ZS_SIZE_CLASS_DELTA); 503 504 return min_t(int, ZS_SIZE_CLASSES - 1, idx); 505 } 506 507 static inline void class_stat_add(struct size_class *class, int type, 508 unsigned long cnt) 509 { 510 class->stats.objs[type] += cnt; 511 } 512 513 static inline void class_stat_sub(struct size_class *class, int type, 514 unsigned long cnt) 515 { 516 class->stats.objs[type] -= cnt; 517 } 518 519 static inline unsigned long class_stat_read(struct size_class *class, int type) 520 { 521 return class->stats.objs[type]; 522 } 523 524 #ifdef CONFIG_ZSMALLOC_STAT 525 526 static void __init zs_stat_init(void) 527 { 528 if (!debugfs_initialized()) { 529 pr_warn("debugfs not available, stat dir not created\n"); 530 return; 531 } 532 533 zs_stat_root = debugfs_create_dir("zsmalloc", NULL); 534 } 535 536 static void __exit zs_stat_exit(void) 537 { 538 debugfs_remove_recursive(zs_stat_root); 539 } 540 541 static unsigned long zs_can_compact(struct size_class *class); 542 543 static int zs_stats_size_show(struct seq_file *s, void *v) 544 { 545 int i, fg; 546 struct zs_pool *pool = s->private; 547 struct size_class *class; 548 int objs_per_zspage; 549 unsigned long obj_allocated, obj_used, pages_used, freeable; 550 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0; 551 unsigned long total_freeable = 0; 552 unsigned long inuse_totals[NR_FULLNESS_GROUPS] = {0, }; 553 554 seq_printf(s, " %5s %5s %9s %9s %9s %9s %9s %9s %9s %9s %9s %9s %9s %13s %10s %10s %16s %8s\n", 555 "class", "size", "10%", "20%", "30%", "40%", 556 "50%", "60%", "70%", "80%", "90%", "99%", "100%", 557 "obj_allocated", "obj_used", "pages_used", 558 "pages_per_zspage", "freeable"); 559 560 for (i = 0; i < ZS_SIZE_CLASSES; i++) { 561 562 class = pool->size_class[i]; 563 564 if (class->index != i) 565 continue; 566 567 spin_lock(&class->lock); 568 569 seq_printf(s, " %5u %5u ", i, class->size); 570 for (fg = ZS_INUSE_RATIO_10; fg < NR_FULLNESS_GROUPS; fg++) { 571 inuse_totals[fg] += class_stat_read(class, fg); 572 seq_printf(s, "%9lu ", class_stat_read(class, fg)); 573 } 574 575 obj_allocated = class_stat_read(class, ZS_OBJS_ALLOCATED); 576 obj_used = class_stat_read(class, ZS_OBJS_INUSE); 577 freeable = zs_can_compact(class); 578 spin_unlock(&class->lock); 579 580 objs_per_zspage = class->objs_per_zspage; 581 pages_used = obj_allocated / objs_per_zspage * 582 class->pages_per_zspage; 583 584 seq_printf(s, "%13lu %10lu %10lu %16d %8lu\n", 585 obj_allocated, obj_used, pages_used, 586 class->pages_per_zspage, freeable); 587 588 total_objs += obj_allocated; 589 total_used_objs += obj_used; 590 total_pages += pages_used; 591 total_freeable += freeable; 592 } 593 594 seq_puts(s, "\n"); 595 seq_printf(s, " %5s %5s ", "Total", ""); 596 597 for (fg = ZS_INUSE_RATIO_10; fg < NR_FULLNESS_GROUPS; fg++) 598 seq_printf(s, "%9lu ", inuse_totals[fg]); 599 600 seq_printf(s, "%13lu %10lu %10lu %16s %8lu\n", 601 total_objs, total_used_objs, total_pages, "", 602 total_freeable); 603 604 return 0; 605 } 606 DEFINE_SHOW_ATTRIBUTE(zs_stats_size); 607 608 static void zs_pool_stat_create(struct zs_pool *pool, const char *name) 609 { 610 if (!zs_stat_root) { 611 pr_warn("no root stat dir, not creating <%s> stat dir\n", name); 612 return; 613 } 614 615 pool->stat_dentry = debugfs_create_dir(name, zs_stat_root); 616 617 debugfs_create_file("classes", S_IFREG | 0444, pool->stat_dentry, pool, 618 &zs_stats_size_fops); 619 } 620 621 static void zs_pool_stat_destroy(struct zs_pool *pool) 622 { 623 debugfs_remove_recursive(pool->stat_dentry); 624 } 625 626 #else /* CONFIG_ZSMALLOC_STAT */ 627 static void __init zs_stat_init(void) 628 { 629 } 630 631 static void __exit zs_stat_exit(void) 632 { 633 } 634 635 static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name) 636 { 637 } 638 639 static inline void zs_pool_stat_destroy(struct zs_pool *pool) 640 { 641 } 642 #endif 643 644 645 /* 646 * For each size class, zspages are divided into different groups 647 * depending on their usage ratio. This function returns fullness 648 * status of the given page. 649 */ 650 static int get_fullness_group(struct size_class *class, struct zspage *zspage) 651 { 652 int inuse, objs_per_zspage, ratio; 653 654 inuse = get_zspage_inuse(zspage); 655 objs_per_zspage = class->objs_per_zspage; 656 657 if (inuse == 0) 658 return ZS_INUSE_RATIO_0; 659 if (inuse == objs_per_zspage) 660 return ZS_INUSE_RATIO_100; 661 662 ratio = 100 * inuse / objs_per_zspage; 663 /* 664 * Take integer division into consideration: a page with one inuse 665 * object out of 127 possible, will end up having 0 usage ratio, 666 * which is wrong as it belongs in ZS_INUSE_RATIO_10 fullness group. 667 */ 668 return ratio / 10 + 1; 669 } 670 671 /* 672 * Each size class maintains various freelists and zspages are assigned 673 * to one of these freelists based on the number of live objects they 674 * have. This functions inserts the given zspage into the freelist 675 * identified by <class, fullness_group>. 676 */ 677 static void insert_zspage(struct size_class *class, 678 struct zspage *zspage, 679 int fullness) 680 { 681 class_stat_add(class, fullness, 1); 682 list_add(&zspage->list, &class->fullness_list[fullness]); 683 zspage->fullness = fullness; 684 } 685 686 /* 687 * This function removes the given zspage from the freelist identified 688 * by <class, fullness_group>. 689 */ 690 static void remove_zspage(struct size_class *class, struct zspage *zspage) 691 { 692 int fullness = zspage->fullness; 693 694 VM_BUG_ON(list_empty(&class->fullness_list[fullness])); 695 696 list_del_init(&zspage->list); 697 class_stat_sub(class, fullness, 1); 698 } 699 700 /* 701 * Each size class maintains zspages in different fullness groups depending 702 * on the number of live objects they contain. When allocating or freeing 703 * objects, the fullness status of the page can change, for instance, from 704 * INUSE_RATIO_80 to INUSE_RATIO_70 when freeing an object. This function 705 * checks if such a status change has occurred for the given page and 706 * accordingly moves the page from the list of the old fullness group to that 707 * of the new fullness group. 708 */ 709 static int fix_fullness_group(struct size_class *class, struct zspage *zspage) 710 { 711 int newfg; 712 713 newfg = get_fullness_group(class, zspage); 714 if (newfg == zspage->fullness) 715 goto out; 716 717 remove_zspage(class, zspage); 718 insert_zspage(class, zspage, newfg); 719 out: 720 return newfg; 721 } 722 723 static struct zspage *get_zspage(struct page *page) 724 { 725 struct zspage *zspage = (struct zspage *)page_private(page); 726 727 BUG_ON(zspage->magic != ZSPAGE_MAGIC); 728 return zspage; 729 } 730 731 static struct page *get_next_page(struct page *page) 732 { 733 struct zspage *zspage = get_zspage(page); 734 735 if (unlikely(ZsHugePage(zspage))) 736 return NULL; 737 738 return (struct page *)page->index; 739 } 740 741 /** 742 * obj_to_location - get (<page>, <obj_idx>) from encoded object value 743 * @obj: the encoded object value 744 * @page: page object resides in zspage 745 * @obj_idx: object index 746 */ 747 static void obj_to_location(unsigned long obj, struct page **page, 748 unsigned int *obj_idx) 749 { 750 *page = pfn_to_page(obj >> OBJ_INDEX_BITS); 751 *obj_idx = (obj & OBJ_INDEX_MASK); 752 } 753 754 static void obj_to_page(unsigned long obj, struct page **page) 755 { 756 *page = pfn_to_page(obj >> OBJ_INDEX_BITS); 757 } 758 759 /** 760 * location_to_obj - get obj value encoded from (<page>, <obj_idx>) 761 * @page: page object resides in zspage 762 * @obj_idx: object index 763 */ 764 static unsigned long location_to_obj(struct page *page, unsigned int obj_idx) 765 { 766 unsigned long obj; 767 768 obj = page_to_pfn(page) << OBJ_INDEX_BITS; 769 obj |= obj_idx & OBJ_INDEX_MASK; 770 771 return obj; 772 } 773 774 static unsigned long handle_to_obj(unsigned long handle) 775 { 776 return *(unsigned long *)handle; 777 } 778 779 static inline bool obj_allocated(struct page *page, void *obj, 780 unsigned long *phandle) 781 { 782 unsigned long handle; 783 struct zspage *zspage = get_zspage(page); 784 785 if (unlikely(ZsHugePage(zspage))) { 786 VM_BUG_ON_PAGE(!is_first_page(page), page); 787 handle = page->index; 788 } else 789 handle = *(unsigned long *)obj; 790 791 if (!(handle & OBJ_ALLOCATED_TAG)) 792 return false; 793 794 /* Clear all tags before returning the handle */ 795 *phandle = handle & ~OBJ_TAG_MASK; 796 return true; 797 } 798 799 static void reset_page(struct page *page) 800 { 801 __ClearPageMovable(page); 802 ClearPagePrivate(page); 803 set_page_private(page, 0); 804 page->index = 0; 805 __ClearPageZsmalloc(page); 806 } 807 808 static int trylock_zspage(struct zspage *zspage) 809 { 810 struct page *cursor, *fail; 811 812 for (cursor = get_first_page(zspage); cursor != NULL; cursor = 813 get_next_page(cursor)) { 814 if (!trylock_page(cursor)) { 815 fail = cursor; 816 goto unlock; 817 } 818 } 819 820 return 1; 821 unlock: 822 for (cursor = get_first_page(zspage); cursor != fail; cursor = 823 get_next_page(cursor)) 824 unlock_page(cursor); 825 826 return 0; 827 } 828 829 static void __free_zspage(struct zs_pool *pool, struct size_class *class, 830 struct zspage *zspage) 831 { 832 struct page *page, *next; 833 834 assert_spin_locked(&class->lock); 835 836 VM_BUG_ON(get_zspage_inuse(zspage)); 837 VM_BUG_ON(zspage->fullness != ZS_INUSE_RATIO_0); 838 839 next = page = get_first_page(zspage); 840 do { 841 VM_BUG_ON_PAGE(!PageLocked(page), page); 842 next = get_next_page(page); 843 reset_page(page); 844 unlock_page(page); 845 dec_zone_page_state(page, NR_ZSPAGES); 846 put_page(page); 847 page = next; 848 } while (page != NULL); 849 850 cache_free_zspage(pool, zspage); 851 852 class_stat_sub(class, ZS_OBJS_ALLOCATED, class->objs_per_zspage); 853 atomic_long_sub(class->pages_per_zspage, &pool->pages_allocated); 854 } 855 856 static void free_zspage(struct zs_pool *pool, struct size_class *class, 857 struct zspage *zspage) 858 { 859 VM_BUG_ON(get_zspage_inuse(zspage)); 860 VM_BUG_ON(list_empty(&zspage->list)); 861 862 /* 863 * Since zs_free couldn't be sleepable, this function cannot call 864 * lock_page. The page locks trylock_zspage got will be released 865 * by __free_zspage. 866 */ 867 if (!trylock_zspage(zspage)) { 868 kick_deferred_free(pool); 869 return; 870 } 871 872 remove_zspage(class, zspage); 873 __free_zspage(pool, class, zspage); 874 } 875 876 /* Initialize a newly allocated zspage */ 877 static void init_zspage(struct size_class *class, struct zspage *zspage) 878 { 879 unsigned int freeobj = 1; 880 unsigned long off = 0; 881 struct page *page = get_first_page(zspage); 882 883 while (page) { 884 struct page *next_page; 885 struct link_free *link; 886 void *vaddr; 887 888 set_first_obj_offset(page, off); 889 890 vaddr = kmap_atomic(page); 891 link = (struct link_free *)vaddr + off / sizeof(*link); 892 893 while ((off += class->size) < PAGE_SIZE) { 894 link->next = freeobj++ << OBJ_TAG_BITS; 895 link += class->size / sizeof(*link); 896 } 897 898 /* 899 * We now come to the last (full or partial) object on this 900 * page, which must point to the first object on the next 901 * page (if present) 902 */ 903 next_page = get_next_page(page); 904 if (next_page) { 905 link->next = freeobj++ << OBJ_TAG_BITS; 906 } else { 907 /* 908 * Reset OBJ_TAG_BITS bit to last link to tell 909 * whether it's allocated object or not. 910 */ 911 link->next = -1UL << OBJ_TAG_BITS; 912 } 913 kunmap_atomic(vaddr); 914 page = next_page; 915 off %= PAGE_SIZE; 916 } 917 918 set_freeobj(zspage, 0); 919 } 920 921 static void create_page_chain(struct size_class *class, struct zspage *zspage, 922 struct page *pages[]) 923 { 924 int i; 925 struct page *page; 926 struct page *prev_page = NULL; 927 int nr_pages = class->pages_per_zspage; 928 929 /* 930 * Allocate individual pages and link them together as: 931 * 1. all pages are linked together using page->index 932 * 2. each sub-page point to zspage using page->private 933 * 934 * we set PG_private to identify the first page (i.e. no other sub-page 935 * has this flag set). 936 */ 937 for (i = 0; i < nr_pages; i++) { 938 page = pages[i]; 939 set_page_private(page, (unsigned long)zspage); 940 page->index = 0; 941 if (i == 0) { 942 zspage->first_page = page; 943 SetPagePrivate(page); 944 if (unlikely(class->objs_per_zspage == 1 && 945 class->pages_per_zspage == 1)) 946 SetZsHugePage(zspage); 947 } else { 948 prev_page->index = (unsigned long)page; 949 } 950 prev_page = page; 951 } 952 } 953 954 /* 955 * Allocate a zspage for the given size class 956 */ 957 static struct zspage *alloc_zspage(struct zs_pool *pool, 958 struct size_class *class, 959 gfp_t gfp) 960 { 961 int i; 962 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE]; 963 struct zspage *zspage = cache_alloc_zspage(pool, gfp); 964 965 if (!zspage) 966 return NULL; 967 968 zspage->magic = ZSPAGE_MAGIC; 969 migrate_lock_init(zspage); 970 971 for (i = 0; i < class->pages_per_zspage; i++) { 972 struct page *page; 973 974 page = alloc_page(gfp); 975 if (!page) { 976 while (--i >= 0) { 977 dec_zone_page_state(pages[i], NR_ZSPAGES); 978 __ClearPageZsmalloc(pages[i]); 979 __free_page(pages[i]); 980 } 981 cache_free_zspage(pool, zspage); 982 return NULL; 983 } 984 __SetPageZsmalloc(page); 985 986 inc_zone_page_state(page, NR_ZSPAGES); 987 pages[i] = page; 988 } 989 990 create_page_chain(class, zspage, pages); 991 init_zspage(class, zspage); 992 zspage->pool = pool; 993 zspage->class = class->index; 994 995 return zspage; 996 } 997 998 static struct zspage *find_get_zspage(struct size_class *class) 999 { 1000 int i; 1001 struct zspage *zspage; 1002 1003 for (i = ZS_INUSE_RATIO_99; i >= ZS_INUSE_RATIO_0; i--) { 1004 zspage = list_first_entry_or_null(&class->fullness_list[i], 1005 struct zspage, list); 1006 if (zspage) 1007 break; 1008 } 1009 1010 return zspage; 1011 } 1012 1013 static inline int __zs_cpu_up(struct mapping_area *area) 1014 { 1015 /* 1016 * Make sure we don't leak memory if a cpu UP notification 1017 * and zs_init() race and both call zs_cpu_up() on the same cpu 1018 */ 1019 if (area->vm_buf) 1020 return 0; 1021 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL); 1022 if (!area->vm_buf) 1023 return -ENOMEM; 1024 return 0; 1025 } 1026 1027 static inline void __zs_cpu_down(struct mapping_area *area) 1028 { 1029 kfree(area->vm_buf); 1030 area->vm_buf = NULL; 1031 } 1032 1033 static void *__zs_map_object(struct mapping_area *area, 1034 struct page *pages[2], int off, int size) 1035 { 1036 int sizes[2]; 1037 void *addr; 1038 char *buf = area->vm_buf; 1039 1040 /* disable page faults to match kmap_atomic() return conditions */ 1041 pagefault_disable(); 1042 1043 /* no read fastpath */ 1044 if (area->vm_mm == ZS_MM_WO) 1045 goto out; 1046 1047 sizes[0] = PAGE_SIZE - off; 1048 sizes[1] = size - sizes[0]; 1049 1050 /* copy object to per-cpu buffer */ 1051 addr = kmap_atomic(pages[0]); 1052 memcpy(buf, addr + off, sizes[0]); 1053 kunmap_atomic(addr); 1054 addr = kmap_atomic(pages[1]); 1055 memcpy(buf + sizes[0], addr, sizes[1]); 1056 kunmap_atomic(addr); 1057 out: 1058 return area->vm_buf; 1059 } 1060 1061 static void __zs_unmap_object(struct mapping_area *area, 1062 struct page *pages[2], int off, int size) 1063 { 1064 int sizes[2]; 1065 void *addr; 1066 char *buf; 1067 1068 /* no write fastpath */ 1069 if (area->vm_mm == ZS_MM_RO) 1070 goto out; 1071 1072 buf = area->vm_buf; 1073 buf = buf + ZS_HANDLE_SIZE; 1074 size -= ZS_HANDLE_SIZE; 1075 off += ZS_HANDLE_SIZE; 1076 1077 sizes[0] = PAGE_SIZE - off; 1078 sizes[1] = size - sizes[0]; 1079 1080 /* copy per-cpu buffer to object */ 1081 addr = kmap_atomic(pages[0]); 1082 memcpy(addr + off, buf, sizes[0]); 1083 kunmap_atomic(addr); 1084 addr = kmap_atomic(pages[1]); 1085 memcpy(addr, buf + sizes[0], sizes[1]); 1086 kunmap_atomic(addr); 1087 1088 out: 1089 /* enable page faults to match kunmap_atomic() return conditions */ 1090 pagefault_enable(); 1091 } 1092 1093 static int zs_cpu_prepare(unsigned int cpu) 1094 { 1095 struct mapping_area *area; 1096 1097 area = &per_cpu(zs_map_area, cpu); 1098 return __zs_cpu_up(area); 1099 } 1100 1101 static int zs_cpu_dead(unsigned int cpu) 1102 { 1103 struct mapping_area *area; 1104 1105 area = &per_cpu(zs_map_area, cpu); 1106 __zs_cpu_down(area); 1107 return 0; 1108 } 1109 1110 static bool can_merge(struct size_class *prev, int pages_per_zspage, 1111 int objs_per_zspage) 1112 { 1113 if (prev->pages_per_zspage == pages_per_zspage && 1114 prev->objs_per_zspage == objs_per_zspage) 1115 return true; 1116 1117 return false; 1118 } 1119 1120 static bool zspage_full(struct size_class *class, struct zspage *zspage) 1121 { 1122 return get_zspage_inuse(zspage) == class->objs_per_zspage; 1123 } 1124 1125 static bool zspage_empty(struct zspage *zspage) 1126 { 1127 return get_zspage_inuse(zspage) == 0; 1128 } 1129 1130 /** 1131 * zs_lookup_class_index() - Returns index of the zsmalloc &size_class 1132 * that hold objects of the provided size. 1133 * @pool: zsmalloc pool to use 1134 * @size: object size 1135 * 1136 * Context: Any context. 1137 * 1138 * Return: the index of the zsmalloc &size_class that hold objects of the 1139 * provided size. 1140 */ 1141 unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size) 1142 { 1143 struct size_class *class; 1144 1145 class = pool->size_class[get_size_class_index(size)]; 1146 1147 return class->index; 1148 } 1149 EXPORT_SYMBOL_GPL(zs_lookup_class_index); 1150 1151 unsigned long zs_get_total_pages(struct zs_pool *pool) 1152 { 1153 return atomic_long_read(&pool->pages_allocated); 1154 } 1155 EXPORT_SYMBOL_GPL(zs_get_total_pages); 1156 1157 /** 1158 * zs_map_object - get address of allocated object from handle. 1159 * @pool: pool from which the object was allocated 1160 * @handle: handle returned from zs_malloc 1161 * @mm: mapping mode to use 1162 * 1163 * Before using an object allocated from zs_malloc, it must be mapped using 1164 * this function. When done with the object, it must be unmapped using 1165 * zs_unmap_object. 1166 * 1167 * Only one object can be mapped per cpu at a time. There is no protection 1168 * against nested mappings. 1169 * 1170 * This function returns with preemption and page faults disabled. 1171 */ 1172 void *zs_map_object(struct zs_pool *pool, unsigned long handle, 1173 enum zs_mapmode mm) 1174 { 1175 struct zspage *zspage; 1176 struct page *page; 1177 unsigned long obj, off; 1178 unsigned int obj_idx; 1179 1180 struct size_class *class; 1181 struct mapping_area *area; 1182 struct page *pages[2]; 1183 void *ret; 1184 1185 /* 1186 * Because we use per-cpu mapping areas shared among the 1187 * pools/users, we can't allow mapping in interrupt context 1188 * because it can corrupt another users mappings. 1189 */ 1190 BUG_ON(in_interrupt()); 1191 1192 /* It guarantees it can get zspage from handle safely */ 1193 read_lock(&pool->migrate_lock); 1194 obj = handle_to_obj(handle); 1195 obj_to_location(obj, &page, &obj_idx); 1196 zspage = get_zspage(page); 1197 1198 /* 1199 * migration cannot move any zpages in this zspage. Here, class->lock 1200 * is too heavy since callers would take some time until they calls 1201 * zs_unmap_object API so delegate the locking from class to zspage 1202 * which is smaller granularity. 1203 */ 1204 migrate_read_lock(zspage); 1205 read_unlock(&pool->migrate_lock); 1206 1207 class = zspage_class(pool, zspage); 1208 off = offset_in_page(class->size * obj_idx); 1209 1210 local_lock(&zs_map_area.lock); 1211 area = this_cpu_ptr(&zs_map_area); 1212 area->vm_mm = mm; 1213 if (off + class->size <= PAGE_SIZE) { 1214 /* this object is contained entirely within a page */ 1215 area->vm_addr = kmap_atomic(page); 1216 ret = area->vm_addr + off; 1217 goto out; 1218 } 1219 1220 /* this object spans two pages */ 1221 pages[0] = page; 1222 pages[1] = get_next_page(page); 1223 BUG_ON(!pages[1]); 1224 1225 ret = __zs_map_object(area, pages, off, class->size); 1226 out: 1227 if (likely(!ZsHugePage(zspage))) 1228 ret += ZS_HANDLE_SIZE; 1229 1230 return ret; 1231 } 1232 EXPORT_SYMBOL_GPL(zs_map_object); 1233 1234 void zs_unmap_object(struct zs_pool *pool, unsigned long handle) 1235 { 1236 struct zspage *zspage; 1237 struct page *page; 1238 unsigned long obj, off; 1239 unsigned int obj_idx; 1240 1241 struct size_class *class; 1242 struct mapping_area *area; 1243 1244 obj = handle_to_obj(handle); 1245 obj_to_location(obj, &page, &obj_idx); 1246 zspage = get_zspage(page); 1247 class = zspage_class(pool, zspage); 1248 off = offset_in_page(class->size * obj_idx); 1249 1250 area = this_cpu_ptr(&zs_map_area); 1251 if (off + class->size <= PAGE_SIZE) 1252 kunmap_atomic(area->vm_addr); 1253 else { 1254 struct page *pages[2]; 1255 1256 pages[0] = page; 1257 pages[1] = get_next_page(page); 1258 BUG_ON(!pages[1]); 1259 1260 __zs_unmap_object(area, pages, off, class->size); 1261 } 1262 local_unlock(&zs_map_area.lock); 1263 1264 migrate_read_unlock(zspage); 1265 } 1266 EXPORT_SYMBOL_GPL(zs_unmap_object); 1267 1268 /** 1269 * zs_huge_class_size() - Returns the size (in bytes) of the first huge 1270 * zsmalloc &size_class. 1271 * @pool: zsmalloc pool to use 1272 * 1273 * The function returns the size of the first huge class - any object of equal 1274 * or bigger size will be stored in zspage consisting of a single physical 1275 * page. 1276 * 1277 * Context: Any context. 1278 * 1279 * Return: the size (in bytes) of the first huge zsmalloc &size_class. 1280 */ 1281 size_t zs_huge_class_size(struct zs_pool *pool) 1282 { 1283 return huge_class_size; 1284 } 1285 EXPORT_SYMBOL_GPL(zs_huge_class_size); 1286 1287 static unsigned long obj_malloc(struct zs_pool *pool, 1288 struct zspage *zspage, unsigned long handle) 1289 { 1290 int i, nr_page, offset; 1291 unsigned long obj; 1292 struct link_free *link; 1293 struct size_class *class; 1294 1295 struct page *m_page; 1296 unsigned long m_offset; 1297 void *vaddr; 1298 1299 class = pool->size_class[zspage->class]; 1300 obj = get_freeobj(zspage); 1301 1302 offset = obj * class->size; 1303 nr_page = offset >> PAGE_SHIFT; 1304 m_offset = offset_in_page(offset); 1305 m_page = get_first_page(zspage); 1306 1307 for (i = 0; i < nr_page; i++) 1308 m_page = get_next_page(m_page); 1309 1310 vaddr = kmap_atomic(m_page); 1311 link = (struct link_free *)vaddr + m_offset / sizeof(*link); 1312 set_freeobj(zspage, link->next >> OBJ_TAG_BITS); 1313 if (likely(!ZsHugePage(zspage))) 1314 /* record handle in the header of allocated chunk */ 1315 link->handle = handle | OBJ_ALLOCATED_TAG; 1316 else 1317 /* record handle to page->index */ 1318 zspage->first_page->index = handle | OBJ_ALLOCATED_TAG; 1319 1320 kunmap_atomic(vaddr); 1321 mod_zspage_inuse(zspage, 1); 1322 1323 obj = location_to_obj(m_page, obj); 1324 record_obj(handle, obj); 1325 1326 return obj; 1327 } 1328 1329 1330 /** 1331 * zs_malloc - Allocate block of given size from pool. 1332 * @pool: pool to allocate from 1333 * @size: size of block to allocate 1334 * @gfp: gfp flags when allocating object 1335 * 1336 * On success, handle to the allocated object is returned, 1337 * otherwise an ERR_PTR(). 1338 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail. 1339 */ 1340 unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp) 1341 { 1342 unsigned long handle; 1343 struct size_class *class; 1344 int newfg; 1345 struct zspage *zspage; 1346 1347 if (unlikely(!size)) 1348 return (unsigned long)ERR_PTR(-EINVAL); 1349 1350 if (unlikely(size > ZS_MAX_ALLOC_SIZE)) 1351 return (unsigned long)ERR_PTR(-ENOSPC); 1352 1353 handle = cache_alloc_handle(pool, gfp); 1354 if (!handle) 1355 return (unsigned long)ERR_PTR(-ENOMEM); 1356 1357 /* extra space in chunk to keep the handle */ 1358 size += ZS_HANDLE_SIZE; 1359 class = pool->size_class[get_size_class_index(size)]; 1360 1361 /* class->lock effectively protects the zpage migration */ 1362 spin_lock(&class->lock); 1363 zspage = find_get_zspage(class); 1364 if (likely(zspage)) { 1365 obj_malloc(pool, zspage, handle); 1366 /* Now move the zspage to another fullness group, if required */ 1367 fix_fullness_group(class, zspage); 1368 class_stat_add(class, ZS_OBJS_INUSE, 1); 1369 1370 goto out; 1371 } 1372 1373 spin_unlock(&class->lock); 1374 1375 zspage = alloc_zspage(pool, class, gfp); 1376 if (!zspage) { 1377 cache_free_handle(pool, handle); 1378 return (unsigned long)ERR_PTR(-ENOMEM); 1379 } 1380 1381 spin_lock(&class->lock); 1382 obj_malloc(pool, zspage, handle); 1383 newfg = get_fullness_group(class, zspage); 1384 insert_zspage(class, zspage, newfg); 1385 atomic_long_add(class->pages_per_zspage, &pool->pages_allocated); 1386 class_stat_add(class, ZS_OBJS_ALLOCATED, class->objs_per_zspage); 1387 class_stat_add(class, ZS_OBJS_INUSE, 1); 1388 1389 /* We completely set up zspage so mark them as movable */ 1390 SetZsPageMovable(pool, zspage); 1391 out: 1392 spin_unlock(&class->lock); 1393 1394 return handle; 1395 } 1396 EXPORT_SYMBOL_GPL(zs_malloc); 1397 1398 static void obj_free(int class_size, unsigned long obj) 1399 { 1400 struct link_free *link; 1401 struct zspage *zspage; 1402 struct page *f_page; 1403 unsigned long f_offset; 1404 unsigned int f_objidx; 1405 void *vaddr; 1406 1407 obj_to_location(obj, &f_page, &f_objidx); 1408 f_offset = offset_in_page(class_size * f_objidx); 1409 zspage = get_zspage(f_page); 1410 1411 vaddr = kmap_atomic(f_page); 1412 link = (struct link_free *)(vaddr + f_offset); 1413 1414 /* Insert this object in containing zspage's freelist */ 1415 if (likely(!ZsHugePage(zspage))) 1416 link->next = get_freeobj(zspage) << OBJ_TAG_BITS; 1417 else 1418 f_page->index = 0; 1419 set_freeobj(zspage, f_objidx); 1420 1421 kunmap_atomic(vaddr); 1422 mod_zspage_inuse(zspage, -1); 1423 } 1424 1425 void zs_free(struct zs_pool *pool, unsigned long handle) 1426 { 1427 struct zspage *zspage; 1428 struct page *f_page; 1429 unsigned long obj; 1430 struct size_class *class; 1431 int fullness; 1432 1433 if (IS_ERR_OR_NULL((void *)handle)) 1434 return; 1435 1436 /* 1437 * The pool->migrate_lock protects the race with zpage's migration 1438 * so it's safe to get the page from handle. 1439 */ 1440 read_lock(&pool->migrate_lock); 1441 obj = handle_to_obj(handle); 1442 obj_to_page(obj, &f_page); 1443 zspage = get_zspage(f_page); 1444 class = zspage_class(pool, zspage); 1445 spin_lock(&class->lock); 1446 read_unlock(&pool->migrate_lock); 1447 1448 class_stat_sub(class, ZS_OBJS_INUSE, 1); 1449 obj_free(class->size, obj); 1450 1451 fullness = fix_fullness_group(class, zspage); 1452 if (fullness == ZS_INUSE_RATIO_0) 1453 free_zspage(pool, class, zspage); 1454 1455 spin_unlock(&class->lock); 1456 cache_free_handle(pool, handle); 1457 } 1458 EXPORT_SYMBOL_GPL(zs_free); 1459 1460 static void zs_object_copy(struct size_class *class, unsigned long dst, 1461 unsigned long src) 1462 { 1463 struct page *s_page, *d_page; 1464 unsigned int s_objidx, d_objidx; 1465 unsigned long s_off, d_off; 1466 void *s_addr, *d_addr; 1467 int s_size, d_size, size; 1468 int written = 0; 1469 1470 s_size = d_size = class->size; 1471 1472 obj_to_location(src, &s_page, &s_objidx); 1473 obj_to_location(dst, &d_page, &d_objidx); 1474 1475 s_off = offset_in_page(class->size * s_objidx); 1476 d_off = offset_in_page(class->size * d_objidx); 1477 1478 if (s_off + class->size > PAGE_SIZE) 1479 s_size = PAGE_SIZE - s_off; 1480 1481 if (d_off + class->size > PAGE_SIZE) 1482 d_size = PAGE_SIZE - d_off; 1483 1484 s_addr = kmap_atomic(s_page); 1485 d_addr = kmap_atomic(d_page); 1486 1487 while (1) { 1488 size = min(s_size, d_size); 1489 memcpy(d_addr + d_off, s_addr + s_off, size); 1490 written += size; 1491 1492 if (written == class->size) 1493 break; 1494 1495 s_off += size; 1496 s_size -= size; 1497 d_off += size; 1498 d_size -= size; 1499 1500 /* 1501 * Calling kunmap_atomic(d_addr) is necessary. kunmap_atomic() 1502 * calls must occurs in reverse order of calls to kmap_atomic(). 1503 * So, to call kunmap_atomic(s_addr) we should first call 1504 * kunmap_atomic(d_addr). For more details see 1505 * Documentation/mm/highmem.rst. 1506 */ 1507 if (s_off >= PAGE_SIZE) { 1508 kunmap_atomic(d_addr); 1509 kunmap_atomic(s_addr); 1510 s_page = get_next_page(s_page); 1511 s_addr = kmap_atomic(s_page); 1512 d_addr = kmap_atomic(d_page); 1513 s_size = class->size - written; 1514 s_off = 0; 1515 } 1516 1517 if (d_off >= PAGE_SIZE) { 1518 kunmap_atomic(d_addr); 1519 d_page = get_next_page(d_page); 1520 d_addr = kmap_atomic(d_page); 1521 d_size = class->size - written; 1522 d_off = 0; 1523 } 1524 } 1525 1526 kunmap_atomic(d_addr); 1527 kunmap_atomic(s_addr); 1528 } 1529 1530 /* 1531 * Find alloced object in zspage from index object and 1532 * return handle. 1533 */ 1534 static unsigned long find_alloced_obj(struct size_class *class, 1535 struct page *page, int *obj_idx) 1536 { 1537 unsigned int offset; 1538 int index = *obj_idx; 1539 unsigned long handle = 0; 1540 void *addr = kmap_atomic(page); 1541 1542 offset = get_first_obj_offset(page); 1543 offset += class->size * index; 1544 1545 while (offset < PAGE_SIZE) { 1546 if (obj_allocated(page, addr + offset, &handle)) 1547 break; 1548 1549 offset += class->size; 1550 index++; 1551 } 1552 1553 kunmap_atomic(addr); 1554 1555 *obj_idx = index; 1556 1557 return handle; 1558 } 1559 1560 static void migrate_zspage(struct zs_pool *pool, struct zspage *src_zspage, 1561 struct zspage *dst_zspage) 1562 { 1563 unsigned long used_obj, free_obj; 1564 unsigned long handle; 1565 int obj_idx = 0; 1566 struct page *s_page = get_first_page(src_zspage); 1567 struct size_class *class = pool->size_class[src_zspage->class]; 1568 1569 while (1) { 1570 handle = find_alloced_obj(class, s_page, &obj_idx); 1571 if (!handle) { 1572 s_page = get_next_page(s_page); 1573 if (!s_page) 1574 break; 1575 obj_idx = 0; 1576 continue; 1577 } 1578 1579 used_obj = handle_to_obj(handle); 1580 free_obj = obj_malloc(pool, dst_zspage, handle); 1581 zs_object_copy(class, free_obj, used_obj); 1582 obj_idx++; 1583 obj_free(class->size, used_obj); 1584 1585 /* Stop if there is no more space */ 1586 if (zspage_full(class, dst_zspage)) 1587 break; 1588 1589 /* Stop if there are no more objects to migrate */ 1590 if (zspage_empty(src_zspage)) 1591 break; 1592 } 1593 } 1594 1595 static struct zspage *isolate_src_zspage(struct size_class *class) 1596 { 1597 struct zspage *zspage; 1598 int fg; 1599 1600 for (fg = ZS_INUSE_RATIO_10; fg <= ZS_INUSE_RATIO_99; fg++) { 1601 zspage = list_first_entry_or_null(&class->fullness_list[fg], 1602 struct zspage, list); 1603 if (zspage) { 1604 remove_zspage(class, zspage); 1605 return zspage; 1606 } 1607 } 1608 1609 return zspage; 1610 } 1611 1612 static struct zspage *isolate_dst_zspage(struct size_class *class) 1613 { 1614 struct zspage *zspage; 1615 int fg; 1616 1617 for (fg = ZS_INUSE_RATIO_99; fg >= ZS_INUSE_RATIO_10; fg--) { 1618 zspage = list_first_entry_or_null(&class->fullness_list[fg], 1619 struct zspage, list); 1620 if (zspage) { 1621 remove_zspage(class, zspage); 1622 return zspage; 1623 } 1624 } 1625 1626 return zspage; 1627 } 1628 1629 /* 1630 * putback_zspage - add @zspage into right class's fullness list 1631 * @class: destination class 1632 * @zspage: target page 1633 * 1634 * Return @zspage's fullness status 1635 */ 1636 static int putback_zspage(struct size_class *class, struct zspage *zspage) 1637 { 1638 int fullness; 1639 1640 fullness = get_fullness_group(class, zspage); 1641 insert_zspage(class, zspage, fullness); 1642 1643 return fullness; 1644 } 1645 1646 #ifdef CONFIG_COMPACTION 1647 /* 1648 * To prevent zspage destroy during migration, zspage freeing should 1649 * hold locks of all pages in the zspage. 1650 */ 1651 static void lock_zspage(struct zspage *zspage) 1652 { 1653 struct page *curr_page, *page; 1654 1655 /* 1656 * Pages we haven't locked yet can be migrated off the list while we're 1657 * trying to lock them, so we need to be careful and only attempt to 1658 * lock each page under migrate_read_lock(). Otherwise, the page we lock 1659 * may no longer belong to the zspage. This means that we may wait for 1660 * the wrong page to unlock, so we must take a reference to the page 1661 * prior to waiting for it to unlock outside migrate_read_lock(). 1662 */ 1663 while (1) { 1664 migrate_read_lock(zspage); 1665 page = get_first_page(zspage); 1666 if (trylock_page(page)) 1667 break; 1668 get_page(page); 1669 migrate_read_unlock(zspage); 1670 wait_on_page_locked(page); 1671 put_page(page); 1672 } 1673 1674 curr_page = page; 1675 while ((page = get_next_page(curr_page))) { 1676 if (trylock_page(page)) { 1677 curr_page = page; 1678 } else { 1679 get_page(page); 1680 migrate_read_unlock(zspage); 1681 wait_on_page_locked(page); 1682 put_page(page); 1683 migrate_read_lock(zspage); 1684 } 1685 } 1686 migrate_read_unlock(zspage); 1687 } 1688 #endif /* CONFIG_COMPACTION */ 1689 1690 static void migrate_lock_init(struct zspage *zspage) 1691 { 1692 rwlock_init(&zspage->lock); 1693 } 1694 1695 static void migrate_read_lock(struct zspage *zspage) __acquires(&zspage->lock) 1696 { 1697 read_lock(&zspage->lock); 1698 } 1699 1700 static void migrate_read_unlock(struct zspage *zspage) __releases(&zspage->lock) 1701 { 1702 read_unlock(&zspage->lock); 1703 } 1704 1705 static void migrate_write_lock(struct zspage *zspage) 1706 { 1707 write_lock(&zspage->lock); 1708 } 1709 1710 static void migrate_write_unlock(struct zspage *zspage) 1711 { 1712 write_unlock(&zspage->lock); 1713 } 1714 1715 #ifdef CONFIG_COMPACTION 1716 1717 static const struct movable_operations zsmalloc_mops; 1718 1719 static void replace_sub_page(struct size_class *class, struct zspage *zspage, 1720 struct page *newpage, struct page *oldpage) 1721 { 1722 struct page *page; 1723 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, }; 1724 int idx = 0; 1725 1726 page = get_first_page(zspage); 1727 do { 1728 if (page == oldpage) 1729 pages[idx] = newpage; 1730 else 1731 pages[idx] = page; 1732 idx++; 1733 } while ((page = get_next_page(page)) != NULL); 1734 1735 create_page_chain(class, zspage, pages); 1736 set_first_obj_offset(newpage, get_first_obj_offset(oldpage)); 1737 if (unlikely(ZsHugePage(zspage))) 1738 newpage->index = oldpage->index; 1739 __SetPageMovable(newpage, &zsmalloc_mops); 1740 } 1741 1742 static bool zs_page_isolate(struct page *page, isolate_mode_t mode) 1743 { 1744 /* 1745 * Page is locked so zspage couldn't be destroyed. For detail, look at 1746 * lock_zspage in free_zspage. 1747 */ 1748 VM_BUG_ON_PAGE(PageIsolated(page), page); 1749 1750 return true; 1751 } 1752 1753 static int zs_page_migrate(struct page *newpage, struct page *page, 1754 enum migrate_mode mode) 1755 { 1756 struct zs_pool *pool; 1757 struct size_class *class; 1758 struct zspage *zspage; 1759 struct page *dummy; 1760 void *s_addr, *d_addr, *addr; 1761 unsigned int offset; 1762 unsigned long handle; 1763 unsigned long old_obj, new_obj; 1764 unsigned int obj_idx; 1765 1766 VM_BUG_ON_PAGE(!PageIsolated(page), page); 1767 1768 /* We're committed, tell the world that this is a Zsmalloc page. */ 1769 __SetPageZsmalloc(newpage); 1770 1771 /* The page is locked, so this pointer must remain valid */ 1772 zspage = get_zspage(page); 1773 pool = zspage->pool; 1774 1775 /* 1776 * The pool migrate_lock protects the race between zpage migration 1777 * and zs_free. 1778 */ 1779 write_lock(&pool->migrate_lock); 1780 class = zspage_class(pool, zspage); 1781 1782 /* 1783 * the class lock protects zpage alloc/free in the zspage. 1784 */ 1785 spin_lock(&class->lock); 1786 /* the migrate_write_lock protects zpage access via zs_map_object */ 1787 migrate_write_lock(zspage); 1788 1789 offset = get_first_obj_offset(page); 1790 s_addr = kmap_atomic(page); 1791 1792 /* 1793 * Here, any user cannot access all objects in the zspage so let's move. 1794 */ 1795 d_addr = kmap_atomic(newpage); 1796 copy_page(d_addr, s_addr); 1797 kunmap_atomic(d_addr); 1798 1799 for (addr = s_addr + offset; addr < s_addr + PAGE_SIZE; 1800 addr += class->size) { 1801 if (obj_allocated(page, addr, &handle)) { 1802 1803 old_obj = handle_to_obj(handle); 1804 obj_to_location(old_obj, &dummy, &obj_idx); 1805 new_obj = (unsigned long)location_to_obj(newpage, 1806 obj_idx); 1807 record_obj(handle, new_obj); 1808 } 1809 } 1810 kunmap_atomic(s_addr); 1811 1812 replace_sub_page(class, zspage, newpage, page); 1813 /* 1814 * Since we complete the data copy and set up new zspage structure, 1815 * it's okay to release migration_lock. 1816 */ 1817 write_unlock(&pool->migrate_lock); 1818 spin_unlock(&class->lock); 1819 migrate_write_unlock(zspage); 1820 1821 get_page(newpage); 1822 if (page_zone(newpage) != page_zone(page)) { 1823 dec_zone_page_state(page, NR_ZSPAGES); 1824 inc_zone_page_state(newpage, NR_ZSPAGES); 1825 } 1826 1827 reset_page(page); 1828 put_page(page); 1829 1830 return MIGRATEPAGE_SUCCESS; 1831 } 1832 1833 static void zs_page_putback(struct page *page) 1834 { 1835 VM_BUG_ON_PAGE(!PageIsolated(page), page); 1836 } 1837 1838 static const struct movable_operations zsmalloc_mops = { 1839 .isolate_page = zs_page_isolate, 1840 .migrate_page = zs_page_migrate, 1841 .putback_page = zs_page_putback, 1842 }; 1843 1844 /* 1845 * Caller should hold page_lock of all pages in the zspage 1846 * In here, we cannot use zspage meta data. 1847 */ 1848 static void async_free_zspage(struct work_struct *work) 1849 { 1850 int i; 1851 struct size_class *class; 1852 struct zspage *zspage, *tmp; 1853 LIST_HEAD(free_pages); 1854 struct zs_pool *pool = container_of(work, struct zs_pool, 1855 free_work); 1856 1857 for (i = 0; i < ZS_SIZE_CLASSES; i++) { 1858 class = pool->size_class[i]; 1859 if (class->index != i) 1860 continue; 1861 1862 spin_lock(&class->lock); 1863 list_splice_init(&class->fullness_list[ZS_INUSE_RATIO_0], 1864 &free_pages); 1865 spin_unlock(&class->lock); 1866 } 1867 1868 list_for_each_entry_safe(zspage, tmp, &free_pages, list) { 1869 list_del(&zspage->list); 1870 lock_zspage(zspage); 1871 1872 class = zspage_class(pool, zspage); 1873 spin_lock(&class->lock); 1874 class_stat_sub(class, ZS_INUSE_RATIO_0, 1); 1875 __free_zspage(pool, class, zspage); 1876 spin_unlock(&class->lock); 1877 } 1878 }; 1879 1880 static void kick_deferred_free(struct zs_pool *pool) 1881 { 1882 schedule_work(&pool->free_work); 1883 } 1884 1885 static void zs_flush_migration(struct zs_pool *pool) 1886 { 1887 flush_work(&pool->free_work); 1888 } 1889 1890 static void init_deferred_free(struct zs_pool *pool) 1891 { 1892 INIT_WORK(&pool->free_work, async_free_zspage); 1893 } 1894 1895 static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) 1896 { 1897 struct page *page = get_first_page(zspage); 1898 1899 do { 1900 WARN_ON(!trylock_page(page)); 1901 __SetPageMovable(page, &zsmalloc_mops); 1902 unlock_page(page); 1903 } while ((page = get_next_page(page)) != NULL); 1904 } 1905 #else 1906 static inline void zs_flush_migration(struct zs_pool *pool) { } 1907 #endif 1908 1909 /* 1910 * 1911 * Based on the number of unused allocated objects calculate 1912 * and return the number of pages that we can free. 1913 */ 1914 static unsigned long zs_can_compact(struct size_class *class) 1915 { 1916 unsigned long obj_wasted; 1917 unsigned long obj_allocated = class_stat_read(class, ZS_OBJS_ALLOCATED); 1918 unsigned long obj_used = class_stat_read(class, ZS_OBJS_INUSE); 1919 1920 if (obj_allocated <= obj_used) 1921 return 0; 1922 1923 obj_wasted = obj_allocated - obj_used; 1924 obj_wasted /= class->objs_per_zspage; 1925 1926 return obj_wasted * class->pages_per_zspage; 1927 } 1928 1929 static unsigned long __zs_compact(struct zs_pool *pool, 1930 struct size_class *class) 1931 { 1932 struct zspage *src_zspage = NULL; 1933 struct zspage *dst_zspage = NULL; 1934 unsigned long pages_freed = 0; 1935 1936 /* 1937 * protect the race between zpage migration and zs_free 1938 * as well as zpage allocation/free 1939 */ 1940 write_lock(&pool->migrate_lock); 1941 spin_lock(&class->lock); 1942 while (zs_can_compact(class)) { 1943 int fg; 1944 1945 if (!dst_zspage) { 1946 dst_zspage = isolate_dst_zspage(class); 1947 if (!dst_zspage) 1948 break; 1949 } 1950 1951 src_zspage = isolate_src_zspage(class); 1952 if (!src_zspage) 1953 break; 1954 1955 migrate_write_lock(src_zspage); 1956 migrate_zspage(pool, src_zspage, dst_zspage); 1957 migrate_write_unlock(src_zspage); 1958 1959 fg = putback_zspage(class, src_zspage); 1960 if (fg == ZS_INUSE_RATIO_0) { 1961 free_zspage(pool, class, src_zspage); 1962 pages_freed += class->pages_per_zspage; 1963 } 1964 src_zspage = NULL; 1965 1966 if (get_fullness_group(class, dst_zspage) == ZS_INUSE_RATIO_100 1967 || rwlock_is_contended(&pool->migrate_lock)) { 1968 putback_zspage(class, dst_zspage); 1969 dst_zspage = NULL; 1970 1971 spin_unlock(&class->lock); 1972 write_unlock(&pool->migrate_lock); 1973 cond_resched(); 1974 write_lock(&pool->migrate_lock); 1975 spin_lock(&class->lock); 1976 } 1977 } 1978 1979 if (src_zspage) 1980 putback_zspage(class, src_zspage); 1981 1982 if (dst_zspage) 1983 putback_zspage(class, dst_zspage); 1984 1985 spin_unlock(&class->lock); 1986 write_unlock(&pool->migrate_lock); 1987 1988 return pages_freed; 1989 } 1990 1991 unsigned long zs_compact(struct zs_pool *pool) 1992 { 1993 int i; 1994 struct size_class *class; 1995 unsigned long pages_freed = 0; 1996 1997 /* 1998 * Pool compaction is performed under pool->migrate_lock so it is basically 1999 * single-threaded. Having more than one thread in __zs_compact() 2000 * will increase pool->migrate_lock contention, which will impact other 2001 * zsmalloc operations that need pool->migrate_lock. 2002 */ 2003 if (atomic_xchg(&pool->compaction_in_progress, 1)) 2004 return 0; 2005 2006 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) { 2007 class = pool->size_class[i]; 2008 if (class->index != i) 2009 continue; 2010 pages_freed += __zs_compact(pool, class); 2011 } 2012 atomic_long_add(pages_freed, &pool->stats.pages_compacted); 2013 atomic_set(&pool->compaction_in_progress, 0); 2014 2015 return pages_freed; 2016 } 2017 EXPORT_SYMBOL_GPL(zs_compact); 2018 2019 void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats) 2020 { 2021 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats)); 2022 } 2023 EXPORT_SYMBOL_GPL(zs_pool_stats); 2024 2025 static unsigned long zs_shrinker_scan(struct shrinker *shrinker, 2026 struct shrink_control *sc) 2027 { 2028 unsigned long pages_freed; 2029 struct zs_pool *pool = shrinker->private_data; 2030 2031 /* 2032 * Compact classes and calculate compaction delta. 2033 * Can run concurrently with a manually triggered 2034 * (by user) compaction. 2035 */ 2036 pages_freed = zs_compact(pool); 2037 2038 return pages_freed ? pages_freed : SHRINK_STOP; 2039 } 2040 2041 static unsigned long zs_shrinker_count(struct shrinker *shrinker, 2042 struct shrink_control *sc) 2043 { 2044 int i; 2045 struct size_class *class; 2046 unsigned long pages_to_free = 0; 2047 struct zs_pool *pool = shrinker->private_data; 2048 2049 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) { 2050 class = pool->size_class[i]; 2051 if (class->index != i) 2052 continue; 2053 2054 pages_to_free += zs_can_compact(class); 2055 } 2056 2057 return pages_to_free; 2058 } 2059 2060 static void zs_unregister_shrinker(struct zs_pool *pool) 2061 { 2062 shrinker_free(pool->shrinker); 2063 } 2064 2065 static int zs_register_shrinker(struct zs_pool *pool) 2066 { 2067 pool->shrinker = shrinker_alloc(0, "mm-zspool:%s", pool->name); 2068 if (!pool->shrinker) 2069 return -ENOMEM; 2070 2071 pool->shrinker->scan_objects = zs_shrinker_scan; 2072 pool->shrinker->count_objects = zs_shrinker_count; 2073 pool->shrinker->batch = 0; 2074 pool->shrinker->private_data = pool; 2075 2076 shrinker_register(pool->shrinker); 2077 2078 return 0; 2079 } 2080 2081 static int calculate_zspage_chain_size(int class_size) 2082 { 2083 int i, min_waste = INT_MAX; 2084 int chain_size = 1; 2085 2086 if (is_power_of_2(class_size)) 2087 return chain_size; 2088 2089 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) { 2090 int waste; 2091 2092 waste = (i * PAGE_SIZE) % class_size; 2093 if (waste < min_waste) { 2094 min_waste = waste; 2095 chain_size = i; 2096 } 2097 } 2098 2099 return chain_size; 2100 } 2101 2102 /** 2103 * zs_create_pool - Creates an allocation pool to work from. 2104 * @name: pool name to be created 2105 * 2106 * This function must be called before anything when using 2107 * the zsmalloc allocator. 2108 * 2109 * On success, a pointer to the newly created pool is returned, 2110 * otherwise NULL. 2111 */ 2112 struct zs_pool *zs_create_pool(const char *name) 2113 { 2114 int i; 2115 struct zs_pool *pool; 2116 struct size_class *prev_class = NULL; 2117 2118 pool = kzalloc(sizeof(*pool), GFP_KERNEL); 2119 if (!pool) 2120 return NULL; 2121 2122 init_deferred_free(pool); 2123 rwlock_init(&pool->migrate_lock); 2124 atomic_set(&pool->compaction_in_progress, 0); 2125 2126 pool->name = kstrdup(name, GFP_KERNEL); 2127 if (!pool->name) 2128 goto err; 2129 2130 if (create_cache(pool)) 2131 goto err; 2132 2133 /* 2134 * Iterate reversely, because, size of size_class that we want to use 2135 * for merging should be larger or equal to current size. 2136 */ 2137 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) { 2138 int size; 2139 int pages_per_zspage; 2140 int objs_per_zspage; 2141 struct size_class *class; 2142 int fullness; 2143 2144 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA; 2145 if (size > ZS_MAX_ALLOC_SIZE) 2146 size = ZS_MAX_ALLOC_SIZE; 2147 pages_per_zspage = calculate_zspage_chain_size(size); 2148 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size; 2149 2150 /* 2151 * We iterate from biggest down to smallest classes, 2152 * so huge_class_size holds the size of the first huge 2153 * class. Any object bigger than or equal to that will 2154 * endup in the huge class. 2155 */ 2156 if (pages_per_zspage != 1 && objs_per_zspage != 1 && 2157 !huge_class_size) { 2158 huge_class_size = size; 2159 /* 2160 * The object uses ZS_HANDLE_SIZE bytes to store the 2161 * handle. We need to subtract it, because zs_malloc() 2162 * unconditionally adds handle size before it performs 2163 * size class search - so object may be smaller than 2164 * huge class size, yet it still can end up in the huge 2165 * class because it grows by ZS_HANDLE_SIZE extra bytes 2166 * right before class lookup. 2167 */ 2168 huge_class_size -= (ZS_HANDLE_SIZE - 1); 2169 } 2170 2171 /* 2172 * size_class is used for normal zsmalloc operation such 2173 * as alloc/free for that size. Although it is natural that we 2174 * have one size_class for each size, there is a chance that we 2175 * can get more memory utilization if we use one size_class for 2176 * many different sizes whose size_class have same 2177 * characteristics. So, we makes size_class point to 2178 * previous size_class if possible. 2179 */ 2180 if (prev_class) { 2181 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) { 2182 pool->size_class[i] = prev_class; 2183 continue; 2184 } 2185 } 2186 2187 class = kzalloc(sizeof(struct size_class), GFP_KERNEL); 2188 if (!class) 2189 goto err; 2190 2191 class->size = size; 2192 class->index = i; 2193 class->pages_per_zspage = pages_per_zspage; 2194 class->objs_per_zspage = objs_per_zspage; 2195 spin_lock_init(&class->lock); 2196 pool->size_class[i] = class; 2197 2198 fullness = ZS_INUSE_RATIO_0; 2199 while (fullness < NR_FULLNESS_GROUPS) { 2200 INIT_LIST_HEAD(&class->fullness_list[fullness]); 2201 fullness++; 2202 } 2203 2204 prev_class = class; 2205 } 2206 2207 /* debug only, don't abort if it fails */ 2208 zs_pool_stat_create(pool, name); 2209 2210 /* 2211 * Not critical since shrinker is only used to trigger internal 2212 * defragmentation of the pool which is pretty optional thing. If 2213 * registration fails we still can use the pool normally and user can 2214 * trigger compaction manually. Thus, ignore return code. 2215 */ 2216 zs_register_shrinker(pool); 2217 2218 return pool; 2219 2220 err: 2221 zs_destroy_pool(pool); 2222 return NULL; 2223 } 2224 EXPORT_SYMBOL_GPL(zs_create_pool); 2225 2226 void zs_destroy_pool(struct zs_pool *pool) 2227 { 2228 int i; 2229 2230 zs_unregister_shrinker(pool); 2231 zs_flush_migration(pool); 2232 zs_pool_stat_destroy(pool); 2233 2234 for (i = 0; i < ZS_SIZE_CLASSES; i++) { 2235 int fg; 2236 struct size_class *class = pool->size_class[i]; 2237 2238 if (!class) 2239 continue; 2240 2241 if (class->index != i) 2242 continue; 2243 2244 for (fg = ZS_INUSE_RATIO_0; fg < NR_FULLNESS_GROUPS; fg++) { 2245 if (list_empty(&class->fullness_list[fg])) 2246 continue; 2247 2248 pr_err("Class-%d fullness group %d is not empty\n", 2249 class->size, fg); 2250 } 2251 kfree(class); 2252 } 2253 2254 destroy_cache(pool); 2255 kfree(pool->name); 2256 kfree(pool); 2257 } 2258 EXPORT_SYMBOL_GPL(zs_destroy_pool); 2259 2260 static int __init zs_init(void) 2261 { 2262 int ret; 2263 2264 ret = cpuhp_setup_state(CPUHP_MM_ZS_PREPARE, "mm/zsmalloc:prepare", 2265 zs_cpu_prepare, zs_cpu_dead); 2266 if (ret) 2267 goto out; 2268 2269 #ifdef CONFIG_ZPOOL 2270 zpool_register_driver(&zs_zpool_driver); 2271 #endif 2272 2273 zs_stat_init(); 2274 2275 return 0; 2276 2277 out: 2278 return ret; 2279 } 2280 2281 static void __exit zs_exit(void) 2282 { 2283 #ifdef CONFIG_ZPOOL 2284 zpool_unregister_driver(&zs_zpool_driver); 2285 #endif 2286 cpuhp_remove_state(CPUHP_MM_ZS_PREPARE); 2287 2288 zs_stat_exit(); 2289 } 2290 2291 module_init(zs_init); 2292 module_exit(zs_exit); 2293 2294 MODULE_LICENSE("Dual BSD/GPL"); 2295 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); 2296 MODULE_DESCRIPTION("zsmalloc memory allocator"); 2297