i2c: mxs: use MXS_DMA_CTRL_WAIT4END instead of DMA_CTRL_ACK
[linux/fpc-iii.git] / mm / swap_state.c
blob4ce014dc4571ac3c3cbdf00a83d30e1c8ab695b2
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/mm/swap_state.c
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
6 * Swap reorganised 29.12.95, Stephen Tweedie
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>
25 #include <asm/pgtable.h>
26 #include "internal.h"
29 * swapper_space is a fiction, retained to simplify the path through
30 * vmscan's shrink_page_list.
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
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;
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)
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)
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))
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)
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)
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;
72 unsigned long total_swapcache_pages(void)
74 unsigned int i, j, nr;
75 unsigned long ret = 0;
76 struct address_space *spaces;
77 struct swap_info_struct *si;
79 for (i = 0; i < MAX_SWAPFILES; i++) {
80 swp_entry_t entry = swp_entry(i, 1);
82 /* Avoid get_swap_device() to warn for bad swap entry */
83 if (!swp_swap_info(entry))
84 continue;
85 /* Prevent swapoff to free swapper_spaces */
86 si = get_swap_device(entry);
87 if (!si)
88 continue;
89 nr = nr_swapper_spaces[i];
90 spaces = swapper_spaces[i];
91 for (j = 0; j < nr; j++)
92 ret += spaces[j].nrpages;
93 put_swap_device(si);
95 return ret;
98 static atomic_t swapin_readahead_hits = ATOMIC_INIT(4);
100 void show_swap_cache_info(void)
102 printk("%lu pages in swap cache\n", total_swapcache_pages());
103 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 printk("Free swap = %ldkB\n",
107 get_nr_swap_pages() << (PAGE_SHIFT - 10));
108 printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
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.
115 int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp)
117 struct address_space *address_space = swap_address_space(entry);
118 pgoff_t idx = swp_offset(entry);
119 XA_STATE_ORDER(xas, &address_space->i_pages, idx, compound_order(page));
120 unsigned long i, nr = compound_nr(page);
122 VM_BUG_ON_PAGE(!PageLocked(page), page);
123 VM_BUG_ON_PAGE(PageSwapCache(page), page);
124 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
126 page_ref_add(page, nr);
127 SetPageSwapCache(page);
129 do {
130 xas_lock_irq(&xas);
131 xas_create_range(&xas);
132 if (xas_error(&xas))
133 goto unlock;
134 for (i = 0; i < nr; i++) {
135 VM_BUG_ON_PAGE(xas.xa_index != idx + i, page);
136 set_page_private(page + i, entry.val + i);
137 xas_store(&xas, page);
138 xas_next(&xas);
140 address_space->nrpages += nr;
141 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
142 ADD_CACHE_INFO(add_total, nr);
143 unlock:
144 xas_unlock_irq(&xas);
145 } while (xas_nomem(&xas, gfp));
147 if (!xas_error(&xas))
148 return 0;
150 ClearPageSwapCache(page);
151 page_ref_sub(page, nr);
152 return xas_error(&xas);
156 * This must be called only on pages that have
157 * been verified to be in the swap cache.
159 void __delete_from_swap_cache(struct page *page, swp_entry_t entry)
161 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 XA_STATE(xas, &address_space->i_pages, idx);
166 VM_BUG_ON_PAGE(!PageLocked(page), page);
167 VM_BUG_ON_PAGE(!PageSwapCache(page), page);
168 VM_BUG_ON_PAGE(PageWriteback(page), page);
170 for (i = 0; i < nr; i++) {
171 void *entry = xas_store(&xas, NULL);
172 VM_BUG_ON_PAGE(entry != page, entry);
173 set_page_private(page + i, 0);
174 xas_next(&xas);
176 ClearPageSwapCache(page);
177 address_space->nrpages -= nr;
178 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
179 ADD_CACHE_INFO(del_total, nr);
183 * add_to_swap - allocate swap space for a page
184 * @page: page we want to move to swap
186 * Allocate swap space for the page and add the page to the
187 * swap cache. Caller needs to hold the page lock.
189 int add_to_swap(struct page *page)
191 swp_entry_t entry;
192 int err;
194 VM_BUG_ON_PAGE(!PageLocked(page), page);
195 VM_BUG_ON_PAGE(!PageUptodate(page), page);
197 entry = get_swap_page(page);
198 if (!entry.val)
199 return 0;
202 * XArray node allocations from PF_MEMALLOC contexts could
203 * completely exhaust the page allocator. __GFP_NOMEMALLOC
204 * stops emergency reserves from being allocated.
206 * TODO: this could cause a theoretical memory reclaim
207 * deadlock in the swap out path.
210 * Add it to the swap cache.
212 err = add_to_swap_cache(page, entry,
213 __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN);
214 if (err)
216 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
217 * clear SWAP_HAS_CACHE flag.
219 goto fail;
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.
230 set_page_dirty(page);
232 return 1;
234 fail:
235 put_swap_page(page, entry);
236 return 0;
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.
245 void delete_from_swap_cache(struct page *page)
247 swp_entry_t entry = { .val = page_private(page) };
248 struct address_space *address_space = swap_address_space(entry);
250 xa_lock_irq(&address_space->i_pages);
251 __delete_from_swap_cache(page, entry);
252 xa_unlock_irq(&address_space->i_pages);
254 put_swap_page(page, entry);
255 page_ref_sub(page, hpage_nr_pages(page));
259 * If we are the only user, then try to free up the swap cache.
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
266 static inline void free_swap_cache(struct page *page)
268 if (PageSwapCache(page) && !page_mapped(page) && trylock_page(page)) {
269 try_to_free_swap(page);
270 unlock_page(page);
275 * Perform a free_page(), also freeing any swap cache associated with
276 * this page if it is the last user of the page.
278 void free_page_and_swap_cache(struct page *page)
280 free_swap_cache(page);
281 if (!is_huge_zero_page(page))
282 put_page(page);
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.
289 void free_pages_and_swap_cache(struct page **pages, int nr)
291 struct page **pagep = pages;
292 int i;
294 lru_add_drain();
295 for (i = 0; i < nr; i++)
296 free_swap_cache(pagep[i]);
297 release_pages(pagep, nr);
300 static inline bool swap_use_vma_readahead(void)
302 return READ_ONCE(enable_vma_readahead) && !atomic_read(&nr_rotate_swap);
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.
311 struct page *lookup_swap_cache(swp_entry_t entry, struct vm_area_struct *vma,
312 unsigned long addr)
314 struct page *page;
315 struct swap_info_struct *si;
317 si = get_swap_device(entry);
318 if (!si)
319 return NULL;
320 page = find_get_page(swap_address_space(entry), swp_offset(entry));
321 put_swap_device(si);
323 INC_CACHE_INFO(find_total);
324 if (page) {
325 bool vma_ra = swap_use_vma_readahead();
326 bool readahead;
328 INC_CACHE_INFO(find_success);
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.
333 if (unlikely(PageTransCompound(page)))
334 return page;
336 readahead = TestClearPageReadahead(page);
337 if (vma && vma_ra) {
338 unsigned long ra_val;
339 int win, hits;
341 ra_val = GET_SWAP_RA_VAL(vma);
342 win = SWAP_RA_WIN(ra_val);
343 hits = SWAP_RA_HITS(ra_val);
344 if (readahead)
345 hits = min_t(int, hits + 1, SWAP_RA_HITS_MAX);
346 atomic_long_set(&vma->swap_readahead_info,
347 SWAP_RA_VAL(addr, win, hits));
350 if (readahead) {
351 count_vm_event(SWAP_RA_HIT);
352 if (!vma || !vma_ra)
353 atomic_inc(&swapin_readahead_hits);
357 return page;
360 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)
364 struct page *found_page = NULL, *new_page = NULL;
365 struct swap_info_struct *si;
366 int err;
367 *new_page_allocated = false;
369 do {
371 * First check the swap cache. Since this is normally
372 * called after lookup_swap_cache() failed, re-calling
373 * that would confuse statistics.
375 si = get_swap_device(entry);
376 if (!si)
377 break;
378 found_page = find_get_page(swap_address_space(entry),
379 swp_offset(entry));
380 put_swap_device(si);
381 if (found_page)
382 break;
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.
392 if (!__swp_swapcount(entry) && swap_slot_cache_enabled)
393 break;
396 * Get a new page to read into from swap.
398 if (!new_page) {
399 new_page = alloc_page_vma(gfp_mask, vma, addr);
400 if (!new_page)
401 break; /* Out of memory */
405 * Swap entry may have been freed since our caller observed it.
407 err = swapcache_prepare(entry);
408 if (err == -EEXIST) {
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.
414 cond_resched();
415 continue;
416 } else if (err) /* swp entry is obsolete ? */
417 break;
419 /* May fail (-ENOMEM) if XArray node allocation failed. */
420 __SetPageLocked(new_page);
421 __SetPageSwapBacked(new_page);
422 err = add_to_swap_cache(new_page, entry,
423 gfp_mask & GFP_RECLAIM_MASK);
424 if (likely(!err)) {
425 /* Initiate read into locked page */
426 SetPageWorkingset(new_page);
427 lru_cache_add_anon(new_page);
428 *new_page_allocated = true;
429 return new_page;
431 __ClearPageLocked(new_page);
433 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
434 * clear SWAP_HAS_CACHE flag.
436 put_swap_page(new_page, entry);
437 } while (err != -ENOMEM);
439 if (new_page)
440 put_page(new_page);
441 return found_page;
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.
450 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)
453 bool page_was_allocated;
454 struct page *retpage = __read_swap_cache_async(entry, gfp_mask,
455 vma, addr, &page_was_allocated);
457 if (page_was_allocated)
458 swap_readpage(retpage, do_poll);
460 return retpage;
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)
469 unsigned int pages, last_ra;
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.
476 pages = hits + 2;
477 if (pages == 2) {
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).
483 if (offset != prev_offset + 1 && offset != prev_offset - 1)
484 pages = 1;
485 } else {
486 unsigned int roundup = 4;
487 while (roundup < pages)
488 roundup <<= 1;
489 pages = roundup;
492 if (pages > max_pages)
493 pages = max_pages;
495 /* Don't shrink readahead too fast */
496 last_ra = prev_win / 2;
497 if (pages < last_ra)
498 pages = last_ra;
500 return pages;
503 static unsigned long swapin_nr_pages(unsigned long offset)
505 static unsigned long prev_offset;
506 unsigned int hits, pages, max_pages;
507 static atomic_t last_readahead_pages;
509 max_pages = 1 << READ_ONCE(page_cluster);
510 if (max_pages <= 1)
511 return 1;
513 hits = atomic_xchg(&swapin_readahead_hits, 0);
514 pages = __swapin_nr_pages(prev_offset, offset, hits, max_pages,
515 atomic_read(&last_readahead_pages));
516 if (!hits)
517 prev_offset = offset;
518 atomic_set(&last_readahead_pages, pages);
520 return pages;
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
529 * Returns the struct page for entry and addr, after queueing swapin.
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...
536 * This has been extended to use the NUMA policies from the mm triggering
537 * the readahead.
539 * Caller must hold read mmap_sem if vmf->vma is not NULL.
541 struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
542 struct vm_fault *vmf)
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 struct swap_info_struct *si = swp_swap_info(entry);
550 struct blk_plug plug;
551 bool do_poll = true, page_allocated;
552 struct vm_area_struct *vma = vmf->vma;
553 unsigned long addr = vmf->address;
555 mask = swapin_nr_pages(offset) - 1;
556 if (!mask)
557 goto skip;
559 /* Test swap type to make sure the dereference is safe */
560 if (likely(si->flags & (SWP_BLKDEV | SWP_FS))) {
561 struct inode *inode = si->swap_file->f_mapping->host;
562 if (inode_read_congested(inode))
563 goto skip;
566 do_poll = false;
567 /* Read a page_cluster sized and aligned cluster around offset. */
568 start_offset = offset & ~mask;
569 end_offset = offset | mask;
570 if (!start_offset) /* First page is swap header. */
571 start_offset++;
572 if (end_offset >= si->max)
573 end_offset = si->max - 1;
575 blk_start_plug(&plug);
576 for (offset = start_offset; offset <= end_offset ; offset++) {
577 /* Ok, do the async read-ahead now */
578 page = __read_swap_cache_async(
579 swp_entry(swp_type(entry), offset),
580 gfp_mask, vma, addr, &page_allocated);
581 if (!page)
582 continue;
583 if (page_allocated) {
584 swap_readpage(page, false);
585 if (offset != entry_offset) {
586 SetPageReadahead(page);
587 count_vm_event(SWAP_RA);
590 put_page(page);
592 blk_finish_plug(&plug);
594 lru_add_drain(); /* Push any new pages onto the LRU now */
595 skip:
596 return read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll);
599 int init_swap_address_space(unsigned int type, unsigned long nr_pages)
601 struct address_space *spaces, *space;
602 unsigned int i, nr;
604 nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES);
605 spaces = kvcalloc(nr, sizeof(struct address_space), GFP_KERNEL);
606 if (!spaces)
607 return -ENOMEM;
608 for (i = 0; i < nr; i++) {
609 space = spaces + i;
610 xa_init_flags(&space->i_pages, XA_FLAGS_LOCK_IRQ);
611 atomic_set(&space->i_mmap_writable, 0);
612 space->a_ops = &swap_aops;
613 /* swap cache doesn't use writeback related tags */
614 mapping_set_no_writeback_tags(space);
616 nr_swapper_spaces[type] = nr;
617 swapper_spaces[type] = spaces;
619 return 0;
622 void exit_swap_address_space(unsigned int type)
624 kvfree(swapper_spaces[type]);
625 nr_swapper_spaces[type] = 0;
626 swapper_spaces[type] = NULL;
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)
636 *start = max3(lpfn, PFN_DOWN(vma->vm_start),
637 PFN_DOWN(faddr & PMD_MASK));
638 *end = min3(rpfn, PFN_DOWN(vma->vm_end),
639 PFN_DOWN((faddr & PMD_MASK) + PMD_SIZE));
642 static void swap_ra_info(struct vm_fault *vmf,
643 struct vma_swap_readahead *ra_info)
645 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
656 max_win = 1 << min_t(unsigned int, READ_ONCE(page_cluster),
657 SWAP_RA_ORDER_CEILING);
658 if (max_win == 1) {
659 ra_info->win = 1;
660 return;
663 faddr = vmf->address;
664 orig_pte = pte = pte_offset_map(vmf->pmd, faddr);
665 entry = pte_to_swp_entry(*pte);
666 if ((unlikely(non_swap_entry(entry)))) {
667 pte_unmap(orig_pte);
668 return;
671 fpfn = PFN_DOWN(faddr);
672 ra_val = GET_SWAP_RA_VAL(vma);
673 pfn = PFN_DOWN(SWAP_RA_ADDR(ra_val));
674 prev_win = SWAP_RA_WIN(ra_val);
675 hits = SWAP_RA_HITS(ra_val);
676 ra_info->win = win = __swapin_nr_pages(pfn, fpfn, hits,
677 max_win, prev_win);
678 atomic_long_set(&vma->swap_readahead_info,
679 SWAP_RA_VAL(faddr, win, 0));
681 if (win == 1) {
682 pte_unmap(orig_pte);
683 return;
686 /* Copy the PTEs because the page table may be unmapped */
687 if (fpfn == pfn + 1)
688 swap_ra_clamp_pfn(vma, faddr, fpfn, fpfn + win, &start, &end);
689 else if (pfn == fpfn + 1)
690 swap_ra_clamp_pfn(vma, faddr, fpfn - win + 1, fpfn + 1,
691 &start, &end);
692 else {
693 left = (win - 1) / 2;
694 swap_ra_clamp_pfn(vma, faddr, fpfn - left, fpfn + win - left,
695 &start, &end);
697 ra_info->nr_pte = end - start;
698 ra_info->offset = fpfn - start;
699 pte -= ra_info->offset;
700 #ifdef CONFIG_64BIT
701 ra_info->ptes = pte;
702 #else
703 tpte = ra_info->ptes;
704 for (pfn = start; pfn != end; pfn++)
705 *tpte++ = *pte++;
706 #endif
707 pte_unmap(orig_pte);
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
716 * Returns the struct page for entry and addr, after queueing swapin.
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.
721 * Caller must hold read mmap_sem if vmf->vma is not NULL.
724 static struct page *swap_vma_readahead(swp_entry_t fentry, gfp_t gfp_mask,
725 struct vm_fault *vmf)
727 struct blk_plug plug;
728 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 struct vma_swap_readahead ra_info = {0,};
736 swap_ra_info(vmf, &ra_info);
737 if (ra_info.win == 1)
738 goto skip;
740 blk_start_plug(&plug);
741 for (i = 0, pte = ra_info.ptes; i < ra_info.nr_pte;
742 i++, pte++) {
743 pentry = *pte;
744 if (pte_none(pentry))
745 continue;
746 if (pte_present(pentry))
747 continue;
748 entry = pte_to_swp_entry(pentry);
749 if (unlikely(non_swap_entry(entry)))
750 continue;
751 page = __read_swap_cache_async(entry, gfp_mask, vma,
752 vmf->address, &page_allocated);
753 if (!page)
754 continue;
755 if (page_allocated) {
756 swap_readpage(page, false);
757 if (i != ra_info.offset) {
758 SetPageReadahead(page);
759 count_vm_event(SWAP_RA);
762 put_page(page);
764 blk_finish_plug(&plug);
765 lru_add_drain();
766 skip:
767 return read_swap_cache_async(fentry, gfp_mask, vma, vmf->address,
768 ra_info.win == 1);
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
777 * Returns the struct page for entry and addr, after queueing swapin.
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.
783 struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
784 struct vm_fault *vmf)
786 return swap_use_vma_readahead() ?
787 swap_vma_readahead(entry, gfp_mask, vmf) :
788 swap_cluster_readahead(entry, gfp_mask, vmf);
791 #ifdef CONFIG_SYSFS
792 static ssize_t vma_ra_enabled_show(struct kobject *kobj,
793 struct kobj_attribute *attr, char *buf)
795 return sprintf(buf, "%s\n", enable_vma_readahead ? "true" : "false");
797 static ssize_t vma_ra_enabled_store(struct kobject *kobj,
798 struct kobj_attribute *attr,
799 const char *buf, size_t count)
801 if (!strncmp(buf, "true", 4) || !strncmp(buf, "1", 1))
802 enable_vma_readahead = true;
803 else if (!strncmp(buf, "false", 5) || !strncmp(buf, "0", 1))
804 enable_vma_readahead = false;
805 else
806 return -EINVAL;
808 return count;
810 static struct kobj_attribute vma_ra_enabled_attr =
811 __ATTR(vma_ra_enabled, 0644, vma_ra_enabled_show,
812 vma_ra_enabled_store);
814 static struct attribute *swap_attrs[] = {
815 &vma_ra_enabled_attr.attr,
816 NULL,
819 static struct attribute_group swap_attr_group = {
820 .attrs = swap_attrs,
823 static int __init swap_init_sysfs(void)
825 int err;
826 struct kobject *swap_kobj;
828 swap_kobj = kobject_create_and_add("swap", mm_kobj);
829 if (!swap_kobj) {
830 pr_err("failed to create swap kobject\n");
831 return -ENOMEM;
833 err = sysfs_create_group(swap_kobj, &swap_attr_group);
834 if (err) {
835 pr_err("failed to register swap group\n");
836 goto delete_obj;
838 return 0;
840 delete_obj:
841 kobject_put(swap_kobj);
842 return err;
844 subsys_initcall(swap_init_sysfs);
845 #endif