blob: 86ade667a7af67a6020fa5f6f380fcf8ac3d0033 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/swapfile.c
4 *
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
6 * Swap reorganised 29.12.95, Stephen Tweedie
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/mm.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010010#include <linux/sched/mm.h>
Ingo Molnar29930022017-02-08 18:51:36 +010011#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/hugetlb.h>
13#include <linux/mman.h>
14#include <linux/slab.h>
15#include <linux/kernel_stat.h>
16#include <linux/swap.h>
17#include <linux/vmalloc.h>
18#include <linux/pagemap.h>
19#include <linux/namei.h>
Hugh Dickins072441e2011-06-27 16:18:02 -070020#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/blkdev.h>
Hugh Dickins20137a42009-01-06 14:39:54 -080022#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/writeback.h>
24#include <linux/proc_fs.h>
25#include <linux/seq_file.h>
26#include <linux/init.h>
Hugh Dickins5ad64682009-12-14 17:59:24 -080027#include <linux/ksm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/rmap.h>
29#include <linux/security.h>
30#include <linux/backing-dev.h>
Ingo Molnarfc0abb12006-01-18 17:42:33 -080031#include <linux/mutex.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080032#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/syscalls.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080034#include <linux/memcontrol.h>
Kay Sievers66d7dd52010-10-26 14:22:06 -070035#include <linux/poll.h>
David Rientjes72788c32011-05-24 17:11:40 -070036#include <linux/oom.h>
Dan Magenheimer38b5faf2012-04-09 17:08:06 -060037#include <linux/frontswap.h>
38#include <linux/swapfile.h>
Mel Gormanf981c592012-07-31 16:44:47 -070039#include <linux/export.h>
Tim Chen67afa382017-02-22 15:45:39 -080040#include <linux/swap_slots.h>
Huang Ying155b5f82017-07-06 15:40:31 -070041#include <linux/sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/tlbflush.h>
44#include <linux/swapops.h>
Johannes Weiner5d1ea482014-12-10 15:44:55 -080045#include <linux/swap_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Hugh Dickins570a335b2009-12-14 17:58:46 -080047static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
48 unsigned char);
49static void free_swap_count_continuations(struct swap_info_struct *);
Lee Schermerhornd4906e12009-12-14 17:58:49 -080050static sector_t map_swap_entry(swp_entry_t, struct block_device**);
Hugh Dickins570a335b2009-12-14 17:58:46 -080051
Dan Magenheimer38b5faf2012-04-09 17:08:06 -060052DEFINE_SPINLOCK(swap_lock);
Adrian Bunk7c363b82008-07-25 19:46:24 -070053static unsigned int nr_swapfiles;
Shaohua Liec8acf22013-02-22 16:34:38 -080054atomic_long_t nr_swap_pages;
Chris Wilsonfb0fec52015-12-04 15:58:53 +000055/*
56 * Some modules use swappable objects and may try to swap them out under
57 * memory pressure (via the shrinker). Before doing so, they may wish to
58 * check to see if any swap space is available.
59 */
60EXPORT_SYMBOL_GPL(nr_swap_pages);
Shaohua Liec8acf22013-02-22 16:34:38 -080061/* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062long total_swap_pages;
Aaron Lua2468cc2017-09-06 16:24:57 -070063static int least_priority = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static const char Bad_file[] = "Bad swap file entry ";
66static const char Unused_file[] = "Unused swap file entry ";
67static const char Bad_offset[] = "Bad swap offset entry ";
68static const char Unused_offset[] = "Unused swap offset entry ";
69
Dan Streetmanadfab832014-06-04 16:09:53 -070070/*
71 * all active swap_info_structs
72 * protected with swap_lock, and ordered by priority.
73 */
Dan Streetman18ab4d42014-06-04 16:09:59 -070074PLIST_HEAD(swap_active_head);
75
76/*
77 * all available (active, not full) swap_info_structs
78 * protected with swap_avail_lock, ordered by priority.
79 * This is used by get_swap_page() instead of swap_active_head
80 * because swap_active_head includes all swap_info_structs,
81 * but get_swap_page() doesn't need to look at full ones.
82 * This uses its own lock instead of swap_lock because when a
83 * swap_info_struct changes between not-full/full, it needs to
84 * add/remove itself to/from this list, but the swap_info_struct->lock
85 * is held and the locking order requires swap_lock to be taken
86 * before any swap_info_struct->lock.
87 */
Colin Ian Kingbfc6b1c2018-04-10 16:29:55 -070088static struct plist_head *swap_avail_heads;
Dan Streetman18ab4d42014-06-04 16:09:59 -070089static DEFINE_SPINLOCK(swap_avail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Dan Magenheimer38b5faf2012-04-09 17:08:06 -060091struct swap_info_struct *swap_info[MAX_SWAPFILES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Ingo Molnarfc0abb12006-01-18 17:42:33 -080093static DEFINE_MUTEX(swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Kay Sievers66d7dd52010-10-26 14:22:06 -070095static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
96/* Activity counter to indicate that a swapon or swapoff has occurred */
97static atomic_t proc_poll_event = ATOMIC_INIT(0);
98
Huang Ying81a02982017-09-06 16:24:43 -070099atomic_t nr_rotate_swap = ATOMIC_INIT(0);
100
Daniel Jordanc10d38c2019-03-05 15:48:19 -0800101static struct swap_info_struct *swap_type_to_swap_info(int type)
102{
103 if (type >= READ_ONCE(nr_swapfiles))
104 return NULL;
105
106 smp_rmb(); /* Pairs with smp_wmb in alloc_swap_info. */
107 return READ_ONCE(swap_info[type]);
108}
109
Hugh Dickins8d69aae2009-12-14 17:58:45 -0800110static inline unsigned char swap_count(unsigned char ent)
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700111{
Daniel Jordan955c97f2018-06-14 15:26:21 -0700112 return ent & ~SWAP_HAS_CACHE; /* may include COUNT_CONTINUED flag */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700113}
114
Huang Yingbcd49e82018-10-26 15:03:46 -0700115/* Reclaim the swap entry anyway if possible */
116#define TTRS_ANYWAY 0x1
117/*
118 * Reclaim the swap entry if there are no more mappings of the
119 * corresponding page
120 */
121#define TTRS_UNMAPPED 0x2
122/* Reclaim the swap entry if swap is getting full*/
123#define TTRS_FULL 0x4
124
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800125/* returns 1 if swap entry is freed */
Huang Yingbcd49e82018-10-26 15:03:46 -0700126static int __try_to_reclaim_swap(struct swap_info_struct *si,
127 unsigned long offset, unsigned long flags)
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700128{
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800129 swp_entry_t entry = swp_entry(si->type, offset);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700130 struct page *page;
131 int ret = 0;
132
Huang Yingbcd49e82018-10-26 15:03:46 -0700133 page = find_get_page(swap_address_space(entry), offset);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700134 if (!page)
135 return 0;
136 /*
Huang Yingbcd49e82018-10-26 15:03:46 -0700137 * When this function is called from scan_swap_map_slots() and it's
138 * called by vmscan.c at reclaiming pages. So, we hold a lock on a page,
139 * here. We have to use trylock for avoiding deadlock. This is a special
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700140 * case and you should use try_to_free_swap() with explicit lock_page()
141 * in usual operations.
142 */
143 if (trylock_page(page)) {
Huang Yingbcd49e82018-10-26 15:03:46 -0700144 if ((flags & TTRS_ANYWAY) ||
145 ((flags & TTRS_UNMAPPED) && !page_mapped(page)) ||
146 ((flags & TTRS_FULL) && mem_cgroup_swap_full(page)))
147 ret = try_to_free_swap(page);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700148 unlock_page(page);
149 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300150 put_page(page);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700151 return ret;
152}
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700153
Aaron Lu4efaceb2019-07-11 20:55:41 -0700154static inline struct swap_extent *first_se(struct swap_info_struct *sis)
155{
156 struct rb_node *rb = rb_first(&sis->swap_extent_root);
157 return rb_entry(rb, struct swap_extent, rb_node);
158}
159
160static inline struct swap_extent *next_se(struct swap_extent *se)
161{
162 struct rb_node *rb = rb_next(&se->rb_node);
163 return rb ? rb_entry(rb, struct swap_extent, rb_node) : NULL;
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800167 * swapon tell device that all the old swap contents can be discarded,
168 * to allow the swap device to optimize its wear-levelling.
169 */
170static int discard_swap(struct swap_info_struct *si)
171{
172 struct swap_extent *se;
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800173 sector_t start_block;
174 sector_t nr_blocks;
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800175 int err = 0;
176
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800177 /* Do not discard the swap header page! */
Aaron Lu4efaceb2019-07-11 20:55:41 -0700178 se = first_se(si);
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800179 start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
180 nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
181 if (nr_blocks) {
182 err = blkdev_issue_discard(si->bdev, start_block,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200183 nr_blocks, GFP_KERNEL, 0);
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800184 if (err)
185 return err;
186 cond_resched();
187 }
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800188
Aaron Lu4efaceb2019-07-11 20:55:41 -0700189 for (se = next_se(se); se; se = next_se(se)) {
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800190 start_block = se->start_block << (PAGE_SHIFT - 9);
191 nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800192
193 err = blkdev_issue_discard(si->bdev, start_block,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200194 nr_blocks, GFP_KERNEL, 0);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800195 if (err)
196 break;
197
198 cond_resched();
199 }
200 return err; /* That will often be -EOPNOTSUPP */
201}
202
Aaron Lu4efaceb2019-07-11 20:55:41 -0700203static struct swap_extent *
204offset_to_swap_extent(struct swap_info_struct *sis, unsigned long offset)
205{
206 struct swap_extent *se;
207 struct rb_node *rb;
208
209 rb = sis->swap_extent_root.rb_node;
210 while (rb) {
211 se = rb_entry(rb, struct swap_extent, rb_node);
212 if (offset < se->start_page)
213 rb = rb->rb_left;
214 else if (offset >= se->start_page + se->nr_pages)
215 rb = rb->rb_right;
216 else
217 return se;
218 }
219 /* It *must* be present */
220 BUG();
221}
222
Jens Axboe04b049a2021-03-02 14:53:21 -0700223sector_t swap_page_sector(struct page *page)
224{
225 struct swap_info_struct *sis = page_swap_info(page);
226 struct swap_extent *se;
227 sector_t sector;
228 pgoff_t offset;
229
230 offset = __page_file_index(page);
231 se = offset_to_swap_extent(sis, offset);
232 sector = se->start_block + (offset - se->start_page);
233 return sector << (PAGE_SHIFT - 9);
234}
235
Hugh Dickins7992fde2009-01-06 14:39:53 -0800236/*
237 * swap allocation tell device that a cluster of swap can now be discarded,
238 * to allow the swap device to optimize its wear-levelling.
239 */
240static void discard_swap_cluster(struct swap_info_struct *si,
241 pgoff_t start_page, pgoff_t nr_pages)
242{
Aaron Lu4efaceb2019-07-11 20:55:41 -0700243 struct swap_extent *se = offset_to_swap_extent(si, start_page);
Hugh Dickins7992fde2009-01-06 14:39:53 -0800244
245 while (nr_pages) {
Aaron Lu4efaceb2019-07-11 20:55:41 -0700246 pgoff_t offset = start_page - se->start_page;
247 sector_t start_block = se->start_block + offset;
248 sector_t nr_blocks = se->nr_pages - offset;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800249
Aaron Lu4efaceb2019-07-11 20:55:41 -0700250 if (nr_blocks > nr_pages)
251 nr_blocks = nr_pages;
252 start_page += nr_blocks;
253 nr_pages -= nr_blocks;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800254
Aaron Lu4efaceb2019-07-11 20:55:41 -0700255 start_block <<= PAGE_SHIFT - 9;
256 nr_blocks <<= PAGE_SHIFT - 9;
257 if (blkdev_issue_discard(si->bdev, start_block,
258 nr_blocks, GFP_NOIO, 0))
259 break;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800260
Aaron Lu4efaceb2019-07-11 20:55:41 -0700261 se = next_se(se);
Hugh Dickins7992fde2009-01-06 14:39:53 -0800262 }
263}
264
Huang Ying38d8b4e2017-07-06 15:37:18 -0700265#ifdef CONFIG_THP_SWAP
266#define SWAPFILE_CLUSTER HPAGE_PMD_NR
Huang Yinga448f2d2018-08-21 21:52:17 -0700267
268#define swap_entry_size(size) (size)
Huang Ying38d8b4e2017-07-06 15:37:18 -0700269#else
Hugh Dickins048c27f2005-09-03 15:54:40 -0700270#define SWAPFILE_CLUSTER 256
Huang Yinga448f2d2018-08-21 21:52:17 -0700271
272/*
273 * Define swap_entry_size() as constant to let compiler to optimize
274 * out some code if !CONFIG_THP_SWAP
275 */
276#define swap_entry_size(size) 1
Huang Ying38d8b4e2017-07-06 15:37:18 -0700277#endif
Hugh Dickins048c27f2005-09-03 15:54:40 -0700278#define LATENCY_LIMIT 256
279
Shaohua Li2a8f9442013-09-11 14:20:28 -0700280static inline void cluster_set_flag(struct swap_cluster_info *info,
281 unsigned int flag)
282{
283 info->flags = flag;
284}
285
286static inline unsigned int cluster_count(struct swap_cluster_info *info)
287{
288 return info->data;
289}
290
291static inline void cluster_set_count(struct swap_cluster_info *info,
292 unsigned int c)
293{
294 info->data = c;
295}
296
297static inline void cluster_set_count_flag(struct swap_cluster_info *info,
298 unsigned int c, unsigned int f)
299{
300 info->flags = f;
301 info->data = c;
302}
303
304static inline unsigned int cluster_next(struct swap_cluster_info *info)
305{
306 return info->data;
307}
308
309static inline void cluster_set_next(struct swap_cluster_info *info,
310 unsigned int n)
311{
312 info->data = n;
313}
314
315static inline void cluster_set_next_flag(struct swap_cluster_info *info,
316 unsigned int n, unsigned int f)
317{
318 info->flags = f;
319 info->data = n;
320}
321
322static inline bool cluster_is_free(struct swap_cluster_info *info)
323{
324 return info->flags & CLUSTER_FLAG_FREE;
325}
326
327static inline bool cluster_is_null(struct swap_cluster_info *info)
328{
329 return info->flags & CLUSTER_FLAG_NEXT_NULL;
330}
331
332static inline void cluster_set_null(struct swap_cluster_info *info)
333{
334 info->flags = CLUSTER_FLAG_NEXT_NULL;
335 info->data = 0;
336}
337
Huang Yinge0709822017-09-06 16:22:16 -0700338static inline bool cluster_is_huge(struct swap_cluster_info *info)
339{
Huang Ying33ee0112018-08-21 21:52:13 -0700340 if (IS_ENABLED(CONFIG_THP_SWAP))
341 return info->flags & CLUSTER_FLAG_HUGE;
342 return false;
Huang Yinge0709822017-09-06 16:22:16 -0700343}
344
345static inline void cluster_clear_huge(struct swap_cluster_info *info)
346{
347 info->flags &= ~CLUSTER_FLAG_HUGE;
348}
349
Huang, Ying235b6212017-02-22 15:45:22 -0800350static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si,
351 unsigned long offset)
352{
353 struct swap_cluster_info *ci;
354
355 ci = si->cluster_info;
356 if (ci) {
357 ci += offset / SWAPFILE_CLUSTER;
358 spin_lock(&ci->lock);
359 }
360 return ci;
361}
362
363static inline void unlock_cluster(struct swap_cluster_info *ci)
364{
365 if (ci)
366 spin_unlock(&ci->lock);
367}
368
Huang Ying59d98bf2018-08-21 21:52:01 -0700369/*
370 * Determine the locking method in use for this device. Return
371 * swap_cluster_info if SSD-style cluster-based locking is in place.
372 */
Huang, Ying235b6212017-02-22 15:45:22 -0800373static inline struct swap_cluster_info *lock_cluster_or_swap_info(
Huang Ying59d98bf2018-08-21 21:52:01 -0700374 struct swap_info_struct *si, unsigned long offset)
Huang, Ying235b6212017-02-22 15:45:22 -0800375{
376 struct swap_cluster_info *ci;
377
Huang Ying59d98bf2018-08-21 21:52:01 -0700378 /* Try to use fine-grained SSD-style locking if available: */
Huang, Ying235b6212017-02-22 15:45:22 -0800379 ci = lock_cluster(si, offset);
Huang Ying59d98bf2018-08-21 21:52:01 -0700380 /* Otherwise, fall back to traditional, coarse locking: */
Huang, Ying235b6212017-02-22 15:45:22 -0800381 if (!ci)
382 spin_lock(&si->lock);
383
384 return ci;
385}
386
387static inline void unlock_cluster_or_swap_info(struct swap_info_struct *si,
388 struct swap_cluster_info *ci)
389{
390 if (ci)
391 unlock_cluster(ci);
392 else
393 spin_unlock(&si->lock);
394}
395
Huang Ying6b534912016-10-07 16:58:42 -0700396static inline bool cluster_list_empty(struct swap_cluster_list *list)
397{
398 return cluster_is_null(&list->head);
399}
400
401static inline unsigned int cluster_list_first(struct swap_cluster_list *list)
402{
403 return cluster_next(&list->head);
404}
405
406static void cluster_list_init(struct swap_cluster_list *list)
407{
408 cluster_set_null(&list->head);
409 cluster_set_null(&list->tail);
410}
411
412static void cluster_list_add_tail(struct swap_cluster_list *list,
413 struct swap_cluster_info *ci,
414 unsigned int idx)
415{
416 if (cluster_list_empty(list)) {
417 cluster_set_next_flag(&list->head, idx, 0);
418 cluster_set_next_flag(&list->tail, idx, 0);
419 } else {
Huang, Ying235b6212017-02-22 15:45:22 -0800420 struct swap_cluster_info *ci_tail;
Huang Ying6b534912016-10-07 16:58:42 -0700421 unsigned int tail = cluster_next(&list->tail);
422
Huang, Ying235b6212017-02-22 15:45:22 -0800423 /*
424 * Nested cluster lock, but both cluster locks are
425 * only acquired when we held swap_info_struct->lock
426 */
427 ci_tail = ci + tail;
428 spin_lock_nested(&ci_tail->lock, SINGLE_DEPTH_NESTING);
429 cluster_set_next(ci_tail, idx);
Huang Ying0ef017d2017-05-03 14:54:36 -0700430 spin_unlock(&ci_tail->lock);
Huang Ying6b534912016-10-07 16:58:42 -0700431 cluster_set_next_flag(&list->tail, idx, 0);
432 }
433}
434
435static unsigned int cluster_list_del_first(struct swap_cluster_list *list,
436 struct swap_cluster_info *ci)
437{
438 unsigned int idx;
439
440 idx = cluster_next(&list->head);
441 if (cluster_next(&list->tail) == idx) {
442 cluster_set_null(&list->head);
443 cluster_set_null(&list->tail);
444 } else
445 cluster_set_next_flag(&list->head,
446 cluster_next(&ci[idx]), 0);
447
448 return idx;
449}
450
Shaohua Li815c2c52013-09-11 14:20:30 -0700451/* Add a cluster to discard list and schedule it to do discard */
452static void swap_cluster_schedule_discard(struct swap_info_struct *si,
453 unsigned int idx)
454{
455 /*
456 * If scan_swap_map() can't find a free cluster, it will check
457 * si->swap_map directly. To make sure the discarding cluster isn't
458 * taken by scan_swap_map(), mark the swap entries bad (occupied). It
459 * will be cleared after discard
460 */
461 memset(si->swap_map + idx * SWAPFILE_CLUSTER,
462 SWAP_MAP_BAD, SWAPFILE_CLUSTER);
463
Huang Ying6b534912016-10-07 16:58:42 -0700464 cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx);
Shaohua Li815c2c52013-09-11 14:20:30 -0700465
466 schedule_work(&si->discard_work);
467}
468
Huang Ying38d8b4e2017-07-06 15:37:18 -0700469static void __free_cluster(struct swap_info_struct *si, unsigned long idx)
470{
471 struct swap_cluster_info *ci = si->cluster_info;
472
473 cluster_set_flag(ci + idx, CLUSTER_FLAG_FREE);
474 cluster_list_add_tail(&si->free_clusters, ci, idx);
475}
476
Shaohua Li815c2c52013-09-11 14:20:30 -0700477/*
478 * Doing discard actually. After a cluster discard is finished, the cluster
479 * will be added to free cluster list. caller should hold si->lock.
480*/
481static void swap_do_scheduled_discard(struct swap_info_struct *si)
482{
Huang, Ying235b6212017-02-22 15:45:22 -0800483 struct swap_cluster_info *info, *ci;
Shaohua Li815c2c52013-09-11 14:20:30 -0700484 unsigned int idx;
485
486 info = si->cluster_info;
487
Huang Ying6b534912016-10-07 16:58:42 -0700488 while (!cluster_list_empty(&si->discard_clusters)) {
489 idx = cluster_list_del_first(&si->discard_clusters, info);
Shaohua Li815c2c52013-09-11 14:20:30 -0700490 spin_unlock(&si->lock);
491
492 discard_swap_cluster(si, idx * SWAPFILE_CLUSTER,
493 SWAPFILE_CLUSTER);
494
495 spin_lock(&si->lock);
Huang, Ying235b6212017-02-22 15:45:22 -0800496 ci = lock_cluster(si, idx * SWAPFILE_CLUSTER);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700497 __free_cluster(si, idx);
Shaohua Li815c2c52013-09-11 14:20:30 -0700498 memset(si->swap_map + idx * SWAPFILE_CLUSTER,
499 0, SWAPFILE_CLUSTER);
Huang, Ying235b6212017-02-22 15:45:22 -0800500 unlock_cluster(ci);
Shaohua Li815c2c52013-09-11 14:20:30 -0700501 }
502}
503
504static void swap_discard_work(struct work_struct *work)
505{
506 struct swap_info_struct *si;
507
508 si = container_of(work, struct swap_info_struct, discard_work);
509
510 spin_lock(&si->lock);
511 swap_do_scheduled_discard(si);
512 spin_unlock(&si->lock);
513}
514
Huang Ying38d8b4e2017-07-06 15:37:18 -0700515static void alloc_cluster(struct swap_info_struct *si, unsigned long idx)
516{
517 struct swap_cluster_info *ci = si->cluster_info;
518
519 VM_BUG_ON(cluster_list_first(&si->free_clusters) != idx);
520 cluster_list_del_first(&si->free_clusters, ci);
521 cluster_set_count_flag(ci + idx, 0, 0);
522}
523
524static void free_cluster(struct swap_info_struct *si, unsigned long idx)
525{
526 struct swap_cluster_info *ci = si->cluster_info + idx;
527
528 VM_BUG_ON(cluster_count(ci) != 0);
529 /*
530 * If the swap is discardable, prepare discard the cluster
531 * instead of free it immediately. The cluster will be freed
532 * after discard.
533 */
534 if ((si->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) ==
535 (SWP_WRITEOK | SWP_PAGE_DISCARD)) {
536 swap_cluster_schedule_discard(si, idx);
537 return;
538 }
539
540 __free_cluster(si, idx);
541}
542
Shaohua Li2a8f9442013-09-11 14:20:28 -0700543/*
544 * The cluster corresponding to page_nr will be used. The cluster will be
545 * removed from free cluster list and its usage counter will be increased.
546 */
547static void inc_cluster_info_page(struct swap_info_struct *p,
548 struct swap_cluster_info *cluster_info, unsigned long page_nr)
549{
550 unsigned long idx = page_nr / SWAPFILE_CLUSTER;
551
552 if (!cluster_info)
553 return;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700554 if (cluster_is_free(&cluster_info[idx]))
555 alloc_cluster(p, idx);
Shaohua Li2a8f9442013-09-11 14:20:28 -0700556
557 VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER);
558 cluster_set_count(&cluster_info[idx],
559 cluster_count(&cluster_info[idx]) + 1);
560}
561
562/*
563 * The cluster corresponding to page_nr decreases one usage. If the usage
564 * counter becomes 0, which means no page in the cluster is in using, we can
565 * optionally discard the cluster and add it to free cluster list.
566 */
567static void dec_cluster_info_page(struct swap_info_struct *p,
568 struct swap_cluster_info *cluster_info, unsigned long page_nr)
569{
570 unsigned long idx = page_nr / SWAPFILE_CLUSTER;
571
572 if (!cluster_info)
573 return;
574
575 VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0);
576 cluster_set_count(&cluster_info[idx],
577 cluster_count(&cluster_info[idx]) - 1);
578
Huang Ying38d8b4e2017-07-06 15:37:18 -0700579 if (cluster_count(&cluster_info[idx]) == 0)
580 free_cluster(p, idx);
Shaohua Li2a8f9442013-09-11 14:20:28 -0700581}
582
583/*
584 * It's possible scan_swap_map() uses a free cluster in the middle of free
585 * cluster list. Avoiding such abuse to avoid list corruption.
586 */
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700587static bool
588scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si,
Shaohua Li2a8f9442013-09-11 14:20:28 -0700589 unsigned long offset)
590{
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700591 struct percpu_cluster *percpu_cluster;
592 bool conflict;
593
Shaohua Li2a8f9442013-09-11 14:20:28 -0700594 offset /= SWAPFILE_CLUSTER;
Huang Ying6b534912016-10-07 16:58:42 -0700595 conflict = !cluster_list_empty(&si->free_clusters) &&
596 offset != cluster_list_first(&si->free_clusters) &&
Shaohua Li2a8f9442013-09-11 14:20:28 -0700597 cluster_is_free(&si->cluster_info[offset]);
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700598
599 if (!conflict)
600 return false;
601
602 percpu_cluster = this_cpu_ptr(si->percpu_cluster);
603 cluster_set_null(&percpu_cluster->index);
604 return true;
605}
606
607/*
608 * Try to get a swap entry from current cpu's swap entry pool (a cluster). This
609 * might involve allocating a new cluster for current CPU too.
610 */
Tim Chen36005ba2017-02-22 15:45:33 -0800611static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700612 unsigned long *offset, unsigned long *scan_base)
613{
614 struct percpu_cluster *cluster;
Huang, Ying235b6212017-02-22 15:45:22 -0800615 struct swap_cluster_info *ci;
Huang, Ying235b6212017-02-22 15:45:22 -0800616 unsigned long tmp, max;
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700617
618new_cluster:
619 cluster = this_cpu_ptr(si->percpu_cluster);
620 if (cluster_is_null(&cluster->index)) {
Huang Ying6b534912016-10-07 16:58:42 -0700621 if (!cluster_list_empty(&si->free_clusters)) {
622 cluster->index = si->free_clusters.head;
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700623 cluster->next = cluster_next(&cluster->index) *
624 SWAPFILE_CLUSTER;
Huang Ying6b534912016-10-07 16:58:42 -0700625 } else if (!cluster_list_empty(&si->discard_clusters)) {
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700626 /*
627 * we don't have free cluster but have some clusters in
Huang Ying49070582020-06-01 21:49:22 -0700628 * discarding, do discard now and reclaim them, then
629 * reread cluster_next_cpu since we dropped si->lock
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700630 */
631 swap_do_scheduled_discard(si);
Huang Ying49070582020-06-01 21:49:22 -0700632 *scan_base = this_cpu_read(*si->cluster_next_cpu);
633 *offset = *scan_base;
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700634 goto new_cluster;
635 } else
Tim Chen36005ba2017-02-22 15:45:33 -0800636 return false;
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700637 }
638
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700639 /*
640 * Other CPUs can use our cluster if they can't find a free cluster,
641 * check if there is still free entry in the cluster
642 */
643 tmp = cluster->next;
Huang, Ying235b6212017-02-22 15:45:22 -0800644 max = min_t(unsigned long, si->max,
645 (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER);
Wei Yang7b9e2de2020-06-01 21:49:07 -0700646 if (tmp < max) {
647 ci = lock_cluster(si, tmp);
648 while (tmp < max) {
649 if (!si->swap_map[tmp])
650 break;
651 tmp++;
652 }
653 unlock_cluster(ci);
Huang, Ying235b6212017-02-22 15:45:22 -0800654 }
Wei Yang0fd0e192020-06-01 21:49:01 -0700655 if (tmp >= max) {
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700656 cluster_set_null(&cluster->index);
657 goto new_cluster;
658 }
659 cluster->next = tmp + 1;
660 *offset = tmp;
661 *scan_base = tmp;
Wei Yangfdff1de2020-06-01 21:49:04 -0700662 return true;
Shaohua Li2a8f9442013-09-11 14:20:28 -0700663}
664
Aaron Lua2468cc2017-09-06 16:24:57 -0700665static void __del_from_avail_list(struct swap_info_struct *p)
666{
667 int nid;
668
Rongwei Wangea8c42b2023-04-04 23:47:16 +0800669 assert_spin_locked(&p->lock);
Aaron Lua2468cc2017-09-06 16:24:57 -0700670 for_each_node(nid)
671 plist_del(&p->avail_lists[nid], &swap_avail_heads[nid]);
672}
673
674static void del_from_avail_list(struct swap_info_struct *p)
675{
676 spin_lock(&swap_avail_lock);
677 __del_from_avail_list(p);
678 spin_unlock(&swap_avail_lock);
679}
680
Huang Ying38d8b4e2017-07-06 15:37:18 -0700681static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset,
682 unsigned int nr_entries)
683{
684 unsigned int end = offset + nr_entries - 1;
685
686 if (offset == si->lowest_bit)
687 si->lowest_bit += nr_entries;
688 if (end == si->highest_bit)
Qian Caia449bf52020-08-14 17:31:31 -0700689 WRITE_ONCE(si->highest_bit, si->highest_bit - nr_entries);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700690 si->inuse_pages += nr_entries;
691 if (si->inuse_pages == si->pages) {
692 si->lowest_bit = si->max;
693 si->highest_bit = 0;
Aaron Lua2468cc2017-09-06 16:24:57 -0700694 del_from_avail_list(si);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700695 }
696}
697
Aaron Lua2468cc2017-09-06 16:24:57 -0700698static void add_to_avail_list(struct swap_info_struct *p)
699{
700 int nid;
701
702 spin_lock(&swap_avail_lock);
703 for_each_node(nid) {
704 WARN_ON(!plist_node_empty(&p->avail_lists[nid]));
705 plist_add(&p->avail_lists[nid], &swap_avail_heads[nid]);
706 }
707 spin_unlock(&swap_avail_lock);
708}
709
Huang Ying38d8b4e2017-07-06 15:37:18 -0700710static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
711 unsigned int nr_entries)
712{
Joonsoo Kim3852f672020-08-11 18:30:47 -0700713 unsigned long begin = offset;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700714 unsigned long end = offset + nr_entries - 1;
715 void (*swap_slot_free_notify)(struct block_device *, unsigned long);
716
717 if (offset < si->lowest_bit)
718 si->lowest_bit = offset;
719 if (end > si->highest_bit) {
720 bool was_full = !si->highest_bit;
721
Qian Caia449bf52020-08-14 17:31:31 -0700722 WRITE_ONCE(si->highest_bit, end);
Aaron Lua2468cc2017-09-06 16:24:57 -0700723 if (was_full && (si->flags & SWP_WRITEOK))
724 add_to_avail_list(si);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700725 }
726 atomic_long_add(nr_entries, &nr_swap_pages);
727 si->inuse_pages -= nr_entries;
728 if (si->flags & SWP_BLKDEV)
729 swap_slot_free_notify =
730 si->bdev->bd_disk->fops->swap_slot_free_notify;
731 else
732 swap_slot_free_notify = NULL;
733 while (offset <= end) {
Steven Price8a848022020-05-13 16:37:49 +0100734 arch_swap_invalidate_page(si->type, offset);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700735 frontswap_invalidate_page(si->type, offset);
736 if (swap_slot_free_notify)
737 swap_slot_free_notify(si->bdev, offset);
738 offset++;
739 }
Joonsoo Kim3852f672020-08-11 18:30:47 -0700740 clear_shadow_from_swap_cache(si->type, begin, end);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700741}
742
Huang Ying49070582020-06-01 21:49:22 -0700743static void set_cluster_next(struct swap_info_struct *si, unsigned long next)
744{
745 unsigned long prev;
746
747 if (!(si->flags & SWP_SOLIDSTATE)) {
748 si->cluster_next = next;
749 return;
750 }
751
752 prev = this_cpu_read(*si->cluster_next_cpu);
753 /*
754 * Cross the swap address space size aligned trunk, choose
755 * another trunk randomly to avoid lock contention on swap
756 * address space if possible.
757 */
758 if ((prev >> SWAP_ADDRESS_SPACE_SHIFT) !=
759 (next >> SWAP_ADDRESS_SPACE_SHIFT)) {
760 /* No free swap slots available */
761 if (si->highest_bit <= si->lowest_bit)
762 return;
763 next = si->lowest_bit +
764 prandom_u32_max(si->highest_bit - si->lowest_bit + 1);
765 next = ALIGN_DOWN(next, SWAP_ADDRESS_SPACE_PAGES);
766 next = max_t(unsigned int, next, si->lowest_bit);
767 }
768 this_cpu_write(*si->cluster_next_cpu, next);
769}
770
Tim Chen36005ba2017-02-22 15:45:33 -0800771static int scan_swap_map_slots(struct swap_info_struct *si,
772 unsigned char usage, int nr,
773 swp_entry_t slots[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Huang, Ying235b6212017-02-22 15:45:22 -0800775 struct swap_cluster_info *ci;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800776 unsigned long offset;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800777 unsigned long scan_base;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800778 unsigned long last_in_cluster = 0;
Hugh Dickins048c27f2005-09-03 15:54:40 -0700779 int latency_ration = LATENCY_LIMIT;
Tim Chen36005ba2017-02-22 15:45:33 -0800780 int n_ret = 0;
Huang Yinged43af12020-06-01 21:49:10 -0700781 bool scanned_many = false;
Tim Chen36005ba2017-02-22 15:45:33 -0800782
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800783 /*
Hugh Dickins7dfad412005-09-03 15:54:38 -0700784 * We try to cluster swap pages by allocating them sequentially
785 * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
786 * way, however, we resort to first-free allocation, starting
787 * a new cluster. This prevents us from scattering swap pages
788 * all over the entire swap partition, so that we reduce
789 * overall disk seek times between swap pages. -- sct
790 * But we do now try to find an empty cluster. -Andrea
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800791 * And we let swap pages go all over an SSD partition. Hugh
Hugh Dickins7dfad412005-09-03 15:54:38 -0700792 */
793
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700794 si->flags += SWP_SCANNING;
Huang Ying49070582020-06-01 21:49:22 -0700795 /*
796 * Use percpu scan base for SSD to reduce lock contention on
797 * cluster and swap cache. For HDD, sequential access is more
798 * important.
799 */
800 if (si->flags & SWP_SOLIDSTATE)
801 scan_base = this_cpu_read(*si->cluster_next_cpu);
802 else
803 scan_base = si->cluster_next;
804 offset = scan_base;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800805
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700806 /* SSD algorithm */
807 if (si->cluster_info) {
Wei Yangbd2d18d2020-06-01 21:48:52 -0700808 if (!scan_swap_map_try_ssd_cluster(si, &offset, &scan_base))
Tim Chen36005ba2017-02-22 15:45:33 -0800809 goto scan;
Wei Yangf4eaf512020-06-01 21:48:49 -0700810 } else if (unlikely(!si->cluster_nr--)) {
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800811 if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
812 si->cluster_nr = SWAPFILE_CLUSTER - 1;
813 goto checks;
814 }
Shaohua Li2a8f9442013-09-11 14:20:28 -0700815
Shaohua Liec8acf22013-02-22 16:34:38 -0800816 spin_unlock(&si->lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700817
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800818 /*
819 * If seek is expensive, start searching for new cluster from
820 * start of partition, to minimize the span of allocated swap.
Chen Yucong50088c42014-06-04 16:10:57 -0700821 * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info
822 * case, just handled by scan_swap_map_try_ssd_cluster() above.
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800823 */
Chen Yucong50088c42014-06-04 16:10:57 -0700824 scan_base = offset = si->lowest_bit;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700825 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
826
827 /* Locate the first empty (unaligned) cluster */
828 for (; last_in_cluster <= si->highest_bit; offset++) {
829 if (si->swap_map[offset])
830 last_in_cluster = offset + SWAPFILE_CLUSTER;
831 else if (offset == last_in_cluster) {
Shaohua Liec8acf22013-02-22 16:34:38 -0800832 spin_lock(&si->lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800833 offset -= SWAPFILE_CLUSTER - 1;
834 si->cluster_next = offset;
835 si->cluster_nr = SWAPFILE_CLUSTER - 1;
836 goto checks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700838 if (unlikely(--latency_ration < 0)) {
839 cond_resched();
840 latency_ration = LATENCY_LIMIT;
841 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700842 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800843
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800844 offset = scan_base;
Shaohua Liec8acf22013-02-22 16:34:38 -0800845 spin_lock(&si->lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800846 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700848
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800849checks:
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700850 if (si->cluster_info) {
Tim Chen36005ba2017-02-22 15:45:33 -0800851 while (scan_swap_map_ssd_cluster_conflict(si, offset)) {
852 /* take a break if we already got some slots */
853 if (n_ret)
854 goto done;
855 if (!scan_swap_map_try_ssd_cluster(si, &offset,
856 &scan_base))
857 goto scan;
858 }
Shaohua Liebc2a1a2013-09-11 14:20:32 -0700859 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800860 if (!(si->flags & SWP_WRITEOK))
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700861 goto no_page;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700862 if (!si->highest_bit)
863 goto no_page;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800864 if (offset > si->highest_bit)
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800865 scan_base = offset = si->lowest_bit;
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700866
Huang, Ying235b6212017-02-22 15:45:22 -0800867 ci = lock_cluster(si, offset);
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -0700868 /* reuse swap entry of cache-only swap if not busy. */
869 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700870 int swap_was_freed;
Huang, Ying235b6212017-02-22 15:45:22 -0800871 unlock_cluster(ci);
Shaohua Liec8acf22013-02-22 16:34:38 -0800872 spin_unlock(&si->lock);
Huang Yingbcd49e82018-10-26 15:03:46 -0700873 swap_was_freed = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY);
Shaohua Liec8acf22013-02-22 16:34:38 -0800874 spin_lock(&si->lock);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700875 /* entry was freed successfully, try to use this again */
876 if (swap_was_freed)
877 goto checks;
878 goto scan; /* check next one */
879 }
880
Huang, Ying235b6212017-02-22 15:45:22 -0800881 if (si->swap_map[offset]) {
882 unlock_cluster(ci);
Tim Chen36005ba2017-02-22 15:45:33 -0800883 if (!n_ret)
884 goto scan;
885 else
886 goto done;
Huang, Ying235b6212017-02-22 15:45:22 -0800887 }
Qian Caia449bf52020-08-14 17:31:31 -0700888 WRITE_ONCE(si->swap_map[offset], usage);
Huang Ying2872bb22017-05-03 14:54:39 -0700889 inc_cluster_info_page(si, si->cluster_info, offset);
890 unlock_cluster(ci);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700891
Huang Ying38d8b4e2017-07-06 15:37:18 -0700892 swap_range_alloc(si, offset, 1);
Tim Chen36005ba2017-02-22 15:45:33 -0800893 slots[n_ret++] = swp_entry(si->type, offset);
Hugh Dickins7992fde2009-01-06 14:39:53 -0800894
Tim Chen36005ba2017-02-22 15:45:33 -0800895 /* got enough slots or reach max slots? */
896 if ((n_ret == nr) || (offset >= si->highest_bit))
897 goto done;
898
899 /* search for next available slot */
900
901 /* time to take a break? */
902 if (unlikely(--latency_ration < 0)) {
903 if (n_ret)
904 goto done;
905 spin_unlock(&si->lock);
906 cond_resched();
907 spin_lock(&si->lock);
908 latency_ration = LATENCY_LIMIT;
909 }
910
911 /* try to get more slots in cluster */
912 if (si->cluster_info) {
913 if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base))
914 goto checks;
Wei Yangf4eaf512020-06-01 21:48:49 -0700915 } else if (si->cluster_nr && !si->swap_map[++offset]) {
916 /* non-ssd case, still more slots in cluster? */
Tim Chen36005ba2017-02-22 15:45:33 -0800917 --si->cluster_nr;
918 goto checks;
919 }
920
Huang Yinged43af12020-06-01 21:49:10 -0700921 /*
922 * Even if there's no free clusters available (fragmented),
923 * try to scan a little more quickly with lock held unless we
924 * have scanned too many slots already.
925 */
926 if (!scanned_many) {
927 unsigned long scan_limit;
928
929 if (offset < scan_base)
930 scan_limit = scan_base;
931 else
932 scan_limit = si->highest_bit;
933 for (; offset <= scan_limit && --latency_ration > 0;
934 offset++) {
935 if (!si->swap_map[offset])
936 goto checks;
937 }
938 }
939
Tim Chen36005ba2017-02-22 15:45:33 -0800940done:
Huang Ying49070582020-06-01 21:49:22 -0700941 set_cluster_next(si, offset + 1);
Tim Chen36005ba2017-02-22 15:45:33 -0800942 si->flags -= SWP_SCANNING;
943 return n_ret;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800944
945scan:
Shaohua Liec8acf22013-02-22 16:34:38 -0800946 spin_unlock(&si->lock);
Qian Caia449bf52020-08-14 17:31:31 -0700947 while (++offset <= READ_ONCE(si->highest_bit)) {
948 if (data_race(!si->swap_map[offset])) {
Shaohua Liec8acf22013-02-22 16:34:38 -0800949 spin_lock(&si->lock);
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700950 goto checks;
951 }
Qian Caia449bf52020-08-14 17:31:31 -0700952 if (vm_swap_full() &&
953 READ_ONCE(si->swap_map[offset]) == SWAP_HAS_CACHE) {
Shaohua Liec8acf22013-02-22 16:34:38 -0800954 spin_lock(&si->lock);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700955 goto checks;
956 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700957 if (unlikely(--latency_ration < 0)) {
958 cond_resched();
959 latency_ration = LATENCY_LIMIT;
Huang Yinged43af12020-06-01 21:49:10 -0700960 scanned_many = true;
Hugh Dickins048c27f2005-09-03 15:54:40 -0700961 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700962 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800963 offset = si->lowest_bit;
Jamie Liua5998062014-01-23 15:53:40 -0800964 while (offset < scan_base) {
Qian Caia449bf52020-08-14 17:31:31 -0700965 if (data_race(!si->swap_map[offset])) {
Shaohua Liec8acf22013-02-22 16:34:38 -0800966 spin_lock(&si->lock);
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800967 goto checks;
968 }
Qian Caia449bf52020-08-14 17:31:31 -0700969 if (vm_swap_full() &&
970 READ_ONCE(si->swap_map[offset]) == SWAP_HAS_CACHE) {
Shaohua Liec8acf22013-02-22 16:34:38 -0800971 spin_lock(&si->lock);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700972 goto checks;
973 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800974 if (unlikely(--latency_ration < 0)) {
975 cond_resched();
976 latency_ration = LATENCY_LIMIT;
Huang Yinged43af12020-06-01 21:49:10 -0700977 scanned_many = true;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800978 }
Jamie Liua5998062014-01-23 15:53:40 -0800979 offset++;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800980 }
Shaohua Liec8acf22013-02-22 16:34:38 -0800981 spin_lock(&si->lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700982
983no_page:
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700984 si->flags -= SWP_SCANNING;
Tim Chen36005ba2017-02-22 15:45:33 -0800985 return n_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Huang Ying38d8b4e2017-07-06 15:37:18 -0700988static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot)
989{
990 unsigned long idx;
991 struct swap_cluster_info *ci;
992 unsigned long offset, i;
993 unsigned char *map;
994
Huang Yingfe5266d2018-08-21 21:52:05 -0700995 /*
996 * Should not even be attempting cluster allocations when huge
997 * page swap is disabled. Warn and fail the allocation.
998 */
999 if (!IS_ENABLED(CONFIG_THP_SWAP)) {
1000 VM_WARN_ON_ONCE(1);
1001 return 0;
1002 }
1003
Huang Ying38d8b4e2017-07-06 15:37:18 -07001004 if (cluster_list_empty(&si->free_clusters))
1005 return 0;
1006
1007 idx = cluster_list_first(&si->free_clusters);
1008 offset = idx * SWAPFILE_CLUSTER;
1009 ci = lock_cluster(si, offset);
1010 alloc_cluster(si, idx);
Huang Yinge0709822017-09-06 16:22:16 -07001011 cluster_set_count_flag(ci, SWAPFILE_CLUSTER, CLUSTER_FLAG_HUGE);
Huang Ying38d8b4e2017-07-06 15:37:18 -07001012
1013 map = si->swap_map + offset;
1014 for (i = 0; i < SWAPFILE_CLUSTER; i++)
1015 map[i] = SWAP_HAS_CACHE;
1016 unlock_cluster(ci);
1017 swap_range_alloc(si, offset, SWAPFILE_CLUSTER);
1018 *slot = swp_entry(si->type, offset);
1019
1020 return 1;
1021}
1022
1023static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx)
1024{
1025 unsigned long offset = idx * SWAPFILE_CLUSTER;
1026 struct swap_cluster_info *ci;
1027
1028 ci = lock_cluster(si, offset);
Huang Ying979aafa2018-10-26 15:03:53 -07001029 memset(si->swap_map + offset, 0, SWAPFILE_CLUSTER);
Huang Ying38d8b4e2017-07-06 15:37:18 -07001030 cluster_set_count_flag(ci, 0, 0);
1031 free_cluster(si, idx);
1032 unlock_cluster(ci);
1033 swap_range_free(si, offset, SWAPFILE_CLUSTER);
1034}
Huang Ying38d8b4e2017-07-06 15:37:18 -07001035
Tim Chen36005ba2017-02-22 15:45:33 -08001036static unsigned long scan_swap_map(struct swap_info_struct *si,
1037 unsigned char usage)
1038{
1039 swp_entry_t entry;
1040 int n_ret;
1041
1042 n_ret = scan_swap_map_slots(si, usage, 1, &entry);
1043
1044 if (n_ret)
1045 return swp_offset(entry);
1046 else
1047 return 0;
1048
1049}
1050
Huang Ying5d5e8f12018-08-21 21:52:20 -07001051int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Huang Ying5d5e8f12018-08-21 21:52:20 -07001053 unsigned long size = swap_entry_size(entry_size);
Dan Streetmanadfab832014-06-04 16:09:53 -07001054 struct swap_info_struct *si, *next;
Tim Chen36005ba2017-02-22 15:45:33 -08001055 long avail_pgs;
1056 int n_ret = 0;
Aaron Lua2468cc2017-09-06 16:24:57 -07001057 int node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Huang Ying38d8b4e2017-07-06 15:37:18 -07001059 /* Only single cluster request supported */
Huang Ying5d5e8f12018-08-21 21:52:20 -07001060 WARN_ON_ONCE(n_goal > 1 && size == SWAPFILE_CLUSTER);
Huang Ying38d8b4e2017-07-06 15:37:18 -07001061
Zhaoyang Huangf472a592020-12-15 20:42:23 -08001062 spin_lock(&swap_avail_lock);
1063
Huang Ying5d5e8f12018-08-21 21:52:20 -07001064 avail_pgs = atomic_long_read(&nr_swap_pages) / size;
Zhaoyang Huangf472a592020-12-15 20:42:23 -08001065 if (avail_pgs <= 0) {
1066 spin_unlock(&swap_avail_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07001067 goto noswap;
Zhaoyang Huangf472a592020-12-15 20:42:23 -08001068 }
Tim Chen36005ba2017-02-22 15:45:33 -08001069
Wei Yang08d30902020-06-01 21:48:55 -07001070 n_goal = min3((long)n_goal, (long)SWAP_BATCH, avail_pgs);
Tim Chen36005ba2017-02-22 15:45:33 -08001071
Huang Ying5d5e8f12018-08-21 21:52:20 -07001072 atomic_long_sub(n_goal * size, &nr_swap_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Dan Streetman18ab4d42014-06-04 16:09:59 -07001074start_over:
Aaron Lua2468cc2017-09-06 16:24:57 -07001075 node = numa_node_id();
1076 plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) {
Dan Streetman18ab4d42014-06-04 16:09:59 -07001077 /* requeue si to after same-priority siblings */
Aaron Lua2468cc2017-09-06 16:24:57 -07001078 plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]);
Dan Streetman18ab4d42014-06-04 16:09:59 -07001079 spin_unlock(&swap_avail_lock);
Shaohua Liec8acf22013-02-22 16:34:38 -08001080 spin_lock(&si->lock);
Dan Streetmanadfab832014-06-04 16:09:53 -07001081 if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) {
Dan Streetman18ab4d42014-06-04 16:09:59 -07001082 spin_lock(&swap_avail_lock);
Aaron Lua2468cc2017-09-06 16:24:57 -07001083 if (plist_node_empty(&si->avail_lists[node])) {
Dan Streetman18ab4d42014-06-04 16:09:59 -07001084 spin_unlock(&si->lock);
1085 goto nextsi;
1086 }
1087 WARN(!si->highest_bit,
1088 "swap_info %d in list but !highest_bit\n",
1089 si->type);
1090 WARN(!(si->flags & SWP_WRITEOK),
1091 "swap_info %d in list but !SWP_WRITEOK\n",
1092 si->type);
Aaron Lua2468cc2017-09-06 16:24:57 -07001093 __del_from_avail_list(si);
Shaohua Liec8acf22013-02-22 16:34:38 -08001094 spin_unlock(&si->lock);
Dan Streetman18ab4d42014-06-04 16:09:59 -07001095 goto nextsi;
Shaohua Liec8acf22013-02-22 16:34:38 -08001096 }
Huang Ying5d5e8f12018-08-21 21:52:20 -07001097 if (size == SWAPFILE_CLUSTER) {
Gao Xiang41663432020-09-25 21:19:01 -07001098 if (si->flags & SWP_BLKDEV)
Huang Yingf0eea182017-09-06 16:22:23 -07001099 n_ret = swap_alloc_cluster(si, swp_entries);
1100 } else
Huang Ying38d8b4e2017-07-06 15:37:18 -07001101 n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE,
1102 n_goal, swp_entries);
Shaohua Liec8acf22013-02-22 16:34:38 -08001103 spin_unlock(&si->lock);
Huang Ying5d5e8f12018-08-21 21:52:20 -07001104 if (n_ret || size == SWAPFILE_CLUSTER)
Tim Chen36005ba2017-02-22 15:45:33 -08001105 goto check_out;
Dan Streetman18ab4d42014-06-04 16:09:59 -07001106 pr_debug("scan_swap_map of si %d failed to find offset\n",
Tim Chen36005ba2017-02-22 15:45:33 -08001107 si->type);
Longlong Xia30187be2023-01-28 09:47:57 +00001108 cond_resched();
Tim Chen36005ba2017-02-22 15:45:33 -08001109
Dan Streetman18ab4d42014-06-04 16:09:59 -07001110 spin_lock(&swap_avail_lock);
1111nextsi:
Dan Streetmanadfab832014-06-04 16:09:53 -07001112 /*
1113 * if we got here, it's likely that si was almost full before,
1114 * and since scan_swap_map() can drop the si->lock, multiple
1115 * callers probably all tried to get a page from the same si
Dan Streetman18ab4d42014-06-04 16:09:59 -07001116 * and it filled up before we could get one; or, the si filled
1117 * up between us dropping swap_avail_lock and taking si->lock.
1118 * Since we dropped the swap_avail_lock, the swap_avail_head
1119 * list may have been modified; so if next is still in the
Tim Chen36005ba2017-02-22 15:45:33 -08001120 * swap_avail_head list then try it, otherwise start over
1121 * if we have not gotten any slots.
Dan Streetmanadfab832014-06-04 16:09:53 -07001122 */
Aaron Lua2468cc2017-09-06 16:24:57 -07001123 if (plist_node_empty(&next->avail_lists[node]))
Dan Streetman18ab4d42014-06-04 16:09:59 -07001124 goto start_over;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07001126
Dan Streetman18ab4d42014-06-04 16:09:59 -07001127 spin_unlock(&swap_avail_lock);
1128
Tim Chen36005ba2017-02-22 15:45:33 -08001129check_out:
1130 if (n_ret < n_goal)
Huang Ying5d5e8f12018-08-21 21:52:20 -07001131 atomic_long_add((long)(n_goal - n_ret) * size,
Huang Ying38d8b4e2017-07-06 15:37:18 -07001132 &nr_swap_pages);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07001133noswap:
Tim Chen36005ba2017-02-22 15:45:33 -08001134 return n_ret;
1135}
1136
Seth Jennings2de1a7e2013-11-12 15:07:46 -08001137/* The only caller of this function is now suspend routine */
Hugh Dickins910321e2010-09-09 16:38:07 -07001138swp_entry_t get_swap_page_of_type(int type)
1139{
Daniel Jordanc10d38c2019-03-05 15:48:19 -08001140 struct swap_info_struct *si = swap_type_to_swap_info(type);
Hugh Dickins910321e2010-09-09 16:38:07 -07001141 pgoff_t offset;
1142
Daniel Jordanc10d38c2019-03-05 15:48:19 -08001143 if (!si)
1144 goto fail;
1145
Shaohua Liec8acf22013-02-22 16:34:38 -08001146 spin_lock(&si->lock);
Daniel Jordanc10d38c2019-03-05 15:48:19 -08001147 if (si->flags & SWP_WRITEOK) {
Hugh Dickins910321e2010-09-09 16:38:07 -07001148 /* This is called for allocating swap entry, not cache */
1149 offset = scan_swap_map(si, 1);
1150 if (offset) {
Zhaoyang Huangf472a592020-12-15 20:42:23 -08001151 atomic_long_dec(&nr_swap_pages);
Shaohua Liec8acf22013-02-22 16:34:38 -08001152 spin_unlock(&si->lock);
Hugh Dickins910321e2010-09-09 16:38:07 -07001153 return swp_entry(type, offset);
1154 }
Hugh Dickins910321e2010-09-09 16:38:07 -07001155 }
Shaohua Liec8acf22013-02-22 16:34:38 -08001156 spin_unlock(&si->lock);
Daniel Jordanc10d38c2019-03-05 15:48:19 -08001157fail:
Hugh Dickins910321e2010-09-09 16:38:07 -07001158 return (swp_entry_t) {0};
1159}
1160
Tim Chene8c26ab2017-02-22 15:45:29 -08001161static struct swap_info_struct *__swap_info_get(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Hugh Dickins73c34b62009-12-14 17:58:43 -08001163 struct swap_info_struct *p;
Huang Yingeb085572019-07-11 20:55:33 -07001164 unsigned long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 if (!entry.val)
1167 goto out;
Huang Yingeb085572019-07-11 20:55:33 -07001168 p = swp_swap_info(entry);
Daniel Jordanc10d38c2019-03-05 15:48:19 -08001169 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 goto bad_nofile;
Qian Caia449bf52020-08-14 17:31:31 -07001171 if (data_race(!(p->flags & SWP_USED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 goto bad_device;
1173 offset = swp_offset(entry);
1174 if (offset >= p->max)
1175 goto bad_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return p;
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178bad_offset:
Huang, Ying6a991fc2017-02-22 15:45:19 -08001179 pr_err("swap_info_get: %s%08lx\n", Bad_offset, entry.val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 goto out;
1181bad_device:
Huang, Ying6a991fc2017-02-22 15:45:19 -08001182 pr_err("swap_info_get: %s%08lx\n", Unused_file, entry.val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 goto out;
1184bad_nofile:
Huang, Ying6a991fc2017-02-22 15:45:19 -08001185 pr_err("swap_info_get: %s%08lx\n", Bad_file, entry.val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186out:
1187 return NULL;
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001188}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Tim Chene8c26ab2017-02-22 15:45:29 -08001190static struct swap_info_struct *_swap_info_get(swp_entry_t entry)
1191{
1192 struct swap_info_struct *p;
1193
1194 p = __swap_info_get(entry);
1195 if (!p)
1196 goto out;
Qian Caia449bf52020-08-14 17:31:31 -07001197 if (data_race(!p->swap_map[swp_offset(entry)]))
Tim Chene8c26ab2017-02-22 15:45:29 -08001198 goto bad_free;
1199 return p;
1200
1201bad_free:
1202 pr_err("swap_info_get: %s%08lx\n", Unused_offset, entry.val);
Tim Chene8c26ab2017-02-22 15:45:29 -08001203out:
1204 return NULL;
1205}
1206
Huang, Ying235b6212017-02-22 15:45:22 -08001207static struct swap_info_struct *swap_info_get(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Huang, Ying235b6212017-02-22 15:45:22 -08001209 struct swap_info_struct *p;
1210
1211 p = _swap_info_get(entry);
1212 if (p)
1213 spin_lock(&p->lock);
1214 return p;
1215}
1216
Tim Chen7c00baf2017-02-22 15:45:36 -08001217static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry,
1218 struct swap_info_struct *q)
1219{
1220 struct swap_info_struct *p;
1221
1222 p = _swap_info_get(entry);
1223
1224 if (p != q) {
1225 if (q != NULL)
1226 spin_unlock(&q->lock);
1227 if (p != NULL)
1228 spin_lock(&p->lock);
1229 }
1230 return p;
1231}
1232
Huang Yingb32d5f32018-08-21 21:52:24 -07001233static unsigned char __swap_entry_free_locked(struct swap_info_struct *p,
1234 unsigned long offset,
1235 unsigned char usage)
Huang, Ying235b6212017-02-22 15:45:22 -08001236{
Hugh Dickins8d69aae2009-12-14 17:58:45 -08001237 unsigned char count;
1238 unsigned char has_cache;
Huang, Ying235b6212017-02-22 15:45:22 -08001239
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001240 count = p->swap_map[offset];
Huang, Ying235b6212017-02-22 15:45:22 -08001241
Hugh Dickins253d5532009-12-14 17:58:44 -08001242 has_cache = count & SWAP_HAS_CACHE;
1243 count &= ~SWAP_HAS_CACHE;
1244
1245 if (usage == SWAP_HAS_CACHE) {
1246 VM_BUG_ON(!has_cache);
1247 has_cache = 0;
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001248 } else if (count == SWAP_MAP_SHMEM) {
1249 /*
1250 * Or we could insist on shmem.c using a special
1251 * swap_shmem_free() and free_shmem_swap_and_cache()...
1252 */
1253 count = 0;
Hugh Dickins570a335b2009-12-14 17:58:46 -08001254 } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
1255 if (count == COUNT_CONTINUED) {
1256 if (swap_count_continued(p, offset, count))
1257 count = SWAP_MAP_MAX | COUNT_CONTINUED;
1258 else
1259 count = SWAP_MAP_MAX;
1260 } else
1261 count--;
1262 }
Hugh Dickins253d5532009-12-14 17:58:44 -08001263
Hugh Dickins253d5532009-12-14 17:58:44 -08001264 usage = count | has_cache;
Qian Caia449bf52020-08-14 17:31:31 -07001265 if (usage)
1266 WRITE_ONCE(p->swap_map[offset], usage);
1267 else
1268 WRITE_ONCE(p->swap_map[offset], SWAP_HAS_CACHE);
Hugh Dickins253d5532009-12-14 17:58:44 -08001269
Huang Yingb32d5f32018-08-21 21:52:24 -07001270 return usage;
1271}
1272
Huang Yingeb085572019-07-11 20:55:33 -07001273/*
1274 * Check whether swap entry is valid in the swap device. If so,
1275 * return pointer to swap_info_struct, and keep the swap entry valid
1276 * via preventing the swap device from being swapoff, until
1277 * put_swap_device() is called. Otherwise return NULL.
1278 *
1279 * The entirety of the RCU read critical section must come before the
1280 * return from or after the call to synchronize_rcu() in
1281 * enable_swap_info() or swapoff(). So if "si->flags & SWP_VALID" is
1282 * true, the si->map, si->cluster_info, etc. must be valid in the
1283 * critical section.
1284 *
1285 * Notice that swapoff or swapoff+swapon can still happen before the
1286 * rcu_read_lock() in get_swap_device() or after the rcu_read_unlock()
1287 * in put_swap_device() if there isn't any other way to prevent
1288 * swapoff, such as page lock, page table lock, etc. The caller must
1289 * be prepared for that. For example, the following situation is
1290 * possible.
1291 *
1292 * CPU1 CPU2
1293 * do_swap_page()
1294 * ... swapoff+swapon
1295 * __read_swap_cache_async()
1296 * swapcache_prepare()
1297 * __swap_duplicate()
1298 * // check swap_map
1299 * // verify PTE not changed
1300 *
1301 * In __swap_duplicate(), the swap_map need to be checked before
1302 * changing partly because the specified swap entry may be for another
1303 * swap device which has been swapoff. And in do_swap_page(), after
1304 * the page is read from the swap device, the PTE is verified not
1305 * changed with the page table locked to check whether the swap device
1306 * has been swapoff or swapoff+swapon.
1307 */
1308struct swap_info_struct *get_swap_device(swp_entry_t entry)
1309{
1310 struct swap_info_struct *si;
1311 unsigned long offset;
1312
1313 if (!entry.val)
1314 goto out;
1315 si = swp_swap_info(entry);
1316 if (!si)
1317 goto bad_nofile;
1318
1319 rcu_read_lock();
Qian Caia449bf52020-08-14 17:31:31 -07001320 if (data_race(!(si->flags & SWP_VALID)))
Huang Yingeb085572019-07-11 20:55:33 -07001321 goto unlock_out;
1322 offset = swp_offset(entry);
1323 if (offset >= si->max)
1324 goto unlock_out;
1325
1326 return si;
1327bad_nofile:
1328 pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val);
1329out:
1330 return NULL;
1331unlock_out:
1332 rcu_read_unlock();
1333 return NULL;
1334}
1335
Huang Yingb32d5f32018-08-21 21:52:24 -07001336static unsigned char __swap_entry_free(struct swap_info_struct *p,
Wei Yang33e16272020-06-01 21:49:16 -07001337 swp_entry_t entry)
Huang Yingb32d5f32018-08-21 21:52:24 -07001338{
1339 struct swap_cluster_info *ci;
1340 unsigned long offset = swp_offset(entry);
Wei Yang33e16272020-06-01 21:49:16 -07001341 unsigned char usage;
Huang Yingb32d5f32018-08-21 21:52:24 -07001342
1343 ci = lock_cluster_or_swap_info(p, offset);
Wei Yang33e16272020-06-01 21:49:16 -07001344 usage = __swap_entry_free_locked(p, offset, 1);
Tim Chen7c00baf2017-02-22 15:45:36 -08001345 unlock_cluster_or_swap_info(p, ci);
Huang Ying10e364d2018-10-26 15:03:49 -07001346 if (!usage)
1347 free_swap_slot(entry);
Huang, Ying235b6212017-02-22 15:45:22 -08001348
Hugh Dickins253d5532009-12-14 17:58:44 -08001349 return usage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Tim Chen7c00baf2017-02-22 15:45:36 -08001352static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry)
1353{
1354 struct swap_cluster_info *ci;
1355 unsigned long offset = swp_offset(entry);
1356 unsigned char count;
1357
1358 ci = lock_cluster(p, offset);
1359 count = p->swap_map[offset];
1360 VM_BUG_ON(count != SWAP_HAS_CACHE);
1361 p->swap_map[offset] = 0;
1362 dec_cluster_info_page(p, p->cluster_info, offset);
1363 unlock_cluster(ci);
1364
Huang Ying38d8b4e2017-07-06 15:37:18 -07001365 mem_cgroup_uncharge_swap(entry, 1);
1366 swap_range_free(p, offset, 1);
Tim Chen7c00baf2017-02-22 15:45:36 -08001367}
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369/*
Seth Jennings2de1a7e2013-11-12 15:07:46 -08001370 * Caller has made sure that the swap device corresponding to entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 * is still around or has not been recycled.
1372 */
1373void swap_free(swp_entry_t entry)
1374{
Hugh Dickins73c34b62009-12-14 17:58:43 -08001375 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Huang, Ying235b6212017-02-22 15:45:22 -08001377 p = _swap_info_get(entry);
Huang Ying10e364d2018-10-26 15:03:49 -07001378 if (p)
Wei Yang33e16272020-06-01 21:49:16 -07001379 __swap_entry_free(p, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
1382/*
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07001383 * Called after dropping swapcache to decrease refcnt to swap entries.
1384 */
Huang Yinga448f2d2018-08-21 21:52:17 -07001385void put_swap_page(struct page *page, swp_entry_t entry)
Huang Ying38d8b4e2017-07-06 15:37:18 -07001386{
1387 unsigned long offset = swp_offset(entry);
1388 unsigned long idx = offset / SWAPFILE_CLUSTER;
1389 struct swap_cluster_info *ci;
1390 struct swap_info_struct *si;
1391 unsigned char *map;
Huang Yinga3aea832017-09-06 16:22:12 -07001392 unsigned int i, free_entries = 0;
1393 unsigned char val;
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001394 int size = swap_entry_size(thp_nr_pages(page));
Huang Yingfe5266d2018-08-21 21:52:05 -07001395
Huang Yinga3aea832017-09-06 16:22:12 -07001396 si = _swap_info_get(entry);
Huang Ying38d8b4e2017-07-06 15:37:18 -07001397 if (!si)
1398 return;
1399
Huang Yingc2343d22018-08-21 21:52:29 -07001400 ci = lock_cluster_or_swap_info(si, offset);
Huang Yinga448f2d2018-08-21 21:52:17 -07001401 if (size == SWAPFILE_CLUSTER) {
Huang Yinga448f2d2018-08-21 21:52:17 -07001402 VM_BUG_ON(!cluster_is_huge(ci));
1403 map = si->swap_map + offset;
1404 for (i = 0; i < SWAPFILE_CLUSTER; i++) {
1405 val = map[i];
1406 VM_BUG_ON(!(val & SWAP_HAS_CACHE));
1407 if (val == SWAP_HAS_CACHE)
1408 free_entries++;
1409 }
Huang Yinga448f2d2018-08-21 21:52:17 -07001410 cluster_clear_huge(ci);
Huang Yinga448f2d2018-08-21 21:52:17 -07001411 if (free_entries == SWAPFILE_CLUSTER) {
Huang Yingc2343d22018-08-21 21:52:29 -07001412 unlock_cluster_or_swap_info(si, ci);
Huang Yinga448f2d2018-08-21 21:52:17 -07001413 spin_lock(&si->lock);
Huang Yinga448f2d2018-08-21 21:52:17 -07001414 mem_cgroup_uncharge_swap(entry, SWAPFILE_CLUSTER);
1415 swap_free_cluster(si, idx);
1416 spin_unlock(&si->lock);
1417 return;
1418 }
1419 }
Huang Yingc2343d22018-08-21 21:52:29 -07001420 for (i = 0; i < size; i++, entry.val++) {
1421 if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) {
1422 unlock_cluster_or_swap_info(si, ci);
1423 free_swap_slot(entry);
1424 if (i == size - 1)
1425 return;
1426 lock_cluster_or_swap_info(si, offset);
Huang Yinga3aea832017-09-06 16:22:12 -07001427 }
1428 }
Huang Yingc2343d22018-08-21 21:52:29 -07001429 unlock_cluster_or_swap_info(si, ci);
Huang Ying38d8b4e2017-07-06 15:37:18 -07001430}
Huang Ying59807682017-09-06 16:22:34 -07001431
Huang Yingfe5266d2018-08-21 21:52:05 -07001432#ifdef CONFIG_THP_SWAP
Huang Ying59807682017-09-06 16:22:34 -07001433int split_swap_cluster(swp_entry_t entry)
1434{
1435 struct swap_info_struct *si;
1436 struct swap_cluster_info *ci;
1437 unsigned long offset = swp_offset(entry);
1438
1439 si = _swap_info_get(entry);
1440 if (!si)
1441 return -EBUSY;
1442 ci = lock_cluster(si, offset);
1443 cluster_clear_huge(ci);
1444 unlock_cluster(ci);
1445 return 0;
1446}
Huang Yingfe5266d2018-08-21 21:52:05 -07001447#endif
Huang Ying38d8b4e2017-07-06 15:37:18 -07001448
Huang Ying155b5f82017-07-06 15:40:31 -07001449static int swp_entry_cmp(const void *ent1, const void *ent2)
1450{
1451 const swp_entry_t *e1 = ent1, *e2 = ent2;
1452
1453 return (int)swp_type(*e1) - (int)swp_type(*e2);
1454}
1455
Tim Chen7c00baf2017-02-22 15:45:36 -08001456void swapcache_free_entries(swp_entry_t *entries, int n)
1457{
1458 struct swap_info_struct *p, *prev;
1459 int i;
1460
1461 if (n <= 0)
1462 return;
1463
1464 prev = NULL;
1465 p = NULL;
Huang Ying155b5f82017-07-06 15:40:31 -07001466
1467 /*
1468 * Sort swap entries by swap device, so each lock is only taken once.
1469 * nr_swapfiles isn't absolutely correct, but the overhead of sort() is
1470 * so low that it isn't necessary to optimize further.
1471 */
1472 if (nr_swapfiles > 1)
1473 sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);
Tim Chen7c00baf2017-02-22 15:45:36 -08001474 for (i = 0; i < n; ++i) {
1475 p = swap_info_get_cont(entries[i], prev);
1476 if (p)
1477 swap_entry_free(p, entries[i]);
Tim Chen7c00baf2017-02-22 15:45:36 -08001478 prev = p;
1479 }
Huang, Ying235b6212017-02-22 15:45:22 -08001480 if (p)
Tim Chen7c00baf2017-02-22 15:45:36 -08001481 spin_unlock(&p->lock);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07001482}
1483
1484/*
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07001485 * How many references to page are currently swapped out?
Hugh Dickins570a335b2009-12-14 17:58:46 -08001486 * This does not give an exact answer when swap count is continued,
1487 * but does include the high COUNT_CONTINUED flag to allow for that.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001489int page_swapcount(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07001491 int count = 0;
1492 struct swap_info_struct *p;
Huang, Ying235b6212017-02-22 15:45:22 -08001493 struct swap_cluster_info *ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 swp_entry_t entry;
Huang, Ying235b6212017-02-22 15:45:22 -08001495 unsigned long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Hugh Dickins4c21e2f2005-10-29 18:16:40 -07001497 entry.val = page_private(page);
Huang, Ying235b6212017-02-22 15:45:22 -08001498 p = _swap_info_get(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (p) {
Huang, Ying235b6212017-02-22 15:45:22 -08001500 offset = swp_offset(entry);
1501 ci = lock_cluster_or_swap_info(p, offset);
1502 count = swap_count(p->swap_map[offset]);
1503 unlock_cluster_or_swap_info(p, ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07001505 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
Huang Yingeb085572019-07-11 20:55:33 -07001508int __swap_count(swp_entry_t entry)
Minchan Kimaa8d22a2017-11-15 17:33:11 -08001509{
Huang Yingeb085572019-07-11 20:55:33 -07001510 struct swap_info_struct *si;
Minchan Kimaa8d22a2017-11-15 17:33:11 -08001511 pgoff_t offset = swp_offset(entry);
Huang Yingeb085572019-07-11 20:55:33 -07001512 int count = 0;
Minchan Kimaa8d22a2017-11-15 17:33:11 -08001513
Huang Yingeb085572019-07-11 20:55:33 -07001514 si = get_swap_device(entry);
1515 if (si) {
1516 count = swap_count(si->swap_map[offset]);
1517 put_swap_device(si);
1518 }
1519 return count;
Minchan Kimaa8d22a2017-11-15 17:33:11 -08001520}
1521
Huang Ying322b8afe2017-05-03 14:52:49 -07001522static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry)
1523{
1524 int count = 0;
1525 pgoff_t offset = swp_offset(entry);
1526 struct swap_cluster_info *ci;
1527
1528 ci = lock_cluster_or_swap_info(si, offset);
1529 count = swap_count(si->swap_map[offset]);
1530 unlock_cluster_or_swap_info(si, ci);
1531 return count;
1532}
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534/*
Minchan Kim8334b962015-09-08 15:00:24 -07001535 * How many references to @entry are currently swapped out?
Tim Chene8c26ab2017-02-22 15:45:29 -08001536 * This does not give an exact answer when swap count is continued,
1537 * but does include the high COUNT_CONTINUED flag to allow for that.
1538 */
1539int __swp_swapcount(swp_entry_t entry)
1540{
1541 int count = 0;
Tim Chene8c26ab2017-02-22 15:45:29 -08001542 struct swap_info_struct *si;
Tim Chene8c26ab2017-02-22 15:45:29 -08001543
Huang Yingeb085572019-07-11 20:55:33 -07001544 si = get_swap_device(entry);
1545 if (si) {
Huang Ying322b8afe2017-05-03 14:52:49 -07001546 count = swap_swapcount(si, entry);
Huang Yingeb085572019-07-11 20:55:33 -07001547 put_swap_device(si);
1548 }
Tim Chene8c26ab2017-02-22 15:45:29 -08001549 return count;
1550}
1551
1552/*
1553 * How many references to @entry are currently swapped out?
Minchan Kim8334b962015-09-08 15:00:24 -07001554 * This considers COUNT_CONTINUED so it returns exact answer.
1555 */
1556int swp_swapcount(swp_entry_t entry)
1557{
1558 int count, tmp_count, n;
1559 struct swap_info_struct *p;
Huang, Ying235b6212017-02-22 15:45:22 -08001560 struct swap_cluster_info *ci;
Minchan Kim8334b962015-09-08 15:00:24 -07001561 struct page *page;
1562 pgoff_t offset;
1563 unsigned char *map;
1564
Huang, Ying235b6212017-02-22 15:45:22 -08001565 p = _swap_info_get(entry);
Minchan Kim8334b962015-09-08 15:00:24 -07001566 if (!p)
1567 return 0;
1568
Huang, Ying235b6212017-02-22 15:45:22 -08001569 offset = swp_offset(entry);
1570
1571 ci = lock_cluster_or_swap_info(p, offset);
1572
1573 count = swap_count(p->swap_map[offset]);
Minchan Kim8334b962015-09-08 15:00:24 -07001574 if (!(count & COUNT_CONTINUED))
1575 goto out;
1576
1577 count &= ~COUNT_CONTINUED;
1578 n = SWAP_MAP_MAX + 1;
1579
Minchan Kim8334b962015-09-08 15:00:24 -07001580 page = vmalloc_to_page(p->swap_map + offset);
1581 offset &= ~PAGE_MASK;
1582 VM_BUG_ON(page_private(page) != SWP_CONTINUED);
1583
1584 do {
Geliang Tanga8ae4992016-01-14 15:20:45 -08001585 page = list_next_entry(page, lru);
Minchan Kim8334b962015-09-08 15:00:24 -07001586 map = kmap_atomic(page);
1587 tmp_count = map[offset];
1588 kunmap_atomic(map);
1589
1590 count += (tmp_count & ~COUNT_CONTINUED) * n;
1591 n *= (SWAP_CONT_MAX + 1);
1592 } while (tmp_count & COUNT_CONTINUED);
1593out:
Huang, Ying235b6212017-02-22 15:45:22 -08001594 unlock_cluster_or_swap_info(p, ci);
Minchan Kim8334b962015-09-08 15:00:24 -07001595 return count;
1596}
1597
Huang Yinge0709822017-09-06 16:22:16 -07001598static bool swap_page_trans_huge_swapped(struct swap_info_struct *si,
1599 swp_entry_t entry)
1600{
1601 struct swap_cluster_info *ci;
1602 unsigned char *map = si->swap_map;
1603 unsigned long roffset = swp_offset(entry);
1604 unsigned long offset = round_down(roffset, SWAPFILE_CLUSTER);
1605 int i;
1606 bool ret = false;
1607
1608 ci = lock_cluster_or_swap_info(si, offset);
1609 if (!ci || !cluster_is_huge(ci)) {
Huang Yingafa47112018-08-21 21:52:09 -07001610 if (swap_count(map[roffset]))
Huang Yinge0709822017-09-06 16:22:16 -07001611 ret = true;
1612 goto unlock_out;
1613 }
1614 for (i = 0; i < SWAPFILE_CLUSTER; i++) {
Huang Yingafa47112018-08-21 21:52:09 -07001615 if (swap_count(map[offset + i])) {
Huang Yinge0709822017-09-06 16:22:16 -07001616 ret = true;
1617 break;
1618 }
1619 }
1620unlock_out:
1621 unlock_cluster_or_swap_info(si, ci);
1622 return ret;
1623}
1624
1625static bool page_swapped(struct page *page)
1626{
1627 swp_entry_t entry;
1628 struct swap_info_struct *si;
1629
Huang Yingfe5266d2018-08-21 21:52:05 -07001630 if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!PageTransCompound(page)))
Huang Yinge0709822017-09-06 16:22:16 -07001631 return page_swapcount(page) != 0;
1632
1633 page = compound_head(page);
1634 entry.val = page_private(page);
1635 si = _swap_info_get(entry);
1636 if (si)
1637 return swap_page_trans_huge_swapped(si, entry);
1638 return false;
1639}
Huang Yingba3c4ce2017-09-06 16:22:19 -07001640
1641static int page_trans_huge_map_swapcount(struct page *page, int *total_mapcount,
1642 int *total_swapcount)
1643{
1644 int i, map_swapcount, _total_mapcount, _total_swapcount;
1645 unsigned long offset = 0;
1646 struct swap_info_struct *si;
1647 struct swap_cluster_info *ci = NULL;
1648 unsigned char *map = NULL;
1649 int mapcount, swapcount = 0;
1650
1651 /* hugetlbfs shouldn't call it */
1652 VM_BUG_ON_PAGE(PageHuge(page), page);
1653
Huang Yingfe5266d2018-08-21 21:52:05 -07001654 if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!PageTransCompound(page))) {
1655 mapcount = page_trans_huge_mapcount(page, total_mapcount);
Huang Yingba3c4ce2017-09-06 16:22:19 -07001656 if (PageSwapCache(page))
1657 swapcount = page_swapcount(page);
1658 if (total_swapcount)
1659 *total_swapcount = swapcount;
1660 return mapcount + swapcount;
1661 }
1662
1663 page = compound_head(page);
1664
1665 _total_mapcount = _total_swapcount = map_swapcount = 0;
1666 if (PageSwapCache(page)) {
1667 swp_entry_t entry;
1668
1669 entry.val = page_private(page);
1670 si = _swap_info_get(entry);
1671 if (si) {
1672 map = si->swap_map;
1673 offset = swp_offset(entry);
1674 }
1675 }
1676 if (map)
1677 ci = lock_cluster(si, offset);
1678 for (i = 0; i < HPAGE_PMD_NR; i++) {
1679 mapcount = atomic_read(&page[i]._mapcount) + 1;
1680 _total_mapcount += mapcount;
1681 if (map) {
1682 swapcount = swap_count(map[offset + i]);
1683 _total_swapcount += swapcount;
1684 }
1685 map_swapcount = max(map_swapcount, mapcount + swapcount);
1686 }
1687 unlock_cluster(ci);
1688 if (PageDoubleMap(page)) {
1689 map_swapcount -= 1;
1690 _total_mapcount -= HPAGE_PMD_NR;
1691 }
1692 mapcount = compound_mapcount(page);
1693 map_swapcount += mapcount;
1694 _total_mapcount += mapcount;
1695 if (total_mapcount)
1696 *total_mapcount = _total_mapcount;
1697 if (total_swapcount)
1698 *total_swapcount = _total_swapcount;
1699
1700 return map_swapcount;
1701}
Huang Yinge0709822017-09-06 16:22:16 -07001702
Minchan Kim8334b962015-09-08 15:00:24 -07001703/*
Hugh Dickins7b1fe592009-01-06 14:39:34 -08001704 * We can write to an anon page without COW if there are no other references
1705 * to it. And as a side-effect, free up its swap: because the old content
1706 * on disk will never be read, and seeking back there to write new content
1707 * later would only waste time away from clustering.
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07001708 *
Huang Yingba3c4ce2017-09-06 16:22:19 -07001709 * NOTE: total_map_swapcount should not be relied upon by the caller if
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07001710 * reuse_swap_page() returns false, but it may be always overwritten
1711 * (see the other implementation for CONFIG_SWAP=n).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 */
Huang Yingba3c4ce2017-09-06 16:22:19 -07001713bool reuse_swap_page(struct page *page, int *total_map_swapcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Huang Yingba3c4ce2017-09-06 16:22:19 -07001715 int count, total_mapcount, total_swapcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Sasha Levin309381fea2014-01-23 15:52:54 -08001717 VM_BUG_ON_PAGE(!PageLocked(page), page);
Hugh Dickins5ad64682009-12-14 17:59:24 -08001718 if (unlikely(PageKsm(page)))
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07001719 return false;
Huang Yingba3c4ce2017-09-06 16:22:19 -07001720 count = page_trans_huge_map_swapcount(page, &total_mapcount,
1721 &total_swapcount);
1722 if (total_map_swapcount)
1723 *total_map_swapcount = total_mapcount + total_swapcount;
1724 if (count == 1 && PageSwapCache(page) &&
1725 (likely(!PageTransCompound(page)) ||
1726 /* The remaining swap count will be freed soon */
1727 total_swapcount == page_swapcount(page))) {
Minchan Kimf0571422017-01-10 16:58:15 -08001728 if (!PageWriteback(page)) {
Huang Yingba3c4ce2017-09-06 16:22:19 -07001729 page = compound_head(page);
Hugh Dickins7b1fe592009-01-06 14:39:34 -08001730 delete_from_swap_cache(page);
1731 SetPageDirty(page);
Minchan Kimf0571422017-01-10 16:58:15 -08001732 } else {
1733 swp_entry_t entry;
1734 struct swap_info_struct *p;
1735
1736 entry.val = page_private(page);
1737 p = swap_info_get(entry);
1738 if (p->flags & SWP_STABLE_WRITES) {
1739 spin_unlock(&p->lock);
1740 return false;
1741 }
1742 spin_unlock(&p->lock);
Hugh Dickins7b1fe592009-01-06 14:39:34 -08001743 }
1744 }
Huang Yingba3c4ce2017-09-06 16:22:19 -07001745
Hugh Dickins5ad64682009-12-14 17:59:24 -08001746 return count <= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
1749/*
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -08001750 * If swap is getting full, or if there are no more mappings of this page,
1751 * then try_to_free_swap is called to free its swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 */
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -08001753int try_to_free_swap(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Sasha Levin309381fea2014-01-23 15:52:54 -08001755 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 if (!PageSwapCache(page))
1758 return 0;
1759 if (PageWriteback(page))
1760 return 0;
Huang Yinge0709822017-09-06 16:22:16 -07001761 if (page_swapped(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return 0;
1763
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -07001764 /*
1765 * Once hibernation has begun to create its image of memory,
1766 * there's a danger that one of the calls to try_to_free_swap()
1767 * - most probably a call from __try_to_reclaim_swap() while
1768 * hibernation is allocating its own swap pages for the image,
1769 * but conceivably even a call from memory reclaim - will free
1770 * the swap from a page which has already been recorded in the
1771 * image as a clean swapcache page, and then reuse its swap for
1772 * another page of the image. On waking from hibernation, the
1773 * original page might be freed under memory pressure, then
1774 * later read back in from swap, now with the wrong data.
1775 *
Seth Jennings2de1a7e2013-11-12 15:07:46 -08001776 * Hibernation suspends storage while it is writing the image
Mel Gormanf90ac392012-01-10 15:07:15 -08001777 * to disk so check that here.
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -07001778 */
Mel Gormanf90ac392012-01-10 15:07:15 -08001779 if (pm_suspended_storage())
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -07001780 return 0;
1781
Huang Yinge0709822017-09-06 16:22:16 -07001782 page = compound_head(page);
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -08001783 delete_from_swap_cache(page);
1784 SetPageDirty(page);
1785 return 1;
Rik van Riel68a223942008-10-18 20:26:23 -07001786}
1787
1788/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 * Free the swap entry like above, but also try to
1790 * free the page cache entry if it is the last user.
1791 */
Hugh Dickins2509ef22009-01-06 14:40:10 -08001792int free_swap_and_cache(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Hugh Dickins2509ef22009-01-06 14:40:10 -08001794 struct swap_info_struct *p;
Tim Chen7c00baf2017-02-22 15:45:36 -08001795 unsigned char count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Andi Kleena7420aa2009-09-16 11:50:05 +02001797 if (non_swap_entry(entry))
Hugh Dickins2509ef22009-01-06 14:40:10 -08001798 return 1;
Christoph Lameter06972122006-06-23 02:03:35 -07001799
Tim Chen7c00baf2017-02-22 15:45:36 -08001800 p = _swap_info_get(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (p) {
Wei Yang33e16272020-06-01 21:49:16 -07001802 count = __swap_entry_free(p, entry);
Huang Yinge0709822017-09-06 16:22:16 -07001803 if (count == SWAP_HAS_CACHE &&
Huang Yingbcd49e82018-10-26 15:03:46 -07001804 !swap_page_trans_huge_swapped(p, entry))
1805 __try_to_reclaim_swap(p, swp_offset(entry),
1806 TTRS_UNMAPPED | TTRS_FULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
Hugh Dickins2509ef22009-01-06 14:40:10 -08001808 return p != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +02001811#ifdef CONFIG_HIBERNATION
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001812/*
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001813 * Find the swap type that corresponds to given device (if any).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001814 *
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001815 * @offset - number of the PAGE_SIZE-sized block of the device, starting
1816 * from 0, in which the swap header is expected to be located.
1817 *
1818 * This is needed for the suspend to disk (aka swsusp).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001819 */
Christoph Hellwig21bd9002020-09-21 09:19:56 +02001820int swap_type_of(dev_t device, sector_t offset)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001821{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001822 int type;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001823
Christoph Hellwig21bd9002020-09-21 09:19:56 +02001824 if (!device)
1825 return -1;
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001826
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001827 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001828 for (type = 0; type < nr_swapfiles; type++) {
1829 struct swap_info_struct *sis = swap_info[type];
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001830
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001831 if (!(sis->flags & SWP_WRITEOK))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001832 continue;
Rafael J. Wysockib6b5bce2006-08-27 01:23:25 -07001833
Christoph Hellwig21bd9002020-09-21 09:19:56 +02001834 if (device == sis->bdev->bd_dev) {
Aaron Lu4efaceb2019-07-11 20:55:41 -07001835 struct swap_extent *se = first_se(sis);
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001836
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001837 if (se->start_block == offset) {
1838 spin_unlock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001839 return type;
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001840 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001841 }
1842 }
1843 spin_unlock(&swap_lock);
Christoph Hellwig21bd9002020-09-21 09:19:56 +02001844 return -ENODEV;
1845}
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001846
Christoph Hellwig21bd9002020-09-21 09:19:56 +02001847int find_first_swap(dev_t *device)
1848{
1849 int type;
1850
1851 spin_lock(&swap_lock);
1852 for (type = 0; type < nr_swapfiles; type++) {
1853 struct swap_info_struct *sis = swap_info[type];
1854
1855 if (!(sis->flags & SWP_WRITEOK))
1856 continue;
1857 *device = sis->bdev->bd_dev;
1858 spin_unlock(&swap_lock);
1859 return type;
1860 }
1861 spin_unlock(&swap_lock);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001862 return -ENODEV;
1863}
1864
1865/*
Hugh Dickins73c34b62009-12-14 17:58:43 -08001866 * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
1867 * corresponding to given index in swap_info (swap type).
1868 */
1869sector_t swapdev_block(int type, pgoff_t offset)
1870{
1871 struct block_device *bdev;
Daniel Jordanc10d38c2019-03-05 15:48:19 -08001872 struct swap_info_struct *si = swap_type_to_swap_info(type);
Hugh Dickins73c34b62009-12-14 17:58:43 -08001873
Daniel Jordanc10d38c2019-03-05 15:48:19 -08001874 if (!si || !(si->flags & SWP_WRITEOK))
Hugh Dickins73c34b62009-12-14 17:58:43 -08001875 return 0;
Lee Schermerhornd4906e12009-12-14 17:58:49 -08001876 return map_swap_entry(swp_entry(type, offset), &bdev);
Hugh Dickins73c34b62009-12-14 17:58:43 -08001877}
1878
1879/*
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001880 * Return either the total number of swap pages of given type, or the number
1881 * of free pages of that type (depending on @free)
1882 *
1883 * This is needed for software suspend
1884 */
1885unsigned int count_swap_pages(int type, int free)
1886{
1887 unsigned int n = 0;
1888
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001889 spin_lock(&swap_lock);
1890 if ((unsigned int)type < nr_swapfiles) {
1891 struct swap_info_struct *sis = swap_info[type];
1892
Shaohua Liec8acf22013-02-22 16:34:38 -08001893 spin_lock(&sis->lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001894 if (sis->flags & SWP_WRITEOK) {
1895 n = sis->pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001896 if (free)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001897 n -= sis->inuse_pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001898 }
Shaohua Liec8acf22013-02-22 16:34:38 -08001899 spin_unlock(&sis->lock);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001900 }
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001901 spin_unlock(&swap_lock);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001902 return n;
1903}
Hugh Dickins73c34b62009-12-14 17:58:43 -08001904#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001905
Hugh Dickins9f8bdb32016-01-15 16:57:34 -08001906static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte)
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001907{
Peter Xu12eb3c22021-06-15 18:23:16 -07001908 return pte_same(pte_swp_clear_flags(pte), swp_pte);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001909}
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911/*
Hugh Dickins72866f62005-10-29 18:15:55 -07001912 * No need to decide whether this PTE shares the swap entry with others,
1913 * just let do_wp_page work it out if a write is requested later - to
1914 * force COW, vm_page_prot omits write permission from any private vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 */
Hugh Dickins044d66c2008-02-07 00:14:04 -08001916static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 unsigned long addr, swp_entry_t entry, struct page *page)
1918{
Hugh Dickins9e16b7f2013-02-22 16:36:09 -08001919 struct page *swapcache;
Hugh Dickins044d66c2008-02-07 00:14:04 -08001920 spinlock_t *ptl;
1921 pte_t *pte;
1922 int ret = 1;
1923
Hugh Dickins9e16b7f2013-02-22 16:36:09 -08001924 swapcache = page;
1925 page = ksm_might_need_to_copy(page, vma, addr);
1926 if (unlikely(!page))
1927 return -ENOMEM;
1928
Hugh Dickins044d66c2008-02-07 00:14:04 -08001929 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Hugh Dickins9f8bdb32016-01-15 16:57:34 -08001930 if (unlikely(!pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) {
Hugh Dickins044d66c2008-02-07 00:14:04 -08001931 ret = 0;
1932 goto out;
1933 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001934
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -08001935 dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001936 inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 get_page(page);
1938 set_pte_at(vma->vm_mm, addr, pte,
1939 pte_mkold(mk_pte(page, vma->vm_page_prot)));
Johannes Weiner00501b52014-08-08 14:19:20 -07001940 if (page == swapcache) {
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001941 page_add_anon_rmap(page, vma, addr, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001942 } else { /* ksm created a completely new copy */
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001943 page_add_new_anon_rmap(page, vma, addr, false);
Joonsoo Kimb5181542020-08-11 18:30:40 -07001944 lru_cache_add_inactive_or_unevictable(page, vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 swap_free(entry);
Hugh Dickins044d66c2008-02-07 00:14:04 -08001947out:
1948 pte_unmap_unlock(pte, ptl);
Hugh Dickins9e16b7f2013-02-22 16:36:09 -08001949 if (page != swapcache) {
1950 unlock_page(page);
1951 put_page(page);
1952 }
Hugh Dickins044d66c2008-02-07 00:14:04 -08001953 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
1956static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001957 unsigned long addr, unsigned long end,
1958 unsigned int type, bool frontswap,
1959 unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001961 struct page *page;
1962 swp_entry_t entry;
Hugh Dickins705e87c2005-10-29 18:16:27 -07001963 pte_t *pte;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001964 struct swap_info_struct *si;
1965 unsigned long offset;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001966 int ret = 0;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001967 volatile unsigned char *swap_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001969 si = swap_info[type];
Hugh Dickins044d66c2008-02-07 00:14:04 -08001970 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 do {
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001972 struct vm_fault vmf;
1973
1974 if (!is_swap_pte(*pte))
1975 continue;
1976
1977 entry = pte_to_swp_entry(*pte);
1978 if (swp_type(entry) != type)
1979 continue;
1980
1981 offset = swp_offset(entry);
1982 if (frontswap && !frontswap_test(si, offset))
1983 continue;
1984
1985 pte_unmap(pte);
1986 swap_map = &si->swap_map[offset];
Andrea Righiebc59512020-06-01 21:48:43 -07001987 page = lookup_swap_cache(entry, vma, addr);
1988 if (!page) {
1989 vmf.vma = vma;
1990 vmf.address = addr;
1991 vmf.pmd = pmd;
1992 page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
1993 &vmf);
1994 }
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001995 if (!page) {
1996 if (*swap_map == 0 || *swap_map == SWAP_MAP_BAD)
1997 goto try_next;
1998 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002000
2001 lock_page(page);
2002 wait_on_page_writeback(page);
2003 ret = unuse_pte(vma, pmd, addr, entry, page);
2004 if (ret < 0) {
2005 unlock_page(page);
2006 put_page(page);
2007 goto out;
2008 }
2009
2010 try_to_free_swap(page);
2011 unlock_page(page);
2012 put_page(page);
2013
2014 if (*fs_pages_to_unuse && !--(*fs_pages_to_unuse)) {
2015 ret = FRONTSWAP_PAGES_UNUSED;
2016 goto out;
2017 }
2018try_next:
2019 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickins044d66c2008-02-07 00:14:04 -08002021 pte_unmap(pte - 1);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002022
2023 ret = 0;
Hugh Dickins044d66c2008-02-07 00:14:04 -08002024out:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002025 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
2028static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
2029 unsigned long addr, unsigned long end,
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002030 unsigned int type, bool frontswap,
2031 unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 pmd_t *pmd;
2034 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002035 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 pmd = pmd_offset(pud, addr);
2038 do {
Hugh Dickinsdc644a02016-12-12 16:44:44 -08002039 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 next = pmd_addr_end(addr, end);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07002041 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002043 ret = unuse_pte_range(vma, pmd, addr, next, type,
2044 frontswap, fs_pages_to_unuse);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002045 if (ret)
2046 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 } while (pmd++, addr = next, addr != end);
2048 return 0;
2049}
2050
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002051static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 unsigned long addr, unsigned long end,
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002053 unsigned int type, bool frontswap,
2054 unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 pud_t *pud;
2057 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002058 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002060 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 do {
2062 next = pud_addr_end(addr, end);
2063 if (pud_none_or_clear_bad(pud))
2064 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002065 ret = unuse_pmd_range(vma, pud, addr, next, type,
2066 frontswap, fs_pages_to_unuse);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002067 if (ret)
2068 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 } while (pud++, addr = next, addr != end);
2070 return 0;
2071}
2072
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002073static inline int unuse_p4d_range(struct vm_area_struct *vma, pgd_t *pgd,
2074 unsigned long addr, unsigned long end,
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002075 unsigned int type, bool frontswap,
2076 unsigned long *fs_pages_to_unuse)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002077{
2078 p4d_t *p4d;
2079 unsigned long next;
2080 int ret;
2081
2082 p4d = p4d_offset(pgd, addr);
2083 do {
2084 next = p4d_addr_end(addr, end);
2085 if (p4d_none_or_clear_bad(p4d))
2086 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002087 ret = unuse_pud_range(vma, p4d, addr, next, type,
2088 frontswap, fs_pages_to_unuse);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002089 if (ret)
2090 return ret;
2091 } while (p4d++, addr = next, addr != end);
2092 return 0;
2093}
2094
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002095static int unuse_vma(struct vm_area_struct *vma, unsigned int type,
2096 bool frontswap, unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
2098 pgd_t *pgd;
2099 unsigned long addr, end, next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002100 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002102 addr = vma->vm_start;
2103 end = vma->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 pgd = pgd_offset(vma->vm_mm, addr);
2106 do {
2107 next = pgd_addr_end(addr, end);
2108 if (pgd_none_or_clear_bad(pgd))
2109 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002110 ret = unuse_p4d_range(vma, pgd, addr, next, type,
2111 frontswap, fs_pages_to_unuse);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002112 if (ret)
2113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 } while (pgd++, addr = next, addr != end);
2115 return 0;
2116}
2117
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002118static int unuse_mm(struct mm_struct *mm, unsigned int type,
2119 bool frontswap, unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 struct vm_area_struct *vma;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002122 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002124 mmap_read_lock(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002126 if (vma->anon_vma) {
2127 ret = unuse_vma(vma, type, frontswap,
2128 fs_pages_to_unuse);
2129 if (ret)
2130 break;
2131 }
Hugh Dickinsdc644a02016-12-12 16:44:44 -08002132 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002134 mmap_read_unlock(mm);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002135 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136}
2137
2138/*
Dan Magenheimer38b5faf2012-04-09 17:08:06 -06002139 * Scan swap_map (or frontswap_map if frontswap parameter is true)
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002140 * from current position to next entry still in use. Return 0
2141 * if there are no inuse entries after prev till end of the map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 */
Hugh Dickins6eb396d2005-09-03 15:54:35 -07002143static unsigned int find_next_to_unuse(struct swap_info_struct *si,
Dan Magenheimer38b5faf2012-04-09 17:08:06 -06002144 unsigned int prev, bool frontswap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002146 unsigned int i;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08002147 unsigned char count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 /*
Hugh Dickins5d337b92005-09-03 15:54:41 -07002150 * No need for swap_lock here: we're just looking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 * for whether an entry is in use, not modifying it; false
2152 * hits are okay, and sys_swapoff() has already prevented new
Hugh Dickins5d337b92005-09-03 15:54:41 -07002153 * allocations from this area (while holding swap_lock).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 */
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002155 for (i = prev + 1; i < si->max; i++) {
Jason Low4db0c3c2015-04-15 16:14:08 -07002156 count = READ_ONCE(si->swap_map[i]);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002157 if (count && swap_count(count) != SWAP_MAP_BAD)
Hugh Dickinsdc644a02016-12-12 16:44:44 -08002158 if (!frontswap || frontswap_test(si, i))
2159 break;
2160 if ((i % LATENCY_LIMIT) == 0)
2161 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002163
2164 if (i == si->max)
2165 i = 0;
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return i;
2168}
2169
2170/*
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002171 * If the boolean frontswap is true, only unuse pages_to_unuse pages;
Dan Magenheimer38b5faf2012-04-09 17:08:06 -06002172 * pages_to_unuse==0 means all pages; ignored if frontswap is false
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 */
Dan Magenheimer38b5faf2012-04-09 17:08:06 -06002174int try_to_unuse(unsigned int type, bool frontswap,
2175 unsigned long pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002177 struct mm_struct *prev_mm;
2178 struct mm_struct *mm;
2179 struct list_head *p;
2180 int retval = 0;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002181 struct swap_info_struct *si = swap_info[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 struct page *page;
2183 swp_entry_t entry;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002184 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Qian Cai21820942020-04-01 21:06:13 -07002186 if (!READ_ONCE(si->inuse_pages))
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002189 if (!frontswap)
2190 pages_to_unuse = 0;
2191
2192retry:
2193 retval = shmem_unuse(type, frontswap, &pages_to_unuse);
2194 if (retval)
2195 goto out;
2196
2197 prev_mm = &init_mm;
2198 mmget(prev_mm);
2199
2200 spin_lock(&mmlist_lock);
2201 p = &init_mm.mmlist;
Qian Cai21820942020-04-01 21:06:13 -07002202 while (READ_ONCE(si->inuse_pages) &&
Hugh Dickins64165b12019-04-18 17:50:09 -07002203 !signal_pending(current) &&
2204 (p = p->next) != &init_mm.mmlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002206 mm = list_entry(p, struct mm_struct, mmlist);
2207 if (!mmget_not_zero(mm))
Hugh Dickinsaaa46862009-12-14 17:58:47 -08002208 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002209 spin_unlock(&mmlist_lock);
2210 mmput(prev_mm);
2211 prev_mm = mm;
2212 retval = unuse_mm(mm, type, frontswap, &pages_to_unuse);
Hugh Dickinsaaa46862009-12-14 17:58:47 -08002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (retval) {
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002215 mmput(prev_mm);
2216 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
2218
2219 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 * Make sure that we aren't completely killing
2221 * interactive performance.
2222 */
2223 cond_resched();
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002224 spin_lock(&mmlist_lock);
2225 }
2226 spin_unlock(&mmlist_lock);
2227
2228 mmput(prev_mm);
2229
2230 i = 0;
Qian Cai21820942020-04-01 21:06:13 -07002231 while (READ_ONCE(si->inuse_pages) &&
Hugh Dickins64165b12019-04-18 17:50:09 -07002232 !signal_pending(current) &&
2233 (i = find_next_to_unuse(si, i, frontswap)) != 0) {
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002234
2235 entry = swp_entry(type, i);
2236 page = find_get_page(swap_address_space(entry), i);
2237 if (!page)
2238 continue;
2239
2240 /*
2241 * It is conceivable that a racing task removed this page from
2242 * swap cache just before we acquired the page lock. The page
2243 * might even be back in swap cache on another swap area. But
2244 * that is okay, try_to_free_swap() only removes stale pages.
2245 */
2246 lock_page(page);
2247 wait_on_page_writeback(page);
2248 try_to_free_swap(page);
2249 unlock_page(page);
2250 put_page(page);
2251
2252 /*
2253 * For frontswap, we just need to unuse pages_to_unuse, if
2254 * it was specified. Need not check frontswap again here as
2255 * we already zeroed out pages_to_unuse if not frontswap.
2256 */
2257 if (pages_to_unuse && --pages_to_unuse == 0)
2258 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
2260
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002261 /*
2262 * Lets check again to see if there are still swap entries in the map.
2263 * If yes, we would need to do retry the unuse logic again.
2264 * Under global memory pressure, swap entries can be reinserted back
2265 * into process space after the mmlist loop above passes over them.
Hugh Dickinsdd862de2019-04-18 17:50:02 -07002266 *
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07002267 * Limit the number of retries? No: when mmget_not_zero() above fails,
2268 * that mm is likely to be freeing swap from exit_mmap(), which proceeds
2269 * at its own independent pace; and even shmem_writepage() could have
2270 * been preempted after get_swap_page(), temporarily hiding that swap.
2271 * It's easy and robust (though cpu-intensive) just to keep retrying.
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002272 */
Qian Cai21820942020-04-01 21:06:13 -07002273 if (READ_ONCE(si->inuse_pages)) {
Hugh Dickins64165b12019-04-18 17:50:09 -07002274 if (!signal_pending(current))
2275 goto retry;
2276 retval = -EINTR;
2277 }
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002278out:
2279 return (retval == FRONTSWAP_PAGES_UNUSED) ? 0 : retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
2282/*
Hugh Dickins5d337b92005-09-03 15:54:41 -07002283 * After a successful try_to_unuse, if no swap is now in use, we know
2284 * we can empty the mmlist. swap_lock must be held on entry and exit.
2285 * Note that mmlist_lock nests inside swap_lock, and an mm must be
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 * added to the mmlist just after page_duplicate - before would be racy.
2287 */
2288static void drain_mmlist(void)
2289{
2290 struct list_head *p, *next;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002291 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002293 for (type = 0; type < nr_swapfiles; type++)
2294 if (swap_info[type]->inuse_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return;
2296 spin_lock(&mmlist_lock);
2297 list_for_each_safe(p, next, &init_mm.mmlist)
2298 list_del_init(p);
2299 spin_unlock(&mmlist_lock);
2300}
2301
2302/*
2303 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
Lee Schermerhornd4906e12009-12-14 17:58:49 -08002304 * corresponds to page offset for the specified swap entry.
2305 * Note that the type of this function is sector_t, but it returns page offset
2306 * into the bdev, not sector offset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 */
Lee Schermerhornd4906e12009-12-14 17:58:49 -08002308static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -08002310 struct swap_info_struct *sis;
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -08002311 struct swap_extent *se;
2312 pgoff_t offset;
2313
Daniel Jordanc10d38c2019-03-05 15:48:19 -08002314 sis = swp_swap_info(entry);
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -08002315 *bdev = sis->bdev;
2316
2317 offset = swp_offset(entry);
Aaron Lu4efaceb2019-07-11 20:55:41 -07002318 se = offset_to_swap_extent(sis, offset);
2319 return se->start_block + (offset - se->start_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
2322/*
Lee Schermerhornd4906e12009-12-14 17:58:49 -08002323 * Returns the page offset into bdev for the specified page's swap entry.
2324 */
2325sector_t map_swap_page(struct page *page, struct block_device **bdev)
2326{
2327 swp_entry_t entry;
2328 entry.val = page_private(page);
2329 return map_swap_entry(entry, bdev);
2330}
2331
2332/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 * Free all of a swapdev's extent information
2334 */
2335static void destroy_swap_extents(struct swap_info_struct *sis)
2336{
Aaron Lu4efaceb2019-07-11 20:55:41 -07002337 while (!RB_EMPTY_ROOT(&sis->swap_extent_root)) {
2338 struct rb_node *rb = sis->swap_extent_root.rb_node;
2339 struct swap_extent *se = rb_entry(rb, struct swap_extent, rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Aaron Lu4efaceb2019-07-11 20:55:41 -07002341 rb_erase(rb, &sis->swap_extent_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 kfree(se);
2343 }
Mel Gorman62c230b2012-07-31 16:44:55 -07002344
Omar Sandovalbc4ae272018-10-26 15:10:51 -07002345 if (sis->flags & SWP_ACTIVATED) {
Mel Gorman62c230b2012-07-31 16:44:55 -07002346 struct file *swap_file = sis->swap_file;
2347 struct address_space *mapping = swap_file->f_mapping;
2348
Omar Sandovalbc4ae272018-10-26 15:10:51 -07002349 sis->flags &= ~SWP_ACTIVATED;
2350 if (mapping->a_ops->swap_deactivate)
2351 mapping->a_ops->swap_deactivate(swap_file);
Mel Gorman62c230b2012-07-31 16:44:55 -07002352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
2355/*
2356 * Add a block range (and the corresponding page range) into this swapdev's
Aaron Lu4efaceb2019-07-11 20:55:41 -07002357 * extent tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 *
Hugh Dickins11d31882005-09-03 15:54:34 -07002359 * This function rather assumes that it is called in ascending page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 */
Mel Gormana509bc12012-07-31 16:44:57 -07002361int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
2363 unsigned long nr_pages, sector_t start_block)
2364{
Aaron Lu4efaceb2019-07-11 20:55:41 -07002365 struct rb_node **link = &sis->swap_extent_root.rb_node, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 struct swap_extent *se;
2367 struct swap_extent *new_se;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Aaron Lu4efaceb2019-07-11 20:55:41 -07002369 /*
2370 * place the new node at the right most since the
2371 * function is called in ascending page order.
2372 */
2373 while (*link) {
2374 parent = *link;
2375 link = &parent->rb_right;
2376 }
2377
2378 if (parent) {
2379 se = rb_entry(parent, struct swap_extent, rb_node);
Hugh Dickins11d31882005-09-03 15:54:34 -07002380 BUG_ON(se->start_page + se->nr_pages != start_page);
2381 if (se->start_block + se->nr_pages == start_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 /* Merge it */
2383 se->nr_pages += nr_pages;
2384 return 0;
2385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387
Aaron Lu4efaceb2019-07-11 20:55:41 -07002388 /* No merge, insert a new extent. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
2390 if (new_se == NULL)
2391 return -ENOMEM;
2392 new_se->start_page = start_page;
2393 new_se->nr_pages = nr_pages;
2394 new_se->start_block = start_block;
2395
Aaron Lu4efaceb2019-07-11 20:55:41 -07002396 rb_link_node(&new_se->rb_node, parent, link);
2397 rb_insert_color(&new_se->rb_node, &sis->swap_extent_root);
Hugh Dickins53092a72005-09-03 15:54:34 -07002398 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
Omar Sandovalaa8aa8a2018-10-26 15:10:55 -07002400EXPORT_SYMBOL_GPL(add_swap_extent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402/*
2403 * A `swap extent' is a simple thing which maps a contiguous range of pages
2404 * onto a contiguous range of disk blocks. An ordered list of swap extents
2405 * is built at swapon time and is then used at swap_writepage/swap_readpage
2406 * time for locating where on disk a page belongs.
2407 *
2408 * If the swapfile is an S_ISBLK block device, a single extent is installed.
2409 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
2410 * swap files identically.
2411 *
2412 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
2413 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
2414 * swapfiles are handled *identically* after swapon time.
2415 *
2416 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
2417 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
2418 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
2419 * requirements, they are simply tossed out - we will never use those blocks
2420 * for swapping.
2421 *
Darrick J. Wong16380452019-08-20 07:55:16 -07002422 * For all swap devices we set S_SWAPFILE across the life of the swapon. This
2423 * prevents users from writing to the swap device, which will corrupt memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 *
2425 * The amount of disk space which a single swap extent represents varies.
2426 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
2427 * extents in the list. To avoid much list walking, we cache the previous
2428 * search location in `curr_swap_extent', and start new searches from there.
2429 * This is extremely effective. The average number of iterations in
2430 * map_swap_page() has been measured at about 0.3 per page. - akpm.
2431 */
Hugh Dickins53092a72005-09-03 15:54:34 -07002432static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
Mel Gorman62c230b2012-07-31 16:44:55 -07002434 struct file *swap_file = sis->swap_file;
2435 struct address_space *mapping = swap_file->f_mapping;
2436 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 int ret;
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (S_ISBLK(inode->i_mode)) {
2440 ret = add_swap_extent(sis, 0, sis->max, 0);
Hugh Dickins53092a72005-09-03 15:54:34 -07002441 *span = sis->pages;
Mel Gormana509bc12012-07-31 16:44:57 -07002442 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444
Mel Gorman62c230b2012-07-31 16:44:55 -07002445 if (mapping->a_ops->swap_activate) {
Mel Gormana509bc12012-07-31 16:44:57 -07002446 ret = mapping->a_ops->swap_activate(sis, swap_file, span);
Omar Sandovalbc4ae272018-10-26 15:10:51 -07002447 if (ret >= 0)
2448 sis->flags |= SWP_ACTIVATED;
Mel Gorman62c230b2012-07-31 16:44:55 -07002449 if (!ret) {
Gao Xiang32646312020-10-13 16:52:04 -07002450 sis->flags |= SWP_FS_OPS;
Mel Gorman62c230b2012-07-31 16:44:55 -07002451 ret = add_swap_extent(sis, 0, sis->max, 0);
2452 *span = sis->pages;
2453 }
Mel Gormana509bc12012-07-31 16:44:57 -07002454 return ret;
Mel Gorman62c230b2012-07-31 16:44:55 -07002455 }
2456
Mel Gormana509bc12012-07-31 16:44:57 -07002457 return generic_swapfile_activate(sis, swap_file, span);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
Aaron Lua2468cc2017-09-06 16:24:57 -07002460static int swap_node(struct swap_info_struct *p)
2461{
2462 struct block_device *bdev;
2463
2464 if (p->bdev)
2465 bdev = p->bdev;
2466 else
2467 bdev = p->swap_file->f_inode->i_sb->s_bdev;
2468
2469 return bdev ? bdev->bd_disk->node_id : NUMA_NO_NODE;
2470}
2471
Huang Yingeb085572019-07-11 20:55:33 -07002472static void setup_swap_info(struct swap_info_struct *p, int prio,
2473 unsigned char *swap_map,
2474 struct swap_cluster_info *cluster_info)
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002475{
Aaron Lua2468cc2017-09-06 16:24:57 -07002476 int i;
2477
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002478 if (prio >= 0)
2479 p->prio = prio;
2480 else
2481 p->prio = --least_priority;
Dan Streetman18ab4d42014-06-04 16:09:59 -07002482 /*
2483 * the plist prio is negated because plist ordering is
2484 * low-to-high, while swap ordering is high-to-low
2485 */
2486 p->list.prio = -p->prio;
Aaron Lua2468cc2017-09-06 16:24:57 -07002487 for_each_node(i) {
2488 if (p->prio >= 0)
2489 p->avail_lists[i].prio = -p->prio;
2490 else {
2491 if (swap_node(p) == i)
2492 p->avail_lists[i].prio = 1;
2493 else
2494 p->avail_lists[i].prio = -p->prio;
2495 }
2496 }
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002497 p->swap_map = swap_map;
Shaohua Li2a8f9442013-09-11 14:20:28 -07002498 p->cluster_info = cluster_info;
Huang Yingeb085572019-07-11 20:55:33 -07002499}
2500
2501static void _enable_swap_info(struct swap_info_struct *p)
2502{
2503 p->flags |= SWP_WRITEOK | SWP_VALID;
Shaohua Liec8acf22013-02-22 16:34:38 -08002504 atomic_long_add(p->pages, &nr_swap_pages);
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002505 total_swap_pages += p->pages;
2506
Dan Streetmanadfab832014-06-04 16:09:53 -07002507 assert_spin_locked(&swap_lock);
Dan Streetmanadfab832014-06-04 16:09:53 -07002508 /*
Dan Streetman18ab4d42014-06-04 16:09:59 -07002509 * both lists are plists, and thus priority ordered.
2510 * swap_active_head needs to be priority ordered for swapoff(),
2511 * which on removal of any swap_info_struct with an auto-assigned
2512 * (i.e. negative) priority increments the auto-assigned priority
2513 * of any lower-priority swap_info_structs.
2514 * swap_avail_head needs to be priority ordered for get_swap_page(),
2515 * which allocates swap pages from the highest available priority
2516 * swap_info_struct.
Dan Streetmanadfab832014-06-04 16:09:53 -07002517 */
Dan Streetman18ab4d42014-06-04 16:09:59 -07002518 plist_add(&p->list, &swap_active_head);
Aaron Lua2468cc2017-09-06 16:24:57 -07002519 add_to_avail_list(p);
Cesar Eduardo Barroscf0cac02012-12-11 16:01:13 -08002520}
2521
2522static void enable_swap_info(struct swap_info_struct *p, int prio,
2523 unsigned char *swap_map,
Shaohua Li2a8f9442013-09-11 14:20:28 -07002524 struct swap_cluster_info *cluster_info,
Cesar Eduardo Barroscf0cac02012-12-11 16:01:13 -08002525 unsigned long *frontswap_map)
2526{
Minchan Kim4f898492013-04-30 15:26:54 -07002527 frontswap_init(p->type, frontswap_map);
Cesar Eduardo Barroscf0cac02012-12-11 16:01:13 -08002528 spin_lock(&swap_lock);
Shaohua Liec8acf22013-02-22 16:34:38 -08002529 spin_lock(&p->lock);
Huang Yingeb085572019-07-11 20:55:33 -07002530 setup_swap_info(p, prio, swap_map, cluster_info);
2531 spin_unlock(&p->lock);
2532 spin_unlock(&swap_lock);
2533 /*
2534 * Guarantee swap_map, cluster_info, etc. fields are valid
2535 * between get/put_swap_device() if SWP_VALID bit is set
2536 */
2537 synchronize_rcu();
2538 spin_lock(&swap_lock);
2539 spin_lock(&p->lock);
2540 _enable_swap_info(p);
Shaohua Liec8acf22013-02-22 16:34:38 -08002541 spin_unlock(&p->lock);
Cesar Eduardo Barroscf0cac02012-12-11 16:01:13 -08002542 spin_unlock(&swap_lock);
2543}
2544
2545static void reinsert_swap_info(struct swap_info_struct *p)
2546{
2547 spin_lock(&swap_lock);
Shaohua Liec8acf22013-02-22 16:34:38 -08002548 spin_lock(&p->lock);
Huang Yingeb085572019-07-11 20:55:33 -07002549 setup_swap_info(p, p->prio, p->swap_map, p->cluster_info);
2550 _enable_swap_info(p);
Shaohua Liec8acf22013-02-22 16:34:38 -08002551 spin_unlock(&p->lock);
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002552 spin_unlock(&swap_lock);
2553}
2554
Tim Chen67afa382017-02-22 15:45:39 -08002555bool has_usable_swap(void)
2556{
2557 bool ret = true;
2558
2559 spin_lock(&swap_lock);
2560 if (plist_head_empty(&swap_active_head))
2561 ret = false;
2562 spin_unlock(&swap_lock);
2563 return ret;
2564}
2565
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01002566SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Hugh Dickins73c34b62009-12-14 17:58:43 -08002568 struct swap_info_struct *p = NULL;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08002569 unsigned char *swap_map;
Shaohua Li2a8f9442013-09-11 14:20:28 -07002570 struct swap_cluster_info *cluster_info;
Minchan Kim4f898492013-04-30 15:26:54 -07002571 unsigned long *frontswap_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 struct file *swap_file, *victim;
2573 struct address_space *mapping;
2574 struct inode *inode;
Jeff Layton91a27b22012-10-10 15:25:28 -04002575 struct filename *pathname;
Dan Streetmanadfab832014-06-04 16:09:53 -07002576 int err, found = 0;
Krzysztof Kozlowski5b808a22013-10-16 13:47:06 -07002577 unsigned int old_block_size;
Hugh Dickins886bb7e2009-01-06 14:39:48 -08002578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (!capable(CAP_SYS_ADMIN))
2580 return -EPERM;
2581
Al Viro191c5422012-02-13 03:58:52 +00002582 BUG_ON(!current->mm);
2583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 pathname = getname(specialfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (IS_ERR(pathname))
Xiaotian Fengf58b59c2012-11-16 14:14:55 -08002586 return PTR_ERR(pathname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Jeff Layton669abf42012-10-10 16:43:10 -04002588 victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 err = PTR_ERR(victim);
2590 if (IS_ERR(victim))
2591 goto out;
2592
2593 mapping = victim->f_mapping;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002594 spin_lock(&swap_lock);
Dan Streetman18ab4d42014-06-04 16:09:59 -07002595 plist_for_each_entry(p, &swap_active_head, list) {
Hugh Dickins22c6f8f2009-01-06 14:39:48 -08002596 if (p->flags & SWP_WRITEOK) {
Dan Streetmanadfab832014-06-04 16:09:53 -07002597 if (p->swap_file->f_mapping == mapping) {
2598 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 break;
Dan Streetmanadfab832014-06-04 16:09:53 -07002600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
Dan Streetmanadfab832014-06-04 16:09:53 -07002603 if (!found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 err = -EINVAL;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002605 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 goto out_dput;
2607 }
Al Viro191c5422012-02-13 03:58:52 +00002608 if (!security_vm_enough_memory_mm(current->mm, p->pages))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 vm_unacct_memory(p->pages);
2610 else {
2611 err = -ENOMEM;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002612 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 goto out_dput;
2614 }
Shaohua Liec8acf22013-02-22 16:34:38 -08002615 spin_lock(&p->lock);
Rongwei Wangea8c42b2023-04-04 23:47:16 +08002616 del_from_avail_list(p);
Hugh Dickins78ecba02008-07-23 21:28:23 -07002617 if (p->prio < 0) {
Dan Streetmanadfab832014-06-04 16:09:53 -07002618 struct swap_info_struct *si = p;
Aaron Lua2468cc2017-09-06 16:24:57 -07002619 int nid;
Dan Streetmanadfab832014-06-04 16:09:53 -07002620
Dan Streetman18ab4d42014-06-04 16:09:59 -07002621 plist_for_each_entry_continue(si, &swap_active_head, list) {
Dan Streetmanadfab832014-06-04 16:09:53 -07002622 si->prio++;
Dan Streetman18ab4d42014-06-04 16:09:59 -07002623 si->list.prio--;
Aaron Lua2468cc2017-09-06 16:24:57 -07002624 for_each_node(nid) {
2625 if (si->avail_lists[nid].prio != 1)
2626 si->avail_lists[nid].prio--;
2627 }
Dan Streetmanadfab832014-06-04 16:09:53 -07002628 }
Hugh Dickins78ecba02008-07-23 21:28:23 -07002629 least_priority++;
2630 }
Dan Streetman18ab4d42014-06-04 16:09:59 -07002631 plist_del(&p->list, &swap_active_head);
Shaohua Liec8acf22013-02-22 16:34:38 -08002632 atomic_long_sub(p->pages, &nr_swap_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 total_swap_pages -= p->pages;
2634 p->flags &= ~SWP_WRITEOK;
Shaohua Liec8acf22013-02-22 16:34:38 -08002635 spin_unlock(&p->lock);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002636 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07002637
Tim Chen039939a2017-02-22 15:45:43 -08002638 disable_swap_slots_cache_lock();
2639
David Rientjese1e12d22012-12-11 16:02:56 -08002640 set_current_oom_origin();
Dan Streetmanadfab832014-06-04 16:09:53 -07002641 err = try_to_unuse(p->type, false, 0); /* force unuse all pages */
David Rientjese1e12d22012-12-11 16:02:56 -08002642 clear_current_oom_origin();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (err) {
2645 /* re-insert swap space back into swap_list */
Cesar Eduardo Barroscf0cac02012-12-11 16:01:13 -08002646 reinsert_swap_info(p);
Tim Chen039939a2017-02-22 15:45:43 -08002647 reenable_swap_slots_cache_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 goto out_dput;
2649 }
Hugh Dickins52b7efdb2005-09-03 15:54:39 -07002650
Tim Chen039939a2017-02-22 15:45:43 -08002651 reenable_swap_slots_cache_unlock();
2652
Huang Yingeb085572019-07-11 20:55:33 -07002653 spin_lock(&swap_lock);
2654 spin_lock(&p->lock);
2655 p->flags &= ~SWP_VALID; /* mark swap device as invalid */
2656 spin_unlock(&p->lock);
2657 spin_unlock(&swap_lock);
2658 /*
2659 * wait for swap operations protected by get/put_swap_device()
2660 * to complete
2661 */
2662 synchronize_rcu();
2663
Shaohua Li815c2c52013-09-11 14:20:30 -07002664 flush_work(&p->discard_work);
2665
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07002666 destroy_swap_extents(p);
Hugh Dickins570a335b2009-12-14 17:58:46 -08002667 if (p->flags & SWP_CONTINUED)
2668 free_swap_count_continuations(p);
2669
Huang Ying81a02982017-09-06 16:24:43 -07002670 if (!p->bdev || !blk_queue_nonrot(bdev_get_queue(p->bdev)))
2671 atomic_dec(&nr_rotate_swap);
2672
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002673 mutex_lock(&swapon_mutex);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002674 spin_lock(&swap_lock);
Shaohua Liec8acf22013-02-22 16:34:38 -08002675 spin_lock(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 drain_mmlist();
Hugh Dickins5d337b92005-09-03 15:54:41 -07002677
2678 /* wait for anyone still in scan_swap_map */
2679 p->highest_bit = 0; /* cuts scans short */
2680 while (p->flags >= SWP_SCANNING) {
Shaohua Liec8acf22013-02-22 16:34:38 -08002681 spin_unlock(&p->lock);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002682 spin_unlock(&swap_lock);
Nishanth Aravamudan13e4b572005-09-10 00:27:25 -07002683 schedule_timeout_uninterruptible(1);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002684 spin_lock(&swap_lock);
Shaohua Liec8acf22013-02-22 16:34:38 -08002685 spin_lock(&p->lock);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002686 }
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 swap_file = p->swap_file;
Krzysztof Kozlowski5b808a22013-10-16 13:47:06 -07002689 old_block_size = p->old_block_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 p->swap_file = NULL;
2691 p->max = 0;
2692 swap_map = p->swap_map;
2693 p->swap_map = NULL;
Shaohua Li2a8f9442013-09-11 14:20:28 -07002694 cluster_info = p->cluster_info;
2695 p->cluster_info = NULL;
Minchan Kim4f898492013-04-30 15:26:54 -07002696 frontswap_map = frontswap_map_get(p);
Shaohua Liec8acf22013-02-22 16:34:38 -08002697 spin_unlock(&p->lock);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002698 spin_unlock(&swap_lock);
Steven Price8a848022020-05-13 16:37:49 +01002699 arch_swap_invalidate_area(p->type);
Dan Streetmanadfab832014-06-04 16:09:53 -07002700 frontswap_invalidate_area(p->type);
Krzysztof Kozlowski58e97ba2013-11-12 15:07:47 -08002701 frontswap_map_set(p, NULL);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002702 mutex_unlock(&swapon_mutex);
Shaohua Liebc2a1a2013-09-11 14:20:32 -07002703 free_percpu(p->percpu_cluster);
2704 p->percpu_cluster = NULL;
Huang Ying49070582020-06-01 21:49:22 -07002705 free_percpu(p->cluster_next_cpu);
2706 p->cluster_next_cpu = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 vfree(swap_map);
Huang Ying54f180d2017-05-08 15:57:40 -07002708 kvfree(cluster_info);
2709 kvfree(frontswap_map);
Seth Jennings2de1a7e2013-11-12 15:07:46 -08002710 /* Destroy swap account information */
Dan Streetmanadfab832014-06-04 16:09:53 -07002711 swap_cgroup_swapoff(p->type);
Huang, Ying4b3ef9d2017-02-22 15:45:26 -08002712 exit_swap_address_space(p->type);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08002713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 inode = mapping->host;
2715 if (S_ISBLK(inode->i_mode)) {
2716 struct block_device *bdev = I_BDEV(inode);
Darrick J. Wong16380452019-08-20 07:55:16 -07002717
Krzysztof Kozlowski5b808a22013-10-16 13:47:06 -07002718 set_blocksize(bdev, old_block_size);
Tejun Heoe525fd82010-11-13 11:55:17 +01002719 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
Darrick J. Wong16380452019-08-20 07:55:16 -07002721
2722 inode_lock(inode);
2723 inode->i_flags &= ~S_SWAPFILE;
2724 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 filp_close(swap_file, NULL);
Weijie Yangf893ab42014-02-06 12:04:23 -08002726
2727 /*
2728 * Clear the SWP_USED flag after all resources are freed so that swapon
2729 * can reuse this swap_info in alloc_swap_info() safely. It is ok to
2730 * not hold p->lock after we cleared its SWP_WRITEOK.
2731 */
2732 spin_lock(&swap_lock);
2733 p->flags = 0;
2734 spin_unlock(&swap_lock);
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 err = 0;
Kay Sievers66d7dd52010-10-26 14:22:06 -07002737 atomic_inc(&proc_poll_event);
2738 wake_up_interruptible(&proc_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740out_dput:
2741 filp_close(victim, NULL);
2742out:
Xiaotian Fengf58b59c2012-11-16 14:14:55 -08002743 putname(pathname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return err;
2745}
2746
2747#ifdef CONFIG_PROC_FS
Al Viro9dd95742017-07-03 00:42:43 -04002748static __poll_t swaps_poll(struct file *file, poll_table *wait)
Kay Sievers66d7dd52010-10-26 14:22:06 -07002749{
Kay Sieversf1514632011-07-12 20:48:39 +02002750 struct seq_file *seq = file->private_data;
Kay Sievers66d7dd52010-10-26 14:22:06 -07002751
2752 poll_wait(file, &proc_poll_wait, wait);
2753
Kay Sieversf1514632011-07-12 20:48:39 +02002754 if (seq->poll_event != atomic_read(&proc_poll_event)) {
2755 seq->poll_event = atomic_read(&proc_poll_event);
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002756 return EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
Kay Sievers66d7dd52010-10-26 14:22:06 -07002757 }
2758
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002759 return EPOLLIN | EPOLLRDNORM;
Kay Sievers66d7dd52010-10-26 14:22:06 -07002760}
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762/* iterator */
2763static void *swap_start(struct seq_file *swap, loff_t *pos)
2764{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002765 struct swap_info_struct *si;
2766 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 loff_t l = *pos;
2768
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002769 mutex_lock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08002771 if (!l)
2772 return SEQ_START_TOKEN;
2773
Daniel Jordanc10d38c2019-03-05 15:48:19 -08002774 for (type = 0; (si = swap_type_to_swap_info(type)); type++) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002775 if (!(si->flags & SWP_USED) || !si->swap_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 continue;
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08002777 if (!--l)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002778 return si;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
2780
2781 return NULL;
2782}
2783
2784static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
2785{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002786 struct swap_info_struct *si = v;
2787 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08002789 if (v == SEQ_START_TOKEN)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002790 type = 0;
2791 else
2792 type = si->type + 1;
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08002793
Vasily Averin10c8d692020-01-30 22:13:39 -08002794 ++(*pos);
Daniel Jordanc10d38c2019-03-05 15:48:19 -08002795 for (; (si = swap_type_to_swap_info(type)); type++) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002796 if (!(si->flags & SWP_USED) || !si->swap_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 continue;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002798 return si;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
2800
2801 return NULL;
2802}
2803
2804static void swap_stop(struct seq_file *swap, void *v)
2805{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002806 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807}
2808
2809static int swap_show(struct seq_file *swap, void *v)
2810{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002811 struct swap_info_struct *si = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 struct file *file;
2813 int len;
Randy Dunlap6f793942020-06-01 21:49:26 -07002814 unsigned int bytes, inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002816 if (si == SEQ_START_TOKEN) {
Randy Dunlap6f793942020-06-01 21:49:26 -07002817 seq_puts(swap,"Filename\t\t\t\tType\t\tSize\t\tUsed\t\tPriority\n");
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08002818 return 0;
2819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Randy Dunlap6f793942020-06-01 21:49:26 -07002821 bytes = si->pages << (PAGE_SHIFT - 10);
2822 inuse = si->inuse_pages << (PAGE_SHIFT - 10);
2823
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002824 file = si->swap_file;
Miklos Szeredi2726d562015-06-19 10:30:28 +02002825 len = seq_file_path(swap, file, " \t\n\\");
Randy Dunlap6f793942020-06-01 21:49:26 -07002826 seq_printf(swap, "%*s%s\t%u\t%s%u\t%s%d\n",
Hugh Dickins886bb7e2009-01-06 14:39:48 -08002827 len < 40 ? 40 - len : 1, " ",
Al Viro496ad9a2013-01-23 17:07:38 -05002828 S_ISBLK(file_inode(file)->i_mode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 "partition" : "file\t",
Randy Dunlap6f793942020-06-01 21:49:26 -07002830 bytes, bytes < 10000000 ? "\t" : "",
2831 inuse, inuse < 10000000 ? "\t" : "",
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002832 si->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return 0;
2834}
2835
Helge Deller15ad7cd2006-12-06 20:40:36 -08002836static const struct seq_operations swaps_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 .start = swap_start,
2838 .next = swap_next,
2839 .stop = swap_stop,
2840 .show = swap_show
2841};
2842
2843static int swaps_open(struct inode *inode, struct file *file)
2844{
Kay Sieversf1514632011-07-12 20:48:39 +02002845 struct seq_file *seq;
Kay Sievers66d7dd52010-10-26 14:22:06 -07002846 int ret;
2847
Kay Sievers66d7dd52010-10-26 14:22:06 -07002848 ret = seq_open(file, &swaps_op);
Kay Sieversf1514632011-07-12 20:48:39 +02002849 if (ret)
Kay Sievers66d7dd52010-10-26 14:22:06 -07002850 return ret;
Kay Sievers66d7dd52010-10-26 14:22:06 -07002851
Kay Sieversf1514632011-07-12 20:48:39 +02002852 seq = file->private_data;
2853 seq->poll_event = atomic_read(&proc_poll_event);
2854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855}
2856
Alexey Dobriyan97a32532020-02-03 17:37:17 -08002857static const struct proc_ops swaps_proc_ops = {
Alexey Dobriyand919b332020-04-06 20:09:01 -07002858 .proc_flags = PROC_ENTRY_PERMANENT,
Alexey Dobriyan97a32532020-02-03 17:37:17 -08002859 .proc_open = swaps_open,
2860 .proc_read = seq_read,
2861 .proc_lseek = seq_lseek,
2862 .proc_release = seq_release,
2863 .proc_poll = swaps_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864};
2865
2866static int __init procswaps_init(void)
2867{
Alexey Dobriyan97a32532020-02-03 17:37:17 -08002868 proc_create("swaps", 0, NULL, &swaps_proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return 0;
2870}
2871__initcall(procswaps_init);
2872#endif /* CONFIG_PROC_FS */
2873
Jan Beulich17963162008-12-16 11:35:24 +00002874#ifdef MAX_SWAPFILES_CHECK
2875static int __init max_swapfiles_check(void)
2876{
2877 MAX_SWAPFILES_CHECK();
2878 return 0;
2879}
2880late_initcall(max_swapfiles_check);
2881#endif
2882
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002883static struct swap_info_struct *alloc_swap_info(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Hugh Dickins73c34b62009-12-14 17:58:43 -08002885 struct swap_info_struct *p;
Qian Caib11a76b2020-12-05 22:14:55 -08002886 struct swap_info_struct *defer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 unsigned int type;
Aaron Lua2468cc2017-09-06 16:24:57 -07002888 int i;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002889
Gustavo A. R. Silva96008742019-03-05 15:49:31 -08002890 p = kvzalloc(struct_size(p, avail_lists, nr_node_ids), GFP_KERNEL);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002891 if (!p)
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002892 return ERR_PTR(-ENOMEM);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002893
Hugh Dickins5d337b92005-09-03 15:54:41 -07002894 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002895 for (type = 0; type < nr_swapfiles; type++) {
2896 if (!(swap_info[type]->flags & SWP_USED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 break;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002898 }
Christoph Lameter06972122006-06-23 02:03:35 -07002899 if (type >= MAX_SWAPFILES) {
Hugh Dickins5d337b92005-09-03 15:54:41 -07002900 spin_unlock(&swap_lock);
Vasily Averin873d7bc2018-11-16 15:08:11 -08002901 kvfree(p);
Cesar Eduardo Barros730c0582011-03-22 16:33:19 -07002902 return ERR_PTR(-EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 }
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002904 if (type >= nr_swapfiles) {
2905 p->type = type;
Daniel Jordanc10d38c2019-03-05 15:48:19 -08002906 WRITE_ONCE(swap_info[type], p);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002907 /*
2908 * Write swap_info[type] before nr_swapfiles, in case a
2909 * racing procfs swap_start() or swap_next() is reading them.
2910 * (We never shrink nr_swapfiles, we never free this entry.)
2911 */
2912 smp_wmb();
Daniel Jordanc10d38c2019-03-05 15:48:19 -08002913 WRITE_ONCE(nr_swapfiles, nr_swapfiles + 1);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002914 } else {
Qian Caib11a76b2020-12-05 22:14:55 -08002915 defer = p;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002916 p = swap_info[type];
2917 /*
2918 * Do not memset this entry: a racing procfs swap_next()
2919 * would be relying on p->type to remain valid.
2920 */
2921 }
Aaron Lu4efaceb2019-07-11 20:55:41 -07002922 p->swap_extent_root = RB_ROOT;
Dan Streetman18ab4d42014-06-04 16:09:59 -07002923 plist_node_init(&p->list, 0);
Aaron Lua2468cc2017-09-06 16:24:57 -07002924 for_each_node(i)
2925 plist_node_init(&p->avail_lists[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 p->flags = SWP_USED;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002927 spin_unlock(&swap_lock);
Qian Caib11a76b2020-12-05 22:14:55 -08002928 kvfree(defer);
Shaohua Liec8acf22013-02-22 16:34:38 -08002929 spin_lock_init(&p->lock);
Huang Ying2628bd62017-11-02 15:59:50 -07002930 spin_lock_init(&p->cont_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002931
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002932 return p;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002933}
2934
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07002935static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
2936{
2937 int error;
2938
2939 if (S_ISBLK(inode->i_mode)) {
Christoph Hellwigef16e1d2020-09-21 09:19:54 +02002940 p->bdev = blkdev_get_by_dev(inode->i_rdev,
Hugh Dickins6f179af2015-08-17 17:34:27 -07002941 FMODE_READ | FMODE_WRITE | FMODE_EXCL, p);
Christoph Hellwigef16e1d2020-09-21 09:19:54 +02002942 if (IS_ERR(p->bdev)) {
2943 error = PTR_ERR(p->bdev);
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07002944 p->bdev = NULL;
Hugh Dickins6f179af2015-08-17 17:34:27 -07002945 return error;
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07002946 }
2947 p->old_block_size = block_size(p->bdev);
2948 error = set_blocksize(p->bdev, PAGE_SIZE);
2949 if (error < 0)
Cesar Eduardo Barros87ade722011-03-22 16:33:27 -07002950 return error;
Naohiro Aota12d29662019-11-30 17:49:56 -08002951 /*
2952 * Zoned block devices contain zones that have a sequential
2953 * write only restriction. Hence zoned block devices are not
2954 * suitable for swapping. Disallow them here.
2955 */
Christoph Hellwige556f6b2020-06-26 10:01:56 +02002956 if (blk_queue_is_zoned(p->bdev->bd_disk->queue))
Naohiro Aota12d29662019-11-30 17:49:56 -08002957 return -EINVAL;
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07002958 p->flags |= SWP_BLKDEV;
2959 } else if (S_ISREG(inode->i_mode)) {
2960 p->bdev = inode->i_sb->s_bdev;
Darrick J. Wong16380452019-08-20 07:55:16 -07002961 }
2962
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07002963 return 0;
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07002964}
2965
Andi Kleen377eeaa2018-06-13 15:48:28 -07002966
2967/*
2968 * Find out how many pages are allowed for a single swap device. There
2969 * are two limiting factors:
2970 * 1) the number of bits for the swap offset in the swp_entry_t type, and
2971 * 2) the number of bits in the swap pte, as defined by the different
2972 * architectures.
2973 *
2974 * In order to find the largest possible bit mask, a swap entry with
2975 * swap type 0 and swap offset ~0UL is created, encoded to a swap pte,
2976 * decoded to a swp_entry_t again, and finally the swap offset is
2977 * extracted.
2978 *
2979 * This will mask all the bits from the initial ~0UL mask that can't
2980 * be encoded in either the swp_entry_t or the architecture definition
2981 * of a swap pte.
2982 */
2983unsigned long generic_max_swapfile_size(void)
2984{
2985 return swp_offset(pte_to_swp_entry(
2986 swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
2987}
2988
2989/* Can be overridden by an architecture for additional checks. */
2990__weak unsigned long max_swapfile_size(void)
2991{
2992 return generic_max_swapfile_size();
2993}
2994
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002995static unsigned long read_swap_header(struct swap_info_struct *p,
2996 union swap_header *swap_header,
2997 struct inode *inode)
2998{
2999 int i;
3000 unsigned long maxpages;
3001 unsigned long swapfilepages;
Raymond Jenningsd6bbbd22013-09-11 14:20:16 -07003002 unsigned long last_page;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07003003
3004 if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
Andrew Morton465c47f2013-09-11 14:20:17 -07003005 pr_err("Unable to find swap-space signature\n");
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07003006 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07003007 }
3008
3009 /* swap partition endianess hack... */
3010 if (swab32(swap_header->info.version) == 1) {
3011 swab32s(&swap_header->info.version);
3012 swab32s(&swap_header->info.last_page);
3013 swab32s(&swap_header->info.nr_badpages);
Jann Horndd111be2016-11-10 10:46:19 -08003014 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
3015 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07003016 for (i = 0; i < swap_header->info.nr_badpages; i++)
3017 swab32s(&swap_header->info.badpages[i]);
3018 }
3019 /* Check the swap header's sub-version */
3020 if (swap_header->info.version != 1) {
Andrew Morton465c47f2013-09-11 14:20:17 -07003021 pr_warn("Unable to handle swap header version %d\n",
3022 swap_header->info.version);
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07003023 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07003024 }
3025
3026 p->lowest_bit = 1;
3027 p->cluster_next = 1;
3028 p->cluster_nr = 0;
3029
Andi Kleen377eeaa2018-06-13 15:48:28 -07003030 maxpages = max_swapfile_size();
Raymond Jenningsd6bbbd22013-09-11 14:20:16 -07003031 last_page = swap_header->info.last_page;
Tom Abrahama06ad632018-04-10 16:29:48 -07003032 if (!last_page) {
3033 pr_warn("Empty swap-file\n");
3034 return 0;
3035 }
Raymond Jenningsd6bbbd22013-09-11 14:20:16 -07003036 if (last_page > maxpages) {
Andrew Morton465c47f2013-09-11 14:20:17 -07003037 pr_warn("Truncating oversized swap area, only using %luk out of %luk\n",
Raymond Jenningsd6bbbd22013-09-11 14:20:16 -07003038 maxpages << (PAGE_SHIFT - 10),
3039 last_page << (PAGE_SHIFT - 10));
3040 }
3041 if (maxpages > last_page) {
3042 maxpages = last_page + 1;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07003043 /* p->max is an unsigned int: don't overflow it */
3044 if ((unsigned int)maxpages == 0)
3045 maxpages = UINT_MAX;
3046 }
3047 p->highest_bit = maxpages - 1;
3048
3049 if (!maxpages)
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07003050 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07003051 swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
3052 if (swapfilepages && maxpages > swapfilepages) {
Andrew Morton465c47f2013-09-11 14:20:17 -07003053 pr_warn("Swap area shorter than signature indicates\n");
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07003054 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07003055 }
3056 if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07003057 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07003058 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07003059 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07003060
3061 return maxpages;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07003062}
3063
Huang, Ying4b3ef9d2017-02-22 15:45:26 -08003064#define SWAP_CLUSTER_INFO_COLS \
Huang, Ying235b6212017-02-22 15:45:22 -08003065 DIV_ROUND_UP(L1_CACHE_BYTES, sizeof(struct swap_cluster_info))
Huang, Ying4b3ef9d2017-02-22 15:45:26 -08003066#define SWAP_CLUSTER_SPACE_COLS \
3067 DIV_ROUND_UP(SWAP_ADDRESS_SPACE_PAGES, SWAPFILE_CLUSTER)
3068#define SWAP_CLUSTER_COLS \
3069 max_t(unsigned int, SWAP_CLUSTER_INFO_COLS, SWAP_CLUSTER_SPACE_COLS)
Huang, Ying235b6212017-02-22 15:45:22 -08003070
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003071static int setup_swap_map_and_extents(struct swap_info_struct *p,
3072 union swap_header *swap_header,
3073 unsigned char *swap_map,
Shaohua Li2a8f9442013-09-11 14:20:28 -07003074 struct swap_cluster_info *cluster_info,
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003075 unsigned long maxpages,
3076 sector_t *span)
3077{
Huang, Ying235b6212017-02-22 15:45:22 -08003078 unsigned int j, k;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003079 unsigned int nr_good_pages;
3080 int nr_extents;
Shaohua Li2a8f9442013-09-11 14:20:28 -07003081 unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
Huang, Ying235b6212017-02-22 15:45:22 -08003082 unsigned long col = p->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_COLS;
3083 unsigned long i, idx;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003084
3085 nr_good_pages = maxpages - 1; /* omit header page */
3086
Huang Ying6b534912016-10-07 16:58:42 -07003087 cluster_list_init(&p->free_clusters);
3088 cluster_list_init(&p->discard_clusters);
Shaohua Li2a8f9442013-09-11 14:20:28 -07003089
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003090 for (i = 0; i < swap_header->info.nr_badpages; i++) {
3091 unsigned int page_nr = swap_header->info.badpages[i];
Cesar Eduardo Barrosbdb8e3f2011-03-22 16:33:33 -07003092 if (page_nr == 0 || page_nr > swap_header->info.last_page)
3093 return -EINVAL;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003094 if (page_nr < maxpages) {
3095 swap_map[page_nr] = SWAP_MAP_BAD;
3096 nr_good_pages--;
Shaohua Li2a8f9442013-09-11 14:20:28 -07003097 /*
3098 * Haven't marked the cluster free yet, no list
3099 * operation involved
3100 */
3101 inc_cluster_info_page(p, cluster_info, page_nr);
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003102 }
3103 }
3104
Shaohua Li2a8f9442013-09-11 14:20:28 -07003105 /* Haven't marked the cluster free yet, no list operation involved */
3106 for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++)
3107 inc_cluster_info_page(p, cluster_info, i);
3108
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003109 if (nr_good_pages) {
3110 swap_map[0] = SWAP_MAP_BAD;
Shaohua Li2a8f9442013-09-11 14:20:28 -07003111 /*
3112 * Not mark the cluster free yet, no list
3113 * operation involved
3114 */
3115 inc_cluster_info_page(p, cluster_info, 0);
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003116 p->max = maxpages;
3117 p->pages = nr_good_pages;
3118 nr_extents = setup_swap_extents(p, span);
Cesar Eduardo Barrosbdb8e3f2011-03-22 16:33:33 -07003119 if (nr_extents < 0)
3120 return nr_extents;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003121 nr_good_pages = p->pages;
3122 }
3123 if (!nr_good_pages) {
Andrew Morton465c47f2013-09-11 14:20:17 -07003124 pr_warn("Empty swap-file\n");
Cesar Eduardo Barrosbdb8e3f2011-03-22 16:33:33 -07003125 return -EINVAL;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003126 }
3127
Shaohua Li2a8f9442013-09-11 14:20:28 -07003128 if (!cluster_info)
3129 return nr_extents;
3130
Huang, Ying235b6212017-02-22 15:45:22 -08003131
Huang, Ying4b3ef9d2017-02-22 15:45:26 -08003132 /*
3133 * Reduce false cache line sharing between cluster_info and
3134 * sharing same address space.
3135 */
Huang, Ying235b6212017-02-22 15:45:22 -08003136 for (k = 0; k < SWAP_CLUSTER_COLS; k++) {
3137 j = (k + col) % SWAP_CLUSTER_COLS;
3138 for (i = 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) {
3139 idx = i * SWAP_CLUSTER_COLS + j;
3140 if (idx >= nr_clusters)
3141 continue;
3142 if (cluster_count(&cluster_info[idx]))
3143 continue;
Shaohua Li2a8f9442013-09-11 14:20:28 -07003144 cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE);
Huang Ying6b534912016-10-07 16:58:42 -07003145 cluster_list_add_tail(&p->free_clusters, cluster_info,
3146 idx);
Shaohua Li2a8f9442013-09-11 14:20:28 -07003147 }
Shaohua Li2a8f9442013-09-11 14:20:28 -07003148 }
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003149 return nr_extents;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003150}
3151
Rafael Aquinidcf6b7d2013-07-03 15:02:46 -07003152/*
3153 * Helper to sys_swapon determining if a given swap
3154 * backing device queue supports DISCARD operations.
3155 */
3156static bool swap_discardable(struct swap_info_struct *si)
3157{
3158 struct request_queue *q = bdev_get_queue(si->bdev);
3159
3160 if (!q || !blk_queue_discard(q))
3161 return false;
3162
3163 return true;
3164}
3165
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003166SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
3167{
3168 struct swap_info_struct *p;
Jeff Layton91a27b22012-10-10 15:25:28 -04003169 struct filename *name;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003170 struct file *swap_file = NULL;
3171 struct address_space *mapping;
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07003172 int prio;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003173 int error;
3174 union swap_header *swap_header;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003175 int nr_extents;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003176 sector_t span;
3177 unsigned long maxpages;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003178 unsigned char *swap_map = NULL;
Shaohua Li2a8f9442013-09-11 14:20:28 -07003179 struct swap_cluster_info *cluster_info = NULL;
Dan Magenheimer38b5faf2012-04-09 17:08:06 -06003180 unsigned long *frontswap_map = NULL;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003181 struct page *page = NULL;
3182 struct inode *inode = NULL;
Omar Sandoval7cbf3192018-05-25 14:47:17 -07003183 bool inced_nr_rotate_swap = false;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003184
Hugh Dickinsd15cab92012-03-28 14:42:42 -07003185 if (swap_flags & ~SWAP_FLAGS_VALID)
3186 return -EINVAL;
3187
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003188 if (!capable(CAP_SYS_ADMIN))
3189 return -EPERM;
3190
Aaron Lua2468cc2017-09-06 16:24:57 -07003191 if (!swap_avail_heads)
3192 return -ENOMEM;
3193
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003194 p = alloc_swap_info();
Cesar Eduardo Barros2542e5132011-03-22 16:33:18 -07003195 if (IS_ERR(p))
3196 return PTR_ERR(p);
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003197
Shaohua Li815c2c52013-09-11 14:20:30 -07003198 INIT_WORK(&p->discard_work, swap_discard_work);
3199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 name = getname(specialfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 if (IS_ERR(name)) {
Cesar Eduardo Barros7de7fb62011-03-22 16:33:22 -07003202 error = PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 name = NULL;
Cesar Eduardo Barrosbd690102011-03-22 16:33:25 -07003204 goto bad_swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 }
Jeff Layton669abf42012-10-10 16:43:10 -04003206 swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 if (IS_ERR(swap_file)) {
Cesar Eduardo Barros7de7fb62011-03-22 16:33:22 -07003208 error = PTR_ERR(swap_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 swap_file = NULL;
Cesar Eduardo Barrosbd690102011-03-22 16:33:25 -07003210 goto bad_swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 }
3212
3213 p->swap_file = swap_file;
3214 mapping = swap_file->f_mapping;
Cesar Eduardo Barros21307812011-03-22 23:03:13 -03003215 inode = mapping->host;
Hugh Dickins6f179af2015-08-17 17:34:27 -07003216
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07003217 error = claim_swapfile(p, inode);
3218 if (unlikely(error))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 goto bad_swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Naohiro Aotad795a902020-03-28 19:17:15 -07003221 inode_lock(inode);
3222 if (IS_SWAPFILE(inode)) {
3223 error = -EBUSY;
3224 goto bad_swap_unlock_inode;
3225 }
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 /*
3228 * Read the swap header.
3229 */
3230 if (!mapping->a_ops->readpage) {
3231 error = -EINVAL;
Naohiro Aotad795a902020-03-28 19:17:15 -07003232 goto bad_swap_unlock_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
Pekka Enberg090d2b12006-06-23 02:05:08 -07003234 page = read_mapping_page(mapping, 0, swap_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 if (IS_ERR(page)) {
3236 error = PTR_ERR(page);
Naohiro Aotad795a902020-03-28 19:17:15 -07003237 goto bad_swap_unlock_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 }
Hugh Dickins81e33972009-01-06 14:39:49 -08003239 swap_header = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07003241 maxpages = read_swap_header(p, swap_header, inode);
3242 if (unlikely(!maxpages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 error = -EINVAL;
Naohiro Aotad795a902020-03-28 19:17:15 -07003244 goto bad_swap_unlock_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
Hugh Dickins886bb7e2009-01-06 14:39:48 -08003246
Hugh Dickins81e33972009-01-06 14:39:49 -08003247 /* OK, set up the swap map and apply the bad block list */
Cesar Eduardo Barros803d0c832011-03-22 16:33:14 -07003248 swap_map = vzalloc(maxpages);
Hugh Dickins81e33972009-01-06 14:39:49 -08003249 if (!swap_map) {
3250 error = -ENOMEM;
Naohiro Aotad795a902020-03-28 19:17:15 -07003251 goto bad_swap_unlock_inode;
Hugh Dickins81e33972009-01-06 14:39:49 -08003252 }
Minchan Kimf0571422017-01-10 16:58:15 -08003253
Christoph Hellwig1cb039f2020-09-24 08:51:38 +02003254 if (p->bdev && blk_queue_stable_writes(p->bdev->bd_disk->queue))
Minchan Kimf0571422017-01-10 16:58:15 -08003255 p->flags |= SWP_STABLE_WRITES;
3256
Christoph Hellwiga8b456d2020-09-24 08:51:36 +02003257 if (p->bdev && p->bdev->bd_disk->fops->rw_page)
Minchan Kim539a6fea2017-11-15 17:33:04 -08003258 p->flags |= SWP_SYNCHRONOUS_IO;
3259
Shaohua Li2a8f9442013-09-11 14:20:28 -07003260 if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) {
Hugh Dickins6f179af2015-08-17 17:34:27 -07003261 int cpu;
Huang, Ying235b6212017-02-22 15:45:22 -08003262 unsigned long ci, nr_cluster;
Hugh Dickins6f179af2015-08-17 17:34:27 -07003263
Shaohua Li2a8f9442013-09-11 14:20:28 -07003264 p->flags |= SWP_SOLIDSTATE;
Huang Ying49070582020-06-01 21:49:22 -07003265 p->cluster_next_cpu = alloc_percpu(unsigned int);
3266 if (!p->cluster_next_cpu) {
3267 error = -ENOMEM;
3268 goto bad_swap_unlock_inode;
3269 }
Shaohua Li2a8f9442013-09-11 14:20:28 -07003270 /*
3271 * select a random position to start with to help wear leveling
3272 * SSD
3273 */
Huang Ying49070582020-06-01 21:49:22 -07003274 for_each_possible_cpu(cpu) {
3275 per_cpu(*p->cluster_next_cpu, cpu) =
3276 1 + prandom_u32_max(p->highest_bit);
3277 }
Huang, Ying235b6212017-02-22 15:45:22 -08003278 nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
Shaohua Li2a8f9442013-09-11 14:20:28 -07003279
Kees Cook778e1cd2018-06-12 14:04:48 -07003280 cluster_info = kvcalloc(nr_cluster, sizeof(*cluster_info),
Huang Ying54f180d2017-05-08 15:57:40 -07003281 GFP_KERNEL);
Shaohua Li2a8f9442013-09-11 14:20:28 -07003282 if (!cluster_info) {
3283 error = -ENOMEM;
Naohiro Aotad795a902020-03-28 19:17:15 -07003284 goto bad_swap_unlock_inode;
Shaohua Li2a8f9442013-09-11 14:20:28 -07003285 }
Huang, Ying235b6212017-02-22 15:45:22 -08003286
3287 for (ci = 0; ci < nr_cluster; ci++)
3288 spin_lock_init(&((cluster_info + ci)->lock));
3289
Shaohua Liebc2a1a2013-09-11 14:20:32 -07003290 p->percpu_cluster = alloc_percpu(struct percpu_cluster);
3291 if (!p->percpu_cluster) {
3292 error = -ENOMEM;
Naohiro Aotad795a902020-03-28 19:17:15 -07003293 goto bad_swap_unlock_inode;
Shaohua Liebc2a1a2013-09-11 14:20:32 -07003294 }
Hugh Dickins6f179af2015-08-17 17:34:27 -07003295 for_each_possible_cpu(cpu) {
Shaohua Liebc2a1a2013-09-11 14:20:32 -07003296 struct percpu_cluster *cluster;
Hugh Dickins6f179af2015-08-17 17:34:27 -07003297 cluster = per_cpu_ptr(p->percpu_cluster, cpu);
Shaohua Liebc2a1a2013-09-11 14:20:32 -07003298 cluster_set_null(&cluster->index);
3299 }
Omar Sandoval7cbf3192018-05-25 14:47:17 -07003300 } else {
Huang Ying81a02982017-09-06 16:24:43 -07003301 atomic_inc(&nr_rotate_swap);
Omar Sandoval7cbf3192018-05-25 14:47:17 -07003302 inced_nr_rotate_swap = true;
3303 }
Hugh Dickins81e33972009-01-06 14:39:49 -08003304
Cesar Eduardo Barros1421ef32011-03-22 16:33:31 -07003305 error = swap_cgroup_swapon(p->type, maxpages);
3306 if (error)
Naohiro Aotad795a902020-03-28 19:17:15 -07003307 goto bad_swap_unlock_inode;
Cesar Eduardo Barros1421ef32011-03-22 16:33:31 -07003308
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003309 nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
Shaohua Li2a8f9442013-09-11 14:20:28 -07003310 cluster_info, maxpages, &span);
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003311 if (unlikely(nr_extents < 0)) {
3312 error = nr_extents;
Naohiro Aotad795a902020-03-28 19:17:15 -07003313 goto bad_swap_unlock_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
Dan Magenheimer38b5faf2012-04-09 17:08:06 -06003315 /* frontswap enabled? set up bit-per-page map for frontswap */
Vlastimil Babka8ea1d2a2016-07-26 15:24:42 -07003316 if (IS_ENABLED(CONFIG_FRONTSWAP))
Kees Cook778e1cd2018-06-12 14:04:48 -07003317 frontswap_map = kvcalloc(BITS_TO_LONGS(maxpages),
3318 sizeof(long),
Huang Ying54f180d2017-05-08 15:57:40 -07003319 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Shaohua Li2a8f9442013-09-11 14:20:28 -07003321 if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) {
3322 /*
3323 * When discard is enabled for swap with no particular
3324 * policy flagged, we set all swap discard flags here in
3325 * order to sustain backward compatibility with older
3326 * swapon(8) releases.
3327 */
3328 p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD |
3329 SWP_PAGE_DISCARD);
Rafael Aquinidcf6b7d2013-07-03 15:02:46 -07003330
Shaohua Li2a8f9442013-09-11 14:20:28 -07003331 /*
3332 * By flagging sys_swapon, a sysadmin can tell us to
3333 * either do single-time area discards only, or to just
3334 * perform discards for released swap page-clusters.
3335 * Now it's time to adjust the p->flags accordingly.
3336 */
3337 if (swap_flags & SWAP_FLAG_DISCARD_ONCE)
3338 p->flags &= ~SWP_PAGE_DISCARD;
3339 else if (swap_flags & SWAP_FLAG_DISCARD_PAGES)
3340 p->flags &= ~SWP_AREA_DISCARD;
Rafael Aquinidcf6b7d2013-07-03 15:02:46 -07003341
Shaohua Li2a8f9442013-09-11 14:20:28 -07003342 /* issue a swapon-time discard if it's still required */
3343 if (p->flags & SWP_AREA_DISCARD) {
3344 int err = discard_swap(p);
3345 if (unlikely(err))
3346 pr_err("swapon: discard_swap(%p): %d\n",
3347 p, err);
Rafael Aquinidcf6b7d2013-07-03 15:02:46 -07003348 }
Hugh Dickins20137a42009-01-06 14:39:54 -08003349 }
Hugh Dickins6a6ba832009-01-06 14:39:51 -08003350
Huang, Ying4b3ef9d2017-02-22 15:45:26 -08003351 error = init_swap_address_space(p->type, maxpages);
3352 if (error)
Naohiro Aotad795a902020-03-28 19:17:15 -07003353 goto bad_swap_unlock_inode;
Huang, Ying4b3ef9d2017-02-22 15:45:26 -08003354
Darrick J. Wongdc617f22019-08-20 07:55:16 -07003355 /*
3356 * Flush any pending IO and dirty mappings before we start using this
3357 * swap device.
3358 */
3359 inode->i_flags |= S_SWAPFILE;
3360 error = inode_drain_writes(inode);
3361 if (error) {
3362 inode->i_flags &= ~S_SWAPFILE;
Miaohe Lin822bca52020-10-13 16:52:30 -07003363 goto free_swap_address_space;
Darrick J. Wongdc617f22019-08-20 07:55:16 -07003364 }
3365
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003366 mutex_lock(&swapon_mutex);
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07003367 prio = -1;
Hugh Dickins78ecba02008-07-23 21:28:23 -07003368 if (swap_flags & SWAP_FLAG_PREFER)
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07003369 prio =
Hugh Dickins78ecba02008-07-23 21:28:23 -07003370 (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
Shaohua Li2a8f9442013-09-11 14:20:28 -07003371 enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map);
Cesar Eduardo Barrosc69dbfb2011-03-22 16:33:35 -07003372
Joe Perches756a0252016-03-17 14:19:47 -07003373 pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s%s%s%s\n",
Jeff Layton91a27b22012-10-10 15:25:28 -04003374 p->pages<<(PAGE_SHIFT-10), name->name, p->prio,
Cesar Eduardo Barrosc69dbfb2011-03-22 16:33:35 -07003375 nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
3376 (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
Dan Magenheimer38b5faf2012-04-09 17:08:06 -06003377 (p->flags & SWP_DISCARDABLE) ? "D" : "",
Rafael Aquinidcf6b7d2013-07-03 15:02:46 -07003378 (p->flags & SWP_AREA_DISCARD) ? "s" : "",
3379 (p->flags & SWP_PAGE_DISCARD) ? "c" : "",
Dan Magenheimer38b5faf2012-04-09 17:08:06 -06003380 (frontswap_map) ? "FS" : "");
Cesar Eduardo Barrosc69dbfb2011-03-22 16:33:35 -07003381
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003382 mutex_unlock(&swapon_mutex);
Kay Sievers66d7dd52010-10-26 14:22:06 -07003383 atomic_inc(&proc_poll_event);
3384 wake_up_interruptible(&proc_poll_wait);
3385
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 error = 0;
3387 goto out;
Miaohe Lin822bca52020-10-13 16:52:30 -07003388free_swap_address_space:
3389 exit_swap_address_space(p->type);
Naohiro Aotad795a902020-03-28 19:17:15 -07003390bad_swap_unlock_inode:
3391 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392bad_swap:
Shaohua Liebc2a1a2013-09-11 14:20:32 -07003393 free_percpu(p->percpu_cluster);
3394 p->percpu_cluster = NULL;
Huang Ying49070582020-06-01 21:49:22 -07003395 free_percpu(p->cluster_next_cpu);
3396 p->cluster_next_cpu = NULL;
Cesar Eduardo Barrosbd690102011-03-22 16:33:25 -07003397 if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
Cesar Eduardo Barrosf2090d22011-03-22 16:33:23 -07003398 set_blocksize(p->bdev, p->old_block_size);
3399 blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 }
Naohiro Aotad795a902020-03-28 19:17:15 -07003401 inode = NULL;
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07003402 destroy_swap_extents(p);
Cesar Eduardo Barrose8e6c2e2011-03-22 16:33:16 -07003403 swap_cgroup_swapoff(p->type);
Hugh Dickins5d337b92005-09-03 15:54:41 -07003404 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 p->swap_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 p->flags = 0;
Hugh Dickins5d337b92005-09-03 15:54:41 -07003407 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 vfree(swap_map);
Darrick J. Wong8606a1a2017-09-08 16:13:25 -07003409 kvfree(cluster_info);
David Rientjesb6b1fd22017-09-08 16:13:29 -07003410 kvfree(frontswap_map);
Omar Sandoval7cbf3192018-05-25 14:47:17 -07003411 if (inced_nr_rotate_swap)
3412 atomic_dec(&nr_rotate_swap);
Naohiro Aotad795a902020-03-28 19:17:15 -07003413 if (swap_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 filp_close(swap_file, NULL);
3415out:
3416 if (page && !IS_ERR(page)) {
3417 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003418 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 }
3420 if (name)
3421 putname(name);
Darrick J. Wong16380452019-08-20 07:55:16 -07003422 if (inode)
Al Viro59551022016-01-22 15:40:57 -05003423 inode_unlock(inode);
Tim Chen039939a2017-02-22 15:45:43 -08003424 if (!error)
3425 enable_swap_slots_cache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 return error;
3427}
3428
3429void si_swapinfo(struct sysinfo *val)
3430{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08003431 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 unsigned long nr_to_be_unused = 0;
3433
Hugh Dickins5d337b92005-09-03 15:54:41 -07003434 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08003435 for (type = 0; type < nr_swapfiles; type++) {
3436 struct swap_info_struct *si = swap_info[type];
3437
3438 if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
3439 nr_to_be_unused += si->inuse_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 }
Shaohua Liec8acf22013-02-22 16:34:38 -08003441 val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 val->totalswap = total_swap_pages + nr_to_be_unused;
Hugh Dickins5d337b92005-09-03 15:54:41 -07003443 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444}
3445
3446/*
3447 * Verify that a swap entry is valid and increment its swap map count.
3448 *
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003449 * Returns error code in following case.
3450 * - success -> 0
3451 * - swp_entry is invalid -> EINVAL
3452 * - swp_entry is migration entry -> EINVAL
3453 * - swap-cache reference is requested but there is already one. -> EEXIST
3454 * - swap-cache reference is requested but the entry is not used. -> ENOENT
Hugh Dickins570a335b2009-12-14 17:58:46 -08003455 * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 */
Hugh Dickins8d69aae2009-12-14 17:58:45 -08003457static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
Hugh Dickins73c34b62009-12-14 17:58:43 -08003459 struct swap_info_struct *p;
Huang, Ying235b6212017-02-22 15:45:22 -08003460 struct swap_cluster_info *ci;
Daniel Jordanc10d38c2019-03-05 15:48:19 -08003461 unsigned long offset;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08003462 unsigned char count;
3463 unsigned char has_cache;
Hugh Dickins253d5532009-12-14 17:58:44 -08003464 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Huang Yingeb085572019-07-11 20:55:33 -07003466 p = get_swap_device(entry);
Daniel Jordanc10d38c2019-03-05 15:48:19 -08003467 if (!p)
Huang Yingeb085572019-07-11 20:55:33 -07003468 goto out;
Daniel Jordanc10d38c2019-03-05 15:48:19 -08003469
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 offset = swp_offset(entry);
Huang, Ying235b6212017-02-22 15:45:22 -08003471 ci = lock_cluster_or_swap_info(p, offset);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003472
Hugh Dickins253d5532009-12-14 17:58:44 -08003473 count = p->swap_map[offset];
Shaohua Liedfe23d2013-09-11 14:20:31 -07003474
3475 /*
3476 * swapin_readahead() doesn't check if a swap entry is valid, so the
3477 * swap entry could be SWAP_MAP_BAD. Check here with lock held.
3478 */
3479 if (unlikely(swap_count(count) == SWAP_MAP_BAD)) {
3480 err = -ENOENT;
3481 goto unlock_out;
3482 }
3483
Hugh Dickins253d5532009-12-14 17:58:44 -08003484 has_cache = count & SWAP_HAS_CACHE;
3485 count &= ~SWAP_HAS_CACHE;
3486 err = 0;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003487
Hugh Dickins253d5532009-12-14 17:58:44 -08003488 if (usage == SWAP_HAS_CACHE) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003489
3490 /* set SWAP_HAS_CACHE if there is no cache and entry is used */
Hugh Dickins253d5532009-12-14 17:58:44 -08003491 if (!has_cache && count)
3492 has_cache = SWAP_HAS_CACHE;
3493 else if (has_cache) /* someone else added cache */
3494 err = -EEXIST;
3495 else /* no users remaining */
3496 err = -ENOENT;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003497
3498 } else if (count || has_cache) {
Hugh Dickins253d5532009-12-14 17:58:44 -08003499
Hugh Dickins570a335b2009-12-14 17:58:46 -08003500 if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
3501 count += usage;
3502 else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
Hugh Dickins253d5532009-12-14 17:58:44 -08003503 err = -EINVAL;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003504 else if (swap_count_continued(p, offset, count))
3505 count = COUNT_CONTINUED;
3506 else
3507 err = -ENOMEM;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003508 } else
Hugh Dickins253d5532009-12-14 17:58:44 -08003509 err = -ENOENT; /* unused swap entry */
3510
Qian Caia449bf52020-08-14 17:31:31 -07003511 WRITE_ONCE(p->swap_map[offset], count | has_cache);
Hugh Dickins253d5532009-12-14 17:58:44 -08003512
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003513unlock_out:
Huang, Ying235b6212017-02-22 15:45:22 -08003514 unlock_cluster_or_swap_info(p, ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515out:
Huang Yingeb085572019-07-11 20:55:33 -07003516 if (p)
3517 put_swap_device(p);
Hugh Dickins253d5532009-12-14 17:58:44 -08003518 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
Hugh Dickins253d5532009-12-14 17:58:44 -08003520
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003521/*
Hugh Dickinsaaa46862009-12-14 17:58:47 -08003522 * Help swapoff by noting that swap entry belongs to shmem/tmpfs
3523 * (in which case its reference count is never incremented).
3524 */
3525void swap_shmem_alloc(swp_entry_t entry)
3526{
3527 __swap_duplicate(entry, SWAP_MAP_SHMEM);
3528}
3529
3530/*
Hugh Dickins08259d52010-03-05 13:42:25 -08003531 * Increase reference count of swap entry by 1.
3532 * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
3533 * but could not be atomically allocated. Returns 0, just as if it succeeded,
3534 * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
3535 * might occur if a page table entry has got corrupted.
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003536 */
Hugh Dickins570a335b2009-12-14 17:58:46 -08003537int swap_duplicate(swp_entry_t entry)
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003538{
Hugh Dickins570a335b2009-12-14 17:58:46 -08003539 int err = 0;
3540
3541 while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
3542 err = add_swap_count_continuation(entry, GFP_ATOMIC);
3543 return err;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003544}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07003546/*
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003547 * @entry: swap entry for which we allocate swap cache.
3548 *
Hugh Dickins73c34b62009-12-14 17:58:43 -08003549 * Called when allocating swap cache for existing swap entry,
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003550 * This can return error codes. Returns 0 at success.
Chen Wandun3eeba132020-04-01 21:06:07 -07003551 * -EEXIST means there is a swap cache.
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003552 * Note: return code is different from swap_duplicate().
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07003553 */
3554int swapcache_prepare(swp_entry_t entry)
3555{
Hugh Dickins253d5532009-12-14 17:58:44 -08003556 return __swap_duplicate(entry, SWAP_HAS_CACHE);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07003557}
3558
Minchan Kim0bcac062017-11-15 17:33:07 -08003559struct swap_info_struct *swp_swap_info(swp_entry_t entry)
3560{
Daniel Jordanc10d38c2019-03-05 15:48:19 -08003561 return swap_type_to_swap_info(swp_type(entry));
Minchan Kim0bcac062017-11-15 17:33:07 -08003562}
3563
Mel Gormanf981c592012-07-31 16:44:47 -07003564struct swap_info_struct *page_swap_info(struct page *page)
3565{
Minchan Kim0bcac062017-11-15 17:33:07 -08003566 swp_entry_t entry = { .val = page_private(page) };
3567 return swp_swap_info(entry);
Mel Gormanf981c592012-07-31 16:44:47 -07003568}
3569
3570/*
3571 * out-of-line __page_file_ methods to avoid include hell.
3572 */
3573struct address_space *__page_file_mapping(struct page *page)
3574{
Mel Gormanf981c592012-07-31 16:44:47 -07003575 return page_swap_info(page)->swap_file->f_mapping;
3576}
3577EXPORT_SYMBOL_GPL(__page_file_mapping);
3578
3579pgoff_t __page_file_index(struct page *page)
3580{
3581 swp_entry_t swap = { .val = page_private(page) };
Mel Gormanf981c592012-07-31 16:44:47 -07003582 return swp_offset(swap);
3583}
3584EXPORT_SYMBOL_GPL(__page_file_index);
3585
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586/*
Hugh Dickins570a335b2009-12-14 17:58:46 -08003587 * add_swap_count_continuation - called when a swap count is duplicated
3588 * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
3589 * page of the original vmalloc'ed swap_map, to hold the continuation count
3590 * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called
3591 * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
3592 *
3593 * These continuation pages are seldom referenced: the common paths all work
3594 * on the original swap_map, only referring to a continuation page when the
3595 * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
3596 *
3597 * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
3598 * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
3599 * can be called after dropping locks.
3600 */
3601int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
3602{
3603 struct swap_info_struct *si;
Huang, Ying235b6212017-02-22 15:45:22 -08003604 struct swap_cluster_info *ci;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003605 struct page *head;
3606 struct page *page;
3607 struct page *list_page;
3608 pgoff_t offset;
3609 unsigned char count;
Huang Yingeb085572019-07-11 20:55:33 -07003610 int ret = 0;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003611
3612 /*
3613 * When debugging, it's easier to use __GFP_ZERO here; but it's better
3614 * for latency not to zero a page while GFP_ATOMIC and holding locks.
3615 */
3616 page = alloc_page(gfp_mask | __GFP_HIGHMEM);
3617
Huang Yingeb085572019-07-11 20:55:33 -07003618 si = get_swap_device(entry);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003619 if (!si) {
3620 /*
3621 * An acceptable race has occurred since the failing
Huang Yingeb085572019-07-11 20:55:33 -07003622 * __swap_duplicate(): the swap device may be swapoff
Hugh Dickins570a335b2009-12-14 17:58:46 -08003623 */
3624 goto outer;
3625 }
Huang Yingeb085572019-07-11 20:55:33 -07003626 spin_lock(&si->lock);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003627
3628 offset = swp_offset(entry);
Huang, Ying235b6212017-02-22 15:45:22 -08003629
3630 ci = lock_cluster(si, offset);
3631
Hugh Dickins570a335b2009-12-14 17:58:46 -08003632 count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
3633
3634 if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
3635 /*
3636 * The higher the swap count, the more likely it is that tasks
3637 * will race to add swap count continuation: we need to avoid
3638 * over-provisioning.
3639 */
3640 goto out;
3641 }
3642
3643 if (!page) {
Huang Yingeb085572019-07-11 20:55:33 -07003644 ret = -ENOMEM;
3645 goto out;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003646 }
3647
3648 /*
3649 * We are fortunate that although vmalloc_to_page uses pte_offset_map,
Seth Jennings2de1a7e2013-11-12 15:07:46 -08003650 * no architecture is using highmem pages for kernel page tables: so it
3651 * will not corrupt the GFP_ATOMIC caller's atomic page table kmaps.
Hugh Dickins570a335b2009-12-14 17:58:46 -08003652 */
3653 head = vmalloc_to_page(si->swap_map + offset);
3654 offset &= ~PAGE_MASK;
3655
Huang Ying2628bd62017-11-02 15:59:50 -07003656 spin_lock(&si->cont_lock);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003657 /*
3658 * Page allocation does not initialize the page's lru field,
3659 * but it does always reset its private field.
3660 */
3661 if (!page_private(head)) {
3662 BUG_ON(count & COUNT_CONTINUED);
3663 INIT_LIST_HEAD(&head->lru);
3664 set_page_private(head, SWP_CONTINUED);
3665 si->flags |= SWP_CONTINUED;
3666 }
3667
3668 list_for_each_entry(list_page, &head->lru, lru) {
3669 unsigned char *map;
3670
3671 /*
3672 * If the previous map said no continuation, but we've found
3673 * a continuation page, free our allocation and use this one.
3674 */
3675 if (!(count & COUNT_CONTINUED))
Huang Ying2628bd62017-11-02 15:59:50 -07003676 goto out_unlock_cont;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003677
Cong Wang9b04c5f2011-11-25 23:14:39 +08003678 map = kmap_atomic(list_page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003679 count = *map;
Cong Wang9b04c5f2011-11-25 23:14:39 +08003680 kunmap_atomic(map);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003681
3682 /*
3683 * If this continuation count now has some space in it,
3684 * free our allocation and use this one.
3685 */
3686 if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
Huang Ying2628bd62017-11-02 15:59:50 -07003687 goto out_unlock_cont;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003688 }
3689
3690 list_add_tail(&page->lru, &head->lru);
3691 page = NULL; /* now it's attached, don't free it */
Huang Ying2628bd62017-11-02 15:59:50 -07003692out_unlock_cont:
3693 spin_unlock(&si->cont_lock);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003694out:
Huang, Ying235b6212017-02-22 15:45:22 -08003695 unlock_cluster(ci);
Shaohua Liec8acf22013-02-22 16:34:38 -08003696 spin_unlock(&si->lock);
Huang Yingeb085572019-07-11 20:55:33 -07003697 put_swap_device(si);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003698outer:
3699 if (page)
3700 __free_page(page);
Huang Yingeb085572019-07-11 20:55:33 -07003701 return ret;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003702}
3703
3704/*
3705 * swap_count_continued - when the original swap_map count is incremented
3706 * from SWAP_MAP_MAX, check if there is already a continuation page to carry
3707 * into, carry if so, or else fail until a new continuation page is allocated;
3708 * when the original swap_map count is decremented from 0 with continuation,
3709 * borrow from the continuation and report whether it still holds more.
Huang, Ying235b6212017-02-22 15:45:22 -08003710 * Called while __swap_duplicate() or swap_entry_free() holds swap or cluster
3711 * lock.
Hugh Dickins570a335b2009-12-14 17:58:46 -08003712 */
3713static bool swap_count_continued(struct swap_info_struct *si,
3714 pgoff_t offset, unsigned char count)
3715{
3716 struct page *head;
3717 struct page *page;
3718 unsigned char *map;
Huang Ying2628bd62017-11-02 15:59:50 -07003719 bool ret;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003720
3721 head = vmalloc_to_page(si->swap_map + offset);
3722 if (page_private(head) != SWP_CONTINUED) {
3723 BUG_ON(count & COUNT_CONTINUED);
3724 return false; /* need to add count continuation */
3725 }
3726
Huang Ying2628bd62017-11-02 15:59:50 -07003727 spin_lock(&si->cont_lock);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003728 offset &= ~PAGE_MASK;
chenqiwu213516a2020-06-01 21:48:36 -07003729 page = list_next_entry(head, lru);
Cong Wang9b04c5f2011-11-25 23:14:39 +08003730 map = kmap_atomic(page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003731
3732 if (count == SWAP_MAP_MAX) /* initial increment from swap_map */
3733 goto init_map; /* jump over SWAP_CONT_MAX checks */
3734
3735 if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
3736 /*
3737 * Think of how you add 1 to 999
3738 */
3739 while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08003740 kunmap_atomic(map);
chenqiwu213516a2020-06-01 21:48:36 -07003741 page = list_next_entry(page, lru);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003742 BUG_ON(page == head);
Cong Wang9b04c5f2011-11-25 23:14:39 +08003743 map = kmap_atomic(page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003744 }
3745 if (*map == SWAP_CONT_MAX) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08003746 kunmap_atomic(map);
chenqiwu213516a2020-06-01 21:48:36 -07003747 page = list_next_entry(page, lru);
Huang Ying2628bd62017-11-02 15:59:50 -07003748 if (page == head) {
3749 ret = false; /* add count continuation */
3750 goto out;
3751 }
Cong Wang9b04c5f2011-11-25 23:14:39 +08003752 map = kmap_atomic(page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003753init_map: *map = 0; /* we didn't zero the page */
3754 }
3755 *map += 1;
Cong Wang9b04c5f2011-11-25 23:14:39 +08003756 kunmap_atomic(map);
chenqiwu213516a2020-06-01 21:48:36 -07003757 while ((page = list_prev_entry(page, lru)) != head) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08003758 map = kmap_atomic(page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003759 *map = COUNT_CONTINUED;
Cong Wang9b04c5f2011-11-25 23:14:39 +08003760 kunmap_atomic(map);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003761 }
Huang Ying2628bd62017-11-02 15:59:50 -07003762 ret = true; /* incremented */
Hugh Dickins570a335b2009-12-14 17:58:46 -08003763
3764 } else { /* decrementing */
3765 /*
3766 * Think of how you subtract 1 from 1000
3767 */
3768 BUG_ON(count != COUNT_CONTINUED);
3769 while (*map == COUNT_CONTINUED) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08003770 kunmap_atomic(map);
chenqiwu213516a2020-06-01 21:48:36 -07003771 page = list_next_entry(page, lru);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003772 BUG_ON(page == head);
Cong Wang9b04c5f2011-11-25 23:14:39 +08003773 map = kmap_atomic(page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003774 }
3775 BUG_ON(*map == 0);
3776 *map -= 1;
3777 if (*map == 0)
3778 count = 0;
Cong Wang9b04c5f2011-11-25 23:14:39 +08003779 kunmap_atomic(map);
chenqiwu213516a2020-06-01 21:48:36 -07003780 while ((page = list_prev_entry(page, lru)) != head) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08003781 map = kmap_atomic(page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003782 *map = SWAP_CONT_MAX | count;
3783 count = COUNT_CONTINUED;
Cong Wang9b04c5f2011-11-25 23:14:39 +08003784 kunmap_atomic(map);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003785 }
Huang Ying2628bd62017-11-02 15:59:50 -07003786 ret = count == COUNT_CONTINUED;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003787 }
Huang Ying2628bd62017-11-02 15:59:50 -07003788out:
3789 spin_unlock(&si->cont_lock);
3790 return ret;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003791}
3792
3793/*
3794 * free_swap_count_continuations - swapoff free all the continuation pages
3795 * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
3796 */
3797static void free_swap_count_continuations(struct swap_info_struct *si)
3798{
3799 pgoff_t offset;
3800
3801 for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
3802 struct page *head;
3803 head = vmalloc_to_page(si->swap_map + offset);
3804 if (page_private(head)) {
Geliang Tang0d576d22016-01-14 15:21:49 -08003805 struct page *page, *next;
3806
3807 list_for_each_entry_safe(page, next, &head->lru, lru) {
3808 list_del(&page->lru);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003809 __free_page(page);
3810 }
3811 }
3812 }
3813}
Aaron Lua2468cc2017-09-06 16:24:57 -07003814
Tejun Heo2cf85582018-07-03 11:14:56 -04003815#if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
Johannes Weiner6caa6a02020-06-03 16:01:38 -07003816void cgroup_throttle_swaprate(struct page *page, gfp_t gfp_mask)
Tejun Heo2cf85582018-07-03 11:14:56 -04003817{
3818 struct swap_info_struct *si, *next;
Johannes Weiner6caa6a02020-06-03 16:01:38 -07003819 int nid = page_to_nid(page);
3820
3821 if (!(gfp_mask & __GFP_IO))
Tejun Heo2cf85582018-07-03 11:14:56 -04003822 return;
3823
3824 if (!blk_cgroup_congested())
3825 return;
3826
3827 /*
3828 * We've already scheduled a throttle, avoid taking the global swap
3829 * lock.
3830 */
3831 if (current->throttle_queue)
3832 return;
3833
3834 spin_lock(&swap_avail_lock);
Johannes Weiner6caa6a02020-06-03 16:01:38 -07003835 plist_for_each_entry_safe(si, next, &swap_avail_heads[nid],
3836 avail_lists[nid]) {
Tejun Heo2cf85582018-07-03 11:14:56 -04003837 if (si->bdev) {
Johannes Weiner6caa6a02020-06-03 16:01:38 -07003838 blkcg_schedule_throttle(bdev_get_queue(si->bdev), true);
Tejun Heo2cf85582018-07-03 11:14:56 -04003839 break;
3840 }
3841 }
3842 spin_unlock(&swap_avail_lock);
3843}
3844#endif
3845
Aaron Lua2468cc2017-09-06 16:24:57 -07003846static int __init swapfile_init(void)
3847{
3848 int nid;
3849
3850 swap_avail_heads = kmalloc_array(nr_node_ids, sizeof(struct plist_head),
3851 GFP_KERNEL);
3852 if (!swap_avail_heads) {
3853 pr_emerg("Not enough memory for swap heads, swap is disabled\n");
3854 return -ENOMEM;
3855 }
3856
3857 for_each_node(nid)
3858 plist_head_init(&swap_avail_heads[nid]);
3859
3860 return 0;
3861}
3862subsys_initcall(swapfile_init);