From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 615C8C44512 for ; Thu, 16 Jul 2026 21:32:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=4TmiXyJvQ92TKxUnkF2EBAbSx4BVAEx8E3qvkE5M06w=; b=yaFFdeLitV0LMMECPU/D9AKDN1 plTPj6vKnKSEE9FhyK0vhC5iGKf1EDBA2EYGnC/Nbv5t/XIO+v8NmnM+FMWmqL1muVTlv/EXOhRaD t9gqBeb0N7oSkKe2hnsnlC6JYAwdiaLrqhkDnSIJIrW2FYFg0xpPHOfln90WdMKmDituOJHmBbC1r U9uc8ct4XRpBsHtymjeTmDy3yoel7wkUTm67AJhT0L9hcU0APYqnMvupZKYI6Ty0gDi9ODdQJliVE mNvyS1Wd8eyJk6E4E9BoluzhJWgb+5+vep9wb/CrcP38FneaIQ03gWMD+u0FPU0TJ83vkmMcvB1ap jkA90+Cg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wkTgZ-00000000clP-04RP; Thu, 16 Jul 2026 21:32:03 +0000 Received: from sea.source.kernel.org ([2600:3c0a:e001:78e:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wkTgX-00000000ckb-1yzf for linux-arm-kernel@lists.infradead.org; Thu, 16 Jul 2026 21:32:01 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id C888342D81; Thu, 16 Jul 2026 21:32:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8ECAE1F00A3A; Thu, 16 Jul 2026 21:31:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784237520; bh=4TmiXyJvQ92TKxUnkF2EBAbSx4BVAEx8E3qvkE5M06w=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=GYgZQmvmtJSCVwlxmpjDgZpiPmaoPOUEQojBo7h8SSWNVzSGj11N/GUGKOHB/4X7E xjyPpfeJfxld/9aBWN4fwcoHpUX+rynA8+i1bnLjwmsTf1Dn3tQf0ldvNMYAOJ3T+g ZYr19KOkbGbG3bwcoR43r0vZOR9u/ENab3upSYP1tLsLhaU5z9woe4i3Vv9e1ZRora iZW4tyHUfHzthiX+ve4ZGiM+50GiUG9l/3ow+618/ct2YyCbuNXjqRtNyjwymX+5oS /kTxJlA8i/wPaUpCmiRfbpO4XiuFMRJncNBkmpIftb1iLfatGaI/pp2BbHa+qq1gny p6Jb+93YStcmQ== From: "Lorenzo Stoakes (ARM)" Date: Thu, 16 Jul 2026 22:31:14 +0100 Subject: [PATCH mm-hotfixes v4 3/4] mm/ptdump: always stabilise against page table freeing using init_mm MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260716-series-vmap-race-fix-v4-3-8c108c4317df@kernel.org> References: <20260716-series-vmap-race-fix-v4-0-8c108c4317df@kernel.org> In-Reply-To: <20260716-series-vmap-race-fix-v4-0-8c108c4317df@kernel.org> To: Andrew Morton , Suren Baghdasaryan , "Liam R. Howlett" , Vlastimil Babka , Shakeel Butt , David Hildenbrand , Mike Rapoport , Michal Hocko , Uladzislau Rezki , Toshi Kani , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , Kiryl Shutsemau , Catalin Marinas , Will Deacon , Dev Jain , Ryan Roberts Cc: David Carlier , ljs@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, "Denis V. Lunev" , stable@vger.kernel.org X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3325; i=ljs@kernel.org; h=from:subject:message-id; bh=VITXU838ytmI5wjkeVjJjzJ76fi6Ga2j8Ljay/OkRtw=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLIifdeK1H72/9qooXMh7OxZvidbk/uXLSj4tneKLMPTC Ya+NpmbOkpZGMS4GGTFFFmefxHfHyQSNq/zgr8bzBxWJpAhDFycAjARTkFGhjPp5b9fLnBdf+/V qrsRtnMETV8qZqkLlsdxxuhvz93xRISR4YmPyWsx52chzOyJO2bXi16zvJtqLaHdqDpniUxxyfd 4bgA= X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Previous commits have established the invariant that kernel page table freeing is performed while an mmap read lock on init_mm is held, which fixes races between ptdump and kernel page table freeing over init_mm. However, x86 and arm64 can perform a ptdump over an mm other than init_mm via ptdump_walk_pgd() and since kernel memory ranges are shared across non-kernel mm's, this means that the race still exists for these cases. Fix this by acquiring a nested mmap write lock for init_mm in ptdump_walk_pgd(). This is safe as we take this after mmap write locking the mm, and nothing acquires the init_mm lock first before locking an arbitrary mm, so no deadlock is possible. Also update walk_page_range_debug() to assert that init_mm is write locked, add a comment explaining why and remove some redundant code, and eliminate the unnecessary and confusing invocation of walk_kernel_page_table_range(). We can safely remove the non-NULL check for walk.mm, as the mmap lock asserts would NULL pointer deref if it was (and of course no callers do this). The first point at which ptdump can race kernel page table freeing is commit b6bdb7517c3d ("mm/vmalloc: add interfaces to free unmapped page table"), so we target this in the Fixes tag. Fixes: b6bdb7517c3d ("mm/vmalloc: add interfaces to free unmapped page table") Cc: stable@vger.kernel.org Reviewed-by: Mike Rapoport (Microsoft) Acked-by: David Hildenbrand (Arm) Reviewed-by: Kiryl Shutsemau Signed-off-by: Lorenzo Stoakes (ARM) --- mm/pagewalk.c | 14 +++++++++----- mm/ptdump.c | 7 +++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/mm/pagewalk.c b/mm/pagewalk.c index bbcfd68d0907..5d87c632a255 100644 --- a/mm/pagewalk.c +++ b/mm/pagewalk.c @@ -702,12 +702,16 @@ int walk_page_range_debug(struct mm_struct *mm, unsigned long start, * to account for page table freeing on vmap huge page mapping. */ mmap_assert_write_locked(mm); + /* + * x86, arm64 ptdump allow walks of efi mm's and x86 ptdump allows walks + * of arbitrary mm's. + * + * However, they both must also hold the init_mm lock to account for + * concurrent kernel page table freeing. + */ + mmap_assert_write_locked(&init_mm); - /* For convenience, we allow traversal of kernel mappings. */ - if (mm == &init_mm) - return walk_kernel_page_table_range(start, end, ops, - pgd, private); - if (start >= end || !walk.mm) + if (start >= end) return -EINVAL; if (!check_ops_safe(ops)) return -EINVAL; diff --git a/mm/ptdump.c b/mm/ptdump.c index 973020000096..5851096e6f65 100644 --- a/mm/ptdump.c +++ b/mm/ptdump.c @@ -178,11 +178,18 @@ void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm, pgd_t *pgd) get_online_mems(); mmap_write_lock(mm); + /* To stabilise kernel page tables we must hold the init_mm lock too. */ + if (mm != &init_mm) + mmap_write_lock_nested(&init_mm, SINGLE_DEPTH_NESTING); + while (range->start != range->end) { walk_page_range_debug(mm, range->start, range->end, &ptdump_ops, pgd, st); range++; } + + if (mm != &init_mm) + mmap_write_unlock(&init_mm); mmap_write_unlock(mm); put_online_mems(); -- 2.55.0