Skip to contentSkip to conversation

alpha fp-emu vs module refcounting

DISCUSSION Christoph Hellwig 4 participants 10 messages May 7, 2004 – May 17, 2004 Alpha port

lore
Share & tools
lore
#0 lore raw

arch/alpha/math-emu/math.c still uses MOD_{INC,DEC}_USE_COUNT to protect from unloading which is

a) unsafe
b) in the way of nuking them as soon as 2.7 is out

any chance we could either do a try_module_get on a struct module *fp_emul_module in alpha core code before or completely disallow module unloading for it (by removing the cleanup_module() callback)?

#1 lore raw
Quoted 3 lines · Christoph Hellwig

any chance we could either do a try_module_get on a struct module *fp_emul_module in alpha core code before or completely disallow module unloading for it (by removing the cleanup_module() callback)?

How about this (also fixes modular build of math-emu)?

Ivan.

arch/alpha/math-emu/math.c +8 -6
@@ -51,7 +51,9 @@ extern void alpha_write_fp_reg_s (unsign
 
 #ifdef MODULE
 
+MODULE_AUTHOR("Richard Henderson <[email protected]>");
 MODULE_DESCRIPTION("FP Software completion module");
+MODULE_LICENSE("GPL");
 
 extern long (*alpha_fp_emul_imprecise)(struct pt_regs *, unsigned long);
 extern long (*alpha_fp_emul) (unsigned long pc);
@@ -106,7 +108,7 @@ alpha_fp_emul (unsigned long pc)
 	__u32 insn;
 	long si_code;
 
-	MOD_INC_USE_COUNT;
+	BUG_ON(!try_module_get(THIS_MODULE));
 
 	get_user(insn, (__u32*)pc);
 	fc     = (insn >>  0) & 0x1f;	/* destination register */
@@ -320,7 +322,7 @@ done:
 			if (_fex & IEEE_TRAP_ENABLE_INV) si_code = FPE_FLTINV;
 		}
 
-		MOD_DEC_USE_COUNT;
+		module_put(THIS_MODULE);
 		return si_code;
 	}
 
@@ -328,13 +330,13 @@ done:
 	   requires that the result *always* be written... so we do the write
 	   immediately after the operations above.  */
 
-	MOD_DEC_USE_COUNT;
+	module_put(THIS_MODULE);
 	return 0;
 
 bad_insn:
 	printk(KERN_ERR "alpha_fp_emul: Invalid FP insn %#x at %#lx\n",
 	       insn, pc);
-	MOD_DEC_USE_COUNT;
+	module_put(THIS_MODULE);
 	return -1;
 }
 
