Module Name:    src
Committed By:   pgoyette
Date:           Sat Feb 22 19:51:57 UTC 2020

Modified Files:
        src/sys/kern: kern_module.c

Log Message:
Teach module infrastructure to attach any static evcnts that might be
defined in a module, similar to the automatic invocation of sysctl(9)
setup functions.  As requested by riastradh@


To generate a diff of this commit:
cvs rdiff -u -r1.146 -r1.147 src/sys/kern/kern_module.c

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

Modified files:

Index: src/sys/kern/kern_module.c
diff -u src/sys/kern/kern_module.c:1.146 src/sys/kern/kern_module.c:1.147
--- src/sys/kern/kern_module.c:1.146	Wed Jan 22 22:39:27 2020
+++ src/sys/kern/kern_module.c	Sat Feb 22 19:51:57 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_module.c,v 1.146 2020/01/22 22:39:27 pgoyette Exp $	*/
+/*	$NetBSD: kern_module.c,v 1.147 2020/02/22 19:51:57 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.146 2020/01/22 22:39:27 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.147 2020/02/22 19:51:57 pgoyette Exp $");
 
 #define _MODULE_INTERNAL
 
@@ -56,6 +56,7 @@ __KERNEL_RCSID(0, "$NetBSD: kern_module.
 #include <sys/kthread.h>
 #include <sys/sysctl.h>
 #include <sys/lock.h>
+#include <sys/evcnt.h>
 
 #include <uvm/uvm_extern.h>
 
@@ -983,8 +984,72 @@ module_load_sysctl(module_t *mod)
 			ls_funcp++;
 		}
 	}
-	else
-		error = 0;	/* no setup funcs registered */
+}
+
+/*
+ * module_load_evcnt
+ *
+ * Check to see if a non-builtin module has any static evcnt's defined;
+ * if so, attach them.
+ */
+
+static void
+module_load_evcnt(module_t *mod)
+{
+	struct evcnt * const *ls_evp;
+	void *ls_start;
+	size_t ls_size, count;
+	int error;
+
+	/*
+	 * Built-in modules' static evcnt stuff will be handled
+	 * automatically as part of general kernel initialization
+	 */
+	if (mod->mod_source == MODULE_SOURCE_KERNEL)
+		return;
+
+	error = kobj_find_section(mod->mod_kobj, "link_set_evcnts",
+	    &ls_start, &ls_size);
+	if (error == 0) {
+		count = ls_size / sizeof(*ls_evp);
+		ls_evp = ls_start;
+		while (count--) {
+			evcnt_attach_static(*ls_evp++);
+		}
+	}
+}
+
+/*
+ * module_unload_evcnt
+ *
+ * Check to see if a non-builtin module has any static evcnt's defined;
+ * if so, detach them.
+ */
+
+static void
+module_unload_evcnt(module_t *mod)
+{
+	struct evcnt * const *ls_evp;
+	void *ls_start;
+	size_t ls_size, count;
+	int error;
+
+	/*
+	 * Built-in modules' static evcnt stuff will be handled
+	 * automatically as part of general kernel initialization
+	 */
+	if (mod->mod_source == MODULE_SOURCE_KERNEL)
+		return;
+
+	error = kobj_find_section(mod->mod_kobj, "link_set_evcnts",
+	    &ls_start, &ls_size);
+	if (error == 0) {
+		count = ls_size / sizeof(*ls_evp);
+		ls_evp = (void *)((char *)ls_start + ls_size);
+		while (count--) {
+			evcnt_detach(*--ls_evp);
+		}
+	}
 }
 
 /*
@@ -1307,6 +1372,7 @@ module_do_load(const char *name, bool is
 	}
 
 	module_load_sysctl(mod);	/* Set-up module's sysctl if any */
+	module_load_evcnt(mod);		/* Attach any static evcnt needed */
 
 	/*
 	 * Good, the module loaded successfully.  Put it onto the
@@ -1395,10 +1461,12 @@ module_do_unload(const char *name, bool 
 	if (mod->mod_sysctllog) {
 		sysctl_teardown(&mod->mod_sysctllog);
 	}
+	module_unload_evcnt(mod);
 	error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL);
 	module_active = prev_active;
 	if (error != 0) {
 		module_load_sysctl(mod);	/* re-enable sysctl stuff */
+		module_load_evcnt(mod);		/* and reenable evcnts */
 		module_print("cannot unload module `%s' error=%d", name,
 		    error);
 		return error;

Reply via email to