Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Many years ago someone created a new __link_set_sysctl_f...



details:   https://anonhg.NetBSD.org/src/rev/71c16614d178
branches:  trunk
changeset: 964534:71c16614d178
user:      pgoyette <pgoyette%NetBSD.org@localhost>
date:      Wed Aug 07 00:38:01 2019 +0000

description:
Many years ago someone created a new __link_set_sysctl_funcs to hold
the list of routines that need to be called for setting up sysctl
variables.  This worked great for all code included in the kernel
itself, but didn't deal with modules that want to create their own
sysctl data.  So, we ended up with a lot of #ifdef _MODULE blocks
so modules could explicitly call their setup functions when loaded
as non-built-in modules.

So today, we complete the task that was started so many years ago.

When modules are loaded, after we've called xxx_modcmd(INIT...) we
check if the module contains its own __link_set_sysctl_funcs, and
if so we call the functions listed.  We add a struct sysctllog member
to the struct module so we can call sysctl_teardown() when the module
gets unloaded.  (The sequence of events ensures that the sysctl stuff
doesn't get created until the rest of the module's init code does any
required memory allocation.)

So, no more need to explicitly call the sysctl setup routines when
built as a loadable module.

diffstat:

 sys/dev/ccd.c                   |  10 +-------
 sys/dev/iscsi/iscsi_main.c      |   7 +-----
 sys/dev/usb/usbnet.c            |  14 +-----------
 sys/kern/kern_module.c          |  44 +++++++++++++++++++++++++++++++++++++++-
 sys/kern/sysv_ipc.c             |  29 +++++++-------------------
 sys/kern/sysv_msg.c             |  10 ++------
 sys/kern/sysv_sem.c             |  10 ++------
 sys/kern/sysv_shm.c             |  10 ++------
 sys/miscfs/genfs/layer_vfsops.c |  13 +----------
 sys/sys/module.h                |   6 ++++-
 sys/sys/msg.h                   |   4 +-
 sys/sys/sem.h                   |   4 +-
 sys/sys/shm.h                   |   4 +-
 13 files changed, 77 insertions(+), 88 deletions(-)

diffs (truncated from 528 to 300 lines):

diff -r 0324710e0e38 -r 71c16614d178 sys/dev/ccd.c
--- a/sys/dev/ccd.c     Tue Aug 06 18:17:52 2019 +0000
+++ b/sys/dev/ccd.c     Wed Aug 07 00:38:01 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ccd.c,v 1.179 2019/03/27 19:13:34 martin Exp $ */
+/*     $NetBSD: ccd.c,v 1.180 2019/08/07 00:38:01 pgoyette Exp $       */
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 1999, 2007, 2009 The NetBSD Foundation, Inc.
@@ -88,7 +88,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ccd.c,v 1.179 2019/03/27 19:13:34 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ccd.c,v 1.180 2019/08/07 00:38:01 pgoyette Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -216,10 +216,6 @@
 static LIST_HEAD(, ccd_softc) ccds = LIST_HEAD_INITIALIZER(ccds);
 static kmutex_t ccd_lock;
 
-#ifdef _MODULE
-static struct sysctllog *ccd_clog;
-#endif
-
 SYSCTL_SETUP_PROTO(sysctl_kern_ccd_setup);
 
 static struct ccd_softc *
@@ -1681,7 +1677,6 @@
 
                error = devsw_attach("ccd", &ccd_bdevsw, &bmajor,
                    &ccd_cdevsw, &cmajor);
-               sysctl_kern_ccd_setup(&ccd_clog);
 #endif
                break;
 
@@ -1696,7 +1691,6 @@
                        error = devsw_detach(&ccd_bdevsw, &ccd_cdevsw);
                        ccddetach();
                }
-               sysctl_teardown(&ccd_clog);
 #endif
                break;
 
