Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S972353AbdDXPyy (ORCPT ); Mon, 24 Apr 2017 11:54:54 -0400 Received: from mail-dm3nam03on0055.outbound.protection.outlook.com ([104.47.41.55]:20960 "EHLO NAM03-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1171961AbdDXPyP (ORCPT ); Mon, 24 Apr 2017 11:54:15 -0400 Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=amd.com; From: Brijesh Singh To: , , , , CC: , , , , , Brijesh Singh Subject: [PATCH] x86: kvm: Avoid guest page table walk when gpa_available is set Date: Mon, 24 Apr 2017 11:52:26 -0400 Message-ID: <1493049146-19261-1-git-send-email-brijesh.singh@amd.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: DM5PR18CA0036.namprd18.prod.outlook.com (10.173.208.22) To BN6PR1201MB0132.namprd12.prod.outlook.com (10.174.114.145) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c1090381-87ef-412d-3977-08d48b2a2454 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(48565401081)(201703131423075)(201703031133081);SRVR:BN6PR1201MB0132; X-Microsoft-Exchange-Diagnostics: 1;BN6PR1201MB0132;3:l4Odp7atC2IpK7qGc4D1spvGqa6aMmWaCWGFLsL4jl9kasDxlwOENJ/TB8YUFBlRm3QPFuHFwkQ3H8xYGnClK1xu7UuMsAW82CC123QJFkbkk505l6RF1UZ27yczkdkQ73O1YR4oa6r9HDNdVjh32QxEZuE/Bt2j1wANCa0lfaXCop1MumfvOMdPu+dcvNjErMwYG/Co+XbDhwJBMYnmSOgiuSE/airQXKOvYfWs/xq14QbF6H5oMzhFO1oNtVuKckTUJ4t3Gu5YSo7dgdmMJ/BELt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yX9A5BEg1CL/xkbRwtqbVWrtVpgp/THDQUD9U4d/VnBND7mB8YeB9oVE0WLLewbzqQYbruHPybRN4fu3CRezKcn+Fs/oOQQjLXgHG1aubxf/WHHntvfc9JE2+BPKMCIVSZa+seZo+HubLKNVklWWM2d7x5CTiis4sHvAHCL/Pzo0M9nlz6n3xKab30C3fTn9iTKy8h06hGWD2PGeMel4nQtqJy6ibyRpidUmHQNANj7+jLQ72Je+YvApYXDR3EWmX7v8RxHIZu3Kfz8M4M6QuDPBcX 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)(5005006)(8121501046)(10201501046)(3002001)(93006095)(93001095)(6055026)(6041248)(20161123562025)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(20161123564025)(20161123560025)(6072148);SRVR:BN6PR1201MB0132;BCL:0;PCL:0;RULEID:;SRVR:BN6PR1201MB0132; X-Microsoft-Exchange-Diagnostics: 1;BN6PR1201MB0132;4:kCUAxFN32WtIvr32RGeXJXYlOx4Jedf3LAqbzFNOX0JMReyWeI0Kr5IAj9rraeD+HLA5+gqdTDLckq+qiIMLTYJ/o6FTUmcBZSpumx3YArH7BjJBvR0k+oNDCQvjX9MVwDuR19GYN3oBQLlNx+taeJ8BsNd82Aih2hOjtOQxC3RakH90GvwRyGL6Rz6N0p7c7ByH4J3VPcl8TRS3CaVbL/RMPGFs7Nx1ax5NPseJTY1XTsByAqZqKzS2ygrlb5gJ0eIlG8d6WFonYegNoqkGCoNS9TU2d0ooWeUWHfmzVDMKs9626AwAGNVL6llLSA72WQGFve9YF0x7EUxPic9nwkcne8LifQsFPQcjxzgpQdVUjTlh1cKqL3N21tKdGgAsrsoxjU1cjTHSswJELn/n9U8CFwFze8WCgpdKCuODZ5sM9e3ivod1AqZOIykZTaSD4WeKMZONoVqrvh4NpMCJrZIkmDsVxaLmpqPtVeO4r1ai1/r6PUML2OUkkoeRtleUuue7PXVqLKu9ZvwaFREYx6nX+Vx6c7Z+vK5RUj7Vg0D0XkyTcT9YiZ80wqAwu/YH00fjutaV63TuiNikrcQPjYw14Z9yZkGFlWElQ0X5pqhH1ZTMNqlbG/ZprXq09naLDHj3w+Ds8IJSr6Ph1EinN/1n31ZvShRnja7agnC0sc8L4VpcrJToRecTPc1WREi5DZrkP446U8DPBCD/TQTM9HEiccOYH1OScW/hxiHCEI6RIoD3AC/J6Vv5Z/17zlUu1fWReZgrcKOp8V4T03CPzg== X-Forefront-PRVS: 0287BBA78D X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(39860400002)(39840400002)(39450400003)(39410400002)(39850400002)(39400400002)(50226002)(47776003)(38730400002)(6666003)(6486002)(305945005)(48376002)(2906002)(50986999)(50466002)(66066001)(53936002)(42186005)(53416004)(6116002)(189998001)(81166006)(25786009)(3846002)(33646002)(8676002)(86362001)(5660300001)(7736002)(4326008)(2201001)(5003940100001)(36756003)(54906002);DIR:OUT;SFP:1101;SCL:1;SRVR:BN6PR1201MB0132;H:brijesh-build-machine.amd.com;FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;BN6PR1201MB0132;23:hizETufLYPWQHqQbASear00iB2Jb3CvApDkn76+?= =?us-ascii?Q?MhlJxJ1aV9TVEbJFEJQ+UWmlqa6GRASC8dDkwIzhYP8zd2tOlSGyOIDLsYkD?= =?us-ascii?Q?vips2Z1gboRHy9rEHRIsx/3R4JIzfyhuvIawIk+8t7c4jFTLgXeaA7ewU5EK?= =?us-ascii?Q?n8Q3t6iTQZ2ihb9EMJqaDDWsjWBl84aYE7sdaunqC0vthf5wUTOpRAt62dxj?= =?us-ascii?Q?7UUc9hp8mA7wg2nmzvHMlrctkUJlktWFLRiDUKYNXdEd7stlE65WXZQ+/F7L?= =?us-ascii?Q?8TuTTblbWOjl6ZXEeyzYZuzDRCFRADlkr84/K24uVfDAgtOnADfe0ANIjJN+?= =?us-ascii?Q?gRfHM1mS9XyPeNq3UfyBcv3gJGBvHm3GiQ1NB5eSOxrnNq1NCgD/dOpnm5SO?= =?us-ascii?Q?kXjooNwkPX0r1iqLju6h4XTODJZMlWffGLylx36f2AmXIyaiZhQf1N9vWMkb?= =?us-ascii?Q?0Y0DZRpCUDISuIA8NJYP93/tt5kimNTxZQVHdqGBHrC5PI9Fg/9y2O9bdaAl?= =?us-ascii?Q?G+/5+Is2GphVQGcNEraNug0NP4BmVRg5GWHiiem0b4JMfc3RrSAsIXhm4aPM?= =?us-ascii?Q?FUmpTG9ilGik11zbOHvIpB5P0ERZUo5gP5riaHZ3As9RBdFup9UDWlNv4bTq?= =?us-ascii?Q?DV3hw9ufxpIJqdfEorp98p8U7QT9mlC030RMUIFAETOmiL6McIkxAaGWmTnP?= =?us-ascii?Q?1BpUr7B2c/Vf3wPPFLju4JMcOJARMs3wfk8fEGNxAQSBbQbm9CNz8z5pajm/?= =?us-ascii?Q?nfUp6v0/i8b8953CGLWdctgk9HJzoZ+N2BgerInbZmzIY2U1gVvNFzd0kgkx?= =?us-ascii?Q?C2cm4a9blr/+4UpTfubnOeVeSxzbFWGlHeb7H6R5K8357Y6oT0QBFa2uO6Z0?= =?us-ascii?Q?17dePj3f5+CQ14eZdGYCXwzMaNbU870ylewbyfqSdtuhmr0250ueHKLYNFxP?= =?us-ascii?Q?gabJO9XBEZKrRzUxSTmaS1Z9dKBeqtzE4ozFi54f2iQ=3D=3D?= X-Microsoft-Exchange-Diagnostics: 1;BN6PR1201MB0132;6:9STIKMSJwxW475pjoHPSnOuc/dS/+rKNqNR3MsWxVcm0trWKksy1CYY4j6eXM8PA0L46vPjIhoyPddic7QzIOAT7DUxc32FEy1AvC8IXz5DrP+5j2D3cB208YqedA3KzuijSHEYqo5cdi3I4NJ19swUhVqWHVnqEKfxzME41v0fmVXw+0R6jG42EwSR4kD38UW/6KSez5sa7plsisQoaoNq3pw1HMw2PSwdKZOdIqG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fyUUb8OberrEHkdMGOBBroc8G4EctHcPg4DSsHQk7YXaQCgjcGiDIpYceAtvdAxMrPuGmgCuYJELEIw5SbvTezcDXcEhS7HKO+wHHKkKqlRKm2Jst4K4JrqIZQyiZ0zcEDDZ30m07il3dXdTbIPG0FuAmNtIAo82grXOhOuOhZ2PMhWgn9BwBeQBk3vaT/Qvw5H8giegSaG9YIMqJIN5p5eb6B35 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Apr 2017 15:54:07.2642 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR1201MB0132 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2969 Lines: 92 On AMD hardware when a guest causes a NFP which requires emulation, 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); -- 1.9.1