From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 904982D7805 for ; Sun, 23 Nov 2025 14:50:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763909403; cv=none; b=UPUJraDvis5WmusCWI9ldVyaKMAOZly6vTVmOTrJIGKu637UCEQcBVpEaNO1j8P4pLCCmQn2QIjZSedVXWup79u7GPvdUO5YY3kmBYKea77/GE+HkxH0ykoj4PamOQvsR/z+NjqruyY1L3xvvJUuTr5DK+BjUWGyHVTOj+Zum3g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763909403; c=relaxed/simple; bh=CdaLA0HIM0qn12DDthZ/+KXN+YDl1tgDZQKgW03YNuM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=flU5XtISv4uBUgoQIPqVNh2ZwZonv06iNsxHyWj2PKD9b3Oac5/B74VLRtBEWVfJWtP5f8M6Azp65YrYJ8iAP+tbm4VWgHKWhQgx1MlpGCx88dmZLqN8Oj9S2p+zSlK+GfVKDIqC+hMcVSidOFfDvdVXO8v0NPK7EBtBJ/tWOug= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 57CF2339; Sun, 23 Nov 2025 06:49:45 -0800 (PST) Received: from [10.164.10.248] (MacBook-Pro.blr.arm.com [10.164.10.248]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 36FA13F6A8; Sun, 23 Nov 2025 06:49:47 -0800 (PST) Message-ID: <57cbf887-d181-418b-a6c7-9f3eff5d632a@arm.com> Date: Sun, 23 Nov 2025 20:19:45 +0530 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] mm: khugepaged: fix memory leak in collapse_file rollback path To: Shardul Bankar , shardulsb08@gmail.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, syzbot+a785d07959bc94837d51@syzkaller.appspotmail.com, akpm@linux-foundation.org, david@kernel.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 References: <20251123132727.3262731-1-shardul.b@mpiricsoftware.com> Content-Language: en-US From: Dev Jain In-Reply-To: <20251123132727.3262731-1-shardul.b@mpiricsoftware.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 23/11/25 6:57 pm, 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 > 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 > > 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. > > Note: This fixes the leak of pre-allocated nodes. A separate fix will > be needed to clean up empty nodes that were inserted into the tree by > xas_create_range() but never populated. No "fix" is needed in this case, the empty nodes are there in the tree and there is no leak. > > Link: https://syzkaller.appspot.com/bug?id=a274d65fc733448ed518ad15481ed575669dd98c > Fixes: cae106dd67b9 ("mm/khugepaged: refactor collapse_file control flow") > Signed-off-by: Shardul Bankar > --- > mm/khugepaged.c | 1 + > 1 file changed, 1 insertion(+) > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c > index abe54f0043c7..f2fe7924afa0 100644 > --- a/mm/khugepaged.c > +++ b/mm/khugepaged.c > @@ -2230,6 +2230,7 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, > goto out; > > rollback: > + xas_destroy(&xas); > /* Something went wrong: roll back page cache changes */ > if (nr_none) { > xas_lock_irq(&xas); I believe that the correct explanation of this patch is - after xas_nomem, if someone else expands the tree and the retry for xas_create_range() trivially succeeds without using the node allocated from xas_nomem, then we need to free that node. Although now I am worried about other users of xas_nomem() which do not call xas_destroy() on failure path... I think this should work, Reviewed-by: Dev Jain