From a5896914f0dece563efea1dd752a715225e39da4 Mon Sep 17 00:00:00 2001 From: Joseph Koshy Date: Tue, 11 Nov 2003 09:09:26 +0000 Subject: [PATCH] Bound the number of iterations a thread can perform inside ktr_resize_pool(); this eliminates a potential livelock. Return ENOSPC only if we encountered an out-of-memory condition when trying to increase the pool size. Reviewed by: jhb, bde (style) --- sys/kern/kern_ktrace.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/sys/kern/kern_ktrace.c b/sys/kern/kern_ktrace.c index 03e08b19418..2ee758fcdeb 100644 --- a/sys/kern/kern_ktrace.c +++ b/sys/kern/kern_ktrace.c @@ -168,7 +168,7 @@ sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS) error = SYSCTL_OUT(req, &oldsize, sizeof(u_int)); if (error) return (error); - if (newsize != wantsize) + if (wantsize > oldsize && newsize < wantsize) return (ENOSPC); return (0); } @@ -179,14 +179,16 @@ static u_int ktrace_resize_pool(u_int newsize) { struct ktr_request *req; + int bound; mtx_assert(&ktrace_mtx, MA_OWNED); print_message = 1; - if (newsize == ktr_requestpool) - return (newsize); - if (newsize < ktr_requestpool) + bound = newsize - ktr_requestpool; + if (bound == 0) + return (ktr_requestpool); + if (bound < 0) /* Shrink pool down to newsize if possible. */ - while (ktr_requestpool > newsize) { + while (bound++ < 0) { req = STAILQ_FIRST(&ktr_free); if (req == NULL) return (ktr_requestpool); @@ -198,7 +200,7 @@ ktrace_resize_pool(u_int newsize) } else /* Grow pool up to newsize. */ - while (ktr_requestpool < newsize) { + while (bound-- > 0) { mtx_unlock(&ktrace_mtx); req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);