History

tracing: Fix sleeping function called from invalid context on RT kernel

When setting bootparams="trace_event=initcall:initcall_start tp_printk=1" in the cmdline, the output_printk() was called, and the spin_lock_irqsave() was called in the atomic and irq disable interrupt context suitation. On the PREEMPT_RT kernel, these locks are replaced with sleepable rt-spinlock, so the stack calltrace will be triggered. Fix it by raw_spin_lock_irqsave when PREEMPT_RT and "trace_event=initcall:initcall_start tp_printk=1" enabled. BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:46 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1, name: swapper/0 preempt_count: 2, expected: 0 RCU nest depth: 0, expected: 0 Preemption disabled at: [<ffffffff8992303e>] try_to_wake_up+0x7e/0xba0 CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.17.1-rt17+ #19 34c5812404187a875f32bee7977f7367f9679ea7 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 Call Trace: <TASK> dump_stack_lvl+0x60/0x8c dump_stack+0x10/0x12 __might_resched.cold+0x11d/0x155 rt_spin_lock+0x40/0x70 trace_event_buffer_commit+0x2fa/0x4c0 ? map_vsyscall+0x93/0x93 trace_event_raw_event_initcall_start+0xbe/0x110 ? perf_trace_initcall_finish+0x210/0x210 ? probe_sched_wakeup+0x34/0x40 ? ttwu_do_wakeup+0xda/0x310 ? trace_hardirqs_on+0x35/0x170 ? map_vsyscall+0x93/0x93 do_one_initcall+0x217/0x3c0 ? trace_event_raw_event_initcall_level+0x170/0x170 ? push_cpu_stop+0x400/0x400 ? cblist_init_generic+0x241/0x290 kernel_init_freeable+0x1ac/0x347 ? _raw_spin_unlock_irq+0x65/0x80 ? rest_init+0xf0/0xf0 kernel_init+0x1e/0x150 ret_from_fork+0x22/0x30 </TASK> Link: https://lkml.kernel.org/r/20220419013910.894370-1-jun.miao@intel.com

Jun Miao committed 4 years ago

Commit 12025ab1 parent 69686fc1 file · +3 −3Browse files

Unified · Split

kernel/trace/trace.c+3−3
@@ -2835,7 +2835,7 @@ trace_event_buffer_lock_reserve(struct trace_buffer **current_rb, · 2834 unmodified lines
2835}2835}
2836EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);2836EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2837 2837
2838static DEFINE_SPINLOCK(tracepoint_iter_lock);2838static DEFINE_RAW_SPINLOCK(tracepoint_iter_lock);
2839static DEFINE_MUTEX(tracepoint_printk_mutex);2839static DEFINE_MUTEX(tracepoint_printk_mutex);
2840 2840
2841static void output_printk(struct trace_event_buffer *fbuffer)2841static void output_printk(struct trace_event_buffer *fbuffer)
@@ -2863,14 +2863,14 @@ static void output_printk(struct trace_event_buffer *fbuffer) · 21 unmodified lines
2863 2863
2864 event = &fbuffer->trace_file->event_call->event;2864 event = &fbuffer->trace_file->event_call->event;
2865 2865
2866 spin_lock_irqsave(&tracepoint_iter_lock, flags);2866 raw_spin_lock_irqsave(&tracepoint_iter_lock, flags);
2867 trace_seq_init(&iter->seq);2867 trace_seq_init(&iter->seq);
2868 iter->ent = fbuffer->entry;2868 iter->ent = fbuffer->entry;
2869 event_call->event.funcs->trace(iter, 0, event);2869 event_call->event.funcs->trace(iter, 0, event);
2870 trace_seq_putc(&iter->seq, 0);2870 trace_seq_putc(&iter->seq, 0);
2871 printk("%s", iter->seq.buffer);2871 printk("%s", iter->seq.buffer);
2872 2872
2873 spin_unlock_irqrestore(&tracepoint_iter_lock, flags);2873 raw_spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
2874}2874}
2875 2875
2876int tracepoint_printk_sysctl(struct ctl_table *table, int write,2876int tracepoint_printk_sysctl(struct ctl_table *table, int write,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t