From patchwork Thu Jul 20 08:39:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13320253 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 88C07EB64DC for ; Thu, 20 Jul 2023 09:58:58 +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: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:In-Reply-To:References: List-Owner; bh=AtGppVstQYA/C+OeuPW2TuJ1YACbncJkKOHLiSIR8hU=; b=xONxq7ritB3Pho VQ6Mps57P25MFQE+vTtGyxTzueEdvlMuMS/Lg+sa+tejd/ceDiIdoK2Hy8CwAY/qTsgUMvUfo/q3S HQPsmTB8pSSyhyqvROEcYQywR7dg29fvXtK/vCJzfYV6WwZFqqqdwlvkZTOT1rIIXNFzGziEIkqQx 5xYuZvFvF+uYtDWNpP6vKxXDCKVa+S/X6NWYqdNofdejS3i6vp/U7P9sMvQjfx7JQc+htOBHu4cuK eUg1VFIoCsn37vv1kTtlSyKhQ2gKTbIUQMWYiOgcB/rJrPVOzOnJEAz97bNW6xCEZXqbNVPorlYch GO3RIuKNEhT/0NK7u+1Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qMQQh-00AdgR-2r; Thu, 20 Jul 2023 09:58:39 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qMQQc-00AddY-0b for linux-arm-kernel@bombadil.infradead.org; Thu, 20 Jul 2023 09:58:34 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-ID:Content-Description:In-Reply-To:References; bh=RPc/276d9SPxX1FIJHjG9BBb1nqejmmtat9YQEinW+8=; b=aeb7jsJLmLQTF4ao+8O3Gmwr8j bXxR7v4Z3ptNIsgixjMextIOHfnx5ReKeyVFOnzLeRER58WwyaQTvlIvhA/4EW5qJG5rdc+EbQN2r TpLIUmc5tTWXw19mHcvZ0Va3730l3BjijJFkyI3ycOSd0Yg2PZ87GxLzl2daIzEbjS66JZ6UGDqt5 0wnj91dsQIsO29n1G0vYrfSsB9JTPo4oZwax0qJRfePHgAiP7IGMzBFdDV3rBQkZaIRTgCLqCfcc4 I1JalvD/PI8LUb/UG3LB+D6ODSnAESeZCrEnX64MJ4rELfLR2KdE2YR1lMl4zSEbj0k9tNxioeWEh qdy7ez2Q==; Received: from mail-mw2nam10on20618.outbound.protection.outlook.com ([2a01:111:f400:7e89::618] helo=NAM10-MW2-obe.outbound.protection.outlook.com) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qMPCS-00FQRF-2K for linux-arm-kernel@lists.infradead.org; Thu, 20 Jul 2023 08:39:54 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=czcMPwRKE6Yr2kIDjbNOnrESZnanBo6EyDVBnAvQ74Ej/fqV0NSWZ2H2jQs4ta+JMsT0L5DVawomLtC3p0cFJ4krT2kc2/Qhq9+/X6QdnPQWbnP45+uH+C+ZfJsrfVJQHwV26g8DMtISfpvtrgQzdngWgWN4EJ75wnrShtjaDi361SOm1kA8daLRX9sD4ml6TfiQRh4uwsJ1VBgjs7NCtN3r4+7HivzAxMi2QlOHD6gFEgauL6TYs23Xg0nWexd652e3T1yJl0fuG0Rec9xaZWxLj2zmkGKlOiOQz/jkuJts58B+QEeuOLnHntcS84mD0oRomZw0cijOWIoThX3/Og== 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=RPc/276d9SPxX1FIJHjG9BBb1nqejmmtat9YQEinW+8=; b=cRnJqlopQ99+U8o08s5fZx8AuhiAjTz+hgFA/8+cwpwrHmzOv8y1dd59QXbQ+oGyjnU6vicdbcnBAOotngxDyQ9wKaSduEmRWF1VT0wIXKizsDuEo9B0CBNqHxTzMUq8H2ATtSzoIzaj9OHIx3vKkaKYcyGdtHrvtMd6hb2HUQQ5WP9dgi+2ZnWTLGydK6jbzoyxaC8A6pVc44KBZ3y4d/1/jtmd92swtpDE46S/9OHV5ETK1MREFlbyQjC6oSCkhcDkqkl32xAR4LfgXgtdw1KgLP91auWOvQx8b4C0f6p3U4TWn0hGRFPIs8k2Gz8xCYbD42Gq75vHDSLCxXOHOw== 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=RPc/276d9SPxX1FIJHjG9BBb1nqejmmtat9YQEinW+8=; b=VeLQOsU7dSdTLfcJ23tS5A9pvFzOHBLAAwJtf2rxoUwv0iRk5jSnZta26VH9ITQA8zxiQD6IM24U9d4pNcMmkdYm7jwqC4+lR2tthHwfyAweOFV3CJ+xYOjegDwZ8ufTpRceYj7/5qSj+VfUiav/SEvyXFaWKNRFA51041agt8HEVeh4UHTTSuM25C30Mhx6Skh1Ad8U7kNXaLJt19gYlSkpi5PGZqXtgf8WnNkty5Yd3SD5xm+8dQ9PFGELEtU6ikIdgE06bTG5cYhXSJCATxiXnfCe2akbG8bYn03kbPBDiYk/eMSWHk+nOXmI4c1R/NEWBj8xGkSpWe5QDS0jqQ== 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 CH3PR12MB8712.namprd12.prod.outlook.com (2603:10b6:610:171::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.33; Thu, 20 Jul 2023 08:39:39 +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.6609.025; Thu, 20 Jul 2023 08:39:38 +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, sj@kernel.org, Alistair Popple Subject: [PATCH v3 0/5] Invalidate secondary IOMMU TLB on permission upgrade Date: Thu, 20 Jul 2023 18:39:22 +1000 Message-Id: X-Mailer: git-send-email 2.39.2 X-ClientProxiedBy: SYCP282CA0023.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:80::35) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|CH3PR12MB8712:EE_ X-MS-Office365-Filtering-Correlation-Id: 6517a9dc-8a8f-4543-1827-08db88fcda74 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hH8TX3bFHJJVxgNIpekeIL0B9ThbtIkT3gJOKkKNbql5HXAYsCFPeaan4TFU4c4LtYCRdwKXJjvWkUz6HZ55Hd6WlRQMeglMv6r4IxGjrunCiGKA3CJgvXD1Sp2gj8BWJaOGxDHrxP60uwU8aw33GxjSBRM5/fQV4ZolcE78QtqUAT/2BDUM9hE8+IZLvfpZXvbJvRClqOH8BAHDJyl3BR43Wo9Yv9dpiVD+cvT1P33tlbOfv0AEwrcsrhoBp2v/jmjT9jY+LlNWka0xN3DzmgJyd7QHM8pDU0+oICV6HdXv3jwn1JAntLXAb1zlyBG+XrEsInBdLTwFh05NFo+eQBuJXE4iBpFA9TronxKYvcNVQp6CZhdX/rKtrQI9f4ic2Iaw+SEXvJswzJEQOU/WQVfKjgvP3srxc9NTYObp+dbazDNX3pwYt/4wg74Nfd04ZEar4jVTZNSzww8YNLteDCmA+hFpu+OcfJeYDJIFevCBFca7MF7me00Ki8nBGtghjwm0HXMeR6GVSJcTO9l2BMrhYakQj31p9Uc/Q/srO5OwZqd+rjbHWhgHAxVr+sEdA2RucdyGd4r/IdOEggxotwUem6rRlSJbwaHz8N3L8qmlboB2Z3nziQOGV+/T8MoEyjXa0D6T4vRSXqUsiUxMqQ== 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)(396003)(376002)(346002)(451199021)(66899021)(6486002)(6666004)(478600001)(86362001)(36756003)(2616005)(83380400001)(41300700001)(966005)(6506007)(38100700002)(186003)(6512007)(107886003)(26005)(2906002)(66556008)(316002)(8936002)(66946007)(7416002)(8676002)(66476007)(4326008)(5660300002)(6916009);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: U1mG3T0zL9VTU7mRQtFV0TQ/6xziaz1V+U8+1VxPFbHwWjgxIpyFv3B5UajTGW06PUl4qy3J1n1vcjYir69LrvfmDpRy3TooP8DV68+tfewu7QdWHD5/379pCE/3itr/E+XQ9NtcOHKKGtZB8wKWD46DtlXSKtDOP8TUGInNuoY6a1fWspT7YmqO1lXitKlBIQR25aALR8oYluyu18qWcxRXmzDpjhYV4TtdXaFWBxJWSyOUwOK5SxgBIZEXYN2fp8eA0HlAwjjdFxL3GaqKt17HHoKr2vnq2w8Y/Of5CC9b+5tkhpwEEYbOMcV5TGMnGhayLgW3j1BTTfx0MSJbkEzuIKh+tGYwHn1ApnQ+G1+5SOnqVheJ/0UjtXBY9+26J3PgVRo+msk7ABy+JU4Cg4LXjcg+SwM1smDZ6sQoveUNgFlzHEKYTBX9GjwroHAjpOVREEuiMz/Chw/f7WfRB/Z1iT2ma7nLA///chfLDQexogYJ9liKA1awpIyK1/vQ6Nz7GEiqqdkCcPA9X6u7TcOnZ2+NJQbXXZ36DDgWeFmGGFHPVoei8ejyY7BqEETct+CFOGiu1cJgo1Z+XxNxHD3j0GL6oZJiGolYKZYQoI3jUHPtYNkhxupiBmShZzzivn9CshKzUg9sg1Mgwh3kRMwIBrQoMyH3W1h9HnqhWkzmZ5c+H+RGennmY7imNBzVSlRmCnDf5D0XPfbSpVRWz+eyb82i0BpyEs3JXriJ1P4+STKzBrQcMA/5PIPdyCQC2jutktwosSfQytKdzD2c2RsErQVMGpLmZir8wY0kEFe/j5PiEXS3fhKqaeAw05at82vn7ENy4DXXWGqSuNwT/ex2Uipn8VxWHM8o833d0NfHkIxToSACIt6VVYI20qGnD2j/Qr5Gu4EUikm0BsTYudGzUj93rGS5PHjAODXmHL3YkeuoJJzVqmOXQiWTP9HIkGiRulXMwQJ/AY5VKjIWJ0EqjOaZRIvb5jg5er+Q/gI/KsA+Iy/q67sBm0gXNgNA/Zs4uLbDv2pP8iI1f2V57VANQHiK1hwWfo39/hdqOKsEH/p4D7BDgLwB9O3vUZohb1Mcxw8nDcbdlFxVQgCWG13ZeuTVAlWMtcCxQPnowPScBzlAUcOFhKJELufiJ+JhgxgETOYJzgD1xHUUCCxKUJBE5KjzAgfMP9J6hgDyRiY5LSv4FWfoDvA3hcN44nKc97OprAUDCdUfL1P65Fb5C9cf/KKu//tfw/RzYaolctW64edEPl/VqZAyPKvE3Y9yPTvu7KcG1vN3e+Cy55IQp5z2BXxs7CbDoUN7W2yNpciW0re0KP2zBJqOyYlthSJFpypjBfT5cysD30LqAbRzRGfh9aGfKGOKJGwRhuY2SeboGhHXj/FmrC58EWFJLG22RLIHWFNF/gLJ68mQTkop4UOot3Sx8aNpmiegAyAVPW2FF06E7x/HR2GMXC3E0bcKSaUvZB7a33IWCngmRQjwtRG0wOrD4gTvZTiBq/vZQT40R7uFgsNEIzS9jcGVIBI6H2lXv4WACLdDYJea+qmsNy7DkwqBKbgP8NrEL05Yao8RwV6XCCz7yw20ysjy6RWD X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6517a9dc-8a8f-4543-1827-08db88fcda74 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Jul 2023 08:39:38.4442 (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: Y61I+lzbuFGiKXwm9INIiYmxKCJX98YKxgYdJ7EgKqsD3LeWXHMooRhy86JjNGGJqfZ8YisHinKQC5qbhEroFg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8712 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230720_093952_876354_455898EC X-CRM114-Status: GOOD ( 17.94 ) 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 main change is to move secondary TLB invalidation mmu notifier callbacks into the architecture specific TLB flushing functions. This makes secondary TLB invalidation mostly match CPU invalidation while still allowing efficient range based invalidations based on the existing TLB batching code. Changes for v3: - On x86 call the invalidation when adding pending TLB invalidates rather than when flushing the batch. This is because the mm is required. It also matches what happens on ARM64. Fixes a bug reported by SeongJae Park (thanks!) Changes for v2: - Rebased on linux-next commit 906fa30154ef ("mm/rmap: correct stale comment of rmap_walk_anon and rmap_walk_file") to fix a minor integration conflict with "arm64: support batched/deferred tlb shootdown during page reclamation/migration". This series will need to be applied after the conflicting patch. - Reordered the function rename until the end of the series as many places that were getting renamed ended up being removed anyway. - Fixed a couple of build issues which broke bisection. - Added a minor patch to fix up a stale/incorrect comment. ========== Background ========== The arm64 architecture specifies TLB permission bits may be cached and therefore the TLB must be invalidated during permission upgrades. For the CPU this currently occurs in the architecture specific ptep_set_access_flags() routine. Secondary TLBs such as implemented by the SMMU IOMMU match the CPU architecture specification and may also cache permission bits and require the same TLB invalidations. This may be achieved in one of two ways. Some SMMU implementations implement broadcast TLB maintenance (BTM). This snoops CPU TLB invalidates and will invalidate any secondary TLB at the same time as the CPU. However implementations are not required to implement BTM. Implementations without BTM rely on mmu notifier callbacks to send explicit TLB invalidation commands to invalidate SMMU TLB. Therefore either generic kernel code or architecture specific code needs to call the mmu notifier on permission upgrade. Currently that doesn't happen so devices will fault indefinitely when writing to a PTE that was previously read-only as nothing invalidates the SMMU TLB. ======== Solution ======== To fix this the series first renames the .invalidate_range() callback to .arch_invalidate_secondary_tlbs() as suggested by Jason and Sean to make it clear this callback is only used for secondary TLBs. That was made possible thanks to Sean's series [1] to remove KVM's incorrect usage. Based on feedback from Jason [2] the proposed solution to the bug is to move the calls to mmu_notifier_arch_invalidate_secondary_tlbs() closer to the architecture specific TLB invalidation code. This ensures the secondary TLB won't miss invalidations, including the existing invalidation in the ARM64 code to deal with permission upgrade. Currently only ARM64, PowerPC and x86 have IOMMU with secondary TLBs requiring SW invalidation so the notifier is only called for those architectures. It is also not called for invalidation of kernel mappings as no secondary IOMMU implementations can access those and hence it is not required. [1] - https://lore.kernel.org/all/20230602011518.787006-1-seanjc@google.com/ [2] - https://lore.kernel.org/linux-mm/ZJMR5bw8l+BbzdJ7@ziepe.ca/ Alistair Popple (5): arm64/smmu: Use TLBI ASID when invalidating entire range mmu_notifiers: Fixup comment in mmu_interval_read_begin() mmu_notifiers: Call invalidate_range() when invalidating TLBs mmu_notifiers: Don't invalidate secondary TLBs as part of mmu_notifier_invalidate_range_end() mmu_notifiers: Rename invalidate_range notifier 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/include/asm/tlbflush.h | 2 +- arch/x86/mm/tlb.c | 2 +- drivers/iommu/amd/iommu_v2.c | 10 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 29 +++-- drivers/iommu/intel/svm.c | 8 +- drivers/misc/ocxl/link.c | 8 +- include/asm-generic/tlb.h | 1 +- include/linux/mmu_notifier.h | 104 ++++------------- kernel/events/uprobes.c | 2 +- mm/huge_memory.c | 29 +---- mm/hugetlb.c | 8 +- mm/memory.c | 8 +- mm/migrate_device.c | 9 +- mm/mmu_notifier.c | 49 +++----- mm/rmap.c | 40 +------- 19 files changed, 111 insertions(+), 211 deletions(-) base-commit: 906fa30154ef42f93d28d7322860e76c6ae392ac