LCOV - code coverage report
Current view: top level - mm - swap_state.c (source / functions) Hit Total Coverage
Test: Real Lines: 28 262 10.7 %
Date: 2020-10-17 15:46:43 Functions: 0 22 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0
       2                 :            : /*
       3                 :            :  *  linux/mm/swap_state.c
       4                 :            :  *
       5                 :            :  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
       6                 :            :  *  Swap reorganised 29.12.95, Stephen Tweedie
       7                 :            :  *
       8                 :            :  *  Rewritten to use page cache, (C) 1998 Stephen Tweedie
       9                 :            :  */
      10                 :            : #include <linux/mm.h>
      11                 :            : #include <linux/gfp.h>
      12                 :            : #include <linux/kernel_stat.h>
      13                 :            : #include <linux/swap.h>
      14                 :            : #include <linux/swapops.h>
      15                 :            : #include <linux/init.h>
      16                 :            : #include <linux/pagemap.h>
      17                 :            : #include <linux/backing-dev.h>
      18                 :            : #include <linux/blkdev.h>
      19                 :            : #include <linux/pagevec.h>
      20                 :            : #include <linux/migrate.h>
      21                 :            : #include <linux/vmalloc.h>
      22                 :            : #include <linux/swap_slots.h>
      23                 :            : #include <linux/huge_mm.h>
      24                 :            : 
      25                 :            : #include <asm/pgtable.h>
      26                 :            : #include "internal.h"
      27                 :            : 
      28                 :            : /*
      29                 :            :  * swapper_space is a fiction, retained to simplify the path through
      30                 :            :  * vmscan's shrink_page_list.
      31                 :            :  */
      32                 :            : static const struct address_space_operations swap_aops = {
      33                 :            :         .writepage      = swap_writepage,
      34                 :            :         .set_page_dirty = swap_set_page_dirty,
      35                 :            : #ifdef CONFIG_MIGRATION
      36                 :            :         .migratepage    = migrate_page,
      37                 :            : #endif
      38                 :            : };
      39                 :            : 
      40                 :            : struct address_space *swapper_spaces[MAX_SWAPFILES] __read_mostly;
      41                 :            : static unsigned int nr_swapper_spaces[MAX_SWAPFILES] __read_mostly;
      42                 :            : static bool enable_vma_readahead __read_mostly = true;
      43                 :            : 
      44                 :            : #define SWAP_RA_WIN_SHIFT       (PAGE_SHIFT / 2)
      45                 :            : #define SWAP_RA_HITS_MASK       ((1UL << SWAP_RA_WIN_SHIFT) - 1)
      46                 :            : #define SWAP_RA_HITS_MAX        SWAP_RA_HITS_MASK
      47                 :            : #define SWAP_RA_WIN_MASK        (~PAGE_MASK & ~SWAP_RA_HITS_MASK)
      48                 :            : 
      49                 :            : #define SWAP_RA_HITS(v)         ((v) & SWAP_RA_HITS_MASK)
      50                 :            : #define SWAP_RA_WIN(v)          (((v) & SWAP_RA_WIN_MASK) >> SWAP_RA_WIN_SHIFT)
      51                 :            : #define SWAP_RA_ADDR(v)         ((v) & PAGE_MASK)
      52                 :            : 
      53                 :            : #define SWAP_RA_VAL(addr, win, hits)                            \
      54                 :            :         (((addr) & PAGE_MASK) |                                     \
      55                 :            :          (((win) << SWAP_RA_WIN_SHIFT) & SWAP_RA_WIN_MASK) |  \
      56                 :            :          ((hits) & SWAP_RA_HITS_MASK))
      57                 :            : 
      58                 :            : /* Initial readahead hits is 4 to start up with a small window */
      59                 :            : #define GET_SWAP_RA_VAL(vma)                                    \
      60                 :            :         (atomic_long_read(&(vma)->swap_readahead_info) ? : 4)
      61                 :            : 
      62                 :            : #define INC_CACHE_INFO(x)       do { swap_cache_info.x++; } while (0)
      63                 :            : #define ADD_CACHE_INFO(x, nr)   do { swap_cache_info.x += (nr); } while (0)
      64                 :            : 
      65                 :            : static struct {
      66                 :            :         unsigned long add_total;
      67                 :            :         unsigned long del_total;
      68                 :            :         unsigned long find_success;
      69                 :            :         unsigned long find_total;
      70                 :            : } swap_cache_info;
      71                 :            : 
      72                 :          3 : unsigned long total_swapcache_pages(void)
      73                 :            : {
      74                 :            :         unsigned int i, j, nr;
      75                 :            :         unsigned long ret = 0;
      76                 :            :         struct address_space *spaces;
      77                 :            :         struct swap_info_struct *si;
      78                 :            : 
      79                 :          3 :         for (i = 0; i < MAX_SWAPFILES; i++) {
      80                 :            :                 swp_entry_t entry = swp_entry(i, 1);
      81                 :            : 
      82                 :            :                 /* Avoid get_swap_device() to warn for bad swap entry */
      83                 :          3 :                 if (!swp_swap_info(entry))
      84                 :          3 :                         continue;
      85                 :            :                 /* Prevent swapoff to free swapper_spaces */
      86                 :          0 :                 si = get_swap_device(entry);
      87                 :          0 :                 if (!si)
      88                 :          0 :                         continue;
      89                 :          0 :                 nr = nr_swapper_spaces[i];
      90                 :          0 :                 spaces = swapper_spaces[i];
      91                 :          0 :                 for (j = 0; j < nr; j++)
      92                 :          0 :                         ret += spaces[j].nrpages;
      93                 :            :                 put_swap_device(si);
      94                 :            :         }
      95                 :          3 :         return ret;
      96                 :            : }
      97                 :            : 
      98                 :            : static atomic_t swapin_readahead_hits = ATOMIC_INIT(4);
      99                 :            : 
     100                 :          0 : void show_swap_cache_info(void)
     101                 :            : {
     102                 :          0 :         printk("%lu pages in swap cache\n", total_swapcache_pages());
     103                 :          0 :         printk("Swap cache stats: add %lu, delete %lu, find %lu/%lu\n",
     104                 :            :                 swap_cache_info.add_total, swap_cache_info.del_total,
     105                 :            :                 swap_cache_info.find_success, swap_cache_info.find_total);
     106                 :          0 :         printk("Free swap  = %ldkB\n",
     107                 :            :                 get_nr_swap_pages() << (PAGE_SHIFT - 10));
     108                 :          0 :         printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
     109                 :          0 : }
     110                 :            : 
     111                 :            : /*
     112                 :            :  * add_to_swap_cache resembles add_to_page_cache_locked on swapper_space,
     113                 :            :  * but sets SwapCache flag and private instead of mapping and index.
     114                 :            :  */
     115                 :          0 : int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp)
     116                 :            : {
     117                 :          0 :         struct address_space *address_space = swap_address_space(entry);
     118                 :            :         pgoff_t idx = swp_offset(entry);
     119                 :          0 :         XA_STATE_ORDER(xas, &address_space->i_pages, idx, compound_order(page));
     120                 :            :         unsigned long i, nr = compound_nr(page);
     121                 :            : 
     122                 :            :         VM_BUG_ON_PAGE(!PageLocked(page), page);
     123                 :            :         VM_BUG_ON_PAGE(PageSwapCache(page), page);
     124                 :            :         VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
     125                 :            : 
     126                 :          0 :         page_ref_add(page, nr);
     127                 :            :         SetPageSwapCache(page);
     128                 :            : 
     129                 :            :         do {
     130                 :          0 :                 xas_lock_irq(&xas);
     131                 :          0 :                 xas_create_range(&xas);
     132                 :          0 :                 if (xas_error(&xas))
     133                 :            :                         goto unlock;
     134                 :          0 :                 for (i = 0; i < nr; i++) {
     135                 :            :                         VM_BUG_ON_PAGE(xas.xa_index != idx + i, page);
     136                 :          0 :                         set_page_private(page + i, entry.val + i);
     137                 :          0 :                         xas_store(&xas, page);
     138                 :          0 :                         xas_next(&xas);
     139                 :            :                 }
     140                 :          0 :                 address_space->nrpages += nr;
     141                 :          0 :                 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
     142                 :          0 :                 ADD_CACHE_INFO(add_total, nr);
     143                 :            : unlock:
     144                 :          0 :                 xas_unlock_irq(&xas);
     145                 :          0 :         } while (xas_nomem(&xas, gfp));
     146                 :            : 
     147                 :          0 :         if (!xas_error(&xas))
     148                 :            :                 return 0;
     149                 :            : 
     150                 :            :         ClearPageSwapCache(page);
     151                 :            :         page_ref_sub(page, nr);
     152                 :          0 :         return xas_error(&xas);
     153                 :            : }
     154                 :            : 
     155                 :            : /*
     156                 :            :  * This must be called only on pages that have
     157                 :            :  * been verified to be in the swap cache.
     158                 :            :  */
     159                 :          0 : void __delete_from_swap_cache(struct page *page, swp_entry_t entry)
     160                 :            : {
     161                 :          0 :         struct address_space *address_space = swap_address_space(entry);
     162                 :            :         int i, nr = hpage_nr_pages(page);
     163                 :            :         pgoff_t idx = swp_offset(entry);
     164                 :          0 :         XA_STATE(xas, &address_space->i_pages, idx);
     165                 :            : 
     166                 :            :         VM_BUG_ON_PAGE(!PageLocked(page), page);
     167                 :            :         VM_BUG_ON_PAGE(!PageSwapCache(page), page);
     168                 :            :         VM_BUG_ON_PAGE(PageWriteback(page), page);
     169                 :            : 
     170                 :          0 :         for (i = 0; i < nr; i++) {
     171                 :          0 :                 void *entry = xas_store(&xas, NULL);
     172                 :            :                 VM_BUG_ON_PAGE(entry != page, entry);
     173                 :          0 :                 set_page_private(page + i, 0);
     174                 :          0 :                 xas_next(&xas);
     175                 :            :         }
     176                 :            :         ClearPageSwapCache(page);
     177                 :          0 :         address_space->nrpages -= nr;
     178                 :          0 :         __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
     179                 :          0 :         ADD_CACHE_INFO(del_total, nr);
     180                 :          0 : }
     181                 :            : 
     182                 :            : /**
     183                 :            :  * add_to_swap - allocate swap space for a page
     184                 :            :  * @page: page we want to move to swap
     185                 :            :  *
     186                 :            :  * Allocate swap space for the page and add the page to the
     187                 :            :  * swap cache.  Caller needs to hold the page lock. 
     188                 :            :  */
     189                 :          0 : int add_to_swap(struct page *page)
     190                 :            : {
     191                 :            :         swp_entry_t entry;
     192                 :            :         int err;
     193                 :            : 
     194                 :            :         VM_BUG_ON_PAGE(!PageLocked(page), page);
     195                 :            :         VM_BUG_ON_PAGE(!PageUptodate(page), page);
     196                 :            : 
     197                 :          0 :         entry = get_swap_page(page);
     198                 :          0 :         if (!entry.val)
     199                 :            :                 return 0;
     200                 :            : 
     201                 :            :         /*
     202                 :            :          * XArray node allocations from PF_MEMALLOC contexts could
     203                 :            :          * completely exhaust the page allocator. __GFP_NOMEMALLOC
     204                 :            :          * stops emergency reserves from being allocated.
     205                 :            :          *
     206                 :            :          * TODO: this could cause a theoretical memory reclaim
     207                 :            :          * deadlock in the swap out path.
     208                 :            :          */
     209                 :            :         /*
     210                 :            :          * Add it to the swap cache.
     211                 :            :          */
     212                 :          0 :         err = add_to_swap_cache(page, entry,
     213                 :            :                         __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN);
     214                 :          0 :         if (err)
     215                 :            :                 /*
     216                 :            :                  * add_to_swap_cache() doesn't return -EEXIST, so we can safely
     217                 :            :                  * clear SWAP_HAS_CACHE flag.
     218                 :            :                  */
     219                 :            :                 goto fail;
     220                 :            :         /*
     221                 :            :          * Normally the page will be dirtied in unmap because its pte should be
     222                 :            :          * dirty. A special case is MADV_FREE page. The page'e pte could have
     223                 :            :          * dirty bit cleared but the page's SwapBacked bit is still set because
     224                 :            :          * clearing the dirty bit and SwapBacked bit has no lock protected. For
     225                 :            :          * such page, unmap will not set dirty bit for it, so page reclaim will
     226                 :            :          * not write the page out. This can cause data corruption when the page
     227                 :            :          * is swap in later. Always setting the dirty bit for the page solves
     228                 :            :          * the problem.
     229                 :            :          */
     230                 :          0 :         set_page_dirty(page);
     231                 :            : 
     232                 :          0 :         return 1;
     233                 :            : 
     234                 :            : fail:
     235                 :          0 :         put_swap_page(page, entry);
     236                 :          0 :         return 0;
     237                 :            : }
     238                 :            : 
     239                 :            : /*
     240                 :            :  * This must be called only on pages that have
     241                 :            :  * been verified to be in the swap cache and locked.
     242                 :            :  * It will never put the page into the free list,
     243                 :            :  * the caller has a reference on the page.
     244                 :            :  */
     245                 :          0 : void delete_from_swap_cache(struct page *page)
     246                 :            : {
     247                 :          0 :         swp_entry_t entry = { .val = page_private(page) };
     248                 :          0 :         struct address_space *address_space = swap_address_space(entry);
     249                 :            : 
     250                 :            :         xa_lock_irq(&address_space->i_pages);
     251                 :          0 :         __delete_from_swap_cache(page, entry);
     252                 :            :         xa_unlock_irq(&address_space->i_pages);
     253                 :            : 
     254                 :          0 :         put_swap_page(page, entry);
     255                 :            :         page_ref_sub(page, hpage_nr_pages(page));
     256                 :          0 : }
     257                 :            : 
     258                 :            : /* 
     259                 :            :  * If we are the only user, then try to free up the swap cache. 
     260                 :            :  * 
     261                 :            :  * Its ok to check for PageSwapCache without the page lock
     262                 :            :  * here because we are going to recheck again inside
     263                 :            :  * try_to_free_swap() _with_ the lock.
     264                 :            :  *                                      - Marcelo
     265                 :            :  */
     266                 :          3 : static inline void free_swap_cache(struct page *page)
     267                 :            : {
     268                 :          3 :         if (PageSwapCache(page) && !page_mapped(page) && trylock_page(page)) {
     269                 :          0 :                 try_to_free_swap(page);
     270                 :          0 :                 unlock_page(page);
     271                 :            :         }
     272                 :          3 : }
     273                 :            : 
     274                 :            : /* 
     275                 :            :  * Perform a free_page(), also freeing any swap cache associated with
     276                 :            :  * this page if it is the last user of the page.
     277                 :            :  */
     278                 :          0 : void free_page_and_swap_cache(struct page *page)
     279                 :            : {
     280                 :          0 :         free_swap_cache(page);
     281                 :            :         if (!is_huge_zero_page(page))
     282                 :          0 :                 put_page(page);
     283                 :          0 : }
     284                 :            : 
     285                 :            : /*
     286                 :            :  * Passed an array of pages, drop them all from swapcache and then release
     287                 :            :  * them.  They are removed from the LRU and freed if this is their last use.
     288                 :            :  */
     289                 :          3 : void free_pages_and_swap_cache(struct page **pages, int nr)
     290                 :            : {
     291                 :            :         struct page **pagep = pages;
     292                 :            :         int i;
     293                 :            : 
     294                 :          3 :         lru_add_drain();
     295                 :          3 :         for (i = 0; i < nr; i++)
     296                 :          3 :                 free_swap_cache(pagep[i]);
     297                 :          3 :         release_pages(pagep, nr);
     298                 :          3 : }
     299                 :            : 
     300                 :            : static inline bool swap_use_vma_readahead(void)
     301                 :            : {
     302                 :          0 :         return READ_ONCE(enable_vma_readahead) && !atomic_read(&nr_rotate_swap);
     303                 :            : }
     304                 :            : 
     305                 :            : /*
     306                 :            :  * Lookup a swap entry in the swap cache. A found page will be returned
     307                 :            :  * unlocked and with its refcount incremented - we rely on the kernel
     308                 :            :  * lock getting page table operations atomic even if we drop the page
     309                 :            :  * lock before returning.
     310                 :            :  */
     311                 :          0 : struct page *lookup_swap_cache(swp_entry_t entry, struct vm_area_struct *vma,
     312                 :            :                                unsigned long addr)
     313                 :            : {
     314                 :            :         struct page *page;
     315                 :            :         struct swap_info_struct *si;
     316                 :            : 
     317                 :          0 :         si = get_swap_device(entry);
     318                 :          0 :         if (!si)
     319                 :            :                 return NULL;
     320                 :          0 :         page = find_get_page(swap_address_space(entry), swp_offset(entry));
     321                 :            :         put_swap_device(si);
     322                 :            : 
     323                 :          0 :         INC_CACHE_INFO(find_total);
     324                 :          0 :         if (page) {
     325                 :            :                 bool vma_ra = swap_use_vma_readahead();
     326                 :            :                 bool readahead;
     327                 :            : 
     328                 :          0 :                 INC_CACHE_INFO(find_success);
     329                 :            :                 /*
     330                 :            :                  * At the moment, we don't support PG_readahead for anon THP
     331                 :            :                  * so let's bail out rather than confusing the readahead stat.
     332                 :            :                  */
     333                 :            :                 if (unlikely(PageTransCompound(page)))
     334                 :            :                         return page;
     335                 :            : 
     336                 :          0 :                 readahead = TestClearPageReadahead(page);
     337                 :          0 :                 if (vma && vma_ra) {
     338                 :            :                         unsigned long ra_val;
     339                 :            :                         int win, hits;
     340                 :            : 
     341                 :          0 :                         ra_val = GET_SWAP_RA_VAL(vma);
     342                 :          0 :                         win = SWAP_RA_WIN(ra_val);
     343                 :          0 :                         hits = SWAP_RA_HITS(ra_val);
     344                 :          0 :                         if (readahead)
     345                 :          0 :                                 hits = min_t(int, hits + 1, SWAP_RA_HITS_MAX);
     346                 :            :                         atomic_long_set(&vma->swap_readahead_info,
     347                 :          0 :                                         SWAP_RA_VAL(addr, win, hits));
     348                 :            :                 }
     349                 :            : 
     350                 :          0 :                 if (readahead) {
     351                 :            :                         count_vm_event(SWAP_RA_HIT);
     352                 :          0 :                         if (!vma || !vma_ra)
     353                 :            :                                 atomic_inc(&swapin_readahead_hits);
     354                 :            :                 }
     355                 :            :         }
     356                 :            : 
     357                 :          0 :         return page;
     358                 :            : }
     359                 :            : 
     360                 :          0 : struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
     361                 :            :                         struct vm_area_struct *vma, unsigned long addr,
     362                 :            :                         bool *new_page_allocated)
     363                 :            : {
     364                 :            :         struct page *found_page = NULL, *new_page = NULL;
     365                 :            :         struct swap_info_struct *si;
     366                 :            :         int err;
     367                 :          0 :         *new_page_allocated = false;
     368                 :            : 
     369                 :            :         do {
     370                 :            :                 /*
     371                 :            :                  * First check the swap cache.  Since this is normally
     372                 :            :                  * called after lookup_swap_cache() failed, re-calling
     373                 :            :                  * that would confuse statistics.
     374                 :            :                  */
     375                 :          0 :                 si = get_swap_device(entry);
     376                 :          0 :                 if (!si)
     377                 :            :                         break;
     378                 :          0 :                 found_page = find_get_page(swap_address_space(entry),
     379                 :            :                                            swp_offset(entry));
     380                 :            :                 put_swap_device(si);
     381                 :          0 :                 if (found_page)
     382                 :            :                         break;
     383                 :            : 
     384                 :            :                 /*
     385                 :            :                  * Just skip read ahead for unused swap slot.
     386                 :            :                  * During swap_off when swap_slot_cache is disabled,
     387                 :            :                  * we have to handle the race between putting
     388                 :            :                  * swap entry in swap cache and marking swap slot
     389                 :            :                  * as SWAP_HAS_CACHE.  That's done in later part of code or
     390                 :            :                  * else swap_off will be aborted if we return NULL.
     391                 :            :                  */
     392                 :          0 :                 if (!__swp_swapcount(entry) && swap_slot_cache_enabled)
     393                 :            :                         break;
     394                 :            : 
     395                 :            :                 /*
     396                 :            :                  * Get a new page to read into from swap.
     397                 :            :                  */
     398                 :          0 :                 if (!new_page) {
     399                 :            :                         new_page = alloc_page_vma(gfp_mask, vma, addr);
     400                 :          0 :                         if (!new_page)
     401                 :            :                                 break;          /* Out of memory */
     402                 :            :                 }
     403                 :            : 
     404                 :            :                 /*
     405                 :            :                  * Swap entry may have been freed since our caller observed it.
     406                 :            :                  */
     407                 :          0 :                 err = swapcache_prepare(entry);
     408                 :          0 :                 if (err == -EEXIST) {
     409                 :            :                         /*
     410                 :            :                          * We might race against get_swap_page() and stumble
     411                 :            :                          * across a SWAP_HAS_CACHE swap_map entry whose page
     412                 :            :                          * has not been brought into the swapcache yet.
     413                 :            :                          */
     414                 :          0 :                         cond_resched();
     415                 :          0 :                         continue;
     416                 :          0 :                 } else if (err)         /* swp entry is obsolete ? */
     417                 :            :                         break;
     418                 :            : 
     419                 :            :                 /* May fail (-ENOMEM) if XArray node allocation failed. */
     420                 :            :                 __SetPageLocked(new_page);
     421                 :            :                 __SetPageSwapBacked(new_page);
     422                 :          0 :                 err = add_to_swap_cache(new_page, entry,
     423                 :            :                                         gfp_mask & GFP_RECLAIM_MASK);
     424                 :          0 :                 if (likely(!err)) {
     425                 :            :                         /* Initiate read into locked page */
     426                 :            :                         SetPageWorkingset(new_page);
     427                 :          0 :                         lru_cache_add_anon(new_page);
     428                 :          0 :                         *new_page_allocated = true;
     429                 :          0 :                         return new_page;
     430                 :            :                 }
     431                 :            :                 __ClearPageLocked(new_page);
     432                 :            :                 /*
     433                 :            :                  * add_to_swap_cache() doesn't return -EEXIST, so we can safely
     434                 :            :                  * clear SWAP_HAS_CACHE flag.
     435                 :            :                  */
     436                 :          0 :                 put_swap_page(new_page, entry);
     437                 :          0 :         } while (err != -ENOMEM);
     438                 :            : 
     439                 :          0 :         if (new_page)
     440                 :          0 :                 put_page(new_page);
     441                 :          0 :         return found_page;
     442                 :            : }
     443                 :            : 
     444                 :            : /*
     445                 :            :  * Locate a page of swap in physical memory, reserving swap cache space
     446                 :            :  * and reading the disk if it is not already cached.
     447                 :            :  * A failure return means that either the page allocation failed or that
     448                 :            :  * the swap entry is no longer in use.
     449                 :            :  */
     450                 :          0 : struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
     451                 :            :                 struct vm_area_struct *vma, unsigned long addr, bool do_poll)
     452                 :            : {
     453                 :            :         bool page_was_allocated;
     454                 :          0 :         struct page *retpage = __read_swap_cache_async(entry, gfp_mask,
     455                 :            :                         vma, addr, &page_was_allocated);
     456                 :            : 
     457                 :          0 :         if (page_was_allocated)
     458                 :          0 :                 swap_readpage(retpage, do_poll);
     459                 :            : 
     460                 :          0 :         return retpage;
     461                 :            : }
     462                 :            : 
     463                 :            : static unsigned int __swapin_nr_pages(unsigned long prev_offset,
     464                 :            :                                       unsigned long offset,
     465                 :            :                                       int hits,
     466                 :            :                                       int max_pages,
     467                 :            :                                       int prev_win)
     468                 :            : {
     469                 :            :         unsigned int pages, last_ra;
     470                 :            : 
     471                 :            :         /*
     472                 :            :          * This heuristic has been found to work well on both sequential and
     473                 :            :          * random loads, swapping to hard disk or to SSD: please don't ask
     474                 :            :          * what the "+ 2" means, it just happens to work well, that's all.
     475                 :            :          */
     476                 :          0 :         pages = hits + 2;
     477                 :          0 :         if (pages == 2) {
     478                 :            :                 /*
     479                 :            :                  * We can have no readahead hits to judge by: but must not get
     480                 :            :                  * stuck here forever, so check for an adjacent offset instead
     481                 :            :                  * (and don't even bother to check whether swap type is same).
     482                 :            :                  */
     483                 :          0 :                 if (offset != prev_offset + 1 && offset != prev_offset - 1)
     484                 :            :                         pages = 1;
     485                 :            :         } else {
     486                 :            :                 unsigned int roundup = 4;
     487                 :          0 :                 while (roundup < pages)
     488                 :          0 :                         roundup <<= 1;
     489                 :          0 :                 pages = roundup;
     490                 :            :         }
     491                 :            : 
     492                 :          0 :         if (pages > max_pages)
     493                 :            :                 pages = max_pages;
     494                 :            : 
     495                 :            :         /* Don't shrink readahead too fast */
     496                 :          0 :         last_ra = prev_win / 2;
     497                 :          0 :         if (pages < last_ra)
     498                 :            :                 pages = last_ra;
     499                 :            : 
     500                 :            :         return pages;
     501                 :            : }
     502                 :            : 
     503                 :          0 : static unsigned long swapin_nr_pages(unsigned long offset)
     504                 :            : {
     505                 :            :         static unsigned long prev_offset;
     506                 :            :         unsigned int hits, pages, max_pages;
     507                 :            :         static atomic_t last_readahead_pages;
     508                 :            : 
     509                 :          0 :         max_pages = 1 << READ_ONCE(page_cluster);
     510                 :          0 :         if (max_pages <= 1)
     511                 :            :                 return 1;
     512                 :            : 
     513                 :          0 :         hits = atomic_xchg(&swapin_readahead_hits, 0);
     514                 :          0 :         pages = __swapin_nr_pages(prev_offset, offset, hits, max_pages,
     515                 :          0 :                                   atomic_read(&last_readahead_pages));
     516                 :          0 :         if (!hits)
     517                 :          0 :                 prev_offset = offset;
     518                 :            :         atomic_set(&last_readahead_pages, pages);
     519                 :            : 
     520                 :          0 :         return pages;
     521                 :            : }
     522                 :            : 
     523                 :            : /**
     524                 :            :  * swap_cluster_readahead - swap in pages in hope we need them soon
     525                 :            :  * @entry: swap entry of this memory
     526                 :            :  * @gfp_mask: memory allocation flags
     527                 :            :  * @vmf: fault information
     528                 :            :  *
     529                 :            :  * Returns the struct page for entry and addr, after queueing swapin.
     530                 :            :  *
     531                 :            :  * Primitive swap readahead code. We simply read an aligned block of
     532                 :            :  * (1 << page_cluster) entries in the swap area. This method is chosen
     533                 :            :  * because it doesn't cost us any seek time.  We also make sure to queue
     534                 :            :  * the 'original' request together with the readahead ones...
     535                 :            :  *
     536                 :            :  * This has been extended to use the NUMA policies from the mm triggering
     537                 :            :  * the readahead.
     538                 :            :  *
     539                 :            :  * Caller must hold read mmap_sem if vmf->vma is not NULL.
     540                 :            :  */
     541                 :          0 : struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
     542                 :            :                                 struct vm_fault *vmf)
     543                 :            : {
     544                 :            :         struct page *page;
     545                 :            :         unsigned long entry_offset = swp_offset(entry);
     546                 :            :         unsigned long offset = entry_offset;
     547                 :            :         unsigned long start_offset, end_offset;
     548                 :            :         unsigned long mask;
     549                 :          0 :         struct swap_info_struct *si = swp_swap_info(entry);
     550                 :            :         struct blk_plug plug;
     551                 :            :         bool do_poll = true, page_allocated;
     552                 :          0 :         struct vm_area_struct *vma = vmf->vma;
     553                 :          0 :         unsigned long addr = vmf->address;
     554                 :            : 
     555                 :          0 :         mask = swapin_nr_pages(offset) - 1;
     556                 :          0 :         if (!mask)
     557                 :            :                 goto skip;
     558                 :            : 
     559                 :            :         /* Test swap type to make sure the dereference is safe */
     560                 :          0 :         if (likely(si->flags & (SWP_BLKDEV | SWP_FS))) {
     561                 :          0 :                 struct inode *inode = si->swap_file->f_mapping->host;
     562                 :          0 :                 if (inode_read_congested(inode))
     563                 :            :                         goto skip;
     564                 :            :         }
     565                 :            : 
     566                 :            :         do_poll = false;
     567                 :            :         /* Read a page_cluster sized and aligned cluster around offset. */
     568                 :          0 :         start_offset = offset & ~mask;
     569                 :          0 :         end_offset = offset | mask;
     570                 :          0 :         if (!start_offset)      /* First page is swap header. */
     571                 :          0 :                 start_offset++;
     572                 :          0 :         if (end_offset >= si->max)
     573                 :          0 :                 end_offset = si->max - 1;
     574                 :            : 
     575                 :          0 :         blk_start_plug(&plug);
     576                 :          0 :         for (offset = start_offset; offset <= end_offset ; offset++) {
     577                 :            :                 /* Ok, do the async read-ahead now */
     578                 :          0 :                 page = __read_swap_cache_async(
     579                 :            :                         swp_entry(swp_type(entry), offset),
     580                 :            :                         gfp_mask, vma, addr, &page_allocated);
     581                 :          0 :                 if (!page)
     582                 :          0 :                         continue;
     583                 :          0 :                 if (page_allocated) {
     584                 :          0 :                         swap_readpage(page, false);
     585                 :          0 :                         if (offset != entry_offset) {
     586                 :            :                                 SetPageReadahead(page);
     587                 :            :                                 count_vm_event(SWAP_RA);
     588                 :            :                         }
     589                 :            :                 }
     590                 :          0 :                 put_page(page);
     591                 :            :         }
     592                 :          0 :         blk_finish_plug(&plug);
     593                 :            : 
     594                 :          0 :         lru_add_drain();        /* Push any new pages onto the LRU now */
     595                 :            : skip:
     596                 :          0 :         return read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll);
     597                 :            : }
     598                 :            : 
     599                 :          3 : int init_swap_address_space(unsigned int type, unsigned long nr_pages)
     600                 :            : {
     601                 :            :         struct address_space *spaces, *space;
     602                 :            :         unsigned int i, nr;
     603                 :            : 
     604                 :          3 :         nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES);
     605                 :            :         spaces = kvcalloc(nr, sizeof(struct address_space), GFP_KERNEL);
     606                 :          3 :         if (!spaces)
     607                 :            :                 return -ENOMEM;
     608                 :          3 :         for (i = 0; i < nr; i++) {
     609                 :          3 :                 space = spaces + i;
     610                 :            :                 xa_init_flags(&space->i_pages, XA_FLAGS_LOCK_IRQ);
     611                 :            :                 atomic_set(&space->i_mmap_writable, 0);
     612                 :          3 :                 space->a_ops = &swap_aops;
     613                 :            :                 /* swap cache doesn't use writeback related tags */
     614                 :            :                 mapping_set_no_writeback_tags(space);
     615                 :            :         }
     616                 :          3 :         nr_swapper_spaces[type] = nr;
     617                 :          3 :         swapper_spaces[type] = spaces;
     618                 :            : 
     619                 :          3 :         return 0;
     620                 :            : }
     621                 :            : 
     622                 :          0 : void exit_swap_address_space(unsigned int type)
     623                 :            : {
     624                 :          0 :         kvfree(swapper_spaces[type]);
     625                 :          0 :         nr_swapper_spaces[type] = 0;
     626                 :          0 :         swapper_spaces[type] = NULL;
     627                 :          0 : }
     628                 :            : 
     629                 :            : static inline void swap_ra_clamp_pfn(struct vm_area_struct *vma,
     630                 :            :                                      unsigned long faddr,
     631                 :            :                                      unsigned long lpfn,
     632                 :            :                                      unsigned long rpfn,
     633                 :            :                                      unsigned long *start,
     634                 :            :                                      unsigned long *end)
     635                 :            : {
     636                 :          0 :         *start = max3(lpfn, PFN_DOWN(vma->vm_start),
     637                 :            :                       PFN_DOWN(faddr & PMD_MASK));
     638                 :          0 :         *end = min3(rpfn, PFN_DOWN(vma->vm_end),
     639                 :            :                     PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE));
     640                 :            : }
     641                 :            : 
     642                 :          0 : static void swap_ra_info(struct vm_fault *vmf,
     643                 :            :                         struct vma_swap_readahead *ra_info)
     644                 :            : {
     645                 :          0 :         struct vm_area_struct *vma = vmf->vma;
     646                 :            :         unsigned long ra_val;
     647                 :            :         swp_entry_t entry;
     648                 :            :         unsigned long faddr, pfn, fpfn;
     649                 :            :         unsigned long start, end;
     650                 :            :         pte_t *pte, *orig_pte;
     651                 :            :         unsigned int max_win, hits, prev_win, win, left;
     652                 :            : #ifndef CONFIG_64BIT
     653                 :            :         pte_t *tpte;
     654                 :            : #endif
     655                 :            : 
     656                 :          0 :         max_win = 1 << min_t(unsigned int, READ_ONCE(page_cluster),
     657                 :            :                              SWAP_RA_ORDER_CEILING);
     658                 :          0 :         if (max_win == 1) {
     659                 :          0 :                 ra_info->win = 1;
     660                 :          0 :                 return;
     661                 :            :         }
     662                 :            : 
     663                 :          0 :         faddr = vmf->address;
     664                 :          0 :         orig_pte = pte = pte_offset_map(vmf->pmd, faddr);
     665                 :          0 :         entry = pte_to_swp_entry(*pte);
     666                 :          0 :         if ((unlikely(non_swap_entry(entry)))) {
     667                 :            :                 pte_unmap(orig_pte);
     668                 :            :                 return;
     669                 :            :         }
     670                 :            : 
     671                 :            :         fpfn = PFN_DOWN(faddr);
     672                 :          0 :         ra_val = GET_SWAP_RA_VAL(vma);
     673                 :          0 :         pfn = PFN_DOWN(SWAP_RA_ADDR(ra_val));
     674                 :          0 :         prev_win = SWAP_RA_WIN(ra_val);
     675                 :          0 :         hits = SWAP_RA_HITS(ra_val);
     676                 :          0 :         ra_info->win = win = __swapin_nr_pages(pfn, fpfn, hits,
     677                 :            :                                                max_win, prev_win);
     678                 :            :         atomic_long_set(&vma->swap_readahead_info,
     679                 :          0 :                         SWAP_RA_VAL(faddr, win, 0));
     680                 :            : 
     681                 :          0 :         if (win == 1) {
     682                 :            :                 pte_unmap(orig_pte);
     683                 :            :                 return;
     684                 :            :         }
     685                 :            : 
     686                 :            :         /* Copy the PTEs because the page table may be unmapped */
     687                 :          0 :         if (fpfn == pfn + 1)
     688                 :          0 :                 swap_ra_clamp_pfn(vma, faddr, fpfn, fpfn + win, &start, &end);
     689                 :          0 :         else if (pfn == fpfn + 1)
     690                 :          0 :                 swap_ra_clamp_pfn(vma, faddr, fpfn - win + 1, fpfn + 1,
     691                 :            :                                   &start, &end);
     692                 :            :         else {
     693                 :          0 :                 left = (win - 1) / 2;
     694                 :          0 :                 swap_ra_clamp_pfn(vma, faddr, fpfn - left, fpfn + win - left,
     695                 :            :                                   &start, &end);
     696                 :            :         }
     697                 :          0 :         ra_info->nr_pte = end - start;
     698                 :          0 :         ra_info->offset = fpfn - start;
     699                 :          0 :         pte -= ra_info->offset;
     700                 :            : #ifdef CONFIG_64BIT
     701                 :            :         ra_info->ptes = pte;
     702                 :            : #else
     703                 :          0 :         tpte = ra_info->ptes;
     704                 :          0 :         for (pfn = start; pfn != end; pfn++)
     705                 :          0 :                 *tpte++ = *pte++;
     706                 :            : #endif
     707                 :            :         pte_unmap(orig_pte);
     708                 :            : }
     709                 :            : 
     710                 :            : /**
     711                 :            :  * swap_vma_readahead - swap in pages in hope we need them soon
     712                 :            :  * @entry: swap entry of this memory
     713                 :            :  * @gfp_mask: memory allocation flags
     714                 :            :  * @vmf: fault information
     715                 :            :  *
     716                 :            :  * Returns the struct page for entry and addr, after queueing swapin.
     717                 :            :  *
     718                 :            :  * Primitive swap readahead code. We simply read in a few pages whoes
     719                 :            :  * virtual addresses are around the fault address in the same vma.
     720                 :            :  *
     721                 :            :  * Caller must hold read mmap_sem if vmf->vma is not NULL.
     722                 :            :  *
     723                 :            :  */
     724                 :          0 : static struct page *swap_vma_readahead(swp_entry_t fentry, gfp_t gfp_mask,
     725                 :            :                                        struct vm_fault *vmf)
     726                 :            : {
     727                 :            :         struct blk_plug plug;
     728                 :          0 :         struct vm_area_struct *vma = vmf->vma;
     729                 :            :         struct page *page;
     730                 :            :         pte_t *pte, pentry;
     731                 :            :         swp_entry_t entry;
     732                 :            :         unsigned int i;
     733                 :            :         bool page_allocated;
     734                 :          0 :         struct vma_swap_readahead ra_info = {0,};
     735                 :            : 
     736                 :          0 :         swap_ra_info(vmf, &ra_info);
     737                 :          0 :         if (ra_info.win == 1)
     738                 :            :                 goto skip;
     739                 :            : 
     740                 :          0 :         blk_start_plug(&plug);
     741                 :          0 :         for (i = 0, pte = ra_info.ptes; i < ra_info.nr_pte;
     742                 :          0 :              i++, pte++) {
     743                 :          0 :                 pentry = *pte;
     744                 :          0 :                 if (pte_none(pentry))
     745                 :          0 :                         continue;
     746                 :          0 :                 if (pte_present(pentry))
     747                 :          0 :                         continue;
     748                 :            :                 entry = pte_to_swp_entry(pentry);
     749                 :          0 :                 if (unlikely(non_swap_entry(entry)))
     750                 :          0 :                         continue;
     751                 :          0 :                 page = __read_swap_cache_async(entry, gfp_mask, vma,
     752                 :            :                                                vmf->address, &page_allocated);
     753                 :          0 :                 if (!page)
     754                 :          0 :                         continue;
     755                 :          0 :                 if (page_allocated) {
     756                 :          0 :                         swap_readpage(page, false);
     757                 :          0 :                         if (i != ra_info.offset) {
     758                 :            :                                 SetPageReadahead(page);
     759                 :            :                                 count_vm_event(SWAP_RA);
     760                 :            :                         }
     761                 :            :                 }
     762                 :          0 :                 put_page(page);
     763                 :            :         }
     764                 :          0 :         blk_finish_plug(&plug);
     765                 :          0 :         lru_add_drain();
     766                 :            : skip:
     767                 :          0 :         return read_swap_cache_async(fentry, gfp_mask, vma, vmf->address,
     768                 :          0 :                                      ra_info.win == 1);
     769                 :            : }
     770                 :            : 
     771                 :            : /**
     772                 :            :  * swapin_readahead - swap in pages in hope we need them soon
     773                 :            :  * @entry: swap entry of this memory
     774                 :            :  * @gfp_mask: memory allocation flags
     775                 :            :  * @vmf: fault information
     776                 :            :  *
     777                 :            :  * Returns the struct page for entry and addr, after queueing swapin.
     778                 :            :  *
     779                 :            :  * It's a main entry function for swap readahead. By the configuration,
     780                 :            :  * it will read ahead blocks by cluster-based(ie, physical disk based)
     781                 :            :  * or vma-based(ie, virtual address based on faulty address) readahead.
     782                 :            :  */
     783                 :          0 : struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
     784                 :            :                                 struct vm_fault *vmf)
     785                 :            : {
     786                 :          0 :         return swap_use_vma_readahead() ?
     787                 :          0 :                         swap_vma_readahead(entry, gfp_mask, vmf) :
     788                 :            :                         swap_cluster_readahead(entry, gfp_mask, vmf);
     789                 :            : }
     790                 :            : 
     791                 :            : #ifdef CONFIG_SYSFS
     792                 :          0 : static ssize_t vma_ra_enabled_show(struct kobject *kobj,
     793                 :            :                                      struct kobj_attribute *attr, char *buf)
     794                 :            : {
     795                 :          0 :         return sprintf(buf, "%s\n", enable_vma_readahead ? "true" : "false");
     796                 :            : }
     797                 :          0 : static ssize_t vma_ra_enabled_store(struct kobject *kobj,
     798                 :            :                                       struct kobj_attribute *attr,
     799                 :            :                                       const char *buf, size_t count)
     800                 :            : {
     801                 :          0 :         if (!strncmp(buf, "true", 4) || !strncmp(buf, "1", 1))
     802                 :          0 :                 enable_vma_readahead = true;
     803                 :          0 :         else if (!strncmp(buf, "false", 5) || !strncmp(buf, "0", 1))
     804                 :          0 :                 enable_vma_readahead = false;
     805                 :            :         else
     806                 :            :                 return -EINVAL;
     807                 :            : 
     808                 :          0 :         return count;
     809                 :            : }
     810                 :            : static struct kobj_attribute vma_ra_enabled_attr =
     811                 :            :         __ATTR(vma_ra_enabled, 0644, vma_ra_enabled_show,
     812                 :            :                vma_ra_enabled_store);
     813                 :            : 
     814                 :            : static struct attribute *swap_attrs[] = {
     815                 :            :         &vma_ra_enabled_attr.attr,
     816                 :            :         NULL,
     817                 :            : };
     818                 :            : 
     819                 :            : static struct attribute_group swap_attr_group = {
     820                 :            :         .attrs = swap_attrs,
     821                 :            : };
     822                 :            : 
     823                 :          3 : static int __init swap_init_sysfs(void)
     824                 :            : {
     825                 :            :         int err;
     826                 :            :         struct kobject *swap_kobj;
     827                 :            : 
     828                 :          3 :         swap_kobj = kobject_create_and_add("swap", mm_kobj);
     829                 :          3 :         if (!swap_kobj) {
     830                 :          0 :                 pr_err("failed to create swap kobject\n");
     831                 :          0 :                 return -ENOMEM;
     832                 :            :         }
     833                 :          3 :         err = sysfs_create_group(swap_kobj, &swap_attr_group);
     834                 :          3 :         if (err) {
     835                 :          0 :                 pr_err("failed to register swap group\n");
     836                 :            :                 goto delete_obj;
     837                 :            :         }
     838                 :            :         return 0;
     839                 :            : 
     840                 :            : delete_obj:
     841                 :          0 :         kobject_put(swap_kobj);
     842                 :          0 :         return err;
     843                 :            : }
     844                 :            : subsys_initcall(swap_init_sysfs);
     845                 :            : #endif
    

Generated by: LCOV version 1.14