From patchwork Wed May 4 19:09:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Suthikulpanit, Suravee" X-Patchwork-Id: 67172 Delivered-To: patch@linaro.org Received: by 10.140.92.199 with SMTP id b65csp357950qge; Wed, 4 May 2016 12:11:49 -0700 (PDT) X-Received: by 10.67.13.144 with SMTP id ey16mr14316795pad.147.1462389109713; Wed, 04 May 2016 12:11:49 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p21si6168923pfj.140.2016.05.04.12.11.49; Wed, 04 May 2016 12:11:49 -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; dkim=pass header.i=@amdcloud.onmicrosoft.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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755509AbcEDTL0 (ORCPT + 29 others); Wed, 4 May 2016 15:11:26 -0400 Received: from mail-by2on0099.outbound.protection.outlook.com ([207.46.100.99]:58496 "EHLO na01-by2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752610AbcEDTLX (ORCPT ); Wed, 4 May 2016 15:11:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:To:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=PUSLsvK88BmyfKmtZ8LZEnAvkP+0w4lZAdxs5MXDfrQ=; b=dQEBpQ1robraecTlyIywikWZW5nT+EVxcTY+6i6LUv0t7YFK1R7ugGZjlRN0xFx717otJbibX0NQdAKrIhJu0yeU+C0YySg4ycqwBU90vSu/cBbUhuBBKXALTF8kkHw3DeFDV8urzR23bEaBgk2m+zjuzn3SNS+94D7e1zWM2w4= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none; redhat.com; dmarc=none action=none header.from=amd.com; Received: from ssuthiku-cz-dev.amd.com (165.204.77.1) by BLUPR12MB0433.namprd12.prod.outlook.com (10.162.92.139) with Microsoft SMTP Server (TLS) id 15.1.477.8; Wed, 4 May 2016 19:11:18 +0000 From: Suravee Suthikulpanit To: , , , , , CC: , , , , Suravee Suthikulpanit Subject: [PART1 V5 13/13] svm: Manage vcpu load/unload when enable AVIC Date: Wed, 4 May 2016 14:09:52 -0500 Message-ID: <1462388992-25242-14-git-send-email-Suravee.Suthikulpanit@amd.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1462388992-25242-1-git-send-email-Suravee.Suthikulpanit@amd.com> References: <1462388992-25242-1-git-send-email-Suravee.Suthikulpanit@amd.com> MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BY2PR21CA0014.namprd21.prod.outlook.com (10.162.74.152) To BLUPR12MB0433.namprd12.prod.outlook.com (10.162.92.139) X-MS-Office365-Filtering-Correlation-Id: 6b5e8d0a-b8bb-4d9b-5895-08d3744fe040 X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0433; 2:xEnukizmmNdbXICgsrFXV0p71jF7rg1LCXrHqN/P63LLzdDhFBk0PPD625SzCB7AB7rC86ovjTfnEVgoq5meruZcrVAXd1kx2ji/3ZAbLANYZEssZGW+w/zR4cVVSEhzbFATbGcT2zEDkjJT7rlxaCh35s+Q4chhdXgpFywTCHf1C5MoRu+VwbNlePkuGZV1; 3:+N5sQnjh76sr67+lm+kco3YrERi3NIRord7O4vf/TBeamuAyRpk99qU1Nw0sSZSdcSlCoLjL0N9Av4gNxmSrOvfDmvE1JzqAzlLlWIjhFG/ESP66lnR5hzzK/nOcwBAZ; 25:VounKzJAiaaLuQT4ueNFmTr2xz4+tM3fVj3glZCT1Ou7+aFG5iTHIMr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G7mp2YKhd+clEb2bhLxSK8tT9tYxqvXRnG0Q/KxPxOqPtzbD47PwDUXvc2EUdyH2Y1mcHXGyKOeC6267+8tvjXOKl5Qwq6rW0/DLsezZKhTONpadJWV1eQe1CCWWTzA7JsGZikCxgZN1c73wnTN6duYVKjuAorim2zh/rDszJcXTxMxrJAS9moXy4YRNIV9N+X/LKIcQ3EaNzUsrHbwyQKdSqlsJrbIX2amhrD5Hiu3c/3bOkieg0z4ahiarDbWu/HMrKr1irvBanvzHX X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(9101528026)(9101521098)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046)(6055026); SRVR:BLUPR12MB0433; BCL:0; PCL:0; RULEID:; SRVR:BLUPR12MB0433; X-Forefront-PRVS: 093290AD39 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(50986999)(76176999)(50226002)(2870700001)(47776003)(66066001)(229853001)(36756003)(189998001)(19580395003)(19580405001)(2906002)(2950100001)(53416004)(2201001)(5004730100002)(92566002)(4326007)(23676002)(5001770100001)(77096005)(586003)(42186005)(5008740100001)(81166005)(5820100001)(3846002)(6116002)(86362001)(50466002); DIR:OUT; SFP:1101; SCL:1; SRVR:BLUPR12MB0433; H:ssuthiku-cz-dev.amd.com; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0433; 23:KxPEFcGZp7FO3EKfOQrb7HHq3n0xRf/uHz592p3O9jSlA8O9aYBm+83kihR8OG1uQl4Z+6/g/ORMPapRf+hwzMOduaNw6j7bxsTE2YXVkdGFX7g9hpKnx27vdM+BuP7+QhNc5xTy2cVmipQp/i6M3KKsM4iURB5OD0UWwXXMyXwOWM8AyA95ZVtS2BU0yHYTKAifqynD6L77wt8KAT3ReaaPhmKfmRBEDLqN4MfNIbF4aeyX1S3RtLaIrIC7rD8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QUxx1PVyiCP0EDQic0aYM3GURFEYs9jAzVciAngvGFJfrxryxoZRHExYZxbF1FMFcxnR0=; 20:15L0SgMWigsnwtArRorJHI9CYsJ9lZzim4xALbgqCEI0YFoe3aqByTWKEan3VsHa3YugGSPfqpFHzwKncACPzfimKx/llSD6T48817goRKigOTLbjQpZlIr8coZ5XHP32gra8m+rfuLBW8hr8Bx4XbGkGCuGWb5poXY1gUYPWSFKhDr2llymp4Fk7ZAQMnPPulYp0JfMX4VmvXdYds/Zku4kxQUGprarXehns9KgZlHNSgmycOcELBR5FCb9hXGd X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 May 2016 19:11:18.6360 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR12MB0433 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Suravee Suthikulpanit When a vcpu is loaded/unloaded to a physical core, we need to update host physical APIC ID information in the Physical APIC-ID table accordingly. Also, when vCPU is blocking/un-blocking (due to halt instruction), we need to make sure that the is-running bit in set accordingly in the physical APIC-ID table. Signed-off-by: Suravee Suthikulpanit Reviewed-by: Radim Krčmář --- arch/x86/kvm/svm.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) -- 1.9.1 diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 721e514..f903d33 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -183,6 +184,7 @@ struct vcpu_svm { u32 ldr_reg; struct page *avic_backing_page; u64 *avic_physical_id_cache; + bool avic_is_blocking; }; #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF) @@ -1315,6 +1317,61 @@ free_avic: return err; } +/** + * This function is called during VCPU halt/unhalt. + */ +static int avic_set_running(struct kvm_vcpu *vcpu, bool is_run) +{ + u64 entry; + int h_physical_id = __default_cpu_present_to_apicid(vcpu->cpu); + struct vcpu_svm *svm = to_svm(vcpu); + + if (!kvm_vcpu_apicv_active(vcpu)) + return 0; + + /* ID = 0xff (broadcast), ID > 0xff (reserved) */ + if (h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT) + return -EINVAL; + + entry = READ_ONCE(*(svm->avic_physical_id_cache)); + WARN_ON(is_run == !!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)); + + entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; + if (is_run) + entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; + WRITE_ONCE(*(svm->avic_physical_id_cache), entry); + + return 0; +} + +static int avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu, bool is_load) +{ + u64 entry; + int h_physical_id = __default_cpu_present_to_apicid(cpu); + struct vcpu_svm *svm = to_svm(vcpu); + + if (!kvm_vcpu_apicv_active(vcpu)) + return 0; + + /* ID = 0xff (broadcast), ID > 0xff (reserved) */ + if (h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT) + return -EINVAL; + + entry = READ_ONCE(*(svm->avic_physical_id_cache)); + WARN_ON(is_load && (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)); + + entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; + if (is_load) { + entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; + entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK); + if (!svm->avic_is_blocking) + entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; + } + WRITE_ONCE(*(svm->avic_physical_id_cache), entry); + + return 0; +} + static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) { struct vcpu_svm *svm = to_svm(vcpu); @@ -1378,6 +1435,11 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) goto free_page4; } + /* We initialize this flag to one to make sure that the is_running + * bit would be set the first time the vcpu is loaded. + */ + svm->avic_is_blocking = false; + svm->nested.hsave = page_address(hsave_page); svm->msrpm = page_address(msrpm_pages); @@ -1454,6 +1516,8 @@ static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) /* This assumes that the kernel never uses MSR_TSC_AUX */ if (static_cpu_has(X86_FEATURE_RDTSCP)) wrmsrl(MSR_TSC_AUX, svm->tsc_aux); + + avic_vcpu_load(vcpu, cpu, true); } static void svm_vcpu_put(struct kvm_vcpu *vcpu) @@ -1461,6 +1525,8 @@ static void svm_vcpu_put(struct kvm_vcpu *vcpu) struct vcpu_svm *svm = to_svm(vcpu); int i; + avic_vcpu_load(vcpu, 0, false); + ++vcpu->stat.host_state_reload; kvm_load_ldt(svm->host.ldt); #ifdef CONFIG_X86_64 @@ -1476,6 +1542,18 @@ static void svm_vcpu_put(struct kvm_vcpu *vcpu) wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); } +static void svm_vcpu_blocking(struct kvm_vcpu *vcpu) +{ + to_svm(vcpu)->avic_is_blocking = true; + avic_set_running(vcpu, false); +} + +static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu) +{ + to_svm(vcpu)->avic_is_blocking = false; + avic_set_running(vcpu, true); +} + static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) { return to_svm(vcpu)->vmcb->save.rflags; @@ -4883,6 +4961,8 @@ static struct kvm_x86_ops svm_x86_ops = { .prepare_guest_switch = svm_prepare_guest_switch, .vcpu_load = svm_vcpu_load, .vcpu_put = svm_vcpu_put, + .vcpu_blocking = svm_vcpu_blocking, + .vcpu_unblocking = svm_vcpu_unblocking, .update_bp_intercept = update_bp_intercept, .get_msr = svm_get_msr,