From nobody Tue Dec 16 21:10:39 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.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 D352D30AAB3 for ; Wed, 22 Oct 2025 08:29:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761121766; cv=none; b=Rik5WPm9ABXS9CMb+C1Tr+Ztm1FRT+tmBrO8ezJ8/646s593WKZ7u/rSCrqM1BhJ4tPGAyKI7Ic5Tw8UNbADhROR3dPyWn2hujEFNEXp9vlvO2SxyiHDFL7OSv7qIKdq2IUuHdmICY/NGENtj3sHy6dHFyzNtiSG1PARJsHKm/U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761121766; c=relaxed/simple; bh=PamdCW5SIExW2MSTVmRrUvcUix6foLzr9uopaA+M2pw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RbU+j3i4W2XhW+tkLNIz68etd6gwdkaQzkNxM0D5cHngo5VmSOQtTicj/r1TbO/CtnzeJqGlephZJmpRuFXx752pLqrNot1zFpoUnysd3yWVe/vxht6o8xPpAaOnZfKQ/SECA17Ib83IkJ1M/n9YmNF638HNxo3fQqZWQu0Q3ko= 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=YIC6nwIj; arc=none smtp.client-ip=192.198.163.18 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="YIC6nwIj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1761121765; x=1792657765; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PamdCW5SIExW2MSTVmRrUvcUix6foLzr9uopaA+M2pw=; b=YIC6nwIjODsMzvMatCwTCe3l95MmT4F+gFqNkzkCGi8Amfjgvt3ALQZK fO4NCnUjLBI4XcUWncCJTMxzehsmM5wNgy72xsKshgWX1kAajPkNsnGec OKUIqVWB6rDGj4avV4T0YpHYLWAHg2ZX2eyil1e8PvSZEnArB0pESrdQI uSrhXoVDlYDnXxyedaMyunesOlyUxdXUyH598rbvIn3NE2PaFJ9le43Fc k+kzqJXOYeKrbHo7DJD6Y1+X1DgVHWekkQ4A4ZqHHfp08zxzVDqNMwCZk Y4qv5zxa3O1lzohtmDNtwoVKW42ipR5iShNmo9Be8D5uYqutnqfh2qv+J Q==; X-CSE-ConnectionGUID: 9XHQceP0Tv+YlFhZRehNgw== X-CSE-MsgGUID: +V1Vd6XPTR6JJPGEgBHGsg== X-IronPort-AV: E=McAfee;i="6800,10657,11586"; a="62471552" X-IronPort-AV: E=Sophos;i="6.19,246,1754982000"; d="scan'208";a="62471552" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Oct 2025 01:29:24 -0700 X-CSE-ConnectionGUID: uYNe54tGQT2jRkTDg8M2GQ== X-CSE-MsgGUID: 96m6+PNSSlueQgOErfr6UA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,246,1754982000"; d="scan'208";a="183516242" Received: from allen-box.sh.intel.com ([10.239.159.52]) by fmviesa007.fm.intel.com with ESMTP; 22 Oct 2025 01:29:17 -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 2/8] mm: Add a ptdesc flag to mark kernel page tables Date: Wed, 22 Oct 2025 16:26:28 +0800 Message-ID: <20251022082635.2462433-3-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 The page tables used to map the kernel and userspace often have very different handling rules. There are frequently *_kernel() variants of functions just for kernel page tables. That's not great and has lead to code duplication. Instead of having completely separate call paths, allow a 'ptdesc' to be marked as being for kernel mappings. Introduce helpers to set and clear this status. Note: this uses the PG_referenced bit. Page flags are a great fit for this since it is truly a single bit of information. Use PG_referenced itself because it's a fairly benign flag (as opposed to things like PG_lock). It's also (according to Willy) unlikely to go away any time soon. PG_referenced is not in PAGE_FLAGS_CHECK_AT_FREE. It does not need to be cleared before freeing the page, and pages coming out of the allocator should have it cleared. Regardless, introduce an API to clear it anyway. Having symmetry in the API makes it easier to change the underlying implementation later, like if there was a need to move to a PAGE_FLAGS_CHECK_AT_FREE bit. 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/linux/mm.h | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index d16b33bacc32..354d7925bf77 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2940,6 +2940,7 @@ static inline pmd_t *pmd_alloc(struct mm_struct *mm, = pud_t *pud, unsigned long a #endif /* CONFIG_MMU */ =20 enum pt_flags { + PT_kernel =3D PG_referenced, PT_reserved =3D PG_reserved, /* High bits are used for zone/node/section */ }; @@ -2965,6 +2966,46 @@ static inline bool pagetable_is_reserved(struct ptde= sc *pt) return test_bit(PT_reserved, &pt->pt_flags.f); } =20 +/** + * ptdesc_set_kernel - Mark a ptdesc used to map the kernel + * @ptdesc: The ptdesc to be marked + * + * Kernel page tables often need special handling. Set a flag so that + * the handling code knows this ptdesc will not be used for userspace. + */ +static inline void ptdesc_set_kernel(struct ptdesc *ptdesc) +{ + set_bit(PT_kernel, &ptdesc->pt_flags.f); +} + +/** + * ptdesc_clear_kernel - Mark a ptdesc as no longer used to map the kernel + * @ptdesc: The ptdesc to be unmarked + * + * Use when the ptdesc is no longer used to map the kernel and no longer + * needs special handling. + */ +static inline void ptdesc_clear_kernel(struct ptdesc *ptdesc) +{ + /* + * Note: the 'PG_referenced' bit does not strictly need to be + * cleared before freeing the page. But this is nice for + * symmetry. + */ + clear_bit(PT_kernel, &ptdesc->pt_flags.f); +} + +/** + * ptdesc_test_kernel - Check if a ptdesc is used to map the kernel + * @ptdesc: The ptdesc being tested + * + * Call to tell if the ptdesc used to map the kernel. + */ +static inline bool ptdesc_test_kernel(const struct ptdesc *ptdesc) +{ + return test_bit(PT_kernel, &ptdesc->pt_flags.f); +} + /** * pagetable_alloc - Allocate pagetables * @gfp: GFP flags --=20 2.43.0