From patchwork Mon Jul 25 09:32:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Suthikulpanit, Suravee" X-Patchwork-Id: 72693 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp1048357qga; Mon, 25 Jul 2016 02:34:25 -0700 (PDT) X-Received: by 10.98.25.8 with SMTP id 8mr27968820pfz.94.1469439263418; Mon, 25 Jul 2016 02:34:23 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z88si32773471pff.218.2016.07.25.02.34.22; Mon, 25 Jul 2016 02:34:23 -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 S1753108AbcGYJeI (ORCPT + 29 others); Mon, 25 Jul 2016 05:34:08 -0400 Received: from mail-sn1nam01on0076.outbound.protection.outlook.com ([104.47.32.76]:16256 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753018AbcGYJdu (ORCPT ); Mon, 25 Jul 2016 05:33:50 -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=06z7vpG2T7DmKVTnrs0PqgTrBn81LXvECXevazYAQw8=; b=NntIAjQnMsKsaWnQBfhj/NX3C8vEMONy3X79rca4OvlfUSyfMob1GAYlADLB/KKHK6powAu9HW7MVWNZj4FbpX5nTjOJ4dDdmoldvfq0IVMMtaZMkHpLhxZS/P5cxPwgGUMMJRTGkU48bGCM17ZThN8jXW9tKiBKiWhugy9Ujbc= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Suravee.Suthikulpanit@amd.com; Received: from localhost.localdomain (114.109.128.54) by BLUPR12MB0434.namprd12.prod.outlook.com (10.162.92.14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.549.15; Mon, 25 Jul 2016 09:33:21 +0000 From: Suravee Suthikulpanit To: , , , CC: , , , Suravee Suthikulpanit , Suravee Suthikulpanit Subject: [PART2 PATCH v5 10/12] svm: Introduces AVIC per-VM ID Date: Mon, 25 Jul 2016 04:32:09 -0500 Message-ID: <1469439131-11308-11-git-send-email-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1469439131-11308-1-git-send-email-suravee.suthikulpanit@amd.com> References: <1469439131-11308-1-git-send-email-suravee.suthikulpanit@amd.com> MIME-Version: 1.0 X-Originating-IP: [114.109.128.54] X-ClientProxiedBy: SG2PR06CA0066.apcprd06.prod.outlook.com (10.167.73.162) To BLUPR12MB0434.namprd12.prod.outlook.com (10.162.92.14) X-MS-Office365-Filtering-Correlation-Id: acdd82c8-d1ab-4a83-6cd1-08d3b46ebcb3 X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0434; 2:ivtBTuokjFM3JszHFGP2TjYkaml7OoFtPJ+5iYwZDuMUE2EtyH3Cu5hcfJ6jTGPKjsucSgjMLUJABXLJ/z/tdqKRSHGtpRZLIiUL3ogwZ5d9XxK+PoQVYK7xitsG3PPFdVAvINaxftvBsE+uKW6uvF5TaMWYlTbzxq1aFXdRry3BhVsc2u6ZfhdiCfurnudP; 3:TRmxniNMe5kabO1joFyGbg113DPjYHOebA6vKw3LA6wWqv5yZfTk4JPQIADZNmEBxMeaIPABz9dBt0JVOxcn2glTapEsebm7RaFtl9mhjhdMdUrnKfcFLUs6aAaH7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iNrfMpW23bjx/WrSxuO91KcQvvbfeoes6dG5kAg53A/EGOB7sfW2Qme+KfQOBpd+sKHeNowFAFLdejFL9kPDfySHxSpDrqumenKwYULhy2zaE0sjMmql6Vqqq3vy0An/qDvTHIFzSF8M1FGxigikxZj8Pgiq4pYTH5/T1uESuhHaEX/2LSAQmi3PV; 4:W2RO0gl1scXQ7OV/bKWknfTteX5feGvSnuXV8K6YhMgDeqE0l2R9AIWtnELQxO6+3xyxREnfvVPcL06NwMdgNC7VzWivjjSA7Rzm9q7TYphkiQvEnhDOOSHkAMm6XNg/6cLEg6ZFJP6hHaLXUzyg8TryQLvscKsS2mPBaDzS91wrrkXQ3fRynyFk4Qu0NG4l/lQcQwwKPGVFsq0rGtriY9OWCNYLfKKIYPHG8Hdezv3TIsyFSzSnZmjkNkD3MYLJa3n+R/bktMDrMT4o2yiMfuH5MqkieLpXiHEnAlqkPlSEmKuunBsFVQYAlgjE0DyyQcj+BrptYPZOiEUy/h89M59nqVxyBMRozXvz54moHa5PuFk997ToD1t4U4zEn0Ylv7G5qD+G3/raqAPcIcGeUfNnZW6+XDMyWYgXkKIlucLc5DKuACTMn6AuZ73Bdv1T X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046)(6055026); SRVR:BLUPR12MB0434; BCL:0; PCL:0; RULEID:; SRVR:BLUPR12MB0434; X-Forefront-PRVS: 0014E2CF50 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6069001)(6009001)(7916002)(189002)(199003)(77096005)(229853001)(50986999)(76176999)(586003)(48376002)(105586002)(19580405001)(81156014)(81166006)(8676002)(97736004)(4326007)(42186005)(5001770100001)(189998001)(2906002)(50226002)(19580395003)(50466002)(7846002)(66066001)(5003940100001)(68736007)(92566002)(86362001)(575784001)(101416001)(7736002)(305945005)(3846002)(6116002)(2950100001)(106356001)(47776003)(36756003)(33646002)(2201001); DIR:OUT; SFP:1101; SCL:1; SRVR:BLUPR12MB0434; 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; BLUPR12MB0434; 23:/vVXPXDv190Jl3bjaoq5RNx8y0j0UsYS23gdE3hDi?= =?us-ascii?Q?C9nPIi8l/X4nFUsiQL1NJ3oiowFNgrXscyAgSu3A9pTUhGFBAfxHldJKPHJI?= =?us-ascii?Q?U4hh5oZJNdmxHUhXeuGVsW7GUxrRJpP5x4ITZiVyb6nQrnhqqHrCREg6p4Bl?= =?us-ascii?Q?w9myrqmMfeSNgm2L/G8oT2E1SXzUKb0yuB+qDQ65RicL0uNhZuFWzu7oWpLc?= =?us-ascii?Q?kOYHOGOrLDaFbK618G41f0lGXn1L4sGUGXvU69RS9dGKk4j4Rz/oJRohAnIZ?= =?us-ascii?Q?TO3o2c1sxtlEWDb0P4sNHVX9FwJkTxL542dZXl5fm0F55Na3Fn8ICVJdtMmD?= =?us-ascii?Q?W3IIXjlKvoBEbhGjfKYLygwsFcW54Pnh2NTpJNi/YB/XV+qGsQACr9iR8gRo?= =?us-ascii?Q?V0MTCYWFbfml3tGo3MmhQxwwZp2Ci4bEDrWP0PnDzK21Hf8gG2/MB5xJctKs?= =?us-ascii?Q?v2c1f3Jut/W7QBc+tVKpfG6bWC62LNGEPQHbWKWPg0sXninLBh2v6mDuM80b?= =?us-ascii?Q?rH9pkTulo62ZoQ8oWZO8DAhMxW20EVNNh2Rgn8w5m25IvCctD27xn8Ai4mQJ?= =?us-ascii?Q?EbCYtdPv6Gkwva9lgA/gCLFW93WLn6SEEli8blDTJyCFmKEL3BIjpminLLZ0?= =?us-ascii?Q?tf0vkesCzL+URDIsjObZoKWN/0++CgoOWBFo96SO78J97FIWcJF3Giu2eiTf?= =?us-ascii?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:bNXaAgQeuqIaUSZg+NFiXEtTfMkKI52Ydq106PeYp4NDc1sg6QWiFZfcEfwYRWxNOrkzQ8vhIwtP3ajbM4V1xp0clqyA9eiGJJM7eX1tN2s=; 7:wHHYJKVD3mp/avAsA7eFq9qlPCy9PEdttJXHbPLC7qqfMkVpYuyrSJ4CvV0N4lugghRmSg9LM67bCc/kB+p2eFJMGaZaPC9y+o70vf897i9GUsPi3FSa07VVeDZgTKtAq8gy3DLdH0WTrMYCZTC146d3e+XgWIpJO0atHf1QYRl8uMXiQOcAaBV6E7VcZQzu6i7rmixhKcza+gdUFjUu7e0nonf8K28SdWGdp0CL6xmt1RFqhLksKfKcvsM1rd3z SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BLUPR12MB0434; 20:Yrh7DHgIf3c7i0KigG1H9agceBxWueWnOiaOx7+9/w+W2Cwm7ncNbWdOM5Rp5b8ynK3PqzoE8LzK4doPHCrHFfVmS9o4J0R031e2JDoHzQZNa4jcDaAzTjOPrVH/OTlw2mF8xOKdIn3w7Dtj2WyuIyvozGkuak6hhxQY7uqeO0sLgqBkR2bOYAb23TJ1KASg7f9/wyb6H9vNljv6bP7TgWd+HU2pbIY780KKl1VXndLPThL97/ONUjIUA2s/GZ1p X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Jul 2016 09:33:21.3433 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR12MB0434 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. Signed-off-by: Suravee Suthikulpanit --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 78 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 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..90dc0fb 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -96,6 +96,18 @@ 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_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 +254,10 @@ static int avic; module_param(avic, int, S_IRUGO); #endif +/* AVIC VM ID bit masks and lock */ +static unsigned long *avic_vm_id_bm; +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 +1296,61 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) return 0; } +static inline int avic_vm_id_init(void) +{ + if (avic_vm_id_bm) + return 0; + + avic_vm_id_bm = kcalloc(BITS_TO_LONGS(AVIC_VM_ID_MASK), + sizeof(long), GFP_KERNEL); + if (!avic_vm_id_bm) + return -ENOMEM; + return 0; +} + +static inline void avic_vm_id_deinit(void) +{ + if (!avic_vm_id_bm) + return; + + kfree(avic_vm_id_bm); + avic_vm_id_bm = NULL; +} + +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_bm, 1, 1); + if (id <= AVIC_VM_ID_MASK) + __set_bit(id, avic_vm_id_bm); + else + id = -EINVAL; + + 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_bm); + 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 +1367,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) @@ -5076,6 +5147,12 @@ static struct kvm_x86_ops svm_x86_ops = { static int __init svm_init(void) { + int ret; + + ret = avic_vm_id_init(); + if (ret) + return ret; + return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm), __alignof__(struct vcpu_svm), THIS_MODULE); } @@ -5083,6 +5160,7 @@ static int __init svm_init(void) static void __exit svm_exit(void) { kvm_exit(); + avic_vm_id_deinit(); } module_init(svm_init)