From patchwork Fri Jul 22 01:50:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 12925870 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 5F59BCCA485 for ; Fri, 22 Jul 2022 01:52:20 +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=s970yZLIC1GFEaSjBX901BwpKgghJLVqg5N6D78Vf7k=; b=av1Ci2DzYT+cuRxceH9SVp4RXK f2XylHmYt/FyXp7lXEmRFPPCne91jknmXzeW1AQKZECgv6AuwyVx+3mowr79gO0zhUvoX8wmYh2lz jzVqFJFdNYH0JkvckOYoJUGDyZupN3pPpIy8hB7HejnOh8B7cYZfCHxSbubvFDo4qFKPSXNLzNiHA hdN+ld5laQKhNSuVwD0POGEBhZVgRpY/v9bZpijMqMpnqmJPlEotsw9trVFsWXKCF0LKtYIFCC8Kc KCr1mDSdH6ynkvZdCVmKn1ifznXRGYiFxhzrmMQuofg6vJNLD6mIOFk4hd++txaRMxTwJaD2yA/QW 9+LAgPBQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oEhp1-00FmMo-SK; Fri, 22 Jul 2022 01:51:20 +0000 Received: from mail-pf1-x44a.google.com ([2607:f8b0:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oEhoj-00FlbO-EQ for linux-arm-kernel@lists.infradead.org; Fri, 22 Jul 2022 01:51:02 +0000 Received: by mail-pf1-x44a.google.com with SMTP id y10-20020a056a00180a00b0052b10093100so1252289pfa.4 for ; Thu, 21 Jul 2022 18:51:00 -0700 (PDT) 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=4BZROGdIJ6ey3ykeGXKKMyG5fxGLOs9dtND9v0m18DI=; b=MwDh0r7e9Z50plp25nl4fU2XJ0ebh5++1X9yJJYV97pjuLK0ahyEY3Y8MqgsKA30wo rDCddGIzUUQ2BQH+lTn83Rja5L3SCm6AZpaWt5SXKfXP8DCl4YIJKMCTCFvVTq2vvadH PuySAk39zirowz3Gyd8+qtVDsZXRxjmr6MpDmamAqsuZex6XJHHbyOA48OK2baHJCwEn 83i97+hNJ7QVpTrzc3m5GFh2VtOAQbiLIxElelsydLR0K+KYHRweZq3CPpJI1rDgH0cV kh6oM05Ggd5zJVKnTVDWLz6qVwtNJZY96QC2Cf+7mEoOOYjlbzYJP+Z2lxv1xHyYq7Ol t5jA== 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=4BZROGdIJ6ey3ykeGXKKMyG5fxGLOs9dtND9v0m18DI=; b=EkScJp1SX3UNXY8IPvP8KfVXikjw54AfW1onpwCNbx7KDmiFZ2ftBq3oH4BOCWWjgn 4rPS6o6XOtxdRKYVi+1YACsQq7M0JCK+oZGQmdceFC+MySx7TltcHUkivHjmr1Zs3Cu6 VpMGB2107Q+buYwgeY5ZdwlhpA1hXdngQO0/Jvt8ylZN7mwX5P89T2vNp1LKcDLKX+OH /c7U3VBq0w7Xht+Ia0fNOymlqkc7P0Smo9wONVKAbRqSEzKCMhRtxFS4cxeDBtknauQh lR18vNlc6GYWJybQf8SfKxooKEsI1HMCTEJ+zPijrmibgTt2GKEKhd1+xCXvTfCv8Q36 KAsQ== X-Gm-Message-State: AJIora+SmMxIoq8LaW6BgvrnZrPqdJnozbsFg8EaN+1mB+1LGqnIe5SP Ts+1Hleypv2xNWC8HeUeeikt6/R2qIsSu26ZQYrYIw9H4mNXrkHwgnR6slHfxw8HOUsm49GPXl7 7ZGf6r5dkZgJTo8xxuyAJ1Avkdiz3eEIJLJcKWufwaXtrsjBa8ISIjMwKNuv4S7ZXa8eP3ZKC X-Google-Smtp-Source: AGRyM1vbtFVBRffsajgh0wYnjlyqGlcLkaXaKkTOQVsukeXgRgms6r3xxeGo4tZIWhNUOJdgZJEPUcI= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:200:7ed4:5864:d5e1:ffe1]) (user=pcc job=sendgmr) by 2002:a17:90a:69e1:b0:1f2:2c0a:c30f with SMTP id s88-20020a17090a69e100b001f22c0ac30fmr7330979pjj.8.1658454659739; Thu, 21 Jul 2022 18:50:59 -0700 (PDT) Date: Thu, 21 Jul 2022 18:50:28 -0700 In-Reply-To: <20220722015034.809663-1-pcc@google.com> Message-Id: <20220722015034.809663-3-pcc@google.com> Mime-Version: 1.0 References: <20220722015034.809663-1-pcc@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v2 2/7] KVM: arm64: Simplify the sanitise_mte_tags() logic From: Peter Collingbourne To: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu Cc: Catalin Marinas , Cornelia Huck , Will Deacon , Marc Zyngier , Evgenii Stepanov , kvm@vger.kernel.org, Steven Price , Vincenzo Frascino , Peter Collingbourne X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220721_185101_526255_94801AC4 X-CRM114-Status: GOOD ( 22.00 ) 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 From: Catalin Marinas Currently sanitise_mte_tags() checks if it's an online page before attempting to sanitise the tags. Such detection should be done in the caller via the VM_MTE_ALLOWED vma flag. Since kvm_set_spte_gfn() does not have the vma, leave the page unmapped if not already tagged. Tag initialisation will be done on a subsequent access fault in user_mem_abort(). Signed-off-by: Catalin Marinas Cc: Will Deacon Cc: Marc Zyngier Cc: Steven Price Cc: Peter Collingbourne --- arch/arm64/kvm/mmu.c | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index c9012707f69c..1a3707aeb41f 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1056,23 +1056,14 @@ static int get_vma_page_shift(struct vm_area_struct *vma, unsigned long hva) * - mmap_lock protects between a VM faulting a page in and the VMM performing * an mprotect() to add VM_MTE */ -static int sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn, - unsigned long size) +static void sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn, + unsigned long size) { unsigned long i, nr_pages = size >> PAGE_SHIFT; - struct page *page; + struct page *page = pfn_to_page(pfn); if (!kvm_has_mte(kvm)) - return 0; - - /* - * pfn_to_online_page() is used to reject ZONE_DEVICE pages - * that may not support tags. - */ - page = pfn_to_online_page(pfn); - - if (!page) - return -EFAULT; + return; for (i = 0; i < nr_pages; i++, page++) { if (!page_mte_tagged(page)) { @@ -1080,8 +1071,6 @@ static int sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn, set_page_mte_tagged(page); } } - - return 0; } static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, @@ -1092,7 +1081,6 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, bool write_fault, writable, force_pte = false; bool exec_fault; bool device = false; - bool shared; unsigned long mmu_seq; struct kvm *kvm = vcpu->kvm; struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache; @@ -1142,8 +1130,6 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, vma_shift = get_vma_page_shift(vma, hva); } - shared = (vma->vm_flags & VM_SHARED); - switch (vma_shift) { #ifndef __PAGETABLE_PMD_FOLDED case PUD_SHIFT: @@ -1264,12 +1250,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, if (fault_status != FSC_PERM && !device && kvm_has_mte(kvm)) { /* Check the VMM hasn't introduced a new VM_SHARED VMA */ - if (!shared) - ret = sanitise_mte_tags(kvm, pfn, vma_pagesize); - else + if ((vma->vm_flags & VM_MTE_ALLOWED) && + !(vma->vm_flags & VM_SHARED)) { + sanitise_mte_tags(kvm, pfn, vma_pagesize); + } else { ret = -EFAULT; - if (ret) goto out_unlock; + } } if (writable) @@ -1491,15 +1478,18 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range) { kvm_pfn_t pfn = pte_pfn(range->pte); - int ret; if (!kvm->arch.mmu.pgt) return false; WARN_ON(range->end - range->start != 1); - ret = sanitise_mte_tags(kvm, pfn, PAGE_SIZE); - if (ret) + /* + * If the page isn't tagged, defer to user_mem_abort() for sanitising + * the MTE tags. The S2 pte should have been unmapped by + * mmu_notifier_invalidate_range_end(). + */ + if (kvm_has_mte(kvm) && !page_mte_tagged(pfn_to_page(pfn))) return false; /*