linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/migrate_device: fix folio refcount leak on folio_split_unmapped failure
@ 2026-03-04 12:01 Usama Arif
  2026-03-04 14:00 ` Kiryl Shutsemau
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Usama Arif @ 2026-03-04 12:01 UTC (permalink / raw)
  To: Andrew Morton, npache, david, ziy, linux-mm
  Cc: matthew.brost, joshua.hahnjy, hannes, rakie.kim, byungchul,
	gourry, ying.huang, apopple, riel, shakeel.butt, kas,
	linux-kernel, kernel-team, Usama Arif

From: Usama Arif <usama.arif@linux.dev>

migrate_vma_split_unmapped_folio() takes an extra reference via
folio_get() before calling folio_split_unmapped().  On success, the
split consumes this reference: __folio_freeze_and_split_unmapped()
expects the +1 in its folio_ref_freeze() check, and distributes it
across the resulting sub-folios via folio_ref_unfreeze(...+1), which
are later balanced by folio_put() calls in __migrate_device_finalize().

If folio_split_unmapped() fails (e.g., unexpected pinning returns
-EAGAIN), the function returns without calling folio_put().  The extra
reference is never released.

Add the missing folio_put() on the error path.

Fixes: 4265d67e405a4 ("mm/migrate_device: add THP splitting during migration")
Closes: https://lore.kernel.org/all/CAA1CXcDyqPPwf_-W7B+PFQtL8HdoJGCEqVsVxq7DhOUB=L4PQA@mail.gmail.com/
Reported-by: Nico Pache <npache@redhat.com>
Signed-off-by: Usama Arif <usama.arif@linux.dev>
---
 mm/migrate_device.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/mm/migrate_device.c b/mm/migrate_device.c
index 0a8b31939640f..351ecd9065d13 100644
--- a/mm/migrate_device.c
+++ b/mm/migrate_device.c
@@ -917,8 +917,10 @@ static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate,
 	folio_get(folio);
 	split_huge_pmd_address(migrate->vma, addr, true);
 	ret = folio_split_unmapped(folio, 0);
-	if (ret)
+	if (ret) {
+		folio_put(folio);
 		return ret;
+	}
 	migrate->src[idx] &= ~MIGRATE_PFN_COMPOUND;
 	flags = migrate->src[idx] & ((1UL << MIGRATE_PFN_SHIFT) - 1);
 	pfn = migrate->src[idx] >> MIGRATE_PFN_SHIFT;
-- 
2.47.3



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

* Re: [PATCH] mm/migrate_device: fix folio refcount leak on folio_split_unmapped failure
  2026-03-04 12:01 [PATCH] mm/migrate_device: fix folio refcount leak on folio_split_unmapped failure Usama Arif
@ 2026-03-04 14:00 ` Kiryl Shutsemau
  2026-03-04 15:17 ` Zi Yan
  2026-03-04 15:25 ` Joshua Hahn
  2 siblings, 0 replies; 4+ messages in thread
From: Kiryl Shutsemau @ 2026-03-04 14:00 UTC (permalink / raw)
  To: Usama Arif
  Cc: Andrew Morton, npache, david, ziy, linux-mm, matthew.brost,
	joshua.hahnjy, hannes, rakie.kim, byungchul, gourry, ying.huang,
	apopple, riel, shakeel.butt, linux-kernel, kernel-team,
	Usama Arif

On Wed, Mar 04, 2026 at 04:01:32AM -0800, Usama Arif wrote:
> From: Usama Arif <usama.arif@linux.dev>
> 
> migrate_vma_split_unmapped_folio() takes an extra reference via
> folio_get() before calling folio_split_unmapped().  On success, the
> split consumes this reference: __folio_freeze_and_split_unmapped()
> expects the +1 in its folio_ref_freeze() check, and distributes it
> across the resulting sub-folios via folio_ref_unfreeze(...+1), which
> are later balanced by folio_put() calls in __migrate_device_finalize().

Without this explanation folio_get() looks very random. And I still
can't say I understand reference management for the folios here.

Who takes reference for the folio if it !THP that gets return in the
_finalize()?

Can we get reference for THP and !THP at the same spot?

I think we should avoid spacial-casing THP where possible.

-- 
  Kiryl Shutsemau / Kirill A. Shutemov


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

