Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1173308AbdDXQQc (ORCPT ); Mon, 24 Apr 2017 12:16:32 -0400 Received: from mail-bn3nam01on0067.outbound.protection.outlook.com ([104.47.33.67]:44312 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S973309AbdDXQQN (ORCPT ); Mon, 24 Apr 2017 12:16:13 -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: , , , , References: <1493049146-19261-1-git-send-email-brijesh.singh@amd.com> CC: , , , , , From: Brijesh Singh Message-ID: <407864ef-c815-6e94-a8c3-0307821d513a@amd.com> Date: Mon, 24 Apr 2017 11:16:05 -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: <1493049146-19261-1-git-send-email-brijesh.singh@amd.com> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: MWHPR17CA0071.namprd17.prod.outlook.com (10.173.106.161) To DM5PR1201MB0139.namprd12.prod.outlook.com (10.174.106.144) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 42086259-e3a9-4561-1028-08d48b2d3879 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:Xp4ACsnoYDJ62sLxTnqiS00S80N1hqqeTbnoHzSnwtwXeRjFys7sMtZwveyLI+QOkT6FwPx5L6clFvh37vo7HNH1eAFTcB67g2E8tH/O9ikkmoP6cpki2kCquUz7M4eMtiTsQXkHBUPb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wR/mD9X+H5QHrCZGItMO46eIQitP/g6vT96PBqdoniG1s1oFHl1xdkDbukKlEWh3lUQBF837e0YwLWjopKpEGc/EmNlL5vagLKo0iO514QoWuzWw8qwywj+qA3ID5I/QQ3eOmTkyp+pXFWFk15zSmk+ybY6KzjmqBqZYSIOAXbL8NZ5JccMU9qGqKdRK2OCPbXt7g549Jsb8Ixw7NDoWCCennQHV4S+M/hcADXZ4JKwHoMCz4oOU/REk0jI3P6VbCiAtROGYlpFYon84lLuAMak7ZEN3+GtUvFM9hz3QSBa+083XEs/AvUTaXcSpMRq3wbuiORF4WvztAqnpfzMtBUZ8+DKP9OKTgt7MIvMR387OZwxxlGDZCwjeLOmHuxGapWbPRds4d4amIJqiSUDJR7B X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(93006095)(93001095)(6055026)(6041248)(20161123560025)(20161123555025)(20161123564025)(20161123562025)(201703131423075)(201702281528075)(201703061421075)(6072148);SRVR:DM5PR1201MB0139;BCL:0;PCL:0;RULEID:;SRVR:DM5PR1201MB0139; X-Microsoft-Exchange-Diagnostics: 1;DM5PR1201MB0139;4:yAxutxdu+/4FNFx6mal8MSxEEFl69L2ogw0gms/AeAFRRsepE6Nhpp2mbOjgXaxMR7lZb/Adt2/hbhISt2bWFrPUzhPE+9vRI/xEcjOYIIh956wfyRRUDwUFh4tPQtUNOcxAYEYxM4GDVPagYwTciy/HmmUz1nEovjbqIhCXznHU3ZDrtFpZtkasBYBMvTe8Atn00ftLYc2FAoubL/pMV4FZx3JQnTpneIzxcCa9yZpujYOyAhIKLGOzCSNBLydJGk3l7T1+8ipbgPH5PfHLvgV8JLxd0c9E1fnHYoWHMrGifULIN2pDv/pR2hW25sTlljbG+SwGp7LhdBI9ob9fzXPghsD4Y6X7n2/OXTwQ8hO0hO0Z8p0DBqBq/bQUUeKcAfplw4lLFbekx6BMIeieZ3l3FxnewqV5lCvPq6cErAsuAELnTaX2vvcWfNHAEGCwZ4OVgFIbCRrPfNtxTGVfubfo1x/TOqfUY2u0IDfZFjWTVlvO2dwyIny99IrJsUnAb2lcW0JAQrD/+hUT8fPQVGLyehCLlR9gTZ8WIsWjGQMQC/2c9tm+yUHqzoMaSNRblixlNvONNewOWLXjuQ0ZHZMfXrGz92y++6uEOS7YcN/w3r3zRWruDmpm3ARbYyza/xf2Q3xJqwsX38LxT8WAPrU4K2YxbD+7QwLjlWYZTt40wqQkNOiuMyLoYfLPnFnqf9Qose1K8Hz9M1r3Uqrrzt47wW+uumGLfv+75lxzYcNSCdB32Hp6IrjD99JyI8DH4hWnfjn6WDx/dJmfuQw5iw== X-Forefront-PRVS: 0287BBA78D X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6049001)(6009001)(39410400002)(39450400003)(39860400002)(39850400002)(39840400002)(39400400002)(24454002)(377454003)(2906002)(83506001)(23746002)(229853002)(3846002)(6116002)(33646002)(189998001)(8676002)(38730400002)(81166006)(2201001)(50466002)(5660300001)(90366009)(42186005)(4001350100001)(53546009)(77096006)(6486002)(4326008)(25786009)(66066001)(54906002)(2950100002)(65956001)(31696002)(36756003)(53936002)(6246003)(7736002)(6666003)(86362001)(305945005)(47776003)(54356999)(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:ydLLv3/lW+bpq/Tiz8ZK2dHfh1OYlibCrJP?= =?Windows-1252?Q?TVJMucSULTZhJbkDU2nh5HWERzJYsOiW2PEcypxp/asbbWdXWs7qrMlH?= =?Windows-1252?Q?gZ3JHCB1SySEKZuA/2v/QlN6i2ShnSjjANx/Tf+jGJdgOe4zFTlGashe?= =?Windows-1252?Q?CtKSbz5Zj/xdqiOwPiRqEnI/M6tXr0GW4WiIEtg6OUap6PvFIVf61ECV?= =?Windows-1252?Q?Lz2i/48f7nznYKARG7eM7qQ6cbhSPZFMaaXgvD1K6rFew2mPBtM0LWsu?= =?Windows-1252?Q?4Mj0AIa4lGSRmn6pSB6h2t7JFmHUv71vzuKvHAnH5k4Py/uEvWcNvBu4?= =?Windows-1252?Q?LolTu2NCLJ+vhkX9kJZPQdecMxRcfcbqTtzE0Fo4D91f0vwgGr9jt19Q?= =?Windows-1252?Q?4GAG16GC8uAlIh9f83dmA8LoMgdpozMwYES0X4dm/kiHjQP2Oaq4xhMD?= =?Windows-1252?Q?FM8ynQB+qkp/F7UOTNySU2Mh4sWFnahc5QQWHbogtgiw+Nfd9Yg/ISiX?= =?Windows-1252?Q?IgB26nkuxBmBK0XY4Y6OHhTdn12ZjCgLUNv6oF+BCjxtgaYN7mxO6Mm6?= =?Windows-1252?Q?AtbtTC1Y4Lev7Y9QcvXJlhgv9AL2OS/fpTeoCC9VYiwpF98VDOW8Zu/6?= =?Windows-1252?Q?0HQEay4RGOo/U6n+KHa3obbeXw/9ChLUXxtoapoOR8ViwbkH6ciejEB9?= =?Windows-1252?Q?o1q99LfI97d4Pq1QZcbLXaIRlvgCiFIZcYPkL02lopsKNoPkC3xW17Xm?= =?Windows-1252?Q?HMuUpqKX1KI//FAD4gIygtLyYpqU8yQMm0eWyeyTdEVJS58wvNNYE7zF?= =?Windows-1252?Q?n1vtCHqD3pB7zQhvzId/6GWU24xGpKA3V7/KXWXgPmWYD/iqT5X8iEH/?= =?Windows-1252?Q?rjNRiSEaTuNRLDY8QY1SZXJIRxfl5+PJcwL0Aq4QoIWNzGcDyPyDPa5z?= =?Windows-1252?Q?PQf73uTPFiZYkwUxfNy9HAmeTwZaocBtXDb1NWNyoo2xOqmSH9bGNCvd?= =?Windows-1252?Q?vuwQ9lt7uZg0SCxK/yv2gWlP9s58aZyX6S+FQIn4L6Vj3sqEaFapvXpp?= =?Windows-1252?Q?JEX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gEYMhCSBTfo32h/UmbwOEPSodnUmRApaFG/JWkMoGwobUdvh7bvUEYKqz0263Ytn4xAvGPEdOJzhhTHs+32Us1HfUrFNzI9TKLbxmftLk= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;DM5PR1201MB0139;7:sPuzy8NuTje8z3AALA2PfDAwFZII+CSXoc/8IXa71ocaL4gzssQhRAx3jhQQ6TfgS8L468PkXA0cNX0OF3POb4gAbAokLTjdbfX0aTHGhh4FZUGVZX/FAC+smlaLQ/Dz4x93hh6jU0KsxRR0Sx3xTc9cNtyB6SHnJfPTeX7l9181H74+ccRb00nDy0PSdC7f93ldrXbLGOLyf5iJ+DMlBD9/OHtApvSyeisoXwfVWRMqW3iP6oncEvikRlY4V/2D9jKo6Be6+k1qNgqbK6so1WsPh7zTvBaK2fDJtDh13LPRsjpx9n8JoH9xRMUR1+31DEn+GPE2V46l9ZKpX7UQEQ==;20:FiWbc5b0jWDBc9fB+iMmoK5paHsk6K0V6i3J8zkDqxWH49/hRPbS84kfWNWI0lZTragwmsMsiu4LuBdnbLn3z8gfsEL+US3yAoKkY8gH6hStXUn6FJVEcfbTanzVo8vUOd19nMhq3bbPYo56fPApPqKqLNcBQasFAvDJe17ilBblYEf47iJ0KA36M2O78FYiAelQ2iCpkvsvrp63xHA+Z3TktXqle30fYuEuPhbfbukLaX63Ye8yAr8HfmYUdLW4 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Apr 2017 16:16:09.1611 (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: 3225 Lines: 98 On 04/24/2017 10:52 AM, Brijesh Singh wrote: > On AMD hardware when a guest causes a NFP which requires emulation, Just realized a typo in patch description, s/NFP/NPF > the vcpu->arch.gpa_available flag is set to indicate that cr2 contains > a valid GPA. > > Currently, emulator_read_write_onepage() makes use of gpa_available flag > to avoid a guest page walk for a known MMIO regions. Lets not limit > the gpa_available optimization to just MMIO region. The patch extends > the check to avoid page walk whenever gpa_available flag is set. > > Signed-off-by: Brijesh Singh > --- > arch/x86/include/asm/kvm_host.h | 1 + > arch/x86/kvm/svm.c | 4 ++++ > arch/x86/kvm/x86.c | 26 +++++++++++++++----------- > 3 files changed, 20 insertions(+), 11 deletions(-) > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > index 74ef58c..491326d 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -677,6 +677,7 @@ struct kvm_vcpu_arch { > > /* GPA available (AMD only) */ > bool gpa_available; > + gpa_t gpa_val; > }; > > 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; > + > 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 > index ccbd45e..18ec746 100644 > --- 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; > > r = x86_emulate_insn(ctxt); > >