From patchwork Mon Feb 12 10:44:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 199751 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp2419487dyd; Mon, 12 Feb 2024 05:17:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IFKqq/95H48N5NHfDTvw8p4qIsciRC70193O2ZFfK+qeagaVa1MsTst8NCJgq7WrUW0tN0c X-Received: by 2002:a2e:95d3:0:b0:2d0:b285:ffc4 with SMTP id y19-20020a2e95d3000000b002d0b285ffc4mr4058318ljh.35.1707743826538; Mon, 12 Feb 2024 05:17:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707743826; cv=pass; d=google.com; s=arc-20160816; b=0o2ubsmPmaVp/d7SXlUlbAKetQb8ZjBFnLXZdEJJ5+2HM3KrOVWekM6xsUCGxhmCRf uQmU1XEekY81+L0MEURYznZOfruhw6/sO7I9CaXXNmX/BvPssOhFuTr+/OCKGm2eUtYj ScOXpl0MamRmqqJr/Tdt1Us1hJA7BT02xHHcCwiyK8ZaDz9U2sjCdPzLSPT9+xH0boM1 7qRXKjzoGFQ7VvLuMo154fYADS+yvB1V6QgfK8rrP6wNOE9MwjSmJad+kIVKUXljP6Tw p6Cw2jj3ZChElF9lz2R6NMmBglEiTr9M3B7FE3IeXoY/yKKlPHsMCrhAXkFK1v/GIu1F ++bg== 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=tC0l902aAQNJZysp4ke9uf0VMLVFZv1kHnyDv7d3b6M=; fh=OCR/FsxB0ODprGp1qEVM6Pi2q1RU81sRoOuJ/okZgpQ=; b=e7DEV3R0ryKxIsk2xEP4yoMxtaRsKGqQvxYhp/J/DFCgk/92plitQrnG/SzSXl1Kx5 sHDBiSpD49F/H6QzA/u3UqEBJkbFX+iTjIYw/sCthtRohEcKLhVQ3NnRM7kRArJRbOkq xbDXwdKKotGlapTnh6L1WMrEI0h2XtV68Bf0ROROiBoiwWSeXsOMfg5s234/pEmCH3Jl akBHrLSNQqEO0ftF5bRUWsSv55N7XhN7eVTxEvHGwjYHCTaSp+KOnIBEyMgzqKKhLN5K s+35etF4sKTY/uTad4/BFMTI3GMyB7GS3d9EI2IjOgSzRIFlXpgVkwv1WUGrIYGYorX7 ERdA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=grjaH0Ry; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-61365-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61365-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=2; AJvYcCW701eXCC5T8vDgFiCNmWwqrA1jLbrSL3jKQgf3PL1Isxy2/TOmD6NyxAj3qYfTT5eUnk+NANvUhXLA6X+UYk8nIqG0yw== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id s10-20020a056402164a00b00560ae7e5939si2690590edx.366.2024.02.12.05.17.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 05:17:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-61365-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=grjaH0Ry; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-61365-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61365-ouuuleilei=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 2034D1F26F99 for ; Mon, 12 Feb 2024 10:50:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A19DE3D974; Mon, 12 Feb 2024 10:45:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="grjaH0Ry" 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 7D9D33CF43 for ; Mon, 12 Feb 2024 10:45:14 +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=1707734716; cv=none; b=ecwVaIbnhAYsWACKIdsZgVsVAcWXX3FigEebe/GWJrmxMPAOM/2RzqXpcac0nrfpZxpoaXauZ7a5OLnMeU+LgYdO0UzdcgNwRYlwbelz1us1zpM9eXd9tx0atVCvQtu7V2I0L4fS0RUgDV0tBQQ7OrK4r7uwS4ilhWCqcUE9KLg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707734716; c=relaxed/simple; bh=9HS0zSPwIGWp/4cXpPzLT+C1ZEn2tRFHFH5LFIKn6KI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fhnp1QJq9DP2D/sTmdG8MwLHd6Chh+eMLdWKLcCvR8dQRcka4HrwhH9cC/RKI3ib4Z+n5qaeu78+oO3ImxliIJdtKdlyUjNgqrxPy1I/hgNxaOAoW0sI7YkCA9ifcEpAcJ681fw2U7DbjdGCiHfazzMa5lixlu23IGftgQH3RYQ= 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=grjaH0Ry; 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=1707734713; x=1739270713; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9HS0zSPwIGWp/4cXpPzLT+C1ZEn2tRFHFH5LFIKn6KI=; b=grjaH0RyD7ofMArPvT39KByUMkhZPdmscywKGcJC3qYpOnVC6KOFWiyf 1WjAQP7c0VEtx5KJeLq9Ugd7Rd19/Ed1Qc0XeQcf/0vScdjrtA97S9lh+ zIWAbtP86bN6dhGxz7tRzaA9Rx/5WPYexHMKNOql5jlVYWoeJqtWbN31e RxaKnRx43HcASyB13YpnEGBCypCWt0n2rvcwajoUkACSsJxrtQd5teRVz sE5Gr61gaiod+c2TsguvJKj1ObE08TdWvvOnkeZdKz3M6Uz13naXGSBCa /S+bcv2g0Ukzmm89dbdqKrq+YTyH2xWoqES5F5yebei/pqdmN04E9nOep Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10981"; a="1585158" X-IronPort-AV: E=Sophos;i="6.05,263,1701158400"; d="scan'208";a="1585158" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Feb 2024 02:45:10 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10981"; a="935035657" X-IronPort-AV: E=Sophos;i="6.05,262,1701158400"; d="scan'208";a="935035657" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 12 Feb 2024 02:45:05 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id AF2DD782; Mon, 12 Feb 2024 12:44:53 +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" Subject: [PATCHv7 15/16] x86/mm: Introduce kernel_ident_mapping_free() Date: Mon, 12 Feb 2024 12:44:47 +0200 Message-ID: <20240212104448.2589568-16-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240212104448.2589568-1-kirill.shutemov@linux.intel.com> References: <20240212104448.2589568-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790699190601698964 X-GMAIL-MSGID: 1790699190601698964 The helper complements kernel_ident_mapping_init(): it frees the identity mapping that was previously allocated. It will be used in the error path to free a partially allocated mapping or if the mapping is no longer needed. The caller provides a struct x86_mapping_info with the free_pgd_page() callback hooked up and the pgd_t to free. Signed-off-by: Kirill A. Shutemov Acked-by: Kai Huang --- arch/x86/include/asm/init.h | 3 ++ arch/x86/mm/ident_map.c | 73 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/arch/x86/include/asm/init.h b/arch/x86/include/asm/init.h index cc9ccf61b6bd..14d72727d7ee 100644 --- a/arch/x86/include/asm/init.h +++ b/arch/x86/include/asm/init.h @@ -6,6 +6,7 @@ struct x86_mapping_info { void *(*alloc_pgt_page)(void *); /* allocate buf for page table */ + void (*free_pgt_page)(void *, void *); /* free buf for page table */ void *context; /* context for alloc_pgt_page */ unsigned long page_flag; /* page flag for PMD or PUD entry */ unsigned long offset; /* ident mapping offset */ @@ -16,4 +17,6 @@ struct x86_mapping_info { int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page, unsigned long pstart, unsigned long pend); +void kernel_ident_mapping_free(struct x86_mapping_info *info, pgd_t *pgd); + #endif /* _ASM_X86_INIT_H */ diff --git a/arch/x86/mm/ident_map.c b/arch/x86/mm/ident_map.c index 968d7005f4a7..3996af7b4abf 100644 --- a/arch/x86/mm/ident_map.c +++ b/arch/x86/mm/ident_map.c @@ -4,6 +4,79 @@ * included by both the compressed kernel and the regular kernel. */ +static void free_pte(struct x86_mapping_info *info, pmd_t *pmd) +{ + pte_t *pte = pte_offset_kernel(pmd, 0); + + info->free_pgt_page(pte, info->context); +} + +static void free_pmd(struct x86_mapping_info *info, pud_t *pud) +{ + pmd_t *pmd = pmd_offset(pud, 0); + int i; + + for (i = 0; i < PTRS_PER_PMD; i++) { + if (!pmd_present(pmd[i])) + continue; + + if (pmd_leaf(pmd[i])) + continue; + + free_pte(info, &pmd[i]); + } + + info->free_pgt_page(pmd, info->context); +} + +static void free_pud(struct x86_mapping_info *info, p4d_t *p4d) +{ + pud_t *pud = pud_offset(p4d, 0); + int i; + + for (i = 0; i < PTRS_PER_PUD; i++) { + if (!pud_present(pud[i])) + continue; + + if (pud_leaf(pud[i])) + continue; + + free_pmd(info, &pud[i]); + } + + info->free_pgt_page(pud, info->context); +} + +static void free_p4d(struct x86_mapping_info *info, pgd_t *pgd) +{ + p4d_t *p4d = p4d_offset(pgd, 0); + int i; + + for (i = 0; i < PTRS_PER_P4D; i++) { + if (!p4d_present(p4d[i])) + continue; + + free_pud(info, &p4d[i]); + } + + if (pgtable_l5_enabled()) + info->free_pgt_page(pgd, info->context); +} + +void kernel_ident_mapping_free(struct x86_mapping_info *info, pgd_t *pgd) +{ + int i; + + for (i = 0; i < PTRS_PER_PGD; i++) { + if (!pgd_present(pgd[i])) + continue; + + free_p4d(info, &pgd[i]); + } + + info->free_pgt_page(pgd, info->context); +} + static void ident_pmd_init(struct x86_mapping_info *info, pmd_t *pmd_page, unsigned long addr, unsigned long end) {