ceph: guard against __ceph_remove_cap races
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): CAAM7YAmquOg5ESMAMa5y0gGAR-UAivYF8m+nqrJNmK=SzG6+wA@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 26 lines · Jeff Layton
Quoted 20 lines · Zheng
Quoted 9 lines · Jeff Layton
I believe it's possible that we could end up with racing calls to
__ceph_remove_capfor the same cap. If that happens, thecap->cipointer will be zereoed out and we can hit a NULL pointer dereference.Once we acquire the
s_cap_lock, check for the ci pointer being NULL, and just return without doing anything if it is.URL: https://tracker.ceph.com/issues/43272
Signed-off-by Jeff Layton <[email protected]>Quoted 9 lines
--- fs/ceph/caps.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)This is the only scenario that made sense to me in light of Ilya's analysis on the tracker above. I could be off here though -- the locking around this code is horrifically complex, and I could be missing what should guard against this scenario.
Quoted 4 lines
I think the simpler fix is, in
trim_caps_cb, check if cap-ci is non-null before calling__ceph_remove_cap(). this should work because__ceph_remove_cap()is always called insidei_ceph_lock
Quoted 4 lines
Is that sufficient though? The stack trace in the bug shows it being called by
ceph_trim_caps, but I think we could hit the same problem with other__ceph_remove_capcallers, if they happen to race in at the right time.
Sorry for resurrecting this old thread, but we just got a report with this issue on a kernel that includes commit d6e47819721a ("ceph: hold i_ceph_lock when removing caps for freeing inode").
Looking at the code, I believe Zheng's suggestion should work as I don't see any __ceph_remove_cap callers that don't hold the i_ceph_lock. So, would something like the diff bellow be acceptable?
Cheers,
Signature · 1 line
Luis
fs/ceph/mds_client.c +2 -1
@@ -1960,7 +1960,8 @@ static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)if (oissued) {/* we aren't the only cap.. just remove us */ -__ceph_remove_cap (cap, true); +if (cap->ci) +__ceph_remove_cap (cap, true); (*remaining)--; }else {struct dentry *dentry;
Quoted 60 lines
Quoted 60 lines
Quoted 60 lines
Thoughts?
fs/ceph/caps.c +16
-6@@ -1046,11 +1046,22 @@ static void drop_inode_snap_realm(struct ceph_inode_info *ci)void __ceph_remove_cap (struct ceph_cap *cap,bool queue_release) {struct ceph_mds_session *session = cap->session; -struct ceph_inode_info *ci = cap->ci; -struct ceph_mds_client *mdsc = -ceph_sb_to_client (ci->vfs_inode.i_sb)->mdsc; +struct ceph_inode_info *ci; +struct ceph_mds_client *mdsc;int removed =0 ; +spin_lock (&session->s_cap_lock); + ci = cap->ci; +if (!ci) { +/* +* Did we race with a competing __ceph_remove_cap call? If +* ci is zeroed out, then just unlock and don't do anything. +* Assume that it's on its way out anyway. +*/ +spin_unlock (&session->s_cap_lock); +return ; + } +dout ("__ceph_remove_cap %p from %p\n" , cap, &ci->vfs_inode);/* remove from inode's cap rbtree, and clear auth cap */ @@ -1058,13 +1069,12 @@ void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)if (ci->i_auth_cap == cap) ci->i_auth_cap =NULL ; -/* remove from session list */ -spin_lock (&session->s_cap_lock);if (session->s_cap_iterator == cap) {/* not yet, we are iterating over this very cap */ dout ("__ceph_remove_cap delaying %p removal from session %p\n" , cap, cap->session); }else { +/* remove from session list */ list_del_init (&cap->session_caps); session->s_nr_caps--; cap->session =NULL ;@@ -1072,6 +1082,7 @@ void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) }/* protect backpointer with s_cap_lock: see iterate_session_caps */ cap->ci =NULL ; + mdsc =ceph_sb_to_client (ci->vfs_inode.i_sb)->mdsc;/* * s_cap_reconnect is protected by s_cap_lock. no one changes -- 2.23.0
Quoted 37 lines · Luis Henriques
Quoted 26 lines · Jeff Layton
Quoted 20 lines · Zheng
Quoted 9 lines · Jeff Layton
I believe it's possible that we could end up with racing calls to
__ceph_remove_capfor the same cap. If that happens, thecap->cipointer will be zereoed out and we can hit a NULL pointer dereference.Once we acquire the
s_cap_lock, check for the ci pointer being NULL, and just return without doing anything if it is.URL: https://tracker.ceph.com/issues/43272
Signed-off-by Jeff Layton <[email protected]>Quoted 9 lines
---
fs/ceph/caps.c | 21 ++++++++++++++++-----1 file changed, 16 insertions(+), 5 deletions(-)This is the only scenario that made sense to me in light of Ilya's analysis on the tracker above. I could be off here though -- the locking around this code is horrifically complex, and I could be missing what should guard against this scenario.
Quoted 4 lines
I think the simpler fix is, in
trim_caps_cb, check if cap-ci is non-null before calling__ceph_remove_cap(). this should work because__ceph_remove_cap()is always called insidei_ceph_lockQuoted 4 lines
Is that sufficient though? The stack trace in the bug shows it being called by
ceph_trim_caps, but I think we could hit the same problem with other__ceph_remove_capcallers, if they happen to race in at the right time.Sorry for resurrecting this old thread, but we just got a report with this issue on a kernel that includes commit
d6e47819721a("ceph: holdi_ceph_lockwhen removing caps for freeing inode").
Quoted 5 lines
Looking at the code, I believe Zheng's suggestion should work as I don't see any
__ceph_remove_capcallers that don't hold thei_ceph_lock. So, would something like the diff bellow be acceptable?Cheers,
I'm still not convinced that's the correct fix.
Why would trim_caps_cb be subject to this race when other __ceph_remove_cap callers are not? Maybe the right fix is to test for a NULL cap->ci in __ceph_remove_cap and just return early if it is?
Signature · 1 line
Jeff Layton <[email protected]>
Quoted 42 lines · Jeff Layton
Quoted 41 lines · Luis Henriques
Quoted 26 lines · Jeff Layton
Quoted 20 lines · Zheng
Quoted 9 lines · Jeff Layton
I believe it's possible that we could end up with racing calls to
__ceph_remove_capfor the same cap. If that happens, thecap->cipointer will be zereoed out and we can hit a NULL pointer dereference.Once we acquire the
s_cap_lock, check for the ci pointer being NULL, and just return without doing anything if it is.URL: https://tracker.ceph.com/issues/43272
Signed-off-by Jeff Layton <[email protected]>Quoted 9 lines
---
fs/ceph/caps.c | 21 ++++++++++++++++-----1 file changed, 16 insertions(+), 5 deletions(-)This is the only scenario that made sense to me in light of Ilya's analysis on the tracker above. I could be off here though -- the locking around this code is horrifically complex, and I could be missing what should guard against this scenario.
Quoted 4 lines
I think the simpler fix is, in
trim_caps_cb, check if cap-ci is non-null before calling__ceph_remove_cap(). this should work because__ceph_remove_cap()is always called insidei_ceph_lockQuoted 4 lines
Is that sufficient though? The stack trace in the bug shows it being called by
ceph_trim_caps, but I think we could hit the same problem with other__ceph_remove_capcallers, if they happen to race in at the right time.Sorry for resurrecting this old thread, but we just got a report with this issue on a kernel that includes commit
d6e47819721a("ceph: holdi_ceph_lockwhen removing caps for freeing inode").Looking at the code, I believe Zheng's suggestion should work as I don't see any
__ceph_remove_capcallers that don't hold thei_ceph_lock. So, would something like the diff bellow be acceptable?
Quoted 7 lines
Quoted 1 line
Cheers,
I'm still not convinced that's the correct fix.
Why would
trim_caps_cbbe subject to this race when other__ceph_remove_capcallers are not? Maybe the right fix is to test for a NULLcap->ciin__ceph_remove_capand just return early if it is?
I see, you're probably right. Looking again at the code I see that there are two possible places where this race could occur, and they're both used as callbacks in ceph_iterate_session_caps. They are trim_caps_cb and remove_session_caps_cb.
These callbacks get the struct ceph_cap as argument and only then they will acquire i_ceph_lock. Since this isn't protected with session->s_cap_lock, I guess this could be where the race window is, where cap->ci can be set to NULL.
Bellow is the patch you suggested. If you think that's acceptable I can resend with a proper commit message.
Cheers,
Signature · 1 line
Luis
fs/ceph/caps.c +7 -2
@@ -1140,12 +1140,17 @@ void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) {struct ceph_mds_session *session = cap->session;struct ceph_inode_info *ci = cap->ci; -struct ceph_mds_client *mdsc = -ceph_sb_to_client (ci->vfs_inode.i_sb)->mdsc; +struct ceph_mds_client *mdsc; +int removed =0 ; +if (!ci) +return ; +dout ("__ceph_remove_cap %p from %p\n" , cap, &ci->vfs_inode); + mdsc =ceph_sb_to_client (ci->vfs_inode.i_sb)->mdsc; +/* remove from inode's cap rbtree, and clear auth cap */ rb_erase (&cap->ci_node, &ci->i_caps);if (ci->i_auth_cap == cap) {
Quoted 2 lines · Luis Henriques
It think this looks reasonable. Minor nits below:
fs/ceph/caps.c void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) · line 1144
Quoted 12 lines
fs/ceph/caps.c +2
-2@@ -1140,12 +1140,17 @@ void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) { struct ceph_mds_session *session = cap->session; struct ceph_inode_info *ci = cap->ci; - struct ceph_mds_client *mdsc = - ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; + struct ceph_mds_client *mdsc; +
nit: remove the above newline
Quoted 2 lines
fs/ceph/caps.c +0
-0int removed = 0;
Maybe add a comment here to the effect that a NULL cap->ci indicates that the remove has already been done?
Quoted 7 lines
+ if (!ci)
+ return;
+
dout("__ceph_remove_cap %p from %p\n", cap,&ci->vfs_inode);
+ mdsc = ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; +
There's a ceph_inode_to_client helper now that may make this a bit more readable.
Quoted 3 lines
/* remove from inode's cap rbtree, and clear auth cap */
rb_erase(&cap->ci_node, &ci->i_caps);
if (ci->i_auth_cap== cap) {
Signature · 1 line
Jeff Layton <[email protected]>
Quoted 27 lines · Jeff Layton
Quoted 2 lines · Luis Henriques
It think this looks reasonable. Minor nits below:
fs/ceph/caps.c void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) · line 1144
Quoted 12 lines
fs/ceph/caps.c +2
-2@@ -1140,12 +1140,17 @@ void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) { struct ceph_mds_session *session = cap->session; struct ceph_inode_info *ci = cap->ci; - struct ceph_mds_client *mdsc = - ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; + struct ceph_mds_client *mdsc; +nit: remove the above newline
Quoted 2 lines
fs/ceph/caps.c +0
-0int removed = 0;Maybe add a comment here to the effect that a NULL
cap->ciindicates that the remove has already been done?
Quoted 14 lines
Quoted 7 lines
+ if (!ci)
+ return;
+
dout("__ceph_remove_cap %p from %p\n", cap,&ci->vfs_inode);
+ mdsc = ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; +There's a
ceph_inode_to_clienthelper now that may make this a bit more readable.Quoted 3 lines
/* remove from inode's cap rbtree, and clear auth cap */
rb_erase(&cap->ci_node, &ci->i_caps);
if (ci->i_auth_cap== cap) {
Thanks Jeff. I'll re-post this soon with your suggestions. I just want to run some more local tests to make sure things aren't breaking with this change.
Cheers,
Signature · 1 line
Luis