#include <kern/task.h>
#include <kern/clock.h> /* for absolutetime_to_microtime() */
-#include <netinet/in.h> /* for TRAFFIC_MGT_SO_BACKGROUND */
+#include <netinet/in.h> /* for TRAFFIC_MGT_SO_* */
#include <sys/socketvar.h> /* for struct socket */
#include <vm/vm_map.h>
int donice(struct proc *curp, struct proc *chgp, int n);
int dosetrlimit(struct proc *p, u_int which, struct rlimit *limp);
-static int do_background_thread(struct proc *curp, int priority);
+int uthread_get_background_state(uthread_t);
+static void do_background_socket(struct proc *p, thread_t thread, int priority);
+static int do_background_thread(struct proc *curp, thread_t thread, int priority);
+static int do_background_proc(struct proc *curp, struct proc *targetp, int priority);
+void proc_apply_task_networkbg_internal(proc_t, thread_t);
+void proc_restore_task_networkbg_internal(proc_t, thread_t);
rlim_t maxdmap = MAXDSIZ; /* XXX */
rlim_t maxsmap = MAXSSIZ - PAGE_SIZE; /* XXX */
*/
__private_extern__ int maxfilesperproc = OPEN_MAX; /* per-proc open files limit */
-SYSCTL_INT( _kern, KERN_MAXPROCPERUID, maxprocperuid, CTLFLAG_RW,
+SYSCTL_INT(_kern, KERN_MAXPROCPERUID, maxprocperuid, CTLFLAG_RW | CTLFLAG_LOCKED,
&maxprocperuid, 0, "Maximum processes allowed per userid" );
-SYSCTL_INT( _kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
+SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW | CTLFLAG_LOCKED,
&maxfilesperproc, 0, "Maximum files allowed open per process" );
/* Args and fn for proc_iteration callback used in setpriority */
ut = get_bsdthread_info(thread);
low = 0;
- if ( (ut->uu_flag & UT_BACKGROUND) != 0 ) {
+ if ( (ut->uu_flag & UT_BACKGROUND_TRAFFIC_MGT) != 0 ) {
low = 1;
}
break;
}
case PRIO_DARWIN_THREAD: {
+ /* process marked for termination no priority management */
+ if ((curp->p_lflag & P_LPTERMINATE) != 0)
+ return(EINVAL);
/* we currently only support the current thread */
if (uap->who != 0) {
return (EINVAL);
}
- error = do_background_thread(curp, uap->prio);
+ error = do_background_thread(curp, current_thread(), uap->prio);
+ if (!error) {
+ (void) do_background_socket(curp, current_thread(), uap->prio);
+ }
found++;
break;
}
+ case PRIO_DARWIN_PROCESS: {
+ if (uap->who == 0)
+ p = curp;
+ else {
+ p = proc_find(uap->who);
+ if (p == 0)
+ break;
+ refheld = 1;
+ }
+
+ /* process marked for termination no priority management */
+ if ((p->p_lflag & P_LPTERMINATE) != 0) {
+ error = EINVAL;
+ } else {
+ error = do_background_proc(curp, p, uap->prio);
+ if (!error) {
+ (void) do_background_socket(p, NULL, uap->prio);
+ }
+
+ }
+ found++;
+ if (refheld != 0)
+ proc_rele(p);
+ break;
+ }
+
default:
return (EINVAL);
}
ucred = kauth_cred_proc_ref(curp);
my_cred = kauth_cred_proc_ref(chgp);
- if (suser(ucred, NULL) && ucred->cr_ruid &&
+ if (suser(ucred, NULL) && kauth_cred_getruid(ucred) &&
kauth_cred_getuid(ucred) != kauth_cred_getuid(my_cred) &&
- ucred->cr_ruid != kauth_cred_getuid(my_cred)) {
+ kauth_cred_getruid(ucred) != kauth_cred_getuid(my_cred)) {
error = EPERM;
goto out;
}
return (error);
}
-/*
- * do_background_thread
- * Returns: 0 Success
- * XXX - todo - does this need a MACF hook?
- */
static int
-do_background_thread(struct proc *curp, int priority)
+do_background_proc(struct proc *curp, struct proc *targetp, int priority)
{
- int i;
- thread_t thread;
- struct uthread *ut;
- thread_precedence_policy_data_t policy;
- struct filedesc *fdp;
- struct fileproc *fp;
-
- thread = current_thread();
- ut = get_bsdthread_info(thread);
+ int error = 0;
+ kauth_cred_t ucred;
+ kauth_cred_t target_cred;
- if ( (priority & PRIO_DARWIN_BG) == 0 ) {
- /* turn off backgrounding of thread */
- if ( (ut->uu_flag & UT_BACKGROUND) == 0 ) {
- /* already off */
- return(0);
- }
+ ucred = kauth_cred_get();
+ target_cred = kauth_cred_proc_ref(targetp);
- /* clear background bit in thread and disable disk IO throttle */
- ut->uu_flag &= ~UT_BACKGROUND;
- ut->uu_iopol_disk = IOPOL_NORMAL;
+ if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
+ kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
+ kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred))
+ {
+ error = EPERM;
+ goto out;
+ }
- /* reset thread priority (we did not save previous value) */
- policy.importance = 0;
- thread_policy_set( thread, THREAD_PRECEDENCE_POLICY,
- (thread_policy_t)&policy,
- THREAD_PRECEDENCE_POLICY_COUNT );
+#if CONFIG_MACF
+ error = mac_proc_check_sched(curp, targetp);
+ if (error)
+ goto out;
+#endif
+
+ if (priority == PRIO_DARWIN_NONUI)
+ error = proc_apply_task_gpuacc(targetp->task, TASK_POLICY_HWACCESS_GPU_ATTRIBUTE_NOACCESS);
+ else
+ error = proc_set_and_apply_bgtaskpolicy(targetp->task, priority);
+ if (error)
+ goto out;
+
+out:
+ kauth_cred_unref(&target_cred);
+ return (error);
+}
+
+static void
+do_background_socket(struct proc *p, thread_t thread, int priority)
+{
+ struct filedesc *fdp;
+ struct fileproc *fp;
+ int i;
+
+ if (priority == PRIO_DARWIN_BG) {
+ /*
+ * For PRIO_DARWIN_PROCESS (thread is NULL), simply mark
+ * the sockets with the background flag. There's nothing
+ * to do here for the PRIO_DARWIN_THREAD case.
+ */
+ if (thread == NULL) {
+ proc_fdlock(p);
+ fdp = p->p_fd;
+
+ for (i = 0; i < fdp->fd_nfiles; i++) {
+ struct socket *sockp;
+
+ fp = fdp->fd_ofiles[i];
+ if (fp == NULL || (fdp->fd_ofileflags[i] & UF_RESERVED) != 0 ||
+ fp->f_fglob->fg_type != DTYPE_SOCKET) {
+ continue;
+ }
+ sockp = (struct socket *)fp->f_fglob->fg_data;
+ socket_set_traffic_mgt_flags(sockp, TRAFFIC_MGT_SO_BACKGROUND);
+ sockp->so_background_thread = NULL;
+ }
+ proc_fdunlock(p);
+ }
+
+ } else {
/* disable networking IO throttle.
* NOTE - It is a known limitation of the current design that we
* could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for
* sockets created by other threads within this process.
*/
- proc_fdlock(curp);
- fdp = curp->p_fd;
+ proc_fdlock(p);
+ fdp = p->p_fd;
for ( i = 0; i < fdp->fd_nfiles; i++ ) {
- struct socket *sockp;
-
+ struct socket *sockp;
+
fp = fdp->fd_ofiles[ i ];
- if ( fp == NULL || (fdp->fd_ofileflags[ i ] & UF_RESERVED) != 0 ||
- fp->f_fglob->fg_type != DTYPE_SOCKET ) {
+ if ( fp == NULL || (fdp->fd_ofileflags[ i ] & UF_RESERVED) != 0 ||
+ fp->f_fglob->fg_type != DTYPE_SOCKET ) {
continue;
}
sockp = (struct socket *)fp->f_fglob->fg_data;
- if ( sockp->so_background_thread != thread ) {
+ /* skip if only clearing this thread's sockets */
+ if ((thread) && (sockp->so_background_thread != thread)) {
continue;
}
- sockp->so_traffic_mgt_flags &= ~TRAFFIC_MGT_SO_BACKGROUND;
+ socket_clear_traffic_mgt_flags(sockp, TRAFFIC_MGT_SO_BACKGROUND);
sockp->so_background_thread = NULL;
}
- proc_fdunlock(curp);
-
- return(0);
+ proc_fdunlock(p);
}
+}
+
+
+/*
+ * do_background_thread
+ * Returns: 0 Success
+ * XXX - todo - does this need a MACF hook?
+ *
+ * NOTE: To maintain binary compatibility with PRIO_DARWIN_THREAD with respect
+ * to network traffic management, UT_BACKGROUND_TRAFFIC_MGT is set/cleared
+ * along with UT_BACKGROUND flag, as the latter alone no longer implies
+ * any form of traffic regulation (it simply means that the thread is
+ * background.) With PRIO_DARWIN_PROCESS, any form of network traffic
+ * management must be explicitly requested via whatever means appropriate,
+ * and only TRAFFIC_MGT_SO_BACKGROUND is set via do_background_socket().
+ */
+static int
+do_background_thread(struct proc *curp __unused, thread_t thread, int priority)
+{
+ struct uthread *ut;
+ int error = 0;
- /* background this thread */
- if ( (ut->uu_flag & UT_BACKGROUND) != 0 ) {
- /* already backgrounded */
- return(0);
+ ut = get_bsdthread_info(thread);
+
+ /* Backgrounding is unsupported for threads in vfork */
+ if ( (ut->uu_flag & UT_VFORK) != 0) {
+ return(EPERM);
}
- /* tag thread as background and throttle disk IO */
- ut->uu_flag |= UT_BACKGROUND;
- ut->uu_iopol_disk = IOPOL_THROTTLE;
+ error = proc_set_and_apply_bgthreadpolicy(curp->task, thread_tid(thread), priority);
+ return(error);
- policy.importance = INT_MIN;
- thread_policy_set( thread, THREAD_PRECEDENCE_POLICY,
- (thread_policy_t)&policy,
- THREAD_PRECEDENCE_POLICY_COUNT );
-
- /* throttle networking IO happens in socket( ) syscall.
- * If UT_BACKGROUND is set in the current thread then
- * TRAFFIC_MGT_SO_BACKGROUND socket option is set.
- */
- return(0);
}
+#if CONFIG_EMBEDDED
+int mach_do_background_thread(thread_t thread, int prio);
+
+int
+mach_do_background_thread(thread_t thread, int prio)
+{
+ int error = 0;
+ struct proc *curp = NULL;
+ struct proc *targetp = NULL;
+ kauth_cred_t ucred;
+
+ targetp = get_bsdtask_info(get_threadtask(thread));
+ if (!targetp) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ curp = proc_self();
+ if (curp == PROC_NULL) {
+ return KERN_FAILURE;
+ }
+
+ ucred = kauth_cred_proc_ref(curp);
+
+ if (suser(ucred, NULL) && curp != targetp) {
+ error = KERN_PROTECTION_FAILURE;
+ goto out;
+ }
+
+ error = do_background_thread(curp, thread, prio);
+ if (!error) {
+ (void) do_background_socket(curp, thread, prio);
+ } else {
+ if (error == EPERM) {
+ error = KERN_PROTECTION_FAILURE;
+ } else {
+ error = KERN_FAILURE;
+ }
+ }
+
+out:
+ proc_rele(curp);
+ kauth_cred_unref(&ucred);
+ return error;
+}
+#endif /* CONFIG_EMBEDDED */
/*
* Returns: 0 Success
size = round_page_64(limp->rlim_cur);
size -= round_page_64(alimp->rlim_cur);
-#if STACK_GROWTH_UP
- /* go to top of current stack */
- addr = p->user_stack + round_page_64(alimp->rlim_cur);
-#else /* STACK_GROWTH_UP */
addr = p->user_stack - round_page_64(limp->rlim_cur);
-#endif /* STACK_GROWTH_UP */
kr = mach_vm_protect(current_map(),
addr, size,
FALSE, VM_PROT_DEFAULT);
*/
cur_sp = thread_adjuserstack(current_thread(),
0);
-#if STACK_GROWTH_UP
- if (cur_sp >= p->user_stack &&
- cur_sp < (p->user_stack +
- round_page_64(alimp->rlim_cur))) {
- /* current stack pointer is in main stack */
- if (cur_sp >= (p->user_stack +
- round_page_64(limp->rlim_cur))) {
- /*
- * New limit would cause
- * current usage to be invalid:
- * reject new limit.
- */
- error = EINVAL;
- goto out;
- }
- } else {
- /* not on the main stack: reject */
- error = EINVAL;
- goto out;
- }
-
-#else /* STACK_GROWTH_UP */
if (cur_sp <= p->user_stack &&
cur_sp > (p->user_stack -
round_page_64(alimp->rlim_cur))) {
error = EINVAL;
goto out;
}
-#endif /* STACK_GROWTH_UP */
size = round_page_64(alimp->rlim_cur);
size -= round_page_64(limp->rlim_cur);
-#if STACK_GROWTH_UP
- addr = p->user_stack + round_page_64(limp->rlim_cur);
-#else /* STACK_GROWTH_UP */
addr = p->user_stack - round_page_64(alimp->rlim_cur);
-#endif /* STACK_GROWTH_UP */
kr = mach_vm_protect(current_map(),
addr, size,
task = p->task;
if (task) {
- task_basic_info_32_data_t tinfo;
+ mach_task_basic_info_data_t tinfo;
task_thread_times_info_data_t ttimesinfo;
task_events_info_data_t teventsinfo;
mach_msg_type_number_t task_info_count, task_ttimes_count;
mach_msg_type_number_t task_events_count;
struct timeval ut,st;
- task_info_count = TASK_BASIC_INFO_32_COUNT;
- task_info(task, TASK_BASIC2_INFO_32,
+ task_info_count = MACH_TASK_BASIC_INFO_COUNT;
+ task_info(task, MACH_TASK_BASIC_INFO,
(task_info_t)&tinfo, &task_info_count);
ut.tv_sec = tinfo.user_time.seconds;
ut.tv_usec = tinfo.user_time.microseconds;
if (p->p_stats->p_ru.ru_nivcsw < 0)
p->p_stats->p_ru.ru_nivcsw = 0;
- p->p_stats->p_ru.ru_maxrss = tinfo.resident_size;
+ p->p_stats->p_ru.ru_maxrss = tinfo.resident_size_max;
}
}
iopolicysys(__unused struct proc *p, __unused struct iopolicysys_args *uap, __unused int32_t *retval)
{
int error = 0;
- thread_t thread = THREAD_NULL;
- int *policy;
- struct uthread *ut = NULL;
struct _iopol_param_t iop_param;
+ int processwide = 0;
if ((error = copyin(uap->arg, &iop_param, sizeof(iop_param))) != 0)
- goto exit;
+ goto out;
if (iop_param.iop_iotype != IOPOL_TYPE_DISK) {
error = EINVAL;
- goto exit;
+ goto out;
}
switch (iop_param.iop_scope) {
case IOPOL_SCOPE_PROCESS:
- policy = &p->p_iopol_disk;
+ processwide = 1;
break;
case IOPOL_SCOPE_THREAD:
- thread = current_thread();
- ut = get_bsdthread_info(thread);
- policy = &ut->uu_iopol_disk;
+ processwide = 0;
break;
default:
error = EINVAL;
- goto exit;
+ goto out;
}
switch(uap->cmd) {
case IOPOL_NORMAL:
case IOPOL_THROTTLE:
case IOPOL_PASSIVE:
- proc_lock(p);
- *policy = iop_param.iop_policy;
- proc_unlock(p);
+ case IOPOL_UTILITY:
+ if(processwide != 0)
+ proc_apply_task_diskacc(current_task(), iop_param.iop_policy);
+ else
+ proc_apply_thread_selfdiskacc(iop_param.iop_policy);
+
break;
default:
error = EINVAL;
- goto exit;
+ goto out;
}
break;
+
case IOPOL_CMD_GET:
- switch (*policy) {
- case IOPOL_DEFAULT:
- case IOPOL_NORMAL:
- case IOPOL_THROTTLE:
- case IOPOL_PASSIVE:
- iop_param.iop_policy = *policy;
- break;
- default: // in-kernel
- // this should never happen
- printf("%s: unknown I/O policy %d\n", __func__, *policy);
- // restore to default value
- *policy = IOPOL_DEFAULT;
- iop_param.iop_policy = *policy;
- }
-
+ if(processwide != 0)
+ iop_param.iop_policy = proc_get_task_disacc(current_task());
+ else
+ iop_param.iop_policy = proc_get_thread_selfdiskacc();
+
error = copyout((caddr_t)&iop_param, uap->arg, sizeof(iop_param));
+
break;
default:
error = EINVAL; // unknown command
break;
}
- exit:
+out:
*retval = error;
return (error);
}
boolean_t thread_is_io_throttled(void);
boolean_t
-thread_is_io_throttled(void) {
-
- int policy;
- struct uthread *ut;
+thread_is_io_throttled(void)
+{
+ return(proc_get_task_selfdiskacc() == IOPOL_THROTTLE);
+}
- policy = current_proc()->p_iopol_disk;
+void
+proc_apply_task_networkbg(void * bsd_info)
+{
+ proc_t p = PROC_NULL;
+ proc_t curp = (proc_t)bsd_info;
+ pid_t pid;
+
+ pid = curp->p_pid;
+ p = proc_find(pid);
+ if (p != PROC_NULL) {
+ do_background_socket(p, NULL, PRIO_DARWIN_BG);
+ proc_rele(p);
+ }
+}
- ut = get_bsdthread_info(current_thread());
+void
+proc_restore_task_networkbg(void * bsd_info)
+{
+ proc_t p = PROC_NULL;
+ proc_t curp = (proc_t)bsd_info;
+ pid_t pid;
+
+ pid = curp->p_pid;
+ p = proc_find(pid);
+ if (p != PROC_NULL) {
+ do_background_socket(p, NULL, 0);
+ proc_rele(p);
+ }
- if (ut->uu_iopol_disk != IOPOL_DEFAULT)
- policy = ut->uu_iopol_disk;
+}
- if (policy == IOPOL_THROTTLE)
- return TRUE;
+void
+proc_set_task_networkbg(void * bsdinfo, int setbg)
+{
+ if (setbg != 0)
+ proc_apply_task_networkbg(bsdinfo);
+ else
+ proc_restore_task_networkbg(bsdinfo);
+}
- return FALSE;
+void
+proc_apply_task_networkbg_internal(proc_t p, thread_t thread)
+{
+ if (p != PROC_NULL) {
+ do_background_socket(p, thread, PRIO_DARWIN_BG);
+ }
}
+void
+proc_restore_task_networkbg_internal(proc_t p, thread_t thread)
+{
+ if (p != PROC_NULL) {
+ do_background_socket(p, thread, PRIO_DARWIN_BG);
+ }
+}
+