Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * zbud.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2013, Seth Jennings, IBM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Concepts based on zcache internal zbud allocator by Dan Magenheimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * zbud is an special purpose allocator for storing compressed pages.  Contrary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * to what its name may suggest, zbud is not a buddy allocator, but rather an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * allocator that "buddies" two compressed pages together in a single memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * While this design limits storage density, it has simple and deterministic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * reclaim properties that make it preferable to a higher density approach when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * reclaim will be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * zbud works by storing compressed pages, or "zpages", together in pairs in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * single memory page called a "zbud page".  The first buddy is "left
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * justified" at the beginning of the zbud page, and the last buddy is "right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * justified" at the end of the zbud page.  The benefit is that if either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * buddy is freed, the freed buddy space, coalesced with whatever slack space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * that existed between the buddies, results in the largest possible free region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * within the zbud page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * zbud also provides an attractive lower bound on density. The ratio of zpages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * to zbud pages can not be less than 1.  This ensures that zbud can never "do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * harm" by using more pages to store zpages than the uncompressed zpages would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * have used on their own.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * zbud pages are divided into "chunks".  The size of the chunks is fixed at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * compile time and determined by NCHUNKS_ORDER below.  Dividing zbud pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * into chunks allows organizing unbuddied zbud pages into a manageable number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * of unbuddied lists according to the number of free chunks available in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * zbud page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * The zbud API differs from that of conventional allocators in that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * allocation function, zbud_alloc(), returns an opaque handle to the user,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * not a dereferenceable pointer.  The user must map the handle using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * zbud_map() in order to get a usable pointer by which to access the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * allocation data and unmap the handle with zbud_unmap() when operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * on the allocation data are complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #include <linux/preempt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #include <linux/zbud.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #include <linux/zpool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) /*****************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * Structures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) *****************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * NCHUNKS_ORDER determines the internal allocation granularity, effectively
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * adjusting internal fragmentation.  It also determines the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * freelists maintained in each pool. NCHUNKS_ORDER of 6 means that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * allocation granularity will be in chunks of size PAGE_SIZE/64. As one chunk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * in allocated page is occupied by zbud header, NCHUNKS will be calculated to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * 63 which shows the max number of free chunks in zbud page, also there will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * 63 freelists per pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #define NCHUNKS_ORDER	6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define CHUNK_SHIFT	(PAGE_SHIFT - NCHUNKS_ORDER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #define CHUNK_SIZE	(1 << CHUNK_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #define ZHDR_SIZE_ALIGNED CHUNK_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #define NCHUNKS		((PAGE_SIZE - ZHDR_SIZE_ALIGNED) >> CHUNK_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * struct zbud_pool - stores metadata for each zbud pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * @lock:	protects all pool fields and first|last_chunk fields of any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  *		zbud page in the pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  * @unbuddied:	array of lists tracking zbud pages that only contain one buddy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  *		the lists each zbud page is added to depends on the size of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  *		its free region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * @buddied:	list tracking the zbud pages that contain two buddies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  *		these zbud pages are full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * @lru:	list tracking the zbud pages in LRU order by most recently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  *		added buddy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * @pages_nr:	number of zbud pages in the pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * @ops:	pointer to a structure of user defined operations specified at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  *		pool creation time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * This structure is allocated at pool creation time and maintains metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * pertaining to a particular zbud pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) struct zbud_pool {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct list_head unbuddied[NCHUNKS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	struct list_head buddied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct list_head lru;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	u64 pages_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	const struct zbud_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #ifdef CONFIG_ZPOOL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	struct zpool *zpool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	const struct zpool_ops *zpool_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  * struct zbud_header - zbud page metadata occupying the first chunk of each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  *			zbud page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * @buddy:	links the zbud page into the unbuddied/buddied lists in the pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  * @lru:	links the zbud page into the lru list in the pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * @first_chunks:	the size of the first buddy in chunks, 0 if free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * @last_chunks:	the size of the last buddy in chunks, 0 if free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct zbud_header {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct list_head buddy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct list_head lru;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	unsigned int first_chunks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	unsigned int last_chunks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	bool under_reclaim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /*****************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  * zpool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  ****************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #ifdef CONFIG_ZPOOL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static int zbud_zpool_evict(struct zbud_pool *pool, unsigned long handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (pool->zpool && pool->zpool_ops && pool->zpool_ops->evict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return pool->zpool_ops->evict(pool->zpool, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static const struct zbud_ops zbud_zpool_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	.evict =	zbud_zpool_evict
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static void *zbud_zpool_create(const char *name, gfp_t gfp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			       const struct zpool_ops *zpool_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			       struct zpool *zpool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	struct zbud_pool *pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	pool = zbud_create_pool(gfp, zpool_ops ? &zbud_zpool_ops : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (pool) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		pool->zpool = zpool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		pool->zpool_ops = zpool_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static void zbud_zpool_destroy(void *pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	zbud_destroy_pool(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int zbud_zpool_malloc(void *pool, size_t size, gfp_t gfp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			unsigned long *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return zbud_alloc(pool, size, gfp, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static void zbud_zpool_free(void *pool, unsigned long handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	zbud_free(pool, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static int zbud_zpool_shrink(void *pool, unsigned int pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			unsigned int *reclaimed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	unsigned int total = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	while (total < pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		ret = zbud_reclaim_page(pool, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		total++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (reclaimed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		*reclaimed = total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static void *zbud_zpool_map(void *pool, unsigned long handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			enum zpool_mapmode mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	return zbud_map(pool, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static void zbud_zpool_unmap(void *pool, unsigned long handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	zbud_unmap(pool, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static u64 zbud_zpool_total_size(void *pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	return zbud_get_pool_size(pool) * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static struct zpool_driver zbud_zpool_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	.type =		"zbud",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	.owner =	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	.create =	zbud_zpool_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	.destroy =	zbud_zpool_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	.malloc =	zbud_zpool_malloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	.free =		zbud_zpool_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	.shrink =	zbud_zpool_shrink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	.map =		zbud_zpool_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	.unmap =	zbud_zpool_unmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	.total_size =	zbud_zpool_total_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) MODULE_ALIAS("zpool-zbud");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) #endif /* CONFIG_ZPOOL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /*****************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  * Helpers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) *****************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /* Just to make the code easier to read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) enum buddy {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	FIRST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	LAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /* Converts an allocation size in bytes to size in zbud chunks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static int size_to_chunks(size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	return (size + CHUNK_SIZE - 1) >> CHUNK_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) #define for_each_unbuddied_list(_iter, _begin) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	for ((_iter) = (_begin); (_iter) < NCHUNKS; (_iter)++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /* Initializes the zbud header of a newly allocated zbud page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static struct zbud_header *init_zbud_page(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct zbud_header *zhdr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	zhdr->first_chunks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	zhdr->last_chunks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	INIT_LIST_HEAD(&zhdr->buddy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	INIT_LIST_HEAD(&zhdr->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	zhdr->under_reclaim = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return zhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /* Resets the struct page fields and frees the page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static void free_zbud_page(struct zbud_header *zhdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	__free_page(virt_to_page(zhdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  * Encodes the handle of a particular buddy within a zbud page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  * Pool lock should be held as this function accesses first|last_chunks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static unsigned long encode_handle(struct zbud_header *zhdr, enum buddy bud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	unsigned long handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	 * For now, the encoded handle is actually just the pointer to the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	 * but this might not always be the case.  A little information hiding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	 * Add CHUNK_SIZE to the handle if it is the first allocation to jump
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	 * over the zbud header in the first chunk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	handle = (unsigned long)zhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (bud == FIRST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		/* skip over zbud header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		handle += ZHDR_SIZE_ALIGNED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	else /* bud == LAST */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		handle += PAGE_SIZE - (zhdr->last_chunks  << CHUNK_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	return handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /* Returns the zbud page where a given handle is stored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static struct zbud_header *handle_to_zbud_header(unsigned long handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	return (struct zbud_header *)(handle & PAGE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /* Returns the number of free chunks in a zbud page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static int num_free_chunks(struct zbud_header *zhdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	 * Rather than branch for different situations, just use the fact that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	 * free buddies have a length of zero to simplify everything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	return NCHUNKS - zhdr->first_chunks - zhdr->last_chunks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) /*****************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  * API Functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) *****************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)  * zbud_create_pool() - create a new zbud pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  * @gfp:	gfp flags when allocating the zbud pool structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)  * @ops:	user-defined operations for the zbud pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)  * Return: pointer to the new zbud pool or NULL if the metadata allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)  * failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct zbud_pool *zbud_create_pool(gfp_t gfp, const struct zbud_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	struct zbud_pool *pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	pool = kzalloc(sizeof(struct zbud_pool), gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (!pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	spin_lock_init(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	for_each_unbuddied_list(i, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		INIT_LIST_HEAD(&pool->unbuddied[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	INIT_LIST_HEAD(&pool->buddied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	INIT_LIST_HEAD(&pool->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	pool->pages_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	pool->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)  * zbud_destroy_pool() - destroys an existing zbud pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)  * @pool:	the zbud pool to be destroyed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)  * The pool should be emptied before this function is called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) void zbud_destroy_pool(struct zbud_pool *pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	kfree(pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)  * zbud_alloc() - allocates a region of a given size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)  * @pool:	zbud pool from which to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)  * @size:	size in bytes of the desired allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)  * @gfp:	gfp flags used if the pool needs to grow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)  * @handle:	handle of the new allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  * This function will attempt to find a free region in the pool large enough to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  * satisfy the allocation request.  A search of the unbuddied lists is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * performed first. If no suitable free region is found, then a new page is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  * allocated and added to the pool to satisfy the request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)  * gfp should not set __GFP_HIGHMEM as highmem pages cannot be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)  * as zbud pool pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)  * Return: 0 if success and handle is set, otherwise -EINVAL if the size or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)  * gfp arguments are invalid or -ENOMEM if the pool was unable to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  * a new page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) int zbud_alloc(struct zbud_pool *pool, size_t size, gfp_t gfp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			unsigned long *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	int chunks, i, freechunks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	struct zbud_header *zhdr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	enum buddy bud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (!size || (gfp & __GFP_HIGHMEM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	if (size > PAGE_SIZE - ZHDR_SIZE_ALIGNED - CHUNK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	chunks = size_to_chunks(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	spin_lock(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	/* First, try to find an unbuddied zbud page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	for_each_unbuddied_list(i, chunks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		if (!list_empty(&pool->unbuddied[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			zhdr = list_first_entry(&pool->unbuddied[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 					struct zbud_header, buddy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			list_del(&zhdr->buddy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			if (zhdr->first_chunks == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 				bud = FIRST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 				bud = LAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	/* Couldn't find unbuddied zbud page, create new one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	spin_unlock(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	page = alloc_page(gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	spin_lock(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	pool->pages_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	zhdr = init_zbud_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	bud = FIRST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	if (bud == FIRST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		zhdr->first_chunks = chunks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		zhdr->last_chunks = chunks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (zhdr->first_chunks == 0 || zhdr->last_chunks == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		/* Add to unbuddied list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		freechunks = num_free_chunks(zhdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		list_add(&zhdr->buddy, &pool->unbuddied[freechunks]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		/* Add to buddied list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		list_add(&zhdr->buddy, &pool->buddied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	/* Add/move zbud page to beginning of LRU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	if (!list_empty(&zhdr->lru))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		list_del(&zhdr->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	list_add(&zhdr->lru, &pool->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	*handle = encode_handle(zhdr, bud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	spin_unlock(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)  * zbud_free() - frees the allocation associated with the given handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)  * @pool:	pool in which the allocation resided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)  * @handle:	handle associated with the allocation returned by zbud_alloc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)  * In the case that the zbud page in which the allocation resides is under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)  * reclaim, as indicated by the PG_reclaim flag being set, this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)  * only sets the first|last_chunks to 0.  The page is actually freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)  * once both buddies are evicted (see zbud_reclaim_page() below).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) void zbud_free(struct zbud_pool *pool, unsigned long handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	struct zbud_header *zhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	int freechunks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	spin_lock(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	zhdr = handle_to_zbud_header(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	/* If first buddy, handle will be page aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	if ((handle - ZHDR_SIZE_ALIGNED) & ~PAGE_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		zhdr->last_chunks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		zhdr->first_chunks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (zhdr->under_reclaim) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		/* zbud page is under reclaim, reclaim will free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		spin_unlock(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	/* Remove from existing buddy list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	list_del(&zhdr->buddy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	if (zhdr->first_chunks == 0 && zhdr->last_chunks == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		/* zbud page is empty, free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		list_del(&zhdr->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		free_zbud_page(zhdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		pool->pages_nr--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		/* Add to unbuddied list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		freechunks = num_free_chunks(zhdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		list_add(&zhdr->buddy, &pool->unbuddied[freechunks]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	spin_unlock(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)  * zbud_reclaim_page() - evicts allocations from a pool page and frees it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)  * @pool:	pool from which a page will attempt to be evicted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)  * @retries:	number of pages on the LRU list for which eviction will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)  *		be attempted before failing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)  * zbud reclaim is different from normal system reclaim in that the reclaim is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)  * done from the bottom, up.  This is because only the bottom layer, zbud, has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)  * information on how the allocations are organized within each zbud page. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)  * has the potential to create interesting locking situations between zbud and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)  * the user, however.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)  * To avoid these, this is how zbud_reclaim_page() should be called:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)  * The user detects a page should be reclaimed and calls zbud_reclaim_page().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)  * zbud_reclaim_page() will remove a zbud page from the pool LRU list and call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)  * the user-defined eviction handler with the pool and handle as arguments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)  * If the handle can not be evicted, the eviction handler should return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)  * non-zero. zbud_reclaim_page() will add the zbud page back to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)  * appropriate list and try the next zbud page on the LRU up to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)  * a user defined number of retries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)  * If the handle is successfully evicted, the eviction handler should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)  * return 0 _and_ should have called zbud_free() on the handle. zbud_free()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)  * contains logic to delay freeing the page if the page is under reclaim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)  * as indicated by the setting of the PG_reclaim flag on the underlying page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)  * If all buddies in the zbud page are successfully evicted, then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)  * zbud page can be freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)  * Returns: 0 if page is successfully freed, otherwise -EINVAL if there are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)  * no pages to evict or an eviction handler is not registered, -EAGAIN if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)  * the retry limit was hit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) int zbud_reclaim_page(struct zbud_pool *pool, unsigned int retries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	int i, ret, freechunks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	struct zbud_header *zhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	unsigned long first_handle = 0, last_handle = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	spin_lock(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	if (!pool->ops || !pool->ops->evict || list_empty(&pool->lru) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 			retries == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		spin_unlock(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	for (i = 0; i < retries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		zhdr = list_last_entry(&pool->lru, struct zbud_header, lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		list_del(&zhdr->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		list_del(&zhdr->buddy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		/* Protect zbud page against free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		zhdr->under_reclaim = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		 * We need encode the handles before unlocking, since we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		 * race with free that will set (first|last)_chunks to 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		first_handle = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		last_handle = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		if (zhdr->first_chunks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			first_handle = encode_handle(zhdr, FIRST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		if (zhdr->last_chunks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			last_handle = encode_handle(zhdr, LAST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		spin_unlock(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		/* Issue the eviction callback(s) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		if (first_handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 			ret = pool->ops->evict(pool, first_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 				goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		if (last_handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 			ret = pool->ops->evict(pool, last_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 				goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		spin_lock(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		zhdr->under_reclaim = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		if (zhdr->first_chunks == 0 && zhdr->last_chunks == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 			 * Both buddies are now free, free the zbud page and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 			 * return success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 			free_zbud_page(zhdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 			pool->pages_nr--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 			spin_unlock(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		} else if (zhdr->first_chunks == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 				zhdr->last_chunks == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 			/* add to unbuddied list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 			freechunks = num_free_chunks(zhdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			list_add(&zhdr->buddy, &pool->unbuddied[freechunks]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 			/* add to buddied list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 			list_add(&zhdr->buddy, &pool->buddied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		/* add to beginning of LRU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		list_add(&zhdr->lru, &pool->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	spin_unlock(&pool->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)  * zbud_map() - maps the allocation associated with the given handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)  * @pool:	pool in which the allocation resides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)  * @handle:	handle associated with the allocation to be mapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)  * While trivial for zbud, the mapping functions for others allocators
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)  * implementing this allocation API could have more complex information encoded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)  * in the handle and could create temporary mappings to make the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)  * accessible to the user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)  * Returns: a pointer to the mapped allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) void *zbud_map(struct zbud_pool *pool, unsigned long handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	return (void *)(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)  * zbud_unmap() - maps the allocation associated with the given handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)  * @pool:	pool in which the allocation resides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)  * @handle:	handle associated with the allocation to be unmapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) void zbud_unmap(struct zbud_pool *pool, unsigned long handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)  * zbud_get_pool_size() - gets the zbud pool size in pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)  * @pool:	pool whose size is being queried
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)  * Returns: size in pages of the given pool.  The pool lock need not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)  * taken to access pages_nr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) u64 zbud_get_pool_size(struct zbud_pool *pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	return pool->pages_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) static int __init init_zbud(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	/* Make sure the zbud header will fit in one chunk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	BUILD_BUG_ON(sizeof(struct zbud_header) > ZHDR_SIZE_ALIGNED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	pr_info("loaded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) #ifdef CONFIG_ZPOOL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	zpool_register_driver(&zbud_zpool_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) static void __exit exit_zbud(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) #ifdef CONFIG_ZPOOL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	zpool_unregister_driver(&zbud_zpool_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	pr_info("unloaded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) module_init(init_zbud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) module_exit(exit_zbud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) MODULE_DESCRIPTION("Buddy Allocator for Compressed Pages");