Skip to content

Commit

Permalink
cifs: protect session channel fields with chan_lock
Browse files Browse the repository at this point in the history
Introducing a new spin lock to protect all the channel related
fields in a cifs_ses struct. This lock should be taken
whenever dealing with the channel fields, and should be held
only for very short intervals which will not sleep.

Currently, all channel related fields in cifs_ses structure
are protected by session_mutex. However, this mutex is held for
long periods (sometimes while waiting for a reply from server).
This makes the codepath quite tricky to change.

Signed-off-by: Shyam Prasad N <[email protected]>
Reviewed-by: Paulo Alcantara (SUSE) <[email protected]>
Signed-off-by: Steve French <[email protected]>
  • Loading branch information
sprasad-microsoft authored and Steve French committed Nov 12, 2021
1 parent 8e07757 commit 724244c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 15 deletions.
2 changes: 2 additions & 0 deletions fs/cifs/cifs_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,14 @@ static int cifs_debug_data_proc_show(struct seq_file *m, void *v)
from_kuid(&init_user_ns, ses->linux_uid),
from_kuid(&init_user_ns, ses->cred_uid));

spin_lock(&ses->chan_lock);
if (ses->chan_count > 1) {
seq_printf(m, "\n\n\tExtra Channels: %zu ",
ses->chan_count-1);
for (j = 1; j < ses->chan_count; j++)
cifs_dump_channel(m, j, &ses->chans[j]);
}
spin_unlock(&ses->chan_lock);

seq_puts(m, "\n\n\tShares: ");
j = 0;
Expand Down
5 changes: 5 additions & 0 deletions fs/cifs/cifsglob.h
Original file line number Diff line number Diff line change
Expand Up @@ -952,16 +952,21 @@ struct cifs_ses {
* iface_lock should be taken when accessing any of these fields
*/
spinlock_t iface_lock;
/* ========= begin: protected by iface_lock ======== */
struct cifs_server_iface *iface_list;
size_t iface_count;
unsigned long iface_last_update; /* jiffies */
/* ========= end: protected by iface_lock ======== */

spinlock_t chan_lock;
/* ========= begin: protected by chan_lock ======== */
#define CIFS_MAX_CHANNELS 16
struct cifs_chan chans[CIFS_MAX_CHANNELS];
struct cifs_chan *binding_chan;
size_t chan_count;
size_t chan_max;
atomic_t chan_seq; /* round robin state */
/* ========= end: protected by chan_lock ======== */
};

/*
Expand Down
25 changes: 22 additions & 3 deletions fs/cifs/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -1577,8 +1577,12 @@ static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
* If an existing session is limited to less channels than
* requested, it should not be reused
*/
if (ses->chan_max < ctx->max_channels)
spin_lock(&ses->chan_lock);
if (ses->chan_max < ctx->max_channels) {
spin_unlock(&ses->chan_lock);
return 0;
}
spin_unlock(&ses->chan_lock);

switch (ses->sectype) {
case Kerberos:
Expand Down Expand Up @@ -1713,6 +1717,7 @@ cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
void cifs_put_smb_ses(struct cifs_ses *ses)
{
unsigned int rc, xid;
unsigned int chan_count;
struct TCP_Server_Info *server = ses->server;
cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);

Expand Down Expand Up @@ -1754,12 +1759,24 @@ void cifs_put_smb_ses(struct cifs_ses *ses)
list_del_init(&ses->smb_ses_list);
spin_unlock(&cifs_tcp_ses_lock);

spin_lock(&ses->chan_lock);
chan_count = ses->chan_count;
spin_unlock(&ses->chan_lock);

/* close any extra channels */
if (ses->chan_count > 1) {
if (chan_count > 1) {
int i;

for (i = 1; i < ses->chan_count; i++)
for (i = 1; i < chan_count; i++) {
/*
* note: for now, we're okay accessing ses->chans
* without chan_lock. But when chans can go away, we'll
* need to introduce ref counting to make sure that chan
* is not freed from under us.
*/
cifs_put_tcp_session(ses->chans[i].server, 0);
ses->chans[i].server = NULL;
}
}

sesInfoFree(ses);
Expand Down Expand Up @@ -2018,9 +2035,11 @@ cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
mutex_lock(&ses->session_mutex);

/* add server as first channel */
spin_lock(&ses->chan_lock);
ses->chans[0].server = server;
ses->chan_count = 1;
ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
spin_unlock(&ses->chan_lock);

rc = cifs_negotiate_protocol(xid, ses);
if (!rc)
Expand Down
1 change: 1 addition & 0 deletions fs/cifs/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ sesInfoAlloc(void)
INIT_LIST_HEAD(&ret_buf->tcon_list);
mutex_init(&ret_buf->session_mutex);
spin_lock_init(&ret_buf->iface_lock);
spin_lock_init(&ret_buf->chan_lock);
}
return ret_buf;
}
Expand Down
50 changes: 38 additions & 12 deletions fs/cifs/sess.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,41 +54,53 @@ bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
{
int i;

spin_lock(&ses->chan_lock);
for (i = 0; i < ses->chan_count; i++) {
if (is_server_using_iface(ses->chans[i].server, iface))
if (is_server_using_iface(ses->chans[i].server, iface)) {
spin_unlock(&ses->chan_lock);
return true;
}
}
spin_unlock(&ses->chan_lock);
return false;
}

