Skip to contentSkip to conversation

RFC/Preliminary patch - simpler raceless module unloading

PATCH Adam J. Richter 1 participant 2 messages Dec 23, 2002

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Here is a completely untested patch to illustrate a simpler
way to raceless module unloading. Code that uses this scheme does not have to deal with the possibilty of try_module_get failing, and, if all users of the old mechanism are converted, we will be able to delete the new most fundamental type of locking primitive code in kernel/module.c.

I've kept this code mostly separated in <linux/module_rwsem.h>
and kernel/module_rwsem.c so that it will not necessitate pulling <linux/module.h> into more include files and it should be more portable across different module loading schems.

The basic theory of operation is that the data structure for
looking up a given type of driver by name or number should be protected by an rw_semaphore (e.g., registering a file system type, finding a file system type by name). When a driver is registered, it call register_module_rwsem to associate to tell the module system that it needs to acquire a write lock on the given rwsem before attempting to remove the module (or checking its reference count for that purpose). The get_foo_driver_by_name(name) and put_foo_driver(driver) routines take a read lock on the rw_semaphore (or assume that they are called with the read lock held) while they look up the driver and manipulate its module reference count. register_foo_driver a takes a write lock on the rw_semaphore (or requires the caller to be holding a write lock on it). unregister_foo_driver assumes that the caller already holds a write lock on the semaphore because sys_delete_module already acquirs all of the rwsem's that were registered for a given module by register_module_rwsem. As a result, no attempt to look up a driver by name or modify its reference count can overlap with the part of sys_delete_module that checks the module reference count through completion of the module's unload routine.

I plan to port net_device, filesystem, file_operations,
block_device_operations, and device_driver to use it. So if what I've described above is too verbose, it should become clearer once I post some ports of code to it.

Signature · 4 lines
Adam J. Richter     __     ______________   575 Oroville Road
[email protected]     \ /                  Milpitas, California 95035
+1 408 309-6081         | g g d r a s i l   United States of America
                         "Free Software For The Rest Of Us."
include/linux/module.h +5 -0
@@ -157,6 +157,11 @@
 	/* Reference counts */
 	struct module_ref ref[NR_CPUS];
 
+	/* Which rwsems to grab before trying to unload the module.
+	   This list of struct module_rwsem is in order sorted by
+	   module_rwsem.rwsem address. */
+	struct list_head rwsems;
+
 	/* What modules depend on me? */
 	struct list_head modules_which_use_me;
 
include/linux/module_rwsem.h +23 -0
@@ -0,0 +1,23 @@
+#ifndef _LINUX_MODULE_RWSEM_H
+#define _LINUX_MODULE_RWSEM_H
+
+/* This is separate from module.h so that most headers will not need
+   to include <linux/module.h>. */
+
+#include <linux/list.h>
+
+struct module;
+struct rw_semaphore;
+
+struct rw_semaphore;
+struct module_rwsem {
+	struct rw_semaphore	*rwsem;
+	struct module		*module;
+	struct list_head	same_module;
+};
+
+extern void register_module_rwsem(struct module_rwsem *);
+extern int module_rwsems_down(struct module *);
+extern void module_rwsems_up(struct module *);
+
+#endif
kernel/module.c +10 -1
@@ -17,6 +17,7 @@
 */
 #include <linux/config.h>
 #include <linux/module.h>
+#include <linux/module_rwsem.h>
 #include <linux/moduleloader.h>
 #include <linux/init.h>
 #include <linux/slab.h>
@@ -116,6 +117,7 @@
 {
 	unsigned int i;
 
+	INIT_LIST_HEAD(&mod->rwsems);
 	INIT_LIST_HEAD(&mod->modules_which_use_me);
 	for (i = 0; i < NR_CPUS; i++)
 		atomic_set(&mod->ref[i].count, 0);
@@ -377,9 +379,13 @@
 	mod = find_module(name);
 	if (!mod) {
 		ret = -ENOENT;
-		goto out;
+		goto out_no_rwsems;
 	}
 
+	ret = module_rwsems_down(mod);
+	if (ret)
+		goto out_no_rwsems;
+
 	if (!list_empty(&mod->modules_which_use_me)) {
 		/* Other modules depend on us: get rid of them first. */
 		ret = -EWOULDBLOCK;
@@ -457,6 +463,9 @@
 	free_module(mod);
 
  out:
+	module_rwsems_up(mod);
+
+ out_no_rwsems:
 	up(&module_mutex);
 	return ret;
 }
kernel/module_rwsem.c +163 -0

163 lines not on this page · raw message

#1 lore raw
Quoted 4 lines · I wrote:

I plan to port net_device, filesystem, file_operations,
block_device_operations, and device_driver to use it. So if what I've described above is too verbose, it should become clearer once I post some ports of code to it.

Here is a preliminary port of fs/filesystems.c, although I have
not done the clean-ups it enables. I am running it now and have verified that I can at least load and unload a filesystem module and that I'm still prevented from unloading a filesystem module that is in use.

Adam J. Richter     __     ______________   575 Oroville Road
[email protected]     \ /                  Milpitas, California 95035
+1 408 309-6081         | g g d r a s i l   United States of America

"Free Software For The Rest Of Us."

include/linux/fs.h +3 -1
@@ -19,6 +19,7 @@
 #include <linux/stat.h>
 #include <linux/cache.h>
 #include <linux/radix-tree.h>
+#include <linux/module_rwsem.h>
 #include <asm/atomic.h>
 
 struct iovec;
@@ -953,7 +954,8 @@
 	int fs_flags;
 	struct super_block *(*get_sb) (struct file_system_type *, int, char *, void *);
 	void (*kill_sb) (struct super_block *);
-	struct module *owner;
+	struct module *owner;	/* Will be subsumed into mod_rwsem. */
+	struct module_rwsem	mod_rwsem;
 	struct file_system_type * next;
 	struct list_head fs_supers;
 };
fs/filesystems.c +23 -19

127 lines not on this page · raw message