[svn-commits] sruffell: linux/trunk r10623 - in /linux/trunk: drivers/dahdi/ include/dahdi/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Apr 3 14:44:32 CDT 2012


Author: sruffell
Date: Tue Apr  3 14:44:27 2012
New Revision: 10623

URL: http://svnview.digium.com/svn/dahdi?view=rev&rev=10623
Log:
dahdi_dynamic: Do not call into dahdi_dynamic without holding reference.

Instead of registering a function pointer, register a dahdi_dynamic_ops
structure that contains the owner as well as the ioctl callback. This way
dahdi.ko can bump up the reference count on dahdi_dynamic.ko before calling
the ioctl callback.

Also, use the registration mutex to guard against the module being unloaded
between the time the structure pointer was checked, and the module reference
is taken.

Signed-off-by: Shaun Ruffell <sruffell at digium.com>

Modified:
    linux/trunk/drivers/dahdi/dahdi-base.c
    linux/trunk/drivers/dahdi/dahdi_dynamic.c
    linux/trunk/include/dahdi/kernel.h

Modified: linux/trunk/drivers/dahdi/dahdi-base.c
URL: http://svnview.digium.com/svn/dahdi/linux/trunk/drivers/dahdi/dahdi-base.c?view=diff&rev=10623&r1=10622&r2=10623
==============================================================================
--- linux/trunk/drivers/dahdi/dahdi-base.c (original)
+++ linux/trunk/drivers/dahdi/dahdi-base.c Tue Apr  3 14:44:27 2012
@@ -121,7 +121,6 @@
 EXPORT_SYMBOL(dahdi_qevent_lock);
 EXPORT_SYMBOL(dahdi_hooksig);
 EXPORT_SYMBOL(dahdi_alarm_notify);
-EXPORT_SYMBOL(dahdi_set_dynamic_ioctl);
 EXPORT_SYMBOL(dahdi_hdlc_abort);
 EXPORT_SYMBOL(dahdi_hdlc_finish);
 EXPORT_SYMBOL(dahdi_hdlc_getbuf);
@@ -4405,12 +4404,14 @@
 	return 0;
 }
 
-static int (*dahdi_dynamic_ioctl)(unsigned int cmd, unsigned long data);
-
-void dahdi_set_dynamic_ioctl(int (*func)(unsigned int cmd, unsigned long data))
-{
-	dahdi_dynamic_ioctl = func;
-}
+static const struct dahdi_dynamic_ops *dahdi_dynamic_ops;
+void dahdi_set_dynamic_ops(const struct dahdi_dynamic_ops *ops)
+{
+	mutex_lock(&registration_mutex);
+	dahdi_dynamic_ops = ops;
+	mutex_unlock(&registration_mutex);
+}
+EXPORT_SYMBOL(dahdi_set_dynamic_ops);
 
 static int (*dahdi_hpec_ioctl)(unsigned int cmd, unsigned long data);
 
@@ -5127,6 +5128,33 @@
 	put_span(s);
 
 	return 0;
+}
+
+static int dahdi_ioctl_dynamic(unsigned int cmd, unsigned long data)
+{
+	bool tried_load = false;
+	int res;
+
+retry_check:
+	mutex_lock(&registration_mutex);
+	if (!dahdi_dynamic_ops) {
+		mutex_unlock(&registration_mutex);
+		if (tried_load)
+			return -ENOSYS;
+
+		request_module("dahdi_dynamic");
+		tried_load = true;
+		goto retry_check;
+	}
+	if (!try_module_get(dahdi_dynamic_ops->owner)) {
+		mutex_unlock(&registration_mutex);
+		return -ENOSYS;
+	}
+	mutex_unlock(&registration_mutex);
+
+	res = dahdi_dynamic_ops->ioctl(cmd, data);
+	module_put(dahdi_dynamic_ops->owner);
+	return res;
 }
 
 static int
