Changeset View
Changeset View
Standalone View
Standalone View
sys/kern/kern_proc.c
Show First 20 Lines • Show All 172 Lines • ▼ Show 20 Lines | |||||
/* | /* | ||||
* Initialize global process hashing structures. | * Initialize global process hashing structures. | ||||
*/ | */ | ||||
void | void | ||||
procinit(void) | procinit(void) | ||||
{ | { | ||||
u_long i; | u_long i; | ||||
char buf[16]; | |||||
sx_init(&allproc_lock, "allproc"); | sx_init(&allproc_lock, "allproc"); | ||||
sx_init(&zombproc_lock, "zombproc"); | sx_init(&zombproc_lock, "zombproc"); | ||||
sx_init(&proctree_lock, "proctree"); | sx_init(&proctree_lock, "proctree"); | ||||
mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); | mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); | ||||
mtx_init(&procid_lock, "procid", NULL, MTX_DEF); | mtx_init(&procid_lock, "procid", NULL, MTX_DEF); | ||||
LIST_INIT(&allproc); | LIST_INIT(&allproc); | ||||
LIST_INIT(&zombproc); | LIST_INIT(&zombproc); | ||||
pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); | pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); | ||||
pidhashlock = (pidhash + 1) / 64; | pidhashlock = (pidhash + 1) / 64; | ||||
if (pidhashlock > 0) | if (pidhashlock > 0) | ||||
pidhashlock--; | pidhashlock--; | ||||
pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1), | pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1), | ||||
M_PROC, M_WAITOK | M_ZERO); | M_PROC, M_WAITOK | M_ZERO); | ||||
for (i = 0; i < pidhashlock + 1; i++) | for (i = 0; i < pidhashlock + 1; i++) { | ||||
sx_init(&pidhashtbl_lock[i], "pidhash"); | snprintf(buf, sizeof(buf), "pidhash%ju", (uintmax_t)i); | ||||
sx_init(&pidhashtbl_lock[i], buf); | |||||
mjg: If this is to appease WITNESS you can add SX_DUPOK instead. No opinion one way or the other… | |||||
rmacklemAuthorUnsubmitted Done Inline ActionsI don't have a strong opinion either, but this way broken code that rmacklem: I don't have a strong opinion either, but this way broken code that
does acquire the same lock… | |||||
} | |||||
pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); | pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); | ||||
proc_zone = uma_zcreate("PROC", sched_sizeof_proc(), | proc_zone = uma_zcreate("PROC", sched_sizeof_proc(), | ||||
proc_ctor, proc_dtor, proc_init, proc_fini, | proc_ctor, proc_dtor, proc_init, proc_fini, | ||||
UMA_ALIGN_PTR, UMA_ZONE_NOFREE); | UMA_ALIGN_PTR, UMA_ZONE_NOFREE); | ||||
uihashinit(); | uihashinit(); | ||||
} | } | ||||
/* | /* | ||||
▲ Show 20 Lines • Show All 165 Lines • ▼ Show 20 Lines | |||||
/* | /* | ||||
* Locate a process by number. | * Locate a process by number. | ||||
* | * | ||||
* By not returning processes in the PRS_NEW state, we allow callers to avoid | * By not returning processes in the PRS_NEW state, we allow callers to avoid | ||||
* testing for that condition to avoid dereferencing p_ucred, et al. | * testing for that condition to avoid dereferencing p_ucred, et al. | ||||
*/ | */ | ||||
static __always_inline struct proc * | static __always_inline struct proc * | ||||
_pfind(pid_t pid, bool zombie) | _pfind(pid_t pid, bool zombie) | ||||
{ | { | ||||
Not Done Inline ActionsThe condition can be written as i <= pidhashlock kib: The condition can be written as `i <= pidhashlock` | |||||
Done Inline ActionsI just copied the line in procinit() that initializes them, so that the code is rmacklem: I just copied the line in procinit() that initializes them, so that the code is
consistent with… | |||||
struct proc *p; | struct proc *p; | ||||
p = curproc; | p = curproc; | ||||
if (p->p_pid == pid) { | if (p->p_pid == pid) { | ||||
PROC_LOCK(p); | PROC_LOCK(p); | ||||
return (p); | return (p); | ||||
} | } | ||||
sx_slock(PIDHASHLOCK(pid)); | sx_slock(PIDHASHLOCK(pid)); | ||||
LIST_FOREACH(p, PIDHASH(pid), p_hash) { | LIST_FOREACH(p, PIDHASH(pid), p_hash) { | ||||
if (p->p_pid == pid) { | if (p->p_pid == pid) { | ||||
PROC_LOCK(p); | PROC_LOCK(p); | ||||
if (p->p_state == PRS_NEW || | if (p->p_state == PRS_NEW || | ||||
(!zombie && p->p_state == PRS_ZOMBIE)) { | (!zombie && p->p_state == PRS_ZOMBIE)) { | ||||
PROC_UNLOCK(p); | PROC_UNLOCK(p); | ||||
p = NULL; | p = NULL; | ||||
} | } | ||||
break; | break; | ||||
Done Inline ActionsBut now pfind and pfind_locked have different semantics with the latter not returning zombies. At the very least this needs a comment. Or you can rename this to pfind_any_locked and you are set. mjg: But now pfind and pfind_locked have different semantics with the latter not returning zombies. | |||||
} | } | ||||
} | } | ||||
sx_sunlock(PIDHASHLOCK(pid)); | sx_sunlock(PIDHASHLOCK(pid)); | ||||
return (p); | return (p); | ||||
} | } | ||||
struct proc * | struct proc * | ||||
pfind(pid_t pid) | pfind(pid_t pid) | ||||
▲ Show 20 Lines • Show All 2,802 Lines • Show Last 20 Lines |
If this is to appease WITNESS you can add SX_DUPOK instead. No opinion one way or the other, but most of the time these kind of locks are not numbered.