Received: by 10.213.65.68 with SMTP id h4csp2532929imn; Mon, 2 Apr 2018 09:13:47 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/+rIS/Y1SdC6pMkgPe+zO7ov4zCUa+++uEPGPUSI90ORm4xicBs972dTnTJXEhit/yGJ0M X-Received: by 10.98.155.12 with SMTP id r12mr7799655pfd.15.1522685627225; Mon, 02 Apr 2018 09:13:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522685627; cv=none; d=google.com; s=arc-20160816; b=CQvqUG9qVKxTmqOBP8zi/GQAJtiY3l+70Q3r5p5gOifpBwv2MtFVua8fPxnDz3qFcA Kn5gIGMEhCJEb0oo85KyLHVGEZubgyNo34XCtXUF6aiLQTZcPMxrINduWeiq4W4wnvpo nHMq0G0uuioxGYB1xIGhO94set/xgxpCs/ZXo8iIut6DqzCr8LDsF4MPA/m5g50ft6a/ /aTgYVjuEQgtWBK9w6hRuCfvF1BaFV/lsxyFJTclBJQELTlNJXlvVryzzuIjoZRjBOw6 czDkJ/9EkRgWIFi92z6qzuii+sLVGRsouJSZOBnHqjOvgr1npdr0Wr7a9SdSgnY5FR+G VaHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=rKoRpTOoHlNBm/3D27P3DYOr0PHmhV96mwDb7KVItK0=; b=Tgc+GijI8P7iAGuHPdUd5etMolyVAseNBz6a5zRAP4bikSL8/+2xx/20ou4pve5K5d EA1ZbUEhfYV/6E5Fl/URml527e4v9fc31q8mzsUL7FPGv9Kqe3pu5gFVISN/QnRuf4QO vR3Kpde80EuB4HB1CPwxF8oqsjvgLU95B9vS/qFF6u3FEJEjs83v7FHON7Dsn0E001Gt NRanb1CALC8cimyj2Mh/0akHDm/d4dhA5m/Odycb9bSo1xtpL1TOfrdUduhsdO6GluPP FJbjqSIOV8wY9gBUdF/PsL5zrfskgtnLrUXe/2WLouAncujDdsnAKCzgA7Rgqx/nDlAt 344g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z8-v6si521821plh.126.2018.04.02.09.13.33; Mon, 02 Apr 2018 09:13:47 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753016AbeDBQLp (ORCPT + 99 others); Mon, 2 Apr 2018 12:11:45 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:39982 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752860AbeDBQLL (ORCPT ); Mon, 2 Apr 2018 12:11:11 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2B93F84250; Mon, 2 Apr 2018 16:11:10 +0000 (UTC) Received: from vitty.brq.redhat.com (ovpn-204-121.brq.redhat.com [10.40.204.121]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1AC4CD7E12; Mon, 2 Apr 2018 16:11:07 +0000 (UTC) From: Vitaly Kuznetsov To: kvm@vger.kernel.org Cc: x86@kernel.org, Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Roman Kagan , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , "Michael Kelley (EOSG)" , Mohammed Gamal , Cathy Avery , linux-kernel@vger.kernel.org Subject: [PATCH 4/5] KVM: x86: hyperv: simplistic HVCALL_FLUSH_VIRTUAL_ADDRESS_{LIST,SPACE}_EX implementation Date: Mon, 2 Apr 2018 18:10:58 +0200 Message-Id: <20180402161059.8488-5-vkuznets@redhat.com> In-Reply-To: <20180402161059.8488-1-vkuznets@redhat.com> References: <20180402161059.8488-1-vkuznets@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Mon, 02 Apr 2018 16:11:10 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Mon, 02 Apr 2018 16:11:10 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'vkuznets@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Implement HvFlushVirtualAddress{List,Space}Ex hypercalls in a simplistic way: do full TLB flush with KVM_REQ_TLB_FLUSH and rely on kvm_vcpu_kick() kicking only vCPUs which are currently IN_GUEST_MODE. Signed-off-by: Vitaly Kuznetsov --- arch/x86/kvm/hyperv.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/trace.h | 27 +++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index aa866994366d..e72f8a67dd82 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -1285,6 +1285,96 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *current_vcpu, u64 ingpa, ((u64)rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET); } +static __always_inline int get_sparse_bank_no(u64 valid_bank_mask, int bank_no) +{ + int i = 0, j; + + if (!(valid_bank_mask & BIT_ULL(bank_no))) + return -1; + + for (j = 0; j < bank_no; j++) + if (valid_bank_mask & BIT_ULL(j)) + i++; + + return i; +} + +static __always_inline int load_bank_guest(struct kvm *kvm, u64 ingpa, + int sparse_bank, u64 *bank_contents) +{ + int offset; + + offset = offsetof(struct hv_tlb_flush_ex, hv_vp_set.bank_contents) + + sizeof(u64) * sparse_bank; + + if (unlikely(kvm_read_guest(kvm, ingpa + offset, + bank_contents, sizeof(u64)))) + return 1; + + return 0; +} + +static int kvm_hv_flush_tlb_ex(struct kvm_vcpu *current_vcpu, u64 ingpa, + u16 rep_cnt) +{ + struct kvm *kvm = current_vcpu->kvm; + struct hv_tlb_flush_ex flush; + struct kvm_vcpu *vcpu; + u64 bank_contents, valid_bank_mask; + int i, current_sparse_bank = -1; + u64 ret = HV_STATUS_INVALID_HYPERCALL_INPUT; + + if (unlikely(kvm_read_guest(kvm, ingpa, &flush, sizeof(flush)))) + return ret; + + valid_bank_mask = flush.hv_vp_set.valid_bank_mask; + + trace_kvm_hv_flush_tlb_ex(valid_bank_mask, flush.hv_vp_set.format, + flush.address_space, flush.flags); + + kvm_for_each_vcpu(i, vcpu, kvm) { + struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv; + int bank = hv->vp_index / 64, sparse_bank; + + if (flush.hv_vp_set.format == HV_GENERIC_SET_SPARCE_4K) { + /* Check is the bank of this vCPU is in sparse set */ + sparse_bank = get_sparse_bank_no(valid_bank_mask, bank); + if (sparse_bank < 0) + continue; + + /* + * Assume hv->vp_index is in ascending order and we can + * optimize by not reloading bank contents for every + * vCPU. + */ + if (sparse_bank != current_sparse_bank) { + if (load_bank_guest(kvm, ingpa, sparse_bank, + &bank_contents)) + return ret; + current_sparse_bank = sparse_bank; + } + + if (!(bank_contents & BIT_ULL(hv->vp_index % 64))) + continue; + } + + kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); + + /* + * It is very unlikely but possible that we're doing an extra + * kick here (e.g. if the vCPU has just entered the guest and + * has its TLB flushed). + */ + if (vcpu != current_vcpu) + kvm_vcpu_kick(vcpu); + } + + /* We always do full TLB flush, set rep_done = rep_cnt. */ + return (u64)HV_STATUS_SUCCESS | + ((u64)rep_cnt << HV_HYPERCALL_REP_START_OFFSET) | + ((u64)rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET); +} + bool kvm_hv_hypercall_enabled(struct kvm *kvm) { return READ_ONCE(kvm->arch.hyperv.hv_hypercall) & HV_X64_MSR_HYPERCALL_ENABLE; @@ -1415,6 +1505,10 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE: ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt); break; + case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX: + case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX: + ret = kvm_hv_flush_tlb_ex(vcpu, ingpa, rep_cnt); + break; default: ret = HV_STATUS_INVALID_HYPERCALL_CODE; break; diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 47a4fd758743..0f997683404f 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -1391,6 +1391,33 @@ TRACE_EVENT(kvm_hv_flush_tlb, __entry->processor_mask, __entry->address_space, __entry->flags) ); + +/* + * Tracepoint for kvm_hv_flush_tlb_ex. + */ +TRACE_EVENT(kvm_hv_flush_tlb_ex, + TP_PROTO(u64 valid_bank_mask, u64 format, u64 address_space, u64 flags), + TP_ARGS(valid_bank_mask, format, address_space, flags), + + TP_STRUCT__entry( + __field(u64, valid_bank_mask) + __field(u64, format) + __field(u64, address_space) + __field(u64, flags) + ), + + TP_fast_assign( + __entry->valid_bank_mask = valid_bank_mask; + __entry->format = format; + __entry->address_space = address_space; + __entry->flags = flags; + ), + + TP_printk("valid_bank_mask 0x%llx format 0x%llx " + "address_space 0x%llx flags 0x%llx", + __entry->valid_bank_mask, __entry->format, + __entry->address_space, __entry->flags) +); #endif /* _TRACE_KVM_H */ #undef TRACE_INCLUDE_PATH -- 2.14.3