@@ -5163,14 +5191,7 @@
 		return dahdi_ioctl_maint(data);
 	case DAHDI_DYNAMIC_CREATE:
 	case DAHDI_DYNAMIC_DESTROY:
-		if (dahdi_dynamic_ioctl) {
-			return dahdi_dynamic_ioctl(cmd, data);
-		} else {
-			request_module("dahdi_dynamic");
-			if (dahdi_dynamic_ioctl)
-				return dahdi_dynamic_ioctl(cmd, data);
-		}
-		return -ENOSYS;
+		return dahdi_ioctl_dynamic(cmd, data);
 	case DAHDI_EC_LICENSE_CHALLENGE:
 	case DAHDI_EC_LICENSE_RESPONSE:
 		if (dahdi_hpec_ioctl) {

Modified: linux/trunk/drivers/dahdi/dahdi_dynamic.c
URL: http://svnview.digium.com/svn/dahdi/linux/trunk/drivers/dahdi/dahdi_dynamic.c?view=diff&rev=10623&r1=10622&r2=10623
==============================================================================
--- linux/trunk/drivers/dahdi/dahdi_dynamic.c (original)
+++ linux/trunk/drivers/dahdi/dahdi_dynamic.c Tue Apr  3 14:44:27 2012
@@ -818,10 +818,13 @@
 	mod_timer(&alarmcheck, jiffies + 1 * HZ);
 }
 
+static const struct dahdi_dynamic_ops dahdi_dynamic_ops = {
+	.owner = THIS_MODULE,
+	.ioctl = dahdi_dynamic_ioctl,
+};
+
 static int dahdi_dynamic_init(void)
 {
-	dahdi_set_dynamic_ioctl(dahdi_dynamic_ioctl);
-
 	/* Start process to check for RED ALARM */
 	init_timer(&alarmcheck);
 	alarmcheck.expires = 0;
@@ -832,19 +835,22 @@
 #ifdef ENABLE_TASKLETS
 	tasklet_init(&dahdi_dynamic_tlet, dahdi_dynamic_tasklet, 0);
 #endif
+	dahdi_set_dynamic_ops(&dahdi_dynamic_ops);
+
 	printk(KERN_INFO "DAHDI Dynamic Span support LOADED\n");
 	return 0;
 }
 
 static void dahdi_dynamic_cleanup(void)
 {
+	dahdi_set_dynamic_ops(NULL);
+
 #ifdef ENABLE_TASKLETS
 	if (taskletpending) {
 		tasklet_disable(&dahdi_dynamic_tlet);
 		tasklet_kill(&dahdi_dynamic_tlet);
 	}
 #endif
-	dahdi_set_dynamic_ioctl(NULL);
 	del_timer(&alarmcheck);
 	printk(KERN_INFO "DAHDI Dynamic Span support unloaded\n");
 }

Modified: linux/trunk/include/dahdi/kernel.h
URL: http://svnview.digium.com/svn/dahdi/linux/trunk/include/dahdi/kernel.h?view=diff&rev=10623&r1=10622&r2=10623
==============================================================================
--- linux/trunk/include/dahdi/kernel.h (original)
+++ linux/trunk/include/dahdi/kernel.h Tue Apr  3 14:44:27 2012
@@ -1260,8 +1260,13 @@
 extern u_char __dahdi_lin2a[16384];
 #endif
 
+struct dahdi_dynamic_ops {
+	struct module *owner;
+	int (*ioctl)(unsigned int cmd, unsigned long data);
+};
+
 /*! \brief Used by dynamic DAHDI -- don't use directly */
-void dahdi_set_dynamic_ioctl(int (*func)(unsigned int cmd, unsigned long data));
+void dahdi_set_dynamic_ops(const struct dahdi_dynamic_ops *ops);
 
 /*! \brief Used by DAHDI HPEC module -- don't use directly */
 void dahdi_set_hpec_ioctl(int (*func)(unsigned int cmd, unsigned long data));




More information about the svn-commits mailing list