Skip to content

Commit 59c68ac

Browse files
Steve Wisedledford
authored andcommitted
iw_cm: free cm_id resources on the last deref
Remove the complicated logic to free the iw_cm_id inside iw_cm event handlers vs when an application thread destroys the cm_id. Also remove the block in iw_destroy_cm_id() to block the application until all references are removed. This block can cause a deadlock when disconnecting or destroying cm_ids inside an rdma_cm event handler. Simply allowing the last deref of the iw_cm_id to free the memory is cleaner and avoids this potential deadlock. Also a flag is added, IW_CM_DROP_EVENTS, that is set when the cm_id is marked for destruction. If any events are pending on this iw_cm_id, then as they are processed they will be dropped vs posted upstream if IW_CM_DROP_EVENTS is set. Signed-off-by: Steve Wise <[email protected]> Signed-off-by: Doug Ledford <[email protected]>
1 parent ad61a4c commit 59c68ac

File tree

2 files changed

+18
-38
lines changed

2 files changed

+18
-38
lines changed

drivers/infiniband/core/iwcm.c

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,14 @@ static void free_cm_id(struct iwcm_id_private *cm_id_priv)
183183

184184
/*
185185
* Release a reference on cm_id. If the last reference is being
186-
* released, enable the waiting thread (in iw_destroy_cm_id) to
187-
* get woken up, and return 1 if a thread is already waiting.
186+
* released, free the cm_id and return 1.
188187
*/
189188
static int iwcm_deref_id(struct iwcm_id_private *cm_id_priv)
190189
{
191190
BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
192191
if (atomic_dec_and_test(&cm_id_priv->refcount)) {
193192
BUG_ON(!list_empty(&cm_id_priv->work_list));
194-
complete(&cm_id_priv->destroy_comp);
193+
free_cm_id(cm_id_priv);
195194
return 1;
196195
}
197196

@@ -208,19 +207,10 @@ static void add_ref(struct iw_cm_id *cm_id)
208207
static void rem_ref(struct iw_cm_id *cm_id)
209208
{
210209
struct iwcm_id_private *cm_id_priv;
211-
int cb_destroy;
212210

213211
cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
214212

215-
/*
216-
* Test bit before deref in case the cm_id gets freed on another
217-
* thread.
218-
*/
219-
cb_destroy = test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
220-
if (iwcm_deref_id(cm_id_priv) && cb_destroy) {
221-
BUG_ON(!list_empty(&cm_id_priv->work_list));
222-
free_cm_id(cm_id_priv);
223-
}
213+
(void)iwcm_deref_id(cm_id_priv);
224214
}
225215

226216
static int cm_event_handler(struct iw_cm_id *cm_id, struct iw_cm_event *event);
@@ -370,6 +360,12 @@ static void destroy_cm_id(struct iw_cm_id *cm_id)
370360
wait_event(cm_id_priv->connect_wait,
371361
!test_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags));
372362

363+
/*
364+
* Since we're deleting the cm_id, drop any events that
365+
* might arrive before the last dereference.
366+
*/
367+
set_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags);
368+
373369
spin_lock_irqsave(&cm_id_priv->lock, flags);
374370
switch (cm_id_priv->state) {
375371
case IW_CM_STATE_LISTEN:
@@ -433,13 +429,7 @@ void iw_destroy_cm_id(struct iw_cm_id *cm_id)
433429
struct iwcm_id_private *cm_id_priv;
434430

435431
cm_id_priv = container_of(cm_id, struct iwcm_id_private, id);
436-
BUG_ON(test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags));
437-
438432
destroy_cm_id(cm_id);
439-
440-
wait_for_completion(&cm_id_priv->destroy_comp);
441-
442-
free_cm_id(cm_id_priv);
443433
}
444434
EXPORT_SYMBOL(iw_destroy_cm_id);
445435

@@ -809,10 +799,7 @@ static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv,
809799
ret = cm_id->cm_handler(cm_id, iw_event);
810800
if (ret) {
811801
iw_cm_reject(cm_id, NULL, 0);
812-
set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
813-
destroy_cm_id(cm_id);
814-
if (atomic_read(&cm_id_priv->refcount)==0)
815-
free_cm_id(cm_id_priv);
802+
iw_destroy_cm_id(cm_id);
816803
}
817804

818805
out:
@@ -1000,7 +987,6 @@ static void cm_work_handler(struct work_struct *_work)
1000987
unsigned long flags;
1001988
int empty;
1002989
int ret = 0;
1003-
int destroy_id;
1004990

1005991
spin_lock_irqsave(&cm_id_priv->lock, flags);
1006992
empty = list_empty(&cm_id_priv->work_list);
@@ -1013,20 +999,14 @@ static void cm_work_handler(struct work_struct *_work)
1013999
put_work(work);
10141000
spin_unlock_irqrestore(&cm_id_priv->lock, flags);
10151001

1016-
ret = process_event(cm_id_priv, &levent);
1017-
if (ret) {
1018-
set_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
1019-
destroy_cm_id(&cm_id_priv->id);
1020-
}
1021-
BUG_ON(atomic_read(&cm_id_priv->refcount)==0);
1022-
destroy_id = test_bit(IWCM_F_CALLBACK_DESTROY, &cm_id_priv->flags);
1023-
if (iwcm_deref_id(cm_id_priv)) {
1024-
if (destroy_id) {
1025-
BUG_ON(!list_empty(&cm_id_priv->work_list));
1026-
free_cm_id(cm_id_priv);
1027-
}
1002+
if (!test_bit(IWCM_F_DROP_EVENTS, &cm_id_priv->flags)) {
1003+
ret = process_event(cm_id_priv, &levent);
1004+
if (ret)
1005+
destroy_cm_id(&cm_id_priv->id);
1006+
} else
1007+
pr_debug("dropping event %d\n", levent.event);
1008+
if (iwcm_deref_id(cm_id_priv))
10281009
return;
1029-
}
10301010
if (empty)
10311011
return;
10321012
spin_lock_irqsave(&cm_id_priv->lock, flags);

drivers/infiniband/core/iwcm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct iwcm_id_private {
5656
struct list_head work_free_list;
5757
};
5858

59-
#define IWCM_F_CALLBACK_DESTROY 1
59+
#define IWCM_F_DROP_EVENTS 1
6060
#define IWCM_F_CONNECT_WAIT 2
6161

6262
#endif /* IWCM_H */

0 commit comments

Comments
 (0)