From owner-freebsd-hackers@FreeBSD.ORG Fri Sep 22 18:58:45 2006 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id BBBA316A403 for ; Fri, 22 Sep 2006 18:58:45 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from server.baldwin.cx (66-23-211-162.clients.speedfactory.net [66.23.211.162]) by mx1.FreeBSD.org (Postfix) with ESMTP id 06C9743D45 for ; Fri, 22 Sep 2006 18:58:44 +0000 (GMT) (envelope-from jhb@freebsd.org) Received: from localhost.corp.yahoo.com (john@localhost [127.0.0.1]) (authenticated bits=0) by server.baldwin.cx (8.13.6/8.13.6) with ESMTP id k8MIwfgg015364; Fri, 22 Sep 2006 14:58:41 -0400 (EDT) (envelope-from jhb@freebsd.org) From: John Baldwin To: "Intron is my alias on the Internet" Date: Fri, 22 Sep 2006 14:44:23 -0400 User-Agent: KMail/1.9.1 References: <200609212136.18850.jhb@freebsd.org> In-Reply-To: MIME-Version: 1.0 Content-Disposition: inline Message-Id: <200609221444.23702.jhb@freebsd.org> Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: 7bit X-Greylist: Sender succeeded SMTP AUTH authentication, not delayed by milter-greylist-2.0.2 (server.baldwin.cx [127.0.0.1]); Fri, 22 Sep 2006 14:58:42 -0400 (EDT) X-Virus-Scanned: ClamAV 0.88.3/1927/Fri Sep 22 06:06:31 2006 on server.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-4.4 required=4.2 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.1.3 X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on server.baldwin.cx Cc: freebsd-hackers@freebsd.org Subject: Re: A Bug in linker_reference_module() ? 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, 22 Sep 2006 18:58:45 -0000 On Thursday 21 September 2006 21:57, Intron is my alias on the Internet wrote: > John Baldwin wrote: > > > On Thursday 21 September 2006 14:12, Intron is my alias on the Internet wrote: > >> Please have a look at the function linker_reference_module() in > >> /sys/kern/kern_linker.c of 7.0-CURRENT. If the module is loaded on demand, > >> why not increase its reference counter after loading? In my opinion, > >> linker_reference_module() behaves differently from linker_load_file(). > > > > This is because a new kld loaded via linker_load_module() starts off > > with a refcount of 1. Thus, if you do: > > > > linker_reference_module(...); > > ... > > linker_release_module(...); > > > > Then with the current code the release_module() call drops the reference > > count to 0 and the module is unloaded. This is the desired operation for > > reference_module/release_module. This model is commonly used in the kernel. > > For example, when creating a credential, one just does 'crget()' and later > > a 'crfree()' to free it instead of doing 'crget(); crhold()' to create one. > > This model is a little confusing. If a module is loaded on demand as > dependency, its reference counter is set to 1. And if the module is loaded > by kldload(2), its reference counter is also set to 1, though in fact > no other loaded module depends on it. kldload(2) is a way for to specify a user reference on the module, and kldunload(2) is how you drop that user reference. > Although this "shift" model can work correctly, I want to know whether > there's a more reasonable way, such as setting up an auto-unloadable flag. There is this effectively done with the userref flag. When you kldload a module, it sets userref to 1, and you can only kldunload a module when userref == 1. This lets the following work: thread 1 thread 2 kldload(foo) ... ... linker_reference_module(foo) (now foo has userref == 1 and refs == 2) kldunload(foo) ... (now foo has userref == 0 and refs == 1, kldunload reports success, but module isn't unloaded due to in-kernel reference) ... linker_release_module(foo) (now refs drops to 0 and module is actually unloaded) The simpler cases also work fine if you try them out. -- John Baldwin