From nobody@FreeBSD.org  Wed Jul  4 15:12:51 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 5B20F16A485
	for <freebsd-gnats-submit@FreeBSD.org>; Wed,  4 Jul 2007 15:12:51 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [69.147.83.33])
	by mx1.freebsd.org (Postfix) with ESMTP id 3FA6A13C459
	for <freebsd-gnats-submit@FreeBSD.org>; Wed,  4 Jul 2007 15:12:51 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id l64FCoID002090
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 4 Jul 2007 15:12:50 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id l64FCogr002089;
	Wed, 4 Jul 2007 15:12:50 GMT
	(envelope-from nobody)
Message-Id: <200707041512.l64FCogr002089@www.freebsd.org>
Date: Wed, 4 Jul 2007 15:12:50 GMT
From: david chosrova <david.chosrova@libertysurf.fr>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [PATCH]dynamic module references
X-Send-Pr-Version: www-3.0

>Number:         114291
>Category:       kern
>Synopsis:       [RFE] [modules] [patch] add dynamic module references
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Jul 04 15:20:03 GMT 2007
>Closed-Date:    
>Last-Modified:  Wed Jun 01 21:57:57 UTC 2011
>Originator:     david chosrova
>Release:        6-2
>Organization:
>Environment:
>Description:
Hi,

I'm new to Freebsd and interested in system programming. So I'have picked
up a task from the project ideas list to start with.

(part of) the subject is : "This task is to define and implement a general
mechanism for tracking these references and use them in handling module
unload requests."

So, to do that, I've added an "int dynrefs" in struct module
(kern_module.c), and functions to increase or decrease this count
(module_add/remove_dynrefs(const char * modname) and
module_updatedynrefs(const char * modname, int action) ) in kern_module.c.

To avoid unload of a module which has a dynrefs count != 0 , I have
modified module_unload(), so that unload is process only if dynrefs=0
or flag=LINKER_UNLOAD_FORCE.

 module_unload(module_t mod, int flags)
 {
 	int error;
-	error = MOD_EVENT(mod, MOD_QUIESCE);
+	MOD_SLOCK;
+	(mod->dynrefs == 0) ? (error = MOD_EVENT(mod, MOD_QUIESCE)) : (error = EPERM);
+	MOD_SUNLOCK;



I have compiled and tested. with a 6-2 RELEASE. For the test I'have used
two dummy module, one adding a dynrefs on the other.

Any comment are welcome

David chosrova 

>How-To-Repeat:

>Fix:


Patch attached with submission follows:

--- kern_module.c.orig	Wed Jul  4 10:33:06 2007
+++ kern_module.c	Wed Jul  4 10:21:28 2007
@@ -52,6 +52,7 @@
 	TAILQ_ENTRY(module)	flink;	/* all modules in a file */
 	struct linker_file	*file;	/* file which contains this module */
 	int			refs;	/* reference count */
+	int			dynrefs;        /* dynamic reference count */
 	int 			id;	/* unique id number */
 	char 			*name;	/* module name */
 	modeventhand_t 		handler;	/* event handler */
@@ -65,6 +66,7 @@
 struct sx modules_sx;
 static int nextid = 1;
 static void module_shutdown(void *, int);
+static int module_updatedynrefs(const char* , int );
 
 static int
 modevent_nop(module_t mod, int what, void *arg)
@@ -152,6 +154,7 @@
 	}
 	newmod->refs = 1;
 	newmod->id = nextid++;
+	newmod->dynrefs = 0;
 	newmod->name = (char *)(newmod + 1);
 	strcpy(newmod->name, data->name);
 	newmod->handler = data->evhand ? data->evhand : modevent_nop;
