From patchwork Wed Dec 1 17:03:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12694351 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 AED1FC433F5 for ; Wed, 1 Dec 2021 17:06:56 +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=/DZ72Hg1A7x+udsw5YjDhLp/7eKqfLLKP6GZRmBmnkU=; b=nrixfuSX/nk2+XCkSVnrpRqmbk +TmlvwzcRlcM406PWExl10XwSn+qP+DylKxjdszhymYx0l5ENEZGwVnsj31qp33h44d94EEE4fi/6 qCMPKdiXNEwoZerLoE9/2qV/zFbx47SnGoyX/azYrjoNu5jlN7cdPQ39+vYEKmJQZ5h5SlAi8p4tB XmnNQEXXGI/G1wCk4KBBdz/IFFMxApXEk0POgBaAs51dWfERkZ1c+qE01JoiUvgzsjFbaqwU7IwxR AM7p6Q2N0BxTBhtOwYg29lBtpOEf/cxS2k+OJbAgijg9dFgcLlpsUQer2j2unAg0UK3iI4viElWz/ 2XneB/2g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1msT2e-009S8H-3W; Wed, 01 Dec 2021 17:05:12 +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 1msT20-009RrY-Hb for linux-arm-kernel@lists.infradead.org; Wed, 01 Dec 2021 17:04:33 +0000 Received: by mail-wr1-x449.google.com with SMTP id p17-20020adff211000000b0017b902a7701so4459617wro.19 for ; Wed, 01 Dec 2021 09:04:30 -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=9uBUypl+EXPtiyqLggdiPCeBqKTFxHsKtVyJKG8pgmY=; b=Vv1c7CHoktHXpAkMllAbrV9Qh8WJZnyTA3AZU15KbH4HMkRzFi9p8jcrrt9SYvw/vm XiJfozl/oFlTHNfLRWY3HBA3gFa9GG27TT0SAeeuzCVqz8nF3/MFgRTE1JTlFJUP24Ec BzDBGevNwW/zf0YB+zazdFhJ4kiNaPq5iHmleo2R5XUDtGWhB+8awKuhqPooz9Z/mSNG gs4ndbhCyBpRHS3gb21jCowKbTdnE1ozwPsjKll5mD7/DVNd1EvOyQ72bfzphCiSXYhN gFAldNGJnBPpUdYyDkMsuJXxN+63Mq6uHhqZqhCmWw9MzrD9lAxNSXUtKzeOMkHVbfX8 pvlg== 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=9uBUypl+EXPtiyqLggdiPCeBqKTFxHsKtVyJKG8pgmY=; b=Qtzpd3OQo/qlRgiSBC41B4GegBKbqUF/OjA/FH2NDBKlukZ4g6WtILSUyxJe5GBUdr EjPAeRMHpZG2Qg6qPUXMrxxTQNdHTtr8+nWGgDSKO0+LyFzul2kqqgg5yl3lgZI7Qeff k8hCF3DWqn/dP60Q1MLkSAZyKXiK0vXVuG9DnRMBuz0RueCpm33PPftiw3CojTRE6ME5 lhQWOTf1IN3ZVA4E9b/N5gzZUCosNS/K2Ty4gjdn5GgbHrL0PxzT+gUHt+LA/qx3LGZt AbBJs5B2Co2wnusNJlsugVZiPzAAUf3nvjw+SwYlBQ50uyv4kWcHx9f0/xhjjg4R9lbt HMqg== X-Gm-Message-State: AOAM53075G5kzXAfvVRWmioPg1WOYRZdLMzvEp2EHHBaXq8kw8VMhwxV nsdz0hpWtDrJ1DaWVZAI+yp7crpgyGc6 X-Google-Smtp-Source: ABdhPJynMlhVluzA+1W+geGVMD660u2IXflLbXQYLj5MfYI1LEe60lA76lE/vavpHK8Z8gHIBVjRNs1KfRK+ X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:1cab:1a20:2ef1:ddde]) (user=qperret job=sendgmr) by 2002:a05:600c:1d1b:: with SMTP id l27mr113174wms.1.1638378269441; Wed, 01 Dec 2021 09:04:29 -0800 (PST) Date: Wed, 1 Dec 2021 17:03:58 +0000 In-Reply-To: <20211201170411.1561936-1-qperret@google.com> Message-Id: <20211201170411.1561936-5-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 04/15] KVM: arm64: Fixup hyp stage-1 refcount 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_090432_605599_C0E3E065 X-CRM114-Status: GOOD ( 16.07 ) 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 In nVHE-protected mode, the hyp stage-1 page-table refcount is broken due to the lack of refcount support in the early allocator. Fix-up the refcount in the finalize walker, once the 'hyp_vmemmap' is up and running. Signed-off-by: Quentin Perret Acked-by: Will Deacon --- arch/arm64/kvm/hyp/nvhe/setup.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c index 578f71798c2e..875b5174342f 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -165,6 +165,7 @@ static int finalize_host_mappings_walker(u64 addr, u64 end, u32 level, enum kvm_pgtable_walk_flags flag, void * const arg) { + struct kvm_pgtable_mm_ops *mm_ops = arg; enum kvm_pgtable_prot prot; enum pkvm_page_state state; kvm_pte_t pte = *ptep; @@ -173,6 +174,15 @@ static int finalize_host_mappings_walker(u64 addr, u64 end, u32 level, if (!kvm_pte_valid(pte)) return 0; + /* + * Fix-up the refcount for the page-table pages as the early allocator + * was unable to access the hyp_vmemmap and so the buddy allocator has + * initialised the refcount to '1'. + */ + mm_ops->get_page(ptep); + if (flag != KVM_PGTABLE_WALK_LEAF) + return 0; + if (level != (KVM_PGTABLE_MAX_LEVELS - 1)) return -EINVAL; @@ -205,7 +215,8 @@ static int finalize_host_mappings(void) { struct kvm_pgtable_walker walker = { .cb = finalize_host_mappings_walker, - .flags = KVM_PGTABLE_WALK_LEAF, + .flags = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST, + .arg = pkvm_pgtable.mm_ops, }; int i, ret; @@ -240,10 +251,6 @@ void __noreturn __pkvm_init_finalise(void) if (ret) goto out; - ret = finalize_host_mappings(); - if (ret) - goto out; - pkvm_pgtable_mm_ops = (struct kvm_pgtable_mm_ops) { .zalloc_page = hyp_zalloc_hyp_page, .phys_to_virt = hyp_phys_to_virt, @@ -253,6 +260,10 @@ void __noreturn __pkvm_init_finalise(void) }; pkvm_pgtable.mm_ops = &pkvm_pgtable_mm_ops; + ret = finalize_host_mappings(); + if (ret) + goto out; + out: /* * We tail-called to here from handle___pkvm_init() and will not return,