[PATCH] fixup loop blkdev, add module_get

From: Jeff Garzik (jgarzik@pobox.com)
Date: Sat Jan 11 2003 - 22:56:20 EST


Sometimes, we are absolutely certain that we have at least one module
reference "locked open" for us. Loop is an example of such a case: the
set-fd and clear-fd struct block_device_operations ioctls already have a
module reference from simply the block device being opened.

Therefore, we can just unconditionally increment the module refcount.
I added module_get to do this.

Implementing try_module_get in terms of module_get is left as an
exercise for the maintainer :) I am too lazy to investigate whether it
is ok for try_module_get to call module_is_live outside of the
get_cpu/put_cpu pair, which is a prereq for try_module_get using
module_get.

Patch against latest Linus BK tree follows... this also eliminates the
"deprecated" warnings for the loop device.

===== drivers/block/loop.c 1.78 vs edited =====
--- 1.78/drivers/block/loop.c Thu Dec 5 10:52:06 2002
+++ edited/drivers/block/loop.c Sat Jan 11 22:45:54 2003
@@ -642,7 +642,7 @@
         int lo_flags = 0;
         int error;
 
- MOD_INC_USE_COUNT;
+ module_get(THIS_MODULE);
 
         error = -EBUSY;
         if (lo->lo_state != Lo_unbound)
@@ -742,7 +742,7 @@
  out_putf:
         fput(file);
  out:
- MOD_DEC_USE_COUNT;
+ module_put(THIS_MODULE);
         return error;
 }
 
@@ -814,7 +814,7 @@
         filp->f_dentry->d_inode->i_mapping->gfp_mask = gfp;
         lo->lo_state = Lo_unbound;
         fput(filp);
- MOD_DEC_USE_COUNT;
+ module_put(THIS_MODULE);
         return 0;
 }
 
===== include/linux/module.h 1.38 vs edited =====
--- 1.38/include/linux/module.h Wed Jan 8 08:19:44 2003
+++ edited/include/linux/module.h Sat Jan 11 22:43:20 2003
@@ -249,6 +249,20 @@
 #define local_dec(x) atomic_dec(x)
 #endif
 
+/* bump a module's refcount.
+ *
+ * called only when we are absolutely certain that
+ * module != NULL, and the locking / call chain
+ * guarantees that the module won't disappear out
+ * from underneath us.
+ */
+static inline void module_get(struct module *module)
+{
+ unsigned int cpu = get_cpu();
+ local_inc(&module->ref[cpu].count);
+ put_cpu();
+}
+
 static inline int try_module_get(struct module *module)
 {
         int ret = 1;
@@ -277,6 +291,7 @@
 }
 
 #else /*!CONFIG_MODULE_UNLOAD*/
+static inline void module_get(struct module *module) {}
 static inline int try_module_get(struct module *module)
 {
         return !module || module_is_live(module);
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Wed Jan 15 2003 - 22:00:39 EST