From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.230.129 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796013964; b=3qlPfYxF7pNeLGTb4iYdX9S47N1xIbfsW/T/pIONbEGSVFURo8XACbOomEDCTwewoTEm xc7cJex1eeWic0b8DeenXC090jZUN/TSVb6ggc/ws4/dh4tCCRpf20MRSHVCJncoRallz yL6B1Jha2YjNtrTn4xcDGj3HPcEHrfF8ZFTrR9+xX0S6HeelwJic6480DnKxBHDa1tPOm RTE/xluFYteLQ+EzNBibFDgiHX7H8z5T7rkSrZykcZVFsl21NURPgDaA/7jul/NtbQVhJ uHPuYooxbj3NloeM7GAlZOvkYCvtXGofd8g1ZGBLkM28efJwze1T8ik/UsEOJHpoFcQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796013964; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=8MGzveOsVMRnxZmA+/3o46eiD4VxzrlBuFV7c6c36mg=; b=3DgLJnvfM0oTr+OQv+3Ag77+9DHQSz6AeRoOpjPLfQ7zejAYIRXFjuJrn7Rlzy/IPvwD z7R012gDT4uxSC/fr2bzBfNgy8OQhAz+2i8tzTuV87z0trH8byDQSEULkf1yk4iogYExp bclG8ZGck7olbIpN4KANiJi33fhkb/Kq2ICNFMdvab0TmX3ldJKEV79MTGZBZK8MY9h9T BEWMVs7z5RWpyy1ZECuOtegBf2RPv6yHT/mdfPI2ew9Qf1JaxOtBbvm2pHe0r9OogLSPK AX05OrDa243At9rWZU22gtVpPwsu5NibxxbeS4SbrX+wW0f+Ps/AeALrLu0aUagCfcg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=stellman-greene.com; spf=none smtp.mailfrom=stellman-greene.com; dkim=pass header.d=stellman-greene-com.20260707.gappssmtp.com header.i=@stellman-greene-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=RJWHmAYf; arc=none smtp.remote-ip=74.125.230.129 Received: from mail-qv2-f1.google.com (mail-qv2-f1.google.com [74.125.230.129]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC44B606A31C for ; Mon, 30 Nov 2026 04:46:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BC44B606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=stellman-greene.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=stellman-greene.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=stellman-greene-com.20260707.gappssmtp.com header.i=@stellman-greene-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=RJWHmAYf Received: by mail-qv2-f1.google.com with SMTP id 6a1803df08f44-91b85af9aa7so81135346d6.0 for ; Sun, 29 Nov 2026 20:46:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stellman-greene-com.20260707.gappssmtp.com; s=20260707; t=1796013962; x=1796618762; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=8MGzveOsVMRnxZmA+/3o46eiD4VxzrlBuFV7c6c36mg=; b=RJWHmAYfyny6ZWJAimbAlksaCchlfpcgNY349asjGfbZMArbwcYhJluN5Sa929PYuc qQRWqppcqFSKLsa5wVsOGDgtS44MasOTSXV2dOxVIJS9sExREoNv8mfOHJZcnAoPAwm2 5gmWgwr9RAcWVRbi17Fb5Se0WRdKnkNowd2YnHRK3d3336Rhay1tnzBsWrIb+XnX9KVL qkeXQrZ0+K2r6wqVfZCrGVgjg/0JnjtmZ6STqSnWO5YhX2nbljCgKOlj1d4t2ci+W9v6 q1v0+KyWSRqVgxW2odGer+Oy0YypVmriItgmENkpSFHFBxxVqmjDkjC/YBYa2/m+hh60 kmsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796013962; x=1796618762; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=8MGzveOsVMRnxZmA+/3o46eiD4VxzrlBuFV7c6c36mg=; b=2WjAHorrXWMBBMfJUFefJSnZ7csH5ale/D5FevZIB/6WTbRo/oA0JG7Nq2dVThSR5L WppY1i4GFlQ6DEf7MdniNN/Z0zP9Ndb+WWbzDBHwoJVv73snA0I5yXn8y3IbiyqDYwF1 c2UysgCAvXojL7YCIJMqqrBZhcxZhXXGx45Z1aGvhC4JaH6hG3pkkCqWvUzZVh7SSg5n fBXFoumNnKtXOclxCjDJQdSXGSGyH3hExwMyOZ8vaeyqTViI8GJ0BQWvj2gNK2DAR8J6 om7+ybWiLNFaEvI117o1xVMmimy2E7EBUnw1SGlLybgLq3dB9QzU9HFBgQPr0VGVUAsk enAA== X-Forwarded-Encrypted: i=1; AL+hErEGkBFEWAj4uv/PwuRLcdBMKkaxDMupjZwGvR2/UWt9PZGOMjq/twbK4cDnaoMGH+WdEMin+lE83sEZWu8=@vger.kernel.org X-Gm-Message-State: APuU4Z+NhHFmJI4Fnk5SwUQYcSpA0xEtjs0ODMyDI7mB4dL7QnlAxe0F wVMY53RNbQAbkvMepdFigDSzVQB2zLeP8OfPDeITiN7dqk1lhAaBwjcjfoHaLVy9l30Iq/6SQia LvXj7ljB7vg== X-Gm-Gg: AZFdTZdw8Hn8PF+MO2daMuvCHOeEhk2s8zB9CT+AXEOON42cjXr1veTSOpnA8JPwYW1 zho9ipoBtTLpYelXvL2y770M+7lHjAztBkSfiYPYxUyoxNTrD+7Akqw1gbJ6Sui6Ek7QuzG8n+n UWbXZrVJKyKtKEOy8x6EczWZRqDsIXC+NpozqgtoJzuMyVgqO+23GGCNsIciJKazgAJmkguPoZX ZFKt1Ty6R9j3DpWl7MN8kIsM1U19rspTQpVB4urPFdj8iMUl2tsAfZnQZTNwjU1KokW/L1FHDyC CjAkgAS9i9lYmQBcyKUYhQjQs/mzr+wIUFW4Wc41PgRDOJXm7+HpZAHyTJtNF45JiirDeM1vhZ7 mCbdKHxICmhmDEd5K7oUEtmXKhB0nh0nINgeGs27Og5CMAryZnMrOvPoF+MtXhQLzbMxLy/YJsl EiPTrQLOnq86wz7UAO2JFOJHBpAHq0dpW1inejSVLtcyQBViipsKvH7lJ8AUzaQH6xiznKKijRU p7tML2KftpKnTO6fg1UM6qDAc43tObKq5aEqUFWxKMMuWprFNa8u6DLZSAYzURxob824OXCfWuX e05y6FqBx3TwaSKwIeOZvuQdjm1/Iq9yqMl9qdudS9ApluTQIAUowxKW5i55JQ5mJJEdokhO33p j+dI= X-Received: by 2002:a05:620a:271a:b0:93f:2938:48ba with SMTP id af79cd13be357-949bab48b6cmr6197684085a.55.1796013962501; Sun, 29 Nov 2026 20:46:02 -0800 (PST) Received: from Mac.mynetworksettings.com ([2600:4041:5c28:200:c430:30ff:570a:a90e]) by smtp.gmail.com with ESMTPSA id af79cd13be357-949bb418241sm2420494585a.38.2026.11.29.20.46.01 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 29 Nov 2026 20:46:01 -0800 (PST) From: Andrew Stellman To: Jason Gunthorpe , Leon Romanovsky Cc: Bernard Metzler , linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 rdma-rc] RDMA/iwcm: Handle CLOSE before passive establishment Date: Sun, 29 Nov 2026 23:46:01 -0500 Message-ID: <20261130044601.28226-1-bugs@stellman-greene.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261129135257.80754-1-bugs@stellman-greene.com> References: <20261129135257.80754-1-bugs@stellman-greene.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Andrew Stellman An accepted siw MPA v2 connection enters RDMA mode before receiving the peer's RTR frame. If the peer sends a malformed RTR frame, siw reports CLOSE while the passive IWCM ID is still in CONN_RECV. cm_close_handler() treats that state as impossible and hits BUG(): kernel BUG at drivers/infiniband/core/iwcm.c:966! Internal error: Oops - BUG: 00000000f2000800 [#1] SMP Workqueue: iw_cm_wq cm_work_handler pc : cm_work_handler+0xf40/0x1098 Call trace: cm_work_handler+0xf40/0x1098 (P) process_one_work+0x5f0/0xcb8 worker_thread+0x3ac/0xaa0 kthread+0x36c/0x420 ret_from_fork+0x10/0x20 The fix belongs in the IWCM core rather than siw, because siw can't report this as ESTABLISHED with an error status: cm_conn_est_handler() moves CONN_RECV to ESTABLISHED regardless of status, and the CMA turns it into RDMA_CM_EVENT_ESTABLISHED. Handle CLOSE in CONN_RECV as a failed passive connection. Move the ID to IDLE, clear the connect wait flag, notify the consumer with -ECONNRESET if the provider gave status zero, and wake waiters. The existing exit path drops the QP reference. The consumer now gets a disconnect event for this case instead of the BUG(). If the peer sends its RTR frame and then FIN, siw can queue CLOSE before ESTABLISHED. ESTABLISHED then finds the ID in IDLE, or in DESTROYING if the consumer destroyed the ID after the disconnect event while the ESTABLISHED work item had already passed the IWCM_F_DROP_EVENTS check. Either way it would hit the BUG_ON() in cm_conn_est_handler(), so ignore ESTABLISHED in IDLE and DESTROYING. ESTABLISHED in CONN_RECV is handled as before. Tested in QEMU (arm64) on mainline 7.4.0-rc4 with KASAN. A peer running as uid 1000 with no capabilities that sent a malformed RTR frame hit the BUG() without the patch. With the patch, the server got RDMA_CM_EVENT_DISCONNECTED with status -104 and no BUG(). With the patch, a valid RTR connection got ESTABLISHED then DISCONNECTED, and rping completed three exchanges. The two ESTABLISHED orderings only occurred with test-only delays: 50 ms after siw queues PEER_CLOSE and 100 ms after its CLOSE upcall, 500 ms in IWCM after the drop check, and 100 ms in the consumer before destroy. With the siw delays, a peer that sent a zero-length RTR frame and then shut down its socket hit the BUG_ON() with the first version of this patch. With all four delays, the second version hit it in the DESTROYING case. With this version, the server got RDMA_CM_EVENT_DISCONNECTED with status -104, destroyed the ID, and there was no BUG. Without the delays, one run each of the second and this version got ESTABLISHED before CLOSE. The issue was found by OpenAI GPT-6 (Codex) while reproducing another siw bug. The fix was drafted with OpenAI GPT-6 (Codex) and this changelog with Claude Sonnet 5.5, and both were reviewed by me. Fixes: 6c52fdc244b5 ("rdma/siw: connection management") Signed-off-by: Andrew Stellman --- v3: Also ignore ESTABLISHED when a concurrent destroy has moved the ID from IDLE to DESTROYING. Sashiko reported this race in its review of v2: https://lore.kernel.org/linux-rdma/20261129135257.80754-1-bugs@stellman-greene.com/ The changelog now describes the whole patch. v1: https://lore.kernel.org/linux-rdma/20261129001745.78689-1-bugs@stellman-greene.com/ Tested at mainline ca20d5a011d3 (7.4.0-rc4), except that the unpatched malformed-RTR BUG() and the RTR-then-FIN BUG_ON() with the first version were run at f93eb7cfc4bf (7.4.0-rc4). A reproducer is available on request. drivers/infiniband/core/iwcm.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/drivers/infiniband/core/iwcm.c b/drivers/infiniband/core/iwcm.c index 4b05867dad895..e03e97cd98c8b 100644 --- a/drivers/infiniband/core/iwcm.c +++ b/drivers/infiniband/core/iwcm.c @@ -843,9 +843,10 @@ static void cm_conn_req_handler(struct iwcm_id_private *listen_id_priv, * FPDU mode. * * This event can only be received in the CONN_RECV state. If the - * remote peer closed, the ESTABLISHED event would be received followed - * by the CLOSE event. If the app closes, it will block until we wake - * it up after processing this event. + * remote peer closes after establishment, ESTABLISHED is followed by + * CLOSE. If it closes before establishment, CLOSE can arrive first. + * If the app closes, it will block until we wake it up after processing + * this event. */ static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv, struct iw_cm_event *iw_event) @@ -861,6 +862,12 @@ static int cm_conn_est_handler(struct iwcm_id_private *cm_id_priv, * from a callback handler is not allowed. */ clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags); + if (cm_id_priv->state == IW_CM_STATE_IDLE || + cm_id_priv->state == IW_CM_STATE_DESTROYING) { + /* CLOSE or destroy won the race with passive establishment. */ + spin_unlock_irqrestore(&cm_id_priv->lock, flags); + return 0; + } BUG_ON(cm_id_priv->state != IW_CM_STATE_CONN_RECV); cm_id_priv->state = IW_CM_STATE_ESTABLISHED; spin_unlock_irqrestore(&cm_id_priv->lock, flags); @@ -950,6 +957,8 @@ static int cm_close_handler(struct iwcm_id_private *cm_id_priv, struct ib_qp *qp; unsigned long flags; int ret = 0, notify_event = 0; + bool wake_connect = false; + spin_lock_irqsave(&cm_id_priv->lock, flags); qp = cm_id_priv->qp; cm_id_priv->qp = NULL; @@ -960,6 +969,15 @@ static int cm_close_handler(struct iwcm_id_private *cm_id_priv, cm_id_priv->state = IW_CM_STATE_IDLE; notify_event = 1; break; + case IW_CM_STATE_CONN_RECV: + /* The accepted connection closed before establishment. */ + cm_id_priv->state = IW_CM_STATE_IDLE; + clear_bit(IWCM_F_CONNECT_WAIT, &cm_id_priv->flags); + if (!iw_event->status) + iw_event->status = -ECONNRESET; + notify_event = 1; + wake_connect = true; + break; case IW_CM_STATE_DESTROYING: break; default: @@ -971,6 +989,8 @@ static int cm_close_handler(struct iwcm_id_private *cm_id_priv, cm_id_priv->id.device->ops.iw_rem_ref(qp); if (notify_event) ret = cm_id_priv->id.cm_handler(&cm_id_priv->id, iw_event); + if (wake_connect) + wake_up_all(&cm_id_priv->connect_wait); return ret; } -- 2.53.0