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