Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *  linux/mm/swapfile.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *  Swap reorganised 29.12.95, Stephen Tweedie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/hugetlb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/kernel_stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/shmem_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/ksm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/rmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/memcontrol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/oom.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/frontswap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/swapfile.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <linux/swap_slots.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/sort.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <linux/swapops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <linux/swap_cgroup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include <trace/hooks/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 				 unsigned char);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) static void free_swap_count_continuations(struct swap_info_struct *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) static sector_t map_swap_entry(swp_entry_t, struct block_device**);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) DEFINE_SPINLOCK(swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) static unsigned int nr_swapfiles;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) atomic_long_t nr_swap_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  * Some modules use swappable objects and may try to swap them out under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * memory pressure (via the shrinker). Before doing so, they may wish to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * check to see if any swap space is available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) EXPORT_SYMBOL_GPL(nr_swap_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) long total_swap_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static int least_priority = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) static const char Bad_file[] = "Bad swap file entry ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) static const char Unused_file[] = "Unused swap file entry ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) static const char Bad_offset[] = "Bad swap offset entry ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) static const char Unused_offset[] = "Unused swap offset entry ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  * all active swap_info_structs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  * protected with swap_lock, and ordered by priority.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) PLIST_HEAD(swap_active_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * all available (active, not full) swap_info_structs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  * protected with swap_avail_lock, ordered by priority.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  * This is used by get_swap_page() instead of swap_active_head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  * because swap_active_head includes all swap_info_structs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  * but get_swap_page() doesn't need to look at full ones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  * This uses its own lock instead of swap_lock because when a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  * swap_info_struct changes between not-full/full, it needs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  * add/remove itself to/from this list, but the swap_info_struct->lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  * is held and the locking order requires swap_lock to be taken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  * before any swap_info_struct->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) static struct plist_head *swap_avail_heads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) static DEFINE_SPINLOCK(swap_avail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) struct swap_info_struct *swap_info[MAX_SWAPFILES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) static DEFINE_MUTEX(swapon_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) /* Activity counter to indicate that a swapon or swapoff has occurred */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) static atomic_t proc_poll_event = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) atomic_t nr_rotate_swap = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) struct swap_info_struct *swap_type_to_swap_info(int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	if (type >= READ_ONCE(nr_swapfiles))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	smp_rmb();	/* Pairs with smp_wmb in alloc_swap_info. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	return READ_ONCE(swap_info[type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) EXPORT_SYMBOL_GPL(swap_type_to_swap_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) static inline unsigned char swap_count(unsigned char ent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	return ent & ~SWAP_HAS_CACHE;	/* may include COUNT_CONTINUED flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) /* Reclaim the swap entry anyway if possible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) #define TTRS_ANYWAY		0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  * Reclaim the swap entry if there are no more mappings of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  * corresponding page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) #define TTRS_UNMAPPED		0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) /* Reclaim the swap entry if swap is getting full*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) #define TTRS_FULL		0x4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) /* returns 1 if swap entry is freed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) static int __try_to_reclaim_swap(struct swap_info_struct *si,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 				 unsigned long offset, unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	swp_entry_t entry = swp_entry(si->type, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	page = find_get_page(swap_address_space(entry), offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	 * When this function is called from scan_swap_map_slots() and it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	 * called by vmscan.c at reclaiming pages. So, we hold a lock on a page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	 * here. We have to use trylock for avoiding deadlock. This is a special
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	 * case and you should use try_to_free_swap() with explicit lock_page()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	 * in usual operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	if (trylock_page(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		if ((flags & TTRS_ANYWAY) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		    ((flags & TTRS_UNMAPPED) && !page_mapped(page)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		    ((flags & TTRS_FULL) && mem_cgroup_swap_full(page)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 			ret = try_to_free_swap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) static inline struct swap_extent *first_se(struct swap_info_struct *sis)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	struct rb_node *rb = rb_first(&sis->swap_extent_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	return rb_entry(rb, struct swap_extent, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) static inline struct swap_extent *next_se(struct swap_extent *se)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	struct rb_node *rb = rb_next(&se->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	return rb ? rb_entry(rb, struct swap_extent, rb_node) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169)  * swapon tell device that all the old swap contents can be discarded,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170)  * to allow the swap device to optimize its wear-levelling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) static int discard_swap(struct swap_info_struct *si)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	struct swap_extent *se;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	sector_t start_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	sector_t nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	/* Do not discard the swap header page! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	se = first_se(si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	if (nr_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		err = blkdev_issue_discard(si->bdev, start_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 				nr_blocks, GFP_KERNEL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	for (se = next_se(se); se; se = next_se(se)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		start_block = se->start_block << (PAGE_SHIFT - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		err = blkdev_issue_discard(si->bdev, start_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 				nr_blocks, GFP_KERNEL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	return err;		/* That will often be -EOPNOTSUPP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) static struct swap_extent *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) offset_to_swap_extent(struct swap_info_struct *sis, unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	struct swap_extent *se;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	struct rb_node *rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	rb = sis->swap_extent_root.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	while (rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		se = rb_entry(rb, struct swap_extent, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		if (offset < se->start_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 			rb = rb->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		else if (offset >= se->start_page + se->nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 			rb = rb->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 			return se;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	/* It *must* be present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) sector_t swap_page_sector(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	struct swap_info_struct *sis = page_swap_info(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	struct swap_extent *se;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	sector_t sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	pgoff_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	offset = __page_file_index(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	se = offset_to_swap_extent(sis, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	sector = se->start_block + (offset - se->start_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	return sector << (PAGE_SHIFT - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239)  * swap allocation tell device that a cluster of swap can now be discarded,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240)  * to allow the swap device to optimize its wear-levelling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) static void discard_swap_cluster(struct swap_info_struct *si,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 				 pgoff_t start_page, pgoff_t nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	struct swap_extent *se = offset_to_swap_extent(si, start_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	while (nr_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		pgoff_t offset = start_page - se->start_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		sector_t start_block = se->start_block + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		sector_t nr_blocks = se->nr_pages - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		if (nr_blocks > nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 			nr_blocks = nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		start_page += nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		nr_pages -= nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		start_block <<= PAGE_SHIFT - 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		nr_blocks <<= PAGE_SHIFT - 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		if (blkdev_issue_discard(si->bdev, start_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 					nr_blocks, GFP_NOIO, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		se = next_se(se);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) #ifdef CONFIG_THP_SWAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) #define SWAPFILE_CLUSTER	HPAGE_PMD_NR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) #define swap_entry_size(size)	(size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) #define SWAPFILE_CLUSTER	256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275)  * Define swap_entry_size() as constant to let compiler to optimize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276)  * out some code if !CONFIG_THP_SWAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) #define swap_entry_size(size)	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) #define LATENCY_LIMIT		256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) static inline void cluster_set_flag(struct swap_cluster_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	unsigned int flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	info->flags = flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) static inline unsigned int cluster_count(struct swap_cluster_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	return info->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) static inline void cluster_set_count(struct swap_cluster_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 				     unsigned int c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	info->data = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) static inline void cluster_set_count_flag(struct swap_cluster_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 					 unsigned int c, unsigned int f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	info->flags = f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	info->data = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) static inline unsigned int cluster_next(struct swap_cluster_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	return info->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) static inline void cluster_set_next(struct swap_cluster_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 				    unsigned int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	info->data = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) static inline void cluster_set_next_flag(struct swap_cluster_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 					 unsigned int n, unsigned int f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	info->flags = f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	info->data = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) static inline bool cluster_is_free(struct swap_cluster_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	return info->flags & CLUSTER_FLAG_FREE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) static inline bool cluster_is_null(struct swap_cluster_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	return info->flags & CLUSTER_FLAG_NEXT_NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) static inline void cluster_set_null(struct swap_cluster_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	info->flags = CLUSTER_FLAG_NEXT_NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	info->data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) static inline bool cluster_is_huge(struct swap_cluster_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	if (IS_ENABLED(CONFIG_THP_SWAP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		return info->flags & CLUSTER_FLAG_HUGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) static inline void cluster_clear_huge(struct swap_cluster_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	info->flags &= ~CLUSTER_FLAG_HUGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 						     unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	ci = si->cluster_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	if (ci) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		ci += offset / SWAPFILE_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		spin_lock(&ci->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	return ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) static inline void unlock_cluster(struct swap_cluster_info *ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	if (ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		spin_unlock(&ci->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372)  * Determine the locking method in use for this device.  Return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373)  * swap_cluster_info if SSD-style cluster-based locking is in place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) static inline struct swap_cluster_info *lock_cluster_or_swap_info(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		struct swap_info_struct *si, unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	/* Try to use fine-grained SSD-style locking if available: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	ci = lock_cluster(si, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	/* Otherwise, fall back to traditional, coarse locking: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	if (!ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	return ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) static inline void unlock_cluster_or_swap_info(struct swap_info_struct *si,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 					       struct swap_cluster_info *ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	if (ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		unlock_cluster(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) static inline bool cluster_list_empty(struct swap_cluster_list *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	return cluster_is_null(&list->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) static inline unsigned int cluster_list_first(struct swap_cluster_list *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	return cluster_next(&list->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) static void cluster_list_init(struct swap_cluster_list *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	cluster_set_null(&list->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	cluster_set_null(&list->tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) static void cluster_list_add_tail(struct swap_cluster_list *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 				  struct swap_cluster_info *ci,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 				  unsigned int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	if (cluster_list_empty(list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		cluster_set_next_flag(&list->head, idx, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		cluster_set_next_flag(&list->tail, idx, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		struct swap_cluster_info *ci_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		unsigned int tail = cluster_next(&list->tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		 * Nested cluster lock, but both cluster locks are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		 * only acquired when we held swap_info_struct->lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		ci_tail = ci + tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		spin_lock_nested(&ci_tail->lock, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		cluster_set_next(ci_tail, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		spin_unlock(&ci_tail->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		cluster_set_next_flag(&list->tail, idx, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) static unsigned int cluster_list_del_first(struct swap_cluster_list *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 					   struct swap_cluster_info *ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	idx = cluster_next(&list->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	if (cluster_next(&list->tail) == idx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		cluster_set_null(&list->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		cluster_set_null(&list->tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		cluster_set_next_flag(&list->head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 				      cluster_next(&ci[idx]), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	return idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) /* Add a cluster to discard list and schedule it to do discard */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) static void swap_cluster_schedule_discard(struct swap_info_struct *si,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		unsigned int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	 * If scan_swap_map() can't find a free cluster, it will check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	 * si->swap_map directly. To make sure the discarding cluster isn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	 * taken by scan_swap_map(), mark the swap entries bad (occupied). It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	 * will be cleared after discard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	memset(si->swap_map + idx * SWAPFILE_CLUSTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 			SWAP_MAP_BAD, SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	schedule_work(&si->discard_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) static void __free_cluster(struct swap_info_struct *si, unsigned long idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	struct swap_cluster_info *ci = si->cluster_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	cluster_set_flag(ci + idx, CLUSTER_FLAG_FREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	cluster_list_add_tail(&si->free_clusters, ci, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480)  * Doing discard actually. After a cluster discard is finished, the cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481)  * will be added to free cluster list. caller should hold si->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) static void swap_do_scheduled_discard(struct swap_info_struct *si)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	struct swap_cluster_info *info, *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	info = si->cluster_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	while (!cluster_list_empty(&si->discard_clusters)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		idx = cluster_list_del_first(&si->discard_clusters, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		discard_swap_cluster(si, idx * SWAPFILE_CLUSTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 				SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		ci = lock_cluster(si, idx * SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		__free_cluster(si, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		memset(si->swap_map + idx * SWAPFILE_CLUSTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 				0, SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		unlock_cluster(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) static void swap_discard_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	struct swap_info_struct *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	si = container_of(work, struct swap_info_struct, discard_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	swap_do_scheduled_discard(si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) static void alloc_cluster(struct swap_info_struct *si, unsigned long idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	struct swap_cluster_info *ci = si->cluster_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	VM_BUG_ON(cluster_list_first(&si->free_clusters) != idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	cluster_list_del_first(&si->free_clusters, ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	cluster_set_count_flag(ci + idx, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) static void free_cluster(struct swap_info_struct *si, unsigned long idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	struct swap_cluster_info *ci = si->cluster_info + idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	VM_BUG_ON(cluster_count(ci) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	 * If the swap is discardable, prepare discard the cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	 * instead of free it immediately. The cluster will be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	 * after discard.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	if ((si->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	    (SWP_WRITEOK | SWP_PAGE_DISCARD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		swap_cluster_schedule_discard(si, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	__free_cluster(si, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546)  * The cluster corresponding to page_nr will be used. The cluster will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547)  * removed from free cluster list and its usage counter will be increased.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) static void inc_cluster_info_page(struct swap_info_struct *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	struct swap_cluster_info *cluster_info, unsigned long page_nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	unsigned long idx = page_nr / SWAPFILE_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	if (!cluster_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	if (cluster_is_free(&cluster_info[idx]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		alloc_cluster(p, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	cluster_set_count(&cluster_info[idx],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		cluster_count(&cluster_info[idx]) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565)  * The cluster corresponding to page_nr decreases one usage. If the usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566)  * counter becomes 0, which means no page in the cluster is in using, we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567)  * optionally discard the cluster and add it to free cluster list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) static void dec_cluster_info_page(struct swap_info_struct *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	struct swap_cluster_info *cluster_info, unsigned long page_nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	unsigned long idx = page_nr / SWAPFILE_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	if (!cluster_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	cluster_set_count(&cluster_info[idx],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		cluster_count(&cluster_info[idx]) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	if (cluster_count(&cluster_info[idx]) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		free_cluster(p, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586)  * It's possible scan_swap_map() uses a free cluster in the middle of free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587)  * cluster list. Avoiding such abuse to avoid list corruption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	struct percpu_cluster *percpu_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	bool conflict;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	offset /= SWAPFILE_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	conflict = !cluster_list_empty(&si->free_clusters) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		offset != cluster_list_first(&si->free_clusters) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		cluster_is_free(&si->cluster_info[offset]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	if (!conflict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	percpu_cluster = this_cpu_ptr(si->percpu_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	cluster_set_null(&percpu_cluster->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610)  * Try to get a swap entry from current cpu's swap entry pool (a cluster). This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611)  * might involve allocating a new cluster for current CPU too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	unsigned long *offset, unsigned long *scan_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	struct percpu_cluster *cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	unsigned long tmp, max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) new_cluster:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	cluster = this_cpu_ptr(si->percpu_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	if (cluster_is_null(&cluster->index)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		if (!cluster_list_empty(&si->free_clusters)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 			cluster->index = si->free_clusters.head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 			cluster->next = cluster_next(&cluster->index) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 					SWAPFILE_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		} else if (!cluster_list_empty(&si->discard_clusters)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 			 * we don't have free cluster but have some clusters in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 			 * discarding, do discard now and reclaim them, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 			 * reread cluster_next_cpu since we dropped si->lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 			swap_do_scheduled_discard(si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 			*scan_base = this_cpu_read(*si->cluster_next_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 			*offset = *scan_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 			goto new_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	 * Other CPUs can use our cluster if they can't find a free cluster,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	 * check if there is still free entry in the cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	tmp = cluster->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	max = min_t(unsigned long, si->max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		    (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	if (tmp < max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		ci = lock_cluster(si, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		while (tmp < max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 			if (!si->swap_map[tmp])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 			tmp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		unlock_cluster(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	if (tmp >= max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		cluster_set_null(&cluster->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		goto new_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	cluster->next = tmp + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	*offset = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	*scan_base = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) static void __del_from_avail_list(struct swap_info_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	int nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	for_each_node(nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		plist_del(&p->avail_lists[nid], &swap_avail_heads[nid]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) static void del_from_avail_list(struct swap_info_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	spin_lock(&swap_avail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	__del_from_avail_list(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	spin_unlock(&swap_avail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 			     unsigned int nr_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	unsigned int end = offset + nr_entries - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	if (offset == si->lowest_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		si->lowest_bit += nr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	if (end == si->highest_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		WRITE_ONCE(si->highest_bit, si->highest_bit - nr_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	si->inuse_pages += nr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	if (si->inuse_pages == si->pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		si->lowest_bit = si->max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		si->highest_bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		del_from_avail_list(si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) static void add_to_avail_list(struct swap_info_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	int nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	spin_lock(&swap_avail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	for_each_node(nid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		WARN_ON(!plist_node_empty(&p->avail_lists[nid]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		plist_add(&p->avail_lists[nid], &swap_avail_heads[nid]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	spin_unlock(&swap_avail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 			    unsigned int nr_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	unsigned long begin = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	unsigned long end = offset + nr_entries - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	void (*swap_slot_free_notify)(struct block_device *, unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	bool skip = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	if (offset < si->lowest_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		si->lowest_bit = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	if (end > si->highest_bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		bool was_full = !si->highest_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		WRITE_ONCE(si->highest_bit, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		if (was_full && (si->flags & SWP_WRITEOK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 			add_to_avail_list(si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	trace_android_vh_account_swap_pages(si, &skip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	if (!skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		atomic_long_add(nr_entries, &nr_swap_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	si->inuse_pages -= nr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	if (si->flags & SWP_BLKDEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		swap_slot_free_notify =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 			si->bdev->bd_disk->fops->swap_slot_free_notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		swap_slot_free_notify = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	while (offset <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		arch_swap_invalidate_page(si->type, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		frontswap_invalidate_page(si->type, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		if (swap_slot_free_notify)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 			swap_slot_free_notify(si->bdev, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		offset++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	clear_shadow_from_swap_cache(si->type, begin, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) static void set_cluster_next(struct swap_info_struct *si, unsigned long next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	unsigned long prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	if (!(si->flags & SWP_SOLIDSTATE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		si->cluster_next = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	prev = this_cpu_read(*si->cluster_next_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	 * Cross the swap address space size aligned trunk, choose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	 * another trunk randomly to avoid lock contention on swap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	 * address space if possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	if ((prev >> SWAP_ADDRESS_SPACE_SHIFT) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	    (next >> SWAP_ADDRESS_SPACE_SHIFT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		/* No free swap slots available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		if (si->highest_bit <= si->lowest_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		next = si->lowest_bit +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 			prandom_u32_max(si->highest_bit - si->lowest_bit + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		next = ALIGN_DOWN(next, SWAP_ADDRESS_SPACE_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		next = max_t(unsigned int, next, si->lowest_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	this_cpu_write(*si->cluster_next_cpu, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) int scan_swap_map_slots(struct swap_info_struct *si,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 			       unsigned char usage, int nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 			       swp_entry_t slots[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	unsigned long scan_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	unsigned long last_in_cluster = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	int latency_ration = LATENCY_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	int n_ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	bool scanned_many = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	 * We try to cluster swap pages by allocating them sequentially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	 * in swap.  Once we've allocated SWAPFILE_CLUSTER pages this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	 * way, however, we resort to first-free allocation, starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	 * a new cluster.  This prevents us from scattering swap pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	 * all over the entire swap partition, so that we reduce
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	 * overall disk seek times between swap pages.  -- sct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	 * But we do now try to find an empty cluster.  -Andrea
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	 * And we let swap pages go all over an SSD partition.  Hugh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	si->flags += SWP_SCANNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	 * Use percpu scan base for SSD to reduce lock contention on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	 * cluster and swap cache.  For HDD, sequential access is more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	 * important.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	if (si->flags & SWP_SOLIDSTATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		scan_base = this_cpu_read(*si->cluster_next_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		scan_base = si->cluster_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	offset = scan_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	/* SSD algorithm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	if (si->cluster_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		if (!scan_swap_map_try_ssd_cluster(si, &offset, &scan_base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 			goto scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	} else if (unlikely(!si->cluster_nr--)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 			si->cluster_nr = SWAPFILE_CLUSTER - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 			goto checks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		 * If seek is expensive, start searching for new cluster from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		 * start of partition, to minimize the span of allocated swap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		 * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		 * case, just handled by scan_swap_map_try_ssd_cluster() above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		scan_base = offset = si->lowest_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		/* Locate the first empty (unaligned) cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		for (; last_in_cluster <= si->highest_bit; offset++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 			if (si->swap_map[offset])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 				last_in_cluster = offset + SWAPFILE_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 			else if (offset == last_in_cluster) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 				spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 				offset -= SWAPFILE_CLUSTER - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 				si->cluster_next = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 				si->cluster_nr = SWAPFILE_CLUSTER - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 				goto checks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			if (unlikely(--latency_ration < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 				cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 				latency_ration = LATENCY_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		offset = scan_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		si->cluster_nr = SWAPFILE_CLUSTER - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) checks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	if (si->cluster_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		while (scan_swap_map_ssd_cluster_conflict(si, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		/* take a break if we already got some slots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 			if (n_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 				goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 			if (!scan_swap_map_try_ssd_cluster(si, &offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 							&scan_base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 				goto scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	if (!(si->flags & SWP_WRITEOK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		goto no_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	if (!si->highest_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		goto no_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	if (offset > si->highest_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		scan_base = offset = si->lowest_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	ci = lock_cluster(si, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	/* reuse swap entry of cache-only swap if not busy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		int swap_was_freed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		unlock_cluster(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		swap_was_freed = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		/* entry was freed successfully, try to use this again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		if (swap_was_freed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			goto checks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		goto scan; /* check next one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	if (si->swap_map[offset]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		unlock_cluster(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		if (!n_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			goto scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	WRITE_ONCE(si->swap_map[offset], usage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	inc_cluster_info_page(si, si->cluster_info, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	unlock_cluster(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	swap_range_alloc(si, offset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	slots[n_ret++] = swp_entry(si->type, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	/* got enough slots or reach max slots? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	if ((n_ret == nr) || (offset >= si->highest_bit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	/* search for next available slot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	/* time to take a break? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	if (unlikely(--latency_ration < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		if (n_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		latency_ration = LATENCY_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	/* try to get more slots in cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	if (si->cluster_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			goto checks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	} else if (si->cluster_nr && !si->swap_map[++offset]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		/* non-ssd case, still more slots in cluster? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		--si->cluster_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		goto checks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	 * Even if there's no free clusters available (fragmented),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	 * try to scan a little more quickly with lock held unless we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	 * have scanned too many slots already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	if (!scanned_many) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		unsigned long scan_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		if (offset < scan_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 			scan_limit = scan_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			scan_limit = si->highest_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		for (; offset <= scan_limit && --latency_ration > 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		     offset++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 			if (!si->swap_map[offset])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 				goto checks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	set_cluster_next(si, offset + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	si->flags -= SWP_SCANNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	return n_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) scan:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	while (++offset <= READ_ONCE(si->highest_bit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		if (data_race(!si->swap_map[offset])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 			goto checks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		if (vm_swap_full() &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		    READ_ONCE(si->swap_map[offset]) == SWAP_HAS_CACHE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			goto checks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		if (unlikely(--latency_ration < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			latency_ration = LATENCY_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			scanned_many = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	offset = si->lowest_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	while (offset < scan_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		if (data_race(!si->swap_map[offset])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			goto checks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		if (vm_swap_full() &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		    READ_ONCE(si->swap_map[offset]) == SWAP_HAS_CACHE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 			spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			goto checks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		if (unlikely(--latency_ration < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 			cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			latency_ration = LATENCY_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 			scanned_many = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		offset++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) no_page:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	si->flags -= SWP_SCANNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	return n_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) EXPORT_SYMBOL_GPL(scan_swap_map_slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	unsigned long idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	unsigned long offset, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	unsigned char *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	 * Should not even be attempting cluster allocations when huge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	 * page swap is disabled.  Warn and fail the allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	if (!IS_ENABLED(CONFIG_THP_SWAP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		VM_WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	if (cluster_list_empty(&si->free_clusters))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	idx = cluster_list_first(&si->free_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	offset = idx * SWAPFILE_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	ci = lock_cluster(si, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	alloc_cluster(si, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	cluster_set_count_flag(ci, SWAPFILE_CLUSTER, CLUSTER_FLAG_HUGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	map = si->swap_map + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	for (i = 0; i < SWAPFILE_CLUSTER; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		map[i] = SWAP_HAS_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	unlock_cluster(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	swap_range_alloc(si, offset, SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	*slot = swp_entry(si->type, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) EXPORT_SYMBOL_GPL(swap_alloc_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	unsigned long offset = idx * SWAPFILE_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	ci = lock_cluster(si, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	memset(si->swap_map + offset, 0, SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	cluster_set_count_flag(ci, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	free_cluster(si, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	unlock_cluster(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	swap_range_free(si, offset, SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) static unsigned long scan_swap_map(struct swap_info_struct *si,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 				   unsigned char usage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	swp_entry_t entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	int n_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	n_ret = scan_swap_map_slots(si, usage, 1, &entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	if (n_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		return swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	unsigned long size = swap_entry_size(entry_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	struct swap_info_struct *si, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	long avail_pgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	int n_ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	int node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	/* Only single cluster request supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	WARN_ON_ONCE(n_goal > 1 && size == SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	spin_lock(&swap_avail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	avail_pgs = atomic_long_read(&nr_swap_pages) / size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	if (avail_pgs <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		spin_unlock(&swap_avail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		goto noswap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	n_goal = min3((long)n_goal, (long)SWAP_BATCH, avail_pgs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	atomic_long_sub(n_goal * size, &nr_swap_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) start_over:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	node = numa_node_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		/* requeue si to after same-priority siblings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		spin_unlock(&swap_avail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 			spin_lock(&swap_avail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 			if (plist_node_empty(&si->avail_lists[node])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 				spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 				goto nextsi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			WARN(!si->highest_bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 			     "swap_info %d in list but !highest_bit\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 			     si->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 			WARN(!(si->flags & SWP_WRITEOK),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 			     "swap_info %d in list but !SWP_WRITEOK\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 			     si->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			__del_from_avail_list(si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 			spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 			goto nextsi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		if (size == SWAPFILE_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 			if (si->flags & SWP_BLKDEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 				n_ret = swap_alloc_cluster(si, swp_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 			n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 						    n_goal, swp_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		if (n_ret || size == SWAPFILE_CLUSTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 			goto check_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		pr_debug("scan_swap_map of si %d failed to find offset\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			si->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		spin_lock(&swap_avail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) nextsi:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		 * if we got here, it's likely that si was almost full before,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		 * and since scan_swap_map() can drop the si->lock, multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		 * callers probably all tried to get a page from the same si
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		 * and it filled up before we could get one; or, the si filled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		 * up between us dropping swap_avail_lock and taking si->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		 * Since we dropped the swap_avail_lock, the swap_avail_head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		 * list may have been modified; so if next is still in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		 * swap_avail_head list then try it, otherwise start over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		 * if we have not gotten any slots.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		if (plist_node_empty(&next->avail_lists[node]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 			goto start_over;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	spin_unlock(&swap_avail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) check_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	if (n_ret < n_goal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		atomic_long_add((long)(n_goal - n_ret) * size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 				&nr_swap_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) noswap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	return n_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) /* The only caller of this function is now suspend routine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) swp_entry_t get_swap_page_of_type(int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	struct swap_info_struct *si = swap_type_to_swap_info(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	pgoff_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	bool skip = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	if (!si)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	if (si->flags & SWP_WRITEOK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		/* This is called for allocating swap entry, not cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		offset = scan_swap_map(si, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		if (offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 			trace_android_vh_account_swap_pages(si, &skip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 			if (!skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 				atomic_long_dec(&nr_swap_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 			spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 			return swp_entry(type, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	return (swp_entry_t) {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) static struct swap_info_struct *__swap_info_get(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	struct swap_info_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	if (!entry.val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	p = swp_swap_info(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		goto bad_nofile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	if (data_race(!(p->flags & SWP_USED)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		goto bad_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	if (offset >= p->max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		goto bad_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) bad_offset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	pr_err("swap_info_get: %s%08lx\n", Bad_offset, entry.val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) bad_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	pr_err("swap_info_get: %s%08lx\n", Unused_file, entry.val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) bad_nofile:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	pr_err("swap_info_get: %s%08lx\n", Bad_file, entry.val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) static struct swap_info_struct *_swap_info_get(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	struct swap_info_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	p = __swap_info_get(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	if (data_race(!p->swap_map[swp_offset(entry)]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		goto bad_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) bad_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	pr_err("swap_info_get: %s%08lx\n", Unused_offset, entry.val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) static struct swap_info_struct *swap_info_get(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	struct swap_info_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	p = _swap_info_get(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		spin_lock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 					struct swap_info_struct *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	struct swap_info_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	p = _swap_info_get(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	if (p != q) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		if (q != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 			spin_unlock(&q->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		if (p != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 			spin_lock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) static unsigned char __swap_entry_free_locked(struct swap_info_struct *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 					      unsigned long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 					      unsigned char usage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	unsigned char count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	unsigned char has_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	count = p->swap_map[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	has_cache = count & SWAP_HAS_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	count &= ~SWAP_HAS_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	if (usage == SWAP_HAS_CACHE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		VM_BUG_ON(!has_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		has_cache = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	} else if (count == SWAP_MAP_SHMEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		 * Or we could insist on shmem.c using a special
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		 * swap_shmem_free() and free_shmem_swap_and_cache()...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	} else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		if (count == COUNT_CONTINUED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 			if (swap_count_continued(p, offset, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 				count = SWAP_MAP_MAX | COUNT_CONTINUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 				count = SWAP_MAP_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 			count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	usage = count | has_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	if (usage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		WRITE_ONCE(p->swap_map[offset], usage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		WRITE_ONCE(p->swap_map[offset], SWAP_HAS_CACHE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	return usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)  * Check whether swap entry is valid in the swap device.  If so,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)  * return pointer to swap_info_struct, and keep the swap entry valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284)  * via preventing the swap device from being swapoff, until
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)  * put_swap_device() is called.  Otherwise return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)  * The entirety of the RCU read critical section must come before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288)  * return from or after the call to synchronize_rcu() in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)  * enable_swap_info() or swapoff().  So if "si->flags & SWP_VALID" is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290)  * true, the si->map, si->cluster_info, etc. must be valid in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)  * critical section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293)  * Notice that swapoff or swapoff+swapon can still happen before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)  * rcu_read_lock() in get_swap_device() or after the rcu_read_unlock()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)  * in put_swap_device() if there isn't any other way to prevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296)  * swapoff, such as page lock, page table lock, etc.  The caller must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)  * be prepared for that.  For example, the following situation is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)  * possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)  *   CPU1				CPU2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301)  *   do_swap_page()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302)  *     ...				swapoff+swapon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303)  *     __read_swap_cache_async()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304)  *       swapcache_prepare()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305)  *         __swap_duplicate()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306)  *           // check swap_map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307)  *     // verify PTE not changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)  * In __swap_duplicate(), the swap_map need to be checked before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310)  * changing partly because the specified swap entry may be for another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311)  * swap device which has been swapoff.  And in do_swap_page(), after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312)  * the page is read from the swap device, the PTE is verified not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313)  * changed with the page table locked to check whether the swap device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314)  * has been swapoff or swapoff+swapon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) struct swap_info_struct *get_swap_device(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	struct swap_info_struct *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	if (!entry.val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	si = swp_swap_info(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	if (!si)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		goto bad_nofile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	if (data_race(!(si->flags & SWP_VALID)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		goto unlock_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	if (offset >= si->max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		goto unlock_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	return si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) bad_nofile:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) unlock_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) static unsigned char __swap_entry_free(struct swap_info_struct *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 				       swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	unsigned long offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	unsigned char usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	ci = lock_cluster_or_swap_info(p, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	usage = __swap_entry_free_locked(p, offset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	unlock_cluster_or_swap_info(p, ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	if (!usage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		free_swap_slot(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	return usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	unsigned long offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	unsigned char count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	ci = lock_cluster(p, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	count = p->swap_map[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	VM_BUG_ON(count != SWAP_HAS_CACHE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	p->swap_map[offset] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	dec_cluster_info_page(p, p->cluster_info, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	unlock_cluster(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	mem_cgroup_uncharge_swap(entry, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	swap_range_free(p, offset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378)  * Caller has made sure that the swap device corresponding to entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)  * is still around or has not been recycled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) void swap_free(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	struct swap_info_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	p = _swap_info_get(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		__swap_entry_free(p, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391)  * Called after dropping swapcache to decrease refcnt to swap entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) void put_swap_page(struct page *page, swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	unsigned long offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	unsigned long idx = offset / SWAPFILE_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	struct swap_info_struct *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	unsigned char *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	unsigned int i, free_entries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	unsigned char val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	int size = swap_entry_size(thp_nr_pages(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	si = _swap_info_get(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	if (!si)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	ci = lock_cluster_or_swap_info(si, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	if (size == SWAPFILE_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		VM_BUG_ON(!cluster_is_huge(ci));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		map = si->swap_map + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 		for (i = 0; i < SWAPFILE_CLUSTER; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 			val = map[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 			VM_BUG_ON(!(val & SWAP_HAS_CACHE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 			if (val == SWAP_HAS_CACHE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 				free_entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		cluster_clear_huge(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		if (free_entries == SWAPFILE_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 			unlock_cluster_or_swap_info(si, ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 			spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 			mem_cgroup_uncharge_swap(entry, SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 			swap_free_cluster(si, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 			spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	for (i = 0; i < size; i++, entry.val++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 			unlock_cluster_or_swap_info(si, ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 			free_swap_slot(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 			if (i == size - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 			lock_cluster_or_swap_info(si, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	unlock_cluster_or_swap_info(si, ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) #ifdef CONFIG_THP_SWAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) int split_swap_cluster(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	struct swap_info_struct *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	unsigned long offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	si = _swap_info_get(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	if (!si)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	ci = lock_cluster(si, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	cluster_clear_huge(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	unlock_cluster(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) static int swp_entry_cmp(const void *ent1, const void *ent2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	const swp_entry_t *e1 = ent1, *e2 = ent2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	return (int)swp_type(*e1) - (int)swp_type(*e2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) void swapcache_free_entries(swp_entry_t *entries, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	struct swap_info_struct *p, *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	if (n <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	 * Sort swap entries by swap device, so each lock is only taken once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	 * nr_swapfiles isn't absolutely correct, but the overhead of sort() is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	 * so low that it isn't necessary to optimize further.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	if (nr_swapfiles > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	for (i = 0; i < n; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		p = swap_info_get_cont(entries[i], prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 		if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 			swap_entry_free(p, entries[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		prev = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		spin_unlock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) EXPORT_SYMBOL_GPL(swapcache_free_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494)  * How many references to page are currently swapped out?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)  * This does not give an exact answer when swap count is continued,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496)  * but does include the high COUNT_CONTINUED flag to allow for that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) int page_swapcount(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	struct swap_info_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	swp_entry_t entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	entry.val = page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	p = _swap_info_get(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		ci = lock_cluster_or_swap_info(p, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 		count = swap_count(p->swap_map[offset]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		unlock_cluster_or_swap_info(p, ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) int __swap_count(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	struct swap_info_struct *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	pgoff_t offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	si = get_swap_device(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	if (si) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 		count = swap_count(si->swap_map[offset]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 		put_swap_device(si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	pgoff_t offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	ci = lock_cluster_or_swap_info(si, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	count = swap_count(si->swap_map[offset]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	unlock_cluster_or_swap_info(si, ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)  * How many references to @entry are currently swapped out?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545)  * This does not give an exact answer when swap count is continued,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546)  * but does include the high COUNT_CONTINUED flag to allow for that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) int __swp_swapcount(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	struct swap_info_struct *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	si = get_swap_device(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	if (si) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		count = swap_swapcount(si, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		put_swap_device(si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562)  * How many references to @entry are currently swapped out?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563)  * This considers COUNT_CONTINUED so it returns exact answer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) int swp_swapcount(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	int count, tmp_count, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	struct swap_info_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	pgoff_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	unsigned char *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	p = _swap_info_get(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	ci = lock_cluster_or_swap_info(p, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	count = swap_count(p->swap_map[offset]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	if (!(count & COUNT_CONTINUED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	count &= ~COUNT_CONTINUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	n = SWAP_MAP_MAX + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	page = vmalloc_to_page(p->swap_map + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	offset &= ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	VM_BUG_ON(page_private(page) != SWP_CONTINUED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		page = list_next_entry(page, lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		map = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 		tmp_count = map[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		kunmap_atomic(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		count += (tmp_count & ~COUNT_CONTINUED) * n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 		n *= (SWAP_CONT_MAX + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	} while (tmp_count & COUNT_CONTINUED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	unlock_cluster_or_swap_info(p, ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) static bool swap_page_trans_huge_swapped(struct swap_info_struct *si,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 					 swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	unsigned char *map = si->swap_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	unsigned long roffset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	unsigned long offset = round_down(roffset, SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	ci = lock_cluster_or_swap_info(si, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	if (!ci || !cluster_is_huge(ci)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		if (swap_count(map[roffset]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 			ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		goto unlock_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	for (i = 0; i < SWAPFILE_CLUSTER; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 		if (swap_count(map[offset + i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 			ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) unlock_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	unlock_cluster_or_swap_info(si, ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) static bool page_swapped(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	swp_entry_t entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	struct swap_info_struct *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!PageTransCompound(page)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		return page_swapcount(page) != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	page = compound_head(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	entry.val = page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	si = _swap_info_get(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	if (si)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 		return swap_page_trans_huge_swapped(si, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) static int page_trans_huge_map_swapcount(struct page *page, int *total_mapcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 					 int *total_swapcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	int i, map_swapcount, _total_mapcount, _total_swapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	unsigned long offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	struct swap_info_struct *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	struct swap_cluster_info *ci = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	unsigned char *map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	int mapcount, swapcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	/* hugetlbfs shouldn't call it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	VM_BUG_ON_PAGE(PageHuge(page), page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!PageTransCompound(page))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		mapcount = page_trans_huge_mapcount(page, total_mapcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 		if (PageSwapCache(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 			swapcount = page_swapcount(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 		if (total_swapcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 			*total_swapcount = swapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		return mapcount + swapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	page = compound_head(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	_total_mapcount = _total_swapcount = map_swapcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	if (PageSwapCache(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		swp_entry_t entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		entry.val = page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		si = _swap_info_get(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		if (si) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 			map = si->swap_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 			offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	if (map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		ci = lock_cluster(si, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	for (i = 0; i < HPAGE_PMD_NR; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 		mapcount = atomic_read(&page[i]._mapcount) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 		_total_mapcount += mapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 		if (map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 			swapcount = swap_count(map[offset + i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 			_total_swapcount += swapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 		map_swapcount = max(map_swapcount, mapcount + swapcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	unlock_cluster(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	if (PageDoubleMap(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		map_swapcount -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 		_total_mapcount -= HPAGE_PMD_NR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	mapcount = compound_mapcount(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	map_swapcount += mapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	_total_mapcount += mapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	if (total_mapcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 		*total_mapcount = _total_mapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	if (total_swapcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 		*total_swapcount = _total_swapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	return map_swapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)  * We can write to an anon page without COW if there are no other references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714)  * to it.  And as a side-effect, free up its swap: because the old content
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715)  * on disk will never be read, and seeking back there to write new content
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716)  * later would only waste time away from clustering.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718)  * NOTE: total_map_swapcount should not be relied upon by the caller if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719)  * reuse_swap_page() returns false, but it may be always overwritten
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720)  * (see the other implementation for CONFIG_SWAP=n).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) bool reuse_swap_page(struct page *page, int *total_map_swapcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	int count, total_mapcount, total_swapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	VM_BUG_ON_PAGE(!PageLocked(page), page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	if (unlikely(PageKsm(page)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	count = page_trans_huge_map_swapcount(page, &total_mapcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 					      &total_swapcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	if (total_map_swapcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		*total_map_swapcount = total_mapcount + total_swapcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	if (count == 1 && PageSwapCache(page) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	    (likely(!PageTransCompound(page)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	     /* The remaining swap count will be freed soon */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	     total_swapcount == page_swapcount(page))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		if (!PageWriteback(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 			page = compound_head(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 			delete_from_swap_cache(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 			SetPageDirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 			swp_entry_t entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 			struct swap_info_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 			entry.val = page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 			p = swap_info_get(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 			if (p->flags & SWP_STABLE_WRITES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 				spin_unlock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 				return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 			spin_unlock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	return count <= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759)  * If swap is getting full, or if there are no more mappings of this page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760)  * then try_to_free_swap is called to free its swap space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) int try_to_free_swap(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	VM_BUG_ON_PAGE(!PageLocked(page), page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	if (!PageSwapCache(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	if (PageWriteback(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	if (page_swapped(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	 * Once hibernation has begun to create its image of memory,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	 * there's a danger that one of the calls to try_to_free_swap()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	 * - most probably a call from __try_to_reclaim_swap() while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	 * hibernation is allocating its own swap pages for the image,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	 * but conceivably even a call from memory reclaim - will free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	 * the swap from a page which has already been recorded in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	 * image as a clean swapcache page, and then reuse its swap for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	 * another page of the image.  On waking from hibernation, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	 * original page might be freed under memory pressure, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	 * later read back in from swap, now with the wrong data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	 * Hibernation suspends storage while it is writing the image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	 * to disk so check that here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	if (pm_suspended_storage())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	page = compound_head(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	delete_from_swap_cache(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	SetPageDirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798)  * Free the swap entry like above, but also try to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799)  * free the page cache entry if it is the last user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) int free_swap_and_cache(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	struct swap_info_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	unsigned char count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	if (non_swap_entry(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	p = _swap_info_get(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 		count = __swap_entry_free(p, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 		if (count == SWAP_HAS_CACHE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		    !swap_page_trans_huge_swapped(p, entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 			__try_to_reclaim_swap(p, swp_offset(entry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 					      TTRS_UNMAPPED | TTRS_FULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	return p != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) #ifdef CONFIG_HIBERNATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822)  * Find the swap type that corresponds to given device (if any).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)  * @offset - number of the PAGE_SIZE-sized block of the device, starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825)  * from 0, in which the swap header is expected to be located.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827)  * This is needed for the suspend to disk (aka swsusp).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) int swap_type_of(dev_t device, sector_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 	int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	if (!device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	for (type = 0; type < nr_swapfiles; type++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		struct swap_info_struct *sis = swap_info[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		if (!(sis->flags & SWP_WRITEOK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 		if (device == sis->bdev->bd_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 			struct swap_extent *se = first_se(sis);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 			if (se->start_block == offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 				spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 				return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) int find_first_swap(dev_t *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	for (type = 0; type < nr_swapfiles; type++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 		struct swap_info_struct *sis = swap_info[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 		if (!(sis->flags & SWP_WRITEOK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 		*device = sis->bdev->bd_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 		spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 		return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875)  * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876)  * corresponding to given index in swap_info (swap type).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) sector_t swapdev_block(int type, pgoff_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	struct swap_info_struct *si = swap_type_to_swap_info(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	if (!si || !(si->flags & SWP_WRITEOK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	return map_swap_entry(swp_entry(type, offset), &bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889)  * Return either the total number of swap pages of given type, or the number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890)  * of free pages of that type (depending on @free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892)  * This is needed for software suspend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) unsigned int count_swap_pages(int type, int free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	unsigned int n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	if ((unsigned int)type < nr_swapfiles) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 		struct swap_info_struct *sis = swap_info[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 		spin_lock(&sis->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 		if (sis->flags & SWP_WRITEOK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 			n = sis->pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 			if (free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 				n -= sis->inuse_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 		spin_unlock(&sis->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) #endif /* CONFIG_HIBERNATION */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	return pte_same(pte_swp_clear_flags(pte), swp_pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921)  * No need to decide whether this PTE shares the swap entry with others,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922)  * just let do_wp_page work it out if a write is requested later - to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923)  * force COW, vm_page_prot omits write permission from any private vma.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 		unsigned long addr, swp_entry_t entry, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	struct page *swapcache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	spinlock_t *ptl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	int ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	swapcache = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	page = ksm_might_need_to_copy(page, vma, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	if (unlikely(!page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	if (unlikely(!pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	get_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	set_pte_at(vma->vm_mm, addr, pte,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 		   pte_mkold(mk_pte(page, vma->vm_page_prot)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 	if (page == swapcache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 		page_add_anon_rmap(page, vma, addr, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	} else { /* ksm created a completely new copy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 		page_add_new_anon_rmap(page, vma, addr, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 		lru_cache_add_inactive_or_unevictable(page, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	swap_free(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	pte_unmap_unlock(pte, ptl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	if (page != swapcache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 			unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 			unsigned int type, bool frontswap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 			unsigned long *fs_pages_to_unuse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	swp_entry_t entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	struct swap_info_struct *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	volatile unsigned char *swap_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	si = swap_info[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	pte = pte_offset_map(pmd, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 		if (!is_swap_pte(*pte))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 		entry = pte_to_swp_entry(*pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 		if (swp_type(entry) != type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 		offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 		if (frontswap && !frontswap_test(si, offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		pte_unmap(pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		swap_map = &si->swap_map[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 		page = lookup_swap_cache(entry, vma, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 		if (!page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 			struct vm_fault vmf = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 				.vma = vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 				.address = addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 				.pmd = pmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 			};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 			page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 						&vmf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		if (!page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 			if (*swap_map == 0 || *swap_map == SWAP_MAP_BAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 				goto try_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 		lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 		wait_on_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 		ret = unuse_pte(vma, pmd, addr, entry, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 			put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 		try_to_free_swap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		trace_android_vh_unuse_swap_page(si, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 		if (*fs_pages_to_unuse && !--(*fs_pages_to_unuse)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 			ret = FRONTSWAP_PAGES_UNUSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) try_next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 		pte = pte_offset_map(pmd, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	} while (pte++, addr += PAGE_SIZE, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	pte_unmap(pte - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 				unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 				unsigned int type, bool frontswap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 				unsigned long *fs_pages_to_unuse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	pmd_t *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	pmd = pmd_offset(pud, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 		next = pmd_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 		if (pmd_none_or_trans_huge_or_clear_bad(pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 		ret = unuse_pte_range(vma, pmd, addr, next, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 				      frontswap, fs_pages_to_unuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	} while (pmd++, addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 				unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 				unsigned int type, bool frontswap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 				unsigned long *fs_pages_to_unuse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	pud_t *pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	pud = pud_offset(p4d, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 		next = pud_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 		if (pud_none_or_clear_bad(pud))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 		ret = unuse_pmd_range(vma, pud, addr, next, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 				      frontswap, fs_pages_to_unuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	} while (pud++, addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) static inline int unuse_p4d_range(struct vm_area_struct *vma, pgd_t *pgd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 				unsigned long addr, unsigned long end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 				unsigned int type, bool frontswap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 				unsigned long *fs_pages_to_unuse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 	p4d_t *p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 	unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	p4d = p4d_offset(pgd, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 		next = p4d_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 		if (p4d_none_or_clear_bad(p4d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 		ret = unuse_pud_range(vma, p4d, addr, next, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 				      frontswap, fs_pages_to_unuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	} while (p4d++, addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) static int unuse_vma(struct vm_area_struct *vma, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 		     bool frontswap, unsigned long *fs_pages_to_unuse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	pgd_t *pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	unsigned long addr, end, next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	addr = vma->vm_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	end = vma->vm_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	pgd = pgd_offset(vma->vm_mm, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 		next = pgd_addr_end(addr, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 		if (pgd_none_or_clear_bad(pgd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 		ret = unuse_p4d_range(vma, pgd, addr, next, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 				      frontswap, fs_pages_to_unuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	} while (pgd++, addr = next, addr != end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) static int unuse_mm(struct mm_struct *mm, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 		    bool frontswap, unsigned long *fs_pages_to_unuse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	struct vm_area_struct *vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	mmap_read_lock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 		if (vma->anon_vma) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 			ret = unuse_vma(vma, type, frontswap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 					fs_pages_to_unuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	mmap_read_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150)  * Scan swap_map (or frontswap_map if frontswap parameter is true)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151)  * from current position to next entry still in use. Return 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152)  * if there are no inuse entries after prev till end of the map.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) static unsigned int find_next_to_unuse(struct swap_info_struct *si,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 					unsigned int prev, bool frontswap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	unsigned char count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	 * No need for swap_lock here: we're just looking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	 * for whether an entry is in use, not modifying it; false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 	 * hits are okay, and sys_swapoff() has already prevented new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 	 * allocations from this area (while holding swap_lock).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	for (i = prev + 1; i < si->max; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 		count = READ_ONCE(si->swap_map[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 		if (count && swap_count(count) != SWAP_MAP_BAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 			if (!frontswap || frontswap_test(si, i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 		if ((i % LATENCY_LIMIT) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 			cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	if (i == si->max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 		i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182)  * If the boolean frontswap is true, only unuse pages_to_unuse pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183)  * pages_to_unuse==0 means all pages; ignored if frontswap is false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) int try_to_unuse(unsigned int type, bool frontswap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 		 unsigned long pages_to_unuse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	struct mm_struct *prev_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 	struct mm_struct *mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	struct list_head *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 	struct swap_info_struct *si = swap_info[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 	swp_entry_t entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	if (!READ_ONCE(si->inuse_pages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	if (!frontswap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 		pages_to_unuse = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 	retval = shmem_unuse(type, frontswap, &pages_to_unuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	prev_mm = &init_mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 	mmget(prev_mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 	spin_lock(&mmlist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	p = &init_mm.mmlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	while (READ_ONCE(si->inuse_pages) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	       !signal_pending(current) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 	       (p = p->next) != &init_mm.mmlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 		mm = list_entry(p, struct mm_struct, mmlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 		if (!mmget_not_zero(mm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 		spin_unlock(&mmlist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 		mmput(prev_mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 		prev_mm = mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 		retval = unuse_mm(mm, type, frontswap, &pages_to_unuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 		if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 			mmput(prev_mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 		 * Make sure that we aren't completely killing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 		 * interactive performance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 		spin_lock(&mmlist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 	spin_unlock(&mmlist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 	mmput(prev_mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 	while (READ_ONCE(si->inuse_pages) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 	       !signal_pending(current) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	       (i = find_next_to_unuse(si, i, frontswap)) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 		entry = swp_entry(type, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 		page = find_get_page(swap_address_space(entry), i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 		 * It is conceivable that a racing task removed this page from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 		 * swap cache just before we acquired the page lock. The page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 		 * might even be back in swap cache on another swap area. But
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 		 * that is okay, try_to_free_swap() only removes stale pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 		lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 		wait_on_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 		try_to_free_swap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 		trace_android_vh_unuse_swap_page(si, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 		 * For frontswap, we just need to unuse pages_to_unuse, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		 * it was specified. Need not check frontswap again here as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 		 * we already zeroed out pages_to_unuse if not frontswap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 		if (pages_to_unuse && --pages_to_unuse == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 	 * Lets check again to see if there are still swap entries in the map.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 	 * If yes, we would need to do retry the unuse logic again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 	 * Under global memory pressure, swap entries can be reinserted back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 	 * into process space after the mmlist loop above passes over them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	 * Limit the number of retries? No: when mmget_not_zero() above fails,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 	 * that mm is likely to be freeing swap from exit_mmap(), which proceeds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 	 * at its own independent pace; and even shmem_writepage() could have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	 * been preempted after get_swap_page(), temporarily hiding that swap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 	 * It's easy and robust (though cpu-intensive) just to keep retrying.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	if (READ_ONCE(si->inuse_pages)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 		if (!signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 		retval = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 	return (retval == FRONTSWAP_PAGES_UNUSED) ? 0 : retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295)  * After a successful try_to_unuse, if no swap is now in use, we know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296)  * we can empty the mmlist.  swap_lock must be held on entry and exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297)  * Note that mmlist_lock nests inside swap_lock, and an mm must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298)  * added to the mmlist just after page_duplicate - before would be racy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) static void drain_mmlist(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 	struct list_head *p, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 	unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 	for (type = 0; type < nr_swapfiles; type++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 		if (swap_info[type]->inuse_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 	spin_lock(&mmlist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	list_for_each_safe(p, next, &init_mm.mmlist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 		list_del_init(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	spin_unlock(&mmlist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315)  * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316)  * corresponds to page offset for the specified swap entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317)  * Note that the type of this function is sector_t, but it returns page offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318)  * into the bdev, not sector offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	struct swap_info_struct *sis;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	struct swap_extent *se;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	pgoff_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 	sis = swp_swap_info(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	*bdev = sis->bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 	offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 	se = offset_to_swap_extent(sis, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	return se->start_block + (offset - se->start_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335)  * Returns the page offset into bdev for the specified page's swap entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) sector_t map_swap_page(struct page *page, struct block_device **bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 	swp_entry_t entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 	entry.val = page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	return map_swap_entry(entry, bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345)  * Free all of a swapdev's extent information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) static void destroy_swap_extents(struct swap_info_struct *sis)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	while (!RB_EMPTY_ROOT(&sis->swap_extent_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 		struct rb_node *rb = sis->swap_extent_root.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 		struct swap_extent *se = rb_entry(rb, struct swap_extent, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 		rb_erase(rb, &sis->swap_extent_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 		kfree(se);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 	if (sis->flags & SWP_ACTIVATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 		struct file *swap_file = sis->swap_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 		struct address_space *mapping = swap_file->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 		sis->flags &= ~SWP_ACTIVATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 		if (mapping->a_ops->swap_deactivate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 			mapping->a_ops->swap_deactivate(swap_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368)  * Add a block range (and the corresponding page range) into this swapdev's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369)  * extent tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371)  * This function rather assumes that it is called in ascending page order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 		unsigned long nr_pages, sector_t start_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	struct rb_node **link = &sis->swap_extent_root.rb_node, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 	struct swap_extent *se;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 	struct swap_extent *new_se;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 	 * place the new node at the right most since the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 	 * function is called in ascending page order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	while (*link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 		parent = *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 		link = &parent->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 	if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 		se = rb_entry(parent, struct swap_extent, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 		BUG_ON(se->start_page + se->nr_pages != start_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 		if (se->start_block + se->nr_pages == start_block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 			/* Merge it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 			se->nr_pages += nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 	/* No merge, insert a new extent. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 	new_se = kmalloc(sizeof(*se), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 	if (new_se == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	new_se->start_page = start_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	new_se->nr_pages = nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 	new_se->start_block = start_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	rb_link_node(&new_se->rb_node, parent, link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	rb_insert_color(&new_se->rb_node, &sis->swap_extent_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) EXPORT_SYMBOL_GPL(add_swap_extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415)  * A `swap extent' is a simple thing which maps a contiguous range of pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416)  * onto a contiguous range of disk blocks.  An ordered list of swap extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417)  * is built at swapon time and is then used at swap_writepage/swap_readpage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418)  * time for locating where on disk a page belongs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420)  * If the swapfile is an S_ISBLK block device, a single extent is installed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421)  * This is done so that the main operating code can treat S_ISBLK and S_ISREG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422)  * swap files identically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424)  * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425)  * extent list operates in PAGE_SIZE disk blocks.  Both S_ISREG and S_ISBLK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426)  * swapfiles are handled *identically* after swapon time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428)  * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429)  * and will parse them into an ordered extent list, in PAGE_SIZE chunks.  If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430)  * some stray blocks are found which do not fall within the PAGE_SIZE alignment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431)  * requirements, they are simply tossed out - we will never use those blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432)  * for swapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434)  * For all swap devices we set S_SWAPFILE across the life of the swapon.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435)  * prevents users from writing to the swap device, which will corrupt memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437)  * The amount of disk space which a single swap extent represents varies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438)  * Typically it is in the 1-4 megabyte range.  So we can have hundreds of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439)  * extents in the list.  To avoid much list walking, we cache the previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440)  * search location in `curr_swap_extent', and start new searches from there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441)  * This is extremely effective.  The average number of iterations in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442)  * map_swap_page() has been measured at about 0.3 per page.  - akpm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 	struct file *swap_file = sis->swap_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 	struct address_space *mapping = swap_file->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	if (S_ISBLK(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 		ret = add_swap_extent(sis, 0, sis->max, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 		*span = sis->pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 	if (mapping->a_ops->swap_activate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 		ret = mapping->a_ops->swap_activate(sis, swap_file, span);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 		if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 			sis->flags |= SWP_ACTIVATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 			sis->flags |= SWP_FS_OPS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 			ret = add_swap_extent(sis, 0, sis->max, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 			*span = sis->pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	return generic_swapfile_activate(sis, swap_file, span);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) static int swap_node(struct swap_info_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 	struct block_device *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	if (p->bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 		bdev = p->bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 		bdev = p->swap_file->f_inode->i_sb->s_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 	return bdev ? bdev->bd_disk->node_id : NUMA_NO_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) static void setup_swap_info(struct swap_info_struct *p, int prio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 			    unsigned char *swap_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 			    struct swap_cluster_info *cluster_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	if (prio >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 		p->prio = prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 		p->prio = --least_priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	 * the plist prio is negated because plist ordering is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	 * low-to-high, while swap ordering is high-to-low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 	p->list.prio = -p->prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 	for_each_node(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 		if (p->prio >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 			p->avail_lists[i].prio = -p->prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 			if (swap_node(p) == i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 				p->avail_lists[i].prio = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 				p->avail_lists[i].prio = -p->prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	p->swap_map = swap_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	p->cluster_info = cluster_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) static void _enable_swap_info(struct swap_info_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 	bool skip = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	p->flags |= SWP_WRITEOK | SWP_VALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 	trace_android_vh_account_swap_pages(p, &skip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 	if (!skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 		atomic_long_add(p->pages, &nr_swap_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 		total_swap_pages += p->pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	assert_spin_locked(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 	 * both lists are plists, and thus priority ordered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 	 * swap_active_head needs to be priority ordered for swapoff(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	 * which on removal of any swap_info_struct with an auto-assigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 	 * (i.e. negative) priority increments the auto-assigned priority
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 	 * of any lower-priority swap_info_structs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	 * swap_avail_head needs to be priority ordered for get_swap_page(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	 * which allocates swap pages from the highest available priority
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	 * swap_info_struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	plist_add(&p->list, &swap_active_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 	add_to_avail_list(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) static void enable_swap_info(struct swap_info_struct *p, int prio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 				unsigned char *swap_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 				struct swap_cluster_info *cluster_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 				unsigned long *frontswap_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 	frontswap_init(p->type, frontswap_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 	spin_lock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 	setup_swap_info(p, prio, swap_map, cluster_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 	spin_unlock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	 * Guarantee swap_map, cluster_info, etc. fields are valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 	 * between get/put_swap_device() if SWP_VALID bit is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 	spin_lock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 	_enable_swap_info(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 	spin_unlock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) static void reinsert_swap_info(struct swap_info_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 	spin_lock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 	setup_swap_info(p, p->prio, p->swap_map, p->cluster_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 	_enable_swap_info(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 	spin_unlock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) bool has_usable_swap(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 	bool ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 	if (plist_head_empty(&swap_active_head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 		ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 	struct swap_info_struct *p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 	unsigned char *swap_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 	struct swap_cluster_info *cluster_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 	unsigned long *frontswap_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 	struct file *swap_file, *victim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 	struct address_space *mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 	struct filename *pathname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 	int err, found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 	unsigned int old_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	bool skip = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	BUG_ON(!current->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	pathname = getname(specialfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	if (IS_ERR(pathname))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 		return PTR_ERR(pathname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 	victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 	err = PTR_ERR(victim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 	if (IS_ERR(victim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 	mapping = victim->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 	plist_for_each_entry(p, &swap_active_head, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 		if (p->flags & SWP_WRITEOK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 			if (p->swap_file->f_mapping == mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 				found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 	if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 		spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 		goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 	if (!security_vm_enough_memory_mm(current->mm, p->pages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 		vm_unacct_memory(p->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 		spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 		goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 	del_from_avail_list(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 	spin_lock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 	if (p->prio < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 		struct swap_info_struct *si = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 		int nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 		plist_for_each_entry_continue(si, &swap_active_head, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 			si->prio++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 			si->list.prio--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 			for_each_node(nid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 				if (si->avail_lists[nid].prio != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 					si->avail_lists[nid].prio--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 		least_priority++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 	plist_del(&p->list, &swap_active_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 	trace_android_vh_account_swap_pages(p, &skip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 	if (!skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 		atomic_long_sub(p->pages, &nr_swap_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 		total_swap_pages -= p->pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 	p->flags &= ~SWP_WRITEOK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 	spin_unlock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 	disable_swap_slots_cache_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 	set_current_oom_origin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 	err = try_to_unuse(p->type, false, 0); /* force unuse all pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 	clear_current_oom_origin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 		/* re-insert swap space back into swap_list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 		reinsert_swap_info(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 		reenable_swap_slots_cache_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 		goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 	reenable_swap_slots_cache_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 	spin_lock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 	p->flags &= ~SWP_VALID;		/* mark swap device as invalid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 	spin_unlock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 	 * wait for swap operations protected by get/put_swap_device()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 	 * to complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	flush_work(&p->discard_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	destroy_swap_extents(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 	if (p->flags & SWP_CONTINUED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 		free_swap_count_continuations(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 	if (!p->bdev || !blk_queue_nonrot(bdev_get_queue(p->bdev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 		atomic_dec(&nr_rotate_swap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 	mutex_lock(&swapon_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 	spin_lock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 	drain_mmlist();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	/* wait for anyone still in scan_swap_map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	p->highest_bit = 0;		/* cuts scans short */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 	while (p->flags >= SWP_SCANNING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 		spin_unlock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 		spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 		schedule_timeout_uninterruptible(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 		spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 		spin_lock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 	swap_file = p->swap_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 	old_block_size = p->old_block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 	p->swap_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 	p->max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 	swap_map = p->swap_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 	p->swap_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 	cluster_info = p->cluster_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 	p->cluster_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 	frontswap_map = frontswap_map_get(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 	spin_unlock(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 	arch_swap_invalidate_area(p->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 	frontswap_invalidate_area(p->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 	frontswap_map_set(p, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 	mutex_unlock(&swapon_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 	free_percpu(p->percpu_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 	p->percpu_cluster = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 	free_percpu(p->cluster_next_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 	p->cluster_next_cpu = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 	vfree(swap_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 	kvfree(cluster_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 	kvfree(frontswap_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	/* Destroy swap account information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	swap_cgroup_swapoff(p->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 	exit_swap_address_space(p->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 	inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 	if (S_ISBLK(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 		struct block_device *bdev = I_BDEV(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 		set_blocksize(bdev, old_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 		blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 	inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 	inode->i_flags &= ~S_SWAPFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 	filp_close(swap_file, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 	 * Clear the SWP_USED flag after all resources are freed so that swapon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 	 * can reuse this swap_info in alloc_swap_info() safely.  It is ok to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 	 * not hold p->lock after we cleared its SWP_WRITEOK.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 	p->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 	atomic_inc(&proc_poll_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 	wake_up_interruptible(&proc_poll_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) out_dput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 	filp_close(victim, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	putname(pathname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) static __poll_t swaps_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	struct seq_file *seq = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 	poll_wait(file, &proc_poll_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 	if (seq->poll_event != atomic_read(&proc_poll_event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 		seq->poll_event = atomic_read(&proc_poll_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 		return EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 	return EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) /* iterator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) static void *swap_start(struct seq_file *swap, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 	struct swap_info_struct *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 	int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 	loff_t l = *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 	mutex_lock(&swapon_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 	if (!l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 		return SEQ_START_TOKEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 	for (type = 0; (si = swap_type_to_swap_info(type)); type++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 		if (!(si->flags & SWP_USED) || !si->swap_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 		if (!--l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 			return si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 	struct swap_info_struct *si = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 	int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 	if (v == SEQ_START_TOKEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 		type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 		type = si->type + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 	++(*pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	for (; (si = swap_type_to_swap_info(type)); type++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 		if (!(si->flags & SWP_USED) || !si->swap_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 		return si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) static void swap_stop(struct seq_file *swap, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 	mutex_unlock(&swapon_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) static int swap_show(struct seq_file *swap, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 	struct swap_info_struct *si = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 	struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 	unsigned int bytes, inuse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 	if (si == SEQ_START_TOKEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 		seq_puts(swap,"Filename\t\t\t\tType\t\tSize\t\tUsed\t\tPriority\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 	bytes = si->pages << (PAGE_SHIFT - 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 	inuse = si->inuse_pages << (PAGE_SHIFT - 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 	file = si->swap_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 	len = seq_file_path(swap, file, " \t\n\\");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 	seq_printf(swap, "%*s%s\t%u\t%s%u\t%s%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 			len < 40 ? 40 - len : 1, " ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 			S_ISBLK(file_inode(file)->i_mode) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 				"partition" : "file\t",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 			bytes, bytes < 10000000 ? "\t" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 			inuse, inuse < 10000000 ? "\t" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 			si->prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) static const struct seq_operations swaps_op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 	.start =	swap_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 	.next =		swap_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 	.stop =		swap_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 	.show =		swap_show
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) static int swaps_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 	struct seq_file *seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 	ret = seq_open(file, &swaps_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 	seq = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 	seq->poll_event = atomic_read(&proc_poll_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) static const struct proc_ops swaps_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 	.proc_flags	= PROC_ENTRY_PERMANENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 	.proc_open	= swaps_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 	.proc_read	= seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 	.proc_lseek	= seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 	.proc_release	= seq_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 	.proc_poll	= swaps_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) static int __init procswaps_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 	proc_create("swaps", 0, NULL, &swaps_proc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) __initcall(procswaps_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) #endif /* CONFIG_PROC_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) #ifdef MAX_SWAPFILES_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) static int __init max_swapfiles_check(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 	MAX_SWAPFILES_CHECK();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) late_initcall(max_swapfiles_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) static struct swap_info_struct *alloc_swap_info(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 	struct swap_info_struct *p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 	struct swap_info_struct *defer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 	unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 	bool skip = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 	trace_android_vh_alloc_si(&p, &skip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 	if (!skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 		p = kvzalloc(struct_size(p, avail_lists, nr_node_ids), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 	for (type = 0; type < nr_swapfiles; type++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 		if (!(swap_info[type]->flags & SWP_USED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 	if (type >= MAX_SWAPFILES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 		spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 		kvfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 		return ERR_PTR(-EPERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 	if (type >= nr_swapfiles) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 		p->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 		WRITE_ONCE(swap_info[type], p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 		 * Write swap_info[type] before nr_swapfiles, in case a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 		 * racing procfs swap_start() or swap_next() is reading them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 		 * (We never shrink nr_swapfiles, we never free this entry.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 		smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 		WRITE_ONCE(nr_swapfiles, nr_swapfiles + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 		defer = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 		p = swap_info[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 		 * Do not memset this entry: a racing procfs swap_next()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 		 * would be relying on p->type to remain valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 	p->swap_extent_root = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 	plist_node_init(&p->list, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 	for_each_node(i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 		plist_node_init(&p->avail_lists[i], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 	p->flags = SWP_USED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 	kvfree(defer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 	spin_lock_init(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 	spin_lock_init(&p->cont_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 	return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 	if (S_ISBLK(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 		p->bdev = blkdev_get_by_dev(inode->i_rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 				   FMODE_READ | FMODE_WRITE | FMODE_EXCL, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 		if (IS_ERR(p->bdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 			error = PTR_ERR(p->bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 			p->bdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 		p->old_block_size = block_size(p->bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 		error = set_blocksize(p->bdev, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 		if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 		 * Zoned block devices contain zones that have a sequential
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 		 * write only restriction.  Hence zoned block devices are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 		 * suitable for swapping.  Disallow them here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 		if (blk_queue_is_zoned(p->bdev->bd_disk->queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 		p->flags |= SWP_BLKDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 	} else if (S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 		p->bdev = inode->i_sb->s_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991)  * Find out how many pages are allowed for a single swap device. There
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992)  * are two limiting factors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993)  * 1) the number of bits for the swap offset in the swp_entry_t type, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994)  * 2) the number of bits in the swap pte, as defined by the different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995)  * architectures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997)  * In order to find the largest possible bit mask, a swap entry with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998)  * swap type 0 and swap offset ~0UL is created, encoded to a swap pte,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999)  * decoded to a swp_entry_t again, and finally the swap offset is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000)  * extracted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002)  * This will mask all the bits from the initial ~0UL mask that can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003)  * be encoded in either the swp_entry_t or the architecture definition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004)  * of a swap pte.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) unsigned long generic_max_swapfile_size(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 	return swp_offset(pte_to_swp_entry(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 			swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) /* Can be overridden by an architecture for additional checks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) __weak unsigned long max_swapfile_size(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 	return generic_max_swapfile_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) static unsigned long read_swap_header(struct swap_info_struct *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 					union swap_header *swap_header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 					struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 	unsigned long maxpages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 	unsigned long swapfilepages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 	unsigned long last_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 	if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 		pr_err("Unable to find swap-space signature\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 	/* swap partition endianess hack... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 	if (swab32(swap_header->info.version) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 		swab32s(&swap_header->info.version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 		swab32s(&swap_header->info.last_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 		swab32s(&swap_header->info.nr_badpages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 		if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 		for (i = 0; i < swap_header->info.nr_badpages; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 			swab32s(&swap_header->info.badpages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 	/* Check the swap header's sub-version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 	if (swap_header->info.version != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 		pr_warn("Unable to handle swap header version %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 			swap_header->info.version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 	p->lowest_bit  = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 	p->cluster_next = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 	p->cluster_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 	maxpages = max_swapfile_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 	last_page = swap_header->info.last_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 	if (!last_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 		pr_warn("Empty swap-file\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 	if (last_page > maxpages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 		pr_warn("Truncating oversized swap area, only using %luk out of %luk\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 			maxpages << (PAGE_SHIFT - 10),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 			last_page << (PAGE_SHIFT - 10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 	if (maxpages > last_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 		maxpages = last_page + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 		/* p->max is an unsigned int: don't overflow it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 		if ((unsigned int)maxpages == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 			maxpages = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 	p->highest_bit = maxpages - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 	if (!maxpages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 	swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 	if (swapfilepages && maxpages > swapfilepages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 		pr_warn("Swap area shorter than signature indicates\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 	if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 	if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 	return maxpages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) #define SWAP_CLUSTER_INFO_COLS						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 	DIV_ROUND_UP(L1_CACHE_BYTES, sizeof(struct swap_cluster_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) #define SWAP_CLUSTER_SPACE_COLS						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 	DIV_ROUND_UP(SWAP_ADDRESS_SPACE_PAGES, SWAPFILE_CLUSTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) #define SWAP_CLUSTER_COLS						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 	max_t(unsigned int, SWAP_CLUSTER_INFO_COLS, SWAP_CLUSTER_SPACE_COLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) static int setup_swap_map_and_extents(struct swap_info_struct *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 					union swap_header *swap_header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 					unsigned char *swap_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 					struct swap_cluster_info *cluster_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 					unsigned long maxpages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 					sector_t *span)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 	unsigned int j, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 	unsigned int nr_good_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 	int nr_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 	unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 	unsigned long col = p->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_COLS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 	unsigned long i, idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 	nr_good_pages = maxpages - 1;	/* omit header page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 	cluster_list_init(&p->free_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 	cluster_list_init(&p->discard_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 	for (i = 0; i < swap_header->info.nr_badpages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 		unsigned int page_nr = swap_header->info.badpages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 		if (page_nr == 0 || page_nr > swap_header->info.last_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 		if (page_nr < maxpages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 			swap_map[page_nr] = SWAP_MAP_BAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 			nr_good_pages--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 			 * Haven't marked the cluster free yet, no list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 			 * operation involved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) 			inc_cluster_info_page(p, cluster_info, page_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) 	/* Haven't marked the cluster free yet, no list operation involved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 	for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 		inc_cluster_info_page(p, cluster_info, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 	if (nr_good_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 		swap_map[0] = SWAP_MAP_BAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 		 * Not mark the cluster free yet, no list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 		 * operation involved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 		inc_cluster_info_page(p, cluster_info, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 		p->max = maxpages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 		p->pages = nr_good_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) 		nr_extents = setup_swap_extents(p, span);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 		if (nr_extents < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 			return nr_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 		nr_good_pages = p->pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 	if (!nr_good_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 		pr_warn("Empty swap-file\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 	if (!cluster_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 		return nr_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 	 * Reduce false cache line sharing between cluster_info and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 	 * sharing same address space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 	for (k = 0; k < SWAP_CLUSTER_COLS; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) 		j = (k + col) % SWAP_CLUSTER_COLS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 		for (i = 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 			idx = i * SWAP_CLUSTER_COLS + j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 			if (idx >= nr_clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 			if (cluster_count(&cluster_info[idx]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 			cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 			cluster_list_add_tail(&p->free_clusters, cluster_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 					      idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) 	return nr_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176)  * Helper to sys_swapon determining if a given swap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177)  * backing device queue supports DISCARD operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) static bool swap_discardable(struct swap_info_struct *si)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 	struct request_queue *q = bdev_get_queue(si->bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 	if (!q || !blk_queue_discard(q))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 	struct swap_info_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 	struct filename *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 	struct file *swap_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 	struct address_space *mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 	int prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 	union swap_header *swap_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 	int nr_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 	sector_t span;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 	unsigned long maxpages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 	unsigned char *swap_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 	struct swap_cluster_info *cluster_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 	unsigned long *frontswap_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) 	struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 	struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 	bool inced_nr_rotate_swap = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 	if (swap_flags & ~SWAP_FLAGS_VALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 	if (!swap_avail_heads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 	p = alloc_swap_info();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 	if (IS_ERR(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 		return PTR_ERR(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 	INIT_WORK(&p->discard_work, swap_discard_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 	name = getname(specialfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 	if (IS_ERR(name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 		error = PTR_ERR(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 		name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) 		goto bad_swap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 	swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) 	if (IS_ERR(swap_file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 		error = PTR_ERR(swap_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 		swap_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 		goto bad_swap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 	p->swap_file = swap_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) 	mapping = swap_file->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) 	inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) 	error = claim_swapfile(p, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) 	if (unlikely(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) 		goto bad_swap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 	inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 	if (IS_SWAPFILE(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) 		error = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 		goto bad_swap_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 	 * Read the swap header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 	if (!mapping->a_ops->readpage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 		error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 		goto bad_swap_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) 	page = read_mapping_page(mapping, 0, swap_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 	if (IS_ERR(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 		error = PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 		goto bad_swap_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 	swap_header = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 	maxpages = read_swap_header(p, swap_header, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 	if (unlikely(!maxpages)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 		error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 		goto bad_swap_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 	/* OK, set up the swap map and apply the bad block list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 	swap_map = vzalloc(maxpages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 	if (!swap_map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) 		error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 		goto bad_swap_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 	if (p->bdev && blk_queue_stable_writes(p->bdev->bd_disk->queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 		p->flags |= SWP_STABLE_WRITES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 	if (p->bdev && p->bdev->bd_disk->fops->rw_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) 		p->flags |= SWP_SYNCHRONOUS_IO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) 	if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) 		int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) 		unsigned long ci, nr_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) 		p->flags |= SWP_SOLIDSTATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) 		p->cluster_next_cpu = alloc_percpu(unsigned int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) 		if (!p->cluster_next_cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 			error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) 			goto bad_swap_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) 		 * select a random position to start with to help wear leveling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) 		 * SSD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 		for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) 			per_cpu(*p->cluster_next_cpu, cpu) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 				1 + prandom_u32_max(p->highest_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) 		nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) 		cluster_info = kvcalloc(nr_cluster, sizeof(*cluster_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 					GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 		if (!cluster_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 			error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) 			goto bad_swap_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 		for (ci = 0; ci < nr_cluster; ci++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) 			spin_lock_init(&((cluster_info + ci)->lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) 		p->percpu_cluster = alloc_percpu(struct percpu_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) 		if (!p->percpu_cluster) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) 			error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) 			goto bad_swap_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) 		for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) 			struct percpu_cluster *cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) 			cluster = per_cpu_ptr(p->percpu_cluster, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) 			cluster_set_null(&cluster->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) 		atomic_inc(&nr_rotate_swap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 		inced_nr_rotate_swap = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 	error = swap_cgroup_swapon(p->type, maxpages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 		goto bad_swap_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 	nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) 		cluster_info, maxpages, &span);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 	if (unlikely(nr_extents < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) 		error = nr_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) 		goto bad_swap_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) 	/* frontswap enabled? set up bit-per-page map for frontswap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) 	if (IS_ENABLED(CONFIG_FRONTSWAP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) 		frontswap_map = kvcalloc(BITS_TO_LONGS(maxpages),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) 					 sizeof(long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) 					 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 	if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 		 * When discard is enabled for swap with no particular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) 		 * policy flagged, we set all swap discard flags here in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) 		 * order to sustain backward compatibility with older
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) 		 * swapon(8) releases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 		p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) 			     SWP_PAGE_DISCARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 		 * By flagging sys_swapon, a sysadmin can tell us to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) 		 * either do single-time area discards only, or to just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) 		 * perform discards for released swap page-clusters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) 		 * Now it's time to adjust the p->flags accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) 		if (swap_flags & SWAP_FLAG_DISCARD_ONCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) 			p->flags &= ~SWP_PAGE_DISCARD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 		else if (swap_flags & SWAP_FLAG_DISCARD_PAGES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 			p->flags &= ~SWP_AREA_DISCARD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) 		/* issue a swapon-time discard if it's still required */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 		if (p->flags & SWP_AREA_DISCARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 			int err = discard_swap(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 			if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) 				pr_err("swapon: discard_swap(%p): %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 					p, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) 	error = init_swap_address_space(p->type, maxpages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) 		goto bad_swap_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) 	 * Flush any pending IO and dirty mappings before we start using this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) 	 * swap device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) 	inode->i_flags |= S_SWAPFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) 	error = inode_drain_writes(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) 		inode->i_flags &= ~S_SWAPFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 		goto free_swap_address_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 	mutex_lock(&swapon_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 	prio = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 	if (swap_flags & SWAP_FLAG_PREFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) 		prio =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) 		  (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) 	enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) 	trace_android_vh_init_swap_info_struct(p, swap_avail_heads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) 	pr_info("Adding %uk swap on %s.  Priority:%d extents:%d across:%lluk %s%s%s%s%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) 		p->pages<<(PAGE_SHIFT-10), name->name, p->prio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) 		nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) 		(p->flags & SWP_SOLIDSTATE) ? "SS" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) 		(p->flags & SWP_DISCARDABLE) ? "D" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) 		(p->flags & SWP_AREA_DISCARD) ? "s" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) 		(p->flags & SWP_PAGE_DISCARD) ? "c" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) 		(frontswap_map) ? "FS" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) 	mutex_unlock(&swapon_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) 	atomic_inc(&proc_poll_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 	wake_up_interruptible(&proc_poll_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 	error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) free_swap_address_space:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) 	exit_swap_address_space(p->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) bad_swap_unlock_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) 	inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) bad_swap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) 	free_percpu(p->percpu_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) 	p->percpu_cluster = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) 	free_percpu(p->cluster_next_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) 	p->cluster_next_cpu = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 	if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) 		set_blocksize(p->bdev, p->old_block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 		blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) 	inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) 	destroy_swap_extents(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) 	swap_cgroup_swapoff(p->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) 	p->swap_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) 	p->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 	vfree(swap_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) 	kvfree(cluster_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) 	kvfree(frontswap_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) 	if (inced_nr_rotate_swap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) 		atomic_dec(&nr_rotate_swap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) 	if (swap_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) 		filp_close(swap_file, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) 	if (page && !IS_ERR(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) 		kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 	if (name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 		putname(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 	if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 		inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) 	if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) 		enable_swap_slots_cache();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) void si_swapinfo(struct sysinfo *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) 	unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) 	unsigned long nr_to_be_unused = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) 	spin_lock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 	for (type = 0; type < nr_swapfiles; type++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 		struct swap_info_struct *si = swap_info[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 		bool skip = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 		trace_android_vh_si_swapinfo(si, &skip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 		if (!skip && (si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 			nr_to_be_unused += si->inuse_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 	val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 	val->totalswap = total_swap_pages + nr_to_be_unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 	spin_unlock(&swap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) EXPORT_SYMBOL_GPL(si_swapinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474)  * Verify that a swap entry is valid and increment its swap map count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476)  * Returns error code in following case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477)  * - success -> 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478)  * - swp_entry is invalid -> EINVAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479)  * - swp_entry is migration entry -> EINVAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480)  * - swap-cache reference is requested but there is already one. -> EEXIST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481)  * - swap-cache reference is requested but the entry is not used. -> ENOENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482)  * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) 	struct swap_info_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) 	unsigned char count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 	unsigned char has_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) 	int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 	p = get_swap_device(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) 	offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) 	ci = lock_cluster_or_swap_info(p, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 	count = p->swap_map[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 	 * swapin_readahead() doesn't check if a swap entry is valid, so the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 	 * swap entry could be SWAP_MAP_BAD. Check here with lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 	if (unlikely(swap_count(count) == SWAP_MAP_BAD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 		err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 		goto unlock_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 	has_cache = count & SWAP_HAS_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) 	count &= ~SWAP_HAS_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 	if (usage == SWAP_HAS_CACHE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 		/* set SWAP_HAS_CACHE if there is no cache and entry is used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 		if (!has_cache && count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 			has_cache = SWAP_HAS_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) 		else if (has_cache)		/* someone else added cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) 			err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) 		else				/* no users remaining */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) 			err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) 	} else if (count || has_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) 		if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) 			count += usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) 		else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) 		else if (swap_count_continued(p, offset, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) 			count = COUNT_CONTINUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) 		err = -ENOENT;			/* unused swap entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) 	WRITE_ONCE(p->swap_map[offset], count | has_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) unlock_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) 	unlock_cluster_or_swap_info(p, ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) 	if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 		put_swap_device(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549)  * Help swapoff by noting that swap entry belongs to shmem/tmpfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550)  * (in which case its reference count is never incremented).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) void swap_shmem_alloc(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 	__swap_duplicate(entry, SWAP_MAP_SHMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558)  * Increase reference count of swap entry by 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559)  * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560)  * but could not be atomically allocated.  Returns 0, just as if it succeeded,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561)  * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562)  * might occur if a page table entry has got corrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) int swap_duplicate(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 	while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) 		err = add_swap_count_continuation(entry, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574)  * @entry: swap entry for which we allocate swap cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576)  * Called when allocating swap cache for existing swap entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577)  * This can return error codes. Returns 0 at success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578)  * -EEXIST means there is a swap cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579)  * Note: return code is different from swap_duplicate().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) int swapcache_prepare(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) 	return __swap_duplicate(entry, SWAP_HAS_CACHE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) struct swap_info_struct *swp_swap_info(swp_entry_t entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) 	return swap_type_to_swap_info(swp_type(entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) EXPORT_SYMBOL_GPL(swp_swap_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) struct swap_info_struct *page_swap_info(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 	swp_entry_t entry = { .val = page_private(page) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) 	return swp_swap_info(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599)  * out-of-line __page_file_ methods to avoid include hell.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) struct address_space *__page_file_mapping(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 	return page_swap_info(page)->swap_file->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) EXPORT_SYMBOL_GPL(__page_file_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) pgoff_t __page_file_index(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) 	swp_entry_t swap = { .val = page_private(page) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) 	return swp_offset(swap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) EXPORT_SYMBOL_GPL(__page_file_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615)  * add_swap_count_continuation - called when a swap count is duplicated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616)  * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617)  * page of the original vmalloc'ed swap_map, to hold the continuation count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618)  * (for that entry and for its neighbouring PAGE_SIZE swap entries).  Called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619)  * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621)  * These continuation pages are seldom referenced: the common paths all work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622)  * on the original swap_map, only referring to a continuation page when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623)  * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625)  * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626)  * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627)  * can be called after dropping locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) 	struct swap_info_struct *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) 	struct swap_cluster_info *ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 	struct page *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) 	struct page *list_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 	pgoff_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 	unsigned char count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 	 * When debugging, it's easier to use __GFP_ZERO here; but it's better
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) 	 * for latency not to zero a page while GFP_ATOMIC and holding locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 	page = alloc_page(gfp_mask | __GFP_HIGHMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 	si = get_swap_device(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 	if (!si) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) 		 * An acceptable race has occurred since the failing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 		 * __swap_duplicate(): the swap device may be swapoff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 		goto outer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 	spin_lock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 	offset = swp_offset(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 	ci = lock_cluster(si, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 	count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 	if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 		 * The higher the swap count, the more likely it is that tasks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 		 * will race to add swap count continuation: we need to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 		 * over-provisioning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 	if (!page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) 	 * We are fortunate that although vmalloc_to_page uses pte_offset_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) 	 * no architecture is using highmem pages for kernel page tables: so it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) 	 * will not corrupt the GFP_ATOMIC caller's atomic page table kmaps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) 	head = vmalloc_to_page(si->swap_map + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) 	offset &= ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) 	spin_lock(&si->cont_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) 	 * Page allocation does not initialize the page's lru field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) 	 * but it does always reset its private field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) 	if (!page_private(head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) 		BUG_ON(count & COUNT_CONTINUED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) 		INIT_LIST_HEAD(&head->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) 		set_page_private(head, SWP_CONTINUED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) 		si->flags |= SWP_CONTINUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) 	list_for_each_entry(list_page, &head->lru, lru) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) 		unsigned char *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) 		 * If the previous map said no continuation, but we've found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) 		 * a continuation page, free our allocation and use this one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) 		if (!(count & COUNT_CONTINUED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) 			goto out_unlock_cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) 		map = kmap_atomic(list_page) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) 		count = *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) 		kunmap_atomic(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) 		 * If this continuation count now has some space in it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) 		 * free our allocation and use this one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) 		if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) 			goto out_unlock_cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) 	list_add_tail(&page->lru, &head->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 	page = NULL;			/* now it's attached, don't free it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) out_unlock_cont:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 	spin_unlock(&si->cont_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 	unlock_cluster(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) 	spin_unlock(&si->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) 	put_swap_device(si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) outer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) 	if (page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) 		__free_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733)  * swap_count_continued - when the original swap_map count is incremented
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734)  * from SWAP_MAP_MAX, check if there is already a continuation page to carry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735)  * into, carry if so, or else fail until a new continuation page is allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736)  * when the original swap_map count is decremented from 0 with continuation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737)  * borrow from the continuation and report whether it still holds more.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738)  * Called while __swap_duplicate() or swap_entry_free() holds swap or cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739)  * lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) static bool swap_count_continued(struct swap_info_struct *si,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) 				 pgoff_t offset, unsigned char count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) 	struct page *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) 	unsigned char *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) 	head = vmalloc_to_page(si->swap_map + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) 	if (page_private(head) != SWP_CONTINUED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 		BUG_ON(count & COUNT_CONTINUED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) 		return false;		/* need to add count continuation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) 	spin_lock(&si->cont_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) 	offset &= ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) 	page = list_next_entry(head, lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) 	map = kmap_atomic(page) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) 	if (count == SWAP_MAP_MAX)	/* initial increment from swap_map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) 		goto init_map;		/* jump over SWAP_CONT_MAX checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) 	if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) 		 * Think of how you add 1 to 999
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) 		while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) 			kunmap_atomic(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) 			page = list_next_entry(page, lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) 			BUG_ON(page == head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) 			map = kmap_atomic(page) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) 		if (*map == SWAP_CONT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) 			kunmap_atomic(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) 			page = list_next_entry(page, lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 			if (page == head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) 				ret = false;	/* add count continuation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) 			map = kmap_atomic(page) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) init_map:		*map = 0;		/* we didn't zero the page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) 		*map += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) 		kunmap_atomic(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) 		while ((page = list_prev_entry(page, lru)) != head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) 			map = kmap_atomic(page) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) 			*map = COUNT_CONTINUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) 			kunmap_atomic(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) 		ret = true;			/* incremented */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) 	} else {				/* decrementing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) 		 * Think of how you subtract 1 from 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) 		BUG_ON(count != COUNT_CONTINUED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) 		while (*map == COUNT_CONTINUED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) 			kunmap_atomic(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) 			page = list_next_entry(page, lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) 			BUG_ON(page == head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) 			map = kmap_atomic(page) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) 		BUG_ON(*map == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) 		*map -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) 		if (*map == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) 			count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) 		kunmap_atomic(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) 		while ((page = list_prev_entry(page, lru)) != head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) 			map = kmap_atomic(page) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) 			*map = SWAP_CONT_MAX | count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) 			count = COUNT_CONTINUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) 			kunmap_atomic(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) 		ret = count == COUNT_CONTINUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) 	spin_unlock(&si->cont_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822)  * free_swap_count_continuations - swapoff free all the continuation pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823)  * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) static void free_swap_count_continuations(struct swap_info_struct *si)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) 	pgoff_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) 	for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) 		struct page *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) 		head = vmalloc_to_page(si->swap_map + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) 		if (page_private(head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) 			struct page *page, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) 			list_for_each_entry_safe(page, next, &head->lru, lru) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) 				list_del(&page->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) 				__free_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) void __cgroup_throttle_swaprate(struct page *page, gfp_t gfp_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) 	struct swap_info_struct *si, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) 	int nid = page_to_nid(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) 	if (!(gfp_mask & __GFP_IO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) 	if (!blk_cgroup_congested())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) 	 * We've already scheduled a throttle, avoid taking the global swap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) 	 * lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) 	if (current->throttle_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) 	spin_lock(&swap_avail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) 	plist_for_each_entry_safe(si, next, &swap_avail_heads[nid],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) 				  avail_lists[nid]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) 		if (si->bdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) 			blkcg_schedule_throttle(bdev_get_queue(si->bdev), true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) 	spin_unlock(&swap_avail_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) static int __init swapfile_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) 	int nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) 	swap_avail_heads = kmalloc_array(nr_node_ids, sizeof(struct plist_head),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) 					 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) 	if (!swap_avail_heads) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) 		pr_emerg("Not enough memory for swap heads, swap is disabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) 	for_each_node(nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) 		plist_head_init(&swap_avail_heads[nid]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) subsys_initcall(swapfile_init);