* [PATCH] ceph: Hold s_mutex while waking up session caps in check_new_map()
@ 2026-12-04 18:45 Praneeth Balasubramanian
2026-12-05 13:09 ` Xiubo Li
0 siblings, 1 reply; 2+ messages in thread
From: Praneeth Balasubramanian @ 2026-12-04 18:45 UTC (permalink / raw)
To: Ilya Dryomov, Alex Markuze, Xiubo Li
Cc: Viacheslav Dubeyko, Max Kellermann, ceph-devel, linux-kernel,
Praneeth Balasubramanian, stable
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@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---
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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] ceph: Hold s_mutex while waking up session caps in check_new_map()
2026-12-04 18:45 [PATCH] ceph: Hold s_mutex while waking up session caps in check_new_map() Praneeth Balasubramanian
@ 2026-12-05 13:09 ` Xiubo Li
0 siblings, 0 replies; 2+ messages in thread
From: Xiubo Li @ 2026-12-05 13:09 UTC (permalink / raw)
To: Praneeth Balasubramanian
Cc: Ilya Dryomov, Alex Markuze, Viacheslav Dubeyko, Max Kellermann,
ceph-devel, linux-kernel, stable
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@clyso.com>
Tested-by: Xiubo Li <xiubo.li@clyso.com>
Thanks
Xiubo
On Fri, 4 Dec 2026 at 19:45, Praneeth Balasubramanian
<praneethb2002@gmail.com> 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@vger.kernel.org
> Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
> ---
>
> 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
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-05 13:09 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 18:45 [PATCH] ceph: Hold s_mutex while waking up session caps in check_new_map() Praneeth Balasubramanian
2026-12-05 13:09 ` Xiubo Li
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.