Skip to contentSkip to conversation

net/rds: do not leave ic->i_cm_id pointing at a freed cm_id

PATCH Superseded Piyus Kedia 3 participants 4 messages Nov 28 – Dec 1

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/rds: do not leave ic->i_cm_id pointing at a freed cm_id

rds_ib_conn_path_shutdown() frees the connection's rdma_cm_id and only clears ic->i_cm_id afterwards:

	rdma_destroy_id(ic->i_cm_id);
	if (ic->rds_ibdev)
		rds_ib_remove_conn(ic->rds_ibdev, conn);
	ic->i_cm_id = NULL;

rds_ib_remove_conn() takes locks and can sleep, so the pointer is left pointing at freed memory for a while. Both FRMR paths read it with nothing that excludes this teardown - rds_ib_get_mr() from sendmsg()'s RDS_CMSG_RDMA_MAP handling, and rds_ib_unreg_frmr() from the MR pool flush - and the NULL test in rds_ib_unreg_frmr() does not help, because the pointer is not NULL, it is dangling:

  BUG: KASAN: slab-use-after-free in rds_ib_unreg_frmr+0x119/0x860
  Read of size 8 at addr ffff888022d67010
   rds_ib_unreg_frmr+0x119/0x860
   rds_ib_flush_mr_pool+0x685/0x9f0
   rds_ib_flush_mrs+0x57/0xc0
   rds_release+0xc9/0x220
   __sock_release+0x65/0x160

  Allocated by task 37:
   __rdma_create_id+0x48/0x4f0
   __rdma_create_kernel_id+0x3c/0x80
   rds_ib_conn_path_connect+0xfa/0x320

  Freed by task 9508:
   kfree+0x1c7/0x660
   rds_ib_conn_path_shutdown+0xc2b/0xd30
   rds_conn_shutdown+0x3ca/0x940

The same dangling pointer reaches the registration side, where a fuzzer found it posting a work request to the QP of a cm_id already destroyed - rxe reports that as "qp has been destroyed" from its own WARN_ON_ONCE(!qp->valid) and then faults on qp->state_lock.

Clear ic->i_cm_id before destroying it, so a racing reader sees NULL rather than freed memory, and check the pointer in rds_ib_post_reg_frmr() the way rds_ib_unreg_frmr() already does - it is the one FRMR site that dereferenced it without a test.

This does not serialise the two sides: a reader that has already loaded a non-NULL pointer can still be preempted before using it. Closing that would mean giving the registration path the exclusion the other users of ic->i_cm_id have, which is a larger change than this one.

We found this with a syzkaller-based fuzzing harness driven by an AI tool.

Fixes 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")
---
 net/rds/ib_cm.c   | 12 ++++++++++--
 net/rds/ib_frmr.c | 12 +++++++++++-
 2 files changed, 21 insertions(+), 3 deletions(-)
