ftrace/module: Call clean up function when module init fails early
Steven Rostedt
maintainerPATCH 1/1 ftrace/module: Call clean up function when module init fails early
[ Rusty, I'm currently testing this patch. Can you give me an Acked-by? ]
If the module init code fails after calling ftrace_module_init() and before calling do_init_module(), we can suffer from a memory leak. This is because ftrace_module_init() allocates pages to store the locations that ftrace hooks are placed in the module text. If do_init_module() fails, it still calls the MODULE_GOING notifiers which will tell ftrace to do a clean up of the pages it allocated for the module. But if load_module() fails before then, the pages allocated by ftrace_module_init() will never be freed.
Call ftrace_release_mod() on the module if load_module() fails before getting to do_init_module().
--- kernel/module.c | 6 ++++++ 1 file changed, 6 insertions(+)
kernel/module.c +6 -0
@@ -3571,6 +3571,12 @@ static int load_module(struct load_info *info, const char __user *uargs,synchronize_sched ();mutex_unlock (&module_mutex); free_module: +/* +* Ftrace needs to clean up what it initialized. +* This does nothing if ftrace_module_init() wasn't called, +* but it must be called outside of module_mutex. +*/ +ftrace_release_mod (mod);/* Free lock-classes; relies on the preceding sync_rcu() */ lockdep_free_key_range (mod->module_core, mod->core_size);
Signature · 1 line
1.8.3.1
Steven Rostedt
maintainerPATCH v2 1/1 ftrace/module: Call clean up function when module init fails early
[ Bah, I forgot to commit the ftrace.h part, lucky I test on another
box than what I compile test on ;-) ]
Quoted 1 line
From
d2621b2c51a14007cd2ca5ec94fa20a486a31244Mon Sep 17 00:00:00 2001
From: "Steven Rostedt (Red Hat)" <[email protected]>
Date: Tue, 5 Jan 2016 20:32:47 -0500
Subject: [PATCH] ftrace/module: Call clean up function when module init fails
early
If the module init code fails after calling ftrace_module_init() and before calling do_init_module(), we can suffer from a memory leak. This is because ftrace_module_init() allocates pages to store the locations that ftrace hooks are placed in the module text. If do_init_module() fails, it still calls the MODULE_GOING notifiers which will tell ftrace to do a clean up of the pages it allocated for the module. But if load_module() fails before then, the pages allocated by ftrace_module_init() will never be freed.
Call ftrace_release_mod() on the module if load_module() fails before getting to do_init_module().
--- include/linux/ftrace.h | 1 + kernel/module.c | 6 ++++++ 2 files changed, 7 insertions(+)
include/linux/ftrace.h +1 -0
@@ -586,6 +586,7 @@ extern int ftrace_arch_read_dyn_info(char *buf, int size);extern int skip_trace (unsigned long ip);extern void ftrace_module_init (struct module *mod); +extern void ftrace_release_mod (struct module *mod);extern void ftrace_disable_daemon (void );extern void ftrace_enable_daemon (void );
kernel/module.c +6 -0
@@ -3571,6 +3571,12 @@ static int load_module(struct load_info *info, const char __user *uargs,synchronize_sched ();mutex_unlock (&module_mutex); free_module: +/* +* Ftrace needs to clean up what it initialized. +* This does nothing if ftrace_module_init() wasn't called, +* but it must be called outside of module_mutex. +*/ +ftrace_release_mod (mod);/* Free lock-classes; relies on the preceding sync_rcu() */ lockdep_free_key_range (mod->module_core, mod->core_size);
Signature · 1 line
1.8.3.1
Quoted 2 lines · Steven Rostedt
[ Bah, I forgot to commit the
ftrace.hpart, lucky I test on another
box than what I compile test on ;-) ]
Cheers,
Rusty.