From patchwork Tue Nov 5 18:14:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13863348 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 7A24ED3177E for ; Tue, 5 Nov 2024 18:24:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Content-Type: Content-Transfer-Encoding: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=jIANugGwnks22NqB2MR2YkkiCEPRDteg9Ba1oy9SDAU=; b=FhLslxBAZ9UymQiZkGntbN39/3 OPj5nRWUt+JcZhDJyXTFy8JPj/zDOrpkebgTVdEj4pDDzMve5ajonYB5VYx/onn2mCzTvLpaLI0H6 ffJX5+qtXFnehmgQtqsjxCbcpZjnRFsPuoUb7YSFYKu9RiZSPJ+ySDPyFgowoRbCnMIMpdPRKfvVz mckg/OBXmRzRsx/R06fFbZ/wfB0HKKaUI7Mg2kGGmlme5VHu77u7d75Y1Ey4HMSCjrMy7aEBwSZNf wVhWl7aOuO85FcQz2N58feyuqjQ7qhXw6WrxLFbu/YlbKNFgfZnp8UvcgcZoOePN+Nn6M0eSBXoyN 1qbUMhhg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t8ODh-00000000MSp-1D7T; Tue, 05 Nov 2024 18:24:01 +0000 Received: from mail-dm6nam04on20605.outbound.protection.outlook.com ([2a01:111:f403:2409::605] helo=NAM04-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t8O58-00000000LGm-3ofH for linux-arm-kernel@lists.infradead.org; Tue, 05 Nov 2024 18:15:22 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=GDMxhwEzLh/5pUVrDVRbCDjJiMnnxjUrKaD3iLnFou+V2akgJgVU1XQJONVXw4pQ8Out3GzvkgZ6GMGlIVwIqGxzyoRn/ewWeEUOZfO12SFhiSwxT2x/U6gCKHimhHiDIXcx8zXxCIdpJ6k+vMjwIoXd0De8eGszQpRu4pnuRq8wrE7iX3/3WpWGDgcncu6QrHFH2wV8FsE/Wl38XaPDuT91e1QEwbYWIaFD0sgM/4ryVXpnFu7aXY5l4KD5WAJmBvIiAvLsXnCgg79XP8Qqsj0DSGK5LQcnB52P+T4DO1GNSaHtUW1H+okHkNTFJKVD7a0S9aztc+FKE5Ym8GAksA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=jIANugGwnks22NqB2MR2YkkiCEPRDteg9Ba1oy9SDAU=; b=sfGUYHSeyF0jODt/AHSLh49qsNqQ/413b4phEF86FDM/QPX5JFsxZcBTIAocsDQF5CAyNqtOYCvG6Ukkr2T0HcyTL3xCXBG6IPfHYeT8taHHLBZaAulj5DcO6Y4s2NiyxzZDwXtcldKrWjl4GiOVTtBbuWd3JXdEIVyMdewOUasdEAd0lGwsVAnP7aRSMTxAlD48Aw3u78skfz90nmE5ZRJ64TZ9vReSrXtIJZDefPbiMwRd4poEIFkYbyuwnJoepSC/NBBWZRyAYgGFl4dR0Y9TWrS053qEML1VkGSoy+MIVaPB8GegGLH+nZFkpxLcRtNx7ONnD8IEZ5v2FZklPQ== 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=jIANugGwnks22NqB2MR2YkkiCEPRDteg9Ba1oy9SDAU=; b=jfKbGY+Yq8hTV1JDouK1v70d5+mg7OuAqu6nYDgOEtsuNMnFb0gxplkfwn3GRwCuyx+0kBruSKAgC+XKfnGURUECLsHD/GCSNxYHhnLq4NVbZbS4JJ0ZasvJFE9OEcVxmQqad5+HGXAh6EluAVs0HdNfneB2Yo83GEKoMdo8b7LzQZCUL7rx0Nxaw2r2slghltpFlNQvx3lHV3ATp8Tiqa2Bv46Z5SUtysZ3KEGMTHOQDij2Zrteed+8+C3rEUP/tclEbJcD2K/z8dGgunWbegeHBOq+14X4qVyxtfH5+4D9gX/lITL2O8LKiduRMpiLP+nplKA90n7YYpwthN9FgQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH3PR12MB8659.namprd12.prod.outlook.com (2603:10b6:610:17c::13) by IA0PR12MB7508.namprd12.prod.outlook.com (2603:10b6:208:440::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8137.18; Tue, 5 Nov 2024 18:14:28 +0000 Received: from CH3PR12MB8659.namprd12.prod.outlook.com ([fe80::6eb6:7d37:7b4b:1732]) by CH3PR12MB8659.namprd12.prod.outlook.com ([fe80::6eb6:7d37:7b4b:1732%4]) with mapi id 15.20.8137.018; Tue, 5 Nov 2024 18:14:28 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Boris Brezillon , dri-devel@lists.freedesktop.org, Liviu Dudau , patches@lists.linux.dev, Steven Price Subject: [PATCH v3 1/3] iommu/io-pgtable-arm: Remove split on unmap behavior Date: Tue, 5 Nov 2024 14:14:24 -0400 Message-ID: <1-v3-b3a5b5937f56+7bb-arm_no_split_jgg@nvidia.com> In-Reply-To: <0-v3-b3a5b5937f56+7bb-arm_no_split_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR05CA0052.namprd05.prod.outlook.com (2603:10b6:208:236::21) To CH3PR12MB8659.namprd12.prod.outlook.com (2603:10b6:610:17c::13) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR12MB8659:EE_|IA0PR12MB7508:EE_ X-MS-Office365-Filtering-Correlation-Id: 8264e3de-73e4-49d6-e7a1-08dcfdc5af89 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|7416014|376014|366016; X-Microsoft-Antispam-Message-Info: 1P4glLet5wBtg0Tb4Na6agMq+CMnMJOGeSTONbnJ8UWptdjNoWlivnarY92wCwbuYguJch/g2jErpWQCIheqXLixnt0eLl8qUZFcQNWq5txoa4H4AGuxLcbyqd84gn02+ELPn57BLEHwFC5Ju0IclI6lmTHxexjl2Ut2ZJ29ZnC4N1mbGzubcoUmqvcxQXpheDNlIhk2destXMVAkgvJRJ+dL4bIpQuH2n8k4tSKixw6Uj4Q1NcdZUAN6CLUIpnPIdS5bTYjJr506kpS206O54DfqGvysLyfeBLSp6wAXAu7e2g4NgLOsnlAwdzeCqQnl44NG3T/C8ejGmL1gI6zAKx3Y430gU5+n63K+BriHOMot/q/WtsMa3hbxknp6sD/FQPKuHY1ijJ6iJruVM5nYkQaZ1DXV0yQB3B9ElO55NGdf9vENT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QnwtDDRjwLfe7FzoNMt2mzGH6U0vh0HH0Ioiaevk3Z3mNAX5kyv2S7GFefjnweD5N+CZCeoX8ahP5pHvfb+lFVGg1X25yjOCq/E7iwDb/VDhTj3zTij5MMPdFUN5Ti3TBkth4Ud62ydnHqnMsBDXz2H4QFlBJM/bwlQFX7G7zqXtBLWxKf2KxaSPuSi6gLlMln+ApqVyeQlZTQyQJnub4HcFXrEujNiURQX9Z9Nv8uEqOG1mwZ6BsnbJaf9OsG2m1Ur4R4y81dj29ubVsU7dzILeX37l8RKvihjPNiTQuyvElXl5ALRRiXmPgu0RfDnZsoLQiwBlLSqkH5gArnCHZ+Z/ed6wTwYzUAYjz223p9AqeSWY/JkUgy6DvgtGgIA0ePHAX4Fqn417u6wbh5SmqgbguIzEpdRlBIVIfShRxChbarxYE7oQA9kAAOc5A395VZMttRrunE4TOsinK6866F22BCjfHb/PJXJTaCSJjFkHitiyghw8I0WRWTYWZd34k8Q9wNCNv42jrLxYNveoNfXSw+TLPJwm9Jq8bSMIoBOajCeAhD0uXk1GXC4fcHXcRbVAOJQaG X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8264e3de-73e4-49d6-e7a1-08dcfdc5af89 X-MS-Exchange-CrossTenant-AuthSource: CH3PR12MB8659.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Nov 2024 18:14:27.6525 (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: 7T/RSCxsZyB8hZ8Ha3tA0z/1jzrD3ESLFouvLwOw/vVXuFPw43bxwEd3ZnHuGUTZ X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7508 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241105_101510_967283_E8352B47 X-CRM114-Status: GOOD ( 20.98 ) 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 A minority of page table implementations (arm_lpae, armv7) are unique in how they handle partial unmap of large IOPTEs. Other implementations will unmap the large IOPTE and return it's length. For example if a 2M IOPTE is present and the first 4K is requested to be unmapped then unmap will remove the whole 2M and report 2M as the result. arm_lpae instead replaces the IOPTE with a table of smaller IOPTEs, unmaps the 4K and returns 4k. This is actually an illegal/non-hitless operation on at least SMMUv3 because of the BBM level 0 rules. Will says this was done to support VFIO, but upon deeper analysis this was never strictly necessary: https://lore.kernel.org/all/20241024134411.GA6956@nvidia.com/ In summary, historical VFIO supported the AMD behavior of unmapping the whole large IOPTE and returning the size, even if asked to unmap a portion. The driver would see this as a request to split a large IOPTE. Modern VFIO always unmaps entire large IOPTEs (except on AMD) and drivers don't see an IOPTE split. Given it doesn't work fully correctly on SMMUv3 and relying on ARM unique behavior would create portability problems across IOMMU drivers, retire this functionality. Outside the iommu users, this will potentially effect io_pgtable users of ARM_32_LPAE_S1, ARM_32_LPAE_S2, ARM_64_LPAE_S1, ARM_64_LPAE_S2, and ARM_MALI_LPAE formats. Cc: Boris Brezillon Cc: Steven Price Cc: Liviu Dudau Cc: dri-devel@lists.freedesktop.org Reviewed-by: Liviu Dudau Signed-off-by: Jason Gunthorpe --- drivers/iommu/io-pgtable-arm.c | 81 +--------------------------------- 1 file changed, 2 insertions(+), 79 deletions(-) diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c index 0e67f1721a3d98..4e7689f5f12b4c 100644 --- a/drivers/iommu/io-pgtable-arm.c +++ b/drivers/iommu/io-pgtable-arm.c @@ -569,66 +569,6 @@ static void arm_lpae_free_pgtable(struct io_pgtable *iop) kfree(data); } -static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, - struct iommu_iotlb_gather *gather, - unsigned long iova, size_t size, - arm_lpae_iopte blk_pte, int lvl, - arm_lpae_iopte *ptep, size_t pgcount) -{ - struct io_pgtable_cfg *cfg = &data->iop.cfg; - arm_lpae_iopte pte, *tablep; - phys_addr_t blk_paddr; - size_t tablesz = ARM_LPAE_GRANULE(data); - size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data); - int ptes_per_table = ARM_LPAE_PTES_PER_TABLE(data); - int i, unmap_idx_start = -1, num_entries = 0, max_entries; - - if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) - return 0; - - tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg, data->iop.cookie); - if (!tablep) - return 0; /* Bytes unmapped */ - - if (size == split_sz) { - unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data); - max_entries = ptes_per_table - unmap_idx_start; - num_entries = min_t(int, pgcount, max_entries); - } - - blk_paddr = iopte_to_paddr(blk_pte, data); - pte = iopte_prot(blk_pte); - - for (i = 0; i < ptes_per_table; i++, blk_paddr += split_sz) { - /* Unmap! */ - if (i >= unmap_idx_start && i < (unmap_idx_start + num_entries)) - continue; - - __arm_lpae_init_pte(data, blk_paddr, pte, lvl, 1, &tablep[i]); - } - - pte = arm_lpae_install_table(tablep, ptep, blk_pte, data); - if (pte != blk_pte) { - __arm_lpae_free_pages(tablep, tablesz, cfg, data->iop.cookie); - /* - * We may race against someone unmapping another part of this - * block, but anything else is invalid. We can't misinterpret - * a page entry here since we're never at the last level. - */ - if (iopte_type(pte) != ARM_LPAE_PTE_TYPE_TABLE) - return 0; - - tablep = iopte_deref(pte, data); - } else if (unmap_idx_start >= 0) { - for (i = 0; i < num_entries; i++) - io_pgtable_tlb_add_page(&data->iop, gather, iova + i * size, size); - - return num_entries * size; - } - - return __arm_lpae_unmap(data, gather, iova, size, pgcount, lvl, tablep); -} - static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, struct iommu_iotlb_gather *gather, unsigned long iova, size_t size, size_t pgcount, @@ -678,12 +618,8 @@ static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, return i * size; } else if (iopte_leaf(pte, lvl, iop->fmt)) { - /* - * Insert a table at the next level to map the old region, - * minus the part we want to unmap - */ - return arm_lpae_split_blk_unmap(data, gather, iova, size, pte, - lvl + 1, ptep, pgcount); + WARN_ONCE(true, "Unmap of a partial large IOPTE is not allowed"); + return 0; } /* Keep on walkin' */ @@ -1347,19 +1283,6 @@ static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) iova += SZ_1G; } - /* Partial unmap */ - size = 1UL << __ffs(cfg->pgsize_bitmap); - if (ops->unmap_pages(ops, SZ_1G + size, size, 1, NULL) != size) - return __FAIL(ops, i); - - /* Remap of partial unmap */ - if (ops->map_pages(ops, SZ_1G + size, size, size, 1, - IOMMU_READ, GFP_KERNEL, &mapped)) - return __FAIL(ops, i); - - if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) - return __FAIL(ops, i); - /* Full unmap */ iova = 0; for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {