Skip to contentSkip to conversation

iWARP Connection Manager patches

PATCH Applied Bart Van Assche 3 participants 10 messages Jun 5, 2024 – Jun 9, 2024 InfiniBand

lore
Share & tools
lore
#0 lore raw

cover iWARP Connection Manager patches

Hi Jason,

This patch series includes four cleanup and one bug fix patch for the iwcm. Please consider this patch series for the next merge window.

Thanks,

Bart.

Bart Van Assche (5):
RDMA/iwcm: Use list_first_entry() where appropriate RDMA/iwcm: Change the return type of iwcm_deref_id() RDMA/iwcm: Simplify cm_event_handler()
RDMA/iwcm: Simplify cm_work_handler()
RDMA/iwcm: Fix a use-after-free related to destroying CM IDs

 drivers/infiniband/core/iwcm.c | 41 +++++++++++++++-------------------
 1 file changed, 18 insertions(+), 23 deletions(-)
#1 lore raw

PATCH 1/5 RDMA/iwcm: Use list_first_entry() where appropriate

Improve source code readability by using list_first_entry() where appropriate.

---
 drivers/infiniband/core/iwcm.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/infiniband/core/iwcm.c +4 -4
index 0301fcad4b48..90d8f3d66990 100644
@@ -143,8 +143,8 @@ static struct iwcm_work *get_work(struct iwcm_id_private *cm_id_priv)
 
 	if (list_empty(&cm_id_priv->work_free_list))
 		return NULL;
-	work = list_entry(cm_id_priv->work_free_list.next, struct iwcm_work,
-			  free_list);
+	work = list_first_entry(&cm_id_priv->work_free_list, struct iwcm_work,
+				free_list);
 	list_del_init(&work->free_list);
 	return work;
 }
@@ -1023,8 +1023,8 @@ static void cm_work_handler(struct work_struct *_work)
 	spin_lock_irqsave(&cm_id_priv->lock, flags);
 	empty = list_empty(&cm_id_priv->work_list);
 	while (!empty) {
-		work = list_entry(cm_id_priv->work_list.next,
-				  struct iwcm_work, list);
+		work = list_first_entry(&cm_id_priv->work_list,
+					struct iwcm_work, list);
 		list_del_init(&work->list);
 		empty = list_empty(&cm_id_priv->work_list);
 		levent = work->event;
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] RDMA/iwcm: Use list_first_entry() where appropriate

Quoted 12 lines · Bart Van Assche

Improve source code readability by using list_first_entry() where appropriate.

---
  drivers/infiniband/core/iwcm.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/infiniband/core/iwcm.c +0 -0
index 0301fcad4b48..90d8f3d66990 100644

drivers/infiniband/core/iwcm.c static struct iwcm_work *get_work(struct iwcm_id_private *cm_id_priv) · line 147

Quoted 7 lines
drivers/infiniband/core/iwcm.c +2 -2
@@ -143,8 +143,8 @@ static struct iwcm_work *get_work(struct iwcm_id_private *cm_id_priv)
 
  	if (list_empty(&cm_id_priv->work_free_list))
  		return NULL;
-	work = list_entry(cm_id_priv->work_free_list.next, struct iwcm_work,
-			  free_list);
+	work = list_first_entry(&cm_id_priv->work_free_list, struct iwcm_work,
+				free_list);

The followings are the definitions of list_entry and list_first_entry.

#define list_entry(ptr, type, member) \

    container_of(ptr, type, member)

#define list_first_entry(ptr, type, member) \
    list_entry((ptr)->next, type, member)F

From the above, IMO, this commit is fine.

Thanks.

Zhu Yanjun

Quoted 14 lines
drivers/infiniband/core/iwcm.c +2 -2
  	list_del_init(&work->free_list);
  	return work;
  }
@@ -1023,8 +1023,8 @@ static void cm_work_handler(struct work_struct *_work)
  	spin_lock_irqsave(&cm_id_priv->lock, flags);
  	empty = list_empty(&cm_id_priv->work_list);
  	while (!empty) {
-		work = list_entry(cm_id_priv->work_list.next,
-				  struct iwcm_work, list);
+		work = list_first_entry(&cm_id_priv->work_list,
+					struct iwcm_work, list);
  		list_del_init(&work->list);
  		empty = list_empty(&cm_id_priv->work_list);
  		levent = work->event;
Signature · 2 lines
Best Regards,
Yanjun.Zhu
#3 lore raw

PATCH 2/5 RDMA/iwcm: Change the return type of iwcm_deref_id()

In reply to Bart Van Assche

Since iwcm_deref_id() returns either 0 or 1, change its return type from 'int' into 'bool'.

---
 drivers/infiniband/core/iwcm.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/infiniband/core/iwcm.c +4 -4
index 90d8f3d66990..ae9c12409f8a 100644
@@ -206,17 +206,17 @@ static void free_cm_id(struct iwcm_id_private *cm_id_priv)
 
 /*
  * Release a reference on cm_id. If the last reference is being
- * released, free the cm_id and return 1.
+ * released, free the cm_id and return 'true'.
  */
-static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
+static bool iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
 {
 	if (refcount_dec_and_test(&cm_id_priv->refcount)) {
 		BUG_ON(!list_empty(&cm_id_priv->work_list));
 		free_cm_id(cm_id_priv);
-		return 1;
+		return true;
 	}
 
-	return 0;
+	return false;
 }
 
 static void add_ref(struct iw_cm_id *cm_id)
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] RDMA/iwcm: Change the return type of iwcm_deref_id()

Quoted 4 lines · Bart Van Assche

Since iwcm_deref_id() returns either 0 or 1, change its return type from 'int' into 'bool'.

I am fine with this. Thanks a lot.

Zhu Yanjun

Quoted 30 lines
---
  drivers/infiniband/core/iwcm.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/infiniband/core/iwcm.c +4 -4
index 90d8f3d66990..ae9c12409f8a 100644
@@ -206,17 +206,17 @@ static void free_cm_id(struct iwcm_id_private *cm_id_priv)
 
  /*
   * Release a reference on cm_id. If the last reference is being
- * released, free the cm_id and return 1.
+ * released, free the cm_id and return 'true'.
   */
-static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
+static bool iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
  {
  	if (refcount_dec_and_test(&cm_id_priv->refcount)) {
  		BUG_ON(!list_empty(&cm_id_priv->work_list));
  		free_cm_id(cm_id_priv);
-		return 1;
+		return true;
  	}
 
-	return 0;
+	return false;
  }
 
  static void add_ref(struct iw_cm_id *cm_id)
Signature · 2 lines
Best Regards,
Yanjun.Zhu
#5 lore raw

PATCH 3/5 RDMA/iwcm: Simplify cm_event_handler()

In reply to Bart Van Assche

queue_work() can test efficiently whether or not work is pending. Hence, simplify cm_event_handler() by always calling queue_work() instead of only if the list with pending work is empty.

---
 drivers/infiniband/core/iwcm.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/infiniband/core/iwcm.c +2 -5
index ae9c12409f8a..3d66aec36899 100644
@@ -1093,11 +1093,8 @@ static int cm_event_handler(struct iw_cm_id *cm_id,
 	}
 
 	refcount_inc(&cm_id_priv->refcount);
-	if (list_empty(&cm_id_priv->work_list)) {
-		list_add_tail(&work->list, &cm_id_priv->work_list);
-		queue_work(iwcm_wq, &work->work);
-	} else
-		list_add_tail(&work->list, &cm_id_priv->work_list);
+	list_add_tail(&work->list, &cm_id_priv->work_list);
+	queue_work(iwcm_wq, &work->work);
 out:
 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
 	return ret;
#6 lore raw

PATCH 4/5 RDMA/iwcm: Simplify cm_work_handler()

In reply to Bart Van Assche

Instead of complicating the code to avoid a spin_lock_irqsave() / spin_lock_irqrestore() pair before returning, simplify the code by removing the local variable 'empty'.

---
 drivers/infiniband/core/iwcm.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/infiniband/core/iwcm.c +1 -6
index 3d66aec36899..4424f430fc08 100644
@@ -1017,16 +1017,13 @@ static void cm_work_handler(struct work_struct *_work)
 	struct iw_cm_event levent;
 	struct iwcm_id_private *cm_id_priv = work->cm_id;
 	unsigned long flags;
-	int empty;
 	int ret = 0;
 
 	spin_lock_irqsave(&cm_id_priv->lock, flags);
