2005-01-06 23:35:40 +00:00
|
|
|
/*-
|
2017-11-27 15:20:12 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2004-02-27 18:33:09 +00:00
|
|
|
* Copyright (c) 2004 John Baldwin <jhb@FreeBSD.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of sleep queues used to hold queue of threads blocked on
|
2017-03-03 21:03:28 +00:00
|
|
|
* a wait channel. Sleep queues are different from turnstiles in that wait
|
2004-02-27 18:33:09 +00:00
|
|
|
* channels are not owned by anyone, so there is no priority propagation.
|
|
|
|
* Sleep queues can also provide a timeout and can also be interrupted by
|
|
|
|
* signals. That said, there are several similarities between the turnstile
|
|
|
|
* and sleep queue implementations. (Note: turnstiles were implemented
|
|
|
|
* first.) For example, both use a hash table of the same size where each
|
|
|
|
* bucket is referred to as a "chain" that contains both a spin lock and
|
|
|
|
* a linked list of queues. An individual queue is located by using a hash
|
|
|
|
* to pick a chain, locking the chain, and then walking the chain searching
|
|
|
|
* for the queue. This means that a wait channel object does not need to
|
2017-03-03 21:03:28 +00:00
|
|
|
* embed its queue head just as locks do not embed their turnstile queue
|
2004-02-27 18:33:09 +00:00
|
|
|
* head. Threads also carry around a sleep queue that they lend to the
|
|
|
|
* wait channel when blocking. Just as in turnstiles, the queue includes
|
|
|
|
* a free list of the sleep queues of other threads blocked on the same
|
|
|
|
* wait channel in the case of multiple waiters.
|
|
|
|
*
|
|
|
|
* Some additional functionality provided by sleep queues include the
|
|
|
|
* ability to set a timeout. The timeout is managed using a per-thread
|
|
|
|
* callout that resumes a thread if it is asleep. A thread may also
|
|
|
|
* catch signals while it is asleep (aka an interruptible sleep). The
|
|
|
|
* signal code uses sleepq_abort() to interrupt a sleeping thread. Finally,
|
|
|
|
* sleep queues also provide some extra assertions. One is not allowed to
|
|
|
|
* mix the sleep/wakeup and cv APIs for a given wait channel. Also, one
|
|
|
|
* must consistently use the same lock to synchronize with a wait channel,
|
|
|
|
* though this check is currently only a warning for sleep/wakeup due to
|
|
|
|
* pre-existing abuse of that API. The same lock must also be held when
|
|
|
|
* awakening threads, though that is currently only enforced for condition
|
|
|
|
* variables.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2006-01-27 22:24:07 +00:00
|
|
|
#include "opt_sleepqueue_profiling.h"
|
|
|
|
#include "opt_ddb.h"
|
2007-06-12 23:27:31 +00:00
|
|
|
#include "opt_sched.h"
|
2016-03-17 01:05:53 +00:00
|
|
|
#include "opt_stack.h"
|
2006-01-27 22:24:07 +00:00
|
|
|
|
2004-02-27 18:33:09 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/ktr.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/proc.h>
|
2008-03-19 07:22:07 +00:00
|
|
|
#include <sys/sbuf.h>
|
2004-02-27 18:33:09 +00:00
|
|
|
#include <sys/sched.h>
|
2012-05-15 01:30:25 +00:00
|
|
|
#include <sys/sdt.h>
|
2004-02-27 18:33:09 +00:00
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/sleepqueue.h>
|
2016-03-16 04:22:32 +00:00
|
|
|
#include <sys/stack.h>
|
2004-06-29 02:30:12 +00:00
|
|
|
#include <sys/sysctl.h>
|
When the RTC is adjusted, reevaluate absolute sleep times based on the RTC
POSIX 2008 says this about clock_settime(2):
If the value of the CLOCK_REALTIME clock is set via clock_settime(),
the new value of the clock shall be used to determine the time
of expiration for absolute time services based upon the
CLOCK_REALTIME clock. This applies to the time at which armed
absolute timers expire. If the absolute time requested at the
invocation of such a time service is before the new value of
the clock, the time service shall expire immediately as if the
clock had reached the requested time normally.
Setting the value of the CLOCK_REALTIME clock via clock_settime()
shall have no effect on threads that are blocked waiting for
a relative time service based upon this clock, including the
nanosleep() function; nor on the expiration of relative timers
based upon this clock. Consequently, these time services shall
expire when the requested relative interval elapses, independently
of the new or old value of the clock.
When the real-time clock is adjusted, such as by clock_settime(3),
wake any threads sleeping until an absolute real-clock time.
Such a sleep is indicated by a non-zero td_rtcgen. The sleep functions
will set that field to zero and return zero to tell the caller
to reevaluate its sleep duration based on the new value of the clock.
At present, this affects the following functions:
pthread_cond_timedwait(3)
pthread_mutex_timedlock(3)
pthread_rwlock_timedrdlock(3)
pthread_rwlock_timedwrlock(3)
sem_timedwait(3)
sem_clockwait_np(3)
I'm working on adding clock_nanosleep(2), which will also be affected.
Reported by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Reviewed by: jhb, kib
MFC after: 2 weeks
Relnotes: yes
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D9791
2017-03-14 19:06:44 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include <machine/atomic.h>
|
2004-02-27 18:33:09 +00:00
|
|
|
|
2007-05-18 06:32:24 +00:00
|
|
|
#include <vm/uma.h>
|
|
|
|
|
2006-01-27 22:24:07 +00:00
|
|
|
#ifdef DDB
|
|
|
|
#include <ddb/ddb.h>
|
|
|
|
#endif
|
|
|
|
|
2016-03-16 04:22:32 +00:00
|
|
|
|
2004-02-27 18:33:09 +00:00
|
|
|
/*
|
2013-03-12 06:58:49 +00:00
|
|
|
* Constants for the hash table of sleep queue chains.
|
|
|
|
* SC_TABLESIZE must be a power of two for SC_MASK to work properly.
|
2004-02-27 18:33:09 +00:00
|
|
|
*/
|
2017-10-22 20:43:50 +00:00
|
|
|
#ifndef SC_TABLESIZE
|
|
|
|
#define SC_TABLESIZE 256
|
|
|
|
#endif
|
|
|
|
CTASSERT(powerof2(SC_TABLESIZE));
|
2004-02-27 18:33:09 +00:00
|
|
|
#define SC_MASK (SC_TABLESIZE - 1)
|
|
|
|
#define SC_SHIFT 8
|
2013-03-12 06:58:49 +00:00
|
|
|
#define SC_HASH(wc) ((((uintptr_t)(wc) >> SC_SHIFT) ^ (uintptr_t)(wc)) & \
|
|
|
|
SC_MASK)
|
2004-02-27 18:33:09 +00:00
|
|
|
#define SC_LOOKUP(wc) &sleepq_chains[SC_HASH(wc)]
|
2006-12-16 06:54:09 +00:00
|
|
|
#define NR_SLEEPQS 2
|
2004-02-27 18:33:09 +00:00
|
|
|
/*
|
2017-03-03 21:03:28 +00:00
|
|
|
* There are two different lists of sleep queues. Both lists are connected
|
2004-02-27 18:33:09 +00:00
|
|
|
* via the sq_hash entries. The first list is the sleep queue chain list
|
|
|
|
* that a sleep queue is on when it is attached to a wait channel. The
|
|
|
|
* second list is the free list hung off of a sleep queue that is attached
|
|
|
|
* to a wait channel.
|
|
|
|
*
|
|
|
|
* Each sleep queue also contains the wait channel it is attached to, the
|
|
|
|
* list of threads blocked on that wait channel, flags specific to the
|
|
|
|
* wait channel, and the lock used to synchronize with a wait channel.
|
|
|
|
* The flags are used to catch mismatches between the various consumers
|
|
|
|
* of the sleep queue API (e.g. sleep/wakeup and condition variables).
|
|
|
|
* The lock pointer is only used when invariants are enabled for various
|
|
|
|
* debugging checks.
|
|
|
|
*
|
|
|
|
* Locking key:
|
|
|
|
* c - sleep queue chain lock
|
|
|
|
*/
|
|
|
|
struct sleepqueue {
|
2006-12-16 06:54:09 +00:00
|
|
|
TAILQ_HEAD(, thread) sq_blocked[NR_SLEEPQS]; /* (c) Blocked threads. */
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
u_int sq_blockedcnt[NR_SLEEPQS]; /* (c) N. of blocked threads. */
|
2004-02-27 18:33:09 +00:00
|
|
|
LIST_ENTRY(sleepqueue) sq_hash; /* (c) Chain and free list. */
|
|
|
|
LIST_HEAD(, sleepqueue) sq_free; /* (c) Free queues. */
|
|
|
|
void *sq_wchan; /* (c) Wait channel. */
|
2004-10-12 18:36:20 +00:00
|
|
|
int sq_type; /* (c) Queue type. */
|
2010-01-09 01:46:38 +00:00
|
|
|
#ifdef INVARIANTS
|
2006-11-16 01:02:00 +00:00
|
|
|
struct lock_object *sq_lock; /* (c) Associated lock. */
|
2004-02-27 18:33:09 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sleepqueue_chain {
|
|
|
|
LIST_HEAD(, sleepqueue) sc_queues; /* List of sleep queues. */
|
|
|
|
struct mtx sc_lock; /* Spin lock for this chain. */
|
2004-06-29 02:30:12 +00:00
|
|
|
#ifdef SLEEPQUEUE_PROFILING
|
|
|
|
u_int sc_depth; /* Length of sc_queues. */
|
|
|
|
u_int sc_max_depth; /* Max length of sc_queues. */
|
|
|
|
#endif
|
2017-10-22 20:43:50 +00:00
|
|
|
} __aligned(CACHE_LINE_SIZE);
|
2004-02-27 18:33:09 +00:00
|
|
|
|
2004-06-29 02:30:12 +00:00
|
|
|
#ifdef SLEEPQUEUE_PROFILING
|
|
|
|
u_int sleepq_max_depth;
|
2011-11-07 15:43:11 +00:00
|
|
|
static SYSCTL_NODE(_debug, OID_AUTO, sleepq, CTLFLAG_RD, 0, "sleepq profiling");
|
|
|
|
static SYSCTL_NODE(_debug_sleepq, OID_AUTO, chains, CTLFLAG_RD, 0,
|
2004-06-29 02:30:12 +00:00
|
|
|
"sleepq chain stats");
|
|
|
|
SYSCTL_UINT(_debug_sleepq, OID_AUTO, max_depth, CTLFLAG_RD, &sleepq_max_depth,
|
|
|
|
0, "maxmimum depth achieved of a single chain");
|
2008-03-19 07:22:07 +00:00
|
|
|
|
|
|
|
static void sleepq_profile(const char *wmesg);
|
|
|
|
static int prof_enabled;
|
2004-06-29 02:30:12 +00:00
|
|
|
#endif
|
2004-02-27 18:33:09 +00:00
|
|
|
static struct sleepqueue_chain sleepq_chains[SC_TABLESIZE];
|
2007-05-18 06:32:24 +00:00
|
|
|
static uma_zone_t sleepq_zone;
|
2004-02-27 18:33:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Prototypes for non-exported routines.
|
|
|
|
*/
|
2008-03-12 06:31:06 +00:00
|
|
|
static int sleepq_catch_signals(void *wchan, int pri);
|
2006-12-16 06:54:09 +00:00
|
|
|
static int sleepq_check_signals(void);
|
2015-01-22 11:12:42 +00:00
|
|
|
static int sleepq_check_timeout(void);
|
2007-05-18 06:32:24 +00:00
|
|
|
#ifdef INVARIANTS
|
|
|
|
static void sleepq_dtor(void *mem, int size, void *arg);
|
|
|
|
#endif
|
|
|
|
static int sleepq_init(void *mem, int size, int flags);
|
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
2008-08-05 20:02:31 +00:00
|
|
|
static int sleepq_resume_thread(struct sleepqueue *sq, struct thread *td,
|
2007-05-18 06:32:24 +00:00
|
|
|
int pri);
|
2008-03-12 06:31:06 +00:00
|
|
|
static void sleepq_switch(void *wchan, int pri);
|
2004-02-27 18:33:09 +00:00
|
|
|
static void sleepq_timeout(void *arg);
|
|
|
|
|
2012-05-15 01:30:25 +00:00
|
|
|
SDT_PROBE_DECLARE(sched, , , sleep);
|
|
|
|
SDT_PROBE_DECLARE(sched, , , wakeup);
|
|
|
|
|
2004-02-27 18:33:09 +00:00
|
|
|
/*
|
2014-06-24 15:16:55 +00:00
|
|
|
* Initialize SLEEPQUEUE_PROFILING specific sysctl nodes.
|
|
|
|
* Note that it must happen after sleepinit() has been fully executed, so
|
|
|
|
* it must happen after SI_SUB_KMEM SYSINIT() subsystem setup.
|
2004-02-27 18:33:09 +00:00
|
|
|
*/
|
2004-06-29 02:30:12 +00:00
|
|
|
#ifdef SLEEPQUEUE_PROFILING
|
2014-06-24 15:16:55 +00:00
|
|
|
static void
|
|
|
|
init_sleepqueue_profiling(void)
|
|
|
|
{
|
2004-06-29 02:30:12 +00:00
|
|
|
char chain_name[10];
|
2014-06-24 15:16:55 +00:00
|
|
|
struct sysctl_oid *chain_oid;
|
|
|
|
u_int i;
|
2004-02-27 18:33:09 +00:00
|
|
|
|
|
|
|
for (i = 0; i < SC_TABLESIZE; i++) {
|
2014-06-24 15:16:55 +00:00
|
|
|
snprintf(chain_name, sizeof(chain_name), "%u", i);
|
2017-03-03 21:03:28 +00:00
|
|
|
chain_oid = SYSCTL_ADD_NODE(NULL,
|
2004-06-29 02:30:12 +00:00
|
|
|
SYSCTL_STATIC_CHILDREN(_debug_sleepq_chains), OID_AUTO,
|
|
|
|
chain_name, CTLFLAG_RD, NULL, "sleepq chain stats");
|
|
|
|
SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
|
|
|
|
"depth", CTLFLAG_RD, &sleepq_chains[i].sc_depth, 0, NULL);
|
|
|
|
SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
|
|
|
|
"max_depth", CTLFLAG_RD, &sleepq_chains[i].sc_max_depth, 0,
|
|
|
|
NULL);
|
2014-06-24 15:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSINIT(sleepqueue_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
|
|
|
|
init_sleepqueue_profiling, NULL);
|
2004-06-29 02:30:12 +00:00
|
|
|
#endif
|
2014-06-24 15:16:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Early initialization of sleep queues that is called from the sleepinit()
|
|
|
|
* SYSINIT.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
init_sleepqueues(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < SC_TABLESIZE; i++) {
|
|
|
|
LIST_INIT(&sleepq_chains[i].sc_queues);
|
|
|
|
mtx_init(&sleepq_chains[i].sc_lock, "sleepq chain", NULL,
|
|
|
|
MTX_SPIN | MTX_RECURSE);
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
2007-05-18 06:32:24 +00:00
|
|
|
sleepq_zone = uma_zcreate("SLEEPQUEUE", sizeof(struct sleepqueue),
|
|
|
|
#ifdef INVARIANTS
|
|
|
|
NULL, sleepq_dtor, sleepq_init, NULL, UMA_ALIGN_CACHE, 0);
|
|
|
|
#else
|
|
|
|
NULL, NULL, sleepq_init, NULL, UMA_ALIGN_CACHE, 0);
|
|
|
|
#endif
|
2017-03-03 21:03:28 +00:00
|
|
|
|
2004-02-27 18:33:09 +00:00
|
|
|
thread0.td_sleepqueue = sleepq_alloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-05-18 06:32:24 +00:00
|
|
|
* Get a sleep queue for a new thread.
|
2004-02-27 18:33:09 +00:00
|
|
|
*/
|
|
|
|
struct sleepqueue *
|
|
|
|
sleepq_alloc(void)
|
|
|
|
{
|
|
|
|
|
2007-05-18 06:32:24 +00:00
|
|
|
return (uma_zalloc(sleepq_zone, M_WAITOK));
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free a sleep queue when a thread is destroyed.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sleepq_free(struct sleepqueue *sq)
|
|
|
|
{
|
|
|
|
|
2007-05-18 06:32:24 +00:00
|
|
|
uma_zfree(sleepq_zone, sq);
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
2004-10-12 18:36:20 +00:00
|
|
|
/*
|
|
|
|
* Lock the sleep queue chain associated with the specified wait channel.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sleepq_lock(void *wchan)
|
|
|
|
{
|
|
|
|
struct sleepqueue_chain *sc;
|
|
|
|
|
|
|
|
sc = SC_LOOKUP(wchan);
|
|
|
|
mtx_lock_spin(&sc->sc_lock);
|
|
|
|
}
|
|
|
|
|
2004-02-27 18:33:09 +00:00
|
|
|
/*
|
|
|
|
* Look up the sleep queue associated with a given wait channel in the hash
|
2004-10-12 18:36:20 +00:00
|
|
|
* table locking the associated sleep queue chain. If no queue is found in
|
|
|
|
* the table, NULL is returned.
|
2004-02-27 18:33:09 +00:00
|
|
|
*/
|
|
|
|
struct sleepqueue *
|
|
|
|
sleepq_lookup(void *wchan)
|
|
|
|
{
|
|
|
|
struct sleepqueue_chain *sc;
|
|
|
|
struct sleepqueue *sq;
|
|
|
|
|
|
|
|
KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
|
|
|
|
sc = SC_LOOKUP(wchan);
|
2004-10-12 18:36:20 +00:00
|
|
|
mtx_assert(&sc->sc_lock, MA_OWNED);
|
2004-02-27 18:33:09 +00:00
|
|
|
LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
|
|
|
|
if (sq->sq_wchan == wchan)
|
|
|
|
return (sq);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unlock the sleep queue chain associated with a given wait channel.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sleepq_release(void *wchan)
|
|
|
|
{
|
|
|
|
struct sleepqueue_chain *sc;
|
|
|
|
|
|
|
|
sc = SC_LOOKUP(wchan);
|
|
|
|
mtx_unlock_spin(&sc->sc_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-11-05 20:19:58 +00:00
|
|
|
* Places the current thread on the sleep queue for the specified wait
|
2004-02-27 18:33:09 +00:00
|
|
|
* channel. If INVARIANTS is enabled, then it associates the passed in
|
|
|
|
* lock with the sleepq to make sure it is held when that sleep queue is
|
|
|
|
* woken up.
|
|
|
|
*/
|
|
|
|
void
|
2006-12-16 06:54:09 +00:00
|
|
|
sleepq_add(void *wchan, struct lock_object *lock, const char *wmesg, int flags,
|
|
|
|
int queue)
|
2004-02-27 18:33:09 +00:00
|
|
|
{
|
|
|
|
struct sleepqueue_chain *sc;
|
2004-10-12 18:36:20 +00:00
|
|
|
struct sleepqueue *sq;
|
2004-11-05 20:19:58 +00:00
|
|
|
struct thread *td;
|
2004-02-27 18:33:09 +00:00
|
|
|
|
|
|
|
td = curthread;
|
|
|
|
sc = SC_LOOKUP(wchan);
|
|
|
|
mtx_assert(&sc->sc_lock, MA_OWNED);
|
|
|
|
MPASS(td->td_sleepqueue != NULL);
|
|
|
|
MPASS(wchan != NULL);
|
2006-12-16 06:54:09 +00:00
|
|
|
MPASS((queue >= 0) && (queue < NR_SLEEPQS));
|
2004-02-27 18:33:09 +00:00
|
|
|
|
2005-09-15 19:05:37 +00:00
|
|
|
/* If this thread is not allowed to sleep, die a horrible death. */
|
2013-03-01 22:03:31 +00:00
|
|
|
KASSERT(td->td_no_sleeping == 0,
|
|
|
|
("%s: td %p to sleep on wchan %p with sleeping prohibited",
|
2012-09-12 22:05:54 +00:00
|
|
|
__func__, td, wchan));
|
2005-09-15 19:05:37 +00:00
|
|
|
|
2004-10-12 18:36:20 +00:00
|
|
|
/* Look up the sleep queue associated with the wait channel 'wchan'. */
|
|
|
|
sq = sleepq_lookup(wchan);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the wait channel does not already have a sleep queue, use
|
|
|
|
* this thread's sleep queue. Otherwise, insert the current thread
|
|
|
|
* into the sleep queue already in use by this wait channel.
|
|
|
|
*/
|
2004-02-27 18:33:09 +00:00
|
|
|
if (sq == NULL) {
|
2006-12-16 06:54:09 +00:00
|
|
|
#ifdef INVARIANTS
|
2006-12-17 00:14:20 +00:00
|
|
|
int i;
|
2006-12-16 21:17:27 +00:00
|
|
|
|
2006-12-17 00:14:20 +00:00
|
|
|
sq = td->td_sleepqueue;
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
for (i = 0; i < NR_SLEEPQS; i++) {
|
2006-12-17 00:14:20 +00:00
|
|
|
KASSERT(TAILQ_EMPTY(&sq->sq_blocked[i]),
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
("thread's sleep queue %d is not empty", i));
|
|
|
|
KASSERT(sq->sq_blockedcnt[i] == 0,
|
|
|
|
("thread's sleep queue %d count mismatches", i));
|
|
|
|
}
|
2006-12-16 06:54:09 +00:00
|
|
|
KASSERT(LIST_EMPTY(&sq->sq_free),
|
|
|
|
("thread's sleep queue has a non-empty free list"));
|
|
|
|
KASSERT(sq->sq_wchan == NULL, ("stale sq_wchan pointer"));
|
2006-12-17 00:14:20 +00:00
|
|
|
sq->sq_lock = lock;
|
2006-12-16 06:54:09 +00:00
|
|
|
#endif
|
2004-06-29 02:30:12 +00:00
|
|
|
#ifdef SLEEPQUEUE_PROFILING
|
|
|
|
sc->sc_depth++;
|
|
|
|
if (sc->sc_depth > sc->sc_max_depth) {
|
|
|
|
sc->sc_max_depth = sc->sc_depth;
|
|
|
|
if (sc->sc_max_depth > sleepq_max_depth)
|
|
|
|
sleepq_max_depth = sc->sc_max_depth;
|
|
|
|
}
|
|
|
|
#endif
|
2006-12-17 00:14:20 +00:00
|
|
|
sq = td->td_sleepqueue;
|
2004-02-27 18:33:09 +00:00
|
|
|
LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash);
|
|
|
|
sq->sq_wchan = wchan;
|
2010-01-09 01:46:38 +00:00
|
|
|
sq->sq_type = flags & SLEEPQ_TYPE;
|
2004-02-27 18:33:09 +00:00
|
|
|
} else {
|
|
|
|
MPASS(wchan == sq->sq_wchan);
|
2004-03-02 15:02:08 +00:00
|
|
|
MPASS(lock == sq->sq_lock);
|
2004-10-12 18:36:20 +00:00
|
|
|
MPASS((flags & SLEEPQ_TYPE) == sq->sq_type);
|
2004-02-27 18:33:09 +00:00
|
|
|
LIST_INSERT_HEAD(&sq->sq_free, td->td_sleepqueue, sq_hash);
|
|
|
|
}
|
2007-09-13 09:12:36 +00:00
|
|
|
thread_lock(td);
|
2006-12-16 06:54:09 +00:00
|
|
|
TAILQ_INSERT_TAIL(&sq->sq_blocked[queue], td, td_slpq);
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
sq->sq_blockedcnt[queue]++;
|
2004-02-27 18:33:09 +00:00
|
|
|
td->td_sleepqueue = NULL;
|
2006-12-16 06:54:09 +00:00
|
|
|
td->td_sqqueue = queue;
|
2004-02-27 18:33:09 +00:00
|
|
|
td->td_wchan = wchan;
|
|
|
|
td->td_wmesg = wmesg;
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
if (flags & SLEEPQ_INTERRUPTIBLE) {
|
2004-08-19 11:31:42 +00:00
|
|
|
td->td_flags |= TDF_SINTR;
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
td->td_flags &= ~TDF_SLEEPABORT;
|
|
|
|
}
|
2007-09-13 09:12:36 +00:00
|
|
|
thread_unlock(td);
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets a timeout that will remove the current thread from the specified
|
|
|
|
* sleep queue after timo ticks if the thread has not already been awakened.
|
|
|
|
*/
|
|
|
|
void
|
2013-03-04 11:51:46 +00:00
|
|
|
sleepq_set_timeout_sbt(void *wchan, sbintime_t sbt, sbintime_t pr,
|
|
|
|
int flags)
|
2004-02-27 18:33:09 +00:00
|
|
|
{
|
2018-05-19 05:00:16 +00:00
|
|
|
struct sleepqueue_chain *sc __unused;
|
2004-02-27 18:33:09 +00:00
|
|
|
struct thread *td;
|
2016-07-28 09:09:55 +00:00
|
|
|
sbintime_t pr1;
|
2004-02-27 18:33:09 +00:00
|
|
|
|
|
|
|
td = curthread;
|
2015-01-22 11:12:42 +00:00
|
|
|
sc = SC_LOOKUP(wchan);
|
|
|
|
mtx_assert(&sc->sc_lock, MA_OWNED);
|
|
|
|
MPASS(TD_ON_SLEEPQ(td));
|
|
|
|
MPASS(td->td_sleepqueue == NULL);
|
|
|
|
MPASS(wchan != NULL);
|
2016-11-25 18:02:43 +00:00
|
|
|
if (cold && td == &thread0)
|
2016-03-31 18:10:29 +00:00
|
|
|
panic("timed sleep before timers are working");
|
2016-07-28 09:09:55 +00:00
|
|
|
KASSERT(td->td_sleeptimo == 0, ("td %d %p td_sleeptimo %jx",
|
|
|
|
td->td_tid, td, (uintmax_t)td->td_sleeptimo));
|
|
|
|
thread_lock(td);
|
|
|
|
callout_when(sbt, pr, flags, &td->td_sleeptimo, &pr1);
|
|
|
|
thread_unlock(td);
|
|
|
|
callout_reset_sbt_on(&td->td_slpcallout, td->td_sleeptimo, pr1,
|
|
|
|
sleepq_timeout, td, PCPU_GET(cpuid), flags | C_PRECALC |
|
|
|
|
C_DIRECT_EXEC);
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
/*
|
|
|
|
* Return the number of actual sleepers for the specified queue.
|
|
|
|
*/
|
|
|
|
u_int
|
|
|
|
sleepq_sleepcnt(void *wchan, int queue)
|
|
|
|
{
|
|
|
|
struct sleepqueue *sq;
|
|
|
|
|
|
|
|
KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
|
|
|
|
MPASS((queue >= 0) && (queue < NR_SLEEPQS));
|
|
|
|
sq = sleepq_lookup(wchan);
|
|
|
|
if (sq == NULL)
|
|
|
|
return (0);
|
|
|
|
return (sq->sq_blockedcnt[queue]);
|
|
|
|
}
|
|
|
|
|
2004-02-27 18:33:09 +00:00
|
|
|
/*
|
|
|
|
* Marks the pending sleep of the current thread as interruptible and
|
|
|
|
* makes an initial check for pending signals before putting a thread
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
* to sleep. Enters and exits with the thread lock held. Thread lock
|
|
|
|
* may have transitioned from the sleepq lock to a run lock.
|
2004-02-27 18:33:09 +00:00
|
|
|
*/
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
static int
|
2008-03-12 06:31:06 +00:00
|
|
|
sleepq_catch_signals(void *wchan, int pri)
|
2004-02-27 18:33:09 +00:00
|
|
|
{
|
|
|
|
struct sleepqueue_chain *sc;
|
|
|
|
struct sleepqueue *sq;
|
|
|
|
struct thread *td;
|
|
|
|
struct proc *p;
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
struct sigacts *ps;
|
2013-03-18 17:23:58 +00:00
|
|
|
int sig, ret;
|
2004-02-27 18:33:09 +00:00
|
|
|
|
2017-02-14 17:13:23 +00:00
|
|
|
ret = 0;
|
2004-02-27 18:33:09 +00:00
|
|
|
td = curthread;
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
p = curproc;
|
2004-02-27 18:33:09 +00:00
|
|
|
sc = SC_LOOKUP(wchan);
|
|
|
|
mtx_assert(&sc->sc_lock, MA_OWNED);
|
|
|
|
MPASS(wchan != NULL);
|
2010-08-20 04:28:30 +00:00
|
|
|
if ((td->td_pflags & TDP_WAKEUP) != 0) {
|
|
|
|
td->td_pflags &= ~TDP_WAKEUP;
|
|
|
|
ret = EINTR;
|
2010-08-20 23:51:34 +00:00
|
|
|
thread_lock(td);
|
2010-08-20 04:28:30 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-03-19 07:35:14 +00:00
|
|
|
/*
|
2017-02-14 17:13:23 +00:00
|
|
|
* See if there are any pending signals or suspension requests for this
|
|
|
|
* thread. If not, we can switch immediately.
|
2008-03-19 07:35:14 +00:00
|
|
|
*/
|
|
|
|
thread_lock(td);
|
2017-02-14 17:13:23 +00:00
|
|
|
if ((td->td_flags & (TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK)) != 0) {
|
|
|
|
thread_unlock(td);
|
|
|
|
mtx_unlock_spin(&sc->sc_lock);
|
|
|
|
CTR3(KTR_PROC, "sleepq catching signals: thread %p (pid %ld, %s)",
|
|
|
|
(void *)td, (long)p->p_pid, td->td_name);
|
|
|
|
PROC_LOCK(p);
|
|
|
|
/*
|
|
|
|
* Check for suspension first. Checking for signals and then
|
|
|
|
* suspending could result in a missed signal, since a signal
|
|
|
|
* can be delivered while this thread is suspended.
|
|
|
|
*/
|
|
|
|
if ((td->td_flags & TDF_NEEDSUSPCHK) != 0) {
|
|
|
|
ret = thread_suspend_check(1);
|
|
|
|
MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
|
|
|
|
if (ret != 0) {
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
mtx_lock_spin(&sc->sc_lock);
|
|
|
|
thread_lock(td);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((td->td_flags & TDF_NEEDSIGCHK) != 0) {
|
|
|
|
ps = p->p_sigacts;
|
|
|
|
mtx_lock(&ps->ps_mtx);
|
|
|
|
sig = cursig(td);
|
|
|
|
if (sig == -1) {
|
|
|
|
mtx_unlock(&ps->ps_mtx);
|
|
|
|
KASSERT((td->td_flags & TDF_SBDRY) != 0,
|
|
|
|
("lost TDF_SBDRY"));
|
|
|
|
KASSERT(TD_SBDRY_INTR(td),
|
|
|
|
("lost TDF_SERESTART of TDF_SEINTR"));
|
|
|
|
KASSERT((td->td_flags &
|
|
|
|
(TDF_SEINTR | TDF_SERESTART)) !=
|
|
|
|
(TDF_SEINTR | TDF_SERESTART),
|
|
|
|
("both TDF_SEINTR and TDF_SERESTART"));
|
|
|
|
ret = TD_SBDRY_ERRNO(td);
|
|
|
|
} else if (sig != 0) {
|
|
|
|
ret = SIGISMEMBER(ps->ps_sigintr, sig) ?
|
|
|
|
EINTR : ERESTART;
|
|
|
|
mtx_unlock(&ps->ps_mtx);
|
|
|
|
} else {
|
|
|
|
mtx_unlock(&ps->ps_mtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Lock the per-process spinlock prior to dropping the PROC_LOCK
|
|
|
|
* to avoid a signal delivery race. PROC_LOCK, PROC_SLOCK, and
|
|
|
|
* thread_lock() are currently held in tdsendsignal().
|
|
|
|
*/
|
|
|
|
PROC_SLOCK(p);
|
|
|
|
mtx_lock_spin(&sc->sc_lock);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
thread_lock(td);
|
|
|
|
PROC_SUNLOCK(p);
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
}
|
2008-12-01 01:54:55 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
sleepq_switch(wchan, pri);
|
|
|
|
return (0);
|
|
|
|
}
|
2010-08-20 04:28:30 +00:00
|
|
|
out:
|
2006-02-23 03:42:17 +00:00
|
|
|
/*
|
|
|
|
* There were pending signals and this thread is still
|
|
|
|
* on the sleep queue, remove it from the sleep queue.
|
|
|
|
*/
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
if (TD_ON_SLEEPQ(td)) {
|
|
|
|
sq = sleepq_lookup(wchan);
|
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
2008-08-05 20:02:31 +00:00
|
|
|
if (sleepq_resume_thread(sq, td, 0)) {
|
|
|
|
#ifdef INVARIANTS
|
|
|
|
/*
|
|
|
|
* This thread hasn't gone to sleep yet, so it
|
|
|
|
* should not be swapped out.
|
|
|
|
*/
|
|
|
|
panic("not waking up swapper");
|
|
|
|
#endif
|
|
|
|
}
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
}
|
|
|
|
mtx_unlock_spin(&sc->sc_lock);
|
|
|
|
MPASS(td->td_lock != &sc->sc_lock);
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
return (ret);
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
* Switches to another thread if we are still asleep on a sleep queue.
|
|
|
|
* Returns with thread lock.
|
2004-02-27 18:33:09 +00:00
|
|
|
*/
|
|
|
|
static void
|
2008-03-12 06:31:06 +00:00
|
|
|
sleepq_switch(void *wchan, int pri)
|
2004-02-27 18:33:09 +00:00
|
|
|
{
|
|
|
|
struct sleepqueue_chain *sc;
|
2008-01-25 02:09:38 +00:00
|
|
|
struct sleepqueue *sq;
|
2004-02-27 18:33:09 +00:00
|
|
|
struct thread *td;
|
When the RTC is adjusted, reevaluate absolute sleep times based on the RTC
POSIX 2008 says this about clock_settime(2):
If the value of the CLOCK_REALTIME clock is set via clock_settime(),
the new value of the clock shall be used to determine the time
of expiration for absolute time services based upon the
CLOCK_REALTIME clock. This applies to the time at which armed
absolute timers expire. If the absolute time requested at the
invocation of such a time service is before the new value of
the clock, the time service shall expire immediately as if the
clock had reached the requested time normally.
Setting the value of the CLOCK_REALTIME clock via clock_settime()
shall have no effect on threads that are blocked waiting for
a relative time service based upon this clock, including the
nanosleep() function; nor on the expiration of relative timers
based upon this clock. Consequently, these time services shall
expire when the requested relative interval elapses, independently
of the new or old value of the clock.
When the real-time clock is adjusted, such as by clock_settime(3),
wake any threads sleeping until an absolute real-clock time.
Such a sleep is indicated by a non-zero td_rtcgen. The sleep functions
will set that field to zero and return zero to tell the caller
to reevaluate its sleep duration based on the new value of the clock.
At present, this affects the following functions:
pthread_cond_timedwait(3)
pthread_mutex_timedlock(3)
pthread_rwlock_timedrdlock(3)
pthread_rwlock_timedwrlock(3)
sem_timedwait(3)
sem_clockwait_np(3)
I'm working on adding clock_nanosleep(2), which will also be affected.
Reported by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Reviewed by: jhb, kib
MFC after: 2 weeks
Relnotes: yes
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D9791
2017-03-14 19:06:44 +00:00
|
|
|
bool rtc_changed;
|
2004-02-27 18:33:09 +00:00
|
|
|
|
|
|
|
td = curthread;
|
|
|
|
sc = SC_LOOKUP(wchan);
|
|
|
|
mtx_assert(&sc->sc_lock, MA_OWNED);
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
THREAD_LOCK_ASSERT(td, MA_OWNED);
|
2008-01-25 02:09:38 +00:00
|
|
|
|
2017-03-03 21:03:28 +00:00
|
|
|
/*
|
2008-01-25 02:09:38 +00:00
|
|
|
* If we have a sleep queue, then we've already been woken up, so
|
|
|
|
* just return.
|
|
|
|
*/
|
2004-02-27 18:33:09 +00:00
|
|
|
if (td->td_sleepqueue != NULL) {
|
|
|
|
mtx_unlock_spin(&sc->sc_lock);
|
|
|
|
return;
|
|
|
|
}
|
2008-01-25 02:09:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If TDF_TIMEOUT is set, then our sleep has been timed out
|
|
|
|
* already but we are still on the sleep queue, so dequeue the
|
2017-03-14 22:02:02 +00:00
|
|
|
* thread and return.
|
|
|
|
*
|
|
|
|
* Do the same if the real-time clock has been adjusted since this
|
|
|
|
* thread calculated its timeout based on that clock. This handles
|
|
|
|
* the following race:
|
|
|
|
* - The Ts thread needs to sleep until an absolute real-clock time.
|
|
|
|
* It copies the global rtc_generation into curthread->td_rtcgen,
|
|
|
|
* reads the RTC, and calculates a sleep duration based on that time.
|
|
|
|
* See umtxq_sleep() for an example.
|
|
|
|
* - The Tc thread adjusts the RTC, bumps rtc_generation, and wakes
|
|
|
|
* threads that are sleeping until an absolute real-clock time.
|
|
|
|
* See tc_setclock() and the POSIX specification of clock_settime().
|
|
|
|
* - Ts reaches the code below. It holds the sleepqueue chain lock,
|
|
|
|
* so Tc has finished waking, so this thread must test td_rtcgen.
|
|
|
|
* (The declaration of td_rtcgen refers to this comment.)
|
2008-01-25 02:09:38 +00:00
|
|
|
*/
|
When the RTC is adjusted, reevaluate absolute sleep times based on the RTC
POSIX 2008 says this about clock_settime(2):
If the value of the CLOCK_REALTIME clock is set via clock_settime(),
the new value of the clock shall be used to determine the time
of expiration for absolute time services based upon the
CLOCK_REALTIME clock. This applies to the time at which armed
absolute timers expire. If the absolute time requested at the
invocation of such a time service is before the new value of
the clock, the time service shall expire immediately as if the
clock had reached the requested time normally.
Setting the value of the CLOCK_REALTIME clock via clock_settime()
shall have no effect on threads that are blocked waiting for
a relative time service based upon this clock, including the
nanosleep() function; nor on the expiration of relative timers
based upon this clock. Consequently, these time services shall
expire when the requested relative interval elapses, independently
of the new or old value of the clock.
When the real-time clock is adjusted, such as by clock_settime(3),
wake any threads sleeping until an absolute real-clock time.
Such a sleep is indicated by a non-zero td_rtcgen. The sleep functions
will set that field to zero and return zero to tell the caller
to reevaluate its sleep duration based on the new value of the clock.
At present, this affects the following functions:
pthread_cond_timedwait(3)
pthread_mutex_timedlock(3)
pthread_rwlock_timedrdlock(3)
pthread_rwlock_timedwrlock(3)
sem_timedwait(3)
sem_clockwait_np(3)
I'm working on adding clock_nanosleep(2), which will also be affected.
Reported by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Reviewed by: jhb, kib
MFC after: 2 weeks
Relnotes: yes
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D9791
2017-03-14 19:06:44 +00:00
|
|
|
rtc_changed = td->td_rtcgen != 0 && td->td_rtcgen != rtc_generation;
|
|
|
|
if ((td->td_flags & TDF_TIMEOUT) || rtc_changed) {
|
|
|
|
if (rtc_changed) {
|
|
|
|
td->td_rtcgen = 0;
|
|
|
|
}
|
2008-01-25 02:09:38 +00:00
|
|
|
MPASS(TD_ON_SLEEPQ(td));
|
|
|
|
sq = sleepq_lookup(wchan);
|
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
2008-08-05 20:02:31 +00:00
|
|
|
if (sleepq_resume_thread(sq, td, 0)) {
|
|
|
|
#ifdef INVARIANTS
|
|
|
|
/*
|
|
|
|
* This thread hasn't gone to sleep yet, so it
|
|
|
|
* should not be swapped out.
|
|
|
|
*/
|
|
|
|
panic("not waking up swapper");
|
|
|
|
#endif
|
|
|
|
}
|
2008-01-25 02:09:38 +00:00
|
|
|
mtx_unlock_spin(&sc->sc_lock);
|
2017-03-03 21:03:28 +00:00
|
|
|
return;
|
2008-01-25 02:09:38 +00:00
|
|
|
}
|
2008-03-19 07:22:07 +00:00
|
|
|
#ifdef SLEEPQUEUE_PROFILING
|
|
|
|
if (prof_enabled)
|
|
|
|
sleepq_profile(td->td_wmesg);
|
|
|
|
#endif
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
MPASS(td->td_sleepqueue == NULL);
|
2008-03-12 06:31:06 +00:00
|
|
|
sched_sleep(td, pri);
|
|
|
|
thread_lock_set(td, &sc->sc_lock);
|
2012-05-15 01:30:25 +00:00
|
|
|
SDT_PROBE0(sched, , , sleep);
|
2004-02-27 18:33:09 +00:00
|
|
|
TD_SET_SLEEPING(td);
|
2008-04-17 04:20:10 +00:00
|
|
|
mi_switch(SW_VOL | SWT_SLEEPQ, NULL);
|
2004-02-27 18:33:09 +00:00
|
|
|
KASSERT(TD_IS_RUNNING(td), ("running but not TDS_RUNNING"));
|
2004-05-14 20:51:42 +00:00
|
|
|
CTR3(KTR_PROC, "sleepq resume: thread %p (pid %ld, %s)",
|
2007-11-14 06:21:24 +00:00
|
|
|
(void *)td, (long)td->td_proc->p_pid, (void *)td->td_name);
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if we timed out.
|
|
|
|
*/
|
|
|
|
static int
|
2015-01-22 11:12:42 +00:00
|
|
|
sleepq_check_timeout(void)
|
2004-02-27 18:33:09 +00:00
|
|
|
{
|
2015-01-22 11:12:42 +00:00
|
|
|
struct thread *td;
|
2016-07-28 09:09:55 +00:00
|
|
|
int res;
|
2015-01-22 11:12:42 +00:00
|
|
|
|
|
|
|
td = curthread;
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
THREAD_LOCK_ASSERT(td, MA_OWNED);
|
2004-02-27 18:33:09 +00:00
|
|
|
|
|
|
|
/*
|
2016-07-28 09:09:55 +00:00
|
|
|
* If TDF_TIMEOUT is set, we timed out. But recheck
|
|
|
|
* td_sleeptimo anyway.
|
2015-01-22 11:12:42 +00:00
|
|
|
*/
|
2016-07-28 09:09:55 +00:00
|
|
|
res = 0;
|
|
|
|
if (td->td_sleeptimo != 0) {
|
|
|
|
if (td->td_sleeptimo <= sbinuptime())
|
|
|
|
res = EWOULDBLOCK;
|
|
|
|
td->td_sleeptimo = 0;
|
2015-01-22 11:12:42 +00:00
|
|
|
}
|
2016-07-28 09:09:55 +00:00
|
|
|
if (td->td_flags & TDF_TIMEOUT)
|
|
|
|
td->td_flags &= ~TDF_TIMEOUT;
|
|
|
|
else
|
|
|
|
/*
|
|
|
|
* We ignore the situation where timeout subsystem was
|
|
|
|
* unable to stop our callout. The struct thread is
|
|
|
|
* type-stable, the callout will use the correct
|
|
|
|
* memory when running. The checks of the
|
|
|
|
* td_sleeptimo value in this function and in
|
|
|
|
* sleepq_timeout() ensure that the thread does not
|
|
|
|
* get spurious wakeups, even if the callout was reset
|
|
|
|
* or thread reused.
|
|
|
|
*/
|
|
|
|
callout_stop(&td->td_slpcallout);
|
|
|
|
return (res);
|
2015-01-15 15:32:30 +00:00
|
|
|
}
|
|
|
|
|
2004-02-27 18:33:09 +00:00
|
|
|
/*
|
|
|
|
* Check to see if we were awoken by a signal.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sleepq_check_signals(void)
|
|
|
|
{
|
|
|
|
struct thread *td;
|
|
|
|
|
|
|
|
td = curthread;
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
THREAD_LOCK_ASSERT(td, MA_OWNED);
|
2004-02-27 18:33:09 +00:00
|
|
|
|
|
|
|
/* We are no longer in an interruptible sleep. */
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
if (td->td_flags & TDF_SINTR)
|
2013-02-06 17:06:51 +00:00
|
|
|
td->td_flags &= ~TDF_SINTR;
|
2004-02-27 18:33:09 +00:00
|
|
|
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
if (td->td_flags & TDF_SLEEPABORT) {
|
|
|
|
td->td_flags &= ~TDF_SLEEPABORT;
|
2004-02-27 18:33:09 +00:00
|
|
|
return (td->td_intrval);
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
}
|
2004-02-27 18:33:09 +00:00
|
|
|
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
return (0);
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Block the current thread until it is awakened from its sleep queue.
|
|
|
|
*/
|
|
|
|
void
|
2008-03-12 06:31:06 +00:00
|
|
|
sleepq_wait(void *wchan, int pri)
|
2004-02-27 18:33:09 +00:00
|
|
|
{
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
struct thread *td;
|
2004-02-27 18:33:09 +00:00
|
|
|
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
td = curthread;
|
|
|
|
MPASS(!(td->td_flags & TDF_SINTR));
|
|
|
|
thread_lock(td);
|
2008-03-12 06:31:06 +00:00
|
|
|
sleepq_switch(wchan, pri);
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
thread_unlock(td);
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Block the current thread until it is awakened from its sleep queue
|
|
|
|
* or it is interrupted by a signal.
|
|
|
|
*/
|
|
|
|
int
|
2008-03-12 06:31:06 +00:00
|
|
|
sleepq_wait_sig(void *wchan, int pri)
|
2004-02-27 18:33:09 +00:00
|
|
|
{
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
int rcatch;
|
2004-02-27 18:33:09 +00:00
|
|
|
int rval;
|
|
|
|
|
2008-03-12 06:31:06 +00:00
|
|
|
rcatch = sleepq_catch_signals(wchan, pri);
|
2004-02-27 18:33:09 +00:00
|
|
|
rval = sleepq_check_signals();
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
thread_unlock(curthread);
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
if (rcatch)
|
|
|
|
return (rcatch);
|
2004-02-27 18:33:09 +00:00
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Block the current thread until it is awakened from its sleep queue
|
|
|
|
* or it times out while waiting.
|
|
|
|
*/
|
|
|
|
int
|
2008-03-12 06:31:06 +00:00
|
|
|
sleepq_timedwait(void *wchan, int pri)
|
2004-02-27 18:33:09 +00:00
|
|
|
{
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
struct thread *td;
|
2004-02-27 18:33:09 +00:00
|
|
|
int rval;
|
|
|
|
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
td = curthread;
|
|
|
|
MPASS(!(td->td_flags & TDF_SINTR));
|
|
|
|
thread_lock(td);
|
2008-03-12 06:31:06 +00:00
|
|
|
sleepq_switch(wchan, pri);
|
2015-01-22 11:12:42 +00:00
|
|
|
rval = sleepq_check_timeout();
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
thread_unlock(td);
|
|
|
|
|
2004-06-28 18:57:06 +00:00
|
|
|
return (rval);
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Block the current thread until it is awakened from its sleep queue,
|
|
|
|
* it is interrupted by a signal, or it times out waiting to be awakened.
|
|
|
|
*/
|
|
|
|
int
|
2008-03-12 06:31:06 +00:00
|
|
|
sleepq_timedwait_sig(void *wchan, int pri)
|
2004-02-27 18:33:09 +00:00
|
|
|
{
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
int rcatch, rvalt, rvals;
|
2004-02-27 18:33:09 +00:00
|
|
|
|
2008-03-12 06:31:06 +00:00
|
|
|
rcatch = sleepq_catch_signals(wchan, pri);
|
2015-01-22 11:12:42 +00:00
|
|
|
rvalt = sleepq_check_timeout();
|
2004-02-27 18:33:09 +00:00
|
|
|
rvals = sleepq_check_signals();
|
2015-01-22 11:12:42 +00:00
|
|
|
thread_unlock(curthread);
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
if (rcatch)
|
|
|
|
return (rcatch);
|
|
|
|
if (rvals)
|
2004-02-27 18:33:09 +00:00
|
|
|
return (rvals);
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
return (rvalt);
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
2010-01-09 01:46:38 +00:00
|
|
|
/*
|
|
|
|
* Returns the type of sleepqueue given a waitchannel.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sleepq_type(void *wchan)
|
|
|
|
{
|
|
|
|
struct sleepqueue *sq;
|
|
|
|
int type;
|
|
|
|
|
|
|
|
MPASS(wchan != NULL);
|
|
|
|
|
|
|
|
sleepq_lock(wchan);
|
|
|
|
sq = sleepq_lookup(wchan);
|
|
|
|
if (sq == NULL) {
|
|
|
|
sleepq_release(wchan);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
type = sq->sq_type;
|
|
|
|
sleepq_release(wchan);
|
|
|
|
return (type);
|
|
|
|
}
|
|
|
|
|
2004-02-27 18:33:09 +00:00
|
|
|
/*
|
2005-04-14 06:30:32 +00:00
|
|
|
* Removes a thread from a sleep queue and makes it
|
|
|
|
* runnable.
|
2004-02-27 18:33:09 +00:00
|
|
|
*/
|
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
2008-08-05 20:02:31 +00:00
|
|
|
static int
|
2005-04-14 06:30:32 +00:00
|
|
|
sleepq_resume_thread(struct sleepqueue *sq, struct thread *td, int pri)
|
2004-02-27 18:33:09 +00:00
|
|
|
{
|
2018-05-19 05:00:16 +00:00
|
|
|
struct sleepqueue_chain *sc __unused;
|
2004-02-27 18:33:09 +00:00
|
|
|
|
|
|
|
MPASS(td != NULL);
|
|
|
|
MPASS(sq->sq_wchan != NULL);
|
|
|
|
MPASS(td->td_wchan == sq->sq_wchan);
|
2006-12-16 06:54:09 +00:00
|
|
|
MPASS(td->td_sqqueue < NR_SLEEPQS && td->td_sqqueue >= 0);
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
THREAD_LOCK_ASSERT(td, MA_OWNED);
|
2004-02-27 18:33:09 +00:00
|
|
|
sc = SC_LOOKUP(sq->sq_wchan);
|
|
|
|
mtx_assert(&sc->sc_lock, MA_OWNED);
|
|
|
|
|
2012-05-15 01:30:25 +00:00
|
|
|
SDT_PROBE2(sched, , , wakeup, td, td->td_proc);
|
|
|
|
|
2004-02-27 18:33:09 +00:00
|
|
|
/* Remove the thread from the queue. */
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
sq->sq_blockedcnt[td->td_sqqueue]--;
|
2006-12-16 06:54:09 +00:00
|
|
|
TAILQ_REMOVE(&sq->sq_blocked[td->td_sqqueue], td, td_slpq);
|
2004-02-27 18:33:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a sleep queue for this thread. If this is the last waiter,
|
|
|
|
* use the queue itself and take it out of the chain, otherwise,
|
|
|
|
* remove a queue from the free list.
|
|
|
|
*/
|
|
|
|
if (LIST_EMPTY(&sq->sq_free)) {
|
|
|
|
td->td_sleepqueue = sq;
|
|
|
|
#ifdef INVARIANTS
|
|
|
|
sq->sq_wchan = NULL;
|
2004-06-29 02:30:12 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SLEEPQUEUE_PROFILING
|
|
|
|
sc->sc_depth--;
|
2004-02-27 18:33:09 +00:00
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
td->td_sleepqueue = LIST_FIRST(&sq->sq_free);
|
|
|
|
LIST_REMOVE(td->td_sleepqueue, sq_hash);
|
|
|
|
|
2004-05-13 20:00:43 +00:00
|
|
|
td->td_wmesg = NULL;
|
|
|
|
td->td_wchan = NULL;
|
2013-02-06 17:06:51 +00:00
|
|
|
td->td_flags &= ~TDF_SINTR;
|
2004-05-13 20:00:43 +00:00
|
|
|
|
2004-05-14 20:51:42 +00:00
|
|
|
CTR3(KTR_PROC, "sleepq_wakeup: thread %p (pid %ld, %s)",
|
2007-11-14 06:21:24 +00:00
|
|
|
(void *)td, (long)td->td_proc->p_pid, td->td_name);
|
2004-02-27 18:33:09 +00:00
|
|
|
|
|
|
|
/* Adjust priority if requested. */
|
2008-03-12 06:31:06 +00:00
|
|
|
MPASS(pri == 0 || (pri >= PRI_MIN && pri <= PRI_MAX));
|
2011-01-14 17:06:54 +00:00
|
|
|
if (pri != 0 && td->td_priority > pri &&
|
|
|
|
PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
|
2004-10-12 16:31:23 +00:00
|
|
|
sched_prio(td, pri);
|
2008-11-04 19:13:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that thread td might not be sleeping if it is running
|
|
|
|
* sleepq_catch_signals() on another CPU or is blocked on its
|
|
|
|
* proc lock to check signals. There's no need to mark the
|
|
|
|
* thread runnable in that case.
|
|
|
|
*/
|
|
|
|
if (TD_IS_SLEEPING(td)) {
|
|
|
|
TD_CLR_SLEEPING(td);
|
|
|
|
return (setrunnable(td));
|
|
|
|
}
|
|
|
|
return (0);
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
2007-05-18 06:32:24 +00:00
|
|
|
#ifdef INVARIANTS
|
|
|
|
/*
|
|
|
|
* UMA zone item deallocator.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
sleepq_dtor(void *mem, int size, void *arg)
|
|
|
|
{
|
|
|
|
struct sleepqueue *sq;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
sq = mem;
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
for (i = 0; i < NR_SLEEPQS; i++) {
|
2007-05-18 06:32:24 +00:00
|
|
|
MPASS(TAILQ_EMPTY(&sq->sq_blocked[i]));
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
MPASS(sq->sq_blockedcnt[i] == 0);
|
|
|
|
}
|
2007-05-18 06:32:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* UMA zone item initializer.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sleepq_init(void *mem, int size, int flags)
|
|
|
|
{
|
|
|
|
struct sleepqueue *sq;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
bzero(mem, size);
|
|
|
|
sq = mem;
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
for (i = 0; i < NR_SLEEPQS; i++) {
|
2007-05-18 06:32:24 +00:00
|
|
|
TAILQ_INIT(&sq->sq_blocked[i]);
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
sq->sq_blockedcnt[i] = 0;
|
|
|
|
}
|
2007-05-18 06:32:24 +00:00
|
|
|
LIST_INIT(&sq->sq_free);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-02-27 18:33:09 +00:00
|
|
|
/*
|
|
|
|
* Find the highest priority thread sleeping on a wait channel and resume it.
|
|
|
|
*/
|
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
2008-08-05 20:02:31 +00:00
|
|
|
int
|
2006-12-16 06:54:09 +00:00
|
|
|
sleepq_signal(void *wchan, int flags, int pri, int queue)
|
2004-02-27 18:33:09 +00:00
|
|
|
{
|
|
|
|
struct sleepqueue *sq;
|
2004-11-05 20:19:58 +00:00
|
|
|
struct thread *td, *besttd;
|
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
2008-08-05 20:02:31 +00:00
|
|
|
int wakeup_swapper;
|
2004-02-27 18:33:09 +00:00
|
|
|
|
|
|
|
CTR2(KTR_PROC, "sleepq_signal(%p, %d)", wchan, flags);
|
|
|
|
KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
|
2006-12-16 06:54:09 +00:00
|
|
|
MPASS((queue >= 0) && (queue < NR_SLEEPQS));
|
2004-02-27 18:33:09 +00:00
|
|
|
sq = sleepq_lookup(wchan);
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
if (sq == NULL)
|
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
2008-08-05 20:02:31 +00:00
|
|
|
return (0);
|
2004-08-19 11:31:42 +00:00
|
|
|
KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE),
|
2004-02-27 18:33:09 +00:00
|
|
|
("%s: mismatch between sleep/wakeup and cv_*", __func__));
|
2004-05-13 20:00:43 +00:00
|
|
|
|
2004-11-05 20:19:58 +00:00
|
|
|
/*
|
|
|
|
* Find the highest priority thread on the queue. If there is a
|
|
|
|
* tie, use the thread that first appears in the queue as it has
|
|
|
|
* been sleeping the longest since threads are always added to
|
|
|
|
* the tail of sleep queues.
|
|
|
|
*/
|
2016-09-04 00:29:48 +00:00
|
|
|
besttd = TAILQ_FIRST(&sq->sq_blocked[queue]);
|
2006-12-16 06:54:09 +00:00
|
|
|
TAILQ_FOREACH(td, &sq->sq_blocked[queue], td_slpq) {
|
2016-09-04 00:29:48 +00:00
|
|
|
if (td->td_priority < besttd->td_priority)
|
2004-11-05 20:19:58 +00:00
|
|
|
besttd = td;
|
|
|
|
}
|
|
|
|
MPASS(besttd != NULL);
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
thread_lock(besttd);
|
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
2008-08-05 20:02:31 +00:00
|
|
|
wakeup_swapper = sleepq_resume_thread(sq, besttd, pri);
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
thread_unlock(besttd);
|
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
2008-08-05 20:02:31 +00:00
|
|
|
return (wakeup_swapper);
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
When the RTC is adjusted, reevaluate absolute sleep times based on the RTC
POSIX 2008 says this about clock_settime(2):
If the value of the CLOCK_REALTIME clock is set via clock_settime(),
the new value of the clock shall be used to determine the time
of expiration for absolute time services based upon the
CLOCK_REALTIME clock. This applies to the time at which armed
absolute timers expire. If the absolute time requested at the
invocation of such a time service is before the new value of
the clock, the time service shall expire immediately as if the
clock had reached the requested time normally.
Setting the value of the CLOCK_REALTIME clock via clock_settime()
shall have no effect on threads that are blocked waiting for
a relative time service based upon this clock, including the
nanosleep() function; nor on the expiration of relative timers
based upon this clock. Consequently, these time services shall
expire when the requested relative interval elapses, independently
of the new or old value of the clock.
When the real-time clock is adjusted, such as by clock_settime(3),
wake any threads sleeping until an absolute real-clock time.
Such a sleep is indicated by a non-zero td_rtcgen. The sleep functions
will set that field to zero and return zero to tell the caller
to reevaluate its sleep duration based on the new value of the clock.
At present, this affects the following functions:
pthread_cond_timedwait(3)
pthread_mutex_timedlock(3)
pthread_rwlock_timedrdlock(3)
pthread_rwlock_timedwrlock(3)
sem_timedwait(3)
sem_clockwait_np(3)
I'm working on adding clock_nanosleep(2), which will also be affected.
Reported by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Reviewed by: jhb, kib
MFC after: 2 weeks
Relnotes: yes
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D9791
2017-03-14 19:06:44 +00:00
|
|
|
static bool
|
|
|
|
match_any(struct thread *td __unused)
|
|
|
|
{
|
2017-03-14 22:02:02 +00:00
|
|
|
|
When the RTC is adjusted, reevaluate absolute sleep times based on the RTC
POSIX 2008 says this about clock_settime(2):
If the value of the CLOCK_REALTIME clock is set via clock_settime(),
the new value of the clock shall be used to determine the time
of expiration for absolute time services based upon the
CLOCK_REALTIME clock. This applies to the time at which armed
absolute timers expire. If the absolute time requested at the
invocation of such a time service is before the new value of
the clock, the time service shall expire immediately as if the
clock had reached the requested time normally.
Setting the value of the CLOCK_REALTIME clock via clock_settime()
shall have no effect on threads that are blocked waiting for
a relative time service based upon this clock, including the
nanosleep() function; nor on the expiration of relative timers
based upon this clock. Consequently, these time services shall
expire when the requested relative interval elapses, independently
of the new or old value of the clock.
When the real-time clock is adjusted, such as by clock_settime(3),
wake any threads sleeping until an absolute real-clock time.
Such a sleep is indicated by a non-zero td_rtcgen. The sleep functions
will set that field to zero and return zero to tell the caller
to reevaluate its sleep duration based on the new value of the clock.
At present, this affects the following functions:
pthread_cond_timedwait(3)
pthread_mutex_timedlock(3)
pthread_rwlock_timedrdlock(3)
pthread_rwlock_timedwrlock(3)
sem_timedwait(3)
sem_clockwait_np(3)
I'm working on adding clock_nanosleep(2), which will also be affected.
Reported by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Reviewed by: jhb, kib
MFC after: 2 weeks
Relnotes: yes
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D9791
2017-03-14 19:06:44 +00:00
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
2004-02-27 18:33:09 +00:00
|
|
|
/*
|
|
|
|
* Resume all threads sleeping on a specified wait channel.
|
|
|
|
*/
|
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
2008-08-05 20:02:31 +00:00
|
|
|
int
|
2006-12-16 06:54:09 +00:00
|
|
|
sleepq_broadcast(void *wchan, int flags, int pri, int queue)
|
2004-02-27 18:33:09 +00:00
|
|
|
{
|
|
|
|
struct sleepqueue *sq;
|
|
|
|
|
|
|
|
CTR2(KTR_PROC, "sleepq_broadcast(%p, %d)", wchan, flags);
|
|
|
|
KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
|
2006-12-16 06:54:09 +00:00
|
|
|
MPASS((queue >= 0) && (queue < NR_SLEEPQS));
|
2004-02-27 18:33:09 +00:00
|
|
|
sq = sleepq_lookup(wchan);
|
2008-03-12 06:31:06 +00:00
|
|
|
if (sq == NULL)
|
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
2008-08-05 20:02:31 +00:00
|
|
|
return (0);
|
2004-08-19 11:31:42 +00:00
|
|
|
KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE),
|
2004-02-27 18:33:09 +00:00
|
|
|
("%s: mismatch between sleep/wakeup and cv_*", __func__));
|
2004-05-13 20:00:43 +00:00
|
|
|
|
When the RTC is adjusted, reevaluate absolute sleep times based on the RTC
POSIX 2008 says this about clock_settime(2):
If the value of the CLOCK_REALTIME clock is set via clock_settime(),
the new value of the clock shall be used to determine the time
of expiration for absolute time services based upon the
CLOCK_REALTIME clock. This applies to the time at which armed
absolute timers expire. If the absolute time requested at the
invocation of such a time service is before the new value of
the clock, the time service shall expire immediately as if the
clock had reached the requested time normally.
Setting the value of the CLOCK_REALTIME clock via clock_settime()
shall have no effect on threads that are blocked waiting for
a relative time service based upon this clock, including the
nanosleep() function; nor on the expiration of relative timers
based upon this clock. Consequently, these time services shall
expire when the requested relative interval elapses, independently
of the new or old value of the clock.
When the real-time clock is adjusted, such as by clock_settime(3),
wake any threads sleeping until an absolute real-clock time.
Such a sleep is indicated by a non-zero td_rtcgen. The sleep functions
will set that field to zero and return zero to tell the caller
to reevaluate its sleep duration based on the new value of the clock.
At present, this affects the following functions:
pthread_cond_timedwait(3)
pthread_mutex_timedlock(3)
pthread_rwlock_timedrdlock(3)
pthread_rwlock_timedwrlock(3)
sem_timedwait(3)
sem_clockwait_np(3)
I'm working on adding clock_nanosleep(2), which will also be affected.
Reported by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Reviewed by: jhb, kib
MFC after: 2 weeks
Relnotes: yes
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D9791
2017-03-14 19:06:44 +00:00
|
|
|
return (sleepq_remove_matching(sq, queue, match_any, pri));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Resume threads on the sleep queue that match the given predicate.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sleepq_remove_matching(struct sleepqueue *sq, int queue,
|
|
|
|
bool (*matches)(struct thread *), int pri)
|
|
|
|
{
|
|
|
|
struct thread *td, *tdn;
|
|
|
|
int wakeup_swapper;
|
|
|
|
|
2016-12-23 05:02:17 +00:00
|
|
|
/*
|
When the RTC is adjusted, reevaluate absolute sleep times based on the RTC
POSIX 2008 says this about clock_settime(2):
If the value of the CLOCK_REALTIME clock is set via clock_settime(),
the new value of the clock shall be used to determine the time
of expiration for absolute time services based upon the
CLOCK_REALTIME clock. This applies to the time at which armed
absolute timers expire. If the absolute time requested at the
invocation of such a time service is before the new value of
the clock, the time service shall expire immediately as if the
clock had reached the requested time normally.
Setting the value of the CLOCK_REALTIME clock via clock_settime()
shall have no effect on threads that are blocked waiting for
a relative time service based upon this clock, including the
nanosleep() function; nor on the expiration of relative timers
based upon this clock. Consequently, these time services shall
expire when the requested relative interval elapses, independently
of the new or old value of the clock.
When the real-time clock is adjusted, such as by clock_settime(3),
wake any threads sleeping until an absolute real-clock time.
Such a sleep is indicated by a non-zero td_rtcgen. The sleep functions
will set that field to zero and return zero to tell the caller
to reevaluate its sleep duration based on the new value of the clock.
At present, this affects the following functions:
pthread_cond_timedwait(3)
pthread_mutex_timedlock(3)
pthread_rwlock_timedrdlock(3)
pthread_rwlock_timedwrlock(3)
sem_timedwait(3)
sem_clockwait_np(3)
I'm working on adding clock_nanosleep(2), which will also be affected.
Reported by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Reviewed by: jhb, kib
MFC after: 2 weeks
Relnotes: yes
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D9791
2017-03-14 19:06:44 +00:00
|
|
|
* The last thread will be given ownership of sq and may
|
|
|
|
* re-enqueue itself before sleepq_resume_thread() returns,
|
|
|
|
* so we must cache the "next" queue item at the beginning
|
|
|
|
* of the final iteration.
|
2016-12-23 05:02:17 +00:00
|
|
|
*/
|
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
2008-08-05 20:02:31 +00:00
|
|
|
wakeup_swapper = 0;
|
2016-12-22 17:51:44 +00:00
|
|
|
TAILQ_FOREACH_SAFE(td, &sq->sq_blocked[queue], td_slpq, tdn) {
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
thread_lock(td);
|
When the RTC is adjusted, reevaluate absolute sleep times based on the RTC
POSIX 2008 says this about clock_settime(2):
If the value of the CLOCK_REALTIME clock is set via clock_settime(),
the new value of the clock shall be used to determine the time
of expiration for absolute time services based upon the
CLOCK_REALTIME clock. This applies to the time at which armed
absolute timers expire. If the absolute time requested at the
invocation of such a time service is before the new value of
the clock, the time service shall expire immediately as if the
clock had reached the requested time normally.
Setting the value of the CLOCK_REALTIME clock via clock_settime()
shall have no effect on threads that are blocked waiting for
a relative time service based upon this clock, including the
nanosleep() function; nor on the expiration of relative timers
based upon this clock. Consequently, these time services shall
expire when the requested relative interval elapses, independently
of the new or old value of the clock.
When the real-time clock is adjusted, such as by clock_settime(3),
wake any threads sleeping until an absolute real-clock time.
Such a sleep is indicated by a non-zero td_rtcgen. The sleep functions
will set that field to zero and return zero to tell the caller
to reevaluate its sleep duration based on the new value of the clock.
At present, this affects the following functions:
pthread_cond_timedwait(3)
pthread_mutex_timedlock(3)
pthread_rwlock_timedrdlock(3)
pthread_rwlock_timedwrlock(3)
sem_timedwait(3)
sem_clockwait_np(3)
I'm working on adding clock_nanosleep(2), which will also be affected.
Reported by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Reviewed by: jhb, kib
MFC after: 2 weeks
Relnotes: yes
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D9791
2017-03-14 19:06:44 +00:00
|
|
|
if (matches(td))
|
|
|
|
wakeup_swapper |= sleepq_resume_thread(sq, td, pri);
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
thread_unlock(td);
|
|
|
|
}
|
When the RTC is adjusted, reevaluate absolute sleep times based on the RTC
POSIX 2008 says this about clock_settime(2):
If the value of the CLOCK_REALTIME clock is set via clock_settime(),
the new value of the clock shall be used to determine the time
of expiration for absolute time services based upon the
CLOCK_REALTIME clock. This applies to the time at which armed
absolute timers expire. If the absolute time requested at the
invocation of such a time service is before the new value of
the clock, the time service shall expire immediately as if the
clock had reached the requested time normally.
Setting the value of the CLOCK_REALTIME clock via clock_settime()
shall have no effect on threads that are blocked waiting for
a relative time service based upon this clock, including the
nanosleep() function; nor on the expiration of relative timers
based upon this clock. Consequently, these time services shall
expire when the requested relative interval elapses, independently
of the new or old value of the clock.
When the real-time clock is adjusted, such as by clock_settime(3),
wake any threads sleeping until an absolute real-clock time.
Such a sleep is indicated by a non-zero td_rtcgen. The sleep functions
will set that field to zero and return zero to tell the caller
to reevaluate its sleep duration based on the new value of the clock.
At present, this affects the following functions:
pthread_cond_timedwait(3)
pthread_mutex_timedlock(3)
pthread_rwlock_timedrdlock(3)
pthread_rwlock_timedwrlock(3)
sem_timedwait(3)
sem_clockwait_np(3)
I'm working on adding clock_nanosleep(2), which will also be affected.
Reported by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Reviewed by: jhb, kib
MFC after: 2 weeks
Relnotes: yes
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D9791
2017-03-14 19:06:44 +00:00
|
|
|
|
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
2008-08-05 20:02:31 +00:00
|
|
|
return (wakeup_swapper);
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Time sleeping threads out. When the timeout expires, the thread is
|
|
|
|
* removed from the sleep queue and made runnable if it is still asleep.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
sleepq_timeout(void *arg)
|
|
|
|
{
|
2018-05-19 05:00:16 +00:00
|
|
|
struct sleepqueue_chain *sc __unused;
|
2015-01-22 11:12:42 +00:00
|
|
|
struct sleepqueue *sq;
|
|
|
|
struct thread *td;
|
|
|
|
void *wchan;
|
|
|
|
int wakeup_swapper;
|
2004-02-27 18:33:09 +00:00
|
|
|
|
2015-01-22 11:12:42 +00:00
|
|
|
td = arg;
|
|
|
|
wakeup_swapper = 0;
|
2004-05-14 20:51:42 +00:00
|
|
|
CTR3(KTR_PROC, "sleepq_timeout: thread %p (pid %ld, %s)",
|
2007-11-14 06:21:24 +00:00
|
|
|
(void *)td, (long)td->td_proc->p_pid, (void *)td->td_name);
|
2004-02-27 18:33:09 +00:00
|
|
|
|
2015-01-15 15:32:30 +00:00
|
|
|
thread_lock(td);
|
2016-07-28 09:09:55 +00:00
|
|
|
|
|
|
|
if (td->td_sleeptimo > sbinuptime() || td->td_sleeptimo == 0) {
|
|
|
|
/*
|
|
|
|
* The thread does not want a timeout (yet).
|
|
|
|
*/
|
|
|
|
} else if (TD_IS_SLEEPING(td) && TD_ON_SLEEPQ(td)) {
|
|
|
|
/*
|
|
|
|
* See if the thread is asleep and get the wait
|
|
|
|
* channel if it is.
|
|
|
|
*/
|
2015-01-22 11:12:42 +00:00
|
|
|
wchan = td->td_wchan;
|
|
|
|
sc = SC_LOOKUP(wchan);
|
|
|
|
THREAD_LOCKPTR_ASSERT(td, &sc->sc_lock);
|
|
|
|
sq = sleepq_lookup(wchan);
|
|
|
|
MPASS(sq != NULL);
|
|
|
|
td->td_flags |= TDF_TIMEOUT;
|
|
|
|
wakeup_swapper = sleepq_resume_thread(sq, td, 0);
|
2016-07-28 09:09:55 +00:00
|
|
|
} else if (TD_ON_SLEEPQ(td)) {
|
|
|
|
/*
|
|
|
|
* If the thread is on the SLEEPQ but isn't sleeping
|
|
|
|
* yet, it can either be on another CPU in between
|
|
|
|
* sleepq_add() and one of the sleepq_*wait*()
|
|
|
|
* routines or it can be in sleepq_catch_signals().
|
|
|
|
*/
|
2015-01-22 11:12:42 +00:00
|
|
|
td->td_flags |= TDF_TIMEOUT;
|
2015-01-15 15:32:30 +00:00
|
|
|
}
|
2015-01-22 11:12:42 +00:00
|
|
|
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
thread_unlock(td);
|
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
2008-08-05 20:02:31 +00:00
|
|
|
if (wakeup_swapper)
|
|
|
|
kick_proc0();
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Resumes a specific thread from the sleep queue associated with a specific
|
|
|
|
* wait channel if it is on that queue.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sleepq_remove(struct thread *td, void *wchan)
|
|
|
|
{
|
|
|
|
struct sleepqueue *sq;
|
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
2008-08-05 20:02:31 +00:00
|
|
|
int wakeup_swapper;
|
2004-02-27 18:33:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Look up the sleep queue for this wait channel, then re-check
|
|
|
|
* that the thread is asleep on that channel, if it is not, then
|
|
|
|
* bail.
|
|
|
|
*/
|
|
|
|
MPASS(wchan != NULL);
|
2004-10-12 18:36:20 +00:00
|
|
|
sleepq_lock(wchan);
|
2004-02-27 18:33:09 +00:00
|
|
|
sq = sleepq_lookup(wchan);
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
/*
|
|
|
|
* We can not lock the thread here as it may be sleeping on a
|
|
|
|
* different sleepq. However, holding the sleepq lock for this
|
|
|
|
* wchan can guarantee that we do not miss a wakeup for this
|
|
|
|
* channel. The asserts below will catch any false positives.
|
|
|
|
*/
|
2004-02-27 18:33:09 +00:00
|
|
|
if (!TD_ON_SLEEPQ(td) || td->td_wchan != wchan) {
|
|
|
|
sleepq_release(wchan);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Thread is asleep on sleep queue sq, so wake it up. */
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
thread_lock(td);
|
|
|
|
MPASS(sq != NULL);
|
|
|
|
MPASS(td->td_wchan == wchan);
|
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
2008-08-05 20:02:31 +00:00
|
|
|
wakeup_swapper = sleepq_resume_thread(sq, td, 0);
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
thread_unlock(td);
|
2004-02-27 18:33:09 +00:00
|
|
|
sleepq_release(wchan);
|
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
2008-08-05 20:02:31 +00:00
|
|
|
if (wakeup_swapper)
|
|
|
|
kick_proc0();
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-05-14 20:51:42 +00:00
|
|
|
* Abort a thread as if an interrupt had occurred. Only abort
|
|
|
|
* interruptible waits (unfortunately it isn't safe to abort others).
|
2004-02-27 18:33:09 +00:00
|
|
|
*/
|
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
2008-08-05 20:02:31 +00:00
|
|
|
int
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
sleepq_abort(struct thread *td, int intrval)
|
2004-02-27 18:33:09 +00:00
|
|
|
{
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
struct sleepqueue *sq;
|
2004-02-27 18:33:09 +00:00
|
|
|
void *wchan;
|
|
|
|
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
THREAD_LOCK_ASSERT(td, MA_OWNED);
|
2004-02-27 18:33:09 +00:00
|
|
|
MPASS(TD_ON_SLEEPQ(td));
|
|
|
|
MPASS(td->td_flags & TDF_SINTR);
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
MPASS(intrval == EINTR || intrval == ERESTART);
|
2004-02-27 18:33:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the TDF_TIMEOUT flag is set, just leave. A
|
|
|
|
* timeout is scheduled anyhow.
|
|
|
|
*/
|
|
|
|
if (td->td_flags & TDF_TIMEOUT)
|
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
2008-08-05 20:02:31 +00:00
|
|
|
return (0);
|
2004-02-27 18:33:09 +00:00
|
|
|
|
2004-05-14 20:51:42 +00:00
|
|
|
CTR3(KTR_PROC, "sleepq_abort: thread %p (pid %ld, %s)",
|
2007-11-14 06:21:24 +00:00
|
|
|
(void *)td, (long)td->td_proc->p_pid, (void *)td->td_name);
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
td->td_intrval = intrval;
|
|
|
|
td->td_flags |= TDF_SLEEPABORT;
|
|
|
|
/*
|
|
|
|
* If the thread has not slept yet it will find the signal in
|
|
|
|
* sleepq_catch_signals() and call sleepq_resume_thread. Otherwise
|
|
|
|
* we have to do it here.
|
|
|
|
*/
|
|
|
|
if (!TD_IS_SLEEPING(td))
|
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
2008-08-05 20:02:31 +00:00
|
|
|
return (0);
|
2004-02-27 18:33:09 +00:00
|
|
|
wchan = td->td_wchan;
|
Commit 2/14 of sched_lock decomposition.
- Adapt sleepqueues to the new thread_lock() mechanism.
- Delay assigning the sleep queue spinlock as the thread lock until after
we've checked for signals. It is illegal for a thread to return in
mi_switch() with any lock assigned to td_lock other than the scheduler
locks.
- Change sleepq_catch_signals() to do the switch if necessary to simplify
the callers.
- Simplify timeout handling now that locking a sleeping thread has the
side-effect of locking the sleepqueue. Some previous races are no
longer possible.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-04 23:50:56 +00:00
|
|
|
MPASS(wchan != NULL);
|
|
|
|
sq = sleepq_lookup(wchan);
|
|
|
|
MPASS(sq != NULL);
|
|
|
|
|
|
|
|
/* Thread is asleep on sleep queue sq, so wake it up. */
|
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
2008-08-05 20:02:31 +00:00
|
|
|
return (sleepq_resume_thread(sq, td, 0));
|
2004-02-27 18:33:09 +00:00
|
|
|
}
|
2006-01-27 22:24:07 +00:00
|
|
|
|
When the RTC is adjusted, reevaluate absolute sleep times based on the RTC
POSIX 2008 says this about clock_settime(2):
If the value of the CLOCK_REALTIME clock is set via clock_settime(),
the new value of the clock shall be used to determine the time
of expiration for absolute time services based upon the
CLOCK_REALTIME clock. This applies to the time at which armed
absolute timers expire. If the absolute time requested at the
invocation of such a time service is before the new value of
the clock, the time service shall expire immediately as if the
clock had reached the requested time normally.
Setting the value of the CLOCK_REALTIME clock via clock_settime()
shall have no effect on threads that are blocked waiting for
a relative time service based upon this clock, including the
nanosleep() function; nor on the expiration of relative timers
based upon this clock. Consequently, these time services shall
expire when the requested relative interval elapses, independently
of the new or old value of the clock.
When the real-time clock is adjusted, such as by clock_settime(3),
wake any threads sleeping until an absolute real-clock time.
Such a sleep is indicated by a non-zero td_rtcgen. The sleep functions
will set that field to zero and return zero to tell the caller
to reevaluate its sleep duration based on the new value of the clock.
At present, this affects the following functions:
pthread_cond_timedwait(3)
pthread_mutex_timedlock(3)
pthread_rwlock_timedrdlock(3)
pthread_rwlock_timedwrlock(3)
sem_timedwait(3)
sem_clockwait_np(3)
I'm working on adding clock_nanosleep(2), which will also be affected.
Reported by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Reviewed by: jhb, kib
MFC after: 2 weeks
Relnotes: yes
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D9791
2017-03-14 19:06:44 +00:00
|
|
|
void
|
|
|
|
sleepq_chains_remove_matching(bool (*matches)(struct thread *))
|
|
|
|
{
|
|
|
|
struct sleepqueue_chain *sc;
|
2018-03-25 20:12:14 +00:00
|
|
|
struct sleepqueue *sq, *sq1;
|
When the RTC is adjusted, reevaluate absolute sleep times based on the RTC
POSIX 2008 says this about clock_settime(2):
If the value of the CLOCK_REALTIME clock is set via clock_settime(),
the new value of the clock shall be used to determine the time
of expiration for absolute time services based upon the
CLOCK_REALTIME clock. This applies to the time at which armed
absolute timers expire. If the absolute time requested at the
invocation of such a time service is before the new value of
the clock, the time service shall expire immediately as if the
clock had reached the requested time normally.
Setting the value of the CLOCK_REALTIME clock via clock_settime()
shall have no effect on threads that are blocked waiting for
a relative time service based upon this clock, including the
nanosleep() function; nor on the expiration of relative timers
based upon this clock. Consequently, these time services shall
expire when the requested relative interval elapses, independently
of the new or old value of the clock.
When the real-time clock is adjusted, such as by clock_settime(3),
wake any threads sleeping until an absolute real-clock time.
Such a sleep is indicated by a non-zero td_rtcgen. The sleep functions
will set that field to zero and return zero to tell the caller
to reevaluate its sleep duration based on the new value of the clock.
At present, this affects the following functions:
pthread_cond_timedwait(3)
pthread_mutex_timedlock(3)
pthread_rwlock_timedrdlock(3)
pthread_rwlock_timedwrlock(3)
sem_timedwait(3)
sem_clockwait_np(3)
I'm working on adding clock_nanosleep(2), which will also be affected.
Reported by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Reviewed by: jhb, kib
MFC after: 2 weeks
Relnotes: yes
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D9791
2017-03-14 19:06:44 +00:00
|
|
|
int i, wakeup_swapper;
|
|
|
|
|
|
|
|
wakeup_swapper = 0;
|
|
|
|
for (sc = &sleepq_chains[0]; sc < sleepq_chains + SC_TABLESIZE; ++sc) {
|
|
|
|
if (LIST_EMPTY(&sc->sc_queues)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mtx_lock_spin(&sc->sc_lock);
|
2018-03-25 20:12:14 +00:00
|
|
|
LIST_FOREACH_SAFE(sq, &sc->sc_queues, sq_hash, sq1) {
|
When the RTC is adjusted, reevaluate absolute sleep times based on the RTC
POSIX 2008 says this about clock_settime(2):
If the value of the CLOCK_REALTIME clock is set via clock_settime(),
the new value of the clock shall be used to determine the time
of expiration for absolute time services based upon the
CLOCK_REALTIME clock. This applies to the time at which armed
absolute timers expire. If the absolute time requested at the
invocation of such a time service is before the new value of
the clock, the time service shall expire immediately as if the
clock had reached the requested time normally.
Setting the value of the CLOCK_REALTIME clock via clock_settime()
shall have no effect on threads that are blocked waiting for
a relative time service based upon this clock, including the
nanosleep() function; nor on the expiration of relative timers
based upon this clock. Consequently, these time services shall
expire when the requested relative interval elapses, independently
of the new or old value of the clock.
When the real-time clock is adjusted, such as by clock_settime(3),
wake any threads sleeping until an absolute real-clock time.
Such a sleep is indicated by a non-zero td_rtcgen. The sleep functions
will set that field to zero and return zero to tell the caller
to reevaluate its sleep duration based on the new value of the clock.
At present, this affects the following functions:
pthread_cond_timedwait(3)
pthread_mutex_timedlock(3)
pthread_rwlock_timedrdlock(3)
pthread_rwlock_timedwrlock(3)
sem_timedwait(3)
sem_clockwait_np(3)
I'm working on adding clock_nanosleep(2), which will also be affected.
Reported by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Reviewed by: jhb, kib
MFC after: 2 weeks
Relnotes: yes
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D9791
2017-03-14 19:06:44 +00:00
|
|
|
for (i = 0; i < NR_SLEEPQS; ++i) {
|
|
|
|
wakeup_swapper |= sleepq_remove_matching(sq, i,
|
|
|
|
matches, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mtx_unlock_spin(&sc->sc_lock);
|
|
|
|
}
|
|
|
|
if (wakeup_swapper) {
|
|
|
|
kick_proc0();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 04:22:32 +00:00
|
|
|
/*
|
|
|
|
* Prints the stacks of all threads presently sleeping on wchan/queue to
|
|
|
|
* the sbuf sb. Sets count_stacks_printed to the number of stacks actually
|
|
|
|
* printed. Typically, this will equal the number of threads sleeping on the
|
|
|
|
* queue, but may be less if sb overflowed before all stacks were printed.
|
|
|
|
*/
|
2016-03-17 01:05:53 +00:00
|
|
|
#ifdef STACK
|
2016-03-16 04:22:32 +00:00
|
|
|
int
|
|
|
|
sleepq_sbuf_print_stacks(struct sbuf *sb, void *wchan, int queue,
|
|
|
|
int *count_stacks_printed)
|
|
|
|
{
|
|
|
|
struct thread *td, *td_next;
|
|
|
|
struct sleepqueue *sq;
|
|
|
|
struct stack **st;
|
|
|
|
struct sbuf **td_infos;
|
|
|
|
int i, stack_idx, error, stacks_to_allocate;
|
2017-12-25 04:48:39 +00:00
|
|
|
bool finished;
|
2016-03-16 04:22:32 +00:00
|
|
|
|
|
|
|
error = 0;
|
|
|
|
finished = false;
|
|
|
|
|
|
|
|
KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
|
|
|
|
MPASS((queue >= 0) && (queue < NR_SLEEPQS));
|
|
|
|
|
|
|
|
stacks_to_allocate = 10;
|
|
|
|
for (i = 0; i < 3 && !finished ; i++) {
|
|
|
|
/* We cannot malloc while holding the queue's spinlock, so
|
|
|
|
* we do our mallocs now, and hope it is enough. If it
|
|
|
|
* isn't, we will free these, drop the lock, malloc more,
|
|
|
|
* and try again, up to a point. After that point we will
|
|
|
|
* give up and report ENOMEM. We also cannot write to sb
|
|
|
|
* during this time since the client may have set the
|
|
|
|
* SBUF_AUTOEXTEND flag on their sbuf, which could cause a
|
|
|
|
* malloc as we print to it. So we defer actually printing
|
|
|
|
* to sb until after we drop the spinlock.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Where we will store the stacks. */
|
|
|
|
st = malloc(sizeof(struct stack *) * stacks_to_allocate,
|
|
|
|
M_TEMP, M_WAITOK);
|
|
|
|
for (stack_idx = 0; stack_idx < stacks_to_allocate;
|
|
|
|
stack_idx++)
|
2017-10-06 21:52:28 +00:00
|
|
|
st[stack_idx] = stack_create(M_WAITOK);
|
2016-03-16 04:22:32 +00:00
|
|
|
|
|
|
|
/* Where we will store the td name, tid, etc. */
|
|
|
|
td_infos = malloc(sizeof(struct sbuf *) * stacks_to_allocate,
|
|
|
|
M_TEMP, M_WAITOK);
|
|
|
|
for (stack_idx = 0; stack_idx < stacks_to_allocate;
|
|
|
|
stack_idx++)
|
|
|
|
td_infos[stack_idx] = sbuf_new(NULL, NULL,
|
|
|
|
MAXCOMLEN + sizeof(struct thread *) * 2 + 40,
|
|
|
|
SBUF_FIXEDLEN);
|
|
|
|
|
|
|
|
sleepq_lock(wchan);
|
|
|
|
sq = sleepq_lookup(wchan);
|
|
|
|
if (sq == NULL) {
|
|
|
|
/* This sleepq does not exist; exit and return ENOENT. */
|
|
|
|
error = ENOENT;
|
|
|
|
finished = true;
|
|
|
|
sleepq_release(wchan);
|
|
|
|
goto loop_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
stack_idx = 0;
|
|
|
|
/* Save thread info */
|
|
|
|
TAILQ_FOREACH_SAFE(td, &sq->sq_blocked[queue], td_slpq,
|
|
|
|
td_next) {
|
|
|
|
if (stack_idx >= stacks_to_allocate)
|
|
|
|
goto loop_end;
|
|
|
|
|
|
|
|
/* Note the td_lock is equal to the sleepq_lock here. */
|
|
|
|
stack_save_td(st[stack_idx], td);
|
|
|
|
|
|
|
|
sbuf_printf(td_infos[stack_idx], "%d: %s %p",
|
|
|
|
td->td_tid, td->td_name, td);
|
|
|
|
|
|
|
|
++stack_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
finished = true;
|
|
|
|
sleepq_release(wchan);
|
|
|
|
|
|
|
|
/* Print the stacks */
|
|
|
|
for (i = 0; i < stack_idx; i++) {
|
|
|
|
sbuf_finish(td_infos[i]);
|
|
|
|
sbuf_printf(sb, "--- thread %s: ---\n", sbuf_data(td_infos[i]));
|
|
|
|
stack_sbuf_print(sb, st[i]);
|
|
|
|
sbuf_printf(sb, "\n");
|
|
|
|
|
|
|
|
error = sbuf_error(sb);
|
|
|
|
if (error == 0)
|
|
|
|
*count_stacks_printed = stack_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
loop_end:
|
|
|
|
if (!finished)
|
|
|
|
sleepq_release(wchan);
|
|
|
|
for (stack_idx = 0; stack_idx < stacks_to_allocate;
|
|
|
|
stack_idx++)
|
|
|
|
stack_destroy(st[stack_idx]);
|
|
|
|
for (stack_idx = 0; stack_idx < stacks_to_allocate;
|
|
|
|
stack_idx++)
|
|
|
|
sbuf_delete(td_infos[stack_idx]);
|
|
|
|
free(st, M_TEMP);
|
|
|
|
free(td_infos, M_TEMP);
|
|
|
|
stacks_to_allocate *= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!finished && error == 0)
|
|
|
|
error = ENOMEM;
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
2016-03-17 01:05:53 +00:00
|
|
|
#endif
|
2016-03-16 04:22:32 +00:00
|
|
|
|
2008-03-19 07:22:07 +00:00
|
|
|
#ifdef SLEEPQUEUE_PROFILING
|
|
|
|
#define SLEEPQ_PROF_LOCATIONS 1024
|
2010-09-16 16:13:12 +00:00
|
|
|
#define SLEEPQ_SBUFSIZE 512
|
2008-03-19 07:22:07 +00:00
|
|
|
struct sleepq_prof {
|
|
|
|
LIST_ENTRY(sleepq_prof) sp_link;
|
|
|
|
const char *sp_wmesg;
|
|
|
|
long sp_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
LIST_HEAD(sqphead, sleepq_prof);
|
|
|
|
|
|
|
|
struct sqphead sleepq_prof_free;
|
|
|
|
struct sqphead sleepq_hash[SC_TABLESIZE];
|
|
|
|
static struct sleepq_prof sleepq_profent[SLEEPQ_PROF_LOCATIONS];
|
|
|
|
static struct mtx sleepq_prof_lock;
|
|
|
|
MTX_SYSINIT(sleepq_prof_lock, &sleepq_prof_lock, "sleepq_prof", MTX_SPIN);
|
|
|
|
|
|
|
|
static void
|
|
|
|
sleepq_profile(const char *wmesg)
|
|
|
|
{
|
|
|
|
struct sleepq_prof *sp;
|
|
|
|
|
|
|
|
mtx_lock_spin(&sleepq_prof_lock);
|
|
|
|
if (prof_enabled == 0)
|
|
|
|
goto unlock;
|
|
|
|
LIST_FOREACH(sp, &sleepq_hash[SC_HASH(wmesg)], sp_link)
|
|
|
|
if (sp->sp_wmesg == wmesg)
|
|
|
|
goto done;
|
|
|
|
sp = LIST_FIRST(&sleepq_prof_free);
|
|
|
|
if (sp == NULL)
|
|
|
|
goto unlock;
|
|
|
|
sp->sp_wmesg = wmesg;
|
|
|
|
LIST_REMOVE(sp, sp_link);
|
|
|
|
LIST_INSERT_HEAD(&sleepq_hash[SC_HASH(wmesg)], sp, sp_link);
|
|
|
|
done:
|
|
|
|
sp->sp_count++;
|
|
|
|
unlock:
|
|
|
|
mtx_unlock_spin(&sleepq_prof_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sleepq_prof_reset(void)
|
|
|
|
{
|
|
|
|
struct sleepq_prof *sp;
|
|
|
|
int enabled;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mtx_lock_spin(&sleepq_prof_lock);
|
|
|
|
enabled = prof_enabled;
|
|
|
|
prof_enabled = 0;
|
|
|
|
for (i = 0; i < SC_TABLESIZE; i++)
|
|
|
|
LIST_INIT(&sleepq_hash[i]);
|
|
|
|
LIST_INIT(&sleepq_prof_free);
|
|
|
|
for (i = 0; i < SLEEPQ_PROF_LOCATIONS; i++) {
|
|
|
|
sp = &sleepq_profent[i];
|
|
|
|
sp->sp_wmesg = NULL;
|
|
|
|
sp->sp_count = 0;
|
|
|
|
LIST_INSERT_HEAD(&sleepq_prof_free, sp, sp_link);
|
|
|
|
}
|
|
|
|
prof_enabled = enabled;
|
|
|
|
mtx_unlock_spin(&sleepq_prof_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
enable_sleepq_prof(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
int error, v;
|
|
|
|
|
|
|
|
v = prof_enabled;
|
|
|
|
error = sysctl_handle_int(oidp, &v, v, req);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
if (req->newptr == NULL)
|
|
|
|
return (error);
|
|
|
|
if (v == prof_enabled)
|
|
|
|
return (0);
|
|
|
|
if (v == 1)
|
|
|
|
sleepq_prof_reset();
|
|
|
|
mtx_lock_spin(&sleepq_prof_lock);
|
|
|
|
prof_enabled = !!v;
|
|
|
|
mtx_unlock_spin(&sleepq_prof_lock);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
reset_sleepq_prof_stats(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
int error, v;
|
|
|
|
|
|
|
|
v = 0;
|
|
|
|
error = sysctl_handle_int(oidp, &v, 0, req);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
if (req->newptr == NULL)
|
|
|
|
return (error);
|
|
|
|
if (v == 0)
|
|
|
|
return (0);
|
|
|
|
sleepq_prof_reset();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dump_sleepq_prof_stats(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
struct sleepq_prof *sp;
|
|
|
|
struct sbuf *sb;
|
|
|
|
int enabled;
|
|
|
|
int error;
|
|
|
|
int i;
|
|
|
|
|
2011-01-27 00:34:12 +00:00
|
|
|
error = sysctl_wire_old_buffer(req, 0);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
2010-09-16 16:13:12 +00:00
|
|
|
sb = sbuf_new_for_sysctl(NULL, NULL, SLEEPQ_SBUFSIZE, req);
|
2008-03-19 07:22:07 +00:00
|
|
|
sbuf_printf(sb, "\nwmesg\tcount\n");
|
|
|
|
enabled = prof_enabled;
|
|
|
|
mtx_lock_spin(&sleepq_prof_lock);
|
|
|
|
prof_enabled = 0;
|
|
|
|
mtx_unlock_spin(&sleepq_prof_lock);
|
|
|
|
for (i = 0; i < SC_TABLESIZE; i++) {
|
|
|
|
LIST_FOREACH(sp, &sleepq_hash[i], sp_link) {
|
|
|
|
sbuf_printf(sb, "%s\t%ld\n",
|
|
|
|
sp->sp_wmesg, sp->sp_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mtx_lock_spin(&sleepq_prof_lock);
|
|
|
|
prof_enabled = enabled;
|
|
|
|
mtx_unlock_spin(&sleepq_prof_lock);
|
|
|
|
|
2010-09-16 16:13:12 +00:00
|
|
|
error = sbuf_finish(sb);
|
2008-03-19 07:22:07 +00:00
|
|
|
sbuf_delete(sb);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSCTL_PROC(_debug_sleepq, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD,
|
|
|
|
NULL, 0, dump_sleepq_prof_stats, "A", "Sleepqueue profiling statistics");
|
|
|
|
SYSCTL_PROC(_debug_sleepq, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW,
|
|
|
|
NULL, 0, reset_sleepq_prof_stats, "I",
|
|
|
|
"Reset sleepqueue profiling statistics");
|
|
|
|
SYSCTL_PROC(_debug_sleepq, OID_AUTO, enable, CTLTYPE_INT | CTLFLAG_RW,
|
|
|
|
NULL, 0, enable_sleepq_prof, "I", "Enable sleepqueue profiling");
|
|
|
|
#endif
|
|
|
|
|
2006-01-27 22:24:07 +00:00
|
|
|
#ifdef DDB
|
|
|
|
DB_SHOW_COMMAND(sleepq, db_show_sleepqueue)
|
|
|
|
{
|
|
|
|
struct sleepqueue_chain *sc;
|
|
|
|
struct sleepqueue *sq;
|
2006-01-28 00:49:31 +00:00
|
|
|
#ifdef INVARIANTS
|
2006-01-27 22:24:07 +00:00
|
|
|
struct lock_object *lock;
|
2006-01-28 00:49:31 +00:00
|
|
|
#endif
|
2006-01-27 22:24:07 +00:00
|
|
|
struct thread *td;
|
|
|
|
void *wchan;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!have_addr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First, see if there is an active sleep queue for the wait channel
|
|
|
|
* indicated by the address.
|
|
|
|
*/
|
|
|
|
wchan = (void *)addr;
|
|
|
|
sc = SC_LOOKUP(wchan);
|
|
|
|
LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
|
|
|
|
if (sq->sq_wchan == wchan)
|
|
|
|
goto found;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Second, see if there is an active sleep queue at the address
|
|
|
|
* indicated.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < SC_TABLESIZE; i++)
|
|
|
|
LIST_FOREACH(sq, &sleepq_chains[i].sc_queues, sq_hash) {
|
|
|
|
if (sq == (struct sleepqueue *)addr)
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
|
|
|
db_printf("Unable to locate a sleep queue via %p\n", (void *)addr);
|
|
|
|
return;
|
|
|
|
found:
|
|
|
|
db_printf("Wait channel: %p\n", sq->sq_wchan);
|
|
|
|
db_printf("Queue type: %d\n", sq->sq_type);
|
2010-01-09 01:46:38 +00:00
|
|
|
#ifdef INVARIANTS
|
2006-01-27 22:24:07 +00:00
|
|
|
if (sq->sq_lock) {
|
2006-11-16 01:02:00 +00:00
|
|
|
lock = sq->sq_lock;
|
2006-01-27 22:24:07 +00:00
|
|
|
db_printf("Associated Interlock: %p - (%s) %s\n", lock,
|
|
|
|
LOCK_CLASS(lock)->lc_name, lock->lo_name);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
db_printf("Blocked threads:\n");
|
2006-12-16 06:54:09 +00:00
|
|
|
for (i = 0; i < NR_SLEEPQS; i++) {
|
|
|
|
db_printf("\nQueue[%d]:\n", i);
|
|
|
|
if (TAILQ_EMPTY(&sq->sq_blocked[i]))
|
|
|
|
db_printf("\tempty\n");
|
|
|
|
else
|
2017-10-09 18:33:29 +00:00
|
|
|
TAILQ_FOREACH(td, &sq->sq_blocked[i],
|
2006-12-16 06:54:09 +00:00
|
|
|
td_slpq) {
|
|
|
|
db_printf("\t%p (tid %d, pid %d, \"%s\")\n", td,
|
|
|
|
td->td_tid, td->td_proc->p_pid,
|
2008-07-28 18:33:43 +00:00
|
|
|
td->td_name);
|
2006-12-16 06:54:09 +00:00
|
|
|
}
|
In current code, threads performing an interruptible sleep (on both
sxlock, via the sx_{s, x}lock_sig() interface, or plain lockmgr), will
leave the waiters flag on forcing the owner to do a wakeup even when if
the waiter queue is empty.
That operation may lead to a deadlock in the case of doing a fake wakeup
on the "preferred" (based on the wakeup algorithm) queue while the other
queue has real waiters on it, because nobody is going to wakeup the 2nd
queue waiters and they will sleep indefinitively.
A similar bug, is present, for lockmgr in the case the waiters are
sleeping with LK_SLEEPFAIL on. In this case, even if the waiters queue
is not empty, the waiters won't progress after being awake but they will
just fail, still not taking care of the 2nd queue waiters (as instead the
lock owned doing the wakeup would expect).
In order to fix this bug in a cheap way (without adding too much locking
and complicating too much the semantic) add a sleepqueue interface which
does report the actual number of waiters on a specified queue of a
waitchannel (sleepq_sleepcnt()) and use it in order to determine if the
exclusive waiters (or shared waiters) are actually present on the lockmgr
(or sx) before to give them precedence in the wakeup algorithm.
This fix alone, however doesn't solve the LK_SLEEPFAIL bug. In order to
cope with it, add the tracking of how many exclusive LK_SLEEPFAIL waiters
a lockmgr has and if all the waiters on the exclusive waiters queue are
LK_SLEEPFAIL just wake both queues.
The sleepq_sleepcnt() introduction and ABI breakage require
__FreeBSD_version bumping.
Reported by: avg, kib, pho
Reviewed by: kib
Tested by: pho
2009-12-12 21:31:07 +00:00
|
|
|
db_printf("(expected: %u)\n", sq->sq_blockedcnt[i]);
|
2006-12-16 06:54:09 +00:00
|
|
|
}
|
2006-01-27 22:24:07 +00:00
|
|
|
}
|
2006-04-17 20:16:32 +00:00
|
|
|
|
|
|
|
/* Alias 'show sleepqueue' to 'show sleepq'. */
|
2008-09-15 22:45:14 +00:00
|
|
|
DB_SHOW_ALIAS(sleepqueue, db_show_sleepqueue);
|
2006-01-27 22:24:07 +00:00
|
|
|
#endif
|