Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp2184182rbb; Tue, 27 Feb 2024 13:28:29 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWTkDWkPDWBbce3ebc31FU0hy7vFS/6eii+8NWqMmM0LS2xTPMUGbrHiGCK4dP/zJkBSRnXYorw0syM9+WOXlTUa9c8W/Bj+f0uspaouA== X-Google-Smtp-Source: AGHT+IE9UtKAbJjNn/8hXDrnK53SwNkEFhxtFNeMap3z63HcYXmW8/cUpHdgaQhkfean97FS2I0R X-Received: by 2002:a17:906:3488:b0:a43:ccd:a816 with SMTP id g8-20020a170906348800b00a430ccda816mr6423067ejb.31.1709069309166; Tue, 27 Feb 2024 13:28:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709069309; cv=pass; d=google.com; s=arc-20160816; b=r098OPRfrtNnQ+FOqDfOP1OL2OXvKd2lMJ2T1Y1vnEd3a5wXrdediGBqqXFohRCU5I NuEmDTjjXZhIDNGtPzpYUj1lMVNlaafiBqcny4TijuAo53pHnPZa+pAocedpaN82AKMa +lCPt1ycEkvlBc307B8wbNNfPmUe5sQ7iRSWrdlrh0voVgpFGwGv/5QReZz2J6TaLnsF LQ6NdSsxKt4ZqWNhx70SRkGoSltyvwAqzX9ob7wCSdPsjTx6WCADQ5xGlf0OoYXAk7IG NTutEbDs5EoSZBSBsMkFVoC6jLQUKExcfeg/LibvSw/3szSqlSLrdo34HC0kTQ15y4b+ Y4zg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=0mAjGPI49n62/EKJrSnKcsTSDrzE9V31hmWpeYbvVb8=; fh=yQt9ROgMHUZejH3hDVFamAWFXphHuLLRaOt+BxF1Gfg=; b=JLAxMRRjaX40BkYY+v5/0dUxmK/D7xqaiz2nzrQbMGIT/I8TuOnm3iT98eYTGuuw+J kskXm54YM0qyYvN8qKY5MfdCWJBrohV5D+2lzGPByZ0mNvZYGn/9YKnd4eor7uoXEGR1 wAcczjFqmJtDms8dzNSos3/Cf0xJDQKRRL6s1lCe4toBVjdgVuSkG+7RB/BdiquZtMNY 7zYj9qGE4NcN/mG2bHudcEv3JFuVFF7RoB7PO/RzUd8gjKCR1p6+/gHgTsO6BBFUIg4/ GJpc1EYakb0R+5xwf+nOfekgy5dx6lUYJYZPGNuiaiqKGbUQ55Uc2j4i7fHRooru5aBM +zvQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LkAEIAH2; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-84046-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84046-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id js14-20020a17090797ce00b00a43b0e7d8cbsi864026ejc.584.2024.02.27.13.28.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 13:28:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84046-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LkAEIAH2; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-84046-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84046-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id B85771F2D6D5 for ; Tue, 27 Feb 2024 21:28:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2D9CB15AAD9; Tue, 27 Feb 2024 21:25:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="LkAEIAH2" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (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 2D0E315959E for ; Tue, 27 Feb 2024 21:25:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709069120; cv=none; b=DwGBDKUBhAx2hi8tvZQu3FJc1tuAMrBsQo5QBld1GugOLqXet9GoE45d/JV6+w7UnJAEeKOQ44/nZJvbGJVBcPjrBFhTOgxYbUVlZpnIbw7qrHtNJrYGx+XRos+lQhaJDhSqIfq3MT8/vXlqsJd6Zm3/tVHMSCu2yNqUjBxMREg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709069120; c=relaxed/simple; bh=dUEuhxA1AlDC3wetWnBXvKm2CT6y7iSHZ1ZynKz3iIA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sMDJ2weRjoSuBodoRAmepH54NeZQojh3fN3r24lgr9P9Q+Wmu0cJ8MB/uQ0oenwSSFR8A9zzMQX7bxREbqU5RP2hlk5hj7q9v7GuIIcH0zaEulEJA36ZvFceTwedius7Evzljq/URS1iU2ucoS80b+0a6W0xuL7eGDWFjeNim6I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=LkAEIAH2; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709069120; x=1740605120; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dUEuhxA1AlDC3wetWnBXvKm2CT6y7iSHZ1ZynKz3iIA=; b=LkAEIAH2t740lJlbSKfA5DWxq33xe4JeMi7DpIfc8aX7FMiYX4PcraIf qxHtfdp3+dqx4QBhhYY1mxiqOdilZufuSBXvS3Cf0lkkVDy4eojSnVYpB sHi0BzGkIKf32t3p9n9u/hean/FPFzqzGRW22JxdMJZckpIMEPLr8/r7i h2ARjwh6iax8Fq/hctXrgll5PVFS46mmnyxBn3/lOlheJxq+XE/cwa8E+ RqH8GO+/DLPkp8HQfAIwGQjuGo0hVzUyzE5TUPX3ebrqrTd6LW/jAcxjD Ao3VK96CqF2F3d2s3P4S7usKAk0FF1KEVdELnDLZ7CdHwCqUDghnzCj7H g==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="3567076" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="3567076" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2024 13:25:10 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="937032924" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="937032924" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 27 Feb 2024 13:25:02 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id 3CC85781; Tue, 27 Feb 2024 23:24:56 +0200 (EET) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org Cc: "Rafael J. Wysocki" , Peter Zijlstra , Adrian Hunter , Kuppuswamy Sathyanarayanan , Elena Reshetova , Jun Nakajima , Rick Edgecombe , Tom Lendacky , "Kalra, Ashish" , Sean Christopherson , "Huang, Kai" , Baoquan He , kexec@lists.infradead.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" , Dave Hansen Subject: [PATCHv8 07/17] x86/mm: Return correct level from lookup_address() if pte is none Date: Tue, 27 Feb 2024 23:24:42 +0200 Message-ID: <20240227212452.3228893-8-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240227212452.3228893-1-kirill.shutemov@linux.intel.com> References: <20240227212452.3228893-1-kirill.shutemov@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: 8bit Currently, lookup_address() returns two things: 1. A "pte_t" (which might be a p[g4um]d_t) 2. The 'level' of the page tables where the "pte_t" was found (returned via a pointer) If no pte_t is found, 'level' is essentially garbage. Always fill out the level. For NULL "pte_t"s, fill in the level where the p*d_none() entry was found mirroring the "found" behavior. Always filling out the level allows using lookup_address() to precisely skip over holes when walking kernel page tables. Add one more entry into enum pg_level to indicate the size of the VA covered by one PGD entry in 5-level paging mode. Update comments for lookup_address() and lookup_address_in_pgd() to reflect changes in the interface. Signed-off-by: Kirill A. Shutemov Reviewed-by: Rick Edgecombe Reviewed-by: Baoquan He Reviewed-by: Dave Hansen --- arch/x86/include/asm/pgtable_types.h | 1 + arch/x86/mm/pat/set_memory.c | 16 ++++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/pgtable_types.h b/arch/x86/include/asm/pgtable_types.h index 0b748ee16b3d..3f648ffdfbe5 100644 --- a/arch/x86/include/asm/pgtable_types.h +++ b/arch/x86/include/asm/pgtable_types.h @@ -548,6 +548,7 @@ enum pg_level { PG_LEVEL_2M, PG_LEVEL_1G, PG_LEVEL_512G, + PG_LEVEL_256T, PG_LEVEL_NUM }; diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index f92da8c9a86d..0d2267ad4e0e 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -657,7 +657,8 @@ static inline pgprot_t verify_rwx(pgprot_t old, pgprot_t new, unsigned long star /* * Lookup the page table entry for a virtual address in a specific pgd. - * Return a pointer to the entry and the level of the mapping. + * Return a pointer to the entry (or NULL if the entry does not exist) and + * the level of the entry. */ pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address, unsigned int *level) @@ -666,32 +667,32 @@ pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address, pud_t *pud; pmd_t *pmd; - *level = PG_LEVEL_NONE; + *level = PG_LEVEL_256T; if (pgd_none(*pgd)) return NULL; + *level = PG_LEVEL_512G; p4d = p4d_offset(pgd, address); if (p4d_none(*p4d)) return NULL; - *level = PG_LEVEL_512G; if (p4d_large(*p4d) || !p4d_present(*p4d)) return (pte_t *)p4d; + *level = PG_LEVEL_1G; pud = pud_offset(p4d, address); if (pud_none(*pud)) return NULL; - *level = PG_LEVEL_1G; if (pud_large(*pud) || !pud_present(*pud)) return (pte_t *)pud; + *level = PG_LEVEL_2M; pmd = pmd_offset(pud, address); if (pmd_none(*pmd)) return NULL; - *level = PG_LEVEL_2M; if (pmd_large(*pmd) || !pmd_present(*pmd)) return (pte_t *)pmd; @@ -704,9 +705,8 @@ pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address, * Lookup the page table entry for a virtual address. Return a pointer * to the entry and the level of the mapping. * - * Note: We return pud and pmd either when the entry is marked large - * or when the present bit is not set. Otherwise we would return a - * pointer to a nonexisting mapping. + * Note: the function returns p4d, pud or pmd either when the entry is marked + * large or when the present bit is not set. Otherwise it returns NULL. */ pte_t *lookup_address(unsigned long address, unsigned int *level) { -- 2.43.0