git: de6ee1fde8bb - main - jail: Simplify refcount manipulation routines

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Fri, 25 Sep 2026 17:00:44 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=de6ee1fde8bbcc3dde643ae9f2122c8e2499fe29

commit de6ee1fde8bbcc3dde643ae9f2122c8e2499fe29
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-09-25 15:16:12 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-09-25 16:50:41 +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
---
 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 69cb003b9fda..31b7c4a1e4f6 100644
--- a/sys/kern/kern_jail.c
+++ b/sys/kern/kern_jail.c
@@ -3432,14 +3432,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));
 }
 
 /*
@@ -3478,19 +3475,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
 }
 
 /*
@@ -3505,14 +3495,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));
 }
 
 /*
@@ -3529,8 +3516,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,