1919#include <linux/seq_file.h>
2020#include <linux/notifier.h>
2121#include <linux/irqflags.h>
22+ #include <linux/irq_work.h>
2223#include <linux/debugfs.h>
2324#include <linux/pagemap.h>
2425#include <linux/hardirq.h>
@@ -84,6 +85,14 @@ static int dummy_set_flag(u32 old_flags, u32 bit, int set)
8485 */
8586static DEFINE_PER_CPU (bool , trace_cmdline_save );
8687
88+ /*
89+ * When a reader is waiting for data, then this variable is
90+ * set to true.
91+ */
92+ static bool trace_wakeup_needed ;
93+
94+ static struct irq_work trace_work_wakeup ;
95+
8796/*
8897 * Kill all tracing for good (never come back).
8998 * It is initialized to 1 but will turn to zero if the initialization
@@ -329,12 +338,18 @@ unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
329338static int trace_stop_count ;
330339static DEFINE_RAW_SPINLOCK (tracing_start_lock );
331340
332- static void wakeup_work_handler (struct work_struct * work )
341+ /**
342+ * trace_wake_up - wake up tasks waiting for trace input
343+ *
344+ * Schedules a delayed work to wake up any task that is blocked on the
345+ * trace_wait queue. These is used with trace_poll for tasks polling the
346+ * trace.
347+ */
348+ static void trace_wake_up (struct irq_work * work )
333349{
334- wake_up (& trace_wait );
335- }
350+ wake_up_all (& trace_wait );
336351
337- static DECLARE_DELAYED_WORK ( wakeup_work , wakeup_work_handler ) ;
352+ }
338353
339354/**
340355 * tracing_on - enable tracing buffers
@@ -389,22 +404,6 @@ int tracing_is_on(void)
389404}
390405EXPORT_SYMBOL_GPL (tracing_is_on );
391406
392- /**
393- * trace_wake_up - wake up tasks waiting for trace input
394- *
395- * Schedules a delayed work to wake up any task that is blocked on the
396- * trace_wait queue. These is used with trace_poll for tasks polling the
397- * trace.
398- */
399- void trace_wake_up (void )
400- {
401- const unsigned long delay = msecs_to_jiffies (2 );
402-
403- if (trace_flags & TRACE_ITER_BLOCK )
404- return ;
405- schedule_delayed_work (& wakeup_work , delay );
406- }
407-
408407static int __init set_buf_size (char * str )
409408{
410409 unsigned long buf_size ;
@@ -753,6 +752,40 @@ update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
753752}
754753#endif /* CONFIG_TRACER_MAX_TRACE */
755754
755+ static void default_wait_pipe (struct trace_iterator * iter )
756+ {
757+ DEFINE_WAIT (wait );
758+
759+ prepare_to_wait (& trace_wait , & wait , TASK_INTERRUPTIBLE );
760+
761+ /*
762+ * The events can happen in critical sections where
763+ * checking a work queue can cause deadlocks.
764+ * After adding a task to the queue, this flag is set
765+ * only to notify events to try to wake up the queue
766+ * using irq_work.
767+ *
768+ * We don't clear it even if the buffer is no longer
769+ * empty. The flag only causes the next event to run
770+ * irq_work to do the work queue wake up. The worse
771+ * that can happen if we race with !trace_empty() is that
772+ * an event will cause an irq_work to try to wake up
773+ * an empty queue.
774+ *
775+ * There's no reason to protect this flag either, as
776+ * the work queue and irq_work logic will do the necessary
777+ * synchronization for the wake ups. The only thing
778+ * that is necessary is that the wake up happens after
779+ * a task has been queued. It's OK for spurious wake ups.
780+ */
781+ trace_wakeup_needed = true;
782+
783+ if (trace_empty (iter ))
784+ schedule ();
785+
786+ finish_wait (& trace_wait , & wait );
787+ }
788+
756789/**
757790 * register_tracer - register a tracer with the ftrace system.
758791 * @type - the plugin for the tracer
@@ -1156,30 +1189,32 @@ void
11561189__buffer_unlock_commit (struct ring_buffer * buffer , struct ring_buffer_event * event )
11571190{
11581191 __this_cpu_write (trace_cmdline_save , true);
1192+ if (trace_wakeup_needed ) {
1193+ trace_wakeup_needed = false;
1194+ /* irq_work_queue() supplies it's own memory barriers */
1195+ irq_work_queue (& trace_work_wakeup );
1196+ }
11591197 ring_buffer_unlock_commit (buffer , event );
11601198}
11611199
11621200static inline void
11631201__trace_buffer_unlock_commit (struct ring_buffer * buffer ,
11641202 struct ring_buffer_event * event ,
1165- unsigned long flags , int pc ,
1166- int wake )
1203+ unsigned long flags , int pc )
11671204{
11681205 __buffer_unlock_commit (buffer , event );
11691206
11701207 ftrace_trace_stack (buffer , flags , 6 , pc );
11711208 ftrace_trace_userstack (buffer , flags , pc );
1172-
1173- if (wake )
1174- trace_wake_up ();
11751209}
11761210
11771211void trace_buffer_unlock_commit (struct ring_buffer * buffer ,
11781212 struct ring_buffer_event * event ,
11791213 unsigned long flags , int pc )
11801214{
1181- __trace_buffer_unlock_commit (buffer , event , flags , pc , 1 );
1215+ __trace_buffer_unlock_commit (buffer , event , flags , pc );
11821216}
1217+ EXPORT_SYMBOL_GPL (trace_buffer_unlock_commit );
11831218
11841219struct ring_buffer_event *
11851220trace_current_buffer_lock_reserve (struct ring_buffer * * current_rb ,
@@ -1196,29 +1231,21 @@ void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
11961231 struct ring_buffer_event * event ,
11971232 unsigned long flags , int pc )
11981233{
1199- __trace_buffer_unlock_commit (buffer , event , flags , pc , 1 );
1234+ __trace_buffer_unlock_commit (buffer , event , flags , pc );
12001235}
12011236EXPORT_SYMBOL_GPL (trace_current_buffer_unlock_commit );
12021237
1203- void trace_nowake_buffer_unlock_commit (struct ring_buffer * buffer ,
1204- struct ring_buffer_event * event ,
1205- unsigned long flags , int pc )
1206- {
1207- __trace_buffer_unlock_commit (buffer , event , flags , pc , 0 );
1208- }
1209- EXPORT_SYMBOL_GPL (trace_nowake_buffer_unlock_commit );
1210-
1211- void trace_nowake_buffer_unlock_commit_regs (struct ring_buffer * buffer ,
1212- struct ring_buffer_event * event ,
1213- unsigned long flags , int pc ,
1214- struct pt_regs * regs )
1238+ void trace_buffer_unlock_commit_regs (struct ring_buffer * buffer ,
1239+ struct ring_buffer_event * event ,
1240+ unsigned long flags , int pc ,
1241+ struct pt_regs * regs )
12151242{
12161243 __buffer_unlock_commit (buffer , event );
12171244
12181245 ftrace_trace_stack_regs (buffer , flags , 0 , pc , regs );
12191246 ftrace_trace_userstack (buffer , flags , pc );
12201247}
1221- EXPORT_SYMBOL_GPL (trace_nowake_buffer_unlock_commit_regs );
1248+ EXPORT_SYMBOL_GPL (trace_buffer_unlock_commit_regs );
12221249
12231250void trace_current_buffer_discard_commit (struct ring_buffer * buffer ,
12241251 struct ring_buffer_event * event )
@@ -3354,19 +3381,6 @@ tracing_poll_pipe(struct file *filp, poll_table *poll_table)
33543381 }
33553382}
33563383
3357-
3358- void default_wait_pipe (struct trace_iterator * iter )
3359- {
3360- DEFINE_WAIT (wait );
3361-
3362- prepare_to_wait (& trace_wait , & wait , TASK_INTERRUPTIBLE );
3363-
3364- if (trace_empty (iter ))
3365- schedule ();
3366-
3367- finish_wait (& trace_wait , & wait );
3368- }
3369-
33703384/*
33713385 * This is a make-shift waitqueue.
33723386 * A tracer might use this callback on some rare cases:
@@ -5107,6 +5121,7 @@ __init static int tracer_alloc_buffers(void)
51075121#endif
51085122
51095123 trace_init_cmdlines ();
5124+ init_irq_work (& trace_work_wakeup , trace_wake_up );
51105125
51115126 register_tracer (& nop_trace );
51125127 current_trace = & nop_trace ;
0 commit comments