Skip to content

Commit

Permalink
mm: simplify try_to_unuse
Browse files Browse the repository at this point in the history
Remove the unused frontswap and pages_to_unuse arguments, and mark the
function static now that the caller in frontswap is gone.

[[email protected]: fix shmem_unuse() stub, per Matthew]

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Christoph Hellwig <[email protected]>
Reviewed-by: Juergen Gross <[email protected]>
Cc: Dan Streetman <[email protected]>
Cc: Geert Uytterhoeven <[email protected]>
Cc: Hugh Dickins <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Matthew Wilcox (Oracle) <[email protected]>
Cc: Seth Jennings <[email protected]>
Cc: Vitaly Wool <[email protected]>
Cc: Naresh Kamboju <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Christoph Hellwig authored and torvalds committed Jan 22, 2022
1 parent 360be5d commit 10a9c49
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 97 deletions.
7 changes: 0 additions & 7 deletions include/linux/frontswap.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@
#include <linux/bitops.h>
#include <linux/jump_label.h>

/*
* Return code to denote that requested number of
* frontswap pages are unused(moved to page cache).
* Used in shmem_unuse and try_to_unuse.
*/
#define FRONTSWAP_PAGES_UNUSED 2

struct frontswap_ops {
void (*init)(unsigned); /* this swap type was just swapon'ed */
int (*store)(unsigned, pgoff_t, struct page *); /* store a page */
Expand Down
3 changes: 1 addition & 2 deletions include/linux/shmem_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ extern void shmem_unlock_mapping(struct address_space *mapping);
extern struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
pgoff_t index, gfp_t gfp_mask);
extern void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end);
extern int shmem_unuse(unsigned int type, bool frontswap,
unsigned long *fs_pages_to_unuse);
int shmem_unuse(unsigned int type);

extern bool shmem_is_huge(struct vm_area_struct *vma,
struct inode *inode, pgoff_t index);
Expand Down
1 change: 0 additions & 1 deletion include/linux/swapfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
extern spinlock_t swap_lock;
extern struct plist_head swap_active_head;
extern struct swap_info_struct *swap_info[];
extern int try_to_unuse(unsigned int, bool, unsigned long);
extern unsigned long generic_max_swapfile_size(void);
extern unsigned long max_swapfile_size(void);

Expand Down
33 changes: 6 additions & 27 deletions mm/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include <linux/uio.h>
#include <linux/khugepaged.h>
#include <linux/hugetlb.h>
#include <linux/frontswap.h>
#include <linux/fs_parser.h>
#include <linux/swapfile.h>

