From patchwork Tue Oct 19 12:13:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12569757 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7747FC433EF for ; Tue, 19 Oct 2021 12:22:21 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 3CD6260FDA for ; Tue, 19 Oct 2021 12:22:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 3CD6260FDA Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org 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:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=5axwvHDqgYC4M7OWgDZvINSW4LQwD483Rpwj8v5MeJ0=; b=xQ+ME9OljQwMrr46ZgkkGLWR4F nOj+BIr46TSndo8jvMuQ6bxzYCnz6Db+X3AinlYOLFH+1dwsGaucWH2jYLZzhRKw9ixdG+xlXzLbs 9qCi9hhmyDTw4AJdp6TTKOQKjMat99CjzJrOf8L8wIq2wbky9lv18WisR+o6wMkImzmIch7b3OLab NTzlhygvWtfpjipSOEUy18sdTL7q/i4p7YQr90wh3Z0maLsQl08WqCxyYrv7yvZn1f7GNpggMInke N4EFXWp2WnhTDPrwk4b+NJkGcmUIhbTfo5agUR+JqQvYzhkqYXeIe0TTBpd+1aGhOSZfb4fOFa72I LxsMOYzA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mco6N-0018UH-Cg; Tue, 19 Oct 2021 12:20:20 +0000 Received: from mail-lj1-x249.google.com ([2a00:1450:4864:20::249]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mcnzw-0015Nu-WD for linux-arm-kernel@lists.infradead.org; Tue, 19 Oct 2021 12:13:42 +0000 Received: by mail-lj1-x249.google.com with SMTP id j8-20020a2e3c08000000b00210e2890d61so812766lja.9 for ; Tue, 19 Oct 2021 05:13:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=ISeivE4VJePhwCDFvsBtzOVIerKBvyMpPq/U4suJ8Dg=; b=pMTyZ0Vbl+u7Dlf64iUAHAL8iII5oExKlGMnTMHZaH1N3KOh6pszIEURsP71VOA8zm YmAfLbs73BpmIWiQFsAwuQdZch7d1aBX0VE1dqPVlo4MR+Iv3SEIvK1dzqA44YUBqokQ 3BWhGEojgqF2KRv1G62YRc39Vn8Nnjta2ATq8LApIpEt7DGix0abtEW9pGr56bx76kdj 22z6U+fFymB5GMOz4WZX6L7B8vnwJ+yGDZM1gxOHNZEdOyXICg/04hJWqSYDUUhm5VGZ 8XPgn1dhN12ntFAgc27i+8EKZxW4j4Rv26/Vjwmr2QvI0pNoKwfj6Pef3gBLMRK7blQh duHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ISeivE4VJePhwCDFvsBtzOVIerKBvyMpPq/U4suJ8Dg=; b=K9nrhC8naorM7SJM0ZoLkemNGFBCd7qDbojrSFo3b9TFIa+sa2kDOTPMGSUTdiCb+h UlOFYuTl6wT8CHUsXsTM/N1qrItHo15tbyZ9Kq1Rs2f7750lYbTXr83KujO3j4WKvxdt TlY8ig7gFIwifULW5fx4FGSuwc48azRrwO/wjFgu4QQtiubB5QUYkROKvCbOPDdC96GV 4gIdDXVictW9Jh0TNRFPm17EOgpcH8/ZrxO7VzdX1zXp01/tpTyqmm7F2PwRY9kwtDeO xvfWIxmAkyRXNRT+Dv53W74CtUGCLrwVqAq5cVb8miA1BOVBw1EtTes7+67Y8GPxbnbH qRrg== X-Gm-Message-State: AOAM530Aly1lRC/HbigNV8brFDxb3mpCxxaHqRiN/hZkhNpV7IZFu7gC 5Dcue2zL13kKrNXEx90mTAwN5JtiPDhn X-Google-Smtp-Source: ABdhPJwgKkmhxiB5XvxU66pG49kdREnEBCBJUkS8jWIoTxJjPaFPdF/auvzHlo/liyhgODAIlIOug/KJN2Yd X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:59ca:401f:83a8:de6d]) (user=qperret job=sendgmr) by 2002:a2e:7816:: with SMTP id t22mr6884979ljc.133.1634645617143; Tue, 19 Oct 2021 05:13:37 -0700 (PDT) Date: Tue, 19 Oct 2021 13:13:02 +0100 In-Reply-To: <20211019121304.2732332-1-qperret@google.com> Message-Id: <20211019121304.2732332-14-qperret@google.com> Mime-Version: 1.0 References: <20211019121304.2732332-1-qperret@google.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH v2 13/15] KVM: arm64: Implement do_unshare() helper for unsharing memory From: Quentin Perret To: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Will Deacon , Fuad Tabba , David Brazdil , Andrew Walbran Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kernel-team@android.com, qperret@google.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211019_051341_111975_50A9B0AA X-CRM114-Status: GOOD ( 13.43 ) 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: Will Deacon Tearing down a previously shared memory region results in the borrower losing access to the underlying pages and returning them to the "owned" state in the owner. Implement a do_unshare() helper, along the same lines as do_share(), to provide this functionality for the host-to-hyp case. Signed-off-by: Will Deacon Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 115 ++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c index ab1732c38b3b..205616c9f107 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -474,6 +474,16 @@ static int host_request_owned_transition(u64 *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) +{ + 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) { @@ -484,6 +494,16 @@ static int host_initiate_share(u64 *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) +{ + 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 enum pkvm_page_state hyp_get_page_state(kvm_pte_t pte) { if (!kvm_pte_valid(pte)) @@ -524,6 +544,17 @@ static int hyp_ack_share(u64 addr, const struct pkvm_mem_transition *tx, return __hyp_check_page_state_range(addr, size, PKVM_NOPAGE); } +static int hyp_ack_unshare(u64 addr, 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_PAGE_SHARED_BORROWED); +} + static int hyp_complete_share(u64 addr, const struct pkvm_mem_transition *tx, enum kvm_pgtable_prot perms) { @@ -534,6 +565,14 @@ static int hyp_complete_share(u64 addr, const struct pkvm_mem_transition *tx, return pkvm_create_mappings_locked(start, end, prot); } +static int hyp_complete_unshare(u64 addr, const struct pkvm_mem_transition *tx) +{ + u64 size = tx->nr_pages * PAGE_SIZE; + int ret = kvm_pgtable_hyp_unmap(&pkvm_pgtable, addr, size); + + return (ret != size) ? -EFAULT : 0; +} + static int check_share(struct pkvm_mem_share *share) { const struct pkvm_mem_transition *tx = &share->tx; @@ -610,6 +649,82 @@ static int do_share(struct pkvm_mem_share *share) return WARN_ON(__do_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); + break; + default: + ret = -EINVAL; + } + + if (ret) + return ret; + + switch (tx->completer.id) { + case PKVM_ID_HYP: + ret = hyp_ack_unshare(completer_addr, tx); + break; + default: + ret = -EINVAL; + } + + return ret; +} + +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); + break; + default: + ret = -EINVAL; + } + + if (ret) + return ret; + + switch (tx->completer.id) { + case PKVM_ID_HYP: + ret = hyp_complete_unshare(completer_addr, tx); + break; + default: + ret = -EINVAL; + } + + return ret; +} + +/* + * do_unshare(): + * + * The page owner revokes access from another component for a range of + * pages which were previously shared using do_share(). + * + * Initiator: SHARED_OWNED => OWNED + * Completer: SHARED_BORROWED => NOPAGE + */ +static int do_unshare(struct pkvm_mem_share *share) +{ + int ret; + + ret = check_unshare(share); + if (ret) + return ret; + + return WARN_ON(__do_unshare(share)); +} + int __pkvm_host_share_hyp(u64 pfn) { int ret;