Skip to contentSkip to conversation

xrpcrdma: add missing error checks in rpcrdma_ep_destroy

PATCH In review Dan Aloni 2 participants 5 messages Jan 25, 2022

Subsystems (3)
lore
Share & tools
lore

Dan Aloni

author
#0 lore raw

PATCH 1/1 xrpcrdma: add missing error checks in rpcrdma_ep_destroy

These pointers can be non-NULL and contain errors if initialization is aborted early. This is similar to how __svc_rdma_free takes care of it.

---
 net/sunrpc/xprtrdma/verbs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/sunrpc/xprtrdma/verbs.c +3 -3
index f172d1298013..7f3173073e72 100644
@@ -336,14 +336,14 @@ static void rpcrdma_ep_destroy(struct kref *kref)
 		ep->re_id->qp = NULL;
 	}
 
-	if (ep->re_attr.recv_cq)
+	if (ep->re_attr.recv_cq && !IS_ERR(ep->re_attr.recv_cq))
 		ib_free_cq(ep->re_attr.recv_cq);
 	ep->re_attr.recv_cq = NULL;
-	if (ep->re_attr.send_cq)
+	if (ep->re_attr.send_cq && !IS_ERR(ep->re_attr.send_cq))
 		ib_free_cq(ep->re_attr.send_cq);
 	ep->re_attr.send_cq = NULL;
 
-	if (ep->re_pd)
+	if (ep->re_pd && !IS_ERR(ep->re_pd))
 		ib_dealloc_pd(ep->re_pd);
 	ep->re_pd = NULL;
 
Signature · 1 line
2.23.0
#1 lore raw
Quoted 7 lines · Dan Aloni

These pointers can be non-NULL and contain errors if initialization is aborted early. This is similar to how __svc_rdma_free takes care of it.

IIUC the only place that can set these values to an ERR_PTR is rpcrdma_ep_create() ? I think I'd rather have rpcrdma_ep_create() set the fields to NULL in the error cases.

Good catch. I'm afraid to ask how you found this.

Quoted 30 lines

---
net/sunrpc/xprtrdma/verbs.c | 6 +++---

1 file changed, 3 insertions(+), 3 deletions(-)
net/sunrpc/xprtrdma/verbs.c +3 -3
index f172d1298013..7f3173073e72 100644
@@ -336,14 +336,14 @@ static void rpcrdma_ep_destroy(struct kref *kref)
 		ep->re_id->qp = NULL;
 	}
 
-	if (ep->re_attr.recv_cq)
+	if (ep->re_attr.recv_cq && !IS_ERR(ep->re_attr.recv_cq))
 		ib_free_cq(ep->re_attr.recv_cq);
 	ep->re_attr.recv_cq = NULL;
-	if (ep->re_attr.send_cq)
+	if (ep->re_attr.send_cq && !IS_ERR(ep->re_attr.send_cq))
 		ib_free_cq(ep->re_attr.send_cq);
 	ep->re_attr.send_cq = NULL;
 
-	if (ep->re_pd)
+	if (ep->re_pd && !IS_ERR(ep->re_pd))
 		ib_dealloc_pd(ep->re_pd);
 	ep->re_pd = NULL;
 
Signature · 1 line
2.23.0

--
Chuck Lever

author
#2 lore raw
Quoted 4 lines · Chuck Lever III
Quoted 4 lines · Dan Aloni
Quoted 8 lines
Quoted 3 lines

These pointers can be non-NULL and contain errors if initialization is aborted early. This is similar to how __svc_rdma_free takes care of it.

IIUC the only place that can set these values to an ERR_PTR is rpcrdma_ep_create() ? I think I'd rather have rpcrdma_ep_create() set the fields to NULL in the error cases.

Actually that was my initialization draft but then I saw __svc_rdma_free. Will send over.

Quoted 1 line

Good catch. I'm afraid to ask how you found this.

