Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1913198lqe; Tue, 9 Apr 2024 04:33:54 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCURC19Ms7RMKUBSD4gwoqbkCehZhn2J0JFZP/9/o2Yd3RC5HBtODV9ZO/sJo6aerrtckuy6EDGkBecAgdOYldBmu80rk417iCskzFiiEw== X-Google-Smtp-Source: AGHT+IFY7lu63hAzhCzJOUW0VHSZCpcJDmnM8d9Vp8YScccFt3PliDaQeO+HJE5AsM+pl2wJ8JW5 X-Received: by 2002:ac8:5fd0:0:b0:434:a4ff:5629 with SMTP id k16-20020ac85fd0000000b00434a4ff5629mr6300267qta.43.1712662434375; Tue, 09 Apr 2024 04:33:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712662434; cv=pass; d=google.com; s=arc-20160816; b=ymXvFME5euhkNJ8AfQ+SF5a+yCcFxDgvIM9+Wmqsuy8zL4DMxd5b1/VMyx9T4vAb1x ESIUpgBx9Ly3R4vXiSAcwibAaWGYjp1r42OcPCF5oRs9VOQ9MOw0pZYDXAf/0FbVBumU IfRfjcM5MGy8qqCfeMHtmfbEVNLINyVI6bJ00w2r9/48qIDJuqmWmeb48HdX5C9ZS4DJ FRIT3hMkb+lM1oz1DvbUCdfrOAjVNUlSY6VSE9NLyZPX8LCR/Lczi9Bmhvd0uMPxqLDi 1jYWxv+JKxXCDRVw7c4PTkrjDxUo+fM2NPu6Ir08rEf8COnU+nuWM5E/85nTPq7B1R/s WqOw== 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=Yw2S7s1ZcF7+jfnU9Zy13ghDfPt/cQWqfRpyK6FQAJQ=; b=A/YpXSvp1I7PpHxjSqZeYmwu5GlnMBKqsmoK6moKMlHfxIYS0yKhHDA0H7VUpuVJ9a E/u1GpZW7cbduGOhzCFzTStvPe1xNt8I77y2x57hQMK98pZMAo1en/MnNznp3GFWneXZ 0Z0gyH2oOIaq5uLF26ZzloF4ZL5YbR+nN3LgJFzeARxi9BS5u8wvdDJSCBC4KlgMZm35 1spOm05e8zaOQ5geNsp8p8+bQu1HXUN5G767+6JFJTUmp1m4KVzElkM+DUCgrXlChTKD p6ORdnFE8rAwfOa8uCLaTrPT23qUiQDQt8DDzNp7P+CDRzm5Q2k+1OZ9QIX8bGwluOUT Pexw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MEFvKD+H; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-136764-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-136764-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. [147.75.199.223]) by mx.google.com with ESMTPS id b8-20020a05622a020800b004347cd2f31fsi6359417qtx.74.2024.04.09.04.33.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 04:33:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-136764-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MEFvKD+H; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-136764-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-136764-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 0FF1B1C24283 for ; Tue, 9 Apr 2024 11:33:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7BE91128387; Tue, 9 Apr 2024 11:30:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="MEFvKD+H" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (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 B1F9B12F5BD for ; Tue, 9 Apr 2024 11:30:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712662238; cv=none; b=ZTRyDFEshH7a+qeE+SZochpHa8Vjt9rlwDVboUyTW9WrAqUEVU43aex8zVq8RSJTwp2sFspgjb74azad9i0loPEu4NCV2vWtpL8H14viiY7skN80Zlxs38Gr7zDQOfQiyhjSU5Zvcz810UwZwDIHkyN60DJPaTTlR0xPnlGeYlA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712662238; c=relaxed/simple; bh=GSg67xlVUPvLYDL37C9OUEs7HbwzTLSXgJBxYPbO+3w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FckhnIsTyAGS7rIoL6XPtpTmEZGiLSeyN9fcX8lvmNDXbFORM8ctfPAxKbBr/eOyJgtvM61naThKIG3Ws+zq7nDF3kel4k8wZUCIxBLqVpCobGYnD8Y/695Thz67TzGdEKIW2kEBuh1jz2fO2u8GFzT2btTf3GNubYJtBWoVEf0= 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=MEFvKD+H; arc=none smtp.client-ip=198.175.65.9 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=1712662236; x=1744198236; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GSg67xlVUPvLYDL37C9OUEs7HbwzTLSXgJBxYPbO+3w=; b=MEFvKD+HMjoilF+TGSle7uFT2p78kDaJIUjZzOWgiXgEZMRQzRZcMS3S Yg1fhObxRIcgBSS6dUIOjqf1FKpD9EXg8mG8ly0IdbRVU3899swKiWkbj ymwXjvKRS61zhrZPZ6/xc5J4Ps9u8dnRDej/VMNVpcHbU0Kw9jg5BcVY1 LZCHnOiPnpwGM1DF2CMNPuCoIflThbLqAc4iyiYBvTvBAZpX7LYbNyLjv tQ74aSThBqfQCAwmtbalPmxaK0GoEjjlkhUo5mgIOFH+EpQASpX+6v5TS 1sgaCMHiz0i9BXcbFZXXPYhnXx2gyYBJWWQAW/vcwf4TPGc1S5W4BWYE4 Q==; X-CSE-ConnectionGUID: iynFaZHERYudGfOa06T9Og== X-CSE-MsgGUID: esFIDl6QSK2pNriJ+8StVQ== X-IronPort-AV: E=McAfee;i="6600,9927,11038"; a="30460472" X-IronPort-AV: E=Sophos;i="6.07,189,1708416000"; d="scan'208";a="30460472" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Apr 2024 04:30:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,11038"; a="937093353" X-IronPort-AV: E=Sophos;i="6.07,189,1708416000"; d="scan'208";a="937093353" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 09 Apr 2024 04:30:28 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 43614F46; Tue, 9 Apr 2024 14:30:18 +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" , Baoquan He , kexec@lists.infradead.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" , Tao Liu Subject: [PATCHv10 16/18] x86/mm: Introduce kernel_ident_mapping_free() Date: Tue, 9 Apr 2024 14:30:08 +0300 Message-ID: <20240409113010.465412-17-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240409113010.465412-1-kirill.shutemov@linux.intel.com> References: <20240409113010.465412-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