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