Received: by 2002:ab2:784b:0:b0:1fd:adc2:8405 with SMTP id m11csp369823lqp; Mon, 10 Jun 2024 06:46:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU970akByWl4SXHq5GYdte1dCK81EC5UQYrn6sRGjiHkWQeH32/e8+qnq5oBbKgiCJwK9y0sMHOCEf7GNzHx8DWUQzwknw1eSGU54i1sw== X-Google-Smtp-Source: AGHT+IHwblwctbzqy4SX/okaml6ErPTMxJSlW6TxI36jRrT7hGusJt1GZGN1y1C8BoXel0Q44rKz X-Received: by 2002:a05:6808:f01:b0:3d2:297d:3c2c with SMTP id 5614622812f47-3d2297d3f03mr3421158b6e.51.1718027173638; Mon, 10 Jun 2024 06:46:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718027173; cv=pass; d=google.com; s=arc-20160816; b=fX2lf3POLak/uQmI31MISge2WR1EdPtKZJtvxY/Wimsy1l3A3oh58Uu2R2ht3TCJIM 0s78dRSQNCY2NVYcAg8TdbjiiOYLZnB+4iVk0BGFxiaLerWiR4L/as7VvRVLpQCxIsel 3GuiYXzpuBGOZ1iWI+nXz9Gn23cIhWZNaL6hzGDSpKdOAL+0lmOry5Ac2QMVE32//rx0 xWb+bcvthes8oTYwyYCQzbp/+ETsESJBc9FuXy94YE4j25yCZYvupXtomi5BWJd1Kpne 4PFZCW8y+z3An9YyQFSQor40skt6c+9rHCY00CkGLzSxqHJg0eAtht2gP3v7Xivj8hnZ fGWg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=wViaZ7usSJnK+wMFBxorlZBu2BB5+TvbeIXfHxfZuiM=; fh=NDCPdPn2hiqHaiIRDYQAzsW9PK20CTia4nECGof6enU=; b=MovGJJCv5KkJ6rvfFPRdYrGoh5TYaFUh3IFR079ftjK6+3A6CLpRCwSrkN1wthonwF aEPWUaaSfa5d2G5Rj+5nq0Vj0ENwYshhMKwfdMGPqOLSO4vfxJpSEiMayZcmoi+YH6k+ nkpspfAqzUitLPAZILBiqCY1C9WWgYWUCuc0PN2GqWB7tVILNRtYVyHNj+hudPGIXomT 0gUyV4QV+TVKl6QLHQbzgO0yI6zDIMNyEuJ8OTGT3Fhn39ea/4PomyP0USvsQZB5jtc9 R3lLG1F/CT/6fhMqagREnF9xIeX7od6a0b2ZTPb1LBuFplaZDNh3xcFoYCvop/WW+9SH LNaQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-208259-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208259-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-797839fb45dsi158495185a.537.2024.06.10.06.46.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 06:46:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-208259-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-208259-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208259-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 5AFB31C22013 for ; Mon, 10 Jun 2024 13:46:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 05A57149C76; Mon, 10 Jun 2024 13:43:02 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2522A147C6E; Mon, 10 Jun 2024 13:42:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718026981; cv=none; b=NGm6xO5/EwbEOdMsvSBrYmXGmtrzgneeUklaurfhOr7XGQrteCv5CehtAtZLWJHIY4ZjWhnv7gx0d7H3sTfzDgrq3dV5XABNHtWlF/+ZtMKMXy8V+vPeKNEb06JaB7n9MdCAd6Gm/k0i9sp449fRlVKrTHXYH5uc5/9oaZ4FQWE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718026981; c=relaxed/simple; bh=DIxNh9RLAx6GBL/bkIFqPK7BjBh9R5CNPUM/GF7UT0A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=otnTFqBjaUsamPjeqdCIuPaTiu+tIY2KN+Z6N8A6U23X6fjC7v8cyw1j9i2pAjflCTkDXXMduWWLzTEW08h/NABcfpiqHlWc1IAFZbriETW6MDixhKfNmZY4G6PEGyEM6kclZaS547kj84DW33QiWwFW1vsez4uryUITID+JuLc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com 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 19EFF106F; Mon, 10 Jun 2024 06:43:24 -0700 (PDT) Received: from e122027.cambridge.arm.com (e122027.cambridge.arm.com [10.1.35.41]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 694863F58B; Mon, 10 Jun 2024 06:42:57 -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 v3 12/43] arm64: RME: Keep a spare page delegated to the RMM Date: Mon, 10 Jun 2024 14:41:31 +0100 Message-Id: <20240610134202.54893-13-steven.price@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240610134202.54893-1-steven.price@arm.com> References: <20240610134202.54893-1-steven.price@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 2MB 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. Reviewed-by: Suzuki K Poulose Signed-off-by: Steven Price --- 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 209cd99f03dd..bd306bd7b64b 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 4d21ec5f2910..f6430d460519 100644 --- a/arch/arm64/kvm/rme.c +++ b/arch/arm64/kvm/rme.c @@ -104,6 +104,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)); @@ -286,6 +287,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; -- 2.34.1