Skip to content

Commit 68a2239

Browse files
Rik van Rieltorvalds
authored andcommitted
vmscan: free swap space on swap-in/activation
If vm_swap_full() (swap space more than 50% full), the system will free swap space at swapin time. With this patch, the system will also free the swap space in the pageout code, when we decide that the page is not a candidate for swapout (and just wasting swap space). Signed-off-by: Rik van Riel <riel@redhat.com> Signed-off-by: Lee Schermerhorn <Lee.Schermerhorn@hp.com> Signed-off-by: MinChan Kim <minchan.kim@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f04e9eb commit 68a2239

5 files changed

Lines changed: 60 additions & 3 deletions

File tree

‎include/linux/pagevec.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ void __pagevec_release_nonlru(struct pagevec *pvec);
2525
void __pagevec_free(struct pagevec *pvec);
2626
void ____pagevec_lru_add(struct pagevec *pvec, enum lru_list lru);
2727
void pagevec_strip(struct pagevec *pvec);
28+
void pagevec_swap_free(struct pagevec *pvec);
2829
unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping,
2930
pgoff_t start, unsigned nr_pages);
3031
unsigned pagevec_lookup_tag(struct pagevec *pvec,

‎include/linux/swap.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ extern sector_t swapdev_block(int, pgoff_t);
265265
extern struct swap_info_struct *get_swap_info_struct(unsigned);
266266
extern int can_share_swap_page(struct page *);
267267
extern int remove_exclusive_swap_page(struct page *);
268+
extern int remove_exclusive_swap_page_ref(struct page *);
268269
struct backing_dev_info;
269270

270271
/* linux/mm/thrash.c */
@@ -353,6 +354,11 @@ static inline int remove_exclusive_swap_page(struct page *p)
353354
return 0;
354355
}
355356

357+
static inline int remove_exclusive_swap_page_ref(struct page *page)
358+
{
359+
return 0;
360+
}
361+
356362
static inline swp_entry_t get_swap_page(void)
357363
{
358364
swp_entry_t entry;

‎mm/swap.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,30 @@ void pagevec_strip(struct pagevec *pvec)
427427
}
428428
}
429429

430+
/**
431+
* pagevec_swap_free - try to free swap space from the pages in a pagevec
432+
* @pvec: pagevec with swapcache pages to free the swap space of
433+
*
434+
* The caller needs to hold an extra reference to each page and
435+
* not hold the page lock on the pages. This function uses a
436+
* trylock on the page lock so it may not always free the swap
437+
* space associated with a page.
438+
*/
439+
void pagevec_swap_free(struct pagevec *pvec)
440+
{
441+
int i;
442+
443+
for (i = 0; i < pagevec_count(pvec); i++) {
444+
struct page *page = pvec->pages[i];
445+
446+
if (PageSwapCache(page) && trylock_page(page)) {
447+
if (PageSwapCache(page))
448+
remove_exclusive_swap_page_ref(page);
449+
unlock_page(page);
450+
}
451+
}
452+
}
453+
430454
/**
431455
* pagevec_lookup - gang pagecache lookup
432456
* @pvec: Where the resulting pages are placed

‎mm/swapfile.c‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ int can_share_swap_page(struct page *page)
344344
* Work out if there are any other processes sharing this
345345
* swap cache page. Free it if you can. Return success.
346346
*/
347-
int remove_exclusive_swap_page(struct page *page)
347+
static int remove_exclusive_swap_page_count(struct page *page, int count)
348348
{
349349
int retval;
350350
struct swap_info_struct * p;
@@ -357,7 +357,7 @@ int remove_exclusive_swap_page(struct page *page)
357357
return 0;
358358
if (PageWriteback(page))
359359
return 0;
360-
if (page_count(page) != 2) /* 2: us + cache */
360+
if (page_count(page) != count) /* us + cache + ptes */
361361
return 0;
362362

363363
entry.val = page_private(page);
@@ -370,7 +370,7 @@ int remove_exclusive_swap_page(struct page *page)
370370
if (p->swap_map[swp_offset(entry)] == 1) {
371371
/* Recheck the page count with the swapcache lock held.. */
372372
spin_lock_irq(&swapper_space.tree_lock);
373-
if ((page_count(page) == 2) && !PageWriteback(page)) {
373+
if ((page_count(page) == count) && !PageWriteback(page)) {
374374
__delete_from_swap_cache(page);
375375
SetPageDirty(page);
376376
retval = 1;
@@ -387,6 +387,25 @@ int remove_exclusive_swap_page(struct page *page)
387387
return retval;
388388
}
389389

390+
/*
391+
* Most of the time the page should have two references: one for the
392+
* process and one for the swap cache.
393+
*/
394+
int remove_exclusive_swap_page(struct page *page)
395+
{
396+
return remove_exclusive_swap_page_count(page, 2);
397+
}
398+
399+
/*
400+
* The pageout code holds an extra reference to the page. That raises
401+
* the reference count to test for to 2 for a page that is only in the
402+
* swap cache plus 1 for each process that maps the page.
403+
*/
404+
int remove_exclusive_swap_page_ref(struct page *page)
405+
{
406+
return remove_exclusive_swap_page_count(page, 2 + page_mapcount(page));
407+
}
408+
390409
/*
391410
* Free the swap entry like above, but also try to
392411
* free the page cache entry if it is the last user.

‎mm/vmscan.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,9 @@ static unsigned long shrink_page_list(struct list_head *page_list,
647647
continue;
648648

649649
activate_locked:
650+
/* Not a candidate for swapping, so reclaim swap space. */
651+
if (PageSwapCache(page) && vm_swap_full())
652+
remove_exclusive_swap_page_ref(page);
650653
SetPageActive(page);
651654
pgactivate++;
652655
keep_locked:
@@ -1228,6 +1231,8 @@ static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
12281231
__mod_zone_page_state(zone, NR_ACTIVE, pgmoved);
12291232
pgmoved = 0;
12301233
spin_unlock_irq(&zone->lru_lock);
1234+
if (vm_swap_full())
1235+
pagevec_swap_free(&pvec);
12311236
__pagevec_release(&pvec);
12321237
spin_lock_irq(&zone->lru_lock);
12331238
}
@@ -1237,6 +1242,8 @@ static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
12371242
__count_zone_vm_events(PGREFILL, zone, pgscanned);
12381243
__count_vm_events(PGDEACTIVATE, pgdeactivate);
12391244
spin_unlock_irq(&zone->lru_lock);
1245+
if (vm_swap_full())
1246+
pagevec_swap_free(&pvec);
12401247

12411248
pagevec_release(&pvec);
12421249
}

0 commit comments

Comments
 (0)