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