From owner-freebsd-current@freebsd.org Wed Aug 2 16:14:37 2017 Return-Path: Delivered-To: freebsd-current@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 71A0DDD43A4; Wed, 2 Aug 2017 16:14:37 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from mail.baldwin.cx (bigwig.baldwin.cx [96.47.65.170]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 2FB906B337; Wed, 2 Aug 2017 16:14:36 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from ralph.baldwin.cx (c-73-231-226-104.hsd1.ca.comcast.net [73.231.226.104]) by mail.baldwin.cx (Postfix) with ESMTPSA id 9949410AF01; Wed, 2 Aug 2017 12:14:27 -0400 (EDT) From: John Baldwin To: freebsd-current@freebsd.org Cc: Hans Petter Selasky , Andriy Gapon , FreeBSD Hackers , FreeBSD Current Subject: Re: order of executing MOD_LOAD and registering module sysctl-s Date: Wed, 02 Aug 2017 08:49:46 -0700 Message-ID: <2718016.8bPh6cqhGc@ralph.baldwin.cx> User-Agent: KMail/4.14.10 (FreeBSD/11.1-STABLE; KDE/4.14.30; amd64; ; ) In-Reply-To: References: <62e7ab4d-8956-545e-b204-4fb63cfe5fbf@FreeBSD.org> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.4.3 (mail.baldwin.cx); Wed, 02 Aug 2017 12:14:27 -0400 (EDT) X-Virus-Scanned: clamav-milter 0.99.2 at mail.baldwin.cx X-Virus-Status: Clean X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 02 Aug 2017 16:14:37 -0000 On Wednesday, August 02, 2017 12:39:36 PM Hans Petter Selasky wrote: > On 08/02/17 12:13, Andriy Gapon wrote: > > > > As far as I understand a module initialization routine is executed via the > > sysinit mechanism. Specifically, module_register_init is set up as the sysinit > > function for every module and it calls MOD_EVENT(mod, MOD_LOAD) to invoke the > > module event handler. > > > > In linker_load_file() I see the following code: > > linker_file_register_sysctls(lf); > > linker_file_sysinit(lf); > > > > I think that this means that any statically declared sysctl-s in the module > > would be registered before the module receives the MOD_LOAD event. > > It's possible that some of the sysctl-s could have procedures as handlers and > > they might access data that is supposed to be initialized by the module event > > handler. > > > > So, for example, running sysctl -a at just the right moment during the loading > > of a module might end up in an expected behavior (including a crash). > > > > Is my interpretation of how the code works correct? > > Can the order of linker_file_sysinit and linker_file_register_sysctls be changed > > without a great risk? > > > > Thank you! > > > > P.S. > > The same applies to: > > linker_file_sysuninit(file); > > linker_file_unregister_sysctls(file); > > > > Hi, > > Not sure if this answers your question. > > If a SYSCTL() is TUNABLE, it's procedure can be called when the sysctl > is created. Else the SYSCTL() procedure callback might be called right > after it's registered. I think there is an own subsystem in sys/kernel.h > which takes care of the actual SYSCTL() creation/destruction - after the > linker is involved. sysctl nodes are created explicitly via linker_file_register_sysctls, not via SYSINITs, so you can't order them with respect to other init functions. I think Andriy's suggestion of doing sysctls "inside" sysinits (so they are registered last and unregistered first) is probably better than the current state and is a simpler fix than changing all sysctls to use SYSINITs. -- John Baldwin