typesafe: Convert stop_machine

Using cast_if_type() we can have a callback funciton either of the
exactly correct type to take "data", or to take a void *.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
---
 include/linux/stop_machine.h |   13 ++++--
 kernel/stop_machine.c        |   89 ++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 96 insertions(+), 6 deletions(-)

diff -r e279190b7b43 include/linux/stop_machine.h
--- a/include/linux/stop_machine.h	Mon Jan 21 14:42:54 2008 +1100
+++ b/include/linux/stop_machine.h	Mon Jan 21 15:04:00 2008 +1100
@@ -5,9 +5,9 @@
    (and more).  So the "read" side to such a lock is anything which
    diables preeempt. */
 #include <linux/cpu.h>
+#include <linux/compiler.h>
 #include <asm/system.h>
 
-#if defined(CONFIG_STOP_MACHINE) && defined(CONFIG_SMP)
 /**
  * stop_machine_run: freeze the machine on all CPUs and run this function
  * @fn: the function to run
@@ -21,7 +21,12 @@
  *
  * This can be thought of as a very heavy write lock, equivalent to
  * grabbing every spinlock in the kernel. */
-int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu);
+#define stop_machine_run(fn, data, cpu)					\
+	stop_machine_run_notype(cast_if_type((fn), int(*)(typeof(data)), \
+					     int(*)(void *)), (data), (cpu))
+
+#if defined(CONFIG_STOP_MACHINE) && defined(CONFIG_SMP)
+int stop_machine_run_notype(int (*fn)(void *), void *data, unsigned int cpu);
 
 /**
  * __stop_machine_run: freeze the machine on all CPUs and run this function
@@ -38,8 +46,8 @@ struct task_struct *__stop_machine_run(i
 
 #else
 
-static inline int stop_machine_run(int (*fn)(void *), void *data,
-				   unsigned int cpu)
+static inline int stop_machine_run_notype(int (*fn)(void *), void *data,
+					  unsigned int cpu)
 {
 	int ret;
 	local_irq_disable();
diff -r e279190b7b43 kernel/stop_machine.c
--- a/kernel/stop_machine.c	Mon Jan 21 14:42:54 2008 +1100
+++ b/kernel/stop_machine.c	Mon Jan 21 15:04:00 2008 +1100
@@ -197,7 +197,7 @@ struct task_struct *__stop_machine_run(i
 	return p;
 }
 
-int stop_machine_run(int (*fn)(void *), void *data, unsigned int cpu)
+int stop_machine_run_notype(int (*fn)(void *), void *data, unsigned int cpu)
 {
 	struct task_struct *p;
 	int ret;
@@ -213,4 +213,89 @@ int stop_machine_run(int (*fn)(void *), 
 
 	return ret;
 }
-EXPORT_SYMBOL_GPL(stop_machine_run);
+EXPORT_SYMBOL_GPL(stop_machine_run_notype);
+
+#if 0 /* Compile test */
+int testfn_voidp(void *);
+int testfn_charp(char *);
+int testfn_char(char);
+int testfn_ulong(unsigned long);
+
+void test(void)
+{
+	char c;
+	unsigned long ul;
+	void *p;
+	struct { void *p; } s;
+
+	/*
+	 * Should work
+	 */
+	/* void * compatible with any pointer. */
+	stop_machine_run(testfn_voidp, NULL, 0);
+	stop_machine_run(testfn_voidp, p, 0);
+	stop_machine_run(testfn_voidp, &c, 0);
+	stop_machine_run(testfn_voidp, &ul, 0);
+
+	/* NULL callbacks compatible with any pointer. */
+	stop_machine_run(NULL, NULL, 0);
+	stop_machine_run(NULL, p, 0);
+	stop_machine_run(NULL, &c, 0);
+	stop_machine_run(NULL, &ul, 0);
+
+	/* Char * match. */
+	stop_machine_run(testfn_charp, &c, 0);
+
+	/* ulong can take any int or long. */
+	stop_machine_run(testfn_ulong, c, 0);
+	stop_machine_run(testfn_ulong, ul, 0);
+
+	/*
+	 * It would be nice if these worked, but they don't (void * arg).
+	 */
+	stop_machine_run(testfn_charp, NULL, 0);
+	stop_machine_run(testfn_charp, p, 0);
+	stop_machine_run(testfn_ulong, NULL, 0);
+	stop_machine_run(testfn_ulong, p, 0);
+
+	/*
+	 * Should complain.
+	 */
+
+	/* void * incompatible with non-pointers. */
+	stop_machine_run(testfn_voidp, c, 0);
+	stop_machine_run(testfn_voidp, ul, 0);
+	stop_machine_run(testfn_voidp, s, 0);
+
+	/* NULL callbacks incompatible with non-pointers. */
+	stop_machine_run(NULL, c, 0);
+	stop_machine_run(NULL, ul, 0);
+	stop_machine_run(NULL, s, 0);
+
+	/* char * function and unsigned long */
+	stop_machine_run(testfn_charp, &ul, 0);
+
+	/* char * incompatible with non-pointers */
+	stop_machine_run(testfn_charp, c, 0);
+	stop_machine_run(testfn_charp, ul, 0);
+	stop_machine_run(testfn_charp, s, 0);
+
+	/* A char function simply can't work as a callback. */
+	stop_machine_run(testfn_char, NULL, 0);
+	stop_machine_run(testfn_char, p, 0);
+	stop_machine_run(testfn_char, &c, 0);
+	stop_machine_run(testfn_char, &ul, 0);
+	stop_machine_run(testfn_char, c, 0);
+	stop_machine_run(testfn_char, ul, 0);
+	stop_machine_run(testfn_char, s, 0);
+
+	/* unsigned long function and char * */
+	stop_machine_run(testfn_ulong, &c, 0);
+
+	/* unsigned long function and unsigned long * */
+	stop_machine_run(testfn_ulong, &ul, 0);
+
+	/* unsigned long function and struct. */
+	stop_machine_run(testfn_ulong, s, 0);
+}
+#endif
