]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/ledger.c
xnu-4903.221.2.tar.gz
[apple/xnu.git] / osfmk / kern / ledger.c
CommitLineData
1c79356b 1/*
d9a64523 2 * Copyright (c) 2010-2018 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
91447636 31
fe8ab488 32#include <kern/kern_types.h>
1c79356b 33#include <kern/ledger.h>
91447636 34#include <kern/kalloc.h>
316670eb 35#include <kern/task.h>
3e170ce0 36#include <kern/thread.h>
91447636 37
316670eb
A
38#include <kern/processor.h>
39#include <kern/machine.h>
40#include <kern/queue.h>
39037602
A
41#include <kern/policy_internal.h>
42
316670eb 43#include <sys/errno.h>
1c79356b 44
316670eb
A
45#include <libkern/OSAtomic.h>
46#include <mach/mach_types.h>
813fb2f6 47#include <os/overflow.h>
1c79356b 48
d9a64523
A
49#include <vm/pmap.h>
50
316670eb
A
51/*
52 * Ledger entry flags. Bits in second nibble (masked by 0xF0) are used for
53 * ledger actions (LEDGER_ACTION_BLOCK, etc).
54 */
39236c6e
A
55#define LF_ENTRY_ACTIVE 0x0001 /* entry is active if set */
56#define LF_WAKE_NEEDED 0x0100 /* one or more threads are asleep */
57#define LF_WAKE_INPROGRESS 0x0200 /* the wait queue is being processed */
58#define LF_REFILL_SCHEDULED 0x0400 /* a refill timer has been set */
59#define LF_REFILL_INPROGRESS 0x0800 /* the ledger is being refilled */
60#define LF_CALLED_BACK 0x1000 /* callback was called for balance in deficit */
61#define LF_WARNED 0x2000 /* callback was called for balance warning */
5ba3f43e 62#define LF_TRACKING_MAX 0x4000 /* track max balance. Exclusive w.r.t refill */
fe8ab488 63#define LF_PANIC_ON_NEGATIVE 0x8000 /* panic if it goes negative */
39037602 64#define LF_TRACK_CREDIT_ONLY 0x10000 /* only update "credit" */
1c79356b 65
316670eb
A
66/* Determine whether a ledger entry exists and has been initialized and active */
67#define ENTRY_VALID(l, e) \
68 (((l) != NULL) && ((e) >= 0) && ((e) < (l)->l_size) && \
39236c6e 69 (((l)->l_entries[e].le_flags & LF_ENTRY_ACTIVE) == LF_ENTRY_ACTIVE))
316670eb 70
fe8ab488
A
71#define ASSERT(a) assert(a)
72
316670eb
A
73#ifdef LEDGER_DEBUG
74int ledger_debug = 0;
75
316670eb
A
76#define lprintf(a) if (ledger_debug) { \
77 printf("%lld ", abstime_to_nsecs(mach_absolute_time() / 1000000)); \
78 printf a ; \
79}
80#else
81#define lprintf(a)
316670eb
A
82#endif
83
84struct ledger_callback {
85 ledger_callback_t lc_func;
86 const void *lc_param0;
87 const void *lc_param1;
88};
89
90struct entry_template {
91 char et_key[LEDGER_NAME_MAX];
92 char et_group[LEDGER_NAME_MAX];
93 char et_units[LEDGER_NAME_MAX];
94 uint32_t et_flags;
95 struct ledger_callback *et_callback;
96};
97
98lck_grp_t ledger_lck_grp;
99
100/*
101 * Modifying the reference count, table size, or table contents requires
102 * holding the lt_lock. Modfying the table address requires both lt_lock
103 * and setting the inuse bit. This means that the lt_entries field can be
104 * safely dereferenced if you hold either the lock or the inuse bit. The
105 * inuse bit exists solely to allow us to swap in a new, larger entries
106 * table without requiring a full lock to be acquired on each lookup.
107 * Accordingly, the inuse bit should never be held for longer than it takes
108 * to extract a value from the table - i.e., 2 or 3 memory references.
109 */
110struct ledger_template {
111 const char *lt_name;
112 int lt_refs;
113 int lt_cnt;
114 int lt_table_size;
115 volatile uint32_t lt_inuse;
116 lck_mtx_t lt_lock;
5ba3f43e 117 zone_t lt_zone;
d9a64523 118 bool lt_initialized;
316670eb
A
119 struct entry_template *lt_entries;
120};
121
122#define template_lock(template) lck_mtx_lock(&(template)->lt_lock)
123#define template_unlock(template) lck_mtx_unlock(&(template)->lt_lock)
124
125#define TEMPLATE_INUSE(s, t) { \
126 s = splsched(); \
127 while (OSCompareAndSwap(0, 1, &((t)->lt_inuse))) \
128 ; \
129}
130
131#define TEMPLATE_IDLE(s, t) { \
132 (t)->lt_inuse = 0; \
133 splx(s); \
134}
135
316670eb
A
136static int ledger_cnt = 0;
137/* ledger ast helper functions */
138static uint32_t ledger_check_needblock(ledger_t l, uint64_t now);
139static kern_return_t ledger_perform_blocking(ledger_t l);
140static uint32_t flag_set(volatile uint32_t *flags, uint32_t bit);
141static uint32_t flag_clear(volatile uint32_t *flags, uint32_t bit);
142
5c9f4661
A
143static void ledger_entry_check_new_balance(thread_t thread, ledger_t ledger,
144 int entry, struct ledger_entry *le);
39037602 145
316670eb
A
146#if 0
147static void
148debug_callback(const void *p0, __unused const void *p1)
1c79356b 149{
316670eb
A
150 printf("ledger: resource exhausted [%s] for task %p\n",
151 (const char *)p0, p1);
152}
153#endif
b0d623f7 154
316670eb
A
155/************************************/
156
157static uint64_t
158abstime_to_nsecs(uint64_t abstime)
159{
160 uint64_t nsecs;
161
162 absolutetime_to_nanoseconds(abstime, &nsecs);
163 return (nsecs);
164}
165
166static uint64_t
167nsecs_to_abstime(uint64_t nsecs)
168{
169 uint64_t abstime;
170
171 nanoseconds_to_absolutetime(nsecs, &abstime);
172 return (abstime);
173}
174
175void
176ledger_init(void)
177{
178 lck_grp_init(&ledger_lck_grp, "ledger", LCK_GRP_ATTR_NULL);
179}
180
181ledger_template_t
182ledger_template_create(const char *name)
183{
184 ledger_template_t template;
185
186 template = (ledger_template_t)kalloc(sizeof (*template));
187 if (template == NULL)
188 return (NULL);
189
190 template->lt_name = name;
191 template->lt_refs = 1;
192 template->lt_cnt = 0;
193 template->lt_table_size = 1;
194 template->lt_inuse = 0;
5ba3f43e 195 template->lt_zone = NULL;
316670eb
A
196 lck_mtx_init(&template->lt_lock, &ledger_lck_grp, LCK_ATTR_NULL);
197
198 template->lt_entries = (struct entry_template *)
199 kalloc(sizeof (struct entry_template) * template->lt_table_size);
200 if (template->lt_entries == NULL) {
201 kfree(template, sizeof (*template));
202 template = NULL;
203 }
204
205 return (template);
206}
207
208void
209ledger_template_dereference(ledger_template_t template)
210{
211 template_lock(template);
212 template->lt_refs--;
213 template_unlock(template);
214
215 if (template->lt_refs == 0)
216 kfree(template, sizeof (*template));
217}
218
219/*
220 * Add a new entry to the list of entries in a ledger template. There is
221 * currently no mechanism to remove an entry. Implementing such a mechanism
222 * would require us to maintain per-entry reference counts, which we would
223 * prefer to avoid if possible.
224 */
225int
226ledger_entry_add(ledger_template_t template, const char *key,
227 const char *group, const char *units)
228{
229 int idx;
230 struct entry_template *et;
231
5ba3f43e 232 if ((key == NULL) || (strlen(key) >= LEDGER_NAME_MAX) || (template->lt_zone != NULL))
316670eb
A
233 return (-1);
234
235 template_lock(template);
236
237 /* If the table is full, attempt to double its size */
238 if (template->lt_cnt == template->lt_table_size) {
239 struct entry_template *new_entries, *old_entries;
813fb2f6 240 int old_cnt, old_sz, new_sz = 0;
316670eb
A
241 spl_t s;
242
243 old_cnt = template->lt_table_size;
813fb2f6
A
244 old_sz = old_cnt * (int)(sizeof(struct entry_template));
245 /* double old_sz allocation, but check for overflow */
246 if (os_mul_overflow(old_sz, 2, &new_sz)) {
247 template_unlock(template);
248 return -1;
249 }
250 new_entries = kalloc(new_sz);
316670eb
A
251 if (new_entries == NULL) {
252 template_unlock(template);
813fb2f6 253 return -1;
1c79356b 254 }
316670eb
A
255 memcpy(new_entries, template->lt_entries, old_sz);
256 memset(((char *)new_entries) + old_sz, 0, old_sz);
813fb2f6 257 /* assume: if the sz didn't overflow, neither will the count */
316670eb
A
258 template->lt_table_size = old_cnt * 2;
259
260 old_entries = template->lt_entries;
261
262 TEMPLATE_INUSE(s, template);
263 template->lt_entries = new_entries;
264 TEMPLATE_IDLE(s, template);
265
266 kfree(old_entries, old_sz);
1c79356b 267 }
316670eb
A
268
269 et = &template->lt_entries[template->lt_cnt];
270 strlcpy(et->et_key, key, LEDGER_NAME_MAX);
271 strlcpy(et->et_group, group, LEDGER_NAME_MAX);
272 strlcpy(et->et_units, units, LEDGER_NAME_MAX);
39236c6e 273 et->et_flags = LF_ENTRY_ACTIVE;
316670eb
A
274 et->et_callback = NULL;
275
276 idx = template->lt_cnt++;
277 template_unlock(template);
278
279 return (idx);
280}
281
282
283kern_return_t
284ledger_entry_setactive(ledger_t ledger, int entry)
285{
286 struct ledger_entry *le;
287
288 if ((ledger == NULL) || (entry < 0) || (entry >= ledger->l_size))
289 return (KERN_INVALID_ARGUMENT);
290
291 le = &ledger->l_entries[entry];
39236c6e
A
292 if ((le->le_flags & LF_ENTRY_ACTIVE) == 0) {
293 flag_set(&le->le_flags, LF_ENTRY_ACTIVE);
1c79356b 294 }
316670eb 295 return (KERN_SUCCESS);
1c79356b
A
296}
297
316670eb
A
298
299int
300ledger_key_lookup(ledger_template_t template, const char *key)
1c79356b 301{
316670eb
A
302 int idx;
303
304 template_lock(template);
305 for (idx = 0; idx < template->lt_cnt; idx++)
3e170ce0 306 if (template->lt_entries != NULL &&
316670eb
A
307 (strcmp(key, template->lt_entries[idx].et_key) == 0))
308 break;
1c79356b 309
316670eb
A
310 if (idx >= template->lt_cnt)
311 idx = -1;
312 template_unlock(template);
313
314 return (idx);
315}
1c79356b 316
5ba3f43e
A
317/*
318 * Complete the initialization of ledger template
319 * by initializing ledger zone. After initializing
320 * the ledger zone, adding an entry in the ledger
321 * template would fail.
322 */
323void
324ledger_template_complete(ledger_template_t template)
325{
326 size_t ledger_size;
327 ledger_size = sizeof(struct ledger) + (template->lt_cnt * sizeof(struct ledger_entry));
328 template->lt_zone = zinit(ledger_size, CONFIG_TASK_MAX * ledger_size,
329 ledger_size,
330 template->lt_name);
d9a64523
A
331 template->lt_initialized = true;
332}
333
334/*
335 * Like ledger_template_complete, except we'll ask
336 * the pmap layer to manage allocations for us.
337 * Meant for ledgers that should be owned by the
338 * pmap layer.
339 */
340void
341ledger_template_complete_secure_alloc(ledger_template_t template)
342{
343 size_t ledger_size;
344 ledger_size = sizeof(struct ledger) + (template->lt_cnt * sizeof(struct ledger_entry));
345 pmap_ledger_alloc_init(ledger_size);
346 template->lt_initialized = true;
5ba3f43e
A
347}
348
316670eb
A
349/*
350 * Create a new ledger based on the specified template. As part of the
351 * ledger creation we need to allocate space for a table of ledger entries.
352 * The size of the table is based on the size of the template at the time
353 * the ledger is created. If additional entries are added to the template
354 * after the ledger is created, they will not be tracked in this ledger.
355 */
356ledger_t
357ledger_instantiate(ledger_template_t template, int entry_type)
358{
359 ledger_t ledger;
5ba3f43e 360 size_t cnt;
316670eb
A
361 int i;
362
316670eb
A
363 template_lock(template);
364 template->lt_refs++;
39037602 365 cnt = template->lt_cnt;
316670eb
A
366 template_unlock(template);
367
d9a64523
A
368 if (template->lt_zone) {
369 ledger = (ledger_t)zalloc(template->lt_zone);
370 } else {
371 ledger = pmap_ledger_alloc();
372 }
373
39037602 374 if (ledger == NULL) {
316670eb 375 ledger_template_dereference(template);
39037602 376 return LEDGER_NULL;
2d21ac55 377 }
1c79356b 378
39037602
A
379 ledger->l_template = template;
380 ledger->l_id = ledger_cnt++;
381 ledger->l_refs = 1;
382 ledger->l_size = (int32_t)cnt;
383
316670eb
A
384 template_lock(template);
385 assert(ledger->l_size <= template->lt_cnt);
386 for (i = 0; i < ledger->l_size; i++) {
387 struct ledger_entry *le = &ledger->l_entries[i];
388 struct entry_template *et = &template->lt_entries[i];
1c79356b 389
316670eb
A
390 le->le_flags = et->et_flags;
391 /* make entry inactive by removing active bit */
392 if (entry_type == LEDGER_CREATE_INACTIVE_ENTRIES)
39236c6e 393 flag_clear(&le->le_flags, LF_ENTRY_ACTIVE);
316670eb
A
394 /*
395 * If template has a callback, this entry is opted-in,
396 * by default.
397 */
398 if (et->et_callback != NULL)
399 flag_set(&le->le_flags, LEDGER_ACTION_CALLBACK);
39236c6e
A
400 le->le_credit = 0;
401 le->le_debit = 0;
402 le->le_limit = LEDGER_LIMIT_INFINITY;
403 le->le_warn_level = LEDGER_LIMIT_INFINITY;
404 le->_le.le_refill.le_refill_period = 0;
405 le->_le.le_refill.le_last_refill = 0;
316670eb
A
406 }
407 template_unlock(template);
408
409 return (ledger);
1c79356b
A
410}
411
316670eb
A
412static uint32_t
413flag_set(volatile uint32_t *flags, uint32_t bit)
1c79356b 414{
316670eb
A
415 return (OSBitOrAtomic(bit, flags));
416}
1c79356b 417
316670eb
A
418static uint32_t
419flag_clear(volatile uint32_t *flags, uint32_t bit)
420{
421 return (OSBitAndAtomic(~bit, flags));
422}
423
424/*
425 * Take a reference on a ledger
426 */
427kern_return_t
428ledger_reference(ledger_t ledger)
429{
430 if (!LEDGER_VALID(ledger))
431 return (KERN_INVALID_ARGUMENT);
432 OSIncrementAtomic(&ledger->l_refs);
433 return (KERN_SUCCESS);
1c79356b
A
434}
435
316670eb
A
436int
437ledger_reference_count(ledger_t ledger)
438{
439 if (!LEDGER_VALID(ledger))
440 return (-1);
441
442 return (ledger->l_refs);
443}
1c79356b
A
444
445/*
316670eb
A
446 * Remove a reference on a ledger. If this is the last reference,
447 * deallocate the unused ledger.
1c79356b 448 */
316670eb
A
449kern_return_t
450ledger_dereference(ledger_t ledger)
1c79356b 451{
316670eb
A
452 int v;
453
454 if (!LEDGER_VALID(ledger))
455 return (KERN_INVALID_ARGUMENT);
456
457 v = OSDecrementAtomic(&ledger->l_refs);
458 ASSERT(v >= 1);
1c79356b 459
316670eb
A
460 /* Just released the last reference. Free it. */
461 if (v == 1) {
d9a64523
A
462 if (ledger->l_template->lt_zone) {
463 zfree(ledger->l_template->lt_zone, ledger);
464 } else {
465 pmap_ledger_free(ledger);
466 }
316670eb
A
467 }
468
469 return (KERN_SUCCESS);
470}
471
39236c6e
A
472/*
473 * Determine whether an entry has exceeded its warning level.
474 */
475static inline int
476warn_level_exceeded(struct ledger_entry *le)
477{
478 ledger_amount_t balance;
479
39037602
A
480 if (le->le_flags & LF_TRACK_CREDIT_ONLY) {
481 assert(le->le_debit == 0);
482 } else {
483 assert((le->le_credit >= 0) && (le->le_debit >= 0));
484 }
39236c6e
A
485
486 /*
487 * XXX - Currently, we only support warnings for ledgers which
488 * use positive limits.
489 */
490 balance = le->le_credit - le->le_debit;
491 if ((le->le_warn_level != LEDGER_LIMIT_INFINITY) && (balance > le->le_warn_level))
492 return (1);
493 return (0);
494}
495
316670eb
A
496/*
497 * Determine whether an entry has exceeded its limit.
498 */
499static inline int
500limit_exceeded(struct ledger_entry *le)
501{
502 ledger_amount_t balance;
503
39037602
A
504 if (le->le_flags & LF_TRACK_CREDIT_ONLY) {
505 assert(le->le_debit == 0);
506 } else {
507 assert((le->le_credit >= 0) && (le->le_debit >= 0));
508 }
39236c6e 509
316670eb
A
510 balance = le->le_credit - le->le_debit;
511 if ((le->le_limit <= 0) && (balance < le->le_limit))
512 return (1);
513
514 if ((le->le_limit > 0) && (balance > le->le_limit))
515 return (1);
516 return (0);
517}
518
519static inline struct ledger_callback *
520entry_get_callback(ledger_t ledger, int entry)
521{
522 struct ledger_callback *callback;
523 spl_t s;
524
525 TEMPLATE_INUSE(s, ledger->l_template);
526 callback = ledger->l_template->lt_entries[entry].et_callback;
527 TEMPLATE_IDLE(s, ledger->l_template);
528
529 return (callback);
530}
531
532/*
533 * If the ledger value is positive, wake up anybody waiting on it.
534 */
535static inline void
536ledger_limit_entry_wakeup(struct ledger_entry *le)
537{
538 uint32_t flags;
539
540 if (!limit_exceeded(le)) {
39236c6e 541 flags = flag_clear(&le->le_flags, LF_CALLED_BACK);
316670eb 542
39236c6e
A
543 while (le->le_flags & LF_WAKE_NEEDED) {
544 flag_clear(&le->le_flags, LF_WAKE_NEEDED);
316670eb
A
545 thread_wakeup((event_t)le);
546 }
547 }
1c79356b
A
548}
549
550/*
316670eb 551 * Refill the coffers.
1c79356b 552 */
316670eb
A
553static void
554ledger_refill(uint64_t now, ledger_t ledger, int entry)
1c79356b 555{
316670eb
A
556 uint64_t elapsed, period, periods;
557 struct ledger_entry *le;
558 ledger_amount_t balance, due;
1c79356b 559
39037602
A
560 assert(entry >= 0 && entry < ledger->l_size);
561
316670eb 562 le = &ledger->l_entries[entry];
1c79356b 563
39236c6e
A
564 assert(le->le_limit != LEDGER_LIMIT_INFINITY);
565
39037602
A
566 if (le->le_flags & LF_TRACK_CREDIT_ONLY) {
567 assert(le->le_debit == 0);
568 return;
569 }
570
1c79356b 571 /*
316670eb 572 * If another thread is handling the refill already, we're not
39236c6e
A
573 * needed.
574 */
575 if (flag_set(&le->le_flags, LF_REFILL_INPROGRESS) & LF_REFILL_INPROGRESS) {
576 return;
577 }
578
579 /*
580 * If the timestamp we're about to use to refill is older than the
581 * last refill, then someone else has already refilled this ledger
582 * and there's nothing for us to do here.
1c79356b 583 */
39236c6e
A
584 if (now <= le->_le.le_refill.le_last_refill) {
585 flag_clear(&le->le_flags, LF_REFILL_INPROGRESS);
316670eb 586 return;
1c79356b
A
587 }
588
316670eb
A
589 /*
590 * See how many refill periods have passed since we last
591 * did a refill.
592 */
39236c6e
A
593 period = le->_le.le_refill.le_refill_period;
594 elapsed = now - le->_le.le_refill.le_last_refill;
316670eb 595 if ((period == 0) || (elapsed < period)) {
39236c6e 596 flag_clear(&le->le_flags, LF_REFILL_INPROGRESS);
316670eb 597 return;
1c79356b 598 }
316670eb
A
599
600 /*
601 * Optimize for the most common case of only one or two
602 * periods elapsing.
603 */
604 periods = 0;
605 while ((periods < 2) && (elapsed > 0)) {
606 periods++;
607 elapsed -= period;
608 }
609
610 /*
611 * OK, it's been a long time. Do a divide to figure out
612 * how long.
613 */
614 if (elapsed > 0)
39236c6e 615 periods = (now - le->_le.le_refill.le_last_refill) / period;
316670eb
A
616
617 balance = le->le_credit - le->le_debit;
618 due = periods * le->le_limit;
813fb2f6 619
316670eb
A
620 if (balance - due < 0)
621 due = balance;
39236c6e 622
813fb2f6 623 assertf(due >= 0,"now=%llu, ledger=%p, entry=%d, balance=%lld, due=%lld", now, ledger, entry, balance, due);
39236c6e 624
316670eb
A
625 OSAddAtomic64(due, &le->le_debit);
626
39236c6e
A
627 assert(le->le_debit >= 0);
628
1c79356b 629 /*
316670eb
A
630 * If we've completely refilled the pool, set the refill time to now.
631 * Otherwise set it to the time at which it last should have been
632 * fully refilled.
1c79356b 633 */
316670eb 634 if (balance == due)
39236c6e 635 le->_le.le_refill.le_last_refill = now;
316670eb 636 else
39236c6e 637 le->_le.le_refill.le_last_refill += (le->_le.le_refill.le_refill_period * periods);
316670eb 638
39236c6e 639 flag_clear(&le->le_flags, LF_REFILL_INPROGRESS);
316670eb
A
640
641 lprintf(("Refill %lld %lld->%lld\n", periods, balance, balance - due));
642 if (!limit_exceeded(le))
643 ledger_limit_entry_wakeup(le);
644}
645
3e170ce0 646void
5c9f4661
A
647ledger_entry_check_new_balance(thread_t thread, ledger_t ledger,
648 int entry, struct ledger_entry *le)
316670eb 649{
39236c6e
A
650 if (le->le_flags & LF_TRACKING_MAX) {
651 ledger_amount_t balance = le->le_credit - le->le_debit;
39236c6e 652
d9a64523
A
653 if (balance > le->_le._le_max.le_lifetime_max){
654 le->_le._le_max.le_lifetime_max = balance;
39236c6e 655 }
5ba3f43e 656
d9a64523
A
657#if CONFIG_LEDGER_INTERVAL_MAX
658 if (balance > le->_le._le_max.le_interval_max) {
659 le->_le._le_max.le_interval_max = balance;
5ba3f43e 660 }
d9a64523 661#endif /* LEDGER_CONFIG_INTERVAL_MAX */
39236c6e
A
662 }
663
316670eb 664 /* Check to see whether we're due a refill */
39236c6e 665 if (le->le_flags & LF_REFILL_SCHEDULED) {
5ba3f43e
A
666 assert(!(le->le_flags & LF_TRACKING_MAX));
667
39236c6e
A
668 uint64_t now = mach_absolute_time();
669 if ((now - le->_le.le_refill.le_last_refill) > le->_le.le_refill.le_refill_period)
316670eb
A
670 ledger_refill(now, ledger, entry);
671 }
672
673 if (limit_exceeded(le)) {
674 /*
675 * We've exceeded the limit for this entry. There
676 * are several possible ways to handle it. We can block,
677 * we can execute a callback, or we can ignore it. In
678 * either of the first two cases, we want to set the AST
679 * flag so we can take the appropriate action just before
680 * leaving the kernel. The one caveat is that if we have
681 * already called the callback, we don't want to do it
682 * again until it gets rearmed.
683 */
684 if ((le->le_flags & LEDGER_ACTION_BLOCK) ||
39236c6e 685 (!(le->le_flags & LF_CALLED_BACK) &&
316670eb 686 entry_get_callback(ledger, entry))) {
5c9f4661 687 act_set_astledger_async(thread);
1c79356b 688 }
316670eb
A
689 } else {
690 /*
39236c6e
A
691 * The balance on the account is below the limit.
692 *
693 * If there are any threads blocked on this entry, now would
316670eb
A
694 * be a good time to wake them up.
695 */
39236c6e 696 if (le->le_flags & LF_WAKE_NEEDED)
316670eb 697 ledger_limit_entry_wakeup(le);
39236c6e
A
698
699 if (le->le_flags & LEDGER_ACTION_CALLBACK) {
700 /*
701 * Client has requested that a callback be invoked whenever
702 * the ledger's balance crosses into or out of the warning
703 * level.
704 */
705 if (warn_level_exceeded(le)) {
706 /*
707 * This ledger's balance is above the warning level.
708 */
709 if ((le->le_flags & LF_WARNED) == 0) {
710 /*
711 * If we are above the warning level and
712 * have not yet invoked the callback,
713 * set the AST so it can be done before returning
714 * to userland.
715 */
5c9f4661 716 act_set_astledger_async(thread);
39236c6e
A
717 }
718 } else {
719 /*
720 * This ledger's balance is below the warning level.
721 */
722 if (le->le_flags & LF_WARNED) {
723 /*
724 * If we are below the warning level and
725 * the LF_WARNED flag is still set, we need
726 * to invoke the callback to let the client
727 * know the ledger balance is now back below
728 * the warning level.
729 */
5c9f4661 730 act_set_astledger_async(thread);
39236c6e
A
731 }
732 }
733 }
1c79356b 734 }
fe8ab488
A
735
736 if ((le->le_flags & LF_PANIC_ON_NEGATIVE) &&
d9a64523
A
737 (le->le_credit < le->le_debit)) {
738 panic("ledger_entry_check_new_balance(%p,%d): negative ledger %p credit:%lld debit:%lld balance:%lld\n",
39037602 739 ledger, entry, le,
d9a64523
A
740 le->le_credit,
741 le->le_debit,
742 le->le_credit - le->le_debit);
fe8ab488 743 }
316670eb 744}
1c79356b 745
39037602 746void
5c9f4661 747ledger_check_new_balance(thread_t thread, ledger_t ledger, int entry)
39037602
A
748{
749 struct ledger_entry *le;
750 assert(entry > 0 && entry <= ledger->l_size);
751 le = &ledger->l_entries[entry];
5c9f4661 752 ledger_entry_check_new_balance(thread, ledger, entry, le);
39037602
A
753}
754
316670eb 755/*
5c9f4661 756 * Add value to an entry in a ledger for a specific thread.
316670eb
A
757 */
758kern_return_t
5c9f4661 759ledger_credit_thread(thread_t thread, ledger_t ledger, int entry, ledger_amount_t amount)
316670eb
A
760{
761 ledger_amount_t old, new;
762 struct ledger_entry *le;
1c79356b 763
316670eb
A
764 if (!ENTRY_VALID(ledger, entry) || (amount < 0))
765 return (KERN_INVALID_VALUE);
766
767 if (amount == 0)
768 return (KERN_SUCCESS);
769
770 le = &ledger->l_entries[entry];
771
772 old = OSAddAtomic64(amount, &le->le_credit);
773 new = old + amount;
5c9f4661
A
774 lprintf(("%p Credit %lld->%lld\n", thread, old, new));
775
d9a64523
A
776 if (thread) {
777 ledger_entry_check_new_balance(thread, ledger, entry, le);
778 }
316670eb
A
779
780 return (KERN_SUCCESS);
1c79356b
A
781}
782
5c9f4661
A
783/*
784 * Add value to an entry in a ledger.
785 */
786kern_return_t
787ledger_credit(ledger_t ledger, int entry, ledger_amount_t amount)
788{
789 return ledger_credit_thread(current_thread(), ledger, entry, amount);
790}
791
d9a64523
A
792/*
793 * Add value to an entry in a ledger; do not check balance after update.
794 */
795kern_return_t
796ledger_credit_nocheck(ledger_t ledger, int entry, ledger_amount_t amount)
797{
798 return ledger_credit_thread(NULL, ledger, entry, amount);
799}
800
fe8ab488
A
801/* Add all of one ledger's values into another.
802 * They must have been created from the same template.
803 * This is not done atomically. Another thread (if not otherwise synchronized)
804 * may see bogus values when comparing one entry to another.
805 * As each entry's credit & debit are modified one at a time, the warning/limit
806 * may spuriously trip, or spuriously fail to trip, or another thread (if not
807 * otherwise synchronized) may see a bogus balance.
808 */
809kern_return_t
810ledger_rollup(ledger_t to_ledger, ledger_t from_ledger)
811{
812 int i;
fe8ab488
A
813
814 assert(to_ledger->l_template == from_ledger->l_template);
815
816 for (i = 0; i < to_ledger->l_size; i++) {
743345f9
A
817 ledger_rollup_entry(to_ledger, from_ledger, i);
818 }
819
820 return (KERN_SUCCESS);
821}
822
823/* Add one ledger entry value to another.
824 * They must have been created from the same template.
825 * Since the credit and debit values are added one
826 * at a time, other thread might read the a bogus value.
827 */
828kern_return_t
829ledger_rollup_entry(ledger_t to_ledger, ledger_t from_ledger, int entry)
830{
831 struct ledger_entry *from_le, *to_le;
832
833 assert(to_ledger->l_template == from_ledger->l_template);
834 if (ENTRY_VALID(from_ledger, entry) && ENTRY_VALID(to_ledger, entry)) {
835 from_le = &from_ledger->l_entries[entry];
836 to_le = &to_ledger->l_entries[entry];
837 OSAddAtomic64(from_le->le_credit, &to_le->le_credit);
838 OSAddAtomic64(from_le->le_debit, &to_le->le_debit);
fe8ab488
A
839 }
840
841 return (KERN_SUCCESS);
842}
843
39236c6e
A
844/*
845 * Zero the balance of a ledger by adding to its credit or debit, whichever is smaller.
846 * Note that some clients of ledgers (notably, task wakeup statistics) require that
847 * le_credit only ever increase as a function of ledger_credit().
848 */
849kern_return_t
850ledger_zero_balance(ledger_t ledger, int entry)
851{
852 struct ledger_entry *le;
813fb2f6 853 ledger_amount_t debit, credit;
39236c6e
A
854
855 if (!ENTRY_VALID(ledger, entry))
856 return (KERN_INVALID_VALUE);
857
858 le = &ledger->l_entries[entry];
859
860top:
813fb2f6
A
861 debit = le->le_debit;
862 credit = le->le_credit;
863
39037602
A
864 if (le->le_flags & LF_TRACK_CREDIT_ONLY) {
865 assert(le->le_debit == 0);
813fb2f6 866 if (!OSCompareAndSwap64(credit, 0, &le->le_credit)) {
39037602
A
867 goto top;
868 }
869 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le->le_credit, 0));
813fb2f6
A
870 } else if (credit > debit) {
871 if (!OSCompareAndSwap64(debit, credit, &le->le_debit))
39236c6e
A
872 goto top;
873 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le->le_debit, le->le_credit));
813fb2f6
A
874 } else if (credit < debit) {
875 if (!OSCompareAndSwap64(credit, debit, &le->le_credit))
39236c6e
A
876 goto top;
877 lprintf(("%p zeroed %lld->%lld\n", current_thread(), le->le_credit, le->le_debit));
878 }
879
880 return (KERN_SUCCESS);
881}
882
883kern_return_t
884ledger_get_limit(ledger_t ledger, int entry, ledger_amount_t *limit)
885{
886 struct ledger_entry *le;
887
888 if (!ENTRY_VALID(ledger, entry))
889 return (KERN_INVALID_VALUE);
890
891 le = &ledger->l_entries[entry];
892 *limit = le->le_limit;
893
894 lprintf(("ledger_get_limit: %lld\n", *limit));
895
896 return (KERN_SUCCESS);
897}
316670eb 898
1c79356b 899/*
316670eb
A
900 * Adjust the limit of a limited resource. This does not affect the
901 * current balance, so the change doesn't affect the thread until the
902 * next refill.
39236c6e
A
903 *
904 * warn_level: If non-zero, causes the callback to be invoked when
905 * the balance exceeds this level. Specified as a percentage [of the limit].
1c79356b 906 */
316670eb 907kern_return_t
39236c6e
A
908ledger_set_limit(ledger_t ledger, int entry, ledger_amount_t limit,
909 uint8_t warn_level_percentage)
1c79356b 910{
316670eb 911 struct ledger_entry *le;
1c79356b 912
316670eb
A
913 if (!ENTRY_VALID(ledger, entry))
914 return (KERN_INVALID_VALUE);
915
39236c6e 916 lprintf(("ledger_set_limit: %lld\n", limit));
316670eb 917 le = &ledger->l_entries[entry];
39236c6e
A
918
919 if (limit == LEDGER_LIMIT_INFINITY) {
920 /*
921 * Caller wishes to disable the limit. This will implicitly
922 * disable automatic refill, as refills implicitly depend
923 * on the limit.
924 */
925 ledger_disable_refill(ledger, entry);
926 }
927
316670eb 928 le->le_limit = limit;
5ba3f43e
A
929 if (le->le_flags & LF_REFILL_SCHEDULED) {
930 assert(!(le->le_flags & LF_TRACKING_MAX));
931 le->_le.le_refill.le_last_refill = 0;
932 }
39236c6e
A
933 flag_clear(&le->le_flags, LF_CALLED_BACK);
934 flag_clear(&le->le_flags, LF_WARNED);
316670eb
A
935 ledger_limit_entry_wakeup(le);
936
39236c6e
A
937 if (warn_level_percentage != 0) {
938 assert(warn_level_percentage <= 100);
939 assert(limit > 0); /* no negative limit support for warnings */
940 assert(limit != LEDGER_LIMIT_INFINITY); /* warn % without limit makes no sense */
941 le->le_warn_level = (le->le_limit * warn_level_percentage) / 100;
942 } else {
943 le->le_warn_level = LEDGER_LIMIT_INFINITY;
944 }
945
946 return (KERN_SUCCESS);
947}
948
d9a64523 949#if CONFIG_LEDGER_INTERVAL_MAX
39236c6e 950kern_return_t
d9a64523
A
951ledger_get_interval_max(ledger_t ledger, int entry,
952 ledger_amount_t *max_interval_balance, int reset)
39236c6e 953{
d9a64523 954 struct ledger_entry *le;
39236c6e
A
955 le = &ledger->l_entries[entry];
956
957 if (!ENTRY_VALID(ledger, entry) || !(le->le_flags & LF_TRACKING_MAX)) {
958 return (KERN_INVALID_VALUE);
959 }
960
d9a64523
A
961 *max_interval_balance = le->_le._le_max.le_interval_max;
962 lprintf(("ledger_get_interval_max: %lld%s\n", *max_interval_balance,
963 (reset) ? " --> 0" : ""));
5ba3f43e 964
d9a64523
A
965 if (reset) {
966 le->_le._le_max.le_interval_max = 0;
967 }
39236c6e
A
968
969 return (KERN_SUCCESS);
970}
d9a64523 971#endif /* CONFIG_LEDGER_INTERVAL_MAX */
39236c6e 972
5ba3f43e
A
973kern_return_t
974ledger_get_lifetime_max(ledger_t ledger, int entry,
975 ledger_amount_t *max_lifetime_balance)
976{
977 struct ledger_entry *le;
978 le = &ledger->l_entries[entry];
979
980 if (!ENTRY_VALID(ledger, entry) || !(le->le_flags & LF_TRACKING_MAX)) {
981 return (KERN_INVALID_VALUE);
982 }
983
d9a64523 984 *max_lifetime_balance = le->_le._le_max.le_lifetime_max;
5ba3f43e
A
985 lprintf(("ledger_get_lifetime_max: %lld\n", *max_lifetime_balance));
986
987 return (KERN_SUCCESS);
988}
989
39236c6e
A
990/*
991 * Enable tracking of periodic maximums for this ledger entry.
992 */
993kern_return_t
994ledger_track_maximum(ledger_template_t template, int entry,
995 __unused int period_in_secs)
996{
997 template_lock(template);
998
999 if ((entry < 0) || (entry >= template->lt_cnt)) {
1000 template_unlock(template);
1001 return (KERN_INVALID_VALUE);
1002 }
1003
5ba3f43e
A
1004 /* Refill is incompatible with max tracking. */
1005 if (template->lt_entries[entry].et_flags & LF_REFILL_SCHEDULED) {
1006 return (KERN_INVALID_VALUE);
1007 }
1008
39236c6e 1009 template->lt_entries[entry].et_flags |= LF_TRACKING_MAX;
5ba3f43e 1010 template_unlock(template);
39236c6e 1011
316670eb
A
1012 return (KERN_SUCCESS);
1013}
1014
fe8ab488
A
1015kern_return_t
1016ledger_panic_on_negative(ledger_template_t template, int entry)
1017{
1018 template_lock(template);
1019
1020 if ((entry < 0) || (entry >= template->lt_cnt)) {
39037602 1021 template_unlock(template);
fe8ab488
A
1022 return (KERN_INVALID_VALUE);
1023 }
1024
1025 template->lt_entries[entry].et_flags |= LF_PANIC_ON_NEGATIVE;
1026
39037602 1027 template_unlock(template);
fe8ab488
A
1028
1029 return (KERN_SUCCESS);
1030}
39037602
A
1031
1032kern_return_t
1033ledger_track_credit_only(ledger_template_t template, int entry)
1034{
1035 template_lock(template);
1036
1037 if ((entry < 0) || (entry >= template->lt_cnt)) {
1038 template_unlock(template);
1039 return (KERN_INVALID_VALUE);
1040 }
1041
1042 template->lt_entries[entry].et_flags |= LF_TRACK_CREDIT_ONLY;
1043
1044 template_unlock(template);
1045
1046 return (KERN_SUCCESS);
1047}
1048
316670eb 1049/*
39236c6e 1050 * Add a callback to be executed when the resource goes into deficit.
316670eb
A
1051 */
1052kern_return_t
1053ledger_set_callback(ledger_template_t template, int entry,
1054 ledger_callback_t func, const void *param0, const void *param1)
1055{
1056 struct entry_template *et;
1057 struct ledger_callback *old_cb, *new_cb;
1058
1059 if ((entry < 0) || (entry >= template->lt_cnt))
1060 return (KERN_INVALID_VALUE);
1061
1062 if (func) {
1063 new_cb = (struct ledger_callback *)kalloc(sizeof (*new_cb));
1064 new_cb->lc_func = func;
1065 new_cb->lc_param0 = param0;
1066 new_cb->lc_param1 = param1;
1067 } else {
1068 new_cb = NULL;
1c79356b 1069 }
1c79356b 1070
316670eb
A
1071 template_lock(template);
1072 et = &template->lt_entries[entry];
1073 old_cb = et->et_callback;
1074 et->et_callback = new_cb;
1075 template_unlock(template);
1076 if (old_cb)
1077 kfree(old_cb, sizeof (*old_cb));
1c79356b 1078
316670eb
A
1079 return (KERN_SUCCESS);
1080}
1c79356b 1081
316670eb
A
1082/*
1083 * Disable callback notification for a specific ledger entry.
1084 *
1085 * Otherwise, if using a ledger template which specified a
1086 * callback function (ledger_set_callback()), it will be invoked when
1087 * the resource goes into deficit.
1088 */
1089kern_return_t
1090ledger_disable_callback(ledger_t ledger, int entry)
1091{
1092 if (!ENTRY_VALID(ledger, entry))
1093 return (KERN_INVALID_VALUE);
1094
39236c6e
A
1095 /*
1096 * le_warn_level is used to indicate *if* this ledger has a warning configured,
1097 * in addition to what that warning level is set to.
1098 * This means a side-effect of ledger_disable_callback() is that the
1099 * warning level is forgotten.
1100 */
1101 ledger->l_entries[entry].le_warn_level = LEDGER_LIMIT_INFINITY;
316670eb
A
1102 flag_clear(&ledger->l_entries[entry].le_flags, LEDGER_ACTION_CALLBACK);
1103 return (KERN_SUCCESS);
1c79356b
A
1104}
1105
1106/*
39236c6e
A
1107 * Enable callback notification for a specific ledger entry.
1108 *
1109 * This is only needed if ledger_disable_callback() has previously
1110 * been invoked against an entry; there must already be a callback
1111 * configured.
1c79356b 1112 */
316670eb 1113kern_return_t
39236c6e 1114ledger_enable_callback(ledger_t ledger, int entry)
1c79356b 1115{
316670eb
A
1116 if (!ENTRY_VALID(ledger, entry))
1117 return (KERN_INVALID_VALUE);
1c79356b 1118
39236c6e
A
1119 assert(entry_get_callback(ledger, entry) != NULL);
1120
1121 flag_set(&ledger->l_entries[entry].le_flags, LEDGER_ACTION_CALLBACK);
1122 return (KERN_SUCCESS);
1123}
1124
1125/*
1126 * Query the automatic refill period for this ledger entry.
1127 *
1128 * A period of 0 means this entry has none configured.
1129 */
1130kern_return_t
1131ledger_get_period(ledger_t ledger, int entry, uint64_t *period)
1132{
1133 struct ledger_entry *le;
1134
1135 if (!ENTRY_VALID(ledger, entry))
1136 return (KERN_INVALID_VALUE);
1137
1138 le = &ledger->l_entries[entry];
1139 *period = abstime_to_nsecs(le->_le.le_refill.le_refill_period);
1140 lprintf(("ledger_get_period: %llx\n", *period));
316670eb 1141 return (KERN_SUCCESS);
1c79356b
A
1142}
1143
1144/*
316670eb 1145 * Adjust the automatic refill period.
1c79356b 1146 */
316670eb
A
1147kern_return_t
1148ledger_set_period(ledger_t ledger, int entry, uint64_t period)
1c79356b 1149{
316670eb 1150 struct ledger_entry *le;
1c79356b 1151
316670eb
A
1152 lprintf(("ledger_set_period: %llx\n", period));
1153 if (!ENTRY_VALID(ledger, entry))
1154 return (KERN_INVALID_VALUE);
1c79356b 1155
316670eb 1156 le = &ledger->l_entries[entry];
1c79356b 1157
39236c6e
A
1158 /*
1159 * A refill period refills the ledger in multiples of the limit,
1160 * so if you haven't set one yet, you need a lesson on ledgers.
1161 */
1162 assert(le->le_limit != LEDGER_LIMIT_INFINITY);
1163
1164 if (le->le_flags & LF_TRACKING_MAX) {
1165 /*
1166 * Refill is incompatible with rolling max tracking.
1167 */
1168 return (KERN_INVALID_VALUE);
1169 }
1170
1171 le->_le.le_refill.le_refill_period = nsecs_to_abstime(period);
1172
1173 /*
1174 * Set the 'starting time' for the next refill to now. Since
1175 * we're resetting the balance to zero here, we consider this
1176 * moment the starting time for accumulating a balance that
1177 * counts towards the limit.
1178 */
1179 le->_le.le_refill.le_last_refill = mach_absolute_time();
1180 ledger_zero_balance(ledger, entry);
1181
1182 flag_set(&le->le_flags, LF_REFILL_SCHEDULED);
1183
1184 return (KERN_SUCCESS);
1185}
1186
1187/*
1188 * Disable automatic refill.
1189 */
1190kern_return_t
1191ledger_disable_refill(ledger_t ledger, int entry)
1192{
1193 struct ledger_entry *le;
1194
1195 if (!ENTRY_VALID(ledger, entry))
1196 return (KERN_INVALID_VALUE);
1197
1198 le = &ledger->l_entries[entry];
1199
1200 flag_clear(&le->le_flags, LF_REFILL_SCHEDULED);
1201
1202 return (KERN_SUCCESS);
1203}
1204
1205kern_return_t
1206ledger_get_actions(ledger_t ledger, int entry, int *actions)
1207{
1208 if (!ENTRY_VALID(ledger, entry))
1209 return (KERN_INVALID_VALUE);
1210
1211 *actions = ledger->l_entries[entry].le_flags & LEDGER_ACTION_MASK;
1212 lprintf(("ledger_get_actions: %#x\n", *actions));
316670eb
A
1213 return (KERN_SUCCESS);
1214}
1215
1216kern_return_t
1217ledger_set_action(ledger_t ledger, int entry, int action)
1218{
39236c6e 1219 lprintf(("ledger_set_action: %#x\n", action));
316670eb
A
1220 if (!ENTRY_VALID(ledger, entry))
1221 return (KERN_INVALID_VALUE);
1222
1223 flag_set(&ledger->l_entries[entry].le_flags, action);
1224 return (KERN_SUCCESS);
1225}
1226
316670eb 1227kern_return_t
5c9f4661 1228ledger_debit_thread(thread_t thread, ledger_t ledger, int entry, ledger_amount_t amount)
316670eb
A
1229{
1230 struct ledger_entry *le;
1231 ledger_amount_t old, new;
1232
1233 if (!ENTRY_VALID(ledger, entry) || (amount < 0))
1234 return (KERN_INVALID_ARGUMENT);
1235
1236 if (amount == 0)
1237 return (KERN_SUCCESS);
1238
1239 le = &ledger->l_entries[entry];
1240
39037602
A
1241 if (le->le_flags & LF_TRACK_CREDIT_ONLY) {
1242 assert(le->le_debit == 0);
1243 old = OSAddAtomic64(-amount, &le->le_credit);
1244 new = old - amount;
1245 } else {
1246 old = OSAddAtomic64(amount, &le->le_debit);
1247 new = old + amount;
1248 }
316670eb 1249 lprintf(("%p Debit %lld->%lld\n", thread, old, new));
39037602 1250
d9a64523
A
1251 if (thread) {
1252 ledger_entry_check_new_balance(thread, ledger, entry, le);
1253 }
5c9f4661 1254
316670eb 1255 return (KERN_SUCCESS);
5c9f4661 1256}
1c79356b 1257
5c9f4661
A
1258kern_return_t
1259ledger_debit(ledger_t ledger, int entry, ledger_amount_t amount)
1260{
1261 return ledger_debit_thread(current_thread(), ledger, entry, amount);
316670eb
A
1262}
1263
d9a64523
A
1264kern_return_t
1265ledger_debit_nocheck(ledger_t ledger, int entry, ledger_amount_t amount)
1266{
1267 return ledger_debit_thread(NULL, ledger, entry, amount);
1268}
1269
316670eb
A
1270void
1271ledger_ast(thread_t thread)
1272{
39236c6e
A
1273 struct ledger *l = thread->t_ledger;
1274 struct ledger *thl;
1275 uint32_t block;
1276 uint64_t now;
1277 uint8_t task_flags;
1278 uint8_t task_percentage;
1279 uint64_t task_interval;
1280
316670eb
A
1281 kern_return_t ret;
1282 task_t task = thread->task;
1283
1284 lprintf(("Ledger AST for %p\n", thread));
1285
1286 ASSERT(task != NULL);
1287 ASSERT(thread == current_thread());
1288
1289top:
39236c6e
A
1290 /*
1291 * Take a self-consistent snapshot of the CPU usage monitor parameters. The task
1292 * can change them at any point (with the task locked).
1293 */
1294 task_lock(task);
1295 task_flags = task->rusage_cpu_flags;
1296 task_percentage = task->rusage_cpu_perthr_percentage;
1297 task_interval = task->rusage_cpu_perthr_interval;
1298 task_unlock(task);
1299
316670eb
A
1300 /*
1301 * Make sure this thread is up to date with regards to any task-wide per-thread
39236c6e 1302 * CPU limit, but only if it doesn't have a thread-private blocking CPU limit.
316670eb 1303 */
39236c6e
A
1304 if (((task_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) != 0) &&
1305 ((thread->options & TH_OPT_PRVT_CPULIMIT) == 0)) {
1306 uint8_t percentage;
1307 uint64_t interval;
1308 int action;
1309
1310 thread_get_cpulimit(&action, &percentage, &interval);
1311
316670eb 1312 /*
39236c6e
A
1313 * If the thread's CPU limits no longer match the task's, or the
1314 * task has a limit but the thread doesn't, update the limit.
316670eb 1315 */
39236c6e
A
1316 if (((thread->options & TH_OPT_PROC_CPULIMIT) == 0) ||
1317 (interval != task_interval) || (percentage != task_percentage)) {
1318 thread_set_cpulimit(THREAD_CPULIMIT_EXCEPTION, task_percentage, task_interval);
1319 assert((thread->options & TH_OPT_PROC_CPULIMIT) != 0);
1320 }
1321 } else if (((task_flags & TASK_RUSECPU_FLAGS_PERTHR_LIMIT) == 0) &&
1322 (thread->options & TH_OPT_PROC_CPULIMIT)) {
1323 assert((thread->options & TH_OPT_PRVT_CPULIMIT) == 0);
1324
316670eb
A
1325 /*
1326 * Task no longer has a per-thread CPU limit; remove this thread's
1327 * corresponding CPU limit.
1328 */
39236c6e 1329 thread_set_cpulimit(THREAD_CPULIMIT_DISABLE, 0, 0);
316670eb 1330 assert((thread->options & TH_OPT_PROC_CPULIMIT) == 0);
1c79356b 1331 }
316670eb
A
1332
1333 /*
1334 * If the task or thread is being terminated, let's just get on with it
1335 */
1336 if ((l == NULL) || !task->active || task->halting || !thread->active)
1337 return;
1338
1339 /*
1340 * Examine all entries in deficit to see which might be eligble for
1341 * an automatic refill, which require callbacks to be issued, and
1342 * which require blocking.
1343 */
1344 block = 0;
1345 now = mach_absolute_time();
1346
39236c6e
A
1347 /*
1348 * Note that thread->t_threadledger may have been changed by the
1349 * thread_set_cpulimit() call above - so don't examine it until afterwards.
1350 */
1351 thl = thread->t_threadledger;
316670eb
A
1352 if (LEDGER_VALID(thl)) {
1353 block |= ledger_check_needblock(thl, now);
1c79356b 1354 }
316670eb 1355 block |= ledger_check_needblock(l, now);
1c79356b 1356
316670eb
A
1357 /*
1358 * If we are supposed to block on the availability of one or more
1359 * resources, find the first entry in deficit for which we should wait.
1360 * Schedule a refill if necessary and then sleep until the resource
1361 * becomes available.
1362 */
1363 if (block) {
1364 if (LEDGER_VALID(thl)) {
1365 ret = ledger_perform_blocking(thl);
1366 if (ret != KERN_SUCCESS)
1367 goto top;
1c79356b 1368 }
316670eb
A
1369 ret = ledger_perform_blocking(l);
1370 if (ret != KERN_SUCCESS)
1371 goto top;
1372 } /* block */
1373}
1c79356b 1374
316670eb
A
1375static uint32_t
1376ledger_check_needblock(ledger_t l, uint64_t now)
1377{
1378 int i;
1379 uint32_t flags, block = 0;
1380 struct ledger_entry *le;
1381 struct ledger_callback *lc;
1382
1383
1384 for (i = 0; i < l->l_size; i++) {
1385 le = &l->l_entries[i];
39236c6e
A
1386
1387 lc = entry_get_callback(l, i);
1388
1389 if (limit_exceeded(le) == FALSE) {
1390 if (le->le_flags & LEDGER_ACTION_CALLBACK) {
1391 /*
1392 * If needed, invoke the callback as a warning.
1393 * This needs to happen both when the balance rises above
1394 * the warning level, and also when it dips back below it.
1395 */
1396 assert(lc != NULL);
1397 /*
1398 * See comments for matching logic in ledger_check_new_balance().
1399 */
1400 if (warn_level_exceeded(le)) {
1401 flags = flag_set(&le->le_flags, LF_WARNED);
1402 if ((flags & LF_WARNED) == 0) {
1403 lc->lc_func(LEDGER_WARNING_ROSE_ABOVE, lc->lc_param0, lc->lc_param1);
1404 }
1405 } else {
1406 flags = flag_clear(&le->le_flags, LF_WARNED);
1407 if (flags & LF_WARNED) {
1408 lc->lc_func(LEDGER_WARNING_DIPPED_BELOW, lc->lc_param0, lc->lc_param1);
1409 }
1410 }
1411 }
1412
316670eb 1413 continue;
39236c6e 1414 }
316670eb 1415
39236c6e
A
1416 /* We're over the limit, so refill if we are eligible and past due. */
1417 if (le->le_flags & LF_REFILL_SCHEDULED) {
5ba3f43e
A
1418 assert(!(le->le_flags & LF_TRACKING_MAX));
1419
39236c6e 1420 if ((le->_le.le_refill.le_last_refill + le->_le.le_refill.le_refill_period) > now) {
316670eb
A
1421 ledger_refill(now, l, i);
1422 if (limit_exceeded(le) == FALSE)
1423 continue;
1424 }
1425 }
1426
1427 if (le->le_flags & LEDGER_ACTION_BLOCK)
1428 block = 1;
1429 if ((le->le_flags & LEDGER_ACTION_CALLBACK) == 0)
1430 continue;
39236c6e
A
1431
1432 /*
1433 * If the LEDGER_ACTION_CALLBACK flag is on, we expect there to
1434 * be a registered callback.
1435 */
316670eb 1436 assert(lc != NULL);
39236c6e 1437 flags = flag_set(&le->le_flags, LF_CALLED_BACK);
316670eb 1438 /* Callback has already been called */
39236c6e 1439 if (flags & LF_CALLED_BACK)
316670eb 1440 continue;
39236c6e 1441 lc->lc_func(FALSE, lc->lc_param0, lc->lc_param1);
1c79356b 1442 }
316670eb
A
1443 return(block);
1444}
1c79356b 1445
316670eb
A
1446
1447/* return KERN_SUCCESS to continue, KERN_FAILURE to restart */
1448static kern_return_t
1449ledger_perform_blocking(ledger_t l)
1450{
1451 int i;
1452 kern_return_t ret;
1453 struct ledger_entry *le;
1454
1455 for (i = 0; i < l->l_size; i++) {
1456 le = &l->l_entries[i];
1457 if ((!limit_exceeded(le)) ||
1458 ((le->le_flags & LEDGER_ACTION_BLOCK) == 0))
1459 continue;
1460
5ba3f43e
A
1461 assert(!(le->le_flags & LF_TRACKING_MAX));
1462
316670eb 1463 /* Prepare to sleep until the resource is refilled */
d9a64523 1464 ret = assert_wait_deadline(le, THREAD_INTERRUPTIBLE,
39236c6e 1465 le->_le.le_refill.le_last_refill + le->_le.le_refill.le_refill_period);
316670eb
A
1466 if (ret != THREAD_WAITING)
1467 return(KERN_SUCCESS);
1468
1469 /* Mark that somebody is waiting on this entry */
39236c6e 1470 flag_set(&le->le_flags, LF_WAKE_NEEDED);
316670eb
A
1471
1472 ret = thread_block_reason(THREAD_CONTINUE_NULL, NULL,
1473 AST_LEDGER);
1474 if (ret != THREAD_AWAKENED)
1475 return(KERN_SUCCESS);
1476
1477 /*
1478 * The world may have changed while we were asleep.
1479 * Some other resource we need may have gone into
1480 * deficit. Or maybe we're supposed to die now.
1481 * Go back to the top and reevaluate.
1482 */
1483 return(KERN_FAILURE);
1484 }
1c79356b 1485 return(KERN_SUCCESS);
1c79356b
A
1486}
1487
1c79356b 1488
316670eb
A
1489kern_return_t
1490ledger_get_entries(ledger_t ledger, int entry, ledger_amount_t *credit,
1491 ledger_amount_t *debit)
1492{
1493 struct ledger_entry *le;
1494
1495 if (!ENTRY_VALID(ledger, entry))
1496 return (KERN_INVALID_ARGUMENT);
1497
1498 le = &ledger->l_entries[entry];
1499
1500 *credit = le->le_credit;
1501 *debit = le->le_debit;
1502
1503 return (KERN_SUCCESS);
1504}
1505
fe8ab488
A
1506kern_return_t
1507ledger_reset_callback_state(ledger_t ledger, int entry)
1508{
1509 struct ledger_entry *le;
1510
1511 if (!ENTRY_VALID(ledger, entry))
1512 return (KERN_INVALID_ARGUMENT);
1513
1514 le = &ledger->l_entries[entry];
1515
1516 flag_clear(&le->le_flags, LF_CALLED_BACK);
1517
1518 return (KERN_SUCCESS);
1519}
1520
1521kern_return_t
1522ledger_disable_panic_on_negative(ledger_t ledger, int entry)
1523{
1524 struct ledger_entry *le;
1525
1526 if (!ENTRY_VALID(ledger, entry))
1527 return (KERN_INVALID_ARGUMENT);
1528
1529 le = &ledger->l_entries[entry];
1530
1531 flag_clear(&le->le_flags, LF_PANIC_ON_NEGATIVE);
1532
1533 return (KERN_SUCCESS);
1534}
1535
d9a64523
A
1536kern_return_t
1537ledger_get_panic_on_negative(ledger_t ledger, int entry, int *panic_on_negative)
1538{
1539 struct ledger_entry *le;
1540
1541 if (!ENTRY_VALID(ledger, entry))
1542 return (KERN_INVALID_ARGUMENT);
1543
1544 le = &ledger->l_entries[entry];
1545
1546 if (le->le_flags & LF_PANIC_ON_NEGATIVE) {
1547 *panic_on_negative = TRUE;
1548 } else {
1549 *panic_on_negative = FALSE;
1550 }
1551
1552 return (KERN_SUCCESS);
1553}
1554
39236c6e
A
1555kern_return_t
1556ledger_get_balance(ledger_t ledger, int entry, ledger_amount_t *balance)
1557{
1558 struct ledger_entry *le;
1559
1560 if (!ENTRY_VALID(ledger, entry))
1561 return (KERN_INVALID_ARGUMENT);
1562
1563 le = &ledger->l_entries[entry];
1564
39037602
A
1565 if (le->le_flags & LF_TRACK_CREDIT_ONLY) {
1566 assert(le->le_debit == 0);
1567 } else {
1568 assert((le->le_credit >= 0) && (le->le_debit >= 0));
1569 }
39236c6e
A
1570
1571 *balance = le->le_credit - le->le_debit;
1572
1573 return (KERN_SUCCESS);
1574}
1575
316670eb
A
1576int
1577ledger_template_info(void **buf, int *len)
1c79356b 1578{
316670eb
A
1579 struct ledger_template_info *lti;
1580 struct entry_template *et;
1581 int i;
1582 ledger_t l;
1c79356b 1583
316670eb
A
1584 /*
1585 * Since all tasks share a ledger template, we'll just use the
1586 * caller's as the source.
1587 */
1588 l = current_task()->ledger;
1589 if ((*len < 0) || (l == NULL))
1590 return (EINVAL);
1591
1592 if (*len > l->l_size)
1593 *len = l->l_size;
1594 lti = kalloc((*len) * sizeof (struct ledger_template_info));
1595 if (lti == NULL)
1596 return (ENOMEM);
1597 *buf = lti;
1598
1599 template_lock(l->l_template);
1600 et = l->l_template->lt_entries;
1601
1602 for (i = 0; i < *len; i++) {
1603 memset(lti, 0, sizeof (*lti));
1604 strlcpy(lti->lti_name, et->et_key, LEDGER_NAME_MAX);
1605 strlcpy(lti->lti_group, et->et_group, LEDGER_NAME_MAX);
1606 strlcpy(lti->lti_units, et->et_units, LEDGER_NAME_MAX);
1607 et++;
1608 lti++;
1c79356b 1609 }
316670eb 1610 template_unlock(l->l_template);
1c79356b 1611
316670eb 1612 return (0);
1c79356b
A
1613}
1614
39236c6e
A
1615static void
1616ledger_fill_entry_info(struct ledger_entry *le,
1617 struct ledger_entry_info *lei,
1618 uint64_t now)
1619{
1620 assert(le != NULL);
1621 assert(lei != NULL);
1622
1623 memset(lei, 0, sizeof (*lei));
1624
1625 lei->lei_limit = le->le_limit;
1626 lei->lei_credit = le->le_credit;
1627 lei->lei_debit = le->le_debit;
1628 lei->lei_balance = lei->lei_credit - lei->lei_debit;
1629 lei->lei_refill_period = (le->le_flags & LF_REFILL_SCHEDULED) ?
1630 abstime_to_nsecs(le->_le.le_refill.le_refill_period) : 0;
1631 lei->lei_last_refill = abstime_to_nsecs(now - le->_le.le_refill.le_last_refill);
1632}
1633
316670eb 1634int
39236c6e 1635ledger_get_task_entry_info_multiple(task_t task, void **buf, int *len)
316670eb
A
1636{
1637 struct ledger_entry_info *lei;
1638 struct ledger_entry *le;
1639 uint64_t now = mach_absolute_time();
1640 int i;
1641 ledger_t l;
1642
1643 if ((*len < 0) || ((l = task->ledger) == NULL))
1644 return (EINVAL);
1c79356b 1645
316670eb
A
1646 if (*len > l->l_size)
1647 *len = l->l_size;
1648 lei = kalloc((*len) * sizeof (struct ledger_entry_info));
1649 if (lei == NULL)
1650 return (ENOMEM);
1651 *buf = lei;
1652
1653 le = l->l_entries;
1654
1655 for (i = 0; i < *len; i++) {
39236c6e 1656 ledger_fill_entry_info(le, lei, now);
316670eb
A
1657 le++;
1658 lei++;
1659 }
1660
1661 return (0);
1662}
1663
39236c6e
A
1664void
1665ledger_get_entry_info(ledger_t ledger,
1666 int entry,
1667 struct ledger_entry_info *lei)
1668{
1669 uint64_t now = mach_absolute_time();
1670
1671 assert(ledger != NULL);
1672 assert(lei != NULL);
39236c6e 1673
39037602
A
1674 if (entry >= 0 && entry < ledger->l_size) {
1675 struct ledger_entry *le = &ledger->l_entries[entry];
1676 ledger_fill_entry_info(le, lei, now);
1677 }
39236c6e
A
1678}
1679
316670eb
A
1680int
1681ledger_info(task_t task, struct ledger_info *info)
1c79356b 1682{
316670eb
A
1683 ledger_t l;
1684
1685 if ((l = task->ledger) == NULL)
1686 return (ENOENT);
1c79356b 1687
316670eb 1688 memset(info, 0, sizeof (*info));
1c79356b 1689
316670eb
A
1690 strlcpy(info->li_name, l->l_template->lt_name, LEDGER_NAME_MAX);
1691 info->li_id = l->l_id;
1692 info->li_entries = l->l_size;
1693 return (0);
1c79356b
A
1694}
1695
316670eb
A
1696#ifdef LEDGER_DEBUG
1697int
1698ledger_limit(task_t task, struct ledger_limit_args *args)
1c79356b 1699{
316670eb
A
1700 ledger_t l;
1701 int64_t limit;
1702 int idx;
1703
1704 if ((l = task->ledger) == NULL)
1705 return (EINVAL);
1706
1707 idx = ledger_key_lookup(l->l_template, args->lla_name);
1708 if ((idx < 0) || (idx >= l->l_size))
1709 return (EINVAL);
1710
1711 /*
1712 * XXX - this doesn't really seem like the right place to have
1713 * a context-sensitive conversion of userspace units into kernel
1714 * units. For now I'll handwave and say that the ledger() system
1715 * call isn't meant for civilians to use - they should be using
1716 * the process policy interfaces.
1717 */
1718 if (idx == task_ledgers.cpu_time) {
1719 int64_t nsecs;
1720
1721 if (args->lla_refill_period) {
1722 /*
1723 * If a refill is scheduled, then the limit is
1724 * specified as a percentage of one CPU. The
1725 * syscall specifies the refill period in terms of
1726 * milliseconds, so we need to convert to nsecs.
1727 */
1728 args->lla_refill_period *= 1000000;
1729 nsecs = args->lla_limit *
1730 (args->lla_refill_period / 100);
1731 lprintf(("CPU limited to %lld nsecs per second\n",
1732 nsecs));
1733 } else {
1734 /*
1735 * If no refill is scheduled, then this is a
1736 * fixed amount of CPU time (in nsecs) that can
1737 * be consumed.
1738 */
1739 nsecs = args->lla_limit;
1740 lprintf(("CPU limited to %lld nsecs\n", nsecs));
1741 }
1742 limit = nsecs_to_abstime(nsecs);
1743 } else {
1744 limit = args->lla_limit;
1745 lprintf(("%s limited to %lld\n", args->lla_name, limit));
1746 }
1747
1748 if (args->lla_refill_period > 0)
1749 ledger_set_period(l, idx, args->lla_refill_period);
b0d623f7 1750
316670eb
A
1751 ledger_set_limit(l, idx, limit);
1752 flag_set(&l->l_entries[idx].le_flags, LEDGER_ACTION_BLOCK);
1753 return (0);
1c79356b 1754}
316670eb 1755#endif