Skip to contentSkip to conversation

fs/ceph:fix double unlock in handle_cap_export()

PATCH Superseded Wu Bo 3 participants 6 messages Apr 28, 2020 – Apr 30, 2020 Ceph client

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wu Bo

author
#0 lore raw

PATCH v2 1/1 fs/ceph:fix double unlock in handle_cap_export()

if the ceph_mdsc_open_export_target_session() return fails, should add a lock to avoid twice unlocking.
Because the lock will be released at the retry or out_unlock tag.

--
v1 -> v2:
add spin_lock(&ci->i_ceph_lock) before goto out_unlock tag.

---
 fs/ceph/caps.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)
fs/ceph/caps.c +15 -12
index 185db76..414c0e2 100644
@@ -3731,22 +3731,25 @@ static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
 
 	/* open target session */
 	tsession = ceph_mdsc_open_export_target_session(mdsc, target);
-	if (!IS_ERR(tsession)) {
-		if (mds > target) {
-			mutex_lock(&session->s_mutex);
-			mutex_lock_nested(&tsession->s_mutex,
-					  SINGLE_DEPTH_NESTING);
-		} else {
-			mutex_lock(&tsession->s_mutex);
-			mutex_lock_nested(&session->s_mutex,
-					  SINGLE_DEPTH_NESTING);
-		}
-		new_cap = ceph_get_cap(mdsc, NULL);
-	} else {
+	if (IS_ERR(tsession)) {
 		WARN_ON(1);
 		tsession = NULL;
 		target = -1;
+		mutex_lock(&session->s_mutex);
+		spin_lock(&ci->i_ceph_lock);
+		goto out_unlock;
+	}
+
+	if (mds > target) {
+		mutex_lock(&session->s_mutex);
+		mutex_lock_nested(&tsession->s_mutex,
+					SINGLE_DEPTH_NESTING);
+	} else {
+		mutex_lock(&tsession->s_mutex);
+		mutex_lock_nested(&session->s_mutex,
+					SINGLE_DEPTH_NESTING);
 	}
+	new_cap = ceph_get_cap(mdsc, NULL);
 	goto retry;
 
 out_unlock:
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 4 lines · Wu Bo

if the ceph_mdsc_open_export_target_session() return fails, should add a lock to avoid twice unlocking.
Because the lock will be released at the retry or out_unlock tag.

The problem looks real, but...

Quoted 26 lines

--
v1 -> v2:
add spin_lock(&ci->i_ceph_lock) before goto out_unlock tag.

---
 fs/ceph/caps.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)
fs/ceph/caps.c +0 -9
index 185db76..414c0e2 100644
@@ -3731,22 +3731,25 @@ static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
 
 	/* open target session */
 	tsession = ceph_mdsc_open_export_target_session(mdsc, target);