/* returns number of channels added */
int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
{
int old_chan_count = ses->chan_count;
int left = ses->chan_max - ses->chan_count;
int old_chan_count, new_chan_count;
int left;
int i = 0;
int rc = 0;
int tries = 0;
struct cifs_server_iface *ifaces = NULL;
size_t iface_count;

if (ses->server->dialect < SMB30_PROT_ID) {
cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
return 0;
}

spin_lock(&ses->chan_lock);

new_chan_count = old_chan_count = ses->chan_count;
left = ses->chan_max - ses->chan_count;

if (left <= 0) {
cifs_dbg(FYI,
"ses already at max_channels (%zu), nothing to open\n",
ses->chan_max);
return 0;
}

if (ses->server->dialect < SMB30_PROT_ID) {
cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
spin_unlock(&ses->chan_lock);
return 0;
}

if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname);
ses->chan_max = 1;
spin_unlock(&ses->chan_lock);
return 0;
}
spin_unlock(&ses->chan_lock);

/*
* Make a copy of the iface list at the time and use that
Expand Down Expand Up @@ -142,10 +154,11 @@ int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
i);
left--;
new_chan_count++;
}

kfree(ifaces);
return ses->chan_count - old_chan_count;
return new_chan_count - old_chan_count;
}

/*
Expand All @@ -157,17 +170,22 @@ cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
{
int i;

spin_lock(&ses->chan_lock);
for (i = 0; i < ses->chan_count; i++) {
if (ses->chans[i].server == server)
if (ses->chans[i].server == server) {
spin_unlock(&ses->chan_lock);
return &ses->chans[i];
}
}
spin_unlock(&ses->chan_lock);
return NULL;
}

static int
cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
struct cifs_server_iface *iface)
{
struct TCP_Server_Info *chan_server;
struct cifs_chan *chan;
struct smb3_fs_context ctx = {NULL};
static const char unc_fmt[] = "\\%s\\foo";
Expand Down Expand Up @@ -240,15 +258,20 @@ cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
SMB2_CLIENT_GUID_SIZE);
ctx.use_client_guid = true;

mutex_lock(&ses->session_mutex);
chan_server = cifs_get_tcp_session(&ctx);

mutex_lock(&ses->session_mutex);
spin_lock(&ses->chan_lock);
chan = ses->binding_chan = &ses->chans[ses->chan_count];
chan->server = cifs_get_tcp_session(&ctx);
chan->server = chan_server;
if (IS_ERR(chan->server)) {
rc = PTR_ERR(chan->server);
chan->server = NULL;
spin_unlock(&ses->chan_lock);
goto out;
}
spin_unlock(&ses->chan_lock);

spin_lock(&cifs_tcp_ses_lock);
chan->server->is_channel = true;
spin_unlock(&cifs_tcp_ses_lock);
Expand Down Expand Up @@ -283,8 +306,11 @@ cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
* ses to the new server.
*/

spin_lock(&ses->chan_lock);
ses->chan_count++;
atomic_set(&ses->chan_seq, 0);
spin_unlock(&ses->chan_lock);

out:
ses->binding = false;
ses->binding_chan = NULL;
Expand Down
3 changes: 3 additions & 0 deletions fs/cifs/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -1044,14 +1044,17 @@ struct TCP_Server_Info *cifs_pick_channel(struct cifs_ses *ses)
if (!ses)
return NULL;

spin_lock(&ses->chan_lock);
if (!ses->binding) {
/* round robin */
if (ses->chan_count > 1) {
index = (uint)atomic_inc_return(&ses->chan_seq);
index %= ses->chan_count;
}
spin_unlock(&ses->chan_lock);
return ses->chans[index].server;
} else {
spin_unlock(&ses->chan_lock);
return cifs_ses_server(ses);
}
}
Expand Down

0 comments on commit 724244c

Please sign in to comment.