From patchwork Fri Mar 19 10:01:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12150587 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, URIBL_BLOCKED,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 02F36C433E6 for ; Fri, 19 Mar 2021 10:16:01 +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 DD39F64EEE for ; Fri, 19 Mar 2021 10:15:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DD39F64EEE 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=rPJGE5bYJURs8blLrThpADNTMQ6BeaKPBZETCm2AH50=; b=oHbc+p1Utqn1dH RfOFWtIE/OPK2jerD7KKK0hLWAd27URgKqthQj60mZQrZuJ9c1x24/NlE1U8pGsOaTsOn5IuSrtMd 4nrUn9JcXdRm/uurWfgD4zW9eCrIOFaPnxlLwP+qjSw2KzGV6GgoyCEObAQ3J/ccOyi3qJKtY9v9U 4yKPhXos+l/kvM+X7Y653nWZXEZhUS9AzWQE6gjcGq3573EKJqlT3yf2dUOUwghfgsIfKlP6E1yDy Cx4zDbVLf/v+OTNzeAuKU7+AlTh9Qo/oc/ddtQ8/z4k9wMoCD0oJGBe/Aw20WdT1rtCvNgnrFAZEy FlAfADR8NAK9SDZG2xjw==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lNC8j-0074ku-9d; Fri, 19 Mar 2021 10:13:58 +0000 Received: from mail-wr1-x44a.google.com ([2a00:1450:4864:20::44a]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lNBy1-0070Sa-AW for linux-arm-kernel@lists.infradead.org; Fri, 19 Mar 2021 10:02:57 +0000 Received: by mail-wr1-x44a.google.com with SMTP id m23so16541146wrh.7 for ; Fri, 19 Mar 2021 03:02:50 -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=uBuSDgADlVyxIdI3BmsAIPCPmlM7WqeZ0Oeo6QVFLts=; b=qCWraLZ0Tszd8AH74n5bMJc2ZNxpli+5BtX1Qu0TYtfObzVU7M02pQnddE7gvCf0iN pxmN8wb8E3OkHG0yFhoGSKXtXucoyZgOukVOd9KIzkfTZ/qis1gqdYkYBY6SgONRydNY KY5mRl/bU+IaXK3gVCVIou3kiMKprdEqZ7Fs1tKl1gCFYmPlEuuIQYwaYZS+QSHTyqNY 47eTUx6l0G5XKkzF68I0V5R/05uQim6YGk9NHHdWAPWlBjSiZ4pqQy1/gCTdPSfuywh9 Pg0WV+45hM8j8NIgdxeyB2hzRuAj3f9jXw1V3wXA8TEq7M0aKy6iE4YGWe1j2bPyM4Xy etHQ== 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=uBuSDgADlVyxIdI3BmsAIPCPmlM7WqeZ0Oeo6QVFLts=; b=UFPgtA9xw1KU7bNN/wUVuqHngLDjeiD9r4CLh1kzN4AFo4meqdiV7p4jDQzs9X2/sP sVHzjtv02NaVh57W7KxH6M71+STacmoclNEO6e9GzOV4DgKi2L4WLgfqkpD3HqH5si+i 6/Aarz2t25FaiylCt1sOfTMD1l2YlhI3WT0c7Uv5+DHaR6s+QKHG50JX5pLirvvd7Imt VfdIwRi71NsDJGCNmx0delLgVQpeYOcW2T5UWLOCv8tHlIO/P+3/4GjSBVq80PwrYKaH 64YMZMjOcS4Ugw/cBB+aDiC5Vnx2wab5pwrzEBXW4hGBPk6QHlUYPJxxTKxrY89Jdian rENA== X-Gm-Message-State: AOAM533D6J20oOoyp6wHZtQlgAhfAvT9yiZ4k4DiSjFpOENljViVpzum j3EtoHUS+fjUi7PY5GsCoaz+Q+VgRwzw X-Google-Smtp-Source: ABdhPJy7gzoGDveYEOxbSnLDlDyWEZkPizO1OQME7oNr7V9YSTxgGFBprzpO+9yJFS/BuMsKlUSbA1VxNmbB X-Received: from r2d2-qp.c.googlers.com ([fda3:e722:ac3:10:28:9cb1:c0a8:1652]) (user=qperret job=sendgmr) by 2002:a05:600c:4ed1:: with SMTP id g17mr2965040wmq.67.1616148170085; Fri, 19 Mar 2021 03:02:50 -0700 (PDT) Date: Fri, 19 Mar 2021 10:01:36 +0000 In-Reply-To: <20210319100146.1149909-1-qperret@google.com> Message-Id: <20210319100146.1149909-29-qperret@google.com> Mime-Version: 1.0 References: <20210319100146.1149909-1-qperret@google.com> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH v6 28/38] 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-20210319_100255_503736_C7C1A7FF X-CRM114-Status: GOOD ( 14.04 ) 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 15de1708cfcd..0a674010afb6 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, @@ -444,6 +443,7 @@ struct stage2_map_data { kvm_pte_t attr; kvm_pte_t *anchor; + kvm_pte_t *childp; struct kvm_s2_mmu *mmu; void *memcache; @@ -533,7 +533,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); } @@ -554,7 +554,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 @@ -600,7 +601,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); } @@ -616,19 +617,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; } @@ -737,7 +743,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);