From patchwork Thu May 18 23:14:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 13247510 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B19D7C7EE29 for ; Thu, 18 May 2023 23:15:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47951900007; Thu, 18 May 2023 19:15:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DB8890000B; Thu, 18 May 2023 19:15:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 20BDE900007; Thu, 18 May 2023 19:15:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id DCAF990000C for ; Thu, 18 May 2023 19:15:03 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B7BDB1C7544 for ; Thu, 18 May 2023 23:15:03 +0000 (UTC) X-FDA: 80804933286.05.A753821 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf05.hostedemail.com (Postfix) with ESMTP id 8CEA510000B for ; Thu, 18 May 2023 23:15:01 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ncml1ons; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf05.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.20) smtp.mailfrom=kirill.shutemov@linux.intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684451701; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+Z277xQyzJUVUug62MeijuIovWvMuEeqBKXde9HZ9Ik=; b=kAd54em/Q1D5ScEI50MkkmEMSpmKipAUy/EmTnF/fmKutm4kLSo5RlH1QH0aIbcET5z4/N fRnGSq25cQ6BkPW4jduVGXIdYTQevzT82eRTt9+pBar3QIGiZ4wYYIFFjSjsNlcCuTsB+U JIxJWzv5jsdseerPELlV37ibRXu8710= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ncml1ons; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf05.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.20) smtp.mailfrom=kirill.shutemov@linux.intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684451701; a=rsa-sha256; cv=none; b=749gUj7iwle+i88Q3G1eLM1Y4RVZAwBJgOkvLX/eHEeyfnuxYT3NKdpPLwOfzml5ENf+Qb YgH7UPFAOzRpHzieE/nYg5D4XxTBkVC43ZRoopR8khofbAVCCxzeFhJZqhVVUIOovSMx2o GL843e/ZUOzkwegWIRZLn9sqsc3gwwY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684451701; x=1715987701; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rBInWBXCqa9zaNha3sJK2PKA62i2doD1PS1LbLbl/Do=; b=ncml1onsq0eoLqlxNu2PUipL4qVOKJZZ1ONvRsekITSRJSZ6pFLpp29e pVmHVpm9gJ5sxkuTXj5qmQNZqTfe55kQMm5FzT18IBukKNyzGGDpaezJt tyqPFfhlRAibmBuJWzcmgfK6F1wYq4NONltlLYTEcFKFLMhpU/hWow2vQ VDuMZvgWgpQgG4/n89fDcKP7nkTtuaiBxgKf2uXxMLksCvtn1YSs9rIMI A2LYbu4dHT0jklAHJYszOXBnLnEfPjtTt7iySZZtX28l+c+lyP+KGVapG k+lm2gHAtmdgkyXlIXahZK04O5UI0q7ta/HJSd7szp9MGxLf1Xwp1sfdj g==; X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="341652114" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="341652114" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2023 16:14:59 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="772065101" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="772065101" Received: from rkiyama-mobl1.amr.corp.intel.com (HELO box.shutemov.name) ([10.251.222.16]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2023 16:14:52 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 4B24010DFD0; Fri, 19 May 2023 02:14:40 +0300 (+03) From: "Kirill A. Shutemov" To: Borislav Petkov , Andy Lutomirski , Dave Hansen , Sean Christopherson , Andrew Morton , Joerg Roedel , Ard Biesheuvel Cc: Andi Kleen , Kuppuswamy Sathyanarayanan , David Rientjes , Vlastimil Babka , Tom Lendacky , Thomas Gleixner , Peter Zijlstra , Paolo Bonzini , Ingo Molnar , Dario Faggioli , Mike Rapoport , David Hildenbrand , Mel Gorman , marcelo.cerri@canonical.com, tim.gardner@canonical.com, khalid.elmously@canonical.com, philip.cox@canonical.com, aarcange@redhat.com, peterx@redhat.com, x86@kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv12 5/9] efi: Add unaccepted memory support Date: Fri, 19 May 2023 02:14:30 +0300 Message-Id: <20230518231434.26080-6-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230518231434.26080-1-kirill.shutemov@linux.intel.com> References: <20230518231434.26080-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 8CEA510000B X-Stat-Signature: m49m8kysatec9xrnumtneoh3erhw75za X-HE-Tag: 1684451701-43700 X-HE-Meta: U2FsdGVkX1+leYeN/xRZmWl7egwON+MjXaQa6QdvtMUQUobQfjJYoyiLCjr+poxGTCUCZtiySZnmhkF/yesCh2KzGgm6BuEKhW3BxNr9dotyaCWb9eFqgRqeqMeJOTDTCRXgZRKneDDIMFQo6AWLGVXVg3UkRpRZnYPkt+7YBDlQxqKa9itFL6KzQ4/qjnW/RUstNkQ2TLn0Xvj21nxOBDglpk925H/j6mXIc3kcV0LE4FuhZ7QPorTzO4vlLSuIdluM2HWJMlm8gm8MlIENKkOeGo9wMT7kyZsfqU3vqWVS2LOLsQ2VDDKbyKEZ3oZid4tQs6jn4ReIFt+rvPoedmo5pRWFBrBuC5nMOuwGR+P1hP4Uw50NxdkEFsQT0Vj9VgQhJ1z08HZ6xgEUZhuDMjRqUz781xQv5XDpoq/wKupK8rvbPjKUG3e5A6KBRtVRDi2GVR5ToXWGLQosUYfPh0Te5NCzLvxm8QSoIwn0gaU8ZvPRTi80Opdrym7CT7A/9K2W9lbJfxEKiJBuXy3XgVO0iKoTPKyWP66BzUOrT/mjAGW4TnBZjkVXY5w/s+Ks2UWsGuH2aLnllZ711A/VSH2YQBb1yea0Tpm40uXuKDFqFeMHL+40cpY+MoqElTAZbX2pcvTPc6420cadnp7Ov6dZYV7gfKIPlar0hMo3y0HwT1bZV3X9PXFrcpn5jKgNjkLqaDSJez7dzCr/kBfsb4o6sUflzg+wRZQmhxv8C5M7ZIjaPwN8LHlV1cHRo1wTee+H+S3JyMdU2IMl5rJ9fhCS+owZq/M3IK6aEQ41p6xj/gZBfw3bdj/f25V3DkcVQR1fUQYjzUQU7eQwbRceLUmF/4kTgJIl6CGViO+QGob6LjaDKYwpoKj3XMnj2+0AmjtGuvxebJzxLNCUjUqNJa/Ct7Jof6uGaRtcj3tIcforCcdjYUYbPh7JKmBr3q+XwMsQm+47KFUGAiw6kGg 1PU7r8jp ukszzOobnLNCjy8w2q8LnpYraTah+xGlKpIsTalPunJOhXEY6WCL5cZRjwq31iyzNXjN/CZeI+yM5FpBXiwRHzQZC3ZR9VLHKU0fArDEOI2edmipUzlUCACJMPnkr2uQN0qq4/ev0uW3H/Bdl4wxZ8ShJAD5/AX8W7UoX/NrH7oSswYjsAvDdqE4WWLgSBjxAusXsIvSvwPFstiP7OHDvw5CPmi0+tw+9E9cd X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: efi_config_parse_tables() reserves memory that holds unaccepted memory configuration table so it won't be reused by page allocator. Core-mm requires few helpers to support unaccepted memory: - accept_memory() checks the range of addresses against the bitmap and accept memory if needed. - range_contains_unaccepted_memory() checks if anything within the range requires acceptance. Architectural code has to provide efi_get_unaccepted_table() that returns pointer to the unaccepted memory configuration table. arch_accept_memory() handles arch-specific part of memory acceptance. Signed-off-by: Kirill A. Shutemov Reviewed-by: Ard Biesheuvel Reviewed-by: Tom Lendacky --- arch/x86/platform/efi/efi.c | 3 + drivers/firmware/efi/Makefile | 1 + drivers/firmware/efi/efi.c | 25 ++++++ drivers/firmware/efi/unaccepted_memory.c | 103 +++++++++++++++++++++++ include/linux/efi.h | 1 + 5 files changed, 133 insertions(+) create mode 100644 drivers/firmware/efi/unaccepted_memory.c diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index f3f2d87cce1b..e9f99c56f3ce 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -96,6 +96,9 @@ static const unsigned long * const efi_tables[] = { #ifdef CONFIG_EFI_COCO_SECRET &efi.coco_secret, #endif +#ifdef CONFIG_UNACCEPTED_MEMORY + &efi.unaccepted, +#endif }; u64 efi_setup; /* efi setup_data physical address */ diff --git a/drivers/firmware/efi/Makefile b/drivers/firmware/efi/Makefile index b51f2a4c821e..e489fefd23da 100644 --- a/drivers/firmware/efi/Makefile +++ b/drivers/firmware/efi/Makefile @@ -41,3 +41,4 @@ obj-$(CONFIG_EFI_CAPSULE_LOADER) += capsule-loader.o obj-$(CONFIG_EFI_EARLYCON) += earlycon.o obj-$(CONFIG_UEFI_CPER_ARM) += cper-arm.o obj-$(CONFIG_UEFI_CPER_X86) += cper-x86.o +obj-$(CONFIG_UNACCEPTED_MEMORY) += unaccepted_memory.o diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index 7dce06e419c5..d817e7afd266 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -50,6 +50,9 @@ struct efi __read_mostly efi = { #ifdef CONFIG_EFI_COCO_SECRET .coco_secret = EFI_INVALID_TABLE_ADDR, #endif +#ifdef CONFIG_UNACCEPTED_MEMORY + .unaccepted = EFI_INVALID_TABLE_ADDR, +#endif }; EXPORT_SYMBOL(efi); @@ -605,6 +608,9 @@ static const efi_config_table_type_t common_tables[] __initconst = { #ifdef CONFIG_EFI_COCO_SECRET {LINUX_EFI_COCO_SECRET_AREA_GUID, &efi.coco_secret, "CocoSecret" }, #endif +#ifdef CONFIG_UNACCEPTED_MEMORY + {LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID, &efi.unaccepted, "Unaccepted" }, +#endif #ifdef CONFIG_EFI_GENERIC_STUB {LINUX_EFI_SCREEN_INFO_TABLE_GUID, &screen_info_table }, #endif @@ -759,6 +765,25 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables, } } + if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && + efi.unaccepted != EFI_INVALID_TABLE_ADDR) { + struct efi_unaccepted_memory *unaccepted; + + unaccepted = early_memremap(efi.unaccepted, sizeof(*unaccepted)); + if (unaccepted) { + unsigned long size; + + if (unaccepted->version == 1) { + size = sizeof(*unaccepted) + unaccepted->size; + memblock_reserve(efi.unaccepted, size); + } else { + efi.unaccepted = EFI_INVALID_TABLE_ADDR; + } + + early_memunmap(unaccepted, sizeof(*unaccepted)); + } + } + return 0; } diff --git a/drivers/firmware/efi/unaccepted_memory.c b/drivers/firmware/efi/unaccepted_memory.c new file mode 100644 index 000000000000..bb91c41f76fb --- /dev/null +++ b/drivers/firmware/efi/unaccepted_memory.c @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include + +/* Protects unaccepted memory bitmap */ +static DEFINE_SPINLOCK(unaccepted_memory_lock); + +void accept_memory(phys_addr_t start, phys_addr_t end) +{ + struct efi_unaccepted_memory *unaccepted; + unsigned long range_start, range_end; + unsigned long flags; + u64 unit_size; + + if (efi.unaccepted == EFI_INVALID_TABLE_ADDR) + return; + + unaccepted = efi_get_unaccepted_table(); + if (!unaccepted) + return; + + unit_size = unaccepted->unit_size; + + /* + * Only care for the part of the range that is represented + * in the bitmap. + */ + if (start < unaccepted->phys_base) + start = unaccepted->phys_base; + if (end < unaccepted->phys_base) + return; + + /* Translate to offsets from the beginning of the bitmap */ + start -= unaccepted->phys_base; + end -= unaccepted->phys_base; + + /* Make sure not to overrun the bitmap */ + if (end > unaccepted->size * unit_size * BITS_PER_BYTE) + end = unaccepted->size * unit_size * BITS_PER_BYTE; + + range_start = start / unit_size; + + spin_lock_irqsave(&unaccepted_memory_lock, flags); + for_each_set_bitrange_from(range_start, range_end, unaccepted->bitmap, + DIV_ROUND_UP(end, unit_size)) { + unsigned long phys_start, phys_end; + unsigned long len = range_end - range_start; + + phys_start = range_start * unit_size + unaccepted->phys_base; + phys_end = range_end * unit_size + unaccepted->phys_base; + + arch_accept_memory(phys_start, phys_end); + bitmap_clear(unaccepted->bitmap, range_start, len); + } + spin_unlock_irqrestore(&unaccepted_memory_lock, flags); +} + +bool range_contains_unaccepted_memory(phys_addr_t start, phys_addr_t end) +{ + struct efi_unaccepted_memory *unaccepted; + unsigned long flags; + bool ret = false; + u64 unit_size; + + unaccepted = efi_get_unaccepted_table(); + if (!unaccepted) + return false; + + unit_size = unaccepted->unit_size; + + /* + * Only care for the part of the range that is represented + * in the bitmap. + */ + if (start < unaccepted->phys_base) + start = unaccepted->phys_base; + if (end < unaccepted->phys_base) + return false; + + /* Translate to offsets from the beginning of the bitmap */ + start -= unaccepted->phys_base; + end -= unaccepted->phys_base; + + /* Make sure not to overrun the bitmap */ + if (end > unaccepted->size * unit_size * BITS_PER_BYTE) + end = unaccepted->size * unit_size * BITS_PER_BYTE; + + spin_lock_irqsave(&unaccepted_memory_lock, flags); + while (start < end) { + if (test_bit(start / unit_size, unaccepted->bitmap)) { + ret = true; + break; + } + + start += unit_size; + } + spin_unlock_irqrestore(&unaccepted_memory_lock, flags); + + return ret; +} diff --git a/include/linux/efi.h b/include/linux/efi.h index 29cc622910da..9864f9c00da2 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -646,6 +646,7 @@ extern struct efi { unsigned long tpm_final_log; /* TPM2 Final Events Log table */ unsigned long mokvar_table; /* MOK variable config table */ unsigned long coco_secret; /* Confidential computing secret table */ + unsigned long unaccepted; /* Unaccepted memory table */ efi_get_time_t *get_time; efi_set_time_t *set_time;