Article View: linux.kernel.netdev
Article #15721[PATCH net-next v12 11/12] net/rds: give cm_id->context a reference on its connection
From: Allison Henderson
Date: Fri, 13 Nov 2026 04:43
Date: Fri, 13 Nov 2026 04:43
179 lines
15485 bytes
15485 bytes
rds_rdma_cm_event_handler_cmn() picks the connection up from
cm_id->context and holds c_cm_lock - a mutex that lives in the
connection's path array - across the transport callbacks, but the
context pointer owns no reference. What keeps the connection alive
under the handler is ordering: rds_ib_conn_path_shutdown() destroys the
id, and rdma_destroy_id() waits for a running handler, before the
destroy drops the connection's initial reference, so no event is
handled on a connection that is being freed. Before this series that
ordering was broken from inside the handler, since rds_conn_destroy()
freed synchronously and the one callback that destroyed its
connection, rds_ib_cm_connect_complete() on a protocol version below
3.1, left the handler's mutex_unlock() running on freed memory;
commit f97d8c7bab78 ("rds: ib: use rds_conn_drop() on protocol version
mismatch") has since switched that path to rds_conn_drop(), which also
removed the deadlock that destroy took on c_cm_lock.
Now that connections are reference counted, give the id a reference of
its own rather than rely on that ordering. rds_ib_conn_path_connect()
takes it once rdma_create_id() has installed the connection as the
context, rds_ib_cm_handle_connect() takes it when it installs the id on
the passive side, and each is dropped after rdma_destroy_id() has
returned: in rds_ib_conn_path_shutdown(), and on the two failure paths
of rds_ib_conn_path_connect() that destroy the id straight away. With
that the handler cannot find its connection freed under it, whatever
the callbacks do to the other holders - the destroy's initial
reference, a socket's cache, a parent's c_passive, an inc. None of the
new puts can be the last reference: the shutdown runs on behalf of a
destroy, or of a drop on a connection still in the hash, and the
connect worker runs on a connection still in the hash, and each of
those holds a reference of its own.
rds_ib_cm_initiate_connect() and rds_ib_cm_handle_connect() get a
defensive check of the same kind. A destroy that began while the
address and route were resolving either waits on c_cm_lock behind the
ROUTE_RESOLVED handler - and its shutdown pass then tears down
whatever the handler set up, QP, cm_id and device reference alike - or
has already destroyed the id, in which case no event for it reaches
the handler. On the passive side a quiesced connection is unhashed,
so rds_conn_create() cannot hand it to a connect request, and the
listeners are stopped before any IB connection is destroyed at unload.
Neither path is reachable today, then; both functions nevertheless
decline to set up a QP, or install a new cm_id, on a connection whose
destroy has begun, so that the contract does not rest on that
ordering. rds_ib_cm_initiate_connect() returns success when it
declines: the id is still ic->i_cm_id, for the shutdown to destroy,
and a non-zero return would have the rdma_cm destroy it instead. An
unload that begins after the check has passed is covered by
rds_ib_add_conn() refusing a device that rds_ib_dev_shutdown() has
marked.
Assisted-by: Claude-Code:claude-fable-5
Signed-off-by: Allison Henderson <achender@kernel.org>
---
net/rds/ib_cm.c | 41 ++++++++++++++++++++++++++++++++++++----
net/rds/rdma_transport.c | 7 +++++++
2 files changed, 44 insertions(+), 4 deletions(-)
diff --git a/net/rds/ib_cm.c b/net/rds/ib_cm.c
index 5bf6b22c10d6..a580a8c667ca 100644
--- a/net/rds/ib_cm.c
+++ b/net/rds/ib_cm.c
@@ -876,6 +876,15 @@ int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
* see the comment above rds_queue_reconnect()
*/
mutex_lock(&conn->c_cm_lock);
+ /* Defensive: a destroy that has quiesced this conn also unhashed
+ * it, so rds_conn_create() cannot have returned it, and the
+ * listeners are stopped before any IB connection is destroyed at
+ * unload. Should a request reach a conn in RDS_CONN_DOWN with
+ * its destroy begun all the same, do not install a new cm_id and
+ * QP that nothing would tear down.
+ */
+ if (rds_destroy_pending(conn))
+ goto out;
if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) {
if (rds_conn_state(conn) == RDS_CONN_UP) {
rdsdebug("incoming connect while connecting\n");
@@ -904,6 +913,10 @@ int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
BUG_ON(ic->i_cm_id);
ic->i_cm_id = cm_id;
+ /* The id owns a reference on the connection it points at, put
+ * after rdma_destroy_id() in rds_ib_conn_path_shutdown().
+ */
+ rds_conn_get(conn);
cm_id->context = conn;
/* We got halfway through setting up the ib_connection, if we
@@ -928,10 +941,8 @@ int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
out:
if (conn) {
mutex_unlock(&conn->c_cm_lock);
- /* Drop the reference rds_conn_create() handed us. The
- * conn stays reachable through cm_id->context without a
- * reference of its own for now; the CM event handler is
- * given one of its own by a following patch.
+ /* Drop the reference rds_conn_create() handed us; the
+ * cm_id holds its own from here on.
*/
rds_conn_put(conn);
}
@@ -950,6 +961,18 @@ int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id, bool isv6)
union rds_ib_conn_priv dp;
int ret;
+ /* Defensive: a destroy that began while the address and route
+ * were being resolved either waits on c_cm_lock behind this
+ * handler, and its shutdown pass then tears down whatever is
+ * set up here, or has already destroyed the id this event
+ * arrived on, in which case the event never gets here. Decline
+ * to set up a QP all the same, and return success: the id is
+ * still ic->i_cm_id, for that shutdown to destroy, and a
+ * non-zero return would have the rdma_cm destroy it instead.
+ */
+ if (rds_destroy_pending(conn))
+ return 0;
+
/* If the peer doesn't do protocol negotiation, we must
* default to RDSv3.0 */
rds_ib_set_protocol(conn, RDS_PROTOCOL_4_1);
@@ -1007,6 +1030,10 @@ int rds_ib_conn_path_connect(struct rds_conn_path *cp)
rdsdebug("rdma_create_id() failed: %d\n", ret);
goto out;
}
+ /* The id owns a reference on the connection it points at, put
+ * after rdma_destroy_id() wherever the id is destroyed.
+ */
+ rds_conn_get(conn);
/* rds_ib_laddr_check() only vouched for the local address being
* on an IB device; the address resolution below picks the device
@@ -1017,6 +1044,7 @@ int rds_ib_conn_path_connect(struct rds_conn_path *cp)
rdsdebug("rdma_restrict_node_type() failed: %d\n", ret);
rdma_destroy_id(ic->i_cm_id);
ic->i_cm_id = NULL;
+ rds_conn_put(conn);
goto out;
}
@@ -1058,6 +1086,7 @@ int rds_ib_conn_path_connect(struct rds_conn_path *cp)
ret);
rdma_destroy_id(ic->i_cm_id);
ic->i_cm_id = NULL;
+ rds_conn_put(conn);
}
out:
@@ -1188,6 +1217,10 @@ void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
rds_ib_recv_clear_ring(ic);
rdma_destroy_id(ic->i_cm_id);
+ /* The id's reference on the connection. Never the last
+ * one: whoever runs this shutdown holds its own.
+ */
+ rds_conn_put(conn);
/*
* Move connection back to the nodev list.
diff --git a/net/rds/rdma_transport.c b/net/rds/rdma_transport.c
index 75532734c054..5d7d41c2db29 100644
--- a/net/rds/rdma_transport.c
+++ b/net/rds/rdma_transport.c
@@ -60,6 +60,13 @@ static int rds_rdma_cm_event_handler_cmn(struct rdma_cm_id *cm_id,
rdsdebug("conn %p id %p handling event %u (%s)\n", conn, cm_id,
event->event, rdma_event_msg(event->event));
+ /* cm_id->context owns a reference on the connection, dropped
+ * only after rdma_destroy_id() has returned, and that waits for
+ * a running handler. The connection, and the mutex released at
+ * out: that lives in its path array, therefore outlive this
+ * handler whatever the callbacks below do to the other holders.
+ */
+
/* Prevent shutdown from tearing down the connection
* while we're executing. */
if (conn) {
--
2.50.1
Technical Details
Message-ID:
<20261113044355.2932029-12-achender@kernel.org>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-362422-linux-kernel-netdev=dewy.ruReferences:
<20261113044355.2932029-1-achender@kernel.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-362422-linux-kernel-netdev=dewy.ru From: achender@kernel.org (Allison Henderson) Newsgroups: linux.kernel.netdev Subject: [PATCH net-next v12 11/12] net/rds: give cm_id->context a reference on its connection Date: Thu, 12 Nov 2026 21:43:54 -0700 Organization: Usenet Network Message-ID: <20261113044355.2932029-12-achender@kernel.org> References: <20261113044355.2932029-1-achender@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1340383"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:yCdSwRsA+rhkk76TpjGqKZ9M2vo= sha256:1HjhGi5bAu3VSllycHM32/059bpA1R5LNAc42WbL9R4= sha1:RDIkYwen3CuvAg/xWSSdXs0kXs4= sha256:htMTa66gLU3AEnAMW6RRmIdndWCMnU9kjGoe9cVcgB4= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 4363611452A; Fri, 13 Nov 2026 15:41:01 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.401 X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id mAw64PpnBjAp; Fri, 13 Nov 2026 15:40:57 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-362422-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4]) by mail.dewy.ru (Postfix) with ESMTPS id 228D0114512; Fri, 13 Nov 2026 15:40:47 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sin.lore.kernel.org (Postfix) with ESMTP id 0BE83102B70; Fri, 13 Nov 2026 04:45:10 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1794545043; b=AflrHu5e18AuiBThQOgxkwH/ffHqeOoFRT9AHRGe3w0E7OJCQyyUsaliwXGZhHnuREu7 X/ukYaaZGDrRtyh14cizKa2zQza3sRwtPp21CcRCeaE1KyNkbqODgrHHcg+azrI4XZ3Bb qHYuEKIgdqTmzUhkvmS/dTm/G+rPUWBgElDECN59MssdJTd25pjTKMkj92YLjVEsCYxVN X5/dXf+9K0GhRoCIGjLrh3I9IukQX8JpUz8h6KOty+bxxryUE7gVG2d5xeOCT/H1a3nBg G9cW7RXlzsnypXyQzaGdxRkvRdcvqXnFZX0itgc8bL3Q8J6OGwlsvHah5Xu7OtiPEHg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794545043; h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:X-Mailing-List: List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe: List-Owner:Precedence:MIME-Version:Content-Transfer-Encoding; bh=7Di1L7BSWw1TBFRpaQGa6rpVzqNErV/jhh7XpUXXTvU=; b=V5iME6LN5hcZzA4nCIPg4ebWCZ1j++jgXljxcXHEftiydxcVZBpBrEWJDXl7v9+4W52Y P5GEstEf8yeQYf2HLF8eLkr3nfBbGsh2G21KPTQGBicGHPeoJRM6BZkI5cIo0zCGU22RA Yc+DVbPfc/Af/8woLJmQpShp8jJQXW6YwPkSMyb+iJx/ywHGeo0ztB6G3b4GPU4IlPdlT hNThmccCMeTNELbzjIN99ecLzdUZ75tHYapwaogegebScpiYGaqeRC5Fa8iAR4ut8eQdh M/0obTitgu6NpWtWC3vgh7pH/Xv+pl4FfBpSu9MzPvmbuWOlx4/LmPXQrn/0JvFwBXA== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=HGy3r64j; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EAE8E60A380B; Fri, 13 Nov 2026 04:44:02 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=HGy3r64j X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794545042; b=PuC+Jxkm7FjFsM6oUjkVXhUNZh3czC5Y0Az30mWWJG4OfawYjUiTWYPaw1Ias5Akuhqa t6j22brKA+6WLmnYJMRbc0Ud+9b4imKtd43vdDrB9p4yLcT7c64uqUuTvkcqaXijwWuzL s8KUX3GmbLZhzA3tErKBYwbwig/eZFbFKq2DJYKT4g46aO9zIWUAjmWT19X6soxvMadDl sd7/e7WV4mYZSPkvJ8XEhfpfblo/3P4SsjaQdVlGDqVw4zHYLAR/95dncRw0wDBfNrdRO SoHLbCb44jnOzmhmH5jjfdED0bgn1rlCCiQbKBLYYEPtenR6J1gFyxXrI224/2TJUIQ== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794545042; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=7Di1L7BSWw1TBFRpaQGa6rpVzqNErV/jhh7XpUXXTvU=; b=gb5whOfbtkQFFXKdaq8KfPRZtbhtTecBjeep2j8ciHQnzYdQi/EmvdF/VIoTfX+rDGCD SnheJajheMkSIE8OMeXtaXsuQ0DQwBiv2zgbmKDS+g1r7TyvHpEw3vyFeiAQfSQSfFTaJ Q2I4dKI9L3Q/bagP/P4MJJG+fttS4anRDJ3h2gggY8SvunE+e+UjU3nD+s2VNLFr0UGfR 2oBo9bq/Au3hen41zY8BK4Zl49JWl5DXPbvX9F4gGKx7raZkD2f3vzTQBvsGyEciciEpA CN5NjulOeagKsg6RDhqV8Ckm9hzNl2WpYWvjpesvAAdGFM+sg9uYU+goRmhaAJ/8kZQ== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=HGy3r64j; arc=none smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15AE460A2626; Fri, 13 Nov 2026 04:44:02 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 931231F000C7; Fri, 13 Nov 2026 04:44:01 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794545041; bh=7Di1L7BSWw1TBFRpaQGa6rpVzqNErV/jhh7XpUXXTvU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=HGy3r64jolpzA3KWE4b3wLL0zN4nKigLtFUCio8dw6C58cE43njddRdWAP35AZHde ivDOESdvXIuFl9Ap4E+jXeI00tUgfHxrKDiDS+1W6uGvCUQOwWLacXVAk3n8xtyOdT Ca8fNezmkhCZGraub6xYraTG7ERY2lOOriOveQmE0+Mx6mZBOQ9vb4SZJ/KROCptb+ hw2aRacxIdVaj8XdSbyeuKrh+sGA6uIJfKFp4MaL6JDgexDtdPNpFUvIZ7CLrLfBrp sg/90ca8ake9wGIMMO9R7/gt3MdIMzIIfFazQOdXLyy9D+zHuw3xNYoXMixqDiX0AO ENkfzpx7G91xQ== X-Cc: achender@kernel.org X-Mailer: git-send-email 2.50.1 X-In-Reply-To: <20261113044355.2932029-1-achender@kernel.org> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list