CIFS: Fix a possible memory corruption during reconnect
Subsystems (2)
PATCH v2 1/1 CIFS: Fix a possible memory corruption during reconnect
We can not unlock/lock cifs_tcp_ses_lock while walking through ses and tcon lists because it can corrupt list iterator pointers and a tcon structure can be released if we don't hold an extra reference. Fix it by moving a reconnect process to a separate delayed work and acquiring a reference to every tcon that needs to be reconnected. Also do not send an echo request on newly established connections.
--- fs/cifs/cifsglob.h | 3 +++ fs/cifs/cifsproto.h | 3 +++ fs/cifs/connect.c | 34 +++++++++++++++++++----- fs/cifs/smb2pdu.c | 75 ++++++++++++++++++++++++++++++++++++----------------- fs/cifs/smb2proto.h | 1 + 5 files changed, 85 insertions(+), 31 deletions(-)
fs/cifs/cifsglob.h +3 -0
@@ -632,6 +632,7 @@ struct TCP_Server_Info {bool sec_mskerberos;/* supports legacy MS Kerberos */ bool large_buf;/* is current buffer large? */ struct delayed_work echo;/* echo ping workqueue job */ +struct delayed_work reconnect;/* reconnect workqueue job */ char *smallbuf;/* pointer to current "small" buffer */ char *bigbuf;/* pointer to current "big" buffer */ unsigned int total_read;/* total amount of data read in this pass */
@@ -648,6 +649,7 @@ struct TCP_Server_Info {__u8 preauth_hash[512 ];#endif /* CONFIG_CIFS_SMB2 */ unsigned long echo_interval; +struct mutex reconnect_mutex;/* prevent simultaneous reconnects */ }; static inline unsigned int
@@ -850,6 +852,7 @@ struct cifs_tcon {struct list_head tcon_list;int tc_count;struct list_head openFileList; +struct list_head rlist;/* reconnect list */ spinlock_t open_file_lock;/* protects list above */ struct cifs_ses *ses;/* pointer to session associated with */ char treeName[MAX_TREE_SIZE +1 ];/* UNC name of resource in ASCII */
fs/cifs/cifsproto.h +3 -0
@@ -206,6 +206,9 @@ extern void cifs_add_pending_open_locked(struct cifs_fid *fid,struct tcon_link *tlink, struct cifs_pending_open *open);extern void cifs_del_pending_open (struct cifs_pending_open *open); +extern void cifs_put_tcp_session (struct TCP_Server_Info *server, +int from_reconnect); +extern void cifs_put_tcon (struct cifs_tcon *tcon);#if IS_ENABLED (CONFIG_CIFS_DFS_UPCALL )extern void cifs_dfs_release_automount_timer (void );
fs/cifs/connect.c +27 -7
@@ -52,6 +52,9 @@#include "nterr.h" #include "rfc1002pdu.h" #include "fscache.h" +#ifdef CONFIG_CIFS_SMB2 +#include "smb2proto.h" +#endif #define CIFS_PORT 445#define RFC1001_PORT 139
@@ -2100,8 +2103,8 @@ cifs_find_tcp_session(struct smb_vol *vol)return NULL ; } -static void -cifs_put_tcp_session (struct TCP_Server_Info *server) +void +cifs_put_tcp_session (struct TCP_Server_Info *server,int from_reconnect) {struct task_struct *task;
@@ -2118,6 +2121,19 @@ cifs_put_tcp_session(struct TCP_Server_Info *server)cancel_delayed_work_sync (&server->echo); +#ifdef CONFIG_CIFS_SMB2 +if (from_reconnect) +/* +* Avoid deadlock here: reconnect work calls +* cifs_put_tcp_session() at its end. Need to be sure +* that reconnect work does nothing with server pointer after +* that step. +*/ +cancel_delayed_work (&server->reconnect); +else +cancel_delayed_work_sync (&server->reconnect); +#endif +spin_lock (&GlobalMid_Lock); server->tcpStatus = CifsExiting;spin_unlock (&GlobalMid_Lock);
@@ -2171,6 +2187,7 @@ cifs_get_tcp_session(struct smb_vol *volume_info)init_waitqueue_head (&tcp_ses->request_q);INIT_LIST_HEAD (&tcp_ses->pending_mid_q);mutex_init (&tcp_ses->srv_mutex); +mutex_init (&tcp_ses->reconnect_mutex);memcpy (tcp_ses->workstation_RFC1001_name, volume_info->source_rfc1001_name,RFC1001_NAME_LEN_WITH_NULL ); memcpy(tcp_ses->server_RFC1001_name,
@@ -2182,6 +2199,9 @@ cifs_get_tcp_session(struct smb_vol *volume_info)INIT_LIST_HEAD (&tcp_ses->tcp_ses_list);INIT_LIST_HEAD (&tcp_ses->smb_ses_list);INIT_DELAYED_WORK (&tcp_ses->echo, cifs_echo_request); +#ifdef CONFIG_CIFS_SMB2 +INIT_DELAYED_WORK (&tcp_ses->reconnect, smb2_reconnect_server); +#endif memcpy (&tcp_ses->srcaddr, &volume_info->srcaddr,sizeof (tcp_ses->srcaddr)); memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
@@ -2340,7 +2360,7 @@ cifs_put_smb_ses(struct cifs_ses *ses)spin_unlock (&cifs_tcp_ses_lock);sesInfoFree (ses); -cifs_put_tcp_session (server); +cifs_put_tcp_session (server,0 ); }#ifdef CONFIG_KEYS
@@ -2514,7 +2534,7 @@ cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)mutex_unlock (&ses->session_mutex);/* existing SMB ses has a server reference already */ -cifs_put_tcp_session (server); +cifs_put_tcp_session (server,0 );free_xid (xid);return ses; }
@@ -2604,7 +2624,7 @@ cifs_find_tcon(struct cifs_ses *ses, const char *unc)return NULL ; } -static void +void cifs_put_tcon (struct cifs_tcon *tcon) {unsigned int xid;
@@ -3792,7 +3812,7 @@ cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)else if (ses)cifs_put_smb_ses (ses);else -cifs_put_tcp_session (server); +cifs_put_tcp_session (server,0 );bdi_destroy (&cifs_sb->bdi); }
@@ -4103,7 +4123,7 @@ cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid) ses =cifs_get_smb_ses (master_tcon->ses->server, vol_info);if (IS_ERR (ses)) { tcon = (struct cifs_tcon *)ses; -cifs_put_tcp_session (master_tcon->ses->server); +cifs_put_tcp_session (master_tcon->ses->server,0 ); goto out; }
fs/cifs/smb2pdu.c +51 -24
89 lines not on this page · raw message
fs/cifs/smb2proto.h +1 -0
@@ -96,6 +96,7 @@ extern int smb2_open_file(const unsigned int xid,extern int smb2_unlock_range (struct cifsFileInfo *cfile,struct file_lock *flock,const unsigned int xid);extern int smb2_push_mandatory_locks (struct cifsFileInfo *cfile); +extern void smb2_reconnect_server (struct work_struct *work);/* * SMB2 Worker functions - most of protocol specific implementation details
Signature · 1 line
2.7.4
2016-11-10 15:31 GMT-08:00 Pavel Shilovsky <[email protected]>:
Quoted 275 lines
We can not unlock/lock
cifs_tcp_ses_lockwhile walking through ses and tcon lists because it can corrupt list iterator pointers and a tcon structure can be released if we don't hold an extra reference. Fix it by moving a reconnect process to a separate delayed work and acquiring a reference to every tcon that needs to be reconnected. Also do not send an echo request on newly established connections.CC Stable <[email protected]>Signed-off-by Pavel Shilovsky <[email protected]>--- fs/cifs/cifsglob.h | 3 +++ fs/cifs/cifsproto.h | 3 +++ fs/cifs/connect.c | 34 +++++++++++++++++++----- fs/cifs/smb2pdu.c | 75 ++++++++++++++++++++++++++++++++++++----------------- fs/cifs/smb2proto.h | 1 + 5 files changed, 85 insertions(+), 31 deletions(-)fs/cifs/cifsglob.h +3
-0@@ -632,6 +632,7 @@ struct TCP_Server_Info {bool sec_mskerberos;/* supports legacy MS Kerberos */ bool large_buf;/* is current buffer large? */ struct delayed_work echo;/* echo ping workqueue job */ +struct delayed_work reconnect;/* reconnect workqueue job */ char *smallbuf;/* pointer to current "small" buffer */ char *bigbuf;/* pointer to current "big" buffer */ unsigned int total_read;/* total amount of data read in this pass */ @@ -648,6 +649,7 @@ struct TCP_Server_Info {__u8 preauth_hash[512 ];#endif /* CONFIG_CIFS_SMB2 */ unsigned long echo_interval; +struct mutex reconnect_mutex;/* prevent simultaneous reconnects */ }; static inline unsigned int@@ -850,6 +852,7 @@ struct cifs_tcon {struct list_head tcon_list;int tc_count;struct list_head openFileList; +struct list_head rlist;/* reconnect list */ spinlock_t open_file_lock;/* protects list above */ struct cifs_ses *ses;/* pointer to session associated with */ char treeName[MAX_TREE_SIZE +1 ];/* UNC name of resource in ASCII */ fs/cifs/cifsproto.h +3
-0@@ -206,6 +206,9 @@ extern void cifs_add_pending_open_locked(struct cifs_fid *fid,struct tcon_link *tlink, struct cifs_pending_open *open);extern void cifs_del_pending_open (struct cifs_pending_open *open); +extern void cifs_put_tcp_session (struct TCP_Server_Info *server, +int from_reconnect); +extern void cifs_put_tcon (struct cifs_tcon *tcon);#if IS_ENABLED (CONFIG_CIFS_DFS_UPCALL )extern void cifs_dfs_release_automount_timer (void );fs/cifs/connect.c +27
-7@@ -52,6 +52,9 @@#include "nterr.h" #include "rfc1002pdu.h" #include "fscache.h" +#ifdef CONFIG_CIFS_SMB2 +#include "smb2proto.h" +#endif #define CIFS_PORT 445#define RFC1001_PORT 139@@ -2100,8 +2103,8 @@ cifs_find_tcp_session(struct smb_vol *vol)return NULL ; } -static void -cifs_put_tcp_session (struct TCP_Server_Info *server) +void +cifs_put_tcp_session (struct TCP_Server_Info *server,int from_reconnect) {struct task_struct *task;@@ -2118,6 +2121,19 @@ cifs_put_tcp_session(struct TCP_Server_Info *server)cancel_delayed_work_sync (&server->echo); +#ifdef CONFIG_CIFS_SMB2 +if (from_reconnect) +/* +* Avoid deadlock here: reconnect work calls +* cifs_put_tcp_session() at its end. Need to be sure +* that reconnect work does nothing with server pointer after +* that step. +*/ +cancel_delayed_work (&server->reconnect); +else +cancel_delayed_work_sync (&server->reconnect); +#endif +spin_lock (&GlobalMid_Lock); server->tcpStatus = CifsExiting;spin_unlock (&GlobalMid_Lock);@@ -2171,6 +2187,7 @@ cifs_get_tcp_session(struct smb_vol *volume_info)init_waitqueue_head (&tcp_ses->request_q);INIT_LIST_HEAD (&tcp_ses->pending_mid_q);mutex_init (&tcp_ses->srv_mutex); +mutex_init (&tcp_ses->reconnect_mutex);memcpy (tcp_ses->workstation_RFC1001_name, volume_info->source_rfc1001_name,RFC1001_NAME_LEN_WITH_NULL ); memcpy(tcp_ses->server_RFC1001_name,@@ -2182,6 +2199,9 @@ cifs_get_tcp_session(struct smb_vol *volume_info)INIT_LIST_HEAD (&tcp_ses->tcp_ses_list);INIT_LIST_HEAD (&tcp_ses->smb_ses_list);INIT_DELAYED_WORK (&tcp_ses->echo, cifs_echo_request); +#ifdef CONFIG_CIFS_SMB2 +INIT_DELAYED_WORK (&tcp_ses->reconnect, smb2_reconnect_server); +#endif memcpy (&tcp_ses->srcaddr, &volume_info->srcaddr,sizeof (tcp_ses->srcaddr)); memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,@@ -2340,7 +2360,7 @@ cifs_put_smb_ses(struct cifs_ses *ses)spin_unlock (&cifs_tcp_ses_lock);sesInfoFree (ses); -cifs_put_tcp_session (server); +cifs_put_tcp_session (server,0 ); }#ifdef CONFIG_KEYS @@ -2514,7 +2534,7 @@ cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)mutex_unlock (&ses->session_mutex);/* existing SMB ses has a server reference already */ -cifs_put_tcp_session (server); +cifs_put_tcp_session (server,0 );free_xid (xid);return ses; }@@ -2604,7 +2624,7 @@ cifs_find_tcon(struct cifs_ses *ses, const char *unc)return NULL ; } -static void +void cifs_put_tcon (struct cifs_tcon *tcon) {unsigned int xid;@@ -3792,7 +3812,7 @@ cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)else if (ses)cifs_put_smb_ses (ses);else -cifs_put_tcp_session (server); +cifs_put_tcp_session (server,0 );bdi_destroy (&cifs_sb->bdi); }@@ -4103,7 +4123,7 @@ cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid) ses =cifs_get_smb_ses (master_tcon->ses->server, vol_info);if (IS_ERR (ses)) { tcon = (struct cifs_tcon *)ses; -cifs_put_tcp_session (master_tcon->ses->server); +cifs_put_tcp_session (master_tcon->ses->server,0 ); goto out; }fs/cifs/smb2pdu.c +51
-24@@ -1972,6 +1972,54 @@ smb2_echo_callback(struct mid_q_entry *mid)add_credits (server, credits_received,CIFS_ECHO_OP ); } +void smb2_reconnect_server (struct work_struct *work) +{ +struct TCP_Server_Info *server =container_of (work, + struct TCP_Server_Info, reconnect.work); +struct cifs_ses *ses; +struct cifs_tcon *tcon, *tcon2; +struct list_head tmp_list; +int tcon_exist = false; + +/* Prevent simultaneous reconnects that can corrupt tcon->rlist list */ +mutex_lock (&server->reconnect_mutex); + +INIT_LIST_HEAD (&tmp_list); +cifs_dbg (FYI ,"Need negotiate, reconnecting tcons\n" ); + +spin_lock (&cifs_tcp_ses_lock); +list_for_each_entry (ses, &server->smb_ses_list, smb_ses_list) { +list_for_each_entry (tcon, &ses->tcon_list, tcon_list) { +if (tcon && tcon->need_reconnect) { + tcon->tc_count++; +list_add_tail (&tcon->rlist, &tmp_list); + tcon_exist = true; + } + } + } +/* +* Get the reference to server struct to be sure that the last call of +* cifs_put_tcon() in the loop below won't release the server pointer. +*/ +if (tcon_exist) + server->srv_count++; + +spin_unlock (&cifs_tcp_ses_lock); + +list_for_each_entry_safe (tcon, tcon2, &tmp_list, rlist) { +smb2_reconnect (SMB2_ECHO , tcon); +list_del_init (&tcon->rlist); +cifs_put_tcon (tcon); + } + +cifs_dbg (FYI ,"Reconnecting tcons finished\n" ); +mutex_unlock (&server->reconnect_mutex); + +/* now we can safely release srv struct */ +if (tcon_exist) +cifs_put_tcp_session (server,1 ); +} +int SMB2_echo (struct TCP_Server_Info *server) {@@ -1984,32 +2032,11 @@ SMB2_echo(struct TCP_Server_Info *server)cifs_dbg (FYI ,"In echo request\n" );if (server->tcpStatus == CifsNeedNegotiate) { -struct list_head *tmp, *tmp2; -struct cifs_ses *ses; -struct cifs_tcon *tcon; - -cifs_dbg (FYI ,"Need negotiate, reconnecting tcons\n" ); -spin_lock (&cifs_tcp_ses_lock); -list_for_each (tmp, &server->smb_ses_list) { - ses = list_entry(tmp, struct cifs_ses, smb_ses_list); -list_for_each (tmp2, &ses->tcon_list) { - tcon = list_entry(tmp2, struct cifs_tcon, - tcon_list); -/* add check for persistent handle reconnect */ -if (tcon && tcon->need_reconnect) { -spin_unlock (&cifs_tcp_ses_lock); - rc =smb2_reconnect (SMB2_ECHO , tcon); -spin_lock (&cifs_tcp_ses_lock); - } - } - } -spin_unlock (&cifs_tcp_ses_lock); +/* No need to send echo on newly established connections */ +mod_delayed_work (cifsiod_wq, &server->reconnect,0 ); +return rc; } -/* if no session, renegotiate failed above */ -if (server->tcpStatus == CifsNeedNegotiate) -return -EIO ; - rc =small_smb2_init (SMB2_ECHO ,NULL , (void **)&req);if (rc)return rc;fs/cifs/smb2proto.h +1
-1@@ -96,6 +96,7 @@ extern int smb2_open_file(const unsigned int xid,extern int smb2_unlock_range (struct cifsFileInfo *cfile,struct file_lock *flock,const unsigned int xid);extern int smb2_push_mandatory_locks (struct cifsFileInfo *cfile); +extern void smb2_reconnect_server (struct work_struct *work);/* * SMB2 Worker functions - most of protocol specific implementation details --
Quoted 2 lines
2.7.4
Any comments on this patch?
Signature · 2 lines
Best regards, Pavel Shilovsky
Aurélien Aptel
Tested-byHi Pavel,
Quoted 7 lines · Pavel Shilovsky
2016-11-10 15:31 GMT-08:00 Pavel Shilovsky <[email protected]>:
Quoted 6 lines
We can not unlock/lock
cifs_tcp_ses_lockwhile walking through ses and tcon lists because it can corrupt list iterator pointers and a tcon structure can be released if we don't hold an extra reference. Fix it by moving a reconnect process to a separate delayed work and acquiring a reference to every tcon that needs to be reconnected. Also do not send an echo request on newly established connections.
I don't fully understand what's going on here but I've successfully tested your patch.
I've applied your patch and triggered a reconnexion on a smb2 mount by virtually unplugging/waiting/replugging the network cable (via qemu set_link <name> on/off). I did not notice any issues.
Let us know if you have a better scenario to test this or a way to reproduce the previous issue.
Signature · 4 lines
Aurélien Aptel / SUSE Labs Samba Team GPG: 1839 CB5F 9F5B FB9B AA97 8C99 03C8 A49B 521B D5D3 SUSE Linux GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
2016-11-24 8:43 GMT-08:00 Aurélien Aptel <aaptel-IBi9RG/[email protected]>:
Quoted 21 lines
Hi Pavel,
Quoted 7 lines · Pavel Shilovsky
2016-11-10 15:31 GMT-08:00 Pavel Shilovsky <[email protected]>:
Quoted 6 lines
We can not unlock/lock
cifs_tcp_ses_lockwhile walking through ses and tcon lists because it can corrupt list iterator pointers and a tcon structure can be released if we don't hold an extra reference. Fix it by moving a reconnect process to a separate delayed work and acquiring a reference to every tcon that needs to be reconnected. Also do not send an echo request on newly established connections.I don't fully understand what's going on here but I've successfully tested your patch.
I've applied your patch and triggered a reconnexion on a smb2 mount by virtually unplugging/waiting/replugging the network cable (via qemu
set_link<name> on/off). I did not notice any issues.Let us know if you have a better scenario to test this or a way to reproduce the previous issue.
Quoted 7 lines
--
Aurélien Aptel / SUSE Labs Samba Team
GPG: 1839 CB5F 9F5B FB9B AA97 8C99 03C8 A49B 521B D5D3
SUSE Linux GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
Thank you for testing this!
I don't have a scripted reproducer but sending something wrong to the server during mounting was triggering this issue. For example, if we send a wrong SMB2 header on Create request the kernel crashes after several mount attempts.
Signature · 2 lines
Best regards, Pavel Shilovsky
Quoted 111 lines · Pavel Shilovsky
We can not unlock/lock
cifs_tcp_ses_lockwhile walking through ses and tcon lists because it can corrupt list iterator pointers and a tcon structure can be released if we don't hold an extra reference. Fix it by moving a reconnect process to a separate delayed work and acquiring a reference to every tcon that needs to be reconnected. Also do not send an echo request on newly established connections.CC Stable <[email protected]>Signed-off-by Pavel Shilovsky <[email protected]>---
fs/cifs/cifsglob.h | 3 +++
fs/cifs/cifsproto.h | 3 +++
fs/cifs/connect.c | 34 +++++++++++++++++++----- fs/cifs/smb2pdu.c | 75 ++++++++++++++++++++++++++++++++++++-------
----------
fs/cifs/smb2proto.h | 1 +5 files changed, 85 insertions(+), 31 deletions(-)fs/cifs/cifsglob.h +0
-0@@ -632,6 +632,7 @@ struct TCP_Server_Info {bool
sec_mskerberos; /* supports
legacy MS Kerberos */
boollarge_buf; /* is current buffer
large? */
structdelayed_workecho; /* echo ping workqueue job
*/fs/cifs/cifsglob.h +1
-0+struct delayed_work reconnect;/* reconnect workqueue job */
char *smallbuf; /* pointer to current "small"
buffer */
char *bigbuf; /* pointer to current "big"
buffer */
unsigned inttotal_read; /* total amount of data read in this pass */fs/cifs/cifsglob.h +1
-0@@ -648,6 +649,7 @@ struct TCP_Server_Info { __u8 preauth_hash[512]; #endif /* CONFIG_CIFS_SMB2 */ unsigned long echo_interval; +struct mutex reconnect_mutex;/* prevent simultaneous reconnects */
};
static inline unsigned intfs/cifs/cifsglob.h +1
-0@@ -850,6 +852,7 @@ struct cifs_tcon { struct list_head tcon_list; int tc_count; struct list_head openFileList; +struct list_head rlist;/* reconnect list */
spinlock_topen_file_lock; /* protects list above */
structcifs_ses*ses; /* pointer to session
associated with */
char treeName[MAX_TREE_SIZE + 1]; /* UNC name of resource in ASCII */fs/cifs/cifsproto.h +0
-0@@ -206,6 +206,9 @@ extern void cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink,struct
cifs_pending_open*open);
extern voidcifs_del_pending_open(struct cifs_pending_open *open);fs/cifs/cifsproto.h +3
-0+extern void cifs_put_tcp_session (struct TCP_Server_Info *server, + int from_reconnect); +extern void cifs_put_tcon (struct cifs_tcon *tcon);
#ifIS_ENABLED(CONFIG_CIFS_DFS_UPCALL)extern voidcifs_dfs_release_automount_timer(void);fs/cifs/connect.c +3
-0@@ -52,6 +52,9 @@ #include "nterr.h" #include "rfc1002pdu.h" #include "fscache.h" +#ifdef CONFIG_CIFS_SMB2 +#include "smb2proto.h" +#endif
#defineCIFS_PORT445
#defineRFC1001_PORT139fs/cifs/connect.c +0
-0@@ -2100,8 +2103,8 @@ cifs_find_tcp_session(struct smb_vol *vol) return NULL; }fs/cifs/connect.c +2
-2-static void -cifs_put_tcp_session (struct TCP_Server_Info *server) +void +cifs_put_tcp_session(struct TCP_Server_Info *server, int
from_reconnect)
{
structtask_struct*task;fs/cifs/connect.c +0
-0@@ -2118,6 +2121,19 @@ cifs_put_tcp_session(struct TCP_Server_Info *server)
cancel_delayed_work_sync(&server->echo);fs/cifs/connect.c +5
-0+#ifdef CONFIG_CIFS_SMB2 +if (from_reconnect) +/* +* Avoid deadlock here: reconnect work calls +* cifs_put_tcp_session() at its end. Need to be sure
fs/cifs/connect.c +1
-0+ * that reconnect work does nothing with serverpointer after
fs/cifs/connect.c cifs_put_tcp_session(struct TCP_Server_Info *server) · line 2135
Quoted 6 lines
fs/cifs/connect.c +6
-0+* that step. +*/ +cancel_delayed_work (&server->reconnect); + else + cancel_delayed_work_sync(&server->reconnect); +#endif
Do you need the #ifdef CONFIG_CIFS_SMB2 here considering that there are no such macro definitions around the declarations around delayed_work reconnect in struct TCP_Server_Info. It would also lead to unused variable in case the kernel is compiled without CONFIG_CIFS_SMB2.
Quoted 175 lines
+
spin_lock(&GlobalMid_Lock);
server->tcpStatus= CifsExiting;
spin_unlock(&GlobalMid_Lock);fs/cifs/connect.c +1
-0@@ -2171,6 +2187,7 @@ cifs_get_tcp_session(struct smb_vol *volume_info) init_waitqueue_head(&tcp_ses->request_q); INIT_LIST_HEAD(&tcp_ses->pending_mid_q); mutex_init(&tcp_ses->srv_mutex); +mutex_init (&tcp_ses->reconnect_mutex); memcpy(tcp_ses->workstation_RFC1001_name, volume_info->source_rfc1001_name,
RFC1001_NAME_LEN_WITH_NULL);
memcpy(tcp_ses->server_RFC1001_name,fs/cifs/connect.c +3
-0@@ -2182,6 +2199,9 @@ cifs_get_tcp_session(struct smb_vol *volume_info) INIT_LIST_HEAD(&tcp_ses->tcp_ses_list); INIT_LIST_HEAD(&tcp_ses->smb_ses_list); INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request); +#ifdef CONFIG_CIFS_SMB2 +INIT_DELAYED_WORK (&tcp_ses->reconnect, smb2_reconnect_server); +#endif memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,sizeof(tcp_ses->srcaddr)); memcpy(&tcp_ses->dstaddr,
&volume_info->dstaddr,fs/cifs/connect.c +0
-0@@ -2340,7 +2360,7 @@ cifs_put_smb_ses(struct cifs_ses *ses) spin_unlock(&cifs_tcp_ses_lock);
sesInfoFree(ses);fs/cifs/connect.c +1
-1-cifs_put_tcp_session (server); +cifs_put_tcp_session (server,0 );}
#ifdefCONFIG_KEYSfs/cifs/connect.c +0
-0@@ -2514,7 +2534,7 @@ cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info) mutex_unlock(&ses->session_mutex);
/* existing SMB ses has a server reference already */fs/cifs/connect.c +1
-1-cifs_put_tcp_session (server); +cifs_put_tcp_session (server,0 );
free_xid(xid);
return ses;
}fs/cifs/connect.c +0
-0@@ -2604,7 +2624,7 @@ cifs_find_tcon(struct cifs_ses *ses, const char *unc) return NULL; }fs/cifs/connect.c +1
-1-static void +void cifs_put_tcon(struct
cifs_tcon*tcon)
{
unsigned int xid;fs/cifs/connect.c +0
-0@@ -3792,7 +3812,7 @@ cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info) else if (ses) cifs_put_smb_ses(ses);else
fs/cifs/connect.c +1
-1-cifs_put_tcp_session (server); +cifs_put_tcp_session (server,0 );
bdi_destroy(&cifs_sb->bdi);
}fs/cifs/connect.c +1
-1@@ -4103,7 +4123,7 @@ cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid) ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info); if (IS_ERR(ses)) { tcon = (struct cifs_tcon *)ses; -cifs_put_tcp_session (master_tcon->ses->server); +cifs_put_tcp_session (master_tcon->ses->server,0 ); goto out; }fs/cifs/smb2pdu.c +0
-0@@ -1972,6 +1972,54 @@ smb2_echo_callback(struct mid_q_entry *mid) add_credits(server, credits_received, CIFS_ECHO_OP); }fs/cifs/smb2pdu.c +4
-0+void smb2_reconnect_server (struct work_struct *work) +{ + struct TCP_Server_Info *server = container_of(work, + struct TCP_Server_Info,reconnect.work);
fs/cifs/smb2pdu.c +6
-0+struct cifs_ses *ses; +struct cifs_tcon *tcon, *tcon2; +struct list_head tmp_list; +int tcon_exist = false; + +/* Prevent simultaneous reconnects that can corrupt tcon- Quoted 1 line
rlist list */
fs/cifs/smb2pdu.c +7
-0+mutex_lock (&server->reconnect_mutex); + +INIT_LIST_HEAD (&tmp_list); +cifs_dbg (FYI ,"Need negotiate, reconnecting tcons\n" ); + +spin_lock (&cifs_tcp_ses_lock); + list_for_each_entry(ses, &server->smb_ses_list,
smb_ses_list) {fs/cifs/smb2pdu.c +1
-0+ list_for_each_entry(tcon, &ses->tcon_list,
tcon_list) {fs/cifs/smb2pdu.c +3
-0+if (tcon && tcon->need_reconnect) { + tcon->tc_count++; + list_add_tail(&tcon->rlist,
&tmp_list);fs/cifs/smb2pdu.c +6
-0+ tcon_exist = true; + } + } + } +/* +* Get the reference to server struct to be sure that the last call of
fs/cifs/smb2pdu.c +1
-0+ * cifs_put_tcon() in the loop below won't release theserver pointer.
fs/cifs/smb2pdu.c +20
-0+*/ +if (tcon_exist) + server->srv_count++; + +spin_unlock (&cifs_tcp_ses_lock); + +list_for_each_entry_safe (tcon, tcon2, &tmp_list, rlist) { +smb2_reconnect (SMB2_ECHO , tcon); +list_del_init (&tcon->rlist); +cifs_put_tcon (tcon); + } + +cifs_dbg (FYI ,"Reconnecting tcons finished\n" ); +mutex_unlock (&server->reconnect_mutex); + +/* now we can safely release srv struct */ +if (tcon_exist) +cifs_put_tcp_session (server,1 ); +} +int
SMB2_echo(structTCP_Server_Info*server)
{fs/cifs/smb2pdu.c +0
-0@@ -1984,32 +2032,11 @@ SMB2_echo(struct TCP_Server_Info *server) cifs_dbg(FYI, "In echo request\n");
if (server->tcpStatus== CifsNeedNegotiate) {fs/cifs/smb2pdu.c +0
-5-struct list_head *tmp, *tmp2; -struct cifs_ses *ses; -struct cifs_tcon *tcon; - - cifs_dbg(FYI, "Need negotiate, reconnectingtcons\n");
fs/cifs/smb2pdu.c +0
-3-spin_lock (&cifs_tcp_ses_lock); -list_for_each (tmp, &server->smb_ses_list) { - ses = list_entry(tmp, struct cifs_ses,
smb_ses_list);fs/cifs/smb2pdu.c +0
-2-list_for_each (tmp2, &ses->tcon_list) { - tcon = list_entry(tmp2, struct
cifs_tcon,fs/cifs/smb2pdu.c +0
-2- tcon_list); -/* add check for persistent handle reconnect */
fs/cifs/smb2pdu.c +0
-2-if (tcon && tcon->need_reconnect) { - spin_unlock(&cifs_tcp_ses_lock);
fs/cifs/smb2pdu.c +0
-1- rc =
smb2_reconnect(SMB2_ECHO, tcon);fs/cifs/smb2pdu.c +0
-1- spin_lock(&cifs_tcp_ses_lock);
fs/cifs/smb2pdu.c +1
-4- } - } - } -spin_unlock (&cifs_tcp_ses_lock); +/* No need to send echo on newly established connections */
fs/cifs/smb2pdu.c SMB2_echo(struct TCP_Server_Info *server) · line 2037
Quoted 2 lines
fs/cifs/smb2pdu.c +2
-0+mod_delayed_work (cifsiod_wq, &server->reconnect,0 ); +return rc;
Shouldn't this be queue_work?
Quoted 15 lines
}
fs/cifs/smb2pdu.c +0
-4-/* if no session, renegotiate failed above */ -if (server->tcpStatus == CifsNeedNegotiate) -return -EIO ; -rc =
small_smb2_init(SMB2_ECHO, NULL, (void **)&req); if (rc)
return rc;fs/cifs/smb2proto.h +0
-0@@ -96,6 +96,7 @@ extern int smb2_open_file(const unsigned int xid, extern int smb2_unlock_range(struct cifsFileInfo *cfile,
Quoted 8 lines
struct
file_lock*flock, const unsigned int xid);
extern intsmb2_push_mandatory_locks(struct cifsFileInfo *cfile); +extern voidsmb2_reconnect_server(struct work_struct *work);
/*
* SMB2 Worker functions - most of protocol specific implementation details
Sachin Prabhu
2016-11-30 2:00 GMT-08:00 Sachin Prabhu <[email protected]>:
Quoted 115 lines · Pavel Shilovsky
Quoted 114 lines
We can not unlock/lock
cifs_tcp_ses_lockwhile walking through ses and tcon lists because it can corrupt list iterator pointers and a tcon structure can be released if we don't hold an extra reference. Fix it by moving a reconnect process to a separate delayed work and acquiring a reference to every tcon that needs to be reconnected. Also do not send an echo request on newly established connections.CC Stable <[email protected]>Signed-off-by Pavel Shilovsky <[email protected]>--- fs/cifs/cifsglob.h | 3 +++ fs/cifs/cifsproto.h | 3 +++ fs/cifs/connect.c | 34 +++++++++++++++++++----- fs/cifs/smb2pdu.c | 75 ++++++++++++++++++++++++++++++++++++------- ---------- fs/cifs/smb2proto.h | 1 + 5 files changed, 85 insertions(+), 31 deletions(-)fs/cifs/cifsglob.h +0
-0@@ -632,6 +632,7 @@ struct TCP_Server_Info {bool sec_mskerberos;/* supports legacy MS Kerberos */
boollarge_buf; /* is current buffer
large? */
structdelayed_workecho; /* echo ping workqueue job
*/
+ structdelayed_workreconnect; /* reconnect workqueue
job */
char *smallbuf; /* pointer to current "small"
buffer */
char *bigbuf; /* pointer to current "big"
buffer */
unsigned inttotal_read; /* total amount of data read in
this pass */fs/cifs/cifsglob.h +1
-0@@ -648,6 +649,7 @@ struct TCP_Server_Info {__u8 preauth_hash[512 ];#endif /* CONFIG_CIFS_SMB2 */ unsigned long echo_interval; +struct mutex reconnect_mutex;/* prevent simultaneous reconnects */
};static inline unsigned int
fs/cifs/cifsglob.h +1
-0@@ -850,6 +852,7 @@ struct cifs_tcon {struct list_head tcon_list;int tc_count;struct list_head openFileList; +struct list_head rlist;/* reconnect list */ spinlock_t open_file_lock;/* protects list above */ struct cifs_ses *ses;/* pointer to session associated with */
char treeName[MAX_TREE_SIZE + 1]; /* UNC name of resource in
ASCII */fs/cifs/cifsproto.h +3
-0@@ -206,6 +206,9 @@ extern void cifs_add_pending_open_locked(structcifs_fid *fid, struct tcon_link *tlink, struct cifs_pending_open *open);extern void cifs_del_pending_open (struct cifs_pending_open *open); +extern void cifs_put_tcp_session (struct TCP_Server_Info *server, +int from_reconnect); +extern void cifs_put_tcon (struct cifs_tcon *tcon);#if
IS_ENABLED(CONFIG_CIFS_DFS_UPCALL)extern voidcifs_dfs_release_automount_timer(void);fs/cifs/connect.c +10
-2@@ -52,6 +52,9 @@#include "nterr.h" #include "rfc1002pdu.h" #include "fscache.h" +#ifdef CONFIG_CIFS_SMB2 +#include "smb2proto.h" +#endif #define CIFS_PORT 445#define RFC1001_PORT 139@@ -2100,8 +2103,8 @@ cifs_find_tcp_session(struct smb_vol *vol)return NULL ; } -static void -cifs_put_tcp_session (struct TCP_Server_Info *server) +void +cifs_put_tcp_session (struct TCP_Server_Info *server,int from_reconnect) {struct task_struct *task;@@ -2118,6 +2121,19 @@ cifs_put_tcp_session(struct TCP_Server_Info *server)cancel_delayed_work_sync (&server->echo); +#ifdef CONFIG_CIFS_SMB2 +if (from_reconnect) +/* +* Avoid deadlock here: reconnect work calls +* cifs_put_tcp_session() at its end. Need to be sure
+ * that reconnect work does nothing with server
pointer afterfs/cifs/connect.c +3
-0+* that step. +*/ +cancel_delayed_work (&server->reconnect);
Quoted 8 lines
fs/cifs/connect.c cifs_put_tcp_session(struct TCP_Server_Info *server) · line 2135
Quoted 3 lines
fs/cifs/connect.c +3
-0+ else + cancel_delayed_work_sync(&server->reconnect); +#endif Do you need the #ifdef
CONFIG_CIFS_SMB2here considering that there are no such macro definitions around the declarations arounddelayed_workreconnect in structTCP_Server_Info. It would also lead to unused variable in case the kernel is compiled withoutCONFIG_CIFS_SMB2.
Thank you for reviewing this.
I posted the next version of the patch yesterday ([PATCH 4/5] CIFS:
Fix a possible memory corruption during reconnect) that addresses this problem by moving fields inside TCP_Server_Info to CONFIG_CIFS_SMB2.
Quoted 172 lines
Quoted 172 lines
fs/cifs/connect.c +2
-0+spin_lock (&GlobalMid_Lock); server->tcpStatus = CifsExiting;spin_unlock (&GlobalMid_Lock);@@ -2171,6 +2187,7 @@ cifs_get_tcp_session(struct smb_vol *volume_info)init_waitqueue_head (&tcp_ses->request_q);INIT_LIST_HEAD (&tcp_ses->pending_mid_q);mutex_init (&tcp_ses->srv_mutex); +mutex_init (&tcp_ses->reconnect_mutex); memcpy(tcp_ses->workstation_RFC1001_name, volume_info->source_rfc1001_name,RFC1001_NAME_LEN_WITH_NULL); memcpy(tcp_ses->server_RFC1001_name,fs/cifs/connect.c +3
-0@@ -2182,6 +2199,9 @@ cifs_get_tcp_session(struct smb_vol *volume_info)INIT_LIST_HEAD (&tcp_ses->tcp_ses_list);INIT_LIST_HEAD (&tcp_ses->smb_ses_list);INIT_DELAYED_WORK (&tcp_ses->echo, cifs_echo_request); +#ifdef CONFIG_CIFS_SMB2 +INIT_DELAYED_WORK (&tcp_ses->reconnect, smb2_reconnect_server); +#endif memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,sizeof(tcp_ses->srcaddr)); memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,fs/cifs/connect.c +2
-2@@ -2340,7 +2360,7 @@ cifs_put_smb_ses(struct cifs_ses *ses)spin_unlock (&cifs_tcp_ses_lock);sesInfoFree (ses); -cifs_put_tcp_session (server); +cifs_put_tcp_session (server,0 ); }#ifdef CONFIG_KEYS @@ -2514,7 +2534,7 @@ cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)mutex_unlock (&ses->session_mutex);/* existing SMB ses has a server reference already */ -cifs_put_tcp_session (server); +cifs_put_tcp_session (server,0 );free_xid (xid);return ses; }fs/cifs/connect.c +1
-1@@ -2604,7 +2624,7 @@ cifs_find_tcon(struct cifs_ses *ses, const char *unc)return NULL ; } -static void +void cifs_put_tcon (struct cifs_tcon *tcon) {unsigned int xid;fs/cifs/connect.c +2
-2@@ -3792,7 +3812,7 @@ cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info) else if (ses) cifs_put_smb_ses(ses); else - cifs_put_tcp_session(server); + cifs_put_tcp_session(server, 0); bdi_destroy(&cifs_sb->bdi); }@@ -4103,7 +4123,7 @@ cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid) ses =cifs_get_smb_ses (master_tcon->ses->server, vol_info);if (IS_ERR (ses)) { tcon = (struct cifs_tcon *)ses; -cifs_put_tcp_session (master_tcon->ses->server); +cifs_put_tcp_session (master_tcon->ses->server,0 ); goto out; }fs/cifs/smb2pdu.c +10
-0@@ -1972,6 +1972,54 @@ smb2_echo_callback(struct mid_q_entry *mid)add_credits (server, credits_received,CIFS_ECHO_OP ); } +void smb2_reconnect_server (struct work_struct *work) +{ +struct TCP_Server_Info *server =container_of (work, + struct TCP_Server_Info, reconnect.work); +struct cifs_ses *ses; +struct cifs_tcon *tcon, *tcon2; +struct list_head tmp_list; +int tcon_exist = false; + +/* Prevent simultaneous reconnects that can corrupt tcon- Quoted 1 line
rlist list */
fs/cifs/smb2pdu.c +7
-0+mutex_lock (&server->reconnect_mutex); + +INIT_LIST_HEAD (&tmp_list); +cifs_dbg (FYI ,"Need negotiate, reconnecting tcons\n" ); + +spin_lock (&cifs_tcp_ses_lock); + list_for_each_entry(ses, &server->smb_ses_list,
smb_ses_list) {
+ list_for_each_entry(tcon,&ses->tcon_list,tcon_list) {fs/cifs/smb2pdu.c +3
-0+if (tcon && tcon->need_reconnect) { + tcon->tc_count++; + list_add_tail(&tcon->rlist,
&tmp_list);fs/cifs/smb2pdu.c +6
-0+ tcon_exist = true; + } + } + } +/* +* Get the reference to server struct to be sure that the last call of
+ *cifs_put_tcon()in the loop below won't release the
server pointer.fs/cifs/smb2pdu.c +20
-12+*/ +if (tcon_exist) + server->srv_count++; + +spin_unlock (&cifs_tcp_ses_lock); + +list_for_each_entry_safe (tcon, tcon2, &tmp_list, rlist) { +smb2_reconnect (SMB2_ECHO , tcon); +list_del_init (&tcon->rlist); +cifs_put_tcon (tcon); + } + +cifs_dbg (FYI ,"Reconnecting tcons finished\n" ); +mutex_unlock (&server->reconnect_mutex); + +/* now we can safely release srv struct */ +if (tcon_exist) +cifs_put_tcp_session (server,1 ); +} +int SMB2_echo (struct TCP_Server_Info *server) {@@ -1984,32 +2032,11 @@ SMB2_echo(struct TCP_Server_Info *server)cifs_dbg (FYI ,"In echo request\n" );if (server->tcpStatus == CifsNeedNegotiate) { -struct list_head *tmp, *tmp2; -struct cifs_ses *ses; -struct cifs_tcon *tcon; - -cifs_dbg (FYI ,"Need negotiate, reconnecting tcons\n"); -spin_lock (&cifs_tcp_ses_lock); -list_for_each (tmp, &server->smb_ses_list) { - ses = list_entry(tmp, struct cifs_ses, smb_ses_list); -list_for_each (tmp2, &ses->tcon_list) { - tcon = list_entry(tmp2, struct cifs_tcon, - tcon_list); -/* add check for persistent handle reconnect */
fs/cifs/smb2pdu.c +0
-2-if (tcon && tcon->need_reconnect) { - spin_unlock(&cifs_tcp_ses_lock);
- rc =smb2_reconnect(SMB2_ECHO, tcon);
- spin_lock(&cifs_tcp_ses_lock
);fs/cifs/smb2pdu.c +0
-2- } - }
Quoted 8 lines
fs/cifs/smb2pdu.c SMB2_echo(struct TCP_Server_Info *server) · line 2037
Quoted 6 lines
fs/cifs/smb2pdu.c +1
-2- } -spin_unlock (&cifs_tcp_ses_lock); +/* No need to send echo on newly established connections */
fs/cifs/smb2pdu.c +2
-0+mod_delayed_work (cifsiod_wq, &server->reconnect,0 ); +return rc;Shouldn't this be
queue_work?
It is made it this way because of the following: if someone submitted the work with a delay previously (now the code doesn't do it but it is possible in the future), we want to overwrite this delay to 0 since we need an immediate session/tcon reconnect here.
Quoted 25 lines
Quoted 21 lines
}fs/cifs/smb2pdu.c +0
-4-/* if no session, renegotiate failed above */ -if (server->tcpStatus == CifsNeedNegotiate) -return -EIO ; - rc =small_smb2_init (SMB2_ECHO ,NULL , (void **)&req);if (rc)return rc;fs/cifs/smb2proto.h +1
-0@@ -96,6 +96,7 @@ extern int smb2_open_file(const unsigned int xid,extern int smb2_unlock_range (struct cifsFileInfo *cfile,struct file_lock *flock,const unsigned int xid);extern int smb2_push_mandatory_locks (struct cifsFileInfo *cfile); +extern void smb2_reconnect_server (struct work_struct *work);/* Quoted 2 lines
* SMB2 Worker functions - most of protocol specific implementation
detailsSachin Prabhu
Signature · 2 lines
Best regards, Pavel Shilovsky
Quoted 8 lines · Pavel Shilovsky
Quoted 6 lines
Do you need the #ifdef
CONFIG_CIFS_SMB2here considering that there are
no such macro definitions around the declarations arounddelayed_work
reconnect in structTCP_Server_Info. It would also lead to unused variable in case the kernel is compiled withoutCONFIG_CIFS_SMB2.
Quoted 7 lines
Thank you for reviewing this.
I posted the next version of the patch yesterday ([PATCH 4/5] CIFS:
Fix a possible memory corruption during reconnect) that addresses this
problem by moving fields insideTCP_Server_InfotoCONFIG_CIFS_SMB2.
Thanks. I'll review the patch separately.
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
+ /* No need to send echo on newly established connections */
+ mod_delayed_work(cifsiod_wq,&server->reconnect, 0);
+ return rc;Shouldn't this be
queue_work?
Quoted 7 lines
It is made it this way because of the following: if someone submitted the work with a delay previously (now the code doesn't do it but it is
possible in the future), we want to overwrite this delay to 0 since we
need an immediate session/tcon reconnect here.
OK. It should work without any problem but I would have stuck to the expected functions and use mod_delayed_work() only when new code which queues the work separately has been added. It is strange to see only mod_delayed_work() when the work isn't queued anywhere else. Apart from that small concern, I am fine with it.
Sachin Prabhu
2016-12-01 1:35 GMT-08:00 Sachin Prabhu <[email protected]>:
Quoted 36 lines · Pavel Shilovsky
Quoted 8 lines
Quoted 7 lines
Do you need the #ifdef
CONFIG_CIFS_SMB2here considering that there are
no such macro definitions around the declarations arounddelayed_work
reconnect in structTCP_Server_Info. It would also lead to unused variable in case the kernel is compiled withoutCONFIG_CIFS_SMB2.Quoted 7 lines
Thank you for reviewing this.
I posted the next version of the patch yesterday ([PATCH 4/5] CIFS:
Fix a possible memory corruption during reconnect) that addresses this
problem by moving fields insideTCP_Server_InfotoCONFIG_CIFS_SMB2.Thanks. I'll review the patch separately.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
+ /* No need to send echo on newly established
connections */
+ mod_delayed_work(cifsiod_wq,&server->reconnect,
0);
+ return rc;Quoted 9 lines
Quoted 1 line
Shouldn't this be
queue_work?It is made it this way because of the following: if someone submitted the work with a delay previously (now the code doesn't do it but it is
possible in the future), we want to overwrite this delay to 0 since we
need an immediate session/tcon reconnect here.
Quoted 7 lines
OK. It should work without any problem but I would have stuck to the expected functions and use
mod_delayed_work()only when new code which queues the work separately has been added. It is strange to see onlymod_delayed_work()when the work isn't queued anywhere else. Apart from that small concern, I am fine with it.Sachin Prabhu
Ok, it makes sense. I have posted the next version (v2) of the patchset that addresses your suggestions to the list.
Signature · 2 lines
Best regards, Pavel Shilovsky