summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sys/kern/kern_exit.c3
-rw-r--r--sys/kern/kern_fork.c5
-rw-r--r--sys/kern/kern_resource.c40
-rw-r--r--sys/kern/sched_bsd.c20
-rw-r--r--sys/sys/proc.h3
-rw-r--r--sys/sys/resourcevar.h4
6 files changed, 51 insertions, 24 deletions
diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c
index b190eb6a7ac..7d38cc93870 100644
--- a/sys/kern/kern_exit.c
+++ b/sys/kern/kern_exit.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_exit.c,v 1.171 2018/11/12 15:09:17 visa Exp $ */
+/* $OpenBSD: kern_exit.c,v 1.172 2019/01/06 12:59:45 visa Exp $ */
/* $NetBSD: kern_exit.c,v 1.39 1996/04/22 01:38:25 christos Exp $ */
/*
@@ -192,6 +192,7 @@ exit1(struct proc *p, int rv, int flags)
fdfree(p);
timeout_del(&pr->ps_realit_to);
+ timeout_del(&pr->ps_rucheck_to);
#ifdef SYSVSEM
semexit(pr);
#endif
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index dd26b0ac5c8..578752c75aa 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_fork.c,v 1.208 2018/11/12 15:09:17 visa Exp $ */
+/* $OpenBSD: kern_fork.c,v 1.209 2019/01/06 12:59:45 visa Exp $ */
/* $NetBSD: kern_fork.c,v 1.29 1996/02/09 18:59:34 christos Exp $ */
/*
@@ -211,6 +211,7 @@ process_initialize(struct process *pr, struct proc *p)
LIST_INIT(&pr->ps_sigiolst);
timeout_set(&pr->ps_realit_to, realitexpire, pr);
+ timeout_set(&pr->ps_rucheck_to, rucheck, pr);
}
@@ -240,6 +241,8 @@ process_new(struct proc *p, struct process *parent, int flags)
/* post-copy fixups */
pr->ps_pptr = parent;
pr->ps_limit->p_refcnt++;
+ if (pr->ps_limit->pl_rlimit[RLIMIT_CPU].rlim_cur != RLIM_INFINITY)
+ timeout_add_msec(&pr->ps_rucheck_to, RUCHECK_INTERVAL);
/* bump references to the text vnode (for sysctl) */
pr->ps_textvp = parent->ps_textvp;
diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c
index 184c538d2ef..e80439fdb6b 100644
--- a/sys/kern/kern_resource.c
+++ b/sys/kern/kern_resource.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_resource.c,v 1.58 2018/02/19 08:59:52 mpi Exp $ */
+/* $OpenBSD: kern_resource.c,v 1.59 2019/01/06 12:59:45 visa Exp $ */
/* $NetBSD: kern_resource.c,v 1.38 1996/10/23 07:19:38 matthias Exp $ */
/*-
@@ -46,6 +46,7 @@
#include <sys/proc.h>
#include <sys/ktrace.h>
#include <sys/sched.h>
+#include <sys/signalvar.h>
#include <sys/mount.h>
#include <sys/syscallargs.h>
@@ -267,6 +268,10 @@ dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
if (limp->rlim_cur > limp->rlim_max)
limp->rlim_cur = limp->rlim_max;
+ if (which == RLIMIT_CPU && limp->rlim_cur != RLIM_INFINITY &&
+ alimp->rlim_cur == RLIM_INFINITY)
+ timeout_add_msec(&p->p_p->ps_rucheck_to, RUCHECK_INTERVAL);
+
if (which == RLIMIT_STACK) {
/*
* Stack is allocated to the max at exec time with only
@@ -492,6 +497,39 @@ ruadd(struct rusage *ru, struct rusage *ru2)
*ip++ += *ip2++;
}
+/*
+ * Check if the process exceeds its cpu resource allocation.
+ * If over max, kill it.
+ */
+void
+rucheck(void *arg)
+{
+ struct process *pr = arg;
+ struct rlimit *rlim;
+ rlim_t runtime;
+ int s;
+
+ KERNEL_ASSERT_LOCKED();
+
+ SCHED_LOCK(s);
+ runtime = pr->ps_tu.tu_runtime.tv_sec;
+ SCHED_UNLOCK(s);
+
+ rlim = &pr->ps_limit->pl_rlimit[RLIMIT_CPU];
+ if (runtime >= rlim->rlim_cur) {
+ if (runtime >= rlim->rlim_max) {
+ prsignal(pr, SIGKILL);
+ } else {
+ prsignal(pr, SIGXCPU);
+ if (rlim->rlim_cur < rlim->rlim_max)
+ rlim->rlim_cur = MIN(rlim->rlim_cur + 5,
+ rlim->rlim_max);
+ }
+ }
+
+ timeout_add_msec(&pr->ps_rucheck_to, RUCHECK_INTERVAL);
+}
+
struct pool plimit_pool;
/*
diff --git a/sys/kern/sched_bsd.c b/sys/kern/sched_bsd.c
index b4de163a296..1fa5d319988 100644
--- a/sys/kern/sched_bsd.c
+++ b/sys/kern/sched_bsd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sched_bsd.c,v 1.47 2017/12/04 09:38:20 mpi Exp $ */
+/* $OpenBSD: sched_bsd.c,v 1.48 2019/01/06 12:59:45 visa Exp $ */
/* $NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $ */
/*-
@@ -336,8 +336,6 @@ mi_switch(void)
struct proc *p = curproc;
struct proc *nextproc;
struct process *pr = p->p_p;
- struct rlimit *rlim;
- rlim_t secs;
struct timespec ts;
#ifdef MULTIPROCESSOR
int hold_count;
@@ -382,22 +380,6 @@ mi_switch(void)
tuagg_unlocked(pr, p);
/*
- * Check if the process exceeds its cpu resource allocation.
- * If over max, kill it.
- */
- rlim = &pr->ps_limit->pl_rlimit[RLIMIT_CPU];
- secs = pr->ps_tu.tu_runtime.tv_sec;
- if (secs >= rlim->rlim_cur) {
- if (secs >= rlim->rlim_max) {
- psignal(p, SIGKILL);
- } else {
- psignal(p, SIGXCPU);
- if (rlim->rlim_cur < rlim->rlim_max)
- rlim->rlim_cur += 5;
- }
- }
-
- /*
* Process is about to yield the CPU; clear the appropriate
* scheduling flags.
*/
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index 1273ee2910a..d97ef01c044 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: proc.h,v 1.262 2019/01/03 21:52:31 beck Exp $ */
+/* $OpenBSD: proc.h,v 1.263 2019/01/06 12:59:45 visa Exp $ */
/* $NetBSD: proc.h,v 1.44 1996/04/22 01:23:21 christos Exp $ */
/*-
@@ -202,6 +202,7 @@ struct process {
struct tusage ps_tu; /* accumulated times. */
struct rusage ps_cru; /* sum of stats for reaped children */
struct itimerval ps_timer[3]; /* timers, indexed by ITIMER_* */
+ struct timeout ps_rucheck_to; /* resource limit check timer */
u_int64_t ps_wxcounter;
diff --git a/sys/sys/resourcevar.h b/sys/sys/resourcevar.h
index 83449e1d738..6329aaa346f 100644
--- a/sys/sys/resourcevar.h
+++ b/sys/sys/resourcevar.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: resourcevar.h,v 1.18 2014/01/24 04:26:51 guenther Exp $ */
+/* $OpenBSD: resourcevar.h,v 1.19 2019/01/06 12:59:45 visa Exp $ */
/* $NetBSD: resourcevar.h,v 1.12 1995/11/22 23:01:53 cgd Exp $ */
/*
@@ -69,5 +69,7 @@ struct plimit *limcopy(struct plimit *);
void limfree(struct plimit *);
void ruadd(struct rusage *, struct rusage *);
+void rucheck(void *);
+#define RUCHECK_INTERVAL 1000 /* check interval in msec */
#endif
#endif /* !_SYS_RESOURCEVAR_H_ */