param: add "mem" type for module_param/core_param

core_param is now called early enough to be useful for arch's mem=
parameter.  Some archs want fancier parsing, but this allows:

	static unsigned long mem_override;
	core_param(mem, mem_override, mem, 0444);

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
---
 include/linux/moduleparam.h |    6 +++++-
 kernel/params.c             |   38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/include/linux/moduleparam.h b/include/linux/moduleparam.h
--- a/include/linux/moduleparam.h
+++ b/include/linux/moduleparam.h
@@ -94,7 +94,7 @@ struct kparam_array
 	__module_param_call(MODULE_PARAM_PREFIX, name, set, get, arg, perm)
 
 /* Helper functions: type is byte, short, ushort, int, uint, long,
-   ulong, charp, bool or invbool, or XXX if you define param_get_XXX,
+   ulong, charp, bool or invbool, mem or XXX if you define param_get_XXX,
    param_set_XXX and param_check_XXX. */
 #define module_param_named(name, value, type, perm)			   \
 	param_check_##type(name, &(value));				   \
@@ -184,6 +184,10 @@ extern int param_get_invbool(char *buffe
 extern int param_get_invbool(char *buffer, struct kernel_param *kp);
 #define param_check_invbool(name, p) __param_check(name, p, int)
 
+extern int param_set_mem(const char *val, struct kernel_param *kp);
+extern int param_get_mem(char *buffer, struct kernel_param *kp);
+#define param_check_mem(name, p) __param_check(name, p, unsigned long)
+
 /* Comma-separated array: *nump is set to number they actually specified. */
 #define module_param_array_named(name, array, type, nump, perm)		\
 	static const struct kparam_array __param_arr_##name		\
diff --git a/kernel/params.c b/kernel/params.c
--- a/kernel/params.c
+++ b/kernel/params.c
@@ -264,6 +264,44 @@ int param_get_invbool(char *buffer, stru
 int param_get_invbool(char *buffer, struct kernel_param *kp)
 {
 	return sprintf(buffer, "%c", (*(int *)kp->arg) ? 'N' : 'Y');
+}
+
+int param_set_mem(const char *val, struct kernel_param *kp)
+{
+	unsigned long long mem;
+	char *endp;
+
+	if (!val || !*val)
+		return -EINVAL;
+
+	mem = memparse(val, &endp);
+	if (*endp)
+		return -EINVAL;
+
+	if ((unsigned long)mem != mem)
+		return -E2BIG;
+	*(unsigned long *)kp->arg = mem;
+	return 0;
+}
+
+int param_get_mem(char *buffer, struct kernel_param *kp)
+{
+	unsigned long mem = *(unsigned long *)kp->arg;
+	const char *suffix = "";
+
+	if (mem > 0) {
+		if (mem % (1024*1024*1024) == 0) {
+			suffix = "G";
+			mem /= 1024*1024*1024;
+		} else if (mem % (1024*1024) == 0) {
+			suffix = "M";
+			mem /= 1024*1024;
+		} else if (mem % 1024 == 0) {
+			suffix = "K";
+			mem /= 1024;
+		}
+	}
+	return sprintf(buffer, "%lu%s", mem, suffix);
 }
 
 /* We break the rule and mangle the string. */
