Commit b891fa50 authored by Eric Ren's avatar Eric Ren Committed by Linus Torvalds

ocfs2: fix deadlock issue when taking inode lock at vfs entry points

Commit 743b5f14 ("ocfs2: take inode lock in ocfs2_iop_set/get_acl()")
results in a deadlock, as the author "Tariq Saeed" realized shortly
after the patch was merged.  The discussion happened here

  https://oss.oracle.com/pipermail/ocfs2-devel/2015-September/011085.html

The reason why taking cluster inode lock at vfs entry points opens up a
self deadlock window, is explained in the previous patch of this series.

So far, we have seen two different code paths that have this issue.

1. do_sys_open
     may_open
      inode_permission
       ocfs2_permission
        ocfs2_inode_lock() <=== take PR
         generic_permission
          get_acl
           ocfs2_iop_get_acl
            ocfs2_inode_lock() <=== take PR

2. fchmod|fchmodat
    chmod_common
     notify_change
      ocfs2_setattr <=== take EX
       posix_acl_chmod
        get_acl
         ocfs2_iop_get_acl <=== take PR
        ocfs2_iop_set_acl <=== take EX

Fixes them by adding the tracking logic (in the previous patch) for these
funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(),
ocfs2_setattr().

Link: http://lkml.kernel.org/r/20170117100948.11657-3-zren@suse.comSigned-off-by: default avatarEric Ren <zren@suse.com>
Reviewed-by: default avatarJunxiao Bi <junxiao.bi@oracle.com>
Reviewed-by: default avatarJoseph Qi <jiangqi903@gmail.com>
Cc: Mark Fasheh <mfasheh@versity.com>
Cc: Joel Becker <jlbec@evilplan.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 439a36b8
...@@ -283,16 +283,14 @@ int ocfs2_set_acl(handle_t *handle, ...@@ -283,16 +283,14 @@ int ocfs2_set_acl(handle_t *handle,
int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl *acl, int type) int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl *acl, int type)
{ {
struct buffer_head *bh = NULL; struct buffer_head *bh = NULL;
int status = 0; int status, had_lock;
struct ocfs2_lock_holder oh;
status = ocfs2_inode_lock(inode, &bh, 1); had_lock = ocfs2_inode_lock_tracker(inode, &bh, 1, &oh);
if (status < 0) { if (had_lock < 0)
if (status != -ENOENT) return had_lock;
mlog_errno(status);
return status;
}
status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL); status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
ocfs2_inode_unlock(inode, 1); ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
brelse(bh); brelse(bh);
return status; return status;
} }
...@@ -302,21 +300,20 @@ struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type) ...@@ -302,21 +300,20 @@ struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
struct ocfs2_super *osb; struct ocfs2_super *osb;
struct buffer_head *di_bh = NULL; struct buffer_head *di_bh = NULL;
struct posix_acl *acl; struct posix_acl *acl;
int ret; int had_lock;
struct ocfs2_lock_holder oh;
osb = OCFS2_SB(inode->i_sb); osb = OCFS2_SB(inode->i_sb);
if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL)) if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
return NULL; return NULL;
ret = ocfs2_inode_lock(inode, &di_bh, 0);
if (ret < 0) { had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 0, &oh);
if (ret != -ENOENT) if (had_lock < 0)
mlog_errno(ret); return ERR_PTR(had_lock);
return ERR_PTR(ret);
}
acl = ocfs2_get_acl_nolock(inode, type, di_bh); acl = ocfs2_get_acl_nolock(inode, type, di_bh);
ocfs2_inode_unlock(inode, 0); ocfs2_inode_unlock_tracker(inode, 0, &oh, had_lock);
brelse(di_bh); brelse(di_bh);
return acl; return acl;
} }
......
...@@ -1138,6 +1138,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) ...@@ -1138,6 +1138,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
handle_t *handle = NULL; handle_t *handle = NULL;
struct dquot *transfer_to[MAXQUOTAS] = { }; struct dquot *transfer_to[MAXQUOTAS] = { };
int qtype; int qtype;
int had_lock;
struct ocfs2_lock_holder oh;
trace_ocfs2_setattr(inode, dentry, trace_ocfs2_setattr(inode, dentry,
(unsigned long long)OCFS2_I(inode)->ip_blkno, (unsigned long long)OCFS2_I(inode)->ip_blkno,
...@@ -1173,11 +1175,30 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) ...@@ -1173,11 +1175,30 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
} }
} }
status = ocfs2_inode_lock(inode, &bh, 1); had_lock = ocfs2_inode_lock_tracker(inode, &bh, 1, &oh);
if (status < 0) { if (had_lock < 0) {
if (status != -ENOENT) status = had_lock;
mlog_errno(status);
goto bail_unlock_rw; goto bail_unlock_rw;
} else if (had_lock) {
/*
* As far as we know, ocfs2_setattr() could only be the first
* VFS entry point in the call chain of recursive cluster
* locking issue.
*
* For instance:
* chmod_common()
* notify_change()
* ocfs2_setattr()
* posix_acl_chmod()
* ocfs2_iop_get_acl()
*
* But, we're not 100% sure if it's always true, because the
* ordering of the VFS entry points in the call chain is out
* of our control. So, we'd better dump the stack here to
* catch the other cases of recursive locking.
*/
mlog(ML_ERROR, "Another case of recursive locking:\n");
dump_stack();
} }
inode_locked = 1; inode_locked = 1;
...@@ -1260,8 +1281,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) ...@@ -1260,8 +1281,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
bail_commit: bail_commit:
ocfs2_commit_trans(osb, handle); ocfs2_commit_trans(osb, handle);
bail_unlock: bail_unlock:
if (status) { if (status && inode_locked) {
ocfs2_inode_unlock(inode, 1); ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
inode_locked = 0; inode_locked = 0;
} }
bail_unlock_rw: bail_unlock_rw:
...@@ -1279,7 +1300,7 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) ...@@ -1279,7 +1300,7 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
mlog_errno(status); mlog_errno(status);
} }
if (inode_locked) if (inode_locked)
ocfs2_inode_unlock(inode, 1); ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
brelse(bh); brelse(bh);
return status; return status;
...@@ -1320,21 +1341,32 @@ int ocfs2_getattr(struct vfsmount *mnt, ...@@ -1320,21 +1341,32 @@ int ocfs2_getattr(struct vfsmount *mnt,
int ocfs2_permission(struct inode *inode, int mask) int ocfs2_permission(struct inode *inode, int mask)
{ {
int ret; int ret, had_lock;
struct ocfs2_lock_holder oh;
if (mask & MAY_NOT_BLOCK) if (mask & MAY_NOT_BLOCK)
return -ECHILD; return -ECHILD;
ret = ocfs2_inode_lock(inode, NULL, 0); had_lock = ocfs2_inode_lock_tracker(inode, NULL, 0, &oh);
if (ret) { if (had_lock < 0) {
if (ret != -ENOENT) ret = had_lock;
mlog_errno(ret);
goto out; goto out;
} else if (had_lock) {
/* See comments in ocfs2_setattr() for details.
* The call chain of this case could be:
* do_sys_open()
* may_open()
* inode_permission()
* ocfs2_permission()
* ocfs2_iop_get_acl()
*/
mlog(ML_ERROR, "Another case of recursive locking:\n");
dump_stack();
} }
ret = generic_permission(inode, mask); ret = generic_permission(inode, mask);
ocfs2_inode_unlock(inode, 0); ocfs2_inode_unlock_tracker(inode, 0, &oh, had_lock);
out: out:
return ret; return ret;
} }
......
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