Module Name:    src
Committed By:   pgoyette
Date:           Wed Jun  9 23:22:51 UTC 2021

Modified Files:
        src/sys/dev: dev_verbose.h

Log Message:
Use the localcount(9)-based module_hook mechanism to prevent the verbose
modules' code and data being unloaded while in use.  Should prevent some
crashes reported by Riastradh@ to occur during suspend/resume operation.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/dev/dev_verbose.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dev_verbose.h
diff -u src/sys/dev/dev_verbose.h:1.5 src/sys/dev/dev_verbose.h:1.6
--- src/sys/dev/dev_verbose.h:1.5	Tue Jun  1 22:58:03 2021
+++ src/sys/dev/dev_verbose.h	Wed Jun  9 23:22:51 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: dev_verbose.h,v 1.5 2021/06/01 22:58:03 riastradh Exp $ */
+/*	$NetBSD: dev_verbose.h,v 1.6 2021/06/09 23:22:51 pgoyette Exp $ */
 
 /*
  * Redistribution and use in source and binary forms, with or without
@@ -25,6 +25,10 @@
 #ifndef _DEV_DEV_VERBOSE_H_
 #define	_DEV_DEV_VERBOSE_H_
 
+#ifdef _KERNEL
+#include <sys/module_hook.h>
+#endif
+
 const char *dev_findvendor(char *, size_t, const char *, size_t, 
 	const uint16_t *, size_t, uint16_t);
 const char *dev_findproduct(char *, size_t, const char *, size_t, 
@@ -50,33 +54,34 @@ tag ## _findproduct_real(char *buf, size
 
 #ifdef _KERNEL
 
+#define DEV_VERBOSE_KERNEL_DECLARE(tag)					\
+MODULE_HOOK(tag ## _findvendor_hook, const char *,			\
+	(char *, size_t, uint16_t));					\
+MODULE_HOOK(tag ## _findproduct_hook, const char *,			\
+	(char *, size_t, uint16_t, uint16_t));				\
+extern int tag ## verbose_loaded;
+
 #define DEV_VERBOSE_MODULE_DEFINE(tag, deps)				\
 DEV_VERBOSE_COMMON_DEFINE(tag)						\
-extern int tag ## verbose_loaded;					\
+DEV_VERBOSE_KERNEL_DECLARE(tag)						\
 									\
 static int								\
 tag ## verbose_modcmd(modcmd_t cmd, void *arg)				\
 {									\
-	static const char *(*saved_findvendor)(char *, size_t,		\
-	    uint16_t);							\
-	static const char *(*saved_findproduct)(char *, size_t,		\
-	    uint16_t, uint16_t);					\
 									\
 	switch (cmd) {							\
 	case MODULE_CMD_INIT:						\
-		saved_findvendor = tag ## _findvendor;			\
-		saved_findproduct = tag ## _findproduct;		\
-		tag ## _findvendor = tag ## _findvendor_real;		\
-		tag ## _findproduct = tag ## _findproduct_real;		\
+		MODULE_HOOK_SET(tag ## _findvendor_hook,		\
+			tag ## _findvendor_real);			\
+		MODULE_HOOK_SET(tag ## _findproduct_hook,		\
+			tag ## _findproduct_real);			\
 		tag ## verbose_loaded = 1;				\
 		return 0;						\
 	case MODULE_CMD_FINI:						\
-		tag ## _findvendor = saved_findvendor;			\
-		tag ## _findproduct = saved_findproduct;		\
 		tag ## verbose_loaded = 0;				\
+		MODULE_HOOK_UNSET(tag ## _findproduct_hook);		\
+		MODULE_HOOK_UNSET(tag ## _findvendor_hook);		\
 		return 0;						\
-	case MODULE_CMD_AUTOUNLOAD:					\
-		return EBUSY;						\
 	default:							\
 		return ENOTTY;						\
 	}								\
@@ -86,11 +91,15 @@ MODULE(MODULE_CLASS_DRIVER, tag ## verbo
 #endif /* KERNEL */
 
 #define DEV_VERBOSE_DECLARE(tag)					\
