blob: eee97a5f445bd460a0efdd43450b90c1974f4cb9 [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;
58 __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET);
59 mutex_unlock(&swap_slots_cache_mutex);
60}
61
62static void reactivate_swap_slots_cache(void)
63{
64 mutex_lock(&swap_slots_cache_mutex);
65 swap_slot_cache_active = true;
66 mutex_unlock(&swap_slots_cache_mutex);
67}
68
69/* Must not be called with cpu hot plug lock */
70void disable_swap_slots_cache_lock(void)
71{
72 mutex_lock(&swap_slots_cache_enable_mutex);
73 swap_slot_cache_enabled = false;
74 if (swap_slot_cache_initialized) {
75 /* serialize with cpu hotplug operations */
76 get_online_cpus();
77 __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET);
78 put_online_cpus();
79 }
80}
81
82static void __reenable_swap_slots_cache(void)
83{
84 swap_slot_cache_enabled = has_usable_swap();
85}
86
87void reenable_swap_slots_cache_unlock(void)
88{
89 __reenable_swap_slots_cache();
90 mutex_unlock(&swap_slots_cache_enable_mutex);
91}
92
93static bool check_cache_active(void)
94{
95 long pages;
96
Zhen Leie0f3ebb2020-08-06 23:20:11 -070097 if (!swap_slot_cache_enabled)
Tim Chen67afa382017-02-22 15:45:39 -080098 return false;
99
100 pages = get_nr_swap_pages();
101 if (!swap_slot_cache_active) {
102 if (pages > num_online_cpus() *
103 THRESHOLD_ACTIVATE_SWAP_SLOTS_CACHE)
104 reactivate_swap_slots_cache();
105 goto out;
106 }
107
108 /* if global pool of slot caches too low, deactivate cache */
109 if (pages < num_online_cpus() * THRESHOLD_DEACTIVATE_SWAP_SLOTS_CACHE)
110 deactivate_swap_slots_cache();
111out:
112 return swap_slot_cache_active;
113}
114
115static int alloc_swap_slot_cache(unsigned int cpu)
116{
117 struct swap_slots_cache *cache;
118 swp_entry_t *slots, *slots_ret;
Bing Hand4eef932022-05-30 16:28:05 +0800119 bool skip = false;
120 int ret = 0;
Tim Chen67afa382017-02-22 15:45:39 -0800121
122 /*
123 * Do allocation outside swap_slots_cache_mutex
Huang Ying54f180d2017-05-08 15:57:40 -0700124 * as kvzalloc could trigger reclaim and get_swap_page,
Tim Chen67afa382017-02-22 15:45:39 -0800125 * which can lock swap_slots_cache_mutex.
126 */
Bing Hand4eef932022-05-30 16:28:05 +0800127 trace_android_vh_alloc_swap_slot_cache(&per_cpu(swp_slots, cpu),
128 &ret, &skip);
129 if (skip)
130 return ret;
Kees Cook778e1cd2018-06-12 14:04:48 -0700131 slots = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t),
Huang Ying54f180d2017-05-08 15:57:40 -0700132 GFP_KERNEL);
Tim Chen67afa382017-02-22 15:45:39 -0800133 if (!slots)
134 return -ENOMEM;
135
Kees Cook778e1cd2018-06-12 14:04:48 -0700136 slots_ret = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t),
Huang Ying54f180d2017-05-08 15:57:40 -0700137 GFP_KERNEL);
Tim Chen67afa382017-02-22 15:45:39 -0800138 if (!slots_ret) {
Huang Ying54f180d2017-05-08 15:57:40 -0700139 kvfree(slots);
Tim Chen67afa382017-02-22 15:45:39 -0800140 return -ENOMEM;
141 }
142
143 mutex_lock(&swap_slots_cache_mutex);
144 cache = &per_cpu(swp_slots, cpu);
Zhen Leif90eae22020-08-06 23:20:05 -0700145 if (cache->slots || cache->slots_ret) {
Tim Chen67afa382017-02-22 15:45:39 -0800146 /* cache already allocated */
Zhen Leif90eae22020-08-06 23:20:05 -0700147 mutex_unlock(&swap_slots_cache_mutex);
148
149 kvfree(slots);
150 kvfree(slots_ret);
151
152 return 0;
153 }
154
Tim Chen67afa382017-02-22 15:45:39 -0800155 if (!cache->lock_initialized) {
156 mutex_init(&cache->alloc_lock);
157 spin_lock_init(&cache->free_lock);
158 cache->lock_initialized = true;
159 }
160 cache->nr = 0;
161 cache->cur = 0;
162 cache->n_ret = 0;
Tim Chena2e16732017-11-15 17:34:18 -0800163 /*
164 * We initialized alloc_lock and free_lock earlier. We use
165 * !cache->slots or !cache->slots_ret to know if it is safe to acquire
166 * the corresponding lock and use the cache. Memory barrier below
167 * ensures the assumption.
168 */
169 mb();
Tim Chen67afa382017-02-22 15:45:39 -0800170 cache->slots = slots;
Tim Chen67afa382017-02-22 15:45:39 -0800171 cache->slots_ret = slots_ret;
Tim Chen67afa382017-02-22 15:45:39 -0800172 mutex_unlock(&swap_slots_cache_mutex);
Tim Chen67afa382017-02-22 15:45:39 -0800173 return 0;
174}
175
176static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type,
177 bool free_slots)
178{
179 struct swap_slots_cache *cache;
180 swp_entry_t *slots = NULL;
Bing Hand4eef932022-05-30 16:28:05 +0800181 bool skip = false;
Tim Chen67afa382017-02-22 15:45:39 -0800182
183 cache = &per_cpu(swp_slots, cpu);
Bing Hand4eef932022-05-30 16:28:05 +0800184 trace_android_vh_drain_slots_cache_cpu(cache, type,
185 free_slots, &skip);
186 if (skip)
187 return;
Tim Chen67afa382017-02-22 15:45:39 -0800188 if ((type & SLOTS_CACHE) && cache->slots) {
189 mutex_lock(&cache->alloc_lock);
190 swapcache_free_entries(cache->slots + cache->cur, cache->nr);
191 cache->cur = 0;
192 cache->nr = 0;
193 if (free_slots && cache->slots) {
Huang Ying54f180d2017-05-08 15:57:40 -0700194 kvfree(cache->slots);
Tim Chen67afa382017-02-22 15:45:39 -0800195 cache->slots = NULL;
196 }
197 mutex_unlock(&cache->alloc_lock);
198 }
199 if ((type & SLOTS_CACHE_RET) && cache->slots_ret) {
200 spin_lock_irq(&cache->free_lock);
201 swapcache_free_entries(cache->slots_ret, cache->n_ret);
202 cache->n_ret = 0;
203 if (free_slots && cache->slots_ret) {
204 slots = cache->slots_ret;
205 cache->slots_ret = NULL;
206 }
207 spin_unlock_irq(&cache->free_lock);
208 if (slots)
Huang Ying54f180d2017-05-08 15:57:40 -0700209 kvfree(slots);
Tim Chen67afa382017-02-22 15:45:39 -0800210 }
211}
212
213static void __drain_swap_slots_cache(unsigned int type)
214{
215 unsigned int cpu;
216
217 /*
218 * This function is called during
219 * 1) swapoff, when we have to make sure no
220 * left over slots are in cache when we remove
221 * a swap device;
222 * 2) disabling of swap slot cache, when we run low
223 * on swap slots when allocating memory and need
224 * to return swap slots to global pool.
225 *
226 * We cannot acquire cpu hot plug lock here as
227 * this function can be invoked in the cpu
228 * hot plug path:
229 * cpu_up -> lock cpu_hotplug -> cpu hotplug state callback
230 * -> memory allocation -> direct reclaim -> get_swap_page
231 * -> drain_swap_slots_cache
232 *
233 * Hence the loop over current online cpu below could miss cpu that
234 * is being brought online but not yet marked as online.
235 * That is okay as we do not schedule and run anything on a
236 * cpu before it has been marked online. Hence, we will not
237 * fill any swap slots in slots cache of such cpu.
238 * There are no slots on such cpu that need to be drained.
239 */
240 for_each_online_cpu(cpu)
241 drain_slots_cache_cpu(cpu, type, false);
242}
243
244static int free_slot_cache(unsigned int cpu)
245{
246 mutex_lock(&swap_slots_cache_mutex);
247 drain_slots_cache_cpu(cpu, SLOTS_CACHE | SLOTS_CACHE_RET, true);
248 mutex_unlock(&swap_slots_cache_mutex);
249 return 0;
250}
251
Miaohe Linf3bc52c2020-10-13 16:52:18 -0700252void enable_swap_slots_cache(void)
Tim Chen67afa382017-02-22 15:45:39 -0800253{
Tim Chen67afa382017-02-22 15:45:39 -0800254 mutex_lock(&swap_slots_cache_enable_mutex);
Zhen Leid69a9572020-08-06 23:20:08 -0700255 if (!swap_slot_cache_initialized) {
256 int ret;
257
258 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "swap_slots_cache",
259 alloc_swap_slot_cache, free_slot_cache);
260 if (WARN_ONCE(ret < 0, "Cache allocation failed (%s), operating "
261 "without swap slots cache.\n", __func__))
262 goto out_unlock;
263
264 swap_slot_cache_initialized = true;
Tim Chen67afa382017-02-22 15:45:39 -0800265 }
266
Tim Chen67afa382017-02-22 15:45:39 -0800267 __reenable_swap_slots_cache();
268out_unlock:
269 mutex_unlock(&swap_slots_cache_enable_mutex);
Tim Chen67afa382017-02-22 15:45:39 -0800270}
271
272/* called with swap slot cache's alloc lock held */
273static int refill_swap_slots_cache(struct swap_slots_cache *cache)
274{
275 if (!use_swap_slot_cache || cache->nr)
276 return 0;
277
278 cache->cur = 0;
279 if (swap_slot_cache_active)
Huang Ying5d5e8f12018-08-21 21:52:20 -0700280 cache->nr = get_swap_pages(SWAP_SLOTS_CACHE_SIZE,
281 cache->slots, 1);
Tim Chen67afa382017-02-22 15:45:39 -0800282
283 return cache->nr;
284}
285
286int free_swap_slot(swp_entry_t entry)
287{
288 struct swap_slots_cache *cache;
Bing Han7222a0b2022-06-10 18:45:31 +0800289 bool skip = false;
Tim Chen67afa382017-02-22 15:45:39 -0800290
Sebastian Andrzej Siewiorf07e0f842017-07-10 15:49:29 -0700291 cache = raw_cpu_ptr(&swp_slots);
Bing Han7222a0b2022-06-10 18:45:31 +0800292 trace_android_vh_free_swap_slot(entry, cache, &skip);
293 if (skip)
294 return 0;
Tim Chena2e16732017-11-15 17:34:18 -0800295 if (likely(use_swap_slot_cache && cache->slots_ret)) {
Tim Chen67afa382017-02-22 15:45:39 -0800296 spin_lock_irq(&cache->free_lock);
297 /* Swap slots cache may be deactivated before acquiring lock */
Sebastian Andrzej Siewiorf07e0f842017-07-10 15:49:29 -0700298 if (!use_swap_slot_cache || !cache->slots_ret) {
Tim Chen67afa382017-02-22 15:45:39 -0800299 spin_unlock_irq(&cache->free_lock);
300 goto direct_free;
301 }
302 if (cache->n_ret >= SWAP_SLOTS_CACHE_SIZE) {
303 /*
304 * Return slots to global pool.
305 * The current swap_map value is SWAP_HAS_CACHE.
306 * Set it to 0 to indicate it is available for
307 * allocation in global pool
308 */
309 swapcache_free_entries(cache->slots_ret, cache->n_ret);
310 cache->n_ret = 0;
311 }
312 cache->slots_ret[cache->n_ret++] = entry;
313 spin_unlock_irq(&cache->free_lock);
314 } else {
315direct_free:
316 swapcache_free_entries(&entry, 1);
317 }
Tim Chen67afa382017-02-22 15:45:39 -0800318
319 return 0;
320}
321
Huang Ying38d8b4e2017-07-06 15:37:18 -0700322swp_entry_t get_swap_page(struct page *page)
Tim Chen67afa382017-02-22 15:45:39 -0800323{
Wei Yang2406b762020-04-01 21:06:16 -0700324 swp_entry_t entry;
Tim Chen67afa382017-02-22 15:45:39 -0800325 struct swap_slots_cache *cache;
Bing Hand4eef932022-05-30 16:28:05 +0800326 bool found = false;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700327 entry.val = 0;
328
Bing Hand4eef932022-05-30 16:28:05 +0800329 trace_android_vh_get_swap_page(page, &entry, raw_cpu_ptr(&swp_slots), &found);
330 if (found)
331 goto out;
332
Huang Ying38d8b4e2017-07-06 15:37:18 -0700333 if (PageTransHuge(page)) {
334 if (IS_ENABLED(CONFIG_THP_SWAP))
Huang Ying5d5e8f12018-08-21 21:52:20 -0700335 get_swap_pages(1, &entry, HPAGE_PMD_NR);
Tejun Heobb98f2c2018-06-07 17:05:31 -0700336 goto out;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700337 }
338
Tim Chen67afa382017-02-22 15:45:39 -0800339 /*
340 * Preemption is allowed here, because we may sleep
341 * in refill_swap_slots_cache(). But it is safe, because
342 * accesses to the per-CPU data structure are protected by the
343 * mutex cache->alloc_lock.
344 *
345 * The alloc path here does not touch cache->slots_ret
346 * so cache->free_lock is not taken.
347 */
348 cache = raw_cpu_ptr(&swp_slots);
349
Tim Chena2e16732017-11-15 17:34:18 -0800350 if (likely(check_cache_active() && cache->slots)) {
Tim Chen67afa382017-02-22 15:45:39 -0800351 mutex_lock(&cache->alloc_lock);
352 if (cache->slots) {
353repeat:
354 if (cache->nr) {
Wei Yang2406b762020-04-01 21:06:16 -0700355 entry = cache->slots[cache->cur];
356 cache->slots[cache->cur++].val = 0;
Tim Chen67afa382017-02-22 15:45:39 -0800357 cache->nr--;
Wei Yang2406b762020-04-01 21:06:16 -0700358 } else if (refill_swap_slots_cache(cache)) {
359 goto repeat;
Tim Chen67afa382017-02-22 15:45:39 -0800360 }
361 }
362 mutex_unlock(&cache->alloc_lock);
363 if (entry.val)
Tejun Heobb98f2c2018-06-07 17:05:31 -0700364 goto out;
Tim Chen67afa382017-02-22 15:45:39 -0800365 }
366
Huang Ying5d5e8f12018-08-21 21:52:20 -0700367 get_swap_pages(1, &entry, 1);
Tejun Heobb98f2c2018-06-07 17:05:31 -0700368out:
369 if (mem_cgroup_try_charge_swap(page, entry)) {
370 put_swap_page(page, entry);
371 entry.val = 0;
372 }
Tim Chen67afa382017-02-22 15:45:39 -0800373 return entry;
374}