Skip to contentSkip to conversation

RPING: Make sure CQ event thread exits before destroying the CQ.

PATCH Superseded Steve Wise 5 participants 15 messages Oct 20, 2010 – Oct 21, 2010 10 messages not in the archive InfiniBand

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Steve Wise

author
#0 lore raw

PATCH 1/2 RPING: Make sure CQ event thread exits before destroying the CQ.

It is possible for the CQ event thread to poll the CQ after it has been destroyed which can result in a seg fault on T3 interfaces. This patch cancels the thread and waits for it to exit before destroying the CQ.

---

 examples/rping.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
examples/rping.c +6 -2
index 91952e7..e603d3b 100644
@@ -800,10 +800,10 @@ static void *rping_persistent_server_thread(void *arg)
 
 	rping_test_server(cb);
 	rdma_disconnect(cb->child_cm_id);
-	rping_free_buffers(cb);
-	rping_free_qp(cb);
 	pthread_cancel(cb->cqthread);
 	pthread_join(cb->cqthread, NULL);
+	rping_free_buffers(cb);
+	rping_free_qp(cb);
 	rdma_destroy_id(cb->child_cm_id);
 	free_cb(cb);
 	return NULL;
@@ -888,6 +888,8 @@ static int rping_run_server(struct rping_cb *cb)
 
 	rping_test_server(cb);
 	rdma_disconnect(cb->child_cm_id);
+	pthread_cancel(cb->cqthread);
+	pthread_join(cb->cqthread, NULL);
 	rdma_destroy_id(cb->child_cm_id);
 err2:
 	rping_free_buffers(cb);
@@ -1055,6 +1057,8 @@ static int rping_run_client(struct rping_cb *cb)
 
 	rping_test_client(cb);
 	rdma_disconnect(cb->cm_id);
+	pthread_cancel(cb->cqthread);
+	pthread_join(cb->cqthread, NULL);
 err2:
 	rping_free_buffers(cb);
 err1:

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20101020192859.1431.68877.stgit-T4OLL4TyM9aNDNWfRnPdfg@public.gmane.org

author
#2 lore raw

PATCH 2/2 RPING: Remove printf for FLUSH completion.

---

 examples/rping.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
examples/rping.c +5 -3
index e603d3b..5eb71e2 100644
@@ -280,10 +280,12 @@ static int rping_cq_event_handler(struct rping_cb *cb)
 		ret = 0;
 
 		if (wc.status) {
-			fprintf(stderr, "cq completion failed status %d\n",
-				wc.status);
-			if (wc.status != IBV_WC_WR_FLUSH_ERR)
+			if (wc.status != IBV_WC_WR_FLUSH_ERR) {
+				fprintf(stderr, 
+					"cq completion failed status %d\n",
+					wc.status);
 				ret = -1;
+			}
 			goto error;
 		}
 

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20101020192905.1431.40267.stgit-T4OLL4TyM9aNDNWfRnPdfg@public.gmane.org

#4 lore raw

RE: [PATCH 2/2] RPING: Remove printf for FLUSH completion.

SSBhcHBsaWVkIHRoaXMsIGJ1dCBhbSB3YWl0aW5nIG9uIHRoZSBvdGhlciBwYXRjaCB1bnRpbCB0 aGUgZGlzY3Vzc2lvbiBmaW5pc2hlcyB1cC4NCg==
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CF9C39F99A89134C9CF9C4CCB68B8DDF25B801FB15-osO9UTpF0USkrb+BlOpmy7fspsVTdybXVpNB7YpNyf8@public.gmane.org

author
#6 lore raw

Re: [PATCH 2/2] RPING: Remove printf for FLUSH completion.

Quoted 1 line · Sean

I applied this, but am waiting on the other patch until the discussion finishes up.

Since pthread_cancel() seems to be iffy for user verbs, maybe I'll try another approach: Since the rping main thread issues an rdma_disconnect(), we can assume that the CQ thread will definitely get an event and then poll the FLUSHED recv wr and pthread_exit(). So the main thread only needs to do a pthread_join() after issuing the disconnect. I'll try this approach on iWARP and IB transports to make sure my assumptions are true...

