1 // SPDX-License-Identifier: GPL-2.0
3 * Manage cache of swap slots to be used for and returned from
6 * Copyright(c) 2016 Intel Corporation.
8 * Author: Tim Chen <tim.c.chen@linux.intel.com>
10 * We allocate the swap slots from the global pool and put
11 * it into local per cpu caches. This has the advantage
12 * of no needing to acquire the swap_info lock every time
15 * There is also opportunity to simply return the slot
16 * to local caches without needing to acquire swap_info
17 * lock. We do not reuse the returned slots directly but
18 * move them back to the global pool in a batch. This
19 * allows the slots to coalesce and reduce fragmentation.
21 * The swap entry allocated is marked with SWAP_HAS_CACHE
22 * flag in map_count that prevents it from being allocated
23 * again from the global pool.
25 * The swap slots cache is protected by a mutex instead of
26 * a spin lock as when we search for slots with scan_swap_map,
27 * we can possibly sleep.
30 #include <linux/swap_slots.h>
31 #include <linux/cpu.h>
32 #include <linux/cpumask.h>
33 #include <linux/slab.h>
34 #include <linux/vmalloc.h>
35 #include <linux/mutex.h>
38 static DEFINE_PER_CPU(struct swap_slots_cache, swp_slots);
39 static bool swap_slot_cache_active;
40 bool swap_slot_cache_enabled;
41 static bool swap_slot_cache_initialized;
42 static DEFINE_MUTEX(swap_slots_cache_mutex);
43 /* Serialize swap slots cache enable/disable operations */
44 static DEFINE_MUTEX(swap_slots_cache_enable_mutex);
46 static void __drain_swap_slots_cache(unsigned int type);
48 #define use_swap_slot_cache (swap_slot_cache_active && swap_slot_cache_enabled)
49 #define SLOTS_CACHE 0x1
50 #define SLOTS_CACHE_RET 0x2
52 static void deactivate_swap_slots_cache(void)
54 mutex_lock(&swap_slots_cache_mutex);
55 swap_slot_cache_active = false;
56 __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET);
57 mutex_unlock(&swap_slots_cache_mutex);
60 static void reactivate_swap_slots_cache(void)
62 mutex_lock(&swap_slots_cache_mutex);
63 swap_slot_cache_active = true;
64 mutex_unlock(&swap_slots_cache_mutex);
67 /* Must not be called with cpu hot plug lock */
68 void disable_swap_slots_cache_lock(void)
70 mutex_lock(&swap_slots_cache_enable_mutex);
71 swap_slot_cache_enabled = false;
72 if (swap_slot_cache_initialized) {
73 /* serialize with cpu hotplug operations */
75 __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET);
80 static void __reenable_swap_slots_cache(void)
82 swap_slot_cache_enabled = has_usable_swap();
85 void reenable_swap_slots_cache_unlock(void)
87 __reenable_swap_slots_cache();
88 mutex_unlock(&swap_slots_cache_enable_mutex);
91 static bool check_cache_active(void)
95 if (!swap_slot_cache_enabled)
98 pages = get_nr_swap_pages();
99 if (!swap_slot_cache_active) {
100 if (pages > num_online_cpus() *
101 THRESHOLD_ACTIVATE_SWAP_SLOTS_CACHE)
102 reactivate_swap_slots_cache();
106 /* if global pool of slot caches too low, deactivate cache */
107 if (pages < num_online_cpus() * THRESHOLD_DEACTIVATE_SWAP_SLOTS_CACHE)
108 deactivate_swap_slots_cache();
110 return swap_slot_cache_active;
113 static int alloc_swap_slot_cache(unsigned int cpu)
115 struct swap_slots_cache *cache;
116 swp_entry_t *slots, *slots_ret;
119 * Do allocation outside swap_slots_cache_mutex
120 * as kvzalloc could trigger reclaim and get_swap_page,
121 * which can lock swap_slots_cache_mutex.
123 slots = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t),
128 slots_ret = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t),
135 mutex_lock(&swap_slots_cache_mutex);
136 cache = &per_cpu(swp_slots, cpu);
137 if (cache->slots || cache->slots_ret) {
138 /* cache already allocated */
139 mutex_unlock(&swap_slots_cache_mutex);
147 if (!cache->lock_initialized) {
148 mutex_init(&cache->alloc_lock);
149 spin_lock_init(&cache->free_lock);
150 cache->lock_initialized = true;
156 * We initialized alloc_lock and free_lock earlier. We use
157 * !cache->slots or !cache->slots_ret to know if it is safe to acquire
158 * the corresponding lock and use the cache. Memory barrier below
159 * ensures the assumption.
162 cache->slots = slots;
163 cache->slots_ret = slots_ret;
164 mutex_unlock(&swap_slots_cache_mutex);
168 static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type,
171 struct swap_slots_cache *cache;
172 swp_entry_t *slots = NULL;
174 cache = &per_cpu(swp_slots, cpu);
175 if ((type & SLOTS_CACHE) && cache->slots) {
176 mutex_lock(&cache->alloc_lock);
177 swapcache_free_entries(cache->slots + cache->cur, cache->nr);
180 if (free_slots && cache->slots) {
181 kvfree(cache->slots);
184 mutex_unlock(&cache->alloc_lock);
186 if ((type & SLOTS_CACHE_RET) && cache->slots_ret) {
187 spin_lock_irq(&cache->free_lock);
188 swapcache_free_entries(cache->slots_ret, cache->n_ret);
190 if (free_slots && cache->slots_ret) {
191 slots = cache->slots_ret;
192 cache->slots_ret = NULL;
194 spin_unlock_irq(&cache->free_lock);
199 static void __drain_swap_slots_cache(unsigned int type)
204 * This function is called during
205 * 1) swapoff, when we have to make sure no
206 * left over slots are in cache when we remove
208 * 2) disabling of swap slot cache, when we run low
209 * on swap slots when allocating memory and need
210 * to return swap slots to global pool.
212 * We cannot acquire cpu hot plug lock here as
213 * this function can be invoked in the cpu
215 * cpu_up -> lock cpu_hotplug -> cpu hotplug state callback
216 * -> memory allocation -> direct reclaim -> get_swap_page
217 * -> drain_swap_slots_cache
219 * Hence the loop over current online cpu below could miss cpu that
220 * is being brought online but not yet marked as online.
221 * That is okay as we do not schedule and run anything on a
222 * cpu before it has been marked online. Hence, we will not
223 * fill any swap slots in slots cache of such cpu.
224 * There are no slots on such cpu that need to be drained.
226 for_each_online_cpu(cpu)
227 drain_slots_cache_cpu(cpu, type, false);
230 static int free_slot_cache(unsigned int cpu)
232 mutex_lock(&swap_slots_cache_mutex);
233 drain_slots_cache_cpu(cpu, SLOTS_CACHE | SLOTS_CACHE_RET, true);
234 mutex_unlock(&swap_slots_cache_mutex);
238 void enable_swap_slots_cache(void)
240 mutex_lock(&swap_slots_cache_enable_mutex);
241 if (!swap_slot_cache_initialized) {
244 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "swap_slots_cache",
245 alloc_swap_slot_cache, free_slot_cache);
246 if (WARN_ONCE(ret < 0, "Cache allocation failed (%s), operating "
247 "without swap slots cache.\n", __func__))
250 swap_slot_cache_initialized = true;
253 __reenable_swap_slots_cache();
255 mutex_unlock(&swap_slots_cache_enable_mutex);
258 /* called with swap slot cache's alloc lock held */
259 static int refill_swap_slots_cache(struct swap_slots_cache *cache)
261 if (!use_swap_slot_cache || cache->nr)
265 if (swap_slot_cache_active)
266 cache->nr = get_swap_pages(SWAP_SLOTS_CACHE_SIZE,
272 int free_swap_slot(swp_entry_t entry)
274 struct swap_slots_cache *cache;
276 cache = raw_cpu_ptr(&swp_slots);
277 if (likely(use_swap_slot_cache && cache->slots_ret)) {
278 spin_lock_irq(&cache->free_lock);
279 /* Swap slots cache may be deactivated before acquiring lock */
280 if (!use_swap_slot_cache || !cache->slots_ret) {
281 spin_unlock_irq(&cache->free_lock);
284 if (cache->n_ret >= SWAP_SLOTS_CACHE_SIZE) {
286 * Return slots to global pool.
287 * The current swap_map value is SWAP_HAS_CACHE.
288 * Set it to 0 to indicate it is available for
289 * allocation in global pool
291 swapcache_free_entries(cache->slots_ret, cache->n_ret);
294 cache->slots_ret[cache->n_ret++] = entry;
295 spin_unlock_irq(&cache->free_lock);
298 swapcache_free_entries(&entry, 1);
304 swp_entry_t get_swap_page(struct page *page)
307 struct swap_slots_cache *cache;
311 if (PageTransHuge(page)) {
312 if (IS_ENABLED(CONFIG_THP_SWAP))
313 get_swap_pages(1, &entry, HPAGE_PMD_NR);
318 * Preemption is allowed here, because we may sleep
319 * in refill_swap_slots_cache(). But it is safe, because
320 * accesses to the per-CPU data structure are protected by the
321 * mutex cache->alloc_lock.
323 * The alloc path here does not touch cache->slots_ret
324 * so cache->free_lock is not taken.
326 cache = raw_cpu_ptr(&swp_slots);
328 if (likely(check_cache_active() && cache->slots)) {
329 mutex_lock(&cache->alloc_lock);
333 entry = cache->slots[cache->cur];
334 cache->slots[cache->cur++].val = 0;
336 } else if (refill_swap_slots_cache(cache)) {
340 mutex_unlock(&cache->alloc_lock);
345 get_swap_pages(1, &entry, 1);
347 if (mem_cgroup_try_charge_swap(page, entry)) {
348 put_swap_page(page, entry);