From patchwork Mon Mar 15 14:35:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12139713 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9DDDCC433DB for ; Mon, 15 Mar 2021 14:47:48 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (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 DDD3B64F0E for ; Mon, 15 Mar 2021 14:47:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DDD3B64F0E Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; 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=oZy/5oiD8ZPQ0SbgSsZNB12bZvoN5V58E66QJvAJrNA=; b=g2X2ahbGEO3xlr zhbYmvZre9bhAG8Lx1TtaiInBDJ1IX3Vw5T0ZMuZTGtWNLwVRxdDs1gmRqegSb3EuYZFlS35NfmjJ s4A4xWDr0gjrMohQRnKXEYrroaS71/1kdSAMlMqbu7Mwjy3mUFWZOrwEAe/ZzRR90GnZQ5ocQShHt xcKMicvIsRGKZVnLFq9PouKn1sC29dTV7zljqhAM6MCBmypbKOK8JVagElrNONjCp0MlQgFEY5ohV BgR3NrA09uOT6OGKR8G1uy0PuH6skt3Qdjr/uCMYfd2t9oT/aTgyj5/nehRCU4K6l4bACnF9JQGIQ clAOjb6QZGdyRzpVII6g==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lLoTS-00G9f1-1a; Mon, 15 Mar 2021 14:45:38 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lLoKl-00G5xH-1O for linux-arm-kernel@lists.infradead.org; Mon, 15 Mar 2021 14:36:41 +0000 Received: by mail-wr1-x449.google.com with SMTP id x9so15062778wro.9 for ; Mon, 15 Mar 2021 07:36:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=J1uZIE4CatR8RJ2mwUZGBIvYuAMTy17ux6AGbOX5tFk=; b=quH1WWPr+L1SVwBqv31K38Go2mHpdC6nZ0w0GUgWu8lifd6IDz7tnxeCHBvwfxXcid s/cfv6PNBlw7Tv1DYVDfMOsXnBOEXJFlXhMZyBMoZVhKnghB6bZBQ7RXezVsiD4bljVh lJBqoO4Or/KxawhVR2OPnIGrxhVyjXNvg07Fc+ZEFDUsWiUlW9oHKY+QEXLr9k42aYSH d2Cae4A8DBkXpZvxeLTGbGBr2odV4gqnrZpx31EngUzons5PvcQ6ycsc99xvdaHgxaKm V/XOC0HOZ3lOrer8gP70svjqgVCAXPSm6Ym7SYFjCDsB/ZmUMa2sDLVxAtskeXAdYfBu oHQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=J1uZIE4CatR8RJ2mwUZGBIvYuAMTy17ux6AGbOX5tFk=; b=qNNUQNntuYvK9F/khdPO6teSju0qiXU+GwVEB5xoN+eOSB6at0JNQ5kfz3TGJk5sgT syC+of5PTOv8sPbnQc+Lk7RWQQ+WhX7z15wo38yT36WJhUkx+7fI64vfoHkdei1ImMSZ NZ3SNmOlhrQUZAo/9gkX97djQBEqCuYXRAqVEvcT2kFu3EP68zsErnsNyn1OFV0UXLXF 5L0g/+SWkpYInJ0ipzz+Jr1hUGdr0cS+9x40F+i+JnQEfeMMOF2ByM84XE1Cb/xREPSb zM41P9igFPtTvSyOA02KN10Lr+KrG1/E5DZdWVZTq6yLqQgMX9vWDM5h0WvBya/6WOE6 bZbQ== X-Gm-Message-State: AOAM532CC+KoWP/0v5/zusDwqkCMthicz7feZJRsmZzrnU+FxSqs6eyj uUszAStOyxulcvWI/L+y1TDmC7hfKayU X-Google-Smtp-Source: ABdhPJxe4+QHHGpZyqH28YWZv8YfJrDVsBARhsCBGZiD6vsppmOxHGv9C0FMe9dp3bfpi8X+Fs4PaGFGP24s X-Received: from r2d2-qp.c.googlers.com ([fda3:e722:ac3:10:28:9cb1:c0a8:1652]) (user=qperret job=sendgmr) by 2002:a05:600c:2053:: with SMTP id p19mr26380067wmg.87.1615818997861; Mon, 15 Mar 2021 07:36:37 -0700 (PDT) Date: Mon, 15 Mar 2021 14:35:28 +0000 In-Reply-To: <20210315143536.214621-1-qperret@google.com> Message-Id: <20210315143536.214621-29-qperret@google.com> Mime-Version: 1.0 References: <20210315143536.214621-1-qperret@google.com> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH v5 28/36] KVM: arm64: Always zero invalid PTEs From: Quentin Perret To: catalin.marinas@arm.com, will@kernel.org, maz@kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com Cc: android-kvm@google.com, seanjc@google.com, mate.toth-pal@arm.com, linux-kernel@vger.kernel.org, robh+dt@kernel.org, linux-arm-kernel@lists.infradead.org, kernel-team@android.com, kvmarm@lists.cs.columbia.edu, tabba@google.com, ardb@kernel.org, mark.rutland@arm.com, dbrazdil@google.com, qperret@google.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210315_143639_298901_664C00FF X-CRM114-Status: GOOD ( 14.36 ) 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 kvm_set_invalid_pte() currently only clears bit 0 from a PTE because stage2_map_walk_table_post() needs to be able to follow the anchor. In preparation for re-using bits 63-01 from invalid PTEs, make sure to zero it entirely by ensuring to cache the anchor's child upfront. Acked-by: Will Deacon Suggested-by: Will Deacon Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/pgtable.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index bdd6e3d4eeb6..f37b4179b880 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -156,10 +156,9 @@ static kvm_pte_t *kvm_pte_follow(kvm_pte_t pte, struct kvm_pgtable_mm_ops *mm_op return mm_ops->phys_to_virt(kvm_pte_to_phys(pte)); } -static void kvm_set_invalid_pte(kvm_pte_t *ptep) +static void kvm_clear_pte(kvm_pte_t *ptep) { - kvm_pte_t pte = *ptep; - WRITE_ONCE(*ptep, pte & ~KVM_PTE_VALID); + WRITE_ONCE(*ptep, 0); } static void kvm_set_table_pte(kvm_pte_t *ptep, kvm_pte_t *childp, @@ -443,6 +442,7 @@ struct stage2_map_data { kvm_pte_t attr; kvm_pte_t *anchor; + kvm_pte_t *childp; struct kvm_s2_mmu *mmu; void *memcache; @@ -532,7 +532,7 @@ static int stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, * There's an existing different valid leaf entry, so perform * break-before-make. */ - kvm_set_invalid_pte(ptep); + kvm_clear_pte(ptep); kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, data->mmu, addr, level); mm_ops->put_page(ptep); } @@ -553,7 +553,8 @@ static int stage2_map_walk_table_pre(u64 addr, u64 end, u32 level, if (!kvm_block_mapping_supported(addr, end, data->phys, level)) return 0; - kvm_set_invalid_pte(ptep); + data->childp = kvm_pte_follow(*ptep, data->mm_ops); + kvm_clear_pte(ptep); /* * Invalidate the whole stage-2, as we may have numerous leaf @@ -599,7 +600,7 @@ static int stage2_map_walk_leaf(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, * will be mapped lazily. */ if (kvm_pte_valid(pte)) { - kvm_set_invalid_pte(ptep); + kvm_clear_pte(ptep); kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, data->mmu, addr, level); mm_ops->put_page(ptep); } @@ -615,19 +616,24 @@ static int stage2_map_walk_table_post(u64 addr, u64 end, u32 level, struct stage2_map_data *data) { struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops; + kvm_pte_t *childp; int ret = 0; if (!data->anchor) return 0; - mm_ops->put_page(kvm_pte_follow(*ptep, mm_ops)); - mm_ops->put_page(ptep); - if (data->anchor == ptep) { + childp = data->childp; data->anchor = NULL; + data->childp = NULL; ret = stage2_map_walk_leaf(addr, end, level, ptep, data); + } else { + childp = kvm_pte_follow(*ptep, mm_ops); } + mm_ops->put_page(childp); + mm_ops->put_page(ptep); + return ret; } @@ -736,7 +742,7 @@ static int stage2_unmap_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, * block entry and rely on the remaining portions being faulted * back lazily. */ - kvm_set_invalid_pte(ptep); + kvm_clear_pte(ptep); kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, mmu, addr, level); mm_ops->put_page(ptep);