From patchwork Wed Dec 15 16:12:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12696300 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 DA713C433FE for ; Wed, 15 Dec 2021 16:25:44 +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=WLALwbkYgXnrDYFCfsLsYQFs6rnzqvkxx/9Pec6KtW0=; b=lzx8VAOgBuqYl7zXfWHRYxswf6 CIMr2pFhL+UGS7Oy0QuQDrrCv6EexJYjlxcuGGIXkDtxDsA3I7X49+QhXUGSGApPjrGkYcpMz/xpe Bwb4D+AGTOlRBpdO0idtw6Xhhvstm6U5YPHZfg7kWvZpDXC2ExdQmbkIwo/NofcSvR7EsuB/hd529 xJ7hxcRR6mKi9kWMIImcjyq3bKSTvWzpuLURlqEK8XfhiLfYBRsfGkzz3QDlkU4m3JMqhruv256q5 utn44+eq/ijXKjd+qqdQdVXtvg3oIc4+c5O922Ata7JYLxMgAzWA3q84OMXLiZP0XvU1pTJSAF/3A ZXnCHnaA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mxX4R-001gf5-TO; Wed, 15 Dec 2021 16:24:00 +0000 Received: from mail-wr1-x44a.google.com ([2a00:1450:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mxWtf-001c0I-3Z for linux-arm-kernel@lists.infradead.org; Wed, 15 Dec 2021 16:12:52 +0000 Received: by mail-wr1-x44a.google.com with SMTP id a11-20020adffb8b000000b001a0b0f4afe9so3236594wrr.13 for ; Wed, 15 Dec 2021 08:12:49 -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=0T7H/Y38j6+Gwobm57eeLVycbzCrTBaKznUeZZoLnRY=; b=BJgpzkCwe1j3WJWKVWNOBVvJCvYLLVWrwYVZlEgV11DB1tQCznJlUwNpFmhV4nNJoe x4rhNTLCG8yYgsw9qn3v98zaLBUf6DRqrpAxVZbBAjhXEVgrI+0cEITsahoz+mFGbZDN Bp1fk3yZMsNpaQ/kcxbipbrFTeC4FKeQzjHWuTODjU6dTmT/KxlZ4zLtMB3B6sO8lBac aiE3ccRuO3vXHCM3Hg3g4J+Vl6C6MJQg8BNnXZo8Z3UDpyroafFj247opJNvQJOLHj8k 4RoVVrI1fiVAJ51smFgIMMfMRq9NWs8SnpWjMzKbPiX2hUX7nBHyLwXOKfUlvLf6MO9S RKNQ== 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=0T7H/Y38j6+Gwobm57eeLVycbzCrTBaKznUeZZoLnRY=; b=efkRWTWNQgZYH0cjrnOgcx4jXST0YGSNfRCjt6qmJUq1DqQINzBjakUyWVD1BVK42S IkwC/Vp7Dy93Mc22GKQFRhM095sUTxmevxNlOOIIK2nDyvgmCA4SSL0xWukaedycYc0B 3CBV0JPG1C4MTywvU1SGnLGk6cSVT0tNQQU3Vi3QVy6aOC+EYKdl/WM0SFaif6X4sdwx jzUoFgKEBT9fjeXgMXFe2LZZhJON9peVeSobC+1xUIrNvuHG17aJXhZvTocbP9W+0YlT St7eKm1oKgKG3SpIjFkTm1rGf6kMaWkZcgO1aP0JuTa84rA9GW7DzosV0W9oA/HJJLWX wqqg== X-Gm-Message-State: AOAM531F0EgHjkeBgPUhe419kLP7oqkYSoYGKf/wXoR4MqPWBMo+dGFr FW/g8Aab5+S7b4DK3Zu99A3da33lWh4u X-Google-Smtp-Source: ABdhPJxs0xcgsWgPokAD4PgxH1YFZ/1fp7HGONpVZTXN+N1q3NOGytELjmmCe6UxqHhDjxU63cTx8+yucVmW X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:fc03:4f5b:4e9b:3ec1]) (user=qperret job=sendgmr) by 2002:a05:600c:3ac3:: with SMTP id d3mr527291wms.174.1639584768216; Wed, 15 Dec 2021 08:12:48 -0800 (PST) Date: Wed, 15 Dec 2021 16:12:20 +0000 In-Reply-To: <20211215161232.1480836-1-qperret@google.com> Message-Id: <20211215161232.1480836-4-qperret@google.com> Mime-Version: 1.0 References: <20211215161232.1480836-1-qperret@google.com> X-Mailer: git-send-email 2.34.1.173.g76aa8bc2d0-goog Subject: [PATCH v4 03/14] 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: qperret@google.com, qwandor@google.com, 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-20211215_081251_205444_EE652CB7 X-CRM114-Status: GOOD ( 15.65 ) 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. Acked-by: Will Deacon Signed-off-by: Quentin Perret --- 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 e31149965204..ab44e004e6d3 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -166,6 +166,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; @@ -174,6 +175,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; @@ -206,7 +216,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; @@ -241,10 +252,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, @@ -254,6 +261,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,