Skip to content

Commit aa8d22a

Browse files
minchanktorvalds
authored andcommitted
mm: swap: SWP_SYNCHRONOUS_IO: skip swapcache only if swapped page has 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. [akpm@linux-foundation.org: coding-style fixes] Link: http://lkml.kernel.org/r/1507620825-5537-1-git-send-email-minchan@kernel.org Signed-off-by: Minchan Kim <minchan@kernel.org> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Ross Zwisler <ross.zwisler@linux.intel.com> Cc: Hugh Dickins <hughd@google.com> Cc: Christoph Hellwig <hch@lst.de> Cc: Ilya Dryomov <idryomov@gmail.com> Cc: Jens Axboe <axboe@kernel.dk> Cc: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Cc: Huang Ying <ying.huang@intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0bcac06 commit aa8d22a

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

‎include/linux/swap.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ extern unsigned int count_swap_pages(int, int);
463463
extern sector_t map_swap_page(struct page *, struct block_device **);
464464
extern sector_t swapdev_block(int, pgoff_t);
465465
extern int page_swapcount(struct page *);
466+
extern int __swap_count(struct swap_info_struct *si, swp_entry_t entry);
466467
extern int __swp_swapcount(swp_entry_t entry);
467468
extern int swp_swapcount(swp_entry_t entry);
468469
extern struct swap_info_struct *page_swap_info(struct page *);
@@ -589,6 +590,11 @@ static inline int page_swapcount(struct page *page)
589590
return 0;
590591
}
591592

593+
static inline int __swap_count(struct swap_info_struct *si, swp_entry_t entry)
594+
{
595+
return 0;
596+
}
597+
592598
static inline int __swp_swapcount(swp_entry_t entry)
593599
{
594600
return 0;

‎mm/memory.c‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,15 +2890,8 @@ int do_swap_page(struct vm_fault *vmf)
28902890
if (!page) {
28912891
struct swap_info_struct *si = swp_swap_info(entry);
28922892

2893-
if (!(si->flags & SWP_SYNCHRONOUS_IO)) {
2894-
if (vma_readahead)
2895-
page = do_swap_page_readahead(entry,
2896-
GFP_HIGHUSER_MOVABLE, vmf, &swap_ra);
2897-
else
2898-
page = swapin_readahead(entry,
2899-
GFP_HIGHUSER_MOVABLE, vma, vmf->address);
2900-
swapcache = page;
2901-
} else {
2893+
if (si->flags & SWP_SYNCHRONOUS_IO &&
2894+
__swap_count(si, entry) == 1) {
29022895
/* skip swapcache */
29032896
page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vmf->address);
29042897
if (page) {
@@ -2908,6 +2901,14 @@ int do_swap_page(struct vm_fault *vmf)
29082901
lru_cache_add_anon(page);
29092902
swap_readpage(page, true);
29102903
}
2904+
} else {
2905+
if (vma_readahead)
2906+
page = do_swap_page_readahead(entry,
2907+
GFP_HIGHUSER_MOVABLE, vmf, &swap_ra);
2908+
else
2909+
page = swapin_readahead(entry,
2910+
GFP_HIGHUSER_MOVABLE, vma, vmf->address);
2911+
swapcache = page;
29112912
}
29122913

29132914
if (!page) {

‎mm/swapfile.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,13 @@ int page_swapcount(struct page *page)
13281328
return count;
13291329
}
13301330

1331+
int __swap_count(struct swap_info_struct *si, swp_entry_t entry)
1332+
{
1333+
pgoff_t offset = swp_offset(entry);
1334+
1335+
return swap_count(si->swap_map[offset]);
1336+
}
1337+
13311338
static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry)
13321339
{
13331340
int count = 0;

0 commit comments

Comments
 (0)