Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp543842lqa; Sat, 27 Apr 2024 12:56:14 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUr2yIzZpsZvPKn08rCaIf17l9MK7qhWpppBGz7OtfSvHM0UPQ2kYq2TJZiAVXJzRr3PDEGHHP51r44sKI+rxJqAXc8ZvztsskS09nkgQ== X-Google-Smtp-Source: AGHT+IFmJNSgj/CDa0xH469s1DIdyjuYLjS7OxL/+6/YUHgyZMCzVhvX657bV90H8Tr0wtG9gZFl X-Received: by 2002:a17:90a:9c3:b0:2ad:6294:7112 with SMTP id 61-20020a17090a09c300b002ad62947112mr6353285pjo.14.1714247774471; Sat, 27 Apr 2024 12:56:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714247774; cv=pass; d=google.com; s=arc-20160816; b=srjk9yBlJvkk/nR8bLJ5k6l1ynryucOlES56zcXhMircf/GumRoBEwAAi8h3gAB4Ek Wr6P8Fvy18cej3gfh1oxPwoGdQsw+5Y9m5+pZCvP8dn85g6Sty58vqJUUPunjMPBdvsB wB0t/btFBlm0bn1dC068H/jXIuy0vphLOGVugqdNJXizBWnpUovbDLK3mDvG5grMDazq Z+0O31101ToUUgF4yx6z38eh44vJWF7wyjEl1G4KExoX4DRed8hNFCuX3QaBZwr6lGP9 hgWjJmA2rHF/UrsAUtrjx/CuDV4zh8+OW06qG2DBTOepJTqYsP2AuWzY0cDWXnV3Aa6w Y4vg== 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=krIUnG/O0aqtWwwWKj1fa+EBsqdaFQnC33X6RIE5XkM=; fh=WI8UrM2pMp42iwf5VE0f97YCuCSbLc8s5/IcVw4XMDE=; b=m0pQiK0+J6d290amyAzEJ0qtU9UG/zSvQHv6ChLjITBv/PY45JPwvCOUa1I3V98DVY GluYtPxFOQ5Ddsc4sUDQMC3qARF50GLW+1EIcEpNMnTFU+f/+ccE7LtPOOz9BVyFJvFr wItotyRekr7FJvsS+ryszezYJ/GoZxYJWrv2sm9n93Ik7rwJhnI0MkGH0tTIiNxIARMw jKcaMZS0iDCjVVIyUzaUJUuKmFKSglklWuQdxGpa+ZDvp/9Ha5qcQrnNoosj4HPLb9j7 B+Oi0okyE5+5q9txr4m5hUs4UvvMtNjkYfAqWsedECP0XfD3zOYVmm6Suu10sHNULdR2 0OIg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jayf9poS; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-161108-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161108-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id t11-20020a17090aba8b00b002a63dcc38d7si17523119pjr.30.2024.04.27.12.56.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Apr 2024 12:56:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-161108-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jayf9poS; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-161108-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161108-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 5CB24B21ED6 for ; Sat, 27 Apr 2024 17:06:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C7CF514291E; Sat, 27 Apr 2024 17:06:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="jayf9poS" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.17]) (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 8403D1C17 for ; Sat, 27 Apr 2024 17:06:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.17 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714237603; cv=none; b=qXpkfSroc2M31VhdnnB3oIdssFfTzXvfu8VT5SntviyfaYqCQvDRGJwKu6LL3w/nPENJFcaxjEwnfZdVtM4GdtEm/YstsyHRmgyVtnV01oa8h688LB3my8XVOjQDg8wMu8nUd4Rqbh26AVJ/kd+20d1VeMRI4k3tFbWxTlqvn1E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714237603; c=relaxed/simple; bh=IKwboz6unkUuaspdbL5m2QJO18HfYn+ShzYqre7Wt1I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n6tc9yefVwev8FWeeYbRbyN9EwG8KIFge7CRMlNlbu49zO8j3fkl6LUyBZxP0Z9m21S1hEz8RSLlG071faeX7g75Fkj12ZUM5mdR6RlgLGf6NiWqVrK4lvS085Ly5OkDmQEgRqs27q8vpMQ2npIWReoXIbTA/UFQnQiiAbfO44g= 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=jayf9poS; arc=none smtp.client-ip=198.175.65.17 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=1714237603; x=1745773603; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IKwboz6unkUuaspdbL5m2QJO18HfYn+ShzYqre7Wt1I=; b=jayf9poSUjDvEOuQDc0PJhkgIQDdBlxbteq6PQyGCOfi0kh+C/gP+tS+ uBWThkZDf9NqRiTV6yCRM8EzQ5150dRhWRKlnVAREnyujkH6Zu0qq8r5l moHkK3TmaxxEaB5My+H7/12oaIgVoVqUM7S5oVIx5YYXoNWqWWe+sGnak hpIr6mQZT56FsP5japADoBKdu1eWaidh64an6lUOGJCKG0ppxkCQ1hfZJ YE7blVx4R/SQ75s3FqjmTv1kUFvMwV5f2aa6LBt/Vv2c5Nggt3prFqtWr 7osCIlyc8cBEzZjIQSJ3t7R/lZmliBWj+ZTyl/0dEstwHvL5Xmhpjr47R Q==; X-CSE-ConnectionGUID: TfVDSxU5THauvc4Zxikhwg== X-CSE-MsgGUID: oQKOSYLvRuqu2xq8+rEZxQ== X-IronPort-AV: E=McAfee;i="6600,9927,11057"; a="10063540" X-IronPort-AV: E=Sophos;i="6.07,235,1708416000"; d="scan'208";a="10063540" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by orvoesa109.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Apr 2024 10:06:42 -0700 X-CSE-ConnectionGUID: CF+2/vVlQgucut3jhAcIUQ== X-CSE-MsgGUID: b/odNh/XTVue/enrZ7AJgw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,235,1708416000"; d="scan'208";a="25780516" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa010.fm.intel.com with ESMTP; 27 Apr 2024 10:06:37 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 023C8118; Sat, 27 Apr 2024 20:06:35 +0300 (EEST) From: "Kirill A. Shutemov" To: bp@alien8.de Cc: adrian.hunter@intel.com, ashish.kalra@amd.com, bhe@redhat.com, dave.hansen@linux.intel.com, elena.reshetova@intel.com, jun.nakajima@intel.com, kai.huang@intel.com, kexec@lists.infradead.org, kirill.shutemov@linux.intel.com, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, ltao@redhat.com, mingo@redhat.com, nik.borisov@suse.com, peterz@infradead.org, rafael@kernel.org, rick.p.edgecombe@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, seanjc@google.com, tglx@linutronix.de, thomas.lendacky@amd.com, x86@kernel.org Subject: [PATCHv10.1 09/18] x86/mm: Adding callbacks to prepare encrypted memory for kexec Date: Sat, 27 Apr 2024 20:06:34 +0300 Message-ID: <20240427170634.2397725-1-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240427164747.GCZi0sM6HBCBYtgWqF@fat_crate.local> References: <20240427164747.GCZi0sM6HBCBYtgWqF@fat_crate.local> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit AMD SEV and Intel TDX guests allocate shared buffers for performing I/O. This is done by allocating pages normally from the buddy allocator and then converting them to shared using set_memory_decrypted(). On kexec, the second kernel is unaware of which memory has been converted in this manner. It only sees E820_TYPE_RAM. Accessing shared memory as private is fatal. Therefore, the memory state must be reset to its original state before starting the new kernel with kexec. The process of converting shared memory back to private occurs in two steps: - enc_kexec_stop_conversion() stops new conversions. - enc_kexec_unshare_mem() unshares all existing shared memory, reverting it back to private. Signed-off-by: Kirill A. Shutemov Reviewed-by: Nikolay Borisov x Reviewed-by: Kai Huang Tested-by: Tao Liu --- arch/x86/include/asm/x86_init.h | 2 ++ arch/x86/kernel/crash.c | 4 ++++ arch/x86/kernel/reboot.c | 12 ++++++++++++ arch/x86/kernel/x86_init.c | 4 ++++ 4 files changed, 22 insertions(+) diff --git a/arch/x86/include/asm/x86_init.h b/arch/x86/include/asm/x86_init.h index 28ac3cb9b987..c731e6bc4343 100644 --- a/arch/x86/include/asm/x86_init.h +++ b/arch/x86/include/asm/x86_init.h @@ -155,6 +155,8 @@ 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_stop_conversion)(bool crash); + void (*enc_kexec_unshare_mem)(void); }; /** diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c index e74d0c4286c1..f1b261be78b4 100644 --- a/arch/x86/kernel/crash.c +++ b/arch/x86/kernel/crash.c @@ -128,6 +128,10 @@ void native_machine_crash_shutdown(struct pt_regs *regs) #ifdef CONFIG_HPET_TIMER hpet_disable(); #endif + + x86_platform.guest.enc_kexec_stop_conversion(true); + x86_platform.guest.enc_kexec_unshare_mem(); + crash_save_cpu(regs, safe_smp_processor_id()); } diff --git a/arch/x86/kernel/reboot.c b/arch/x86/kernel/reboot.c index f3130f762784..c1920ec34f0c 100644 --- a/arch/x86/kernel/reboot.c +++ b/arch/x86/kernel/reboot.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -716,6 +717,14 @@ static void native_machine_emergency_restart(void) void native_machine_shutdown(void) { + /* + * Call enc_kexec_stop_conversion() while all CPUs are still active and + * interrupts are enabled. This will allow all in-flight memory + * conversions to finish cleanly. + */ + if (kexec_in_progress) + x86_platform.guest.enc_kexec_stop_conversion(false); + /* Stop the cpus and apics */ #ifdef CONFIG_X86_IO_APIC /* @@ -752,6 +761,9 @@ void native_machine_shutdown(void) #ifdef CONFIG_X86_64 x86_platform.iommu_shutdown(); #endif + + if (kexec_in_progress) + x86_platform.guest.enc_kexec_unshare_mem(); } static void __machine_emergency_restart(int emergency) diff --git a/arch/x86/kernel/x86_init.c b/arch/x86/kernel/x86_init.c index a7143bb7dd93..045ce1c70070 100644 --- a/arch/x86/kernel/x86_init.c +++ b/arch/x86/kernel/x86_init.c @@ -138,6 +138,8 @@ static int enc_status_change_prepare_noop(unsigned long vaddr, int npages, bool static int enc_status_change_finish_noop(unsigned long vaddr, int npages, bool enc) { return 0; } static bool enc_tlb_flush_required_noop(bool enc) { return false; } static bool enc_cache_flush_required_noop(void) { return false; } +static void enc_kexec_stop_conversion_noop(bool crash) {} +static void enc_kexec_unshare_mem_noop(void) {} static bool is_private_mmio_noop(u64 addr) {return false; } struct x86_platform_ops x86_platform __ro_after_init = { @@ -161,6 +163,8 @@ struct x86_platform_ops x86_platform __ro_after_init = { .enc_status_change_finish = enc_status_change_finish_noop, .enc_tlb_flush_required = enc_tlb_flush_required_noop, .enc_cache_flush_required = enc_cache_flush_required_noop, + .enc_kexec_stop_conversion = enc_kexec_stop_conversion_noop, + .enc_kexec_unshare_mem = enc_kexec_unshare_mem_noop, }, }; -- 2.43.0