Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp2532781lqb; Tue, 28 May 2024 02:59:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWPwWuGDj81muTvcn/ota0+DGG9NZXLkLWwLL81tp6OyB8iRqVpu3/2GoPEyom9eef3rpAbz640SfhsF6Y+NfRBePKMKJrLtMSUmZwNOg== X-Google-Smtp-Source: AGHT+IGPukjvfIP4bJCR+fAxVjTnDn9hcznA60w5NzzVBeN0icMgPiNYArHhyyMEzOk4vBiS6vjA X-Received: by 2002:aca:2812:0:b0:3c5:f62a:435 with SMTP id 5614622812f47-3d1a7b3087dmr11137311b6e.48.1716890349693; Tue, 28 May 2024 02:59:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716890349; cv=pass; d=google.com; s=arc-20160816; b=AFp5siMGDvwT8rcUlSY7dGs/kKji7zuodsZEp/tk3GnXHB9MsTLQ9hRzbprKQFjqvT tV982iLPw7qj1n8FblVLzBRx0d+jdN9Fd9wdfbtZgnCRET4XyA4qPZ7EBNNindhFzl5v NGkSKGFOl1bTU9t7KAOFebZs5tRrSyS7G1J5GUgzb8iGt34m5j5Bs21D+KgWt0s3Me93 veZvX4sW701OFpng92b3eDtJvp13ZHsj0plUMDEPgxWkk+9O1k16fKmEBcag/DlLtih0 22No5thBQkMsT2v/sWKm94NUnNjxQ6cvLulzXIAOBh0Mx8WSHDTrxT/oc2zKtvPsLSPX 1T+A== 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=m3/D2EhARcQzdq0OWrerN7jF+DG54QooZAFqErvCBN8=; fh=jKzY+IJTxktZ685R93VhJUbGF0rvTl3iTwtLazyIugI=; b=iOsCbUqWPG579mgG4uHN6EzSLaiHqyx/QSNRhKcBu8ohmwuJxo6C6it5oYodMFi3ek 7yfdNs3adQDs0QHZ5RNtGM5dqKG14cbE+DH0MGkVaPACxIv/I7bsJGPhQa4gTO62gTeK N1R/itJo4NkZXvdXujWgIwavh9nZca7etT83O1sNriu3KDzMjFUoZ5kNByCGpUnBK10j GH2IC1u7O4hogHQCo9QhdIm5uXSFbgreUQ9LeiMaHBlsu9rMZKSJGs/BBaABklmrdsU5 bO0tE8JH9V3e7G7HBM64M24lq9j9AfAHbb+XjPKCDVgNLgPsACcevGMUm2XqFbbY0UQK 3ANw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YcNQH5b1; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-192054-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192054-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 6a1803df08f44-6ad717151d8si69087616d6.114.2024.05.28.02.59.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 May 2024 02:59:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-192054-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YcNQH5b1; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-192054-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192054-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 5247B1C23DAB for ; Tue, 28 May 2024 09:59:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EEDC816EBF2; Tue, 28 May 2024 09:55:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="YcNQH5b1" 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 0125F16DEA4; Tue, 28 May 2024 09:55:55 +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=1716890157; cv=none; b=lfP2vd67drtcfNaL//M2vABfErk9ZsAU40IenStL/LLWzAiDEKCGVOQuoOrsHqWfmRlqBuU2p5nob9bX8ySVxDwm2bebCxdvkrUVEB6SvgJRe4RmD2YJLHQOYNP0mv/6/aaBCItNDlbXN68mEeAw36NlRkfRSztU8xcuDz3niDY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716890157; c=relaxed/simple; bh=GSg67xlVUPvLYDL37C9OUEs7HbwzTLSXgJBxYPbO+3w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KwS2qTYmoK5ghk7ed1UzcqSBJU4fBSu+TNIuNSVw3o1slrWiEqfS60lBOySdmZUXQsEQArEdN/zH/l+9D1GpB/JZT9nM9BkyPY4OKmhrUSCeViSSddcj5QoAJuldnrZVgGrKzdKhwjWy7huLZ6gdB8nuXX/77xdJmyQVSpbEtOw= 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=YcNQH5b1; 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=1716890156; x=1748426156; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GSg67xlVUPvLYDL37C9OUEs7HbwzTLSXgJBxYPbO+3w=; b=YcNQH5b13IzDn2D9iXHwmPGc0gzi+S+bRxdnXyItmAv/bfi93acFyHRn GZKQI632aIrdgA8X+46fxG50zimL7gkMfQzqbjoMhb2GOBSOho8yNrtOX Fi/8l3WIE+0VjYvCcxZuawF0AsVxVATeU38Uunp4mr33th+KtozbZqn5W Jkju4bKDX4eVtrKOhl/dh/8lJugHAlCt7t0kjZhX2KQcJLnnpO/g/6JYZ 9YBgcAlwZcdZsQ/eTOVoVGCPWEHL3OkxjxJ7tZakQ1L7RlYWPcYQbyhf0 mYZwGrAo3ItV5CkOV6QAU/SeMtXNv/MO7y8KiO0vPmI5zNLMgZeqgo40R w==; X-CSE-ConnectionGUID: fLtQ6RVySESfhQj8o9GbYg== X-CSE-MsgGUID: KWjFtPClSZq1/kpug11z0A== X-IronPort-AV: E=McAfee;i="6600,9927,11085"; a="13097859" X-IronPort-AV: E=Sophos;i="6.08,194,1712646000"; d="scan'208";a="13097859" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2024 02:55:47 -0700 X-CSE-ConnectionGUID: 7nAdNUDpRICNt8kaeimCAA== X-CSE-MsgGUID: muYZWZLMQm6HxxgLAl+xvw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,194,1712646000"; d="scan'208";a="34951830" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa007.fm.intel.com with ESMTP; 28 May 2024 02:55:41 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 25EA5A5C; Tue, 28 May 2024 12:55:27 +0300 (EEST) 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" , Ard Biesheuvel , Baoquan He , "H. Peter Anvin" , "Kirill A. Shutemov" , "K. Y. Srinivasan" , Haiyang Zhang , kexec@lists.infradead.org, linux-hyperv@vger.kernel.org, linux-acpi@vger.kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, Tao Liu Subject: [PATCHv11 17/19] x86/mm: Introduce kernel_ident_mapping_free() Date: Tue, 28 May 2024 12:55:20 +0300 Message-ID: <20240528095522.509667-18-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240528095522.509667-1-kirill.shutemov@linux.intel.com> References: <20240528095522.509667-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 Tested-by: Tao Liu --- 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