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=1796388791; b=xhTYvUpNKrJZIeBcsBlwvNQLgu2hvo2qro8TFIam8cb7vYBdeMmtc+PhA42BcwvvqorJ 2869hX3k+DTorAoYMSaHiHzmXUC9XwhDcwj6CUHCiki7/iF8S9oHPjmqkwgCjWm8S82im 2b0TlkfyFjAGp66FsVo2jsXO8MEDT6qKITVywAJFj7R3QZkjavFNgNSqZY4OAY85jekN0 lS7IE4MHRDZpsgi87g0Ub2yqUanqS82IFLhA8Q5yCpLgEdyiUj40D6r5bdTD5pAgk5oel pBOmqWBi6NRfVlDTvjxOM0KMQt48mdFML2zJC1fTu565YDRY3dtNMQzNl9eLOpYvb8g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388791; 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=IBI/pbng2VlB+tZYF3B2s7hk4qewIooiUOdtw1DSxqk=; b=smiZJ7oLA/V0WzLTtZGR0OUzRSwj8aPV9Caxeqim7yMc33I4V5LjmaC+LtGWFvjGxaVO 4/cdxuAvYwomsDaCP6QpjhHRKxRpmzLwgzXhOj7HlJwsjG9rHw7/TtFFM2EHgM9wErs2R 4VBBoWLdhHqnurox4TlJWTwLL+r3+Mp/57sNjCYQYZyHOR3SDru6VVtaCA/3T44yORnHP mFOXPKFa5jiJgRqnNSZGnKzrc9PtUYN5wD7e/KB5L0wDiAgkkDRfjrdsv703MM+qxKv6x wz7X1A4rYb7EOFhbaEA1Oz6twV7pEJGF8qhEJZ0Ldepa6qouX4DbAbeLJvL7P7qJP2A== 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=KWRX+vie; 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 D7022606A32D; Fri, 4 Dec 2026 12:53:11 +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=KWRX+vie Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F4F01F00936; Fri, 4 Dec 2026 12:52:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796388791; bh=IBI/pbng2VlB+tZYF3B2s7hk4qewIooiUOdtw1DSxqk=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=KWRX+viegQUr3lHPgaAIhzN1luufHl67ADfTqMQ6jx6WLz0Ot+eQ0HQLk05enAgbk IP6315bIx1a/YxGWJZ+yFYTHU1ZAYXttWCQwa1VhLNgoJG2F8QKMrRJHvez+zgmcg7 ueQGFX8AVsEtB01p8qZn0Mi3HZiXi7bsdbVeYmD4caHYlM3JrIxtt+4+ImQXC1ScuL 7pZugqg3jFsUJEbss1wfvqQPi7w8vlIYMLhNckp5wadrhtVKLWOlfdkeIyV1aDffrN qw83Sqz4vrUWizu19wCI7LGPPrTrKdIf1laF/KTaj8VD58prKsle5W/ofMiO7L2ZHs aFoR7PPzixP8g== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:50:00 +0100 Subject: [PATCH v2 09/31] mm/khugepaged: check for uffd outside the i_pages lock X-Mailing-List: stable@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: <20261204-fixes_core_3-v2-9-9aad65727632@kernel.org> References: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> In-Reply-To: <20261204-fixes_core_3-v2-0-9aad65727632@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 , "Jose A. Perez de Azpillaga" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5590; i=david@kernel.org; h=from:subject:message-id; bh=I8XQzIf7PnPpT1b/VW5yDnlbxaTJBnpQRwZrcXeWryo=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtv1z2VbKErrkymuju2HL0kWTX1ozXLO7yPgk99+Li uqpRx4zdpSyMIhxMMiKKbIEBd83ePlyXW21Gx8zzBxWJpAhDFycAjARJ32G32wtTpoJhzVVDuw7 vuz8o4Lj928F+p9dcLVx7SLmOUys/mEM/xMXizwNW6Vo2VC2N+nHHccz97UTHoRbaTLpVee/zDf ewA8A 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 Reviewed-by: Jose A. Perez de Azpillaga Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) 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 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