From patchwork Tue Feb 27 18:16:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 207446 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2973858dyb; Tue, 27 Feb 2024 13:25:35 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUZjP5eozlm0K1nTlSOjOiM3WAMHWlBnD/cTAdLqJaUldOha96MeaBCie+v/NvH/RzmbAyQ18Tj8Ltal7j/1PeyKCsGNA== X-Google-Smtp-Source: AGHT+IGJ3jxgbXfGftqgGIKEPtkg7QnZmpveIzSgfSD/LOv4cXfjwoP2C1qMUzDEzveA6w+k2BPz X-Received: by 2002:a05:6358:939e:b0:17b:c624:b0a1 with SMTP id h30-20020a056358939e00b0017bc624b0a1mr2711690rwb.23.1709069135002; Tue, 27 Feb 2024 13:25:35 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709069134; cv=pass; d=google.com; s=arc-20160816; b=oIeVnoPf6/7QwOiVpvfDEQEhWr9nq7Mje55+PEzOEK7nYljPAGLCzencZQ14QmJ6LO 5JyvnnEUDoODuEvDpoAs40cJCbD6jkaaetKV/KgR9DyoQSx8X/d/8x0vLHJ6kCmzGpiF ZfZOgJu10TTVQ9tgiz2aPgQQGRCqpVzYG5XDS2s42K33QcgKTzW7r23MphIkCnv4IRwJ 23Twer8Im+J79SI6IxSwEoT9BSs5zRzoGC/EjrVZ5518juK4bDGq8RQsfj/NtzsyW8aB jqZ/xXsPlQyqwupBLWwyses6C0h/a4ZMKLqrahbz99ZgdzhqL8VEZlTD/XTAUqmTTu+U CZyQ== 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83823-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id iw14-20020a0562140f2e00b006900841048asi5775793qvb.282.2024.02.27.13.25.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 13:25:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-83823-ouuuleilei=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=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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83823-ouuuleilei=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 BB10F1C2734A for ; Tue, 27 Feb 2024 18:20:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3A314156963; Tue, 27 Feb 2024 18:17:14 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792088877174244907 X-GMAIL-MSGID: 1792088877174244907 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) {