git: b7eded5ea1f1 - stable/12 - sched_ule(4): Fix possible significance loss.

From: Alexander Motin <mav_at_FreeBSD.org>
Date: Thu, 21 Oct 2021 22:44:55 UTC
The branch stable/12 has been updated by mav:

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

commit b7eded5ea1f1b051489627989c13d9e48521ed9f
Author:     Alexander Motin <mav@FreeBSD.org>
AuthorDate: 2021-10-02 03:47:18 +0000
Commit:     Alexander Motin <mav@FreeBSD.org>
CommitDate: 2021-10-21 22:44:29 +0000

    sched_ule(4): Fix possible significance loss.
    
    Before this change kern.sched.interact sysctl setting above 32 gave
    all interactive threads identical priority of PRI_MIN_INTERACT due to
    ((PRI_MAX_INTERACT - PRI_MIN_INTERACT + 1) / sched_interact) turning
    zero.  Setting the sysctl lower reduced the range of used priority
    levels up to half, that is not great either.
    
    Change of the operations order should fix the issue, always using full
    range of priorities, while overflow is impossible there since both
    score and priority values are small.  While there, make the variables
    unsigned as they really are.
    
    MFC after:      1 month
    
    (cherry picked from commit 1c119e173ddc7f5603a3b6cf940dc524e494a667)
---
 sys/kern/sched_ule.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/sys/kern/sched_ule.c b/sys/kern/sched_ule.c
index 20835a0b2c5f..ed5cef8ab80a 100644
--- a/sys/kern/sched_ule.c
+++ b/sys/kern/sched_ule.c
@@ -206,7 +206,7 @@ _Static_assert(sizeof(struct thread) + sizeof(struct td_sched) <=
  * sched_slice:		Runtime of each thread before rescheduling.
  * preempt_thresh:	Priority threshold for preemption and remote IPIs.
  */
-static int __read_mostly sched_interact = SCHED_INTERACT_THRESH;
+static u_int __read_mostly sched_interact = SCHED_INTERACT_THRESH;
 static int __read_mostly tickincr = 8 << SCHED_TICK_SHIFT;
 static int __read_mostly realstathz = 127;	/* reset during boot. */
 static int __read_mostly sched_slice = 10;	/* reset during boot. */
@@ -1576,8 +1576,7 @@ sched_interact_score(struct thread *td)
 static void
 sched_priority(struct thread *td)
 {
-	int score;
-	int pri;
+	u_int pri, score;
 
 	if (PRI_BASE(td->td_pri_class) != PRI_TIMESHARE)
 		return;
@@ -1597,10 +1596,10 @@ sched_priority(struct thread *td)
 	score = imax(0, sched_interact_score(td) + td->td_proc->p_nice);
 	if (score < sched_interact) {
 		pri = PRI_MIN_INTERACT;
-		pri += ((PRI_MAX_INTERACT - PRI_MIN_INTERACT + 1) /
-		    sched_interact) * score;
+		pri += (PRI_MAX_INTERACT - PRI_MIN_INTERACT + 1) * score /
+		    sched_interact;
 		KASSERT(pri >= PRI_MIN_INTERACT && pri <= PRI_MAX_INTERACT,
-		    ("sched_priority: invalid interactive priority %d score %d",
+		    ("sched_priority: invalid interactive priority %u score %u",
 		    pri, score));
 	} else {
 		pri = SCHED_PRI_MIN;
@@ -1609,7 +1608,7 @@ sched_priority(struct thread *td)
 			    SCHED_PRI_RANGE - 1);
 		pri += SCHED_PRI_NICE(td->td_proc->p_nice);
 		KASSERT(pri >= PRI_MIN_BATCH && pri <= PRI_MAX_BATCH,
-		    ("sched_priority: invalid priority %d: nice %d, " 
+		    ("sched_priority: invalid priority %u: nice %d, "
 		    "ticks %d ftick %d ltick %d tick pri %d",
 		    pri, td->td_proc->p_nice, td_get_sched(td)->ts_ticks,
 		    td_get_sched(td)->ts_ftick, td_get_sched(td)->ts_ltick,
@@ -3070,7 +3069,7 @@ SYSCTL_PROC(_kern_sched, OID_AUTO, quantum, CTLTYPE_INT | CTLFLAG_RW,
     "Quantum for timeshare threads in microseconds");
 SYSCTL_INT(_kern_sched, OID_AUTO, slice, CTLFLAG_RW, &sched_slice, 0,
     "Quantum for timeshare threads in stathz ticks");
-SYSCTL_INT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0,
+SYSCTL_UINT(_kern_sched, OID_AUTO, interact, CTLFLAG_RW, &sched_interact, 0,
     "Interactivity score threshold");
 SYSCTL_INT(_kern_sched, OID_AUTO, preempt_thresh, CTLFLAG_RW,
     &preempt_thresh, 0,