1 // SPDX-License-Identifier: GPL-2.0 2 /* 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 coalesce 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/slab.h> 34 #include <linux/vmalloc.h> 35 #include <linux/mutex.h> 36 #include <linux/mm.h> 37 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); 45 46 static void __drain_swap_slots_cache(void); 47 48 #define use_swap_slot_cache (swap_slot_cache_active && swap_slot_cache_enabled) 49 50 static void deactivate_swap_slots_cache(void) 51 { 52 mutex_lock(&swap_slots_cache_mutex); 53 swap_slot_cache_active = false; 54 __drain_swap_slots_cache(); 55 mutex_unlock(&swap_slots_cache_mutex); 56 } 57 58 static void reactivate_swap_slots_cache(void) 59 { 60 mutex_lock(&swap_slots_cache_mutex); 61 swap_slot_cache_active = true; 62 mutex_unlock(&swap_slots_cache_mutex); 63 } 64 65 /* Must not be called with cpu hot plug lock */ 66 void disable_swap_slots_cache_lock(void) 67 { 68 mutex_lock(&swap_slots_cache_enable_mutex); 69 swap_slot_cache_enabled = false; 70 if (swap_slot_cache_initialized) { 71 /* serialize with cpu hotplug operations */ 72 cpus_read_lock(); 73 __drain_swap_slots_cache(); 74 cpus_read_unlock(); 75 } 76 } 77 78 static void __reenable_swap_slots_cache(void) 79 { 80 swap_slot_cache_enabled = has_usable_swap(); 81 } 82 83 void reenable_swap_slots_cache_unlock(void) 84 { 85 __reenable_swap_slots_cache(); 86 mutex_unlock(&swap_slots_cache_enable_mutex); 87 } 88 89 static bool check_cache_active(void) 90 { 91 long pages; 92 93 if (!swap_slot_cache_enabled) 94 return false; 95 96 pages = get_nr_swap_pages(); 97 if (!swap_slot_cache_active) { 98 if (pages > num_online_cpus() * 99 THRESHOLD_ACTIVATE_SWAP_SLOTS_CACHE) 100 reactivate_swap_slots_cache(); 101 goto out; 102 } 103 104 /* if global pool of slot caches too low, deactivate cache */ 105 if (pages < num_online_cpus() * THRESHOLD_DEACTIVATE_SWAP_SLOTS_CACHE) 106 deactivate_swap_slots_cache(); 107 out: 108 return swap_slot_cache_active; 109 } 110 111 static int alloc_swap_slot_cache(unsigned int cpu) 112 { 113 struct swap_slots_cache *cache; 114 swp_entry_t *slots; 115 116 /* 117 * Do allocation outside swap_slots_cache_mutex 118 * as kvzalloc could trigger reclaim and folio_alloc_swap, 119 * which can lock swap_slots_cache_mutex. 120 */ 121 slots = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t), 122 GFP_KERNEL); 123 if (!slots) 124 return -ENOMEM; 125 126 mutex_lock(&swap_slots_cache_mutex); 127 cache = &per_cpu(swp_slots, cpu); 128 if (cache->slots) { 129 /* cache already allocated */ 130 mutex_unlock(&swap_slots_cache_mutex); 131 132 kvfree(slots); 133 134 return 0; 135 } 136 137 if (!cache->lock_initialized) { 138 mutex_init(&cache->alloc_lock); 139 cache->lock_initialized = true; 140 } 141 cache->nr = 0; 142 cache->cur = 0; 143 cache->n_ret = 0; 144 /* 145 * We initialized alloc_lock and free_lock earlier. We use 146 * !cache->slots or !cache->slots_ret to know if it is safe to acquire 147 * the corresponding lock and use the cache. Memory barrier below 148 * ensures the assumption. 149 */ 150 mb(); 151 cache->slots = slots; 152 mutex_unlock(&swap_slots_cache_mutex); 153 return 0; 154 } 155 156 static void drain_slots_cache_cpu(unsigned int cpu, bool free_slots) 157 { 158 struct swap_slots_cache *cache; 159 160 cache = &per_cpu(swp_slots, cpu); 161 if (cache->slots) { 162 mutex_lock(&cache->alloc_lock); 163 swapcache_free_entries(cache->slots + cache->cur, cache->nr); 164 cache->cur = 0; 165 cache->nr = 0; 166 if (free_slots && cache->slots) { 167 kvfree(cache->slots); 168 cache->slots = NULL; 169 } 170 mutex_unlock(&cache->alloc_lock); 171 } 172 } 173 174 static void __drain_swap_slots_cache(void) 175 { 176 unsigned int cpu; 177 178 /* 179 * This function is called during 180 * 1) swapoff, when we have to make sure no 181 * left over slots are in cache when we remove 182 * a swap device; 183 * 2) disabling of swap slot cache, when we run low 184 * on swap slots when allocating memory and need 185 * to return swap slots to global pool. 186 * 187 * We cannot acquire cpu hot plug lock here as 188 * this function can be invoked in the cpu 189 * hot plug path: 190 * cpu_up -> lock cpu_hotplug -> cpu hotplug state callback 191 * -> memory allocation -> direct reclaim -> folio_alloc_swap 192 * -> drain_swap_slots_cache 193 * 194 * Hence the loop over current online cpu below could miss cpu that 195 * is being brought online but not yet marked as online. 196 * That is okay as we do not schedule and run anything on a 197 * cpu before it has been marked online. Hence, we will not 198 * fill any swap slots in slots cache of such cpu. 199 * There are no slots on such cpu that need to be drained. 200 */ 201 for_each_online_cpu(cpu) 202 drain_slots_cache_cpu(cpu, false); 203 } 204 205 static int free_slot_cache(unsigned int cpu) 206 { 207 mutex_lock(&swap_slots_cache_mutex); 208 drain_slots_cache_cpu(cpu, true); 209 mutex_unlock(&swap_slots_cache_mutex); 210 return 0; 211 } 212 213 void enable_swap_slots_cache(void) 214 { 215 mutex_lock(&swap_slots_cache_enable_mutex); 216 if (!swap_slot_cache_initialized) { 217 int ret; 218 219 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "swap_slots_cache", 220 alloc_swap_slot_cache, free_slot_cache); 221 if (WARN_ONCE(ret < 0, "Cache allocation failed (%s), operating " 222 "without swap slots cache.\n", __func__)) 223 goto out_unlock; 224 225 swap_slot_cache_initialized = true; 226 } 227 228 __reenable_swap_slots_cache(); 229 out_unlock: 230 mutex_unlock(&swap_slots_cache_enable_mutex); 231 } 232 233 /* called with swap slot cache's alloc lock held */ 234 static int refill_swap_slots_cache(struct swap_slots_cache *cache) 235 { 236 if (!use_swap_slot_cache) 237 return 0; 238 239 cache->cur = 0; 240 if (swap_slot_cache_active) 241 cache->nr = get_swap_pages(SWAP_SLOTS_CACHE_SIZE, 242 cache->slots, 0); 243 244 return cache->nr; 245 } 246 247 swp_entry_t folio_alloc_swap(struct folio *folio) 248 { 249 swp_entry_t entry; 250 struct swap_slots_cache *cache; 251 252 entry.val = 0; 253 254 if (folio_test_large(folio)) { 255 if (IS_ENABLED(CONFIG_THP_SWAP)) 256 get_swap_pages(1, &entry, folio_order(folio)); 257 goto out; 258 } 259 260 /* 261 * Preemption is allowed here, because we may sleep 262 * in refill_swap_slots_cache(). But it is safe, because 263 * accesses to the per-CPU data structure are protected by the 264 * mutex cache->alloc_lock. 265 * 266 * The alloc path here does not touch cache->slots_ret 267 * so cache->free_lock is not taken. 268 */ 269 cache = raw_cpu_ptr(&swp_slots); 270 271 if (likely(check_cache_active() && cache->slots)) { 272 mutex_lock(&cache->alloc_lock); 273 if (cache->slots) { 274 repeat: 275 if (cache->nr) { 276 entry = cache->slots[cache->cur]; 277 cache->slots[cache->cur++].val = 0; 278 cache->nr--; 279 } else if (refill_swap_slots_cache(cache)) { 280 goto repeat; 281 } 282 } 283 mutex_unlock(&cache->alloc_lock); 284 if (entry.val) 285 goto out; 286 } 287 288 get_swap_pages(1, &entry, 0); 289 out: 290 if (mem_cgroup_try_charge_swap(folio, entry)) { 291 put_swap_folio(folio, entry); 292 entry.val = 0; 293 } 294 return entry; 295 } 296