jcs's openbsd hax
openbsd

Do not retake the SCHED_LOCK when going out of mi_switch(). No code really depends on that and it is one extra mutex operation for no good reason.

Switch spc_curpriority to be volatile. It is only modified by the curcpu()
but read by other CPUs. Mark it like this in sched.h.
OK mpi@

claudio 4b3f9329 54467b34

+11 -17
+1 -4
sys/kern/kern_sched.c
··· 1 - /* $OpenBSD: kern_sched.c,v 1.107 2025/05/28 03:27:44 jsg Exp $ */ 1 + /* $OpenBSD: kern_sched.c,v 1.108 2025/05/31 06:58:27 claudio Exp $ */ 2 2 /* 3 3 * Copyright (c) 2007, 2008 Artur Grabowski <art@openbsd.org> 4 4 * ··· 151 151 p->p_cpu = ci; 152 152 atomic_setbits_int(&p->p_flag, P_CPUPEG); 153 153 mi_switch(); 154 - SCHED_UNLOCK(); 155 154 156 155 KASSERT(ci == curcpu()); 157 156 KASSERT(curproc == spc->spc_idleproc); ··· 163 162 SCHED_LOCK(); 164 163 p->p_stat = SSLEEP; 165 164 mi_switch(); 166 - SCHED_UNLOCK(); 167 165 168 166 while ((dead = TAILQ_FIRST(&spc->spc_deadproc))) { 169 167 TAILQ_REMOVE(&spc->spc_deadproc, dead, p_runq); ··· 630 628 setrunqueue(ci, p, p->p_usrpri); 631 629 p->p_ru.ru_nvcsw++; 632 630 mi_switch(); 633 - SCHED_UNLOCK(); 634 631 } 635 632 636 633 void
+4 -4
sys/kern/kern_sig.c
··· 1 - /* $OpenBSD: kern_sig.c,v 1.364 2025/03/10 09:28:56 claudio Exp $ */ 1 + /* $OpenBSD: kern_sig.c,v 1.365 2025/05/31 06:58:27 claudio Exp $ */ 2 2 /* $NetBSD: kern_sig.c,v 1.54 1996/04/22 01:38:32 christos Exp $ */ 3 3 4 4 /* ··· 1686 1686 if (p->p_stat == SSTOP) { 1687 1687 p->p_ru.ru_nvcsw++; 1688 1688 mi_switch(); 1689 + } else { 1690 + KASSERT(p->p_stat == SONPROC); 1691 + SCHED_UNLOCK(); 1689 1692 } 1690 - KASSERT(p->p_stat == SONPROC); 1691 - 1692 - SCHED_UNLOCK(); 1693 1693 mtx_enter(&pr->ps_mtx); 1694 1694 } 1695 1695
+2 -3
sys/kern/kern_synch.c
··· 1 - /* $OpenBSD: kern_synch.c,v 1.223 2025/05/01 06:58:21 dlg Exp $ */ 1 + /* $OpenBSD: kern_synch.c,v 1.224 2025/05/31 06:58:27 claudio Exp $ */ 2 2 /* $NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $ */ 3 3 4 4 /* ··· 406 406 } else { 407 407 KASSERT(p->p_stat == SONPROC || p->p_stat == SSLEEP); 408 408 p->p_stat = SONPROC; 409 + SCHED_UNLOCK(); 409 410 } 410 411 411 412 #ifdef DIAGNOSTIC ··· 414 415 #endif 415 416 416 417 p->p_cpu->ci_schedstate.spc_curpriority = p->p_usrpri; 417 - SCHED_UNLOCK(); 418 418 419 419 /* 420 420 * Even though this belongs to the signal handling part of sleep, ··· 672 672 setrunqueue(p->p_cpu, p, newprio); 673 673 p->p_ru.ru_nvcsw++; 674 674 mi_switch(); 675 - SCHED_UNLOCK(); 676 675 677 676 return (0); 678 677 }
+1 -4
sys/kern/sched_bsd.c
··· 1 - /* $OpenBSD: sched_bsd.c,v 1.99 2025/03/10 09:28:56 claudio Exp $ */ 1 + /* $OpenBSD: sched_bsd.c,v 1.100 2025/05/31 06:58:27 claudio Exp $ */ 2 2 /* $NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $ */ 3 3 4 4 /*- ··· 317 317 setrunqueue(p->p_cpu, p, p->p_usrpri); 318 318 p->p_ru.ru_nvcsw++; 319 319 mi_switch(); 320 - SCHED_UNLOCK(); 321 320 } 322 321 323 322 /* ··· 335 334 setrunqueue(p->p_cpu, p, p->p_usrpri); 336 335 p->p_ru.ru_nivcsw++; 337 336 mi_switch(); 338 - SCHED_UNLOCK(); 339 337 } 340 338 341 339 void ··· 440 438 if (hold_count) 441 439 __mp_acquire_count(&kernel_lock, hold_count); 442 440 #endif 443 - SCHED_LOCK(); 444 441 } 445 442 446 443 /*
+3 -2
sys/sys/sched.h
··· 1 - /* $OpenBSD: sched.h,v 1.74 2025/05/16 13:40:30 mpi Exp $ */ 1 + /* $OpenBSD: sched.h,v 1.75 2025/05/31 06:58:27 claudio Exp $ */ 2 2 /* $NetBSD: sched.h,v 1.2 1999/02/28 18:14:58 ross Exp $ */ 3 3 4 4 /*- ··· 104 104 105 105 /* 106 106 * Per-CPU scheduler state. 107 + * o owned (modified only) by this CPU 107 108 */ 108 109 struct schedstate_percpu { 109 110 struct proc *spc_idleproc; /* idle proc for this cpu */ ··· 113 114 volatile int spc_schedflags; /* flags; see below */ 114 115 u_int spc_schedticks; /* ticks for schedclock() */ 115 116 u_int64_t spc_cp_time[CPUSTATES]; /* CPU state statistics */ 116 - u_char spc_curpriority; /* usrpri of curproc */ 117 117 118 118 struct clockintr spc_itimer; /* [o] itimer_update handle */ 119 119 struct clockintr spc_profclock; /* [o] profclock handle */ ··· 131 131 u_char spc_smrexpedite; /* if set, dispatch smr entries 132 132 * without delay */ 133 133 u_char spc_smrgp; /* this CPU's view of grace period */ 134 + volatile u_char spc_curpriority; /* [o] usrpri of curproc */ 134 135 }; 135 136 136 137 /* spc_flags */