Commit 049fb9bd416077b3622d317a45796be4f2431df3

Authored by Steven Rostedt (Red Hat)
Committed by Steven Rostedt
1 parent f36d1be293

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().

Link: http://lkml.kernel.org/r/567CEA31.1070507@intel.com

Reported-by: "Qiu, PeiyangX" <peiyangx.qiu@intel.com>
Fixes: a949ae560a511 "ftrace/module: Hardcode ftrace_module_init() call into load_module()"
Cc: stable@vger.kernel.org # v2.6.38+
Acked-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>

Showing 2 changed files with 7 additions and 0 deletions Side-by-side Diff

include/linux/ftrace.h
... ... @@ -586,6 +586,7 @@
586 586  
587 587 extern int skip_trace(unsigned long ip);
588 588 extern void ftrace_module_init(struct module *mod);
  589 +extern void ftrace_release_mod(struct module *mod);
589 590  
590 591 extern void ftrace_disable_daemon(void);
591 592 extern void ftrace_enable_daemon(void);
... ... @@ -3571,6 +3571,12 @@
3571 3571 synchronize_sched();
3572 3572 mutex_unlock(&module_mutex);
3573 3573 free_module:
  3574 + /*
  3575 + * Ftrace needs to clean up what it initialized.
  3576 + * This does nothing if ftrace_module_init() wasn't called,
  3577 + * but it must be called outside of module_mutex.
  3578 + */
  3579 + ftrace_release_mod(mod);
3574 3580 /* Free lock-classes; relies on the preceding sync_rcu() */
3575 3581 lockdep_free_key_range(mod->module_core, mod->core_size);
3576 3582