kern_racct.c revision 290857
1220137Strasz/*-
2220137Strasz * Copyright (c) 2010 The FreeBSD Foundation
3220137Strasz * All rights reserved.
4220137Strasz *
5220137Strasz * This software was developed by Edward Tomasz Napierala under sponsorship
6220137Strasz * from the FreeBSD Foundation.
7220137Strasz *
8220137Strasz * Redistribution and use in source and binary forms, with or without
9220137Strasz * modification, are permitted provided that the following conditions
10220137Strasz * are met:
11220137Strasz * 1. Redistributions of source code must retain the above copyright
12220137Strasz *    notice, this list of conditions and the following disclaimer.
13220137Strasz * 2. Redistributions in binary form must reproduce the above copyright
14220137Strasz *    notice, this list of conditions and the following disclaimer in the
15220137Strasz *    documentation and/or other materials provided with the distribution.
16220137Strasz *
17220137Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18220137Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19220137Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20220137Strasz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21220137Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22220137Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23220137Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24220137Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25220137Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26220137Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27220137Strasz * SUCH DAMAGE.
28220137Strasz *
29220137Strasz * $FreeBSD: head/sys/kern/kern_racct.c 290857 2015-11-15 12:10:51Z trasz $
30220137Strasz */
31220137Strasz
32220137Strasz#include <sys/cdefs.h>
33220137Strasz__FBSDID("$FreeBSD: head/sys/kern/kern_racct.c 290857 2015-11-15 12:10:51Z trasz $");
34220137Strasz
35242139Strasz#include "opt_sched.h"
36220137Strasz
37220137Strasz#include <sys/param.h>
38228430Savg#include <sys/systm.h>
39220137Strasz#include <sys/eventhandler.h>
40220137Strasz#include <sys/jail.h>
41220137Strasz#include <sys/kernel.h>
42220137Strasz#include <sys/kthread.h>
43220137Strasz#include <sys/lock.h>
44220137Strasz#include <sys/loginclass.h>
45220137Strasz#include <sys/malloc.h>
46220137Strasz#include <sys/mutex.h>
47220137Strasz#include <sys/proc.h>
48220137Strasz#include <sys/racct.h>
49220137Strasz#include <sys/resourcevar.h>
50220137Strasz#include <sys/sbuf.h>
51220137Strasz#include <sys/sched.h>
52220137Strasz#include <sys/sdt.h>
53242139Strasz#include <sys/smp.h>
54220137Strasz#include <sys/sx.h>
55242139Strasz#include <sys/sysctl.h>
56220137Strasz#include <sys/sysent.h>
57220137Strasz#include <sys/sysproto.h>
58220137Strasz#include <sys/umtx.h>
59242139Strasz#include <machine/smp.h>
60220137Strasz
61220137Strasz#ifdef RCTL
62220137Strasz#include <sys/rctl.h>
63220137Strasz#endif
64220137Strasz
65220137Strasz#ifdef RACCT
66220137Strasz
67220137StraszFEATURE(racct, "Resource Accounting");
68220137Strasz
69242139Strasz/*
70242139Strasz * Do not block processes that have their %cpu usage <= pcpu_threshold.
71242139Strasz */
72242139Straszstatic int pcpu_threshold = 1;
73282901Strasz#ifdef RACCT_DEFAULT_TO_DISABLED
74282213Straszint racct_enable = 0;
75282213Strasz#else
76282213Straszint racct_enable = 1;
77282213Strasz#endif
78242139Strasz
79242139StraszSYSCTL_NODE(_kern, OID_AUTO, racct, CTLFLAG_RW, 0, "Resource Accounting");
80282213StraszSYSCTL_UINT(_kern_racct, OID_AUTO, enable, CTLFLAG_RDTUN, &racct_enable,
81282213Strasz    0, "Enable RACCT/RCTL");
82242139StraszSYSCTL_UINT(_kern_racct, OID_AUTO, pcpu_threshold, CTLFLAG_RW, &pcpu_threshold,
83242139Strasz    0, "Processes with higher %cpu usage than this value can be throttled.");
84242139Strasz
85242139Strasz/*
86242139Strasz * How many seconds it takes to use the scheduler %cpu calculations.  When a
87242139Strasz * process starts, we compute its %cpu usage by dividing its runtime by the
88242139Strasz * process wall clock time.  After RACCT_PCPU_SECS pass, we use the value
89242139Strasz * provided by the scheduler.
90242139Strasz */
91242139Strasz#define RACCT_PCPU_SECS		3
92242139Strasz
93220137Straszstatic struct mtx racct_lock;
94220137StraszMTX_SYSINIT(racct_lock, &racct_lock, "racct lock", MTX_DEF);
95220137Strasz
96220137Straszstatic uma_zone_t racct_zone;
97220137Strasz
98220137Straszstatic void racct_sub_racct(struct racct *dest, const struct racct *src);
99220137Straszstatic void racct_sub_cred_locked(struct ucred *cred, int resource,
100220137Strasz		uint64_t amount);
101220137Straszstatic void racct_add_cred_locked(struct ucred *cred, int resource,
102220137Strasz		uint64_t amount);
103220137Strasz
104220137StraszSDT_PROVIDER_DEFINE(racct);
105258622SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, add, "struct proc *", "int",
106220137Strasz    "uint64_t");
107258622SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, add__failure,
108220137Strasz    "struct proc *", "int", "uint64_t");
109258622SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, add__cred, "struct ucred *",
110220137Strasz    "int", "uint64_t");
111258622SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, add__force, "struct proc *",
112220137Strasz    "int", "uint64_t");
113258622SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, set, "struct proc *", "int",
114220137Strasz    "uint64_t");
115258622SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, set__failure,
116220137Strasz    "struct proc *", "int", "uint64_t");
117258622SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, sub, "struct proc *", "int",
118220137Strasz    "uint64_t");
119258622SavgSDT_PROBE_DEFINE3(racct, kernel, rusage, sub__cred, "struct ucred *",
120220137Strasz    "int", "uint64_t");
121258622SavgSDT_PROBE_DEFINE1(racct, kernel, racct, create, "struct racct *");
122258622SavgSDT_PROBE_DEFINE1(racct, kernel, racct, destroy, "struct racct *");
123258622SavgSDT_PROBE_DEFINE2(racct, kernel, racct, join, "struct racct *",
124220137Strasz    "struct racct *");
125258622SavgSDT_PROBE_DEFINE2(racct, kernel, racct, join__failure,
126220137Strasz    "struct racct *", "struct racct *");
127258622SavgSDT_PROBE_DEFINE2(racct, kernel, racct, leave, "struct racct *",
128220137Strasz    "struct racct *");
129220137Strasz
130220137Straszint racct_types[] = {
131220137Strasz	[RACCT_CPU] =
132224036Strasz		RACCT_IN_MILLIONS,
133220137Strasz	[RACCT_DATA] =
134220137Strasz		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
135220137Strasz	[RACCT_STACK] =
136220137Strasz		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
137220137Strasz	[RACCT_CORE] =
138220137Strasz		RACCT_DENIABLE,
139220137Strasz	[RACCT_RSS] =
140220137Strasz		RACCT_RECLAIMABLE,
141220137Strasz	[RACCT_MEMLOCK] =
142220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE,
143220137Strasz	[RACCT_NPROC] =
144220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE,
145220137Strasz	[RACCT_NOFILE] =
146220137Strasz		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
147220137Strasz	[RACCT_VMEM] =
148220137Strasz		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
149220137Strasz	[RACCT_NPTS] =
150220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
151220137Strasz	[RACCT_SWAP] =
152220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
153220137Strasz	[RACCT_NTHR] =
154220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE,
155220137Strasz	[RACCT_MSGQQUEUED] =
156220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
157220137Strasz	[RACCT_MSGQSIZE] =
158220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
159220137Strasz	[RACCT_NMSGQ] =
160220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
161220137Strasz	[RACCT_NSEM] =
162220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
163220137Strasz	[RACCT_NSEMOP] =
164220137Strasz		RACCT_RECLAIMABLE | RACCT_INHERITABLE | RACCT_DENIABLE,
165220137Strasz	[RACCT_NSHM] =
166220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
167220137Strasz	[RACCT_SHMSIZE] =
168220137Strasz		RACCT_RECLAIMABLE | RACCT_DENIABLE | RACCT_SLOPPY,
169220137Strasz	[RACCT_WALLCLOCK] =
170242139Strasz		RACCT_IN_MILLIONS,
171242139Strasz	[RACCT_PCTCPU] =
172242139Strasz		RACCT_DECAYING | RACCT_DENIABLE | RACCT_IN_MILLIONS };
173220137Strasz
174242139Straszstatic const fixpt_t RACCT_DECAY_FACTOR = 0.3 * FSCALE;
175242139Strasz
176242139Strasz#ifdef SCHED_4BSD
177242139Strasz/*
178242139Strasz * Contains intermediate values for %cpu calculations to avoid using floating
179242139Strasz * point in the kernel.
180242139Strasz * ccpu_exp[k] = FSCALE * (ccpu/FSCALE)^k = FSCALE * exp(-k/20)
181242139Strasz * It is needed only for the 4BSD scheduler, because in ULE, the ccpu equals to
182242139Strasz * zero so the calculations are more straightforward.
183242139Strasz */
184242139Straszfixpt_t ccpu_exp[] = {
185242139Strasz	[0] = FSCALE * 1,
186242139Strasz	[1] = FSCALE * 0.95122942450071400909,
187242139Strasz	[2] = FSCALE * 0.90483741803595957316,
188242139Strasz	[3] = FSCALE * 0.86070797642505780722,
189242139Strasz	[4] = FSCALE * 0.81873075307798185866,
190242139Strasz	[5] = FSCALE * 0.77880078307140486824,
191242139Strasz	[6] = FSCALE * 0.74081822068171786606,
192242139Strasz	[7] = FSCALE * 0.70468808971871343435,
193242139Strasz	[8] = FSCALE * 0.67032004603563930074,
194242139Strasz	[9] = FSCALE * 0.63762815162177329314,
195242139Strasz	[10] = FSCALE * 0.60653065971263342360,
196242139Strasz	[11] = FSCALE * 0.57694981038048669531,
197242139Strasz	[12] = FSCALE * 0.54881163609402643262,
198242139Strasz	[13] = FSCALE * 0.52204577676101604789,
199242139Strasz	[14] = FSCALE * 0.49658530379140951470,
200242139Strasz	[15] = FSCALE * 0.47236655274101470713,
201242139Strasz	[16] = FSCALE * 0.44932896411722159143,
202242139Strasz	[17] = FSCALE * 0.42741493194872666992,
203242139Strasz	[18] = FSCALE * 0.40656965974059911188,
204242139Strasz	[19] = FSCALE * 0.38674102345450120691,
205242139Strasz	[20] = FSCALE * 0.36787944117144232159,
206242139Strasz	[21] = FSCALE * 0.34993774911115535467,
207242139Strasz	[22] = FSCALE * 0.33287108369807955328,
208242139Strasz	[23] = FSCALE * 0.31663676937905321821,
209242139Strasz	[24] = FSCALE * 0.30119421191220209664,
210242139Strasz	[25] = FSCALE * 0.28650479686019010032,
211242139Strasz	[26] = FSCALE * 0.27253179303401260312,
212242139Strasz	[27] = FSCALE * 0.25924026064589150757,
213242139Strasz	[28] = FSCALE * 0.24659696394160647693,
214242139Strasz	[29] = FSCALE * 0.23457028809379765313,
215242139Strasz	[30] = FSCALE * 0.22313016014842982893,
216242139Strasz	[31] = FSCALE * 0.21224797382674305771,
217242139Strasz	[32] = FSCALE * 0.20189651799465540848,
218242139Strasz	[33] = FSCALE * 0.19204990862075411423,
219242139Strasz	[34] = FSCALE * 0.18268352405273465022,
220242139Strasz	[35] = FSCALE * 0.17377394345044512668,
221242139Strasz	[36] = FSCALE * 0.16529888822158653829,
222242139Strasz	[37] = FSCALE * 0.15723716631362761621,
223242139Strasz	[38] = FSCALE * 0.14956861922263505264,
224242139Strasz	[39] = FSCALE * 0.14227407158651357185,
225242139Strasz	[40] = FSCALE * 0.13533528323661269189,
226242139Strasz	[41] = FSCALE * 0.12873490358780421886,
227242139Strasz	[42] = FSCALE * 0.12245642825298191021,
228242139Strasz	[43] = FSCALE * 0.11648415777349695786,
229242139Strasz	[44] = FSCALE * 0.11080315836233388333,
230242139Strasz	[45] = FSCALE * 0.10539922456186433678,
231242139Strasz	[46] = FSCALE * 0.10025884372280373372,
232242139Strasz	[47] = FSCALE * 0.09536916221554961888,
233242139Strasz	[48] = FSCALE * 0.09071795328941250337,
234242139Strasz	[49] = FSCALE * 0.08629358649937051097,
235242139Strasz	[50] = FSCALE * 0.08208499862389879516,
236242139Strasz	[51] = FSCALE * 0.07808166600115315231,
237242139Strasz	[52] = FSCALE * 0.07427357821433388042,
238242139Strasz	[53] = FSCALE * 0.07065121306042958674,
239242139Strasz	[54] = FSCALE * 0.06720551273974976512,
240242139Strasz	[55] = FSCALE * 0.06392786120670757270,
241242139Strasz	[56] = FSCALE * 0.06081006262521796499,
242242139Strasz	[57] = FSCALE * 0.05784432087483846296,
243242139Strasz	[58] = FSCALE * 0.05502322005640722902,
244242139Strasz	[59] = FSCALE * 0.05233970594843239308,
245242139Strasz	[60] = FSCALE * 0.04978706836786394297,
246242139Strasz	[61] = FSCALE * 0.04735892439114092119,
247242139Strasz	[62] = FSCALE * 0.04504920239355780606,
248242139Strasz	[63] = FSCALE * 0.04285212686704017991,
249242139Strasz	[64] = FSCALE * 0.04076220397836621516,
250242139Strasz	[65] = FSCALE * 0.03877420783172200988,
251242139Strasz	[66] = FSCALE * 0.03688316740124000544,
252242139Strasz	[67] = FSCALE * 0.03508435410084502588,
253242139Strasz	[68] = FSCALE * 0.03337326996032607948,
254242139Strasz	[69] = FSCALE * 0.03174563637806794323,
255242139Strasz	[70] = FSCALE * 0.03019738342231850073,
256242139Strasz	[71] = FSCALE * 0.02872463965423942912,
257242139Strasz	[72] = FSCALE * 0.02732372244729256080,
258242139Strasz	[73] = FSCALE * 0.02599112877875534358,
259242139Strasz	[74] = FSCALE * 0.02472352647033939120,
260242139Strasz	[75] = FSCALE * 0.02351774585600910823,
261242139Strasz	[76] = FSCALE * 0.02237077185616559577,
262242139Strasz	[77] = FSCALE * 0.02127973643837716938,
263242139Strasz	[78] = FSCALE * 0.02024191144580438847,
264242139Strasz	[79] = FSCALE * 0.01925470177538692429,
265242139Strasz	[80] = FSCALE * 0.01831563888873418029,
266242139Strasz	[81] = FSCALE * 0.01742237463949351138,
267242139Strasz	[82] = FSCALE * 0.01657267540176124754,
268242139Strasz	[83] = FSCALE * 0.01576441648485449082,
269242139Strasz	[84] = FSCALE * 0.01499557682047770621,
270242139Strasz	[85] = FSCALE * 0.01426423390899925527,
271242139Strasz	[86] = FSCALE * 0.01356855901220093175,
272242139Strasz	[87] = FSCALE * 0.01290681258047986886,
273242139Strasz	[88] = FSCALE * 0.01227733990306844117,
274242139Strasz	[89] = FSCALE * 0.01167856697039544521,
275242139Strasz	[90] = FSCALE * 0.01110899653824230649,
276242139Strasz	[91] = FSCALE * 0.01056720438385265337,
277242139Strasz	[92] = FSCALE * 0.01005183574463358164,
278242139Strasz	[93] = FSCALE * 0.00956160193054350793,
279242139Strasz	[94] = FSCALE * 0.00909527710169581709,
280242139Strasz	[95] = FSCALE * 0.00865169520312063417,
281242139Strasz	[96] = FSCALE * 0.00822974704902002884,
282242139Strasz	[97] = FSCALE * 0.00782837754922577143,
283242139Strasz	[98] = FSCALE * 0.00744658307092434051,
284242139Strasz	[99] = FSCALE * 0.00708340892905212004,
285242139Strasz	[100] = FSCALE * 0.00673794699908546709,
286242139Strasz	[101] = FSCALE * 0.00640933344625638184,
287242139Strasz	[102] = FSCALE * 0.00609674656551563610,
288242139Strasz	[103] = FSCALE * 0.00579940472684214321,
289242139Strasz	[104] = FSCALE * 0.00551656442076077241,
290242139Strasz	[105] = FSCALE * 0.00524751839918138427,
291242139Strasz	[106] = FSCALE * 0.00499159390691021621,
292242139Strasz	[107] = FSCALE * 0.00474815099941147558,
293242139Strasz	[108] = FSCALE * 0.00451658094261266798,
294242139Strasz	[109] = FSCALE * 0.00429630469075234057,
295242139Strasz	[110] = FSCALE * 0.00408677143846406699,
296242139Strasz};
297242139Strasz#endif
298242139Strasz
299242139Strasz#define	CCPU_EXP_MAX	110
300242139Strasz
301242139Strasz/*
302242139Strasz * This function is analogical to the getpcpu() function in the ps(1) command.
303242139Strasz * They should both calculate in the same way so that the racct %cpu
304242139Strasz * calculations are consistent with the values showed by the ps(1) tool.
305242139Strasz * The calculations are more complex in the 4BSD scheduler because of the value
306242139Strasz * of the ccpu variable.  In ULE it is defined to be zero which saves us some
307242139Strasz * work.
308242139Strasz */
309242139Straszstatic uint64_t
310242139Straszracct_getpcpu(struct proc *p, u_int pcpu)
311242139Strasz{
312242139Strasz	u_int swtime;
313242139Strasz#ifdef SCHED_4BSD
314242139Strasz	fixpt_t pctcpu, pctcpu_next;
315242139Strasz#endif
316242139Strasz#ifdef SMP
317242139Strasz	struct pcpu *pc;
318242139Strasz	int found;
319242139Strasz#endif
320242139Strasz	fixpt_t p_pctcpu;
321242139Strasz	struct thread *td;
322242139Strasz
323282213Strasz	ASSERT_RACCT_ENABLED();
324282213Strasz
325242139Strasz	/*
326242139Strasz	 * If the process is swapped out, we count its %cpu usage as zero.
327242139Strasz	 * This behaviour is consistent with the userland ps(1) tool.
328242139Strasz	 */
329242139Strasz	if ((p->p_flag & P_INMEM) == 0)
330242139Strasz		return (0);
331242139Strasz	swtime = (ticks - p->p_swtick) / hz;
332242139Strasz
333242139Strasz	/*
334242139Strasz	 * For short-lived processes, the sched_pctcpu() returns small
335242139Strasz	 * values even for cpu intensive processes.  Therefore we use
336242139Strasz	 * our own estimate in this case.
337242139Strasz	 */
338242139Strasz	if (swtime < RACCT_PCPU_SECS)
339242139Strasz		return (pcpu);
340242139Strasz
341242139Strasz	p_pctcpu = 0;
342242139Strasz	FOREACH_THREAD_IN_PROC(p, td) {
343242139Strasz		if (td == PCPU_GET(idlethread))
344242139Strasz			continue;
345242139Strasz#ifdef SMP
346242139Strasz		found = 0;
347242139Strasz		STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
348242139Strasz			if (td == pc->pc_idlethread) {
349242139Strasz				found = 1;
350242139Strasz				break;
351242139Strasz			}
352242139Strasz		}
353242139Strasz		if (found)
354242139Strasz			continue;
355242139Strasz#endif
356242139Strasz		thread_lock(td);
357242139Strasz#ifdef SCHED_4BSD
358242139Strasz		pctcpu = sched_pctcpu(td);
359242139Strasz		/* Count also the yet unfinished second. */
360242139Strasz		pctcpu_next = (pctcpu * ccpu_exp[1]) >> FSHIFT;
361242139Strasz		pctcpu_next += sched_pctcpu_delta(td);
362242139Strasz		p_pctcpu += max(pctcpu, pctcpu_next);
363242139Strasz#else
364242139Strasz		/*
365242139Strasz		 * In ULE the %cpu statistics are updated on every
366242139Strasz		 * sched_pctcpu() call.  So special calculations to
367242139Strasz		 * account for the latest (unfinished) second are
368242139Strasz		 * not needed.
369242139Strasz		 */
370242139Strasz		p_pctcpu += sched_pctcpu(td);
371242139Strasz#endif
372242139Strasz		thread_unlock(td);
373242139Strasz	}
374242139Strasz
375242139Strasz#ifdef SCHED_4BSD
376242139Strasz	if (swtime <= CCPU_EXP_MAX)
377242139Strasz		return ((100 * (uint64_t)p_pctcpu * 1000000) /
378242139Strasz		    (FSCALE - ccpu_exp[swtime]));
379242139Strasz#endif
380242139Strasz
381242139Strasz	return ((100 * (uint64_t)p_pctcpu * 1000000) / FSCALE);
382242139Strasz}
383242139Strasz
384220137Straszstatic void
385220137Straszracct_add_racct(struct racct *dest, const struct racct *src)
386220137Strasz{
387220137Strasz	int i;
388220137Strasz
389282213Strasz	ASSERT_RACCT_ENABLED();
390220137Strasz	mtx_assert(&racct_lock, MA_OWNED);
391220137Strasz
392220137Strasz	/*
393220137Strasz	 * Update resource usage in dest.
394220137Strasz	 */
395220137Strasz	for (i = 0; i <= RACCT_MAX; i++) {
396220137Strasz		KASSERT(dest->r_resources[i] >= 0,
397243088Strasz		    ("%s: resource %d propagation meltdown: dest < 0",
398243088Strasz		    __func__, i));
399220137Strasz		KASSERT(src->r_resources[i] >= 0,
400243088Strasz		    ("%s: resource %d propagation meltdown: src < 0",
401243088Strasz		    __func__, i));
402220137Strasz		dest->r_resources[i] += src->r_resources[i];
403220137Strasz	}
404220137Strasz}
405220137Strasz
406220137Straszstatic void
407220137Straszracct_sub_racct(struct racct *dest, const struct racct *src)
408220137Strasz{
409220137Strasz	int i;
410220137Strasz
411282213Strasz	ASSERT_RACCT_ENABLED();
412220137Strasz	mtx_assert(&racct_lock, MA_OWNED);
413220137Strasz
414220137Strasz	/*
415220137Strasz	 * Update resource usage in dest.
416220137Strasz	 */
417220137Strasz	for (i = 0; i <= RACCT_MAX; i++) {
418243070Strasz		if (!RACCT_IS_SLOPPY(i) && !RACCT_IS_DECAYING(i)) {
419220137Strasz			KASSERT(dest->r_resources[i] >= 0,
420243088Strasz			    ("%s: resource %d propagation meltdown: dest < 0",
421243088Strasz			    __func__, i));
422220137Strasz			KASSERT(src->r_resources[i] >= 0,
423243088Strasz			    ("%s: resource %d propagation meltdown: src < 0",
424243088Strasz			    __func__, i));
425220137Strasz			KASSERT(src->r_resources[i] <= dest->r_resources[i],
426243088Strasz			    ("%s: resource %d propagation meltdown: src > dest",
427243088Strasz			    __func__, i));
428220137Strasz		}
429242139Strasz		if (RACCT_CAN_DROP(i)) {
430220137Strasz			dest->r_resources[i] -= src->r_resources[i];
431220137Strasz			if (dest->r_resources[i] < 0) {
432243070Strasz				KASSERT(RACCT_IS_SLOPPY(i) ||
433243070Strasz				    RACCT_IS_DECAYING(i),
434243088Strasz				    ("%s: resource %d usage < 0", __func__, i));
435220137Strasz				dest->r_resources[i] = 0;
436220137Strasz			}
437220137Strasz		}
438220137Strasz	}
439220137Strasz}
440220137Strasz
441220137Straszvoid
442220137Straszracct_create(struct racct **racctp)
443220137Strasz{
444220137Strasz
445282213Strasz	if (!racct_enable)
446282213Strasz		return;
447282213Strasz
448288336Savg	SDT_PROBE1(racct, kernel, racct, create, racctp);
449220137Strasz
450220137Strasz	KASSERT(*racctp == NULL, ("racct already allocated"));
451220137Strasz
452220137Strasz	*racctp = uma_zalloc(racct_zone, M_WAITOK | M_ZERO);
453220137Strasz}
454220137Strasz
455220137Straszstatic void
456220137Straszracct_destroy_locked(struct racct **racctp)
457220137Strasz{
458220137Strasz	int i;
459220137Strasz	struct racct *racct;
460220137Strasz
461282213Strasz	ASSERT_RACCT_ENABLED();
462282213Strasz
463288336Savg	SDT_PROBE1(racct, kernel, racct, destroy, racctp);
464220137Strasz
465220137Strasz	mtx_assert(&racct_lock, MA_OWNED);
466220137Strasz	KASSERT(racctp != NULL, ("NULL racctp"));
467220137Strasz	KASSERT(*racctp != NULL, ("NULL racct"));
468220137Strasz
469220137Strasz	racct = *racctp;
470220137Strasz
471220137Strasz	for (i = 0; i <= RACCT_MAX; i++) {
472223844Strasz		if (RACCT_IS_SLOPPY(i))
473220137Strasz			continue;
474223844Strasz		if (!RACCT_IS_RECLAIMABLE(i))
475220137Strasz			continue;
476220137Strasz		KASSERT(racct->r_resources[i] == 0,
477220137Strasz		    ("destroying non-empty racct: "
478220137Strasz		    "%ju allocated for resource %d\n",
479220137Strasz		    racct->r_resources[i], i));
480220137Strasz	}
481220137Strasz	uma_zfree(racct_zone, racct);
482220137Strasz	*racctp = NULL;
483220137Strasz}
484220137Strasz
485220137Straszvoid
486220137Straszracct_destroy(struct racct **racct)
487220137Strasz{
488220137Strasz
489282213Strasz	if (!racct_enable)
490282213Strasz		return;
491282213Strasz
492220137Strasz	mtx_lock(&racct_lock);
493220137Strasz	racct_destroy_locked(racct);
494220137Strasz	mtx_unlock(&racct_lock);
495220137Strasz}
496220137Strasz
497220137Strasz/*
498220137Strasz * Increase consumption of 'resource' by 'amount' for 'racct'
499220137Strasz * and all its parents.  Differently from other cases, 'amount' here
500220137Strasz * may be less than zero.
501220137Strasz */
502220137Straszstatic void
503284378Sjlhracct_adjust_resource(struct racct *racct, int resource,
504220137Strasz    uint64_t amount)
505220137Strasz{
506220137Strasz
507282213Strasz	ASSERT_RACCT_ENABLED();
508220137Strasz	mtx_assert(&racct_lock, MA_OWNED);
509220137Strasz	KASSERT(racct != NULL, ("NULL racct"));
510220137Strasz
511220137Strasz	racct->r_resources[resource] += amount;
512220137Strasz	if (racct->r_resources[resource] < 0) {
513242139Strasz		KASSERT(RACCT_IS_SLOPPY(resource) || RACCT_IS_DECAYING(resource),
514243088Strasz		    ("%s: resource %d usage < 0", __func__, resource));
515220137Strasz		racct->r_resources[resource] = 0;
516220137Strasz	}
517242139Strasz
518242139Strasz	/*
519242139Strasz	 * There are some cases where the racct %cpu resource would grow
520290662Sjpaetzel	 * beyond 100% per core.  For example in racct_proc_exit() we add
521290662Sjpaetzel	 * the process %cpu usage to the ucred racct containers.  If too
522290662Sjpaetzel	 * many processes terminated in a short time span, the ucred %cpu
523290662Sjpaetzel	 * resource could grow too much.  Also, the 4BSD scheduler sometimes
524290662Sjpaetzel	 * returns for a thread more than 100% cpu usage. So we set a sane
525290662Sjpaetzel	 * boundary here to 100% * the maxumum number of CPUs.
526242139Strasz	 */
527242139Strasz	if ((resource == RACCT_PCTCPU) &&
528290662Sjpaetzel	    (racct->r_resources[RACCT_PCTCPU] > 100 * 1000000 * (int64_t)MAXCPU))
529290662Sjpaetzel		racct->r_resources[RACCT_PCTCPU] = 100 * 1000000 * (int64_t)MAXCPU;
530220137Strasz}
531220137Strasz
532225944Straszstatic int
533225944Straszracct_add_locked(struct proc *p, int resource, uint64_t amount)
534220137Strasz{
535220137Strasz#ifdef RCTL
536220137Strasz	int error;
537220137Strasz#endif
538220137Strasz
539282213Strasz	ASSERT_RACCT_ENABLED();
540282213Strasz
541288336Savg	SDT_PROBE3(racct, kernel, rusage, add, p, resource, amount);
542220137Strasz
543220137Strasz	/*
544220137Strasz	 * We need proc lock to dereference p->p_ucred.
545220137Strasz	 */
546220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
547220137Strasz
548220137Strasz#ifdef RCTL
549220137Strasz	error = rctl_enforce(p, resource, amount);
550223844Strasz	if (error && RACCT_IS_DENIABLE(resource)) {
551288336Savg		SDT_PROBE3(racct, kernel, rusage, add__failure, p, resource,
552288336Savg		    amount);
553220137Strasz		return (error);
554220137Strasz	}
555220137Strasz#endif
556284378Sjlh	racct_adjust_resource(p->p_racct, resource, amount);
557220137Strasz	racct_add_cred_locked(p->p_ucred, resource, amount);
558220137Strasz
559220137Strasz	return (0);
560220137Strasz}
561220137Strasz
562225944Strasz/*
563225944Strasz * Increase allocation of 'resource' by 'amount' for process 'p'.
564225944Strasz * Return 0 if it's below limits, or errno, if it's not.
565225944Strasz */
566225944Straszint
567225944Straszracct_add(struct proc *p, int resource, uint64_t amount)
568225944Strasz{
569225944Strasz	int error;
570225944Strasz
571282213Strasz	if (!racct_enable)
572282213Strasz		return (0);
573282213Strasz
574225944Strasz	mtx_lock(&racct_lock);
575225944Strasz	error = racct_add_locked(p, resource, amount);
576225944Strasz	mtx_unlock(&racct_lock);
577225944Strasz	return (error);
578225944Strasz}
579225944Strasz
580220137Straszstatic void
581220137Straszracct_add_cred_locked(struct ucred *cred, int resource, uint64_t amount)
582220137Strasz{
583220137Strasz	struct prison *pr;
584220137Strasz
585282213Strasz	ASSERT_RACCT_ENABLED();
586282213Strasz
587288336Savg	SDT_PROBE3(racct, kernel, rusage, add__cred, cred, resource, amount);
588220137Strasz
589284378Sjlh	racct_adjust_resource(cred->cr_ruidinfo->ui_racct, resource, amount);
590220137Strasz	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
591284378Sjlh		racct_adjust_resource(pr->pr_prison_racct->prr_racct, resource,
592221362Strasz		    amount);
593284378Sjlh	racct_adjust_resource(cred->cr_loginclass->lc_racct, resource, amount);
594220137Strasz}
595220137Strasz
596220137Strasz/*
597220137Strasz * Increase allocation of 'resource' by 'amount' for credential 'cred'.
598220137Strasz * Doesn't check for limits and never fails.
599220137Strasz *
600220137Strasz * XXX: Shouldn't this ever return an error?
601220137Strasz */
602220137Straszvoid
603220137Straszracct_add_cred(struct ucred *cred, int resource, uint64_t amount)
604220137Strasz{
605220137Strasz
606282213Strasz	if (!racct_enable)
607282213Strasz		return;
608282213Strasz
609220137Strasz	mtx_lock(&racct_lock);
610220137Strasz	racct_add_cred_locked(cred, resource, amount);
611220137Strasz	mtx_unlock(&racct_lock);
612220137Strasz}
613220137Strasz
614220137Strasz/*
615220137Strasz * Increase allocation of 'resource' by 'amount' for process 'p'.
616220137Strasz * Doesn't check for limits and never fails.
617220137Strasz */
618220137Straszvoid
619220137Straszracct_add_force(struct proc *p, int resource, uint64_t amount)
620220137Strasz{
621220137Strasz
622282213Strasz	if (!racct_enable)
623282213Strasz		return;
624282213Strasz
625288336Savg	SDT_PROBE3(racct, kernel, rusage, add__force, p, resource, amount);
626220137Strasz
627220137Strasz	/*
628220137Strasz	 * We need proc lock to dereference p->p_ucred.
629220137Strasz	 */
630220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
631220137Strasz
632220137Strasz	mtx_lock(&racct_lock);
633284378Sjlh	racct_adjust_resource(p->p_racct, resource, amount);
634220137Strasz	mtx_unlock(&racct_lock);
635220137Strasz	racct_add_cred(p->p_ucred, resource, amount);
636220137Strasz}
637220137Strasz
638220137Straszstatic int
639220137Straszracct_set_locked(struct proc *p, int resource, uint64_t amount)
640220137Strasz{
641242139Strasz	int64_t old_amount, decayed_amount;
642242139Strasz	int64_t diff_proc, diff_cred;
643220137Strasz#ifdef RCTL
644220137Strasz	int error;
645220137Strasz#endif
646220137Strasz
647282213Strasz	ASSERT_RACCT_ENABLED();
648282213Strasz
649288336Savg	SDT_PROBE3(racct, kernel, rusage, set, p, resource, amount);
650220137Strasz
651220137Strasz	/*
652220137Strasz	 * We need proc lock to dereference p->p_ucred.
653220137Strasz	 */
654220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
655220137Strasz
656242139Strasz	old_amount = p->p_racct->r_resources[resource];
657242139Strasz	/*
658242139Strasz	 * The diffs may be negative.
659242139Strasz	 */
660242139Strasz	diff_proc = amount - old_amount;
661242139Strasz	if (RACCT_IS_DECAYING(resource)) {
662242139Strasz		/*
663242139Strasz		 * Resources in per-credential racct containers may decay.
664242139Strasz		 * If this is the case, we need to calculate the difference
665242139Strasz		 * between the new amount and the proportional value of the
666242139Strasz		 * old amount that has decayed in the ucred racct containers.
667242139Strasz		 */
668242139Strasz		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
669242139Strasz		diff_cred = amount - decayed_amount;
670242139Strasz	} else
671242139Strasz		diff_cred = diff_proc;
672220137Strasz#ifdef notyet
673242139Strasz	KASSERT(diff_proc >= 0 || RACCT_CAN_DROP(resource),
674243088Strasz	    ("%s: usage of non-droppable resource %d dropping", __func__,
675220137Strasz	     resource));
676220137Strasz#endif
677220137Strasz#ifdef RCTL
678242139Strasz	if (diff_proc > 0) {
679242139Strasz		error = rctl_enforce(p, resource, diff_proc);
680223844Strasz		if (error && RACCT_IS_DENIABLE(resource)) {
681288336Savg			SDT_PROBE3(racct, kernel, rusage, set__failure, p,
682288336Savg			    resource, amount);
683220137Strasz			return (error);
684220137Strasz		}
685220137Strasz	}
686220137Strasz#endif
687284378Sjlh	racct_adjust_resource(p->p_racct, resource, diff_proc);
688242139Strasz	if (diff_cred > 0)
689242139Strasz		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
690242139Strasz	else if (diff_cred < 0)
691242139Strasz		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
692220137Strasz
693220137Strasz	return (0);
694220137Strasz}
695220137Strasz
696220137Strasz/*
697220137Strasz * Set allocation of 'resource' to 'amount' for process 'p'.
698220137Strasz * Return 0 if it's below limits, or errno, if it's not.
699220137Strasz *
700220137Strasz * Note that decreasing the allocation always returns 0,
701220137Strasz * even if it's above the limit.
702220137Strasz */
703220137Straszint
704220137Straszracct_set(struct proc *p, int resource, uint64_t amount)
705220137Strasz{
706220137Strasz	int error;
707220137Strasz
708282213Strasz	if (!racct_enable)
709282213Strasz		return (0);
710282213Strasz
711220137Strasz	mtx_lock(&racct_lock);
712220137Strasz	error = racct_set_locked(p, resource, amount);
713220137Strasz	mtx_unlock(&racct_lock);
714220137Strasz	return (error);
715220137Strasz}
716220137Strasz
717242139Straszstatic void
718242139Straszracct_set_force_locked(struct proc *p, int resource, uint64_t amount)
719220137Strasz{
720242139Strasz	int64_t old_amount, decayed_amount;
721242139Strasz	int64_t diff_proc, diff_cred;
722220137Strasz
723282213Strasz	ASSERT_RACCT_ENABLED();
724282213Strasz
725288336Savg	SDT_PROBE3(racct, kernel, rusage, set, p, resource, amount);
726220137Strasz
727220137Strasz	/*
728220137Strasz	 * We need proc lock to dereference p->p_ucred.
729220137Strasz	 */
730220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
731220137Strasz
732242139Strasz	old_amount = p->p_racct->r_resources[resource];
733242139Strasz	/*
734242139Strasz	 * The diffs may be negative.
735242139Strasz	 */
736242139Strasz	diff_proc = amount - old_amount;
737242139Strasz	if (RACCT_IS_DECAYING(resource)) {
738242139Strasz		/*
739242139Strasz		 * Resources in per-credential racct containers may decay.
740242139Strasz		 * If this is the case, we need to calculate the difference
741242139Strasz		 * between the new amount and the proportional value of the
742242139Strasz		 * old amount that has decayed in the ucred racct containers.
743242139Strasz		 */
744242139Strasz		decayed_amount = old_amount * RACCT_DECAY_FACTOR / FSCALE;
745242139Strasz		diff_cred = amount - decayed_amount;
746242139Strasz	} else
747242139Strasz		diff_cred = diff_proc;
748242139Strasz
749284378Sjlh	racct_adjust_resource(p->p_racct, resource, diff_proc);
750242139Strasz	if (diff_cred > 0)
751242139Strasz		racct_add_cred_locked(p->p_ucred, resource, diff_cred);
752242139Strasz	else if (diff_cred < 0)
753242139Strasz		racct_sub_cred_locked(p->p_ucred, resource, -diff_cred);
754242139Strasz}
755242139Strasz
756242139Straszvoid
757242139Straszracct_set_force(struct proc *p, int resource, uint64_t amount)
758242139Strasz{
759282213Strasz
760282213Strasz	if (!racct_enable)
761282213Strasz		return;
762282213Strasz
763220137Strasz	mtx_lock(&racct_lock);
764242139Strasz	racct_set_force_locked(p, resource, amount);
765220137Strasz	mtx_unlock(&racct_lock);
766220137Strasz}
767220137Strasz
768220137Strasz/*
769220137Strasz * Returns amount of 'resource' the process 'p' can keep allocated.
770220137Strasz * Allocating more than that would be denied, unless the resource
771220137Strasz * is marked undeniable.  Amount of already allocated resource does
772220137Strasz * not matter.
773220137Strasz */
774220137Straszuint64_t
775220137Straszracct_get_limit(struct proc *p, int resource)
776220137Strasz{
777220137Strasz
778282213Strasz	if (!racct_enable)
779282213Strasz		return (UINT64_MAX);
780282213Strasz
781220137Strasz#ifdef RCTL
782220137Strasz	return (rctl_get_limit(p, resource));
783220137Strasz#else
784220137Strasz	return (UINT64_MAX);
785220137Strasz#endif
786220137Strasz}
787220137Strasz
788220137Strasz/*
789220137Strasz * Returns amount of 'resource' the process 'p' can keep allocated.
790220137Strasz * Allocating more than that would be denied, unless the resource
791220137Strasz * is marked undeniable.  Amount of already allocated resource does
792220137Strasz * matter.
793220137Strasz */
794220137Straszuint64_t
795220137Straszracct_get_available(struct proc *p, int resource)
796220137Strasz{
797220137Strasz
798282213Strasz	if (!racct_enable)
799282213Strasz		return (UINT64_MAX);
800282213Strasz
801220137Strasz#ifdef RCTL
802220137Strasz	return (rctl_get_available(p, resource));
803220137Strasz#else
804220137Strasz	return (UINT64_MAX);
805220137Strasz#endif
806220137Strasz}
807220137Strasz
808220137Strasz/*
809242139Strasz * Returns amount of the %cpu resource that process 'p' can add to its %cpu
810242139Strasz * utilization.  Adding more than that would lead to the process being
811242139Strasz * throttled.
812242139Strasz */
813242139Straszstatic int64_t
814242139Straszracct_pcpu_available(struct proc *p)
815242139Strasz{
816242139Strasz
817282213Strasz	ASSERT_RACCT_ENABLED();
818282213Strasz
819242139Strasz#ifdef RCTL
820242139Strasz	return (rctl_pcpu_available(p));
821242139Strasz#else
822242139Strasz	return (INT64_MAX);
823242139Strasz#endif
824242139Strasz}
825242139Strasz
826242139Strasz/*
827220137Strasz * Decrease allocation of 'resource' by 'amount' for process 'p'.
828220137Strasz */
829220137Straszvoid
830220137Straszracct_sub(struct proc *p, int resource, uint64_t amount)
831220137Strasz{
832220137Strasz
833282213Strasz	if (!racct_enable)
834282213Strasz		return;
835282213Strasz
836288336Savg	SDT_PROBE3(racct, kernel, rusage, sub, p, resource, amount);
837220137Strasz
838220137Strasz	/*
839220137Strasz	 * We need proc lock to dereference p->p_ucred.
840220137Strasz	 */
841220137Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
842242139Strasz	KASSERT(RACCT_CAN_DROP(resource),
843243088Strasz	    ("%s: called for non-droppable resource %d", __func__, resource));
844220137Strasz
845220137Strasz	mtx_lock(&racct_lock);
846220137Strasz	KASSERT(amount <= p->p_racct->r_resources[resource],
847243088Strasz	    ("%s: freeing %ju of resource %d, which is more "
848243088Strasz	     "than allocated %jd for %s (pid %d)", __func__, amount, resource,
849220137Strasz	    (intmax_t)p->p_racct->r_resources[resource], p->p_comm, p->p_pid));
850220137Strasz
851284378Sjlh	racct_adjust_resource(p->p_racct, resource, -amount);
852220137Strasz	racct_sub_cred_locked(p->p_ucred, resource, amount);
853220137Strasz	mtx_unlock(&racct_lock);
854220137Strasz}
855220137Strasz
856220137Straszstatic void
857220137Straszracct_sub_cred_locked(struct ucred *cred, int resource, uint64_t amount)
858220137Strasz{
859220137Strasz	struct prison *pr;
860220137Strasz
861282213Strasz	ASSERT_RACCT_ENABLED();
862282213Strasz
863288336Savg	SDT_PROBE3(racct, kernel, rusage, sub__cred, cred, resource, amount);
864220137Strasz
865220137Strasz#ifdef notyet
866242139Strasz	KASSERT(RACCT_CAN_DROP(resource),
867243088Strasz	    ("%s: called for resource %d which can not drop", __func__,
868220137Strasz	     resource));
869220137Strasz#endif
870220137Strasz
871284378Sjlh	racct_adjust_resource(cred->cr_ruidinfo->ui_racct, resource, -amount);
872220137Strasz	for (pr = cred->cr_prison; pr != NULL; pr = pr->pr_parent)
873284378Sjlh		racct_adjust_resource(pr->pr_prison_racct->prr_racct, resource,
874221362Strasz		    -amount);
875284378Sjlh	racct_adjust_resource(cred->cr_loginclass->lc_racct, resource, -amount);
876220137Strasz}
877220137Strasz
878220137Strasz/*
879220137Strasz * Decrease allocation of 'resource' by 'amount' for credential 'cred'.
880220137Strasz */
881220137Straszvoid
882220137Straszracct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
883220137Strasz{
884220137Strasz
885282213Strasz	if (!racct_enable)
886282213Strasz		return;
887282213Strasz
888220137Strasz	mtx_lock(&racct_lock);
889220137Strasz	racct_sub_cred_locked(cred, resource, amount);
890220137Strasz	mtx_unlock(&racct_lock);
891220137Strasz}
892220137Strasz
893220137Strasz/*
894220137Strasz * Inherit resource usage information from the parent process.
895220137Strasz */
896220137Straszint
897220137Straszracct_proc_fork(struct proc *parent, struct proc *child)
898220137Strasz{
899220137Strasz	int i, error = 0;
900220137Strasz
901282213Strasz	if (!racct_enable)
902282213Strasz		return (0);
903282213Strasz
904220137Strasz	/*
905220137Strasz	 * Create racct for the child process.
906220137Strasz	 */
907220137Strasz	racct_create(&child->p_racct);
908220137Strasz
909220137Strasz	PROC_LOCK(parent);
910220137Strasz	PROC_LOCK(child);
911220137Strasz	mtx_lock(&racct_lock);
912220137Strasz
913225981Strasz#ifdef RCTL
914225981Strasz	error = rctl_proc_fork(parent, child);
915225981Strasz	if (error != 0)
916225981Strasz		goto out;
917225981Strasz#endif
918225981Strasz
919242139Strasz	/* Init process cpu time. */
920242139Strasz	child->p_prev_runtime = 0;
921242139Strasz	child->p_throttled = 0;
922242139Strasz
923220137Strasz	/*
924220137Strasz	 * Inherit resource usage.
925220137Strasz	 */
926220137Strasz	for (i = 0; i <= RACCT_MAX; i++) {
927220137Strasz		if (parent->p_racct->r_resources[i] == 0 ||
928223844Strasz		    !RACCT_IS_INHERITABLE(i))
929220137Strasz			continue;
930220137Strasz
931220137Strasz		error = racct_set_locked(child, i,
932220137Strasz		    parent->p_racct->r_resources[i]);
933225938Strasz		if (error != 0)
934220137Strasz			goto out;
935220137Strasz	}
936220137Strasz
937225944Strasz	error = racct_add_locked(child, RACCT_NPROC, 1);
938225944Strasz	error += racct_add_locked(child, RACCT_NTHR, 1);
939225944Strasz
940220137Straszout:
941220137Strasz	mtx_unlock(&racct_lock);
942220137Strasz	PROC_UNLOCK(child);
943220137Strasz	PROC_UNLOCK(parent);
944220137Strasz
945235787Strasz	if (error != 0)
946235787Strasz		racct_proc_exit(child);
947235787Strasz
948220137Strasz	return (error);
949220137Strasz}
950220137Strasz
951225940Strasz/*
952225940Strasz * Called at the end of fork1(), to handle rules that require the process
953225940Strasz * to be fully initialized.
954225940Strasz */
955220137Straszvoid
956225940Straszracct_proc_fork_done(struct proc *child)
957225940Strasz{
958225940Strasz
959225940Strasz#ifdef RCTL
960282213Strasz	if (!racct_enable)
961282213Strasz		return;
962282213Strasz
963225940Strasz	PROC_LOCK(child);
964225940Strasz	mtx_lock(&racct_lock);
965225940Strasz	rctl_enforce(child, RACCT_NPROC, 0);
966225940Strasz	rctl_enforce(child, RACCT_NTHR, 0);
967225940Strasz	mtx_unlock(&racct_lock);
968225940Strasz	PROC_UNLOCK(child);
969225940Strasz#endif
970225940Strasz}
971225940Strasz
972225940Straszvoid
973220137Straszracct_proc_exit(struct proc *p)
974220137Strasz{
975225364Strasz	int i;
976220137Strasz	uint64_t runtime;
977242139Strasz	struct timeval wallclock;
978242139Strasz	uint64_t pct_estimate, pct;
979220137Strasz
980282213Strasz	if (!racct_enable)
981282213Strasz		return;
982282213Strasz
983220137Strasz	PROC_LOCK(p);
984220137Strasz	/*
985220137Strasz	 * We don't need to calculate rux, proc_reap() has already done this.
986220137Strasz	 */
987220137Strasz	runtime = cputick2usec(p->p_rux.rux_runtime);
988220137Strasz#ifdef notyet
989220137Strasz	KASSERT(runtime >= p->p_prev_runtime, ("runtime < p_prev_runtime"));
990220137Strasz#else
991220137Strasz	if (runtime < p->p_prev_runtime)
992220137Strasz		runtime = p->p_prev_runtime;
993220137Strasz#endif
994242139Strasz	microuptime(&wallclock);
995242139Strasz	timevalsub(&wallclock, &p->p_stats->p_start);
996242957Strasz	if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
997242957Strasz		pct_estimate = (1000000 * runtime * 100) /
998242957Strasz		    ((uint64_t)wallclock.tv_sec * 1000000 +
999242957Strasz		    wallclock.tv_usec);
1000242957Strasz	} else
1001242957Strasz		pct_estimate = 0;
1002242139Strasz	pct = racct_getpcpu(p, pct_estimate);
1003242139Strasz
1004225364Strasz	mtx_lock(&racct_lock);
1005225364Strasz	racct_set_locked(p, RACCT_CPU, runtime);
1006242139Strasz	racct_add_cred_locked(p->p_ucred, RACCT_PCTCPU, pct);
1007220137Strasz
1008225364Strasz	for (i = 0; i <= RACCT_MAX; i++) {
1009225364Strasz		if (p->p_racct->r_resources[i] == 0)
1010225364Strasz			continue;
1011225364Strasz	    	if (!RACCT_IS_RECLAIMABLE(i))
1012225364Strasz			continue;
1013225364Strasz		racct_set_locked(p, i, 0);
1014225364Strasz	}
1015225364Strasz
1016225364Strasz	mtx_unlock(&racct_lock);
1017220137Strasz	PROC_UNLOCK(p);
1018220137Strasz
1019220137Strasz#ifdef RCTL
1020220137Strasz	rctl_racct_release(p->p_racct);
1021220137Strasz#endif
1022220137Strasz	racct_destroy(&p->p_racct);
1023220137Strasz}
1024220137Strasz
1025220137Strasz/*
1026220137Strasz * Called after credentials change, to move resource utilisation
1027220137Strasz * between raccts.
1028220137Strasz */
1029220137Straszvoid
1030220137Straszracct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
1031220137Strasz    struct ucred *newcred)
1032220137Strasz{
1033220137Strasz	struct uidinfo *olduip, *newuip;
1034220137Strasz	struct loginclass *oldlc, *newlc;
1035220137Strasz	struct prison *oldpr, *newpr, *pr;
1036220137Strasz
1037282213Strasz	if (!racct_enable)
1038282213Strasz		return;
1039282213Strasz
1040220137Strasz	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1041220137Strasz
1042220137Strasz	newuip = newcred->cr_ruidinfo;
1043220137Strasz	olduip = oldcred->cr_ruidinfo;
1044220137Strasz	newlc = newcred->cr_loginclass;
1045220137Strasz	oldlc = oldcred->cr_loginclass;
1046220137Strasz	newpr = newcred->cr_prison;
1047220137Strasz	oldpr = oldcred->cr_prison;
1048220137Strasz
1049220137Strasz	mtx_lock(&racct_lock);
1050220137Strasz	if (newuip != olduip) {
1051220137Strasz		racct_sub_racct(olduip->ui_racct, p->p_racct);
1052220137Strasz		racct_add_racct(newuip->ui_racct, p->p_racct);
1053220137Strasz	}
1054220137Strasz	if (newlc != oldlc) {
1055220137Strasz		racct_sub_racct(oldlc->lc_racct, p->p_racct);
1056220137Strasz		racct_add_racct(newlc->lc_racct, p->p_racct);
1057220137Strasz	}
1058220137Strasz	if (newpr != oldpr) {
1059220137Strasz		for (pr = oldpr; pr != NULL; pr = pr->pr_parent)
1060221362Strasz			racct_sub_racct(pr->pr_prison_racct->prr_racct,
1061221362Strasz			    p->p_racct);
1062220137Strasz		for (pr = newpr; pr != NULL; pr = pr->pr_parent)
1063221362Strasz			racct_add_racct(pr->pr_prison_racct->prr_racct,
1064221362Strasz			    p->p_racct);
1065220137Strasz	}
1066220137Strasz	mtx_unlock(&racct_lock);
1067220137Strasz
1068220137Strasz#ifdef RCTL
1069220137Strasz	rctl_proc_ucred_changed(p, newcred);
1070220137Strasz#endif
1071220137Strasz}
1072220137Strasz
1073232598Straszvoid
1074232598Straszracct_move(struct racct *dest, struct racct *src)
1075232598Strasz{
1076232598Strasz
1077282213Strasz	ASSERT_RACCT_ENABLED();
1078282213Strasz
1079232598Strasz	mtx_lock(&racct_lock);
1080232598Strasz
1081232598Strasz	racct_add_racct(dest, src);
1082232598Strasz	racct_sub_racct(src, src);
1083232598Strasz
1084232598Strasz	mtx_unlock(&racct_lock);
1085232598Strasz}
1086232598Strasz
1087220137Straszstatic void
1088242139Straszracct_proc_throttle(struct proc *p)
1089242139Strasz{
1090242139Strasz	struct thread *td;
1091242139Strasz#ifdef SMP
1092242139Strasz	int cpuid;
1093242139Strasz#endif
1094242139Strasz
1095282213Strasz	ASSERT_RACCT_ENABLED();
1096242139Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
1097242139Strasz
1098242139Strasz	/*
1099242139Strasz	 * Do not block kernel processes.  Also do not block processes with
1100242139Strasz	 * low %cpu utilization to improve interactivity.
1101242139Strasz	 */
1102242139Strasz	if (((p->p_flag & (P_SYSTEM | P_KTHREAD)) != 0) ||
1103242139Strasz	    (p->p_racct->r_resources[RACCT_PCTCPU] <= pcpu_threshold))
1104242139Strasz		return;
1105242139Strasz	p->p_throttled = 1;
1106242139Strasz
1107242139Strasz	FOREACH_THREAD_IN_PROC(p, td) {
1108248298Strasz		thread_lock(td);
1109242139Strasz		switch (td->td_state) {
1110242139Strasz		case TDS_RUNQ:
1111242139Strasz			/*
1112242139Strasz			 * If the thread is on the scheduler run-queue, we can
1113242139Strasz			 * not just remove it from there.  So we set the flag
1114242139Strasz			 * TDF_NEEDRESCHED for the thread, so that once it is
1115242139Strasz			 * running, it is taken off the cpu as soon as possible.
1116242139Strasz			 */
1117242139Strasz			td->td_flags |= TDF_NEEDRESCHED;
1118242139Strasz			break;
1119242139Strasz		case TDS_RUNNING:
1120242139Strasz			/*
1121242139Strasz			 * If the thread is running, we request a context
1122242139Strasz			 * switch for it by setting the TDF_NEEDRESCHED flag.
1123242139Strasz			 */
1124242139Strasz			td->td_flags |= TDF_NEEDRESCHED;
1125242139Strasz#ifdef SMP
1126242139Strasz			cpuid = td->td_oncpu;
1127242139Strasz			if ((cpuid != NOCPU) && (td != curthread))
1128242139Strasz				ipi_cpu(cpuid, IPI_AST);
1129242139Strasz#endif
1130242139Strasz			break;
1131242139Strasz		default:
1132242139Strasz			break;
1133242139Strasz		}
1134248298Strasz		thread_unlock(td);
1135242139Strasz	}
1136242139Strasz}
1137242139Strasz
1138242139Straszstatic void
1139242139Straszracct_proc_wakeup(struct proc *p)
1140242139Strasz{
1141282213Strasz
1142282213Strasz	ASSERT_RACCT_ENABLED();
1143282213Strasz
1144242139Strasz	PROC_LOCK_ASSERT(p, MA_OWNED);
1145242139Strasz
1146242139Strasz	if (p->p_throttled) {
1147242139Strasz		p->p_throttled = 0;
1148242139Strasz		wakeup(p->p_racct);
1149242139Strasz	}
1150242139Strasz}
1151242139Strasz
1152242139Straszstatic void
1153242139Straszracct_decay_resource(struct racct *racct, void * res, void* dummy)
1154242139Strasz{
1155242139Strasz	int resource;
1156242139Strasz	int64_t r_old, r_new;
1157242139Strasz
1158282213Strasz	ASSERT_RACCT_ENABLED();
1159290857Strasz	mtx_assert(&racct_lock, MA_OWNED);
1160282213Strasz
1161242139Strasz	resource = *(int *)res;
1162242139Strasz	r_old = racct->r_resources[resource];
1163242139Strasz
1164242139Strasz	/* If there is nothing to decay, just exit. */
1165242139Strasz	if (r_old <= 0)
1166242139Strasz		return;
1167242139Strasz
1168242139Strasz	r_new = r_old * RACCT_DECAY_FACTOR / FSCALE;
1169242139Strasz	racct->r_resources[resource] = r_new;
1170290857Strasz}
1171290857Strasz
1172290857Straszstatic void
1173290857Straszracct_decay_pre(void)
1174290857Strasz{
1175290857Strasz
1176290857Strasz	mtx_lock(&racct_lock);
1177290857Strasz}
1178290857Strasz
1179290857Straszstatic void
1180290857Straszracct_decay_post(void)
1181290857Strasz{
1182290857Strasz
1183242139Strasz	mtx_unlock(&racct_lock);
1184242139Strasz}
1185242139Strasz
1186242139Straszstatic void
1187242139Straszracct_decay(int resource)
1188242139Strasz{
1189282213Strasz
1190282213Strasz	ASSERT_RACCT_ENABLED();
1191282213Strasz
1192290857Strasz	ui_racct_foreach(racct_decay_resource, racct_decay_pre,
1193290857Strasz	    racct_decay_post, &resource, NULL);
1194290857Strasz	loginclass_racct_foreach(racct_decay_resource, racct_decay_pre,
1195290857Strasz	    racct_decay_post, &resource, NULL);
1196290857Strasz	prison_racct_foreach(racct_decay_resource, racct_decay_pre,
1197290857Strasz	    racct_decay_post, &resource, NULL);
1198242139Strasz}
1199242139Strasz
1200242139Straszstatic void
1201220137Straszracctd(void)
1202220137Strasz{
1203220137Strasz	struct thread *td;
1204220137Strasz	struct proc *p;
1205220137Strasz	struct timeval wallclock;
1206220137Strasz	uint64_t runtime;
1207242139Strasz	uint64_t pct, pct_estimate;
1208220137Strasz
1209282213Strasz	ASSERT_RACCT_ENABLED();
1210282213Strasz
1211220137Strasz	for (;;) {
1212242139Strasz		racct_decay(RACCT_PCTCPU);
1213242139Strasz
1214220137Strasz		sx_slock(&allproc_lock);
1215220137Strasz
1216242139Strasz		LIST_FOREACH(p, &zombproc, p_list) {
1217242139Strasz			PROC_LOCK(p);
1218242139Strasz			racct_set(p, RACCT_PCTCPU, 0);
1219242139Strasz			PROC_UNLOCK(p);
1220242139Strasz		}
1221242139Strasz
1222220137Strasz		FOREACH_PROC_IN_SYSTEM(p) {
1223242139Strasz			PROC_LOCK(p);
1224242139Strasz			if (p->p_state != PRS_NORMAL) {
1225242139Strasz				PROC_UNLOCK(p);
1226220137Strasz				continue;
1227242139Strasz			}
1228220137Strasz
1229220137Strasz			microuptime(&wallclock);
1230220137Strasz			timevalsub(&wallclock, &p->p_stats->p_start);
1231275121Skib			PROC_STATLOCK(p);
1232232782Strasz			FOREACH_THREAD_IN_PROC(p, td)
1233220137Strasz				ruxagg(p, td);
1234220137Strasz			runtime = cputick2usec(p->p_rux.rux_runtime);
1235275121Skib			PROC_STATUNLOCK(p);
1236220137Strasz#ifdef notyet
1237220137Strasz			KASSERT(runtime >= p->p_prev_runtime,
1238220137Strasz			    ("runtime < p_prev_runtime"));
1239220137Strasz#else
1240220137Strasz			if (runtime < p->p_prev_runtime)
1241220137Strasz				runtime = p->p_prev_runtime;
1242220137Strasz#endif
1243220137Strasz			p->p_prev_runtime = runtime;
1244242957Strasz			if (wallclock.tv_sec > 0 || wallclock.tv_usec > 0) {
1245242957Strasz				pct_estimate = (1000000 * runtime * 100) /
1246242957Strasz				    ((uint64_t)wallclock.tv_sec * 1000000 +
1247242957Strasz				    wallclock.tv_usec);
1248242957Strasz			} else
1249242957Strasz				pct_estimate = 0;
1250242139Strasz			pct = racct_getpcpu(p, pct_estimate);
1251220137Strasz			mtx_lock(&racct_lock);
1252242139Strasz			racct_set_force_locked(p, RACCT_PCTCPU, pct);
1253220137Strasz			racct_set_locked(p, RACCT_CPU, runtime);
1254220137Strasz			racct_set_locked(p, RACCT_WALLCLOCK,
1255233126Sjh			    (uint64_t)wallclock.tv_sec * 1000000 +
1256233126Sjh			    wallclock.tv_usec);
1257220137Strasz			mtx_unlock(&racct_lock);
1258220137Strasz			PROC_UNLOCK(p);
1259220137Strasz		}
1260242139Strasz
1261242139Strasz		/*
1262242139Strasz		 * To ensure that processes are throttled in a fair way, we need
1263242139Strasz		 * to iterate over all processes again and check the limits
1264242139Strasz		 * for %cpu resource only after ucred racct containers have been
1265242139Strasz		 * properly filled.
1266242139Strasz		 */
1267242139Strasz		FOREACH_PROC_IN_SYSTEM(p) {
1268242139Strasz			PROC_LOCK(p);
1269242139Strasz			if (p->p_state != PRS_NORMAL) {
1270242139Strasz				PROC_UNLOCK(p);
1271242139Strasz				continue;
1272242139Strasz			}
1273242139Strasz
1274242139Strasz			if (racct_pcpu_available(p) <= 0)
1275242139Strasz				racct_proc_throttle(p);
1276242139Strasz			else if (p->p_throttled)
1277242139Strasz				racct_proc_wakeup(p);
1278242139Strasz			PROC_UNLOCK(p);
1279242139Strasz		}
1280220137Strasz		sx_sunlock(&allproc_lock);
1281220137Strasz		pause("-", hz);
1282220137Strasz	}
1283220137Strasz}
1284220137Strasz
1285220137Straszstatic struct kproc_desc racctd_kp = {
1286220137Strasz	"racctd",
1287220137Strasz	racctd,
1288220137Strasz	NULL
1289220137Strasz};
1290220137Strasz
1291220137Straszstatic void
1292282213Straszracctd_init(void)
1293282213Strasz{
1294282213Strasz	if (!racct_enable)
1295282213Strasz		return;
1296282213Strasz
1297282213Strasz	kproc_start(&racctd_kp);
1298282213Strasz}
1299282213StraszSYSINIT(racctd, SI_SUB_RACCTD, SI_ORDER_FIRST, racctd_init, NULL);
1300282213Strasz
1301282213Straszstatic void
1302220137Straszracct_init(void)
1303220137Strasz{
1304282213Strasz	if (!racct_enable)
1305282213Strasz		return;
1306220137Strasz
1307220137Strasz	racct_zone = uma_zcreate("racct", sizeof(struct racct),
1308220137Strasz	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1309220137Strasz	/*
1310220137Strasz	 * XXX: Move this somewhere.
1311220137Strasz	 */
1312221362Strasz	prison0.pr_prison_racct = prison_racct_find("0");
1313220137Strasz}
1314220137StraszSYSINIT(racct, SI_SUB_RACCT, SI_ORDER_FIRST, racct_init, NULL);
1315220137Strasz
1316220137Strasz#endif /* !RACCT */
1317