From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756434AbcAXWJh (ORCPT ); Sun, 24 Jan 2016 17:09:37 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:32854 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754968AbcAXWFa (ORCPT ); Sun, 24 Jan 2016 17:05:30 -0500 From: Luis Henriques To: linux-kernel@vger.kernel.org, stable@vger.kernel.org, kernel-team@lists.ubuntu.com Cc: Steven Rostedt , Luis Henriques Subject: [PATCH 3.16.y-ckt 121/128] ftrace/module: Call clean up function when module init fails early Date: Sun, 24 Jan 2016 22:01:16 +0000 Message-Id: <1453672883-2708-122-git-send-email-luis.henriques@canonical.com> In-Reply-To: <1453672883-2708-1-git-send-email-luis.henriques@canonical.com> References: <1453672883-2708-1-git-send-email-luis.henriques@canonical.com> X-Extended-Stable: 3.16 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.7-ckt23 -stable review patch. If anyone has any objections, please let me know. ---8<------------------------------------------------------------ From: "Steven Rostedt (Red Hat)" commit 049fb9bd416077b3622d317a45796be4f2431df3 upstream. 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" Fixes: a949ae560a511 "ftrace/module: Hardcode ftrace_module_init() call into load_module()" Acked-by: Rusty Russell Signed-off-by: Steven Rostedt [ luis: backported to 3.16: adjusted context ] Signed-off-by: Luis Henriques --- include/linux/ftrace.h | 1 + kernel/module.c | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index 721de254ba7a..c314fe2e0f46 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -541,6 +541,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); diff --git a/kernel/module.c b/kernel/module.c index ed4d3b7ec58a..365b0a9d87b3 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -3339,6 +3339,12 @@ static int load_module(struct load_info *info, const char __user *uargs, wake_up_all(&module_wq); 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); module_deallocate(mod, info); free_copy: free_copy(info);