From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Andrew Morton <akpm@linux-foundation.org>,
Zi Yan <ziy@nvidia.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
"Liam R. Howlett" <liam@infradead.org>,
Nico Pache <nico.pache@linux.dev>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>,
Lance Yang <lance.yang@linux.dev>,
Usama Arif <usama.arif@linux.dev>,
Kiryl Shutsemau <kas@kernel.org>, Qi Zheng <qi.zheng@linux.dev>,
Hugh Dickins <hughd@google.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org,
"David Hildenbrand (Arm)" <david@kernel.org>
Subject: [PATCH] mm/khugepaged: free unused page table when setting a huge PMD fails
Date: Thu, 05 Nov 2026 13:16:05 +0100 [thread overview]
Message-ID: <20261105-khugepaged_set_pmd-v1-1-cb79ab117199@kernel.org> (raw)
do_set_pmd() can allocate vmf.prealloc_pte on architectures that need a
deposited page table, such as PowerPC with the hash MMU. If the PMD is
no longer empty when it takes the PMD lock, it returns VM_FAULT_FALLBACK
without consuming that table.
Since collapse_pte_mapped_thp() uses mmap_read_lock(), a concurrent
userfaultfd operation can install a PTE table before taking the folio
lock. If that races with set_huge_pmd(), the unused preallocated table
is lost when its local vm_fault goes out of scope.
Free vmf.prealloc_pte on failure, as do_fault() already does. Successful
PMD installation consumes the table and clears the pointer.
Having to do this cleanup outside of do_set_pmd() is rather nasty, but
other callers depend on it already having been allocated. This needs a
cleanup at some point.
Fixes: 1043173eb5eb ("mm/khugepaged: collapse_pte_mapped_thp() with mmap_read_lock()")
Cc: stable@vger.kernel.org
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
This isn't super urgent, so it can wait for 7.5.
---
mm/khugepaged.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 8d9f05a39afb3..213c005fa8575 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1826,8 +1826,11 @@ static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a
}
vmf.pmd = pmdp;
- if (do_set_pmd(&vmf, folio, page))
+ if (do_set_pmd(&vmf, folio, page)) {
+ if (vmf.prealloc_pte)
+ pte_free(mm, vmf.prealloc_pte);
return SCAN_FAIL;
+ }
folio_get(folio);
return SCAN_SUCCEED;
---
base-commit: 02f72aa35c5e7dff443c62cd9964686396c8b201
change-id: 20261105-khugepaged_set_pmd-60a02f718e64
--
Cheers,
David
next reply other threads:[~2026-11-05 12:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 12:16 David Hildenbrand (Arm) [this message]
2026-11-05 13:02 ` Kiryl Shutsemau
2026-11-05 13:04 ` Kiryl Shutsemau
2026-11-05 16:40 ` David Hildenbrand (Arm)
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=20261105-khugepaged_set_pmd-v1-1-cb79ab117199@kernel.org \
--to=david@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=dev.jain@arm.com \
--cc=hughd@google.com \
--cc=kas@kernel.org \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nico.pache@linux.dev \
--cc=qi.zheng@linux.dev \
--cc=ryan.roberts@arm.com \
--cc=stable@vger.kernel.org \
--cc=usama.arif@linux.dev \
--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
all inboxes | Powered by JetHome®