From: Zhen Lei <thunder.leizhen@huawei.com>
To: Doug Ledford <dledford@redhat.com>,
Jason Gunthorpe <jgg@ziepe.ca>,
"Leon Romanovsky" <leon@kernel.org>,
linux-rdma <linux-rdma@vger.kernel.org>
Cc: Zhen Lei <thunder.leizhen@huawei.com>
Subject: [PATCH v2 2/2] RDMA/cm: Optimise rbtree searching
Date: Wed, 12 May 2021 18:05:37 +0800 [thread overview]
Message-ID: <20210512100537.6273-3-thunder.leizhen@huawei.com> (raw)
In-Reply-To: <20210512100537.6273-1-thunder.leizhen@huawei.com>
Rewrite the rbtree traversing in the normal pattern:
if (a < b)
..
else if (a > b)
..
else // a == b
..
This gives us some performance improvement. Because the search complexity
of the rbtree is log(N). That is, after an average of "log(N) - 1" search
failures, a success or failure is known only in the last round. That is,
the (a == b) only needs to be compared once, whereas our current writing
compares log(N) times on average.
Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
---
drivers/infiniband/core/cm.c | 50 +++++++++++++++++++-----------------
1 file changed, 26 insertions(+), 24 deletions(-)
diff --git a/drivers/infiniband/core/cm.c b/drivers/infiniband/core/cm.c
index 510beb25f5b8a0b..2198de857a06591 100644
--- a/drivers/infiniband/core/cm.c
+++ b/drivers/infiniband/core/cm.c
@@ -643,29 +643,14 @@ static struct cm_id_private *cm_insert_listen(struct cm_id_private *cm_id_priv,
parent = *link;
cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
service_node);
- if ((cur_cm_id_priv->id.service_mask & service_id) ==
- (service_mask & cur_cm_id_priv->id.service_id) &&
- (cm_id_priv->id.device == cur_cm_id_priv->id.device)) {
- /*
- * Sharing an ib_cm_id with different handlers is not
- * supported
- */
- if (cur_cm_id_priv->id.cm_handler != shared_handler ||
- cur_cm_id_priv->id.context ||
- WARN_ON(!cur_cm_id_priv->id.cm_handler)) {
- spin_unlock_irqrestore(&cm.lock, flags);
- return NULL;
- }
- refcount_inc(&cur_cm_id_priv->refcount);
- cur_cm_id_priv->listen_sharecount++;
- spin_unlock_irqrestore(&cm.lock, flags);
- return cur_cm_id_priv;
- }
if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
link = &(*link)->rb_left;
else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
link = &(*link)->rb_right;
+ else if ((cur_cm_id_priv->id.service_mask & service_id) ==
+ (service_mask & cur_cm_id_priv->id.service_id))
+ goto found;
else if (be64_lt(service_id, cur_cm_id_priv->id.service_id))
link = &(*link)->rb_left;
else
@@ -676,6 +661,22 @@ static struct cm_id_private *cm_insert_listen(struct cm_id_private *cm_id_priv,
rb_insert_color(&cm_id_priv->service_node, &cm.listen_service_table);
spin_unlock_irqrestore(&cm.lock, flags);
return cm_id_priv;
+
+found:
+ /*
+ * Sharing an ib_cm_id with different handlers is not
+ * supported
+ */
+ if (cur_cm_id_priv->id.cm_handler != shared_handler ||
+ cur_cm_id_priv->id.context ||
+ WARN_ON(!cur_cm_id_priv->id.cm_handler)) {
+ spin_unlock_irqrestore(&cm.lock, flags);
+ return NULL;
+ }
+ refcount_inc(&cur_cm_id_priv->refcount);
+ cur_cm_id_priv->listen_sharecount++;
+ spin_unlock_irqrestore(&cm.lock, flags);
+ return cur_cm_id_priv;
}
static struct cm_id_private *cm_find_listen(struct ib_device *device,
@@ -686,22 +687,23 @@ static struct cm_id_private *cm_find_listen(struct ib_device *device,
while (node) {
cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
- if ((cm_id_priv->id.service_mask & service_id) ==
- cm_id_priv->id.service_id &&
- (cm_id_priv->id.device == device)) {
- refcount_inc(&cm_id_priv->refcount);
- return cm_id_priv;
- }
+
if (device < cm_id_priv->id.device)
node = node->rb_left;
else if (device > cm_id_priv->id.device)
node = node->rb_right;
+ else if ((cm_id_priv->id.service_mask & service_id) == cm_id_priv->id.service_id)
+ goto found;
else if (be64_lt(service_id, cm_id_priv->id.service_id))
node = node->rb_left;
else
node = node->rb_right;
}
return NULL;
+
+found:
+ refcount_inc(&cm_id_priv->refcount);
+ return cm_id_priv;
}
static struct cm_timewait_info *
--
2.26.0.106.g9fadedd
next prev parent reply other threads:[~2021-05-12 10:05 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-12 10:05 [PATCH v2 0/2] RDMA/cm: Optimise rbtree searching Zhen Lei
2021-05-12 10:05 ` [PATCH v2 1/2] RDMA/cm: Delete two redundant condition branches Zhen Lei
2021-05-12 10:05 ` Zhen Lei [this message]
2021-05-12 12:50 ` [PATCH v2 2/2] RDMA/cm: Optimise rbtree searching Jason Gunthorpe
2021-05-12 13:12 ` Leizhen (ThunderTown)
2021-05-12 14:26 ` Jason Gunthorpe
2021-05-13 2:25 ` Leizhen (ThunderTown)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210512100537.6273-3-thunder.leizhen@huawei.com \
--to=thunder.leizhen@huawei.com \
--cc=dledford@redhat.com \
--cc=jgg@ziepe.ca \
--cc=leon@kernel.org \
--cc=linux-rdma@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox