Skip to content

Commit aa45dad

Browse files
sprasad-microsoftSteve French
authored andcommitted
cifs: change iface_list from array to sorted linked list
A server's published interface list can change over time, and needs to be updated. We've storing iface_list as a simple array, which makes it difficult to manipulate an existing list. With this change, iface_list is modified into a linked list of interfaces, which is kept sorted by speed. Also added a reference counter for an iface entry, so that each channel can maintain a backpointer to the iface and drop it easily when needed. Signed-off-by: Shyam Prasad N <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent 9de7499 commit aa45dad

File tree

6 files changed

+201
-129
lines changed

6 files changed

+201
-129
lines changed

fs/cifs/cifs_debug.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ cifs_dump_iface(struct seq_file *m, struct cifs_server_iface *iface)
162162
seq_printf(m, "\t\tIPv4: %pI4\n", &ipv4->sin_addr);
163163
else if (iface->sockaddr.ss_family == AF_INET6)
164164
seq_printf(m, "\t\tIPv6: %pI6\n", &ipv6->sin6_addr);
165+
if (!iface->is_active)
166+
seq_puts(m, "\t\t[for-cleanup]\n");
165167
}
166168

167169
static int cifs_debug_files_proc_show(struct seq_file *m, void *v)
@@ -221,6 +223,7 @@ static int cifs_debug_data_proc_show(struct seq_file *m, void *v)
221223
struct TCP_Server_Info *server;
222224
struct cifs_ses *ses;
223225
struct cifs_tcon *tcon;
226+
struct cifs_server_iface *iface;
224227
int c, i, j;
225228

