Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp3552319rwd; Mon, 22 May 2023 16:05:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4xNC/Ez1WREKMYxrNZQBf5KEU2Rv67R0ep3qbyejVpss67if/6pfq+Jqwr+GAyHjyuZhXL X-Received: by 2002:a17:903:483:b0:1ad:eeea:32d7 with SMTP id jj3-20020a170903048300b001adeeea32d7mr12659075plb.48.1684796715587; Mon, 22 May 2023 16:05:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684796715; cv=none; d=google.com; s=arc-20160816; b=z+XVCet29wHcgP83i8I+0Jlmp/2vFi9f49sesQ+auhl30kV7OlPo5D7N8dv3uT/dob 6N/ckhPb3DPcDM4I2cQAkpdDk5lt0/kzPCvGku7LlzM5v8HVcsBxWrps4ViCpVMqslr/ FQ+8xBoAJzOWFvo7g3A2NNP7fKbCQeUdvMpj/sfFH/npWcXAUOQNQz/s2wumvdZWRnsl G47G4557GGrDqjK0UTHKKP7VyT1sP2eFeNfReitVwtaVFDTjAlLZWfqRoXgpxtmP1XsZ IfnHVU0sxUJGxqgfrkfnWqjBEbJzZGCgL6m1bZ7LlgF1kvKhrwt6LFkrcJMLvwrLxSiE 8Vtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=bc5jJ+m1tcxtw8j616gCjMtw6tcRDR1frZV9vlfkfio=; b=qnbBWRyafOtfMi95Uii0Qq4d9FToUw3G3D7ZZuV/dlZ/xMUfyPFAwucvxsCzl6m46e kSjVg+/KZiNXZ/5sJQItUNP7WBpD337pgEs/I2VRmafrXtAj8O69NG6VfNfQdkD+odrY zp/5oxtZV76zl/w0LugrrbaxrlLvnc5ipUKeYG/S9gW1N17rhSXCvFLDkDRUPvqkc1Cg 4+UC0wKnKcl92DrQAA13wMWu7Ri5QDwkgzX0BNT7Vh+j7JAQywnVPntpYD3hLtZU/KkB SQk0uwPCHgq2n2D8JRNqDtzkyw3VfjO1S0ONhK6lsQgfYyxiN9dKYhYwxSQwR8l/XWsV Ig7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=zWTqbmrQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j9-20020a170903024900b001a986e5bd92si958859plh.83.2023.05.22.16.05.02; Mon, 22 May 2023 16:05:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=zWTqbmrQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234684AbjEVWvb (ORCPT + 99 others); Mon, 22 May 2023 18:51:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234924AbjEVWvO (ORCPT ); Mon, 22 May 2023 18:51:14 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82DDE10EC for ; Mon, 22 May 2023 15:50:51 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-534107e73cfso3565051a12.1 for ; Mon, 22 May 2023 15:50:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684795851; x=1687387851; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bc5jJ+m1tcxtw8j616gCjMtw6tcRDR1frZV9vlfkfio=; b=zWTqbmrQVIwk4Hve+ZzgfU5EKSEgtGvUP65d9xqBIvTMso2XJ7FsHytFLhw0uKduv4 HY5EujbVEii/zaSAuEe2chCkZCL2vTuFED9rB75iha0g65+ygf0Q7NdPTOYBnC7heuvn 9LaEaNeugxMMVVDJiJZxlVoSVncfgwThodi3SUyWKKxg7AeACCiHjv3hkI4kzgMhhVvA YPJBJaNKnt0Oww1V5VPrZ0NMhm7WkMSKv+2Y5CzNK3Vj6nfM9B+k77leL8LkzIatLjX8 AhyOmKNceg4VzbkJ5taqKetpTE6HYu6GcpmEnNMPgMx4nIM5TeAB5nLHjxfg69ZtmvFT MnaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684795851; x=1687387851; 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=bc5jJ+m1tcxtw8j616gCjMtw6tcRDR1frZV9vlfkfio=; b=WFqtP/7zyspTujrNAP38HYIrkbQSadrIeHP2/kl4+LugTZ3MwDrcmJrqZCqEaWLIu4 e6cAyNaMDeCZZtGnsNeWXWE7IcaFv3lMfR2n/OjD57342SZbkX2+VBNw6sPbPmJHn4aV mCkm+eU2YQQ1jMrPhrnE+SNyiKYnrU49rFpBBOtWI/pX1EZNw22beGB2Ju0V/AhWmqK5 8Ya/rRiH3vEhvGFG7lcqG8osGs4buyQKj/rSzPSg5URL5DRB9QBX+dtibu9njB87LIr9 MOZLvwREQssnTODhRRHYuh1919fPxlcvCnYZXRlPWLFZqCrlpOSnFtrhwlS6lfyFGg9y KiAg== X-Gm-Message-State: AC+VfDxDTsZmtsWj/Y1VojUTlN/GFELmx3QhIlrwOIOZ5mZ0KwGnrHkq owzSX/i5XCF+jIkSUoNMpjW8oEXgdNM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:5310:0:b0:530:866e:c3c0 with SMTP id h16-20020a635310000000b00530866ec3c0mr2799819pgb.10.1684795850767; Mon, 22 May 2023 15:50:50 -0700 (PDT) Date: Mon, 22 May 2023 15:50:49 -0700 In-Reply-To: <20230411125718.2297768-4-aik@amd.com> Mime-Version: 1.0 References: <20230411125718.2297768-1-aik@amd.com> <20230411125718.2297768-4-aik@amd.com> Message-ID: Subject: Re: [PATCH kernel v5 3/6] KVM: SEV-ES: explicitly disable debug From: Sean Christopherson To: Alexey Kardashevskiy Cc: kvm@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, Tom Lendacky , Pankaj Gupta , Nikunj A Dadhania , Santosh Shukla , Carlos Bilbao Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 11, 2023, Alexey Kardashevskiy wrote: > SVM/SEV enable debug registers intercepts to skip swapping DRs > on entering/exiting the guest. When the guest is in control of > debug registers (vcpu->guest_debug == 0), there is an optimisation to > reduce the number of context switches: intercepts are cleared and > the KVM_DEBUGREG_WONT_EXIT flag is set to tell KVM to do swapping > on guest enter/exit. > > The same code also executes for SEV-ES, however it has no effect as > - it always takes (vcpu->guest_debug == 0) branch; > - KVM_DEBUGREG_WONT_EXIT is set but DR7 intercept is not cleared; > - vcpu_enter_guest() writes DRs but VMRUN for SEV-ES swaps them > with the values from _encrypted_ VMSA. > > Be explicit about SEV-ES not supporting debug: > - return right away from dr_interception() and skip unnecessary processing; > - clear vcpu->guest_debug at SEV-ES' LAUNCH_UPDATE_VMSA if debugging > was already enabled; after that point the generic x86's > KVM_SET_GUEST_DEBUG ioctl disallows enabling debug. > > Add WARN_ON to kvm_x86::sync_dirty_debug_regs() (saves guest DRs on > guest exit) to signify that SEV-ES won't hit that path. > > Suggested-by: Sean Christopherson > Signed-off-by: Alexey Kardashevskiy > Reviewed-by: Tom Lendacky > --- > Changes: > v5: > * new in the series > --- > arch/x86/kvm/svm/sev.c | 6 ++++++ > arch/x86/kvm/svm/svm.c | 10 +++++++++- > 2 files changed, 15 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c > index 0f4761a57d86..b4365622222b 100644 > --- a/arch/x86/kvm/svm/sev.c > +++ b/arch/x86/kvm/svm/sev.c > @@ -639,6 +639,12 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu, > return ret; > > vcpu->arch.guest_state_protected = true; > + > + if (vcpu->guest_debug) > + pr_warn_ratelimited("guest_debug (%lx) not supported for SEV-ES", Note, this needs a newline in the printk, otherwise it'll get buffered until the next non-cont printk comes along (guess how many times I've been burned by this). > + vcpu->guest_debug); > + vcpu->guest_debug = 0; Argh, KVM's APIs can be quite frustrating. IIUC, guest_debug can never actually be consumed because, per Tom[*], "A guest can't run before the LAUNCH_UPDATE process is complete". But because the fact that the VM is an SEV-ES is communicated to KVM after KVM_CREATE_VM, userspace can do KVM_SET_GUEST_DEBUG before KVM_SEV_ES_INIT and before KVM_SEV_LAUNCH_UPDATE_VMSA, and thus get KVM into a state where guest_debug is non-zero for an SEV-ES guest. Blech. Instead of a ratelimited warn, can KVM get away with simply rejecting KVM_SEV_LAUNCH_UPDATE_VMSA if guest_debug is non-zero? That combo can't work, so it's seems unlikely userspace is relying on being able to do KVM_SET_GUEST_DEBUG. If we do "have" to keep this approach, I'm generally opposed to any kind of printk in KVM, but this one does seem to be justified since the most likely scenario is that there's a human interactively debugging the guest (or at least, trying to debug the guest). But I would say explicitly call out the ioctl(), "guest_debug" probably won't mean anything to a random user. And I vote to not print the value, that implies that the specific value is unsupported, not that debug in general is disallowed. Something like this (if we have to)? pr_warn_ratelimited("Suppressing KVM_SET_GUEST_DEBUG for SEV-ES guest\n" [*] https://lore.kernel.org/all/7edcf2c3-005f-04bd-7ec6-80baee236f40@amd.com > + > return 0; > } > > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c > index dc12de325cca..179952a31d3b 100644 > --- a/arch/x86/kvm/svm/svm.c > +++ b/arch/x86/kvm/svm/svm.c > @@ -1980,7 +1980,7 @@ static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) > { > struct vcpu_svm *svm = to_svm(vcpu); > > - if (vcpu->arch.guest_state_protected) > + if (WARN_ON_ONCE(sev_es_guest(vcpu->kvm))) > return; > > get_debugreg(vcpu->arch.db[0], 0); > @@ -2698,6 +2698,14 @@ static int dr_interception(struct kvm_vcpu *vcpu) > unsigned long val; > int err = 0; > > + /* > + * SEV-ES intercepts DR7 only to disable guest debugging > + * and the guest issues a VMGEXIT for DR7 write only. KVM cannot Wrapping is a bit aggressive (wrap at 80, not earlier). > + * change DR7 (always swapped as type 'A') so return early. > + */ > + if (sev_es_guest(vcpu->kvm)) > + return 1; > + > if (vcpu->guest_debug == 0) { > /* > * No more DR vmexits; force a reload of the debug registers > -- > 2.39.1 >