The counter of free swap turn to negtive during swapoff, this phenomenon can be observed in oom log, such as: 1226 total pagecache pages 47 pages in swap cache Swap cache stats: add 119633, delete 119814, find 26832/82125 Free swap = -31796kB Total swap = 0kB 1048576 pages RAM Subtract the size of swap partition from free swap should put behind try_to_unuse, otherwise, it will result in negtive counter if swap partition is in use. Fixes: ec8acf20afb8 ("swap: add per-partition lock for swapfile") Signed-off-by: Chen Wandun <chenwandun@xxxxxxxxxx> --- mm/swapfile.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 72e481aacd5d..5b9b695be84b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2316,8 +2316,6 @@ static void setup_swap_info(struct swap_info_struct *p, int prio, static void _enable_swap_info(struct swap_info_struct *p) { p->flags |= SWP_WRITEOK; - atomic_long_add(p->pages, &nr_swap_pages); - total_swap_pages += p->pages; assert_spin_locked(&swap_lock); /* @@ -2353,6 +2351,8 @@ static void enable_swap_info(struct swap_info_struct *p, int prio, spin_lock(&swap_lock); spin_lock(&p->lock); _enable_swap_info(p); + atomic_long_add(p->pages, &nr_swap_pages); + total_swap_pages += p->pages; spin_unlock(&p->lock); spin_unlock(&swap_lock); } @@ -2444,8 +2444,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) least_priority++; } plist_del(&p->list, &swap_active_head); - atomic_long_sub(p->pages, &nr_swap_pages); - total_swap_pages -= p->pages; p->flags &= ~SWP_WRITEOK; spin_unlock(&p->lock); spin_unlock(&swap_lock); @@ -2500,6 +2498,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) spin_lock(&p->lock); } + atomic_long_sub(p->pages, &nr_swap_pages); + total_swap_pages -= p->pages; swap_file = p->swap_file; old_block_size = p->old_block_size; p->swap_file = NULL; -- 2.25.1