All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/shmem: don't allocate the replacement folio with the folio locked
@ 2026-12-01 22:13 Jose A. Perez de Azpillaga
  2026-12-02  7:45 ` Baolin Wang
  0 siblings, 1 reply; 4+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-12-01 22:13 UTC (permalink / raw)
  To: Hugh Dickins, David Hildenbrand, Andrew Morton
  Cc: Baolin Wang, linux-mm, Jose A. Perez de Azpillaga

shmem_swapin_folio() allocates the replacement for a swapcache folio in
the wrong zone while holding the folio lock, usually with __GFP_FS.

The zone check doesn't need the lock, so allocate the replacement before
locking the folio. The folio can't be split in the meantime, since we
hold a reference to it.

Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
Link: https://lore.kernel.org/c45c8b8c-0ccc-4e0e-898f-039f84fe1e56@kernel.org
Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
---
Tested on x86_64 in QEMU.

 mm/shmem.c | 46 +++++++++++++++++++++++++++-------------------
 1 file changed, 27 insertions(+), 19 deletions(-)

diff --git a/mm/shmem.c b/mm/shmem.c
index e06dadd2be2c..3ffd54e4bfb4 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2369,32 +2369,32 @@ static bool shmem_should_replace_folio(struct folio *folio, gfp_t gfp)
 	return folio_zonenum(folio) > gfp_zone(gfp);
 }
 
-static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,
-				struct shmem_inode_info *info, pgoff_t index,
-				struct vm_area_struct *vma)
+static struct folio *shmem_alloc_replacement(struct folio *old, gfp_t gfp,
+		struct shmem_inode_info *info, pgoff_t index,
+		struct vm_area_struct *vma)
 {
-	struct swap_cluster_info *ci;
-	struct folio *new, *old = *foliop;
-	swp_entry_t entry = old->swap;
-	int nr_pages = folio_nr_pages(old);
-	int error = 0;
-
 	/*
 	 * We have arrived here because our zones are constrained, so don't
 	 * limit chance of success by further cpuset and node constraints.
 	 */
 	gfp &= ~GFP_CONSTRAINT_MASK;
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
-	if (nr_pages > 1) {
+	if (folio_test_large(old)) {
 		gfp_t huge_gfp = vma_thp_gfp_mask(vma);
 
 		gfp = thp_shmem_limit_gfp_mask(huge_gfp, gfp);
 	}
 #endif
 
-	new = shmem_alloc_folio(gfp, folio_order(old), info, index);
-	if (!new)
-		return -ENOMEM;
+	return shmem_alloc_folio(gfp, folio_order(old), info, index);
+}
+
+static void shmem_replace_folio(struct folio **foliop, struct folio *new)
+{
+	struct swap_cluster_info *ci;
+	struct folio *old = *foliop;
+	swp_entry_t entry = old->swap;
+	int nr_pages = folio_nr_pages(old);
 
 	folio_ref_add(new, nr_pages);
 	folio_copy(new, old);
@@ -2426,7 +2426,6 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,
 	 * cache.
 	 */
 	folio_put_refs(old, nr_pages + 1);
-	return error;
 }
 
 static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index,
@@ -2545,7 +2544,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
 	swp_entry_t swap;
 	softleaf_t index_entry;
 	struct swap_info_struct *si;
-	struct folio *folio = NULL;
+	struct folio *folio = NULL, *new = NULL;
 	int error, nr_pages, order;
 	pgoff_t offset;
 
@@ -2636,6 +2635,14 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
 		index = round_down(index, nr_pages);
 	}
 
+	if (shmem_should_replace_folio(folio, gfp)) {
+		new = shmem_alloc_replacement(folio, gfp, info, index, vma);
+		if (!new) {
+			error = -ENOMEM;
+			goto failed_nolock;
+		}
+	}
+
 	/*
 	 * We have to do this with the folio locked to prevent races.
 	 * The shmem_confirm_swap below only checks if the first swap
@@ -2664,10 +2671,9 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
 	 */
 	arch_swap_restore(folio_swap(swap, folio), folio);
 
-	if (shmem_should_replace_folio(folio, gfp)) {
-		error = shmem_replace_folio(&folio, gfp, info, index, vma);
-		if (error)
-			goto failed;
+	if (new) {
+		shmem_replace_folio(&folio, new);
+		new = NULL;
 	}
 
 	error = shmem_add_to_page_cache(folio, mapping, index,
@@ -2707,6 +2713,8 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
 	if (folio)
 		folio_unlock(folio);
 failed_nolock:
+	if (new)
+		folio_put(new);
 	if (folio)
 		folio_put(folio);
 	put_swap_device(si);

base-commit: 996888d33a3e7fb548a0d85e85b4a5cfed0c8ec9
-- 
2.56.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/shmem: don't allocate the replacement folio with the folio locked
  2026-12-01 22:13 [PATCH] mm/shmem: don't allocate the replacement folio with the folio locked Jose A. Perez de Azpillaga
@ 2026-12-02  7:45 ` Baolin Wang
  2026-12-02  8:05   ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 4+ messages in thread