Steve.

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw
In reply to a message not in the archive
Quoted 43 lines · Steve Wise

It is possible for the CQ event thread to poll the CQ after it has been destroyed which can result in a seg fault on T3 interfaces.  This patch cancels the thread and waits for it to exit before destroying the CQ.

---

 examples/rping.c |    8 ++++++--

 1 files changed, 6 insertions(+), 2 deletions(-)
examples/rping.c +4 -2
index 91952e7..e603d3b 100644
@@ -800,10 +800,10 @@ static void *rping_persistent_server_thread(void *arg)
 
        rping_test_server(cb);
        rdma_disconnect(cb->child_cm_id);
-       rping_free_buffers(cb);
-       rping_free_qp(cb);
        pthread_cancel(cb->cqthread);
        pthread_join(cb->cqthread, NULL);
+       rping_free_buffers(cb);
+       rping_free_qp(cb);
        rdma_destroy_id(cb->child_cm_id);
        free_cb(cb);
        return NULL;
@@ -888,6 +888,8 @@ static int rping_run_server(struct rping_cb *cb)
 
        rping_test_server(cb);
        rdma_disconnect(cb->child_cm_id);
+       pthread_cancel(cb->cqthread);
+       pthread_join(cb->cqthread, NULL);
        rdma_destroy_id(cb->child_cm_id);

 err2:
       rping_free_buffers(cb);

examples/rping.c +2 -0
@@ -1055,6 +1057,8 @@ static int rping_run_client(struct rping_cb *cb)
 
        rping_test_client(cb);
        rdma_disconnect(cb->cm_id);
+       pthread_cancel(cb->cqthread);
+       pthread_join(cb->cqthread, NULL);
Quoted 3 lines

 err2:
       rping_free_buffers(cb);
 err1:

Hello Steve,

Are you aware that in general it is easy to trigger a deadlock or other undesired behavior by invoking pthread_cancel() ? If a thread e.g. gets canceled after having obtained a mutex lock and before that mutex is unlocked, this will cause trouble for any other thread that tries to grab the mutex.

Bart.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): AANLkTi=uAuXT1RMEQ+vsA4FkN2828mZ2q0KPKXxpb6H0-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

author
#9 lore raw
Quoted 50 lines · Bart Van Assche
Quoted 38 lines · Steve Wise

It is possible for the CQ event thread to poll the CQ after it has been destroyed which can result in a seg fault on T3 interfaces. This patch cancels the thread and waits for it to exit before destroying the CQ.

---

  examples/rping.c |    8 ++++++--
  1 files changed, 6 insertions(+), 2 deletions(-)
examples/rping.c +4 -2
index 91952e7..e603d3b 100644
@@ -800,10 +800,10 @@ static void *rping_persistent_server_thread(void *arg)
 
        rping_test_server(cb);
        rdma_disconnect(cb->child_cm_id);
-       rping_free_buffers(cb);
-       rping_free_qp(cb);
        pthread_cancel(cb->cqthread);
        pthread_join(cb->cqthread, NULL);
+       rping_free_buffers(cb);
+       rping_free_qp(cb);
        rdma_destroy_id(cb->child_cm_id);
        free_cb(cb);
        return NULL;
@@ -888,6 +888,8 @@ static int rping_run_server(struct rping_cb *cb)
 
        rping_test_server(cb);
        rdma_disconnect(cb->child_cm_id);
+       pthread_cancel(cb->cqthread);
+       pthread_join(cb->cqthread, NULL);
        rdma_destroy_id(cb->child_cm_id);
  err2:
        rping_free_buffers(cb);

examples/rping.c static int rping_run_client(struct rping_cb *cb) · line 1064

Quoted 8 lines
examples/rping.c +2 -0
@@ -1055,6 +1057,8 @@ static int rping_run_client(struct rping_cb *cb)
 
        rping_test_client(cb);
        rdma_disconnect(cb->cm_id);
