1 // SPDX-License-Identifier: GPL-2.0
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
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>
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
,
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)
66 unsigned long add_total
;
67 unsigned long del_total
;
68 unsigned long find_success
;
69 unsigned long find_total
;
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
))
85 /* Prevent swapoff to free swapper_spaces */
86 si
= get_swap_device(entry
);
89 nr
= nr_swapper_spaces
[i
];
90 spaces
= swapper_spaces
[i
];
91 for (j
= 0; j
< nr
; j
++)
92 ret
+= spaces
[j
].nrpages
;
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
);
131 xas_create_range(&xas
);
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
);
140 address_space
->nrpages
+= nr
;
141 __mod_node_page_state(page_pgdat(page
), NR_FILE_PAGES
, nr
);
142 ADD_CACHE_INFO(add_total
, nr
);
144 xas_unlock_irq(&xas
);
145 } while (xas_nomem(&xas
, gfp
));
147 if (!xas_error(&xas
))
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);
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
)
194 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
195 VM_BUG_ON_PAGE(!PageUptodate(page
), page
);
197 entry
= get_swap_page(page
);
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
);
216 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
217 * clear SWAP_HAS_CACHE flag.
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
230 set_page_dirty(page
);
235 put_swap_page(page
, entry
);
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.
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
);
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
))
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
;
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
,
315 struct swap_info_struct
*si
;
317 si
= get_swap_device(entry
);
320 page
= find_get_page(swap_address_space(entry
), swp_offset(entry
));
323 INC_CACHE_INFO(find_total
);
325 bool vma_ra
= swap_use_vma_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
)))
336 readahead
= TestClearPageReadahead(page
);
338 unsigned long ra_val
;
341 ra_val
= GET_SWAP_RA_VAL(vma
);
342 win
= SWAP_RA_WIN(ra_val
);
343 hits
= SWAP_RA_HITS(ra_val
);
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
));
351 count_vm_event(SWAP_RA_HIT
);
353 atomic_inc(&swapin_readahead_hits
);
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
;
367 *new_page_allocated
= false;
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
);
378 found_page
= find_get_page(swap_address_space(entry
),
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
)
396 * Get a new page to read into from swap.
399 new_page
= alloc_page_vma(gfp_mask
, vma
, addr
);
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.
416 } else if (err
) /* swp entry is obsolete ? */
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
);
425 /* Initiate read into locked page */
426 SetPageWorkingset(new_page
);
427 lru_cache_add_anon(new_page
);
428 *new_page_allocated
= true;
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
);
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
);
463 static unsigned int __swapin_nr_pages(unsigned long prev_offset
,
464 unsigned long offset
,
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.
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)
486 unsigned int roundup
= 4;
487 while (roundup
< pages
)
492 if (pages
> max_pages
)
495 /* Don't shrink readahead too fast */
496 last_ra
= prev_win
/ 2;
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
);
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
));
517 prev_offset
= offset
;
518 atomic_set(&last_readahead_pages
, 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
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
)
545 unsigned long entry_offset
= swp_offset(entry
);
546 unsigned long offset
= entry_offset
;
547 unsigned long start_offset
, end_offset
;
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;
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
))
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. */
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
);
583 if (page_allocated
) {
584 swap_readpage(page
, false);
585 if (offset
!= entry_offset
) {
586 SetPageReadahead(page
);
587 count_vm_event(SWAP_RA
);
592 blk_finish_plug(&plug
);
594 lru_add_drain(); /* Push any new pages onto the LRU now */
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
;
604 nr
= DIV_ROUND_UP(nr_pages
, SWAP_ADDRESS_SPACE_PAGES
);
605 spaces
= kvcalloc(nr
, sizeof(struct address_space
), GFP_KERNEL
);
608 for (i
= 0; i
< nr
; 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
;
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
,
633 unsigned long *start
,
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
;
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
;
656 max_win
= 1 << min_t(unsigned int, READ_ONCE(page_cluster
),
657 SWAP_RA_ORDER_CEILING
);
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
)))) {
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
,
678 atomic_long_set(&vma
->swap_readahead_info
,
679 SWAP_RA_VAL(faddr
, win
, 0));
686 /* Copy the PTEs because the page table may be unmapped */
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,
693 left
= (win
- 1) / 2;
694 swap_ra_clamp_pfn(vma
, faddr
, fpfn
- left
, fpfn
+ win
- left
,
697 ra_info
->nr_pte
= end
- start
;
698 ra_info
->offset
= fpfn
- start
;
699 pte
-= ra_info
->offset
;
703 tpte
= ra_info
->ptes
;
704 for (pfn
= start
; pfn
!= end
; pfn
++)
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
;
734 struct vma_swap_readahead ra_info
= {0,};
736 swap_ra_info(vmf
, &ra_info
);
737 if (ra_info
.win
== 1)
740 blk_start_plug(&plug
);
741 for (i
= 0, pte
= ra_info
.ptes
; i
< ra_info
.nr_pte
;
744 if (pte_none(pentry
))
746 if (pte_present(pentry
))
748 entry
= pte_to_swp_entry(pentry
);
749 if (unlikely(non_swap_entry(entry
)))
751 page
= __read_swap_cache_async(entry
, gfp_mask
, vma
,
752 vmf
->address
, &page_allocated
);
755 if (page_allocated
) {
756 swap_readpage(page
, false);
757 if (i
!= ra_info
.offset
) {
758 SetPageReadahead(page
);
759 count_vm_event(SWAP_RA
);
764 blk_finish_plug(&plug
);
767 return read_swap_cache_async(fentry
, gfp_mask
, vma
, vmf
->address
,
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
);
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;
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
,
819 static struct attribute_group swap_attr_group
= {
823 static int __init
swap_init_sysfs(void)
826 struct kobject
*swap_kobj
;
828 swap_kobj
= kobject_create_and_add("swap", mm_kobj
);
830 pr_err("failed to create swap kobject\n");
833 err
= sysfs_create_group(swap_kobj
, &swap_attr_group
);
835 pr_err("failed to register swap group\n");
841 kobject_put(swap_kobj
);
844 subsys_initcall(swap_init_sysfs
);