diff mbox series

swapfile: fix uncorrect statistical count about free swap

Message ID 20221205024726.3111808-1-chenwandun@huawei.com (mailing list archive)
State New
Headers show
Series swapfile: fix uncorrect statistical count about free swap | expand

Commit Message

Chen Wandun Dec. 5, 2022, 2:47 a.m. UTC
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@huawei.com>
---
 mm/swapfile.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Hugh Dickins Dec. 5, 2022, 4:13 a.m. UTC | #1
On Mon, 5 Dec 2022, Chen Wandun wrote:

> 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@huawei.com>

No: it is intentional, and long established, that the count goes negative
during swapoff - that helps to inform you that it is in swapoff.

Hugh

> ---
>  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
> 
>
diff mbox series

Patch

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;