diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c index 7bcd94fa8818..036cb0ccb945 100644 --- a/sys/kern/kern_resource.c +++ b/sys/kern/kern_resource.c @@ -1236,6 +1236,14 @@ lim_free(struct plimit *limp) free((void *)limp, M_PLIMIT); } +void +lim_freen(struct plimit *limp, int n) +{ + + if (refcount_releasen(&limp->pl_refcnt, n)) + free((void *)limp, M_PLIMIT); +} + /* * Make a copy of the plimit structure. * We share these structures copy-on-write after fork. diff --git a/sys/kern/kern_thread.c b/sys/kern/kern_thread.c index 2388e1f25669..2c2af713d373 100644 --- a/sys/kern/kern_thread.c +++ b/sys/kern/kern_thread.c @@ -537,6 +537,8 @@ thread_reap(void) struct thread *itd, *ntd; struct tidbatch tidbatch; int tdcount; + struct plimit *lim; + int limcount; /* * Reading upfront is pessimal if followed by concurrent atomic_swap, @@ -552,11 +554,23 @@ thread_reap(void) tidbatch_prep(&tidbatch); tdcount = 0; + lim = NULL; + limcount = 0; while (itd != NULL) { ntd = itd->td_zombie; EVENTHANDLER_DIRECT_INVOKE(thread_dtor, itd); tidbatch_add(&tidbatch, itd); - thread_cow_free(itd); + MPASS(itd->td_realucred != NULL); + crcowfree(itd); + MPASS(itd->td_limit != NULL); + if (lim != itd->td_limit) { + if (limcount != 0) { + lim_freen(lim, limcount); + limcount = 0; + } + } + lim = itd->td_limit; + limcount++; thread_free_batched(itd); tidbatch_process(&tidbatch); tdcount++; @@ -571,6 +585,8 @@ thread_reap(void) if (tdcount != 0) { thread_count_sub(tdcount); } + MPASS(limcount != 0); + lim_freen(lim, limcount); } /* diff --git a/sys/sys/resourcevar.h b/sys/sys/resourcevar.h index c33511f94434..e83b170ca291 100644 --- a/sys/sys/resourcevar.h +++ b/sys/sys/resourcevar.h @@ -145,6 +145,7 @@ rlim_t lim_cur(struct thread *td, int which); rlim_t lim_cur_proc(struct proc *p, int which); void lim_fork(struct proc *p1, struct proc *p2); void lim_free(struct plimit *limp); +void lim_freen(struct plimit *limp, int n); struct plimit *lim_hold(struct plimit *limp); rlim_t lim_max(struct thread *td, int which);