226229
seq_puts(m,
@@ -456,11 +459,10 @@ static int cifs_debug_data_proc_show(struct seq_file *m, void *v)
456459
if (ses->iface_count)
457460
seq_printf(m, "\n\n\tServer interfaces: %zu",
458461
ses->iface_count);
459-
for (j = 0; j < ses->iface_count; j++) {
460-
struct cifs_server_iface *iface;
461-
462-
iface = &ses->iface_list[j];
463-
seq_printf(m, "\n\t%d)", j+1);
462+
j = 0;
463+
list_for_each_entry(iface, &ses->iface_list,
464+
iface_head) {
465+
seq_printf(m, "\n\t%d)", ++j);
464466
cifs_dump_iface(m, iface);
465467
if (is_ses_using_iface(ses, iface))
466468
seq_puts(m, "\t\t[CONNECTED]\n");

fs/cifs/cifsglob.h

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,15 +933,67 @@ static inline void cifs_set_net_ns(struct TCP_Server_Info *srv, struct net *net)
933933
#endif
934934

935935
struct cifs_server_iface {
936+
struct list_head iface_head;
937+
struct kref refcount;
936938
size_t speed;
937939
unsigned int rdma_capable : 1;
938940
unsigned int rss_capable : 1;
941+
unsigned int is_active : 1; /* unset if non existent */
939942
struct sockaddr_storage sockaddr;
940943
};
941944

945+
/* release iface when last ref is dropped */
946+
static inline void
947+
release_iface(struct kref *ref)
948+
{
949+
struct cifs_server_iface *iface = container_of(ref,
950+
struct cifs_server_iface,
951+
refcount);
952+
list_del_init(&iface->iface_head);
953+
kfree(iface);
954+
}
955+
956+
/*
957+
* compare two interfaces a and b
958+
* return 0 if everything matches.
959+
* return 1 if a has higher link speed, or rdma capable, or rss capable
960+
* return -1 otherwise.
961+
*/
962+
static inline int
963+
iface_cmp(struct cifs_server_iface *a, struct cifs_server_iface *b)
964+
{
965+
int cmp_ret = 0;
966+
967+
WARN_ON(!a || !b);
968+
if (a->speed == b->speed) {
969+
if (a->rdma_capable == b->rdma_capable) {
970+
if (a->rss_capable == b->rss_capable) {
971+
cmp_ret = memcmp(&a->sockaddr, &b->sockaddr,
972+
sizeof(a->sockaddr));
973+
if (!cmp_ret)
974+
return 0;
975+
else if (cmp_ret > 0)
976+
return 1;
977+
else
978+
return -1;
979+
} else if (a->rss_capable > b->rss_capable)
980+
return 1;
981+
else
982+
return -1;
983+
} else if (a->rdma_capable > b->rdma_capable)
984+
return 1;
985+
else
986+
return -1;
987+
} else if (a->speed > b->speed)
988+
return 1;
989+
else
990+
return -1;
991+
}
992+
942993
struct cifs_chan {
943994
unsigned int in_reconnect : 1; /* if session setup in progress for this channel */
944995
struct TCP_Server_Info *server;
996+
struct cifs_server_iface *iface; /* interface in use */
945997
__u8 signkey[SMB3_SIGN_KEY_SIZE];
946998
};
947999

@@ -993,7 +1045,7 @@ struct cifs_ses {
9931045
*/
9941046
spinlock_t iface_lock;
9951047
/* ========= begin: protected by iface_lock ======== */
996-
struct cifs_server_iface *iface_list;
1048+
struct list_head iface_list;
9971049
size_t iface_count;
9981050
unsigned long iface_last_update; /* jiffies */
9991051
/* ========= end: protected by iface_lock ======== */

fs/cifs/connect.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,9 +1894,11 @@ void cifs_put_smb_ses(struct cifs_ses *ses)
18941894
int i;
18951895

18961896
for (i = 1; i < chan_count; i++) {
1897-
spin_unlock(&ses->chan_lock);
1897+
if (ses->chans[i].iface) {
1898+
kref_put(&ses->chans[i].iface->refcount, release_iface);
1899+
ses->chans[i].iface = NULL;
1900+
}
18981901
cifs_put_tcp_session(ses->chans[i].server, 0);
1899-
spin_lock(&ses->chan_lock);
19001902
ses->chans[i].server = NULL;
19011903
}
19021904
}

fs/cifs/misc.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ sesInfoAlloc(void)
7575
INIT_LIST_HEAD(&ret_buf->tcon_list);
7676
mutex_init(&ret_buf->session_mutex);
7777
spin_lock_init(&ret_buf->iface_lock);
78+
INIT_LIST_HEAD(&ret_buf->iface_list);
7879
spin_lock_init(&ret_buf->chan_lock);
7980
}
8081
return ret_buf;
@@ -83,6 +84,8 @@ sesInfoAlloc(void)
8384
void
8485
sesInfoFree(struct cifs_ses *buf_to_free)
8586
{
87+
struct cifs_server_iface *iface = NULL, *niface = NULL;
88+
8689
if (buf_to_free == NULL) {
8790
cifs_dbg(FYI, "Null buffer passed to sesInfoFree\n");
8891
return;
@@ -96,7 +99,11 @@ sesInfoFree(struct cifs_ses *buf_to_free)
9699
kfree(buf_to_free->user_name);
97100
kfree(buf_to_free->domainName);
98101
kfree_sensitive(buf_to_free->auth_key.response);
99-
kfree(buf_to_free->iface_list);
102+
spin_lock(&buf_to_free->iface_lock);
103+
list_for_each_entry_safe(iface, niface, &buf_to_free->iface_list,
104+
iface_head)
105+
kref_put(&iface->refcount, release_iface);
106+
spin_unlock(&buf_to_free->iface_lock);
100107
kfree_sensitive(buf_to_free);
101108
}
102109

fs/cifs/sess.c

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
5858

5959
spin_lock(&ses->chan_lock);
6060
for (i = 0; i < ses->chan_count; i++) {
61-
if (is_server_using_iface(ses->chans[i].server, iface)) {
61+
if (ses->chans[i].iface == iface) {
6262
spin_unlock(&ses->chan_lock);
6363
return true;
6464
}
@@ -151,11 +151,9 @@ int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
151151
{
152152
int old_chan_count, new_chan_count;
153153
int left;
154-
int i = 0;
155154
int rc = 0;
156155
int tries = 0;
157-
struct cifs_server_iface *ifaces = NULL;
158-
size_t iface_count;
156+
struct cifs_server_iface *iface = NULL, *niface = NULL;
159157

160158
spin_lock(&ses->chan_lock);
161159

@@ -184,33 +182,17 @@ int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
184182
}
185183
spin_unlock(&ses->chan_lock);
186184

187-
/*
188-
* Make a copy of the iface list at the time and use that
189-
* instead so as to not hold the iface spinlock for opening
190-
* channels
191-
*/
192-
spin_lock(&ses->iface_lock);
193-
iface_count = ses->iface_count;
194-
if (iface_count <= 0) {
195-
spin_unlock(&ses->iface_lock);
196-
cifs_dbg(VFS, "no iface list available to open channels\n");
197-
return 0;
198-
}
199-
ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
200-
GFP_ATOMIC);
201-
if (!ifaces) {
202-
spin_unlock(&ses->iface_lock);
203-
return 0;
204-
}
205-
spin_unlock(&ses->iface_lock);
206-
207185
/*
208186
* Keep connecting to same, fastest, iface for all channels as
209187
* long as its RSS. Try next fastest one if not RSS or channel
210188
* creation fails.
211189
*/
190+
spin_lock(&ses->iface_lock);
191+
iface = list_first_entry(&ses->iface_list, struct cifs_server_iface,
192+
iface_head);
193+
spin_unlock(&ses->iface_lock);
194+
212195
while (left > 0) {
213-
struct cifs_server_iface *iface;
214196

215197
tries++;
216198
if (tries > 3*ses->chan_max) {
@@ -219,27 +201,46 @@ int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
219201
break;
220202
}
221203

222-
iface = &ifaces[i];
223-
if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
224-
i = (i+1) % iface_count;
225-
continue;
204+
spin_lock(&ses->iface_lock);
205+
if (!ses->iface_count) {
206+
spin_unlock(&ses->iface_lock);
207+
break;
226208
}
227209

228-
rc = cifs_ses_add_channel(cifs_sb, ses, iface);
229-
if (rc) {
230-
cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
231-
i, rc);
232-
i = (i+1) % iface_count;
233-
continue;
210+
list_for_each_entry_safe_from(iface, niface, &ses->iface_list,
211+
iface_head) {
212+
/* skip ifaces that are unusable */
213+
if (!iface->is_active ||
214+
(is_ses_using_iface(ses, iface) &&
215+
!iface->rss_capable)) {
216+
continue;
217+
}
218+
219+
/* take ref before unlock */
220+
kref_get(&iface->refcount);
221+
222+
spin_unlock(&ses->iface_lock);
223+
rc = cifs_ses_add_channel(cifs_sb, ses, iface);
224+
spin_lock(&ses->iface_lock);
225+
226+
if (rc) {
227+
cifs_dbg(VFS, "failed to open extra channel on iface:%pIS rc=%d\n",
228+
&iface->sockaddr,
229+
rc);
230+
kref_put(&iface->refcount, release_iface);
231+
continue;
232+
}
233+
234+
cifs_dbg(FYI, "successfully opened new channel on iface:%pIS\n",
235+
&iface->sockaddr);
236+
break;
234237
}
238+
spin_unlock(&ses->iface_lock);
235239

236-
cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
237-
i);
238240
left--;
239241
new_chan_count++;
240242
}
241243

242-
kfree(ifaces);
243244
return new_chan_count - old_chan_count;
244245
}
245246

@@ -355,6 +356,7 @@ cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
355356
spin_unlock(&ses->chan_lock);
356357
goto out;
357358
}
359+
chan->iface = iface;
358360
ses->chan_count++;
359361
atomic_set(&ses->chan_seq, 0);
360362

0 commit comments

Comments
 (0)