Just some adapter entering error state in firmware.

Signature · 1 line
Dan Aloni
author
#3 lore raw

PATCH 1/1 xprtrdma: fix pointer derefs in error cases of rpcrdma_ep_create

In reply to Chuck Lever III

If there are failures then we must not leave the non-NULL pointers with the error value, otherwise rpcrdma_ep_destroy gets confused and tries free them, resulting in an Oops.

---
 net/sunrpc/xprtrdma/verbs.c | 3 +++
 1 file changed, 3 insertions(+)
net/sunrpc/xprtrdma/verbs.c +3 -0
index 3d3673ba9e1e..2a2e1514ac79 100644
@@ -436,6 +436,7 @@ static int rpcrdma_ep_create(struct rpcrdma_xprt *r_xprt)
 					      IB_POLL_WORKQUEUE);
 	if (IS_ERR(ep->re_attr.send_cq)) {
 		rc = PTR_ERR(ep->re_attr.send_cq);
+		ep->re_attr.send_cq = NULL;
 		goto out_destroy;
 	}
 
@@ -444,6 +445,7 @@ static int rpcrdma_ep_create(struct rpcrdma_xprt *r_xprt)
 					      IB_POLL_WORKQUEUE);
 	if (IS_ERR(ep->re_attr.recv_cq)) {
 		rc = PTR_ERR(ep->re_attr.recv_cq);
+		ep->re_attr.recv_cq = NULL;
 		goto out_destroy;
 	}
 	ep->re_receive_count = 0;
@@ -482,6 +484,7 @@ static int rpcrdma_ep_create(struct rpcrdma_xprt *r_xprt)
 	ep->re_pd = ib_alloc_pd(device, 0);
 	if (IS_ERR(ep->re_pd)) {
 		rc = PTR_ERR(ep->re_pd);
+		ep->re_pd = NULL;
 		goto out_destroy;
 	}
 
Signature · 1 line
2.23.0
Acked-by on v1 1/1
#4 lore raw

Re: [PATCH] xprtrdma: fix pointer derefs in error cases of rpcrdma_ep_create

Hi Anna-

Quoted 7 lines · Dan Aloni

If there are failures then we must not leave the non-NULL pointers with the error value, otherwise rpcrdma_ep_destroy gets confused and tries free them, resulting in an Oops.

Quoted 35 lines

---
net/sunrpc/xprtrdma/verbs.c | 3 +++

1 file changed, 3 insertions(+)
net/sunrpc/xprtrdma/verbs.c +3 -0
index 3d3673ba9e1e..2a2e1514ac79 100644
@@ -436,6 +436,7 @@ static int rpcrdma_ep_create(struct rpcrdma_xprt *r_xprt)
 					      IB_POLL_WORKQUEUE);
 	if (IS_ERR(ep->re_attr.send_cq)) {
 		rc = PTR_ERR(ep->re_attr.send_cq);
+		ep->re_attr.send_cq = NULL;
 		goto out_destroy;
 	}
 
@@ -444,6 +445,7 @@ static int rpcrdma_ep_create(struct rpcrdma_xprt *r_xprt)
 					      IB_POLL_WORKQUEUE);
 	if (IS_ERR(ep->re_attr.recv_cq)) {
 		rc = PTR_ERR(ep->re_attr.recv_cq);
+		ep->re_attr.recv_cq = NULL;
 		goto out_destroy;
 	}
 	ep->re_receive_count = 0;
@@ -482,6 +484,7 @@ static int rpcrdma_ep_create(struct rpcrdma_xprt *r_xprt)
 	ep->re_pd = ib_alloc_pd(device, 0);
 	if (IS_ERR(ep->re_pd)) {
 		rc = PTR_ERR(ep->re_pd);
+		ep->re_pd = NULL;
 		goto out_destroy;
 	}
 
Signature · 1 line
2.23.0

--
Chuck Lever