From patchwork Thu Aug 18 19:42:42 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Suthikulpanit, Suravee" X-Patchwork-Id: 74192 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp70149qga; Thu, 18 Aug 2016 18:17:52 -0700 (PDT) X-Received: by 10.66.177.7 with SMTP id cm7mr8988516pac.132.1471569471860; Thu, 18 Aug 2016 18:17:51 -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.51; Thu, 18 Aug 2016 18:17:51 -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 S1755096AbcHSBRq (ORCPT + 27 others); Thu, 18 Aug 2016 21:17:46 -0400 Received: from mail-sn1nam01on0088.outbound.protection.outlook.com ([104.47.32.88]:30848 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1755033AbcHSBRi (ORCPT ); Thu, 18 Aug 2016 21:17:38 -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=RkQUvD7/bTXFniSQSoG5PEuAJCrUToRh6XXSHEwz+No=; b=l+78ABYipqASyPLShfn6cv4Vlggrrzc8uZZsWA3o+zlDgh0tuuaBZV1v+0yWB6UaP2njT/4Op6Sx/fKeecIKeippVyN3MPNlq0l1hNE8rT/ilam/a1u6hEtcc+1pKZZ9bEIbSKckMUFM+TuJoTV0M3r9UY0Oh2IXLKV7xX39aEM= 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:43:55 +0000 From: Suravee Suthikulpanit To: , , , CC: , , , Suravee Suthikulpanit Subject: [PART2 PATCH v6 10/12] svm: Introduces AVIC per-VM ID Date: Thu, 18 Aug 2016 14:42:42 -0500 Message-ID: <1471549364-6672-11-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: 052d38a0-a368-494e-371d-08d3c79ffec5 X-Microsoft-Exchange-Diagnostics: 1; MWHPR12MB1453; 2:dgmXtugf6XmI7FSwM75P4MLBj301rx8STVlIyNhcQu0uFFMoRgs0kd2pbjpeDjFad6e4xnuk4nvt2LHS/dQoOwJf1zzvX2QUQSGu3Js+4JM6XnlMUKIbwdzaR5J7droJEHagwrX0JWYpjhHhRN71h79+cAeH+31HJYk6AxyOdscoK8ZjF17lthtTu96u0huY; 3:C0R4DG84zoqlLlLLhm1G34QIMQHtscjcA6OnFngHldNvl/Y229p2j2xZkbqTPCd4mVkVJmJbCKVQQiI024Apfq0KPL/a765y9jhb31Oelz3OEjuWRXb8w9Uf6WmWMa/g; 25:QJBZKfeoUflHd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zrYpcBNctgum619xeZw90swjTK1Caq1x4EzOg3FIakg7oHv2mS9B2lzlMw6CpeMdA1sqsMjeDIeBIoGB3OGlHusqngD9x6if+nQojjs1ZRxQEWcqJpCQQldNleiWFy+wthBZy8bsLAIfpEAbigKOd5cztkgxDJPa1uhBDMk6DHcdbGxJ 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:YlWmrRjHdmoGaWohm/fuu1DhML9hR84apAZBkljFKa+7o3RiUJaCQRuaDPunPy6Ozl7zghxGFHUj+Axfr4FVAGUTg9e9vfGHUmpmpw71buFQkWvRBv+1EJglMGsLXK7DQN/EocXgWMd/Pr6mOs8nxQPj5d2Trg9EEBr6t5qkJ57NiwJiJLNymRaWRMszl9M1okxGHLEAZKC6uj8Z0IIDVWvYQTUiPBNfmo79mdYoD1aXuPKNO3BYP1J1pfnmkoNijfte3FEaSHavDkwQnIRuZK+pJpQ3BJoPyaUxgaZ3KXVTXl/FtmtlDSqCQZS+h3tvsydNbIGC0vrB1YYRbY4Gsd7XUQ6a6Jwp3tmJEk8GE76yi6k7yatxpEdf9xgXdEnSQ0GluzIVI8+OpeEnRpRqBN9LF7UEyk69wLmCYvVOml3Cjo/QYCeSAjPDHtjwy4bS X-Forefront-PRVS: 0038DE95A2 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6069001)(6009001)(7916002)(189002)(199003)(86362001)(42186005)(50226002)(19580395003)(586003)(68736007)(92566002)(3846002)(6116002)(189998001)(105586002)(8676002)(81156014)(81166006)(5003940100001)(47776003)(5001770100001)(2950100001)(97736004)(48376002)(77096005)(50986999)(50466002)(106356001)(36756003)(2201001)(305945005)(19580405001)(76176999)(7846002)(7736002)(4326007)(2906002)(66066001)(229853001)(101416001); 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: =?us-ascii?Q?1; MWHPR12MB1453; 23:5asa9n063NZrsztwBaJGDBw3hOvWIjjUwO4XmN2tJ?= =?us-ascii?Q?3/czoQ7dJJMRNQHBKklo+jHPEAYKIQq4AnMoHz3zMVv/M8JfE2hoU6/WCEIq?= =?us-ascii?Q?+yHQX1ZT374fK+cF/qswDvLdcgP/vOeQ1og4Afph7YgHT5QoqTrmjZ25Rr0W?= =?us-ascii?Q?SRYCAAXj336WylwlZYBzUAIJ+1znIToKpmvIfdWVvK6wercStiaQ7/tz8pEP?= =?us-ascii?Q?OQpBQQ0Gwa9BnEsttVtLVFKHwmJoQkAOsGWssp9QJ6oEUID7AyhDZpc8HS7B?= =?us-ascii?Q?hPSkAik8rcCeOafYp0aWwTgHF13BN4UTaT4dDhdfPcTQlgj/D/TWJbxGJR+9?= =?us-ascii?Q?agbMJQGImxyHjKQUlXQxOYTiX9BSMNd5JogjP0tONhRevlFyjVZNY3Xn1TMG?= =?us-ascii?Q?6s6UFyzsJfG2z/+YFIS/WtZuRujiMzcIk/ZyflwI/H4/IohLBGa+dDZVBG36?= =?us-ascii?Q?KV0jL0B0+dLB5fszPBgKpHkyJHIcRWmltfLDTj0KPOfGGEP8pS88ov3Ngc6Y?= =?us-ascii?Q?MlPZyeTMJAdGDrVyUj1ZUcx7Iuq2b+U3omLm3ncmpcJG6EJ/+kV5wSbonVkc?= =?us-ascii?Q?7DrptUDEmssyY3O9pn708P4lPVR63xmGQmYBzan4AdVagBExPCd13vaGSlSq?= =?us-ascii?Q?FRjCJBqF9ZV7mA4BHAMLfG7P1GKomJh0zABwlQSjz+fUvSrP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1WfIJ1Fc52BFK+Fc4erA122sMRqQ9gpcBEyIMTW0s5rzwWMiV7hcuRuwKWnWiZ86ESwIw=; 7:11kZJt2lD36aNkYe2cPLxufNpx5LXVT2n20RTIJVMCTu7oHeXK8jxXunrbKba94EVc5MIHw0qt/CqPYIbe6SmudCpC7LFCBLqa0TMw4anvnj1ULc1nCi1zS0nMIRqSrN4VB3M+0mh1Pw7wnwN21v81mCg3oKOJdiTlGI8iFv1C9K5UVPqsRq7BMK83gVev/rGhvTaETELjHw9HaTwQvR1j4RgxwR5pVYYI53Tur+PLqsBwJ1vEf/9kKoKNVVljkV SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; MWHPR12MB1453; 20:NJ7LdAHih5G4wqIXdFf3WgRP12SLyBT2BU0ZeG/wQPzxQO/a5N5AYApI5MVuQaYoxpxUDxvsR60LZF7WceIlQO+9DTBA4QeIkeRrko0lW3LZKx61LRRr6DYQ6U3qqDbti6AgfleVsIWSdwHU40XxT4FCw8cqZdYxjfveJ1o2RjxeQ3/wA6AtUsAE2edKcGpNmqUhoLlHJ4feKajWcu0eRkiwvsNdju6YfOxAbmqrCNJjDx4cyGdOHdZvZga9iAqC X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Aug 2016 19:43:55.2746 (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 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. 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..88acb41 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_VM_ID_BITS) + 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)