Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 624E33AAF57 for ; Wed, 19 Aug 2026 14:56:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787151385; cv=none; b=uGqHVMljPn72l/wYHnyCmZRHvi85T6d0cEN2zoyulzn72q6B0lWh9RWkRXXR4fRSZ765zljOQ4atyS8shZ+hnAav8isK70N9eukSLyOmNXqbGYPMc8gk5i6gW8ylLtPW2HHffOcHqguzTFvL6NA/PxiWcH4YldtxjHIsQST/EyQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787151385; c=relaxed/simple; bh=ExybWqohdgaANCm+kk/koDpDKul2LMabMe+bcJTeUhs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bjRD+uI04WY+6kLfSnYsLDhlsOfNZkfrXGYBmo0n+2H49d13V9tP7K2ninS6hAxQv3ozj8zK5tPIufknoZIJxV3t01Eh4GlDimHqWONgVWULnOct+6aHlXNWfx/XtKxmqWvX/Eyj+NHFALMSWWXVBxaDULwIlXUN9cactZTr6r0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=O39LjekO; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="O39LjekO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1D3B1F00A3A; Wed, 19 Aug 2026 14:56:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1787151374; bh=gtr2xS3IGv3hE/sW3IdpzxozW3E6b96o0SXpzheg0eA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=O39LjekOSrLjbW6K+iVrP6wXC9cQ32gNCcURuWBgj3rD7dgX5Ju4QgJjF2Lx8bop3 NI2Tu6UMJd9ukjqrti6Po+hG8I1F/Ds5/MGE7vz1iTX7B8vzzm0S3uf7S4678m8kIK 9/svtQopBJkHeKpS6cE0VCuPF2/IS3D1rqYZnUplhMbRdgFjESzB+5YIocpte2G3Hc LpsGamB3iKKSgb4BOkaeNq1r7GrOWl8QrWq/AZcZfY8Sw1+CMbBGik9bYgwEtPur7E dahLj3Pb1HE7b291vltQY7lZw5sG6ZOW9EJrdpYeOkH0R6F9RL+m1hwuYI9fDm3axc 8wtrkQ5scMoOw== From: Sasha Levin To: stable@vger.kernel.org Cc: "Lorenzo Stoakes (ARM)" , "Mike Rapoport (Microsoft)" , "David Hildenbrand (Arm)" , Kiryl Shutsemau , Andy Lutomirski , "Borah, Chaitanya Kumar" , "Borislav Petkov (AMD)" , Catalin Marinas , Dave Hansen , David Carlier , Dev Jain , "H. Peter Anvin" , Ingo Molnar , "Liam R. Howlett" , Michal Hocko , Peter Zijlstra , Ryan Roberts , Shakeel Butt , Suren Baghdasaryan , Toshi Kani , "Uladzislau Rezki (Sony)" , Vlastimil Babka , Will Deacon , Andrew Morton , Sasha Levin Subject: [PATCH 6.1.y 3/3] mm/ptdump: always stabilise against page table freeing using init_mm Date: Wed, 19 Aug 2026 10:56:02 -0400 Message-ID: <20260819145602.3898518-3-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260819145602.3898518-1-sashal@kernel.org> References: <2026081757-agreed-lagging-0a18@gregkh> <20260819145602.3898518-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Lorenzo Stoakes (ARM)" [ Upstream commit 27c32e5538344b13c1505a08861e04620c125d47 ] 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. Link: https://lore.kernel.org/20260723-series-vmap-race-fix-v6-4-8cc77dcc0018@kernel.org Fixes: b6bdb7517c3d ("mm/vmalloc: add interfaces to free unmapped page table") Signed-off-by: Lorenzo Stoakes (ARM) Reviewed-by: Mike Rapoport (Microsoft) Acked-by: David Hildenbrand (Arm) Reviewed-by: Kiryl Shutsemau Cc: Andy Lutomirski Cc: "Borah, Chaitanya Kumar" Cc: "Borislav Petkov (AMD)" Cc: Catalin Marinas Cc: Dave Hansen Cc: David Carlier Cc: Dev Jain Cc: "H. Peter Anvin" Cc: Ingo Molnar Cc: Liam R. Howlett Cc: Michal Hocko Cc: Peter Zijlstra Cc: Ryan Roberts Cc: Shakeel Butt Cc: Suren Baghdasaryan Cc: Toshi Kani Cc: "Uladzislau Rezki (Sony)" Cc: Vlastimil Babka Cc: Will Deacon Cc: Signed-off-by: Andrew Morton Signed-off-by: Sasha Levin --- 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 3172ce96d66c..831330aa7f7b 100644 --- a/mm/pagewalk.c +++ b/mm/pagewalk.c @@ -568,12 +568,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; return walk_pgd_range(start, end, &walk); diff --git a/mm/ptdump.c b/mm/ptdump.c index 0b8c9eb6aa29..9ce9fed8a987 100644 --- a/mm/ptdump.c +++ b/mm/ptdump.c @@ -155,11 +155,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.53.0