Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2335718ybl; Thu, 29 Aug 2019 06:58:21 -0700 (PDT) X-Google-Smtp-Source: APXvYqyEpsxuTTDY45RF1Xzl6fBJDJFsEljISzdigYuUTF2SwUSMu1QizZeOtuXWfurrS3y9IBgU X-Received: by 2002:a62:8c96:: with SMTP id m144mr1482765pfd.154.1567087101091; Thu, 29 Aug 2019 06:58:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1567087101; cv=pass; d=google.com; s=arc-20160816; b=kaGHuebB319miscP8DtPFm0KpX0YiZPfm/xhnrSVs8yw9+9ihjCH29jmzsyAuRDEAS 2Q0tNqz0UYrYqjj+NMpN1lpUpy4M4uOLp5Vnj0c2neabLlr0ag5yIA4kKNmWGXB1JfZO xU4xYucAK77r3ePLHNiTCwTXYSP/1Zrt9dyV3FSswUW6DvDeyZHMU0WhK2moea6BOt/U YP8itUKe5Tp4DkD/8s34OjX/zDFa4J++zIC0wqJ2Yi6mJjY3RKo3Rn7I5EQFQJKvs/dm hhjrasyau27eUlOTuikWqAiO/k/2GKP2J1vhmgQgk+4OEm3iYaYvnv5Tezi9IqRJjHfB kztA== 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=Yt/bfLKgQHOoSW18ZgVO03tVEQ15LpQUKo55Jl5uBdQ=; b=k9vlKMsg3U9J2tnE++pwyuYxysE/cD51ZQZBqJZLs2YOaxSCOgvxuc23UZa9CEXtP3 3dfYOGjphlBSoV8fSx2BO9VREde8kmJjBLX4g+hX6W0FAudikl62h133RrrunUXQqx3r sFkKTOGe3oB6Mb5tHFVbgE4wICOPIzqJwQPlyu4vUi446UMKgsTKEOkxoydiR7itqQln 8CMRYIdJv0LjvrEf5UQDLV2pa5HqCFXVFvK1g2xMu2xxWNvDXRCstmj5JgFzb3WVUm1N sK03X0psrdphu7HwMSmEPWYPoTOXEExv/ohua62tA6/PVfBcm7gMzCG6uMYh+IQOFb1h RYVw== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=HgoEOXAh; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=sP3NgMqF; 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 c5si1977836plr.291.2019.08.29.06.58.05; Thu, 29 Aug 2019 06:58:21 -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=HgoEOXAh; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=sP3NgMqF; 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 S1728251AbfH2N4s (ORCPT + 99 others); Thu, 29 Aug 2019 09:56:48 -0400 Received: from esa3.hgst.iphmx.com ([216.71.153.141]:25530 "EHLO esa3.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727415AbfH2N4r (ORCPT ); Thu, 29 Aug 2019 09:56:47 -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=1567087007; x=1598623007; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=uv2gLrVAhGUVXfeMidSGNzVSpJY2Y/+bfRKfpmsRQwQ=; b=HgoEOXAhhCeePvzESSwWl1WXcrkhH/BBXJHhTIBo1+KMdRUP4HXaKX+F eh1m75MGoEC/fEM0c0JAAxd3rRFU3vFTy3lcIa3OmLz+8QnkHvtJbPnw4 ZbuPVVNqvpLcaGIDhqQyneqG+NdbPYCKYVXAsewIDK8t1bzUh3U6MCpbB 2WhON0YRjYuGZenXSZ7APJJsI8sf0FnNx8fPKn5QDDKZPFn4IgTo5P4ZH BGwoS4JuAmZCPp10+u7L9lRG0lvoyd1cFTILYVvQ3v7raiEurKGY9hwQM TBXXGyLlns8Gt3HiAff1niMh9/eCtcAmjZXmVTfjdyYOGVRhfXHQ1cFqK Q==; IronPort-SDR: 5uEg0HcBeF1caCOZNx1YbupfaWLQba59LkCw7lTSMyKjXd5z2XShLUPE/Ll+l/0WyRUkJOZ9cO 9vS91/ipXJx84Yc2kshjuh7ndKZxmYk3+hRO3bHuQ5EfUqxskb9+PfDAEF3Vi5qEQu9D7w+rgD nkg4A1TS1ftrh4XgaqsveTtooei5fQOGMRoTYpSVlDvQqsZ8ryB03O8M0az57AJ7mnbdqTLDP4 jNSOV8OHlnuTwoJT3YHLK55KAExcbvTjRv5rBiycvioxzBxQOukpbY+PE1cnYdzXeJmLSdwnd+ yls= X-IronPort-AV: E=Sophos;i="5.64,443,1559491200"; d="scan'208";a="121525737" Received: from mail-by2nam05lp2051.outbound.protection.outlook.com (HELO NAM05-BY2-obe.outbound.protection.outlook.com) ([104.47.50.51]) by ob1.hgst.iphmx.com with ESMTP; 29 Aug 2019 21:56:46 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RyHt0bCupSjkPKZMcyJW3Q/ge0gdo7iS0ew/7ztc/z3aiGTWGtePsKEc/tHPjtydLMSC0ak4LBy1+OtUSHSubogGLBTklKbsElxBKU8i/4qooL/JBBrmhJNL7m1z6ougr5f8pWWsoOBMLeNd0g0rICPjcp8INQqoB0LFXwuOMW+Wwk8NQew1RgBLyqByvAWRyoT/UemGaj41UZBUB7gmm94poVj2bpun7hCx3mGEhSVTFcdKruM6bSTe5UNSqMXRaTKp2Hl0x5eSlvj+nYCFkquujkHYNrMWLOyvJ/L88MTas5EV/JVAbjW50Pa+x6PzeDiuQdp5N981FtE8EdYXlA== 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=Yt/bfLKgQHOoSW18ZgVO03tVEQ15LpQUKo55Jl5uBdQ=; b=jCqkEqXtwwaGSeehbVxA44LZfKigKceiO5pYPUecpLXEcf/lylkcYaUau5RK0HZyHbtTcv3e0yOGB4TU6Hj45AtrXw9sJ13MJXZklMxcEtuUABje0RCBRRF93EzQUP4EJGWqKKJHuecNvjAnh8LS687XBeQRmTM2j+ZJxokW+Om349Otli/JbJh+3IpgngGotd28aOzZSV6KVj4d4wgXf5e4s3wpfNFQBDEFbBs8slUnBWOnZMOrQajI44xYsBJdxNlcbbWw4MNtpI1mEzIGfjcpSywolIafblbNfMCTien9ZTmSax3TObOOJ1TMZ/9Sbo6/OrzidQ7CpW6UmwKd9w== 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=Yt/bfLKgQHOoSW18ZgVO03tVEQ15LpQUKo55Jl5uBdQ=; b=sP3NgMqFxypM3VE/VCjCkygNrSKX3UFJfEvceQIds+I8m3Y1aXUYjdzuNgIDnKQPhshiSfUE/FSGBIvBNDTNoR2SaFFHvQGwI19R1cEG4+iErq6mcRqMtOwcUdfI2JLHbIHqBRI7IGAF4URq8oViSW1O+NpycwQpVO8YckGW7Q8= Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB5616.namprd04.prod.outlook.com (20.179.22.29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2199.21; Thu, 29 Aug 2019 13:56:44 +0000 Received: from MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::e1a5:8de2:c3b1:3fb0]) by MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::e1a5:8de2:c3b1:3fb0%7]) with mapi id 15.20.2220.013; Thu, 29 Aug 2019 13:56:44 +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 v6 14/21] RISC-V: KVM: Implement MMU notifiers Thread-Topic: [PATCH v6 14/21] RISC-V: KVM: Implement MMU notifiers Thread-Index: AQHVXnGX6kPw4I2XmUqzxUHstl0rpg== Date: Thu, 29 Aug 2019 13:56:44 +0000 Message-ID: <20190829135427.47808-15-anup.patel@wdc.com> References: <20190829135427.47808-1-anup.patel@wdc.com> In-Reply-To: <20190829135427.47808-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MAXPR0101CA0072.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:e::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: [49.207.51.114] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 11f81a66-eb5c-45ff-ce38-08d72c88b9cc x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(7168020)(4627221)(201703031133081)(201702281549075)(8990200)(5600166)(711020)(4605104)(1401327)(4618075)(2017052603328)(7193020);SRVR:MN2PR04MB5616; x-ms-traffictypediagnostic: MN2PR04MB5616: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: wdcipoutbound: EOP-TRUE x-ms-oob-tlc-oobclassifiers: OLM:785; x-forefront-prvs: 0144B30E41 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(4636009)(39860400002)(136003)(366004)(376002)(346002)(396003)(189003)(199004)(7416002)(52116002)(81166006)(316002)(6506007)(386003)(25786009)(7736002)(81156014)(305945005)(2906002)(36756003)(71190400001)(14454004)(99286004)(1076003)(256004)(14444005)(4326008)(66946007)(76176011)(66446008)(64756008)(66556008)(66476007)(102836004)(6116002)(50226002)(55236004)(476003)(6512007)(3846002)(71200400001)(5660300002)(26005)(186003)(44832011)(86362001)(66066001)(6436002)(446003)(8936002)(6486002)(110136005)(54906003)(2616005)(486006)(53936002)(11346002)(478600001)(8676002);DIR:OUT;SFP:1102;SCL:1;SRVR:MN2PR04MB5616;H:MN2PR04MB6061.namprd04.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: 5GzXynkTvWCcWpAI3MQzobb0Fcyx6lfA2hEvy8KdLewjFahHwzlLRZSL392xvtUYCOCA6wkFjFdeQ+CHrUBBF2SMS6erc+XZPx3+v8e0NLQ0G0MWreY1s8RigOIZZcLrGyNrpxWq4DMOOtdk7J5nckxqorFimo5EDFslFJnh4x8jqmtd9UclOLQi94WgphxcIiIHOK9gP9mjByiqx2GRmM81f7+jL7+Z3W0aHU8iATdOB5YfqYYxUz578TWYrlRiRLerdYtiVxYTCGDYsME0uD8frN8AGnPXKHyxkqNXbrAn1HxU3/iSunCB63qD7ER4mBS4VCJjbm/injZ8oG7a1LQ3bm5UGm6V+PWbg5obm/nFzNBFM/QbogNL4QSMthVozMSE2VsyhXrHxC9Uz6EQx1WhWlHP0jm3inA/xzFMqFM= 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: 11f81a66-eb5c-45ff-ce38-08d72c88b9cc X-MS-Exchange-CrossTenant-originalarrivaltime: 29 Aug 2019 13:56:44.2704 (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: uG0dirECJhggn3PpbZWrpFoK5OBuMRLjIKaFKhdjYz0aj67dmblcDUI654o0fmZXVQ/CKW0NHZabwVixQEsB0A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB5616 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch implements MMU notifiers for KVM RISC-V so that Guest physical address space is in-sync with Host physical address space. This will allow swapping, page migration, etc to work transparently with KVM RISC-V. Signed-off-by: Anup Patel Acked-by: Paolo Bonzini Reviewed-by: Paolo Bonzini Reviewed-by: Alexander Graf --- arch/riscv/include/asm/kvm_host.h | 7 ++ arch/riscv/kvm/Kconfig | 1 + arch/riscv/kvm/mmu.c | 200 +++++++++++++++++++++++++++++- arch/riscv/kvm/vm.c | 1 + 4 files changed, 208 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm= _host.h index cc4aa5b8286d..9e7f6a0c74dd 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -192,6 +192,13 @@ 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 +#define KVM_ARCH_WANT_MMU_NOTIFIER +int kvm_unmap_hva_range(struct kvm *kvm, + unsigned long start, unsigned long end); +int kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte); +int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end); +int kvm_test_age_hva(struct kvm *kvm, unsigned long hva); + void __kvm_riscv_hfence_gvma_vmid_gpa(unsigned long vmid, unsigned long gpa); void __kvm_riscv_hfence_gvma_vmid(unsigned long vmid); diff --git a/arch/riscv/kvm/Kconfig b/arch/riscv/kvm/Kconfig index 35fd30d0e432..002e14ee37f6 100644 --- a/arch/riscv/kvm/Kconfig +++ b/arch/riscv/kvm/Kconfig @@ -20,6 +20,7 @@ if VIRTUALIZATION config KVM tristate "Kernel-based Virtual Machine (KVM) support" depends on OF + select MMU_NOTIFIER select PREEMPT_NOTIFIERS select ANON_INODES select KVM_MMIO diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 590669290139..d8a692d3e640 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -67,6 +67,66 @@ static void *stage2_cache_alloc(struct kvm_mmu_page_cach= e *pcache) return p; } =20 +static int stage2_pgdp_test_and_clear_young(pgd_t *pgd) +{ + return ptep_test_and_clear_young(NULL, 0, (pte_t *)pgd); +} + +static int stage2_pmdp_test_and_clear_young(pmd_t *pmd) +{ + return ptep_test_and_clear_young(NULL, 0, (pte_t *)pmd); +} + +static int stage2_ptep_test_and_clear_young(pte_t *pte) +{ + return ptep_test_and_clear_young(NULL, 0, pte); +} + +static bool stage2_get_leaf_entry(struct kvm *kvm, gpa_t addr, + pgd_t **pgdpp, pmd_t **pmdpp, pte_t **ptepp) +{ + pgd_t *pgdp; + pmd_t *pmdp; + pte_t *ptep; + + *pgdpp =3D NULL; + *pmdpp =3D NULL; + *ptepp =3D NULL; + + pgdp =3D &kvm->arch.pgd[pgd_index(addr)]; + if (!pgd_val(*pgdp)) + return false; + if (pgd_val(*pgdp) & _PAGE_LEAF) { + *pgdpp =3D pgdp; + return true; + } + + if (stage2_have_pmd) { + pmdp =3D (void *)pgd_page_vaddr(*pgdp); + pmdp =3D &pmdp[pmd_index(addr)]; + if (!pmd_present(*pmdp)) + return false; + if (pmd_val(*pmdp) & _PAGE_LEAF) { + *pmdpp =3D pmdp; + return true; + } + + ptep =3D (void *)pmd_page_vaddr(*pmdp); + } else { + ptep =3D (void *)pgd_page_vaddr(*pgdp); + } + + ptep =3D &ptep[pte_index(addr)]; + if (!pte_present(*ptep)) + return false; + if (pte_val(*ptep) & _PAGE_LEAF) { + *ptepp =3D ptep; + return true; + } + + return false; +} + struct local_guest_tlb_info { struct kvm_vmid *vmid; gpa_t addr; @@ -450,6 +510,38 @@ int stage2_ioremap(struct kvm *kvm, gpa_t gpa, phys_ad= dr_t hpa, =20 } =20 +static int handle_hva_to_gpa(struct kvm *kvm, + unsigned long start, + unsigned long end, + int (*handler)(struct kvm *kvm, + gpa_t gpa, u64 size, + void *data), + void *data) +{ + struct kvm_memslots *slots; + struct kvm_memory_slot *memslot; + int ret =3D 0; + + slots =3D kvm_memslots(kvm); + + /* we only care about the pages that the guest sees */ + kvm_for_each_memslot(memslot, slots) { + unsigned long hva_start, hva_end; + gfn_t gpa; + + hva_start =3D max(start, memslot->userspace_addr); + hva_end =3D min(end, memslot->userspace_addr + + (memslot->npages << PAGE_SHIFT)); + if (hva_start >=3D hva_end) + continue; + + gpa =3D hva_to_gfn_memslot(hva_start, memslot) << PAGE_SHIFT; + ret |=3D handler(kvm, gpa, (u64)(hva_end - hva_start), data); + } + + return ret; +} + void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, struct kvm_memory_slot *dont) { @@ -582,6 +674,106 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, return ret; } =20 +static int kvm_unmap_hva_handler(struct kvm *kvm, + gpa_t gpa, u64 size, void *data) +{ + stage2_unmap_range(kvm, gpa, size); + return 0; +} + +int kvm_unmap_hva_range(struct kvm *kvm, + unsigned long start, unsigned long end) +{ + if (!kvm->arch.pgd) + return 0; + + handle_hva_to_gpa(kvm, start, end, + &kvm_unmap_hva_handler, NULL); + return 0; +} + +static int kvm_set_spte_handler(struct kvm *kvm, + gpa_t gpa, u64 size, void *data) +{ + pte_t *pte =3D (pte_t *)data; + + WARN_ON(size !=3D PAGE_SIZE); + stage2_set_pte(kvm, NULL, gpa, pte); + + return 0; +} + +int kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte) +{ + unsigned long end =3D hva + PAGE_SIZE; + kvm_pfn_t pfn =3D pte_pfn(pte); + pte_t stage2_pte; + + if (!kvm->arch.pgd) + return 0; + + stage2_pte =3D pfn_pte(pfn, PAGE_WRITE_EXEC); + handle_hva_to_gpa(kvm, hva, end, + &kvm_set_spte_handler, &stage2_pte); + + return 0; +} + +static int kvm_age_hva_handler(struct kvm *kvm, + gpa_t gpa, u64 size, void *data) +{ + pgd_t *pgd; + pmd_t *pmd; + pte_t *pte; + + WARN_ON(size !=3D PAGE_SIZE && size !=3D PMD_SIZE && size !=3D PGDIR_SIZE= ); + if (!stage2_get_leaf_entry(kvm, gpa, &pgd, &pmd, &pte)) + return 0; + + if (pgd) + return stage2_pgdp_test_and_clear_young(pgd); + else if (pmd) + return stage2_pmdp_test_and_clear_young(pmd); + else + return stage2_ptep_test_and_clear_young(pte); +} + +int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end) +{ + if (!kvm->arch.pgd) + return 0; + + return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL); +} + +static int kvm_test_age_hva_handler(struct kvm *kvm, + gpa_t gpa, u64 size, void *data) +{ + pgd_t *pgd; + pmd_t *pmd; + pte_t *pte; + + WARN_ON(size !=3D PAGE_SIZE && size !=3D PMD_SIZE); + if (!stage2_get_leaf_entry(kvm, gpa, &pgd, &pmd, &pte)) + return 0; + + if (pgd) + return pte_young(*((pte_t *)pgd)); + else if (pmd) + return pte_young(*((pte_t *)pmd)); + else + return pte_young(*pte); +} + +int kvm_test_age_hva(struct kvm *kvm, unsigned long hva) +{ + if (!kvm->arch.pgd) + return 0; + + return handle_hva_to_gpa(kvm, hva, hva, + kvm_test_age_hva_handler, NULL); +} + int kvm_riscv_stage2_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long h= va, bool is_write) { @@ -593,7 +785,7 @@ int kvm_riscv_stage2_map(struct kvm_vcpu *vcpu, gpa_t g= pa, unsigned long hva, struct vm_area_struct *vma; struct kvm *kvm =3D vcpu->kvm; struct kvm_mmu_page_cache *pcache =3D &vcpu->arch.mmu_page_cache; - unsigned long vma_pagesize; + unsigned long vma_pagesize, mmu_seq; =20 down_read(¤t->mm->mmap_sem); =20 @@ -623,6 +815,8 @@ int kvm_riscv_stage2_map(struct kvm_vcpu *vcpu, gpa_t g= pa, unsigned long hva, return ret; } =20 + mmu_seq =3D kvm->mmu_notifier_seq; + hfn =3D gfn_to_pfn_prot(kvm, gfn, is_write, &writeable); if (hfn =3D=3D KVM_PFN_ERR_HWPOISON) { if (is_vm_hugetlb_page(vma)) @@ -641,6 +835,9 @@ int kvm_riscv_stage2_map(struct kvm_vcpu *vcpu, gpa_t g= pa, unsigned long hva, =20 spin_lock(&kvm->mmu_lock); =20 + if (mmu_notifier_retry(kvm, mmu_seq)) + goto out_unlock; + if (writeable) { kvm_set_pfn_dirty(hfn); ret =3D stage2_map_page(kvm, pcache, gpa, hfn << PAGE_SHIFT, @@ -653,6 +850,7 @@ int kvm_riscv_stage2_map(struct kvm_vcpu *vcpu, gpa_t g= pa, unsigned long hva, if (ret) kvm_err("Failed to map in stage2\n"); =20 +out_unlock: spin_unlock(&kvm->mmu_lock); kvm_set_pfn_accessed(hfn); kvm_release_pfn_clean(hfn); diff --git a/arch/riscv/kvm/vm.c b/arch/riscv/kvm/vm.c index c5aab5478c38..fd84b4d914dc 100644 --- a/arch/riscv/kvm/vm.c +++ b/arch/riscv/kvm/vm.c @@ -54,6 +54,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ex= t) switch (ext) { case KVM_CAP_DEVICE_CTRL: case KVM_CAP_USER_MEMORY: + case KVM_CAP_SYNC_MMU: case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: case KVM_CAP_ONE_REG: case KVM_CAP_READONLY_MEM: --=20 2.17.1