Skip to content

Commit 28ddd9c

Browse files
jgunthorpegregkh
authored andcommitted
iommu/arm-smmu-v3: Use the new rb tree helpers
[ Upstream commit a2bb820e862d61f9ca1499e500915f9f505a2655 ] 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 <[email protected]> Reviewed-by: Mostafa Saleh <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]> Stable-dep-of: b00d24997a11 ("iommu/arm-smmu-v3: Fix iommu_device_probe bug due to duplicated stream ids") Signed-off-by: Sasha Levin <[email protected]>
1 parent 4c4f168 commit 28ddd9c

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,26 +1443,37 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
14431443
return 0;
14441444
}
14451445

1446+
static int arm_smmu_streams_cmp_key(const void *lhs, const struct rb_node *rhs)
1447+
{
1448+
struct arm_smmu_stream *stream_rhs =
1449+
rb_entry(rhs, struct arm_smmu_stream, node);
1450+
const u32 *sid_lhs = lhs;
1451+
1452+
if (*sid_lhs < stream_rhs->id)
1453+
return -1;
1454+
if (*sid_lhs > stream_rhs->id)
1455+
return 1;
1456+
return 0;
1457+
}
1458+
1459+
static int arm_smmu_streams_cmp_node(struct rb_node *lhs,
1460+
const struct rb_node *rhs)
1461+
{
1462+
return arm_smmu_streams_cmp_key(
1463+
&rb_entry(lhs, struct arm_smmu_stream, node)->id, rhs);
1464+
}
1465+
14461466
static struct arm_smmu_master *
14471467
arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid)
14481468
{
14491469
struct rb_node *node;
1450-
struct arm_smmu_stream *stream;
14511470

14521471
lockdep_assert_held(&smmu->streams_mutex);
14531472

1454-
node = smmu->streams.rb_node;
1455-
while (node) {
1456-
stream = rb_entry(node, struct arm_smmu_stream, node);
1457-
if (stream->id < sid)
1458-
node = node->rb_right;
1459-
else if (stream->id > sid)
1460-
node = node->rb_left;
1461-
else
1462-
return stream->master;
1463-
}
1464-
1465-
return NULL;
1473+
node = rb_find(&sid, &smmu->streams, arm_smmu_streams_cmp_key);
1474+
if (!node)
1475+
return NULL;
1476+
return rb_entry(node, struct arm_smmu_stream, node)->master;
14661477
}
14671478

14681479
/* IRQ and event handlers */
@@ -2590,8 +2601,6 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
25902601
{
25912602
int i;
25922603
int ret = 0;
2593-
struct arm_smmu_stream *new_stream, *cur_stream;
2594-
struct rb_node **new_node, *parent_node = NULL;
25952604
struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);
25962605

25972606
master->streams = kcalloc(fwspec->num_ids, sizeof(*master->streams),
@@ -2602,9 +2611,9 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
26022611

26032612
mutex_lock(&smmu->streams_mutex);
26042613
for (i = 0; i < fwspec->num_ids; i++) {
2614+
struct arm_smmu_stream *new_stream = &master->streams[i];
26052615
u32 sid = fwspec->ids[i];
26062616

2607-
new_stream = &master->streams[i];
26082617
new_stream->id = sid;
26092618
new_stream->master = master;
26102619

@@ -2613,28 +2622,13 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
26132622
break;
26142623

26152624
/* Insert into SID tree */
2616-
new_node = &(smmu->streams.rb_node);
2617-
while (*new_node) {
2618-
cur_stream = rb_entry(*new_node, struct arm_smmu_stream,
2619-
node);
2620-
parent_node = *new_node;
2621-
if (cur_stream->id > new_stream->id) {
2622-
new_node = &((*new_node)->rb_left);
2623-
} else if (cur_stream->id < new_stream->id) {
2624-
new_node = &((*new_node)->rb_right);
2625-
} else {
2626-
dev_warn(master->dev,
2627-
"stream %u already in tree\n",
2628-
cur_stream->id);
2629-
ret = -EINVAL;
2630-
break;
2631-
}
2632-
}
2633-
if (ret)
2625+
if (rb_find_add(&new_stream->node, &smmu->streams,
2626+
arm_smmu_streams_cmp_node)) {
2627+
dev_warn(master->dev, "stream %u already in tree\n",
2628+
sid);
2629+
ret = -EINVAL;
26342630
break;
2635-
2636-
rb_link_node(&new_stream->node, parent_node, new_node);
2637-
rb_insert_color(&new_stream->node, &smmu->streams);
2631+
}
26382632
}
26392633

26402634
if (ret) {

0 commit comments

Comments
 (0)