ceph: cleanup of processing ci->i_ceph_flags bits
Subsystems (2)
Viacheslav Dubeyko
maintainerPATCH v3 1/1 ceph: cleanup of processing ci->i_ceph_flags bits
From: Viacheslav Dubeyko <[email protected]>
The Coverity Scan service has detected potential
race condition in ceph_check_delayed_caps() [1].
The CID 1590633 contains explanation: "Accessing ci->i_ceph_flags without holding lock ceph_inode_info.i_ceph_lock. The value of the shared data will be determined by the interleaving of thread execution. Thread shared data is accessed without holding an appropriate lock, possibly causing a race condition (CWE-366)".
The patch reworks the logic of accessing ci->i_ceph_flags. At first, it removes ci item from a mdsc->cap_delay_list. Then it unlocks mdsc->cap_delay_lock and it locks ci->i_ceph_lock. Then, it calls smp_mb__before_atomic() to be sure that ci->i_ceph_flags has consistent state of the bits. The is_metadata_under_flush variable stores the state of CEPH_I_FLUSH_BIT. Finally, it unlocks the ci->i_ceph_lock and it locks the mdsc->cap_delay_lock. The is_metadata_under_flush is used to check the condition that ci needs to be removed from mdsc->cap_delay_list. If it is not the case, then ci will be added into the head of mdsc->cap_delay_list.
This patch reworks the logic of checking the CEPH_I_FLUSH_BIT, CEPH_I_FLUSH_SNAPS_BIT, CEPH_I_KICK_FLUSH_BIT, CEPH_ASYNC_CREATE_BIT, CEPH_I_ERROR_FILELOCK_BIT by test_bit() method and calling smp_mb__before_atomic() to ensure that bit state is consistent. It switches on calling the set_bit(), clear_bit() for these bits, and calling smp_mb__after_atomic() after these methods to ensure that modified bit is visible.
Additionally, __must_hold() has been added for __cap_delay_requeue(), __cap_delay_requeue_front(), and __prep_cap() to help the sparse with lock checking and it was commented that caller of __cap_delay_requeue_front() and __prep_cap() must lock the ci->i_ceph_lock.
v.2
Alex Markuze suggested to rework all Ceph inode's flags. Now, every declaration has CEPH_I_<*> and CEPH_I_<*>_BIT pair.
v.3
The logic of operating by ci->i_ceph_flags bits on using test_bit(), clear_bit(), set_bit() and smp_mb__before_atomic(), smp_mb__after_atomic() has been reworked in addr.c, inode.c, locks.c, mds_client.c, snap.c, super.h, xattr.c additionally to caps.c.
[1] https://scan5.scan.coverity.com/#/project-view/64304/10063?selectedIssue=1590633
--- fs/ceph/addr.c | 8 ++- fs/ceph/caps.c | 132 +++++++++++++++++++++++++++++++++---------- fs/ceph/inode.c | 11 +++- fs/ceph/locks.c | 14 +++-- fs/ceph/mds_client.c | 10 +++- fs/ceph/snap.c | 6 +- fs/ceph/super.h | 75 ++++++++++++++++-------- fs/ceph/xattr.c | 11 +++- 8 files changed, 198 insertions(+), 69 deletions(-)
fs/ceph/addr.c +7 -1
@@ -2546,6 +2546,8 @@ int ceph_pool_perm_check(struct inode *inode, int need)return 0 ;spin_lock (&ci->i_ceph_lock); +/* ensure that bit state is consistent */ +smp_mb__before_atomic (); flags = ci->i_ceph_flags; pool = ci->i_layout.pool_id;spin_unlock (&ci->i_ceph_lock);
@@ -2578,8 +2580,12 @@ int ceph_pool_perm_check(struct inode *inode, int need)if (pool == ci->i_layout.pool_id && pool_ns ==rcu_dereference_raw (ci->i_layout.pool_ns)) { ci->i_ceph_flags |= flags; - }else { +/* ensure modified bit is visible */ +smp_mb__after_atomic (); + }else { pool = ci->i_layout.pool_id; +/* ensure that bit state is consistent */ +smp_mb__before_atomic (); flags = ci->i_ceph_flags; }spin_unlock (&ci->i_ceph_lock);
fs/ceph/caps.c +103 -29
300 lines not on this page · raw message
fs/ceph/inode.c +8 -3
@@ -1105,8 +1105,11 @@ int ceph_fill_inode(struct inode *inode, struct page *locked_page, lockdep_is_held(&ci->i_ceph_lock));rcu_assign_pointer (ci->i_layout.pool_ns, pool_ns); -if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns) - ci->i_ceph_flags &= ~CEPH_I_POOL_PERM ; +if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns) { +clear_bit (CEPH_I_POOL_PERM_BIT , &ci->i_ceph_flags); +/* ensure modified bit is visible */ +smp_mb__after_atomic (); + } pool_ns = old_ns;
@@ -3149,7 +3152,9 @@ void ceph_inode_shutdown(struct inode *inode)bool invalidate = false;spin_lock (&ci->i_ceph_lock); - ci->i_ceph_flags |=CEPH_I_SHUTDOWN ; +set_bit (CEPH_I_SHUTDOWN_BIT , &ci->i_ceph_flags); +/* ensure modified bit is visible */ +smp_mb__after_atomic (); p =rb_first (&ci->i_caps);while (p) { struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
fs/ceph/locks.c +9 -5
@@ -58,7 +58,9 @@ static void ceph_fl_release_lock(struct file_lock *fl)if (atomic_dec_and_test (&ci->i_filelock_ref)) {/* clear error when all locks are released */ spin_lock (&ci->i_ceph_lock); - ci->i_ceph_flags &= ~CEPH_I_ERROR_FILELOCK ; +clear_bit (CEPH_I_ERROR_FILELOCK_BIT , &ci->i_ceph_flags); +/* ensure modified bit is visible */ +smp_mb__after_atomic ();spin_unlock (&ci->i_ceph_lock); } fl->fl_u.ceph.inode =NULL ;
@@ -269,9 +271,10 @@ int ceph_lock(struct file *file, int cmd, struct file_lock *fl) wait =1 ;spin_lock (&ci->i_ceph_lock); -if (ci->i_ceph_flags &CEPH_I_ERROR_FILELOCK ) { +/* ensure that bit state is consistent */ +smp_mb__before_atomic (); +if (test_bit (CEPH_I_ERROR_FILELOCK_BIT , &ci->i_ceph_flags)) err = -EIO ; - }spin_unlock (&ci->i_ceph_lock);if (err <0 ) {if (op ==CEPH_MDS_OP_SETFILELOCK &&lock_is_unlock (fl))
@@ -329,9 +332,10 @@ int ceph_flock(struct file *file, int cmd, struct file_lock *fl)doutc (cl,"fl_file: %p\n" , fl->c.flc_file);spin_lock (&ci->i_ceph_lock); -if (ci->i_ceph_flags &CEPH_I_ERROR_FILELOCK ) { +/* ensure that bit state is consistent */ +smp_mb__before_atomic (); +if (test_bit (CEPH_I_ERROR_FILELOCK_BIT , &ci->i_ceph_flags)) err = -EIO ; - }spin_unlock (&ci->i_ceph_lock);if (err <0 ) {if (lock_is_unlock (fl))
fs/ceph/mds_client.c +8 -2
@@ -3549,7 +3549,10 @@ static void __do_request(struct ceph_mds_client *mdsc,spin_lock (&ci->i_ceph_lock); cap = ci->i_auth_cap; -if (ci->i_ceph_flags &CEPH_I_ASYNC_CREATE && mds != cap->mds) { +/* ensure that bit state is consistent */ +smp_mb__before_atomic (); +if (test_bit (CEPH_ASYNC_CREATE_BIT , &ci->i_ceph_flags) && + mds != cap->mds) {doutc (cl,"session changed for auth cap %d -> %d\n" , cap->session->s_mds, session->s_mds);
@@ -4630,8 +4633,11 @@ static int reconnect_caps_cb(struct inode *inode, int mds, void *arg) rec.v2.issued =cpu_to_le32 (cap->issued); rec.v2.snaprealm =cpu_to_le64 (ci->i_snap_realm->ino); rec.v2.pathbase =cpu_to_le64 (pathbase); +/* ensure that bit state is consistent */ +smp_mb__before_atomic (); rec.v2.flock_len = (__force __le32) - ((ci->i_ceph_flags &CEPH_I_ERROR_FILELOCK ) ?0 :1 ); + (test_bit (CEPH_I_ERROR_FILELOCK_BIT , + &ci->i_ceph_flags) ?0 :1 ); }else {struct timespec64 ts;
fs/ceph/snap.c +5 -1
@@ -661,6 +661,7 @@ static void ceph_queue_cap_snap(struct ceph_inode_info *ci,*/ int __ceph_finish_cap_snap (struct ceph_inode_info *ci,struct ceph_cap_snap *capsnap) + __must_hold(ci->i_ceph_lock) {struct inode *inode = &ci->netfs.inode;struct ceph_mds_client *mdsc =ceph_sb_to_mdsc (inode->i_sb);
@@ -700,7 +701,10 @@ int __ceph_finish_cap_snap(struct ceph_inode_info *ci,return 0 ; } - ci->i_ceph_flags |=CEPH_I_FLUSH_SNAPS ; +set_bit (CEPH_I_FLUSH_SNAPS_BIT , &ci->i_ceph_flags); +/* ensure modified bit is visible */ +smp_mb__after_atomic (); + doutc(cl, "%p %llx.%llx cap_snap %p snapc %p %llu %s s=%llu\n", inode, ceph_vinop(inode), capsnap, capsnap->context, capsnap->context->seq, ceph_cap_string(capsnap->dirty),
fs/ceph/super.h +50 -25
102 lines not on this page · raw message
fs/ceph/xattr.c +8 -3
@@ -1055,8 +1055,11 @@ ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,if (current->journal_info && !strncmp (name,XATTR_SECURITY_PREFIX ,XATTR_SECURITY_PREFIX_LEN ) && -security_ismaclabel (name +XATTR_SECURITY_PREFIX_LEN )) - ci->i_ceph_flags |=CEPH_I_SEC_INITED ; +security_ismaclabel (name +XATTR_SECURITY_PREFIX_LEN )) { +set_bit (CEPH_I_SEC_INITED_BIT , &ci->i_ceph_flags); +/* ensure modified bit is visible */ +smp_mb__after_atomic (); + } out:spin_unlock (&ci->i_ceph_lock);return err;
@@ -1366,7 +1369,9 @@ bool ceph_security_xattr_deadlock(struct inode *in)return false; ci =ceph_inode (in);spin_lock (&ci->i_ceph_lock); - ret = !(ci->i_ceph_flags &CEPH_I_SEC_INITED ) && +/* ensure that bit state is consistent */ +smp_mb__before_atomic (); + ret = !test_bit (CEPH_I_SEC_INITED_BIT , &ci->i_ceph_flags) && !(ci->i_xattrs.version >0 &&__ceph_caps_issued_mask (ci,CEPH_CAP_XATTR_SHARED ,0 ));spin_unlock (&ci->i_ceph_lock);
Signature · 1 line
2.50.1