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