blob: 702bc3fd687abeb44a0c193489ada527f1a1c7f0 [file] [log] [blame]
Nitin Gupta61989a82012-01-09 16:51:56 -06001/*
2 * zsmalloc memory allocator
3 *
4 * Copyright (C) 2011 Nitin Gupta
Minchan Kim31fc00b2014-01-30 15:45:55 -08005 * Copyright (C) 2012, 2013 Minchan Kim
Nitin Gupta61989a82012-01-09 16:51:56 -06006 *
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
Nitin Gupta2db51da2012-06-09 17:41:14 -070014/*
Nitin Gupta2db51da2012-06-09 17:41:14 -070015 * 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:
Minchan Kim37836892016-07-26 15:23:23 -070019 * page->private: points to zspage
Matthew Wilcox (Oracle)ffedd092021-10-04 14:46:47 +010020 * page->index: links together all component pages of a zspage
Minchan Kim48b48002016-07-26 15:23:31 -070021 * For the huge page, this is always 0, so we use this field
22 * to store handle.
Matthew Wilcox (Oracle)ffedd092021-10-04 14:46:47 +010023 * page->page_type: first object offset in a subpage of zspage
Nitin Gupta2db51da2012-06-09 17:41:14 -070024 *
25 * Usage of struct page flags:
26 * PG_private: identifies the first component page
Xishi Qiu399d8ee2017-02-22 15:45:01 -080027 * PG_owner_priv_1: identifies the huge component page
Nitin Gupta2db51da2012-06-09 17:41:14 -070028 *
29 */
30
Dan Streetman4abaac92016-05-26 15:16:27 -070031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Minchan Kimb475d422022-01-21 22:14:13 -080033/*
34 * lock ordering:
35 * page_lock
Nhat Phamc0547d02022-11-28 11:16:12 -080036 * pool->lock
Minchan Kimb475d422022-01-21 22:14:13 -080037 * zspage->lock
38 */
39
Nitin Gupta61989a82012-01-09 16:51:56 -060040#include <linux/module.h>
41#include <linux/kernel.h>
Minchan Kim312fcae2015-04-15 16:15:30 -070042#include <linux/sched.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060043#include <linux/bitops.h>
44#include <linux/errno.h>
45#include <linux/highmem.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060046#include <linux/string.h>
47#include <linux/slab.h>
Mike Rapoportca5999f2020-06-08 21:32:38 -070048#include <linux/pgtable.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -070049#include <asm/tlbflush.h>
Nitin Gupta61989a82012-01-09 16:51:56 -060050#include <linux/cpumask.h>
51#include <linux/cpu.h>
Seth Jennings0cbb6132012-02-13 08:47:49 -060052#include <linux/vmalloc.h>
Sergey Senozhatsky759b26b2015-11-06 16:29:29 -080053#include <linux/preempt.h>
Seth Jennings0959c632012-08-08 15:12:17 +090054#include <linux/spinlock.h>
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -080055#include <linux/shrinker.h>
Seth Jennings0959c632012-08-08 15:12:17 +090056#include <linux/types.h>
Ganesh Mahendran0f050d92015-02-12 15:00:54 -080057#include <linux/debugfs.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080058#include <linux/zsmalloc.h>
Dan Streetmanc7957792014-08-06 16:08:38 -070059#include <linux/zpool.h>
Minchan Kimdd4123f2016-07-26 15:26:50 -070060#include <linux/migrate.h>
Henry Burns701d6782019-08-24 17:55:06 -070061#include <linux/wait.h>
Minchan Kim48b48002016-07-26 15:23:31 -070062#include <linux/pagemap.h>
Sergey Senozhatskycdc346b2018-01-04 16:18:02 -080063#include <linux/fs.h>
Mike Galbraitha3726592022-01-21 22:14:17 -080064#include <linux/local_lock.h>
Minchan Kim48b48002016-07-26 15:23:31 -070065
66#define ZSPAGE_MAGIC 0x58
Seth Jennings0959c632012-08-08 15:12:17 +090067
68/*
Shijie Luocb152a12021-05-06 18:05:51 -070069 * This must be power of 2 and greater than or equal to sizeof(link_free).
Seth Jennings0959c632012-08-08 15:12:17 +090070 * 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
Minchan Kim2e40e162015-04-15 16:15:23 -070083#define ZS_HANDLE_SIZE (sizeof(unsigned long))
84
Seth Jennings0959c632012-08-08 15:12:17 +090085/*
86 * Object location (<PFN>, <obj_idx>) is encoded as
Randy Dunlapb956b5a2020-08-11 18:33:31 -070087 * a single (unsigned long) handle value.
Seth Jennings0959c632012-08-08 15:12:17 +090088 *
Minchan Kimbfd093f2016-07-26 15:23:28 -070089 * Note that object index <obj_idx> starts from 0.
Seth Jennings0959c632012-08-08 15:12:17 +090090 *
91 * This is made more complicated by various memory models and PAE.
92 */
93
Kirill A. Shutemov02390b82018-02-14 14:16:49 +030094#ifndef MAX_POSSIBLE_PHYSMEM_BITS
95#ifdef MAX_PHYSMEM_BITS
96#define MAX_POSSIBLE_PHYSMEM_BITS MAX_PHYSMEM_BITS
97#else
Seth Jennings0959c632012-08-08 15:12:17 +090098/*
99 * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
100 * be PAGE_SHIFT
101 */
Kirill A. Shutemov02390b82018-02-14 14:16:49 +0300102#define MAX_POSSIBLE_PHYSMEM_BITS BITS_PER_LONG
Seth Jennings0959c632012-08-08 15:12:17 +0900103#endif
104#endif
Kirill A. Shutemov02390b82018-02-14 14:16:49 +0300105
106#define _PFN_BITS (MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT)
Minchan Kim312fcae2015-04-15 16:15:30 -0700107
108/*
Minchan Kim312fcae2015-04-15 16:15:30 -0700109 * 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
Nhat Pham85b32582023-01-10 15:17:01 -0800116
117#ifdef CONFIG_ZPOOL
118/*
119 * The second least-significant bit in the object's header identifies if the
120 * value stored at the header is a deferred handle from the last reclaim
121 * attempt.
122 *
123 * As noted above, this is valid because we have room for two bits.
124 */
125#define OBJ_DEFERRED_HANDLE_TAG 2
126#define OBJ_TAG_BITS 2
127#define OBJ_TAG_MASK (OBJ_ALLOCATED_TAG | OBJ_DEFERRED_HANDLE_TAG)
128#else
129#define OBJ_TAG_BITS 1
130#define OBJ_TAG_MASK OBJ_ALLOCATED_TAG
131#endif /* CONFIG_ZPOOL */
132
Minchan Kim312fcae2015-04-15 16:15:30 -0700133#define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
Seth Jennings0959c632012-08-08 15:12:17 +0900134#define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
135
Minchan Kima41ec882022-01-21 22:14:04 -0800136#define HUGE_BITS 1
Jerome Marchandcf8e0fed2017-07-10 15:50:18 -0700137#define FULLNESS_BITS 2
138#define CLASS_BITS 8
139#define ISOLATED_BITS 3
140#define MAGIC_VAL_BITS 8
141
Seth Jennings0959c632012-08-08 15:12:17 +0900142#define MAX(a, b) ((a) >= (b) ? (a) : (b))
143/* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
144#define ZS_MIN_ALLOC_SIZE \
145 MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
Minchan Kim2e40e162015-04-15 16:15:23 -0700146/* each chunk includes extra space to keep handle */
Minchan Kim7b60a682015-04-15 16:15:39 -0700147#define ZS_MAX_ALLOC_SIZE PAGE_SIZE
Seth Jennings0959c632012-08-08 15:12:17 +0900148
149/*
Weijie Yang7eb52512014-06-04 16:11:08 -0700150 * On systems with 4K page size, this gives 255 size classes! There is a
Seth Jennings0959c632012-08-08 15:12:17 +0900151 * trader-off here:
152 * - Large number of size classes is potentially wasteful as free page are
153 * spread across these classes
154 * - Small number of size classes causes large internal fragmentation
155 * - Probably its better to use specific size classes (empirically
156 * determined). NOTE: all those class sizes must be set as multiple of
157 * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
158 *
159 * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
160 * (reason above)
161 */
Minchan Kim37836892016-07-26 15:23:23 -0700162#define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
Jerome Marchandcf8e0fed2017-07-10 15:50:18 -0700163#define ZS_SIZE_CLASSES (DIV_ROUND_UP(ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE, \
164 ZS_SIZE_CLASS_DELTA) + 1)
Seth Jennings0959c632012-08-08 15:12:17 +0900165
Seth Jennings0959c632012-08-08 15:12:17 +0900166enum fullness_group {
Seth Jennings0959c632012-08-08 15:12:17 +0900167 ZS_EMPTY,
Minchan Kim48b48002016-07-26 15:23:31 -0700168 ZS_ALMOST_EMPTY,
169 ZS_ALMOST_FULL,
170 ZS_FULL,
171 NR_ZS_FULLNESS,
Seth Jennings0959c632012-08-08 15:12:17 +0900172};
173
Minchan Kim3828a762022-01-21 22:13:54 -0800174enum class_stat_type {
Minchan Kim48b48002016-07-26 15:23:31 -0700175 CLASS_EMPTY,
176 CLASS_ALMOST_EMPTY,
177 CLASS_ALMOST_FULL,
178 CLASS_FULL,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800179 OBJ_ALLOCATED,
180 OBJ_USED,
Minchan Kim48b48002016-07-26 15:23:31 -0700181 NR_ZS_STAT_TYPE,
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800182};
183
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800184struct zs_size_stat {
185 unsigned long objs[NR_ZS_STAT_TYPE];
186};
187
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700188#ifdef CONFIG_ZSMALLOC_STAT
189static struct dentry *zs_stat_root;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800190#endif
191
Seth Jennings0959c632012-08-08 15:12:17 +0900192/*
193 * We assign a page to ZS_ALMOST_EMPTY fullness group when:
194 * n <= N / f, where
195 * n = number of allocated objects
196 * N = total number of objects zspage can store
Wang Sheng-Hui6dd97372014-10-09 15:29:59 -0700197 * f = fullness_threshold_frac
Seth Jennings0959c632012-08-08 15:12:17 +0900198 *
199 * Similarly, we assign zspage to:
200 * ZS_ALMOST_FULL when n > N / f
201 * ZS_EMPTY when n == 0
202 * ZS_FULL when n == N
203 *
204 * (see: fix_fullness_group())
205 */
206static const int fullness_threshold_frac = 4;
Sergey Senozhatsky010b4952018-04-05 16:24:43 -0700207static size_t huge_class_size;
Seth Jennings0959c632012-08-08 15:12:17 +0900208
209struct size_class {
Minchan Kim48b48002016-07-26 15:23:31 -0700210 struct list_head fullness_list[NR_ZS_FULLNESS];
Seth Jennings0959c632012-08-08 15:12:17 +0900211 /*
212 * Size of objects stored in this class. Must be multiple
213 * of ZS_ALIGN.
214 */
215 int size;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700216 int objs_per_zspage;
Weijie Yang7dfa4612016-01-14 15:22:40 -0800217 /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
218 int pages_per_zspage;
Minchan Kim48b48002016-07-26 15:23:31 -0700219
220 unsigned int index;
221 struct zs_size_stat stats;
Seth Jennings0959c632012-08-08 15:12:17 +0900222};
223
224/*
225 * Placed within free objects to form a singly linked list.
Minchan Kim37836892016-07-26 15:23:23 -0700226 * For every zspage, zspage->freeobj gives head of this list.
Seth Jennings0959c632012-08-08 15:12:17 +0900227 *
228 * This must be power of 2 and less than or equal to ZS_ALIGN
229 */
230struct link_free {
Minchan Kim2e40e162015-04-15 16:15:23 -0700231 union {
232 /*
Minchan Kimbfd093f2016-07-26 15:23:28 -0700233 * Free object index;
Minchan Kim2e40e162015-04-15 16:15:23 -0700234 * It's valid for non-allocated object
235 */
Minchan Kimbfd093f2016-07-26 15:23:28 -0700236 unsigned long next;
Minchan Kim2e40e162015-04-15 16:15:23 -0700237 /*
238 * Handle of allocated object.
239 */
240 unsigned long handle;
Nhat Pham85b32582023-01-10 15:17:01 -0800241#ifdef CONFIG_ZPOOL
242 /*
243 * Deferred handle of a reclaimed object.
244 */
245 unsigned long deferred_handle;
246#endif
Minchan Kim2e40e162015-04-15 16:15:23 -0700247 };
Seth Jennings0959c632012-08-08 15:12:17 +0900248};
249
250struct zs_pool {
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800251 const char *name;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800252
Jerome Marchandcf8e0fed2017-07-10 15:50:18 -0700253 struct size_class *size_class[ZS_SIZE_CLASSES];
Minchan Kim2e40e162015-04-15 16:15:23 -0700254 struct kmem_cache *handle_cachep;
Minchan Kim37836892016-07-26 15:23:23 -0700255 struct kmem_cache *zspage_cachep;
Seth Jennings0959c632012-08-08 15:12:17 +0900256
Minchan Kim13de8932014-10-09 15:29:48 -0700257 atomic_long_t pages_allocated;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800258
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -0700259 struct zs_pool_stats stats;
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -0700260
261 /* Compact classes */
262 struct shrinker shrinker;
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -0800263
Nhat Pham64f768c2022-11-28 11:16:13 -0800264#ifdef CONFIG_ZPOOL
265 /* List tracking the zspages in LRU order by most recently added object */
266 struct list_head lru;
Nhat Phambd0fded2022-11-28 11:16:14 -0800267 struct zpool *zpool;
268 const struct zpool_ops *zpool_ops;
Nhat Pham64f768c2022-11-28 11:16:13 -0800269#endif
270
Ganesh Mahendran0f050d92015-02-12 15:00:54 -0800271#ifdef CONFIG_ZSMALLOC_STAT
272 struct dentry *stat_dentry;
273#endif
Minchan Kim48b48002016-07-26 15:23:31 -0700274#ifdef CONFIG_COMPACTION
Minchan Kim48b48002016-07-26 15:23:31 -0700275 struct work_struct free_work;
276#endif
Nhat Phamc0547d02022-11-28 11:16:12 -0800277 spinlock_t lock;
Seth Jennings0959c632012-08-08 15:12:17 +0900278};
Nitin Gupta61989a82012-01-09 16:51:56 -0600279
Minchan Kim37836892016-07-26 15:23:23 -0700280struct zspage {
281 struct {
Minchan Kima41ec882022-01-21 22:14:04 -0800282 unsigned int huge:HUGE_BITS;
Minchan Kim37836892016-07-26 15:23:23 -0700283 unsigned int fullness:FULLNESS_BITS;
Minchan Kim85d492f2017-04-13 14:56:40 -0700284 unsigned int class:CLASS_BITS + 1;
Minchan Kim48b48002016-07-26 15:23:31 -0700285 unsigned int isolated:ISOLATED_BITS;
286 unsigned int magic:MAGIC_VAL_BITS;
Minchan Kim37836892016-07-26 15:23:23 -0700287 };
288 unsigned int inuse;
Minchan Kimbfd093f2016-07-26 15:23:28 -0700289 unsigned int freeobj;
Minchan Kim37836892016-07-26 15:23:23 -0700290 struct page *first_page;
291 struct list_head list; /* fullness list */
Nhat Pham64f768c2022-11-28 11:16:13 -0800292
293#ifdef CONFIG_ZPOOL
294 /* links the zspage to the lru list in the pool */
295 struct list_head lru;
Nhat Pham9997bc02022-11-28 11:16:15 -0800296 bool under_reclaim;
Nhat Pham64f768c2022-11-28 11:16:13 -0800297#endif
298
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -0400299 struct zs_pool *pool;
Minchan Kim48b48002016-07-26 15:23:31 -0700300 rwlock_t lock;
Minchan Kim37836892016-07-26 15:23:23 -0700301};
Nitin Gupta61989a82012-01-09 16:51:56 -0600302
Seth Jenningsf5536462012-07-18 11:55:56 -0500303struct mapping_area {
Mike Galbraitha3726592022-01-21 22:14:17 -0800304 local_lock_t lock;
Seth Jenningsf5536462012-07-18 11:55:56 -0500305 char *vm_buf; /* copy buffer for objects that span pages */
Seth Jenningsf5536462012-07-18 11:55:56 -0500306 char *vm_addr; /* address of kmap_atomic()'ed pages */
307 enum zs_mapmode vm_mm; /* mapping mode */
308};
309
Minchan Kima41ec882022-01-21 22:14:04 -0800310/* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
311static void SetZsHugePage(struct zspage *zspage)
312{
313 zspage->huge = 1;
314}
315
316static bool ZsHugePage(struct zspage *zspage)
317{
318 return zspage->huge;
319}
320
Minchan Kim48b48002016-07-26 15:23:31 -0700321static void migrate_lock_init(struct zspage *zspage);
322static void migrate_read_lock(struct zspage *zspage);
323static void migrate_read_unlock(struct zspage *zspage);
Nhat Pham9997bc02022-11-28 11:16:15 -0800324
325#ifdef CONFIG_COMPACTION
Minchan Kimb475d422022-01-21 22:14:13 -0800326static void migrate_write_lock(struct zspage *zspage);
327static void migrate_write_lock_nested(struct zspage *zspage);
328static void migrate_write_unlock(struct zspage *zspage);
Minchan Kim48b48002016-07-26 15:23:31 -0700329static void kick_deferred_free(struct zs_pool *pool);
330static void init_deferred_free(struct zs_pool *pool);
331static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
332#else
Minchan Kimb475d422022-01-21 22:14:13 -0800333static void migrate_write_lock(struct zspage *zspage) {}
334static void migrate_write_lock_nested(struct zspage *zspage) {}
335static void migrate_write_unlock(struct zspage *zspage) {}
Minchan Kim48b48002016-07-26 15:23:31 -0700336static void kick_deferred_free(struct zs_pool *pool) {}
337static void init_deferred_free(struct zs_pool *pool) {}
338static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
339#endif
340
Minchan Kim37836892016-07-26 15:23:23 -0700341static int create_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700342{
343 pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
344 0, 0, NULL);
Minchan Kim37836892016-07-26 15:23:23 -0700345 if (!pool->handle_cachep)
346 return 1;
347
348 pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
349 0, 0, NULL);
350 if (!pool->zspage_cachep) {
351 kmem_cache_destroy(pool->handle_cachep);
352 pool->handle_cachep = NULL;
353 return 1;
354 }
355
356 return 0;
Minchan Kim2e40e162015-04-15 16:15:23 -0700357}
358
Minchan Kim37836892016-07-26 15:23:23 -0700359static void destroy_cache(struct zs_pool *pool)
Minchan Kim2e40e162015-04-15 16:15:23 -0700360{
Sergey Senozhatskycd10add2015-09-08 15:04:55 -0700361 kmem_cache_destroy(pool->handle_cachep);
Minchan Kim37836892016-07-26 15:23:23 -0700362 kmem_cache_destroy(pool->zspage_cachep);
Minchan Kim2e40e162015-04-15 16:15:23 -0700363}
364
Minchan Kim37836892016-07-26 15:23:23 -0700365static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
Minchan Kim2e40e162015-04-15 16:15:23 -0700366{
367 return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
Minchan Kim48b48002016-07-26 15:23:31 -0700368 gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Minchan Kim2e40e162015-04-15 16:15:23 -0700369}
370
Minchan Kim37836892016-07-26 15:23:23 -0700371static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
Minchan Kim2e40e162015-04-15 16:15:23 -0700372{
373 kmem_cache_free(pool->handle_cachep, (void *)handle);
374}
375
Minchan Kim37836892016-07-26 15:23:23 -0700376static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
377{
Miaohe Linf0231302021-02-25 17:18:27 -0800378 return kmem_cache_zalloc(pool->zspage_cachep,
Minchan Kim48b48002016-07-26 15:23:31 -0700379 flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
Xishi Qiu399d8ee2017-02-22 15:45:01 -0800380}
Minchan Kim37836892016-07-26 15:23:23 -0700381
382static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
383{
384 kmem_cache_free(pool->zspage_cachep, zspage);
385}
386
Nhat Phamc0547d02022-11-28 11:16:12 -0800387/* pool->lock(which owns the handle) synchronizes races */
Minchan Kim2e40e162015-04-15 16:15:23 -0700388static void record_obj(unsigned long handle, unsigned long obj)
389{
Minchan Kimb475d422022-01-21 22:14:13 -0800390 *(unsigned long *)handle = obj;
Minchan Kim2e40e162015-04-15 16:15:23 -0700391}
392
Dan Streetmanc7957792014-08-06 16:08:38 -0700393/* zpool driver */
394
395#ifdef CONFIG_ZPOOL
396
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -0800397static void *zs_zpool_create(const char *name, gfp_t gfp,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -0700398 const struct zpool_ops *zpool_ops,
Dan Streetman479305f2015-06-25 15:00:40 -0700399 struct zpool *zpool)
Dan Streetmanc7957792014-08-06 16:08:38 -0700400{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700401 /*
402 * Ignore global gfp flags: zs_malloc() may be invoked from
403 * different contexts and its caller must provide a valid
404 * gfp mask.
405 */
Nhat Phambd0fded2022-11-28 11:16:14 -0800406 struct zs_pool *pool = zs_create_pool(name);
407
408 if (pool) {
409 pool->zpool = zpool;
410 pool->zpool_ops = zpool_ops;
411 }
412
413 return pool;
Dan Streetmanc7957792014-08-06 16:08:38 -0700414}
415
416static void zs_zpool_destroy(void *pool)
417{
418 zs_destroy_pool(pool);
419}
420
421static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
422 unsigned long *handle)
423{
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -0700424 *handle = zs_malloc(pool, size, gfp);
Hui Zhuc7e6f172022-07-14 16:07:57 +0800425
Deming Wang65917b532022-11-03 22:38:18 -0400426 if (IS_ERR_VALUE(*handle))
Hui Zhuc7e6f172022-07-14 16:07:57 +0800427 return PTR_ERR((void *)*handle);
428 return 0;
Dan Streetmanc7957792014-08-06 16:08:38 -0700429}
430static void zs_zpool_free(void *pool, unsigned long handle)
431{
432 zs_free(pool, handle);
433}
434
Nhat Pham9997bc02022-11-28 11:16:15 -0800435static int zs_reclaim_page(struct zs_pool *pool, unsigned int retries);
436
437static int zs_zpool_shrink(void *pool, unsigned int pages,
438 unsigned int *reclaimed)
439{
440 unsigned int total = 0;
441 int ret = -EINVAL;
442
443 while (total < pages) {
444 ret = zs_reclaim_page(pool, 8);
445 if (ret < 0)
446 break;
447 total++;
448 }
449
450 if (reclaimed)
451 *reclaimed = total;
452
453 return ret;
454}
455
Dan Streetmanc7957792014-08-06 16:08:38 -0700456static void *zs_zpool_map(void *pool, unsigned long handle,
457 enum zpool_mapmode mm)
458{
459 enum zs_mapmode zs_mm;
460
461 switch (mm) {
462 case ZPOOL_MM_RO:
463 zs_mm = ZS_MM_RO;
464 break;
465 case ZPOOL_MM_WO:
466 zs_mm = ZS_MM_WO;
467 break;
Joe Perchese4a9bc52020-04-06 20:08:39 -0700468 case ZPOOL_MM_RW:
Dan Streetmanc7957792014-08-06 16:08:38 -0700469 default:
470 zs_mm = ZS_MM_RW;
471 break;
472 }
473
474 return zs_map_object(pool, handle, zs_mm);
475}
476static void zs_zpool_unmap(void *pool, unsigned long handle)
477{
478 zs_unmap_object(pool, handle);
479}
480
481static u64 zs_zpool_total_size(void *pool)
482{
Minchan Kim722cdc12014-10-09 15:29:50 -0700483 return zs_get_total_pages(pool) << PAGE_SHIFT;
Dan Streetmanc7957792014-08-06 16:08:38 -0700484}
485
486static struct zpool_driver zs_zpool_driver = {
Hui Zhuc165f252019-09-23 15:39:37 -0700487 .type = "zsmalloc",
488 .owner = THIS_MODULE,
489 .create = zs_zpool_create,
490 .destroy = zs_zpool_destroy,
491 .malloc_support_movable = true,
492 .malloc = zs_zpool_malloc,
493 .free = zs_zpool_free,
Nhat Pham9997bc02022-11-28 11:16:15 -0800494 .shrink = zs_zpool_shrink,
Hui Zhuc165f252019-09-23 15:39:37 -0700495 .map = zs_zpool_map,
496 .unmap = zs_zpool_unmap,
497 .total_size = zs_zpool_total_size,
Dan Streetmanc7957792014-08-06 16:08:38 -0700498};
499
Kees Cook137f8cf2014-08-29 15:18:40 -0700500MODULE_ALIAS("zpool-zsmalloc");
Dan Streetmanc7957792014-08-06 16:08:38 -0700501#endif /* CONFIG_ZPOOL */
502
Nitin Gupta61989a82012-01-09 16:51:56 -0600503/* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
Mike Galbraitha3726592022-01-21 22:14:17 -0800504static DEFINE_PER_CPU(struct mapping_area, zs_map_area) = {
505 .lock = INIT_LOCAL_LOCK(lock),
506};
Nitin Gupta61989a82012-01-09 16:51:56 -0600507
Nick Desaulniers3457f412017-07-10 15:47:26 -0700508static __maybe_unused int is_first_page(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600509{
Minchan Kima27545bf2012-04-25 15:23:09 +0900510 return PagePrivate(page);
Nitin Gupta61989a82012-01-09 16:51:56 -0600511}
512
Nhat Phamc0547d02022-11-28 11:16:12 -0800513/* Protected by pool->lock */
Minchan Kim37836892016-07-26 15:23:23 -0700514static inline int get_zspage_inuse(struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600515{
Minchan Kim37836892016-07-26 15:23:23 -0700516 return zspage->inuse;
Nitin Gupta61989a82012-01-09 16:51:56 -0600517}
518
Minchan Kim4f420472016-07-26 15:23:17 -0700519
Minchan Kim37836892016-07-26 15:23:23 -0700520static inline void mod_zspage_inuse(struct zspage *zspage, int val)
Minchan Kim4f420472016-07-26 15:23:17 -0700521{
Minchan Kim37836892016-07-26 15:23:23 -0700522 zspage->inuse += val;
Minchan Kim4f420472016-07-26 15:23:17 -0700523}
524
Minchan Kim48b48002016-07-26 15:23:31 -0700525static inline struct page *get_first_page(struct zspage *zspage)
526{
527 struct page *first_page = zspage->first_page;
528
529 VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
530 return first_page;
531}
532
Alexey Romanov671f2fa2022-09-09 11:37:22 +0300533static inline unsigned int get_first_obj_offset(struct page *page)
Minchan Kim4f420472016-07-26 15:23:17 -0700534{
Matthew Wilcox (Oracle)ffedd092021-10-04 14:46:47 +0100535 return page->page_type;
Minchan Kim4f420472016-07-26 15:23:17 -0700536}
537
Alexey Romanov671f2fa2022-09-09 11:37:22 +0300538static inline void set_first_obj_offset(struct page *page, unsigned int offset)
Minchan Kim4f420472016-07-26 15:23:17 -0700539{
Matthew Wilcox (Oracle)ffedd092021-10-04 14:46:47 +0100540 page->page_type = offset;
Minchan Kim4f420472016-07-26 15:23:17 -0700541}
542
Minchan Kimbfd093f2016-07-26 15:23:28 -0700543static inline unsigned int get_freeobj(struct zspage *zspage)
Minchan Kim4f420472016-07-26 15:23:17 -0700544{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700545 return zspage->freeobj;
Minchan Kim4f420472016-07-26 15:23:17 -0700546}
547
Minchan Kimbfd093f2016-07-26 15:23:28 -0700548static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
Minchan Kim4f420472016-07-26 15:23:17 -0700549{
Minchan Kimbfd093f2016-07-26 15:23:28 -0700550 zspage->freeobj = obj;
Minchan Kim4f420472016-07-26 15:23:17 -0700551}
552
Minchan Kim37836892016-07-26 15:23:23 -0700553static void get_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700554 unsigned int *class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600555 enum fullness_group *fullness)
556{
Minchan Kim48b48002016-07-26 15:23:31 -0700557 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
558
Minchan Kim37836892016-07-26 15:23:23 -0700559 *fullness = zspage->fullness;
560 *class_idx = zspage->class;
Nitin Gupta61989a82012-01-09 16:51:56 -0600561}
562
Minchan Kim67f1c9cd2022-01-21 22:13:51 -0800563static struct size_class *zspage_class(struct zs_pool *pool,
564 struct zspage *zspage)
565{
566 return pool->size_class[zspage->class];
567}
568
Minchan Kim37836892016-07-26 15:23:23 -0700569static void set_zspage_mapping(struct zspage *zspage,
Minchan Kima4209462016-05-20 16:59:36 -0700570 unsigned int class_idx,
Nitin Gupta61989a82012-01-09 16:51:56 -0600571 enum fullness_group fullness)
572{
Minchan Kim37836892016-07-26 15:23:23 -0700573 zspage->class = class_idx;
574 zspage->fullness = fullness;
Nitin Gupta61989a82012-01-09 16:51:56 -0600575}
576
Nitin Cuptac3e3e88a2013-12-11 11:04:37 +0900577/*
578 * zsmalloc divides the pool into various size classes where each
579 * class maintains a list of zspages where each zspage is divided
580 * into equal sized chunks. Each allocation falls into one of these
581 * classes depending on its size. This function returns index of the
Shijie Luocb152a12021-05-06 18:05:51 -0700582 * size class which has chunk size big enough to hold the given size.
Nitin Cuptac3e3e88a2013-12-11 11:04:37 +0900583 */
Nitin Gupta61989a82012-01-09 16:51:56 -0600584static int get_size_class_index(int size)
585{
586 int idx = 0;
587
588 if (likely(size > ZS_MIN_ALLOC_SIZE))
589 idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
590 ZS_SIZE_CLASS_DELTA);
591
Jerome Marchandcf8e0fed2017-07-10 15:50:18 -0700592 return min_t(int, ZS_SIZE_CLASSES - 1, idx);
Nitin Gupta61989a82012-01-09 16:51:56 -0600593}
594
Minchan Kim3828a762022-01-21 22:13:54 -0800595/* type can be of enum type class_stat_type or fullness_group */
596static inline void class_stat_inc(struct size_class *class,
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700597 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700598{
Minchan Kim48b48002016-07-26 15:23:31 -0700599 class->stats.objs[type] += cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700600}
601
Minchan Kim3828a762022-01-21 22:13:54 -0800602/* type can be of enum type class_stat_type or fullness_group */
603static inline void class_stat_dec(struct size_class *class,
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700604 int type, unsigned long cnt)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700605{
Minchan Kim48b48002016-07-26 15:23:31 -0700606 class->stats.objs[type] -= cnt;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700607}
608
Minchan Kim3828a762022-01-21 22:13:54 -0800609/* type can be of enum type class_stat_type or fullness_group */
Minchan Kim248ca1b2015-04-15 16:15:42 -0700610static inline unsigned long zs_stat_get(struct size_class *class,
Matthias Kaehlcke3eb95fe2017-09-08 16:13:02 -0700611 int type)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700612{
Minchan Kim48b48002016-07-26 15:23:31 -0700613 return class->stats.objs[type];
Minchan Kim248ca1b2015-04-15 16:15:42 -0700614}
615
Sergey Senozhatsky57244592015-09-08 15:04:27 -0700616#ifdef CONFIG_ZSMALLOC_STAT
617
Dan Streetman4abaac92016-05-26 15:16:27 -0700618static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700619{
Dan Streetman4abaac92016-05-26 15:16:27 -0700620 if (!debugfs_initialized()) {
621 pr_warn("debugfs not available, stat dir not created\n");
622 return;
623 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700624
625 zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700626}
627
628static void __exit zs_stat_exit(void)
629{
630 debugfs_remove_recursive(zs_stat_root);
631}
632
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700633static unsigned long zs_can_compact(struct size_class *class);
634
Minchan Kim248ca1b2015-04-15 16:15:42 -0700635static int zs_stats_size_show(struct seq_file *s, void *v)
636{
637 int i;
638 struct zs_pool *pool = s->private;
639 struct size_class *class;
640 int objs_per_zspage;
641 unsigned long class_almost_full, class_almost_empty;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700642 unsigned long obj_allocated, obj_used, pages_used, freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700643 unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
644 unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700645 unsigned long total_freeable = 0;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700646
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700647 seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700648 "class", "size", "almost_full", "almost_empty",
649 "obj_allocated", "obj_used", "pages_used",
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700650 "pages_per_zspage", "freeable");
Minchan Kim248ca1b2015-04-15 16:15:42 -0700651
Jerome Marchandcf8e0fed2017-07-10 15:50:18 -0700652 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Minchan Kim248ca1b2015-04-15 16:15:42 -0700653 class = pool->size_class[i];
654
655 if (class->index != i)
656 continue;
657
Nhat Phamc0547d02022-11-28 11:16:12 -0800658 spin_lock(&pool->lock);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700659 class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
660 class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
661 obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
662 obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700663 freeable = zs_can_compact(class);
Nhat Phamc0547d02022-11-28 11:16:12 -0800664 spin_unlock(&pool->lock);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700665
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -0700666 objs_per_zspage = class->objs_per_zspage;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700667 pages_used = obj_allocated / objs_per_zspage *
668 class->pages_per_zspage;
669
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700670 seq_printf(s, " %5u %5u %11lu %12lu %13lu"
671 " %10lu %10lu %16d %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700672 i, class->size, class_almost_full, class_almost_empty,
673 obj_allocated, obj_used, pages_used,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700674 class->pages_per_zspage, freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700675
676 total_class_almost_full += class_almost_full;
677 total_class_almost_empty += class_almost_empty;
678 total_objs += obj_allocated;
679 total_used_objs += obj_used;
680 total_pages += pages_used;
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700681 total_freeable += freeable;
Minchan Kim248ca1b2015-04-15 16:15:42 -0700682 }
683
684 seq_puts(s, "\n");
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700685 seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
Minchan Kim248ca1b2015-04-15 16:15:42 -0700686 "Total", "", total_class_almost_full,
687 total_class_almost_empty, total_objs,
Sergey Senozhatsky1120ed52016-03-17 14:20:42 -0700688 total_used_objs, total_pages, "", total_freeable);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700689
690 return 0;
691}
Andy Shevchenko5ad35092018-04-05 16:23:16 -0700692DEFINE_SHOW_ATTRIBUTE(zs_stats_size);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700693
Dan Streetmand34f6152016-05-20 16:59:56 -0700694static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700695{
Dan Streetman4abaac92016-05-26 15:16:27 -0700696 if (!zs_stat_root) {
697 pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
Dan Streetmand34f6152016-05-20 16:59:56 -0700698 return;
Dan Streetman4abaac92016-05-26 15:16:27 -0700699 }
Minchan Kim248ca1b2015-04-15 16:15:42 -0700700
Greg Kroah-Hartman42685092019-01-22 16:21:09 +0100701 pool->stat_dentry = debugfs_create_dir(name, zs_stat_root);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700702
Greg Kroah-Hartman42685092019-01-22 16:21:09 +0100703 debugfs_create_file("classes", S_IFREG | 0444, pool->stat_dentry, pool,
704 &zs_stats_size_fops);
Minchan Kim248ca1b2015-04-15 16:15:42 -0700705}
706
707static void zs_pool_stat_destroy(struct zs_pool *pool)
708{
709 debugfs_remove_recursive(pool->stat_dentry);
710}
711
712#else /* CONFIG_ZSMALLOC_STAT */
Dan Streetman4abaac92016-05-26 15:16:27 -0700713static void __init zs_stat_init(void)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700714{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700715}
716
717static void __exit zs_stat_exit(void)
718{
719}
720
Dan Streetmand34f6152016-05-20 16:59:56 -0700721static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
Minchan Kim248ca1b2015-04-15 16:15:42 -0700722{
Minchan Kim248ca1b2015-04-15 16:15:42 -0700723}
724
725static inline void zs_pool_stat_destroy(struct zs_pool *pool)
726{
727}
Minchan Kim248ca1b2015-04-15 16:15:42 -0700728#endif
729
Minchan Kim48b48002016-07-26 15:23:31 -0700730
Nitin Cuptac3e3e88a2013-12-11 11:04:37 +0900731/*
732 * For each size class, zspages are divided into different groups
733 * depending on how "full" they are. This was done so that we could
734 * easily find empty or nearly empty zspages when we try to shrink
735 * the pool (not yet implemented). This function returns fullness
736 * status of the given page.
737 */
Minchan Kim1fc6e272016-07-26 15:23:11 -0700738static enum fullness_group get_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700739 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600740{
Minchan Kim1fc6e272016-07-26 15:23:11 -0700741 int inuse, objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600742 enum fullness_group fg;
Minchan Kim830e4bc2016-05-20 16:59:39 -0700743
Minchan Kim37836892016-07-26 15:23:23 -0700744 inuse = get_zspage_inuse(zspage);
Minchan Kim1fc6e272016-07-26 15:23:11 -0700745 objs_per_zspage = class->objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600746
747 if (inuse == 0)
748 fg = ZS_EMPTY;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700749 else if (inuse == objs_per_zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600750 fg = ZS_FULL;
Minchan Kim1fc6e272016-07-26 15:23:11 -0700751 else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
Nitin Gupta61989a82012-01-09 16:51:56 -0600752 fg = ZS_ALMOST_EMPTY;
753 else
754 fg = ZS_ALMOST_FULL;
755
756 return fg;
757}
758
Nitin Cuptac3e3e88a2013-12-11 11:04:37 +0900759/*
760 * Each size class maintains various freelists and zspages are assigned
761 * to one of these freelists based on the number of live objects they
762 * have. This functions inserts the given zspage into the freelist
763 * identified by <class, fullness_group>.
764 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700765static void insert_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700766 struct zspage *zspage,
767 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600768{
Minchan Kim37836892016-07-26 15:23:23 -0700769 struct zspage *head;
Nitin Gupta61989a82012-01-09 16:51:56 -0600770
Minchan Kim3828a762022-01-21 22:13:54 -0800771 class_stat_inc(class, fullness, 1);
Minchan Kim37836892016-07-26 15:23:23 -0700772 head = list_first_entry_or_null(&class->fullness_list[fullness],
773 struct zspage, list);
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700774 /*
Minchan Kim37836892016-07-26 15:23:23 -0700775 * We want to see more ZS_FULL pages and less almost empty/full.
776 * Put pages with higher ->inuse first.
Sergey Senozhatsky58f17112015-09-08 15:04:44 -0700777 */
Miaohe Lin110ceb82020-12-14 19:14:22 -0800778 if (head && get_zspage_inuse(zspage) < get_zspage_inuse(head))
779 list_add(&zspage->list, &head->list);
780 else
781 list_add(&zspage->list, &class->fullness_list[fullness]);
Nitin Gupta61989a82012-01-09 16:51:56 -0600782}
783
Nitin Cuptac3e3e88a2013-12-11 11:04:37 +0900784/*
785 * This function removes the given zspage from the freelist identified
786 * by <class, fullness_group>.
787 */
Minchan Kim251cbb92016-05-20 16:59:42 -0700788static void remove_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700789 struct zspage *zspage,
790 enum fullness_group fullness)
Nitin Gupta61989a82012-01-09 16:51:56 -0600791{
Minchan Kim37836892016-07-26 15:23:23 -0700792 VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
Nitin Gupta61989a82012-01-09 16:51:56 -0600793
Minchan Kim37836892016-07-26 15:23:23 -0700794 list_del_init(&zspage->list);
Minchan Kim3828a762022-01-21 22:13:54 -0800795 class_stat_dec(class, fullness, 1);
Nitin Gupta61989a82012-01-09 16:51:56 -0600796}
797
Nitin Cuptac3e3e88a2013-12-11 11:04:37 +0900798/*
799 * Each size class maintains zspages in different fullness groups depending
800 * on the number of live objects they contain. When allocating or freeing
801 * objects, the fullness status of the page can change, say, from ALMOST_FULL
802 * to ALMOST_EMPTY when freeing an object. This function checks if such
803 * a status change has occurred for the given page and accordingly moves the
804 * page from the freelist of the old fullness group to that of the new
805 * fullness group.
806 */
Minchan Kimc7806262015-04-15 16:15:26 -0700807static enum fullness_group fix_fullness_group(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -0700808 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -0600809{
810 int class_idx;
Nitin Gupta61989a82012-01-09 16:51:56 -0600811 enum fullness_group currfg, newfg;
812
Minchan Kim37836892016-07-26 15:23:23 -0700813 get_zspage_mapping(zspage, &class_idx, &currfg);
814 newfg = get_fullness_group(class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -0600815 if (newfg == currfg)
816 goto out;
817
Minchan Kimc4549b82022-01-21 22:14:07 -0800818 remove_zspage(class, zspage, currfg);
819 insert_zspage(class, zspage, newfg);
Minchan Kim37836892016-07-26 15:23:23 -0700820 set_zspage_mapping(zspage, class_idx, newfg);
Nitin Gupta61989a82012-01-09 16:51:56 -0600821out:
822 return newfg;
823}
824
825/*
826 * We have to decide on how many pages to link together
827 * to form a zspage for each size class. This is important
828 * to reduce wastage due to unusable space left at end of
829 * each zspage which is given as:
Yinghao Xie888fa3742015-04-15 16:15:49 -0700830 * wastage = Zp % class_size
831 * usage = Zp - wastage
Nitin Gupta61989a82012-01-09 16:51:56 -0600832 * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
833 *
834 * For example, for size class of 3/8 * PAGE_SIZE, we should
835 * link together 3 PAGE_SIZE sized pages to form a zspage
836 * since then we can perfectly fit in 8 such objects.
837 */
Minchan Kim2e3b61542012-05-03 15:40:39 +0900838static int get_pages_per_zspage(int class_size)
Nitin Gupta61989a82012-01-09 16:51:56 -0600839{
840 int i, max_usedpc = 0;
841 /* zspage order which gives maximum used size per KB */
842 int max_usedpc_order = 1;
843
Seth Jennings84d4faa2012-03-05 11:33:21 -0600844 for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -0600845 int zspage_size;
846 int waste, usedpc;
847
848 zspage_size = i * PAGE_SIZE;
849 waste = zspage_size % class_size;
850 usedpc = (zspage_size - waste) * 100 / zspage_size;
851
852 if (usedpc > max_usedpc) {
853 max_usedpc = usedpc;
854 max_usedpc_order = i;
855 }
856 }
857
858 return max_usedpc_order;
859}
860
Minchan Kim37836892016-07-26 15:23:23 -0700861static struct zspage *get_zspage(struct page *page)
Nitin Gupta61989a82012-01-09 16:51:56 -0600862{
Miaohe Lina6c5e0f2021-02-25 17:18:34 -0800863 struct zspage *zspage = (struct zspage *)page_private(page);
Minchan Kim48b48002016-07-26 15:23:31 -0700864
865 BUG_ON(zspage->magic != ZSPAGE_MAGIC);
866 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -0600867}
868
869static struct page *get_next_page(struct page *page)
870{
Minchan Kima41ec882022-01-21 22:14:04 -0800871 struct zspage *zspage = get_zspage(page);
872
873 if (unlikely(ZsHugePage(zspage)))
Minchan Kim48b48002016-07-26 15:23:31 -0700874 return NULL;
875
Matthew Wilcox (Oracle)ffedd092021-10-04 14:46:47 +0100876 return (struct page *)page->index;
Nitin Gupta61989a82012-01-09 16:51:56 -0600877}
878
Minchan Kimbfd093f2016-07-26 15:23:28 -0700879/**
880 * obj_to_location - get (<page>, <obj_idx>) from encoded object value
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700881 * @obj: the encoded object value
Minchan Kimbfd093f2016-07-26 15:23:28 -0700882 * @page: page object resides in zspage
883 * @obj_idx: object index
Olav Haugan67296872013-11-22 09:30:41 -0800884 */
Minchan Kim312fcae2015-04-15 16:15:30 -0700885static void obj_to_location(unsigned long obj, struct page **page,
Minchan Kimbfd093f2016-07-26 15:23:28 -0700886 unsigned int *obj_idx)
Nitin Gupta61989a82012-01-09 16:51:56 -0600887{
Minchan Kim312fcae2015-04-15 16:15:30 -0700888 obj >>= OBJ_TAG_BITS;
889 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
890 *obj_idx = (obj & OBJ_INDEX_MASK);
Nitin Gupta61989a82012-01-09 16:51:56 -0600891}
892
Minchan Kim67f1c9cd2022-01-21 22:13:51 -0800893static void obj_to_page(unsigned long obj, struct page **page)
894{
895 obj >>= OBJ_TAG_BITS;
896 *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
897}
898
Minchan Kimbfd093f2016-07-26 15:23:28 -0700899/**
900 * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
901 * @page: page object resides in zspage
902 * @obj_idx: object index
903 */
904static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
905{
906 unsigned long obj;
907
908 obj = page_to_pfn(page) << OBJ_INDEX_BITS;
909 obj |= obj_idx & OBJ_INDEX_MASK;
910 obj <<= OBJ_TAG_BITS;
911
912 return obj;
913}
914
Minchan Kim2e40e162015-04-15 16:15:23 -0700915static unsigned long handle_to_obj(unsigned long handle)
916{
917 return *(unsigned long *)handle;
918}
919
Nhat Pham85b32582023-01-10 15:17:01 -0800920static bool obj_tagged(struct page *page, void *obj, unsigned long *phandle,
921 int tag)
Minchan Kim312fcae2015-04-15 16:15:30 -0700922{
Minchan Kim3ae92ac2022-01-21 22:14:01 -0800923 unsigned long handle;
Minchan Kima41ec882022-01-21 22:14:04 -0800924 struct zspage *zspage = get_zspage(page);
Minchan Kim3ae92ac2022-01-21 22:14:01 -0800925
Minchan Kima41ec882022-01-21 22:14:04 -0800926 if (unlikely(ZsHugePage(zspage))) {
Minchan Kim830e4bc2016-05-20 16:59:39 -0700927 VM_BUG_ON_PAGE(!is_first_page(page), page);
Minchan Kim3ae92ac2022-01-21 22:14:01 -0800928 handle = page->index;
Minchan Kim7b60a682015-04-15 16:15:39 -0700929 } else
Minchan Kim3ae92ac2022-01-21 22:14:01 -0800930 handle = *(unsigned long *)obj;
931
Nhat Pham85b32582023-01-10 15:17:01 -0800932 if (!(handle & tag))
Minchan Kim3ae92ac2022-01-21 22:14:01 -0800933 return false;
934
Nhat Pham85b32582023-01-10 15:17:01 -0800935 /* Clear all tags before returning the handle */
936 *phandle = handle & ~OBJ_TAG_MASK;
Minchan Kim3ae92ac2022-01-21 22:14:01 -0800937 return true;
Minchan Kim312fcae2015-04-15 16:15:30 -0700938}
939
Nhat Pham85b32582023-01-10 15:17:01 -0800940static inline bool obj_allocated(struct page *page, void *obj, unsigned long *phandle)
941{
942 return obj_tagged(page, obj, phandle, OBJ_ALLOCATED_TAG);
943}
944
945#ifdef CONFIG_ZPOOL
946static bool obj_stores_deferred_handle(struct page *page, void *obj,
947 unsigned long *phandle)
948{
949 return obj_tagged(page, obj, phandle, OBJ_DEFERRED_HANDLE_TAG);
950}
951#endif
952
Nitin Guptaf4477e92012-04-02 09:13:56 -0500953static void reset_page(struct page *page)
954{
Minchan Kim48b48002016-07-26 15:23:31 -0700955 __ClearPageMovable(page);
Ganesh Mahendran18fd06b2016-07-28 15:48:00 -0700956 ClearPagePrivate(page);
Nitin Guptaf4477e92012-04-02 09:13:56 -0500957 set_page_private(page, 0);
Minchan Kim48b48002016-07-26 15:23:31 -0700958 page_mapcount_reset(page);
Matthew Wilcox (Oracle)ffedd092021-10-04 14:46:47 +0100959 page->index = 0;
Nitin Guptaf4477e92012-04-02 09:13:56 -0500960}
961
Colin Ian King4d0a5402018-08-17 15:46:50 -0700962static int trylock_zspage(struct zspage *zspage)
Minchan Kim48b48002016-07-26 15:23:31 -0700963{
964 struct page *cursor, *fail;
965
966 for (cursor = get_first_page(zspage); cursor != NULL; cursor =
967 get_next_page(cursor)) {
968 if (!trylock_page(cursor)) {
969 fail = cursor;
970 goto unlock;
971 }
972 }
973
974 return 1;
975unlock:
976 for (cursor = get_first_page(zspage); cursor != fail; cursor =
977 get_next_page(cursor))
978 unlock_page(cursor);
979
980 return 0;
981}
982
Nhat Pham9997bc02022-11-28 11:16:15 -0800983#ifdef CONFIG_ZPOOL
Nhat Pham85b32582023-01-10 15:17:01 -0800984static unsigned long find_deferred_handle_obj(struct size_class *class,
985 struct page *page, int *obj_idx);
986
Nhat Pham9997bc02022-11-28 11:16:15 -0800987/*
988 * Free all the deferred handles whose objects are freed in zs_free.
989 */
Nhat Pham85b32582023-01-10 15:17:01 -0800990static void free_handles(struct zs_pool *pool, struct size_class *class,
991 struct zspage *zspage)
Nhat Pham9997bc02022-11-28 11:16:15 -0800992{
Nhat Pham85b32582023-01-10 15:17:01 -0800993 int obj_idx = 0;
994 struct page *page = get_first_page(zspage);
995 unsigned long handle;
Nhat Pham9997bc02022-11-28 11:16:15 -0800996
Nhat Pham85b32582023-01-10 15:17:01 -0800997 while (1) {
998 handle = find_deferred_handle_obj(class, page, &obj_idx);
999 if (!handle) {
1000 page = get_next_page(page);
1001 if (!page)
1002 break;
1003 obj_idx = 0;
1004 continue;
1005 }
Nhat Pham9997bc02022-11-28 11:16:15 -08001006
1007 cache_free_handle(pool, handle);
Nhat Pham85b32582023-01-10 15:17:01 -08001008 obj_idx++;
Nhat Pham9997bc02022-11-28 11:16:15 -08001009 }
1010}
1011#else
Nhat Pham85b32582023-01-10 15:17:01 -08001012static inline void free_handles(struct zs_pool *pool, struct size_class *class,
1013 struct zspage *zspage) {}
Nhat Pham9997bc02022-11-28 11:16:15 -08001014#endif
1015
Minchan Kim48b48002016-07-26 15:23:31 -07001016static void __free_zspage(struct zs_pool *pool, struct size_class *class,
1017 struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001018{
Minchan Kim37836892016-07-26 15:23:23 -07001019 struct page *page, *next;
Minchan Kim48b48002016-07-26 15:23:31 -07001020 enum fullness_group fg;
1021 unsigned int class_idx;
1022
1023 get_zspage_mapping(zspage, &class_idx, &fg);
1024
Nhat Phamc0547d02022-11-28 11:16:12 -08001025 assert_spin_locked(&pool->lock);
Nitin Gupta61989a82012-01-09 16:51:56 -06001026
Minchan Kim37836892016-07-26 15:23:23 -07001027 VM_BUG_ON(get_zspage_inuse(zspage));
Minchan Kim48b48002016-07-26 15:23:31 -07001028 VM_BUG_ON(fg != ZS_EMPTY);
Nitin Gupta61989a82012-01-09 16:51:56 -06001029
Nhat Pham9997bc02022-11-28 11:16:15 -08001030 /* Free all deferred handles from zs_free */
Nhat Pham85b32582023-01-10 15:17:01 -08001031 free_handles(pool, class, zspage);
Nhat Pham9997bc02022-11-28 11:16:15 -08001032
Minchan Kim48b48002016-07-26 15:23:31 -07001033 next = page = get_first_page(zspage);
Minchan Kim37836892016-07-26 15:23:23 -07001034 do {
Minchan Kim48b48002016-07-26 15:23:31 -07001035 VM_BUG_ON_PAGE(!PageLocked(page), page);
1036 next = get_next_page(page);
Minchan Kim37836892016-07-26 15:23:23 -07001037 reset_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001038 unlock_page(page);
Minchan Kim91537fe2016-07-26 15:24:45 -07001039 dec_zone_page_state(page, NR_ZSPAGES);
Minchan Kim37836892016-07-26 15:23:23 -07001040 put_page(page);
1041 page = next;
1042 } while (page != NULL);
Nitin Gupta61989a82012-01-09 16:51:56 -06001043
Minchan Kim37836892016-07-26 15:23:23 -07001044 cache_free_zspage(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001045
Minchan Kim3828a762022-01-21 22:13:54 -08001046 class_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07001047 atomic_long_sub(class->pages_per_zspage,
1048 &pool->pages_allocated);
1049}
1050
1051static void free_zspage(struct zs_pool *pool, struct size_class *class,
1052 struct zspage *zspage)
1053{
1054 VM_BUG_ON(get_zspage_inuse(zspage));
1055 VM_BUG_ON(list_empty(&zspage->list));
1056
Minchan Kimb475d422022-01-21 22:14:13 -08001057 /*
1058 * Since zs_free couldn't be sleepable, this function cannot call
1059 * lock_page. The page locks trylock_zspage got will be released
1060 * by __free_zspage.
1061 */
Minchan Kim48b48002016-07-26 15:23:31 -07001062 if (!trylock_zspage(zspage)) {
1063 kick_deferred_free(pool);
1064 return;
1065 }
1066
1067 remove_zspage(class, zspage, ZS_EMPTY);
Nhat Pham64f768c2022-11-28 11:16:13 -08001068#ifdef CONFIG_ZPOOL
1069 list_del(&zspage->lru);
1070#endif
Minchan Kim48b48002016-07-26 15:23:31 -07001071 __free_zspage(pool, class, zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001072}
1073
1074/* Initialize a newly allocated zspage */
Minchan Kim37836892016-07-26 15:23:23 -07001075static void init_zspage(struct size_class *class, struct zspage *zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001076{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001077 unsigned int freeobj = 1;
Nitin Gupta61989a82012-01-09 16:51:56 -06001078 unsigned long off = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07001079 struct page *page = get_first_page(zspage);
Minchan Kim830e4bc2016-05-20 16:59:39 -07001080
Nitin Gupta61989a82012-01-09 16:51:56 -06001081 while (page) {
1082 struct page *next_page;
1083 struct link_free *link;
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001084 void *vaddr;
Nitin Gupta61989a82012-01-09 16:51:56 -06001085
Minchan Kim37836892016-07-26 15:23:23 -07001086 set_first_obj_offset(page, off);
Nitin Gupta61989a82012-01-09 16:51:56 -06001087
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001088 vaddr = kmap_atomic(page);
1089 link = (struct link_free *)vaddr + off / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001090
Dan Streetman5538c562014-10-09 15:30:01 -07001091 while ((off += class->size) < PAGE_SIZE) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001092 link->next = freeobj++ << OBJ_TAG_BITS;
Dan Streetman5538c562014-10-09 15:30:01 -07001093 link += class->size / sizeof(*link);
Nitin Gupta61989a82012-01-09 16:51:56 -06001094 }
1095
1096 /*
1097 * We now come to the last (full or partial) object on this
1098 * page, which must point to the first object on the next
1099 * page (if present)
1100 */
1101 next_page = get_next_page(page);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001102 if (next_page) {
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001103 link->next = freeobj++ << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001104 } else {
1105 /*
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001106 * Reset OBJ_TAG_BITS bit to last link to tell
Minchan Kimbfd093f2016-07-26 15:23:28 -07001107 * whether it's allocated object or not.
1108 */
Nick Desaulniers01a6ad92018-01-31 16:20:15 -08001109 link->next = -1UL << OBJ_TAG_BITS;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001110 }
Minchan Kimaf4ee5e2014-12-12 16:56:58 -08001111 kunmap_atomic(vaddr);
Nitin Gupta61989a82012-01-09 16:51:56 -06001112 page = next_page;
Dan Streetman5538c562014-10-09 15:30:01 -07001113 off %= PAGE_SIZE;
Nitin Gupta61989a82012-01-09 16:51:56 -06001114 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001115
Nhat Pham64f768c2022-11-28 11:16:13 -08001116#ifdef CONFIG_ZPOOL
1117 INIT_LIST_HEAD(&zspage->lru);
Nhat Pham9997bc02022-11-28 11:16:15 -08001118 zspage->under_reclaim = false;
Nhat Pham64f768c2022-11-28 11:16:13 -08001119#endif
1120
Minchan Kimbfd093f2016-07-26 15:23:28 -07001121 set_freeobj(zspage, 0);
Nitin Gupta61989a82012-01-09 16:51:56 -06001122}
1123
Minchan Kim48b48002016-07-26 15:23:31 -07001124static void create_page_chain(struct size_class *class, struct zspage *zspage,
1125 struct page *pages[])
Nitin Gupta61989a82012-01-09 16:51:56 -06001126{
Minchan Kimbdb0af72016-07-26 15:23:20 -07001127 int i;
1128 struct page *page;
1129 struct page *prev_page = NULL;
Minchan Kim48b48002016-07-26 15:23:31 -07001130 int nr_pages = class->pages_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001131
1132 /*
1133 * Allocate individual pages and link them together as:
Matthew Wilcox (Oracle)ffedd092021-10-04 14:46:47 +01001134 * 1. all pages are linked together using page->index
Minchan Kim37836892016-07-26 15:23:23 -07001135 * 2. each sub-page point to zspage using page->private
Nitin Gupta61989a82012-01-09 16:51:56 -06001136 *
Minchan Kim37836892016-07-26 15:23:23 -07001137 * we set PG_private to identify the first page (i.e. no other sub-page
Yisheng Xie22c5cef2017-02-24 14:59:42 -08001138 * has this flag set).
Nitin Gupta61989a82012-01-09 16:51:56 -06001139 */
Minchan Kimbdb0af72016-07-26 15:23:20 -07001140 for (i = 0; i < nr_pages; i++) {
1141 page = pages[i];
Minchan Kim37836892016-07-26 15:23:23 -07001142 set_page_private(page, (unsigned long)zspage);
Matthew Wilcox (Oracle)ffedd092021-10-04 14:46:47 +01001143 page->index = 0;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001144 if (i == 0) {
Minchan Kim37836892016-07-26 15:23:23 -07001145 zspage->first_page = page;
Minchan Kima27545bf2012-04-25 15:23:09 +09001146 SetPagePrivate(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001147 if (unlikely(class->objs_per_zspage == 1 &&
1148 class->pages_per_zspage == 1))
Minchan Kima41ec882022-01-21 22:14:04 -08001149 SetZsHugePage(zspage);
Minchan Kim37836892016-07-26 15:23:23 -07001150 } else {
Matthew Wilcox (Oracle)ffedd092021-10-04 14:46:47 +01001151 prev_page->index = (unsigned long)page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001152 }
Nitin Gupta61989a82012-01-09 16:51:56 -06001153 prev_page = page;
1154 }
Minchan Kimbdb0af72016-07-26 15:23:20 -07001155}
Nitin Gupta61989a82012-01-09 16:51:56 -06001156
Minchan Kimbdb0af72016-07-26 15:23:20 -07001157/*
1158 * Allocate a zspage for the given size class
1159 */
Minchan Kim37836892016-07-26 15:23:23 -07001160static struct zspage *alloc_zspage(struct zs_pool *pool,
1161 struct size_class *class,
1162 gfp_t gfp)
Minchan Kimbdb0af72016-07-26 15:23:20 -07001163{
1164 int i;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001165 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
Minchan Kim37836892016-07-26 15:23:23 -07001166 struct zspage *zspage = cache_alloc_zspage(pool, gfp);
1167
1168 if (!zspage)
1169 return NULL;
1170
Minchan Kim48b48002016-07-26 15:23:31 -07001171 zspage->magic = ZSPAGE_MAGIC;
1172 migrate_lock_init(zspage);
Nitin Gupta61989a82012-01-09 16:51:56 -06001173
Minchan Kimbdb0af72016-07-26 15:23:20 -07001174 for (i = 0; i < class->pages_per_zspage; i++) {
1175 struct page *page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001176
Minchan Kim37836892016-07-26 15:23:23 -07001177 page = alloc_page(gfp);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001178 if (!page) {
Minchan Kim91537fe2016-07-26 15:24:45 -07001179 while (--i >= 0) {
1180 dec_zone_page_state(pages[i], NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001181 __free_page(pages[i]);
Minchan Kim91537fe2016-07-26 15:24:45 -07001182 }
Minchan Kim37836892016-07-26 15:23:23 -07001183 cache_free_zspage(pool, zspage);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001184 return NULL;
1185 }
Minchan Kim91537fe2016-07-26 15:24:45 -07001186
1187 inc_zone_page_state(page, NR_ZSPAGES);
Minchan Kimbdb0af72016-07-26 15:23:20 -07001188 pages[i] = page;
Nitin Gupta61989a82012-01-09 16:51:56 -06001189 }
1190
Minchan Kim48b48002016-07-26 15:23:31 -07001191 create_page_chain(class, zspage, pages);
Minchan Kim37836892016-07-26 15:23:23 -07001192 init_zspage(class, zspage);
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -04001193 zspage->pool = pool;
Minchan Kimbdb0af72016-07-26 15:23:20 -07001194
Minchan Kim37836892016-07-26 15:23:23 -07001195 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001196}
1197
Minchan Kim37836892016-07-26 15:23:23 -07001198static struct zspage *find_get_zspage(struct size_class *class)
Nitin Gupta61989a82012-01-09 16:51:56 -06001199{
1200 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001201 struct zspage *zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001202
Minchan Kim48b48002016-07-26 15:23:31 -07001203 for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
Minchan Kim37836892016-07-26 15:23:23 -07001204 zspage = list_first_entry_or_null(&class->fullness_list[i],
1205 struct zspage, list);
1206 if (zspage)
Nitin Gupta61989a82012-01-09 16:51:56 -06001207 break;
1208 }
1209
Minchan Kim37836892016-07-26 15:23:23 -07001210 return zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06001211}
1212
Seth Jenningsf5536462012-07-18 11:55:56 -05001213static inline int __zs_cpu_up(struct mapping_area *area)
1214{
1215 /*
1216 * Make sure we don't leak memory if a cpu UP notification
1217 * and zs_init() race and both call zs_cpu_up() on the same cpu
1218 */
1219 if (area->vm_buf)
1220 return 0;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001221 area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
Seth Jenningsf5536462012-07-18 11:55:56 -05001222 if (!area->vm_buf)
1223 return -ENOMEM;
1224 return 0;
1225}
1226
1227static inline void __zs_cpu_down(struct mapping_area *area)
1228{
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08001229 kfree(area->vm_buf);
Seth Jenningsf5536462012-07-18 11:55:56 -05001230 area->vm_buf = NULL;
1231}
1232
1233static void *__zs_map_object(struct mapping_area *area,
1234 struct page *pages[2], int off, int size)
1235{
Seth Jennings5f6019022012-07-02 16:15:49 -05001236 int sizes[2];
1237 void *addr;
Seth Jenningsf5536462012-07-18 11:55:56 -05001238 char *buf = area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001239
Seth Jenningsf5536462012-07-18 11:55:56 -05001240 /* disable page faults to match kmap_atomic() return conditions */
1241 pagefault_disable();
1242
1243 /* no read fastpath */
1244 if (area->vm_mm == ZS_MM_WO)
1245 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -05001246
1247 sizes[0] = PAGE_SIZE - off;
1248 sizes[1] = size - sizes[0];
1249
Seth Jennings5f6019022012-07-02 16:15:49 -05001250 /* copy object to per-cpu buffer */
1251 addr = kmap_atomic(pages[0]);
1252 memcpy(buf, addr + off, sizes[0]);
1253 kunmap_atomic(addr);
1254 addr = kmap_atomic(pages[1]);
1255 memcpy(buf + sizes[0], addr, sizes[1]);
1256 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001257out:
1258 return area->vm_buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001259}
1260
Seth Jenningsf5536462012-07-18 11:55:56 -05001261static void __zs_unmap_object(struct mapping_area *area,
1262 struct page *pages[2], int off, int size)
Seth Jennings5f6019022012-07-02 16:15:49 -05001263{
Seth Jennings5f6019022012-07-02 16:15:49 -05001264 int sizes[2];
1265 void *addr;
Minchan Kim2e40e162015-04-15 16:15:23 -07001266 char *buf;
Seth Jennings5f6019022012-07-02 16:15:49 -05001267
Seth Jenningsf5536462012-07-18 11:55:56 -05001268 /* no write fastpath */
1269 if (area->vm_mm == ZS_MM_RO)
1270 goto out;
Seth Jennings5f6019022012-07-02 16:15:49 -05001271
Minchan Kim7b60a682015-04-15 16:15:39 -07001272 buf = area->vm_buf;
YiPing Xua82cbf02016-03-17 14:20:39 -07001273 buf = buf + ZS_HANDLE_SIZE;
1274 size -= ZS_HANDLE_SIZE;
1275 off += ZS_HANDLE_SIZE;
Minchan Kim2e40e162015-04-15 16:15:23 -07001276
Seth Jennings5f6019022012-07-02 16:15:49 -05001277 sizes[0] = PAGE_SIZE - off;
1278 sizes[1] = size - sizes[0];
1279
1280 /* copy per-cpu buffer to object */
1281 addr = kmap_atomic(pages[0]);
1282 memcpy(addr + off, buf, sizes[0]);
1283 kunmap_atomic(addr);
1284 addr = kmap_atomic(pages[1]);
1285 memcpy(addr, buf + sizes[0], sizes[1]);
1286 kunmap_atomic(addr);
Seth Jenningsf5536462012-07-18 11:55:56 -05001287
1288out:
1289 /* enable page faults to match kunmap_atomic() return conditions */
1290 pagefault_enable();
Seth Jennings5f6019022012-07-02 16:15:49 -05001291}
Nitin Gupta61989a82012-01-09 16:51:56 -06001292
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001293static int zs_cpu_prepare(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001294{
Nitin Gupta61989a82012-01-09 16:51:56 -06001295 struct mapping_area *area;
1296
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001297 area = &per_cpu(zs_map_area, cpu);
1298 return __zs_cpu_up(area);
Nitin Gupta61989a82012-01-09 16:51:56 -06001299}
1300
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001301static int zs_cpu_dead(unsigned int cpu)
Nitin Gupta61989a82012-01-09 16:51:56 -06001302{
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001303 struct mapping_area *area;
Nitin Gupta61989a82012-01-09 16:51:56 -06001304
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01001305 area = &per_cpu(zs_map_area, cpu);
1306 __zs_cpu_down(area);
1307 return 0;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001308}
1309
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001310static bool can_merge(struct size_class *prev, int pages_per_zspage,
1311 int objs_per_zspage)
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001312{
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001313 if (prev->pages_per_zspage == pages_per_zspage &&
1314 prev->objs_per_zspage == objs_per_zspage)
1315 return true;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001316
Ganesh Mahendran64d90462016-07-28 15:47:51 -07001317 return false;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08001318}
1319
Minchan Kim37836892016-07-26 15:23:23 -07001320static bool zspage_full(struct size_class *class, struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001321{
Minchan Kim37836892016-07-26 15:23:23 -07001322 return get_zspage_inuse(zspage) == class->objs_per_zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001323}
1324
Alexey Romanov7c2af302022-11-09 20:50:42 +09001325/**
1326 * zs_lookup_class_index() - Returns index of the zsmalloc &size_class
1327 * that hold objects of the provided size.
1328 * @pool: zsmalloc pool to use
1329 * @size: object size
1330 *
1331 * Context: Any context.
1332 *
1333 * Return: the index of the zsmalloc &size_class that hold objects of the
1334 * provided size.
1335 */
1336unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size)
1337{
1338 struct size_class *class;
1339
1340 class = pool->size_class[get_size_class_index(size)];
1341
1342 return class->index;
1343}
1344EXPORT_SYMBOL_GPL(zs_lookup_class_index);
1345
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001346unsigned long zs_get_total_pages(struct zs_pool *pool)
1347{
1348 return atomic_long_read(&pool->pages_allocated);
1349}
1350EXPORT_SYMBOL_GPL(zs_get_total_pages);
1351
1352/**
1353 * zs_map_object - get address of allocated object from handle.
1354 * @pool: pool from which the object was allocated
1355 * @handle: handle returned from zs_malloc
Ingo Molnarf0953a12021-05-06 18:06:47 -07001356 * @mm: mapping mode to use
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001357 *
1358 * Before using an object allocated from zs_malloc, it must be mapped using
1359 * this function. When done with the object, it must be unmapped using
1360 * zs_unmap_object.
1361 *
1362 * Only one object can be mapped per cpu at a time. There is no protection
1363 * against nested mappings.
1364 *
1365 * This function returns with preemption and page faults disabled.
1366 */
1367void *zs_map_object(struct zs_pool *pool, unsigned long handle,
1368 enum zs_mapmode mm)
1369{
Minchan Kim37836892016-07-26 15:23:23 -07001370 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001371 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001372 unsigned long obj, off;
1373 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001374
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001375 struct size_class *class;
1376 struct mapping_area *area;
1377 struct page *pages[2];
Minchan Kim2e40e162015-04-15 16:15:23 -07001378 void *ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001379
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001380 /*
1381 * Because we use per-cpu mapping areas shared among the
1382 * pools/users, we can't allow mapping in interrupt context
1383 * because it can corrupt another users mappings.
1384 */
Sergey Senozhatsky1aedcaf2017-11-15 17:34:03 -08001385 BUG_ON(in_interrupt());
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001386
Minchan Kimb475d422022-01-21 22:14:13 -08001387 /* It guarantees it can get zspage from handle safely */
Nhat Phamc0547d02022-11-28 11:16:12 -08001388 spin_lock(&pool->lock);
Minchan Kim2e40e162015-04-15 16:15:23 -07001389 obj = handle_to_obj(handle);
1390 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001391 zspage = get_zspage(page);
Minchan Kim48b48002016-07-26 15:23:31 -07001392
Nhat Pham64f768c2022-11-28 11:16:13 -08001393#ifdef CONFIG_ZPOOL
1394 /*
1395 * Move the zspage to front of pool's LRU.
1396 *
1397 * Note that this is swap-specific, so by definition there are no ongoing
1398 * accesses to the memory while the page is swapped out that would make
1399 * it "hot". A new entry is hot, then ages to the tail until it gets either
1400 * written back or swaps back in.
1401 *
1402 * Furthermore, map is also called during writeback. We must not put an
1403 * isolated page on the LRU mid-reclaim.
1404 *
1405 * As a result, only update the LRU when the page is mapped for write
1406 * when it's first instantiated.
1407 *
1408 * This is a deviation from the other backends, which perform this update
1409 * in the allocation function (zbud_alloc, z3fold_alloc).
1410 */
1411 if (mm == ZS_MM_WO) {
1412 if (!list_empty(&zspage->lru))
1413 list_del(&zspage->lru);
1414 list_add(&zspage->lru, &pool->lru);
1415 }
1416#endif
1417
Minchan Kimb475d422022-01-21 22:14:13 -08001418 /*
Nhat Phamc0547d02022-11-28 11:16:12 -08001419 * migration cannot move any zpages in this zspage. Here, pool->lock
Minchan Kimb475d422022-01-21 22:14:13 -08001420 * is too heavy since callers would take some time until they calls
1421 * zs_unmap_object API so delegate the locking from class to zspage
1422 * which is smaller granularity.
1423 */
Minchan Kim48b48002016-07-26 15:23:31 -07001424 migrate_read_lock(zspage);
Nhat Phamc0547d02022-11-28 11:16:12 -08001425 spin_unlock(&pool->lock);
Minchan Kim48b48002016-07-26 15:23:31 -07001426
Minchan Kim67f1c9cd2022-01-21 22:13:51 -08001427 class = zspage_class(pool, zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001428 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001429
Mike Galbraitha3726592022-01-21 22:14:17 -08001430 local_lock(&zs_map_area.lock);
1431 area = this_cpu_ptr(&zs_map_area);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001432 area->vm_mm = mm;
1433 if (off + class->size <= PAGE_SIZE) {
1434 /* this object is contained entirely within a page */
1435 area->vm_addr = kmap_atomic(page);
Minchan Kim2e40e162015-04-15 16:15:23 -07001436 ret = area->vm_addr + off;
1437 goto out;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001438 }
1439
1440 /* this object spans two pages */
1441 pages[0] = page;
1442 pages[1] = get_next_page(page);
1443 BUG_ON(!pages[1]);
1444
Minchan Kim2e40e162015-04-15 16:15:23 -07001445 ret = __zs_map_object(area, pages, off, class->size);
1446out:
Minchan Kima41ec882022-01-21 22:14:04 -08001447 if (likely(!ZsHugePage(zspage)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001448 ret += ZS_HANDLE_SIZE;
1449
1450 return ret;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001451}
1452EXPORT_SYMBOL_GPL(zs_map_object);
1453
1454void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
1455{
Minchan Kim37836892016-07-26 15:23:23 -07001456 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001457 struct page *page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001458 unsigned long obj, off;
1459 unsigned int obj_idx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001460
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001461 struct size_class *class;
1462 struct mapping_area *area;
1463
Minchan Kim2e40e162015-04-15 16:15:23 -07001464 obj = handle_to_obj(handle);
1465 obj_to_location(obj, &page, &obj_idx);
Minchan Kim37836892016-07-26 15:23:23 -07001466 zspage = get_zspage(page);
Minchan Kim67f1c9cd2022-01-21 22:13:51 -08001467 class = zspage_class(pool, zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001468 off = (class->size * obj_idx) & ~PAGE_MASK;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001469
1470 area = this_cpu_ptr(&zs_map_area);
1471 if (off + class->size <= PAGE_SIZE)
1472 kunmap_atomic(area->vm_addr);
1473 else {
1474 struct page *pages[2];
1475
1476 pages[0] = page;
1477 pages[1] = get_next_page(page);
1478 BUG_ON(!pages[1]);
1479
1480 __zs_unmap_object(area, pages, off, class->size);
1481 }
Mike Galbraitha3726592022-01-21 22:14:17 -08001482 local_unlock(&zs_map_area.lock);
Minchan Kim48b48002016-07-26 15:23:31 -07001483
1484 migrate_read_unlock(zspage);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001485}
1486EXPORT_SYMBOL_GPL(zs_unmap_object);
1487
Sergey Senozhatsky010b4952018-04-05 16:24:43 -07001488/**
1489 * zs_huge_class_size() - Returns the size (in bytes) of the first huge
1490 * zsmalloc &size_class.
1491 * @pool: zsmalloc pool to use
1492 *
1493 * The function returns the size of the first huge class - any object of equal
1494 * or bigger size will be stored in zspage consisting of a single physical
1495 * page.
1496 *
1497 * Context: Any context.
1498 *
1499 * Return: the size (in bytes) of the first huge zsmalloc &size_class.
1500 */
1501size_t zs_huge_class_size(struct zs_pool *pool)
1502{
1503 return huge_class_size;
1504}
1505EXPORT_SYMBOL_GPL(zs_huge_class_size);
1506
Minchan Kim0a5f0792022-01-21 22:13:57 -08001507static unsigned long obj_malloc(struct zs_pool *pool,
Minchan Kim37836892016-07-26 15:23:23 -07001508 struct zspage *zspage, unsigned long handle)
Minchan Kimc7806262015-04-15 16:15:26 -07001509{
Minchan Kimbfd093f2016-07-26 15:23:28 -07001510 int i, nr_page, offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001511 unsigned long obj;
1512 struct link_free *link;
Minchan Kim0a5f0792022-01-21 22:13:57 -08001513 struct size_class *class;
Minchan Kimc7806262015-04-15 16:15:26 -07001514
1515 struct page *m_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001516 unsigned long m_offset;
Minchan Kimc7806262015-04-15 16:15:26 -07001517 void *vaddr;
1518
Minchan Kim0a5f0792022-01-21 22:13:57 -08001519 class = pool->size_class[zspage->class];
Minchan Kim312fcae2015-04-15 16:15:30 -07001520 handle |= OBJ_ALLOCATED_TAG;
Minchan Kim37836892016-07-26 15:23:23 -07001521 obj = get_freeobj(zspage);
Minchan Kimbfd093f2016-07-26 15:23:28 -07001522
1523 offset = obj * class->size;
1524 nr_page = offset >> PAGE_SHIFT;
1525 m_offset = offset & ~PAGE_MASK;
1526 m_page = get_first_page(zspage);
1527
1528 for (i = 0; i < nr_page; i++)
1529 m_page = get_next_page(m_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001530
1531 vaddr = kmap_atomic(m_page);
1532 link = (struct link_free *)vaddr + m_offset / sizeof(*link);
Minchan Kim3b1d9ca2016-07-26 15:23:37 -07001533 set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
Minchan Kima41ec882022-01-21 22:14:04 -08001534 if (likely(!ZsHugePage(zspage)))
Minchan Kim7b60a682015-04-15 16:15:39 -07001535 /* record handle in the header of allocated chunk */
1536 link->handle = handle;
1537 else
Minchan Kim37836892016-07-26 15:23:23 -07001538 /* record handle to page->index */
1539 zspage->first_page->index = handle;
1540
Minchan Kimc7806262015-04-15 16:15:26 -07001541 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001542 mod_zspage_inuse(zspage, 1);
Minchan Kimc7806262015-04-15 16:15:26 -07001543
Minchan Kimbfd093f2016-07-26 15:23:28 -07001544 obj = location_to_obj(m_page, obj);
1545
Minchan Kimc7806262015-04-15 16:15:26 -07001546 return obj;
1547}
1548
1549
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001550/**
1551 * zs_malloc - Allocate block of given size from pool.
1552 * @pool: pool to allocate from
1553 * @size: size of block to allocate
Ganesh Mahendranfd854462016-07-28 15:47:54 -07001554 * @gfp: gfp flags when allocating object
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001555 *
1556 * On success, handle to the allocated object is returned,
Hui Zhuc7e6f172022-07-14 16:07:57 +08001557 * otherwise an ERR_PTR().
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001558 * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
1559 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07001560unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001561{
Minchan Kim2e40e162015-04-15 16:15:23 -07001562 unsigned long handle, obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001563 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07001564 enum fullness_group newfg;
Minchan Kim37836892016-07-26 15:23:23 -07001565 struct zspage *zspage;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001566
Minchan Kim7b60a682015-04-15 16:15:39 -07001567 if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
Hui Zhuc7e6f172022-07-14 16:07:57 +08001568 return (unsigned long)ERR_PTR(-EINVAL);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001569
Minchan Kim37836892016-07-26 15:23:23 -07001570 handle = cache_alloc_handle(pool, gfp);
Minchan Kim2e40e162015-04-15 16:15:23 -07001571 if (!handle)
Hui Zhuc7e6f172022-07-14 16:07:57 +08001572 return (unsigned long)ERR_PTR(-ENOMEM);
Minchan Kim2e40e162015-04-15 16:15:23 -07001573
1574 /* extra space in chunk to keep the handle */
1575 size += ZS_HANDLE_SIZE;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001576 class = pool->size_class[get_size_class_index(size)];
1577
Nhat Phamc0547d02022-11-28 11:16:12 -08001578 /* pool->lock effectively protects the zpage migration */
1579 spin_lock(&pool->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001580 zspage = find_get_zspage(class);
Minchan Kim48b48002016-07-26 15:23:31 -07001581 if (likely(zspage)) {
Minchan Kim0a5f0792022-01-21 22:13:57 -08001582 obj = obj_malloc(pool, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001583 /* Now move the zspage to another fullness group, if required */
1584 fix_fullness_group(class, zspage);
1585 record_obj(handle, obj);
Minchan Kim0a5f0792022-01-21 22:13:57 -08001586 class_stat_inc(class, OBJ_USED, 1);
Nhat Phamc0547d02022-11-28 11:16:12 -08001587 spin_unlock(&pool->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001588
Minchan Kim48b48002016-07-26 15:23:31 -07001589 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001590 }
1591
Nhat Phamc0547d02022-11-28 11:16:12 -08001592 spin_unlock(&pool->lock);
Minchan Kim48b48002016-07-26 15:23:31 -07001593
1594 zspage = alloc_zspage(pool, class, gfp);
1595 if (!zspage) {
1596 cache_free_handle(pool, handle);
Hui Zhuc7e6f172022-07-14 16:07:57 +08001597 return (unsigned long)ERR_PTR(-ENOMEM);
Minchan Kim48b48002016-07-26 15:23:31 -07001598 }
1599
Nhat Phamc0547d02022-11-28 11:16:12 -08001600 spin_lock(&pool->lock);
Minchan Kim0a5f0792022-01-21 22:13:57 -08001601 obj = obj_malloc(pool, zspage, handle);
Minchan Kim48b48002016-07-26 15:23:31 -07001602 newfg = get_fullness_group(class, zspage);
1603 insert_zspage(class, zspage, newfg);
1604 set_zspage_mapping(zspage, class->index, newfg);
Minchan Kim2e40e162015-04-15 16:15:23 -07001605 record_obj(handle, obj);
Minchan Kim48b48002016-07-26 15:23:31 -07001606 atomic_long_add(class->pages_per_zspage,
1607 &pool->pages_allocated);
Minchan Kim3828a762022-01-21 22:13:54 -08001608 class_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
Minchan Kim0a5f0792022-01-21 22:13:57 -08001609 class_stat_inc(class, OBJ_USED, 1);
Minchan Kim48b48002016-07-26 15:23:31 -07001610
1611 /* We completely set up zspage so mark them as movable */
1612 SetZsPageMovable(pool, zspage);
Nhat Phamc0547d02022-11-28 11:16:12 -08001613 spin_unlock(&pool->lock);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001614
Minchan Kim2e40e162015-04-15 16:15:23 -07001615 return handle;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001616}
1617EXPORT_SYMBOL_GPL(zs_malloc);
1618
Nhat Pham85b32582023-01-10 15:17:01 -08001619static void obj_free(int class_size, unsigned long obj, unsigned long *handle)
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001620{
1621 struct link_free *link;
Minchan Kim37836892016-07-26 15:23:23 -07001622 struct zspage *zspage;
1623 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001624 unsigned long f_offset;
1625 unsigned int f_objidx;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001626 void *vaddr;
1627
Minchan Kimc7806262015-04-15 16:15:26 -07001628 obj_to_location(obj, &f_page, &f_objidx);
Minchan Kim0a5f0792022-01-21 22:13:57 -08001629 f_offset = (class_size * f_objidx) & ~PAGE_MASK;
Minchan Kim37836892016-07-26 15:23:23 -07001630 zspage = get_zspage(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001631
Minchan Kimc7806262015-04-15 16:15:26 -07001632 vaddr = kmap_atomic(f_page);
Minchan Kimc7806262015-04-15 16:15:26 -07001633 link = (struct link_free *)(vaddr + f_offset);
Nhat Pham85b32582023-01-10 15:17:01 -08001634
1635 if (handle) {
1636#ifdef CONFIG_ZPOOL
1637 /* Stores the (deferred) handle in the object's header */
1638 *handle |= OBJ_DEFERRED_HANDLE_TAG;
1639 *handle &= ~OBJ_ALLOCATED_TAG;
1640
1641 if (likely(!ZsHugePage(zspage)))
1642 link->deferred_handle = *handle;
1643 else
1644 f_page->index = *handle;
1645#endif
1646 } else {
1647 /* Insert this object in containing zspage's freelist */
1648 if (likely(!ZsHugePage(zspage)))
1649 link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
1650 else
1651 f_page->index = 0;
1652 set_freeobj(zspage, f_objidx);
1653 }
1654
Minchan Kimc7806262015-04-15 16:15:26 -07001655 kunmap_atomic(vaddr);
Minchan Kim37836892016-07-26 15:23:23 -07001656 mod_zspage_inuse(zspage, -1);
Minchan Kimc7806262015-04-15 16:15:26 -07001657}
1658
1659void zs_free(struct zs_pool *pool, unsigned long handle)
1660{
Minchan Kim37836892016-07-26 15:23:23 -07001661 struct zspage *zspage;
1662 struct page *f_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001663 unsigned long obj;
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001664 struct size_class *class;
1665 enum fullness_group fullness;
1666
Sergey Senozhatskya5d21722022-08-16 14:09:06 +09001667 if (IS_ERR_OR_NULL((void *)handle))
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001668 return;
1669
Minchan Kimb475d422022-01-21 22:14:13 -08001670 /*
Nhat Phamc0547d02022-11-28 11:16:12 -08001671 * The pool->lock protects the race with zpage's migration
Minchan Kimb475d422022-01-21 22:14:13 -08001672 * so it's safe to get the page from handle.
1673 */
Nhat Phamc0547d02022-11-28 11:16:12 -08001674 spin_lock(&pool->lock);
Minchan Kim2e40e162015-04-15 16:15:23 -07001675 obj = handle_to_obj(handle);
Minchan Kim67f1c9cd2022-01-21 22:13:51 -08001676 obj_to_page(obj, &f_page);
Minchan Kim37836892016-07-26 15:23:23 -07001677 zspage = get_zspage(f_page);
Minchan Kim67f1c9cd2022-01-21 22:13:51 -08001678 class = zspage_class(pool, zspage);
Minchan Kimb475d422022-01-21 22:14:13 -08001679
Minchan Kim0a5f0792022-01-21 22:13:57 -08001680 class_stat_dec(class, OBJ_USED, 1);
Minchan Kim48b48002016-07-26 15:23:31 -07001681
Nhat Pham9997bc02022-11-28 11:16:15 -08001682#ifdef CONFIG_ZPOOL
1683 if (zspage->under_reclaim) {
1684 /*
1685 * Reclaim needs the handles during writeback. It'll free
1686 * them along with the zspage when it's done with them.
1687 *
Nhat Pham85b32582023-01-10 15:17:01 -08001688 * Record current deferred handle in the object's header.
Nhat Pham9997bc02022-11-28 11:16:15 -08001689 */
Nhat Pham85b32582023-01-10 15:17:01 -08001690 obj_free(class->size, obj, &handle);
Nhat Pham9997bc02022-11-28 11:16:15 -08001691 spin_unlock(&pool->lock);
1692 return;
1693 }
1694#endif
Nhat Pham85b32582023-01-10 15:17:01 -08001695 obj_free(class->size, obj, NULL);
1696
Nhat Pham9997bc02022-11-28 11:16:15 -08001697 fullness = fix_fullness_group(class, zspage);
1698 if (fullness == ZS_EMPTY)
1699 free_zspage(pool, class, zspage);
1700
Nhat Phamc0547d02022-11-28 11:16:12 -08001701 spin_unlock(&pool->lock);
Minchan Kim37836892016-07-26 15:23:23 -07001702 cache_free_handle(pool, handle);
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08001703}
1704EXPORT_SYMBOL_GPL(zs_free);
1705
Minchan Kim251cbb92016-05-20 16:59:42 -07001706static void zs_object_copy(struct size_class *class, unsigned long dst,
1707 unsigned long src)
Minchan Kim312fcae2015-04-15 16:15:30 -07001708{
1709 struct page *s_page, *d_page;
Minchan Kimbfd093f2016-07-26 15:23:28 -07001710 unsigned int s_objidx, d_objidx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001711 unsigned long s_off, d_off;
1712 void *s_addr, *d_addr;
1713 int s_size, d_size, size;
1714 int written = 0;
1715
1716 s_size = d_size = class->size;
1717
1718 obj_to_location(src, &s_page, &s_objidx);
1719 obj_to_location(dst, &d_page, &d_objidx);
1720
Minchan Kimbfd093f2016-07-26 15:23:28 -07001721 s_off = (class->size * s_objidx) & ~PAGE_MASK;
1722 d_off = (class->size * d_objidx) & ~PAGE_MASK;
Minchan Kim312fcae2015-04-15 16:15:30 -07001723
1724 if (s_off + class->size > PAGE_SIZE)
1725 s_size = PAGE_SIZE - s_off;
1726
1727 if (d_off + class->size > PAGE_SIZE)
1728 d_size = PAGE_SIZE - d_off;
1729
1730 s_addr = kmap_atomic(s_page);
1731 d_addr = kmap_atomic(d_page);
1732
1733 while (1) {
1734 size = min(s_size, d_size);
1735 memcpy(d_addr + d_off, s_addr + s_off, size);
1736 written += size;
1737
1738 if (written == class->size)
1739 break;
1740
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001741 s_off += size;
1742 s_size -= size;
1743 d_off += size;
1744 d_size -= size;
1745
Alexey Romanov050a3882022-08-11 18:37:54 +03001746 /*
1747 * Calling kunmap_atomic(d_addr) is necessary. kunmap_atomic()
1748 * calls must occurs in reverse order of calls to kmap_atomic().
1749 * So, to call kunmap_atomic(s_addr) we should first call
Alexey Romanov46e87152022-08-15 17:48:25 +03001750 * kunmap_atomic(d_addr). For more details see
1751 * Documentation/mm/highmem.rst.
Alexey Romanov050a3882022-08-11 18:37:54 +03001752 */
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001753 if (s_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001754 kunmap_atomic(d_addr);
1755 kunmap_atomic(s_addr);
1756 s_page = get_next_page(s_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001757 s_addr = kmap_atomic(s_page);
1758 d_addr = kmap_atomic(d_page);
1759 s_size = class->size - written;
1760 s_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001761 }
1762
Sergey Senozhatsky495819e2015-04-15 16:16:15 -07001763 if (d_off >= PAGE_SIZE) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001764 kunmap_atomic(d_addr);
1765 d_page = get_next_page(d_page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001766 d_addr = kmap_atomic(d_page);
1767 d_size = class->size - written;
1768 d_off = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001769 }
1770 }
1771
1772 kunmap_atomic(d_addr);
1773 kunmap_atomic(s_addr);
1774}
1775
1776/*
Nhat Pham85b32582023-01-10 15:17:01 -08001777 * Find object with a certain tag in zspage from index object and
Minchan Kim312fcae2015-04-15 16:15:30 -07001778 * return handle.
1779 */
Nhat Pham85b32582023-01-10 15:17:01 -08001780static unsigned long find_tagged_obj(struct size_class *class,
1781 struct page *page, int *obj_idx, int tag)
Minchan Kim312fcae2015-04-15 16:15:30 -07001782{
Alexey Romanov671f2fa2022-09-09 11:37:22 +03001783 unsigned int offset;
Ganesh Mahendrancf675acb2016-07-28 15:47:46 -07001784 int index = *obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001785 unsigned long handle = 0;
1786 void *addr = kmap_atomic(page);
1787
Minchan Kim37836892016-07-26 15:23:23 -07001788 offset = get_first_obj_offset(page);
Minchan Kim312fcae2015-04-15 16:15:30 -07001789 offset += class->size * index;
1790
1791 while (offset < PAGE_SIZE) {
Nhat Pham85b32582023-01-10 15:17:01 -08001792 if (obj_tagged(page, addr + offset, &handle, tag))
Minchan Kimb475d422022-01-21 22:14:13 -08001793 break;
Minchan Kim312fcae2015-04-15 16:15:30 -07001794
1795 offset += class->size;
1796 index++;
1797 }
1798
1799 kunmap_atomic(addr);
Ganesh Mahendrancf675acb2016-07-28 15:47:46 -07001800
1801 *obj_idx = index;
1802
Minchan Kim312fcae2015-04-15 16:15:30 -07001803 return handle;
1804}
1805
Nhat Pham85b32582023-01-10 15:17:01 -08001806/*
1807 * Find alloced object in zspage from index object and
1808 * return handle.
1809 */
1810static unsigned long find_alloced_obj(struct size_class *class,
1811 struct page *page, int *obj_idx)
1812{
1813 return find_tagged_obj(class, page, obj_idx, OBJ_ALLOCATED_TAG);
1814}
1815
1816#ifdef CONFIG_ZPOOL
1817/*
1818 * Find object storing a deferred handle in header in zspage from index object
1819 * and return handle.
1820 */
1821static unsigned long find_deferred_handle_obj(struct size_class *class,
1822 struct page *page, int *obj_idx)
1823{
1824 return find_tagged_obj(class, page, obj_idx, OBJ_DEFERRED_HANDLE_TAG);
1825}
1826#endif
1827
Minchan Kim312fcae2015-04-15 16:15:30 -07001828struct zs_compact_control {
Minchan Kim37836892016-07-26 15:23:23 -07001829 /* Source spage for migration which could be a subpage of zspage */
Minchan Kim312fcae2015-04-15 16:15:30 -07001830 struct page *s_page;
1831 /* Destination page for migration which should be a first page
1832 * of zspage. */
1833 struct page *d_page;
1834 /* Starting object index within @s_page which used for live object
1835 * in the subpage. */
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001836 int obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001837};
1838
1839static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
1840 struct zs_compact_control *cc)
1841{
1842 unsigned long used_obj, free_obj;
1843 unsigned long handle;
1844 struct page *s_page = cc->s_page;
1845 struct page *d_page = cc->d_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001846 int obj_idx = cc->obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001847 int ret = 0;
1848
1849 while (1) {
Ganesh Mahendrancf675acb2016-07-28 15:47:46 -07001850 handle = find_alloced_obj(class, s_page, &obj_idx);
Minchan Kim312fcae2015-04-15 16:15:30 -07001851 if (!handle) {
1852 s_page = get_next_page(s_page);
1853 if (!s_page)
1854 break;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001855 obj_idx = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07001856 continue;
1857 }
1858
1859 /* Stop if there is no more space */
Minchan Kim37836892016-07-26 15:23:23 -07001860 if (zspage_full(class, get_zspage(d_page))) {
Minchan Kim312fcae2015-04-15 16:15:30 -07001861 ret = -ENOMEM;
1862 break;
1863 }
1864
1865 used_obj = handle_to_obj(handle);
Minchan Kim0a5f0792022-01-21 22:13:57 -08001866 free_obj = obj_malloc(pool, get_zspage(d_page), handle);
Minchan Kim251cbb92016-05-20 16:59:42 -07001867 zs_object_copy(class, free_obj, used_obj);
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001868 obj_idx++;
Minchan Kim312fcae2015-04-15 16:15:30 -07001869 record_obj(handle, free_obj);
Nhat Pham85b32582023-01-10 15:17:01 -08001870 obj_free(class->size, used_obj, NULL);
Minchan Kim312fcae2015-04-15 16:15:30 -07001871 }
1872
1873 /* Remember last position in this iteration */
1874 cc->s_page = s_page;
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07001875 cc->obj_idx = obj_idx;
Minchan Kim312fcae2015-04-15 16:15:30 -07001876
1877 return ret;
1878}
1879
Minchan Kim37836892016-07-26 15:23:23 -07001880static struct zspage *isolate_zspage(struct size_class *class, bool source)
Minchan Kim312fcae2015-04-15 16:15:30 -07001881{
1882 int i;
Minchan Kim37836892016-07-26 15:23:23 -07001883 struct zspage *zspage;
1884 enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
Minchan Kim312fcae2015-04-15 16:15:30 -07001885
Minchan Kim37836892016-07-26 15:23:23 -07001886 if (!source) {
1887 fg[0] = ZS_ALMOST_FULL;
1888 fg[1] = ZS_ALMOST_EMPTY;
1889 }
1890
1891 for (i = 0; i < 2; i++) {
1892 zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
1893 struct zspage, list);
1894 if (zspage) {
1895 remove_zspage(class, zspage, fg[i]);
1896 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001897 }
1898 }
1899
Minchan Kim37836892016-07-26 15:23:23 -07001900 return zspage;
Minchan Kim312fcae2015-04-15 16:15:30 -07001901}
1902
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001903/*
Minchan Kim37836892016-07-26 15:23:23 -07001904 * putback_zspage - add @zspage into right class's fullness list
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001905 * @class: destination class
Minchan Kim37836892016-07-26 15:23:23 -07001906 * @zspage: target page
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001907 *
Minchan Kim37836892016-07-26 15:23:23 -07001908 * Return @zspage's fullness_group
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001909 */
Minchan Kim4aa409c2016-07-26 15:23:26 -07001910static enum fullness_group putback_zspage(struct size_class *class,
Minchan Kim37836892016-07-26 15:23:23 -07001911 struct zspage *zspage)
Minchan Kim312fcae2015-04-15 16:15:30 -07001912{
Minchan Kim312fcae2015-04-15 16:15:30 -07001913 enum fullness_group fullness;
1914
Minchan Kim37836892016-07-26 15:23:23 -07001915 fullness = get_fullness_group(class, zspage);
1916 insert_zspage(class, zspage, fullness);
1917 set_zspage_mapping(zspage, class->index, fullness);
Minchan Kim839373e2015-04-15 16:16:18 -07001918
Sergey Senozhatsky860c7072015-09-08 15:04:38 -07001919 return fullness;
Minchan Kim312fcae2015-04-15 16:15:30 -07001920}
1921
Nhat Pham9997bc02022-11-28 11:16:15 -08001922#if defined(CONFIG_ZPOOL) || defined(CONFIG_COMPACTION)
Colin Ian King4d0a5402018-08-17 15:46:50 -07001923/*
1924 * To prevent zspage destroy during migration, zspage freeing should
1925 * hold locks of all pages in the zspage.
1926 */
1927static void lock_zspage(struct zspage *zspage)
1928{
Sultan Alsawaf2505a982022-05-13 15:11:26 -07001929 struct page *curr_page, *page;
Colin Ian King4d0a5402018-08-17 15:46:50 -07001930
Sultan Alsawaf2505a982022-05-13 15:11:26 -07001931 /*
1932 * Pages we haven't locked yet can be migrated off the list while we're
1933 * trying to lock them, so we need to be careful and only attempt to
1934 * lock each page under migrate_read_lock(). Otherwise, the page we lock
1935 * may no longer belong to the zspage. This means that we may wait for
1936 * the wrong page to unlock, so we must take a reference to the page
1937 * prior to waiting for it to unlock outside migrate_read_lock().
1938 */
1939 while (1) {
1940 migrate_read_lock(zspage);
1941 page = get_first_page(zspage);
1942 if (trylock_page(page))
1943 break;
1944 get_page(page);
1945 migrate_read_unlock(zspage);
1946 wait_on_page_locked(page);
1947 put_page(page);
1948 }
1949
1950 curr_page = page;
1951 while ((page = get_next_page(curr_page))) {
1952 if (trylock_page(page)) {
1953 curr_page = page;
1954 } else {
1955 get_page(page);
1956 migrate_read_unlock(zspage);
1957 wait_on_page_locked(page);
1958 put_page(page);
1959 migrate_read_lock(zspage);
1960 }
1961 }
1962 migrate_read_unlock(zspage);
Colin Ian King4d0a5402018-08-17 15:46:50 -07001963}
Nhat Pham9997bc02022-11-28 11:16:15 -08001964#endif /* defined(CONFIG_ZPOOL) || defined(CONFIG_COMPACTION) */
1965
1966#ifdef CONFIG_ZPOOL
1967/*
1968 * Unlocks all the pages of the zspage.
1969 *
1970 * pool->lock must be held before this function is called
1971 * to prevent the underlying pages from migrating.
1972 */
1973static void unlock_zspage(struct zspage *zspage)
1974{
1975 struct page *page = get_first_page(zspage);
1976
1977 do {
1978 unlock_page(page);
1979 } while ((page = get_next_page(page)) != NULL);
1980}
1981#endif /* CONFIG_ZPOOL */
Colin Ian King4d0a5402018-08-17 15:46:50 -07001982
Minchan Kim48b48002016-07-26 15:23:31 -07001983static void migrate_lock_init(struct zspage *zspage)
1984{
1985 rwlock_init(&zspage->lock);
1986}
1987
Jules Irengecfc451cf2020-04-06 20:08:21 -07001988static void migrate_read_lock(struct zspage *zspage) __acquires(&zspage->lock)
Minchan Kim48b48002016-07-26 15:23:31 -07001989{
1990 read_lock(&zspage->lock);
1991}
1992
Jules Irenge8a374cc2020-04-06 20:08:24 -07001993static void migrate_read_unlock(struct zspage *zspage) __releases(&zspage->lock)
Minchan Kim48b48002016-07-26 15:23:31 -07001994{
1995 read_unlock(&zspage->lock);
1996}
1997
Nhat Pham9997bc02022-11-28 11:16:15 -08001998#ifdef CONFIG_COMPACTION
Minchan Kim48b48002016-07-26 15:23:31 -07001999static void migrate_write_lock(struct zspage *zspage)
2000{
2001 write_lock(&zspage->lock);
2002}
2003
Minchan Kimb475d422022-01-21 22:14:13 -08002004static void migrate_write_lock_nested(struct zspage *zspage)
2005{
2006 write_lock_nested(&zspage->lock, SINGLE_DEPTH_NESTING);
2007}
2008
Minchan Kim48b48002016-07-26 15:23:31 -07002009static void migrate_write_unlock(struct zspage *zspage)
2010{
2011 write_unlock(&zspage->lock);
2012}
2013
2014/* Number of isolated subpage for *page migration* in this zspage */
2015static void inc_zspage_isolation(struct zspage *zspage)
2016{
2017 zspage->isolated++;
2018}
2019
2020static void dec_zspage_isolation(struct zspage *zspage)
2021{
Minchan Kimc4549b82022-01-21 22:14:07 -08002022 VM_BUG_ON(zspage->isolated == 0);
Minchan Kim48b48002016-07-26 15:23:31 -07002023 zspage->isolated--;
2024}
2025
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -04002026static const struct movable_operations zsmalloc_mops;
2027
Minchan Kim48b48002016-07-26 15:23:31 -07002028static void replace_sub_page(struct size_class *class, struct zspage *zspage,
2029 struct page *newpage, struct page *oldpage)
2030{
2031 struct page *page;
2032 struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
2033 int idx = 0;
2034
2035 page = get_first_page(zspage);
2036 do {
2037 if (page == oldpage)
2038 pages[idx] = newpage;
2039 else
2040 pages[idx] = page;
2041 idx++;
2042 } while ((page = get_next_page(page)) != NULL);
2043
2044 create_page_chain(class, zspage, pages);
2045 set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
Minchan Kima41ec882022-01-21 22:14:04 -08002046 if (unlikely(ZsHugePage(zspage)))
Minchan Kim48b48002016-07-26 15:23:31 -07002047 newpage->index = oldpage->index;
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -04002048 __SetPageMovable(newpage, &zsmalloc_mops);
Minchan Kim48b48002016-07-26 15:23:31 -07002049}
2050
Colin Ian King4d0a5402018-08-17 15:46:50 -07002051static bool zs_page_isolate(struct page *page, isolate_mode_t mode)
Minchan Kim48b48002016-07-26 15:23:31 -07002052{
Minchan Kim48b48002016-07-26 15:23:31 -07002053 struct zspage *zspage;
Minchan Kim48b48002016-07-26 15:23:31 -07002054
2055 /*
2056 * Page is locked so zspage couldn't be destroyed. For detail, look at
2057 * lock_zspage in free_zspage.
2058 */
2059 VM_BUG_ON_PAGE(!PageMovable(page), page);
2060 VM_BUG_ON_PAGE(PageIsolated(page), page);
2061
2062 zspage = get_zspage(page);
Minchan Kimc4549b82022-01-21 22:14:07 -08002063 migrate_write_lock(zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07002064 inc_zspage_isolation(zspage);
Minchan Kimc4549b82022-01-21 22:14:07 -08002065 migrate_write_unlock(zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07002066
2067 return true;
2068}
2069
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -04002070static int zs_page_migrate(struct page *newpage, struct page *page,
2071 enum migrate_mode mode)
Minchan Kim48b48002016-07-26 15:23:31 -07002072{
2073 struct zs_pool *pool;
2074 struct size_class *class;
Minchan Kim48b48002016-07-26 15:23:31 -07002075 struct zspage *zspage;
2076 struct page *dummy;
2077 void *s_addr, *d_addr, *addr;
Alexey Romanov671f2fa2022-09-09 11:37:22 +03002078 unsigned int offset;
Minchan Kim3ae92ac2022-01-21 22:14:01 -08002079 unsigned long handle;
Minchan Kim48b48002016-07-26 15:23:31 -07002080 unsigned long old_obj, new_obj;
2081 unsigned int obj_idx;
Minchan Kim48b48002016-07-26 15:23:31 -07002082
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07002083 /*
2084 * We cannot support the _NO_COPY case here, because copy needs to
2085 * happen under the zs lock, which does not work with
2086 * MIGRATE_SYNC_NO_COPY workflow.
2087 */
2088 if (mode == MIGRATE_SYNC_NO_COPY)
2089 return -EINVAL;
2090
Minchan Kim48b48002016-07-26 15:23:31 -07002091 VM_BUG_ON_PAGE(!PageMovable(page), page);
2092 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2093
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -04002094 /* The page is locked, so this pointer must remain valid */
2095 zspage = get_zspage(page);
2096 pool = zspage->pool;
Minchan Kimb475d422022-01-21 22:14:13 -08002097
2098 /*
Nhat Phamc0547d02022-11-28 11:16:12 -08002099 * The pool's lock protects the race between zpage migration
Minchan Kimb475d422022-01-21 22:14:13 -08002100 * and zs_free.
2101 */
Nhat Phamc0547d02022-11-28 11:16:12 -08002102 spin_lock(&pool->lock);
Minchan Kim67f1c9cd2022-01-21 22:13:51 -08002103 class = zspage_class(pool, zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07002104
Minchan Kimb475d422022-01-21 22:14:13 -08002105 /* the migrate_write_lock protects zpage access via zs_map_object */
2106 migrate_write_lock(zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07002107
Minchan Kimb475d422022-01-21 22:14:13 -08002108 offset = get_first_obj_offset(page);
Minchan Kim48b48002016-07-26 15:23:31 -07002109 s_addr = kmap_atomic(page);
Minchan Kim48b48002016-07-26 15:23:31 -07002110
2111 /*
2112 * Here, any user cannot access all objects in the zspage so let's move.
2113 */
2114 d_addr = kmap_atomic(newpage);
2115 memcpy(d_addr, s_addr, PAGE_SIZE);
2116 kunmap_atomic(d_addr);
2117
Minchan Kimb475d422022-01-21 22:14:13 -08002118 for (addr = s_addr + offset; addr < s_addr + PAGE_SIZE;
Minchan Kim48b48002016-07-26 15:23:31 -07002119 addr += class->size) {
Minchan Kim3ae92ac2022-01-21 22:14:01 -08002120 if (obj_allocated(page, addr, &handle)) {
Minchan Kim48b48002016-07-26 15:23:31 -07002121
2122 old_obj = handle_to_obj(handle);
2123 obj_to_location(old_obj, &dummy, &obj_idx);
2124 new_obj = (unsigned long)location_to_obj(newpage,
2125 obj_idx);
Minchan Kim48b48002016-07-26 15:23:31 -07002126 record_obj(handle, new_obj);
2127 }
2128 }
Minchan Kimb475d422022-01-21 22:14:13 -08002129 kunmap_atomic(s_addr);
Minchan Kim48b48002016-07-26 15:23:31 -07002130
2131 replace_sub_page(class, zspage, newpage, page);
Minchan Kimb475d422022-01-21 22:14:13 -08002132 /*
2133 * Since we complete the data copy and set up new zspage structure,
Nhat Phamc0547d02022-11-28 11:16:12 -08002134 * it's okay to release the pool's lock.
Minchan Kimb475d422022-01-21 22:14:13 -08002135 */
Nhat Phamc0547d02022-11-28 11:16:12 -08002136 spin_unlock(&pool->lock);
Minchan Kim48b48002016-07-26 15:23:31 -07002137 dec_zspage_isolation(zspage);
Minchan Kimb475d422022-01-21 22:14:13 -08002138 migrate_write_unlock(zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07002139
Minchan Kimb475d422022-01-21 22:14:13 -08002140 get_page(newpage);
Chanho Minac8f05d2020-01-04 12:59:36 -08002141 if (page_zone(newpage) != page_zone(page)) {
2142 dec_zone_page_state(page, NR_ZSPAGES);
2143 inc_zone_page_state(newpage, NR_ZSPAGES);
2144 }
2145
Minchan Kim48b48002016-07-26 15:23:31 -07002146 reset_page(page);
2147 put_page(page);
Minchan Kim48b48002016-07-26 15:23:31 -07002148
Minchan Kimb475d422022-01-21 22:14:13 -08002149 return MIGRATEPAGE_SUCCESS;
Minchan Kim48b48002016-07-26 15:23:31 -07002150}
2151
Colin Ian King4d0a5402018-08-17 15:46:50 -07002152static void zs_page_putback(struct page *page)
Minchan Kim48b48002016-07-26 15:23:31 -07002153{
Minchan Kim48b48002016-07-26 15:23:31 -07002154 struct zspage *zspage;
2155
2156 VM_BUG_ON_PAGE(!PageMovable(page), page);
2157 VM_BUG_ON_PAGE(!PageIsolated(page), page);
2158
2159 zspage = get_zspage(page);
Minchan Kimc4549b82022-01-21 22:14:07 -08002160 migrate_write_lock(zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07002161 dec_zspage_isolation(zspage);
Minchan Kimc4549b82022-01-21 22:14:07 -08002162 migrate_write_unlock(zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07002163}
2164
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -04002165static const struct movable_operations zsmalloc_mops = {
Minchan Kim48b48002016-07-26 15:23:31 -07002166 .isolate_page = zs_page_isolate,
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -04002167 .migrate_page = zs_page_migrate,
Minchan Kim48b48002016-07-26 15:23:31 -07002168 .putback_page = zs_page_putback,
2169};
2170
Minchan Kim48b48002016-07-26 15:23:31 -07002171/*
2172 * Caller should hold page_lock of all pages in the zspage
2173 * In here, we cannot use zspage meta data.
2174 */
2175static void async_free_zspage(struct work_struct *work)
2176{
2177 int i;
2178 struct size_class *class;
2179 unsigned int class_idx;
2180 enum fullness_group fullness;
2181 struct zspage *zspage, *tmp;
2182 LIST_HEAD(free_pages);
2183 struct zs_pool *pool = container_of(work, struct zs_pool,
2184 free_work);
2185
Jerome Marchandcf8e0fed2017-07-10 15:50:18 -07002186 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Minchan Kim48b48002016-07-26 15:23:31 -07002187 class = pool->size_class[i];
2188 if (class->index != i)
2189 continue;
2190
Nhat Phamc0547d02022-11-28 11:16:12 -08002191 spin_lock(&pool->lock);
Minchan Kim48b48002016-07-26 15:23:31 -07002192 list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
Nhat Phamc0547d02022-11-28 11:16:12 -08002193 spin_unlock(&pool->lock);
Minchan Kim48b48002016-07-26 15:23:31 -07002194 }
2195
Minchan Kim48b48002016-07-26 15:23:31 -07002196 list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
2197 list_del(&zspage->list);
2198 lock_zspage(zspage);
2199
2200 get_zspage_mapping(zspage, &class_idx, &fullness);
2201 VM_BUG_ON(fullness != ZS_EMPTY);
2202 class = pool->size_class[class_idx];
Nhat Phamc0547d02022-11-28 11:16:12 -08002203 spin_lock(&pool->lock);
Nhat Pham64f768c2022-11-28 11:16:13 -08002204#ifdef CONFIG_ZPOOL
2205 list_del(&zspage->lru);
2206#endif
Miaohe Lin33848332021-06-30 18:53:04 -07002207 __free_zspage(pool, class, zspage);
Nhat Phamc0547d02022-11-28 11:16:12 -08002208 spin_unlock(&pool->lock);
Minchan Kim48b48002016-07-26 15:23:31 -07002209 }
2210};
2211
2212static void kick_deferred_free(struct zs_pool *pool)
2213{
2214 schedule_work(&pool->free_work);
2215}
2216
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -04002217static void zs_flush_migration(struct zs_pool *pool)
2218{
2219 flush_work(&pool->free_work);
2220}
2221
Minchan Kim48b48002016-07-26 15:23:31 -07002222static void init_deferred_free(struct zs_pool *pool)
2223{
2224 INIT_WORK(&pool->free_work, async_free_zspage);
2225}
2226
2227static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
2228{
2229 struct page *page = get_first_page(zspage);
2230
2231 do {
2232 WARN_ON(!trylock_page(page));
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -04002233 __SetPageMovable(page, &zsmalloc_mops);
Minchan Kim48b48002016-07-26 15:23:31 -07002234 unlock_page(page);
2235 } while ((page = get_next_page(page)) != NULL);
2236}
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -04002237#else
2238static inline void zs_flush_migration(struct zs_pool *pool) { }
Minchan Kim48b48002016-07-26 15:23:31 -07002239#endif
2240
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002241/*
2242 *
2243 * Based on the number of unused allocated objects calculate
2244 * and return the number of pages that we can free.
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002245 */
2246static unsigned long zs_can_compact(struct size_class *class)
2247{
2248 unsigned long obj_wasted;
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002249 unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
2250 unsigned long obj_used = zs_stat_get(class, OBJ_USED);
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002251
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002252 if (obj_allocated <= obj_used)
2253 return 0;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002254
Sergey Senozhatsky44f43e92016-05-09 16:28:49 -07002255 obj_wasted = obj_allocated - obj_used;
Ganesh Mahendranb4fd07a2016-07-28 15:47:49 -07002256 obj_wasted /= class->objs_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002257
Minchan Kim6cbf16b2015-09-08 15:04:49 -07002258 return obj_wasted * class->pages_per_zspage;
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002259}
2260
Rokudo Yan23959282021-02-25 17:18:31 -08002261static unsigned long __zs_compact(struct zs_pool *pool,
2262 struct size_class *class)
Minchan Kim312fcae2015-04-15 16:15:30 -07002263{
Minchan Kim312fcae2015-04-15 16:15:30 -07002264 struct zs_compact_control cc;
Minchan Kim37836892016-07-26 15:23:23 -07002265 struct zspage *src_zspage;
2266 struct zspage *dst_zspage = NULL;
Rokudo Yan23959282021-02-25 17:18:31 -08002267 unsigned long pages_freed = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07002268
Nhat Phamc0547d02022-11-28 11:16:12 -08002269 /*
2270 * protect the race between zpage migration and zs_free
2271 * as well as zpage allocation/free
2272 */
2273 spin_lock(&pool->lock);
Minchan Kim37836892016-07-26 15:23:23 -07002274 while ((src_zspage = isolate_zspage(class, true))) {
Minchan Kimb475d422022-01-21 22:14:13 -08002275 /* protect someone accessing the zspage(i.e., zs_map_object) */
2276 migrate_write_lock(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002277
Sergey Senozhatsky04f05902015-09-08 15:04:30 -07002278 if (!zs_can_compact(class))
2279 break;
2280
Ganesh Mahendran41b88e12016-07-28 15:47:43 -07002281 cc.obj_idx = 0;
Minchan Kim48b48002016-07-26 15:23:31 -07002282 cc.s_page = get_first_page(src_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002283
Minchan Kim37836892016-07-26 15:23:23 -07002284 while ((dst_zspage = isolate_zspage(class, false))) {
Minchan Kimb475d422022-01-21 22:14:13 -08002285 migrate_write_lock_nested(dst_zspage);
2286
Minchan Kim48b48002016-07-26 15:23:31 -07002287 cc.d_page = get_first_page(dst_zspage);
Minchan Kim312fcae2015-04-15 16:15:30 -07002288 /*
Sergey Senozhatsky0dc63d482015-09-08 15:04:33 -07002289 * If there is no more space in dst_page, resched
2290 * and see if anyone had allocated another zspage.
Minchan Kim312fcae2015-04-15 16:15:30 -07002291 */
2292 if (!migrate_zspage(pool, class, &cc))
2293 break;
2294
Minchan Kim4aa409c2016-07-26 15:23:26 -07002295 putback_zspage(class, dst_zspage);
Minchan Kimb475d422022-01-21 22:14:13 -08002296 migrate_write_unlock(dst_zspage);
2297 dst_zspage = NULL;
Nhat Phamc0547d02022-11-28 11:16:12 -08002298 if (spin_is_contended(&pool->lock))
Minchan Kimb475d422022-01-21 22:14:13 -08002299 break;
Minchan Kim312fcae2015-04-15 16:15:30 -07002300 }
2301
2302 /* Stop if we couldn't find slot */
Minchan Kim37836892016-07-26 15:23:23 -07002303 if (dst_zspage == NULL)
Minchan Kim312fcae2015-04-15 16:15:30 -07002304 break;
2305
Minchan Kim4aa409c2016-07-26 15:23:26 -07002306 putback_zspage(class, dst_zspage);
Minchan Kimb475d422022-01-21 22:14:13 -08002307 migrate_write_unlock(dst_zspage);
2308
Minchan Kim4aa409c2016-07-26 15:23:26 -07002309 if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
Minchan Kimb475d422022-01-21 22:14:13 -08002310 migrate_write_unlock(src_zspage);
Minchan Kim48b48002016-07-26 15:23:31 -07002311 free_zspage(pool, class, src_zspage);
Rokudo Yan23959282021-02-25 17:18:31 -08002312 pages_freed += class->pages_per_zspage;
Minchan Kimb475d422022-01-21 22:14:13 -08002313 } else
2314 migrate_write_unlock(src_zspage);
Nhat Phamc0547d02022-11-28 11:16:12 -08002315 spin_unlock(&pool->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002316 cond_resched();
Nhat Phamc0547d02022-11-28 11:16:12 -08002317 spin_lock(&pool->lock);
Minchan Kim312fcae2015-04-15 16:15:30 -07002318 }
2319
Minchan Kimb475d422022-01-21 22:14:13 -08002320 if (src_zspage) {
Minchan Kim4aa409c2016-07-26 15:23:26 -07002321 putback_zspage(class, src_zspage);
Minchan Kimb475d422022-01-21 22:14:13 -08002322 migrate_write_unlock(src_zspage);
2323 }
Minchan Kim312fcae2015-04-15 16:15:30 -07002324
Nhat Phamc0547d02022-11-28 11:16:12 -08002325 spin_unlock(&pool->lock);
Rokudo Yan23959282021-02-25 17:18:31 -08002326
2327 return pages_freed;
Minchan Kim312fcae2015-04-15 16:15:30 -07002328}
2329
2330unsigned long zs_compact(struct zs_pool *pool)
2331{
2332 int i;
Minchan Kim312fcae2015-04-15 16:15:30 -07002333 struct size_class *class;
Rokudo Yan23959282021-02-25 17:18:31 -08002334 unsigned long pages_freed = 0;
Minchan Kim312fcae2015-04-15 16:15:30 -07002335
Jerome Marchandcf8e0fed2017-07-10 15:50:18 -07002336 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Minchan Kim312fcae2015-04-15 16:15:30 -07002337 class = pool->size_class[i];
Minchan Kim312fcae2015-04-15 16:15:30 -07002338 if (class->index != i)
2339 continue;
Rokudo Yan23959282021-02-25 17:18:31 -08002340 pages_freed += __zs_compact(pool, class);
Minchan Kim312fcae2015-04-15 16:15:30 -07002341 }
Rokudo Yan23959282021-02-25 17:18:31 -08002342 atomic_long_add(pages_freed, &pool->stats.pages_compacted);
Minchan Kim312fcae2015-04-15 16:15:30 -07002343
Rokudo Yan23959282021-02-25 17:18:31 -08002344 return pages_freed;
Minchan Kim312fcae2015-04-15 16:15:30 -07002345}
2346EXPORT_SYMBOL_GPL(zs_compact);
2347
Sergey Senozhatsky7d3f3932015-09-08 15:04:35 -07002348void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
2349{
2350 memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
2351}
2352EXPORT_SYMBOL_GPL(zs_pool_stats);
2353
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002354static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
2355 struct shrink_control *sc)
2356{
2357 unsigned long pages_freed;
2358 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2359 shrinker);
2360
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002361 /*
2362 * Compact classes and calculate compaction delta.
2363 * Can run concurrently with a manually triggered
2364 * (by user) compaction.
2365 */
Rokudo Yan23959282021-02-25 17:18:31 -08002366 pages_freed = zs_compact(pool);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002367
2368 return pages_freed ? pages_freed : SHRINK_STOP;
2369}
2370
2371static unsigned long zs_shrinker_count(struct shrinker *shrinker,
2372 struct shrink_control *sc)
2373{
2374 int i;
2375 struct size_class *class;
2376 unsigned long pages_to_free = 0;
2377 struct zs_pool *pool = container_of(shrinker, struct zs_pool,
2378 shrinker);
2379
Jerome Marchandcf8e0fed2017-07-10 15:50:18 -07002380 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002381 class = pool->size_class[i];
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002382 if (class->index != i)
2383 continue;
2384
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002385 pages_to_free += zs_can_compact(class);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002386 }
2387
2388 return pages_to_free;
2389}
2390
2391static void zs_unregister_shrinker(struct zs_pool *pool)
2392{
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -08002393 unregister_shrinker(&pool->shrinker);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002394}
2395
2396static int zs_register_shrinker(struct zs_pool *pool)
2397{
2398 pool->shrinker.scan_objects = zs_shrinker_scan;
2399 pool->shrinker.count_objects = zs_shrinker_count;
2400 pool->shrinker.batch = 0;
2401 pool->shrinker.seeks = DEFAULT_SEEKS;
2402
Roman Gushchine33c2672022-05-31 20:22:24 -07002403 return register_shrinker(&pool->shrinker, "mm-zspool:%s",
2404 pool->name);
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002405}
2406
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002407/**
2408 * zs_create_pool - Creates an allocation pool to work from.
Ganesh Mahendranfd854462016-07-28 15:47:54 -07002409 * @name: pool name to be created
Davidlohr Bueso4bbc0bc2013-01-04 12:14:00 -08002410 *
2411 * This function must be called before anything when using
2412 * the zsmalloc allocator.
2413 *
2414 * On success, a pointer to the newly created pool is returned,
2415 * otherwise NULL.
2416 */
Sergey Senozhatskyd0d8da22016-05-20 16:59:48 -07002417struct zs_pool *zs_create_pool(const char *name)
Nitin Gupta61989a82012-01-09 16:51:56 -06002418{
Ganesh Mahendran18136652014-12-12 16:57:10 -08002419 int i;
Nitin Gupta61989a82012-01-09 16:51:56 -06002420 struct zs_pool *pool;
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002421 struct size_class *prev_class = NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002422
Ganesh Mahendran18136652014-12-12 16:57:10 -08002423 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
Nitin Gupta61989a82012-01-09 16:51:56 -06002424 if (!pool)
2425 return NULL;
2426
Minchan Kim48b48002016-07-26 15:23:31 -07002427 init_deferred_free(pool);
Nhat Phamc0547d02022-11-28 11:16:12 -08002428 spin_lock_init(&pool->lock);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002429
Minchan Kim2e40e162015-04-15 16:15:23 -07002430 pool->name = kstrdup(name, GFP_KERNEL);
2431 if (!pool->name)
2432 goto err;
2433
Minchan Kim37836892016-07-26 15:23:23 -07002434 if (create_cache(pool))
Minchan Kim2e40e162015-04-15 16:15:23 -07002435 goto err;
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002436
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002437 /*
Xishi Qiu399d8ee2017-02-22 15:45:01 -08002438 * Iterate reversely, because, size of size_class that we want to use
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002439 * for merging should be larger or equal to current size.
2440 */
Jerome Marchandcf8e0fed2017-07-10 15:50:18 -07002441 for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002442 int size;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002443 int pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002444 int objs_per_zspage;
Nitin Gupta61989a82012-01-09 16:51:56 -06002445 struct size_class *class;
Minchan Kim37836892016-07-26 15:23:23 -07002446 int fullness = 0;
Nitin Gupta61989a82012-01-09 16:51:56 -06002447
2448 size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
2449 if (size > ZS_MAX_ALLOC_SIZE)
2450 size = ZS_MAX_ALLOC_SIZE;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002451 pages_per_zspage = get_pages_per_zspage(size);
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002452 objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
Nitin Gupta61989a82012-01-09 16:51:56 -06002453
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002454 /*
Sergey Senozhatsky010b4952018-04-05 16:24:43 -07002455 * We iterate from biggest down to smallest classes,
2456 * so huge_class_size holds the size of the first huge
2457 * class. Any object bigger than or equal to that will
2458 * endup in the huge class.
2459 */
2460 if (pages_per_zspage != 1 && objs_per_zspage != 1 &&
2461 !huge_class_size) {
2462 huge_class_size = size;
2463 /*
2464 * The object uses ZS_HANDLE_SIZE bytes to store the
2465 * handle. We need to subtract it, because zs_malloc()
2466 * unconditionally adds handle size before it performs
2467 * size class search - so object may be smaller than
2468 * huge class size, yet it still can end up in the huge
2469 * class because it grows by ZS_HANDLE_SIZE extra bytes
2470 * right before class lookup.
2471 */
2472 huge_class_size -= (ZS_HANDLE_SIZE - 1);
2473 }
2474
2475 /*
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002476 * size_class is used for normal zsmalloc operation such
2477 * as alloc/free for that size. Although it is natural that we
2478 * have one size_class for each size, there is a chance that we
2479 * can get more memory utilization if we use one size_class for
2480 * many different sizes whose size_class have same
2481 * characteristics. So, we makes size_class point to
2482 * previous size_class if possible.
2483 */
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002484 if (prev_class) {
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002485 if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002486 pool->size_class[i] = prev_class;
2487 continue;
2488 }
2489 }
2490
2491 class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
2492 if (!class)
2493 goto err;
2494
Nitin Gupta61989a82012-01-09 16:51:56 -06002495 class->size = size;
2496 class->index = i;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002497 class->pages_per_zspage = pages_per_zspage;
Ganesh Mahendran64d90462016-07-28 15:47:51 -07002498 class->objs_per_zspage = objs_per_zspage;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002499 pool->size_class[i] = class;
Minchan Kim48b48002016-07-26 15:23:31 -07002500 for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
2501 fullness++)
Minchan Kim37836892016-07-26 15:23:23 -07002502 INIT_LIST_HEAD(&class->fullness_list[fullness]);
Ganesh Mahendrandf8b5bb2014-12-12 16:57:07 -08002503
2504 prev_class = class;
Nitin Gupta61989a82012-01-09 16:51:56 -06002505 }
2506
Dan Streetmand34f6152016-05-20 16:59:56 -07002507 /* debug only, don't abort if it fails */
2508 zs_pool_stat_create(pool, name);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002509
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002510 /*
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -08002511 * Not critical since shrinker is only used to trigger internal
2512 * defragmentation of the pool which is pretty optional thing. If
2513 * registration fails we still can use the pool normally and user can
2514 * trigger compaction manually. Thus, ignore return code.
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002515 */
Aliaksei Karaliou93144ca2018-01-31 16:18:40 -08002516 zs_register_shrinker(pool);
2517
Nhat Pham64f768c2022-11-28 11:16:13 -08002518#ifdef CONFIG_ZPOOL
2519 INIT_LIST_HEAD(&pool->lru);
2520#endif
2521
Nitin Gupta61989a82012-01-09 16:51:56 -06002522 return pool;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002523
2524err:
2525 zs_destroy_pool(pool);
2526 return NULL;
Nitin Gupta61989a82012-01-09 16:51:56 -06002527}
2528EXPORT_SYMBOL_GPL(zs_create_pool);
2529
2530void zs_destroy_pool(struct zs_pool *pool)
2531{
2532 int i;
2533
Sergey Senozhatskyab9d3062015-09-08 15:04:41 -07002534 zs_unregister_shrinker(pool);
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -04002535 zs_flush_migration(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002536 zs_pool_stat_destroy(pool);
2537
Jerome Marchandcf8e0fed2017-07-10 15:50:18 -07002538 for (i = 0; i < ZS_SIZE_CLASSES; i++) {
Nitin Gupta61989a82012-01-09 16:51:56 -06002539 int fg;
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002540 struct size_class *class = pool->size_class[i];
2541
Alexey Romanov4249a052022-10-13 14:28:25 +03002542 if (!class)
2543 continue;
2544
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002545 if (class->index != i)
2546 continue;
Nitin Gupta61989a82012-01-09 16:51:56 -06002547
Minchan Kim48b48002016-07-26 15:23:31 -07002548 for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
Minchan Kim37836892016-07-26 15:23:23 -07002549 if (!list_empty(&class->fullness_list[fg])) {
Marlies Ruck93ad5ab2013-05-15 16:56:49 -04002550 pr_info("Freeing non-empty class with size %db, fullness group %d\n",
Nitin Gupta61989a82012-01-09 16:51:56 -06002551 class->size, fg);
2552 }
2553 }
Joonsoo Kim9eec4cd2014-12-12 16:56:44 -08002554 kfree(class);
Nitin Gupta61989a82012-01-09 16:51:56 -06002555 }
Mahendran Ganesh40f9fb82014-12-12 16:57:01 -08002556
Minchan Kim37836892016-07-26 15:23:23 -07002557 destroy_cache(pool);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002558 kfree(pool->name);
Nitin Gupta61989a82012-01-09 16:51:56 -06002559 kfree(pool);
2560}
2561EXPORT_SYMBOL_GPL(zs_destroy_pool);
2562
Nhat Pham9997bc02022-11-28 11:16:15 -08002563#ifdef CONFIG_ZPOOL
Nhat Pham85b32582023-01-10 15:17:01 -08002564static void restore_freelist(struct zs_pool *pool, struct size_class *class,
2565 struct zspage *zspage)
2566{
2567 unsigned int obj_idx = 0;
2568 unsigned long handle, off = 0; /* off is within-page offset */
2569 struct page *page = get_first_page(zspage);
2570 struct link_free *prev_free = NULL;
2571 void *prev_page_vaddr = NULL;
2572
2573 /* in case no free object found */
2574 set_freeobj(zspage, (unsigned int)(-1UL));
2575
2576 while (page) {
2577 void *vaddr = kmap_atomic(page);
2578 struct page *next_page;
2579
2580 while (off < PAGE_SIZE) {
2581 void *obj_addr = vaddr + off;
2582
2583 /* skip allocated object */
2584 if (obj_allocated(page, obj_addr, &handle)) {
2585 obj_idx++;
2586 off += class->size;
2587 continue;
2588 }
2589
2590 /* free deferred handle from reclaim attempt */
2591 if (obj_stores_deferred_handle(page, obj_addr, &handle))
2592 cache_free_handle(pool, handle);
2593
2594 if (prev_free)
2595 prev_free->next = obj_idx << OBJ_TAG_BITS;
2596 else /* first free object found */
2597 set_freeobj(zspage, obj_idx);
2598
2599 prev_free = (struct link_free *)vaddr + off / sizeof(*prev_free);
2600 /* if last free object in a previous page, need to unmap */
2601 if (prev_page_vaddr) {
2602 kunmap_atomic(prev_page_vaddr);
2603 prev_page_vaddr = NULL;
2604 }
2605
2606 obj_idx++;
2607 off += class->size;
2608 }
2609
2610 /*
2611 * Handle the last (full or partial) object on this page.
2612 */
2613 next_page = get_next_page(page);
2614 if (next_page) {
2615 if (!prev_free || prev_page_vaddr) {
2616 /*
2617 * There is no free object in this page, so we can safely
2618 * unmap it.
2619 */
2620 kunmap_atomic(vaddr);
2621 } else {
2622 /* update prev_page_vaddr since prev_free is on this page */
2623 prev_page_vaddr = vaddr;
2624 }
2625 } else { /* this is the last page */
2626 if (prev_free) {
2627 /*
2628 * Reset OBJ_TAG_BITS bit to last link to tell
2629 * whether it's allocated object or not.
2630 */
2631 prev_free->next = -1UL << OBJ_TAG_BITS;
2632 }
2633
2634 /* unmap previous page (if not done yet) */
2635 if (prev_page_vaddr) {
2636 kunmap_atomic(prev_page_vaddr);
2637 prev_page_vaddr = NULL;
2638 }
2639
2640 kunmap_atomic(vaddr);
2641 }
2642
2643 page = next_page;
2644 off %= PAGE_SIZE;
2645 }
2646}
2647
Nhat Pham9997bc02022-11-28 11:16:15 -08002648static int zs_reclaim_page(struct zs_pool *pool, unsigned int retries)
2649{
2650 int i, obj_idx, ret = 0;
2651 unsigned long handle;
2652 struct zspage *zspage;
2653 struct page *page;
2654 enum fullness_group fullness;
2655
2656 /* Lock LRU and fullness list */
2657 spin_lock(&pool->lock);
2658 if (list_empty(&pool->lru)) {
2659 spin_unlock(&pool->lock);
2660 return -EINVAL;
2661 }
2662
2663 for (i = 0; i < retries; i++) {
2664 struct size_class *class;
2665
2666 zspage = list_last_entry(&pool->lru, struct zspage, lru);
2667 list_del(&zspage->lru);
2668
2669 /* zs_free may free objects, but not the zspage and handles */
2670 zspage->under_reclaim = true;
2671
2672 class = zspage_class(pool, zspage);
2673 fullness = get_fullness_group(class, zspage);
2674
2675 /* Lock out object allocations and object compaction */
2676 remove_zspage(class, zspage, fullness);
2677
2678 spin_unlock(&pool->lock);
2679 cond_resched();
2680
2681 /* Lock backing pages into place */
2682 lock_zspage(zspage);
2683
2684 obj_idx = 0;
2685 page = get_first_page(zspage);
2686 while (1) {
2687 handle = find_alloced_obj(class, page, &obj_idx);
2688 if (!handle) {
2689 page = get_next_page(page);
2690 if (!page)
2691 break;
2692 obj_idx = 0;
2693 continue;
2694 }
2695
2696 /*
2697 * This will write the object and call zs_free.
2698 *
2699 * zs_free will free the object, but the
2700 * under_reclaim flag prevents it from freeing
2701 * the zspage altogether. This is necessary so
2702 * that we can continue working with the
2703 * zspage potentially after the last object
2704 * has been freed.
2705 */
2706 ret = pool->zpool_ops->evict(pool->zpool, handle);
2707 if (ret)
2708 goto next;
2709
2710 obj_idx++;
2711 }
2712
2713next:
2714 /* For freeing the zspage, or putting it back in the pool and LRU list. */
2715 spin_lock(&pool->lock);
2716 zspage->under_reclaim = false;
2717
2718 if (!get_zspage_inuse(zspage)) {
2719 /*
2720 * Fullness went stale as zs_free() won't touch it
2721 * while the page is removed from the pool. Fix it
2722 * up for the check in __free_zspage().
2723 */
2724 zspage->fullness = ZS_EMPTY;
2725
2726 __free_zspage(pool, class, zspage);
2727 spin_unlock(&pool->lock);
2728 return 0;
2729 }
2730
Nhat Pham85b32582023-01-10 15:17:01 -08002731 /*
2732 * Eviction fails on one of the handles, so we need to restore zspage.
2733 * We need to rebuild its freelist (and free stored deferred handles),
2734 * put it back to the correct size class, and add it to the LRU list.
2735 */
2736 restore_freelist(pool, class, zspage);
Nhat Pham9997bc02022-11-28 11:16:15 -08002737 putback_zspage(class, zspage);
2738 list_add(&zspage->lru, &pool->lru);
2739 unlock_zspage(zspage);
2740 }
2741
2742 spin_unlock(&pool->lock);
2743 return -EAGAIN;
2744}
2745#endif /* CONFIG_ZPOOL */
2746
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002747static int __init zs_init(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002748{
Minchan Kim48b48002016-07-26 15:23:31 -07002749 int ret;
2750
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002751 ret = cpuhp_setup_state(CPUHP_MM_ZS_PREPARE, "mm/zsmalloc:prepare",
2752 zs_cpu_prepare, zs_cpu_dead);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002753 if (ret)
Matthew Wilcox (Oracle)68f27362022-06-07 15:38:48 -04002754 goto out;
Nitin Gupta61989a82012-01-09 16:51:56 -06002755
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002756#ifdef CONFIG_ZPOOL
2757 zpool_register_driver(&zs_zpool_driver);
2758#endif
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002759
Dan Streetman4abaac92016-05-26 15:16:27 -07002760 zs_stat_init();
2761
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002762 return 0;
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002763
Minchan Kim48b48002016-07-26 15:23:31 -07002764out:
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002765 return ret;
Nitin Gupta61989a82012-01-09 16:51:56 -06002766}
Nitin Gupta61989a82012-01-09 16:51:56 -06002767
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002768static void __exit zs_exit(void)
Nitin Gupta61989a82012-01-09 16:51:56 -06002769{
Ganesh Mahendran66cdef62014-12-18 16:17:40 -08002770#ifdef CONFIG_ZPOOL
2771 zpool_unregister_driver(&zs_zpool_driver);
2772#endif
Sebastian Andrzej Siewior215c89d2016-11-27 00:13:38 +01002773 cpuhp_remove_state(CPUHP_MM_ZS_PREPARE);
Ganesh Mahendran0f050d92015-02-12 15:00:54 -08002774
2775 zs_stat_exit();
Nitin Gupta61989a82012-01-09 16:51:56 -06002776}
Ben Hutchings069f1012012-06-20 02:31:11 +01002777
2778module_init(zs_init);
2779module_exit(zs_exit);
2780
2781MODULE_LICENSE("Dual BSD/GPL");
2782MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");