From nobody Tue Dec 16 21:10:39 2025 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) (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 0892B309F17 for ; Wed, 22 Oct 2025 08:29:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761121775; cv=none; b=BqumasmzaSQ7htt5LftUA3URIdYxG2FTD9Incn6SWBP6x411vfvHKxcrUqhEZnp/8Vuxl2iWKhHqG55M5Cih0NTxjfchJp5WfFFu7lyOX2iHoX85rPH84NJ31HPsCCWmUFI8dDLGqfI5976KHDV517KQhgUCwerw8CQnZ8x4ThM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761121775; c=relaxed/simple; bh=6/Nv8E+vFyhulzaBjC/GB/VBGNF8clh7bzlNZP5LHAk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mZk4q5lvuV5Ji0hIE2hqJ52jjyHjs/D/DTj7yColEj/DF+4JEuGLj2Yil51pjnfocW11ok6CVtP2msXIyp/cMqZopxOPwZrAtPHRRC+9umfBCQtjPydg81AQ7h/Vo/NxkC92jJdFPFO/6BYGBhh6iZBVG52UxOWdAXgGn+GR4zU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=jtIOxvSF; arc=none smtp.client-ip=198.175.65.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="jtIOxvSF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1761121774; x=1792657774; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6/Nv8E+vFyhulzaBjC/GB/VBGNF8clh7bzlNZP5LHAk=; b=jtIOxvSFRVfsgMqrosQVDfoja/Ea7lK0lo4rWm67aVvc42W0uYZ5Lk/M DAGD1wBE0ZdMDlFZfPlxvbVlhahR+es4nv+MHE/6Xs4Lx8QJq1xG918xl RkB28knjoiYaXLii+52i9nj32GfpMQeq9XKEohL0RcRq4Kbp2bLrD2iQQ GoDRyJ7cBgMKDYWbFqiVlLQki978WUCuYDj7s0c/2EYNPWEkiNfOUi+yC pV3zBZrw1itg5AKLJIU89djBM41QTRMau3aG/FsvPCqgjKa4o5EnpREiV jQhig0tXWlYefvkV/GbRxfPcHn2mSVFVILmhtTJ2eLIHguFNABJOmqu4s A==; X-CSE-ConnectionGUID: YIlP66PETuSI7wP9maRN2Q== X-CSE-MsgGUID: KHXkFQCNQT+Pt84cv1sYCQ== X-IronPort-AV: E=McAfee;i="6800,10657,11531"; a="67099637" X-IronPort-AV: E=Sophos;i="6.17,312,1747724400"; d="scan'208";a="67099637" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Oct 2025 01:29:33 -0700 X-CSE-ConnectionGUID: qms/D5zxRzOlqkuzqQHGFA== X-CSE-MsgGUID: zjDxKFmJSg6hP+Fa5Bz5bA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,246,1754982000"; d="scan'208";a="183516294" Received: from allen-box.sh.intel.com ([10.239.159.52]) by fmviesa007.fm.intel.com with ESMTP; 22 Oct 2025 01:29:24 -0700 From: Lu Baolu To: Joerg Roedel , Will Deacon , Robin Murphy , Kevin Tian , Jason Gunthorpe , Jann Horn , Vasant Hegde , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Alistair Popple , Peter Zijlstra , Uladzislau Rezki , Jean-Philippe Brucker , Andy Lutomirski , Yi Lai , David Hildenbrand , Lorenzo Stoakes , "Liam R . Howlett" , Andrew Morton , Vlastimil Babka , Mike Rapoport , Michal Hocko , Matthew Wilcox , Vinicius Costa Gomes Cc: iommu@lists.linux.dev, security@kernel.org, x86@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Dave Hansen , Lu Baolu Subject: [PATCH v7 3/8] mm: Actually mark kernel page table pages Date: Wed, 22 Oct 2025 16:26:29 +0800 Message-ID: <20251022082635.2462433-4-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251022082635.2462433-1-baolu.lu@linux.intel.com> References: <20251022082635.2462433-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Dave Hansen Now that the API is in place, mark kernel page table pages just after they are allocated. Unmark them just before they are freed. Note: Unconditionally clearing the 'kernel' marking (via ptdesc_clear_kernel()) would be functionally identical to what is here. But having the if() makes it logically clear that this function can be used for kernel and non-kernel page tables. Signed-off-by: Dave Hansen Signed-off-by: Lu Baolu Reviewed-by: Jason Gunthorpe Reviewed-by: Kevin Tian Acked-by: David Hildenbrand Acked-by: Mike Rapoport (Microsoft) --- include/asm-generic/pgalloc.h | 18 ++++++++++++++++++ include/linux/mm.h | 3 +++ 2 files changed, 21 insertions(+) diff --git a/include/asm-generic/pgalloc.h b/include/asm-generic/pgalloc.h index 3c8ec3bfea44..b9d2a7c79b93 100644 --- a/include/asm-generic/pgalloc.h +++ b/include/asm-generic/pgalloc.h @@ -28,6 +28,8 @@ static inline pte_t *__pte_alloc_one_kernel_noprof(struct= mm_struct *mm) return NULL; } =20 + ptdesc_set_kernel(ptdesc); + return ptdesc_address(ptdesc); } #define __pte_alloc_one_kernel(...) alloc_hooks(__pte_alloc_one_kernel_nop= rof(__VA_ARGS__)) @@ -146,6 +148,10 @@ static inline pmd_t *pmd_alloc_one_noprof(struct mm_st= ruct *mm, unsigned long ad pagetable_free(ptdesc); return NULL; } + + if (mm =3D=3D &init_mm) + ptdesc_set_kernel(ptdesc); + return ptdesc_address(ptdesc); } #define pmd_alloc_one(...) alloc_hooks(pmd_alloc_one_noprof(__VA_ARGS__)) @@ -179,6 +185,10 @@ static inline pud_t *__pud_alloc_one_noprof(struct mm_= struct *mm, unsigned long return NULL; =20 pagetable_pud_ctor(ptdesc); + + if (mm =3D=3D &init_mm) + ptdesc_set_kernel(ptdesc); + return ptdesc_address(ptdesc); } #define __pud_alloc_one(...) alloc_hooks(__pud_alloc_one_noprof(__VA_ARGS_= _)) @@ -233,6 +243,10 @@ static inline p4d_t *__p4d_alloc_one_noprof(struct mm_= struct *mm, unsigned long return NULL; =20 pagetable_p4d_ctor(ptdesc); + + if (mm =3D=3D &init_mm) + ptdesc_set_kernel(ptdesc); + return ptdesc_address(ptdesc); } #define __p4d_alloc_one(...) alloc_hooks(__p4d_alloc_one_noprof(__VA_ARGS_= _)) @@ -277,6 +291,10 @@ static inline pgd_t *__pgd_alloc_noprof(struct mm_stru= ct *mm, unsigned int order return NULL; =20 pagetable_pgd_ctor(ptdesc); + + if (mm =3D=3D &init_mm) + ptdesc_set_kernel(ptdesc); + return ptdesc_address(ptdesc); } #define __pgd_alloc(...) alloc_hooks(__pgd_alloc_noprof(__VA_ARGS__)) diff --git a/include/linux/mm.h b/include/linux/mm.h index 354d7925bf77..cca5946a9771 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3035,6 +3035,9 @@ static inline void pagetable_free(struct ptdesc *pt) { struct page *page =3D ptdesc_page(pt); =20 + if (ptdesc_test_kernel(pt)) + ptdesc_clear_kernel(pt); + __free_pages(page, compound_order(page)); } =20 --=20 2.43.0