Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1425406AbdD1TPy (ORCPT ); Fri, 28 Apr 2017 15:15:54 -0400 Received: from mail-cys01nam02on0050.outbound.protection.outlook.com ([104.47.37.50]:10464 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1423634AbdD1TPs (ORCPT ); Fri, 28 Apr 2017 15:15:48 -0400 Authentication-Results: amd.com; dkim=none (message not signed) header.d=none;amd.com; dmarc=none action=none header.from=amd.com; Subject: Re: [PATCH] x86: kvm: Avoid guest page table walk when gpa_available is set To: =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= References: <1493049146-19261-1-git-send-email-brijesh.singh@amd.com> <20170424205236.GE5713@potion> <77f51978-5937-0c94-13b6-885345921b03@amd.com> <20170425140351.GF5713@potion> <6e453f35-cd26-1df8-5f8e-68fa09c6a1a3@amd.com> <20170426204427.GA7135@potion> CC: , , , , , , , , , From: Brijesh Singh Message-ID: <7e27af5e-5978-5b57-f008-58515b218cce@amd.com> Date: Fri, 28 Apr 2017 14:15:39 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <20170426204427.GA7135@potion> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BN6PR14CA0010.namprd14.prod.outlook.com (10.173.157.148) To CY4PR1201MB0135.namprd12.prod.outlook.com (10.172.78.17) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 2fff9f0d-f0f5-44bb-9b3b-08d48e6af8b9 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(48565401081)(201703131423075)(201703031133081);SRVR:CY4PR1201MB0135; X-Microsoft-Exchange-Diagnostics: 1;CY4PR1201MB0135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h7tKlQjsgRl1EHh4cLF6VKMD5gxGVRsKZDr36SM=;20:p2mGYR2DYVRomNKjaWqAnloofdvj7UvbNgaztI/2K8VtdVl9IuEkLdNlF5KhLa+zGy4VDPsKTxSD9/gMNXVyGvh1Lz3NkZ2A7CPBwAJWc1zDevrCIbirKwPHGQhikcr13Lq6/91WON/KJMIvS2mHKNSfs9IM5NWnClvnL3FTbF4pTu379NRLZcxjIr7W1gXPIMbyJJMPfHDFHsJV8Go8NLovXaMf/7zR3+cjZOMrTjK1ZEUP8KKTKxAoPya2To/cWZOlcT/j5Vdl3ONUv2lJgM/B3hKJQKsDUqqyRjw0WVwReULWE9DxpoituqdenpzDVHLA3vujBrxuHiZsFLXFjkbIalMhp18wrZcWTk0ZkIS7mSkX8NseGLhvFJkto2T/V6EeUnwBNKvIKPPJ9ofXZrxZZlAuPJYqYpAa9sY/CAJONtPMWj0FrtUzrnPpKDryBaONIk8A1tXFNIy1AFzJdqTxskbZZ+Xkeo2I73zRN8nanM33Cd6RwTcLKf+i2eqD X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(17755550239193); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(601004)(2401047)(8121501046)(5005006)(93006095)(93001095)(10201501046)(3002001)(6055026)(6041248)(20161123560025)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(20161123564025)(20161123562025)(6072148);SRVR:CY4PR1201MB0135;BCL:0;PCL:0;RULEID:;SRVR:CY4PR1201MB0135; X-Microsoft-Exchange-Diagnostics: 1;CY4PR1201MB0135;4:LZN4ys4FXU+ZhW+VF7z4CxpFdJOgtNs0FiLalkIB2qka/NmK9TOSNTOOMNPDrDS82rL0y+M90mEUheV5eFRm1bCyIp7WQdTEKVHr+14C1EhyE8zfBoAT4DUZULlu2FSO08pwIoqXbQuCXXt+B1fbksiuZlH7opT300+tWBrA6bQKKRwB+15NZwT5RZJTIT4B85aA91lPvnVJq9HImPE/sklc7hJ948BtMlLDCV+7p+ESUrGTMj/mYOAqjJuzAx28Yk2aXh8ujgpB9vo+bvXwfdDfifARLxj15p2kPTcBQR5V7TqrsZBPbJ1sYbCrGUIVhZalRI2Xkd4xe6YvDalumBMIQDvvvQwMd4HXM6VuAxw3Yib0kM25vZ2h9PBJJHKWnr8WbdVQdWvjUFHZa7eSOQy0rmW0ucQtcHWBbtCqU/WCpE82cVOPjkuf9Fx8kXtNivo5B4No9MUk4h7zElZUMLeZJSK+M2AaUsfGHsgczNd6cMbYQC4vG9aWB74nrMqU65ns0EqhiKyvr7fHVMVMF8PKxOGaVh+Mw3+ddLGncBUhOZ94p7jvMLz8prX+WmDdX5V/3nocLrd3BB4GTSLEBm1EWmx70RyZDMuuZZ3vQsQMP5kCJ8Zj2RWtxdCMQseZ2G+svw0RuKDt+jW6yqVbx4/c17+v72mtRJfiaZQe/EuW2V+/9V1qMgTqIpQGmis019Hn0OvKQ7wZfJqU7RlvPgvldXzHJdsjIPePD/Iaea9M7cR8yT1SVtRznEHF2rJ3xdZMWiMINyd6CwzMzJodpg== X-Forefront-PRVS: 029174C036 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6049001)(6009001)(39450400003)(39840400002)(39850400002)(39410400002)(39860400002)(39400400002)(65826007)(81166006)(2906002)(23746002)(47776003)(8676002)(305945005)(230700001)(189998001)(6486002)(229853002)(36756003)(66066001)(65806001)(83506001)(4001350100001)(38730400002)(90366009)(53936002)(25786009)(7736002)(31686004)(42186005)(4326008)(77096006)(54906002)(93886004)(33646002)(50466002)(2950100002)(6916009)(3846002)(76176999)(5660300001)(50986999)(54356999)(6246003)(86362001)(6666003)(31696002)(6116002);DIR:OUT;SFP:1101;SCL:1;SRVR:CY4PR1201MB0135;H:[10.236.136.62];FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?Windows-1252?Q?1;CY4PR1201MB0135;23:YOikFqQq6szxgC5RoUtQ0fW8eh2eC2Lenx2?= =?Windows-1252?Q?yW8Y+KU8dk4GLAdeCUEG+2d3yLrBJwYI8iQ0eadAbSKBNwjCpA1p+5ZN?= =?Windows-1252?Q?joub2j9dhfZq15uTLgdyA2y01NAd6xlwxM2doQEh73PeUZ1V+vQND1VJ?= =?Windows-1252?Q?Yxlf3Xx8O/vZeHiL6ZafVnDL+xM5Z0JB+z3KlMGW8fDb0Nz+q0qXRRkD?= =?Windows-1252?Q?eGZLEnRSfyUKN90R1rFXBdhCQ3IN/sB7Q8x4ehfd6YsYLcdGeXw3FVQE?= =?Windows-1252?Q?B0w++rvo5wtyc4haM1MYWSvyx9LFZwYe0FMdLBmLsNZ3ANlNPALXY22y?= =?Windows-1252?Q?zOAC6IgkqBHdA496ATsRB7aPOMNfAdBlqSOSO1MhZsrWKZWhUjGeW7Lf?= =?Windows-1252?Q?+wKvn/8bQ5/NIJKpaJ1CNKMst8NvMLmfgUwAgoB7FbASxQcMoeqD5fYO?= =?Windows-1252?Q?Fhj1BO07awsKyVBmAWV4V+uwTC17UkbCoFy5FlunWZcWQNFN7LcolxnS?= =?Windows-1252?Q?B10D3FluYv0uT/9arBTZ7AWNYsW8+fBI8lEQQJYLULBCuvk2hmbt/l1m?= =?Windows-1252?Q?FCMakQKf2ANM4v6ZuuT8hs6dF/aJStJKfBL6in8EOoDcNEgXK7+p3oHZ?= =?Windows-1252?Q?NJRyYPy478HC1OSwACEHrvLUaceog8gVfdh8oPC259fbcS4YOBSPloha?= =?Windows-1252?Q?Y2EROFVL9pdFI5TFJxynulPFY9qHPf8v33fvsocKQeC+6ZHM7E1uq+Pb?= =?Windows-1252?Q?8fcSZS9T6hzE69bbe0jJispjHakAuCNzInr84fsmeDYeLvvL+fUzgjyr?= =?Windows-1252?Q?QrQlcMI5rYX10NYlOaj75NZUSa11yA48JT7IjG6V2hl4d8USLxfxz+SP?= =?Windows-1252?Q?F1yeRN/ugJ7VtLQIJCqIfaTVSfmDPLPcf6Z5oxHMz+YI5Ez3XxO3G/9h?= =?Windows-1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znPb2WL6oTVeJ6AEKxMfAPsqw=;5:LqDPm7X9DYiy/TjSUMhJ2f8nVHndlC6WnZn6G8xQruCXsyyxpMguURTye7ct3TT0LL9cEwt+VT/T3JsX6kJN7w7Iwr6lxwK2ocfItY+aYbIIoFrSikm+dWSmm/KYFPTCwGjd2vT4CWjfRMu0E6piKA==;24:BUMwT/H0l64ULADv+Ijas7Ry+DlnQMH5VoYGkrd8FxTCTeih2xFy/9bG14y+bZnjU59/lkWsxH9UespAl8wr6X95HMN92t6gjID+Z0vxNNY= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;CY4PR1201MB0135;7:b/4UFvql0s9Q2GuBXmytkAXtL0pRxgWmy0SOANVxTggrVLXJdYe7GQ/cjERi4wXiSC0ny4OfkWIZ8Mou+xKKkfVafGgYt+rryeGyfqIfpBl7MVQax5+yf/wacUk/M12lLwjyFhVCehTHiiEpvKII98Cd2/7XCM6+OuZWHlsbTSUQoGp26JyHXi3UidJ6kMuWOi3gp3lkyrucKKeZpTyQ87Ehxgf7+jGDb63n//W4pNvK413LdxzzqwGmLik8Xnrhqb0zNd4KC6+UdCo6HuJQnUBstsmYdcSGFmVH2vFwcAHLH6qtTpTzliGbTk2911x/GglMmIC0hOFjk147XgDkug==;20:iLAwm9WoYZg49gKiiCUhoYTrCVJQOXgz87LUmKDnBPhPilrM7ObfgGic0x431AI26hFTFqfo/4R999NohhQtPcVX8fwxfueXMN/j+qRmv6objlqIQEMXqOeAHQNVsM517NfpwLEEwTs2lW9hMJw6THuIjtsrJNXjqPyEbZ9RunAa1owJi5I5ycRnnplz2eP3CsFSlUIwca1bO8N9tohrvp1ZBsr+8yzTYZXzOExGjKshbU9DZMjalg1OGEhp5wWZ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Apr 2017 19:15:42.8890 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR1201MB0135 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3636 Lines: 100 Hi Radim, > > This will probably return false negatives when then vcpu->arch.gpa_val > couldn't be used anyway (possibly after a VM exits), so it is hard to > draw a conclusion. > > I would really like if we had a solution that passed the gpa inside > function parameters. > > (Btw. cr2 can also be a virtual address, so we can call it gpa directly) > I've tried the below patch and it seems to work fine. This does not consider PIO case and as you rightly pointed PIO should trigger #NPF relatively rarely. At least so far in my runs I have not seen PIO causing #NPF. If this sounds acceptable approach then I can submit v2 with these changes and remove gpa_val additional. diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 13c132b..c040e38 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4662,17 +4662,17 @@ static int emulator_read_write_onepage(unsigned long addr, void *val, */ if (vcpu->arch.gpa_available && emulator_can_use_gpa(ctxt) && - vcpu_is_mmio_gpa(vcpu, addr, exception->address, write) && (addr & ~PAGE_MASK) == (exception->address & ~PAGE_MASK)) { gpa = exception->address; - goto mmio; + ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write); + } else { + dump_stack(); + ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write); + + if (ret < 0) + return X86EMUL_PROPAGATE_FAULT; } - ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write); - - if (ret < 0) - return X86EMUL_PROPAGATE_FAULT; - /* For APIC access vmexit */ if (ret) goto mmio; @@ -7056,11 +7056,11 @@ static int vcpu_run(struct kvm_vcpu *vcpu) return r; } -static inline int complete_emulated_io(struct kvm_vcpu *vcpu) +static inline int complete_emulated_io(struct kvm_vcpu *vcpu, unsigned long cr2) { int r; vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); - r = emulate_instruction(vcpu, EMULTYPE_NO_DECODE); + r = x86_emulate_instruction(vcpu, cr2, EMULTYPE_NO_DECODE, NULL, 0); srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); if (r != EMULATE_DONE) return 0; @@ -7071,7 +7071,7 @@ static int complete_emulated_pio(struct kvm_vcpu *vcpu) { BUG_ON(!vcpu->arch.pio.count); - return complete_emulated_io(vcpu); + return complete_emulated_io(vcpu, 0); } /* @@ -7097,6 +7097,7 @@ static int complete_emulated_mmio(struct kvm_vcpu *vcpu) struct kvm_run *run = vcpu->run; struct kvm_mmio_fragment *frag; unsigned len; + gpa_t gpa; BUG_ON(!vcpu->mmio_needed); @@ -7106,6 +7107,12 @@ static int complete_emulated_mmio(struct kvm_vcpu *vcpu) if (!vcpu->mmio_is_write) memcpy(frag->data, run->mmio.data, len); + /* + * lets use the GPA from previous guest page table walk to avoid yet + * another guest page table walk when completing the MMIO page-fault. + */ + gpa = frag->gpa; + if (frag->len <= 8) { /* Switch to the next fragment. */ frag++; @@ -7124,7 +7131,7 @@ static int complete_emulated_mmio(struct kvm_vcpu *vcpu) if (vcpu->mmio_is_write) return 1; vcpu->mmio_read_completed = 1; - return complete_emulated_io(vcpu); + return complete_emulated_io(vcpu, gpa); } run->exit_reason = KVM_EXIT_MMIO;