Skip to contentSkip to conversation

ceph: fix orphaned MDS locks after an interrupted lock wait

PATCH In review Venky Shankar 4 participants 7 messages Oct 15 – Oct 22 Ceph client

lore
Share & tools
lore
#0 lore raw

v2 cover ceph: fix orphaned MDS locks after an interrupted lock wait

A process blocked in F_SETLKW or flock(LOCK_EX) on CephFS can leave a lock held on the MDS that nobody will ever release. This happens when the process is interrupted (typically SIGKILL) just after the MDS has granted the lock, but before the client has processed the grant.

The kernel client marks the request aborted and sends a FCNTL_INTR or FLOCK_INTR request. The MDS handles an interrupt by cancelling a *waiting* lock only. It deliberately leaves granted locks alone, because same-owner POSIX locks merge and dropping a held lock could take away locks the owner had before the interrupted request. So the MDS keeps the lock, and replies 0 to the original request. That reply arrives for an aborted request and handle_reply() drops it ("reply arrived after request N was aborted"). posix_lock_file() or locks_lock_file_wait() never runs, so nothing is sent on close or exit, and the lock isn't sent on reconnect either. Other clients block on it, or get EAGAIN, until this client drops its last cap on the inode or the MDS restarts.

The patch stops aborting a lock request once it has been sent. It still sends the interrupt, then waits for the MDS's reply to the original request. A reply of 0 means the lock was granted: it is recorded locally, and the exiting process unlocks it as usual. A reply of -EINTR means the wait was cancelled, and becomes -ERESTARTSYS as before. The wait flag is cleared too, so a request resent to a new MDS answers at once instead of blocking again; -EAGAIN from such a resend also becomes -ERESTARTSYS.

The cost is an uninterruptible wait for that reply, which is normally one round trip after the interrupt. If the MDS is down and there is no standby, the task stays in D state until the MDS recovers. Other options are a TASK_KILLABLE wait that sends an explicit unlock if a grant turns up later, or doing that unlock from handle_reply() for aborted lock requests. Both are more involved, and I'd welcome opinions.

Tracker: https://tracker.ceph.com/issues/81697
ceph PR: https://github.com/ceph/ceph/pull/72723 (qa tests)

v2:
- Use Xiubo's correct email address.

Venky Shankar (1):
ceph: don't drop a lock grant that races with an interrupt

 fs/ceph/locks.c | 45 +++++++++++++++++++++++++++++++++++----------
 1 file changed, 35 insertions(+), 10 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH v2 1/1 ceph: don't drop a lock grant that races with an interrupt

When a blocked F_SETLKW or LOCK_EX request is interrupted, ceph_lock_wait_for_completion() marks the request aborted and sends a FCNTL_INTR/FLOCK_INTR request to the MDS. The MDS only cancels a waiting lock on an interrupt request; it leaves a granted lock alone, since it cannot tell a newly granted lock apart from locks the owner already held. If the MDS granted the lock before the interrupt reached it, the grant reply arrives for an aborted request and handle_reply() drops it. The caller returns an error, never records the lock locally, and so never unlocks it: the lock stays held on the MDS, under an owner that no longer exists, until the client releases its caps on the inode or the MDS restarts. Other clients block on it meanwhile.

This is easy to hit with a waiter that gets SIGKILLed (e.g. a container being killed) just as the lock holder unlocks. With a fatal signal pending, the wait for the original request after the interrupt returns at once as well.

Don't abort a request that has been sent. Send the interrupt request and wait, uninterruptibly, for the MDS to reply to the original
request: it replies 0 if the lock was granted first and -EINTR if the wait was cancelled. Return 0 so that the caller records a granted lock, and turn -EINTR back into -ERESTARTSYS. Clear the request's wait flag so that, if the request is resent to another MDS, it gets an immediate answer instead of waiting behind a conflicting lock; -EAGAIN from such a resend also becomes -ERESTARTSYS.

---
v2:
- Use Xiubo's correct email address.

 fs/ceph/locks.c | 45 +++++++++++++++++++++++++++++++++++----------
 1 file changed, 35 insertions(+), 10 deletions(-)
