blob: a200aafc0a36d418462705a8b68a1628a457395e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Tim Chen67afa382017-02-22 15:45:39 -08002/*
3 * Manage cache of swap slots to be used for and returned from
4 * swap.
5 *
6 * Copyright(c) 2016 Intel Corporation.
7 *
8 * Author: Tim Chen <tim.c.chen@linux.intel.com>
9 *
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
13 * we need a new slot.
14 *
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 coaellesce and reduce fragmentation.
20 *
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.
24 *
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.
28 */
29
30#include <linux/swap_slots.h>
31#include <linux/cpu.h>
32#include <linux/cpumask.h>
33#include <linux/vmalloc.h>
34#include <linux/mutex.h>
Huang Ying54f180d2017-05-08 15:57:40 -070035#include <linux/mm.h>
Bing Hand4eef932022-05-30 16:28:05 +080036#include <trace/hooks/mm.h>
Tim Chen67afa382017-02-22 15:45:39 -080037
Tim Chen67afa382017-02-22 15:45:39 -080038static DEFINE_PER_CPU(struct swap_slots_cache, swp_slots);
39static bool swap_slot_cache_active;
Huang Yingba81f832017-02-22 15:45:46 -080040bool swap_slot_cache_enabled;
Tim Chen67afa382017-02-22 15:45:39 -080041static bool swap_slot_cache_initialized;
Colin Ian King31f21da2018-08-17 15:46:54 -070042static DEFINE_MUTEX(swap_slots_cache_mutex);
Tim Chen67afa382017-02-22 15:45:39 -080043/* Serialize swap slots cache enable/disable operations */
Colin Ian King31f21da2018-08-17 15:46:54 -070044static DEFINE_MUTEX(swap_slots_cache_enable_mutex);
Tim Chen67afa382017-02-22 15:45:39 -080045
46static void __drain_swap_slots_cache(unsigned int type);
47static void deactivate_swap_slots_cache(void);
48static void reactivate_swap_slots_cache(void);
49
Zhen Leie0f3ebb2020-08-06 23:20:11 -070050#define use_swap_slot_cache (swap_slot_cache_active && swap_slot_cache_enabled)
Tim Chen67afa382017-02-22 15:45:39 -080051#define SLOTS_CACHE 0x1
52#define SLOTS_CACHE_RET 0x2
53
54static void deactivate_swap_slots_cache(void)
55{
56 mutex_lock(&swap_slots_cache_mutex);
57 swap_slot_cache_active = false;
Bing Hanf6f18f72022-06-11 16:06:16 +080058 trace_android_vh_swap_slot_cache_active(false);
Tim Chen67afa382017-02-22 15:45:39 -080059 __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET);
60 mutex_unlock(&swap_slots_cache_mutex);
61}
62
63static void reactivate_swap_slots_cache(void)
64{
65 mutex_lock(&swap_slots_cache_mutex);
66 swap_slot_cache_active = true;
Bing Hanf6f18f72022-06-11 16:06:16 +080067 trace_android_vh_swap_slot_cache_active(true);
Tim Chen67afa382017-02-22 15:45:39 -080068 mutex_unlock(&swap_slots_cache_mutex);
69}
70
71/* Must not be called with cpu hot plug lock */
72void disable_swap_slots_cache_lock(void)
73{
74 mutex_lock(&swap_slots_cache_enable_mutex);
75 swap_slot_cache_enabled = false;
76 if (swap_slot_cache_initialized) {
77 /* serialize with cpu hotplug operations */
78 get_online_cpus();
79 __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET);
80 put_online_cpus();
81 }
82}
83
84static void __reenable_swap_slots_cache(void)
85{
86 swap_slot_cache_enabled = has_usable_swap();
87}
88
89void reenable_swap_slots_cache_unlock(void)
90{
91 __reenable_swap_slots_cache();
92 mutex_unlock(&swap_slots_cache_enable_mutex);
93}
94
Bing Han06c27662022-06-10 19:40:35 +080095bool check_cache_active(void)
Tim Chen67afa382017-02-22 15:45:39 -080096{
97 long pages;
98
Zhen Leie0f3ebb2020-08-06 23:20:11 -070099 if (!swap_slot_cache_enabled)
Tim Chen67afa382017-02-22 15:45:39 -0800100 return false;
101
102 pages = get_nr_swap_pages();
103 if (!swap_slot_cache_active) {
104 if (pages > num_online_cpus() *
105 THRESHOLD_ACTIVATE_SWAP_SLOTS_CACHE)
106 reactivate_swap_slots_cache();
107 goto out;
108 }
109
110 /* if global pool of slot caches too low, deactivate cache */
111 if (pages < num_online_cpus() * THRESHOLD_DEACTIVATE_SWAP_SLOTS_CACHE)
112 deactivate_swap_slots_cache();
113out:
114 return swap_slot_cache_active;
115}
Bing Han06c27662022-06-10 19:40:35 +0800116EXPORT_SYMBOL_GPL(check_cache_active);
Tim Chen67afa382017-02-22 15:45:39 -0800117
118static int alloc_swap_slot_cache(unsigned int cpu)
119{
120 struct swap_slots_cache *cache;
121 swp_entry_t *slots, *slots_ret;
Bing Hand4eef932022-05-30 16:28:05 +0800122 bool skip = false;
123 int ret = 0;
Tim Chen67afa382017-02-22 15:45:39 -0800124
125 /*
126 * Do allocation outside swap_slots_cache_mutex
Huang Ying54f180d2017-05-08 15:57:40 -0700127 * as kvzalloc could trigger reclaim and get_swap_page,
Tim Chen67afa382017-02-22 15:45:39 -0800128 * which can lock swap_slots_cache_mutex.
129 */
Bing Hand4eef932022-05-30 16:28:05 +0800130 trace_android_vh_alloc_swap_slot_cache(&per_cpu(swp_slots, cpu),
131 &ret, &skip);
132 if (skip)
133 return ret;
Kees Cook778e1cd2018-06-12 14:04:48 -0700134 slots = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t),
Huang Ying54f180d2017-05-08 15:57:40 -0700135 GFP_KERNEL);
Tim Chen67afa382017-02-22 15:45:39 -0800136 if (!slots)
137 return -ENOMEM;
138
Kees Cook778e1cd2018-06-12 14:04:48 -0700139 slots_ret = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t),
Huang Ying54f180d2017-05-08 15:57:40 -0700140 GFP_KERNEL);
Tim Chen67afa382017-02-22 15:45:39 -0800141 if (!slots_ret) {
Huang Ying54f180d2017-05-08 15:57:40 -0700142 kvfree(slots);
Tim Chen67afa382017-02-22 15:45:39 -0800143 return -ENOMEM;
144 }
145
146 mutex_lock(&swap_slots_cache_mutex);
147 cache = &per_cpu(swp_slots, cpu);
Zhen Leif90eae22020-08-06 23:20:05 -0700148 if (cache->slots || cache->slots_ret) {
Tim Chen67afa382017-02-22 15:45:39 -0800149 /* cache already allocated */
Zhen Leif90eae22020-08-06 23:20:05 -0700150 mutex_unlock(&swap_slots_cache_mutex);
151
152 kvfree(slots);
153 kvfree(slots_ret);
154
155 return 0;
156 }
157
Tim Chen67afa382017-02-22 15:45:39 -0800158 if (!cache->lock_initialized) {
159 mutex_init(&cache->alloc_lock);
160 spin_lock_init(&cache->free_lock);
161 cache->lock_initialized = true;
162 }
163 cache->nr = 0;
164 cache->cur = 0;
165 cache->n_ret = 0;
Tim Chena2e16732017-11-15 17:34:18 -0800166 /*
167 * We initialized alloc_lock and free_lock earlier. We use
168 * !cache->slots or !cache->slots_ret to know if it is safe to acquire
169 * the corresponding lock and use the cache. Memory barrier below
170 * ensures the assumption.
171 */
172 mb();
Tim Chen67afa382017-02-22 15:45:39 -0800173 cache->slots = slots;
Tim Chen67afa382017-02-22 15:45:39 -0800174 cache->slots_ret = slots_ret;
Tim Chen67afa382017-02-22 15:45:39 -0800175 mutex_unlock(&swap_slots_cache_mutex);
Tim Chen67afa382017-02-22 15:45:39 -0800176 return 0;
177}
178
179static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type,
180 bool free_slots)
181{
182 struct swap_slots_cache *cache;
183 swp_entry_t *slots = NULL;
Bing Hand4eef932022-05-30 16:28:05 +0800184 bool skip = false;
Tim Chen67afa382017-02-22 15:45:39 -0800185
186 cache = &per_cpu(swp_slots, cpu);
Bing Hand4eef932022-05-30 16:28:05 +0800187 trace_android_vh_drain_slots_cache_cpu(cache, type,
188 free_slots, &skip);
189 if (skip)
190 return;
Tim Chen67afa382017-02-22 15:45:39 -0800191 if ((type & SLOTS_CACHE) && cache->slots) {
192 mutex_lock(&cache->alloc_lock);
193 swapcache_free_entries(cache->slots + cache->cur, cache->nr);
194 cache->cur = 0;
195 cache->nr = 0;
196 if (free_slots && cache->slots) {
Huang Ying54f180d2017-05-08 15:57:40 -0700197 kvfree(cache->slots);
Tim Chen67afa382017-02-22 15:45:39 -0800198 cache->slots = NULL;
199 }
200 mutex_unlock(&cache->alloc_lock);
201 }
202 if ((type & SLOTS_CACHE_RET) && cache->slots_ret) {
203 spin_lock_irq(&cache->free_lock);
204 swapcache_free_entries(cache->slots_ret, cache->n_ret);
205 cache->n_ret = 0;
206 if (free_slots && cache->slots_ret) {
207 slots = cache->slots_ret;
208 cache->slots_ret = NULL;
209 }
210 spin_unlock_irq(&cache->free_lock);
211 if (slots)
Huang Ying54f180d2017-05-08 15:57:40 -0700212 kvfree(slots);
Tim Chen67afa382017-02-22 15:45:39 -0800213 }
214}
215
216static void __drain_swap_slots_cache(unsigned int type)
217{
218 unsigned int cpu;
219
220 /*
221 * This function is called during
222 * 1) swapoff, when we have to make sure no
223 * left over slots are in cache when we remove
224 * a swap device;
225 * 2) disabling of swap slot cache, when we run low
226 * on swap slots when allocating memory and need
227 * to return swap slots to global pool.
228 *
229 * We cannot acquire cpu hot plug lock here as
230 * this function can be invoked in the cpu
231 * hot plug path:
232 * cpu_up -> lock cpu_hotplug -> cpu hotplug state callback
233 * -> memory allocation -> direct reclaim -> get_swap_page
234 * -> drain_swap_slots_cache
235 *
236 * Hence the loop over current online cpu below could miss cpu that
237 * is being brought online but not yet marked as online.
238 * That is okay as we do not schedule and run anything on a
239 * cpu before it has been marked online. Hence, we will not
240 * fill any swap slots in slots cache of such cpu.
241 * There are no slots on such cpu that need to be drained.
242 */
243 for_each_online_cpu(cpu)
244 drain_slots_cache_cpu(cpu, type, false);
245}
246
247static int free_slot_cache(unsigned int cpu)
248{
249 mutex_lock(&swap_slots_cache_mutex);
250 drain_slots_cache_cpu(cpu, SLOTS_CACHE | SLOTS_CACHE_RET, true);
251 mutex_unlock(&swap_slots_cache_mutex);
252 return 0;
253}
254
Miaohe Linf3bc52c2020-10-13 16:52:18 -0700255void enable_swap_slots_cache(void)
Tim Chen67afa382017-02-22 15:45:39 -0800256{
Tim Chen67afa382017-02-22 15:45:39 -0800257 mutex_lock(&swap_slots_cache_enable_mutex);
Zhen Leid69a9572020-08-06 23:20:08 -0700258 if (!swap_slot_cache_initialized) {
259 int ret;
260
261 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "swap_slots_cache",
262 alloc_swap_slot_cache, free_slot_cache);
263 if (WARN_ONCE(ret < 0, "Cache allocation failed (%s), operating "
264 "without swap slots cache.\n", __func__))
265 goto out_unlock;
266
267 swap_slot_cache_initialized = true;
Tim Chen67afa382017-02-22 15:45:39 -0800268 }
269
Tim Chen67afa382017-02-22 15:45:39 -0800270 __reenable_swap_slots_cache();
271out_unlock:
272 mutex_unlock(&swap_slots_cache_enable_mutex);
Tim Chen67afa382017-02-22 15:45:39 -0800273}
274
275/* called with swap slot cache's alloc lock held */
276static int refill_swap_slots_cache(struct swap_slots_cache *cache)
277{
278 if (!use_swap_slot_cache || cache->nr)
279 return 0;
280
281 cache->cur = 0;
282 if (swap_slot_cache_active)
Huang Ying5d5e8f12018-08-21 21:52:20 -0700283 cache->nr = get_swap_pages(SWAP_SLOTS_CACHE_SIZE,
284 cache->slots, 1);
Tim Chen67afa382017-02-22 15:45:39 -0800285
286 return cache->nr;
287}
288
289int free_swap_slot(swp_entry_t entry)
290{
291 struct swap_slots_cache *cache;
Bing Han7222a0b2022-06-10 18:45:31 +0800292 bool skip = false;
Tim Chen67afa382017-02-22 15:45:39 -0800293
Sebastian Andrzej Siewiorf07e0f842017-07-10 15:49:29 -0700294 cache = raw_cpu_ptr(&swp_slots);
Bing Han7222a0b2022-06-10 18:45:31 +0800295 trace_android_vh_free_swap_slot(entry, cache, &skip);
296 if (skip)
297 return 0;
Tim Chena2e16732017-11-15 17:34:18 -0800298 if (likely(use_swap_slot_cache && cache->slots_ret)) {
Tim Chen67afa382017-02-22 15:45:39 -0800299 spin_lock_irq(&cache->free_lock);
300 /* Swap slots cache may be deactivated before acquiring lock */
Sebastian Andrzej Siewiorf07e0f842017-07-10 15:49:29 -0700301 if (!use_swap_slot_cache || !cache->slots_ret) {
Tim Chen67afa382017-02-22 15:45:39 -0800302 spin_unlock_irq(&cache->free_lock);
303 goto direct_free;
304 }
305 if (cache->n_ret >= SWAP_SLOTS_CACHE_SIZE) {
306 /*
307 * Return slots to global pool.
308 * The current swap_map value is SWAP_HAS_CACHE.
309 * Set it to 0 to indicate it is available for
310 * allocation in global pool
311 */
312 swapcache_free_entries(cache->slots_ret, cache->n_ret);
313 cache->n_ret = 0;
314 }
315 cache->slots_ret[cache->n_ret++] = entry;
316 spin_unlock_irq(&cache->free_lock);
317 } else {
318direct_free:
319 swapcache_free_entries(&entry, 1);
320 }
Tim Chen67afa382017-02-22 15:45:39 -0800321
322 return 0;
323}
324
Huang Ying38d8b4e2017-07-06 15:37:18 -0700325swp_entry_t get_swap_page(struct page *page)
Tim Chen67afa382017-02-22 15:45:39 -0800326{
Wei Yang2406b762020-04-01 21:06:16 -0700327 swp_entry_t entry;
Tim Chen67afa382017-02-22 15:45:39 -0800328 struct swap_slots_cache *cache;
Bing Hand4eef932022-05-30 16:28:05 +0800329 bool found = false;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700330 entry.val = 0;
331
Bing Hand4eef932022-05-30 16:28:05 +0800332 trace_android_vh_get_swap_page(page, &entry, raw_cpu_ptr(&swp_slots), &found);
333 if (found)
334 goto out;
335
Huang Ying38d8b4e2017-07-06 15:37:18 -0700336 if (PageTransHuge(page)) {
337 if (IS_ENABLED(CONFIG_THP_SWAP))
Huang Ying5d5e8f12018-08-21 21:52:20 -0700338 get_swap_pages(1, &entry, HPAGE_PMD_NR);
Tejun Heobb98f2c2018-06-07 17:05:31 -0700339 goto out;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700340 }
341
Tim Chen67afa382017-02-22 15:45:39 -0800342 /*
343 * Preemption is allowed here, because we may sleep
344 * in refill_swap_slots_cache(). But it is safe, because
345 * accesses to the per-CPU data structure are protected by the
346 * mutex cache->alloc_lock.
347 *
348 * The alloc path here does not touch cache->slots_ret
349 * so cache->free_lock is not taken.
350 */
351 cache = raw_cpu_ptr(&swp_slots);
352
Tim Chena2e16732017-11-15 17:34:18 -0800353 if (likely(check_cache_active() && cache->slots)) {
Tim Chen67afa382017-02-22 15:45:39 -0800354 mutex_lock(&cache->alloc_lock);
355 if (cache->slots) {
356repeat:
357 if (cache->nr) {
Wei Yang2406b762020-04-01 21:06:16 -0700358 entry = cache->slots[cache->cur];
359 cache->slots[cache->cur++].val = 0;
Tim Chen67afa382017-02-22 15:45:39 -0800360 cache->nr--;
Wei Yang2406b762020-04-01 21:06:16 -0700361 } else if (refill_swap_slots_cache(cache)) {
362 goto repeat;
Tim Chen67afa382017-02-22 15:45:39 -0800363 }
364 }
365 mutex_unlock(&cache->alloc_lock);
366 if (entry.val)
Tejun Heobb98f2c2018-06-07 17:05:31 -0700367 goto out;
Tim Chen67afa382017-02-22 15:45:39 -0800368 }
369
Huang Ying5d5e8f12018-08-21 21:52:20 -0700370 get_swap_pages(1, &entry, 1);
Tejun Heobb98f2c2018-06-07 17:05:31 -0700371out:
372 if (mem_cgroup_try_charge_swap(page, entry)) {
373 put_swap_page(page, entry);
374 entry.val = 0;
375 }
Tim Chen67afa382017-02-22 15:45:39 -0800376 return entry;
377}