From patchwork Thu Aug 18 19:42:43 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Suthikulpanit, Suravee" X-Patchwork-Id: 74191 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp70107qga; Thu, 18 Aug 2016 18:17:44 -0700 (PDT) X-Received: by 10.98.65.81 with SMTP id o78mr9378154pfa.48.1471569464002; Thu, 18 Aug 2016 18:17:44 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d63si2096977pfc.34.2016.08.18.18.17.35; Thu, 18 Aug 2016 18:17:43 -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 S1755006AbcHSBRR (ORCPT + 27 others); Thu, 18 Aug 2016 21:17:17 -0400 Received: from mail-sn1nam01on0086.outbound.protection.outlook.com ([104.47.32.86]:25383 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754934AbcHSBQx (ORCPT ); Thu, 18 Aug 2016 21:16:53 -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=zKL/POCdXWqGiur66chUXusd9Oxm0ATWuX4hPD8Yb7o=; b=kPL/E4UNUQadO+eXjmGYkr6XSre1Et0ilpfccwwsVRic2i2Bs7bSm5ZIBTQIzTGpd5nlMvAyVOiRXhUysKhajeDtj5cFTXBqS3k0wmqYIO8zthbMgglSwFbpN/zb3iLi9lU2yhMzgvSIVZQjay0lR1px+8jQBLspvAFm7mMrNJw= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Suravee.Suthikulpanit@amd.com; Received: from localhost.localdomain (114.109.128.54) by MWHPR12MB1453.namprd12.prod.outlook.com (10.172.55.22) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.587.9; Thu, 18 Aug 2016 19:44:04 +0000 From: Suravee Suthikulpanit To: , , , CC: , , , Suravee Suthikulpanit Subject: [PART2 PATCH v6 11/12] svm: Introduce AMD IOMMU avic_ga_log_notifier Date: Thu, 18 Aug 2016 14:42:43 -0500 Message-ID: <1471549364-6672-12-git-send-email-Suravee.Suthikulpanit@amd.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1471549364-6672-1-git-send-email-Suravee.Suthikulpanit@amd.com> References: <1471549364-6672-1-git-send-email-Suravee.Suthikulpanit@amd.com> MIME-Version: 1.0 X-Originating-IP: [114.109.128.54] X-ClientProxiedBy: KL1PR0201CA0016.apcprd02.prod.outlook.com (10.167.53.154) To MWHPR12MB1453.namprd12.prod.outlook.com (10.172.55.22) X-MS-Office365-Filtering-Correlation-Id: b1deb204-f19f-46d1-82ad-08d3c7a00456 X-Microsoft-Exchange-Diagnostics: 1; MWHPR12MB1453; 2:GgzBUCt+lkpEt2YY/aMT9UnS/UJdp3ZWTnBJqwbZkDdMuVsUQiSqK/Q6RX0pT2g+Hbdaqgkah6rE/MRe2wJ8YtzmkD57OMou7q7qlwgRnm3f8LodXfE3hn/Z3Q2EcscEakGtjgZyphydIibmz1XQLO4gj1ZOfk/9W3aPqge90eZ0SMYy+LnVXvFxibdOBpuX; 3:F9Hq5pch24tkfOf83KGFam3PPvcqiTeex/x+oHM3r48IzNxFz+qWaIDIQ+8tUgvW80+/Xe6fQL8wjPSmUQHbUCWSFqm5vpBMV0aiY6JNCP9CIHXqr4zaww5BUOhaeSxe X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:MWHPR12MB1453; X-Microsoft-Exchange-Diagnostics: 1; MWHPR12MB1453; 25:H9P3H63852FG7W1jojQz+Yh01vN5SUSV1umJSTY39GvMymWLFdJyCiQsDb50FlMrPmH+8GHKBPar468w2I+agEnaZQUZ0E7LsMaTpnZ5xn+t1fkBQfodM2FtKssQXsLLNwCtSL3V1Jfi0yObwLiBpiK05UNpynbYLMdTCnMFIe8NGf1nbsFMDP1OU9ZmP8rU8qCuVPh3NpO+UOMLGuOXsRE3YRrgbTqVEsF881IvJk4Qv9qeY1fLbGY50AdGTh1DCdp21pfJaZJJEfrQTwKQg+solbDROOwLT46fah1Ycl83cr7tQ6FpOOVp86Cn85Cnrg3xklzgyLBY743qNyEKK+MZSkXqzACpc7q1sUDlLzlJCzuU3HlQZ+UVFjG7oFy2tUlTQaxRbnmY1hz1CpSGk4G2cCDatpSgjLLaYQBzOIWN40CRyWmVaBi3g6JafV+omJGgkAaVb8fwFJvuONtT3aufSf2UsDU3JhQMK+rtyPMETGQYwDnqBwKKew1tANyhvP6HSCJ23i2R7gaKzk6E+tkcuiocNMzbgV5c8szBtFpSQKu1iurQBPmGwwO705A3ORvECrIdg7ATvM5NfFGvhTKvkfodQteV49EJMUKCyQwKgdJjxrr21s18IKVcooVEVUavZrzXNGtqDA0IiA6FwMu4zMEF1nwqwncuOuvyPO3OuhwtmkOXgF5BLUne/oQ0FcWwDESULrypmYiZZ6BmQnvNOMn7cKnYS1ifLgJHCbUI313MRuIwXtCQo0r+TZQh5TS7qKk1MUgzfNQPv4+VrQ== X-Microsoft-Exchange-Diagnostics: 1; MWHPR12MB1453; 31:KrrOKafprGP2SzTBJOUDkvyn9p8ZQY0EjgEUeupAXqbYfg8JE1iWTTl8qK0xHgQquVmXC4VhNmsyuikuRKphI7oauYCGgdqkocR9lor9iJQ64z9uz+7XbFh6MiAEAwitRIhNuJ8OkwOgSKafvTO2gvyavoAyRi506d0BKT1AKbJ/DrDocAjJN3rr9K02h1mnsbuGW8u9quLOOuQWF/ycH1PqsvryprHH1ZTPdgpgt4s=; 20:uwAPRoNhfbmOWGC+MIgYw3MPu4ep/BSh2Fnic/GNLEkh4EAnI21Zp/ODVb8EsfynIDZdQVZrJy5505D1r/0YiKz0oE4wcOjhwnvn8b/2qbrlWN9rW7RwNPJisf67HZhMCbtIq/By1j7ToQYcbVaGUsbaJ3z4u23oCva8t/XCb+0AniNqjjXMogdAxDSwMy8cThQ7F878fmzRElu1WYPZxFqO9gfPaDc0H0Ut82niv+aVXf7tjO+XYqqztYE15ofGkRVMIBa3q9CLkoReJsokvxA4t2L8euo1NLuDNB/wcwoVpDc7K3khLMnDeo2enbFW9XviJDh0QLW2zRFL7i9tba6UFddJ3P4Tc6nY+EWkyl4bZDrQ9ImerHNAjRilAUxIPmHBRmo3V+rW80oROA+zcqZv4TXFPTz4bb/ZApCP7miMCTeDPwtJ2SFYxhBmx36nO5lns59YpNokl2R8alPnkxF1fQQ9dSJ45yfSf8JUAXqKheOobxRXtiliru+trC6L 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)(8121501046)(5005006)(10201501046)(3002001)(6055026); SRVR:MWHPR12MB1453; BCL:0; PCL:0; RULEID:; SRVR:MWHPR12MB1453; X-Microsoft-Exchange-Diagnostics: 1; MWHPR12MB1453; 4:SzkUJk9atvVmrsDf/vxuhCwiSuMvJuzcbZcSEbbt9lVjP+DYKsqzlINHvMS6jUFSZ9GkXbCbS2noldg1/39L/cexVQo4XCEd3ZrnoWbQZbU4AZ+jGmRQDmErdgRQ8aH6Txew1x8ssP0tdmyxCiPXGOwLMpvtI4AAlJLxz7t7kxTptR4D0vd151s409kCsmmdg3A2K+vMIDCVjPEtc5l6Kn+F2ezFLJP3UMl3M1OOlV/bWchj7G2TM6XFgXbwyvwDS0IzYFlkdGSlE1B7bwvk6E5nRTgZFKW9uTwknYEpeCygt8cLvV7kObVbgbukwp7MZEAvR5krDYaP1m6SW/K9N+t+mYWZpHKUJkmLU94gPEaD80pF8nYa3O0rB5o6NDQw84zBEDQMCfJ8VUfzmV9VxcwwUNIbANXcJMkYgWVd6Qqn5keohvyY5T/QC+taAPFQ X-Forefront-PRVS: 0038DE95A2 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6069001)(6009001)(7916002)(189002)(199003)(86362001)(42186005)(50226002)(19580395003)(586003)(5820100001)(68736007)(92566002)(2870700001)(3846002)(6116002)(189998001)(105586002)(8676002)(81156014)(81166006)(47776003)(5001770100001)(2950100001)(97736004)(77096005)(50986999)(23676002)(50466002)(106356001)(36756003)(2201001)(305945005)(19580405001)(76176999)(7846002)(7736002)(4326007)(2906002)(66066001)(229853001)(101416001)(217873001); DIR:OUT; SFP:1101; SCL:1; SRVR:MWHPR12MB1453; H:localhost.localdomain; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtNV0hQUjEyTUIxNDUzOzIzOnUvTUY5S1VOQmk2dWQ4alEwdG8xeFhnV3g5?= =?utf-8?B?WG0yNzdCbnExT0RYUVBvVDZoanowNEtZTkcvYnVzYlIxaU51MytmTDgrdWxq?= =?utf-8?B?NGt3VEpNeTVoWml6dXlDYVRiaC93Z2lpckVrQUdzUlVUSHVNZTFHdm96Y1ph?= =?utf-8?B?NjEzSUQwcWRkemh1eEkxeUhzOGJ0UUNNMUdpd2o0d2E1VVcyNjVVWHUxSkVW?= =?utf-8?B?Z2ZWU2F3Tm9wQnFRQ2llWUxMWE90Rm9LS1d4L3YwVTZ1SzZibHdqRllleVd6?= =?utf-8?B?ZUVKTkV4NFRDbkhhVXgrSTFiTlZzTzZPbzhka3FzQWFzeGtsU3VjZzlvMW5Q?= =?ut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O8M2ZOnbw4lDvB8IiUVAZmoMQcSQxIXRPlDGa2mM6CRJMUNq9HkautXDKDEPbW2eG4MYpA1NcpAh5lDHz9u/eZCxzZnpxtM3knwYc6+7Hfe3+0g8vRaC2uar3U5fUViawoZFl82rK6lGmjkwMkXIGpmIChgNl37d/YJT8OBwidY5kBPvKYCR9gmM SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; MWHPR12MB1453; 20:ApNVhc+AhjqgVTolCUpMBI7X7rK+6kle3G/jEPL6MY2o1MK47xhNUph6eL+K1rIID0n6LCheXJgE2FoGZKzM/D2nPA3KATmrDzLnOXcKFWmC1v1+8i4uBQsGePahAlBGyuw6pzWS6Xqgn0NQFpkN/MwjJwOuOoEe+YlxgVjnldGr1TM5B+Ko4Wm18JcdftCXmB0/6wq2LzeMlhXpJ/OJ9XWbnVw0CpN4rk9Z3FODUbe9eoiRJyA/JrRkGbhr8L1S X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Aug 2016 19:44:04.6124 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR12MB1453 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 73 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 72 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 88acb41..c060e05 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,58 @@ 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; + struct vcpu_svm *svm = 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; + + svm = to_svm(vcpu); + + /* 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 +1057,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 +1386,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 +1395,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 +1432,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: