Received: by 2002:ab2:7903:0:b0:1fb:b500:807b with SMTP id a3csp438830lqj; Sun, 2 Jun 2024 05:44:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVhjxWaYLGj07sp9S9fLC0CboSQ6KI5V5HYf2xLdtV/ts22XWwqzLDUMVwQSeIUi4CyVgbcBlKJDreaRad/7aalHRhitm9HqjplVj/EyA== X-Google-Smtp-Source: AGHT+IEotXyLiQPY92zKWxNMZxhbTjaXBnb6dD7bAl2iKAs6Md7HW9mJ4FAeMF/Wk/YC4/Dojtlf X-Received: by 2002:a17:907:9867:b0:a59:d4f0:cfc3 with SMTP id a640c23a62f3a-a682204a7a5mr427098566b.59.1717332271884; Sun, 02 Jun 2024 05:44:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717332271; cv=pass; d=google.com; s=arc-20160816; b=pNH/grwFnKX+YGWyCiIwfgozn8YqjHVhodC8jiw8bWVEqeDD0AoRCfde07z6G0sHhA n9QvVjXIMWOKnsXZIqaeN/2+ATZ0jVf8ts6RpQnG+tMAuODxFP6wrCUd2jZPe/CxRLr3 icOiURrJc0FvtjZELueBR++4L47bw4gLLLffTI5pM7n2sIByBCWerOZK/1tHwjCo+Q2R WlohWbl/RQcSMxSgKcbVPyMP7Z24btUTWaZhsNFwQHRQgyNwqCmd3EKjxY8ICjjtEI9x eXPr4D/yxAhsDkthFGK+e76lKu2AexmyePI1jvvOJA+hsfbUVHrRAX1ostERAuXhgPOA axVw== 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=1Wkc7rKkiuyhysb40S2H/dvzAQXoetzfKXkY0vmf3VU=; fh=jwblSfZdM8dQT/9Q+j5jnCLA2Dr5ojbtvds5fqW6SUw=; b=s0jfIkmzHSF1DKPQLv89XWk+zuE1aoMx+UOMg3rUUDm9Hub7G8CHV+mEuvC4o71yVd TZJcSte3h95m9FAL0k7/WDWPWASBoL9DCaTHfqX5N9t2uRKyBW8wk+SKeYCNT3b2lLpW FboRjAGKd2VPIFa1YuyFmgvGBx9f6K7Hp/QczGGy/zXzt4N/wFtS/HsILO1QdZJtFjSP ojTf3bvacRNnh2LE2lnm/quwtfSFWAhM4Kve6PzCcH11Yf4Z6Nq72LXgmcZEwRy6YYf/ R01v6OZtmolex6jln6SRtg/B4gAm/plWfFxb8dxVpU+PJTd7SAsJBIDq6zGJ7ydQbupo b1GA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="Af14jfW/"; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-198238-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-198238-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. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a683aa7e891si275536666b.82.2024.06.02.05.44.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Jun 2024 05:44:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-198238-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="Af14jfW/"; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-198238-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-198238-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 6CD441F21336 for ; Sun, 2 Jun 2024 12:44:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 816EC3BB4D; Sun, 2 Jun 2024 12:44:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Af14jfW/" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) (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 37624376E4; Sun, 2 Jun 2024 12:44:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.15 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717332259; cv=none; b=UjpOIe3yOv3zRywSt4y0QBw+4BAnSQSz63Vl+GMG24eccuqhpDlVa4wKFOeMYkLyqG8eVe5+dlOr9vFflC7+zdl2e7x4kZWtAwSdpFTY1M1E0GNbiJ7iOWfJVjNTwde7tL84RdudHTSMTTpLA5PZrpoa78L1fyvrrFROG+12beY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717332259; c=relaxed/simple; bh=mkpeFBgjDljlkmrinZYmpoOZT5krUdTPk7Na7eE7CoY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=H5gyaXZ29x1dVgA7269V++RdWHggIIpIxt50egb9i7vrgX1USaPiv7AeMmj/cDcPPGyAyPr6Bd5GAEEKcZkr+0YHOnOuQLS+K/5yEbv82tjbEkp4JiO1dba2Fvs+sY8ra9tHufOgeOjFmqeQMV4OAhbUX0zlcy1mT4E6KzOMyKo= 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=Af14jfW/; arc=none smtp.client-ip=198.175.65.15 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=1717332259; x=1748868259; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mkpeFBgjDljlkmrinZYmpoOZT5krUdTPk7Na7eE7CoY=; b=Af14jfW/k7LabriRV1CHna3uPvgl/cmK49AmNDv8IfNbjT/yARtyxh3j QGm0Sb99vp8axvmDMJpH9YOlsjGM93T2+c9KEfaj3Mk+EyccWREEdbHpP 8yrXwwPAMKyzKyPDEDFGB23X5DVQwO/ccFHRlPxN1lEX6ZZVjOh2ZE88f KO+GRz+gUXhEASJcuWp1nVIOMozptiENE3S4mE9mmdyQ90UKxff6nIYXP 6nVkmfudauNDgwi2NO5Nk4hEQIQdhq3XJfCHyXUxv0Hnnqw2Wf7r2aRly WdWd3/7Fig8vr/FHgkfT4mY69OiwgOZ2n8K/SHVp9yLyVP6jMqqLE/p2l Q==; X-CSE-ConnectionGUID: +Kk1ibkuRVapr0zZQ0yLMg== X-CSE-MsgGUID: BpiVLiExSDuPhFTQEur35A== X-IronPort-AV: E=McAfee;i="6600,9927,11091"; a="17624457" X-IronPort-AV: E=Sophos;i="6.08,209,1712646000"; d="scan'208";a="17624457" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jun 2024 05:44:18 -0700 X-CSE-ConnectionGUID: XVqrcbSBS3qNM9oIMac+pw== X-CSE-MsgGUID: KeLOpNPdQSuX7HJAShotqQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,209,1712646000"; d="scan'208";a="59792867" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa002.fm.intel.com with ESMTP; 02 Jun 2024 05:44:11 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 8DDF51CB; Sun, 02 Jun 2024 15:44:10 +0300 (EEST) From: "Kirill A. Shutemov" To: bp@alien8.de Cc: adrian.hunter@intel.com, ardb@kernel.org, ashish.kalra@amd.com, bhe@redhat.com, dave.hansen@linux.intel.com, elena.reshetova@intel.com, haiyangz@microsoft.com, hpa@zytor.com, jun.nakajima@intel.com, kai.huang@intel.com, kexec@lists.infradead.org, kirill.shutemov@linux.intel.com, kys@microsoft.com, linux-acpi@vger.kernel.org, linux-coco@lists.linux.dev, linux-hyperv@vger.kernel.org, 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: [PATCHv11.2 10/19] x86/mm: Add callbacks to prepare encrypted memory for kexec Date: Sun, 2 Jun 2024 15:44:03 +0300 Message-ID: <20240602124403.2122693-1-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240529104257.GIZlcGsTkJHVBblkrY@fat_crate.local> References: <20240529104257.GIZlcGsTkJHVBblkrY@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_begin() stops new conversions. - enc_kexec_finish() unshares all existing shared memory, reverting it back to private. Signed-off-by: Kirill A. Shutemov Reviewed-by: Nikolay Borisov Reviewed-by: Kai Huang Tested-by: Tao Liu Signed-off-by: Kirill A. Shutemov --- arch/x86/include/asm/x86_init.h | 12 ++++++++++++ arch/x86/kernel/crash.c | 12 ++++++++++++ arch/x86/kernel/reboot.c | 12 ++++++++++++ arch/x86/kernel/x86_init.c | 4 ++++ 4 files changed, 40 insertions(+) diff --git a/arch/x86/include/asm/x86_init.h b/arch/x86/include/asm/x86_init.h index 28ac3cb9b987..b0f313278967 100644 --- a/arch/x86/include/asm/x86_init.h +++ b/arch/x86/include/asm/x86_init.h @@ -149,12 +149,24 @@ struct x86_init_acpi { * @enc_status_change_finish Notify HV after the encryption status of a range is changed * @enc_tlb_flush_required Returns true if a TLB flush is needed before changing page encryption status * @enc_cache_flush_required Returns true if a cache flush is needed before changing page encryption status + * @enc_kexec_begin Begin the two-step process of converting shared memory back + * to private. It stops the new conversions from being started + * and waits in-flight conversions to finish, if possible. + * The @crash parameter denotes whether the function is being + * called in the crash shutdown path. + * @enc_kexec_finish Finish the two-step process of converting shared memory to + * private. All memory is private after the call when + * the function returns. + * It is called on only one CPU while the others are shut down + * and with interrupts disabled. */ struct x86_guest { int (*enc_status_change_prepare)(unsigned long vaddr, int npages, bool enc); 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_begin)(bool crash); + void (*enc_kexec_finish)(void); }; /** diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c index f06501445cd9..fc52ea80cdc8 100644 --- a/arch/x86/kernel/crash.c +++ b/arch/x86/kernel/crash.c @@ -128,6 +128,18 @@ void native_machine_crash_shutdown(struct pt_regs *regs) #ifdef CONFIG_HPET_TIMER hpet_disable(); #endif + + /* + * Non-crash kexec calls enc_kexec_begin() while scheduling is still + * active. This allows the callback to wait until all in-flight + * shared<->private conversions are complete. In a crash scenario, + * enc_kexec_begin() gets called after all but one CPU have been shut + * down and interrupts have been disabled. This allows the callback to + * detect a race with the conversion and report it. + */ + x86_platform.guest.enc_kexec_begin(true); + x86_platform.guest.enc_kexec_finish(); + crash_save_cpu(regs, safe_smp_processor_id()); } diff --git a/arch/x86/kernel/reboot.c b/arch/x86/kernel/reboot.c index f3130f762784..097313147ad3 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_begin() 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_begin(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_finish(); } 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..8a79fb505303 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_begin_noop(bool crash) {} +static void enc_kexec_finish_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_begin = enc_kexec_begin_noop, + .enc_kexec_finish = enc_kexec_finish_noop, }, }; -- 2.43.0