Skip to contentSkip to conversation

RDMA/rxe: Fix a race condition related to the QP error state

PATCH In review Bart Van Assche 6 participants 12 messages Jan 9, 2018 – Jan 12, 2018 3 messages not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RDMA/rxe: Fix a race condition related to the QP error state

The following sequence:
* Change queue pair state into IB_QPS_ERR.
* Post a work request on the queue pair.
Triggers the following race condition in the rdma_rxe driver:
* rxe_qp_error() triggers an asynchronous call of rxe_completer(), the function that examines the QP send queue.
* rxe_post_send() posts a work request on the QP send queue.
Avoid that this race causes a work request to be ignored by scheduling an rxe_completer() call from rxe_post_send() for queues that are in the error state.

---
 drivers/infiniband/sw/rxe/rxe_verbs.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/infiniband/sw/rxe/rxe_verbs.c +2 -0
index a6fbed48db8a..8f631d64c192 100644
@@ -814,6 +814,8 @@ static int rxe_post_send_kernel(struct rxe_qp *qp, struct ib_send_wr *wr,
 			(queue_count(qp->sq.queue) > 1);
 
 	rxe_run_task(&qp->req.task, must_sched);
+	if (unlikely(qp->req.state == QP_STATE_ERROR))
+		rxe_run_task(&qp->comp.task, 1);
 
 	return err;
 }
Signature · 1 line
2.15.1
#1 lore raw
Quoted 7 lines · Bart Van Assche

The following sequence:
* Change queue pair state into IB_QPS_ERR.
* Post a work request on the queue pair.
Triggers the following race condition in the rdma_rxe driver:
* rxe_qp_error() triggers an asynchronous call of rxe_completer(), the function that examines the QP send queue.
* rxe_post_send() posts a work request on the QP send queue.

If rxe_completer() runs before rxe_post_send(), the send queue is believed to be empty while a stale work request stays on the send queue indefinitely. To avoid this race, schedule rxe_completer() after a work request is queued on a qp in the error state by rxe_post_send().

I think that improves the log message, yes?

Signature · 3 lines
Doug Ledford <[email protected]>
    GPG KeyID: B826A3330E572FDD
    Key fingerprint = AE6B 1BDA 122B 23B4 265B  1274 B826 A333 0E57 2FDD
#2 lore raw
Quoted 9 lines · Doug Ledford
Quoted 7 lines · Bart Van Assche

The following sequence:
* Change queue pair state into IB_QPS_ERR.
* Post a work request on the queue pair.
Triggers the following race condition in the rdma_rxe driver:
* rxe_qp_error() triggers an asynchronous call of rxe_completer(), the function that examines the QP send queue.
* rxe_post_send() posts a work request on the QP send queue.

Quoted 7 lines

If rxe_completer() runs before rxe_post_send(), the send queue is believed to be empty while a stale work request stays on the send queue indefinitely. To avoid this race, schedule rxe_completer() after a work request is queued on a qp in the error state by rxe_post_send().

I think that improves the log message, yes?

I did some further edits. But, patch applied to for-next.

Signature · 3 lines
Doug Ledford <[email protected]>
    GPG KeyID: B826A3330E572FDD
    Key fingerprint = AE6B 1BDA 122B 23B4 265B  1274 B826 A333 0E57 2FDD
#3 lore raw
Quoted 15 lines · Doug Ledford
Quoted 14 lines · Doug Ledford
Quoted 7 lines · Bart Van Assche

The following sequence:
* Change queue pair state into IB_QPS_ERR.
* Post a work request on the queue pair.
Triggers the following race condition in the rdma_rxe driver:
* rxe_qp_error() triggers an asynchronous call of rxe_completer(), the function that examines the QP send queue.
* rxe_post_send() posts a work request on the QP send queue.

If rxe_completer() runs before rxe_post_send(), the send queue is believed to be empty while a stale work request stays on the send queue indefinitely. To avoid this race, schedule rxe_completer() after a work request is queued on a qp in the error state by rxe_post_send().

Quoted 4 lines
Quoted 2 lines

I think that improves the log message, yes?

I did some further edits. But, patch applied to for-next.

The proposed patch definitely decreases the chance of races, but it is not fixing them. There is a chance to have change in qp state immediately after your "if ..." check.

Thanks

Quoted 5 lines

--
Doug Ledford <[email protected]>

    GPG KeyID: B826A3330E572FDD
    Key fingerprint = AE6B 1BDA 122B 23B4 265B  1274 B826 A333 0E57 2FDD
#13 lore raw
In reply to Bart Van Assche
Quoted 32 lines · Bart Van Assche

The following sequence:
* Change queue pair state into IB_QPS_ERR.
* Post a work request on the queue pair.
Triggers the following race condition in the rdma_rxe driver:
* rxe_qp_error() triggers an asynchronous call of rxe_completer(), the function that examines the QP send queue.
* rxe_post_send() posts a work request on the QP send queue.
Avoid that this race causes a work request to be ignored by scheduling an rxe_completer() call from rxe_post_send() for queues that are in the error state.

