Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp1092884rdb; Fri, 20 Oct 2023 08:14:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGU/YrOy3pghlFX+kd38sjX/iE7t3RD1daSfTn+l7i+d625jmEXR2aqSDBBIJtL15hoPjVd X-Received: by 2002:a17:90a:130c:b0:274:7db1:f50f with SMTP id h12-20020a17090a130c00b002747db1f50fmr2943470pja.15.1697814870614; Fri, 20 Oct 2023 08:14:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697814870; cv=none; d=google.com; s=arc-20160816; b=YXdx3/kM9SQRFjisP2kbq422uR68t266floMtvBtVCYar1KUHga2WjgmkuAp0Mq0L4 F0vt4rKxVQzSZabjxD80kA3ZN3iSqBoVLdCeqdzZG0JwaLvNVYgpFmPOrrqyAi5YDn13 +ngfQAXK3OuBF0cFE2t3ATSihKKNKydo3J0vrFSSoxHK/66K0xuU6AgzpqchA3ppe1kX 8JqlMuXe+RR3JuPbH53IJF5GQq8no8smOifn35byRSJh56GcH+r+2FRUAcl1mAEWn1DN Fa6/U34YDbhJLdsA2TG6RpZJS+cXfiOVxJmei0kPUZjQSRKAsSSPayvj9+W2xpT+EhZV yhDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Kq2Gc3fY7sxiop6jc9bpf1EVj2aZ52HhuSFMGwd/PJA=; fh=OvJRnOqsMTm9XoNmEwebcqh9Ud7yh1CTeKAP84ols98=; b=wWXddr5tHFQQobztf5R+NWMWvzpzxdbm1itfyDOMUNec3YXQvyCvkBBaX6Y6FW9zag KaT/VVnyM1JXvPTUhfIM8EuVOQT7NXfZS0HMLcG5zfcy297ahBS8LltdUJeOaRqAhj0P 9/jADqxOPsI3IQoYxsPcjwkR7uaUHFPzmey28As3Xlf5RMP6itBop3+Or6JqNk6zyz78 t++84+Llm55A8RzP9f/YM/7d5HapmTOuRi5iSjnwEQaYYm6GXIVQAu1JjjWdca8EPwvk DqAyaD73r4XHd7P/7aRZ/0vjw4ERjisEPMqkFH0CqsW91jlY25DZnRuv93yO6w550OB8 hviQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=aDVZdjxS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id b7-20020a17090ae38700b0027d20cab1d3si1997705pjz.42.2023.10.20.08.14.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 08:14:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=aDVZdjxS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id DBE9D803D820; Fri, 20 Oct 2023 08:14:27 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377774AbjJTPOG (ORCPT + 99 others); Fri, 20 Oct 2023 11:14:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377655AbjJTPNn (ORCPT ); Fri, 20 Oct 2023 11:13:43 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E697199A for ; Fri, 20 Oct 2023 08:13:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697814809; x=1729350809; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HRNPLDVUIxAZyLBln62K0h8qTymbHiDs6/gObbq9Ccw=; b=aDVZdjxSrjYIldklq4f7hXLIYHP/DPmGrV9WkEgspfjhYTb8qefRioH+ N7fe/Ej7VC5Wou71sxetGk2lmbZJ7ccEOaVe3ygRZ3TFvhsIs2h/RpHTa 7g2F+9v4U3L18Y2KZZuuYHcHkog2ag4rpHspr0+eXPW+ZNbAZbls6JGc1 s4ykH5/9UOKN2HM59qm9m8vvUMdy/XhVILsO2fwJoO7YXkubdYCO7e1At ukkaeYyYDKPxG7QLArliHc1+JzmOXApmiEP4LeV9yiRmvZqYDHXm3e9ev m1si79VdafzFEb6BzyGTyR4XSb+U76HYTsOZaEM3yfgwoDMgMxi4hE9nA Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10869"; a="366742918" X-IronPort-AV: E=Sophos;i="6.03,239,1694761200"; d="scan'208";a="366742918" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Oct 2023 08:13:01 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10869"; a="1004634226" X-IronPort-AV: E=Sophos;i="6.03,239,1694761200"; d="scan'208";a="1004634226" Received: from dgutows1-mobl.ger.corp.intel.com (HELO box.shutemov.name) ([10.249.39.237]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Oct 2023 08:12:56 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 456FA10A29B; Fri, 20 Oct 2023 18:12:45 +0300 (+03) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org Cc: "Rafael J. Wysocki" , Peter Zijlstra , Adrian Hunter , Kuppuswamy Sathyanarayanan , Elena Reshetova , Jun Nakajima , Rick Edgecombe , Tom Lendacky , "Kalra, Ashish" , Sean Christopherson , "Huang, Kai" , Baoquan He , kexec@lists.infradead.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv2 09/13] x86/tdx: Convert shared memory back to private on kexec Date: Fri, 20 Oct 2023 18:12:38 +0300 Message-ID: <20231020151242.1814-10-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231020151242.1814-1-kirill.shutemov@linux.intel.com> References: <20231020151242.1814-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Fri, 20 Oct 2023 08:14:28 -0700 (PDT) TDX guests allocate shared buffers to perform I/O. It is done by allocating pages normally from the buddy allocator and converting them to shared with set_memory_decrypted(). The second kernel has no idea what memory is converted this way. It only sees E820_TYPE_RAM. Accessing shared memory via private mapping is fatal. It leads to unrecoverable TD exit. On kexec walk direct mapping and convert all shared memory back to private. It makes all RAM private again and second kernel may use it normally. Signed-off-by: Kirill A. Shutemov --- arch/x86/coco/tdx/kexec.c | 0 arch/x86/coco/tdx/tdx.c | 120 +++++++++++++++++++++++++++++++- arch/x86/include/asm/x86_init.h | 1 + arch/x86/kernel/crash.c | 4 ++ arch/x86/kernel/reboot.c | 5 ++ 5 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 arch/x86/coco/tdx/kexec.c diff --git a/arch/x86/coco/tdx/kexec.c b/arch/x86/coco/tdx/kexec.c new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 587bdeb88afa..2be23fe8cb3d 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -6,14 +6,17 @@ #include #include +#include #include #include +#include #include #include #include #include #include #include +#include /* MMIO direction */ #define EPT_READ 0 @@ -40,6 +43,9 @@ static atomic_long_t nr_shared; +static atomic_t conversions_in_progress; +static bool conversion_allowed = true; + static inline bool pte_decrypted(pte_t pte) { return cc_mkdec(pte_val(pte)) == pte_val(pte); @@ -704,6 +710,14 @@ static bool tdx_tlb_flush_required(bool private) static bool tdx_cache_flush_required(void) { + /* + * Avoid issuing CLFLUSH on set_memory_decrypted() if conversions + * stopped. Otherwise it can race with unshare_all_memory() and trigger + * implicit conversion to shared. + */ + if (!conversion_allowed) + return false; + /* * AMD SME/SEV can avoid cache flushing if HW enforces cache coherence. * TDX doesn't have such capability. @@ -787,12 +801,25 @@ static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc) static int tdx_enc_status_change_prepare(unsigned long vaddr, int numpages, bool enc) { + atomic_inc(&conversions_in_progress); + + /* + * Check after bumping conversions_in_progress to serialize + * against tdx_shutdown(). + */ + if (!conversion_allowed) { + atomic_dec(&conversions_in_progress); + return -EBUSY; + } + /* * Only handle shared->private conversion here. * See the comment in tdx_early_init(). */ - if (enc && !tdx_enc_status_changed(vaddr, numpages, enc)) + if (enc && !tdx_enc_status_changed(vaddr, numpages, enc)) { + atomic_dec(&conversions_in_progress); return -EIO; + } return 0; } @@ -804,17 +831,104 @@ static int tdx_enc_status_change_finish(unsigned long vaddr, int numpages, * Only handle private->shared conversion here. * See the comment in tdx_early_init(). */ - if (!enc && !tdx_enc_status_changed(vaddr, numpages, enc)) + if (!enc && !tdx_enc_status_changed(vaddr, numpages, enc)) { + atomic_dec(&conversions_in_progress); return -EIO; + } if (enc) atomic_long_sub(numpages, &nr_shared); else atomic_long_add(numpages, &nr_shared); + atomic_dec(&conversions_in_progress); + return 0; } +void tdx_kexec_unshare_mem(bool crash) +{ + unsigned long addr, end; + long found = 0, shared; + + /* Stop new private<->shared conversions */ + conversion_allowed = false; + + /* + * Crash kernel reaches here with interrupts disabled: can't wait for + * conversions to finish. + * + * If race happened, just report and proceed. + */ + if (!crash) { + unsigned long timeout; + + /* + * Wait for in-flight conversions to complete. + * + * Do not wait more than 30 seconds. + */ + timeout = 30 * USEC_PER_SEC; + while (atomic_read(&conversions_in_progress) && timeout--) + udelay(1); + } + + if (atomic_read(&conversions_in_progress)) + pr_warn("Failed to finish shared<->private conversions\n"); + + /* + * Walk direct mapping and convert all shared memory back to private, + */ + + addr = PAGE_OFFSET; + end = PAGE_OFFSET + get_max_mapped(); + + while (addr < end) { + unsigned long size; + unsigned int level; + pte_t *pte; + + pte = lookup_address(addr, &level); + size = page_level_size(level); + + if (pte && pte_decrypted(*pte)) { + int pages = size / PAGE_SIZE; + + /* + * Touching memory with shared bit set triggers implicit + * conversion to shared. + * + * Make sure nobody touches the shared range from + * now on. + * + * Bypass unmapping for crash scenario. Unmapping + * requires sleepable context, but in crash case kernel + * hits the code path with interrupts disabled. + * It shouldn't be a problem as all secondary CPUs are + * down and kernel runs with interrupts disabled, so + * there is no room for race. + */ + if (!crash) + set_memory_np(addr, pages); + + if (!tdx_enc_status_changed(addr, pages, true)) { + pr_err("Failed to unshare range %#lx-%#lx\n", + addr, addr + size); + } + + found += pages; + } + + addr += size; + } + + shared = atomic_long_read(&nr_shared); + if (shared != found) { + pr_err("shared page accounting is off\n"); + pr_err("nr_shared = %ld, nr_found = %ld\n", shared, found); + } +} + void __init tdx_early_init(void) { struct tdx_module_args args = { @@ -874,6 +988,8 @@ void __init tdx_early_init(void) x86_platform.guest.enc_cache_flush_required = tdx_cache_flush_required; x86_platform.guest.enc_tlb_flush_required = tdx_tlb_flush_required; + x86_platform.guest.enc_kexec_unshare_mem = tdx_kexec_unshare_mem; + /* * TDX intercepts the RDMSR to read the X2APIC ID in the parallel * bringup low level code. That raises #VE which cannot be handled diff --git a/arch/x86/include/asm/x86_init.h b/arch/x86/include/asm/x86_init.h index 5031cbc6e211..008a34b42057 100644 --- a/arch/x86/include/asm/x86_init.h +++ b/arch/x86/include/asm/x86_init.h @@ -154,6 +154,7 @@ struct x86_guest { int (*enc_status_change_finish)(unsigned long vaddr, int npages, bool enc); bool (*enc_tlb_flush_required)(bool enc); bool (*enc_cache_flush_required)(void); + void (*enc_kexec_unshare_mem)(bool crash); }; /** diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c index c92d88680dbf..1618224775f5 100644 --- a/arch/x86/kernel/crash.c +++ b/arch/x86/kernel/crash.c @@ -40,6 +40,7 @@ #include #include #include +#include /* Used while preparing memory map entries for second kernel */ struct crash_memmap_data { @@ -107,6 +108,9 @@ void native_machine_crash_shutdown(struct pt_regs *regs) crash_smp_send_stop(); + if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) + x86_platform.guest.enc_kexec_unshare_mem(true); + cpu_emergency_disable_virtualization(); /* diff --git a/arch/x86/kernel/reboot.c b/arch/x86/kernel/reboot.c index 830425e6d38e..9fb302562bfd 100644 --- a/arch/x86/kernel/reboot.c +++ b/arch/x86/kernel/reboot.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,7 @@ #include #include #include +#include /* * Power off function, if any @@ -716,6 +718,9 @@ static void native_machine_emergency_restart(void) void native_machine_shutdown(void) { + if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) && kexec_in_progress) + x86_platform.guest.enc_kexec_unshare_mem(false); + /* Stop the cpus and apics */ #ifdef CONFIG_X86_IO_APIC /* -- 2.41.0