ceph: fix orphaned MDS locks after an interrupted lock wait
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)
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)
PATCH 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.
--- fs/ceph/locks.c | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-)
fs/ceph/locks.c +35 -10
@@ -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)