Commit 31d94d26 authored by Dave Wysochanski's avatar Dave Wysochanski Committed by Stefan Bader

cifs: Fix cifsInodeInfo lock_sem deadlock when reconnect occurs

BugLink: https://bugs.launchpad.net/bugs/1852110

[ Upstream commit d46b0da7 ]

There's a deadlock that is possible and can easily be seen with
a test where multiple readers open/read/close of the same file
and a disruption occurs causing reconnect.  The deadlock is due
a reader thread inside cifs_strict_readv calling down_read and
obtaining lock_sem, and then after reconnect inside
cifs_reopen_file calling down_read a second time.  If in
between the two down_read calls, a down_write comes from
another process, deadlock occurs.

        CPU0                    CPU1
        ----                    ----
cifs_strict_readv()
 down_read(&cifsi->lock_sem);
                               _cifsFileInfo_put
                                  OR
                               cifs_new_fileinfo
                                down_write(&cifsi->lock_sem);
cifs_reopen_file()
 down_read(&cifsi->lock_sem);

Fix the above by changing all down_write(lock_sem) calls to
down_write_trylock(lock_sem)/msleep() loop, which in turn
makes the second down_read call benign since it will never
block behind the writer while holding lock_sem.
Signed-off-by: default avatarDave Wysochanski <dwysocha@redhat.com>
Suggested-by: default avatarRonnie Sahlberg <lsahlber@redhat.com>
Reviewed--by: default avatarRonnie Sahlberg <lsahlber@redhat.com>
Reviewed-by: default avatarPavel Shilovsky <pshilov@microsoft.com>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Signed-off-by: default avatarConnor Kuehl <connor.kuehl@canonical.com>
Signed-off-by: default avatarKhalid Elmously <khalid.elmously@canonical.com>
parent c4799779
...@@ -1187,6 +1187,11 @@ void cifsFileInfo_put(struct cifsFileInfo *cifs_file); ...@@ -1187,6 +1187,11 @@ void cifsFileInfo_put(struct cifsFileInfo *cifs_file);
struct cifsInodeInfo { struct cifsInodeInfo {
bool can_cache_brlcks; bool can_cache_brlcks;
struct list_head llist; /* locks helb by this inode */ struct list_head llist; /* locks helb by this inode */
/*
* NOTE: Some code paths call down_read(lock_sem) twice, so
* we must always use use cifs_down_write() instead of down_write()
* for this semaphore to avoid deadlocks.
*/
struct rw_semaphore lock_sem; /* protect the fields above */ struct rw_semaphore lock_sem; /* protect the fields above */
/* BB add in lists for dirty pages i.e. write caching info for oplock */ /* BB add in lists for dirty pages i.e. write caching info for oplock */
struct list_head openFileList; struct list_head openFileList;
......
...@@ -145,6 +145,7 @@ extern int cifs_unlock_range(struct cifsFileInfo *cfile, ...@@ -145,6 +145,7 @@ extern int cifs_unlock_range(struct cifsFileInfo *cfile,
struct file_lock *flock, const unsigned int xid); struct file_lock *flock, const unsigned int xid);
extern int cifs_push_mandatory_locks(struct cifsFileInfo *cfile); extern int cifs_push_mandatory_locks(struct cifsFileInfo *cfile);
extern void cifs_down_write(struct rw_semaphore *sem);
extern struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, extern struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid,
struct file *file, struct file *file,
struct tcon_link *tlink, struct tcon_link *tlink,
......
...@@ -280,6 +280,13 @@ cifs_has_mand_locks(struct cifsInodeInfo *cinode) ...@@ -280,6 +280,13 @@ cifs_has_mand_locks(struct cifsInodeInfo *cinode)
return has_locks; return has_locks;
} }
void
cifs_down_write(struct rw_semaphore *sem)
{
while (!down_write_trylock(sem))
msleep(10);
}
struct cifsFileInfo * struct cifsFileInfo *
cifs_new_fileinfo(struct cifs_fid *fid, struct file *file, cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
struct tcon_link *tlink, __u32 oplock) struct tcon_link *tlink, __u32 oplock)
...@@ -305,7 +312,7 @@ cifs_new_fileinfo(struct cifs_fid *fid, struct file *file, ...@@ -305,7 +312,7 @@ cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
INIT_LIST_HEAD(&fdlocks->locks); INIT_LIST_HEAD(&fdlocks->locks);
fdlocks->cfile = cfile; fdlocks->cfile = cfile;
cfile->llist = fdlocks; cfile->llist = fdlocks;
down_write(&cinode->lock_sem); cifs_down_write(&cinode->lock_sem);
list_add(&fdlocks->llist, &cinode->llist); list_add(&fdlocks->llist, &cinode->llist);
up_write(&cinode->lock_sem); up_write(&cinode->lock_sem);
...@@ -438,7 +445,7 @@ void cifsFileInfo_put(struct cifsFileInfo *cifs_file) ...@@ -438,7 +445,7 @@ void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
* Delete any outstanding lock records. We'll lose them when the file * Delete any outstanding lock records. We'll lose them when the file
* is closed anyway. * is closed anyway.
*/ */
down_write(&cifsi->lock_sem); cifs_down_write(&cifsi->lock_sem);
list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) { list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
list_del(&li->llist); list_del(&li->llist);
cifs_del_lock_waiters(li); cifs_del_lock_waiters(li);
...@@ -947,7 +954,7 @@ static void ...@@ -947,7 +954,7 @@ static void
cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock) cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
{ {
struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
down_write(&cinode->lock_sem); cifs_down_write(&cinode->lock_sem);
list_add_tail(&lock->llist, &cfile->llist->locks); list_add_tail(&lock->llist, &cfile->llist->locks);
up_write(&cinode->lock_sem); up_write(&cinode->lock_sem);
} }
...@@ -969,7 +976,7 @@ cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock, ...@@ -969,7 +976,7 @@ cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
try_again: try_again:
exist = false; exist = false;
down_write(&cinode->lock_sem); cifs_down_write(&cinode->lock_sem);
exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length, exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
lock->type, &conf_lock, CIFS_LOCK_OP); lock->type, &conf_lock, CIFS_LOCK_OP);
...@@ -991,7 +998,7 @@ cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock, ...@@ -991,7 +998,7 @@ cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
(lock->blist.next == &lock->blist)); (lock->blist.next == &lock->blist));
if (!rc) if (!rc)
goto try_again; goto try_again;
down_write(&cinode->lock_sem); cifs_down_write(&cinode->lock_sem);
list_del_init(&lock->blist); list_del_init(&lock->blist);
} }
...@@ -1044,7 +1051,7 @@ cifs_posix_lock_set(struct file *file, struct file_lock *flock) ...@@ -1044,7 +1051,7 @@ cifs_posix_lock_set(struct file *file, struct file_lock *flock)
return rc; return rc;
try_again: try_again:
down_write(&cinode->lock_sem); cifs_down_write(&cinode->lock_sem);
if (!cinode->can_cache_brlcks) { if (!cinode->can_cache_brlcks) {
up_write(&cinode->lock_sem); up_write(&cinode->lock_sem);
return rc; return rc;
...@@ -1242,7 +1249,7 @@ cifs_push_locks(struct cifsFileInfo *cfile) ...@@ -1242,7 +1249,7 @@ cifs_push_locks(struct cifsFileInfo *cfile)
int rc = 0; int rc = 0;
/* we are going to update can_cache_brlcks here - need a write access */ /* we are going to update can_cache_brlcks here - need a write access */
down_write(&cinode->lock_sem); cifs_down_write(&cinode->lock_sem);
if (!cinode->can_cache_brlcks) { if (!cinode->can_cache_brlcks) {
up_write(&cinode->lock_sem); up_write(&cinode->lock_sem);
return rc; return rc;
...@@ -1430,7 +1437,7 @@ cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock, ...@@ -1430,7 +1437,7 @@ cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
if (!buf) if (!buf)
return -ENOMEM; return -ENOMEM;
down_write(&cinode->lock_sem); cifs_down_write(&cinode->lock_sem);
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
cur = buf; cur = buf;
num = 0; num = 0;
......
...@@ -138,7 +138,7 @@ smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock, ...@@ -138,7 +138,7 @@ smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
cur = buf; cur = buf;
down_write(&cinode->lock_sem); cifs_down_write(&cinode->lock_sem);
list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
if (flock->fl_start > li->offset || if (flock->fl_start > li->offset ||
(flock->fl_start + length) < (flock->fl_start + length) <
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment