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