linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vlastimil Babka <vbabka@suse.cz>
To: Zhaoyang Huang <huangzhaoyang@gmail.com>,
	Zhaoyang Huang <zhaoyang.huang@unisoc.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] mm: fix a race on nr_swap_pages
Date: Fri, 4 Dec 2020 17:19:35 +0100	[thread overview]
Message-ID: <56407d04-2178-8a0e-ae1b-a1a4b9d7912a@suse.cz> (raw)
In-Reply-To: <1607050340-4535-1-git-send-email-zhaoyang.huang@unisoc.com>

On 12/4/20 3:52 AM, Zhaoyang Huang wrote:
> The scenario on which "Free swap = -4kB" happens in my system, which is caused
> by several get_swap_pages racing with each other and show_swap_cache_info
> happens simutaniously. No need to add a lock on get_swap_page_of_type as we
> remove "Presub/PosAdd" here.
> 
> ProcessA			ProcessB			ProcessC
> ngoals = 1			ngoals = 1
> avail = nr_swap_pages(1)	avail = nr_swap_pages(1)
> nr_swap_pages(1) -= ngoals
> 				nr_swap_pages(0) -= ngoals
> 								nr_swap_pages = -1
> 
> Signed-off-by: Zhaoyang Huang <zhaoyang.huang@unisoc.com>

Better now.
Acked-by: Vlastimil Babka <vbabka@suse.cz>

> ---
> change of v2: fix bug of unpaired of spin_lock
> ---
> ---
>  mm/swapfile.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index cf63b5f..1212f17 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -974,9 +974,13 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size)
>  	/* Only single cluster request supported */
>  	WARN_ON_ONCE(n_goal > 1 && size == SWAPFILE_CLUSTER);
>  
> +	spin_lock(&swap_avail_lock);
> +
>  	avail_pgs = atomic_long_read(&nr_swap_pages) / size;
> -	if (avail_pgs <= 0)
> +	if (avail_pgs <= 0) {
> +		spin_unlock(&swap_avail_lock);
>  		goto noswap;
> +	}
>  
>  	if (n_goal > SWAP_BATCH)
>  		n_goal = SWAP_BATCH;
> @@ -986,8 +990,6 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size)
>  
>  	atomic_long_sub(n_goal * size, &nr_swap_pages);
>  
> -	spin_lock(&swap_avail_lock);
> -
>  start_over:
>  	node = numa_node_id();
>  	plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) {
> @@ -1061,14 +1063,13 @@ swp_entry_t get_swap_page_of_type(int type)
>  
>  	spin_lock(&si->lock);
>  	if (si->flags & SWP_WRITEOK) {
> -		atomic_long_dec(&nr_swap_pages);
>  		/* This is called for allocating swap entry, not cache */
>  		offset = scan_swap_map(si, 1);
>  		if (offset) {
> +			atomic_long_dec(&nr_swap_pages);
>  			spin_unlock(&si->lock);
>  			return swp_entry(type, offset);
>  		}
> -		atomic_long_inc(&nr_swap_pages);
>  	}
>  	spin_unlock(&si->lock);
>  fail:
> 


      reply	other threads:[~2020-12-04 16:20 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-04  2:52 [PATCH v2] mm: fix a race on nr_swap_pages Zhaoyang Huang
2020-12-04 16:19 ` Vlastimil Babka [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56407d04-2178-8a0e-ae1b-a1a4b9d7912a@suse.cz \
    --to=vbabka@suse.cz \
    --cc=huangzhaoyang@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=zhaoyang.huang@unisoc.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).