---
 drivers/infiniband/sw/rxe/rxe_verbs.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/infiniband/sw/rxe/rxe_verbs.c +2 -1
index a6fbed48db8a..8f631d64c192 100644
@@ -814,6 +814,8 @@ static int rxe_post_send_kernel(struct rxe_qp *qp, struct ib_send_wr *wr,
                        (queue_count(qp->sq.queue) > 1);
 
        rxe_run_task(&qp->req.task, must_sched);
+       if (unlikely(qp->req.state == QP_STATE_ERROR))
+               rxe_run_task(&qp->comp.task, 1);
 
        return err;
 }
--
Quoted 2 lines

2.15.1

Maybe I am missing something but I think that the race is when qp is in ERROR state and the following functions run in parallel
* rxe_drain_req_pkts (called from rxe_requester after post_send)
* rxe_drain_resp_pkts (called from rxe_completer after modify to ERROR)

Am I right?

#5 lore raw
In reply to a message not in the archive

T24gVGh1LCAyMDE4LTAxLTExIGF0IDA4OjIyICswMjAwLCBMZW9uIFJvbWFub3Zza3kgd3JvdGU6 DQo+IFRoZSBwcm9wb3NlZCBwYXRjaCBkZWZpbml0ZWx5IGRlY3JlYXNlcyB0aGUgY2hhbmNlIG9m IHJhY2VzLCBidXQgaXQgaXMgbm90IGZpeGluZyB0aGVtLg0KPiBUaGVyZSBpcyBhIGNoYW5jZSB0 byBoYXZlIGNoYW5nZSBpbiBxcCBzdGF0ZSBpbW1lZGlhdGVseSBhZnRlciB5b3VyICJpZiAuLi4i IGNoZWNrLg0KDQpIZWxsbyBMZW9uLA0KDQpQbGVhc2UgaGF2ZSBhIGxvb2sgYXQgcnhlX3FwX2Vy cm9yKCkgYW5kIHlvdSB3aWxsIHNlZSB0aGF0IHRoZSBwYXRjaCBJIHBvc3RlZA0KaXMgYSBwcm9w ZXIgZml4LiBJbiB0aGUgc2NlbmFyaW8geW91IGRlc2NyaWJlZCByeGVfcXBfZXJyb3IoKSB3aWxs IHRyaWdnZXIgYQ0KcnVuIG9mIHJ4ZV9jb21wbGV0ZXIoKS4NCg0KQmFydC4=
--
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 Moni Shoua
Quoted 6 lines · Moni Shoua

Maybe I am missing something but I think that the race is when qp is in ERROR state and the following functions run in parallel
* rxe_drain_req_pkts (called from rxe_requester after post_send)
* rxe_drain_resp_pkts (called from rxe_completer after modify to ERROR)

Am I right?

Hello Moni,

I think that's a real race and a race that has to be fixed but not the race that caused the missing completions in the tests I ran myself.

Best regards,

Bart.

#7 lore raw
In reply to a message not in the archive
Quoted 9 lines · Bart Van Assche
Quoted 2 lines · Leon Romanovsky

The proposed patch definitely decreases the chance of races, but it is not fixing them. There is a chance to have change in qp state immediately after your "if ..." check.

Hello Leon,

Please have a look at rxe_qp_error() and you will see that the patch I posted is a proper fix. In the scenario you described rxe_qp_error() will trigger a run of rxe_completer().

Bart,

What am I missing?

