All fragments of a PDU are built into the same smbdirect_send_batch by smbdirect_connection_send_single_iter(). If building a fragment failed, e.g. because a credit wait was interrupted by a fatal signal or a DMA mapping failed, smbdirect_connection_send_iter() and the smbd_send() of the client still flushed the batch with is_last=true. That posts the fragments built so far, the last of them with a remaining_data_length that is never satisfied, fragments may also have been posted already by a flush with is_last=false. The peer then appends the next PDU to the incomplete one: cifs reconnects asynchronously, ksmbd keeps the connection and its next response continues the truncated one. Add smbdirect_connection_send_batch_abort() and use it instead of the final flush after an error: if nothing of the PDU was built yet, it only gives back the batch credit, otherwise it frees the messages not posted yet and tears down the connection, as the peer can no longer get a consistent stream. smbdirect_connection_send_wait_zero_pending() now also waits for all posted messages if the connection is broken, before it returned as soon as the status changed, which happens right away in smbdirect_connection_send_batch_abort(), while the device could still read the pages of the caller for fragments posted by a flush with is_last=false. They complete with IB_WC_WR_FLUSH_ERR once the teardown moved the QP to the error state. smbdirect_connection_send_single_iter() gives back the batch credit itself on its error paths, without waking up the waiters, so smbdirect_connection_send_batch_abort() doesn't see it, that is fixed by "smb: smbdirect: wake up the credit waiters after a failed send". Fixes: 2c1ac39ce9cd ("smb: client: use smbdirect_send_batch processing") Fixes: 54abc694ebb8 ("smb: smbdirect: introduce smbdirect_connection_send_iter() and related functions") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/client/smbdirect.c | 14 +++++- fs/smb/smbdirect/connection.c | 81 ++++++++++++++++++++++++++++++++--- fs/smb/smbdirect/socket.h | 7 +++ include/linux/smbdirect.h | 3 ++ 4 files changed, 98 insertions(+), 7 deletions(-) diff --git a/fs/smb/client/smbdirect.c b/fs/smb/client/smbdirect.c index 563ef488a225..64d27d00079d 100644 --- a/fs/smb/client/smbdirect.c +++ b/fs/smb/client/smbdirect.c @@ -485,9 +485,19 @@ int smbd_send(struct TCP_Server_Info *server, } while (++rqst_idx < num_rqst); - rc = smbdirect_connection_send_batch_flush(sc, batch, true); - if (unlikely(!rc && error)) + if (unlikely(error)) { + /* + * Don't post a partial PDU, if some of it was + * already built, this tears down the connection. + * Fragments may have been posted already, so we + * still wait for them below, they may use the + * pages of the request. + */ + smbdirect_connection_send_batch_abort(sc, batch); rc = error; + } else { + rc = smbdirect_connection_send_batch_flush(sc, batch, true); + } /* * As an optimization, we don't wait for individual I/O to finish diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c index c50096e605ee..9a824f4f76c5 100644 --- a/fs/smb/smbdirect/connection.c +++ b/fs/smb/smbdirect/connection.c @@ -925,6 +925,7 @@ static void smbdirect_connection_send_batch_init(struct smbdirect_send_batch *ba batch->need_invalidate_rkey = need_invalidate_rkey; batch->remote_key = remote_key; batch->credit = 0; + batch->msg_cnt = 0; } int smbdirect_connection_send_batch_flush(struct smbdirect_socket *sc, @@ -988,6 +989,58 @@ int smbdirect_connection_send_batch_flush(struct smbdirect_socket *sc, } EXPORT_SYMBOL_GPL(smbdirect_connection_send_batch_flush); +/* + * Abort the PDU of a batch after an error, instead of flushing it with + * is_last=true. + * + * All fragments of a PDU use the same batch. If some of them were + * already added to the batch, possibly posted by a flush with + * is_last=false, the peer can no longer get a complete PDU, its + * remaining_data_length would just continue with the next PDU. So the + * messages not posted yet are freed and the connection is torn down. + * + * If nothing of the PDU was built yet, e.g. the wait for the first + * credit failed, only the batch credit is given back. + */ +void smbdirect_connection_send_batch_abort(struct smbdirect_socket *sc, + struct smbdirect_send_batch *batch) +{ + struct smbdirect_send_io *msg, *next; + u32 freed = 0; + + list_for_each_entry_safe(msg, next, &batch->msg_list, sibling_list) { + list_del_init(&msg->sibling_list); + smbdirect_connection_free_send_io(msg); + freed++; + } + batch->wr_cnt = 0; + + /* + * Each message took a local and a send credit, + * see smbdirect_connection_send_single_iter(). + */ + if (freed) { + atomic_add(freed, &sc->send_io.credits.count); + wake_up(&sc->send_io.credits.wait_queue); + atomic_add(freed, &sc->send_io.lcredits.count); + wake_up(&sc->send_io.lcredits.wait_queue); + } + + if (batch->credit) { + atomic_add(batch->credit, &sc->send_io.bcredits.count); + batch->credit = 0; + wake_up(&sc->send_io.bcredits.wait_queue); + } + + if (batch->msg_cnt) { + smbdirect_log_write(sc, SMBDIRECT_LOG_ERR, + "aborting a partial PDU after %u message(s)\n", + batch->msg_cnt); + smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED); + } +} +EXPORT_SYMBOL_GPL(smbdirect_connection_send_batch_abort); + struct smbdirect_send_batch * smbdirect_init_send_batch_storage(struct smbdirect_send_batch_storage *storage, bool need_invalidate_rkey, @@ -1097,6 +1150,7 @@ static int smbdirect_connection_post_send_io(struct smbdirect_socket *sc, } list_add_tail(&msg->sibling_list, &batch->msg_list); batch->wr_cnt++; + batch->msg_cnt++; return 0; } @@ -1303,9 +1357,15 @@ int smbdirect_connection_send_wait_zero_pending(struct smbdirect_socket *sc) * that means all the I/Os have been out and we are good to return */ + /* + * Also wait if the connection is broken: the device may still + * read the pages of the caller for posted messages, e.g. earlier + * fragments of a PDU that smbdirect_connection_send_batch_abort() + * gave up. Once the teardown moved the QP to the error state, + * they complete with IB_WC_WR_FLUSH_ERR. + */ wait_event(sc->send_io.pending.zero_wait_queue, - atomic_read(&sc->send_io.pending.count) == 0 || - sc->status != SMBDIRECT_SOCKET_CONNECTED); + atomic_read(&sc->send_io.pending.count) == 0); if (sc->status != SMBDIRECT_SOCKET_CONNECTED) { smbdirect_log_write(sc, SMBDIRECT_LOG_ERR, "status=%s first_error=%1pe => %1pe\n", @@ -1382,9 +1442,20 @@ int smbdirect_connection_send_iter(struct smbdirect_socket *sc, } } - ret = smbdirect_connection_send_batch_flush(sc, &batch, true); - if (unlikely(ret && !error)) - error = ret; + if (unlikely(error)) { + /* + * Don't post a partial PDU, see + * smbdirect_connection_send_batch_abort(). + * Fragments may have been posted already, so we + * still wait for them below, they may use the + * pages of the caller. + */ + smbdirect_connection_send_batch_abort(sc, &batch); + } else { + ret = smbdirect_connection_send_batch_flush(sc, &batch, true); + if (unlikely(ret)) + error = ret; + } /* * As an optimization, we don't wait for individual I/O to finish diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h index 14114a2567a2..dc1e906316ff 100644 --- a/fs/smb/smbdirect/socket.h +++ b/fs/smb/smbdirect/socket.h @@ -783,6 +783,13 @@ struct smbdirect_send_batch { u32 remote_key; int credit; + + /* + * Number of messages added to the batch, + * unlike wr_cnt not reset by a flush, see + * smbdirect_connection_send_batch_abort(). + */ + u32 msg_cnt; }; struct smbdirect_recv_io { diff --git a/include/linux/smbdirect.h b/include/linux/smbdirect.h index 97f5ba730fa7..74fff00cdf88 100644 --- a/include/linux/smbdirect.h +++ b/include/linux/smbdirect.h @@ -115,6 +115,9 @@ int smbdirect_connection_send_batch_flush(struct smbdirect_socket *sc, struct smbdirect_send_batch *batch, bool is_last); +void smbdirect_connection_send_batch_abort(struct smbdirect_socket *sc, + struct smbdirect_send_batch *batch); + /* * This is only temporary and only needed * as long as the client still requires -- 2.43.0