Skip to contentSkip to conversation

fixup loop blkdev, add module_get

PATCH In review Jeff Garzik 3 participants 5 messages Jan 12, 2003 – Jan 13, 2003

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

PATCH 1/1 fixup loop blkdev, add module_get

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 =====

drivers/block/loop.c +3 -3
@@ -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 =====

include/linux/module.h +15 -0
@@ -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);
#1 lore raw

In message <[email protected]> you write:

Quoted 7 lines

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.

Hi Jeff,

We may yet want such a primitive, but I've been resisting it
for the moment.

Firstly, because it's a very specialized and rare case which
lends itself to being abused, and secondly because if I "rmmod --wait" the module, then such operations which try to hold the module in place *should* fail. Not doing so is impolite, at least.

Sure, it's a fairly strange corner case, but that argument
cuts both ways.

I prefer this patch (-EBUSY is a little unusual, but as clear as any).

Thoughts?
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

drivers/block/loop.c +7 -3
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5-bk/drivers/block/loop.c working-2.5-bk-loop/drivers/block/loop.c
@@ -642,7 +642,9 @@ static int loop_set_fd(struct loop_devic
 	int		lo_flags = 0;
 	int		error;
 
-	MOD_INC_USE_COUNT;
+	if (!try_module_get(THIS_MODULE))
+		/* I'm going away: pretend I'm not here. */
+		return -EBUSY;
 
 	error = -EBUSY;
 	if (lo->lo_state != Lo_unbound)
@@ -742,7 +744,7 @@ static int loop_set_fd(struct loop_devic
  out_putf:
 	fput(file);
  out:
-	MOD_DEC_USE_COUNT;
+	module_put(THIS_MODULE);
 	return error;
 }
 
@@ -814,7 +816,9 @@ static int loop_clr_fd(struct loop_devic
 	filp->f_dentry->d_inode->i_mapping->gfp_mask = gfp;
 	lo->lo_state = Lo_unbound;
 	fput(filp);
-	MOD_DEC_USE_COUNT;
+
+	/* This won't drop to zero, since we're inside an ioctl. */
+	module_put(THIS_MODULE);
 	return 0;
 }
 
#2 lore raw
Quoted 11 lines · Rusty Russell

In message <[email protected]> you write:

Quoted 7 lines

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.

Hi Jeff,

Quoted 7 lines

We may yet want such a primitive, but I've been resisting it
for the moment.

Firstly, because it's a very specialized and rare case which
lends itself to being abused, and secondly because if I "rmmod --wait" the module, then such operations which try to hold the module in place *should* fail. Not doing so is impolite, at least.

Eh... You are trying to chase infinity with 'rmmod --wait'.

More below...

Quoted 4 lines

diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5-bk/drivers/block/loop.c working-2.5-bk-loop/drivers/block/loop.c

drivers/block/loop.c static int loop_set_fd(struct loop_devic · line 650

Quoted 10 lines
drivers/block/loop.c +3 -1
@@ -642,7 +642,9 @@ static int loop_set_fd(struct loop_devic
 	int		lo_flags = 0;
 	int		error;
 
-	MOD_INC_USE_COUNT;
+	if (!try_module_get(THIS_MODULE))
+		/* I'm going away: pretend I'm not here. */
+		return -EBUSY;
 
 	error = -EBUSY;
 	if (lo->lo_state != Lo_unbound)

I disagree:

1) we do not prevent root from shooting themselves in the foot,

2) moreover we do not prevent them from doing something that may be perfectly reasonable,

3) and this kind of code just adds error handling for no reason, when _not_ handling the error keeps the code more clean.

In general this is just caring way too much about an obscure corner case. Is the increased complexity of error handling when we _know_ the refcnt is locked for worth it?

Note that Linus turned off the 'deprecated' warning because MOD.*COUNT users are just too frequent, still.

Jeff

#3 lore raw

In message <[email protected]> you write:

Quoted 15 lines · Rusty Russell
Quoted 14 lines

In message <[email protected]> you write:

Quoted 7 lines

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.

Hi Jeff,

We may yet want such a primitive, but I've been resisting it
for the moment.

Quoted 6 lines
Quoted 4 lines

Firstly, because it's a very specialized and rare case which
lends itself to being abused, and secondly because if I "rmmod --wait" the module, then such operations which try to hold the module in place *should* fail. Not doing so is impolite, at least.

Eh... You are trying to chase infinity with 'rmmod --wait'.

No, you are trying to remove something and you want to chase down and kill the users, scripts, whatever. It guarantees that no new users will access the module.

Quoted 3 lines

I disagree:

1) we do not prevent root from shooting themselves in the foot,

I don't understand this point.

Quoted 2 lines

2) moreover we do not prevent them from doing something that may be perfectly reasonable,

Nor this one, which seems to bethe same.

Quoted 2 lines

3) and this kind of code just adds error handling for no reason, when _not_ handling the error keeps the code more clean.

No, the reason is simple: the admin has said they want the damn module removed. They've *told* you what they want. Why do you want to disobey them? 8)

Quoted 3 lines

In general this is just caring way too much about an obscure corner case. Is the increased complexity of error handling when we _know_ the refcnt is locked for worth it?

Is the increased complexity of another primitive for "you know you have a refcount" worth it? 8)

If there were 10 of these cases, sure, a __try_module_get() makes
sense: IMHO this is one of those areas on which intelligent people can disagree, I think.

Quoted 2 lines

Note that Linus turned off the 'deprecated' warning because MOD.*COUNT users are just too frequent, still.

Note that I didn't put the damn thing in there 8)

Hope he turned them back into macros, so the __unsafe runtime warning doesn't report "module.h".

Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

#4 lore raw

Hi,

Quoted 3 lines · Rusty Russell
Quoted 1 line

1) we do not prevent root from shooting themselves in the foot,

I don't understand this point.

Something to remember here: When we kill processes with SIGKILL, do we risk kernel stability? E.g. do we turn TASK_UNINTERRUPTIBLE into TASK_INTERRUPTIBLE, do we allow to kill init or do we leave resources behind?

Quoted 6 lines
Quoted 2 lines

3) and this kind of code just adds error handling for no reason, when _not_ handling the error keeps the code more clean.

No, the reason is simple: the admin has said they want the damn module removed. They've *told* you what they want. Why do you want to disobey them? 8)

How do you want to make this mechanism halfway safe to use? Did you check that all modules can still be deconfigured after you put them into the going state via the wait option?
Both the wait and the force option are extremely dangerous options. You are trying to implement something behind the modules back, what can only go wrong as soon as the module becomes slightly more complex. If you want to force the removal of a module, you should least attempt to keep it safe and this is not possible without the help of the module. A global don't-use switch is a worse idea than the global module count, if it's forced onto the modules.
I'm really curious when we get the first bug reports from people, who "only" unloaded a module. :(

bye, Roman