@@ -231,7 +234,9 @@
 module_unload(module_t mod, int flags)
 {
 	int error;
-	error = MOD_EVENT(mod, MOD_QUIESCE);
+	MOD_SLOCK;
+	(mod->dynrefs == 0) ? (error = MOD_EVENT(mod, MOD_QUIESCE)) : (error = EPERM);
+	MOD_SUNLOCK;
 	if (error == EOPNOTSUPP || error == EINVAL)
 		error = 0;
 	if (flags == LINKER_UNLOAD_NORMAL && error != 0)
@@ -261,6 +266,37 @@
 
 	MOD_XLOCK_ASSERT;
 	mod->data = *datap;
+}
+
+static int
+module_updatedynrefs(const char* modname, int action)
+{
+	module_t mod;
+	
+	MOD_XLOCK;
+	mod = module_lookupbyname(modname);
+
+	if(mod == 0) {
+		MOD_XUNLOCK;
+		return(-1);
+	}
+
+	(action == 1) ? mod->dynrefs++ : mod->dynrefs--;
+	MOD_XUNLOCK;
+	return (0);
+}
+
+int
+module_add_dynrefs(const char *modname)
+{
+
+	return (module_updatedynrefs(modname,1));
+}
+
+int
+module_remove_dynrefs(const char * modname)
+{
+	return (module_updatedynrefs(modname,0));
 }
 
 /*	


>Release-Note:
>Audit-Trail:
Adding to audit trail from misfiled PR kern/114295:

Date: Wed,  4 Jul 2007 17:48:41 +0200
From: "=?iso-8859-1?Q?david.chosrova@libertysurf.fr?=" <david.chosrova@libertysurf.fr>
 
 This is the patch for module.h
 
 --_=__=_XaM3_.1183564121.2A.697510.42.28273.52.42.007.27119
 Content-Type: text/plain;
 	name="module.diff"
 Content-Transfer-Encoding: base64
 Content-Disposition: attachment;
 	filename="module.diff"
 
 LS0tIG1vZHVsZS5oLm9yaWcJV2VkIEp1bCAgNCAxMDoyOTo1MyAyMDA3CisrKyBtb2R1bGUu
 aAlXZWQgSnVsICA0IDEwOjIxOjQwIDIwMDcKQEAgLTE0Nyw3ICsxNDcsOCBAQAogaW50CW1v
 ZHVsZV9nZXRpZChtb2R1bGVfdCk7CiBtb2R1bGVfdAltb2R1bGVfZ2V0Zm5leHQobW9kdWxl
 X3QpOwogdm9pZAltb2R1bGVfc2V0c3BlY2lmaWMobW9kdWxlX3QsIG1vZHNwZWNpZmljX3Qg
 Kik7Ci0KK2ludAltb2R1bGVfYWRkX2R5bnJlZnMoY29uc3QgY2hhciAqKTsKK2ludAltb2R1
 bGVfcmVtb3ZlX2R5bnJlZnMoY29uc3QgY2hhciAqKTsKIAogI2lmZGVmCU1PRF9ERUJVRwog
 ZXh0ZXJuIGludCBtb2RfZGVidWc7Cg==
 
 --_=__=_XaM3_.1183564121.2A.697510.42.28273.52.42.007.27119--
 
Responsible-Changed-From-To: freebsd-bugs->kmacy 
Responsible-Changed-By: kmacy 
Responsible-Changed-When: Fri Nov 16 18:20:28 UTC 2007 
Responsible-Changed-Why:  

Need to see if this is something that we in fact want. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=114291 

From: david chosrova <dada2372@gmail.com>
To: bug-followup@FreeBSD.org, dada2372@gmail.com
Cc:  
Subject: Re: kern/114291: [RFE] [modules] [patch] add dynamic module references
Date: Tue, 16 Nov 2010 16:04:34 +0100

 --0016363b8206e2157504952cdfb1
 Content-Type: text/plain; charset=ISO-8859-1
 
 Hello,
 
  It is quite a long time. What was in fact needed  ?
 
 Regards.
 
 david Chosrova
 
 --0016363b8206e2157504952cdfb1
 Content-Type: text/html; charset=ISO-8859-1
 Content-Transfer-Encoding: quoted-printable
 
 <br clear=3D"all"><div>=A0</div>
 <div><span class=3D"120290215-16112010"><font face=3D"Arial" size=3D"2">Hel=
 lo,</font></span></div>
 <div><span class=3D"120290215-16112010"></span>=A0</div>
 <div><span class=3D"120290215-16112010"><font face=3D"Arial" size=3D"2">=A0=
 It is quite a long=20
 time. What was in fact needed=A0 ?<br></font></span></div>
 <div><span class=3D"120290215-16112010"></span>=A0</div>
 <div><span class=3D"120290215-16112010"><font face=3D"Arial" size=3D"2">Reg=
 ards.</font></span></div>
 <div><span class=3D"120290215-16112010"></span>=A0</div>
 <div><span class=3D"120290215-16112010"><font face=3D"Arial" size=3D"2">dav=
 id=20
 Chosrova</font></span></div><br>
 
 --0016363b8206e2157504952cdfb1--
Responsible-Changed-From-To: kmacy->freebsd-bugs 
Responsible-Changed-By: gavin 
Responsible-Changed-When: Wed Jun 1 20:41:37 UTC 2011 
Responsible-Changed-Why:  
kmacy has asked for all of his PRs to be reassigned, put back into the 
pool. 

To submitter: I'm afraid that I don't know the status of this idea -  
it may be worth posting to freebsd-hackers to give your patch a 
larger audience. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=114291 
>Unformatted:
