The connect and accept code and their rdma cm event handlers check the status with SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}() or WARN_ON_ONCE() and then set the status of the next step, but a concurrent teardown can run in between, e.g. when the waiter of a connect was interrupted by a signal or the listener of an accepted socket is shut down: CPU0 CPU1 CHECK_STATUS_DISCONNECT() __smbdirect_socket_schedule_cleanup() first_error = ... status = ..._FAILED smbdirect_socket_cleanup_work() status = DISCONNECTED status = NEGOTIATE_NEEDED The teardown status is overwritten with a status of the connection setup. smbdirect_socket_cleanup_work() then no longer calls rdma_disconnect() for a connection that was established and smbdirect_socket_destroy() warns about a status below SMBDIRECT_SOCKET_DISCONNECTING. smbdirect_accept_connect_request() even calls rdma_accept() for a socket whose teardown already started. The teardown sets first_error under sc->cleanup_lock before it changes the status. Add smbdirect_socket_set_status(), which only moves the status to the next step under the same lock while first_error is still 0, and use it for all status changes of the connection setup. If the status is not the expected one without a teardown, that's an internal error, it warns and starts the teardown itself, so that first_error is set whenever it returns false. Either the teardown then sees the new status and handles it, or the setup sees first_error and gives up as if the status check had failed. Fixes: 1adb2dab9727 ("smb: smbdirect: introduce SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}()") Fixes: dc691b91ad16 ("smb: smbdirect: introduce smbdirect_socket_{listen,accept}()") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-a45eaa5a-b564-4557-bd53-2c0b037a6acb Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/accept.c | 24 ++++++++++++------- fs/smb/smbdirect/connect.c | 32 +++++++++++++++++++------- fs/smb/smbdirect/connection.c | 8 +++---- fs/smb/smbdirect/internal.h | 4 ++++ fs/smb/smbdirect/listen.c | 6 ++--- fs/smb/smbdirect/socket.c | 43 +++++++++++++++++++++++++++++++++++ 6 files changed, 93 insertions(+), 24 deletions(-) diff --git a/fs/smb/smbdirect/accept.c b/fs/smb/smbdirect/accept.c index 5a33fcde3653..4b85e78bc885 100644 --- a/fs/smb/smbdirect/accept.c +++ b/fs/smb/smbdirect/accept.c @@ -114,9 +114,11 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc, */ recv_io = NULL; - /* already checked with SMBDIRECT_CHECK_STATUS_WARN above */ - WARN_ON_ONCE(sc->status != SMBDIRECT_SOCKET_CREATED); - sc->status = SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_CREATED, + SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED)) { + ret = sc->first_error; + goto cleanup; + } /* * We already negotiated sp->initiator_depth @@ -139,9 +141,11 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc, conn_param.rnr_retry_count = SMBDIRECT_RDMA_CM_RNR_RETRY; conn_param.flow_control = 0; - /* explicitly set above */ - WARN_ON_ONCE(sc->status != SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED); - sc->status = SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED, + SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING)) { + ret = sc->first_error; + goto cleanup; + } ret = rdma_accept(sc->rdma.cm_id, &conn_param); if (ret) { smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, @@ -371,7 +375,9 @@ static void smbdirect_accept_negotiate_recv_work(struct work_struct *work) if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_NEGOTIATE_NEEDED)) return; - sc->status = SMBDIRECT_SOCKET_NEGOTIATE_RUNNING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_NEGOTIATE_NEEDED, + SMBDIRECT_SOCKET_NEGOTIATE_RUNNING)) + return; /* * Note recv_io is already part of the free list, @@ -816,7 +822,9 @@ static int smbdirect_accept_rdma_event_handler(struct rdma_cm_id *id, */ if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING)) return 0; - sc->status = SMBDIRECT_SOCKET_NEGOTIATE_NEEDED; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING, + SMBDIRECT_SOCKET_NEGOTIATE_NEEDED)) + return 0; spin_lock_irqsave(&sc->cleanup_lock, flags); if (!sc->first_error) queue_work(sc->workqueues.accept, &sc->connect.work); diff --git a/fs/smb/smbdirect/connect.c b/fs/smb/smbdirect/connect.c index fc834f8960fa..6c5504c0b0fc 100644 --- a/fs/smb/smbdirect/connect.c +++ b/fs/smb/smbdirect/connect.c @@ -70,7 +70,9 @@ static int smbdirect_connect_setup_connection(struct smbdirect_socket *sc) if (SMBDIRECT_CHECK_STATUS_WARN(sc, SMBDIRECT_SOCKET_CREATED)) return -EINVAL; - sc->status = SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_CREATED, + SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED)) + return sc->first_error; return 0; } @@ -91,7 +93,9 @@ static int smbdirect_connect_resolve_addr(struct smbdirect_socket *sc, if (SMBDIRECT_CHECK_STATUS_WARN(sc, SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED)) return -EINVAL; - sc->status = SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED, + SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING)) + return sc->first_error; sc->rdma.expected_event = RDMA_CM_EVENT_ADDR_RESOLVED; ret = rdma_resolve_addr(sc->rdma.cm_id, src_addr, dst_addr, sp->resolve_addr_timeout_msec); @@ -112,7 +116,9 @@ static int smbdirect_connect_resolve_route(struct smbdirect_socket *sc) if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED)) return sc->first_error; - sc->status = SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED, + SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING)) + return sc->first_error; sc->rdma.expected_event = RDMA_CM_EVENT_ROUTE_RESOLVED; ret = rdma_resolve_route(sc->rdma.cm_id, sp->resolve_route_timeout_msec); if (ret) { @@ -219,7 +225,9 @@ static int smbdirect_connect_rdma_connect(struct smbdirect_socket *sc) if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED)) return sc->first_error; - sc->status = SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED, + SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING)) + return sc->first_error; sc->rdma.expected_event = RDMA_CM_EVENT_ESTABLISHED; ret = rdma_connect_locked(sc->rdma.cm_id, &conn_param); if (ret) { @@ -313,7 +321,9 @@ static int smbdirect_connect_rdma_event_handler(struct rdma_cm_id *id, case RDMA_CM_EVENT_ADDR_RESOLVED: if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING)) return 0; - sc->status = SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING, + SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED)) + return 0; ret = smbdirect_connect_resolve_route(sc); if (ret) @@ -323,7 +333,9 @@ static int smbdirect_connect_rdma_event_handler(struct rdma_cm_id *id, case RDMA_CM_EVENT_ROUTE_RESOLVED: if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING)) return 0; - sc->status = SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING, + SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED)) + return 0; ret = smbdirect_connect_rdma_connect(sc); if (ret) @@ -335,7 +347,9 @@ static int smbdirect_connect_rdma_event_handler(struct rdma_cm_id *id, if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING)) return 0; - sc->status = SMBDIRECT_SOCKET_NEGOTIATE_NEEDED; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING, + SMBDIRECT_SOCKET_NEGOTIATE_NEEDED)) + return 0; /* * Here we work around an inconsistency between @@ -392,7 +406,9 @@ static int smbdirect_connect_negotiate_start(struct smbdirect_socket *sc) if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_NEGOTIATE_NEEDED)) return sc->first_error; - sc->status = SMBDIRECT_SOCKET_NEGOTIATE_RUNNING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_NEGOTIATE_NEEDED, + SMBDIRECT_SOCKET_NEGOTIATE_RUNNING)) + return sc->first_error; ret = smbdirect_connection_create_mem_pools(sc); if (ret) { diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c index 40b45759ebb3..832aef84dfc9 100644 --- a/fs/smb/smbdirect/connection.c +++ b/fs/smb/smbdirect/connection.c @@ -184,11 +184,9 @@ void smbdirect_connection_negotiation_done(struct smbdirect_socket *sc) /* * We are done, so we can wake up the waiter. */ - WARN_ONCE(sc->status == SMBDIRECT_SOCKET_CONNECTED, - "status=%s first_error=%1pe", - smbdirect_socket_status_string(sc->status), - SMBDIRECT_DEBUG_ERR_PTR(sc->first_error)); - sc->status = SMBDIRECT_SOCKET_CONNECTED; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_NEGOTIATE_RUNNING, + SMBDIRECT_SOCKET_CONNECTED)) + return; done: smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, diff --git a/fs/smb/smbdirect/internal.h b/fs/smb/smbdirect/internal.h index 3215f76302f8..ecaf95f629b2 100644 --- a/fs/smb/smbdirect/internal.h +++ b/fs/smb/smbdirect/internal.h @@ -57,6 +57,10 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, unsigned int line, int error, enum smbdirect_socket_status *force_status); +bool smbdirect_socket_set_status(struct smbdirect_socket *sc, + enum smbdirect_socket_status old_status, + enum smbdirect_socket_status new_status); + #define smbdirect_socket_schedule_cleanup(__sc, __error) \ __smbdirect_socket_schedule_cleanup(__sc, \ "smbdirect_socket_schedule_cleanup", SMBDIRECT_LOG_ERR, \ diff --git a/fs/smb/smbdirect/listen.c b/fs/smb/smbdirect/listen.c index 2c4a2f249f45..56929202041f 100644 --- a/fs/smb/smbdirect/listen.c +++ b/fs/smb/smbdirect/listen.c @@ -111,9 +111,9 @@ int smbdirect_socket_listen(struct smbdirect_socket *sc, int backlog) "try to listen on addr: %pISpsfc\n", &sc->rdma.cm_id->route.addr.src_addr); - /* already checked above */ - WARN_ON_ONCE(sc->status != SMBDIRECT_SOCKET_CREATED); - sc->status = SMBDIRECT_SOCKET_LISTENING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_CREATED, + SMBDIRECT_SOCKET_LISTENING)) + return sc->first_error; sc->rdma.expected_event = RDMA_CM_EVENT_CONNECT_REQUEST; rdma_lock_handler(sc->rdma.cm_id); sc->rdma.cm_id->event_handler = smbdirect_listen_rdma_event_handler; diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c index a1c0572a79ca..0497a3483e66 100644 --- a/fs/smb/smbdirect/socket.c +++ b/fs/smb/smbdirect/socket.c @@ -409,6 +409,49 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, queue_work(sc->workqueues.cleanup, &sc->disconnect_work); } +/* + * Moves sc->status from @old_status to @new_status for the next step + * of the connection setup, unless the teardown already started. The + * teardown sets first_error under cleanup_lock before it changes the + * status, so either it sees @new_status and handles it, or we see + * first_error and leave the status of the teardown alone. + * + * The callers already checked @old_status with + * SMBDIRECT_CHECK_STATUS_*(), only the teardown can change it + * concurrently, anything else is an internal error, then we start + * the teardown ourselves, so that first_error is always set if we + * return false. + */ +bool smbdirect_socket_set_status(struct smbdirect_socket *sc, + enum smbdirect_socket_status old_status, + enum smbdirect_socket_status new_status) +{ + enum smbdirect_socket_status status; + unsigned long flags; + bool unexpected = false; + bool ok = false; + + spin_lock_irqsave(&sc->cleanup_lock, flags); + status = sc->status; + if (!sc->first_error) { + unexpected = status != old_status; + if (likely(!unexpected)) { + WRITE_ONCE(sc->status, new_status); + ok = true; + } + } + spin_unlock_irqrestore(&sc->cleanup_lock, flags); + + if (WARN_ONCE(unexpected, + "status=%s instead of %s before changing to %s\n", + smbdirect_socket_status_string(status), + smbdirect_socket_status_string(old_status), + smbdirect_socket_status_string(new_status))) + smbdirect_socket_schedule_cleanup(sc, -EINVAL); + + return ok; +} + static void smbdirect_socket_cleanup_work(struct work_struct *work) { struct smbdirect_socket *sc = -- 2.43.0