fs/ceph/locks.c +35 -10
index 677221b..1aac400 100644
@@ -128,6 +128,15 @@ static int ceph_lock_message(u8 lock_type, u16 operation, struct inode *inode,
 	if (!err)
 		err = ceph_mdsc_wait_request(mdsc, req, wait ?
 					ceph_lock_wait_for_completion : NULL);
+	/*
+	 * ceph_lock_wait_for_completion() clears the wait flag when the
+	 * wait is interrupted. The MDS then cancels the wait (-EINTR) or,
+	 * if the request was resent, refuses a conflicting lock (-EAGAIN).
+	 * Either way the lock was not granted because of the interrupt.
+	 */
+	if (wait && !req->r_args.filelock_change.wait &&
+	    (err == -EINTR || err == -EAGAIN))
+		err = -ERESTARTSYS;
 	if (!err && operation == CEPH_MDS_OP_GETFILELOCK) {
 		fl->c.flc_pid = -le64_to_cpu(req->r_reply_info.filelock_reply->pid);
 		if (CEPH_LOCK_SHARED == req->r_reply_info.filelock_reply->type)
@@ -180,8 +189,11 @@ static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
 	mutex_lock(&mdsc->mutex);
 	if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
 		err = 0;
-	} else {
+	} else if (!req->r_session) {
 		/*
+		 * haven't sent the request, so the MDS can't have granted
+		 * the lock: abort it.
+		 *
 		 * ensure we aren't running concurrently with
 		 * ceph_fill_trace or ceph_readdir_prepopulate, which
 		 * rely on locks (dir mutex) held by our caller.
@@ -190,11 +202,20 @@ static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
 		req->r_err = err;
 		set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
 		mutex_unlock(&req->r_fill_mutex);
-
-		if (!req->r_session) {
-			// haven't sent the request
-			err = 0;
-		}
+		err = 0;
+	} else {
+		/*
+		 * The MDS may have granted the lock already, and the
+		 * interrupt request below only cancels a waiting lock, it
+		 * does not undo a grant. So don't abort the request: if
+		 * its reply were dropped, a granted lock would never be
+		 * recorded locally and nothing would ever unlock it.
+		 *
+		 * Don't let the request wait on the MDS again if it has to
+		 * be resent (e.g. to a new MDS), so that its reply is not
+		 * held up behind a conflicting lock.
+		 */
+		req->r_args.filelock_change.wait = 0;
 	}
 	mutex_unlock(&mdsc->mutex);
 	if (!err)
@@ -219,10 +240,14 @@ static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
 	if (err && err != -ERESTARTSYS)
 		return err;
 
-	err = wait_for_completion_killable(&req->r_safe_completion);
-	if (err)
-		return err;
-
+	/*
+	 * Once it has handled the interrupt, the MDS replies to the
+	 * original request: 0 if the lock was granted first, -EINTR if
+	 * the wait was cancelled. Wait for that reply even with a fatal
+	 * signal pending, so that the caller records a granted lock and
+	 * the lock gets released when the file is closed.
+	 */
+	wait_for_completion(&req->r_completion);
 	return 0;
 }
 
Signature · 1 line
2.50.1 (Apple Git-155)
#3 lore raw

Re: [PATCH v2 1/1] ceph: don't drop a lock grant that races with an interrupt

Quoted 3 lines · Venky Shankar

When a blocked F_SETLKW or LOCK_EX request is interrupted, ceph_lock_wait_for_completion() marks the request aborted and sends a FCNTL_INTR/FLOCK_INTR request to the MDS.

[...]

Hi Venky,

Thanks for chasing this down. The analysis looks right to me: once the request has been sent we must not drop its reply, and clearing the wait flag so that a resend gets an immediate answer is a nice touch. I checked that __prepare_send_request() rebuilds the message from r_args for SETFILELOCK (it never gets an unsafe reply), so the cleared flag does take effect on a resend. Also, r_session is only cleared in __unregister_request(), so !req->r_session reliably means "never sent".

A couple of comments inline.

Quoted 6 lines
fs/ceph/locks.c +0 -2
@@ -219,10 +240,14 @@ static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
        if (err && err != -ERESTARTSYS)
                return err;
 
-       err = wait_for_completion_killable(&req->r_safe_completion);
-       if (err)

fs/ceph/locks.c static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc, · line 250

Quoted 10 lines
fs/ceph/locks.c +8 -2
-               return err;
-
+       /*
+        * Once it has handled the interrupt, the MDS replies to the
+        * original request: 0 if the lock was granted first, -EINTR if
+        * the wait was cancelled. Wait for that reply even with a fatal
+        * signal pending, so that the caller records a granted lock and
+        * the lock gets released when the file is closed.
+        */
+       wait_for_completion(&req->r_completion);

This turns a killable wait into an unbounded uninterruptible one. If the MDS doesn't answer (laggy MDS, a network partition with the session still open, a slow failover), the task sits in D state and can't be killed. That's the container-kill case the patch is trying to fix, and the pod would now hang in Terminating, with hung_task warnings on top.

It's also not guaranteed that the interrupt reaches the MDS at all. With SIGKILL pending, ceph_mdsc_do_request(intr_req) returns -ERESTARTSYS right away. If the session isn't open at that moment (e.g. it is reconnecting), intr_req is only queued and then aborted, and __do_request() won't send it after that. The original request then waits behind the conflicting lock until it is resent to a new MDS (with wait == 0) or the holder unlocks, which can take arbitrarily long.

Could we keep the wait killable and, when it is killed, leave the request registered instead of aborting it? An r_callback on the request (as the async create/unlink paths do) could then send an async unlock to the MDS if the late reply turns out to be a grant. That avoids leaking the lock without making the task unkillable. At the very least I'd prefer a bounded wait here.

Quoted 4 lines
(quoted diff) +0 -0
        intr_req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
                                            USE_AUTH_MDS);
        if (IS_ERR(intr_req))
                return PTR_ERR(intr_req);

[...]

Quoted 2 lines
        if (err && err != -ERESTARTSYS)
                return err;

These two error paths still abort a request that has been sent:
wait_func returns a negative error without CEPH_MDS_R_GOT_RESULT set, so ceph_mdsc_wait_request() goes into its abort branch and sets CEPH_MDS_R_ABORTED. If the MDS had already granted the lock, the reply is dropped and the lock leaks, the same bug this patch fixes. They are unlikely (-ENOMEM, -EIO/-ETIMEDOUT from the interrupt request), but the commit message says "Don't abort a request that has been sent", so they should either wait for the original reply too or go through the same orphan/async-unlock handling as above.

fs/ceph/locks.c static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc, · line 197

Quoted 7 lines
fs/ceph/locks.c +4 -1
-       } else {
+       } else if (!req->r_session) {
                /*
+                * haven't sent the request, so the MDS can't have granted
+                * the lock: abort it.
+                *
                 * ensure we aren't running concurrently with

Nit: the two comments read a bit disjointed stacked like this. Maybe merge them into one paragraph.

Thanks
Xiubo

Quoted 11 lines · Alex Markuze <[email protected]> 于2026年10月15日周四 19:58写道:

Hi Venky,

Thanks for the patch.

--
Alex Markuze

maintainer
#4 lore raw

PATCH 1/1 ceph: unlock an abandoned file-lock grant from its reply

Venky Shankar's "ceph: don't drop a lock grant that races with an interrupt" keeps a sent SETFILELOCK registered and waits for the MDS reply, so a grant is not dropped. Xiubo Li pointed out that wait_for_completion() leaves a killed task in D state, and that a failed interrupt request still aborts a request the MDS may already have granted.

After the interrupt is sent, wait with wait_for_completion_killable(). If that wait is killed, or the interrupt request fails with -ENOMEM, -EIO, -ETIMEDOUT, or -ERESTARTSYS, leave the original request registered and mark it abandoned. ceph_mdsc_wait_request() returns that error instead of aborting or reporting a late grant as success. The reply callback sends an unlock when the result is a grant.

---
Base: https://github.com/ceph/ceph-client.git testing 797fedea5c2bc44fc6d92526999fd4242052a4ee ("ceph: fix uninitialized err in ceph_netfs_issue_read()")

 fs/ceph/locks.c      | 119 +++++++++++++++++++++++++++++++++----------
 fs/ceph/mds_client.c |  11 +++-
 fs/ceph/mds_client.h |   1 +
 3 files changed, 102 insertions(+), 29 deletions(-)
fs/ceph/locks.c +92 -27
index 4bd96d0beba4..308de4c4f31a 100644
@@ -163,6 +163,66 @@ static int ceph_lock_message(u8 lock_type, u16 operation, struct inode *inode,
 	return err;
 }
 
+/*
+ * The waiter was killed, or the interrupt request failed, after the
+ * original SETFILELOCK was sent. The MDS may still grant it. Unlock a
+ * grant so the lock is not left held by a caller that already returned
+ * an error and did not record it locally.
+ */
+static void ceph_lock_orphan_cb(struct ceph_mds_client *mdsc,
+				struct ceph_mds_request *req)
+{
+	struct ceph_client *cl = mdsc->fsc->client;
+	struct ceph_mds_request *unlock;
+	struct inode *inode = req->r_inode;
+	int result, err;
+
+	if (!req->r_reply_info.head)
+		return;
+	result = le32_to_cpu(req->r_reply_info.head->result);
+	if (result)
+		return;
+
+	unlock = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
+					  USE_AUTH_MDS);
+	if (IS_ERR(unlock)) {
+		pr_warn_ratelimited_client(cl,
+			"abandoned lock grant on %llx.%llx could not be unlocked: %ld\n",
+			ceph_vinop(inode), PTR_ERR(unlock));
+		return;
+	}
+
+	unlock->r_inode = inode;
+	ihold(inode);
+	unlock->r_num_caps = 1;
+	unlock->r_args.filelock_change = req->r_args.filelock_change;
+	unlock->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
+	unlock->r_args.filelock_change.wait = 0;
+
+	err = ceph_mdsc_submit_request(mdsc, inode, unlock);
+	if (err)
+		pr_warn_ratelimited_client(cl,
+			"abandoned lock grant on %llx.%llx could not be unlocked: %d\n",
+			ceph_vinop(inode), err);
+	ceph_mdsc_put_request(unlock);
+}
+
+/* mdsc->mutex held. The request has been sent and has no result yet. */
+static int ceph_lock_abandon(struct ceph_mds_client *mdsc,
+			     struct ceph_mds_request *req, int err)
+{
+	lockdep_assert_held(&mdsc->mutex);
+
+	if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags))
+		return 0;
+
+	req->r_args.filelock_change.wait = 0;
+	req->r_callback = ceph_lock_orphan_cb;
+	req->r_err = err;
+	set_bit(CEPH_MDS_R_ABANDONED, &req->r_req_flags);
+	return err;
+}
+
 static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
                                          struct ceph_mds_request *req)
 {
@@ -191,12 +251,11 @@ static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
 		err = 0;
 	} else if (!req->r_session) {
 		/*
-		 * haven't sent the request, so the MDS can't have granted
-		 * the lock: abort it.
-		 *
-		 * ensure we aren't running concurrently with
-		 * ceph_fill_trace or ceph_readdir_prepopulate, which
-		 * rely on locks (dir mutex) held by our caller.
+		 * Not sent, so the MDS cannot have granted the lock:
+		 * abort it. Take r_fill_mutex so this does not run
+		 * concurrently with ceph_fill_trace or
+		 * ceph_readdir_prepopulate, which rely on locks held
+		 * by our caller.
 		 */
 		mutex_lock(&req->r_fill_mutex);
 		req->r_err = err;
@@ -223,32 +282,38 @@ static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
 
 	intr_req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
 					    USE_AUTH_MDS);
-	if (IS_ERR(intr_req))
-		return PTR_ERR(intr_req);
-
-	intr_req->r_inode = inode;
-	ihold(inode);
-	intr_req->r_num_caps = 1;
+	if (!IS_ERR(intr_req)) {
+		intr_req->r_inode = inode;
+		ihold(inode);
+		intr_req->r_num_caps = 1;
 
-	intr_req->r_args.filelock_change = req->r_args.filelock_change;
-	intr_req->r_args.filelock_change.rule = lock_type;
-	intr_req->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
+		intr_req->r_args.filelock_change = req->r_args.filelock_change;
+		intr_req->r_args.filelock_change.rule = lock_type;
+		intr_req->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
 
-	err = ceph_mdsc_do_request(mdsc, inode, intr_req);
-	ceph_mdsc_put_request(intr_req);
-
-	if (err && err != -ERESTARTSYS)
-		return err;
+		err = ceph_mdsc_do_request(mdsc, inode, intr_req);
+		ceph_mdsc_put_request(intr_req);
+	} else {
+		err = PTR_ERR(intr_req);
+	}
 
 	/*
-	 * Once it has handled the interrupt, the MDS replies to the
-	 * original request: 0 if the lock was granted first, -EINTR if
-	 * the wait was cancelled. Wait for that reply even with a fatal
-	 * signal pending, so that the caller records a granted lock and
-	 * the lock gets released when the file is closed.
+	 * The MDS replies to the original request after the interrupt:
+	 * 0 if the lock was granted first, -EINTR if the wait was
+	 * cancelled. A fatal signal must not turn this into an
+	 * uninterruptible sleep. If this wait is killed, or the
+	 * interrupt request never went out, leave the original request
+	 * registered. Its reply callback unlocks a late grant.
 	 */
-	wait_for_completion(&req->r_completion);
-	return 0;
+	if (!err)
+		err = wait_for_completion_killable(&req->r_completion);
+	if (!err)
+		return 0;
+
+	mutex_lock(&mdsc->mutex);
+	err = ceph_lock_abandon(mdsc, req, err);
+	mutex_unlock(&mdsc->mutex);
+	return err;
 }
 
 static int try_unlock_file(struct file *file, struct file_lock *fl)
fs/ceph/mds_client.c +9 -2
index d4ef8a355a48..f485beb3584b 100644
@@ -4076,8 +4076,15 @@ int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
 	boutc(cl, "do_request waited, got %d\n", err);
 	mutex_lock(&mdsc->mutex);
 
-	/* only abort if we didn't race with a real reply */
-	if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
+	/*
+	 * The caller was killed or the interrupt request failed after
+	 * this request was sent. Leave it registered. A late grant is
+	 * unlocked by r_callback; do not turn that grant into success
+	 * for a caller that has already given up.
+	 */
+	if (test_bit(CEPH_MDS_R_ABANDONED, &req->r_req_flags)) {
+		err = req->r_err;
+	} else if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
 		err = le32_to_cpu(req->r_reply_info.head->result);
 		if (err > 0 || err < -MAX_ERRNO) {
 			/*
fs/ceph/mds_client.h +1 -0
index 863cdb8d588a..e198ede629fd 100644
@@ -360,6 +360,7 @@ struct ceph_mds_request {
 #define CEPH_MDS_R_PARENT_LOCKED	(7) /* is r_parent->i_rwsem wlocked? */
 #define CEPH_MDS_R_ASYNC		(8) /* async request */
 #define CEPH_MDS_R_FSCRYPT_FILE		(9) /* must marshal fscrypt_file field */
+#define CEPH_MDS_R_ABANDONED		(10) /* caller left; reply unlocks a grant */
 	unsigned long	r_req_flags;
 
 	struct mutex r_fill_mutex;

base-commit: 797fedea5c2bc44fc6d92526999fd4242052a4ee

Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH] ceph: unlock an abandoned file-lock grant from its reply

Hi Alex,

Quoted 161 lines · Alex Markuze

Venky Shankar's "ceph: don't drop a lock grant that races with an interrupt" keeps a sent SETFILELOCK registered and waits for the MDS reply, so a grant is not dropped. Xiubo Li pointed out that wait_for_completion() leaves a killed task in D state, and that a failed interrupt request still aborts a request the MDS may already have granted.

After the interrupt is sent, wait with wait_for_completion_killable(). If that wait is killed, or the interrupt request fails with -ENOMEM, -EIO, -ETIMEDOUT, or -ERESTARTSYS, leave the original request registered and mark it abandoned. ceph_mdsc_wait_request() returns that error instead of aborting or reporting a late grant as success. The reply callback sends an unlock when the result is a grant.

---
Base: https://github.com/ceph/ceph-client.git testing 797fedea5c2bc44fc6d92526999fd4242052a4ee ("ceph: fix uninitialized err in ceph_netfs_issue_read()")

 fs/ceph/locks.c      | 119 +++++++++++++++++++++++++++++++++----------
 fs/ceph/mds_client.c |  11 +++-
 fs/ceph/mds_client.h |   1 +
 3 files changed, 102 insertions(+), 29 deletions(-)
fs/ceph/locks.c +83 -25
index 4bd96d0beba4..308de4c4f31a 100644
@@ -163,6 +163,66 @@ static int ceph_lock_message(u8 lock_type, u16 operation, struct inode *inode,
        return err;
 }
 
+/*
+ * The waiter was killed, or the interrupt request failed, after the
+ * original SETFILELOCK was sent. The MDS may still grant it. Unlock a
+ * grant so the lock is not left held by a caller that already returned
+ * an error and did not record it locally.
+ */
+static void ceph_lock_orphan_cb(struct ceph_mds_client *mdsc,
+                               struct ceph_mds_request *req)
+{
+       struct ceph_client *cl = mdsc->fsc->client;
+       struct ceph_mds_request *unlock;
+       struct inode *inode = req->r_inode;
+       int result, err;
+
+       if (!req->r_reply_info.head)
+               return;
+       result = le32_to_cpu(req->r_reply_info.head->result);
+       if (result)
+               return;
+
+       unlock = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
+                                         USE_AUTH_MDS);
+       if (IS_ERR(unlock)) {
+               pr_warn_ratelimited_client(cl,
+                       "abandoned lock grant on %llx.%llx could not be unlocked: %ld\n",
+                       ceph_vinop(inode), PTR_ERR(unlock));
+               return;
+       }
+
+       unlock->r_inode = inode;
+       ihold(inode);
+       unlock->r_num_caps = 1;
+       unlock->r_args.filelock_change = req->r_args.filelock_change;
+       unlock->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
+       unlock->r_args.filelock_change.wait = 0;
+
+       err = ceph_mdsc_submit_request(mdsc, inode, unlock);
+       if (err)
+               pr_warn_ratelimited_client(cl,
+                       "abandoned lock grant on %llx.%llx could not be unlocked: %d\n",
+                       ceph_vinop(inode), err);
+       ceph_mdsc_put_request(unlock);
+}
+
+/* mdsc->mutex held. The request has been sent and has no result yet. */
+static int ceph_lock_abandon(struct ceph_mds_client *mdsc,
+                            struct ceph_mds_request *req, int err)
+{
+       lockdep_assert_held(&mdsc->mutex);
+
+       if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags))
+               return 0;
+
+       req->r_args.filelock_change.wait = 0;
+       req->r_callback = ceph_lock_orphan_cb;
+       req->r_err = err;
+       set_bit(CEPH_MDS_R_ABANDONED, &req->r_req_flags);
+       return err;
+}
+
 static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
                                          struct ceph_mds_request *req)
 {
@@ -191,12 +251,11 @@ static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
                err = 0;
        } else if (!req->r_session) {
                /*
-                * haven't sent the request, so the MDS can't have granted
-                * the lock: abort it.
-                *
-                * ensure we aren't running concurrently with
-                * ceph_fill_trace or ceph_readdir_prepopulate, which
-                * rely on locks (dir mutex) held by our caller.
+                * Not sent, so the MDS cannot have granted the lock:
+                * abort it. Take r_fill_mutex so this does not run
+                * concurrently with ceph_fill_trace or
+                * ceph_readdir_prepopulate, which rely on locks held
+                * by our caller.
                 */
                mutex_lock(&req->r_fill_mutex);
                req->r_err = err;
@@ -223,32 +282,38 @@ static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
 
        intr_req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
                                            USE_AUTH_MDS);
-       if (IS_ERR(intr_req))
-               return PTR_ERR(intr_req);
-
-       intr_req->r_inode = inode;
-       ihold(inode);
-       intr_req->r_num_caps = 1;
+       if (!IS_ERR(intr_req)) {
+               intr_req->r_inode = inode;
+               ihold(inode);
+               intr_req->r_num_caps = 1;
 
-       intr_req->r_args.filelock_change = req->r_args.filelock_change;
-       intr_req->r_args.filelock_change.rule = lock_type;
-       intr_req->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
+               intr_req->r_args.filelock_change = req->r_args.filelock_change;
+               intr_req->r_args.filelock_change.rule = lock_type;
+               intr_req->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
 
-       err = ceph_mdsc_do_request(mdsc, inode, intr_req);
-       ceph_mdsc_put_request(intr_req);
-
-       if (err && err != -ERESTARTSYS)
-               return err;
+               err = ceph_mdsc_do_request(mdsc, inode, intr_req);
+               ceph_mdsc_put_request(intr_req);
+       } else {
+               err = PTR_ERR(intr_req);
+       }
 
        /*
-        * Once it has handled the interrupt, the MDS replies to the
-        * original request: 0 if the lock was granted first, -EINTR if
-        * the wait was cancelled. Wait for that reply even with a fatal
-        * signal pending, so that the caller records a granted lock and
-        * the lock gets released when the file is closed.
+        * The MDS replies to the original request after the interrupt:
+        * 0 if the lock was granted first, -EINTR if the wait was
+        * cancelled. A fatal signal must not turn this into an
+        * uninterruptible sleep. If this wait is killed, or the
+        * interrupt request never went out, leave the original request
+        * registered. Its reply callback unlocks a late grant.

fs/ceph/locks.c static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc, · line 314

Quoted 10 lines
fs/ceph/locks.c +7 -2
         */
-       wait_for_completion(&req->r_completion);
-       return 0;
+       if (!err)
+               err = wait_for_completion_killable(&req->r_completion);
+       if (!err)
+               return 0;
+
+       mutex_lock(&mdsc->mutex);
+       err = ceph_lock_abandon(mdsc, req, err);

This also runs when the INTR request itself fails (say -ENOMEM, or -ERESTARTSYS from ceph_mdsc_wait_for_reset()) after a non-fatal signal.

Suppose the task holds a read lock on a byte range and requests to upgrade it to a write lock, which waits on the MDS. A handled signal interrupts the wait and the INTR request fails, so the lock call (correctly) fails in user-space and the task still holds its read lock. If the MDS later grants the upgrade, it merges it with the read lock, and ceph_lock_orphan_cb() callback then sends an unlock for the range. That leaves the owner with no lock on the range on the MDS, while the task still thinks it holds the read lock. Consequently, another client can now acquire a conflicting write lock.

The same happens if the call is restarted (SA_RESTART) or retried before the late grant: the new grant is recorded locally and then undone on the MDS by the callback's unlock.

Quoted 44 lines
fs/ceph/locks.c +2 -0
+       mutex_unlock(&mdsc->mutex);
+       return err;
 }
 
 static int try_unlock_file(struct file *file, struct file_lock *fl)
fs/ceph/mds_client.c +9 -2
index d4ef8a355a48..f485beb3584b 100644
@@ -4076,8 +4076,15 @@ int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
        boutc(cl, "do_request waited, got %d\n", err);
        mutex_lock(&mdsc->mutex);
 
