syzbot reported this warning: expected[RESOLVE_ADDR_RUNNING] != DISCONNECTED first_error=-ERESTARTSYS local=127.0.0.1:51036 remote=127.0.0.1:445 WARNING: fs/smb/smbdirect/connect.c:307 at smbdirect_connect_rdma_event_handler+0x1e02/0x2820 fs/smb/smbdirect/connect.c:307, CPU#1: kworker/u8:7/198 Workqueue: rdma_cm cma_work_handler Call Trace: cma_cm_event_handler+0xac/0x2c0 drivers/infiniband/core/cma.c:2181 cma_work_handler+0x265/0x520 drivers/infiniband/core/cma.c:3040 The waiter in smbdirect_connection_wait_for_connected() was interrupted by a signal, so it called smbdirect_socket_schedule_cleanup(), which sets first_error and changes the status, without the handler lock of the rdma cm id, while smbdirect_connect_rdma_event_handler() was running. SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}() check first_error before the status, but there was nothing that made sure the first_error written before the status is also seen before it, the compiler could even reuse the value of the first_error check at the start of the event handler. So the handler saw first_error == 0 and the status changed by the teardown, which is not unexpected. Now __smbdirect_socket_schedule_cleanup() and, if it sets first_error itself, smbdirect_socket_cleanup_work() write first_error with WRITE_ONCE() and order it before the status changes with smp_wmb() and __SMBDIRECT_CHECK_STATUS_FAILED() reads both with READ_ONCE() and checks first_error again, after smp_rmb(), if the status is not the expected one, so such a status change is handled like an error without a warning. Fixes: 1adb2dab9727 ("smb: smbdirect: introduce SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}()") Fixes: d1f187656797 ("smb: smbdirect: introduce smbdirect_connect[_sync]()") 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: syzbot+a989758ffc6f93223eb6@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=a989758ffc6f93223eb6 Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/socket.c | 21 ++++++++++++++++++--- fs/smb/smbdirect/socket.h | 18 +++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c index cb090c558321..53be81077adb 100644 --- a/fs/smb/smbdirect/socket.c +++ b/fs/smb/smbdirect/socket.c @@ -280,9 +280,9 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, spin_lock_irqsave(&sc->cleanup_lock, flags); if (!sc->first_error) { if (error) - sc->first_error = error; + WRITE_ONCE(sc->first_error, error); else - sc->first_error = -ECONNABORTED; + WRITE_ONCE(sc->first_error, -ECONNABORTED); was_first = true; /* @@ -298,6 +298,14 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, } spin_unlock_irqrestore(&sc->cleanup_lock, flags); + /* + * Callers of SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}() + * may run concurrently without our locks, they need + * to see first_error before the status changes below, + * see __SMBDIRECT_CHECK_STATUS_FAILED(). + */ + smp_wmb(); + if (was_first) ___smbdirect_log_generic(sc, func, line, lvl, @@ -428,7 +436,7 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) */ spin_lock_irqsave(&sc->cleanup_lock, flags); if (!sc->first_error) { - sc->first_error = -ECONNABORTED; + WRITE_ONCE(sc->first_error, -ECONNABORTED); was_first = true; disable_work(&sc->connect.work); @@ -439,6 +447,13 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) } spin_unlock_irqrestore(&sc->cleanup_lock, flags); + /* + * Like in __smbdirect_socket_schedule_cleanup(), + * first_error needs to be seen before the status + * changes below. + */ + smp_wmb(); + if (was_first) smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, "%s called with first_error==0\n", diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h index 1520b6e48e7d..c669778fac4b 100644 --- a/fs/smb/smbdirect/socket.h +++ b/fs/smb/smbdirect/socket.h @@ -692,14 +692,26 @@ static __always_inline void smbdirect_socket_init(struct smbdirect_socket *sc) sc->logging.vaprintf = __smbdirect_log_vaprintf; } +/* + * A concurrent __smbdirect_socket_schedule_cleanup() or + * smbdirect_socket_cleanup_work() sets first_error before it changes + * the status, so if the status is not the expected one, it may only + * have changed because of that, which is not unexpected. That's why + * first_error is checked again after smp_rmb(). + */ #define __SMBDIRECT_CHECK_STATUS_FAILED(__sc, __expected_status, __error_cmd, __unexpected_cmd) ({ \ bool __failed = false; \ - if (unlikely((__sc)->first_error)) { \ + if (unlikely(READ_ONCE((__sc)->first_error))) { \ __failed = true; \ __error_cmd \ - } else if (unlikely((__sc)->status != (__expected_status))) { \ + } else if (unlikely(READ_ONCE((__sc)->status) != (__expected_status))) { \ __failed = true; \ - __unexpected_cmd \ + smp_rmb(); /* pairs with smp_wmb() in the cleanup functions */ \ + if (READ_ONCE((__sc)->first_error)) { \ + __error_cmd \ + } else { \ + __unexpected_cmd \ + } \ } \ __failed; \ }) -- 2.43.0