• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_RING_BUFFER_H
3 #define _LINUX_RING_BUFFER_H
4 
5 #include <linux/mm.h>
6 #include <linux/poll.h>
7 #include <linux/ring_buffer_ext.h>
8 #include <linux/seq_file.h>
9 
10 #include <asm/local.h>
11 
12 struct trace_buffer;
13 struct ring_buffer_iter;
14 
15 /*
16  * Don't refer to this struct directly, use functions below.
17  */
18 struct ring_buffer_event {
19 	u32		type_len:5, time_delta:27;
20 
21 	u32		array[];
22 };
23 
24 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
25 
26 /**
27  * enum ring_buffer_type - internal ring buffer types
28  *
29  * @RINGBUF_TYPE_PADDING:	Left over page padding or discarded event
30  *				 If time_delta is 0:
31  *				  array is ignored
32  *				  size is variable depending on how much
33  *				  padding is needed
34  *				 If time_delta is non zero:
35  *				  array[0] holds the actual length
36  *				  size = 4 + length (bytes)
37  *
38  * @RINGBUF_TYPE_TIME_EXTEND:	Extend the time delta
39  *				 array[0] = time delta (28 .. 59)
40  *				 size = 8 bytes
41  *
42  * @RINGBUF_TYPE_TIME_STAMP:	Absolute timestamp
43  *				 Same format as TIME_EXTEND except that the
44  *				 value is an absolute timestamp, not a delta
45  *				 event.time_delta contains bottom 27 bits
46  *				 array[0] = top (28 .. 59) bits
47  *				 size = 8 bytes
48  *
49  * <= @RINGBUF_TYPE_DATA_TYPE_LEN_MAX:
50  *				Data record
51  *				 If type_len is zero:
52  *				  array[0] holds the actual length
53  *				  array[1..(length+3)/4] holds data
54  *				  size = 4 + length (bytes)
55  *				 else
56  *				  length = type_len << 2
57  *				  array[0..(length+3)/4-1] holds data
58  *				  size = 4 + length (bytes)
59  */
60 enum ring_buffer_type {
61 	RINGBUF_TYPE_DATA_TYPE_LEN_MAX = 28,
62 	RINGBUF_TYPE_PADDING,
63 	RINGBUF_TYPE_TIME_EXTEND,
64 	RINGBUF_TYPE_TIME_STAMP,
65 };
66 
67 #define TS_SHIFT        27
68 #define TS_MASK         ((1ULL << TS_SHIFT) - 1)
69 #define TS_DELTA_TEST   (~TS_MASK)
70 
71 /*
72  * We need to fit the time_stamp delta into 27 bits.
73  */
test_time_stamp(u64 delta)74 static inline int test_time_stamp(u64 delta)
75 {
76 	if (delta & TS_DELTA_TEST)
77 		return 1;
78 	return 0;
79 }
80 
81 unsigned ring_buffer_event_length(struct ring_buffer_event *event);
82 void *ring_buffer_event_data(struct ring_buffer_event *event);
83 u64 ring_buffer_event_time_stamp(struct trace_buffer *buffer,
84 				 struct ring_buffer_event *event);
85 
86 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
87 
88 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
89 
90 #define RB_ALIGNMENT		4U
91 #define RB_MAX_SMALL_DATA	(RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
92 #define RB_EVNT_MIN_SIZE	8U	/* two 32bit words */
93 
94 #ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS
95 # define RB_FORCE_8BYTE_ALIGNMENT	0
96 # define RB_ARCH_ALIGNMENT		RB_ALIGNMENT
97 #else
98 # define RB_FORCE_8BYTE_ALIGNMENT	1
99 # define RB_ARCH_ALIGNMENT		8U
100 #endif
101 
102 #define RB_ALIGN_DATA		__aligned(RB_ARCH_ALIGNMENT)
103 
104 struct buffer_data_page {
105 	u64		 time_stamp;	/* page time stamp */
106 	local_t		 commit;	/* write committed index */
107 	unsigned char	 data[] RB_ALIGN_DATA;	/* data of buffer page */
108 };
109 
110 /*
111  * ring_buffer_discard_commit will remove an event that has not
112  *   been committed yet. If this is used, then ring_buffer_unlock_commit
113  *   must not be called on the discarded event. This function
114  *   will try to remove the event from the ring buffer completely
115  *   if another event has not been written after it.
116  *
117  * Example use:
118  *
119  *  if (some_condition)
120  *    ring_buffer_discard_commit(buffer, event);
121  *  else
122  *    ring_buffer_unlock_commit(buffer, event);
123  */
124 void ring_buffer_discard_commit(struct trace_buffer *buffer,
125 				struct ring_buffer_event *event);
126 
127 /*
128  * size is in bytes for each per CPU buffer.
129  */
130 struct trace_buffer *
131 __ring_buffer_alloc(unsigned long size, unsigned flags, struct lock_class_key *key);
132 
133 /*
134  * Because the ring buffer is generic, if other users of the ring buffer get
135  * traced by ftrace, it can produce lockdep warnings. We need to keep each
136  * ring buffer's lock class separate.
137  */
138 #define ring_buffer_alloc(size, flags)			\
139 ({							\
140 	static struct lock_class_key __key;		\
141 	__ring_buffer_alloc((size), (flags), &__key);	\
142 })
143 
144 struct ring_buffer_ext_cb {
145 	int (*update_footers)(int cpu);
146 	int (*swap_reader)(int cpu);
147 };
148 
149 struct trace_buffer *
150 ring_buffer_alloc_ext(unsigned long size, struct ring_buffer_ext_cb *cb);
151 
152 int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full);
153 __poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
154 			  struct file *filp, poll_table *poll_table, int full);
155 void ring_buffer_wake_waiters(struct trace_buffer *buffer, int cpu);
156 
157 #define RING_BUFFER_ALL_CPUS -1
158 
159 void ring_buffer_free(struct trace_buffer *buffer);
160 
161 int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size, int cpu);
162 
163 void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val);
164 
165 struct ring_buffer_event *ring_buffer_lock_reserve(struct trace_buffer *buffer,
166 						   unsigned long length);
167 int ring_buffer_unlock_commit(struct trace_buffer *buffer,
168 			      struct ring_buffer_event *event);
169 int ring_buffer_write(struct trace_buffer *buffer,
170 		      unsigned long length, void *data);
171 
172 void ring_buffer_nest_start(struct trace_buffer *buffer);
173 void ring_buffer_nest_end(struct trace_buffer *buffer);
174 
175 struct ring_buffer_event *
176 ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts,
177 		 unsigned long *lost_events);
178 struct ring_buffer_event *
179 ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
180 		    unsigned long *lost_events);
181 
182 struct ring_buffer_iter *
183 ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags);
184 void ring_buffer_read_prepare_sync(void);
185 void ring_buffer_read_start(struct ring_buffer_iter *iter);
186 void ring_buffer_read_finish(struct ring_buffer_iter *iter);
187 
188 struct ring_buffer_event *
189 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts);
190 void ring_buffer_iter_advance(struct ring_buffer_iter *iter);
191 void ring_buffer_iter_reset(struct ring_buffer_iter *iter);
192 int ring_buffer_iter_empty(struct ring_buffer_iter *iter);
193 bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter);
194 
195 unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu);
196 
197 void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu);
198 void ring_buffer_reset_online_cpus(struct trace_buffer *buffer);
199 void ring_buffer_reset(struct trace_buffer *buffer);
200 
201 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
202 int ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
203 			 struct trace_buffer *buffer_b, int cpu);
204 #else
205 static inline int
ring_buffer_swap_cpu(struct trace_buffer * buffer_a,struct trace_buffer * buffer_b,int cpu)206 ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
207 		     struct trace_buffer *buffer_b, int cpu)
208 {
209 	return -ENODEV;
210 }
211 #endif
212 
213 bool ring_buffer_empty(struct trace_buffer *buffer);
214 bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu);
215 
216 void ring_buffer_record_disable(struct trace_buffer *buffer);
217 void ring_buffer_record_enable(struct trace_buffer *buffer);
218 void ring_buffer_record_off(struct trace_buffer *buffer);
219 void ring_buffer_record_on(struct trace_buffer *buffer);
220 bool ring_buffer_record_is_on(struct trace_buffer *buffer);
221 bool ring_buffer_record_is_set_on(struct trace_buffer *buffer);
222 void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu);
223 void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu);
224 
225 u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu);
226 unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu);
227 unsigned long ring_buffer_entries(struct trace_buffer *buffer);
228 unsigned long ring_buffer_overruns(struct trace_buffer *buffer);
229 unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu);
230 unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu);
231 unsigned long ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu);
232 unsigned long ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu);
233 unsigned long ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu);
234 
235 u64 ring_buffer_time_stamp(struct trace_buffer *buffer);
236 void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer,
237 				      int cpu, u64 *ts);
238 void ring_buffer_set_clock(struct trace_buffer *buffer,
239 			   u64 (*clock)(void));
240 void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs);
241 bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer);
242 
243 size_t ring_buffer_nr_pages(struct trace_buffer *buffer, int cpu);
244 size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu);
245 
246 void *ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu);
247 void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu, void *data);
248 int ring_buffer_read_page(struct trace_buffer *buffer, void **data_page,
249 			  size_t len, int cpu, int full);
250 
251 struct trace_seq;
252 
253 int ring_buffer_print_entry_header(struct trace_seq *s);
254 int ring_buffer_print_page_header(struct trace_seq *s);
255 
256 enum ring_buffer_flags {
257 	RB_FL_OVERWRITE		= 1 << 0,
258 };
259 
260 #ifdef CONFIG_RING_BUFFER
261 int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node);
262 #else
263 #define trace_rb_cpu_prepare	NULL
264 #endif
265 
266 size_t trace_buffer_pack_size(struct trace_buffer *trace_buffer);
267 int trace_buffer_pack(struct trace_buffer *trace_buffer, struct trace_buffer_pack *pack);
268 int ring_buffer_poke(struct trace_buffer *buffer, int cpu);
269 
270 #endif /* _LINUX_RING_BUFFER_H */
271