From patchwork Wed Feb 1 12:52:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13124410 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DC82CC05027 for ; Wed, 1 Feb 2023 14:28:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=0X63k/pWfJpiq4ta51WMkfhj3eNjsg+DUqJ815sU1hE=; b=duR5H2/I9DNG1u hVW4wiz+PRbEumOj6aO7v1rQjtjrXJavS7NB+mlXs6c0gJChg45fEQn1DMdHiUr+lGlNFjDx0ZjbV 77frueLgQJBkoydpU0CIPl5YagadAWg3aTCRMYdmArDCpwxgkQ09YNvlFTwRGOWxW0sEn5f4oobP2 ZBX+cKpgpFE+X8kCw+Hd5xMJUgHLnQBqeDDq3FQWZX2R+5qs97enox1di1FVPUiN3RyAUm1u/SOeA Oxy5bMDHh60arNKmGOLEJVDev7DcyifG1SOoObgb3R/9y9vaViaeylQvqFzeoqyFiyJYVC6o25cV2 a0Q2BLU+invWSy/DBOBA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNE4m-00CLVT-Dz; Wed, 01 Feb 2023 14:27:04 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNDhb-00CBke-7q for linux-arm-kernel@bombadil.infradead.org; Wed, 01 Feb 2023 14:03:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=yvsZEZd93DQdk3Gw4NS1NraTmYfO9Oz159bdinMkA6M=; b=RNk/BUlx0tFg9d1qlp9/aVsUvS KcCLK8J2K6tzNT+dQn9IEj82fKWcNSvRqBEJIgWGeTpWaK/1WqwFqDkefl5vZYektWFFIAUrv3+B7 8trJ3folXrS/f6bMxXSv2+Dy9QKPKWIwvWFsLMub6SDoiWfOH8t+iBC/rC8mpdoG6mSoFbJp6KJnY 5eBY9YnfhgiAHMepryRtqC+3o0ZhAGB97IxQkgNVsBliHeREZwlfN54pYJL4Gx7PrqsVEKOaDfQxD XnXsnYbBSTqtD3ByeRCzC71ZeTpVdagmpnNo4FXbFoswRI6Hr/iCl5hk3ABE1YuzNdQ7POORuBBIa W0Wm0PeQ==; Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pNChZ-004m0p-0e for linux-arm-kernel@lists.infradead.org; Wed, 01 Feb 2023 12:59:05 +0000 Received: by mail-wm1-x333.google.com with SMTP id n13so5597881wmr.4 for ; Wed, 01 Feb 2023 04:59:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yvsZEZd93DQdk3Gw4NS1NraTmYfO9Oz159bdinMkA6M=; b=oItCvCgzbiMM79tWChYKkG5G1ZGcdUz0gKcyWcJtAcqX32+3N+ydf/mJG3YDZ3H4yU ZlRpMMjbnmXiOTDySDVdU5ydxmEmOVJbBDWuilJNR4fr7Om+Lwre+KiXYG+a02PzMgXl vA7UC+RpgBu+Dvd1PGbVsC3j2RzgivRmI3OzYGbO1h9qivNQDPZ2hCqw/OHp518gf29x DTLO2VAbm1JKbIUI2mup5i3KtCbnFe2NgQX7RwLjWUIC9pkcJSMfmVUdoxCboL+XsXpP h9osaVsOzKGfSfIR3kFDQggjhnvQvHJM8Q1oWL+hq/425+aTB8HpZdlDJp7i7xcON34p LZGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yvsZEZd93DQdk3Gw4NS1NraTmYfO9Oz159bdinMkA6M=; b=w9BCSL07PwiUTG+//hYLqTLdBGggr2YaBHHirJeJADXMfkH4BIErKK9lJ+AKVaDp5r SYSpQ2Pl1J9kbHtGK+tDQsvhwGQNF0rNJJjjCK9rg8cP7443T6/A1tDM1g5M7sfQcE06 HX6cUHN5kHcI/OKUpzR3IBhnUrMWJbpWv3UillRi8yxfPqQl/QHv4UfGl7PUCqX2nm+D U/JtPAg/48hL5NhfftJ4rE8/nCttTeHrBKksDZPJQTlqMeS1P+7si7DojSHoNa336kW7 72DsCumEdXJVlkm3wdnfgmjufviAXywCQt9RrGc5n4t01eTuV3014Bx6vH6KgdzOWLAU KKbA== X-Gm-Message-State: AO0yUKWXymqzq3RScd3EpFWn34bq981pNPuocCCJWqugA+H/h0LZ3KcM 8rZOUNO1m/ig1yzkP3fpFaE/bw== X-Google-Smtp-Source: AK7set9N6OXBd89JzPHrqu15NAgJtTXIO8rAEPNule/gn66F+3qLMRl53iaOB7E+zcMNN668hoyKoA== X-Received: by 2002:a7b:ce94:0:b0:3dc:43a0:83bb with SMTP id q20-20020a7bce94000000b003dc43a083bbmr2087275wmj.3.1675256368023; Wed, 01 Feb 2023 04:59:28 -0800 (PST) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id m15-20020a056000024f00b002bfae16ee2fsm17972811wrz.111.2023.02.01.04.59.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 04:59:27 -0800 (PST) From: Jean-Philippe Brucker To: maz@kernel.org, catalin.marinas@arm.com, will@kernel.org, joro@8bytes.org Cc: robin.murphy@arm.com, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com, smostafa@google.com, dbrazdil@google.com, ryan.roberts@arm.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, iommu@lists.linux.dev, Jean-Philippe Brucker Subject: [RFC PATCH 11/45] KVM: arm64: pkvm: Expose pkvm_admit_host_page() Date: Wed, 1 Feb 2023 12:52:55 +0000 Message-Id: <20230201125328.2186498-12-jean-philippe@linaro.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230201125328.2186498-1-jean-philippe@linaro.org> References: <20230201125328.2186498-1-jean-philippe@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230201_125901_499234_819A619B X-CRM114-Status: GOOD ( 17.56 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Since the IOMMU driver will need admit_host_page(), make it non static. As a result we can drop refill_memcache() and call admit_host_page() directly from pkvm_refill_memcache(). Signed-off-by: Jean-Philippe Brucker --- arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 2 -- arch/arm64/kvm/hyp/include/nvhe/mm.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 16 ++++++++--- arch/arm64/kvm/hyp/nvhe/mm.c | 27 +++++-------------- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h index 40decbe4cc70..d4f4ffbb7dbb 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h @@ -83,8 +83,6 @@ void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt); int hyp_pin_shared_mem(void *from, void *to); void hyp_unpin_shared_mem(void *from, void *to); void reclaim_guest_pages(struct pkvm_hyp_vm *vm, struct kvm_hyp_memcache *mc); -int refill_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages, - struct kvm_hyp_memcache *host_mc); void *pkvm_map_donated_memory(unsigned long host_va, size_t size); void pkvm_unmap_donated_memory(void *va, size_t size); diff --git a/arch/arm64/kvm/hyp/include/nvhe/mm.h b/arch/arm64/kvm/hyp/include/nvhe/mm.h index 84db840f2057..a8c46a0ebc4a 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/mm.h +++ b/arch/arm64/kvm/hyp/include/nvhe/mm.h @@ -26,6 +26,7 @@ int pkvm_create_mappings_locked(void *from, void *to, enum kvm_pgtable_prot prot int __pkvm_create_private_mapping(phys_addr_t phys, size_t size, enum kvm_pgtable_prot prot, unsigned long *haddr); +void *pkvm_admit_host_page(struct kvm_hyp_memcache *mc); int pkvm_alloc_private_va_range(size_t size, unsigned long *haddr); int pkvm_create_hyp_device_mapping(u64 base, u64 size, void __iomem *haddr); diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c index e8328f54200e..29ce7b09edbb 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -766,14 +766,24 @@ static void handle___kvm_vcpu_run(struct kvm_cpu_context *host_ctxt) cpu_reg(host_ctxt, 1) = ret; } +static void *admit_host_page(void *arg) +{ + return pkvm_admit_host_page(arg); +} + static int pkvm_refill_memcache(struct pkvm_hyp_vcpu *hyp_vcpu) { + int ret; struct pkvm_hyp_vm *hyp_vm = pkvm_hyp_vcpu_to_hyp_vm(hyp_vcpu); u64 nr_pages = VTCR_EL2_LVLS(hyp_vm->kvm.arch.vtcr) - 1; - struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; + struct kvm_hyp_memcache host_mc = hyp_vcpu->host_vcpu->arch.pkvm_memcache; + + ret = __topup_hyp_memcache(&hyp_vcpu->vcpu.arch.pkvm_memcache, + nr_pages, admit_host_page, + hyp_virt_to_phys, &host_mc); - return refill_memcache(&hyp_vcpu->vcpu.arch.pkvm_memcache, nr_pages, - &host_vcpu->arch.pkvm_memcache); + hyp_vcpu->host_vcpu->arch.pkvm_memcache = host_mc; + return ret; } static void handle___pkvm_host_map_guest(struct kvm_cpu_context *host_ctxt) diff --git a/arch/arm64/kvm/hyp/nvhe/mm.c b/arch/arm64/kvm/hyp/nvhe/mm.c index 318298eb3d6b..9daaf2b2b191 100644 --- a/arch/arm64/kvm/hyp/nvhe/mm.c +++ b/arch/arm64/kvm/hyp/nvhe/mm.c @@ -340,35 +340,20 @@ int hyp_create_idmap(u32 hyp_va_bits) return __pkvm_create_mappings(start, end - start, start, PAGE_HYP_EXEC); } -static void *admit_host_page(void *arg) +void *pkvm_admit_host_page(struct kvm_hyp_memcache *mc) { - struct kvm_hyp_memcache *host_mc = arg; - - if (!host_mc->nr_pages) + if (!mc->nr_pages) return NULL; /* * The host still owns the pages in its memcache, so we need to go * through a full host-to-hyp donation cycle to change it. Fortunately, * __pkvm_host_donate_hyp() takes care of races for us, so if it - * succeeds we're good to go. + * succeeds we're good to go. Because mc is a copy of the memcache + * struct, the host cannot modify mc->head between donate and pop. */ - if (__pkvm_host_donate_hyp(hyp_phys_to_pfn(host_mc->head), 1)) + if (__pkvm_host_donate_hyp(hyp_phys_to_pfn(mc->head), 1)) return NULL; - return pop_hyp_memcache(host_mc, hyp_phys_to_virt); -} - -/* Refill our local memcache by poping pages from the one provided by the host. */ -int refill_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages, - struct kvm_hyp_memcache *host_mc) -{ - struct kvm_hyp_memcache tmp = *host_mc; - int ret; - - ret = __topup_hyp_memcache(mc, min_pages, admit_host_page, - hyp_virt_to_phys, &tmp); - *host_mc = tmp; - - return ret; + return pop_hyp_memcache(mc, hyp_phys_to_virt); }