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