Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 4 Oct 2014 18:38:14 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r272536 - in head/sys: conf kern sys vm
Message-ID:  <201410041838.s94IcEYm004488@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kib
Date: Sat Oct  4 18:38:14 2014
New Revision: 272536
URL: https://svnweb.freebsd.org/changeset/base/272536

Log:
  Add kernel option KSTACK_USAGE_PROF to sample the stack depth on
  interrupts and report the largest value seen as sysctl
  debug.max_kstack_used.  Useful to estimate how close the kernel stack
  size is to overflow.
  
  In collaboration with:	Larry Baird <lab@gta.com>
  Sponsored by:	The FreeBSD Foundation (kib)
  MFC after:	1 week

Modified:
  head/sys/conf/NOTES
  head/sys/conf/options
  head/sys/kern/kern_intr.c
  head/sys/sys/systm.h
  head/sys/vm/vm_glue.c

Modified: head/sys/conf/NOTES
==============================================================================
--- head/sys/conf/NOTES	Sat Oct  4 18:35:00 2014	(r272535)
+++ head/sys/conf/NOTES	Sat Oct  4 18:38:14 2014	(r272536)
@@ -2958,6 +2958,7 @@ options 	SC_RENDER_DEBUG	# syscons rende
 options 	VFS_BIO_DEBUG	# VFS buffer I/O debugging
 
 options 	KSTACK_MAX_PAGES=32 # Maximum pages to give the kernel stack
+options 	KSTACK_USAGE_PROF
 
 # Adaptec Array Controller driver options
 options 	AAC_DEBUG	# Debugging levels:

Modified: head/sys/conf/options
==============================================================================
--- head/sys/conf/options	Sat Oct  4 18:35:00 2014	(r272535)
+++ head/sys/conf/options	Sat Oct  4 18:38:14 2014	(r272536)
@@ -136,6 +136,7 @@ KDTRACE_FRAME	opt_kdtrace.h
 KN_HASHSIZE	opt_kqueue.h
 KSTACK_MAX_PAGES
 KSTACK_PAGES
+KSTACK_USAGE_PROF
 KTRACE
 KTRACE_REQUEST_POOL	opt_ktrace.h
 LIBICONV

Modified: head/sys/kern/kern_intr.c
==============================================================================
--- head/sys/kern/kern_intr.c	Sat Oct  4 18:35:00 2014	(r272535)
+++ head/sys/kern/kern_intr.c	Sat Oct  4 18:38:14 2014	(r272536)
@@ -28,6 +28,7 @@
 __FBSDID("$FreeBSD$");
 
 #include "opt_ddb.h"
+#include "opt_kstack_usage_prof.h"
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -1396,6 +1397,10 @@ intr_event_handle(struct intr_event *ie,
 
 	td = curthread;
 
+#ifdef KSTACK_USAGE_PROF
+	intr_prof_stack_use(td, frame);
+#endif
+
 	/* An interrupt with no event or handlers is a stray interrupt. */
 	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
 		return (EINVAL);

Modified: head/sys/sys/systm.h
==============================================================================
--- head/sys/sys/systm.h	Sat Oct  4 18:35:00 2014	(r272535)
+++ head/sys/sys/systm.h	Sat Oct  4 18:38:14 2014	(r272536)
@@ -443,4 +443,6 @@ bitcount16(uint32_t x)
 	return (x);
 }
 
+void	intr_prof_stack_use(struct thread *td, struct trapframe *frame);
+
 #endif /* !_SYS_SYSTM_H_ */

Modified: head/sys/vm/vm_glue.c
==============================================================================
--- head/sys/vm/vm_glue.c	Sat Oct  4 18:35:00 2014	(r272535)
+++ head/sys/vm/vm_glue.c	Sat Oct  4 18:38:14 2014	(r272536)
@@ -62,6 +62,7 @@ __FBSDID("$FreeBSD$");
 #include "opt_vm.h"
 #include "opt_kstack_pages.h"
 #include "opt_kstack_max_pages.h"
+#include "opt_kstack_usage_prof.h"
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -98,6 +99,8 @@ __FBSDID("$FreeBSD$");
 #include <vm/vm_pager.h>
 #include <vm/swap_pager.h>
 
+#include <machine/cpu.h>
+
 #ifndef NO_SWAPPING
 static int swapout(struct proc *);
 static void swapclear(struct proc *);
@@ -486,6 +489,52 @@ kstack_cache_init(void *nulll)
 
 SYSINIT(vm_kstacks, SI_SUB_KTHREAD_INIT, SI_ORDER_ANY, kstack_cache_init, NULL);
 
+#ifdef KSTACK_USAGE_PROF
+/*
+ * Track maximum stack used by a thread in kernel.
+ */
+static int max_kstack_used;
+
+SYSCTL_INT(_debug, OID_AUTO, max_kstack_used, CTLFLAG_RD,
+    &max_kstack_used, 0,
+    "Maxiumum stack depth used by a thread in kernel");
+
+void
+intr_prof_stack_use(struct thread *td, struct trapframe *frame)
+{
+	vm_offset_t stack_top;
+	vm_offset_t current;
+	int used, prev_used;
+
+	/*
+	 * Testing for interrupted kernel mode isn't strictly
+	 * needed. It optimizes the execution, since interrupts from
+	 * usermode will have only the trap frame on the stack.
+	 */
+	if (TRAPF_USERMODE(frame))
+		return;
+
+	stack_top = td->td_kstack + td->td_kstack_pages * PAGE_SIZE;
+	current = (vm_offset_t)(uintptr_t)&stack_top;
+
+	/*
+	 * Try to detect if interrupt is using kernel thread stack.
+	 * Hardware could use a dedicated stack for interrupt handling.
+	 */
+	if (stack_top <= current || current < td->td_kstack)
+		return;
+
+	used = stack_top - current;
+	for (;;) {
+		prev_used = max_kstack_used;
+		if (prev_used >= used)
+			break;
+		if (atomic_cmpset_int(&max_kstack_used, prev_used, used))
+			break;
+	}
+}
+#endif /* KSTACK_USAGE_PROF */
+
 #ifndef NO_SWAPPING
 /*
  * Allow a thread's kernel stack to be paged out.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201410041838.s94IcEYm004488>