diff options
author | jhb <jhb@FreeBSD.org> | 2008-08-05 20:02:31 +0000 |
---|---|---|
committer | jhb <jhb@FreeBSD.org> | 2008-08-05 20:02:31 +0000 |
commit | 8af56fb6875120edaae014a4cc547f5e14609a12 (patch) | |
tree | 80c45ebfc07976bf62d8de55267fa69fa52fdc1d /sys/kern/kern_synch.c | |
parent | be95b0fe3c10d07ed8c0887e7695a358ee6a2e17 (diff) | |
download | FreeBSD-src-8af56fb6875120edaae014a4cc547f5e14609a12.zip FreeBSD-src-8af56fb6875120edaae014a4cc547f5e14609a12.tar.gz |
If a thread that is swapped out is made runnable, then the setrunnable()
routine wakes up proc0 so that proc0 can swap the thread back in.
Historically, this has been done by waking up proc0 directly from
setrunnable() itself via a wakeup(). When waking up a sleeping thread
that was swapped out (the usual case when waking proc0 since only sleeping
threads are eligible to be swapped out), this resulted in a bit of
recursion (e.g. wakeup() -> setrunnable() -> wakeup()).
With sleep queues having separate locks in 6.x and later, this caused a
spin lock LOR (sleepq lock -> sched_lock/thread lock -> sleepq lock).
An attempt was made to fix this in 7.0 by making the proc0 wakeup use
the ithread mechanism for doing the wakeup. However, this required
grabbing proc0's thread lock to perform the wakeup. If proc0 was asleep
elsewhere in the kernel (e.g. waiting for disk I/O), then this degenerated
into the same LOR since the thread lock would be some other sleepq lock.
Fix this by deferring the wakeup of the swapper until after the sleepq
lock held by the upper layer has been locked. The setrunnable() routine
now returns a boolean value to indicate whether or not proc0 needs to be
woken up. The end result is that consumers of the sleepq API such as
*sleep/wakeup, condition variables, sx locks, and lockmgr, have to wakeup
proc0 if they get a non-zero return value from sleepq_abort(),
sleepq_broadcast(), or sleepq_signal().
Discussed with: jeff
Glanced at by: sam
Tested by: Jurgen Weber jurgen - ish com au
MFC after: 2 weeks
Diffstat (limited to 'sys/kern/kern_synch.c')
-rw-r--r-- | sys/kern/kern_synch.c | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/sys/kern/kern_synch.c b/sys/kern/kern_synch.c index c322ace..6c10ba4 100644 --- a/sys/kern/kern_synch.c +++ b/sys/kern/kern_synch.c @@ -327,10 +327,13 @@ pause(const char *wmesg, int timo) void wakeup(void *ident) { + int wakeup_swapper; sleepq_lock(ident); - sleepq_broadcast(ident, SLEEPQ_SLEEP, 0, 0); + wakeup_swapper = sleepq_broadcast(ident, SLEEPQ_SLEEP, 0, 0); sleepq_release(ident); + if (wakeup_swapper) + kick_proc0(); } /* @@ -341,10 +344,13 @@ wakeup(void *ident) void wakeup_one(void *ident) { + int wakeup_swapper; sleepq_lock(ident); - sleepq_signal(ident, SLEEPQ_SLEEP, 0, 0); + wakeup_swapper = sleepq_signal(ident, SLEEPQ_SLEEP, 0, 0); sleepq_release(ident); + if (wakeup_swapper) + kick_proc0(); } static void @@ -440,11 +446,11 @@ mi_switch(int flags, struct thread *newtd) } /* - * Change process state to be runnable, - * placing it on the run queue if it is in memory, - * and awakening the swapper if it isn't in memory. + * Change thread state to be runnable, placing it on the run queue if + * it is in memory. If it is swapped out, return true so our caller + * will know to awaken the swapper. */ -void +int setrunnable(struct thread *td) { @@ -454,15 +460,15 @@ setrunnable(struct thread *td) switch (td->td_state) { case TDS_RUNNING: case TDS_RUNQ: - return; + return (0); case TDS_INHIBITED: /* * If we are only inhibited because we are swapped out * then arange to swap in this process. Otherwise just return. */ if (td->td_inhibitors != TDI_SWAPPED) - return; - /* XXX: intentional fall-through ? */ + return (0); + /* FALLTHROUGH */ case TDS_CAN_RUN: break; default: @@ -472,15 +478,11 @@ setrunnable(struct thread *td) if ((td->td_flags & TDF_INMEM) == 0) { if ((td->td_flags & TDF_SWAPINREQ) == 0) { td->td_flags |= TDF_SWAPINREQ; - /* - * due to a LOR between the thread lock and - * the sleepqueue chain locks, use - * lower level scheduling functions. - */ - kick_proc0(); + return (1); } } else sched_wakeup(td); + return (0); } /* |