From patchwork Wed Dec 15 16:12:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12696309 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 2B190C433F5 for ; Wed, 15 Dec 2021 16:37:08 +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: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=YBldWurwexN56tOkqXZGJYHKh3bufUvoviW+rQvx5iE=; b=ofY2/Jz45Vt9BJRWPH1cgrr1v1 WZZmkGqe1XV0VJw36M53iaiD3lfqsVaXFatZ3juzQ9D+adOpAXk/X3vUs9zfHI7B2cNaLIN/8j2G0 mKdGr+yVm2HxfAThfWZvTUQrsbRNFmsPfUAds95+0tdkKrlXGZJbw3oMtDbBKXk/qVk/WzZDeHB59 LM7rbOeVinwexg9fzAj8XMS/vra8FhknPdzqtXerXjRlkKlydLasEO0RikvbtgnszD9Bfpz2udOHl wVFrMRqVaycPGqldFSFJBby6MdaWYxspCKws7K9ZNptXz7UAhUiBortiRwA188WQuBbZFDNey8xPq /z6F0uIQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mxXCp-001k5G-TE; Wed, 15 Dec 2021 16:32:40 +0000 Received: from mail-wm1-x34a.google.com ([2a00:1450:4864:20::34a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mxWuH-001cLF-5A for linux-arm-kernel@lists.infradead.org; Wed, 15 Dec 2021 16:13:31 +0000 Received: by mail-wm1-x34a.google.com with SMTP id b75-20020a1c804e000000b0034569bde713so967807wmd.9 for ; Wed, 15 Dec 2021 08:13:28 -0800 (PST) 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=1Uv+uSK+k6gp/yCPD9bTsZMOeiUHXNuD9f7KA3qC+RU=; b=KHgYwK5BuPaiya24VhTskfkiHXY2KhERyO0Ns6i1bSlMuj6flpuQDquCIk2QoDFenC qTmtKo85C3U8k5CIbTtsPzVuIsDG0EzIcMAMs8xXZutQe60JwvibzIwGDQfXZrKXe9ip QOIdy2AkmCY6IoD11KQzFsIEcK0g0LRW1VCgr3FiPgyv3tZrI0lLtvljJpAS5pUS5VFe TjWXW9mB7oo0J5n+gli4u5slLrAbdAL3Hq+LMILIxsmiEb3tiuUplb0yyuM9saLdY1cd fAAII2+k49Xx7Lf+1kcWMi+20d3kSa1LrMGGBC90mBWw4Va6TToTO8C4rtUgIHTVn8ac MZMw== 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=1Uv+uSK+k6gp/yCPD9bTsZMOeiUHXNuD9f7KA3qC+RU=; b=iACofBC+aD4Ig+dYx16m9QEfczrIG1Xm5pdN7FqH7KZJw1ekajkOBUD8vpDWbSEi/K lDmFR+sbE7d3jlLt1NotqQy8q0jT+ppLZlMUQGeAZv/0FMEJazBWtChX4xdjJRGSABOJ iKXLvP1pxI9xJsx/+R2OXHzYqKOPXHChbiPxIp5YJpuDhS1evTMch64asEVmrU8e5OCC wELvTk377Dt4qcfln0PEAuZ776q/ytB2B0/GAcgh9DQGBXPQOB/9xd1d5RC0hW/h1FNk rjaik8YNjnf6rhllB+0YKVedP6IkzDFCnK8veu7gS00f8RGWQoPI+fDbSvLlLNR0BvtR 3w7w== X-Gm-Message-State: AOAM5322Q7vHHFP5Dby89R9BmpiDLyGCI4Op+7gLDoS9aNL45TzK/e8J Q9101W275E2uUzu93bi3CbOiXV+Jx9cG X-Google-Smtp-Source: ABdhPJzmVQ2X/pz8yOEnI/iDk9eDx3O/fvoBqSDacRJg+34A6UuREUZ3WUgDTLdGeCW+BXycOlr3EccUsebH X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:fc03:4f5b:4e9b:3ec1]) (user=qperret job=sendgmr) by 2002:a05:600c:4104:: with SMTP id j4mr540003wmi.178.1639584806973; Wed, 15 Dec 2021 08:13:26 -0800 (PST) Date: Wed, 15 Dec 2021 16:12:29 +0000 In-Reply-To: <20211215161232.1480836-1-qperret@google.com> Message-Id: <20211215161232.1480836-13-qperret@google.com> Mime-Version: 1.0 References: <20211215161232.1480836-1-qperret@google.com> X-Mailer: git-send-email 2.34.1.173.g76aa8bc2d0-goog Subject: [PATCH v4 12/14] 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 Cc: qperret@google.com, qwandor@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211215_081329_275275_F24E53CD X-CRM114-Status: GOOD ( 12.84 ) 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. Reviewed-by: Andrew Walbran 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 14823e318585..2b23b2db8d4a 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -486,6 +486,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) { @@ -496,6 +506,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)) @@ -536,6 +556,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) { @@ -546,6 +577,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; @@ -622,6 +661,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;