fx.c revision 8c34bbb743f69040b9eb165ea639dddb7bb91f37
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/sysmacros.h>
#include <sys/priocntl.h>
#include <sys/fxpriocntl.h>
#include <sys/schedctl.h>
#include <sys/tnf_probe.h>
"FX",
0
};
};
static struct modlinkage modlinkage = {
};
/*
* control flags (kparms->fx_cflags).
*/
#define FX_MAX_UNPRIV_PRI 0 /* maximum unpriviledge priority */
/*
* The fxproc_t structures that have a registered callback vector,
* are also kept in an array of circular doubly linked lists. A hash on
* the thread id (from ddi_get_kt_did()) is used to determine which list
* each of such fxproc structures should be placed. Each list has a dummy
* "head" which is never removed, so the list is never empty.
*/
/* Insert fxproc into callback list */
#define FX_CB_LIST_INSERT(fxpp) \
{ \
mutex_enter(lockp); \
mutex_exit(lockp); \
}
/*
* Remove thread from callback list.
*/
#define FX_CB_LIST_DELETE(fxpp) \
{ \
mutex_enter(lockp); \
mutex_exit(lockp); \
}
/* adjust x to be between 0 and fx_maxumdpri */
#define FX_ADJUST_PRI(pri) \
{ \
if (pri < 0) \
pri = 0; \
else if (pri > fx_maxumdpri) \
pri = fx_maxumdpri; \
}
#define FX_ADJUST_QUANTUM(q) \
{ \
if (q > INT_MAX) \
q = INT_MAX; \
else if (q <= 0) \
q = FX_TQINF; \
}
/* that have callbacks */
/* list of fxprocs with */
/* callbacks */
static int fx_getclinfo(void *);
static int fx_parmsin(void *);
static int fx_parmsout(void *, pc_vaparms_t *);
static int fx_vaparmsin(void *, pc_vaparms_t *);
static int fx_vaparmsout(void *, pc_vaparms_t *);
static int fx_getclpri(pcpri_t *);
static int fx_alloc(void **, int);
static void fx_free(void *);
static void fx_exitclass(void *);
static void fx_parmsget(kthread_t *, void *);
static void fx_trapret(kthread_t *);
static void fx_preempt(kthread_t *);
static void fx_nullsys();
extern fxdpent_t *fx_getdptbl(void);
static void fx_list_release(fxproc_t *);
static struct classfuncs fx_classfuncs = {
/* class functions */
/* thread functions */
fx_nullsys, /* active */
fx_nullsys, /* inactive */
fx_nullsys, /* set_process_group */
};
int
_init()
{
return (mod_install(&modlinkage));
}
int
_fini()
{
return (EBUSY);
}
int
{
}
/*
* Fixed priority class initialization. Called by dispinit() at boot time.
* We can ignore the clparmsz argument since we know that the smallest
* possible parameter buffer is big enough for us.
*/
/* ARGSUSED */
static pri_t
{
int i;
extern pri_t fx_getmaxumdpri(void);
fx_dptbl = fx_getdptbl();
/*
* Initialize the hash table for fxprocs with callbacks
*/
for (i = 0; i < FX_CB_LISTS; i++) {
&fx_cb_plisthead[i];
}
/*
* We're required to return a pointer to our classfuncs
* structure and the highest global priority value we use.
*/
*clfuncspp = &fx_classfuncs;
return (fx_maxglobpri);
}
/*
* Get or reset the fx_dptbl values per the user's request.
*/
static int
{
int userdpsz;
int i;
if (get_udatamodel() == DATAMODEL_NATIVE) {
return (EFAULT);
}
#ifdef _SYSCALL32_IMPL
else {
/* get fxadmin struct from ILP32 caller */
return (EFAULT);
}
#endif /* _SYSCALL32_IMPL */
case FX_GETDPSIZE:
if (get_udatamodel() == DATAMODEL_NATIVE) {
return (EFAULT);
}
#ifdef _SYSCALL32_IMPL
else {
/* return fxadmin struct to ILP32 caller */
return (EFAULT);
}
#endif /* _SYSCALL32_IMPL */
break;
case FX_GETDPTBL:
fxdpsz);
return (EFAULT);
if (get_udatamodel() == DATAMODEL_NATIVE) {
return (EFAULT);
}
#ifdef _SYSCALL32_IMPL
else {
/* return fxadmin struct to ILP32 callers */
return (EFAULT);
}
#endif /* _SYSCALL32_IMPL */
break;
case FX_SETDPTBL:
/*
* We require that the requesting process has sufficient
* privileges. We also require that the table supplied by
* the user exactly match the current fx_dptbl in size.
*/
if (secpolicy_dispadm(reqpcredp) != 0) {
return (EPERM);
}
return (EINVAL);
}
/*
* We read the user supplied table into a temporary buffer
* where it is validated before being copied over the
* fx_dptbl.
*/
return (EFAULT);
}
for (i = 0; i < fxadmin.fx_ndpents; i++) {
/*
* Validate the user supplied values. All we are doing
* here is verifying that the values are within their
* allowable ranges and will not panic the system. We
* make no attempt to ensure that the resulting
* configuration makes sense or results in reasonable
* performance.
*/
if (tmpdpp[i].fx_quantum <= 0 &&
return (EINVAL);
}
}
/*
* Copy the user supplied values over the current fx_dptbl
* values. The fx_globpri member is read-only so we don't
* overwrite it.
*/
for (i = 0; i < fxadmin.fx_ndpents; i++) {
}
break;
default:
return (EINVAL);
}
return (0);
}
/*
* Allocate a fixed priority class specific thread structure and
* initialize it with the parameters supplied. Also move the thread
* to specified priority.
*/
static int
void *bufp)
{
/*
* Initialize the fxproc structure.
*/
/*
* Use default values.
*/
} else {
/*
* Use supplied values.
*/
reqfxuprilim = 0;
} else {
secpolicy_setpriority(reqpcredp) != 0)
return (EPERM);
}
} else {
secpolicy_setpriority(reqpcredp) != 0)
return (EPERM);
/*
* Set the user priority to the requested value
* or the upri limit, whichever is lower.
*/
if (reqfxupri > reqfxuprilim)
}
/ fx_maxupri;
} else {
if (secpolicy_setpriority(reqpcredp) != 0)
return (EPERM);
else {
}
}
}
thread_lock(t); /* get dispatcher lock on thread */
t->t_schedflag &= ~TS_RUNQMATCH;
fx_change_priority(t, fxpp);
thread_unlock(t);
return (0);
}
/*
* The thread is exiting.
*/
static void
{
thread_lock(t);
/*
* A thread could be exiting in between clock ticks, so we need to
* calculate how much CPU time it used since it was charged last time.
*
* CPU caps are not enforced on exiting processes - it is usually
* desirable to exit as soon as possible to free resources.
*/
thread_unlock(t);
return;
}
thread_unlock(t);
}
/*
* Exiting the class. Free fxproc structure of thread.
*/
static void
fx_exitclass(void *procp)
{
} else
}
/* ARGSUSED */
static int
{
/*
* A thread can always leave the FX class
*/
return (0);
}
/*
* Initialize fixed-priority class specific proc structure for a child.
* callbacks are not inherited upon fork.
*/
static int
{
thread_lock(t);
/*
* Initialize child's fxproc structure.
*/
thread_unlock(t);
/*
* Link new structure into fxproc list.
*/
return (0);
}
/*
* Child is placed at back of dispatcher queue and parent gives
* up processor so that the child runs first after the fork.
* This allows the child immediately execing to break the multiple
* use of copy on write pages with no disk home. The parent will
* get to steal them back rather than uselessly copying them.
*/
static void
{
/*
* Grab the child's p_lock before dropping pidlock to ensure
* the process does not disappear before we set it running.
*/
thread_lock(t);
fx_setrun(t);
thread_unlock(t);
swtch();
}
/*
* Get information about the fixed-priority class into the buffer
* pointed to by fxinfop. The maximum configured user priority
* is the only information we supply.
*/
static int
fx_getclinfo(void *infop)
{
return (0);
}
/*
* Return the global scheduling priority ranges for the fixed-priority
* class in pcpri_t structure.
*/
static int
{
return (0);
}
static void
{}
/*
* Get the fixed-priority parameters of the thread pointed to by
* fxprocp into the buffer pointed to by fxparmsp.
*/
static void
{
}
/*
* Check the validity of the fixed-priority parameters in the buffer
* pointed to by fxparmsp.
*/
static int
fx_parmsin(void *parmsp)
{
/*
* Check validity of parameters.
*/
fxparmsp->fx_uprilim < 0) &&
return (EINVAL);
return (EINVAL);
return (EINVAL);
cflags |= FX_DOUPRILIM;
}
/*
* convert the buffer to kernel format.
*/
if (fxparmsp->fx_tqnsecs >= 0) {
return (ERANGE);
} else {
return (EINVAL);
}
return (0);
}
/*
* Check the validity of the fixed-priority parameters in the pc_vaparms_t
* structure vaparmsp and put them in the buffer pointed to by fxprmsp.
* pc_vaparms_t contains (key, value) pairs of parameter.
*/
static int
{
int nsecs = 0;
/*
* First check the validity of parameters and convert them
* from the user supplied format to the internal format.
*/
return (EINVAL);
case FX_KY_UPRILIM:
if (limflag++)
return (EINVAL);
fxprmsp->fx_uprilim < 0)
return (EINVAL);
break;
case FX_KY_UPRI:
if (priflag++)
return (EINVAL);
return (EINVAL);
break;
case FX_KY_TQSECS:
if (secflag++)
return (EINVAL);
break;
case FX_KY_TQNSECS:
if (nsecflag++)
return (EINVAL);
break;
default:
return (EINVAL);
}
}
if (vaparmsp->pc_vaparmscnt == 0) {
/*
* Use default parameters.
*/
fxprmsp->fx_uprilim = 0;
return (EINVAL);
if (nsecs >= 0) {
return (ERANGE);
} else {
return (EINVAL);
}
}
return (0);
}
/*
* Nothing to do here but return success.
*/
/* ARGSUSED */
static int
{
return (0);
/*
* Quantum field set to special value (e.g. FX_TQINF)
*/
} else {
/* Convert quantum from ticks to seconds-nanoseconds */
}
return (0);
}
/*
* Copy all selected fixed-priority class parameters to the user.
* The parameters are specified by a key.
*/
static int
{
int nsecs;
return (EINVAL);
/*
* Quantum field set to special value (e.g. FX_TQINF).
*/
secs = 0;
} else {
/*
* Convert quantum from ticks to seconds-nanoseconds.
*/
}
case FX_KY_UPRILIM:
if (limflag++)
return (EINVAL);
return (EFAULT);
break;
case FX_KY_UPRI:
if (priflag++)
return (EINVAL);
return (EFAULT);
break;
case FX_KY_TQSECS:
if (secflag++)
return (EINVAL);
return (EFAULT);
break;
case FX_KY_TQNSECS:
if (nsecflag++)
return (EINVAL);
return (EFAULT);
break;
default:
return (EINVAL);
}
}
return (0);
}
/*
* Set the scheduling parameters of the thread pointed to by fxprocp
* to those specified in the buffer pointed to by fxparmsp.
*/
/* ARGSUSED */
static int
{
char nice;
else
/*
* Basic permissions enforced by generic kernel code
* for all classes require that a thread attempting
* to change the scheduling parameters of a target
* thread be privileged or have a real or effective
* UID matching that of the target thread. We are not
* called unless these basic permission checks have
* already passed. The fixed priority class requires in
* addition that the calling thread be privileged if it
* is attempting to raise the pri above its current
* value This may have been checked previously but if our
* caller passed us a non-NULL credential pointer we assume
* it hasn't and we check it here.
*/
secpolicy_setpriority(reqpcredp) != 0) {
return (EPERM);
}
else
/*
* Make sure the user priority doesn't exceed the upri limit.
*/
if (reqfxupri > reqfxuprilim)
/*
* Set fx_nice to the nice value corresponding to the user
* priority we are setting. Note that setting the nice field
* of the parameter struct won't affect upri or nice.
*/
return (0);
}
/*
* Return the global scheduling priority that would be assigned
* to a thread entering the fixed-priority class with the fx_upri.
*/
static pri_t
fx_globpri(kthread_t *t)
{
}
/*
* Arrange for thread to be placed in appropriate location
* on dispatcher queue.
*
* This is called with the current thread in TS_ONPROC and locked.
*/
static void
fx_preempt(kthread_t *t)
{
/*
* Check to see if we're doing "preemption control" here. If
* we are, and if the user has requested that this thread not
* be preempted, and if preemptions haven't been put off for
* too long, let the preemption happen here but try to make
* sure the thread is rescheduled as soon as possible. We do
* this by putting it on the front of the highest priority run
* queue in the FX class. If the preemption has been put off
* for too long, clear the "nopreempt" bit and let the thread
* be preempted.
*/
if (t->t_schedctl && schedctl_get_nopreempt(t)) {
schedctl_set_yield(t, 1);
setfrontdq(t);
return;
} else {
schedctl_set_nopreempt(t, 0);
/*
* Fall through and be preempted below.
*/
}
}
&new_quantum, &newpri);
}
}
/*
* This thread may be placed on wait queue by CPU Caps. In this case we
* do not need to do anything until it is removed from the wait queue.
*/
if (CPUCAPS_ENFORCE(t)) {
return;
}
setbackdq(t);
} else {
setfrontdq(t);
}
}
static void
{
if (t->t_disp_time != lbolt)
setbackdq(t);
else
setfrontdq(t);
}
/*
* Prepare thread for sleep. We reset the thread priority so it will
* run at the kernel priority level when it wakes up.
*/
static void
{
ASSERT(THREAD_LOCK_HELD(t));
/*
* Account for time spent on CPU before going to sleep.
*/
}
}
/*
* Return Values:
*
* -1 if the thread is loaded or is not eligible to be swapped in.
*
* FX and RT threads are designed so that they don't swapout; however,
* it is possible that while the thread is swapped out and in another class, it
* can be changed to FX or RT. Since these threads should be swapped in
* as soon as they're runnable, rt_swapin returns SHRT_MAX, and fx_swapin
* returns SHRT_MAX - 1, so that it gives deference to any swapped out
* RT threads.
*/
/* ARGSUSED */
static pri_t
{
ASSERT(THREAD_LOCK_HELD(t));
}
return (tpri);
}
/*
* Return Values
* -1 if the thread isn't loaded or is not eligible to be swapped out.
*/
/* ARGSUSED */
static pri_t
{
ASSERT(THREAD_LOCK_HELD(t));
return (-1);
}
/* ARGSUSED */
static void
{
ASSERT(THREAD_LOCK_HELD(t));
}
}
/*
* Check for time slice expiration. If time slice has expired
* set runrun to cause preemption.
*/
static void
{
thread_lock(t);
&new_quantum, &newpri);
}
fx_change_priority(t, fxpp);
}
}
/*
* Keep track of thread's project CPU usage. Note that projects
* get charged even when threads are running in the kernel.
*/
(--fxpp->fx_timeleft <= 0)) {
/*
* If we're doing preemption control and trying to
* avoid preempting this thread, just note that
* the thread should yield soon and let it keep
* running (unless it's been a while).
*/
if (t->t_schedctl && schedctl_get_nopreempt(t)) {
kthread_t *, t);
schedctl_set_yield(t, 1);
return;
}
"schedctl FX fx_tick", /* CSTYLED */,
}
/*
* When the priority of a thread is changed,
* it may be necessary to adjust its position
* on a sleep queue or dispatch queue. Even
* when the priority is not changed, we need
* to preserve round robin on dispatch queue.
* The function thread_change_pri accomplishes
* this.
*/
if (thread_change_pri(t, new_pri, 0)) {
} else {
}
}
if (call_cpu_surrender) {
cpu_surrender(t);
}
thread_unlock_nopreempt(t); /* clock thread can't be preempted */
}
static void
fx_trapret(kthread_t *t)
{
ASSERT(THREAD_LOCK_HELD(t));
}
/*
* Processes waking up go to the back of their queue.
*/
static void
{
ASSERT(THREAD_LOCK_HELD(t));
&new_quantum, &newpri);
}
}
}
if (t->t_disp_time != lbolt)
setbackdq(t);
else
setfrontdq(t);
}
/*
* When a thread yields, put it on the back of the run queue.
*/
static void
{
ASSERT(THREAD_LOCK_HELD(t));
/*
* Collect CPU usage spent before yielding CPU.
*/
&new_quantum, &newpri);
}
}
/*
* Clear the preemption control "yield" bit since the user is
* doing a yield.
*/
if (t->t_schedctl)
schedctl_set_yield(t, 0);
if (fxpp->fx_timeleft <= 0) {
/*
* Time slice was artificially extended to avoid
* preemption, so pretend we're preempting it now.
*/
}
setbackdq(t);
}
/*
* Increment the nice value of the specified thread by incr and
* return the new value in *retvalp.
*/
static int
{
int newnice;
/* If there's no change to priority, just return current setting */
if (incr == 0) {
if (retvalp) {
}
return (0);
}
secpolicy_setpriority(cr) != 0)
return (EPERM);
/*
* Specifying a nice increment greater than the upper limit of
* 2 * NZERO - 1 will result in the thread's nice value being
* set to the upper limit. We check for this before computing
* the new value because otherwise we could get overflow
* if a privileged user specified some ridiculous increment.
*/
else if (newnice < 0)
newnice = 0;
/*
* Reset the uprilim and upri values of the thread. Adjust
* time quantum accordingly.
*/
/*
* Although fx_parmsset already reset fx_nice it may
* not have been set to precisely the value calculated above
* because fx_parmsset determines the nice value from the
* user priority and we may have truncated during the integer
* conversion from nice value to user priority and back.
* We reset fx_nice to the value we calculated above.
*/
if (retvalp)
return (0);
}
static void
{
ASSERT(THREAD_LOCK_HELD(t));
/* curthread is always onproc */
THREAD_CHANGE_PRI(t, new_pri);
if (t == cp->cpu_dispthread)
if (DISP_MUST_SURRENDER(t)) {
cpu_surrender(t);
} else {
}
} else {
/*
* When the priority of a thread is changed,
* it may be necessary to adjust its position
* on a sleep queue or dispatch queue.
* The function thread_change_pri accomplishes
* this.
*/
if (thread_change_pri(t, new_pri, 0)) {
/*
* The thread was on a run queue. Reset
* its CPU timeleft from the quantum
* associated with the new priority.
*/
} else {
}
}
}
static int
{
void *bufp;
return (ENOMEM);
} else {
*p = bufp;
return (0);
}
}
static void
{
if (bufp)
}
/*
* Release the callback list mutex after successful lookup
*/
void
{
}
fxproc_t *
{
/*
* The caller is responsible for calling
* fx_list_release to drop the lock upon
* successful lookup
*/
return (fxpp);
}
}
}
/*
* register a callback set of routines for current thread
* thread should already be in FX class
*/
int
{
if (fx_callback == NULL)
return (EINVAL);
if (secpolicy_dispadm(CRED()) != 0)
return (EPERM);
return (EINVAL);
return (EINVAL);
return (EINVAL);
}
return (EINVAL);
}
if (pri != FX_CB_NOCHANGE) {
} else if (quantum != FX_NOCHANGE) {
}
else {
}
}
/*
* Link new structure into fxproc list.
*/
return (0);
}
/* unregister a callback set of routines for current thread */
int
{
/*
* did not have a registered callback;
*/
return (EINVAL);
}
return (0);
}
/*
*/
int
{
return (EINVAL);
/*
* either thread had exited or did not have a registered
* callback;
*/
return (ESRCH);
}
if (pri != FX_CB_NOCHANGE) {
} else if (quantum != FX_NOCHANGE) {
}
} else {
}
}
return (0);
}
/*
* return an iblock cookie for mutex initialization to be used in callbacks
*/
void *
{
}
/*
* return maximum relative priority
*/
{
return (fx_maxumdpri);
}