From patchwork Tue Aug 23 18:52:41 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Suthikulpanit, Suravee" X-Patchwork-Id: 74519 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp2253920qga; Tue, 23 Aug 2016 12:13:59 -0700 (PDT) X-Received: by 10.98.157.154 with SMTP id a26mr56121324pfk.68.1471979639842; Tue, 23 Aug 2016 12:13:59 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i67si5154788pfg.136.2016.08.23.12.13.55; Tue, 23 Aug 2016 12:13:59 -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 S1754624AbcHWTNw (ORCPT + 27 others); Tue, 23 Aug 2016 15:13:52 -0400 Received: from mail-co1nam03on0064.outbound.protection.outlook.com ([104.47.40.64]:26016 "EHLO NAM03-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752864AbcHWTNs (ORCPT ); Tue, 23 Aug 2016 15:13:48 -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=gVDRFXlwO/4qjq7TBftUN83HcaIo1RhrBtpDxVNgwYI=; b=2TTo0KMObseWx6TyoyR81pXLNpaZ/B6QuxaeVU3OZ/8plG43jP9eFtEMnx8VAcXbltKiosJnWXO+kGDbnpDRJDkL/j1ruyg7hEd6GFYup9KpzKUIf7pKNaialAewxkflLyjim1S0QoQaG42DVQhHOkeJvL4mfLIHoWRd0kX73Us= 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:39 +0000 From: Suravee Suthikulpanit To: , , , CC: , , , Suravee Suthikulpanit Subject: [PART2 PATCH v7 10/12] svm: Introduces AVIC per-VM ID Date: Tue, 23 Aug 2016 13:52:41 -0500 Message-ID: <1471978363-13756-11-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: e53a8850-fc4a-486d-9937-08d3cb86cd8e X-Microsoft-Exchange-Diagnostics: 1; DM5PR12MB1452; 2:WMqLpzytVzT/mKvce7ThxACHL3k0B1kctr7qg1gjUq4I15/6LP17iM6lvKO58JZdMox4BQGggrlpv+4opQPsI1tF4ZESwDGyjGbLbf9MYZ6zsWhTmB5/WOeFn6e9815Ef8GU3mi/ytj6EuYDJkD2MwV7Tse07dcq41bC62e9m+rc2aU0bxjMztpHFZJj9WOU; 3:edd83jPFN4bz0axEWbOn18zTTm1hR5MTr6G5QHYsMZk1jRk5Kx0aMN6h7SrkzdEVwKYYkLKNU4Pf1eByDNA6p/Qk58ql9zBGgfSy/vonJiLqOthUvcexj1xtxeYBSoIw X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:DM5PR12MB1452; X-Microsoft-Exchange-Diagnostics: 1; DM5PR12MB1452; 25:HdOuFReDZ5JGLccZ1YumVvRoo7+/2gpKbcJEyAi7ML2AZF8JUB6A/5SJ9FNzhpZDfaOvXup/LYWJWm+eMux75JQIWl3R3ONI5hDj9KW8okrK2MUtv30ILr4geoAbuRzStBt7706bOvnU+lWqupM9m4OaTAkehB84j5BD8UVFakqRDMYr7f6DhwfGXbr3ygsf6rWRgC2cMBygsWlX/iopS3EGIw8E34Jq9Y9Bhcx7bMhVlRCnjexSBTQxSUIvdVIYTi+k/1corEcKoAZlJU6sCKMyQVBSQgmSqeocA7k0565t4cDilZtKs5X4faEhSWMs1Ej5znoLREgMpotIO8+mJwi9WHXaKLbNTbseEU+9h8nZu6XZLTjrNEpCz8nuSi0CLTUs7CeWS1khvoJtn/3QszO0rfOVTmr2OVDxalqurjljvwrc5SfkUKR4Kz8wkiHJ7fjWJljsbSxZAkEJmCY9mHGgeIXE4/pfj98JWvrnzGJdJW6rPrLo4pwHTmGVqLFX5bnucxQXuZh+yu/fr9D8dzxG7oq1ykBPnPW3ZHFKpbCIK+SEWquNxv9dqTl1/6waHMtOSuB9wkDSJTnDoLyN7d+wSqag+2P6XLjw5JqI0eFs419MmI+diL+AtKgNo8VjVnoqkhYNpnXof9/DoOsV0GqzSDEfmnLDUbekIUNE/A+Gw8MEz9/Q2doz5v37RBL9NnVdafTh9Uy+vLqEofjmms6sjAMcvFr9W+tVYh3fGoV428pWdwZLykJPRwRKeSOm X-Microsoft-Exchange-Diagnostics: 1; DM5PR12MB1452; 31:fkBhQMXxseKOYgIRiaiEJi2A1tP9JNx2A9rGBQ4kRcZmZNOUgzdATGloD21BMH/HyaBUQegcv32clXjRH7YUd77sbByDy9zwl3YMkQT2AAYHh4NxnPQalzzWFCjnCtEw4b6doLF6M6YMhygREEPlBQGyCeZKnfYiaWaUst16llkc3xMmCZEgDfXYOt5/FbdBS0qzzcH6k9UrXPlwEUJBgbdCHEEcgKg5NY1qg/DytIk=; 20:I9pgA4riqikSB0AvlC50+jhIz0dKZLfRoZaN6ljMQyYcZzboyPbvtZFbH73v+8i9k5blIxjOUm7EFsuB0i5Kx7icUcUouWRUdtDlh1ptgeDUOrwW5V/da+z5mdFe+4ARX2+7pVDh2Tjhr3Dp++m/jiO+9sGG33JHDPNMh4GPmSP1rPEBDjYMsgRpe0CWUMrGd0UEej9OXEKpDl5EMUYj4W7QjV/aB5GfmQ25xeUgDOKKwfxOnLWyxLdHhYIFe/OAaHsTHeX6i793MKL3Mq2bHyWxiK0JsDcr9Q0eK6qT5HSnxIu36zjoCm1BdXwVY7bmMWK9+6nUplTP4802fx6r/XhHJ40fInKYvP9wQ0DpZ8yGmzvepcrSK6vreXHIXhEqZ6j4V1+5pOA9zRIefq+PJ+VS7UMUmFA5z0orWh81T52dVXrK+BL/fCTJIHzkATXWFQRG6aSywrzFQdk89f+mhEbJpFuIdZQAFcJXt5oBLfobnfuDa+kaiPVy3qjz+ebN 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:Sq4/2JpinhMdKgoLQgO1b8C1ZR7LQrJcQQpnt6Ots4NiXZNB23BCDCmhkX0Sbf+ygOk+6EzJq2ELwh3+b8N6fMQwV+TaxWrUkFT4CY7giQi7OWw2vGKUvIopQOsQPlwYGkZbBcHPiahnt/O7aVgVCJQ8SZQbjr1Hey0kBUdEHkxBzMFyb2yHm/MS5D7ZGHISg9G+t0rgEfBCfmA5+KmS5xBgqpwdzdYsIabdHDRR4w0SuyE4f6qyoPMg4QyesqKIN3nw76vA1R07OLbJCuovKjOBaoRzzOy0sbWfxAHpeFEohi20u8eaVSaOQ5X2zmtOyFN8fRq+MbCg8SxyLtNAkFbSTp34RiUzIKL/VRwzvDTONd6xQ8aD1kRFkOzAH8TRHvhstyIzKeE9i0k4cOFo2jn+yeSBxZaLhm12cbMC1GgmDdUw95dEZc/zu10FV3Xa 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)(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); 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?MTtETTVQUjEyTUIxNDUyOzIzOkFpNks2cHdET0tzWlQ1cnloVFFzMHFseWVU?= =?utf-8?B?dUhDaitLOHFyd0FteHBIK2NtOWZBbkhXZFVKdFJTMWp4eU44NllaUGRrYVV5?= =?utf-8?B?TnBNU1dWdHZDREdiRHU1ZExaQ3JUYzcyM2NRWll6NjNBejJmeXlRMDNRSnhp?= =?utf-8?B?RXRlTEs4U1NXL0g5dWxTS2VsZnM3eG1QbVBSWFNSYllaV2I1QmFSd2RrbUs5?= =?utf-8?B?ZGJLSS9hZkl6WXRVTUtaWU9FVkVaVUdvV1V3VUt6MWtVL1RkY01JdnN0cUxV?= =?utf-8?B?dVl4NWZHcksxclpwQlkwZXFxcjZPMFVFV0c5UVlsVTJWVG9jTFZESHdUREJF?= =?utf-8?B?TnIvMm82REc5dGNucFJsVWpXZUlFWn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r30nGiOgqXz2otZfSTc9FuYSx8IrfLtv5J3i/KZTFVxzcgmslEOP7pNkIdLIrVTUvlHB9QOgvsiLcWB3rjp9Ch3LcLogmjPkvUngy2kRkb+MRYPhWOq9zoj5WsAZM6obKVuDGVrip/me1uKnjjWDW SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DM5PR12MB1452; 20:bo9YD6dXvV3flM90HWzEjmOBIFQqzcJzckN8GB5wg+ax5K9F/objmb+U4VDxcP8d7LHFxQ7z2LbbgzvY1GA4XkgWr3s9lmy/TF0ww/Wj7cn8G3/mRSllDiMwtYwNFzTXa4QWSZc79QR5RQmdtbkzgcpPdd2rkbwtBgaKGnKHkXfewuK+zZKGvDPVsdHzMYxBDBOpDZKbJ5mTc4NlZgtGbLHr+w8P5i2dwzBW7aOqrW3L+lAmQWam6ECnWMQtGayL X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Aug 2016 18:53:39.5058 (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 Introduces per-VM AVIC ID and helper functions to manage the IDs. Currently, the ID will be used to implement 32-bit AVIC IOMMU GA tag. The ID is 24-bit one-based indexing value, and is managed via helper functions to get the next ID, or to free an ID once a VM is destroyed. There should be no ID conflict for any active VMs. Reviewed-by: Radim Krčmář Signed-off-by: Suravee Suthikulpanit --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 51 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) -- 1.9.1 diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 69e62862..16b4d1d 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -776,6 +776,7 @@ struct kvm_arch { bool disabled_lapic_found; /* Struct members for AVIC */ + u32 avic_vm_id; u32 ldr_mode; struct page *avic_logical_id_table_page; struct page *avic_physical_id_table_page; diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 16ef31b..a718854 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -96,6 +96,19 @@ MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id); #define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0 #define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF +/* AVIC GATAG is encoded using VM and VCPU IDs */ +#define AVIC_VCPU_ID_BITS 8 +#define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1) + +#define AVIC_VM_ID_BITS 24 +#define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS) +#define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1) + +#define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \ + (y & AVIC_VCPU_ID_MASK)) +#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK) +#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK) + static bool erratum_383_found __read_mostly; static const u32 host_save_user_msrs[] = { @@ -242,6 +255,10 @@ static int avic; module_param(avic, int, S_IRUGO); #endif +/* AVIC VM ID bit masks and lock */ +static DECLARE_BITMAP(avic_vm_id_bitmap, AVIC_VM_ID_NR); +static DEFINE_SPINLOCK(avic_vm_id_lock); + static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0); static void svm_flush_tlb(struct kvm_vcpu *vcpu); static void svm_complete_interrupts(struct vcpu_svm *svm); @@ -1280,10 +1297,40 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) return 0; } +static inline int avic_get_next_vm_id(void) +{ + int id; + + spin_lock(&avic_vm_id_lock); + + /* AVIC VM ID is one-based. */ + id = find_next_zero_bit(avic_vm_id_bitmap, AVIC_VM_ID_NR, 1); + if (id <= AVIC_VM_ID_MASK) + __set_bit(id, avic_vm_id_bitmap); + else + id = -EAGAIN; + + spin_unlock(&avic_vm_id_lock); + return id; +} + +static inline int avic_free_vm_id(int id) +{ + if (id <= 0 || id > AVIC_VM_ID_MASK) + return -EINVAL; + + spin_lock(&avic_vm_id_lock); + __clear_bit(id, avic_vm_id_bitmap); + spin_unlock(&avic_vm_id_lock); + return 0; +} + static void avic_vm_destroy(struct kvm *kvm) { struct kvm_arch *vm_data = &kvm->arch; + avic_free_vm_id(vm_data->avic_vm_id); + if (vm_data->avic_logical_id_table_page) __free_page(vm_data->avic_logical_id_table_page); if (vm_data->avic_physical_id_table_page) @@ -1300,6 +1347,10 @@ static int avic_vm_init(struct kvm *kvm) if (!avic) return 0; + vm_data->avic_vm_id = avic_get_next_vm_id(); + if (vm_data->avic_vm_id < 0) + return vm_data->avic_vm_id; + /* Allocating physical APIC ID table (4KB) */ p_page = alloc_page(GFP_KERNEL); if (!p_page)