From: Baolin Wang @ 2026-12-02  7:45 UTC (permalink / raw)
  To: Jose A. Perez de Azpillaga, Hugh Dickins, David Hildenbrand,
	Andrew Morton
  Cc: linux-mm



On 12/2/26 6:13 AM, Jose A. Perez de Azpillaga wrote:
> shmem_swapin_folio() allocates the replacement for a swapcache folio in
> the wrong zone while holding the folio lock, usually with __GFP_FS.
> 
> The zone check doesn't need the lock, so allocate the replacement before
> locking the folio. The folio can't be split in the meantime, since we
> hold a reference to it.
> 
> Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
> Link: https://lore.kernel.org/c45c8b8c-0ccc-4e0e-898f-039f84fe1e56@kernel.org
> Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
> ---
> Tested on x86_64 in QEMU.
> 
>   mm/shmem.c | 46 +++++++++++++++++++++++++++-------------------
>   1 file changed, 27 insertions(+), 19 deletions(-)
> 
> diff --git a/mm/shmem.c b/mm/shmem.c
> index e06dadd2be2c..3ffd54e4bfb4 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -2369,32 +2369,32 @@ static bool shmem_should_replace_folio(struct folio *folio, gfp_t gfp)
>   	return folio_zonenum(folio) > gfp_zone(gfp);
>   }
>   
> -static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,
> -				struct shmem_inode_info *info, pgoff_t index,
> -				struct vm_area_struct *vma)
> +static struct folio *shmem_alloc_replacement(struct folio *old, gfp_t gfp,
> +		struct shmem_inode_info *info, pgoff_t index,
> +		struct vm_area_struct *vma)
>   {
> -	struct swap_cluster_info *ci;
> -	struct folio *new, *old = *foliop;
> -	swp_entry_t entry = old->swap;
> -	int nr_pages = folio_nr_pages(old);
> -	int error = 0;
> -
>   	/*
>   	 * We have arrived here because our zones are constrained, so don't
>   	 * limit chance of success by further cpuset and node constraints.
>   	 */
>   	gfp &= ~GFP_CONSTRAINT_MASK;
>   #ifdef CONFIG_TRANSPARENT_HUGEPAGE
> -	if (nr_pages > 1) {
> +	if (folio_test_large(old)) {
>   		gfp_t huge_gfp = vma_thp_gfp_mask(vma);
>   
>   		gfp = thp_shmem_limit_gfp_mask(huge_gfp, gfp);
>   	}
>   #endif
>   
> -	new = shmem_alloc_folio(gfp, folio_order(old), info, index);
> -	if (!new)
> -		return -ENOMEM;
> +	return shmem_alloc_folio(gfp, folio_order(old), info, index);
> +}
> +
> +static void shmem_replace_folio(struct folio **foliop, struct folio *new)
> +{
> +	struct swap_cluster_info *ci;
> +	struct folio *old = *foliop;
> +	swp_entry_t entry = old->swap;
> +	int nr_pages = folio_nr_pages(old);
>   
>   	folio_ref_add(new, nr_pages);
>   	folio_copy(new, old);
> @@ -2426,7 +2426,6 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,
>   	 * cache.
>   	 */
>   	folio_put_refs(old, nr_pages + 1);
> -	return error;
>   }
>   
>   static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index,
> @@ -2545,7 +2544,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
>   	swp_entry_t swap;
>   	softleaf_t index_entry;
>   	struct swap_info_struct *si;
> -	struct folio *folio = NULL;
> +	struct folio *folio = NULL, *new = NULL;
>   	int error, nr_pages, order;
>   	pgoff_t offset;
>   
> @@ -2636,6 +2635,14 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
>   		index = round_down(index, nr_pages);
>   	}
>   
> +	if (shmem_should_replace_folio(folio, gfp)) {
> +		new = shmem_alloc_replacement(folio, gfp, info, index, vma);
> +		if (!new) {
> +			error = -ENOMEM;
> +			goto failed_nolock;
> +		}
> +	}