@@ -344,7 +346,7 @@ alpha_fp_emul_imprecise (struct pt_regs 
 	unsigned long trigger_pc = regs->pc - 4;
 	unsigned long insn, opcode, rc, si_code = 0;
 
-	MOD_INC_USE_COUNT;
+	BUG_ON(!try_module_get(THIS_MODULE));
 
 	/*
 	 * Turn off the bits corresponding to registers that are the
@@ -403,6 +405,6 @@ alpha_fp_emul_imprecise (struct pt_regs 
 	}
 
 egress:
-	MOD_DEC_USE_COUNT;
+	module_put(THIS_MODULE);
 	return si_code;
 }
arch/alpha/math-emu/Makefile +3 -1
@@ -4,4 +4,6 @@
 
 EXTRA_CFLAGS := -w
 
-obj-$(CONFIG_MATHEMU) += math.o qrnnd.o
+obj-$(CONFIG_MATHEMU) += math-emu.o
+
+math-emu-objs := math.o qrnnd.o
#2 lore raw

Review

Quoted 2 lines · Ivan Kokshaysky
(quoted diff) +1 -1
-	MOD_INC_USE_COUNT;
+	BUG_ON(!try_module_get(THIS_MODULE));

This is broken. If you know you already have a reference somewhere - and I can't find how that would work - you'd have to use __module_get, but if it's not you can get a failure from try_module_get legally and you should do the try_module_get outside the module, before it's code is exectuted.

#3 lore raw
Quoted 9 lines · Christoph Hellwig

Review

Quoted 2 lines · Ivan Kokshaysky
(quoted diff) +1 -1
-	MOD_INC_USE_COUNT;
+	BUG_ON(!try_module_get(THIS_MODULE));

This is broken. If you know you already have a reference somewhere - and I can't find how that would work - you'd have to use __module_get, but if it's not you can get a failure from try_module_get legally and you should do the try_module_get outside the module, before it's code is exectuted.

Ok, I realize - this seems to be confusing. I'll try to clarify:
- First of all, mere mortals are _not_ allowed to compile mandatory alpha IEEE fp emulation code as a module. Which is documented in arch/alpha/Kconfig.
- Roughly speaking, the fp emu _module_ code intercepts the fp traps, so races vs module loading/unloading are fundamentally unavoidable. These refcounting attempts just narrow the window.

And no, try_module_get should never fail here.

Alternatively, we could just drop _all_ module related stuff from alpha/math-emu...

Ivan.

#4 lore raw
Quoted 8 lines · Ivan Kokshaysky

Ok, I realize - this seems to be confusing. I'll try to clarify:
- First of all, mere mortals are _not_ allowed to compile mandatory alpha IEEE fp emulation code as a module. Which is documented in arch/alpha/Kconfig.
- Roughly speaking, the fp emu _module_ code intercepts the fp traps, so races vs module loading/unloading are fundamentally unavoidable. These refcounting attempts just narrow the window.

Quoted 4 lines

And no, try_module_get should never fail here.

Alternatively, we could just drop _all_ module related stuff from alpha/math-emu...

either that or just marking it unloadable by removing the cleanup_module handler sound like the simplest solution I guess.

#5 lore raw
Quoted 2 lines · Christoph Hellwig

either that or just marking it unloadable by removing the cleanup_module handler sound like the simplest solution I guess.

Or leave it as it is for now - 'CONFIG_MATHEMU=m' won't compile. ;-)

Ivan.

#6 lore raw
Quoted 5 lines · Ivan Kokshaysky
Quoted 2 lines · Christoph Hellwig

either that or just marking it unloadable by removing the cleanup_module handler sound like the simplest solution I guess.

Or leave it as it is for now - 'CONFIG_MATHEMU=m' won't compile. ;-)

Well, still false positives in grep. What about this patch to simply remove any traces of CONFIG_MATHEMU and modular math emulation?

arch/alpha/Kconfig +0 -8
@@ -625,14 +625,6 @@
 	  Say Y here if you are developing drivers or trying to debug and
 	  identify kernel problems.
 
-config MATHEMU
-	tristate "Kernel FP software completion" if DEBUG_KERNEL
-	default y if !DEBUG_KERNEL
-	help
-	  This option is required for IEEE compliant floating point arithmetic
-	  on the Alpha. The only time you would ever not say Y is to say M in
-	  order to debug the code. Say Y unless you know what you are doing.
-
 config DEBUG_SLAB
 	bool "Debug memory allocations"
 	depends on DEBUG_KERNEL

===== arch/alpha/kernel/alpha_ksyms.c 1.37 vs edited =====

arch/alpha/kernel/alpha_ksyms.c +0 -7
@@ -169,13 +169,6 @@
 EXPORT_SYMBOL(csum_partial_copy_from_user);
 EXPORT_SYMBOL(csum_ipv6_magic);
 
-#ifdef CONFIG_MATHEMU_MODULE
-extern long (*alpha_fp_emul_imprecise)(struct pt_regs *, unsigned long);
-extern long (*alpha_fp_emul) (unsigned long pc);
-EXPORT_SYMBOL(alpha_fp_emul_imprecise);
-EXPORT_SYMBOL(alpha_fp_emul);
-#endif
-
 #ifdef CONFIG_ALPHA_BROKEN_IRQ_MASK
 EXPORT_SYMBOL(__min_ipl);
 #endif

===== arch/alpha/kernel/traps.c 1.29 vs edited =====

arch/alpha/kernel/traps.c +0 -8
@@ -191,16 +191,8 @@
 	do_exit(SIGSEGV);
 }
 
-#ifndef CONFIG_MATHEMU
-static long dummy_emul(void) { return 0; }
-long (*alpha_fp_emul_imprecise)(struct pt_regs *regs, unsigned long writemask)
-  = (void *)dummy_emul;
-long (*alpha_fp_emul) (unsigned long pc)
-  = (void *)dummy_emul;
-#else
 long alpha_fp_emul_imprecise(struct pt_regs *regs, unsigned long writemask);
 long alpha_fp_emul (unsigned long pc);
-#endif
 
 asmlinkage void
 do_entArith(unsigned long summary, unsigned long write_mask,

===== arch/alpha/math-emu/Makefile 1.9 vs edited =====

arch/alpha/math-emu/Makefile +1 -1
@@ -4,4 +4,4 @@
 
 EXTRA_CFLAGS := -w
 
-obj-$(CONFIG_MATHEMU) += math.o qrnnd.o
+obj-y += math.o qrnnd.o

===== arch/alpha/math-emu/math.c 1.5 vs edited =====

arch/alpha/math-emu/math.c +0 -45

85 lines not on this page · raw message

#7 lore raw
Quoted 2 lines · Christoph Hellwig

Well, still false positives in grep. What about this patch to simply remove any traces of CONFIG_MATHEMU and modular math emulation?

Personally, I'm fine with it.

Yet another simple solution would be just to remove refcounting and only allow modular build of math-emu when CONFIG_SMP=n, which is safe vs module unload and still fine for debugging.

Richard?

Ivan.

#8 lore raw
Quoted 9 lines · Ivan Kokshaysky
Quoted 2 lines · Christoph Hellwig

Well, still false positives in grep. What about this patch to simply remove any traces of CONFIG_MATHEMU and modular math emulation?

Personally, I'm fine with it.

Yet another simple solution would be just to remove refcounting and only allow modular build of math-emu when CONFIG_SMP=n, which is safe vs module unload and still fine for debugging.

UP is not safe vs module unloading per se. Especially not with CONFIG_PREEMPT. I'd say just apply the patch and if someone badly needs modular mathemu for debugging he/she can apply some local hack.

#9 lore raw
In reply to Ivan Kokshaysky
Quoted 5 lines · Ivan Kokshaysky

Yet another simple solution would be just to remove refcounting and only allow modular build of math-emu when CONFIG_SMP=n, which is safe vs module unload and still fine for debugging.

Richard?

Seems reasonable. We've already got it under the debugging config section, right?

r~