Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Generic ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/trace_events.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/ring_buffer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/trace_clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/sched/clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/trace_seq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/irq_work.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/hardirq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/kthread.h>	/* for self test */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/oom.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <asm/local.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) static void update_pages_handler(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  * The ring buffer header is special. We must manually up keep it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) int ring_buffer_print_entry_header(struct trace_seq *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	trace_seq_puts(s, "# compressed entry header\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	trace_seq_puts(s, "\ttype_len    :    5 bits\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	trace_seq_puts(s, "\ttime_delta  :   27 bits\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	trace_seq_puts(s, "\tarray       :   32 bits\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	trace_seq_putc(s, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	trace_seq_printf(s, "\tpadding     : type == %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 			 RINGBUF_TYPE_PADDING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	trace_seq_printf(s, "\ttime_extend : type == %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 			 RINGBUF_TYPE_TIME_EXTEND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	trace_seq_printf(s, "\ttime_stamp : type == %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 			 RINGBUF_TYPE_TIME_STAMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	trace_seq_printf(s, "\tdata max type_len  == %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 			 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	return !trace_seq_has_overflowed(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * The ring buffer is made up of a list of pages. A separate list of pages is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  * allocated for each CPU. A writer may only write to a buffer that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * associated with the CPU it is currently executing on.  A reader may read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * from any per cpu buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * The reader is special. For each per cpu buffer, the reader has its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  * reader page. When a reader has read the entire reader page, this reader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  * page is swapped with another page in the ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  * Now, as long as the writer is off the reader page, the reader can do what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  * ever it wants with that page. The writer will never write to that page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  * again (as long as it is out of the ring buffer).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * Here's some silly ASCII art.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  *   +------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  *   |reader|          RING BUFFER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  *   |page  |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  *   +------+        +---+   +---+   +---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  *                   |   |-->|   |-->|   |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  *                   +---+   +---+   +---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  *                     ^               |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  *                     |               |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  *                     +---------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  *   +------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  *   |reader|          RING BUFFER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  *   |page  |------------------v
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  *   +------+        +---+   +---+   +---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  *                   |   |-->|   |-->|   |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  *                   +---+   +---+   +---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  *                     ^               |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  *                     |               |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  *                     +---------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  *   +------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  *   |reader|          RING BUFFER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  *   |page  |------------------v
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96)  *   +------+        +---+   +---+   +---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97)  *      ^            |   |-->|   |-->|   |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  *      |            +---+   +---+   +---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  *      |                              |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  *      |                              |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  *      +------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  *   +------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  *   |buffer|          RING BUFFER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  *   |page  |------------------v
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  *   +------+        +---+   +---+   +---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108)  *      ^            |   |   |   |-->|   |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  *      |   New      +---+   +---+   +---+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  *      |  Reader------^               |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  *      |   page                       |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112)  *      +------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  * After we make this swap, the reader can hand this page off to the splice
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116)  * code and be done with it. It can even allocate a new page if it needs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117)  * and swap that into the ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  * We will be using cmpxchg soon to make all this lockless.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) /* Used for individual buffers (after the counter) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) #define RB_BUFFER_OFF		(1 << 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) #define RB_ALIGNMENT		4U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) #define RB_MAX_SMALL_DATA	(RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) #define RB_EVNT_MIN_SIZE	8U	/* two 32bit words */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) #ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) # define RB_FORCE_8BYTE_ALIGNMENT	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) # define RB_ARCH_ALIGNMENT		RB_ALIGNMENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) # define RB_FORCE_8BYTE_ALIGNMENT	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) # define RB_ARCH_ALIGNMENT		8U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) #define RB_ALIGN_DATA		__aligned(RB_ARCH_ALIGNMENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	RB_LEN_TIME_EXTEND = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	RB_LEN_TIME_STAMP =  8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) #define skip_time_extend(event) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) #define extended_time(event) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	(event->type_len >= RINGBUF_TYPE_TIME_EXTEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) static inline int rb_null_event(struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) static void rb_event_set_padding(struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	/* padding has a NULL time_delta */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	event->type_len = RINGBUF_TYPE_PADDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	event->time_delta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) static unsigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) rb_event_data_length(struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	unsigned length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	if (event->type_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		length = event->type_len * RB_ALIGNMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		length = event->array[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	return length + RB_EVNT_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182)  * Return the length of the given event. Will return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183)  * the length of the time extend if the event is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184)  * time extend.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) static inline unsigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) rb_event_length(struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	switch (event->type_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	case RINGBUF_TYPE_PADDING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		if (rb_null_event(event))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 			/* undefined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		return  event->array[0] + RB_EVNT_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	case RINGBUF_TYPE_TIME_EXTEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		return RB_LEN_TIME_EXTEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	case RINGBUF_TYPE_TIME_STAMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		return RB_LEN_TIME_STAMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	case RINGBUF_TYPE_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		return rb_event_data_length(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	/* not hit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  * Return total length of time extend and data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  *   or just the event length for all other events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) static inline unsigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) rb_event_ts_length(struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	unsigned len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	if (extended_time(event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		/* time extends include the data event after it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		len = RB_LEN_TIME_EXTEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		event = skip_time_extend(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	return len + rb_event_length(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229)  * ring_buffer_event_length - return the length of the event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230)  * @event: the event to get the length of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232)  * Returns the size of the data load of a data event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233)  * If the event is something other than a data event, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234)  * returns the size of the event itself. With the exception
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235)  * of a TIME EXTEND, where it still returns the size of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236)  * data load of the data event after it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) unsigned ring_buffer_event_length(struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	unsigned length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	if (extended_time(event))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		event = skip_time_extend(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	length = rb_event_length(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		return length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	length -= RB_EVNT_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250)                 length -= sizeof(event->array[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	return length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) EXPORT_SYMBOL_GPL(ring_buffer_event_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) /* inline for ring buffer fast paths */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) static __always_inline void *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) rb_event_data(struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	if (extended_time(event))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		event = skip_time_extend(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	WARN_ON_ONCE(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	/* If length is in len field, then array[0] has the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	if (event->type_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		return (void *)&event->array[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	/* Otherwise length is in array[0] and array[1] has the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	return (void *)&event->array[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270)  * ring_buffer_event_data - return the data of the event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271)  * @event: the event to get the data from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) void *ring_buffer_event_data(struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	return rb_event_data(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) EXPORT_SYMBOL_GPL(ring_buffer_event_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) #define for_each_buffer_cpu(buffer, cpu)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	for_each_cpu(cpu, buffer->cpumask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) #define for_each_online_buffer_cpu(buffer, cpu)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	for_each_cpu_and(cpu, buffer->cpumask, cpu_online_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) #define TS_SHIFT	27
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) #define TS_MASK		((1ULL << TS_SHIFT) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) #define TS_DELTA_TEST	(~TS_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290)  * ring_buffer_event_time_stamp - return the event's extended timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291)  * @event: the event to get the timestamp of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293)  * Returns the extended timestamp associated with a data event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294)  * An extended time_stamp is a 64-bit timestamp represented
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295)  * internally in a special way that makes the best use of space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296)  * contained within a ring buffer event.  This function decodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297)  * it and maps it to a straight u64 value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) u64 ring_buffer_event_time_stamp(struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	u64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	ts = event->array[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	ts <<= TS_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	ts += event->time_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	return ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) /* Flag when events were overwritten */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) #define RB_MISSED_EVENTS	(1 << 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) /* Missed count stored at end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) #define RB_MISSED_STORED	(1 << 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) struct buffer_data_page {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	u64		 time_stamp;	/* page time stamp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	local_t		 commit;	/* write committed index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	unsigned char	 data[] RB_ALIGN_DATA;	/* data of buffer page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322)  * Note, the buffer_page list must be first. The buffer pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323)  * are allocated in cache lines, which means that each buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324)  * page will be at the beginning of a cache line, and thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325)  * the least significant bits will be zero. We use this to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326)  * add flags in the list struct pointers, to make the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327)  * lockless.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) struct buffer_page {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	struct list_head list;		/* list of buffer pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	local_t		 write;		/* index for next write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	unsigned	 read;		/* index for next read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	local_t		 entries;	/* entries on this page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	unsigned long	 real_end;	/* real end of data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	struct buffer_data_page *page;	/* Actual data page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339)  * The buffer page counters, write and entries, must be reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340)  * atomically when crossing page boundaries. To synchronize this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341)  * update, two counters are inserted into the number. One is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342)  * the actual counter for the write position or count on the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344)  * The other is a counter of updaters. Before an update happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345)  * the update partition of the counter is incremented. This will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346)  * allow the updater to update the counter atomically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348)  * The counter is 20 bits, and the state data is 12.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) #define RB_WRITE_MASK		0xfffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) #define RB_WRITE_INTCNT		(1 << 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) static void rb_init_page(struct buffer_data_page *bpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	local_set(&bpage->commit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359)  * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360)  * this issue out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) static void free_buffer_page(struct buffer_page *bpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	free_page((unsigned long)bpage->page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	kfree(bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369)  * We need to fit the time_stamp delta into 27 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) static inline int test_time_stamp(u64 delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	if (delta & TS_DELTA_TEST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) int ring_buffer_print_page_header(struct trace_seq *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	struct buffer_data_page field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	trace_seq_printf(s, "\tfield: u64 timestamp;\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 			 "offset:0;\tsize:%u;\tsigned:%u;\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 			 (unsigned int)sizeof(field.time_stamp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 			 (unsigned int)is_signed_type(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	trace_seq_printf(s, "\tfield: local_t commit;\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 			 "offset:%u;\tsize:%u;\tsigned:%u;\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 			 (unsigned int)offsetof(typeof(field), commit),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 			 (unsigned int)sizeof(field.commit),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 			 (unsigned int)is_signed_type(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	trace_seq_printf(s, "\tfield: int overwrite;\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 			 "offset:%u;\tsize:%u;\tsigned:%u;\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 			 (unsigned int)offsetof(typeof(field), commit),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 			 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 			 (unsigned int)is_signed_type(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	trace_seq_printf(s, "\tfield: char data;\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 			 "offset:%u;\tsize:%u;\tsigned:%u;\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 			 (unsigned int)offsetof(typeof(field), data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 			 (unsigned int)BUF_PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 			 (unsigned int)is_signed_type(char));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	return !trace_seq_has_overflowed(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) struct rb_irq_work {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	struct irq_work			work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	wait_queue_head_t		waiters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	wait_queue_head_t		full_waiters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	bool				waiters_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	bool				full_waiters_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	bool				wakeup_full;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423)  * Structure to hold event state and handle nested events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) struct rb_event_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	u64			ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	u64			delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	u64			before;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	u64			after;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	unsigned long		length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	struct buffer_page	*tail_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	int			add_timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436)  * Used for the add_timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437)  *  NONE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438)  *  EXTEND - wants a time extend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439)  *  ABSOLUTE - the buffer requests all events to have absolute time stamps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440)  *  FORCE - force a full time stamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	RB_ADD_STAMP_NONE		= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	RB_ADD_STAMP_EXTEND		= BIT(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	RB_ADD_STAMP_ABSOLUTE		= BIT(2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	RB_ADD_STAMP_FORCE		= BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449)  * Used for which event context the event is in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  *  TRANSITION = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451)  *  NMI     = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452)  *  IRQ     = 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453)  *  SOFTIRQ = 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454)  *  NORMAL  = 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456)  * See trace_recursive_lock() comment below for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	RB_CTX_TRANSITION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	RB_CTX_NMI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	RB_CTX_IRQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	RB_CTX_SOFTIRQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	RB_CTX_NORMAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	RB_CTX_MAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) #if BITS_PER_LONG == 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) #define RB_TIME_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) /* To test on 64 bit machines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) //#define RB_TIME_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) #ifdef RB_TIME_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) struct rb_time_struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	local_t		cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	local_t		top;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	local_t		bottom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) #include <asm/local64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) struct rb_time_struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	local64_t	time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) typedef struct rb_time_struct rb_time_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490)  * head_page == tail_page && head == tail then buffer is empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) struct ring_buffer_per_cpu {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	int				cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	atomic_t			record_disabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	atomic_t			resize_disabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	struct trace_buffer	*buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	raw_spinlock_t			reader_lock;	/* serialize readers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	arch_spinlock_t			lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	struct lock_class_key		lock_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	struct buffer_data_page		*free_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	unsigned long			nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	unsigned int			current_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	struct list_head		*pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	struct buffer_page		*head_page;	/* read from head */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	struct buffer_page		*tail_page;	/* write to tail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	struct buffer_page		*commit_page;	/* committed pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	struct buffer_page		*reader_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	unsigned long			lost_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	unsigned long			last_overrun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	unsigned long			nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	local_t				entries_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	local_t				entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	local_t				overrun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	local_t				commit_overrun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	local_t				dropped_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	local_t				committing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	local_t				commits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	local_t				pages_touched;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	local_t				pages_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	long				last_pages_touch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	size_t				shortest_full;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	unsigned long			read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	unsigned long			read_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	rb_time_t			write_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	rb_time_t			before_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	u64				read_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	/* ring buffer pages to update, > 0 to add, < 0 to remove */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	long				nr_pages_to_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	struct list_head		new_pages; /* new pages to add */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	struct work_struct		update_pages_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	struct completion		update_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	struct rb_irq_work		irq_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) struct trace_buffer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	unsigned			flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	int				cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	atomic_t			record_disabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	cpumask_var_t			cpumask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	struct lock_class_key		*reader_lock_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	struct mutex			mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	struct ring_buffer_per_cpu	**buffers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	struct hlist_node		node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	u64				(*clock)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	struct rb_irq_work		irq_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	bool				time_stamp_abs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) struct ring_buffer_iter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	struct ring_buffer_per_cpu	*cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	unsigned long			head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	unsigned long			next_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	struct buffer_page		*head_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	struct buffer_page		*cache_reader_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	unsigned long			cache_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	u64				read_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	u64				page_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	struct ring_buffer_event	*event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	int				missed_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) #ifdef RB_TIME_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571)  * On 32 bit machines, local64_t is very expensive. As the ring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572)  * buffer doesn't need all the features of a true 64 bit atomic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573)  * on 32 bit, it uses these functions (64 still uses local64_t).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575)  * For the ring buffer, 64 bit required operations for the time is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576)  * the following:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578)  *  - Only need 59 bits (uses 60 to make it even).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579)  *  - Reads may fail if it interrupted a modification of the time stamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580)  *      It will succeed if it did not interrupt another write even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581)  *      the read itself is interrupted by a write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582)  *      It returns whether it was successful or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584)  *  - Writes always succeed and will overwrite other writes and writes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585)  *      that were done by events interrupting the current write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587)  *  - A write followed by a read of the same time stamp will always succeed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588)  *      but may not contain the same value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590)  *  - A cmpxchg will fail if it interrupted another write or cmpxchg.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591)  *      Other than that, it acts like a normal cmpxchg.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593)  * The 60 bit time stamp is broken up by 30 bits in a top and bottom half
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594)  *  (bottom being the least significant 30 bits of the 60 bit time stamp).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596)  * The two most significant bits of each half holds a 2 bit counter (0-3).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597)  * Each update will increment this counter by one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598)  * When reading the top and bottom, if the two counter bits match then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599)  *  top and bottom together make a valid 60 bit number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) #define RB_TIME_SHIFT	30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) #define RB_TIME_VAL_MASK ((1 << RB_TIME_SHIFT) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) static inline int rb_time_cnt(unsigned long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	return (val >> RB_TIME_SHIFT) & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) static inline u64 rb_time_val(unsigned long top, unsigned long bottom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	val = top & RB_TIME_VAL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	val <<= RB_TIME_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	val |= bottom & RB_TIME_VAL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) static inline bool __rb_time_read(rb_time_t *t, u64 *ret, unsigned long *cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	unsigned long top, bottom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	unsigned long c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	 * If the read is interrupted by a write, then the cnt will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	 * be different. Loop until both top and bottom have been read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	 * without interruption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		c = local_read(&t->cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		top = local_read(&t->top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		bottom = local_read(&t->bottom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	} while (c != local_read(&t->cnt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	*cnt = rb_time_cnt(top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	/* If top and bottom counts don't match, this interrupted a write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	if (*cnt != rb_time_cnt(bottom))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	*ret = rb_time_val(top, bottom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) static bool rb_time_read(rb_time_t *t, u64 *ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	unsigned long cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	return __rb_time_read(t, ret, &cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) static inline unsigned long rb_time_val_cnt(unsigned long val, unsigned long cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	return (val & RB_TIME_VAL_MASK) | ((cnt & 3) << RB_TIME_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) static inline void rb_time_split(u64 val, unsigned long *top, unsigned long *bottom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	*top = (unsigned long)((val >> RB_TIME_SHIFT) & RB_TIME_VAL_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	*bottom = (unsigned long)(val & RB_TIME_VAL_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) static inline void rb_time_val_set(local_t *t, unsigned long val, unsigned long cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	val = rb_time_val_cnt(val, cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	local_set(t, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) static void rb_time_set(rb_time_t *t, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	unsigned long cnt, top, bottom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	rb_time_split(val, &top, &bottom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	/* Writes always succeed with a valid number even if it gets interrupted. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		cnt = local_inc_return(&t->cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		rb_time_val_set(&t->top, top, cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		rb_time_val_set(&t->bottom, bottom, cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	} while (cnt != local_read(&t->cnt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) static inline bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) rb_time_read_cmpxchg(local_t *l, unsigned long expect, unsigned long set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	unsigned long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	ret = local_cmpxchg(l, expect, set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	return ret == expect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) static int rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	unsigned long cnt, top, bottom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	unsigned long cnt2, top2, bottom2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	/* The cmpxchg always fails if it interrupted an update */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	 if (!__rb_time_read(t, &val, &cnt2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		 return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	 if (val != expect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		 return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	 cnt = local_read(&t->cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	 if ((cnt & 3) != cnt2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		 return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	 cnt2 = cnt + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	 rb_time_split(val, &top, &bottom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	 top = rb_time_val_cnt(top, cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	 bottom = rb_time_val_cnt(bottom, cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	 rb_time_split(set, &top2, &bottom2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	 top2 = rb_time_val_cnt(top2, cnt2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	 bottom2 = rb_time_val_cnt(bottom2, cnt2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	if (!rb_time_read_cmpxchg(&t->cnt, cnt, cnt2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	if (!rb_time_read_cmpxchg(&t->top, top, top2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	if (!rb_time_read_cmpxchg(&t->bottom, bottom, bottom2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) #else /* 64 bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) /* local64_t always succeeds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) static inline bool rb_time_read(rb_time_t *t, u64 *ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	*ret = local64_read(&t->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) static void rb_time_set(rb_time_t *t, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	local64_set(&t->time, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	val = local64_cmpxchg(&t->time, expect, set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	return val == expect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752)  * ring_buffer_nr_pages - get the number of buffer pages in the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753)  * @buffer: The ring_buffer to get the number of pages from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754)  * @cpu: The cpu of the ring_buffer to get the number of pages from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756)  * Returns the number of pages used by a per_cpu buffer of the ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) size_t ring_buffer_nr_pages(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	return buffer->buffers[cpu]->nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764)  * ring_buffer_nr_pages_dirty - get the number of used pages in the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765)  * @buffer: The ring_buffer to get the number of pages from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766)  * @cpu: The cpu of the ring_buffer to get the number of pages from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768)  * Returns the number of pages that have content in the ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	size_t read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	size_t cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	read = local_read(&buffer->buffers[cpu]->pages_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	cnt = local_read(&buffer->buffers[cpu]->pages_touched);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	/* The reader can read an empty page, but not more than that */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	if (cnt < read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		WARN_ON_ONCE(read > cnt + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	return cnt - read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787)  * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789)  * Schedules a delayed work to wake up any task that is blocked on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790)  * ring buffer waiters queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) static void rb_wake_up_waiters(struct irq_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	wake_up_all(&rbwork->waiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	if (rbwork->wakeup_full) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		rbwork->wakeup_full = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		wake_up_all(&rbwork->full_waiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804)  * ring_buffer_wait - wait for input to the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805)  * @buffer: buffer to wait on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806)  * @cpu: the cpu buffer to wait on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807)  * @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809)  * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810)  * as data is added to any of the @buffer's cpu buffers. Otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811)  * it will wait for data to be added to a specific cpu buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	struct rb_irq_work *work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	 * Depending on what the caller is waiting for, either any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	 * data in any cpu buffer, or a specific buffer, put the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	 * caller on the appropriate wait queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	if (cpu == RING_BUFFER_ALL_CPUS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		work = &buffer->irq_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		/* Full only makes sense on per cpu reads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		full = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		work = &cpu_buffer->irq_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		if (full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			prepare_to_wait(&work->full_waiters, &wait, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 			prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		 * The events can happen in critical sections where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		 * checking a work queue can cause deadlocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		 * After adding a task to the queue, this flag is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		 * only to notify events to try to wake up the queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		 * using irq_work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		 * We don't clear it even if the buffer is no longer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		 * empty. The flag only causes the next event to run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		 * irq_work to do the work queue wake up. The worse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		 * that can happen if we race with !trace_empty() is that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		 * an event will cause an irq_work to try to wake up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		 * an empty queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		 * There's no reason to protect this flag either, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		 * the work queue and irq_work logic will do the necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		 * synchronization for the wake ups. The only thing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		 * that is necessary is that the wake up happens after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		 * a task has been queued. It's OK for spurious wake ups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		if (full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 			work->full_waiters_pending = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 			work->waiters_pending = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 			ret = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		if (cpu != RING_BUFFER_ALL_CPUS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		    !ring_buffer_empty_cpu(buffer, cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 			unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 			bool pagebusy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 			size_t nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			size_t dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			if (!full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 			raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 			pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			nr_pages = cpu_buffer->nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 			dirty = ring_buffer_nr_dirty_pages(buffer, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 			if (!cpu_buffer->shortest_full ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 			    cpu_buffer->shortest_full < full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 				cpu_buffer->shortest_full = full;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 			if (!pagebusy &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 			    (!nr_pages || (dirty * 100) > full * nr_pages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	if (full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		finish_wait(&work->full_waiters, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		finish_wait(&work->waiters, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911)  * ring_buffer_poll_wait - poll on buffer input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912)  * @buffer: buffer to wait on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913)  * @cpu: the cpu buffer to wait on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914)  * @filp: the file descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915)  * @poll_table: The poll descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917)  * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918)  * as data is added to any of the @buffer's cpu buffers. Otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919)  * it will wait for data to be added to a specific cpu buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921)  * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922)  * zero otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) __poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 			  struct file *filp, poll_table *poll_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	struct rb_irq_work *work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	if (cpu == RING_BUFFER_ALL_CPUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		work = &buffer->irq_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		work = &cpu_buffer->irq_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	poll_wait(filp, &work->waiters, poll_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	work->waiters_pending = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	 * There's a tight race between setting the waiters_pending and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	 * checking if the ring buffer is empty.  Once the waiters_pending bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	 * is set, the next event will wake the task up, but we can get stuck
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	 * if there's only a single event in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	 * FIXME: Ideally, we need a memory barrier on the writer side as well,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	 * but adding a memory barrier to all events will cause too much of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	 * performance hit in the fast path.  We only need a memory barrier when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	 * the buffer goes from empty to having content.  But as this race is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	 * extremely small, and it's not a problem if another event comes in, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	 * will fix it later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	    (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		return EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) /* buffer may be either ring_buffer or ring_buffer_per_cpu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) #define RB_WARN_ON(b, cond)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	({								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		int _____ret = unlikely(cond);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		if (_____ret) {						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 			if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 				struct ring_buffer_per_cpu *__b =	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 					(void *)b;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 				atomic_inc(&__b->buffer->record_disabled); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 			} else						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 				atomic_inc(&b->record_disabled);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			WARN_ON(1);					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		}							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		_____ret;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) /* Up this if you want to test the TIME_EXTENTS and normalization */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) #define DEBUG_SHIFT 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) static inline u64 rb_time_stamp(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	u64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	/* Skip retpolines :-( */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	if (IS_ENABLED(CONFIG_RETPOLINE) && likely(buffer->clock == trace_clock_local))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		ts = trace_clock_local();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		ts = buffer->clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	/* shift to debug/test normalization and TIME_EXTENTS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	return ts << DEBUG_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) u64 ring_buffer_time_stamp(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	u64 time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	preempt_disable_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	time = rb_time_stamp(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	preempt_enable_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	return time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 				      int cpu, u64 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	/* Just stupid testing the normalize function and deltas */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	*ts >>= DEBUG_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)  * Making the ring buffer lockless makes things tricky.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)  * Although writes only happen on the CPU that they are on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)  * and they only need to worry about interrupts. Reads can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)  * happen on any CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)  * The reader page is always off the ring buffer, but when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)  * reader finishes with a page, it needs to swap its page with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)  * a new one from the buffer. The reader needs to take from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)  * the head (writes go to the tail). But if a writer is in overwrite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)  * mode and wraps, it must push the head page forward.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)  * Here lies the problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)  * The reader must be careful to replace only the head page, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)  * not another one. As described at the top of the file in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)  * ASCII art, the reader sets its old page to point to the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)  * page after head. It then sets the page after head to point to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)  * the old reader page. But if the writer moves the head page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)  * during this operation, the reader could end up with the tail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)  * We use cmpxchg to help prevent this race. We also do something
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)  * special with the page before head. We set the LSB to 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)  * When the writer must push the page forward, it will clear the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)  * bit that points to the head page, move the head, and then set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)  * the bit that points to the new head page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)  * We also don't want an interrupt coming in and moving the head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)  * page on another writer. Thus we use the second LSB to catch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)  * that too. Thus:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)  * head->list->prev->next        bit 1          bit 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)  *                              -------        -------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)  * Normal page                     0              0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)  * Points to head page             0              1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)  * New head page                   1              0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)  * Note we can not trust the prev pointer of the head page, because:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)  * +----+       +-----+        +-----+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)  * |    |------>|  T  |---X--->|  N  |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)  * |    |<------|     |        |     |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)  * +----+       +-----+        +-----+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060)  *   ^                           ^ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)  *   |          +-----+          | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)  *   +----------|  R  |----------+ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)  *              |     |<-----------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)  *              +-----+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)  * Key:  ---X-->  HEAD flag set in pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)  *         T      Tail page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068)  *         R      Reader page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)  *         N      Next page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)  * (see __rb_reserve_next() to see where this happens)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)  *  What the above shows is that the reader just swapped out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)  *  the reader page with a page in the buffer, but before it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)  *  could make the new header point back to the new page added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)  *  it was preempted by a writer. The writer moved forward onto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)  *  the new page added by the reader and is about to move forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)  *  again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)  *  You can see, it is legitimate for the previous pointer of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081)  *  the head (or any page) not to point back to itself. But only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)  *  temporarily.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) #define RB_PAGE_NORMAL		0UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) #define RB_PAGE_HEAD		1UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) #define RB_PAGE_UPDATE		2UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) #define RB_FLAG_MASK		3UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) /* PAGE_MOVED is not part of the mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) #define RB_PAGE_MOVED		4UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)  * rb_list_head - remove any bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) static struct list_head *rb_list_head(struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	unsigned long val = (unsigned long)list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	return (struct list_head *)(val & ~RB_FLAG_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)  * rb_is_head_page - test if the given page is the head page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)  * Because the reader may move the head_page pointer, we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)  * not trust what the head page is (it may be pointing to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)  * the reader page). But if the next page is a header page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111)  * its flags will be non zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		struct buffer_page *page, struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	val = (unsigned long)list->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		return RB_PAGE_MOVED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	return val & RB_FLAG_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)  * rb_is_reader_page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)  * The unique thing about the reader page, is that, if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131)  * writer is ever on it, the previous pointer never points
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132)  * back to the reader page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) static bool rb_is_reader_page(struct buffer_page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	struct list_head *list = page->list.prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	return rb_list_head(list->next) != &page->list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)  * rb_set_list_to_head - set a list_head to be pointing to head.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 				struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	unsigned long *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	ptr = (unsigned long *)&list->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	*ptr |= RB_PAGE_HEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	*ptr &= ~RB_PAGE_UPDATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)  * rb_head_page_activate - sets up head page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	struct buffer_page *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	head = cpu_buffer->head_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	if (!head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	 * Set the previous list pointer to have the HEAD flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	rb_set_list_to_head(cpu_buffer, head->list.prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) static void rb_list_head_clear(struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	unsigned long *ptr = (unsigned long *)&list->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	*ptr &= ~RB_FLAG_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)  * rb_head_page_deactivate - clears head page ptr (for free list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	struct list_head *hd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	/* Go through the whole list and clear any pointers found. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	rb_list_head_clear(cpu_buffer->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	list_for_each(hd, cpu_buffer->pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		rb_list_head_clear(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 			    struct buffer_page *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 			    struct buffer_page *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			    int old_flag, int new_flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	struct list_head *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	unsigned long val = (unsigned long)&head->list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	unsigned long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	list = &prev->list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	val &= ~RB_FLAG_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	ret = cmpxchg((unsigned long *)&list->next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		      val | old_flag, val | new_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	/* check if the reader took the page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	if ((ret & ~RB_FLAG_MASK) != val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		return RB_PAGE_MOVED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	return ret & RB_FLAG_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 				   struct buffer_page *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 				   struct buffer_page *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 				   int old_flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	return rb_head_page_set(cpu_buffer, head, prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 				old_flag, RB_PAGE_UPDATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 				 struct buffer_page *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 				 struct buffer_page *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 				 int old_flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	return rb_head_page_set(cpu_buffer, head, prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 				old_flag, RB_PAGE_HEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 				   struct buffer_page *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 				   struct buffer_page *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 				   int old_flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	return rb_head_page_set(cpu_buffer, head, prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 				old_flag, RB_PAGE_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 			       struct buffer_page **bpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	struct list_head *p = rb_list_head((*bpage)->list.next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	*bpage = list_entry(p, struct buffer_page, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) static struct buffer_page *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	struct buffer_page *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	struct buffer_page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	struct list_head *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	/* sanity check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	list = cpu_buffer->pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	page = head = cpu_buffer->head_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	 * It is possible that the writer moves the header behind
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	 * where we started, and we miss in one loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	 * A second loop should grab the header, but we'll do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	 * three loops just because I'm paranoid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	for (i = 0; i < 3; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 			if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 				cpu_buffer->head_page = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 				return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 			rb_inc_page(cpu_buffer, &page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		} while (page != head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	RB_WARN_ON(cpu_buffer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) static int rb_head_page_replace(struct buffer_page *old,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 				struct buffer_page *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	unsigned long *ptr = (unsigned long *)&old->list.prev->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	unsigned long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	val = *ptr & ~RB_FLAG_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	val |= RB_PAGE_HEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	ret = cmpxchg(ptr, val, (unsigned long)&new->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	return ret == val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305)  * rb_tail_page_update - move the tail page forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 			       struct buffer_page *tail_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 			       struct buffer_page *next_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	unsigned long old_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	unsigned long old_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	 * The tail page now needs to be moved forward.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	 * We need to reset the tail page, but without messing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	 * with possible erasing of data brought in by interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	 * that have moved the tail page and are currently on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	 * We add a counter to the write field to denote this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	local_inc(&cpu_buffer->pages_touched);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	 * Just make sure we have seen our old_write and synchronize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	 * with any interrupts that come in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	 * If the tail page is still the same as what we think
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	 * it is, then it is up to us to update the tail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	 * pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	if (tail_page == READ_ONCE(cpu_buffer->tail_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		/* Zero the write counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		unsigned long val = old_write & ~RB_WRITE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		unsigned long eval = old_entries & ~RB_WRITE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		 * This will only succeed if an interrupt did
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		 * not come in and change it. In which case, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		 * do not want to modify it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		 * We add (void) to let the compiler know that we do not care
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		 * about the return value of these functions. We use the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		 * cmpxchg to only update if an interrupt did not already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		 * do it for us. If the cmpxchg fails, we don't care.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		(void)local_cmpxchg(&next_page->write, old_write, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		(void)local_cmpxchg(&next_page->entries, old_entries, eval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		 * No need to worry about races with clearing out the commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		 * it only can increment when a commit takes place. But that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 		 * only happens in the outer most nested commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 		local_set(&next_page->page->commit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		/* Again, either we update tail_page or an interrupt does */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		(void)cmpxchg(&cpu_buffer->tail_page, tail_page, next_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			  struct buffer_page *bpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	unsigned long val = (unsigned long)bpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380)  * rb_check_list - make sure a pointer to a list has the last bits zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 			 struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)  * rb_check_pages - integrity check of buffer pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394)  * @cpu_buffer: CPU buffer with pages to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)  * As a safety measure we check to make sure the data pages have not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)  * been corrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	struct list_head *head = cpu_buffer->pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	struct buffer_page *bpage, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	/* Reset the head page if it exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	if (cpu_buffer->head_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 		rb_set_head_page(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	rb_head_page_deactivate(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	if (rb_check_list(cpu_buffer, head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	list_for_each_entry_safe(bpage, tmp, head, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		if (RB_WARN_ON(cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 			       bpage->list.next->prev != &bpage->list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		if (RB_WARN_ON(cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 			       bpage->list.prev->next != &bpage->list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		if (rb_check_list(cpu_buffer, &bpage->list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	rb_head_page_activate(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) static int __rb_allocate_pages(long nr_pages, struct list_head *pages, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	struct buffer_page *bpage, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	bool user_thread = current->mm != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	gfp_t mflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	 * Check if the available memory is there first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	 * Note, si_mem_available() only gives us a rough estimate of available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	 * memory. It may not be accurate. But we don't care, we just want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	 * to prevent doing any allocation when it is obvious that it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	 * not going to succeed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	i = si_mem_available();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	if (i < nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	 * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	 * gracefully without invoking oom-killer and the system is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	 * destabilized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	 * If a user thread allocates too much, and si_mem_available()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	 * reports there's enough memory, even though there is not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	 * Make sure the OOM killer kills this thread. This can happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	 * even with RETRY_MAYFAIL because another task may be doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	 * an allocation after this task has taken all memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	 * This is the task the OOM killer needs to take out during this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	 * loop, even if it was triggered by an allocation somewhere else.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	if (user_thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		set_current_oom_origin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 				    mflags, cpu_to_node(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		if (!bpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 			goto free_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		list_add(&bpage->list, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		page = alloc_pages_node(cpu_to_node(cpu), mflags, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 			goto free_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		bpage->page = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 		rb_init_page(bpage->page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		if (user_thread && fatal_signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 			goto free_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	if (user_thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 		clear_current_oom_origin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) free_pages:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	list_for_each_entry_safe(bpage, tmp, pages, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		list_del_init(&bpage->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		free_buffer_page(bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	if (user_thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		clear_current_oom_origin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 			     unsigned long nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	LIST_HEAD(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	WARN_ON(!nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	 * The ring buffer page list is a circular list that does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	 * start and end with a list head. All page list items point to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	 * other pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	cpu_buffer->pages = pages.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	list_del(&pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	cpu_buffer->nr_pages = nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	rb_check_pages(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) static struct ring_buffer_per_cpu *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	struct buffer_page *bpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 				  GFP_KERNEL, cpu_to_node(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	if (!cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	cpu_buffer->cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	cpu_buffer->buffer = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	raw_spin_lock_init(&cpu_buffer->reader_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	init_completion(&cpu_buffer->update_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	init_waitqueue_head(&cpu_buffer->irq_work.waiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 			    GFP_KERNEL, cpu_to_node(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	if (!bpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		goto fail_free_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	rb_check_bpage(cpu_buffer, bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	cpu_buffer->reader_page = bpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		goto fail_free_reader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	bpage->page = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	rb_init_page(bpage->page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	INIT_LIST_HEAD(&cpu_buffer->new_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	ret = rb_allocate_pages(cpu_buffer, nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		goto fail_free_reader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	cpu_buffer->head_page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		= list_entry(cpu_buffer->pages, struct buffer_page, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	rb_head_page_activate(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	return cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583)  fail_free_reader:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	free_buffer_page(cpu_buffer->reader_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586)  fail_free_buffer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	kfree(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	struct list_head *head = cpu_buffer->pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	struct buffer_page *bpage, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	free_buffer_page(cpu_buffer->reader_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	rb_head_page_deactivate(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	if (head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		list_for_each_entry_safe(bpage, tmp, head, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 			list_del_init(&bpage->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 			free_buffer_page(bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		bpage = list_entry(head, struct buffer_page, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 		free_buffer_page(bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	kfree(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613)  * __ring_buffer_alloc - allocate a new ring_buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614)  * @size: the size in bytes per cpu that is needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)  * @flags: attributes to set for the ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616)  * @key: ring buffer reader_lock_key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618)  * Currently the only flag that is available is the RB_FL_OVERWRITE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619)  * flag. This flag means that the buffer will overwrite old data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620)  * when the buffer wraps. If this flag is not set, the buffer will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621)  * drop data when the tail hits the head.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 					struct lock_class_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	struct trace_buffer *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	long nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	int bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	/* keep it in its own cache line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 			 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		goto fail_free_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	buffer->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	buffer->clock = trace_clock_local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	buffer->reader_lock_key = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	init_waitqueue_head(&buffer->irq_work.waiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	/* need at least two pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	if (nr_pages < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		nr_pages = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	buffer->cpus = nr_cpu_ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	bsize = sizeof(void *) * nr_cpu_ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 				  GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	if (!buffer->buffers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		goto fail_free_cpumask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	cpu = raw_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	cpumask_set_cpu(cpu, buffer->cpumask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	if (!buffer->buffers[cpu])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 		goto fail_free_buffers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		goto fail_free_buffers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	mutex_init(&buffer->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	return buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675)  fail_free_buffers:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	for_each_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		if (buffer->buffers[cpu])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 			rb_free_cpu_buffer(buffer->buffers[cpu]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	kfree(buffer->buffers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682)  fail_free_cpumask:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	free_cpumask_var(buffer->cpumask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685)  fail_free_buffer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692)  * ring_buffer_free - free a ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693)  * @buffer: the buffer to free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) ring_buffer_free(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	for_each_buffer_cpu(buffer, cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		rb_free_cpu_buffer(buffer->buffers[cpu]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	kfree(buffer->buffers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	free_cpumask_var(buffer->cpumask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) EXPORT_SYMBOL_GPL(ring_buffer_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) void ring_buffer_set_clock(struct trace_buffer *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 			   u64 (*clock)(void))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	buffer->clock = clock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	buffer->time_stamp_abs = abs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	return buffer->time_stamp_abs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) static inline unsigned long rb_page_entries(struct buffer_page *bpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	return local_read(&bpage->entries) & RB_WRITE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) static inline unsigned long rb_page_write(struct buffer_page *bpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	return local_read(&bpage->write) & RB_WRITE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	struct list_head *tail_page, *to_remove, *next_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	struct buffer_page *to_remove_page, *tmp_iter_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	struct buffer_page *last_page, *first_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	unsigned long nr_removed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	unsigned long head_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	int page_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	head_bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	raw_spin_lock_irq(&cpu_buffer->reader_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	atomic_inc(&cpu_buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	 * We don't race with the readers since we have acquired the reader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	 * lock. We also don't race with writers after disabling recording.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	 * This makes it easy to figure out the first and the last page to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	 * removed from the list. We unlink all the pages in between including
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	 * the first and last pages. This is done in a busy loop so that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	 * lose the least number of traces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	 * The pages are freed after we restart recording and unlock readers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	tail_page = &cpu_buffer->tail_page->list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	 * tail page might be on reader page, we remove the next page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	 * from the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	if (cpu_buffer->tail_page == cpu_buffer->reader_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 		tail_page = rb_list_head(tail_page->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	to_remove = tail_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	/* start of pages to remove */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	first_page = list_entry(rb_list_head(to_remove->next),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 				struct buffer_page, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 		to_remove = rb_list_head(to_remove)->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 		head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	next_page = rb_list_head(to_remove)->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	 * Now we remove all pages between tail_page and next_page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	 * Make sure that we have head_bit value preserved for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	 * next page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	tail_page->next = (struct list_head *)((unsigned long)next_page |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 						head_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	next_page = rb_list_head(next_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	next_page->prev = tail_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	/* make sure pages points to a valid page in the ring buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	cpu_buffer->pages = next_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	/* update head page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	if (head_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 		cpu_buffer->head_page = list_entry(next_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 						struct buffer_page, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	 * change read pointer to make sure any read iterators reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	 * themselves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	cpu_buffer->read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	/* pages are removed, resume tracing and then free the pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	atomic_dec(&cpu_buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	raw_spin_unlock_irq(&cpu_buffer->reader_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	/* last buffer page to remove */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 				list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	tmp_iter_page = first_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 		to_remove_page = tmp_iter_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 		rb_inc_page(cpu_buffer, &tmp_iter_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		/* update the counters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 		page_entries = rb_page_entries(to_remove_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 		if (page_entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 			 * If something was added to this page, it was full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 			 * since it is not the tail page. So we deduct the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 			 * bytes consumed in ring buffer from here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 			 * Increment overrun to account for the lost events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 			local_add(page_entries, &cpu_buffer->overrun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 			local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 		 * We have already removed references to this list item, just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		 * free up the buffer_page and its page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		free_buffer_page(to_remove_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 		nr_removed--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	} while (to_remove_page != last_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	RB_WARN_ON(cpu_buffer, nr_removed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	return nr_removed == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	struct list_head *pages = &cpu_buffer->new_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	int retries, success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	raw_spin_lock_irq(&cpu_buffer->reader_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	 * We are holding the reader lock, so the reader page won't be swapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	 * in the ring buffer. Now we are racing with the writer trying to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	 * move head page and the tail page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	 * We are going to adapt the reader page update process where:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	 * 1. We first splice the start and end of list of new pages between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	 *    the head page and its previous page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	 * 2. We cmpxchg the prev_page->next to point from head page to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	 *    start of new pages list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	 * 3. Finally, we update the head->prev to the end of new list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	 * We will try this process 10 times, to make sure that we don't keep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	 * spinning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	retries = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	success = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 	while (retries--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 		struct list_head *head_page, *prev_page, *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 		struct list_head *last_page, *first_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 		struct list_head *head_page_with_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 		head_page = &rb_set_head_page(cpu_buffer)->list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 		if (!head_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 		prev_page = head_page->prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 		first_page = pages->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 		last_page  = pages->prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		head_page_with_bit = (struct list_head *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 				     ((unsigned long)head_page | RB_PAGE_HEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 		last_page->next = head_page_with_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 		first_page->prev = prev_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 		r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 		if (r == head_page_with_bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 			 * yay, we replaced the page pointer to our new list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 			 * now, we just have to update to head page's prev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 			 * pointer to point to end of list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 			head_page->prev = last_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 			success = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	if (success)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 		INIT_LIST_HEAD(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	 * If we weren't successful in adding in new pages, warn and stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	 * tracing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	RB_WARN_ON(cpu_buffer, !success);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	raw_spin_unlock_irq(&cpu_buffer->reader_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	/* free pages if they weren't inserted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	if (!success) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 		struct buffer_page *bpage, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 		list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 					 list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 			list_del_init(&bpage->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 			free_buffer_page(bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	return success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	int success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	if (cpu_buffer->nr_pages_to_update > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 		success = rb_insert_pages(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 		success = rb_remove_pages(cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 					-cpu_buffer->nr_pages_to_update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	if (success)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) static void update_pages_handler(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 			struct ring_buffer_per_cpu, update_pages_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	rb_update_pages(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	complete(&cpu_buffer->update_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952)  * ring_buffer_resize - resize the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953)  * @buffer: the buffer to resize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954)  * @size: the new size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955)  * @cpu_id: the cpu buffer to resize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957)  * Minimum size is 2 * BUF_PAGE_SIZE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959)  * Returns 0 on success and < 0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 			int cpu_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	unsigned long nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	int cpu, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	 * Always succeed at resizing a non-existent buffer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	/* Make sure the requested buffer exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	if (cpu_id != RING_BUFFER_ALL_CPUS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	    !cpumask_test_cpu(cpu_id, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	/* we need a minimum of two pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	if (nr_pages < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 		nr_pages = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	size = nr_pages * BUF_PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 	/* prevent another thread from changing buffer sizes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	mutex_lock(&buffer->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	if (cpu_id == RING_BUFFER_ALL_CPUS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		 * Don't succeed if resizing is disabled, as a reader might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 		 * manipulating the ring buffer and is expecting a sane state while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 		 * this is true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 		for_each_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 			cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 			if (atomic_read(&cpu_buffer->resize_disabled)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 				err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 				goto out_err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		/* calculate the pages to update */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 		for_each_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 			cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 			cpu_buffer->nr_pages_to_update = nr_pages -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 							cpu_buffer->nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 			 * nothing more to do for removing pages or no update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 			if (cpu_buffer->nr_pages_to_update <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 			 * to add pages, make sure all new pages can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 			 * allocated without receiving ENOMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 			INIT_LIST_HEAD(&cpu_buffer->new_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 			if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 						&cpu_buffer->new_pages, cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 				/* not enough memory for new pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 				err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 				goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 		get_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 		 * Fire off all the required work handlers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 		 * We can't schedule on offline CPUs, but it's not necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		 * since we can change their buffer sizes without any race.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		for_each_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 			cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 			if (!cpu_buffer->nr_pages_to_update)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 			/* Can't run something on an offline CPU. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 			if (!cpu_online(cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 				rb_update_pages(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 				cpu_buffer->nr_pages_to_update = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 				schedule_work_on(cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 						&cpu_buffer->update_pages_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 		/* wait for all the updates to complete */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 		for_each_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 			cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 			if (!cpu_buffer->nr_pages_to_update)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 			if (cpu_online(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 				wait_for_completion(&cpu_buffer->update_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 			cpu_buffer->nr_pages_to_update = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 		put_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 		/* Make sure this CPU has been initialized */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 		if (!cpumask_test_cpu(cpu_id, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 		cpu_buffer = buffer->buffers[cpu_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 		if (nr_pages == cpu_buffer->nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 		 * Don't succeed if resizing is disabled, as a reader might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 		 * manipulating the ring buffer and is expecting a sane state while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 		 * this is true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 		if (atomic_read(&cpu_buffer->resize_disabled)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 			err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 			goto out_err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 		cpu_buffer->nr_pages_to_update = nr_pages -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 						cpu_buffer->nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 		INIT_LIST_HEAD(&cpu_buffer->new_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 		if (cpu_buffer->nr_pages_to_update > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 			__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 					    &cpu_buffer->new_pages, cpu_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 		get_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 		/* Can't run something on an offline CPU. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 		if (!cpu_online(cpu_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 			rb_update_pages(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 			schedule_work_on(cpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 					 &cpu_buffer->update_pages_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 			wait_for_completion(&cpu_buffer->update_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 		cpu_buffer->nr_pages_to_update = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 		put_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	 * The ring buffer resize can happen with the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	 * enabled, so that the update disturbs the tracing as little
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	 * as possible. But if the buffer is disabled, we do not need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	 * to worry about that, and we can take the time to verify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	 * that the buffer is not corrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	if (atomic_read(&buffer->record_disabled)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 		atomic_inc(&buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 		 * Even though the buffer was disabled, we must make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 		 * that it is truly disabled before calling rb_check_pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 		 * There could have been a race between checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 		 * record_disable and incrementing it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 		synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 		for_each_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 			cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 			rb_check_pages(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 		atomic_dec(&buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	mutex_unlock(&buffer->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135)  out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	for_each_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 		struct buffer_page *bpage, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 		cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 		cpu_buffer->nr_pages_to_update = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 		if (list_empty(&cpu_buffer->new_pages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 		list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 					list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 			list_del_init(&bpage->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 			free_buffer_page(bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151)  out_err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	mutex_unlock(&buffer->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) EXPORT_SYMBOL_GPL(ring_buffer_resize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	mutex_lock(&buffer->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 	if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 		buffer->flags |= RB_FL_OVERWRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 		buffer->flags &= ~RB_FL_OVERWRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 	mutex_unlock(&buffer->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) static __always_inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	return bpage->page->data + index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) static __always_inline struct ring_buffer_event *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	return __rb_page_index(cpu_buffer->reader_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 			       cpu_buffer->reader_page->read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) static __always_inline unsigned rb_page_commit(struct buffer_page *bpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	return local_read(&bpage->page->commit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) static struct ring_buffer_event *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) rb_iter_head_event(struct ring_buffer_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 	struct buffer_page *iter_head_page = iter->head_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	unsigned long commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	unsigned length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	if (iter->head != iter->next_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 		return iter->event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	 * When the writer goes across pages, it issues a cmpxchg which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 	 * is a mb(), which will synchronize with the rmb here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 	 * (see rb_tail_page_update() and __rb_reserve_next())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 	commit = rb_page_commit(iter_head_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	event = __rb_page_index(iter_head_page, iter->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 	length = rb_event_length(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	 * READ_ONCE() doesn't work on functions and we don't want the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	 * compiler doing any crazy optimizations with length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	if ((iter->head + length) > commit || length > BUF_MAX_DATA_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 		/* Writer corrupted the read? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 		goto reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	memcpy(iter->event, event, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 	 * If the page stamp is still the same after this rmb() then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	 * event was safely copied without the writer entering the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 	smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	/* Make sure the page didn't change since we read this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	if (iter->page_stamp != iter_head_page->page->time_stamp ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	    commit > rb_page_commit(iter_head_page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 		goto reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	iter->next_event = iter->head + length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	return iter->event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230)  reset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 	/* Reset to the beginning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	iter->head = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	iter->next_event = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	iter->missed_events = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) /* Size is determined by what has been committed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) static __always_inline unsigned rb_page_size(struct buffer_page *bpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 	return rb_page_commit(bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) static __always_inline unsigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 	return rb_page_commit(cpu_buffer->commit_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) static __always_inline unsigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) rb_event_index(struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	unsigned long addr = (unsigned long)event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) static void rb_inc_iter(struct ring_buffer_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 	struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 	 * The iterator could be on the reader page (it starts there).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 	 * But the head could have moved, since the reader was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 	 * found. Check for this case and assign the iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 	 * to the head page instead of next.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 	if (iter->head_page == cpu_buffer->reader_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 		iter->head_page = rb_set_head_page(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 		rb_inc_page(cpu_buffer, &iter->head_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 	iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 	iter->head = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 	iter->next_event = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280)  * rb_handle_head_page - writer hit the head page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282)  * Returns: +1 to retry page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283)  *           0 to continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284)  *          -1 on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 		    struct buffer_page *tail_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 		    struct buffer_page *next_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 	struct buffer_page *new_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 	int entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 	int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 	entries = rb_page_entries(next_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	 * The hard part is here. We need to move the head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 	 * forward, and protect against both readers on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 	 * other CPUs and writers coming in via interrupts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 	type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 				       RB_PAGE_HEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 	 * type can be one of four:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 	 *  NORMAL - an interrupt already moved it for us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	 *  HEAD   - we are the first to get here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	 *  UPDATE - we are the interrupt interrupting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	 *           a current move.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 	 *  MOVED  - a reader on another CPU moved the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 	 *           pointer to its reader page. Give up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 	 *           and try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	case RB_PAGE_HEAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 		 * We changed the head to UPDATE, thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 		 * it is our responsibility to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 		 * the counters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 		local_add(entries, &cpu_buffer->overrun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 		local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 		 * The entries will be zeroed out when we move the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 		 * tail page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 		/* still more to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	case RB_PAGE_UPDATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 		 * This is an interrupt that interrupt the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 		 * previous update. Still more to do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	case RB_PAGE_NORMAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 		 * An interrupt came in before the update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 		 * and processed this for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 		 * Nothing left to do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	case RB_PAGE_MOVED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 		 * The reader is on another CPU and just did
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 		 * a swap with our next_page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 		 * Try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 		RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	 * Now that we are here, the old head pointer is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	 * set to UPDATE. This will keep the reader from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 	 * swapping the head page with the reader page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	 * The reader (on another CPU) will spin till
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	 * we are finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 	 * We just need to protect against interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 	 * doing the job. We will set the next pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	 * to HEAD. After that, we set the old pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 	 * to NORMAL, but only if it was HEAD before.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 	 * otherwise we are an interrupt, and only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 	 * want the outer most commit to reset it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 	new_head = next_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 	rb_inc_page(cpu_buffer, &new_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 				    RB_PAGE_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	 * Valid returns are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 	 *  HEAD   - an interrupt came in and already set it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 	 *  NORMAL - One of two things:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	 *            1) We really set it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	 *            2) A bunch of interrupts came in and moved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	 *               the page forward again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 	case RB_PAGE_HEAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 	case RB_PAGE_NORMAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 		/* OK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 		RB_WARN_ON(cpu_buffer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 	 * It is possible that an interrupt came in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 	 * set the head up, then more interrupts came in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 	 * and moved it again. When we get back here,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 	 * the page would have been set to NORMAL but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 	 * just set it back to HEAD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	 * How do you detect this? Well, if that happened
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 	 * the tail page would have moved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	if (ret == RB_PAGE_NORMAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 		struct buffer_page *buffer_tail_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 		buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 		 * If the tail had moved passed next, then we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 		 * to reset the pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 		if (buffer_tail_page != tail_page &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 		    buffer_tail_page != next_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 			rb_head_page_set_normal(cpu_buffer, new_head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 						next_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 						RB_PAGE_HEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 	 * If this was the outer most commit (the one that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	 * changed the original pointer from HEAD to UPDATE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 	 * then it is up to us to reset it to NORMAL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 	if (type == RB_PAGE_HEAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 		ret = rb_head_page_set_normal(cpu_buffer, next_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 					      tail_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 					      RB_PAGE_UPDATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 		if (RB_WARN_ON(cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 			       ret != RB_PAGE_UPDATE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 	      unsigned long tail, struct rb_event_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	struct buffer_page *tail_page = info->tail_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 	struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 	unsigned long length = info->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 	 * Only the event that crossed the page boundary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 	 * must fill the old tail_page with padding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 	if (tail >= BUF_PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 		 * If the page was filled, then we still need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 		 * to update the real_end. Reset it to zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 		 * and the reader will ignore it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 		if (tail == BUF_PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 			tail_page->real_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 		local_sub(length, &tail_page->write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	event = __rb_page_index(tail_page, tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 	/* account for padding bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 	local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	 * Save the original length to the meta data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	 * This will be used by the reader to add lost event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	 * counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	tail_page->real_end = tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	 * If this event is bigger than the minimum size, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	 * we need to be careful that we don't subtract the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	 * write counter enough to allow another writer to slip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 	 * in on this page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 	 * We put in a discarded commit instead, to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	 * that this space is not used again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	 * If we are less than the minimum size, we don't need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	 * worry about it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 		/* No room for any events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 		/* Mark the rest of the page with padding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 		rb_event_set_padding(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 		/* Set the write back to the previous setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 		local_sub(length, &tail_page->write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 	/* Put in a discarded event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 	event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 	event->type_len = RINGBUF_TYPE_PADDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 	/* time delta must be non zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	event->time_delta = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 	/* Set write to end of buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 	length = (tail + length) - BUF_PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 	local_sub(length, &tail_page->write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513)  * This is the slow path, force gcc not to inline it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) static noinline struct ring_buffer_event *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	     unsigned long tail, struct rb_event_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 	struct buffer_page *tail_page = info->tail_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 	struct buffer_page *commit_page = cpu_buffer->commit_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 	struct trace_buffer *buffer = cpu_buffer->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	struct buffer_page *next_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 	next_page = tail_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	rb_inc_page(cpu_buffer, &next_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	 * If for some reason, we had an interrupt storm that made
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	 * it all the way around the buffer, bail, and warn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	 * about it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	if (unlikely(next_page == commit_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 		local_inc(&cpu_buffer->commit_overrun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 		goto out_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 	 * This is where the fun begins!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 	 * We are fighting against races between a reader that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 	 * could be on another CPU trying to swap its reader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 	 * page with the buffer head.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 	 * We are also fighting against interrupts coming in and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 	 * moving the head or tail on us as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	 * If the next page is the head page then we have filled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	 * the buffer, unless the commit page is still on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 	 * reader page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 	if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 		 * If the commit is not on the reader page, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 		 * move the header page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 		if (!rb_is_reader_page(cpu_buffer->commit_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 			 * If we are not in overwrite mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 			 * this is easy, just stop here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 			if (!(buffer->flags & RB_FL_OVERWRITE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 				local_inc(&cpu_buffer->dropped_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 				goto out_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 			ret = rb_handle_head_page(cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 						  tail_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 						  next_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 				goto out_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 				goto out_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 			 * We need to be careful here too. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 			 * commit page could still be on the reader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 			 * page. We could have a small buffer, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 			 * have filled up the buffer with events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 			 * from interrupts and such, and wrapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 			 * Note, if the tail page is also the on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 			 * reader_page, we let it move out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 			if (unlikely((cpu_buffer->commit_page !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 				      cpu_buffer->tail_page) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 				     (cpu_buffer->commit_page ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 				      cpu_buffer->reader_page))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 				local_inc(&cpu_buffer->commit_overrun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 				goto out_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	rb_tail_page_update(cpu_buffer, tail_page, next_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599)  out_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	rb_reset_tail(cpu_buffer, tail, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 	/* Commit what we have for now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 	rb_end_commit(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 	/* rb_end_commit() decs committing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 	local_inc(&cpu_buffer->committing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	/* fail and let the caller try again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 	return ERR_PTR(-EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611)  out_reset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 	/* reset write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 	rb_reset_tail(cpu_buffer, tail, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) /* Slow path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) static struct ring_buffer_event *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) rb_add_time_stamp(struct ring_buffer_event *event, u64 delta, bool abs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 	if (abs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 		event->type_len = RINGBUF_TYPE_TIME_STAMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 		event->type_len = RINGBUF_TYPE_TIME_EXTEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	/* Not the first event on the page, or not delta? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	if (abs || rb_event_index(event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 		event->time_delta = delta & TS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 		event->array[0] = delta >> TS_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 		/* nope, just zero it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 		event->time_delta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 		event->array[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 	return skip_time_extend(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) static inline bool rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 				     struct ring_buffer_event *event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) static inline bool sched_clock_stable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 		   struct rb_event_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 	u64 write_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 	WARN_ONCE(1, "Delta way too big! %llu ts=%llu before=%llu after=%llu write stamp=%llu\n%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 		  (unsigned long long)info->delta,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 		  (unsigned long long)info->ts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 		  (unsigned long long)info->before,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 		  (unsigned long long)info->after,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 		  (unsigned long long)(rb_time_read(&cpu_buffer->write_stamp, &write_stamp) ? write_stamp : 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 		  sched_clock_stable() ? "" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 		  "If you just came from a suspend/resume,\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 		  "please switch to the trace global clock:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 		  "  echo global > /sys/kernel/debug/tracing/trace_clock\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 		  "or add trace_clock=global to the kernel command line\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) static void rb_add_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 				      struct ring_buffer_event **event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 				      struct rb_event_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 				      u64 *delta,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 				      unsigned int *length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 	bool abs = info->add_timestamp &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 		(RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 	if (unlikely(info->delta > (1ULL << 59))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 		/* did the clock go backwards */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 		if (info->before == info->after && info->before > info->ts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 			/* not interrupted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 			static int once;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 			 * This is possible with a recalibrating of the TSC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 			 * Do not produce a call stack, but just report it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 			if (!once) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 				once++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 				pr_warn("Ring buffer clock went backwards: %llu -> %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 					info->before, info->ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 			rb_check_timestamp(cpu_buffer, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 		if (!abs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 			info->delta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	*event = rb_add_time_stamp(*event, info->delta, abs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	*length -= RB_LEN_TIME_EXTEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 	*delta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704)  * rb_update_event - update event type and data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705)  * @cpu_buffer: The per cpu buffer of the @event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706)  * @event: the event to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707)  * @info: The info to update the @event with (contains length and delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709)  * Update the type and data fields of the @event. The length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710)  * is the actual size that is written to the ring buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711)  * and with this, we can determine what to place into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712)  * data field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 		struct ring_buffer_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 		struct rb_event_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 	unsigned length = info->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 	u64 delta = info->delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 	 * If we need to add a timestamp, then we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 	 * add it to the start of the reserved space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 	if (unlikely(info->add_timestamp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 		rb_add_timestamp(cpu_buffer, &event, info, &delta, &length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 	event->time_delta = delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	length -= RB_EVNT_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 		event->type_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 		event->array[0] = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 		event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) static unsigned rb_calculate_event_length(unsigned length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	struct ring_buffer_event event; /* Used only for sizeof array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 	/* zero length can cause confusions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 	if (!length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 		length++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 	if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 		length += sizeof(event.array[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 	length += RB_EVNT_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 	length = ALIGN(length, RB_ARCH_ALIGNMENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 	 * In case the time delta is larger than the 27 bits for it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	 * in the header, we need to add a timestamp. If another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 	 * event comes in when trying to discard this one to increase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	 * the length, then the timestamp will be added in the allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 	 * space of this event. If length is bigger than the size needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 	 * for the TIME_EXTEND, then padding has to be used. The events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 	 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 	 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 	 * As length is a multiple of 4, we only need to worry if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 	 * is 12 (RB_LEN_TIME_EXTEND + 4).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 	if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 		length += RB_ALIGNMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 	return length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) static __always_inline bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 		   struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 	unsigned long addr = (unsigned long)event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 	unsigned long index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 	index = rb_event_index(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 	addr &= PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 	return cpu_buffer->commit_page->page == (void *)addr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 		rb_commit_index(cpu_buffer) == index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) static u64 rb_time_delta(struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 	switch (event->type_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 	case RINGBUF_TYPE_PADDING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 	case RINGBUF_TYPE_TIME_EXTEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 		return ring_buffer_event_time_stamp(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 	case RINGBUF_TYPE_TIME_STAMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 	case RINGBUF_TYPE_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 		return event->time_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 		  struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 	unsigned long new_index, old_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 	struct buffer_page *bpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 	unsigned long index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 	unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 	u64 write_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	u64 delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 	new_index = rb_event_index(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	old_index = new_index + rb_event_ts_length(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 	addr = (unsigned long)event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	addr &= PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 	bpage = READ_ONCE(cpu_buffer->tail_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 	delta = rb_time_delta(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 	if (!rb_time_read(&cpu_buffer->write_stamp, &write_stamp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 	/* Make sure the write stamp is read before testing the location */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 	barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 	if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 		unsigned long write_mask =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 			local_read(&bpage->write) & ~RB_WRITE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 		unsigned long event_length = rb_event_length(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 		/* Something came in, can't discard */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 		if (!rb_time_cmpxchg(&cpu_buffer->write_stamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 				       write_stamp, write_stamp - delta))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 		 * It's possible that the event time delta is zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 		 * (has the same time stamp as the previous event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 		 * in which case write_stamp and before_stamp could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 		 * be the same. In such a case, force before_stamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 		 * to be different than write_stamp. It doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 		 * matter what it is, as long as its different.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 		if (!delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 			rb_time_set(&cpu_buffer->before_stamp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 		 * If an event were to come in now, it would see that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 		 * write_stamp and the before_stamp are different, and assume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 		 * that this event just added itself before updating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 		 * the write stamp. The interrupting event will fix the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 		 * write stamp for us, and use the before stamp as its delta.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 		 * This is on the tail page. It is possible that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 		 * a write could come in and move the tail page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 		 * and write to the next page. That is fine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 		 * because we just shorten what is on this page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 		old_index += write_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 		new_index += write_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 		index = local_cmpxchg(&bpage->write, old_index, new_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 		if (index == old_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 			/* update counters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 			local_sub(event_length, &cpu_buffer->entries_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 	/* could not discard */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 	local_inc(&cpu_buffer->committing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 	local_inc(&cpu_buffer->commits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) static __always_inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 	unsigned long max_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 	 * We only race with interrupts and NMIs on this CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 	 * If we own the commit event, then we can commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 	 * all others that interrupted us, since the interruptions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 	 * are in stack format (they finish before they come
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 	 * back to us). This allows us to do a simple loop to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 	 * assign the commit to the tail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897)  again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 	max_count = cpu_buffer->nr_pages * 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 	while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 		if (RB_WARN_ON(cpu_buffer, !(--max_count)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 		if (RB_WARN_ON(cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 			       rb_is_reader_page(cpu_buffer->tail_page)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 		local_set(&cpu_buffer->commit_page->page->commit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 			  rb_page_write(cpu_buffer->commit_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 		rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 		/* add barrier to keep gcc from optimizing too much */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 		barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 	while (rb_commit_index(cpu_buffer) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 	       rb_page_write(cpu_buffer->commit_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 		local_set(&cpu_buffer->commit_page->page->commit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 			  rb_page_write(cpu_buffer->commit_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 		RB_WARN_ON(cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 			   local_read(&cpu_buffer->commit_page->page->commit) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 			   ~RB_WRITE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 		barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 	/* again, keep gcc from optimizing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 	barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 	 * If an interrupt came in just after the first while loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 	 * and pushed the tail page forward, we will be left with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 	 * a dangling commit that will never go forward.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 	if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) static __always_inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 	unsigned long commits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 	if (RB_WARN_ON(cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 		       !local_read(&cpu_buffer->committing)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943)  again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 	commits = local_read(&cpu_buffer->commits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 	/* synchronize with interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 	barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 	if (local_read(&cpu_buffer->committing) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 		rb_set_commit_to_write(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 	local_dec(&cpu_buffer->committing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 	/* synchronize with interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 	barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 	 * Need to account for interrupts coming in between the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 	 * updating of the commit page and the clearing of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 	 * committing counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 	if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 	    !local_read(&cpu_buffer->committing)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 		local_inc(&cpu_buffer->committing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) static inline void rb_event_discard(struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 	if (extended_time(event))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 		event = skip_time_extend(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 	/* array[0] holds the actual length for the discarded event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 	event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 	event->type_len = RINGBUF_TYPE_PADDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 	/* time delta must be non zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 	if (!event->time_delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 		event->time_delta = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 		      struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 	local_inc(&cpu_buffer->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 	rb_end_commit(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) static __always_inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) rb_wakeups(struct trace_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 	size_t nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 	size_t dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 	size_t full;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 	if (buffer->irq_work.waiters_pending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 		buffer->irq_work.waiters_pending = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 		/* irq_work_queue() supplies it's own memory barriers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 		irq_work_queue(&buffer->irq_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 	if (cpu_buffer->irq_work.waiters_pending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 		cpu_buffer->irq_work.waiters_pending = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 		/* irq_work_queue() supplies it's own memory barriers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 		irq_work_queue(&cpu_buffer->irq_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 	if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 	if (cpu_buffer->reader_page == cpu_buffer->commit_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 	if (!cpu_buffer->irq_work.full_waiters_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 	cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 	full = cpu_buffer->shortest_full;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 	nr_pages = cpu_buffer->nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 	dirty = ring_buffer_nr_dirty_pages(buffer, cpu_buffer->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 	if (full && nr_pages && (dirty * 100) <= full * nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 	cpu_buffer->irq_work.wakeup_full = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 	cpu_buffer->irq_work.full_waiters_pending = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 	/* irq_work_queue() supplies it's own memory barriers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 	irq_work_queue(&cpu_buffer->irq_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030)  * The lock and unlock are done within a preempt disable section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031)  * The current_context per_cpu variable can only be modified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032)  * by the current task between lock and unlock. But it can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033)  * be modified more than once via an interrupt. To pass this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034)  * information from the lock to the unlock without having to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035)  * access the 'in_interrupt()' functions again (which do show
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036)  * a bit of overhead in something as critical as function tracing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037)  * we use a bitmask trick.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039)  *  bit 1 =  NMI context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040)  *  bit 2 =  IRQ context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041)  *  bit 3 =  SoftIRQ context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042)  *  bit 4 =  normal context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044)  * This works because this is the order of contexts that can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045)  * preempt other contexts. A SoftIRQ never preempts an IRQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046)  * context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048)  * When the context is determined, the corresponding bit is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049)  * checked and set (if it was set, then a recursion of that context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050)  * happened).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052)  * On unlock, we need to clear this bit. To do so, just subtract
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053)  * 1 from the current_context and AND it to itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055)  * (binary)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056)  *  101 - 1 = 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057)  *  101 & 100 = 100 (clearing bit zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059)  *  1010 - 1 = 1001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060)  *  1010 & 1001 = 1000 (clearing bit 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062)  * The least significant bit can be cleared this way, and it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063)  * just so happens that it is the same bit corresponding to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064)  * the current context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066)  * Now the TRANSITION bit breaks the above slightly. The TRANSITION bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067)  * is set when a recursion is detected at the current context, and if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068)  * the TRANSITION bit is already set, it will fail the recursion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069)  * This is needed because there's a lag between the changing of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070)  * interrupt context and updating the preempt count. In this case,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071)  * a false positive will be found. To handle this, one extra recursion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072)  * is allowed, and this is done by the TRANSITION bit. If the TRANSITION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073)  * bit is already set, then it is considered a recursion and the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074)  * ends. Otherwise, the TRANSITION bit is set, and that bit is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076)  * On the trace_recursive_unlock(), the TRANSITION bit will be the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077)  * to be cleared. Even if it wasn't the context that set it. That is,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078)  * if an interrupt comes in while NORMAL bit is set and the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079)  * is called before preempt_count() is updated, since the check will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080)  * be on the NORMAL bit, the TRANSITION bit will then be set. If an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081)  * NMI then comes in, it will set the NMI bit, but when the NMI code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082)  * does the trace_recursive_unlock() it will clear the TRANSTION bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083)  * and leave the NMI bit set. But this is fine, because the interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084)  * code that set the TRANSITION bit will then clear the NMI bit when it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085)  * calls trace_recursive_unlock(). If another NMI comes in, it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086)  * set the TRANSITION bit and continue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088)  * Note: The TRANSITION bit only handles a single transition between context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) static __always_inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 	unsigned int val = cpu_buffer->current_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 	unsigned long pc = preempt_count();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 	int bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 	if (!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 		bit = RB_CTX_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 		bit = pc & NMI_MASK ? RB_CTX_NMI :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 			pc & HARDIRQ_MASK ? RB_CTX_IRQ : RB_CTX_SOFTIRQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 	if (unlikely(val & (1 << (bit + cpu_buffer->nest)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 		 * It is possible that this was called by transitioning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 		 * between interrupt context, and preempt_count() has not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 		 * been updated yet. In this case, use the TRANSITION bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 		bit = RB_CTX_TRANSITION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 		if (val & (1 << (bit + cpu_buffer->nest)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 	val |= (1 << (bit + cpu_buffer->nest));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 	cpu_buffer->current_context = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) static __always_inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) 	cpu_buffer->current_context &=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 		cpu_buffer->current_context - (1 << cpu_buffer->nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) /* The recursive locking above uses 5 bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) #define NESTED_BITS 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132)  * ring_buffer_nest_start - Allow to trace while nested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133)  * @buffer: The ring buffer to modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135)  * The ring buffer has a safety mechanism to prevent recursion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136)  * But there may be a case where a trace needs to be done while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137)  * tracing something else. In this case, calling this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138)  * will allow this function to nest within a currently active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139)  * ring_buffer_lock_reserve().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141)  * Call this function before calling another ring_buffer_lock_reserve() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142)  * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) void ring_buffer_nest_start(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 	/* Enabled by ring_buffer_nest_end() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 	preempt_disable_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 	cpu = raw_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 	/* This is the shift value for the above recursive locking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 	cpu_buffer->nest += NESTED_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158)  * ring_buffer_nest_end - Allow to trace while nested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159)  * @buffer: The ring buffer to modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161)  * Must be called after ring_buffer_nest_start() and after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162)  * ring_buffer_unlock_commit().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) void ring_buffer_nest_end(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 	/* disabled by ring_buffer_nest_start() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 	cpu = raw_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) 	/* This is the shift value for the above recursive locking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) 	cpu_buffer->nest -= NESTED_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) 	preempt_enable_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178)  * ring_buffer_unlock_commit - commit a reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179)  * @buffer: The buffer to commit to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180)  * @event: The event pointer to commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182)  * This commits the data to the ring buffer, and releases any locks held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184)  * Must be paired with ring_buffer_lock_reserve.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) int ring_buffer_unlock_commit(struct trace_buffer *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) 			      struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) 	int cpu = raw_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 	rb_commit(cpu_buffer, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 	rb_wakeups(buffer, cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 	trace_recursive_unlock(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 	preempt_enable_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) static struct ring_buffer_event *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 		  struct rb_event_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 	struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 	struct buffer_page *tail_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 	unsigned long tail, write, w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 	bool a_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 	bool b_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 	/* Don't let the compiler play games with cpu_buffer->tail_page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 	tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219)  /*A*/	w = local_read(&tail_page->write) & RB_WRITE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 	barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 	b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 	a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 	barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 	info->ts = rb_time_stamp(cpu_buffer->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 	if ((info->add_timestamp & RB_ADD_STAMP_ABSOLUTE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) 		info->delta = info->ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) 		 * If interrupting an event time update, we may need an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 		 * absolute timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 		 * Don't bother if this is the start of a new page (w == 0).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 		if (unlikely(!a_ok || !b_ok || (info->before != info->after && w))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 			info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 			info->length += RB_LEN_TIME_EXTEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) 			info->delta = info->ts - info->after;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) 			if (unlikely(test_time_stamp(info->delta))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) 				info->add_timestamp |= RB_ADD_STAMP_EXTEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) 				info->length += RB_LEN_TIME_EXTEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246)  /*B*/	rb_time_set(&cpu_buffer->before_stamp, info->ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248)  /*C*/	write = local_add_return(info->length, &tail_page->write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 	/* set write to only the index of the write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 	write &= RB_WRITE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 	tail = write - info->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 	/* See if we shot pass the end of this buffer page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 	if (unlikely(write > BUF_PAGE_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) 		/* before and after may now different, fix it up*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 		b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 		a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 		if (a_ok && b_ok && info->before != info->after)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 			(void)rb_time_cmpxchg(&cpu_buffer->before_stamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 					      info->before, info->after);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 		return rb_move_tail(cpu_buffer, tail, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 	if (likely(tail == w)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 		u64 save_before;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 		bool s_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 		/* Nothing interrupted us between A and C */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271)  /*D*/		rb_time_set(&cpu_buffer->write_stamp, info->ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 		barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273)  /*E*/		s_ok = rb_time_read(&cpu_buffer->before_stamp, &save_before);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 		RB_WARN_ON(cpu_buffer, !s_ok);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) 		if (likely(!(info->add_timestamp &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 			     (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 			/* This did not interrupt any time update */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 			info->delta = info->ts - info->after;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 			/* Just use full timestamp for inerrupting event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) 			info->delta = info->ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) 		barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) 		if (unlikely(info->ts != save_before)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) 			/* SLOW PATH - Interrupted between C and E */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 			a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) 			RB_WARN_ON(cpu_buffer, !a_ok);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) 			/* Write stamp must only go forward */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 			if (save_before > info->after) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) 				 * We do not care about the result, only that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) 				 * it gets updated atomically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) 				(void)rb_time_cmpxchg(&cpu_buffer->write_stamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 						      info->after, save_before);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 		u64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) 		/* SLOW PATH - Interrupted between A and C */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 		a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) 		/* Was interrupted before here, write_stamp must be valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 		RB_WARN_ON(cpu_buffer, !a_ok);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 		ts = rb_time_stamp(cpu_buffer->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 		barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307)  /*E*/		if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) 		    info->after < ts &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) 		    rb_time_cmpxchg(&cpu_buffer->write_stamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 				    info->after, ts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) 			/* Nothing came after this event between C and E */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) 			info->delta = ts - info->after;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) 			info->ts = ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) 			 * Interrupted beween C and E:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) 			 * Lost the previous events time stamp. Just set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) 			 * delta to zero, and this will be the same time as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) 			 * the event this event interrupted. And the events that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) 			 * came after this will still be correct (as they would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) 			 * have built their delta on the previous event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 			info->delta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 		info->add_timestamp &= ~RB_ADD_STAMP_FORCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 	 * If this is the first commit on the page, then it has the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 	 * timestamp as the page itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 	if (unlikely(!tail && !(info->add_timestamp &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) 				(RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 		info->delta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) 	/* We reserved something on the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) 	event = __rb_page_index(tail_page, tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) 	rb_update_event(cpu_buffer, event, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) 	local_inc(&tail_page->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 	 * If this is the first commit on the page, then update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) 	 * its timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) 	if (unlikely(!tail))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) 		tail_page->page->time_stamp = info->ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) 	/* account for these added bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 	local_add(info->length, &cpu_buffer->entries_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 	return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) static __always_inline struct ring_buffer_event *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) rb_reserve_next_event(struct trace_buffer *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) 		      struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) 		      unsigned long length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) 	struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 	struct rb_event_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 	int nr_loops = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 	int add_ts_default;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 	rb_start_commit(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 	/* The commit page can not change after this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) 	 * Due to the ability to swap a cpu buffer from a buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) 	 * it is possible it was swapped before we committed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) 	 * (committing stops a swap). We check for it here and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) 	 * if it happened, we have to fail the write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) 	barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) 	if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 		local_dec(&cpu_buffer->committing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) 		local_dec(&cpu_buffer->commits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) 	info.length = rb_calculate_event_length(length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 	if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) 		add_ts_default = RB_ADD_STAMP_ABSOLUTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) 		info.length += RB_LEN_TIME_EXTEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 		add_ts_default = RB_ADD_STAMP_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393)  again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) 	info.add_timestamp = add_ts_default;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 	info.delta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) 	 * We allow for interrupts to reenter here and do a trace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) 	 * If one does, it will cause this original code to loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) 	 * back here. Even with heavy interrupts happening, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) 	 * should only happen a few times in a row. If this happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) 	 * 1000 times in a row, there must be either an interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) 	 * storm or we have something buggy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) 	 * Bail!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) 	if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) 	event = __rb_reserve_next(cpu_buffer, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 	if (unlikely(PTR_ERR(event) == -EAGAIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) 		if (info.add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) 			info.length -= RB_LEN_TIME_EXTEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) 	if (likely(event))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) 		return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419)  out_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) 	rb_end_commit(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425)  * ring_buffer_lock_reserve - reserve a part of the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426)  * @buffer: the ring buffer to reserve from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427)  * @length: the length of the data to reserve (excluding event header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429)  * Returns a reserved event on the ring buffer to copy directly to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430)  * The user of this interface will need to get the body to write into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431)  * and can use the ring_buffer_event_data() interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433)  * The length is the length of the data needed, not the event length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434)  * which also includes the event header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436)  * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437)  * If NULL is returned, then nothing has been allocated or locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) struct ring_buffer_event *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) 	struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 	/* If we are tracing schedule, we don't want to recurse */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 	preempt_disable_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) 	if (unlikely(atomic_read(&buffer->record_disabled)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 	cpu = raw_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) 	if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 	if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 	if (unlikely(length > BUF_MAX_DATA_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 	if (unlikely(trace_recursive_lock(cpu_buffer)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 	event = rb_reserve_next_event(buffer, cpu_buffer, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 	if (!event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 	return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474)  out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 	trace_recursive_unlock(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) 	preempt_enable_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483)  * Decrement the entries to the page that an event is on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484)  * The event does not even need to exist, only the pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485)  * to the page it is on. This may only be called before the commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486)  * takes place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 		   struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) 	unsigned long addr = (unsigned long)event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 	struct buffer_page *bpage = cpu_buffer->commit_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 	struct buffer_page *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 	addr &= PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) 	/* Do the likely case first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) 	if (likely(bpage->page == (void *)addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 		local_dec(&bpage->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) 	 * Because the commit page may be on the reader page we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 	 * start with the next page and check the end loop there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 	rb_inc_page(cpu_buffer, &bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 	start = bpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 		if (bpage->page == (void *)addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) 			local_dec(&bpage->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 		rb_inc_page(cpu_buffer, &bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 	} while (bpage != start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 	/* commit not part of this buffer?? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 	RB_WARN_ON(cpu_buffer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523)  * ring_buffer_commit_discard - discard an event that has not been committed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524)  * @buffer: the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525)  * @event: non committed event to discard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527)  * Sometimes an event that is in the ring buffer needs to be ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528)  * This function lets the user discard an event in the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529)  * and then that event will not be read later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531)  * This function only works if it is called before the item has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532)  * committed. It will try to free the event from the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533)  * if another event has not been added behind it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535)  * If another event has been added behind it, it will set the event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536)  * up as discarded, and perform the commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538)  * If this function is called, do not call ring_buffer_unlock_commit on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539)  * the event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) void ring_buffer_discard_commit(struct trace_buffer *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) 				struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) 	/* The event is discarded regardless */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) 	rb_event_discard(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) 	cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 	 * This must only be called if the event has not been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) 	 * committed yet. Thus we can assume that preemption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 	 * is still disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) 	RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 	rb_decrement_entry(cpu_buffer, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) 	if (rb_try_to_discard(cpu_buffer, event))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 	rb_end_commit(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 	trace_recursive_unlock(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) 	preempt_enable_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575)  * ring_buffer_write - write data to the buffer without reserving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576)  * @buffer: The ring buffer to write to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577)  * @length: The length of the data being written (excluding the event header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578)  * @data: The data to write to the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580)  * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581)  * one function. If you already have the data to write to the buffer, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582)  * may be easier to simply call this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584)  * Note, like ring_buffer_lock_reserve, the length is the length of the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585)  * and not the length of the event which would hold the header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) int ring_buffer_write(struct trace_buffer *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) 		      unsigned long length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) 		      void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) 	struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) 	void *body;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 	int ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) 	preempt_disable_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) 	if (atomic_read(&buffer->record_disabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) 	cpu = raw_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) 	if (atomic_read(&cpu_buffer->record_disabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) 	if (length > BUF_MAX_DATA_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) 	if (unlikely(trace_recursive_lock(cpu_buffer)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) 	event = rb_reserve_next_event(buffer, cpu_buffer, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) 	if (!event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) 	body = rb_event_data(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 	memcpy(body, data, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) 	rb_commit(cpu_buffer, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) 	rb_wakeups(buffer, cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632)  out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 	trace_recursive_unlock(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 	preempt_enable_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) EXPORT_SYMBOL_GPL(ring_buffer_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 	struct buffer_page *reader = cpu_buffer->reader_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 	struct buffer_page *head = rb_set_head_page(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 	struct buffer_page *commit = cpu_buffer->commit_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 	/* In case of error, head will be NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) 	if (unlikely(!head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 	/* Reader should exhaust content in reader page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 	if (reader->read != rb_page_commit(reader))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 	 * If writers are committing on the reader page, knowing all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 	 * committed content has been read, the ring buffer is empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 	if (commit == reader)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 	 * If writers are committing on a page other than reader page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 	 * and head page, there should always be content to read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 	if (commit != head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 	 * Writers are committing on the head page, we just need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 	 * to care about there're committed data, and the reader will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 	 * swap reader page with head page when it is to read data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) 	return rb_page_commit(commit) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679)  * ring_buffer_record_disable - stop all writes into the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680)  * @buffer: The ring buffer to stop writes to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682)  * This prevents all writes to the buffer. Any attempt to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683)  * to the buffer after this will fail and return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685)  * The caller should call synchronize_rcu() after this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) void ring_buffer_record_disable(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) 	atomic_inc(&buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694)  * ring_buffer_record_enable - enable writes to the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695)  * @buffer: The ring buffer to enable writes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697)  * Note, multiple disables will need the same number of enables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698)  * to truly enable the writing (much like preempt_disable).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) void ring_buffer_record_enable(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 	atomic_dec(&buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707)  * ring_buffer_record_off - stop all writes into the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708)  * @buffer: The ring buffer to stop writes to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710)  * This prevents all writes to the buffer. Any attempt to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711)  * to the buffer after this will fail and return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713)  * This is different than ring_buffer_record_disable() as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714)  * it works like an on/off switch, where as the disable() version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715)  * must be paired with a enable().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) void ring_buffer_record_off(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 	unsigned int rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) 	unsigned int new_rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 		rd = atomic_read(&buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) 		new_rd = rd | RB_BUFFER_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) 	} while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) EXPORT_SYMBOL_GPL(ring_buffer_record_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730)  * ring_buffer_record_on - restart writes into the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731)  * @buffer: The ring buffer to start writes to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733)  * This enables all writes to the buffer that was disabled by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734)  * ring_buffer_record_off().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736)  * This is different than ring_buffer_record_enable() as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737)  * it works like an on/off switch, where as the enable() version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738)  * must be paired with a disable().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) void ring_buffer_record_on(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) 	unsigned int rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) 	unsigned int new_rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) 		rd = atomic_read(&buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) 		new_rd = rd & ~RB_BUFFER_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) 	} while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) EXPORT_SYMBOL_GPL(ring_buffer_record_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753)  * ring_buffer_record_is_on - return true if the ring buffer can write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754)  * @buffer: The ring buffer to see if write is enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756)  * Returns true if the ring buffer is in a state that it accepts writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) bool ring_buffer_record_is_on(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) 	return !atomic_read(&buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764)  * ring_buffer_record_is_set_on - return true if the ring buffer is set writable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765)  * @buffer: The ring buffer to see if write is set enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767)  * Returns true if the ring buffer is set writable by ring_buffer_record_on().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768)  * Note that this does NOT mean it is in a writable state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770)  * It may return true when the ring buffer has been disabled by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771)  * ring_buffer_record_disable(), as that is a temporary disabling of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772)  * the ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) bool ring_buffer_record_is_set_on(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 	return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780)  * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781)  * @buffer: The ring buffer to stop writes to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782)  * @cpu: The CPU buffer to stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784)  * This prevents all writes to the buffer. Any attempt to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785)  * to the buffer after this will fail and return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787)  * The caller should call synchronize_rcu() after this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) 	atomic_inc(&cpu_buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802)  * ring_buffer_record_enable_cpu - enable writes to the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803)  * @buffer: The ring buffer to enable writes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804)  * @cpu: The CPU to enable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806)  * Note, multiple disables will need the same number of enables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807)  * to truly enable the writing (much like preempt_disable).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) 	atomic_dec(&cpu_buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822)  * The total entries in the ring buffer is the running counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823)  * of entries entered into the ring buffer, minus the sum of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824)  * the entries read from the ring buffer and the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825)  * entries that were overwritten.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) static inline unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) 	return local_read(&cpu_buffer->entries) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) 		(local_read(&cpu_buffer->overrun) + cpu_buffer->read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835)  * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836)  * @buffer: The ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837)  * @cpu: The per CPU buffer to read from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) 	struct buffer_page *bpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) 	u64 ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) 	raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) 	 * if the tail is on reader_page, oldest time stamp is on the reader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) 	 * page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) 	if (cpu_buffer->tail_page == cpu_buffer->reader_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) 		bpage = cpu_buffer->reader_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) 		bpage = rb_set_head_page(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) 	if (bpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) 		ret = bpage->page->time_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) 	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868)  * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869)  * @buffer: The ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870)  * @cpu: The per CPU buffer to read from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) 	unsigned long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) 	ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888)  * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889)  * @buffer: The ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890)  * @cpu: The per CPU buffer to get the entries from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) 	return rb_num_of_entries(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906)  * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907)  * buffer wrapping around (only if RB_FL_OVERWRITE is on).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908)  * @buffer: The ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909)  * @cpu: The per CPU buffer to get the number of overruns from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) 	unsigned long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) 	ret = local_read(&cpu_buffer->overrun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927)  * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928)  * commits failing due to the buffer wrapping around while there are uncommitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929)  * events, such as during an interrupt storm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930)  * @buffer: The ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931)  * @cpu: The per CPU buffer to get the number of overruns from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) 	unsigned long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) 	ret = local_read(&cpu_buffer->commit_overrun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950)  * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951)  * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952)  * @buffer: The ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953)  * @cpu: The per CPU buffer to get the number of overruns from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) 	unsigned long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) 	ret = local_read(&cpu_buffer->dropped_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972)  * ring_buffer_read_events_cpu - get the number of events successfully read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973)  * @buffer: The ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974)  * @cpu: The per CPU buffer to get the number of events read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) 	return cpu_buffer->read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990)  * ring_buffer_entries - get the number of entries in a buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991)  * @buffer: The ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993)  * Returns the total number of entries in the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994)  * (all CPU entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) unsigned long ring_buffer_entries(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) 	unsigned long entries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) 	/* if you care about this being correct, lock the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) 	for_each_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) 		cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) 		entries += rb_num_of_entries(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) 	return entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) EXPORT_SYMBOL_GPL(ring_buffer_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013)  * ring_buffer_overruns - get the number of overruns in buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014)  * @buffer: The ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016)  * Returns the total number of overruns in the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017)  * (all CPU entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) unsigned long ring_buffer_overruns(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) 	unsigned long overruns = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) 	/* if you care about this being correct, lock the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) 	for_each_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) 		cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) 		overruns += local_read(&cpu_buffer->overrun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) 	return overruns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) EXPORT_SYMBOL_GPL(ring_buffer_overruns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) static void rb_iter_reset(struct ring_buffer_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) 	struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) 	/* Iterator usage is expected to have record disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) 	iter->head_page = cpu_buffer->reader_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) 	iter->head = cpu_buffer->reader_page->read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) 	iter->next_event = iter->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) 	iter->cache_reader_page = iter->head_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) 	iter->cache_read = cpu_buffer->read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) 	if (iter->head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) 		iter->read_stamp = cpu_buffer->read_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) 		iter->page_stamp = cpu_buffer->reader_page->page->time_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) 		iter->read_stamp = iter->head_page->page->time_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) 		iter->page_stamp = iter->read_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057)  * ring_buffer_iter_reset - reset an iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058)  * @iter: The iterator to reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060)  * Resets the iterator, so that it will start from the beginning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061)  * again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) 	if (!iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) 	cpu_buffer = iter->cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) 	raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) 	rb_iter_reset(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) 	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080)  * ring_buffer_iter_empty - check if an iterator has no more to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081)  * @iter: The iterator to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) 	struct buffer_page *reader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) 	struct buffer_page *head_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) 	struct buffer_page *commit_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) 	struct buffer_page *curr_commit_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) 	unsigned commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) 	u64 curr_commit_ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) 	u64 commit_ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) 	cpu_buffer = iter->cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) 	reader = cpu_buffer->reader_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) 	head_page = cpu_buffer->head_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) 	commit_page = cpu_buffer->commit_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) 	commit_ts = commit_page->page->time_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) 	 * When the writer goes across pages, it issues a cmpxchg which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) 	 * is a mb(), which will synchronize with the rmb here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) 	 * (see rb_tail_page_update())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) 	smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) 	commit = rb_page_commit(commit_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) 	/* We want to make sure that the commit page doesn't change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) 	smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) 	/* Make sure commit page didn't change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) 	curr_commit_page = READ_ONCE(cpu_buffer->commit_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) 	curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) 	/* If the commit page changed, then there's more data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) 	if (curr_commit_page != commit_page ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) 	    curr_commit_ts != commit_ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) 	/* Still racy, as it may return a false positive, but that's OK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) 	return ((iter->head_page == commit_page && iter->head >= commit) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) 		(iter->head_page == reader && commit_page == head_page &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) 		 head_page->read == commit &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) 		 iter->head == rb_page_commit(cpu_buffer->reader_page)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) 		     struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) 	u64 delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) 	switch (event->type_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) 	case RINGBUF_TYPE_PADDING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) 	case RINGBUF_TYPE_TIME_EXTEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) 		delta = ring_buffer_event_time_stamp(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) 		cpu_buffer->read_stamp += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) 	case RINGBUF_TYPE_TIME_STAMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) 		delta = ring_buffer_event_time_stamp(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) 		cpu_buffer->read_stamp = delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) 	case RINGBUF_TYPE_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) 		cpu_buffer->read_stamp += event->time_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) 		RB_WARN_ON(cpu_buffer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) 			  struct ring_buffer_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) 	u64 delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) 	switch (event->type_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) 	case RINGBUF_TYPE_PADDING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) 	case RINGBUF_TYPE_TIME_EXTEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) 		delta = ring_buffer_event_time_stamp(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) 		iter->read_stamp += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) 	case RINGBUF_TYPE_TIME_STAMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) 		delta = ring_buffer_event_time_stamp(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) 		iter->read_stamp = delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) 	case RINGBUF_TYPE_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) 		iter->read_stamp += event->time_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) 		RB_WARN_ON(iter->cpu_buffer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) static struct buffer_page *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) 	struct buffer_page *reader = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) 	unsigned long overwrite;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) 	int nr_loops = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) 	arch_spin_lock(&cpu_buffer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199)  again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) 	 * This should normally only loop twice. But because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) 	 * start of the reader inserts an empty page, it causes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) 	 * a case where we will loop three times. There should be no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) 	 * reason to loop four times (that I know of).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) 	if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) 		reader = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) 	reader = cpu_buffer->reader_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) 	/* If there's more to read, return this page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) 	if (cpu_buffer->reader_page->read < rb_page_size(reader))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) 	/* Never should we have an index greater than the size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) 	if (RB_WARN_ON(cpu_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) 		       cpu_buffer->reader_page->read > rb_page_size(reader)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) 	/* check if we caught up to the tail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) 	reader = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) 	if (cpu_buffer->commit_page == cpu_buffer->reader_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) 	/* Don't bother swapping if the ring buffer is empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) 	if (rb_num_of_entries(cpu_buffer) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) 	 * Reset the reader page to size zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) 	local_set(&cpu_buffer->reader_page->write, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) 	local_set(&cpu_buffer->reader_page->entries, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) 	local_set(&cpu_buffer->reader_page->page->commit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) 	cpu_buffer->reader_page->real_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239)  spin:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) 	 * Splice the empty reader page into the list around the head.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) 	reader = rb_set_head_page(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) 	if (!reader)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) 	cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) 	cpu_buffer->reader_page->list.prev = reader->list.prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) 	 * cpu_buffer->pages just needs to point to the buffer, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) 	 *  has no specific buffer page to point to. Lets move it out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) 	 *  of our way so we don't accidentally swap it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) 	cpu_buffer->pages = reader->list.prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) 	/* The reader page will be pointing to the new head */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) 	rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) 	 * We want to make sure we read the overruns after we set up our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) 	 * pointers to the next object. The writer side does a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) 	 * cmpxchg to cross pages which acts as the mb on the writer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) 	 * side. Note, the reader will constantly fail the swap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) 	 * while the writer is updating the pointers, so this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) 	 * guarantees that the overwrite recorded here is the one we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) 	 * want to compare with the last_overrun.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) 	overwrite = local_read(&(cpu_buffer->overrun));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) 	 * Here's the tricky part.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) 	 * We need to move the pointer past the header page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) 	 * But we can only do that if a writer is not currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) 	 * moving it. The page before the header page has the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) 	 * flag bit '1' set if it is pointing to the page we want.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) 	 * but if the writer is in the process of moving it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) 	 * than it will be '2' or already moved '0'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) 	ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) 	 * If we did not convert it, then we must try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) 		goto spin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) 	 * Yay! We succeeded in replacing the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) 	 * Now make the new head point back to the reader page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) 	rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) 	rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) 	local_inc(&cpu_buffer->pages_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) 	/* Finally update the reader page to the new head */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301) 	cpu_buffer->reader_page = reader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) 	cpu_buffer->reader_page->read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) 	if (overwrite != cpu_buffer->last_overrun) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) 		cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) 		cpu_buffer->last_overrun = overwrite;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) 	goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) 	/* Update the read_stamp on the first event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) 	if (reader && reader->read == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) 		cpu_buffer->read_stamp = reader->page->time_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) 	arch_spin_unlock(&cpu_buffer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) 	return reader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) 	struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) 	struct buffer_page *reader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326) 	unsigned length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) 	reader = rb_get_reader_page(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) 	/* This function should not be called when buffer is empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) 	if (RB_WARN_ON(cpu_buffer, !reader))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) 	event = rb_reader_event(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) 	if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) 		cpu_buffer->read++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) 	rb_update_read_stamp(cpu_buffer, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) 	length = rb_event_length(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) 	cpu_buffer->reader_page->read += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) static void rb_advance_iter(struct ring_buffer_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) 	cpu_buffer = iter->cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) 	/* If head == next_event then we need to jump to the next event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) 	if (iter->head == iter->next_event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) 		/* If the event gets overwritten again, there's nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) 		if (rb_iter_head_event(iter) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358) 	iter->head = iter->next_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) 	 * Check if we are at the end of the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) 	if (iter->next_event >= rb_page_size(iter->head_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364) 		/* discarded commits can make the page empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365) 		if (iter->head_page == cpu_buffer->commit_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367) 		rb_inc_iter(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) 	rb_update_iter_read_stamp(iter, iter->event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) 	return cpu_buffer->lost_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) static struct ring_buffer_event *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381) 	       unsigned long *lost_events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) 	struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) 	struct buffer_page *reader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) 	int nr_loops = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) 	if (ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) 		*ts = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389)  again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) 	 * We repeat when a time extend is encountered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392) 	 * Since the time extend is always attached to a data event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) 	 * we should never loop more than once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) 	 * (We never hit the following condition more than twice).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) 	if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) 	reader = rb_get_reader_page(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) 	if (!reader)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) 	event = rb_reader_event(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) 	switch (event->type_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) 	case RINGBUF_TYPE_PADDING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) 		if (rb_null_event(event))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) 			RB_WARN_ON(cpu_buffer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) 		 * Because the writer could be discarding every
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) 		 * event it creates (which would probably be bad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) 		 * if we were to go back to "again" then we may never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) 		 * catch up, and will trigger the warn on, or lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) 		 * the box. Return the padding, and we will release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) 		 * the current locks, and try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) 		return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) 	case RINGBUF_TYPE_TIME_EXTEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) 		/* Internal data, OK to advance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) 		rb_advance_reader(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) 	case RINGBUF_TYPE_TIME_STAMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) 		if (ts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) 			*ts = ring_buffer_event_time_stamp(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) 			ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) 							 cpu_buffer->cpu, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) 		/* Internal data, OK to advance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) 		rb_advance_reader(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) 	case RINGBUF_TYPE_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) 		if (ts && !(*ts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) 			*ts = cpu_buffer->read_stamp + event->time_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) 			ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) 							 cpu_buffer->cpu, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) 		if (lost_events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) 			*lost_events = rb_lost_events(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) 		return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) 		RB_WARN_ON(cpu_buffer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) EXPORT_SYMBOL_GPL(ring_buffer_peek);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) static struct ring_buffer_event *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) 	struct trace_buffer *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) 	struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) 	int nr_loops = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) 	if (ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) 		*ts = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) 	cpu_buffer = iter->cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) 	buffer = cpu_buffer->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) 	 * Check if someone performed a consuming read to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468) 	 * the buffer. A consuming read invalidates the iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) 	 * and we need to reset the iterator in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) 	if (unlikely(iter->cache_read != cpu_buffer->read ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) 		     iter->cache_reader_page != cpu_buffer->reader_page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) 		rb_iter_reset(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475)  again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476) 	if (ring_buffer_iter_empty(iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480) 	 * As the writer can mess with what the iterator is trying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481) 	 * to read, just give up if we fail to get an event after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) 	 * three tries. The iterator is not as reliable when reading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) 	 * the ring buffer with an active write as the consumer is.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) 	 * Do not warn if the three failures is reached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486) 	if (++nr_loops > 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489) 	if (rb_per_cpu_empty(cpu_buffer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) 	if (iter->head >= rb_page_size(iter->head_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493) 		rb_inc_iter(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) 	event = rb_iter_head_event(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498) 	if (!event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501) 	switch (event->type_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502) 	case RINGBUF_TYPE_PADDING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) 		if (rb_null_event(event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504) 			rb_inc_iter(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505) 			goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507) 		rb_advance_iter(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) 		return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510) 	case RINGBUF_TYPE_TIME_EXTEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) 		/* Internal data, OK to advance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512) 		rb_advance_iter(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515) 	case RINGBUF_TYPE_TIME_STAMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516) 		if (ts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517) 			*ts = ring_buffer_event_time_stamp(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518) 			ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519) 							 cpu_buffer->cpu, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521) 		/* Internal data, OK to advance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522) 		rb_advance_iter(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525) 	case RINGBUF_TYPE_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526) 		if (ts && !(*ts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527) 			*ts = iter->read_stamp + event->time_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528) 			ring_buffer_normalize_time_stamp(buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529) 							 cpu_buffer->cpu, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531) 		return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) 		RB_WARN_ON(cpu_buffer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4537) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4539) EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541) static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543) 	if (likely(!in_nmi())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544) 		raw_spin_lock(&cpu_buffer->reader_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549) 	 * If an NMI die dumps out the content of the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550) 	 * trylock must be used to prevent a deadlock if the NMI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) 	 * preempted a task that holds the ring buffer locks. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552) 	 * we get the lock then all is fine, if not, then continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) 	 * to do the read, but this can corrupt the ring buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554) 	 * so it must be permanently disabled from future writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555) 	 * Reading from NMI is a oneshot deal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557) 	if (raw_spin_trylock(&cpu_buffer->reader_lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560) 	/* Continue without locking, but disable the ring buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) 	atomic_inc(&cpu_buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4568) 	if (likely(locked))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4569) 		raw_spin_unlock(&cpu_buffer->reader_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574)  * ring_buffer_peek - peek at the next event to be read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575)  * @buffer: The ring buffer to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576)  * @cpu: The cpu to peak at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577)  * @ts: The timestamp counter of this event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578)  * @lost_events: a variable to store if events were lost (may be NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580)  * This will return the event that will be read next, but does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581)  * not consume the data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583) struct ring_buffer_event *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584) ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) 		 unsigned long *lost_events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587) 	struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588) 	struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) 	bool dolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595)  again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) 	dolock = rb_reader_lock(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598) 	event = rb_buffer_peek(cpu_buffer, ts, lost_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599) 	if (event && event->type_len == RINGBUF_TYPE_PADDING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600) 		rb_advance_reader(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601) 	rb_reader_unlock(cpu_buffer, dolock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604) 	if (event && event->type_len == RINGBUF_TYPE_PADDING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) 	return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) /** ring_buffer_iter_dropped - report if there are dropped events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611)  * @iter: The ring buffer iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613)  * Returns true if there was dropped events since the last peek.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615) bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617) 	bool ret = iter->missed_events != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619) 	iter->missed_events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622) EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625)  * ring_buffer_iter_peek - peek at the next event to be read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626)  * @iter: The ring buffer iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627)  * @ts: The timestamp counter of this event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629)  * This will return the event that will be read next, but does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630)  * not increment the iterator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632) struct ring_buffer_event *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633) ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635) 	struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636) 	struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639)  again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) 	raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) 	event = rb_iter_peek(iter, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642) 	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) 	if (event && event->type_len == RINGBUF_TYPE_PADDING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) 	return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651)  * ring_buffer_consume - return an event and consume it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652)  * @buffer: The ring buffer to get the next event from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653)  * @cpu: the cpu to read the buffer from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654)  * @ts: a variable to store the timestamp (may be NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655)  * @lost_events: a variable to store if events were lost (may be NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657)  * Returns the next event in the ring buffer, and that event is consumed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658)  * Meaning, that sequential reads will keep returning a different event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659)  * and eventually empty the ring buffer if the producer is slower.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661) struct ring_buffer_event *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662) ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) 		    unsigned long *lost_events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666) 	struct ring_buffer_event *event = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668) 	bool dolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670)  again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671) 	/* might be called in atomic */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672) 	preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) 	dolock = rb_reader_lock(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) 	event = rb_buffer_peek(cpu_buffer, ts, lost_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682) 	if (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683) 		cpu_buffer->lost_events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) 		rb_advance_reader(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687) 	rb_reader_unlock(cpu_buffer, dolock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691) 	preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693) 	if (event && event->type_len == RINGBUF_TYPE_PADDING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696) 	return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) EXPORT_SYMBOL_GPL(ring_buffer_consume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4701)  * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4702)  * @buffer: The ring buffer to read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4703)  * @cpu: The cpu buffer to iterate over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4704)  * @flags: gfp flags to use for memory allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4705)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4706)  * This performs the initial preparations necessary to iterate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4707)  * through the buffer.  Memory is allocated, buffer recording
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4708)  * is disabled, and the iterator pointer is returned to the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4709)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4710)  * Disabling buffer recording prevents the reading from being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4711)  * corrupted. This is not a consuming read, so a producer is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4712)  * expected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4713)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4714)  * After a sequence of ring_buffer_read_prepare calls, the user is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4715)  * expected to make at least one call to ring_buffer_read_prepare_sync.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4716)  * Afterwards, ring_buffer_read_start is invoked to get things going
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4717)  * for real.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4718)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4719)  * This overall must be paired with ring_buffer_read_finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4720)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4721) struct ring_buffer_iter *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4722) ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4724) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4725) 	struct ring_buffer_iter *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4727) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4728) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4730) 	iter = kzalloc(sizeof(*iter), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4731) 	if (!iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4732) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4734) 	iter->event = kmalloc(BUF_MAX_DATA_SIZE, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4735) 	if (!iter->event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4736) 		kfree(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4737) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4740) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4742) 	iter->cpu_buffer = cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4744) 	atomic_inc(&cpu_buffer->resize_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4746) 	return iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4748) EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4750) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4751)  * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4752)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4753)  * All previously invoked ring_buffer_read_prepare calls to prepare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4754)  * iterators will be synchronized.  Afterwards, read_buffer_read_start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4755)  * calls on those iterators are allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4756)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4757) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4758) ring_buffer_read_prepare_sync(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4760) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4762) EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4764) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4765)  * ring_buffer_read_start - start a non consuming read of the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4766)  * @iter: The iterator returned by ring_buffer_read_prepare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4767)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4768)  * This finalizes the startup of an iteration through the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4769)  * The iterator comes from a call to ring_buffer_read_prepare and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4770)  * an intervening ring_buffer_read_prepare_sync must have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4771)  * performed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4772)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4773)  * Must be paired with ring_buffer_read_finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4774)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4775) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4776) ring_buffer_read_start(struct ring_buffer_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4778) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4779) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4781) 	if (!iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4782) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4784) 	cpu_buffer = iter->cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4786) 	raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4787) 	arch_spin_lock(&cpu_buffer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4788) 	rb_iter_reset(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4789) 	arch_spin_unlock(&cpu_buffer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4790) 	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4792) EXPORT_SYMBOL_GPL(ring_buffer_read_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4794) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4795)  * ring_buffer_read_finish - finish reading the iterator of the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4796)  * @iter: The iterator retrieved by ring_buffer_start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4797)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4798)  * This re-enables the recording to the buffer, and frees the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4799)  * iterator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4800)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4801) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4802) ring_buffer_read_finish(struct ring_buffer_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4804) 	struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4805) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4807) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4808) 	 * Ring buffer is disabled from recording, here's a good place
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4809) 	 * to check the integrity of the ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4810) 	 * Must prevent readers from trying to read, as the check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4811) 	 * clears the HEAD page and readers require it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4812) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4813) 	raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4814) 	rb_check_pages(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4815) 	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4817) 	atomic_dec(&cpu_buffer->resize_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4818) 	kfree(iter->event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4819) 	kfree(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4821) EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4823) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4824)  * ring_buffer_iter_advance - advance the iterator to the next location
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4825)  * @iter: The ring buffer iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4826)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4827)  * Move the location of the iterator such that the next read will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4828)  * be the next location of the iterator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4829)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4830) void ring_buffer_iter_advance(struct ring_buffer_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4831) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4832) 	struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4833) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4835) 	raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4837) 	rb_advance_iter(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4839) 	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4841) EXPORT_SYMBOL_GPL(ring_buffer_iter_advance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4843) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4844)  * ring_buffer_size - return the size of the ring buffer (in bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4845)  * @buffer: The ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4846)  * @cpu: The CPU to get ring buffer size from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4847)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4848) unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4849) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4850) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4851) 	 * Earlier, this method returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4852) 	 *	BUF_PAGE_SIZE * buffer->nr_pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4853) 	 * Since the nr_pages field is now removed, we have converted this to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4854) 	 * return the per cpu buffer value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4855) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4856) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4857) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4859) 	return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4861) EXPORT_SYMBOL_GPL(ring_buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4863) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4864) rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4865) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4866) 	rb_head_page_deactivate(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4868) 	cpu_buffer->head_page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4869) 		= list_entry(cpu_buffer->pages, struct buffer_page, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4870) 	local_set(&cpu_buffer->head_page->write, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4871) 	local_set(&cpu_buffer->head_page->entries, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4872) 	local_set(&cpu_buffer->head_page->page->commit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4874) 	cpu_buffer->head_page->read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4876) 	cpu_buffer->tail_page = cpu_buffer->head_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4877) 	cpu_buffer->commit_page = cpu_buffer->head_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4879) 	INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4880) 	INIT_LIST_HEAD(&cpu_buffer->new_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4881) 	local_set(&cpu_buffer->reader_page->write, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4882) 	local_set(&cpu_buffer->reader_page->entries, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4883) 	local_set(&cpu_buffer->reader_page->page->commit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4884) 	cpu_buffer->reader_page->read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4886) 	local_set(&cpu_buffer->entries_bytes, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4887) 	local_set(&cpu_buffer->overrun, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4888) 	local_set(&cpu_buffer->commit_overrun, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4889) 	local_set(&cpu_buffer->dropped_events, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4890) 	local_set(&cpu_buffer->entries, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4891) 	local_set(&cpu_buffer->committing, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4892) 	local_set(&cpu_buffer->commits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4893) 	local_set(&cpu_buffer->pages_touched, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4894) 	local_set(&cpu_buffer->pages_read, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4895) 	cpu_buffer->last_pages_touch = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4896) 	cpu_buffer->shortest_full = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4897) 	cpu_buffer->read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4898) 	cpu_buffer->read_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4900) 	rb_time_set(&cpu_buffer->write_stamp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4901) 	rb_time_set(&cpu_buffer->before_stamp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4903) 	cpu_buffer->lost_events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4904) 	cpu_buffer->last_overrun = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4906) 	rb_head_page_activate(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4909) /* Must have disabled the cpu buffer then done a synchronize_rcu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4910) static void reset_disabled_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4911) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4912) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4914) 	raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4916) 	if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4917) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4919) 	arch_spin_lock(&cpu_buffer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4921) 	rb_reset_cpu(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4923) 	arch_spin_unlock(&cpu_buffer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4925)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4926) 	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4929) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4930)  * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4931)  * @buffer: The ring buffer to reset a per cpu buffer of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4932)  * @cpu: The CPU buffer to be reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4933)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4934) void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4935) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4936) 	struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4938) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4939) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4941) 	/* prevent another thread from changing buffer sizes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4942) 	mutex_lock(&buffer->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4944) 	atomic_inc(&cpu_buffer->resize_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4945) 	atomic_inc(&cpu_buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4947) 	/* Make sure all commits have finished */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4948) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4950) 	reset_disabled_cpu_buffer(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4952) 	atomic_dec(&cpu_buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4953) 	atomic_dec(&cpu_buffer->resize_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4955) 	mutex_unlock(&buffer->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4957) EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4959) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4960)  * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4961)  * @buffer: The ring buffer to reset a per cpu buffer of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4962)  * @cpu: The CPU buffer to be reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4963)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4964) void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4965) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4966) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4967) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4969) 	/* prevent another thread from changing buffer sizes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4970) 	mutex_lock(&buffer->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4972) 	for_each_online_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4973) 		cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4975) 		atomic_inc(&cpu_buffer->resize_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4976) 		atomic_inc(&cpu_buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4977) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4979) 	/* Make sure all commits have finished */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4980) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4982) 	for_each_online_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4983) 		cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4985) 		reset_disabled_cpu_buffer(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4987) 		atomic_dec(&cpu_buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4988) 		atomic_dec(&cpu_buffer->resize_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4989) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4991) 	mutex_unlock(&buffer->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4994) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4995)  * ring_buffer_reset - reset a ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4996)  * @buffer: The ring buffer to reset all cpu buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4997)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4998) void ring_buffer_reset(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5000) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5001) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5003) 	/* prevent another thread from changing buffer sizes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5004) 	mutex_lock(&buffer->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5006) 	for_each_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5007) 		cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5009) 		atomic_inc(&cpu_buffer->resize_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5010) 		atomic_inc(&cpu_buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5011) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5013) 	/* Make sure all commits have finished */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5014) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5016) 	for_each_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5017) 		cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5019) 		reset_disabled_cpu_buffer(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5021) 		atomic_dec(&cpu_buffer->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5022) 		atomic_dec(&cpu_buffer->resize_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5023) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5025) 	mutex_unlock(&buffer->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5027) EXPORT_SYMBOL_GPL(ring_buffer_reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5029) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5030)  * rind_buffer_empty - is the ring buffer empty?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5031)  * @buffer: The ring buffer to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5032)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5033) bool ring_buffer_empty(struct trace_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5035) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5036) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5037) 	bool dolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5038) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5039) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5041) 	/* yes this is racy, but if you don't like the race, lock the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5042) 	for_each_buffer_cpu(buffer, cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5043) 		cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5044) 		local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5045) 		dolock = rb_reader_lock(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5046) 		ret = rb_per_cpu_empty(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5047) 		rb_reader_unlock(cpu_buffer, dolock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5048) 		local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5050) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5051) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5052) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5054) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5056) EXPORT_SYMBOL_GPL(ring_buffer_empty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5058) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5059)  * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5060)  * @buffer: The ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5061)  * @cpu: The CPU buffer to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5062)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5063) bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5065) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5066) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5067) 	bool dolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5068) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5070) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5071) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5073) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5074) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5075) 	dolock = rb_reader_lock(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5076) 	ret = rb_per_cpu_empty(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5077) 	rb_reader_unlock(cpu_buffer, dolock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5078) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5080) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5082) EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5084) #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5085) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5086)  * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5087)  * @buffer_a: One buffer to swap with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5088)  * @buffer_b: The other buffer to swap with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5089)  * @cpu: the CPU of the buffers to swap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5090)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5091)  * This function is useful for tracers that want to take a "snapshot"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5092)  * of a CPU buffer and has another back up buffer lying around.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5093)  * it is expected that the tracer handles the cpu buffer not being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5094)  * used at the moment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5095)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5096) int ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5097) 			 struct trace_buffer *buffer_b, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5098) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5099) 	struct ring_buffer_per_cpu *cpu_buffer_a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5100) 	struct ring_buffer_per_cpu *cpu_buffer_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5101) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5103) 	if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5104) 	    !cpumask_test_cpu(cpu, buffer_b->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5105) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5107) 	cpu_buffer_a = buffer_a->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5108) 	cpu_buffer_b = buffer_b->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5110) 	/* At least make sure the two buffers are somewhat the same */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5111) 	if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5112) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5114) 	ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5116) 	if (atomic_read(&buffer_a->record_disabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5117) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5119) 	if (atomic_read(&buffer_b->record_disabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5120) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5122) 	if (atomic_read(&cpu_buffer_a->record_disabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5123) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5125) 	if (atomic_read(&cpu_buffer_b->record_disabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5126) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5128) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5129) 	 * We can't do a synchronize_rcu here because this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5130) 	 * function can be called in atomic context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5131) 	 * Normally this will be called from the same CPU as cpu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5132) 	 * If not it's up to the caller to protect this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5133) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5134) 	atomic_inc(&cpu_buffer_a->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5135) 	atomic_inc(&cpu_buffer_b->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5137) 	ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5138) 	if (local_read(&cpu_buffer_a->committing))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5139) 		goto out_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5140) 	if (local_read(&cpu_buffer_b->committing))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5141) 		goto out_dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5143) 	buffer_a->buffers[cpu] = cpu_buffer_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5144) 	buffer_b->buffers[cpu] = cpu_buffer_a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5146) 	cpu_buffer_b->buffer = buffer_a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5147) 	cpu_buffer_a->buffer = buffer_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5149) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5151) out_dec:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5152) 	atomic_dec(&cpu_buffer_a->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5153) 	atomic_dec(&cpu_buffer_b->record_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5154) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5155) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5157) EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5158) #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5160) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5161)  * ring_buffer_alloc_read_page - allocate a page to read from buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5162)  * @buffer: the buffer to allocate for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5163)  * @cpu: the cpu buffer to allocate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5164)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5165)  * This function is used in conjunction with ring_buffer_read_page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5166)  * When reading a full page from the ring buffer, these functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5167)  * can be used to speed up the process. The calling function should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5168)  * allocate a few pages first with this function. Then when it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5169)  * needs to get pages from the ring buffer, it passes the result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5170)  * of this function into ring_buffer_read_page, which will swap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5171)  * the page that was allocated, with the read page of the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5172)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5173)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5174)  *  The page allocated, or ERR_PTR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5175)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5176) void *ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5178) 	struct ring_buffer_per_cpu *cpu_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5179) 	struct buffer_data_page *bpage = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5180) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5181) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5183) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5184) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5186) 	cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5187) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5188) 	arch_spin_lock(&cpu_buffer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5190) 	if (cpu_buffer->free_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5191) 		bpage = cpu_buffer->free_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5192) 		cpu_buffer->free_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5195) 	arch_spin_unlock(&cpu_buffer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5196) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5198) 	if (bpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5199) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5201) 	page = alloc_pages_node(cpu_to_node(cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5202) 				GFP_KERNEL | __GFP_NORETRY, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5203) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5204) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5206) 	bpage = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5208)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5209) 	rb_init_page(bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5211) 	return bpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5213) EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5215) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5216)  * ring_buffer_free_read_page - free an allocated read page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5217)  * @buffer: the buffer the page was allocate for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5218)  * @cpu: the cpu buffer the page came from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5219)  * @data: the page to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5220)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5221)  * Free a page allocated from ring_buffer_alloc_read_page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5222)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5223) void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5225) 	struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5226) 	struct buffer_data_page *bpage = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5227) 	struct page *page = virt_to_page(bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5228) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5230) 	/* If the page is still in use someplace else, we can't reuse it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5231) 	if (page_ref_count(page) > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5232) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5234) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5235) 	arch_spin_lock(&cpu_buffer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5237) 	if (!cpu_buffer->free_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5238) 		cpu_buffer->free_page = bpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5239) 		bpage = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5242) 	arch_spin_unlock(&cpu_buffer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5243) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5245)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5246) 	free_page((unsigned long)bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5248) EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5250) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5251)  * ring_buffer_read_page - extract a page from the ring buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5252)  * @buffer: buffer to extract from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5253)  * @data_page: the page to use allocated from ring_buffer_alloc_read_page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5254)  * @len: amount to extract
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5255)  * @cpu: the cpu of the buffer to extract
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5256)  * @full: should the extraction only happen when the page is full.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5257)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5258)  * This function will pull out a page from the ring buffer and consume it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5259)  * @data_page must be the address of the variable that was returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5260)  * from ring_buffer_alloc_read_page. This is because the page might be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5261)  * to swap with a page in the ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5262)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5263)  * for example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5264)  *	rpage = ring_buffer_alloc_read_page(buffer, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5265)  *	if (IS_ERR(rpage))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5266)  *		return PTR_ERR(rpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5267)  *	ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5268)  *	if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5269)  *		process_page(rpage, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5270)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5271)  * When @full is set, the function will not return true unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5272)  * the writer is off the reader page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5273)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5274)  * Note: it is up to the calling functions to handle sleeps and wakeups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5275)  *  The ring buffer can be used anywhere in the kernel and can not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5276)  *  blindly call wake_up. The layer that uses the ring buffer must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5277)  *  responsible for that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5278)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5279)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5280)  *  >=0 if data has been transferred, returns the offset of consumed data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5281)  *  <0 if no data has been transferred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5282)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5283) int ring_buffer_read_page(struct trace_buffer *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5284) 			  void **data_page, size_t len, int cpu, int full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5286) 	struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5287) 	struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5288) 	struct buffer_data_page *bpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5289) 	struct buffer_page *reader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5290) 	unsigned long missed_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5291) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5292) 	unsigned int commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5293) 	unsigned int read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5294) 	u64 save_timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5295) 	int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5297) 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5298) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5300) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5301) 	 * If len is not big enough to hold the page header, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5302) 	 * we can not copy anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5303) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5304) 	if (len <= BUF_PAGE_HDR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5305) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5307) 	len -= BUF_PAGE_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5309) 	if (!data_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5310) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5312) 	bpage = *data_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5313) 	if (!bpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5314) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5316) 	raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5318) 	reader = rb_get_reader_page(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5319) 	if (!reader)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5320) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5322) 	event = rb_reader_event(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5324) 	read = reader->read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5325) 	commit = rb_page_commit(reader);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5327) 	/* Check if any events were dropped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5328) 	missed_events = cpu_buffer->lost_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5330) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5331) 	 * If this page has been partially read or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5332) 	 * if len is not big enough to read the rest of the page or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5333) 	 * a writer is still on the page, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5334) 	 * we must copy the data from the page to the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5335) 	 * Otherwise, we can simply swap the page with the one passed in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5336) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5337) 	if (read || (len < (commit - read)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5338) 	    cpu_buffer->reader_page == cpu_buffer->commit_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5339) 		struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5340) 		unsigned int rpos = read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5341) 		unsigned int pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5342) 		unsigned int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5344) 		if (full)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5345) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5347) 		if (len > (commit - read))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5348) 			len = (commit - read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5350) 		/* Always keep the time extend and data together */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5351) 		size = rb_event_ts_length(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5353) 		if (len < size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5354) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5356) 		/* save the current timestamp, since the user will need it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5357) 		save_timestamp = cpu_buffer->read_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5359) 		/* Need to copy one event at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5360) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5361) 			/* We need the size of one event, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5362) 			 * rb_advance_reader only advances by one event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5363) 			 * whereas rb_event_ts_length may include the size of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5364) 			 * one or two events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5365) 			 * We have already ensured there's enough space if this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5366) 			 * is a time extend. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5367) 			size = rb_event_length(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5368) 			memcpy(bpage->data + pos, rpage->data + rpos, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5370) 			len -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5372) 			rb_advance_reader(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5373) 			rpos = reader->read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5374) 			pos += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5376) 			if (rpos >= commit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5377) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5379) 			event = rb_reader_event(cpu_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5380) 			/* Always keep the time extend and data together */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5381) 			size = rb_event_ts_length(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5382) 		} while (len >= size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5384) 		/* update bpage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5385) 		local_set(&bpage->commit, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5386) 		bpage->time_stamp = save_timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5388) 		/* we copied everything to the beginning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5389) 		read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5390) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5391) 		/* update the entry counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5392) 		cpu_buffer->read += rb_page_entries(reader);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5393) 		cpu_buffer->read_bytes += BUF_PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5395) 		/* swap the pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5396) 		rb_init_page(bpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5397) 		bpage = reader->page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5398) 		reader->page = *data_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5399) 		local_set(&reader->write, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5400) 		local_set(&reader->entries, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5401) 		reader->read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5402) 		*data_page = bpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5404) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5405) 		 * Use the real_end for the data size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5406) 		 * This gives us a chance to store the lost events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5407) 		 * on the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5408) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5409) 		if (reader->real_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5410) 			local_set(&bpage->commit, reader->real_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5412) 	ret = read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5414) 	cpu_buffer->lost_events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5416) 	commit = local_read(&bpage->commit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5417) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5418) 	 * Set a flag in the commit field if we lost events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5419) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5420) 	if (missed_events) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5421) 		/* If there is room at the end of the page to save the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5422) 		 * missed events, then record it there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5423) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5424) 		if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5425) 			memcpy(&bpage->data[commit], &missed_events,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5426) 			       sizeof(missed_events));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5427) 			local_add(RB_MISSED_STORED, &bpage->commit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5428) 			commit += sizeof(missed_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5429) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5430) 		local_add(RB_MISSED_EVENTS, &bpage->commit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5433) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5434) 	 * This page may be off to user land. Zero it out here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5435) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5436) 	if (commit < BUF_PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5437) 		memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5439)  out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5440) 	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5442)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5443) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5445) EXPORT_SYMBOL_GPL(ring_buffer_read_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5447) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5448)  * We only allocate new buffers, never free them if the CPU goes down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5449)  * If we were to free the buffer, then the user would lose any trace that was in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5450)  * the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5451)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5452) int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5454) 	struct trace_buffer *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5455) 	long nr_pages_same;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5456) 	int cpu_i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5457) 	unsigned long nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5459) 	buffer = container_of(node, struct trace_buffer, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5460) 	if (cpumask_test_cpu(cpu, buffer->cpumask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5461) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5463) 	nr_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5464) 	nr_pages_same = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5465) 	/* check if all cpu sizes are same */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5466) 	for_each_buffer_cpu(buffer, cpu_i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5467) 		/* fill in the size from first enabled cpu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5468) 		if (nr_pages == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5469) 			nr_pages = buffer->buffers[cpu_i]->nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5470) 		if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5471) 			nr_pages_same = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5472) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5473) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5475) 	/* allocate minimum pages, user can later expand it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5476) 	if (!nr_pages_same)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5477) 		nr_pages = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5478) 	buffer->buffers[cpu] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5479) 		rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5480) 	if (!buffer->buffers[cpu]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5481) 		WARN(1, "failed to allocate ring buffer on CPU %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5482) 		     cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5483) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5485) 	smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5486) 	cpumask_set_cpu(cpu, buffer->cpumask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5487) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5490) #ifdef CONFIG_RING_BUFFER_STARTUP_TEST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5491) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5492)  * This is a basic integrity check of the ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5493)  * Late in the boot cycle this test will run when configured in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5494)  * It will kick off a thread per CPU that will go into a loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5495)  * writing to the per cpu ring buffer various sizes of data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5496)  * Some of the data will be large items, some small.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5497)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5498)  * Another thread is created that goes into a spin, sending out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5499)  * IPIs to the other CPUs to also write into the ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5500)  * this is to test the nesting ability of the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5501)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5502)  * Basic stats are recorded and reported. If something in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5503)  * ring buffer should happen that's not expected, a big warning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5504)  * is displayed and all ring buffers are disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5505)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5506) static struct task_struct *rb_threads[NR_CPUS] __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5508) struct rb_test_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5509) 	struct trace_buffer *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5510) 	unsigned long		events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5511) 	unsigned long		bytes_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5512) 	unsigned long		bytes_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5513) 	unsigned long		bytes_dropped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5514) 	unsigned long		events_nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5515) 	unsigned long		bytes_written_nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5516) 	unsigned long		bytes_alloc_nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5517) 	unsigned long		bytes_dropped_nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5518) 	int			min_size_nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5519) 	int			max_size_nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5520) 	int			max_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5521) 	int			min_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5522) 	int			cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5523) 	int			cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5524) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5526) static struct rb_test_data rb_data[NR_CPUS] __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5528) /* 1 meg per cpu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5529) #define RB_TEST_BUFFER_SIZE	1048576
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5531) static char rb_string[] __initdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5532) 	"abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5533) 	"?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5534) 	"!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5536) static bool rb_test_started __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5538) struct rb_item {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5539) 	int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5540) 	char str[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5541) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5543) static __init int rb_write_something(struct rb_test_data *data, bool nested)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5545) 	struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5546) 	struct rb_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5547) 	bool started;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5548) 	int event_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5549) 	int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5550) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5551) 	int cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5553) 	/* Have nested writes different that what is written */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5554) 	cnt = data->cnt + (nested ? 27 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5556) 	/* Multiply cnt by ~e, to make some unique increment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5557) 	size = (cnt * 68 / 25) % (sizeof(rb_string) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5559) 	len = size + sizeof(struct rb_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5561) 	started = rb_test_started;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5562) 	/* read rb_test_started before checking buffer enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5563) 	smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5565) 	event = ring_buffer_lock_reserve(data->buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5566) 	if (!event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5567) 		/* Ignore dropped events before test starts. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5568) 		if (started) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5569) 			if (nested)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5570) 				data->bytes_dropped += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5571) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5572) 				data->bytes_dropped_nested += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5573) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5574) 		return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5577) 	event_len = ring_buffer_event_length(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5579) 	if (RB_WARN_ON(data->buffer, event_len < len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5580) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5582) 	item = ring_buffer_event_data(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5583) 	item->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5584) 	memcpy(item->str, rb_string, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5586) 	if (nested) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5587) 		data->bytes_alloc_nested += event_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5588) 		data->bytes_written_nested += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5589) 		data->events_nested++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5590) 		if (!data->min_size_nested || len < data->min_size_nested)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5591) 			data->min_size_nested = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5592) 		if (len > data->max_size_nested)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5593) 			data->max_size_nested = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5594) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5595) 		data->bytes_alloc += event_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5596) 		data->bytes_written += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5597) 		data->events++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5598) 		if (!data->min_size || len < data->min_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5599) 			data->max_size = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5600) 		if (len > data->max_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5601) 			data->max_size = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5602) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5604)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5605) 	ring_buffer_unlock_commit(data->buffer, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5607) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5610) static __init int rb_test(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5612) 	struct rb_test_data *data = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5614) 	while (!kthread_should_stop()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5615) 		rb_write_something(data, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5616) 		data->cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5618) 		set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5619) 		/* Now sleep between a min of 100-300us and a max of 1ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5620) 		usleep_range(((data->cnt % 3) + 1) * 100, 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5623) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5626) static __init void rb_ipi(void *ignore)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5628) 	struct rb_test_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5629) 	int cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5631) 	data = &rb_data[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5632) 	rb_write_something(data, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5635) static __init int rb_hammer_test(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5637) 	while (!kthread_should_stop()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5639) 		/* Send an IPI to all cpus to write data! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5640) 		smp_call_function(rb_ipi, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5641) 		/* No sleep, but for non preempt, let others run */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5642) 		schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5643) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5645) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5648) static __init int test_ringbuffer(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5650) 	struct task_struct *rb_hammer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5651) 	struct trace_buffer *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5652) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5653) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5655) 	if (security_locked_down(LOCKDOWN_TRACEFS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5656) 		pr_warn("Lockdown is enabled, skipping ring buffer tests\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5657) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5658) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5660) 	pr_info("Running ring buffer tests...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5662) 	buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5663) 	if (WARN_ON(!buffer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5664) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5666) 	/* Disable buffer so that threads can't write to it yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5667) 	ring_buffer_record_off(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5669) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5670) 		rb_data[cpu].buffer = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5671) 		rb_data[cpu].cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5672) 		rb_data[cpu].cnt = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5673) 		rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5674) 						 "rbtester/%d", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5675) 		if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5676) 			pr_cont("FAILED\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5677) 			ret = PTR_ERR(rb_threads[cpu]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5678) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5679) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5681) 		kthread_bind(rb_threads[cpu], cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5682)  		wake_up_process(rb_threads[cpu]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5683) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5685) 	/* Now create the rb hammer! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5686) 	rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5687) 	if (WARN_ON(IS_ERR(rb_hammer))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5688) 		pr_cont("FAILED\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5689) 		ret = PTR_ERR(rb_hammer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5690) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5691) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5693) 	ring_buffer_record_on(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5694) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5695) 	 * Show buffer is enabled before setting rb_test_started.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5696) 	 * Yes there's a small race window where events could be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5697) 	 * dropped and the thread wont catch it. But when a ring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5698) 	 * buffer gets enabled, there will always be some kind of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5699) 	 * delay before other CPUs see it. Thus, we don't care about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5700) 	 * those dropped events. We care about events dropped after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5701) 	 * the threads see that the buffer is active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5702) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5703) 	smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5704) 	rb_test_started = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5706) 	set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5707) 	/* Just run for 10 seconds */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5708) 	schedule_timeout(10 * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5710) 	kthread_stop(rb_hammer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5712)  out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5713) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5714) 		if (!rb_threads[cpu])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5715) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5716) 		kthread_stop(rb_threads[cpu]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5717) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5718) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5719) 		ring_buffer_free(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5720) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5721) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5723) 	/* Report! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5724) 	pr_info("finished\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5725) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5726) 		struct ring_buffer_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5727) 		struct rb_test_data *data = &rb_data[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5728) 		struct rb_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5729) 		unsigned long total_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5730) 		unsigned long total_dropped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5731) 		unsigned long total_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5732) 		unsigned long total_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5733) 		unsigned long total_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5734) 		unsigned long total_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5735) 		unsigned long total_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5736) 		unsigned long total_lost = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5737) 		unsigned long lost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5738) 		int big_event_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5739) 		int small_event_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5741) 		ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5743) 		total_events = data->events + data->events_nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5744) 		total_written = data->bytes_written + data->bytes_written_nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5745) 		total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5746) 		total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5748) 		big_event_size = data->max_size + data->max_size_nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5749) 		small_event_size = data->min_size + data->min_size_nested;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5751) 		pr_info("CPU %d:\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5752) 		pr_info("              events:    %ld\n", total_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5753) 		pr_info("       dropped bytes:    %ld\n", total_dropped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5754) 		pr_info("       alloced bytes:    %ld\n", total_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5755) 		pr_info("       written bytes:    %ld\n", total_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5756) 		pr_info("       biggest event:    %d\n", big_event_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5757) 		pr_info("      smallest event:    %d\n", small_event_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5759) 		if (RB_WARN_ON(buffer, total_dropped))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5760) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5762) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5764) 		while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5765) 			total_lost += lost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5766) 			item = ring_buffer_event_data(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5767) 			total_len += ring_buffer_event_length(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5768) 			total_size += item->size + sizeof(struct rb_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5769) 			if (memcmp(&item->str[0], rb_string, item->size) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5770) 				pr_info("FAILED!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5771) 				pr_info("buffer had: %.*s\n", item->size, item->str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5772) 				pr_info("expected:   %.*s\n", item->size, rb_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5773) 				RB_WARN_ON(buffer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5774) 				ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5775) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5776) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5777) 			total_read++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5778) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5779) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5780) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5782) 		ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5784) 		pr_info("         read events:   %ld\n", total_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5785) 		pr_info("         lost events:   %ld\n", total_lost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5786) 		pr_info("        total events:   %ld\n", total_lost + total_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5787) 		pr_info("  recorded len bytes:   %ld\n", total_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5788) 		pr_info(" recorded size bytes:   %ld\n", total_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5789) 		if (total_lost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5790) 			pr_info(" With dropped events, record len and size may not match\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5791) 				" alloced and written from above\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5792) 		if (!total_lost) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5793) 			if (RB_WARN_ON(buffer, total_len != total_alloc ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5794) 				       total_size != total_written))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5795) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5796) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5797) 		if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5798) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5800) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5801) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5802) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5803) 		pr_info("Ring buffer PASSED!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5805) 	ring_buffer_free(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5806) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5809) late_initcall(test_ringbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5810) #endif /* CONFIG_RING_BUFFER_STARTUP_TEST */