Re: [PATCH] ceph: Hold s_mutex while waking up session caps in check_new_map()

From: Xiubo Li

Date: Sat Dec 05 2026 - 08:09:40 EST


Hi Praneeth,

Good catch.

The fix looks right, and I could reproduce the problem. The changelog
could say how the race happens, though, since check_new_map() holds
mdsc->mutex across wake_up_session_caps() and handle_session() takes
mdsc->mutex before s_mutex. The window is the unlock/relock above it:
a session message for the same MDS passes its mdsc->mutex section while
check_new_map() has dropped mdsc->mutex, waits for s_mutex, and gets it
when check_new_map() releases s_mutex right before the wakeup. Only
ceph_trim_caps() (RECALL_STATE), RENEWCAPS/FORCE_RO wakeups and debugfs
can race this way; CLOSE, forced umount and send_mds_reconnect() take
mdsc->mutex again before iterating.

Tested on testing (3f90412e989c), KASAN + lockdep, vstart, with msleep()
injection to open the window:

- one active MDS, failover + "cache drop": base leaves s_cap_iterator
NULL after the trim runs inside the wakeup; patched serializes them.
- two active ranks, a dir with caps from both: base hits
"kernel BUG at mds_client.c:2011!" (BUG_ON(cap->session != session))
in wake_up_session_caps() <- check_new_map(), after trim_caps_cb()
removed the cap the wakeup was on; patched is clean. A single MDS
cannot crash this way: trim only removes a cap directly when another
MDS also issued caps on the inode.
- patched, no injection: fsstress + 10 failovers with cache drops, no
lockdep/KASAN/hung-task reports.

Nit: "ceph: hold s_mutex ..." (lowercase).

Reviewed-by: Xiubo Li <xiubo.li@xxxxxxxxx>
Tested-by: Xiubo Li <xiubo.li@xxxxxxxxx>

Thanks
Xiubo

On Fri, 4 Dec 2026 at 19:45, Praneeth Balasubramanian
<praneethb2002@xxxxxxxxx> wrote:
>
> ceph_iterate_session_caps() says that the caller must hold the session
> s_mutex. The iterator drops s_cap_lock around each callback and records
> the current cap in session->s_cap_iterator, a single slot.
> __ceph_remove_cap() leaves that cap on the list so that the iterator
> can finish removing it. This only works if one iteration runs at a
> time. Every other caller holds s_mutex: remove_session_caps(),
> ceph_trim_caps(), send_mds_reconnect(), handle_session() for
> RENEWCAPS and FORCE_RO, and the debugfs caps file.
>
> check_new_map() drops s_mutex right after ceph_kick_flushing_caps() and
> then calls wake_up_session_caps(s, RECONNECT). Session messages for the
> same MDS are handled on its connection under s_mutex and can run at the
> same time, for example CEPH_SESSION_RECALL_STATE, which trims caps with
> ceph_trim_caps(). The second iteration overwrites s_cap_iterator and
> clears it when it ends, so the cap that the first iteration is working
> on loses its protection. If that cap is removed and put in the meantime,
> the first iteration then follows the list pointers of a cap that was
> already put.
>
> Move the mutex_unlock() below wake_up_session_caps(). The session is
> locked before mdsc->mutex here, which is the usual order, and
> wake_up_session_cb() takes neither lock.
>
> The missing lock was found by a static check that turns the locking rule
> in a function's comment into a Clang context-analysis requirement on its
> callers.
>
> Fixes: 0dc2570fab22 ("ceph: reset requested max_size after mds reconnect")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Praneeth Balasubramanian <praneethb2002@xxxxxxxxx>
> ---
>
> Notes:
> This was found by a compiler-based check, part of research at UCSB SecLab.
> The locking rule stated in the kernel-doc comment of the called function
> was turned into a Clang context-analysis requirement (__must_hold) on that
> function, and Clang 23 with CONFIG_WARN_CONTEXT_ANALYSIS then checked every
> caller. An LLM was used only to turn the comment into a machine-readable
> rule; the report itself comes from the compiler. The patch and changelog
> were drafted with Claude Opus 5.5. I reviewed the patch and take
> responsibility for it.
>
> Testing: builds without new warnings with W=1 (x86_64, clang 23,
> allmodconfig). Not runtime-tested, and I have not observed the race
> myself.
>
> fs/ceph/mds_client.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c
> index 3791fc627714..61fc8bb826df 100644
> --- a/fs/ceph/mds_client.c
> +++ b/fs/ceph/mds_client.c
> @@ -6005,8 +6005,8 @@ static void check_new_map(struct ceph_mds_client *mdsc,
> mutex_lock(&mdsc->mutex);
> ceph_put_mds_session(s);
> ceph_kick_flushing_caps(mdsc, s);
> - mutex_unlock(&s->s_mutex);
> wake_up_session_caps(s, RECONNECT);
> + mutex_unlock(&s->s_mutex);
> }
> }
>
> --
> 2.25.1
>