Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S978745AbdDXWOf (ORCPT ); Mon, 24 Apr 2017 18:14:35 -0400 Received: from mail-by2nam01on0073.outbound.protection.outlook.com ([104.47.34.73]:47603 "EHLO NAM01-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S977261AbdDXWO2 (ORCPT ); Mon, 24 Apr 2017 18:14:28 -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> CC: , , , , , , , , , From: Brijesh Singh Message-ID: <77f51978-5937-0c94-13b6-885345921b03@amd.com> Date: Mon, 24 Apr 2017 17:14:21 -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: <20170424205236.GE5713@potion> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BN6PR1001CA0021.namprd10.prod.outlook.com (10.174.84.34) To DM5PR1201MB0139.namprd12.prod.outlook.com (10.174.106.144) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ef5858f6-3c6f-447a-6258-08d48b5f45aa X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(48565401081)(201703131423075)(201703031133081);SRVR:DM5PR1201MB0139; X-Microsoft-Exchange-Diagnostics: 1;DM5PR1201MB0139;3:ettJ5LQZvGODwSOrQxcElqFwU+GpzK23TOJJYqO0pZeV11iWBYvb8RaMOSScImS16Usky9sgbJBVeOtf+d4G2ESs6u7glmlgUtSfyn9BTrsb2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gPJVmtbE0ifvaa7qLTRGP3+DG8dgNncUuci/iHXClhSF5LJWvNg6wAuZZkKJ4U5ENVphwiFJSpGH6n7Cqv9Z5dBG7h4NXzuCPd/iVNE5uCVVVMsR2w3Ub64R2vQl/bDe8Mob1ph1Gm+on9EmjgpcguXNoztZdXs2Ieo7bjXnFpAAVB8MiNQTfpwKWQEPQs5V48OtDARneyQY7YWhXUlFAbwkqyik+K2hSQGClI1F+ICj3lBlzpXuHQqN98rNNAQBY8WJGRzxhdFwMphL8YOyt2D4fuXwQv628D/VHuIyHE5OXkBuH0lQxvsWTeqzS30oXdLyZQAV5cpzS8aEx3vaYP+4OlbXrtjeASLfmiLbxlnbC4H204VIqeQAztEJOnAFzfg2zranoWwxIG0r9ibGjmM+bzSfiYSnQ2Me1Xw4mhnZZ+wlWTf6lLc13ve5LrxNJOfvmB X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046)(93006095)(93001095)(6055026)(6041248)(201703131423075)(201702281528075)(201703061421075)(20161123562025)(20161123560025)(20161123555025)(20161123564025)(6072148);SRVR:DM5PR1201MB0139;BCL:0;PCL:0;RULEID:;SRVR:DM5PR1201MB0139; X-Microsoft-Exchange-Diagnostics: 1;DM5PR1201MB0139;4:YcKlS6/VWQjcJopUlh5IuKOuQ/Ao3Xip+lM++hfMscEz/Dws/BsKK94yRSHXTk3TrEFu9xQAtHYmtUmZMrVji9cEneRuvg8XGFoTYFXgFi1deKKoc8yS/CZOUBsuNifeRWKwCmeuEsQoz2aKLrwFVBxLqnHlpSpeo8nW5RBL2TWPIHT5++4PKtomNU4UASy1wLbQ3PMXLSCO7HceF+z9Qm4UApbTOAegQ2EQiFOfZVRGmYeI/W2x+Va4+13qy7J8Jbt/WQnfT/+oj+MIraLalwpYXEavR3TcsuyLt6CWzYFBv4tgKNcQJeZRgzjrF7ay/V3gBnqvY2E3Itcs/P15L+alorqGrZU9aRU4+peM8sOmD1XIJJktH3ptgTc8iMc4cXeUpJyOEZNqYsF3XLHD37WtQSCzravF2CuO9nfleK+opFx8MLjq6/8MgTWJiL+KvMmQtyYZ+ZmcetiSNYPGaDvKXMczrLgvAW0kDzc+6P6mIaI01HLKxusdW1fQDZlL9kR+yCL0gsJiGM5iB9A5BsGIjDEsozTFKf6YVDvfD4wR1z9Q166xBTP9ml2/vvGXhYjdwE4Q+REBJjShEugUD3tWmZtbGlB9vTq/qFX40oeamZl5/Xg5oxiK+UzSlUzL6LGKHNblAL+5xgCsR+h+Uer3zp1kMsmrIMIfn++ft+s/niy5ZkKa9lnQDjqqPrK6rAViDsj38mN2LKnjzXcx6Sbcf2SIjoNNPlobGyvecvY= X-Forefront-PRVS: 0287BBA78D X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(6049001)(39410400002)(39400400002)(39860400002)(39850400002)(39840400002)(2906002)(83506001)(23746002)(229853002)(230700001)(3846002)(6116002)(33646002)(189998001)(8676002)(38730400002)(50466002)(5660300001)(110136004)(64126003)(81166006)(42186005)(4001350100001)(31686004)(4326008)(77096006)(6486002)(66066001)(54906002)(25786009)(65956001)(54356999)(2950100002)(36756003)(31696002)(53936002)(6246003)(86362001)(6666003)(305945005)(7736002)(47776003)(6916009)(50986999)(76176999);DIR:OUT;SFP:1101;SCL:1;SRVR:DM5PR1201MB0139;H:[10.236.136.62];FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?Windows-1252?Q?1;DM5PR1201MB0139;23:88f2Ay/thmo7pzy+VHlJqUMvz0/8SMVfvwU?= =?Windows-1252?Q?SU5XI7f+9PKN3hIbqG6D9oSZgA0ZtklpWMURaun9G/g4kpj+Nr9oEMrk?= =?Windows-1252?Q?48eA62gFNjxLdNKA7GITpyP+ghRJLDA9WVLJB2e972Z20bkt9lSVAGUC?= =?Windows-1252?Q?HIuwuhTRg3nJj1eqtFIiuMS5CGKL682hVsRnczmQM+zz+9mLGfz/GMl8?= =?Windows-1252?Q?iW35SbMUytGX8JVKkcr1t5TEVAwMXcGVELk99HN7SiRYVGOWpK3ecSOF?= =?Windows-1252?Q?s93CwfzIhQSxRoymSl8C0AuiYZydkfTK1LabOqybLBce1AgWRhXq4b4A?= =?Windows-1252?Q?Mqo9u/zIDoklMbBjHweovMk8hviUDqRVZmvtt0tdRXTrPpXc/1U2q2zR?= =?Windows-1252?Q?3gt27HX1wBMo8pxoASnzqGR0lHz8hT+K1ipKxrJRl2H7Sf+CqqOmgMfG?= =?Windows-1252?Q?7BuqXqyWRR9QAT1NwkTXHIEFwhiYjMTwSB1U69sNFQFW7Dim9CYgEpT0?= =?Windows-1252?Q?GdfEwfJRut8e1PqcvHIXwNXu1VyqicDpby2NB1jxPtweuDdsRRCCN0oK?= =?Windows-1252?Q?qaiqSp0nfic7I2yAKzlCcz0CnMBqiPh7eRZdZJ+sMzdhDKLAIYu3JMsU?= =?Windows-1252?Q?9SK1jWCyD41O0/1wAwnalP93YOluYvujzuR4Eyk3AZOSf4D4RFBQ3CWJ?= =?Windows-1252?Q?oFVA4QEZRaV+cQ9vF0+l8Tp9To08YkUPUATq9LlwG98H2cI31yzkyTjn?= =?Windows-1252?Q?uBdlN1ahi5jhbRHcddYR49i6KrtG3d/31/efcHR1JXYZwERmsAlcUZ38?= =?Windows-1252?Q?LDkKB55xMjbeEvvtYJEkGgx5OcHcYdclvBIkRt6Q2Rb1Rc1qeI13mhWi?= =?Windows-1252?Q?9wifxaVDkQG0RiRhb5MlIMEwVxXiG7Djmz90XnYGgd+dyGnZfvHdVo8M?= =?Windows-1252?Q?Un8M3DjelC4w0pbaGF7xktIreU/xNd8LPvllBT6bXSIoSYlA7QHnAezM?= =?Windows-1252?Q?0h7tWoDZrjrJHC7IOVR3T2ljx4ArVepcAGDiVk++oO+HAcquQi1OBkjW?= =?Windows-1252?Q?Uf8h11lwbSL1HAg1UMaob8TK+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X2/uohjQwCSt+Prd/TI6V38Qe6/cEqSRZi98jtnpTiKLpVnAmt5HeNAvZPyqzyoftRv4zG0K3lVWC0= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;DM5PR1201MB0139;7:ytQlA8ajpyME6ac81KWnLBTcyBo443PM/vZZXvxSBsN27bVwP7mezRmifK+8J7OIEJ/qFs4l6+1eVQ9QBW+qiDmHgu2/xsvzBK1AI047BNxXtJILUnYs6NIjFjPblIfNRwmy+cleDtYUBEuclJHwyXLlG1Nx6t89qfOVj2/OL1H7nJYtEeRPjTB4t7aFx2yJC8K3VUzNVWiNYgGz4mJXv1Vs95B0nLGbNiK/AFvib6AV1Yaq0OPFem8/Resa2tL78AIs1dQ70SdnERkl64tcfjIcY+8BKlmWuuepCfyxafT6sN2fYZ3IAlWRqPd2iEayldr3mGL7fsRXU+2LBwRHOQ==;20:Lydvg6GzlFYKxbbw0wS1otj7/yOlGVmCjcc2vd32NuLskk2X2rVQvZXPwbhIL+XGkgcFeDCpaTlUdJC5oX/PhzysQwiausOUjhhYiqDhqwH8dkIqFsKcAom+N2Xoa+TC0xEBl6wIjJMJqgCxxKJ5T9QIMwzGE7ydy1wfxueCq7UQemEOZCycYH7kpASqiYrbcFzTokKEAZhofE+anawpgjQ1ONApCXUZ7XMg5FWqtjtKzQ+rLIxEyrQyOpb3P5Ai X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Apr 2017 22:14:24.9053 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1201MB0139 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3944 Lines: 126 Hi Radim, >> /* GPA available (AMD only) */ >> bool gpa_available; >> + gpa_t gpa_val; > > Can't we pass this information through function parameters? > > (I'd rather avoid intractable variables.) > I also wanted to avoid adding yet another variable but we can't depend on cr2 parameters passed into x86_emulate_instruction(). The x86_emulate_instruction() function is called from two places: 1) handling the page-fault. pf_interception [svm.c] kvm_mmu_page_fault [mmu.c] x86_emulate_instruction [x86.c] 2) completing the IO/MMIO's from previous instruction decode kvm_arch_vcpu_ioctl_run complete_emulated_io emulate_instruction x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0) In #1, we are guaranteed that cr2 variable will contain a valid GPA but in #2, CR2 is set to zero. >> }; >> >> struct kvm_lpage_info { >> diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c >> index 5fba706..8827e4b 100644 >> --- a/arch/x86/kvm/svm.c >> +++ b/arch/x86/kvm/svm.c >> @@ -4159,6 +4159,10 @@ static int handle_exit(struct kvm_vcpu *vcpu) >> >> vcpu->arch.gpa_available = (exit_code == SVM_EXIT_NPF); >> >> + /* On #NPF, exit_info_2 contain a valid GPA */ >> + if (vcpu->arch.gpa_available) >> + vcpu->arch.gpa_val = svm->vmcb->control.exit_info_2; > > How is vcpu->arch.gpa_val used between here and the NPF handler? > handle_exit [svm.c] pf_interception [svm.c] /* it invokes the fault handler with CR2 = svm->vmcb->control.exit_info_2 */ kvm_mmu_page_fault [mmu.c] x86_emulate_instruction [x86.c] emulator_read_write_onepage [x86.c] /* *this is where we walk the guest page table to translate * a GVA to GPA. If gpa_available is set then we use the * gpa_val instead of walking the pgtable. */ >> + >> if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE)) >> vcpu->arch.cr0 = svm->vmcb->save.cr0; >> if (npt_enabled) >> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c >> @@ -4653,18 +4653,16 @@ static int emulator_read_write_onepage(unsigned long addr, void *val, >> * occurred. >> */ >> 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)) { >> + emulator_can_use_gpa(ctxt) && >> + (addr & ~PAGE_MASK) == (exception->address & ~PAGE_MASK)) { >> gpa = exception->address; >> - goto mmio; >> + ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write); >> + } else { >> + 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; >> @@ -5675,8 +5673,14 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, >> } >> >> restart: >> - /* Save the faulting GPA (cr2) in the address field */ >> - ctxt->exception.address = cr2; >> + /* >> + * Save the faulting GPA (cr2) in the address field >> + * NOTE: If gpa_available is set then gpa_val will contain a valid GPA >> + */ >> + if (vcpu->arch.gpa_available) >> + ctxt->exception.address = vcpu->arch.gpa_val; >> + else >> + ctxt->exception.address = cr2; > > And related, shouldn't vcpu->arch.gpa_val be in cr2? > See my previous comment. In some cases CR2 may be set to zero (e.g when completing the instruction from previous io/mmio page-fault). If we are decide to add the gpa_val then we can remove above if statement from x86_emulate_instruction() and update emulator_read_write_onepage to use the vcpu->arch.gpa_val instead of exception->address. if (vcpu->arch.gpa_available && emulator_can_use_gpa(ctxt) && (addr & ~PAGE_MASK) == (exception->address & ~PAGE_MASK)) { gpa = vcpu=>arch.gpa_val; ... ... } -Brijesh