From owner-freebsd-hackers@FreeBSD.ORG Fri Aug 5 15:21:40 2011 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 92A701065675 for ; Fri, 5 Aug 2011 15:21:40 +0000 (UTC) (envelope-from pluknet@gmail.com) Received: from mail-yi0-f54.google.com (mail-yi0-f54.google.com [209.85.218.54]) by mx1.freebsd.org (Postfix) with ESMTP id 542058FC19 for ; Fri, 5 Aug 2011 15:21:40 +0000 (UTC) Received: by yic13 with SMTP id 13so2183376yic.13 for ; Fri, 05 Aug 2011 08:21:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=ZQLrKuA6eGeqUICKdmSBkiwQL8akI792vDxUPRTqDEE=; b=mlY+0z9BwAiD8JzRqLRZMZ31pAhn56cI8LMKzPXrDnLCuSLY5CTos3Tp5NXGCBZ1ZE YjtEK0EQHzHW3hVKTLsvCsZDuR8BvRkafMIOJ/zdid2vpv8q5emnJMLo2xHJcfMg2w2I zuDKlobCmoPjAj6wDwcrzM7cd3NgX+BAe1jTs= MIME-Version: 1.0 Received: by 10.150.166.1 with SMTP id o1mr200107ybe.73.1312557699657; Fri, 05 Aug 2011 08:21:39 -0700 (PDT) Received: by 10.150.200.3 with HTTP; Fri, 5 Aug 2011 08:21:39 -0700 (PDT) In-Reply-To: References: Date: Fri, 5 Aug 2011 19:21:39 +0400 Message-ID: From: Sergey Kandaurov To: Garrett Cooper Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: freebsd-hackers@freebsd.org Subject: Re: module_register_init fails, but driver is still loaded? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 05 Aug 2011 15:21:40 -0000 On 4 August 2011 20:23, Garrett Cooper wrote: > Hi hackers, > =A0 =A0I noticed that if anything fails while initializing a driver, the > driver stays attached to the kernel as a module instead of being > kicked when all references to the driver go to 0. Is this desired > behavior (it doesn't seem like it, but I can see potential pros and > cons of kicking the driver out of the kernel immediately when a > failure state occurs)? I've seen this on 7.2 ~ 9-CURRENT. Example > sourcecode and invocation attached below. Hi. I have cooked something that might work, though I don't know how much is it correct from locking & cleanup side. Can you try it? Anyway, in its current form we cannot return error from module_register_init() because it's usually called from SYSINIT, so kldload(8) will say nonsense: can't load ./bad_module.ko: No error: 0. Index: sys/kern/kern_module.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- sys/kern/kern_module.c (revision 224471) +++ sys/kern/kern_module.c (working copy) @@ -112,6 +117,7 @@ module_register_init(const void *arg) const moduledata_t *data =3D (const moduledata_t *)arg; int error; module_t mod; + linker_file_t lf; mtx_lock(&Giant); MOD_SLOCK; @@ -123,12 +129,14 @@ module_register_init(const void *arg) error =3D MOD_EVENT(mod, MOD_LOAD); if (error) { MOD_EVENT(mod, MOD_UNLOAD); + lf =3D mod->file; MOD_XLOCK; module_release(mod); MOD_XUNLOCK; printf("module_register_init: MOD_LOAD (%s, %p, %p) error" " %d\n", data->name, (void *)data->evhand, data->priv, error); + linker_release_module(NULL, NULL, lf); } else { MOD_XLOCK; if (mod->file) { --=20 wbr, pluknet