Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2375850rdb; Mon, 12 Feb 2024 02:50:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IHflxvmW4JExymX+IzEmQpjhYAp3IbhzqVJuPLxer1nItLidleTKH8SPJjdJ3t2nSR0bVLu X-Received: by 2002:a17:906:cb89:b0:a38:259c:1334 with SMTP id mf9-20020a170906cb8900b00a38259c1334mr4350192ejb.36.1707735015528; Mon, 12 Feb 2024 02:50:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707735015; cv=pass; d=google.com; s=arc-20160816; b=GahPJK4ztJhioNaApUYBe8e3DNj3XHzD7a0Mq665V2nV8e3VFf2tMHygrRo/5zl7DA sCrIX9aiKA/PF4+vKB55kv93Z0YK5axBwkhQ4jDsBKkPorlcXQKkX/MEWjDEAffRtfqd hUVZ4aFbb8rtvY+3dEZi/JNOAzn6n2HjjHDG+6w3rmzui9/zsIah5I74PKPxS8u4TlZA 9PdMsdfdhyULFL05WwOJu/7B8M/sBjRyRXXAjrsM3QifW9Fzr18GGKe2OKbLB3BU5Udl RAYqJgjFz04Bn40bad4x8sq6UJiSyp3GdkaIba3YYrbTfDG6iyCMHH9deEx7vKfVFudt CTvw== 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=ejBE1y57yksm9cana/wOvUi9dhLHM2uW5y6KtGgJslM=; b=RHAis74SIW23b5A0XW++oyUC86zHq6+hrAfF0G8NmrwCR+yV6l0Aur8qySIsDApCeU 7n3ACOZoyCEfMeB1SsVlhi6E5w1/fopG6Rkc4CdbljlOrBLWA6hf+1kYNZQU69iYmyTZ b/XHObEVbaCpcaQ6J4+rpNrNG5enNSILLgnCgwImkMj8yKuvT3O8rH9JnnkX1YgSOANG 3fUzjRne2QULqQH4soYun5cNe3LA9HIFkOkg+wUjmPMbYKlnySHDADg2G6cdORKvv+Zo PJgFZlJ8otM/SkVB50mNGLSloSYDRRE9XehNpt9s+49USGg6qiMxQjgW7t5E5MQC7gS4 hxlw==; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61365-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=2; AJvYcCV8gzJ/zv5Eht+Ygn9IT27YKQniT0zUiLU272w5C2UzJaCF1ZTWR5UEUqMiNT3GuEhmz6hP5RQV70dw4ovKlxkBksNjs9Nq+T6S+3Cb3A== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id j14-20020a170906410e00b00a37f77efc4fsi66725ejk.790.2024.02.12.02.50.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 02:50:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-61365-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=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-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61365-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 32D071F269E5 for ; Mon, 12 Feb 2024 10:50:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 42E133D3BD; Mon, 12 Feb 2024 10:45:17 +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 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 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) { -- 2.43.0