From cd672ca60f7c08a5189078c18579e12b65606f4c Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Thu, 16 Jul 2015 14:30:11 +0000 Subject: [PATCH] Get rid of lim_update_thread and cred_update_thread. Their primary use was in thread_cow_update to free up old resources. Freeing had to be done with proc lock held and _cow_ funcs already knew how to free old structs. --- sys/kern/init_main.c | 5 ++++- sys/kern/kern_prot.c | 18 ------------------ sys/kern/kern_resource.c | 14 -------------- sys/kern/kern_thread.c | 24 ++++++++++++++++++------ sys/sys/resourcevar.h | 2 -- sys/sys/ucred.h | 1 - 6 files changed, 22 insertions(+), 42 deletions(-) diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index 7552d517200f..a362d00fbfdf 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -827,6 +827,7 @@ static void create_init(const void *udata __unused) { struct ucred *newcred, *oldcred; + struct thread *td; int error; error = fork1(&thread0, RFFDG | RFPROC | RFSTOPPED, 0, &initproc, @@ -850,7 +851,9 @@ create_init(const void *udata __unused) audit_cred_proc1(newcred); #endif proc_set_cred(initproc, newcred); - cred_update_thread(FIRST_THREAD_IN_PROC(initproc)); + td = FIRST_THREAD_IN_PROC(initproc); + crfree(td->td_ucred); + td->td_ucred = crhold(initproc->p_ucred); PROC_UNLOCK(initproc); sx_xunlock(&proctree_lock); crfree(oldcred); diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c index b531763b8b14..d7ec70134ddf 100644 --- a/sys/kern/kern_prot.c +++ b/sys/kern/kern_prot.c @@ -1934,24 +1934,6 @@ cru2x(struct ucred *cr, struct xucred *xcr) ngroups * sizeof(*cr->cr_groups)); } -/* - * small routine to swap a thread's current ucred for the correct one taken - * from the process. - */ -void -cred_update_thread(struct thread *td) -{ - struct proc *p; - struct ucred *cred; - - p = td->td_proc; - cred = td->td_ucred; - PROC_LOCK_ASSERT(p, MA_OWNED); - td->td_ucred = crhold(p->p_ucred); - if (cred != NULL) - crfree(cred); -} - /* * Set initial process credentials. * Callers are responsible for providing the reference for provided credentials. diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c index 48427637338f..b6dbab99188c 100644 --- a/sys/kern/kern_resource.c +++ b/sys/kern/kern_resource.c @@ -1436,17 +1436,3 @@ chgkqcnt(struct uidinfo *uip, int diff, rlim_t max) return (chglimit(uip, &uip->ui_kqcnt, diff, max, "kqcnt")); } - -void -lim_update_thread(struct thread *td) -{ - struct proc *p; - struct plimit *lim; - - p = td->td_proc; - lim = td->td_limit; - PROC_LOCK_ASSERT(p, MA_OWNED); - td->td_limit = lim_hold(p->p_limit); - if (lim != NULL) - lim_free(lim); -} diff --git a/sys/kern/kern_thread.c b/sys/kern/kern_thread.c index 0f65403f77fa..3c6232cc1e21 100644 --- a/sys/kern/kern_thread.c +++ b/sys/kern/kern_thread.c @@ -409,9 +409,9 @@ void thread_cow_free(struct thread *td) { - if (td->td_ucred) + if (td->td_ucred != NULL) crfree(td->td_ucred); - if (td->td_limit) + if (td->td_limit != NULL) lim_free(td->td_limit); } @@ -419,15 +419,27 @@ void thread_cow_update(struct thread *td) { struct proc *p; + struct ucred *oldcred; + struct plimit *oldlimit; p = td->td_proc; + oldcred = NULL; + oldlimit = NULL; PROC_LOCK(p); - if (td->td_ucred != p->p_ucred) - cred_update_thread(td); - if (td->td_limit != p->p_limit) - lim_update_thread(td); + if (td->td_ucred != p->p_ucred) { + oldcred = td->td_ucred; + td->td_ucred = crhold(p->p_ucred); + } + if (td->td_limit != p->p_limit) { + oldlimit = td->td_limit; + td->td_limit = lim_hold(p->p_limit); + } td->td_cowgen = p->p_cowgen; PROC_UNLOCK(p); + if (oldcred != NULL) + crfree(oldcred); + if (oldlimit != NULL) + lim_free(oldlimit); } /* diff --git a/sys/sys/resourcevar.h b/sys/sys/resourcevar.h index d374472c8269..9612f12aacea 100644 --- a/sys/sys/resourcevar.h +++ b/sys/sys/resourcevar.h @@ -159,7 +159,5 @@ void ui_racct_foreach(void (*callback)(struct racct *racct, void *arg2, void *arg3), void *arg2, void *arg3); #endif -void lim_update_thread(struct thread *td); - #endif /* _KERNEL */ #endif /* !_SYS_RESOURCEVAR_H_ */ diff --git a/sys/sys/ucred.h b/sys/sys/ucred.h index 9a453087ff0e..d7e7fa5a947c 100644 --- a/sys/sys/ucred.h +++ b/sys/sys/ucred.h @@ -105,7 +105,6 @@ void change_svuid(struct ucred *newcred, uid_t svuid); void crcopy(struct ucred *dest, struct ucred *src); struct ucred *crcopysafe(struct proc *p, struct ucred *cr); struct ucred *crdup(struct ucred *cr); -void cred_update_thread(struct thread *td); void proc_set_cred_init(struct proc *p, struct ucred *cr); struct ucred *proc_set_cred(struct proc *p, struct ucred *cr); void crfree(struct ucred *cr);