Received: by 10.213.65.68 with SMTP id h4csp78340imn; Thu, 15 Mar 2018 10:04:06 -0700 (PDT) X-Google-Smtp-Source: AG47ELu3BBc1M9l6UmZqRffwY7NE6XrCsLKdr4cQeBpb9bQPS3oEhc+wOhhuNi6K+VxNWJ4JhPgd X-Received: by 2002:a17:902:24a5:: with SMTP id w34-v6mr3798602pla.328.1521133445975; Thu, 15 Mar 2018 10:04:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521133445; cv=none; d=google.com; s=arc-20160816; b=K5GkZlhuel/wPaglQCdC4Ws8GSp6/dX8QNL0THVilM6tTuAdaunncYL8jLK1rTctpf 6+wG8ckvbITEli/rZ8hKTGuGzRSsELmyZhKpOhyphth9i1nlMfTO5mHCT+8QndlgnlFV RI/E3fIWDGmnW8RRqW7XcHF/fI7rK8ThRIF1r8g+URzhoun7+vX0dThsbOPpj1P4YfXf 12LZccwOUz03eOxSPSvpPS2SXjNbeja1VTTWuLwI8yoZ30wbrMMxhrc4r6RCCIkmF6B0 A9+W1JuypDNAZoN4fngvgp9p4KHqwMZCf6vDucA/58gKbNlLuQyEPrRtt1dOkr6+j/ck OUvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :arc-authentication-results; bh=0nsNRrDwOisrsFDlhD3L7WysCoDSENzWLaaajooWrmQ=; b=1FvIXyPqk2lkZxByY0WwniF+6th3UNiEpP/dgBSKIsLxpoYrXCAxYUUrWNE3Ky7l/B HLVKs6o1WMLTWDhUmu8FOiMPZnfkFFDciohsftW5HZOUlzbn+vQ/3qaCMLEHZW+wmkAG 8jBjDJulNe5MNfGi0SDmZyDtPGC+9y6zZVACgERwhXi8RNm2Q0/tWFW3ri7v41Lkkqfg 4hJgfVtQRT2elvuFKFVSCHHcU1ZOOCl7E+vLSbG7+17lCgaFJixcAGeg9TZ7G1Tqc0bM LDQwL66XRszfoi9wUvSiZuojxDzgtyh5llvgrEj9BILxjphvHNyq6zBuQnTiDChxI70J ajvg== 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 z22si4049680pfa.4.2018.03.15.10.03.51; Thu, 15 Mar 2018 10:04:05 -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 S1752361AbeCORCL (ORCPT + 99 others); Thu, 15 Mar 2018 13:02:11 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:48044 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751834AbeCORCJ (ORCPT ); Thu, 15 Mar 2018 13:02:09 -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 B03424040856; Thu, 15 Mar 2018 17:02:08 +0000 (UTC) Received: from flask (unknown [10.43.2.80]) by smtp.corp.redhat.com (Postfix) with SMTP id D28CFC1FB6; Thu, 15 Mar 2018 17:02:03 +0000 (UTC) Received: by flask (sSMTP sendmail emulation); Thu, 15 Mar 2018 18:02:03 +0100 Date: Thu, 15 Mar 2018 18:02:03 +0100 From: Radim =?utf-8?B?S3LEjW3DocWZ?= To: Vitaly Kuznetsov Cc: Paolo Bonzini , kvm@vger.kernel.org, x86@kernel.org, "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , "Michael Kelley (EOSG)" , Mohammed Gamal , Cathy Avery , Bandan Das , linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 7/7] x86/kvm: use Enlightened VMCS when running on Hyper-V Message-ID: <20180315170202.GA5180@flask> References: <20180309140249.2840-1-vkuznets@redhat.com> <20180309140249.2840-8-vkuznets@redhat.com> <2747cc75-b549-61bb-9c1b-0f554a49b536@redhat.com> <87zi399xih.fsf@vitty.brq.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87zi399xih.fsf@vitty.brq.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.5]); Thu, 15 Mar 2018 17:02:08 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Thu, 15 Mar 2018 17:02:08 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'rkrcmar@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 2018-03-15 16:19+0100, Vitaly Kuznetsov: > Paolo Bonzini writes: > > > On 09/03/2018 15:02, Vitaly Kuznetsov wrote: > >> Enlightened VMCS is just a structure in memory, the main benefit > >> besides avoiding somewhat slower VMREAD/VMWRITE is using clean field > >> mask: we tell the underlying hypervisor which fields were modified > >> since VMEXIT so there's no need to inspect them all. > >> > >> Tight CPUID loop test shows significant speedup: > >> Before: 18890 cycles > >> After: 8304 cycles > >> > >> Static key is being used to avoid performance penalty for non-Hyper-V > >> deployments. Tests show we add around 3 (three) CPU cycles on each > >> VMEXIT (1077.5 cycles before, 1080.7 cycles after for the same CPUID > >> loop on bare metal). We can probably avoid one test/jmp in vmx_vcpu_run() > >> but I don't see a clean way to use static key in assembly. > > > > If you want to live dangerously, you can use text_poke_early to change > > the vmwrite to mov. It's just a single instruction, so it's probably > > not too hard. > > It is not: > > +#if IS_ENABLED(CONFIG_HYPERV) && defined(CONFIG_X86_64) > + > +/* Luckily, both original and new instructions are of the same length */ > +#define EVMCS_RSP_OPCODE_LEN 3 > +static evmcs_patch_vmx_cpu_run(void) > +{ > + u8 *addr; > + u8 opcode_old[] = {0x0f, 0x79, 0xd4}; // vmwrite rsp, rdx > + u8 opcode_new[] = {0x48, 0x89, 0x26}; // mov rsp, (rsi) > + > + /* > + * What we're searching for MUST be present in vmx_cpu_run(). > + * We replace the first occurance only. > + */ > + for (addr = (u8 *)vmx_vcpu_run; ; addr++) { > + if (!memcmp(addr, opcode_old, EVMCS_RSP_OPCODE_LEN)) { > + /* > + * vmx_vcpu_run is not currently running on other CPUs but > + * using text_poke_early() would require us to do manual > + * RW remapping of the area. > + */ > + text_poke(addr, opcode_new, EVMCS_RSP_OPCODE_LEN); > + break; > + } > + } > +} > +#endif > + > > text_poke() also needs to be exported. > > This works. But hell, this is a crude hack :-) Not sure if there's a > cleaner way to find what needs to be patched without something like jump > label table ... Yeah, I can see us accidently patching parts of other instructions. :) The target instruction address can be made into a C-accessible symbol with the same trick that vmx_return uses -- add a .global containing the address of a label (not sure if a more direct approach would work). The evil in me likes it. (The good is too lazy to add a decent patching infrastructure for just one user.) I would be a bit happier if we didn't assume the desired instruction and therefore put constraints on a remote code. We actually already have mov in the assembly: "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" "je 1f \n\t" "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" // here __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" "1: \n\t" Is there a drawback in switching '%c[host_rsp](%0)' to be a general memory pointer and put either &vmx->host_rsp or ¤t_evmcs->host_rsp in there? We could just overwrite ASM_VMX_VMWRITE_RSP_RDX with a nop then. :) Thanks.