From owner-freebsd-hackers@FreeBSD.ORG Fri Oct 3 16:46:15 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 2E4E492D for ; Fri, 3 Oct 2014 16:46:15 +0000 (UTC) Received: from mailgate.gta.com (mailgate.gta.com [199.120.225.23]) by mx1.freebsd.org (Postfix) with ESMTP id B48326ED for ; Fri, 3 Oct 2014 16:46:14 +0000 (UTC) Received: (qmail 79250 invoked by uid 1000); 3 Oct 2014 16:46:13 -0000 Date: Fri, 3 Oct 2014 12:46:13 -0400 From: Larry Baird To: Konstantin Belousov Subject: Re: Kernel/Compiler bug Message-ID: <20141003164613.GA78971@gta.com> References: <20141001031553.GA14360@gta.com> <20141001134044.GA57022@gta.com> <542C8C75.30007@FreeBSD.org> <20141002075537.GU26076@kib.kiev.ua> <20141002140232.GA52387@gta.com> <20141002143345.GY26076@kib.kiev.ua> <20141003015456.GA27080@gta.com> <20141003073517.GC26076@kib.kiev.ua> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20141003073517.GC26076@kib.kiev.ua> User-Agent: Mutt/1.5.23 (2014-03-12) Cc: "freebsd-hackers@freebsd.org" , Ryan Stone , Dimitry Andric , Bryan Drewery X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 03 Oct 2014 16:46:15 -0000 On Fri, Oct 03, 2014 at 10:35:17AM +0300, Konstantin Belousov wrote: > I have several notes. Mostly, it comes from my desire to make the patch > committable. I went ahead of made the changes over lunch. Hopefully the patch below addresses all of of the issues. Are you able to shepherd this into head or should I open a PR? Index: kern_intr.c =================================================================== --- kern_intr.c (revision 44897) +++ kern_intr.c (working copy) @@ -1386,6 +1386,14 @@ } } +#ifdef DEBUG_KERNEL_THREAD_STACK +static int max_kern_thread_stack_used; + +SYSCTL_INT(_kern, OID_AUTO, max_kern_thread_stack_used, CTLFLAG_RD, + &max_kern_thread_stack_used, 0, + "Maxiumum stack depth used by a kernel thread"); +#endif /* DEBUG_KERNEL_THREAD_STACK */ + /* * Main interrupt handling body. * @@ -1407,6 +1415,40 @@ td = curthread; +#ifdef DEBUG_KERNEL_THREAD_STACK + /* + * Track maximum stack used by a kernel thread. + * + * Testing for kernel thread isn't strictly needed. It optimizes the + * execution, since interrupts from usermode will have only the trap + * frame on the stack. + */ + char *bottom_of_stack; + char *current; + int used; + + if (!TRAPF_USERMODE(frame)) { + bottom_of_stack = (char *)(td->td_kstack + td->td_kstack_pages * + PAGE_SIZE - 1); + current = (char *)&ih; + + /* + * Try to detect if interrupt is using kernel thread stack. + * Hardware could use a dedicated stack for interrupt handling. + */ + if (bottom_of_stack > current && + current > (char *)(td->td_kstack - PAGE_SIZE)) { + used = bottom_of_stack - current; + + while (atomic_load_acq_int(&max_kern_thread_stack_used) + < used) { + atomic_store_rel_int(&max_kern_thread_stack_used, + used); + } + } + } +#endif /* DEBUG_KERNEL_THREAD_STACK */ + /* An interrupt with no event or handlers is a stray interrupt. */ if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers)) return (EINVAL); -- ------------------------------------------------------------------------ Larry Baird Global Technology Associates, Inc. 1992-2012 | http://www.gta.com Celebrating Twenty Years of Software Innovation | Orlando, FL Email: lab@gta.com | TEL 407-380-0220