From patchwork Wed Oct 13 15:58: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: 12556281 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 B6EB1C433F5 for ; Wed, 13 Oct 2021 16:08: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 78A4261100 for ; Wed, 13 Oct 2021 16:08:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 78A4261100 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=MKgAa8VrSHlnmSwdUbIegBczM11T8uHWuHwO7jbFaSg=; b=ly8NyosC29gmPgk/KF8yKxIq7o AvllV52zZdLhL2NiXGsMK/V0/OICZyt9SKzM+xyOjpO5jTufpaLxDQgiNyv1P4k8LmSPrZ6Jwvtkz Eq0tPDQt1QDixN2oNfr1BXF1yHPPUtJ0ywAl68Gc6oh6KJkjWrjrJoGM2vEWeT54nxs5YpCI8I4cN I3JoXUDfqnwD9OU2gHpHLAB1fQWXH1PdQ9vSwMU5gBlH3g+fNzBft3kMSHLmKypV44w+YW+OmL5DH YH4WTv1HJ16MJOvteAPZMB9XebW7bM1r/pepxPmlWZo5ZX12nAxIDxUdSoW4ObGP1W0ZmhtKfbSMS 8PULnBLA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1maglk-00HWXZ-25; Wed, 13 Oct 2021 16:06:17 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mageq-00HTSF-Hf for linux-arm-kernel@lists.infradead.org; Wed, 13 Oct 2021 15:59:10 +0000 Received: by mail-wr1-x449.google.com with SMTP id f1-20020a5d64c1000000b001611832aefeso2335805wri.17 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=jUaruWGk7r9qluPf1KFY3T+BMgXp1ZnJlmBiemG0achRAFDpAFPIkcH3q6stDWNsui o7bw0XUzrqccJZ1bOGRuhcgBEIYqVDLTNvxX3x5UgJ0p0a7odX50uBhwYAgxHc4s4G8x ozMQHNlhGTh6mU0NfsWKvOB0HOaC59GxWQhxG/PChz8JC6J04eZs2YCse5m50MgNNjsl MdzIl6ymokmWKMnKqkp3p7r3abvQaf9ruIvPSXVgylnQ6iJtrySyaqUzJ+IjQnU/MUWz K4+8xCTj8KItAavkG05miXdi06XH+oxRVy8cDE4w7eZ5R9HWelXD+4jcx4oPqWTwni9s IlSg== X-Gm-Message-State: AOAM533gHUfXT5UEgAWIXFg07eaUWeOM+bN5z1tNie4rhzhFGc9X696d ZtQdHrqKx9Ce2JHVOcm38RzrprmMnIuD X-Google-Smtp-Source: ABdhPJycAkElHpHQ2AGMbSwm3DR+JicypQn/HrqmtGvR6c4BW8hrAjG63/uNsstYDVBkOL0PzEyul2i7qtoG 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211013_085908_711651_29A11EE0 X-CRM114-Status: GOOD ( 13.97 ) 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 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 */