Skip to content

Commit 57b39aa

Browse files
committed
ns: add asserts for active refcount underflow
Add a few more assert to detect active reference count underflows. Link: https://patch.msgid.link/20251109-namespace-6-19-fixes-v1-6-ae8a4ad5a3b3@kernel.org Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent f8d5a89 commit 57b39aa

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

include/linux/ns_common.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ void __ns_ref_active_put(struct ns_common *ns);
294294

295295
static __always_inline struct ns_common *__must_check ns_get_unless_inactive(struct ns_common *ns)
296296
{
297-
VFS_WARN_ON_ONCE(__ns_ref_active_read(ns) && !__ns_ref_read(ns));
298297
if (!__ns_ref_active_read(ns)) {
299298
VFS_WARN_ON_ONCE(is_ns_init_id(ns));
300299
return NULL;

kernel/nscommon.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,10 @@ void __ns_ref_active_put(struct ns_common *ns)
170170
if (is_ns_init_id(ns))
171171
return;
172172

173-
if (!atomic_dec_and_test(&ns->__ns_ref_active))
173+
if (!atomic_dec_and_test(&ns->__ns_ref_active)) {
174+
VFS_WARN_ON_ONCE(__ns_ref_active_read(ns) < 0);
174175
return;
176+
}
175177

176178
VFS_WARN_ON_ONCE(is_ns_init_id(ns));
177179
VFS_WARN_ON_ONCE(!__ns_ref_read(ns));
@@ -181,8 +183,10 @@ void __ns_ref_active_put(struct ns_common *ns)
181183
if (!ns)
182184
return;
183185
VFS_WARN_ON_ONCE(is_ns_init_id(ns));
184-
if (!atomic_dec_and_test(&ns->__ns_ref_active))
186+
if (!atomic_dec_and_test(&ns->__ns_ref_active)) {
187+
VFS_WARN_ON_ONCE(__ns_ref_active_read(ns) < 0);
185188
return;
189+
}
186190
}
187191
}
188192

@@ -280,12 +284,16 @@ void __ns_ref_active_put(struct ns_common *ns)
280284
*/
281285
void __ns_ref_active_get(struct ns_common *ns)
282286
{
287+
int prev;
288+
283289
/* Initial namespaces are always active. */
284290
if (is_ns_init_id(ns))
285291
return;
286292

287293
/* If we didn't resurrect the namespace we're done. */
288-
if (atomic_fetch_add(1, &ns->__ns_ref_active))
294+
prev = atomic_fetch_add(1, &ns->__ns_ref_active);
295+
VFS_WARN_ON_ONCE(prev < 0);
296+
if (likely(prev))
289297
return;
290298

291299
/*
@@ -298,7 +306,9 @@ void __ns_ref_active_get(struct ns_common *ns)
298306
return;
299307

300308
VFS_WARN_ON_ONCE(is_ns_init_id(ns));
301-
if (atomic_fetch_add(1, &ns->__ns_ref_active))
309+
prev = atomic_fetch_add(1, &ns->__ns_ref_active);
310+
VFS_WARN_ON_ONCE(prev < 0);
311+
if (likely(prev))
302312
return;
303313
}
304314
}

0 commit comments

Comments
 (0)