CPU1 CPU2

							if (unlikely....
						<---
/* move the qp to the error state */
void rxe_qp_error(struct rxe_qp *qp)
{
        qp->req.state = QP_STATE_ERROR;
        qp->resp.state = QP_STATE_ERROR;
        qp->attr.qp_state = IB_QPS_ERR;
						--->
							rxe_run_task(&qp->req.task, must_sched);

It is more or less the same as without "if (unlikely..."

Thanks

Quoted 2 lines

Bart.

#8 lore raw
Quoted 29 lines · Leon Romanovsky
Quoted 9 lines · Bart Van Assche
Quoted 2 lines · Leon Romanovsky

The proposed patch definitely decreases the chance of races, but it is not fixing them. There is a chance to have change in qp state immediately after your "if ..." check.

Hello Leon,

Please have a look at rxe_qp_error() and you will see that the patch I posted is a proper fix. In the scenario you described rxe_qp_error() will trigger a run of rxe_completer().

Bart,

What am I missing?

CPU1 CPU2

							if (unlikely....
						<---
/* move the qp to the error state */
void rxe_qp_error(struct rxe_qp *qp)
{
        qp->req.state = QP_STATE_ERROR;
        qp->resp.state = QP_STATE_ERROR;
        qp->attr.qp_state = IB_QPS_ERR;
						--->
							rxe_run_task(&qp->req.task, must_sched);
Quoted 1 line

It is more or less the same as without "if (unlikely..."

Hello Leon,

In the above the part of rxe_qp_error() that I was referring to in my e-mail is missing:

	if (qp_type(qp) == IB_QPT_RC)
		rxe_run_task(&qp->comp.task, 1);

Bart.

#9 lore raw
Quoted 32 lines · Bart Van Assche
Quoted 29 lines · Leon Romanovsky
Quoted 9 lines · Bart Van Assche
Quoted 2 lines · Leon Romanovsky

The proposed patch definitely decreases the chance of races, but it is not fixing them. There is a chance to have change in qp state immediately after your "if ..." check.

Hello Leon,

Please have a look at rxe_qp_error() and you will see that the patch I posted is a proper fix. In the scenario you described rxe_qp_error() will trigger a run of rxe_completer().

Bart,

What am I missing?

CPU1 CPU2

							if (unlikely....
						<---
/* move the qp to the error state */
void rxe_qp_error(struct rxe_qp *qp)
{
        qp->req.state = QP_STATE_ERROR;
        qp->resp.state = QP_STATE_ERROR;
        qp->attr.qp_state = IB_QPS_ERR;
						--->
							rxe_run_task(&qp->req.task, must_sched);
Quoted 1 line

It is more or less the same as without "if (unlikely..."

Quoted 7 lines

Hello Leon,

In the above the part of rxe_qp_error() that I was referring to in my e-mail is missing:

	if (qp_type(qp) == IB_QPT_RC)
		rxe_run_task(&qp->comp.task, 1);

But it is exactly where race exists, as long QP isn't protected, it can switch CPUs and create race.

Thanks

Quoted 2 lines

Bart.

#11 lore raw
In reply to a message not in the archive

T24gRnJpLCAyMDE4LTAxLTEyIGF0IDA4OjIzICswMjAwLCBMZW9uIFJvbWFub3Zza3kgd3JvdGU6 DQo+IE9uIFRodSwgSmFuIDExLCAyMDE4IGF0IDA3OjA3OjA2UE0gKzAwMDAsIEJhcnQgVmFuIEFz c2NoZSB3cm90ZToNCj4gPiBPbiBUaHUsIDIwMTgtMDEtMTEgYXQgMjE6MDAgKzAyMDAsIExlb24g Um9tYW5vdnNreSB3cm90ZToNCj4gPiA+IE9uIFRodSwgSmFuIDExLCAyMDE4IGF0IDA0OjAyOjMz UE0gKzAwMDAsIEJhcnQgVmFuIEFzc2NoZSB3cm90ZToN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h bmQgQm90dG9tIEhhbHZlcyIgY2hhcHRlciBpbiBMaW51eCBEZXZpY2UgRHJpdmVycywNCjNyZCBl ZGl0aW9uLiBTZWUgYWxzbyB0aGUgdGFza2xldF9zY2hlZHVsZSgpIGltcGxlbWVudGF0aW9uIGlu DQo8bGludXgvaW50ZXJydXB0Lmg+IGFuZCBpbiBrZXJuZWwvc29mdGlycS5jLg0KDQpUaGFua3Ms DQoNCkJhcnQu
--
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

#12 lore raw
Quoted 46 lines · Bart Van Assche
Quoted 38 lines · Leon Romanovsky
Quoted 37 lines · Bart Van Assche
Quoted 29 lines · Leon Romanovsky
Quoted 9 lines · Bart Van Assche
Quoted 2 lines · Leon Romanovsky

The proposed patch definitely decreases the chance of races, but it is not fixing them. There is a chance to have change in qp state immediately after your "if ..." check.

Hello Leon,

Please have a look at rxe_qp_error() and you will see that the patch I posted is a proper fix. In the scenario you described rxe_qp_error() will trigger a run of rxe_completer().

Bart,

What am I missing?

CPU1 CPU2

							if (unlikely....
						<---
/* move the qp to the error state */
void rxe_qp_error(struct rxe_qp *qp)
{
        qp->req.state = QP_STATE_ERROR;
        qp->resp.state = QP_STATE_ERROR;
        qp->attr.qp_state = IB_QPS_ERR;
						--->
							rxe_run_task(&qp->req.task, must_sched);
Quoted 1 line

It is more or less the same as without "if (unlikely..."

Hello Leon,

In the above the part of rxe_qp_error() that I was referring to in my e-mail is missing:

Quoted 6 lines
Quoted 2 lines
	if (qp_type(qp) == IB_QPT_RC)
		rxe_run_task(&qp->comp.task, 1);

But it is exactly where race exists, as long QP isn't protected, it can switch CPUs and create race.

Quoted 7 lines

Hello Leon,

Can you clarify which race you are referring to? rxe_run_task() uses the tasklet mechanism and tasklets are guaranteed to run on at most one CPU at a time. See also the "Top and Bottom Halves" chapter in Linux Device Drivers, 3rd edition. See also the tasklet_schedule() implementation in <linux/interrupt.h> and in kernel/softirq.c.

Ahh, Bart, Sorry.

I found the cause of my misunderstanding, it is "comp" in the rxe_run_task call and not "req".

Thanks

Quoted 4 lines

Thanks,

Bart.