diff -r 0324710e0e38 -r 71c16614d178 sys/dev/iscsi/iscsi_main.c
--- a/sys/dev/iscsi/iscsi_main.c        Tue Aug 06 18:17:52 2019 +0000
+++ b/sys/dev/iscsi/iscsi_main.c        Wed Aug 07 00:38:01 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: iscsi_main.c,v 1.30 2019/07/13 17:06:00 mlelstv Exp $  */
+/*     $NetBSD: iscsi_main.c,v 1.31 2019/08/07 00:38:02 pgoyette Exp $ */
 
 /*-
  * Copyright (c) 2004,2005,2006,2011 The NetBSD Foundation, Inc.
@@ -677,7 +677,6 @@
 #ifdef _MODULE
        devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
        int error;
-       static struct sysctllog *clog;
 #endif
 
        switch (cmd) {
@@ -723,8 +722,6 @@
                        config_cfdriver_detach(&iscsi_cd);
                        return ENXIO;
                }
-
-               sysctl_iscsi_setup(&clog);
 #endif
                return 0;
                break;
@@ -735,8 +732,6 @@
                if (error)
                        return error;
 
-               sysctl_teardown(&clog);
-
                config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
                config_cfdriver_detach(&iscsi_cd);
                devsw_detach(NULL, &iscsi_cdevsw);
diff -r 0324710e0e38 -r 71c16614d178 sys/dev/usb/usbnet.c
--- a/sys/dev/usb/usbnet.c      Tue Aug 06 18:17:52 2019 +0000
+++ b/sys/dev/usb/usbnet.c      Wed Aug 07 00:38:01 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: usbnet.c,v 1.6 2019/08/06 01:42:22 mrg Exp $   */
+/*     $NetBSD: usbnet.c,v 1.7 2019/08/07 00:38:02 pgoyette Exp $      */
 
 /*
  * Copyright (c) 2019 Matthew R. Green
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.6 2019/08/06 01:42:22 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.7 2019/08/07 00:38:02 pgoyette Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -1207,18 +1207,8 @@
 {
        switch (cmd) {
        case MODULE_CMD_INIT:
-#ifdef _MODULE
-# if defined(USB_DEBUG) && defined(USBNET_DEBUG)
-               sysctl_hw_usbnet_setup(&usbnet_clog);
-# endif
-#endif
                return 0;
        case MODULE_CMD_FINI:
-#ifdef _MODULE
-# if defined(USB_DEBUG) && defined(USBNET_DEBUG)
-               sysctl_teardown(&usbnet_clog);
-# endif
-#endif
                return 0;
        case MODULE_CMD_STAT:
        case MODULE_CMD_AUTOUNLOAD:
diff -r 0324710e0e38 -r 71c16614d178 sys/kern/kern_module.c
--- a/sys/kern/kern_module.c    Tue Aug 06 18:17:52 2019 +0000
+++ b/sys/kern/kern_module.c    Wed Aug 07 00:38:01 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_module.c,v 1.136 2019/06/19 15:01:01 pgoyette Exp $       */
+/*     $NetBSD: kern_module.c,v 1.137 2019/08/07 00:38:02 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.136 2019/06/19 15:01:01 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.137 2019/08/07 00:38:02 pgoyette Exp $");
 
 #define _MODULE_INTERNAL
 
@@ -947,6 +947,35 @@
 }
 
 /*
+ * module_load_sysctl
+ *
+ * Check to see if the module has any SYSCTL_SETUP() routine(s)
+ * registered.  If so, call it (them).
+ */
+
+static void
+module_load_sysctl(module_t *mod)
+{
+       void (**ls_funcp)(struct sysctllog **);
+       void *ls_start;
+       size_t ls_size, count;
+       int error;
+
+       error = kobj_find_section(mod->mod_kobj, "link_set_sysctl_funcs",
+           &ls_start, &ls_size);
+       if (error == 0) {
+               count = ls_size / sizeof(ls_start);
+               ls_funcp = ls_start;
+               while (count--) {
+                       (**ls_funcp)(&mod->mod_sysctllog);
+                       ls_funcp++;
+               }
+       }
+       else
+               error = 0;      /* no setup funcs registered */
+}
+
+/*
  * module_do_load:
  *
  *     Helper routine: load a module from the file system, or one
@@ -1265,6 +1294,8 @@
                goto fail1;
        }
 
+       module_load_sysctl(mod);        /* Set-up module's sysctl if any */
+
        /*
         * Good, the module loaded successfully.  Put it onto the
         * list and add references to its requisite modules.
@@ -1344,9 +1375,18 @@
        prev_active = module_active;
        module_active = mod;
        module_callback_unload(mod);
+
+       /*
+        * If there were any registered SYSCTL_SETUP funcs, make sure
+        * we release the sysctl entries
+        */
+       if (mod->mod_sysctllog) {
+               sysctl_teardown(&mod->mod_sysctllog);
+       }
        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_print("cannot unload module `%s' error=%d", name,
                    error);
                return error;
