diff mbox series

[5/5] mm/swap_state: fix swap_cluster_readahead() race with swapoff

Message ID 20210408130820.48233-6-linmiaohe@huawei.com (mailing list archive)
State New, archived
Headers show
Series close various race windows for swap | expand

Commit Message

Miaohe Lin April 8, 2021, 1:08 p.m. UTC
swap_cluster_readahead() could race with swapoff and might dereference
si->swap_file after it's released by swapoff. Close this race window by
using get/put_swap_device() pair.

Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
---
 mm/swap_state.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Comments

Huang, Ying April 13, 2021, 1:36 a.m. UTC | #1
Miaohe Lin <linmiaohe@huawei.com> writes:

> swap_cluster_readahead() could race with swapoff and might dereference
> si->swap_file after it's released by swapoff. Close this race window by
> using get/put_swap_device() pair.

I think we should fix the callers instead to reduce the overhead.  Now,
do_swap_page() has been fixed.  We need to fix shmem_swapin().

Best Regards,
Huang, Ying

> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> ---
>  mm/swap_state.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/mm/swap_state.c b/mm/swap_state.c
> index 3bf0d0c297bc..eba6b0cf6cf9 100644
> --- a/mm/swap_state.c
> +++ b/mm/swap_state.c
> @@ -626,12 +626,17 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
>  	unsigned long offset = entry_offset;
>  	unsigned long start_offset, end_offset;
>  	unsigned long mask;
> -	struct swap_info_struct *si = swp_swap_info(entry);
> +	struct swap_info_struct *si;
>  	struct blk_plug plug;
>  	bool do_poll = true, page_allocated;
>  	struct vm_area_struct *vma = vmf->vma;
>  	unsigned long addr = vmf->address;
>  
> +	si = get_swap_device(entry);
> +	/* In case we raced with swapoff. */
> +	if (!si)
> +		return NULL;
> +
>  	mask = swapin_nr_pages(offset) - 1;
>  	if (!mask)
>  		goto skip;
> @@ -673,7 +678,9 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
>  
>  	lru_add_drain();	/* Push any new pages onto the LRU now */
>  skip:
> -	return read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll);
> +	page = read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll);
> +	put_swap_device(si);
> +	return page;
>  }
>  
>  int init_swap_address_space(unsigned int type, unsigned long nr_pages)
Miaohe Lin April 14, 2021, 2:43 a.m. UTC | #2
On 2021/4/13 9:36, Huang, Ying wrote:
> Miaohe Lin <linmiaohe@huawei.com> writes:
> 
>> swap_cluster_readahead() could race with swapoff and might dereference
>> si->swap_file after it's released by swapoff. Close this race window by
>> using get/put_swap_device() pair.
> 
> I think we should fix the callers instead to reduce the overhead.  Now,
> do_swap_page() has been fixed.  We need to fix shmem_swapin().
> 

Will do. Many thanks.

> Best Regards,
> Huang, Ying
> 
>> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
>> ---
>>  mm/swap_state.c | 11 +++++++++--
>>  1 file changed, 9 insertions(+), 2 deletions(-)
>>
>> diff --git a/mm/swap_state.c b/mm/swap_state.c
>> index 3bf0d0c297bc..eba6b0cf6cf9 100644
>> --- a/mm/swap_state.c
>> +++ b/mm/swap_state.c
>> @@ -626,12 +626,17 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
>>  	unsigned long offset = entry_offset;
>>  	unsigned long start_offset, end_offset;
>>  	unsigned long mask;
>> -	struct swap_info_struct *si = swp_swap_info(entry);
>> +	struct swap_info_struct *si;
>>  	struct blk_plug plug;
>>  	bool do_poll = true, page_allocated;
>>  	struct vm_area_struct *vma = vmf->vma;
>>  	unsigned long addr = vmf->address;
>>  
>> +	si = get_swap_device(entry);
>> +	/* In case we raced with swapoff. */
>> +	if (!si)
>> +		return NULL;
>> +
>>  	mask = swapin_nr_pages(offset) - 1;
>>  	if (!mask)
>>  		goto skip;
>> @@ -673,7 +678,9 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
>>  
>>  	lru_add_drain();	/* Push any new pages onto the LRU now */
>>  skip:
>> -	return read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll);
>> +	page = read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll);
>> +	put_swap_device(si);
>> +	return page;
>>  }
>>  
>>  int init_swap_address_space(unsigned int type, unsigned long nr_pages)
> .
>
diff mbox series

Patch

diff --git a/mm/swap_state.c b/mm/swap_state.c
index 3bf0d0c297bc..eba6b0cf6cf9 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -626,12 +626,17 @@  struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	unsigned long offset = entry_offset;
 	unsigned long start_offset, end_offset;
 	unsigned long mask;
-	struct swap_info_struct *si = swp_swap_info(entry);
+	struct swap_info_struct *si;
 	struct blk_plug plug;
 	bool do_poll = true, page_allocated;
 	struct vm_area_struct *vma = vmf->vma;
 	unsigned long addr = vmf->address;
 
+	si = get_swap_device(entry);
+	/* In case we raced with swapoff. */
+	if (!si)
+		return NULL;
+
 	mask = swapin_nr_pages(offset) - 1;
 	if (!mask)
 		goto skip;
@@ -673,7 +678,9 @@  struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
 
 	lru_add_drain();	/* Push any new pages onto the LRU now */
 skip:
-	return read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll);
+	page = read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll);
+	put_swap_device(si);
+	return page;
 }
 
 int init_swap_address_space(unsigned int type, unsigned long nr_pages)