Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp732584ybl; Thu, 22 Aug 2019 04:08:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqxbureBrdPRQSh/V+CDSZyh3p+yb0tjJn8b6yozn7Newc0t+GC6pUVcZ5x1B91dQyqc/bYc X-Received: by 2002:a63:484a:: with SMTP id x10mr32992195pgk.430.1566472093239; Thu, 22 Aug 2019 04:08:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1566472093; cv=pass; d=google.com; s=arc-20160816; b=MTITURvCq4IeP8qrZG+XVxq/W7cycAai/rjrkjtChSEfMKAjxcJjM3j//Pxfiahdzn 31t+AZgkYtaoNrzeMjq64OdtppTyjImm82UQVr/pMWdjrTdafGBtFfK2L1YpNZZ/SgAM 9gBYuwZNReSnVuUJb6obIh16SD8MmL4+nT8Su3ybkzUAjq+g/KkbJAo/ad/I8Ht4KNCC V3rs0KBVoAsXOvrnp2M9y1bXnZ5Ge+6REAyqaLAfob8hnbyM3nO4uLj17o3yDraJx8+p QtbgO53eboCfTDxugBaOqM/xL1DIZVuoeV1qAE9fkuwQifrC/6gUYDIgAH7WseUXcxKr YktA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :wdcipoutbound:content-language:accept-language:in-reply-to :references:message-id:date:thread-index:thread-topic:subject:cc:to :from:dkim-signature:ironport-sdr:dkim-signature; bh=ra/F5QZy/beG07/D/SSKCf55gox+0kqXLyYmfDFUSgk=; b=qLzq2baJz4xGZOQRVdwHLk3EQb54Srb7t8yd/JHmW/Gm5WZAnKzZ0PEgyZP9qbLNRg LuVG+NIykVl7HrnMYBogEYd4YnLCFo7g1kxMr1ywRy66HYpFdyihAQHQm8ACdHBB8V6j pHIX9I81MWw7ErNqKQZmy4fyuV88bdxIioVmDPEhhK2V3C3WTsb80aqNQ5dX43ELkbJw X00qGUwHXvnxwwctxm7Vj0qFLi+CMrr8lSAOO9XG1KNPnFW0ZF70TjEEd78JbAA+WA0X n+O2y2/5fHklR/vjjMvnkqQ0RsnJ9G5pep5Yh1mk5pIZ7WtG4PRA9YaKNFIlxJCK6QHg 0Aig== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=pfBsblkQ; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=vGrQ3NE1; arc=pass (i=1 spf=pass spfdomain=wdc.com dkim=pass dkdomain=wdc.com dmarc=pass fromdomain=wdc.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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t13si17945311pfh.245.2019.08.22.04.07.57; Thu, 22 Aug 2019 04:08:13 -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=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=pfBsblkQ; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=vGrQ3NE1; arc=pass (i=1 spf=pass spfdomain=wdc.com dkim=pass dkdomain=wdc.com dmarc=pass fromdomain=wdc.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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732624AbfHVIpX (ORCPT + 99 others); Thu, 22 Aug 2019 04:45:23 -0400 Received: from esa5.hgst.iphmx.com ([216.71.153.144]:2015 "EHLO esa5.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726039AbfHVIpW (ORCPT ); Thu, 22 Aug 2019 04:45:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1566463522; x=1597999522; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=vHzbl3OB6Gfajc1v4PC8lGbMhBsQBoyBeiE3LgxYXys=; b=pfBsblkQI8rrHcHK53JDwAfU1DSngYzjZ7ZXSq6Z70njMbHLQGpRhPUz 5Pb8QY41ob801N8nvqaBV2H363G9qlZbu0K6TfSA/Jhd/hzywFdPTkmZv mk7ea530phz/ALKY5nPBaMWuVpohcQOflEDiLMZd4EIjnU2du5VVaamay g0trdxC2r9FMfPFH9GgdsXKBUZK/YKzEzoa3czqDLFG97olsNS1LpDXzS uS1+n40G6DdSQ7fWjCKGJ1vCdGdvy0ZuO3jpe5AnfwiErGifienrPcBtg rrQlmP4x/GVSo1B+j/CUp2p9vKW5rhA8l7hKONQ6YKfShTgWz9deBm4yD g==; IronPort-SDR: 0jA0e1gFV5c9WfPWdhhPNpFeu0aLlIaTIhOCRALACM7PvNZdOBeFVrzXzkwU6jc/IDnVmPoyJv 5JdVZk1AKPWPQeiE930XkkNgkPaH3kQKOw45KnSzdReqQc/SMUsvztKNP5cNwCi9p9CDClG57+ szZvhOAnXMDC6xNtTA+mvr4vjAoNo0eg8W+LPoLnPce8qrjb5KdUk9hRNT7onnPqSH090xgqBs xJAF5FQtFMTEGao6+QiLX7YN7StbEYzK7bLcW+HcyomkdJC73DqTrav3SAUdaW+QNbdMpD69hY R18= X-IronPort-AV: E=Sophos;i="5.64,416,1559491200"; d="scan'208";a="117334177" Received: from mail-co1nam03lp2055.outbound.protection.outlook.com (HELO NAM03-CO1-obe.outbound.protection.outlook.com) ([104.47.40.55]) by ob1.hgst.iphmx.com with ESMTP; 22 Aug 2019 16:45:20 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TVSj6QtFLKN4aOFlp1CikW2PhyEcclR/JtNIwB9+qaEni3uPWY1ltJT8SuwVnFVXIqkp/IqhZqSWTfj9Bvf7dHXr8KXaydOHkfLzywC969Q53Oza4RjZcmc5juZ0vxbpceSoneDrWMqpQGEED4EJYr+iUA0nzDAgisJXVu/luILIz/P9bRzBFj144PHA5aHzp2UMvkIMMwkQA/cRwwccVu3pZasG6Ar13xV4BQBNmd6DwvifOPa6FbR9qqNsPJRyQqWSwzKzjHQHQEhBnoWYbTca+Z5Orwfp9isax96MzSmX7dWskv842FgoLdkDCvXdnc8WUbmZQbCzPbwdyr0pqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ra/F5QZy/beG07/D/SSKCf55gox+0kqXLyYmfDFUSgk=; b=hGN3H32vE5ycy9lwcomJWsDqPPCqAeXcG+xcvy0aOP7xHM2f46lKfPcCiyPlnoYWga4ViXFUm8AgktvAAh7nNJ6G/Vm/ehFqeYw0fKDN9UK8z3GiNYak7PyejpFLB32aiCAb/lIHQwhGaCrQuifskUIXPnLOeV7Okeuq3zk+Pt/C+QTBqBhYCxrQ3GdhxuXcc/ZvKXdPg2j5yFVi1X3LBHuuP3e4H1ScOq7v2eWwASCQ5082mWiHKAH3ufrC3UESvW6Dki75yfI1fO1EkXJWBeedwuSMNP1r9D1JgM00ReSvkEEXfjVBthcAFYBa4hLhHEovvcAUYT9rP/AJgbc+bA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wdc.com; dmarc=pass action=none header.from=wdc.com; dkim=pass header.d=wdc.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector2-sharedspace-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ra/F5QZy/beG07/D/SSKCf55gox+0kqXLyYmfDFUSgk=; b=vGrQ3NE1hm7gk+nfh+aZGJD7UeNDLFZYRFFSUzfgl9ohfLqRLUELtnNFGbDisMoItTWq7stdt3Q+uq7Cz6k1sBFUJUqNiJBw1R4E1O3yuI3nALu2bf8h2JBkIcUFccqS9uhSjliJ9FyhySAX6/ndEbtgZ0vMA8jhk6j5XDcerCM= Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB6048.namprd04.prod.outlook.com (20.178.249.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2178.19; Thu, 22 Aug 2019 08:45:19 +0000 Received: from MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::a815:e61a:b4aa:60c8]) by MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::a815:e61a:b4aa:60c8%7]) with mapi id 15.20.2178.018; Thu, 22 Aug 2019 08:45:19 +0000 From: Anup Patel To: Palmer Dabbelt , Paul Walmsley , Paolo Bonzini , Radim K CC: Daniel Lezcano , Thomas Gleixner , Atish Patra , Alistair Francis , Damien Le Moal , Christoph Hellwig , Anup Patel , "kvm@vger.kernel.org" , "linux-riscv@lists.infradead.org" , "linux-kernel@vger.kernel.org" , Anup Patel Subject: [PATCH v5 12/20] RISC-V: KVM: Implement VMID allocator Thread-Topic: [PATCH v5 12/20] RISC-V: KVM: Implement VMID allocator Thread-Index: AQHVWMXtXSjdIDrkzk6hRbi+DJsQXA== Date: Thu, 22 Aug 2019 08:45:19 +0000 Message-ID: <20190822084131.114764-13-anup.patel@wdc.com> References: <20190822084131.114764-1-anup.patel@wdc.com> In-Reply-To: <20190822084131.114764-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MA1PR01CA0118.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:1::34) To MN2PR04MB6061.namprd04.prod.outlook.com (2603:10b6:208:d8::15) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Anup.Patel@wdc.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.17.1 x-originating-ip: [199.255.44.175] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: fe897919-06e6-42a0-ad31-08d726dd0f95 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(5600166)(711020)(4605104)(1401327)(4618075)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7193020);SRVR:MN2PR04MB6048; x-ms-traffictypediagnostic: MN2PR04MB6048: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: wdcipoutbound: EOP-TRUE x-ms-oob-tlc-oobclassifiers: OLM:8273; x-forefront-prvs: 01371B902F x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(4636009)(39860400002)(346002)(366004)(396003)(376002)(136003)(199004)(189003)(6116002)(6486002)(3846002)(54906003)(316002)(76176011)(52116002)(2616005)(50226002)(446003)(5660300002)(1076003)(8936002)(8676002)(81156014)(81166006)(11346002)(478600001)(14454004)(53936002)(110136005)(86362001)(4326008)(186003)(26005)(66066001)(66476007)(25786009)(102836004)(7736002)(36756003)(6512007)(305945005)(386003)(6506007)(99286004)(71190400001)(71200400001)(7416002)(486006)(476003)(6436002)(66946007)(44832011)(256004)(66556008)(64756008)(14444005)(2906002)(66446008);DIR:OUT;SFP:1102;SCL:1;SRVR:MN2PR04MB6048;H:MN2PR04MB6061.namprd04.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: ocuCOtr276pKeaU3kF6e9C4ZwuRXqx6dgGJwYwTmOZWXJgk+f5t0CzkvtRmHqB0LYehd7oEAodbT89pk6X3yvziFhS4M69QAHAMJtGAMpi88elV5gEugEeiDhDvN6F5tFTy2FMLo8pm220kKDtxkrocpZpxGt8s7PAdfYFTMxvNw8HQizDuVZ0C+NoCQ2xTydXzIcnItr60XsOj3fzuvzU6YtZQ+yEA7rvpv1p2aMqjwuxZXEqFTCBEbh+KaHMQtle2ZyLCwcB+gx+uPs7/uLkOPqFP1pIhXDml5pPotnm2wZgTZ6dTYbj+jRjGbWbnIUnOsUr3P7oZJppoL8t7KFHs219mOiLU9y8lHqN5He6lMk733U0N6p4Bple8A7m1muRQVadTXVs2J/7yutL+T/+LUHbuzie7WivRJh3CcJZM= Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: fe897919-06e6-42a0-ad31-08d726dd0f95 X-MS-Exchange-CrossTenant-originalarrivaltime: 22 Aug 2019 08:45:19.5405 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: 9wp/1hfk7XdB3hS4AVZAwWRorljd7k+febsd+s6dFc8l7n8q35HseMfpebD82QgGe9CBpq1KVF/8T5YC//AhKQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB6048 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We implement a simple VMID allocator for Guests/VMs which: 1. Detects number of VMID bits at boot-time 2. Uses atomic number to track VMID version and increments VMID version whenever we run-out of VMIDs 3. Flushes Guest TLBs on all host CPUs whenever we run-out of VMIDs 4. Force updates HW Stage2 VMID for each Guest VCPU whenever VMID changes using VCPU request KVM_REQ_UPDATE_HGATP Signed-off-by: Anup Patel Acked-by: Paolo Bonzini Reviewed-by: Paolo Bonzini --- arch/riscv/include/asm/kvm_host.h | 25 ++++++ arch/riscv/kvm/Makefile | 3 +- arch/riscv/kvm/main.c | 4 + arch/riscv/kvm/tlb.S | 43 +++++++++++ arch/riscv/kvm/vcpu.c | 9 +++ arch/riscv/kvm/vm.c | 6 ++ arch/riscv/kvm/vmid.c | 123 ++++++++++++++++++++++++++++++ 7 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/kvm/tlb.S create mode 100644 arch/riscv/kvm/vmid.c diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm= _host.h index 4388bace6d70..3b09158f80f2 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -27,6 +27,7 @@ #define KVM_REQ_SLEEP \ KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(1) +#define KVM_REQ_UPDATE_HGATP KVM_ARCH_REQ(2) =20 struct kvm_vm_stat { ulong remote_tlb_flush; @@ -47,7 +48,19 @@ struct kvm_vcpu_stat { struct kvm_arch_memory_slot { }; =20 +struct kvm_vmid { + /* + * Writes to vmid_version and vmid happen with vmid_lock held + * whereas reads happen without any lock held. + */ + unsigned long vmid_version; + unsigned long vmid; +}; + struct kvm_arch { + /* stage2 vmid */ + struct kvm_vmid vmid; + /* stage2 page table */ pgd_t *pgd; phys_addr_t pgd_phys; @@ -169,6 +182,12 @@ static inline void kvm_arch_vcpu_block_finish(struct k= vm_vcpu *vcpu) {} int kvm_riscv_setup_vsip(void); void kvm_riscv_cleanup_vsip(void); =20 +extern void __kvm_riscv_hfence_gvma_vmid_gpa(unsigned long vmid, + unsigned long gpa); +extern void __kvm_riscv_hfence_gvma_vmid(unsigned long vmid); +extern void __kvm_riscv_hfence_gvma_gpa(unsigned long gpa); +extern void __kvm_riscv_hfence_gvma_all(void); + int kvm_riscv_stage2_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long h= va, bool is_write); void kvm_riscv_stage2_flush_cache(struct kvm_vcpu *vcpu); @@ -176,6 +195,12 @@ int kvm_riscv_stage2_alloc_pgd(struct kvm *kvm); void kvm_riscv_stage2_free_pgd(struct kvm *kvm); void kvm_riscv_stage2_update_hgatp(struct kvm_vcpu *vcpu); =20 +void kvm_riscv_stage2_vmid_detect(void); +unsigned long kvm_riscv_stage2_vmid_bits(void); +int kvm_riscv_stage2_vmid_init(struct kvm *kvm); +bool kvm_riscv_stage2_vmid_ver_changed(struct kvm_vmid *vmid); +void kvm_riscv_stage2_vmid_update(struct kvm_vcpu *vcpu); + int kvm_riscv_vcpu_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run)= ; int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, unsigned long scause, unsigned long stval); diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile index 845579273727..c0f57f26c13d 100644 --- a/arch/riscv/kvm/Makefile +++ b/arch/riscv/kvm/Makefile @@ -8,6 +8,7 @@ ccflags-y :=3D -Ivirt/kvm -Iarch/riscv/kvm =20 kvm-objs :=3D $(common-objs-y) =20 -kvm-objs +=3D main.o vm.o mmu.o vcpu.o vcpu_exit.o vcpu_switch.o +kvm-objs +=3D main.o vm.o vmid.o tlb.o mmu.o +kvm-objs +=3D vcpu.o vcpu_exit.o vcpu_switch.o =20 obj-$(CONFIG_KVM) +=3D kvm.o diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c index d088247843c5..55df85184241 100644 --- a/arch/riscv/kvm/main.c +++ b/arch/riscv/kvm/main.c @@ -72,8 +72,12 @@ int kvm_arch_init(void *opaque) if (ret) return ret; =20 + kvm_riscv_stage2_vmid_detect(); + kvm_info("hypervisor extension available\n"); =20 + kvm_info("host has %ld VMID bits\n", kvm_riscv_stage2_vmid_bits()); + return 0; } =20 diff --git a/arch/riscv/kvm/tlb.S b/arch/riscv/kvm/tlb.S new file mode 100644 index 000000000000..453fca8d7940 --- /dev/null +++ b/arch/riscv/kvm/tlb.S @@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2019 Western Digital Corporation or its affiliates. + * + * Authors: + * Anup Patel + */ + +#include +#include + + .text + .altmacro + .option norelax + + /* + * Instruction encoding of hfence.gvma is: + * 0110001 rs2(5) rs1(5) 000 00000 1110011 + */ + +ENTRY(__kvm_riscv_hfence_gvma_vmid_gpa) + /* hfence.gvma a1, a0 */ + .word 0x62a60073 + ret +ENDPROC(__kvm_riscv_hfence_gvma_vmid_gpa) + +ENTRY(__kvm_riscv_hfence_gvma_vmid) + /* hfence.gvma zero, a0 */ + .word 0x62a00073 + ret +ENDPROC(__kvm_riscv_hfence_gvma_vmid) + +ENTRY(__kvm_riscv_hfence_gvma_gpa) + /* hfence.gvma a0 */ + .word 0x62050073 + ret +ENDPROC(__kvm_riscv_hfence_gvma_gpa) + +ENTRY(__kvm_riscv_hfence_gvma_all) + /* hfence.gvma */ + .word 0x62000073 + ret +ENDPROC(__kvm_riscv_hfence_gvma_all) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index e6d74a9a2fdf..6124077d154f 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -628,6 +628,12 @@ static void kvm_riscv_check_vcpu_requests(struct kvm_v= cpu *vcpu) =20 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) kvm_riscv_reset_vcpu(vcpu); + + if (kvm_check_request(KVM_REQ_UPDATE_HGATP, vcpu)) + kvm_riscv_stage2_update_hgatp(vcpu); + + if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) + __kvm_riscv_hfence_gvma_all(); } } =20 @@ -690,6 +696,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, stru= ct kvm_run *run) /* Check conditions before entering the guest */ cond_resched(); =20 + kvm_riscv_stage2_vmid_update(vcpu); + kvm_riscv_check_vcpu_requests(vcpu); =20 preempt_disable(); @@ -726,6 +734,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, stru= ct kvm_run *run) kvm_riscv_update_vsip(vcpu); =20 if (ret <=3D 0 || + kvm_riscv_stage2_vmid_ver_changed(&vcpu->kvm->arch.vmid) || kvm_request_pending(vcpu)) { vcpu->mode =3D OUTSIDE_GUEST_MODE; local_irq_enable(); diff --git a/arch/riscv/kvm/vm.c b/arch/riscv/kvm/vm.c index ac0211820521..c5aab5478c38 100644 --- a/arch/riscv/kvm/vm.c +++ b/arch/riscv/kvm/vm.c @@ -26,6 +26,12 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type= ) if (r) return r; =20 + r =3D kvm_riscv_stage2_vmid_init(kvm); + if (r) { + kvm_riscv_stage2_free_pgd(kvm); + return r; + } + return 0; } =20 diff --git a/arch/riscv/kvm/vmid.c b/arch/riscv/kvm/vmid.c new file mode 100644 index 000000000000..8154feea12d5 --- /dev/null +++ b/arch/riscv/kvm/vmid.c @@ -0,0 +1,123 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2019 Western Digital Corporation or its affiliates. + * + * Authors: + * Anup Patel + */ + +#include +#include +#include +#include +#include +#include +#include + +static unsigned long vmid_version =3D 1; +static unsigned long vmid_next; +static unsigned long vmid_bits; +static DEFINE_SPINLOCK(vmid_lock); + +void kvm_riscv_stage2_vmid_detect(void) +{ + unsigned long old; + + /* Figure-out number of VMID bits in HW */ + old =3D csr_read(CSR_HGATP); + csr_write(CSR_HGATP, old | HGATP_VMID_MASK); + vmid_bits =3D csr_read(CSR_HGATP); + vmid_bits =3D (vmid_bits & HGATP_VMID_MASK) >> HGATP_VMID_SHIFT; + vmid_bits =3D fls_long(vmid_bits); + csr_write(CSR_HGATP, old); + + /* We polluted local TLB so flush all guest TLB */ + __kvm_riscv_hfence_gvma_all(); + + /* We don't use VMID bits if they are not sufficient */ + if ((1UL << vmid_bits) < num_possible_cpus()) + vmid_bits =3D 0; +} + +unsigned long kvm_riscv_stage2_vmid_bits(void) +{ + return vmid_bits; +} + +int kvm_riscv_stage2_vmid_init(struct kvm *kvm) +{ + /* Mark the initial VMID and VMID version invalid */ + kvm->arch.vmid.vmid_version =3D 0; + kvm->arch.vmid.vmid =3D 0; + + return 0; +} + +static void local_guest_tlb_flush_all(void *info) +{ + __kvm_riscv_hfence_gvma_all(); +} + +bool kvm_riscv_stage2_vmid_ver_changed(struct kvm_vmid *vmid) +{ + if (!vmid_bits) + return false; + + return unlikely(READ_ONCE(vmid->vmid_version) !=3D + READ_ONCE(vmid_version)); +} + +void kvm_riscv_stage2_vmid_update(struct kvm_vcpu *vcpu) +{ + int i; + struct kvm_vcpu *v; + struct kvm_vmid *vmid =3D &vcpu->kvm->arch.vmid; + + if (!kvm_riscv_stage2_vmid_ver_changed(vmid)) + return; + + spin_lock(&vmid_lock); + + /* + * We need to re-check the vmid_version here to ensure that if + * another vcpu already allocated a valid vmid for this vm. + */ + if (!kvm_riscv_stage2_vmid_ver_changed(vmid)) { + spin_unlock(&vmid_lock); + return; + } + + /* First user of a new VMID version? */ + if (unlikely(vmid_next =3D=3D 0)) { + WRITE_ONCE(vmid_version, READ_ONCE(vmid_version) + 1); + vmid_next =3D 1; + + /* + * We ran out of VMIDs so we increment vmid_version and + * start assigning VMIDs from 1. + * + * This also means existing VMIDs assignement to all Guest + * instances is invalid and we have force VMID re-assignement + * for all Guest instances. The Guest instances that were not + * running will automatically pick-up new VMIDs because will + * call kvm_riscv_stage2_vmid_update() whenever they enter + * in-kernel run loop. For Guest instances that are already + * running, we force VM exits on all host CPUs using IPI and + * flush all Guest TLBs. + */ + smp_call_function_many(cpu_all_mask, local_guest_tlb_flush_all, + NULL, true); + } + + vmid->vmid =3D vmid_next; + vmid_next++; + vmid_next &=3D (1 << vmid_bits) - 1; + + WRITE_ONCE(vmid->vmid_version, READ_ONCE(vmid_version)); + + spin_unlock(&vmid_lock); + + /* Request stage2 page table update for all VCPUs */ + kvm_for_each_vcpu(i, v, vcpu->kvm) + kvm_make_request(KVM_REQ_UPDATE_HGATP, v); +} --=20 2.17.1