From owner-freebsd-arch@FreeBSD.ORG Thu Mar 31 13:24:05 2005 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3773116A4CE for ; Thu, 31 Mar 2005 13:24:05 +0000 (GMT) Received: from mail22.sea5.speakeasy.net (mail22.sea5.speakeasy.net [69.17.117.24]) by mx1.FreeBSD.org (Postfix) with ESMTP id D90A143D31 for ; Thu, 31 Mar 2005 13:24:04 +0000 (GMT) (envelope-from john@baldwin.cx) Received: (qmail 27573 invoked from network); 31 Mar 2005 13:24:04 -0000 Received: from server.baldwin.cx ([216.27.160.63]) (envelope-sender )AES256-SHA encrypted SMTP for ; 31 Mar 2005 13:24:03 -0000 Received: from [192.168.0.15] (osx.baldwin.cx [192.168.0.15]) (authenticated bits=0) by server.baldwin.cx (8.13.1/8.13.1) with ESMTP id j2VDNwQl024609; Thu, 31 Mar 2005 08:23:58 -0500 (EST) (envelope-from john@baldwin.cx) In-Reply-To: References: Mime-Version: 1.0 (Apple Message framework v619.2) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: Content-Transfer-Encoding: 7bit From: John Baldwin Date: Thu, 31 Mar 2005 08:23:57 -0500 To: "dragonfly dragonfly" X-Mailer: Apple Mail (2.619.2) X-Spam-Status: No, score=-102.8 required=4.2 tests=ALL_TRUSTED, USER_IN_WHITELIST autolearn=failed version=3.0.2 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on server.baldwin.cx cc: freebsd-arch@freebsd.org Subject: Re: FreeBSD module inference count problem X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 31 Mar 2005 13:24:05 -0000 On Mar 30, 2005, at 8:24 PM, dragonfly dragonfly wrote: > Hi all, Recently I am doing some programming on FreeBSD KLD. In my KLD > codes,I will start some kernel threads to serve requests,but i can not > find how to increase the module reference count,something like > 'MOD_INC_USE_COUNT' in Linux.If not do so,if the user downloaded the > module before all kernel threads exit, the system must be panic.I > searched the file 'module.h' carefully,only find > module_reference().But it seemed in total kernel source,the function > does not be used.Even use it,when i use kldstat to see its reference > count,it keep __1__! When i download the module before the kernel > thread wake up, system panic as expect. my KLD codes like below: > module_t curmod; void do_job(void *arg) { if (!curmod) printf("Module > not found\n"); else module_reference(curmod); > tsleep(curproc,PRIBIO,"foo worker",15*hz); printf("Wake up\n"); if > (curmod) module_release(curmod); kthread_exit(0); } static int > foo_loader(struct module *m, int what, void *arg) { int err = 0; > struct proc *newpp; > switch (what) { case MOD_LOAD: curmod=m; > kthread_create(do_job,NULL,&newpp,0,0,"foo worker"); printf("foo > loaded\n"); break; case MOD_UNLOAD: case MOD_SHUTDOWN: printf("foo > unloaded.\n"); break; default: err = EOPNOTSUPP; break; } > return(err); } Could you help me? Thanks. Right now you need to provide your own reference count and fail the operation with EBUSY when MOD_UNLOAD is called if you have open references. You can also have MOD_UNLOAD wake up the kthread and instruct it to die and then block until the kthread has exited before it returns. (You should do that anyway if you really need to unload this module to ensure that the kthread's code isn't unmapped out from under it while it is running.) -- John Baldwin <>< http://www.FreeBSD.org/~jhb/ "Power Users Use the Power to Serve" = http://www.FreeBSD.org