From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.13]) (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 BA10D3BED40 for ; Thu, 26 Mar 2026 08:45:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.13 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774514743; cv=none; b=kAIWa8Bs/jrxGe0RVaHZEuQR9SQtqwDbRh3yinbTHrxC5GEXI3J2s14IiOFcsggvCyPeqwXcQLPgnj8pi1NTcaQNwhV4ciDAuqoPRZvG6QMS+1ACigyYPQdienmeMT2BagHO5+7kGRmuMJ85z6ER+LhE+NpMTzutYMOnAuwmbzs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774514743; c=relaxed/simple; bh=1fx+fAJLsWNxzd2CEiDJi6nvSpDeuresO1vN6drzTF4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KVrvzG1cM8TslWLChbl+d8xjhBcaiOub0PEJ5Mgc6G+oEGqtAQV1eSUeva7ErEHp9/fpsOi1VgcBz7G1JeJH4jgoTZ8OEeqkMLbw+sp3TF+QKlZntuKU4aQKxhzc9YKu4vqZuJAmvCd35kaDbOXDxreU63F/dUM0Wqj2cqnxjLE= 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=T67Xr8zx; arc=none smtp.client-ip=198.175.65.13 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="T67Xr8zx" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1774514741; x=1806050741; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1fx+fAJLsWNxzd2CEiDJi6nvSpDeuresO1vN6drzTF4=; b=T67Xr8zxpR3+GHLUlQZg3qfJqOQYVk52kJKUpxky0nN+VCEs46c2fuAU ZDYLTzPxCT2RvHGF8UJri+wPazcTuKPLJcMaJCSdKTWq1ufB3yKcKeCRI OOQN0K6Fqr4gQAC9XnfreEV4oQwwu17KvgyZpUuwKU2dBWxBqVxqU9yfh yNUtvFkQ+AMF4njqfx9UQSEPpBhXJlATQkrZP0qWPYR0zf8sFscRpRiWy 6FdarN4bPGdxQleNUdwZgK6PInb8dCqQsxrRHJxqpz/uVgx1k+7/LlO59 PZJG/A7tjsfavi6HWY8R2V8aHglYdL/4BDkm4UEwhHIEuqSi7e8scUvWr g==; X-CSE-ConnectionGUID: HsUp/nwXTiCfhvmfa4g8Yw== X-CSE-MsgGUID: HdL1a1I8R8iaEyRGlzS1Iw== X-IronPort-AV: E=McAfee;i="6800,10657,11740"; a="86644704" X-IronPort-AV: E=Sophos;i="6.23,141,1770624000"; d="scan'208";a="86644704" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by orvoesa105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Mar 2026 01:45:23 -0700 X-CSE-ConnectionGUID: 74eYlt/JSDesN3+1kt6+1Q== X-CSE-MsgGUID: AaH2xMepQV6TEu7e5NlPEg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,141,1770624000"; d="scan'208";a="224967218" Received: from 984fee019967.jf.intel.com ([10.23.153.244]) by orviesa008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Mar 2026 01:45:22 -0700 From: Chao Gao To: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, kvm@vger.kernel.org Cc: binbin.wu@linux.intel.com, dan.j.williams@intel.com, dave.hansen@linux.intel.com, ira.weiny@intel.com, kai.huang@intel.com, kas@kernel.org, nik.borisov@suse.com, paulmck@kernel.org, pbonzini@redhat.com, reinette.chatre@intel.com, rick.p.edgecombe@intel.com, sagis@google.com, seanjc@google.com, tony.lindgren@linux.intel.com, vannapurve@google.com, vishal.l.verma@intel.com, yilun.xu@linux.intel.com, xiaoyao.li@intel.com, yan.y.zhao@intel.com, Chao Gao , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH v6 12/22] x86/virt/tdx: Reset software states during TDX module shutdown Date: Thu, 26 Mar 2026 01:44:03 -0700 Message-ID: <20260326084448.29947-13-chao.gao@intel.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260326084448.29947-1-chao.gao@intel.com> References: <20260326084448.29947-1-chao.gao@intel.com> Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The TDX module requires a one-time global initialization (TDH.SYS.INIT) and per-CPU initialization (TDH.SYS.LP.INIT) before use. These initializations are guarded by software flags to prevent repetition. After TDX module updates, the new TDX module requires the same global and per-CPU initializations, but the existing software flags prevent re-initialization. Reset all software flags guarding the initialization flows to allow the global and per-CPU initializations to be triggered again after updates. Signed-off-by: Chao Gao Reviewed-by: Tony Lindgren Reviewed-by: Kai Huang --- v6: - reset tdx_lp_initialized for offlined CPUs and update the comment accordingly [Kai] v5: - add a comment to clarify why state access doesn't require holding a lock. [Kai] --- arch/x86/virt/vmx/tdx/tdx.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index f87fad429f4e..a2a46c734d5e 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -55,6 +55,8 @@ static struct tdmr_info_list tdx_tdmr_list; static enum tdx_module_status_t tdx_module_status; static DEFINE_MUTEX(tdx_module_lock); +static bool sysinit_done; +static int sysinit_ret; /* All TDX-usable memory regions. Protected by mem_hotplug_lock. */ static LIST_HEAD(tdx_memlist); @@ -70,8 +72,6 @@ static int try_init_module_global(void) { struct tdx_module_args args = {}; static DEFINE_RAW_SPINLOCK(sysinit_lock); - static bool sysinit_done; - static int sysinit_ret; lockdep_assert_irqs_disabled(); @@ -1179,6 +1179,7 @@ EXPORT_SYMBOL_FOR_KVM(tdx_enable); int tdx_module_shutdown(void) { struct tdx_module_args args = {}; + int ret, cpu; /* * Shut down the TDX module and prepare handoff data for the next @@ -1188,7 +1189,23 @@ int tdx_module_shutdown(void) * modules as new modules likely have higher handoff version. */ args.rcx = tdx_sysinfo.handoff.module_hv; - return seamcall_prerr(TDH_SYS_SHUTDOWN, &args); + ret = seamcall_prerr(TDH_SYS_SHUTDOWN, &args); + if (ret) + return ret; + + tdx_module_status = TDX_MODULE_UNINITIALIZED; + sysinit_done = false; + sysinit_ret = 0; + + /* + * Since the TDX module is shut down and gone, mark all CPUs + * (including offlined ones) as uninitialied. This is called in + * stop_machine() (where CPU hotplug is disabled), preventing + * races with other tdx_lp_initialized accesses. + */ + for_each_possible_cpu(cpu) + per_cpu(tdx_lp_initialized, cpu) = false; + return 0; } static bool is_pamt_page(unsigned long phys) -- 2.47.3