Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp2261727rbb; Tue, 27 Feb 2024 16:49:01 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVJB+BwHi9EKurtHNRzr1+sdQuJCgGqO30pRn6SUCuKIhlKWGheIqJukTmdivwyOCl3/jlF7ftZWg9uhc3om1acwUOYdmudhMEI/pvHwg== X-Google-Smtp-Source: AGHT+IHYqO5AOMaquPk97pGgbQrviyqBWiuPwZ/IAVBgyrLuHm7dCZqSQKYha9FMqzATBW1J1ljB X-Received: by 2002:a05:6a20:ce4d:b0:19e:9a59:20df with SMTP id id13-20020a056a20ce4d00b0019e9a5920dfmr4508759pzb.9.1709081340993; Tue, 27 Feb 2024 16:49:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709081340; cv=pass; d=google.com; s=arc-20160816; b=yHkR/RQnCn6Z4Y918mPdIoDMmbHXkdbSTascMm/rCdejSLrKWQyW6saD63LsnogAY3 7P5sTH53QvlJwrwNN4KyT6N2tmCe2AEUrWEd7EGm+nb0h9vR75U0MezFOmk4Epud8Wbg 5iDivcjOPxDu6smChj288tSHguJL5vyHj8zPcPeGXwB03gq75s+/r0Uzv4Db0U3LyE8j EE/Jz7RZiEjauyrDW27pzbGdlms+NVfPaQS95n7ZdmGC4abVkiy/Gh5Q6lsKtXC3z4tO KDo5gGOAgB+uT+nHAks0t+JEGjUNFE7xGqOwXq3rcexS/0a5xEj2I7XXWdqgtg2ie5VD ohWg== 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=O0AdIajMed0x+5DvNJ7ZCJxKktw59OJ5uBa8s2TLyMY=; fh=OvJRnOqsMTm9XoNmEwebcqh9Ud7yh1CTeKAP84ols98=; b=RQTbblLpXdrxIHK7lI1In5YPCv/Ua64bJ4wOlcEK08nMTr+0pLtDq/BfJhInHGi7Ph CPOvlxK6Pj2j6tyTOIGo895s7/o8uYn2+HJ2iM9+rbnJce+iInjYXWPT/d04lQIx049a 1BmehceNqCgSlJUT+jgLWN6Yzm3SJ6Aa/MdblsfHq2Di6PXC48f8xJR7Po70Z5UY1wQh 8NCVcGu/9nPsrKIYzkZ3Bs1Skd8o+uQxzbExC1qtUhFs6LSx401VHgG/0Zz7Hgnm5iyh lKGqCrvkNQ4pPZdy1TDvEBGEOS8QdalvzopWxtHd8YTm9Sa4CJ9r6DveGp9oZ6L079UC mD+Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=d5htavVf; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-84048-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84048-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. [147.75.48.161]) by mx.google.com with ESMTPS id ba9-20020a170902720900b001dc91f97b58si2135074plb.652.2024.02.27.16.49.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 16:49:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84048-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=d5htavVf; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-84048-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84048-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 2616AB2AE23 for ; Tue, 27 Feb 2024 21:28:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ED1DB15B0F6; Tue, 27 Feb 2024 21:25:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="d5htavVf" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (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 EDB4615A4A2 for ; Tue, 27 Feb 2024 21:25:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709069121; cv=none; b=aanqDoNhTkSfkg4Zf3Ry0OzbNp0LCNzDNAgftAifmg+FLNDu7SxEV/zOBPIfaIPYc55aJO3r35+Y/XijGTkRVisDPHY0pENlpUYFa2OpFd7Xu2Efw5UXnY/ztLbnSXjTJ6EY9v7DFlD1iOqfP+Wqo92VVlq2AMCqxu0KST9+who= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709069121; c=relaxed/simple; bh=wfElhcuLUzOIt/rpQx3fYg+5YYi1jjhrzzGUyBH3dyE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Y3i5po2a9QeZjwF2ubkFk1sbj69JxWbW1JJf5OJ2aGPetGAINYH4F/p7bWwGh5CEPs9DbrEirwE6Sb5P2grQikMZm+FEBBwLwcQrJxpSKHFEohDqxX/N01p6Dx1/vqPK+0BL8Y66WBu759dC0hr3+qm0oy/Dc7YtjMzmQvG/IJ8= 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=d5htavVf; arc=none smtp.client-ip=198.175.65.16 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=1709069120; x=1740605120; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wfElhcuLUzOIt/rpQx3fYg+5YYi1jjhrzzGUyBH3dyE=; b=d5htavVfh4u7Y2SzqMnvLs0NzMF/pR9hIVZlnC0PGCYAa4mocgYHxp2V GikQuwSvozU+ElU86D98dUue8+gdDKEDenbDdgWooEqt5J04lpJiBvi1C Dk6M+Psn4BxuyESr+sRWqzJDEe7je30bT6C2YknpfkRQWxmg+BbwpZTu3 hzQ6LhtrnyEybh42gTZi662hvgnGIY2czfRbVHUS2zo3Rk12OYo+wRqtf sl4QDKp9pdn8c1OWCyB5bBNWcPnIx9qotgdSz2ugwY02SCnYJuYsPEtgG GGyEjDUHu2QMow9Q6dfxN7jMIT9kMlrKwqEoa8RWvkHbKYbI+D554Cmq6 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="3567108" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="3567108" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2024 13:25:14 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="937032931" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="937032931" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 27 Feb 2024 13:25:08 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id AAD311063; Tue, 27 Feb 2024 23:24:56 +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: [PATCHv8 15/17] x86/mm: Introduce kernel_ident_mapping_free() Date: Tue, 27 Feb 2024 23:24:50 +0200 Message-ID: <20240227212452.3228893-16-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240227212452.3228893-1-kirill.shutemov@linux.intel.com> References: <20240227212452.3228893-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 f50cc210a981..bfc6611fd98d 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