Skip to content
Snippets Groups Projects
swapfile.c 77.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • Kenneth Johansson's avatar
    Kenneth Johansson committed
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    /*
     *  linux/mm/swapfile.c
     *
     *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
     *  Swap reorganised 29.12.95, Stephen Tweedie
     */
    
    #include <linux/mm.h>
    #include <linux/hugetlb.h>
    #include <linux/mman.h>
    #include <linux/slab.h>
    #include <linux/kernel_stat.h>
    #include <linux/swap.h>
    #include <linux/vmalloc.h>
    #include <linux/pagemap.h>
    #include <linux/namei.h>
    #include <linux/shmem_fs.h>
    #include <linux/blkdev.h>
    #include <linux/random.h>
    #include <linux/writeback.h>
    #include <linux/proc_fs.h>
    #include <linux/seq_file.h>
    #include <linux/init.h>
    #include <linux/ksm.h>
    #include <linux/rmap.h>
    #include <linux/security.h>
    #include <linux/backing-dev.h>
    #include <linux/mutex.h>
    #include <linux/capability.h>
    #include <linux/syscalls.h>
    #include <linux/memcontrol.h>
    #include <linux/poll.h>
    #include <linux/oom.h>
    #include <linux/frontswap.h>
    #include <linux/swapfile.h>
    #include <linux/export.h>
    
    #include <asm/pgtable.h>
    #include <asm/tlbflush.h>
    #include <linux/swapops.h>
    #include <linux/swap_cgroup.h>
    
    static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
    				 unsigned char);
    static void free_swap_count_continuations(struct swap_info_struct *);
    static sector_t map_swap_entry(swp_entry_t, struct block_device**);
    
    DEFINE_SPINLOCK(swap_lock);
    static unsigned int nr_swapfiles;
    atomic_long_t nr_swap_pages;
    /*
     * Some modules use swappable objects and may try to swap them out under
     * memory pressure (via the shrinker). Before doing so, they may wish to
     * check to see if any swap space is available.
     */
    EXPORT_SYMBOL_GPL(nr_swap_pages);
    /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
    long total_swap_pages;
    static int least_priority;
    
    static const char Bad_file[] = "Bad swap file entry ";
    static const char Unused_file[] = "Unused swap file entry ";
    static const char Bad_offset[] = "Bad swap offset entry ";
    static const char Unused_offset[] = "Unused swap offset entry ";
    
    /*
     * all active swap_info_structs
     * protected with swap_lock, and ordered by priority.
     */
    PLIST_HEAD(swap_active_head);
    
    /*
     * all available (active, not full) swap_info_structs
     * protected with swap_avail_lock, ordered by priority.
     * This is used by get_swap_page() instead of swap_active_head
     * because swap_active_head includes all swap_info_structs,
     * but get_swap_page() doesn't need to look at full ones.
     * This uses its own lock instead of swap_lock because when a
     * swap_info_struct changes between not-full/full, it needs to
     * add/remove itself to/from this list, but the swap_info_struct->lock
     * is held and the locking order requires swap_lock to be taken
     * before any swap_info_struct->lock.
     */
    static PLIST_HEAD(swap_avail_head);
    static DEFINE_SPINLOCK(swap_avail_lock);
    
    struct swap_info_struct *swap_info[MAX_SWAPFILES];
    
    static DEFINE_MUTEX(swapon_mutex);
    
    static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
    /* Activity counter to indicate that a swapon or swapoff has occurred */
    static atomic_t proc_poll_event = ATOMIC_INIT(0);
    
    static inline unsigned char swap_count(unsigned char ent)
    {
    	return ent & ~SWAP_HAS_CACHE;	/* may include SWAP_HAS_CONT flag */
    }
    
    /* returns 1 if swap entry is freed */
    static int
    __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
    {
    	swp_entry_t entry = swp_entry(si->type, offset);
    	struct page *page;
    	int ret = 0;
    
    	page = find_get_page(swap_address_space(entry), swp_offset(entry));
    	if (!page)
    		return 0;
    	/*
    	 * This function is called from scan_swap_map() and it's called
    	 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
    	 * We have to use trylock for avoiding deadlock. This is a special
    	 * case and you should use try_to_free_swap() with explicit lock_page()
    	 * in usual operations.
    	 */
    	if (trylock_page(page)) {
    		ret = try_to_free_swap(page);
    		unlock_page(page);
    	}
    	put_page(page);
    	return ret;
    }
    
    /*
     * swapon tell device that all the old swap contents can be discarded,
     * to allow the swap device to optimize its wear-levelling.
     */
    static int discard_swap(struct swap_info_struct *si)
    {
    	struct swap_extent *se;
    	sector_t start_block;
    	sector_t nr_blocks;
    	int err = 0;
    
    	/* Do not discard the swap header page! */
    	se = &si->first_swap_extent;
    	start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
    	nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
    	if (nr_blocks) {
    		err = blkdev_issue_discard(si->bdev, start_block,
    				nr_blocks, GFP_KERNEL, 0);
    		if (err)
    			return err;
    		cond_resched();
    	}
    
    	list_for_each_entry(se, &si->first_swap_extent.list, list) {
    		start_block = se->start_block << (PAGE_SHIFT - 9);
    		nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
    
    		err = blkdev_issue_discard(si->bdev, start_block,
    				nr_blocks, GFP_KERNEL, 0);
    		if (err)
    			break;
    
    		cond_resched();
    	}
    	return err;		/* That will often be -EOPNOTSUPP */
    }
    
    /*
     * swap allocation tell device that a cluster of swap can now be discarded,
     * to allow the swap device to optimize its wear-levelling.
     */
    static void discard_swap_cluster(struct swap_info_struct *si,
    				 pgoff_t start_page, pgoff_t nr_pages)
    {
    	struct swap_extent *se = si->curr_swap_extent;
    	int found_extent = 0;
    
    	while (nr_pages) {
    		if (se->start_page <= start_page &&
    		    start_page < se->start_page + se->nr_pages) {
    			pgoff_t offset = start_page - se->start_page;
    			sector_t start_block = se->start_block + offset;
    			sector_t nr_blocks = se->nr_pages - offset;
    
    			if (nr_blocks > nr_pages)
    				nr_blocks = nr_pages;
    			start_page += nr_blocks;
    			nr_pages -= nr_blocks;
    
    			if (!found_extent++)
    				si->curr_swap_extent = se;
    
    			start_block <<= PAGE_SHIFT - 9;
    			nr_blocks <<= PAGE_SHIFT - 9;
    			if (blkdev_issue_discard(si->bdev, start_block,
    				    nr_blocks, GFP_NOIO, 0))
    				break;
    		}
    
    		se = list_next_entry(se, list);
    	}
    }
    
    #define SWAPFILE_CLUSTER	256
    #define LATENCY_LIMIT		256
    
    static inline void cluster_set_flag(struct swap_cluster_info *info,
    	unsigned int flag)
    {
    	info->flags = flag;
    }
    
    static inline unsigned int cluster_count(struct swap_cluster_info *info)
    {
    	return info->data;
    }
    
    static inline void cluster_set_count(struct swap_cluster_info *info,
    				     unsigned int c)
    {
    	info->data = c;
    }
    
    static inline void cluster_set_count_flag(struct swap_cluster_info *info,
    					 unsigned int c, unsigned int f)
    {
    	info->flags = f;
    	info->data = c;
    }
    
    static inline unsigned int cluster_next(struct swap_cluster_info *info)
    {
    	return info->data;
    }
    
    static inline void cluster_set_next(struct swap_cluster_info *info,
    				    unsigned int n)
    {
    	info->data = n;
    }
    
    static inline void cluster_set_next_flag(struct swap_cluster_info *info,
    					 unsigned int n, unsigned int f)
    {
    	info->flags = f;
    	info->data = n;
    }
    
    static inline bool cluster_is_free(struct swap_cluster_info *info)
    {
    	return info->flags & CLUSTER_FLAG_FREE;
    }
    
    static inline bool cluster_is_null(struct swap_cluster_info *info)
    {
    	return info->flags & CLUSTER_FLAG_NEXT_NULL;
    }
    
    static inline void cluster_set_null(struct swap_cluster_info *info)
    {
    	info->flags = CLUSTER_FLAG_NEXT_NULL;
    	info->data = 0;
    }
    
    static inline bool cluster_list_empty(struct swap_cluster_list *list)
    {
    	return cluster_is_null(&list->head);
    }
    
    static inline unsigned int cluster_list_first(struct swap_cluster_list *list)
    {
    	return cluster_next(&list->head);
    }
    
    static void cluster_list_init(struct swap_cluster_list *list)
    {
    	cluster_set_null(&list->head);
    	cluster_set_null(&list->tail);
    }
    
    static void cluster_list_add_tail(struct swap_cluster_list *list,
    				  struct swap_cluster_info *ci,
    				  unsigned int idx)
    {
    	if (cluster_list_empty(list)) {
    		cluster_set_next_flag(&list->head, idx, 0);
    		cluster_set_next_flag(&list->tail, idx, 0);
    	} else {
    		unsigned int tail = cluster_next(&list->tail);
    
    		cluster_set_next(&ci[tail], idx);
    		cluster_set_next_flag(&list->tail, idx, 0);
    	}
    }
    
    static unsigned int cluster_list_del_first(struct swap_cluster_list *list,
    					   struct swap_cluster_info *ci)
    {
    	unsigned int idx;
    
    	idx = cluster_next(&list->head);
    	if (cluster_next(&list->tail) == idx) {
    		cluster_set_null(&list->head);
    		cluster_set_null(&list->tail);
    	} else
    		cluster_set_next_flag(&list->head,
    				      cluster_next(&ci[idx]), 0);
    
    	return idx;
    }
    
    /* Add a cluster to discard list and schedule it to do discard */
    static void swap_cluster_schedule_discard(struct swap_info_struct *si,
    		unsigned int idx)
    {
    	/*
    	 * If scan_swap_map() can't find a free cluster, it will check
    	 * si->swap_map directly. To make sure the discarding cluster isn't
    	 * taken by scan_swap_map(), mark the swap entries bad (occupied). It
    	 * will be cleared after discard
    	 */
    	memset(si->swap_map + idx * SWAPFILE_CLUSTER,
    			SWAP_MAP_BAD, SWAPFILE_CLUSTER);
    
    	cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx);
    
    	schedule_work(&si->discard_work);
    }
    
    /*
     * Doing discard actually. After a cluster discard is finished, the cluster
     * will be added to free cluster list. caller should hold si->lock.
    */
    static void swap_do_scheduled_discard(struct swap_info_struct *si)
    {
    	struct swap_cluster_info *info;
    	unsigned int idx;
    
    	info = si->cluster_info;
    
    	while (!cluster_list_empty(&si->discard_clusters)) {
    		idx = cluster_list_del_first(&si->discard_clusters, info);
    		spin_unlock(&si->lock);
    
    		discard_swap_cluster(si, idx * SWAPFILE_CLUSTER,
    				SWAPFILE_CLUSTER);
    
    		spin_lock(&si->lock);
    		cluster_set_flag(&info[idx], CLUSTER_FLAG_FREE);
    		cluster_list_add_tail(&si->free_clusters, info, idx);
    		memset(si->swap_map + idx * SWAPFILE_CLUSTER,
    				0, SWAPFILE_CLUSTER);
    	}
    }
    
    static void swap_discard_work(struct work_struct *work)
    {
    	struct swap_info_struct *si;
    
    	si = container_of(work, struct swap_info_struct, discard_work);
    
    	spin_lock(&si->lock);
    	swap_do_scheduled_discard(si);
    	spin_unlock(&si->lock);
    }
    
    /*
     * The cluster corresponding to page_nr will be used. The cluster will be
     * removed from free cluster list and its usage counter will be increased.
     */
    static void inc_cluster_info_page(struct swap_info_struct *p,
    	struct swap_cluster_info *cluster_info, unsigned long page_nr)
    {
    	unsigned long idx = page_nr / SWAPFILE_CLUSTER;
    
    	if (!cluster_info)
    		return;
    	if (cluster_is_free(&cluster_info[idx])) {
    		VM_BUG_ON(cluster_list_first(&p->free_clusters) != idx);
    		cluster_list_del_first(&p->free_clusters, cluster_info);
    		cluster_set_count_flag(&cluster_info[idx], 0, 0);
    	}
    
    	VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER);
    	cluster_set_count(&cluster_info[idx],
    		cluster_count(&cluster_info[idx]) + 1);
    }
    
    /*
     * The cluster corresponding to page_nr decreases one usage. If the usage
     * counter becomes 0, which means no page in the cluster is in using, we can
     * optionally discard the cluster and add it to free cluster list.
     */
    static void dec_cluster_info_page(struct swap_info_struct *p,
    	struct swap_cluster_info *cluster_info, unsigned long page_nr)
    {
    	unsigned long idx = page_nr / SWAPFILE_CLUSTER;
    
    	if (!cluster_info)
    		return;
    
    	VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0);
    	cluster_set_count(&cluster_info[idx],
    		cluster_count(&cluster_info[idx]) - 1);
    
    	if (cluster_count(&cluster_info[idx]) == 0) {
    		/*
    		 * If the swap is discardable, prepare discard the cluster
    		 * instead of free it immediately. The cluster will be freed
    		 * after discard.
    		 */
    		if ((p->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) ==
    				 (SWP_WRITEOK | SWP_PAGE_DISCARD)) {
    			swap_cluster_schedule_discard(p, idx);
    			return;
    		}
    
    		cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE);
    		cluster_list_add_tail(&p->free_clusters, cluster_info, idx);
    	}
    }
    
    /*
     * It's possible scan_swap_map() uses a free cluster in the middle of free
     * cluster list. Avoiding such abuse to avoid list corruption.
     */
    static bool
    scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si,
    	unsigned long offset)
    {
    	struct percpu_cluster *percpu_cluster;
    	bool conflict;
    
    	offset /= SWAPFILE_CLUSTER;
    	conflict = !cluster_list_empty(&si->free_clusters) &&
    		offset != cluster_list_first(&si->free_clusters) &&
    		cluster_is_free(&si->cluster_info[offset]);
    
    	if (!conflict)
    		return false;
    
    	percpu_cluster = this_cpu_ptr(si->percpu_cluster);
    	cluster_set_null(&percpu_cluster->index);
    	return true;
    }
    
    /*
     * Try to get a swap entry from current cpu's swap entry pool (a cluster). This
     * might involve allocating a new cluster for current CPU too.
     */
    static void scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
    	unsigned long *offset, unsigned long *scan_base)
    {
    	struct percpu_cluster *cluster;
    	bool found_free;
    	unsigned long tmp;
    
    new_cluster:
    	cluster = this_cpu_ptr(si->percpu_cluster);
    	if (cluster_is_null(&cluster->index)) {
    		if (!cluster_list_empty(&si->free_clusters)) {
    			cluster->index = si->free_clusters.head;
    			cluster->next = cluster_next(&cluster->index) *
    					SWAPFILE_CLUSTER;
    		} else if (!cluster_list_empty(&si->discard_clusters)) {
    			/*
    			 * we don't have free cluster but have some clusters in
    			 * discarding, do discard now and reclaim them
    			 */
    			swap_do_scheduled_discard(si);
    			*scan_base = *offset = si->cluster_next;
    			goto new_cluster;
    		} else
    			return;
    	}
    
    	found_free = false;
    
    	/*
    	 * Other CPUs can use our cluster if they can't find a free cluster,
    	 * check if there is still free entry in the cluster
    	 */
    	tmp = cluster->next;
    	while (tmp < si->max && tmp < (cluster_next(&cluster->index) + 1) *
    	       SWAPFILE_CLUSTER) {
    		if (!si->swap_map[tmp]) {
    			found_free = true;
    			break;
    		}
    		tmp++;
    	}
    	if (!found_free) {
    		cluster_set_null(&cluster->index);
    		goto new_cluster;
    	}
    	cluster->next = tmp + 1;
    	*offset = tmp;
    	*scan_base = tmp;
    }
    
    static unsigned long scan_swap_map(struct swap_info_struct *si,
    				   unsigned char usage)
    {
    	unsigned long offset;
    	unsigned long scan_base;
    	unsigned long last_in_cluster = 0;
    	int latency_ration = LATENCY_LIMIT;
    
    	/*
    	 * We try to cluster swap pages by allocating them sequentially
    	 * in swap.  Once we've allocated SWAPFILE_CLUSTER pages this
    	 * way, however, we resort to first-free allocation, starting
    	 * a new cluster.  This prevents us from scattering swap pages
    	 * all over the entire swap partition, so that we reduce
    	 * overall disk seek times between swap pages.  -- sct
    	 * But we do now try to find an empty cluster.  -Andrea
    	 * And we let swap pages go all over an SSD partition.  Hugh
    	 */
    
    	si->flags += SWP_SCANNING;
    	scan_base = offset = si->cluster_next;
    
    	/* SSD algorithm */
    	if (si->cluster_info) {
    		scan_swap_map_try_ssd_cluster(si, &offset, &scan_base);
    		goto checks;
    	}
    
    	if (unlikely(!si->cluster_nr--)) {
    		if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
    			si->cluster_nr = SWAPFILE_CLUSTER - 1;
    			goto checks;
    		}
    
    		spin_unlock(&si->lock);
    
    		/*
    		 * If seek is expensive, start searching for new cluster from
    		 * start of partition, to minimize the span of allocated swap.
    		 * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info
    		 * case, just handled by scan_swap_map_try_ssd_cluster() above.
    		 */
    		scan_base = offset = si->lowest_bit;
    		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
    
    		/* Locate the first empty (unaligned) cluster */
    		for (; last_in_cluster <= si->highest_bit; offset++) {
    			if (si->swap_map[offset])
    				last_in_cluster = offset + SWAPFILE_CLUSTER;
    			else if (offset == last_in_cluster) {
    				spin_lock(&si->lock);
    				offset -= SWAPFILE_CLUSTER - 1;
    				si->cluster_next = offset;
    				si->cluster_nr = SWAPFILE_CLUSTER - 1;
    				goto checks;
    			}
    			if (unlikely(--latency_ration < 0)) {
    				cond_resched();
    				latency_ration = LATENCY_LIMIT;
    			}
    		}
    
    		offset = scan_base;
    		spin_lock(&si->lock);
    		si->cluster_nr = SWAPFILE_CLUSTER - 1;
    	}
    
    checks:
    	if (si->cluster_info) {
    		while (scan_swap_map_ssd_cluster_conflict(si, offset))
    			scan_swap_map_try_ssd_cluster(si, &offset, &scan_base);
    	}
    	if (!(si->flags & SWP_WRITEOK))
    		goto no_page;
    	if (!si->highest_bit)
    		goto no_page;
    	if (offset > si->highest_bit)
    		scan_base = offset = si->lowest_bit;
    
    	/* reuse swap entry of cache-only swap if not busy. */
    	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
    		int swap_was_freed;
    		spin_unlock(&si->lock);
    		swap_was_freed = __try_to_reclaim_swap(si, offset);
    		spin_lock(&si->lock);
    		/* entry was freed successfully, try to use this again */
    		if (swap_was_freed)
    			goto checks;
    		goto scan; /* check next one */
    	}
    
    	if (si->swap_map[offset])
    		goto scan;
    
    	if (offset == si->lowest_bit)
    		si->lowest_bit++;
    	if (offset == si->highest_bit)
    		si->highest_bit--;
    	si->inuse_pages++;
    	if (si->inuse_pages == si->pages) {
    		si->lowest_bit = si->max;
    		si->highest_bit = 0;
    		spin_lock(&swap_avail_lock);
    		plist_del(&si->avail_list, &swap_avail_head);
    		spin_unlock(&swap_avail_lock);
    	}
    	si->swap_map[offset] = usage;
    	inc_cluster_info_page(si, si->cluster_info, offset);
    	si->cluster_next = offset + 1;
    	si->flags -= SWP_SCANNING;
    
    	return offset;
    
    scan:
    	spin_unlock(&si->lock);
    	while (++offset <= si->highest_bit) {
    		if (!si->swap_map[offset]) {
    			spin_lock(&si->lock);
    			goto checks;
    		}
    		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
    			spin_lock(&si->lock);
    			goto checks;
    		}
    		if (unlikely(--latency_ration < 0)) {
    			cond_resched();
    			latency_ration = LATENCY_LIMIT;
    		}
    	}
    	offset = si->lowest_bit;
    	while (offset < scan_base) {
    		if (!si->swap_map[offset]) {
    			spin_lock(&si->lock);
    			goto checks;
    		}
    		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
    			spin_lock(&si->lock);
    			goto checks;
    		}
    		if (unlikely(--latency_ration < 0)) {
    			cond_resched();
    			latency_ration = LATENCY_LIMIT;
    		}
    		offset++;
    	}
    	spin_lock(&si->lock);
    
    no_page:
    	si->flags -= SWP_SCANNING;
    	return 0;
    }
    
    swp_entry_t get_swap_page(void)
    {
    	struct swap_info_struct *si, *next;
    	pgoff_t offset;
    
    	if (atomic_long_read(&nr_swap_pages) <= 0)
    		goto noswap;
    	atomic_long_dec(&nr_swap_pages);
    
    	spin_lock(&swap_avail_lock);
    
    start_over:
    	plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) {
    		/* requeue si to after same-priority siblings */
    		plist_requeue(&si->avail_list, &swap_avail_head);
    		spin_unlock(&swap_avail_lock);
    		spin_lock(&si->lock);
    		if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) {
    			spin_lock(&swap_avail_lock);
    			if (plist_node_empty(&si->avail_list)) {
    				spin_unlock(&si->lock);
    				goto nextsi;
    			}
    			WARN(!si->highest_bit,
    			     "swap_info %d in list but !highest_bit\n",
    			     si->type);
    			WARN(!(si->flags & SWP_WRITEOK),
    			     "swap_info %d in list but !SWP_WRITEOK\n",
    			     si->type);
    			plist_del(&si->avail_list, &swap_avail_head);
    			spin_unlock(&si->lock);
    			goto nextsi;
    		}
    
    		/* This is called for allocating swap entry for cache */
    		offset = scan_swap_map(si, SWAP_HAS_CACHE);
    		spin_unlock(&si->lock);
    		if (offset)
    			return swp_entry(si->type, offset);
    		pr_debug("scan_swap_map of si %d failed to find offset\n",
    		       si->type);
    		spin_lock(&swap_avail_lock);
    nextsi:
    		/*
    		 * if we got here, it's likely that si was almost full before,
    		 * and since scan_swap_map() can drop the si->lock, multiple
    		 * callers probably all tried to get a page from the same si
    		 * and it filled up before we could get one; or, the si filled
    		 * up between us dropping swap_avail_lock and taking si->lock.
    		 * Since we dropped the swap_avail_lock, the swap_avail_head
    		 * list may have been modified; so if next is still in the
    		 * swap_avail_head list then try it, otherwise start over.
    		 */
    		if (plist_node_empty(&next->avail_list))
    			goto start_over;
    	}
    
    	spin_unlock(&swap_avail_lock);
    
    	atomic_long_inc(&nr_swap_pages);
    noswap:
    	return (swp_entry_t) {0};
    }
    
    /* The only caller of this function is now suspend routine */
    swp_entry_t get_swap_page_of_type(int type)
    {
    	struct swap_info_struct *si;
    	pgoff_t offset;
    
    	si = swap_info[type];
    	spin_lock(&si->lock);
    	if (si && (si->flags & SWP_WRITEOK)) {
    		atomic_long_dec(&nr_swap_pages);
    		/* This is called for allocating swap entry, not cache */
    		offset = scan_swap_map(si, 1);
    		if (offset) {
    			spin_unlock(&si->lock);
    			return swp_entry(type, offset);
    		}
    		atomic_long_inc(&nr_swap_pages);
    	}
    	spin_unlock(&si->lock);
    	return (swp_entry_t) {0};
    }
    
    static struct swap_info_struct *swap_info_get(swp_entry_t entry)
    {
    	struct swap_info_struct *p;
    	unsigned long offset, type;
    
    	if (!entry.val)
    		goto out;
    	type = swp_type(entry);
    	if (type >= nr_swapfiles)
    		goto bad_nofile;
    	p = swap_info[type];
    	if (!(p->flags & SWP_USED))
    		goto bad_device;
    	offset = swp_offset(entry);
    	if (offset >= p->max)
    		goto bad_offset;
    	if (!p->swap_map[offset])
    		goto bad_free;
    	spin_lock(&p->lock);
    	return p;
    
    bad_free:
    	pr_err("swap_free: %s%08lx\n", Unused_offset, entry.val);
    	goto out;
    bad_offset:
    	pr_err("swap_free: %s%08lx\n", Bad_offset, entry.val);
    	goto out;
    bad_device:
    	pr_err("swap_free: %s%08lx\n", Unused_file, entry.val);
    	goto out;
    bad_nofile:
    	pr_err("swap_free: %s%08lx\n", Bad_file, entry.val);
    out:
    	return NULL;
    }
    
    static unsigned char swap_entry_free(struct swap_info_struct *p,
    				     swp_entry_t entry, unsigned char usage)
    {
    	unsigned long offset = swp_offset(entry);
    	unsigned char count;
    	unsigned char has_cache;
    
    	count = p->swap_map[offset];
    	has_cache = count & SWAP_HAS_CACHE;
    	count &= ~SWAP_HAS_CACHE;
    
    	if (usage == SWAP_HAS_CACHE) {
    		VM_BUG_ON(!has_cache);
    		has_cache = 0;
    	} else if (count == SWAP_MAP_SHMEM) {
    		/*
    		 * Or we could insist on shmem.c using a special
    		 * swap_shmem_free() and free_shmem_swap_and_cache()...
    		 */
    		count = 0;
    	} else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
    		if (count == COUNT_CONTINUED) {
    			if (swap_count_continued(p, offset, count))
    				count = SWAP_MAP_MAX | COUNT_CONTINUED;
    			else
    				count = SWAP_MAP_MAX;
    		} else
    			count--;
    	}
    
    	usage = count | has_cache;
    	p->swap_map[offset] = usage;
    
    	/* free if no reference */
    	if (!usage) {
    		mem_cgroup_uncharge_swap(entry);
    		dec_cluster_info_page(p, p->cluster_info, offset);
    		if (offset < p->lowest_bit)
    			p->lowest_bit = offset;
    		if (offset > p->highest_bit) {
    			bool was_full = !p->highest_bit;
    			p->highest_bit = offset;
    			if (was_full && (p->flags & SWP_WRITEOK)) {
    				spin_lock(&swap_avail_lock);
    				WARN_ON(!plist_node_empty(&p->avail_list));
    				if (plist_node_empty(&p->avail_list))
    					plist_add(&p->avail_list,
    						  &swap_avail_head);
    				spin_unlock(&swap_avail_lock);
    			}
    		}
    		atomic_long_inc(&nr_swap_pages);
    		p->inuse_pages--;
    		frontswap_invalidate_page(p->type, offset);
    		if (p->flags & SWP_BLKDEV) {
    			struct gendisk *disk = p->bdev->bd_disk;
    			if (disk->fops->swap_slot_free_notify)
    				disk->fops->swap_slot_free_notify(p->bdev,
    								  offset);
    		}
    	}
    
    	return usage;
    }
    
    /*
     * Caller has made sure that the swap device corresponding to entry
     * is still around or has not been recycled.
     */
    void swap_free(swp_entry_t entry)
    {
    	struct swap_info_struct *p;
    
    	p = swap_info_get(entry);
    	if (p) {
    		swap_entry_free(p, entry, 1);
    		spin_unlock(&p->lock);
    	}
    }
    
    /*
     * Called after dropping swapcache to decrease refcnt to swap entries.
     */
    void swapcache_free(swp_entry_t entry)
    {
    	struct swap_info_struct *p;
    
    	p = swap_info_get(entry);
    	if (p) {
    		swap_entry_free(p, entry, SWAP_HAS_CACHE);
    		spin_unlock(&p->lock);
    	}
    }
    
    /*
     * How many references to page are currently swapped out?
     * This does not give an exact answer when swap count is continued,
     * but does include the high COUNT_CONTINUED flag to allow for that.
     */
    int page_swapcount(struct page *page)
    {
    	int count = 0;
    	struct swap_info_struct *p;
    	swp_entry_t entry;
    
    	entry.val = page_private(page);
    	p = swap_info_get(entry);
    	if (p) {
    		count = swap_count(p->swap_map[swp_offset(entry)]);
    		spin_unlock(&p->lock);
    	}
    	return count;
    }
    
    /*
     * How many references to @entry are currently swapped out?
     * This considers COUNT_CONTINUED so it returns exact answer.
     */
    int swp_swapcount(swp_entry_t entry)
    {
    	int count, tmp_count, n;
    	struct swap_info_struct *p;
    	struct page *page;
    	pgoff_t offset;
    	unsigned char *map;
    
    	p = swap_info_get(entry);
    	if (!p)
    		return 0;
    
    	count = swap_count(p->swap_map[swp_offset(entry)]);
    	if (!(count & COUNT_CONTINUED))
    		goto out;
    
    	count &= ~COUNT_CONTINUED;
    	n = SWAP_MAP_MAX + 1;
    
    	offset = swp_offset(entry);
    	page = vmalloc_to_page(p->swap_map + offset);
    	offset &= ~PAGE_MASK;
    	VM_BUG_ON(page_private(page) != SWP_CONTINUED);
    
    	do {
    		page = list_next_entry(page, lru);
    		map = kmap_atomic(page);
    		tmp_count = map[offset];
    		kunmap_atomic(map);
    
    		count += (tmp_count & ~COUNT_CONTINUED) * n;
    		n *= (SWAP_CONT_MAX + 1);
    	} while (tmp_count & COUNT_CONTINUED);
    out:
    	spin_unlock(&p->lock);
    	return count;
    }
    
    /*
     * We can write to an anon page without COW if there are no other references
     * to it.  And as a side-effect, free up its swap: because the old content
     * on disk will never be read, and seeking back there to write new content
     * later would only waste time away from clustering.
     *
     * NOTE: total_mapcount should not be relied upon by the caller if
     * reuse_swap_page() returns false, but it may be always overwritten
     * (see the other implementation for CONFIG_SWAP=n).
     */
    bool reuse_swap_page(struct page *page, int *total_mapcount)
    {
    	int count;
    
    	VM_BUG_ON_PAGE(!PageLocked(page), page);
    	if (unlikely(PageKsm(page)))
    		return false;
    	count = page_trans_huge_mapcount(page, total_mapcount);
    	if (count <= 1 && PageSwapCache(page)) {
    		count += page_swapcount(page);
    		if (count != 1)
    			goto out;
    		if (!PageWriteback(page)) {
    			delete_from_swap_cache(page);
    			SetPageDirty(page);
    		} else {
    			swp_entry_t entry;
    			struct swap_info_struct *p;
    
    			entry.val = page_private(page);
    			p = swap_info_get(entry);
    			if (p->flags & SWP_STABLE_WRITES) {
    				spin_unlock(&p->lock);
    				return false;
    			}
    			spin_unlock(&p->lock);
    		}
    	}
    out:
    	return count <= 1;
    }
    
    /*
     * If swap is getting full, or if there are no more mappings of this page,
     * then try_to_free_swap is called to free its swap space.
     */
    int try_to_free_swap(struct page *page)
    {
    	VM_BUG_ON_PAGE(!PageLocked(page), page);
    
    	if (!PageSwapCache(page))
    		return 0;
    	if (PageWriteback(page))
    		return 0;
    	if (page_swapcount(page))
    		return 0;
    
    	/*
    	 * Once hibernation has begun to create its image of memory,
    	 * there's a danger that one of the calls to try_to_free_swap()
    	 * - most probably a call from __try_to_reclaim_swap() while
    	 * hibernation is allocating its own swap pages for the image,
    	 * but conceivably even a call from memory reclaim - will free
    	 * the swap from a page which has already been recorded in the
    	 * image as a clean swapcache page, and then reuse its swap for
    	 * another page of the image.  On waking from hibernation, the
    	 * original page might be freed under memory pressure, then
    	 * later read back in from swap, now with the wrong data.
    	 *
    	 * Hibernation suspends storage while it is writing the image
    	 * to disk so check that here.
    	 */
    	if (pm_suspended_storage())
    		return 0;