/* * Free the swap entry like above, but also try to * free the page cache entry if it is the last user. */ void free_swap_and_cache(swp_entry_t entry) { struct swap_info_struct * p; struct page *page = NULL; if (is_migration_entry(entry)) return; p = swap_info_get(entry); if (p) { if (swap_entry_free(p, swp_offset(entry)) == 1) { page = find_get_page(&swapper_space, entry.val); if (page && unlikely(TestSetPageLocked(page))) { page_cache_release(page); page = NULL; } } spin_unlock(&swap_lock); } if (page) { int one_user; BUG_ON(PagePrivate(page)); one_user = (page_count(page) == 2); /* Only cache user (+us), or swap space full? Free it! */ /* Also recheck PageSwapCache after page is locked (above) */ if (PageSwapCache(page) && !PageWriteback(page) && (one_user || vm_swap_full())) { delete_from_swap_cache(page); SetPageDirty(page); } unlock_page(page); page_cache_release(page); } }
/* * Caller has made sure that the swapdevice corresponding to entry * is still around or has not been recycled. */ void swap_free(swp_entry_t entry) { struct swap_info_struct * p; p = swap_info_get(entry); if (p) { swap_entry_free(p, SWP_OFFSET(entry)); swap_info_put(p); } }
/* * Caller has made sure that the swapdevice corresponding to entry * is still around or has not been recycled. */ void swap_free(swp_entry_t entry) { struct swap_info_struct * p; p = swap_info_get(entry); if (p) { swap_entry_free(p, swp_offset(entry)); spin_unlock(&swap_lock); } }
/* * How many references to page are currently swapped out? */ static inline int page_swapcount(struct page *page) { int count = 0; struct swap_info_struct *p; swp_entry_t entry; entry.val = page_private(page); p = swap_info_get(entry); if (p) { /* Subtract the 1 for the swap cache itself */ count = p->swap_map[swp_offset(entry)] - 1; spin_unlock(&swap_lock); } return count; }
/* * Work out if there are any other processes sharing this * swap cache page. Free it if you can. Return success. */ int remove_exclusive_swap_page(struct page *page) { int retval; struct swap_info_struct * p; swp_entry_t entry; BUG_ON(PagePrivate(page)); BUG_ON(!PageLocked(page)); if (!PageSwapCache(page)) return 0; if (PageWriteback(page)) return 0; if (page_count(page) != 2) /* 2: us + cache */ return 0; entry.val = page_private(page); p = swap_info_get(entry); if (!p) return 0; /* Is the only swap cache user the cache itself? */ retval = 0; if (p->swap_map[swp_offset(entry)] == 1) { /* Recheck the page count with the swapcache lock held.. */ write_lock_irq(&swapper_space.tree_lock); if ((page_count(page) == 2) && !PageWriteback(page)) { __delete_from_swap_cache(page); SetPageDirty(page); retval = 1; } write_unlock_irq(&swapper_space.tree_lock); } spin_unlock(&swap_lock); if (retval) { swap_free(entry); page_cache_release(page); } return retval; }
/* /proc/kpageswapn - an array exposing page swap counts * * Each entry is a u64 representing the corresponding * physical page swap count. */ static ssize_t kpageswapn_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { u64 __user *out = (u64 __user *)buf; unsigned long src = *ppos, dst; swp_entry_t swap_entry; ssize_t ret = 0; struct swap_info_struct *p; dst = src / KPMSIZE; /* Format the swap entry from the corresponding pagemap value */ swap_entry = swp_entry(dst >> (SWP_TYPE_SHIFT(swap_entry) + RADIX_TREE_EXCEPTIONAL_SHIFT), dst & SWP_OFFSET_MASK(swap_entry)); /*pr_info("kpageswapn_read src: %lx\n", src); */ /*pr_info("kpageswapn_read swap entry: %lx\n", swap_entry.val); */ if (src & KPMMASK || count & KPMMASK) { pr_err("kpageswapn_read return EINVAL\n"); return -EINVAL; } p = swap_info_get(swap_entry); if (p) { u64 swapcount = swap_count(p->swap_map[swp_offset(swap_entry)]); if (put_user(swapcount, out)) { pr_err("pageswapn_read put user failed\n"); ret = -EFAULT; } swap_info_unlock(p); } else { pr_err("kpageswapn_read swap_info_get failed\n"); ret = -EFAULT; } if (!ret) { *ppos += KPMSIZE; ret = KPMSIZE; } return ret; }
/* * Check if we're the only user of a swap page, * when the page is locked. */ static int exclusive_swap_page(struct page *page) { int retval = 0; struct swap_info_struct * p; swp_entry_t entry; entry.val = page->index; p = swap_info_get(entry); if (p) { /* Is the only swap cache user the cache itself? */ if (p->swap_map[SWP_OFFSET(entry)] == 1) { /* Recheck the page count with the pagecache lock held.. */ spin_lock(&pagecache_lock); if (page_count(page) - !!page->buffers == 2) retval = 1; spin_unlock(&pagecache_lock); } swap_info_put(p); } return retval; }
/* * Work out if there are any other processes sharing this * swap cache page. Free it if you can. Return success. */ int remove_exclusive_swap_page(struct page *page) { int retval; struct swap_info_struct * p; swp_entry_t entry; if (!PageLocked(page)) BUG(); if (!PageSwapCache(page)) return 0; if (page_count(page) - !!page->buffers != 2) /* 2: us + cache */ return 0; entry.val = page->index; p = swap_info_get(entry); if (!p) return 0; /* Is the only swap cache user the cache itself? */ retval = 0; if (p->swap_map[SWP_OFFSET(entry)] == 1) { /* Recheck the page count with the pagecache lock held.. */ spin_lock(&pagecache_lock); if (page_count(page) - !!page->buffers == 2) { __delete_from_swap_cache(page); SetPageDirty(page); retval = 1; } spin_unlock(&pagecache_lock); } swap_info_put(p); if (retval) { block_flushpage(page, 0); swap_free(entry); page_cache_release(page); } return retval; }
/* * Free the swap entry like above, but also try to * free the page cache entry if it is the last user. */ void free_swap_and_cache(swp_entry_t entry) { struct swap_info_struct * p; struct page *page = NULL; p = swap_info_get(entry); if (p) { if (swap_entry_free(p, SWP_OFFSET(entry)) == 1) page = find_trylock_page(&swapper_space, entry.val); swap_info_put(p); } if (page) { page_cache_get(page); /* Only cache user (+us), or swap space full? Free it! */ if (page_count(page) - !!page->buffers == 2 || vm_swap_full()) { delete_from_swap_cache(page); SetPageDirty(page); } UnlockPage(page); page_cache_release(page); } }