From patchwork Wed Jul 19 12:18:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13318884 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 558B4C001DC for ; Wed, 19 Jul 2023 12:20:01 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ErjF9zZYBnUiAuVmSrZniN7jBJCXH4m7CwA8LBJ4iT8=; b=1gkf0MuihijaZ8 YfsWfQS3RqzvO1zW52pen8AjUjz+TLOT5A6AmPUJR35WPomF6DSDLLr66gFyXcBIv3MngU19L4oQN TxkZt8drI1GBNlymZHkEh2KfNLq8ghsf005zPd/reukcMx5WuRDY6WX1tiHGeCR7iFOaVg235pRbP Yn4QGkT04NDFfxJyecHxBfXdoWDnceArEan9k8IeJWKePtYBrE0LTcNWWPX6Qe2cHpsH442YtuuPj 0usXpT/Sq7keD5I9ct9g9vCuEn5aUqqvsueG+kzim0RTXpT3rx9UpWmBk5qeFKE0ZHRObIBd+Cy9/ ljtRnarIQ21+FhNWGEkw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qM69b-007ODH-29; Wed, 19 Jul 2023 12:19:39 +0000 Received: from mail-mw2nam12on2061f.outbound.protection.outlook.com ([2a01:111:f400:fe5a::61f] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qM69W-007O9O-36 for linux-arm-kernel@lists.infradead.org; Wed, 19 Jul 2023 12:19:36 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lT4Hn+0PVVo3rDt+eWn5r8qKuc3X4dogYNmojWkqrNbDA1P1CF2PVBR+KP9NG0otGbj7ARI5HJc55SVuV+JM67c6GAkobLC0FeVrF4OqCYnkpV12zDjG1ESc0MGY0cx6iwCC7pbwCweSu4uquV00fikp4kFmcIudJke1wgtEHKC2kLkcs4A53vDGWz455m0+aiSdpgJqb9xF64CUMlUj6nHhLWFrvTmfPyDl8RNTzvwaxaCOeYwe6Xuw+uRgaCmM8KSyLc7uYEmLNxnKDBEWpSkiTY06ukY7hHmmTem5ccU/GoOCayLUJ441UgX1K4RTe8rbezmQh1R65trsPP7MIA== 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=E7ln65n/Hpq4F8/Xi4Ld2pMN/wvlMSEVhslNm/lYul8=; b=av9IOjAfkTccCOaG7Roau4LZeCgkjJI8KG5801kht/tx2V1cHJMqWKWwjq7J8jgmcQy/hrVtXbSoEhpvkmRNKNBFAlMAwNaUJi6lOQEI0vCtc8KbLRHZ5gUBOUaoCy2oRAmQ1hjLH0HWqjf2Jrfhr+3uWprBa3UWKGrN7UehRte6ELSGZNsflVEqpaG0QqhnF7Sqwk6TycvyCJj/WH/4X3iYnmlTJdY9MBF1dcuJErEWKZTVO70UULtOIwrbTOsUboDaGUopmRWVAgA2YWpu4BLeEgT/PaK45dguTPpOfOSsp1r7X4pol/pODo2vMl1PLzDFe/+E5ZDSPFo9FAmQ0Q== 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=E7ln65n/Hpq4F8/Xi4Ld2pMN/wvlMSEVhslNm/lYul8=; b=Kk9d2h/UXu8tHao5JqM+1d+vCXPa9l8xGIjEBGf+Dct6lhHNPTrdeEji4/x6VUQd100CAFLhiJObP/IImT1pABxZlyFbd7cOUnIDDgU0mRFh8wBUa/3ocWYOjR4Qx3bFpCpPBCWQYy5I8LRZfufNB4M5bL0bu5f4QIJ/mOS9SuRhMmItD6CAqOGThlsaZk86z65UezDedI6bVe6rJQe6ZFPwcohIFlz5jAxfHhRf895ZaOc9HeAXn6b5eyFs3c3hnOKfmFCN9s6wJqTY59bQDAUzTpaG3KnGbu+FFz5l1IwaSJTR5/zDDl5QG+zYkEgZgfC3o60lKz78rZ3aeWziDw== 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 DM4PR12MB5359.namprd12.prod.outlook.com (2603:10b6:5:39e::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.33; Wed, 19 Jul 2023 12:19:32 +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; Wed, 19 Jul 2023 12:19:32 +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 v2 3/5] mmu_notifiers: Call invalidate_range() when invalidating TLBs Date: Wed, 19 Jul 2023 22:18:44 +1000 Message-Id: <8f293bb51a423afa71ddc3ba46e9f323ee9ffbc7.1689768831.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-ClientProxiedBy: SY6PR01CA0013.ausprd01.prod.outlook.com (2603:10c6:10:e8::18) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|DM4PR12MB5359:EE_ X-MS-Office365-Filtering-Correlation-Id: bec1a96b-0b83-4e70-ea91-08db8852681e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EtM+koDEZOdci30wx6IY9TSUmvjAhX0GGnKtEVLQsdAbRVZUd+MGCxgdIMOuFqYh+h/8Hyrorrzu+RlGct/iQqczUAdCvxYHLODwTG4xiCHmZzyhMEk+mN8VK2+eDWOwGPyac50mHkkQph0eR2OAJZa8Q9vq1ROxaVi3Ovu7+GNfg4fVvnFJjs1vjXk/WLbqJ8MVEYDp584KLAVgXxJytMN7aWzE6EWiZHmH7xtKoTrIeyJigJS3Ri3CL1HNT1XysVZ+C5XXdQqPxrJppuMmsGpFkeZ1esW0e/eu90a9pMxCvzTaFRMdDtviCb09hddMupdIJyO6V5/OXc/N2jjVBGIx0io4MMixuSHz7EE37Ag2GgBckOZQIU5BUi8m/2SyySYpeHH9mGe++YuGesC+jwiX3hcTh4sdiu2TV2AAq8JI285sydnT3FyImVgGDCzfQldXTo0agCmT+vep+tzbTF/6u8t+0W2wIWVLlrxzf60AsSwmsA1j4S2k3uAalqVbAIfj7oQi1f4X1I5JfLl62x3QrZgykb/9jsnGxcyVqa8y0iYuwgbjfmGercoPHKU7zVJvJTJU01ZbKNTemzij4W94aQnpNTr0IBDXjSt3FgIB2sEDxwGndi2sAHw36JFd 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)(396003)(39860400002)(376002)(366004)(136003)(346002)(451199021)(26005)(5660300002)(8936002)(478600001)(8676002)(6506007)(83380400001)(2616005)(107886003)(2906002)(6512007)(38100700002)(316002)(186003)(7416002)(66946007)(66476007)(66556008)(6666004)(6916009)(4326008)(6486002)(86362001)(36756003)(41300700001)(473944003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 60km7DvzxHdwrqsIpGcxs/Q3i0aOP5via7XTlHRCeP5RCZX+FGHUuIJNn+xBbR35KEFEOxglvfFYoIyEAnWKZLR0MvMYIXSgmkzc7VwQAZ7h+VFpxnKspk/OiAXOvVYiLVbdRir8AzBzaNYFjCuez2bTim4YDD5NIm37p/7+R8dOTXVU/4NyUpouXbLQOVVwA/bl9FRJuwP2jGXJj6ZPBNyoOb0rmd8mQObWwL2gQKHLmIRgAWZPEYdYIy7QyyPUQ8PLBnjSsSXXquaoTHxhEHyh9b8O/YIb1d5hcSXIxmhgFdN6svKIseLGTzPX6k5ySv9p8eO9icoqmI8g9aX1FAtavUlP4ZBBejE4jYnRE9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NaV/kw+In3ynUw5w2uzp9WasuEZN8zFUUnnZa0+ivs1ZR7fHFmCod2W46vKDsqMIWl5CeO0WEbOpD6v//hLUZIrCBJ96SkwDYbk/adSDN70D36XVMo6IOW2d7/+cdPB4KcUzsjR0oI+bZNxFj X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: bec1a96b-0b83-4e70-ea91-08db8852681e X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Jul 2023 12:19:31.9847 (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: H3fNEqTHibnay/bQtxw1p9JiEFoELxCfoOlQxAKb+feCYhlvr7bVZ3tkdpEzC6pcdqnILdxLUZXTMqEeJ2mCpA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5359 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230719_051935_002505_AA9D4356 X-CRM114-Status: GOOD ( 18.45 ) 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 The invalidate_range() is going to become 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 Tested-by: SeongJae Park --- 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 | 3 +++ include/asm-generic/tlb.h | 1 - 6 files changed, 16 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/tlbflush.h b/arch/arm64/include/asm/tlbflush.h index 3456866..a99349d 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_invalidate_range(mm, 0, -1UL); } static inline void __flush_tlb_page_nosync(struct mm_struct *mm, @@ -263,6 +265,8 @@ static inline void __flush_tlb_page_nosync(struct mm_struct *mm, addr = __TLBI_VADDR(uaddr, ASID(mm)); __tlbi(vale1is, addr); __tlbi_user(vale1is, addr); + mmu_notifier_invalidate_range(mm, uaddr & PAGE_MASK, + (uaddr & PAGE_MASK) + PAGE_SIZE); } static inline void flush_tlb_page_nosync(struct vm_area_struct *vma, @@ -396,6 +400,7 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, scale++; } dsb(ish); + mmu_notifier_invalidate_range(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..f3fb49f 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_invalidate_range(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..9724b26 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_invalidate_range(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_invalidate_range(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_invalidate_range(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_invalidate_range(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_invalidate_range(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 267acf2..c30fbcd 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -1036,6 +1037,7 @@ void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, put_flush_tlb_info(); put_cpu(); + mmu_notifier_invalidate_range(mm, start, end); } @@ -1263,6 +1265,7 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) put_flush_tlb_info(); put_cpu(); + mmu_notifier_invalidate_range(current->mm, 0, -1UL); } /* diff --git a/include/asm-generic/tlb.h b/include/asm-generic/tlb.h index b466172..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_range(tlb->mm, tlb->start, tlb->end); __tlb_reset_range(tlb); }