+       pthread_cancel(cb->cqthread);
+       pthread_join(cb->cqthread, NULL);
  err2:
        rping_free_buffers(cb);
  err1:

Hello Steve,

Quoted 7 lines

Are you aware that in general it is easy to trigger a deadlock or other undesired behavior by invoking pthread_cancel() ? If a thread e.g. gets canceled after having obtained a mutex lock and before that mutex is unlocked, this will cause trouble for any other thread that tries to grab the mutex.

I was under the impression that the thread would only be canceled at precise cancellation points. Like in system calls, or if the thread calls pthread_testcancel(), which is what rping does. There are no mutexes held by the thread being canceled either. I think its ok. Do you see some other issue with the rping CQ event thread that would cause these problems?

Steve.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 4CBF4D30.3050500-7bPotxP6k4+P2YhJcF5u+vpXobYPEAuW@public.gmane.org

#11 lore raw
Quoted 62 lines · Steve Wise
Quoted 55 lines · Bart Van Assche
Quoted 44 lines · Steve Wise

It is possible for the CQ event thread to poll the CQ after it has been destroyed which can result in a seg fault on T3 interfaces.  This patch cancels the thread and waits for it to exit before destroying the CQ.

---

 examples/rping.c |    8 ++++++--

 1 files changed, 6 insertions(+), 2 deletions(-)
examples/rping.c +2 -2
index 91952e7..e603d3b 100644
@@ -800,10 +800,10 @@ static void *rping_persistent_server_thread(void
 *arg)
 
        rping_test_server(cb);
        rdma_disconnect(cb->child_cm_id);
-       rping_free_buffers(cb);
-       rping_free_qp(cb);
        pthread_cancel(cb->cqthread);
        pthread_join(cb->cqthread, NULL);
+       rping_free_buffers(cb);
+       rping_free_qp(cb);
        rdma_destroy_id(cb->child_cm_id);
        free_cb(cb);

       return NULL;

examples/rping.c +2 -0
@@ -888,6 +888,8 @@ static int rping_run_server(struct rping_cb *cb)
 
        rping_test_server(cb);
        rdma_disconnect(cb->child_cm_id);
+       pthread_cancel(cb->cqthread);
+       pthread_join(cb->cqthread, NULL);
        rdma_destroy_id(cb->child_cm_id);

 err2:
       rping_free_buffers(cb);

examples/rping.c +2 -0
@@ -1055,6 +1057,8 @@ static int rping_run_client(struct rping_cb *cb)
 
        rping_test_client(cb);
        rdma_disconnect(cb->cm_id);
+       pthread_cancel(cb->cqthread);
+       pthread_join(cb->cqthread, NULL);
Quoted 4 lines

 err2:
       rping_free_buffers(cb);
 err1:

Hello Steve,

Quoted 5 lines

Are you aware that in general it is easy to trigger a deadlock or other undesired behavior by invoking pthread_cancel() ? If a thread e.g. gets canceled after having obtained a mutex lock and before that mutex is unlocked, this will cause trouble for any other thread that tries to grab the mutex.

Quoted 5 lines

I was under the impression that the thread would only be canceled at precise cancellation points.  Like in system calls, or if the thread calls pthread_testcancel(), which is what rping does.   There are no mutexes held by the thread being canceled either.  I think its ok.  Do you see some other issue with the rping CQ event thread that would cause these problems?

Please keep in mind that glibc uses locking internally for many file I/O functions, e.g. printf() and fprintf().

Bart.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): AANLkTimTpM_h7P8tacZic2aP7i=dBiAquDN5S_D881ae-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

author
#13 lore raw
Quoted 71 lines · Bart Van Assche
Quoted 62 lines · Steve Wise
Quoted 54 lines · Bart Van Assche
Quoted 40 lines · Steve Wise

It is possible for the CQ event thread to poll the CQ after it has been destroyed which can result in a seg fault on T3 interfaces. This patch cancels the thread and waits for it to exit before destroying the CQ.

