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