tracing: A few updates to fix bugs in the latest -rc release
Subsystems (4)
Steven Rostedt
maintainercover tracing: A few updates to fix bugs in the latest -rc release
Will be sending out a pull request soon.
Eiichi Tsukata (1):
tracing/snapshot: Resize spare buffer if size changed
Jiri Kosina (1):
ftrace/x86: Anotate text_mutex split between ftrace_arch_code_modify_post_process() and ftrace_arch_code_modify_prepare()
Petr Mladek (1):
ftrace/x86: Remove possible deadlock between register_kprobe() and ftrace_run_update_code()
Steven Rostedt (VMware) (1):
ftrace/x86: Add a comment to why we take text_mutex in ftrace_arch_code_modify_prepare()
Takeshi Misawa (1):
tracing: Fix memory leak in tracing_err_log_open()
----
arch/x86/kernel/ftrace.c | 10 ++++++++++ kernel/trace/ftrace.c | 10 +--------- kernel/trace/trace.c | 24 +++++++++++++++++++----- 3 files changed, 30 insertions(+), 14 deletions(-)
Steven Rostedt
maintainerPATCH 1/5 ftrace/x86: Remove possible deadlock between register_kprobe() and ftrace_run_update_code()
From: Petr Mladek <[email protected]>
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.
[
As reviewed by Miroslav Benes <[email protected]>, removed return value of ftrace_run_update_code() as it is a void function.
]
--- arch/x86/kernel/ftrace.c | 3 +++ kernel/trace/ftrace.c | 10 +--------- 2 files changed, 4 insertions(+), 9 deletions(-)
arch/x86/kernel/ftrace.c +3 -0
@@ -22,6 +22,7 @@#include <linux/init.h> #include <linux/list.h> #include <linux/module.h> +#include <linux/memory.h> #include <trace/syscall.h>
@@ -35,6 +36,7 @@int ftrace_arch_code_modify_prepare (void ) { +mutex_lock (&text_mutex);set_kernel_text_rw ();set_all_modules_text_rw ();return 0 ;
@@ -44,6 +46,7 @@ int ftrace_arch_code_modify_post_process(void) {set_all_modules_text_ro ();set_kernel_text_ro (); +mutex_unlock (&text_mutex);return 0 ; }
kernel/trace/ftrace.c +1 -9
@@ -34,7 +34,6 @@#include <linux/hash.h> #include <linux/rcupdate.h> #include <linux/kprobes.h> -#include <linux/memory.h> #include <trace/events/sched.h>
@@ -2611,12 +2610,10 @@ static void ftrace_run_update_code(int command) {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.
@@ -2628,9 +2625,6 @@ static void ftrace_run_update_code(int command) 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,
@@ -5784,7 +5778,6 @@ void ftrace_module_enable(struct module *mod)struct ftrace_page *pg;mutex_lock (&ftrace_lock); -mutex_lock (&text_mutex);if (ftrace_disabled) goto out_unlock;
@@ -5846,7 +5839,6 @@ void ftrace_module_enable(struct module *mod)ftrace_arch_code_modify_post_process (); out_unlock: -mutex_unlock (&text_mutex);mutex_unlock (&ftrace_lock);process_cached_mods (mod->name);
Signature · 1 line
2.20.1
Steven Rostedt
maintainerPATCH 2/5 ftrace/x86: Add a comment to why we take text_mutex in ftrace_arch_code_modify_prepare()
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Taking the text_mutex in ftrace_arch_code_modify_prepare() is to fix a race against module loading and live kernel patching that might try to change the text permissions while ftrace has it as read/write. This really needs to be documented in the code. Add a comment that does such.
--- arch/x86/kernel/ftrace.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/kernel/ftrace.c +5 -0
@@ -36,6 +36,11 @@int ftrace_arch_code_modify_prepare (void ) { +/* +* Need to grab text_mutex to prevent a race from module loading +* and live kernel patching from changing the text permissions while +* ftrace has it set to "read/write". +*/ mutex_lock (&text_mutex);set_kernel_text_rw ();set_all_modules_text_rw ();
Signature · 1 line
2.20.1
Steven Rostedt
maintainerPATCH 3/5 tracing: Fix memory leak in tracing_err_log_open()
In reply to Steven RostedtFrom: Takeshi Misawa <[email protected]>
When tracing_err_log_open() calls seq_open(), allocated memory is not freed.
kmemleak report:
unreferenced object 0xffff92c0781d1100 (size 128):
comm "tail", pid 15116, jiffies 4295163855 (age 22.704s) hex dump (first 32 bytes):
00 f0 08 e5 c0 92 ff ff 00 10 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
backtrace:
[<000000000d0687d5>] kmem_cache_alloc+0x11f/0x1e0
[<000000003e3039a8>] seq_open+0x2f/0x90
[<000000008dd36b7d>] tracing_err_log_open+0x67/0x140
[<000000005a431ae2>] do_dentry_open+0x1df/0x3a0
[<00000000a2910603>] vfs_open+0x2f/0x40
[<0000000038b0a383>] path_openat+0x2e8/0x1690
[<00000000fe025bda>] do_filp_open+0x9b/0x110
[<00000000483a5091>] do_sys_open+0x1ba/0x260
[<00000000c558b5fd>] __x64_sys_openat+0x20/0x30
[<000000006881ec07>] do_syscall_64+0x5a/0x130
[<00000000571c2e94>] entry_SYSCALL_64_after_hwframe+0x44/0xa9Fix this by calling seq_release() in tracing_err_log_fops.release().
--- kernel/trace/trace.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
kernel/trace/trace.c +13 -1
@@ -7126,12 +7126,24 @@ static ssize_t tracing_err_log_write(struct file *file,return count; } +static int tracing_err_log_release (struct inode *inode,struct file *file) +{ +struct trace_array *tr = inode->i_private; + +trace_array_put (tr); + +if (file->f_mode &FMODE_READ ) +seq_release (inode, file); + +return 0 ; +} +static const struct file_operations tracing_err_log_fops = { .open = tracing_err_log_open, .write = tracing_err_log_write, .read = seq_read, .llseek = seq_lseek, - .release = tracing_release_generic_tr, + .release = tracing_err_log_release, };static int tracing_buffers_open (struct inode *inode,struct file *filp)
Signature · 1 line
2.20.1
Steven Rostedt
maintainerPATCH 4/5 tracing/snapshot: Resize spare buffer if size changed
In reply to Steven RostedtFrom: Eiichi Tsukata <[email protected]>
Current snapshot implementation swaps two ring_buffers even though their sizes are different from each other, that can cause an inconsistency between the contents of buffer_size_kb file and the current buffer size.
For example:
# cat buffer_size_kb 7 (expanded: 1408) # echo 1 > events/enable # grep bytes per_cpu/cpu0/stats bytes: 1441020 # echo 1 > snapshot // current:1408, spare:1408 # echo 123 > buffer_size_kb // current:123, spare:1408 # echo 1 > snapshot // current:1408, spare:123 # grep bytes per_cpu/cpu0/stats bytes: 1443700 # cat buffer_size_kb 123 // != current:1408
And also, a similar per-cpu case hits the following WARNING:
Reproducer:
# echo 1 > per_cpu/cpu0/snapshot # echo 123 > buffer_size_kb # echo 1 > per_cpu/cpu0/snapshot
WARNING:
WARNING: CPU: 0 PID: 1946 at kernel/trace/trace.c:1607 update_max_tr_single.part.0+0x2b8/0x380 Modules linked in: CPU: 0 PID: 1946 Comm: bash Not tainted 5.2.0-rc6 #20 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-2.fc30 04/01/2014 RIP: 0010:update_max_tr_single.part.0+0x2b8/0x380 Code: ff e8 dc da f9 ff 0f 0b e9 88 fe ff ff e8 d0 da f9 ff 44 89 ee bf f5 ff ff ff e8 33 dc f9 ff 41 83 fd f5 74 96 e8 b8 da f9 ff <0f> 0b eb 8d e8 af da f9 ff 0f 0b e9 bf fd ff ff e8 a3 da f9 ff 48 RSP: 0018:ffff888063e4fca0 EFLAGS: 00010093 RAX: ffff888066214380 RBX: ffffffff99850fe0 RCX: ffffffff964298a8 RDX: 0000000000000000 RSI: 00000000fffffff5 RDI: 0000000000000005 RBP: 1ffff1100c7c9f96 R08: ffff888066214380 R09: ffffed100c7c9f9b R10: ffffed100c7c9f9a R11: 0000000000000003 R12: 0000000000000000 R13: 00000000ffffffea R14: ffff888066214380 R15: ffffffff99851060 FS: 00007f9f8173c700(0000) GS:ffff88806d000000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000714dc0 CR3: 0000000066fa6000 CR4: 00000000000006f0 Call Trace: ? trace_array_printk_buf+0x140/0x140 ? __mutex_lock_slowpath+0x10/0x10 tracing_snapshot_write+0x4c8/0x7f0 ? trace_printk_init_buffers+0x60/0x60 ? selinux_file_permission+0x3b/0x540 ? tracer_preempt_off+0x38/0x506 ? trace_printk_init_buffers+0x60/0x60 __vfs_write+0x81/0x100 vfs_write+0x1e1/0x560 ksys_write+0x126/0x250 ? __ia32_sys_read+0xb0/0xb0 ? do_syscall_64+0x1f/0x390 do_syscall_64+0xc1/0x390 entry_SYSCALL_64_after_hwframe+0x49/0xbe
This patch adds resize_buffer_duplicate_size() to check if there is a difference between current/spare buffer sizes and resize a spare buffer if necessary.
--- kernel/trace/trace.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/trace/trace.c +6 -4
@@ -6719,11 +6719,13 @@ tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt, break; }#endif -if (!tr->allocated_snapshot) { +if (tr->allocated_snapshot) + ret =resize_buffer_duplicate_size (&tr->max_buffer, + &tr->trace_buffer, iter->cpu_file); +else ret =tracing_alloc_snapshot_instance (tr); -if (ret <0 ) -break ; - } +if (ret <0 ) +break ;local_irq_disable ();/* Now, we're going to swap */ if (iter->cpu_file ==RING_BUFFER_ALL_CPUS )
Signature · 1 line
2.20.1
Steven Rostedt
maintainerPATCH 5/5 ftrace/x86: Anotate text_mutex split between ftrace_arch_code_modify_post_process() and ftrace_arch_code_modify_prepare()
In reply to Steven RostedtFrom: Jiri Kosina <[email protected]>
ftrace_arch_code_modify_prepare() is acquiring text_mutex, while the corresponding release is happening in ftrace_arch_code_modify_post_process().
This has already been documented in the code, but let's also make the fact that this is intentional clear to the semantic analysis tools such as sparse.
--- arch/x86/kernel/ftrace.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kernel/ftrace.c +2 -0
@@ -35,6 +35,7 @@#ifdef CONFIG_DYNAMIC_FTRACE int ftrace_arch_code_modify_prepare (void ) + __acquires(&text_mutex) {/* * Need to grab text_mutex to prevent a race from module loading
@@ -48,6 +49,7 @@ int ftrace_arch_code_modify_prepare(void) }int ftrace_arch_code_modify_post_process (void ) + __releases(&text_mutex) {set_all_modules_text_ro ();set_kernel_text_ro ();
Signature · 1 line
2.20.1