From patchwork Wed Oct 13 15:58:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12556221 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 73FCFC433F5 for ; Wed, 13 Oct 2021 16:01:08 +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 490D5610FE for ; Wed, 13 Oct 2021 16:01:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 490D5610FE 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=J0/WmI1S6qrmr3e+AO3AqTZg/U+hc5hCScl7bVnl578=; b=syN4kiPsXKUO6IQ+H1c4RjZ0vW k9GISILnTwQvJwpM4JCphcFUACgRvScvAaA83JwgP1gJbVgPLX7HrfKTXp2sl1Iqb6GajFzbqUOvs oTUeTgOV0joyq+ru2t5K0gMF9utXP2A0kiRNDfZ2fd+fYosddlY9DwhJUgKuQ1dzPbjGyecjl75bM VMmeIMSq7fIeyoJJqCkzvtTaKYs4YX3xeKSp5W6G7KnBg+vSIWd33q0v61ysan/YxAHS9rBm6+/US hbtNE0s4CLrt23wAzv/HWMN+1sigc2sOAwnzRS9QMplC8x9SwnsFO1fQ80Hdsu25dy1HkfPqRShIC 84GOJJbQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1magf5-00HTWW-LL; Wed, 13 Oct 2021 15:59:24 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mageO-00HTIn-Sb for linux-arm-kernel@lists.infradead.org; Wed, 13 Oct 2021 15:58:42 +0000 Received: by mail-yb1-xb49.google.com with SMTP id i83-20020a252256000000b005b67a878f56so3570676ybi.17 for ; Wed, 13 Oct 2021 08:58:39 -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=xRWQMLf+apv1h1Y0VPkMZJQJu4PG3LU0eGsIqqLuHBE=; b=dtGpO69Q22L/0AT3DiJZIAlyjD9Ntx7ATixiCAjJqElqUscnJANNHy8xGBlhLAzASH pdMTCLVoMIGPRc5OMmlvsOgZ4F3PpWd403IcwNLULm+Sd0mCbfyt4BbbVrcv+U6xPwy8 9l/EZD+oqsJ2pZSywdCKsEp5AT9hI6AyFaBDm3LexUQUcce8VitixF3STuVa/UttUXgC TL0UmSbwA8c4ZKzdrmvE2y9yd9aY0DQKn60kJWdeTYg0Wett0q+QIYVGPd1tVo09H0sz 15tm3VQaYKS+GlDNE6LsTero1j0v8nx0eH2shAT8yi2ZENzZBvorWzBnQx+UYLi99Fvz 9f3A== 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=xRWQMLf+apv1h1Y0VPkMZJQJu4PG3LU0eGsIqqLuHBE=; b=7IOumWwBPKm8FeQuNfZB3cxFeQGpYMUX+lHooT9ft61uMVN7zJtU/yO3VunSjIP68C LUuFyIKkvm//Uw6wxVviRGHFMpPGqY5r+zM2b2FcGMRQphYRg74gpMG+3Ga/nueMXUFC QMFRvoIaw2DwlXm9ik3+qNcdKEBzXIfZVHregTeY7RbgtUpJlBT/F+MSu6mO1fUGv+ec 61E2dJ1BWrCryAvXMdNbvr/sWLtaDbIbWvQn8rIR9APYl3s3yr2RiPeCuPeOG0S5epWp +r8890wLnhK8WCtkaReMLSMAhcmnl2pqUkihwEkezpOna0HHYApwF209fVxQ6DTCdzWA TIfA== X-Gm-Message-State: AOAM530uOCriITz/EmBOCSHlunFFHHjx58eUiS+upzvW1uWGiOZxLhe8 rJv4S8XDlUPhOMNJ1vQH1NsKH7WPqaJ4 X-Google-Smtp-Source: ABdhPJz807qmcV7sOwt20TlAiEdslYI8Ep37s2M/k2R6SI9SlHRjD+sPlh7lm4qrKlIzL5wx+7NKSuJQiHA8 X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:65b5:73d3:1558:b9ae]) (user=qperret job=sendgmr) by 2002:a05:6902:1024:: with SMTP id x4mr207401ybt.514.1634140718834; Wed, 13 Oct 2021 08:58:38 -0700 (PDT) Date: Wed, 13 Oct 2021 16:58:17 +0100 In-Reply-To: <20211013155831.943476-1-qperret@google.com> Message-Id: <20211013155831.943476-3-qperret@google.com> Mime-Version: 1.0 References: <20211013155831.943476-1-qperret@google.com> X-Mailer: git-send-email 2.33.0.882.g93a45727a2-goog Subject: [PATCH 02/16] KVM: arm64: Implement __pkvm_host_share_hyp() using do_share() From: Quentin Perret To: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Will Deacon , Fuad Tabba , David Brazdil Cc: 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-20211013_085840_972512_EDA8F127 X-CRM114-Status: GOOD ( 16.47 ) 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 __pkvm_host_share_hyp() shares memory between the host and the hypervisor, so implement it as an invocation of the new do_share() mechanism. Note that the new semantics are slightly stricter than before, as we now validate the physical address when double-sharing a page. However, this makes no functional difference as long as no other transitions are supported and the host can only share pages by pfn. Signed-off-by: Will Deacon Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 121 +++++++------------------- 1 file changed, 33 insertions(+), 88 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c index 53e503501044..6983b83f799f 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -342,94 +342,6 @@ static int host_stage2_idmap(u64 addr) return ret; } -static inline bool check_prot(enum kvm_pgtable_prot prot, - enum kvm_pgtable_prot required, - enum kvm_pgtable_prot denied) -{ - return (prot & (required | denied)) == required; -} - -int __pkvm_host_share_hyp(u64 pfn) -{ - phys_addr_t addr = hyp_pfn_to_phys(pfn); - enum kvm_pgtable_prot prot, cur; - void *virt = __hyp_va(addr); - enum pkvm_page_state state; - kvm_pte_t pte; - int ret; - - if (!addr_is_memory(addr)) - return -EINVAL; - - hyp_spin_lock(&host_kvm.lock); - hyp_spin_lock(&pkvm_pgd_lock); - - ret = kvm_pgtable_get_leaf(&host_kvm.pgt, addr, &pte, NULL); - if (ret) - goto unlock; - if (!pte) - goto map_shared; - - /* - * Check attributes in the host stage-2 PTE. We need the page to be: - * - mapped RWX as we're sharing memory; - * - not borrowed, as that implies absence of ownership. - * Otherwise, we can't let it got through - */ - cur = kvm_pgtable_stage2_pte_prot(pte); - prot = pkvm_mkstate(0, PKVM_PAGE_SHARED_BORROWED); - if (!check_prot(cur, PKVM_HOST_MEM_PROT, prot)) { - ret = -EPERM; - goto unlock; - } - - state = pkvm_getstate(cur); - if (state == PKVM_PAGE_OWNED) - goto map_shared; - - /* - * Tolerate double-sharing the same page, but this requires - * cross-checking the hypervisor stage-1. - */ - if (state != PKVM_PAGE_SHARED_OWNED) { - ret = -EPERM; - goto unlock; - } - - ret = kvm_pgtable_get_leaf(&pkvm_pgtable, (u64)virt, &pte, NULL); - if (ret) - goto unlock; - - /* - * If the page has been shared with the hypervisor, it must be - * already mapped as SHARED_BORROWED in its stage-1. - */ - cur = kvm_pgtable_hyp_pte_prot(pte); - prot = pkvm_mkstate(PAGE_HYP, PKVM_PAGE_SHARED_BORROWED); - if (!check_prot(cur, prot, ~prot)) - ret = -EPERM; - goto unlock; - -map_shared: - /* - * If the page is not yet shared, adjust mappings in both page-tables - * while both locks are held. - */ - prot = pkvm_mkstate(PAGE_HYP, PKVM_PAGE_SHARED_BORROWED); - ret = pkvm_create_mappings_locked(virt, virt + PAGE_SIZE, prot); - BUG_ON(ret); - - prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, PKVM_PAGE_SHARED_OWNED); - ret = host_stage2_idmap_locked(addr, PAGE_SIZE, prot); - BUG_ON(ret); - -unlock: - hyp_spin_unlock(&pkvm_pgd_lock); - hyp_spin_unlock(&host_kvm.lock); - - return ret; -} - void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt) { struct kvm_vcpu_fault_info fault; @@ -758,3 +670,36 @@ static int do_share(struct pkvm_mem_share *share) out: return ret; } + +int __pkvm_host_share_hyp(u64 pfn) +{ + int ret; + u64 host_addr = hyp_pfn_to_phys(pfn); + u64 hyp_addr = (u64)__hyp_va(host_addr); + struct pkvm_mem_share share = { + .tx = { + .nr_pages = 1, + .initiator = { + .id = PKVM_ID_HOST, + .addr = host_addr, + .host = { + .completer_addr = hyp_addr, + }, + }, + .completer = { + .id = PKVM_ID_HYP, + }, + }, + .prot = PAGE_HYP, + }; + + host_lock_component(); + hyp_lock_component(); + + ret = do_share(&share); + + hyp_unlock_component(); + host_unlock_component(); + + return ret; +}