-	empty = list_empty(&cm_id_priv->work_list);
-	while (!empty) {
+	while (!list_empty(&cm_id_priv->work_list)) {
 		work = list_first_entry(&cm_id_priv->work_list,
 					struct iwcm_work, list);
 		list_del_init(&work->list);
-		empty = list_empty(&cm_id_priv->work_list);
 		levent = work->event;
 		put_work(work);
 		spin_unlock_irqrestore(&cm_id_priv->lock, flags);
@@ -1039,8 +1036,6 @@ static void cm_work_handler(struct work_struct *_work)
 			pr_debug("dropping event %d\n", levent.event);
 		if (iwcm_deref_id(cm_id_priv))
 			return;
-		if (empty)
-			return;
 		spin_lock_irqsave(&cm_id_priv->lock, flags);
 	}
 	spin_unlock_irqrestore(&cm_id_priv->lock, flags);
#7 lore raw

PATCH 5/5 RDMA/iwcm: Fix a use-after-free related to destroying CM IDs

In reply to Bart Van Assche

iw_conn_req_handler() associates a new struct rdma_id_private (conn_id) with an existing struct iw_cm_id (cm_id) as follows:

        conn_id->cm_id.iw = cm_id;
        cm_id->context = conn_id;
        cm_id->cm_handler = cma_iw_handler;

rdma_destroy_id() frees both the cm_id and the struct rdma_id_private. Make sure that cm_work_handler() does not trigger a use-after-free by only freeing of the struct rdma_id_private after all pending work has finished.

---
 drivers/infiniband/core/iwcm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/infiniband/core/iwcm.c +7 -4
index 4424f430fc08..1a6339f3a63f 100644
@@ -368,8 +368,10 @@ EXPORT_SYMBOL(iw_cm_disconnect);
  *
  * Clean up all resources associated with the connection and release
  * the initial reference taken by iw_create_cm_id.
+ *
+ * Returns true if and only if the last cm_id_priv reference has been dropped.
  */
-static void destroy_cm_id(struct iw_cm_id *cm_id)
+static bool destroy_cm_id(struct iw_cm_id *cm_id)
 {
 	struct iwcm_id_private *cm_id_priv;
 	struct ib_qp *qp;
@@ -439,7 +441,7 @@ static void destroy_cm_id(struct iw_cm_id *cm_id)
 		iwpm_remove_mapping(&cm_id->local_addr, RDMA_NL_IWCM);
 	}
 
-	(void)iwcm_deref_id(cm_id_priv);
+	return iwcm_deref_id(cm_id_priv);
 }
 
 /*
@@ -450,7 +452,8 @@ static void destroy_cm_id(struct iw_cm_id *cm_id)
  */
 void iw_destroy_cm_id(struct iw_cm_id *cm_id)
 {
-	destroy_cm_id(cm_id);
+	if (!destroy_cm_id(cm_id))
+		flush_workqueue(iwcm_wq);
 }
 EXPORT_SYMBOL(iw_destroy_cm_id);
 
@@ -1031,7 +1034,7 @@ static void cm_work_handler(struct work_struct *_work)
 		if (!test_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags)) {
 			ret = process_event(cm_id_priv, &levent);
 			if (ret)
-				destroy_cm_id(&cm_id_priv->id);
+				WARN_ON_ONCE(destroy_cm_id(&cm_id_priv->id));
 		} else
 			pr_debug("dropping event %d\n", levent.event);
 		if (iwcm_deref_id(cm_id_priv))
#8 lore raw

Re: [PATCH 5/5] RDMA/iwcm: Fix a use-after-free related to destroying CM IDs

Quoted 7 lines · Bart Van Assche

iw_conn_req_handler() associates a new struct rdma_id_private (conn_id) with an existing struct iw_cm_id (cm_id) as follows:

        conn_id->cm_id.iw = cm_id;
        cm_id->context = conn_id;
        cm_id->cm_handler = cma_iw_handler;
Quoted 5 lines

rdma_destroy_id() frees both the cm_id and the struct rdma_id_private. Make sure that cm_work_handler() does not trigger a use-after-free by only freeing of the struct rdma_id_private after all pending work has finished.

Cc stable

This is not right way to mark a patch for stable. I added the following to the commit message and applied the patch:

Fixes 59c68ac31e15 ("iw_cm: free cm_id resources on the last deref")

There is no clear Fixes tag which I can use, so I used the latest significant commit that touch that area.

Thanks

Quoted 6 lines
---
 drivers/infiniband/core/iwcm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
#9 lore raw
In reply to Bart Van Assche
Quoted 8 lines · Bart Van Assche

This patch series includes four cleanup and one bug fix patch for the iwcm. Please consider this patch series for the next merge window.

Thanks,

Bart.

[...]

Applied, thanks!

[1/5] RDMA/iwcm: Use list_first_entry() where appropriate

      https://git.kernel.org/rdma/rdma/c/b1bc15f8fb5f20
[2/5] RDMA/iwcm: Change the return type of iwcm_deref_id()
      https://git.kernel.org/rdma/rdma/c/fc772e38bce563
[3/5] RDMA/iwcm: Simplify cm_event_handler()
      https://git.kernel.org/rdma/rdma/c/e1168f09b33149
[4/5] RDMA/iwcm: Simplify cm_work_handler()
      https://git.kernel.org/rdma/rdma/c/a1babdb5b61575
[5/5] RDMA/iwcm: Fix a use-after-free related to destroying CM IDs
      https://git.kernel.org/rdma/rdma/c/aee2424246f9f1

Best regards,

Signature · 1 line
Leon Romanovsky <[email protected]>