Expand Down Expand Up @@ -1152,7 +1151,7 @@ static void shmem_evict_inode(struct inode *inode)
static int shmem_find_swap_entries(struct address_space *mapping,
pgoff_t start, unsigned int nr_entries,
struct page **entries, pgoff_t *indices,
unsigned int type, bool frontswap)
unsigned int type)
{
XA_STATE(xas, &mapping->i_pages, start);
struct page *page;
Expand All @@ -1173,9 +1172,6 @@ static int shmem_find_swap_entries(struct address_space *mapping,
entry = radix_to_swp_entry(page);
if (swp_type(entry) != type)
continue;
if (frontswap &&
!frontswap_test(swap_info[type], swp_offset(entry)))
continue;

indices[ret] = xas.xa_index;
entries[ret] = page;
Expand Down Expand Up @@ -1228,26 +1224,20 @@ static int shmem_unuse_swap_entries(struct inode *inode, struct pagevec pvec,
/*
* If swap found in inode, free it and move page from swapcache to filecache.
*/
static int shmem_unuse_inode(struct inode *inode, unsigned int type,
bool frontswap, unsigned long *fs_pages_to_unuse)
static int shmem_unuse_inode(struct inode *inode, unsigned int type)
{
struct address_space *mapping = inode->i_mapping;
pgoff_t start = 0;
struct pagevec pvec;
pgoff_t indices[PAGEVEC_SIZE];
bool frontswap_partial = (frontswap && *fs_pages_to_unuse > 0);
int ret = 0;

pagevec_init(&pvec);
do {
unsigned int nr_entries = PAGEVEC_SIZE;

if (frontswap_partial && *fs_pages_to_unuse < PAGEVEC_SIZE)
nr_entries = *fs_pages_to_unuse;

pvec.nr = shmem_find_swap_entries(mapping, start, nr_entries,
pvec.pages, indices,
type, frontswap);
pvec.pages, indices, type);
if (pvec.nr == 0) {
ret = 0;
break;
Expand All @@ -1257,14 +1247,6 @@ static int shmem_unuse_inode(struct inode *inode, unsigned int type,
if (ret < 0)
break;

if (frontswap_partial) {
*fs_pages_to_unuse -= ret;
if (*fs_pages_to_unuse == 0) {
ret = FRONTSWAP_PAGES_UNUSED;
break;
}
}

start = indices[pvec.nr - 1];
} while (true);

Expand All @@ -1276,8 +1258,7 @@ static int shmem_unuse_inode(struct inode *inode, unsigned int type,
* device 'type' back into memory, so the swap device can be
* unused.
*/
int shmem_unuse(unsigned int type, bool frontswap,
unsigned long *fs_pages_to_unuse)
int shmem_unuse(unsigned int type)
{
struct shmem_inode_info *info, *next;
int error = 0;
Expand All @@ -1300,8 +1281,7 @@ int shmem_unuse(unsigned int type, bool frontswap,
atomic_inc(&info->stop_eviction);
mutex_unlock(&shmem_swaplist_mutex);

error = shmem_unuse_inode(&info->vfs_inode, type, frontswap,
fs_pages_to_unuse);
error = shmem_unuse_inode(&info->vfs_inode, type);
cond_resched();

mutex_lock(&shmem_swaplist_mutex);
Expand Down Expand Up @@ -4015,8 +3995,7 @@ int __init shmem_init(void)
return 0;
}

int shmem_unuse(unsigned int type, bool frontswap,
unsigned long *fs_pages_to_unuse)
int shmem_unuse(unsigned int type)
{
return 0;
}
Expand Down
83 changes: 23 additions & 60 deletions mm/swapfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1923,8 +1923,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,

static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
unsigned long addr, unsigned long end,
unsigned int type, bool frontswap,
unsigned long *fs_pages_to_unuse)
unsigned int type)
{
struct page *page;
swp_entry_t entry;
Expand All @@ -1945,9 +1944,6 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
continue;

offset = swp_offset(entry);
if (frontswap && !frontswap_test(si, offset))
continue;

pte_unmap(pte);
swap_map = &si->swap_map[offset];
page = lookup_swap_cache(entry, vma, addr);
Expand Down Expand Up @@ -1979,11 +1975,6 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
try_to_free_swap(page);
unlock_page(page);
put_page(page);

if (*fs_pages_to_unuse && !--(*fs_pages_to_unuse)) {
ret = FRONTSWAP_PAGES_UNUSED;
goto out;
}
try_next:
pte = pte_offset_map(pmd, addr);
} while (pte++, addr += PAGE_SIZE, addr != end);
Expand All @@ -1996,8 +1987,7 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,

static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
unsigned long addr, unsigned long end,
unsigned int type, bool frontswap,
unsigned long *fs_pages_to_unuse)
unsigned int type)
{
pmd_t *pmd;
unsigned long next;
Expand All @@ -2009,8 +1999,7 @@ static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
next = pmd_addr_end(addr, end);
if (pmd_none_or_trans_huge_or_clear_bad(pmd))
continue;
ret = unuse_pte_range(vma, pmd, addr, next, type,
frontswap, fs_pages_to_unuse);
ret = unuse_pte_range(vma, pmd, addr, next, type);
if (ret)
return ret;
} while (pmd++, addr = next, addr != end);
Expand All @@ -2019,8 +2008,7 @@ static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,

static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d,
unsigned long addr, unsigned long end,
unsigned int type, bool frontswap,
unsigned long *fs_pages_to_unuse)
unsigned int type)
{
pud_t *pud;
unsigned long next;
Expand All @@ -2031,8 +2019,7 @@ static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d,
next = pud_addr_end(addr, end);
if (pud_none_or_clear_bad(pud))
continue;
ret = unuse_pmd_range(vma, pud, addr, next, type,
frontswap, fs_pages_to_unuse);
ret = unuse_pmd_range(vma, pud, addr, next, type);
if (ret)
return ret;
} while (pud++, addr = next, addr != end);
Expand All @@ -2041,8 +2028,7 @@ static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d,

static inline int unuse_p4d_range(struct vm_area_struct *vma, pgd_t *pgd,
unsigned long addr, unsigned long end,
unsigned int type, bool frontswap,
unsigned long *fs_pages_to_unuse)
unsigned int type)
{
p4d_t *p4d;
unsigned long next;
Expand All @@ -2053,16 +2039,14 @@ static inline int unuse_p4d_range(struct vm_area_struct *vma, pgd_t *pgd,
next = p4d_addr_end(addr, end);
if (p4d_none_or_clear_bad(p4d))
continue;
ret = unuse_pud_range(vma, p4d, addr, next, type,
frontswap, fs_pages_to_unuse);
ret = unuse_pud_range(vma, p4d, addr, next, type);
if (ret)
return ret;
} while (p4d++, addr = next, addr != end);
return 0;
}

