Skip to content

Commit e6df34a

Browse files
committed
param: add a free hook to kernel_param_ops.
This allows us to generalize the KPARAM_KMALLOCED flag, by calling a function on every parameter when a module is unloaded. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Reviewed-by: Takashi Iwai <tiwai@suse.de> Tested-by: Phil Carmody <ext-phil.2.carmody@nokia.com>
1 parent 6a84152 commit e6df34a

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎include/linux/moduleparam.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ struct kernel_param_ops {
3636
int (*set)(const char *val, const struct kernel_param *kp);
3737
/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
3838
int (*get)(char *buffer, const struct kernel_param *kp);
39+
/* Optional function to free kp->arg when module unloaded. */
40+
void (*free)(void *arg);
3941
};
4042

4143
/* Flag bits for kernel_param.flags */

‎kernel/params.c‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,20 @@ static int param_array_get(char *buffer, const struct kernel_param *kp)
399399
return off;
400400
}
401401

402+
static void param_array_free(void *arg)
403+
{
404+
unsigned int i;
405+
const struct kparam_array *arr = arg;
406+
407+
if (arr->ops->free)
408+
for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
409+
arr->ops->free(arr->elem + arr->elemsize * i);
410+
}
411+
402412
struct kernel_param_ops param_array_ops = {
403413
.set = param_array_set,
404414
.get = param_array_get,
415+
.free = param_array_free,
405416
};
406417
EXPORT_SYMBOL(param_array_ops);
407418

@@ -634,7 +645,11 @@ void module_param_sysfs_remove(struct module *mod)
634645

635646
void destroy_params(const struct kernel_param *params, unsigned num)
636647
{
637-
/* FIXME: This should free kmalloced charp parameters. It doesn't. */
648+
unsigned int i;
649+
650+
for (i = 0; i < num; i++)
651+
if (params[i].ops->free)
652+
params[i].ops->free(params[i].arg);
638653
}
639654

640655
static void __init kernel_add_sysfs_param(const char *name,

0 commit comments

Comments
 (0)