net/rds/ib_cm.c +10 -2
index 16bfeaaf3b97..17d748f08607 100644
@@ -1077,6 +1077,7 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
 {
 	struct rds_connection *conn = cp->cp_conn;
 	struct rds_ib_connection *ic = conn->c_transport_data;
+	struct rdma_cm_id *cm_id;
 	int err = 0;
 
 	rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,
@@ -1178,7 +1179,15 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
 		if (ic->i_recvs)
 			rds_ib_recv_clear_ring(ic);
 
-		rdma_destroy_id(ic->i_cm_id);
+		/* Clear the pointer before freeing what it points at.
+		 * rds_ib_unreg_frmr() and rds_ib_get_mr() read ic->i_cm_id
+		 * without holding anything that excludes this teardown, and
+		 * rds_ib_remove_conn() below can sleep, so a pointer left
+		 * behind here is one they dereference after the free.
+		 */
+		cm_id = ic->i_cm_id;
+		ic->i_cm_id = NULL;
+		rdma_destroy_id(cm_id);
 
 		/*
 		 * Move connection back to the nodev list.
@@ -1186,7 +1195,6 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
 		if (ic->rds_ibdev)
 			rds_ib_remove_conn(ic->rds_ibdev, conn);
 
-		ic->i_cm_id = NULL;
 		ic->i_pd = NULL;
 		ic->i_send_cq = NULL;
 		ic->i_recv_cq = NULL;
net/rds/ib_frmr.c +11 -1
index 8397aa4a17ac..09e732ac2324 100644
@@ -124,6 +124,7 @@ static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr)
 {
 	struct rds_ib_frmr *frmr = &ibmr->u.frmr;
 	struct ib_reg_wr reg_wr;
+	struct rdma_cm_id *i_cm_id;
 	int ret, off = 0;
 
 	while (atomic_dec_return(&ibmr->ic->i_fastreg_wrs) <= 0) {
@@ -164,7 +165,16 @@ static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr)
 			IB_ACCESS_REMOTE_WRITE;
 	reg_wr.wr.send_flags = IB_SEND_SIGNALED;
 
-	ret = ib_post_send(ibmr->ic->i_cm_id->qp, &reg_wr.wr, NULL);
+	/* Check it the way rds_ib_unreg_frmr() does: the teardown clears
+	 * ic->i_cm_id, and nothing here excludes it.
+	 */
+	i_cm_id = ibmr->ic->i_cm_id;
+	if (!i_cm_id || !i_cm_id->qp) {
+		rds_transition_frwr_state(ibmr, FRMR_IS_INUSE, FRMR_IS_STALE);
+		ret = -ECONNABORTED;
+		goto out_inc;
+	}
+	ret = ib_post_send(i_cm_id->qp, &reg_wr.wr, NULL);
 	if (unlikely(ret)) {
 		/* Failure here can be because of -ENOMEM as well */
 		rds_transition_frwr_state(ibmr, FRMR_IS_INUSE, FRMR_IS_STALE);
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] net/rds: QP use-after-free and NULL deref in rds_ib_post_reg_frmr() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] net/rds: QP use-after-free and NULL deref in rds_ib_post_reg_frmr()
--

commit b01d67e9312d5821512773afb6e27ad4f563c07e
Author: Piyus Kedia <[email protected]>

net/rds: do not leave ic->i_cm_id pointing at a freed cm_id

This commit clears ic->i_cm_id before calling rdma_destroy_id() in rds_ib_conn_path_shutdown() and checks i_cm_id and i_cm_id->qp for NULL before posting the registration work request in rds_ib_post_reg_frmr().

Quoted 4 lines
net/rds/ib_cm.c +0 -0
index 16bfeaaf3b974..17d748f086071 100644

[ ... ]

Quoted 45 lines
net/rds/ib_cm.c +9 -2
@@ -1178,7 +1179,15 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
 		if (ic->i_recvs)
 			rds_ib_recv_clear_ring(ic);
 
-		rdma_destroy_id(ic->i_cm_id);
+		/* Clear the pointer before freeing what it points at.
+		 * rds_ib_unreg_frmr() and rds_ib_get_mr() read ic->i_cm_id
+		 * without holding anything that excludes this teardown, and
+		 * rds_ib_remove_conn() below can sleep, so a pointer left
+		 * behind here is one they dereference after the free.
+		 */
+		cm_id = ic->i_cm_id;
+		ic->i_cm_id = NULL;
+		rdma_destroy_id(cm_id);
 
 		/*
 		 * Move connection back to the nodev list.
@@ -1186,7 +1195,6 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
 		if (ic->rds_ibdev)
 			rds_ib_remove_conn(ic->rds_ibdev, conn);
 
-		ic->i_cm_id = NULL;
 		ic->i_pd = NULL;
 		ic->i_send_cq = NULL;
 		ic->i_recv_cq = NULL;
net/rds/ib_frmr.c +4 -1
index 8397aa4a17ac8..09e732ac23240 100644
@@ -124,6 +124,7 @@ static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr)
 {
 	struct rds_ib_frmr *frmr = &ibmr->u.frmr;
 	struct ib_reg_wr reg_wr;
+	struct rdma_cm_id *i_cm_id;
 	int ret, off = 0;
 
 	while (atomic_dec_return(&ibmr->ic->i_fastreg_wrs) <= 0) {
@@ -164,7 +165,16 @@ static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr)
 			IB_ACCESS_REMOTE_WRITE;
 	reg_wr.wr.send_flags = IB_SEND_SIGNALED;
 
-	ret = ib_post_send(ibmr->ic->i_cm_id->qp, &reg_wr.wr, NULL);
+	/* Check it the way rds_ib_unreg_frmr() does: the teardown clears
+	 * ic->i_cm_id, and nothing here excludes it.
+	 */

