From patchwork Tue Apr 30 17:21:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13649704 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 4ECDBC4345F for ; Tue, 30 Apr 2024 17:22:19 +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=Z8QKIBzp/LndW+ePuFGXgtBb8QsIKlY9kk38BVGEleY=; b=rpze14iC/x+7Wr NHpvng0QhGszvnH+YiVfdcSqSxKmPpZmQ/HbXXvYEd6pgWa0OPcTtQtqHiW4zoOlX1GS2pwojmDRh 64951Rae5rtOhSEeRO6F8j8hd4u30eiRrcDV17Cr47yzF98JOPQbU06zltoSdgPMnZoZTEyWUFESD jsVDru+39JaLV2RMPV2rtx1Vfcaygqz0Yfqe66HwLliqmh/N3rQTJUuruqCwSqitGBpaiYAMD4ETc sqCbbI9ykuM0ts5wQ8jmUAfv8xsTdq1dJfaXGuCOXJikr0nkD4cpymEAxULIngPXsMHtBeTCJUEKA fKVbSnZioJHR7qhCjXDw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s1rBB-00000007Oey-3I77; Tue, 30 Apr 2024 17:22:09 +0000 Received: from mail-bn8nam12on20601.outbound.protection.outlook.com ([2a01:111:f403:2418::601] helo=NAM12-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s1rAx-00000007OX2-2mXG for linux-arm-kernel@lists.infradead.org; Tue, 30 Apr 2024 17:21:57 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KvX8dat27HkB8X2m3pq18HqyF6gqBFXJhwEDvGvZJfu5KYHp+JGhfM0elluuMWLC1JZs5zua8pYFOyHXqyGHtmx8qGWjVIY0yktslOqk0EJ53tnLfZfcg4mRw4uVfP7UitR6RZzXYAAGFBhNihhNbkXvH07odKViYZZa+m8EUbP9imrw3ZQQ87vssSFD6dZnApwNfdu8wOJ6ImXz+xKFF8u4zOr5pBG20iSSgBtAEFKsLkLxEDSa74CwK5ApEOgsqe/oA4dHXpKEdUIz1yx8ycbwDFOul8Ln/WjOntBSgqro2FRJmMD96VaD13z/x+qJNZn7CvxSu/PazCi+I1vEXA== 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=wGze6D26bkOaVaTHFLcgfCu+3JgXqXg1DoOzkI1N41Y=; b=OvFQxZ+gRFsJZ0o1Komml80S9PvUfvONvrurg2TcNhrELZo+2sNaM381CCowPl1vJFGgK1DD0JDbLhXvN4ZuTlq18tFS5+IGvo5LegwSQ+kRf2CG6JoE+2mMSSHoRkFJzUDa2/ln8t/JI0I177o6mbuvvFPH0oKVsGiO6eSuosvQrFAEXWCqw+dHpmAqJC2XTj06EjqQEdqI+F5V/PCI7Rpv2Yqh+wxROr+POHO0TwkTsn+AP8aPPLOPJbG8RmNHKAJ7aab5Ug55jJr4j5sWEVjG2NsGB8I1p57Q/B5Cav+gSjPWeKpYhmEMdimae9JOsJkQEdoSWsIrVW0KT4l8bQ== 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=wGze6D26bkOaVaTHFLcgfCu+3JgXqXg1DoOzkI1N41Y=; b=TdYFce/v4IQX4GKw8lYWrwJgSotqTOZnLbS1bSAN3DCrv9eQQ7NwCD21IM14KCNp5ajITUfOu6u5PeTOBUSaJJlRQDbnygeuvMSCxkgbVUxfSnZjDFG9tS9gluk5exjKGbsjR0QbNUfezBsuV+ZG5cdVY5m+qY9xvJrlqv7ky+NgNWIYfRLMJ82DEiEFtoHS09YM6eDjMLmO0YpCvK3CiaA3wdMVkegfCslYNVxVNYve5vIaXzdvUft0uN9PBSqD1/+JCD74dX+vCFCW9PHnuoN/d/sb5zZUnYeQA4e6PKlM7BCiwyDKWIYUO6tIGsDSR/5143kEDymLyfblZtjHWQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by DM6PR12MB4172.namprd12.prod.outlook.com (2603:10b6:5:212::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.35; Tue, 30 Apr 2024 17:21:43 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%3]) with mapi id 15.20.7519.031; Tue, 30 Apr 2024 17:21:43 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameer Kolothum , Mostafa Saleh Subject: [PATCH v9 5/9] iommu/arm-smmu-v3: Make arm_smmu_alloc_cd_ptr() Date: Tue, 30 Apr 2024 14:21:37 -0300 Message-ID: <5-v9-5040dc602008+177d7-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v9-5040dc602008+177d7-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR01CA0046.prod.exchangelabs.com (2603:10b6:208:23f::15) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|DM6PR12MB4172:EE_ X-MS-Office365-Filtering-Correlation-Id: f2177a4e-fb1a-4a58-4024-08dc693a010a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|376005|7416005|366007; X-Microsoft-Antispam-Message-Info: b1fpW/ybhoyNvE15fULq+m+qUA5Go0NbgkRUAGOgdZHTL1J50nkRipBJ9L2rpJFLBLtAtf7azU8K07jDbN/jc2HJaDj5vFs66kv80ojzPb/5/FG49C2ul4u8JSmKybOnOFGbmHA0NFbPmbCB17LAiq9l1QjQPEB+z4ugpBdQFheD8wCap/bZ+RLZp1o4amkFxWeHxxdWW5KKBaIwCLl6KdocFJQC5+FAQhGnPN8IXqrAD9DrkH535BuUzQnPfmjcgOQ6U3Iu3eq6fJlSQD06bCf5xBfgwthzpZmYl2mhzpWpdAx/kCNHUeMvocPGQ2f3X/Wh8GcixvLrrKUj50fveLHt7G71EhdhFB5hYDdErekvC9TD4NseZQs02vO3O4m1ol9553SxqwkROkPmzn+qrTkRLY5FCzpDfFKBFj5Fy7fpbZbuPRE8AYpGv4Xaf1SjwOUHHfXK//zHKJLTWkEHfc9Ymil9kVulCcQoR95F80SXupsgTSwnNk9xh+dmSfBAI6JpVTLuiZvDvka6B2+mArX5W7hi82w5FGc/+KvaQxHEeCsbRfdhMbvZY7wTh9by+SCTjtAiezMlQy9+CTsnDIaz6zqzyLyLpbE/H8nbGDo4MLrEltFDYHegEDgHXvzNsvCWMESIAAqLVBy5yEl1Z+cSYOl09ApsWoHf6I6Ri0fHEPPMvPFL4F2x1VBCP55/HWAkvqIj+nqo8V1kkTixd1e+BIOr0KZ8e5+EIXBWdlpgLfmlh07mNaUeWkAAe10U2sMFr70OzTdS0RU6GjArK4nZroylZl69BedLrUsQNQ57eM4QNtdcWkoMYhGwIASZTaxSmdMOFPOjAnZjcm2eZOIxfyIZgNAxDhYdz+5xqDEWKZv+Uk6OiPX6pVew5SKdg2faVsq3S3Y5MYHu4LkpM9a/4PQWWtYGJJ0qktG48gVRZdSI7eBfflfZNMbvjyaBLIEpW8zL+cbTVoA+hjqYJ900kbXnlnipKRIceZllYEko6OvTGbIIPf2TJbQr49p0Ab/VOYxuigA2SbuPCKf1RCfXKtJnoMkAfivpl7XrrwIkBB5PmqyLzckyH73StYhn+HXhltMs4cC5vARvVC15IPzhlkTLVPPcUuQ4UuxxqfuKCZbRpQafPy8wSksfwBWTkADuCcn4JlVW7aaFkljaWcV0seaWSHKGSvHHos8Rr4+ZUEbnuH2ZBF+refMAxG29qsoqhPqsciqlIiGy13CMqQyJcqABIeMjzQ7+XwmPs57O9hr9IbuP1QE5hLQ3iDhlxbvm2bwlXpxiHr14t4WdFQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(1800799015)(376005)(7416005)(366007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-Ch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cRltFCd5F+PamxEeX86ToYWwWWRF6BtLcOIYXZaom1DYG5p/ZQBPavOAaCCGx1H7gRJv8kj+mv2Qsb3fV1mGrU78RgMQVzCD5hx4YJGfHESvxNTLt0ZE7S6gP+1t+ULxixSojJRlHIPQgmI5+QDAs8R1lTazuZwTF6Bzkqt7b/DRfIahsHcd9gZFrAtmn8HMag6JSY5H4Ti0gLAdBRTJIe1WDvzcm9e4J1Wga3PngJErGK0F7DMae4v94EdeQgGm3nbcxPduH/EtZZSkXhGblOIrMUjEVtJaor46bEvjQobmNwqGe8AshI6Q50uF7F0Ys82inyahc+zMZGFsBwgJeoJvOu5pzDQqTtx/tuOvmhQzMcrFZAHFDchiYqKdH1LtOlTUYivUSVZgM+1cXrkSZzJrloT8cyqaP0A6ktccc+OFsZqEyE1KJ8PQM/Su8lLXAo0N3yNBQDI/Rg3faGBXxwnZcWIQiA52Siikc3UH6YtUGIhaBa2IH58gMlfNGomTazoi72HJ6O17YwNcQIJPRsGuF7u5D2pXgcrXJtYAOEGEUj X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: f2177a4e-fb1a-4a58-4024-08dc693a010a X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Apr 2024 17:21:42.7648 (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: 0baSJfp/LCwWXZj6QunsQKy4yWHEUUm28mbTFSOzXpjOI9XKrpsbnChC7SynyzKp X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4172 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240430_102155_790961_2D05EF48 X-CRM114-Status: GOOD ( 16.12 ) 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 Only the attach callers can perform an allocation for the CD table entry, the other callers must not do so, they do not have the correct locking and they cannot sleep. Split up the functions so this is clear. arm_smmu_get_cd_ptr() will return pointer to a CD table entry without doing any kind of allocation. arm_smmu_alloc_cd_ptr() will allocate the table and any required leaf. A following patch will add lockdep assertions to arm_smmu_alloc_cd_ptr() once the restructuring is completed and arm_smmu_alloc_cd_ptr() is never called in the wrong context. Tested-by: Nicolin Chen Reviewed-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 61 +++++++++++++-------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 3 +- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index 8a6c8aa247eb38..7dce9cf52ee37c 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -97,6 +97,7 @@ static struct arm_smmu_option_prop arm_smmu_options[] = { static int arm_smmu_domain_finalise(struct arm_smmu_domain *smmu_domain, struct arm_smmu_device *smmu); +static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master); static void parse_driver_options(struct arm_smmu_device *smmu) { @@ -1206,29 +1207,51 @@ static void arm_smmu_write_cd_l1_desc(__le64 *dst, struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid) { - __le64 *l1ptr; - unsigned int idx; struct arm_smmu_l1_ctx_desc *l1_desc; - struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; + if (!cd_table->cdtab) + return NULL; + if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_LINEAR) return (struct arm_smmu_cd *)(cd_table->cdtab + ssid * CTXDESC_CD_DWORDS); - idx = ssid >> CTXDESC_SPLIT; - l1_desc = &cd_table->l1_desc[idx]; - if (!l1_desc->l2ptr) { - if (arm_smmu_alloc_cd_leaf_table(smmu, l1_desc)) - return NULL; + l1_desc = &cd_table->l1_desc[ssid / CTXDESC_L2_ENTRIES]; + if (!l1_desc->l2ptr) + return NULL; + return &l1_desc->l2ptr[ssid % CTXDESC_L2_ENTRIES]; +} - l1ptr = cd_table->cdtab + idx * CTXDESC_L1_DESC_DWORDS; - arm_smmu_write_cd_l1_desc(l1ptr, l1_desc); - /* An invalid L1CD can be cached */ - arm_smmu_sync_cd(master, ssid, false); +static struct arm_smmu_cd *arm_smmu_alloc_cd_ptr(struct arm_smmu_master *master, + u32 ssid) +{ + struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; + struct arm_smmu_device *smmu = master->smmu; + + if (!cd_table->cdtab) { + if (arm_smmu_alloc_cd_tables(master)) + return NULL; } - idx = ssid & (CTXDESC_L2_ENTRIES - 1); - return &l1_desc->l2ptr[idx]; + + if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_64K_L2) { + unsigned int idx = ssid / CTXDESC_L2_ENTRIES; + struct arm_smmu_l1_ctx_desc *l1_desc; + + l1_desc = &cd_table->l1_desc[idx]; + if (!l1_desc->l2ptr) { + __le64 *l1ptr; + + if (arm_smmu_alloc_cd_leaf_table(smmu, l1_desc)) + return NULL; + + l1ptr = cd_table->cdtab + idx * CTXDESC_L1_DESC_DWORDS; + arm_smmu_write_cd_l1_desc(l1ptr, l1_desc); + /* An invalid L1CD can be cached */ + arm_smmu_sync_cd(master, ssid, false); + } + } + return arm_smmu_get_cd_ptr(master, ssid); } struct arm_smmu_cd_writer { @@ -1354,7 +1377,7 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, if (WARN_ON(ssid >= (1 << cd_table->s1cdmax))) return -E2BIG; - cd_table_entry = arm_smmu_get_cd_ptr(master, ssid); + cd_table_entry = arm_smmu_alloc_cd_ptr(master, ssid); if (!cd_table_entry) return -ENOMEM; @@ -2671,13 +2694,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) struct arm_smmu_cd target_cd; struct arm_smmu_cd *cdptr; - if (!master->cd_table.cdtab) { - ret = arm_smmu_alloc_cd_tables(master); - if (ret) - goto out_list_del; - } - - cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); + cdptr = arm_smmu_alloc_cd_ptr(master, IOMMU_NO_PASID); if (!cdptr) { ret = -ENOMEM; goto out_list_del; diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h index 99fd6f24caa818..c5c55d3e281865 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -275,8 +275,7 @@ struct arm_smmu_ste { * 2lvl: at most 1024 L1 entries, * 1024 lazy entries per table. */ -#define CTXDESC_SPLIT 10 -#define CTXDESC_L2_ENTRIES (1 << CTXDESC_SPLIT) +#define CTXDESC_L2_ENTRIES 1024 #define CTXDESC_L1_DESC_DWORDS 1 #define CTXDESC_L1_DESC_V (1UL << 0)