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