diff -r 0324710e0e38 -r 71c16614d178 sys/kern/sysv_ipc.c
--- a/sys/kern/sysv_ipc.c       Tue Aug 06 18:17:52 2019 +0000
+++ b/sys/kern/sysv_ipc.c       Wed Aug 07 00:38:01 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sysv_ipc.c,v 1.39 2019/04/10 10:03:50 pgoyette Exp $   */
+/*     $NetBSD: sysv_ipc.c,v 1.40 2019/08/07 00:38:02 pgoyette Exp $   */
 
 /*-
  * Copyright (c) 1998, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.39 2019/04/10 10:03:50 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sysv_ipc.c,v 1.40 2019/08/07 00:38:02 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_sysv.h"
@@ -132,8 +132,6 @@
  
 SYSCTL_SETUP_PROTO(sysctl_ipc_setup);
 
-static struct sysctllog *sysctl_sysvipc_clog = NULL;
-
 static const struct syscall_package sysvipc_syscalls[] = {
 #if defined(SYSVSHM)
        { SYS___shmctl50, 0, (sy_call_t *)sys___shmctl50 },
@@ -180,12 +178,12 @@
                 * sysctl data
                 */
 #ifdef SYSVSHM
-               error = shminit(&sysctl_sysvipc_clog);
+               error = shminit();
                if (error != 0)
                        return error;
 #endif
 #ifdef SYSVSEM
-               error = seminit(&sysctl_sysvipc_clog);
+               error = seminit();
                if (error != 0) {
 #ifdef SYSVSHM
                        shmfini();
@@ -194,7 +192,7 @@
                }
 #endif
 #ifdef SYSVMSG
-               error = msginit(&sysctl_sysvipc_clog);
+               error = msginit();
                if (error != 0) {
 #ifdef SYSVSEM
                        semfini();
@@ -205,11 +203,6 @@
                        return error;
                }
 #endif
-
-#ifdef _MODULE
-               /* Set up the common sysctl tree */
-               sysctl_ipc_setup(&sysctl_sysvipc_clog);
-#endif
                break;
        case MODULE_CMD_FINI:
                /*
@@ -228,7 +221,7 @@
 #ifdef SYSVSEM
                if (semfini()) {
 #ifdef SYSVSHM
-                       shminit(NULL);
+                       shminit();
 #endif
                        return EBUSY;
                }
@@ -236,20 +229,14 @@
 #ifdef SYSVMSG
                if (msgfini()) {
 #ifdef SYSVSEM
-                       seminit(NULL);
+                       seminit();
 #endif
 #ifdef SYSVSHM
-                       shminit(NULL);
+                       shminit();
 #endif
                        return EBUSY;
                }
 #endif
-
-#ifdef _MODULE
-               /* Remove the sysctl sub-trees */
-               sysctl_teardown(&sysctl_sysvipc_clog);
-#endif
-
                /* Unlink the system calls. */
                error = syscall_disestablish(NULL, sysvipc_syscalls);
                if (error)
diff -r 0324710e0e38 -r 71c16614d178 sys/kern/sysv_msg.c
--- a/sys/kern/sysv_msg.c       Tue Aug 06 18:17:52 2019 +0000
+++ b/sys/kern/sysv_msg.c       Wed Aug 07 00:38:01 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sysv_msg.c,v 1.74 2019/04/10 10:03:50 pgoyette Exp $   */
+/*     $NetBSD: sysv_msg.c,v 1.75 2019/08/07 00:38:02 pgoyette Exp $   */



Home | Main Index | Thread Index | Old Index