From: netdev-bot+sashiko@kernel.org
To: piyus@iiitd.ac.in
Cc: achender@kernel.org, netdev@vger.kernel.org,
linux-rdma@vger.kernel.org, rds-devel@oss.oracle.com,
davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
pabeni@redhat.com, willemb@google.com, horms@kernel.org,
ssantosh@kernel.org, avinash.repaka@oracle.com,
linux-kernel@vger.kernel.org, kapil@presceralabs.ai
Subject: Re: [PATCH net v2] net/rds: do not leave ic->i_cm_id pointing at a freed cm_id
Date: Sat, 05 Dec 2026 06:22:53 +0000 [thread overview]
Message-ID: <179645177348.3146.7485409361187588099@kernel.org> (raw)
In-Reply-To: <20261201120335.3527546-1-piyus@iiitd.ac.in>
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 2 (1) · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The patch shrinks the use-after-free it carries Fixes:/Cc:
stable for, but does not close it.
- [High] The new guard in rds_ib_post_reg_frmr() treats a non-NULL
i_cm_id->qp as a live send queue, but teardown destroys the QP while…
- [Medium] rds_ib_conn_path_connect() still lets lockless readers see an
error pointer in ic->i_cm_id.
- [Low] The new comments describe the lockless readers inconsistently.
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but
rds_ib_conn_info_visitor() and rds6_ib_conn_info_visitor()…
--
net/rds: do not leave ic->i_cm_id pointing at a freed cm_id
This patch changes the shutdown and connect failure paths so that
ic->i_cm_id is cleared with WRITE_ONCE() before rdma_destroy_id() frees
it. It switches the FRMR readers to a single READ_ONCE() snapshot. It also
adds a NULL check in rds_ib_post_reg_frmr() that returns -ENODEV.
Link: https://lore.kernel.org/all/20261128033737.467966-1-piyus@iiitd.ac.in/
> diff --git a/net/rds/ib_cm.c b/net/rds/ib_cm.c
> index 16bfeaaf3b974..1de3866b1ab62 100644
> --- a/net/rds/ib_cm.c
> +++ b/net/rds/ib_cm.c
[ ... ]
> @@ -994,7 +995,7 @@ int rds_ib_conn_path_connect(struct rds_conn_path *cp)
> RDMA_PS_TCP, IB_QPT_RC);
> if (IS_ERR(ic->i_cm_id)) {
> ret = PTR_ERR(ic->i_cm_id);
> - ic->i_cm_id = NULL;
> + WRITE_ONCE(ic->i_cm_id, NULL);
[Severity: Medium]
Can a lockless reader see an error pointer in ic->i_cm_id here? The
assignment just above this hunk is a plain store of the raw return value
of rdma_create_id(), made before the IS_ERR() test:
ic->i_cm_id = rdma_create_id(&init_net, handler, conn,
RDMA_PS_TCP, IB_QPT_RC);
if (IS_ERR(ic->i_cm_id)) {
Until the WRITE_ONCE(ic->i_cm_id, NULL) runs, the field holds a value such
as ERR_PTR(-ENOMEM).
Every reader this patch touches tests only for NULL before it
dereferences ->qp:
rds_ib_get_mr():
i_cm_id = ic ? READ_ONCE(ic->i_cm_id) : NULL;
if (!i_cm_id || !i_cm_id->qp) {
rds_ib_post_reg_frmr():
i_cm_id = READ_ONCE(ibmr->ic->i_cm_id);
if (!i_cm_id || !i_cm_id->qp) {
rds_ib_post_inv():
if (!i_cm_id || !i_cm_id->qp || !frmr->mr)
__rds_rdma_map() passes cp->cp_conn to rds_ib_get_mr() whatever state the
connection is in. So sendmsg(RDS_CMSG_RDMA_MAP) can run while the connect
worker is inside rds_ib_conn_path_connect().
Suppose rdma_create_id() fails with -ENOMEM in that window. Would the
reader pass the NULL test and then load i_cm_id->qp from
ERR_PTR(-ENOMEM) + 0x10, which is address 0x4?
Could rdma_create_id() be assigned to the local cm_id instead, with
IS_ERR() checked on the local, so that only a valid pointer is ever
published with WRITE_ONCE()?
On the passive side, rds_ib_cm_handle_connect() still publishes the field
with a plain store:
ic->i_cm_id = cm_id;
cm_id->context = conn;
Should that store also be a WRITE_ONCE(), to pair with the new READ_ONCE()
readers?
[ ... ]
> @@ -1006,8 +1007,12 @@ int rds_ib_conn_path_connect(struct rds_conn_path *cp)
> ret = rdma_restrict_node_type(ic->i_cm_id, RDMA_NODE_IB_CA);
> if (ret) {
> rdsdebug("rdma_restrict_node_type() failed: %d\n", ret);
> - rdma_destroy_id(ic->i_cm_id);
> - ic->i_cm_id = NULL;
> + /* Clear it before the free, as rds_ib_conn_path_shutdown()
> + * does: the readers below take no lock.
> + */
[Severity: Low]
This isn't a bug, but which readers does "the readers below" mean? In this
function only rdma_resolve_addr() and rdsdebug() follow. The lockless
readers live in ib_rdma.c and ib_frmr.c.
The comment added in rds_ib_conn_path_shutdown() is also incomplete. It
names only rds_ib_post_inv() and rds_ib_get_mr() as lockless readers. This
same patch makes rds_ib_post_reg_frmr() a READ_ONCE() reader, and the new
comment there says "nothing here excludes it".
[ ... ]
> @@ -1178,7 +1185,14 @@ 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, so a
> + * racing reader sees NULL instead of a dangling pointer.
> + * rds_ib_post_inv() and rds_ib_get_mr() read ic->i_cm_id
> + * without holding anything that excludes this teardown.
> + */
> + cm_id = ic->i_cm_id;
> + WRITE_ONCE(ic->i_cm_id, NULL);
> + rdma_destroy_id(cm_id);
[Severity: High]
Does this close the use-after-free in the KASAN report, or does it only
narrow the window? Nothing here waits for a reader that took its snapshot
of ic->i_cm_id before the WRITE_ONCE().
In rds_ib_post_inv(), the snapshot is followed directly by the ->qp load
that KASAN flagged:
struct rdma_cm_id *i_cm_id = READ_ONCE(ibmr->ic->i_cm_id);
...
if (!i_cm_id || !i_cm_id->qp || !frmr->mr)
That code runs in preemptible process context:
rds_release()->rds_ib_flush_mrs()->rds_ib_flush_mr_pool()->
rds_ib_unreg_frmr()->rds_ib_post_inv()
Suppose the task is preempted between the READ_ONCE() and the ->qp load,
and meanwhile rds_conn_shutdown()->rds_ib_conn_path_shutdown() reaches
rdma_destroy_id(). Would the ->qp read still land on the freed cm_id?
rds_ib_post_reg_frmr() takes its snapshot after
atomic_inc(&ibmr->ic->i_fastreg_inuse_count). That increment can happen
after rds_ib_conn_path_shutdown_check_wait() has already seen
i_fastreg_inuse_count == 0. If so, nothing seems to order the gap between
the snapshot and ib_post_send(i_cm_id->qp, ...) against rdma_destroy_id().
rds_ib_get_mr() follows the same snapshot-then-dereference pattern on the
sendmsg(RDS_CMSG_RDMA_MAP) path.
The commit message acknowledges this:
This does not serialise the two sides: a reader that has already loaded a
non-NULL pointer can still be preempted before using it.
The patch carries Fixes: and Cc: stable tags, and the subject says the
pointer no longer dangles. Should the FRMR paths get real exclusion
against teardown instead of only a reordered clear? Options include a
reference that keeps the cm_id alive across the posts, or quiescing FRMR
users before rdma_destroy_id().
[Severity: High]
This isn't a bug introduced by this patch, but the comment above names
only rds_ib_post_inv() and rds_ib_get_mr(). The info visitors in
net/rds/ib.c also read ic->i_cm_id and ic->rds_ibdev without excluding
this teardown:
net/rds/ib.c:rds_ib_conn_info_visitor() {
...
if (rds_conn_state(conn) == RDS_CONN_UP) {
struct rds_ib_device *rds_ibdev;
rdma_read_gids(ic->i_cm_id, (union ib_gid *)&iinfo->src_gid,
(union ib_gid *)&iinfo->dst_gid);
rds_ibdev = ic->rds_ibdev;
...
iinfo->max_send_sge = rds_ibdev->max_sge;
rds_ib_get_mr_info(rds_ibdev, iinfo);
...
}
rds6_ib_conn_info_visitor() does the same. Both are reached through:
getsockopt(SOL_RDS, RDS_INFO_IB_CONNECTIONS)->rds_info_getsockopt()->
rds_ib_ic_info()->rds_for_each_conn_info()
rds_for_each_conn_info() holds only rcu_read_lock(), and carries the
comment "XXX no c_lock usage..". rdma_destroy_id() frees the cm_id
synchronously, and rds_ib_remove_conn() sets ic->rds_ibdev = NULL.
Suppose a visitor sees RDS_CONN_UP and is then delayed while
rds_conn_shutdown()->rds_ib_conn_path_shutdown() runs. Can rdma_read_gids()
then dereference cm_id->device through a freed or NULL cm_id? Can the
rds_ibdev->max_sge read become a NULL dereference?
[ ... ]
> diff --git a/net/rds/ib_frmr.c b/net/rds/ib_frmr.c
> index 8397aa4a17ac8..1fd7837dd53b0 100644
> --- a/net/rds/ib_frmr.c
> +++ b/net/rds/ib_frmr.c
[ ... ]
> @@ -164,7 +165,21 @@ 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, ®_wr.wr, NULL);
> + /* Test it the way rds_ib_post_inv() does: the teardown clears
> + * ic->i_cm_id and nothing here excludes it. -ENODEV is what
> + * rds_ib_get_mr() reports for the same condition, and
> + * __rds_rdma_map() turns that into rds_conn_connect_if_down() and
> + * rds_sendmsg() into -EAGAIN, so the caller retries once the
> + * connection is back.
> + */
> + i_cm_id = READ_ONCE(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 = -ENODEV;
> + goto out_inc;
> + }
> +
> + ret = ib_post_send(i_cm_id->qp, ®_wr.wr, NULL);
[Severity: High]
Can this check pass while the QP is being destroyed?
rds_ib_conn_path_shutdown() destroys the QP long before the new
WRITE_ONCE(ic->i_cm_id, NULL):
if (ic->i_cm_id->qp)
rdma_destroy_qp(ic->i_cm_id);
rdma_destroy_qp() also frees the QP before it clears id.qp:
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);
}
Take a registration that starts after
rds_ib_conn_path_shutdown_check_wait() has passed:
rds_ib_get_mr()->rds_ib_reg_frmr()->rds_ib_map_frmr()->
rds_ib_post_reg_frmr()
It would see a non-NULL i_cm_id and a non-NULL ->qp. It would then call
ib_post_send(), and so qp->device->ops.post_send, on a QP that is being or
has been freed.
->qp is also loaded twice without READ_ONCE(): once for the test and once
for the ib_post_send() argument. If the second load sees the
id.qp = NULL written by rdma_destroy_qp(), does ib_post_send(NULL, ...)
dereference qp->device?
What if the post instead succeeds after
disable_work_sync(&ic->i_send_work) but before the CQs are destroyed? Is
the REG_MR completion ever processed, or does the sender block forever
here:
wait_event(frmr->fr_reg_done, !frmr->fr_reg);
rds_ib_post_inv(), which this check copies, does the same test and later
reload of ->qp across its i_fastreg_wrs spin.
The v2 changelog says the destroyed-QP crash needs its own fix. The new
in-tree comment, though, reads as if this test makes the site safe
against teardown. Should the comment, or the check, reflect that gap?
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201120335.3527546-1-piyus%40iiitd.ac.in
prev parent reply other threads:[~2026-12-05 6:22 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 12:03 [PATCH net v2] net/rds: do not leave ic->i_cm_id pointing at a freed cm_id Piyus Kedia
2026-12-02 12:03 ` sashiko-bot
2026-12-05 6:22 ` netdev-bot+sashiko [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179645177348.3146.7485409361187588099@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=achender@kernel.org \
--cc=avinash.repaka@oracle.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kapil@presceralabs.ai \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rdma@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=piyus@iiitd.ac.in \
--cc=rds-devel@oss.oracle.com \
--cc=ssantosh@kernel.org \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox