Received: by 2002:a05:7412:98c1:b0:fa:551:50a7 with SMTP id kc1csp1709302rdb; Mon, 8 Jan 2024 07:53:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IGO2KBrwhUb+o6Gwg5PX9+h+7rAJkmR2QqybSPEvEOyKXT3Sgy0yrDXBS/qnr+8kploODFh X-Received: by 2002:a17:902:c952:b0:1d5:251:15d9 with SMTP id i18-20020a170902c95200b001d5025115d9mr1794776pla.136.1704729195228; Mon, 08 Jan 2024 07:53:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704729195; cv=none; d=google.com; s=arc-20160816; b=FJp1Q0Zp5IARg4MVaXffnYISdqBC2NkQxXV7o7fLpOAFX31zN2seK56+IA2ZhGoyTZ KR6YJ6lZ7rPTqh5SZ4go1sbbHBWXUZT+ZWqC/36ssNSFmouPNcXH6vpXuXyuUG5WVPJp 1YUq1uqwrW2lFkXobAm/Sjdxx/ZYZmSvcDHhbAcYTDWeu9PhzbkFuOSOgivwle3n0TPd EwNQ4g/HuIYCYt+f4WZYtOJZ4NQULwwzUcZdBql28RYijcPvdn3vbx9QC14KIgNpVw5X kinl0lfBEQG16/UFzSOnKiZerRgwzowXEBqv0j7F0sCFekfMQlPu3bEk36VQ9rJIIo7N OEAQ== ARC-Message-Signature: i=1; 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=LXpfvDdR25mi61ZRv+hUGYfziJrbv6+q0uR9W3pdtgw=; fh=f15ODaj2IY2nkjRr09nV0PESmEif8C/S7qr9tDmUJqw=; b=PfVgcOvJEJWtZjcNjdd93mDCSzBRk/DGZ8/3NUBD+XPAyzwN8UwC1rU7REpguungX1 /sWeTt9i4X2PdDpnaRjTOin8c4GRb+oyHeAxR+07cJ2zA7AmvpsBXDB8hVuJGKlEGTeK kOs5kt8jbjfgimTONL4Y30+vXBpN4Ne/ryGMycJvR8twFiwV3wwe+53Y/NT8mG32YlvR blzMV6VRyWNDs4KYHpxi9I0etHCPVy4Bv+K9cp2C5XvkQcUwuFezl1QjVvuKCjL4Qu0X qK4ZGkWfFbLNNBMhBxa6c75tj2J0fX8wyVKA4gE7SnsnD31JYEu1d/XLZOMUUhbp1Zex V1vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ScUJdTf6; spf=pass (google.com: domain of linux-crypto+bounces-1284-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-crypto+bounces-1284-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id h5-20020a170902f54500b001d4d7730bf9si74443plf.42.2024.01.08.07.53.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 07:53:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto+bounces-1284-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ScUJdTf6; spf=pass (google.com: domain of linux-crypto+bounces-1284-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-crypto+bounces-1284-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 5BF2AB2138A for ; Mon, 8 Jan 2024 15:53:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A050651C2B; Mon, 8 Jan 2024 15:53:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ScUJdTf6" X-Original-To: linux-crypto@vger.kernel.org Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 1CF125101D for ; Mon, 8 Jan 2024 15:53:04 +0000 (UTC) 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-yb1-f202.google.com with SMTP id 3f1490d57ef6-dbed7ba6545so2247004276.1 for ; Mon, 08 Jan 2024 07:53:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704729184; x=1705333984; 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=LXpfvDdR25mi61ZRv+hUGYfziJrbv6+q0uR9W3pdtgw=; b=ScUJdTf6pY1EyzXXhTCOXHjVT97d9to7GQ83OVVOdFBTRhGje7AGwchBSTlrSGUrYQ VFbN4vMFF6Hla0gcX4RSkXf6EB87oabE/uJ0REVkG1YoQnSO9DHgfBiIqpjFeVPpYgzK HXHrCrHGNHHUcptvmkPg86uQDGNIfA4Fh/ldNw7mjAzVqIsfH6Lfsq5tLDEKfe/MjSVD Ejfo41gQFHZtoLFYZpVLtvZAUILVJCvSpfdWYL5sbxmnQCSYpM4snT6X7Do0wUrHWdlt SDtLaa4/o7/jEn/nOJYlnSZIvd4GiuE5MFW8M3iYnzZS699H8VSVAX1CKzzhaTLXF02A Cn7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704729184; x=1705333984; 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=LXpfvDdR25mi61ZRv+hUGYfziJrbv6+q0uR9W3pdtgw=; b=KwmS+C3er9LPC3K9zDghuCO4AtHp11yYRHmtzP4dYVIJM63ZaWdhSSgchLmz2oX/3u WUaoWBk+WcjV9NeA1brgtmUrFsM4ha5dzoht2/1daiNYdHKMW/fzLUDx3S9rcMoAX4ic i4mNEGJXk8fe/Q+R7p6EhhmIdsuU4/BsYUG41CbbfoTVbjwp3DgZynOgOAl/gGUDkkIt aHTSqeaLOWfVbbcUELSFwRmncSW1xV4ml+bpKLxopkV9Ow6c1hkxZt0PGsRytFlnTmn9 F5UpWa+z5HtN3nvW3XacfgRdqGfABC4mi2jgjBhznj+HlSUGbRMYuwvtIQPJY9ceMNEZ cNTw== X-Gm-Message-State: AOJu0Yx9GRvLNeJq7ZuhY9bnbWcWgB689qyhkupCh7FKihw6sq1T6Hsf fAtkLCuuIHToiHnqav3QXMLtorwnD0BTXaVYTw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:83d0:0:b0:dbe:a20a:6330 with SMTP id v16-20020a2583d0000000b00dbea20a6330mr1497318ybm.9.1704729184054; Mon, 08 Jan 2024 07:53:04 -0800 (PST) Date: Mon, 8 Jan 2024 07:53:02 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231230172351.574091-1-michael.roth@amd.com> <20231230172351.574091-27-michael.roth@amd.com> Message-ID: Subject: Re: [PATCH v11 26/35] KVM: SEV: Support SEV-SNP AP Creation NAE event From: Sean Christopherson To: Jacob Xu Cc: Michael Roth , kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, jroedel@suse.de, thomas.lendacky@amd.com, hpa@zytor.com, ardb@kernel.org, pbonzini@redhat.com, vkuznets@redhat.com, jmattson@google.com, luto@kernel.org, dave.hansen@linux.intel.com, slp@redhat.com, pgonda@google.com, peterz@infradead.org, srinivas.pandruvada@linux.intel.com, rientjes@google.com, dovmurik@linux.ibm.com, tobin@ibm.com, bp@alien8.de, vbabka@suse.cz, kirill@shutemov.name, ak@linux.intel.com, tony.luck@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, alpergun@google.com, jarkko@kernel.org, ashish.kalra@amd.com, nikunj.dadhania@amd.com, pankaj.gupta@amd.com, liam.merwick@oracle.com, zhi.a.wang@intel.com, Brijesh Singh , Adam Dunlap Content-Type: text/plain; charset="us-ascii" On Fri, Jan 05, 2024, Jacob Xu wrote: > > + if (kick) { > > + if (target_vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED) > > + target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; > > + > > + kvm_make_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, target_vcpu); > > I think we should switch the order of these two statements for > setting mp_state and for making the request for > KVM_REQ_UPDATE_PROTECTED_GUEST_STATE. > There is a race condition I observed when booting with SVSM where: > 1. BSP sets target vcpu to KVM_MP_STATE_RUNNABLE > 2. AP thread within the loop of arch/x86/kvm.c:vcpu_run() checks > vm_vcpu_running() > 3. AP enters the guest without having updated the VMSA state from > KVM_REQ_UPDATE_PROTECTED_GUEST_STATE > > This results in the AP executing on a bad RIP and then crashing. > If we set the request first, then we avoid the race condition. That just introducs a different race, e.g. if this task gets delayed and the target vCPU processes KVM_REQ_UPDATE_PROTECTED_GUEST_STATE before its marked RUNNABLE, then the target vCPU could end up stuck in the UNINITIALIZED loop. Reading and writing arch.mp_state across vCPUs is simply not safe. There's a reason why KVM atomically manages INITs and SIPIs and only modifies mp_state when processing events on the target vCPU. > > + kvm_vcpu_kick(target_vcpu); ... > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > index 87b78d63e81d..df9ec357d538 100644 > > --- a/arch/x86/kvm/x86.c > > +++ b/arch/x86/kvm/x86.c > > @@ -10858,6 +10858,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) > > > > if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu)) > > static_call(kvm_x86_update_cpu_dirty_logging)(vcpu); > > + > > + if (kvm_check_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) { > > + kvm_vcpu_reset(vcpu, true); > > + if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) { > > + r = 1; > > + goto out; > > + } > > + } > > } > > > > if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win || > > @@ -13072,6 +13080,9 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) > > if (kvm_test_request(KVM_REQ_PMI, vcpu)) > > return true; > > > > + if (kvm_test_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) > > + return true; > > + > > if (kvm_arch_interrupt_allowed(vcpu) && > > (kvm_cpu_has_interrupt(vcpu) || > > kvm_guest_apic_has_interrupt(vcpu))) > > -- > > 2.25.1 > > > >