blob: b40394473a3cdbfdf407433b9dde28489f7ffe55 [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>
Tim Chen67afa382017-02-22 15:45:39 -080036
Tim Chen67afa382017-02-22 15:45:39 -080037static DEFINE_PER_CPU(struct swap_slots_cache, swp_slots);
38static bool swap_slot_cache_active;
Huang Yingba81f832017-02-22 15:45:46 -080039bool swap_slot_cache_enabled;
Tim Chen67afa382017-02-22 15:45:39 -080040static bool swap_slot_cache_initialized;
Colin Ian King31f21da2018-08-17 15:46:54 -070041static DEFINE_MUTEX(swap_slots_cache_mutex);
Tim Chen67afa382017-02-22 15:45:39 -080042/* Serialize swap slots cache enable/disable operations */
Colin Ian King31f21da2018-08-17 15:46:54 -070043static DEFINE_MUTEX(swap_slots_cache_enable_mutex);
Tim Chen67afa382017-02-22 15:45:39 -080044
45static void __drain_swap_slots_cache(unsigned int type);
46static void deactivate_swap_slots_cache(void);
47static void reactivate_swap_slots_cache(void);
48
49#define use_swap_slot_cache (swap_slot_cache_active && \
50 swap_slot_cache_enabled && swap_slot_cache_initialized)
51#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
97 if (!swap_slot_cache_enabled || !swap_slot_cache_initialized)
98 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;
119
120 /*
121 * Do allocation outside swap_slots_cache_mutex
Huang Ying54f180d2017-05-08 15:57:40 -0700122 * as kvzalloc could trigger reclaim and get_swap_page,
Tim Chen67afa382017-02-22 15:45:39 -0800123 * which can lock swap_slots_cache_mutex.
124 */
Kees Cook778e1cd2018-06-12 14:04:48 -0700125 slots = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t),
Huang Ying54f180d2017-05-08 15:57:40 -0700126 GFP_KERNEL);
Tim Chen67afa382017-02-22 15:45:39 -0800127 if (!slots)
128 return -ENOMEM;
129
Kees Cook778e1cd2018-06-12 14:04:48 -0700130 slots_ret = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t),
Huang Ying54f180d2017-05-08 15:57:40 -0700131 GFP_KERNEL);
Tim Chen67afa382017-02-22 15:45:39 -0800132 if (!slots_ret) {
Huang Ying54f180d2017-05-08 15:57:40 -0700133 kvfree(slots);
Tim Chen67afa382017-02-22 15:45:39 -0800134 return -ENOMEM;
135 }
136
137 mutex_lock(&swap_slots_cache_mutex);
138 cache = &per_cpu(swp_slots, cpu);
Zhen Leif90eae22020-08-06 23:20:05 -0700139 if (cache->slots || cache->slots_ret) {
Tim Chen67afa382017-02-22 15:45:39 -0800140 /* cache already allocated */
Zhen Leif90eae22020-08-06 23:20:05 -0700141 mutex_unlock(&swap_slots_cache_mutex);
142
143 kvfree(slots);
144 kvfree(slots_ret);
145
146 return 0;
147 }
148
Tim Chen67afa382017-02-22 15:45:39 -0800149 if (!cache->lock_initialized) {
150 mutex_init(&cache->alloc_lock);
151 spin_lock_init(&cache->free_lock);
152 cache->lock_initialized = true;
153 }
154 cache->nr = 0;
155 cache->cur = 0;
156 cache->n_ret = 0;
Tim Chena2e16732017-11-15 17:34:18 -0800157 /*
158 * We initialized alloc_lock and free_lock earlier. We use
159 * !cache->slots or !cache->slots_ret to know if it is safe to acquire
160 * the corresponding lock and use the cache. Memory barrier below
161 * ensures the assumption.
162 */
163 mb();
Tim Chen67afa382017-02-22 15:45:39 -0800164 cache->slots = slots;
Tim Chen67afa382017-02-22 15:45:39 -0800165 cache->slots_ret = slots_ret;
Tim Chen67afa382017-02-22 15:45:39 -0800166 mutex_unlock(&swap_slots_cache_mutex);
Tim Chen67afa382017-02-22 15:45:39 -0800167 return 0;
168}
169
170static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type,
171 bool free_slots)
172{
173 struct swap_slots_cache *cache;
174 swp_entry_t *slots = NULL;
175
176 cache = &per_cpu(swp_slots, cpu);
177 if ((type & SLOTS_CACHE) && cache->slots) {
178 mutex_lock(&cache->alloc_lock);
179 swapcache_free_entries(cache->slots + cache->cur, cache->nr);
180 cache->cur = 0;
181 cache->nr = 0;
182 if (free_slots && cache->slots) {
Huang Ying54f180d2017-05-08 15:57:40 -0700183 kvfree(cache->slots);
Tim Chen67afa382017-02-22 15:45:39 -0800184 cache->slots = NULL;
185 }
186 mutex_unlock(&cache->alloc_lock);
187 }
188 if ((type & SLOTS_CACHE_RET) && cache->slots_ret) {
189 spin_lock_irq(&cache->free_lock);
190 swapcache_free_entries(cache->slots_ret, cache->n_ret);
191 cache->n_ret = 0;
192 if (free_slots && cache->slots_ret) {
193 slots = cache->slots_ret;
194 cache->slots_ret = NULL;
195 }
196 spin_unlock_irq(&cache->free_lock);
197 if (slots)
Huang Ying54f180d2017-05-08 15:57:40 -0700198 kvfree(slots);
Tim Chen67afa382017-02-22 15:45:39 -0800199 }
200}
201
202static void __drain_swap_slots_cache(unsigned int type)
203{
204 unsigned int cpu;
205
206 /*
207 * This function is called during
208 * 1) swapoff, when we have to make sure no
209 * left over slots are in cache when we remove
210 * a swap device;
211 * 2) disabling of swap slot cache, when we run low
212 * on swap slots when allocating memory and need
213 * to return swap slots to global pool.
214 *
215 * We cannot acquire cpu hot plug lock here as
216 * this function can be invoked in the cpu
217 * hot plug path:
218 * cpu_up -> lock cpu_hotplug -> cpu hotplug state callback
219 * -> memory allocation -> direct reclaim -> get_swap_page
220 * -> drain_swap_slots_cache
221 *
222 * Hence the loop over current online cpu below could miss cpu that
223 * is being brought online but not yet marked as online.
224 * That is okay as we do not schedule and run anything on a
225 * cpu before it has been marked online. Hence, we will not
226 * fill any swap slots in slots cache of such cpu.
227 * There are no slots on such cpu that need to be drained.
228 */
229 for_each_online_cpu(cpu)
230 drain_slots_cache_cpu(cpu, type, false);
231}
232
233static int free_slot_cache(unsigned int cpu)
234{
235 mutex_lock(&swap_slots_cache_mutex);
236 drain_slots_cache_cpu(cpu, SLOTS_CACHE | SLOTS_CACHE_RET, true);
237 mutex_unlock(&swap_slots_cache_mutex);
238 return 0;
239}
240
241int enable_swap_slots_cache(void)
242{
Tim Chen67afa382017-02-22 15:45:39 -0800243 mutex_lock(&swap_slots_cache_enable_mutex);
Zhen Leid69a9572020-08-06 23:20:08 -0700244 if (!swap_slot_cache_initialized) {
245 int ret;
246
247 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "swap_slots_cache",
248 alloc_swap_slot_cache, free_slot_cache);
249 if (WARN_ONCE(ret < 0, "Cache allocation failed (%s), operating "
250 "without swap slots cache.\n", __func__))
251 goto out_unlock;
252
253 swap_slot_cache_initialized = true;
Tim Chen67afa382017-02-22 15:45:39 -0800254 }
255
Tim Chen67afa382017-02-22 15:45:39 -0800256 __reenable_swap_slots_cache();
257out_unlock:
258 mutex_unlock(&swap_slots_cache_enable_mutex);
259 return 0;
260}
261
262/* called with swap slot cache's alloc lock held */
263static int refill_swap_slots_cache(struct swap_slots_cache *cache)
264{
265 if (!use_swap_slot_cache || cache->nr)
266 return 0;
267
268 cache->cur = 0;
269 if (swap_slot_cache_active)
Huang Ying5d5e8f12018-08-21 21:52:20 -0700270 cache->nr = get_swap_pages(SWAP_SLOTS_CACHE_SIZE,
271 cache->slots, 1);
Tim Chen67afa382017-02-22 15:45:39 -0800272
273 return cache->nr;
274}
275
276int free_swap_slot(swp_entry_t entry)
277{
278 struct swap_slots_cache *cache;
279
Sebastian Andrzej Siewiorf07e0f842017-07-10 15:49:29 -0700280 cache = raw_cpu_ptr(&swp_slots);
Tim Chena2e16732017-11-15 17:34:18 -0800281 if (likely(use_swap_slot_cache && cache->slots_ret)) {
Tim Chen67afa382017-02-22 15:45:39 -0800282 spin_lock_irq(&cache->free_lock);
283 /* Swap slots cache may be deactivated before acquiring lock */
Sebastian Andrzej Siewiorf07e0f842017-07-10 15:49:29 -0700284 if (!use_swap_slot_cache || !cache->slots_ret) {
Tim Chen67afa382017-02-22 15:45:39 -0800285 spin_unlock_irq(&cache->free_lock);
286 goto direct_free;
287 }
288 if (cache->n_ret >= SWAP_SLOTS_CACHE_SIZE) {
289 /*
290 * Return slots to global pool.
291 * The current swap_map value is SWAP_HAS_CACHE.
292 * Set it to 0 to indicate it is available for
293 * allocation in global pool
294 */
295 swapcache_free_entries(cache->slots_ret, cache->n_ret);
296 cache->n_ret = 0;
297 }
298 cache->slots_ret[cache->n_ret++] = entry;
299 spin_unlock_irq(&cache->free_lock);
300 } else {
301direct_free:
302 swapcache_free_entries(&entry, 1);
303 }
Tim Chen67afa382017-02-22 15:45:39 -0800304
305 return 0;
306}
307
Huang Ying38d8b4e2017-07-06 15:37:18 -0700308swp_entry_t get_swap_page(struct page *page)
Tim Chen67afa382017-02-22 15:45:39 -0800309{
Wei Yang2406b762020-04-01 21:06:16 -0700310 swp_entry_t entry;
Tim Chen67afa382017-02-22 15:45:39 -0800311 struct swap_slots_cache *cache;
312
Huang Ying38d8b4e2017-07-06 15:37:18 -0700313 entry.val = 0;
314
315 if (PageTransHuge(page)) {
316 if (IS_ENABLED(CONFIG_THP_SWAP))
Huang Ying5d5e8f12018-08-21 21:52:20 -0700317 get_swap_pages(1, &entry, HPAGE_PMD_NR);
Tejun Heobb98f2c2018-06-07 17:05:31 -0700318 goto out;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700319 }
320
Tim Chen67afa382017-02-22 15:45:39 -0800321 /*
322 * Preemption is allowed here, because we may sleep
323 * in refill_swap_slots_cache(). But it is safe, because
324 * accesses to the per-CPU data structure are protected by the
325 * mutex cache->alloc_lock.
326 *
327 * The alloc path here does not touch cache->slots_ret
328 * so cache->free_lock is not taken.
329 */
330 cache = raw_cpu_ptr(&swp_slots);
331
Tim Chena2e16732017-11-15 17:34:18 -0800332 if (likely(check_cache_active() && cache->slots)) {
Tim Chen67afa382017-02-22 15:45:39 -0800333 mutex_lock(&cache->alloc_lock);
334 if (cache->slots) {
335repeat:
336 if (cache->nr) {
Wei Yang2406b762020-04-01 21:06:16 -0700337 entry = cache->slots[cache->cur];
338 cache->slots[cache->cur++].val = 0;
Tim Chen67afa382017-02-22 15:45:39 -0800339 cache->nr--;
Wei Yang2406b762020-04-01 21:06:16 -0700340 } else if (refill_swap_slots_cache(cache)) {
341 goto repeat;
Tim Chen67afa382017-02-22 15:45:39 -0800342 }
343 }
344 mutex_unlock(&cache->alloc_lock);
345 if (entry.val)
Tejun Heobb98f2c2018-06-07 17:05:31 -0700346 goto out;
Tim Chen67afa382017-02-22 15:45:39 -0800347 }
348
Huang Ying5d5e8f12018-08-21 21:52:20 -0700349 get_swap_pages(1, &entry, 1);
Tejun Heobb98f2c2018-06-07 17:05:31 -0700350out:
351 if (mem_cgroup_try_charge_swap(page, entry)) {
352 put_swap_page(page, entry);
353 entry.val = 0;
354 }
Tim Chen67afa382017-02-22 15:45:39 -0800355 return entry;
356}