git: ba2c48a7f21e - stable/13 - PP mutexes: lock: Reduce 'umtx_lock' holding before taking the user lock

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Thu, 09 May 2024 13:30:20 UTC
The branch stable/13 has been updated by olce:

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

commit ba2c48a7f21e426ec84856b70db6f9b4c47f8abb
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2024-02-22 09:13:38 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2024-05-09 13:29:25 +0000

    PP mutexes: lock: Reduce 'umtx_lock' holding before taking the user lock
    
    There is no need to have it for the priority check (that the thread
    doesn't have a higher priority than the mutex's ceiling), and there's
    also no need to take it if the thread doesn't have privileges to set its
    priority to the mutex's ceiling.
    
    While here, turn 'su' into a 'bool' and compute the internal priority
    corresponding to the mutex's ceiling once and for all, putting it in new
    'new_pri'.
    
    Reviewed by:            kib
    Approved by:            emaste (mentor)
    MFC after:              2 weeks
    Sponsored by:           The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D44045
    
    (cherry picked from commit 39e4665c9694b9f9b07d1cfa85befc288cabe906)
    
    Approved by:            emaste (mentor)
---
 sys/kern/kern_umtx.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/sys/kern/kern_umtx.c b/sys/kern/kern_umtx.c
index 05a47f405d30..d8587c51e6fd 100644
--- a/sys/kern/kern_umtx.c
+++ b/sys/kern/kern_umtx.c
@@ -2521,7 +2521,8 @@ do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
 	struct umtx_pi *pi;
 	uint32_t ceiling;
 	uint32_t owner, id;
-	int error, pri, old_inherited_pri, su, rv;
+	int error, pri, old_inherited_pri, new_pri, rv;
+	bool su;
 
 	id = td->td_tid;
 	uq = td->td_umtxq;
@@ -2550,21 +2551,23 @@ do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
 			error = EINVAL;
 			goto out;
 		}
+		new_pri = PRI_MIN_REALTIME + ceiling;
 
-		mtx_lock(&umtx_lock);
-		if (td->td_base_user_pri < PRI_MIN_REALTIME + ceiling) {
-			mtx_unlock(&umtx_lock);
+		if (td->td_base_user_pri < new_pri) {
 			error = EINVAL;
 			goto out;
 		}
-		if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
-			uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
-			thread_lock(td);
-			if (uq->uq_inherited_pri < UPRI(td))
-				sched_lend_user_prio(td, uq->uq_inherited_pri);
-			thread_unlock(td);
+		if (su) {
+			mtx_lock(&umtx_lock);
+			if (new_pri < uq->uq_inherited_pri) {
+				uq->uq_inherited_pri = new_pri;
+				thread_lock(td);
+				if (new_pri < UPRI(td))
+					sched_lend_user_prio(td, new_pri);
+				thread_unlock(td);
+			}
+			mtx_unlock(&umtx_lock);
 		}
-		mtx_unlock(&umtx_lock);
 
 		rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
 		    id | UMUTEX_CONTESTED);