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 Gupta | 2db51da | 2012-06-09 17:41:14 -0700 | [diff] [blame] | 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: |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 19 | * page->private: points to zspage |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 20 | * page->freelist(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. |
Ganesh Mahendran | fd85446 | 2016-07-28 15:47:54 -0700 | [diff] [blame] | 23 | * page->units: first object offset in a subpage of zspage |
Nitin Gupta | 2db51da | 2012-06-09 17:41:14 -0700 | [diff] [blame] | 24 | * |
| 25 | * Usage of struct page flags: |
| 26 | * PG_private: identifies the first component page |
| 27 | * PG_private2: identifies the last component page |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 28 | * PG_owner_priv_1: indentifies the huge component page |
Nitin Gupta | 2db51da | 2012-06-09 17:41:14 -0700 | [diff] [blame] | 29 | * |
| 30 | */ |
| 31 | |
Dan Streetman | 4abaac9 | 2016-05-26 15:16:27 -0700 | [diff] [blame] | 32 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 33 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 34 | #include <linux/module.h> |
| 35 | #include <linux/kernel.h> |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 36 | #include <linux/sched.h> |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 37 | #include <linux/bitops.h> |
| 38 | #include <linux/errno.h> |
| 39 | #include <linux/highmem.h> |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 40 | #include <linux/string.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <asm/tlbflush.h> |
| 43 | #include <asm/pgtable.h> |
| 44 | #include <linux/cpumask.h> |
| 45 | #include <linux/cpu.h> |
Seth Jennings | 0cbb613 | 2012-02-13 08:47:49 -0600 | [diff] [blame] | 46 | #include <linux/vmalloc.h> |
Sergey Senozhatsky | 759b26b | 2015-11-06 16:29:29 -0800 | [diff] [blame] | 47 | #include <linux/preempt.h> |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 48 | #include <linux/spinlock.h> |
| 49 | #include <linux/types.h> |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 50 | #include <linux/debugfs.h> |
Minchan Kim | bcf1647 | 2014-01-30 15:45:50 -0800 | [diff] [blame] | 51 | #include <linux/zsmalloc.h> |
Dan Streetman | c795779 | 2014-08-06 16:08:38 -0700 | [diff] [blame] | 52 | #include <linux/zpool.h> |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 53 | #include <linux/mount.h> |
Minchan Kim | dd4123f | 2016-07-26 15:26:50 -0700 | [diff] [blame] | 54 | #include <linux/migrate.h> |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 55 | #include <linux/pagemap.h> |
| 56 | |
| 57 | #define ZSPAGE_MAGIC 0x58 |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * This must be power of 2 and greater than of equal to sizeof(link_free). |
| 61 | * These two conditions ensure that any 'struct link_free' itself doesn't |
| 62 | * span more than 1 page which avoids complex case of mapping 2 pages simply |
| 63 | * to restore link_free pointer values. |
| 64 | */ |
| 65 | #define ZS_ALIGN 8 |
| 66 | |
| 67 | /* |
| 68 | * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single) |
| 69 | * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N. |
| 70 | */ |
| 71 | #define ZS_MAX_ZSPAGE_ORDER 2 |
| 72 | #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER) |
| 73 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 74 | #define ZS_HANDLE_SIZE (sizeof(unsigned long)) |
| 75 | |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 76 | /* |
| 77 | * Object location (<PFN>, <obj_idx>) is encoded as |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 78 | * as single (unsigned long) handle value. |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 79 | * |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 80 | * Note that object index <obj_idx> starts from 0. |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 81 | * |
| 82 | * This is made more complicated by various memory models and PAE. |
| 83 | */ |
| 84 | |
| 85 | #ifndef MAX_PHYSMEM_BITS |
| 86 | #ifdef CONFIG_HIGHMEM64G |
| 87 | #define MAX_PHYSMEM_BITS 36 |
| 88 | #else /* !CONFIG_HIGHMEM64G */ |
| 89 | /* |
| 90 | * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just |
| 91 | * be PAGE_SHIFT |
| 92 | */ |
| 93 | #define MAX_PHYSMEM_BITS BITS_PER_LONG |
| 94 | #endif |
| 95 | #endif |
| 96 | #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT) |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * Memory for allocating for handle keeps object position by |
| 100 | * encoding <page, obj_idx> and the encoded value has a room |
| 101 | * in least bit(ie, look at obj_to_location). |
| 102 | * We use the bit to synchronize between object access by |
| 103 | * user and migration. |
| 104 | */ |
| 105 | #define HANDLE_PIN_BIT 0 |
| 106 | |
| 107 | /* |
| 108 | * Head in allocated object should have OBJ_ALLOCATED_TAG |
| 109 | * to identify the object was allocated or not. |
| 110 | * It's okay to add the status bit in the least bit because |
| 111 | * header keeps handle which is 4byte-aligned address so we |
| 112 | * have room for two bit at least. |
| 113 | */ |
| 114 | #define OBJ_ALLOCATED_TAG 1 |
| 115 | #define OBJ_TAG_BITS 1 |
| 116 | #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS) |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 117 | #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1) |
| 118 | |
| 119 | #define MAX(a, b) ((a) >= (b) ? (a) : (b)) |
| 120 | /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */ |
| 121 | #define ZS_MIN_ALLOC_SIZE \ |
| 122 | MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS)) |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 123 | /* each chunk includes extra space to keep handle */ |
Minchan Kim | 7b60a68 | 2015-04-15 16:15:39 -0700 | [diff] [blame] | 124 | #define ZS_MAX_ALLOC_SIZE PAGE_SIZE |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 125 | |
| 126 | /* |
Weijie Yang | 7eb5251 | 2014-06-04 16:11:08 -0700 | [diff] [blame] | 127 | * 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] | 128 | * trader-off here: |
| 129 | * - Large number of size classes is potentially wasteful as free page are |
| 130 | * spread across these classes |
| 131 | * - Small number of size classes causes large internal fragmentation |
| 132 | * - Probably its better to use specific size classes (empirically |
| 133 | * determined). NOTE: all those class sizes must be set as multiple of |
| 134 | * ZS_ALIGN to make sure link_free itself never has to span 2 pages. |
| 135 | * |
| 136 | * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN |
| 137 | * (reason above) |
| 138 | */ |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 139 | #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS) |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 140 | |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 141 | enum fullness_group { |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 142 | ZS_EMPTY, |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 143 | ZS_ALMOST_EMPTY, |
| 144 | ZS_ALMOST_FULL, |
| 145 | ZS_FULL, |
| 146 | NR_ZS_FULLNESS, |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 147 | }; |
| 148 | |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 149 | enum zs_stat_type { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 150 | CLASS_EMPTY, |
| 151 | CLASS_ALMOST_EMPTY, |
| 152 | CLASS_ALMOST_FULL, |
| 153 | CLASS_FULL, |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 154 | OBJ_ALLOCATED, |
| 155 | OBJ_USED, |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 156 | NR_ZS_STAT_TYPE, |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 157 | }; |
| 158 | |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 159 | struct zs_size_stat { |
| 160 | unsigned long objs[NR_ZS_STAT_TYPE]; |
| 161 | }; |
| 162 | |
Sergey Senozhatsky | 5724459 | 2015-09-08 15:04:27 -0700 | [diff] [blame] | 163 | #ifdef CONFIG_ZSMALLOC_STAT |
| 164 | static struct dentry *zs_stat_root; |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 165 | #endif |
| 166 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 167 | #ifdef CONFIG_COMPACTION |
| 168 | static struct vfsmount *zsmalloc_mnt; |
| 169 | #endif |
| 170 | |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 171 | /* |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 172 | * number of size_classes |
| 173 | */ |
| 174 | static int zs_size_classes; |
| 175 | |
| 176 | /* |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 177 | * We assign a page to ZS_ALMOST_EMPTY fullness group when: |
| 178 | * n <= N / f, where |
| 179 | * n = number of allocated objects |
| 180 | * N = total number of objects zspage can store |
Wang Sheng-Hui | 6dd9737 | 2014-10-09 15:29:59 -0700 | [diff] [blame] | 181 | * f = fullness_threshold_frac |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 182 | * |
| 183 | * Similarly, we assign zspage to: |
| 184 | * ZS_ALMOST_FULL when n > N / f |
| 185 | * ZS_EMPTY when n == 0 |
| 186 | * ZS_FULL when n == N |
| 187 | * |
| 188 | * (see: fix_fullness_group()) |
| 189 | */ |
| 190 | static const int fullness_threshold_frac = 4; |
Sergey Senozhatsky | da33b2a | 2018-04-05 16:24:43 -0700 | [diff] [blame^] | 191 | static size_t huge_class_size; |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 192 | |
| 193 | struct size_class { |
Sergey Senozhatsky | 5724459 | 2015-09-08 15:04:27 -0700 | [diff] [blame] | 194 | spinlock_t lock; |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 195 | struct list_head fullness_list[NR_ZS_FULLNESS]; |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 196 | /* |
| 197 | * Size of objects stored in this class. Must be multiple |
| 198 | * of ZS_ALIGN. |
| 199 | */ |
| 200 | int size; |
Minchan Kim | 1fc6e27 | 2016-07-26 15:23:11 -0700 | [diff] [blame] | 201 | int objs_per_zspage; |
Weijie Yang | 7dfa461 | 2016-01-14 15:22:40 -0800 | [diff] [blame] | 202 | /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */ |
| 203 | int pages_per_zspage; |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 204 | |
| 205 | unsigned int index; |
| 206 | struct zs_size_stat stats; |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 207 | }; |
| 208 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 209 | /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */ |
| 210 | static void SetPageHugeObject(struct page *page) |
| 211 | { |
| 212 | SetPageOwnerPriv1(page); |
| 213 | } |
| 214 | |
| 215 | static void ClearPageHugeObject(struct page *page) |
| 216 | { |
| 217 | ClearPageOwnerPriv1(page); |
| 218 | } |
| 219 | |
| 220 | static int PageHugeObject(struct page *page) |
| 221 | { |
| 222 | return PageOwnerPriv1(page); |
| 223 | } |
| 224 | |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 225 | /* |
| 226 | * Placed within free objects to form a singly linked list. |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 227 | * For every zspage, zspage->freeobj gives head of this list. |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 228 | * |
| 229 | * This must be power of 2 and less than or equal to ZS_ALIGN |
| 230 | */ |
| 231 | struct link_free { |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 232 | union { |
| 233 | /* |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 234 | * Free object index; |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 235 | * It's valid for non-allocated object |
| 236 | */ |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 237 | unsigned long next; |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 238 | /* |
| 239 | * Handle of allocated object. |
| 240 | */ |
| 241 | unsigned long handle; |
| 242 | }; |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | struct zs_pool { |
Sergey SENOZHATSKY | 6f3526d | 2015-11-06 16:29:21 -0800 | [diff] [blame] | 246 | const char *name; |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 247 | |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 248 | struct size_class **size_class; |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 249 | struct kmem_cache *handle_cachep; |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 250 | struct kmem_cache *zspage_cachep; |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 251 | |
Minchan Kim | 13de893 | 2014-10-09 15:29:48 -0700 | [diff] [blame] | 252 | atomic_long_t pages_allocated; |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 253 | |
Sergey Senozhatsky | 7d3f393 | 2015-09-08 15:04:35 -0700 | [diff] [blame] | 254 | struct zs_pool_stats stats; |
Sergey Senozhatsky | ab9d306 | 2015-09-08 15:04:41 -0700 | [diff] [blame] | 255 | |
| 256 | /* Compact classes */ |
| 257 | struct shrinker shrinker; |
| 258 | /* |
| 259 | * To signify that register_shrinker() was successful |
| 260 | * and unregister_shrinker() will not Oops. |
| 261 | */ |
| 262 | bool shrinker_enabled; |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 263 | #ifdef CONFIG_ZSMALLOC_STAT |
| 264 | struct dentry *stat_dentry; |
| 265 | #endif |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 266 | #ifdef CONFIG_COMPACTION |
| 267 | struct inode *inode; |
| 268 | struct work_struct free_work; |
| 269 | #endif |
Seth Jennings | 0959c63 | 2012-08-08 15:12:17 +0900 | [diff] [blame] | 270 | }; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 271 | |
| 272 | /* |
| 273 | * A zspage's class index and fullness group |
| 274 | * are encoded in its (first)page->mapping |
| 275 | */ |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 276 | #define FULLNESS_BITS 2 |
| 277 | #define CLASS_BITS 8 |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 278 | #define ISOLATED_BITS 3 |
| 279 | #define MAGIC_VAL_BITS 8 |
Minchan Kim | 4f42047 | 2016-07-26 15:23:17 -0700 | [diff] [blame] | 280 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 281 | struct zspage { |
| 282 | struct { |
| 283 | unsigned int fullness:FULLNESS_BITS; |
Minchan Kim | d19f745 | 2017-04-13 14:56:40 -0700 | [diff] [blame] | 284 | unsigned int class:CLASS_BITS + 1; |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 285 | unsigned int isolated:ISOLATED_BITS; |
| 286 | unsigned int magic:MAGIC_VAL_BITS; |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 287 | }; |
| 288 | unsigned int inuse; |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 289 | unsigned int freeobj; |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 290 | struct page *first_page; |
| 291 | struct list_head list; /* fullness list */ |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 292 | #ifdef CONFIG_COMPACTION |
| 293 | rwlock_t lock; |
| 294 | #endif |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 295 | }; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 296 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 297 | struct mapping_area { |
Minchan Kim | 1b945ae | 2013-12-11 11:04:36 +0900 | [diff] [blame] | 298 | #ifdef CONFIG_PGTABLE_MAPPING |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 299 | struct vm_struct *vm; /* vm area for mapping object that span pages */ |
| 300 | #else |
| 301 | char *vm_buf; /* copy buffer for objects that span pages */ |
| 302 | #endif |
| 303 | char *vm_addr; /* address of kmap_atomic()'ed pages */ |
| 304 | enum zs_mapmode vm_mm; /* mapping mode */ |
| 305 | }; |
| 306 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 307 | #ifdef CONFIG_COMPACTION |
| 308 | static int zs_register_migration(struct zs_pool *pool); |
| 309 | static void zs_unregister_migration(struct zs_pool *pool); |
| 310 | static void migrate_lock_init(struct zspage *zspage); |
| 311 | static void migrate_read_lock(struct zspage *zspage); |
| 312 | static void migrate_read_unlock(struct zspage *zspage); |
| 313 | static void kick_deferred_free(struct zs_pool *pool); |
| 314 | static void init_deferred_free(struct zs_pool *pool); |
| 315 | static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage); |
| 316 | #else |
| 317 | static int zsmalloc_mount(void) { return 0; } |
| 318 | static void zsmalloc_unmount(void) {} |
| 319 | static int zs_register_migration(struct zs_pool *pool) { return 0; } |
| 320 | static void zs_unregister_migration(struct zs_pool *pool) {} |
| 321 | static void migrate_lock_init(struct zspage *zspage) {} |
| 322 | static void migrate_read_lock(struct zspage *zspage) {} |
| 323 | static void migrate_read_unlock(struct zspage *zspage) {} |
| 324 | static void kick_deferred_free(struct zs_pool *pool) {} |
| 325 | static void init_deferred_free(struct zs_pool *pool) {} |
| 326 | static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {} |
| 327 | #endif |
| 328 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 329 | static int create_cache(struct zs_pool *pool) |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 330 | { |
| 331 | pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE, |
| 332 | 0, 0, NULL); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 333 | if (!pool->handle_cachep) |
| 334 | return 1; |
| 335 | |
| 336 | pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage), |
| 337 | 0, 0, NULL); |
| 338 | if (!pool->zspage_cachep) { |
| 339 | kmem_cache_destroy(pool->handle_cachep); |
| 340 | pool->handle_cachep = NULL; |
| 341 | return 1; |
| 342 | } |
| 343 | |
| 344 | return 0; |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 347 | static void destroy_cache(struct zs_pool *pool) |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 348 | { |
Sergey Senozhatsky | cd10add | 2015-09-08 15:04:55 -0700 | [diff] [blame] | 349 | kmem_cache_destroy(pool->handle_cachep); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 350 | kmem_cache_destroy(pool->zspage_cachep); |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 353 | static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp) |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 354 | { |
| 355 | return (unsigned long)kmem_cache_alloc(pool->handle_cachep, |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 356 | gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE)); |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 359 | static void cache_free_handle(struct zs_pool *pool, unsigned long handle) |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 360 | { |
| 361 | kmem_cache_free(pool->handle_cachep, (void *)handle); |
| 362 | } |
| 363 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 364 | static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags) |
| 365 | { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 366 | return kmem_cache_alloc(pool->zspage_cachep, |
| 367 | flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE)); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage) |
| 371 | { |
| 372 | kmem_cache_free(pool->zspage_cachep, zspage); |
| 373 | } |
| 374 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 375 | static void record_obj(unsigned long handle, unsigned long obj) |
| 376 | { |
Junil Lee | c102f07 | 2016-01-20 14:58:18 -0800 | [diff] [blame] | 377 | /* |
| 378 | * lsb of @obj represents handle lock while other bits |
| 379 | * represent object value the handle is pointing so |
| 380 | * updating shouldn't do store tearing. |
| 381 | */ |
| 382 | WRITE_ONCE(*(unsigned long *)handle, obj); |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Dan Streetman | c795779 | 2014-08-06 16:08:38 -0700 | [diff] [blame] | 385 | /* zpool driver */ |
| 386 | |
| 387 | #ifdef CONFIG_ZPOOL |
| 388 | |
Sergey SENOZHATSKY | 6f3526d | 2015-11-06 16:29:21 -0800 | [diff] [blame] | 389 | static void *zs_zpool_create(const char *name, gfp_t gfp, |
Krzysztof Kozlowski | 7867277 | 2015-09-08 15:05:03 -0700 | [diff] [blame] | 390 | const struct zpool_ops *zpool_ops, |
Dan Streetman | 479305f | 2015-06-25 15:00:40 -0700 | [diff] [blame] | 391 | struct zpool *zpool) |
Dan Streetman | c795779 | 2014-08-06 16:08:38 -0700 | [diff] [blame] | 392 | { |
Sergey Senozhatsky | d0d8da2 | 2016-05-20 16:59:48 -0700 | [diff] [blame] | 393 | /* |
| 394 | * Ignore global gfp flags: zs_malloc() may be invoked from |
| 395 | * different contexts and its caller must provide a valid |
| 396 | * gfp mask. |
| 397 | */ |
| 398 | return zs_create_pool(name); |
Dan Streetman | c795779 | 2014-08-06 16:08:38 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static void zs_zpool_destroy(void *pool) |
| 402 | { |
| 403 | zs_destroy_pool(pool); |
| 404 | } |
| 405 | |
| 406 | static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp, |
| 407 | unsigned long *handle) |
| 408 | { |
Sergey Senozhatsky | d0d8da2 | 2016-05-20 16:59:48 -0700 | [diff] [blame] | 409 | *handle = zs_malloc(pool, size, gfp); |
Dan Streetman | c795779 | 2014-08-06 16:08:38 -0700 | [diff] [blame] | 410 | return *handle ? 0 : -1; |
| 411 | } |
| 412 | static void zs_zpool_free(void *pool, unsigned long handle) |
| 413 | { |
| 414 | zs_free(pool, handle); |
| 415 | } |
| 416 | |
| 417 | static int zs_zpool_shrink(void *pool, unsigned int pages, |
| 418 | unsigned int *reclaimed) |
| 419 | { |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | |
| 423 | static void *zs_zpool_map(void *pool, unsigned long handle, |
| 424 | enum zpool_mapmode mm) |
| 425 | { |
| 426 | enum zs_mapmode zs_mm; |
| 427 | |
| 428 | switch (mm) { |
| 429 | case ZPOOL_MM_RO: |
| 430 | zs_mm = ZS_MM_RO; |
| 431 | break; |
| 432 | case ZPOOL_MM_WO: |
| 433 | zs_mm = ZS_MM_WO; |
| 434 | break; |
| 435 | case ZPOOL_MM_RW: /* fallthru */ |
| 436 | default: |
| 437 | zs_mm = ZS_MM_RW; |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | return zs_map_object(pool, handle, zs_mm); |
| 442 | } |
| 443 | static void zs_zpool_unmap(void *pool, unsigned long handle) |
| 444 | { |
| 445 | zs_unmap_object(pool, handle); |
| 446 | } |
| 447 | |
| 448 | static u64 zs_zpool_total_size(void *pool) |
| 449 | { |
Minchan Kim | 722cdc1 | 2014-10-09 15:29:50 -0700 | [diff] [blame] | 450 | return zs_get_total_pages(pool) << PAGE_SHIFT; |
Dan Streetman | c795779 | 2014-08-06 16:08:38 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static struct zpool_driver zs_zpool_driver = { |
| 454 | .type = "zsmalloc", |
| 455 | .owner = THIS_MODULE, |
| 456 | .create = zs_zpool_create, |
| 457 | .destroy = zs_zpool_destroy, |
| 458 | .malloc = zs_zpool_malloc, |
| 459 | .free = zs_zpool_free, |
| 460 | .shrink = zs_zpool_shrink, |
| 461 | .map = zs_zpool_map, |
| 462 | .unmap = zs_zpool_unmap, |
| 463 | .total_size = zs_zpool_total_size, |
| 464 | }; |
| 465 | |
Kees Cook | 137f8cf | 2014-08-29 15:18:40 -0700 | [diff] [blame] | 466 | MODULE_ALIAS("zpool-zsmalloc"); |
Dan Streetman | c795779 | 2014-08-06 16:08:38 -0700 | [diff] [blame] | 467 | #endif /* CONFIG_ZPOOL */ |
| 468 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 469 | /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */ |
| 470 | static DEFINE_PER_CPU(struct mapping_area, zs_map_area); |
| 471 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 472 | static bool is_zspage_isolated(struct zspage *zspage) |
| 473 | { |
| 474 | return zspage->isolated; |
| 475 | } |
| 476 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 477 | static int is_first_page(struct page *page) |
| 478 | { |
Minchan Kim | a27545bf | 2012-04-25 15:23:09 +0900 | [diff] [blame] | 479 | return PagePrivate(page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 480 | } |
| 481 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 482 | /* Protected by class->lock */ |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 483 | static inline int get_zspage_inuse(struct zspage *zspage) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 484 | { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 485 | return zspage->inuse; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 486 | } |
| 487 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 488 | static inline void set_zspage_inuse(struct zspage *zspage, int val) |
Minchan Kim | 4f42047 | 2016-07-26 15:23:17 -0700 | [diff] [blame] | 489 | { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 490 | zspage->inuse = val; |
Minchan Kim | 4f42047 | 2016-07-26 15:23:17 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 493 | static inline void mod_zspage_inuse(struct zspage *zspage, int val) |
Minchan Kim | 4f42047 | 2016-07-26 15:23:17 -0700 | [diff] [blame] | 494 | { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 495 | zspage->inuse += val; |
Minchan Kim | 4f42047 | 2016-07-26 15:23:17 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 498 | static inline struct page *get_first_page(struct zspage *zspage) |
| 499 | { |
| 500 | struct page *first_page = zspage->first_page; |
| 501 | |
| 502 | VM_BUG_ON_PAGE(!is_first_page(first_page), first_page); |
| 503 | return first_page; |
| 504 | } |
| 505 | |
Minchan Kim | 4f42047 | 2016-07-26 15:23:17 -0700 | [diff] [blame] | 506 | static inline int get_first_obj_offset(struct page *page) |
| 507 | { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 508 | return page->units; |
Minchan Kim | 4f42047 | 2016-07-26 15:23:17 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | static inline void set_first_obj_offset(struct page *page, int offset) |
| 512 | { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 513 | page->units = offset; |
Minchan Kim | 4f42047 | 2016-07-26 15:23:17 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 516 | static inline unsigned int get_freeobj(struct zspage *zspage) |
Minchan Kim | 4f42047 | 2016-07-26 15:23:17 -0700 | [diff] [blame] | 517 | { |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 518 | return zspage->freeobj; |
Minchan Kim | 4f42047 | 2016-07-26 15:23:17 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 521 | static inline void set_freeobj(struct zspage *zspage, unsigned int obj) |
Minchan Kim | 4f42047 | 2016-07-26 15:23:17 -0700 | [diff] [blame] | 522 | { |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 523 | zspage->freeobj = obj; |
Minchan Kim | 4f42047 | 2016-07-26 15:23:17 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 526 | static void get_zspage_mapping(struct zspage *zspage, |
Minchan Kim | a420946 | 2016-05-20 16:59:36 -0700 | [diff] [blame] | 527 | unsigned int *class_idx, |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 528 | enum fullness_group *fullness) |
| 529 | { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 530 | BUG_ON(zspage->magic != ZSPAGE_MAGIC); |
| 531 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 532 | *fullness = zspage->fullness; |
| 533 | *class_idx = zspage->class; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 534 | } |
| 535 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 536 | static void set_zspage_mapping(struct zspage *zspage, |
Minchan Kim | a420946 | 2016-05-20 16:59:36 -0700 | [diff] [blame] | 537 | unsigned int class_idx, |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 538 | enum fullness_group fullness) |
| 539 | { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 540 | zspage->class = class_idx; |
| 541 | zspage->fullness = fullness; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 542 | } |
| 543 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 544 | /* |
| 545 | * zsmalloc divides the pool into various size classes where each |
| 546 | * class maintains a list of zspages where each zspage is divided |
| 547 | * into equal sized chunks. Each allocation falls into one of these |
| 548 | * classes depending on its size. This function returns index of the |
| 549 | * size class which has chunk size big enough to hold the give size. |
| 550 | */ |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 551 | static int get_size_class_index(int size) |
| 552 | { |
| 553 | int idx = 0; |
| 554 | |
| 555 | if (likely(size > ZS_MIN_ALLOC_SIZE)) |
| 556 | idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE, |
| 557 | ZS_SIZE_CLASS_DELTA); |
| 558 | |
Minchan Kim | 7b60a68 | 2015-04-15 16:15:39 -0700 | [diff] [blame] | 559 | return min(zs_size_classes - 1, idx); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 560 | } |
| 561 | |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 562 | static inline void zs_stat_inc(struct size_class *class, |
| 563 | enum zs_stat_type type, unsigned long cnt) |
| 564 | { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 565 | class->stats.objs[type] += cnt; |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static inline void zs_stat_dec(struct size_class *class, |
| 569 | enum zs_stat_type type, unsigned long cnt) |
| 570 | { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 571 | class->stats.objs[type] -= cnt; |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | static inline unsigned long zs_stat_get(struct size_class *class, |
| 575 | enum zs_stat_type type) |
| 576 | { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 577 | return class->stats.objs[type]; |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Sergey Senozhatsky | 5724459 | 2015-09-08 15:04:27 -0700 | [diff] [blame] | 580 | #ifdef CONFIG_ZSMALLOC_STAT |
| 581 | |
Dan Streetman | 4abaac9 | 2016-05-26 15:16:27 -0700 | [diff] [blame] | 582 | static void __init zs_stat_init(void) |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 583 | { |
Dan Streetman | 4abaac9 | 2016-05-26 15:16:27 -0700 | [diff] [blame] | 584 | if (!debugfs_initialized()) { |
| 585 | pr_warn("debugfs not available, stat dir not created\n"); |
| 586 | return; |
| 587 | } |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 588 | |
| 589 | zs_stat_root = debugfs_create_dir("zsmalloc", NULL); |
| 590 | if (!zs_stat_root) |
Dan Streetman | 4abaac9 | 2016-05-26 15:16:27 -0700 | [diff] [blame] | 591 | pr_warn("debugfs 'zsmalloc' stat dir creation failed\n"); |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | static void __exit zs_stat_exit(void) |
| 595 | { |
| 596 | debugfs_remove_recursive(zs_stat_root); |
| 597 | } |
| 598 | |
Sergey Senozhatsky | 1120ed5 | 2016-03-17 14:20:42 -0700 | [diff] [blame] | 599 | static unsigned long zs_can_compact(struct size_class *class); |
| 600 | |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 601 | static int zs_stats_size_show(struct seq_file *s, void *v) |
| 602 | { |
| 603 | int i; |
| 604 | struct zs_pool *pool = s->private; |
| 605 | struct size_class *class; |
| 606 | int objs_per_zspage; |
| 607 | unsigned long class_almost_full, class_almost_empty; |
Sergey Senozhatsky | 1120ed5 | 2016-03-17 14:20:42 -0700 | [diff] [blame] | 608 | unsigned long obj_allocated, obj_used, pages_used, freeable; |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 609 | unsigned long total_class_almost_full = 0, total_class_almost_empty = 0; |
| 610 | unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0; |
Sergey Senozhatsky | 1120ed5 | 2016-03-17 14:20:42 -0700 | [diff] [blame] | 611 | unsigned long total_freeable = 0; |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 612 | |
Sergey Senozhatsky | 1120ed5 | 2016-03-17 14:20:42 -0700 | [diff] [blame] | 613 | seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n", |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 614 | "class", "size", "almost_full", "almost_empty", |
| 615 | "obj_allocated", "obj_used", "pages_used", |
Sergey Senozhatsky | 1120ed5 | 2016-03-17 14:20:42 -0700 | [diff] [blame] | 616 | "pages_per_zspage", "freeable"); |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 617 | |
| 618 | for (i = 0; i < zs_size_classes; i++) { |
| 619 | class = pool->size_class[i]; |
| 620 | |
| 621 | if (class->index != i) |
| 622 | continue; |
| 623 | |
| 624 | spin_lock(&class->lock); |
| 625 | class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL); |
| 626 | class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY); |
| 627 | obj_allocated = zs_stat_get(class, OBJ_ALLOCATED); |
| 628 | obj_used = zs_stat_get(class, OBJ_USED); |
Sergey Senozhatsky | 1120ed5 | 2016-03-17 14:20:42 -0700 | [diff] [blame] | 629 | freeable = zs_can_compact(class); |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 630 | spin_unlock(&class->lock); |
| 631 | |
Ganesh Mahendran | b4fd07a | 2016-07-28 15:47:49 -0700 | [diff] [blame] | 632 | objs_per_zspage = class->objs_per_zspage; |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 633 | pages_used = obj_allocated / objs_per_zspage * |
| 634 | class->pages_per_zspage; |
| 635 | |
Sergey Senozhatsky | 1120ed5 | 2016-03-17 14:20:42 -0700 | [diff] [blame] | 636 | seq_printf(s, " %5u %5u %11lu %12lu %13lu" |
| 637 | " %10lu %10lu %16d %8lu\n", |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 638 | i, class->size, class_almost_full, class_almost_empty, |
| 639 | obj_allocated, obj_used, pages_used, |
Sergey Senozhatsky | 1120ed5 | 2016-03-17 14:20:42 -0700 | [diff] [blame] | 640 | class->pages_per_zspage, freeable); |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 641 | |
| 642 | total_class_almost_full += class_almost_full; |
| 643 | total_class_almost_empty += class_almost_empty; |
| 644 | total_objs += obj_allocated; |
| 645 | total_used_objs += obj_used; |
| 646 | total_pages += pages_used; |
Sergey Senozhatsky | 1120ed5 | 2016-03-17 14:20:42 -0700 | [diff] [blame] | 647 | total_freeable += freeable; |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | seq_puts(s, "\n"); |
Sergey Senozhatsky | 1120ed5 | 2016-03-17 14:20:42 -0700 | [diff] [blame] | 651 | seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n", |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 652 | "Total", "", total_class_almost_full, |
| 653 | total_class_almost_empty, total_objs, |
Sergey Senozhatsky | 1120ed5 | 2016-03-17 14:20:42 -0700 | [diff] [blame] | 654 | total_used_objs, total_pages, "", total_freeable); |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static int zs_stats_size_open(struct inode *inode, struct file *file) |
| 660 | { |
| 661 | return single_open(file, zs_stats_size_show, inode->i_private); |
| 662 | } |
| 663 | |
| 664 | static const struct file_operations zs_stat_size_ops = { |
| 665 | .open = zs_stats_size_open, |
| 666 | .read = seq_read, |
| 667 | .llseek = seq_lseek, |
| 668 | .release = single_release, |
| 669 | }; |
| 670 | |
Dan Streetman | d34f615 | 2016-05-20 16:59:56 -0700 | [diff] [blame] | 671 | static void zs_pool_stat_create(struct zs_pool *pool, const char *name) |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 672 | { |
| 673 | struct dentry *entry; |
| 674 | |
Dan Streetman | 4abaac9 | 2016-05-26 15:16:27 -0700 | [diff] [blame] | 675 | if (!zs_stat_root) { |
| 676 | pr_warn("no root stat dir, not creating <%s> stat dir\n", name); |
Dan Streetman | d34f615 | 2016-05-20 16:59:56 -0700 | [diff] [blame] | 677 | return; |
Dan Streetman | 4abaac9 | 2016-05-26 15:16:27 -0700 | [diff] [blame] | 678 | } |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 679 | |
| 680 | entry = debugfs_create_dir(name, zs_stat_root); |
| 681 | if (!entry) { |
| 682 | pr_warn("debugfs dir <%s> creation failed\n", name); |
Dan Streetman | d34f615 | 2016-05-20 16:59:56 -0700 | [diff] [blame] | 683 | return; |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 684 | } |
| 685 | pool->stat_dentry = entry; |
| 686 | |
| 687 | entry = debugfs_create_file("classes", S_IFREG | S_IRUGO, |
| 688 | pool->stat_dentry, pool, &zs_stat_size_ops); |
| 689 | if (!entry) { |
| 690 | pr_warn("%s: debugfs file entry <%s> creation failed\n", |
| 691 | name, "classes"); |
Dan Streetman | 4abaac9 | 2016-05-26 15:16:27 -0700 | [diff] [blame] | 692 | debugfs_remove_recursive(pool->stat_dentry); |
| 693 | pool->stat_dentry = NULL; |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 694 | } |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | static void zs_pool_stat_destroy(struct zs_pool *pool) |
| 698 | { |
| 699 | debugfs_remove_recursive(pool->stat_dentry); |
| 700 | } |
| 701 | |
| 702 | #else /* CONFIG_ZSMALLOC_STAT */ |
Dan Streetman | 4abaac9 | 2016-05-26 15:16:27 -0700 | [diff] [blame] | 703 | static void __init zs_stat_init(void) |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 704 | { |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static void __exit zs_stat_exit(void) |
| 708 | { |
| 709 | } |
| 710 | |
Dan Streetman | d34f615 | 2016-05-20 16:59:56 -0700 | [diff] [blame] | 711 | static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name) |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 712 | { |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | static inline void zs_pool_stat_destroy(struct zs_pool *pool) |
| 716 | { |
| 717 | } |
Minchan Kim | 248ca1b | 2015-04-15 16:15:42 -0700 | [diff] [blame] | 718 | #endif |
| 719 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 720 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 721 | /* |
| 722 | * For each size class, zspages are divided into different groups |
| 723 | * depending on how "full" they are. This was done so that we could |
| 724 | * easily find empty or nearly empty zspages when we try to shrink |
| 725 | * the pool (not yet implemented). This function returns fullness |
| 726 | * status of the given page. |
| 727 | */ |
Minchan Kim | 1fc6e27 | 2016-07-26 15:23:11 -0700 | [diff] [blame] | 728 | static enum fullness_group get_fullness_group(struct size_class *class, |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 729 | struct zspage *zspage) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 730 | { |
Minchan Kim | 1fc6e27 | 2016-07-26 15:23:11 -0700 | [diff] [blame] | 731 | int inuse, objs_per_zspage; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 732 | enum fullness_group fg; |
Minchan Kim | 830e4bc | 2016-05-20 16:59:39 -0700 | [diff] [blame] | 733 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 734 | inuse = get_zspage_inuse(zspage); |
Minchan Kim | 1fc6e27 | 2016-07-26 15:23:11 -0700 | [diff] [blame] | 735 | objs_per_zspage = class->objs_per_zspage; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 736 | |
| 737 | if (inuse == 0) |
| 738 | fg = ZS_EMPTY; |
Minchan Kim | 1fc6e27 | 2016-07-26 15:23:11 -0700 | [diff] [blame] | 739 | else if (inuse == objs_per_zspage) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 740 | fg = ZS_FULL; |
Minchan Kim | 1fc6e27 | 2016-07-26 15:23:11 -0700 | [diff] [blame] | 741 | else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 742 | fg = ZS_ALMOST_EMPTY; |
| 743 | else |
| 744 | fg = ZS_ALMOST_FULL; |
| 745 | |
| 746 | return fg; |
| 747 | } |
| 748 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 749 | /* |
| 750 | * Each size class maintains various freelists and zspages are assigned |
| 751 | * to one of these freelists based on the number of live objects they |
| 752 | * have. This functions inserts the given zspage into the freelist |
| 753 | * identified by <class, fullness_group>. |
| 754 | */ |
Minchan Kim | 251cbb9 | 2016-05-20 16:59:42 -0700 | [diff] [blame] | 755 | static void insert_zspage(struct size_class *class, |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 756 | struct zspage *zspage, |
| 757 | enum fullness_group fullness) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 758 | { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 759 | struct zspage *head; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 760 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 761 | zs_stat_inc(class, fullness, 1); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 762 | head = list_first_entry_or_null(&class->fullness_list[fullness], |
| 763 | struct zspage, list); |
Sergey Senozhatsky | 58f1711 | 2015-09-08 15:04:44 -0700 | [diff] [blame] | 764 | /* |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 765 | * We want to see more ZS_FULL pages and less almost empty/full. |
| 766 | * Put pages with higher ->inuse first. |
Sergey Senozhatsky | 58f1711 | 2015-09-08 15:04:44 -0700 | [diff] [blame] | 767 | */ |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 768 | if (head) { |
| 769 | if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) { |
| 770 | list_add(&zspage->list, &head->list); |
| 771 | return; |
| 772 | } |
| 773 | } |
| 774 | list_add(&zspage->list, &class->fullness_list[fullness]); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 775 | } |
| 776 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 777 | /* |
| 778 | * This function removes the given zspage from the freelist identified |
| 779 | * by <class, fullness_group>. |
| 780 | */ |
Minchan Kim | 251cbb9 | 2016-05-20 16:59:42 -0700 | [diff] [blame] | 781 | static void remove_zspage(struct size_class *class, |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 782 | struct zspage *zspage, |
| 783 | enum fullness_group fullness) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 784 | { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 785 | VM_BUG_ON(list_empty(&class->fullness_list[fullness])); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 786 | VM_BUG_ON(is_zspage_isolated(zspage)); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 787 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 788 | list_del_init(&zspage->list); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 789 | zs_stat_dec(class, fullness, 1); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 790 | } |
| 791 | |
Nitin Cupta | c3e3e88 | 2013-12-11 11:04:37 +0900 | [diff] [blame] | 792 | /* |
| 793 | * Each size class maintains zspages in different fullness groups depending |
| 794 | * on the number of live objects they contain. When allocating or freeing |
| 795 | * objects, the fullness status of the page can change, say, from ALMOST_FULL |
| 796 | * to ALMOST_EMPTY when freeing an object. This function checks if such |
| 797 | * a status change has occurred for the given page and accordingly moves the |
| 798 | * page from the freelist of the old fullness group to that of the new |
| 799 | * fullness group. |
| 800 | */ |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame] | 801 | static enum fullness_group fix_fullness_group(struct size_class *class, |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 802 | struct zspage *zspage) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 803 | { |
| 804 | int class_idx; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 805 | enum fullness_group currfg, newfg; |
| 806 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 807 | get_zspage_mapping(zspage, &class_idx, &currfg); |
| 808 | newfg = get_fullness_group(class, zspage); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 809 | if (newfg == currfg) |
| 810 | goto out; |
| 811 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 812 | if (!is_zspage_isolated(zspage)) { |
| 813 | remove_zspage(class, zspage, currfg); |
| 814 | insert_zspage(class, zspage, newfg); |
| 815 | } |
| 816 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 817 | set_zspage_mapping(zspage, class_idx, newfg); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 818 | |
| 819 | out: |
| 820 | return newfg; |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | * We have to decide on how many pages to link together |
| 825 | * to form a zspage for each size class. This is important |
| 826 | * to reduce wastage due to unusable space left at end of |
| 827 | * each zspage which is given as: |
Yinghao Xie | 888fa374 | 2015-04-15 16:15:49 -0700 | [diff] [blame] | 828 | * wastage = Zp % class_size |
| 829 | * usage = Zp - wastage |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 830 | * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ... |
| 831 | * |
| 832 | * For example, for size class of 3/8 * PAGE_SIZE, we should |
| 833 | * link together 3 PAGE_SIZE sized pages to form a zspage |
| 834 | * since then we can perfectly fit in 8 such objects. |
| 835 | */ |
Minchan Kim | 2e3b615 | 2012-05-03 15:40:39 +0900 | [diff] [blame] | 836 | static int get_pages_per_zspage(int class_size) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 837 | { |
| 838 | int i, max_usedpc = 0; |
| 839 | /* zspage order which gives maximum used size per KB */ |
| 840 | int max_usedpc_order = 1; |
| 841 | |
Seth Jennings | 84d4faa | 2012-03-05 11:33:21 -0600 | [diff] [blame] | 842 | for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) { |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 843 | int zspage_size; |
| 844 | int waste, usedpc; |
| 845 | |
| 846 | zspage_size = i * PAGE_SIZE; |
| 847 | waste = zspage_size % class_size; |
| 848 | usedpc = (zspage_size - waste) * 100 / zspage_size; |
| 849 | |
| 850 | if (usedpc > max_usedpc) { |
| 851 | max_usedpc = usedpc; |
| 852 | max_usedpc_order = i; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | return max_usedpc_order; |
| 857 | } |
| 858 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 859 | static struct zspage *get_zspage(struct page *page) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 860 | { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 861 | struct zspage *zspage = (struct zspage *)page->private; |
| 862 | |
| 863 | BUG_ON(zspage->magic != ZSPAGE_MAGIC); |
| 864 | return zspage; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | static struct page *get_next_page(struct page *page) |
| 868 | { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 869 | if (unlikely(PageHugeObject(page))) |
| 870 | return NULL; |
| 871 | |
| 872 | return page->freelist; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 873 | } |
| 874 | |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 875 | /** |
| 876 | * obj_to_location - get (<page>, <obj_idx>) from encoded object value |
| 877 | * @page: page object resides in zspage |
| 878 | * @obj_idx: object index |
Olav Haugan | 6729687 | 2013-11-22 09:30:41 -0800 | [diff] [blame] | 879 | */ |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 880 | static void obj_to_location(unsigned long obj, struct page **page, |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 881 | unsigned int *obj_idx) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 882 | { |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 883 | obj >>= OBJ_TAG_BITS; |
| 884 | *page = pfn_to_page(obj >> OBJ_INDEX_BITS); |
| 885 | *obj_idx = (obj & OBJ_INDEX_MASK); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 886 | } |
| 887 | |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 888 | /** |
| 889 | * location_to_obj - get obj value encoded from (<page>, <obj_idx>) |
| 890 | * @page: page object resides in zspage |
| 891 | * @obj_idx: object index |
| 892 | */ |
| 893 | static unsigned long location_to_obj(struct page *page, unsigned int obj_idx) |
| 894 | { |
| 895 | unsigned long obj; |
| 896 | |
| 897 | obj = page_to_pfn(page) << OBJ_INDEX_BITS; |
| 898 | obj |= obj_idx & OBJ_INDEX_MASK; |
| 899 | obj <<= OBJ_TAG_BITS; |
| 900 | |
| 901 | return obj; |
| 902 | } |
| 903 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 904 | static unsigned long handle_to_obj(unsigned long handle) |
| 905 | { |
| 906 | return *(unsigned long *)handle; |
| 907 | } |
| 908 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 909 | static unsigned long obj_to_head(struct page *page, void *obj) |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 910 | { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 911 | if (unlikely(PageHugeObject(page))) { |
Minchan Kim | 830e4bc | 2016-05-20 16:59:39 -0700 | [diff] [blame] | 912 | VM_BUG_ON_PAGE(!is_first_page(page), page); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 913 | return page->index; |
Minchan Kim | 7b60a68 | 2015-04-15 16:15:39 -0700 | [diff] [blame] | 914 | } else |
| 915 | return *(unsigned long *)obj; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 918 | static inline int testpin_tag(unsigned long handle) |
| 919 | { |
| 920 | return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle); |
| 921 | } |
| 922 | |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 923 | static inline int trypin_tag(unsigned long handle) |
| 924 | { |
Minchan Kim | 1b8320b | 2016-07-26 15:23:14 -0700 | [diff] [blame] | 925 | return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | static void pin_tag(unsigned long handle) |
| 929 | { |
Minchan Kim | 1b8320b | 2016-07-26 15:23:14 -0700 | [diff] [blame] | 930 | bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | static void unpin_tag(unsigned long handle) |
| 934 | { |
Minchan Kim | 1b8320b | 2016-07-26 15:23:14 -0700 | [diff] [blame] | 935 | bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 938 | static void reset_page(struct page *page) |
| 939 | { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 940 | __ClearPageMovable(page); |
Ganesh Mahendran | 18fd06b | 2016-07-28 15:48:00 -0700 | [diff] [blame] | 941 | ClearPagePrivate(page); |
| 942 | ClearPagePrivate2(page); |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 943 | set_page_private(page, 0); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 944 | page_mapcount_reset(page); |
| 945 | ClearPageHugeObject(page); |
| 946 | page->freelist = NULL; |
Nitin Gupta | f4477e9 | 2012-04-02 09:13:56 -0500 | [diff] [blame] | 947 | } |
| 948 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 949 | /* |
| 950 | * To prevent zspage destroy during migration, zspage freeing should |
| 951 | * hold locks of all pages in the zspage. |
| 952 | */ |
| 953 | void lock_zspage(struct zspage *zspage) |
| 954 | { |
| 955 | struct page *page = get_first_page(zspage); |
| 956 | |
| 957 | do { |
| 958 | lock_page(page); |
| 959 | } while ((page = get_next_page(page)) != NULL); |
| 960 | } |
| 961 | |
| 962 | int trylock_zspage(struct zspage *zspage) |
| 963 | { |
| 964 | struct page *cursor, *fail; |
| 965 | |
| 966 | for (cursor = get_first_page(zspage); cursor != NULL; cursor = |
| 967 | get_next_page(cursor)) { |
| 968 | if (!trylock_page(cursor)) { |
| 969 | fail = cursor; |
| 970 | goto unlock; |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | return 1; |
| 975 | unlock: |
| 976 | for (cursor = get_first_page(zspage); cursor != fail; cursor = |
| 977 | get_next_page(cursor)) |
| 978 | unlock_page(cursor); |
| 979 | |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | static void __free_zspage(struct zs_pool *pool, struct size_class *class, |
| 984 | struct zspage *zspage) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 985 | { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 986 | struct page *page, *next; |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 987 | enum fullness_group fg; |
| 988 | unsigned int class_idx; |
| 989 | |
| 990 | get_zspage_mapping(zspage, &class_idx, &fg); |
| 991 | |
| 992 | assert_spin_locked(&class->lock); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 993 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 994 | VM_BUG_ON(get_zspage_inuse(zspage)); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 995 | VM_BUG_ON(fg != ZS_EMPTY); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 996 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 997 | next = page = get_first_page(zspage); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 998 | do { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 999 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 1000 | next = get_next_page(page); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1001 | reset_page(page); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1002 | unlock_page(page); |
Minchan Kim | 91537fe | 2016-07-26 15:24:45 -0700 | [diff] [blame] | 1003 | dec_zone_page_state(page, NR_ZSPAGES); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1004 | put_page(page); |
| 1005 | page = next; |
| 1006 | } while (page != NULL); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1007 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1008 | cache_free_zspage(pool, zspage); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1009 | |
Ganesh Mahendran | b4fd07a | 2016-07-28 15:47:49 -0700 | [diff] [blame] | 1010 | zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1011 | atomic_long_sub(class->pages_per_zspage, |
| 1012 | &pool->pages_allocated); |
| 1013 | } |
| 1014 | |
| 1015 | static void free_zspage(struct zs_pool *pool, struct size_class *class, |
| 1016 | struct zspage *zspage) |
| 1017 | { |
| 1018 | VM_BUG_ON(get_zspage_inuse(zspage)); |
| 1019 | VM_BUG_ON(list_empty(&zspage->list)); |
| 1020 | |
| 1021 | if (!trylock_zspage(zspage)) { |
| 1022 | kick_deferred_free(pool); |
| 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | remove_zspage(class, zspage, ZS_EMPTY); |
| 1027 | __free_zspage(pool, class, zspage); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /* Initialize a newly allocated zspage */ |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1031 | static void init_zspage(struct size_class *class, struct zspage *zspage) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1032 | { |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1033 | unsigned int freeobj = 1; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1034 | unsigned long off = 0; |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1035 | struct page *page = get_first_page(zspage); |
Minchan Kim | 830e4bc | 2016-05-20 16:59:39 -0700 | [diff] [blame] | 1036 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1037 | while (page) { |
| 1038 | struct page *next_page; |
| 1039 | struct link_free *link; |
Minchan Kim | af4ee5e | 2014-12-12 16:56:58 -0800 | [diff] [blame] | 1040 | void *vaddr; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1041 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1042 | set_first_obj_offset(page, off); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1043 | |
Minchan Kim | af4ee5e | 2014-12-12 16:56:58 -0800 | [diff] [blame] | 1044 | vaddr = kmap_atomic(page); |
| 1045 | link = (struct link_free *)vaddr + off / sizeof(*link); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1046 | |
Dan Streetman | 5538c56 | 2014-10-09 15:30:01 -0700 | [diff] [blame] | 1047 | while ((off += class->size) < PAGE_SIZE) { |
Minchan Kim | 3b1d9ca | 2016-07-26 15:23:37 -0700 | [diff] [blame] | 1048 | link->next = freeobj++ << OBJ_TAG_BITS; |
Dan Streetman | 5538c56 | 2014-10-09 15:30:01 -0700 | [diff] [blame] | 1049 | link += class->size / sizeof(*link); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * We now come to the last (full or partial) object on this |
| 1054 | * page, which must point to the first object on the next |
| 1055 | * page (if present) |
| 1056 | */ |
| 1057 | next_page = get_next_page(page); |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1058 | if (next_page) { |
Minchan Kim | 3b1d9ca | 2016-07-26 15:23:37 -0700 | [diff] [blame] | 1059 | link->next = freeobj++ << OBJ_TAG_BITS; |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1060 | } else { |
| 1061 | /* |
Minchan Kim | 3b1d9ca | 2016-07-26 15:23:37 -0700 | [diff] [blame] | 1062 | * Reset OBJ_TAG_BITS bit to last link to tell |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1063 | * whether it's allocated object or not. |
| 1064 | */ |
Minchan Kim | 3b1d9ca | 2016-07-26 15:23:37 -0700 | [diff] [blame] | 1065 | link->next = -1 << OBJ_TAG_BITS; |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1066 | } |
Minchan Kim | af4ee5e | 2014-12-12 16:56:58 -0800 | [diff] [blame] | 1067 | kunmap_atomic(vaddr); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1068 | page = next_page; |
Dan Streetman | 5538c56 | 2014-10-09 15:30:01 -0700 | [diff] [blame] | 1069 | off %= PAGE_SIZE; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1070 | } |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1071 | |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1072 | set_freeobj(zspage, 0); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1073 | } |
| 1074 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1075 | static void create_page_chain(struct size_class *class, struct zspage *zspage, |
| 1076 | struct page *pages[]) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1077 | { |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1078 | int i; |
| 1079 | struct page *page; |
| 1080 | struct page *prev_page = NULL; |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1081 | int nr_pages = class->pages_per_zspage; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1082 | |
| 1083 | /* |
| 1084 | * Allocate individual pages and link them together as: |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1085 | * 1. all pages are linked together using page->freelist |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1086 | * 2. each sub-page point to zspage using page->private |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1087 | * |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1088 | * we set PG_private to identify the first page (i.e. no other sub-page |
| 1089 | * has this flag set) and PG_private_2 to identify the last page. |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1090 | */ |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1091 | for (i = 0; i < nr_pages; i++) { |
| 1092 | page = pages[i]; |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1093 | set_page_private(page, (unsigned long)zspage); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1094 | page->freelist = NULL; |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1095 | if (i == 0) { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1096 | zspage->first_page = page; |
Minchan Kim | a27545bf | 2012-04-25 15:23:09 +0900 | [diff] [blame] | 1097 | SetPagePrivate(page); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1098 | if (unlikely(class->objs_per_zspage == 1 && |
| 1099 | class->pages_per_zspage == 1)) |
| 1100 | SetPageHugeObject(page); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1101 | } else { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1102 | prev_page->freelist = page; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1103 | } |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1104 | if (i == nr_pages - 1) |
Minchan Kim | a27545bf | 2012-04-25 15:23:09 +0900 | [diff] [blame] | 1105 | SetPagePrivate2(page); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1106 | prev_page = page; |
| 1107 | } |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1108 | } |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1109 | |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1110 | /* |
| 1111 | * Allocate a zspage for the given size class |
| 1112 | */ |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1113 | static struct zspage *alloc_zspage(struct zs_pool *pool, |
| 1114 | struct size_class *class, |
| 1115 | gfp_t gfp) |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1116 | { |
| 1117 | int i; |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1118 | struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE]; |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1119 | struct zspage *zspage = cache_alloc_zspage(pool, gfp); |
| 1120 | |
| 1121 | if (!zspage) |
| 1122 | return NULL; |
| 1123 | |
| 1124 | memset(zspage, 0, sizeof(struct zspage)); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1125 | zspage->magic = ZSPAGE_MAGIC; |
| 1126 | migrate_lock_init(zspage); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1127 | |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1128 | for (i = 0; i < class->pages_per_zspage; i++) { |
| 1129 | struct page *page; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1130 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1131 | page = alloc_page(gfp); |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1132 | if (!page) { |
Minchan Kim | 91537fe | 2016-07-26 15:24:45 -0700 | [diff] [blame] | 1133 | while (--i >= 0) { |
| 1134 | dec_zone_page_state(pages[i], NR_ZSPAGES); |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1135 | __free_page(pages[i]); |
Minchan Kim | 91537fe | 2016-07-26 15:24:45 -0700 | [diff] [blame] | 1136 | } |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1137 | cache_free_zspage(pool, zspage); |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1138 | return NULL; |
| 1139 | } |
Minchan Kim | 91537fe | 2016-07-26 15:24:45 -0700 | [diff] [blame] | 1140 | |
| 1141 | inc_zone_page_state(page, NR_ZSPAGES); |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1142 | pages[i] = page; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1143 | } |
| 1144 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1145 | create_page_chain(class, zspage, pages); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1146 | init_zspage(class, zspage); |
Minchan Kim | bdb0af7 | 2016-07-26 15:23:20 -0700 | [diff] [blame] | 1147 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1148 | return zspage; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1149 | } |
| 1150 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1151 | static struct zspage *find_get_zspage(struct size_class *class) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1152 | { |
| 1153 | int i; |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1154 | struct zspage *zspage; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1155 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1156 | for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1157 | zspage = list_first_entry_or_null(&class->fullness_list[i], |
| 1158 | struct zspage, list); |
| 1159 | if (zspage) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1160 | break; |
| 1161 | } |
| 1162 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1163 | return zspage; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1164 | } |
| 1165 | |
Minchan Kim | 1b945ae | 2013-12-11 11:04:36 +0900 | [diff] [blame] | 1166 | #ifdef CONFIG_PGTABLE_MAPPING |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1167 | static inline int __zs_cpu_up(struct mapping_area *area) |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 1168 | { |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1169 | /* |
| 1170 | * Make sure we don't leak memory if a cpu UP notification |
| 1171 | * and zs_init() race and both call zs_cpu_up() on the same cpu |
| 1172 | */ |
| 1173 | if (area->vm) |
| 1174 | return 0; |
| 1175 | area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL); |
| 1176 | if (!area->vm) |
| 1177 | return -ENOMEM; |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | static inline void __zs_cpu_down(struct mapping_area *area) |
| 1182 | { |
| 1183 | if (area->vm) |
| 1184 | free_vm_area(area->vm); |
| 1185 | area->vm = NULL; |
| 1186 | } |
| 1187 | |
| 1188 | static inline void *__zs_map_object(struct mapping_area *area, |
| 1189 | struct page *pages[2], int off, int size) |
| 1190 | { |
WANG Chao | f6f8ed4 | 2014-08-06 16:06:58 -0700 | [diff] [blame] | 1191 | BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages)); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1192 | area->vm_addr = area->vm->addr; |
| 1193 | return area->vm_addr + off; |
| 1194 | } |
| 1195 | |
| 1196 | static inline void __zs_unmap_object(struct mapping_area *area, |
| 1197 | struct page *pages[2], int off, int size) |
| 1198 | { |
| 1199 | unsigned long addr = (unsigned long)area->vm_addr; |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1200 | |
Joerg Roedel | d95abbb | 2013-03-27 01:43:14 +0100 | [diff] [blame] | 1201 | unmap_kernel_range(addr, PAGE_SIZE * 2); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1202 | } |
| 1203 | |
Minchan Kim | 1b945ae | 2013-12-11 11:04:36 +0900 | [diff] [blame] | 1204 | #else /* CONFIG_PGTABLE_MAPPING */ |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1205 | |
| 1206 | static inline int __zs_cpu_up(struct mapping_area *area) |
| 1207 | { |
| 1208 | /* |
| 1209 | * Make sure we don't leak memory if a cpu UP notification |
| 1210 | * and zs_init() race and both call zs_cpu_up() on the same cpu |
| 1211 | */ |
| 1212 | if (area->vm_buf) |
| 1213 | return 0; |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 1214 | area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1215 | if (!area->vm_buf) |
| 1216 | return -ENOMEM; |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | static inline void __zs_cpu_down(struct mapping_area *area) |
| 1221 | { |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 1222 | kfree(area->vm_buf); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1223 | area->vm_buf = NULL; |
| 1224 | } |
| 1225 | |
| 1226 | static void *__zs_map_object(struct mapping_area *area, |
| 1227 | struct page *pages[2], int off, int size) |
| 1228 | { |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 1229 | int sizes[2]; |
| 1230 | void *addr; |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1231 | char *buf = area->vm_buf; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 1232 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1233 | /* disable page faults to match kmap_atomic() return conditions */ |
| 1234 | pagefault_disable(); |
| 1235 | |
| 1236 | /* no read fastpath */ |
| 1237 | if (area->vm_mm == ZS_MM_WO) |
| 1238 | goto out; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 1239 | |
| 1240 | sizes[0] = PAGE_SIZE - off; |
| 1241 | sizes[1] = size - sizes[0]; |
| 1242 | |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 1243 | /* copy object to per-cpu buffer */ |
| 1244 | addr = kmap_atomic(pages[0]); |
| 1245 | memcpy(buf, addr + off, sizes[0]); |
| 1246 | kunmap_atomic(addr); |
| 1247 | addr = kmap_atomic(pages[1]); |
| 1248 | memcpy(buf + sizes[0], addr, sizes[1]); |
| 1249 | kunmap_atomic(addr); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1250 | out: |
| 1251 | return area->vm_buf; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 1252 | } |
| 1253 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1254 | static void __zs_unmap_object(struct mapping_area *area, |
| 1255 | struct page *pages[2], int off, int size) |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 1256 | { |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 1257 | int sizes[2]; |
| 1258 | void *addr; |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1259 | char *buf; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 1260 | |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1261 | /* no write fastpath */ |
| 1262 | if (area->vm_mm == ZS_MM_RO) |
| 1263 | goto out; |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 1264 | |
Minchan Kim | 7b60a68 | 2015-04-15 16:15:39 -0700 | [diff] [blame] | 1265 | buf = area->vm_buf; |
YiPing Xu | a82cbf0 | 2016-03-17 14:20:39 -0700 | [diff] [blame] | 1266 | buf = buf + ZS_HANDLE_SIZE; |
| 1267 | size -= ZS_HANDLE_SIZE; |
| 1268 | off += ZS_HANDLE_SIZE; |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1269 | |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 1270 | sizes[0] = PAGE_SIZE - off; |
| 1271 | sizes[1] = size - sizes[0]; |
| 1272 | |
| 1273 | /* copy per-cpu buffer to object */ |
| 1274 | addr = kmap_atomic(pages[0]); |
| 1275 | memcpy(addr + off, buf, sizes[0]); |
| 1276 | kunmap_atomic(addr); |
| 1277 | addr = kmap_atomic(pages[1]); |
| 1278 | memcpy(addr, buf + sizes[0], sizes[1]); |
| 1279 | kunmap_atomic(addr); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1280 | |
| 1281 | out: |
| 1282 | /* enable page faults to match kunmap_atomic() return conditions */ |
| 1283 | pagefault_enable(); |
Seth Jennings | 5f60190 | 2012-07-02 16:15:49 -0500 | [diff] [blame] | 1284 | } |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1285 | |
Minchan Kim | 1b945ae | 2013-12-11 11:04:36 +0900 | [diff] [blame] | 1286 | #endif /* CONFIG_PGTABLE_MAPPING */ |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1287 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1288 | static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action, |
| 1289 | void *pcpu) |
| 1290 | { |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1291 | int ret, cpu = (long)pcpu; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1292 | struct mapping_area *area; |
| 1293 | |
| 1294 | switch (action) { |
| 1295 | case CPU_UP_PREPARE: |
| 1296 | area = &per_cpu(zs_map_area, cpu); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1297 | ret = __zs_cpu_up(area); |
| 1298 | if (ret) |
| 1299 | return notifier_from_errno(ret); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1300 | break; |
| 1301 | case CPU_DEAD: |
| 1302 | case CPU_UP_CANCELED: |
| 1303 | area = &per_cpu(zs_map_area, cpu); |
Seth Jennings | f553646 | 2012-07-18 11:55:56 -0500 | [diff] [blame] | 1304 | __zs_cpu_down(area); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1305 | break; |
| 1306 | } |
| 1307 | |
| 1308 | return NOTIFY_OK; |
| 1309 | } |
| 1310 | |
| 1311 | static struct notifier_block zs_cpu_nb = { |
| 1312 | .notifier_call = zs_cpu_notifier |
| 1313 | }; |
| 1314 | |
Sergey Senozhatsky | b1b00a5 | 2014-12-12 16:56:56 -0800 | [diff] [blame] | 1315 | static int zs_register_cpu_notifier(void) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1316 | { |
Sergey Senozhatsky | b1b00a5 | 2014-12-12 16:56:56 -0800 | [diff] [blame] | 1317 | int cpu, uninitialized_var(ret); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1318 | |
Srivatsa S. Bhat | f0e71fc | 2014-03-11 02:09:59 +0530 | [diff] [blame] | 1319 | cpu_notifier_register_begin(); |
| 1320 | |
| 1321 | __register_cpu_notifier(&zs_cpu_nb); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1322 | for_each_online_cpu(cpu) { |
| 1323 | ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu); |
Sergey Senozhatsky | b1b00a5 | 2014-12-12 16:56:56 -0800 | [diff] [blame] | 1324 | if (notifier_to_errno(ret)) |
| 1325 | break; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 1326 | } |
Srivatsa S. Bhat | f0e71fc | 2014-03-11 02:09:59 +0530 | [diff] [blame] | 1327 | |
| 1328 | cpu_notifier_register_done(); |
Sergey Senozhatsky | b1b00a5 | 2014-12-12 16:56:56 -0800 | [diff] [blame] | 1329 | return notifier_to_errno(ret); |
| 1330 | } |
| 1331 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1332 | static void zs_unregister_cpu_notifier(void) |
| 1333 | { |
| 1334 | int cpu; |
| 1335 | |
| 1336 | cpu_notifier_register_begin(); |
| 1337 | |
| 1338 | for_each_online_cpu(cpu) |
| 1339 | zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu); |
| 1340 | __unregister_cpu_notifier(&zs_cpu_nb); |
| 1341 | |
| 1342 | cpu_notifier_register_done(); |
| 1343 | } |
| 1344 | |
Ganesh Mahendran | 35b3445 | 2016-07-28 15:47:57 -0700 | [diff] [blame] | 1345 | static void __init init_zs_size_classes(void) |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 1346 | { |
| 1347 | int nr; |
| 1348 | |
| 1349 | nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1; |
| 1350 | if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA) |
| 1351 | nr += 1; |
| 1352 | |
| 1353 | zs_size_classes = nr; |
| 1354 | } |
| 1355 | |
Ganesh Mahendran | 64d9046 | 2016-07-28 15:47:51 -0700 | [diff] [blame] | 1356 | static bool can_merge(struct size_class *prev, int pages_per_zspage, |
| 1357 | int objs_per_zspage) |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1358 | { |
Ganesh Mahendran | 64d9046 | 2016-07-28 15:47:51 -0700 | [diff] [blame] | 1359 | if (prev->pages_per_zspage == pages_per_zspage && |
| 1360 | prev->objs_per_zspage == objs_per_zspage) |
| 1361 | return true; |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1362 | |
Ganesh Mahendran | 64d9046 | 2016-07-28 15:47:51 -0700 | [diff] [blame] | 1363 | return false; |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 1364 | } |
| 1365 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1366 | static bool zspage_full(struct size_class *class, struct zspage *zspage) |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1367 | { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1368 | return get_zspage_inuse(zspage) == class->objs_per_zspage; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1371 | unsigned long zs_get_total_pages(struct zs_pool *pool) |
| 1372 | { |
| 1373 | return atomic_long_read(&pool->pages_allocated); |
| 1374 | } |
| 1375 | EXPORT_SYMBOL_GPL(zs_get_total_pages); |
| 1376 | |
| 1377 | /** |
| 1378 | * zs_map_object - get address of allocated object from handle. |
| 1379 | * @pool: pool from which the object was allocated |
| 1380 | * @handle: handle returned from zs_malloc |
| 1381 | * |
| 1382 | * Before using an object allocated from zs_malloc, it must be mapped using |
| 1383 | * this function. When done with the object, it must be unmapped using |
| 1384 | * zs_unmap_object. |
| 1385 | * |
| 1386 | * Only one object can be mapped per cpu at a time. There is no protection |
| 1387 | * against nested mappings. |
| 1388 | * |
| 1389 | * This function returns with preemption and page faults disabled. |
| 1390 | */ |
| 1391 | void *zs_map_object(struct zs_pool *pool, unsigned long handle, |
| 1392 | enum zs_mapmode mm) |
| 1393 | { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1394 | struct zspage *zspage; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1395 | struct page *page; |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1396 | unsigned long obj, off; |
| 1397 | unsigned int obj_idx; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1398 | |
| 1399 | unsigned int class_idx; |
| 1400 | enum fullness_group fg; |
| 1401 | struct size_class *class; |
| 1402 | struct mapping_area *area; |
| 1403 | struct page *pages[2]; |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1404 | void *ret; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1405 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1406 | /* |
| 1407 | * Because we use per-cpu mapping areas shared among the |
| 1408 | * pools/users, we can't allow mapping in interrupt context |
| 1409 | * because it can corrupt another users mappings. |
| 1410 | */ |
Sergey Senozhatsky | 1618400 | 2017-11-15 17:34:03 -0800 | [diff] [blame] | 1411 | BUG_ON(in_interrupt()); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1412 | |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1413 | /* From now on, migration cannot move the object */ |
| 1414 | pin_tag(handle); |
| 1415 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1416 | obj = handle_to_obj(handle); |
| 1417 | obj_to_location(obj, &page, &obj_idx); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1418 | zspage = get_zspage(page); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1419 | |
| 1420 | /* migration cannot move any subpage in this zspage */ |
| 1421 | migrate_read_lock(zspage); |
| 1422 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1423 | get_zspage_mapping(zspage, &class_idx, &fg); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1424 | class = pool->size_class[class_idx]; |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1425 | off = (class->size * obj_idx) & ~PAGE_MASK; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1426 | |
| 1427 | area = &get_cpu_var(zs_map_area); |
| 1428 | area->vm_mm = mm; |
| 1429 | if (off + class->size <= PAGE_SIZE) { |
| 1430 | /* this object is contained entirely within a page */ |
| 1431 | area->vm_addr = kmap_atomic(page); |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1432 | ret = area->vm_addr + off; |
| 1433 | goto out; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | /* this object spans two pages */ |
| 1437 | pages[0] = page; |
| 1438 | pages[1] = get_next_page(page); |
| 1439 | BUG_ON(!pages[1]); |
| 1440 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1441 | ret = __zs_map_object(area, pages, off, class->size); |
| 1442 | out: |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1443 | if (likely(!PageHugeObject(page))) |
Minchan Kim | 7b60a68 | 2015-04-15 16:15:39 -0700 | [diff] [blame] | 1444 | ret += ZS_HANDLE_SIZE; |
| 1445 | |
| 1446 | return ret; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1447 | } |
| 1448 | EXPORT_SYMBOL_GPL(zs_map_object); |
| 1449 | |
| 1450 | void zs_unmap_object(struct zs_pool *pool, unsigned long handle) |
| 1451 | { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1452 | struct zspage *zspage; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1453 | struct page *page; |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1454 | unsigned long obj, off; |
| 1455 | unsigned int obj_idx; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1456 | |
| 1457 | unsigned int class_idx; |
| 1458 | enum fullness_group fg; |
| 1459 | struct size_class *class; |
| 1460 | struct mapping_area *area; |
| 1461 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1462 | obj = handle_to_obj(handle); |
| 1463 | obj_to_location(obj, &page, &obj_idx); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1464 | zspage = get_zspage(page); |
| 1465 | get_zspage_mapping(zspage, &class_idx, &fg); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1466 | class = pool->size_class[class_idx]; |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1467 | off = (class->size * obj_idx) & ~PAGE_MASK; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1468 | |
| 1469 | area = this_cpu_ptr(&zs_map_area); |
| 1470 | if (off + class->size <= PAGE_SIZE) |
| 1471 | kunmap_atomic(area->vm_addr); |
| 1472 | else { |
| 1473 | struct page *pages[2]; |
| 1474 | |
| 1475 | pages[0] = page; |
| 1476 | pages[1] = get_next_page(page); |
| 1477 | BUG_ON(!pages[1]); |
| 1478 | |
| 1479 | __zs_unmap_object(area, pages, off, class->size); |
| 1480 | } |
| 1481 | put_cpu_var(zs_map_area); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1482 | |
| 1483 | migrate_read_unlock(zspage); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1484 | unpin_tag(handle); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1485 | } |
| 1486 | EXPORT_SYMBOL_GPL(zs_unmap_object); |
| 1487 | |
Sergey Senozhatsky | da33b2a | 2018-04-05 16:24:43 -0700 | [diff] [blame^] | 1488 | /** |
| 1489 | * zs_huge_class_size() - Returns the size (in bytes) of the first huge |
| 1490 | * zsmalloc &size_class. |
| 1491 | * @pool: zsmalloc pool to use |
| 1492 | * |
| 1493 | * The function returns the size of the first huge class - any object of equal |
| 1494 | * or bigger size will be stored in zspage consisting of a single physical |
| 1495 | * page. |
| 1496 | * |
| 1497 | * Context: Any context. |
| 1498 | * |
| 1499 | * Return: the size (in bytes) of the first huge zsmalloc &size_class. |
| 1500 | */ |
| 1501 | size_t zs_huge_class_size(struct zs_pool *pool) |
| 1502 | { |
| 1503 | return huge_class_size; |
| 1504 | } |
| 1505 | EXPORT_SYMBOL_GPL(zs_huge_class_size); |
| 1506 | |
Minchan Kim | 251cbb9 | 2016-05-20 16:59:42 -0700 | [diff] [blame] | 1507 | static unsigned long obj_malloc(struct size_class *class, |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1508 | struct zspage *zspage, unsigned long handle) |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame] | 1509 | { |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1510 | int i, nr_page, offset; |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame] | 1511 | unsigned long obj; |
| 1512 | struct link_free *link; |
| 1513 | |
| 1514 | struct page *m_page; |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1515 | unsigned long m_offset; |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame] | 1516 | void *vaddr; |
| 1517 | |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1518 | handle |= OBJ_ALLOCATED_TAG; |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1519 | obj = get_freeobj(zspage); |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1520 | |
| 1521 | offset = obj * class->size; |
| 1522 | nr_page = offset >> PAGE_SHIFT; |
| 1523 | m_offset = offset & ~PAGE_MASK; |
| 1524 | m_page = get_first_page(zspage); |
| 1525 | |
| 1526 | for (i = 0; i < nr_page; i++) |
| 1527 | m_page = get_next_page(m_page); |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame] | 1528 | |
| 1529 | vaddr = kmap_atomic(m_page); |
| 1530 | link = (struct link_free *)vaddr + m_offset / sizeof(*link); |
Minchan Kim | 3b1d9ca | 2016-07-26 15:23:37 -0700 | [diff] [blame] | 1531 | set_freeobj(zspage, link->next >> OBJ_TAG_BITS); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1532 | if (likely(!PageHugeObject(m_page))) |
Minchan Kim | 7b60a68 | 2015-04-15 16:15:39 -0700 | [diff] [blame] | 1533 | /* record handle in the header of allocated chunk */ |
| 1534 | link->handle = handle; |
| 1535 | else |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1536 | /* record handle to page->index */ |
| 1537 | zspage->first_page->index = handle; |
| 1538 | |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame] | 1539 | kunmap_atomic(vaddr); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1540 | mod_zspage_inuse(zspage, 1); |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame] | 1541 | zs_stat_inc(class, OBJ_USED, 1); |
| 1542 | |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1543 | obj = location_to_obj(m_page, obj); |
| 1544 | |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame] | 1545 | return obj; |
| 1546 | } |
| 1547 | |
| 1548 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1549 | /** |
| 1550 | * zs_malloc - Allocate block of given size from pool. |
| 1551 | * @pool: pool to allocate from |
| 1552 | * @size: size of block to allocate |
Ganesh Mahendran | fd85446 | 2016-07-28 15:47:54 -0700 | [diff] [blame] | 1553 | * @gfp: gfp flags when allocating object |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1554 | * |
| 1555 | * On success, handle to the allocated object is returned, |
| 1556 | * otherwise 0. |
| 1557 | * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail. |
| 1558 | */ |
Sergey Senozhatsky | d0d8da2 | 2016-05-20 16:59:48 -0700 | [diff] [blame] | 1559 | unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp) |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1560 | { |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1561 | unsigned long handle, obj; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1562 | struct size_class *class; |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1563 | enum fullness_group newfg; |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1564 | struct zspage *zspage; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1565 | |
Minchan Kim | 7b60a68 | 2015-04-15 16:15:39 -0700 | [diff] [blame] | 1566 | if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE)) |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1567 | return 0; |
| 1568 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1569 | handle = cache_alloc_handle(pool, gfp); |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1570 | if (!handle) |
| 1571 | return 0; |
| 1572 | |
| 1573 | /* extra space in chunk to keep the handle */ |
| 1574 | size += ZS_HANDLE_SIZE; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1575 | class = pool->size_class[get_size_class_index(size)]; |
| 1576 | |
| 1577 | spin_lock(&class->lock); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1578 | zspage = find_get_zspage(class); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1579 | if (likely(zspage)) { |
| 1580 | obj = obj_malloc(class, zspage, handle); |
| 1581 | /* Now move the zspage to another fullness group, if required */ |
| 1582 | fix_fullness_group(class, zspage); |
| 1583 | record_obj(handle, obj); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1584 | spin_unlock(&class->lock); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1585 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1586 | return handle; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1587 | } |
| 1588 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1589 | spin_unlock(&class->lock); |
| 1590 | |
| 1591 | zspage = alloc_zspage(pool, class, gfp); |
| 1592 | if (!zspage) { |
| 1593 | cache_free_handle(pool, handle); |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | spin_lock(&class->lock); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1598 | obj = obj_malloc(class, zspage, handle); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1599 | newfg = get_fullness_group(class, zspage); |
| 1600 | insert_zspage(class, zspage, newfg); |
| 1601 | set_zspage_mapping(zspage, class->index, newfg); |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1602 | record_obj(handle, obj); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1603 | atomic_long_add(class->pages_per_zspage, |
| 1604 | &pool->pages_allocated); |
Ganesh Mahendran | b4fd07a | 2016-07-28 15:47:49 -0700 | [diff] [blame] | 1605 | zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1606 | |
| 1607 | /* We completely set up zspage so mark them as movable */ |
| 1608 | SetZsPageMovable(pool, zspage); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1609 | spin_unlock(&class->lock); |
| 1610 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1611 | return handle; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1612 | } |
| 1613 | EXPORT_SYMBOL_GPL(zs_malloc); |
| 1614 | |
Minchan Kim | 1ee4716 | 2016-05-20 16:59:45 -0700 | [diff] [blame] | 1615 | static void obj_free(struct size_class *class, unsigned long obj) |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1616 | { |
| 1617 | struct link_free *link; |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1618 | struct zspage *zspage; |
| 1619 | struct page *f_page; |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1620 | unsigned long f_offset; |
| 1621 | unsigned int f_objidx; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1622 | void *vaddr; |
| 1623 | |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1624 | obj &= ~OBJ_ALLOCATED_TAG; |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame] | 1625 | obj_to_location(obj, &f_page, &f_objidx); |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1626 | f_offset = (class->size * f_objidx) & ~PAGE_MASK; |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1627 | zspage = get_zspage(f_page); |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame] | 1628 | |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame] | 1629 | vaddr = kmap_atomic(f_page); |
| 1630 | |
| 1631 | /* Insert this object in containing zspage's freelist */ |
| 1632 | link = (struct link_free *)(vaddr + f_offset); |
Minchan Kim | 3b1d9ca | 2016-07-26 15:23:37 -0700 | [diff] [blame] | 1633 | link->next = get_freeobj(zspage) << OBJ_TAG_BITS; |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame] | 1634 | kunmap_atomic(vaddr); |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1635 | set_freeobj(zspage, f_objidx); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1636 | mod_zspage_inuse(zspage, -1); |
Minchan Kim | c780626 | 2015-04-15 16:15:26 -0700 | [diff] [blame] | 1637 | zs_stat_dec(class, OBJ_USED, 1); |
| 1638 | } |
| 1639 | |
| 1640 | void zs_free(struct zs_pool *pool, unsigned long handle) |
| 1641 | { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1642 | struct zspage *zspage; |
| 1643 | struct page *f_page; |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1644 | unsigned long obj; |
| 1645 | unsigned int f_objidx; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1646 | int class_idx; |
| 1647 | struct size_class *class; |
| 1648 | enum fullness_group fullness; |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1649 | bool isolated; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1650 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1651 | if (unlikely(!handle)) |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1652 | return; |
| 1653 | |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1654 | pin_tag(handle); |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1655 | obj = handle_to_obj(handle); |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 1656 | obj_to_location(obj, &f_page, &f_objidx); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1657 | zspage = get_zspage(f_page); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1658 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1659 | migrate_read_lock(zspage); |
| 1660 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1661 | get_zspage_mapping(zspage, &class_idx, &fullness); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1662 | class = pool->size_class[class_idx]; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1663 | |
| 1664 | spin_lock(&class->lock); |
Minchan Kim | 1ee4716 | 2016-05-20 16:59:45 -0700 | [diff] [blame] | 1665 | obj_free(class, obj); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1666 | fullness = fix_fullness_group(class, zspage); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1667 | if (fullness != ZS_EMPTY) { |
| 1668 | migrate_read_unlock(zspage); |
| 1669 | goto out; |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1670 | } |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1671 | |
| 1672 | isolated = is_zspage_isolated(zspage); |
| 1673 | migrate_read_unlock(zspage); |
| 1674 | /* If zspage is isolated, zs_page_putback will free the zspage */ |
| 1675 | if (likely(!isolated)) |
| 1676 | free_zspage(pool, class, zspage); |
| 1677 | out: |
| 1678 | |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1679 | spin_unlock(&class->lock); |
| 1680 | unpin_tag(handle); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1681 | cache_free_handle(pool, handle); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 1682 | } |
| 1683 | EXPORT_SYMBOL_GPL(zs_free); |
| 1684 | |
Minchan Kim | 251cbb9 | 2016-05-20 16:59:42 -0700 | [diff] [blame] | 1685 | static void zs_object_copy(struct size_class *class, unsigned long dst, |
| 1686 | unsigned long src) |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1687 | { |
| 1688 | struct page *s_page, *d_page; |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1689 | unsigned int s_objidx, d_objidx; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1690 | unsigned long s_off, d_off; |
| 1691 | void *s_addr, *d_addr; |
| 1692 | int s_size, d_size, size; |
| 1693 | int written = 0; |
| 1694 | |
| 1695 | s_size = d_size = class->size; |
| 1696 | |
| 1697 | obj_to_location(src, &s_page, &s_objidx); |
| 1698 | obj_to_location(dst, &d_page, &d_objidx); |
| 1699 | |
Minchan Kim | bfd093f | 2016-07-26 15:23:28 -0700 | [diff] [blame] | 1700 | s_off = (class->size * s_objidx) & ~PAGE_MASK; |
| 1701 | d_off = (class->size * d_objidx) & ~PAGE_MASK; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1702 | |
| 1703 | if (s_off + class->size > PAGE_SIZE) |
| 1704 | s_size = PAGE_SIZE - s_off; |
| 1705 | |
| 1706 | if (d_off + class->size > PAGE_SIZE) |
| 1707 | d_size = PAGE_SIZE - d_off; |
| 1708 | |
| 1709 | s_addr = kmap_atomic(s_page); |
| 1710 | d_addr = kmap_atomic(d_page); |
| 1711 | |
| 1712 | while (1) { |
| 1713 | size = min(s_size, d_size); |
| 1714 | memcpy(d_addr + d_off, s_addr + s_off, size); |
| 1715 | written += size; |
| 1716 | |
| 1717 | if (written == class->size) |
| 1718 | break; |
| 1719 | |
Sergey Senozhatsky | 495819e | 2015-04-15 16:16:15 -0700 | [diff] [blame] | 1720 | s_off += size; |
| 1721 | s_size -= size; |
| 1722 | d_off += size; |
| 1723 | d_size -= size; |
| 1724 | |
| 1725 | if (s_off >= PAGE_SIZE) { |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1726 | kunmap_atomic(d_addr); |
| 1727 | kunmap_atomic(s_addr); |
| 1728 | s_page = get_next_page(s_page); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1729 | s_addr = kmap_atomic(s_page); |
| 1730 | d_addr = kmap_atomic(d_page); |
| 1731 | s_size = class->size - written; |
| 1732 | s_off = 0; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
Sergey Senozhatsky | 495819e | 2015-04-15 16:16:15 -0700 | [diff] [blame] | 1735 | if (d_off >= PAGE_SIZE) { |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1736 | kunmap_atomic(d_addr); |
| 1737 | d_page = get_next_page(d_page); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1738 | d_addr = kmap_atomic(d_page); |
| 1739 | d_size = class->size - written; |
| 1740 | d_off = 0; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | kunmap_atomic(d_addr); |
| 1745 | kunmap_atomic(s_addr); |
| 1746 | } |
| 1747 | |
| 1748 | /* |
| 1749 | * Find alloced object in zspage from index object and |
| 1750 | * return handle. |
| 1751 | */ |
Minchan Kim | 251cbb9 | 2016-05-20 16:59:42 -0700 | [diff] [blame] | 1752 | static unsigned long find_alloced_obj(struct size_class *class, |
Ganesh Mahendran | cf675ac | 2016-07-28 15:47:46 -0700 | [diff] [blame] | 1753 | struct page *page, int *obj_idx) |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1754 | { |
| 1755 | unsigned long head; |
| 1756 | int offset = 0; |
Ganesh Mahendran | cf675ac | 2016-07-28 15:47:46 -0700 | [diff] [blame] | 1757 | int index = *obj_idx; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1758 | unsigned long handle = 0; |
| 1759 | void *addr = kmap_atomic(page); |
| 1760 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1761 | offset = get_first_obj_offset(page); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1762 | offset += class->size * index; |
| 1763 | |
| 1764 | while (offset < PAGE_SIZE) { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1765 | head = obj_to_head(page, addr + offset); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1766 | if (head & OBJ_ALLOCATED_TAG) { |
| 1767 | handle = head & ~OBJ_ALLOCATED_TAG; |
| 1768 | if (trypin_tag(handle)) |
| 1769 | break; |
| 1770 | handle = 0; |
| 1771 | } |
| 1772 | |
| 1773 | offset += class->size; |
| 1774 | index++; |
| 1775 | } |
| 1776 | |
| 1777 | kunmap_atomic(addr); |
Ganesh Mahendran | cf675ac | 2016-07-28 15:47:46 -0700 | [diff] [blame] | 1778 | |
| 1779 | *obj_idx = index; |
| 1780 | |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1781 | return handle; |
| 1782 | } |
| 1783 | |
| 1784 | struct zs_compact_control { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1785 | /* Source spage for migration which could be a subpage of zspage */ |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1786 | struct page *s_page; |
| 1787 | /* Destination page for migration which should be a first page |
| 1788 | * of zspage. */ |
| 1789 | struct page *d_page; |
| 1790 | /* Starting object index within @s_page which used for live object |
| 1791 | * in the subpage. */ |
Ganesh Mahendran | 41b88e1 | 2016-07-28 15:47:43 -0700 | [diff] [blame] | 1792 | int obj_idx; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1793 | }; |
| 1794 | |
| 1795 | static int migrate_zspage(struct zs_pool *pool, struct size_class *class, |
| 1796 | struct zs_compact_control *cc) |
| 1797 | { |
| 1798 | unsigned long used_obj, free_obj; |
| 1799 | unsigned long handle; |
| 1800 | struct page *s_page = cc->s_page; |
| 1801 | struct page *d_page = cc->d_page; |
Ganesh Mahendran | 41b88e1 | 2016-07-28 15:47:43 -0700 | [diff] [blame] | 1802 | int obj_idx = cc->obj_idx; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1803 | int ret = 0; |
| 1804 | |
| 1805 | while (1) { |
Ganesh Mahendran | cf675ac | 2016-07-28 15:47:46 -0700 | [diff] [blame] | 1806 | handle = find_alloced_obj(class, s_page, &obj_idx); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1807 | if (!handle) { |
| 1808 | s_page = get_next_page(s_page); |
| 1809 | if (!s_page) |
| 1810 | break; |
Ganesh Mahendran | 41b88e1 | 2016-07-28 15:47:43 -0700 | [diff] [blame] | 1811 | obj_idx = 0; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1812 | continue; |
| 1813 | } |
| 1814 | |
| 1815 | /* Stop if there is no more space */ |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1816 | if (zspage_full(class, get_zspage(d_page))) { |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1817 | unpin_tag(handle); |
| 1818 | ret = -ENOMEM; |
| 1819 | break; |
| 1820 | } |
| 1821 | |
| 1822 | used_obj = handle_to_obj(handle); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1823 | free_obj = obj_malloc(class, get_zspage(d_page), handle); |
Minchan Kim | 251cbb9 | 2016-05-20 16:59:42 -0700 | [diff] [blame] | 1824 | zs_object_copy(class, free_obj, used_obj); |
Ganesh Mahendran | 41b88e1 | 2016-07-28 15:47:43 -0700 | [diff] [blame] | 1825 | obj_idx++; |
Junil Lee | c102f07 | 2016-01-20 14:58:18 -0800 | [diff] [blame] | 1826 | /* |
| 1827 | * record_obj updates handle's value to free_obj and it will |
| 1828 | * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which |
| 1829 | * breaks synchronization using pin_tag(e,g, zs_free) so |
| 1830 | * let's keep the lock bit. |
| 1831 | */ |
| 1832 | free_obj |= BIT(HANDLE_PIN_BIT); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1833 | record_obj(handle, free_obj); |
| 1834 | unpin_tag(handle); |
Minchan Kim | 1ee4716 | 2016-05-20 16:59:45 -0700 | [diff] [blame] | 1835 | obj_free(class, used_obj); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | /* Remember last position in this iteration */ |
| 1839 | cc->s_page = s_page; |
Ganesh Mahendran | 41b88e1 | 2016-07-28 15:47:43 -0700 | [diff] [blame] | 1840 | cc->obj_idx = obj_idx; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1841 | |
| 1842 | return ret; |
| 1843 | } |
| 1844 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1845 | static struct zspage *isolate_zspage(struct size_class *class, bool source) |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1846 | { |
| 1847 | int i; |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1848 | struct zspage *zspage; |
| 1849 | enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL}; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1850 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1851 | if (!source) { |
| 1852 | fg[0] = ZS_ALMOST_FULL; |
| 1853 | fg[1] = ZS_ALMOST_EMPTY; |
| 1854 | } |
| 1855 | |
| 1856 | for (i = 0; i < 2; i++) { |
| 1857 | zspage = list_first_entry_or_null(&class->fullness_list[fg[i]], |
| 1858 | struct zspage, list); |
| 1859 | if (zspage) { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1860 | VM_BUG_ON(is_zspage_isolated(zspage)); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1861 | remove_zspage(class, zspage, fg[i]); |
| 1862 | return zspage; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1863 | } |
| 1864 | } |
| 1865 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1866 | return zspage; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
Sergey Senozhatsky | 860c707 | 2015-09-08 15:04:38 -0700 | [diff] [blame] | 1869 | /* |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1870 | * putback_zspage - add @zspage into right class's fullness list |
Sergey Senozhatsky | 860c707 | 2015-09-08 15:04:38 -0700 | [diff] [blame] | 1871 | * @class: destination class |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1872 | * @zspage: target page |
Sergey Senozhatsky | 860c707 | 2015-09-08 15:04:38 -0700 | [diff] [blame] | 1873 | * |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1874 | * Return @zspage's fullness_group |
Sergey Senozhatsky | 860c707 | 2015-09-08 15:04:38 -0700 | [diff] [blame] | 1875 | */ |
Minchan Kim | 4aa409c | 2016-07-26 15:23:26 -0700 | [diff] [blame] | 1876 | static enum fullness_group putback_zspage(struct size_class *class, |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1877 | struct zspage *zspage) |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1878 | { |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1879 | enum fullness_group fullness; |
| 1880 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1881 | VM_BUG_ON(is_zspage_isolated(zspage)); |
| 1882 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 1883 | fullness = get_fullness_group(class, zspage); |
| 1884 | insert_zspage(class, zspage, fullness); |
| 1885 | set_zspage_mapping(zspage, class->index, fullness); |
Minchan Kim | 839373e | 2015-04-15 16:16:18 -0700 | [diff] [blame] | 1886 | |
Sergey Senozhatsky | 860c707 | 2015-09-08 15:04:38 -0700 | [diff] [blame] | 1887 | return fullness; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 1890 | #ifdef CONFIG_COMPACTION |
| 1891 | static struct dentry *zs_mount(struct file_system_type *fs_type, |
| 1892 | int flags, const char *dev_name, void *data) |
| 1893 | { |
| 1894 | static const struct dentry_operations ops = { |
| 1895 | .d_dname = simple_dname, |
| 1896 | }; |
| 1897 | |
| 1898 | return mount_pseudo(fs_type, "zsmalloc:", NULL, &ops, ZSMALLOC_MAGIC); |
| 1899 | } |
| 1900 | |
| 1901 | static struct file_system_type zsmalloc_fs = { |
| 1902 | .name = "zsmalloc", |
| 1903 | .mount = zs_mount, |
| 1904 | .kill_sb = kill_anon_super, |
| 1905 | }; |
| 1906 | |
| 1907 | static int zsmalloc_mount(void) |
| 1908 | { |
| 1909 | int ret = 0; |
| 1910 | |
| 1911 | zsmalloc_mnt = kern_mount(&zsmalloc_fs); |
| 1912 | if (IS_ERR(zsmalloc_mnt)) |
| 1913 | ret = PTR_ERR(zsmalloc_mnt); |
| 1914 | |
| 1915 | return ret; |
| 1916 | } |
| 1917 | |
| 1918 | static void zsmalloc_unmount(void) |
| 1919 | { |
| 1920 | kern_unmount(zsmalloc_mnt); |
| 1921 | } |
| 1922 | |
| 1923 | static void migrate_lock_init(struct zspage *zspage) |
| 1924 | { |
| 1925 | rwlock_init(&zspage->lock); |
| 1926 | } |
| 1927 | |
| 1928 | static void migrate_read_lock(struct zspage *zspage) |
| 1929 | { |
| 1930 | read_lock(&zspage->lock); |
| 1931 | } |
| 1932 | |
| 1933 | static void migrate_read_unlock(struct zspage *zspage) |
| 1934 | { |
| 1935 | read_unlock(&zspage->lock); |
| 1936 | } |
| 1937 | |
| 1938 | static void migrate_write_lock(struct zspage *zspage) |
| 1939 | { |
| 1940 | write_lock(&zspage->lock); |
| 1941 | } |
| 1942 | |
| 1943 | static void migrate_write_unlock(struct zspage *zspage) |
| 1944 | { |
| 1945 | write_unlock(&zspage->lock); |
| 1946 | } |
| 1947 | |
| 1948 | /* Number of isolated subpage for *page migration* in this zspage */ |
| 1949 | static void inc_zspage_isolation(struct zspage *zspage) |
| 1950 | { |
| 1951 | zspage->isolated++; |
| 1952 | } |
| 1953 | |
| 1954 | static void dec_zspage_isolation(struct zspage *zspage) |
| 1955 | { |
| 1956 | zspage->isolated--; |
| 1957 | } |
| 1958 | |
| 1959 | static void replace_sub_page(struct size_class *class, struct zspage *zspage, |
| 1960 | struct page *newpage, struct page *oldpage) |
| 1961 | { |
| 1962 | struct page *page; |
| 1963 | struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, }; |
| 1964 | int idx = 0; |
| 1965 | |
| 1966 | page = get_first_page(zspage); |
| 1967 | do { |
| 1968 | if (page == oldpage) |
| 1969 | pages[idx] = newpage; |
| 1970 | else |
| 1971 | pages[idx] = page; |
| 1972 | idx++; |
| 1973 | } while ((page = get_next_page(page)) != NULL); |
| 1974 | |
| 1975 | create_page_chain(class, zspage, pages); |
| 1976 | set_first_obj_offset(newpage, get_first_obj_offset(oldpage)); |
| 1977 | if (unlikely(PageHugeObject(oldpage))) |
| 1978 | newpage->index = oldpage->index; |
| 1979 | __SetPageMovable(newpage, page_mapping(oldpage)); |
| 1980 | } |
| 1981 | |
| 1982 | bool zs_page_isolate(struct page *page, isolate_mode_t mode) |
| 1983 | { |
| 1984 | struct zs_pool *pool; |
| 1985 | struct size_class *class; |
| 1986 | int class_idx; |
| 1987 | enum fullness_group fullness; |
| 1988 | struct zspage *zspage; |
| 1989 | struct address_space *mapping; |
| 1990 | |
| 1991 | /* |
| 1992 | * Page is locked so zspage couldn't be destroyed. For detail, look at |
| 1993 | * lock_zspage in free_zspage. |
| 1994 | */ |
| 1995 | VM_BUG_ON_PAGE(!PageMovable(page), page); |
| 1996 | VM_BUG_ON_PAGE(PageIsolated(page), page); |
| 1997 | |
| 1998 | zspage = get_zspage(page); |
| 1999 | |
| 2000 | /* |
| 2001 | * Without class lock, fullness could be stale while class_idx is okay |
| 2002 | * because class_idx is constant unless page is freed so we should get |
| 2003 | * fullness again under class lock. |
| 2004 | */ |
| 2005 | get_zspage_mapping(zspage, &class_idx, &fullness); |
| 2006 | mapping = page_mapping(page); |
| 2007 | pool = mapping->private_data; |
| 2008 | class = pool->size_class[class_idx]; |
| 2009 | |
| 2010 | spin_lock(&class->lock); |
| 2011 | if (get_zspage_inuse(zspage) == 0) { |
| 2012 | spin_unlock(&class->lock); |
| 2013 | return false; |
| 2014 | } |
| 2015 | |
| 2016 | /* zspage is isolated for object migration */ |
| 2017 | if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) { |
| 2018 | spin_unlock(&class->lock); |
| 2019 | return false; |
| 2020 | } |
| 2021 | |
| 2022 | /* |
| 2023 | * If this is first time isolation for the zspage, isolate zspage from |
| 2024 | * size_class to prevent further object allocation from the zspage. |
| 2025 | */ |
| 2026 | if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) { |
| 2027 | get_zspage_mapping(zspage, &class_idx, &fullness); |
| 2028 | remove_zspage(class, zspage, fullness); |
| 2029 | } |
| 2030 | |
| 2031 | inc_zspage_isolation(zspage); |
| 2032 | spin_unlock(&class->lock); |
| 2033 | |
| 2034 | return true; |
| 2035 | } |
| 2036 | |
| 2037 | int zs_page_migrate(struct address_space *mapping, struct page *newpage, |
| 2038 | struct page *page, enum migrate_mode mode) |
| 2039 | { |
| 2040 | struct zs_pool *pool; |
| 2041 | struct size_class *class; |
| 2042 | int class_idx; |
| 2043 | enum fullness_group fullness; |
| 2044 | struct zspage *zspage; |
| 2045 | struct page *dummy; |
| 2046 | void *s_addr, *d_addr, *addr; |
| 2047 | int offset, pos; |
| 2048 | unsigned long handle, head; |
| 2049 | unsigned long old_obj, new_obj; |
| 2050 | unsigned int obj_idx; |
| 2051 | int ret = -EAGAIN; |
| 2052 | |
| 2053 | VM_BUG_ON_PAGE(!PageMovable(page), page); |
| 2054 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
| 2055 | |
| 2056 | zspage = get_zspage(page); |
| 2057 | |
| 2058 | /* Concurrent compactor cannot migrate any subpage in zspage */ |
| 2059 | migrate_write_lock(zspage); |
| 2060 | get_zspage_mapping(zspage, &class_idx, &fullness); |
| 2061 | pool = mapping->private_data; |
| 2062 | class = pool->size_class[class_idx]; |
| 2063 | offset = get_first_obj_offset(page); |
| 2064 | |
| 2065 | spin_lock(&class->lock); |
| 2066 | if (!get_zspage_inuse(zspage)) { |
| 2067 | ret = -EBUSY; |
| 2068 | goto unlock_class; |
| 2069 | } |
| 2070 | |
| 2071 | pos = offset; |
| 2072 | s_addr = kmap_atomic(page); |
| 2073 | while (pos < PAGE_SIZE) { |
| 2074 | head = obj_to_head(page, s_addr + pos); |
| 2075 | if (head & OBJ_ALLOCATED_TAG) { |
| 2076 | handle = head & ~OBJ_ALLOCATED_TAG; |
| 2077 | if (!trypin_tag(handle)) |
| 2078 | goto unpin_objects; |
| 2079 | } |
| 2080 | pos += class->size; |
| 2081 | } |
| 2082 | |
| 2083 | /* |
| 2084 | * Here, any user cannot access all objects in the zspage so let's move. |
| 2085 | */ |
| 2086 | d_addr = kmap_atomic(newpage); |
| 2087 | memcpy(d_addr, s_addr, PAGE_SIZE); |
| 2088 | kunmap_atomic(d_addr); |
| 2089 | |
| 2090 | for (addr = s_addr + offset; addr < s_addr + pos; |
| 2091 | addr += class->size) { |
| 2092 | head = obj_to_head(page, addr); |
| 2093 | if (head & OBJ_ALLOCATED_TAG) { |
| 2094 | handle = head & ~OBJ_ALLOCATED_TAG; |
| 2095 | if (!testpin_tag(handle)) |
| 2096 | BUG(); |
| 2097 | |
| 2098 | old_obj = handle_to_obj(handle); |
| 2099 | obj_to_location(old_obj, &dummy, &obj_idx); |
| 2100 | new_obj = (unsigned long)location_to_obj(newpage, |
| 2101 | obj_idx); |
| 2102 | new_obj |= BIT(HANDLE_PIN_BIT); |
| 2103 | record_obj(handle, new_obj); |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | replace_sub_page(class, zspage, newpage, page); |
| 2108 | get_page(newpage); |
| 2109 | |
| 2110 | dec_zspage_isolation(zspage); |
| 2111 | |
| 2112 | /* |
| 2113 | * Page migration is done so let's putback isolated zspage to |
| 2114 | * the list if @page is final isolated subpage in the zspage. |
| 2115 | */ |
| 2116 | if (!is_zspage_isolated(zspage)) |
| 2117 | putback_zspage(class, zspage); |
| 2118 | |
| 2119 | reset_page(page); |
| 2120 | put_page(page); |
| 2121 | page = newpage; |
| 2122 | |
Minchan Kim | dd4123f | 2016-07-26 15:26:50 -0700 | [diff] [blame] | 2123 | ret = MIGRATEPAGE_SUCCESS; |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 2124 | unpin_objects: |
| 2125 | for (addr = s_addr + offset; addr < s_addr + pos; |
| 2126 | addr += class->size) { |
| 2127 | head = obj_to_head(page, addr); |
| 2128 | if (head & OBJ_ALLOCATED_TAG) { |
| 2129 | handle = head & ~OBJ_ALLOCATED_TAG; |
| 2130 | if (!testpin_tag(handle)) |
| 2131 | BUG(); |
| 2132 | unpin_tag(handle); |
| 2133 | } |
| 2134 | } |
| 2135 | kunmap_atomic(s_addr); |
| 2136 | unlock_class: |
| 2137 | spin_unlock(&class->lock); |
| 2138 | migrate_write_unlock(zspage); |
| 2139 | |
| 2140 | return ret; |
| 2141 | } |
| 2142 | |
| 2143 | void zs_page_putback(struct page *page) |
| 2144 | { |
| 2145 | struct zs_pool *pool; |
| 2146 | struct size_class *class; |
| 2147 | int class_idx; |
| 2148 | enum fullness_group fg; |
| 2149 | struct address_space *mapping; |
| 2150 | struct zspage *zspage; |
| 2151 | |
| 2152 | VM_BUG_ON_PAGE(!PageMovable(page), page); |
| 2153 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
| 2154 | |
| 2155 | zspage = get_zspage(page); |
| 2156 | get_zspage_mapping(zspage, &class_idx, &fg); |
| 2157 | mapping = page_mapping(page); |
| 2158 | pool = mapping->private_data; |
| 2159 | class = pool->size_class[class_idx]; |
| 2160 | |
| 2161 | spin_lock(&class->lock); |
| 2162 | dec_zspage_isolation(zspage); |
| 2163 | if (!is_zspage_isolated(zspage)) { |
| 2164 | fg = putback_zspage(class, zspage); |
| 2165 | /* |
| 2166 | * Due to page_lock, we cannot free zspage immediately |
| 2167 | * so let's defer. |
| 2168 | */ |
| 2169 | if (fg == ZS_EMPTY) |
| 2170 | schedule_work(&pool->free_work); |
| 2171 | } |
| 2172 | spin_unlock(&class->lock); |
| 2173 | } |
| 2174 | |
| 2175 | const struct address_space_operations zsmalloc_aops = { |
| 2176 | .isolate_page = zs_page_isolate, |
| 2177 | .migratepage = zs_page_migrate, |
| 2178 | .putback_page = zs_page_putback, |
| 2179 | }; |
| 2180 | |
| 2181 | static int zs_register_migration(struct zs_pool *pool) |
| 2182 | { |
| 2183 | pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb); |
| 2184 | if (IS_ERR(pool->inode)) { |
| 2185 | pool->inode = NULL; |
| 2186 | return 1; |
| 2187 | } |
| 2188 | |
| 2189 | pool->inode->i_mapping->private_data = pool; |
| 2190 | pool->inode->i_mapping->a_ops = &zsmalloc_aops; |
| 2191 | return 0; |
| 2192 | } |
| 2193 | |
| 2194 | static void zs_unregister_migration(struct zs_pool *pool) |
| 2195 | { |
| 2196 | flush_work(&pool->free_work); |
Markus Elfring | c3491ec | 2016-07-28 15:48:59 -0700 | [diff] [blame] | 2197 | iput(pool->inode); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | /* |
| 2201 | * Caller should hold page_lock of all pages in the zspage |
| 2202 | * In here, we cannot use zspage meta data. |
| 2203 | */ |
| 2204 | static void async_free_zspage(struct work_struct *work) |
| 2205 | { |
| 2206 | int i; |
| 2207 | struct size_class *class; |
| 2208 | unsigned int class_idx; |
| 2209 | enum fullness_group fullness; |
| 2210 | struct zspage *zspage, *tmp; |
| 2211 | LIST_HEAD(free_pages); |
| 2212 | struct zs_pool *pool = container_of(work, struct zs_pool, |
| 2213 | free_work); |
| 2214 | |
| 2215 | for (i = 0; i < zs_size_classes; i++) { |
| 2216 | class = pool->size_class[i]; |
| 2217 | if (class->index != i) |
| 2218 | continue; |
| 2219 | |
| 2220 | spin_lock(&class->lock); |
| 2221 | list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages); |
| 2222 | spin_unlock(&class->lock); |
| 2223 | } |
| 2224 | |
| 2225 | |
| 2226 | list_for_each_entry_safe(zspage, tmp, &free_pages, list) { |
| 2227 | list_del(&zspage->list); |
| 2228 | lock_zspage(zspage); |
| 2229 | |
| 2230 | get_zspage_mapping(zspage, &class_idx, &fullness); |
| 2231 | VM_BUG_ON(fullness != ZS_EMPTY); |
| 2232 | class = pool->size_class[class_idx]; |
| 2233 | spin_lock(&class->lock); |
| 2234 | __free_zspage(pool, pool->size_class[class_idx], zspage); |
| 2235 | spin_unlock(&class->lock); |
| 2236 | } |
| 2237 | }; |
| 2238 | |
| 2239 | static void kick_deferred_free(struct zs_pool *pool) |
| 2240 | { |
| 2241 | schedule_work(&pool->free_work); |
| 2242 | } |
| 2243 | |
| 2244 | static void init_deferred_free(struct zs_pool *pool) |
| 2245 | { |
| 2246 | INIT_WORK(&pool->free_work, async_free_zspage); |
| 2247 | } |
| 2248 | |
| 2249 | static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) |
| 2250 | { |
| 2251 | struct page *page = get_first_page(zspage); |
| 2252 | |
| 2253 | do { |
| 2254 | WARN_ON(!trylock_page(page)); |
| 2255 | __SetPageMovable(page, pool->inode->i_mapping); |
| 2256 | unlock_page(page); |
| 2257 | } while ((page = get_next_page(page)) != NULL); |
| 2258 | } |
| 2259 | #endif |
| 2260 | |
Sergey Senozhatsky | 04f0590 | 2015-09-08 15:04:30 -0700 | [diff] [blame] | 2261 | /* |
| 2262 | * |
| 2263 | * Based on the number of unused allocated objects calculate |
| 2264 | * and return the number of pages that we can free. |
Sergey Senozhatsky | 04f0590 | 2015-09-08 15:04:30 -0700 | [diff] [blame] | 2265 | */ |
| 2266 | static unsigned long zs_can_compact(struct size_class *class) |
| 2267 | { |
| 2268 | unsigned long obj_wasted; |
Sergey Senozhatsky | 44f43e9 | 2016-05-09 16:28:49 -0700 | [diff] [blame] | 2269 | unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED); |
| 2270 | unsigned long obj_used = zs_stat_get(class, OBJ_USED); |
Sergey Senozhatsky | 04f0590 | 2015-09-08 15:04:30 -0700 | [diff] [blame] | 2271 | |
Sergey Senozhatsky | 44f43e9 | 2016-05-09 16:28:49 -0700 | [diff] [blame] | 2272 | if (obj_allocated <= obj_used) |
| 2273 | return 0; |
Sergey Senozhatsky | 04f0590 | 2015-09-08 15:04:30 -0700 | [diff] [blame] | 2274 | |
Sergey Senozhatsky | 44f43e9 | 2016-05-09 16:28:49 -0700 | [diff] [blame] | 2275 | obj_wasted = obj_allocated - obj_used; |
Ganesh Mahendran | b4fd07a | 2016-07-28 15:47:49 -0700 | [diff] [blame] | 2276 | obj_wasted /= class->objs_per_zspage; |
Sergey Senozhatsky | 04f0590 | 2015-09-08 15:04:30 -0700 | [diff] [blame] | 2277 | |
Minchan Kim | 6cbf16b | 2015-09-08 15:04:49 -0700 | [diff] [blame] | 2278 | return obj_wasted * class->pages_per_zspage; |
Sergey Senozhatsky | 04f0590 | 2015-09-08 15:04:30 -0700 | [diff] [blame] | 2279 | } |
| 2280 | |
Sergey Senozhatsky | 7d3f393 | 2015-09-08 15:04:35 -0700 | [diff] [blame] | 2281 | static void __zs_compact(struct zs_pool *pool, struct size_class *class) |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2282 | { |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2283 | struct zs_compact_control cc; |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 2284 | struct zspage *src_zspage; |
| 2285 | struct zspage *dst_zspage = NULL; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2286 | |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2287 | spin_lock(&class->lock); |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 2288 | while ((src_zspage = isolate_zspage(class, true))) { |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2289 | |
Sergey Senozhatsky | 04f0590 | 2015-09-08 15:04:30 -0700 | [diff] [blame] | 2290 | if (!zs_can_compact(class)) |
| 2291 | break; |
| 2292 | |
Ganesh Mahendran | 41b88e1 | 2016-07-28 15:47:43 -0700 | [diff] [blame] | 2293 | cc.obj_idx = 0; |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 2294 | cc.s_page = get_first_page(src_zspage); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2295 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 2296 | while ((dst_zspage = isolate_zspage(class, false))) { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 2297 | cc.d_page = get_first_page(dst_zspage); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2298 | /* |
Sergey Senozhatsky | 0dc63d4 | 2015-09-08 15:04:33 -0700 | [diff] [blame] | 2299 | * If there is no more space in dst_page, resched |
| 2300 | * and see if anyone had allocated another zspage. |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2301 | */ |
| 2302 | if (!migrate_zspage(pool, class, &cc)) |
| 2303 | break; |
| 2304 | |
Minchan Kim | 4aa409c | 2016-07-26 15:23:26 -0700 | [diff] [blame] | 2305 | putback_zspage(class, dst_zspage); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | /* Stop if we couldn't find slot */ |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 2309 | if (dst_zspage == NULL) |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2310 | break; |
| 2311 | |
Minchan Kim | 4aa409c | 2016-07-26 15:23:26 -0700 | [diff] [blame] | 2312 | putback_zspage(class, dst_zspage); |
| 2313 | if (putback_zspage(class, src_zspage) == ZS_EMPTY) { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 2314 | free_zspage(pool, class, src_zspage); |
Minchan Kim | 6cbf16b | 2015-09-08 15:04:49 -0700 | [diff] [blame] | 2315 | pool->stats.pages_compacted += class->pages_per_zspage; |
Minchan Kim | 4aa409c | 2016-07-26 15:23:26 -0700 | [diff] [blame] | 2316 | } |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2317 | spin_unlock(&class->lock); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2318 | cond_resched(); |
| 2319 | spin_lock(&class->lock); |
| 2320 | } |
| 2321 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 2322 | if (src_zspage) |
Minchan Kim | 4aa409c | 2016-07-26 15:23:26 -0700 | [diff] [blame] | 2323 | putback_zspage(class, src_zspage); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2324 | |
Sergey Senozhatsky | 7d3f393 | 2015-09-08 15:04:35 -0700 | [diff] [blame] | 2325 | spin_unlock(&class->lock); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | unsigned long zs_compact(struct zs_pool *pool) |
| 2329 | { |
| 2330 | int i; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2331 | struct size_class *class; |
| 2332 | |
| 2333 | for (i = zs_size_classes - 1; i >= 0; i--) { |
| 2334 | class = pool->size_class[i]; |
| 2335 | if (!class) |
| 2336 | continue; |
| 2337 | if (class->index != i) |
| 2338 | continue; |
Sergey Senozhatsky | 7d3f393 | 2015-09-08 15:04:35 -0700 | [diff] [blame] | 2339 | __zs_compact(pool, class); |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2340 | } |
| 2341 | |
Sergey Senozhatsky | 860c707 | 2015-09-08 15:04:38 -0700 | [diff] [blame] | 2342 | return pool->stats.pages_compacted; |
Minchan Kim | 312fcae | 2015-04-15 16:15:30 -0700 | [diff] [blame] | 2343 | } |
| 2344 | EXPORT_SYMBOL_GPL(zs_compact); |
| 2345 | |
Sergey Senozhatsky | 7d3f393 | 2015-09-08 15:04:35 -0700 | [diff] [blame] | 2346 | void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats) |
| 2347 | { |
| 2348 | memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats)); |
| 2349 | } |
| 2350 | EXPORT_SYMBOL_GPL(zs_pool_stats); |
| 2351 | |
Sergey Senozhatsky | ab9d306 | 2015-09-08 15:04:41 -0700 | [diff] [blame] | 2352 | static unsigned long zs_shrinker_scan(struct shrinker *shrinker, |
| 2353 | struct shrink_control *sc) |
| 2354 | { |
| 2355 | unsigned long pages_freed; |
| 2356 | struct zs_pool *pool = container_of(shrinker, struct zs_pool, |
| 2357 | shrinker); |
| 2358 | |
| 2359 | pages_freed = pool->stats.pages_compacted; |
| 2360 | /* |
| 2361 | * Compact classes and calculate compaction delta. |
| 2362 | * Can run concurrently with a manually triggered |
| 2363 | * (by user) compaction. |
| 2364 | */ |
| 2365 | pages_freed = zs_compact(pool) - pages_freed; |
| 2366 | |
| 2367 | return pages_freed ? pages_freed : SHRINK_STOP; |
| 2368 | } |
| 2369 | |
| 2370 | static unsigned long zs_shrinker_count(struct shrinker *shrinker, |
| 2371 | struct shrink_control *sc) |
| 2372 | { |
| 2373 | int i; |
| 2374 | struct size_class *class; |
| 2375 | unsigned long pages_to_free = 0; |
| 2376 | struct zs_pool *pool = container_of(shrinker, struct zs_pool, |
| 2377 | shrinker); |
| 2378 | |
Sergey Senozhatsky | ab9d306 | 2015-09-08 15:04:41 -0700 | [diff] [blame] | 2379 | for (i = zs_size_classes - 1; i >= 0; i--) { |
| 2380 | class = pool->size_class[i]; |
| 2381 | if (!class) |
| 2382 | continue; |
| 2383 | if (class->index != i) |
| 2384 | continue; |
| 2385 | |
Sergey Senozhatsky | ab9d306 | 2015-09-08 15:04:41 -0700 | [diff] [blame] | 2386 | pages_to_free += zs_can_compact(class); |
Sergey Senozhatsky | ab9d306 | 2015-09-08 15:04:41 -0700 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | return pages_to_free; |
| 2390 | } |
| 2391 | |
| 2392 | static void zs_unregister_shrinker(struct zs_pool *pool) |
| 2393 | { |
| 2394 | if (pool->shrinker_enabled) { |
| 2395 | unregister_shrinker(&pool->shrinker); |
| 2396 | pool->shrinker_enabled = false; |
| 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | static int zs_register_shrinker(struct zs_pool *pool) |
| 2401 | { |
| 2402 | pool->shrinker.scan_objects = zs_shrinker_scan; |
| 2403 | pool->shrinker.count_objects = zs_shrinker_count; |
| 2404 | pool->shrinker.batch = 0; |
| 2405 | pool->shrinker.seeks = DEFAULT_SEEKS; |
| 2406 | |
| 2407 | return register_shrinker(&pool->shrinker); |
| 2408 | } |
| 2409 | |
Davidlohr Bueso | 4bbc0bc | 2013-01-04 12:14:00 -0800 | [diff] [blame] | 2410 | /** |
| 2411 | * zs_create_pool - Creates an allocation pool to work from. |
Ganesh Mahendran | fd85446 | 2016-07-28 15:47:54 -0700 | [diff] [blame] | 2412 | * @name: pool name to be created |
Davidlohr Bueso | 4bbc0bc | 2013-01-04 12:14:00 -0800 | [diff] [blame] | 2413 | * |
| 2414 | * This function must be called before anything when using |
| 2415 | * the zsmalloc allocator. |
| 2416 | * |
| 2417 | * On success, a pointer to the newly created pool is returned, |
| 2418 | * otherwise NULL. |
| 2419 | */ |
Sergey Senozhatsky | d0d8da2 | 2016-05-20 16:59:48 -0700 | [diff] [blame] | 2420 | struct zs_pool *zs_create_pool(const char *name) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2421 | { |
Ganesh Mahendran | 1813665 | 2014-12-12 16:57:10 -0800 | [diff] [blame] | 2422 | int i; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2423 | struct zs_pool *pool; |
Ganesh Mahendran | df8b5bb | 2014-12-12 16:57:07 -0800 | [diff] [blame] | 2424 | struct size_class *prev_class = NULL; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2425 | |
Ganesh Mahendran | 1813665 | 2014-12-12 16:57:10 -0800 | [diff] [blame] | 2426 | pool = kzalloc(sizeof(*pool), GFP_KERNEL); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2427 | if (!pool) |
| 2428 | return NULL; |
| 2429 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 2430 | init_deferred_free(pool); |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 2431 | pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *), |
| 2432 | GFP_KERNEL); |
| 2433 | if (!pool->size_class) { |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 2434 | kfree(pool); |
| 2435 | return NULL; |
| 2436 | } |
| 2437 | |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 2438 | pool->name = kstrdup(name, GFP_KERNEL); |
| 2439 | if (!pool->name) |
| 2440 | goto err; |
| 2441 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 2442 | if (create_cache(pool)) |
Minchan Kim | 2e40e16 | 2015-04-15 16:15:23 -0700 | [diff] [blame] | 2443 | goto err; |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 2444 | |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 2445 | /* |
| 2446 | * Iterate reversly, because, size of size_class that we want to use |
| 2447 | * for merging should be larger or equal to current size. |
| 2448 | */ |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 2449 | for (i = zs_size_classes - 1; i >= 0; i--) { |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2450 | int size; |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 2451 | int pages_per_zspage; |
Ganesh Mahendran | 64d9046 | 2016-07-28 15:47:51 -0700 | [diff] [blame] | 2452 | int objs_per_zspage; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2453 | struct size_class *class; |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 2454 | int fullness = 0; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2455 | |
| 2456 | size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA; |
| 2457 | if (size > ZS_MAX_ALLOC_SIZE) |
| 2458 | size = ZS_MAX_ALLOC_SIZE; |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 2459 | pages_per_zspage = get_pages_per_zspage(size); |
Ganesh Mahendran | 64d9046 | 2016-07-28 15:47:51 -0700 | [diff] [blame] | 2460 | objs_per_zspage = pages_per_zspage * PAGE_SIZE / size; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2461 | |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 2462 | /* |
Sergey Senozhatsky | da33b2a | 2018-04-05 16:24:43 -0700 | [diff] [blame^] | 2463 | * We iterate from biggest down to smallest classes, |
| 2464 | * so huge_class_size holds the size of the first huge |
| 2465 | * class. Any object bigger than or equal to that will |
| 2466 | * endup in the huge class. |
| 2467 | */ |
| 2468 | if (pages_per_zspage != 1 && objs_per_zspage != 1 && |
| 2469 | !huge_class_size) { |
| 2470 | huge_class_size = size; |
| 2471 | /* |
| 2472 | * The object uses ZS_HANDLE_SIZE bytes to store the |
| 2473 | * handle. We need to subtract it, because zs_malloc() |
| 2474 | * unconditionally adds handle size before it performs |
| 2475 | * size class search - so object may be smaller than |
| 2476 | * huge class size, yet it still can end up in the huge |
| 2477 | * class because it grows by ZS_HANDLE_SIZE extra bytes |
| 2478 | * right before class lookup. |
| 2479 | */ |
| 2480 | huge_class_size -= (ZS_HANDLE_SIZE - 1); |
| 2481 | } |
| 2482 | |
| 2483 | /* |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 2484 | * size_class is used for normal zsmalloc operation such |
| 2485 | * as alloc/free for that size. Although it is natural that we |
| 2486 | * have one size_class for each size, there is a chance that we |
| 2487 | * can get more memory utilization if we use one size_class for |
| 2488 | * many different sizes whose size_class have same |
| 2489 | * characteristics. So, we makes size_class point to |
| 2490 | * previous size_class if possible. |
| 2491 | */ |
Ganesh Mahendran | df8b5bb | 2014-12-12 16:57:07 -0800 | [diff] [blame] | 2492 | if (prev_class) { |
Ganesh Mahendran | 64d9046 | 2016-07-28 15:47:51 -0700 | [diff] [blame] | 2493 | if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) { |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 2494 | pool->size_class[i] = prev_class; |
| 2495 | continue; |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | class = kzalloc(sizeof(struct size_class), GFP_KERNEL); |
| 2500 | if (!class) |
| 2501 | goto err; |
| 2502 | |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2503 | class->size = size; |
| 2504 | class->index = i; |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 2505 | class->pages_per_zspage = pages_per_zspage; |
Ganesh Mahendran | 64d9046 | 2016-07-28 15:47:51 -0700 | [diff] [blame] | 2506 | class->objs_per_zspage = objs_per_zspage; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2507 | spin_lock_init(&class->lock); |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 2508 | pool->size_class[i] = class; |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 2509 | for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS; |
| 2510 | fullness++) |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 2511 | INIT_LIST_HEAD(&class->fullness_list[fullness]); |
Ganesh Mahendran | df8b5bb | 2014-12-12 16:57:07 -0800 | [diff] [blame] | 2512 | |
| 2513 | prev_class = class; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2514 | } |
| 2515 | |
Dan Streetman | d34f615 | 2016-05-20 16:59:56 -0700 | [diff] [blame] | 2516 | /* debug only, don't abort if it fails */ |
| 2517 | zs_pool_stat_create(pool, name); |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 2518 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 2519 | if (zs_register_migration(pool)) |
| 2520 | goto err; |
| 2521 | |
Sergey Senozhatsky | ab9d306 | 2015-09-08 15:04:41 -0700 | [diff] [blame] | 2522 | /* |
| 2523 | * Not critical, we still can use the pool |
| 2524 | * and user can trigger compaction manually. |
| 2525 | */ |
| 2526 | if (zs_register_shrinker(pool) == 0) |
| 2527 | pool->shrinker_enabled = true; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2528 | return pool; |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 2529 | |
| 2530 | err: |
| 2531 | zs_destroy_pool(pool); |
| 2532 | return NULL; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2533 | } |
| 2534 | EXPORT_SYMBOL_GPL(zs_create_pool); |
| 2535 | |
| 2536 | void zs_destroy_pool(struct zs_pool *pool) |
| 2537 | { |
| 2538 | int i; |
| 2539 | |
Sergey Senozhatsky | ab9d306 | 2015-09-08 15:04:41 -0700 | [diff] [blame] | 2540 | zs_unregister_shrinker(pool); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 2541 | zs_unregister_migration(pool); |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 2542 | zs_pool_stat_destroy(pool); |
| 2543 | |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 2544 | for (i = 0; i < zs_size_classes; i++) { |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2545 | int fg; |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 2546 | struct size_class *class = pool->size_class[i]; |
| 2547 | |
| 2548 | if (!class) |
| 2549 | continue; |
| 2550 | |
| 2551 | if (class->index != i) |
| 2552 | continue; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2553 | |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 2554 | for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) { |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 2555 | if (!list_empty(&class->fullness_list[fg])) { |
Marlies Ruck | 93ad5ab | 2013-05-15 16:56:49 -0400 | [diff] [blame] | 2556 | 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] | 2557 | class->size, fg); |
| 2558 | } |
| 2559 | } |
Joonsoo Kim | 9eec4cd | 2014-12-12 16:56:44 -0800 | [diff] [blame] | 2560 | kfree(class); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2561 | } |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 2562 | |
Minchan Kim | 3783689 | 2016-07-26 15:23:23 -0700 | [diff] [blame] | 2563 | destroy_cache(pool); |
Mahendran Ganesh | 40f9fb8 | 2014-12-12 16:57:01 -0800 | [diff] [blame] | 2564 | kfree(pool->size_class); |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 2565 | kfree(pool->name); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2566 | kfree(pool); |
| 2567 | } |
| 2568 | EXPORT_SYMBOL_GPL(zs_destroy_pool); |
| 2569 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 2570 | static int __init zs_init(void) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2571 | { |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 2572 | int ret; |
| 2573 | |
| 2574 | ret = zsmalloc_mount(); |
| 2575 | if (ret) |
| 2576 | goto out; |
| 2577 | |
| 2578 | ret = zs_register_cpu_notifier(); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2579 | |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 2580 | if (ret) |
| 2581 | goto notifier_fail; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2582 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 2583 | init_zs_size_classes(); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2584 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 2585 | #ifdef CONFIG_ZPOOL |
| 2586 | zpool_register_driver(&zs_zpool_driver); |
| 2587 | #endif |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 2588 | |
Dan Streetman | 4abaac9 | 2016-05-26 15:16:27 -0700 | [diff] [blame] | 2589 | zs_stat_init(); |
| 2590 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 2591 | return 0; |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 2592 | |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 2593 | notifier_fail: |
| 2594 | zs_unregister_cpu_notifier(); |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 2595 | zsmalloc_unmount(); |
| 2596 | out: |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 2597 | return ret; |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2598 | } |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2599 | |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 2600 | static void __exit zs_exit(void) |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2601 | { |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 2602 | #ifdef CONFIG_ZPOOL |
| 2603 | zpool_unregister_driver(&zs_zpool_driver); |
| 2604 | #endif |
Minchan Kim | 48b4800 | 2016-07-26 15:23:31 -0700 | [diff] [blame] | 2605 | zsmalloc_unmount(); |
Ganesh Mahendran | 66cdef6 | 2014-12-18 16:17:40 -0800 | [diff] [blame] | 2606 | zs_unregister_cpu_notifier(); |
Ganesh Mahendran | 0f050d9 | 2015-02-12 15:00:54 -0800 | [diff] [blame] | 2607 | |
| 2608 | zs_stat_exit(); |
Nitin Gupta | 61989a8 | 2012-01-09 16:51:56 -0600 | [diff] [blame] | 2609 | } |
Ben Hutchings | 069f101 | 2012-06-20 02:31:11 +0100 | [diff] [blame] | 2610 | |
| 2611 | module_init(zs_init); |
| 2612 | module_exit(zs_exit); |
| 2613 | |
| 2614 | MODULE_LICENSE("Dual BSD/GPL"); |
| 2615 | MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); |