blob: cbf4fb457cb73ccb3622a0b8fbbbff2e7d2fc132 [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
Christoph Hellwigc97ab272022-04-20 06:27:19 +02009#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/mm.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010011#include <linux/sched/mm.h>
Ingo Molnar29930022017-02-08 18:51:36 +010012#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/hugetlb.h>
14#include <linux/mman.h>
15#include <linux/slab.h>
16#include <linux/kernel_stat.h>
17#include <linux/swap.h>
18#include <linux/vmalloc.h>
19#include <linux/pagemap.h>
20#include <linux/namei.h>
Hugh Dickins072441e2011-06-27 16:18:02 -070021#include <linux/shmem_fs.h>
Christoph Hellwige41d12f2021-09-20 14:33:13 +020022#include <linux/blk-cgroup.h>
Hugh Dickins20137a42009-01-06 14:39:54 -080023#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/writeback.h>
25#include <linux/proc_fs.h>
26#include <linux/seq_file.h>
27#include <linux/init.h>
Hugh Dickins5ad64682009-12-14 17:59:24 -080028#include <linux/ksm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/rmap.h>
30#include <linux/security.h>
31#include <linux/backing-dev.h>
Ingo Molnarfc0abb12006-01-18 17:42:33 -080032#include <linux/mutex.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080033#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/syscalls.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080035#include <linux/memcontrol.h>
Kay Sievers66d7dd52010-10-26 14:22:06 -070036#include <linux/poll.h>
David Rientjes72788c32011-05-24 17:11:40 -070037#include <linux/oom.h>
Dan Magenheimer38b5faf2012-04-09 17:08:06 -060038#include <linux/frontswap.h>
39#include <linux/swapfile.h>
Mel Gormanf981c592012-07-31 16:44:47 -070040#include <linux/export.h>
Tim Chen67afa382017-02-22 15:45:39 -080041#include <linux/swap_slots.h>
Huang Ying155b5f82017-07-06 15:40:31 -070042#include <linux/sort.h>
Miaohe Lin63d86202021-06-28 19:36:46 -070043#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/tlbflush.h>
46#include <linux/swapops.h>
Johannes Weiner5d1ea48bd2014-12-10 15:44:55 -080047#include <linux/swap_cgroup.h>
NeilBrown014bb1d2022-05-09 18:20:47 -070048#include "swap.h"
Shreyas K Kd7e1f4f2023-05-31 10:16:20 +053049#include <trace/hooks/bl_hib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Hugh Dickins570a335b2009-12-14 17:58:46 -080051static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
52 unsigned char);
53static void free_swap_count_continuations(struct swap_info_struct *);
54
Christoph Hellwig633423a2022-01-21 22:15:07 -080055static DEFINE_SPINLOCK(swap_lock);
Adrian Bunk7c363b82008-07-25 19:46:24 -070056static unsigned int nr_swapfiles;
Shaohua Liec8acf22013-02-22 16:34:38 -080057atomic_long_t nr_swap_pages;
Chris Wilsonfb0fec52015-12-04 15:58:53 +000058/*
59 * Some modules use swappable objects and may try to swap them out under
60 * memory pressure (via the shrinker). Before doing so, they may wish to
61 * check to see if any swap space is available.
62 */
63EXPORT_SYMBOL_GPL(nr_swap_pages);
Shaohua Liec8acf22013-02-22 16:34:38 -080064/* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065long total_swap_pages;
Aaron Lua2468cc2017-09-06 16:24:57 -070066static int least_priority = -1;
Peter Xube45a492022-08-11 12:13:30 -040067unsigned long swapfile_maximum_size;
Peter Xu5154e602022-08-11 12:13:31 -040068#ifdef CONFIG_MIGRATION
69bool swap_migration_ad_supported;
70#endif /* CONFIG_MIGRATION */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static const char Bad_file[] = "Bad swap file entry ";
73static const char Unused_file[] = "Unused swap file entry ";
74static const char Bad_offset[] = "Bad swap offset entry ";
75static const char Unused_offset[] = "Unused swap offset entry ";
76
Dan Streetmanadfab832014-06-04 16:09:53 -070077/*
78 * all active swap_info_structs
79 * protected with swap_lock, and ordered by priority.
80 */
Christoph Hellwig633423a2022-01-21 22:15:07 -080081static PLIST_HEAD(swap_active_head);
Dan Streetman18ab4d42014-06-04 16:09:59 -070082
83/*
84 * all available (active, not full) swap_info_structs
85 * protected with swap_avail_lock, ordered by priority.
Matthew Wilcox (Oracle)e2e3fdc2022-05-12 20:23:02 -070086 * This is used by folio_alloc_swap() instead of swap_active_head
Dan Streetman18ab4d42014-06-04 16:09:59 -070087 * because swap_active_head includes all swap_info_structs,
Matthew Wilcox (Oracle)e2e3fdc2022-05-12 20:23:02 -070088 * but folio_alloc_swap() doesn't need to look at full ones.
Dan Streetman18ab4d42014-06-04 16:09:59 -070089 * This uses its own lock instead of swap_lock because when a
90 * swap_info_struct changes between not-full/full, it needs to
91 * add/remove itself to/from this list, but the swap_info_struct->lock
92 * is held and the locking order requires swap_lock to be taken
93 * before any swap_info_struct->lock.
94 */
Colin Ian Kingbfc6b1c2018-04-10 16:29:55 -070095static struct plist_head *swap_avail_heads;
Dan Streetman18ab4d42014-06-04 16:09:59 -070096static DEFINE_SPINLOCK(swap_avail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Dan Magenheimer38b5faf2012-04-09 17:08:06 -060098struct swap_info_struct *swap_info[MAX_SWAPFILES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800100static DEFINE_MUTEX(swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Kay Sievers66d7dd52010-10-26 14:22:06 -0700102static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
103/* Activity counter to indicate that a swapon or swapoff has occurred */
104static atomic_t proc_poll_event = ATOMIC_INIT(0);
105
Huang Ying81a02982017-09-06 16:24:43 -0700106atomic_t nr_rotate_swap = ATOMIC_INIT(0);
107
Daniel Jordanc10d38c2019-03-05 15:48:19 -0800108static struct swap_info_struct *swap_type_to_swap_info(int type)
109{
Huang Yinga4b45112021-06-28 19:37:09 -0700110 if (type >= MAX_SWAPFILES)
Daniel Jordanc10d38c2019-03-05 15:48:19 -0800111 return NULL;
112
Huang Yinga4b45112021-06-28 19:37:09 -0700113 return READ_ONCE(swap_info[type]); /* rcu_dereference() */
Daniel Jordanc10d38c2019-03-05 15:48:19 -0800114}
115
Hugh Dickins8d69aae2009-12-14 17:58:45 -0800116static inline unsigned char swap_count(unsigned char ent)
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700117{
Daniel Jordan955c97f2018-06-14 15:26:21 -0700118 return ent & ~SWAP_HAS_CACHE; /* may include COUNT_CONTINUED flag */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700119}
120
Huang Yingbcd49e82018-10-26 15:03:46 -0700121/* Reclaim the swap entry anyway if possible */
122#define TTRS_ANYWAY 0x1
123/*
124 * Reclaim the swap entry if there are no more mappings of the
125 * corresponding page
126 */
127#define TTRS_UNMAPPED 0x2
128/* Reclaim the swap entry if swap is getting full*/
129#define TTRS_FULL 0x4
130
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800131/* returns 1 if swap entry is freed */
Huang Yingbcd49e82018-10-26 15:03:46 -0700132static int __try_to_reclaim_swap(struct swap_info_struct *si,
133 unsigned long offset, unsigned long flags)
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700134{
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800135 swp_entry_t entry = swp_entry(si->type, offset);
Matthew Wilcox (Oracle)2c3f6192022-09-02 20:46:31 +0100136 struct folio *folio;
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700137 int ret = 0;
138
Matthew Wilcox (Oracle)2c3f6192022-09-02 20:46:31 +0100139 folio = filemap_get_folio(swap_address_space(entry), offset);
140 if (!folio)
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700141 return 0;
142 /*
Huang Yingbcd49e82018-10-26 15:03:46 -0700143 * When this function is called from scan_swap_map_slots() and it's
Matthew Wilcox (Oracle)2c3f6192022-09-02 20:46:31 +0100144 * called by vmscan.c at reclaiming folios. So we hold a folio lock
Huang Yingbcd49e82018-10-26 15:03:46 -0700145 * here. We have to use trylock for avoiding deadlock. This is a special
Matthew Wilcox (Oracle)2c3f6192022-09-02 20:46:31 +0100146 * case and you should use folio_free_swap() with explicit folio_lock()
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700147 * in usual operations.
148 */
Matthew Wilcox (Oracle)2c3f6192022-09-02 20:46:31 +0100149 if (folio_trylock(folio)) {
Huang Yingbcd49e82018-10-26 15:03:46 -0700150 if ((flags & TTRS_ANYWAY) ||
Matthew Wilcox (Oracle)2c3f6192022-09-02 20:46:31 +0100151 ((flags & TTRS_UNMAPPED) && !folio_mapped(folio)) ||
Matthew Wilcox (Oracle)9202d527b2022-09-02 20:46:43 +0100152 ((flags & TTRS_FULL) && mem_cgroup_swap_full(folio)))
Matthew Wilcox (Oracle)2c3f6192022-09-02 20:46:31 +0100153 ret = folio_free_swap(folio);
154 folio_unlock(folio);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700155 }
Matthew Wilcox (Oracle)2c3f6192022-09-02 20:46:31 +0100156 folio_put(folio);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700157 return ret;
158}
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700159
Aaron Lu4efaceb2019-07-11 20:55:41 -0700160static inline struct swap_extent *first_se(struct swap_info_struct *sis)
161{
162 struct rb_node *rb = rb_first(&sis->swap_extent_root);
163 return rb_entry(rb, struct swap_extent, rb_node);
164}
165
166static inline struct swap_extent *next_se(struct swap_extent *se)
167{
168 struct rb_node *rb = rb_next(&se->rb_node);
169 return rb ? rb_entry(rb, struct swap_extent, rb_node) : NULL;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800173 * swapon tell device that all the old swap contents can be discarded,
174 * to allow the swap device to optimize its wear-levelling.
175 */
176static int discard_swap(struct swap_info_struct *si)
177{
178 struct swap_extent *se;
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800179 sector_t start_block;
180 sector_t nr_blocks;
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800181 int err = 0;
182
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800183 /* Do not discard the swap header page! */
Aaron Lu4efaceb2019-07-11 20:55:41 -0700184 se = first_se(si);
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800185 start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
186 nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
187 if (nr_blocks) {
188 err = blkdev_issue_discard(si->bdev, start_block,
Christoph Hellwig44abff2c2022-04-15 06:52:57 +0200189 nr_blocks, GFP_KERNEL);
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800190 if (err)
191 return err;
192 cond_resched();
193 }
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800194
Aaron Lu4efaceb2019-07-11 20:55:41 -0700195 for (se = next_se(se); se; se = next_se(se)) {
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800196 start_block = se->start_block << (PAGE_SHIFT - 9);
197 nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800198
199 err = blkdev_issue_discard(si->bdev, start_block,
Christoph Hellwig44abff2c2022-04-15 06:52:57 +0200200 nr_blocks, GFP_KERNEL);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800201 if (err)
202 break;
203
204 cond_resched();
205 }
206 return err; /* That will often be -EOPNOTSUPP */
207}
208
Aaron Lu4efaceb2019-07-11 20:55:41 -0700209static struct swap_extent *
210offset_to_swap_extent(struct swap_info_struct *sis, unsigned long offset)
211{
212 struct swap_extent *se;
213 struct rb_node *rb;
214
215 rb = sis->swap_extent_root.rb_node;
216 while (rb) {
217 se = rb_entry(rb, struct swap_extent, rb_node);
218 if (offset < se->start_page)
219 rb = rb->rb_left;
220 else if (offset >= se->start_page + se->nr_pages)
221 rb = rb->rb_right;
222 else
223 return se;
224 }
225 /* It *must* be present */
226 BUG();
227}
228
Jens Axboecaf69122021-03-02 14:53:21 -0700229sector_t swap_page_sector(struct page *page)
230{
231 struct swap_info_struct *sis = page_swap_info(page);
232 struct swap_extent *se;
233 sector_t sector;
234 pgoff_t offset;
235
236 offset = __page_file_index(page);
237 se = offset_to_swap_extent(sis, offset);
238 sector = se->start_block + (offset - se->start_page);
239 return sector << (PAGE_SHIFT - 9);
240}
241
Hugh Dickins7992fde2009-01-06 14:39:53 -0800242/*
243 * swap allocation tell device that a cluster of swap can now be discarded,
244 * to allow the swap device to optimize its wear-levelling.
245 */
246static void discard_swap_cluster(struct swap_info_struct *si,
247 pgoff_t start_page, pgoff_t nr_pages)
248{
Aaron Lu4efaceb2019-07-11 20:55:41 -0700249 struct swap_extent *se = offset_to_swap_extent(si, start_page);
Hugh Dickins7992fde2009-01-06 14:39:53 -0800250
251 while (nr_pages) {
Aaron Lu4efaceb2019-07-11 20:55:41 -0700252 pgoff_t offset = start_page - se->start_page;
253 sector_t start_block = se->start_block + offset;
254 sector_t nr_blocks = se->nr_pages - offset;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800255
Aaron Lu4efaceb2019-07-11 20:55:41 -0700256 if (nr_blocks > nr_pages)
257 nr_blocks = nr_pages;
258 start_page += nr_blocks;
259 nr_pages -= nr_blocks;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800260
Aaron Lu4efaceb2019-07-11 20:55:41 -0700261 start_block <<= PAGE_SHIFT - 9;
262 nr_blocks <<= PAGE_SHIFT - 9;
263 if (blkdev_issue_discard(si->bdev, start_block,
Christoph Hellwig44abff2c2022-04-15 06:52:57 +0200264 nr_blocks, GFP_NOIO))
Aaron Lu4efaceb2019-07-11 20:55:41 -0700265 break;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800266
Aaron Lu4efaceb2019-07-11 20:55:41 -0700267 se = next_se(se);
Hugh Dickins7992fde2009-01-06 14:39:53 -0800268 }
269}
270
Huang Ying38d8b4e2017-07-06 15:37:18 -0700271#ifdef CONFIG_THP_SWAP
272#define SWAPFILE_CLUSTER HPAGE_PMD_NR
Huang Yinga448f2d2018-08-21 21:52:17 -0700273
274#define swap_entry_size(size) (size)
Huang Ying38d8b4e2017-07-06 15:37:18 -0700275#else
Hugh Dickins048c27f2005-09-03 15:54:40 -0700276#define SWAPFILE_CLUSTER 256
Huang Yinga448f2d2018-08-21 21:52:17 -0700277
278/*
279 * Define swap_entry_size() as constant to let compiler to optimize
280 * out some code if !CONFIG_THP_SWAP
281 */
282#define swap_entry_size(size) 1
Huang Ying38d8b4e2017-07-06 15:37:18 -0700283#endif
Hugh Dickins048c27f2005-09-03 15:54:40 -0700284#define LATENCY_LIMIT 256
285
Shaohua Li2a8f9442013-09-11 14:20:28 -0700286static inline void cluster_set_flag(struct swap_cluster_info *info,
287 unsigned int flag)
288{
289 info->flags = flag;
290}
291
292static inline unsigned int cluster_count(struct swap_cluster_info *info)
293{
294 return info->data;
295}
296
297static inline void cluster_set_count(struct swap_cluster_info *info,
298 unsigned int c)
299{
300 info->data = c;
301}
302
303static inline void cluster_set_count_flag(struct swap_cluster_info *info,
304 unsigned int c, unsigned int f)
305{
306 info->flags = f;
307 info->data = c;
308}
309
310static inline unsigned int cluster_next(struct swap_cluster_info *info)
311{
312 return info->data;
313}
314
315static inline void cluster_set_next(struct swap_cluster_info *info,
316 unsigned int n)
317{
318 info->data = n;
319}
320
321static inline void cluster_set_next_flag(struct swap_cluster_info *info,
322 unsigned int n, unsigned int f)
323{
324 info->flags = f;
325 info->data = n;
326}
327
328static inline bool cluster_is_free(struct swap_cluster_info *info)
329{
330 return info->flags & CLUSTER_FLAG_FREE;
331}
332
333static inline bool cluster_is_null(struct swap_cluster_info *info)
334{
335 return info->flags & CLUSTER_FLAG_NEXT_NULL;
336}
337
338static inline void cluster_set_null(struct swap_cluster_info *info)
339{
340 info->flags = CLUSTER_FLAG_NEXT_NULL;
341 info->data = 0;
342}
343
Huang Yinge0709822017-09-06 16:22:16 -0700344static inline bool cluster_is_huge(struct swap_cluster_info *info)
345{
Huang Ying33ee0112018-08-21 21:52:13 -0700346 if (IS_ENABLED(CONFIG_THP_SWAP))
347 return info->flags & CLUSTER_FLAG_HUGE;
348 return false;
Huang Yinge0709822017-09-06 16:22:16 -0700349}
350
351static inline void cluster_clear_huge(struct swap_cluster_info *info)
352{
353 info->flags &= ~CLUSTER_FLAG_HUGE;
354}
355
Huang, Ying235b6212017-02-22 15:45:22 -0800356static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si,
357 unsigned long offset)
358{
359 struct swap_cluster_info *ci;
360
361 ci = si->cluster_info;
362 if (ci) {
363 ci += offset / SWAPFILE_CLUSTER;
364 spin_lock(&ci->lock);
365 }
366 return ci;
367}
368
369static inline void unlock_cluster(struct swap_cluster_info *ci)
370{
371 if (ci)
372 spin_unlock(&ci->lock);
373}
374
Huang Ying59d98bf2018-08-21 21:52:01 -0700375/*
376 * Determine the locking method in use for this device. Return
377 * swap_cluster_info if SSD-style cluster-based locking is in place.
378 */
Huang, Ying235b6212017-02-22 15:45:22 -0800379static inline struct swap_cluster_info *lock_cluster_or_swap_info(
Huang Ying59d98bf2018-08-21 21:52:01 -0700380 struct swap_info_struct *si, unsigned long offset)
Huang, Ying235b6212017-02-22 15:45:22 -0800381{
382 struct swap_cluster_info *ci;
383
Huang Ying59d98bf2018-08-21 21:52:01 -0700384 /* Try to use fine-grained SSD-style locking if available: */
Huang, Ying235b6212017-02-22 15:45:22 -0800385 ci = lock_cluster(si, offset);
Huang Ying59d98bf2018-08-21 21:52:01 -0700386 /* Otherwise, fall back to traditional, coarse locking: */
Huang, Ying235b6212017-02-22 15:45:22 -0800387 if (!ci)
388 spin_lock(&si->lock);
389
390 return ci;
391}
392
393static inline void unlock_cluster_or_swap_info(struct swap_info_struct *si,
394 struct swap_cluster_info *ci)
395{
396 if (ci)
397 unlock_cluster(ci);
398 else
399 spin_unlock(&si->lock);
400}
401
Huang Ying6b534912016-10-07 16:58:42 -0700402static inline bool cluster_list_empty(struct swap_cluster_list *list)
403{
404 return cluster_is_null(&list->head);
405}
406
407static inline unsigned int cluster_list_first(struct swap_cluster_list *list)
408{
409 return cluster_next(&list->head);
410}
411
412static void cluster_list_init(struct swap_cluster_list *list)
413{
414 cluster_set_null(&list->head);
415 cluster_set_null(&list->tail);
416}
417
418static void cluster_list_add_tail(struct swap_cluster_list *list,
419 struct swap_cluster_info *ci,
420 unsigned int idx)
421{
422 if (cluster_list_empty(list)) {
423 cluster_set_next_flag(&list->head, idx, 0);
424 cluster_set_next_flag(&list->tail, idx, 0);
425 } else {
Huang, Ying235b6212017-02-22 15:45:22 -0800426 struct swap_cluster_info *ci_tail;
Huang Ying6b534912016-10-07 16:58:42 -0700427 unsigned int tail = cluster_next(&list->tail);
428
Huang, Ying235b6212017-02-22 15:45:22 -0800429 /*
430 * Nested cluster lock, but both cluster locks are
431 * only acquired when we held swap_info_struct->lock
432 */
433 ci_tail = ci + tail;
434 spin_lock_nested(&ci_tail->lock, SINGLE_DEPTH_NESTING);
435 cluster_set_next(ci_tail, idx);
Huang Ying0ef017d2017-05-03 14:54:36 -0700436 spin_unlock(&ci_tail->lock);
Huang Ying6b534912016-10-07 16:58:42 -0700437 cluster_set_next_flag(&list->tail, idx, 0);
438 }
439}
440
441static unsigned int cluster_list_del_first(struct swap_cluster_list *list,
442 struct swap_cluster_info *ci)
443{
444 unsigned int idx;
445
446 idx = cluster_next(&list->head);
447 if (cluster_next(&list->tail) == idx) {
448 cluster_set_null(&list->head);
449 cluster_set_null(&list->tail);
450 } else
451 cluster_set_next_flag(&list->head,
452 cluster_next(&ci[idx]), 0);
453
454 return idx;
455}
456
Shaohua Li815c2c52013-09-11 14:20:30 -0700457/* Add a cluster to discard list and schedule it to do discard */
458static void swap_cluster_schedule_discard(struct swap_info_struct *si,
459 unsigned int idx)
460{
461 /*
Miaohe Linbb243f72021-06-28 19:37:00 -0700462 * If scan_swap_map_slots() can't find a free cluster, it will check
Shaohua Li815c2c52013-09-11 14:20:30 -0700463 * si->swap_map directly. To make sure the discarding cluster isn't
Miaohe Linbb243f72021-06-28 19:37:00 -0700464 * taken by scan_swap_map_slots(), mark the swap entries bad (occupied).
465 * It will be cleared after discard
Shaohua Li815c2c52013-09-11 14:20:30 -0700466 */
467 memset(si->swap_map + idx * SWAPFILE_CLUSTER,
468 SWAP_MAP_BAD, SWAPFILE_CLUSTER);
469
Huang Ying6b534912016-10-07 16:58:42 -0700470 cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx);
Shaohua Li815c2c52013-09-11 14:20:30 -0700471
472 schedule_work(&si->discard_work);
473}
474
Huang Ying38d8b4e2017-07-06 15:37:18 -0700475static void __free_cluster(struct swap_info_struct *si, unsigned long idx)
476{
477 struct swap_cluster_info *ci = si->cluster_info;
478
479 cluster_set_flag(ci + idx, CLUSTER_FLAG_FREE);
480 cluster_list_add_tail(&si->free_clusters, ci, idx);
481}
482
Shaohua Li815c2c52013-09-11 14:20:30 -0700483/*
484 * Doing discard actually. After a cluster discard is finished, the cluster
485 * will be added to free cluster list. caller should hold si->lock.
486*/
487static void swap_do_scheduled_discard(struct swap_info_struct *si)
488{
Huang, Ying235b6212017-02-22 15:45:22 -0800489 struct swap_cluster_info *info, *ci;
Shaohua Li815c2c52013-09-11 14:20:30 -0700490 unsigned int idx;
491
492 info = si->cluster_info;
493
Huang Ying6b534912016-10-07 16:58:42 -0700494 while (!cluster_list_empty(&si->discard_clusters)) {
495 idx = cluster_list_del_first(&si->discard_clusters, info);
Shaohua Li815c2c52013-09-11 14:20:30 -0700496 spin_unlock(&si->lock);
497
498 discard_swap_cluster(si, idx * SWAPFILE_CLUSTER,
499 SWAPFILE_CLUSTER);
500
501 spin_lock(&si->lock);
Huang, Ying235b6212017-02-22 15:45:22 -0800502 ci = lock_cluster(si, idx * SWAPFILE_CLUSTER);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700503 __free_cluster(si, idx);
Shaohua Li815c2c52013-09-11 14:20:30 -0700504 memset(si->swap_map + idx * SWAPFILE_CLUSTER,
505 0, SWAPFILE_CLUSTER);
Huang, Ying235b6212017-02-22 15:45:22 -0800506 unlock_cluster(ci);
Shaohua Li815c2c52013-09-11 14:20:30 -0700507 }
508}
509
510static void swap_discard_work(struct work_struct *work)
511{
512 struct swap_info_struct *si;
513
514 si = container_of(work, struct swap_info_struct, discard_work);
515
516 spin_lock(&si->lock);
517 swap_do_scheduled_discard(si);
518 spin_unlock(&si->lock);
519}
520
Miaohe Lin63d86202021-06-28 19:36:46 -0700521static void swap_users_ref_free(struct percpu_ref *ref)
522{
523 struct swap_info_struct *si;
524
525 si = container_of(ref, struct swap_info_struct, users);
526 complete(&si->comp);
527}
528
Huang Ying38d8b4e2017-07-06 15:37:18 -0700529static void alloc_cluster(struct swap_info_struct *si, unsigned long idx)
530{
531 struct swap_cluster_info *ci = si->cluster_info;
532
533 VM_BUG_ON(cluster_list_first(&si->free_clusters) != idx);
534 cluster_list_del_first(&si->free_clusters, ci);
535 cluster_set_count_flag(ci + idx, 0, 0);
536}
537
538static void free_cluster(struct swap_info_struct *si, unsigned long idx)
539{
540 struct swap_cluster_info *ci = si->cluster_info + idx;
541
542 VM_BUG_ON(cluster_count(ci) != 0);
543 /*
544 * If the swap is discardable, prepare discard the cluster
545 * instead of free it immediately. The cluster will be freed
546 * after discard.
547 */
548 if ((si->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) ==
549 (SWP_WRITEOK | SWP_PAGE_DISCARD)) {
550 swap_cluster_schedule_discard(si, idx);
551 return;
552 }
553
554 __free_cluster(si, idx);
555}
556
Shaohua Li2a8f9442013-09-11 14:20:28 -0700557/*
558 * The cluster corresponding to page_nr will be used. The cluster will be
559 * removed from free cluster list and its usage counter will be increased.
560 */
561static void inc_cluster_info_page(struct swap_info_struct *p,
562 struct swap_cluster_info *cluster_info, unsigned long page_nr)
563{
564 unsigned long idx = page_nr / SWAPFILE_CLUSTER;
565
566 if (!cluster_info)
567 return;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700568 if (cluster_is_free(&cluster_info[idx]))
569 alloc_cluster(p, idx);
Shaohua Li2a8f9442013-09-11 14:20:28 -0700570
571 VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER);
572 cluster_set_count(&cluster_info[idx],
573 cluster_count(&cluster_info[idx]) + 1);
574}
575
576/*
577 * The cluster corresponding to page_nr decreases one usage. If the usage
578 * counter becomes 0, which means no page in the cluster is in using, we can
579 * optionally discard the cluster and add it to free cluster list.
580 */
581static void dec_cluster_info_page(struct swap_info_struct *p,
582 struct swap_cluster_info *cluster_info, unsigned long page_nr)
583{
584 unsigned long idx = page_nr / SWAPFILE_CLUSTER;
585
586 if (!cluster_info)
587 return;
588
589 VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0);
590 cluster_set_count(&cluster_info[idx],
591 cluster_count(&cluster_info[idx]) - 1);
592
Huang Ying38d8b4e2017-07-06 15:37:18 -0700593 if (cluster_count(&cluster_info[idx]) == 0)
594 free_cluster(p, idx);
Shaohua Li2a8f9442013-09-11 14:20:28 -0700595}
596
597/*
Miaohe Linbb243f72021-06-28 19:37:00 -0700598 * It's possible scan_swap_map_slots() uses a free cluster in the middle of free
Shaohua Li2a8f9442013-09-11 14:20:28 -0700599 * cluster list. Avoiding such abuse to avoid list corruption.
600 */
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700601static bool
602scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si,
Shaohua Li2a8f9442013-09-11 14:20:28 -0700603 unsigned long offset)
604{
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700605 struct percpu_cluster *percpu_cluster;
606 bool conflict;
607
Shaohua Li2a8f9442013-09-11 14:20:28 -0700608 offset /= SWAPFILE_CLUSTER;
Huang Ying6b534912016-10-07 16:58:42 -0700609 conflict = !cluster_list_empty(&si->free_clusters) &&
610 offset != cluster_list_first(&si->free_clusters) &&
Shaohua Li2a8f9442013-09-11 14:20:28 -0700611 cluster_is_free(&si->cluster_info[offset]);
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700612
613 if (!conflict)
614 return false;
615
616 percpu_cluster = this_cpu_ptr(si->percpu_cluster);
617 cluster_set_null(&percpu_cluster->index);
618 return true;
619}
620
621/*
622 * Try to get a swap entry from current cpu's swap entry pool (a cluster). This
623 * might involve allocating a new cluster for current CPU too.
624 */
Tim Chen36005ba2017-02-22 15:45:33 -0800625static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700626 unsigned long *offset, unsigned long *scan_base)
627{
628 struct percpu_cluster *cluster;
Huang, Ying235b6212017-02-22 15:45:22 -0800629 struct swap_cluster_info *ci;
Huang, Ying235b6212017-02-22 15:45:22 -0800630 unsigned long tmp, max;
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700631
632new_cluster:
633 cluster = this_cpu_ptr(si->percpu_cluster);
634 if (cluster_is_null(&cluster->index)) {
Huang Ying6b534912016-10-07 16:58:42 -0700635 if (!cluster_list_empty(&si->free_clusters)) {
636 cluster->index = si->free_clusters.head;
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700637 cluster->next = cluster_next(&cluster->index) *
638 SWAPFILE_CLUSTER;
Huang Ying6b534912016-10-07 16:58:42 -0700639 } else if (!cluster_list_empty(&si->discard_clusters)) {
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700640 /*
641 * we don't have free cluster but have some clusters in
Huang Ying49070582020-06-01 21:49:22 -0700642 * discarding, do discard now and reclaim them, then
643 * reread cluster_next_cpu since we dropped si->lock
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700644 */
645 swap_do_scheduled_discard(si);
Huang Ying49070582020-06-01 21:49:22 -0700646 *scan_base = this_cpu_read(*si->cluster_next_cpu);
647 *offset = *scan_base;
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700648 goto new_cluster;
649 } else
Tim Chen36005ba2017-02-22 15:45:33 -0800650 return false;
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700651 }
652
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700653 /*
654 * Other CPUs can use our cluster if they can't find a free cluster,
655 * check if there is still free entry in the cluster
656 */
657 tmp = cluster->next;
Huang, Ying235b6212017-02-22 15:45:22 -0800658 max = min_t(unsigned long, si->max,
659 (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER);
Wei Yang7b9e2de2020-06-01 21:49:07 -0700660 if (tmp < max) {
661 ci = lock_cluster(si, tmp);
662 while (tmp < max) {
663 if (!si->swap_map[tmp])
664 break;
665 tmp++;
666 }
667 unlock_cluster(ci);
Huang, Ying235b6212017-02-22 15:45:22 -0800668 }
Wei Yang0fd0e192020-06-01 21:49:01 -0700669 if (tmp >= max) {
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700670 cluster_set_null(&cluster->index);
671 goto new_cluster;
672 }
673 cluster->next = tmp + 1;
674 *offset = tmp;
675 *scan_base = tmp;
Wei Yangfdff1de2020-06-01 21:49:04 -0700676 return true;
Shaohua Li2a8f9442013-09-11 14:20:28 -0700677}
678
Aaron Lua2468cc2017-09-06 16:24:57 -0700679static void __del_from_avail_list(struct swap_info_struct *p)
680{
681 int nid;
682
Rongwei Wang85cc1182023-04-04 23:47:16 +0800683 assert_spin_locked(&p->lock);
Aaron Lua2468cc2017-09-06 16:24:57 -0700684 for_each_node(nid)
685 plist_del(&p->avail_lists[nid], &swap_avail_heads[nid]);
686}
687
688static void del_from_avail_list(struct swap_info_struct *p)
689{
690 spin_lock(&swap_avail_lock);
691 __del_from_avail_list(p);
692 spin_unlock(&swap_avail_lock);
693}
694
Huang Ying38d8b4e2017-07-06 15:37:18 -0700695static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset,
696 unsigned int nr_entries)
697{
698 unsigned int end = offset + nr_entries - 1;
699
700 if (offset == si->lowest_bit)
701 si->lowest_bit += nr_entries;
702 if (end == si->highest_bit)
Qian Caia449bf52020-08-14 17:31:31 -0700703 WRITE_ONCE(si->highest_bit, si->highest_bit - nr_entries);
Miaohe Linc8945302022-06-08 22:40:30 +0800704 WRITE_ONCE(si->inuse_pages, si->inuse_pages + nr_entries);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700705 if (si->inuse_pages == si->pages) {
706 si->lowest_bit = si->max;
707 si->highest_bit = 0;
Aaron Lua2468cc2017-09-06 16:24:57 -0700708 del_from_avail_list(si);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700709 }
710}
711
Aaron Lua2468cc2017-09-06 16:24:57 -0700712static void add_to_avail_list(struct swap_info_struct *p)
713{
714 int nid;
715
716 spin_lock(&swap_avail_lock);
717 for_each_node(nid) {
718 WARN_ON(!plist_node_empty(&p->avail_lists[nid]));
719 plist_add(&p->avail_lists[nid], &swap_avail_heads[nid]);
720 }
721 spin_unlock(&swap_avail_lock);
722}
723
Huang Ying38d8b4e2017-07-06 15:37:18 -0700724static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
725 unsigned int nr_entries)
726{
Joonsoo Kim3852f672020-08-11 18:30:47 -0700727 unsigned long begin = offset;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700728 unsigned long end = offset + nr_entries - 1;
729 void (*swap_slot_free_notify)(struct block_device *, unsigned long);
730
731 if (offset < si->lowest_bit)
732 si->lowest_bit = offset;
733 if (end > si->highest_bit) {
734 bool was_full = !si->highest_bit;
735
Qian Caia449bf52020-08-14 17:31:31 -0700736 WRITE_ONCE(si->highest_bit, end);
Aaron Lua2468cc2017-09-06 16:24:57 -0700737 if (was_full && (si->flags & SWP_WRITEOK))
738 add_to_avail_list(si);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700739 }
740 atomic_long_add(nr_entries, &nr_swap_pages);
Miaohe Linc8945302022-06-08 22:40:30 +0800741 WRITE_ONCE(si->inuse_pages, si->inuse_pages - nr_entries);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700742 if (si->flags & SWP_BLKDEV)
743 swap_slot_free_notify =
744 si->bdev->bd_disk->fops->swap_slot_free_notify;
745 else
746 swap_slot_free_notify = NULL;
747 while (offset <= end) {
Steven Price8a848022020-05-13 16:37:49 +0100748 arch_swap_invalidate_page(si->type, offset);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700749 frontswap_invalidate_page(si->type, offset);
750 if (swap_slot_free_notify)
751 swap_slot_free_notify(si->bdev, offset);
752 offset++;
753 }
Joonsoo Kim3852f672020-08-11 18:30:47 -0700754 clear_shadow_from_swap_cache(si->type, begin, end);
Huang Ying38d8b4e2017-07-06 15:37:18 -0700755}
756
Huang Ying49070582020-06-01 21:49:22 -0700757static void set_cluster_next(struct swap_info_struct *si, unsigned long next)
758{
759 unsigned long prev;
760
761 if (!(si->flags & SWP_SOLIDSTATE)) {
762 si->cluster_next = next;
763 return;
764 }
765
766 prev = this_cpu_read(*si->cluster_next_cpu);
767 /*
768 * Cross the swap address space size aligned trunk, choose
769 * another trunk randomly to avoid lock contention on swap
770 * address space if possible.
771 */
772 if ((prev >> SWAP_ADDRESS_SPACE_SHIFT) !=
773 (next >> SWAP_ADDRESS_SPACE_SHIFT)) {
774 /* No free swap slots available */
775 if (si->highest_bit <= si->lowest_bit)
776 return;
777 next = si->lowest_bit +
778 prandom_u32_max(si->highest_bit - si->lowest_bit + 1);
779 next = ALIGN_DOWN(next, SWAP_ADDRESS_SPACE_PAGES);
780 next = max_t(unsigned int, next, si->lowest_bit);
781 }
782 this_cpu_write(*si->cluster_next_cpu, next);
783}
784
Miaohe Lin4b9ae8422022-05-19 14:08:52 -0700785static bool swap_offset_available_and_locked(struct swap_info_struct *si,
786 unsigned long offset)
787{
788 if (data_race(!si->swap_map[offset])) {
789 spin_lock(&si->lock);
790 return true;
791 }
792
793 if (vm_swap_full() && READ_ONCE(si->swap_map[offset]) == SWAP_HAS_CACHE) {
794 spin_lock(&si->lock);
795 return true;
796 }
797
798 return false;
799}
800
Tim Chen36005ba2017-02-22 15:45:33 -0800801static int scan_swap_map_slots(struct swap_info_struct *si,
802 unsigned char usage, int nr,
803 swp_entry_t slots[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Huang, Ying235b6212017-02-22 15:45:22 -0800805 struct swap_cluster_info *ci;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800806 unsigned long offset;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800807 unsigned long scan_base;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800808 unsigned long last_in_cluster = 0;
Hugh Dickins048c27f2005-09-03 15:54:40 -0700809 int latency_ration = LATENCY_LIMIT;
Tim Chen36005ba2017-02-22 15:45:33 -0800810 int n_ret = 0;
Huang Yinged43af12020-06-01 21:49:10 -0700811 bool scanned_many = false;
Tim Chen36005ba2017-02-22 15:45:33 -0800812
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800813 /*
Hugh Dickins7dfad412005-09-03 15:54:38 -0700814 * We try to cluster swap pages by allocating them sequentially
815 * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
816 * way, however, we resort to first-free allocation, starting
817 * a new cluster. This prevents us from scattering swap pages
818 * all over the entire swap partition, so that we reduce
819 * overall disk seek times between swap pages. -- sct
820 * But we do now try to find an empty cluster. -Andrea
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800821 * And we let swap pages go all over an SSD partition. Hugh
Hugh Dickins7dfad412005-09-03 15:54:38 -0700822 */
823
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700824 si->flags += SWP_SCANNING;
Huang Ying49070582020-06-01 21:49:22 -0700825 /*
826 * Use percpu scan base for SSD to reduce lock contention on
827 * cluster and swap cache. For HDD, sequential access is more
828 * important.
829 */
830 if (si->flags & SWP_SOLIDSTATE)
831 scan_base = this_cpu_read(*si->cluster_next_cpu);
832 else
833 scan_base = si->cluster_next;
834 offset = scan_base;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800835
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700836 /* SSD algorithm */
837 if (si->cluster_info) {
Wei Yangbd2d18d2020-06-01 21:48:52 -0700838 if (!scan_swap_map_try_ssd_cluster(si, &offset, &scan_base))
Tim Chen36005ba2017-02-22 15:45:33 -0800839 goto scan;
Wei Yangf4eaf512020-06-01 21:48:49 -0700840 } else if (unlikely(!si->cluster_nr--)) {
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800841 if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
842 si->cluster_nr = SWAPFILE_CLUSTER - 1;
843 goto checks;
844 }
Shaohua Li2a8f9442013-09-11 14:20:28 -0700845
Shaohua Liec8acf22013-02-22 16:34:38 -0800846 spin_unlock(&si->lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700847
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800848 /*
849 * If seek is expensive, start searching for new cluster from
850 * start of partition, to minimize the span of allocated swap.
Chen Yucong50088c42014-06-04 16:10:57 -0700851 * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info
852 * case, just handled by scan_swap_map_try_ssd_cluster() above.
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800853 */
Chen Yucong50088c42014-06-04 16:10:57 -0700854 scan_base = offset = si->lowest_bit;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700855 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
856
857 /* Locate the first empty (unaligned) cluster */
858 for (; last_in_cluster <= si->highest_bit; offset++) {
859 if (si->swap_map[offset])
860 last_in_cluster = offset + SWAPFILE_CLUSTER;
861 else if (offset == last_in_cluster) {
Shaohua Liec8acf22013-02-22 16:34:38 -0800862 spin_lock(&si->lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800863 offset -= SWAPFILE_CLUSTER - 1;
864 si->cluster_next = offset;
865 si->cluster_nr = SWAPFILE_CLUSTER - 1;
866 goto checks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700868 if (unlikely(--latency_ration < 0)) {
869 cond_resched();
870 latency_ration = LATENCY_LIMIT;
871 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700872 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800873
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800874 offset = scan_base;
Shaohua Liec8acf22013-02-22 16:34:38 -0800875 spin_lock(&si->lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800876 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700878
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800879checks:
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700880 if (si->cluster_info) {
Tim Chen36005ba2017-02-22 15:45:33 -0800881 while (scan_swap_map_ssd_cluster_conflict(si, offset)) {
882 /* take a break if we already got some slots */
883 if (n_ret)
884 goto done;
885 if (!scan_swap_map_try_ssd_cluster(si, &offset,
886 &scan_base))
887 goto scan;
888 }
Shaohua Liebc2a1a62013-09-11 14:20:32 -0700889 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800890 if (!(si->flags & SWP_WRITEOK))
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700891 goto no_page;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700892 if (!si->highest_bit)
893 goto no_page;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800894 if (offset > si->highest_bit)
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800895 scan_base = offset = si->lowest_bit;
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700896
Huang, Ying235b6212017-02-22 15:45:22 -0800897 ci = lock_cluster(si, offset);
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -0700898 /* reuse swap entry of cache-only swap if not busy. */
899 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700900 int swap_was_freed;
Huang, Ying235b6212017-02-22 15:45:22 -0800901 unlock_cluster(ci);
Shaohua Liec8acf22013-02-22 16:34:38 -0800902 spin_unlock(&si->lock);
Huang Yingbcd49e82018-10-26 15:03:46 -0700903 swap_was_freed = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY);
Shaohua Liec8acf22013-02-22 16:34:38 -0800904 spin_lock(&si->lock);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700905 /* entry was freed successfully, try to use this again */
906 if (swap_was_freed)
907 goto checks;
908 goto scan; /* check next one */
909 }
910
Huang, Ying235b6212017-02-22 15:45:22 -0800911 if (si->swap_map[offset]) {
912 unlock_cluster(ci);
Tim Chen36005ba2017-02-22 15:45:33 -0800913 if (!n_ret)
914 goto scan;
915 else
916 goto done;
Huang, Ying235b6212017-02-22 15:45:22 -0800917 }
Qian Caia449bf52020-08-14 17:31:31 -0700918 WRITE_ONCE(si->swap_map[offset], usage);
Huang Ying2872bb22017-05-03 14:54:39 -0700919 inc_cluster_info_page(si, si->cluster_info, offset);
920 unlock_cluster(ci);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700921
Huang Ying38d8b4e2017-07-06 15:37:18 -0700922 swap_range_alloc(si, offset, 1);
Tim Chen36005ba2017-02-22 15:45:33 -0800923 slots[n_ret++] = swp_entry(si->type, offset);
Hugh Dickins7992fde2009-01-06 14:39:53 -0800924
Tim Chen36005ba2017-02-22 15:45:33 -0800925 /* got enough slots or reach max slots? */
926 if ((n_ret == nr) || (offset >= si->highest_bit))
927 goto done;
928
929 /* search for next available slot */
930
931 /* time to take a break? */
932 if (unlikely(--latency_ration < 0)) {
933 if (n_ret)
934 goto done;
935 spin_unlock(&si->lock);
936 cond_resched();
937 spin_lock(&si->lock);
938 latency_ration = LATENCY_LIMIT;
939 }
940
941 /* try to get more slots in cluster */
942 if (si->cluster_info) {
943 if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base))
944 goto checks;
Wei Yangf4eaf512020-06-01 21:48:49 -0700945 } else if (si->cluster_nr && !si->swap_map[++offset]) {
946 /* non-ssd case, still more slots in cluster? */
Tim Chen36005ba2017-02-22 15:45:33 -0800947 --si->cluster_nr;
948 goto checks;
949 }
950
Huang Yinged43af12020-06-01 21:49:10 -0700951 /*
952 * Even if there's no free clusters available (fragmented),
953 * try to scan a little more quickly with lock held unless we
954 * have scanned too many slots already.
955 */
956 if (!scanned_many) {
957 unsigned long scan_limit;
958
959 if (offset < scan_base)
960 scan_limit = scan_base;
961 else
962 scan_limit = si->highest_bit;
963 for (; offset <= scan_limit && --latency_ration > 0;
964 offset++) {
965 if (!si->swap_map[offset])
966 goto checks;
967 }
968 }
969
Tim Chen36005ba2017-02-22 15:45:33 -0800970done:
Huang Ying49070582020-06-01 21:49:22 -0700971 set_cluster_next(si, offset + 1);
Tim Chen36005ba2017-02-22 15:45:33 -0800972 si->flags -= SWP_SCANNING;
973 return n_ret;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800974
975scan:
Shaohua Liec8acf22013-02-22 16:34:38 -0800976 spin_unlock(&si->lock);
Qian Caia449bf52020-08-14 17:31:31 -0700977 while (++offset <= READ_ONCE(si->highest_bit)) {
Hugh Dickins048c27f2005-09-03 15:54:40 -0700978 if (unlikely(--latency_ration < 0)) {
979 cond_resched();
980 latency_ration = LATENCY_LIMIT;
Huang Yinged43af12020-06-01 21:49:10 -0700981 scanned_many = true;
Hugh Dickins048c27f2005-09-03 15:54:40 -0700982 }
Chen Wandunde1ccfb2022-11-18 21:38:50 +0800983 if (swap_offset_available_and_locked(si, offset))
984 goto checks;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700985 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800986 offset = si->lowest_bit;
Jamie Liua5998062014-01-23 15:53:40 -0800987 while (offset < scan_base) {
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800988 if (unlikely(--latency_ration < 0)) {
989 cond_resched();
990 latency_ration = LATENCY_LIMIT;
Huang Yinged43af12020-06-01 21:49:10 -0700991 scanned_many = true;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800992 }
Chen Wandunde1ccfb2022-11-18 21:38:50 +0800993 if (swap_offset_available_and_locked(si, offset))
994 goto checks;
Jamie Liua5998062014-01-23 15:53:40 -0800995 offset++;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800996 }
Shaohua Liec8acf22013-02-22 16:34:38 -0800997 spin_lock(&si->lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700998
999no_page:
Hugh Dickins52b7efdb2005-09-03 15:54:39 -07001000 si->flags -= SWP_SCANNING;
Tim Chen36005ba2017-02-22 15:45:33 -08001001 return n_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Huang Ying38d8b4e2017-07-06 15:37:18 -07001004static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot)
1005{
1006 unsigned long idx;
1007 struct swap_cluster_info *ci;
Miaohe Lin661c7562020-12-14 19:06:07 -08001008 unsigned long offset;
Huang Ying38d8b4e2017-07-06 15:37:18 -07001009
Huang Yingfe5266d2018-08-21 21:52:05 -07001010 /*
1011 * Should not even be attempting cluster allocations when huge
1012 * page swap is disabled. Warn and fail the allocation.
1013 */
1014 if (!IS_ENABLED(CONFIG_THP_SWAP)) {
1015 VM_WARN_ON_ONCE(1);
1016 return 0;
1017 }
1018
Huang Ying38d8b4e2017-07-06 15:37:18 -07001019 if (cluster_list_empty(&si->free_clusters))
1020 return 0;
1021
1022 idx = cluster_list_first(&si->free_clusters);
1023 offset = idx * SWAPFILE_CLUSTER;
1024 ci = lock_cluster(si, offset);
1025 alloc_cluster(si, idx);
Huang Yinge0709822017-09-06 16:22:16 -07001026 cluster_set_count_flag(ci, SWAPFILE_CLUSTER, CLUSTER_FLAG_HUGE);
Huang Ying38d8b4e2017-07-06 15:37:18 -07001027
Miaohe Lin661c7562020-12-14 19:06:07 -08001028 memset(si->swap_map + offset, SWAP_HAS_CACHE, SWAPFILE_CLUSTER);
Huang Ying38d8b4e2017-07-06 15:37:18 -07001029 unlock_cluster(ci);
1030 swap_range_alloc(si, offset, SWAPFILE_CLUSTER);
1031 *slot = swp_entry(si->type, offset);
1032
1033 return 1;
1034}
1035
1036static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx)
1037{
1038 unsigned long offset = idx * SWAPFILE_CLUSTER;
1039 struct swap_cluster_info *ci;
1040
1041 ci = lock_cluster(si, offset);
Huang Ying979aafa2018-10-26 15:03:53 -07001042 memset(si->swap_map + offset, 0, SWAPFILE_CLUSTER);
Huang Ying38d8b4e2017-07-06 15:37:18 -07001043 cluster_set_count_flag(ci, 0, 0);
1044 free_cluster(si, idx);
1045 unlock_cluster(ci);
1046 swap_range_free(si, offset, SWAPFILE_CLUSTER);
1047}
Huang Ying38d8b4e2017-07-06 15:37:18 -07001048
Huang Ying5d5e8f12018-08-21 21:52:20 -07001049int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Huang Ying5d5e8f12018-08-21 21:52:20 -07001051 unsigned long size = swap_entry_size(entry_size);
Dan Streetmanadfab832014-06-04 16:09:53 -07001052 struct swap_info_struct *si, *next;
Tim Chen36005ba2017-02-22 15:45:33 -08001053 long avail_pgs;
1054 int n_ret = 0;
Aaron Lua2468cc2017-09-06 16:24:57 -07001055 int node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Huang Ying38d8b4e2017-07-06 15:37:18 -07001057 /* Only single cluster request supported */
Huang Ying5d5e8f12018-08-21 21:52:20 -07001058 WARN_ON_ONCE(n_goal > 1 && size == SWAPFILE_CLUSTER);
Huang Ying38d8b4e2017-07-06 15:37:18 -07001059
Zhaoyang Huangb50da6e2020-12-15 20:42:23 -08001060 spin_lock(&swap_avail_lock);
1061
Huang Ying5d5e8f12018-08-21 21:52:20 -07001062 avail_pgs = atomic_long_read(&nr_swap_pages) / size;
Zhaoyang Huangb50da6e2020-12-15 20:42:23 -08001063 if (avail_pgs <= 0) {
1064 spin_unlock(&swap_avail_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07001065 goto noswap;
Zhaoyang Huangb50da6e2020-12-15 20:42:23 -08001066 }
Tim Chen36005ba2017-02-22 15:45:33 -08001067
Wei Yang08d30902020-06-01 21:48:55 -07001068 n_goal = min3((long)n_goal, (long)SWAP_BATCH, avail_pgs);
Tim Chen36005ba2017-02-22 15:45:33 -08001069
Huang Ying5d5e8f12018-08-21 21:52:20 -07001070 atomic_long_sub(n_goal * size, &nr_swap_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Dan Streetman18ab4d42014-06-04 16:09:59 -07001072start_over:
Aaron Lua2468cc2017-09-06 16:24:57 -07001073 node = numa_node_id();
1074 plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) {
Dan Streetman18ab4d42014-06-04 16:09:59 -07001075 /* requeue si to after same-priority siblings */
Aaron Lua2468cc2017-09-06 16:24:57 -07001076 plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]);
Dan Streetman18ab4d42014-06-04 16:09:59 -07001077 spin_unlock(&swap_avail_lock);
Shaohua Liec8acf22013-02-22 16:34:38 -08001078 spin_lock(&si->lock);
Dan Streetmanadfab832014-06-04 16:09:53 -07001079 if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) {
Dan Streetman18ab4d42014-06-04 16:09:59 -07001080 spin_lock(&swap_avail_lock);
Aaron Lua2468cc2017-09-06 16:24:57 -07001081 if (plist_node_empty(&si->avail_lists[node])) {
Dan Streetman18ab4d42014-06-04 16:09:59 -07001082 spin_unlock(&si->lock);
1083 goto nextsi;
1084 }
1085 WARN(!si->highest_bit,
1086 "swap_info %d in list but !highest_bit\n",
1087 si->type);
1088 WARN(!(si->flags & SWP_WRITEOK),
1089 "swap_info %d in list but !SWP_WRITEOK\n",
1090 si->type);
Aaron Lua2468cc2017-09-06 16:24:57 -07001091 __del_from_avail_list(si);
Shaohua Liec8acf22013-02-22 16:34:38 -08001092 spin_unlock(&si->lock);
Dan Streetman18ab4d42014-06-04 16:09:59 -07001093 goto nextsi;
Shaohua Liec8acf22013-02-22 16:34:38 -08001094 }
Huang Ying5d5e8f12018-08-21 21:52:20 -07001095 if (size == SWAPFILE_CLUSTER) {
Gao Xiang41663432020-09-25 21:19:01 -07001096 if (si->flags & SWP_BLKDEV)
Huang Yingf0eea182017-09-06 16:22:23 -07001097 n_ret = swap_alloc_cluster(si, swp_entries);
1098 } else
Huang Ying38d8b4e2017-07-06 15:37:18 -07001099 n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE,
1100 n_goal, swp_entries);
Shaohua Liec8acf22013-02-22 16:34:38 -08001101 spin_unlock(&si->lock);
Huang Ying5d5e8f12018-08-21 21:52:20 -07001102 if (n_ret || size == SWAPFILE_CLUSTER)
Tim Chen36005ba2017-02-22 15:45:33 -08001103 goto check_out;
Dan Streetman18ab4d42014-06-04 16:09:59 -07001104 pr_debug("scan_swap_map of si %d failed to find offset\n",
Tim Chen36005ba2017-02-22 15:45:33 -08001105 si->type);
Longlong Xia49178d42023-01-28 09:47:57 +00001106 cond_resched();
Tim Chen36005ba2017-02-22 15:45:33 -08001107
Dan Streetman18ab4d42014-06-04 16:09:59 -07001108 spin_lock(&swap_avail_lock);
1109nextsi:
Dan Streetmanadfab832014-06-04 16:09:53 -07001110 /*
1111 * if we got here, it's likely that si was almost full before,
Miaohe Linbb243f72021-06-28 19:37:00 -07001112 * and since scan_swap_map_slots() can drop the si->lock,
1113 * multiple callers probably all tried to get a page from the
1114 * same si and it filled up before we could get one; or, the si
1115 * filled up between us dropping swap_avail_lock and taking
1116 * si->lock. Since we dropped the swap_avail_lock, the
1117 * swap_avail_head list may have been modified; so if next is
1118 * still in the swap_avail_head list then try it, otherwise
1119 * start over if we have not gotten any slots.
Dan Streetmanadfab832014-06-04 16:09:53 -07001120 */
Aaron Lua2468cc2017-09-06 16:24:57 -07001121 if (plist_node_empty(&next->avail_lists[node]))
Dan Streetman18ab4d42014-06-04 16:09:59 -07001122 goto start_over;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07001124
Dan Streetman18ab4d42014-06-04 16:09:59 -07001125 spin_unlock(&swap_avail_lock);
1126
Tim Chen36005ba2017-02-22 15:45:33 -08001127check_out:
1128 if (n_ret < n_goal)
Huang Ying5d5e8f12018-08-21 21:52:20 -07001129 atomic_long_add((long)(n_goal - n_ret) * size,
Huang Ying38d8b4e2017-07-06 15:37:18 -07001130 &nr_swap_pages);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07001131noswap:
Tim Chen36005ba2017-02-22 15:45:33 -08001132 return n_ret;
1133}
1134
Miaohe Linafba72b12022-05-19 14:08:50 -07001135static struct swap_info_struct *_swap_info_get(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Hugh Dickins73c34b62009-12-14 17:58:43 -08001137 struct swap_info_struct *p;
Huang Yingeb085572019-07-11 20:55:33 -07001138 unsigned long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 if (!entry.val)
1141 goto out;
Huang Yingeb085572019-07-11 20:55:33 -07001142 p = swp_swap_info(entry);
Daniel Jordanc10d38c2019-03-05 15:48:19 -08001143 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 goto bad_nofile;
Qian Caia449bf52020-08-14 17:31:31 -07001145 if (data_race(!(p->flags & SWP_USED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 goto bad_device;
1147 offset = swp_offset(entry);
1148 if (offset >= p->max)
1149 goto bad_offset;
Miaohe Linafba72b12022-05-19 14:08:50 -07001150 if (data_race(!p->swap_map[swp_offset(entry)]))
1151 goto bad_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return p;
1153
Miaohe Linafba72b12022-05-19 14:08:50 -07001154bad_free:
1155 pr_err("%s: %s%08lx\n", __func__, Unused_offset, entry.val);
1156 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157bad_offset:
Stephen Zhangcf532fa2021-02-24 12:02:58 -08001158 pr_err("%s: %s%08lx\n", __func__, Bad_offset, entry.val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 goto out;
1160bad_device:
Stephen Zhangcf532fa2021-02-24 12:02:58 -08001161 pr_err("%s: %s%08lx\n", __func__, Unused_file, entry.val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 goto out;
1163bad_nofile:
Stephen Zhangcf532fa2021-02-24 12:02:58 -08001164 pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165out:
1166 return NULL;
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Tim Chen7c00baf2017-02-22 15:45:36 -08001169static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry,
1170 struct swap_info_struct *q)
1171{
1172 struct swap_info_struct *p;
1173
1174 p = _swap_info_get(entry);
1175
1176 if (p != q) {
1177 if (q != NULL)
1178 spin_unlock(&q->lock);
1179 if (p != NULL)
1180 spin_lock(&p->lock);
1181 }
1182 return p;
1183}
1184
Huang Yingb32d5f32018-08-21 21:52:24 -07001185static unsigned char __swap_entry_free_locked(struct swap_info_struct *p,
1186 unsigned long offset,
1187 unsigned char usage)
Huang, Ying235b6212017-02-22 15:45:22 -08001188{
Hugh Dickins8d69aae2009-12-14 17:58:45 -08001189 unsigned char count;
1190 unsigned char has_cache;
Huang, Ying235b6212017-02-22 15:45:22 -08001191
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001192 count = p->swap_map[offset];
Huang, Ying235b6212017-02-22 15:45:22 -08001193
Hugh Dickins253d5532009-12-14 17:58:44 -08001194 has_cache = count & SWAP_HAS_CACHE;
1195 count &= ~SWAP_HAS_CACHE;
1196
1197 if (usage == SWAP_HAS_CACHE) {
1198 VM_BUG_ON(!has_cache);
1199 has_cache = 0;
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001200 } else if (count == SWAP_MAP_SHMEM) {
1201 /*
1202 * Or we could insist on shmem.c using a special
1203 * swap_shmem_free() and free_shmem_swap_and_cache()...
1204 */
1205 count = 0;
Hugh Dickins570a335b2009-12-14 17:58:46 -08001206 } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
1207 if (count == COUNT_CONTINUED) {
1208 if (swap_count_continued(p, offset, count))
1209 count = SWAP_MAP_MAX | COUNT_CONTINUED;
1210 else
1211 count = SWAP_MAP_MAX;
1212 } else
1213 count--;
1214 }
Hugh Dickins253d5532009-12-14 17:58:44 -08001215
Hugh Dickins253d5532009-12-14 17:58:44 -08001216 usage = count | has_cache;
Qian Caia449bf52020-08-14 17:31:31 -07001217 if (usage)
1218 WRITE_ONCE(p->swap_map[offset], usage);
1219 else
1220 WRITE_ONCE(p->swap_map[offset], SWAP_HAS_CACHE);
Hugh Dickins253d5532009-12-14 17:58:44 -08001221
Huang Yingb32d5f32018-08-21 21:52:24 -07001222 return usage;
1223}
1224
Huang Yingeb085572019-07-11 20:55:33 -07001225/*
Huang Ying509546f2023-05-29 14:13:55 +08001226 * When we get a swap entry, if there aren't some other ways to
1227 * prevent swapoff, such as the folio in swap cache is locked, page
1228 * table lock is held, etc., the swap entry may become invalid because
1229 * of swapoff. Then, we need to enclose all swap related functions
1230 * with get_swap_device() and put_swap_device(), unless the swap
1231 * functions call get/put_swap_device() by themselves.
1232 *
Ryan Roberts1ede7f12024-03-06 14:03:56 +00001233 * Note that when only holding the PTL, swapoff might succeed immediately
1234 * after freeing a swap entry. Therefore, immediately after
1235 * __swap_entry_free(), the swap info might become stale and should not
1236 * be touched without a prior get_swap_device().
1237 *
Huang Yingeb085572019-07-11 20:55:33 -07001238 * Check whether swap entry is valid in the swap device. If so,
1239 * return pointer to swap_info_struct, and keep the swap entry valid
1240 * via preventing the swap device from being swapoff, until
1241 * put_swap_device() is called. Otherwise return NULL.
1242 *
Huang Yingeb085572019-07-11 20:55:33 -07001243 * Notice that swapoff or swapoff+swapon can still happen before the
Miaohe Lin63d86202021-06-28 19:36:46 -07001244 * percpu_ref_tryget_live() in get_swap_device() or after the
1245 * percpu_ref_put() in put_swap_device() if there isn't any other way
Huang Ying509546f2023-05-29 14:13:55 +08001246 * to prevent swapoff. The caller must be prepared for that. For
1247 * example, the following situation is possible.
Huang Yingeb085572019-07-11 20:55:33 -07001248 *
1249 * CPU1 CPU2
1250 * do_swap_page()
1251 * ... swapoff+swapon
1252 * __read_swap_cache_async()
1253 * swapcache_prepare()
1254 * __swap_duplicate()
1255 * // check swap_map
1256 * // verify PTE not changed
1257 *
1258 * In __swap_duplicate(), the swap_map need to be checked before
1259 * changing partly because the specified swap entry may be for another
1260 * swap device which has been swapoff. And in do_swap_page(), after
1261 * the page is read from the swap device, the PTE is verified not
1262 * changed with the page table locked to check whether the swap device
1263 * has been swapoff or swapoff+swapon.
1264 */
1265struct swap_info_struct *get_swap_device(swp_entry_t entry)
1266{
1267 struct swap_info_struct *si;
1268 unsigned long offset;
1269
1270 if (!entry.val)
1271 goto out;
1272 si = swp_swap_info(entry);
1273 if (!si)
1274 goto bad_nofile;
Miaohe Lin63d86202021-06-28 19:36:46 -07001275 if (!percpu_ref_tryget_live(&si->users))
1276 goto out;
1277 /*
1278 * Guarantee the si->users are checked before accessing other
1279 * fields of swap_info_struct.
1280 *
1281 * Paired with the spin_unlock() after setup_swap_info() in
1282 * enable_swap_info().
1283 */
1284 smp_rmb();
Huang Yingeb085572019-07-11 20:55:33 -07001285 offset = swp_offset(entry);
1286 if (offset >= si->max)
Miaohe Lin63d86202021-06-28 19:36:46 -07001287 goto put_out;
Huang Yingeb085572019-07-11 20:55:33 -07001288
1289 return si;
1290bad_nofile:
1291 pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val);
1292out:
1293 return NULL;
Miaohe Lin63d86202021-06-28 19:36:46 -07001294put_out:
Miaohe Lin23b230b2022-05-19 14:08:51 -07001295 pr_err("%s: %s%08lx\n", __func__, Bad_offset, entry.val);
Miaohe Lin63d86202021-06-28 19:36:46 -07001296 percpu_ref_put(&si->users);
Huang Yingeb085572019-07-11 20:55:33 -07001297 return NULL;
1298}
1299
Huang Yingb32d5f32018-08-21 21:52:24 -07001300static unsigned char __swap_entry_free(struct swap_info_struct *p,
Wei Yang33e16272020-06-01 21:49:16 -07001301 swp_entry_t entry)
Huang Yingb32d5f32018-08-21 21:52:24 -07001302{
1303 struct swap_cluster_info *ci;
1304 unsigned long offset = swp_offset(entry);
Wei Yang33e16272020-06-01 21:49:16 -07001305 unsigned char usage;
Huang Yingb32d5f32018-08-21 21:52:24 -07001306
1307 ci = lock_cluster_or_swap_info(p, offset);
Wei Yang33e16272020-06-01 21:49:16 -07001308 usage = __swap_entry_free_locked(p, offset, 1);
Tim Chen7c00baf2017-02-22 15:45:36 -08001309 unlock_cluster_or_swap_info(p, ci);
Huang Ying10e364d2018-10-26 15:03:49 -07001310 if (!usage)
1311 free_swap_slot(entry);
Huang, Ying235b6212017-02-22 15:45:22 -08001312
Hugh Dickins253d5532009-12-14 17:58:44 -08001313 return usage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Tim Chen7c00baf2017-02-22 15:45:36 -08001316static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry)
1317{
1318 struct swap_cluster_info *ci;
1319 unsigned long offset = swp_offset(entry);
1320 unsigned char count;
1321
1322 ci = lock_cluster(p, offset);
1323 count = p->swap_map[offset];
1324 VM_BUG_ON(count != SWAP_HAS_CACHE);
1325 p->swap_map[offset] = 0;
1326 dec_cluster_info_page(p, p->cluster_info, offset);
1327 unlock_cluster(ci);
1328
Huang Ying38d8b4e2017-07-06 15:37:18 -07001329 mem_cgroup_uncharge_swap(entry, 1);
1330 swap_range_free(p, offset, 1);
Tim Chen7c00baf2017-02-22 15:45:36 -08001331}
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333/*
Seth Jennings2de1a7e2013-11-12 15:07:46 -08001334 * Caller has made sure that the swap device corresponding to entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * is still around or has not been recycled.
1336 */
1337void swap_free(swp_entry_t entry)
1338{
Hugh Dickins73c34b62009-12-14 17:58:43 -08001339 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Huang, Ying235b6212017-02-22 15:45:22 -08001341 p = _swap_info_get(entry);
Huang Ying10e364d2018-10-26 15:03:49 -07001342 if (p)
Wei Yang33e16272020-06-01 21:49:16 -07001343 __swap_entry_free(p, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
1346/*
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07001347 * Called after dropping swapcache to decrease refcnt to swap entries.
1348 */
Matthew Wilcox (Oracle)4081f742022-09-02 20:46:09 +01001349void put_swap_folio(struct folio *folio, swp_entry_t entry)
Huang Ying38d8b4e2017-07-06 15:37:18 -07001350{
1351 unsigned long offset = swp_offset(entry);
1352 unsigned long idx = offset / SWAPFILE_CLUSTER;
1353 struct swap_cluster_info *ci;
1354 struct swap_info_struct *si;
1355 unsigned char *map;
Huang Yinga3aea832017-09-06 16:22:12 -07001356 unsigned int i, free_entries = 0;
1357 unsigned char val;
Matthew Wilcox (Oracle)4081f742022-09-02 20:46:09 +01001358 int size = swap_entry_size(folio_nr_pages(folio));
Huang Yingfe5266d2018-08-21 21:52:05 -07001359
Huang Yinga3aea832017-09-06 16:22:12 -07001360 si = _swap_info_get(entry);
Huang Ying38d8b4e2017-07-06 15:37:18 -07001361 if (!si)
1362 return;
1363
Huang Yingc2343d22018-08-21 21:52:29 -07001364 ci = lock_cluster_or_swap_info(si, offset);
Huang Yinga448f2d2018-08-21 21:52:17 -07001365 if (size == SWAPFILE_CLUSTER) {
Huang Yinga448f2d2018-08-21 21:52:17 -07001366 VM_BUG_ON(!cluster_is_huge(ci));
1367 map = si->swap_map + offset;
1368 for (i = 0; i < SWAPFILE_CLUSTER; i++) {
1369 val = map[i];
1370 VM_BUG_ON(!(val & SWAP_HAS_CACHE));
1371 if (val == SWAP_HAS_CACHE)
1372 free_entries++;
1373 }
Huang Yinga448f2d2018-08-21 21:52:17 -07001374 cluster_clear_huge(ci);
Huang Yinga448f2d2018-08-21 21:52:17 -07001375 if (free_entries == SWAPFILE_CLUSTER) {
Huang Yingc2343d22018-08-21 21:52:29 -07001376 unlock_cluster_or_swap_info(si, ci);
Huang Yinga448f2d2018-08-21 21:52:17 -07001377 spin_lock(&si->lock);
Huang Yinga448f2d2018-08-21 21:52:17 -07001378 mem_cgroup_uncharge_swap(entry, SWAPFILE_CLUSTER);
1379 swap_free_cluster(si, idx);
1380 spin_unlock(&si->lock);
1381 return;
1382 }
1383 }
Huang Yingc2343d22018-08-21 21:52:29 -07001384 for (i = 0; i < size; i++, entry.val++) {
1385 if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) {
1386 unlock_cluster_or_swap_info(si, ci);
1387 free_swap_slot(entry);
1388 if (i == size - 1)
1389 return;
1390 lock_cluster_or_swap_info(si, offset);
Huang Yinga3aea832017-09-06 16:22:12 -07001391 }
1392 }
Huang Yingc2343d22018-08-21 21:52:29 -07001393 unlock_cluster_or_swap_info(si, ci);
Huang Ying38d8b4e2017-07-06 15:37:18 -07001394}
Huang Ying59807682017-09-06 16:22:34 -07001395
Huang Yingfe5266d2018-08-21 21:52:05 -07001396#ifdef CONFIG_THP_SWAP
Huang Ying59807682017-09-06 16:22:34 -07001397int split_swap_cluster(swp_entry_t entry)
1398{
1399 struct swap_info_struct *si;
1400 struct swap_cluster_info *ci;
1401 unsigned long offset = swp_offset(entry);
1402
1403 si = _swap_info_get(entry);
1404 if (!si)
1405 return -EBUSY;
1406 ci = lock_cluster(si, offset);
1407 cluster_clear_huge(ci);
1408 unlock_cluster(ci);
1409 return 0;
1410}
Huang Yingfe5266d2018-08-21 21:52:05 -07001411#endif
Huang Ying38d8b4e2017-07-06 15:37:18 -07001412
Huang Ying155b5f82017-07-06 15:40:31 -07001413static int swp_entry_cmp(const void *ent1, const void *ent2)
1414{
1415 const swp_entry_t *e1 = ent1, *e2 = ent2;
1416
1417 return (int)swp_type(*e1) - (int)swp_type(*e2);
1418}
1419
Tim Chen7c00baf2017-02-22 15:45:36 -08001420void swapcache_free_entries(swp_entry_t *entries, int n)
1421{
1422 struct swap_info_struct *p, *prev;
1423 int i;
1424
1425 if (n <= 0)
1426 return;
1427
1428 prev = NULL;
1429 p = NULL;
Huang Ying155b5f82017-07-06 15:40:31 -07001430
1431 /*
1432 * Sort swap entries by swap device, so each lock is only taken once.
1433 * nr_swapfiles isn't absolutely correct, but the overhead of sort() is
1434 * so low that it isn't necessary to optimize further.
1435 */
1436 if (nr_swapfiles > 1)
1437 sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);
Tim Chen7c00baf2017-02-22 15:45:36 -08001438 for (i = 0; i < n; ++i) {
1439 p = swap_info_get_cont(entries[i], prev);
1440 if (p)
1441 swap_entry_free(p, entries[i]);
Tim Chen7c00baf2017-02-22 15:45:36 -08001442 prev = p;
1443 }
Huang, Ying235b6212017-02-22 15:45:22 -08001444 if (p)
Tim Chen7c00baf2017-02-22 15:45:36 -08001445 spin_unlock(&p->lock);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07001446}
1447
Huang Yingeb085572019-07-11 20:55:33 -07001448int __swap_count(swp_entry_t entry)
Minchan Kimaa8d22a2017-11-15 17:33:11 -08001449{
Huang Yingeb085572019-07-11 20:55:33 -07001450 struct swap_info_struct *si;
Minchan Kimaa8d22a2017-11-15 17:33:11 -08001451 pgoff_t offset = swp_offset(entry);
Huang Yingeb085572019-07-11 20:55:33 -07001452 int count = 0;
Minchan Kimaa8d22a2017-11-15 17:33:11 -08001453
Huang Yingeb085572019-07-11 20:55:33 -07001454 si = get_swap_device(entry);
1455 if (si) {
1456 count = swap_count(si->swap_map[offset]);
1457 put_swap_device(si);
1458 }
1459 return count;
Minchan Kimaa8d22a2017-11-15 17:33:11 -08001460}
1461
Matthew Wilcox (Oracle)14d01ee2022-09-02 20:46:05 +01001462/*
1463 * How many references to @entry are currently swapped out?
1464 * This does not give an exact answer when swap count is continued,
1465 * but does include the high COUNT_CONTINUED flag to allow for that.
1466 */
Huang Ying322b8afe2017-05-03 14:52:49 -07001467static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry)
1468{
Huang Ying322b8afe2017-05-03 14:52:49 -07001469 pgoff_t offset = swp_offset(entry);
1470 struct swap_cluster_info *ci;
Matthew Wilcox (Oracle)14d01ee2022-09-02 20:46:05 +01001471 int count;
Huang Ying322b8afe2017-05-03 14:52:49 -07001472
1473 ci = lock_cluster_or_swap_info(si, offset);
1474 count = swap_count(si->swap_map[offset]);
1475 unlock_cluster_or_swap_info(si, ci);
1476 return count;
1477}
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479/*
Minchan Kim8334b962015-09-08 15:00:24 -07001480 * How many references to @entry are currently swapped out?
Tim Chene8c26ab2017-02-22 15:45:29 -08001481 * This does not give an exact answer when swap count is continued,
1482 * but does include the high COUNT_CONTINUED flag to allow for that.
1483 */
1484int __swp_swapcount(swp_entry_t entry)
1485{
1486 int count = 0;
Tim Chene8c26ab2017-02-22 15:45:29 -08001487 struct swap_info_struct *si;
Tim Chene8c26ab2017-02-22 15:45:29 -08001488
Huang Yingeb085572019-07-11 20:55:33 -07001489 si = get_swap_device(entry);
1490 if (si) {
Huang Ying322b8afe2017-05-03 14:52:49 -07001491 count = swap_swapcount(si, entry);
Huang Yingeb085572019-07-11 20:55:33 -07001492 put_swap_device(si);
1493 }
Tim Chene8c26ab2017-02-22 15:45:29 -08001494 return count;
1495}
1496
1497/*
1498 * How many references to @entry are currently swapped out?
Minchan Kim8334b962015-09-08 15:00:24 -07001499 * This considers COUNT_CONTINUED so it returns exact answer.
1500 */
1501int swp_swapcount(swp_entry_t entry)
1502{
1503 int count, tmp_count, n;
1504 struct swap_info_struct *p;
Huang, Ying235b6212017-02-22 15:45:22 -08001505 struct swap_cluster_info *ci;
Minchan Kim8334b962015-09-08 15:00:24 -07001506 struct page *page;
1507 pgoff_t offset;
1508 unsigned char *map;
1509
Huang, Ying235b6212017-02-22 15:45:22 -08001510 p = _swap_info_get(entry);
Minchan Kim8334b962015-09-08 15:00:24 -07001511 if (!p)
1512 return 0;
1513
Huang, Ying235b6212017-02-22 15:45:22 -08001514 offset = swp_offset(entry);
1515
1516 ci = lock_cluster_or_swap_info(p, offset);
1517
1518 count = swap_count(p->swap_map[offset]);
Minchan Kim8334b962015-09-08 15:00:24 -07001519 if (!(count & COUNT_CONTINUED))
1520 goto out;
1521
1522 count &= ~COUNT_CONTINUED;
1523 n = SWAP_MAP_MAX + 1;
1524
Minchan Kim8334b962015-09-08 15:00:24 -07001525 page = vmalloc_to_page(p->swap_map + offset);
1526 offset &= ~PAGE_MASK;
1527 VM_BUG_ON(page_private(page) != SWP_CONTINUED);
1528
1529 do {
Geliang Tanga8ae4992016-01-14 15:20:45 -08001530 page = list_next_entry(page, lru);
Minchan Kim8334b962015-09-08 15:00:24 -07001531 map = kmap_atomic(page);
1532 tmp_count = map[offset];
1533 kunmap_atomic(map);
1534
1535 count += (tmp_count & ~COUNT_CONTINUED) * n;
1536 n *= (SWAP_CONT_MAX + 1);
1537 } while (tmp_count & COUNT_CONTINUED);
1538out:
Huang, Ying235b6212017-02-22 15:45:22 -08001539 unlock_cluster_or_swap_info(p, ci);
Minchan Kim8334b962015-09-08 15:00:24 -07001540 return count;
1541}
1542
Huang Yinge0709822017-09-06 16:22:16 -07001543static bool swap_page_trans_huge_swapped(struct swap_info_struct *si,
1544 swp_entry_t entry)
1545{
1546 struct swap_cluster_info *ci;
1547 unsigned char *map = si->swap_map;
1548 unsigned long roffset = swp_offset(entry);
1549 unsigned long offset = round_down(roffset, SWAPFILE_CLUSTER);
1550 int i;
1551 bool ret = false;
1552
1553 ci = lock_cluster_or_swap_info(si, offset);
1554 if (!ci || !cluster_is_huge(ci)) {
Huang Yingafa47112018-08-21 21:52:09 -07001555 if (swap_count(map[roffset]))
Huang Yinge0709822017-09-06 16:22:16 -07001556 ret = true;
1557 goto unlock_out;
1558 }
1559 for (i = 0; i < SWAPFILE_CLUSTER; i++) {
Huang Yingafa47112018-08-21 21:52:09 -07001560 if (swap_count(map[offset + i])) {
Huang Yinge0709822017-09-06 16:22:16 -07001561 ret = true;
1562 break;
1563 }
1564 }
1565unlock_out:
1566 unlock_cluster_or_swap_info(si, ci);
1567 return ret;
1568}
1569
Matthew Wilcox (Oracle)2397f782022-06-17 18:50:10 +01001570static bool folio_swapped(struct folio *folio)
Huang Yinge0709822017-09-06 16:22:16 -07001571{
Matthew Wilcox (Oracle)14d01ee2022-09-02 20:46:05 +01001572 swp_entry_t entry = folio_swap_entry(folio);
1573 struct swap_info_struct *si = _swap_info_get(entry);
1574
1575 if (!si)
1576 return false;
Huang Yinge0709822017-09-06 16:22:16 -07001577
Matthew Wilcox (Oracle)2397f782022-06-17 18:50:10 +01001578 if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!folio_test_large(folio)))
Matthew Wilcox (Oracle)14d01ee2022-09-02 20:46:05 +01001579 return swap_swapcount(si, entry) != 0;
Huang Yinge0709822017-09-06 16:22:16 -07001580
Matthew Wilcox (Oracle)14d01ee2022-09-02 20:46:05 +01001581 return swap_page_trans_huge_swapped(si, entry);
Huang Yinge0709822017-09-06 16:22:16 -07001582}
Huang Yingba3c4ce2017-09-06 16:22:19 -07001583
Matthew Wilcox (Oracle)bdb0ed52022-09-02 20:46:06 +01001584/**
1585 * folio_free_swap() - Free the swap space used for this folio.
1586 * @folio: The folio to remove.
1587 *
1588 * If swap is getting full, or if there are no more mappings of this folio,
1589 * then call folio_free_swap to free its swap space.
1590 *
1591 * Return: true if we were able to release the swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 */
Matthew Wilcox (Oracle)bdb0ed52022-09-02 20:46:06 +01001593bool folio_free_swap(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Matthew Wilcox (Oracle)2397f782022-06-17 18:50:10 +01001595 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Matthew Wilcox (Oracle)2397f782022-06-17 18:50:10 +01001597 if (!folio_test_swapcache(folio))
Matthew Wilcox (Oracle)bdb0ed52022-09-02 20:46:06 +01001598 return false;
Matthew Wilcox (Oracle)2397f782022-06-17 18:50:10 +01001599 if (folio_test_writeback(folio))
Matthew Wilcox (Oracle)bdb0ed52022-09-02 20:46:06 +01001600 return false;
Matthew Wilcox (Oracle)2397f782022-06-17 18:50:10 +01001601 if (folio_swapped(folio))
Matthew Wilcox (Oracle)bdb0ed52022-09-02 20:46:06 +01001602 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -07001604 /*
1605 * Once hibernation has begun to create its image of memory,
Matthew Wilcox (Oracle)bdb0ed52022-09-02 20:46:06 +01001606 * there's a danger that one of the calls to folio_free_swap()
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -07001607 * - most probably a call from __try_to_reclaim_swap() while
1608 * hibernation is allocating its own swap pages for the image,
1609 * but conceivably even a call from memory reclaim - will free
Matthew Wilcox (Oracle)bdb0ed52022-09-02 20:46:06 +01001610 * the swap from a folio which has already been recorded in the
1611 * image as a clean swapcache folio, and then reuse its swap for
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -07001612 * another page of the image. On waking from hibernation, the
Matthew Wilcox (Oracle)bdb0ed52022-09-02 20:46:06 +01001613 * original folio might be freed under memory pressure, then
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -07001614 * later read back in from swap, now with the wrong data.
1615 *
Seth Jennings2de1a7e2013-11-12 15:07:46 -08001616 * Hibernation suspends storage while it is writing the image
Mel Gormanf90ac392012-01-10 15:07:15 -08001617 * to disk so check that here.
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -07001618 */
Mel Gormanf90ac392012-01-10 15:07:15 -08001619 if (pm_suspended_storage())
Matthew Wilcox (Oracle)bdb0ed52022-09-02 20:46:06 +01001620 return false;
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -07001621
Matthew Wilcox (Oracle)75fa68a2022-06-17 18:50:19 +01001622 delete_from_swap_cache(folio);
Matthew Wilcox (Oracle)2397f782022-06-17 18:50:10 +01001623 folio_set_dirty(folio);
Matthew Wilcox (Oracle)bdb0ed52022-09-02 20:46:06 +01001624 return true;
Rik van Riel68a223942008-10-18 20:26:23 -07001625}
1626
1627/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 * Free the swap entry like above, but also try to
1629 * free the page cache entry if it is the last user.
1630 */
Hugh Dickins2509ef22009-01-06 14:40:10 -08001631int free_swap_and_cache(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Hugh Dickins2509ef22009-01-06 14:40:10 -08001633 struct swap_info_struct *p;
Tim Chen7c00baf2017-02-22 15:45:36 -08001634 unsigned char count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Andi Kleena7420aa2009-09-16 11:50:05 +02001636 if (non_swap_entry(entry))
Hugh Dickins2509ef22009-01-06 14:40:10 -08001637 return 1;
Christoph Lameter06972122006-06-23 02:03:35 -07001638
Ryan Roberts1ede7f12024-03-06 14:03:56 +00001639 p = get_swap_device(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (p) {
Ryan Roberts1ede7f12024-03-06 14:03:56 +00001641 if (WARN_ON(data_race(!p->swap_map[swp_offset(entry)]))) {
1642 put_swap_device(p);
1643 return 0;
1644 }
1645
Wei Yang33e16272020-06-01 21:49:16 -07001646 count = __swap_entry_free(p, entry);
Huang Yinge0709822017-09-06 16:22:16 -07001647 if (count == SWAP_HAS_CACHE &&
Huang Yingbcd49e82018-10-26 15:03:46 -07001648 !swap_page_trans_huge_swapped(p, entry))
1649 __try_to_reclaim_swap(p, swp_offset(entry),
1650 TTRS_UNMAPPED | TTRS_FULL);
Ryan Roberts1ede7f12024-03-06 14:03:56 +00001651 put_swap_device(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
Hugh Dickins2509ef22009-01-06 14:40:10 -08001653 return p != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +02001656#ifdef CONFIG_HIBERNATION
Miaohe Linbb243f72021-06-28 19:37:00 -07001657
1658swp_entry_t get_swap_page_of_type(int type)
1659{
1660 struct swap_info_struct *si = swap_type_to_swap_info(type);
1661 swp_entry_t entry = {0};
1662
1663 if (!si)
1664 goto fail;
1665
1666 /* This is called for allocating swap entry, not cache */
1667 spin_lock(&si->lock);
1668 if ((si->flags & SWP_WRITEOK) && scan_swap_map_slots(si, 1, 1, &entry))
1669 atomic_long_dec(&nr_swap_pages);
1670 spin_unlock(&si->lock);
1671fail:
1672 return entry;
1673}
1674
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001675/*
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001676 * Find the swap type that corresponds to given device (if any).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001677 *
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001678 * @offset - number of the PAGE_SIZE-sized block of the device, starting
1679 * from 0, in which the swap header is expected to be located.
1680 *
1681 * This is needed for the suspend to disk (aka swsusp).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001682 */
Christoph Hellwig21bd9002020-09-21 09:19:56 +02001683int swap_type_of(dev_t device, sector_t offset)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001684{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001685 int type;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001686
Christoph Hellwig21bd9002020-09-21 09:19:56 +02001687 if (!device)
1688 return -1;
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001689
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001690 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001691 for (type = 0; type < nr_swapfiles; type++) {
1692 struct swap_info_struct *sis = swap_info[type];
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001693
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001694 if (!(sis->flags & SWP_WRITEOK))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001695 continue;
Rafael J. Wysockib6b5bce2006-08-27 01:23:25 -07001696
Christoph Hellwig21bd9002020-09-21 09:19:56 +02001697 if (device == sis->bdev->bd_dev) {
Aaron Lu4efaceb2019-07-11 20:55:41 -07001698 struct swap_extent *se = first_se(sis);
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001699
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001700 if (se->start_block == offset) {
1701 spin_unlock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001702 return type;
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001703 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001704 }
1705 }
1706 spin_unlock(&swap_lock);
Christoph Hellwig21bd9002020-09-21 09:19:56 +02001707 return -ENODEV;
1708}
Rafael J. Wysocki915bae92006-12-06 20:34:07 -08001709
Christoph Hellwig21bd9002020-09-21 09:19:56 +02001710int find_first_swap(dev_t *device)
1711{
1712 int type;
1713
1714 spin_lock(&swap_lock);
1715 for (type = 0; type < nr_swapfiles; type++) {
1716 struct swap_info_struct *sis = swap_info[type];
1717
1718 if (!(sis->flags & SWP_WRITEOK))
1719 continue;
1720 *device = sis->bdev->bd_dev;
1721 spin_unlock(&swap_lock);
1722 return type;
1723 }
1724 spin_unlock(&swap_lock);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001725 return -ENODEV;
1726}
1727
1728/*
Hugh Dickins73c34b62009-12-14 17:58:43 -08001729 * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
1730 * corresponding to given index in swap_info (swap type).
1731 */
1732sector_t swapdev_block(int type, pgoff_t offset)
1733{
Daniel Jordanc10d38c2019-03-05 15:48:19 -08001734 struct swap_info_struct *si = swap_type_to_swap_info(type);
Christoph Hellwigf8850562021-02-09 18:14:19 +01001735 struct swap_extent *se;
Hugh Dickins73c34b62009-12-14 17:58:43 -08001736
Daniel Jordanc10d38c2019-03-05 15:48:19 -08001737 if (!si || !(si->flags & SWP_WRITEOK))
Hugh Dickins73c34b62009-12-14 17:58:43 -08001738 return 0;
Christoph Hellwigf8850562021-02-09 18:14:19 +01001739 se = offset_to_swap_extent(si, offset);
1740 return se->start_block + (offset - se->start_page);
Hugh Dickins73c34b62009-12-14 17:58:43 -08001741}
1742
1743/*
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001744 * Return either the total number of swap pages of given type, or the number
1745 * of free pages of that type (depending on @free)
1746 *
1747 * This is needed for software suspend
1748 */
1749unsigned int count_swap_pages(int type, int free)
1750{
1751 unsigned int n = 0;
1752
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001753 spin_lock(&swap_lock);
1754 if ((unsigned int)type < nr_swapfiles) {
1755 struct swap_info_struct *sis = swap_info[type];
1756
Shaohua Liec8acf22013-02-22 16:34:38 -08001757 spin_lock(&sis->lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001758 if (sis->flags & SWP_WRITEOK) {
1759 n = sis->pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001760 if (free)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001761 n -= sis->inuse_pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001762 }
Shaohua Liec8acf22013-02-22 16:34:38 -08001763 spin_unlock(&sis->lock);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001764 }
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001765 spin_unlock(&swap_lock);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001766 return n;
1767}
Hugh Dickins73c34b62009-12-14 17:58:43 -08001768#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001769
Hugh Dickins9f8bdb32016-01-15 16:57:34 -08001770static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte)
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001771{
Peter Xu099dd682021-06-15 18:23:16 -07001772 return pte_same(pte_swp_clear_flags(pte), swp_pte);
Cyrill Gorcunov179ef712013-08-13 16:00:49 -07001773}
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775/*
Hugh Dickins72866f62005-10-29 18:15:55 -07001776 * No need to decide whether this PTE shares the swap entry with others,
1777 * just let do_wp_page work it out if a write is requested later - to
1778 * force COW, vm_page_prot omits write permission from any private vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 */
Hugh Dickins044d66c2008-02-07 00:14:04 -08001780static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
Matthew Wilcox (Oracle)f102cd82022-09-02 20:46:32 +01001781 unsigned long addr, swp_entry_t entry, struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Matthew Wilcox (Oracle)f102cd82022-09-02 20:46:32 +01001783 struct page *page = folio_file_page(folio, swp_offset(entry));
Hugh Dickins9e16b7f2013-02-22 16:36:09 -08001784 struct page *swapcache;
Hugh Dickins044d66c2008-02-07 00:14:04 -08001785 spinlock_t *ptl;
Miaohe Lin14a762dd2022-05-19 20:50:27 +08001786 pte_t *pte, new_pte;
Hugh Dickins044d66c2008-02-07 00:14:04 -08001787 int ret = 1;
1788
Hugh Dickins9e16b7f2013-02-22 16:36:09 -08001789 swapcache = page;
1790 page = ksm_might_need_to_copy(page, vma, addr);
1791 if (unlikely(!page))
1792 return -ENOMEM;
1793
Hugh Dickins044d66c2008-02-07 00:14:04 -08001794 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Hugh Dickins9f8bdb32016-01-15 16:57:34 -08001795 if (unlikely(!pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) {
Hugh Dickins044d66c2008-02-07 00:14:04 -08001796 ret = 0;
1797 goto out;
1798 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001799
Miaohe Lin9f186f92022-05-19 20:50:26 +08001800 if (unlikely(!PageUptodate(page))) {
1801 pte_t pteval;
1802
1803 dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
1804 pteval = swp_entry_to_pte(make_swapin_error_entry(page));
1805 set_pte_at(vma->vm_mm, addr, pte, pteval);
1806 swap_free(entry);
1807 ret = 0;
1808 goto out;
1809 }
1810
Peter Collingbourne131714e2023-05-18 14:29:14 -07001811 /*
1812 * Some architectures may have to restore extra metadata to the page
1813 * when reading from swap. This metadata may be indexed by swap entry
1814 * so this must be called before swap_free().
1815 */
1816 arch_swap_restore(entry, page_folio(page));
1817
David Hildenbrand78fbe902022-05-09 18:20:44 -07001818 /* See do_swap_page() */
1819 BUG_ON(!PageAnon(page) && PageMappedToDisk(page));
1820 BUG_ON(PageAnon(page) && PageAnonExclusive(page));
1821
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -08001822 dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -08001823 inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 get_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07001825 if (page == swapcache) {
David Hildenbrand1493a192022-05-09 18:20:45 -07001826 rmap_t rmap_flags = RMAP_NONE;
1827
1828 /*
1829 * See do_swap_page(): PageWriteback() would be problematic.
1830 * However, we do a wait_on_page_writeback() just before this
1831 * call and have the page locked.
1832 */
1833 VM_BUG_ON_PAGE(PageWriteback(page), page);
1834 if (pte_swp_exclusive(*pte))
1835 rmap_flags |= RMAP_EXCLUSIVE;
1836
1837 page_add_anon_rmap(page, vma, addr, rmap_flags);
Johannes Weiner00501b52014-08-08 14:19:20 -07001838 } else { /* ksm created a completely new copy */
David Hildenbrand40f2bbf2022-05-09 18:20:43 -07001839 page_add_new_anon_rmap(page, vma, addr);
Joonsoo Kimb5181542020-08-11 18:30:40 -07001840 lru_cache_add_inactive_or_unevictable(page, vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001841 }
Miaohe Lin14a762dd2022-05-19 20:50:27 +08001842 new_pte = pte_mkold(mk_pte(page, vma->vm_page_prot));
1843 if (pte_swp_soft_dirty(*pte))
1844 new_pte = pte_mksoft_dirty(new_pte);
1845 if (pte_swp_uffd_wp(*pte))
1846 new_pte = pte_mkuffd_wp(new_pte);
1847 set_pte_at(vma->vm_mm, addr, pte, new_pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 swap_free(entry);
Hugh Dickins044d66c2008-02-07 00:14:04 -08001849out:
1850 pte_unmap_unlock(pte, ptl);
Hugh Dickins9e16b7f2013-02-22 16:36:09 -08001851 if (page != swapcache) {
1852 unlock_page(page);
1853 put_page(page);
1854 }
Hugh Dickins044d66c2008-02-07 00:14:04 -08001855 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
1858static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001859 unsigned long addr, unsigned long end,
Christoph Hellwig10a9c492022-01-21 22:14:57 -08001860 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001862 swp_entry_t entry;
Hugh Dickins705e87c2005-10-29 18:16:27 -07001863 pte_t *pte;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001864 struct swap_info_struct *si;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001865 int ret = 0;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001866 volatile unsigned char *swap_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001868 si = swap_info[type];
Hugh Dickins044d66c2008-02-07 00:14:04 -08001869 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 do {
Matthew Wilcox (Oracle)f102cd82022-09-02 20:46:32 +01001871 struct folio *folio;
1872 unsigned long offset;
1873
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001874 if (!is_swap_pte(*pte))
1875 continue;
1876
1877 entry = pte_to_swp_entry(*pte);
1878 if (swp_type(entry) != type)
1879 continue;
1880
1881 offset = swp_offset(entry);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001882 pte_unmap(pte);
1883 swap_map = &si->swap_map[offset];
Matthew Wilcox (Oracle)f102cd82022-09-02 20:46:32 +01001884 folio = swap_cache_get_folio(entry, vma, addr);
1885 if (!folio) {
1886 struct page *page;
Will Deacon8c63ca52021-01-14 15:42:14 +00001887 struct vm_fault vmf = {
1888 .vma = vma,
1889 .address = addr,
Nadav Amit824ddc62022-03-22 14:45:32 -07001890 .real_address = addr,
Will Deacon8c63ca52021-01-14 15:42:14 +00001891 .pmd = pmd,
1892 };
1893
Andrea Righiebc59512020-06-01 21:48:43 -07001894 page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
1895 &vmf);
Matthew Wilcox (Oracle)f102cd82022-09-02 20:46:32 +01001896 if (page)
1897 folio = page_folio(page);
Andrea Righiebc59512020-06-01 21:48:43 -07001898 }
Matthew Wilcox (Oracle)f102cd82022-09-02 20:46:32 +01001899 if (!folio) {
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001900 if (*swap_map == 0 || *swap_map == SWAP_MAP_BAD)
1901 goto try_next;
1902 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001904
Matthew Wilcox (Oracle)f102cd82022-09-02 20:46:32 +01001905 folio_lock(folio);
1906 folio_wait_writeback(folio);
1907 ret = unuse_pte(vma, pmd, addr, entry, folio);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001908 if (ret < 0) {
Matthew Wilcox (Oracle)f102cd82022-09-02 20:46:32 +01001909 folio_unlock(folio);
1910 folio_put(folio);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001911 goto out;
1912 }
1913
Matthew Wilcox (Oracle)f102cd82022-09-02 20:46:32 +01001914 folio_free_swap(folio);
1915 folio_unlock(folio);
1916 folio_put(folio);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001917try_next:
1918 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickins044d66c2008-02-07 00:14:04 -08001920 pte_unmap(pte - 1);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001921
1922 ret = 0;
Hugh Dickins044d66c2008-02-07 00:14:04 -08001923out:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001924 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
1927static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
1928 unsigned long addr, unsigned long end,
Christoph Hellwig10a9c492022-01-21 22:14:57 -08001929 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 pmd_t *pmd;
1932 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001933 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 pmd = pmd_offset(pud, addr);
1936 do {
Hugh Dickinsdc644a02016-12-12 16:44:44 -08001937 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 next = pmd_addr_end(addr, end);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07001939 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 continue;
Christoph Hellwig10a9c492022-01-21 22:14:57 -08001941 ret = unuse_pte_range(vma, pmd, addr, next, type);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001942 if (ret)
1943 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 } while (pmd++, addr = next, addr != end);
1945 return 0;
1946}
1947
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001948static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 unsigned long addr, unsigned long end,
Christoph Hellwig10a9c492022-01-21 22:14:57 -08001950 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 pud_t *pud;
1953 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001954 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001956 pud = pud_offset(p4d, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 do {
1958 next = pud_addr_end(addr, end);
1959 if (pud_none_or_clear_bad(pud))
1960 continue;
Christoph Hellwig10a9c492022-01-21 22:14:57 -08001961 ret = unuse_pmd_range(vma, pud, addr, next, type);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001962 if (ret)
1963 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 } while (pud++, addr = next, addr != end);
1965 return 0;
1966}
1967
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001968static inline int unuse_p4d_range(struct vm_area_struct *vma, pgd_t *pgd,
1969 unsigned long addr, unsigned long end,
Christoph Hellwig10a9c492022-01-21 22:14:57 -08001970 unsigned int type)
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001971{
1972 p4d_t *p4d;
1973 unsigned long next;
1974 int ret;
1975
1976 p4d = p4d_offset(pgd, addr);
1977 do {
1978 next = p4d_addr_end(addr, end);
1979 if (p4d_none_or_clear_bad(p4d))
1980 continue;
Christoph Hellwig10a9c492022-01-21 22:14:57 -08001981 ret = unuse_pud_range(vma, p4d, addr, next, type);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03001982 if (ret)
1983 return ret;
1984 } while (p4d++, addr = next, addr != end);
1985 return 0;
1986}
1987
Christoph Hellwig10a9c492022-01-21 22:14:57 -08001988static int unuse_vma(struct vm_area_struct *vma, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
1990 pgd_t *pgd;
1991 unsigned long addr, end, next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001992 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001994 addr = vma->vm_start;
1995 end = vma->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 pgd = pgd_offset(vma->vm_mm, addr);
1998 do {
1999 next = pgd_addr_end(addr, end);
2000 if (pgd_none_or_clear_bad(pgd))
2001 continue;
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002002 ret = unuse_p4d_range(vma, pgd, addr, next, type);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002003 if (ret)
2004 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 } while (pgd++, addr = next, addr != end);
2006 return 0;
2007}
2008
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002009static int unuse_mm(struct mm_struct *mm, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011 struct vm_area_struct *vma;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002012 int ret = 0;
Liam R. Howlett208c09d2022-09-06 19:49:04 +00002013 VMA_ITERATOR(vmi, mm, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002015 mmap_read_lock(mm);
Liam R. Howlett208c09d2022-09-06 19:49:04 +00002016 for_each_vma(vmi, vma) {
Liu Shixin6ec0fe32024-10-15 09:45:21 +08002017 if (vma->anon_vma && !is_vm_hugetlb_page(vma)) {
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002018 ret = unuse_vma(vma, type);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002019 if (ret)
2020 break;
2021 }
Liam R. Howlett208c09d2022-09-06 19:49:04 +00002022
Hugh Dickinsdc644a02016-12-12 16:44:44 -08002023 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002025 mmap_read_unlock(mm);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002026 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
2029/*
Miaohe Lin3c3115a2022-05-19 14:08:52 -07002030 * Scan swap_map from current position to next entry still in use.
2031 * Return 0 if there are no inuse entries after prev till end of
2032 * the map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 */
Hugh Dickins6eb396d2005-09-03 15:54:35 -07002034static unsigned int find_next_to_unuse(struct swap_info_struct *si,
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002035 unsigned int prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002037 unsigned int i;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08002038 unsigned char count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 /*
Hugh Dickins5d337b92005-09-03 15:54:41 -07002041 * No need for swap_lock here: we're just looking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 * for whether an entry is in use, not modifying it; false
2043 * hits are okay, and sys_swapoff() has already prevented new
Hugh Dickins5d337b92005-09-03 15:54:41 -07002044 * allocations from this area (while holding swap_lock).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 */
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002046 for (i = prev + 1; i < si->max; i++) {
Jason Low4db0c3c2015-04-15 16:14:08 -07002047 count = READ_ONCE(si->swap_map[i]);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002048 if (count && swap_count(count) != SWAP_MAP_BAD)
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002049 break;
Hugh Dickinsdc644a02016-12-12 16:44:44 -08002050 if ((i % LATENCY_LIMIT) == 0)
2051 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002053
2054 if (i == si->max)
2055 i = 0;
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return i;
2058}
2059
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002060static int try_to_unuse(unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002062 struct mm_struct *prev_mm;
2063 struct mm_struct *mm;
2064 struct list_head *p;
2065 int retval = 0;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002066 struct swap_info_struct *si = swap_info[type];
Matthew Wilcox (Oracle)000085b2022-09-02 20:46:30 +01002067 struct folio *folio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 swp_entry_t entry;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002069 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Qian Cai21820942020-04-01 21:06:13 -07002071 if (!READ_ONCE(si->inuse_pages))
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002074retry:
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002075 retval = shmem_unuse(type);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002076 if (retval)
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002077 return retval;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002078
2079 prev_mm = &init_mm;
2080 mmget(prev_mm);
2081
2082 spin_lock(&mmlist_lock);
2083 p = &init_mm.mmlist;
Qian Cai21820942020-04-01 21:06:13 -07002084 while (READ_ONCE(si->inuse_pages) &&
Hugh Dickins64165b12019-04-18 17:50:09 -07002085 !signal_pending(current) &&
2086 (p = p->next) != &init_mm.mmlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002088 mm = list_entry(p, struct mm_struct, mmlist);
2089 if (!mmget_not_zero(mm))
Hugh Dickinsaaa46862009-12-14 17:58:47 -08002090 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002091 spin_unlock(&mmlist_lock);
2092 mmput(prev_mm);
2093 prev_mm = mm;
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002094 retval = unuse_mm(mm, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (retval) {
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002096 mmput(prev_mm);
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002097 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099
2100 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 * Make sure that we aren't completely killing
2102 * interactive performance.
2103 */
2104 cond_resched();
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002105 spin_lock(&mmlist_lock);
2106 }
2107 spin_unlock(&mmlist_lock);
2108
2109 mmput(prev_mm);
2110
2111 i = 0;
Qian Cai21820942020-04-01 21:06:13 -07002112 while (READ_ONCE(si->inuse_pages) &&
Hugh Dickins64165b12019-04-18 17:50:09 -07002113 !signal_pending(current) &&
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002114 (i = find_next_to_unuse(si, i)) != 0) {
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002115
2116 entry = swp_entry(type, i);
Matthew Wilcox (Oracle)000085b2022-09-02 20:46:30 +01002117 folio = filemap_get_folio(swap_address_space(entry), i);
2118 if (!folio)
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002119 continue;
2120
2121 /*
Matthew Wilcox (Oracle)000085b2022-09-02 20:46:30 +01002122 * It is conceivable that a racing task removed this folio from
2123 * swap cache just before we acquired the page lock. The folio
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002124 * might even be back in swap cache on another swap area. But
Matthew Wilcox (Oracle)000085b2022-09-02 20:46:30 +01002125 * that is okay, folio_free_swap() only removes stale folios.
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002126 */
Matthew Wilcox (Oracle)000085b2022-09-02 20:46:30 +01002127 folio_lock(folio);
2128 folio_wait_writeback(folio);
2129 folio_free_swap(folio);
2130 folio_unlock(folio);
2131 folio_put(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002134 /*
2135 * Lets check again to see if there are still swap entries in the map.
2136 * If yes, we would need to do retry the unuse logic again.
2137 * Under global memory pressure, swap entries can be reinserted back
2138 * into process space after the mmlist loop above passes over them.
Hugh Dickinsdd862de2019-04-18 17:50:02 -07002139 *
Matthew Wilcox (Oracle)e2e3fdc2022-05-12 20:23:02 -07002140 * Limit the number of retries? No: when mmget_not_zero()
2141 * above fails, that mm is likely to be freeing swap from
2142 * exit_mmap(), which proceeds at its own independent pace;
2143 * and even shmem_writepage() could have been preempted after
2144 * folio_alloc_swap(), temporarily hiding that swap. It's easy
2145 * and robust (though cpu-intensive) just to keep retrying.
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08002146 */
Qian Cai21820942020-04-01 21:06:13 -07002147 if (READ_ONCE(si->inuse_pages)) {
Hugh Dickins64165b12019-04-18 17:50:09 -07002148 if (!signal_pending(current))
2149 goto retry;
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002150 return -EINTR;
Hugh Dickins64165b12019-04-18 17:50:09 -07002151 }
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002152
2153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
2156/*
Hugh Dickins5d337b92005-09-03 15:54:41 -07002157 * After a successful try_to_unuse, if no swap is now in use, we know
2158 * we can empty the mmlist. swap_lock must be held on entry and exit.
2159 * Note that mmlist_lock nests inside swap_lock, and an mm must be
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 * added to the mmlist just after page_duplicate - before would be racy.
2161 */
2162static void drain_mmlist(void)
2163{
2164 struct list_head *p, *next;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002165 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002167 for (type = 0; type < nr_swapfiles; type++)
2168 if (swap_info[type]->inuse_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return;
2170 spin_lock(&mmlist_lock);
2171 list_for_each_safe(p, next, &init_mm.mmlist)
2172 list_del_init(p);
2173 spin_unlock(&mmlist_lock);
2174}
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176/*
2177 * Free all of a swapdev's extent information
2178 */
2179static void destroy_swap_extents(struct swap_info_struct *sis)
2180{
Aaron Lu4efaceb2019-07-11 20:55:41 -07002181 while (!RB_EMPTY_ROOT(&sis->swap_extent_root)) {
2182 struct rb_node *rb = sis->swap_extent_root.rb_node;
2183 struct swap_extent *se = rb_entry(rb, struct swap_extent, rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Aaron Lu4efaceb2019-07-11 20:55:41 -07002185 rb_erase(rb, &sis->swap_extent_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 kfree(se);
2187 }
Mel Gorman62c230b2012-07-31 16:44:55 -07002188
Omar Sandovalbc4ae272018-10-26 15:10:51 -07002189 if (sis->flags & SWP_ACTIVATED) {
Mel Gorman62c230b2012-07-31 16:44:55 -07002190 struct file *swap_file = sis->swap_file;
2191 struct address_space *mapping = swap_file->f_mapping;
2192
Omar Sandovalbc4ae272018-10-26 15:10:51 -07002193 sis->flags &= ~SWP_ACTIVATED;
2194 if (mapping->a_ops->swap_deactivate)
2195 mapping->a_ops->swap_deactivate(swap_file);
Mel Gorman62c230b2012-07-31 16:44:55 -07002196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
2199/*
2200 * Add a block range (and the corresponding page range) into this swapdev's
Aaron Lu4efaceb2019-07-11 20:55:41 -07002201 * extent tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 *
Hugh Dickins11d31882005-09-03 15:54:34 -07002203 * This function rather assumes that it is called in ascending page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 */
Mel Gormana509bc12012-07-31 16:44:57 -07002205int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
2207 unsigned long nr_pages, sector_t start_block)
2208{
Aaron Lu4efaceb2019-07-11 20:55:41 -07002209 struct rb_node **link = &sis->swap_extent_root.rb_node, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 struct swap_extent *se;
2211 struct swap_extent *new_se;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Aaron Lu4efaceb2019-07-11 20:55:41 -07002213 /*
2214 * place the new node at the right most since the
2215 * function is called in ascending page order.
2216 */
2217 while (*link) {
2218 parent = *link;
2219 link = &parent->rb_right;
2220 }
2221
2222 if (parent) {
2223 se = rb_entry(parent, struct swap_extent, rb_node);
Hugh Dickins11d31882005-09-03 15:54:34 -07002224 BUG_ON(se->start_page + se->nr_pages != start_page);
2225 if (se->start_block + se->nr_pages == start_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /* Merge it */
2227 se->nr_pages += nr_pages;
2228 return 0;
2229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
2231
Aaron Lu4efaceb2019-07-11 20:55:41 -07002232 /* No merge, insert a new extent. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
2234 if (new_se == NULL)
2235 return -ENOMEM;
2236 new_se->start_page = start_page;
2237 new_se->nr_pages = nr_pages;
2238 new_se->start_block = start_block;
2239
Aaron Lu4efaceb2019-07-11 20:55:41 -07002240 rb_link_node(&new_se->rb_node, parent, link);
2241 rb_insert_color(&new_se->rb_node, &sis->swap_extent_root);
Hugh Dickins53092a72005-09-03 15:54:34 -07002242 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
Omar Sandovalaa8aa8a2018-10-26 15:10:55 -07002244EXPORT_SYMBOL_GPL(add_swap_extent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246/*
2247 * A `swap extent' is a simple thing which maps a contiguous range of pages
Miaohe Linff351f42022-05-19 14:08:52 -07002248 * onto a contiguous range of disk blocks. A rbtree of swap extents is
2249 * built at swapon time and is then used at swap_writepage/swap_readpage
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 * time for locating where on disk a page belongs.
2251 *
2252 * If the swapfile is an S_ISBLK block device, a single extent is installed.
2253 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
2254 * swap files identically.
2255 *
2256 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
Miaohe Linff351f42022-05-19 14:08:52 -07002257 * extent rbtree operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 * swapfiles are handled *identically* after swapon time.
2259 *
2260 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
Miaohe Linff351f42022-05-19 14:08:52 -07002261 * and will parse them into a rbtree, in PAGE_SIZE chunks. If some stray
2262 * blocks are found which do not fall within the PAGE_SIZE alignment
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 * requirements, they are simply tossed out - we will never use those blocks
2264 * for swapping.
2265 *
Darrick J. Wong16380452019-08-20 07:55:16 -07002266 * For all swap devices we set S_SWAPFILE across the life of the swapon. This
2267 * prevents users from writing to the swap device, which will corrupt memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 *
2269 * The amount of disk space which a single swap extent represents varies.
2270 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
Miaohe Linff351f42022-05-19 14:08:52 -07002271 * extents in the rbtree. - akpm.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 */
Hugh Dickins53092a72005-09-03 15:54:34 -07002273static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Mel Gorman62c230b2012-07-31 16:44:55 -07002275 struct file *swap_file = sis->swap_file;
2276 struct address_space *mapping = swap_file->f_mapping;
2277 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 int ret;
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (S_ISBLK(inode->i_mode)) {
2281 ret = add_swap_extent(sis, 0, sis->max, 0);
Hugh Dickins53092a72005-09-03 15:54:34 -07002282 *span = sis->pages;
Mel Gormana509bc12012-07-31 16:44:57 -07002283 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285
Mel Gorman62c230b2012-07-31 16:44:55 -07002286 if (mapping->a_ops->swap_activate) {
Mel Gormana509bc12012-07-31 16:44:57 -07002287 ret = mapping->a_ops->swap_activate(sis, swap_file, span);
NeilBrown4b60c0f2022-05-09 18:20:48 -07002288 if (ret < 0)
2289 return ret;
2290 sis->flags |= SWP_ACTIVATED;
NeilBrowne1209d32022-05-09 18:20:48 -07002291 if ((sis->flags & SWP_FS_OPS) &&
2292 sio_pool_init() != 0) {
2293 destroy_swap_extents(sis);
2294 return -ENOMEM;
Mel Gorman62c230b2012-07-31 16:44:55 -07002295 }
Mel Gormana509bc12012-07-31 16:44:57 -07002296 return ret;
Mel Gorman62c230b2012-07-31 16:44:55 -07002297 }
2298
Mel Gormana509bc12012-07-31 16:44:57 -07002299 return generic_swapfile_activate(sis, swap_file, span);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
Aaron Lua2468cc2017-09-06 16:24:57 -07002302static int swap_node(struct swap_info_struct *p)
2303{
2304 struct block_device *bdev;
2305
2306 if (p->bdev)
2307 bdev = p->bdev;
2308 else
2309 bdev = p->swap_file->f_inode->i_sb->s_bdev;
2310
2311 return bdev ? bdev->bd_disk->node_id : NUMA_NO_NODE;
2312}
2313
Huang Yingeb085572019-07-11 20:55:33 -07002314static void setup_swap_info(struct swap_info_struct *p, int prio,
2315 unsigned char *swap_map,
2316 struct swap_cluster_info *cluster_info)
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002317{
Aaron Lua2468cc2017-09-06 16:24:57 -07002318 int i;
2319
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002320 if (prio >= 0)
2321 p->prio = prio;
2322 else
2323 p->prio = --least_priority;
Dan Streetman18ab4d42014-06-04 16:09:59 -07002324 /*
2325 * the plist prio is negated because plist ordering is
2326 * low-to-high, while swap ordering is high-to-low
2327 */
2328 p->list.prio = -p->prio;
Aaron Lua2468cc2017-09-06 16:24:57 -07002329 for_each_node(i) {
2330 if (p->prio >= 0)
2331 p->avail_lists[i].prio = -p->prio;
2332 else {
2333 if (swap_node(p) == i)
2334 p->avail_lists[i].prio = 1;
2335 else
2336 p->avail_lists[i].prio = -p->prio;
2337 }
2338 }
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002339 p->swap_map = swap_map;
Shaohua Li2a8f9442013-09-11 14:20:28 -07002340 p->cluster_info = cluster_info;
Huang Yingeb085572019-07-11 20:55:33 -07002341}
2342
2343static void _enable_swap_info(struct swap_info_struct *p)
2344{
Miaohe Lin63d86202021-06-28 19:36:46 -07002345 p->flags |= SWP_WRITEOK;
Shaohua Liec8acf22013-02-22 16:34:38 -08002346 atomic_long_add(p->pages, &nr_swap_pages);
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002347 total_swap_pages += p->pages;
2348
Dan Streetmanadfab832014-06-04 16:09:53 -07002349 assert_spin_locked(&swap_lock);
Dan Streetmanadfab832014-06-04 16:09:53 -07002350 /*
Dan Streetman18ab4d42014-06-04 16:09:59 -07002351 * both lists are plists, and thus priority ordered.
2352 * swap_active_head needs to be priority ordered for swapoff(),
2353 * which on removal of any swap_info_struct with an auto-assigned
2354 * (i.e. negative) priority increments the auto-assigned priority
2355 * of any lower-priority swap_info_structs.
Matthew Wilcox (Oracle)e2e3fdc2022-05-12 20:23:02 -07002356 * swap_avail_head needs to be priority ordered for folio_alloc_swap(),
Dan Streetman18ab4d42014-06-04 16:09:59 -07002357 * which allocates swap pages from the highest available priority
2358 * swap_info_struct.
Dan Streetmanadfab832014-06-04 16:09:53 -07002359 */
Dan Streetman18ab4d42014-06-04 16:09:59 -07002360 plist_add(&p->list, &swap_active_head);
Aaron Lua2468cc2017-09-06 16:24:57 -07002361 add_to_avail_list(p);
Cesar Eduardo Barroscf0cac02012-12-11 16:01:13 -08002362}
2363
2364static void enable_swap_info(struct swap_info_struct *p, int prio,
2365 unsigned char *swap_map,
Shaohua Li2a8f9442013-09-11 14:20:28 -07002366 struct swap_cluster_info *cluster_info,
Cesar Eduardo Barroscf0cac02012-12-11 16:01:13 -08002367 unsigned long *frontswap_map)
2368{
Christoph Hellwig1cf53c82022-01-21 22:14:51 -08002369 if (IS_ENABLED(CONFIG_FRONTSWAP))
2370 frontswap_init(p->type, frontswap_map);
Cesar Eduardo Barroscf0cac02012-12-11 16:01:13 -08002371 spin_lock(&swap_lock);
Shaohua Liec8acf22013-02-22 16:34:38 -08002372 spin_lock(&p->lock);
Huang Yingeb085572019-07-11 20:55:33 -07002373 setup_swap_info(p, prio, swap_map, cluster_info);
2374 spin_unlock(&p->lock);
2375 spin_unlock(&swap_lock);
2376 /*
Miaohe Lin63d86202021-06-28 19:36:46 -07002377 * Finished initializing swap device, now it's safe to reference it.
Huang Yingeb085572019-07-11 20:55:33 -07002378 */
Miaohe Lin63d86202021-06-28 19:36:46 -07002379 percpu_ref_resurrect(&p->users);
Huang Yingeb085572019-07-11 20:55:33 -07002380 spin_lock(&swap_lock);
2381 spin_lock(&p->lock);
2382 _enable_swap_info(p);
Shaohua Liec8acf22013-02-22 16:34:38 -08002383 spin_unlock(&p->lock);
Cesar Eduardo Barroscf0cac02012-12-11 16:01:13 -08002384 spin_unlock(&swap_lock);
2385}
2386
2387static void reinsert_swap_info(struct swap_info_struct *p)
2388{
2389 spin_lock(&swap_lock);
Shaohua Liec8acf22013-02-22 16:34:38 -08002390 spin_lock(&p->lock);
Huang Yingeb085572019-07-11 20:55:33 -07002391 setup_swap_info(p, p->prio, p->swap_map, p->cluster_info);
2392 _enable_swap_info(p);
Shaohua Liec8acf22013-02-22 16:34:38 -08002393 spin_unlock(&p->lock);
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002394 spin_unlock(&swap_lock);
2395}
2396
Tim Chen67afa382017-02-22 15:45:39 -08002397bool has_usable_swap(void)
2398{
2399 bool ret = true;
2400
2401 spin_lock(&swap_lock);
2402 if (plist_head_empty(&swap_active_head))
2403 ret = false;
2404 spin_unlock(&swap_lock);
2405 return ret;
2406}
2407
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01002408SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
Hugh Dickins73c34b62009-12-14 17:58:43 -08002410 struct swap_info_struct *p = NULL;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08002411 unsigned char *swap_map;
Shaohua Li2a8f9442013-09-11 14:20:28 -07002412 struct swap_cluster_info *cluster_info;
Minchan Kim4f898492013-04-30 15:26:54 -07002413 unsigned long *frontswap_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 struct file *swap_file, *victim;
2415 struct address_space *mapping;
2416 struct inode *inode;
Jeff Layton91a27b22012-10-10 15:25:28 -04002417 struct filename *pathname;
Dan Streetmanadfab832014-06-04 16:09:53 -07002418 int err, found = 0;
Krzysztof Kozlowski5b808a22013-10-16 13:47:06 -07002419 unsigned int old_block_size;
Shreyas K Kd7e1f4f2023-05-31 10:16:20 +05302420 bool hibernation_swap = false;
Hugh Dickins886bb7e2009-01-06 14:39:48 -08002421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (!capable(CAP_SYS_ADMIN))
2423 return -EPERM;
2424
Al Viro191c5422012-02-13 03:58:52 +00002425 BUG_ON(!current->mm);
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 pathname = getname(specialfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if (IS_ERR(pathname))
Xiaotian Fengf58b59c2012-11-16 14:14:55 -08002429 return PTR_ERR(pathname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Jeff Layton669abf42012-10-10 16:43:10 -04002431 victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 err = PTR_ERR(victim);
2433 if (IS_ERR(victim))
2434 goto out;
2435
2436 mapping = victim->f_mapping;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002437 spin_lock(&swap_lock);
Dan Streetman18ab4d42014-06-04 16:09:59 -07002438 plist_for_each_entry(p, &swap_active_head, list) {
Hugh Dickins22c6f8f2009-01-06 14:39:48 -08002439 if (p->flags & SWP_WRITEOK) {
Dan Streetmanadfab832014-06-04 16:09:53 -07002440 if (p->swap_file->f_mapping == mapping) {
2441 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 break;
Dan Streetmanadfab832014-06-04 16:09:53 -07002443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
Dan Streetmanadfab832014-06-04 16:09:53 -07002446 if (!found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 err = -EINVAL;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002448 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 goto out_dput;
2450 }
Al Viro191c5422012-02-13 03:58:52 +00002451 if (!security_vm_enough_memory_mm(current->mm, p->pages))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 vm_unacct_memory(p->pages);
2453 else {
2454 err = -ENOMEM;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002455 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 goto out_dput;
2457 }
Shaohua Liec8acf22013-02-22 16:34:38 -08002458 spin_lock(&p->lock);
Rongwei Wang85cc1182023-04-04 23:47:16 +08002459 del_from_avail_list(p);
Hugh Dickins78ecba02008-07-23 21:28:23 -07002460 if (p->prio < 0) {
Dan Streetmanadfab832014-06-04 16:09:53 -07002461 struct swap_info_struct *si = p;
Aaron Lua2468cc2017-09-06 16:24:57 -07002462 int nid;
Dan Streetmanadfab832014-06-04 16:09:53 -07002463
Dan Streetman18ab4d42014-06-04 16:09:59 -07002464 plist_for_each_entry_continue(si, &swap_active_head, list) {
Dan Streetmanadfab832014-06-04 16:09:53 -07002465 si->prio++;
Dan Streetman18ab4d42014-06-04 16:09:59 -07002466 si->list.prio--;
Aaron Lua2468cc2017-09-06 16:24:57 -07002467 for_each_node(nid) {
2468 if (si->avail_lists[nid].prio != 1)
2469 si->avail_lists[nid].prio--;
2470 }
Dan Streetmanadfab832014-06-04 16:09:53 -07002471 }
Hugh Dickins78ecba02008-07-23 21:28:23 -07002472 least_priority++;
2473 }
Dan Streetman18ab4d42014-06-04 16:09:59 -07002474 plist_del(&p->list, &swap_active_head);
Shaohua Liec8acf22013-02-22 16:34:38 -08002475 atomic_long_sub(p->pages, &nr_swap_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 total_swap_pages -= p->pages;
2477 p->flags &= ~SWP_WRITEOK;
Shaohua Liec8acf22013-02-22 16:34:38 -08002478 spin_unlock(&p->lock);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002479 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07002480
Tim Chen039939a2017-02-22 15:45:43 -08002481 disable_swap_slots_cache_lock();
2482
David Rientjese1e12d22012-12-11 16:02:56 -08002483 set_current_oom_origin();
Christoph Hellwig10a9c492022-01-21 22:14:57 -08002484 err = try_to_unuse(p->type);
David Rientjese1e12d22012-12-11 16:02:56 -08002485 clear_current_oom_origin();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (err) {
2488 /* re-insert swap space back into swap_list */
Cesar Eduardo Barroscf0cac02012-12-11 16:01:13 -08002489 reinsert_swap_info(p);
Tim Chen039939a2017-02-22 15:45:43 -08002490 reenable_swap_slots_cache_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 goto out_dput;
2492 }
Hugh Dickins52b7efdb2005-09-03 15:54:39 -07002493
Tim Chen039939a2017-02-22 15:45:43 -08002494 reenable_swap_slots_cache_unlock();
2495
Huang Yingeb085572019-07-11 20:55:33 -07002496 /*
Miaohe Lin63d86202021-06-28 19:36:46 -07002497 * Wait for swap operations protected by get/put_swap_device()
2498 * to complete.
2499 *
2500 * We need synchronize_rcu() here to protect the accessing to
2501 * the swap cache data structure.
Huang Yingeb085572019-07-11 20:55:33 -07002502 */
Miaohe Lin63d86202021-06-28 19:36:46 -07002503 percpu_ref_kill(&p->users);
Huang Yingeb085572019-07-11 20:55:33 -07002504 synchronize_rcu();
Miaohe Lin63d86202021-06-28 19:36:46 -07002505 wait_for_completion(&p->comp);
Huang Yingeb085572019-07-11 20:55:33 -07002506
Shaohua Li815c2c52013-09-11 14:20:30 -07002507 flush_work(&p->discard_work);
2508
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07002509 destroy_swap_extents(p);
Shreyas K Kd7e1f4f2023-05-31 10:16:20 +05302510
2511 trace_android_vh_check_hibernation_swap(p->bdev, &hibernation_swap);
2512
Hugh Dickins570a335b2009-12-14 17:58:46 -08002513 if (p->flags & SWP_CONTINUED)
2514 free_swap_count_continuations(p);
2515
Shreyas K Kd7e1f4f2023-05-31 10:16:20 +05302516 if (!p->bdev || hibernation_swap ||
2517 !bdev_nonrot(p->bdev))
Huang Ying81a02982017-09-06 16:24:43 -07002518 atomic_dec(&nr_rotate_swap);
2519
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002520 mutex_lock(&swapon_mutex);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002521 spin_lock(&swap_lock);
Shaohua Liec8acf22013-02-22 16:34:38 -08002522 spin_lock(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 drain_mmlist();
Hugh Dickins5d337b92005-09-03 15:54:41 -07002524
Miaohe Linbb243f72021-06-28 19:37:00 -07002525 /* wait for anyone still in scan_swap_map_slots */
Hugh Dickins5d337b92005-09-03 15:54:41 -07002526 p->highest_bit = 0; /* cuts scans short */
2527 while (p->flags >= SWP_SCANNING) {
Shaohua Liec8acf22013-02-22 16:34:38 -08002528 spin_unlock(&p->lock);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002529 spin_unlock(&swap_lock);
Nishanth Aravamudan13e4b572005-09-10 00:27:25 -07002530 schedule_timeout_uninterruptible(1);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002531 spin_lock(&swap_lock);
Shaohua Liec8acf22013-02-22 16:34:38 -08002532 spin_lock(&p->lock);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002533 }
2534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 swap_file = p->swap_file;
Krzysztof Kozlowski5b808a22013-10-16 13:47:06 -07002536 old_block_size = p->old_block_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 p->swap_file = NULL;
2538 p->max = 0;
2539 swap_map = p->swap_map;
2540 p->swap_map = NULL;
Shaohua Li2a8f9442013-09-11 14:20:28 -07002541 cluster_info = p->cluster_info;
2542 p->cluster_info = NULL;
Minchan Kim4f898492013-04-30 15:26:54 -07002543 frontswap_map = frontswap_map_get(p);
Shaohua Liec8acf22013-02-22 16:34:38 -08002544 spin_unlock(&p->lock);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002545 spin_unlock(&swap_lock);
Steven Price8a848022020-05-13 16:37:49 +01002546 arch_swap_invalidate_area(p->type);
Dan Streetmanadfab832014-06-04 16:09:53 -07002547 frontswap_invalidate_area(p->type);
Krzysztof Kozlowski58e97ba2013-11-12 15:07:47 -08002548 frontswap_map_set(p, NULL);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002549 mutex_unlock(&swapon_mutex);
Shaohua Liebc2a1a62013-09-11 14:20:32 -07002550 free_percpu(p->percpu_cluster);
2551 p->percpu_cluster = NULL;
Huang Ying49070582020-06-01 21:49:22 -07002552 free_percpu(p->cluster_next_cpu);
2553 p->cluster_next_cpu = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 vfree(swap_map);
Huang Ying54f180d2017-05-08 15:57:40 -07002555 kvfree(cluster_info);
2556 kvfree(frontswap_map);
Seth Jennings2de1a7e2013-11-12 15:07:46 -08002557 /* Destroy swap account information */
Dan Streetmanadfab832014-06-04 16:09:53 -07002558 swap_cgroup_swapoff(p->type);
Huang, Ying4b3ef9d2017-02-22 15:45:26 -08002559 exit_swap_address_space(p->type);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08002560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 inode = mapping->host;
2562 if (S_ISBLK(inode->i_mode)) {
2563 struct block_device *bdev = I_BDEV(inode);
Darrick J. Wong16380452019-08-20 07:55:16 -07002564
Krzysztof Kozlowski5b808a22013-10-16 13:47:06 -07002565 set_blocksize(bdev, old_block_size);
Tejun Heoe525fd82010-11-13 11:55:17 +01002566 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
Darrick J. Wong16380452019-08-20 07:55:16 -07002568
2569 inode_lock(inode);
2570 inode->i_flags &= ~S_SWAPFILE;
2571 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 filp_close(swap_file, NULL);
Weijie Yangf893ab412014-02-06 12:04:23 -08002573
2574 /*
2575 * Clear the SWP_USED flag after all resources are freed so that swapon
2576 * can reuse this swap_info in alloc_swap_info() safely. It is ok to
2577 * not hold p->lock after we cleared its SWP_WRITEOK.
2578 */
2579 spin_lock(&swap_lock);
2580 p->flags = 0;
2581 spin_unlock(&swap_lock);
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 err = 0;
Kay Sievers66d7dd52010-10-26 14:22:06 -07002584 atomic_inc(&proc_poll_event);
2585 wake_up_interruptible(&proc_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587out_dput:
2588 filp_close(victim, NULL);
2589out:
Xiaotian Fengf58b59c2012-11-16 14:14:55 -08002590 putname(pathname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return err;
2592}
2593
2594#ifdef CONFIG_PROC_FS
Al Viro9dd95742017-07-03 00:42:43 -04002595static __poll_t swaps_poll(struct file *file, poll_table *wait)
Kay Sievers66d7dd52010-10-26 14:22:06 -07002596{
Kay Sieversf1514632011-07-12 20:48:39 +02002597 struct seq_file *seq = file->private_data;
Kay Sievers66d7dd52010-10-26 14:22:06 -07002598
2599 poll_wait(file, &proc_poll_wait, wait);
2600
Kay Sieversf1514632011-07-12 20:48:39 +02002601 if (seq->poll_event != atomic_read(&proc_poll_event)) {
2602 seq->poll_event = atomic_read(&proc_poll_event);
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002603 return EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
Kay Sievers66d7dd52010-10-26 14:22:06 -07002604 }
2605
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002606 return EPOLLIN | EPOLLRDNORM;
Kay Sievers66d7dd52010-10-26 14:22:06 -07002607}
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609/* iterator */
2610static void *swap_start(struct seq_file *swap, loff_t *pos)
2611{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002612 struct swap_info_struct *si;
2613 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 loff_t l = *pos;
2615
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002616 mutex_lock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08002618 if (!l)
2619 return SEQ_START_TOKEN;
2620
Daniel Jordanc10d38c2019-03-05 15:48:19 -08002621 for (type = 0; (si = swap_type_to_swap_info(type)); type++) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002622 if (!(si->flags & SWP_USED) || !si->swap_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 continue;
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08002624 if (!--l)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002625 return si;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
2627
2628 return NULL;
2629}
2630
2631static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
2632{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002633 struct swap_info_struct *si = v;
2634 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08002636 if (v == SEQ_START_TOKEN)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002637 type = 0;
2638 else
2639 type = si->type + 1;
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08002640
Vasily Averin10c8d692020-01-30 22:13:39 -08002641 ++(*pos);
Daniel Jordanc10d38c2019-03-05 15:48:19 -08002642 for (; (si = swap_type_to_swap_info(type)); type++) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002643 if (!(si->flags & SWP_USED) || !si->swap_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 continue;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002645 return si;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 }
2647
2648 return NULL;
2649}
2650
2651static void swap_stop(struct seq_file *swap, void *v)
2652{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002653 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654}
2655
2656static int swap_show(struct seq_file *swap, void *v)
2657{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002658 struct swap_info_struct *si = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 struct file *file;
2660 int len;
Rafael Aquini642929a2021-11-05 13:37:22 -07002661 unsigned long bytes, inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002663 if (si == SEQ_START_TOKEN) {
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07002664 seq_puts(swap, "Filename\t\t\t\tType\t\tSize\t\tUsed\t\tPriority\n");
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08002665 return 0;
2666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Randy Dunlap6f793942020-06-01 21:49:26 -07002668 bytes = si->pages << (PAGE_SHIFT - 10);
Miaohe Linc8945302022-06-08 22:40:30 +08002669 inuse = READ_ONCE(si->inuse_pages) << (PAGE_SHIFT - 10);
Randy Dunlap6f793942020-06-01 21:49:26 -07002670
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002671 file = si->swap_file;
Miklos Szeredi2726d562015-06-19 10:30:28 +02002672 len = seq_file_path(swap, file, " \t\n\\");
Rafael Aquini642929a2021-11-05 13:37:22 -07002673 seq_printf(swap, "%*s%s\t%lu\t%s%lu\t%s%d\n",
Hugh Dickins886bb7e2009-01-06 14:39:48 -08002674 len < 40 ? 40 - len : 1, " ",
Al Viro496ad9a2013-01-23 17:07:38 -05002675 S_ISBLK(file_inode(file)->i_mode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 "partition" : "file\t",
Randy Dunlap6f793942020-06-01 21:49:26 -07002677 bytes, bytes < 10000000 ? "\t" : "",
2678 inuse, inuse < 10000000 ? "\t" : "",
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002679 si->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 return 0;
2681}
2682
Helge Deller15ad7cd2006-12-06 20:40:36 -08002683static const struct seq_operations swaps_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 .start = swap_start,
2685 .next = swap_next,
2686 .stop = swap_stop,
2687 .show = swap_show
2688};
2689
2690static int swaps_open(struct inode *inode, struct file *file)
2691{
Kay Sieversf1514632011-07-12 20:48:39 +02002692 struct seq_file *seq;
Kay Sievers66d7dd52010-10-26 14:22:06 -07002693 int ret;
2694
Kay Sievers66d7dd52010-10-26 14:22:06 -07002695 ret = seq_open(file, &swaps_op);
Kay Sieversf1514632011-07-12 20:48:39 +02002696 if (ret)
Kay Sievers66d7dd52010-10-26 14:22:06 -07002697 return ret;
Kay Sievers66d7dd52010-10-26 14:22:06 -07002698
Kay Sieversf1514632011-07-12 20:48:39 +02002699 seq = file->private_data;
2700 seq->poll_event = atomic_read(&proc_poll_event);
2701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
Alexey Dobriyan97a32532020-02-03 17:37:17 -08002704static const struct proc_ops swaps_proc_ops = {
Alexey Dobriyand919b332020-04-06 20:09:01 -07002705 .proc_flags = PROC_ENTRY_PERMANENT,
Alexey Dobriyan97a32532020-02-03 17:37:17 -08002706 .proc_open = swaps_open,
2707 .proc_read = seq_read,
2708 .proc_lseek = seq_lseek,
2709 .proc_release = seq_release,
2710 .proc_poll = swaps_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711};
2712
2713static int __init procswaps_init(void)
2714{
Alexey Dobriyan97a32532020-02-03 17:37:17 -08002715 proc_create("swaps", 0, NULL, &swaps_proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return 0;
2717}
2718__initcall(procswaps_init);
2719#endif /* CONFIG_PROC_FS */
2720
Jan Beulich17963162008-12-16 11:35:24 +00002721#ifdef MAX_SWAPFILES_CHECK
2722static int __init max_swapfiles_check(void)
2723{
2724 MAX_SWAPFILES_CHECK();
2725 return 0;
2726}
2727late_initcall(max_swapfiles_check);
2728#endif
2729
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002730static struct swap_info_struct *alloc_swap_info(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
Hugh Dickins73c34b62009-12-14 17:58:43 -08002732 struct swap_info_struct *p;
Qian Caib11a76b2020-12-05 22:14:55 -08002733 struct swap_info_struct *defer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 unsigned int type;
Aaron Lua2468cc2017-09-06 16:24:57 -07002735 int i;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002736
Gustavo A. R. Silva96008742019-03-05 15:49:31 -08002737 p = kvzalloc(struct_size(p, avail_lists, nr_node_ids), GFP_KERNEL);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002738 if (!p)
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002739 return ERR_PTR(-ENOMEM);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002740
Miaohe Lin63d86202021-06-28 19:36:46 -07002741 if (percpu_ref_init(&p->users, swap_users_ref_free,
2742 PERCPU_REF_INIT_DEAD, GFP_KERNEL)) {
2743 kvfree(p);
2744 return ERR_PTR(-ENOMEM);
2745 }
2746
Hugh Dickins5d337b92005-09-03 15:54:41 -07002747 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002748 for (type = 0; type < nr_swapfiles; type++) {
2749 if (!(swap_info[type]->flags & SWP_USED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 break;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002751 }
Christoph Lameter06972122006-06-23 02:03:35 -07002752 if (type >= MAX_SWAPFILES) {
Hugh Dickins5d337b92005-09-03 15:54:41 -07002753 spin_unlock(&swap_lock);
Miaohe Lin63d86202021-06-28 19:36:46 -07002754 percpu_ref_exit(&p->users);
Vasily Averin873d7bc2018-11-16 15:08:11 -08002755 kvfree(p);
Cesar Eduardo Barros730c0582011-03-22 16:33:19 -07002756 return ERR_PTR(-EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 }
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002758 if (type >= nr_swapfiles) {
2759 p->type = type;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002760 /*
Huang Yinga4b45112021-06-28 19:37:09 -07002761 * Publish the swap_info_struct after initializing it.
2762 * Note that kvzalloc() above zeroes all its fields.
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002763 */
Huang Yinga4b45112021-06-28 19:37:09 -07002764 smp_store_release(&swap_info[type], p); /* rcu_assign_pointer() */
2765 nr_swapfiles++;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002766 } else {
Qian Caib11a76b2020-12-05 22:14:55 -08002767 defer = p;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002768 p = swap_info[type];
2769 /*
2770 * Do not memset this entry: a racing procfs swap_next()
2771 * would be relying on p->type to remain valid.
2772 */
2773 }
Aaron Lu4efaceb2019-07-11 20:55:41 -07002774 p->swap_extent_root = RB_ROOT;
Dan Streetman18ab4d42014-06-04 16:09:59 -07002775 plist_node_init(&p->list, 0);
Aaron Lua2468cc2017-09-06 16:24:57 -07002776 for_each_node(i)
2777 plist_node_init(&p->avail_lists[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 p->flags = SWP_USED;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002779 spin_unlock(&swap_lock);
Miaohe Lin63d86202021-06-28 19:36:46 -07002780 if (defer) {
2781 percpu_ref_exit(&defer->users);
2782 kvfree(defer);
2783 }
Shaohua Liec8acf22013-02-22 16:34:38 -08002784 spin_lock_init(&p->lock);
Huang Ying2628bd62017-11-02 15:59:50 -07002785 spin_lock_init(&p->cont_lock);
Miaohe Lin63d86202021-06-28 19:36:46 -07002786 init_completion(&p->comp);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002787
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002788 return p;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002789}
2790
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07002791static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
2792{
2793 int error;
2794
2795 if (S_ISBLK(inode->i_mode)) {
Christoph Hellwigef16e1d2020-09-21 09:19:54 +02002796 p->bdev = blkdev_get_by_dev(inode->i_rdev,
Hugh Dickins6f179af2015-08-17 17:34:27 -07002797 FMODE_READ | FMODE_WRITE | FMODE_EXCL, p);
Christoph Hellwigef16e1d2020-09-21 09:19:54 +02002798 if (IS_ERR(p->bdev)) {
2799 error = PTR_ERR(p->bdev);
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07002800 p->bdev = NULL;
Hugh Dickins6f179af2015-08-17 17:34:27 -07002801 return error;
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07002802 }
2803 p->old_block_size = block_size(p->bdev);
2804 error = set_blocksize(p->bdev, PAGE_SIZE);
2805 if (error < 0)
Cesar Eduardo Barros87ade722011-03-22 16:33:27 -07002806 return error;
Naohiro Aota12d29662019-11-30 17:49:56 -08002807 /*
2808 * Zoned block devices contain zones that have a sequential
2809 * write only restriction. Hence zoned block devices are not
2810 * suitable for swapping. Disallow them here.
2811 */
Christoph Hellwig9964e672022-04-15 06:52:41 +02002812 if (bdev_is_zoned(p->bdev))
Naohiro Aota12d29662019-11-30 17:49:56 -08002813 return -EINVAL;
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07002814 p->flags |= SWP_BLKDEV;
2815 } else if (S_ISREG(inode->i_mode)) {
2816 p->bdev = inode->i_sb->s_bdev;
Darrick J. Wong16380452019-08-20 07:55:16 -07002817 }
2818
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07002819 return 0;
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07002820}
2821
Andi Kleen377eeaa2018-06-13 15:48:28 -07002822
2823/*
2824 * Find out how many pages are allowed for a single swap device. There
2825 * are two limiting factors:
2826 * 1) the number of bits for the swap offset in the swp_entry_t type, and
2827 * 2) the number of bits in the swap pte, as defined by the different
2828 * architectures.
2829 *
2830 * In order to find the largest possible bit mask, a swap entry with
2831 * swap type 0 and swap offset ~0UL is created, encoded to a swap pte,
2832 * decoded to a swp_entry_t again, and finally the swap offset is
2833 * extracted.
2834 *
2835 * This will mask all the bits from the initial ~0UL mask that can't
2836 * be encoded in either the swp_entry_t or the architecture definition
2837 * of a swap pte.
2838 */
2839unsigned long generic_max_swapfile_size(void)
2840{
2841 return swp_offset(pte_to_swp_entry(
2842 swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
2843}
2844
2845/* Can be overridden by an architecture for additional checks. */
Peter Xube45a492022-08-11 12:13:30 -04002846__weak unsigned long arch_max_swapfile_size(void)
Andi Kleen377eeaa2018-06-13 15:48:28 -07002847{
2848 return generic_max_swapfile_size();
2849}
2850
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002851static unsigned long read_swap_header(struct swap_info_struct *p,
2852 union swap_header *swap_header,
2853 struct inode *inode)
2854{
2855 int i;
2856 unsigned long maxpages;
2857 unsigned long swapfilepages;
Raymond Jenningsd6bbbd22013-09-11 14:20:16 -07002858 unsigned long last_page;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002859
2860 if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
Andrew Morton465c47f2013-09-11 14:20:17 -07002861 pr_err("Unable to find swap-space signature\n");
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07002862 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002863 }
2864
Zhen Lei041711c2021-06-30 18:53:17 -07002865 /* swap partition endianness hack... */
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002866 if (swab32(swap_header->info.version) == 1) {
2867 swab32s(&swap_header->info.version);
2868 swab32s(&swap_header->info.last_page);
2869 swab32s(&swap_header->info.nr_badpages);
Jann Horndd111be2016-11-10 10:46:19 -08002870 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
2871 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002872 for (i = 0; i < swap_header->info.nr_badpages; i++)
2873 swab32s(&swap_header->info.badpages[i]);
2874 }
2875 /* Check the swap header's sub-version */
2876 if (swap_header->info.version != 1) {
Andrew Morton465c47f2013-09-11 14:20:17 -07002877 pr_warn("Unable to handle swap header version %d\n",
2878 swap_header->info.version);
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07002879 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002880 }
2881
2882 p->lowest_bit = 1;
2883 p->cluster_next = 1;
2884 p->cluster_nr = 0;
2885
Peter Xube45a492022-08-11 12:13:30 -04002886 maxpages = swapfile_maximum_size;
Raymond Jenningsd6bbbd22013-09-11 14:20:16 -07002887 last_page = swap_header->info.last_page;
Tom Abrahama06ad632018-04-10 16:29:48 -07002888 if (!last_page) {
2889 pr_warn("Empty swap-file\n");
2890 return 0;
2891 }
Raymond Jenningsd6bbbd22013-09-11 14:20:16 -07002892 if (last_page > maxpages) {
Andrew Morton465c47f2013-09-11 14:20:17 -07002893 pr_warn("Truncating oversized swap area, only using %luk out of %luk\n",
Raymond Jenningsd6bbbd22013-09-11 14:20:16 -07002894 maxpages << (PAGE_SHIFT - 10),
2895 last_page << (PAGE_SHIFT - 10));
2896 }
2897 if (maxpages > last_page) {
2898 maxpages = last_page + 1;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002899 /* p->max is an unsigned int: don't overflow it */
2900 if ((unsigned int)maxpages == 0)
2901 maxpages = UINT_MAX;
2902 }
2903 p->highest_bit = maxpages - 1;
2904
2905 if (!maxpages)
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07002906 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002907 swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
2908 if (swapfilepages && maxpages > swapfilepages) {
Andrew Morton465c47f2013-09-11 14:20:17 -07002909 pr_warn("Swap area shorter than signature indicates\n");
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07002910 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002911 }
2912 if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07002913 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002914 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07002915 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002916
2917 return maxpages;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002918}
2919
Huang, Ying4b3ef9d2017-02-22 15:45:26 -08002920#define SWAP_CLUSTER_INFO_COLS \
Huang, Ying235b6212017-02-22 15:45:22 -08002921 DIV_ROUND_UP(L1_CACHE_BYTES, sizeof(struct swap_cluster_info))
Huang, Ying4b3ef9d2017-02-22 15:45:26 -08002922#define SWAP_CLUSTER_SPACE_COLS \
2923 DIV_ROUND_UP(SWAP_ADDRESS_SPACE_PAGES, SWAPFILE_CLUSTER)
2924#define SWAP_CLUSTER_COLS \
2925 max_t(unsigned int, SWAP_CLUSTER_INFO_COLS, SWAP_CLUSTER_SPACE_COLS)
Huang, Ying235b6212017-02-22 15:45:22 -08002926
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002927static int setup_swap_map_and_extents(struct swap_info_struct *p,
2928 union swap_header *swap_header,
2929 unsigned char *swap_map,
Shaohua Li2a8f9442013-09-11 14:20:28 -07002930 struct swap_cluster_info *cluster_info,
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002931 unsigned long maxpages,
2932 sector_t *span)
2933{
Huang, Ying235b6212017-02-22 15:45:22 -08002934 unsigned int j, k;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002935 unsigned int nr_good_pages;
2936 int nr_extents;
Shaohua Li2a8f9442013-09-11 14:20:28 -07002937 unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
Huang, Ying235b6212017-02-22 15:45:22 -08002938 unsigned long col = p->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_COLS;
2939 unsigned long i, idx;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002940
2941 nr_good_pages = maxpages - 1; /* omit header page */
2942
Huang Ying6b534912016-10-07 16:58:42 -07002943 cluster_list_init(&p->free_clusters);
2944 cluster_list_init(&p->discard_clusters);
Shaohua Li2a8f9442013-09-11 14:20:28 -07002945
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002946 for (i = 0; i < swap_header->info.nr_badpages; i++) {
2947 unsigned int page_nr = swap_header->info.badpages[i];
Cesar Eduardo Barrosbdb8e3f2011-03-22 16:33:33 -07002948 if (page_nr == 0 || page_nr > swap_header->info.last_page)
2949 return -EINVAL;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002950 if (page_nr < maxpages) {
2951 swap_map[page_nr] = SWAP_MAP_BAD;
2952 nr_good_pages--;
Shaohua Li2a8f9442013-09-11 14:20:28 -07002953 /*
2954 * Haven't marked the cluster free yet, no list
2955 * operation involved
2956 */
2957 inc_cluster_info_page(p, cluster_info, page_nr);
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002958 }
2959 }
2960
Shaohua Li2a8f9442013-09-11 14:20:28 -07002961 /* Haven't marked the cluster free yet, no list operation involved */
2962 for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++)
2963 inc_cluster_info_page(p, cluster_info, i);
2964
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002965 if (nr_good_pages) {
2966 swap_map[0] = SWAP_MAP_BAD;
Shaohua Li2a8f9442013-09-11 14:20:28 -07002967 /*
2968 * Not mark the cluster free yet, no list
2969 * operation involved
2970 */
2971 inc_cluster_info_page(p, cluster_info, 0);
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002972 p->max = maxpages;
2973 p->pages = nr_good_pages;
2974 nr_extents = setup_swap_extents(p, span);
Cesar Eduardo Barrosbdb8e3f2011-03-22 16:33:33 -07002975 if (nr_extents < 0)
2976 return nr_extents;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002977 nr_good_pages = p->pages;
2978 }
2979 if (!nr_good_pages) {
Andrew Morton465c47f2013-09-11 14:20:17 -07002980 pr_warn("Empty swap-file\n");
Cesar Eduardo Barrosbdb8e3f2011-03-22 16:33:33 -07002981 return -EINVAL;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002982 }
2983
Shaohua Li2a8f9442013-09-11 14:20:28 -07002984 if (!cluster_info)
2985 return nr_extents;
2986
Huang, Ying235b6212017-02-22 15:45:22 -08002987
Huang, Ying4b3ef9d2017-02-22 15:45:26 -08002988 /*
2989 * Reduce false cache line sharing between cluster_info and
2990 * sharing same address space.
2991 */
Huang, Ying235b6212017-02-22 15:45:22 -08002992 for (k = 0; k < SWAP_CLUSTER_COLS; k++) {
2993 j = (k + col) % SWAP_CLUSTER_COLS;
2994 for (i = 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) {
2995 idx = i * SWAP_CLUSTER_COLS + j;
2996 if (idx >= nr_clusters)
2997 continue;
2998 if (cluster_count(&cluster_info[idx]))
2999 continue;
Shaohua Li2a8f9442013-09-11 14:20:28 -07003000 cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE);
Huang Ying6b534912016-10-07 16:58:42 -07003001 cluster_list_add_tail(&p->free_clusters, cluster_info,
3002 idx);
Shaohua Li2a8f9442013-09-11 14:20:28 -07003003 }
Shaohua Li2a8f9442013-09-11 14:20:28 -07003004 }
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003005 return nr_extents;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003006}
3007
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003008SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
3009{
3010 struct swap_info_struct *p;
Jeff Layton91a27b22012-10-10 15:25:28 -04003011 struct filename *name;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003012 struct file *swap_file = NULL;
3013 struct address_space *mapping;
Hugh Dickins51cc3a62021-09-02 14:53:57 -07003014 struct dentry *dentry;
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07003015 int prio;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003016 int error;
3017 union swap_header *swap_header;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003018 int nr_extents;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003019 sector_t span;
3020 unsigned long maxpages;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003021 unsigned char *swap_map = NULL;
Shaohua Li2a8f9442013-09-11 14:20:28 -07003022 struct swap_cluster_info *cluster_info = NULL;
Dan Magenheimer38b5faf2012-04-09 17:08:06 -06003023 unsigned long *frontswap_map = NULL;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003024 struct page *page = NULL;
3025 struct inode *inode = NULL;
Omar Sandoval7cbf3192018-05-25 14:47:17 -07003026 bool inced_nr_rotate_swap = false;
Shreyas K Kd7e1f4f2023-05-31 10:16:20 +05303027 bool hibernation_swap = false;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003028
Hugh Dickinsd15cab92012-03-28 14:42:42 -07003029 if (swap_flags & ~SWAP_FLAGS_VALID)
3030 return -EINVAL;
3031
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003032 if (!capable(CAP_SYS_ADMIN))
3033 return -EPERM;
3034
Aaron Lua2468cc2017-09-06 16:24:57 -07003035 if (!swap_avail_heads)
3036 return -ENOMEM;
3037
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003038 p = alloc_swap_info();
Cesar Eduardo Barros2542e5132011-03-22 16:33:18 -07003039 if (IS_ERR(p))
3040 return PTR_ERR(p);
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07003041
Shaohua Li815c2c52013-09-11 14:20:30 -07003042 INIT_WORK(&p->discard_work, swap_discard_work);
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 name = getname(specialfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 if (IS_ERR(name)) {
Cesar Eduardo Barros7de7fb62011-03-22 16:33:22 -07003046 error = PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 name = NULL;
Cesar Eduardo Barrosbd690102011-03-22 16:33:25 -07003048 goto bad_swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
Jeff Layton669abf42012-10-10 16:43:10 -04003050 swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 if (IS_ERR(swap_file)) {
Cesar Eduardo Barros7de7fb62011-03-22 16:33:22 -07003052 error = PTR_ERR(swap_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 swap_file = NULL;
Cesar Eduardo Barrosbd690102011-03-22 16:33:25 -07003054 goto bad_swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
3056
3057 p->swap_file = swap_file;
3058 mapping = swap_file->f_mapping;
Hugh Dickins51cc3a62021-09-02 14:53:57 -07003059 dentry = swap_file->f_path.dentry;
Cesar Eduardo Barros21307812011-03-22 23:03:13 -03003060 inode = mapping->host;
Hugh Dickins6f179af2015-08-17 17:34:27 -07003061
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07003062 error = claim_swapfile(p, inode);
3063 if (unlikely(error))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 goto bad_swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Naohiro Aotad795a902020-03-28 19:17:15 -07003066 inode_lock(inode);
Hugh Dickins51cc3a62021-09-02 14:53:57 -07003067 if (d_unlinked(dentry) || cant_mount(dentry)) {
3068 error = -ENOENT;
3069 goto bad_swap_unlock_inode;
3070 }
Naohiro Aotad795a902020-03-28 19:17:15 -07003071 if (IS_SWAPFILE(inode)) {
3072 error = -EBUSY;
3073 goto bad_swap_unlock_inode;
3074 }
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 /*
3077 * Read the swap header.
3078 */
Matthew Wilcox (Oracle)7e0a1262022-04-29 11:53:28 -04003079 if (!mapping->a_ops->read_folio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 error = -EINVAL;
Naohiro Aotad795a902020-03-28 19:17:15 -07003081 goto bad_swap_unlock_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
Pekka Enberg090d2b12006-06-23 02:05:08 -07003083 page = read_mapping_page(mapping, 0, swap_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 if (IS_ERR(page)) {
3085 error = PTR_ERR(page);
Naohiro Aotad795a902020-03-28 19:17:15 -07003086 goto bad_swap_unlock_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 }
Hugh Dickins81e33972009-01-06 14:39:49 -08003088 swap_header = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07003090 maxpages = read_swap_header(p, swap_header, inode);
3091 if (unlikely(!maxpages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 error = -EINVAL;
Naohiro Aotad795a902020-03-28 19:17:15 -07003093 goto bad_swap_unlock_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
Hugh Dickins886bb7e2009-01-06 14:39:48 -08003095
Hugh Dickins81e33972009-01-06 14:39:49 -08003096 /* OK, set up the swap map and apply the bad block list */
Cesar Eduardo Barros803d0c832011-03-22 16:33:14 -07003097 swap_map = vzalloc(maxpages);
Hugh Dickins81e33972009-01-06 14:39:49 -08003098 if (!swap_map) {
3099 error = -ENOMEM;
Naohiro Aotad795a902020-03-28 19:17:15 -07003100 goto bad_swap_unlock_inode;
Hugh Dickins81e33972009-01-06 14:39:49 -08003101 }
Minchan Kimf0571422017-01-10 16:58:15 -08003102
Shreyas K Kd7e1f4f2023-05-31 10:16:20 +05303103 trace_android_vh_check_hibernation_swap(p->bdev, &hibernation_swap);
3104
Christoph Hellwig36d25482022-04-15 06:52:45 +02003105 if (p->bdev && bdev_stable_writes(p->bdev))
Minchan Kimf0571422017-01-10 16:58:15 -08003106 p->flags |= SWP_STABLE_WRITES;
3107
Christoph Hellwiga8b456d2020-09-24 08:51:36 +02003108 if (p->bdev && p->bdev->bd_disk->fops->rw_page)
Minchan Kim539a6fe2017-11-15 17:33:04 -08003109 p->flags |= SWP_SYNCHRONOUS_IO;
3110
Shreyas K Kd7e1f4f2023-05-31 10:16:20 +05303111 if (p->bdev && !hibernation_swap &&
3112 bdev_nonrot(p->bdev)) {
Hugh Dickins6f179af2015-08-17 17:34:27 -07003113 int cpu;
Huang, Ying235b6212017-02-22 15:45:22 -08003114 unsigned long ci, nr_cluster;
Hugh Dickins6f179af2015-08-17 17:34:27 -07003115
Shaohua Li2a8f9442013-09-11 14:20:28 -07003116 p->flags |= SWP_SOLIDSTATE;
Huang Ying49070582020-06-01 21:49:22 -07003117 p->cluster_next_cpu = alloc_percpu(unsigned int);
3118 if (!p->cluster_next_cpu) {
3119 error = -ENOMEM;
3120 goto bad_swap_unlock_inode;
3121 }
Shaohua Li2a8f9442013-09-11 14:20:28 -07003122 /*
3123 * select a random position to start with to help wear leveling
3124 * SSD
3125 */
Huang Ying49070582020-06-01 21:49:22 -07003126 for_each_possible_cpu(cpu) {
3127 per_cpu(*p->cluster_next_cpu, cpu) =
3128 1 + prandom_u32_max(p->highest_bit);
3129 }
Huang, Ying235b6212017-02-22 15:45:22 -08003130 nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
Shaohua Li2a8f9442013-09-11 14:20:28 -07003131
Kees Cook778e1cd2018-06-12 14:04:48 -07003132 cluster_info = kvcalloc(nr_cluster, sizeof(*cluster_info),
Huang Ying54f180d2017-05-08 15:57:40 -07003133 GFP_KERNEL);
Shaohua Li2a8f9442013-09-11 14:20:28 -07003134 if (!cluster_info) {
3135 error = -ENOMEM;
Naohiro Aotad795a902020-03-28 19:17:15 -07003136 goto bad_swap_unlock_inode;
Shaohua Li2a8f9442013-09-11 14:20:28 -07003137 }
Huang, Ying235b6212017-02-22 15:45:22 -08003138
3139 for (ci = 0; ci < nr_cluster; ci++)
3140 spin_lock_init(&((cluster_info + ci)->lock));
3141
Shaohua Liebc2a1a62013-09-11 14:20:32 -07003142 p->percpu_cluster = alloc_percpu(struct percpu_cluster);
3143 if (!p->percpu_cluster) {
3144 error = -ENOMEM;
Naohiro Aotad795a902020-03-28 19:17:15 -07003145 goto bad_swap_unlock_inode;
Shaohua Liebc2a1a62013-09-11 14:20:32 -07003146 }
Hugh Dickins6f179af2015-08-17 17:34:27 -07003147 for_each_possible_cpu(cpu) {
Shaohua Liebc2a1a62013-09-11 14:20:32 -07003148 struct percpu_cluster *cluster;
Hugh Dickins6f179af2015-08-17 17:34:27 -07003149 cluster = per_cpu_ptr(p->percpu_cluster, cpu);
Shaohua Liebc2a1a62013-09-11 14:20:32 -07003150 cluster_set_null(&cluster->index);
3151 }
Omar Sandoval7cbf3192018-05-25 14:47:17 -07003152 } else {
Huang Ying81a02982017-09-06 16:24:43 -07003153 atomic_inc(&nr_rotate_swap);
Omar Sandoval7cbf3192018-05-25 14:47:17 -07003154 inced_nr_rotate_swap = true;
3155 }
Hugh Dickins81e33972009-01-06 14:39:49 -08003156
Cesar Eduardo Barros1421ef32011-03-22 16:33:31 -07003157 error = swap_cgroup_swapon(p->type, maxpages);
3158 if (error)
Naohiro Aotad795a902020-03-28 19:17:15 -07003159 goto bad_swap_unlock_inode;
Cesar Eduardo Barros1421ef32011-03-22 16:33:31 -07003160
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003161 nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
Shaohua Li2a8f9442013-09-11 14:20:28 -07003162 cluster_info, maxpages, &span);
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07003163 if (unlikely(nr_extents < 0)) {
3164 error = nr_extents;
Naohiro Aotad795a902020-03-28 19:17:15 -07003165 goto bad_swap_unlock_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 }
Dan Magenheimer38b5faf2012-04-09 17:08:06 -06003167 /* frontswap enabled? set up bit-per-page map for frontswap */
Vlastimil Babka8ea1d2a2016-07-26 15:24:42 -07003168 if (IS_ENABLED(CONFIG_FRONTSWAP))
Kees Cook778e1cd2018-06-12 14:04:48 -07003169 frontswap_map = kvcalloc(BITS_TO_LONGS(maxpages),
3170 sizeof(long),
Huang Ying54f180d2017-05-08 15:57:40 -07003171 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Christoph Hellwig70200572022-04-15 06:52:55 +02003173 if ((swap_flags & SWAP_FLAG_DISCARD) &&
3174 p->bdev && bdev_max_discard_sectors(p->bdev)) {
Shaohua Li2a8f9442013-09-11 14:20:28 -07003175 /*
3176 * When discard is enabled for swap with no particular
3177 * policy flagged, we set all swap discard flags here in
3178 * order to sustain backward compatibility with older
3179 * swapon(8) releases.
3180 */
3181 p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD |
3182 SWP_PAGE_DISCARD);
Rafael Aquinidcf6b7d2013-07-03 15:02:46 -07003183
Shaohua Li2a8f9442013-09-11 14:20:28 -07003184 /*
3185 * By flagging sys_swapon, a sysadmin can tell us to
3186 * either do single-time area discards only, or to just
3187 * perform discards for released swap page-clusters.
3188 * Now it's time to adjust the p->flags accordingly.
3189 */
3190 if (swap_flags & SWAP_FLAG_DISCARD_ONCE)
3191 p->flags &= ~SWP_PAGE_DISCARD;
3192 else if (swap_flags & SWAP_FLAG_DISCARD_PAGES)
3193 p->flags &= ~SWP_AREA_DISCARD;
Rafael Aquinidcf6b7d2013-07-03 15:02:46 -07003194
Shaohua Li2a8f9442013-09-11 14:20:28 -07003195 /* issue a swapon-time discard if it's still required */
3196 if (p->flags & SWP_AREA_DISCARD) {
3197 int err = discard_swap(p);
3198 if (unlikely(err))
3199 pr_err("swapon: discard_swap(%p): %d\n",
3200 p, err);
Rafael Aquinidcf6b7d2013-07-03 15:02:46 -07003201 }
Hugh Dickins20137a42009-01-06 14:39:54 -08003202 }
Hugh Dickins6a6ba832009-01-06 14:39:51 -08003203
Huang, Ying4b3ef9d2017-02-22 15:45:26 -08003204 error = init_swap_address_space(p->type, maxpages);
3205 if (error)
Naohiro Aotad795a902020-03-28 19:17:15 -07003206 goto bad_swap_unlock_inode;
Huang, Ying4b3ef9d2017-02-22 15:45:26 -08003207
Darrick J. Wongdc617f22019-08-20 07:55:16 -07003208 /*
3209 * Flush any pending IO and dirty mappings before we start using this
3210 * swap device.
3211 */
3212 inode->i_flags |= S_SWAPFILE;
3213 error = inode_drain_writes(inode);
3214 if (error) {
3215 inode->i_flags &= ~S_SWAPFILE;
Miaohe Lin822bca52020-10-13 16:52:30 -07003216 goto free_swap_address_space;
Darrick J. Wongdc617f22019-08-20 07:55:16 -07003217 }
3218
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003219 mutex_lock(&swapon_mutex);
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07003220 prio = -1;
Hugh Dickins78ecba02008-07-23 21:28:23 -07003221 if (swap_flags & SWAP_FLAG_PREFER)
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07003222 prio =
Hugh Dickins78ecba02008-07-23 21:28:23 -07003223 (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
Shaohua Li2a8f9442013-09-11 14:20:28 -07003224 enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map);
Cesar Eduardo Barrosc69dbfb2011-03-22 16:33:35 -07003225
Joe Perches756a0252016-03-17 14:19:47 -07003226 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 -04003227 p->pages<<(PAGE_SHIFT-10), name->name, p->prio,
Cesar Eduardo Barrosc69dbfb2011-03-22 16:33:35 -07003228 nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
3229 (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
Dan Magenheimer38b5faf2012-04-09 17:08:06 -06003230 (p->flags & SWP_DISCARDABLE) ? "D" : "",
Rafael Aquinidcf6b7d2013-07-03 15:02:46 -07003231 (p->flags & SWP_AREA_DISCARD) ? "s" : "",
3232 (p->flags & SWP_PAGE_DISCARD) ? "c" : "",
Dan Magenheimer38b5faf2012-04-09 17:08:06 -06003233 (frontswap_map) ? "FS" : "");
Cesar Eduardo Barrosc69dbfb2011-03-22 16:33:35 -07003234
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003235 mutex_unlock(&swapon_mutex);
Kay Sievers66d7dd52010-10-26 14:22:06 -07003236 atomic_inc(&proc_poll_event);
3237 wake_up_interruptible(&proc_poll_wait);
3238
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 error = 0;
3240 goto out;
Miaohe Lin822bca52020-10-13 16:52:30 -07003241free_swap_address_space:
3242 exit_swap_address_space(p->type);
Naohiro Aotad795a902020-03-28 19:17:15 -07003243bad_swap_unlock_inode:
3244 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245bad_swap:
Shaohua Liebc2a1a62013-09-11 14:20:32 -07003246 free_percpu(p->percpu_cluster);
3247 p->percpu_cluster = NULL;
Huang Ying49070582020-06-01 21:49:22 -07003248 free_percpu(p->cluster_next_cpu);
3249 p->cluster_next_cpu = NULL;
Cesar Eduardo Barrosbd690102011-03-22 16:33:25 -07003250 if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
Cesar Eduardo Barrosf2090d22011-03-22 16:33:23 -07003251 set_blocksize(p->bdev, p->old_block_size);
3252 blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
Naohiro Aotad795a902020-03-28 19:17:15 -07003254 inode = NULL;
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07003255 destroy_swap_extents(p);
Cesar Eduardo Barrose8e6c2e2011-03-22 16:33:16 -07003256 swap_cgroup_swapoff(p->type);
Hugh Dickins5d337b92005-09-03 15:54:41 -07003257 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 p->swap_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 p->flags = 0;
Hugh Dickins5d337b92005-09-03 15:54:41 -07003260 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 vfree(swap_map);
Darrick J. Wong8606a1a2017-09-08 16:13:25 -07003262 kvfree(cluster_info);
David Rientjesb6b1fd22017-09-08 16:13:29 -07003263 kvfree(frontswap_map);
Omar Sandoval7cbf3192018-05-25 14:47:17 -07003264 if (inced_nr_rotate_swap)
3265 atomic_dec(&nr_rotate_swap);
Naohiro Aotad795a902020-03-28 19:17:15 -07003266 if (swap_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 filp_close(swap_file, NULL);
3268out:
3269 if (page && !IS_ERR(page)) {
3270 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003271 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 }
3273 if (name)
3274 putname(name);
Darrick J. Wong16380452019-08-20 07:55:16 -07003275 if (inode)
Al Viro59551022016-01-22 15:40:57 -05003276 inode_unlock(inode);
Tim Chen039939a2017-02-22 15:45:43 -08003277 if (!error)
3278 enable_swap_slots_cache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return error;
3280}
3281
3282void si_swapinfo(struct sysinfo *val)
3283{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08003284 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 unsigned long nr_to_be_unused = 0;
3286
Hugh Dickins5d337b92005-09-03 15:54:41 -07003287 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08003288 for (type = 0; type < nr_swapfiles; type++) {
3289 struct swap_info_struct *si = swap_info[type];
3290
3291 if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
Miaohe Linc8945302022-06-08 22:40:30 +08003292 nr_to_be_unused += READ_ONCE(si->inuse_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 }
Shaohua Liec8acf22013-02-22 16:34:38 -08003294 val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 val->totalswap = total_swap_pages + nr_to_be_unused;
Hugh Dickins5d337b92005-09-03 15:54:41 -07003296 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297}
Vijayanand Jittaa47a1cf2020-12-29 15:48:00 +05303298EXPORT_SYMBOL_NS_GPL(si_swapinfo, MINIDUMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
3300/*
3301 * Verify that a swap entry is valid and increment its swap map count.
3302 *
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003303 * Returns error code in following case.
3304 * - success -> 0
3305 * - swp_entry is invalid -> EINVAL
3306 * - swp_entry is migration entry -> EINVAL
3307 * - swap-cache reference is requested but there is already one. -> EEXIST
3308 * - swap-cache reference is requested but the entry is not used. -> ENOENT
Hugh Dickins570a335b2009-12-14 17:58:46 -08003309 * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 */
Hugh Dickins8d69aae2009-12-14 17:58:45 -08003311static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
Hugh Dickins73c34b62009-12-14 17:58:43 -08003313 struct swap_info_struct *p;
Huang, Ying235b6212017-02-22 15:45:22 -08003314 struct swap_cluster_info *ci;
Daniel Jordanc10d38c2019-03-05 15:48:19 -08003315 unsigned long offset;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08003316 unsigned char count;
3317 unsigned char has_cache;
Miaohe Lin9d9a0332020-12-14 19:06:04 -08003318 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
Huang Yingeb085572019-07-11 20:55:33 -07003320 p = get_swap_device(entry);
Daniel Jordanc10d38c2019-03-05 15:48:19 -08003321 if (!p)
Miaohe Lin9d9a0332020-12-14 19:06:04 -08003322 return -EINVAL;
Daniel Jordanc10d38c2019-03-05 15:48:19 -08003323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 offset = swp_offset(entry);
Huang, Ying235b6212017-02-22 15:45:22 -08003325 ci = lock_cluster_or_swap_info(p, offset);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003326
Hugh Dickins253d5532009-12-14 17:58:44 -08003327 count = p->swap_map[offset];
Shaohua Liedfe23d2013-09-11 14:20:31 -07003328
3329 /*
3330 * swapin_readahead() doesn't check if a swap entry is valid, so the
3331 * swap entry could be SWAP_MAP_BAD. Check here with lock held.
3332 */
3333 if (unlikely(swap_count(count) == SWAP_MAP_BAD)) {
3334 err = -ENOENT;
3335 goto unlock_out;
3336 }
3337
Hugh Dickins253d5532009-12-14 17:58:44 -08003338 has_cache = count & SWAP_HAS_CACHE;
3339 count &= ~SWAP_HAS_CACHE;
3340 err = 0;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003341
Hugh Dickins253d5532009-12-14 17:58:44 -08003342 if (usage == SWAP_HAS_CACHE) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003343
3344 /* set SWAP_HAS_CACHE if there is no cache and entry is used */
Hugh Dickins253d5532009-12-14 17:58:44 -08003345 if (!has_cache && count)
3346 has_cache = SWAP_HAS_CACHE;
3347 else if (has_cache) /* someone else added cache */
3348 err = -EEXIST;
3349 else /* no users remaining */
3350 err = -ENOENT;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003351
3352 } else if (count || has_cache) {
Hugh Dickins253d5532009-12-14 17:58:44 -08003353
Hugh Dickins570a335b2009-12-14 17:58:46 -08003354 if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
3355 count += usage;
3356 else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
Hugh Dickins253d5532009-12-14 17:58:44 -08003357 err = -EINVAL;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003358 else if (swap_count_continued(p, offset, count))
3359 count = COUNT_CONTINUED;
3360 else
3361 err = -ENOMEM;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003362 } else
Hugh Dickins253d5532009-12-14 17:58:44 -08003363 err = -ENOENT; /* unused swap entry */
3364
Qian Caia449bf52020-08-14 17:31:31 -07003365 WRITE_ONCE(p->swap_map[offset], count | has_cache);
Hugh Dickins253d5532009-12-14 17:58:44 -08003366
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003367unlock_out:
Huang, Ying235b6212017-02-22 15:45:22 -08003368 unlock_cluster_or_swap_info(p, ci);
Miaohe Lindab8dff2022-05-19 14:08:51 -07003369 put_swap_device(p);
Hugh Dickins253d5532009-12-14 17:58:44 -08003370 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
Hugh Dickins253d5532009-12-14 17:58:44 -08003372
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003373/*
Hugh Dickinsaaa46862009-12-14 17:58:47 -08003374 * Help swapoff by noting that swap entry belongs to shmem/tmpfs
3375 * (in which case its reference count is never incremented).
3376 */
3377void swap_shmem_alloc(swp_entry_t entry)
3378{
3379 __swap_duplicate(entry, SWAP_MAP_SHMEM);
3380}
3381
3382/*
Hugh Dickins08259d52010-03-05 13:42:25 -08003383 * Increase reference count of swap entry by 1.
3384 * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
3385 * but could not be atomically allocated. Returns 0, just as if it succeeded,
3386 * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
3387 * might occur if a page table entry has got corrupted.
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003388 */
Hugh Dickins570a335b2009-12-14 17:58:46 -08003389int swap_duplicate(swp_entry_t entry)
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003390{
Hugh Dickins570a335b2009-12-14 17:58:46 -08003391 int err = 0;
3392
3393 while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
3394 err = add_swap_count_continuation(entry, GFP_ATOMIC);
3395 return err;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07003398/*
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003399 * @entry: swap entry for which we allocate swap cache.
3400 *
Hugh Dickins73c34b62009-12-14 17:58:43 -08003401 * Called when allocating swap cache for existing swap entry,
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003402 * This can return error codes. Returns 0 at success.
Chen Wandun3eeba132020-04-01 21:06:07 -07003403 * -EEXIST means there is a swap cache.
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07003404 * Note: return code is different from swap_duplicate().
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07003405 */
3406int swapcache_prepare(swp_entry_t entry)
3407{
Hugh Dickins253d5532009-12-14 17:58:44 -08003408 return __swap_duplicate(entry, SWAP_HAS_CACHE);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07003409}
3410
Kairui Song2dedda72024-02-07 02:25:59 +08003411void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry)
3412{
3413 struct swap_cluster_info *ci;
3414 unsigned long offset = swp_offset(entry);
3415 unsigned char usage;
3416
3417 ci = lock_cluster_or_swap_info(si, offset);
3418 usage = __swap_entry_free_locked(si, offset, SWAP_HAS_CACHE);
3419 unlock_cluster_or_swap_info(si, ci);
3420 if (!usage)
3421 free_swap_slot(entry);
3422}
3423
Minchan Kim0bcac062017-11-15 17:33:07 -08003424struct swap_info_struct *swp_swap_info(swp_entry_t entry)
3425{
Daniel Jordanc10d38c2019-03-05 15:48:19 -08003426 return swap_type_to_swap_info(swp_type(entry));
Minchan Kim0bcac062017-11-15 17:33:07 -08003427}
3428
Mel Gormanf981c592012-07-31 16:44:47 -07003429struct swap_info_struct *page_swap_info(struct page *page)
3430{
Minchan Kim0bcac062017-11-15 17:33:07 -08003431 swp_entry_t entry = { .val = page_private(page) };
3432 return swp_swap_info(entry);
Mel Gormanf981c592012-07-31 16:44:47 -07003433}
3434
3435/*
Matthew Wilcox (Oracle)2f525782020-12-10 10:55:05 -05003436 * out-of-line methods to avoid include hell.
Mel Gormanf981c592012-07-31 16:44:47 -07003437 */
Matthew Wilcox (Oracle)2f525782020-12-10 10:55:05 -05003438struct address_space *swapcache_mapping(struct folio *folio)
Mel Gormanf981c592012-07-31 16:44:47 -07003439{
Matthew Wilcox (Oracle)2f525782020-12-10 10:55:05 -05003440 return page_swap_info(&folio->page)->swap_file->f_mapping;
Mel Gormanf981c592012-07-31 16:44:47 -07003441}
Matthew Wilcox (Oracle)2f525782020-12-10 10:55:05 -05003442EXPORT_SYMBOL_GPL(swapcache_mapping);
Mel Gormanf981c592012-07-31 16:44:47 -07003443
3444pgoff_t __page_file_index(struct page *page)
3445{
3446 swp_entry_t swap = { .val = page_private(page) };
Mel Gormanf981c592012-07-31 16:44:47 -07003447 return swp_offset(swap);
3448}
3449EXPORT_SYMBOL_GPL(__page_file_index);
3450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451/*
Hugh Dickins570a335b2009-12-14 17:58:46 -08003452 * add_swap_count_continuation - called when a swap count is duplicated
3453 * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
3454 * page of the original vmalloc'ed swap_map, to hold the continuation count
3455 * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called
3456 * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
3457 *
3458 * These continuation pages are seldom referenced: the common paths all work
3459 * on the original swap_map, only referring to a continuation page when the
3460 * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
3461 *
3462 * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
3463 * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
3464 * can be called after dropping locks.
3465 */
3466int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
3467{
3468 struct swap_info_struct *si;
Huang, Ying235b6212017-02-22 15:45:22 -08003469 struct swap_cluster_info *ci;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003470 struct page *head;
3471 struct page *page;
3472 struct page *list_page;
3473 pgoff_t offset;
3474 unsigned char count;
Huang Yingeb085572019-07-11 20:55:33 -07003475 int ret = 0;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003476
3477 /*
3478 * When debugging, it's easier to use __GFP_ZERO here; but it's better
3479 * for latency not to zero a page while GFP_ATOMIC and holding locks.
3480 */
3481 page = alloc_page(gfp_mask | __GFP_HIGHMEM);
3482
Huang Yingeb085572019-07-11 20:55:33 -07003483 si = get_swap_device(entry);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003484 if (!si) {
3485 /*
3486 * An acceptable race has occurred since the failing
Huang Yingeb085572019-07-11 20:55:33 -07003487 * __swap_duplicate(): the swap device may be swapoff
Hugh Dickins570a335b2009-12-14 17:58:46 -08003488 */
3489 goto outer;
3490 }
Huang Yingeb085572019-07-11 20:55:33 -07003491 spin_lock(&si->lock);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003492
3493 offset = swp_offset(entry);
Huang, Ying235b6212017-02-22 15:45:22 -08003494
3495 ci = lock_cluster(si, offset);
3496
Miaohe Lind8aa24e2020-12-14 19:05:58 -08003497 count = swap_count(si->swap_map[offset]);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003498
3499 if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
3500 /*
3501 * The higher the swap count, the more likely it is that tasks
3502 * will race to add swap count continuation: we need to avoid
3503 * over-provisioning.
3504 */
3505 goto out;
3506 }
3507
3508 if (!page) {
Huang Yingeb085572019-07-11 20:55:33 -07003509 ret = -ENOMEM;
3510 goto out;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003511 }
3512
3513 /*
3514 * We are fortunate that although vmalloc_to_page uses pte_offset_map,
Seth Jennings2de1a7e2013-11-12 15:07:46 -08003515 * no architecture is using highmem pages for kernel page tables: so it
3516 * will not corrupt the GFP_ATOMIC caller's atomic page table kmaps.
Hugh Dickins570a335b2009-12-14 17:58:46 -08003517 */
3518 head = vmalloc_to_page(si->swap_map + offset);
3519 offset &= ~PAGE_MASK;
3520
Huang Ying2628bd62017-11-02 15:59:50 -07003521 spin_lock(&si->cont_lock);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003522 /*
3523 * Page allocation does not initialize the page's lru field,
3524 * but it does always reset its private field.
3525 */
3526 if (!page_private(head)) {
3527 BUG_ON(count & COUNT_CONTINUED);
3528 INIT_LIST_HEAD(&head->lru);
3529 set_page_private(head, SWP_CONTINUED);
3530 si->flags |= SWP_CONTINUED;
3531 }
3532
3533 list_for_each_entry(list_page, &head->lru, lru) {
3534 unsigned char *map;
3535
3536 /*
3537 * If the previous map said no continuation, but we've found
3538 * a continuation page, free our allocation and use this one.
3539 */
3540 if (!(count & COUNT_CONTINUED))
Huang Ying2628bd62017-11-02 15:59:50 -07003541 goto out_unlock_cont;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003542
Cong Wang9b04c5f2011-11-25 23:14:39 +08003543 map = kmap_atomic(list_page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003544 count = *map;
Cong Wang9b04c5f2011-11-25 23:14:39 +08003545 kunmap_atomic(map);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003546
3547 /*
3548 * If this continuation count now has some space in it,
3549 * free our allocation and use this one.
3550 */
3551 if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
Huang Ying2628bd62017-11-02 15:59:50 -07003552 goto out_unlock_cont;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003553 }
3554
3555 list_add_tail(&page->lru, &head->lru);
3556 page = NULL; /* now it's attached, don't free it */
Huang Ying2628bd62017-11-02 15:59:50 -07003557out_unlock_cont:
3558 spin_unlock(&si->cont_lock);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003559out:
Huang, Ying235b6212017-02-22 15:45:22 -08003560 unlock_cluster(ci);
Shaohua Liec8acf22013-02-22 16:34:38 -08003561 spin_unlock(&si->lock);
Huang Yingeb085572019-07-11 20:55:33 -07003562 put_swap_device(si);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003563outer:
3564 if (page)
3565 __free_page(page);
Huang Yingeb085572019-07-11 20:55:33 -07003566 return ret;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003567}
3568
3569/*
3570 * swap_count_continued - when the original swap_map count is incremented
3571 * from SWAP_MAP_MAX, check if there is already a continuation page to carry
3572 * into, carry if so, or else fail until a new continuation page is allocated;
3573 * when the original swap_map count is decremented from 0 with continuation,
3574 * borrow from the continuation and report whether it still holds more.
Huang, Ying235b6212017-02-22 15:45:22 -08003575 * Called while __swap_duplicate() or swap_entry_free() holds swap or cluster
3576 * lock.
Hugh Dickins570a335b2009-12-14 17:58:46 -08003577 */
3578static bool swap_count_continued(struct swap_info_struct *si,
3579 pgoff_t offset, unsigned char count)
3580{
3581 struct page *head;
3582 struct page *page;
3583 unsigned char *map;
Huang Ying2628bd62017-11-02 15:59:50 -07003584 bool ret;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003585
3586 head = vmalloc_to_page(si->swap_map + offset);
3587 if (page_private(head) != SWP_CONTINUED) {
3588 BUG_ON(count & COUNT_CONTINUED);
3589 return false; /* need to add count continuation */
3590 }
3591
Huang Ying2628bd62017-11-02 15:59:50 -07003592 spin_lock(&si->cont_lock);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003593 offset &= ~PAGE_MASK;
chenqiwu213516a2020-06-01 21:48:36 -07003594 page = list_next_entry(head, lru);
Cong Wang9b04c5f2011-11-25 23:14:39 +08003595 map = kmap_atomic(page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003596
3597 if (count == SWAP_MAP_MAX) /* initial increment from swap_map */
3598 goto init_map; /* jump over SWAP_CONT_MAX checks */
3599
3600 if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
3601 /*
3602 * Think of how you add 1 to 999
3603 */
3604 while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08003605 kunmap_atomic(map);
chenqiwu213516a2020-06-01 21:48:36 -07003606 page = list_next_entry(page, lru);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003607 BUG_ON(page == head);
Cong Wang9b04c5f2011-11-25 23:14:39 +08003608 map = kmap_atomic(page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003609 }
3610 if (*map == SWAP_CONT_MAX) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08003611 kunmap_atomic(map);
chenqiwu213516a2020-06-01 21:48:36 -07003612 page = list_next_entry(page, lru);
Huang Ying2628bd62017-11-02 15:59:50 -07003613 if (page == head) {
3614 ret = false; /* add count continuation */
3615 goto out;
3616 }
Cong Wang9b04c5f2011-11-25 23:14:39 +08003617 map = kmap_atomic(page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003618init_map: *map = 0; /* we didn't zero the page */
3619 }
3620 *map += 1;
Cong Wang9b04c5f2011-11-25 23:14:39 +08003621 kunmap_atomic(map);
chenqiwu213516a2020-06-01 21:48:36 -07003622 while ((page = list_prev_entry(page, lru)) != head) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08003623 map = kmap_atomic(page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003624 *map = COUNT_CONTINUED;
Cong Wang9b04c5f2011-11-25 23:14:39 +08003625 kunmap_atomic(map);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003626 }
Huang Ying2628bd62017-11-02 15:59:50 -07003627 ret = true; /* incremented */
Hugh Dickins570a335b2009-12-14 17:58:46 -08003628
3629 } else { /* decrementing */
3630 /*
3631 * Think of how you subtract 1 from 1000
3632 */
3633 BUG_ON(count != COUNT_CONTINUED);
3634 while (*map == COUNT_CONTINUED) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08003635 kunmap_atomic(map);
chenqiwu213516a2020-06-01 21:48:36 -07003636 page = list_next_entry(page, lru);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003637 BUG_ON(page == head);
Cong Wang9b04c5f2011-11-25 23:14:39 +08003638 map = kmap_atomic(page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003639 }
3640 BUG_ON(*map == 0);
3641 *map -= 1;
3642 if (*map == 0)
3643 count = 0;
Cong Wang9b04c5f2011-11-25 23:14:39 +08003644 kunmap_atomic(map);
chenqiwu213516a2020-06-01 21:48:36 -07003645 while ((page = list_prev_entry(page, lru)) != head) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08003646 map = kmap_atomic(page) + offset;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003647 *map = SWAP_CONT_MAX | count;
3648 count = COUNT_CONTINUED;
Cong Wang9b04c5f2011-11-25 23:14:39 +08003649 kunmap_atomic(map);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003650 }
Huang Ying2628bd62017-11-02 15:59:50 -07003651 ret = count == COUNT_CONTINUED;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003652 }
Huang Ying2628bd62017-11-02 15:59:50 -07003653out:
3654 spin_unlock(&si->cont_lock);
3655 return ret;
Hugh Dickins570a335b2009-12-14 17:58:46 -08003656}
3657
3658/*
3659 * free_swap_count_continuations - swapoff free all the continuation pages
3660 * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
3661 */
3662static void free_swap_count_continuations(struct swap_info_struct *si)
3663{
3664 pgoff_t offset;
3665
3666 for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
3667 struct page *head;
3668 head = vmalloc_to_page(si->swap_map + offset);
3669 if (page_private(head)) {
Geliang Tang0d576d22016-01-14 15:21:49 -08003670 struct page *page, *next;
3671
3672 list_for_each_entry_safe(page, next, &head->lru, lru) {
3673 list_del(&page->lru);
Hugh Dickins570a335b2009-12-14 17:58:46 -08003674 __free_page(page);
3675 }
3676 }
3677 }
3678}
Aaron Lua2468cc2017-09-06 16:24:57 -07003679
Tejun Heo2cf85582018-07-03 11:14:56 -04003680#if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
Suren Baghdasaryan01c4b282021-09-02 14:54:54 -07003681void __cgroup_throttle_swaprate(struct page *page, gfp_t gfp_mask)
Tejun Heo2cf85582018-07-03 11:14:56 -04003682{
3683 struct swap_info_struct *si, *next;
Johannes Weiner6caa6a02020-06-03 16:01:38 -07003684 int nid = page_to_nid(page);
3685
3686 if (!(gfp_mask & __GFP_IO))
Tejun Heo2cf85582018-07-03 11:14:56 -04003687 return;
3688
3689 if (!blk_cgroup_congested())
3690 return;
3691
3692 /*
3693 * We've already scheduled a throttle, avoid taking the global swap
3694 * lock.
3695 */
3696 if (current->throttle_queue)
3697 return;
3698
3699 spin_lock(&swap_avail_lock);
Johannes Weiner6caa6a02020-06-03 16:01:38 -07003700 plist_for_each_entry_safe(si, next, &swap_avail_heads[nid],
3701 avail_lists[nid]) {
Tejun Heo2cf85582018-07-03 11:14:56 -04003702 if (si->bdev) {
Christoph Hellwigde185b52022-09-21 20:05:00 +02003703 blkcg_schedule_throttle(si->bdev->bd_disk, true);
Tejun Heo2cf85582018-07-03 11:14:56 -04003704 break;
3705 }
3706 }
3707 spin_unlock(&swap_avail_lock);
3708}
3709#endif
3710
Aaron Lua2468cc2017-09-06 16:24:57 -07003711static int __init swapfile_init(void)
3712{
3713 int nid;
3714
3715 swap_avail_heads = kmalloc_array(nr_node_ids, sizeof(struct plist_head),
3716 GFP_KERNEL);
3717 if (!swap_avail_heads) {
3718 pr_emerg("Not enough memory for swap heads, swap is disabled\n");
3719 return -ENOMEM;
3720 }
3721
3722 for_each_node(nid)
3723 plist_head_init(&swap_avail_heads[nid]);
3724
Peter Xube45a492022-08-11 12:13:30 -04003725 swapfile_maximum_size = arch_max_swapfile_size();
3726
Peter Xu5154e602022-08-11 12:13:31 -04003727#ifdef CONFIG_MIGRATION
3728 if (swapfile_maximum_size >= (1UL << SWP_MIG_TOTAL_BITS))
3729 swap_migration_ad_supported = true;
3730#endif /* CONFIG_MIGRATION */
3731
Aaron Lua2468cc2017-09-06 16:24:57 -07003732 return 0;
3733}
3734subsys_initcall(swapfile_init);