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)  * Frontswap frontend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * This code provides the generic "frontend" layer to call a matching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * "backend" driver implementation of frontswap.  See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Documentation/vm/frontswap.rst for more information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright (C) 2009-2012 Oracle Corp.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Author: Dan Magenheimer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/swapops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/frontswap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/swapfile.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * frontswap_ops are added by frontswap_register_ops, and provide the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * frontswap "backend" implementation functions.  Multiple implementations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * may be registered, but implementations can never deregister.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * is a simple singly-linked list of all registered implementations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static struct frontswap_ops *frontswap_ops __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define for_each_frontswap_ops(ops)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	for ((ops) = frontswap_ops; (ops); (ops) = (ops)->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * If enabled, frontswap_store will return failure even on success.  As
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * a result, the swap subsystem will always write the page to swap, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * effect converting frontswap into a writethrough cache.  In this mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * there is no direct reduction in swap writes, but a frontswap backend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * can unilaterally "reclaim" any pages in use with no data loss, thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * providing increases control over maximum memory usage due to frontswap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static bool frontswap_writethrough_enabled __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * If enabled, the underlying tmem implementation is capable of doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * exclusive gets, so frontswap_load, on a successful tmem_get must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * mark the page as no longer in frontswap AND mark it dirty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static bool frontswap_tmem_exclusive_gets_enabled __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #ifdef CONFIG_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * Counters available via /sys/kernel/debug/frontswap (if debugfs is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * properly configured).  These are for information only so are not protected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * against increment races.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static u64 frontswap_loads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static u64 frontswap_succ_stores;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static u64 frontswap_failed_stores;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static u64 frontswap_invalidates;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static inline void inc_frontswap_loads(void) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	data_race(frontswap_loads++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static inline void inc_frontswap_succ_stores(void) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	data_race(frontswap_succ_stores++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static inline void inc_frontswap_failed_stores(void) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	data_race(frontswap_failed_stores++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static inline void inc_frontswap_invalidates(void) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	data_race(frontswap_invalidates++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static inline void inc_frontswap_loads(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static inline void inc_frontswap_succ_stores(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static inline void inc_frontswap_failed_stores(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static inline void inc_frontswap_invalidates(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * Due to the asynchronous nature of the backends loading potentially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * _after_ the swap system has been activated, we have chokepoints
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * on all frontswap functions to not call the backend until the backend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * has registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * This would not guards us against the user deciding to call swapoff right as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * we are calling the backend to initialize (so swapon is in action).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * Fortunately for us, the swapon_mutex has been taken by the callee so we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * OK. The other scenario where calls to frontswap_store (called via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * swap_writepage) is racing with frontswap_invalidate_area (called via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * swapoff) is again guarded by the swap subsystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * While no backend is registered all calls to frontswap_[store|load|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  * invalidate_area|invalidate_page] are ignored or fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * The time between the backend being registered and the swap file system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * calling the backend (via the frontswap_* functions) is indeterminate as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  * frontswap_ops is not atomic_t (or a value guarded by a spinlock).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * That is OK as we are comfortable missing some of these calls to the newly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  * registered backend.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  * Obviously the opposite (unloading the backend) must be done after all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  * the frontswap_[store|load|invalidate_area|invalidate_page] start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  * ignoring or failing the requests.  However, there is currently no way
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  * to unload a backend once it is registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  * Register operations for frontswap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) void frontswap_register_ops(struct frontswap_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	DECLARE_BITMAP(a, MAX_SWAPFILES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	DECLARE_BITMAP(b, MAX_SWAPFILES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct swap_info_struct *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	bitmap_zero(a, MAX_SWAPFILES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	bitmap_zero(b, MAX_SWAPFILES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	plist_for_each_entry(si, &swap_active_head, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		if (!WARN_ON(!si->frontswap_map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			set_bit(si->type, a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/* the new ops needs to know the currently active swap devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	for_each_set_bit(i, a, MAX_SWAPFILES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		ops->init(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	 * Setting frontswap_ops must happen after the ops->init() calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	 * above; cmpxchg implies smp_mb() which will ensure the init is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	 * complete at this point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		ops->next = frontswap_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	} while (cmpxchg(&frontswap_ops, ops->next, ops) != ops->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	static_branch_inc(&frontswap_enabled_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	plist_for_each_entry(si, &swap_active_head, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		if (si->frontswap_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			set_bit(si->type, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	 * On the very unlikely chance that a swap device was added or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	 * removed between setting the "a" list bits and the ops init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 * calls, we re-check and do init or invalidate for any changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	 * bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (unlikely(!bitmap_equal(a, b, MAX_SWAPFILES))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		for (i = 0; i < MAX_SWAPFILES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			if (!test_bit(i, a) && test_bit(i, b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				ops->init(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			else if (test_bit(i, a) && !test_bit(i, b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				ops->invalidate_area(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) EXPORT_SYMBOL(frontswap_register_ops);
^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)  * Enable/disable frontswap writethrough (see above).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) void frontswap_writethrough(bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	frontswap_writethrough_enabled = enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) EXPORT_SYMBOL(frontswap_writethrough);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  * Enable/disable frontswap exclusive gets (see above).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) void frontswap_tmem_exclusive_gets(bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	frontswap_tmem_exclusive_gets_enabled = enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) EXPORT_SYMBOL(frontswap_tmem_exclusive_gets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  * Called when a swap device is swapon'd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) void __frontswap_init(unsigned type, unsigned long *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	struct swap_info_struct *sis = swap_info[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct frontswap_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	VM_BUG_ON(sis == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	 * p->frontswap is a bitmap that we MUST have to figure out which page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	 * has gone in frontswap. Without it there is no point of continuing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (WARN_ON(!map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	 * Irregardless of whether the frontswap backend has been loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	 * before this function or it will be later, we _MUST_ have the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	 * p->frontswap set to something valid to work properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	frontswap_map_set(sis, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	for_each_frontswap_ops(ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		ops->init(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) EXPORT_SYMBOL(__frontswap_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) bool __frontswap_test(struct swap_info_struct *sis,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 				pgoff_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	if (sis->frontswap_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		return test_bit(offset, sis->frontswap_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) EXPORT_SYMBOL(__frontswap_test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static inline void __frontswap_set(struct swap_info_struct *sis,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				   pgoff_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	set_bit(offset, sis->frontswap_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	atomic_inc(&sis->frontswap_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static inline void __frontswap_clear(struct swap_info_struct *sis,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 				     pgoff_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	clear_bit(offset, sis->frontswap_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	atomic_dec(&sis->frontswap_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  * "Store" data from a page to frontswap and associate it with the page's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  * swaptype and offset.  Page must be locked and in the swap cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  * If frontswap already contains a page with matching swaptype and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  * offset, the frontswap implementation may either overwrite the data and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  * return success or invalidate the page from frontswap and return failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) int __frontswap_store(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	swp_entry_t entry = { .val = page_private(page), };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	int type = swp_type(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	struct swap_info_struct *sis = swap_info[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	pgoff_t offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	struct frontswap_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	VM_BUG_ON(!frontswap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	VM_BUG_ON(!PageLocked(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	VM_BUG_ON(sis == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	 * If a dup, we must remove the old page first; we can't leave the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	 * old page no matter if the store of the new page succeeds or fails,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	 * and we can't rely on the new page replacing the old page as we may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	 * not store to the same implementation that contains the old page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (__frontswap_test(sis, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		__frontswap_clear(sis, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		for_each_frontswap_ops(ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			ops->invalidate_page(type, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	/* Try to store in each implementation, until one succeeds. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	for_each_frontswap_ops(ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		ret = ops->store(type, offset, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		if (!ret) /* successful store */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		__frontswap_set(sis, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		inc_frontswap_succ_stores();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		inc_frontswap_failed_stores();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (frontswap_writethrough_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		/* report failure so swap also writes to swap device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) EXPORT_SYMBOL(__frontswap_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  * "Get" data from frontswap associated with swaptype and offset that were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)  * specified when the data was put to frontswap and use it to fill the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  * specified page with data. Page must be locked and in the swap cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) int __frontswap_load(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	swp_entry_t entry = { .val = page_private(page), };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	int type = swp_type(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	struct swap_info_struct *sis = swap_info[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	pgoff_t offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	struct frontswap_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	VM_BUG_ON(!frontswap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	VM_BUG_ON(!PageLocked(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	VM_BUG_ON(sis == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (!__frontswap_test(sis, offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	/* Try loading from each implementation, until one succeeds. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	for_each_frontswap_ops(ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		ret = ops->load(type, offset, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		if (!ret) /* successful load */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		inc_frontswap_loads();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		if (frontswap_tmem_exclusive_gets_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			SetPageDirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			__frontswap_clear(sis, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) EXPORT_SYMBOL(__frontswap_load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)  * Invalidate any data from frontswap associated with the specified swaptype
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)  * and offset so that a subsequent "get" will fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	struct swap_info_struct *sis = swap_info[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	struct frontswap_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	VM_BUG_ON(!frontswap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	VM_BUG_ON(sis == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (!__frontswap_test(sis, offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	for_each_frontswap_ops(ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		ops->invalidate_page(type, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	__frontswap_clear(sis, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	inc_frontswap_invalidates();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) EXPORT_SYMBOL(__frontswap_invalidate_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)  * Invalidate all data from frontswap associated with all offsets for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)  * specified swaptype.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) void __frontswap_invalidate_area(unsigned type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	struct swap_info_struct *sis = swap_info[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	struct frontswap_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	VM_BUG_ON(!frontswap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	VM_BUG_ON(sis == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (sis->frontswap_map == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	for_each_frontswap_ops(ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		ops->invalidate_area(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	atomic_set(&sis->frontswap_pages, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	bitmap_zero(sis->frontswap_map, sis->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) EXPORT_SYMBOL(__frontswap_invalidate_area);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static unsigned long __frontswap_curr_pages(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	unsigned long totalpages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	struct swap_info_struct *si = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	assert_spin_locked(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	plist_for_each_entry(si, &swap_active_head, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		totalpages += atomic_read(&si->frontswap_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	return totalpages;
^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) static int __frontswap_unuse_pages(unsigned long total, unsigned long *unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 					int *swapid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	struct swap_info_struct *si = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	int si_frontswap_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	unsigned long total_pages_to_unuse = total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	unsigned long pages = 0, pages_to_unuse = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	assert_spin_locked(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	plist_for_each_entry(si, &swap_active_head, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		si_frontswap_pages = atomic_read(&si->frontswap_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		if (total_pages_to_unuse < si_frontswap_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			pages = pages_to_unuse = total_pages_to_unuse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			pages = si_frontswap_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 			pages_to_unuse = 0; /* unuse all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		/* ensure there is enough RAM to fetch pages from frontswap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		if (security_vm_enough_memory_mm(current->mm, pages)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		vm_unacct_memory(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		*unused = pages_to_unuse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		*swapid = si->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)  * Used to check if it's necessary and feasible to unuse pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)  * Return 1 when nothing to do, 0 when need to shrink pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)  * error code when there is an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static int __frontswap_shrink(unsigned long target_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 				unsigned long *pages_to_unuse,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 				int *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	unsigned long total_pages = 0, total_pages_to_unuse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	assert_spin_locked(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	total_pages = __frontswap_curr_pages();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	if (total_pages <= target_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		/* Nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		*pages_to_unuse = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	total_pages_to_unuse = total_pages - target_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	return __frontswap_unuse_pages(total_pages_to_unuse, pages_to_unuse, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)  * Frontswap, like a true swap device, may unnecessarily retain pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)  * under certain circumstances; "shrink" frontswap is essentially a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)  * "partial swapoff" and works by calling try_to_unuse to attempt to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)  * unuse enough frontswap pages to attempt to -- subject to memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)  * constraints -- reduce the number of pages in frontswap to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)  * number given in the parameter target_pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) void frontswap_shrink(unsigned long target_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	unsigned long pages_to_unuse = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	int type, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	 * we don't want to hold swap_lock while doing a very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	 * lengthy try_to_unuse, but swap_list may change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	 * so restart scan from swap_active_head each time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	ret = __frontswap_shrink(target_pages, &pages_to_unuse, &type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		try_to_unuse(type, true, pages_to_unuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) EXPORT_SYMBOL(frontswap_shrink);
^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)  * Count and return the number of frontswap pages across all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)  * swap devices.  This is exported so that backend drivers can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)  * determine current usage without reading debugfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) unsigned long frontswap_curr_pages(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	unsigned long totalpages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	totalpages = __frontswap_curr_pages();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	return totalpages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) EXPORT_SYMBOL(frontswap_curr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static int __init init_frontswap(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) #ifdef CONFIG_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	struct dentry *root = debugfs_create_dir("frontswap", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	if (root == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	debugfs_create_u64("loads", 0444, root, &frontswap_loads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	debugfs_create_u64("succ_stores", 0444, root, &frontswap_succ_stores);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	debugfs_create_u64("failed_stores", 0444, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 			   &frontswap_failed_stores);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	debugfs_create_u64("invalidates", 0444, root, &frontswap_invalidates);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) module_init(init_frontswap);