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