- remove blocked/unblocked upcall ordering.

- always wait for unblocked upcall if we have to continue a blocked
  thread.

=> removes wakeup from sys_sa_stacks when a stack is returned.
=> avoids extra sa_unblockyield syscall when unblocked upcall is
   delivered before blocked upcall is processed.
=> avoids double pagefault if we continued a thread before the
   pagefault was resolved.
=> avoids losing unblocked state if we continued a thread after
   skipping the unblocked upcall.
This commit is contained in:
cl 2003-11-17 22:57:52 +00:00
parent 2e11d201c2
commit ac4fd64ba5

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_sa.c,v 1.43 2003/11/17 22:52:09 cl Exp $ */ /* $NetBSD: kern_sa.c,v 1.44 2003/11/17 22:57:52 cl Exp $ */
/*- /*-
* Copyright (c) 2001 The NetBSD Foundation, Inc. * Copyright (c) 2001 The NetBSD Foundation, Inc.
@ -37,7 +37,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_sa.c,v 1.43 2003/11/17 22:52:09 cl Exp $"); __KERNEL_RCSID(0, "$NetBSD: kern_sa.c,v 1.44 2003/11/17 22:57:52 cl Exp $");
#include <sys/param.h> #include <sys/param.h>
#include <sys/systm.h> #include <sys/systm.h>
@ -250,7 +250,6 @@ sys_sa_stacks(struct lwp *l, void *v, register_t *retval)
if ((l2 = sast->sast_blocker)) { if ((l2 = sast->sast_blocker)) {
l2->l_upcallstack = NULL; l2->l_upcallstack = NULL;
sast->sast_blocker = NULL; sast->sast_blocker = NULL;
wakeup(&l2->l_upcallstack);
} }
if (SLIST_NEXT(sast, sast_list) != (void *)-1) { if (SLIST_NEXT(sast, sast_list) != (void *)-1) {
count = i; count = i;
@ -440,20 +439,22 @@ sa_preempt(struct lwp *l)
/* /*
* Help userspace library resolve locks and critical sections * Help userspace library resolve locks and critical sections:
* - return if the unblocked upcall has already been delivered.
* This case is usually already detected in userspace.
* - recycles the calling LWP and its stack if it was not preempted * - recycles the calling LWP and its stack if it was not preempted
* and idle the VP until the sa_id LWP unblocks * and the unblocked upcall was not yet delivered. Put the sa_id
* - recycles the to be unblocked LWP if the calling LWP was preempted * LWP on the VP and wait until it unblocks or switch to it if it's
* and returns control to the userspace library so it can switch to * ready. There will be no unblocked upcall.
* the blocked thread * - recycles the blocked LWP if up_preempted == NULL. This is used
* This is used if a thread blocks because of a pagefault and is in a * if the blocked LWP is an idle thread and we don't care for the
* critical section in the userspace library and the critical section * unblocked upcall.
* resolving code cannot continue until the blocked thread is unblocked. * - otherwise, wait for the blocked LWP to get ready. The unblocked
* If the userspace library switches to the blocked thread in the second * upcall is delivered when we return.
* case, it will either continue (because the pagefault has been handled) * This is used if a thread blocks (mostly because of a pagefault) and
* or it will pagefault again. The second pagefault will be detected by * is in a critical section in the userspace library and the critical
* the double pagefault code and the VP will idle until the pagefault * section resolving code cannot continue until the blocked thread is
* has been handled. * unblocked.
*/ */
int int
sys_sa_unblockyield(struct lwp *l, void *v, register_t *retval) sys_sa_unblockyield(struct lwp *l, void *v, register_t *retval)
@ -465,7 +466,7 @@ sys_sa_unblockyield(struct lwp *l, void *v, register_t *retval)
} */ *uap = v; } */ *uap = v;
struct sadata *sa = l->l_proc->p_sa; struct sadata *sa = l->l_proc->p_sa;
struct proc *p = l->l_proc; struct proc *p = l->l_proc;
struct lwp *l2; struct lwp *l2, **hp;
struct sastack sast; struct sastack sast;
int error, f, s; int error, f, s;
void *preempted; void *preempted;
@ -498,79 +499,100 @@ sys_sa_unblockyield(struct lwp *l, void *v, register_t *retval)
break; break;
} }
} }
if (l2 && (l2->l_upcallstack == NULL ||
(l2->l_upcallstack->sast_blocker != l2 &&
l2->l_upcallstack->sast_blocker != NULL)))
l2 = NULL;
if (l2 == NULL) { if (l2 == NULL) {
SCHED_UNLOCK(s); /* just return, prevented in userland most of the time */
return (ESRCH); DPRINTFN(11,("sys_sa_unblockyield(%d.%d) unblocked upcall done\n",
} p->p_pid, l->l_lid));
if (l2->l_upcallstack->sast_blocker != l2 || KDASSERT(preempted != NULL);
sast.sast_stack.ss_sp != l2->l_upcallstack->sast_stack.ss_sp) { } else if (preempted == (void *)-1) {
SCHED_UNLOCK(s); /* recycle blocked LWP */
return (EINVAL);
}
/*
* upcall not interrupted: (*up_preempted == NULL)
* - lwp ready: (wchan == upcallstacks)
* ==> recycle stack, put lwp on vp,
* unsleep lwp, make runnable, recycle upcall lwp (=l)
* - lwp not ready:
* ==> recycle stack, put lwp on vp, recycle upcall lwp (=l)
*
* upcall interrupted: (*up_preempted != NULL || up_preempted == NULL)
* ==> recycle upcall lwp
*/
if (preempted != NULL) {
DPRINTFN(11,("sys_sa_unblockyield(%d.%d) recycle %d " DPRINTFN(11,("sys_sa_unblockyield(%d.%d) recycle %d "
"(was %sready) upcall stack %p\n", "(was %sready) upcall stack %p\n",
p->p_pid, l->l_lid, l2->l_lid, p->p_pid, l->l_lid, l2->l_lid,
(l2->l_wchan == &l2->l_upcallstack) ? "" : (l2->l_upcallstack->sast_blocker == NULL) ? "" :
"not ", l2->l_upcallstack->sast_stack.ss_sp)); "not ", l2->l_upcallstack->sast_stack.ss_sp));
l2->l_upcallstack->sast_blocker = NULL; if (l2->l_upcallstack->sast_blocker == NULL) {
if (l2->l_wchan == &l2->l_upcallstack) { /*
unsleep(l2); * l2 is on the wokenq, remove it and put l2
if (l2->l_stat == LSSLEEP) { * in the cache
l2->l_slptime = 0; */
l2->l_stat = LSRUN; hp = &sa->sa_wokenq_head;
l2->l_proc->p_nrlwps++; while (*hp != l2)
if (l2->l_flag & L_INMEM) hp = &(*hp)->l_forw;
setrunqueue(l2); *hp = l2->l_forw;
else if (sa->sa_wokenq_tailp == &l2->l_forw)
sched_wakeup((caddr_t)&proc0); sa->sa_wokenq_tailp = hp;
} l2->l_flag &= ~L_SA_BLOCKING;
l2->l_upcallstack = NULL;
sa_putcachelwp(p, l2); /* PHOLD from sa_setwoken */
} else
/* let sa_setwoken put it in the cache */
l2->l_upcallstack->sast_blocker = NULL;
} else if (preempted != NULL) {
/* wait for the blocked LWP to get ready, then return */
DPRINTFN(11,("sys_sa_unblockyield(%d.%d) waiting for %d "
"(was %sready) upcall stack %p\n",
p->p_pid, l->l_lid, l2->l_lid,
(l2->l_upcallstack->sast_blocker == NULL) ? "" :
"not ", l2->l_upcallstack->sast_stack.ss_sp));
if (l2->l_upcallstack->sast_blocker != NULL) {
l2->l_upcallstack->sast_blocker = l;
SCHED_UNLOCK(s); /* XXXcl we're still holding
* the kernel lock, is that
* good enough? */
SA_LWP_STATE_LOCK(l, f);
tsleep((caddr_t) &l2->l_upcallstack, PWAIT,
"saunblock", 0);
SA_LWP_STATE_UNLOCK(l, f);
if (p->p_flag & P_WEXIT)
lwp_exit(l);
return(0);
} }
} else { } else {
/* recycle calling LWP and resume blocked LWP */
DPRINTFN(11,("sys_sa_unblockyield(%d.%d) resuming %d " DPRINTFN(11,("sys_sa_unblockyield(%d.%d) resuming %d "
"(is %sready) upcall stack %p\n", "(is %sready) upcall stack %p\n",
p->p_pid, l->l_lid, l2->l_lid, p->p_pid, l->l_lid, l2->l_lid,
(l2->l_wchan == &l2->l_upcallstack) ? "" : (l2->l_upcallstack->sast_blocker == NULL) ? "" :
"not ", l2->l_upcallstack->sast_stack.ss_sp)); "not ", l2->l_upcallstack->sast_stack.ss_sp));
sa->sa_vp = l2; sa->sa_vp = l2;
l2->l_flag &= ~L_SA_BLOCKING; l2->l_flag &= ~L_SA_BLOCKING;
l2->l_upcallstack->sast_blocker = NULL;
SLIST_INSERT_HEAD(&sa->sa_stackslist, l2->l_upcallstack, SLIST_INSERT_HEAD(&sa->sa_stackslist, l2->l_upcallstack,
sast_list); sast_list);
l2->l_upcallstack = NULL;
if (l2->l_wchan == &l2->l_upcallstack) { if (l2->l_upcallstack->sast_blocker == NULL) {
unsleep(l2); /*
if (l2->l_stat == LSSLEEP) { * l2 is on the wokenq, remove it and
l2->l_slptime = 0; * continue l2
l2->l_stat = LSRUN; */
l2->l_proc->p_nrlwps++; hp = &sa->sa_wokenq_head;
if (l2->l_flag & L_INMEM) while (*hp != l2)
setrunqueue(l2); hp = &(*hp)->l_forw;
else *hp = l2->l_forw;
sched_wakeup((caddr_t)&proc0); if (sa->sa_wokenq_tailp == &l2->l_forw)
} sa->sa_wokenq_tailp = hp;
l2->l_upcallstack = NULL;
setrunnable(l2);
PRELE(l2); /* PHOLD from sa_setwoken */
} else {
/* cleanup l_upcallstack */
l2->l_upcallstack->sast_blocker = NULL;
l2->l_upcallstack = NULL;
l2 = NULL; /* don't continue l2 yet */
} }
p->p_nrlwps--; p->p_nrlwps--;
PHOLD(l); PHOLD(l);
sa_putcachelwp(p, l); sa_putcachelwp(p, l);
mi_switch(l, NULL); mi_switch(l, l2);
/* mostly NOTREACHED */ /* mostly NOTREACHED */
SCHED_ASSERT_UNLOCKED(); SCHED_ASSERT_UNLOCKED();
splx(s); splx(s);
@ -856,6 +878,8 @@ sa_switch(struct lwp *l, int type)
return; return;
} }
DPRINTFN(8,("sa_switch(%d.%d) blocked upcall %d, stack %p\n",
p->p_pid, l->l_lid, l2->l_lid, sast->sast_stack.ss_sp));
SIMPLEQ_INSERT_TAIL(&sa->sa_upcalls, sau, sau_next); SIMPLEQ_INSERT_TAIL(&sa->sa_upcalls, sau, sau_next);
l2->l_flag |= L_SA_UPCALL; l2->l_flag |= L_SA_UPCALL;
@ -1036,50 +1060,24 @@ sa_unblock_userret(struct lwp *l)
DPRINTFN(7,("sa_unblock_userret(%d.%d %x) \n", p->p_pid, l->l_lid, DPRINTFN(7,("sa_unblock_userret(%d.%d %x) \n", p->p_pid, l->l_lid,
l->l_flag)); l->l_flag));
while (l->l_upcallstack != NULL) { sa_setwoken(l);
if (l->l_upcallstack->sast_blocker == NULL) { /* maybe NOTREACHED */
SCHED_LOCK(s);
l->l_flag &= ~(L_SA_UPCALL|L_SA_BLOCKING);
l->l_upcallstack = NULL;
p->p_nrlwps--;
PHOLD(l);
sa_putcachelwp(p, l);
SA_LWP_STATE_UNLOCK(l, f);
mi_switch(l, NULL);
/* mostly NOTREACHED */
SCHED_ASSERT_UNLOCKED();
splx(s);
KDASSERT(p->p_flag & P_WEXIT);
lwp_exit(l);
}
if ((l->l_flag & L_SA_BLOCKING) == 0) {
l->l_upcallstack->sast_blocker = NULL;
l->l_upcallstack = NULL;
break;
}
tsleep((caddr_t) &l->l_upcallstack, PWAIT,
"saunblock", 0);
if (p->p_flag & P_WEXIT)
lwp_exit(l);
}
if (l->l_flag & L_SA_BLOCKING) { SCHED_LOCK(s);
if (l != sa->sa_vp) {
/* Invoke an "unblocked" upcall */ /* Invoke an "unblocked" upcall */
sa_setwoken(l);
/* maybe NOTREACHED */
DPRINTFN(8,("sa_unblock_userret(%d.%d) unblocking\n", DPRINTFN(8,("sa_unblock_userret(%d.%d) unblocking\n",
p->p_pid, l->l_lid)); p->p_pid, l->l_lid));
SCHED_ASSERT_UNLOCKED();
l2 = sa_vp_repossess(l); l2 = sa_vp_repossess(l);
SCHED_UNLOCK(s);
if (l2 == NULL) if (l2 == NULL)
lwp_exit(l); lwp_exit(l);
SCHED_ASSERT_UNLOCKED(); PHOLD(l2);
sau = sadata_upcall_alloc(1); sau = sadata_upcall_alloc(1);
sau->sau_arg = NULL; sau->sau_arg = NULL;
@ -1113,13 +1111,14 @@ sa_unblock_userret(struct lwp *l)
/* NOTREACHED */ /* NOTREACHED */
} }
SCHED_LOCK(s);
SIMPLEQ_INSERT_TAIL(&sa->sa_upcalls, sau, sau_next); SIMPLEQ_INSERT_TAIL(&sa->sa_upcalls, sau, sau_next);
l->l_flag |= L_SA_UPCALL; l->l_flag |= L_SA_UPCALL;
l->l_flag &= ~L_SA_BLOCKING; l->l_flag &= ~L_SA_BLOCKING;
SCHED_LOCK(s); l->l_upcallstack = NULL;
sa_putcachelwp(p, l2); /* PHOLD from sa_vp_repossess */ sa_putcachelwp(p, l2);
SCHED_UNLOCK(s);
} }
SCHED_UNLOCK(s);
SA_LWP_STATE_UNLOCK(l, f); SA_LWP_STATE_UNLOCK(l, f);
KERNEL_PROC_UNLOCK(l); KERNEL_PROC_UNLOCK(l);
@ -1170,7 +1169,6 @@ sa_upcall_userret(struct lwp *l)
SCHED_UNLOCK(s); SCHED_UNLOCK(s);
PHOLD(l2);
if (p->p_flag & P_WEXIT) if (p->p_flag & P_WEXIT)
lwp_exit(l); lwp_exit(l);
@ -1201,8 +1199,9 @@ sa_upcall_userret(struct lwp *l)
SIMPLEQ_INSERT_TAIL(&sa->sa_upcalls, sau, sau_next); SIMPLEQ_INSERT_TAIL(&sa->sa_upcalls, sau, sau_next);
l2->l_flag &= ~L_SA_BLOCKING; l2->l_flag &= ~L_SA_BLOCKING;
l2->l_upcallstack = NULL;
SCHED_LOCK(s); SCHED_LOCK(s);
sa_putcachelwp(p, l2); sa_putcachelwp(p, l2); /* PHOLD from sa_setwoken */
} }
SCHED_UNLOCK(s); SCHED_UNLOCK(s);
@ -1314,11 +1313,11 @@ sa_upcall_userret(struct lwp *l)
eventq = l2->l_forw; eventq = l2->l_forw;
DPRINTFN(8,("sa_upcall_userret(%d.%d) unblocking extra %d\n", DPRINTFN(8,("sa_upcall_userret(%d.%d) unblocking extra %d\n",
p->p_pid, l->l_lid, l2->l_lid)); p->p_pid, l->l_lid, l2->l_lid));
PHOLD(l2);
sa_upcall_getstate(&e_ss, l2); sa_upcall_getstate(&e_ss, l2);
l2->l_flag &= ~L_SA_BLOCKING;
SCHED_LOCK(s); SCHED_LOCK(s);
sa_putcachelwp(p, l2); l2->l_flag &= ~L_SA_BLOCKING;
l2->l_upcallstack = NULL;
sa_putcachelwp(p, l2); /* PHOLD from sa_setwoken */
SCHED_UNLOCK(s); SCHED_UNLOCK(s);
if (copyout(&e_ss.ss_captured.ss_ctx, if (copyout(&e_ss.ss_captured.ss_ctx,
e_ss.ss_captured.ss_sa.sa_context, e_ss.ss_captured.ss_sa.sa_context,
@ -1399,6 +1398,11 @@ sa_setwoken(struct lwp *l)
SCHED_LOCK(s); SCHED_LOCK(s);
if ((l->l_flag & L_SA_BLOCKING) == 0) {
SCHED_UNLOCK(s);
return;
}
p = l->l_proc; p = l->l_proc;
sa = p->p_sa; sa = p->p_sa;
vp_lwp = sa->sa_vp; vp_lwp = sa->sa_vp;
@ -1409,6 +1413,23 @@ sa_setwoken(struct lwp *l)
l->l_proc->p_pid, l->l_lid, l->l_flag, l->l_proc->p_pid, l->l_lid, l->l_flag,
vp_lwp->l_lid)); vp_lwp->l_lid));
if (l->l_upcallstack && l->l_upcallstack->sast_blocker == NULL) {
DPRINTFN(11,("sa_setwoken(%d.%d) recycle\n",
l->l_proc->p_pid, l->l_lid));
l->l_flag &= ~(L_SA_UPCALL|L_SA_BLOCKING);
l->l_flag |= L_SA;
l->l_upcallstack = NULL;
p->p_nrlwps--;
PHOLD(l);
sa_putcachelwp(p, l);
mi_switch(l, NULL);
/* mostly NOTREACHED */
SCHED_ASSERT_UNLOCKED();
splx(s);
KDASSERT(p->p_flag & P_WEXIT);
lwp_exit(l);
}
#if notyet #if notyet
if (vp_lwp->l_flag & L_SA_IDLE) { if (vp_lwp->l_flag & L_SA_IDLE) {
KDASSERT((vp_lwp->l_flag & L_SA_UPCALL) == 0); KDASSERT((vp_lwp->l_flag & L_SA_UPCALL) == 0);
@ -1426,6 +1447,7 @@ sa_setwoken(struct lwp *l)
l->l_proc->p_pid, l->l_lid, vp_lwp->l_lid, l->l_proc->p_pid, l->l_lid, vp_lwp->l_lid,
vp_lwp->l_stat)); vp_lwp->l_stat));
PHOLD(l);
if (sa->sa_wokenq_head == NULL) if (sa->sa_wokenq_head == NULL)
sa->sa_wokenq_head = l; sa->sa_wokenq_head = l;
else else
@ -1482,6 +1504,12 @@ sa_setwoken(struct lwp *l)
panic("sa_vp LWP not sleeping/onproc/runnable"); panic("sa_vp LWP not sleeping/onproc/runnable");
} }
if (l->l_upcallstack) {
if (l->l_upcallstack->sast_blocker != l)
sched_wakeup(&l->l_upcallstack);
l->l_upcallstack->sast_blocker = NULL;
}
l->l_stat = LSSUSPENDED; l->l_stat = LSSUSPENDED;
p->p_nrlwps--; p->p_nrlwps--;
mi_switch(l, l2); mi_switch(l, l2);
@ -1498,7 +1526,8 @@ sa_vp_repossess(struct lwp *l)
struct lwp *l2; struct lwp *l2;
struct proc *p = l->l_proc; struct proc *p = l->l_proc;
struct sadata *sa = p->p_sa; struct sadata *sa = p->p_sa;
int s;
SCHED_ASSERT_LOCKED();
/* /*
* Put ourselves on the virtual processor and note that the * Put ourselves on the virtual processor and note that the
@ -1514,8 +1543,6 @@ sa_vp_repossess(struct lwp *l)
KDASSERT(l2 != l); KDASSERT(l2 != l);
if (l2) { if (l2) {
PHOLD(l2);
SCHED_LOCK(s);
switch (l2->l_stat) { switch (l2->l_stat) {
case LSRUN: case LSRUN:
remrunqueue(l2); remrunqueue(l2);
@ -1539,7 +1566,6 @@ sa_vp_repossess(struct lwp *l)
#endif #endif
} }
l2->l_stat = LSSUSPENDED; l2->l_stat = LSSUSPENDED;
SCHED_UNLOCK(s);
} }
return l2; return l2;
} }