git: ce7dc52f07d0 - stable/14 - 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:26:59 UTC
The branch stable/14 has been updated by olce:

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

commit ce7dc52f07d05d00054d48faf00fcac4036d56df
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:26:21 +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 5abc1e71d763..092fcade9d19 100644
--- a/sys/kern/kern_umtx.c
+++ b/sys/kern/kern_umtx.c
@@ -2520,7 +2520,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;
@@ -2549,21 +2550,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);