---

  examples/rping.c |    8 ++++++--
  1 files changed, 6 insertions(+), 2 deletions(-)
examples/rping.c +2 -2
index 91952e7..e603d3b 100644
@@ -800,10 +800,10 @@ static void *rping_persistent_server_thread(void
 *arg)
 
        rping_test_server(cb);
        rdma_disconnect(cb->child_cm_id);
-       rping_free_buffers(cb);
-       rping_free_qp(cb);
        pthread_cancel(cb->cqthread);
        pthread_join(cb->cqthread, NULL);
+       rping_free_buffers(cb);
+       rping_free_qp(cb);
        rdma_destroy_id(cb->child_cm_id);
        free_cb(cb);
        return NULL;
examples/rping.c +2 -0
@@ -888,6 +888,8 @@ static int rping_run_server(struct rping_cb *cb)
 
        rping_test_server(cb);
        rdma_disconnect(cb->child_cm_id);
+       pthread_cancel(cb->cqthread);
+       pthread_join(cb->cqthread, NULL);
        rdma_destroy_id(cb->child_cm_id);
  err2:
        rping_free_buffers(cb);

examples/rping.c static int rping_run_client(struct rping_cb *cb) · line 1064

Quoted 8 lines
examples/rping.c +2 -0
@@ -1055,6 +1057,8 @@ static int rping_run_client(struct rping_cb *cb)
 
        rping_test_client(cb);
        rdma_disconnect(cb->cm_id);
+       pthread_cancel(cb->cqthread);
+       pthread_join(cb->cqthread, NULL);
  err2:
        rping_free_buffers(cb);
  err1:

Hello Steve,

Quoted 6 lines

Are you aware that in general it is easy to trigger a deadlock or other undesired behavior by invoking pthread_cancel() ? If a thread e.g. gets canceled after having obtained a mutex lock and before that mutex is unlocked, this will cause trouble for any other thread that tries to grab the mutex.

Quoted 6 lines

I was under the impression that the thread would only be canceled at precise cancellation points. Like in system calls, or if the thread calls
pthread_testcancel(), which is what rping does. There are no mutexes held
by the thread being canceled either. I think its ok. Do you see some other issue with the rping CQ event thread that would cause these problems?

Quoted 5 lines

Please keep in mind that glibc uses locking internally for many file I/O functions, e.g. printf() and fprintf().

Bart.

Ok, so if pthread_cancel() is broken, then what should I be using?

Steve.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#14 lore raw
In reply to a message not in the archive
Quoted 2 lines · Bart Van Assche

Please keep in mind that glibc uses locking internally for many file I/O functions, e.g. printf() and fprintf().

POSIX strictly defines what functions are, aren't and could be cancellation points, yes it is a surprising list. But, glibc won't break internally if you cancel any of its function calls. It has cleanup handlers and what not to protect itself.

rping doesn't hold locks, or allocate memory in the cq_thread, so it seems OK to me. My main question would be what happens internally to ibverbs, and does it call any possible cancellation point while holding locks - is it missing cleanup handlers? Etc.

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20101020203551.GO10362-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org

author
#16 lore raw
Quoted 11 lines · Jason Gunthorpe
Quoted 3 lines · Bart Van Assche

Please keep in mind that glibc uses locking internally for many file I/O functions, e.g. printf() and fprintf().

POSIX strictly defines what functions are, aren't and could be cancellation points, yes it is a surprising list. But, glibc won't break internally if you cancel any of its function calls. It has cleanup handlers and what not to protect itself.

Quoted 7 lines

rping doesn't hold locks, or allocate memory in the cq_thread, so it seems OK to me. My main question would be what happens internally to ibverbs, and does it call any possible cancellation point while holding locks - is it missing cleanup handlers? Etc.

Jason

Hey Jason, do you have a pointer to the list of pthread cancellation points for Linux?

Steve.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 4CBF5786.2020203-7bPotxP6k4+P2YhJcF5u+vpXobYPEAuW@public.gmane.org