-	if (!IS_ERR(tsession)) {
-		if (mds > target) {
-			mutex_lock(&session->s_mutex);
-			mutex_lock_nested(&tsession->s_mutex,
-					  SINGLE_DEPTH_NESTING);
-		} else {
-			mutex_lock(&tsession->s_mutex);
-			mutex_lock_nested(&session->s_mutex,
-					  SINGLE_DEPTH_NESTING);

fs/ceph/caps.c static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, · line 3740

Quoted 10 lines
fs/ceph/caps.c +4 -3
-		}
-		new_cap = ceph_get_cap(mdsc, NULL);
-	} else {
+	if (IS_ERR(tsession)) {
 		WARN_ON(1);
 		tsession = NULL;
 		target = -1;
+		mutex_lock(&session->s_mutex);
+		spin_lock(&ci->i_ceph_lock);
+		goto out_unlock;

Why did you make this case goto out_unlock instead of retrying as it did before?

Quoted 15 lines
fs/ceph/caps.c +11 -0
+	}
+
+	if (mds > target) {
+		mutex_lock(&session->s_mutex);
+		mutex_lock_nested(&tsession->s_mutex,
+					SINGLE_DEPTH_NESTING);
+	} else {
+		mutex_lock(&tsession->s_mutex);
+		mutex_lock_nested(&session->s_mutex,
+					SINGLE_DEPTH_NESTING);
 	}
+	new_cap = ceph_get_cap(mdsc, NULL);
 	goto retry;
 
 out_unlock:
Signature · 1 line
Jeff Layton <[email protected]>
author
#2 lore raw
Quoted 39 lines · Jeff Layton
Quoted 4 lines · Wu Bo

if the ceph_mdsc_open_export_target_session() return fails, should add a lock to avoid twice unlocking.
Because the lock will be released at the retry or out_unlock tag.

The problem looks real, but...

Quoted 31 lines

--
v1 -> v2:
add spin_lock(&ci->i_ceph_lock) before goto out_unlock tag.

---
  fs/ceph/caps.c | 27 +++++++++++++++------------
  1 file changed, 15 insertions(+), 12 deletions(-)
fs/ceph/caps.c +1 -12
index 185db76..414c0e2 100644
@@ -3731,22 +3731,25 @@ static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
 
  	/* open target session */
  	tsession = ceph_mdsc_open_export_target_session(mdsc, target);
-	if (!IS_ERR(tsession)) {
-		if (mds > target) {
-			mutex_lock(&session->s_mutex);
-			mutex_lock_nested(&tsession->s_mutex,
-					  SINGLE_DEPTH_NESTING);
-		} else {
-			mutex_lock(&tsession->s_mutex);
-			mutex_lock_nested(&session->s_mutex,
-					  SINGLE_DEPTH_NESTING);
-		}
-		new_cap = ceph_get_cap(mdsc, NULL);
-	} else {
+	if (IS_ERR(tsession)) {
  		WARN_ON(1);
Quoted 9 lines

fs/ceph/caps.c static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, · line 3740

Quoted 5 lines
fs/ceph/caps.c +3 -0
  		tsession = NULL;
  		target = -1;
+		mutex_lock(&session->s_mutex);
+		spin_lock(&ci->i_ceph_lock);
+		goto out_unlock;

Why did you make this case goto out_unlock instead of retrying as it did before?

If the problem occurs, target = -1, and goto retry lable, you need to call __get_cap_for_mds() or even call __ceph_remove_cap(), and then jump to out_unlock lable. All I think is unnecessary, goto out_unlock instead of retrying directly.

Thanks.
Wu Bo

Quoted 16 lines
Quoted 15 lines
fs/ceph/caps.c +11 -0
+	}
+
+	if (mds > target) {
+		mutex_lock(&session->s_mutex);
+		mutex_lock_nested(&tsession->s_mutex,
+					SINGLE_DEPTH_NESTING);
+	} else {
+		mutex_lock(&tsession->s_mutex);
+		mutex_lock_nested(&session->s_mutex,
+					SINGLE_DEPTH_NESTING);
  	}
+	new_cap = ceph_get_cap(mdsc, NULL);
  	goto retry;
 
  out_unlock:
#3 lore raw
Quoted 36 lines · Wu Bo
Quoted 35 lines · Jeff Layton
Quoted 4 lines · Wu Bo

if the ceph_mdsc_open_export_target_session() return fails, should add a lock to avoid twice unlocking.
Because the lock will be released at the retry or out_unlock tag.

The problem looks real, but...

Quoted 27 lines

--
v1 -> v2:
add spin_lock(&ci->i_ceph_lock) before goto out_unlock tag.

---
  fs/ceph/caps.c | 27 +++++++++++++++------------
  1 file changed, 15 insertions(+), 12 deletions(-)
fs/ceph/caps.c +0 -10
index 185db76..414c0e2 100644
@@ -3731,22 +3731,25 @@ static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
 
  	/* open target session */
  	tsession = ceph_mdsc_open_export_target_session(mdsc, target);
-	if (!IS_ERR(tsession)) {
-		if (mds > target) {
-			mutex_lock(&session->s_mutex);
-			mutex_lock_nested(&tsession->s_mutex,
-					  SINGLE_DEPTH_NESTING);
-		} else {
-			mutex_lock(&tsession->s_mutex);
-			mutex_lock_nested(&session->s_mutex,
-					  SINGLE_DEPTH_NESTING);
-		}
Quoted 8 lines
Quoted 8 lines

fs/ceph/caps.c static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, · line 3739

Quoted 8 lines
fs/ceph/caps.c +3 -2
-		new_cap = ceph_get_cap(mdsc, NULL);
-	} else {
+	if (IS_ERR(tsession)) {
  		WARN_ON(1);
  		tsession = NULL;
  		target = -1;
+		mutex_lock(&session->s_mutex);
+		spin_lock(&ci->i_ceph_lock);

Rather than taking the spinlock here, it'd be nicer to set a new label above the mutex (out_unlock_mutex or something) and jump to that.

Quoted 11 lines
Quoted 5 lines

fs/ceph/caps.c static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, · line 3740

Quoted 1 line
fs/ceph/caps.c +1 -0
+		goto out_unlock;

Why did you make this case goto out_unlock instead of retrying as it did before?

If the problem occurs, target = -1, and goto retry lable, you need to call __get_cap_for_mds() or even call __ceph_remove_cap(), and then jump to out_unlock lable. All I think is unnecessary, goto out_unlock instead of retrying directly.

(cc'ing Zheng since he understands the IMPORT/EXPORT code better than I)

I'm not quite convinced. It certainly looks like this was done deliberately before, and that the expectation is that the cap be removed in this case.

If we do want to make this change, then at the very least the changelog needs to spell out why this safe and desirable.

Quoted 17 lines
Quoted 15 lines
Quoted 15 lines
fs/ceph/caps.c +11 -0
+	}
+
+	if (mds > target) {
+		mutex_lock(&session->s_mutex);
+		mutex_lock_nested(&tsession->s_mutex,
+					SINGLE_DEPTH_NESTING);
+	} else {
+		mutex_lock(&tsession->s_mutex);
+		mutex_lock_nested(&session->s_mutex,
+					SINGLE_DEPTH_NESTING);
  	}
+	new_cap = ceph_get_cap(mdsc, NULL);
  	goto retry;
 
  out_unlock:
Signature · 1 line
Jeff Layton <[email protected]>
#4 lore raw
In reply to Wu Bo
Quoted 51 lines · Wu Bo
Quoted 39 lines · Jeff Layton
Quoted 4 lines · Wu Bo

if the ceph_mdsc_open_export_target_session() return fails, should add a lock to avoid twice unlocking.
Because the lock will be released at the retry or out_unlock tag.

The problem looks real, but...

Quoted 31 lines

--
v1 -> v2:
add spin_lock(&ci->i_ceph_lock) before goto out_unlock tag.

---
  fs/ceph/caps.c | 27 +++++++++++++++------------
  1 file changed, 15 insertions(+), 12 deletions(-)
fs/ceph/caps.c +1 -12
index 185db76..414c0e2 100644
@@ -3731,22 +3731,25 @@ static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
 
     /* open target session */
     tsession = ceph_mdsc_open_export_target_session(mdsc, target);
-    if (!IS_ERR(tsession)) {
-            if (mds > target) {
-                    mutex_lock(&session->s_mutex);
-                    mutex_lock_nested(&tsession->s_mutex,
-                                      SINGLE_DEPTH_NESTING);
-            } else {
-                    mutex_lock(&tsession->s_mutex);
-                    mutex_lock_nested(&session->s_mutex,
-                                      SINGLE_DEPTH_NESTING);
-            }
-            new_cap = ceph_get_cap(mdsc, NULL);
-    } else {
+    if (IS_ERR(tsession)) {
             WARN_ON(1);
Quoted 9 lines

fs/ceph/caps.c static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, · line 3740

Quoted 5 lines
fs/ceph/caps.c +3 -0
             tsession = NULL;
             target = -1;
+            mutex_lock(&session->s_mutex);
+            spin_lock(&ci->i_ceph_lock);
+            goto out_unlock;

Why did you make this case goto out_unlock instead of retrying as it did before?

Quoted 5 lines

If the problem occurs, target = -1, and goto retry lable, you need to call __get_cap_for_mds() or even call __ceph_remove_cap(), and then jump to out_unlock lable. All I think is unnecessary, goto out_unlock instead of retrying directly.

__ceph_remove_cap() must be called even if opening target session failed. I think adding a mutex_lock(&session->s_mutex) to the IS_ERR(tsession) block should be enough.

Quoted 21 lines

Thanks.
Wu Bo

Quoted 16 lines
Quoted 15 lines
fs/ceph/caps.c +11 -0
+    }
+
+    if (mds > target) {
+            mutex_lock(&session->s_mutex);
+            mutex_lock_nested(&tsession->s_mutex,
+                                    SINGLE_DEPTH_NESTING);
+    } else {
+            mutex_lock(&tsession->s_mutex);
+            mutex_lock_nested(&session->s_mutex,
+                                    SINGLE_DEPTH_NESTING);
     }
+    new_cap = ceph_get_cap(mdsc, NULL);
     goto retry;
 
  out_unlock:
author
#5 lore raw
Quoted 58 lines · Zheng
Quoted 51 lines · Wu Bo
Quoted 39 lines · Jeff Layton
Quoted 4 lines · Wu Bo

if the ceph_mdsc_open_export_target_session() return fails, should add a lock to avoid twice unlocking.
Because the lock will be released at the retry or out_unlock tag.

The problem looks real, but...

Quoted 31 lines

--
v1 -> v2:
add spin_lock(&ci->i_ceph_lock) before goto out_unlock tag.

---
   fs/ceph/caps.c | 27 +++++++++++++++------------
   1 file changed, 15 insertions(+), 12 deletions(-)
fs/ceph/caps.c +1 -12
index 185db76..414c0e2 100644
@@ -3731,22 +3731,25 @@ static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
 
      /* open target session */
      tsession = ceph_mdsc_open_export_target_session(mdsc, target);
-    if (!IS_ERR(tsession)) {
-            if (mds > target) {
-                    mutex_lock(&session->s_mutex);
-                    mutex_lock_nested(&tsession->s_mutex,
-                                      SINGLE_DEPTH_NESTING);
-            } else {
-                    mutex_lock(&tsession->s_mutex);
-                    mutex_lock_nested(&session->s_mutex,
-                                      SINGLE_DEPTH_NESTING);
-            }
-            new_cap = ceph_get_cap(mdsc, NULL);
-    } else {
+    if (IS_ERR(tsession)) {
              WARN_ON(1);
Quoted 9 lines

fs/ceph/caps.c static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, · line 3740

Quoted 5 lines
fs/ceph/caps.c +3 -0
              tsession = NULL;
              target = -1;
+            mutex_lock(&session->s_mutex);
+            spin_lock(&ci->i_ceph_lock);
+            goto out_unlock;

Why did you make this case goto out_unlock instead of retrying as it did before?

Quoted 5 lines

If the problem occurs, target = -1, and goto retry lable, you need to call __get_cap_for_mds() or even call __ceph_remove_cap(), and then jump to out_unlock lable. All I think is unnecessary, goto out_unlock instead of retrying directly.

Quoted 4 lines

__ceph_remove_cap() must be called even if opening target session failed. I think adding a mutex_lock(&session->s_mutex) to the IS_ERR(tsession) block should be enough.

Yes,I will send the V3 patch later.

Quoted 25 lines
Quoted 22 lines

Thanks.
Wu Bo

Quoted 16 lines
Quoted 15 lines
fs/ceph/caps.c +11 -0
+    }
+
+    if (mds > target) {
+            mutex_lock(&session->s_mutex);
+            mutex_lock_nested(&tsession->s_mutex,
+                                    SINGLE_DEPTH_NESTING);
+    } else {
+            mutex_lock(&tsession->s_mutex);
+            mutex_lock_nested(&session->s_mutex,
+                                    SINGLE_DEPTH_NESTING);
      }
+    new_cap = ceph_get_cap(mdsc, NULL);
      goto retry;
 
   out_unlock:

.