-       /* only abort if we didn't race with a real reply */
-       if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
+       /*
+        * The caller was killed or the interrupt request failed after
+        * this request was sent. Leave it registered. A late grant is
+        * unlocked by r_callback; do not turn that grant into success
+        * for a caller that has already given up.
+        */
+       if (test_bit(CEPH_MDS_R_ABANDONED, &req->r_req_flags)) {
+               err = req->r_err;
+       } else if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
                err = le32_to_cpu(req->r_reply_info.head->result);
                if (err > 0 || err < -MAX_ERRNO) {
                        /*
fs/ceph/mds_client.h +1 -0
index 863cdb8d588a..e198ede629fd 100644
@@ -360,6 +360,7 @@ struct ceph_mds_request {
 #define CEPH_MDS_R_PARENT_LOCKED       (7) /* is r_parent->i_rwsem wlocked? */
 #define CEPH_MDS_R_ASYNC               (8) /* async request */
 #define CEPH_MDS_R_FSCRYPT_FILE                (9) /* must marshal fscrypt_file field */
+#define CEPH_MDS_R_ABANDONED           (10) /* caller left; reply unlocks a grant */
        unsigned long   r_req_flags;
 
        struct mutex r_fill_mutex;

base-commit: 797fedea5c2bc44fc6d92526999fd4242052a4ee --
2.34.1

Signature · 2 lines
Cheers,
Venky
#6 lore raw

Re: [PATCH] ceph: unlock an abandoned file-lock grant from its reply

Quoted 14 lines · Venky Shankar

This also runs when the INTR request itself fails (say -ENOMEM, or -ERESTARTSYS from ceph_mdsc_wait_for_reset()) after a non-fatal signal.

Suppose the task holds a read lock on a byte range and requests to upgrade it to a write lock, which waits on the MDS. A handled signal interrupts the wait and the INTR request fails, so the lock call (correctly) fails in user-space and the task still holds its read lock. If the MDS later grants the upgrade, it merges it with the read lock, and ceph_lock_orphan_cb() callback then sends an unlock for the range. That leaves the owner with no lock on the range on the MDS, while the task still thinks it holds the read lock. Consequently, another client can now acquire a conflicting write lock.

Quoted 3 lines

The same happens if the call is restarted (SA_RESTART) or retried before the late grant: the new grant is recorded locally and then undone on the MDS by the callback's unlock.

I reproduced both on a test VM. I applied the patch on top of testing (797fedea5c2b) and compared it against the unpatched testing module. To make the INTR request fail, I used failslab on the ceph_mds_request cache, limited to the waiting task. Client A holds a read lock on [0,100) and blocks upgrading it to a write lock. Client B also holds a read lock on [0,100).

Upgrade, INTR fails with -ENOMEM, then B unlocks:

testing: A local READ; MDS has A's WRITE. B: F_GETLK -> conflict,

            F_SETLK(F_WRLCK) -> EAGAIN
  patched:  A local READ; MDS has nothing.   B: F_GETLK -> no conflict,
            F_SETLK(F_WRLCK) -> success, while A still holds its read
            lock

Same, but A retries the upgrade before B unlocks:

testing: A local WRITE, B gets EAGAIN
patched: A local WRITE, B gets the write lock as well

Note that before this patch, a failed INTR left the MDS with a stronger lock than the one recorded locally. That lock is released on unlock or close, so the only cost is blocking other clients for longer. The unconditional unlock in ceph_lock_orphan_cb() turns this into a weaker lock on the MDS than the one recorded locally, which breaks the lock semantics. An unlock is sent by owner and range, so it cannot tell the abandoned request apart from a lock the same owner holds, or obtains later, on that range.

On the -ERESTARTSYS case: I had assumed that only a fatal signal could make the INTR request fail with -ERESTARTSYS. You're right, ceph_mdsc_wait_for_reset() returns it right away for any pending signal while a reset is in progress. In my test, though, the reset then tears the session down, so no late grant ever arrives. What happens instead is the r_err problem below.

There are a few more problems, which I also reproduced:

1) ceph_lock_abandon() sets req->r_err but leaves the request registered.

fs/ceph/locks.c static int ceph_lock_message(u8 lock_type, u16 operation, struct inode *inode, · line 222

Quoted 4 lines
fs/ceph/locks.c +4 -0
+       req->r_args.filelock_change.wait = 0;
+       req->r_callback = ceph_lock_orphan_cb;
+       req->r_err = err;
+       set_bit(CEPH_MDS_R_ABANDONED, &req->r_req_flags);

a) handle_forward() has BUG_ON(req->r_err) on the resend path. With two active ranks, I abandoned a queued SETFILELOCK and then pinned its directory to the other rank. The MDS forwards the queued request and the client crashes:

kernel BUG at fs/ceph/mds_client.c:4457!
RIP: 0010:handle_forward.isra.0+0x10f7/0x1520 [ceph]
RAX: 00000000fffffff4 (-ENOMEM, i.e. req->r_err)
Workqueue: ceph-msgr ceph_con_workfn [libceph]

KASAN then reports a slab-use-after-free in mutex_can_spin_on_owner(), and the mount is wedged. Without the patch, the same sequence logs "forward tid N aborted, unregistering".

b) __do_request() returns early when r_err is set, without sending or unregistering the request. After a session reset (debugfs reset/trigger), which zeroes r_attempts and kicks the requests, the abandoned SETFILELOCK stays in the mdsc request list forever, and umount waits for the whole mount_timeout (60.7s vs 0.8s without the patch).

The error can come straight from the wait_func return value instead. ceph_mdsc_wait_request() only needs to return err in the ABANDONED case, and must not touch req->r_err.

