From patchwork Tue Jul 18 07:56:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13316834 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8B388EB64DA for ; Tue, 18 Jul 2023 07:57:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231666AbjGRH5Q (ORCPT ); Tue, 18 Jul 2023 03:57:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229847AbjGRH5K (ORCPT ); Tue, 18 Jul 2023 03:57:10 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2072.outbound.protection.outlook.com [40.107.244.72]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E70410F9; Tue, 18 Jul 2023 00:57:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fr3Db1Q1p86dTkyi183SVLGv+4fFxb/Rmr44HlOcUK7xvqt5YCMV4YMyHJC0BHYKc3hx2PPCfidTj2rDb6y8k2i5zTB9d3c5RAbf1Be7X48Is98kHcrqd2TCIqmewg4JC7d+Hf7T4kZ5VhcYspoKTsavB/syuFbTl6FEHlN0DfYb/4Uogko5f0DWWWiK4ne00Z95/0k9t2RdJAaebBrquFE3T8RhCRI9K7dkrbp8xNKe3P1LaH7OkyISp+ONlBgbWZDGx1QsNNavbQ51iZULdp1AfXXA0Az8zyqak4WNfP2eyHkFCUCx74nDjyRB5K1pBEnjyoKSGbwxbYzStHscgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=t/1ZlJ6hZJaDce6b/fep0UVd3G4r1VZkyBk0pDLwBXk=; b=aqBnfrlvs8g6RMbX5cGrapxq0oZIhp/5u7XQZ0lZCU0G1kyI2ni95uLj3H/3L4qrFoHseeP14xvrXCC29z3/ppork5gtXbIJnY8DZkrKWSAPKzEEYoWqsGfCxUsZTuNo03MDGUdvQGjct25n5uDZK6UeuKqj8jlAWkeLkPBS+hx4F8cUOw7/EC7lkWGE2CmJoNlS3QBeHd/zECGUxYtT5GwUYiDIF6V53zxvq+j8nICXGHNknZCXSIqlghphi0k4WgERONSX5fQ6ISUqUreInK/olAE6CObSa2rOI8+zDyqe+pqQA8V0uqc0xO75bhB+PBw8rKHzWejpdemnZfH+EQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=t/1ZlJ6hZJaDce6b/fep0UVd3G4r1VZkyBk0pDLwBXk=; b=RdSTji4gs12tJW++2iqykAh3y+LCjIMoMYtA1XKQ3ItbGQduuJ4pzafCqqoS4Bj08Qd4qQepMMkNh7HddA0VwJ/zi0/ll08CgT3rOJc0J9W0guak0lp4SgVbx86jhi4H6VMnw6Oh7aVN0gPdUGdXR37JRCVaTn2Yx3prILOqpHxQaEfQNXnMMQ/9whJsr3GyhOZiEtNr88eIxyVH3XOQ681jOjS9598d3P43Wx6JsWkiTwdCETeVxfmit71jmmHOor+Xw2gEMKD9O1r6Dfl8sSPWzhg2yw9GChVmowIO2RO7q2f+ccs3kv6HjfisF354Fh84KdnLjO1KvJXOMyRHvg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB8180.namprd12.prod.outlook.com (2603:10b6:510:2b6::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6565.32; Tue, 18 Jul 2023 07:56:58 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::cd5e:7e33:c2c9:fb74]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::cd5e:7e33:c2c9:fb74%7]) with mapi id 15.20.6588.031; Tue, 18 Jul 2023 07:56:58 +0000 From: Alistair Popple To: akpm@linux-foundation.org Cc: ajd@linux.ibm.com, catalin.marinas@arm.com, fbarrat@linux.ibm.com, iommu@lists.linux.dev, jgg@ziepe.ca, jhubbard@nvidia.com, kevin.tian@intel.com, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, mpe@ellerman.id.au, nicolinc@nvidia.com, npiggin@gmail.com, robin.murphy@arm.com, seanjc@google.com, will@kernel.org, x86@kernel.org, zhi.wang.linux@gmail.com, Alistair Popple Subject: [PATCH 3/4] mmu_notifiers: Call arch_invalidate_secondary_tlbs() when invalidating TLBs Date: Tue, 18 Jul 2023 17:56:17 +1000 Message-Id: <791a6c1c4a79de6f99bffc594b53a39a6234e87f.1689666760.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-ClientProxiedBy: SY5PR01CA0082.ausprd01.prod.outlook.com (2603:10c6:10:1f5::13) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB8180:EE_ X-MS-Office365-Filtering-Correlation-Id: 10ac55c0-0b87-41be-7dc8-08db87649017 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: d1R9HuDA+yMQ5Wua+eTSXXiX8nQzHqTrYo6f/pwpzYfA3BI22tEBKnkKDDHkAH+yGyBqZ9o27F19qDhg0W68X8ypetqlz+aE6u566GKGmvN4qvVnqAwUI9q6sMkSaBET+/dwzuoTDLrPXH3YIi2MJSQSJEnNMTKrP1HZw40Ir21u4Ax+MzQmfo7z4sxCPbxfk+h6DYN2SvHaNmhybqvkz7gdyoq6ciCvTW62yORcBizgltMZSR4ekn9IU4ENmGaEcbxyBy4pvjXcxinJvV8RbGWShJjUcyL1ySRvp7n6YP6w/SykOCAruvjNGVxLH2jBUKEzhG7WY/1E9rCQBHMniojF9q9gbsm+PrBZQ6L9c6g81guhpW7JuG9a6QU51OFe2XxNL5wX/coCvmq9sL/slerNmT8z2ooozkh0O7hwLQR+YAGNAxB8BS9uHaZ26fBg8blJHPwqGrEdVetEzfcjnNKbsQjRcfzndleArLMQDljVrqjKlZxvI9nRVDwCdQEv3vprFblG4f/SrQFRuwwQY/XvdcrEQw0DFbY25bry0hNR5UB1yKMUuBjD8wtZFetANuikqzsb2iuW37xeushnuOlR9ME2WxgWc3JQy9eOmn0= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(366004)(136003)(346002)(376002)(396003)(451199021)(86362001)(38100700002)(36756003)(66946007)(478600001)(5660300002)(26005)(41300700001)(107886003)(8676002)(186003)(6506007)(6512007)(8936002)(7416002)(2906002)(66556008)(2616005)(6916009)(83380400001)(6666004)(316002)(4326008)(6486002)(66476007)(473944003);DIR:OUT;SFP:1101; X-MS-Exchange-A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/xTVsz4p3DCC7ydvfL/s6qxOeyWZsrTKsH+lxMw4+gfuSpOe6z0qeMe8nPPDOe82qCgMKUVJD7N20eHe+2VlNspL2ISfw+0USqNvWkru0WnD90JYOmgzup5mrcarA6oiWfSKmsuZ5Rr1i5huuK7IBrYrmp8/m2jBX0btOJ84PFXSne8r/7kQS+X4MwaBqQWjd/hTgE/Yao47JdGJ2pQc7CisECVTFUG2msBSsElIHAyMGdPY0HMHA1z+iDqCaiELhn0xwfcg9FoaNdGjgFPfC9uN8R0l12WK551mNLFRMtK7nFilbfczRcMusmwAlAdazRr4BbWzZTf5AbfEOTMnrJmJ/zVZZ4wfAmXscOeqIDBn5PgDONDEjooEzApgHAfgBjD0TNSg+h13HrxEKktP++saIVppzDuu34YztUT+HYiA0xnxB05XlWMSKarhShy1xxLxCoLczR6RwGu/KGziF6XfXNKjh97d33BQ7GMAxCa8GdlGqnqfugzlzgkMSJhgS4TY5QVfVzmkTHr+yLU9o4TshrTxc+jOpA+lCT6f+sWKeRkL7M X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 10ac55c0-0b87-41be-7dc8-08db87649017 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jul 2023 07:56:58.8316 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: b5oQkw+iskrQNiL2SgbsA1/hfrC74KIshAqfJ0Uf3hs3nzpb3cpHxpQ9kdS64USkXUoOF9lJUv7Uyn4nmG7nlA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB8180 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The arch_invalidate_secondary_tlbs() is an architecture specific mmu notifier used to keep the TLB of secondary MMUs such as an IOMMU in sync with the CPU page tables. Currently it is called from separate code paths to the main CPU TLB invalidations. This can lead to a secondary TLB not getting invalidated when required and makes it hard to reason about when exactly the secondary TLB is invalidated. To fix this move the notifier call to the architecture specific TLB maintenance functions for architectures that have secondary MMUs requiring explicit software invalidations. This fixes a SMMU bug on ARM64. On ARM64 PTE permission upgrades require a TLB invalidation. This invalidation is done by the architecutre specific ptep_set_access_flags() which calls flush_tlb_page() if required. However this doesn't call the notifier resulting in infinite faults being generated by devices using the SMMU if it has previously cached a read-only PTE in it's TLB. Moving the invalidations into the TLB invalidation functions ensures all invalidations happen at the same time as the CPU invalidation. The architecture specific flush_tlb_all() routines do not call the notifier as none of the IOMMUs require this. Signed-off-by: Alistair Popple Suggested-by: Jason Gunthorpe --- arch/arm64/include/asm/tlbflush.h | 5 +++++ arch/powerpc/include/asm/book3s/64/tlbflush.h | 1 + arch/powerpc/mm/book3s64/radix_hugetlbpage.c | 1 + arch/powerpc/mm/book3s64/radix_tlb.c | 6 ++++++ arch/x86/mm/tlb.c | 2 ++ include/asm-generic/tlb.h | 1 - 6 files changed, 15 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/tlbflush.h b/arch/arm64/include/asm/tlbflush.h index 412a3b9..386f0f7 100644 --- a/arch/arm64/include/asm/tlbflush.h +++ b/arch/arm64/include/asm/tlbflush.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -252,6 +253,7 @@ static inline void flush_tlb_mm(struct mm_struct *mm) __tlbi(aside1is, asid); __tlbi_user(aside1is, asid); dsb(ish); + mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL); } static inline void flush_tlb_page_nosync(struct vm_area_struct *vma, @@ -263,6 +265,8 @@ static inline void flush_tlb_page_nosync(struct vm_area_struct *vma, addr = __TLBI_VADDR(uaddr, ASID(vma->vm_mm)); __tlbi(vale1is, addr); __tlbi_user(vale1is, addr); + mmu_notifier_arch_invalidate_secondary_tlbs(vma->vm_mm, uaddr & PAGE_MASK, + (uaddr & PAGE_MASK) + PAGE_SIZE); } static inline void flush_tlb_page(struct vm_area_struct *vma, @@ -358,6 +362,7 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, scale++; } dsb(ish); + mmu_notifier_arch_invalidate_secondary_tlbs(vma->vm_mm, start, end); } static inline void flush_tlb_range(struct vm_area_struct *vma, diff --git a/arch/powerpc/include/asm/book3s/64/tlbflush.h b/arch/powerpc/include/asm/book3s/64/tlbflush.h index 0d0c144..dca0477 100644 --- a/arch/powerpc/include/asm/book3s/64/tlbflush.h +++ b/arch/powerpc/include/asm/book3s/64/tlbflush.h @@ -5,6 +5,7 @@ #define MMU_NO_CONTEXT ~0UL #include +#include #include #include diff --git a/arch/powerpc/mm/book3s64/radix_hugetlbpage.c b/arch/powerpc/mm/book3s64/radix_hugetlbpage.c index 5e31955..17075c7 100644 --- a/arch/powerpc/mm/book3s64/radix_hugetlbpage.c +++ b/arch/powerpc/mm/book3s64/radix_hugetlbpage.c @@ -39,6 +39,7 @@ void radix__flush_hugetlb_tlb_range(struct vm_area_struct *vma, unsigned long st radix__flush_tlb_pwc_range_psize(vma->vm_mm, start, end, psize); else radix__flush_tlb_range_psize(vma->vm_mm, start, end, psize); + mmu_notifier_arch_invalidate_secondary_tlbs(vma->vm_mm, start, end); } void radix__huge_ptep_modify_prot_commit(struct vm_area_struct *vma, diff --git a/arch/powerpc/mm/book3s64/radix_tlb.c b/arch/powerpc/mm/book3s64/radix_tlb.c index 0bd4866..64c11a4 100644 --- a/arch/powerpc/mm/book3s64/radix_tlb.c +++ b/arch/powerpc/mm/book3s64/radix_tlb.c @@ -752,6 +752,8 @@ void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmadd return radix__local_flush_hugetlb_page(vma, vmaddr); #endif radix__local_flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize); + mmu_notifier_arch_invalidate_secondary_tlbs(vma->vm_mm, vmaddr, + vmaddr + mmu_virtual_psize); } EXPORT_SYMBOL(radix__local_flush_tlb_page); @@ -987,6 +989,7 @@ void radix__flush_tlb_mm(struct mm_struct *mm) } } preempt_enable(); + mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL); } EXPORT_SYMBOL(radix__flush_tlb_mm); @@ -1020,6 +1023,7 @@ static void __flush_all_mm(struct mm_struct *mm, bool fullmm) _tlbiel_pid_multicast(mm, pid, RIC_FLUSH_ALL); } preempt_enable(); + mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL); } void radix__flush_all_mm(struct mm_struct *mm) @@ -1228,6 +1232,7 @@ static inline void __radix__flush_tlb_range(struct mm_struct *mm, } out: preempt_enable(); + mmu_notifier_arch_invalidate_secondary_tlbs(mm, start, end); } void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start, @@ -1392,6 +1397,7 @@ static void __radix__flush_tlb_range_psize(struct mm_struct *mm, } out: preempt_enable(); + mmu_notifier_arch_invalidate_secondary_tlbs(mm, start, end); } void radix__flush_tlb_range_psize(struct mm_struct *mm, unsigned long start, diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index eaefc10..0b990fb 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1037,6 +1037,7 @@ void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, put_flush_tlb_info(); put_cpu(); + mmu_notifier_arch_invalidate_secondary_tlbs(mm, start, end); } @@ -1264,6 +1265,7 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) put_flush_tlb_info(); put_cpu(); + mmu_notifier_arch_invalidate_secondary_tlbs(current->mm, 0, -1UL); } /* diff --git a/include/asm-generic/tlb.h b/include/asm-generic/tlb.h index 48c81b9..bc32a22 100644 --- a/include/asm-generic/tlb.h +++ b/include/asm-generic/tlb.h @@ -456,7 +456,6 @@ static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb) return; tlb_flush(tlb); - mmu_notifier_invalidate_secondary_tlbs(tlb->mm, tlb->start, tlb->end); __tlb_reset_range(tlb); }