Skip to content

Commit

Permalink
mm: swap: SWP_SYNCHRONOUS_IO: skip swapcache only if swapped page has…
Browse files Browse the repository at this point in the history
… no other reference

When SWP_SYNCHRONOUS_IO swapped-in pages are shared by several
processes, it can cause unnecessary memory wastage by skipping swap
cache.  Because, with swapin fault by read, they could share a page if
the page were in swap cache.  Thus, it avoids allocating same content
new pages.

This patch makes the swapcache skipping work only if the swap pte is
non-sharable.

[[email protected]: coding-style fixes]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Minchan Kim <[email protected]>
Cc: Dan Williams <[email protected]>
Cc: Ross Zwisler <[email protected]>
Cc: Hugh Dickins <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: Ilya Dryomov <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: Sergey Senozhatsky <[email protected]>
Cc: Huang Ying <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
minchank authored and torvalds committed Nov 16, 2017
1 parent 0bcac06 commit aa8d22a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
6 changes: 6 additions & 0 deletions include/linux/swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ extern unsigned int count_swap_pages(int, int);
extern sector_t map_swap_page(struct page *, struct block_device **);
extern sector_t swapdev_block(int, pgoff_t);
extern int page_swapcount(struct page *);
extern int __swap_count(struct swap_info_struct *si, swp_entry_t entry);
extern int __swp_swapcount(swp_entry_t entry);
extern int swp_swapcount(swp_entry_t entry);
extern struct swap_info_struct *page_swap_info(struct page *);
Expand Down Expand Up @@ -589,6 +590,11 @@ static inline int page_swapcount(struct page *page)
return 0;
}

static inline int __swap_count(struct swap_info_struct *si, swp_entry_t entry)
{
return 0;
}

static inline int __swp_swapcount(swp_entry_t entry)
{
return 0;
Expand Down
19 changes: 10 additions & 9 deletions mm/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2890,15 +2890,8 @@ int do_swap_page(struct vm_fault *vmf)
if (!page) {
struct swap_info_struct *si = swp_swap_info(entry);

if (!(si->flags & SWP_SYNCHRONOUS_IO)) {
if (vma_readahead)
page = do_swap_page_readahead(entry,
GFP_HIGHUSER_MOVABLE, vmf, &swap_ra);
else
page = swapin_readahead(entry,
GFP_HIGHUSER_MOVABLE, vma, vmf->address);
swapcache = page;
} else {
if (si->flags & SWP_SYNCHRONOUS_IO &&
__swap_count(si, entry) == 1) {
/* skip swapcache */
page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address);
if (page) {
Expand All @@ -2908,6 +2901,14 @@ int do_swap_page(struct vm_fault *vmf)
lru_cache_add_anon(page);
swap_readpage(page, true);
}
} else {
if (vma_readahead)
page = do_swap_page_readahead(entry,
GFP_HIGHUSER_MOVABLE, vmf, &swap_ra);
else
page = swapin_readahead(entry,
GFP_HIGHUSER_MOVABLE, vma, vmf->address);
swapcache = page;
}

if (!page) {
Expand Down
7 changes: 7 additions & 0 deletions mm/swapfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,13 @@ int page_swapcount(struct page *page)
return count;
}

int __swap_count(struct swap_info_struct *si, swp_entry_t entry)
{
pgoff_t offset = swp_offset(entry);

return swap_count(si->swap_map[offset]);
}

static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry)
{
int count = 0;
Expand Down

0 comments on commit aa8d22a

Please sign in to comment.