Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1744677rwi; Wed, 19 Oct 2022 15:00:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM55Lq/X+8DrvFnl5tyWBAnsyAGFvW4TQYKvM++sRIcbt8ww5/D6CuGGLJruZCWbcUfb0qE/ X-Received: by 2002:a17:903:230b:b0:186:6041:51bf with SMTP id d11-20020a170903230b00b00186604151bfmr886110plh.24.1666216853761; Wed, 19 Oct 2022 15:00:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666216853; cv=none; d=google.com; s=arc-20160816; b=wVyjFcfnFbAYPGNDQ3rC0xT0E/QG9GIXOLBuul5xhJjGqqYFkxN1vHcm7ai/iNonyZ fv2By5JUuwRZBMmJo0/qdReduV+dbPmwKQKzHvbdftgTKFV/mwq2K2Fegk0tDdFOhPRR p/mwF5ah4XSfVsRCxCQEqjV8a1OWjSU3v82U3KL4SRWeVbiMQGZyVLuvWWZeF+9gvEWW JOm/e+rC38brcZ+JKBnqWY7fR62Pw7+AaBvQ/SBGRSA92kxGoOIg69jQQtj1pUjvFEDO rpX1TNPcmBxieXm+PYkEJzVPOIJbUPsoXMDzLEixNRfMA5Nzm3kJ9EaFTK6kv3qHYR5C HYZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=LmQWrSQW0LKNFVqDHiPaFtM2LkOlxWeMwLtjmwHy94w=; b=yy/rSeDhkkB2FuX2Lj5tZHKZFSpjnkkB+H1cPzZ/88o1RE+atyG4vlbecccL4nnQGT O97kPdW0WNyvYK7qNaMTjQrkhWerHofRGDxK5TvIMqHQIGmR+oUhaJo76DUEVc4tRweN CbeMDtmfJANhxThqPy1JltkoQHqr1wBKpcmccOkWhOOolsadDQ1KRl6sSyPAYPT+Rwmr Ab7kltHxFqSLfW7Jn/JzeqOz/4Yi9HILJk8h/L1Wj12NIvMEPRo0ZCo2uRIWMkupqz50 ZE5DCbtQxpQRmAUhGbFBNmlmdhik9PxFFq0Cl375ZIiFC2khX05yt9xro9jwV1wRKaGZ e/Fw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=IuypjP5j; 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 n18-20020a170902e55200b0016d9c8479b5si22308532plf.344.2022.10.19.15.00.41; Wed, 19 Oct 2022 15:00:53 -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=20210112 header.b=IuypjP5j; 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 S231187AbiJSVxc (ORCPT + 99 others); Wed, 19 Oct 2022 17:53:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229964AbiJSVxa (ORCPT ); Wed, 19 Oct 2022 17:53:30 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B957E193EDA for ; Wed, 19 Oct 2022 14:53:26 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id q10-20020a17090a304a00b0020b1d5f6975so1254782pjl.0 for ; Wed, 19 Oct 2022 14:53:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=LmQWrSQW0LKNFVqDHiPaFtM2LkOlxWeMwLtjmwHy94w=; b=IuypjP5jcyXG/slqTu8xmJdF3cD3A/OCZGFN3AAV8/MRYRuRNzz6CNtouHsa/s8EyR oQq0Co6YvQmYtHIX5BzccsSjkcYLkQFPveA1WpPwU+qxIRvj4EhBo0wzRY8G/15ySc8J w4ddB/qOMIr7MIAgr+zEtFfjDAnDJNppacPH4s9T2+I84xVjDwX/Z1lLK64+hfSp80fS hmlqOUF/fryzRn+43DiI9dXHx8N5Ce+uB7PFei2koH8vpzvBa6Nv/u8V5ro5Xk8WeIVA 1B+FdtgGnDFNdrDCqtPVLkN1xkBAHP+yH9LeP0RA6RUIupBXxrTz0yXlANaaVorw9VSc Q3XA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=LmQWrSQW0LKNFVqDHiPaFtM2LkOlxWeMwLtjmwHy94w=; b=DMdIpg5pYbE3ckJ848KopAnRWpYGKbiN8l0s6Ial8TAMI3l+fZvOQk3Xgj0Fi+Q7JR JB8FExLI1WONwVpqadbD/aedzHAvUEofx50GgV7bEsQ8MksthzhMPbx5QVvul+TkvG9r OPDW3S1PMLh/uwjKWBciiJBDBUPt991nHCVyRy3XpoIz+dMMrRpk4S+pf/7j28PxLQZL gH9jA0hVPYTpgb4CaaJUSoBGIfzadv4AlPS45EoJQKB8sknp7jjvzTTFEQ27K8OIZaKN +hhlWkq9akS+L2833Ozsf9Yn2ymTbqdrD2SQwq8qT1DOMH9IDpTi8QJZggMk8NOaaYlm 9Jwg== X-Gm-Message-State: ACrzQf14HByG88LEnoDKFwky+kQdB9tkAN+qjDsASna5D39nuzR0jiS2 YAYe2ZFi6yAPqjwWNCMaiBPuAw== X-Received: by 2002:a17:90a:f190:b0:20a:a8b1:b199 with SMTP id bv16-20020a17090af19000b0020aa8b1b199mr48297286pjb.83.1666216404723; Wed, 19 Oct 2022 14:53:24 -0700 (PDT) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id n30-20020aa7985e000000b00562a36c0b32sm11832847pfq.119.2022.10.19.14.53.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 14:53:24 -0700 (PDT) Date: Wed, 19 Oct 2022 21:53:21 +0000 From: Sean Christopherson To: Vitaly Kuznetsov Cc: kvm@vger.kernel.org, Paolo Bonzini , Wanpeng Li , Jim Mattson , Michael Kelley , Siddharth Chandrasekaran , Yuan Yao , Maxim Levitsky , linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v11 23/46] KVM: x86: hyper-v: L2 TLB flush Message-ID: References: <20221004123956.188909-1-vkuznets@redhat.com> <20221004123956.188909-24-vkuznets@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221004123956.188909-24-vkuznets@redhat.com> X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_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, Oct 04, 2022, Vitaly Kuznetsov wrote: > @@ -2225,10 +2264,27 @@ static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result) > > static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result) > { > + int ret; > + > trace_kvm_hv_hypercall_done(result); > kvm_hv_hypercall_set_result(vcpu, result); > ++vcpu->stat.hypercalls; > - return kvm_skip_emulated_instruction(vcpu); > + ret = kvm_skip_emulated_instruction(vcpu); > + > + if (unlikely(hv_result_success(result) && is_guest_mode(vcpu) > + && kvm_hv_is_tlb_flush_hcall(vcpu))) { "&&" goes on the previous line. > + struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); > + u32 tlb_lock_count; > + > + if (unlikely(kvm_read_guest(vcpu->kvm, hv_vcpu->nested.pa_page_gpa, Nit, I'd say leave off the "unlikely", the hints almost never provide meaningful performance benefits, e.g. code generation is identical with and without the unlikely, and IMO the extra line length and parantheses depth makes the code harder to read. > + &tlb_lock_count, sizeof(tlb_lock_count)))) > + kvm_inject_gp(vcpu, 0); This will inject a #GP on the _next_ instruction. That seems wrong. And why #GP in the first place? E.g. if userspace yanks out the memslot, injecting #GP into the guest is less-than-ideal behavior. What about reading tlb_lock_count before skipping the hypercall, e.g. static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result) { u32 tlb_lock_count = 0; int ret; if (hv_result_success(result) && is_guest_mode(vcpu) && kvm_hv_is_tlb_flush_hcall(vcpu) && kvm_read_guest(vcpu->kvm, to_hv_vcpu(vcpu)->nested.pa_page_gpa, &tlb_lock_count, sizeof(tlb_lock_count))) result = HV_STATUS_INVALID_HYPERCALL_INPUT; trace_kvm_hv_hypercall_done(result); kvm_hv_hypercall_set_result(vcpu, result); ++vcpu->stat.hypercalls; ret = kvm_skip_emulated_instruction(vcpu); if (tlb_lock_count) kvm_x86_ops.nested_ops->hv_inject_synthetic_vmexit_post_tlb_flush(vcpu); return ret; } > + > + if (tlb_lock_count) tlb_lock_count will be uninitialized if kvm_read_guest() fails. > + kvm_x86_ops.nested_ops->hv_inject_synthetic_vmexit_post_tlb_flush(vcpu); Ugh, kvm_skip_emulated_instruction() is flawed. If skipping the emulated instruction fails, i.e. if EMULTYPE_SKIP emulation fails, then synthesizing a VM-Exit is technically wrong. But kvm_skip_emulated_instruction() also returns '0' for a KVM_EXIT_DEBUG, which happens after skipping the instruction. Not worth handling here, e.g. nested_svm_vmrun() has the same "bug". Failure is effectively limited to old AMD CPUs, and userspace is likely goiing to kill the VM anyways.