Received: by 10.213.65.68 with SMTP id h4csp20137imn; Thu, 15 Mar 2018 08:21:12 -0700 (PDT) X-Google-Smtp-Source: AG47ELsSF3nb41oG5j+oNjZ4dSxsa690m8JRsstSbexL8i/5gqnWPJF0l6Txaw7eJU+RaQRBDnLM X-Received: by 10.99.100.197 with SMTP id y188mr6913799pgb.277.1521127272138; Thu, 15 Mar 2018 08:21:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521127272; cv=none; d=google.com; s=arc-20160816; b=Az3SOBOeo++m5Bk9+mid4linj6GWlVstzAJ45VTDuwNrJcc5pyNVSL+bpJxnRF8RNa lWoGlzOmmjm4nPaA198JYxrL2/zhFTMYkJELZWoAGYeD2Yj+hnyP+l6NgKpha4MsB+2a hN/nvP38rJPMmjxrSiUceYIzzKQFatUtwoYSM5n37GF9ysk8/25Az/y+fj0yh4CpKgoG BxSWKvCqX6XRVHH5osdLCTWhjoTEA5YV08oteyHWP7HCiwCsOupu81C1ddBPbUG0RZXY 0478Y3d0WH58fWA5DWWtoKoL89cI0BEshwPdhD5kApLRvjEOTalPPlAn2hpgGc+rX4CL +Yvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:message-id :in-reply-to:date:references:subject:cc:to:from :arc-authentication-results; bh=D15NzN9tardhvvAfIun+z6GX54eS+SgezZKgG9wSV+E=; b=aIknhNWob4xNOtlRzlRVzQfMuxzDILHHjqmjTZDa44OSO4+a5wnomJfM0WFoFTHFGV ClBQtHAjwwMXhFfDRq12k9ojVooKxXbYRJhkWxTY7uPeg9yZdA3PK/KYqN2rDdPEHk92 wjZi29Jm3ReNcfkgceC8h8TNCoX57TEK0FtqcYR+OgaAlLWudyvMGI/MuM4hVVswPQNj Eli7m2qZ2N+RA/fN6Bsl8ddzDgVbVIOKgvYs6NlbwPYyFxi+m54E6P9KSQfOooVSYHiL bM8OSp2ZZdVebaRX54f3pnZofbn8ulht7Y2KDcqwmsuVZcP9eVmYGJtWS5Uzi/eNqpBE 8UYQ== 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 q127si3958780pfb.414.2018.03.15.08.20.57; Thu, 15 Mar 2018 08:21:12 -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 S1752601AbeCOPT4 (ORCPT + 99 others); Thu, 15 Mar 2018 11:19:56 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:44482 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752254AbeCOPTz (ORCPT ); Thu, 15 Mar 2018 11:19:55 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9139AEB6F6; Thu, 15 Mar 2018 15:19:54 +0000 (UTC) Received: from vitty.brq.redhat.com.redhat.com (unknown [10.40.205.96]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 451072166BB2; Thu, 15 Mar 2018 15:19:52 +0000 (UTC) From: Vitaly Kuznetsov To: Paolo Bonzini Cc: kvm@vger.kernel.org, x86@kernel.org, Radim =?utf-8?B?S3LEjW3DocWZ?= , "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 References: <20180309140249.2840-1-vkuznets@redhat.com> <20180309140249.2840-8-vkuznets@redhat.com> <2747cc75-b549-61bb-9c1b-0f554a49b536@redhat.com> Date: Thu, 15 Mar 2018 16:19:50 +0100 In-Reply-To: <2747cc75-b549-61bb-9c1b-0f554a49b536@redhat.com> (Paolo Bonzini's message of "Wed, 14 Mar 2018 15:53:32 +0100") Message-ID: <87zi399xih.fsf@vitty.brq.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Thu, 15 Mar 2018 15:19:54 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Thu, 15 Mar 2018 15:19:54 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.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 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 ... -- Vitaly