From patchwork Thu Nov 10 19:02:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 13039235 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 04855C4332F for ; Thu, 10 Nov 2022 19:25:07 +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=s9TMHbFbonzFFg5dDhJv6c5yAvkoIBTIYvNAQbeC5cg=; b=naoonw3NMmFVHO VO9k8Dn48EPIvl/JozXZ1ekWMllX4Sobwmi3/dOSUT7B9im1kY6zryK2WtZz88GC3HnJogmkLGpNB 58h8AUpFtc0VuaX7S0sTbQ9IgaG1hJfH5B9B3MtiALrYHy9N2m06dTEp57hnx1OxC2eWgrSAs5ajw KDxGdVHTHYiznZXL48YkgtX7aZcuimGp6/NqB8F4onaoluWVBo/YP3HZ9hojQR8Z6DU2dlMbQZ1B4 v0chXN1SDbiCIH2QAJR65HvEKFB4yt6XvGAqSBgkc67uclkR9cUgy4uKw2hzAes6MsASfRmJXIMtl p7z3RSaYrP94MnTzsxqA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1otD9W-008gBm-Tx; Thu, 10 Nov 2022 19:23:55 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1otCqv-008Swt-Di for linux-arm-kernel@lists.infradead.org; Thu, 10 Nov 2022 19:04:43 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E1D9B61E13; Thu, 10 Nov 2022 19:04:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8ECFDC433C1; Thu, 10 Nov 2022 19:04:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668107080; bh=1KJO8iA6xVHfnIq1ZFiL1RKJbbbrtLlsLB0pcQj4/Fg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fCKAIv8kfapc/4oI1qg4JSMH5Tcj8oRkVLHN7Euh5YzyrbRnSUFi0l1afqx2weURZ XAjXaedJjmC0SsrwVibCLAQepYzCTdbbLzHw6o9JWrtSquxV1Ej30A6FNpHOdfPQFo D6dghI9YLR8axfJV8dVlTG8wIjLGR21cvIWKmAHNbHR+MxiG/buz+HsyFDRnZZAdzB d4vkltFL+wTjGraAxRPeHdEl3cFiEnMR4CYhScyEGdCMK0DgAz0Ba/7IUJGoY+QmyR 1pXGuicM9R+JpR2Aiu+BjuwYzAPhVkVu1Wm/Yij2N7FRDvNHJjEp8H5tCZDRBEZ7Fl k8oxN92B54a7w== From: Will Deacon To: kvmarm@lists.linux.dev Cc: Will Deacon , Sean Christopherson , Vincent Donnefort , Alexandru Elisei , Catalin Marinas , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , James Morse , Chao Peng , Quentin Perret , Suzuki K Poulose , Mark Rutland , Fuad Tabba , Oliver Upton , Marc Zyngier , kernel-team@android.com, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v6 25/26] KVM: arm64: Clean out the odd handling of completer_addr Date: Thu, 10 Nov 2022 19:02:58 +0000 Message-Id: <20221110190259.26861-26-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20221110190259.26861-1-will@kernel.org> References: <20221110190259.26861-1-will@kernel.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221110_110441_600559_08D92F39 X-CRM114-Status: GOOD ( 17.08 ) 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 From: Oliver Upton The layout of struct pkvm_mem_transition is a bit weird; the destination address for the transition is actually stashed in the initiator address context. Even weirder so, that address is thrown inside a union and return from helpers by use of an out pointer. Rip out the whole mess and move the destination address into the destination context sub-struct. No functional change intended. Signed-off-by: Oliver Upton [will: Extended to include host/hyp donation paths] Signed-off-by: Will Deacon --- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 130 ++++++++++---------------- 1 file changed, 48 insertions(+), 82 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c index 94cd48f7850e..dfeddaf2a462 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -534,20 +534,12 @@ struct pkvm_mem_transition { enum pkvm_component_id id; /* Address in the initiator's address space */ u64 addr; - - union { - struct { - /* Address in the completer's address space */ - u64 completer_addr; - } host; - struct { - u64 completer_addr; - } hyp; - }; } initiator; struct { enum pkvm_component_id id; + /* Address in the completer's address space */ + u64 addr; } completer; }; @@ -619,53 +611,43 @@ static int __host_set_page_state_range(u64 addr, u64 size, return host_stage2_idmap_locked(addr, size, prot); } -static int host_request_owned_transition(u64 *completer_addr, - const struct pkvm_mem_transition *tx) +static int host_request_owned_transition(const struct pkvm_mem_transition *tx) { u64 size = tx->nr_pages * PAGE_SIZE; u64 addr = tx->initiator.addr; - *completer_addr = tx->initiator.host.completer_addr; return __host_check_page_state_range(addr, size, PKVM_PAGE_OWNED); } -static int host_request_unshare(u64 *completer_addr, - const struct pkvm_mem_transition *tx) +static int host_request_unshare(const struct pkvm_mem_transition *tx) { u64 size = tx->nr_pages * PAGE_SIZE; u64 addr = tx->initiator.addr; - *completer_addr = tx->initiator.host.completer_addr; return __host_check_page_state_range(addr, size, PKVM_PAGE_SHARED_OWNED); } -static int host_initiate_share(u64 *completer_addr, - const struct pkvm_mem_transition *tx) +static int host_initiate_share(const struct pkvm_mem_transition *tx) { u64 size = tx->nr_pages * PAGE_SIZE; u64 addr = tx->initiator.addr; - *completer_addr = tx->initiator.host.completer_addr; return __host_set_page_state_range(addr, size, PKVM_PAGE_SHARED_OWNED); } -static int host_initiate_unshare(u64 *completer_addr, - const struct pkvm_mem_transition *tx) +static int host_initiate_unshare(const struct pkvm_mem_transition *tx) { u64 size = tx->nr_pages * PAGE_SIZE; u64 addr = tx->initiator.addr; - *completer_addr = tx->initiator.host.completer_addr; return __host_set_page_state_range(addr, size, PKVM_PAGE_OWNED); } -static int host_initiate_donation(u64 *completer_addr, - const struct pkvm_mem_transition *tx) +static int host_initiate_donation(const struct pkvm_mem_transition *tx) { u8 owner_id = tx->completer.id; u64 size = tx->nr_pages * PAGE_SIZE; - *completer_addr = tx->initiator.host.completer_addr; return host_stage2_set_owner_locked(tx->initiator.addr, size, owner_id); } @@ -686,17 +668,17 @@ static int __host_ack_transition(u64 addr, const struct pkvm_mem_transition *tx, return __host_check_page_state_range(addr, size, state); } -static int host_ack_donation(u64 addr, const struct pkvm_mem_transition *tx) +static int host_ack_donation(const struct pkvm_mem_transition *tx) { - return __host_ack_transition(addr, tx, PKVM_NOPAGE); + return __host_ack_transition(tx->completer.addr, tx, PKVM_NOPAGE); } -static int host_complete_donation(u64 addr, const struct pkvm_mem_transition *tx) +static int host_complete_donation(const struct pkvm_mem_transition *tx) { u64 size = tx->nr_pages * PAGE_SIZE; u8 host_id = tx->completer.id; - return host_stage2_set_owner_locked(addr, size, host_id); + return host_stage2_set_owner_locked(tx->completer.addr, size, host_id); } static enum pkvm_page_state hyp_get_page_state(kvm_pte_t pte) @@ -719,23 +701,19 @@ static int __hyp_check_page_state_range(u64 addr, u64 size, return check_page_state_range(&pkvm_pgtable, addr, size, &d); } -static int hyp_request_donation(u64 *completer_addr, - const struct pkvm_mem_transition *tx) +static int hyp_request_donation(const struct pkvm_mem_transition *tx) { u64 size = tx->nr_pages * PAGE_SIZE; u64 addr = tx->initiator.addr; - *completer_addr = tx->initiator.hyp.completer_addr; return __hyp_check_page_state_range(addr, size, PKVM_PAGE_OWNED); } -static int hyp_initiate_donation(u64 *completer_addr, - const struct pkvm_mem_transition *tx) +static int hyp_initiate_donation(const struct pkvm_mem_transition *tx) { u64 size = tx->nr_pages * PAGE_SIZE; int ret; - *completer_addr = tx->initiator.hyp.completer_addr; ret = kvm_pgtable_hyp_unmap(&pkvm_pgtable, tx->initiator.addr, size); return (ret != size) ? -EFAULT : 0; } @@ -746,7 +724,7 @@ static bool __hyp_ack_skip_pgtable_check(const struct pkvm_mem_transition *tx) tx->initiator.id != PKVM_ID_HOST); } -static int hyp_ack_share(u64 addr, const struct pkvm_mem_transition *tx, +static int hyp_ack_share(const struct pkvm_mem_transition *tx, enum kvm_pgtable_prot perms) { u64 size = tx->nr_pages * PAGE_SIZE; @@ -757,12 +735,12 @@ static int hyp_ack_share(u64 addr, const struct pkvm_mem_transition *tx, if (__hyp_ack_skip_pgtable_check(tx)) return 0; - return __hyp_check_page_state_range(addr, size, PKVM_NOPAGE); + return __hyp_check_page_state_range(tx->completer.addr, size, PKVM_NOPAGE); } -static int hyp_ack_unshare(u64 addr, const struct pkvm_mem_transition *tx) +static int hyp_ack_unshare(const struct pkvm_mem_transition *tx) { - u64 size = tx->nr_pages * PAGE_SIZE; + u64 size = tx->nr_pages * PAGE_SIZE, addr = tx->completer.addr; if (tx->initiator.id == PKVM_ID_HOST && hyp_page_count((void *)addr)) return -EBUSY; @@ -774,38 +752,40 @@ static int hyp_ack_unshare(u64 addr, const struct pkvm_mem_transition *tx) PKVM_PAGE_SHARED_BORROWED); } -static int hyp_ack_donation(u64 addr, const struct pkvm_mem_transition *tx) +static int hyp_ack_donation(const struct pkvm_mem_transition *tx) { u64 size = tx->nr_pages * PAGE_SIZE; if (__hyp_ack_skip_pgtable_check(tx)) return 0; - return __hyp_check_page_state_range(addr, size, PKVM_NOPAGE); + return __hyp_check_page_state_range(tx->completer.addr, size, + PKVM_NOPAGE); } -static int hyp_complete_share(u64 addr, const struct pkvm_mem_transition *tx, +static int hyp_complete_share(const struct pkvm_mem_transition *tx, enum kvm_pgtable_prot perms) { - void *start = (void *)addr, *end = start + (tx->nr_pages * PAGE_SIZE); + void *start = (void *)tx->completer.addr; + void *end = start + (tx->nr_pages * PAGE_SIZE); enum kvm_pgtable_prot prot; prot = pkvm_mkstate(perms, PKVM_PAGE_SHARED_BORROWED); return pkvm_create_mappings_locked(start, end, prot); } -static int hyp_complete_unshare(u64 addr, const struct pkvm_mem_transition *tx) +static int hyp_complete_unshare(const struct pkvm_mem_transition *tx) { u64 size = tx->nr_pages * PAGE_SIZE; - int ret = kvm_pgtable_hyp_unmap(&pkvm_pgtable, addr, size); + int ret = kvm_pgtable_hyp_unmap(&pkvm_pgtable, tx->completer.addr, size); return (ret != size) ? -EFAULT : 0; } -static int hyp_complete_donation(u64 addr, - const struct pkvm_mem_transition *tx) +static int hyp_complete_donation(const struct pkvm_mem_transition *tx) { - void *start = (void *)addr, *end = start + (tx->nr_pages * PAGE_SIZE); + void *start = (void *)tx->completer.addr; + void *end = start + (tx->nr_pages * PAGE_SIZE); enum kvm_pgtable_prot prot = pkvm_mkstate(PAGE_HYP, PKVM_PAGE_OWNED); return pkvm_create_mappings_locked(start, end, prot); @@ -814,12 +794,11 @@ static int hyp_complete_donation(u64 addr, static int check_share(struct pkvm_mem_share *share) { const struct pkvm_mem_transition *tx = &share->tx; - u64 completer_addr; int ret; switch (tx->initiator.id) { case PKVM_ID_HOST: - ret = host_request_owned_transition(&completer_addr, tx); + ret = host_request_owned_transition(tx); break; default: ret = -EINVAL; @@ -830,7 +809,7 @@ static int check_share(struct pkvm_mem_share *share) switch (tx->completer.id) { case PKVM_ID_HYP: - ret = hyp_ack_share(completer_addr, tx, share->completer_prot); + ret = hyp_ack_share(tx, share->completer_prot); break; default: ret = -EINVAL; @@ -842,12 +821,11 @@ static int check_share(struct pkvm_mem_share *share) static int __do_share(struct pkvm_mem_share *share) { const struct pkvm_mem_transition *tx = &share->tx; - u64 completer_addr; int ret; switch (tx->initiator.id) { case PKVM_ID_HOST: - ret = host_initiate_share(&completer_addr, tx); + ret = host_initiate_share(tx); break; default: ret = -EINVAL; @@ -858,7 +836,7 @@ static int __do_share(struct pkvm_mem_share *share) switch (tx->completer.id) { case PKVM_ID_HYP: - ret = hyp_complete_share(completer_addr, tx, share->completer_prot); + ret = hyp_complete_share(tx, share->completer_prot); break; default: ret = -EINVAL; @@ -890,12 +868,11 @@ static int do_share(struct pkvm_mem_share *share) static int check_unshare(struct pkvm_mem_share *share) { const struct pkvm_mem_transition *tx = &share->tx; - u64 completer_addr; int ret; switch (tx->initiator.id) { case PKVM_ID_HOST: - ret = host_request_unshare(&completer_addr, tx); + ret = host_request_unshare(tx); break; default: ret = -EINVAL; @@ -906,7 +883,7 @@ static int check_unshare(struct pkvm_mem_share *share) switch (tx->completer.id) { case PKVM_ID_HYP: - ret = hyp_ack_unshare(completer_addr, tx); + ret = hyp_ack_unshare(tx); break; default: ret = -EINVAL; @@ -918,12 +895,11 @@ static int check_unshare(struct pkvm_mem_share *share) static int __do_unshare(struct pkvm_mem_share *share) { const struct pkvm_mem_transition *tx = &share->tx; - u64 completer_addr; int ret; switch (tx->initiator.id) { case PKVM_ID_HOST: - ret = host_initiate_unshare(&completer_addr, tx); + ret = host_initiate_unshare(tx); break; default: ret = -EINVAL; @@ -934,7 +910,7 @@ static int __do_unshare(struct pkvm_mem_share *share) switch (tx->completer.id) { case PKVM_ID_HYP: - ret = hyp_complete_unshare(completer_addr, tx); + ret = hyp_complete_unshare(tx); break; default: ret = -EINVAL; @@ -966,15 +942,14 @@ static int do_unshare(struct pkvm_mem_share *share) static int check_donation(struct pkvm_mem_donation *donation) { const struct pkvm_mem_transition *tx = &donation->tx; - u64 completer_addr; int ret; switch (tx->initiator.id) { case PKVM_ID_HOST: - ret = host_request_owned_transition(&completer_addr, tx); + ret = host_request_owned_transition(tx); break; case PKVM_ID_HYP: - ret = hyp_request_donation(&completer_addr, tx); + ret = hyp_request_donation(tx); break; default: ret = -EINVAL; @@ -985,10 +960,10 @@ static int check_donation(struct pkvm_mem_donation *donation) switch (tx->completer.id) { case PKVM_ID_HOST: - ret = host_ack_donation(completer_addr, tx); + ret = host_ack_donation(tx); break; case PKVM_ID_HYP: - ret = hyp_ack_donation(completer_addr, tx); + ret = hyp_ack_donation(tx); break; default: ret = -EINVAL; @@ -1000,15 +975,14 @@ static int check_donation(struct pkvm_mem_donation *donation) static int __do_donate(struct pkvm_mem_donation *donation) { const struct pkvm_mem_transition *tx = &donation->tx; - u64 completer_addr; int ret; switch (tx->initiator.id) { case PKVM_ID_HOST: - ret = host_initiate_donation(&completer_addr, tx); + ret = host_initiate_donation(tx); break; case PKVM_ID_HYP: - ret = hyp_initiate_donation(&completer_addr, tx); + ret = hyp_initiate_donation(tx); break; default: ret = -EINVAL; @@ -1019,10 +993,10 @@ static int __do_donate(struct pkvm_mem_donation *donation) switch (tx->completer.id) { case PKVM_ID_HOST: - ret = host_complete_donation(completer_addr, tx); + ret = host_complete_donation(tx); break; case PKVM_ID_HYP: - ret = hyp_complete_donation(completer_addr, tx); + ret = hyp_complete_donation(tx); break; default: ret = -EINVAL; @@ -1062,12 +1036,10 @@ int __pkvm_host_share_hyp(u64 pfn) .initiator = { .id = PKVM_ID_HOST, .addr = host_addr, - .host = { - .completer_addr = hyp_addr, - }, }, .completer = { .id = PKVM_ID_HYP, + .addr = hyp_addr, }, }, .completer_prot = PAGE_HYP, @@ -1095,12 +1067,10 @@ int __pkvm_host_unshare_hyp(u64 pfn) .initiator = { .id = PKVM_ID_HOST, .addr = host_addr, - .host = { - .completer_addr = hyp_addr, - }, }, .completer = { .id = PKVM_ID_HYP, + .addr = hyp_addr, }, }, .completer_prot = PAGE_HYP, @@ -1128,12 +1098,10 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages) .initiator = { .id = PKVM_ID_HOST, .addr = host_addr, - .host = { - .completer_addr = hyp_addr, - }, }, .completer = { .id = PKVM_ID_HYP, + .addr = hyp_addr, }, }, }; @@ -1160,12 +1128,10 @@ int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages) .initiator = { .id = PKVM_ID_HYP, .addr = hyp_addr, - .hyp = { - .completer_addr = host_addr, - }, }, .completer = { .id = PKVM_ID_HOST, + .addr = host_addr, }, }, };