svn commit: r339463 - in head/sys: kern sys

Conrad Meyer cem at FreeBSD.org
Sat Oct 20 18:08:44 UTC 2018


Author: cem
Date: Sat Oct 20 18:08:43 2018
New Revision: 339463
URL: https://svnweb.freebsd.org/changeset/base/339463

Log:
  Add flags variants to linker_files / stack(9) symbol resolution
  
  Some best-effort consumers may find trylock behavior for stack(9) symbol
  resolution acceptable.  Expose that behavior to such consumers.
  
  This API is ugly.  If in the future the modules and linker file list locking
  is cleaned up such that the linker_files list can be iterated safely without
  acquiring a sleepable lock, this API should be removed.  However, most of
  the time nothing will be holding the linker files lock exclusive and the
  acquisition can proceed.
  
  Reviewed by:	markj
  Sponsored by:	Dell EMC Isilon
  Differential Revision:	https://reviews.freebsd.org/D17620

Modified:
  head/sys/kern/kern_linker.c
  head/sys/kern/subr_stack.c
  head/sys/sys/linker.h
  head/sys/sys/stack.h

Modified: head/sys/kern/kern_linker.c
==============================================================================
--- head/sys/kern/kern_linker.c	Sat Oct 20 18:01:48 2018	(r339462)
+++ head/sys/kern/kern_linker.c	Sat Oct 20 18:08:43 2018	(r339463)
@@ -1021,15 +1021,33 @@ linker_ddb_search_symbol_name(caddr_t value, char *buf
  * obey locking protocols, and offer a significantly less complex interface.
  */
 int
-linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
-    long *offset)
+linker_search_symbol_name_flags(caddr_t value, char *buf, u_int buflen,
+    long *offset, int flags)
 {
 	int error;
 
-	sx_slock(&kld_sx);
+	KASSERT((flags & (M_NOWAIT | M_WAITOK)) != 0 &&
+	    (flags & (M_NOWAIT | M_WAITOK)) != (M_NOWAIT | M_WAITOK),
+	    ("%s: bad flags: 0x%x", __func__, flags));
+
+	if (flags & M_NOWAIT) {
+		if (!sx_try_slock(&kld_sx))
+			return (EWOULDBLOCK);
+	} else
+		sx_slock(&kld_sx);
+
 	error = linker_debug_search_symbol_name(value, buf, buflen, offset);
 	sx_sunlock(&kld_sx);
 	return (error);
+}
+
+int
+linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
+    long *offset)
+{
+
+	return (linker_search_symbol_name_flags(value, buf, buflen, offset,
+	    M_WAITOK));
 }
 
 /*

Modified: head/sys/kern/subr_stack.c
==============================================================================
--- head/sys/kern/subr_stack.c	Sat Oct 20 18:01:48 2018	(r339462)
+++ head/sys/kern/subr_stack.c	Sat Oct 20 18:08:43 2018	(r339463)
@@ -48,7 +48,7 @@ FEATURE(stack, "Support for capturing kernel stack");
 static MALLOC_DEFINE(M_STACK, "stack", "Stack Traces");
 
 static int stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen,
-	    long *offset);
+	    long *offset, int flags);
 static int stack_symbol_ddb(vm_offset_t pc, const char **name, long *offset);
 
 struct stack *
@@ -102,7 +102,7 @@ stack_print(const struct stack *st)
 	KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
 	for (i = 0; i < st->depth; i++) {
 		(void)stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
-		    &offset);
+		    &offset, M_WAITOK);
 		printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
 		    namebuf, offset);
 	}
@@ -120,7 +120,7 @@ stack_print_short(const struct stack *st)
 		if (i > 0)
 			printf(" ");
 		if (stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
-		    &offset) == 0)
+		    &offset, M_WAITOK) == 0)
 			printf("%s+%#lx", namebuf, offset);
 		else
 			printf("%p", (void *)st->pcs[i]);
@@ -165,25 +165,36 @@ stack_print_short_ddb(const struct stack *st)
 #endif
 
 /*
- * Two print routines -- one for use from DDB and DDB-like contexts, the
- * other for use in the live kernel.
+ * Format stack into sbuf from live kernel.
+ *
+ * flags - M_WAITOK or M_NOWAIT (EWOULDBLOCK).
  */
-void
-stack_sbuf_print(struct sbuf *sb, const struct stack *st)
+int
+stack_sbuf_print_flags(struct sbuf *sb, const struct stack *st, int flags)
 {
 	char namebuf[64];
 	long offset;
-	int i;
+	int i, error;
 
 	KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
 	for (i = 0; i < st->depth; i++) {
-		(void)stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
-		    &offset);
+		error = stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
+		    &offset, flags);
+		if (error == EWOULDBLOCK)
+			return (error);
 		sbuf_printf(sb, "#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
 		    namebuf, offset);
 	}
+	return (0);
 }
 
+void
+stack_sbuf_print(struct sbuf *sb, const struct stack *st)
+{
+
+	(void)stack_sbuf_print_flags(sb, st, M_WAITOK);
+}
+
 #if defined(DDB) || defined(WITNESS)
 void
 stack_sbuf_print_ddb(struct sbuf *sb, const struct stack *st)
@@ -246,16 +257,19 @@ stack_ktr(u_int mask, const char *file, int line, cons
  * and bypasses linker locking, and the other that doesn't.
  */
 static int
-stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen, long *offset)
+stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen, long *offset,
+    int flags)
 {
+	int error;
 
-	if (linker_search_symbol_name((caddr_t)pc, namebuf, buflen,
-	    offset) != 0) {
-		*offset = 0;
-		strlcpy(namebuf, "??", buflen);
-		return (ENOENT);
-	} else
-		return (0);
+	error = linker_search_symbol_name_flags((caddr_t)pc, namebuf, buflen,
+	    offset, flags);
+	if (error == 0 || error == EWOULDBLOCK)
+		return (error);
+
+	*offset = 0;
+	strlcpy(namebuf, "??", buflen);
+	return (ENOENT);
 }
 
 static int

Modified: head/sys/sys/linker.h
==============================================================================
--- head/sys/sys/linker.h	Sat Oct 20 18:01:48 2018	(r339462)
+++ head/sys/sys/linker.h	Sat Oct 20 18:08:43 2018	(r339463)
@@ -183,6 +183,8 @@ int linker_ddb_search_symbol_name(caddr_t value, char 
 /*
  * stack(9) helper for situations where kernel locking is required.
  */
+int linker_search_symbol_name_flags(caddr_t value, char *buf, u_int buflen,
+    long *offset, int flags);
 int linker_search_symbol_name(caddr_t value, char *buf, u_int buflen,
     long *offset);
 

Modified: head/sys/sys/stack.h
==============================================================================
--- head/sys/sys/stack.h	Sat Oct 20 18:01:48 2018	(r339462)
+++ head/sys/sys/stack.h	Sat Oct 20 18:08:43 2018	(r339463)
@@ -47,6 +47,8 @@ void		 stack_print_short(const struct stack *);
 void		 stack_print_short_ddb(const struct stack *);
 void		 stack_sbuf_print(struct sbuf *, const struct stack *);
 void		 stack_sbuf_print_ddb(struct sbuf *, const struct stack *);
+int		 stack_sbuf_print_flags(struct sbuf *, const struct stack *,
+		 int);
 #ifdef KTR
 void		 stack_ktr(u_int, const char *, int, const struct stack *,
 		    u_int, int);


More information about the svn-src-head mailing list