linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Dev Jain <dev.jain@arm.com>
To: "David Hildenbrand (Red Hat)" <david@kernel.org>,
	Shardul Bankar <shardul.b@mpiricsoftware.com>,
	shardulsb08@gmail.com
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	syzbot+a785d07959bc94837d51@syzkaller.appspotmail.com,
	akpm@linux-foundation.org, lorenzo.stoakes@oracle.com,
	ziy@nvidia.com, baolin.wang@linux.alibaba.com,
	Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com,
	baohua@kernel.org, lance.yang@linux.dev,
	janak@mpiricsoftware.com
Subject: Re: [PATCH] mm: khugepaged: fix memory leak in collapse_file rollback path
Date: Mon, 24 Nov 2025 17:16:56 +0530	[thread overview]
Message-ID: <9d4774ec-41d4-4c6f-899a-991187e58897@arm.com> (raw)
In-Reply-To: <36896775-5083-4b3e-9023-949de7722194@kernel.org>

[-- Attachment #1: Type: text/plain, Size: 2218 bytes --]


On 24/11/25 3:32 pm, David Hildenbrand (Red Hat) wrote:
> On 11/23/25 14:27, Shardul Bankar wrote:
>> When collapse_file() fails after xas_create_range() succeeds, the
>> rollback path does not clean up pre-allocated XArray nodes stored in
>> xas->xa_alloc. These nodes are allocated by xas_nomem() when
>> xas_create() fails with GFP_NOWAIT and need to be freed.
>>
>> The leak occurs because:
>> 1. xas_create_range() may call xas_nomem() which allocates a node
>>     and stores it in xas->xa_alloc
>
> Do you mean that, if xas_create_range() failed, collapse_file() will 
> call xas_nomem() to preallocate memory?
>
> I don't immediately see how xas_create_range() would call xas_nomem().
>
>> 2. If the collapse operation fails later, the rollback path jumps
>>     to the 'rollback:' label
>> 3. The rollback path cleans up folios but does not call xas_destroy()
>>     to free the pre-allocated nodes in xas->xa_alloc
>
> Note that after we call xas_nomem(), we retry xas_create_range() -- 
> that previously failed to to -ENOMEM.
>
> So the assumption is that the xas_create_range() call would consume 
> that memory.
>
> I'm sure there is some corner case where it is not the case (some 
> concurrent action? not sure)

Someone else can put slots in the xarray since we dropped the lock. I 
cannot prove this, but surely

disproving this is harder : )


>
>>
>> Fix this by calling xas_destroy(&xas) at the beginning of the rollback
>> path to free any pre-allocated nodes. This is safe because xas_destroy()
>> only frees nodes in xas->xa_alloc that were never inserted into the
>> XArray tree.
>
> Shouldn't we just call xas_destroy() in any case, also when everything 
> succeeded?

Yeah you are right. We should probably do

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index abe54f0043c7..0794a99c807f100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1872,11+1872,14@@ staticintcollapse_file(structmm_struct *mm, 
unsignedlongaddr,
do{
xas_lock_irq(&xas);
xas_create_range(&xas);
- if(!xas_error(&xas))
+ if(!xas_error(&xas)) {
+ xas_destroy(&xas);
break;
+ }
xas_unlock_irq(&xas);
if(!xas_nomem(&xas, GFP_KERNEL)) {
result = SCAN_FAIL;
+ xas_destroy(&xas);
gotorollback;
}
} while(1);


[-- Attachment #2: Type: text/html, Size: 7279 bytes --]

  reply	other threads:[~2025-11-24 11:47 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-23 13:27 Shardul Bankar
2025-11-23 14:49 ` Dev Jain
2025-11-24 10:02 ` David Hildenbrand (Red Hat)
2025-11-24 11:46   ` Dev Jain [this message]
2025-11-24 15:23     ` Shardul Bankar
2025-11-24 16:11       ` [PATCH v2] mm: khugepaged: fix memory leak in collapse_file xas retry loop Shardul Bankar
2025-11-24 16:21         ` Matthew Wilcox
2025-11-24 17:37           ` Shardul Bankar
2025-12-01  7:45             ` [PATCH v3] lib: xarray: free unused spare node in xas_create_range() Shardul Bankar
2025-12-01  8:39               ` David Hildenbrand (Red Hat)
2025-12-04 14:15                 ` Shardul Bankar
2025-12-04 21:15                   ` David Hildenbrand (Red Hat)

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=9d4774ec-41d4-4c6f-899a-991187e58897@arm.com \
    --to=dev.jain@arm.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=janak@mpiricsoftware.com \
    --cc=lance.yang@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=npache@redhat.com \
    --cc=ryan.roberts@arm.com \
    --cc=shardul.b@mpiricsoftware.com \
    --cc=shardulsb08@gmail.com \
    --cc=syzbot+a785d07959bc94837d51@syzkaller.appspotmail.com \
    --cc=ziy@nvidia.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