smb: client: protect server->smbd_conn with RCU and refcounting
Subsystems (3)
PATCH v3 1/1 smb: client: protect server->smbd_conn with RCU and refcounting
Several paths check and dereference server->smbd_conn without holding a lock that serializes against smbd_destroy() during reconnect or teardown (including smb2_new_read_req(), smb2_async_writev(), smbd_send(), cifs_readv_from_socket(), cifs_prepare_read/write(), smb3_negotiate_r/wsize(), and smbd_debug_proc_show()). Meanwhile smbd_destroy() releases the underlying socket and calls kfree(info) before clearing server->smbd_conn = NULL, exposing a use-after-free window to concurrent readers.
Annotate server->smbd_conn with __rcu and add a refcount and completion to struct smbd_connection. Readers acquire a short-lived reference via smbd_conn_get() under rcu_read_lock() and drop it with smbd_conn_put(). In smbd_destroy(), clear server->smbd_conn first, shut down the socket to wake any blocked callers, wait for active references and an RCU grace period to complete, and then release the socket and free info in process context. Pass server directly to smbd_recv() and smbd_register_mr() so connection pinning stays encapsulated inside smbdirect.c.
In the cifs_prepare_read/write() hot paths, which only need the negotiated max_frmr_depth value, skip the refcount round trip entirely:
guard on server->rdma and read the parameters under rcu_read_lock(), which is sufficient because smbd_destroy() waits for synchronize_rcu() before releasing the socket and freeing the connection.
Found with an LLM-assisted code review tool and verified by inspection on v7.4-rc4 (b6a9fa34ce4). Build-tested only; no RDMA-capable SMB server was available for runtime testing.
---
v3: cifs_prepare_read/write() no longer bounce conn->refcount on every
I/O subrequest just to read max_frmr_depth; guard on server->rdma and read the parameters under rcu_read_lock() instead, per sashiko's review on v2.
v2: resend via direct SMTP without line-wrapping corruption in the diff.
fs/smb/client/cifsglob.h | 2 +- fs/smb/client/connect.c | 11 ++-- fs/smb/client/file.c | 35 +++++++---- fs/smb/client/smb2ops.c | 72 ++++++++++++---------- fs/smb/client/smb2pdu.c | 6 +- fs/smb/client/smbdirect.c | 111 +++++++++++++++++++++++++---------- fs/smb/client/smbdirect.h | 16 +++-- fs/smb/client/transport.c | 4 +- 8 files changed, 172 insertions(+), 85 deletions(-)
fs/smb/client/cifsglob.h +1 -1
@@ -760,7 +760,7 @@ struct TCP_Server_Info {/* use SMBD connection instead of socket */ bool rdma;/* point to the SMBD connection if RDMA is used instead of socket */ -struct smbd_connection *smbd_conn; +struct smbd_connection __rcu *smbd_conn;struct delayed_work echo;/* echo ping workqueue job */ char *smallbuf;/* pointer to current "small" buffer */ char *bigbuf;/* pointer to current "big" buffer */
fs/smb/client/connect.c +6 -5
@@ -774,8 +774,8 @@ cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)if (server_unresponsive (server))return -ECONNABORTED ; -if (cifs_rdma_enabled (server) && server->smbd_conn) - length =smbd_recv (server->smbd_conn, smb_msg); +if (cifs_rdma_enabled (server)) + length =smbd_recv (server, smb_msg);else length =sock_recvmsg (server->ssocket, smb_msg,0 );
@@ -1888,9 +1888,10 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx, rc = -ENOENT ; goto out_err_crypto_release;#endif - tcp_ses->smbd_conn =smbd_get_connection ( - tcp_ses, (struct sockaddr *)&ctx->dstaddr); -if (tcp_ses->smbd_conn) { +rcu_assign_pointer (tcp_ses->smbd_conn, +smbd_get_connection (tcp_ses, + (struct sockaddr *)&ctx->dstaddr)); +if (rcu_access_pointer (tcp_ses->smbd_conn)) {cifs_dbg (VFS ,"RDMA transport established\n" ); rc =0 ; goto smbd_connected;
fs/smb/client/file.c +25 -10
@@ -391,11 +391,21 @@ static void cifs_prepare_write(struct netfs_io_subrequest *subreq) cifs_trace_rw_credits_write_prepare);#ifdef CONFIG_CIFS_SMB_DIRECT -if (server->smbd_conn) { -const struct smbdirect_socket_parameters *sp = -smbd_get_parameters (server->smbd_conn); - - stream->sreq_max_segs = sp->max_frmr_depth; +if (server->rdma) { +struct smbd_connection *conn; + +/* A plain rcu read is enough here: smbd_destroy() clears +* server->smbd_conn and then waits for synchronize_rcu() +* before releasing the socket and freeing the connection, +* so the parameters memory stays valid for the read-side +* critical section. +*/ +rcu_read_lock (); + conn =rcu_dereference (server->smbd_conn); +if (conn) + stream->sreq_max_segs = +smbd_get_parameters (conn)->max_frmr_depth; +rcu_read_unlock (); }#endif }
@@ -610,11 +620,16 @@ static int cifs_prepare_read(struct netfs_io_subrequest *subreq) cifs_trace_rw_credits_read_submit);#ifdef CONFIG_CIFS_SMB_DIRECT -if (server->smbd_conn) { -const struct smbdirect_socket_parameters *sp = -smbd_get_parameters (server->smbd_conn); - - rreq->io_streams[0 ].sreq_max_segs = sp->max_frmr_depth; +if (server->rdma) { +struct smbd_connection *conn; + +/* See cifs_prepare_write() for why a plain rcu read is safe. */ +rcu_read_lock (); + conn =rcu_dereference (server->smbd_conn); +if (conn) + rreq->io_streams[0 ].sreq_max_segs = +smbd_get_parameters (conn)->max_frmr_depth; +rcu_read_unlock (); }#endif return 0 ;
fs/smb/client/smb2ops.c +40 -32
84 lines not on this page · raw message
fs/smb/client/smb2pdu.c +3 -3
@@ -4651,7 +4651,7 @@ static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms)return false;/* we can only offload on an rdma connection */ -if (!server->rdma || !server->smbd_conn) +if (!server->rdma || !rcu_access_pointer (server->smbd_conn))return false;/* we don't support signed offload yet */
@@ -4721,7 +4721,7 @@ int smb2_new_read_req(void **buf, unsigned int *total_len,struct smbdirect_buffer_descriptor_v1 *v1;bool need_invalidate = server->dialect ==SMB30_PROT_ID ; - rdata->mr =smbd_register_mr (server->smbd_conn, &rdata->subreq.io_iter, + rdata->mr =smbd_register_mr (server, &rdata->subreq.io_iter, true, need_invalidate);if (!rdata->mr) { rc = -EAGAIN ;
@@ -5362,7 +5362,7 @@ smb2_async_writev(struct cifs_io_subrequest *wdata)struct smbdirect_buffer_descriptor_v1 *v1;bool need_invalidate = server->dialect ==SMB30_PROT_ID ; - wdata->mr =smbd_register_mr (server->smbd_conn, &wdata->subreq.io_iter, + wdata->mr =smbd_register_mr (server, &wdata->subreq.io_iter, false, need_invalidate);if (!wdata->mr) { rc = -EAGAIN ;
fs/smb/client/smbdirect.c +83 -28
211 lines not on this page · raw message
fs/smb/client/smbdirect.h +13 -3
@@ -12,6 +12,9 @@#include "cifsglob.h" +#include <linux/completion.h> +#include <linux/rcupdate.h> +#include <linux/refcount.h> #include <linux/smbdirect.h> extern int rdma_readwrite_threshold;
@@ -25,8 +28,13 @@ extern int smbd_receive_credit_max;struct smbd_connection {struct smbdirect_socket *socket; +refcount_t refcount; +struct completion destroy_done; }; +struct smbd_connection *smbd_conn_get (struct TCP_Server_Info *server); +void smbd_conn_put (struct smbd_connection *info); +/* Create a SMBDirect session */ struct smbd_connection *smbd_get_connection (struct TCP_Server_Info *server,struct sockaddr *dstaddr);
@@ -39,13 +47,13 @@ int smbd_reconnect(struct TCP_Server_Info *server);void smbd_destroy (struct TCP_Server_Info *server);/* Interface for carrying upper layer I/O through send/recv */ -int smbd_recv (struct smbd_connection *info,struct msghdr *msg); +int smbd_recv (struct TCP_Server_Info *server,struct msghdr *msg);int smbd_send (struct TCP_Server_Info *server,int num_rqst,struct smb_rqst *rqst);/* Interfaces to register and deregister MR for RDMA read/write */ struct smbdirect_mr_io *smbd_register_mr ( -struct smbd_connection *info,struct iov_iter *iter, +struct TCP_Server_Info *server,struct iov_iter *iter,bool writing,bool need_invalidate);void smbd_mr_fill_buffer_descriptor (struct smbdirect_mr_io *mr,struct smbdirect_buffer_descriptor_v1 *v1);
@@ -60,8 +68,10 @@ static inline void *smbd_get_connection( struct TCP_Server_Info *server, struct sockaddr *dstaddr) {return NULL;}static inline int smbd_reconnect (struct TCP_Server_Info *server) {return -1 ; }static inline void smbd_destroy (struct TCP_Server_Info *server) {} -static inline int smbd_recv (struct smbd_connection *info,struct msghdr *msg) {return -1 ; } +static inline int smbd_recv (struct TCP_Server_Info *server,struct msghdr *msg) {return -1 ; }static inline int smbd_send (struct TCP_Server_Info *server,int num_rqst,struct smb_rqst *rqst) {return -1 ; } +static inline struct smbd_connection *smbd_conn_get (struct TCP_Server_Info *server) {return NULL ; } +static inline void smbd_conn_put (struct smbd_connection *info) {}#endif #endif
fs/smb/client/transport.c +1 -3
@@ -257,9 +257,7 @@ int __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,cifs_in_send_inc (server);if (cifs_rdma_enabled (server)) {/* return -EAGAIN when connecting or reconnecting */ - rc = -EAGAIN ; -if (server->smbd_conn) - rc =smbd_send (server, num_rqst, rqst); + rc =smbd_send (server, num_rqst, rqst); goto smbd_done; }
Signature · 1 line
2.50.1 (Apple Git-155)
Stefan Metzmacher
reviewerHi Qican,
thanks for looking into this! I already have fixes for this (and the related zero-limit/NULL dereference in smb3_negotiate_[rw]size()) in my queue, tested on real RDMA hardware, which avoid waiting for references in smbd_destroy(), which is called with cifs_server_lock() held:
smb: smbdirect: add smbdirect_socket_get() and smbdirect_socket_put()
https://git.samba.org/?p=metze/linux/wip.git;a=commit;h=bea8f6615fc2
smb: client: return the real error from smbd_get_connection()
https://git.samba.org/?p=metze/linux/wip.git;a=commit;h=a4b0de969d81
smb: client: change server->smbd_conn only under server->srv_lock
https://git.samba.org/?p=metze/linux/wip.git;a=commit;h=4b01286ea773
smb: client: pass server to smbd_get_parameters()
https://git.samba.org/?p=metze/linux/wip.git;a=commit;h=da5eb9054218
smb: client: pass server to smbd_register_mr()
https://git.samba.org/?p=metze/linux/wip.git;a=commit;h=eccd137e81d9
smb: client: don't use server->smbd_conn without a reference or lock
https://git.samba.org/?p=metze/linux/wip.git;a=commit;h=8c5a4aa267ee
smb: client: don't use zero RDMA limits without a connection
https://git.samba.org/?p=metze/linux/wip.git;a=commit;h=c7a1d04a3c14
I already tested these on top of v7.3-rc6 and wanted to retest on top of v7.4-rcX before posting them.
I'll post them in the next days, please hold off on this patch until then.
Thanks
metze
Am 26.11.26 um 17:13 schrieb Qican Ma:
Quoted 562 lines
Several paths check and dereference
server->smbd_connwithout holding a lock that serializes againstsmbd_destroy()during reconnect or teardown (includingsmb2_new_read_req(),smb2_async_writev(),smbd_send(),cifs_readv_from_socket(), cifs_prepare_read/write(), smb3_negotiate_r/wsize(), andsmbd_debug_proc_show()). Meanwhilesmbd_destroy()releases the underlying socket and calls kfree(info) before clearingserver->smbd_conn= NULL, exposing a use-after-free window to concurrent readers.Annotate
server->smbd_connwith__rcuand add a refcount and completion to structsmbd_connection. Readers acquire a short-lived reference viasmbd_conn_get()underrcu_read_lock()and drop it withsmbd_conn_put(). Insmbd_destroy(), clearserver->smbd_connfirst, shut down the socket to wake any blocked callers, wait for active references and an RCU grace period to complete, and then release the socket and free info in process context. Pass server directly tosmbd_recv()andsmbd_register_mr()so connection pinning stays encapsulated insidesmbdirect.c.In the cifs_prepare_read/write() hot paths, which only need the negotiated
max_frmr_depthvalue, skip the refcount round trip entirely:
guard onserver->rdmaand read the parameters underrcu_read_lock(), which is sufficient becausesmbd_destroy()waits forsynchronize_rcu()before releasing the socket and freeing the connection.Found with an LLM-assisted code review tool and verified by inspection on v7.4-rc4 (
b6a9fa34ce4). Build-tested only; no RDMA-capable SMB server was available for runtime testing.Fixes f198186aa9bb ("CIFS: SMBD: Establish SMB Direct connection")Signed-off-by Qican Ma <[email protected]>---
v3: cifs_prepare_read/write() no longer bounceconn->refcounton every
I/O subrequest just to readmax_frmr_depth; guard onserver->rdmaand read the parameters underrcu_read_lock()instead, per sashiko's review on v2.
v2: resend via direct SMTP without line-wrapping corruption in the diff.fs/smb/client/cifsglob.h | 2 +- fs/smb/client/connect.c | 11 ++-- fs/smb/client/file.c | 35 +++++++---- fs/smb/client/smb2ops.c | 72 ++++++++++++---------- fs/smb/client/smb2pdu.c | 6 +- fs/smb/client/smbdirect.c | 111 +++++++++++++++++++++++++---------- fs/smb/client/smbdirect.h | 16 +++-- fs/smb/client/transport.c | 4 +- 8 files changed, 172 insertions(+), 85 deletions(-)fs/smb/client/cifsglob.h +1
-1@@ -760,7 +760,7 @@ struct TCP_Server_Info {/* use SMBD connection instead of socket */ bool rdma;/* point to the SMBD connection if RDMA is used instead of socket */ -struct smbd_connection *smbd_conn; +struct smbd_connection __rcu *smbd_conn;struct delayed_work echo;/* echo ping workqueue job */ char *smallbuf;/* pointer to current "small" buffer */ char *bigbuf;/* pointer to current "big" buffer */ fs/smb/client/connect.c +6
-5@@ -774,8 +774,8 @@ cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)if (server_unresponsive (server))return -ECONNABORTED ; -if (cifs_rdma_enabled (server) && server->smbd_conn) - length =smbd_recv (server->smbd_conn, smb_msg); +if (cifs_rdma_enabled (server)) + length =smbd_recv (server, smb_msg);else length =sock_recvmsg (server->ssocket, smb_msg,0 );@@ -1888,9 +1888,10 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx, rc = -ENOENT ; goto out_err_crypto_release;#endif - tcp_ses->smbd_conn =smbd_get_connection ( - tcp_ses, (struct sockaddr *)&ctx->dstaddr); -if (tcp_ses->smbd_conn) { +rcu_assign_pointer (tcp_ses->smbd_conn, +smbd_get_connection (tcp_ses, + (struct sockaddr *)&ctx->dstaddr)); +if (rcu_access_pointer (tcp_ses->smbd_conn)) {cifs_dbg (VFS ,"RDMA transport established\n" ); rc =0 ; goto smbd_connected;fs/smb/client/file.c +25
-10@@ -391,11 +391,21 @@ static void cifs_prepare_write(struct netfs_io_subrequest *subreq) cifs_trace_rw_credits_write_prepare);#ifdef CONFIG_CIFS_SMB_DIRECT -if (server->smbd_conn) { -const struct smbdirect_socket_parameters *sp = -smbd_get_parameters (server->smbd_conn); - - stream->sreq_max_segs = sp->max_frmr_depth; +if (server->rdma) { +struct smbd_connection *conn; + +/* A plain rcu read is enough here: smbd_destroy() clears +* server->smbd_conn and then waits for synchronize_rcu() +* before releasing the socket and freeing the connection, +* so the parameters memory stays valid for the read-side +* critical section. +*/ +rcu_read_lock (); + conn =rcu_dereference (server->smbd_conn); +if (conn) + stream->sreq_max_segs = +smbd_get_parameters (conn)->max_frmr_depth; +rcu_read_unlock (); }#endif }@@ -610,11 +620,16 @@ static int cifs_prepare_read(struct netfs_io_subrequest *subreq) cifs_trace_rw_credits_read_submit);#ifdef CONFIG_CIFS_SMB_DIRECT -if (server->smbd_conn) { -const struct smbdirect_socket_parameters *sp = -smbd_get_parameters (server->smbd_conn); - - rreq->io_streams[0 ].sreq_max_segs = sp->max_frmr_depth; +if (server->rdma) { +struct smbd_connection *conn; + +/* See cifs_prepare_write() for why a plain rcu read is safe. */ +rcu_read_lock (); + conn =rcu_dereference (server->smbd_conn); +if (conn) + rreq->io_streams[0 ].sreq_max_segs = +smbd_get_parameters (conn)->max_frmr_depth; +rcu_read_unlock (); }#endif return 0 ;fs/smb/client/smb2ops.c +40
-32@@ -520,22 +520,26 @@ smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx) wsize = min_t(unsigned int, wsize, server->max_write);#ifdef CONFIG_CIFS_SMB_DIRECT if (server->rdma) { -const struct smbdirect_socket_parameters *sp = -smbd_get_parameters (server->smbd_conn); - -if (server->sign) -/* -* Account for SMB2 data transfer packet header and -* possible encryption header -*/ - wsize = min_t(unsigned int, - wsize, - sp->max_fragmented_send_size - -SMB2_READWRITE_PDU_HEADER_SIZE - -sizeof (struct smb2_transform_hdr )); -else - wsize = min_t(unsigned int, - wsize, sp->max_read_write_size); +struct smbd_connection *conn =smbd_conn_get (server); + +if (conn) { +const struct smbdirect_socket_parameters *sp = +smbd_get_parameters (conn); + +if (server->sign) +/* +* Account for SMB2 data transfer packet header and +* possible encryption header +*/ + wsize = min_t(unsigned int, wsize, + sp->max_fragmented_send_size - +SMB2_READWRITE_PDU_HEADER_SIZE - +sizeof (struct smb2_transform_hdr )); +else + wsize = min_t(unsigned int, wsize, + sp->max_read_write_size); +smbd_conn_put (conn); + } }#endif if (!(server->capabilities &SMB2_GLOBAL_CAP_LARGE_MTU ))@@ -571,22 +575,26 @@ smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx) rsize = min_t(unsigned int, rsize, server->max_read);#ifdef CONFIG_CIFS_SMB_DIRECT if (server->rdma) { -const struct smbdirect_socket_parameters *sp = -smbd_get_parameters (server->smbd_conn); - -if (server->sign) -/* -* Account for SMB2 data transfer packet header and -* possible encryption header -*/ - rsize = min_t(unsigned int, - rsize, - sp->max_fragmented_recv_size - -SMB2_READWRITE_PDU_HEADER_SIZE - -sizeof (struct smb2_transform_hdr )); -else - rsize = min_t(unsigned int, - rsize, sp->max_read_write_size); +struct smbd_connection *conn =smbd_conn_get (server); + +if (conn) { +const struct smbdirect_socket_parameters *sp = +smbd_get_parameters (conn); + +if (server->sign) +/* +* Account for SMB2 data transfer packet header and +* possible encryption header +*/ + rsize = min_t(unsigned int, rsize, + sp->max_fragmented_recv_size - +SMB2_READWRITE_PDU_HEADER_SIZE - +sizeof (struct smb2_transform_hdr )); +else + rsize = min_t(unsigned int, rsize, + sp->max_read_write_size); +smbd_conn_put (conn); + } }#endif fs/smb/client/smb2pdu.c +3
-3@@ -4651,7 +4651,7 @@ static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms)return false;/* we can only offload on an rdma connection */ -if (!server->rdma || !server->smbd_conn) +if (!server->rdma || !rcu_access_pointer (server->smbd_conn))return false;/* we don't support signed offload yet */ @@ -4721,7 +4721,7 @@ int smb2_new_read_req(void **buf, unsigned int *total_len,struct smbdirect_buffer_descriptor_v1 *v1;bool need_invalidate = server->dialect ==SMB30_PROT_ID ; - rdata->mr =smbd_register_mr (server->smbd_conn, &rdata->subreq.io_iter, + rdata->mr =smbd_register_mr (server, &rdata->subreq.io_iter, true, need_invalidate);if (!rdata->mr) { rc = -EAGAIN ;@@ -5362,7 +5362,7 @@ smb2_async_writev(struct cifs_io_subrequest *wdata)struct smbdirect_buffer_descriptor_v1 *v1;bool need_invalidate = server->dialect ==SMB30_PROT_ID ; - wdata->mr =smbd_register_mr (server->smbd_conn, &wdata->subreq.io_iter, + wdata->mr =smbd_register_mr (server, &wdata->subreq.io_iter, false, need_invalidate);if (!wdata->mr) { rc = -EAGAIN ;fs/smb/client/smbdirect.c +83
-28@@ -194,6 +194,25 @@ static int smbd_post_send_full_iter(struct smbdirect_socket *sc,return bytes; } +struct smbd_connection *smbd_conn_get (struct TCP_Server_Info *server) +{ +struct smbd_connection *info; + +rcu_read_lock (); + info =rcu_dereference (server->smbd_conn); +if (info && !refcount_inc_not_zero (&info->refcount)) + info =NULL ; +rcu_read_unlock (); + +return info; +} + +void smbd_conn_put (struct smbd_connection *info) +{ +if (info &&refcount_dec_and_test (&info->refcount)) +complete (&info->destroy_done); +} +/* * Destroy the transport and related RDMA and memory resources * Need to go through all the pending counters and make sure on one is using @@ -201,17 +220,22 @@ static int smbd_post_send_full_iter(struct smbdirect_socket *sc,*/ void smbd_destroy (struct TCP_Server_Info *server) { -struct smbd_connection *info = server->smbd_conn; +struct smbd_connection *info = +rcu_dereference_protected (server->smbd_conn,1 );if (!info) {log_rdma_event (INFO ,"rdma session already destroyed\n" );return ; } -smbdirect_socket_release (info->socket); +RCU_INIT_POINTER (server->smbd_conn,NULL ); +smbdirect_socket_shutdown (info->socket); +if (!refcount_dec_and_test (&info->refcount)) +wait_for_completion (&info->destroy_done); +synchronize_rcu (); +smbdirect_socket_release (info->socket);kfree (info); - server->smbd_conn =NULL ; }/* @@ -222,7 +246,7 @@ int smbd_reconnect(struct TCP_Server_Info *server) {log_rdma_event (INFO ,"reconnecting rdma session\n" ); -if (!server->smbd_conn) { +if (!rcu_access_pointer (server->smbd_conn)) {log_rdma_event (INFO ,"rdma session already destroyed\n" ); goto create_conn; }@@ -236,10 +260,11 @@ int smbd_reconnect(struct TCP_Server_Info *server) create_conn:log_rdma_event (INFO ,"creating rdma session\n" ); - server->smbd_conn =smbd_get_connection ( - server, (struct sockaddr *) &server->dstaddr); +rcu_assign_pointer (server->smbd_conn, +smbd_get_connection (server, + (struct sockaddr *)&server->dstaddr)); -if (server->smbd_conn) { +if (rcu_access_pointer (server->smbd_conn)) {cifs_dbg (VFS ,"RDMA transport re-established\n" );trace_smb3_smbd_connect_done (server->hostname, server->conn_id, &server->dstaddr);return 0 ;@@ -303,6 +328,8 @@ static struct smbd_connection *_smbd_get_connection( info =kzalloc_obj (*info);if (!info)return NULL ; +refcount_set (&info->refcount,1 ); +init_completion (&info->destroy_done); ret =smbdirect_socket_create_kern (net, &sc);if (ret) goto socket_init_failed;@@ -393,14 +420,23 @@ struct smbd_connection *smbd_get_connection(* consumed. But this will require more changes to upper layer code, and also * need to consider packet boundaries while they still being reassembled. */ -int smbd_recv (struct smbd_connection *info,struct msghdr *msg) +int smbd_recv (struct TCP_Server_Info *server,struct msghdr *msg) { -struct smbdirect_socket *sc = info->socket; +struct smbd_connection *info =smbd_conn_get (server); +struct smbdirect_socket *sc; +int rc; -if (!smbdirect_connection_is_connected (sc)) +if (!info)return -ENOTCONN ; -return smbdirect_connection_recvmsg (sc, msg,0 ); + sc = info->socket; +if (!smbdirect_connection_is_connected (sc)) + rc = -ENOTCONN ; +else + rc =smbdirect_connection_recvmsg (sc, msg,0 ); + +smbd_conn_put (info); +return rc; }/* @@ -412,9 +448,9 @@ int smbd_recv(struct smbd_connection *info, struct msghdr *msg)int smbd_send (struct TCP_Server_Info *server,int num_rqst,struct smb_rqst *rqst_array) { -struct smbd_connection *info = server->smbd_conn; -struct smbdirect_socket *sc = info->socket; -const struct smbdirect_socket_parameters *sp =smbd_get_parameters (info); +struct smbd_connection *info =smbd_conn_get (server); +const struct smbdirect_socket_parameters *sp; +struct smbdirect_socket *sc;struct smb_rqst *rqst;struct iov_iter iter;struct smbdirect_send_batch_storage bstorage;@@ -423,9 +459,16 @@ int smbd_send(struct TCP_Server_Info *server,int rc, i, rqst_idx;int error =0 ; -if (!smbdirect_connection_is_connected (sc)) +if (!info)return -EAGAIN ; + sc = info->socket; + sp =smbd_get_parameters (info); +if (!smbdirect_connection_is_connected (sc)) { + error = -EAGAIN ; + goto out; + } +/* * Add in the page array if there is one. The caller needs to set * rq_tailsz to PAGE_SIZE when the buffer has multiple pages and @@ -439,7 +482,8 @@ int smbd_send(struct TCP_Server_Info *server,/* assertion: payload never exceeds negotiated maximum */ log_write (ERR ,"payload size %d > max size %d\n" , remaining_data_length, sp->max_fragmented_send_size); -return -EINVAL ; + error = -EINVAL ; + goto out; } log_write(INFO, "num_rqst=%d total length=%u\n",@@ -501,10 +545,9 @@ int smbd_send(struct TCP_Server_Info *server,if (unlikely (rc && !error)) error = -EAGAIN ; -if (unlikely (error)) -return error; - -return 0 ; +out: +smbd_conn_put (info); +return error; }/* @@ -514,16 +557,24 @@ int smbd_send(struct TCP_Server_Info *server,* need_invalidate: true if this MR needs to be locally invalidated after I/O * return value: the MR registered, NULL if failed. */ -struct smbdirect_mr_io *smbd_register_mr (struct smbd_connection *info, -struct iov_iter *iter, -bool writing,bool need_invalidate) +struct smbdirect_mr_io *smbd_register_mr (struct TCP_Server_Info *server, +struct iov_iter *iter, +bool writing,bool need_invalidate) { -struct smbdirect_socket *sc = info->socket; +struct smbd_connection *info =smbd_conn_get (server); +struct smbdirect_mr_io *mr =NULL ; +struct smbdirect_socket *sc; -if (!smbdirect_connection_is_connected (sc)) +if (!info)return NULL ; -return smbdirect_connection_register_mr_io (sc, iter, writing, need_invalidate); + sc = info->socket; +if (smbdirect_connection_is_connected (sc)) + mr =smbdirect_connection_register_mr_io (sc, iter, writing, + need_invalidate); + +smbd_conn_put (info); +return mr; } void smbd_mr_fill_buffer_descriptor(struct smbdirect_mr_io *mr,@@ -545,17 +596,21 @@ void smbd_deregister_mr(struct smbdirect_mr_io *mr)void smbd_debug_proc_show (struct TCP_Server_Info *server,struct seq_file *m) { +struct smbd_connection *info; +if (!server->rdma)return ; -if (!server->smbd_conn) { + info =smbd_conn_get (server); +if (!info) {seq_puts (m,"\nSMBDirect transport not available" );return ; } -smbdirect_connection_legacy_debug_proc_show (server->smbd_conn->socket, +smbdirect_connection_legacy_debug_proc_show (info->socket, server->rdma_readwrite_threshold, m); +smbd_conn_put (info); }MODULE_IMPORT_NS ("SMBDIRECT" );fs/smb/client/smbdirect.h +13
-3@@ -12,6 +12,9 @@#include "cifsglob.h" +#include <linux/completion.h> +#include <linux/rcupdate.h> +#include <linux/refcount.h> #include <linux/smbdirect.h> extern int rdma_readwrite_threshold;@@ -25,8 +28,13 @@ extern int smbd_receive_credit_max;struct smbd_connection {struct smbdirect_socket *socket; +refcount_t refcount; +struct completion destroy_done; }; +struct smbd_connection *smbd_conn_get (struct TCP_Server_Info *server); +void smbd_conn_put (struct smbd_connection *info); +/* Create a SMBDirect session */ struct smbd_connection *smbd_get_connection (struct TCP_Server_Info *server,struct sockaddr *dstaddr);@@ -39,13 +47,13 @@ int smbd_reconnect(struct TCP_Server_Info *server);void smbd_destroy (struct TCP_Server_Info *server);/* Interface for carrying upper layer I/O through send/recv */ -int smbd_recv (struct smbd_connection *info,struct msghdr *msg); +int smbd_recv (struct TCP_Server_Info *server,struct msghdr *msg);int smbd_send (struct TCP_Server_Info *server,int num_rqst,struct smb_rqst *rqst);/* Interfaces to register and deregister MR for RDMA read/write */ struct smbdirect_mr_io *smbd_register_mr ( -struct smbd_connection *info,struct iov_iter *iter, +struct TCP_Server_Info *server,struct iov_iter *iter,bool writing,bool need_invalidate);void smbd_mr_fill_buffer_descriptor (struct smbdirect_mr_io *mr,struct smbdirect_buffer_descriptor_v1 *v1);@@ -60,8 +68,10 @@ static inline void *smbd_get_connection( struct TCP_Server_Info *server, struct sockaddr *dstaddr) {return NULL;}static inline int smbd_reconnect (struct TCP_Server_Info *server) {return -1 ; }static inline void smbd_destroy (struct TCP_Server_Info *server) {} -static inline int smbd_recv (struct smbd_connection *info,struct msghdr *msg) {return -1 ; } +static inline int smbd_recv (struct TCP_Server_Info *server,struct msghdr *msg) {return -1 ; }static inline int smbd_send (struct TCP_Server_Info *server,int num_rqst,struct smb_rqst *rqst) {return -1 ; } +static inline struct smbd_connection *smbd_conn_get (struct TCP_Server_Info *server) {return NULL ; } +static inline void smbd_conn_put (struct smbd_connection *info) {}#endif #endif fs/smb/client/transport.c +1
-3@@ -257,9 +257,7 @@ int __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,cifs_in_send_inc (server);if (cifs_rdma_enabled (server)) {/* return -EAGAIN when connecting or reconnecting */ - rc = -EAGAIN ; -if (server->smbd_conn) - rc =smbd_send (server, num_rqst, rqst); + rc =smbd_send (server, num_rqst, rqst); goto smbd_done; }