Use proper locking macros in RACCT in RCTL.

MFC after:	1 month
Sponsored by:	The FreeBSD Foundation
This commit is contained in:
Edward Tomasz Napierala 2016-04-05 11:30:52 +00:00
parent c77702de74
commit 4c230cdafd
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=297578
2 changed files with 83 additions and 75 deletions

View File

@ -93,6 +93,10 @@ SYSCTL_UINT(_kern_racct, OID_AUTO, pcpu_threshold, CTLFLAG_RW, &pcpu_threshold,
static struct mtx racct_lock;
MTX_SYSINIT(racct_lock, &racct_lock, "racct lock", MTX_DEF);
#define RACCT_LOCK() mtx_lock(&racct_lock)
#define RACCT_UNLOCK() mtx_unlock(&racct_lock)
#define RACCT_LOCK_ASSERT() mtx_assert(&racct_lock, MA_OWNED)
static uma_zone_t racct_zone;
static void racct_sub_racct(struct racct *dest, const struct racct *src);
@ -391,7 +395,7 @@ racct_add_racct(struct racct *dest, const struct racct *src)
int i;
ASSERT_RACCT_ENABLED();
mtx_assert(&racct_lock, MA_OWNED);
RACCT_LOCK_ASSERT();
/*
* Update resource usage in dest.
@ -413,7 +417,7 @@ racct_sub_racct(struct racct *dest, const struct racct *src)
int i;
ASSERT_RACCT_ENABLED();
mtx_assert(&racct_lock, MA_OWNED);
RACCT_LOCK_ASSERT();
/*
* Update resource usage in dest.
@ -466,7 +470,7 @@ racct_destroy_locked(struct racct **racctp)
SDT_PROBE1(racct, , racct, destroy, racctp);
mtx_assert(&racct_lock, MA_OWNED);
RACCT_LOCK_ASSERT();
KASSERT(racctp != NULL, ("NULL racctp"));
KASSERT(*racctp != NULL, ("NULL racct"));
@ -493,9 +497,9 @@ racct_destroy(struct racct **racct)
if (!racct_enable)
return;
mtx_lock(&racct_lock);
RACCT_LOCK();
racct_destroy_locked(racct);
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
}
/*
@ -509,7 +513,7 @@ racct_adjust_resource(struct racct *racct, int resource,
{
ASSERT_RACCT_ENABLED();
mtx_assert(&racct_lock, MA_OWNED);
RACCT_LOCK_ASSERT();
KASSERT(racct != NULL, ("NULL racct"));
racct->r_resources[resource] += amount;
@ -574,9 +578,9 @@ racct_add(struct proc *p, int resource, uint64_t amount)
SDT_PROBE3(racct, , rusage, add, p, resource, amount);
mtx_lock(&racct_lock);
RACCT_LOCK();
error = racct_add_locked(p, resource, amount, 0);
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
return (error);
}
@ -593,9 +597,9 @@ racct_add_force(struct proc *p, int resource, uint64_t amount)
SDT_PROBE3(racct, , rusage, add__force, p, resource, amount);
mtx_lock(&racct_lock);
RACCT_LOCK();
racct_add_locked(p, resource, amount, 1);
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
}
static void
@ -625,9 +629,9 @@ racct_add_cred(struct ucred *cred, int resource, uint64_t amount)
if (!racct_enable)
return;
mtx_lock(&racct_lock);
RACCT_LOCK();
racct_add_cred_locked(cred, resource, amount);
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
}
static int
@ -703,9 +707,9 @@ racct_set(struct proc *p, int resource, uint64_t amount)
SDT_PROBE3(racct, , rusage, set__force, p, resource, amount);
mtx_lock(&racct_lock);
RACCT_LOCK();
error = racct_set_locked(p, resource, amount, 0);
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
return (error);
}
@ -718,9 +722,9 @@ racct_set_force(struct proc *p, int resource, uint64_t amount)
SDT_PROBE3(racct, , rusage, set, p, resource, amount);
mtx_lock(&racct_lock);
RACCT_LOCK();
racct_set_locked(p, resource, amount, 1);
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
}
/*
@ -800,7 +804,7 @@ racct_sub(struct proc *p, int resource, uint64_t amount)
KASSERT(RACCT_CAN_DROP(resource),
("%s: called for non-droppable resource %d", __func__, resource));
mtx_lock(&racct_lock);
RACCT_LOCK();
KASSERT(amount <= p->p_racct->r_resources[resource],
("%s: freeing %ju of resource %d, which is more "
"than allocated %jd for %s (pid %d)", __func__, amount, resource,
@ -808,7 +812,7 @@ racct_sub(struct proc *p, int resource, uint64_t amount)
racct_adjust_resource(p->p_racct, resource, -amount);
racct_sub_cred_locked(p->p_ucred, resource, amount);
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
}
static void
@ -843,9 +847,9 @@ racct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
if (!racct_enable)
return;
mtx_lock(&racct_lock);
RACCT_LOCK();
racct_sub_cred_locked(cred, resource, amount);
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
}
/*
@ -866,7 +870,7 @@ racct_proc_fork(struct proc *parent, struct proc *child)
PROC_LOCK(parent);
PROC_LOCK(child);
mtx_lock(&racct_lock);
RACCT_LOCK();
#ifdef RCTL
error = rctl_proc_fork(parent, child);
@ -896,7 +900,7 @@ racct_proc_fork(struct proc *parent, struct proc *child)
error += racct_add_locked(child, RACCT_NTHR, 1, 0);
out:
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
PROC_UNLOCK(child);
PROC_UNLOCK(parent);
@ -919,10 +923,10 @@ racct_proc_fork_done(struct proc *child)
if (!racct_enable)
return;
mtx_lock(&racct_lock);
RACCT_LOCK();
rctl_enforce(child, RACCT_NPROC, 0);
rctl_enforce(child, RACCT_NTHR, 0);
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
#endif
}
@ -958,7 +962,7 @@ racct_proc_exit(struct proc *p)
pct_estimate = 0;
pct = racct_getpcpu(p, pct_estimate);
mtx_lock(&racct_lock);
RACCT_LOCK();
racct_set_locked(p, RACCT_CPU, runtime, 0);
racct_add_cred_locked(p->p_ucred, RACCT_PCTCPU, pct);
@ -970,7 +974,7 @@ racct_proc_exit(struct proc *p)
racct_set_locked(p, i, 0, 0);
}
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
PROC_UNLOCK(p);
#ifdef RCTL
@ -1003,7 +1007,7 @@ racct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
newpr = newcred->cr_prison;
oldpr = oldcred->cr_prison;
mtx_lock(&racct_lock);
RACCT_LOCK();
if (newuip != olduip) {
racct_sub_racct(olduip->ui_racct, p->p_racct);
racct_add_racct(newuip->ui_racct, p->p_racct);
@ -1020,7 +1024,7 @@ racct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
racct_add_racct(pr->pr_prison_racct->prr_racct,
p->p_racct);
}
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
#ifdef RCTL
rctl_proc_ucred_changed(p, newcred);
@ -1033,12 +1037,10 @@ racct_move(struct racct *dest, struct racct *src)
ASSERT_RACCT_ENABLED();
mtx_lock(&racct_lock);
RACCT_LOCK();
racct_add_racct(dest, src);
racct_sub_racct(src, src);
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
}
static void
@ -1112,7 +1114,7 @@ racct_decay_callback(struct racct *racct, void *dummy1, void *dummy2)
int64_t r_old, r_new;
ASSERT_RACCT_ENABLED();
mtx_assert(&racct_lock, MA_OWNED);
RACCT_LOCK_ASSERT();
r_old = racct->r_resources[RACCT_PCTCPU];
@ -1128,14 +1130,14 @@ static void
racct_decay_pre(void)
{
mtx_lock(&racct_lock);
RACCT_LOCK();
}
static void
racct_decay_post(void)
{
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
}
static void
@ -1203,13 +1205,13 @@ racctd(void)
} else
pct_estimate = 0;
pct = racct_getpcpu(p, pct_estimate);
mtx_lock(&racct_lock);
RACCT_LOCK();
racct_set_locked(p, RACCT_PCTCPU, pct, 1);
racct_set_locked(p, RACCT_CPU, runtime, 0);
racct_set_locked(p, RACCT_WALLCLOCK,
(uint64_t)wallclock.tv_sec * 1000000 +
wallclock.tv_usec, 0);
mtx_unlock(&racct_lock);
RACCT_UNLOCK();
PROC_UNLOCK(p);
}

View File

@ -181,6 +181,13 @@ static uma_zone_t rctl_rule_zone;
static struct rwlock rctl_lock;
RW_SYSINIT(rctl_lock, &rctl_lock, "RCTL lock");
#define RCTL_RLOCK() rw_rlock(&rctl_lock)
#define RCTL_RUNLOCK() rw_runlock(&rctl_lock)
#define RCTL_WLOCK() rw_wlock(&rctl_lock)
#define RCTL_WUNLOCK() rw_wunlock(&rctl_lock)
#define RCTL_LOCK_ASSERT() rw_assert(&rctl_lock, RA_LOCKED)
#define RCTL_WLOCK_ASSERT() rw_assert(&rctl_lock, RA_WLOCKED)
static int rctl_rule_fully_specified(const struct rctl_rule *rule);
static void rctl_rule_to_sbuf(struct sbuf *sb, const struct rctl_rule *rule);
@ -231,7 +238,7 @@ rctl_proc_rule_to_racct(const struct proc *p, const struct rctl_rule *rule)
struct ucred *cred = p->p_ucred;
ASSERT_RACCT_ENABLED();
rw_assert(&rctl_lock, RA_LOCKED);
RCTL_LOCK_ASSERT();
switch (rule->rr_per) {
case RCTL_SUBJECT_TYPE_PROCESS:
@ -258,7 +265,7 @@ rctl_available_resource(const struct proc *p, const struct rctl_rule *rule)
const struct racct *racct;
ASSERT_RACCT_ENABLED();
rw_assert(&rctl_lock, RA_LOCKED);
RCTL_LOCK_ASSERT();
racct = rctl_proc_rule_to_racct(p, rule);
available = rule->rr_amount - racct->r_resources[rule->rr_resource];
@ -277,8 +284,7 @@ rctl_would_exceed(const struct proc *p, const struct rctl_rule *rule,
int64_t available;
ASSERT_RACCT_ENABLED();
rw_assert(&rctl_lock, RA_LOCKED);
RCTL_LOCK_ASSERT();
available = rctl_available_resource(p, rule);
if (available >= amount)
@ -302,7 +308,7 @@ rctl_pcpu_available(const struct proc *p) {
minavailable = INT64_MAX;
limit = 0;
rw_rlock(&rctl_lock);
RCTL_RLOCK();
LIST_FOREACH(link, &p->p_racct->r_rule_links, rrl_next) {
rule = link->rrl_rule;
@ -317,7 +323,7 @@ rctl_pcpu_available(const struct proc *p) {
}
}
rw_runlock(&rctl_lock);
RCTL_RUNLOCK();
/*
* Return slightly less than actual value of the available
@ -352,7 +358,7 @@ rctl_enforce(struct proc *p, int resource, uint64_t amount)
ASSERT_RACCT_ENABLED();
rw_rlock(&rctl_lock);
RCTL_RLOCK();
/*
* There may be more than one matching rule; go through all of them.
@ -460,7 +466,7 @@ rctl_enforce(struct proc *p, int resource, uint64_t amount)
}
}
rw_runlock(&rctl_lock);
RCTL_RUNLOCK();
if (should_deny) {
/*
@ -482,7 +488,7 @@ rctl_get_limit(struct proc *p, int resource)
ASSERT_RACCT_ENABLED();
rw_rlock(&rctl_lock);
RCTL_RLOCK();
/*
* There may be more than one matching rule; go through all of them.
@ -498,7 +504,7 @@ rctl_get_limit(struct proc *p, int resource)
amount = rule->rr_amount;
}
rw_runlock(&rctl_lock);
RCTL_RUNLOCK();
return (amount);
}
@ -514,7 +520,7 @@ rctl_get_available(struct proc *p, int resource)
ASSERT_RACCT_ENABLED();
rw_rlock(&rctl_lock);
RCTL_RLOCK();
/*
* There may be more than one matching rule; go through all of them.
@ -531,7 +537,7 @@ rctl_get_available(struct proc *p, int resource)
minavailable = available;
}
rw_runlock(&rctl_lock);
RCTL_RUNLOCK();
/*
* XXX: Think about this _hard_.
@ -675,9 +681,9 @@ rctl_racct_add_rule(struct racct *racct, struct rctl_rule *rule)
link->rrl_rule = rule;
link->rrl_exceeded = 0;
rw_wlock(&rctl_lock);
RCTL_WLOCK();
LIST_INSERT_HEAD(&racct->r_rule_links, link, rrl_next);
rw_wunlock(&rctl_lock);
RCTL_WUNLOCK();
}
static int
@ -687,7 +693,7 @@ rctl_racct_add_rule_locked(struct racct *racct, struct rctl_rule *rule)
ASSERT_RACCT_ENABLED();
KASSERT(rctl_rule_fully_specified(rule), ("rule not fully specified"));
rw_assert(&rctl_lock, RA_WLOCKED);
RCTL_WLOCK_ASSERT();
link = uma_zalloc(rctl_rule_link_zone, M_NOWAIT);
if (link == NULL)
@ -713,7 +719,7 @@ rctl_racct_remove_rules(struct racct *racct,
struct rctl_rule_link *link, *linktmp;
ASSERT_RACCT_ENABLED();
rw_assert(&rctl_lock, RA_WLOCKED);
RCTL_WLOCK_ASSERT();
LIST_FOREACH_SAFE(link, &racct->r_rule_links, rrl_next, linktmp) {
if (!rctl_rule_matches(link->rrl_rule, filter))
@ -1172,14 +1178,14 @@ static void
rctl_rule_pre_callback(void)
{
rw_wlock(&rctl_lock);
RCTL_WLOCK();
}
static void
rctl_rule_post_callback(void)
{
rw_wunlock(&rctl_lock);
RCTL_WUNLOCK();
}
static void
@ -1189,7 +1195,7 @@ rctl_rule_remove_callback(struct racct *racct, void *arg2, void *arg3)
int found = 0;
ASSERT_RACCT_ENABLED();
rw_assert(&rctl_lock, RA_WLOCKED);
RCTL_WLOCK_ASSERT();
found += rctl_racct_remove_rules(racct, filter);
@ -1210,9 +1216,9 @@ rctl_rule_remove(struct rctl_rule *filter)
if (filter->rr_subject_type == RCTL_SUBJECT_TYPE_PROCESS &&
filter->rr_subject.rs_proc != NULL) {
p = filter->rr_subject.rs_proc;
rw_wlock(&rctl_lock);
RCTL_WLOCK();
found = rctl_racct_remove_rules(p->p_racct, filter);
rw_wunlock(&rctl_lock);
RCTL_WUNLOCK();
if (found)
return (0);
return (ESRCH);
@ -1229,11 +1235,11 @@ rctl_rule_remove(struct rctl_rule *filter)
filter, (void *)&found);
sx_assert(&allproc_lock, SA_LOCKED);
rw_wlock(&rctl_lock);
RCTL_WLOCK();
FOREACH_PROC_IN_SYSTEM(p) {
found += rctl_racct_remove_rules(p->p_racct, filter);
}
rw_wunlock(&rctl_lock);
RCTL_WUNLOCK();
if (found)
return (0);
@ -1460,7 +1466,7 @@ rctl_get_rules_callback(struct racct *racct, void *arg2, void *arg3)
struct sbuf *sb = (struct sbuf *)arg3;
ASSERT_RACCT_ENABLED();
rw_assert(&rctl_lock, RA_LOCKED);
RCTL_LOCK_ASSERT();
LIST_FOREACH(link, &racct->r_rule_links, rrl_next) {
if (!rctl_rule_matches(link->rrl_rule, filter))
@ -1511,7 +1517,7 @@ sys_rctl_get_rules(struct thread *td, struct rctl_get_rules_args *uap)
KASSERT(sb != NULL, ("sbuf_new failed"));
FOREACH_PROC_IN_SYSTEM(p) {
rw_rlock(&rctl_lock);
RCTL_RLOCK();
LIST_FOREACH(link, &p->p_racct->r_rule_links, rrl_next) {
/*
* Non-process rules will be added to the buffer later.
@ -1525,7 +1531,7 @@ sys_rctl_get_rules(struct thread *td, struct rctl_get_rules_args *uap)
rctl_rule_to_sbuf(sb, link->rrl_rule);
sbuf_printf(sb, ",");
}
rw_runlock(&rctl_lock);
RCTL_RUNLOCK();
}
loginclass_racct_foreach(rctl_get_rules_callback,
@ -1612,13 +1618,13 @@ sys_rctl_get_limits(struct thread *td, struct rctl_get_limits_args *uap)
sb = sbuf_new(NULL, buf, bufsize, SBUF_FIXEDLEN);
KASSERT(sb != NULL, ("sbuf_new failed"));
rw_rlock(&rctl_lock);
RCTL_RLOCK();
LIST_FOREACH(link, &filter->rr_subject.rs_proc->p_racct->r_rule_links,
rrl_next) {
rctl_rule_to_sbuf(sb, link->rrl_rule);
sbuf_printf(sb, ",");
}
rw_runlock(&rctl_lock);
RCTL_RUNLOCK();
if (sbuf_error(sb) == ENOMEM) {
error = ERANGE;
goto out;
@ -1743,7 +1749,7 @@ rctl_proc_ucred_changed(struct proc *p, struct ucred *newcred)
* credentials.
*/
rulecnt = 0;
rw_rlock(&rctl_lock);
RCTL_RLOCK();
LIST_FOREACH(link, &p->p_racct->r_rule_links, rrl_next) {
if (link->rrl_rule->rr_subject_type ==
RCTL_SUBJECT_TYPE_PROCESS)
@ -1755,7 +1761,7 @@ rctl_proc_ucred_changed(struct proc *p, struct ucred *newcred)
rulecnt++;
LIST_FOREACH(link, &newprr->prr_racct->r_rule_links, rrl_next)
rulecnt++;
rw_runlock(&rctl_lock);
RCTL_RUNLOCK();
/*
* Create temporary list. We've dropped the rctl_lock in order
@ -1773,7 +1779,7 @@ rctl_proc_ucred_changed(struct proc *p, struct ucred *newcred)
/*
* Assign rules to the newly allocated list entries.
*/
rw_wlock(&rctl_lock);
RCTL_WLOCK();
LIST_FOREACH(link, &p->p_racct->r_rule_links, rrl_next) {
if (link->rrl_rule->rr_subject_type ==
RCTL_SUBJECT_TYPE_PROCESS) {
@ -1841,13 +1847,13 @@ rctl_proc_ucred_changed(struct proc *p, struct ucred *newcred)
newlink, rrl_next);
}
rw_wunlock(&rctl_lock);
RCTL_WUNLOCK();
return;
}
goaround:
rw_wunlock(&rctl_lock);
RCTL_WUNLOCK();
/*
* Rule list changed while we were not holding the rctl_lock.
@ -1879,7 +1885,7 @@ rctl_proc_fork(struct proc *parent, struct proc *child)
ASSERT_RACCT_ENABLED();
KASSERT(parent->p_racct != NULL, ("process without racct; p = %p", parent));
rw_wlock(&rctl_lock);
RCTL_WLOCK();
/*
* Go through limits applicable to the parent and assign them
@ -1908,7 +1914,7 @@ rctl_proc_fork(struct proc *parent, struct proc *child)
}
}
rw_wunlock(&rctl_lock);
RCTL_WUNLOCK();
return (0);
fail:
@ -1918,7 +1924,7 @@ rctl_proc_fork(struct proc *parent, struct proc *child)
rctl_rule_release(link->rrl_rule);
uma_zfree(rctl_rule_link_zone, link);
}
rw_wunlock(&rctl_lock);
RCTL_WUNLOCK();
return (EAGAIN);
}
@ -1932,14 +1938,14 @@ rctl_racct_release(struct racct *racct)
ASSERT_RACCT_ENABLED();
rw_wlock(&rctl_lock);
RCTL_WLOCK();
while (!LIST_EMPTY(&racct->r_rule_links)) {
link = LIST_FIRST(&racct->r_rule_links);
LIST_REMOVE(link, rrl_next);
rctl_rule_release(link->rrl_rule);
uma_zfree(rctl_rule_link_zone, link);
}
rw_wunlock(&rctl_lock);
RCTL_WUNLOCK();
}
static void