2002-11-26 06:46:39

by Rusty Russell

[permalink] [raw]
Subject: [PATCH] module_param() 1/3

Linus,
This is the core support, with the out-by-one error which made
it not boot with 0 params fixed (Doh!). Tested on 2.5.49 (with a test
parameter and without).

The comment says __setup() is obsolete, but you might want to
delete that: there are some real cases where a top-level argument is
desired, and __module_param_call() is a bit of a mouthful 8)

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

Name: Parameter Core Patch
Author: Rusty Russell
Status: Tested on 2.5.49
Depends: Misc/strcspn.patch.gz
Depends: Module/module-i386.patch.gz

D: This patch is a rewrite of the insmod and boot parameter handling,
D: to unify them.
D:
D: The new format is fairly simple: built on top of __module_param_call there
D: are several helpers, eg "module_param(foo, int, 000)". The final argument
D: is the permissions bits, for exposing parameters in sysfs (if
D: non-zero) at a later stage.

diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .19111-linux-2.5.49/arch/i386/vmlinux.lds.S .19111-linux-2.5.49.updated/arch/i386/vmlinux.lds.S
--- .19111-linux-2.5.49/arch/i386/vmlinux.lds.S 2002-11-19 09:58:38.000000000 +1100
+++ .19111-linux-2.5.49.updated/arch/i386/vmlinux.lds.S 2002-11-25 13:29:05.000000000 +1100
@@ -67,6 +67,9 @@ SECTIONS
__setup_start = .;
.init.setup : { *(.init.setup) }
__setup_end = .;
+ __param_start = .;
+ .param.init : { *(.param.init) }
+ __param_end = .;
__initcall_start = .;
.initcall.init : {
*(.initcall1.init)
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .19111-linux-2.5.49/arch/ia64/vmlinux.lds.S .19111-linux-2.5.49.updated/arch/ia64/vmlinux.lds.S
--- .19111-linux-2.5.49/arch/ia64/vmlinux.lds.S 2002-11-05 10:54:24.000000000 +1100
+++ .19111-linux-2.5.49.updated/arch/ia64/vmlinux.lds.S 2002-11-25 13:29:05.000000000 +1100
@@ -102,6 +102,10 @@ SECTIONS
.init.setup : AT(ADDR(.init.setup) - PAGE_OFFSET)
{ *(.init.setup) }
__setup_end = .;
+ __param_start = .;
+ .param.init : AT(ADDR(.param.init) - PAGE_OFFSET)
+ { *(.param.init) }
+ __param_end = .;
__initcall_start = .;
.initcall.init : AT(ADDR(.initcall.init) - PAGE_OFFSET)
{
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .19111-linux-2.5.49/arch/ppc/vmlinux.lds.S .19111-linux-2.5.49.updated/arch/ppc/vmlinux.lds.S
--- .19111-linux-2.5.49/arch/ppc/vmlinux.lds.S 2002-11-11 20:00:54.000000000 +1100
+++ .19111-linux-2.5.49.updated/arch/ppc/vmlinux.lds.S 2002-11-25 13:29:05.000000000 +1100
@@ -103,6 +103,9 @@ SECTIONS
__setup_start = .;
.init.setup : { *(.init.setup) }
__setup_end = .;
+ __param_start = .;
+ .param.init : { *(.param.init) }
+ __param_end = .;
__initcall_start = .;
.initcall.init : {
*(.initcall1.init)
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .19111-linux-2.5.49/arch/ppc64/vmlinux.lds.S .19111-linux-2.5.49.updated/arch/ppc64/vmlinux.lds.S
--- .19111-linux-2.5.49/arch/ppc64/vmlinux.lds.S 2002-11-11 20:00:55.000000000 +1100
+++ .19111-linux-2.5.49.updated/arch/ppc64/vmlinux.lds.S 2002-11-25 13:29:05.000000000 +1100
@@ -91,6 +91,9 @@ SECTIONS
__setup_start = .;
.init.setup : { *(.init.setup) }
__setup_end = .;
+ __param_start = .;
+ .param.init : { *(.param.init) }
+ __param_end = .;
__initcall_start = .;
.initcall.init : {
*(.initcall1.init)
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .19111-linux-2.5.49/arch/sparc64/vmlinux.lds.S .19111-linux-2.5.49.updated/arch/sparc64/vmlinux.lds.S
--- .19111-linux-2.5.49/arch/sparc64/vmlinux.lds.S 2002-11-25 08:43:49.000000000 +1100
+++ .19111-linux-2.5.49.updated/arch/sparc64/vmlinux.lds.S 2002-11-25 13:29:05.000000000 +1100
@@ -51,6 +51,9 @@ SECTIONS
__setup_start = .;
.init.setup : { *(.init.setup) }
__setup_end = .;
+ __param_start = .;
+ .param.init : { *(.param.init) }
+ __param_end = .;
__initcall_start = .;
.initcall.init : {
*(.initcall1.init)
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .19111-linux-2.5.49/include/linux/init.h .19111-linux-2.5.49.updated/include/linux/init.h
--- .19111-linux-2.5.49/include/linux/init.h 2002-11-19 09:58:51.000000000 +1100
+++ .19111-linux-2.5.49.updated/include/linux/init.h 2002-11-25 13:29:05.000000000 +1100
@@ -86,19 +86,15 @@ typedef void (*exitcall_t)(void);
#define __exitcall(fn) \
static exitcall_t __exitcall_##fn __exit_call = fn

-/*
- * Used for kernel command line parameter setup
- */
-struct kernel_param {
+struct obs_kernel_param {
const char *str;
int (*setup_func)(char *);
};

-extern struct kernel_param __setup_start, __setup_end;
-
+/* OBSOLETE: see params.h for the right way. */
#define __setup(str, fn) \
static char __setup_str_##fn[] __initdata = str; \
- static struct kernel_param __setup_##fn \
+ static struct obs_kernel_param __setup_##fn \
__attribute__((unused,__section__ (".init.setup"))) \
= { __setup_str_##fn, fn }

@@ -177,6 +173,16 @@ extern struct kernel_param __setup_start
/* Data marked not to be saved by software_suspend() */
#define __nosavedata __attribute__ ((__section__ (".data.nosave")))

+/* This means "can be init if no module support, otherwise module load
+ may call it." */
+#ifdef CONFIG_MODULES
+#define __init_or_module
+#define __initdata_or_module
+#else
+#define __init_or_module __init
+#define __initdata_or_module __initdata
+#endif /*CONFIG_MODULES*/
+
#ifdef CONFIG_HOTPLUG
#define __devinit
#define __devinitdata
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .19111-linux-2.5.49/include/linux/kernel.h .19111-linux-2.5.49.updated/include/linux/kernel.h
--- .19111-linux-2.5.49/include/linux/kernel.h 2002-11-19 09:58:51.000000000 +1100
+++ .19111-linux-2.5.49.updated/include/linux/kernel.h 2002-11-25 13:29:05.000000000 +1100
@@ -71,7 +71,7 @@ extern int sscanf(const char *, const ch
extern int vsscanf(const char *, const char *, va_list);

extern int get_option(char **str, int *pint);
-extern char *get_options(char *str, int nints, int *ints);
+extern char *get_options(const char *str, int nints, int *ints);
extern unsigned long long memparse(char *ptr, char **retptr);
extern void dev_probe_lock(void);
extern void dev_probe_unlock(void);
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .19111-linux-2.5.49/include/linux/moduleparam.h .19111-linux-2.5.49.updated/include/linux/moduleparam.h
--- .19111-linux-2.5.49/include/linux/moduleparam.h 1970-01-01 10:00:00.000000000 +1000
+++ .19111-linux-2.5.49.updated/include/linux/moduleparam.h 2002-11-25 13:29:05.000000000 +1100
@@ -0,0 +1,123 @@
+#ifndef _LINUX_MODULE_PARAMS_H
+#define _LINUX_MODULE_PARAMS_H
+/* (C) Copyright 2001, 2002 Rusty Russell IBM Corporation */
+#include <linux/init.h>
+#include <linux/stringify.h>
+
+/* You can override this manually, but generally this should match the
+ module name. */
+#define MODULE_PARAM_PREFIX __stringify(KBUILD_MODNAME) "."
+
+struct kernel_param;
+
+/* Returns 0, or -errno. arg is in kp->arg. */
+typedef int (*param_set_fn)(const char *val, struct kernel_param *kp);
+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
+typedef int (*param_get_fn)(char *buffer, struct kernel_param *kp);
+
+struct kernel_param {
+ const char *name;
+ unsigned int perm;
+ param_set_fn set;
+ param_get_fn get;
+ void *arg;
+};
+
+/* Special one for strings we want to copy into */
+struct kparam_string {
+ unsigned int maxlen;
+ char *string;
+};
+
+/* This is the fundamental function for registering boot/module
+ parameters. perm sets the visibility in driverfs: 000 means it's
+ not there, read bits mean it's readable, write bits mean it's
+ writable. */
+#define __module_param_call(prefix, name, set, get, arg, perm) \
+ static char __param_str_##name[] __initdata = prefix #name; \
+ static struct kernel_param __param_##name \
+ __attribute__ ((unused,__section__ (".param.init"))) \
+ = { __param_str_##name, perm, set, get, arg }
+
+#define module_param_call(name, set, get, arg, perm) \
+ __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,
+ param_set_XXX and param_check_XXX. */
+#define module_param_named(name, value, type, perm) \
+ param_check_##type(name, &(value)); \
+ module_param_call(name, param_set_##type, param_get_##type, &value, perm)
+
+#define module_param(name, type, perm) \
+ module_param_named(name, name, type, perm)
+
+/* Actually copy string: maxlen param is usually sizeof(string). */
+#define module_param_string(name, string, len, perm) \
+ static struct kparam_string __param_string_##name __initdata \
+ = { len, string }; \
+ module_param_call(name, param_set_copystring, param_get_charp, \
+ &__param_string_##name, perm)
+
+/* Called on module insert or kernel boot */
+extern int parse_args(const char *name,
+ char *args,
+ struct kernel_param *params,
+ unsigned num,
+ int (*unknown)(char *param, char *val));
+
+/* All the helper functions */
+/* The macros to do compile-time type checking stolen from Jakub
+ Jelinek, who IIRC came up with this idea for the 2.4 module init code. */
+#define __param_check(name, p, type) \
+ static inline type *__check_##name(void) { return(p); }
+
+extern int param_set_short(const char *val, struct kernel_param *kp);
+extern int param_get_short(char *buffer, struct kernel_param *kp);
+#define param_check_short(name, p) __param_check(name, p, short)
+
+extern int param_set_ushort(const char *val, struct kernel_param *kp);
+extern int param_get_ushort(char *buffer, struct kernel_param *kp);
+#define param_check_ushort(name, p) __param_check(name, p, unsigned short)
+
+extern int param_set_int(const char *val, struct kernel_param *kp);
+extern int param_get_int(char *buffer, struct kernel_param *kp);
+#define param_check_int(name, p) __param_check(name, p, int)
+
+extern int param_set_uint(const char *val, struct kernel_param *kp);
+extern int param_get_uint(char *buffer, struct kernel_param *kp);
+#define param_check_uint(name, p) __param_check(name, p, unsigned int)
+
+extern int param_set_long(const char *val, struct kernel_param *kp);
+extern int param_get_long(char *buffer, struct kernel_param *kp);
+#define param_check_long(name, p) __param_check(name, p, long)
+
+extern int param_set_ulong(const char *val, struct kernel_param *kp);
+extern int param_get_ulong(char *buffer, struct kernel_param *kp);
+#define param_check_ulong(name, p) __param_check(name, p, unsigned long)
+
+extern int param_set_charp(const char *val, struct kernel_param *kp);
+extern int param_get_charp(char *buffer, struct kernel_param *kp);
+#define param_check_charp(name, p) __param_check(name, p, char *)
+
+extern int param_set_bool(const char *val, struct kernel_param *kp);
+extern int param_get_bool(char *buffer, struct kernel_param *kp);
+#define param_check_bool(name, p) __param_check(name, p, int)
+
+extern int param_set_invbool(const char *val, struct kernel_param *kp);
+extern int param_get_invbool(char *buffer, struct kernel_param *kp);
+#define param_check_invbool(name, p) __param_check(name, p, int)
+
+/* First two elements are the max and min array length (which don't change) */
+extern int param_set_intarray(const char *val, struct kernel_param *kp);
+extern int param_get_intarray(char *buffer, struct kernel_param *kp);
+#define param_check_intarray(name, p) __param_check(name, p, int *)
+
+extern int param_set_copystring(const char *val, struct kernel_param *kp);
+
+int param_array(const char *name,
+ const char *val,
+ unsigned int min, unsigned int max,
+ void *elem, int elemsize,
+ int (*set)(const char *, struct kernel_param *kp));
+#endif /* _LINUX_MODULE_PARAM_TYPES_H */
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .19111-linux-2.5.49/init/main.c .19111-linux-2.5.49.updated/init/main.c
--- .19111-linux-2.5.49/init/main.c 2002-11-19 09:58:52.000000000 +1100
+++ .19111-linux-2.5.49.updated/init/main.c 2002-11-25 13:29:05.000000000 +1100
@@ -33,6 +33,7 @@
#include <linux/workqueue.h>
#include <linux/profile.h>
#include <linux/rcupdate.h>
+#include <linux/moduleparam.h>

#include <asm/io.h>
#include <asm/bugs.h>
@@ -136,9 +137,10 @@ char * envp_init[MAX_INIT_ENVS+2] = { "H

__setup("profile=", profile_setup);

-static int __init checksetup(char *line)
+static int __init obsolete_checksetup(char *line)
{
- struct kernel_param *p;
+ struct obs_kernel_param *p;
+ extern struct obs_kernel_param __setup_start, __setup_end;

p = &__setup_start;
do {
@@ -221,74 +223,45 @@ static int __init quiet_kernel(char *str
__setup("debug", debug_kernel);
__setup("quiet", quiet_kernel);

-/*
- * This is a simple kernel command line parsing function: it parses
- * the command line, and fills in the arguments/environment to init
- * as appropriate. Any cmd-line option is taken to be an environment
- * variable if it contains the character '='.
- *
- * This routine also checks for options meant for the kernel.
- * These options are not given to init - they are for internal kernel use only.
- */
-static void __init parse_options(char *line)
+/* Unknown boot options get handed to init, unless they look like
+ failed parameters */
+static int __init unknown_bootoption(char *param, char *val)
{
- char *next,*quote;
- int args, envs;
+ /* Change NUL term back to "=", to make "param" the whole string. */
+ if (val)
+ val[-1] = '=';

- if (!*line)
- return;
- args = 0;
- envs = 1; /* TERM is set to 'linux' by default */
- next = line;
- while ((line = next) != NULL) {
- quote = strchr(line,'"');
- next = strchr(line, ' ');
- while (next != NULL && quote != NULL && quote < next) {
- /* we found a left quote before the next blank
- * now we have to find the matching right quote
- */
- next = strchr(quote+1, '"');
- if (next != NULL) {
- quote = strchr(next+1, '"');
- next = strchr(next+1, ' ');
- }
- }
- if (next != NULL)
- *next++ = 0;
- if (!strncmp(line,"init=",5)) {
- line += 5;
- execute_command = line;
- /* In case LILO is going to boot us with default command line,
- * it prepends "auto" before the whole cmdline which makes
- * the shell think it should execute a script with such name.
- * So we ignore all arguments entered _before_ init=... [MJ]
- */
- args = 0;
- continue;
+ /* Handle obsolete-style parameters */
+ if (obsolete_checksetup(param))
+ return 0;
+
+ /* Preemptive maintenance for "why didn't my mispelled command
+ line work?" */
+ if (strchr(param, '.') && (!val || strchr(param, '.') < val)) {
+ printk(KERN_ERR "Unknown boot option `%s': ignoring\n", param);
+ return 0;
+ }
+
+ if (val) {
+ /* Environment option */
+ unsigned int i;
+ for (i = 0; envp_init[i]; i++) {
+ if (i == MAX_INIT_ENVS)
+ panic("Too many boot env vars at `%s'", param);
}
- if (checksetup(line))
- continue;
-
- /*
- * Then check if it's an environment variable or
- * an option.
- */
- if (strchr(line,'=')) {
- if (envs >= MAX_INIT_ENVS)
- break;
- envp_init[++envs] = line;
- } else {
- if (args >= MAX_INIT_ARGS)
- break;
- if (*line)
- argv_init[++args] = line;
+ envp_init[i] = param;
+ } else {
+ /* Command line option */
+ unsigned int i;
+ for (i = 0; argv_init[i]; i++) {
+ if (i == MAX_INIT_ARGS)
+ panic("Too many boot init vars at `%s'",param);
}
+ argv_init[i] = param;
}
- argv_init[args+1] = NULL;
- envp_init[envs+1] = NULL;
+ return 0;
}

-
extern void setup_arch(char **);
extern void cpu_idle(void);

@@ -382,6 +355,7 @@ asmlinkage void __init start_kernel(void
{
char * command_line;
extern char saved_command_line[];
+ extern struct kernel_param __param_start, __param_end;
/*
* Interrupts are still disabled. Do necessary setups, then
* enable them
@@ -393,7 +367,9 @@ asmlinkage void __init start_kernel(void
build_all_zonelists();
page_alloc_init();
printk("Kernel command line: %s\n", saved_command_line);
- parse_options(command_line);
+ parse_args("Booting kernel", command_line, &__param_start,
+ &__param_end - &__param_start,
+ &unknown_bootoption);
trap_init();
extable_init();
rcu_init();
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .19111-linux-2.5.49/kernel/Makefile .19111-linux-2.5.49.updated/kernel/Makefile
--- .19111-linux-2.5.49/kernel/Makefile 2002-11-25 08:44:18.000000000 +1100
+++ .19111-linux-2.5.49.updated/kernel/Makefile 2002-11-25 13:30:40.000000000 +1100
@@ -4,13 +4,13 @@

export-objs = signal.o sys.o kmod.o workqueue.o ksyms.o pm.o exec_domain.o \
printk.o platform.o suspend.o dma.o module.o cpufreq.o \
- profile.o rcupdate.o intermodule.o
+ profile.o rcupdate.o intermodule.o params.o

obj-y = sched.o fork.o exec_domain.o panic.o printk.o profile.o \
exit.o itimer.o time.o softirq.o resource.o \
sysctl.o capability.o ptrace.o timer.o user.o \
signal.o sys.o kmod.o workqueue.o futex.o platform.o pid.o \
- rcupdate.o intermodule.o extable.o
+ rcupdate.o intermodule.o extable.o params.o

obj-$(CONFIG_GENERIC_ISA_DMA) += dma.o
obj-$(CONFIG_SMP) += cpu.o
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .19111-linux-2.5.49/kernel/params.c .19111-linux-2.5.49.updated/kernel/params.c
--- .19111-linux-2.5.49/kernel/params.c 1970-01-01 10:00:00.000000000 +1000
+++ .19111-linux-2.5.49.updated/kernel/params.c 2002-11-25 13:29:05.000000000 +1100
@@ -0,0 +1,338 @@
+/* Helpers for initial module or kernel cmdline parsing
+ Copyright (C) 2001 Rusty Russell.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+#include <linux/moduleparam.h>
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/errno.h>
+#include <linux/module.h>
+
+#if 0
+#define DEBUGP printk
+#else
+#define DEBUGP(fmt , ...)
+#endif
+
+static int parse_one(char *param,
+ char *val,
+ struct kernel_param *params,
+ unsigned num_params,
+ int (*handle_unknown)(char *param, char *val))
+{
+ unsigned int i;
+
+ /* Find parameter */
+ for (i = 0; i < num_params; i++) {
+ if (strcmp(param, params[i].name) == 0) {
+ DEBUGP("They are equal! Calling %p\n",
+ params[i].set);
+ return params[i].set(val, &params[i]);
+ }
+ }
+
+ if (handle_unknown) {
+ DEBUGP("Unknown argument: calling %p\n", handle_unknown);
+ return handle_unknown(param, val);
+ }
+
+ DEBUGP("Unknown argument `%s'\n", param);
+ return -ENOENT;
+}
+
+/* You can use " around spaces, but can't escape ". */
+/* Hyphens and underscores equivalent in parameter names. */
+static char *next_arg(char *args, char **param, char **val)
+{
+ unsigned int i, equals = 0;
+ int in_quote = 0;
+
+ /* Chew any extra spaces */
+ while (*args == ' ') args++;
+
+ for (i = 0; args[i]; i++) {
+ if (args[i] == ' ' && !in_quote)
+ break;
+ if (equals == 0) {
+ if (args[i] == '=')
+ equals = i;
+ else if (args[i] == '-')
+ args[i] = '_';
+ }
+ if (args[i] == '"')
+ in_quote = !in_quote;
+ }
+
+ *param = args;
+ if (!equals)
+ *val = NULL;
+ else {
+ args[equals] = '\0';
+ *val = args + equals + 1;
+ }
+
+ if (args[i]) {
+ args[i] = '\0';
+ return args + i + 1;
+ } else
+ return args + i;
+}
+
+/* Args looks like "foo=bar,bar2 baz=fuz wiz". */
+int parse_args(const char *name,
+ char *args,
+ struct kernel_param *params,
+ unsigned num,
+ int (*unknown)(char *param, char *val))
+{
+ char *param, *val;
+
+ DEBUGP("Parsing ARGS: %s\n", args);
+
+ while (*args) {
+ int ret;
+
+ args = next_arg(args, &param, &val);
+ ret = parse_one(param, val, params, num, unknown);
+ switch (ret) {
+ case -ENOENT:
+ printk(KERN_ERR "%s: Unknown parameter `%s'\n",
+ name, param);
+ return ret;
+ case -ENOSPC:
+ printk(KERN_ERR
+ "%s: `%s' too large for parameter `%s'\n",
+ name, val ?: "", param);
+ return ret;
+ case 0:
+ break;
+ default:
+ printk(KERN_ERR
+ "%s: `%s' invalid for parameter `%s'\n",
+ name, val ?: "", param);
+ return ret;
+ }
+ }
+
+ /* All parsed OK. */
+ return 0;
+}
+
+/* Lazy bastard, eh? */
+#define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
+ int param_set_##name(const char *val, struct kernel_param *kp) \
+ { \
+ char *endp; \
+ tmptype l; \
+ \
+ if (!val) return -EINVAL; \
+ l = strtolfn(val, &endp, 0); \
+ if (endp == val || *endp || ((type)l != l)) \
+ return -EINVAL; \
+ *((type *)kp->arg) = l; \
+ return 0; \
+ } \
+ int param_get_##name(char *buffer, struct kernel_param *kp) \
+ { \
+ return sprintf(buffer, format, *((type *)kp->arg)); \
+ }
+
+STANDARD_PARAM_DEF(short, short, "%hi", long, simple_strtol);
+STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", long, simple_strtol);
+STANDARD_PARAM_DEF(int, int, "%i", long, simple_strtol);
+STANDARD_PARAM_DEF(uint, unsigned int, "%u", long, simple_strtol);
+STANDARD_PARAM_DEF(long, long, "%li", long, simple_strtol);
+STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, simple_strtoul);
+
+int param_set_charp(const char *val, struct kernel_param *kp)
+{
+ if (!val) {
+ printk(KERN_ERR "%s: string parameter expected\n",
+ kp->name);
+ return -EINVAL;
+ }
+
+ if (strlen(val) > 1024) {
+ printk(KERN_ERR "%s: string parameter too long\n",
+ kp->name);
+ return -ENOSPC;
+ }
+
+ *(char **)kp->arg = (char *)val;
+ return 0;
+}
+
+int param_get_charp(char *buffer, struct kernel_param *kp)
+{
+ return sprintf(buffer, "%s", *((char **)kp->arg));
+}
+
+int param_set_bool(const char *val, struct kernel_param *kp)
+{
+ /* No equals means "set"... */
+ if (!val) val = "1";
+
+ /* One of =[yYnN01] */
+ switch (val[0]) {
+ case 'y': case 'Y': case '1':
+ *(int *)kp->arg = 1;
+ return 0;
+ case 'n': case 'N': case '0':
+ *(int *)kp->arg = 0;
+ return 0;
+ }
+ return -EINVAL;
+}
+
+int param_get_bool(char *buffer, struct kernel_param *kp)
+{
+ /* Y and N chosen as being relatively non-coder friendly */
+ return sprintf(buffer, "%c", (*(int *)kp->arg) ? 'Y' : 'N');
+}
+
+int param_set_invbool(const char *val, struct kernel_param *kp)
+{
+ int boolval, ret;
+ struct kernel_param dummy = { .arg = &boolval };
+
+ ret = param_set_bool(val, &dummy);
+ if (ret == 0)
+ *(int *)kp->arg = !boolval;
+ return ret;
+}
+
+int param_get_invbool(char *buffer, struct kernel_param *kp)
+{
+ int val;
+ struct kernel_param dummy = { .arg = &val };
+
+ val = !*(int *)kp->arg;
+ return param_get_bool(buffer, &dummy);
+}
+
+/* We cheat here and temporarily mangle the string. */
+int param_array(const char *name,
+ const char *val,
+ unsigned int min, unsigned int max,
+ void *elem, int elemsize,
+ int (*set)(const char *, struct kernel_param *kp))
+{
+ int ret;
+ unsigned int count = 0;
+ struct kernel_param kp;
+
+ /* Get the name right for errors. */
+ kp.name = name;
+ kp.arg = elem;
+
+ /* No equals sign? */
+ if (!val) {
+ printk(KERN_ERR "%s: expects arguments\n", name);
+ return -EINVAL;
+ }
+
+ /* We expect a comma-separated list of values. */
+ do {
+ int len;
+ char save;
+
+ if (count > max) {
+ printk(KERN_ERR "%s: can only take %i arguments\n",
+ name, max);
+ return -EINVAL;
+ }
+ len = strcspn(val, ",");
+
+ /* Temporarily nul-terminate and parse */
+ save = val[len];
+ ((char *)val)[len] = '\0';
+ ret = set(val, &kp);
+ ((char *)val)[len] = save;
+
+ if (ret != 0)
+ return ret;
+ kp.arg += elemsize;
+ val += len+1;
+ count++;
+ } while (val[-1] == ',');
+
+ if (count < min) {
+ printk(KERN_ERR "%s: needs at least %i arguments\n",
+ name, min);
+ return -EINVAL;
+ }
+ return 0;
+}
+
+/* First two elements are the max and min array length (which don't change) */
+int param_set_intarray(const char *val, struct kernel_param *kp)
+{
+ int *array;
+
+ /* Grab min and max as first two elements */
+ array = kp->arg;
+ return param_array(kp->name, val, array[0], array[1], &array[2],
+ sizeof(int), param_set_int);
+}
+
+int param_get_intarray(char *buffer, struct kernel_param *kp)
+{
+ int max;
+ int *array;
+ unsigned int i;
+
+ array = kp->arg;
+ max = array[1];
+
+ for (i = 2; i < max + 2; i++)
+ sprintf(buffer, "%s%i", i > 2 ? "," : "", array[i]);
+ return strlen(buffer);
+}
+
+int param_set_copystring(const char *val, struct kernel_param *kp)
+{
+ struct kparam_string *kps = kp->arg;
+
+ if (strlen(val)+1 > kps->maxlen) {
+ printk(KERN_ERR "%s: string doesn't fit in %u chars.\n",
+ kp->name, kps->maxlen-1);
+ return -ENOSPC;
+ }
+ strcpy(kps->string, val);
+ return 0;
+}
+
+EXPORT_SYMBOL(param_set_short);
+EXPORT_SYMBOL(param_get_short);
+EXPORT_SYMBOL(param_set_ushort);
+EXPORT_SYMBOL(param_get_ushort);
+EXPORT_SYMBOL(param_set_int);
+EXPORT_SYMBOL(param_get_int);
+EXPORT_SYMBOL(param_set_uint);
+EXPORT_SYMBOL(param_get_uint);
+EXPORT_SYMBOL(param_set_long);
+EXPORT_SYMBOL(param_get_long);
+EXPORT_SYMBOL(param_set_ulong);
+EXPORT_SYMBOL(param_get_ulong);
+EXPORT_SYMBOL(param_set_charp);
+EXPORT_SYMBOL(param_get_charp);
+EXPORT_SYMBOL(param_set_bool);
+EXPORT_SYMBOL(param_get_bool);
+EXPORT_SYMBOL(param_set_invbool);
+EXPORT_SYMBOL(param_get_invbool);
+EXPORT_SYMBOL(param_set_intarray);
+EXPORT_SYMBOL(param_get_intarray);
+EXPORT_SYMBOL(param_set_copystring);
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .19111-linux-2.5.49/lib/cmdline.c .19111-linux-2.5.49.updated/lib/cmdline.c
--- .19111-linux-2.5.49/lib/cmdline.c 2001-04-13 05:25:53.000000000 +1000
+++ .19111-linux-2.5.49.updated/lib/cmdline.c 2002-11-25 13:29:05.000000000 +1100
@@ -64,12 +64,12 @@ int get_option (char **str, int *pint)
* completely parseable).
*/

-char *get_options (char *str, int nints, int *ints)
+char *get_options (const char *str, int nints, int *ints)
{
int res, i = 1;

while (i < nints) {
- res = get_option (&str, ints + i);
+ res = get_option ((char **)&str, ints + i);
if (res == 0)
break;
i++;
@@ -77,7 +77,7 @@ char *get_options (char *str, int nints,
break;
}
ints[0] = i - 1;
- return (str);
+ return (char *)str;
}

/**


2002-11-26 07:23:11

by Rusty Russell

[permalink] [raw]
Subject: [PATCH] module_param() 2/3

This enables it in the module code.

Name: Parameter Implementation for modules
Author: Rusty Russell
Status: Tested on 2.5.49
Depends: Module/param.patch.gz

D: This activates parameter parsing for module_param() declarations in modules.

diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .31807-linux-2.5.43/kernel/module.c .31807-linux-2.5.43.updated/kernel/module.c
--- .31807-linux-2.5.43/kernel/module.c 2002-10-18 17:17:25.000000000 +1000
+++ .31807-linux-2.5.43.updated/kernel/module.c 2002-10-18 17:17:57.000000000 +1000
@@ -25,6 +25,7 @@
#include <linux/fcntl.h>
#include <linux/rcupdate.h>
#include <linux/cpu.h>
+#include <linux/moduleparam.h>
#include <asm/uaccess.h>
#include <asm/semaphore.h>
#include <asm/pgalloc.h>
@@ -951,8 +952,7 @@ static struct module *load_module(void *
if (err < 0)
goto cleanup;

-#if 0 /* Needs param support */
- /* Size of section 0 is 0, so this works well */
+ /* Size of section 0 is 0, so this works well if no params */
err = parse_args(mod->args,
(struct kernel_param *)
sechdrs[setupindex].sh_offset,
@@ -961,7 +961,6 @@ static struct module *load_module(void *
NULL);
if (err < 0)
goto cleanup;
-#endif

/* Get rid of temporary copy */
vfree(hdr);

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

2002-11-26 07:23:19

by Rusty Russell

[permalink] [raw]
Subject: [PATCH] module_param() 3/3

And this is the backwards compat wedge for MODULE_PARM(), so they
"just work".

Name: MODULE_PARM support for older modules
Author: Rusty Russell
Status: Tested on 2.5.49
Depends: Module/param-modules.patch.gz
Depends: Module/forceunload.patch.gz

D: This is the backwards compatibility code for MODULE_PARM, and moves
D: __MODULE_STRING() down to the graveyard at the bottom of module.h.

diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .20113-linux-2.5.49/include/linux/module.h .20113-linux-2.5.49.updated/include/linux/module.h
--- .20113-linux-2.5.49/include/linux/module.h 2002-11-25 08:44:18.000000000 +1100
+++ .20113-linux-2.5.49.updated/include/linux/module.h 2002-11-26 12:00:02.000000000 +1100
@@ -13,16 +13,13 @@
#include <linux/stat.h>
#include <linux/compiler.h>
#include <linux/cache.h>
+#include <linux/stringify.h>
#include <linux/kmod.h>
#include <linux/elf.h>

#include <asm/module.h>
#include <asm/uaccess.h> /* For struct exception_table_entry */

-/* Indirect stringification */
-#define __MODULE_STRING_1(x) #x
-#define __MODULE_STRING(x) __MODULE_STRING_1(x)
-
/* Not Yet Implemented */
#define MODULE_LICENSE(name)
#define MODULE_AUTHOR(name)
@@ -277,6 +274,17 @@ extern spinlock_t modlist_lock;
#define __MOD_DEC_USE_COUNT(mod) module_put(mod)
#define SET_MODULE_OWNER(dev) ((dev)->owner = THIS_MODULE)

+struct obsolete_modparm { char name[64]; char type[64]; void *addr; };
+#ifdef MODULE
+/* DEPRECATED: Do not use. */
+#define MODULE_PARM(var,type) \
+struct obsolete_modparm __parm_##var __attribute__((section(".obsparm"))) = \
+{ __stringify(var), type };
+
+#else
+#define MODULE_PARM(var,type)
+#endif
+
/* People do this inside their init routines, when the module isn't
"live" yet. They should no longer be doing that, but
meanwhile... */
@@ -289,11 +297,11 @@ extern spinlock_t modlist_lock;
#endif
#define MOD_DEC_USE_COUNT module_put(THIS_MODULE)
#define try_inc_mod_count(mod) try_module_get(mod)
-#define MODULE_PARM(parm,string)
#define EXPORT_NO_SYMBOLS
extern int module_dummy_usage;
#define GET_USE_COUNT(module) (module_dummy_usage)
#define MOD_IN_USE 0
+#define __MODULE_STRING(x) __stringify(x)
#define __mod_between(a_start, a_len, b_start, b_len) \
(((a_start) >= (b_start) && (a_start) <= (b_start)+(b_len)) \
|| ((a_start)+(a_len) >= (b_start) \
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .20113-linux-2.5.49/init/Kconfig .20113-linux-2.5.49.updated/init/Kconfig
--- .20113-linux-2.5.49/init/Kconfig 2002-11-19 09:58:52.000000000 +1100
+++ .20113-linux-2.5.49.updated/init/Kconfig 2002-11-26 11:58:56.000000000 +1100
@@ -135,6 +135,15 @@ config MODULE_FORCE_UNLOAD
rmmod). This is mainly for kernel developers and desparate users.
If unsure, say N.

+config OBSOLETE_MODPARM
+ bool
+ default y
+ depends on MODULES
+ help
+ You need this option to use module parameters on modules which
+ have not been converted to the new module parameter system yet.
+ If unsure, say Y.
+
config KMOD
bool "Kernel module loader"
depends on MODULES
@@ -150,4 +159,3 @@ config KMOD
in <file:Documentation/kmod.txt>.

endmenu
-
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .20113-linux-2.5.49/kernel/module.c .20113-linux-2.5.49.updated/kernel/module.c
--- .20113-linux-2.5.49/kernel/module.c 2002-11-26 11:58:30.000000000 +1100
+++ .20113-linux-2.5.49.updated/kernel/module.c 2002-11-26 11:58:56.000000000 +1100
@@ -520,6 +520,129 @@ sys_delete_module(const char *name_user,

#endif /* CONFIG_MODULE_UNLOAD */

+#ifdef CONFIG_OBSOLETE_MODPARM
+static int param_set_byte(const char *val, struct kernel_param *kp)
+{
+ char *endp;
+ long l;
+
+ if (!val) return -EINVAL;
+ l = simple_strtol(val, &endp, 0);
+ if (endp == val || *endp || ((char)l != l))
+ return -EINVAL;
+ *((char *)kp->arg) = l;
+ return 0;
+}
+
+static int param_string(const char *name, const char *val,
+ unsigned int min, unsigned int max,
+ char *dest)
+{
+ if (strlen(val) < min || strlen(val) > max) {
+ printk(KERN_ERR
+ "Parameter %s length must be %u-%u characters\n",
+ name, min, max);
+ return -EINVAL;
+ }
+ strcpy(dest, val);
+ return 0;
+}
+
+extern int set_obsolete(const char *val, struct kernel_param *kp)
+{
+ unsigned int min, max;
+ char *p, *endp;
+ struct obsolete_modparm *obsparm = kp->arg;
+
+ if (!val) {
+ printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
+ return -EINVAL;
+ }
+
+ /* type is: [min[-max]]{b,h,i,l,s} */
+ p = obsparm->type;
+ min = simple_strtol(p, &endp, 10);
+ if (endp == obsparm->type)
+ min = max = 1;
+ else if (*endp == '-') {
+ p = endp+1;
+ max = simple_strtol(p, &endp, 10);
+ } else
+ max = min;
+ switch (*endp) {
+ case 'b':
+ return param_array(kp->name, val, min, max, obsparm->addr,
+ 1, param_set_byte);
+ case 'h':
+ return param_array(kp->name, val, min, max, obsparm->addr,
+ sizeof(short), param_set_short);
+ case 'i':
+ return param_array(kp->name, val, min, max, obsparm->addr,
+ sizeof(int), param_set_int);
+ case 'l':
+ return param_array(kp->name, val, min, max, obsparm->addr,
+ sizeof(long), param_set_long);
+ case 's':
+ return param_string(kp->name, val, min, max, obsparm->addr);
+ }
+ printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
+ return -EINVAL;
+}
+
+static int obsolete_params(const char *name,
+ char *args,
+ struct obsolete_modparm obsparm[],
+ unsigned int num,
+ Elf_Shdr *sechdrs,
+ unsigned int symindex,
+ const char *strtab)
+{
+ struct kernel_param *kp;
+ unsigned int i;
+ int ret;
+
+ kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
+ if (!kp)
+ return -ENOMEM;
+
+ for (i = 0; i < num; i++) {
+ kp[i].name = obsparm[i].name;
+ kp[i].perm = 000;
+ kp[i].set = set_obsolete;
+ kp[i].get = NULL;
+ obsparm[i].addr
+ = (void *)find_local_symbol(sechdrs, symindex, strtab,
+ obsparm[i].name);
+ if (!obsparm[i].addr) {
+ printk("%s: falsely claims to have parameter %s\n",
+ name, obsparm[i].name);
+ ret = -EINVAL;
+ goto out;
+ }
+ kp[i].arg = &obsparm[i];
+ }
+
+ ret = parse_args(name, args, kp, num, NULL);
+ out:
+ kfree(kp);
+ return ret;
+}
+#else
+static int obsolete_params(const char *name,
+ char *args,
+ struct obsolete_modparm obsparm[],
+ unsigned int num,
+ Elf_Shdr *sechdrs,
+ unsigned int symindex,
+ const char *strtab)
+{
+ if (num != 0)
+ printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
+ name);
+ return 0;
+}
+#endif /* CONFIG_OBSOLETE_MODPARM */
+
/* Find an symbol for this module (ie. resolve internals first).
It we find one, record usage. Must be holding module_mutex. */
unsigned long find_symbol_internal(Elf_Shdr *sechdrs,
@@ -816,7 +939,7 @@ static struct module *load_module(void *
Elf_Shdr *sechdrs;
char *secstrings;
unsigned int i, symindex, exportindex, strindex, setupindex, exindex,
- modnameindex;
+ modnameindex, obsparmindex;
long arglen;
unsigned long common_length;
struct sizes sizes, used;
@@ -854,7 +977,7 @@ static struct module *load_module(void *

/* May not export symbols, or have setup params, so these may
not exist */
- exportindex = setupindex = 0;
+ exportindex = setupindex = obsparmindex = 0;

/* And these should exist, but gcc whinges if we don't init them */
symindex = strindex = exindex = modnameindex = 0;
@@ -890,6 +1013,11 @@ static struct module *load_module(void *
/* Exception table */
DEBUGP("Exception table found in section %u\n", i);
exindex = i;
+ } else if (strcmp(secstrings+sechdrs[i].sh_name, ".obsparm")
+ == 0) {
+ /* Obsolete MODULE_PARM() table */
+ DEBUGP("Obsolete param found in section %u\n", i);
+ obsparmindex = i;
}
#ifdef CONFIG_KALLSYMS
/* symbol and string tables for decoding later. */
@@ -1032,13 +1160,23 @@ static struct module *load_module(void *
if (err < 0)
goto cleanup;

- /* Size of section 0 is 0, so this works well if no params */
- err = parse_args(mod->args,
- (struct kernel_param *)
- sechdrs[setupindex].sh_offset,
- sechdrs[setupindex].sh_size
- / sizeof(struct kernel_param),
- NULL);
+ if (obsparmindex) {
+ err = obsolete_params(mod->name, mod->args,
+ (struct obsolete_modparm *)
+ sechdrs[obsparmindex].sh_offset,
+ sechdrs[obsparmindex].sh_size
+ / sizeof(struct obsolete_modparm),
+ sechdrs, symindex,
+ (char *)sechdrs[strindex].sh_offset);
+ } else {
+ /* Size of section 0 is 0, so this works well if no params */
+ err = parse_args(mod->name, mod->args,
+ (struct kernel_param *)
+ sechdrs[setupindex].sh_offset,
+ sechdrs[setupindex].sh_size
+ / sizeof(struct kernel_param),
+ NULL);
+ }
if (err < 0)
goto cleanup;


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

2002-11-27 04:50:32

by Thomas Molina

[permalink] [raw]
Subject: Re: [PATCH] module_param() 1/3

On Tue, 26 Nov 2002, Rusty Russell wrote:

> Linus,
> This is the core support, with the out-by-one error which made
> it not boot with 0 params fixed (Doh!). Tested on 2.5.49 (with a test
> parameter and without).

System is RedHat 8.0 with mod-init tools 0.7 added. I pulled down a fresh
2.5.49 tarball and added all three of your patches.

modprobe pcmcia_core gives the following error:

pcmcia_core: falsely claims to have parameter t
FATAL: Error inserting /lib/modules/2.5.49/kernel/pcmcia_core.o: Invalid argument


2002-11-28 08:09:47

by Rusty Russell

[permalink] [raw]
Subject: Re: [PATCH] module_param() 1/3

In message <[email protected]> you write:
> System is RedHat 8.0 with mod-init tools 0.7 added. I pulled down a fresh
> 2.5.49 tarball and added all three of your patches.
>
> modprobe pcmcia_core gives the following error:

Fuck! A user! 8)

Thanks, reproduced here. 3/3 patch fixed (and attached below).

Padding had been inserted in between the obsolete_modparm entries in
the section, because I wasn't careful enough about the size.
pcmcia_core has enough parameters to show up this bug clearly.

Thanks very much for the bug report! Does this work for you now?
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

Name: MODULE_PARM support for older modules
Author: Rusty Russell
Status: Tested on 2.5.50
Depends: Module/param-modules.patch.gz

D: This is the backwards compatibility code for MODULE_PARM, and moves
D: __MODULE_STRING() down to the graveyard at the bottom of module.h.

diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .984-2.5.50-param-oldstyle.pre/include/linux/module.h .984-2.5.50-param-oldstyle/include/linux/module.h
--- .984-2.5.50-param-oldstyle.pre/include/linux/module.h 2002-11-25 08:44:18.000000000 +1100
+++ .984-2.5.50-param-oldstyle/include/linux/module.h 2002-11-28 18:55:09.000000000 +1100
@@ -13,16 +13,13 @@
#include <linux/stat.h>
#include <linux/compiler.h>
#include <linux/cache.h>
+#include <linux/stringify.h>
#include <linux/kmod.h>
#include <linux/elf.h>

#include <asm/module.h>
#include <asm/uaccess.h> /* For struct exception_table_entry */

-/* Indirect stringification */
-#define __MODULE_STRING_1(x) #x
-#define __MODULE_STRING(x) __MODULE_STRING_1(x)
-
/* Not Yet Implemented */
#define MODULE_LICENSE(name)
#define MODULE_AUTHOR(name)
@@ -277,6 +274,21 @@ extern spinlock_t modlist_lock;
#define __MOD_DEC_USE_COUNT(mod) module_put(mod)
#define SET_MODULE_OWNER(dev) ((dev)->owner = THIS_MODULE)

+struct obsolete_modparm {
+ char name[64];
+ char type[64-sizeof(void *)];
+ void *addr;
+};
+#ifdef MODULE
+/* DEPRECATED: Do not use. */
+#define MODULE_PARM(var,type) \
+struct obsolete_modparm __parm_##var __attribute__((section(".obsparm"))) = \
+{ __stringify(var), type };
+
+#else
+#define MODULE_PARM(var,type)
+#endif
+
/* People do this inside their init routines, when the module isn't
"live" yet. They should no longer be doing that, but
meanwhile... */
@@ -289,11 +301,11 @@ extern spinlock_t modlist_lock;
#endif
#define MOD_DEC_USE_COUNT module_put(THIS_MODULE)
#define try_inc_mod_count(mod) try_module_get(mod)
-#define MODULE_PARM(parm,string)
#define EXPORT_NO_SYMBOLS
extern int module_dummy_usage;
#define GET_USE_COUNT(module) (module_dummy_usage)
#define MOD_IN_USE 0
+#define __MODULE_STRING(x) __stringify(x)
#define __mod_between(a_start, a_len, b_start, b_len) \
(((a_start) >= (b_start) && (a_start) <= (b_start)+(b_len)) \
|| ((a_start)+(a_len) >= (b_start) \
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .984-2.5.50-param-oldstyle.pre/init/Kconfig .984-2.5.50-param-oldstyle/init/Kconfig
--- .984-2.5.50-param-oldstyle.pre/init/Kconfig 2002-11-19 09:58:52.000000000 +1100
+++ .984-2.5.50-param-oldstyle/init/Kconfig 2002-11-28 18:55:09.000000000 +1100
@@ -135,6 +135,15 @@ config MODULE_FORCE_UNLOAD
rmmod). This is mainly for kernel developers and desparate users.
If unsure, say N.

+config OBSOLETE_MODPARM
+ bool
+ default y
+ depends on MODULES
+ help
+ You need this option to use module parameters on modules which
+ have not been converted to the new module parameter system yet.
+ If unsure, say Y.
+
config KMOD
bool "Kernel module loader"
depends on MODULES
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal .984-2.5.50-param-oldstyle.pre/kernel/module.c .984-2.5.50-param-oldstyle/kernel/module.c
--- .984-2.5.50-param-oldstyle.pre/kernel/module.c 2002-11-28 18:55:08.000000000 +1100
+++ .984-2.5.50-param-oldstyle/kernel/module.c 2002-11-28 18:55:09.000000000 +1100
@@ -520,6 +520,134 @@ sys_delete_module(const char *name_user,

#endif /* CONFIG_MODULE_UNLOAD */

+#ifdef CONFIG_OBSOLETE_MODPARM
+static int param_set_byte(const char *val, struct kernel_param *kp)
+{
+ char *endp;
+ long l;
+
+ if (!val) return -EINVAL;
+ l = simple_strtol(val, &endp, 0);
+ if (endp == val || *endp || ((char)l != l))
+ return -EINVAL;
+ *((char *)kp->arg) = l;
+ return 0;
+}
+
+static int param_string(const char *name, const char *val,
+ unsigned int min, unsigned int max,
+ char *dest)
+{
+ if (strlen(val) < min || strlen(val) > max) {
+ printk(KERN_ERR
+ "Parameter %s length must be %u-%u characters\n",
+ name, min, max);
+ return -EINVAL;
+ }
+ strcpy(dest, val);
+ return 0;
+}
+
+extern int set_obsolete(const char *val, struct kernel_param *kp)
+{
+ unsigned int min, max;
+ char *p, *endp;
+ struct obsolete_modparm *obsparm = kp->arg;
+
+ if (!val) {
+ printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
+ return -EINVAL;
+ }
+
+ /* type is: [min[-max]]{b,h,i,l,s} */
+ p = obsparm->type;
+ min = simple_strtol(p, &endp, 10);
+ if (endp == obsparm->type)
+ min = max = 1;
+ else if (*endp == '-') {
+ p = endp+1;
+ max = simple_strtol(p, &endp, 10);
+ } else
+ max = min;
+ switch (*endp) {
+ case 'b':
+ return param_array(kp->name, val, min, max, obsparm->addr,
+ 1, param_set_byte);
+ case 'h':
+ return param_array(kp->name, val, min, max, obsparm->addr,
+ sizeof(short), param_set_short);
+ case 'i':
+ return param_array(kp->name, val, min, max, obsparm->addr,
+ sizeof(int), param_set_int);
+ case 'l':
+ return param_array(kp->name, val, min, max, obsparm->addr,
+ sizeof(long), param_set_long);
+ case 's':
+ return param_string(kp->name, val, min, max, obsparm->addr);
+ }
+ printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
+ return -EINVAL;
+}
+
+static int obsolete_params(const char *name,
+ char *args,
+ struct obsolete_modparm obsparm[],
+ unsigned int num,
+ Elf_Shdr *sechdrs,
+ unsigned int symindex,
+ const char *strtab)
+{
+ struct kernel_param *kp;
+ unsigned int i;
+ int ret;
+
+ kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
+ if (!kp)
+ return -ENOMEM;
+
+ DEBUGP("Module %s has %u obsolete params\n", name, num);
+ for (i = 0; i < num; i++)
+ DEBUGP("Param %i: %s type %s\n",
+ num, obsparm[i].name, obsparm[i].type);
+
+ for (i = 0; i < num; i++) {
+ kp[i].name = obsparm[i].name;
+ kp[i].perm = 000;
+ kp[i].set = set_obsolete;
+ kp[i].get = NULL;
+ obsparm[i].addr
+ = (void *)find_local_symbol(sechdrs, symindex, strtab,
+ obsparm[i].name);
+ if (!obsparm[i].addr) {
+ printk("%s: falsely claims to have parameter %s\n",
+ name, obsparm[i].name);
+ ret = -EINVAL;
+ goto out;
+ }
+ kp[i].arg = &obsparm[i];
+ }
+
+ ret = parse_args(name, args, kp, num, NULL);
+ out:
+ kfree(kp);
+ return ret;
+}
+#else
+static int obsolete_params(const char *name,
+ char *args,
+ struct obsolete_modparm obsparm[],
+ unsigned int num,
+ Elf_Shdr *sechdrs,
+ unsigned int symindex,
+ const char *strtab)
+{
+ if (num != 0)
+ printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
+ name);
+ return 0;
+}
+#endif /* CONFIG_OBSOLETE_MODPARM */
+
/* Find an symbol for this module (ie. resolve internals first).
It we find one, record usage. Must be holding module_mutex. */
unsigned long find_symbol_internal(Elf_Shdr *sechdrs,
@@ -816,7 +944,7 @@ static struct module *load_module(void *
Elf_Shdr *sechdrs;
char *secstrings;
unsigned int i, symindex, exportindex, strindex, setupindex, exindex,
- modnameindex;
+ modnameindex, obsparmindex;
long arglen;
unsigned long common_length;
struct sizes sizes, used;
@@ -854,7 +982,7 @@ static struct module *load_module(void *

/* May not export symbols, or have setup params, so these may
not exist */
- exportindex = setupindex = 0;
+ exportindex = setupindex = obsparmindex = 0;

/* And these should exist, but gcc whinges if we don't init them */
symindex = strindex = exindex = modnameindex = 0;
@@ -890,6 +1018,11 @@ static struct module *load_module(void *
/* Exception table */
DEBUGP("Exception table found in section %u\n", i);
exindex = i;
+ } else if (strcmp(secstrings+sechdrs[i].sh_name, ".obsparm")
+ == 0) {
+ /* Obsolete MODULE_PARM() table */
+ DEBUGP("Obsolete param found in section %u\n", i);
+ obsparmindex = i;
}
#ifdef CONFIG_KALLSYMS
/* symbol and string tables for decoding later. */
@@ -1032,13 +1165,23 @@ static struct module *load_module(void *
if (err < 0)
goto cleanup;

- /* Size of section 0 is 0, so this works well if no params */
- err = parse_args(mod->args,
- (struct kernel_param *)
- sechdrs[setupindex].sh_offset,
- sechdrs[setupindex].sh_size
- / sizeof(struct kernel_param),
- NULL);
+ if (obsparmindex) {
+ err = obsolete_params(mod->name, mod->args,
+ (struct obsolete_modparm *)
+ sechdrs[obsparmindex].sh_offset,
+ sechdrs[obsparmindex].sh_size
+ / sizeof(struct obsolete_modparm),
+ sechdrs, symindex,
+ (char *)sechdrs[strindex].sh_offset);
+ } else {
+ /* Size of section 0 is 0, so this works well if no params */
+ err = parse_args(mod->name, mod->args,
+ (struct kernel_param *)
+ sechdrs[setupindex].sh_offset,
+ sechdrs[setupindex].sh_size
+ / sizeof(struct kernel_param),
+ NULL);
+ }
if (err < 0)
goto cleanup;