Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1912478lqe; Tue, 9 Apr 2024 04:32:30 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWcWyPJYnHVLQFLgUuIMkb+eYWYAk7tduqGz7ndXb5LfX9CO3FqS4kfdCtUxyYntVtqpUmHaV/6P6wcZ0mfeTI4RQebg0y1LRsJ6QIetw== X-Google-Smtp-Source: AGHT+IGF16uAjaeVAcPD+wJszr0xvrhEX1V8fOOySzk7z7lFqAIf2AROQlGsu5oI4U+JvOK7GZhM X-Received: by 2002:a50:9e84:0:b0:56c:24e6:ca7e with SMTP id a4-20020a509e84000000b0056c24e6ca7emr7289953edf.2.1712662350226; Tue, 09 Apr 2024 04:32:30 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712662350; cv=pass; d=google.com; s=arc-20160816; b=uwH5/XpkfzztX2CqCNS9FruqcCSE8RJs3+421+MvpX5KF6W1Qcr05azn7gpKsjQqqC FiXe4px7+8sq0f5o+TWofgonGUWuVLNvk3vMcIN3fkvURAHW0WBUXPnKrwG9D5TM81db r56+Oh0EhqFP53iP0pqTgAvomgVxvUkc0/JqlC6NwW34fLTpLAtClnpTY0pKTcI3mFNV cJSjJ2aqnDQwKFLsh1wx3nTbD/zIkWHdq+iyK6opV1Qc468CyVmhZsSf82f1XlGipXZb 2AE/StSdURe/zWg0T1E9aEg0MvaI7bYBLHLeUmCb88dET6ly0Ou+7iGzR9Fdv+CVUXpt 7oWw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=XgmLVAG1hSzMh9u7EV/awIw/vpAhO9ZfsMXIUXmG9lk=; fh=Yw2S7s1ZcF7+jfnU9Zy13ghDfPt/cQWqfRpyK6FQAJQ=; b=PeJOxZPkklYdpJcxziPp9jOFuqap6JxRlBmzoerok124Zoc/+zy1O9p1UvtwfvWTBA Al+FVSjd/CgUSLG4aJZy7Zs+RiXe/Omr+ZJu7ACodJv8ilVb5mH7AyESYq5j98ewVYyK +l0nsdD3dzOg6ziDB7xZnZpVF6sb28eStedhMhjE2yUGFIBeSxJ72t0mn4N9Xa2HVRgl aqPS19g8k0tXabnECt8FlHBtofpCgWMTsPbFWsm1hg1p8LcdVzwXVxOQ0g7/xKpkXcjY w2OWutGCB311asojCqTiYeZq7jShyJnnF7cjjWBQQDJ68ILHOCBKu4v/hwLh0VAkTKOF l6hA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="VfaKoX/R"; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-136758-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-136758-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id r2-20020a056402234200b0056c05aff452si4795644eda.192.2024.04.09.04.32.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 04:32:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-136758-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="VfaKoX/R"; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-136758-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-136758-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id C3D251F25D17 for ; Tue, 9 Apr 2024 11:32:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8CA1112F5AE; Tue, 9 Apr 2024 11:30:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="VfaKoX/R" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3259312D75A for ; Tue, 9 Apr 2024 11:30:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712662234; cv=none; b=YVcGqx63vrEb4+kaLdwTuw6hGskmsK3k2q7gnRjbJNxaIqlwNw3vwyeOV/rLFwHpCHj8/C5DHrHj7c//6X/gdYhAXsuRSNoV6SM8MpuNv6ceajzk22065pk+561ssZjfRij7lnOJ99GCxNk0J54p9YES/zHxHV75ij62cyrKLy0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712662234; c=relaxed/simple; bh=a64FQvJDD4gr5bqdxdYkrWENcXNUNDUgRRX0Ht95AlE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=luvONipQdgwFC2oCh0jZM4ERVhOV7Fv4ejOp8upenUsVO0vDqIFlP/4vmGE3cGtkqkRX8O7br97HV/rcwG1fCX7yQHt9matpwOg/324d/NgvDMlCz9eDSjfltYsQPETgQaYnAbndauU7h8iWY5+ITUeGbZzaZ4tDGLTUkDn0dp4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=VfaKoX/R; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712662232; x=1744198232; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=a64FQvJDD4gr5bqdxdYkrWENcXNUNDUgRRX0Ht95AlE=; b=VfaKoX/RedsqBID0DdR+5FMxbD/DyNHRxMjrdgOLFAFTbiN/Sqbkojew ds/Fvgups6L3dgbif9AV27jYeIjbSFju03e5L+Q6MLqitsRRY4ytiitTj gAjzgfCXG3I2EMkZbQNDfRINBwf/05AzcavtC/XCpKejDoWFdQVe0YCNE 3B1kZSFbGqlNOLgRtr1WcxuyZe5dTXXV6oBik4lhoCp7mUrq4tk0iqERZ DyVjoPg53MY+1iD638oA1NyRvb5u7oFTFKuTEmo5Uv1vCBcl8TrDGjyeX XL0cYzDPx3JDHPuC2/Nd+l0wnV8HfPlDkOVj10FEQQN9Xef/TiVYuQ/Cj Q==; X-CSE-ConnectionGUID: eRTJsV5vQxCixaIxyLVzOw== X-CSE-MsgGUID: /XoqRurzTGeunPBFLmjIVg== X-IronPort-AV: E=McAfee;i="6600,9927,11038"; a="30460414" X-IronPort-AV: E=Sophos;i="6.07,189,1708416000"; d="scan'208";a="30460414" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Apr 2024 04:30:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,11038"; a="937093337" X-IronPort-AV: E=Sophos;i="6.07,189,1708416000"; d="scan'208";a="937093337" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 09 Apr 2024 04:30:24 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id F0AA9D63; Tue, 9 Apr 2024 14:30:17 +0300 (EEST) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org Cc: "Rafael J. Wysocki" , Peter Zijlstra , Adrian Hunter , Kuppuswamy Sathyanarayanan , Elena Reshetova , Jun Nakajima , Rick Edgecombe , Tom Lendacky , "Kalra, Ashish" , Sean Christopherson , "Huang, Kai" , Baoquan He , kexec@lists.infradead.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" , Tao Liu Subject: [PATCHv10 10/18] x86/tdx: Convert shared memory back to private on kexec Date: Tue, 9 Apr 2024 14:30:02 +0300 Message-ID: <20240409113010.465412-11-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240409113010.465412-1-kirill.shutemov@linux.intel.com> References: <20240409113010.465412-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. The conversion occurs in two steps: stopping new conversions and unsharing all memory. In the case of normal kexec, the stopping of conversions takes place while scheduling is still functioning. This allows for waiting until any ongoing conversions are finished. The second step is carried out when all CPUs except one are inactive and interrupts are disabled. This prevents any conflicts with code that may access shared memory. Signed-off-by: Kirill A. Shutemov Reviewed-by: Rick Edgecombe Reviewed-by: Kai Huang Tested-by: Tao Liu --- arch/x86/coco/tdx/tdx.c | 72 +++++++++++++++++++++++++++++++ arch/x86/include/asm/pgtable.h | 5 +++ arch/x86/include/asm/set_memory.h | 3 ++ arch/x86/mm/pat/set_memory.c | 35 +++++++++++++-- 4 files changed, 112 insertions(+), 3 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 979891e97d83..59776ce1c1d7 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -14,6 +15,7 @@ #include #include #include +#include /* MMIO direction */ #define EPT_READ 0 @@ -831,6 +833,73 @@ static int tdx_enc_status_change_finish(unsigned long vaddr, int numpages, return 0; } +/* Stop new private<->shared conversions */ +static void tdx_kexec_stop_conversion(bool crash) +{ + /* + * Crash kernel reaches here with interrupts disabled: can't wait for + * conversions to finish. + * + * If race happened, just report and proceed. + */ + bool wait_for_lock = !crash; + + if (!stop_memory_enc_conversion(wait_for_lock)) + pr_warn("Failed to stop shared<->private conversions\n"); +} + +static void tdx_kexec_unshare_mem(void) +{ + unsigned long addr, end; + long found = 0, shared; + + /* + * 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. + */ + set_pte(pte, __pte(0)); + + if (!tdx_enc_status_changed(addr, pages, true)) { + pr_err("Failed to unshare range %#lx-%#lx\n", + addr, addr + size); + } + + found += pages; + } + + addr += size; + } + + __flush_tlb_all(); + + 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 = { @@ -890,6 +959,9 @@ 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_stop_conversion = tdx_kexec_stop_conversion; + 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/pgtable.h b/arch/x86/include/asm/pgtable.h index 315535ffb258..17f4d97fae06 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -140,6 +140,11 @@ static inline int pte_young(pte_t pte) return pte_flags(pte) & _PAGE_ACCESSED; } +static inline bool pte_decrypted(pte_t pte) +{ + return cc_mkdec(pte_val(pte)) == pte_val(pte); +} + #define pmd_dirty pmd_dirty static inline bool pmd_dirty(pmd_t pmd) { diff --git a/arch/x86/include/asm/set_memory.h b/arch/x86/include/asm/set_memory.h index 9aee31862b4a..44b6d711296c 100644 --- a/arch/x86/include/asm/set_memory.h +++ b/arch/x86/include/asm/set_memory.h @@ -49,8 +49,11 @@ int set_memory_wb(unsigned long addr, int numpages); int set_memory_np(unsigned long addr, int numpages); int set_memory_p(unsigned long addr, int numpages); int set_memory_4k(unsigned long addr, int numpages); + +bool stop_memory_enc_conversion(bool wait); int set_memory_encrypted(unsigned long addr, int numpages); int set_memory_decrypted(unsigned long addr, int numpages); + int set_memory_np_noalias(unsigned long addr, int numpages); int set_memory_nonglobal(unsigned long addr, int numpages); int set_memory_global(unsigned long addr, int numpages); diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 6c49f69c0368..21835339c0e6 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2188,12 +2188,41 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc) return ret; } +static DECLARE_RWSEM(mem_enc_lock); + +/* + * Stop new private<->shared conversions. + * + * Taking the exclusive mem_enc_lock waits for in-flight conversions to complete. + * The lock is not released to prevent new conversions from being started. + * + * If sleep is not allowed, as in a crash scenario, try to take the lock. + * Failure indicates that there is a race with the conversion. + */ +bool stop_memory_enc_conversion(bool wait) +{ + if (!wait) + return down_write_trylock(&mem_enc_lock); + + down_write(&mem_enc_lock); + + return true; +} + static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) { - if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) - return __set_memory_enc_pgtable(addr, numpages, enc); + int ret = 0; - return 0; + if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) { + if (!down_read_trylock(&mem_enc_lock)) + return -EBUSY; + + ret =__set_memory_enc_pgtable(addr, numpages, enc); + + up_read(&mem_enc_lock); + } + + return ret; } int set_memory_encrypted(unsigned long addr, int numpages) -- 2.43.0