Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp794910lqt; Fri, 19 Apr 2024 10:33:31 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWk8QwfzleByhEj+cEEoRqqcY4O6YEGGRgirZFU/XPk9QfH3rubOPdoetlmCQe+28Swy3G74ZcAevMC1wcByRgXa63X++q9XXZLHjIK5g== X-Google-Smtp-Source: AGHT+IGzzqIPUrqisf868ZhaXnBdTe5pK5OVkDiDczqxhegtrD7CvNNcY4ls0c8+E5hcbdNcQa11 X-Received: by 2002:a17:903:32cd:b0:1e6:622c:7bb4 with SMTP id i13-20020a17090332cd00b001e6622c7bb4mr3863216plr.19.1713548011040; Fri, 19 Apr 2024 10:33:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713548011; cv=pass; d=google.com; s=arc-20160816; b=xpVZ42WW2aGzBEK4afymsSpPx7JcHG1vtX3wOePjFYVVHme+jgWDg/Uul7m7/mfI3s E7gnF6vuJDeaSHnL8t8MCaZ39IdgnzC9GdPRziCnO7ETdDLSuqkgeXVVKoSVpWPTJbDk 8rYJpEv0cUYLZbk1mp0/DDnShwOVcPjHbg4EIv5IQS1lGS4+e4OLDe/WeWQFX1ST3dCl BvuxuKonajGrjOB9wsARiCNjnKYqYYW6yvvQsivEZz82s4EfSoM8bX0CdYWHdKLUsCmp qtT7smzmw2a7Kq6g6Gv7ifDn6BF2ZVZjoEktqiDb7udqnDkIV4vezgG3/4xTtbFFeDS9 OeAg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=5WPGtsMCwemSIr+JDp0r8nt/aMHu/Jjken9XpaKI2j4=; fh=/J0qDSADA9/qoF8uhFr9bgcj7GgEN36Xm5//6ixTOXw=; b=Jy6EF94z6eXH6JYn9kXtKzUbdyYKdvB9BYkD1usbPmFStCX80VT3egMuvicpPMhEIo MgaNyVmR/+z/YLid3ejuu44ZAjb1mCmbEczUytnyIihGXoXmF3+DFs4/1YhjSKsrN3s5 Rp1kx9LqD9DukQcsaDUAJ7VYnZ99hOsOGlzjpREd8ig2HdGQPb0LniGALzfQ2/fV1NXW iet3uQkfBPd7Q6Sho5OVqufFgUvKbf9W8x1/P2uyjudhIFdFSKyMgpoLyR0GBv4chAE5 UFLZ+e48W3E+Eqr74ONae3IQbZiIPv0BqS69138rw8NOhcWJqfeT3rK64KJF2LE33Qm1 M3+g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=I2wLMuhE; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-151818-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151818-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id kd15-20020a17090313cf00b001e5886e2a55si3797690plb.54.2024.04.19.10.33.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 10:33:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-151818-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=I2wLMuhE; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-151818-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151818-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id C0387286C18 for ; Fri, 19 Apr 2024 17:24:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C62C713AA53; Fri, 19 Apr 2024 17:23:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="I2wLMuhE" Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 802378004E for ; Fri, 19 Apr 2024 17:23:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713547435; cv=none; b=Jr8GvK46tbxQ7pRwYP/ZHzkOFGYdhBuFmX/0ZR2AtDobnrXP8YX/jKd4mSZJ/BJQ4Eypi+kh2DKbgPehqQAo/7o/L3gh2cZnkxLthcvc84W7+km+g46EYATzqzOnexKCIO1FfbF+L/tv58y/1tSeDi5aq10vcgOCor+FFHXHFD0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713547435; c=relaxed/simple; bh=4zqi94Uc/R8iBsYBBImj/1Cm2SNGTxyyrkhMn0eenOY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DVM3ILNav3buwuL/aRRUOTJv7zrxh2Re5XlHeSH2HXsP22vXTX6+TYyIfqMJsmYdpUocBNZJD6xCvyp4ifLR3j5+BCFYyNPvme4vmotSV6sH3/z39qw2pVW0MHck7PZTg3XmX6fH/tznWC+9htfdvV1qCgvp9JIkwixURVieWQQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=I2wLMuhE; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2a2c4a28e43so2531577a91.2 for ; Fri, 19 Apr 2024 10:23:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713547434; x=1714152234; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5WPGtsMCwemSIr+JDp0r8nt/aMHu/Jjken9XpaKI2j4=; b=I2wLMuhEPUQlJ/JAHQd9rpfMn/rWXxVBCSRNRSosBQRBgPfVNs4g1jYZbKF4IMMNJT 3j/U0j+RL13ceVq5SYdZGRRImSTZYybS6SFvd0ZtEERCoXiQdo2S++wgCpt8BSv7w0Pr AZ9f/Y8l+J24Q00EL8nnxMVvrrQPKSb2dUU/TKIDc9Ynb/rJ6EirRpm8jDxJXihbs3zn cEps/grfphrNm8UsWINftQ3E8BAUDjJZqb2ASHf8yxDQr2Z9E+GAZw0earqMDm4Mh5ep CIxD5tl87957emeXdBO2WzLKh4hP19MZWAlX0Ssja9mbXchQuz2a6EZsC86DJTeDS36D 099w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713547434; x=1714152234; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5WPGtsMCwemSIr+JDp0r8nt/aMHu/Jjken9XpaKI2j4=; b=f6IYlHogPkfGTqw2TUKLOjwFC/VQRLJKxApZNx6tqTVyBmbNVrdo6/tXMCGeGen8EF LyFXdAysM6Z383lyQNgaVgYNOdpm5Iyv4xZ53fg0BDE42RFDFWkX9s+6iL7xrvLQWdTF MmgpcXgV+nrM/IBJIWaP/RgYOy3PXpVAwc7YD21DCVBCEGIOiA7HBJcJc88ajotDwpfh XQjAy2tpBUckBrCfkMMpuZJOpxmJRhi9IBjry3zB93Me+OXmrftNcxTJfa24yfrmkweD 6yGxyBoSBWKA3d1vFJXK42YILdG/gfbX3SWcSzdHJ+5sgevdvRBs1XzmpF1GawkqZDlu uknA== X-Forwarded-Encrypted: i=1; AJvYcCU6whz8idHMdQZWHvZD46NWFAPs1t9f3Hg5g1Rr/d4vecuf+XQiCZbizYyt19+0RwsFr4fNeyY0vq68xU15wUv1cWYHtV2vhlPKDw/o X-Gm-Message-State: AOJu0YyrbOfW4BSvOmzFMMLeW7m3RzTuxnTeZU2/+EinIG8uIe7MqBkc sMQEGb27P/nswluzig3lK5HLfZWzsyVoOlEsjK5n4gyitcGTOOqXe812WJX434ottt4mbFlbEPM wHA== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:f411:b0:2ac:3c42:57ea with SMTP id ch17-20020a17090af41100b002ac3c4257eamr8941pjb.5.1713547433583; Fri, 19 Apr 2024 10:23:53 -0700 (PDT) Date: Fri, 19 Apr 2024 10:23:52 -0700 In-Reply-To: <22b19d11-056c-402b-ac19-a389000d6339@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <6cd2a9ce-f46a-44d0-9f76-8e493b940dc4@intel.com> <900fc6f75b3704780ac16c90ace23b2f465bb689.camel@intel.com> <2383a1e9-ba2b-470f-8807-5f5f2528c7ad@intel.com> <5ffd4052-4735-449a-9bee-f42563add778@intel.com> <22b19d11-056c-402b-ac19-a389000d6339@intel.com> Message-ID: Subject: Re: [PATCH v19 023/130] KVM: TDX: Initialize the TDX module when loading the KVM intel kernel module From: Sean Christopherson To: Kai Huang Cc: Tina Zhang , Hang Yuan , Bo2 Chen , "sagis@google.com" , "isaku.yamahata@gmail.com" , "linux-kernel@vger.kernel.org" , Erdem Aktas , "kvm@vger.kernel.org" , "pbonzini@redhat.com" , Isaku Yamahata , "isaku.yamahata@linux.intel.com" Content-Type: text/plain; charset="us-ascii" On Fri, Apr 19, 2024, Kai Huang wrote: > On 19/04/2024 2:30 am, Sean Christopherson wrote: > > No, that will deadlock as cpuhp_setup_state() does cpus_read_lock(). > > Right, but it takes cpus_read_lock()/unlock() internally. I was talking > about: > > if (enable_tdx) { > kvm_x86_virtualization_enable(); > > /* > * Unfortunately currently tdx_enable() internally has > * lockdep_assert_cpus_held(). > */ > cpus_read_lock(); > tdx_enable(); > cpus_read_unlock(); > } Ah. Just have tdx_enable() do cpus_read_lock(), I suspect/assume the current implemention was purely done in anticipation of KVM "needing" to do tdx_enable() while holding cpu_hotplug_lock. And tdx_enable() should also do its best to verify that the caller is post-VMXON: if (WARN_ON_ONCE(!(__read_cr4() & X86_CR4_VMXE))) return -EINVAL; > > > Btw, why couldn't we do the 'system_state' check at the very beginning of > > > this function? > > > > We could, but we'd still need to check after, and adding a small bit of extra > > complexity just to try to catch a very rare situation isn't worth it. > > > > To prevent races, system_state needs to be check after register_syscore_ops(), > > because only once kvm_syscore_ops is registered is KVM guaranteed to get notified > > of a shutdown. > > > And because the kvm_syscore_ops hooks disable virtualization, they should be called > > after cpuhp_setup_state(). That's not strictly required, as the per-CPU > > hardware_enabled flag will prevent true problems if the system enter shutdown > > state before KVM reaches cpuhp_setup_state(). > > > > Hmm, but the same edge cases exists in the above flow. If the system enters > > shutdown _just_ after register_syscore_ops(), KVM would see that in system_state > > and do cpuhp_remove_state(), i.e. invoke kvm_offline_cpu() and thus do a double > > disable (which again is benign because of hardware_enabled). > > > > Ah, but registering syscore ops before doing cpuhp_setup_state() has another race, > > and one that could be fatal. If the system does suspend+resume before the cpuhup > > hooks are registered, kvm_resume() would enable virtualization. And then if > > cpuhp_setup_state() failed, virtualization would be left enabled. > > > > So cpuhp_setup_state() *must* come before register_syscore_ops(), and > > register_syscore_ops() *must* come before the system_state check. > > OK. I guess I have to double check here to completely understand the races. > :-) > > So I think we have consensus to go with the approach that shows in your > second diff -- that is to always enable virtualization during module loading > for all other ARCHs other than x86, for which we only always enables > virtualization during module loading for TDX. Assuming the other arch maintainers are ok with that approach. If waiting until a VM is created is desirable for other architectures, then we'll need to figure out a plan b. E.g. KVM arm64 doesn't support being built as a module, so enabling hardware during initialization would mean virtualization is enabled for any kernel that is built with CONFIG_KVM=y. Actually, duh. There's absolutely no reason to force other architectures to choose when to enable virtualization. As evidenced by the massaging to have x86 keep enabling virtualization on-demand for !TDX, the cleanups don't come from enabling virtualization during module load, they come from registering cpuup and syscore ops when virtualization is enabled. I.e. we can keep kvm_usage_count in common code, and just do exactly what I proposed for kvm_x86_enable_virtualization(). I have patches to do this, and initial testing suggests they aren't wildly broken. I'll post them soon-ish, assuming nothing pops up in testing. They are clean enough that they can land in advance of TDX, e.g. in kvm-coco-queue even before other architectures verify I didn't break them. > Then how about "do kvm_x86_virtualization_enable() within > late_hardware_setup() in kvm_x86_vendor_init()" vs "do > kvm_x86_virtualization_enable() in TDX-specific code after > kvm_x86_vendor_init()"? > > Which do you prefer? The latter, assuming it doesn't make the TDX code more complex than it needs to be. The fewer kvm_x86_ops hooks, the better.