ceph: fix pending vmtruncate race
PATCH 1/3 ceph: fix pending vmtruncate race
From: "Yan, Zheng" <[email protected]>
The locking order for pending vmtruncate is wrong, it can lead to following race:
write wmtruncate work
------------------------ ----------------------
lock i_mutex
check i_truncate_pending check i_truncate_pendingtruncate_inode_pages() lock i_mutex (blocked)
copy data to page cache
unlock i_mutex
truncate_inode_pages()
The fix is take i_mutex before calling __ceph_do_pending_vmtruncate()
--- fs/ceph/caps.c | 6 +++++- fs/ceph/file.c | 2 +- fs/ceph/inode.c | 14 ++++++-------- fs/ceph/super.h | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-)
fs/ceph/caps.c +5 -1
@@ -2057,7 +2057,11 @@ static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,/* finish pending truncate */ while (ci->i_truncate_pending) {spin_unlock (&ci->i_ceph_lock); -__ceph_do_pending_vmtruncate (inode, !(need &CEPH_CAP_FILE_WR )); +if (!(need &CEPH_CAP_FILE_WR )) +mutex_lock (&inode->i_mutex); +__ceph_do_pending_vmtruncate (inode); +if (!(need &CEPH_CAP_FILE_WR )) +mutex_unlock (&inode->i_mutex);spin_lock (&ci->i_ceph_lock); }
fs/ceph/file.c +1 -1
@@ -822,7 +822,7 @@ static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)int ret;mutex_lock (&inode->i_mutex); -__ceph_do_pending_vmtruncate (inode, false); +__ceph_do_pending_vmtruncate (inode);if (whence ==SEEK_END || whence ==SEEK_DATA || whence ==SEEK_HOLE ) { ret =ceph_do_getattr (inode,CEPH_STAT_CAP_SIZE );
fs/ceph/inode.c +6 -8
@@ -1465,7 +1465,9 @@ static void ceph_vmtruncate_work(struct work_struct *work)struct inode *inode = &ci->vfs_inode;dout ("vmtruncate_work %p\n" , inode); -__ceph_do_pending_vmtruncate (inode, true); +mutex_lock (&inode->i_mutex); +__ceph_do_pending_vmtruncate (inode); +mutex_unlock (&inode->i_mutex);iput (inode); }
@@ -1492,7 +1494,7 @@ void ceph_queue_vmtruncate(struct inode *inode)* Make sure any pending truncation is applied before doing anything * that may depend on it. */ -void __ceph_do_pending_vmtruncate (struct inode *inode,bool needlock) +void __ceph_do_pending_vmtruncate (struct inode *inode) {struct ceph_inode_info *ci =ceph_inode (inode);u64 to;
@@ -1525,11 +1527,7 @@ retry: ci->i_truncate_pending, to);spin_unlock (&ci->i_ceph_lock); -if (needlock) -mutex_lock (&inode->i_mutex);truncate_inode_pages (inode->i_mapping, to); -if (needlock) -mutex_unlock (&inode->i_mutex);spin_lock (&ci->i_ceph_lock);if (to == ci->i_truncate_size) {
@@ -1588,7 +1586,7 @@ int ceph_setattr(struct dentry *dentry, struct iattr *attr)if (ceph_snap (inode) !=CEPH_NOSNAP )return -EROFS ; -__ceph_do_pending_vmtruncate (inode, false); +__ceph_do_pending_vmtruncate (inode); err =inode_change_ok (inode, attr);if (err !=0 )
@@ -1770,7 +1768,7 @@ int ceph_setattr(struct dentry *dentry, struct iattr *attr) ceph_cap_string(dirtied), mask);ceph_mdsc_put_request (req); -__ceph_do_pending_vmtruncate (inode, false); +__ceph_do_pending_vmtruncate (inode);return err; out:spin_unlock (&ci->i_ceph_lock);
fs/ceph/super.h +1 -1
@@ -692,7 +692,7 @@ extern int ceph_readdir_prepopulate(struct ceph_mds_request *req,extern int ceph_inode_holds_cap (struct inode *inode,int mask);extern int ceph_inode_set_size (struct inode *inode,loff_t size); -extern void __ceph_do_pending_vmtruncate (struct inode *inode,bool needlock); +extern void __ceph_do_pending_vmtruncate (struct inode *inode);extern void ceph_queue_vmtruncate (struct inode *inode);extern void ceph_queue_invalidate (struct inode *inode);
Signature · 1 line
1.8.1.4
PATCH 2/3 ceph: fix cap revoke race
From: "Yan, Zheng" <[email protected]>
If caps are been revoking by the auth MDS, don't consider them as issued even they are still issued by non-auth MDS. The non-auth MDS should also be revoking/exporting these caps, the client just hasn't received the cap revoke/export message.
The race I encountered is: When caps are exporting to new MDS, the client receives cap import message and cap revoke message from the new MDS, then receives cap export message from the old MDS. When the client receives cap revoke message from the new MDS, the revoking caps are still issued by the old MDS, so the client does nothing. Later when the cap export message is received, the client removes the caps issued by the old MDS. (Another way to fix the race is calling ceph_check_caps() in handle_cap_export())
--- fs/ceph/caps.c | 9 +++++++++ 1 file changed, 9 insertions(+)
fs/ceph/caps.c +9 -0
@@ -690,6 +690,15 @@ int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)if (implemented) *implemented |= cap->implemented; } +/* +* exclude caps issued by non-auth MDS, but are been revoking +* by the auth MDS. The non-auth MDS should be revoking/exporting +* these caps, but the message is delayed. +*/ +if (ci->i_auth_cap) { + cap = ci->i_auth_cap; + have &= ~cap->implemented | cap->issued; + }return have; }
Signature · 1 line
1.8.1.4
Re: [PATCH 2/3] ceph: fix cap revoke race
Quoted 45 lines · Zheng
From: "Yan, Zheng" <[email protected]>
If caps are been revoking by the auth MDS, don't consider them as issued even they are still issued by non-auth MDS. The non-auth MDS should also be revoking/exporting these caps, the client just hasn't received the cap revoke/export message.
The race I encountered is: When caps are exporting to new MDS, the client receives cap import message and cap revoke message from the new MDS, then receives cap export message from the old MDS. When the client receives cap revoke message from the new MDS, the revoking caps are still issued by the old MDS, so the client does nothing. Later when the cap export message is received, the client removes the caps issued by the old MDS. (Another way to fix the race is calling
ceph_check_caps()inhandle_cap_export())Signed-off-by Yan, Zheng <[email protected]>--- fs/ceph/caps.c | 9 +++++++++ 1 file changed, 9 insertions(+)fs/ceph/caps.c +9
-0@@ -690,6 +690,15 @@ int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)if (implemented) *implemented |= cap->implemented; } +/* +* exclude caps issued by non-auth MDS, but are been revoking +* by the auth MDS. The non-auth MDS should be revoking/exporting +* these caps, but the message is delayed. +*/ +if (ci->i_auth_cap) { + cap = ci->i_auth_cap; + have &= ~cap->implemented | cap->issued; + }return have; }Signature · 1 line
1.8.1.4
PATCH 3/3 ceph: fix race between cap issue and revoke
In reply to Yan, ZhengFrom: "Yan, Zheng" <[email protected]>
If we receive new caps from the auth MDS and the non-auth MDS is revoking the newly issued caps, we should release the caps from the non-auth MDS. The scenario is filelock's state changes from SYNC to LOCK. Non-auth MDS revokes Fc cap, the client gets Fc cap from the auth MDS at the same time.
--- fs/ceph/caps.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)
fs/ceph/caps.c +20 -9
@@ -806,22 +806,28 @@ int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)/* * Return true if mask caps are currently being revoked by an MDS. */ -int ceph_caps_revoking (struct ceph_inode_info *ci,int mask) +int __ceph_caps_revoking_other (struct ceph_inode_info *ci, +struct ceph_cap *ocap,int mask) { -struct inode *inode = &ci->vfs_inode;struct ceph_cap *cap;struct rb_node *p; -int ret =0 ; -spin_lock (&ci->i_ceph_lock);for (p =rb_first (&ci->i_caps); p; p =rb_next (p)) { cap = rb_entry(p, struct ceph_cap, ci_node); -if (__cap_is_valid (cap) && - (cap->implemented & ~cap->issued & mask)) { - ret =1 ; -break ; - } +if (cap != ocap &&__cap_is_valid (cap) && + (cap->implemented & ~cap->issued & mask)) +return 1 ; } +return 0 ; +} + +int ceph_caps_revoking (struct ceph_inode_info *ci,int mask) +{ +struct inode *inode = &ci->vfs_inode; +int ret; + +spin_lock (&ci->i_ceph_lock); + ret =__ceph_caps_revoking_other (ci,NULL , mask);spin_unlock (&ci->i_ceph_lock);dout ("ceph_caps_revoking %p %s = %d\n" , inode,ceph_cap_string (mask), ret);
@@ -2488,6 +2494,11 @@ static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant, }else {dout ("grant: %s -> %s\n" ,ceph_cap_string (cap->issued),ceph_cap_string (newcaps)); +/* non-auth MDS is revoking the newly grant caps ? */ +if (cap == ci->i_auth_cap && +__ceph_caps_revoking_other (ci, cap, newcaps)) + check_caps =2 ; + cap->issued = newcaps; cap->implemented |= newcaps;/* add bits only, to * avoid stepping on a
Signature · 1 line
1.8.1.4
Re: [PATCH 3/3] ceph: fix race between cap issue and revoke
Quoted 71 lines · Zheng
From: "Yan, Zheng" <[email protected]>
If we receive new caps from the auth MDS and the non-auth MDS is revoking the newly issued caps, we should release the caps from the non-auth MDS. The scenario is filelock's state changes from SYNC to LOCK. Non-auth MDS revokes Fc cap, the client gets Fc cap from the auth MDS at the same time.
Signed-off-by Yan, Zheng <[email protected]>--- fs/ceph/caps.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)fs/ceph/caps.c +20
-9@@ -806,22 +806,28 @@ int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)/* * Return true if mask caps are currently being revoked by an MDS. */ -int ceph_caps_revoking (struct ceph_inode_info *ci,int mask) +int __ceph_caps_revoking_other (struct ceph_inode_info *ci, +struct ceph_cap *ocap,int mask) { -struct inode *inode = &ci->vfs_inode;struct ceph_cap *cap;struct rb_node *p; -int ret =0 ; -spin_lock (&ci->i_ceph_lock);for (p =rb_first (&ci->i_caps); p; p =rb_next (p)) { cap = rb_entry(p, struct ceph_cap, ci_node); -if (__cap_is_valid (cap) && - (cap->implemented & ~cap->issued & mask)) { - ret =1 ; -break ; - } +if (cap != ocap &&__cap_is_valid (cap) && + (cap->implemented & ~cap->issued & mask)) +return 1 ; } +return 0 ; +} + +int ceph_caps_revoking (struct ceph_inode_info *ci,int mask) +{ +struct inode *inode = &ci->vfs_inode; +int ret; + +spin_lock (&ci->i_ceph_lock); + ret =__ceph_caps_revoking_other (ci,NULL , mask);spin_unlock (&ci->i_ceph_lock);dout ("ceph_caps_revoking %p %s = %d\n" , inode,ceph_cap_string (mask), ret);@@ -2488,6 +2494,11 @@ static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant, }else {dout ("grant: %s -> %s\n" ,ceph_cap_string (cap->issued),ceph_cap_string (newcaps)); +/* non-auth MDS is revoking the newly grant caps ? */ +if (cap == ci->i_auth_cap && +__ceph_caps_revoking_other (ci, cap, newcaps)) + check_caps =2 ; + cap->issued = newcaps; cap->implemented |= newcaps;/* add bits only, to * avoid stepping on a Signature · 1 line
1.8.1.4
Quoted 30 lines · Zheng
From: "Yan, Zheng" <[email protected]>
The locking order for pending vmtruncate is wrong, it can lead to following race:
write wmtruncate work
------------------------ ----------------------
locki_mutex
checki_truncate_pendingchecki_truncate_pendingtruncate_inode_pages()locki_mutex(blocked)
copy data to page cache
unlocki_mutex
truncate_inode_pages()The fix is take
i_mutexbefore calling__ceph_do_pending_vmtruncate()Fixes #5453Signed-off-by Yan, Zheng <[email protected]>--- fs/ceph/caps.c | 6 +++++- fs/ceph/file.c | 2 +- fs/ceph/inode.c | 14 ++++++-------- fs/ceph/super.h | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-)fs/ceph/caps.c +0
-0
fs/ceph/caps.c static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want, · line 2064
Quoted 9 lines
fs/ceph/caps.c +5
-1@@ -2057,7 +2057,11 @@ static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,/* finish pending truncate */ while (ci->i_truncate_pending) {spin_unlock (&ci->i_ceph_lock); -__ceph_do_pending_vmtruncate (inode, !(need &CEPH_CAP_FILE_WR )); +if (!(need &CEPH_CAP_FILE_WR )) +mutex_lock (&inode->i_mutex); +__ceph_do_pending_vmtruncate (inode); +if (!(need &CEPH_CAP_FILE_WR )) +mutex_unlock (&inode->i_mutex);
At some point I think we should make this implicit "if you need WR you must have already locked i_mutex" behavior more explicit (locked_imutex flag arg to try_get_cap_refs() maybe). This is no worse than before, though.
Quoted 87 lines
fs/ceph/caps.c +0
-0spin_lock (&ci->i_ceph_lock); }fs/ceph/file.c +1
-1@@ -822,7 +822,7 @@ static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)int ret;mutex_lock (&inode->i_mutex); -__ceph_do_pending_vmtruncate (inode, false); +__ceph_do_pending_vmtruncate (inode);if (whence ==SEEK_END || whence ==SEEK_DATA || whence ==SEEK_HOLE ) { ret =ceph_do_getattr (inode,CEPH_STAT_CAP_SIZE );fs/ceph/inode.c +6
-8@@ -1465,7 +1465,9 @@ static void ceph_vmtruncate_work(struct work_struct *work)struct inode *inode = &ci->vfs_inode;dout ("vmtruncate_work %p\n" , inode); -__ceph_do_pending_vmtruncate (inode, true); +mutex_lock (&inode->i_mutex); +__ceph_do_pending_vmtruncate (inode); +mutex_unlock (&inode->i_mutex);iput (inode); }@@ -1492,7 +1494,7 @@ void ceph_queue_vmtruncate(struct inode *inode)* Make sure any pending truncation is applied before doing anything * that may depend on it. */ -void __ceph_do_pending_vmtruncate (struct inode *inode,bool needlock) +void __ceph_do_pending_vmtruncate (struct inode *inode) {struct ceph_inode_info *ci =ceph_inode (inode);u64 to;@@ -1525,11 +1527,7 @@ retry: ci->i_truncate_pending, to);spin_unlock (&ci->i_ceph_lock); -if (needlock) -mutex_lock (&inode->i_mutex);truncate_inode_pages (inode->i_mapping, to); -if (needlock) -mutex_unlock (&inode->i_mutex);spin_lock (&ci->i_ceph_lock);if (to == ci->i_truncate_size) {@@ -1588,7 +1586,7 @@ int ceph_setattr(struct dentry *dentry, struct iattr *attr)if (ceph_snap (inode) !=CEPH_NOSNAP )return -EROFS ; -__ceph_do_pending_vmtruncate (inode, false); +__ceph_do_pending_vmtruncate (inode); err =inode_change_ok (inode, attr);if (err !=0 )@@ -1770,7 +1768,7 @@ int ceph_setattr(struct dentry *dentry, struct iattr *attr) ceph_cap_string(dirtied), mask);ceph_mdsc_put_request (req); -__ceph_do_pending_vmtruncate (inode, false); +__ceph_do_pending_vmtruncate (inode);return err; out:spin_unlock (&ci->i_ceph_lock);fs/ceph/super.h +1
-1@@ -692,7 +692,7 @@ extern int ceph_readdir_prepopulate(struct ceph_mds_request *req,extern int ceph_inode_holds_cap (struct inode *inode,int mask);extern int ceph_inode_set_size (struct inode *inode,loff_t size); -extern void __ceph_do_pending_vmtruncate (struct inode *inode,bool needlock); +extern void __ceph_do_pending_vmtruncate (struct inode *inode);extern void ceph_queue_vmtruncate (struct inode *inode);extern void ceph_queue_invalidate (struct inode *inode);Signature · 1 line
1.8.1.4