From nobody Fri Dec 19 20:34:49 2025 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (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 3352D1C84AB for ; Tue, 14 Oct 2025 13:07:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760447264; cv=none; b=SGqeAvAX72PMWscu/k/WU6Ywv+FguXKDaTLizk0wpJ75arZ6p4ix2gdispBzTv4OZQMjrN7/LdtIVDWP/gBKyJnfqXIStnXP1b1LAgg3IzJfAHAbmlJ02hPCtmPZxd3KbEhl2+FpXG0UK7Hyb5pKo055g5samoiPSl5hohxqCAE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760447264; c=relaxed/simple; bh=lfUFBXUxo+w4vJ46hfUjKQJDAze+72p1E8gLDcEic/g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oeN3M2ZXaTKbLP7T3ghO0JH0hpNWE4hA4jvtmzVqtE7D0cszFLjWGqd+jC/B1PRS4x3posSXWaTn0VVpLqg6sgwIk6HxRUd2z2yhO/mN/kjyaxBlxqe1S8Cc1j5PfS30RZzgTZu5+aoQEHBiI8RgXvw544cKDJ50WcGDJaSc2M4= 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=emHwBVz8; arc=none smtp.client-ip=198.175.65.21 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="emHwBVz8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1760447263; x=1791983263; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=lfUFBXUxo+w4vJ46hfUjKQJDAze+72p1E8gLDcEic/g=; b=emHwBVz8h4pPFbCol/v7IQM2wzOC+SfrVWgFhfx4X++aaOEcrMn22gED SnxJ04tSL2VkKuamDR92y5P510fSKJBkM6Iehr3hkZaUGQ2YMTtgSf6iD A9IwU06nfc5KrBk4/t/nz1PEXA7hN6F0RV/pT8Hu8tGFghirtqgigUibe E5BbNUvmJgXOBzsL14+uKOWpx/urAf2y44b3kBYchqKP9GStJJhFd3Ed7 E1QLHY0Nbn+t6hcxNpWZPAIiNMbi4XSorc0cfeN6T7p+j97ZwFITwoU9P 23vbF+/IUzIOW7M1sH616lxTxrmMf5+wQpuEp8rSvuUtOVpcCWy6UrP+T g==; X-CSE-ConnectionGUID: T0oYIKFCT8K6Y8YRLwim1w== X-CSE-MsgGUID: mBY+VAAYQtiCN6X0MV1Wjw== X-IronPort-AV: E=McAfee;i="6800,10657,11531"; a="62515356" X-IronPort-AV: E=Sophos;i="6.17,312,1747724400"; d="scan'208";a="62515356" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Oct 2025 06:07:43 -0700 X-CSE-ConnectionGUID: fEakTVjhQOicHC7aB8RVkQ== X-CSE-MsgGUID: Aj7IYaXoRgOL4s+gZknpVQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,228,1754982000"; d="scan'208";a="182675578" Received: from allen-box.sh.intel.com ([10.239.159.52]) by fmviesa010.fm.intel.com with ESMTP; 14 Oct 2025 06:07:32 -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 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 v6 2/7] mm: Actually mark kernel page table pages Date: Tue, 14 Oct 2025 21:04:32 +0800 Message-ID: <20251014130437.1090448-3-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251014130437.1090448-1-baolu.lu@linux.intel.com> References: <20251014130437.1090448-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 9741affc574e..15ce0c415d36 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