From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from DUZPR83CU001.outbound.protection.outlook.com (mail-northeuropeazon11012040.outbound.protection.outlook.com [52.101.66.40]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 740553EC833; Tue, 6 Oct 2026 12:15:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.66.40 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791288914; cv=fail; b=q0eFH9vGWXEeomvgfmHiJtEyYsNXyGkJnZx9dVcaRT4cvQDuF1PO86coqhfU50GSX7qj060bzMMrIaH4Q3QSBizByWjGLQxBUfXneS0ZF9aVkzvD1uwiqFpJO2bAExEbrHNpfW+QZwQmjvicgykE+mhadm+XBCnrpAqBm2lIn3A= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791288914; c=relaxed/simple; bh=wCliVYBWdQGiIiiNBpzJ8pw7NTp1MwG8bbccDUkXVwc=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=mLGHLgLsU0xFK6e8M8SfpmOPQwK/SFzu6VHoH5AA6HXxlDivYLY7OgfodHDXP49zw8UUqqQTrbEL4zGFb+vlbLE6kKwmrvqVJncH62fw69VXd28TLWw1UTHRCoQzQIWgNitKz4DSpC88HyJQ1/2BDrYWekHu6+hetI9JGvrlm34= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=oss.nxp.com; spf=pass smtp.mailfrom=oss.nxp.com; dkim=pass (2048-bit key) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b=qA7YGyx8; arc=fail smtp.client-ip=52.101.66.40 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=oss.nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.nxp.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b="qA7YGyx8" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Q06Vp1nGgYhkm3N8UXS/ONGeJms4V5U5GBz2GPG8h1TeufZXo/hD75oGRo7ygwopo8tKKWjrtfedGqDk+O4iOTEUo9+ekotjmNnJjXR7UlU9PkvnrFzrvAO1Cbg5xdaJU9KqIx0GzFdiz6EsYNXwjD2edfrk6/N9a7h/yeoi9ATfndBflDGTehYCUxOWDe2oQh+3m5lZnQMir+Y2+pzRehar7FnL6rcytNLmXkNKOzshDtEd5SKy3hsTXBlT5gtfhOzGPDJfxmzfNzb6GAOF0wCS6hvR7oQWPlVShadjlTn4BUaBXePmo99pdNWxxRxUZcPKhDatJwuQavhw7EfLAw== 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=OvZbc4/TlXjmGWDlSla0zeOSZpNlpV1NuIZjS2q9lyc=; b=l7zx2JIXtKRCg9c/LAxyIC1INTdeH7LOY0epBRLS383Z+fcU3cRfNOcq7KHDg0zM+WzjtVAaFX0P0hduTuw8dTfid2PCk8vg6KybXeBig8I3WmneBiBxyzkRuU//3qaXKbdps+XL8nMqOS1fkFrJP8xghr0AlLGXbAax44hF9kZsYDG61Imqu5kPzDugJHJuvBC7CGVku+PzXyrdqdEW1Fay7Ee4fWLqoGJnw3N56KYDguhe8R1P4FO2wFi9WtCXc9Xut+xnIFvV0J25P5rrSEERPWogwx11jfqErp/Wax0sHcg9AhlzBn6oW/4dDIQMtMlec/f3Aqd1BFJEZKaqHw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oss.nxp.com; dmarc=pass action=none header.from=oss.nxp.com; dkim=pass header.d=oss.nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=NXP1.onmicrosoft.com; s=selector1-NXP1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=OvZbc4/TlXjmGWDlSla0zeOSZpNlpV1NuIZjS2q9lyc=; b=qA7YGyx8+4ieQ7nBqLknsEGAeoEjo0Wmf8TkN2dIoO2kgpvCc5utWOeHYw4dJ8xltBIilMoMejWh0jzZX8geBkSmpCD68+7GtyPL8o+wC3rb8TqPaCN7njCvPC5JxvDoKO4GmzcqSTwoGk2L88bsPioGdhgBGzlnQeJkSqE2+COWpolf8fjiK/kIHubXKg2Hr7/jBEyxhrw7qdnpGRolIohP/XyD1KHjoi5vofrH2eVHpVYu0MOt8gNS9jgCQNVSxxzP53GmgPAbv0Ce/g0Ps7U41n9GYwrthNqOI/7xvKoLp2c2K3kojBD/b4Ga92WJP+o5lyVBc2Co6C35MDVZRA== Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=oss.nxp.com; Received: from AM8PR04MB7874.eurprd04.prod.outlook.com (2603:10a6:20b:24d::9) by FRZPR04MB12432.eurprd04.prod.outlook.com (2603:10a6:d10:1ca::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.472.20; Tue, 6 Oct 2026 12:15:09 +0000 Received: from AM8PR04MB7874.eurprd04.prod.outlook.com ([fe80::ac38:1699:6f18:c5d9]) by AM8PR04MB7874.eurprd04.prod.outlook.com ([fe80::ac38:1699:6f18:c5d9%6]) with mapi id 15.21.0451.022; Tue, 6 Oct 2026 12:15:09 +0000 From: "Peng Fan (OSS)" Date: Tue, 06 Oct 2026 20:19:09 +0800 Subject: [PATCH RFC v5 3/6] iommu/arm-smmu-v3: Delay stream allocation to inside the mutex Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261006-smmu-shared-sid-v5-3-169a59c671d3@nxp.com> References: <20261006-smmu-shared-sid-v5-0-169a59c671d3@nxp.com> In-Reply-To: <20261006-smmu-shared-sid-v5-0-169a59c671d3@nxp.com> To: Will Deacon , Robin Murphy , "Joerg Roedel (AMD)" , Jean-Philippe Brucker , Nicolin Chen , Jason Gunthorpe , Thierry Reding , Krishna Reddy , Jonathan Hunter , Jason Gunthorpe Cc: linux-arm-kernel@lists.infradead.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-tegra@vger.kernel.org, Peng Fan X-Mailer: b4 0.16-dev-7bc12 X-ClientProxiedBy: MA5PR01CA0206.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a01:1b0::10) To AM8PR04MB7874.eurprd04.prod.outlook.com (2603:10a6:20b:24d::9) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM8PR04MB7874:EE_|FRZPR04MB12432:EE_ X-MS-Office365-Filtering-Correlation-Id: 5014daa5-0d2d-4544-0203-08df23a376e5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|366016|23010399003|19092799006|1800799024|10067099003|56012099006|11063799006|18002099003|22082099003|921020; X-Microsoft-Antispam-Message-Info: ZlGGuXOaoL5RgjQTqL5dZYxefGmfoMnaNAsQxIy0qd+h/HCjsnZfPbZUziYr/M6lfrcTCNpvbKqgQvCk8bvtlkuO6RVLu9uXueEtiU2SjSxmrjAF+5+ROrV4sCzkNjYeD91Qvhj+DfpMznJZgHrfuVbxUm6vDMTS6YBN81CvIQ2ezmtI8Qr634j5fZ7EgJqyXnz7moLeMQi1BDYPbMjitqWd+VI/v9oz5nzAJXsfUJDzerDle/45bHtfBp5xA714s61lPdGiKEOM3TDbjuQP8nwcJAKqkzBjnEE1zw1xBJZhca5W/hsADhX3Y2ndCfhdEkpeED0GBBnAgE0yb0g6plKZuMfDiQ3UUcp3ZwEtPc7492nSpNzF+QeLDJRK2NfocYmOfirHaFggPR4vOM5oxzhmY9+vxFqJJkCdiLX8eXSR0SqH07aupx/asiPBcxW7NJUL8PE3dOD7D3LD3DM+UYj9afiyZWBgyeV40/X4r7tb/EAA3Pk3ZgQVsAN2mfVpJWcYQGJDzJqaGULvXak+Y5oAFbOc4SZOYtkmU026Yeq2rfsV59+jDGOT+tAZ0lf6KgGxpTS9xWQlx3LHx1AfJNhEmTHtKZM2DdgQDGNROdEVnpRzHDJS6n1nQBRohp+NOq17xUyNicHjuin7SwC4DgVBQJbva4sGuSthm8+ya99IxB/msR+OsYlk2T9skF2dp8U8x7CnglX1u8V2/TK+yg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:AM8PR04MB7874.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(7416014)(376014)(366016)(23010399003)(19092799006)(1800799024)(10067099003)(56012099006)(11063799006)(18002099003)(22082099003)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?YTg3Y1VTbGlGVkRicGVsMXBPUis2S1AvcGdkWTBpL3Z2YlZKVkxGeW5YQkc4?= =?utf-8?B?eFFsK29YNGV6YzFtMEx2Z015OHpoYkJHQVYwaFZZTlAxL0dYUjR4NmMzR0hv?= =?utf-8?B?UDZZTk1WVjJ6dlcxTkxXSWwyZWJHQnMvazk0bE5KUW1WT1krUkdBR3J4azU0?= =?utf-8?B?YmxlbVZ4ampUeGpUajd5RmNYZ2lzNzIvc3huanVFK092QUJGaS8xZnA1ZDk5?= =?utf-8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QVc1SEtHQ2MySVRZ?= =?utf-8?B?WFVVM0dvSldUMXJXYTJpNzgxU0FZWm5nR2RCbDBCc2lsbGtZQ2U0Tm9uWFlJ?= =?utf-8?B?N2lSNGhpcGpmb3lDZnVhUkc5NjVid3hiM1l1ellUVWt5QVlkWGF6M3diVStR?= =?utf-8?B?YXdJZGtZNW5PVHVnQVcyL3NLbnVENHlDSThCYnBiZVdYRm4rRHhPUk9Mc1d3?= =?utf-8?B?WlV2OFBnTXFFRFNzZ0NpcU8vWGhRWUNmQWJwcnROQ2U5SUgwR0RCcjRZcW1T?= =?utf-8?B?U1JPcDk0NDRWMkhaNGxhanhkOXlqZm9yRFlaNEg0Wkt5SHB1Mk16ak5KZGVp?= =?utf-8?Q?1ildFUgioXno4y1FGmM6eLCtyFsIr3vDPCE7E?= X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5014daa5-0d2d-4544-0203-08df23a376e5 X-MS-Exchange-CrossTenant-AuthSource: AM8PR04MB7874.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Oct 2026 12:15:09.2809 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Q0QqaboRpu7IgUp0q6cSZ9SpzuyCBPETtnEDr7+UnC/DMTl3eMO/rA0gSXpKs8WBUcy5F4ALl+tDF7CD2zX3VhRqJToh7eWRhVGqZHSyzpTD6HZHcV6GFlbxXXAYAj2E X-MS-Exchange-Transport-CrossTenantHeadersStamped: FRZPR04MB12432 From: Peng Fan Move arm_smmu_stream allocation from upfront (before the mutex) into the mutex-protected loop in arm_smmu_insert_master(). Instead of pre-allocating all stream objects and then inserting them into the RB tree, first look up whether the SID already exists in the tree. Only allocate and insert a new stream when no existing entry is found, then avoid unnecessary allocations when bridged PCI devices produce duplicated IDs. Prepare the code for a subsequent patch that will reuse existing streams when stream IDs are shared across masters. The sort is also moved after the mutex section, since streams are now populated inside the loop rather than beforehand. Assisted-by: LLM Signed-off-by: Peng Fan --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 102 +++++++++++++++------------- 1 file changed, 53 insertions(+), 49 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 9d34eac196a65..69c2c3596b06a 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -4110,52 +4110,29 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu, return -ENOMEM; } - for (i = 0; i < fwspec->num_ids; i++) { - struct arm_smmu_stream *new_stream; - - new_stream = kzalloc_obj(*new_stream, GFP_KERNEL); - if (!new_stream) { - ret = -ENOMEM; - goto out_free_streams; - } - new_stream->id = fwspec->ids[i]; - new_stream->master = master; - master->streams[i] = new_stream; - } - - /* Put the ids into order for sorted to_merge/to_unref arrays */ - sort(master->streams, master->num_streams, - sizeof(master->streams[0]), arm_smmu_stream_id_cmp, - NULL); - - /* - * Clear after sorting: RB_CLEAR_NODE() records the node's own address, - * which sort_nonatomic() invalidates by relocating the entries. - */ - for (i = 0; i < fwspec->num_ids; i++) - RB_CLEAR_NODE(&master->streams[i]->node); - mutex_lock(&smmu->streams_mutex); for (i = 0; i < fwspec->num_ids; i++) { - struct arm_smmu_stream *new_stream = master->streams[i]; + struct arm_smmu_stream *stream; struct rb_node *existing; - u32 sid = new_stream->id; + u32 sid = fwspec->ids[i]; ret = arm_smmu_init_sid_strtab(smmu, sid); if (ret) break; - /* Insert into SID tree */ - existing = rb_find_add(&new_stream->node, &smmu->streams, - arm_smmu_streams_cmp_node); + existing = rb_find(&sid, &smmu->streams, + arm_smmu_streams_cmp_key); if (existing) { struct arm_smmu_master *existing_master = rb_entry(existing, struct arm_smmu_stream, node) ->master; /* Bridged PCI devices may end up with duplicated IDs */ - if (existing_master == master) + if (existing_master == master) { + master->streams[i] = rb_entry(existing, + struct arm_smmu_stream, node); continue; + } dev_warn(master->dev, "Aliasing StreamID 0x%x (from %s) unsupported, expect DMA to be broken\n", @@ -4163,45 +4140,72 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu, ret = -ENODEV; break; } + + stream = kzalloc_obj(*stream, GFP_KERNEL); + if (!stream) { + ret = -ENOMEM; + break; + } + stream->id = sid; + stream->master = master; + + rb_find_add(&stream->node, &smmu->streams, + arm_smmu_streams_cmp_node); + master->streams[i] = stream; } if (ret) { - for (i--; i >= 0; i--) - if (!RB_EMPTY_NODE(&master->streams[i]->node)) - rb_erase(&master->streams[i]->node, - &smmu->streams); + for (i--; i >= 0; i--) { + int j; + + if (!master->streams[i]) + continue; + /* Skip duplicated SID pointers already freed */ + for (j = 0; j < i; j++) + if (master->streams[j] == master->streams[i]) + break; + if (j < i) + continue; + rb_erase(&master->streams[i]->node, &smmu->streams); + kfree(master->streams[i]); + } mutex_unlock(&smmu->streams_mutex); - goto out_free_streams; + kfree(master->streams); + kfree(master->build_invs); + return ret; } mutex_unlock(&smmu->streams_mutex); - return 0; + /* Put the ids into order for sorted to_merge/to_unref arrays */ + sort(master->streams, master->num_streams, + sizeof(master->streams[0]), arm_smmu_stream_id_cmp, + NULL); -out_free_streams: - for (i = 0; i < master->num_streams; i++) - kfree(master->streams[i]); - kfree(master->streams); - kfree(master->build_invs); - return ret; + return 0; } static void arm_smmu_remove_master(struct arm_smmu_master *master) { int i; struct arm_smmu_device *smmu = master->smmu; - struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev); if (!smmu || !master->streams) return; mutex_lock(&smmu->streams_mutex); - for (i = 0; i < fwspec->num_ids; i++) - if (!RB_EMPTY_NODE(&master->streams[i]->node)) - rb_erase(&master->streams[i]->node, &smmu->streams); - mutex_unlock(&smmu->streams_mutex); + for (i = 0; i < master->num_streams; i++) { + int j; - for (i = 0; i < master->num_streams; i++) + /* Skip duplicated SID pointers already freed */ + for (j = 0; j < i; j++) + if (master->streams[j] == master->streams[i]) + break; + if (j < i) + continue; + rb_erase(&master->streams[i]->node, &smmu->streams); kfree(master->streams[i]); + } + mutex_unlock(&smmu->streams_mutex); kfree(master->streams); kfree(master->build_invs); -- 2.34.1