From patchwork Tue Aug 6 23:31:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13755489 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 5B92EC52D6F for ; Tue, 6 Aug 2024 23:36:55 +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=CfyBdVA/x16kGwnKRbwfMRQ5CJ0bzDTpS4paTmqFNMc=; b=ZEisgvEQNyyqCqfZEMFHb1Lp1p W8BmWcb3eL75RcsYTtsd2OFnsNy/xMf7OSJj2+wApTw2+ei5kOevnPWmNmoPJepYNMvns5HTao+by IpQFs/4VXRb9ZXKvRZKIczOMxdtl925YhjTLf6DIGTVmzXWkqjY0LkeGaK6qc9QgdsWijVgBSjxdO Hb8jRnviYiJbRAoFh0HXDbatGQYAnfWlHE5KPBlc4h3U7xWnBgLrPzFm1JjQDQlqQMfCuNnDaNY79 6kwxhlxgZDBMmDUfvS117G8FXvz36ovEciLL0kCY3AH+iAWbU4OzChRo/mhVYLZFQ5Izbl/fVwqiR ng6MIyIg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbTjQ-00000003Kxx-1L48; Tue, 06 Aug 2024 23:36:44 +0000 Received: from mail-dm3nam02on20601.outbound.protection.outlook.com ([2a01:111:f403:2405::601] helo=NAM02-DM3-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbTeh-00000003Jil-03x0 for linux-arm-kernel@lists.infradead.org; Tue, 06 Aug 2024 23:31:52 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=lAv2AsHA1NiHKtFYW70xkPWbbVc0KgJN289DERCPioFFhF6OfQnIbulBmSQd/n6Rgvq1sRShdyFnRrAnnP9CEadpPRbTethWNhi8jsHA/P2iZiYZ4/v7TrZn9mvxRejmE65MkYzV8QjQYQOvUosmO0/laERvwIFZz6Tt/F5iraNVL2vm/YpidD4rQZZd3DR3+sLEitLMWxzgjHknkWMotvS1IdK+9xLWsw4qkcYV/74vP1TKXgGyho43yDLq92aonV0+1feJy2Ay5TB2em+bFqqIq/73evGZdzSQIit2Fv7NctmqZhFcqp4XJZrXCU+0tUB3Lr9w/Bu2P8NxEZSHIg== 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=CfyBdVA/x16kGwnKRbwfMRQ5CJ0bzDTpS4paTmqFNMc=; b=kxKYlvvofUgp/dyWOXYlAxIvoaLoAP3tu0PIicQA0BDwvdKT6eKrVoMVa2M6hFRwrLDWVp2NrLtDsTVhHOUhAKpQXBWCHfgVLsX5xgl7gnWflFoG81xtkUKkqxzootRWE+NBYhbDYfA1C+pXAYOQn7i1OvjqqHUzw7ArnrldOswgV8vX6Yj/Sk5Gr8lu4pk8eym7kdIe25xebpJCFDHEIfV3BcgJnukfgTaehw2+S1UoEIm/68X+wFADBMOl+MdshIajoPzIR8hNS0yGZChEgNph5RwZRxqcpGBNAVm8utwpKd80Zv8QODeowj1WQQIj2ShT81VO/ykxZnhxK75nsQ== 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=CfyBdVA/x16kGwnKRbwfMRQ5CJ0bzDTpS4paTmqFNMc=; b=m+pAYo2StUWh/voimbRoFdZMl7grkZ5de2PHSr2JfjqU92mf+lwsJSBEeX9wWJlF9XP+jPJmFT+xG83HNbcTqvq1qBY/Dqxrnu5ArgzZZXYDGDjX/tLjMvPakUbV1OtO0WTSisxDe0hj3j5oYZLcdbpCUbopjV6ADNghkMnVKEeFjH6R5p1teBLz3ELyg4bwkRdCUX+ab+P6GA71+b60coD+ivwUbht8/QRdse4BPMcNVUXWUzzBOssWQt+GgZOhY3BzQM5s8hY1CgJKUPRJ372lbV6iKHFKhsMMpf0Jw5C4+JAE029F3cEOmSt529PkeBufX90q/IH7tAuOzxEh+Q== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH3PR12MB7763.namprd12.prod.outlook.com (2603:10b6:610:145::10) by PH0PR12MB7839.namprd12.prod.outlook.com (2603:10b6:510:286::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7828.23; Tue, 6 Aug 2024 23:31:32 +0000 Received: from CH3PR12MB7763.namprd12.prod.outlook.com ([fe80::8b63:dd80:c182:4ce8]) by CH3PR12MB7763.namprd12.prod.outlook.com ([fe80::8b63:dd80:c182:4ce8%3]) with mapi id 15.20.7849.008; Tue, 6 Aug 2024 23:31:32 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Ryan Roberts , Mostafa Saleh Subject: [PATCH v3 1/9] iommu/arm-smmu-v3: Use the new rb tree helpers Date: Tue, 6 Aug 2024 20:31:15 -0300 Message-ID: <1-v3-9fef8cdc2ff6+150d1-smmuv3_tidy_jgg@nvidia.com> In-Reply-To: <0-v3-9fef8cdc2ff6+150d1-smmuv3_tidy_jgg@nvidia.com> References: X-ClientProxiedBy: BN9PR03CA0089.namprd03.prod.outlook.com (2603:10b6:408:fc::34) To CH3PR12MB7763.namprd12.prod.outlook.com (2603:10b6:610:145::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR12MB7763:EE_|PH0PR12MB7839:EE_ X-MS-Office365-Filtering-Correlation-Id: 94bf0f7b-5870-4a0d-e50a-08dcb66fe49b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|1800799024; X-Microsoft-Antispam-Message-Info: rff6NF0ATZ//aEyoITPVxfCq6QqAMKMW6xzH849PtqTA1gx6SyxqrOyUm7TRMPp0tsBWh1kHnlpVhWdp9YQM7JNu4EFmbKvnYrGuqsa255m59ZtjM0V/7tmv3ydQZsjRWquy49eGB1GslaOI+UawLSPvmTDP4WkPu85HPCQbn7mdjfmGc+g3T43WAgnM0OEKwNmEDfRi22Ue2uQQu9LjtTKrMevupFoxHGD4n8RZp/qAjbFrOUFIUe3D2LUyyQ5ZFVF9rs6Y2kNjtBcI5GZzJWReOmI27ANQF44D37lo/dZ/JKwR3Bokd/JHylmPEFwpXOfGs8yMtcyyUg/FcCFfes1pbhuOTXTpgd4CNzRuTci4r+P6NH9QT/24y9Lo+N+9eBoSrdzsiYvIpIhyQ9UgdAhDnISRfOfPP/pcs2C+/zQKwxrpG5hdyVO5hOR2qhatTxuIqC/mHZ3R157KcGLykkK8XXyeey0HdD2JngdWskzPP556K9MuIAB4zr+I46iWQU98kWw+bPuNQBA0+edH4YvenzL72Cj77lr2yssI3Q5r1AR+SRpN6j5e8W3wjGuJEJ8LGkRMNQ+epiFYo8klf8YZAe0jQKkQGAqhqrZwftEnpxpRqzmcNmxH3soOA4t2H444nNxX35/01iRzncIu4PyU7MJU38Ps/CD9Uk7laCt7vwA7/MAWqN3Wl+FUQw4p/gbkUzgDqr2TeTXOAwZLhihVfA/ynjAappwHuvFHF8MTQf9pOL4TJb27iFoMEyDMQcEOn9Txvdg8z5yWmRKE5UY1oABeeg5biioEf69Ar/SUWbrMJxAAAN6IfIOC9rp+QWK0m6wml9WJ1GeVUGOpEBbhQ8yrkcfatgW+IGnGbCapud1lTXVWlkxeZqsO6lUYaxfnktB/sRm7xGrPrc1hdG/xWOZ9aPSs6YXhEWARrTGEqD7nWdiL0hphz6Rp6pZX4E+XB0Grombn2D/rxAw7YgNUNN2CTXwjm0xHNyM+kYW1whoZCYfghlXr6u/ZGCtWL0D1liEhiyvZ2r0L9pEajI1F2WsWv2+hl06qISE9cg3gEzoJlG4lZcPlVj/1YejD9N99xhNLlYNG+Gpn3HARYzhDWae7dUH904j4Mmrh/1T6YDp7ZY5z01VPzvF5xK2wExJuKPPfVPH81clGt5WCpK3ktQQbFQbUmX47S+8OQymt94ForTW2/g5cc9WOBsEr2L/Gp61JT0xlDRZd8WCDIkdyn91TbXOpwYfbZSr15+vnmOwZ7G1x2ixEeAz5xnTxbrOWJG6Sh/Zeadn/CLenQILdP0OY4G6ci+U13ZDx3DEOa2NhVz91ckDUNhGryrriBnzbd15TOX8qRN3KhL/AzA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH3PR12MB7763.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(376014)(1800799024);DIR:OUT;SFP:1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TgHzSPfLGs9nhtMczQ/52l3IBkP7K4ygtwuOuvqlYystbwrQhhwEU3TSW+UQo17HSqNqUNh9lGPgXGgd7xWj6LxAgS486IuNLtZPWQBXDaqlPuNfBt4QWP7Bg9eJr7u2tdTdV8WOVf1RBzZg093lyEhLNja/ABWfvEeZeNsURvs9pMGsVMqiNbUQSaygQ58o0RPr/iUeRYBOqBM41uH1oTSV+F2lKGnhNeB9wvsZ8QIfTWjGvdJ9Yoo2++ZCAZa0Hith8TTn5XOdHuUPyNmN7InGPXl1nVShjuw6YR9bYfBeygC/4pKLQhZEjcphhh6QVHInjzy10YkxguD7WDvbPwkWUDup7ISabZlYNvF1jCusTSTn79RMlcOw9qvgCKyeS+8Ex8oY104ykIXLdl4nGTXeclO9tbuIReXpBhae9m+Qbd+71GH6Zxd/hch3JY+G74so4IHNxL129ikr/omk0iQMdl2uTMNFPqtlO7t1CMzsqBs0M8ZVzJqkBfClZluiAw3NTKQV4Xj2E70doMA2g9v48pY5+n1yyMTlp4m1ZaKP57SaNkXk98cWk= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 94bf0f7b-5870-4a0d-e50a-08dcb66fe49b X-MS-Exchange-CrossTenant-AuthSource: CH3PR12MB7763.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Aug 2024 23:31:27.3678 (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: VpXrTgTz5jadElhcvFdxY/VfL7yE1lU/gMIOUgmduBMMDMWdBp2OJjuHizPHpodQ X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7839 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240806_163151_086854_2E1A83AC X-CRM114-Status: GOOD ( 14.89 ) 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 Since v5.12 the rbtree has gained some simplifying helpers aimed at making rb tree users write less convoluted boiler plate code. Instead the caller provides a single comparison function and the helpers generate the prior open-coded stuff. Update smmu->streams to use rb_find_add() and rb_find(). Tested-by: Nicolin Chen Reviewed-by: Mostafa Saleh Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 68 ++++++++++----------- 1 file changed, 31 insertions(+), 37 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 a31460f9f3d421..b80f3359a8d12b 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1696,26 +1696,37 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid) return 0; } +static int arm_smmu_streams_cmp_key(const void *lhs, const struct rb_node *rhs) +{ + struct arm_smmu_stream *stream_rhs = + rb_entry(rhs, struct arm_smmu_stream, node); + const u32 *sid_lhs = lhs; + + if (*sid_lhs < stream_rhs->id) + return -1; + if (*sid_lhs > stream_rhs->id) + return 1; + return 0; +} + +static int arm_smmu_streams_cmp_node(struct rb_node *lhs, + const struct rb_node *rhs) +{ + return arm_smmu_streams_cmp_key( + &rb_entry(lhs, struct arm_smmu_stream, node)->id, rhs); +} + static struct arm_smmu_master * arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid) { struct rb_node *node; - struct arm_smmu_stream *stream; lockdep_assert_held(&smmu->streams_mutex); - node = smmu->streams.rb_node; - while (node) { - stream = rb_entry(node, struct arm_smmu_stream, node); - if (stream->id < sid) - node = node->rb_right; - else if (stream->id > sid) - node = node->rb_left; - else - return stream->master; - } - - return NULL; + node = rb_find(&sid, &smmu->streams, arm_smmu_streams_cmp_key); + if (!node) + return NULL; + return rb_entry(node, struct arm_smmu_stream, node)->master; } /* IRQ and event handlers */ @@ -3173,8 +3184,6 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu, { int i; int ret = 0; - struct arm_smmu_stream *new_stream, *cur_stream; - struct rb_node **new_node, *parent_node = NULL; struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev); master->streams = kcalloc(fwspec->num_ids, sizeof(*master->streams), @@ -3185,9 +3194,9 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu, mutex_lock(&smmu->streams_mutex); for (i = 0; i < fwspec->num_ids; i++) { + struct arm_smmu_stream *new_stream = &master->streams[i]; u32 sid = fwspec->ids[i]; - new_stream = &master->streams[i]; new_stream->id = sid; new_stream->master = master; @@ -3196,28 +3205,13 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu, break; /* Insert into SID tree */ - new_node = &(smmu->streams.rb_node); - while (*new_node) { - cur_stream = rb_entry(*new_node, struct arm_smmu_stream, - node); - parent_node = *new_node; - if (cur_stream->id > new_stream->id) { - new_node = &((*new_node)->rb_left); - } else if (cur_stream->id < new_stream->id) { - new_node = &((*new_node)->rb_right); - } else { - dev_warn(master->dev, - "stream %u already in tree\n", - cur_stream->id); - ret = -EINVAL; - break; - } - } - if (ret) + if (rb_find_add(&new_stream->node, &smmu->streams, + arm_smmu_streams_cmp_node)) { + dev_warn(master->dev, "stream %u already in tree\n", + sid); + ret = -EINVAL; break; - - rb_link_node(&new_stream->node, parent_node, new_node); - rb_insert_color(&new_stream->node, &smmu->streams); + } } if (ret) {