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