Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3596830pxb; Wed, 13 Oct 2021 09:04:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxTNN4CI1FvmNpPGlKkexJDNoqbN+iqQPJY+WEckyGas6SQPm9W7uw1e/N+bqDUaXg7LaJI X-Received: by 2002:a17:906:17d5:: with SMTP id u21mr155793eje.16.1634141086617; Wed, 13 Oct 2021 09:04:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634141086; cv=none; d=google.com; s=arc-20160816; b=nZF1m40Uee/H1OjZCR9CglMR2X0N7Czw8UkNdXWTfBojU4204Lf62wEMp4vqP4QApj 9wRjFESgKwGFQ8h3CMWWE3xuG8X3JBiSwruRoGTpd/ULBd+vmFNhLRKdcijz+m0CNeg0 s+gIHf/5xOilQVKyGaELIoh4CKj2GNwrWJ6T+WWr0T2gM+w2SV77SX6HOxf7z8BuvPKD uulY4JXxP5WdYONlSQDOsQhGtkhYNH4/qs6BzUEpNmfQ67jK3dIE6naoH1BSALTNsHY5 icUmnIW6bExbWbS4sFxM8j5Ulbl+lCmoHU6Bokc7S/klwph66oAy3bo/XJNaO95aWx/j K0VQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=5BoaM6D6gNLK4spf2rCygUFZYyg82caSdHjxEWdrlQU=; b=xRAm4a3tpdE7aQBmhvr+CKsrwIVbIe1LNHeb5caDYuEW+NviLiaEs/usK8MCGEaBqo Xv/Rnz0y6vLSDzjhEiMICjcH53POmXN4cD1X2DzsT257m5Q27agqyjfMdIenFohhv1AB Fp0wsgpcaP0XwMFuvsRh9Y2wayUkvHM++FhXDKqNx2PqjIw6h9HSCnD1p3TMIwheHJNM yjc2WDWmvFexD2mzJS7aw2Huu14+RcjIekmbfs87rdVm/IgXBIcieEFwyGzckI3amiC+ G5J5j0T6dGn4F7zAsndoePx2yaGoiGCS8BqgP+3lqMjeQXTao2i8fkyPQiAcZaYfq3AK Y+UA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=lCYZR0Ko; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bc27si19715823edb.455.2021.10.13.09.04.13; Wed, 13 Oct 2021 09:04:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=lCYZR0Ko; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237619AbhJMQB3 (ORCPT + 99 others); Wed, 13 Oct 2021 12:01:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237194AbhJMQBL (ORCPT ); Wed, 13 Oct 2021 12:01:11 -0400 Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C29F9C061765 for ; Wed, 13 Oct 2021 08:59:07 -0700 (PDT) Received: by mail-wr1-x449.google.com with SMTP id v15-20020adfa1cf000000b00160940b17a2so2327009wrv.19 for ; Wed, 13 Oct 2021 08:59:07 -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=5BoaM6D6gNLK4spf2rCygUFZYyg82caSdHjxEWdrlQU=; b=lCYZR0Kok1ZJ/F1Bc4qnzND5K7Zjs/qag8dPQUKLfKY1Vuju+0hrk97PbxTbLFIE92 dhF98EBgTqhqQt92nIfoE7GMC2K4/ghR4OjSzZq65MJp4AT7ibXNQwUHSePHfSjriq+s qj1OBYf3ab/2GRzkKLmCCNN9sNsIntPaQklJ6mwQ/XKR3fKPsRo3EWTXDVVPSTrHWBVD 1rD/QwOnSIbGCAAkjEKSTIRdQXrNpoLM7Rs+deBDWlZEA49xNVEQoxCXD+YunfwM3WR5 9SFni/ujR7WAYzyn4Pom8B/djSepucfAa3KiUobigruMi97cJGEedWHGkG8p0udLt1y2 BQTA== 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=5BoaM6D6gNLK4spf2rCygUFZYyg82caSdHjxEWdrlQU=; b=mK91XMePgpoJlcmxCwIjVi/2OWgtnWn9MD3HYxSVhGvjzlx7bghkQTglw20yVGS5vW K28HVCmVVd7+57bRUyp4nlkicGIFKVUHHHxlzELZkYCQW7fo6RhbCu+UsMje0TEVrTd2 1P4Kt6JKA5r3m8c7RxKYNbOZKNRGEz89KoY73HWZEbDmkhJI8yNSZuEreOiNzsrGWxpD e5aIHvwxHtynY1bXFv0rtDm7bL8DDz7+CwGSODTBYSuRdM/0Ux891ySmd/TcA6osEkFG ukEpnYAnduVVe0pz/A1ZZP5lTQpbNTkw6aWeMv3X+chDBjB2S2A4KEHTYrx0u2L6Ha4A +/Zg== X-Gm-Message-State: AOAM530ilrfUHaNKe6NMCeX9HOQrjikJLlxrYfS5kCrWAXOjT/U6JO/i pgTwjFuN52nkpuR4aAoiEfV7YNIlW2kW X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:65b5:73d3:1558:b9ae]) (user=qperret job=sendgmr) by 2002:a7b:cd90:: with SMTP id y16mr93339wmj.146.1634140746365; Wed, 13 Oct 2021 08:59:06 -0700 (PDT) Date: Wed, 13 Oct 2021 16:58:29 +0100 In-Reply-To: <20211013155831.943476-1-qperret@google.com> Message-Id: <20211013155831.943476-15-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 14/16] KVM: arm64: Refcount shared pages at EL2 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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We currently allow double sharing of pages from the hypervisor to the host, but don't track how many times each page is shared. In order to prepare the introduction of an unshare operation in the hypervisor, refcount the physical pages which the host shares more than once. Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c index 3378117d010c..cad76bc68e53 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -560,6 +560,9 @@ static int hyp_check_incoming_share(struct pkvm_page_req *req, if (ack->completer.prot != prot) return -EPERM; + if (WARN_ON(!hyp_phys_to_page(req->phys)->refcount)) + return -EINVAL; + return 0; } @@ -619,13 +622,22 @@ static int hyp_complete_share(struct pkvm_page_req *req, enum kvm_pgtable_prot perms) { void *start = (void *)req->completer.addr, *end = start + PAGE_SIZE; + struct hyp_page *page = hyp_phys_to_page(req->phys); enum kvm_pgtable_prot prot; + int ret = 0; - if (req->initiator.state == PKVM_PAGE_SHARED_OWNED) + if (req->initiator.state == PKVM_PAGE_SHARED_OWNED) { + hyp_page_ref_inc(page); return 0; + } prot = pkvm_mkstate(perms, PKVM_PAGE_SHARED_BORROWED); - return pkvm_create_mappings_locked(start, end, prot); + ret = pkvm_create_mappings_locked(start, end, prot); + + if (!ret) + hyp_set_page_refcounted(page); + + return ret; } /* Update the completer's page-table for the page-sharing request */ -- 2.33.0.882.g93a45727a2-goog