Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2375653rdb; Mon, 12 Feb 2024 02:49:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IERe8D4UrH81O8UPwwg8rKFLS1v4MT6JplYnmIfvU1W+OmTFnwMkFyNZcvU0XMrF+FVXXy6 X-Received: by 2002:a05:6902:18c7:b0:d80:68d1:b826 with SMTP id ck7-20020a05690218c700b00d8068d1b826mr6800378ybb.6.1707734979691; Mon, 12 Feb 2024 02:49:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707734979; cv=pass; d=google.com; s=arc-20160816; b=TMgUMIhdpvPgpq3ry+fyCjxVkv6FfyRvcJkaI9oSxM6ONKa9HmT2KQl+CgrNTJNOkG N2mRoeqqY73EShu98YanDdOFkhKVYd4wUoBfCOSvHB+PePCqXTOd8rLwy7IU6WNYFEkD C9Vg+89Q46qG7s27duCMndnXWyUGKCQ5jnTA+s6HO+mdBmDYtAvsK5px/pLHNFry+ens oy2KT/Ofb8Llc2/Mb5u959Vwk7cmKTv0q+xhIIAwMea+nW/HrXL/1T8jwij4qRoPoQEw pYj1hEgVjrn1DCj/a2XPpsqKJBID9wXwgF/p9TXq0n1iQfQfs3rfv0eroAgCumF63xu6 qLuw== 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=WD2JGEweFTEcyVG1Zgdz/cxp/owXWYDMRqs2tjc+ULE=; fh=wdLbUoaM8wtFuQzNuRFMpNdP03JCvP3Kq9B4RjKftE0=; b=smJm9Sk8YcR3PW5MGClJ/3cK6wfS3KkS3iKHz1FxGMU+HINvM+KHSOn5jLChVI50ea lpu5TCuaMzVitIYA2QhxBTysFjwUuUT4J+635XZ6iV9JShxidKMTDHQ6l6Z8zbgfwcRT smlTTGNo8bHdzjOcPHH3Pf+NLOJvEbr3QfXjGRQEMVYsApPpSRDnf754ndiCXcIKNm6a zUS/D/cJQZBgtot0Zg/6XhuV5be8/UWxSbSap0Hd4sXL0Br6b7+jzvoh5R8rRs4YlR4V 5TtMa7mXLgOtBRn2xeQ5+9SNRmpEju2o1rYYMPsCtNqLnJ7Kn8iHq6xx/tkU1O8PhLvH GvMg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=S3Abb8J9; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-61364-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61364-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=2; AJvYcCUAuHdA9jz6VesvpWg7r86mjcUkSVhhuH2ZKRxY8gbPg45WbXwFe0MBTAAesQaPHonmMk9OgUd6mCfVcmU7mHTdKj5kWoJ2EFR2DpsZnw== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id hu15-20020a056214234f00b0068cdda62595si79609qvb.383.2024.02.12.02.49.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 02:49:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-61364-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=S3Abb8J9; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-61364-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61364-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 461AA1C22D7E for ; Mon, 12 Feb 2024 10:49:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A33BF3D0A4; Mon, 12 Feb 2024 10:45:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="S3Abb8J9" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.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 B8FD53C487 for ; Mon, 12 Feb 2024 10:45:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707734714; cv=none; b=UezZweDOt7L3xPV3J7RudxramN38ADSPe/uWt9IRfuDSBaSXmhcZinPX+sCYHziDgBtowwbIzCAzHklKCCj0Q799zsfa25znBcWYq2vFmtm5OGEhToVMpupxZXEfMJUM2RE33Cjyl+LX/62sC84QNnBHEUX0MSMPYI5XXKU5+Gc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707734714; c=relaxed/simple; bh=284d6MdNJnzxJrEwLF3i2F8vgHGQ2onD2qQDE8fELP4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GjNQd9OyLOK0UTPdb7TaE2GwueCMBLy7fPUh38R1iWn60jNhf3mgskPWL8vWRI1v8zVBgmloXvIpo71qs9Z4GFTLDHfxG7qZUm1P4+42PnkZ+5jqPwmzo8zcQcTlAsddYY5qvnsqJKeqfZehBfe1+X8zJZwa5VH9daoBB1Hh7hU= 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=S3Abb8J9; arc=none smtp.client-ip=192.198.163.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=1707734712; x=1739270712; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=284d6MdNJnzxJrEwLF3i2F8vgHGQ2onD2qQDE8fELP4=; b=S3Abb8J9B+KW7mcaAVNacFvpW574YMG8rFNZe/c+pX6pRypbVH2HC/q2 36cemfk8Cn6SWqAalS6DNfJGkuPpf3ci47IN3Ly9GN5S6AT8UJPc5CrYC spSJtWL/JJaKgI9NowCzr/YdAAsPZE9ku41mQbJeLbtZq3xZIw9exjA8L QeV/uVuKhxdA1ypeO3V4SW2Xi3Ye287bnJQInp+GOAg1Umm+07TwrWcm0 yAkcjUmJqmrp0nQwy+idNoS1oml5W7BFce0D00UEcbXZoM3sgmI5RGqQm eq8BvmV474EaXIU60dV2UF8FGwS0zUZA0+FItFHzfmGmKwvC4EqgjZHdO A==; X-IronPort-AV: E=McAfee;i="6600,9927,10981"; a="1585127" X-IronPort-AV: E=Sophos;i="6.05,263,1701158400"; d="scan'208";a="1585127" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Feb 2024 02:45:05 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10981"; a="935035615" X-IronPort-AV: E=Sophos;i="6.05,262,1701158400"; d="scan'208";a="935035615" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 12 Feb 2024 02:45:00 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id 616FD604; Mon, 12 Feb 2024 12:44:53 +0200 (EET) 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" , Nikolay Borisov Subject: [PATCHv7 09/16] x86/mm: Adding callbacks to prepare encrypted memory for kexec Date: Mon, 12 Feb 2024 12:44:41 +0200 Message-ID: <20240212104448.2589568-10-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240212104448.2589568-1-kirill.shutemov@linux.intel.com> References: <20240212104448.2589568-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 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 --- arch/x86/include/asm/x86_init.h | 2 ++ arch/x86/kernel/crash.c | 6 ++++++ arch/x86/kernel/reboot.c | 12 ++++++++++++ arch/x86/kernel/x86_init.c | 4 ++++ 4 files changed, 24 insertions(+) diff --git a/arch/x86/include/asm/x86_init.h b/arch/x86/include/asm/x86_init.h index c9503fe2d13a..3196ff20a29e 100644 --- a/arch/x86/include/asm/x86_init.h +++ b/arch/x86/include/asm/x86_init.h @@ -154,6 +154,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 b6b044356f1b..3001f4857ed7 100644 --- a/arch/x86/kernel/crash.c +++ b/arch/x86/kernel/crash.c @@ -124,6 +124,12 @@ void native_machine_crash_shutdown(struct pt_regs *regs) #ifdef CONFIG_HPET_TIMER hpet_disable(); #endif + + if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) { + 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 830425e6d38e..0574d4ad6b41 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 (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) && 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 (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) && 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 f0f54e109eb9..b95206ebc621 100644 --- a/arch/x86/kernel/x86_init.c +++ b/arch/x86/kernel/x86_init.c @@ -135,6 +135,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 = { @@ -158,6 +160,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