* Re: [PATCH] mm/migrate_device: fix folio refcount leak on folio_split_unmapped failure
  2026-03-04 12:01 [PATCH] mm/migrate_device: fix folio refcount leak on folio_split_unmapped failure Usama Arif
  2026-03-04 14:00 ` Kiryl Shutsemau
@ 2026-03-04 15:17 ` Zi Yan
  2026-03-04 15:25 ` Joshua Hahn
  2 siblings, 0 replies; 4+ messages in thread
From: Zi Yan @ 2026-03-04 15:17 UTC (permalink / raw)
  To: Usama Arif, Balbir Singh
  Cc: Andrew Morton, npache, david, linux-mm, matthew.brost,
	joshua.hahnjy, hannes, rakie.kim, byungchul, gourry, ying.huang,
	apopple, riel, shakeel.butt, kas, linux-kernel, kernel-team,
	Usama Arif

On 4 Mar 2026, at 7:01, Usama Arif wrote:

> From: Usama Arif <usama.arif@linux.dev>
>
> migrate_vma_split_unmapped_folio() takes an extra reference via
> folio_get() before calling folio_split_unmapped().  On success, the
> split consumes this reference: __folio_freeze_and_split_unmapped()
> expects the +1 in its folio_ref_freeze() check, and distributes it
> across the resulting sub-folios via folio_ref_unfreeze(...+1), which
> are later balanced by folio_put() calls in __migrate_device_finalize().
>
> If folio_split_unmapped() fails (e.g., unexpected pinning returns
> -EAGAIN), the function returns without calling folio_put().  The extra
> reference is never released.
>
> Add the missing folio_put() on the error path.
>
> Fixes: 4265d67e405a4 ("mm/migrate_device: add THP splitting during migration")
> Closes: https://lore.kernel.org/all/CAA1CXcDyqPPwf_-W7B+PFQtL8HdoJGCEqVsVxq7DhOUB=L4PQA@mail.gmail.com/
> Reported-by: Nico Pache <npache@redhat.com>
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
>  mm/migrate_device.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/mm/migrate_device.c b/mm/migrate_device.c
> index 0a8b31939640f..351ecd9065d13 100644
> --- a/mm/migrate_device.c
> +++ b/mm/migrate_device.c
> @@ -917,8 +917,10 @@ static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate,
>  	folio_get(folio);
>  	split_huge_pmd_address(migrate->vma, addr, true);
>  	ret = folio_split_unmapped(folio, 0);
> -	if (ret)
> +	if (ret) {
> +		folio_put(folio);
>  		return ret;
> +	}
>  	migrate->src[idx] &= ~MIGRATE_PFN_COMPOUND;
>  	flags = migrate->src[idx] & ((1UL << MIGRATE_PFN_SHIFT) - 1);
>  	pfn = migrate->src[idx] >> MIGRATE_PFN_SHIFT;
> -- 
> 2.47.3

Add Balbir, who wrote the code, to comment on this.

Best Regards,
Yan, Zi


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

* Re: [PATCH] mm/migrate_device: fix folio refcount leak on folio_split_unmapped failure
  2026-03-04 12:01 [PATCH] mm/migrate_device: fix folio refcount leak on folio_split_unmapped failure Usama Arif
  2026-03-04 14:00 ` Kiryl Shutsemau
  2026-03-04 15:17 ` Zi Yan
@ 2026-03-04 15:25 ` Joshua Hahn
  2 siblings, 0 replies; 4+ messages in thread
From: Joshua Hahn @ 2026-03-04 15:25 UTC (permalink / raw)
  To: Usama Arif
  Cc: Andrew Morton, npache, david, ziy, linux-mm, matthew.brost,
	joshua.hahnjy, hannes, rakie.kim, byungchul, gourry, ying.huang,
	apopple, riel, shakeel.butt, kas, linux-kernel, kernel-team,
	Usama Arif

On Wed,  4 Mar 2026 04:01:32 -0800 Usama Arif <usamaarif642@gmail.com> wrote:

> From: Usama Arif <usama.arif@linux.dev>
> 
> migrate_vma_split_unmapped_folio() takes an extra reference via
> folio_get() before calling folio_split_unmapped().  On success, the
> split consumes this reference: __folio_freeze_and_split_unmapped()
> expects the +1 in its folio_ref_freeze() check, and distributes it
> across the resulting sub-folios via folio_ref_unfreeze(...+1), which
> are later balanced by folio_put() calls in __migrate_device_finalize().
> 
> If folio_split_unmapped() fails (e.g., unexpected pinning returns
> -EAGAIN), the function returns without calling folio_put().  The extra
> reference is never released.
> 
> Add the missing folio_put() on the error path.

Agreed with Kiryl that maybe there is an opportunity to do additional
cleanup. But as a fix for the issue, I think that this patch looks good
to me. We can send a follow-up patch in the future if we want to
clean this area up : -)

Reviewed-by: Joshua Hahn <joshua.hahnjy@gmail.com>

> Fixes: 4265d67e405a4 ("mm/migrate_device: add THP splitting during migration")
> Closes: https://lore.kernel.org/all/CAA1CXcDyqPPwf_-W7B+PFQtL8HdoJGCEqVsVxq7DhOUB=L4PQA@mail.gmail.com/
> Reported-by: Nico Pache <npache@redhat.com>
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
>  mm/migrate_device.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/mm/migrate_device.c b/mm/migrate_device.c
> index 0a8b31939640f..351ecd9065d13 100644
> --- a/mm/migrate_device.c
> +++ b/mm/migrate_device.c
> @@ -917,8 +917,10 @@ static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate,
>  	folio_get(folio);
>  	split_huge_pmd_address(migrate->vma, addr, true);
>  	ret = folio_split_unmapped(folio, 0);
> -	if (ret)
> +	if (ret) {
> +		folio_put(folio);
>  		return ret;
> +	}
>  	migrate->src[idx] &= ~MIGRATE_PFN_COMPOUND;
>  	flags = migrate->src[idx] & ((1UL << MIGRATE_PFN_SHIFT) - 1);
>  	pfn = migrate->src[idx] >> MIGRATE_PFN_SHIFT;
> -- 
> 2.47.3


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

end of thread, other threads:[~2026-03-04 15:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-04 12:01 [PATCH] mm/migrate_device: fix folio refcount leak on folio_split_unmapped failure Usama Arif
2026-03-04 14:00 ` Kiryl Shutsemau
2026-03-04 15:17 ` Zi Yan
2026-03-04 15:25 ` Joshua Hahn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox