Skip to content

Commit

Permalink
swap_info: note SWAP_MAP_SHMEM
Browse files Browse the repository at this point in the history
While we're fiddling with the swap_map values, let's assign a particular
value to shmem/tmpfs swap pages: their swap counts are never incremented,
and it helps swapoff's try_to_unuse() a little if it can immediately
distinguish those pages from process pages.

Since we've no use for SWAP_MAP_BAD | COUNT_CONTINUED,
we might as well use that 0xbf value for SWAP_MAP_SHMEM.

Signed-off-by: Hugh Dickins <[email protected]>
Reviewed-by: KAMEZAWA Hiroyuki <[email protected]>
Cc: Rik van Riel <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Hugh Dickins authored and torvalds committed Dec 15, 2009
1 parent 570a335 commit aaa4686
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
6 changes: 6 additions & 0 deletions include/linux/swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ enum {
#define SWAP_HAS_CACHE 0x40 /* Flag page is cached, in first swap_map */
#define SWAP_CONT_MAX 0x7f /* Max count, in each swap_map continuation */
#define COUNT_CONTINUED 0x80 /* See swap_map continuation for full count */
#define SWAP_MAP_SHMEM 0xbf /* Owned by shmem/tmpfs, in first swap_map */

/*
* The in-memory structure used to track swap areas.
Expand Down Expand Up @@ -316,6 +317,7 @@ extern swp_entry_t get_swap_page(void);
extern swp_entry_t get_swap_page_of_type(int);
extern int valid_swaphandles(swp_entry_t, unsigned long *);
extern int add_swap_count_continuation(swp_entry_t, gfp_t);
extern void swap_shmem_alloc(swp_entry_t);
extern int swap_duplicate(swp_entry_t);
extern int swapcache_prepare(swp_entry_t);
extern void swap_free(swp_entry_t);
Expand Down Expand Up @@ -394,6 +396,10 @@ static inline int add_swap_count_continuation(swp_entry_t swp, gfp_t gfp_mask)
return 0;
}

static inline void swap_shmem_alloc(swp_entry_t swp)
{
}

static inline int swap_duplicate(swp_entry_t swp)
{
return 0;
Expand Down
11 changes: 9 additions & 2 deletions mm/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,14 @@ int shmem_unuse(swp_entry_t entry, struct page *page)
goto out;
}
mutex_unlock(&shmem_swaplist_mutex);
out: return found; /* 0 or 1 or -ENOMEM */
/*
* Can some race bring us here? We've been holding page lock,
* so I think not; but would rather try again later than BUG()
*/
unlock_page(page);
page_cache_release(page);
out:
return (found < 0) ? found : 0;
}

/*
Expand Down Expand Up @@ -1080,7 +1087,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
else
inode = NULL;
spin_unlock(&info->lock);
swap_duplicate(swap);
swap_shmem_alloc(swap);
BUG_ON(page_mapped(page));
page_cache_release(page); /* pagecache ref */
swap_writepage(page, wbc);
Expand Down
47 changes: 27 additions & 20 deletions mm/swapfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,12 @@ static unsigned char swap_entry_free(struct swap_info_struct *p,
if (usage == SWAP_HAS_CACHE) {
VM_BUG_ON(!has_cache);
has_cache = 0;
} else if (count == SWAP_MAP_SHMEM) {
/*
* Or we could insist on shmem.c using a special
* swap_shmem_free() and free_shmem_swap_and_cache()...
*/
count = 0;
} else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
if (count == COUNT_CONTINUED) {
if (swap_count_continued(p, offset, count))
Expand Down Expand Up @@ -1031,7 +1037,6 @@ static int try_to_unuse(unsigned int type)
swp_entry_t entry;
unsigned int i = 0;
int retval = 0;
int shmem;

/*
* When searching mms for an entry, a good strategy is to
Expand Down Expand Up @@ -1107,17 +1112,18 @@ static int try_to_unuse(unsigned int type)

/*
* Remove all references to entry.
* Whenever we reach init_mm, there's no address space
* to search, but use it as a reminder to search shmem.
*/
shmem = 0;
swcount = *swap_map;
if (swap_count(swcount)) {
if (start_mm == &init_mm)
shmem = shmem_unuse(entry, page);
else
retval = unuse_mm(start_mm, entry, page);
if (swap_count(swcount) == SWAP_MAP_SHMEM) {
retval = shmem_unuse(entry, page);
/* page has already been unlocked and released */
if (retval < 0)
break;
continue;
}
if (swap_count(swcount) && start_mm != &init_mm)
retval = unuse_mm(start_mm, entry, page);

if (swap_count(*swap_map)) {
int set_start_mm = (*swap_map >= swcount);
struct list_head *p = &start_mm->mmlist;
Expand All @@ -1128,7 +1134,7 @@ static int try_to_unuse(unsigned int type)
atomic_inc(&new_start_mm->mm_users);
atomic_inc(&prev_mm->mm_users);
spin_lock(&mmlist_lock);
while (swap_count(*swap_map) && !retval && !shmem &&
while (swap_count(*swap_map) && !retval &&
(p = p->next) != &start_mm->mmlist) {
mm = list_entry(p, struct mm_struct, mmlist);
if (!atomic_inc_not_zero(&mm->mm_users))
Expand All @@ -1142,10 +1148,9 @@ static int try_to_unuse(unsigned int type)
swcount = *swap_map;
if (!swap_count(swcount)) /* any usage ? */
;
else if (mm == &init_mm) {
else if (mm == &init_mm)
set_start_mm = 1;
shmem = shmem_unuse(entry, page);
} else
else
retval = unuse_mm(mm, entry, page);

if (set_start_mm && *swap_map < swcount) {
Expand All @@ -1161,13 +1166,6 @@ static int try_to_unuse(unsigned int type)
mmput(start_mm);
start_mm = new_start_mm;
}
if (shmem) {
/* page has already been unlocked and released */
if (shmem > 0)
continue;
retval = shmem;
break;
}
if (retval) {
unlock_page(page);
page_cache_release(page);
Expand Down Expand Up @@ -2126,6 +2124,15 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
goto out;
}

/*
* Help swapoff by noting that swap entry belongs to shmem/tmpfs
* (in which case its reference count is never incremented).
*/
void swap_shmem_alloc(swp_entry_t entry)
{
__swap_duplicate(entry, SWAP_MAP_SHMEM);
}

/*
* increase reference count of swap entry by 1.
*/
Expand Down

0 comments on commit aaa4686

Please sign in to comment.