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