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