Why not just drop the __GFP_FS flag in shmem_replace_folio(), like the 
fixes in your previous patchset? I think we should still check the swap 
status and the folio state first to decide whether a retry is needed.

         folio_lock(folio);
         if (!folio_matches_swap_entry(folio, swap) ||
             shmem_confirm_swap(mapping, index, swap) < 0) {
                 error = -EEXIST;
                 goto unlock;
         }
         if (!folio_test_uptodate(folio)) {
                 error = -EIO;
                 goto failed;
         }

>   	/*
>   	 * We have to do this with the folio locked to prevent races.
>   	 * The shmem_confirm_swap below only checks if the first swap
> @@ -2664,10 +2671,9 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
>   	 */
>   	arch_swap_restore(folio_swap(swap, folio), folio);
>   
> -	if (shmem_should_replace_folio(folio, gfp)) {
> -		error = shmem_replace_folio(&folio, gfp, info, index, vma);
> -		if (error)
> -			goto failed;
> +	if (new) {
> +		shmem_replace_folio(&folio, new);
> +		new = NULL;
>   	}
>   
>   	error = shmem_add_to_page_cache(folio, mapping, index,
> @@ -2707,6 +2713,8 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
>   	if (folio)
>   		folio_unlock(folio);
>   failed_nolock:
> +	if (new)
> +		folio_put(new);
>   	if (folio)
>   		folio_put(folio);
>   	put_swap_device(si);
> 
> base-commit: 996888d33a3e7fb548a0d85e85b4a5cfed0c8ec9



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/shmem: don't allocate the replacement folio with the folio locked
  2026-12-02  7:45 ` Baolin Wang
@ 2026-12-02  8:05   ` David Hildenbrand (Arm)
  2026-12-02  8:14     ` Baolin Wang
  0 siblings, 1 reply; 4+ messages in thread
From: David Hildenbrand (Arm) @ 2026-12-02  8:05 UTC (permalink / raw)
  To: Baolin Wang, Jose A. Perez de Azpillaga, Hugh Dickins,
	Andrew Morton
  Cc: linux-mm

On 12/2/26 08:45, Baolin Wang wrote:
> 
> 
> On 12/2/26 6:13 AM, Jose A. Perez de Azpillaga wrote:
>> shmem_swapin_folio() allocates the replacement for a swapcache folio in
>> the wrong zone while holding the folio lock, usually with __GFP_FS.
>>
>> The zone check doesn't need the lock, so allocate the replacement before
>> locking the folio. The folio can't be split in the meantime, since we
>> hold a reference to it.
>>
>> Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
>> Link: https://lore.kernel.org/c45c8b8c-0ccc-4e0e-898f-039f84fe1e56@kernel.org
>> Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
>> ---
>> Tested on x86_64 in QEMU.
>>
>>   mm/shmem.c | 46 +++++++++++++++++++++++++++-------------------
>>   1 file changed, 27 insertions(+), 19 deletions(-)
>>
>> diff --git a/mm/shmem.c b/mm/shmem.c
>> index e06dadd2be2c..3ffd54e4bfb4 100644
>> --- a/mm/shmem.c
>> +++ b/mm/shmem.c
>> @@ -2369,32 +2369,32 @@ static bool shmem_should_replace_folio(struct folio *folio, gfp_t gfp)
>>   	return folio_zonenum(folio) > gfp_zone(gfp);
>>   }
>>   
>> -static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,
>> -				struct shmem_inode_info *info, pgoff_t index,
>> -				struct vm_area_struct *vma)
>> +static struct folio *shmem_alloc_replacement(struct folio *old, gfp_t gfp,
>> +		struct shmem_inode_info *info, pgoff_t index,
>> +		struct vm_area_struct *vma)
>>   {
>> -	struct swap_cluster_info *ci;
>> -	struct folio *new, *old = *foliop;
>> -	swp_entry_t entry = old->swap;
>> -	int nr_pages = folio_nr_pages(old);
>> -	int error = 0;
>> -
>>   	/*
>>   	 * We have arrived here because our zones are constrained, so don't
>>   	 * limit chance of success by further cpuset and node constraints.
>>   	 */
>>   	gfp &= ~GFP_CONSTRAINT_MASK;
>>   #ifdef CONFIG_TRANSPARENT_HUGEPAGE
>> -	if (nr_pages > 1) {
>> +	if (folio_test_large(old)) {
>>   		gfp_t huge_gfp = vma_thp_gfp_mask(vma);
>>   
>>   		gfp = thp_shmem_limit_gfp_mask(huge_gfp, gfp);
>>   	}
>>   #endif
>>   
>> -	new = shmem_alloc_folio(gfp, folio_order(old), info, index);
>> -	if (!new)
>> -		return -ENOMEM;
>> +	return shmem_alloc_folio(gfp, folio_order(old), info, index);
>> +}
>> +
>> +static void shmem_replace_folio(struct folio **foliop, struct folio *new)
>> +{
>> +	struct swap_cluster_info *ci;
>> +	struct folio *old = *foliop;
>> +	swp_entry_t entry = old->swap;
>> +	int nr_pages = folio_nr_pages(old);
>>   
>>   	folio_ref_add(new, nr_pages);
>>   	folio_copy(new, old);
>> @@ -2426,7 +2426,6 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,
>>   	 * cache.
>>   	 */
>>   	folio_put_refs(old, nr_pages + 1);
>> -	return error;
>>   }
>>   
>>   static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index,
>> @@ -2545,7 +2544,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
>>   	swp_entry_t swap;
>>   	softleaf_t index_entry;
>>   	struct swap_info_struct *si;
>> -	struct folio *folio = NULL;
>> +	struct folio *folio = NULL, *new = NULL;
>>   	int error, nr_pages, order;
>>   	pgoff_t offset;
>>   
>> @@ -2636,6 +2635,14 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
>>   		index = round_down(index, nr_pages);
>>   	}
>>   
>> +	if (shmem_should_replace_folio(folio, gfp)) {
>> +		new = shmem_alloc_replacement(folio, gfp, info, index, vma);
>> +		if (!new) {
>> +			error = -ENOMEM;
>> +			goto failed_nolock;
>> +		}
>> +	}
> 
> Why not just drop the __GFP_FS flag in shmem_replace_folio(), like the 
> fixes in your previous patchset? 

I think we should try to avoid allocating under folio lock wherever we easily
can. It just screams for bugs :)

-- 
Cheers,

David


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/shmem: don't allocate the replacement folio with the folio locked
  2026-12-02  8:05   ` David Hildenbrand (Arm)
@ 2026-12-02  8:14     ` Baolin Wang
  0 siblings, 0 replies; 4+ messages in thread
From: Baolin Wang @ 2026-12-02  8:14 UTC (permalink / raw)
  To: David Hildenbrand (Arm), Jose A. Perez de Azpillaga, Hugh Dickins,
	Andrew Morton
  Cc: linux-mm



On 12/2/26 4:05 PM, David Hildenbrand (Arm) wrote:
> On 12/2/26 08:45, Baolin Wang wrote:
>>
>>
>> On 12/2/26 6:13 AM, Jose A. Perez de Azpillaga wrote:
>>> shmem_swapin_folio() allocates the replacement for a swapcache folio in
>>> the wrong zone while holding the folio lock, usually with __GFP_FS.
>>>
>>> The zone check doesn't need the lock, so allocate the replacement before
>>> locking the folio. The folio can't be split in the meantime, since we
>>> hold a reference to it.
>>>
>>> Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
>>> Link: https://lore.kernel.org/c45c8b8c-0ccc-4e0e-898f-039f84fe1e56@kernel.org
>>> Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
>>> ---
>>> Tested on x86_64 in QEMU.
>>>
>>>    mm/shmem.c | 46 +++++++++++++++++++++++++++-------------------
>>>    1 file changed, 27 insertions(+), 19 deletions(-)
>>>
>>> diff --git a/mm/shmem.c b/mm/shmem.c
>>> index e06dadd2be2c..3ffd54e4bfb4 100644
>>> --- a/mm/shmem.c
>>> +++ b/mm/shmem.c
>>> @@ -2369,32 +2369,32 @@ static bool shmem_should_replace_folio(struct folio *folio, gfp_t gfp)
>>>    	return folio_zonenum(folio) > gfp_zone(gfp);
>>>    }
>>>    
>>> -static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,
>>> -				struct shmem_inode_info *info, pgoff_t index,
>>> -				struct vm_area_struct *vma)
>>> +static struct folio *shmem_alloc_replacement(struct folio *old, gfp_t gfp,
>>> +		struct shmem_inode_info *info, pgoff_t index,
>>> +		struct vm_area_struct *vma)
>>>    {
>>> -	struct swap_cluster_info *ci;
>>> -	struct folio *new, *old = *foliop;
>>> -	swp_entry_t entry = old->swap;
>>> -	int nr_pages = folio_nr_pages(old);
>>> -	int error = 0;
>>> -
>>>    	/*
>>>    	 * We have arrived here because our zones are constrained, so don't
>>>    	 * limit chance of success by further cpuset and node constraints.
>>>    	 */
>>>    	gfp &= ~GFP_CONSTRAINT_MASK;
>>>    #ifdef CONFIG_TRANSPARENT_HUGEPAGE
>>> -	if (nr_pages > 1) {
>>> +	if (folio_test_large(old)) {
>>>    		gfp_t huge_gfp = vma_thp_gfp_mask(vma);
>>>    
>>>    		gfp = thp_shmem_limit_gfp_mask(huge_gfp, gfp);
>>>    	}
>>>    #endif
>>>    
>>> -	new = shmem_alloc_folio(gfp, folio_order(old), info, index);
>>> -	if (!new)
>>> -		return -ENOMEM;
>>> +	return shmem_alloc_folio(gfp, folio_order(old), info, index);
>>> +}
>>> +
>>> +static void shmem_replace_folio(struct folio **foliop, struct folio *new)
>>> +{
>>> +	struct swap_cluster_info *ci;
>>> +	struct folio *old = *foliop;
>>> +	swp_entry_t entry = old->swap;
>>> +	int nr_pages = folio_nr_pages(old);
>>>    
>>>    	folio_ref_add(new, nr_pages);
>>>    	folio_copy(new, old);
>>> @@ -2426,7 +2426,6 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,
>>>    	 * cache.
>>>    	 */
>>>    	folio_put_refs(old, nr_pages + 1);
>>> -	return error;
>>>    }
>>>    
>>>    static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index,
>>> @@ -2545,7 +2544,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
>>>    	swp_entry_t swap;
>>>    	softleaf_t index_entry;
>>>    	struct swap_info_struct *si;
>>> -	struct folio *folio = NULL;
>>> +	struct folio *folio = NULL, *new = NULL;
>>>    	int error, nr_pages, order;
>>>    	pgoff_t offset;
>>>    
>>> @@ -2636,6 +2635,14 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
>>>    		index = round_down(index, nr_pages);
>>>    	}
>>>    
>>> +	if (shmem_should_replace_folio(folio, gfp)) {
>>> +		new = shmem_alloc_replacement(folio, gfp, info, index, vma);
>>> +		if (!new) {
>>> +			error = -ENOMEM;
>>> +			goto failed_nolock;
>>> +		}
>>> +	}
>>
>> Why not just drop the __GFP_FS flag in shmem_replace_folio(), like the
>> fixes in your previous patchset?
> 
> I think we should try to avoid allocating under folio lock wherever we easily
> can. It just screams for bugs :)

OK. But I still feel uncomfortable about skipping the swap entry check. 
We could still defer returning -ENOMEM in shmem_replace_folio() if the 
pre-allocated replacement folio fails.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-02  8:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 22:13 [PATCH] mm/shmem: don't allocate the replacement folio with the folio locked Jose A. Perez de Azpillaga
2026-12-02  7:45 ` Baolin Wang
2026-12-02  8:05   ` David Hildenbrand (Arm)
2026-12-02  8:14     ` Baolin Wang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.