From patchwork Fri Apr 12 08:42:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Price X-Patchwork-Id: 13627262 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 8EC67C4345F for ; Fri, 12 Apr 2024 08:46:05 +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=/VCa2jj0vu7OY60OofeYD5tTx53N2SjJb1fxzEu6ltc=; b=diltbXGSiHfx8U 9FfSJmtpRwSmtY9hLt5h/A4S5nW77sl2QyiF2pufa+uazJxpt2ggFSX0Gpul4HMaAL4ilQrVRtLJA rBKN8u6CA+VE19lEoafJ02bTlcgYd7zbj5WoPWZjhUjQw15s5cuqP/Mj7m1y0heVfJ4Cu8eDtp5ZH DE3k4PJGBH+E5SY4jjkKuHQpKOJkYIXR3Hnte6UnhJUrrgG3O8WK+QWa+P4zSZm3C2yZl2YBIyLuL DtQoIFokOKGiLMoYMTQL9rDItHJruuyRsP/gxLOtN4sU7uOUuHTyFiFX/eOcHDXjihiRvsw1wC0q3 gYWu/52EFOqKLdLYWtCw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rvCXi-0000000G9eu-0F8w; Fri, 12 Apr 2024 08:45:54 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rvCVe-0000000G7zW-2pet for linux-arm-kernel@lists.infradead.org; Fri, 12 Apr 2024 08:43:48 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B82B21576; Fri, 12 Apr 2024 01:44:15 -0700 (PDT) Received: from e112269-lin.cambridge.arm.com (e112269-lin.cambridge.arm.com [10.1.194.51]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5C5003F6C4; Fri, 12 Apr 2024 01:43:44 -0700 (PDT) From: Steven Price To: kvm@vger.kernel.org, kvmarm@lists.linux.dev Cc: Steven Price , Catalin Marinas , Marc Zyngier , Will Deacon , James Morse , Oliver Upton , Suzuki K Poulose , Zenghui Yu , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Joey Gouly , Alexandru Elisei , Christoffer Dall , Fuad Tabba , linux-coco@lists.linux.dev, Ganapatrao Kulkarni Subject: [PATCH v2 12/43] arm64: RME: Keep a spare page delegated to the RMM Date: Fri, 12 Apr 2024 09:42:38 +0100 Message-Id: <20240412084309.1733783-13-steven.price@arm.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240412084309.1733783-1-steven.price@arm.com> References: <20240412084056.1733704-1-steven.price@arm.com> <20240412084309.1733783-1-steven.price@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240412_014347_294221_20534FB3 X-CRM114-Status: GOOD ( 15.49 ) 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 Pages can only be populated/destroyed on the RMM at the 4KB granule, this requires creating the full depth of RTTs. However if the pages are going to be combined into a 4MB huge page the last RTT is only temporarily needed. Similarly when freeing memory the huge page must be temporarily split requiring temporary usage of the full depth oF RTTs. To avoid needing to perform a temporary allocation and delegation of a page for this purpose we keep a spare delegated page around. In particular this avoids the need for memory allocation while destroying the realm guest. Signed-off-by: Steven Price Reviewed-by: Suzuki K Poulose --- arch/arm64/include/asm/kvm_rme.h | 5 +++++ arch/arm64/kvm/rme.c | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/arch/arm64/include/asm/kvm_rme.h b/arch/arm64/include/asm/kvm_rme.h index cf8cc4d30364..fba85e9ce3ae 100644 --- a/arch/arm64/include/asm/kvm_rme.h +++ b/arch/arm64/include/asm/kvm_rme.h @@ -50,6 +50,9 @@ enum realm_state { * @state: The lifetime state machine for the realm * @rd: Kernel mapping of the Realm Descriptor (RD) * @params: Parameters for the RMI_REALM_CREATE command + * @spare_page: A physical page that has been delegated to the Realm world but + * is otherwise free. Used to avoid temporary allocation during + * RTT operations. * @num_aux: The number of auxiliary pages required by the RMM * @vmid: VMID to be used by the RMM for the realm * @ia_bits: Number of valid Input Address bits in the IPA @@ -60,6 +63,8 @@ struct realm { void *rd; struct realm_params *params; + phys_addr_t spare_page; + unsigned long num_aux; unsigned int vmid; unsigned int ia_bits; diff --git a/arch/arm64/kvm/rme.c b/arch/arm64/kvm/rme.c index 658d14e8d87d..9652ec6ab2fd 100644 --- a/arch/arm64/kvm/rme.c +++ b/arch/arm64/kvm/rme.c @@ -103,6 +103,7 @@ static int realm_create_rd(struct kvm *kvm) } realm->rd = rd; + realm->spare_page = PHYS_ADDR_MAX; if (WARN_ON(rmi_rec_aux_count(rd_phys, &realm->num_aux))) { WARN_ON(rmi_realm_destroy(rd_phys)); @@ -283,6 +284,13 @@ void kvm_destroy_realm(struct kvm *kvm) rme_vmid_release(realm->vmid); + if (realm->spare_page != PHYS_ADDR_MAX) { + /* Leak the page if the undelegate fails */ + if (!WARN_ON(rmi_granule_undelegate(realm->spare_page))) + free_page((unsigned long)phys_to_virt(realm->spare_page)); + realm->spare_page = PHYS_ADDR_MAX; + } + for (i = 0; i < pgt->pgd_pages; i++) { phys_addr_t pgd_phys = kvm->arch.mmu.pgd_phys + i * PAGE_SIZE;