cifs_pick_channel() returns the server of a channel without a reference, only protected by the srv_count the session holds for the channel. The callers keep using it, e.g. across the back-off before a replay or a wait for the reconnect, and the netfs read and write subrequests keep it in rdata->server and wdata->server. When the channel is removed meanwhile, e.g. if the server doesn't support multichannel anymore after a reconnect, the server struct can be freed while it's still used. Take a reference in cifs_pick_channel() under ses->chan_lock, which also protects the removal of a channel, and drop it when the server isn't used anymore: - The callers with a local server variable use the new __free(cifs_put_channel) cleanup, and cifs_repick_channel() at the start of each replay pass, which drops the reference of the previous pass. - The netfs subrequests use cifs_repick_channel() in cifs_prepare_read() and cifs_prepare_write(), which are called again for a retry, and cifs_free_subrequest() drops the reference. - SMB2_read() and SMB2_write() only pick a channel if the caller didn't set io_parms->server, they hold that reference for the call and reset io_parms->server afterwards. This is also a prerequisite for "smb: client: use cifs_server_wait_for_exit_ms() for the replay back-off", so it carries the Fixes tag of that patch as well. Fixes: 5f68ea4aa98b ("cifs: multichannel: move channel selection in function") Fixes: 4f1fffa23769 ("cifs: commands that are retried should have replay flag set") Cc: stable@vger.kernel.org Cc: Aurelien Aptel Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/client/cached_dir.c | 4 +- fs/smb/client/cifsproto.h | 20 +++++++ fs/smb/client/file.c | 16 +++-- fs/smb/client/link.c | 4 +- fs/smb/client/smb2inode.c | 11 ++-- fs/smb/client/smb2ops.c | 16 ++--- fs/smb/client/smb2pdu.c | 117 ++++++++++++++++++++++++------------- fs/smb/client/transport.c | 10 +++- 8 files changed, 134 insertions(+), 64 deletions(-) diff --git a/fs/smb/client/cached_dir.c b/fs/smb/client/cached_dir.c index 50217f47a0ed..5e0e33d28815 100644 --- a/fs/smb/client/cached_dir.c +++ b/fs/smb/client/cached_dir.c @@ -138,7 +138,7 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, bool lookup_only, struct cached_fid **ret_cfid) { struct cifs_ses *ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; struct cifs_open_parms oparms; struct smb2_create_rsp *o_rsp = NULL; struct smb2_query_info_rsp *qi_rsp = NULL; @@ -174,7 +174,7 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, /* reinitialize for possible replay */ flags = 0; oplock = SMB2_OPLOCK_LEVEL_II; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); if (!server->ops->new_lease_key) return smb_EIO(smb_eio_trace_no_lease_key); diff --git a/fs/smb/client/cifsproto.h b/fs/smb/client/cifsproto.h index c9ae85422f10..35276c1c2772 100644 --- a/fs/smb/client/cifsproto.h +++ b/fs/smb/client/cifsproto.h @@ -7,6 +7,7 @@ */ #ifndef _CIFSPROTO_H #define _CIFSPROTO_H +#include #include #include #include "cifsglob.h" @@ -281,6 +282,25 @@ struct TCP_Server_Info *cifs_get_tcp_session(struct smb3_fs_context *ctx, void cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect); void cifs_tcp_server_get(struct TCP_Server_Info *server); void cifs_tcp_server_put(struct TCP_Server_Info *server); + +/* drop the reference of a server cifs_pick_channel() returned */ +static inline void cifs_put_channel(struct TCP_Server_Info *server) +{ + if (server) + cifs_tcp_server_put(server); +} +DEFINE_FREE(cifs_put_channel, struct TCP_Server_Info *, cifs_put_channel(_T)) + +/* + * Drop the reference of the channel server picked before, e.g. by the + * previous pass of a replay loop, and pick one again. + */ +static inline void cifs_repick_channel(struct TCP_Server_Info **server, + struct cifs_ses *ses) +{ + cifs_put_channel(*server); + *server = cifs_pick_channel(ses); +} void cifs_put_tcon(struct cifs_tcon *tcon, enum smb3_tcon_ref_trace trace); void cifs_release_automount_timer(void); diff --git a/fs/smb/client/file.c b/fs/smb/client/file.c index e7ed1ad58f37..46da7b8d5525 100644 --- a/fs/smb/client/file.c +++ b/fs/smb/client/file.c @@ -316,8 +316,9 @@ static void cifs_prepare_write(struct netfs_io_subrequest *subreq) wdata->have_xid = true; } - server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses); - wdata->server = server; + /* the reference of the channel is dropped by cifs_free_subrequest() */ + cifs_repick_channel(&wdata->server, tlink_tcon(open_file->tlink)->ses); + server = wdata->server; wdata->deferred = false; if (reprepared) { @@ -575,8 +576,9 @@ static int cifs_prepare_read(struct netfs_io_subrequest *subreq) rdata->have_xid = true; } - server = cifs_pick_channel(tlink_tcon(req->cfile->tlink)->ses); - rdata->server = server; + /* the reference of the channel is dropped by cifs_free_subrequest() */ + cifs_repick_channel(&rdata->server, tlink_tcon(req->cfile->tlink)->ses); + server = rdata->server; if (cifs_io_aborted(rdata) || (reprepared && cifs_io_stopped(rdata))) @@ -794,6 +796,10 @@ static void cifs_free_subrequest(struct netfs_io_subrequest *subreq) if (rdata->have_xid) free_xid(rdata->xid); + + /* from cifs_pick_channel() in cifs_prepare_read/write() */ + cifs_put_channel(rdata->server); + rdata->server = NULL; } const struct netfs_request_ops cifs_req_ops = { @@ -1721,7 +1727,7 @@ cifs_relock_file(struct cifsFileInfo *cfile) static bool reopen_aborted(struct cifs_tcon *tcon, int aborts) { - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; if (atomic_read(&tcon->umount_aborts) == aborts) return false; diff --git a/fs/smb/client/link.c b/fs/smb/client/link.c index e865aeea8dbb..133b9e1bebe8 100644 --- a/fs/smb/client/link.c +++ b/fs/smb/client/link.c @@ -319,7 +319,7 @@ int smb2_query_mf_symlink(const unsigned int xid, struct kvec read_iov[2], close_iov; struct cifsFileInfo *cfile = NULL; struct smb2_read_rsp *read_rsp; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; int retries = 0, cur_sleep = 0; struct cifs_io_parms io_parms; struct smb2_read_req *req; @@ -342,7 +342,7 @@ int smb2_query_mf_symlink(const unsigned int xid, num_rqst = 0; req = NULL; oplock = SMB2_OPLOCK_LEVEL_NONE; - server = cifs_pick_channel(tcon->ses); + cifs_repick_channel(&server, tcon->ses); if (smb3_encryption_required(tcon)) flags |= CIFS_TRANSFORM_REQ; diff --git a/fs/smb/client/smb2inode.c b/fs/smb/client/smb2inode.c index cc29ff042c56..6a4d9ace9ece 100644 --- a/fs/smb/client/smb2inode.c +++ b/fs/smb/client/smb2inode.c @@ -222,7 +222,7 @@ static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon, struct cifs_open_info_data *idata; struct cifs_ses *ses = tcon->ses; struct reparse_data_buffer *rbuf; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; int resp_buftype[MAX_COMPOUND]; int retries = 0, cur_sleep = 0; __u8 delete_pending[8] = {1,}; @@ -243,7 +243,7 @@ static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon, flags = 0; oplock = SMB2_OPLOCK_LEVEL_NONE; num_rqst = 0; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); vars = kzalloc_obj(*vars); if (vars == NULL) { @@ -1111,7 +1111,7 @@ smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name, struct kvec open_iov[SMB2_CREATE_IOV_SIZE]; __le16 *utf16_path __free(kfree) = NULL; int retries = 0, cur_sleep = 0; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; struct cifs_open_parms oparms; struct smb2_create_req *creq; struct inode *inode = NULL; @@ -1132,7 +1132,7 @@ smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name, flags |= CIFS_TRANSFORM_REQ; again: oplock = SMB2_OPLOCK_LEVEL_NONE; - server = cifs_pick_channel(tcon->ses); + cifs_repick_channel(&server, tcon->ses); memset(rqst, 0, sizeof(rqst)); memset(resp_buftype, 0, sizeof(resp_buftype)); @@ -1277,7 +1277,8 @@ int smb2_rename_path(const unsigned int xid, static int clear_tmpfile_attr(const unsigned int xid, struct cifs_tcon *tcon, struct inode *inode, const char *full_path) { - struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses); + struct TCP_Server_Info *server __free(cifs_put_channel) = + cifs_pick_channel(tcon->ses); struct cifsInodeInfo *cinode = CIFS_I(inode); FILE_BASIC_INFO fi; diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c index 0ba6345ed656..37cba39274f1 100644 --- a/fs/smb/client/smb2ops.c +++ b/fs/smb/client/smb2ops.c @@ -1213,7 +1213,7 @@ static int smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon, { struct smb2_compound_vars *vars; struct cifs_ses *ses = tcon->ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; struct smb_rqst *rqst; struct kvec *rsp_iov; __le16 *utf16_path = NULL; @@ -1236,7 +1236,7 @@ static int smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon, used_len = 0; flags = CIFS_CP_CREATE_CLOSE_OP; oplock = SMB2_OPLOCK_LEVEL_NONE; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); if (smb3_encryption_required(tcon)) flags |= CIFS_TRANSFORM_REQ; @@ -1612,7 +1612,7 @@ smb2_ioctl_query_info(const unsigned int xid, struct smb_rqst *rqst; struct kvec *rsp_iov; struct cifs_ses *ses = tcon->ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; char __user *arg = (char __user *)p; struct smb_query_info qi; struct smb_query_info __user *pqi; @@ -1636,7 +1636,7 @@ smb2_ioctl_query_info(const unsigned int xid, buffer = NULL; flags = CIFS_CP_CREATE_CLOSE_OP; oplock = SMB2_OPLOCK_LEVEL_NONE; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); vars = kzalloc_obj(*vars, GFP_ATOMIC); if (vars == NULL) @@ -2613,14 +2613,14 @@ smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon, struct cifs_open_parms oparms; struct smb2_query_directory_rsp *qd_rsp = NULL; struct smb2_create_rsp *op_rsp = NULL; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; int retries = 0, cur_sleep = 0; replay_again: /* reinitialize for possible replay */ flags = 0; oplock = SMB2_OPLOCK_LEVEL_NONE; - server = cifs_pick_channel(tcon->ses); + cifs_repick_channel(&server, tcon->ses); utf16_path = cifs_convert_path_to_utf16(path, cifs_sb); if (!utf16_path) @@ -2978,7 +2978,7 @@ smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon, { struct smb2_compound_vars *vars; struct cifs_ses *ses = tcon->ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; int flags = CIFS_CP_CREATE_CLOSE_OP; struct smb_rqst *rqst; int resp_buftype[3]; @@ -2996,7 +2996,7 @@ smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon, cfid = NULL; flags = CIFS_CP_CREATE_CLOSE_OP; oplock = SMB2_OPLOCK_LEVEL_NONE; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); if (!path) path = ""; diff --git a/fs/smb/client/smb2pdu.c b/fs/smb/client/smb2pdu.c index 8d6c11099f6a..2a7cbb7e5387 100644 --- a/fs/smb/client/smb2pdu.c +++ b/fs/smb/client/smb2pdu.c @@ -2177,7 +2177,8 @@ int SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree, __le16 *unc_path = NULL; int flags = 0; unsigned int total_len; - struct TCP_Server_Info *server = cifs_pick_channel(ses); + struct TCP_Server_Info *server __free(cifs_put_channel) = + cifs_pick_channel(ses); cifs_dbg(FYI, "TCON\n"); @@ -2318,7 +2319,8 @@ SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon) struct smb2_tree_disconnect_req *req; /* response is trivial */ int rc = 0; struct cifs_ses *ses = tcon->ses; - struct TCP_Server_Info *server = cifs_pick_channel(ses); + struct TCP_Server_Info *server __free(cifs_put_channel) = + cifs_pick_channel(ses); int flags = 0; unsigned int total_len; struct kvec iov[1]; @@ -3007,7 +3009,7 @@ int smb311_posix_mkdir(const unsigned int xid, struct inode *inode, int flags = 0; unsigned int total_len; __le16 *utf16_path = NULL; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; int retries = 0, cur_sleep = 0; replay_again: @@ -3015,7 +3017,7 @@ int smb311_posix_mkdir(const unsigned int xid, struct inode *inode, pc_buf = NULL; flags = 0; n_iov = 2; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); cifs_dbg(FYI, "mkdir\n"); @@ -3370,7 +3372,7 @@ SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path, struct smb2_create_rsp *rsp = NULL; struct cifs_tcon *tcon = oparms->tcon; struct cifs_ses *ses = tcon->ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; struct kvec iov[SMB2_CREATE_IOV_SIZE]; struct kvec rsp_iov = {NULL, 0}; int resp_buftype = CIFS_NO_BUFFER; @@ -3392,7 +3394,7 @@ SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path, resp_buftype = CIFS_NO_BUFFER; memset(&rsp_iov, 0, sizeof(rsp_iov)); flags = 0; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); /* reissue the same create: a durable reconnect clears ->reconnect */ oparms->disposition = disp; oparms->reconnect = reconnect; @@ -3639,7 +3641,7 @@ SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, struct smb_rqst rqst; struct smb2_ioctl_rsp *rsp = NULL; struct cifs_ses *ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; struct kvec iov[SMB2_IOCTL_IOV_SIZE]; struct kvec rsp_iov = {NULL, 0}; int resp_buftype = CIFS_NO_BUFFER; @@ -3659,7 +3661,7 @@ SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, resp_buftype = CIFS_NO_BUFFER; memset(&rsp_iov, 0, sizeof(rsp_iov)); flags = 0; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); if (!server) return smb_EIO(smb_eio_trace_null_pointers); @@ -3841,7 +3843,7 @@ __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, struct smb_rqst rqst; struct smb2_close_rsp *rsp = NULL; struct cifs_ses *ses = tcon->ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; struct kvec iov[1]; struct kvec rsp_iov; int resp_buftype = CIFS_NO_BUFFER; @@ -3856,7 +3858,7 @@ __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, memset(&rsp_iov, 0, sizeof(rsp_iov)); flags = 0; query_attrs = false; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); cifs_dbg(FYI, "Close\n"); @@ -4054,7 +4056,7 @@ query_info(const unsigned int xid, struct cifs_tcon *tcon, int rc = 0; int resp_buftype = CIFS_NO_BUFFER; struct cifs_ses *ses = tcon->ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; int flags = 0; bool allocated = false; int retries = 0, cur_sleep = 0; @@ -4070,7 +4072,7 @@ query_info(const unsigned int xid, struct cifs_tcon *tcon, memset(&rsp_iov, 0, sizeof(rsp_iov)); flags = 0; allocated = false; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); if (!server) return smb_EIO(smb_eio_trace_null_pointers); @@ -4237,7 +4239,7 @@ SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon, u32 *plen /* returned data len */) { struct cifs_ses *ses = tcon->ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; struct smb_rqst rqst; struct smb2_change_notify_rsp *smb_rsp; struct kvec iov[1]; @@ -4252,7 +4254,7 @@ SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon, resp_buftype = CIFS_NO_BUFFER; memset(&rsp_iov, 0, sizeof(rsp_iov)); flags = 0; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); cifs_dbg(FYI, "change notify\n"); if (!ses || !server) @@ -4588,7 +4590,7 @@ SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, struct smb_rqst rqst; struct kvec iov[1]; struct kvec rsp_iov = {NULL, 0}; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; int resp_buftype = CIFS_NO_BUFFER; int flags = 0; int rc = 0; @@ -4599,7 +4601,7 @@ SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, resp_buftype = CIFS_NO_BUFFER; memset(&rsp_iov, 0, sizeof(rsp_iov)); flags = 0; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); cifs_dbg(FYI, "flush\n"); if (!ses || !(ses->server)) @@ -5036,9 +5038,9 @@ smb2_async_readv(struct cifs_io_subrequest *rdata) return rc; } -int -SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms, - unsigned int *nbytes, char **buf, int *buf_type) +static int +__SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms, + unsigned int *nbytes, char **buf, int *buf_type) { struct smb_rqst rqst; int resp_buftype, rc; @@ -5051,9 +5053,6 @@ SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms, int flags = CIFS_LOG_ERROR; struct cifs_ses *ses = io_parms->tcon->ses; - if (!io_parms->server) - io_parms->server = cifs_pick_channel(io_parms->tcon->ses); - *nbytes = 0; rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0); if (rc) @@ -5141,6 +5140,27 @@ SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms, return rc; } +/* + * If the caller didn't choose a channel in io_parms->server, use one for + * this call only, cifs_pick_channel() returns it with a reference. + */ +int +SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms, + unsigned int *nbytes, char **buf, int *buf_type) +{ + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; + int rc; + + if (io_parms->server) + return __SMB2_read(xid, io_parms, nbytes, buf, buf_type); + + server = cifs_pick_channel(io_parms->tcon->ses); + io_parms->server = server; + rc = __SMB2_read(xid, io_parms, nbytes, buf, buf_type); + io_parms->server = NULL; + return rc; +} + /* * Check the mid_state and signature on received buffer (if any), and queue the * workqueue completion task. @@ -5474,9 +5494,9 @@ smb2_async_writev(struct cifs_io_subrequest *wdata) * elements with data to write that begins with position 1 in iov array. All * data length is specified by count. */ -int -SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms, - unsigned int *nbytes, struct kvec *iov, int n_vec) +static int +__SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms, + unsigned int *nbytes, struct kvec *iov, int n_vec) { struct smb_rqst rqst; int rc = 0; @@ -5493,8 +5513,6 @@ SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms, /* reinitialize for possible replay */ flags = 0; *nbytes = 0; - if (!io_parms->server) - io_parms->server = cifs_pick_channel(io_parms->tcon->ses); server = io_parms->server; if (server == NULL) return -ECONNABORTED; @@ -5578,6 +5596,24 @@ SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms, return rc; } +/* see SMB2_read() for io_parms->server */ +int +SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms, + unsigned int *nbytes, struct kvec *iov, int n_vec) +{ + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; + int rc; + + if (io_parms->server) + return __SMB2_write(xid, io_parms, nbytes, iov, n_vec); + + server = cifs_pick_channel(io_parms->tcon->ses); + io_parms->server = server; + rc = __SMB2_write(xid, io_parms, nbytes, iov, n_vec); + io_parms->server = NULL; + return rc; +} + int posix_info_sid_size(const void *beg, const void *end) { size_t subauth; @@ -5900,7 +5936,7 @@ SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon, struct kvec rsp_iov; int rc = 0; struct cifs_ses *ses = tcon->ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; int flags = 0; int retries = 0, cur_sleep = 0; @@ -5909,7 +5945,7 @@ SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon, resp_buftype = CIFS_NO_BUFFER; memset(&rsp_iov, 0, sizeof(rsp_iov)); flags = 0; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); if (!ses || !(ses->server)) return smb_EIO(smb_eio_trace_null_pointers); @@ -6041,14 +6077,14 @@ send_set_info(const unsigned int xid, struct cifs_tcon *tcon, int rc = 0; int resp_buftype; struct cifs_ses *ses = tcon->ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; int flags = 0; int retries = 0, cur_sleep = 0; replay_again: /* reinitialize for possible replay */ flags = 0; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); if (!ses || !server) return smb_EIO(smb_eio_trace_null_pointers); @@ -6173,7 +6209,7 @@ SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon, int rc; struct smb2_oplock_break *req = NULL; struct cifs_ses *ses = tcon->ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; int flags = CIFS_OBREAK_OP; unsigned int total_len; struct kvec iov[1]; @@ -6184,7 +6220,7 @@ SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon, replay_again: /* reinitialize for possible replay */ flags = CIFS_OBREAK_OP; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); cifs_dbg(FYI, "SMB2_oplock_break\n"); rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server, @@ -6313,7 +6349,7 @@ SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon, int rc = 0; int resp_buftype; struct cifs_ses *ses = tcon->ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; FILE_SYSTEM_POSIX_INFO *info = NULL; int flags = 0; int retries = 0, cur_sleep = 0; @@ -6321,7 +6357,7 @@ SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon, replay_again: /* reinitialize for possible replay */ flags = 0; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); rc = build_qfs_info_req(&iov, tcon, server, FS_POSIX_INFORMATION, @@ -6382,7 +6418,7 @@ SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, int rc = 0; int resp_buftype, max_len, min_len; struct cifs_ses *ses = tcon->ses; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; unsigned int rsp_len, offset; int flags = 0; int retries = 0, cur_sleep = 0; @@ -6390,7 +6426,7 @@ SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, replay_again: /* reinitialize for possible replay */ flags = 0; - server = cifs_pick_channel(ses); + cifs_repick_channel(&server, ses); if (level == FS_DEVICE_INFORMATION) { max_len = sizeof(FILE_SYSTEM_DEVICE_INFO); @@ -6488,13 +6524,13 @@ smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon, unsigned int count; int flags = CIFS_NO_RSP_BUF; unsigned int total_len; - struct TCP_Server_Info *server; + struct TCP_Server_Info *server __free(cifs_put_channel) = NULL; int retries = 0, cur_sleep = 0; replay_again: /* reinitialize for possible replay */ flags = CIFS_NO_RSP_BUF; - server = cifs_pick_channel(tcon->ses); + cifs_repick_channel(&server, tcon->ses); cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock); @@ -6595,7 +6631,8 @@ SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon, int resp_buf_type; __u64 *please_key_high; __u64 *please_key_low; - struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses); + struct TCP_Server_Info *server __free(cifs_put_channel) = + cifs_pick_channel(tcon->ses); cifs_dbg(FYI, "SMB2_lease_break\n"); rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server, diff --git a/fs/smb/client/transport.c b/fs/smb/client/transport.c index 04e1e0f668e8..5cc842aafb37 100644 --- a/fs/smb/client/transport.c +++ b/fs/smb/client/transport.c @@ -919,8 +919,8 @@ cifs_mark_compound_mids_cancelled(struct mid_q_entry **mid, int count) * reconnect), preferring the least loaded one. If no eligible channel is * found, fall back to the primary channel (index 0). * - * Return: TCP_Server_Info pointer for the chosen channel, or NULL if @ses is - * NULL. + * Return: TCP_Server_Info pointer for the chosen channel with a reference, + * which the caller drops with cifs_put_channel(), or NULL if @ses is NULL. */ struct TCP_Server_Info *cifs_pick_channel(struct cifs_ses *ses) { @@ -958,6 +958,12 @@ struct TCP_Server_Info *cifs_pick_channel(struct cifs_ses *ses) } server = ses->chans[index].server; + /* + * A channel can be removed while the caller still uses its + * server, e.g. across a replay back-off, so take a reference. + */ + if (server) + cifs_tcp_server_get(server); spin_unlock(&ses->chan_lock); return server; -- 2.43.0