-extern const char * (*tag ## _findvendor)(char *, size_t, uint16_t);	\
-extern const char * (*tag ## _findproduct)(char *, size_t, uint16_t, uint16_t)
+extern const char * tag ## _findvendor(char *, size_t, uint16_t);	\
+extern const char * tag ## _findproduct(char *, size_t, uint16_t, uint16_t)
 
 #if defined(_KERNEL)
+
 #define DEV_VERBOSE_DEFINE(tag)						\
+DEV_VERBOSE_KERNEL_DECLARE(tag)						\
+struct tag ## _findvendor_hook_t tag ## _findvendor_hook;		\
+struct tag ## _findproduct_hook_t tag ## _findproduct_hook;		\
 int tag ## verbose_loaded = 0;						\
 									\
 static void								\
@@ -101,46 +110,48 @@ tag ## _load_verbose(void)						\
 		module_autoload(# tag "verbose", MODULE_CLASS_DRIVER);	\
 }									\
 									\
-static const char *							\
-tag ## _findvendor_stub(char *buf, size_t len, uint16_t vendor)		\
+const char *								\
+tag ## _findvendor(char *buf, size_t len, uint16_t vendor)		\
 {									\
+	const char *retval = NULL;					\
 									\
 	tag ## _load_verbose();						\
-	if (tag ## verbose_loaded)					\
-		return tag ## _findvendor(buf, len, vendor);		\
-	else {								\
-		snprintf(buf, len, "vendor %4.4x", vendor);		\
-		return NULL;						\
-	}								\
+	MODULE_HOOK_CALL(tag ## _findvendor_hook, (buf, len, vendor),	\
+		{snprintf(buf, len, "vendor %4.4x", vendor); NULL; },	\
+		retval);						\
+	return retval;							\
 }									\
 									\
-static const char *							\
-tag ## _findproduct_stub(char *buf, size_t len, uint16_t vendor,	\
+const char *								\
+tag ## _findproduct(char *buf, size_t len, uint16_t vendor,		\
     uint16_t product)							\
 {									\
+	const char *retval = NULL;					\
 									\
 	tag ## _load_verbose();						\
-	if (tag ## verbose_loaded)					\
-		return tag ## _findproduct(buf, len, vendor, product);	\
-	else {								\
-		snprintf(buf, len, "product %4.4x", product);		\
-		return NULL;						\
-	}								\
+	MODULE_HOOK_CALL(tag ## _findproduct_hook,			\
+		(buf, len, vendor, product),				\
+		{snprintf(buf, len, "product %4.4x", product); NULL; },	\
+		retval);						\
+	return retval;							\
 }									\
-									\
-const char *(*tag ## _findvendor)(char *, size_t, uint16_t) = 		\
-    tag ## _findvendor_stub;						\
-const char *(*tag ## _findproduct)(char *, size_t, uint16_t, uint16_t) =\
-    tag ## _findproduct_stub						\
 
-#else
+#else	/* _KERNEL */
 
 #define DEV_VERBOSE_DEFINE(tag)						\
 DEV_VERBOSE_COMMON_DEFINE(tag)						\
-const char *(*tag ## _findvendor)(char *, size_t, uint16_t) = 		\
-    tag ## _findvendor_real;						\
-const char *(*tag ## _findproduct)(char *, size_t, uint16_t, uint16_t) =\
-    tag ## _findproduct_real						\
+const char *tag ## _findvendor(char *buf, size_t len, uint16_t vendor)	\
+{									\
+									\
+	return tag ## _findvendor_real(buf, len, vendor);		\
+}									\
+									\
+const char *tag ## _findproduct(char *buf, size_t len, uint16_t vendor,	\
+		uint16_t product)					\
+{									\
+									\
+	return tag ## _findproduct_real(buf, len, vendor, product);	\
+}
 
 #endif /* _KERNEL */
 

Reply via email to