#18 lore raw
Quoted 20 lines · Steve Wise
Quoted 11 lines · Jason Gunthorpe
Quoted 3 lines · Bart Van Assche

Please keep in mind that glibc uses locking internally for many file I/O functions, e.g. printf() and fprintf().

POSIX strictly defines what functions are, aren't and could be cancellation points, yes it is a surprising list. But, glibc won't break internally if you cancel any of its function calls. It has cleanup handlers and what not to protect itself.

Quoted 7 lines

rping doesn't hold locks, or allocate memory in the cq_thread, so it seems OK to me. My main question would be what happens internally to ibverbs, and does it call any possible cancellation point while holding locks - is it missing cleanup handlers? Etc.

Jason

Quoted 8 lines

Hey Jason, do you have a pointer to the list of pthread cancellation points for Linux?

Steve.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Steve:

On a linux system, just try
man 7 pthreads
and scroll down to the cancellation points lists.

Bob

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#19 lore raw
In reply to a message not in the archive
Quoted 2 lines · Steve Wise

Hey Jason, do you have a pointer to the list of pthread cancellation points for Linux?

Sure do

http://www.kernel.org/doc/man-pages/online/pages/man7/pthreads.7.html

POSIX:2008 is a more authoritative source, the above is cribbed from there. You can see the list of cancelation points is pretty wild :(

IMHO, it would be a useful job for someone to sort this out for verbs and the drivers - document what ibv interfaces are not cancelation points and make all the ibv apis cancelation-safe. I bet there isn't much coding, just lots of auditing. Just now I looked through all the functions cq_thread called and decided everything but ibv_poll_cq was certainly not a cancelation point for mlx4/mtcha, while ibv_poll_cq was too complex to tell right away.

In glibc if a function is not cancelable it will be marked with __THROW. I wonder if gcc (or sparse?) could check that functions marked __THROW don't call other functions not marked __THROW ?

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 20101020211617.GP10362-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org

#21 lore raw
Quoted 5 lines · Jason Gunthorpe
Quoted 2 lines · Steve Wise

Hey Jason, do you have a pointer to the list of pthread cancellation points for Linux?

Quoted 7 lines

Sure do

http://www.kernel.org/doc/man-pages/online/pages/man7/pthreads.7.html

POSIX:2008 is a more authoritative source, the above is cribbed from there.
[ ... ]

The IEEE Std 1003.1, 2004 Edition list of cancellation points is available here:
http://www.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_09.html

Bart.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): AANLkTi=eJdjgQ4MKPxddUA2eBOswf-5fo5wm=JzX=Hib-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#23 lore raw
Quoted 10 lines · Bart Van Assche
Quoted 7 lines · Jason Gunthorpe
Quoted 2 lines · Steve Wise

Hey Jason, do you have a pointer to the list of pthread cancellation points for Linux?

Sure do

Quoted 8 lines
Quoted 5 lines

http://www.kernel.org/doc/man-pages/online/pages/man7/pthreads.7.html

POSIX:2008 is a more authoritative source, the above is cribbed from there.
[ ... ]

The IEEE Std 1003.1, 2004 Edition list of cancellation points is available here:
http://www.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_09.html

Just found out that the POSIX:2008 list is also available online:
http://www.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html

Bart.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#24 lore raw
In reply to a message not in the archive
Quoted 5 lines · Bart Van Assche

Are you aware that in general it is easy to trigger a deadlock or other undesired behavior by invoking pthread_cancel() ? If a thread e.g. gets canceled after having obtained a mutex lock and before that mutex is unlocked, this will cause trouble for any other thread that tries to grab the mutex.

Steve's specific case looks OK to me, at least considering how mlx4 and ibverbs operates, but I'm not sure that verbs and all of the drivers have been carefully designed with cancellation safety in mind?

ie what happens to a ibv_poll_cq version that calls out to the kernel like soft-iwarp? That will probably embed a cancelation point. Should ibv_poll_cq be a cancellation point? etc..

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html