2) ceph_lock_orphan_cb() runs in handle_reply(), in the ceph-msgr work for that session, and calls ceph_mdsc_submit_request(), which blocks in ceph_mdsc_wait_for_reset() while a reset is in progress. I let a late grant arrive during a reset drain. The worker sits here:

     ceph_mdsc_wait_for_reset+0x3aa/0xc50 [ceph]
     ceph_mdsc_submit_request+0x79/0x8c0 [ceph]
     ceph_lock_orphan_cb+0x325/0x490 [ceph]
     complete_request+0xaa/0x210 [ceph]
     handle_reply+0x1079/0x2d60 [ceph]
     mds_dispatch+0x242/0x490 [ceph]

The drain waits for cap flush acks, which sit behind this blocked connection, so the drain runs into its 30s timeout and falls back to forced teardown ("dropping dirty+flushing AxFx state"). Without the patch, the same run drains normally. In my run the flush itself had already reached the MDS, so nothing was actually lost, but the callback should not submit requests from the reply path. Please defer the unlock to a work item.

3) ceph_lock_orphan_cb() checks r_reply_info.head. Checking CEPH_MDS_R_GOT_RESULT would be more robust, since head can be set when parse_reply_info() fails.

4) The changelog says the request is abandoned for -ENOMEM, -EIO, -ETIMEDOUT or -ERESTARTSYS, but the code abandons on any error from the INTR request, including errors that the MDS returns.

As for the direction: I think the request should be abandoned only when the task has a fatal signal pending. In that case the task is exiting and nobody can keep or retry a lock under that owner. When the INTR request cannot be sent and there is no fatal signal, keep the original request as it is and wait for its reply with wait_for_completion_killable(), then return the real result (record the grant if there is one). Abandon only if that wait is killed. While a reset is in progress, the cost is that a signal cannot interrupt the wait right away, which seems acceptable. That would fix both of your cases and 1). The flock case (a struct file shared across fork) still needs a separate look, even with a fatal signal.

Thanks,
Xiubo

Quoted 244 lines · Venky Shankar

Hi Alex,

Quoted 161 lines · Alex Markuze

Venky Shankar's "ceph: don't drop a lock grant that races with an interrupt" keeps a sent SETFILELOCK registered and waits for the MDS reply, so a grant is not dropped. Xiubo Li pointed out that wait_for_completion() leaves a killed task in D state, and that a failed interrupt request still aborts a request the MDS may already have granted.

After the interrupt is sent, wait with wait_for_completion_killable(). If that wait is killed, or the interrupt request fails with -ENOMEM, -EIO, -ETIMEDOUT, or -ERESTARTSYS, leave the original request registered and mark it abandoned. ceph_mdsc_wait_request() returns that error instead of aborting or reporting a late grant as success. The reply callback sends an unlock when the result is a grant.

---
Base: https://github.com/ceph/ceph-client.git testing 797fedea5c2bc44fc6d92526999fd4242052a4ee ("ceph: fix uninitialized err in ceph_netfs_issue_read()")

 fs/ceph/locks.c      | 119 +++++++++++++++++++++++++++++++++----------
 fs/ceph/mds_client.c |  11 +++-
 fs/ceph/mds_client.h |   1 +
 3 files changed, 102 insertions(+), 29 deletions(-)
fs/ceph/locks.c +83 -25
index 4bd96d0beba4..308de4c4f31a 100644
@@ -163,6 +163,66 @@ static int ceph_lock_message(u8 lock_type, u16 operation, struct inode *inode,
        return err;
 }
 
+/*
+ * The waiter was killed, or the interrupt request failed, after the
+ * original SETFILELOCK was sent. The MDS may still grant it. Unlock a
+ * grant so the lock is not left held by a caller that already returned
+ * an error and did not record it locally.
+ */
+static void ceph_lock_orphan_cb(struct ceph_mds_client *mdsc,
+                               struct ceph_mds_request *req)
+{
+       struct ceph_client *cl = mdsc->fsc->client;
+       struct ceph_mds_request *unlock;
+       struct inode *inode = req->r_inode;
+       int result, err;
+
+       if (!req->r_reply_info.head)
+               return;
+       result = le32_to_cpu(req->r_reply_info.head->result);
+       if (result)
+               return;
+
+       unlock = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
+                                         USE_AUTH_MDS);
+       if (IS_ERR(unlock)) {
+               pr_warn_ratelimited_client(cl,
+                       "abandoned lock grant on %llx.%llx could not be unlocked: %ld\n",
+                       ceph_vinop(inode), PTR_ERR(unlock));
+               return;
+       }
+
+       unlock->r_inode = inode;
+       ihold(inode);
+       unlock->r_num_caps = 1;
+       unlock->r_args.filelock_change = req->r_args.filelock_change;
+       unlock->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
+       unlock->r_args.filelock_change.wait = 0;
+
+       err = ceph_mdsc_submit_request(mdsc, inode, unlock);
+       if (err)
+               pr_warn_ratelimited_client(cl,
+                       "abandoned lock grant on %llx.%llx could not be unlocked: %d\n",
+                       ceph_vinop(inode), err);
+       ceph_mdsc_put_request(unlock);
+}
+
+/* mdsc->mutex held. The request has been sent and has no result yet. */
+static int ceph_lock_abandon(struct ceph_mds_client *mdsc,
+                            struct ceph_mds_request *req, int err)
+{
+       lockdep_assert_held(&mdsc->mutex);
+
+       if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags))
+               return 0;
+
+       req->r_args.filelock_change.wait = 0;
+       req->r_callback = ceph_lock_orphan_cb;
+       req->r_err = err;
+       set_bit(CEPH_MDS_R_ABANDONED, &req->r_req_flags);
+       return err;
+}
+
 static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
                                          struct ceph_mds_request *req)
 {
@@ -191,12 +251,11 @@ static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
                err = 0;
        } else if (!req->r_session) {
                /*
-                * haven't sent the request, so the MDS can't have granted
-                * the lock: abort it.
-                *
-                * ensure we aren't running concurrently with
-                * ceph_fill_trace or ceph_readdir_prepopulate, which
-                * rely on locks (dir mutex) held by our caller.
+                * Not sent, so the MDS cannot have granted the lock:
+                * abort it. Take r_fill_mutex so this does not run
+                * concurrently with ceph_fill_trace or
+                * ceph_readdir_prepopulate, which rely on locks held
+                * by our caller.
                 */
                mutex_lock(&req->r_fill_mutex);
                req->r_err = err;
@@ -223,32 +282,38 @@ static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
 
        intr_req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
                                            USE_AUTH_MDS);
