Skip to content

Commit 496c37b

Browse files
jgunthorpevijay-suman
authored andcommitted
iommu/arm-smmu-v3: Use the new rb tree helpers
[ Upstream commit a2bb820 ] 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]> (cherry picked from commit 6077d3a53d99ba1b97f4f372fb4304db3438a455) Signed-off-by: Vijayendra Suman <[email protected]>
1 parent 74956cf commit 496c37b

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
@@ -1523,26 +1523,37 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
15231523
return 0;
15241524
}
15251525

1526+
static int arm_smmu_streams_cmp_key(const void *lhs, const struct rb_node *rhs)
1527+
{
1528+
struct arm_smmu_stream *stream_rhs =
1529+
rb_entry(rhs, struct arm_smmu_stream, node);
1530+
const u32 *sid_lhs = lhs;
1531+
1532+
if (*sid_lhs < stream_rhs->id)
1533+
return -1;
1534+
if (*sid_lhs > stream_rhs->id)
1535+
return 1;
1536+
return 0;
1537+
}
1538+
1539+
static int arm_smmu_streams_cmp_node(struct rb_node *lhs,
1540+
const struct rb_node *rhs)
1541+
{
1542+
return arm_smmu_streams_cmp_key(
1543+
&rb_entry(lhs, struct arm_smmu_stream, node)->id, rhs);
1544+
}
1545+
15261546
static struct arm_smmu_master *
15271547
arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid)
15281548
{
15291549
struct rb_node *node;
1530-
struct arm_smmu_stream *stream;
15311550

15321551
lockdep_assert_held(&smmu->streams_mutex);
15331552

1534-
node = smmu->streams.rb_node;
1535-
while (node) {
1536-
stream = rb_entry(node, struct arm_smmu_stream, node);
1537-
if (stream->id < sid)
1538-
node = node->rb_right;
1539-
else if (stream->id > sid)
1540-
node = node->rb_left;
1541-
else
1542-
return stream->master;
1543-
}
1544-
1545-
return NULL;
1553+
node = rb_find(&sid, &smmu->streams, arm_smmu_streams_cmp_key);
1554+
if (!node)
1555+
return NULL;
1556+
return rb_entry(node, struct arm_smmu_stream, node)->master;
15461557
}
15471558

15481559
/* IRQ and event handlers */
@@ -2653,8 +2664,6 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
26532664
{
26542665
int i;
26552666
int ret = 0;
2656-
struct arm_smmu_stream *new_stream, *cur_stream;
2657-
struct rb_node **new_node, *parent_node = NULL;
26582667
struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);
26592668

26602669
master->streams = kcalloc(fwspec->num_ids, sizeof(*master->streams),
@@ -2665,9 +2674,9 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
26652674

26662675
mutex_lock(&smmu->streams_mutex);
26672676
for (i = 0; i < fwspec->num_ids; i++) {
2677+
struct arm_smmu_stream *new_stream = &master->streams[i];
26682678
u32 sid = fwspec->ids[i];
26692679

2670-
new_stream = &master->streams[i];
26712680
new_stream->id = sid;
26722681
new_stream->master = master;
26732682

@@ -2687,28 +2696,13 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
26872696
}
26882697

26892698
/* Insert into SID tree */
2690-
new_node = &(smmu->streams.rb_node);
2691-
while (*new_node) {
2692-
cur_stream = rb_entry(*new_node, struct arm_smmu_stream,
2693-
node);
2694-
parent_node = *new_node;
2695-
if (cur_stream->id > new_stream->id) {
2696-
new_node = &((*new_node)->rb_left);
2697-
} else if (cur_stream->id < new_stream->id) {
2698-
new_node = &((*new_node)->rb_right);
2699-
} else {
2700-
dev_warn(master->dev,
2701-
"stream %u already in tree\n",
2702-
cur_stream->id);
2703-
ret = -EINVAL;
2704-
break;
2705-
}
2706-
}
2707-
if (ret)
2699+
if (rb_find_add(&new_stream->node, &smmu->streams,
2700+
arm_smmu_streams_cmp_node)) {
2701+
dev_warn(master->dev, "stream %u already in tree\n",
2702+
sid);
2703+
ret = -EINVAL;
27082704
break;
2709-
2710-
rb_link_node(&new_stream->node, parent_node, new_node);
2711-
rb_insert_color(&new_stream->node, &smmu->streams);
2705+
}
27122706
}
27132707

27142708
if (ret) {

0 commit comments

Comments
 (0)