From patchwork Mon Nov 8 15:46:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12608765 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 8A093C433EF for ; Mon, 8 Nov 2021 15:48:25 +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 516D86128B for ; Mon, 8 Nov 2021 15:48:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 516D86128B 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:Mime-Version: Message-Id:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=ll9NM6nNicq2hsJadf+aOerv5TRa5DGXm90JXhcyvZM=; b=eek HrFd+JNlhXVKWV4L8QlUonAEJq+5pdnrzozSMCIxSJ48KBdNZ4oIT/+lDpT+HZcNENJQYOL4OHcL2 QLlc0DwlL7b3Tcxkkl3o5Hsiar9TKrnMzBvIvIsFs+x8qW7IuaG7Z3IfjMI7V3dlRqyzJAimyBxje A1WAHCyMl0Rs75OJh1MvxBatPPjTmgeMrOLWjYs6e0gNh3F3IUXLx0/Jde0fplgsQvsYsizhq2z4r bw6HAjXpVPtsUvwRDM7vWYPm06bnoXb5tgniVF3IxIjdFjRjTizhmg1zI4pn62/s0bZpHkdgBQWtZ r3vzrpifzMpotgLFLDhHKkg6cBNiqcQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mk6r7-00Gs81-2t; Mon, 08 Nov 2021 15:46:45 +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 1mk6r3-00Gs73-L6 for linux-arm-kernel@lists.infradead.org; Mon, 08 Nov 2021 15:46:43 +0000 Received: by mail-wr1-x44a.google.com with SMTP id p3-20020a056000018300b00186b195d4ddso4155931wrx.15 for ; Mon, 08 Nov 2021 07:46:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=AKApMG+OBphO2mXMNWEAzsKBITa8c+pAso67pj90vC8=; b=tG6WMOy8dE74seq/qZL5tl10JmW8g5oPqehlE/OLFpxU36Ysf7ZW95dwBXeXgmcMF8 YEWHUhQQOWRykLtNUx2WTgIx8tOmvq9soy+zWO4U29ZLZD7+5I4VvJqbdW+/vmnwr2Yk EItavDge61Azr5p7RdHNoJnde9HhafcPYYeub9RMPClQthw6TRcpLY1/asXTnzDBLc2S gjUAanJK6ajTf+rC+yFSGDXgHiK0pGx10EvBgimhWb8xAuX55XDt4mQpFt/ixoE4aqS5 nMqKwSAKLLTPKuKelYGlJmiQ9rtga1sjS0ipfi403/FpDqPaupXIs/M8wJpSQnJwwxRU YtIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=AKApMG+OBphO2mXMNWEAzsKBITa8c+pAso67pj90vC8=; b=3jU0vJja9iXZNgxU4ZxXG2bpgaqt1QMzz9VhsqS4b+v9U6ftcXWaPHb44ZfvTSoCIm bzgJDIXcdJbQOz7RVOMkYcXYYBjiwRINRC31d41tTUEMQjyVQkpFi20Jxn8mOhGrPB8K cWs8EdFOrLMqGl0PlcZTQYSfaejSifpdmYUKtY5jhCb/MwshnVPId5+rPTEsQAVAlwM0 Johp1ZHpu/h/hWK5iuqdPub/wmUWjBhSmaaBtLRaDHR4u5IDCPexqsWL0OTzEKbaMCBK aAoUA5gHiTAUm+I3lr0rgF66TVPqo4kgcaP35vFJpuf01fVNeH1wpzIBjW+M7iHR2Zov 8x/Q== X-Gm-Message-State: AOAM531uj2D0qnaf9LFJ4XeorJc90Rqz4nlgHF4IyOylVrwj7erAZ2o1 idDgUSpoV+rhhO892eVVMd0cKd3m3fgz X-Google-Smtp-Source: ABdhPJwzJyzbAG53O2weMHNNmNKKDpLhMyRm0Wi+7RXoQ9dLo6WBHGjX8UZ98cpkXILxFN6jSDpOUH0xoW/G X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:909f:3aba:7f9a:3a93]) (user=qperret job=sendgmr) by 2002:a05:600c:3ba5:: with SMTP id n37mr36644382wms.168.1636386399156; Mon, 08 Nov 2021 07:46:39 -0800 (PST) Date: Mon, 8 Nov 2021 15:46:32 +0000 Message-Id: <20211108154636.393384-1-qperret@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.34.0.rc0.344.g81b53c2807-goog Subject: [PATCH] KVM: arm64: Fix host stage-2 finalization From: Quentin Perret To: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Will Deacon , Fuad Tabba , Mark Rutland , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org Cc: dbrazdil@google.com, qperret@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211108_074641_718567_7D143F48 X-CRM114-Status: GOOD ( 18.59 ) 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 walk the hypervisor stage-1 page-table towards the end of hyp init in nVHE protected mode and adjust the host page ownership attributes in its stage-2 in order to get a consistent state from both point of views. The walk is done on the entire hyp VA space, and expects to only ever find page-level mappings. While this expectation is reasonable in the half of hyp VA space that maps memory with a fixed offset (see the loop in pkvm_create_mappings_locked()), it can be incorrect in the other half where nothing prevents the usage of block mappings. For instance, on systems where memory is physically aligned at an address that happens to maps to a PMD aligned VA in the hyp_vmemmap, kvm_pgtable_hyp_map() will install block mappings when backing the hyp_vmemmap, which will later cause finalize_host_mappings() to fail. Furthermore, it should be noted that all pages backing the hyp_vmemmap are also mapped in the 'fixed offset range' of the hypervisor, which implies that finalize_host_mappings() will walk both aliases and update the host stage-2 attributes twice. The order in which this happens is unpredictable, though, since the hyp VA layout is highly dependent on the position of the idmap page, hence resulting in a fragile mess at best. In order to fix all of this, let's restrict the finalization walk to only cover memory regions in the 'fixed-offset range' of the hyp VA space and nothing else. This not only fixes a correctness issue, but will also result in a slighlty faster hyp initialization overall. Fixes: 2c50166c62ba ("KVM: arm64: Mark host bss and rodata section as shared") Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/nvhe/setup.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c index 862c7b514e20..578f71798c2e 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -178,7 +178,7 @@ static int finalize_host_mappings_walker(u64 addr, u64 end, u32 level, phys = kvm_pte_to_phys(pte); if (!addr_is_memory(phys)) - return 0; + return -EINVAL; /* * Adjust the host stage-2 mappings to match the ownership attributes @@ -207,8 +207,18 @@ static int finalize_host_mappings(void) .cb = finalize_host_mappings_walker, .flags = KVM_PGTABLE_WALK_LEAF, }; + int i, ret; + + for (i = 0; i < hyp_memblock_nr; i++) { + struct memblock_region *reg = &hyp_memory[i]; + u64 start = (u64)hyp_phys_to_virt(reg->base); + + ret = kvm_pgtable_walk(&pkvm_pgtable, start, reg->size, &walker); + if (ret) + return ret; + } - return kvm_pgtable_walk(&pkvm_pgtable, 0, BIT(pkvm_pgtable.ia_bits), &walker); + return 0; } void __noreturn __pkvm_init_finalise(void)