From patchwork Wed Oct 13 15:58:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12556261 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 CD500C433EF for ; Wed, 13 Oct 2021 16:03:15 +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 981726109E for ; Wed, 13 Oct 2021 16:03:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 981726109E 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=jgy6M+7kuCxQaU0Igod/bTEwuZaqLARHDI3MUc7KOyw=; b=UhvS9C2orLnK/DbVRCOVAtP5ic bIuQ6//N0ltZMZehtwY/ht0sFZyJQUONg0a9wicLh4KXXU7XUXjfP3QeTdUdELu2c+bT5kL4DCqME /bk1pAsa4gYdPmBaiuNsLYjqmE+HQ0pWmBcLiWwLe5ozZw5vriwm3u6zgWXppaWVDEwJBr+fWfZ9D YFINOJ8t+9MSTGgDo2XSeXELYKhaXfnX3eM++DoQD/6a+ZdQ4NkspO3szyfDYlfb6CunR6izZEVIv PFnLAnacIADh8eFH1vA4Kwh7/Gvyvh8FsLEJTjgp38PaPrK91DTmT2mnCP6Wh+rz56MISJAGB9QvE dvKxszjg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1magh3-00HUMn-Us; Wed, 13 Oct 2021 16:01:26 +0000 Received: from mail-qk1-x74a.google.com ([2607:f8b0:4864:20::74a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mageb-00HTMy-4Z for linux-arm-kernel@lists.infradead.org; Wed, 13 Oct 2021 15:58:54 +0000 Received: by mail-qk1-x74a.google.com with SMTP id c16-20020a05620a0cf000b0045f1d55407aso2193572qkj.22 for ; Wed, 13 Oct 2021 08:58:51 -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=ztuYYF9Onc0ZIjngTVEp2z77n51MjY0wGMZOLwDTwhY=; b=GVq5GFcw/0eVvulNbba6ktlvmE8YFALwpDIhCJ2/vsiC21sLdLoc9O7Xq36D1lJ5lf ZQpif0gOuVc4Y1gCrCxInx9Dy5dbafYfdL55owb7iUdw+pbGb6j7FjBOyhKrHNDwIkvj IUagiUdDR+zKO/Ac9HpWerSCBqwNR3R74jb/oiqdXP39kp5RokKJ70PgYiZ7br199OT/ fxtAXX/51HuCFEYze6DhFLzMABWI4+IwL8WM23aFrRth8DgCQnF6HKbnFhSTNuYZOayb PcC6MbtcinQD4fvigVVSdsAf5MhLy6ddmj2xBzYeMb40ITQkrfyGcy+2NPXD9hI6eXPQ nvgA== 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=ztuYYF9Onc0ZIjngTVEp2z77n51MjY0wGMZOLwDTwhY=; b=EnL98h22+LD65NZKN9gYc+fKFgxHrA9VT2sqbEm6h/tmthfxkDjy7wrFv3nTep46Fx 0dHKrb0B5YFj/zRMDRiJkIdcgMZkaeB3rkiGd95wqVLRv27ASLV/m/sP/MUqDK1Gstr3 muySwGfG/qirYam2KaGXdnJHqoPdnI+rhkXNc1yZzLucauRUslGNWmgYduSUOTigCfGu mqiLecraLYq5Z17WiEKZywoSYGHLA+c7sWsmzf65QIshuOPLxnaRnM15o781VgCytyGl kCYAhFX3Qq1EF4EzTAJQjGzYFUmUgS94PHpLmDA5/Ss5JkzNGZ7R0FC8xy9em3tsFoOT H5yg== X-Gm-Message-State: AOAM532iQ6/FJLszI76MCk36E98EaJO41BxGNFBNHoKb6wwnLv+mnuVH GdaiNWf4J4wel9XwAC+sObwgOwxjOw4F X-Google-Smtp-Source: ABdhPJxp1hUAJxs/riqQe2cYJzZaiMjwH6ehh74ltN5CiIrrR1/FVufG6S8GAC+vFxwjhGJSaTXJSPc9xIJG X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:65b5:73d3:1558:b9ae]) (user=qperret job=sendgmr) by 2002:a05:622a:13c6:: with SMTP id p6mr65288qtk.165.1634140730556; Wed, 13 Oct 2021 08:58:50 -0700 (PDT) Date: Wed, 13 Oct 2021 16:58:22 +0100 In-Reply-To: <20211013155831.943476-1-qperret@google.com> Message-Id: <20211013155831.943476-8-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 07/16] 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 , 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_085853_211071_871440B6 X-CRM114-Status: GOOD ( 12.95 ) 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, };