From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795946989; b=D+vokwr0smEy++RzljAYOQtihsaPtTd5gEq7q0bgNclGAwP75dEqJKIGrFGjsTfffxwk oMFEV6+RjOdfkk+g9RFuDhFPkk1xvRf0O/9s1XoPKjdKJ0g4mZJoiqAXguRsG4WW+dSpL rSSMPj3ahQsYdltwR9BUbyoXPbBN3Hq/dhOFzaS0WEsE+cD9G4mBr6OQ5/hCfdAdIcoF8 w92AychEmgVOdKUPrpfkBeFsnYYBomex8q0o9ScxdvZ2xTIUaTbykMsOu+bt7UwNzMOjs 5fcXTyugpvR7UqNw/0YxnP0/5M0q93syjbUB0VzOYhAOaTxUAiAvRvpG7J0IgpDa7KA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795946989; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=he2hWK8x1hR0aHDaH6OQst6KwixJPPilWDzV5MLb2GM=; b=woMfz7ojZuPQ4xgaXpK4GFjzYGlxUFtSAVUA1KUENJqEyZQ49CqoW8Yy8+1eGL9/iUpB gQY1PxQhIsMiGMFXvCRgiql6FfJlHTkL7C4Esgp4GV3b4nTQrGAklZo+Wu4FkL7cUVRMc qRyDKzIoCI6HA6BXJpGxQ1dQOiTVvwhviAZzy/xZFfFUNBpjimucNIb4pnallJQnXfAOM TXPIJJZO11FKgJMQvWNjM56kZVQQIJWjsXQ7olO4T2lggrBEFMzt+a+FF36AJPTWe2ceJ +4OfIjIec8i1bHVRY/yPTPFRzK6+EdphQdDImas/4QGWPwUA2MsmpPeoYFJ8dhn27yg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=B1rbfj1y; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18ADA606A323; Sun, 29 Nov 2026 10:09:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=B1rbfj1y Received: by smtp.kernel.org (Postfix) with ESMTPSA id 51D031F00896; Sun, 29 Nov 2026 10:09:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795946988; bh=he2hWK8x1hR0aHDaH6OQst6KwixJPPilWDzV5MLb2GM=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=B1rbfj1yxh8oACUs8qw5QenDPNQTn9hz/8ecdGz47H4oevAnZ/BI4z+GLsigPaYp1 fTJsdSQccEP08f2XAotkxHk35j2WjqNYnYBpFKDf98Rvj6M2f9Y3+sgxxe7ZrB5lh0 MFfrNy78U3CJ11pfxaDrbKMhqQ5I6ASE/HxZYBY0qczdCm89Yq/aXHfftjBl0kyJH+ Q6gvqZznNnyD11m7pHYkMDnBf9uMKZEF83VhnZRKY5YikDSOA8sG5372ulExd2xeU9 P8awpEm7ttsJKV0DMFo2WpdSSnKT9EW0eEpWCVspa+9jem8aHO6AY5on2MTjyrEFSR xbPqqrnLWkWtw== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:06:40 +0100 Subject: [PATCH 09/32] mm/khugepaged: check for uffd outside the i_pages lock X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261129-fixes_core_3-v1-9-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> In-Reply-To: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> To: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg 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 , "David Hildenbrand (Arm)" , stable@vger.kernel.org, Chris Mason X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5422; i=david@kernel.org; h=from:subject:message-id; bh=A/BER3wbP+CLmPndgwIxA5eUBlJXt11JB8OsuC+tumM=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f+oYzJrPPUlrWYRXcuaPk1WTE9Wdw+TXZTwq/P90l ciyIk2rjlIWBjEOBlkxRZag4PsGL1+uq61242OGmcPKBDKEgYtTACYysYjhn/1qnsB1WzOX5j+Y nR7ZMsuE+YG/01YX05Snc4W+rpplGMPIsMjkb2nj3YrIZ6L7si7++zb/UvrRCYLP2/7+kpx4rVE 6gQ0A X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A 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 Signed-off-by: David Hildenbrand (Arm) --- 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 7296673378c72..5fa9e67351332 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2232,6 +2232,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. * @@ -2247,8 +2266,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; @@ -2555,27 +2574,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 @@ -2586,19 +2587,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 8ee1c7eb10d59..09e383370b48f 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -2193,10 +2193,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