From patchwork Wed Dec 1 17:03:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12694350 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 51FB3C433F5 for ; Wed, 1 Dec 2021 17:06:42 +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=FvF5onZwwitvB0GJDXUDEFer13+6BzmVcw8rC3JoLes=; b=hk1H2ux9KjSmGIiu8vSoR0BPZG rf7KKFdlcaWDPXk0Qp2ZBO8G9w/ldaAyQHfRarkmCwQRKljomGCJgGBLiNgpAUPdO15/qBrTXXUEB nv2PL5NOI3AvCpgJEqrabhJ2l1JejgXcGIztbqgre+r7jRxDs8KcKFSljHFNzhMt9jhbMwuEIFbxZ 9Eu1mwAaqm849YDBTY/9E4Cb7pzW10QUWf4AV368YEwhfNAFgw4ru0dk+hfqh5kvQdCPS6+osW66W cDfrBHWbvRWdL8g+p3dfCwtUX84uDgj0jfpI0fCkzNkQ/HjmEf3/wKoTPPZrGnEJss9iEWEpVGoMr G6RIUJNQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1msT2O-009S0P-Na; Wed, 01 Dec 2021 17:04:56 +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 1msT1v-009RqY-QH for linux-arm-kernel@lists.infradead.org; Wed, 01 Dec 2021 17:04:29 +0000 Received: by mail-wm1-x34a.google.com with SMTP id i131-20020a1c3b89000000b00337f92384e0so135690wma.5 for ; Wed, 01 Dec 2021 09:04:26 -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=A7hr35xG6SzEHwDet74XtvHzJsI/X+Zg/yL5/WYnOz0=; b=ah12j+uUrkgUrk9b1HPkNYJu1wVkvH0SX1GjxWlFdsGE3bEQ46fAXvn0aJfcBwEo+8 6LrYpwJQVU32AtFiqZZFehFhMRCpiksII11x5No7K2l4MzTVhOGCcKSwW4VsaKYB/KwZ STISVRv3cmicA+IFYacwY4r+EdcvE6uDIJ6X9F/HMudy2oqDeRgFfx9EYMTh5ZhcNkDE y+NXXLWJwOH5boJw7lqmcJwIiiAh7SITqADYhwT/9haBjdb8CHxhJXGD/TIgVldyEoGn uw+wuq1gmGXOB6VUh00f2IDCloa5CVB3P1CMZ5640YlWHcMYBtJVsIffUl4qaumK2x0t XUbw== 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=A7hr35xG6SzEHwDet74XtvHzJsI/X+Zg/yL5/WYnOz0=; b=SfGFy4CXMCr7uFU7apj8DvX4vKpqK8OedZ15sc08cMUT7Cp/JuuOfCFw2bnhHLu2nz J2DnVJt8iOXfs6rs2U//Ak8O/3EMVBPWaphVfBnB3t5B1WO1LtecxYkFiYsyYcRPD43L jp7bZFv+qzo9QkdbTWqRzI2UMb11CKQj0SoBcHAUuOW6VAQElPeV9AAgEys6Dfd2zQ8q 23UD7z4OIw1fLqx13vmjznrs03sRL9kRt3mOy+66/5Tb8fds09XHNMCAGm7yBaZjM3v8 MdgnIFt0e1aB0i2pAFEDEEKjCQqnOK1AZRUwM6MYdzpnyOla77QEErEMBXnBNx6M/ZN3 mkRQ== X-Gm-Message-State: AOAM533LQpTpkGaKzAyTG773Ft3kIadCrNzY3co7K8e9breZXemwZLp/ lKda0ggLwQ2abz6xuehwZZlKtRyvAbHu X-Google-Smtp-Source: ABdhPJxp5L6PtF860OZ96JvJzz+R53mWZq1H92zAhum23/hQpBFE1gQt/KAt2QyLRQi8U4J5dXjZ/F1KEhLb X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:1cab:1a20:2ef1:ddde]) (user=qperret job=sendgmr) by 2002:a5d:40cf:: with SMTP id b15mr8314262wrq.161.1638378265569; Wed, 01 Dec 2021 09:04:25 -0800 (PST) Date: Wed, 1 Dec 2021 17:03:57 +0000 In-Reply-To: <20211201170411.1561936-1-qperret@google.com> Message-Id: <20211201170411.1561936-4-qperret@google.com> Mime-Version: 1.0 References: <20211201170411.1561936-1-qperret@google.com> X-Mailer: git-send-email 2.34.0.rc2.393.gf8c9666880-goog Subject: [PATCH v3 03/15] KVM: arm64: Refcount hyp stage-1 pgtable pages From: Quentin Perret To: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Will Deacon 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-20211201_090427_879243_CAB0E606 X-CRM114-Status: GOOD ( 12.86 ) 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 To prepare the ground for allowing hyp stage-1 mappings to be removed at run-time, update the KVM page-table code to maintain a correct refcount using the ->{get,put}_page() function callbacks. Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/pgtable.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index f8ceebe4982e..768a58835153 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -408,8 +408,10 @@ static bool hyp_map_walker_try_leaf(u64 addr, u64 end, u32 level, return false; new = kvm_init_valid_leaf_pte(phys, data->attr, level); - if (hyp_pte_needs_update(old, new)) + if (hyp_pte_needs_update(old, new)) { smp_store_release(ptep, new); + data->mm_ops->get_page(ptep); + } data->phys += granule; return true; @@ -433,6 +435,7 @@ static int hyp_map_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, return -ENOMEM; kvm_set_table_pte(ptep, childp, mm_ops); + mm_ops->get_page(ptep); return 0; } @@ -482,8 +485,16 @@ static int hyp_free_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, enum kvm_pgtable_walk_flags flag, void * const arg) { struct kvm_pgtable_mm_ops *mm_ops = arg; + kvm_pte_t pte = *ptep; + + if (!kvm_pte_valid(pte)) + return 0; + + mm_ops->put_page(ptep); + + if (kvm_pte_table(pte, level)) + mm_ops->put_page(kvm_pte_follow(pte, mm_ops)); - mm_ops->put_page((void *)kvm_pte_follow(*ptep, mm_ops)); return 0; } @@ -491,7 +502,7 @@ void kvm_pgtable_hyp_destroy(struct kvm_pgtable *pgt) { struct kvm_pgtable_walker walker = { .cb = hyp_free_walker, - .flags = KVM_PGTABLE_WALK_TABLE_POST, + .flags = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST, .arg = pgt->mm_ops, };