2 * Manage cache of swap slots to be used for and returned from
5 * Copyright(c) 2016 Intel Corporation.
7 * Author: Tim Chen <tim.c.chen@linux.intel.com>
9 * We allocate the swap slots from the global pool and put
10 * it into local per cpu caches. This has the advantage
11 * of no needing to acquire the swap_info lock every time
14 * There is also opportunity to simply return the slot
15 * to local caches without needing to acquire swap_info
16 * lock. We do not reuse the returned slots directly but
17 * move them back to the global pool in a batch. This
18 * allows the slots to coaellesce and reduce fragmentation.
20 * The swap entry allocated is marked with SWAP_HAS_CACHE
21 * flag in map_count that prevents it from being allocated
22 * again from the global pool.
24 * The swap slots cache is protected by a mutex instead of
25 * a spin lock as when we search for slots with scan_swap_map,
26 * we can possibly sleep.
29 #include <linux/swap_slots.h>
30 #include <linux/cpu.h>
31 #include <linux/cpumask.h>
32 #include <linux/vmalloc.h>
33 #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 DEFINE_MUTEX(swap_slots_cache_mutex);
43 /* Serialize swap slots cache enable/disable operations */
44 DEFINE_MUTEX(swap_slots_cache_enable_mutex);
46 static void __drain_swap_slots_cache(unsigned int type);
47 static void deactivate_swap_slots_cache(void);
48 static void reactivate_swap_slots_cache(void);
50 #define use_swap_slot_cache (swap_slot_cache_active && \
51 swap_slot_cache_enabled && swap_slot_cache_initialized)
52 #define SLOTS_CACHE 0x1
53 #define SLOTS_CACHE_RET 0x2
55 static void deactivate_swap_slots_cache(void)
57 mutex_lock(&swap_slots_cache_mutex);
58 swap_slot_cache_active = false;
59 __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET);
60 mutex_unlock(&swap_slots_cache_mutex);
63 static void reactivate_swap_slots_cache(void)
65 mutex_lock(&swap_slots_cache_mutex);
66 swap_slot_cache_active = true;
67 mutex_unlock(&swap_slots_cache_mutex);
70 /* Must not be called with cpu hot plug lock */
71 void disable_swap_slots_cache_lock(void)
73 mutex_lock(&swap_slots_cache_enable_mutex);
74 swap_slot_cache_enabled = false;
75 if (swap_slot_cache_initialized) {
76 /* serialize with cpu hotplug operations */
78 __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET);
83 static void __reenable_swap_slots_cache(void)
85 swap_slot_cache_enabled = has_usable_swap();
88 void reenable_swap_slots_cache_unlock(void)
90 __reenable_swap_slots_cache();
91 mutex_unlock(&swap_slots_cache_enable_mutex);
94 static bool check_cache_active(void)
98 if (!swap_slot_cache_enabled || !swap_slot_cache_initialized)
101 pages = get_nr_swap_pages();
102 if (!swap_slot_cache_active) {
103 if (pages > num_online_cpus() *
104 THRESHOLD_ACTIVATE_SWAP_SLOTS_CACHE)
105 reactivate_swap_slots_cache();
109 /* if global pool of slot caches too low, deactivate cache */
110 if (pages < num_online_cpus() * THRESHOLD_DEACTIVATE_SWAP_SLOTS_CACHE)
111 deactivate_swap_slots_cache();
113 return swap_slot_cache_active;
116 static int alloc_swap_slot_cache(unsigned int cpu)
118 struct swap_slots_cache *cache;
119 swp_entry_t *slots, *slots_ret;
122 * Do allocation outside swap_slots_cache_mutex
123 * as kvzalloc could trigger reclaim and get_swap_page,
124 * which can lock swap_slots_cache_mutex.
126 slots = kvzalloc(sizeof(swp_entry_t) * SWAP_SLOTS_CACHE_SIZE,
131 slots_ret = kvzalloc(sizeof(swp_entry_t) * SWAP_SLOTS_CACHE_SIZE,
138 mutex_lock(&swap_slots_cache_mutex);
139 cache = &per_cpu(swp_slots, cpu);
140 if (cache->slots || cache->slots_ret)
141 /* cache already allocated */
143 if (!cache->lock_initialized) {
144 mutex_init(&cache->alloc_lock);
145 spin_lock_init(&cache->free_lock);
146 cache->lock_initialized = true;
151 cache->slots = slots;
153 cache->slots_ret = slots_ret;
156 mutex_unlock(&swap_slots_cache_mutex);
164 static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type,
167 struct swap_slots_cache *cache;
168 swp_entry_t *slots = NULL;
170 cache = &per_cpu(swp_slots, cpu);
171 if ((type & SLOTS_CACHE) && cache->slots) {
172 mutex_lock(&cache->alloc_lock);
173 swapcache_free_entries(cache->slots + cache->cur, cache->nr);
176 if (free_slots && cache->slots) {
177 kvfree(cache->slots);
180 mutex_unlock(&cache->alloc_lock);
182 if ((type & SLOTS_CACHE_RET) && cache->slots_ret) {
183 spin_lock_irq(&cache->free_lock);
184 swapcache_free_entries(cache->slots_ret, cache->n_ret);
186 if (free_slots && cache->slots_ret) {
187 slots = cache->slots_ret;
188 cache->slots_ret = NULL;
190 spin_unlock_irq(&cache->free_lock);
196 static void __drain_swap_slots_cache(unsigned int type)
201 * This function is called during
202 * 1) swapoff, when we have to make sure no
203 * left over slots are in cache when we remove
205 * 2) disabling of swap slot cache, when we run low
206 * on swap slots when allocating memory and need
207 * to return swap slots to global pool.
209 * We cannot acquire cpu hot plug lock here as
210 * this function can be invoked in the cpu
212 * cpu_up -> lock cpu_hotplug -> cpu hotplug state callback
213 * -> memory allocation -> direct reclaim -> get_swap_page
214 * -> drain_swap_slots_cache
216 * Hence the loop over current online cpu below could miss cpu that
217 * is being brought online but not yet marked as online.
218 * That is okay as we do not schedule and run anything on a
219 * cpu before it has been marked online. Hence, we will not
220 * fill any swap slots in slots cache of such cpu.
221 * There are no slots on such cpu that need to be drained.
223 for_each_online_cpu(cpu)
224 drain_slots_cache_cpu(cpu, type, false);
227 static int free_slot_cache(unsigned int cpu)
229 mutex_lock(&swap_slots_cache_mutex);
230 drain_slots_cache_cpu(cpu, SLOTS_CACHE | SLOTS_CACHE_RET, true);
231 mutex_unlock(&swap_slots_cache_mutex);
235 int enable_swap_slots_cache(void)
239 mutex_lock(&swap_slots_cache_enable_mutex);
240 if (swap_slot_cache_initialized) {
241 __reenable_swap_slots_cache();
245 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "swap_slots_cache",
246 alloc_swap_slot_cache, free_slot_cache);
247 if (WARN_ONCE(ret < 0, "Cache allocation failed (%s), operating "
248 "without swap slots cache.\n", __func__))
251 swap_slot_cache_initialized = true;
252 __reenable_swap_slots_cache();
254 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, false,
272 int free_swap_slot(swp_entry_t entry)
274 struct swap_slots_cache *cache;
276 cache = raw_cpu_ptr(&swp_slots);
277 if (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)
306 swp_entry_t entry, *pentry;
307 struct swap_slots_cache *cache;
311 if (PageTransHuge(page)) {
312 if (IS_ENABLED(CONFIG_THP_SWAP))
313 get_swap_pages(1, true, &entry);
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 (check_cache_active()) {
329 mutex_lock(&cache->alloc_lock);
333 pentry = &cache->slots[cache->cur++];
338 if (refill_swap_slots_cache(cache))
342 mutex_unlock(&cache->alloc_lock);
347 get_swap_pages(1, false, &entry);
352 #endif /* CONFIG_SWAP */