From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Daniel Bristot de Oliveira <bristot@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
Boqun Feng <boqun.feng@gmail.com>,
stable@vger.kerne.org, Waiman Long <longman@redhat.com>
Subject: [for-next][PATCH 16/20] tracing: Disable interrupt or preemption before acquiring arch_spinlock_t
Date: Tue, 27 Sep 2022 12:02:32 -0400 [thread overview]
Message-ID: <20220927160249.931823392@goodmis.org> (raw)
In-Reply-To: 20220927160216.349640304@goodmis.org
From: Waiman Long <longman@redhat.com>
It was found that some tracing functions in kernel/trace/trace.c acquire
an arch_spinlock_t with preemption and irqs enabled. An example is the
tracing_saved_cmdlines_size_read() function which intermittently causes
a "BUG: using smp_processor_id() in preemptible" warning when the LTP
read_all_proc test is run.
That can be problematic in case preemption happens after acquiring the
lock. Add the necessary preemption or interrupt disabling code in the
appropriate places before acquiring an arch_spinlock_t.
The convention here is to disable preemption for trace_cmdline_lock and
interupt for max_lock.
Link: https://lkml.kernel.org/r/20220922145622.1744826-1-longman@redhat.com
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Will Deacon <will@kernel.org>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: stable@vger.kerne.org
Fixes: a35873a0993b ("tracing: Add conditional snapshot")
Fixes: 939c7a4f04fc ("tracing: Introduce saved_cmdlines_size file")
Suggested-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Waiman Long <longman@redhat.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
kernel/trace/trace.c | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index d3005279165d..aed7ea6e6045 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -1193,12 +1193,14 @@ void *tracing_cond_snapshot_data(struct trace_array *tr)
{
void *cond_data = NULL;
+ local_irq_disable();
arch_spin_lock(&tr->max_lock);
if (tr->cond_snapshot)
cond_data = tr->cond_snapshot->cond_data;
arch_spin_unlock(&tr->max_lock);
+ local_irq_enable();
return cond_data;
}
@@ -1334,9 +1336,11 @@ int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
goto fail_unlock;
}
+ local_irq_disable();
arch_spin_lock(&tr->max_lock);
tr->cond_snapshot = cond_snapshot;
arch_spin_unlock(&tr->max_lock);
+ local_irq_enable();
mutex_unlock(&trace_types_lock);
@@ -1363,6 +1367,7 @@ int tracing_snapshot_cond_disable(struct trace_array *tr)
{
int ret = 0;
+ local_irq_disable();
arch_spin_lock(&tr->max_lock);
if (!tr->cond_snapshot)
@@ -1373,6 +1378,7 @@ int tracing_snapshot_cond_disable(struct trace_array *tr)
}
arch_spin_unlock(&tr->max_lock);
+ local_irq_enable();
return ret;
}
@@ -2200,6 +2206,11 @@ static size_t tgid_map_max;
#define SAVED_CMDLINES_DEFAULT 128
#define NO_CMDLINE_MAP UINT_MAX
+/*
+ * Preemption must be disabled before acquiring trace_cmdline_lock.
+ * The various trace_arrays' max_lock must be acquired in a context
+ * where interrupt is disabled.
+ */
static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
struct saved_cmdlines_buffer {
unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
@@ -2412,7 +2423,11 @@ static int trace_save_cmdline(struct task_struct *tsk)
* the lock, but we also don't want to spin
* nor do we want to disable interrupts,
* so if we miss here, then better luck next time.
+ *
+ * This is called within the scheduler and wake up, so interrupts
+ * had better been disabled and run queue lock been held.
*/
+ lockdep_assert_preemption_disabled();
if (!arch_spin_trylock(&trace_cmdline_lock))
return 0;
@@ -5890,9 +5905,11 @@ tracing_saved_cmdlines_size_read(struct file *filp, char __user *ubuf,
char buf[64];
int r;
+ preempt_disable();
arch_spin_lock(&trace_cmdline_lock);
r = scnprintf(buf, sizeof(buf), "%u\n", savedcmd->cmdline_num);
arch_spin_unlock(&trace_cmdline_lock);
+ preempt_enable();
return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
}
@@ -5917,10 +5934,12 @@ static int tracing_resize_saved_cmdlines(unsigned int val)
return -ENOMEM;
}
+ preempt_disable();
arch_spin_lock(&trace_cmdline_lock);
savedcmd_temp = savedcmd;
savedcmd = s;
arch_spin_unlock(&trace_cmdline_lock);
+ preempt_enable();
free_saved_cmdlines_buffer(savedcmd_temp);
return 0;
@@ -6373,10 +6392,12 @@ int tracing_set_tracer(struct trace_array *tr, const char *buf)
#ifdef CONFIG_TRACER_SNAPSHOT
if (t->use_max_tr) {
+ local_irq_disable();
arch_spin_lock(&tr->max_lock);
if (tr->cond_snapshot)
ret = -EBUSY;
arch_spin_unlock(&tr->max_lock);
+ local_irq_enable();
if (ret)
goto out;
}
@@ -7436,10 +7457,12 @@ tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
goto out;
}
+ local_irq_disable();
arch_spin_lock(&tr->max_lock);
if (tr->cond_snapshot)
ret = -EBUSY;
arch_spin_unlock(&tr->max_lock);
+ local_irq_enable();
if (ret)
goto out;
--
2.35.1
next prev parent reply other threads:[~2022-09-27 16:02 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-27 16:02 [for-next][PATCH 00/20] tracing: Update for 6.1 Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 01/20] tracing/eprobe: Add eprobe filter support Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 02/20] selftests/ftrace: Add eprobe syntax error testcase Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 03/20] rv/monitors: add static qualifier for local symbols Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 04/20] rv/dot2K: add static qualifier for local variable Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 05/20] tracing: Add numeric delta time to the trace event benchmark Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 06/20] tracing/hist: Call hist functions directly via a switch statement Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 07/20] tracing: Move struct filter_pred into trace_events_filter.c Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 08/20] tracing/filter: Call filter predicate functions directly via a switch statement Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 09/20] tracepoint: Optimize the critical region of mutex_lock in tracepoint_module_coming() Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 10/20] x86/ftrace: Remove unused modifying_ftrace_code declaration Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 11/20] x86/kprobes: Remove unused arch_kprobe_override_function() declaration Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 12/20] tracing: kprobe: Fix kprobe event gen test module on exit Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 13/20] tracing: kprobe: Make gen test module work in arm and riscv Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 14/20] tracing/osnoise: Fix possible recursive locking in stop_per_cpu_kthreads Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 15/20] rv/monitor: Add __init/__exit annotations to module init/exit funcs Steven Rostedt
2022-09-27 16:02 ` Steven Rostedt [this message]
2022-09-27 16:02 ` [for-next][PATCH 17/20] ftrace: Remove obsoleted code from ftrace and task_struct Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 18/20] x86: kprobes: Remove unused macro stack_addr Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 19/20] tracing/eprobe: Fix alloc event dir failed when event name no set Steven Rostedt
2022-09-27 16:02 ` [for-next][PATCH 20/20] ftrace: Properly unset FTRACE_HASH_FL_MOD Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220927160249.931823392@goodmis.org \
--to=rostedt@goodmis.org \
--cc=boqun.feng@gmail.com \
--cc=bristot@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=stable@vger.kerne.org \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox