Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 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 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 Ying | 54f180d | 2017-05-08 15:57:40 -0700 | [diff] [blame] | 35 | #include <linux/mm.h> |
Bing Han | d4eef93 | 2022-05-30 16:28:05 +0800 | [diff] [blame] | 36 | #include <trace/hooks/mm.h> |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 37 | |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 38 | static DEFINE_PER_CPU(struct swap_slots_cache, swp_slots); |
| 39 | static bool swap_slot_cache_active; |
Huang Ying | ba81f83 | 2017-02-22 15:45:46 -0800 | [diff] [blame] | 40 | bool swap_slot_cache_enabled; |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 41 | static bool swap_slot_cache_initialized; |
Colin Ian King | 31f21da | 2018-08-17 15:46:54 -0700 | [diff] [blame] | 42 | static DEFINE_MUTEX(swap_slots_cache_mutex); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 43 | /* Serialize swap slots cache enable/disable operations */ |
Colin Ian King | 31f21da | 2018-08-17 15:46:54 -0700 | [diff] [blame] | 44 | static DEFINE_MUTEX(swap_slots_cache_enable_mutex); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 45 | |
| 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); |
| 49 | |
Zhen Lei | e0f3ebb | 2020-08-06 23:20:11 -0700 | [diff] [blame] | 50 | #define use_swap_slot_cache (swap_slot_cache_active && swap_slot_cache_enabled) |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 51 | #define SLOTS_CACHE 0x1 |
| 52 | #define SLOTS_CACHE_RET 0x2 |
| 53 | |
| 54 | static void deactivate_swap_slots_cache(void) |
| 55 | { |
| 56 | mutex_lock(&swap_slots_cache_mutex); |
| 57 | swap_slot_cache_active = false; |
Bing Han | f6f18f7 | 2022-06-11 16:06:16 +0800 | [diff] [blame] | 58 | trace_android_vh_swap_slot_cache_active(false); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 59 | __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET); |
| 60 | mutex_unlock(&swap_slots_cache_mutex); |
| 61 | } |
| 62 | |
| 63 | static void reactivate_swap_slots_cache(void) |
| 64 | { |
| 65 | mutex_lock(&swap_slots_cache_mutex); |
| 66 | swap_slot_cache_active = true; |
Bing Han | f6f18f7 | 2022-06-11 16:06:16 +0800 | [diff] [blame] | 67 | trace_android_vh_swap_slot_cache_active(true); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 68 | mutex_unlock(&swap_slots_cache_mutex); |
| 69 | } |
| 70 | |
| 71 | /* Must not be called with cpu hot plug lock */ |
| 72 | void 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 | |
| 84 | static void __reenable_swap_slots_cache(void) |
| 85 | { |
| 86 | swap_slot_cache_enabled = has_usable_swap(); |
| 87 | } |
| 88 | |
| 89 | void reenable_swap_slots_cache_unlock(void) |
| 90 | { |
| 91 | __reenable_swap_slots_cache(); |
| 92 | mutex_unlock(&swap_slots_cache_enable_mutex); |
| 93 | } |
| 94 | |
Bing Han | e064059 | 2022-05-30 16:39:40 +0800 | [diff] [blame^] | 95 | bool is_swap_slot_cache_enabled(void) |
| 96 | { |
| 97 | return swap_slot_cache_enabled; |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(is_swap_slot_cache_enabled); |
| 100 | |
Bing Han | 06c2766 | 2022-06-10 19:40:35 +0800 | [diff] [blame] | 101 | bool check_cache_active(void) |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 102 | { |
| 103 | long pages; |
| 104 | |
Zhen Lei | e0f3ebb | 2020-08-06 23:20:11 -0700 | [diff] [blame] | 105 | if (!swap_slot_cache_enabled) |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 106 | return false; |
| 107 | |
| 108 | pages = get_nr_swap_pages(); |
| 109 | if (!swap_slot_cache_active) { |
| 110 | if (pages > num_online_cpus() * |
| 111 | THRESHOLD_ACTIVATE_SWAP_SLOTS_CACHE) |
| 112 | reactivate_swap_slots_cache(); |
| 113 | goto out; |
| 114 | } |
| 115 | |
| 116 | /* if global pool of slot caches too low, deactivate cache */ |
| 117 | if (pages < num_online_cpus() * THRESHOLD_DEACTIVATE_SWAP_SLOTS_CACHE) |
| 118 | deactivate_swap_slots_cache(); |
| 119 | out: |
| 120 | return swap_slot_cache_active; |
| 121 | } |
Bing Han | 06c2766 | 2022-06-10 19:40:35 +0800 | [diff] [blame] | 122 | EXPORT_SYMBOL_GPL(check_cache_active); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 123 | |
| 124 | static int alloc_swap_slot_cache(unsigned int cpu) |
| 125 | { |
| 126 | struct swap_slots_cache *cache; |
| 127 | swp_entry_t *slots, *slots_ret; |
Bing Han | d4eef93 | 2022-05-30 16:28:05 +0800 | [diff] [blame] | 128 | bool skip = false; |
| 129 | int ret = 0; |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * Do allocation outside swap_slots_cache_mutex |
Huang Ying | 54f180d | 2017-05-08 15:57:40 -0700 | [diff] [blame] | 133 | * as kvzalloc could trigger reclaim and get_swap_page, |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 134 | * which can lock swap_slots_cache_mutex. |
| 135 | */ |
Bing Han | d4eef93 | 2022-05-30 16:28:05 +0800 | [diff] [blame] | 136 | trace_android_vh_alloc_swap_slot_cache(&per_cpu(swp_slots, cpu), |
| 137 | &ret, &skip); |
| 138 | if (skip) |
| 139 | return ret; |
Kees Cook | 778e1cd | 2018-06-12 14:04:48 -0700 | [diff] [blame] | 140 | slots = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t), |
Huang Ying | 54f180d | 2017-05-08 15:57:40 -0700 | [diff] [blame] | 141 | GFP_KERNEL); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 142 | if (!slots) |
| 143 | return -ENOMEM; |
| 144 | |
Kees Cook | 778e1cd | 2018-06-12 14:04:48 -0700 | [diff] [blame] | 145 | slots_ret = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t), |
Huang Ying | 54f180d | 2017-05-08 15:57:40 -0700 | [diff] [blame] | 146 | GFP_KERNEL); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 147 | if (!slots_ret) { |
Huang Ying | 54f180d | 2017-05-08 15:57:40 -0700 | [diff] [blame] | 148 | kvfree(slots); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 149 | return -ENOMEM; |
| 150 | } |
| 151 | |
| 152 | mutex_lock(&swap_slots_cache_mutex); |
| 153 | cache = &per_cpu(swp_slots, cpu); |
Zhen Lei | f90eae2 | 2020-08-06 23:20:05 -0700 | [diff] [blame] | 154 | if (cache->slots || cache->slots_ret) { |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 155 | /* cache already allocated */ |
Zhen Lei | f90eae2 | 2020-08-06 23:20:05 -0700 | [diff] [blame] | 156 | mutex_unlock(&swap_slots_cache_mutex); |
| 157 | |
| 158 | kvfree(slots); |
| 159 | kvfree(slots_ret); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 164 | if (!cache->lock_initialized) { |
| 165 | mutex_init(&cache->alloc_lock); |
| 166 | spin_lock_init(&cache->free_lock); |
| 167 | cache->lock_initialized = true; |
| 168 | } |
| 169 | cache->nr = 0; |
| 170 | cache->cur = 0; |
| 171 | cache->n_ret = 0; |
Tim Chen | a2e1673 | 2017-11-15 17:34:18 -0800 | [diff] [blame] | 172 | /* |
| 173 | * We initialized alloc_lock and free_lock earlier. We use |
| 174 | * !cache->slots or !cache->slots_ret to know if it is safe to acquire |
| 175 | * the corresponding lock and use the cache. Memory barrier below |
| 176 | * ensures the assumption. |
| 177 | */ |
| 178 | mb(); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 179 | cache->slots = slots; |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 180 | cache->slots_ret = slots_ret; |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 181 | mutex_unlock(&swap_slots_cache_mutex); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type, |
| 186 | bool free_slots) |
| 187 | { |
| 188 | struct swap_slots_cache *cache; |
| 189 | swp_entry_t *slots = NULL; |
Bing Han | d4eef93 | 2022-05-30 16:28:05 +0800 | [diff] [blame] | 190 | bool skip = false; |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 191 | |
| 192 | cache = &per_cpu(swp_slots, cpu); |
Bing Han | d4eef93 | 2022-05-30 16:28:05 +0800 | [diff] [blame] | 193 | trace_android_vh_drain_slots_cache_cpu(cache, type, |
| 194 | free_slots, &skip); |
| 195 | if (skip) |
| 196 | return; |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 197 | if ((type & SLOTS_CACHE) && cache->slots) { |
| 198 | mutex_lock(&cache->alloc_lock); |
| 199 | swapcache_free_entries(cache->slots + cache->cur, cache->nr); |
| 200 | cache->cur = 0; |
| 201 | cache->nr = 0; |
| 202 | if (free_slots && cache->slots) { |
Huang Ying | 54f180d | 2017-05-08 15:57:40 -0700 | [diff] [blame] | 203 | kvfree(cache->slots); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 204 | cache->slots = NULL; |
| 205 | } |
| 206 | mutex_unlock(&cache->alloc_lock); |
| 207 | } |
| 208 | if ((type & SLOTS_CACHE_RET) && cache->slots_ret) { |
| 209 | spin_lock_irq(&cache->free_lock); |
| 210 | swapcache_free_entries(cache->slots_ret, cache->n_ret); |
| 211 | cache->n_ret = 0; |
| 212 | if (free_slots && cache->slots_ret) { |
| 213 | slots = cache->slots_ret; |
| 214 | cache->slots_ret = NULL; |
| 215 | } |
| 216 | spin_unlock_irq(&cache->free_lock); |
| 217 | if (slots) |
Huang Ying | 54f180d | 2017-05-08 15:57:40 -0700 | [diff] [blame] | 218 | kvfree(slots); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | static void __drain_swap_slots_cache(unsigned int type) |
| 223 | { |
| 224 | unsigned int cpu; |
| 225 | |
| 226 | /* |
| 227 | * This function is called during |
| 228 | * 1) swapoff, when we have to make sure no |
| 229 | * left over slots are in cache when we remove |
| 230 | * a swap device; |
| 231 | * 2) disabling of swap slot cache, when we run low |
| 232 | * on swap slots when allocating memory and need |
| 233 | * to return swap slots to global pool. |
| 234 | * |
| 235 | * We cannot acquire cpu hot plug lock here as |
| 236 | * this function can be invoked in the cpu |
| 237 | * hot plug path: |
| 238 | * cpu_up -> lock cpu_hotplug -> cpu hotplug state callback |
| 239 | * -> memory allocation -> direct reclaim -> get_swap_page |
| 240 | * -> drain_swap_slots_cache |
| 241 | * |
| 242 | * Hence the loop over current online cpu below could miss cpu that |
| 243 | * is being brought online but not yet marked as online. |
| 244 | * That is okay as we do not schedule and run anything on a |
| 245 | * cpu before it has been marked online. Hence, we will not |
| 246 | * fill any swap slots in slots cache of such cpu. |
| 247 | * There are no slots on such cpu that need to be drained. |
| 248 | */ |
| 249 | for_each_online_cpu(cpu) |
| 250 | drain_slots_cache_cpu(cpu, type, false); |
| 251 | } |
| 252 | |
| 253 | static int free_slot_cache(unsigned int cpu) |
| 254 | { |
| 255 | mutex_lock(&swap_slots_cache_mutex); |
| 256 | drain_slots_cache_cpu(cpu, SLOTS_CACHE | SLOTS_CACHE_RET, true); |
| 257 | mutex_unlock(&swap_slots_cache_mutex); |
| 258 | return 0; |
| 259 | } |
| 260 | |
Miaohe Lin | f3bc52c | 2020-10-13 16:52:18 -0700 | [diff] [blame] | 261 | void enable_swap_slots_cache(void) |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 262 | { |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 263 | mutex_lock(&swap_slots_cache_enable_mutex); |
Zhen Lei | d69a957 | 2020-08-06 23:20:08 -0700 | [diff] [blame] | 264 | if (!swap_slot_cache_initialized) { |
| 265 | int ret; |
| 266 | |
| 267 | ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "swap_slots_cache", |
| 268 | alloc_swap_slot_cache, free_slot_cache); |
| 269 | if (WARN_ONCE(ret < 0, "Cache allocation failed (%s), operating " |
| 270 | "without swap slots cache.\n", __func__)) |
| 271 | goto out_unlock; |
| 272 | |
| 273 | swap_slot_cache_initialized = true; |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 276 | __reenable_swap_slots_cache(); |
| 277 | out_unlock: |
| 278 | mutex_unlock(&swap_slots_cache_enable_mutex); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /* called with swap slot cache's alloc lock held */ |
| 282 | static int refill_swap_slots_cache(struct swap_slots_cache *cache) |
| 283 | { |
| 284 | if (!use_swap_slot_cache || cache->nr) |
| 285 | return 0; |
| 286 | |
| 287 | cache->cur = 0; |
| 288 | if (swap_slot_cache_active) |
Huang Ying | 5d5e8f1 | 2018-08-21 21:52:20 -0700 | [diff] [blame] | 289 | cache->nr = get_swap_pages(SWAP_SLOTS_CACHE_SIZE, |
| 290 | cache->slots, 1); |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 291 | |
| 292 | return cache->nr; |
| 293 | } |
| 294 | |
| 295 | int free_swap_slot(swp_entry_t entry) |
| 296 | { |
| 297 | struct swap_slots_cache *cache; |
Bing Han | 7222a0b | 2022-06-10 18:45:31 +0800 | [diff] [blame] | 298 | bool skip = false; |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 299 | |
Sebastian Andrzej Siewior | f07e0f84 | 2017-07-10 15:49:29 -0700 | [diff] [blame] | 300 | cache = raw_cpu_ptr(&swp_slots); |
Bing Han | 7222a0b | 2022-06-10 18:45:31 +0800 | [diff] [blame] | 301 | trace_android_vh_free_swap_slot(entry, cache, &skip); |
| 302 | if (skip) |
| 303 | return 0; |
Tim Chen | a2e1673 | 2017-11-15 17:34:18 -0800 | [diff] [blame] | 304 | if (likely(use_swap_slot_cache && cache->slots_ret)) { |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 305 | spin_lock_irq(&cache->free_lock); |
| 306 | /* Swap slots cache may be deactivated before acquiring lock */ |
Sebastian Andrzej Siewior | f07e0f84 | 2017-07-10 15:49:29 -0700 | [diff] [blame] | 307 | if (!use_swap_slot_cache || !cache->slots_ret) { |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 308 | spin_unlock_irq(&cache->free_lock); |
| 309 | goto direct_free; |
| 310 | } |
| 311 | if (cache->n_ret >= SWAP_SLOTS_CACHE_SIZE) { |
| 312 | /* |
| 313 | * Return slots to global pool. |
| 314 | * The current swap_map value is SWAP_HAS_CACHE. |
| 315 | * Set it to 0 to indicate it is available for |
| 316 | * allocation in global pool |
| 317 | */ |
| 318 | swapcache_free_entries(cache->slots_ret, cache->n_ret); |
| 319 | cache->n_ret = 0; |
| 320 | } |
| 321 | cache->slots_ret[cache->n_ret++] = entry; |
| 322 | spin_unlock_irq(&cache->free_lock); |
| 323 | } else { |
| 324 | direct_free: |
| 325 | swapcache_free_entries(&entry, 1); |
| 326 | } |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 331 | swp_entry_t get_swap_page(struct page *page) |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 332 | { |
Wei Yang | 2406b76 | 2020-04-01 21:06:16 -0700 | [diff] [blame] | 333 | swp_entry_t entry; |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 334 | struct swap_slots_cache *cache; |
Bing Han | d4eef93 | 2022-05-30 16:28:05 +0800 | [diff] [blame] | 335 | bool found = false; |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 336 | entry.val = 0; |
| 337 | |
Bing Han | d4eef93 | 2022-05-30 16:28:05 +0800 | [diff] [blame] | 338 | trace_android_vh_get_swap_page(page, &entry, raw_cpu_ptr(&swp_slots), &found); |
| 339 | if (found) |
| 340 | goto out; |
| 341 | |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 342 | if (PageTransHuge(page)) { |
| 343 | if (IS_ENABLED(CONFIG_THP_SWAP)) |
Huang Ying | 5d5e8f1 | 2018-08-21 21:52:20 -0700 | [diff] [blame] | 344 | get_swap_pages(1, &entry, HPAGE_PMD_NR); |
Tejun Heo | bb98f2c | 2018-06-07 17:05:31 -0700 | [diff] [blame] | 345 | goto out; |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 348 | /* |
| 349 | * Preemption is allowed here, because we may sleep |
| 350 | * in refill_swap_slots_cache(). But it is safe, because |
| 351 | * accesses to the per-CPU data structure are protected by the |
| 352 | * mutex cache->alloc_lock. |
| 353 | * |
| 354 | * The alloc path here does not touch cache->slots_ret |
| 355 | * so cache->free_lock is not taken. |
| 356 | */ |
| 357 | cache = raw_cpu_ptr(&swp_slots); |
| 358 | |
Tim Chen | a2e1673 | 2017-11-15 17:34:18 -0800 | [diff] [blame] | 359 | if (likely(check_cache_active() && cache->slots)) { |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 360 | mutex_lock(&cache->alloc_lock); |
| 361 | if (cache->slots) { |
| 362 | repeat: |
| 363 | if (cache->nr) { |
Wei Yang | 2406b76 | 2020-04-01 21:06:16 -0700 | [diff] [blame] | 364 | entry = cache->slots[cache->cur]; |
| 365 | cache->slots[cache->cur++].val = 0; |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 366 | cache->nr--; |
Wei Yang | 2406b76 | 2020-04-01 21:06:16 -0700 | [diff] [blame] | 367 | } else if (refill_swap_slots_cache(cache)) { |
| 368 | goto repeat; |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | mutex_unlock(&cache->alloc_lock); |
| 372 | if (entry.val) |
Tejun Heo | bb98f2c | 2018-06-07 17:05:31 -0700 | [diff] [blame] | 373 | goto out; |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Huang Ying | 5d5e8f1 | 2018-08-21 21:52:20 -0700 | [diff] [blame] | 376 | get_swap_pages(1, &entry, 1); |
Tejun Heo | bb98f2c | 2018-06-07 17:05:31 -0700 | [diff] [blame] | 377 | out: |
| 378 | if (mem_cgroup_try_charge_swap(page, entry)) { |
| 379 | put_swap_page(page, entry); |
| 380 | entry.val = 0; |
| 381 | } |
Tim Chen | 67afa38 | 2017-02-22 15:45:39 -0800 | [diff] [blame] | 382 | return entry; |
| 383 | } |