Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp2664639lqb; Tue, 28 May 2024 07:01:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV7P8Vw03kNhvL3m7NDTwRKtYwG9AotKR4diWpfjIi5zBlwWPAlq8g6CO9hzYOixqA07rqjv4zvsBwrVpEEhAgHh9U0bmg6fDqfsCAyyA== X-Google-Smtp-Source: AGHT+IGAxW/SaAcNx7Kp7TeRhVCV2K25Mp7twkWUnTfsG13NPAs7B4ut4WErfb16d+9mfNpF+Myq X-Received: by 2002:a05:6a21:27a5:b0:1b1:f0d4:71b6 with SMTP id adf61e73a8af0-1b212e2cb11mr11312512637.54.1716904905746; Tue, 28 May 2024 07:01:45 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716904905; cv=pass; d=google.com; s=arc-20160816; b=SnH7dkXVVTO2E66SbooeEVUtwZ8f6eqak1xvOIaWb4JXYDxp9KZu0mVLgrpRVZ3uU1 DwmxVsELpBDiMg7oqf+5/ta3hyd3em04FoJscCLBAg+inNhZP4SrNpCEwi9Mvez2kCAq f3gv+iKG1uDo4Xjhug6M0DapwWhKFAAJ1GOlCtWzqZ6EZQik5XwLe4GpeFFia4rwh4Uz nyOjhscQRvPhzHgCq0ioXWFrHtBCxv2qsihGJ+5fkrCzCLAdcIZUDsduLwpCCoRXVkdW y3y/UBZn10VakeYzq7NTU0YDd4ddWw8IyRWsVVEPeIS3PDFWrjZftMpNCiWYFna0itK6 DXxg== 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=t+8r7YelJvdC+106kb1BYk4JxPgnGiFrL0sBYL6hoec=; fh=ovKGQAtVjTk/d+yrtY8HhWqFcBxdiS20rITf4pdsDg4=; b=YnCxQ4UUHXUzqG3jurHlaTHgjr/+cWzTnhGmXWNSndSoDoI0t8m54R50rU6uKn+D1J 8zwcQxsSgdWO96vwwnBZTwLQutSxL0KiD9Ne4m0h1NPK6iplcm9jAN2UhpgKUJlU6MgJ i2UrNjKjIG3XmKLyHwPyIgLjURn67ExQ1+AsBqdNzXuSBg2HcSpJJUyoaB18miglqkX+ ywFTHca/ztxlQWCqYSQRWlyV+rkaLpcOakSaFNnIumcCzefb29jRdYEu3MGoM3cnT7De ZbV7VEi3Ihjl+d/0SE11MhcYkqda6IFTzeBXJtZ5bWsv++jGHJr9vQAMuW39R6NdI25p gEqw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BDNBILNn; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-192042-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192042-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d2e1a72fcca58-701bc8e2a0esi1733657b3a.330.2024.05.28.07.01.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 May 2024 07:01:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-192042-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BDNBILNn; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-192042-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192042-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 48A00286511 for ; Tue, 28 May 2024 09:56:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D5C3716C843; Tue, 28 May 2024 09:55:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="BDNBILNn" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (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 437F516C438; Tue, 28 May 2024 09:55:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716890142; cv=none; b=Lc7p/Z6DPd6awkjdUf8SC5JAP3jFtW8UftBbbng5NGyzWpoxR+I2242c/v4nt/4Ldh/F4WqYGk2M9xGhO+AxT3K4rmyt4c9lsuDnojsx0LuUwwaOURan3T7n6IotDQHX5/tkSsENii0iMZC3besGNajREIsAUGXBABoICRG8rao= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716890142; c=relaxed/simple; bh=iLfKKiSk3QwOSaKGHDKAXV6IdTXtC5sh1KcXVbMH51s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UkghMroGWDeFIfuZ6WMHOLZjQGijJgshDrg0OqvaGiW/xSTsnzQPLKkswZdEVldkDL0qWoH5cyyGl7FsaERkV9GXo24EHyDNoN8JqPTda9TEgBVh6X/x7lyUozKfT1xHr+4KJyryB/lzcUPTsgnU2KQT+bfA+Qu0MB+YhuAFVV0= 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=BDNBILNn; arc=none smtp.client-ip=192.198.163.17 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=1716890141; x=1748426141; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iLfKKiSk3QwOSaKGHDKAXV6IdTXtC5sh1KcXVbMH51s=; b=BDNBILNneZrjIS05NmV93cDNcYtLatp6yALvnDIy/pKSa0LLRdlWZ9Zk +dJo/exNbsjnSg6IakAC9SU5cBeJu192A05PIHrtHSJivuJiJGl0o0rSb pfmVQULuttReTAox4oWMVzaLRieLTe29sf6TFOtNnoKzkNnucYXnfmXXs piRZnVtRCQ85oNPlh2shenEiuxJw7JXqcwgJMbhdoIFTh1JCVUy90LOrr hD5m1e0YFftvCC20aPv7Q24DP/Cro9zmTjBq4vUqNzUQ/ImZuD8IxAZZu 6bYtsgd30K5qC+Ojbk+FGh8k0HpgUilY8Nr266oKKtByv/1zg9cTQ+txz w==; X-CSE-ConnectionGUID: U+/alO+RTJ+/iTz2fXA+wQ== X-CSE-MsgGUID: 5X6iwyGNQ3iX1MtKnvPFIQ== X-IronPort-AV: E=McAfee;i="6600,9927,11085"; a="13097730" X-IronPort-AV: E=Sophos;i="6.08,194,1712646000"; d="scan'208";a="13097730" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2024 02:55:40 -0700 X-CSE-ConnectionGUID: Arl+5l2YQWS0kYGKnT0lzQ== X-CSE-MsgGUID: WV2V9toCS3iSacsINRJXEw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,194,1712646000"; d="scan'208";a="34951740" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa007.fm.intel.com with ESMTP; 28 May 2024 02:55:34 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 9F3006E4; Tue, 28 May 2024 12:55:26 +0300 (EEST) 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" , Ard Biesheuvel , Baoquan He , "H. Peter Anvin" , "Kirill A. Shutemov" , "K. Y. Srinivasan" , Haiyang Zhang , kexec@lists.infradead.org, linux-hyperv@vger.kernel.org, linux-acpi@vger.kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, Dave Hansen , Tao Liu Subject: [PATCHv11 08/19] x86/mm: Return correct level from lookup_address() if pte is none Date: Tue, 28 May 2024 12:55:11 +0300 Message-ID: <20240528095522.509667-9-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240528095522.509667-1-kirill.shutemov@linux.intel.com> References: <20240528095522.509667-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 Tested-by: Tao Liu --- arch/x86/include/asm/pgtable_types.h | 1 + arch/x86/mm/pat/set_memory.c | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/arch/x86/include/asm/pgtable_types.h b/arch/x86/include/asm/pgtable_types.h index b78644962626..2f321137736c 100644 --- a/arch/x86/include/asm/pgtable_types.h +++ b/arch/x86/include/asm/pgtable_types.h @@ -549,6 +549,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 498812f067cd..a7a7a6c6a3fb 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -662,8 +662,9 @@ 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, the level of the mapping, and the effective - * NX and RW bits of all page table levels. + * Return a pointer to the entry (or NULL if the entry does not exist), + * the level of the entry, and the effective NX and RW bits of all + * page table levels. */ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address, unsigned int *level, bool *nx, bool *rw) @@ -672,13 +673,14 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address, pud_t *pud; pmd_t *pmd; - *level = PG_LEVEL_NONE; + *level = PG_LEVEL_256T; *nx = false; *rw = true; if (pgd_none(*pgd)) return NULL; + *level = PG_LEVEL_512G; *nx |= pgd_flags(*pgd) & _PAGE_NX; *rw &= pgd_flags(*pgd) & _PAGE_RW; @@ -686,10 +688,10 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address, if (p4d_none(*p4d)) return NULL; - *level = PG_LEVEL_512G; if (p4d_leaf(*p4d) || !p4d_present(*p4d)) return (pte_t *)p4d; + *level = PG_LEVEL_1G; *nx |= p4d_flags(*p4d) & _PAGE_NX; *rw &= p4d_flags(*p4d) & _PAGE_RW; @@ -697,10 +699,10 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address, if (pud_none(*pud)) return NULL; - *level = PG_LEVEL_1G; if (pud_leaf(*pud) || !pud_present(*pud)) return (pte_t *)pud; + *level = PG_LEVEL_2M; *nx |= pud_flags(*pud) & _PAGE_NX; *rw &= pud_flags(*pud) & _PAGE_RW; @@ -708,15 +710,13 @@ pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address, if (pmd_none(*pmd)) return NULL; - *level = PG_LEVEL_2M; if (pmd_leaf(*pmd) || !pmd_present(*pmd)) return (pte_t *)pmd; + *level = PG_LEVEL_4K; *nx |= pmd_flags(*pmd) & _PAGE_NX; *rw &= pmd_flags(*pmd) & _PAGE_RW; - *level = PG_LEVEL_4K; - return pte_offset_kernel(pmd, address); } @@ -736,9 +736,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