git: db6a902078b4 - stable/15 - jail: Simplify refcount manipulation routines

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 22 Oct 2026 17:31:46 UTC
The branch stable/15 has been updated by markj:

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

commit db6a902078b4fa50d900cbdff028581920c4ac7c
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-09-25 15:16:12 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-10-22 15:45:40 +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 c2cb111882fc..5629f529b1dd 100644
--- a/sys/kern/kern_jail.c
+++ b/sys/kern/kern_jail.c
@@ -3294,14 +3294,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));
 }
 
 /*
@@ -3340,19 +3337,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
 }
 
 /*
@@ -3367,14 +3357,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));
 }
 
 /*
@@ -3391,8 +3378,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,