[PATCH] ceph: unlink req->r_wait on unregister, unsafe reply, and forward

From: Yiming Zhu

Date: Fri Dec 04 2026 - 03:25:16 EST


Waiting lists (session->s_waiting and mdsc->waiting_for_map) link
requests via req->r_wait under mdsc->mutex without taking a reference on
req->r_kref; while registered, requests are kept alive by
mdsc->request_tree (alongside any caller references). All paths that
leave a waiting state or drop a request from request_tree must therefore
unlink req->r_wait first.

Several paths in fs/ceph/mds_client.c fail to uphold this invariant when
an in-flight FORWARD or REPLY races with MDS session reconnects/resets
that re-queue the request onto a waiting list:

1. handle_forward() calls put_request_session(req) and
__do_request(mdsc, req) without first unlinking req->r_wait (unlike
__wake_requests() and kick_requests()). If req is still on a waiting
list, __do_request() can re-add req->r_wait onto another waiting
list, leaving the old list pointing at req and triggering list
corruption when __wake_requests() later drains it. The following
crash on a 5.14 kernel after SESSION_CLOSE is consistent with this
mechanism:

[2369519.417712] ceph: mds0 reconnect denied
[2369520.833830] ceph: mds1 reconnect denied
[2369520.836765] list_del corruption. next->prev should be ffff90365228e6a0, but was ffff8fed2da19740
[2369520.846202] kernel BUG at lib/list_debug.c:56!
[2369520.877784] Workqueue: ceph-msgr ceph_con_workfn [libceph]
[2369520.883936] RIP: 0010:__list_del_entry_valid.cold.1+0x20/0x47
[2369520.999969] __wake_requests+0x7d/0x100 [ceph]
[2369521.005064] handle_session+0x5a6/0x7a0 [ceph]
[2369521.010157] mds_dispatch+0x6b5/0xc40 [ceph]
[2369521.027281] ceph_con_process_message+0x73/0x140 [libceph]

In addition, unlinking req->r_wait must happen before
put_request_session(req), otherwise dropping the last reference to
old_session can free old_session->s_waiting while req->r_wait is
still linked on it.

Conversely, if a stale FORWARD arrives after a re-sent request has
already received an unsafe reply (CEPH_MDS_R_GOT_UNSAFE) on a new
session, handle_forward() looks up req only by tid and can drop the
new session reference and re-queue req while r_unsafe_item remains on
s_unsafe (or hit BUG_ON if CEPH_MDS_R_GOT_RESULT is already set).

2. __unregister_request() unlinks req->r_unsafe_item and removes req
from request_tree without unlinking req->r_wait. If a queued request
is unregistered from handle_forward() (CEPH_MDS_R_ABORTED),
handle_reply() (safe reply), or cleanup_session_requests(), the
final ceph_mdsc_put_request() can free req while it is still linked
on a waiting list (triggering the WARN_ON_ONCE in
ceph_mdsc_release_request() and potentially a slab use-after-free).

3. handle_reply() in the unsafe (!head->safe) branch adds req to
s_unsafe and drops mdsc->mutex without unlinking req->r_wait,
leaving an unsafe-replied request exposed to __wake_requests().

Fix these under mdsc->mutex:
- Unlink req->r_wait in __unregister_request() and drop the now
redundant list_del_init(&req->r_wait) in wait_requests().
- Unlink req->r_wait in the !head->safe branch of handle_reply().
- Ignore stale FORWARDs in handle_forward() once CEPH_MDS_R_GOT_UNSAFE
is set, and unlink req->r_wait before put_request_session(req) on the
resend path.

Cc: stable@xxxxxxxxxxxxxxx
Fixes: 2f2dc053404f ("ceph: MDS client")
Fixes: 2a8e5e3637e2 ("ceph: clean up on forwarded aborted mds request")
Signed-off-by: Yiming Zhu <zhuyiming@xxxxxxxxxxxx>
---
fs/ceph/mds_client.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c
index 085ae0cfb5f7..5cca05b7a472 100644
--- a/fs/ceph/mds_client.c
+++ b/fs/ceph/mds_client.c
@@ -1305,7 +1305,8 @@ static void __unregister_request(struct ceph_mds_client *mdsc,
{
doutc(mdsc->fsc->client, "%p tid %lld\n", req, req->r_tid);

- /* Never leave an unregistered request on an unsafe list! */
+ /* Never leave an unregistered request on a waiting or unsafe list! */
+ list_del_init(&req->r_wait);
list_del_init(&req->r_unsafe_item);

if (req->r_tid == mdsc->oldest_tid) {
@@ -4139,6 +4140,7 @@ static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
}
} else {
set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
+ list_del_init(&req->r_wait);
list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
}

@@ -4314,7 +4316,10 @@ static void handle_forward(struct ceph_mds_client *mdsc,
return; /* dup reply? */
}

- if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
+ if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
+ doutc(cl, "forward tid %llu already got unsafe reply, ignoring\n",
+ tid);
+ } else if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
doutc(cl, "forward tid %llu aborted, unregistering\n", tid);
__unregister_request(mdsc, req);
} else if (fwd_seq <= req->r_num_fwd || (uint32_t)fwd_seq >= U32_MAX) {
@@ -4341,6 +4346,7 @@ static void handle_forward(struct ceph_mds_client *mdsc,
req->r_attempts = 0;
req->r_num_fwd = fwd_seq;
req->r_resend_mds = next_mds;
+ list_del_init(&req->r_wait);
put_request_session(req);
__do_request(mdsc, req);
}
@@ -6434,7 +6440,6 @@ static void wait_requests(struct ceph_mds_client *mdsc)
mutex_lock(&mdsc->mutex);
while ((req = __get_oldest_req(mdsc))) {
doutc(cl, "timed out on tid %llu\n", req->r_tid);
- list_del_init(&req->r_wait);
__unregister_request(mdsc, req);
}
}

base-commit: 7be4bc52fd9e0e55dc9a2e0232b89427d2c4b614
--
2.56.1