RDMA/cm: Optimise rbtree searching
v2 cover RDMA/cm: Optimise rbtree searching
v1 --> v2:
1. On the advice of Jason Gunthorpe, add patch 2 Rewrite the rbtree traversing in the normal pattern:
if (a < b)
..
else if (a > b)
..
else // a == b
..Zhen Lei (2):
RDMA/cm: Delete two redundant condition branches RDMA/cm: Optimise rbtree searching
drivers/infiniband/core/cm.c | 54 +++++++++++++++++------------------- 1 file changed, 26 insertions(+), 28 deletions(-)
Signature · 1 line
2.26.0.106.g9fadedd
PATCH v2 1/2 RDMA/cm: Delete two redundant condition branches
The statement of the last "if (xxx)" branch is the same as the "else" branch. Delete it to simplify code.
No functional change.
--- drivers/infiniband/core/cm.c | 4 ---- 1 file changed, 4 deletions(-)
drivers/infiniband/core/cm.c +0 -4
@@ -668,8 +668,6 @@ static struct cm_id_private *cm_insert_listen(struct cm_id_private *cm_id_priv, link = &(*link)->rb_right; else if (be64_lt(service_id, cur_cm_id_priv->id.service_id)) link = &(*link)->rb_left; - else if (be64_gt(service_id, cur_cm_id_priv->id.service_id)) - link = &(*link)->rb_right; else link = &(*link)->rb_right; }
@@ -700,8 +698,6 @@ static struct cm_id_private *cm_find_listen(struct ib_device *device, node = node->rb_right; else if (be64_lt(service_id, cm_id_priv->id.service_id)) node = node->rb_left; - else if (be64_gt(service_id, cm_id_priv->id.service_id)) - node = node->rb_right; else node = node->rb_right; }
Signature · 1 line
2.26.0.106.g9fadedd
PATCH v2 2/2 RDMA/cm: Optimise rbtree searching
In reply to Zhen LeiRewrite 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.
--- drivers/infiniband/core/cm.c | 50 +++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 24 deletions(-)
drivers/infiniband/core/cm.c +26 -24
@@ -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 *
Signature · 1 line
2.26.0.106.g9fadedd
Quoted 13 lines · Zhen Lei
static struct
cm_id_private*cm_find_listen(structib_device*device,drivers/infiniband/core/cm.c +1
-6@@ -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)
drivers/infiniband/core/cm.c static struct cm_id_private *cm_find_listen(struct ib_device *device, · line 701
Quoted 10 lines
drivers/infiniband/core/cm.c +2
-0node = 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; }
This is not the pattern I showed you. Drop the first patch and rely on the implicit equality in the final else.
Jason
Quoted 19 lines · Jason Gunthorpe
Quoted 18 lines · Zhen Lei
static struct
cm_id_private*cm_find_listen(structib_device*device,drivers/infiniband/core/cm.c +3
-6@@ -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;
Quoted 8 lines
drivers/infiniband/core/cm.c static struct cm_id_private *cm_find_listen(struct ib_device *device, · line 701
Quoted 5 lines
drivers/infiniband/core/cm.c +0
-0else if (be64_lt (service_id, cm_id_priv->id.service_id)) node = node->rb_left;else node = node->rb_right; }This is not the pattern I showed you. Drop the first patch and rely on the implicit equality in the final else.
Do you mean treate the "found" process as the else branch?
But ((cm_id_priv->id.service_mask & service_id) == cm_id_priv->id.service_id) is different from (service_id == cm_id_priv->id.service_id),I'm just worried that it might change the original logic.
Quoted 5 lines
Jason
.
Quoted 31 lines · Leizhen (ThunderTown)
Quoted 19 lines · Jason Gunthorpe
Quoted 18 lines · Zhen Lei
static struct
cm_id_private*cm_find_listen(structib_device*device,drivers/infiniband/core/cm.c +3
-6@@ -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;Quoted 8 lines
drivers/infiniband/core/cm.c static struct cm_id_private *cm_find_listen(struct ib_device *device, · line 701
Quoted 5 lines
drivers/infiniband/core/cm.c +0
-0else if (be64_lt (service_id, cm_id_priv->id.service_id)) node = node->rb_left;else node = node->rb_right; }This is not the pattern I showed you. Drop the first patch and rely on the implicit equality in the final else.
Quoted 6 lines
Do you mean treate the "found" process as the else branch?
But ((
cm_id_priv->id.service_mask&service_id) ==cm_id_priv->id.service_id) is different from (service_id== cm_id_priv->id.service_id),I'm just worried that it might change the original logic.
The service_mask is always ~cpu_to_be64(0), it is some non-working dead code that has been left in here.
If you really want to touch this then you should have a prep patch to remove that entire API facet, then the above will make sense.
Jason
Quoted 39 lines · Jason Gunthorpe
Quoted 31 lines · Leizhen (ThunderTown)
Quoted 19 lines · Jason Gunthorpe
Quoted 18 lines · Zhen Lei
static struct
cm_id_private*cm_find_listen(structib_device*device,drivers/infiniband/core/cm.c +3
-6@@ -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;Quoted 8 lines
drivers/infiniband/core/cm.c static struct cm_id_private *cm_find_listen(struct ib_device *device, · line 701
Quoted 5 lines
drivers/infiniband/core/cm.c +0
-0else if (be64_lt (service_id, cm_id_priv->id.service_id)) node = node->rb_left;else node = node->rb_right; }This is not the pattern I showed you. Drop the first patch and rely on the implicit equality in the final else.
Quoted 6 lines
Do you mean treate the "found" process as the else branch?
But ((
cm_id_priv->id.service_mask&service_id) ==cm_id_priv->id.service_id) is different from (service_id== cm_id_priv->id.service_id),I'm just worried that it might change the original logic.
Quoted 5 lines
The
service_maskis always ~cpu_to_be64(0), it is some non-working dead code that has been left in here.If you really want to touch this then you should have a prep patch to remove that entire API facet, then the above will make sense.
Okay, I'll try.
Quoted 5 lines
Jason
.