static int unuse_vma(struct vm_area_struct *vma, unsigned int type,
bool frontswap, unsigned long *fs_pages_to_unuse)
static int unuse_vma(struct vm_area_struct *vma, unsigned int type)
{
pgd_t *pgd;
unsigned long addr, end, next;
Expand All @@ -2076,25 +2060,22 @@ static int unuse_vma(struct vm_area_struct *vma, unsigned int type,
next = pgd_addr_end(addr, end);
if (pgd_none_or_clear_bad(pgd))
continue;
ret = unuse_p4d_range(vma, pgd, addr, next, type,
frontswap, fs_pages_to_unuse);
ret = unuse_p4d_range(vma, pgd, addr, next, type);
if (ret)
return ret;
} while (pgd++, addr = next, addr != end);
return 0;
}

static int unuse_mm(struct mm_struct *mm, unsigned int type,
bool frontswap, unsigned long *fs_pages_to_unuse)
static int unuse_mm(struct mm_struct *mm, unsigned int type)
{
struct vm_area_struct *vma;
int ret = 0;

mmap_read_lock(mm);
for (vma = mm->mmap; vma; vma = vma->vm_next) {
if (vma->anon_vma) {
ret = unuse_vma(vma, type, frontswap,
fs_pages_to_unuse);
ret = unuse_vma(vma, type);
if (ret)
break;
}
Expand All @@ -2110,7 +2091,7 @@ static int unuse_mm(struct mm_struct *mm, unsigned int type,
* if there are no inuse entries after prev till end of the map.
*/
static unsigned int find_next_to_unuse(struct swap_info_struct *si,
unsigned int prev, bool frontswap)
unsigned int prev)
{
unsigned int i;
unsigned char count;
Expand All @@ -2124,8 +2105,7 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
for (i = prev + 1; i < si->max; i++) {
count = READ_ONCE(si->swap_map[i]);
if (count && swap_count(count) != SWAP_MAP_BAD)
if (!frontswap || frontswap_test(si, i))
break;
break;
if ((i % LATENCY_LIMIT) == 0)
cond_resched();
}
Expand All @@ -2136,12 +2116,7 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
return i;
}

/*
* If the boolean frontswap is true, only unuse pages_to_unuse pages;
* pages_to_unuse==0 means all pages; ignored if frontswap is false
*/
int try_to_unuse(unsigned int type, bool frontswap,
unsigned long pages_to_unuse)
static int try_to_unuse(unsigned int type)
{
struct mm_struct *prev_mm;
struct mm_struct *mm;
Expand All @@ -2155,13 +2130,10 @@ int try_to_unuse(unsigned int type, bool frontswap,
if (!READ_ONCE(si->inuse_pages))
return 0;

if (!frontswap)
pages_to_unuse = 0;

retry:
retval = shmem_unuse(type, frontswap, &pages_to_unuse);
retval = shmem_unuse(type);
if (retval)
goto out;
return retval;

prev_mm = &init_mm;
mmget(prev_mm);
Expand All @@ -2178,11 +2150,10 @@ int try_to_unuse(unsigned int type, bool frontswap,
spin_unlock(&mmlist_lock);
mmput(prev_mm);
prev_mm = mm;
retval = unuse_mm(mm, type, frontswap, &pages_to_unuse);

retval = unuse_mm(mm, type);
if (retval) {
mmput(prev_mm);
goto out;
return retval;
}

/*
Expand All @@ -2199,7 +2170,7 @@ int try_to_unuse(unsigned int type, bool frontswap,
i = 0;
while (READ_ONCE(si->inuse_pages) &&
!signal_pending(current) &&
(i = find_next_to_unuse(si, i, frontswap)) != 0) {
(i = find_next_to_unuse(si, i)) != 0) {

entry = swp_entry(type, i);
page = find_get_page(swap_address_space(entry), i);
Expand All @@ -2217,14 +2188,6 @@ int try_to_unuse(unsigned int type, bool frontswap,
try_to_free_swap(page);
unlock_page(page);
put_page(page);

/*
* For frontswap, we just need to unuse pages_to_unuse, if
* it was specified. Need not check frontswap again here as
* we already zeroed out pages_to_unuse if not frontswap.
*/
if (pages_to_unuse && --pages_to_unuse == 0)
goto out;
}

/*
Expand All @@ -2242,10 +2205,10 @@ int try_to_unuse(unsigned int type, bool frontswap,
if (READ_ONCE(si->inuse_pages)) {
if (!signal_pending(current))
goto retry;
retval = -EINTR;
return -EINTR;
}
out:
return (retval == FRONTSWAP_PAGES_UNUSED) ? 0 : retval;

return 0;
}

/*
Expand Down Expand Up @@ -2577,7 +2540,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
disable_swap_slots_cache_lock();

set_current_oom_origin();
err = try_to_unuse(p->type, false, 0); /* force unuse all pages */
err = try_to_unuse(p->type);
clear_current_oom_origin();

if (err) {
Expand Down

0 comments on commit 10a9c49

Please sign in to comment.