From patchwork Wed Jan 24 12:55:56 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: 191623 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp1063083dyi; Wed, 24 Jan 2024 07:27:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IFtI28DWG41F+lwxB0KN0D9HrxudOt/s0jNO16Ze/EMeyyW0yZQj90d0+NkgUoanD85Et8Y X-Received: by 2002:aa7:cf90:0:b0:55c:8251:1884 with SMTP id z16-20020aa7cf90000000b0055c82511884mr1570185edx.11.1706110024717; Wed, 24 Jan 2024 07:27:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706110024; cv=pass; d=google.com; s=arc-20160816; b=lVrMaXpXR0MkVU8YcYqJq2YKwaQQa5k8X+xVhI7YjaKClxxGtPDLr6jk9g+Igjp5Q1 iRJVrbxKq0K7AAJzG3dQzuQOfwekYXklwHXbqIBgFpJeTDddHkNDdpzXDPejnbe0vy5H xnwWg80h3YpbNCKKeoP+u9huQXKnOH1er+pNKNRWUd18y4/qxs8kIhiZRgaZgqOzcKFb JWmT6qOHjhSH55kd/btF0O0Olo96gX212GMH1KqgtgNuQXY19crhhROGbumtlRciMZ9w cemZoRikYXoLuCuuDfMeQ2h+B9rx+L4C3TeUzUpw9jIPjiTxIAHA+9GMJk1V838N4AOP Lg/g== 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=IWxwcVmxExMk4SNiPrHHmBcdK1CL6A6dH5sbeMa1Py0=; fh=OvJRnOqsMTm9XoNmEwebcqh9Ud7yh1CTeKAP84ols98=; b=OIdlIAxShathqg9AONgc2Yauf9u5/6tDfYgZ8Q9dmc0W3GpWGmJTPFEQtCW9kDhxUX iPqdcqgjNiIADa1HX5/RKO/rTXbvA+NdoOPQViQlj+xHud/+oP3dIK96Ft095uJ4pfV2 RuKjqBKXJ2I1kUiGhYjLODWuqr+ekvFMTLAxpups1TAM/1UgLJwJI3cFj1mp9czauRk8 0V4vgTCKPGAUEQb6MGGa6YxQHE1vzn34Mw8L603YZFnyIzU8TxeSAE479VKGpnRJTmvB 2in6sFzTYPDkcUjc5+SZOWmJ4q86D7ho6jjLnL6rOF5yQIc4KNK+xZBqehi5/j6DwlwC 8urw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nvZo7bBm; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-37037-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37037-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id ew6-20020a056402538600b0055a63faf522si6181040edb.3.2024.01.24.07.27.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 07:27:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-37037-ouuuleilei=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=nvZo7bBm; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-37037-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37037-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 325651F2A2AE for ; Wed, 24 Jan 2024 13:00:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5E14D77F3C; Wed, 24 Jan 2024 12:57:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="nvZo7bBm" Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.93]) (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 49E2A7764B for ; Wed, 24 Jan 2024 12:57:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.55.52.93 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706101066; cv=none; b=LQDKA0ph1GbL3dzHtmqg6JxTWQmxX0XLiNyFGTJaznSbccpg8mDF7nwZe+ITg1aH/Y7l1A3bMf+NnoCk+RM3S/mKSsGRtWF1ALPEZH3QXzbsepd4pLvp2W0c8/uZ5bcZFZZEMlI3omigZjRgy8rBReXHfoTl3CiZ2LrKcbNHVwQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706101066; c=relaxed/simple; bh=M5FU8ERT1UOGSpIjXvNR+/fCsjlJAuzf0kiANygS8PU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=o0XvCKH0A8YCSeKFuBtxu5yxCkXXoQB1Ecw/rV7RJYDX1OfZDzNFbDUgMMAyK95plI1GSP35qCvoijGnHLlhGgUsFXQ1sOd4e3A82KHQMIkLxrSYybjNPpUQxsLaQpzDIfBcVJ8kKLE3givXg1xbzLrUmRscum6xu9oyyvEeHtM= 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=nvZo7bBm; arc=none smtp.client-ip=192.55.52.93 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=1706101065; x=1737637065; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=M5FU8ERT1UOGSpIjXvNR+/fCsjlJAuzf0kiANygS8PU=; b=nvZo7bBmDP8mc1UwknvxsACgOoaR41HctvJyNrHZAPONbdWmKEGcx/4q v0w7FCGtWJPvKprQ44zAuf6aXhGwHZnV1pXRSigeN6XuxIXkLkT7BJ6ll B0L3A6tXVEkGJKuPGFHQE4vU4pj97kZPez90XxSWKPYQsvxSK0BP36U1Q cvPMakUDabidLPhqW3jcd/URi4TkhKKUBrT7NRz+uUkDqGvVCjQ55MhAM 7tq6RkjgSYzcq+FqBpCNOAmj1acEp8SWp0v5js3M9lTfg5zzu912W2NCw hIypBZKWO5ObokN1rlPpU7kc6mo9QGp+E7PC8VJpPbVs5AGoFB7NTgR+N Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="398990896" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="398990896" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jan 2024 04:57:43 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="876685871" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="876685871" Received: from black.fi.intel.com ([10.237.72.28]) by FMSMGA003.fm.intel.com with ESMTP; 24 Jan 2024 04:57:38 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id D3FCCB01; Wed, 24 Jan 2024 14:56:02 +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: [PATCHv6 15/16] x86/mm: Introduce kernel_ident_mapping_free() Date: Wed, 24 Jan 2024 14:55:56 +0200 Message-ID: <20240124125557.493675-16-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240124125557.493675-1-kirill.shutemov@linux.intel.com> References: <20240124125557.493675-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: 1788986025159093757 X-GMAIL-MSGID: 1788986025159093757 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) {