Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp7149378ybi; Mon, 8 Jul 2019 15:38:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqwBvTE90SMwPNoWtUYoTdqysFBZ9Bpm64XWggnGr05uE2Cqy/rj4N/9F3bpu0UYlJJWAZLp X-Received: by 2002:a63:1e0b:: with SMTP id e11mr24863975pge.402.1562625490138; Mon, 08 Jul 2019 15:38:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562625490; cv=none; d=google.com; s=arc-20160816; b=BueaZuhEFK+YigZrd6wC7sY0AE8KRmBiZqzPtv5M7jY0XVo3evJGNQzS5jEgl6YZZh sYnb8ZCh1VBJx1HKmNsxB2yM07s+SzsuS2NtCm8zSgxMqb9Ia1TN6w7/qGYVJY7BY0Lr B4CdcC1oRk0abksC1GeZCHkGNvUmDG8bYEmKlLJBnUhW0cTLzFx0svS0J9Or+8UXepXA gGAj6b/kvt/SwmBMjL1TXI4A7RsIlGqoy7d39ZLKdfdVsQ0Kwl9qdAdPB4n+xN+Le43i YV2CMYZDboqZN49QjrP3AwTr2PA2r9KK5Lg09FKYjSuQKgl80lKMubPnun+dAwE0y0Zx zRsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=7Ea5xBFpsRXa2nL19Td8y5Qj6kxQ/sQTdhCQtzMuSxg=; b=KXrfI77cmmxUnbpAJtnlJvO8OwHfwWZtar2YmzsE9hCUAORKIa6ZN4s4ESGb2D2Ep6 Kn+jNmknsprUuHgD4MGwmPbCHcmLgo0u6jH7Ze4G5bAZ56rNHgi/FskFyNZd9OX9U0rR 3KQoUe8c+7cBHNVHu9y+J22y5KE341pd0wG0QrIgZ5Hig0II0sugreHZK0BdmMfFT+P1 6FVKNKe4Bdfmqzl6WFDQ6i5YR3Sv0o5gVh+LfhFEBN4N++UtXX43a6PV+HXXwrTH4M/O D65lG7TRfx59/vuAr8jrw1uPCuh2u4ZT+m+eqbfLRgibkneZkHa/bXW4yJFQ7knWciLW EhkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=n6+JtOrQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g11si19761082pgq.592.2019.07.08.15.37.54; Mon, 08 Jul 2019 15:38:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=n6+JtOrQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388543AbfGHPZP (ORCPT + 99 others); Mon, 8 Jul 2019 11:25:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:52548 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388437AbfGHPZK (ORCPT ); Mon, 8 Jul 2019 11:25:10 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B9C9E204EC; Mon, 8 Jul 2019 15:25:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562599509; bh=3rlcjAJthrL8jMYOtcCNxy3wPnFG1Y09KFzXtGtO8pc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n6+JtOrQOQOF6J9MnG5MhAwWfHCxURK2SIbGkz4S06AuuTUl4Xk+9ZZX0pzkE1i/E nmbbr84TWWRAkQ7tlmOK41I5nddUnW1yalgp/cmb62/nMetJ6rr0WVWnROos6mzwaC mVUuAr8P0RlRvi3YTt1PgikPnZgKtjmgnVSFtK2A= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Thomas Gleixner , Miroslav Benes , Josh Poimboeuf , Petr Mladek , "Steven Rostedt (VMware)" Subject: [PATCH 4.14 38/56] ftrace/x86: Remove possible deadlock between register_kprobe() and ftrace_run_update_code() Date: Mon, 8 Jul 2019 17:13:30 +0200 Message-Id: <20190708150523.325386979@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190708150514.376317156@linuxfoundation.org> References: <20190708150514.376317156@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Petr Mladek commit d5b844a2cf507fc7642c9ae80a9d585db3065c28 upstream. The commit 9f255b632bf12c4dd7 ("module: Fix livepatch/ftrace module text permissions race") causes a possible deadlock between register_kprobe() and ftrace_run_update_code() when ftrace is using stop_machine(). The existing dependency chain (in reverse order) is: -> #1 (text_mutex){+.+.}: validate_chain.isra.21+0xb32/0xd70 __lock_acquire+0x4b8/0x928 lock_acquire+0x102/0x230 __mutex_lock+0x88/0x908 mutex_lock_nested+0x32/0x40 register_kprobe+0x254/0x658 init_kprobes+0x11a/0x168 do_one_initcall+0x70/0x318 kernel_init_freeable+0x456/0x508 kernel_init+0x22/0x150 ret_from_fork+0x30/0x34 kernel_thread_starter+0x0/0xc -> #0 (cpu_hotplug_lock.rw_sem){++++}: check_prev_add+0x90c/0xde0 validate_chain.isra.21+0xb32/0xd70 __lock_acquire+0x4b8/0x928 lock_acquire+0x102/0x230 cpus_read_lock+0x62/0xd0 stop_machine+0x2e/0x60 arch_ftrace_update_code+0x2e/0x40 ftrace_run_update_code+0x40/0xa0 ftrace_startup+0xb2/0x168 register_ftrace_function+0x64/0x88 klp_patch_object+0x1a2/0x290 klp_enable_patch+0x554/0x980 do_one_initcall+0x70/0x318 do_init_module+0x6e/0x250 load_module+0x1782/0x1990 __s390x_sys_finit_module+0xaa/0xf0 system_call+0xd8/0x2d0 Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(text_mutex); lock(cpu_hotplug_lock.rw_sem); lock(text_mutex); lock(cpu_hotplug_lock.rw_sem); It is similar problem that has been solved by the commit 2d1e38f56622b9b ("kprobes: Cure hotplug lock ordering issues"). Many locks are involved. To be on the safe side, text_mutex must become a low level lock taken after cpu_hotplug_lock.rw_sem. This can't be achieved easily with the current ftrace design. For example, arm calls set_all_modules_text_rw() already in ftrace_arch_code_modify_prepare(), see arch/arm/kernel/ftrace.c. This functions is called: + outside stop_machine() from ftrace_run_update_code() + without stop_machine() from ftrace_module_enable() Fortunately, the problematic fix is needed only on x86_64. It is the only architecture that calls set_all_modules_text_rw() in ftrace path and supports livepatching at the same time. Therefore it is enough to move text_mutex handling from the generic kernel/trace/ftrace.c into arch/x86/kernel/ftrace.c: ftrace_arch_code_modify_prepare() ftrace_arch_code_modify_post_process() This patch basically reverts the ftrace part of the problematic commit 9f255b632bf12c4dd7 ("module: Fix livepatch/ftrace module text permissions race"). And provides x86_64 specific-fix. Some refactoring of the ftrace code will be needed when livepatching is implemented for arm or nds32. These architectures call set_all_modules_text_rw() and use stop_machine() at the same time. Link: http://lkml.kernel.org/r/20190627081334.12793-1-pmladek@suse.com Fixes: 9f255b632bf12c4dd7 ("module: Fix livepatch/ftrace module text permissions race") Acked-by: Thomas Gleixner Reported-by: Miroslav Benes Reviewed-by: Miroslav Benes Reviewed-by: Josh Poimboeuf Signed-off-by: Petr Mladek [ As reviewed by Miroslav Benes , removed return value of ftrace_run_update_code() as it is a void function. ] Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/ftrace.c | 3 +++ kernel/trace/ftrace.c | 10 +--------- 2 files changed, 4 insertions(+), 9 deletions(-) --- a/arch/x86/kernel/ftrace.c +++ b/arch/x86/kernel/ftrace.c @@ -22,6 +22,7 @@ #include #include #include +#include #include @@ -36,6 +37,7 @@ int ftrace_arch_code_modify_prepare(void) { + mutex_lock(&text_mutex); set_kernel_text_rw(); set_all_modules_text_rw(); return 0; @@ -45,6 +47,7 @@ int ftrace_arch_code_modify_post_process { set_all_modules_text_ro(); set_kernel_text_ro(); + mutex_unlock(&text_mutex); return 0; } --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -34,7 +34,6 @@ #include #include #include -#include #include @@ -2693,12 +2692,10 @@ static void ftrace_run_update_code(int c { int ret; - mutex_lock(&text_mutex); - ret = ftrace_arch_code_modify_prepare(); FTRACE_WARN_ON(ret); if (ret) - goto out_unlock; + return; /* * By default we use stop_machine() to modify the code. @@ -2710,9 +2707,6 @@ static void ftrace_run_update_code(int c ret = ftrace_arch_code_modify_post_process(); FTRACE_WARN_ON(ret); - -out_unlock: - mutex_unlock(&text_mutex); } static void ftrace_run_modify_code(struct ftrace_ops *ops, int command, @@ -5800,7 +5794,6 @@ void ftrace_module_enable(struct module struct ftrace_page *pg; mutex_lock(&ftrace_lock); - mutex_lock(&text_mutex); if (ftrace_disabled) goto out_unlock; @@ -5861,7 +5854,6 @@ void ftrace_module_enable(struct module ftrace_arch_code_modify_post_process(); out_unlock: - mutex_unlock(&text_mutex); mutex_unlock(&ftrace_lock); process_cached_mods(mod->name);