From patchwork Thu May 18 23:14:26 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: 13247507 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 733D6C7EE2D for ; Thu, 18 May 2023 23:15:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1E1E8900008; Thu, 18 May 2023 19:14:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 143B2900007; Thu, 18 May 2023 19:14:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E4000900008; Thu, 18 May 2023 19:14:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B987A900006 for ; Thu, 18 May 2023 19:14:57 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 8CC8980970 for ; Thu, 18 May 2023 23:14:57 +0000 (UTC) X-FDA: 80804933034.09.1F4041F Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by imf14.hostedemail.com (Postfix) with ESMTP id 5BF18100009 for ; Thu, 18 May 2023 23:14:55 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="b1JMy/vL"; spf=none (imf14.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.88) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684451695; 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=mbYuUfn2+Mrux+1OXsmbBAKJgFmZ2oFMyWmi1uRfMR8=; b=pJQKfH09UACvAOjQKrgjqmsDvP/sLehl4V1oCPYQWG1ka2rn96kNoD4ENCLov5XuzzIPdb 8MyG2wGtSIMK1ONiintJ3TwVC5IYhQK97QowGlDbXHb1FrnS1eXQu2oJqjBR2U9A+FtrJS nepbCRWw1S0UoX0vY6E8DZY4vcGh5qQ= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="b1JMy/vL"; spf=none (imf14.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.88) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684451695; a=rsa-sha256; cv=none; b=mCmnmr1hvIC37hBFsQncb9n8LLouaIfNp7iJJDlD31ylQsQ1cdjqSmxs8XgGUaSGNSN/kE HhD29fqQy+X8qYagTS+Jidm3Y+pH0OmYlMlcUGDY5n/8EcbGEBHunCxDl3bx7c3o789wJK eAl4IfdkaKP/hEDlpMav0ankLhwdGg0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684451695; x=1715987695; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=db9taUrn11hgDxvXkyxqziZdCMhZCofmcOE8yuxYxXA=; b=b1JMy/vLmaI1gKTcA0XuqriZueDz5OgHdqatXPE7DTYn9sEIM3xlgysZ 3CyOOASiekPWpgd9mbx7KPGhSrk0AL+4CYiBMswLt/LfzqKCcZrOWsU6N Cfc1yyAXbVJP3ME3jI7VxIhU+GJoYFQRTBHxDymDEeXWaRj0NCX+Cc08u KWirDER7vIHeV0qqT8pT0rJM8zKaDnSzCtDoERQ/Fhq9CL2MKlkSbQCEp IQ6xM8BIRk6fd9GUKYaHsaeb5uMq6KyO3pcMxygd2Uj8njd1xhe6mLZra eA8e30vmerZe02/6ZHPt4KCQn9lCV6huPD3C6htHKcglh0+w33w5qp6iq w==; X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="380431725" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="380431725" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2023 16:14:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="846669543" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="846669543" Received: from rkiyama-mobl1.amr.corp.intel.com (HELO box.shutemov.name) ([10.251.222.16]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2023 16:14:42 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 1E6B810DFC5; 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" , Mike Rapoport Subject: [PATCHv12 1/9] mm: Add support for unaccepted memory Date: Fri, 19 May 2023 02:14:26 +0300 Message-Id: <20230518231434.26080-2-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-Rspamd-Queue-Id: 5BF18100009 X-Rspam-User: X-Stat-Signature: mr8xdgm7ybky3qf44sxni65a5dbn6ds9 X-Rspamd-Server: rspam01 X-HE-Tag: 1684451695-774449 X-HE-Meta: U2FsdGVkX1+tZyWsoJBoxE9izL0cM4PFXKDCVDASlh6CFoWKjNTdOlyRquOgAoZWmwwR5R0XW2MfFVlTJk6AaWb/OBvT9oygC/S2luJquqwHKWa/4SsZRVNJglHl9rinIFxSrFcA+1dYqX5FqdOl4w7OEX1ZKTuROsnhNlmLmNKog85xM42C3J5gyttrOIuZwwQ87iL7+vf7R73tVPqC71mHkKqmV7tCQ3qr/CE6HRJAz9395kq8yHIHveKDWIuTRuu6+bjE6hj9TscUBjzz872VQeJr9lScDRMA9mO03umphe/hCEjQ15+EmCSayvevmlZ+igj95J3ILvSlFVlAGdt2kefF8fhPo82G6WIXsaFaVfyTEScqt2TkBl4z1ydNnFAc7JvYgD55rp0MKuej4rXeTqK2LVzUAwpHbAHsOvd3m/RBRwn72Uyc59VLMwKvbYeAVIUWXCdwXRmsvtgAD54XaV2fNDiVNvWksGoUr4IAAf5rDRaF9y1qs+jTDUbAHeRZeqbOlqtv+1ZGQxoS5EagCOX4sf0xe64FAVBta0Yk4sYy5ALRJGJsevyOOBvx9kBLICAEk4piJriDnLAuo3hGBLRNitN248c5MZRbr054Z0aL43XWvYTDQHZ/4sIlXsgFJuLelEoxVxP4kIAWULSPLpjcW+GiaIxDLt1uoF1tN162V9gY7sJcYVX3gybxq6llhpRPXhS21v8FjxHdXY6ocqS0Nr3w11KLwD3wpMETcSY4MAYfNsa/Uz7KhH0abYt2fYg+Yr/+KbrM34noNPZ/Pf4V/znsrHuVYpdXPGDD9aa2Gh2Cn6jNoLoaMe+eQF/Erq/OVxfOK4XWxjpqCDoqhqXm0Zw8+YHU+RQEpbVmBqSw5eUcGJCeBDnMQZvW29pwocsj2FFgiiun+B0Ep8dJAmHJ+ZF0ZejsvfyMJa8GopljMXRVWxHUpFhAAxikhjlwbrmnIN1R0hhg16a z2FNIyO5 bzkK4Kzjf1n7sdKej0oBJrONBT3UzQBd0YoXrI82/UsBhSFvHEENl4yTLmxDFp5k5bZxcqctHao4ZRBox7snYJxyNmpAv0i3+/js9Q9VfUPG+cDvO3k7dl6BRVLwbF9rGXucAY4+2IgYRtQUbXJ92eAht+S62TQOBmdop7gm15+Y8/Rw= 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: UEFI Specification version 2.9 introduces the concept of memory acceptance. Some Virtual Machine platforms, such as Intel TDX or AMD SEV-SNP, require memory to be accepted before it can be used by the guest. Accepting happens via a protocol specific to the Virtual Machine platform. There are several ways kernel can deal with unaccepted memory: 1. Accept all the memory during the boot. It is easy to implement and it doesn't have runtime cost once the system is booted. The downside is very long boot time. Accept can be parallelized to multiple CPUs to keep it manageable (i.e. via DEFERRED_STRUCT_PAGE_INIT), but it tends to saturate memory bandwidth and does not scale beyond the point. 2. Accept a block of memory on the first use. It requires more infrastructure and changes in page allocator to make it work, but it provides good boot time. On-demand memory accept means latency spikes every time kernel steps onto a new memory block. The spikes will go away once workload data set size gets stabilized or all memory gets accepted. 3. Accept all memory in background. Introduce a thread (or multiple) that gets memory accepted proactively. It will minimize time the system experience latency spikes on memory allocation while keeping low boot time. This approach cannot function on its own. It is an extension of #2: background memory acceptance requires functional scheduler, but the page allocator may need to tap into unaccepted memory before that. The downside of the approach is that these threads also steal CPU cycles and memory bandwidth from the user's workload and may hurt user experience. The patch implements #1 and #2 for now. #2 is the default. Some workloads may want to use #1 with accept_memory=eager in kernel command line. #3 can be implemented later based on user's demands. Support of unaccepted memory requires a few changes in core-mm code: - memblock has to accept memory on allocation; - page allocator has to accept memory on the first allocation of the page; Memblock change is trivial. The page allocator is modified to accept pages. New memory gets accepted before putting pages on free lists. It is done lazily: only accept new pages when we run out of already accepted memory. The memory gets accepted until the high watermark is reached. EFI code will provide two helpers if the platform supports unaccepted memory: - accept_memory() makes a range of physical addresses accepted. - range_contains_unaccepted_memory() checks anything within the range of physical addresses requires acceptance. Signed-off-by: Kirill A. Shutemov Acked-by: Mike Rapoport # memblock Reviewed-by: Vlastimil Babka --- drivers/base/node.c | 7 ++ fs/proc/meminfo.c | 5 ++ include/linux/mm.h | 19 +++++ include/linux/mmzone.h | 8 ++ mm/memblock.c | 9 +++ mm/mm_init.c | 7 ++ mm/page_alloc.c | 173 +++++++++++++++++++++++++++++++++++++++++ mm/vmstat.c | 3 + 8 files changed, 231 insertions(+) diff --git a/drivers/base/node.c b/drivers/base/node.c index b46db17124f3..655975946ef6 100644 --- a/drivers/base/node.c +++ b/drivers/base/node.c @@ -448,6 +448,9 @@ static ssize_t node_read_meminfo(struct device *dev, "Node %d ShmemPmdMapped: %8lu kB\n" "Node %d FileHugePages: %8lu kB\n" "Node %d FilePmdMapped: %8lu kB\n" +#endif +#ifdef CONFIG_UNACCEPTED_MEMORY + "Node %d Unaccepted: %8lu kB\n" #endif , nid, K(node_page_state(pgdat, NR_FILE_DIRTY)), @@ -477,6 +480,10 @@ static ssize_t node_read_meminfo(struct device *dev, nid, K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)), nid, K(node_page_state(pgdat, NR_FILE_THPS)), nid, K(node_page_state(pgdat, NR_FILE_PMDMAPPED)) +#endif +#ifdef CONFIG_UNACCEPTED_MEMORY + , + nid, K(sum_zone_node_page_state(nid, NR_UNACCEPTED)) #endif ); len += hugetlb_report_node_meminfo(buf, len, nid); diff --git a/fs/proc/meminfo.c b/fs/proc/meminfo.c index b43d0bd42762..8dca4d6d96c7 100644 --- a/fs/proc/meminfo.c +++ b/fs/proc/meminfo.c @@ -168,6 +168,11 @@ static int meminfo_proc_show(struct seq_file *m, void *v) global_zone_page_state(NR_FREE_CMA_PAGES)); #endif +#ifdef CONFIG_UNACCEPTED_MEMORY + show_val_kb(m, "Unaccepted: ", + global_zone_page_state(NR_UNACCEPTED)); +#endif + hugetlb_report_meminfo(m); arch_report_meminfo(m); diff --git a/include/linux/mm.h b/include/linux/mm.h index 27ce77080c79..d9174d464348 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3816,4 +3816,23 @@ madvise_set_anon_name(struct mm_struct *mm, unsigned long start, } #endif +#ifdef CONFIG_UNACCEPTED_MEMORY + +bool range_contains_unaccepted_memory(phys_addr_t start, phys_addr_t end); +void accept_memory(phys_addr_t start, phys_addr_t end); + +#else + +static inline bool range_contains_unaccepted_memory(phys_addr_t start, + phys_addr_t end) +{ + return false; +} + +static inline void accept_memory(phys_addr_t start, phys_addr_t end) +{ +} + +#endif + #endif /* _LINUX_MM_H */ diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index a4889c9d4055..6c1c2fc13017 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -143,6 +143,9 @@ enum zone_stat_item { NR_ZSPAGES, /* allocated in zsmalloc */ #endif NR_FREE_CMA_PAGES, +#ifdef CONFIG_UNACCEPTED_MEMORY + NR_UNACCEPTED, +#endif NR_VM_ZONE_STAT_ITEMS }; enum node_stat_item { @@ -910,6 +913,11 @@ struct zone { /* free areas of different sizes */ struct free_area free_area[MAX_ORDER + 1]; +#ifdef CONFIG_UNACCEPTED_MEMORY + /* Pages to be accepted. All pages on the list are MAX_ORDER */ + struct list_head unaccepted_pages; +#endif + /* zone flags, see below */ unsigned long flags; diff --git a/mm/memblock.c b/mm/memblock.c index 3feafea06ab2..50b921119600 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -1436,6 +1436,15 @@ phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, */ kmemleak_alloc_phys(found, size, 0); + /* + * Some Virtual Machine platforms, such as Intel TDX or AMD SEV-SNP, + * require memory to be accepted before it can be used by the + * guest. + * + * Accept the memory of the allocated buffer. + */ + accept_memory(found, found + size); + return found; } diff --git a/mm/mm_init.c b/mm/mm_init.c index 7f7f9c677854..1cfc08e25f93 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -1375,6 +1375,10 @@ static void __meminit zone_init_free_lists(struct zone *zone) INIT_LIST_HEAD(&zone->free_area[order].free_list[t]); zone->free_area[order].nr_free = 0; } + +#ifdef CONFIG_UNACCEPTED_MEMORY + INIT_LIST_HEAD(&zone->unaccepted_pages); +#endif } void __meminit init_currently_empty_zone(struct zone *zone, @@ -1960,6 +1964,9 @@ static void __init deferred_free_range(unsigned long pfn, return; } + /* Accept chunks smaller than MAX_ORDER upfront */ + accept_memory(PFN_PHYS(pfn), PFN_PHYS(pfn + nr_pages)); + for (i = 0; i < nr_pages; i++, page++, pfn++) { if (pageblock_aligned(pfn)) set_pageblock_migratetype(page, MIGRATE_MOVABLE); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 47421bedc12b..d239fba3f31c 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -387,6 +387,12 @@ EXPORT_SYMBOL(nr_node_ids); EXPORT_SYMBOL(nr_online_nodes); #endif +static bool page_contains_unaccepted(struct page *page, unsigned int order); +static void accept_page(struct page *page, unsigned int order); +static bool try_to_accept_memory(struct zone *zone, unsigned int order); +static inline bool has_unaccepted_memory(void); +static bool __free_unaccepted(struct page *page); + int page_group_by_mobility_disabled __read_mostly; #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT @@ -1481,6 +1487,13 @@ void __free_pages_core(struct page *page, unsigned int order) atomic_long_add(nr_pages, &page_zone(page)->managed_pages); + if (page_contains_unaccepted(page, order)) { + if (order == MAX_ORDER && __free_unaccepted(page)) + return; + + accept_page(page, order); + } + /* * Bypass PCP and place fresh pages right to the tail, primarily * relevant for memory onlining. @@ -3159,6 +3172,9 @@ static inline long __zone_watermark_unusable_free(struct zone *z, if (!(alloc_flags & ALLOC_CMA)) unusable_free += zone_page_state(z, NR_FREE_CMA_PAGES); #endif +#ifdef CONFIG_UNACCEPTED_MEMORY + unusable_free += zone_page_state(z, NR_UNACCEPTED); +#endif return unusable_free; } @@ -3458,6 +3474,11 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags, gfp_mask)) { int ret; + if (has_unaccepted_memory()) { + if (try_to_accept_memory(zone, order)) + goto try_this_zone; + } + #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT /* * Watermark failed for this zone, but see if we can @@ -3510,6 +3531,11 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags, return page; } else { + if (has_unaccepted_memory()) { + if (try_to_accept_memory(zone, order)) + goto try_this_zone; + } + #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT /* Try again if zone has deferred pages */ if (deferred_pages_enabled()) { @@ -7215,3 +7241,150 @@ bool has_managed_dma(void) return false; } #endif /* CONFIG_ZONE_DMA */ + +#ifdef CONFIG_UNACCEPTED_MEMORY + +/* Counts number of zones with unaccepted pages. */ +static DEFINE_STATIC_KEY_FALSE(zones_with_unaccepted_pages); + +static bool lazy_accept = true; + +static int __init accept_memory_parse(char *p) +{ + if (!strcmp(p, "lazy")) { + lazy_accept = true; + return 0; + } else if (!strcmp(p, "eager")) { + lazy_accept = false; + return 0; + } else { + return -EINVAL; + } +} +early_param("accept_memory", accept_memory_parse); + +static bool page_contains_unaccepted(struct page *page, unsigned int order) +{ + phys_addr_t start = page_to_phys(page); + phys_addr_t end = start + (PAGE_SIZE << order); + + return range_contains_unaccepted_memory(start, end); +} + +static void accept_page(struct page *page, unsigned int order) +{ + phys_addr_t start = page_to_phys(page); + + accept_memory(start, start + (PAGE_SIZE << order)); +} + +static bool try_to_accept_memory_one(struct zone *zone) +{ + unsigned long flags; + struct page *page; + bool last; + + if (list_empty(&zone->unaccepted_pages)) + return false; + + spin_lock_irqsave(&zone->lock, flags); + page = list_first_entry_or_null(&zone->unaccepted_pages, + struct page, lru); + if (!page) { + spin_unlock_irqrestore(&zone->lock, flags); + return false; + } + + list_del(&page->lru); + last = list_empty(&zone->unaccepted_pages); + + __mod_zone_freepage_state(zone, -MAX_ORDER_NR_PAGES, MIGRATE_MOVABLE); + __mod_zone_page_state(zone, NR_UNACCEPTED, -MAX_ORDER_NR_PAGES); + spin_unlock_irqrestore(&zone->lock, flags); + + accept_page(page, MAX_ORDER); + + __free_pages_ok(page, MAX_ORDER, FPI_TO_TAIL); + + if (last) + static_branch_dec(&zones_with_unaccepted_pages); + + return true; +} + +static bool try_to_accept_memory(struct zone *zone, unsigned int order) +{ + long to_accept; + int ret = false; + + /* How much to accept to get to high watermark? */ + to_accept = high_wmark_pages(zone) - + (zone_page_state(zone, NR_FREE_PAGES) - + __zone_watermark_unusable_free(zone, order, 0)); + + /* Accept at least one page */ + do { + if (!try_to_accept_memory_one(zone)) + break; + ret = true; + to_accept -= MAX_ORDER_NR_PAGES; + } while (to_accept > 0); + + return ret; +} + +static inline bool has_unaccepted_memory(void) +{ + return static_branch_unlikely(&zones_with_unaccepted_pages); +} + +static bool __free_unaccepted(struct page *page) +{ + struct zone *zone = page_zone(page); + unsigned long flags; + bool first = false; + + if (!lazy_accept) + return false; + + spin_lock_irqsave(&zone->lock, flags); + first = list_empty(&zone->unaccepted_pages); + list_add_tail(&page->lru, &zone->unaccepted_pages); + __mod_zone_freepage_state(zone, MAX_ORDER_NR_PAGES, MIGRATE_MOVABLE); + __mod_zone_page_state(zone, NR_UNACCEPTED, MAX_ORDER_NR_PAGES); + spin_unlock_irqrestore(&zone->lock, flags); + + if (first) + static_branch_inc(&zones_with_unaccepted_pages); + + return true; +} + +#else + +static bool page_contains_unaccepted(struct page *page, unsigned int order) +{ + return false; +} + +static void accept_page(struct page *page, unsigned int order) +{ +} + +static bool try_to_accept_memory(struct zone *zone, unsigned int order) +{ + return false; +} + +static inline bool has_unaccepted_memory(void) +{ + return false; +} + +static bool __free_unaccepted(struct page *page) +{ + BUILD_BUG(); + return false; +} + +#endif /* CONFIG_UNACCEPTED_MEMORY */ diff --git a/mm/vmstat.c b/mm/vmstat.c index c28046371b45..282349cabf01 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -1180,6 +1180,9 @@ const char * const vmstat_text[] = { "nr_zspages", #endif "nr_free_cma", +#ifdef CONFIG_UNACCEPTED_MEMORY + "nr_unaccepted", +#endif /* enum numa_stat_item counters */ #ifdef CONFIG_NUMA From patchwork Thu May 18 23:14:27 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: 13247504 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 672C0C77B73 for ; Thu, 18 May 2023 23:14:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B5739900005; Thu, 18 May 2023 19:14:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ADF94900003; Thu, 18 May 2023 19:14:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 958DF900005; Thu, 18 May 2023 19:14:55 -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 7E473900004 for ; Thu, 18 May 2023 19:14:55 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 31449AE40B for ; Thu, 18 May 2023 23:14:55 +0000 (UTC) X-FDA: 80804932950.26.3FADB30 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf18.hostedemail.com (Postfix) with ESMTP id 4AAEA1C0011 for ; Thu, 18 May 2023 23:14:51 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DC06KNWH; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf18.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=1684451693; a=rsa-sha256; cv=none; b=Kdaj1ed8EbyJjXS+yWgzMmElb6EKhZpmUemf+ZQRYv41xPuFSjhAI7kzFLn+npc6/MylK5 w+9bSwOlajIxYa+2N2J7OSBk9fy/tvljsAVxPM58Wy09FJ5UqmGxLekGPgeml7WechGFxq xFM5ut+H2pixQYLU7xoOO3kw7wDvj14= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DC06KNWH; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf18.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=1684451693; 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=BaZyb9j0toOFpXjTH7X5V8WTk1CeGrfyPyJRw/21oXU=; b=r10B1LYTAyP3ZakP4lSYDgjIUSe1BipQv/SFWznQDk0F0bn7kERZ33fv58iKvUprjDm02N DuTC+NiVYKOYK0TMGAKMRSQ0zFSGEerYeMJROQ4z69IpDxiLkFmQSZLsLOaqoKC1ezzLOq XEkcjtiilaZK1g9QdAZp86h2IaF4iYM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684451692; x=1715987692; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LJWuKaqvYOvqAXDR1TKqUlI78Dny7nmmQ0aMOVcXkCg=; b=DC06KNWHKgwqrKSPdqeutaWGgg4aR4MpDQSCdxojG7oFVamKFMwJU2my OMN7+byX4ULn9guExbElvZHUZpf97x7JXIsm8lPlpX/zMavDUb0l6d3NO Ae3y1jKZ4XA8Fr11lD4jW1qg5wizBk4UPYNptrzKNVnze1Uj7+SRNIRIU 31jPoJAT4qsu21fxyKDuYvsmKbQaEV26jsugFNTYEmtwPSCK0t+lnDAns 4KJcJCPid6UfF6CNK2EBibQSGvW/vNew8xBb/x45AvzK0RHbduK7kE0mR PZ9I6inpd0YZ7T3WvG47Dc9RemGFXrmLjbJZQSBkvdvs/7lLN6QVZvGUL A==; X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="341652060" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="341652060" 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:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="772065073" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="772065073" 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:42 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 2A6C610DFC6; 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" , Borislav Petkov Subject: [PATCHv12 2/9] efi/x86: Get full memory map in allocate_e820() Date: Fri, 19 May 2023 02:14:27 +0300 Message-Id: <20230518231434.26080-3-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: rspam06 X-Rspamd-Queue-Id: 4AAEA1C0011 X-Stat-Signature: wk8xbrzgkne16tqjx696mtfk51gmkq8k X-HE-Tag: 1684451691-588167 X-HE-Meta: U2FsdGVkX185gN+qYs8Bqmp/XWa4B0cKsvGA1al1xerz8tJTXbr91FKRx39ziyjxc7fATjILbesQtWubjN7d0sjWuTOolhtzEpt+ddhvPBiffX7cVibE0zEGMG+U6Glg/dmFKNV62iOCr/haQ2MAlitbgvGtz7ft7Vt3fXEfX6KLe8LDlPNvjL2gN8cZ2ye+cVby9Qkek3pGjEqttqzGnccNl5v4+ceBlgL00Jg97kknbsRUkYXhzjhDRA6bkovCtGCrTPFs693Cabbngb2d+122Vq3d6cVMxoJV2SQdPS7XTGsIJNmgzZc9MKrBtWchLCzqc4QtFdhoacGUgaUjx+/8vJCfKqUgskVPzc4/aJHHihaEmMj8oHycJWDCbbhGhzYD7pWjsDlQRQesruziv0edTt29atkv5idGlf7UtVk4X11yujM/ksdOXhGqkLEEtxHumaIugJrjQdAWtIOt6eMj5h8kARw5Fgm60d/7dDh3M6XR1GfiPQlL+LRUH/8Z4I6FScG+qB8XQzn/y1lHDQFmmPbRzASKNKLd2Sr2yKc/wUCTaCG3e0Fiwl7RZrf6foVK7l6i5MrszPKlhxgkNZ4MwG5FxixK/mQM+Wys5GkEPD8fMaaD0v9FzaRahbXe0WXTyzQ0L/jMeqtVXJgPLeBjZHhV4kmjmA0EKp2MTFbv34Dkv+ILtTCvB+Lgnu0bhXvvGzkf3KvljM8cgHb+Q7IAQA394xP5KUsOYOp/a/NeCNP7Zjl6WBr4Q9d8Fu7zlzNQ0gORulRJZ8vnLgZWew4WqOurQXyc8+1CtpgtT7NTODLchNiChKnuVrNaCbDNRGw9I9Bi44UR5BQqEJSddob/jHQl2d72wQ8Dk1WFm/4qGTvVI5X7I1uRlWjKG5OgVUDQX+5xvRuD6lhnkHaSnWDed/VH1na08M9wBtILpwybg3mnktCunhaPEePYz3qLnJ9S8V3D4dW1M4fbx7O AsKa3ZGV eFHpBsQrBMAOr3hbkhnkjRvwREoNBi6xNutF2pD+YDiUJd1rAxCECYKOO0Jx9DXWyaG+XpfSkh2rJDJN3j5+RjhZK//t/EsuM22ANbQmTju61K2Do92tjgMqdQscYEXkmR2QuOn3SXBI6QT53soT3JDSSgQ7NH0q+k0MMsJRHaru0P7QtgnfKYHmRrjOpizC7+4Eevxmwio2czPCYo2G4eF4r+ldh9WZ4RakrvQKMPrA3D335zVaSPDgJ5FxRBtTcBzW0397bpaeZbJw= 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: Currently allocate_e820() is only interested in the size of map and size of memory descriptor to determine how many e820 entries the kernel needs. UEFI Specification version 2.9 introduces a new memory type -- unaccepted memory. To track unaccepted memory kernel needs to allocate a bitmap. The size of the bitmap is dependent on the maximum physical address present in the system. A full memory map is required to find the maximum address. Modify allocate_e820() to get a full memory map. Signed-off-by: Kirill A. Shutemov Reviewed-by: Borislav Petkov Acked-by: Ard Biesheuvel Reviewed-by: Tom Lendacky --- drivers/firmware/efi/libstub/x86-stub.c | 26 +++++++++++-------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index a0bfd31358ba..fff81843169c 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -681,28 +681,24 @@ static efi_status_t allocate_e820(struct boot_params *params, struct setup_data **e820ext, u32 *e820ext_size) { - unsigned long map_size, desc_size, map_key; + struct efi_boot_memmap *map; efi_status_t status; - __u32 nr_desc, desc_version; + __u32 nr_desc; - /* Only need the size of the mem map and size of each mem descriptor */ - map_size = 0; - status = efi_bs_call(get_memory_map, &map_size, NULL, &map_key, - &desc_size, &desc_version); - if (status != EFI_BUFFER_TOO_SMALL) - return (status != EFI_SUCCESS) ? status : EFI_UNSUPPORTED; + status = efi_get_memory_map(&map, false); + if (status != EFI_SUCCESS) + return status; - nr_desc = map_size / desc_size + EFI_MMAP_NR_SLACK_SLOTS; - - if (nr_desc > ARRAY_SIZE(params->e820_table)) { - u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table); + nr_desc = map->map_size / map->desc_size; + if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) { + u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) + + EFI_MMAP_NR_SLACK_SLOTS; status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size); - if (status != EFI_SUCCESS) - return status; } - return EFI_SUCCESS; + efi_bs_call(free_pool, map); + return status; } struct exit_boot_struct { From patchwork Thu May 18 23:14:28 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: 13247505 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 616F3C77B7A for ; Thu, 18 May 2023 23:14:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E65C0900003; Thu, 18 May 2023 19:14:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DED1C900007; Thu, 18 May 2023 19:14:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A69C6900004; Thu, 18 May 2023 19:14:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 77A3F900003 for ; Thu, 18 May 2023 19:14:55 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3CA3E1A09B2 for ; Thu, 18 May 2023 23:14:55 +0000 (UTC) X-FDA: 80804932950.24.DD4C65E Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by imf14.hostedemail.com (Postfix) with ESMTP id E3F3B100007 for ; Thu, 18 May 2023 23:14:52 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jBiqYrUZ; spf=none (imf14.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.88) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684451693; 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=ChER3xQfMLZMGlQSzT78Lr6o2Uvg2Cvho7Rp1ehqnsY=; b=we7NP4eB84ZCE91P5z26HSvcIqBQpar1H4tyYahAF0SwOVzhcoZQ6/+60qmwbiA6jF+7DS ff8V7l4WCFGrWWCt4rV+xRHyLHKBU9T7AkarVsf5Dp+xMakd2yaPdb0TafgDEnAOKreAWI sAYb1f0Ep7Gh0lmshldqJmrHoYhXnQQ= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jBiqYrUZ; spf=none (imf14.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.88) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684451693; a=rsa-sha256; cv=none; b=Vml2i3mxe/1FBzIGYif1Q+2ET3ASMAMKYvHSrREpmlgeewqhss9j8l7OKUcA8oQeHal6YU CgKX6VSeZEODsC0QXYPhLlEkLmYExKLfC6n2awJeMxI+1e2Cb7zRZooyZyZxmPy99xBaBL B/8n78Qop5iADS/OUuYPTTgsGgX/Wns= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684451693; x=1715987693; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=n9SDPh3Ob0T0sTasYeFOdu7CKW9d+5L1TvbhmirjRWA=; b=jBiqYrUZDtjEdcMMsLGR/Uwa26LwbDQNtXUZrMAKdwgquCmI7+m2j2B/ qgfmaaglc9I1FCIu5bd7e/++Wbcc6RqoTa10MqmYqJLf8KNe99ZMX90jM joSTb3j/CwIkvTZ79tdwsdDRiwMwU0NzWC/h0AT8qeLV2R1sDK4RQYm2v 0Q25IDtsim8JCkIGyKLXMHtjfMfbVRwyjvih5/VFYmmzpN+IfvFbbgZBi QYvyjFTAPdK/7uczCHSqtRYvlUsFQGIA4RmuvlXPqsWRHLa5KyX6M/1lx peW2IhOPhrOscodzRkbd2GBRW38KeF0LJ7fuCAAKzFpa1jHt4bKZcNpIo g==; X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="380431690" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="380431690" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2023 16:14:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="846669537" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="846669537" Received: from rkiyama-mobl1.amr.corp.intel.com (HELO box.shutemov.name) ([10.251.222.16]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2023 16:14:42 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 344EA10DFCE; 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 3/9] efi/libstub: Implement support for unaccepted memory Date: Fri, 19 May 2023 02:14:28 +0300 Message-Id: <20230518231434.26080-4-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-Rspamd-Queue-Id: E3F3B100007 X-Rspam-User: X-Stat-Signature: sg7x9ixg49ox5yqqycgu9j3k4ats6cde X-Rspamd-Server: rspam01 X-HE-Tag: 1684451692-607613 X-HE-Meta: U2FsdGVkX1++bbJQ+DhSoGz04UPGpe7WZWVTdbXsJBFz2hoGen3/Lr7s4Ngm/PIUsVjLKlNliwgWc79tyBbcgcrWGQbs5VZPboq/BuRhGoWcRVia0tpkY5oco2h0KVfTgpeq9+q6rCvXNp61KdXzq6Sv4inBgzR9CkNMUf27U17Achk93S959d22lJitlkLnzHu3DZtCeF46E9G08dlQBVgoSNZcq44TCzGdubtSdoZnew6ttjIUK+AwB4o+VMxIxpgLu/KMzwcVRYvmQubDxgY8soiHCK2g6siNxnJsvt6tgn/1lW6XP6njHsSB6NGBbwZ7MATdnQyn4+gs+GPkd0Cpf3qB+bTwA0fcad0KAzZJGRHBNv7NUaCM7Z5VdUS2CCpQ9B1E0Ndy8zqfABEPhmT7lo+0YTLLwBoA7bTyLOty3+Gf971fli20uDvOg3EDuIghwc27UgYcPzddPvO9ny4MpW0Ld55AxplSciOSMQtA3XoFZYr5DJVLkm1AEPqgGlV2ch7YCSKBKIj0rOmwauX7ITP43zg/4t2I6m2cLHCuxs8L04lc136Ohli4KEtwLPtBb6+to3W0/tY/uhyXkSIh7XS1Tf49XQhxt1gbvu1kpbBjqwmRhGt5tU12acc8d7yhbu7Q+rddGbGOQe82p2Kce/nJ3aw+Q3gHd584C8uNo03r2dzlGtOfF17P7Ni0HyST/sreS2CHxZmSfqw94OV8RjuZXuhTucPOReq/HxJV22ycl+861cGVqvNrFV+uZ47juW0YFb7ZesxQevAIH5Hao3BSG/ZwcrcZ45Wh2Ak1OCuKhE78OGYd68fsU08gD+rrjvCNEi8mTj61eqyXbFlqWrnoyOFuFyyYOeYNKJW+Gue4B9ZFo0TvEBohnspOVpNvGjCyTaZ/K3WCMV1xT1GPcKA/cXHBzmHk3llFEdIJqLf+QL6LYhJqL9pER587pypVBmWf1HgIS9U02YV iDyNub2C 4OsE2B1l/16oZXi7S9g5N9BVcmDwavBN9SgRtuB55S7Dit41Kd/BLePIIrMf6yEpGqJuoTp/BYFUUC2pWBjMeDzKN1LrSY1DlMRgcrv1wyi8FaJWpdT1ltnpGayz7hHXGdQ6snVEL0m5a+KPNIsz/uPGUn0Nmnk8YB7P9DloXsQRleIaxhTdH1SjUAkX0uC4FAkT72QXsIVIesNcMjRm+mqu9Q1XkqPW4oQ44rYIxkrd9d8s= 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: UEFI Specification version 2.9 introduces the concept of memory acceptance: Some Virtual Machine platforms, such as Intel TDX or AMD SEV-SNP, requiring memory to be accepted before it can be used by the guest. Accepting happens via a protocol specific for the Virtual Machine platform. Accepting memory is costly and it makes VMM allocate memory for the accepted guest physical address range. It's better to postpone memory acceptance until memory is needed. It lowers boot time and reduces memory overhead. The kernel needs to know what memory has been accepted. Firmware communicates this information via memory map: a new memory type -- EFI_UNACCEPTED_MEMORY -- indicates such memory. Range-based tracking works fine for firmware, but it gets bulky for the kernel: e820 (or whatever the arch uses) has to be modified on every page acceptance. It leads to table fragmentation and there's a limited number of entries in the e820 table. Another option is to mark such memory as usable in e820 and track if the range has been accepted in a bitmap. One bit in the bitmap represents a naturally aligned power-2-sized region of address space -- unit. For x86, unit size is 2MiB: 4k of the bitmap is enough to track 64GiB or physical address space. In the worst-case scenario -- a huge hole in the middle of the address space -- It needs 256MiB to handle 4PiB of the address space. Any unaccepted memory that is not aligned to unit_size gets accepted upfront. The bitmap is allocated and constructed in the EFI stub and passed down to the kernel via EFI configuration table. allocate_e820() allocates the bitmap if unaccepted memory is present, according to the size of unaccepted region. Signed-off-by: Kirill A. Shutemov Reviewed-by: Ard Biesheuvel Reviewed-by: Tom Lendacky --- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/mem.c | 9 + arch/x86/include/asm/efi.h | 2 + drivers/firmware/efi/Kconfig | 14 ++ drivers/firmware/efi/efi.c | 1 + drivers/firmware/efi/libstub/Makefile | 2 + drivers/firmware/efi/libstub/bitmap.c | 41 +++ drivers/firmware/efi/libstub/efistub.h | 6 + drivers/firmware/efi/libstub/find.c | 43 ++++ .../firmware/efi/libstub/unaccepted_memory.c | 234 ++++++++++++++++++ drivers/firmware/efi/libstub/x86-stub.c | 13 + include/linux/efi.h | 12 +- 12 files changed, 377 insertions(+), 1 deletion(-) create mode 100644 arch/x86/boot/compressed/mem.c create mode 100644 drivers/firmware/efi/libstub/bitmap.c create mode 100644 drivers/firmware/efi/libstub/find.c create mode 100644 drivers/firmware/efi/libstub/unaccepted_memory.c diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index 6b6cfe607bdb..cc4978123c30 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -107,6 +107,7 @@ endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o $(obj)/tdcall.o +vmlinux-objs-$(CONFIG_UNACCEPTED_MEMORY) += $(obj)/mem.o vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o diff --git a/arch/x86/boot/compressed/mem.c b/arch/x86/boot/compressed/mem.c new file mode 100644 index 000000000000..67594fcb11d9 --- /dev/null +++ b/arch/x86/boot/compressed/mem.c @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "error.h" + +void arch_accept_memory(phys_addr_t start, phys_addr_t end) +{ + /* Platform-specific memory-acceptance call goes here */ + error("Cannot accept memory"); +} diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h index 419280d263d2..8b4be7cecdb8 100644 --- a/arch/x86/include/asm/efi.h +++ b/arch/x86/include/asm/efi.h @@ -31,6 +31,8 @@ extern unsigned long efi_mixed_mode_stack_pa; #define ARCH_EFI_IRQ_FLAGS_MASK X86_EFLAGS_IF +#define EFI_UNACCEPTED_UNIT_SIZE PMD_SIZE + /* * The EFI services are called through variadic functions in many cases. These * functions are implemented in assembler and support only a fixed number of diff --git a/drivers/firmware/efi/Kconfig b/drivers/firmware/efi/Kconfig index 043ca31c114e..231f1c70d1db 100644 --- a/drivers/firmware/efi/Kconfig +++ b/drivers/firmware/efi/Kconfig @@ -269,6 +269,20 @@ config EFI_COCO_SECRET virt/coco/efi_secret module to access the secrets, which in turn allows userspace programs to access the injected secrets. +config UNACCEPTED_MEMORY + bool + depends on EFI_STUB + help + Some Virtual Machine platforms, such as Intel TDX, require + some memory to be "accepted" by the guest before it can be used. + This mechanism helps prevent malicious hosts from making changes + to guest memory. + + UEFI specification v2.9 introduced EFI_UNACCEPTED_MEMORY memory type. + + This option adds support for unaccepted memory and makes such memory + usable by the kernel. + config EFI_EMBEDDED_FIRMWARE bool select CRYPTO_LIB_SHA256 diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index abeff7dc0b58..7dce06e419c5 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -843,6 +843,7 @@ static __initdata char memory_type_name[][13] = { "MMIO Port", "PAL Code", "Persistent", + "Unaccepted", }; char * __init efi_md_typeattr_format(char *buf, size_t size, diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile index 3abb2b357482..16d64a34d1e1 100644 --- a/drivers/firmware/efi/libstub/Makefile +++ b/drivers/firmware/efi/libstub/Makefile @@ -96,6 +96,8 @@ CFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) zboot-obj-$(CONFIG_RISCV) := lib-clz_ctz.o lib-ashldi3.o lib-$(CONFIG_EFI_ZBOOT) += zboot.o $(zboot-obj-y) +lib-$(CONFIG_UNACCEPTED_MEMORY) += unaccepted_memory.o bitmap.o find.o + extra-y := $(lib-y) lib-y := $(patsubst %.o,%.stub.o,$(lib-y)) diff --git a/drivers/firmware/efi/libstub/bitmap.c b/drivers/firmware/efi/libstub/bitmap.c new file mode 100644 index 000000000000..5c9bba0d549b --- /dev/null +++ b/drivers/firmware/efi/libstub/bitmap.c @@ -0,0 +1,41 @@ +#include + +void __bitmap_set(unsigned long *map, unsigned int start, int len) +{ + unsigned long *p = map + BIT_WORD(start); + const unsigned int size = start + len; + int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); + unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start); + + while (len - bits_to_set >= 0) { + *p |= mask_to_set; + len -= bits_to_set; + bits_to_set = BITS_PER_LONG; + mask_to_set = ~0UL; + p++; + } + if (len) { + mask_to_set &= BITMAP_LAST_WORD_MASK(size); + *p |= mask_to_set; + } +} + +void __bitmap_clear(unsigned long *map, unsigned int start, int len) +{ + unsigned long *p = map + BIT_WORD(start); + const unsigned int size = start + len; + int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); + unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start); + + while (len - bits_to_clear >= 0) { + *p &= ~mask_to_clear; + len -= bits_to_clear; + bits_to_clear = BITS_PER_LONG; + mask_to_clear = ~0UL; + p++; + } + if (len) { + mask_to_clear &= BITMAP_LAST_WORD_MASK(size); + *p &= ~mask_to_clear; + } +} diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h index 67d5a20802e0..8659a01664b8 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -1133,4 +1133,10 @@ const u8 *__efi_get_smbios_string(const struct efi_smbios_record *record, void efi_remap_image(unsigned long image_base, unsigned alloc_size, unsigned long code_size); +efi_status_t allocate_unaccepted_bitmap(__u32 nr_desc, + struct efi_boot_memmap *map); +void process_unaccepted_memory(u64 start, u64 end); +void accept_memory(phys_addr_t start, phys_addr_t end); +void arch_accept_memory(phys_addr_t start, phys_addr_t end); + #endif diff --git a/drivers/firmware/efi/libstub/find.c b/drivers/firmware/efi/libstub/find.c new file mode 100644 index 000000000000..4e7740d28987 --- /dev/null +++ b/drivers/firmware/efi/libstub/find.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include + +/* + * Common helper for find_next_bit() function family + * @FETCH: The expression that fetches and pre-processes each word of bitmap(s) + * @MUNGE: The expression that post-processes a word containing found bit (may be empty) + * @size: The bitmap size in bits + * @start: The bitnumber to start searching at + */ +#define FIND_NEXT_BIT(FETCH, MUNGE, size, start) \ +({ \ + unsigned long mask, idx, tmp, sz = (size), __start = (start); \ + \ + if (unlikely(__start >= sz)) \ + goto out; \ + \ + mask = MUNGE(BITMAP_FIRST_WORD_MASK(__start)); \ + idx = __start / BITS_PER_LONG; \ + \ + for (tmp = (FETCH) & mask; !tmp; tmp = (FETCH)) { \ + if ((idx + 1) * BITS_PER_LONG >= sz) \ + goto out; \ + idx++; \ + } \ + \ + sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(tmp)), sz); \ +out: \ + sz; \ +}) + +unsigned long _find_next_bit(const unsigned long *addr, unsigned long nbits, unsigned long start) +{ + return FIND_NEXT_BIT(addr[idx], /* nop */, nbits, start); +} + +unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits, + unsigned long start) +{ + return FIND_NEXT_BIT(~addr[idx], /* nop */, nbits, start); +} diff --git a/drivers/firmware/efi/libstub/unaccepted_memory.c b/drivers/firmware/efi/libstub/unaccepted_memory.c new file mode 100644 index 000000000000..fd6a3195c68f --- /dev/null +++ b/drivers/firmware/efi/libstub/unaccepted_memory.c @@ -0,0 +1,234 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include "efistub.h" + +static struct efi_unaccepted_memory *unaccepted_table; + +/* + * Decompressor needs to initialize the variable to cover cases when the table + * is not allocated by EFI stub or EFI stub copied the kernel image with + * efi_relocate_kernel() before the variable is set. + * + * It must be call before the first usage of accept_memory() by decompressor. + */ +void set_unaccepted_table(struct efi_unaccepted_memory *table) +{ + unaccepted_table = table; +} + +efi_status_t allocate_unaccepted_bitmap(__u32 nr_desc, + struct efi_boot_memmap *map) +{ + efi_guid_t unaccepted_table_guid = LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID; + u64 unaccepted_start = ULLONG_MAX, unaccepted_end = 0, bitmap_size; + efi_status_t status; + int i; + + /* Check if the table is already installed */ + unaccepted_table = get_efi_config_table(unaccepted_table_guid); + if (unaccepted_table) { + if (unaccepted_table->version != 1) { + efi_err("Unknown version of unaccepted memory table\n"); + return EFI_UNSUPPORTED; + } + return EFI_SUCCESS; + } + + /* Check if there's any unaccepted memory and find the max address */ + for (i = 0; i < nr_desc; i++) { + efi_memory_desc_t *d; + unsigned long m = (unsigned long)map->map; + + d = efi_early_memdesc_ptr(m, map->desc_size, i); + if (d->type != EFI_UNACCEPTED_MEMORY) + continue; + + unaccepted_start = min(unaccepted_start, d->phys_addr); + unaccepted_end = max(unaccepted_end, + d->phys_addr + d->num_pages * PAGE_SIZE); + } + + if (unaccepted_start == ULLONG_MAX) + return EFI_SUCCESS; + + unaccepted_start = round_down(unaccepted_start, + EFI_UNACCEPTED_UNIT_SIZE); + unaccepted_end = round_up(unaccepted_end, EFI_UNACCEPTED_UNIT_SIZE); + + /* + * If unaccepted memory is present, allocate a bitmap to track what + * memory has to be accepted before access. + * + * One bit in the bitmap represents 2MiB in the address space: + * A 4k bitmap can track 64GiB of physical address space. + * + * In the worst case scenario -- a huge hole in the middle of the + * address space -- It needs 256MiB to handle 4PiB of the address + * space. + * + * The bitmap will be populated in setup_e820() according to the memory + * map after efi_exit_boot_services(). + */ + bitmap_size = DIV_ROUND_UP(unaccepted_end - unaccepted_start, + EFI_UNACCEPTED_UNIT_SIZE * BITS_PER_BYTE); + + status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, + sizeof(*unaccepted_table) + bitmap_size, + (void **)&unaccepted_table); + if (status != EFI_SUCCESS) { + efi_err("Failed to allocate unaccepted memory config table\n"); + return status; + } + + unaccepted_table->version = 1; + unaccepted_table->unit_size = EFI_UNACCEPTED_UNIT_SIZE; + unaccepted_table->phys_base = unaccepted_start; + unaccepted_table->size = bitmap_size; + memset(unaccepted_table->bitmap, 0, bitmap_size); + + status = efi_bs_call(install_configuration_table, + &unaccepted_table_guid, unaccepted_table); + if (status != EFI_SUCCESS) { + efi_bs_call(free_pool, unaccepted_table); + efi_err("Failed to install unaccepted memory config table!\n"); + } + + return status; +} + +/* + * The accepted memory bitmap only works at unit_size granularity. Take + * unaligned start/end addresses and either: + * 1. Accepts the memory immediately and in its entirety + * 2. Accepts unaligned parts, and marks *some* aligned part unaccepted + * + * The function will never reach the bitmap_set() with zero bits to set. + */ +void process_unaccepted_memory(u64 start, u64 end) +{ + u64 unit_size = unaccepted_table->unit_size; + u64 unit_mask = unaccepted_table->unit_size - 1; + u64 bitmap_size = unaccepted_table->size; + + /* + * Ensure that at least one bit will be set in the bitmap by + * immediately accepting all regions under 2*unit_size. This is + * imprecise and may immediately accept some areas that could + * have been represented in the bitmap. But, results in simpler + * code below + * + * Consider case like this (assuming unit_size == 2MB): + * + * | 4k | 2044k | 2048k | + * ^ 0x0 ^ 2MB ^ 4MB + * + * Only the first 4k has been accepted. The 0MB->2MB region can not be + * represented in the bitmap. The 2MB->4MB region can be represented in + * the bitmap. But, the 0MB->4MB region is <2*unit_size and will be + * immediately accepted in its entirety. + */ + if (end - start < 2 * unit_size) { + arch_accept_memory(start, end); + return; + } + + /* + * No matter how the start and end are aligned, at least one unaccepted + * unit_size area will remain to be marked in the bitmap. + */ + + /* Immediately accept a phys_base) { + arch_accept_memory(start, + min(unaccepted_table->phys_base, end)); + start = unaccepted_table->phys_base; + } + + /* Nothing to record */ + if (end < unaccepted_table->phys_base) + return; + + /* Translate to offsets from the beginning of the bitmap */ + start -= unaccepted_table->phys_base; + end -= unaccepted_table->phys_base; + + /* Accept memory that doesn't fit into bitmap */ + if (end > bitmap_size * unit_size * BITS_PER_BYTE) { + unsigned long phys_start, phys_end; + + phys_start = bitmap_size * unit_size * BITS_PER_BYTE + + unaccepted_table->phys_base; + phys_end = end + unaccepted_table->phys_base; + + arch_accept_memory(phys_start, phys_end); + end = bitmap_size * unit_size * BITS_PER_BYTE; + } + + /* + * 'start' and 'end' are now both unit_size-aligned. + * Record the range as being unaccepted: + */ + bitmap_set(unaccepted_table->bitmap, + start / unit_size, (end - start) / unit_size); +} + +void accept_memory(phys_addr_t start, phys_addr_t end) +{ + unsigned long range_start, range_end; + unsigned long bitmap_size; + u64 unit_size; + + if (!unaccepted_table) + return; + + unit_size = unaccepted_table->unit_size; + + /* + * Only care for the part of the range that is represented + * in the bitmap. + */ + if (start < unaccepted_table->phys_base) + start = unaccepted_table->phys_base; + if (end < unaccepted_table->phys_base) + return; + + /* Translate to offsets from the beginning of the bitmap */ + start -= unaccepted_table->phys_base; + end -= unaccepted_table->phys_base; + + /* Make sure not to overrun the bitmap */ + if (end > unaccepted_table->size * unit_size * BITS_PER_BYTE) + end = unaccepted_table->size * unit_size * BITS_PER_BYTE; + + range_start = start / unit_size; + bitmap_size = DIV_ROUND_UP(end, unit_size); + + for_each_set_bitrange_from(range_start, range_end, + unaccepted_table->bitmap, bitmap_size) { + unsigned long phys_start, phys_end; + + phys_start = range_start * unit_size + unaccepted_table->phys_base; + phys_end = range_end * unit_size + unaccepted_table->phys_base; + + arch_accept_memory(phys_start, phys_end); + bitmap_clear(unaccepted_table->bitmap, + range_start, range_end - range_start); + } +} diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index fff81843169c..8d17cee8b98e 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -613,6 +613,16 @@ setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_s e820_type = E820_TYPE_PMEM; break; + case EFI_UNACCEPTED_MEMORY: + if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY)) { + efi_warn_once( +"The system has unaccepted memory, but kernel does not support it\nConsider enabling CONFIG_UNACCEPTED_MEMORY\n"); + continue; + } + e820_type = E820_TYPE_RAM; + process_unaccepted_memory(d->phys_addr, + d->phys_addr + PAGE_SIZE * d->num_pages); + break; default: continue; } @@ -697,6 +707,9 @@ static efi_status_t allocate_e820(struct boot_params *params, status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size); } + if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && status == EFI_SUCCESS) + status = allocate_unaccepted_bitmap(nr_desc, map); + efi_bs_call(free_pool, map); return status; } diff --git a/include/linux/efi.h b/include/linux/efi.h index 7aa62c92185f..29cc622910da 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -108,7 +108,8 @@ typedef struct { #define EFI_MEMORY_MAPPED_IO_PORT_SPACE 12 #define EFI_PAL_CODE 13 #define EFI_PERSISTENT_MEMORY 14 -#define EFI_MAX_MEMORY_TYPE 15 +#define EFI_UNACCEPTED_MEMORY 15 +#define EFI_MAX_MEMORY_TYPE 16 /* Attribute values: */ #define EFI_MEMORY_UC ((u64)0x0000000000000001ULL) /* uncached */ @@ -417,6 +418,7 @@ void efi_native_runtime_setup(void); #define LINUX_EFI_MOK_VARIABLE_TABLE_GUID EFI_GUID(0xc451ed2b, 0x9694, 0x45d3, 0xba, 0xba, 0xed, 0x9f, 0x89, 0x88, 0xa3, 0x89) #define LINUX_EFI_COCO_SECRET_AREA_GUID EFI_GUID(0xadf956ad, 0xe98c, 0x484c, 0xae, 0x11, 0xb5, 0x1c, 0x7d, 0x33, 0x64, 0x47) #define LINUX_EFI_BOOT_MEMMAP_GUID EFI_GUID(0x800f683f, 0xd08b, 0x423a, 0xa2, 0x93, 0x96, 0x5c, 0x3c, 0x6f, 0xe2, 0xb4) +#define LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID EFI_GUID(0xd5d1de3c, 0x105c, 0x44f9, 0x9e, 0xa9, 0xbc, 0xef, 0x98, 0x12, 0x00, 0x31) #define RISCV_EFI_BOOT_PROTOCOL_GUID EFI_GUID(0xccd15fec, 0x6f73, 0x4eec, 0x83, 0x95, 0x3e, 0x69, 0xe4, 0xb9, 0x40, 0xbf) @@ -534,6 +536,14 @@ struct efi_boot_memmap { efi_memory_desc_t map[]; }; +struct efi_unaccepted_memory { + u32 version; + u32 unit_size; + u64 phys_base; + u64 size; + unsigned long bitmap[]; +}; + /* * Architecture independent structure for describing a memory map for the * benefit of efi_memmap_init_early(), and for passing context between From patchwork Thu May 18 23:14:29 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: 13247508 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 AB4FFC7EE29 for ; Thu, 18 May 2023 23:15:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C935900006; Thu, 18 May 2023 19:14:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 363FD900009; Thu, 18 May 2023 19:14:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 02F66900006; Thu, 18 May 2023 19:14:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id D7319900007 for ; Thu, 18 May 2023 19:14:57 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AF62D160894 for ; Thu, 18 May 2023 23:14:57 +0000 (UTC) X-FDA: 80804933034.08.322B307 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by imf25.hostedemail.com (Postfix) with ESMTP id 9F7F5A0006 for ; Thu, 18 May 2023 23:14:55 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=WZm4vFtQ; spf=none (imf25.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.88) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684451695; 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=EHtYYH840G6GuJWKO1YmEg6CGyOjwgzbfTxofltA3cM=; b=08MRrNFzfi2KqdTm+Kn0S7IXEPfnFahqzcRPjZ79SXJAw+gBgM2Im+Jn8f147K92ZDxSjL rTaNAtsQl2Qy52WHavaXRj1LEmLuKVCatGluNzS5QJyJYroPaBKDaUo8wDjAtjMvLLhrUl O0jUECMV7LUJipz19AAw7fArC4w6M9Q= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684451695; a=rsa-sha256; cv=none; b=mrZBkqLqctdiPaymKJdBf5hrDJYoagg5D9/EyGu4g/9ZUIO+i9HPiMKpEyAIqr3N8PkBMX SCYgxXSIpTW72JZpJ6cVpLLkx7HUND2/uYwTX6EUAxoIOXaWrA101uY3Y2yY3oXfVKRLTg wvsDCcUxw6JjrMyfcsu7HAaE8MlM3ZM= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=WZm4vFtQ; spf=none (imf25.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.88) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684451695; x=1715987695; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BuLaVtTf5RKQWpr3anFnaNnaMQK4KaotupS5o/iFyhQ=; b=WZm4vFtQqx6AwNl8B3Es47UqNDpfCFQ7UG/3lBj/g+QhZKVDZ3YmB5K1 mVUcug1CIkHoo0z5zejPLc7QSTFm7Znn10rKTvkuhNHKXfvwFs7bTF+Hw uFVVOFWixnkzBZjywlW/lTmFeYSSkN52MkSr9chY0OHtE/0f5c7bdcI2W VT8PG+ozJNljm21wvQ6jeqvrNjapbcZur/hPR4ZoLCQbqAYfeMP5MxcZS 1NRMvtpg3fB5vQdH0XeXBGjqHL7oWILls0StQXnb6+ypY9UzR1H0OkkSo BNWOAh6CUB7VGqg70H10waWKHBxG0OBHo5B4ZGGbgOhbaLuN2ceL21EnB w==; X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="380431740" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="380431740" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2023 16:14:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="846669541" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="846669541" Received: from rkiyama-mobl1.amr.corp.intel.com (HELO box.shutemov.name) ([10.251.222.16]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2023 16:14:42 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 3F46A10DFCF; 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" , Liam Merwick Subject: [PATCHv12 4/9] x86/boot/compressed: Handle unaccepted memory Date: Fri, 19 May 2023 02:14:29 +0300 Message-Id: <20230518231434.26080-5-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-Stat-Signature: 949sda8u4z864g4q5nxtywz93yu9388p X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 9F7F5A0006 X-Rspam-User: X-HE-Tag: 1684451695-655486 X-HE-Meta: U2FsdGVkX18i8bOlWtfAFluaQJ5zM7OO/YTpdEuffnRly4BpwoOsgZ76KTNuUjGfsMuetqNU/GjDZeanFYO9aArOrBYXnvm8U2LwlVpXQfpFtD1hlIpXOqiez/11jOyehhLkdNaaaWT1eodVnj95aSemr1owAQe0YD4wZIc04IpIcpiPpE2M3HbG8Q7bAqJqEUUVrNnPWp1KQ0XemBT+epZI30n6dSYv5VjSfCXH4tKoYWEjqA0Q6s4jLzR1BMXBJd+YU4ttbD2uyEh3NyfdYf1N43xsKrVcf3fJb5pbx4mkZXNIW7CU4WvwON0EDzBgubesU3P91fqvWi0jmNBK6Xm5bIcE4x1j72/6DOJUHbVUILdbyUca3cd7JxG8PzYb8f4kvb+swyeDGMkMIGCshtPXQE5iO0jORmbP3yD1+i6At/DBzVDgRJam2dVTCRw+voO1XXRPhTsksvmr7MAeaNM8+rwiSIk/3K+CfOjR7SONQz/cbSooOXCfTXcztaDEu9usB9uALvdb7f//8Ofw+/7RlhdejOoXLb/f1xNqDmUGk70T3ewVywPHEpp0JLkF5CzvTwwh/WZG+iOvAzAJ9xZSDsLZM3guyJh8ht1mNPXfMJMPuxwQUeHoKGw3EtJDsYM+i3S58uu68D//RHcmOSkgRCxWH81WglXBKxls8VraEGgKupT8+X1kZmkl4A54fBL+JgGNSMqA4l4UE2Nxx04wSh22RC/HNGHf9Lb1eAxJpW6pulgQoLiIcxEzVi67AKs/H/HqtAtXlNZRuamZC84gicevLxnGjYRVzYVpmV8/Z9SKRikYahofDI4hy5QaQRCeVEdtZLK7LFC/cpFV9frxZ+1T+cUtU9V9vZ/v7wdBSVEkm14i4bTacYxI0tnrfE/earWgcB1iY+1+sNiI95Z6M1OwHRS6xoT3u8v6EedWza574DqVwZ7MSIJvgBB++Db515w0RVJ/CtdMpfE XkEnAIeN z/Z6BKbiwA7O13UZDT/VaP2xtBEMoEvNKZKc6r8Zq13XjPq0yJfS5QufUGKIbCOKT3pnVz8NzpvTyGQXAxUFvjIWj1YsBrRqY65iqhpek/4muwj1RTfIjBMbTYrdsysM8uRxyYj5YIe8PsmHhQcbC2P4Ud1FYeJT/xVVWXHwaej3H8O8O+8sZX3wU9pN3R+5qK0TB 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: The firmware will pre-accept the memory used to run the stub. But, the stub is responsible for accepting the memory into which it decompresses the main kernel. Accept memory just before decompression starts. The stub is also responsible for choosing a physical address in which to place the decompressed kernel image. The KASLR mechanism will randomize this physical address. Since the accepted memory region is relatively small, KASLR would be quite ineffective if it only used the pre-accepted area (EFI_CONVENTIONAL_MEMORY). Ensure that KASLR randomizes among the entire physical address space by also including EFI_UNACCEPTED_MEMORY. Signed-off-by: Kirill A. Shutemov Reviewed-by: Liam Merwick Reviewed-by: Tom Lendacky Reviewed-by: Ard Biesheuvel --- arch/x86/boot/compressed/efi.h | 10 +++++++++ arch/x86/boot/compressed/kaslr.c | 35 +++++++++++++++++++++----------- arch/x86/boot/compressed/mem.c | 23 +++++++++++++++++++++ arch/x86/boot/compressed/misc.c | 7 +++++++ arch/x86/boot/compressed/misc.h | 6 ++++++ 5 files changed, 69 insertions(+), 12 deletions(-) diff --git a/arch/x86/boot/compressed/efi.h b/arch/x86/boot/compressed/efi.h index 7db2f41b54cd..866c0af8b5b9 100644 --- a/arch/x86/boot/compressed/efi.h +++ b/arch/x86/boot/compressed/efi.h @@ -16,6 +16,7 @@ typedef guid_t efi_guid_t __aligned(__alignof__(u32)); #define ACPI_TABLE_GUID EFI_GUID(0xeb9d2d30, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d) #define ACPI_20_TABLE_GUID EFI_GUID(0x8868e871, 0xe4f1, 0x11d3, 0xbc, 0x22, 0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81) #define EFI_CC_BLOB_GUID EFI_GUID(0x067b1f5f, 0xcf26, 0x44c5, 0x85, 0x54, 0x93, 0xd7, 0x77, 0x91, 0x2d, 0x42) +#define LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID EFI_GUID(0xd5d1de3c, 0x105c, 0x44f9, 0x9e, 0xa9, 0xbc, 0xef, 0x98, 0x12, 0x00, 0x31) #define EFI32_LOADER_SIGNATURE "EL32" #define EFI64_LOADER_SIGNATURE "EL64" @@ -32,6 +33,7 @@ typedef struct { } efi_table_hdr_t; #define EFI_CONVENTIONAL_MEMORY 7 +#define EFI_UNACCEPTED_MEMORY 15 #define EFI_MEMORY_MORE_RELIABLE \ ((u64)0x0000000000010000ULL) /* higher reliability */ @@ -104,6 +106,14 @@ struct efi_setup_data { u64 reserved[8]; }; +struct efi_unaccepted_memory { + u32 version; + u32 unit_size; + u64 phys_base; + u64 size; + unsigned long bitmap[]; +}; + static inline int efi_guidcmp (efi_guid_t left, efi_guid_t right) { return memcmp(&left, &right, sizeof (efi_guid_t)); diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index 454757fbdfe5..749f0fe7e446 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -672,6 +672,28 @@ static bool process_mem_region(struct mem_vector *region, } #ifdef CONFIG_EFI + +/* + * Only EFI_CONVENTIONAL_MEMORY and EFI_UNACCEPTED_MEMORY (if supported) are + * guaranteed to be free. + * + * It is more conservative in picking free memory than the EFI spec allows: + * + * According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also free memory + * and thus available to place the kernel image into, but in practice there's + * firmware where using that memory leads to crashes. + */ +static inline bool memory_type_is_free(efi_memory_desc_t *md) +{ + if (md->type == EFI_CONVENTIONAL_MEMORY) + return true; + + if (md->type == EFI_UNACCEPTED_MEMORY) + return IS_ENABLED(CONFIG_UNACCEPTED_MEMORY); + + return false; +} + /* * Returns true if we processed the EFI memmap, which we prefer over the E820 * table if it is available. @@ -716,18 +738,7 @@ process_efi_entries(unsigned long minimum, unsigned long image_size) for (i = 0; i < nr_desc; i++) { md = efi_early_memdesc_ptr(pmap, e->efi_memdesc_size, i); - /* - * Here we are more conservative in picking free memory than - * the EFI spec allows: - * - * According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also - * free memory and thus available to place the kernel image into, - * but in practice there's firmware where using that memory leads - * to crashes. - * - * Only EFI_CONVENTIONAL_MEMORY is guaranteed to be free. - */ - if (md->type != EFI_CONVENTIONAL_MEMORY) + if (!memory_type_is_free(md)) continue; if (efi_soft_reserve_enabled() && diff --git a/arch/x86/boot/compressed/mem.c b/arch/x86/boot/compressed/mem.c index 67594fcb11d9..87372b96d613 100644 --- a/arch/x86/boot/compressed/mem.c +++ b/arch/x86/boot/compressed/mem.c @@ -1,9 +1,32 @@ // SPDX-License-Identifier: GPL-2.0-only #include "error.h" +#include "misc.h" void arch_accept_memory(phys_addr_t start, phys_addr_t end) { /* Platform-specific memory-acceptance call goes here */ error("Cannot accept memory"); } + +void init_unaccepted_memory(void) +{ + guid_t guid = LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID; + struct efi_unaccepted_memory *unaccepted_table; + unsigned long cfg_table_pa; + unsigned int cfg_table_len; + int ret; + + ret = efi_get_conf_table(boot_params, &cfg_table_pa, &cfg_table_len); + if (ret) + error("EFI config table not found."); + + unaccepted_table = (void *)efi_find_vendor_table(boot_params, + cfg_table_pa, + cfg_table_len, + guid); + if (unaccepted_table->version != 1) + error("Unknown version of unaccepted memory table\n"); + + set_unaccepted_table(unaccepted_table); +} diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c index 014ff222bf4b..36535a3753f5 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -455,6 +455,13 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, #endif debug_putstr("\nDecompressing Linux... "); + + if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY)) { + debug_putstr("Accepting memory... "); + init_unaccepted_memory(); + accept_memory(__pa(output), __pa(output) + needed_size); + } + __decompress(input_data, input_len, NULL, NULL, output, output_len, NULL, error); entry_offset = parse_elf(output); diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index 2f155a0e3041..e1a0b49e0ed2 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -247,4 +247,10 @@ static inline unsigned long efi_find_vendor_table(struct boot_params *bp, } #endif /* CONFIG_EFI */ +void init_unaccepted_memory(void); + +/* Implemented in EFI stub */ +void set_unaccepted_table(struct efi_unaccepted_memory *table); +void accept_memory(phys_addr_t start, phys_addr_t end); + #endif /* BOOT_COMPRESSED_MISC_H */ 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; From patchwork Thu May 18 23:14:31 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: 13247512 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 5712DC77B7A for ; Thu, 18 May 2023 23:15:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CB62D90000C; Thu, 18 May 2023 19:15:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C4324900009; Thu, 18 May 2023 19:15:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A444990000D; Thu, 18 May 2023 19:15:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8C58E90000C for ; Thu, 18 May 2023 19:15:07 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 69F18A054C for ; Thu, 18 May 2023 23:15:07 +0000 (UTC) X-FDA: 80804933454.13.30ABAC5 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf16.hostedemail.com (Postfix) with ESMTP id 2CFF3180010 for ; Thu, 18 May 2023 23:15:04 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=RS08nMGa; spf=none (imf16.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; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684451705; 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=oGRDwK5r+gHSrZclkxM5aTLGsgT2GEXWwSoW4pfzbqM=; b=sd+DJE+1qFBgnSyDjTO442XOwL8R9EkwSIdwSPgEWmWb9Z0+n7TI83Dh9eg02VSr7joCjN TJE9xaMWAOO/wtyLJP8XdTOmy8rqDtk4u40+gkGCADQTKuqBCTVoV9rQmZmIF3gWJ5XfFz I8yhtUfni6Dea6BArXuJKNxvu/8QnNk= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=RS08nMGa; spf=none (imf16.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; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684451705; a=rsa-sha256; cv=none; b=hymRIkREw6QfRVvRLMzzYM11vDOOsW9+P18C4kGNKHCzLCA2jj2dP0R1Z5X76Qmh55HrL5 8aKk77cx5DENkpRmVjfCmqaq7D5diTOYbgpY2TbILLbnulWqOK7tchy4/XWAEB6W5txv6A Qz+a/+N9nMBommlCDfRpA3d3tiIqpbY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684451705; x=1715987705; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=aHz7WO7wShdd679ArCfVvIALDE990d9+23AGlVsKS9M=; b=RS08nMGamIcZd/L/EFr5pBHCLZBqr9tpByoymGLKhFtMmXpabA8W7Pi/ 3UYqvN1c6Llv1JoMGHSCfE3ydpjxHCkRV72lRV8tEBRQ1L4nS/TWyOoI+ BTZkid7yjuZkqURkGtQILugVuiPdiTACLKg6aPMZDAypNVEE9GRsgPtBx zUss+a3aEU82EnvtNRG7fWZcn/fM8Vg1Xik24lblG+C2/Q74VBf24/pVp AZIOs+Z5nQNvqoGgFmEHUUxi13aEZvdbYJ2S0ZT1v7/WVflTSgjNG980T DZJNKhc23xWDZvtLScISTyD3UvwzW87z77N19x0Xtgf2fJPXDcGyA+Bzl g==; X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="341652142" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="341652142" 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:15:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="772065105" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="772065105" 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 5530610DFD2; 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" , Dave Hansen Subject: [PATCHv12 6/9] efi/unaccepted: Avoid load_unaligned_zeropad() stepping into unaccepted memory Date: Fri, 19 May 2023 02:14:31 +0300 Message-Id: <20230518231434.26080-7-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-Rspamd-Queue-Id: 2CFF3180010 X-Rspam-User: X-Stat-Signature: wsphs5ah17j1y9w4mjiq8xkegu17cjka X-Rspamd-Server: rspam01 X-HE-Tag: 1684451704-143954 X-HE-Meta: U2FsdGVkX19l0O+ZXCiI4+NZFecev8ZZFfqLJQsXqmALU2EcsmjgAf5pIoHNyybZbLekTIS7P+EX5LEF4lCvLQLZnyhVJInx+GMQRod/s7QRyGDWjdz5aAPBWERllPATjz7zE4yf0YA4F8EKSTtodgT6Zig1AcFMxRxptNuKTKH7JIPu+2faOpWq7OwGpg0pTZZ11yAGWMSitvJVdI5fvNVT+hK1sDAZnO058kqTH+tJt9m5Xu0JeqUAoh0wK4bWepQTl4AGDuXzuCF2vtmKStghw7ZH29AH5yqwqgOUuNEnVkgdp0rSQVWGm6Jr6hnLkvAHn30fh2ZH04M1k9X/2lLiy3GJhJVGMKxGfuLwSBIVijHmO0bYkQNj3+HucYo3ykz8fuh6do2LnDDu7FjzLy36nNdwpFV/dxaCTvkS7V9ptGX+L3srNKdVKd+MfX3K9S93YLKAJC4d7Q9mW0N7DM6/Z9TDleKEnQuolQOo1mLSKZphkQQO2ycmzBJ1I1UsfZpy7InpGBOf6iAVZqInM0QVCjPBX/vYGUFeI8UUbBj7tSa+MLuj32Ci2TECd032gIG+tSqpDj7JQsROXIEdzNZ1tElEKXE2wjJdz/JZ9GRBDlLZ83DKE+KvWIddZAS7GtVGSC5MB81VhfdGxeax/oBe3d16zqs5hd9nNMWqsmnZZtcmKorZuiEQuDYN7HBbpWj2GWcsxlQQDWf6i+GgqLgI4Zy9+rk2TBvWBQe+IzRKCutigsIDRZzq2ph6j/ZHqcsRT1Lubztq4ykqDNLYas4tAsgCYBwiHQ2hoXkSRbYp/82vQ+loB3Qgj+phbaTuS3C/A4z7jQwHT9mEXTOqqPjTFahrVGTAdkic9H/QEaid86S77b/B8A8iR8+uKflrxNqoXssWP1TWJVNG6wYGWe7bwgPx8do+GzSpUkW799HF9WwY3riAwwwENLWte7dAKYlYCAyl35H+FXNEPZS itD42ubp XMZwr0NWobklETFsukA86Ka9EoahEPO1pk8n89y/8BIte3siz44YR9yuZwW/7JfW5CMIQ/GzIvExSMuDphJsuT++mGrm5ENkFiF6od0e5Z2vOYViAYHuH0Q5cxb7pTZFKc23BUOoy2oEqJRzZwS+JIOCHxscsK6z5xzLoKcOxrnVBJmxpZGtaGlTHCpEcuaXkxEtZEAzIQ3Tb7+oiAZhNRKff4qrpWfFoFPkZgfxLPYQ2lwwb8KTfvWh2EENCedyAcG8nvyFVyMAgvdI= 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: load_unaligned_zeropad() can lead to unwanted loads across page boundaries. The unwanted loads are typically harmless. But, they might be made to totally unrelated or even unmapped memory. load_unaligned_zeropad() relies on exception fixup (#PF, #GP and now #VE) to recover from these unwanted loads. But, this approach does not work for unaccepted memory. For TDX, a load from unaccepted memory will not lead to a recoverable exception within the guest. The guest will exit to the VMM where the only recourse is to terminate the guest. There are two parts to fix this issue and comprehensively avoid access to unaccepted memory. Together these ensure that an extra "guard" page is accepted in addition to the memory that needs to be used. 1. Implicitly extend the range_contains_unaccepted_memory(start, end) checks up to end+unit_size if 'end' is aligned on a unit_size boundary. 2. Implicitly extend accept_memory(start, end) to end+unit_size if 'end' is aligned on a unit_size boundary. Side note: This leads to something strange. Pages which were accepted at boot, marked by the firmware as accepted and will never _need_ to be accepted might be on unaccepted_pages list This is a cue to ensure that the next page is accepted before 'page' can be used. This is an actual, real-world problem which was discovered during TDX testing. Signed-off-by: Kirill A. Shutemov Reviewed-by: Dave Hansen Reviewed-by: Ard Biesheuvel Reviewed-by: Tom Lendacky --- drivers/firmware/efi/unaccepted_memory.c | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/drivers/firmware/efi/unaccepted_memory.c b/drivers/firmware/efi/unaccepted_memory.c index bb91c41f76fb..3d1ca60916dd 100644 --- a/drivers/firmware/efi/unaccepted_memory.c +++ b/drivers/firmware/efi/unaccepted_memory.c @@ -37,6 +37,34 @@ void accept_memory(phys_addr_t start, phys_addr_t end) start -= unaccepted->phys_base; end -= unaccepted->phys_base; + /* + * load_unaligned_zeropad() can lead to unwanted loads across page + * boundaries. The unwanted loads are typically harmless. But, they + * might be made to totally unrelated or even unmapped memory. + * load_unaligned_zeropad() relies on exception fixup (#PF, #GP and now + * #VE) to recover from these unwanted loads. + * + * But, this approach does not work for unaccepted memory. For TDX, a + * load from unaccepted memory will not lead to a recoverable exception + * within the guest. The guest will exit to the VMM where the only + * recourse is to terminate the guest. + * + * There are two parts to fix this issue and comprehensively avoid + * access to unaccepted memory. Together these ensure that an extra + * "guard" page is accepted in addition to the memory that needs to be + * used: + * + * 1. Implicitly extend the range_contains_unaccepted_memory(start, end) + * checks up to end+unit_size if 'end' is aligned on a unit_size + * boundary. + * + * 2. Implicitly extend accept_memory(start, end) to end+unit_size if + * 'end' is aligned on a unit_size boundary. (immediately following + * this comment) + */ + if (!(end % unit_size)) + end += unit_size; + /* Make sure not to overrun the bitmap */ if (end > unaccepted->size * unit_size * BITS_PER_BYTE) end = unaccepted->size * unit_size * BITS_PER_BYTE; @@ -84,6 +112,13 @@ bool range_contains_unaccepted_memory(phys_addr_t start, phys_addr_t end) start -= unaccepted->phys_base; end -= unaccepted->phys_base; + /* + * Also consider the unaccepted state of the *next* page. See fix #1 in + * the comment on load_unaligned_zeropad() in accept_memory(). + */ + if (!(end % unit_size)) + end += unit_size; + /* Make sure not to overrun the bitmap */ if (end > unaccepted->size * unit_size * BITS_PER_BYTE) end = unaccepted->size * unit_size * BITS_PER_BYTE; From patchwork Thu May 18 23:14:32 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: 13247513 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 2FFFCC77B73 for ; Thu, 18 May 2023 23:15:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9D05390000D; Thu, 18 May 2023 19:15:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 95A43900009; Thu, 18 May 2023 19:15:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7380390000D; Thu, 18 May 2023 19:15:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 588C8900009 for ; Thu, 18 May 2023 19:15:09 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 246D21C78B7 for ; Thu, 18 May 2023 23:15:09 +0000 (UTC) X-FDA: 80804933538.05.4962B5A Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf05.hostedemail.com (Postfix) with ESMTP id 1107810000B for ; Thu, 18 May 2023 23:15:06 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="c7/jejpI"; 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=1684451707; 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=cAZ5dclHmr3bPqqN6wlS/ms38HkegOt+k+1Qe8V3J/Y=; b=uVHjELikxoOaDgGW4CMqaJ7lciEfeAcYnjNWZZuCfdbaOb6ImjvWQokfQzCtZEfApZvMAn V37to9sfqrwk9B7FLazBVDMwsPU7xZ5FMQBgteKpIav87QmQAa6hMvLTcE09BBol/RxZkn UgxDf0ty5SN3iMk/a6YH1RL/wRfKF+Y= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="c7/jejpI"; 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=1684451707; a=rsa-sha256; cv=none; b=cpMtrNhaNarvSAZemTY68FmodHSHYgxQIsRxlnnRMd/7Vl8GOn1NS2hvv+EeL6ijPg/WDf 62A66ycQnrxkmrGhOd1610W1yMclGFtHhDmllinVI+t+SYedTPZcPRDJQR5P9PxdyvmQQX 0Hf4WS40nwYXWoSS11ogv/ZHQy9m4zA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684451707; x=1715987707; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Eu57mUO9ct2ICW4Etq/GrS2vGWKQMEDS8IVWtzUCpBo=; b=c7/jejpIlTcZsq0kzlwlZbnEm0mg8apDPq1pMIyxFoYAA+wt2K6gMZc3 kXj+9KQpYdqdczUcs1UdlKOizU4kVYbePI2s2GFvHj36vUBr2d0+LLRor rCAwvPwu0EK7scYRgq0EfDfvZLs/rG+aBWq7W3vvRPYFgpaC+87gSs9vG dnvBjEObFoEQfLYaR7FQOiflprLzVDUWTHnTWepVWEoy27XUbJBptO0AQ qRG/NvHix3lwRXnwv9nLkdcJSUJjZQ1QBzxX4a2DBtCuzlbxuE3aGttV9 sZ6YFxEmt8foJpXkzLyUTwhvAK+KO//ahpZOY46ewCRzH8vbwpYDlnyEC g==; X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="341652156" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="341652156" 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:15:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="772065107" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="772065107" 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 6149C10DFD4; 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" , Dave Hansen Subject: [PATCHv12 7/9] x86/tdx: Make _tdx_hypercall() and __tdx_module_call() available in boot stub Date: Fri, 19 May 2023 02:14:32 +0300 Message-Id: <20230518231434.26080-8-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: 1107810000B X-Stat-Signature: xmbzx313rcyqbtwa7irxde88a1rnsdoc X-HE-Tag: 1684451706-66272 X-HE-Meta: U2FsdGVkX19hxr8Kf7MQ4a+2OfPA9xyAWe5SRtTCG7WSaLl645YpZmGZJ4bk4m97SAvhPO19TF7Dp56b1DplDo2uht/+EtI+dv51kuCNepqeWTFq087k9KLfzKgNkXxH5w+ijVjF4vdTWm/iKoIJrHScwH3JA9f2Ctr0UHl0Sn8GR2Giy4TcMFwhh45u0DDRyLksw53ztqbZ8IG7pUWa4qNE2Oqe+GAtqNoH+2mLqeZTQudiDQBTMadsRWOK7nqqhZiwLoDMkeIX8gl+NYN+u2unxvQSJ3gZBo3zEghy00j6nDDKpahwBT6oiWA6GuoQvOtXCW3914lKyljMN8/x2Cdi0Zr4b6W9gFIdxmY9BO7uGdiT6nZ1sX72r0uLzCI0ZItOyX4CVf0/7Eer023fMpzBbWCnF2V10oQbrxAkbrSQ5sEKTl3m3bHahMl2PhZlMh5/bj0d1JX+xc0fm/ZAd+fEzholxmvLz8Lhbdf1exonClAh2VvZbiXstYQkMgc2it4imvRQzGT0cY6Iiav+WPUkCbrL4ugn3qwW0lgpwbLDMrJDdhDSw4YO0Kn4gfZe/4Q4W6ybMNkcXpGVoPtRMKa/motEuotd0OlOe8/CQ6c8SZgYelmCbPYFobt/DcO2v20nYF7k80QX4V+EmhZC2PfJ1RgaSjwKQwEmlu3nXVkIRD7wPDP/m3PO2hVX98kZMo0GZNzX2bh/5G89KOUv6BXzUxBpA0usK9yNR44LrWHpWevWrNupS3DLZJau6nMNOzmvzXTVUY1Ujn/R+Zlw6V+FeyetGp4FuQ386O8RQ24gd2LMVf8geNlEfoERuMPjytM9nwARMUoEqbC99Y38J1kzJsDSRIooCs3r1m/7g5qKZ75fVIVC7WC3cItzurce3WdNFwmoxgF4NSok6Rd0Eret7EVIK+wnjIOBudXCsbUBJVxyPBzsxNeM7LZCiOXO8dDfaEwD4tyyBcWFKvo gU7VC/Cb pMSRT9ygLMuF6AmqVv1BHxsi1uymadQ+uUyc8wiGTMFWUOypp2QbQ97t33L407fRZ6NJg+9Q1n/n7ywusDDxYDI3Q3NgfDI6883hZhqHON4uYzZL3gzCsxLUhRrG2/wj0bVtOgp8iZnTKbSK1ppQIiZ+3XWQjB56fn9/T0IBlG6Gn9wbCWiIT9IxxOLp7aVWe7iQ7 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: Memory acceptance requires a hypercall and one or multiple module calls. Make helpers for the calls available in boot stub. It has to accept memory where kernel image and initrd are placed. Signed-off-by: Kirill A. Shutemov Reviewed-by: Dave Hansen --- arch/x86/coco/tdx/tdx.c | 32 ------------------- arch/x86/include/asm/shared/tdx.h | 51 +++++++++++++++++++++++++++++++ arch/x86/include/asm/tdx.h | 19 ------------ 3 files changed, 51 insertions(+), 51 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index e146b599260f..e6f4c2758a68 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -14,20 +14,6 @@ #include #include -/* TDX module Call Leaf IDs */ -#define TDX_GET_INFO 1 -#define TDX_GET_VEINFO 3 -#define TDX_GET_REPORT 4 -#define TDX_ACCEPT_PAGE 6 -#define TDX_WR 8 - -/* TDCS fields. To be used by TDG.VM.WR and TDG.VM.RD module calls */ -#define TDCS_NOTIFY_ENABLES 0x9100000000000010 - -/* TDX hypercall Leaf IDs */ -#define TDVMCALL_MAP_GPA 0x10001 -#define TDVMCALL_REPORT_FATAL_ERROR 0x10003 - /* MMIO direction */ #define EPT_READ 0 #define EPT_WRITE 1 @@ -51,24 +37,6 @@ #define TDREPORT_SUBTYPE_0 0 -/* - * Wrapper for standard use of __tdx_hypercall with no output aside from - * return code. - */ -static inline u64 _tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15) -{ - struct tdx_hypercall_args args = { - .r10 = TDX_HYPERCALL_STANDARD, - .r11 = fn, - .r12 = r12, - .r13 = r13, - .r14 = r14, - .r15 = r15, - }; - - return __tdx_hypercall(&args); -} - /* Called from __tdx_hypercall() for unrecoverable failure */ noinstr void __tdx_hypercall_failed(void) { diff --git a/arch/x86/include/asm/shared/tdx.h b/arch/x86/include/asm/shared/tdx.h index 2631e01f6e0f..1ff0ee822961 100644 --- a/arch/x86/include/asm/shared/tdx.h +++ b/arch/x86/include/asm/shared/tdx.h @@ -10,6 +10,20 @@ #define TDX_CPUID_LEAF_ID 0x21 #define TDX_IDENT "IntelTDX " +/* TDX module Call Leaf IDs */ +#define TDX_GET_INFO 1 +#define TDX_GET_VEINFO 3 +#define TDX_GET_REPORT 4 +#define TDX_ACCEPT_PAGE 6 +#define TDX_WR 8 + +/* TDCS fields. To be used by TDG.VM.WR and TDG.VM.RD module calls */ +#define TDCS_NOTIFY_ENABLES 0x9100000000000010 + +/* TDX hypercall Leaf IDs */ +#define TDVMCALL_MAP_GPA 0x10001 +#define TDVMCALL_REPORT_FATAL_ERROR 0x10003 + #ifndef __ASSEMBLY__ /* @@ -37,8 +51,45 @@ struct tdx_hypercall_args { u64 __tdx_hypercall(struct tdx_hypercall_args *args); u64 __tdx_hypercall_ret(struct tdx_hypercall_args *args); +/* + * Wrapper for standard use of __tdx_hypercall with no output aside from + * return code. + */ +static inline u64 _tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15) +{ + struct tdx_hypercall_args args = { + .r10 = TDX_HYPERCALL_STANDARD, + .r11 = fn, + .r12 = r12, + .r13 = r13, + .r14 = r14, + .r15 = r15, + }; + + return __tdx_hypercall(&args); +} + + /* Called from __tdx_hypercall() for unrecoverable failure */ void __tdx_hypercall_failed(void); +/* + * Used in __tdx_module_call() to gather the output registers' values of the + * TDCALL instruction when requesting services from the TDX module. This is a + * software only structure and not part of the TDX module/VMM ABI + */ +struct tdx_module_output { + u64 rcx; + u64 rdx; + u64 r8; + u64 r9; + u64 r10; + u64 r11; +}; + +/* Used to communicate with the TDX module */ +u64 __tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9, + struct tdx_module_output *out); + #endif /* !__ASSEMBLY__ */ #endif /* _ASM_X86_SHARED_TDX_H */ diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index 28d889c9aa16..234197ec17e4 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -20,21 +20,6 @@ #ifndef __ASSEMBLY__ -/* - * Used to gather the output registers values of the TDCALL and SEAMCALL - * instructions when requesting services from the TDX module. - * - * This is a software only structure and not part of the TDX module/VMM ABI. - */ -struct tdx_module_output { - u64 rcx; - u64 rdx; - u64 r8; - u64 r9; - u64 r10; - u64 r11; -}; - /* * Used by the #VE exception handler to gather the #VE exception * info from the TDX module. This is a software only structure @@ -55,10 +40,6 @@ struct ve_info { void __init tdx_early_init(void); -/* Used to communicate with the TDX module */ -u64 __tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9, - struct tdx_module_output *out); - void tdx_get_ve_info(struct ve_info *ve); bool tdx_handle_virt_exception(struct pt_regs *regs, struct ve_info *ve); From patchwork Thu May 18 23:14:33 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: 13247511 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 83F4DC77B73 for ; Thu, 18 May 2023 23:15:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8495690000B; Thu, 18 May 2023 19:15:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D354900009; Thu, 18 May 2023 19:15:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5D76090000B; Thu, 18 May 2023 19:15:07 -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 41A03900009 for ; Thu, 18 May 2023 19:15:07 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id E436FAE414 for ; Thu, 18 May 2023 23:15:06 +0000 (UTC) X-FDA: 80804933412.19.CD19028 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf05.hostedemail.com (Postfix) with ESMTP id D216210000B for ; Thu, 18 May 2023 23:15:03 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=idvJUdtF; 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=1684451704; 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=5QNweM8SAaqY0llbQtMpYPXKIrYhie9z8hDfHUvFa6Y=; b=ZSfKxAtwTteWFUtVi4dkqXkG9jnF1t2smrbg6SY5OQYRSjxCw1CYh4Rg578hEMQC+6P4s5 h8X9LjFK9X4wjJYkKAtO0Ziau9m3Je5XCPH4KF9j03xYw8d1f3NNdihySmRNqR2UIiPZzD C52w4crQEHrm9OquKCctqjt9UflwEhY= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=idvJUdtF; 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=1684451704; a=rsa-sha256; cv=none; b=r85CvABwGY+G7jKOeZHosE0TeDCX7QwmKZEeljKKO2YQ7LSd972Qk9FMV01C1MonzwS3EU cH/7tm6fmfJfRJbfzJ5PwKUAhzeH0RbD8vpY4bhE8hT/T343FEzgfC+Hnl5lcmTevqXY+Q RrN5l62XYMubRYj++ODuAUhxhPWqCa4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684451703; x=1715987703; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZRWbbgKJS3P7pe8ruC5AYEzSpFJMHJrlR+e2/skszlg=; b=idvJUdtFGGXroHMxtHb8rHYNPhDbuvhY/8m3b3wSoenZvFT15+ON/xLj +N/VyzM5bkGKDIJAupu6i9wZsdTt97me/EsrstLTQXZBYBoSPDRUNLxqF pr8OkOIcKvihvUQHd1vOlKzPfpPWmiNMBL8WTp1uQbSBHwb8mVv50oilT z7Shumpy/ezU90tzfFdEYnYeOTuA8ntGWn1d/dPkTbnddySTVz55c0d1a NbdBG07ZAb4lXiBY3LKna8IMql9gbzRL1F+gaOFgsIn/loeKUiZWxvnAG a4n4U+vtruazGI3EmkMBUM6PH3Enfjo49Uv89O/67GXOWF7K+bBTPp/Ry A==; X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="341652126" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="341652126" 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:15:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="772065103" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="772065103" 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 6D73610DFD6; 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" , Dave Hansen Subject: [PATCHv12 8/9] x86/tdx: Refactor try_accept_one() Date: Fri, 19 May 2023 02:14:33 +0300 Message-Id: <20230518231434.26080-9-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: D216210000B X-Stat-Signature: wekcqcpz7fs6oiuujqhur7adfecgob8o X-HE-Tag: 1684451703-252579 X-HE-Meta: U2FsdGVkX18taP9D/Yp9mpvgrJ4RxtarOufw1q32ePSHAjHggrFqVM2H9Qv7O7WNeCBz8KWvVH/9++bBln7f+PApjYDmIMgr9LeQT4XJ0gxggPfQAzW5IAdt9FsI3XY/KEiJNNrIH6joI84IVp8a2yBWQaRPpslDXgdDHmdmr6nBCChqhVmBKQFKUneFq/e2DTDoa1vfR4IR1LVefDxy8XQHhBKCSTtDECI+WXR141NVncQSSRbXZ4OdfRfIbQl+ucjyIoZTs/VrorO+pflv5Xq44xz8Z7g23aWDxXlXu74zX2Et2IIuiwPU4ujCbHn+sCkQ434Npfz5qIJBJIEeODY3w/4RHu5QUC940AY/aDIDxvQ8qJNbHEDovnOl+4S8KBlSF2pxiIc1VPAc+OL/v9L+bhB+BXf21dYhSistve0XlnPbt2WEQj4VzIl4DFgwh7ZXZopdEZKwIlK2JIuaBI2B8AigLT6V9/L+tnaM5P31JafMat49/0lxYEe03X3ZNfHtTzbHHbQrixztP4XBaGM8jSDm0JymHkCNXQJN71oF3SM24m5JXZKRf9h2eaOpv4n8SFzOneBC4rXy+GfwMtd++uKjqCcmGg5qjnldqW8jmcsIP1GH1tA4XjbllQCox7uKapttsyosfIGckvU3SgxUeJqIMRbUqHB2X8g2x23xeIEdrawfGIGqdU4CS7DBiJoEjuV7IgxvfKw88KnfjItadPwmXTeESSd7ndywN7cM8QTo0/PlkcAFrFlFF/vPIknl5ZSjHAbOXxvtWb6YYIUH3AnlOd4SfvSBB/AU00j7EkqtXIOdN4h6HCJAxI1qCrNhEWzInKbgnVUZhu5qWNg9vAM0Q65CbhlI8oyvr8xvhx6jtBWFmNeQtW5DQBCr+E4eiR1x7VmtWcPuOcpavNcmk5wfOaQK8LYOj4v35cH16DFOhyXLladIy3ZoJLXRVIQsG78/M7KXNkT6x8W mg5vSg4M YOUu3wcv2vhvm8e0K29zXPLEj/AT6nvWLlDEXRj6OqvGQh0AiENL9vivKiC8MusyVtHJLr/wRvOHEJZAf0OjjISTNQr+DHb+VbBwH+bpi/63VG5KGoAuC0z9XU2hDgs9HuN7lwdR39UBx+b0rW6ltNb99knHrk6vIwBOVsNgvX9BbR5jNhUDxyruYSA== 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: Rework try_accept_one() to return accepted size instead of modifying 'start' inside the helper. It makes 'start' in-only argument and streamlines code on the caller side. Signed-off-by: Kirill A. Shutemov Suggested-by: Borislav Petkov Reviewed-by: Dave Hansen --- arch/x86/coco/tdx/tdx.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index e6f4c2758a68..0d5fe6e24e45 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -713,18 +713,18 @@ static bool tdx_cache_flush_required(void) return true; } -static bool try_accept_one(phys_addr_t *start, unsigned long len, - enum pg_level pg_level) +static unsigned long try_accept_one(phys_addr_t start, unsigned long len, + enum pg_level pg_level) { unsigned long accept_size = page_level_size(pg_level); u64 tdcall_rcx; u8 page_size; - if (!IS_ALIGNED(*start, accept_size)) - return false; + if (!IS_ALIGNED(start, accept_size)) + return 0; if (len < accept_size) - return false; + return 0; /* * Pass the page physical address to the TDX module to accept the @@ -743,15 +743,14 @@ static bool try_accept_one(phys_addr_t *start, unsigned long len, page_size = 2; break; default: - return false; + return 0; } - tdcall_rcx = *start | page_size; + tdcall_rcx = start | page_size; if (__tdx_module_call(TDX_ACCEPT_PAGE, tdcall_rcx, 0, 0, 0, NULL)) - return false; + return 0; - *start += accept_size; - return true; + return accept_size; } /* @@ -788,21 +787,22 @@ static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc) */ while (start < end) { unsigned long len = end - start; + unsigned long accept_size; /* * Try larger accepts first. It gives chance to VMM to keep - * 1G/2M SEPT entries where possible and speeds up process by - * cutting number of hypercalls (if successful). + * 1G/2M Secure EPT entries where possible and speeds up + * process by cutting number of hypercalls (if successful). */ - if (try_accept_one(&start, len, PG_LEVEL_1G)) - continue; - - if (try_accept_one(&start, len, PG_LEVEL_2M)) - continue; - - if (!try_accept_one(&start, len, PG_LEVEL_4K)) + accept_size = try_accept_one(start, len, PG_LEVEL_1G); + if (!accept_size) + accept_size = try_accept_one(start, len, PG_LEVEL_2M); + if (!accept_size) + accept_size = try_accept_one(start, len, PG_LEVEL_4K); + if (!accept_size) return false; + start += accept_size; } return true; From patchwork Thu May 18 23:14:34 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: 13247509 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 C1F6EC77B7A for ; Thu, 18 May 2023 23:15:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C1A4790000A; Thu, 18 May 2023 19:15:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BCC8C900007; Thu, 18 May 2023 19:15:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9CEB490000A; Thu, 18 May 2023 19:15:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 84944900007 for ; Thu, 18 May 2023 19:15:03 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 12D7AC08EA for ; Thu, 18 May 2023 23:15:03 +0000 (UTC) X-FDA: 80804933286.13.13230DF Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by imf25.hostedemail.com (Postfix) with ESMTP id B676CA0004 for ; Thu, 18 May 2023 23:15:00 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=a5lqeR9l; spf=none (imf25.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.88) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=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=wQ0PMJnplfjYimd9FmbieRCCg+9TBNd1HkELhTSazkk=; b=G4bxTCZAw3rHrS1abprrUTjYrW8fzIJ3Vn7SKxYhSG7ULl42kz4qnDZh341LVKIiNxZjbs 8khYBZ32zNe2jHUzDn7BmlMj39JOdfcQziTCSg+KvGF15P5Mz9eOAIhcA1nrJFc6gP6Bj0 +MyydY/c3BuoEGG4KzpUgYE6xZadznE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684451701; a=rsa-sha256; cv=none; b=OnhE/vZYshHR2tjAFq4o5QPBMIay9dau2HUJQDI+F1ymtWKf/QqESQrw2mRR1LQ8vpFVaZ QkdD7XEPobwc7Y9Be/ExsqyDZjFfNRyAgzATArbWdEWf+nVXYqD/m9x1jBZNYkptbguFu9 K43NcWDLCKFCTr8MgSSKG1Ju5j0Adx8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=a5lqeR9l; spf=none (imf25.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.88) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684451700; x=1715987700; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WocS7y0ges2OdJqRz+xrLKS0zSZ/Dr1Rwu72pFULcnE=; b=a5lqeR9lkCF/b49zajsBBGZJ570A++zhilY1XsnlKTFNnIHJA3xI2kQt NlcWIU8i9meGXplazfuWkb3UU7ovlFHGlGERfgGphPHO1UK/DJoYWr/GR yf8dVKgcM7aCvMn/atz0Jpl3Bq51B7/JeeAZFf/vxer7pC3o3lhQiFar7 uoA3E3syJacMBGmcUYPdZzD4FSjOOOJIDPiFR/CbEEG9qXBpMt7SLMLlj 2aXmEebawT1OfG360pvdGhLL4WhXBF5K+BjuJEzwPN0DVLizDdfcBo1rG 8jQqiqzTw+T7wytNG7Y3KjEbRthCdk3f6r/Z3f0v54v1ztyhXV6r7C3Jb Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10714"; a="380431788" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="380431788" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.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="846669578" X-IronPort-AV: E=Sophos;i="6.00,175,1681196400"; d="scan'208";a="846669578" Received: from rkiyama-mobl1.amr.corp.intel.com (HELO box.shutemov.name) ([10.251.222.16]) by fmsmga001-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 79FC910DFD7; 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 9/9] x86/tdx: Add unaccepted memory support Date: Fri, 19 May 2023 02:14:34 +0300 Message-Id: <20230518231434.26080-10-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-Stat-Signature: jafdxd3pbokocueb97sofynx5rf6sgus X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: B676CA0004 X-Rspam-User: X-HE-Tag: 1684451700-675051 X-HE-Meta: U2FsdGVkX1+15pZkCa0rRJgooCvRAj07eolUhIvywuJr38R1qYINhe2xEkP+oQmcoyfQg7maeHtYE3aKkeE9cm/ANh6y/sg2kJuFhE2Hyx9a6f5qArhA+/Xr0XYFbqt7PaIZA1/WIvIjv4ucCGUf9zER6Wzcn7NN/ewz8aZINbz9ZtZe94K+Qj+KUEgxBGQa8Qct/9EMJPrhI7k+XHDEbKsdaNZqm2CoClHPrOZLcWaMQnhHW7WIpX0bflRCRRf5qShgskfo7VWGt6aJg6q9iWBjBQYC8xfz580MlrVjZ/GajYj2sopg7SoS4V9lrRWFaKNRpCiggi8KHEXWSs1HUEYWei7xE9UdvC2+ucb1cq+4O4ssZwkhpJQO8GTrBJVMipMSUCq0g237XqXWM5XPKGue8dTKgqk3vb3f9yH7VYQqGCdWQoo4kgF8tDp3Z4oEn1G0YwnThd0uVPufucpxyCy0ewdlVwsVW6hL1HxUJtccyLQfD5MU6hxs20ogfrYTWILHcgHdN1JBrDm7q40VWnY7hFAnWPYX9x0Wklz8mARC10Q5ljDIvm6Rtqp1x9zkMeGBssiJdYAS+UN1hWqUNJPdbkMT/veGO9+c05vko0E7zfJBXGGtefH4s2SgvququF31W5r3TzJ591QjUdlQuejBs6HcuzbTPmYbyOFwYAfGYP/AruJqMfP/v8+ByWurtNDMKUY8+9N0RJLg056Fls3Jojz6/SmOIcq9ypLUwL999An0ZZEbQ/Waan5yyQes3ouG0QK7WoE8pDmses2M+0CmtAr+dZ3MnnmXOKN2nl6+Ctdg4FxJQyCInkoH6RvH0zsjHsXuvR8gG7aq2a7mkjy5noyaId611Lc6nj8EKbdsukQxVAfg6YLD+mQoaZ0FiOOziXbNRWJTOsXomZM9kefYfVTmIg4Es5Qcuhj5blq3pMfVTYjnpOltd/EBOCKOP/3S29yNNgT5RVSnxmK 6B/JR49a i/68QWLWOFhy+oRwg12sqO77NlVUzoSm35x+swMoyR5ken8HNUrmTrLYhPU9dsXmy/97Vixd0blnoSFigbnMM6ZRFLNRJAe8RvuVop7f9fkmpDdfBmwTXaKhRZo55JaB90mW+0cLoE2z1dk2a1WgHQiewmPh9lVpIcD9hZFzAZYDZ+iMLQb7j0Df/A8AoCU25emPHBe+3KD95TmA= 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: Hookup TDX-specific code to accept memory. Accepting the memory is the same process as converting memory from shared to private: kernel notifies VMM with MAP_GPA hypercall and then accept pages with ACCEPT_PAGE module call. The implementation in core kernel uses tdx_enc_status_changed(). It already used for converting memory to shared and back for I/O transactions. Boot stub provides own implementation of tdx_accept_memory(). It is similar in structure to tdx_enc_status_changed(), but only cares about converting memory to private. Signed-off-by: Kirill A. Shutemov Signed-off-by: Kirill A. Shutemov --- arch/x86/Kconfig | 2 + arch/x86/boot/compressed/Makefile | 2 +- arch/x86/boot/compressed/error.c | 19 +++++ arch/x86/boot/compressed/error.h | 1 + arch/x86/boot/compressed/mem.c | 33 +++++++- arch/x86/boot/compressed/tdx-shared.c | 2 + arch/x86/boot/compressed/tdx.c | 37 +++++++++ arch/x86/coco/tdx/Makefile | 2 +- arch/x86/coco/tdx/tdx-shared.c | 95 ++++++++++++++++++++++++ arch/x86/coco/tdx/tdx.c | 86 +-------------------- arch/x86/include/asm/shared/tdx.h | 2 + arch/x86/include/asm/tdx.h | 2 + arch/x86/include/asm/unaccepted_memory.h | 23 ++++++ 13 files changed, 219 insertions(+), 87 deletions(-) create mode 100644 arch/x86/boot/compressed/tdx-shared.c create mode 100644 arch/x86/coco/tdx/tdx-shared.c create mode 100644 arch/x86/include/asm/unaccepted_memory.h diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 53bab123a8ee..5c72067c06d4 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -884,9 +884,11 @@ config INTEL_TDX_GUEST bool "Intel TDX (Trust Domain Extensions) - Guest Support" depends on X86_64 && CPU_SUP_INTEL depends on X86_X2APIC + depends on EFI_STUB select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT select X86_MCE + select UNACCEPTED_MEMORY help Support running as a guest under Intel TDX. Without this support, the guest kernel can not boot or run under TDX. diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index cc4978123c30..b13a58021086 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -106,7 +106,7 @@ ifdef CONFIG_X86_64 endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o -vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o $(obj)/tdcall.o +vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o $(obj)/tdcall.o $(obj)/tdx-shared.o vmlinux-objs-$(CONFIG_UNACCEPTED_MEMORY) += $(obj)/mem.o vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o diff --git a/arch/x86/boot/compressed/error.c b/arch/x86/boot/compressed/error.c index c881878e56d3..5313c5cb2b80 100644 --- a/arch/x86/boot/compressed/error.c +++ b/arch/x86/boot/compressed/error.c @@ -22,3 +22,22 @@ void error(char *m) while (1) asm("hlt"); } + +/* EFI libstub provides vsnprintf() */ +#ifdef CONFIG_EFI_STUB +void panic(const char *fmt, ...) +{ + static char buf[1024]; + va_list args; + int len; + + va_start(args, fmt); + len = vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + + if (len && buf[len - 1] == '\n') + buf[len - 1] = '\0'; + + error(buf); +} +#endif diff --git a/arch/x86/boot/compressed/error.h b/arch/x86/boot/compressed/error.h index 1de5821184f1..86fe33b93715 100644 --- a/arch/x86/boot/compressed/error.h +++ b/arch/x86/boot/compressed/error.h @@ -6,5 +6,6 @@ void warn(char *m); void error(char *m) __noreturn; +void panic(const char *fmt, ...) __noreturn __cold; #endif /* BOOT_COMPRESSED_ERROR_H */ diff --git a/arch/x86/boot/compressed/mem.c b/arch/x86/boot/compressed/mem.c index 87372b96d613..0108c97399a5 100644 --- a/arch/x86/boot/compressed/mem.c +++ b/arch/x86/boot/compressed/mem.c @@ -2,11 +2,42 @@ #include "error.h" #include "misc.h" +#include "tdx.h" +#include + +/* + * accept_memory() and process_unaccepted_memory() called from EFI stub which + * runs before decompresser and its early_tdx_detect(). + * + * Enumerate TDX directly from the early users. + */ +static bool early_is_tdx_guest(void) +{ + static bool once; + static bool is_tdx; + + if (!IS_ENABLED(CONFIG_INTEL_TDX_GUEST)) + return false; + + if (!once) { + u32 eax, sig[3]; + + cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, + &sig[0], &sig[2], &sig[1]); + is_tdx = !memcmp(TDX_IDENT, sig, sizeof(sig)); + once = true; + } + + return is_tdx; +} void arch_accept_memory(phys_addr_t start, phys_addr_t end) { /* Platform-specific memory-acceptance call goes here */ - error("Cannot accept memory"); + if (early_is_tdx_guest()) + tdx_accept_memory(start, end); + else + error("Cannot accept memory: unknown platform\n"); } void init_unaccepted_memory(void) diff --git a/arch/x86/boot/compressed/tdx-shared.c b/arch/x86/boot/compressed/tdx-shared.c new file mode 100644 index 000000000000..5ac43762fe13 --- /dev/null +++ b/arch/x86/boot/compressed/tdx-shared.c @@ -0,0 +1,2 @@ +#include "error.h" +#include "../../coco/tdx/tdx-shared.c" diff --git a/arch/x86/boot/compressed/tdx.c b/arch/x86/boot/compressed/tdx.c index 2d81d3cc72a1..d073764eaa50 100644 --- a/arch/x86/boot/compressed/tdx.c +++ b/arch/x86/boot/compressed/tdx.c @@ -9,6 +9,9 @@ #include #include +#include + +static u64 cc_mask; /* Called from __tdx_hypercall() for unrecoverable failure */ void __tdx_hypercall_failed(void) @@ -16,6 +19,38 @@ void __tdx_hypercall_failed(void) error("TDVMCALL failed. TDX module bug?"); } +static u64 get_cc_mask(void) +{ + struct tdx_module_output out; + unsigned int gpa_width; + + /* + * TDINFO TDX module call is used to get the TD execution environment + * information like GPA width, number of available vcpus, debug mode + * information, etc. More details about the ABI can be found in TDX + * Guest-Host-Communication Interface (GHCI), section 2.4.2 TDCALL + * [TDG.VP.INFO]. + * + * The GPA width that comes out of this call is critical. TDX guests + * can not meaningfully run without it. + */ + if (__tdx_module_call(TDX_GET_INFO, 0, 0, 0, 0, &out)) + error("TDCALL GET_INFO failed (Buggy TDX module!)\n"); + + gpa_width = out.rcx & GENMASK(5, 0); + + /* + * The highest bit of a guest physical address is the "sharing" bit. + * Set it for shared pages and clear it for private pages. + */ + return BIT_ULL(gpa_width - 1); +} + +u64 cc_mkdec(u64 val) +{ + return val & ~cc_mask; +} + static inline unsigned int tdx_io_in(int size, u16 port) { struct tdx_hypercall_args args = { @@ -70,6 +105,8 @@ void early_tdx_detect(void) if (memcmp(TDX_IDENT, sig, sizeof(sig))) return; + cc_mask = get_cc_mask(); + /* Use hypercalls instead of I/O instructions */ pio_ops.f_inb = tdx_inb; pio_ops.f_outb = tdx_outb; diff --git a/arch/x86/coco/tdx/Makefile b/arch/x86/coco/tdx/Makefile index 46c55998557d..2c7dcbf1458b 100644 --- a/arch/x86/coco/tdx/Makefile +++ b/arch/x86/coco/tdx/Makefile @@ -1,3 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 -obj-y += tdx.o tdcall.o +obj-y += tdx.o tdx-shared.o tdcall.o diff --git a/arch/x86/coco/tdx/tdx-shared.c b/arch/x86/coco/tdx/tdx-shared.c new file mode 100644 index 000000000000..ee74f7bbe806 --- /dev/null +++ b/arch/x86/coco/tdx/tdx-shared.c @@ -0,0 +1,95 @@ +#include +#include + +static unsigned long try_accept_one(phys_addr_t start, unsigned long len, + enum pg_level pg_level) +{ + unsigned long accept_size = page_level_size(pg_level); + u64 tdcall_rcx; + u8 page_size; + + if (!IS_ALIGNED(start, accept_size)) + return 0; + + if (len < accept_size) + return 0; + + /* + * Pass the page physical address to the TDX module to accept the + * pending, private page. + * + * Bits 2:0 of RCX encode page size: 0 - 4K, 1 - 2M, 2 - 1G. + */ + switch (pg_level) { + case PG_LEVEL_4K: + page_size = 0; + break; + case PG_LEVEL_2M: + page_size = 1; + break; + case PG_LEVEL_1G: + page_size = 2; + break; + default: + return 0; + } + + tdcall_rcx = start | page_size; + if (__tdx_module_call(TDX_ACCEPT_PAGE, tdcall_rcx, 0, 0, 0, NULL)) + return 0; + + return accept_size; +} + +bool tdx_enc_status_changed_phys(phys_addr_t start, phys_addr_t end, bool enc) +{ + if (!enc) { + /* Set the shared (decrypted) bits: */ + start |= cc_mkdec(0); + end |= cc_mkdec(0); + } + + /* + * Notify the VMM about page mapping conversion. More info about ABI + * can be found in TDX Guest-Host-Communication Interface (GHCI), + * section "TDG.VP.VMCALL" + */ + if (_tdx_hypercall(TDVMCALL_MAP_GPA, start, end - start, 0, 0)) + return false; + + /* private->shared conversion requires only MapGPA call */ + if (!enc) + return true; + + /* + * For shared->private conversion, accept the page using + * TDX_ACCEPT_PAGE TDX module call. + */ + while (start < end) { + unsigned long len = end - start; + unsigned long accept_size; + + /* + * Try larger accepts first. It gives chance to VMM to keep + * 1G/2M Secure EPT entries where possible and speeds up + * process by cutting number of hypercalls (if successful). + */ + + accept_size = try_accept_one(start, len, PG_LEVEL_1G); + if (!accept_size) + accept_size = try_accept_one(start, len, PG_LEVEL_2M); + if (!accept_size) + accept_size = try_accept_one(start, len, PG_LEVEL_4K); + if (!accept_size) + return false; + start += accept_size; + } + + return true; +} + +void tdx_accept_memory(phys_addr_t start, phys_addr_t end) +{ + if (!tdx_enc_status_changed_phys(start, end, true)) + panic("Accepting memory failed: %#llx-%#llx\n", start, end); +} diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 0d5fe6e24e45..32501277ef84 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -713,46 +713,6 @@ static bool tdx_cache_flush_required(void) return true; } -static unsigned long try_accept_one(phys_addr_t start, unsigned long len, - enum pg_level pg_level) -{ - unsigned long accept_size = page_level_size(pg_level); - u64 tdcall_rcx; - u8 page_size; - - if (!IS_ALIGNED(start, accept_size)) - return 0; - - if (len < accept_size) - return 0; - - /* - * Pass the page physical address to the TDX module to accept the - * pending, private page. - * - * Bits 2:0 of RCX encode page size: 0 - 4K, 1 - 2M, 2 - 1G. - */ - switch (pg_level) { - case PG_LEVEL_4K: - page_size = 0; - break; - case PG_LEVEL_2M: - page_size = 1; - break; - case PG_LEVEL_1G: - page_size = 2; - break; - default: - return 0; - } - - tdcall_rcx = start | page_size; - if (__tdx_module_call(TDX_ACCEPT_PAGE, tdcall_rcx, 0, 0, 0, NULL)) - return 0; - - return accept_size; -} - /* * Inform the VMM of the guest's intent for this physical page: shared with * the VMM or private to the guest. The VMM is expected to change its mapping @@ -761,51 +721,9 @@ static unsigned long try_accept_one(phys_addr_t start, unsigned long len, static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc) { phys_addr_t start = __pa(vaddr); - phys_addr_t end = __pa(vaddr + numpages * PAGE_SIZE); + phys_addr_t end = __pa(vaddr + numpages * PAGE_SIZE); - if (!enc) { - /* Set the shared (decrypted) bits: */ - start |= cc_mkdec(0); - end |= cc_mkdec(0); - } - - /* - * Notify the VMM about page mapping conversion. More info about ABI - * can be found in TDX Guest-Host-Communication Interface (GHCI), - * section "TDG.VP.VMCALL" - */ - if (_tdx_hypercall(TDVMCALL_MAP_GPA, start, end - start, 0, 0)) - return false; - - /* private->shared conversion requires only MapGPA call */ - if (!enc) - return true; - - /* - * For shared->private conversion, accept the page using - * TDX_ACCEPT_PAGE TDX module call. - */ - while (start < end) { - unsigned long len = end - start; - unsigned long accept_size; - - /* - * Try larger accepts first. It gives chance to VMM to keep - * 1G/2M Secure EPT entries where possible and speeds up - * process by cutting number of hypercalls (if successful). - */ - - accept_size = try_accept_one(start, len, PG_LEVEL_1G); - if (!accept_size) - accept_size = try_accept_one(start, len, PG_LEVEL_2M); - if (!accept_size) - accept_size = try_accept_one(start, len, PG_LEVEL_4K); - if (!accept_size) - return false; - start += accept_size; - } - - return true; + return tdx_enc_status_changed_phys(start, end, enc); } void __init tdx_early_init(void) diff --git a/arch/x86/include/asm/shared/tdx.h b/arch/x86/include/asm/shared/tdx.h index 1ff0ee822961..95fbe7376694 100644 --- a/arch/x86/include/asm/shared/tdx.h +++ b/arch/x86/include/asm/shared/tdx.h @@ -91,5 +91,7 @@ struct tdx_module_output { u64 __tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9, struct tdx_module_output *out); +void tdx_accept_memory(phys_addr_t start, phys_addr_t end); + #endif /* !__ASSEMBLY__ */ #endif /* _ASM_X86_SHARED_TDX_H */ diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index 234197ec17e4..3a7340ad9a4b 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -50,6 +50,8 @@ bool tdx_early_handle_ve(struct pt_regs *regs); int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport); +bool tdx_enc_status_changed_phys(phys_addr_t start, phys_addr_t end, bool enc); + #else static inline void tdx_early_init(void) { }; diff --git a/arch/x86/include/asm/unaccepted_memory.h b/arch/x86/include/asm/unaccepted_memory.h new file mode 100644 index 000000000000..72b354f992bb --- /dev/null +++ b/arch/x86/include/asm/unaccepted_memory.h @@ -0,0 +1,23 @@ +#ifndef _ASM_X86_UNACCEPTED_MEMORY_H +#define _ASM_X86_UNACCEPTED_MEMORY_H + +#include +#include + +static inline void arch_accept_memory(phys_addr_t start, phys_addr_t end) +{ + /* Platform-specific memory-acceptance call goes here */ + if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) { + tdx_accept_memory(start, end); + } else { + panic("Cannot accept memory: unknown platform\n"); + } +} + +static inline struct efi_unaccepted_memory *efi_get_unaccepted_table(void) +{ + if (efi.unaccepted == EFI_INVALID_TABLE_ADDR) + return NULL; + return __va(efi.unaccepted); +} +#endif