-       if (IS_ERR(intr_req))
-               return PTR_ERR(intr_req);
-
-       intr_req->r_inode = inode;
-       ihold(inode);
-       intr_req->r_num_caps = 1;
+       if (!IS_ERR(intr_req)) {
+               intr_req->r_inode = inode;
+               ihold(inode);
+               intr_req->r_num_caps = 1;
 
-       intr_req->r_args.filelock_change = req->r_args.filelock_change;
-       intr_req->r_args.filelock_change.rule = lock_type;
-       intr_req->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
+               intr_req->r_args.filelock_change = req->r_args.filelock_change;
+               intr_req->r_args.filelock_change.rule = lock_type;
+               intr_req->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
 
-       err = ceph_mdsc_do_request(mdsc, inode, intr_req);
-       ceph_mdsc_put_request(intr_req);
-
-       if (err && err != -ERESTARTSYS)
-               return err;
+               err = ceph_mdsc_do_request(mdsc, inode, intr_req);
+               ceph_mdsc_put_request(intr_req);
+       } else {
+               err = PTR_ERR(intr_req);
+       }
 
        /*
-        * Once it has handled the interrupt, the MDS replies to the
-        * original request: 0 if the lock was granted first, -EINTR if
-        * the wait was cancelled. Wait for that reply even with a fatal
-        * signal pending, so that the caller records a granted lock and
-        * the lock gets released when the file is closed.
+        * The MDS replies to the original request after the interrupt:
+        * 0 if the lock was granted first, -EINTR if the wait was
+        * cancelled. A fatal signal must not turn this into an
+        * uninterruptible sleep. If this wait is killed, or the
+        * interrupt request never went out, leave the original request
+        * registered. Its reply callback unlocks a late grant.

fs/ceph/locks.c static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc, · line 314

Quoted 10 lines
fs/ceph/locks.c +7 -2
         */
-       wait_for_completion(&req->r_completion);
-       return 0;
+       if (!err)
+               err = wait_for_completion_killable(&req->r_completion);
+       if (!err)
+               return 0;
+
+       mutex_lock(&mdsc->mutex);
+       err = ceph_lock_abandon(mdsc, req, err);

This also runs when the INTR request itself fails (say -ENOMEM, or -ERESTARTSYS from ceph_mdsc_wait_for_reset()) after a non-fatal signal.

Suppose the task holds a read lock on a byte range and requests to upgrade it to a write lock, which waits on the MDS. A handled signal interrupts the wait and the INTR request fails, so the lock call (correctly) fails in user-space and the task still holds its read lock. If the MDS later grants the upgrade, it merges it with the read lock, and ceph_lock_orphan_cb() callback then sends an unlock for the range. That leaves the owner with no lock on the range on the MDS, while the task still thinks it holds the read lock. Consequently, another client can now acquire a conflicting write lock.

The same happens if the call is restarted (SA_RESTART) or retried before the late grant: the new grant is recorded locally and then undone on the MDS by the callback's unlock.

Quoted 40 lines
fs/ceph/locks.c +2 -0
+       mutex_unlock(&mdsc->mutex);
+       return err;
 }
 
 static int try_unlock_file(struct file *file, struct file_lock *fl)
fs/ceph/mds_client.c +9 -2
index d4ef8a355a48..f485beb3584b 100644
@@ -4076,8 +4076,15 @@ int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
        boutc(cl, "do_request waited, got %d\n", err);
        mutex_lock(&mdsc->mutex);
 
-       /* only abort if we didn't race with a real reply */
-       if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
+       /*
+        * The caller was killed or the interrupt request failed after
+        * this request was sent. Leave it registered. A late grant is
+        * unlocked by r_callback; do not turn that grant into success
+        * for a caller that has already given up.
+        */
+       if (test_bit(CEPH_MDS_R_ABANDONED, &req->r_req_flags)) {
+               err = req->r_err;
+       } else if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
                err = le32_to_cpu(req->r_reply_info.head->result);
                if (err > 0 || err < -MAX_ERRNO) {
                        /*
fs/ceph/mds_client.h +1 -0
index 863cdb8d588a..e198ede629fd 100644
@@ -360,6 +360,7 @@ struct ceph_mds_request {
 #define CEPH_MDS_R_PARENT_LOCKED       (7) /* is r_parent->i_rwsem wlocked? */
 #define CEPH_MDS_R_ASYNC               (8) /* async request */
 #define CEPH_MDS_R_FSCRYPT_FILE                (9) /* must marshal fscrypt_file field */
+#define CEPH_MDS_R_ABANDONED           (10) /* caller left; reply unlocks a grant */
        unsigned long   r_req_flags;
 
        struct mutex r_fill_mutex;
Quoted 4 lines

base-commit: 797fedea5c2bc44fc6d92526999fd4242052a4ee --
2.34.1

--
Cheers,
Venky