git: 7fc0292c9fee - stable/14 - jail: Simplify refcount manipulation routines
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 26 Oct 2026 19:46:49 UTC
The branch stable/14 has been updated by markj:
URL: https://cgit.FreeBSD.org/src/commit/?id=7fc0292c9fee31dee10f7b50499d97794ed245af
commit 7fc0292c9fee31dee10f7b50499d97794ed245af
Author: Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-09-25 15:16:12 +0000
Commit: Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-10-26 14:12:15 +0000
jail: Simplify refcount manipulation routines
- refcount_acquire() returns the old value, use that to assert that the
old value was non-zero.
- refcount_release() already asserts that the refcount value is
non-zero, so don't bother asserting that again in the jail code.
- Use __diagused instead of having separate implementations for
INVARIANTS and !INVARIANTS.
No functional change intended.
Reviewed by: jamie
MFC after: 1 week
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D59983
(cherry picked from commit de6ee1fde8bbcc3dde643ae9f2122c8e2499fe29)
---
sys/kern/kern_jail.c | 37 +++++++++++--------------------------
1 file changed, 11 insertions(+), 26 deletions(-)
diff --git a/sys/kern/kern_jail.c b/sys/kern/kern_jail.c
index 5063410e63b1..5e2992527cf2 100644
--- a/sys/kern/kern_jail.c
+++ b/sys/kern/kern_jail.c
@@ -2936,14 +2936,11 @@ prison_hold_locked(struct prison *pr)
void
prison_hold(struct prison *pr)
{
-#ifdef INVARIANTS
- int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
+ u_int old __diagused;
- KASSERT(was_valid,
- ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
-#else
- refcount_acquire(&pr->pr_ref);
-#endif
+ old = refcount_acquire(&pr->pr_ref);
+ KASSERT(old > 0,
+ ("cannot hold a non-alive prison (jid=%d)", pr->pr_id));
}
/*
@@ -2982,19 +2979,12 @@ prison_free(struct prison *pr)
static void
prison_free_not_last(struct prison *pr)
{
-#ifdef INVARIANTS
- int lastref;
+ bool released __diagused;
- KASSERT(refcount_load(&pr->pr_ref) > 0,
- ("Trying to free dead prison %p (jid=%d).",
- pr, pr->pr_id));
- lastref = refcount_release(&pr->pr_ref);
- KASSERT(!lastref,
+ released = refcount_release(&pr->pr_ref);
+ KASSERT(!released,
("prison_free_not_last freed last ref on prison %p (jid=%d).",
pr, pr->pr_id));
-#else
- refcount_release(&pr->pr_ref);
-#endif
}
/*
@@ -3009,14 +2999,11 @@ prison_free_not_last(struct prison *pr)
void
prison_proc_hold(struct prison *pr)
{
-#ifdef INVARIANTS
- int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
+ u_int old __diagused;
- KASSERT(was_alive,
- ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
-#else
- refcount_acquire(&pr->pr_uref);
-#endif
+ old = refcount_acquire(&pr->pr_uref);
+ KASSERT(old > 0,
+ ("cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
}
/*
@@ -3033,8 +3020,6 @@ prison_proc_free(struct prison *pr)
* This allows assurance that a locked prison will remain alive
* until it is unlocked.
*/
- KASSERT(refcount_load(&pr->pr_uref) > 0,
- ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
if (!refcount_release_if_not_last(&pr->pr_uref)) {
/*
* Don't remove the last user reference in this context,