diff --git a/sys/amd64/include/cpu.h b/sys/amd64/include/cpu.h index db3554fad84f8f..1b8a552d3e7cfd 100644 --- a/sys/amd64/include/cpu.h +++ b/sys/amd64/include/cpu.h @@ -77,6 +77,7 @@ extern char etext[]; extern void (*vmm_resume_p)(void); void cpu_halt(void); +void cpu_lock_delay(void); void cpu_reset(void); void fork_trampoline(void); void swi_vm(void *); diff --git a/sys/arm/include/cpu.h b/sys/arm/include/cpu.h index 1ef5798ecddbb4..7c7de38a25360f 100644 --- a/sys/arm/include/cpu.h +++ b/sys/arm/include/cpu.h @@ -61,6 +61,7 @@ get_cyclecount(void) #define cpu_getstack(td) ((td)->td_frame->tf_usr_sp) #define cpu_setstack(td, sp) ((td)->td_frame->tf_usr_sp = (sp)) #define cpu_spinwait() /* nothing */ +#define cpu_lock_delay() DELAY(1) #define ARM_NVEC 8 #define ARM_VEC_ALL 0xffffffff diff --git a/sys/arm64/include/cpu.h b/sys/arm64/include/cpu.h index f5d7909b7899f5..3cfc59c25bff53 100644 --- a/sys/arm64/include/cpu.h +++ b/sys/arm64/include/cpu.h @@ -51,6 +51,7 @@ #define cpu_getstack(td) ((td)->td_frame->tf_sp) #define cpu_setstack(td, sp) ((td)->td_frame->tf_sp = (sp)) #define cpu_spinwait() __asm __volatile("yield" ::: "memory") +#define cpu_lock_delay() DELAY(1) /* Extract CPU affinity levels 0-3 */ #define CPU_AFF0(mpidr) (u_int)(((mpidr) >> 0) & 0xff) diff --git a/sys/i386/include/cpu.h b/sys/i386/include/cpu.h index 8cd94fa7e1ff48..8ba5e455f71484 100644 --- a/sys/i386/include/cpu.h +++ b/sys/i386/include/cpu.h @@ -72,6 +72,7 @@ extern char btext[]; extern char etext[]; void cpu_halt(void); +void cpu_lock_delay(void); void cpu_reset(void); void fork_trampoline(void); void swi_vm(void *); diff --git a/sys/kern/kern_mutex.c b/sys/kern/kern_mutex.c index c3f566dd027948..65c2e299bbc752 100644 --- a/sys/kern/kern_mutex.c +++ b/sys/kern/kern_mutex.c @@ -1206,7 +1206,7 @@ _mtx_lock_indefinite_check(struct mtx *m, struct lock_delay_arg *ldap) ldap->spin_cnt++; if (ldap->spin_cnt < 60000000 || kdb_active || panicstr != NULL) - DELAY(1); + cpu_lock_delay(); else { td = mtx_owner(m); diff --git a/sys/mips/include/cpu.h b/sys/mips/include/cpu.h index a54ea2a2d6bfa9..b4f5c59f72f57a 100644 --- a/sys/mips/include/cpu.h +++ b/sys/mips/include/cpu.h @@ -71,6 +71,7 @@ #define cpu_getstack(td) ((td)->td_frame->sp) #define cpu_setstack(td, nsp) ((td)->td_frame->sp = (nsp)) #define cpu_spinwait() /* nothing */ +#define cpu_lock_delay() DELAY(1) /* * A machine-independent interface to the CPU's counter. diff --git a/sys/powerpc/include/cpu.h b/sys/powerpc/include/cpu.h index 3573a0a85d2003..00f8437c8c4980 100644 --- a/sys/powerpc/include/cpu.h +++ b/sys/powerpc/include/cpu.h @@ -128,6 +128,7 @@ get_cyclecount(void) #define cpu_getstack(td) ((td)->td_frame->fixreg[1]) #define cpu_spinwait() __asm __volatile("or 27,27,27") /* yield */ +#define cpu_lock_delay() DELAY(1) extern char btext[]; extern char etext[]; diff --git a/sys/riscv/include/cpu.h b/sys/riscv/include/cpu.h index 92c5c67ef59783..989f8946333dfb 100644 --- a/sys/riscv/include/cpu.h +++ b/sys/riscv/include/cpu.h @@ -46,6 +46,7 @@ #define cpu_getstack(td) ((td)->td_frame->tf_sp) #define cpu_setstack(td, sp) ((td)->td_frame->tf_sp = (sp)) #define cpu_spinwait() /* nothing */ +#define cpu_lock_delay() DELAY(1) #ifdef _KERNEL diff --git a/sys/sparc64/include/cpu.h b/sys/sparc64/include/cpu.h index 54deaf41516720..a91159f6a7a217 100644 --- a/sys/sparc64/include/cpu.h +++ b/sys/sparc64/include/cpu.h @@ -48,6 +48,7 @@ #define cpu_getstack(td) ((td)->td_frame->tf_sp) #define cpu_setstack(td, sp) ((td)->td_frame->tf_sp = (sp)) #define cpu_spinwait() /* nothing */ +#define cpu_lock_delay() DELAY(1) #ifdef _KERNEL diff --git a/sys/x86/x86/delay.c b/sys/x86/x86/delay.c index d0900c4330b8b9..9e93bbc159de85 100644 --- a/sys/x86/x86/delay.c +++ b/sys/x86/x86/delay.c @@ -51,11 +51,19 @@ __FBSDID("$FreeBSD$"); #include #include -static u_int -get_tsc(__unused struct timecounter *tc) +static void +delay_tsc(int n) { + uint64_t end, now; - return (rdtsc32()); + sched_pin(); + now = rdtsc(); + end = now + tsc_freq * n / 1000000; + do { + cpu_spinwait(); + now = rdtsc(); + } while (now < end); + sched_unpin(); } static int @@ -66,22 +74,18 @@ delay_tc(int n) uint64_t end, freq, now; u_int last, mask, u; - tc = timecounter; - freq = atomic_load_acq_64(&tsc_freq); - if (tsc_is_invariant && freq != 0) { - func = get_tsc; - mask = ~0u; - } else { - if (tc->tc_quality <= 0) - return (0); - func = tc->tc_get_timecount; - mask = tc->tc_counter_mask; - freq = tc->tc_frequency; + if (tsc_is_invariant && tsc_freq != 0) { + delay_tsc(n); + return (1); } + tc = timecounter; + if (tc->tc_quality <= 0) + return (0); + func = tc->tc_get_timecount; + mask = tc->tc_counter_mask; + freq = tc->tc_frequency; now = 0; end = freq * n / 1000000; - if (func == get_tsc) - sched_pin(); last = func(tc) & mask; do { cpu_spinwait(); @@ -92,8 +96,6 @@ delay_tc(int n) now += u - last; last = u; } while (now < end); - if (func == get_tsc) - sched_unpin(); return (1); } @@ -110,3 +112,18 @@ DELAY(int n) init_ops.early_delay(n); TSEXIT(); } + +void +cpu_lock_delay(void) +{ + + /* + * Use TSC to wait for a usec if present, otherwise fall back + * to reading from port 0x84. We can't call into timecounters + * for this delay since timecounters might use spin locks. + */ + if (tsc_freq != 0) + delay_tsc(1); + else + inb(0x84); +}