Received: by 2002:a05:6500:1b45:b0:1f5:f2ab:c469 with SMTP id cz5csp1247958lqb; Thu, 18 Apr 2024 04:50:06 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX8kOuZoK7Y4GZghpUdUiKX9PX9gYImiQajsnBXZKWbCfMneK4m++ad4+qZCLqCYIzDP/NPDuf/34hlu/eN3gC1649bzLiPFCsQkKg39Q== X-Google-Smtp-Source: AGHT+IEmA072SyNYF/330Tm82XtryHAnbduu4HmdhcGcReslJPOmKQR7PWnf1UvkP55L1lQ8PrV9 X-Received: by 2002:a05:622a:1b8d:b0:436:1139:25b1 with SMTP id bp13-20020a05622a1b8d00b00436113925b1mr2774147qtb.19.1713441005773; Thu, 18 Apr 2024 04:50:05 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713441005; cv=pass; d=google.com; s=arc-20160816; b=z1hJC04fYzzuclnSPIxqLEPi7tJ/jxZMgwV1TTBecN/j+tGxxA/8oGM+8cCKazL30V Y6df6kFwFc0dGGxWC8HZkBGiwrOtBdTsFl9UdoI//7BtZpNIka/i7zNEG2aI4vmmjah/ sG0pmNCxJku7a5wLo5Zt2NgPMq7bZjZM+y6Kp6/qeCmjrGh8KFF9NU5x16BDfiKEEQX9 VrXRXPWipCabOSUPnkdF58O0QRgNGfDI2r24HvVixAWYt+j0waaQFGKHXg/JVNcNgn6H sqpPuSLYqxDk1ankr6I7MdcoxsMpsgj3Sr6Z99owAPZP0V6k3wI0bGhTwsmwNdV2Ofd+ RJpg== 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=D/nurO+weMC1pExZ6VZfMuLyDdEzuoEmU2W4tGKDWhg=; fh=VTqdLKNapz3kVcaNXwmpu/9dDcq2MEeWblnhClqCYpA=; b=UcYEyJsSWZW36W1NnfpmQik2EpgjqJXuwS0s2UzvtnAnSk5Ibn9GGiLRsLODZ4a7JX mxlEQeUPzmw7snjVNCWjR8OJYPDImtCIOyFehrTMZoziUmtRJAHSEBT0E3NbovTVU98z HqU1OjKNrdCUBYerEjBC19UxILh7XJglvgflISrmXRo1cc4Q5Vee4z508A75+gSxoyDy xeXqN4gn3GkMfK4Vx+C3DWI4as2fXxsWd0i2DGAB2Oos7/2eCstIu4zPAK1CGUrPn5/M 6N5HsNPMvSexbrmScjEDQkHkWTUcTgrZQJNB33piqAQOum+WDShWTM7613QHCUTClxhw gSsg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MtRLS6bq; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-150005-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-150005-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id n12-20020a05622a040c00b00431817ba3a8si1327751qtx.761.2024.04.18.04.50.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 04:50:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-150005-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MtRLS6bq; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-150005-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-150005-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 71E811C2118E for ; Thu, 18 Apr 2024 11:50:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C93091607BF; Thu, 18 Apr 2024 11:48:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="MtRLS6bq" 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 477AD1607BD for ; Thu, 18 Apr 2024 11:48:40 +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=1713440921; cv=none; b=cNMnC/BTh0wx7uye/w6CnDvWrLQmYm8iZmj6r1Ten4S8ZTNuMpD4+iDaYOucxl+bBc6zhlcLeIYJlhTUMkHcMriTDP+EZdD9UcJA89xBd3S6DDJN7QJ6DOUFOuEPUIUewoDWvrswiIUVx9Ds933xRZBcCKliF4qJwhQ/uVlMdV0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713440921; c=relaxed/simple; bh=w5AQltEjcfuaA8t+xAbaDYXarkRZWOmtHIHnBJWtT88=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=clCBVFEII9icdZhOIhygLdMWl8f8Eho1LY8MnhtCwSZieFLTqSRJ6N5fTY+dwHa83gvnYwKOZT5C9Lm4S410NOSr4WsIAGkYU4+11XepTNw17bFDKLV7y5JcoipKPNDEwMoSFC5wrwWfzyD/OA3cKkxirjZEyFVBgJWAr6Y4TZE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=MtRLS6bq; arc=none smtp.client-ip=198.175.65.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1713440921; x=1744976921; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=w5AQltEjcfuaA8t+xAbaDYXarkRZWOmtHIHnBJWtT88=; b=MtRLS6bqvAwzSkXbfcMxWldT0Y/zqSnjQ2vdQjgoGvRMH3MqDAxki25e bWaBU3PG2+q7Sb/gvmlK4diG/56o3qGAX7HKLu4NPU7gugX2jlIcr330C VxC00u40t0XAaPCDaaEuDOffnFQ3imYpCuPiuyrnIkaUiJi+Barchjzbm gO4gKdgI0GPt/7eeivYoFshGUzoniSWPaw6/xHj5K00nN/pMd5lOXzh7b ZOHQZ45RR8c+AuDf/qrbELNDZ+LSzIiQtIN9LLp2FMe7NRrIgs2msdsfD ulsdS9mLHKKtVTBC/piYeiZq4k6PV4dn/7O5iKhEl8wOr6hKl6AXNxnUs g==; X-CSE-ConnectionGUID: tPimh6ZbRS6FVrD3QlZmWg== X-CSE-MsgGUID: +djylzeqRciLgaEx8Dk13A== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="12769428" X-IronPort-AV: E=Sophos;i="6.07,212,1708416000"; d="scan'208";a="12769428" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 04:48:40 -0700 X-CSE-ConnectionGUID: SEXsAfl3T7C0eqhQY5U4gw== X-CSE-MsgGUID: +Y1TomJCR4GxpG7qxyYYEw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,212,1708416000"; d="scan'208";a="23019924" Received: from vgannu-mobl.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.212.134.136]) by fmviesa010-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Apr 2024 04:48:35 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@intel.com, bp@alien8.de, kirill.shutemov@linux.intel.com, tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, luto@kernel.org, peterz@infradead.org, rick.p.edgecombe@intel.com, thomas.lendacky@amd.com, ashish.kalra@amd.com, chao.gao@intel.com, bhe@redhat.com, nik.borisov@suse.com, pbonzini@redhat.com, seanjc@google.com Subject: [PATCH v4 5/5] x86/virt/tdx: Add TDX memory reset notifier to reset other private pages Date: Thu, 18 Apr 2024 23:48:05 +1200 Message-ID: X-Mailer: git-send-email 2.43.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit TL;DR: To cover both normal kexec and crash kexec, add a TDX specific memory reset notifier to let "in-kernel TDX users" use their own way to convert TDX private pages (that they manage respectively) in tdx_reset_memory(). Long version: On the platforms with TDX "partial write machine check" erratum, during kexec, the kernel needs to convert TDX private memory back to normal before jumping to the second kernel to avoid the second kernel seeing potential machine check. For now tdx_reset_memory() only resets PAMT pages. KVM will be the first in-kernel TDX user to support running TDX guests, and by then other TDX private pages will start to exist. They need to be covered too. Currently the kernel doesn't have a unified way to tell whether a given page is TDX private page or not. One choice is to add such unified way, and there are couple of options to do it: 1) Use a bitmap, or Xarray, etc to track TDX private page for all PFNs; 2) Use a "software-only" bit in the direct-mapping PTE to mark a given page is TDX private page; 3) Use a new flag in 'struct page' to mark TDX private page; 4) ... potential other ways. Option 1) consumes additional memory. E.g., if using bitmap, the overhead is "number of total RAM pages / 8" bytes. Option 2) would cause splitting large-page mapping to 4K mapping in the direct mapping when one page is allocated as TDX private page, and cause additional TLB flush etc. It's not ideal for such use case. Option 3) apparently contradicts to the effort to reduce the use of the flags of 'struct page'. None of above is ideal. Therefore, instead of providing a unified way to tell whether a given page is TDX private page or not, leave "resetting TDX private pages" to the "in-kernel user" of TDX. This is motivated by the fact that KVM is already maintaining an Xarray to track "memory attributes (e.g., private or shared)" for each GFN for each guest. Thus KVM can use its own way to find all TDX private pages that it manages and convert them back to normal. For the normal kexec the reboot notifier could be used, but it doesn't cover the cash kexec. Add a TDX specific memory reset notifier to achieve this. The in-kernel TDX users will need to register their own notifiers to reset TDX private pages. Call these notifiers in tdx_reset_memory() right before resetting PAMT pages. KVM will be the first user of this notifier. Export the "register" and "unregister" APIs for KVM to use. Signed-off-by: Kai Huang --- arch/x86/include/asm/tdx.h | 14 ++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 45 +++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index ed3ac9a8a079..7c2c0a0b9754 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -117,12 +117,26 @@ int tdx_cpu_enable(void); int tdx_enable(void); const char *tdx_dump_mce_info(struct mce *m); void tdx_reset_memory(void); + +struct notifier_block; + +int tdx_register_memory_reset_notifier(struct notifier_block *nb); +void tdx_unregister_memory_reset_notifier(struct notifier_block *nb); #else static inline void tdx_init(void) { } static inline int tdx_cpu_enable(void) { return -ENODEV; } static inline int tdx_enable(void) { return -ENODEV; } static inline const char *tdx_dump_mce_info(struct mce *m) { return NULL; } static inline void tdx_reset_memory(void) { } + +struct notifier_block; + +static inline int tdx_register_memory_reset_notifier(struct notifier_block *nb) +{ + return -EOPNOTSUPP; +} +static inline void tdx_unregister_memory_reset_notifier( + struct notifier_block *nb) { } #endif /* CONFIG_INTEL_TDX_HOST */ #endif /* !__ASSEMBLY__ */ diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 7f5d388c5461..af62fbffcd96 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -54,6 +55,8 @@ static LIST_HEAD(tdx_memlist); static bool tdx_may_have_private_memory __read_mostly; +static BLOCKING_NOTIFIER_HEAD(tdx_memory_reset_chain); + typedef void (*sc_err_func_t)(u64 fn, u64 err, struct tdx_module_args *args); static inline void seamcall_err(u64 fn, u64 err, struct tdx_module_args *args) @@ -1511,6 +1514,27 @@ void __init tdx_init(void) check_tdx_erratum(); } +int tdx_register_memory_reset_notifier(struct notifier_block *nb) +{ + return blocking_notifier_chain_register(&tdx_memory_reset_chain, nb); +} +EXPORT_SYMBOL_GPL(tdx_register_memory_reset_notifier); + +void tdx_unregister_memory_reset_notifier(struct notifier_block *nb) +{ + blocking_notifier_chain_unregister(&tdx_memory_reset_chain, nb); +} +EXPORT_SYMBOL_GPL(tdx_unregister_memory_reset_notifier); + +static int notify_reset_memory(void) +{ + int ret; + + ret = blocking_notifier_call_chain(&tdx_memory_reset_chain, 0, NULL); + + return notifier_to_errno(ret); +} + void tdx_reset_memory(void) { if (!boot_cpu_has(X86_FEATURE_TDX_HOST_PLATFORM)) @@ -1553,18 +1577,15 @@ void tdx_reset_memory(void) native_wbinvd(); /* - * It's ideal to cover all types of TDX private pages here, but - * currently there's no unified way to tell whether a given page - * is TDX private page or not. - * - * Just convert PAMT pages now, as currently TDX private pages - * can only be PAMT pages. - * - * TODO: - * - * This leaves all other types of TDX private pages undealt - * with. They must be handled in _some_ way when they become - * possible to exist. + * Tell all in-kernel TDX users to reset TDX private pages + * that they manage. + */ + if (notify_reset_memory()) + pr_err("Failed to reset all TDX private pages.\n"); + + /* + * The only remaining TDX private pages are PAMT pages. + * Reset them. */ tdmrs_reset_pamt_all(&tdx_tdmr_list); } -- 2.43.2