If the credential on an incoming thread is correct, don't bother

reaquiring it. In the same vein, don't bother dropping the thread cred
when goinf ot userland. We are guaranteed to nned it when we come back,
(which we are guaranteed to do).

Reviewed by:	jhb@freebsd.org, bde@freebsd.org (slightly different version)
This commit is contained in:
Julian Elischer 2002-02-17 01:09:56 +00:00
parent 39a2e724e1
commit 2eb927e2bb
6 changed files with 44 additions and 15 deletions

View File

@ -256,9 +256,8 @@ trap(frame)
sticks = td->td_kse->ke_sticks;
td->td_frame = &frame;
KASSERT(td->td_ucred == NULL, ("already have a ucred"));
PROC_LOCK(p);
td->td_ucred = crhold(p->p_ucred);
PROC_UNLOCK(p);
if (td->td_ucred != p->p_ucred)
cred_update_thread(td);
switch (type) {
case T_PRIVINFLT: /* privileged instruction fault */
@ -644,10 +643,12 @@ trap(frame)
userret(td, &frame, sticks);
mtx_assert(&Giant, MA_NOTOWNED);
userout:
#ifdef INVARIANTS
mtx_lock(&Giant);
crfree(td->td_ucred);
mtx_unlock(&Giant);
td->td_ucred = NULL;
#endif
out:
return;
}
@ -954,9 +955,8 @@ syscall(frame)
sticks = td->td_kse->ke_sticks;
td->td_frame = &frame;
KASSERT(td->td_ucred == NULL, ("already have a ucred"));
PROC_LOCK(p);
td->td_ucred = crhold(p->p_ucred);
PROC_UNLOCK(p);
if (td->td_ucred != p->p_ucred)
cred_update_thread(td);
params = (caddr_t)frame.tf_esp + sizeof(int);
code = frame.tf_eax;
orig_tf_eflags = frame.tf_eflags;
@ -1099,10 +1099,12 @@ syscall(frame)
*/
STOPEVENT(p, S_SCX, code);
#ifdef INVARIANTS
mtx_lock(&Giant);
crfree(td->td_ucred);
mtx_unlock(&Giant);
td->td_ucred = NULL;
#endif
#ifdef WITNESS
if (witness_list(td)) {
panic("system call %s returning with mutex(s) held\n",

View File

@ -256,9 +256,8 @@ trap(frame)
sticks = td->td_kse->ke_sticks;
td->td_frame = &frame;
KASSERT(td->td_ucred == NULL, ("already have a ucred"));
PROC_LOCK(p);
td->td_ucred = crhold(p->p_ucred);
PROC_UNLOCK(p);
if (td->td_ucred != p->p_ucred)
cred_update_thread(td);
switch (type) {
case T_PRIVINFLT: /* privileged instruction fault */
@ -644,10 +643,12 @@ trap(frame)
userret(td, &frame, sticks);
mtx_assert(&Giant, MA_NOTOWNED);
userout:
#ifdef INVARIANTS
mtx_lock(&Giant);
crfree(td->td_ucred);
mtx_unlock(&Giant);
td->td_ucred = NULL;
#endif
out:
return;
}
@ -954,9 +955,8 @@ syscall(frame)
sticks = td->td_kse->ke_sticks;
td->td_frame = &frame;
KASSERT(td->td_ucred == NULL, ("already have a ucred"));
PROC_LOCK(p);
td->td_ucred = crhold(p->p_ucred);
PROC_UNLOCK(p);
if (td->td_ucred != p->p_ucred)
cred_update_thread(td);
params = (caddr_t)frame.tf_esp + sizeof(int);
code = frame.tf_eax;
orig_tf_eflags = frame.tf_eflags;
@ -1099,10 +1099,12 @@ syscall(frame)
*/
STOPEVENT(p, S_SCX, code);
#ifdef INVARIANTS
mtx_lock(&Giant);
crfree(td->td_ucred);
mtx_unlock(&Giant);
td->td_ucred = NULL;
#endif
#ifdef WITNESS
if (witness_list(td)) {
panic("system call %s returning with mutex(s) held\n",

View File

@ -799,10 +799,12 @@ fork_exit(callout, arg, frame)
kthread_exit(0);
}
PROC_UNLOCK(p);
#ifdef INVARIANTS
mtx_lock(&Giant);
crfree(td->td_ucred);
mtx_unlock(&Giant);
td->td_ucred = NULL;
#endif
mtx_assert(&Giant, MA_NOTOWNED);
}

View File

@ -1683,6 +1683,27 @@ crdup(cr)
return (newcr);
}
/*
* 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;
p = td->td_proc;
if (td->td_ucred != NULL) {
mtx_lock(&Giant);
crfree(td->td_ucred);
mtx_unlock(&Giant);
td->td_ucred = NULL;
}
PROC_LOCK(p);
td->td_ucred = crhold(p->p_ucred);
PROC_UNLOCK(p);
}
/*
* Get login name, if available.
*/

View File

@ -161,9 +161,8 @@ ast(framep)
p->p_stats->p_prof.pr_ticks = 0;
}
mtx_unlock_spin(&sched_lock);
PROC_LOCK(p);
td->td_ucred = crhold(p->p_ucred);
PROC_UNLOCK(p);
if (td->td_ucred != p->p_ucred)
cred_update_thread(td);
if (flags & KEF_OWEUPC && sflag & PS_PROFIL)
addupc_task(ke, p->p_stats->p_prof.pr_addr, prticks);
if (sflag & PS_ALRMPEND) {
@ -188,10 +187,12 @@ ast(framep)
}
userret(td, framep, sticks);
#ifdef INVARIANTS
mtx_lock(&Giant);
crfree(td->td_ucred);
mtx_unlock(&Giant);
td->td_ucred = NULL;
#endif
s = cpu_critical_enter();
}
mtx_assert(&Giant, MA_NOTOWNED);

View File

@ -83,6 +83,7 @@ struct xucred {
#ifdef _KERNEL
void cred_update_thread(struct thread *td);
void change_egid (struct ucred *newcred, gid_t egid);
void change_euid (struct ucred *newcred, uid_t euid);
void change_rgid (struct ucred *newcred, gid_t rgid);