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