Skip to content

Commit a2bb820

Browse files
jgunthorpewilldeacon
authored andcommitted
iommu/arm-smmu-v3: Use the new rb tree helpers
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]>
1 parent 483e0bd commit a2bb820

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
@@ -1735,26 +1735,37 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
17351735
return 0;
17361736
}
17371737

1738+
static int arm_smmu_streams_cmp_key(const void *lhs, const struct rb_node *rhs)
1739+
{
1740+
struct arm_smmu_stream *stream_rhs =
1741+
rb_entry(rhs, struct arm_smmu_stream, node);
1742+
const u32 *sid_lhs = lhs;
1743+
1744+
if (*sid_lhs < stream_rhs->id)
1745+
return -1;
1746+
if (*sid_lhs > stream_rhs->id)
1747+
return 1;
1748+
return 0;
1749+
}
1750+
1751+
static int arm_smmu_streams_cmp_node(struct rb_node *lhs,
1752+
const struct rb_node *rhs)
1753+
{
1754+
return arm_smmu_streams_cmp_key(
1755+
&rb_entry(lhs, struct arm_smmu_stream, node)->id, rhs);
1756+
}
1757+
17381758
static struct arm_smmu_master *
17391759
arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid)
17401760
{
17411761
struct rb_node *node;
1742-
struct arm_smmu_stream *stream;
17431762

17441763
lockdep_assert_held(&smmu->streams_mutex);
17451764

1746-
node = smmu->streams.rb_node;
1747-
while (node) {
1748-
stream = rb_entry(node, struct arm_smmu_stream, node);
1749-
if (stream->id < sid)
1750-
node = node->rb_right;
1751-
else if (stream->id > sid)
1752-
node = node->rb_left;
1753-
else
1754-
return stream->master;
1755-
}
1756-
1757-
return NULL;
1765+
node = rb_find(&sid, &smmu->streams, arm_smmu_streams_cmp_key);
1766+
if (!node)
1767+
return NULL;
1768+
return rb_entry(node, struct arm_smmu_stream, node)->master;
17581769
}
17591770

17601771
/* IRQ and event handlers */
@@ -3210,8 +3221,6 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
32103221
{
32113222
int i;
32123223
int ret = 0;
3213-
struct arm_smmu_stream *new_stream, *cur_stream;
3214-
struct rb_node **new_node, *parent_node = NULL;
32153224
struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);
32163225

32173226
master->streams = kcalloc(fwspec->num_ids, sizeof(*master->streams),
@@ -3222,9 +3231,9 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
32223231

32233232
mutex_lock(&smmu->streams_mutex);
32243233
for (i = 0; i < fwspec->num_ids; i++) {
3234+
struct arm_smmu_stream *new_stream = &master->streams[i];
32253235
u32 sid = fwspec->ids[i];
32263236

3227-
new_stream = &master->streams[i];
32283237
new_stream->id = sid;
32293238
new_stream->master = master;
32303239

@@ -3233,28 +3242,13 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
32333242
break;
32343243

32353244
/* Insert into SID tree */
3236-
new_node = &(smmu->streams.rb_node);
3237-
while (*new_node) {
3238-
cur_stream = rb_entry(*new_node, struct arm_smmu_stream,
3239-
node);
3240-
parent_node = *new_node;
3241-
if (cur_stream->id > new_stream->id) {
3242-
new_node = &((*new_node)->rb_left);
3243-
} else if (cur_stream->id < new_stream->id) {
3244-
new_node = &((*new_node)->rb_right);
3245-
} else {
3246-
dev_warn(master->dev,
3247-
"stream %u already in tree\n",
3248-
cur_stream->id);
3249-
ret = -EINVAL;
3250-
break;
3251-
}
3252-
}
3253-
if (ret)
3245+
if (rb_find_add(&new_stream->node, &smmu->streams,
3246+
arm_smmu_streams_cmp_node)) {
3247+
dev_warn(master->dev, "stream %u already in tree\n",
3248+
sid);
3249+
ret = -EINVAL;
32543250
break;
3255-
3256-
rb_link_node(&new_stream->node, parent_node, new_node);
3257-
rb_insert_color(&new_stream->node, &smmu->streams);
3251+
}
32583252
}
32593253

32603254
if (ret) {

0 commit comments

Comments
 (0)