These take and drop a reference on sc->refs.destroy, which only keeps the memory of the socket alive. smbdirect_socket_release() still disconnects and destroys the socket, after that it is no longer connected and all functions fail gracefully. This will be used by the smb client in order to use the socket, e.g. for smbdirect_connection_register_mr_io(), while cifs_abort_connection() may release it at the same time. Fixes: b8aef8c8808c ("smb: client: make use of smbdirect_socket_create_kern()/smbdirect_socket_release()") Cc: stable@vger.kernel.org Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/socket.c | 27 +++++++++++++++++++++++++++ include/linux/smbdirect.h | 3 +++ 2 files changed, 30 insertions(+) diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c index e270e36b3145..13cf50b96bc9 100644 --- a/fs/smb/smbdirect/socket.c +++ b/fs/smb/smbdirect/socket.c @@ -851,6 +851,33 @@ void smbdirect_socket_release(struct smbdirect_socket *sc) } EXPORT_SYMBOL_GPL(smbdirect_socket_release); +/* + * smbdirect_socket_get() and smbdirect_socket_put() only keep + * the memory of the socket alive, they don't prevent + * smbdirect_socket_release() from disconnecting and destroying + * it, after that the socket is no longer connected and + * all functions fail gracefully. + * + * This allows a caller to use the socket, e.g. for + * smbdirect_connection_register_mr_io(), while another + * thread may call smbdirect_socket_release() at the same time. + * + * smbdirect_socket_put() may free the socket, + * so it must be called from process context. + */ +void smbdirect_socket_get(struct smbdirect_socket *sc) +{ + kref_get(&sc->refs.destroy); +} +EXPORT_SYMBOL_GPL(smbdirect_socket_get); + +void smbdirect_socket_put(struct smbdirect_socket *sc) +{ + might_sleep(); + kref_put(&sc->refs.destroy, smbdirect_socket_release_destroy); +} +EXPORT_SYMBOL_GPL(smbdirect_socket_put); + int smbdirect_socket_wait_for_credits(struct smbdirect_socket *sc, enum smbdirect_socket_status expected_status, int unexpected_errno, diff --git a/include/linux/smbdirect.h b/include/linux/smbdirect.h index 97f5ba730fa7..7f0b1ed6cf16 100644 --- a/include/linux/smbdirect.h +++ b/include/linux/smbdirect.h @@ -111,6 +111,9 @@ void smbdirect_socket_shutdown(struct smbdirect_socket *sc); void smbdirect_socket_release(struct smbdirect_socket *sc); +void smbdirect_socket_get(struct smbdirect_socket *sc); +void smbdirect_socket_put(struct smbdirect_socket *sc); + int smbdirect_connection_send_batch_flush(struct smbdirect_socket *sc, struct smbdirect_send_batch *batch, bool is_last); -- 2.43.0