From patchwork Tue Aug 23 18:52:42 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Suthikulpanit, Suravee" X-Patchwork-Id: 74521 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp2260416qga; Tue, 23 Aug 2016 12:31:55 -0700 (PDT) X-Received: by 10.66.12.102 with SMTP id x6mr55505187pab.48.1471980715283; Tue, 23 Aug 2016 12:31:55 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id zc9si5252423pac.27.2016.08.23.12.31.55; Tue, 23 Aug 2016 12:31:55 -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 S932190AbcHWTbp (ORCPT + 27 others); Tue, 23 Aug 2016 15:31:45 -0400 Received: from mail-by2nam01on0061.outbound.protection.outlook.com ([104.47.34.61]:56192 "EHLO NAM01-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1755111AbcHWTbl (ORCPT ); Tue, 23 Aug 2016 15:31:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=g8C9LckjK4WyyxKAe5W4DiBcnXZCPJzZjr3ir2K8rxE=; b=pvffATLbIgJrJh2qfW/mHxU5sE8NZG8772jpqaVBD7g0vvstGHDkH5siP/BQ8OFwUUCINiXdN6dum+BSp8BxcU70hvoVp5AVua8qWnO7fyp4mANcf3PzyDuAAW7OS6gP76O+gj+wmmOKzatZ8xNwDG6QCpe0y0omdh/54o1tt9Q= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Suravee.Suthikulpanit@amd.com; Received: from localhost.localdomain (114.109.128.54) by DM5PR12MB1452.namprd12.prod.outlook.com (10.172.38.141) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.557.21; Tue, 23 Aug 2016 18:53:43 +0000 From: Suravee Suthikulpanit To: , , , CC: , , , Suravee Suthikulpanit Subject: [PART2 PATCH v7 11/12] svm: Introduce AMD IOMMU avic_ga_log_notifier Date: Tue, 23 Aug 2016 13:52:42 -0500 Message-ID: <1471978363-13756-12-git-send-email-Suravee.Suthikulpanit@amd.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1471978363-13756-1-git-send-email-Suravee.Suthikulpanit@amd.com> References: <1471978363-13756-1-git-send-email-Suravee.Suthikulpanit@amd.com> MIME-Version: 1.0 X-Originating-IP: [114.109.128.54] X-ClientProxiedBy: KL1PR02CA0053.apcprd02.prod.outlook.com (10.167.54.21) To DM5PR12MB1452.namprd12.prod.outlook.com (10.172.38.141) X-MS-Office365-Filtering-Correlation-Id: ed0bbdee-9a0f-4130-8fa8-08d3cb86cfa5 X-Microsoft-Exchange-Diagnostics: 1; DM5PR12MB1452; 2:kbQIzBjufTDMyf+RwmH2Yca9BwDFMRBTDBm9mTv3zj20DzUZNzyeCBp1YTWRi2rsO09qTshc0FMBC0UoZP3ohG4UjjtL+QsSR4jcqzmKqOG9/OfJAVLEpE220G/c83eyuSxFIaoTKiu8JfnrxQbXIMsjiI+V9qojx6Es+pjYUd8jNIm3APjd4n8TgeUYCKjU; 3:AL0MsclJOOa/B40L6g2SDRWloGS5i+84dS+m14ESQ9SpZjfrfY/nOjUDh2/n1oSkuhLv9C2GH+FLcSfc1HHw+XMPNrO6lVjazrGvLrNqxY9zqYtg11yyg5lPrIV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GKS/LFRUhEpqCgPJa/d9dyk/wxkpZ2MdGk+M9If0r+CIt23zNsBrmwhbO7ysfnvvxq/MtPkOxhVNFrlty3bF9qRL4BJI=; 20:ZbvaZwQs8LkJ95nF0hxCm4LuMAX/JzL505oyLtjbJDJpdKc/3laSRSjrehVoFAQOufglEDp+yqie+2k2aGzkwDpAy01Lyw8SahCCdxSvLVj3S+9QawV0Pz6wbwE88klhY2mY7c5uLS6wtuqTPkyf+SmGaiI5Pj6gvKZ/1ZtpPlfxyty+vPf/QKY0/nw46uDPL7NYt8hZ1EBljDe7YqbxR9o3VTqpdlkdjNRo8l1U7FOfEsoANG4/vDb1sntOQ0PyDuzY7/VvuBbOPRAh6xTYCrjxpeP+uMgygB3yRemf2X9QRMv+petsZl4+8dgV14VlolTanB6Dbju5456dwjLVNYRkyYrq/U/PC5yyf0Rj30q0WmRKrzlc4cQ9oG4nkLWZObfwuOqKvhajcmgxSlDBb9eE+M07yRy+x1NTZ5f7r0FdR73j38nBg7ytn3TRBiR+M/1i/rqmvV+9lcIGct519XPegdSFhwj9GeG4Oc4u0pQI17k0ixu4icbqebJ15rIC X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046)(6055026); SRVR:DM5PR12MB1452; BCL:0; PCL:0; RULEID:; SRVR:DM5PR12MB1452; X-Microsoft-Exchange-Diagnostics: 1; DM5PR12MB1452; 4:iSbz4RyErYITGPTEkYilXDQGEfwmpTB+3Qgo/lTlS1G5ruvcTYJXGNAD1VQDKj2/mXewdTXgTfvacubVlpLjKQbLliBRz9Hfn2wKRjXprVGtsTFGxojtULsWRCq2iQQPGkxuWp2uBCjvDG7cQvQJcCnIAXrAFvXQhVjziskpQjsIsjy6bM2pC9ekPhx5G7Tehalw+zidpKSQMqz9heU+kUw1pHm25dd1s7o6V/Hj4cs9xQyxQIl+IJDG0/RvxZU80eWvcIJu2ONsNNs6kwfLNWnz2ANxz9zbBRh39KErHtymPQ6WUHwWsqre85aHI7f0PvfbLA78N8maOqwhl9kDdnPoTRlGM1/3fmH9Qap4L82Dcwn9H3/drmPUDZdSesG5MCqtLRh7QJkHYY12j1Iadh52vqHR61iF9jnlocI/JytfUglgxnj3QqFeknTjqeCi X-Forefront-PRVS: 004395A01C X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6069001)(6009001)(7916002)(199003)(189002)(42186005)(76176999)(101416001)(50466002)(77096005)(5820100001)(5660300001)(575784001)(92566002)(105586002)(36756003)(86362001)(106356001)(66066001)(50986999)(5001770100001)(19580395003)(19580405001)(50226002)(97736004)(189998001)(229853001)(3846002)(2950100001)(47776003)(8676002)(7736002)(2870700001)(6116002)(586003)(81156014)(68736007)(4326007)(2201001)(81166006)(305945005)(7846002)(23676002)(2906002)(217873001); DIR:OUT; SFP:1101; SCL:1; SRVR:DM5PR12MB1452; H:localhost.localdomain; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtETTVQUjEyTUIxNDUyOzIzOitzOXlFcExPbnMzNVVSeE9lMVphbmVUemRt?= =?utf-8?B?ZTZiM2lYME5udGFHbWpCMkF5KzVBUk8yMjNTTDlLN1VIT2pwaDZsaENlZG00?= =?utf-8?B?aDFvNFVNRngvN0MyK1RMSUNLZGcyQ0xrd1gxQjY4QlFjNWczcXZxOWVTdnAy?= =?utf-8?B?ZlhCNTdWTUdTVEZ1WFdxSEdweVFWSC9aN2tYTFNJMVpmRWc4YU1hT29hSXRt?= =?utf-8?B?VlAxWkg3SU1tdEhBT05JUEQyK0w0RmlwNGVRWmo3NzFQeDFSMUVkM05wRmhk?= =?utf-8?B?OXhZaklOSitONFF3WkRNSU9GZSt6SFU1MU11VDlJckJ6WjI5bEJNWG9Eb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OddHFM=; 7:ZU5RRVmVLrZAxL3upGUocXbLXIy29pcYJYtJ1bl3X4cCXvXC09YEBUcgKxmPcRCETBbyZXXMhFX9VbQwrA6933mgGvwjp184jrktwx83q6jo3ydH/DSTGw7Xm9haPpTyeQ1E1tbcslMJcvnyVHLgNsJVRteBqinWnJojv2NMHbFi9FOy4xjkyVzr1rQrdgHwjWZKt0uVWxX7tN8UBXWZZo3EPJhcQLhi1eLRcKNTgf4PpCkcdJ2DvjAyMQkIEc7A SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DM5PR12MB1452; 20:2TTaFF5aGzTeMcmCbuu6EZbK6vgzVlsYKOCJeJSzEXdTga5aO3ihlcHN8l1SEHLJ9D+3KiPhtZNgFof8WlRXA/RXQ3NCIshJPB+DMalryhEEFuvsG/OHWt3N3b5aZeKn/KO7XfxZ4tuf1jT3+mpXoSOtV8ofGGAodGzi74MNiPtTOQPh/zZxijLQZWPYP8lAdz0fHkyjwZ1jgzhfdP/R4HN8W2ErpNrMhiYoliORc9yFjZ3XiyQRGVUNMPxnsEhY X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Aug 2016 18:53:43.0227 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1452 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Suravee Suthikulpanit This patch introduces avic_ga_log_notifier, which will be called by IOMMU driver whenever it handles the Guest vAPIC (GA) log entry. Reviewed-by: Radim Krčmář Signed-off-by: Suravee Suthikulpanit --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 70 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 2 deletions(-) -- 1.9.1 diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 16b4d1d..a9466ad 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -780,6 +780,7 @@ struct kvm_arch { u32 ldr_mode; struct page *avic_logical_id_table_page; struct page *avic_physical_id_table_page; + struct hlist_node hnode; }; struct kvm_vm_stat { diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index a718854..8f87a0a 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -34,6 +34,8 @@ #include #include #include +#include +#include #include #include @@ -945,6 +947,55 @@ static void svm_disable_lbrv(struct vcpu_svm *svm) set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0); } +/* Note: + * This hash table is used to map VM_ID to a struct kvm_arch, + * when handling AMD IOMMU GALOG notification to schedule in + * a particular vCPU. + */ +#define SVM_VM_DATA_HASH_BITS 8 +DECLARE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS); +static spinlock_t svm_vm_data_hash_lock; + +/* Note: + * This function is called from IOMMU driver to notify + * SVM to schedule in a particular vCPU of a particular VM. + */ +static int avic_ga_log_notifier(u32 ga_tag) +{ + unsigned long flags; + struct kvm_arch *ka = NULL; + struct kvm_vcpu *vcpu = NULL; + u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag); + u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag); + + pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id); + + spin_lock_irqsave(&svm_vm_data_hash_lock, flags); + hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) { + struct kvm *kvm = container_of(ka, struct kvm, arch); + struct kvm_arch *vm_data = &kvm->arch; + + if (vm_data->avic_vm_id != vm_id) + continue; + vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); + break; + } + spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); + + if (!vcpu) + return 0; + + /* Note: + * At this point, the IOMMU should have already set the pending + * bit in the vAPIC backing page. So, we just need to schedule + * in the vcpu. + */ + if (vcpu->mode == OUTSIDE_GUEST_MODE) + kvm_vcpu_wake_up(vcpu); + + return 0; +} + static __init int svm_hardware_setup(void) { int cpu; @@ -1003,10 +1054,15 @@ static __init int svm_hardware_setup(void) if (avic) { if (!npt_enabled || !boot_cpu_has(X86_FEATURE_AVIC) || - !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) + !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) { avic = false; - else + } else { pr_info("AVIC enabled\n"); + + hash_init(svm_vm_data_hash); + spin_lock_init(&svm_vm_data_hash_lock); + amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier); + } } return 0; @@ -1327,6 +1383,7 @@ static inline int avic_free_vm_id(int id) static void avic_vm_destroy(struct kvm *kvm) { + unsigned long flags; struct kvm_arch *vm_data = &kvm->arch; avic_free_vm_id(vm_data->avic_vm_id); @@ -1335,10 +1392,15 @@ static void avic_vm_destroy(struct kvm *kvm) __free_page(vm_data->avic_logical_id_table_page); if (vm_data->avic_physical_id_table_page) __free_page(vm_data->avic_physical_id_table_page); + + spin_lock_irqsave(&svm_vm_data_hash_lock, flags); + hash_del(&vm_data->hnode); + spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); } static int avic_vm_init(struct kvm *kvm) { + unsigned long flags; int err = -ENOMEM; struct kvm_arch *vm_data = &kvm->arch; struct page *p_page; @@ -1367,6 +1429,10 @@ static int avic_vm_init(struct kvm *kvm) vm_data->avic_logical_id_table_page = l_page; clear_page(page_address(l_page)); + spin_lock_irqsave(&svm_vm_data_hash_lock, flags); + hash_add(svm_vm_data_hash, &vm_data->hnode, vm_data->avic_vm_id); + spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); + return 0; free_avic: