Skip to content

Commit 340cea8

Browse files
sprasad-microsoftsmfrench
authored andcommitted
cifs: open files should not hold ref on superblock
Today whenever we deal with a file, in addition to holding a reference on the dentry, we also get a reference on the superblock. This happens in two cases: 1. when a new cinode is allocated 2. when an oplock break is being processed The reasoning for holding the superblock ref was to make sure that when umount happens, if there are users of inodes and dentries, it does not try to clean them up and wait for the last ref to superblock to be dropped by last of such users. But the side effect of doing that is that umount silently drops a ref on the superblock and we could have deferred closes and lease breaks still holding these refs. Ideally, we should ensure that all of these users of inodes and dentries are cleaned up at the time of umount, which is what this code is doing. This code change allows these code paths to use a ref on the dentry (and hence the inode). That way, umount is ensured to clean up SMB client resources when it's the last ref on the superblock (For ex: when same objects are shared). The code change also moves the call to close all the files in deferred close list to the umount code path. It also waits for oplock_break workers to be flushed before calling kill_anon_super (which eventually frees up those objects). Fixes: 24261fc ("cifs: delay super block destruction until all cifsFileInfo objects are gone") Fixes: 705c791 ("smb: client: fix use-after-free in cifs_oplock_break") Cc: <stable@vger.kernel.org> Signed-off-by: Shyam Prasad N <sprasad@microsoft.com> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent 26bc83b commit 340cea8

5 files changed

Lines changed: 50 additions & 13 deletions

File tree

fs/smb/client/cifsfs.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,14 @@ static void cifs_kill_sb(struct super_block *sb)
332332

333333
/*
334334
* We need to release all dentries for the cached directories
335-
* before we kill the sb.
335+
* and close all deferred file handles before we kill the sb.
336336
*/
337337
if (cifs_sb->root) {
338338
close_all_cached_dirs(cifs_sb);
339+
cifs_close_all_deferred_files_sb(cifs_sb);
340+
341+
/* Wait for all pending oplock breaks to complete */
342+
flush_workqueue(cifsoplockd_wq);
339343

340344
/* finally release root dentry */
341345
dput(cifs_sb->root);
@@ -868,7 +872,6 @@ static void cifs_umount_begin(struct super_block *sb)
868872
spin_unlock(&tcon->tc_lock);
869873
spin_unlock(&cifs_tcp_ses_lock);
870874

871-
cifs_close_all_deferred_files(tcon);
872875
/* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
873876
/* cancel_notify_requests(tcon); */
874877
if (tcon->ses && tcon->ses->server) {

fs/smb/client/cifsproto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ void cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode);
261261

262262
void cifs_close_all_deferred_files(struct cifs_tcon *tcon);
263263

264+
void cifs_close_all_deferred_files_sb(struct cifs_sb_info *cifs_sb);
264265
void cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon,
265266
struct dentry *dentry);
266267

fs/smb/client/file.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,6 @@ struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
711711
mutex_init(&cfile->fh_mutex);
712712
spin_lock_init(&cfile->file_info_lock);
713713

714-
cifs_sb_active(inode->i_sb);
715-
716714
/*
717715
* If the server returned a read oplock and we have mandatory brlocks,
718716
* set oplock level to None.
@@ -767,7 +765,6 @@ static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
767765
struct inode *inode = d_inode(cifs_file->dentry);
768766
struct cifsInodeInfo *cifsi = CIFS_I(inode);
769767
struct cifsLockInfo *li, *tmp;
770-
struct super_block *sb = inode->i_sb;
771768

772769
/*
773770
* Delete any outstanding lock records. We'll lose them when the file
@@ -785,7 +782,6 @@ static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
785782

786783
cifs_put_tlink(cifs_file->tlink);
787784
dput(cifs_file->dentry);
788-
cifs_sb_deactive(sb);
789785
kfree(cifs_file->symlink_target);
790786
kfree(cifs_file);
791787
}
@@ -3163,12 +3159,6 @@ void cifs_oplock_break(struct work_struct *work)
31633159
__u64 persistent_fid, volatile_fid;
31643160
__u16 net_fid;
31653161

3166-
/*
3167-
* Hold a reference to the superblock to prevent it and its inodes from
3168-
* being freed while we are accessing cinode. Otherwise, _cifsFileInfo_put()
3169-
* may release the last reference to the sb and trigger inode eviction.
3170-
*/
3171-
cifs_sb_active(sb);
31723162
wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
31733163
TASK_UNINTERRUPTIBLE);
31743164

@@ -3253,7 +3243,6 @@ void cifs_oplock_break(struct work_struct *work)
32533243
cifs_put_tlink(tlink);
32543244
out:
32553245
cifs_done_oplock_break(cinode);
3256-
cifs_sb_deactive(sb);
32573246
}
32583247

32593248
static int cifs_swap_activate(struct swap_info_struct *sis,

fs/smb/client/misc.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
#include "fs_context.h"
2929
#include "cached_dir.h"
3030

31+
struct tcon_list {
32+
struct list_head entry;
33+
struct cifs_tcon *tcon;
34+
};
35+
3136
/* The xid serves as a useful identifier for each incoming vfs request,
3237
in a similar way to the mid which is useful to track each sent smb,
3338
and CurrentXid can also provide a running counter (although it
@@ -554,6 +559,43 @@ cifs_close_all_deferred_files(struct cifs_tcon *tcon)
554559
}
555560
}
556561

562+
void cifs_close_all_deferred_files_sb(struct cifs_sb_info *cifs_sb)
563+
{
564+
struct rb_root *root = &cifs_sb->tlink_tree;
565+
struct rb_node *node;
566+
struct cifs_tcon *tcon;
567+
struct tcon_link *tlink;
568+
struct tcon_list *tmp_list, *q;
569+
LIST_HEAD(tcon_head);
570+
571+
spin_lock(&cifs_sb->tlink_tree_lock);
572+
for (node = rb_first(root); node; node = rb_next(node)) {
573+
tlink = rb_entry(node, struct tcon_link, tl_rbnode);
574+
tcon = tlink_tcon(tlink);
575+
if (IS_ERR(tcon))
576+
continue;
577+
tmp_list = kmalloc_obj(struct tcon_list, GFP_ATOMIC);
578+
if (tmp_list == NULL)
579+
break;
580+
tmp_list->tcon = tcon;
581+
/* Take a reference on tcon to prevent it from being freed */
582+
spin_lock(&tcon->tc_lock);
583+
++tcon->tc_count;
584+
trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
585+
netfs_trace_tcon_ref_get_close_defer_files);
586+
spin_unlock(&tcon->tc_lock);
587+
list_add_tail(&tmp_list->entry, &tcon_head);
588+
}
589+
spin_unlock(&cifs_sb->tlink_tree_lock);
590+
591+
list_for_each_entry_safe(tmp_list, q, &tcon_head, entry) {
592+
cifs_close_all_deferred_files(tmp_list->tcon);
593+
list_del(&tmp_list->entry);
594+
cifs_put_tcon(tmp_list->tcon, netfs_trace_tcon_ref_put_close_defer_files);
595+
kfree(tmp_list);
596+
}
597+
}
598+
557599
void cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon,
558600
struct dentry *dentry)
559601
{

fs/smb/client/trace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
EM(netfs_trace_tcon_ref_get_cached_laundromat, "GET Ch-Lau") \
177177
EM(netfs_trace_tcon_ref_get_cached_lease_break, "GET Ch-Lea") \
178178
EM(netfs_trace_tcon_ref_get_cancelled_close, "GET Cn-Cls") \
179+
EM(netfs_trace_tcon_ref_get_close_defer_files, "GET Cl-Def") \
179180
EM(netfs_trace_tcon_ref_get_dfs_refer, "GET DfsRef") \
180181
EM(netfs_trace_tcon_ref_get_find, "GET Find ") \
181182
EM(netfs_trace_tcon_ref_get_find_sess_tcon, "GET FndSes") \
@@ -187,6 +188,7 @@
187188
EM(netfs_trace_tcon_ref_put_cancelled_close, "PUT Cn-Cls") \
188189
EM(netfs_trace_tcon_ref_put_cancelled_close_fid, "PUT Cn-Fid") \
189190
EM(netfs_trace_tcon_ref_put_cancelled_mid, "PUT Cn-Mid") \
191+
EM(netfs_trace_tcon_ref_put_close_defer_files, "PUT Cl-Def") \
190192
EM(netfs_trace_tcon_ref_put_mnt_ctx, "PUT MntCtx") \
191193
EM(netfs_trace_tcon_ref_put_dfs_refer, "PUT DfsRfr") \
192194
EM(netfs_trace_tcon_ref_put_reconnect_server, "PUT Reconn") \

0 commit comments

Comments
 (0)