Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp368807pxf; Thu, 8 Apr 2021 05:02:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYhX6GSGeM8Y6i+eJPTR72ldgmvdxtH1LqUiGS2r+eUMeL7a8D/kvaZuML4QaeWCpYIo5D X-Received: by 2002:aa7:c74c:: with SMTP id c12mr10752958eds.346.1617883363155; Thu, 08 Apr 2021 05:02:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617883363; cv=none; d=google.com; s=arc-20160816; b=MvYs6r3FMF0haHa5tpip3683AU/2vB8O4mLYC4Vbk/tCW+u8+mGtiu1wYPsizgfShH z6c7H2P8wrJR3Iif9tp4IuMi3pe64Bfv3hWOp6kXlVyRAFGFdj+CAtcjpHIA1tVYpiXi A8WUnHj6llxkI9DIFM4UG+0nGu3Q6sd7WH6Q5LgZ6tsNPObYr0nnycNrA7wN8D7uX49p o4HUSoakt03KhigT0DEBnQmt+4vEfPDYIBz8Tn+dBowPiki9ScCcMV53pq38G1DqEOxp ti8mYrscbwIfE9kDcQEfQAexSFT9BBMe2jrmEi3pJhUA/HFFp6Z35MuYAT5qPDTYxuJb FzXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=vNug/8lWcscFDRAAJ94dUdldY7ZTZuugHf7cy9Hmh9U=; b=vWuwq4xHGX1YBUnPshg5CNu7mRDjpvq7gldM0aYhMj1NG0MLzcatQjHaRHV0iHp27W dhJProo5amzoKygpPTbKwMl915mljzMYTCZ6jXdtxlPbQ1tNnqsdunrbiwU+mhdtSj5S cpjs9fSnE5x3CMkPkLY0csuDFyr3gC0yX+ygzQ4FdgJ1jXG740/D/h2NkS7YJowKy2ay QxrRb8XBwiVhOsk+zouL1yaj2PbZ4gxoByzDHvGiJDD3IMnS+IY6V9lZ1c2OwVGGfGcq pyOQ+WbJxvg5bNCL0t8vUqJpFgxnjA/agJsHM2N/A5C3PWH6rOv6qO1sm797klfsZHxi VoBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="H/qFze13"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p26si12729739edq.240.2021.04.08.05.02.18; Thu, 08 Apr 2021 05:02:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="H/qFze13"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231336AbhDHMBb (ORCPT + 99 others); Thu, 8 Apr 2021 08:01:31 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:59075 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229964AbhDHMB2 (ORCPT ); Thu, 8 Apr 2021 08:01:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617883277; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=vNug/8lWcscFDRAAJ94dUdldY7ZTZuugHf7cy9Hmh9U=; b=H/qFze13tfRK6BS22oD1bHTNZa2Zu8d42LsEu9uQQOZZcLWQrKAzWgr53TbMDFA9GodEL6 0WQpdVVv05jZUhSFg43xOicAMYGwOc8lmtrqI2duy2UKVdkRTHgb4Glx0JHwyMmj7JcXAo 1dsFpiGL4mG7UHbSLliShqEEF6CD3Lw= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-418-zhhDH4vsNM60RBzRZS7CJg-1; Thu, 08 Apr 2021 08:01:15 -0400 X-MC-Unique: zhhDH4vsNM60RBzRZS7CJg-1 Received: by mail-ej1-f72.google.com with SMTP id o11so735041ejx.23 for ; Thu, 08 Apr 2021 05:01:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=vNug/8lWcscFDRAAJ94dUdldY7ZTZuugHf7cy9Hmh9U=; b=a0jxdE/gbomVxwKl+ACBRnovwncgFrn4Kps1Kby+IHPr9T7HNUoHNopW5c/GpvPetT bCSvv9OAoe1S9xONIfmY6u43qXuZXRVQqP1sB11Q3IrKsqvoF7bqlNV98f4DbIqtAJ8V ehy1Adsg6nmuDmXMW9HNwGwgvGPp66V89K8gYNFHb4SFfvkySEi+5OgC1DSIlaU3NXWy iFsH0yfoaP2+DrQcls+galPU6twPPxrLmuPXpQ/jI2gqMAzGgEFeblHlkCaF9jcK3/yI VU1pkKqXHkK/9AF3tfP1TXabKuPEY97zHi14Ubp/oodvj2+R0ej0UxFDFgtVC2kQT+df JNgg== X-Gm-Message-State: AOAM5315ZrrvJ/dTLp3DiYOsZbExQ5E4CKum0nOt2BGWrcHMsdvvlj1V lO71Jb52Oy7hn8spyzKAjalFh/c52vBYpxE6IaKJv/bSPIw6FSeRzXR9bZ9KGOqYQmthlTQN7kB gxOl5sVczo4EnajOmqKpnh+er X-Received: by 2002:a17:906:4801:: with SMTP id w1mr9843107ejq.475.1617883273716; Thu, 08 Apr 2021 05:01:13 -0700 (PDT) X-Received: by 2002:a17:906:4801:: with SMTP id w1mr9843088ejq.475.1617883273512; Thu, 08 Apr 2021 05:01:13 -0700 (PDT) Received: from vitty.brq.redhat.com (g-server-2.ign.cz. [91.219.240.2]) by smtp.gmail.com with ESMTPSA id t12sm8963436ejb.76.2021.04.08.05.01.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Apr 2021 05:01:12 -0700 (PDT) From: Vitaly Kuznetsov To: Siddharth Chandrasekaran Cc: Alexander Graf , Evgeny Iakovlev , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: Re: [PATCH 3/4] KVM: x86: kvm_hv_flush_tlb use inputs from XMM registers In-Reply-To: <20210407211954.32755-4-sidcha@amazon.de> References: <20210407211954.32755-1-sidcha@amazon.de> <20210407211954.32755-4-sidcha@amazon.de> Date: Thu, 08 Apr 2021 14:01:11 +0200 Message-ID: <87eefl7zp4.fsf@vitty.brq.redhat.com> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Siddharth Chandrasekaran writes: > Hyper-V supports the use of XMM registers to perform fast hypercalls. > This allows guests to take advantage of the improved performance of the > fast hypercall interface even though a hypercall may require more than > (the current maximum of) two input registers. > > The XMM fast hypercall interface uses six additional XMM registers (XMM0 > to XMM5) to allow the guest to pass an input parameter block of up to > 112 bytes. Hyper-V can also return data back to the guest in the > remaining XMM registers that are not used by the current hypercall. > > Add framework to read/write to XMM registers in kvm_hv_hypercall() and > use the additional hypercall inputs from XMM registers in > kvm_hv_flush_tlb() when possible. > > Cc: Alexander Graf > Co-developed-by: Evgeny Iakovlev > Signed-off-by: Evgeny Iakovlev > Signed-off-by: Siddharth Chandrasekaran > --- > arch/x86/kvm/hyperv.c | 109 ++++++++++++++++++++++++++++++++++-------- > 1 file changed, 90 insertions(+), 19 deletions(-) > > diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c > index 8f6babd1ea0d..bf2f86f263f1 100644 > --- a/arch/x86/kvm/hyperv.c > +++ b/arch/x86/kvm/hyperv.c > @@ -36,6 +36,7 @@ > > #include "trace.h" > #include "irq.h" > +#include "fpu.h" > > /* "Hv#1" signature */ > #define HYPERV_CPUID_SIGNATURE_EAX 0x31237648 > @@ -1623,6 +1624,8 @@ static __always_inline unsigned long *sparse_set_to_vcpu_mask( > return vcpu_bitmap; > } > > +#define KVM_HV_HYPERCALL_MAX_XMM_REGISTERS 6 > + > struct kvm_hv_hcall { > u64 param; > u64 ingpa; > @@ -1632,10 +1635,14 @@ struct kvm_hv_hcall { > u16 rep_idx; > bool fast; > bool rep; > + sse128_t xmm[KVM_HV_HYPERCALL_MAX_XMM_REGISTERS]; > + bool xmm_dirty; > }; > > static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, bool ex) > { > + int i, j; > + gpa_t gpa; > struct kvm *kvm = vcpu->kvm; > struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); > struct hv_tlb_flush_ex flush_ex; > @@ -1649,8 +1656,15 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, bool > bool all_cpus; > > if (!ex) { > - if (unlikely(kvm_read_guest(kvm, hc->ingpa, &flush, sizeof(flush)))) > - return HV_STATUS_INVALID_HYPERCALL_INPUT; > + if (hc->fast) { > + flush.address_space = hc->ingpa; > + flush.flags = hc->outgpa; > + flush.processor_mask = sse128_lo(hc->xmm[0]); > + } else { > + if (unlikely(kvm_read_guest(kvm, hc->ingpa, > + &flush, sizeof(flush)))) > + return HV_STATUS_INVALID_HYPERCALL_INPUT; > + } > > trace_kvm_hv_flush_tlb(flush.processor_mask, > flush.address_space, flush.flags); > @@ -1668,9 +1682,16 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, bool > all_cpus = (flush.flags & HV_FLUSH_ALL_PROCESSORS) || > flush.processor_mask == 0; > } else { > - if (unlikely(kvm_read_guest(kvm, hc->ingpa, &flush_ex, > - sizeof(flush_ex)))) > - return HV_STATUS_INVALID_HYPERCALL_INPUT; > + if (hc->fast) { > + flush_ex.address_space = hc->ingpa; > + flush_ex.flags = hc->outgpa; > + memcpy(&flush_ex.hv_vp_set, > + &hc->xmm[0], sizeof(hc->xmm[0])); > + } else { > + if (unlikely(kvm_read_guest(kvm, hc->ingpa, &flush_ex, > + sizeof(flush_ex)))) > + return HV_STATUS_INVALID_HYPERCALL_INPUT; > + } > > trace_kvm_hv_flush_tlb_ex(flush_ex.hv_vp_set.valid_bank_mask, > flush_ex.hv_vp_set.format, > @@ -1681,20 +1702,29 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, bool > all_cpus = flush_ex.hv_vp_set.format != > HV_GENERIC_SET_SPARSE_4K; > > - sparse_banks_len = > - bitmap_weight((unsigned long *)&valid_bank_mask, 64) * > - sizeof(sparse_banks[0]); > + sparse_banks_len = bitmap_weight((unsigned long *)&valid_bank_mask, 64); > > if (!sparse_banks_len && !all_cpus) > goto ret_success; > > - if (!all_cpus && > - kvm_read_guest(kvm, > - hc->ingpa + offsetof(struct hv_tlb_flush_ex, > - hv_vp_set.bank_contents), > - sparse_banks, > - sparse_banks_len)) > - return HV_STATUS_INVALID_HYPERCALL_INPUT; > + if (!all_cpus) { > + if (hc->fast) { > + if (sparse_banks_len > KVM_HV_HYPERCALL_MAX_XMM_REGISTERS - 1) > + return HV_STATUS_INVALID_HYPERCALL_INPUT; > + for (i = 0, j = 1; i < sparse_banks_len; i += 2, j++) { > + sparse_banks[i + 0] = sse128_lo(hc->xmm[j]); > + sparse_banks[i + 1] = sse128_hi(hc->xmm[j]); > + } > + } else { > + gpa = hc->ingpa; > + gpa += offsetof(struct hv_tlb_flush_ex, > + hv_vp_set.bank_contents); > + if (unlikely(kvm_read_guest(kvm, gpa, sparse_banks, > + sparse_banks_len * > + sizeof(sparse_banks[0])))) > + return HV_STATUS_INVALID_HYPERCALL_INPUT; > + } > + } > } > > cpumask_clear(&hv_vcpu->tlb_flush); > @@ -1890,6 +1920,41 @@ static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *h > return HV_STATUS_SUCCESS; > } > > +static bool is_xmm_fast_hypercall(struct kvm_hv_hcall *hc) > +{ > + switch (hc->code) { > + case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST: > + case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE: > + case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX: > + case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX: > + return true; > + } > + > + return false; > +} > + > +static inline void kvm_hv_hypercall_read_xmm(struct kvm_hv_hcall *hc) > +{ > + int reg; > + > + kvm_fpu_get(); > + for (reg = 0; reg < KVM_HV_HYPERCALL_MAX_XMM_REGISTERS; reg++) > + _kvm_read_sse_reg(reg, &hc->xmm[reg]); > + kvm_fpu_put(); > + hc->xmm_dirty = false; > +} > + > +static inline void kvm_hv_hypercall_write_xmm(struct kvm_hv_hcall *hc) > +{ > + int reg; > + > + kvm_fpu_get(); > + for (reg = 0; reg < KVM_HV_HYPERCALL_MAX_XMM_REGISTERS; reg++) > + _kvm_write_sse_reg(reg, &hc->xmm[reg]); > + kvm_fpu_put(); > + hc->xmm_dirty = false; > +} > + > int kvm_hv_hypercall(struct kvm_vcpu *vcpu) > { > struct kvm_hv_hcall hc; > @@ -1926,6 +1991,9 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) > hc.rep_idx = (hc.param >> HV_HYPERCALL_REP_START_OFFSET) & 0xfff; > hc.rep = !!(hc.rep_cnt || hc.rep_idx); > > + if (is_xmm_fast_hypercall(&hc)) > + kvm_hv_hypercall_read_xmm(&hc); is_xmm_fast_hypercall() check should probably be complemented with " && hc.fast" as there's no point in reading this regs when the hypercall is not 'fast'. Also, we can probably defer kvm_hv_hypercall_read_xmm() until we know how many regs we actually need to not read them all (we will always need xmm[0] I guess so we can as well read it here). > + > trace_kvm_hv_hypercall(hc.code, hc.fast, hc.rep_cnt, hc.rep_idx, > hc.ingpa, hc.outgpa); > > @@ -1961,28 +2029,28 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) > kvm_hv_hypercall_complete_userspace; > return 0; > case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST: > - if (unlikely(hc.fast || !hc.rep_cnt || hc.rep_idx)) { > + if (unlikely(!hc.rep_cnt || hc.rep_idx)) { > ret = HV_STATUS_INVALID_HYPERCALL_INPUT; > break; > } > ret = kvm_hv_flush_tlb(vcpu, &hc, false); > break; > case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE: > - if (unlikely(hc.fast || hc.rep)) { > + if (unlikely(hc.rep)) { > ret = HV_STATUS_INVALID_HYPERCALL_INPUT; > break; > } > ret = kvm_hv_flush_tlb(vcpu, &hc, false); > break; > case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX: > - if (unlikely(hc.fast || !hc.rep_cnt || hc.rep_idx)) { > + if (unlikely(!hc.rep_cnt || hc.rep_idx)) { > ret = HV_STATUS_INVALID_HYPERCALL_INPUT; > break; > } > ret = kvm_hv_flush_tlb(vcpu, &hc, true); > break; > case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX: > - if (unlikely(hc.fast || hc.rep)) { > + if (unlikely(hc.rep)) { > ret = HV_STATUS_INVALID_HYPERCALL_INPUT; > break; > } > @@ -2035,6 +2103,9 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) > break; > } > > + if (hc.xmm_dirty) > + kvm_hv_hypercall_write_xmm(&hc); > + > return kvm_hv_hypercall_complete(vcpu, ret); > } -- Vitaly