From mboxrd@z Thu Jan 1 00:00:00 1970 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 397D52F8EA2; Mon, 17 Aug 2026 14:01:45 +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=1786975306; cv=none; b=Yp3IntBzE4feS5mkrfZ0rsjimfbXpTtNBcuvgi4ZDsoHetX+efcTIgZ4F6ZqJ5IPnj7AGRIaamro+F9TvpGfzuOgy9A5aetXdL1oomGBepQ+PwW8/CLSFXZ23UFhsIM9T5YByXRQKcETt2odksK1FJNNZMaZ1Byk2BnZCnkjXTo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786975306; c=relaxed/simple; bh=Vv5SQlMrEttfmM9UeVoTlDChESNBg/cBR2vy5XZ30b4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FMVLAwyT0eXXfPjfUlJckrfCu5TZ8BdKu+h37svD7eE8mfbyxCgOzVDXRMzmtYexUyzQrVrGpaT9s/uiarfOHucnnFNNqyz7NVQvE1KDASZk2N7e28n8oLgtoeljRHwLi7Ij/qi67+qmglqBCDQ/cES56MMMy/AciZK1buMLmCI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=J8Fj0Euw; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="J8Fj0Euw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6D4C01F00A3A; Mon, 17 Aug 2026 14:01:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1786975305; bh=VxppmD4qpgcKesv0cLmdjHLGgeR6JGL5J7D+OyD0wtc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=J8Fj0Euwzi7h01+Lfmjqd0dOt9gZF/LwM9YeKHsWw1Qx7s1KvWUlxUhXlubBKpmSr Yf+4regLD/5MJ7zB2a7OzR/ThsslYwSpTG3NNTLO/8Y58aDW+33bgafObkf7Jz+Wqg PjQqAqJnZi1EeAYKTtb9gjMoXwOFLKGzcr0sZa+Y= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "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 Subject: [PATCH 6.18 232/250] mm/ptdump: always stabilise against page table freeing using init_mm Date: Mon, 17 Aug 2026 15:33:13 +0200 Message-ID: <20260817132545.970868979@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260817132536.466235697@linuxfoundation.org> References: <20260817132536.466235697@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Lorenzo Stoakes (ARM) commit 27c32e5538344b13c1505a08861e04620c125d47 upstream. 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: Greg Kroah-Hartman --- mm/pagewalk.c | 14 +++++++++----- mm/ptdump.c | 7 +++++++ 2 files changed, 16 insertions(+), 5 deletions(-) --- a/mm/pagewalk.c +++ b/mm/pagewalk.c @@ -702,12 +702,16 @@ int walk_page_range_debug(struct mm_stru * 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_valid(ops)) return -EINVAL; --- a/mm/ptdump.c +++ b/mm/ptdump.c @@ -178,11 +178,18 @@ void ptdump_walk_pgd(struct ptdump_state 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();