From nobody Fri Dec 19 20:34:54 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 4487F269CE8 for ; Tue, 14 Oct 2025 13:07:32 +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=1760447253; cv=none; b=lZuHGzmVRCOy6BjsZh9Ji76Awvkoa5REA1uhj2Cyd/VW5Dehqp5ApPLcFq5u998fZFd3AMeolCSK79IqGY3+N6cOSwFNynmNAh5mlMq0y2YIpcB8xmDmf/aarrHuHQanYU2oZr/5KyWrU37VyS4OZZckmGRJghyihsxja0k812c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760447253; c=relaxed/simple; bh=gJZPFOgCCY7Vm1gn07xeeMUAM+JDjIVlwa8HcX5Wx9M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EagDl3VCyA0yWDNhg/KTgm57XLUNgMgYgQqqIEzpqlvMIrNonkvrHvXe3yLRGFX8lggTLWcC1niZK/SK5a9dc+wmFs+2+qCy2nqgDxXiz/kbBZJ21PA+jt6/67Sn3Cu9T8mQ9O6urXMPxRltSN4g2YViqTtTAfRDdHFoB6vWuYs= 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=VKjYmE6y; 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="VKjYmE6y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1760447253; x=1791983253; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gJZPFOgCCY7Vm1gn07xeeMUAM+JDjIVlwa8HcX5Wx9M=; b=VKjYmE6yjYgscVm1jQUeeHvKgCOhYliz7cp2WMVMixA2EdhuB/Ii6wv5 JWxIUYyS1Y8shBQg73jQuTgd2LQUYwurxTd0F+174WuL19HyvPJNUuiFx eLUyxILRYWE7JDordtrBd1KGC5PKHDTZ2WOzDQpMRgUZa79OXczL7GQvI pHAovNU5Mq/pa8UMOWo6WrcNhQ4LdniBevN+eQQYxnaPgckpi2AzbExUz w9hnU2pMTRQaKRpOu5HJVPjOat6viMs3DZ5L5niPFkAAYKKWWUo4SU/m7 14tQIKvhrWUWDSCxZWiQZSuxb9Umc/CM6qCjxTpY4Whv3hjnirl8J589A g==; X-CSE-ConnectionGUID: l3aN9MGdQSyHi/+d6QWNaA== X-CSE-MsgGUID: AvQ0GG+rRC2uFQWW3lnadQ== X-IronPort-AV: E=McAfee;i="6800,10657,11531"; a="62515324" X-IronPort-AV: E=Sophos;i="6.17,312,1747724400"; d="scan'208";a="62515324" 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:32 -0700 X-CSE-ConnectionGUID: LLjJGhPvRVORbJ3C2K7/Og== X-CSE-MsgGUID: WwAf0EKjTJi6kFtDp18FpQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,228,1754982000"; d="scan'208";a="182675546" Received: from allen-box.sh.intel.com ([10.239.159.52]) by fmviesa010.fm.intel.com with ESMTP; 14 Oct 2025 06:07:18 -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 1/7] mm: Add a ptdesc flag to mark kernel page tables Date: Tue, 14 Oct 2025 21:04:31 +0800 Message-ID: <20251014130437.1090448-2-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 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 --- include/linux/mm.h | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index d16b33bacc32..9741affc574e 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(struct ptdesc *ptdesc) +{ + return test_bit(PT_kernel, &ptdesc->pt_flags.f); +} + /** * pagetable_alloc - Allocate pagetables * @gfp: GFP flags --=20 2.43.0