net/rds/ib_frmr.c static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr) · line 180

Quoted 10 lines
net/rds/ib_frmr.c +7 -0
+	i_cm_id = ibmr->ic->i_cm_id;
+	if (!i_cm_id || !i_cm_id->qp) {
+		rds_transition_frwr_state(ibmr, FRMR_IS_INUSE, FRMR_IS_STALE);
+		ret = -ECONNABORTED;
+		goto out_inc;
+	}
+	ret = ib_post_send(i_cm_id->qp, &reg_wr.wr, NULL);
 	if (unlikely(ret)) {
 		/* Failure here can be because of -ENOMEM as well */
 		rds_transition_frwr_state(ibmr, FRMR_IS_INUSE, FRMR_IS_STALE);

[Severity: High]
Can rds_ib_post_reg_frmr() still call ib_post_send() on a destroyed or NULL i_cm_id->qp?

rds_ib_conn_path_shutdown() destroys the QP much earlier than it clears ic->i_cm_id, and rdma_destroy_qp() frees id.qp inside ib_destroy_qp() before setting id.qp = NULL:

drivers/infiniband/core/cma.c:rdma_destroy_qp() {
	mutex_lock(&id_priv->qp_mutex);
	ib_destroy_qp(id_priv->id.qp);
	id_priv->id.qp = NULL;
	mutex_unlock(&id_priv->qp_mutex);
}

When sendmsg() with RDS_CMSG_RDMA_MAP races with connection shutdown, both ic->i_cm_id and i_cm_id->qp remain non-NULL throughout ib_destroy_qp():

CPU0 CPU1
rds_ib_conn_path_shutdown()
rdma_destroy_qp(ic->i_cm_id)

    ib_destroy_qp(id_priv->id.qp)
      rxe_destroy_qp()
        ...
      kfree(qp)
                                      rds_ib_post_reg_frmr()
                                        i_cm_id = ibmr->ic->i_cm_id;
                                        if (!i_cm_id || !i_cm_id->qp)
                                        ib_post_send(i_cm_id->qp, ...)
                                          <-- use-after-free on qp
    id_priv->id.qp = NULL;

...
ic->i_cm_id = NULL;
rdma_destroy_id(cm_id);

This leaves the rxe "qp has been destroyed" warning and qp->state_lock use-after-free reachable.

In addition, rds_ib_post_reg_frmr() re-reads i_cm_id->qp in the ib_post_send() call instead of using a local pointer, so if CPU0 sets id_priv->id.qp = NULL between the !i_cm_id->qp check and ib_post_send(), ib_post_send() dereferences a NULL qp pointer. And because nothing serializes the reader against teardown, a thread preempted after loading i_cm_id can still dereference i_cm_id after rdma_destroy_id() frees it.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Piyus Kedia

Hi Piyus,

Thanks for the patch. The Sashiko review for this one didn't get mailed out, so you may not have seen it:
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261128033737.467966-1-piyus%40iiitd.ac.in

Comments below:

Quoted 33 lines · Piyus Kedia

rds_ib_conn_path_shutdown() frees the connection's rdma_cm_id and only clears ic->i_cm_id afterwards:

	rdma_destroy_id(ic->i_cm_id);
	if (ic->rds_ibdev)
		rds_ib_remove_conn(ic->rds_ibdev, conn);
	ic->i_cm_id = NULL;

rds_ib_remove_conn() takes locks and can sleep, so the pointer is left pointing at freed memory for a while. Both FRMR paths read it with nothing that excludes this teardown - rds_ib_get_mr() from sendmsg()'s RDS_CMSG_RDMA_MAP handling, and rds_ib_unreg_frmr() from the MR pool flush - and the NULL test in rds_ib_unreg_frmr() does not help, because the pointer is not NULL, it is dangling:

  BUG: KASAN: slab-use-after-free in rds_ib_unreg_frmr+0x119/0x860
  Read of size 8 at addr ffff888022d67010
   rds_ib_unreg_frmr+0x119/0x860
   rds_ib_flush_mr_pool+0x685/0x9f0
   rds_ib_flush_mrs+0x57/0xc0
   rds_release+0xc9/0x220
   __sock_release+0x65/0x160

  Allocated by task 37:
   __rdma_create_id+0x48/0x4f0
   __rdma_create_kernel_id+0x3c/0x80
   rds_ib_conn_path_connect+0xfa/0x320

  Freed by task 9508:
   kfree+0x1c7/0x660
   rds_ib_conn_path_shutdown+0xc2b/0xd30
   rds_conn_shutdown+0x3ca/0x940
Quoted 4 lines

The same dangling pointer reaches the registration side, where a fuzzer found it posting a work request to the QP of a cm_id already destroyed - rxe reports that as "qp has been destroyed" from its own WARN_ON_ONCE(!qp->valid) and then faults on qp->state_lock.

The rxe "qp has been destroyed" crash doesn't look like it comes from the freed cm_id. rdma_destroy_qp() frees the QP and clears id->qp well before rdma_destroy_id(), so that post went through a live cm_id. The new check in rds_ib_post_reg_frmr() doesn't close that. So please either drop that claim from the commit message or fix it separately.

Quoted 11 lines

Clear ic->i_cm_id before destroying it, so a racing reader sees NULL rather than freed memory, and check the pointer in rds_ib_post_reg_frmr() the way rds_ib_unreg_frmr() already does - it is the one FRMR site that dereferenced it without a test.

This does not serialise the two sides: a reader that has already loaded a non-NULL pointer can still be preempted before using it. Closing that would mean giving the registration path the exclusion the other users of ic->i_cm_id have, which is a larger change than this one.

Quoted 6 lines

We found this with a syzkaller-based fuzzing harness driven by an AI tool.

Fixes 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")

Nit: as written, the sob chain says Kapil handled the patch before its author did. If Kapil co-developed this, his Signed-off-by needs a Co-developed-by ahead of it.

Quoted 18 lines
---
 net/rds/ib_cm.c   | 12 ++++++++++--
 net/rds/ib_frmr.c | 12 +++++++++++-
 2 files changed, 21 insertions(+), 3 deletions(-)
net/rds/ib_cm.c +1 -0
index 16bfeaaf3b97..17d748f08607 100644
@@ -1077,6 +1077,7 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
 {
 	struct rds_connection *conn = cp->cp_conn;
 	struct rds_ib_connection *ic = conn->c_transport_data;
+	struct rdma_cm_id *cm_id;
 	int err = 0;
 
 	rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,

net/rds/ib_cm.c void rds_ib_conn_path_shutdown(struct rds_conn_path *cp) · line 1186

Quoted 9 lines
net/rds/ib_cm.c +5 -1
@@ -1178,7 +1179,15 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
 		if (ic->i_recvs)
 			rds_ib_recv_clear_ring(ic);
 
-		rdma_destroy_id(ic->i_cm_id);
+		/* Clear the pointer before freeing what it points at.
+		 * rds_ib_unreg_frmr() and rds_ib_get_mr() read ic->i_cm_id
+		 * without holding anything that excludes this teardown, and
+		 * rds_ib_remove_conn() below can sleep, so a pointer left
+		 * behind here is one they dereference after the free.

rds_ib_remove_conn() doesn't sleep, it only takes spinlocks, so the "can sleep" part should be reworded here and in the commit message. Also, the load and NULL check are in rds_ib_post_inv(), not rds_ib_unreg_frmr(), so this comment, the one in rds_ib_post_reg_frmr() and the commit message should refer to that instead.

net/rds/ib_cm.c void rds_ib_conn_path_shutdown(struct rds_conn_path *cp) · line 1189

Quoted 3 lines
net/rds/ib_cm.c +3 -0
+		 */
+		cm_id = ic->i_cm_id;
+		ic->i_cm_id = NULL;

Since the readers are lockless, please use WRITE_ONCE() for the store and a single READ_ONCE() snapshot in the readers (rds_ib_get_mr() currently loads ic->i_cm_id twice). Also, the same free-then-clear ordering appears on both error paths in rds_ib_conn_path_connect(), so please apply the same change there.

Quoted 27 lines
net/rds/ib_cm.c +1 -0
+		rdma_destroy_id(cm_id);
 
 
 		/*
net/rds/ib_cm.c +0 -1
 		 * Move connection back to the nodev list.
@@ -1186,7 +1195,6 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
 		if (ic->rds_ibdev)
 			rds_ib_remove_conn(ic->rds_ibdev, conn);
 
-		ic->i_cm_id = NULL;
 		ic->i_pd = NULL;
 		ic->i_send_cq = NULL;
 		ic->i_recv_cq = NULL;
net/rds/ib_frmr.c +1 -0
index 8397aa4a17ac..09e732ac2324 100644
@@ -124,6 +124,7 @@ static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr)
 {
 	struct rds_ib_frmr *frmr = &ibmr->u.frmr;
 	struct ib_reg_wr reg_wr;
+	struct rdma_cm_id *i_cm_id;
 	int ret, off = 0;
 
 	while (atomic_dec_return(&ibmr->ic->i_fastreg_wrs) <= 0) {
@@ -164,7 +165,16 @@ static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr)
 			IB_ACCESS_REMOTE_WRITE;

net/rds/ib_frmr.c static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr) · line 174

Quoted 10 lines
net/rds/ib_frmr.c +7 -1
 	reg_wr.wr.send_flags = IB_SEND_SIGNALED;
 
-	ret = ib_post_send(ibmr->ic->i_cm_id->qp, &reg_wr.wr, NULL);
+	/* Check it the way rds_ib_unreg_frmr() does: the teardown clears
+	 * ic->i_cm_id, and nothing here excludes it.
+	 */
+	i_cm_id = ibmr->ic->i_cm_id;
+	if (!i_cm_id || !i_cm_id->qp) {
+		rds_transition_frwr_state(ibmr, FRMR_IS_INUSE, FRMR_IS_STALE);
+		ret = -ECONNABORTED;

This should return -ENODEV like rds_ib_get_mr() does, so that sendmsg() gets -EAGAIN and a reconnect is started.

net/rds/ib_frmr.c static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr) · line 180

Quoted 6 lines
net/rds/ib_frmr.c +3 -0
+		goto out_inc;
+	}
+	ret = ib_post_send(i_cm_id->qp, &reg_wr.wr, NULL);
 	if (unlikely(ret)) {
 		/* Failure here can be because of -ENOMEM as well */
 		rds_transition_frwr_state(ibmr, FRMR_IS_INUSE, FRMR_IS_STALE);

Lastly, the CI shows a conflict with commit 5b61d9aed8f6 ("net/rds: give cm_id->context a reference on its connection") in net-next. This patch targets net, but we should mention the conflict below the --- in v2 so the maintainers know to expect it.

Thanks for working on this!
Allison

#3 lore raw

Hi Allison,

Thanks for the review! We have submitted a new patch that addresses the issues mentioned in your review. Kindly check.

https://lore.kernel.org/all/[email protected]/

Best regards,
Piyus Kedia
Kapil Vaswani

Quoted 176 lines · Allison Henderson

Hi Piyus,

Thanks for the patch. The Sashiko review for this one didn't get mailed out, so you may not have seen it:
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261128033737.467966-1-piyus%40iiitd.ac.in

Comments below:

Quoted 33 lines · Piyus Kedia

rds_ib_conn_path_shutdown() frees the connection's rdma_cm_id and only clears ic->i_cm_id afterwards:

      rdma_destroy_id(ic->i_cm_id);
      if (ic->rds_ibdev)
              rds_ib_remove_conn(ic->rds_ibdev, conn);
      ic->i_cm_id = NULL;

rds_ib_remove_conn() takes locks and can sleep, so the pointer is left pointing at freed memory for a while. Both FRMR paths read it with nothing that excludes this teardown - rds_ib_get_mr() from sendmsg()'s RDS_CMSG_RDMA_MAP handling, and rds_ib_unreg_frmr() from the MR pool flush - and the NULL test in rds_ib_unreg_frmr() does not help, because the pointer is not NULL, it is dangling:

  BUG: KASAN: slab-use-after-free in rds_ib_unreg_frmr+0x119/0x860
  Read of size 8 at addr ffff888022d67010
   rds_ib_unreg_frmr+0x119/0x860
   rds_ib_flush_mr_pool+0x685/0x9f0
   rds_ib_flush_mrs+0x57/0xc0
   rds_release+0xc9/0x220
   __sock_release+0x65/0x160

  Allocated by task 37:
   __rdma_create_id+0x48/0x4f0
   __rdma_create_kernel_id+0x3c/0x80
   rds_ib_conn_path_connect+0xfa/0x320

  Freed by task 9508:
   kfree+0x1c7/0x660
   rds_ib_conn_path_shutdown+0xc2b/0xd30
   rds_conn_shutdown+0x3ca/0x940
Quoted 4 lines

The same dangling pointer reaches the registration side, where a fuzzer found it posting a work request to the QP of a cm_id already destroyed - rxe reports that as "qp has been destroyed" from its own WARN_ON_ONCE(!qp->valid) and then faults on qp->state_lock.

The rxe "qp has been destroyed" crash doesn't look like it comes from the freed cm_id. rdma_destroy_qp() frees the QP and clears id->qp well before rdma_destroy_id(), so that post went through a live cm_id. The new check in rds_ib_post_reg_frmr() doesn't close that. So please either drop that claim from the commit message or fix it separately.

Quoted 11 lines

Clear ic->i_cm_id before destroying it, so a racing reader sees NULL rather than freed memory, and check the pointer in rds_ib_post_reg_frmr() the way rds_ib_unreg_frmr() already does - it is the one FRMR site that dereferenced it without a test.

This does not serialise the two sides: a reader that has already loaded a non-NULL pointer can still be preempted before using it. Closing that would mean giving the registration path the exclusion the other users of ic->i_cm_id have, which is a larger change than this one.

Quoted 6 lines

We found this with a syzkaller-based fuzzing harness driven by an AI tool.

Fixes 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")

Nit: as written, the sob chain says Kapil handled the patch before its author did. If Kapil co-developed this, his Signed-off-by needs a Co-developed-by ahead of it.

Quoted 18 lines
---
 net/rds/ib_cm.c   | 12 ++++++++++--
 net/rds/ib_frmr.c | 12 +++++++++++-
 2 files changed, 21 insertions(+), 3 deletions(-)
net/rds/ib_cm.c +1 -0
index 16bfeaaf3b97..17d748f08607 100644
@@ -1077,6 +1077,7 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
 {
      struct rds_connection *conn = cp->cp_conn;
      struct rds_ib_connection *ic = conn->c_transport_data;
+     struct rdma_cm_id *cm_id;
      int err = 0;
 
      rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,

net/rds/ib_cm.c void rds_ib_conn_path_shutdown(struct rds_conn_path *cp) · line 1186

Quoted 9 lines
net/rds/ib_cm.c +5 -1
@@ -1178,7 +1179,15 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
              if (ic->i_recvs)
                      rds_ib_recv_clear_ring(ic);
 
-             rdma_destroy_id(ic->i_cm_id);
+             /* Clear the pointer before freeing what it points at.
+              * rds_ib_unreg_frmr() and rds_ib_get_mr() read ic->i_cm_id
+              * without holding anything that excludes this teardown, and
+              * rds_ib_remove_conn() below can sleep, so a pointer left
+              * behind here is one they dereference after the free.

rds_ib_remove_conn() doesn't sleep, it only takes spinlocks, so the "can sleep" part should be reworded here and in the commit message. Also, the load and NULL check are in rds_ib_post_inv(), not rds_ib_unreg_frmr(), so this comment, the one in rds_ib_post_reg_frmr() and the commit message should refer to that instead.

net/rds/ib_cm.c void rds_ib_conn_path_shutdown(struct rds_conn_path *cp) · line 1189

Quoted 3 lines
net/rds/ib_cm.c +3 -0
+              */
+             cm_id = ic->i_cm_id;
+             ic->i_cm_id = NULL;

Since the readers are lockless, please use WRITE_ONCE() for the store and a single READ_ONCE() snapshot in the readers (rds_ib_get_mr() currently loads ic->i_cm_id twice). Also, the same free-then-clear ordering appears on both error paths in rds_ib_conn_path_connect(), so please apply the same change there.

Quoted 27 lines
net/rds/ib_cm.c +1 -0
+             rdma_destroy_id(cm_id);
 
 
              /*

* Move connection back to the nodev list.

net/rds/ib_cm.c +0 -1
@@ -1186,7 +1195,6 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
              if (ic->rds_ibdev)
                      rds_ib_remove_conn(ic->rds_ibdev, conn);
 
-             ic->i_cm_id = NULL;
              ic->i_pd = NULL;
              ic->i_send_cq = NULL;
              ic->i_recv_cq = NULL;
net/rds/ib_frmr.c +1 -0
index 8397aa4a17ac..09e732ac2324 100644
@@ -124,6 +124,7 @@ static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr)
 {
      struct rds_ib_frmr *frmr = &ibmr->u.frmr;
      struct ib_reg_wr reg_wr;
+     struct rdma_cm_id *i_cm_id;
      int ret, off = 0;
 
      while (atomic_dec_return(&ibmr->ic->i_fastreg_wrs) <= 0) {
@@ -164,7 +165,16 @@ static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr)
                      IB_ACCESS_REMOTE_WRITE;

net/rds/ib_frmr.c static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr) · line 174

Quoted 10 lines
net/rds/ib_frmr.c +7 -1
      reg_wr.wr.send_flags = IB_SEND_SIGNALED;
 
-     ret = ib_post_send(ibmr->ic->i_cm_id->qp, &reg_wr.wr, NULL);
+     /* Check it the way rds_ib_unreg_frmr() does: the teardown clears
+      * ic->i_cm_id, and nothing here excludes it.
+      */
+     i_cm_id = ibmr->ic->i_cm_id;
+     if (!i_cm_id || !i_cm_id->qp) {
+             rds_transition_frwr_state(ibmr, FRMR_IS_INUSE, FRMR_IS_STALE);
+             ret = -ECONNABORTED;

This should return -ENODEV like rds_ib_get_mr() does, so that sendmsg() gets -EAGAIN and a reconnect is started.

net/rds/ib_frmr.c static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr) · line 180

Quoted 6 lines
net/rds/ib_frmr.c +3 -0
+             goto out_inc;
+     }
+     ret = ib_post_send(i_cm_id->qp, &reg_wr.wr, NULL);
      if (unlikely(ret)) {
              /* Failure here can be because of -ENOMEM as well */
              rds_transition_frwr_state(ibmr, FRMR_IS_INUSE, FRMR_IS_STALE);

Lastly, the CI shows a conflict with commit 5b61d9aed8f6 ("net/rds: give cm_id->context a reference on its connection") in net-next. This patch targets net, but we should mention the conflict below the --- in v2 so the maintainers know to expect it.

Thanks for working on this!
Allison