Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 2 | /* |
| 3 | * z3fold.c |
| 4 | * |
| 5 | * Author: Vitaly Wool <vitaly.wool@konsulko.com> |
| 6 | * Copyright (C) 2016, Sony Mobile Communications Inc. |
| 7 | * |
| 8 | * This implementation is based on zbud written by Seth Jennings. |
| 9 | * |
| 10 | * z3fold is an special purpose allocator for storing compressed pages. It |
| 11 | * can store up to three compressed pages per page which improves the |
| 12 | * compression ratio of zbud while retaining its main concepts (e. g. always |
| 13 | * storing an integral number of objects per page) and simplicity. |
| 14 | * It still has simple and deterministic reclaim properties that make it |
| 15 | * preferable to a higher density approach (with no requirement on integral |
| 16 | * number of object per page) when reclaim is used. |
| 17 | * |
| 18 | * As in zbud, pages are divided into "chunks". The size of the chunks is |
| 19 | * fixed at compile time and is determined by NCHUNKS_ORDER below. |
| 20 | * |
| 21 | * z3fold doesn't export any API and is meant to be used via zpool API. |
| 22 | */ |
| 23 | |
| 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 25 | |
| 26 | #include <linux/atomic.h> |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 27 | #include <linux/sched.h> |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 28 | #include <linux/cpumask.h> |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 29 | #include <linux/list.h> |
| 30 | #include <linux/mm.h> |
| 31 | #include <linux/module.h> |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 32 | #include <linux/page-flags.h> |
| 33 | #include <linux/migrate.h> |
| 34 | #include <linux/node.h> |
| 35 | #include <linux/compaction.h> |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 36 | #include <linux/percpu.h> |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 37 | #include <linux/mount.h> |
David Howells | ea8157a | 2019-05-21 07:55:45 +0100 | [diff] [blame] | 38 | #include <linux/pseudo_fs.h> |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 39 | #include <linux/fs.h> |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 40 | #include <linux/preempt.h> |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 41 | #include <linux/workqueue.h> |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 42 | #include <linux/slab.h> |
| 43 | #include <linux/spinlock.h> |
Henry Burns | d776aaa | 2019-08-24 17:54:37 -0700 | [diff] [blame] | 44 | #include <linux/wait.h> |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 45 | #include <linux/zpool.h> |
David Howells | ea8157a | 2019-05-21 07:55:45 +0100 | [diff] [blame] | 46 | #include <linux/magic.h> |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 47 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 48 | /* |
| 49 | * NCHUNKS_ORDER determines the internal allocation granularity, effectively |
| 50 | * adjusting internal fragmentation. It also determines the number of |
| 51 | * freelists maintained in each pool. NCHUNKS_ORDER of 6 means that the |
Vitaly Wool | ede9321 | 2017-02-24 14:57:17 -0800 | [diff] [blame] | 52 | * allocation granularity will be in chunks of size PAGE_SIZE/64. Some chunks |
| 53 | * in the beginning of an allocated page are occupied by z3fold header, so |
| 54 | * NCHUNKS will be calculated to 63 (or 62 in case CONFIG_DEBUG_SPINLOCK=y), |
| 55 | * which shows the max number of free chunks in z3fold page, also there will |
| 56 | * be 63, or 62, respectively, freelists per pool. |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 57 | */ |
| 58 | #define NCHUNKS_ORDER 6 |
| 59 | |
| 60 | #define CHUNK_SHIFT (PAGE_SHIFT - NCHUNKS_ORDER) |
| 61 | #define CHUNK_SIZE (1 << CHUNK_SHIFT) |
Vitaly Wool | ede9321 | 2017-02-24 14:57:17 -0800 | [diff] [blame] | 62 | #define ZHDR_SIZE_ALIGNED round_up(sizeof(struct z3fold_header), CHUNK_SIZE) |
| 63 | #define ZHDR_CHUNKS (ZHDR_SIZE_ALIGNED >> CHUNK_SHIFT) |
| 64 | #define TOTAL_CHUNKS (PAGE_SIZE >> CHUNK_SHIFT) |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 65 | #define NCHUNKS ((PAGE_SIZE - ZHDR_SIZE_ALIGNED) >> CHUNK_SHIFT) |
| 66 | |
zhong jiang | f201ebd | 2017-02-22 15:46:51 -0800 | [diff] [blame] | 67 | #define BUDDY_MASK (0x3) |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 68 | #define BUDDY_SHIFT 2 |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 69 | #define SLOTS_ALIGN (0x40) |
| 70 | |
| 71 | /***************** |
| 72 | * Structures |
| 73 | *****************/ |
| 74 | struct z3fold_pool; |
| 75 | struct z3fold_ops { |
| 76 | int (*evict)(struct z3fold_pool *pool, unsigned long handle); |
| 77 | }; |
| 78 | |
| 79 | enum buddy { |
| 80 | HEADLESS = 0, |
| 81 | FIRST, |
| 82 | MIDDLE, |
| 83 | LAST, |
| 84 | BUDDIES_MAX = LAST |
| 85 | }; |
| 86 | |
| 87 | struct z3fold_buddy_slots { |
| 88 | /* |
| 89 | * we are using BUDDY_MASK in handle_to_buddy etc. so there should |
| 90 | * be enough slots to hold all possible variants |
| 91 | */ |
| 92 | unsigned long slot[BUDDY_MASK + 1]; |
| 93 | unsigned long pool; /* back link + flags */ |
| 94 | }; |
| 95 | #define HANDLE_FLAG_MASK (0x03) |
| 96 | |
| 97 | /* |
| 98 | * struct z3fold_header - z3fold page metadata occupying first chunks of each |
| 99 | * z3fold page, except for HEADLESS pages |
| 100 | * @buddy: links the z3fold page into the relevant list in the |
| 101 | * pool |
| 102 | * @page_lock: per-page lock |
| 103 | * @refcount: reference count for the z3fold page |
| 104 | * @work: work_struct for page layout optimization |
| 105 | * @slots: pointer to the structure holding buddy slots |
Vitaly Wool | bb9a374 | 2019-07-16 16:25:48 -0700 | [diff] [blame] | 106 | * @pool: pointer to the containing pool |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 107 | * @cpu: CPU which this page "belongs" to |
| 108 | * @first_chunks: the size of the first buddy in chunks, 0 if free |
| 109 | * @middle_chunks: the size of the middle buddy in chunks, 0 if free |
| 110 | * @last_chunks: the size of the last buddy in chunks, 0 if free |
| 111 | * @first_num: the starting number (for the first handle) |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 112 | * @mapped_count: the number of objects currently mapped |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 113 | */ |
| 114 | struct z3fold_header { |
| 115 | struct list_head buddy; |
| 116 | spinlock_t page_lock; |
| 117 | struct kref refcount; |
| 118 | struct work_struct work; |
| 119 | struct z3fold_buddy_slots *slots; |
Vitaly Wool | bb9a374 | 2019-07-16 16:25:48 -0700 | [diff] [blame] | 120 | struct z3fold_pool *pool; |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 121 | short cpu; |
| 122 | unsigned short first_chunks; |
| 123 | unsigned short middle_chunks; |
| 124 | unsigned short last_chunks; |
| 125 | unsigned short start_middle; |
| 126 | unsigned short first_num:2; |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 127 | unsigned short mapped_count:2; |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 128 | }; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 129 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 130 | /** |
| 131 | * struct z3fold_pool - stores metadata for each z3fold pool |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 132 | * @name: pool name |
| 133 | * @lock: protects pool unbuddied/lru lists |
| 134 | * @stale_lock: protects pool stale page list |
| 135 | * @unbuddied: per-cpu array of lists tracking z3fold pages that contain 2- |
| 136 | * buddies; the list each z3fold page is added to depends on |
| 137 | * the size of its free region. |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 138 | * @lru: list tracking the z3fold pages in LRU order by most recently |
| 139 | * added buddy. |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 140 | * @stale: list of pages marked for freeing |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 141 | * @pages_nr: number of z3fold pages in the pool. |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 142 | * @c_handle: cache for z3fold_buddy_slots allocation |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 143 | * @ops: pointer to a structure of user defined operations specified at |
| 144 | * pool creation time. |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 145 | * @compact_wq: workqueue for page layout background optimization |
| 146 | * @release_wq: workqueue for safe page release |
| 147 | * @work: work_struct for safe page release |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 148 | * @inode: inode for z3fold pseudo filesystem |
Henry Burns | d776aaa | 2019-08-24 17:54:37 -0700 | [diff] [blame] | 149 | * @destroying: bool to stop migration once we start destruction |
| 150 | * @isolated: int to count the number of pages currently in isolation |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 151 | * |
| 152 | * This structure is allocated at pool creation time and maintains metadata |
| 153 | * pertaining to a particular z3fold pool. |
| 154 | */ |
| 155 | struct z3fold_pool { |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 156 | const char *name; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 157 | spinlock_t lock; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 158 | spinlock_t stale_lock; |
| 159 | struct list_head *unbuddied; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 160 | struct list_head lru; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 161 | struct list_head stale; |
Vitaly Wool | 12d59ae | 2017-02-24 14:57:15 -0800 | [diff] [blame] | 162 | atomic64_t pages_nr; |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 163 | struct kmem_cache *c_handle; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 164 | const struct z3fold_ops *ops; |
| 165 | struct zpool *zpool; |
| 166 | const struct zpool_ops *zpool_ops; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 167 | struct workqueue_struct *compact_wq; |
| 168 | struct workqueue_struct *release_wq; |
Henry Burns | d776aaa | 2019-08-24 17:54:37 -0700 | [diff] [blame] | 169 | struct wait_queue_head isolate_wait; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 170 | struct work_struct work; |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 171 | struct inode *inode; |
Henry Burns | d776aaa | 2019-08-24 17:54:37 -0700 | [diff] [blame] | 172 | bool destroying; |
| 173 | int isolated; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 176 | /* |
| 177 | * Internal z3fold page flags |
| 178 | */ |
| 179 | enum z3fold_page_flags { |
Vitaly Wool | 5a27aa8 | 2017-02-24 14:57:26 -0800 | [diff] [blame] | 180 | PAGE_HEADLESS = 0, |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 181 | MIDDLE_CHUNK_MAPPED, |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 182 | NEEDS_COMPACTING, |
Vitaly Wool | 6098d7e | 2018-05-11 16:01:46 -0700 | [diff] [blame] | 183 | PAGE_STALE, |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 184 | PAGE_CLAIMED, /* by either reclaim or free */ |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | /***************** |
| 188 | * Helpers |
| 189 | *****************/ |
| 190 | |
| 191 | /* Converts an allocation size in bytes to size in z3fold chunks */ |
| 192 | static int size_to_chunks(size_t size) |
| 193 | { |
| 194 | return (size + CHUNK_SIZE - 1) >> CHUNK_SHIFT; |
| 195 | } |
| 196 | |
| 197 | #define for_each_unbuddied_list(_iter, _begin) \ |
| 198 | for ((_iter) = (_begin); (_iter) < NCHUNKS; (_iter)++) |
| 199 | |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 200 | static void compact_page_work(struct work_struct *w); |
| 201 | |
Vitaly Wool | bb9f6f6 | 2019-05-31 22:30:39 -0700 | [diff] [blame] | 202 | static inline struct z3fold_buddy_slots *alloc_slots(struct z3fold_pool *pool, |
| 203 | gfp_t gfp) |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 204 | { |
Henry Burns | f1549cb | 2019-07-16 16:26:03 -0700 | [diff] [blame] | 205 | struct z3fold_buddy_slots *slots; |
| 206 | |
| 207 | slots = kmem_cache_alloc(pool->c_handle, |
| 208 | (gfp & ~(__GFP_HIGHMEM | __GFP_MOVABLE))); |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 209 | |
| 210 | if (slots) { |
| 211 | memset(slots->slot, 0, sizeof(slots->slot)); |
| 212 | slots->pool = (unsigned long)pool; |
| 213 | } |
| 214 | |
| 215 | return slots; |
| 216 | } |
| 217 | |
| 218 | static inline struct z3fold_pool *slots_to_pool(struct z3fold_buddy_slots *s) |
| 219 | { |
| 220 | return (struct z3fold_pool *)(s->pool & ~HANDLE_FLAG_MASK); |
| 221 | } |
| 222 | |
| 223 | static inline struct z3fold_buddy_slots *handle_to_slots(unsigned long handle) |
| 224 | { |
| 225 | return (struct z3fold_buddy_slots *)(handle & ~(SLOTS_ALIGN - 1)); |
| 226 | } |
| 227 | |
| 228 | static inline void free_handle(unsigned long handle) |
| 229 | { |
| 230 | struct z3fold_buddy_slots *slots; |
| 231 | int i; |
| 232 | bool is_free; |
| 233 | |
| 234 | if (handle & (1 << PAGE_HEADLESS)) |
| 235 | return; |
| 236 | |
| 237 | WARN_ON(*(unsigned long *)handle == 0); |
| 238 | *(unsigned long *)handle = 0; |
| 239 | slots = handle_to_slots(handle); |
| 240 | is_free = true; |
| 241 | for (i = 0; i <= BUDDY_MASK; i++) { |
| 242 | if (slots->slot[i]) { |
| 243 | is_free = false; |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (is_free) { |
| 249 | struct z3fold_pool *pool = slots_to_pool(slots); |
| 250 | |
| 251 | kmem_cache_free(pool->c_handle, slots); |
| 252 | } |
| 253 | } |
| 254 | |
David Howells | ea8157a | 2019-05-21 07:55:45 +0100 | [diff] [blame] | 255 | static int z3fold_init_fs_context(struct fs_context *fc) |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 256 | { |
David Howells | ea8157a | 2019-05-21 07:55:45 +0100 | [diff] [blame] | 257 | return init_pseudo(fc, Z3FOLD_MAGIC) ? 0 : -ENOMEM; |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static struct file_system_type z3fold_fs = { |
| 261 | .name = "z3fold", |
David Howells | ea8157a | 2019-05-21 07:55:45 +0100 | [diff] [blame] | 262 | .init_fs_context = z3fold_init_fs_context, |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 263 | .kill_sb = kill_anon_super, |
| 264 | }; |
| 265 | |
| 266 | static struct vfsmount *z3fold_mnt; |
| 267 | static int z3fold_mount(void) |
| 268 | { |
| 269 | int ret = 0; |
| 270 | |
| 271 | z3fold_mnt = kern_mount(&z3fold_fs); |
| 272 | if (IS_ERR(z3fold_mnt)) |
| 273 | ret = PTR_ERR(z3fold_mnt); |
| 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static void z3fold_unmount(void) |
| 279 | { |
| 280 | kern_unmount(z3fold_mnt); |
| 281 | } |
| 282 | |
| 283 | static const struct address_space_operations z3fold_aops; |
| 284 | static int z3fold_register_migration(struct z3fold_pool *pool) |
| 285 | { |
| 286 | pool->inode = alloc_anon_inode(z3fold_mnt->mnt_sb); |
| 287 | if (IS_ERR(pool->inode)) { |
| 288 | pool->inode = NULL; |
| 289 | return 1; |
| 290 | } |
| 291 | |
| 292 | pool->inode->i_mapping->private_data = pool; |
| 293 | pool->inode->i_mapping->a_ops = &z3fold_aops; |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static void z3fold_unregister_migration(struct z3fold_pool *pool) |
| 298 | { |
| 299 | if (pool->inode) |
| 300 | iput(pool->inode); |
| 301 | } |
| 302 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 303 | /* Initializes the z3fold header of a newly allocated z3fold page */ |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 304 | static struct z3fold_header *init_z3fold_page(struct page *page, |
Vitaly Wool | bb9f6f6 | 2019-05-31 22:30:39 -0700 | [diff] [blame] | 305 | struct z3fold_pool *pool, gfp_t gfp) |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 306 | { |
| 307 | struct z3fold_header *zhdr = page_address(page); |
Vitaly Wool | bb9f6f6 | 2019-05-31 22:30:39 -0700 | [diff] [blame] | 308 | struct z3fold_buddy_slots *slots = alloc_slots(pool, gfp); |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 309 | |
| 310 | if (!slots) |
| 311 | return NULL; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 312 | |
| 313 | INIT_LIST_HEAD(&page->lru); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 314 | clear_bit(PAGE_HEADLESS, &page->private); |
| 315 | clear_bit(MIDDLE_CHUNK_MAPPED, &page->private); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 316 | clear_bit(NEEDS_COMPACTING, &page->private); |
| 317 | clear_bit(PAGE_STALE, &page->private); |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 318 | clear_bit(PAGE_CLAIMED, &page->private); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 319 | |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 320 | spin_lock_init(&zhdr->page_lock); |
Vitaly Wool | 5a27aa8 | 2017-02-24 14:57:26 -0800 | [diff] [blame] | 321 | kref_init(&zhdr->refcount); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 322 | zhdr->first_chunks = 0; |
| 323 | zhdr->middle_chunks = 0; |
| 324 | zhdr->last_chunks = 0; |
| 325 | zhdr->first_num = 0; |
| 326 | zhdr->start_middle = 0; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 327 | zhdr->cpu = -1; |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 328 | zhdr->slots = slots; |
Vitaly Wool | bb9a374 | 2019-07-16 16:25:48 -0700 | [diff] [blame] | 329 | zhdr->pool = pool; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 330 | INIT_LIST_HEAD(&zhdr->buddy); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 331 | INIT_WORK(&zhdr->work, compact_page_work); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 332 | return zhdr; |
| 333 | } |
| 334 | |
| 335 | /* Resets the struct page fields and frees the page */ |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 336 | static void free_z3fold_page(struct page *page, bool headless) |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 337 | { |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 338 | if (!headless) { |
| 339 | lock_page(page); |
| 340 | __ClearPageMovable(page); |
| 341 | unlock_page(page); |
| 342 | } |
| 343 | ClearPagePrivate(page); |
Vitaly Wool | 5a27aa8 | 2017-02-24 14:57:26 -0800 | [diff] [blame] | 344 | __free_page(page); |
| 345 | } |
| 346 | |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 347 | /* Lock a z3fold page */ |
| 348 | static inline void z3fold_page_lock(struct z3fold_header *zhdr) |
| 349 | { |
| 350 | spin_lock(&zhdr->page_lock); |
| 351 | } |
| 352 | |
Vitaly Wool | 76e32a2 | 2017-04-13 14:56:14 -0700 | [diff] [blame] | 353 | /* Try to lock a z3fold page */ |
| 354 | static inline int z3fold_page_trylock(struct z3fold_header *zhdr) |
| 355 | { |
| 356 | return spin_trylock(&zhdr->page_lock); |
| 357 | } |
| 358 | |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 359 | /* Unlock a z3fold page */ |
| 360 | static inline void z3fold_page_unlock(struct z3fold_header *zhdr) |
| 361 | { |
| 362 | spin_unlock(&zhdr->page_lock); |
| 363 | } |
| 364 | |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 365 | /* Helper function to build the index */ |
| 366 | static inline int __idx(struct z3fold_header *zhdr, enum buddy bud) |
| 367 | { |
| 368 | return (bud + zhdr->first_num) & BUDDY_MASK; |
| 369 | } |
| 370 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 371 | /* |
| 372 | * Encodes the handle of a particular buddy within a z3fold page |
| 373 | * Pool lock should be held as this function accesses first_num |
| 374 | */ |
| 375 | static unsigned long encode_handle(struct z3fold_header *zhdr, enum buddy bud) |
| 376 | { |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 377 | struct z3fold_buddy_slots *slots; |
| 378 | unsigned long h = (unsigned long)zhdr; |
| 379 | int idx = 0; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 380 | |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 381 | /* |
| 382 | * For a headless page, its handle is its pointer with the extra |
| 383 | * PAGE_HEADLESS bit set |
| 384 | */ |
| 385 | if (bud == HEADLESS) |
| 386 | return h | (1 << PAGE_HEADLESS); |
| 387 | |
| 388 | /* otherwise, return pointer to encoded handle */ |
| 389 | idx = __idx(zhdr, bud); |
| 390 | h += idx; |
| 391 | if (bud == LAST) |
| 392 | h |= (zhdr->last_chunks << BUDDY_SHIFT); |
| 393 | |
| 394 | slots = zhdr->slots; |
| 395 | slots->slot[idx] = h; |
| 396 | return (unsigned long)&slots->slot[idx]; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* Returns the z3fold page where a given handle is stored */ |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 400 | static inline struct z3fold_header *handle_to_z3fold_header(unsigned long h) |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 401 | { |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 402 | unsigned long addr = h; |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 403 | |
| 404 | if (!(addr & (1 << PAGE_HEADLESS))) |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 405 | addr = *(unsigned long *)h; |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 406 | |
| 407 | return (struct z3fold_header *)(addr & PAGE_MASK); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 410 | /* only for LAST bud, returns zero otherwise */ |
| 411 | static unsigned short handle_to_chunks(unsigned long handle) |
| 412 | { |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 413 | unsigned long addr = *(unsigned long *)handle; |
| 414 | |
| 415 | return (addr & ~PAGE_MASK) >> BUDDY_SHIFT; |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 416 | } |
| 417 | |
zhong jiang | f201ebd | 2017-02-22 15:46:51 -0800 | [diff] [blame] | 418 | /* |
| 419 | * (handle & BUDDY_MASK) < zhdr->first_num is possible in encode_handle |
| 420 | * but that doesn't matter. because the masking will result in the |
| 421 | * correct buddy number. |
| 422 | */ |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 423 | static enum buddy handle_to_buddy(unsigned long handle) |
| 424 | { |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 425 | struct z3fold_header *zhdr; |
| 426 | unsigned long addr; |
| 427 | |
| 428 | WARN_ON(handle & (1 << PAGE_HEADLESS)); |
| 429 | addr = *(unsigned long *)handle; |
| 430 | zhdr = (struct z3fold_header *)(addr & PAGE_MASK); |
| 431 | return (addr - zhdr->first_num) & BUDDY_MASK; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 434 | static inline struct z3fold_pool *zhdr_to_pool(struct z3fold_header *zhdr) |
| 435 | { |
Vitaly Wool | bb9a374 | 2019-07-16 16:25:48 -0700 | [diff] [blame] | 436 | return zhdr->pool; |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 439 | static void __release_z3fold_page(struct z3fold_header *zhdr, bool locked) |
| 440 | { |
| 441 | struct page *page = virt_to_page(zhdr); |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 442 | struct z3fold_pool *pool = zhdr_to_pool(zhdr); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 443 | |
| 444 | WARN_ON(!list_empty(&zhdr->buddy)); |
| 445 | set_bit(PAGE_STALE, &page->private); |
Vitaly Wool | 3552935 | 2017-10-03 16:15:06 -0700 | [diff] [blame] | 446 | clear_bit(NEEDS_COMPACTING, &page->private); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 447 | spin_lock(&pool->lock); |
| 448 | if (!list_empty(&page->lru)) |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 449 | list_del_init(&page->lru); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 450 | spin_unlock(&pool->lock); |
| 451 | if (locked) |
| 452 | z3fold_page_unlock(zhdr); |
| 453 | spin_lock(&pool->stale_lock); |
| 454 | list_add(&zhdr->buddy, &pool->stale); |
| 455 | queue_work(pool->release_wq, &pool->work); |
| 456 | spin_unlock(&pool->stale_lock); |
| 457 | } |
| 458 | |
| 459 | static void __attribute__((__unused__)) |
| 460 | release_z3fold_page(struct kref *ref) |
| 461 | { |
| 462 | struct z3fold_header *zhdr = container_of(ref, struct z3fold_header, |
| 463 | refcount); |
| 464 | __release_z3fold_page(zhdr, false); |
| 465 | } |
| 466 | |
| 467 | static void release_z3fold_page_locked(struct kref *ref) |
| 468 | { |
| 469 | struct z3fold_header *zhdr = container_of(ref, struct z3fold_header, |
| 470 | refcount); |
| 471 | WARN_ON(z3fold_page_trylock(zhdr)); |
| 472 | __release_z3fold_page(zhdr, true); |
| 473 | } |
| 474 | |
| 475 | static void release_z3fold_page_locked_list(struct kref *ref) |
| 476 | { |
| 477 | struct z3fold_header *zhdr = container_of(ref, struct z3fold_header, |
| 478 | refcount); |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 479 | struct z3fold_pool *pool = zhdr_to_pool(zhdr); |
| 480 | spin_lock(&pool->lock); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 481 | list_del_init(&zhdr->buddy); |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 482 | spin_unlock(&pool->lock); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 483 | |
| 484 | WARN_ON(z3fold_page_trylock(zhdr)); |
| 485 | __release_z3fold_page(zhdr, true); |
| 486 | } |
| 487 | |
| 488 | static void free_pages_work(struct work_struct *w) |
| 489 | { |
| 490 | struct z3fold_pool *pool = container_of(w, struct z3fold_pool, work); |
| 491 | |
| 492 | spin_lock(&pool->stale_lock); |
| 493 | while (!list_empty(&pool->stale)) { |
| 494 | struct z3fold_header *zhdr = list_first_entry(&pool->stale, |
| 495 | struct z3fold_header, buddy); |
| 496 | struct page *page = virt_to_page(zhdr); |
| 497 | |
| 498 | list_del(&zhdr->buddy); |
| 499 | if (WARN_ON(!test_bit(PAGE_STALE, &page->private))) |
| 500 | continue; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 501 | spin_unlock(&pool->stale_lock); |
| 502 | cancel_work_sync(&zhdr->work); |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 503 | free_z3fold_page(page, false); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 504 | cond_resched(); |
| 505 | spin_lock(&pool->stale_lock); |
| 506 | } |
| 507 | spin_unlock(&pool->stale_lock); |
| 508 | } |
| 509 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 510 | /* |
| 511 | * Returns the number of free chunks in a z3fold page. |
| 512 | * NB: can't be used with HEADLESS pages. |
| 513 | */ |
| 514 | static int num_free_chunks(struct z3fold_header *zhdr) |
| 515 | { |
| 516 | int nfree; |
| 517 | /* |
| 518 | * If there is a middle object, pick up the bigger free space |
| 519 | * either before or after it. Otherwise just subtract the number |
| 520 | * of chunks occupied by the first and the last objects. |
| 521 | */ |
| 522 | if (zhdr->middle_chunks != 0) { |
| 523 | int nfree_before = zhdr->first_chunks ? |
Vitaly Wool | ede9321 | 2017-02-24 14:57:17 -0800 | [diff] [blame] | 524 | 0 : zhdr->start_middle - ZHDR_CHUNKS; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 525 | int nfree_after = zhdr->last_chunks ? |
Vitaly Wool | ede9321 | 2017-02-24 14:57:17 -0800 | [diff] [blame] | 526 | 0 : TOTAL_CHUNKS - |
| 527 | (zhdr->start_middle + zhdr->middle_chunks); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 528 | nfree = max(nfree_before, nfree_after); |
| 529 | } else |
| 530 | nfree = NCHUNKS - zhdr->first_chunks - zhdr->last_chunks; |
| 531 | return nfree; |
| 532 | } |
| 533 | |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 534 | /* Add to the appropriate unbuddied list */ |
| 535 | static inline void add_to_unbuddied(struct z3fold_pool *pool, |
| 536 | struct z3fold_header *zhdr) |
| 537 | { |
| 538 | if (zhdr->first_chunks == 0 || zhdr->last_chunks == 0 || |
| 539 | zhdr->middle_chunks == 0) { |
| 540 | struct list_head *unbuddied = get_cpu_ptr(pool->unbuddied); |
| 541 | |
| 542 | int freechunks = num_free_chunks(zhdr); |
| 543 | spin_lock(&pool->lock); |
| 544 | list_add(&zhdr->buddy, &unbuddied[freechunks]); |
| 545 | spin_unlock(&pool->lock); |
| 546 | zhdr->cpu = smp_processor_id(); |
| 547 | put_cpu_ptr(pool->unbuddied); |
| 548 | } |
| 549 | } |
| 550 | |
Vitaly Wool | ede9321 | 2017-02-24 14:57:17 -0800 | [diff] [blame] | 551 | static inline void *mchunk_memmove(struct z3fold_header *zhdr, |
| 552 | unsigned short dst_chunk) |
| 553 | { |
| 554 | void *beg = zhdr; |
| 555 | return memmove(beg + (dst_chunk << CHUNK_SHIFT), |
| 556 | beg + (zhdr->start_middle << CHUNK_SHIFT), |
| 557 | zhdr->middle_chunks << CHUNK_SHIFT); |
| 558 | } |
| 559 | |
Vitaly Wool | 1b096e5 | 2017-02-24 14:57:20 -0800 | [diff] [blame] | 560 | #define BIG_CHUNK_GAP 3 |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 561 | /* Has to be called with lock held */ |
| 562 | static int z3fold_compact_page(struct z3fold_header *zhdr) |
| 563 | { |
| 564 | struct page *page = virt_to_page(zhdr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 565 | |
Vitaly Wool | ede9321 | 2017-02-24 14:57:17 -0800 | [diff] [blame] | 566 | if (test_bit(MIDDLE_CHUNK_MAPPED, &page->private)) |
| 567 | return 0; /* can't move middle chunk, it's used */ |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 568 | |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 569 | if (unlikely(PageIsolated(page))) |
| 570 | return 0; |
| 571 | |
Vitaly Wool | ede9321 | 2017-02-24 14:57:17 -0800 | [diff] [blame] | 572 | if (zhdr->middle_chunks == 0) |
| 573 | return 0; /* nothing to compact */ |
| 574 | |
| 575 | if (zhdr->first_chunks == 0 && zhdr->last_chunks == 0) { |
| 576 | /* move to the beginning */ |
| 577 | mchunk_memmove(zhdr, ZHDR_CHUNKS); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 578 | zhdr->first_chunks = zhdr->middle_chunks; |
| 579 | zhdr->middle_chunks = 0; |
| 580 | zhdr->start_middle = 0; |
| 581 | zhdr->first_num++; |
Vitaly Wool | 1b096e5 | 2017-02-24 14:57:20 -0800 | [diff] [blame] | 582 | return 1; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 583 | } |
Vitaly Wool | 1b096e5 | 2017-02-24 14:57:20 -0800 | [diff] [blame] | 584 | |
| 585 | /* |
| 586 | * moving data is expensive, so let's only do that if |
| 587 | * there's substantial gain (at least BIG_CHUNK_GAP chunks) |
| 588 | */ |
| 589 | if (zhdr->first_chunks != 0 && zhdr->last_chunks == 0 && |
| 590 | zhdr->start_middle - (zhdr->first_chunks + ZHDR_CHUNKS) >= |
| 591 | BIG_CHUNK_GAP) { |
| 592 | mchunk_memmove(zhdr, zhdr->first_chunks + ZHDR_CHUNKS); |
| 593 | zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS; |
| 594 | return 1; |
| 595 | } else if (zhdr->last_chunks != 0 && zhdr->first_chunks == 0 && |
| 596 | TOTAL_CHUNKS - (zhdr->last_chunks + zhdr->start_middle |
| 597 | + zhdr->middle_chunks) >= |
| 598 | BIG_CHUNK_GAP) { |
| 599 | unsigned short new_start = TOTAL_CHUNKS - zhdr->last_chunks - |
| 600 | zhdr->middle_chunks; |
| 601 | mchunk_memmove(zhdr, new_start); |
| 602 | zhdr->start_middle = new_start; |
| 603 | return 1; |
| 604 | } |
| 605 | |
| 606 | return 0; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 609 | static void do_compact_page(struct z3fold_header *zhdr, bool locked) |
| 610 | { |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 611 | struct z3fold_pool *pool = zhdr_to_pool(zhdr); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 612 | struct page *page; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 613 | |
| 614 | page = virt_to_page(zhdr); |
| 615 | if (locked) |
| 616 | WARN_ON(z3fold_page_trylock(zhdr)); |
| 617 | else |
| 618 | z3fold_page_lock(zhdr); |
Vitaly Wool | 5d03a66 | 2017-11-17 15:26:16 -0800 | [diff] [blame] | 619 | if (WARN_ON(!test_and_clear_bit(NEEDS_COMPACTING, &page->private))) { |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 620 | z3fold_page_unlock(zhdr); |
| 621 | return; |
| 622 | } |
| 623 | spin_lock(&pool->lock); |
| 624 | list_del_init(&zhdr->buddy); |
| 625 | spin_unlock(&pool->lock); |
| 626 | |
Vitaly Wool | 5d03a66 | 2017-11-17 15:26:16 -0800 | [diff] [blame] | 627 | if (kref_put(&zhdr->refcount, release_z3fold_page_locked)) { |
| 628 | atomic64_dec(&pool->pages_nr); |
| 629 | return; |
| 630 | } |
| 631 | |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 632 | if (unlikely(PageIsolated(page) || |
| 633 | test_bit(PAGE_STALE, &page->private))) { |
| 634 | z3fold_page_unlock(zhdr); |
| 635 | return; |
| 636 | } |
| 637 | |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 638 | z3fold_compact_page(zhdr); |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 639 | add_to_unbuddied(pool, zhdr); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 640 | z3fold_page_unlock(zhdr); |
| 641 | } |
| 642 | |
| 643 | static void compact_page_work(struct work_struct *w) |
| 644 | { |
| 645 | struct z3fold_header *zhdr = container_of(w, struct z3fold_header, |
| 646 | work); |
| 647 | |
| 648 | do_compact_page(zhdr, false); |
| 649 | } |
| 650 | |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 651 | /* returns _locked_ z3fold page header or NULL */ |
| 652 | static inline struct z3fold_header *__z3fold_alloc(struct z3fold_pool *pool, |
| 653 | size_t size, bool can_sleep) |
| 654 | { |
| 655 | struct z3fold_header *zhdr = NULL; |
| 656 | struct page *page; |
| 657 | struct list_head *unbuddied; |
| 658 | int chunks = size_to_chunks(size), i; |
| 659 | |
| 660 | lookup: |
| 661 | /* First, try to find an unbuddied z3fold page. */ |
| 662 | unbuddied = get_cpu_ptr(pool->unbuddied); |
| 663 | for_each_unbuddied_list(i, chunks) { |
| 664 | struct list_head *l = &unbuddied[i]; |
| 665 | |
| 666 | zhdr = list_first_entry_or_null(READ_ONCE(l), |
| 667 | struct z3fold_header, buddy); |
| 668 | |
| 669 | if (!zhdr) |
| 670 | continue; |
| 671 | |
| 672 | /* Re-check under lock. */ |
| 673 | spin_lock(&pool->lock); |
| 674 | l = &unbuddied[i]; |
| 675 | if (unlikely(zhdr != list_first_entry(READ_ONCE(l), |
| 676 | struct z3fold_header, buddy)) || |
| 677 | !z3fold_page_trylock(zhdr)) { |
| 678 | spin_unlock(&pool->lock); |
| 679 | zhdr = NULL; |
| 680 | put_cpu_ptr(pool->unbuddied); |
| 681 | if (can_sleep) |
| 682 | cond_resched(); |
| 683 | goto lookup; |
| 684 | } |
| 685 | list_del_init(&zhdr->buddy); |
| 686 | zhdr->cpu = -1; |
| 687 | spin_unlock(&pool->lock); |
| 688 | |
| 689 | page = virt_to_page(zhdr); |
| 690 | if (test_bit(NEEDS_COMPACTING, &page->private)) { |
| 691 | z3fold_page_unlock(zhdr); |
| 692 | zhdr = NULL; |
| 693 | put_cpu_ptr(pool->unbuddied); |
| 694 | if (can_sleep) |
| 695 | cond_resched(); |
| 696 | goto lookup; |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * this page could not be removed from its unbuddied |
| 701 | * list while pool lock was held, and then we've taken |
| 702 | * page lock so kref_put could not be called before |
| 703 | * we got here, so it's safe to just call kref_get() |
| 704 | */ |
| 705 | kref_get(&zhdr->refcount); |
| 706 | break; |
| 707 | } |
| 708 | put_cpu_ptr(pool->unbuddied); |
| 709 | |
Vitaly Wool | 351618b | 2019-05-13 17:22:46 -0700 | [diff] [blame] | 710 | if (!zhdr) { |
| 711 | int cpu; |
| 712 | |
| 713 | /* look for _exact_ match on other cpus' lists */ |
| 714 | for_each_online_cpu(cpu) { |
| 715 | struct list_head *l; |
| 716 | |
| 717 | unbuddied = per_cpu_ptr(pool->unbuddied, cpu); |
| 718 | spin_lock(&pool->lock); |
| 719 | l = &unbuddied[chunks]; |
| 720 | |
| 721 | zhdr = list_first_entry_or_null(READ_ONCE(l), |
| 722 | struct z3fold_header, buddy); |
| 723 | |
| 724 | if (!zhdr || !z3fold_page_trylock(zhdr)) { |
| 725 | spin_unlock(&pool->lock); |
| 726 | zhdr = NULL; |
| 727 | continue; |
| 728 | } |
| 729 | list_del_init(&zhdr->buddy); |
| 730 | zhdr->cpu = -1; |
| 731 | spin_unlock(&pool->lock); |
| 732 | |
| 733 | page = virt_to_page(zhdr); |
| 734 | if (test_bit(NEEDS_COMPACTING, &page->private)) { |
| 735 | z3fold_page_unlock(zhdr); |
| 736 | zhdr = NULL; |
| 737 | if (can_sleep) |
| 738 | cond_resched(); |
| 739 | continue; |
| 740 | } |
| 741 | kref_get(&zhdr->refcount); |
| 742 | break; |
| 743 | } |
| 744 | } |
| 745 | |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 746 | return zhdr; |
| 747 | } |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 748 | |
| 749 | /* |
| 750 | * API Functions |
| 751 | */ |
| 752 | |
| 753 | /** |
| 754 | * z3fold_create_pool() - create a new z3fold pool |
| 755 | * @name: pool name |
| 756 | * @gfp: gfp flags when allocating the z3fold pool structure |
| 757 | * @ops: user-defined operations for the z3fold pool |
| 758 | * |
| 759 | * Return: pointer to the new z3fold pool or NULL if the metadata allocation |
| 760 | * failed. |
| 761 | */ |
| 762 | static struct z3fold_pool *z3fold_create_pool(const char *name, gfp_t gfp, |
| 763 | const struct z3fold_ops *ops) |
| 764 | { |
| 765 | struct z3fold_pool *pool = NULL; |
| 766 | int i, cpu; |
| 767 | |
| 768 | pool = kzalloc(sizeof(struct z3fold_pool), gfp); |
| 769 | if (!pool) |
| 770 | goto out; |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 771 | pool->c_handle = kmem_cache_create("z3fold_handle", |
| 772 | sizeof(struct z3fold_buddy_slots), |
| 773 | SLOTS_ALIGN, 0, NULL); |
| 774 | if (!pool->c_handle) |
| 775 | goto out_c; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 776 | spin_lock_init(&pool->lock); |
| 777 | spin_lock_init(&pool->stale_lock); |
Henry Burns | d776aaa | 2019-08-24 17:54:37 -0700 | [diff] [blame] | 778 | init_waitqueue_head(&pool->isolate_wait); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 779 | pool->unbuddied = __alloc_percpu(sizeof(struct list_head)*NCHUNKS, 2); |
Xidong Wang | 1ec6995 | 2018-04-10 16:29:34 -0700 | [diff] [blame] | 780 | if (!pool->unbuddied) |
| 781 | goto out_pool; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 782 | for_each_possible_cpu(cpu) { |
| 783 | struct list_head *unbuddied = |
| 784 | per_cpu_ptr(pool->unbuddied, cpu); |
| 785 | for_each_unbuddied_list(i, 0) |
| 786 | INIT_LIST_HEAD(&unbuddied[i]); |
| 787 | } |
| 788 | INIT_LIST_HEAD(&pool->lru); |
| 789 | INIT_LIST_HEAD(&pool->stale); |
| 790 | atomic64_set(&pool->pages_nr, 0); |
| 791 | pool->name = name; |
| 792 | pool->compact_wq = create_singlethread_workqueue(pool->name); |
| 793 | if (!pool->compact_wq) |
Xidong Wang | 1ec6995 | 2018-04-10 16:29:34 -0700 | [diff] [blame] | 794 | goto out_unbuddied; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 795 | pool->release_wq = create_singlethread_workqueue(pool->name); |
| 796 | if (!pool->release_wq) |
| 797 | goto out_wq; |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 798 | if (z3fold_register_migration(pool)) |
| 799 | goto out_rwq; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 800 | INIT_WORK(&pool->work, free_pages_work); |
| 801 | pool->ops = ops; |
| 802 | return pool; |
| 803 | |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 804 | out_rwq: |
| 805 | destroy_workqueue(pool->release_wq); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 806 | out_wq: |
| 807 | destroy_workqueue(pool->compact_wq); |
Xidong Wang | 1ec6995 | 2018-04-10 16:29:34 -0700 | [diff] [blame] | 808 | out_unbuddied: |
| 809 | free_percpu(pool->unbuddied); |
| 810 | out_pool: |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 811 | kmem_cache_destroy(pool->c_handle); |
| 812 | out_c: |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 813 | kfree(pool); |
Xidong Wang | 1ec6995 | 2018-04-10 16:29:34 -0700 | [diff] [blame] | 814 | out: |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 815 | return NULL; |
| 816 | } |
| 817 | |
Henry Burns | d776aaa | 2019-08-24 17:54:37 -0700 | [diff] [blame] | 818 | static bool pool_isolated_are_drained(struct z3fold_pool *pool) |
| 819 | { |
| 820 | bool ret; |
| 821 | |
| 822 | spin_lock(&pool->lock); |
| 823 | ret = pool->isolated == 0; |
| 824 | spin_unlock(&pool->lock); |
| 825 | return ret; |
| 826 | } |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 827 | /** |
| 828 | * z3fold_destroy_pool() - destroys an existing z3fold pool |
| 829 | * @pool: the z3fold pool to be destroyed |
| 830 | * |
| 831 | * The pool should be emptied before this function is called. |
| 832 | */ |
| 833 | static void z3fold_destroy_pool(struct z3fold_pool *pool) |
| 834 | { |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 835 | kmem_cache_destroy(pool->c_handle); |
Henry Burns | d776aaa | 2019-08-24 17:54:37 -0700 | [diff] [blame] | 836 | /* |
| 837 | * We set pool-> destroying under lock to ensure that |
| 838 | * z3fold_page_isolate() sees any changes to destroying. This way we |
| 839 | * avoid the need for any memory barriers. |
| 840 | */ |
| 841 | |
| 842 | spin_lock(&pool->lock); |
| 843 | pool->destroying = true; |
| 844 | spin_unlock(&pool->lock); |
| 845 | |
| 846 | /* |
| 847 | * We need to ensure that no pages are being migrated while we destroy |
| 848 | * these workqueues, as migration can queue work on either of the |
| 849 | * workqueues. |
| 850 | */ |
| 851 | wait_event(pool->isolate_wait, !pool_isolated_are_drained(pool)); |
Henry Burns | 6051d3b | 2019-08-13 15:37:21 -0700 | [diff] [blame] | 852 | |
| 853 | /* |
| 854 | * We need to destroy pool->compact_wq before pool->release_wq, |
| 855 | * as any pending work on pool->compact_wq will call |
| 856 | * queue_work(pool->release_wq, &pool->work). |
Henry Burns | b997052 | 2019-08-13 15:37:25 -0700 | [diff] [blame] | 857 | * |
| 858 | * There are still outstanding pages until both workqueues are drained, |
| 859 | * so we cannot unregister migration until then. |
Henry Burns | 6051d3b | 2019-08-13 15:37:21 -0700 | [diff] [blame] | 860 | */ |
| 861 | |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 862 | destroy_workqueue(pool->compact_wq); |
Henry Burns | 6051d3b | 2019-08-13 15:37:21 -0700 | [diff] [blame] | 863 | destroy_workqueue(pool->release_wq); |
Henry Burns | b997052 | 2019-08-13 15:37:25 -0700 | [diff] [blame] | 864 | z3fold_unregister_migration(pool); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 865 | kfree(pool); |
| 866 | } |
| 867 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 868 | /** |
| 869 | * z3fold_alloc() - allocates a region of a given size |
| 870 | * @pool: z3fold pool from which to allocate |
| 871 | * @size: size in bytes of the desired allocation |
| 872 | * @gfp: gfp flags used if the pool needs to grow |
| 873 | * @handle: handle of the new allocation |
| 874 | * |
| 875 | * This function will attempt to find a free region in the pool large enough to |
| 876 | * satisfy the allocation request. A search of the unbuddied lists is |
| 877 | * performed first. If no suitable free region is found, then a new page is |
| 878 | * allocated and added to the pool to satisfy the request. |
| 879 | * |
| 880 | * gfp should not set __GFP_HIGHMEM as highmem pages cannot be used |
| 881 | * as z3fold pool pages. |
| 882 | * |
| 883 | * Return: 0 if success and handle is set, otherwise -EINVAL if the size or |
| 884 | * gfp arguments are invalid or -ENOMEM if the pool was unable to allocate |
| 885 | * a new page. |
| 886 | */ |
| 887 | static int z3fold_alloc(struct z3fold_pool *pool, size_t size, gfp_t gfp, |
| 888 | unsigned long *handle) |
| 889 | { |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 890 | int chunks = size_to_chunks(size); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 891 | struct z3fold_header *zhdr = NULL; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 892 | struct page *page = NULL; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 893 | enum buddy bud; |
Matthew Wilcox | 8a97ea54 | 2018-04-10 16:29:37 -0700 | [diff] [blame] | 894 | bool can_sleep = gfpflags_allow_blocking(gfp); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 895 | |
Henry Burns | f1549cb | 2019-07-16 16:26:03 -0700 | [diff] [blame] | 896 | if (!size) |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 897 | return -EINVAL; |
| 898 | |
| 899 | if (size > PAGE_SIZE) |
| 900 | return -ENOSPC; |
| 901 | |
| 902 | if (size > PAGE_SIZE - ZHDR_SIZE_ALIGNED - CHUNK_SIZE) |
| 903 | bud = HEADLESS; |
| 904 | else { |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 905 | retry: |
| 906 | zhdr = __z3fold_alloc(pool, size, can_sleep); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 907 | if (zhdr) { |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 908 | if (zhdr->first_chunks == 0) { |
| 909 | if (zhdr->middle_chunks != 0 && |
| 910 | chunks >= zhdr->start_middle) |
| 911 | bud = LAST; |
| 912 | else |
| 913 | bud = FIRST; |
| 914 | } else if (zhdr->last_chunks == 0) |
| 915 | bud = LAST; |
| 916 | else if (zhdr->middle_chunks == 0) |
| 917 | bud = MIDDLE; |
| 918 | else { |
Vitaly Wool | 5a27aa8 | 2017-02-24 14:57:26 -0800 | [diff] [blame] | 919 | if (kref_put(&zhdr->refcount, |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 920 | release_z3fold_page_locked)) |
Vitaly Wool | 5a27aa8 | 2017-02-24 14:57:26 -0800 | [diff] [blame] | 921 | atomic64_dec(&pool->pages_nr); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 922 | else |
| 923 | z3fold_page_unlock(zhdr); |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 924 | pr_err("No free chunks in unbuddied\n"); |
| 925 | WARN_ON(1); |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 926 | goto retry; |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 927 | } |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 928 | page = virt_to_page(zhdr); |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 929 | goto found; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 930 | } |
| 931 | bud = FIRST; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 932 | } |
| 933 | |
Vitaly Wool | 5c9bab5 | 2018-04-05 16:23:32 -0700 | [diff] [blame] | 934 | page = NULL; |
| 935 | if (can_sleep) { |
| 936 | spin_lock(&pool->stale_lock); |
| 937 | zhdr = list_first_entry_or_null(&pool->stale, |
| 938 | struct z3fold_header, buddy); |
| 939 | /* |
| 940 | * Before allocating a page, let's see if we can take one from |
| 941 | * the stale pages list. cancel_work_sync() can sleep so we |
| 942 | * limit this case to the contexts where we can sleep |
| 943 | */ |
| 944 | if (zhdr) { |
| 945 | list_del(&zhdr->buddy); |
| 946 | spin_unlock(&pool->stale_lock); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 947 | cancel_work_sync(&zhdr->work); |
Vitaly Wool | 5c9bab5 | 2018-04-05 16:23:32 -0700 | [diff] [blame] | 948 | page = virt_to_page(zhdr); |
| 949 | } else { |
| 950 | spin_unlock(&pool->stale_lock); |
| 951 | } |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 952 | } |
Vitaly Wool | 5c9bab5 | 2018-04-05 16:23:32 -0700 | [diff] [blame] | 953 | if (!page) |
| 954 | page = alloc_page(gfp); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 955 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 956 | if (!page) |
| 957 | return -ENOMEM; |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 958 | |
Vitaly Wool | bb9f6f6 | 2019-05-31 22:30:39 -0700 | [diff] [blame] | 959 | zhdr = init_z3fold_page(page, pool, gfp); |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 960 | if (!zhdr) { |
| 961 | __free_page(page); |
| 962 | return -ENOMEM; |
| 963 | } |
| 964 | atomic64_inc(&pool->pages_nr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 965 | |
| 966 | if (bud == HEADLESS) { |
| 967 | set_bit(PAGE_HEADLESS, &page->private); |
| 968 | goto headless; |
| 969 | } |
Henry Burns | 810481a | 2019-07-11 20:52:14 -0700 | [diff] [blame] | 970 | if (can_sleep) { |
| 971 | lock_page(page); |
| 972 | __SetPageMovable(page, pool->inode->i_mapping); |
| 973 | unlock_page(page); |
| 974 | } else { |
| 975 | if (trylock_page(page)) { |
| 976 | __SetPageMovable(page, pool->inode->i_mapping); |
| 977 | unlock_page(page); |
| 978 | } |
| 979 | } |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 980 | z3fold_page_lock(zhdr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 981 | |
| 982 | found: |
| 983 | if (bud == FIRST) |
| 984 | zhdr->first_chunks = chunks; |
| 985 | else if (bud == LAST) |
| 986 | zhdr->last_chunks = chunks; |
| 987 | else { |
| 988 | zhdr->middle_chunks = chunks; |
Vitaly Wool | ede9321 | 2017-02-24 14:57:17 -0800 | [diff] [blame] | 989 | zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 990 | } |
Vitaly Wool | 9050cce | 2019-05-13 17:22:43 -0700 | [diff] [blame] | 991 | add_to_unbuddied(pool, zhdr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 992 | |
| 993 | headless: |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 994 | spin_lock(&pool->lock); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 995 | /* Add/move z3fold page to beginning of LRU */ |
| 996 | if (!list_empty(&page->lru)) |
| 997 | list_del(&page->lru); |
| 998 | |
| 999 | list_add(&page->lru, &pool->lru); |
| 1000 | |
| 1001 | *handle = encode_handle(zhdr, bud); |
| 1002 | spin_unlock(&pool->lock); |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 1003 | if (bud != HEADLESS) |
| 1004 | z3fold_page_unlock(zhdr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1005 | |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * z3fold_free() - frees the allocation associated with the given handle |
| 1011 | * @pool: pool in which the allocation resided |
| 1012 | * @handle: handle associated with the allocation returned by z3fold_alloc() |
| 1013 | * |
| 1014 | * In the case that the z3fold page in which the allocation resides is under |
| 1015 | * reclaim, as indicated by the PG_reclaim flag being set, this function |
| 1016 | * only sets the first|last_chunks to 0. The page is actually freed |
| 1017 | * once both buddies are evicted (see z3fold_reclaim_page() below). |
| 1018 | */ |
| 1019 | static void z3fold_free(struct z3fold_pool *pool, unsigned long handle) |
| 1020 | { |
| 1021 | struct z3fold_header *zhdr; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1022 | struct page *page; |
| 1023 | enum buddy bud; |
| 1024 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1025 | zhdr = handle_to_z3fold_header(handle); |
| 1026 | page = virt_to_page(zhdr); |
| 1027 | |
| 1028 | if (test_bit(PAGE_HEADLESS, &page->private)) { |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 1029 | /* if a headless page is under reclaim, just leave. |
| 1030 | * NB: we use test_and_set_bit for a reason: if the bit |
| 1031 | * has not been set before, we release this page |
| 1032 | * immediately so we don't care about its value any more. |
| 1033 | */ |
| 1034 | if (!test_and_set_bit(PAGE_CLAIMED, &page->private)) { |
| 1035 | spin_lock(&pool->lock); |
| 1036 | list_del(&page->lru); |
| 1037 | spin_unlock(&pool->lock); |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1038 | free_z3fold_page(page, true); |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 1039 | atomic64_dec(&pool->pages_nr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1040 | } |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 1041 | return; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 1044 | /* Non-headless case */ |
| 1045 | z3fold_page_lock(zhdr); |
| 1046 | bud = handle_to_buddy(handle); |
| 1047 | |
| 1048 | switch (bud) { |
| 1049 | case FIRST: |
| 1050 | zhdr->first_chunks = 0; |
| 1051 | break; |
| 1052 | case MIDDLE: |
| 1053 | zhdr->middle_chunks = 0; |
| 1054 | break; |
| 1055 | case LAST: |
| 1056 | zhdr->last_chunks = 0; |
| 1057 | break; |
| 1058 | default: |
| 1059 | pr_err("%s: unknown bud %d\n", __func__, bud); |
| 1060 | WARN_ON(1); |
| 1061 | z3fold_page_unlock(zhdr); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 1062 | return; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Vitaly Wool | 7c2b8ba | 2019-05-13 17:22:49 -0700 | [diff] [blame] | 1065 | free_handle(handle); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 1066 | if (kref_put(&zhdr->refcount, release_z3fold_page_locked_list)) { |
| 1067 | atomic64_dec(&pool->pages_nr); |
| 1068 | return; |
| 1069 | } |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 1070 | if (test_bit(PAGE_CLAIMED, &page->private)) { |
Vitaly Wool | 6098d7e | 2018-05-11 16:01:46 -0700 | [diff] [blame] | 1071 | z3fold_page_unlock(zhdr); |
| 1072 | return; |
| 1073 | } |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1074 | if (unlikely(PageIsolated(page)) || |
| 1075 | test_and_set_bit(NEEDS_COMPACTING, &page->private)) { |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 1076 | z3fold_page_unlock(zhdr); |
| 1077 | return; |
| 1078 | } |
| 1079 | if (zhdr->cpu < 0 || !cpu_online(zhdr->cpu)) { |
| 1080 | spin_lock(&pool->lock); |
| 1081 | list_del_init(&zhdr->buddy); |
| 1082 | spin_unlock(&pool->lock); |
| 1083 | zhdr->cpu = -1; |
Vitaly Wool | 5d03a66 | 2017-11-17 15:26:16 -0800 | [diff] [blame] | 1084 | kref_get(&zhdr->refcount); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 1085 | do_compact_page(zhdr, true); |
| 1086 | return; |
| 1087 | } |
Vitaly Wool | 5d03a66 | 2017-11-17 15:26:16 -0800 | [diff] [blame] | 1088 | kref_get(&zhdr->refcount); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 1089 | queue_work_on(zhdr->cpu, pool->compact_wq, &zhdr->work); |
| 1090 | z3fold_page_unlock(zhdr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /** |
| 1094 | * z3fold_reclaim_page() - evicts allocations from a pool page and frees it |
| 1095 | * @pool: pool from which a page will attempt to be evicted |
Mike Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 1096 | * @retries: number of pages on the LRU list for which eviction will |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1097 | * be attempted before failing |
| 1098 | * |
| 1099 | * z3fold reclaim is different from normal system reclaim in that it is done |
| 1100 | * from the bottom, up. This is because only the bottom layer, z3fold, has |
| 1101 | * information on how the allocations are organized within each z3fold page. |
| 1102 | * This has the potential to create interesting locking situations between |
| 1103 | * z3fold and the user, however. |
| 1104 | * |
| 1105 | * To avoid these, this is how z3fold_reclaim_page() should be called: |
Mike Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 1106 | * |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1107 | * The user detects a page should be reclaimed and calls z3fold_reclaim_page(). |
| 1108 | * z3fold_reclaim_page() will remove a z3fold page from the pool LRU list and |
| 1109 | * call the user-defined eviction handler with the pool and handle as |
| 1110 | * arguments. |
| 1111 | * |
| 1112 | * If the handle can not be evicted, the eviction handler should return |
| 1113 | * non-zero. z3fold_reclaim_page() will add the z3fold page back to the |
| 1114 | * appropriate list and try the next z3fold page on the LRU up to |
| 1115 | * a user defined number of retries. |
| 1116 | * |
| 1117 | * If the handle is successfully evicted, the eviction handler should |
| 1118 | * return 0 _and_ should have called z3fold_free() on the handle. z3fold_free() |
| 1119 | * contains logic to delay freeing the page if the page is under reclaim, |
| 1120 | * as indicated by the setting of the PG_reclaim flag on the underlying page. |
| 1121 | * |
| 1122 | * If all buddies in the z3fold page are successfully evicted, then the |
| 1123 | * z3fold page can be freed. |
| 1124 | * |
| 1125 | * Returns: 0 if page is successfully freed, otherwise -EINVAL if there are |
| 1126 | * no pages to evict or an eviction handler is not registered, -EAGAIN if |
| 1127 | * the retry limit was hit. |
| 1128 | */ |
| 1129 | static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries) |
| 1130 | { |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 1131 | int i, ret = 0; |
| 1132 | struct z3fold_header *zhdr = NULL; |
| 1133 | struct page *page = NULL; |
| 1134 | struct list_head *pos; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1135 | unsigned long first_handle = 0, middle_handle = 0, last_handle = 0; |
| 1136 | |
| 1137 | spin_lock(&pool->lock); |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 1138 | if (!pool->ops || !pool->ops->evict || retries == 0) { |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1139 | spin_unlock(&pool->lock); |
| 1140 | return -EINVAL; |
| 1141 | } |
| 1142 | for (i = 0; i < retries; i++) { |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 1143 | if (list_empty(&pool->lru)) { |
| 1144 | spin_unlock(&pool->lock); |
| 1145 | return -EINVAL; |
| 1146 | } |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 1147 | list_for_each_prev(pos, &pool->lru) { |
| 1148 | page = list_entry(pos, struct page, lru); |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 1149 | |
| 1150 | /* this bit could have been set by free, in which case |
| 1151 | * we pass over to the next page in the pool. |
| 1152 | */ |
| 1153 | if (test_and_set_bit(PAGE_CLAIMED, &page->private)) |
| 1154 | continue; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1155 | |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1156 | if (unlikely(PageIsolated(page))) |
| 1157 | continue; |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 1158 | if (test_bit(PAGE_HEADLESS, &page->private)) |
| 1159 | break; |
| 1160 | |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1161 | zhdr = page_address(page); |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 1162 | if (!z3fold_page_trylock(zhdr)) { |
| 1163 | zhdr = NULL; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 1164 | continue; /* can't evict at this point */ |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 1165 | } |
Vitaly Wool | 5a27aa8 | 2017-02-24 14:57:26 -0800 | [diff] [blame] | 1166 | kref_get(&zhdr->refcount); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 1167 | list_del_init(&zhdr->buddy); |
| 1168 | zhdr->cpu = -1; |
Vitaly Wool | 6098d7e | 2018-05-11 16:01:46 -0700 | [diff] [blame] | 1169 | break; |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 1172 | if (!zhdr) |
| 1173 | break; |
| 1174 | |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 1175 | list_del_init(&page->lru); |
| 1176 | spin_unlock(&pool->lock); |
| 1177 | |
| 1178 | if (!test_bit(PAGE_HEADLESS, &page->private)) { |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1179 | /* |
| 1180 | * We need encode the handles before unlocking, since |
| 1181 | * we can race with free that will set |
| 1182 | * (first|last)_chunks to 0 |
| 1183 | */ |
| 1184 | first_handle = 0; |
| 1185 | last_handle = 0; |
| 1186 | middle_handle = 0; |
| 1187 | if (zhdr->first_chunks) |
| 1188 | first_handle = encode_handle(zhdr, FIRST); |
| 1189 | if (zhdr->middle_chunks) |
| 1190 | middle_handle = encode_handle(zhdr, MIDDLE); |
| 1191 | if (zhdr->last_chunks) |
| 1192 | last_handle = encode_handle(zhdr, LAST); |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 1193 | /* |
| 1194 | * it's safe to unlock here because we hold a |
| 1195 | * reference to this page |
| 1196 | */ |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 1197 | z3fold_page_unlock(zhdr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1198 | } else { |
| 1199 | first_handle = encode_handle(zhdr, HEADLESS); |
| 1200 | last_handle = middle_handle = 0; |
| 1201 | } |
| 1202 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1203 | /* Issue the eviction callback(s) */ |
| 1204 | if (middle_handle) { |
| 1205 | ret = pool->ops->evict(pool, middle_handle); |
| 1206 | if (ret) |
| 1207 | goto next; |
| 1208 | } |
| 1209 | if (first_handle) { |
| 1210 | ret = pool->ops->evict(pool, first_handle); |
| 1211 | if (ret) |
| 1212 | goto next; |
| 1213 | } |
| 1214 | if (last_handle) { |
| 1215 | ret = pool->ops->evict(pool, last_handle); |
| 1216 | if (ret) |
| 1217 | goto next; |
| 1218 | } |
| 1219 | next: |
Vitaly Wool | 5a27aa8 | 2017-02-24 14:57:26 -0800 | [diff] [blame] | 1220 | if (test_bit(PAGE_HEADLESS, &page->private)) { |
| 1221 | if (ret == 0) { |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1222 | free_z3fold_page(page, true); |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 1223 | atomic64_dec(&pool->pages_nr); |
Vitaly Wool | 5a27aa8 | 2017-02-24 14:57:26 -0800 | [diff] [blame] | 1224 | return 0; |
Vitaly Wool | 5a27aa8 | 2017-02-24 14:57:26 -0800 | [diff] [blame] | 1225 | } |
Vitaly Wool | 6098d7e | 2018-05-11 16:01:46 -0700 | [diff] [blame] | 1226 | spin_lock(&pool->lock); |
| 1227 | list_add(&page->lru, &pool->lru); |
Vitaly Wool | d5567c9 | 2017-10-03 16:14:47 -0700 | [diff] [blame] | 1228 | spin_unlock(&pool->lock); |
Vitaly Wool | 6098d7e | 2018-05-11 16:01:46 -0700 | [diff] [blame] | 1229 | } else { |
| 1230 | z3fold_page_lock(zhdr); |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 1231 | clear_bit(PAGE_CLAIMED, &page->private); |
Vitaly Wool | 6098d7e | 2018-05-11 16:01:46 -0700 | [diff] [blame] | 1232 | if (kref_put(&zhdr->refcount, |
| 1233 | release_z3fold_page_locked)) { |
| 1234 | atomic64_dec(&pool->pages_nr); |
| 1235 | return 0; |
| 1236 | } |
| 1237 | /* |
| 1238 | * if we are here, the page is still not completely |
| 1239 | * free. Take the global pool lock then to be able |
| 1240 | * to add it back to the lru list |
| 1241 | */ |
| 1242 | spin_lock(&pool->lock); |
| 1243 | list_add(&page->lru, &pool->lru); |
| 1244 | spin_unlock(&pool->lock); |
| 1245 | z3fold_page_unlock(zhdr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
Vitaly Wool | 6098d7e | 2018-05-11 16:01:46 -0700 | [diff] [blame] | 1248 | /* We started off locked to we need to lock the pool back */ |
| 1249 | spin_lock(&pool->lock); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1250 | } |
| 1251 | spin_unlock(&pool->lock); |
| 1252 | return -EAGAIN; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * z3fold_map() - maps the allocation associated with the given handle |
| 1257 | * @pool: pool in which the allocation resides |
| 1258 | * @handle: handle associated with the allocation to be mapped |
| 1259 | * |
| 1260 | * Extracts the buddy number from handle and constructs the pointer to the |
| 1261 | * correct starting chunk within the page. |
| 1262 | * |
| 1263 | * Returns: a pointer to the mapped allocation |
| 1264 | */ |
| 1265 | static void *z3fold_map(struct z3fold_pool *pool, unsigned long handle) |
| 1266 | { |
| 1267 | struct z3fold_header *zhdr; |
| 1268 | struct page *page; |
| 1269 | void *addr; |
| 1270 | enum buddy buddy; |
| 1271 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1272 | zhdr = handle_to_z3fold_header(handle); |
| 1273 | addr = zhdr; |
| 1274 | page = virt_to_page(zhdr); |
| 1275 | |
| 1276 | if (test_bit(PAGE_HEADLESS, &page->private)) |
| 1277 | goto out; |
| 1278 | |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 1279 | z3fold_page_lock(zhdr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1280 | buddy = handle_to_buddy(handle); |
| 1281 | switch (buddy) { |
| 1282 | case FIRST: |
| 1283 | addr += ZHDR_SIZE_ALIGNED; |
| 1284 | break; |
| 1285 | case MIDDLE: |
| 1286 | addr += zhdr->start_middle << CHUNK_SHIFT; |
| 1287 | set_bit(MIDDLE_CHUNK_MAPPED, &page->private); |
| 1288 | break; |
| 1289 | case LAST: |
Vitaly Wool | ca0246b | 2018-11-16 15:07:56 -0800 | [diff] [blame] | 1290 | addr += PAGE_SIZE - (handle_to_chunks(handle) << CHUNK_SHIFT); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1291 | break; |
| 1292 | default: |
| 1293 | pr_err("unknown buddy id %d\n", buddy); |
| 1294 | WARN_ON(1); |
| 1295 | addr = NULL; |
| 1296 | break; |
| 1297 | } |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 1298 | |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1299 | if (addr) |
| 1300 | zhdr->mapped_count++; |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 1301 | z3fold_page_unlock(zhdr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1302 | out: |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1303 | return addr; |
| 1304 | } |
| 1305 | |
| 1306 | /** |
| 1307 | * z3fold_unmap() - unmaps the allocation associated with the given handle |
| 1308 | * @pool: pool in which the allocation resides |
| 1309 | * @handle: handle associated with the allocation to be unmapped |
| 1310 | */ |
| 1311 | static void z3fold_unmap(struct z3fold_pool *pool, unsigned long handle) |
| 1312 | { |
| 1313 | struct z3fold_header *zhdr; |
| 1314 | struct page *page; |
| 1315 | enum buddy buddy; |
| 1316 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1317 | zhdr = handle_to_z3fold_header(handle); |
| 1318 | page = virt_to_page(zhdr); |
| 1319 | |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 1320 | if (test_bit(PAGE_HEADLESS, &page->private)) |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1321 | return; |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1322 | |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 1323 | z3fold_page_lock(zhdr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1324 | buddy = handle_to_buddy(handle); |
| 1325 | if (buddy == MIDDLE) |
| 1326 | clear_bit(MIDDLE_CHUNK_MAPPED, &page->private); |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1327 | zhdr->mapped_count--; |
Vitaly Wool | 2f1e5e4 | 2017-02-24 14:57:23 -0800 | [diff] [blame] | 1328 | z3fold_page_unlock(zhdr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | /** |
| 1332 | * z3fold_get_pool_size() - gets the z3fold pool size in pages |
| 1333 | * @pool: pool whose size is being queried |
| 1334 | * |
Vitaly Wool | 12d59ae | 2017-02-24 14:57:15 -0800 | [diff] [blame] | 1335 | * Returns: size in pages of the given pool. |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1336 | */ |
| 1337 | static u64 z3fold_get_pool_size(struct z3fold_pool *pool) |
| 1338 | { |
Vitaly Wool | 12d59ae | 2017-02-24 14:57:15 -0800 | [diff] [blame] | 1339 | return atomic64_read(&pool->pages_nr); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Henry Burns | d776aaa | 2019-08-24 17:54:37 -0700 | [diff] [blame] | 1342 | /* |
| 1343 | * z3fold_dec_isolated() expects to be called while pool->lock is held. |
| 1344 | */ |
| 1345 | static void z3fold_dec_isolated(struct z3fold_pool *pool) |
| 1346 | { |
| 1347 | assert_spin_locked(&pool->lock); |
| 1348 | VM_BUG_ON(pool->isolated <= 0); |
| 1349 | pool->isolated--; |
| 1350 | |
| 1351 | /* |
| 1352 | * If we have no more isolated pages, we have to see if |
| 1353 | * z3fold_destroy_pool() is waiting for a signal. |
| 1354 | */ |
| 1355 | if (pool->isolated == 0 && waitqueue_active(&pool->isolate_wait)) |
| 1356 | wake_up_all(&pool->isolate_wait); |
| 1357 | } |
| 1358 | |
| 1359 | static void z3fold_inc_isolated(struct z3fold_pool *pool) |
| 1360 | { |
| 1361 | pool->isolated++; |
| 1362 | } |
| 1363 | |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1364 | static bool z3fold_page_isolate(struct page *page, isolate_mode_t mode) |
| 1365 | { |
| 1366 | struct z3fold_header *zhdr; |
| 1367 | struct z3fold_pool *pool; |
| 1368 | |
| 1369 | VM_BUG_ON_PAGE(!PageMovable(page), page); |
| 1370 | VM_BUG_ON_PAGE(PageIsolated(page), page); |
| 1371 | |
| 1372 | if (test_bit(PAGE_HEADLESS, &page->private)) |
| 1373 | return false; |
| 1374 | |
| 1375 | zhdr = page_address(page); |
| 1376 | z3fold_page_lock(zhdr); |
| 1377 | if (test_bit(NEEDS_COMPACTING, &page->private) || |
| 1378 | test_bit(PAGE_STALE, &page->private)) |
| 1379 | goto out; |
| 1380 | |
| 1381 | pool = zhdr_to_pool(zhdr); |
| 1382 | |
| 1383 | if (zhdr->mapped_count == 0) { |
| 1384 | kref_get(&zhdr->refcount); |
| 1385 | if (!list_empty(&zhdr->buddy)) |
| 1386 | list_del_init(&zhdr->buddy); |
| 1387 | spin_lock(&pool->lock); |
| 1388 | if (!list_empty(&page->lru)) |
| 1389 | list_del(&page->lru); |
Henry Burns | d776aaa | 2019-08-24 17:54:37 -0700 | [diff] [blame] | 1390 | /* |
| 1391 | * We need to check for destruction while holding pool->lock, as |
| 1392 | * otherwise destruction could see 0 isolated pages, and |
| 1393 | * proceed. |
| 1394 | */ |
| 1395 | if (unlikely(pool->destroying)) { |
| 1396 | spin_unlock(&pool->lock); |
| 1397 | /* |
| 1398 | * If this page isn't stale, somebody else holds a |
| 1399 | * reference to it. Let't drop our refcount so that they |
| 1400 | * can call the release logic. |
| 1401 | */ |
| 1402 | if (unlikely(kref_put(&zhdr->refcount, |
| 1403 | release_z3fold_page_locked))) { |
| 1404 | /* |
| 1405 | * If we get here we have kref problems, so we |
| 1406 | * should freak out. |
| 1407 | */ |
| 1408 | WARN(1, "Z3fold is experiencing kref problems\n"); |
Gustavo A. R. Silva | 14108b9 | 2019-08-30 16:04:43 -0700 | [diff] [blame] | 1409 | z3fold_page_unlock(zhdr); |
Henry Burns | d776aaa | 2019-08-24 17:54:37 -0700 | [diff] [blame] | 1410 | return false; |
| 1411 | } |
| 1412 | z3fold_page_unlock(zhdr); |
| 1413 | return false; |
| 1414 | } |
| 1415 | |
| 1416 | |
| 1417 | z3fold_inc_isolated(pool); |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1418 | spin_unlock(&pool->lock); |
| 1419 | z3fold_page_unlock(zhdr); |
| 1420 | return true; |
| 1421 | } |
| 1422 | out: |
| 1423 | z3fold_page_unlock(zhdr); |
| 1424 | return false; |
| 1425 | } |
| 1426 | |
| 1427 | static int z3fold_page_migrate(struct address_space *mapping, struct page *newpage, |
| 1428 | struct page *page, enum migrate_mode mode) |
| 1429 | { |
| 1430 | struct z3fold_header *zhdr, *new_zhdr; |
| 1431 | struct z3fold_pool *pool; |
| 1432 | struct address_space *new_mapping; |
| 1433 | |
| 1434 | VM_BUG_ON_PAGE(!PageMovable(page), page); |
| 1435 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
Henry Burns | 810481a | 2019-07-11 20:52:14 -0700 | [diff] [blame] | 1436 | VM_BUG_ON_PAGE(!PageLocked(newpage), newpage); |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1437 | |
| 1438 | zhdr = page_address(page); |
| 1439 | pool = zhdr_to_pool(zhdr); |
| 1440 | |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1441 | if (!z3fold_page_trylock(zhdr)) { |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1442 | return -EAGAIN; |
| 1443 | } |
| 1444 | if (zhdr->mapped_count != 0) { |
| 1445 | z3fold_page_unlock(zhdr); |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1446 | return -EBUSY; |
| 1447 | } |
Henry Burns | c92d2f3 | 2019-07-16 16:26:21 -0700 | [diff] [blame] | 1448 | if (work_pending(&zhdr->work)) { |
| 1449 | z3fold_page_unlock(zhdr); |
| 1450 | return -EAGAIN; |
| 1451 | } |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1452 | new_zhdr = page_address(newpage); |
| 1453 | memcpy(new_zhdr, zhdr, PAGE_SIZE); |
| 1454 | newpage->private = page->private; |
| 1455 | page->private = 0; |
| 1456 | z3fold_page_unlock(zhdr); |
| 1457 | spin_lock_init(&new_zhdr->page_lock); |
Henry Burns | c92d2f3 | 2019-07-16 16:26:21 -0700 | [diff] [blame] | 1458 | INIT_WORK(&new_zhdr->work, compact_page_work); |
| 1459 | /* |
| 1460 | * z3fold_page_isolate() ensures that new_zhdr->buddy is empty, |
| 1461 | * so we only have to reinitialize it. |
| 1462 | */ |
| 1463 | INIT_LIST_HEAD(&new_zhdr->buddy); |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1464 | new_mapping = page_mapping(page); |
| 1465 | __ClearPageMovable(page); |
| 1466 | ClearPagePrivate(page); |
| 1467 | |
| 1468 | get_page(newpage); |
| 1469 | z3fold_page_lock(new_zhdr); |
| 1470 | if (new_zhdr->first_chunks) |
| 1471 | encode_handle(new_zhdr, FIRST); |
| 1472 | if (new_zhdr->last_chunks) |
| 1473 | encode_handle(new_zhdr, LAST); |
| 1474 | if (new_zhdr->middle_chunks) |
| 1475 | encode_handle(new_zhdr, MIDDLE); |
| 1476 | set_bit(NEEDS_COMPACTING, &newpage->private); |
| 1477 | new_zhdr->cpu = smp_processor_id(); |
| 1478 | spin_lock(&pool->lock); |
| 1479 | list_add(&newpage->lru, &pool->lru); |
| 1480 | spin_unlock(&pool->lock); |
| 1481 | __SetPageMovable(newpage, new_mapping); |
| 1482 | z3fold_page_unlock(new_zhdr); |
| 1483 | |
| 1484 | queue_work_on(new_zhdr->cpu, pool->compact_wq, &new_zhdr->work); |
| 1485 | |
Henry Burns | d776aaa | 2019-08-24 17:54:37 -0700 | [diff] [blame] | 1486 | spin_lock(&pool->lock); |
| 1487 | z3fold_dec_isolated(pool); |
| 1488 | spin_unlock(&pool->lock); |
| 1489 | |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1490 | page_mapcount_reset(page); |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1491 | put_page(page); |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
| 1495 | static void z3fold_page_putback(struct page *page) |
| 1496 | { |
| 1497 | struct z3fold_header *zhdr; |
| 1498 | struct z3fold_pool *pool; |
| 1499 | |
| 1500 | zhdr = page_address(page); |
| 1501 | pool = zhdr_to_pool(zhdr); |
| 1502 | |
| 1503 | z3fold_page_lock(zhdr); |
| 1504 | if (!list_empty(&zhdr->buddy)) |
| 1505 | list_del_init(&zhdr->buddy); |
| 1506 | INIT_LIST_HEAD(&page->lru); |
| 1507 | if (kref_put(&zhdr->refcount, release_z3fold_page_locked)) { |
| 1508 | atomic64_dec(&pool->pages_nr); |
Henry Burns | d776aaa | 2019-08-24 17:54:37 -0700 | [diff] [blame] | 1509 | spin_lock(&pool->lock); |
| 1510 | z3fold_dec_isolated(pool); |
| 1511 | spin_unlock(&pool->lock); |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1512 | return; |
| 1513 | } |
| 1514 | spin_lock(&pool->lock); |
| 1515 | list_add(&page->lru, &pool->lru); |
Henry Burns | d776aaa | 2019-08-24 17:54:37 -0700 | [diff] [blame] | 1516 | z3fold_dec_isolated(pool); |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1517 | spin_unlock(&pool->lock); |
| 1518 | z3fold_page_unlock(zhdr); |
| 1519 | } |
| 1520 | |
| 1521 | static const struct address_space_operations z3fold_aops = { |
| 1522 | .isolate_page = z3fold_page_isolate, |
| 1523 | .migratepage = z3fold_page_migrate, |
| 1524 | .putback_page = z3fold_page_putback, |
| 1525 | }; |
| 1526 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1527 | /***************** |
| 1528 | * zpool |
| 1529 | ****************/ |
| 1530 | |
| 1531 | static int z3fold_zpool_evict(struct z3fold_pool *pool, unsigned long handle) |
| 1532 | { |
| 1533 | if (pool->zpool && pool->zpool_ops && pool->zpool_ops->evict) |
| 1534 | return pool->zpool_ops->evict(pool->zpool, handle); |
| 1535 | else |
| 1536 | return -ENOENT; |
| 1537 | } |
| 1538 | |
| 1539 | static const struct z3fold_ops z3fold_zpool_ops = { |
| 1540 | .evict = z3fold_zpool_evict |
| 1541 | }; |
| 1542 | |
| 1543 | static void *z3fold_zpool_create(const char *name, gfp_t gfp, |
| 1544 | const struct zpool_ops *zpool_ops, |
| 1545 | struct zpool *zpool) |
| 1546 | { |
| 1547 | struct z3fold_pool *pool; |
| 1548 | |
Vitaly Wool | d30561c | 2017-09-06 16:24:47 -0700 | [diff] [blame] | 1549 | pool = z3fold_create_pool(name, gfp, |
| 1550 | zpool_ops ? &z3fold_zpool_ops : NULL); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1551 | if (pool) { |
| 1552 | pool->zpool = zpool; |
| 1553 | pool->zpool_ops = zpool_ops; |
| 1554 | } |
| 1555 | return pool; |
| 1556 | } |
| 1557 | |
| 1558 | static void z3fold_zpool_destroy(void *pool) |
| 1559 | { |
| 1560 | z3fold_destroy_pool(pool); |
| 1561 | } |
| 1562 | |
| 1563 | static int z3fold_zpool_malloc(void *pool, size_t size, gfp_t gfp, |
| 1564 | unsigned long *handle) |
| 1565 | { |
| 1566 | return z3fold_alloc(pool, size, gfp, handle); |
| 1567 | } |
| 1568 | static void z3fold_zpool_free(void *pool, unsigned long handle) |
| 1569 | { |
| 1570 | z3fold_free(pool, handle); |
| 1571 | } |
| 1572 | |
| 1573 | static int z3fold_zpool_shrink(void *pool, unsigned int pages, |
| 1574 | unsigned int *reclaimed) |
| 1575 | { |
| 1576 | unsigned int total = 0; |
| 1577 | int ret = -EINVAL; |
| 1578 | |
| 1579 | while (total < pages) { |
| 1580 | ret = z3fold_reclaim_page(pool, 8); |
| 1581 | if (ret < 0) |
| 1582 | break; |
| 1583 | total++; |
| 1584 | } |
| 1585 | |
| 1586 | if (reclaimed) |
| 1587 | *reclaimed = total; |
| 1588 | |
| 1589 | return ret; |
| 1590 | } |
| 1591 | |
| 1592 | static void *z3fold_zpool_map(void *pool, unsigned long handle, |
| 1593 | enum zpool_mapmode mm) |
| 1594 | { |
| 1595 | return z3fold_map(pool, handle); |
| 1596 | } |
| 1597 | static void z3fold_zpool_unmap(void *pool, unsigned long handle) |
| 1598 | { |
| 1599 | z3fold_unmap(pool, handle); |
| 1600 | } |
| 1601 | |
| 1602 | static u64 z3fold_zpool_total_size(void *pool) |
| 1603 | { |
| 1604 | return z3fold_get_pool_size(pool) * PAGE_SIZE; |
| 1605 | } |
| 1606 | |
| 1607 | static struct zpool_driver z3fold_zpool_driver = { |
| 1608 | .type = "z3fold", |
| 1609 | .owner = THIS_MODULE, |
| 1610 | .create = z3fold_zpool_create, |
| 1611 | .destroy = z3fold_zpool_destroy, |
| 1612 | .malloc = z3fold_zpool_malloc, |
| 1613 | .free = z3fold_zpool_free, |
| 1614 | .shrink = z3fold_zpool_shrink, |
| 1615 | .map = z3fold_zpool_map, |
| 1616 | .unmap = z3fold_zpool_unmap, |
| 1617 | .total_size = z3fold_zpool_total_size, |
| 1618 | }; |
| 1619 | |
| 1620 | MODULE_ALIAS("zpool-z3fold"); |
| 1621 | |
| 1622 | static int __init init_z3fold(void) |
| 1623 | { |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1624 | int ret; |
| 1625 | |
Vitaly Wool | ede9321 | 2017-02-24 14:57:17 -0800 | [diff] [blame] | 1626 | /* Make sure the z3fold header is not larger than the page size */ |
| 1627 | BUILD_BUG_ON(ZHDR_SIZE_ALIGNED > PAGE_SIZE); |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1628 | ret = z3fold_mount(); |
| 1629 | if (ret) |
| 1630 | return ret; |
| 1631 | |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1632 | zpool_register_driver(&z3fold_zpool_driver); |
| 1633 | |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | static void __exit exit_z3fold(void) |
| 1638 | { |
Vitaly Wool | 1f86298 | 2019-05-13 17:22:52 -0700 | [diff] [blame] | 1639 | z3fold_unmount(); |
Vitaly Wool | 9a001fc | 2016-05-20 16:58:30 -0700 | [diff] [blame] | 1640 | zpool_unregister_driver(&z3fold_zpool_driver); |
| 1641 | } |
| 1642 | |
| 1643 | module_init(init_z3fold); |
| 1644 | module_exit(exit_z3fold); |
| 1645 | |
| 1646 | MODULE_LICENSE("GPL"); |
| 1647 | MODULE_AUTHOR("Vitaly Wool <vitalywool@gmail.com>"); |
| 1648 | MODULE_DESCRIPTION("3-Fold Allocator for Compressed Pages"); |