From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Hugh Dickins <hughd@google.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Andrew Morton <akpm@linux-foundation.org>,
Mike Rapoport <rppt@kernel.org>, Peter Xu <peterx@redhat.com>,
Michal Hocko <mhocko@suse.com>, Kiryl Shutsemau <kas@kernel.org>,
Manfred Spraul <manfred@colorfullife.com>,
Davidlohr Bueso <dave@stgolabs.net>,
Cyrill Gorcunov <gorcunov@gmail.com>, Zi Yan <ziy@nvidia.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>,
Lorenzo Stoakes <ljs@kernel.org>,
Rik van Riel <riel@surriel.com>,
Vlastimil Babka <vbabka@kernel.org>,
Harry Yoo <harry@kernel.org>, Jann Horn <jannh@google.com>,
Yang Shi <shy828301@gmail.com>,
Zach O'Keefe <zokeefe@google.com>, Song Liu <song@kernel.org>,
David Stevens <stevensd@chromium.org>,
Kefeng Wang <wangkefeng.wang@huawei.com>,
Alex Williamson <alex@shazbot.org>,
Suren Baghdasaryan <surenb@google.com>,
Dave Jiang <dave.jiang@intel.com>,
Matthew Wilcox <willy@infradead.org>,
Miaohe Lin <linmiaohe@huawei.com>,
David Rientjes <rientjes@google.com>,
Shakeel Butt <shakeel.butt@linux.dev>, Jan Kara <jack@suse.cz>,
Mel Gorman <mgorman@suse.de>, Jason Gunthorpe <jgg@ziepe.ca>,
John Hubbard <jhubbard@nvidia.com>,
Gerd Hoffmann <kraxel@redhat.com>,
Vivek Kasireddy <vivek.kasireddy@intel.com>,
Dave Airlie <airlied@redhat.com>,
Matthew Brost <matthew.brost@intel.com>,
Joshua Hahn <joshua.hahnjy@gmail.com>,
Byungchul Park <byungchul@sk.com>,
Gregory Price <gourry@gourry.net>,
Ying Huang <ying.huang@linux.alibaba.com>,
Alistair Popple <apopple@nvidia.com>,
Hagen Paul Pfeifer <hagen@jauu.net>,
James Bottomley <James.Bottomley@HansenPartnership.com>,
Felix Kuehling <Felix.Kuehling@amd.com>,
Alex Sierra <alex.sierra@amd.com>,
Balbir Singh <balbirs@nvidia.com>,
Kairui Song <kasong@tencent.com>, Qi Zheng <qi.zheng@linux.dev>,
Axel Rasmussen <axelrasmussen@google.com>,
Yuanchu Xie <yuanchu@google.com>, Wei Xu <weixugc@google.com>,
Baoquan He <baoquan.he@linux.dev>,
Oscar Salvador <osalvador@suse.de>,
Muchun Song <muchun.song@linux.dev>,
Dan Williams <djbw@kernel.org>, Rakie Kim <rakie.kim@sk.com>,
Hao Li <hao.li@linux.dev>, Christoph Lameter <cl@gentwo.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Clark Williams <clrkwllms@kernel.org>,
Steven Rostedt <rostedt@goodmis.org>,
Alexei Starovoitov <ast@kernel.org>, Xu Xin <xu.xin@linux.dev>,
Chengming Zhou <chengming.zhou@linux.dev>,
Shivank Garg <shivankg@amd.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org,
linux-rt-devel@lists.linux.dev, SJ Park <sj@kernel.org>,
"David Hildenbrand (Arm)" <david@kernel.org>,
stable@vger.kernel.org, Chris Mason <mason@kernel.org>,
"Jose A. Perez de Azpillaga" <azpijr@gmail.com>
Subject: [PATCH v2 09/31] mm/khugepaged: check for uffd outside the i_pages lock
Date: Fri, 04 Dec 2026 13:50:00 +0100 [thread overview]
Message-ID: <20261204-fixes_core_3-v2-9-9aad65727632@kernel.org> (raw)
In-Reply-To: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org>
collapse_file() walks the VMAs mapping a shmem range to check for UFFD
missing registrations before filling holes. It does this with the
i_pages lock held and interrupts disabled.
Consequently, a large number of mappings can cause excessive interrupt
latency.
Let's fix it by moving the walk before taking the i_pages lock. Hold
i_mmap_rwsem for read until the retry entries are stored, and take it for
write when registering UFFD missing mode on a file-backed VMA. This
prevents new missing-mode registrations between checking the VMAs and
installing the retry entries.
Move hole counting and retry-entry insertion into
collapse_file_mark_holes().
Also pass end - 1 to mapping_rmap_tree_foreach(), which takes an
inclusive last index. Otherwise, a UFFD missing-mode registration
immediately beyond the collapse range can incorrectly prevent collapse.
This has medium urgency, as unprivileged users can trigger it.
Fixes: ac492b9c70ca ("mm/khugepaged: skip shmem with userfaultfd")
Cc: stable@vger.kernel.org
Reported-by: Chris Mason <mason@kernel.org>
Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
Tested-by: Gregory Price (Meta) <gourry@gourry.net>
Reviewed-by: Gregory Price (Meta) <gourry@gourry.net>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
mm/khugepaged.c | 61 ++++++++++++++++++++++++++++++++------------------------
mm/userfaultfd.c | 12 +++++++++++
2 files changed, 47 insertions(+), 26 deletions(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 83471396bd996..1c439b88879d3 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -2233,6 +2233,25 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
i_mmap_unlock_read(mapping);
}
+static int collapse_file_mark_holes(struct xa_state *xas,
+ pgoff_t start, pgoff_t end)
+{
+ pgoff_t index;
+ int nr_none = 0;
+
+ xas_set(xas, start);
+ for (index = start; index < end; index++) {
+ if (xas_next(xas))
+ continue;
+ xas_store(xas, XA_RETRY_ENTRY);
+ if (xas_error(xas))
+ return xas_error(xas);
+ nr_none++;
+ }
+
+ return nr_none;
+}
+
/**
* collapse_file - collapse filemap/tmpfs/shmem pages into huge one.
*
@@ -2248,8 +2267,8 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
* + swap/gup in pages if necessary;
* - copy data to new page
* - handle shmem holes
- * + re-validate that holes weren't filled by someone else
* + check for userfaultfd
+ * + re-validate that holes weren't filled by someone else
* - finalize updates to the page cache;
* - if replacing succeeds:
* + unlock huge page;
@@ -2556,27 +2575,9 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
if (nr_none) {
struct vm_area_struct *vma;
- int nr_none_check = 0;
+ int nr_none_check;
i_mmap_lock_read(mapping);
- xas_lock_irq(&xas);
-
- xas_set(&xas, start);
- for (index = start; index < end; index++) {
- if (!xas_next(&xas)) {
- xas_store(&xas, XA_RETRY_ENTRY);
- if (xas_error(&xas)) {
- result = SCAN_STORE_FAILED;
- goto immap_locked;
- }
- nr_none_check++;
- }
- }
-
- if (nr_none != nr_none_check) {
- result = SCAN_PAGE_FILLED;
- goto immap_locked;
- }
/*
* If userspace observed a missing page in a VMA with
@@ -2587,19 +2588,27 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,
* guarantees that the kernel doesn't fill a missing
* page with a zero page, so they don't matter here.
*
- * Any userfaultfds registered after this point will
- * not be able to observe any missing pages due to the
- * previously inserted retry entries.
+ * i_mmap_rwsem syncs against concurrent userfaultfd MISSING
+ * registration in userfaultfd_set_ctx(). We replace holes with
+ * retry entries before dropping i_mmap_rwsem, so later
+ * registrations cannot observe missing pages.
*/
- mapping_rmap_tree_foreach(vma, mapping, start, end) {
+ mapping_rmap_tree_foreach(vma, mapping, start, end - 1) {
if (userfaultfd_missing(vma)) {
+ i_mmap_unlock_read(mapping);
result = SCAN_EXCEED_NONE_PTE;
- goto immap_locked;
+ goto rollback;
}
+ cond_resched();
}
-immap_locked:
+ xas_lock_irq(&xas);
+ nr_none_check = collapse_file_mark_holes(&xas, start, end);
i_mmap_unlock_read(mapping);
+ if (nr_none_check < 0)
+ result = SCAN_STORE_FAILED;
+ else if (nr_none != nr_none_check)
+ result = SCAN_PAGE_FILLED;
if (result != SCAN_SUCCEED) {
xas_set(&xas, start);
for (index = start; index < end; index++) {
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index d7a9ca70fb954..af94600086213 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -2194,10 +2194,22 @@ static void userfaultfd_set_ctx(struct vm_area_struct *vma,
struct userfaultfd_ctx *ctx,
vm_flags_t vm_flags)
{
+ struct address_space *mapping = NULL;
+
vma_start_write(vma);
+
+ /* Sync with collapse_file() scanning for VM_UFFD_MISSING VMAs. */
+ if (vma->vm_file && (vm_flags & VM_UFFD_MISSING)) {
+ mapping = vma->vm_file->f_mapping;
+ i_mmap_lock_write(mapping);
+ }
+
vma->vm_userfaultfd_ctx = (struct vm_userfaultfd_ctx){ctx};
userfaultfd_set_vm_flags(vma,
(vma->vm_flags & ~__VM_UFFD_FLAGS) | vm_flags);
+
+ if (mapping)
+ i_mmap_unlock_write(mapping);
}
static void userfaultfd_reset_ctx(struct vm_area_struct *vma)
--
2.56.1
next prev parent reply other threads:[~2026-12-04 12:53 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 12:49 [PATCH v2 00/31] mm: various (urgent?) fixes, part 3 David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 02/31] mm/shmem: don't skip parts of a split swap entry when punching holes David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 03/31] mm/shmem: reject userfaultfd fills during hole punch David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 04/31] mm/shmem: fix commit limit bypass with very large shmem sizes David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 05/31] mm/shmem: don't pin inodes with eviction held off in the huge shrinker David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 06/31] mm/shmem: seal the size of SysV and shared anon files David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 07/31] mm/khugepaged: check the VMA before walking page tables when collapsing David Hildenbrand (Arm)
2026-12-04 12:49 ` [PATCH v2 08/31] mm/khugepaged: flush file after unlocking folios in collapse_file() David Hildenbrand (Arm)
2026-12-04 12:50 ` David Hildenbrand (Arm) [this message]
2026-12-04 12:50 ` [PATCH v2 10/31] mm/khugepaged: don't wait for userfaultfd David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 12/31] mm/khugepaged: check PMD support with the folios locked David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 15/31] mm/huge_memory: don't map huge PFN entries if the arch restricts PFNs David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 16/31] mm/memory: don't clear a PUD leaf as bad in a zap David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 17/31] mm/memory: drop the swap device ref before do_wp_page() David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 19/31] mm/gup: check for fatal signals in memfd_pin_folios() David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 20/31] mm/migrate_device: don't collect secretmem pages David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 21/31] mm/migrate_device: mark folios leaving device memory dirty David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 22/31] mm: taint device sections before setting subsections valid David Hildenbrand (Arm)
2026-12-04 17:53 ` Rik van Riel
2026-12-04 12:50 ` [PATCH v2 24/31] mm/mempolicy: reject an empty set of allowed nodes David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 25/31] mm/ksm: look up the folio again in cmp_and_merge_page() David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 26/31] mm/ksm: copy KSM folios at swap-in without VM_MERGEABLE David Hildenbrand (Arm)
2026-12-04 13:00 ` Lorenzo Stoakes (ARM)
2026-12-04 13:01 ` David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 27/31] mm/ksm: don't convert a shared anon folio into a KSM folio David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 29/31] mm/migrate: fix KSM oddity by checking expected refcount without mappings David Hildenbrand (Arm)
2026-12-04 12:50 ` [PATCH v2 30/31] mm/migrate_device: don't collect KSM folios 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=20261204-fixes_core_3-v2-9-9aad65727632@kernel.org \
--to=david@kernel.org \
--cc=Felix.Kuehling@amd.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=airlied@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=alex.sierra@amd.com \
--cc=alex@shazbot.org \
--cc=apopple@nvidia.com \
--cc=ast@kernel.org \
--cc=axelrasmussen@google.com \
--cc=azpijr@gmail.com \
--cc=balbirs@nvidia.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=baoquan.he@linux.dev \
--cc=bigeasy@linutronix.de \
--cc=byungchul@sk.com \
--cc=chengming.zhou@linux.dev \
--cc=cl@gentwo.org \
--cc=clrkwllms@kernel.org \
--cc=dave.jiang@intel.com \
--cc=dave@stgolabs.net \
--cc=dev.jain@arm.com \
--cc=djbw@kernel.org \
--cc=gorcunov@gmail.com \
--cc=gourry@gourry.net \
--cc=hagen@jauu.net \
--cc=hao.li@linux.dev \
--cc=harry@kernel.org \
--cc=hughd@google.com \
--cc=jack@suse.cz \
--cc=jannh@google.com \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=joshua.hahnjy@gmail.com \
--cc=kas@kernel.org \
--cc=kasong@tencent.com \
--cc=kraxel@redhat.com \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linmiaohe@huawei.com \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-rt-devel@lists.linux.dev \
--cc=ljs@kernel.org \
--cc=manfred@colorfullife.com \
--cc=mason@kernel.org \
--cc=matthew.brost@intel.com \
--cc=mgorman@suse.de \
--cc=mhocko@suse.com \
--cc=muchun.song@linux.dev \
--cc=nico.pache@linux.dev \
--cc=osalvador@suse.de \
--cc=peterx@redhat.com \
--cc=qi.zheng@linux.dev \
--cc=rakie.kim@sk.com \
--cc=riel@surriel.com \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=shakeel.butt@linux.dev \
--cc=shivankg@amd.com \
--cc=shy828301@gmail.com \
--cc=sj@kernel.org \
--cc=song@kernel.org \
--cc=stable@vger.kernel.org \
--cc=stevensd@chromium.org \
--cc=surenb@google.com \
--cc=usama.arif@linux.dev \
--cc=vbabka@kernel.org \
--cc=vivek.kasireddy@intel.com \
--cc=wangkefeng.wang@huawei.com \
--cc=weixugc@google.com \
--cc=willy@infradead.org \
--cc=xu.xin@linux.dev \
--cc=ying.huang@linux.alibaba.com \
--cc=yuanchu@google.com \
--cc=ziy@nvidia.com \
--cc=zokeefe@google.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