3232#include <linux/splice.h>
3333#include <linux/kdebug.h>
3434#include <linux/string.h>
35+ #include <linux/rwsem.h>
3536#include <linux/ctype.h>
3637#include <linux/init.h>
3738#include <linux/poll.h>
@@ -102,9 +103,6 @@ static inline void ftrace_enable_cpu(void)
102103
103104static cpumask_var_t __read_mostly tracing_buffer_mask ;
104105
105- /* Define which cpu buffers are currently read in trace_pipe */
106- static cpumask_var_t tracing_reader_cpumask ;
107-
108106#define for_each_tracing_cpu (cpu ) \
109107 for_each_cpu(cpu, tracing_buffer_mask)
110108
@@ -243,12 +241,91 @@ static struct tracer *current_trace __read_mostly;
243241
244242/*
245243 * trace_types_lock is used to protect the trace_types list.
246- * This lock is also used to keep user access serialized.
247- * Accesses from userspace will grab this lock while userspace
248- * activities happen inside the kernel.
249244 */
250245static DEFINE_MUTEX (trace_types_lock );
251246
247+ /*
248+ * serialize the access of the ring buffer
249+ *
250+ * ring buffer serializes readers, but it is low level protection.
251+ * The validity of the events (which returns by ring_buffer_peek() ..etc)
252+ * are not protected by ring buffer.
253+ *
254+ * The content of events may become garbage if we allow other process consumes
255+ * these events concurrently:
256+ * A) the page of the consumed events may become a normal page
257+ * (not reader page) in ring buffer, and this page will be rewrited
258+ * by events producer.
259+ * B) The page of the consumed events may become a page for splice_read,
260+ * and this page will be returned to system.
261+ *
262+ * These primitives allow multi process access to different cpu ring buffer
263+ * concurrently.
264+ *
265+ * These primitives don't distinguish read-only and read-consume access.
266+ * Multi read-only access are also serialized.
267+ */
268+
269+ #ifdef CONFIG_SMP
270+ static DECLARE_RWSEM (all_cpu_access_lock );
271+ static DEFINE_PER_CPU (struct mutex , cpu_access_lock ) ;
272+
273+ static inline void trace_access_lock (int cpu )
274+ {
275+ if (cpu == TRACE_PIPE_ALL_CPU ) {
276+ /* gain it for accessing the whole ring buffer. */
277+ down_write (& all_cpu_access_lock );
278+ } else {
279+ /* gain it for accessing a cpu ring buffer. */
280+
281+ /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
282+ down_read (& all_cpu_access_lock );
283+
284+ /* Secondly block other access to this @cpu ring buffer. */
285+ mutex_lock (& per_cpu (cpu_access_lock , cpu ));
286+ }
287+ }
288+
289+ static inline void trace_access_unlock (int cpu )
290+ {
291+ if (cpu == TRACE_PIPE_ALL_CPU ) {
292+ up_write (& all_cpu_access_lock );
293+ } else {
294+ mutex_unlock (& per_cpu (cpu_access_lock , cpu ));
295+ up_read (& all_cpu_access_lock );
296+ }
297+ }
298+
299+ static inline void trace_access_lock_init (void )
300+ {
301+ int cpu ;
302+
303+ for_each_possible_cpu (cpu )
304+ mutex_init (& per_cpu (cpu_access_lock , cpu ));
305+ }
306+
307+ #else
308+
309+ static DEFINE_MUTEX (access_lock );
310+
311+ static inline void trace_access_lock (int cpu )
312+ {
313+ (void )cpu ;
314+ mutex_lock (& access_lock );
315+ }
316+
317+ static inline void trace_access_unlock (int cpu )
318+ {
319+ (void )cpu ;
320+ mutex_unlock (& access_lock );
321+ }
322+
323+ static inline void trace_access_lock_init (void )
324+ {
325+ }
326+
327+ #endif
328+
252329/* trace_wait is a waitqueue for tasks blocked on trace_poll */
253330static DECLARE_WAIT_QUEUE_HEAD (trace_wait );
254331
@@ -1580,12 +1657,6 @@ static void tracing_iter_reset(struct trace_iterator *iter, int cpu)
15801657}
15811658
15821659/*
1583- * No necessary locking here. The worst thing which can
1584- * happen is loosing events consumed at the same time
1585- * by a trace_pipe reader.
1586- * Other than that, we don't risk to crash the ring buffer
1587- * because it serializes the readers.
1588- *
15891660 * The current tracer is copied to avoid a global locking
15901661 * all around.
15911662 */
@@ -1640,12 +1711,16 @@ static void *s_start(struct seq_file *m, loff_t *pos)
16401711 }
16411712
16421713 trace_event_read_lock ();
1714+ trace_access_lock (cpu_file );
16431715 return p ;
16441716}
16451717
16461718static void s_stop (struct seq_file * m , void * p )
16471719{
1720+ struct trace_iterator * iter = m -> private ;
1721+
16481722 atomic_dec (& trace_record_cmdline_disabled );
1723+ trace_access_unlock (iter -> cpu_file );
16491724 trace_event_read_unlock ();
16501725}
16511726
@@ -2836,22 +2911,6 @@ static int tracing_open_pipe(struct inode *inode, struct file *filp)
28362911
28372912 mutex_lock (& trace_types_lock );
28382913
2839- /* We only allow one reader per cpu */
2840- if (cpu_file == TRACE_PIPE_ALL_CPU ) {
2841- if (!cpumask_empty (tracing_reader_cpumask )) {
2842- ret = - EBUSY ;
2843- goto out ;
2844- }
2845- cpumask_setall (tracing_reader_cpumask );
2846- } else {
2847- if (!cpumask_test_cpu (cpu_file , tracing_reader_cpumask ))
2848- cpumask_set_cpu (cpu_file , tracing_reader_cpumask );
2849- else {
2850- ret = - EBUSY ;
2851- goto out ;
2852- }
2853- }
2854-
28552914 /* create a buffer to store the information to pass to userspace */
28562915 iter = kzalloc (sizeof (* iter ), GFP_KERNEL );
28572916 if (!iter ) {
@@ -2907,12 +2966,6 @@ static int tracing_release_pipe(struct inode *inode, struct file *file)
29072966
29082967 mutex_lock (& trace_types_lock );
29092968
2910- if (iter -> cpu_file == TRACE_PIPE_ALL_CPU )
2911- cpumask_clear (tracing_reader_cpumask );
2912- else
2913- cpumask_clear_cpu (iter -> cpu_file , tracing_reader_cpumask );
2914-
2915-
29162969 if (iter -> trace -> pipe_close )
29172970 iter -> trace -> pipe_close (iter );
29182971
@@ -3074,6 +3127,7 @@ tracing_read_pipe(struct file *filp, char __user *ubuf,
30743127 iter -> pos = -1 ;
30753128
30763129 trace_event_read_lock ();
3130+ trace_access_lock (iter -> cpu_file );
30773131 while (find_next_entry_inc (iter ) != NULL ) {
30783132 enum print_line_t ret ;
30793133 int len = iter -> seq .len ;
@@ -3090,6 +3144,7 @@ tracing_read_pipe(struct file *filp, char __user *ubuf,
30903144 if (iter -> seq .len >= cnt )
30913145 break ;
30923146 }
3147+ trace_access_unlock (iter -> cpu_file );
30933148 trace_event_read_unlock ();
30943149
30953150 /* Now copy what we have to the user */
@@ -3215,6 +3270,7 @@ static ssize_t tracing_splice_read_pipe(struct file *filp,
32153270 }
32163271
32173272 trace_event_read_lock ();
3273+ trace_access_lock (iter -> cpu_file );
32183274
32193275 /* Fill as many pages as possible. */
32203276 for (i = 0 , rem = len ; i < PIPE_BUFFERS && rem ; i ++ ) {
@@ -3238,6 +3294,7 @@ static ssize_t tracing_splice_read_pipe(struct file *filp,
32383294 trace_seq_init (& iter -> seq );
32393295 }
32403296
3297+ trace_access_unlock (iter -> cpu_file );
32413298 trace_event_read_unlock ();
32423299 mutex_unlock (& iter -> mutex );
32433300
@@ -3539,10 +3596,12 @@ tracing_buffers_read(struct file *filp, char __user *ubuf,
35393596
35403597 info -> read = 0 ;
35413598
3599+ trace_access_lock (info -> cpu );
35423600 ret = ring_buffer_read_page (info -> tr -> buffer ,
35433601 & info -> spare ,
35443602 count ,
35453603 info -> cpu , 0 );
3604+ trace_access_unlock (info -> cpu );
35463605 if (ret < 0 )
35473606 return 0 ;
35483607
@@ -3670,6 +3729,7 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos,
36703729 len &= PAGE_MASK ;
36713730 }
36723731
3732+ trace_access_lock (info -> cpu );
36733733 entries = ring_buffer_entries_cpu (info -> tr -> buffer , info -> cpu );
36743734
36753735 for (i = 0 ; i < PIPE_BUFFERS && len && entries ; i ++ , len -= PAGE_SIZE ) {
@@ -3717,6 +3777,7 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos,
37173777 entries = ring_buffer_entries_cpu (info -> tr -> buffer , info -> cpu );
37183778 }
37193779
3780+ trace_access_unlock (info -> cpu );
37203781 spd .nr_pages = i ;
37213782
37223783 /* did we read anything? */
@@ -4153,6 +4214,8 @@ static __init int tracer_init_debugfs(void)
41534214 struct dentry * d_tracer ;
41544215 int cpu ;
41554216
4217+ trace_access_lock_init ();
4218+
41564219 d_tracer = tracing_init_dentry ();
41574220
41584221 trace_create_file ("tracing_enabled" , 0644 , d_tracer ,
@@ -4387,9 +4450,6 @@ __init static int tracer_alloc_buffers(void)
43874450 if (!alloc_cpumask_var (& tracing_cpumask , GFP_KERNEL ))
43884451 goto out_free_buffer_mask ;
43894452
4390- if (!zalloc_cpumask_var (& tracing_reader_cpumask , GFP_KERNEL ))
4391- goto out_free_tracing_cpumask ;
4392-
43934453 /* To save memory, keep the ring buffer size to its minimum */
43944454 if (ring_buffer_expanded )
43954455 ring_buf_size = trace_buf_size ;
@@ -4447,8 +4507,6 @@ __init static int tracer_alloc_buffers(void)
44474507 return 0 ;
44484508
44494509out_free_cpumask :
4450- free_cpumask_var (tracing_reader_cpumask );
4451- out_free_tracing_cpumask :
44524510 free_cpumask_var (tracing_cpumask );
44534511out_free_buffer_mask :
44544512 free_cpumask_var (tracing_buffer_mask );
0 commit comments