swap_slots.c 9.4 KB

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