Skip to content

Commit 550d1f5

Browse files
committed
Merge tag 'trace-v5.2-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace
Pull tracing fixes from Steven Rostedt: "This includes three fixes: - Fix a deadlock from a previous fix to keep module loading and function tracing text modifications from stepping on each other (this has a few patches to help document the issue in comments) - Fix a crash when the snapshot buffer gets out of sync with the main ring buffer - Fix a memory leak when reading the memory logs" * tag 'trace-v5.2-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace: ftrace/x86: Anotate text_mutex split between ftrace_arch_code_modify_post_process() and ftrace_arch_code_modify_prepare() tracing/snapshot: Resize spare buffer if size changed tracing: Fix memory leak in tracing_err_log_open() ftrace/x86: Add a comment to why we take text_mutex in ftrace_arch_code_modify_prepare() ftrace/x86: Remove possible deadlock between register_kprobe() and ftrace_run_update_code()
2 parents 179c96d + 074376a commit 550d1f5

3 files changed

Lines changed: 30 additions & 14 deletions

File tree

‎arch/x86/kernel/ftrace.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/init.h>
2323
#include <linux/list.h>
2424
#include <linux/module.h>
25+
#include <linux/memory.h>
2526

2627
#include <trace/syscall.h>
2728

@@ -34,16 +35,25 @@
3435
#ifdef CONFIG_DYNAMIC_FTRACE
3536

3637
int ftrace_arch_code_modify_prepare(void)
38+
__acquires(&text_mutex)
3739
{
40+
/*
41+
* Need to grab text_mutex to prevent a race from module loading
42+
* and live kernel patching from changing the text permissions while
43+
* ftrace has it set to "read/write".
44+
*/
45+
mutex_lock(&text_mutex);
3846
set_kernel_text_rw();
3947
set_all_modules_text_rw();
4048
return 0;
4149
}
4250

4351
int ftrace_arch_code_modify_post_process(void)
52+
__releases(&text_mutex)
4453
{
4554
set_all_modules_text_ro();
4655
set_kernel_text_ro();
56+
mutex_unlock(&text_mutex);
4757
return 0;
4858
}
4959

‎kernel/trace/ftrace.c‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <linux/hash.h>
3535
#include <linux/rcupdate.h>
3636
#include <linux/kprobes.h>
37-
#include <linux/memory.h>
3837

3938
#include <trace/events/sched.h>
4039

@@ -2611,12 +2610,10 @@ static void ftrace_run_update_code(int command)
26112610
{
26122611
int ret;
26132612

2614-
mutex_lock(&text_mutex);
2615-
26162613
ret = ftrace_arch_code_modify_prepare();
26172614
FTRACE_WARN_ON(ret);
26182615
if (ret)
2619-
goto out_unlock;
2616+
return;
26202617

26212618
/*
26222619
* By default we use stop_machine() to modify the code.
@@ -2628,9 +2625,6 @@ static void ftrace_run_update_code(int command)
26282625

26292626
ret = ftrace_arch_code_modify_post_process();
26302627
FTRACE_WARN_ON(ret);
2631-
2632-
out_unlock:
2633-
mutex_unlock(&text_mutex);
26342628
}
26352629

26362630
static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
@@ -5784,7 +5778,6 @@ void ftrace_module_enable(struct module *mod)
57845778
struct ftrace_page *pg;
57855779

57865780
mutex_lock(&ftrace_lock);
5787-
mutex_lock(&text_mutex);
57885781

57895782
if (ftrace_disabled)
57905783
goto out_unlock;
@@ -5846,7 +5839,6 @@ void ftrace_module_enable(struct module *mod)
58465839
ftrace_arch_code_modify_post_process();
58475840

58485841
out_unlock:
5849-
mutex_unlock(&text_mutex);
58505842
mutex_unlock(&ftrace_lock);
58515843

58525844
process_cached_mods(mod->name);

‎kernel/trace/trace.c‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6719,11 +6719,13 @@ tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
67196719
break;
67206720
}
67216721
#endif
6722-
if (!tr->allocated_snapshot) {
6722+
if (tr->allocated_snapshot)
6723+
ret = resize_buffer_duplicate_size(&tr->max_buffer,
6724+
&tr->trace_buffer, iter->cpu_file);
6725+
else
67236726
ret = tracing_alloc_snapshot_instance(tr);
6724-
if (ret < 0)
6725-
break;
6726-
}
6727+
if (ret < 0)
6728+
break;
67276729
local_irq_disable();
67286730
/* Now, we're going to swap */
67296731
if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
@@ -7126,12 +7128,24 @@ static ssize_t tracing_err_log_write(struct file *file,
71267128
return count;
71277129
}
71287130

7131+
static int tracing_err_log_release(struct inode *inode, struct file *file)
7132+
{
7133+
struct trace_array *tr = inode->i_private;
7134+
7135+
trace_array_put(tr);
7136+
7137+
if (file->f_mode & FMODE_READ)
7138+
seq_release(inode, file);
7139+
7140+
return 0;
7141+
}
7142+
71297143
static const struct file_operations tracing_err_log_fops = {
71307144
.open = tracing_err_log_open,
71317145
.write = tracing_err_log_write,
71327146
.read = seq_read,
71337147
.llseek = seq_lseek,
7134-
.release = tracing_release_generic_tr,
7148+
.release = tracing_err_log_release,
71357149
};
71367150

71377151
static int tracing_buffers_open(struct inode *inode, struct file *filp)

0 commit comments

Comments
 (0)