Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp1248232rdb; Wed, 24 Jan 2024 09:04:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IHddEvUB5MYSdG/5iKZ9UbjeNoJA4Id+eyYfRMvjgrvGTgitIs0Ya9W4V8PZsyPk29mMAmD X-Received: by 2002:a17:902:ef89:b0:1d3:f795:fb22 with SMTP id iz9-20020a170902ef8900b001d3f795fb22mr798318plb.62.1706115899308; Wed, 24 Jan 2024 09:04:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706115899; cv=pass; d=google.com; s=arc-20160816; b=A3Pn9llQAsDoNKo7/HK3HaoZTRZcVQP+HqYBsSFtZhgr//bwxcCPQhxeFUvyd7L4qx nuRKwAEjB1Kobr62td4yRUx2LYBR0rbUh1H4hYOcjqDbtRvS8Vl4Bf4RIP4jgTlmJjVo wvFr3liSM9yjtVKsf7j0BGK/qORCPn7yib4B18YmGpe/DKYTX+518FwotnuEoLlclVQn FL1uf1zlXH4QFO3//CPNMCTiMs7ti9I2kJ8q+XprQCVHmFf1jlTx00NFPMsK2VRPNJgC Obqdmg/U1BGBb05waPTI75u0wmPeexYqtHVaMOiGtbU74ITv/xIYDSB1floDxYehNJpa YyLQ== 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37037-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id h9-20020a170902748900b001d76ae4c939si3529215pll.62.2024.01.24.09.04.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 09:04:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-37037-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37037-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 58D71B2C84D for ; Wed, 24 Jan 2024 13:00:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6FF2C77F20; Wed, 24 Jan 2024 12:57:47 +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 Content-Transfer-Encoding: 8bit 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 --- 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) { -- 2.43.0