Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp2210579rbb; Tue, 27 Feb 2024 14:35:37 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUWK7GPeE/VJipPi3/ZrfLrBWqxPT8qZHAOxZD890Z1UuICittfKmqiXcij/JiyhNG3dKJkHaCKOtRnSHmub7vpkFlDVU8GUU+uuw1qBw== X-Google-Smtp-Source: AGHT+IEiRFXrV3d6gdGNnuvLAKi2JAmlKeCaXuyXsnKa03A2BZX5/jKEzEiBHz6kyYfbZHTXnnCV X-Received: by 2002:a17:902:db09:b0:1dc:c8b8:3c9b with SMTP id m9-20020a170902db0900b001dcc8b83c9bmr265353plx.12.1709073337042; Tue, 27 Feb 2024 14:35:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709073337; cv=pass; d=google.com; s=arc-20160816; b=r261Reb7W98xs9tgsNi0NeZEk5VgkInaXc3eF9inMcRSqwqM5gYGG3lNQ5rR9uM7TY AkdzrKXlWYEnio4+cHJbt3CZ2K6FgX/aFP9uQlJp8CGKD1QLKr/c4gf7y4vgW4LMXhOZ auiQzqWwfDD8y+kq5AVs6OHHhuwawBLd78sP5dd6Zgt3hlZgQCkcUldmYmzHovzXqFSt CKokEblrPDyekN7/hm9827bkRl319HqWTxnM6FILnLOS6oFpm4q4JszwnjAx5I08wVvi KIYSfYjQal4disLY9viO4RXbr3FSMR7+YPnxVjgmW+dG7h9GkE0ThCnp3zMCZlWB4jE5 S+LA== 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=OvJRnOqsMTm9XoNmEwebcqh9Ud7yh1CTeKAP84ols98=; b=MTM6W9RzSsCEapEasyuH/np5wo5F9Y04AxhWmBeJKle3BNkGBRSAIbW8NNOItcOgqG OtlSLztPOica2xJTKpkepf/wCxr9jZPRIiSSBH2/fpN/QR8OF8mSp0U6/cMto+JkosZs 5nvPNtp6Q33xxkjU/osaervDfQJ6CKBvF1HMrN5s21xvRtpS9OZQUe2m61CqNfDT+gzR DdQpGIcVxsYh1XnACzn3xD6gaqarCrI+1VpoeYA0Qdrf4LgiXWHv/sG9ViXZCgTTlSer WaydEPBShqFg44FJjhQRpmhP+P86hpUK2+GQyTAglIGzaX+iuacqZqM2knsaa17SmDbv gKew==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=k8VPUy6J; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-83823-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83823-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id z18-20020a170902ccd200b001dbe7a31ab9si2087907ple.315.2024.02.27.14.35.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 14:35:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-83823-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=k8VPUy6J; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-83823-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83823-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 9BDCB28F11A for ; Tue, 27 Feb 2024 18:19:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 92CFC155A45; Tue, 27 Feb 2024 18:17:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="k8VPUy6J" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (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 025C214F99E for ; Tue, 27 Feb 2024 18:17:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.8 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709057829; cv=none; b=OfOFIX0/90EeqlnTAWNXs/F2zUtnLfASMKdi/gNWL30uQ9/7SvA0Dy4hmDiE+LKmdHbGAYoDGUQLngjsqFfbLB5YLJrqQ7Ulk8zU1Sc+UCjEB5u5kvpri0nGLe76NIG6wHKR6JqfB1JJ9l+eE9WYopa29ZlNnJ3bs5Aco/l6g2I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709057829; c=relaxed/simple; bh=9HS0zSPwIGWp/4cXpPzLT+C1ZEn2tRFHFH5LFIKn6KI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OD9FwAdijdbi7uOGtooxoQhv3rziCg8zs8NdXSUki1HAvaTp6nPMSSnew4lQvQ34A3pXqrp7AwVl4M27ebOuyMjlr5CRC09NQc/LIGVDfgc6YIVRE8aaRmE5L4pq1NWXRAzXdsiHG/TetxYWAYQB+cvXTUu1GZfp7+IbIPAKLA0= 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=k8VPUy6J; arc=none smtp.client-ip=192.198.163.8 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=1709057827; x=1740593827; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9HS0zSPwIGWp/4cXpPzLT+C1ZEn2tRFHFH5LFIKn6KI=; b=k8VPUy6J3tjI96DGHLF8XB3JsMxyRwpMcKH8WCtAgvn8VgiiUKVpippr 3ZzfRE4znzb9h354SeYoWNdkfARJcejVqqO81+R2UDwQKaiZiPu8Go0Sf pbKGZKsqS8ROZ0OfO/luknJNpShxClK1cqrkrmHXjLh48C1qLXt/tigTK hWVk7R4v5K+oo4uQijscINlBiS/yOqSK21mD37k8/N9QN5ZKoK4Du1C3Y DxhfkpBiuVMvutb98zZ/2+KB2QP+uYKEVKEjQJsjZeVvCwt/wIfG3xFCk /moDXZG/QAdFRS1bYwE/51pnrufnMPGu31bON/U1uzE1LhDYDT8Y/FP14 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="20966210" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="20966210" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2024 10:16:36 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="937032747" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="937032747" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 27 Feb 2024 10:16:31 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id 0507CAE0; Tue, 27 Feb 2024 20:16:20 +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/16] x86/mm: Introduce kernel_ident_mapping_free() Date: Tue, 27 Feb 2024 20:16:15 +0200 Message-ID: <20240227181616.3032032-16-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240227181616.3032032-1-kirill.shutemov@linux.intel.com> References: <20240227181616.3032032-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