Steven Rostedt (VMware) | bcea3f9 | 2018-08-16 11:23:53 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Generic ring buffer |
| 4 | * |
| 5 | * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com> |
| 6 | */ |
Steven Rostedt (VMware) | 28575c6 | 2020-11-02 14:43:10 -0500 | [diff] [blame] | 7 | #include <linux/trace_recursion.h> |
Steven Rostedt (Red Hat) | af658dc | 2015-04-29 14:36:05 -0400 | [diff] [blame] | 8 | #include <linux/trace_events.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 9 | #include <linux/ring_buffer.h> |
Ingo Molnar | 14131f2 | 2009-02-26 18:47:11 +0100 | [diff] [blame] | 10 | #include <linux/trace_clock.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 11 | #include <linux/sched/clock.h> |
Steven Rostedt | 0b07436 | 2013-01-22 16:58:30 -0500 | [diff] [blame] | 12 | #include <linux/trace_seq.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 13 | #include <linux/spinlock.h> |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 14 | #include <linux/irq_work.h> |
Steven Rostedt (VMware) | a356646 | 2019-12-02 16:25:27 -0500 | [diff] [blame] | 15 | #include <linux/security.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Steven Rostedt | a81bd80 | 2009-02-06 01:45:16 -0500 | [diff] [blame] | 17 | #include <linux/hardirq.h> |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 18 | #include <linux/kthread.h> /* for self test */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/percpu.h> |
| 21 | #include <linux/mutex.h> |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 22 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 24 | #include <linux/init.h> |
| 25 | #include <linux/hash.h> |
| 26 | #include <linux/list.h> |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 27 | #include <linux/cpu.h> |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 28 | #include <linux/oom.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 29 | |
Christoph Lameter | 7961576 | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 30 | #include <asm/local.h> |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 31 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 32 | static void update_pages_handler(struct work_struct *work); |
| 33 | |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 34 | /* |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 35 | * The ring buffer header is special. We must manually up keep it. |
| 36 | */ |
| 37 | int ring_buffer_print_entry_header(struct trace_seq *s) |
| 38 | { |
Steven Rostedt (Red Hat) | c0cd93a | 2014-11-12 11:49:00 -0500 | [diff] [blame] | 39 | trace_seq_puts(s, "# compressed entry header\n"); |
| 40 | trace_seq_puts(s, "\ttype_len : 5 bits\n"); |
| 41 | trace_seq_puts(s, "\ttime_delta : 27 bits\n"); |
| 42 | trace_seq_puts(s, "\tarray : 32 bits\n"); |
| 43 | trace_seq_putc(s, '\n'); |
| 44 | trace_seq_printf(s, "\tpadding : type == %d\n", |
| 45 | RINGBUF_TYPE_PADDING); |
| 46 | trace_seq_printf(s, "\ttime_extend : type == %d\n", |
| 47 | RINGBUF_TYPE_TIME_EXTEND); |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 48 | trace_seq_printf(s, "\ttime_stamp : type == %d\n", |
| 49 | RINGBUF_TYPE_TIME_STAMP); |
Steven Rostedt (Red Hat) | c0cd93a | 2014-11-12 11:49:00 -0500 | [diff] [blame] | 50 | trace_seq_printf(s, "\tdata max type_len == %d\n", |
| 51 | RINGBUF_TYPE_DATA_TYPE_LEN_MAX); |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 52 | |
Steven Rostedt (Red Hat) | c0cd93a | 2014-11-12 11:49:00 -0500 | [diff] [blame] | 53 | return !trace_seq_has_overflowed(s); |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* |
Steven Rostedt | 5cc9854 | 2009-03-12 22:24:17 -0400 | [diff] [blame] | 57 | * The ring buffer is made up of a list of pages. A separate list of pages is |
| 58 | * allocated for each CPU. A writer may only write to a buffer that is |
| 59 | * associated with the CPU it is currently executing on. A reader may read |
| 60 | * from any per cpu buffer. |
| 61 | * |
| 62 | * The reader is special. For each per cpu buffer, the reader has its own |
| 63 | * reader page. When a reader has read the entire reader page, this reader |
| 64 | * page is swapped with another page in the ring buffer. |
| 65 | * |
| 66 | * Now, as long as the writer is off the reader page, the reader can do what |
| 67 | * ever it wants with that page. The writer will never write to that page |
| 68 | * again (as long as it is out of the ring buffer). |
| 69 | * |
| 70 | * Here's some silly ASCII art. |
| 71 | * |
| 72 | * +------+ |
| 73 | * |reader| RING BUFFER |
| 74 | * |page | |
| 75 | * +------+ +---+ +---+ +---+ |
| 76 | * | |-->| |-->| | |
| 77 | * +---+ +---+ +---+ |
| 78 | * ^ | |
| 79 | * | | |
| 80 | * +---------------+ |
| 81 | * |
| 82 | * |
| 83 | * +------+ |
| 84 | * |reader| RING BUFFER |
| 85 | * |page |------------------v |
| 86 | * +------+ +---+ +---+ +---+ |
| 87 | * | |-->| |-->| | |
| 88 | * +---+ +---+ +---+ |
| 89 | * ^ | |
| 90 | * | | |
| 91 | * +---------------+ |
| 92 | * |
| 93 | * |
| 94 | * +------+ |
| 95 | * |reader| RING BUFFER |
| 96 | * |page |------------------v |
| 97 | * +------+ +---+ +---+ +---+ |
| 98 | * ^ | |-->| |-->| | |
| 99 | * | +---+ +---+ +---+ |
| 100 | * | | |
| 101 | * | | |
| 102 | * +------------------------------+ |
| 103 | * |
| 104 | * |
| 105 | * +------+ |
| 106 | * |buffer| RING BUFFER |
| 107 | * |page |------------------v |
| 108 | * +------+ +---+ +---+ +---+ |
| 109 | * ^ | | | |-->| | |
| 110 | * | New +---+ +---+ +---+ |
| 111 | * | Reader------^ | |
| 112 | * | page | |
| 113 | * +------------------------------+ |
| 114 | * |
| 115 | * |
| 116 | * After we make this swap, the reader can hand this page off to the splice |
| 117 | * code and be done with it. It can even allocate a new page if it needs to |
| 118 | * and swap that into the ring buffer. |
| 119 | * |
| 120 | * We will be using cmpxchg soon to make all this lockless. |
| 121 | * |
| 122 | */ |
| 123 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 124 | /* Used for individual buffers (after the counter) */ |
| 125 | #define RB_BUFFER_OFF (1 << 20) |
| 126 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 127 | #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data) |
| 128 | |
Steven Rostedt | e3d6bf0 | 2009-03-03 13:53:07 -0500 | [diff] [blame] | 129 | #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array)) |
Andrew Morton | 67d3472 | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 130 | #define RB_ALIGNMENT 4U |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 131 | #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 132 | #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */ |
Steven Rostedt (VMware) | adab66b | 2020-12-14 12:33:51 -0500 | [diff] [blame] | 133 | |
| 134 | #ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS |
| 135 | # define RB_FORCE_8BYTE_ALIGNMENT 0 |
| 136 | # define RB_ARCH_ALIGNMENT RB_ALIGNMENT |
| 137 | #else |
| 138 | # define RB_FORCE_8BYTE_ALIGNMENT 1 |
| 139 | # define RB_ARCH_ALIGNMENT 8U |
| 140 | #endif |
| 141 | |
| 142 | #define RB_ALIGN_DATA __aligned(RB_ARCH_ALIGNMENT) |
James Hogan | 649508f | 2012-05-30 12:11:19 +0100 | [diff] [blame] | 143 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 144 | /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */ |
| 145 | #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 146 | |
| 147 | enum { |
| 148 | RB_LEN_TIME_EXTEND = 8, |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 149 | RB_LEN_TIME_STAMP = 8, |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 150 | }; |
| 151 | |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 152 | #define skip_time_extend(event) \ |
| 153 | ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND)) |
| 154 | |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 155 | #define extended_time(event) \ |
| 156 | (event->type_len >= RINGBUF_TYPE_TIME_EXTEND) |
| 157 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 158 | static inline int rb_null_event(struct ring_buffer_event *event) |
| 159 | { |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 160 | return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static void rb_event_set_padding(struct ring_buffer_event *event) |
| 164 | { |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 165 | /* padding has a NULL time_delta */ |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 166 | event->type_len = RINGBUF_TYPE_PADDING; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 167 | event->time_delta = 0; |
| 168 | } |
| 169 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 170 | static unsigned |
| 171 | rb_event_data_length(struct ring_buffer_event *event) |
| 172 | { |
| 173 | unsigned length; |
| 174 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 175 | if (event->type_len) |
| 176 | length = event->type_len * RB_ALIGNMENT; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 177 | else |
| 178 | length = event->array[0]; |
| 179 | return length + RB_EVNT_HDR_SIZE; |
| 180 | } |
| 181 | |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 182 | /* |
| 183 | * Return the length of the given event. Will return |
| 184 | * the length of the time extend if the event is a |
| 185 | * time extend. |
| 186 | */ |
| 187 | static inline unsigned |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 188 | rb_event_length(struct ring_buffer_event *event) |
| 189 | { |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 190 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 191 | case RINGBUF_TYPE_PADDING: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 192 | if (rb_null_event(event)) |
| 193 | /* undefined */ |
| 194 | return -1; |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 195 | return event->array[0] + RB_EVNT_HDR_SIZE; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 196 | |
| 197 | case RINGBUF_TYPE_TIME_EXTEND: |
| 198 | return RB_LEN_TIME_EXTEND; |
| 199 | |
| 200 | case RINGBUF_TYPE_TIME_STAMP: |
| 201 | return RB_LEN_TIME_STAMP; |
| 202 | |
| 203 | case RINGBUF_TYPE_DATA: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 204 | return rb_event_data_length(event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 205 | default: |
Steven Rostedt (VMware) | da4d401 | 2020-05-13 15:36:22 -0400 | [diff] [blame] | 206 | WARN_ON_ONCE(1); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 207 | } |
| 208 | /* not hit */ |
| 209 | return 0; |
| 210 | } |
| 211 | |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 212 | /* |
| 213 | * Return total length of time extend and data, |
| 214 | * or just the event length for all other events. |
| 215 | */ |
| 216 | static inline unsigned |
| 217 | rb_event_ts_length(struct ring_buffer_event *event) |
| 218 | { |
| 219 | unsigned len = 0; |
| 220 | |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 221 | if (extended_time(event)) { |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 222 | /* time extends include the data event after it */ |
| 223 | len = RB_LEN_TIME_EXTEND; |
| 224 | event = skip_time_extend(event); |
| 225 | } |
| 226 | return len + rb_event_length(event); |
| 227 | } |
| 228 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 229 | /** |
| 230 | * ring_buffer_event_length - return the length of the event |
| 231 | * @event: the event to get the length of |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 232 | * |
| 233 | * Returns the size of the data load of a data event. |
| 234 | * If the event is something other than a data event, it |
| 235 | * returns the size of the event itself. With the exception |
| 236 | * of a TIME EXTEND, where it still returns the size of the |
| 237 | * data load of the data event after it. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 238 | */ |
| 239 | unsigned ring_buffer_event_length(struct ring_buffer_event *event) |
| 240 | { |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 241 | unsigned length; |
| 242 | |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 243 | if (extended_time(event)) |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 244 | event = skip_time_extend(event); |
| 245 | |
| 246 | length = rb_event_length(event); |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 247 | if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
Robert Richter | 465634a | 2009-01-07 15:32:11 +0100 | [diff] [blame] | 248 | return length; |
| 249 | length -= RB_EVNT_HDR_SIZE; |
| 250 | if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0])) |
| 251 | length -= sizeof(event->array[0]); |
| 252 | return length; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 253 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 254 | EXPORT_SYMBOL_GPL(ring_buffer_event_length); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 255 | |
| 256 | /* inline for ring buffer fast paths */ |
Steven Rostedt (Red Hat) | 929ddbf | 2016-11-23 11:40:34 -0500 | [diff] [blame] | 257 | static __always_inline void * |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 258 | rb_event_data(struct ring_buffer_event *event) |
| 259 | { |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 260 | if (extended_time(event)) |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 261 | event = skip_time_extend(event); |
Steven Rostedt (VMware) | da4d401 | 2020-05-13 15:36:22 -0400 | [diff] [blame] | 262 | WARN_ON_ONCE(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 263 | /* If length is in len field, then array[0] has the data */ |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 264 | if (event->type_len) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 265 | return (void *)&event->array[0]; |
| 266 | /* Otherwise length is in array[0] and array[1] has the data */ |
| 267 | return (void *)&event->array[1]; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * ring_buffer_event_data - return the data of the event |
| 272 | * @event: the event to get the data from |
| 273 | */ |
| 274 | void *ring_buffer_event_data(struct ring_buffer_event *event) |
| 275 | { |
| 276 | return rb_event_data(event); |
| 277 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 278 | EXPORT_SYMBOL_GPL(ring_buffer_event_data); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 279 | |
| 280 | #define for_each_buffer_cpu(buffer, cpu) \ |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 281 | for_each_cpu(cpu, buffer->cpumask) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 282 | |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame] | 283 | #define for_each_online_buffer_cpu(buffer, cpu) \ |
| 284 | for_each_cpu_and(cpu, buffer->cpumask, cpu_online_mask) |
| 285 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 286 | #define TS_SHIFT 27 |
| 287 | #define TS_MASK ((1ULL << TS_SHIFT) - 1) |
| 288 | #define TS_DELTA_TEST (~TS_MASK) |
| 289 | |
Steven Rostedt (VMware) | e20044f | 2021-03-16 12:41:01 -0400 | [diff] [blame] | 290 | static u64 rb_event_time_stamp(struct ring_buffer_event *event) |
| 291 | { |
| 292 | u64 ts; |
| 293 | |
| 294 | ts = event->array[0]; |
| 295 | ts <<= TS_SHIFT; |
| 296 | ts += event->time_delta; |
| 297 | |
| 298 | return ts; |
| 299 | } |
| 300 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 301 | /* Flag when events were overwritten */ |
| 302 | #define RB_MISSED_EVENTS (1 << 31) |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 303 | /* Missed count stored at end */ |
| 304 | #define RB_MISSED_STORED (1 << 30) |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 305 | |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 306 | struct buffer_data_page { |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 307 | u64 time_stamp; /* page time stamp */ |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 308 | local_t commit; /* write committed index */ |
James Hogan | 649508f | 2012-05-30 12:11:19 +0100 | [diff] [blame] | 309 | unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */ |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 310 | }; |
| 311 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 312 | /* |
| 313 | * Note, the buffer_page list must be first. The buffer pages |
| 314 | * are allocated in cache lines, which means that each buffer |
| 315 | * page will be at the beginning of a cache line, and thus |
| 316 | * the least significant bits will be zero. We use this to |
| 317 | * add flags in the list struct pointers, to make the ring buffer |
| 318 | * lockless. |
| 319 | */ |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 320 | struct buffer_page { |
Steven Rostedt | 778c55d | 2009-05-01 18:44:45 -0400 | [diff] [blame] | 321 | struct list_head list; /* list of buffer pages */ |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 322 | local_t write; /* index for next write */ |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 323 | unsigned read; /* index for next read */ |
Steven Rostedt | 778c55d | 2009-05-01 18:44:45 -0400 | [diff] [blame] | 324 | local_t entries; /* entries on this page */ |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 325 | unsigned long real_end; /* real end of data */ |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 326 | struct buffer_data_page *page; /* Actual data page */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 327 | }; |
| 328 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 329 | /* |
| 330 | * The buffer page counters, write and entries, must be reset |
| 331 | * atomically when crossing page boundaries. To synchronize this |
| 332 | * update, two counters are inserted into the number. One is |
| 333 | * the actual counter for the write position or count on the page. |
| 334 | * |
| 335 | * The other is a counter of updaters. Before an update happens |
| 336 | * the update partition of the counter is incremented. This will |
| 337 | * allow the updater to update the counter atomically. |
| 338 | * |
| 339 | * The counter is 20 bits, and the state data is 12. |
| 340 | */ |
| 341 | #define RB_WRITE_MASK 0xfffff |
| 342 | #define RB_WRITE_INTCNT (1 << 20) |
| 343 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 344 | static void rb_init_page(struct buffer_data_page *bpage) |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 345 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 346 | local_set(&bpage->commit, 0); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 349 | /* |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 350 | * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing |
| 351 | * this issue out. |
| 352 | */ |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 353 | static void free_buffer_page(struct buffer_page *bpage) |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 354 | { |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 355 | free_page((unsigned long)bpage->page); |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 356 | kfree(bpage); |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 360 | * We need to fit the time_stamp delta into 27 bits. |
| 361 | */ |
| 362 | static inline int test_time_stamp(u64 delta) |
| 363 | { |
| 364 | if (delta & TS_DELTA_TEST) |
| 365 | return 1; |
| 366 | return 0; |
| 367 | } |
| 368 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 369 | #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 370 | |
Steven Rostedt | be957c4 | 2009-05-11 14:42:53 -0400 | [diff] [blame] | 371 | /* Max payload is BUF_PAGE_SIZE - header (8bytes) */ |
| 372 | #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2)) |
| 373 | |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 374 | int ring_buffer_print_page_header(struct trace_seq *s) |
| 375 | { |
| 376 | struct buffer_data_page field; |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 377 | |
Steven Rostedt (Red Hat) | c0cd93a | 2014-11-12 11:49:00 -0500 | [diff] [blame] | 378 | trace_seq_printf(s, "\tfield: u64 timestamp;\t" |
| 379 | "offset:0;\tsize:%u;\tsigned:%u;\n", |
| 380 | (unsigned int)sizeof(field.time_stamp), |
| 381 | (unsigned int)is_signed_type(u64)); |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 382 | |
Steven Rostedt (Red Hat) | c0cd93a | 2014-11-12 11:49:00 -0500 | [diff] [blame] | 383 | trace_seq_printf(s, "\tfield: local_t commit;\t" |
| 384 | "offset:%u;\tsize:%u;\tsigned:%u;\n", |
| 385 | (unsigned int)offsetof(typeof(field), commit), |
| 386 | (unsigned int)sizeof(field.commit), |
| 387 | (unsigned int)is_signed_type(long)); |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 388 | |
Steven Rostedt (Red Hat) | c0cd93a | 2014-11-12 11:49:00 -0500 | [diff] [blame] | 389 | trace_seq_printf(s, "\tfield: int overwrite;\t" |
| 390 | "offset:%u;\tsize:%u;\tsigned:%u;\n", |
| 391 | (unsigned int)offsetof(typeof(field), commit), |
| 392 | 1, |
| 393 | (unsigned int)is_signed_type(long)); |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 394 | |
Steven Rostedt (Red Hat) | c0cd93a | 2014-11-12 11:49:00 -0500 | [diff] [blame] | 395 | trace_seq_printf(s, "\tfield: char data;\t" |
| 396 | "offset:%u;\tsize:%u;\tsigned:%u;\n", |
| 397 | (unsigned int)offsetof(typeof(field), data), |
| 398 | (unsigned int)BUF_PAGE_SIZE, |
| 399 | (unsigned int)is_signed_type(char)); |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 400 | |
Steven Rostedt (Red Hat) | c0cd93a | 2014-11-12 11:49:00 -0500 | [diff] [blame] | 401 | return !trace_seq_has_overflowed(s); |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 402 | } |
| 403 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 404 | struct rb_irq_work { |
| 405 | struct irq_work work; |
| 406 | wait_queue_head_t waiters; |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 407 | wait_queue_head_t full_waiters; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 408 | bool waiters_pending; |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 409 | bool full_waiters_pending; |
| 410 | bool wakeup_full; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 411 | }; |
| 412 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 413 | /* |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 414 | * Structure to hold event state and handle nested events. |
| 415 | */ |
| 416 | struct rb_event_info { |
| 417 | u64 ts; |
| 418 | u64 delta; |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 419 | u64 before; |
| 420 | u64 after; |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 421 | unsigned long length; |
| 422 | struct buffer_page *tail_page; |
| 423 | int add_timestamp; |
| 424 | }; |
| 425 | |
| 426 | /* |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 427 | * Used for the add_timestamp |
| 428 | * NONE |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 429 | * EXTEND - wants a time extend |
| 430 | * ABSOLUTE - the buffer requests all events to have absolute time stamps |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 431 | * FORCE - force a full time stamp. |
| 432 | */ |
| 433 | enum { |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 434 | RB_ADD_STAMP_NONE = 0, |
| 435 | RB_ADD_STAMP_EXTEND = BIT(1), |
| 436 | RB_ADD_STAMP_ABSOLUTE = BIT(2), |
| 437 | RB_ADD_STAMP_FORCE = BIT(3) |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 438 | }; |
| 439 | /* |
Steven Rostedt (Red Hat) | a497adb | 2015-05-29 10:32:28 -0400 | [diff] [blame] | 440 | * Used for which event context the event is in. |
Steven Rostedt (VMware) | b02414c | 2020-11-02 15:31:27 -0500 | [diff] [blame] | 441 | * TRANSITION = 0 |
| 442 | * NMI = 1 |
| 443 | * IRQ = 2 |
| 444 | * SOFTIRQ = 3 |
| 445 | * NORMAL = 4 |
Steven Rostedt (Red Hat) | a497adb | 2015-05-29 10:32:28 -0400 | [diff] [blame] | 446 | * |
| 447 | * See trace_recursive_lock() comment below for more details. |
| 448 | */ |
| 449 | enum { |
Steven Rostedt (VMware) | b02414c | 2020-11-02 15:31:27 -0500 | [diff] [blame] | 450 | RB_CTX_TRANSITION, |
Steven Rostedt (Red Hat) | a497adb | 2015-05-29 10:32:28 -0400 | [diff] [blame] | 451 | RB_CTX_NMI, |
| 452 | RB_CTX_IRQ, |
| 453 | RB_CTX_SOFTIRQ, |
| 454 | RB_CTX_NORMAL, |
| 455 | RB_CTX_MAX |
| 456 | }; |
| 457 | |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 458 | #if BITS_PER_LONG == 32 |
| 459 | #define RB_TIME_32 |
| 460 | #endif |
| 461 | |
| 462 | /* To test on 64 bit machines */ |
| 463 | //#define RB_TIME_32 |
| 464 | |
| 465 | #ifdef RB_TIME_32 |
| 466 | |
| 467 | struct rb_time_struct { |
| 468 | local_t cnt; |
| 469 | local_t top; |
| 470 | local_t bottom; |
| 471 | }; |
| 472 | #else |
| 473 | #include <asm/local64.h> |
| 474 | struct rb_time_struct { |
| 475 | local64_t time; |
| 476 | }; |
| 477 | #endif |
| 478 | typedef struct rb_time_struct rb_time_t; |
| 479 | |
Steven Rostedt (VMware) | 8672e494 | 2021-03-16 12:41:02 -0400 | [diff] [blame] | 480 | #define MAX_NEST 5 |
| 481 | |
Steven Rostedt (Red Hat) | a497adb | 2015-05-29 10:32:28 -0400 | [diff] [blame] | 482 | /* |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 483 | * head_page == tail_page && head == tail then buffer is empty. |
| 484 | */ |
| 485 | struct ring_buffer_per_cpu { |
| 486 | int cpu; |
Richard Kennedy | 985023d | 2010-03-25 11:27:36 +0000 | [diff] [blame] | 487 | atomic_t record_disabled; |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 488 | atomic_t resize_disabled; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 489 | struct trace_buffer *buffer; |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 490 | raw_spinlock_t reader_lock; /* serialize readers */ |
Thomas Gleixner | 445c895 | 2009-12-02 19:49:50 +0100 | [diff] [blame] | 491 | arch_spinlock_t lock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 492 | struct lock_class_key lock_key; |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 493 | struct buffer_data_page *free_page; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 494 | unsigned long nr_pages; |
Steven Rostedt (Red Hat) | 58a09ec | 2015-05-27 10:27:47 -0400 | [diff] [blame] | 495 | unsigned int current_context; |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 496 | struct list_head *pages; |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 497 | struct buffer_page *head_page; /* read from head */ |
| 498 | struct buffer_page *tail_page; /* write to tail */ |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 499 | struct buffer_page *commit_page; /* committed pages */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 500 | struct buffer_page *reader_page; |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 501 | unsigned long lost_events; |
| 502 | unsigned long last_overrun; |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 503 | unsigned long nest; |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 504 | local_t entries_bytes; |
Steven Rostedt | e4906ef | 2009-04-30 20:49:44 -0400 | [diff] [blame] | 505 | local_t entries; |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 506 | local_t overrun; |
| 507 | local_t commit_overrun; |
| 508 | local_t dropped_events; |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 509 | local_t committing; |
| 510 | local_t commits; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 511 | local_t pages_touched; |
| 512 | local_t pages_read; |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 513 | long last_pages_touch; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 514 | size_t shortest_full; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 515 | unsigned long read; |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 516 | unsigned long read_bytes; |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 517 | rb_time_t write_stamp; |
| 518 | rb_time_t before_stamp; |
Steven Rostedt (VMware) | 8672e494 | 2021-03-16 12:41:02 -0400 | [diff] [blame] | 519 | u64 event_stamp[MAX_NEST]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 520 | u64 read_stamp; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 521 | /* ring buffer pages to update, > 0 to add, < 0 to remove */ |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 522 | long nr_pages_to_update; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 523 | struct list_head new_pages; /* new pages to add */ |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 524 | struct work_struct update_pages_work; |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 525 | struct completion update_done; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 526 | |
| 527 | struct rb_irq_work irq_work; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 528 | }; |
| 529 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 530 | struct trace_buffer { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 531 | unsigned flags; |
| 532 | int cpus; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 533 | atomic_t record_disabled; |
Arnaldo Carvalho de Melo | 00f62f6 | 2009-02-09 17:04:06 -0200 | [diff] [blame] | 534 | cpumask_var_t cpumask; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 535 | |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 536 | struct lock_class_key *reader_lock_key; |
| 537 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 538 | struct mutex mutex; |
| 539 | |
| 540 | struct ring_buffer_per_cpu **buffers; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 541 | |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 542 | struct hlist_node node; |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 543 | u64 (*clock)(void); |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 544 | |
| 545 | struct rb_irq_work irq_work; |
Tom Zanussi | 00b4145 | 2018-01-15 20:51:39 -0600 | [diff] [blame] | 546 | bool time_stamp_abs; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 547 | }; |
| 548 | |
| 549 | struct ring_buffer_iter { |
| 550 | struct ring_buffer_per_cpu *cpu_buffer; |
| 551 | unsigned long head; |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 552 | unsigned long next_event; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 553 | struct buffer_page *head_page; |
Steven Rostedt | 492a74f | 2010-01-25 15:17:47 -0500 | [diff] [blame] | 554 | struct buffer_page *cache_reader_page; |
| 555 | unsigned long cache_read; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 556 | u64 read_stamp; |
Steven Rostedt (VMware) | 28e3fc5 | 2020-03-17 17:32:26 -0400 | [diff] [blame] | 557 | u64 page_stamp; |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 558 | struct ring_buffer_event *event; |
Steven Rostedt (VMware) | c9b7a4a | 2020-03-17 17:32:32 -0400 | [diff] [blame] | 559 | int missed_events; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 560 | }; |
| 561 | |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 562 | #ifdef RB_TIME_32 |
| 563 | |
| 564 | /* |
| 565 | * On 32 bit machines, local64_t is very expensive. As the ring |
| 566 | * buffer doesn't need all the features of a true 64 bit atomic, |
| 567 | * on 32 bit, it uses these functions (64 still uses local64_t). |
| 568 | * |
| 569 | * For the ring buffer, 64 bit required operations for the time is |
| 570 | * the following: |
| 571 | * |
| 572 | * - Only need 59 bits (uses 60 to make it even). |
| 573 | * - Reads may fail if it interrupted a modification of the time stamp. |
| 574 | * It will succeed if it did not interrupt another write even if |
| 575 | * the read itself is interrupted by a write. |
| 576 | * It returns whether it was successful or not. |
| 577 | * |
| 578 | * - Writes always succeed and will overwrite other writes and writes |
| 579 | * that were done by events interrupting the current write. |
| 580 | * |
| 581 | * - A write followed by a read of the same time stamp will always succeed, |
| 582 | * but may not contain the same value. |
| 583 | * |
| 584 | * - A cmpxchg will fail if it interrupted another write or cmpxchg. |
| 585 | * Other than that, it acts like a normal cmpxchg. |
| 586 | * |
| 587 | * The 60 bit time stamp is broken up by 30 bits in a top and bottom half |
| 588 | * (bottom being the least significant 30 bits of the 60 bit time stamp). |
| 589 | * |
| 590 | * The two most significant bits of each half holds a 2 bit counter (0-3). |
| 591 | * Each update will increment this counter by one. |
| 592 | * When reading the top and bottom, if the two counter bits match then the |
| 593 | * top and bottom together make a valid 60 bit number. |
| 594 | */ |
| 595 | #define RB_TIME_SHIFT 30 |
| 596 | #define RB_TIME_VAL_MASK ((1 << RB_TIME_SHIFT) - 1) |
| 597 | |
| 598 | static inline int rb_time_cnt(unsigned long val) |
| 599 | { |
| 600 | return (val >> RB_TIME_SHIFT) & 3; |
| 601 | } |
| 602 | |
| 603 | static inline u64 rb_time_val(unsigned long top, unsigned long bottom) |
| 604 | { |
| 605 | u64 val; |
| 606 | |
| 607 | val = top & RB_TIME_VAL_MASK; |
| 608 | val <<= RB_TIME_SHIFT; |
| 609 | val |= bottom & RB_TIME_VAL_MASK; |
| 610 | |
| 611 | return val; |
| 612 | } |
| 613 | |
| 614 | static inline bool __rb_time_read(rb_time_t *t, u64 *ret, unsigned long *cnt) |
| 615 | { |
| 616 | unsigned long top, bottom; |
| 617 | unsigned long c; |
| 618 | |
| 619 | /* |
| 620 | * If the read is interrupted by a write, then the cnt will |
| 621 | * be different. Loop until both top and bottom have been read |
| 622 | * without interruption. |
| 623 | */ |
| 624 | do { |
| 625 | c = local_read(&t->cnt); |
| 626 | top = local_read(&t->top); |
| 627 | bottom = local_read(&t->bottom); |
| 628 | } while (c != local_read(&t->cnt)); |
| 629 | |
| 630 | *cnt = rb_time_cnt(top); |
| 631 | |
| 632 | /* If top and bottom counts don't match, this interrupted a write */ |
| 633 | if (*cnt != rb_time_cnt(bottom)) |
| 634 | return false; |
| 635 | |
| 636 | *ret = rb_time_val(top, bottom); |
| 637 | return true; |
| 638 | } |
| 639 | |
| 640 | static bool rb_time_read(rb_time_t *t, u64 *ret) |
| 641 | { |
| 642 | unsigned long cnt; |
| 643 | |
| 644 | return __rb_time_read(t, ret, &cnt); |
| 645 | } |
| 646 | |
| 647 | static inline unsigned long rb_time_val_cnt(unsigned long val, unsigned long cnt) |
| 648 | { |
| 649 | return (val & RB_TIME_VAL_MASK) | ((cnt & 3) << RB_TIME_SHIFT); |
| 650 | } |
| 651 | |
| 652 | static inline void rb_time_split(u64 val, unsigned long *top, unsigned long *bottom) |
| 653 | { |
| 654 | *top = (unsigned long)((val >> RB_TIME_SHIFT) & RB_TIME_VAL_MASK); |
| 655 | *bottom = (unsigned long)(val & RB_TIME_VAL_MASK); |
| 656 | } |
| 657 | |
| 658 | static inline void rb_time_val_set(local_t *t, unsigned long val, unsigned long cnt) |
| 659 | { |
| 660 | val = rb_time_val_cnt(val, cnt); |
| 661 | local_set(t, val); |
| 662 | } |
| 663 | |
| 664 | static void rb_time_set(rb_time_t *t, u64 val) |
| 665 | { |
| 666 | unsigned long cnt, top, bottom; |
| 667 | |
| 668 | rb_time_split(val, &top, &bottom); |
| 669 | |
| 670 | /* Writes always succeed with a valid number even if it gets interrupted. */ |
| 671 | do { |
| 672 | cnt = local_inc_return(&t->cnt); |
| 673 | rb_time_val_set(&t->top, top, cnt); |
| 674 | rb_time_val_set(&t->bottom, bottom, cnt); |
| 675 | } while (cnt != local_read(&t->cnt)); |
| 676 | } |
| 677 | |
| 678 | static inline bool |
| 679 | rb_time_read_cmpxchg(local_t *l, unsigned long expect, unsigned long set) |
| 680 | { |
| 681 | unsigned long ret; |
| 682 | |
| 683 | ret = local_cmpxchg(l, expect, set); |
| 684 | return ret == expect; |
| 685 | } |
| 686 | |
| 687 | static int rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set) |
| 688 | { |
| 689 | unsigned long cnt, top, bottom; |
| 690 | unsigned long cnt2, top2, bottom2; |
| 691 | u64 val; |
| 692 | |
| 693 | /* The cmpxchg always fails if it interrupted an update */ |
| 694 | if (!__rb_time_read(t, &val, &cnt2)) |
| 695 | return false; |
| 696 | |
| 697 | if (val != expect) |
| 698 | return false; |
| 699 | |
| 700 | cnt = local_read(&t->cnt); |
| 701 | if ((cnt & 3) != cnt2) |
| 702 | return false; |
| 703 | |
| 704 | cnt2 = cnt + 1; |
| 705 | |
| 706 | rb_time_split(val, &top, &bottom); |
| 707 | top = rb_time_val_cnt(top, cnt); |
| 708 | bottom = rb_time_val_cnt(bottom, cnt); |
| 709 | |
| 710 | rb_time_split(set, &top2, &bottom2); |
| 711 | top2 = rb_time_val_cnt(top2, cnt2); |
| 712 | bottom2 = rb_time_val_cnt(bottom2, cnt2); |
| 713 | |
| 714 | if (!rb_time_read_cmpxchg(&t->cnt, cnt, cnt2)) |
| 715 | return false; |
| 716 | if (!rb_time_read_cmpxchg(&t->top, top, top2)) |
| 717 | return false; |
| 718 | if (!rb_time_read_cmpxchg(&t->bottom, bottom, bottom2)) |
| 719 | return false; |
| 720 | return true; |
| 721 | } |
| 722 | |
| 723 | #else /* 64 bits */ |
| 724 | |
| 725 | /* local64_t always succeeds */ |
| 726 | |
| 727 | static inline bool rb_time_read(rb_time_t *t, u64 *ret) |
| 728 | { |
| 729 | *ret = local64_read(&t->time); |
| 730 | return true; |
| 731 | } |
| 732 | static void rb_time_set(rb_time_t *t, u64 val) |
| 733 | { |
| 734 | local64_set(&t->time, val); |
| 735 | } |
| 736 | |
| 737 | static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set) |
| 738 | { |
| 739 | u64 val; |
| 740 | val = local64_cmpxchg(&t->time, expect, set); |
| 741 | return val == expect; |
| 742 | } |
| 743 | #endif |
| 744 | |
Steven Rostedt (VMware) | efe6196 | 2021-03-16 12:41:04 -0400 | [diff] [blame^] | 745 | static inline u64 rb_time_stamp(struct trace_buffer *buffer); |
| 746 | |
| 747 | /** |
| 748 | * ring_buffer_event_time_stamp - return the event's current time stamp |
| 749 | * @buffer: The buffer that the event is on |
| 750 | * @event: the event to get the time stamp of |
| 751 | * |
| 752 | * Note, this must be called after @event is reserved, and before it is |
| 753 | * committed to the ring buffer. And must be called from the same |
| 754 | * context where the event was reserved (normal, softirq, irq, etc). |
| 755 | * |
| 756 | * Returns the time stamp associated with the current event. |
| 757 | * If the event has an extended time stamp, then that is used as |
| 758 | * the time stamp to return. |
| 759 | * In the highly unlikely case that the event was nested more than |
| 760 | * the max nesting, then the write_stamp of the buffer is returned, |
| 761 | * otherwise current time is returned, but that really neither of |
| 762 | * the last two cases should ever happen. |
| 763 | */ |
| 764 | u64 ring_buffer_event_time_stamp(struct trace_buffer *buffer, |
| 765 | struct ring_buffer_event *event) |
| 766 | { |
| 767 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[smp_processor_id()]; |
| 768 | unsigned int nest; |
| 769 | u64 ts; |
| 770 | |
| 771 | /* If the event includes an absolute time, then just use that */ |
| 772 | if (event->type_len == RINGBUF_TYPE_TIME_STAMP) |
| 773 | return rb_event_time_stamp(event); |
| 774 | |
| 775 | /* Read the current saved nesting level time stamp */ |
| 776 | nest = local_read(&cpu_buffer->committing) - 1; |
| 777 | if (likely(nest < MAX_NEST)) |
| 778 | return cpu_buffer->event_stamp[nest]; |
| 779 | |
| 780 | WARN_ON_ONCE(1); |
| 781 | |
| 782 | /* Can only fail on 32 bit */ |
| 783 | if (!rb_time_read(&cpu_buffer->write_stamp, &ts)) |
| 784 | /* Screw it, just read the current time */ |
| 785 | ts = rb_time_stamp(cpu_buffer->buffer); |
| 786 | |
| 787 | return ts; |
| 788 | } |
| 789 | |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 790 | /** |
| 791 | * ring_buffer_nr_pages - get the number of buffer pages in the ring buffer |
| 792 | * @buffer: The ring_buffer to get the number of pages from |
| 793 | * @cpu: The cpu of the ring_buffer to get the number of pages from |
| 794 | * |
| 795 | * Returns the number of pages used by a per_cpu buffer of the ring buffer. |
| 796 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 797 | size_t ring_buffer_nr_pages(struct trace_buffer *buffer, int cpu) |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 798 | { |
| 799 | return buffer->buffers[cpu]->nr_pages; |
| 800 | } |
| 801 | |
| 802 | /** |
| 803 | * ring_buffer_nr_pages_dirty - get the number of used pages in the ring buffer |
| 804 | * @buffer: The ring_buffer to get the number of pages from |
| 805 | * @cpu: The cpu of the ring_buffer to get the number of pages from |
| 806 | * |
| 807 | * Returns the number of pages that have content in the ring buffer. |
| 808 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 809 | size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu) |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 810 | { |
| 811 | size_t read; |
| 812 | size_t cnt; |
| 813 | |
| 814 | read = local_read(&buffer->buffers[cpu]->pages_read); |
| 815 | cnt = local_read(&buffer->buffers[cpu]->pages_touched); |
| 816 | /* The reader can read an empty page, but not more than that */ |
| 817 | if (cnt < read) { |
| 818 | WARN_ON_ONCE(read > cnt + 1); |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | return cnt - read; |
| 823 | } |
| 824 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 825 | /* |
| 826 | * rb_wake_up_waiters - wake up tasks waiting for ring buffer input |
| 827 | * |
| 828 | * Schedules a delayed work to wake up any task that is blocked on the |
| 829 | * ring buffer waiters queue. |
| 830 | */ |
| 831 | static void rb_wake_up_waiters(struct irq_work *work) |
| 832 | { |
| 833 | struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work); |
| 834 | |
| 835 | wake_up_all(&rbwork->waiters); |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 836 | if (rbwork->wakeup_full) { |
| 837 | rbwork->wakeup_full = false; |
| 838 | wake_up_all(&rbwork->full_waiters); |
| 839 | } |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /** |
| 843 | * ring_buffer_wait - wait for input to the ring buffer |
| 844 | * @buffer: buffer to wait on |
| 845 | * @cpu: the cpu buffer to wait on |
Qiujun Huang | e1981f7 | 2020-10-17 17:52:46 +0800 | [diff] [blame] | 846 | * @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 847 | * |
| 848 | * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon |
| 849 | * as data is added to any of the @buffer's cpu buffers. Otherwise |
| 850 | * it will wait for data to be added to a specific cpu buffer. |
| 851 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 852 | int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full) |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 853 | { |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 854 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 855 | DEFINE_WAIT(wait); |
| 856 | struct rb_irq_work *work; |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 857 | int ret = 0; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 858 | |
| 859 | /* |
| 860 | * Depending on what the caller is waiting for, either any |
| 861 | * data in any cpu buffer, or a specific buffer, put the |
| 862 | * caller on the appropriate wait queue. |
| 863 | */ |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 864 | if (cpu == RING_BUFFER_ALL_CPUS) { |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 865 | work = &buffer->irq_work; |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 866 | /* Full only makes sense on per cpu reads */ |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 867 | full = 0; |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 868 | } else { |
Steven Rostedt (Red Hat) | 8b8b368 | 2014-06-10 09:46:00 -0400 | [diff] [blame] | 869 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 870 | return -ENODEV; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 871 | cpu_buffer = buffer->buffers[cpu]; |
| 872 | work = &cpu_buffer->irq_work; |
| 873 | } |
| 874 | |
| 875 | |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 876 | while (true) { |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 877 | if (full) |
| 878 | prepare_to_wait(&work->full_waiters, &wait, TASK_INTERRUPTIBLE); |
| 879 | else |
| 880 | prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE); |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 881 | |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 882 | /* |
| 883 | * The events can happen in critical sections where |
| 884 | * checking a work queue can cause deadlocks. |
| 885 | * After adding a task to the queue, this flag is set |
| 886 | * only to notify events to try to wake up the queue |
| 887 | * using irq_work. |
| 888 | * |
| 889 | * We don't clear it even if the buffer is no longer |
| 890 | * empty. The flag only causes the next event to run |
| 891 | * irq_work to do the work queue wake up. The worse |
| 892 | * that can happen if we race with !trace_empty() is that |
| 893 | * an event will cause an irq_work to try to wake up |
| 894 | * an empty queue. |
| 895 | * |
| 896 | * There's no reason to protect this flag either, as |
| 897 | * the work queue and irq_work logic will do the necessary |
| 898 | * synchronization for the wake ups. The only thing |
| 899 | * that is necessary is that the wake up happens after |
| 900 | * a task has been queued. It's OK for spurious wake ups. |
| 901 | */ |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 902 | if (full) |
| 903 | work->full_waiters_pending = true; |
| 904 | else |
| 905 | work->waiters_pending = true; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 906 | |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 907 | if (signal_pending(current)) { |
| 908 | ret = -EINTR; |
| 909 | break; |
| 910 | } |
| 911 | |
| 912 | if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) |
| 913 | break; |
| 914 | |
| 915 | if (cpu != RING_BUFFER_ALL_CPUS && |
| 916 | !ring_buffer_empty_cpu(buffer, cpu)) { |
| 917 | unsigned long flags; |
| 918 | bool pagebusy; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 919 | size_t nr_pages; |
| 920 | size_t dirty; |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 921 | |
| 922 | if (!full) |
| 923 | break; |
| 924 | |
| 925 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 926 | pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 927 | nr_pages = cpu_buffer->nr_pages; |
| 928 | dirty = ring_buffer_nr_dirty_pages(buffer, cpu); |
| 929 | if (!cpu_buffer->shortest_full || |
| 930 | cpu_buffer->shortest_full < full) |
| 931 | cpu_buffer->shortest_full = full; |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 932 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 933 | if (!pagebusy && |
| 934 | (!nr_pages || (dirty * 100) > full * nr_pages)) |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 935 | break; |
| 936 | } |
| 937 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 938 | schedule(); |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 939 | } |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 940 | |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 941 | if (full) |
| 942 | finish_wait(&work->full_waiters, &wait); |
| 943 | else |
| 944 | finish_wait(&work->waiters, &wait); |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 945 | |
| 946 | return ret; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | /** |
| 950 | * ring_buffer_poll_wait - poll on buffer input |
| 951 | * @buffer: buffer to wait on |
| 952 | * @cpu: the cpu buffer to wait on |
| 953 | * @filp: the file descriptor |
| 954 | * @poll_table: The poll descriptor |
| 955 | * |
| 956 | * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon |
| 957 | * as data is added to any of the @buffer's cpu buffers. Otherwise |
| 958 | * it will wait for data to be added to a specific cpu buffer. |
| 959 | * |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 960 | * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers, |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 961 | * zero otherwise. |
| 962 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 963 | __poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu, |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 964 | struct file *filp, poll_table *poll_table) |
| 965 | { |
| 966 | struct ring_buffer_per_cpu *cpu_buffer; |
| 967 | struct rb_irq_work *work; |
| 968 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 969 | if (cpu == RING_BUFFER_ALL_CPUS) |
| 970 | work = &buffer->irq_work; |
| 971 | else { |
Steven Rostedt (Red Hat) | 6721cb6 | 2013-05-23 14:21:36 -0400 | [diff] [blame] | 972 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 973 | return -EINVAL; |
| 974 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 975 | cpu_buffer = buffer->buffers[cpu]; |
| 976 | work = &cpu_buffer->irq_work; |
| 977 | } |
| 978 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 979 | poll_wait(filp, &work->waiters, poll_table); |
Josef Bacik | 4ce97db | 2014-08-25 13:59:41 -0400 | [diff] [blame] | 980 | work->waiters_pending = true; |
| 981 | /* |
| 982 | * There's a tight race between setting the waiters_pending and |
| 983 | * checking if the ring buffer is empty. Once the waiters_pending bit |
| 984 | * is set, the next event will wake the task up, but we can get stuck |
| 985 | * if there's only a single event in. |
| 986 | * |
| 987 | * FIXME: Ideally, we need a memory barrier on the writer side as well, |
| 988 | * but adding a memory barrier to all events will cause too much of a |
| 989 | * performance hit in the fast path. We only need a memory barrier when |
| 990 | * the buffer goes from empty to having content. But as this race is |
| 991 | * extremely small, and it's not a problem if another event comes in, we |
| 992 | * will fix it later. |
| 993 | */ |
| 994 | smp_mb(); |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 995 | |
| 996 | if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) || |
| 997 | (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu))) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 998 | return EPOLLIN | EPOLLRDNORM; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 999 | return 0; |
| 1000 | } |
| 1001 | |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 1002 | /* buffer may be either ring_buffer or ring_buffer_per_cpu */ |
Steven Rostedt | 077c540 | 2009-09-03 19:53:46 -0400 | [diff] [blame] | 1003 | #define RB_WARN_ON(b, cond) \ |
| 1004 | ({ \ |
| 1005 | int _____ret = unlikely(cond); \ |
| 1006 | if (_____ret) { \ |
| 1007 | if (__same_type(*(b), struct ring_buffer_per_cpu)) { \ |
| 1008 | struct ring_buffer_per_cpu *__b = \ |
| 1009 | (void *)b; \ |
| 1010 | atomic_inc(&__b->buffer->record_disabled); \ |
| 1011 | } else \ |
| 1012 | atomic_inc(&b->record_disabled); \ |
| 1013 | WARN_ON(1); \ |
| 1014 | } \ |
| 1015 | _____ret; \ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1016 | }) |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 1017 | |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 1018 | /* Up this if you want to test the TIME_EXTENTS and normalization */ |
| 1019 | #define DEBUG_SHIFT 0 |
| 1020 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1021 | static inline u64 rb_time_stamp(struct trace_buffer *buffer) |
Steven Rostedt | 88eb012 | 2009-05-11 16:28:23 -0400 | [diff] [blame] | 1022 | { |
Steven Rostedt (VMware) | bbeba3e | 2020-06-30 13:05:29 -0400 | [diff] [blame] | 1023 | u64 ts; |
| 1024 | |
| 1025 | /* Skip retpolines :-( */ |
| 1026 | if (IS_ENABLED(CONFIG_RETPOLINE) && likely(buffer->clock == trace_clock_local)) |
| 1027 | ts = trace_clock_local(); |
| 1028 | else |
| 1029 | ts = buffer->clock(); |
| 1030 | |
Steven Rostedt | 88eb012 | 2009-05-11 16:28:23 -0400 | [diff] [blame] | 1031 | /* shift to debug/test normalization and TIME_EXTENTS */ |
Steven Rostedt (VMware) | bbeba3e | 2020-06-30 13:05:29 -0400 | [diff] [blame] | 1032 | return ts << DEBUG_SHIFT; |
Steven Rostedt | 88eb012 | 2009-05-11 16:28:23 -0400 | [diff] [blame] | 1033 | } |
| 1034 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1035 | u64 ring_buffer_time_stamp(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 1036 | { |
| 1037 | u64 time; |
| 1038 | |
| 1039 | preempt_disable_notrace(); |
Jiri Olsa | 6d3f1e1 | 2009-10-23 19:36:19 -0400 | [diff] [blame] | 1040 | time = rb_time_stamp(buffer); |
Peter Zijlstra | d6097c9 | 2019-04-23 22:03:18 +0200 | [diff] [blame] | 1041 | preempt_enable_notrace(); |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 1042 | |
| 1043 | return time; |
| 1044 | } |
| 1045 | EXPORT_SYMBOL_GPL(ring_buffer_time_stamp); |
| 1046 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1047 | void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer, |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 1048 | int cpu, u64 *ts) |
| 1049 | { |
| 1050 | /* Just stupid testing the normalize function and deltas */ |
| 1051 | *ts >>= DEBUG_SHIFT; |
| 1052 | } |
| 1053 | EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp); |
| 1054 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1055 | /* |
| 1056 | * Making the ring buffer lockless makes things tricky. |
| 1057 | * Although writes only happen on the CPU that they are on, |
| 1058 | * and they only need to worry about interrupts. Reads can |
| 1059 | * happen on any CPU. |
| 1060 | * |
| 1061 | * The reader page is always off the ring buffer, but when the |
| 1062 | * reader finishes with a page, it needs to swap its page with |
| 1063 | * a new one from the buffer. The reader needs to take from |
| 1064 | * the head (writes go to the tail). But if a writer is in overwrite |
| 1065 | * mode and wraps, it must push the head page forward. |
| 1066 | * |
| 1067 | * Here lies the problem. |
| 1068 | * |
| 1069 | * The reader must be careful to replace only the head page, and |
| 1070 | * not another one. As described at the top of the file in the |
| 1071 | * ASCII art, the reader sets its old page to point to the next |
| 1072 | * page after head. It then sets the page after head to point to |
| 1073 | * the old reader page. But if the writer moves the head page |
| 1074 | * during this operation, the reader could end up with the tail. |
| 1075 | * |
| 1076 | * We use cmpxchg to help prevent this race. We also do something |
| 1077 | * special with the page before head. We set the LSB to 1. |
| 1078 | * |
| 1079 | * When the writer must push the page forward, it will clear the |
| 1080 | * bit that points to the head page, move the head, and then set |
| 1081 | * the bit that points to the new head page. |
| 1082 | * |
| 1083 | * We also don't want an interrupt coming in and moving the head |
| 1084 | * page on another writer. Thus we use the second LSB to catch |
| 1085 | * that too. Thus: |
| 1086 | * |
| 1087 | * head->list->prev->next bit 1 bit 0 |
| 1088 | * ------- ------- |
| 1089 | * Normal page 0 0 |
| 1090 | * Points to head page 0 1 |
| 1091 | * New head page 1 0 |
| 1092 | * |
| 1093 | * Note we can not trust the prev pointer of the head page, because: |
| 1094 | * |
| 1095 | * +----+ +-----+ +-----+ |
| 1096 | * | |------>| T |---X--->| N | |
| 1097 | * | |<------| | | | |
| 1098 | * +----+ +-----+ +-----+ |
| 1099 | * ^ ^ | |
| 1100 | * | +-----+ | | |
| 1101 | * +----------| R |----------+ | |
| 1102 | * | |<-----------+ |
| 1103 | * +-----+ |
| 1104 | * |
| 1105 | * Key: ---X--> HEAD flag set in pointer |
| 1106 | * T Tail page |
| 1107 | * R Reader page |
| 1108 | * N Next page |
| 1109 | * |
| 1110 | * (see __rb_reserve_next() to see where this happens) |
| 1111 | * |
| 1112 | * What the above shows is that the reader just swapped out |
| 1113 | * the reader page with a page in the buffer, but before it |
| 1114 | * could make the new header point back to the new page added |
| 1115 | * it was preempted by a writer. The writer moved forward onto |
| 1116 | * the new page added by the reader and is about to move forward |
| 1117 | * again. |
| 1118 | * |
| 1119 | * You can see, it is legitimate for the previous pointer of |
| 1120 | * the head (or any page) not to point back to itself. But only |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 1121 | * temporarily. |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1122 | */ |
| 1123 | |
| 1124 | #define RB_PAGE_NORMAL 0UL |
| 1125 | #define RB_PAGE_HEAD 1UL |
| 1126 | #define RB_PAGE_UPDATE 2UL |
| 1127 | |
| 1128 | |
| 1129 | #define RB_FLAG_MASK 3UL |
| 1130 | |
| 1131 | /* PAGE_MOVED is not part of the mask */ |
| 1132 | #define RB_PAGE_MOVED 4UL |
| 1133 | |
| 1134 | /* |
| 1135 | * rb_list_head - remove any bit |
| 1136 | */ |
| 1137 | static struct list_head *rb_list_head(struct list_head *list) |
| 1138 | { |
| 1139 | unsigned long val = (unsigned long)list; |
| 1140 | |
| 1141 | return (struct list_head *)(val & ~RB_FLAG_MASK); |
| 1142 | } |
| 1143 | |
| 1144 | /* |
Jiri Olsa | 6d3f1e1 | 2009-10-23 19:36:19 -0400 | [diff] [blame] | 1145 | * rb_is_head_page - test if the given page is the head page |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1146 | * |
| 1147 | * Because the reader may move the head_page pointer, we can |
| 1148 | * not trust what the head page is (it may be pointing to |
| 1149 | * the reader page). But if the next page is a header page, |
| 1150 | * its flags will be non zero. |
| 1151 | */ |
Jesper Juhl | 42b16b3 | 2011-01-17 00:09:38 +0100 | [diff] [blame] | 1152 | static inline int |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 1153 | rb_is_head_page(struct buffer_page *page, struct list_head *list) |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1154 | { |
| 1155 | unsigned long val; |
| 1156 | |
| 1157 | val = (unsigned long)list->next; |
| 1158 | |
| 1159 | if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list) |
| 1160 | return RB_PAGE_MOVED; |
| 1161 | |
| 1162 | return val & RB_FLAG_MASK; |
| 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | * rb_is_reader_page |
| 1167 | * |
| 1168 | * The unique thing about the reader page, is that, if the |
| 1169 | * writer is ever on it, the previous pointer never points |
| 1170 | * back to the reader page. |
| 1171 | */ |
Yaowei Bai | 06ca320 | 2015-09-29 22:43:31 +0800 | [diff] [blame] | 1172 | static bool rb_is_reader_page(struct buffer_page *page) |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1173 | { |
| 1174 | struct list_head *list = page->list.prev; |
| 1175 | |
| 1176 | return rb_list_head(list->next) != &page->list; |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * rb_set_list_to_head - set a list_head to be pointing to head. |
| 1181 | */ |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 1182 | static void rb_set_list_to_head(struct list_head *list) |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1183 | { |
| 1184 | unsigned long *ptr; |
| 1185 | |
| 1186 | ptr = (unsigned long *)&list->next; |
| 1187 | *ptr |= RB_PAGE_HEAD; |
| 1188 | *ptr &= ~RB_PAGE_UPDATE; |
| 1189 | } |
| 1190 | |
| 1191 | /* |
| 1192 | * rb_head_page_activate - sets up head page |
| 1193 | */ |
| 1194 | static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer) |
| 1195 | { |
| 1196 | struct buffer_page *head; |
| 1197 | |
| 1198 | head = cpu_buffer->head_page; |
| 1199 | if (!head) |
| 1200 | return; |
| 1201 | |
| 1202 | /* |
| 1203 | * Set the previous list pointer to have the HEAD flag. |
| 1204 | */ |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 1205 | rb_set_list_to_head(head->list.prev); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | static void rb_list_head_clear(struct list_head *list) |
| 1209 | { |
| 1210 | unsigned long *ptr = (unsigned long *)&list->next; |
| 1211 | |
| 1212 | *ptr &= ~RB_FLAG_MASK; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 1216 | * rb_head_page_deactivate - clears head page ptr (for free list) |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1217 | */ |
| 1218 | static void |
| 1219 | rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer) |
| 1220 | { |
| 1221 | struct list_head *hd; |
| 1222 | |
| 1223 | /* Go through the whole list and clear any pointers found. */ |
| 1224 | rb_list_head_clear(cpu_buffer->pages); |
| 1225 | |
| 1226 | list_for_each(hd, cpu_buffer->pages) |
| 1227 | rb_list_head_clear(hd); |
| 1228 | } |
| 1229 | |
| 1230 | static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer, |
| 1231 | struct buffer_page *head, |
| 1232 | struct buffer_page *prev, |
| 1233 | int old_flag, int new_flag) |
| 1234 | { |
| 1235 | struct list_head *list; |
| 1236 | unsigned long val = (unsigned long)&head->list; |
| 1237 | unsigned long ret; |
| 1238 | |
| 1239 | list = &prev->list; |
| 1240 | |
| 1241 | val &= ~RB_FLAG_MASK; |
| 1242 | |
Steven Rostedt | 08a4081 | 2009-09-14 09:31:35 -0400 | [diff] [blame] | 1243 | ret = cmpxchg((unsigned long *)&list->next, |
| 1244 | val | old_flag, val | new_flag); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1245 | |
| 1246 | /* check if the reader took the page */ |
| 1247 | if ((ret & ~RB_FLAG_MASK) != val) |
| 1248 | return RB_PAGE_MOVED; |
| 1249 | |
| 1250 | return ret & RB_FLAG_MASK; |
| 1251 | } |
| 1252 | |
| 1253 | static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer, |
| 1254 | struct buffer_page *head, |
| 1255 | struct buffer_page *prev, |
| 1256 | int old_flag) |
| 1257 | { |
| 1258 | return rb_head_page_set(cpu_buffer, head, prev, |
| 1259 | old_flag, RB_PAGE_UPDATE); |
| 1260 | } |
| 1261 | |
| 1262 | static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer, |
| 1263 | struct buffer_page *head, |
| 1264 | struct buffer_page *prev, |
| 1265 | int old_flag) |
| 1266 | { |
| 1267 | return rb_head_page_set(cpu_buffer, head, prev, |
| 1268 | old_flag, RB_PAGE_HEAD); |
| 1269 | } |
| 1270 | |
| 1271 | static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer, |
| 1272 | struct buffer_page *head, |
| 1273 | struct buffer_page *prev, |
| 1274 | int old_flag) |
| 1275 | { |
| 1276 | return rb_head_page_set(cpu_buffer, head, prev, |
| 1277 | old_flag, RB_PAGE_NORMAL); |
| 1278 | } |
| 1279 | |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 1280 | static inline void rb_inc_page(struct buffer_page **bpage) |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1281 | { |
| 1282 | struct list_head *p = rb_list_head((*bpage)->list.next); |
| 1283 | |
| 1284 | *bpage = list_entry(p, struct buffer_page, list); |
| 1285 | } |
| 1286 | |
| 1287 | static struct buffer_page * |
| 1288 | rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer) |
| 1289 | { |
| 1290 | struct buffer_page *head; |
| 1291 | struct buffer_page *page; |
| 1292 | struct list_head *list; |
| 1293 | int i; |
| 1294 | |
| 1295 | if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page)) |
| 1296 | return NULL; |
| 1297 | |
| 1298 | /* sanity check */ |
| 1299 | list = cpu_buffer->pages; |
| 1300 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list)) |
| 1301 | return NULL; |
| 1302 | |
| 1303 | page = head = cpu_buffer->head_page; |
| 1304 | /* |
| 1305 | * It is possible that the writer moves the header behind |
| 1306 | * where we started, and we miss in one loop. |
| 1307 | * A second loop should grab the header, but we'll do |
| 1308 | * three loops just because I'm paranoid. |
| 1309 | */ |
| 1310 | for (i = 0; i < 3; i++) { |
| 1311 | do { |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 1312 | if (rb_is_head_page(page, page->list.prev)) { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1313 | cpu_buffer->head_page = page; |
| 1314 | return page; |
| 1315 | } |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 1316 | rb_inc_page(&page); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1317 | } while (page != head); |
| 1318 | } |
| 1319 | |
| 1320 | RB_WARN_ON(cpu_buffer, 1); |
| 1321 | |
| 1322 | return NULL; |
| 1323 | } |
| 1324 | |
| 1325 | static int rb_head_page_replace(struct buffer_page *old, |
| 1326 | struct buffer_page *new) |
| 1327 | { |
| 1328 | unsigned long *ptr = (unsigned long *)&old->list.prev->next; |
| 1329 | unsigned long val; |
| 1330 | unsigned long ret; |
| 1331 | |
| 1332 | val = *ptr & ~RB_FLAG_MASK; |
| 1333 | val |= RB_PAGE_HEAD; |
| 1334 | |
Steven Rostedt | 08a4081 | 2009-09-14 09:31:35 -0400 | [diff] [blame] | 1335 | ret = cmpxchg(ptr, val, (unsigned long)&new->list); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1336 | |
| 1337 | return ret == val; |
| 1338 | } |
| 1339 | |
| 1340 | /* |
| 1341 | * rb_tail_page_update - move the tail page forward |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1342 | */ |
Steven Rostedt (Red Hat) | 7000498 | 2015-11-17 15:15:19 -0500 | [diff] [blame] | 1343 | static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1344 | struct buffer_page *tail_page, |
| 1345 | struct buffer_page *next_page) |
| 1346 | { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1347 | unsigned long old_entries; |
| 1348 | unsigned long old_write; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1349 | |
| 1350 | /* |
| 1351 | * The tail page now needs to be moved forward. |
| 1352 | * |
| 1353 | * We need to reset the tail page, but without messing |
| 1354 | * with possible erasing of data brought in by interrupts |
| 1355 | * that have moved the tail page and are currently on it. |
| 1356 | * |
| 1357 | * We add a counter to the write field to denote this. |
| 1358 | */ |
| 1359 | old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write); |
| 1360 | old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries); |
| 1361 | |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 1362 | local_inc(&cpu_buffer->pages_touched); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1363 | /* |
| 1364 | * Just make sure we have seen our old_write and synchronize |
| 1365 | * with any interrupts that come in. |
| 1366 | */ |
| 1367 | barrier(); |
| 1368 | |
| 1369 | /* |
| 1370 | * If the tail page is still the same as what we think |
| 1371 | * it is, then it is up to us to update the tail |
| 1372 | * pointer. |
| 1373 | */ |
Steven Rostedt (Red Hat) | 8573636 | 2015-11-17 14:03:11 -0500 | [diff] [blame] | 1374 | if (tail_page == READ_ONCE(cpu_buffer->tail_page)) { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1375 | /* Zero the write counter */ |
| 1376 | unsigned long val = old_write & ~RB_WRITE_MASK; |
| 1377 | unsigned long eval = old_entries & ~RB_WRITE_MASK; |
| 1378 | |
| 1379 | /* |
| 1380 | * This will only succeed if an interrupt did |
| 1381 | * not come in and change it. In which case, we |
| 1382 | * do not want to modify it. |
Lai Jiangshan | da706d8 | 2009-07-15 16:27:30 +0800 | [diff] [blame] | 1383 | * |
| 1384 | * We add (void) to let the compiler know that we do not care |
| 1385 | * about the return value of these functions. We use the |
| 1386 | * cmpxchg to only update if an interrupt did not already |
| 1387 | * do it for us. If the cmpxchg fails, we don't care. |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1388 | */ |
Lai Jiangshan | da706d8 | 2009-07-15 16:27:30 +0800 | [diff] [blame] | 1389 | (void)local_cmpxchg(&next_page->write, old_write, val); |
| 1390 | (void)local_cmpxchg(&next_page->entries, old_entries, eval); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1391 | |
| 1392 | /* |
| 1393 | * No need to worry about races with clearing out the commit. |
| 1394 | * it only can increment when a commit takes place. But that |
| 1395 | * only happens in the outer most nested commit. |
| 1396 | */ |
| 1397 | local_set(&next_page->page->commit, 0); |
| 1398 | |
Steven Rostedt (Red Hat) | 7000498 | 2015-11-17 15:15:19 -0500 | [diff] [blame] | 1399 | /* Again, either we update tail_page or an interrupt does */ |
| 1400 | (void)cmpxchg(&cpu_buffer->tail_page, tail_page, next_page); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1401 | } |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer, |
| 1405 | struct buffer_page *bpage) |
| 1406 | { |
| 1407 | unsigned long val = (unsigned long)bpage; |
| 1408 | |
| 1409 | if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK)) |
| 1410 | return 1; |
| 1411 | |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
| 1415 | /** |
| 1416 | * rb_check_list - make sure a pointer to a list has the last bits zero |
| 1417 | */ |
| 1418 | static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer, |
| 1419 | struct list_head *list) |
| 1420 | { |
| 1421 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev)) |
| 1422 | return 1; |
| 1423 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next)) |
| 1424 | return 1; |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1428 | /** |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 1429 | * rb_check_pages - integrity check of buffer pages |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1430 | * @cpu_buffer: CPU buffer with pages to test |
| 1431 | * |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 1432 | * As a safety measure we check to make sure the data pages have not |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1433 | * been corrupted. |
| 1434 | */ |
| 1435 | static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer) |
| 1436 | { |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1437 | struct list_head *head = cpu_buffer->pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1438 | struct buffer_page *bpage, *tmp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1439 | |
Steven Rostedt | 308f7ee | 2012-05-16 19:46:32 -0400 | [diff] [blame] | 1440 | /* Reset the head page if it exists */ |
| 1441 | if (cpu_buffer->head_page) |
| 1442 | rb_set_head_page(cpu_buffer); |
| 1443 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1444 | rb_head_page_deactivate(cpu_buffer); |
| 1445 | |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1446 | if (RB_WARN_ON(cpu_buffer, head->next->prev != head)) |
| 1447 | return -1; |
| 1448 | if (RB_WARN_ON(cpu_buffer, head->prev->next != head)) |
| 1449 | return -1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1450 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1451 | if (rb_check_list(cpu_buffer, head)) |
| 1452 | return -1; |
| 1453 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1454 | list_for_each_entry_safe(bpage, tmp, head, list) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1455 | if (RB_WARN_ON(cpu_buffer, |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1456 | bpage->list.next->prev != &bpage->list)) |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1457 | return -1; |
| 1458 | if (RB_WARN_ON(cpu_buffer, |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1459 | bpage->list.prev->next != &bpage->list)) |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1460 | return -1; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1461 | if (rb_check_list(cpu_buffer, &bpage->list)) |
| 1462 | return -1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1463 | } |
| 1464 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1465 | rb_head_page_activate(cpu_buffer); |
| 1466 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1467 | return 0; |
| 1468 | } |
| 1469 | |
Qiujun Huang | 74e2afc | 2020-10-15 19:38:42 +0800 | [diff] [blame] | 1470 | static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, |
| 1471 | long nr_pages, struct list_head *pages) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1472 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1473 | struct buffer_page *bpage, *tmp; |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1474 | bool user_thread = current->mm != NULL; |
| 1475 | gfp_t mflags; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 1476 | long i; |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1477 | |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1478 | /* |
| 1479 | * Check if the available memory is there first. |
| 1480 | * Note, si_mem_available() only gives us a rough estimate of available |
| 1481 | * memory. It may not be accurate. But we don't care, we just want |
| 1482 | * to prevent doing any allocation when it is obvious that it is |
| 1483 | * not going to succeed. |
| 1484 | */ |
Steven Rostedt (VMware) | 2a872fa | 2018-04-02 10:33:56 -0400 | [diff] [blame] | 1485 | i = si_mem_available(); |
| 1486 | if (i < nr_pages) |
| 1487 | return -ENOMEM; |
| 1488 | |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1489 | /* |
| 1490 | * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails |
| 1491 | * gracefully without invoking oom-killer and the system is not |
| 1492 | * destabilized. |
| 1493 | */ |
| 1494 | mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL; |
| 1495 | |
| 1496 | /* |
| 1497 | * If a user thread allocates too much, and si_mem_available() |
| 1498 | * reports there's enough memory, even though there is not. |
| 1499 | * Make sure the OOM killer kills this thread. This can happen |
| 1500 | * even with RETRY_MAYFAIL because another task may be doing |
| 1501 | * an allocation after this task has taken all memory. |
| 1502 | * This is the task the OOM killer needs to take out during this |
| 1503 | * loop, even if it was triggered by an allocation somewhere else. |
| 1504 | */ |
| 1505 | if (user_thread) |
| 1506 | set_current_oom_origin(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1507 | for (i = 0; i < nr_pages; i++) { |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 1508 | struct page *page; |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1509 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1510 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
Qiujun Huang | 74e2afc | 2020-10-15 19:38:42 +0800 | [diff] [blame] | 1511 | mflags, cpu_to_node(cpu_buffer->cpu)); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1512 | if (!bpage) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1513 | goto free_pages; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1514 | |
Qiujun Huang | 74e2afc | 2020-10-15 19:38:42 +0800 | [diff] [blame] | 1515 | rb_check_bpage(cpu_buffer, bpage); |
| 1516 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1517 | list_add(&bpage->list, pages); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1518 | |
Qiujun Huang | 74e2afc | 2020-10-15 19:38:42 +0800 | [diff] [blame] | 1519 | page = alloc_pages_node(cpu_to_node(cpu_buffer->cpu), mflags, 0); |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 1520 | if (!page) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1521 | goto free_pages; |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 1522 | bpage->page = page_address(page); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1523 | rb_init_page(bpage->page); |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1524 | |
| 1525 | if (user_thread && fatal_signal_pending(current)) |
| 1526 | goto free_pages; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1527 | } |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1528 | if (user_thread) |
| 1529 | clear_current_oom_origin(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1530 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1531 | return 0; |
| 1532 | |
| 1533 | free_pages: |
| 1534 | list_for_each_entry_safe(bpage, tmp, pages, list) { |
| 1535 | list_del_init(&bpage->list); |
| 1536 | free_buffer_page(bpage); |
| 1537 | } |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1538 | if (user_thread) |
| 1539 | clear_current_oom_origin(); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1540 | |
| 1541 | return -ENOMEM; |
| 1542 | } |
| 1543 | |
| 1544 | static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 1545 | unsigned long nr_pages) |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1546 | { |
| 1547 | LIST_HEAD(pages); |
| 1548 | |
| 1549 | WARN_ON(!nr_pages); |
| 1550 | |
Qiujun Huang | 74e2afc | 2020-10-15 19:38:42 +0800 | [diff] [blame] | 1551 | if (__rb_allocate_pages(cpu_buffer, nr_pages, &pages)) |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1552 | return -ENOMEM; |
| 1553 | |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1554 | /* |
| 1555 | * The ring buffer page list is a circular list that does not |
| 1556 | * start and end with a list head. All page list items point to |
| 1557 | * other pages. |
| 1558 | */ |
| 1559 | cpu_buffer->pages = pages.next; |
| 1560 | list_del(&pages); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1561 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1562 | cpu_buffer->nr_pages = nr_pages; |
| 1563 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1564 | rb_check_pages(cpu_buffer); |
| 1565 | |
| 1566 | return 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | static struct ring_buffer_per_cpu * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1570 | rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1571 | { |
| 1572 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1573 | struct buffer_page *bpage; |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 1574 | struct page *page; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1575 | int ret; |
| 1576 | |
| 1577 | cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()), |
| 1578 | GFP_KERNEL, cpu_to_node(cpu)); |
| 1579 | if (!cpu_buffer) |
| 1580 | return NULL; |
| 1581 | |
| 1582 | cpu_buffer->cpu = cpu; |
| 1583 | cpu_buffer->buffer = buffer; |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 1584 | raw_spin_lock_init(&cpu_buffer->reader_lock); |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 1585 | lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key); |
Thomas Gleixner | edc35bd | 2009-12-03 12:38:57 +0100 | [diff] [blame] | 1586 | cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1587 | INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler); |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 1588 | init_completion(&cpu_buffer->update_done); |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 1589 | init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters); |
Steven Rostedt (Red Hat) | f1dc672 | 2013-03-04 17:33:05 -0500 | [diff] [blame] | 1590 | init_waitqueue_head(&cpu_buffer->irq_work.waiters); |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 1591 | init_waitqueue_head(&cpu_buffer->irq_work.full_waiters); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1592 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1593 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1594 | GFP_KERNEL, cpu_to_node(cpu)); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1595 | if (!bpage) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1596 | goto fail_free_buffer; |
| 1597 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1598 | rb_check_bpage(cpu_buffer, bpage); |
| 1599 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1600 | cpu_buffer->reader_page = bpage; |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 1601 | page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0); |
| 1602 | if (!page) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1603 | goto fail_free_reader; |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 1604 | bpage->page = page_address(page); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1605 | rb_init_page(bpage->page); |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1606 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1607 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); |
Vaibhav Nagarnaik | 44b9946 | 2012-06-22 11:50:05 -0700 | [diff] [blame] | 1608 | INIT_LIST_HEAD(&cpu_buffer->new_pages); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1609 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1610 | ret = rb_allocate_pages(cpu_buffer, nr_pages); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1611 | if (ret < 0) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1612 | goto fail_free_reader; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1613 | |
| 1614 | cpu_buffer->head_page |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1615 | = list_entry(cpu_buffer->pages, struct buffer_page, list); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1616 | cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1617 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1618 | rb_head_page_activate(cpu_buffer); |
| 1619 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1620 | return cpu_buffer; |
| 1621 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1622 | fail_free_reader: |
| 1623 | free_buffer_page(cpu_buffer->reader_page); |
| 1624 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1625 | fail_free_buffer: |
| 1626 | kfree(cpu_buffer); |
| 1627 | return NULL; |
| 1628 | } |
| 1629 | |
| 1630 | static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer) |
| 1631 | { |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1632 | struct list_head *head = cpu_buffer->pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1633 | struct buffer_page *bpage, *tmp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1634 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1635 | free_buffer_page(cpu_buffer->reader_page); |
| 1636 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1637 | rb_head_page_deactivate(cpu_buffer); |
| 1638 | |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1639 | if (head) { |
| 1640 | list_for_each_entry_safe(bpage, tmp, head, list) { |
| 1641 | list_del_init(&bpage->list); |
| 1642 | free_buffer_page(bpage); |
| 1643 | } |
| 1644 | bpage = list_entry(head, struct buffer_page, list); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1645 | free_buffer_page(bpage); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1646 | } |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1647 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1648 | kfree(cpu_buffer); |
| 1649 | } |
| 1650 | |
| 1651 | /** |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 1652 | * __ring_buffer_alloc - allocate a new ring_buffer |
Robert Richter | 68814b5 | 2008-11-24 12:24:12 +0100 | [diff] [blame] | 1653 | * @size: the size in bytes per cpu that is needed. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1654 | * @flags: attributes to set for the ring buffer. |
Fabian Frederick | 59e7cff | 2014-06-05 20:22:05 +0200 | [diff] [blame] | 1655 | * @key: ring buffer reader_lock_key. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1656 | * |
| 1657 | * Currently the only flag that is available is the RB_FL_OVERWRITE |
| 1658 | * flag. This flag means that the buffer will overwrite old data |
| 1659 | * when the buffer wraps. If this flag is not set, the buffer will |
| 1660 | * drop data when the tail hits the head. |
| 1661 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1662 | struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags, |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 1663 | struct lock_class_key *key) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1664 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1665 | struct trace_buffer *buffer; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 1666 | long nr_pages; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1667 | int bsize; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 1668 | int cpu; |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 1669 | int ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1670 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1671 | /* keep it in its own cache line */ |
| 1672 | buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()), |
| 1673 | GFP_KERNEL); |
| 1674 | if (!buffer) |
| 1675 | return NULL; |
| 1676 | |
Sebastian Andrzej Siewior | b18cc3d | 2016-12-07 14:31:33 +0100 | [diff] [blame] | 1677 | if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL)) |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1678 | goto fail_free_buffer; |
| 1679 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1680 | nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1681 | buffer->flags = flags; |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 1682 | buffer->clock = trace_clock_local; |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 1683 | buffer->reader_lock_key = key; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1684 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 1685 | init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters); |
Steven Rostedt (Red Hat) | f1dc672 | 2013-03-04 17:33:05 -0500 | [diff] [blame] | 1686 | init_waitqueue_head(&buffer->irq_work.waiters); |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 1687 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1688 | /* need at least two pages */ |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1689 | if (nr_pages < 2) |
| 1690 | nr_pages = 2; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1691 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1692 | buffer->cpus = nr_cpu_ids; |
| 1693 | |
| 1694 | bsize = sizeof(void *) * nr_cpu_ids; |
| 1695 | buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()), |
| 1696 | GFP_KERNEL); |
| 1697 | if (!buffer->buffers) |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1698 | goto fail_free_cpumask; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1699 | |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 1700 | cpu = raw_smp_processor_id(); |
| 1701 | cpumask_set_cpu(cpu, buffer->cpumask); |
| 1702 | buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu); |
| 1703 | if (!buffer->buffers[cpu]) |
| 1704 | goto fail_free_buffers; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1705 | |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 1706 | ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node); |
| 1707 | if (ret < 0) |
| 1708 | goto fail_free_buffers; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1709 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1710 | mutex_init(&buffer->mutex); |
| 1711 | |
| 1712 | return buffer; |
| 1713 | |
| 1714 | fail_free_buffers: |
| 1715 | for_each_buffer_cpu(buffer, cpu) { |
| 1716 | if (buffer->buffers[cpu]) |
| 1717 | rb_free_cpu_buffer(buffer->buffers[cpu]); |
| 1718 | } |
| 1719 | kfree(buffer->buffers); |
| 1720 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1721 | fail_free_cpumask: |
| 1722 | free_cpumask_var(buffer->cpumask); |
| 1723 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1724 | fail_free_buffer: |
| 1725 | kfree(buffer); |
| 1726 | return NULL; |
| 1727 | } |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 1728 | EXPORT_SYMBOL_GPL(__ring_buffer_alloc); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1729 | |
| 1730 | /** |
| 1731 | * ring_buffer_free - free a ring buffer. |
| 1732 | * @buffer: the buffer to free. |
| 1733 | */ |
| 1734 | void |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1735 | ring_buffer_free(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1736 | { |
| 1737 | int cpu; |
| 1738 | |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 1739 | cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1740 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1741 | for_each_buffer_cpu(buffer, cpu) |
| 1742 | rb_free_cpu_buffer(buffer->buffers[cpu]); |
| 1743 | |
Eric Dumazet | bd3f022 | 2009-08-07 12:49:29 +0200 | [diff] [blame] | 1744 | kfree(buffer->buffers); |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1745 | free_cpumask_var(buffer->cpumask); |
| 1746 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1747 | kfree(buffer); |
| 1748 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1749 | EXPORT_SYMBOL_GPL(ring_buffer_free); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1750 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1751 | void ring_buffer_set_clock(struct trace_buffer *buffer, |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 1752 | u64 (*clock)(void)) |
| 1753 | { |
| 1754 | buffer->clock = clock; |
| 1755 | } |
| 1756 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1757 | void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs) |
Tom Zanussi | 00b4145 | 2018-01-15 20:51:39 -0600 | [diff] [blame] | 1758 | { |
| 1759 | buffer->time_stamp_abs = abs; |
| 1760 | } |
| 1761 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1762 | bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer) |
Tom Zanussi | 00b4145 | 2018-01-15 20:51:39 -0600 | [diff] [blame] | 1763 | { |
| 1764 | return buffer->time_stamp_abs; |
| 1765 | } |
| 1766 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1767 | static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer); |
| 1768 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1769 | static inline unsigned long rb_page_entries(struct buffer_page *bpage) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1770 | { |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1771 | return local_read(&bpage->entries) & RB_WRITE_MASK; |
| 1772 | } |
| 1773 | |
| 1774 | static inline unsigned long rb_page_write(struct buffer_page *bpage) |
| 1775 | { |
| 1776 | return local_read(&bpage->write) & RB_WRITE_MASK; |
| 1777 | } |
| 1778 | |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1779 | static int |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 1780 | rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages) |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1781 | { |
| 1782 | struct list_head *tail_page, *to_remove, *next_page; |
| 1783 | struct buffer_page *to_remove_page, *tmp_iter_page; |
| 1784 | struct buffer_page *last_page, *first_page; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 1785 | unsigned long nr_removed; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1786 | unsigned long head_bit; |
| 1787 | int page_entries; |
| 1788 | |
| 1789 | head_bit = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1790 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 1791 | raw_spin_lock_irq(&cpu_buffer->reader_lock); |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1792 | atomic_inc(&cpu_buffer->record_disabled); |
| 1793 | /* |
| 1794 | * We don't race with the readers since we have acquired the reader |
| 1795 | * lock. We also don't race with writers after disabling recording. |
| 1796 | * This makes it easy to figure out the first and the last page to be |
| 1797 | * removed from the list. We unlink all the pages in between including |
| 1798 | * the first and last pages. This is done in a busy loop so that we |
| 1799 | * lose the least number of traces. |
| 1800 | * The pages are freed after we restart recording and unlock readers. |
| 1801 | */ |
| 1802 | tail_page = &cpu_buffer->tail_page->list; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1803 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1804 | /* |
| 1805 | * tail page might be on reader page, we remove the next page |
| 1806 | * from the ring buffer |
| 1807 | */ |
| 1808 | if (cpu_buffer->tail_page == cpu_buffer->reader_page) |
| 1809 | tail_page = rb_list_head(tail_page->next); |
| 1810 | to_remove = tail_page; |
| 1811 | |
| 1812 | /* start of pages to remove */ |
| 1813 | first_page = list_entry(rb_list_head(to_remove->next), |
| 1814 | struct buffer_page, list); |
| 1815 | |
| 1816 | for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) { |
| 1817 | to_remove = rb_list_head(to_remove)->next; |
| 1818 | head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1819 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1820 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1821 | next_page = rb_list_head(to_remove)->next; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1822 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1823 | /* |
| 1824 | * Now we remove all pages between tail_page and next_page. |
| 1825 | * Make sure that we have head_bit value preserved for the |
| 1826 | * next page |
| 1827 | */ |
| 1828 | tail_page->next = (struct list_head *)((unsigned long)next_page | |
| 1829 | head_bit); |
| 1830 | next_page = rb_list_head(next_page); |
| 1831 | next_page->prev = tail_page; |
| 1832 | |
| 1833 | /* make sure pages points to a valid page in the ring buffer */ |
| 1834 | cpu_buffer->pages = next_page; |
| 1835 | |
| 1836 | /* update head page */ |
| 1837 | if (head_bit) |
| 1838 | cpu_buffer->head_page = list_entry(next_page, |
| 1839 | struct buffer_page, list); |
| 1840 | |
| 1841 | /* |
| 1842 | * change read pointer to make sure any read iterators reset |
| 1843 | * themselves |
| 1844 | */ |
| 1845 | cpu_buffer->read = 0; |
| 1846 | |
| 1847 | /* pages are removed, resume tracing and then free the pages */ |
| 1848 | atomic_dec(&cpu_buffer->record_disabled); |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 1849 | raw_spin_unlock_irq(&cpu_buffer->reader_lock); |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1850 | |
| 1851 | RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)); |
| 1852 | |
| 1853 | /* last buffer page to remove */ |
| 1854 | last_page = list_entry(rb_list_head(to_remove), struct buffer_page, |
| 1855 | list); |
| 1856 | tmp_iter_page = first_page; |
| 1857 | |
| 1858 | do { |
Vaibhav Nagarnaik | 83f3655 | 2018-09-07 15:31:29 -0700 | [diff] [blame] | 1859 | cond_resched(); |
| 1860 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1861 | to_remove_page = tmp_iter_page; |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 1862 | rb_inc_page(&tmp_iter_page); |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1863 | |
| 1864 | /* update the counters */ |
| 1865 | page_entries = rb_page_entries(to_remove_page); |
| 1866 | if (page_entries) { |
| 1867 | /* |
| 1868 | * If something was added to this page, it was full |
| 1869 | * since it is not the tail page. So we deduct the |
| 1870 | * bytes consumed in ring buffer from here. |
Vaibhav Nagarnaik | 48fdc72 | 2012-06-29 12:31:41 -0700 | [diff] [blame] | 1871 | * Increment overrun to account for the lost events. |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1872 | */ |
Vaibhav Nagarnaik | 48fdc72 | 2012-06-29 12:31:41 -0700 | [diff] [blame] | 1873 | local_add(page_entries, &cpu_buffer->overrun); |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1874 | local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes); |
| 1875 | } |
| 1876 | |
| 1877 | /* |
| 1878 | * We have already removed references to this list item, just |
| 1879 | * free up the buffer_page and its page |
| 1880 | */ |
| 1881 | free_buffer_page(to_remove_page); |
| 1882 | nr_removed--; |
| 1883 | |
| 1884 | } while (to_remove_page != last_page); |
| 1885 | |
| 1886 | RB_WARN_ON(cpu_buffer, nr_removed); |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1887 | |
| 1888 | return nr_removed == 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1889 | } |
| 1890 | |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1891 | static int |
| 1892 | rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1893 | { |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1894 | struct list_head *pages = &cpu_buffer->new_pages; |
| 1895 | int retries, success; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1896 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 1897 | raw_spin_lock_irq(&cpu_buffer->reader_lock); |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1898 | /* |
| 1899 | * We are holding the reader lock, so the reader page won't be swapped |
| 1900 | * in the ring buffer. Now we are racing with the writer trying to |
| 1901 | * move head page and the tail page. |
| 1902 | * We are going to adapt the reader page update process where: |
| 1903 | * 1. We first splice the start and end of list of new pages between |
| 1904 | * the head page and its previous page. |
| 1905 | * 2. We cmpxchg the prev_page->next to point from head page to the |
| 1906 | * start of new pages list. |
| 1907 | * 3. Finally, we update the head->prev to the end of new list. |
| 1908 | * |
| 1909 | * We will try this process 10 times, to make sure that we don't keep |
| 1910 | * spinning. |
| 1911 | */ |
| 1912 | retries = 10; |
| 1913 | success = 0; |
| 1914 | while (retries--) { |
| 1915 | struct list_head *head_page, *prev_page, *r; |
| 1916 | struct list_head *last_page, *first_page; |
| 1917 | struct list_head *head_page_with_bit; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1918 | |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1919 | head_page = &rb_set_head_page(cpu_buffer)->list; |
Steven Rostedt | 54f7be5 | 2012-11-29 22:27:22 -0500 | [diff] [blame] | 1920 | if (!head_page) |
| 1921 | break; |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1922 | prev_page = head_page->prev; |
| 1923 | |
| 1924 | first_page = pages->next; |
| 1925 | last_page = pages->prev; |
| 1926 | |
| 1927 | head_page_with_bit = (struct list_head *) |
| 1928 | ((unsigned long)head_page | RB_PAGE_HEAD); |
| 1929 | |
| 1930 | last_page->next = head_page_with_bit; |
| 1931 | first_page->prev = prev_page; |
| 1932 | |
| 1933 | r = cmpxchg(&prev_page->next, head_page_with_bit, first_page); |
| 1934 | |
| 1935 | if (r == head_page_with_bit) { |
| 1936 | /* |
| 1937 | * yay, we replaced the page pointer to our new list, |
| 1938 | * now, we just have to update to head page's prev |
| 1939 | * pointer to point to end of list |
| 1940 | */ |
| 1941 | head_page->prev = last_page; |
| 1942 | success = 1; |
| 1943 | break; |
| 1944 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1945 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1946 | |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1947 | if (success) |
| 1948 | INIT_LIST_HEAD(pages); |
| 1949 | /* |
| 1950 | * If we weren't successful in adding in new pages, warn and stop |
| 1951 | * tracing |
| 1952 | */ |
| 1953 | RB_WARN_ON(cpu_buffer, !success); |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 1954 | raw_spin_unlock_irq(&cpu_buffer->reader_lock); |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1955 | |
| 1956 | /* free pages if they weren't inserted */ |
| 1957 | if (!success) { |
| 1958 | struct buffer_page *bpage, *tmp; |
| 1959 | list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages, |
| 1960 | list) { |
| 1961 | list_del_init(&bpage->list); |
| 1962 | free_buffer_page(bpage); |
| 1963 | } |
| 1964 | } |
| 1965 | return success; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1966 | } |
| 1967 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1968 | static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer) |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1969 | { |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1970 | int success; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1971 | |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1972 | if (cpu_buffer->nr_pages_to_update > 0) |
| 1973 | success = rb_insert_pages(cpu_buffer); |
| 1974 | else |
| 1975 | success = rb_remove_pages(cpu_buffer, |
| 1976 | -cpu_buffer->nr_pages_to_update); |
| 1977 | |
| 1978 | if (success) |
| 1979 | cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | static void update_pages_handler(struct work_struct *work) |
| 1983 | { |
| 1984 | struct ring_buffer_per_cpu *cpu_buffer = container_of(work, |
| 1985 | struct ring_buffer_per_cpu, update_pages_work); |
| 1986 | rb_update_pages(cpu_buffer); |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 1987 | complete(&cpu_buffer->update_done); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1988 | } |
| 1989 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1990 | /** |
| 1991 | * ring_buffer_resize - resize the ring buffer |
| 1992 | * @buffer: the buffer to resize. |
| 1993 | * @size: the new size. |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 1994 | * @cpu_id: the cpu buffer to resize |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1995 | * |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1996 | * Minimum size is 2 * BUF_PAGE_SIZE. |
| 1997 | * |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1998 | * Returns 0 on success and < 0 on failure. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1999 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2000 | int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size, |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2001 | int cpu_id) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2002 | { |
| 2003 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 2004 | unsigned long nr_pages; |
Qiujun Huang | 0a1754b | 2020-10-19 22:22:42 +0800 | [diff] [blame] | 2005 | int cpu, err; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2006 | |
Ingo Molnar | ee51a1d | 2008-11-13 14:58:31 +0100 | [diff] [blame] | 2007 | /* |
| 2008 | * Always succeed at resizing a non-existent buffer: |
| 2009 | */ |
| 2010 | if (!buffer) |
Qiujun Huang | 0a1754b | 2020-10-19 22:22:42 +0800 | [diff] [blame] | 2011 | return 0; |
Ingo Molnar | ee51a1d | 2008-11-13 14:58:31 +0100 | [diff] [blame] | 2012 | |
Steven Rostedt | 6a31e1f | 2012-05-23 15:35:17 -0400 | [diff] [blame] | 2013 | /* Make sure the requested buffer exists */ |
| 2014 | if (cpu_id != RING_BUFFER_ALL_CPUS && |
| 2015 | !cpumask_test_cpu(cpu_id, buffer->cpumask)) |
Qiujun Huang | 0a1754b | 2020-10-19 22:22:42 +0800 | [diff] [blame] | 2016 | return 0; |
Steven Rostedt | 6a31e1f | 2012-05-23 15:35:17 -0400 | [diff] [blame] | 2017 | |
Steven Rostedt (Red Hat) | 59643d1 | 2016-05-13 09:34:12 -0400 | [diff] [blame] | 2018 | nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2019 | |
| 2020 | /* we need a minimum of two pages */ |
Steven Rostedt (Red Hat) | 59643d1 | 2016-05-13 09:34:12 -0400 | [diff] [blame] | 2021 | if (nr_pages < 2) |
| 2022 | nr_pages = 2; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2023 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2024 | /* prevent another thread from changing buffer sizes */ |
| 2025 | mutex_lock(&buffer->mutex); |
| 2026 | |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 2027 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2028 | if (cpu_id == RING_BUFFER_ALL_CPUS) { |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 2029 | /* |
| 2030 | * Don't succeed if resizing is disabled, as a reader might be |
| 2031 | * manipulating the ring buffer and is expecting a sane state while |
| 2032 | * this is true. |
| 2033 | */ |
| 2034 | for_each_buffer_cpu(buffer, cpu) { |
| 2035 | cpu_buffer = buffer->buffers[cpu]; |
| 2036 | if (atomic_read(&cpu_buffer->resize_disabled)) { |
| 2037 | err = -EBUSY; |
| 2038 | goto out_err_unlock; |
| 2039 | } |
| 2040 | } |
| 2041 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2042 | /* calculate the pages to update */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2043 | for_each_buffer_cpu(buffer, cpu) { |
| 2044 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2045 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2046 | cpu_buffer->nr_pages_to_update = nr_pages - |
| 2047 | cpu_buffer->nr_pages; |
Vaibhav Nagarnaik | d7ec4bf | 2011-06-07 17:01:42 -0700 | [diff] [blame] | 2048 | /* |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2049 | * nothing more to do for removing pages or no update |
Vaibhav Nagarnaik | d7ec4bf | 2011-06-07 17:01:42 -0700 | [diff] [blame] | 2050 | */ |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2051 | if (cpu_buffer->nr_pages_to_update <= 0) |
| 2052 | continue; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2053 | /* |
| 2054 | * to add pages, make sure all new pages can be |
| 2055 | * allocated without receiving ENOMEM |
| 2056 | */ |
| 2057 | INIT_LIST_HEAD(&cpu_buffer->new_pages); |
Qiujun Huang | 74e2afc | 2020-10-15 19:38:42 +0800 | [diff] [blame] | 2058 | if (__rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update, |
| 2059 | &cpu_buffer->new_pages)) { |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2060 | /* not enough memory for new pages */ |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2061 | err = -ENOMEM; |
| 2062 | goto out_err; |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | get_online_cpus(); |
| 2067 | /* |
| 2068 | * Fire off all the required work handlers |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2069 | * We can't schedule on offline CPUs, but it's not necessary |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2070 | * since we can change their buffer sizes without any race. |
| 2071 | */ |
| 2072 | for_each_buffer_cpu(buffer, cpu) { |
| 2073 | cpu_buffer = buffer->buffers[cpu]; |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2074 | if (!cpu_buffer->nr_pages_to_update) |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2075 | continue; |
| 2076 | |
Corey Minyard | 021c5b3 | 2014-07-16 14:07:13 -0500 | [diff] [blame] | 2077 | /* Can't run something on an offline CPU. */ |
| 2078 | if (!cpu_online(cpu)) { |
Steven Rostedt (Red Hat) | f5eb558 | 2013-03-07 09:27:42 -0500 | [diff] [blame] | 2079 | rb_update_pages(cpu_buffer); |
| 2080 | cpu_buffer->nr_pages_to_update = 0; |
| 2081 | } else { |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2082 | schedule_work_on(cpu, |
| 2083 | &cpu_buffer->update_pages_work); |
Steven Rostedt (Red Hat) | f5eb558 | 2013-03-07 09:27:42 -0500 | [diff] [blame] | 2084 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2085 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2086 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2087 | /* wait for all the updates to complete */ |
| 2088 | for_each_buffer_cpu(buffer, cpu) { |
| 2089 | cpu_buffer = buffer->buffers[cpu]; |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2090 | if (!cpu_buffer->nr_pages_to_update) |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2091 | continue; |
| 2092 | |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2093 | if (cpu_online(cpu)) |
| 2094 | wait_for_completion(&cpu_buffer->update_done); |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2095 | cpu_buffer->nr_pages_to_update = 0; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2096 | } |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2097 | |
| 2098 | put_online_cpus(); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2099 | } else { |
| 2100 | cpu_buffer = buffer->buffers[cpu_id]; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2101 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2102 | if (nr_pages == cpu_buffer->nr_pages) |
| 2103 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2104 | |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 2105 | /* |
| 2106 | * Don't succeed if resizing is disabled, as a reader might be |
| 2107 | * manipulating the ring buffer and is expecting a sane state while |
| 2108 | * this is true. |
| 2109 | */ |
| 2110 | if (atomic_read(&cpu_buffer->resize_disabled)) { |
| 2111 | err = -EBUSY; |
| 2112 | goto out_err_unlock; |
| 2113 | } |
| 2114 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2115 | cpu_buffer->nr_pages_to_update = nr_pages - |
| 2116 | cpu_buffer->nr_pages; |
| 2117 | |
| 2118 | INIT_LIST_HEAD(&cpu_buffer->new_pages); |
| 2119 | if (cpu_buffer->nr_pages_to_update > 0 && |
Qiujun Huang | 74e2afc | 2020-10-15 19:38:42 +0800 | [diff] [blame] | 2120 | __rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update, |
| 2121 | &cpu_buffer->new_pages)) { |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2122 | err = -ENOMEM; |
| 2123 | goto out_err; |
| 2124 | } |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2125 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2126 | get_online_cpus(); |
| 2127 | |
Corey Minyard | 021c5b3 | 2014-07-16 14:07:13 -0500 | [diff] [blame] | 2128 | /* Can't run something on an offline CPU. */ |
| 2129 | if (!cpu_online(cpu_id)) |
Steven Rostedt (Red Hat) | f5eb558 | 2013-03-07 09:27:42 -0500 | [diff] [blame] | 2130 | rb_update_pages(cpu_buffer); |
| 2131 | else { |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2132 | schedule_work_on(cpu_id, |
| 2133 | &cpu_buffer->update_pages_work); |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2134 | wait_for_completion(&cpu_buffer->update_done); |
Steven Rostedt (Red Hat) | f5eb558 | 2013-03-07 09:27:42 -0500 | [diff] [blame] | 2135 | } |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2136 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2137 | cpu_buffer->nr_pages_to_update = 0; |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2138 | put_online_cpus(); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2139 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2140 | |
| 2141 | out: |
Steven Rostedt | 659f451 | 2012-05-14 17:02:33 -0400 | [diff] [blame] | 2142 | /* |
| 2143 | * The ring buffer resize can happen with the ring buffer |
| 2144 | * enabled, so that the update disturbs the tracing as little |
| 2145 | * as possible. But if the buffer is disabled, we do not need |
| 2146 | * to worry about that, and we can take the time to verify |
| 2147 | * that the buffer is not corrupt. |
| 2148 | */ |
| 2149 | if (atomic_read(&buffer->record_disabled)) { |
| 2150 | atomic_inc(&buffer->record_disabled); |
| 2151 | /* |
| 2152 | * Even though the buffer was disabled, we must make sure |
| 2153 | * that it is truly disabled before calling rb_check_pages. |
| 2154 | * There could have been a race between checking |
| 2155 | * record_disable and incrementing it. |
| 2156 | */ |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 2157 | synchronize_rcu(); |
Steven Rostedt | 659f451 | 2012-05-14 17:02:33 -0400 | [diff] [blame] | 2158 | for_each_buffer_cpu(buffer, cpu) { |
| 2159 | cpu_buffer = buffer->buffers[cpu]; |
| 2160 | rb_check_pages(cpu_buffer); |
| 2161 | } |
| 2162 | atomic_dec(&buffer->record_disabled); |
| 2163 | } |
| 2164 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2165 | mutex_unlock(&buffer->mutex); |
Qiujun Huang | 0a1754b | 2020-10-19 22:22:42 +0800 | [diff] [blame] | 2166 | return 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2167 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2168 | out_err: |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2169 | for_each_buffer_cpu(buffer, cpu) { |
| 2170 | struct buffer_page *bpage, *tmp; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2171 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2172 | cpu_buffer = buffer->buffers[cpu]; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2173 | cpu_buffer->nr_pages_to_update = 0; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2174 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2175 | if (list_empty(&cpu_buffer->new_pages)) |
| 2176 | continue; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2177 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2178 | list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages, |
| 2179 | list) { |
| 2180 | list_del_init(&bpage->list); |
| 2181 | free_buffer_page(bpage); |
| 2182 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2183 | } |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 2184 | out_err_unlock: |
Vegard Nossum | 641d2f6 | 2008-11-18 19:22:13 +0100 | [diff] [blame] | 2185 | mutex_unlock(&buffer->mutex); |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2186 | return err; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2187 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2188 | EXPORT_SYMBOL_GPL(ring_buffer_resize); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2189 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2190 | void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val) |
David Sharp | 750912f | 2010-12-08 13:46:47 -0800 | [diff] [blame] | 2191 | { |
| 2192 | mutex_lock(&buffer->mutex); |
| 2193 | if (val) |
| 2194 | buffer->flags |= RB_FL_OVERWRITE; |
| 2195 | else |
| 2196 | buffer->flags &= ~RB_FL_OVERWRITE; |
| 2197 | mutex_unlock(&buffer->mutex); |
| 2198 | } |
| 2199 | EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite); |
| 2200 | |
Steven Rostedt (Red Hat) | 2289d56 | 2016-11-23 20:35:32 -0500 | [diff] [blame] | 2201 | static __always_inline void *__rb_page_index(struct buffer_page *bpage, unsigned index) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2202 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 2203 | return bpage->page->data + index; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2204 | } |
| 2205 | |
Steven Rostedt (Red Hat) | 2289d56 | 2016-11-23 20:35:32 -0500 | [diff] [blame] | 2206 | static __always_inline struct ring_buffer_event * |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2207 | rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2208 | { |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 2209 | return __rb_page_index(cpu_buffer->reader_page, |
| 2210 | cpu_buffer->reader_page->read); |
| 2211 | } |
| 2212 | |
Steven Rostedt (Red Hat) | 2289d56 | 2016-11-23 20:35:32 -0500 | [diff] [blame] | 2213 | static __always_inline unsigned rb_page_commit(struct buffer_page *bpage) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2214 | { |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 2215 | return local_read(&bpage->page->commit); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2216 | } |
| 2217 | |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 2218 | static struct ring_buffer_event * |
| 2219 | rb_iter_head_event(struct ring_buffer_iter *iter) |
| 2220 | { |
| 2221 | struct ring_buffer_event *event; |
| 2222 | struct buffer_page *iter_head_page = iter->head_page; |
| 2223 | unsigned long commit; |
| 2224 | unsigned length; |
| 2225 | |
Steven Rostedt (VMware) | 153368c | 2020-03-17 17:32:29 -0400 | [diff] [blame] | 2226 | if (iter->head != iter->next_event) |
| 2227 | return iter->event; |
| 2228 | |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 2229 | /* |
| 2230 | * When the writer goes across pages, it issues a cmpxchg which |
| 2231 | * is a mb(), which will synchronize with the rmb here. |
| 2232 | * (see rb_tail_page_update() and __rb_reserve_next()) |
| 2233 | */ |
| 2234 | commit = rb_page_commit(iter_head_page); |
| 2235 | smp_rmb(); |
| 2236 | event = __rb_page_index(iter_head_page, iter->head); |
| 2237 | length = rb_event_length(event); |
| 2238 | |
| 2239 | /* |
| 2240 | * READ_ONCE() doesn't work on functions and we don't want the |
| 2241 | * compiler doing any crazy optimizations with length. |
| 2242 | */ |
| 2243 | barrier(); |
| 2244 | |
| 2245 | if ((iter->head + length) > commit || length > BUF_MAX_DATA_SIZE) |
| 2246 | /* Writer corrupted the read? */ |
| 2247 | goto reset; |
| 2248 | |
| 2249 | memcpy(iter->event, event, length); |
| 2250 | /* |
| 2251 | * If the page stamp is still the same after this rmb() then the |
| 2252 | * event was safely copied without the writer entering the page. |
| 2253 | */ |
| 2254 | smp_rmb(); |
| 2255 | |
| 2256 | /* Make sure the page didn't change since we read this */ |
| 2257 | if (iter->page_stamp != iter_head_page->page->time_stamp || |
| 2258 | commit > rb_page_commit(iter_head_page)) |
| 2259 | goto reset; |
| 2260 | |
| 2261 | iter->next_event = iter->head + length; |
| 2262 | return iter->event; |
| 2263 | reset: |
| 2264 | /* Reset to the beginning */ |
| 2265 | iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp; |
| 2266 | iter->head = 0; |
| 2267 | iter->next_event = 0; |
Steven Rostedt (VMware) | c9b7a4a | 2020-03-17 17:32:32 -0400 | [diff] [blame] | 2268 | iter->missed_events = 1; |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 2269 | return NULL; |
| 2270 | } |
| 2271 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2272 | /* Size is determined by what has been committed */ |
Steven Rostedt (Red Hat) | 2289d56 | 2016-11-23 20:35:32 -0500 | [diff] [blame] | 2273 | static __always_inline unsigned rb_page_size(struct buffer_page *bpage) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2274 | { |
| 2275 | return rb_page_commit(bpage); |
| 2276 | } |
| 2277 | |
Steven Rostedt (Red Hat) | 2289d56 | 2016-11-23 20:35:32 -0500 | [diff] [blame] | 2278 | static __always_inline unsigned |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2279 | rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer) |
| 2280 | { |
| 2281 | return rb_page_commit(cpu_buffer->commit_page); |
| 2282 | } |
| 2283 | |
Steven Rostedt (Red Hat) | 2289d56 | 2016-11-23 20:35:32 -0500 | [diff] [blame] | 2284 | static __always_inline unsigned |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2285 | rb_event_index(struct ring_buffer_event *event) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2286 | { |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2287 | unsigned long addr = (unsigned long)event; |
| 2288 | |
Steven Rostedt | 22f470f | 2009-06-11 09:29:58 -0400 | [diff] [blame] | 2289 | return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2290 | } |
| 2291 | |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 2292 | static void rb_inc_iter(struct ring_buffer_iter *iter) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2293 | { |
| 2294 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 2295 | |
| 2296 | /* |
| 2297 | * The iterator could be on the reader page (it starts there). |
| 2298 | * But the head could have moved, since the reader was |
| 2299 | * found. Check for this case and assign the iterator |
| 2300 | * to the head page instead of next. |
| 2301 | */ |
| 2302 | if (iter->head_page == cpu_buffer->reader_page) |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2303 | iter->head_page = rb_set_head_page(cpu_buffer); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2304 | else |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 2305 | rb_inc_page(&iter->head_page); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2306 | |
Steven Rostedt (VMware) | 28e3fc5 | 2020-03-17 17:32:26 -0400 | [diff] [blame] | 2307 | iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2308 | iter->head = 0; |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 2309 | iter->next_event = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2310 | } |
| 2311 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2312 | /* |
| 2313 | * rb_handle_head_page - writer hit the head page |
| 2314 | * |
| 2315 | * Returns: +1 to retry page |
| 2316 | * 0 to continue |
| 2317 | * -1 on error |
| 2318 | */ |
| 2319 | static int |
| 2320 | rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer, |
| 2321 | struct buffer_page *tail_page, |
| 2322 | struct buffer_page *next_page) |
| 2323 | { |
| 2324 | struct buffer_page *new_head; |
| 2325 | int entries; |
| 2326 | int type; |
| 2327 | int ret; |
| 2328 | |
| 2329 | entries = rb_page_entries(next_page); |
| 2330 | |
| 2331 | /* |
| 2332 | * The hard part is here. We need to move the head |
| 2333 | * forward, and protect against both readers on |
| 2334 | * other CPUs and writers coming in via interrupts. |
| 2335 | */ |
| 2336 | type = rb_head_page_set_update(cpu_buffer, next_page, tail_page, |
| 2337 | RB_PAGE_HEAD); |
| 2338 | |
| 2339 | /* |
| 2340 | * type can be one of four: |
| 2341 | * NORMAL - an interrupt already moved it for us |
| 2342 | * HEAD - we are the first to get here. |
| 2343 | * UPDATE - we are the interrupt interrupting |
| 2344 | * a current move. |
| 2345 | * MOVED - a reader on another CPU moved the next |
| 2346 | * pointer to its reader page. Give up |
| 2347 | * and try again. |
| 2348 | */ |
| 2349 | |
| 2350 | switch (type) { |
| 2351 | case RB_PAGE_HEAD: |
| 2352 | /* |
| 2353 | * We changed the head to UPDATE, thus |
| 2354 | * it is our responsibility to update |
| 2355 | * the counters. |
| 2356 | */ |
| 2357 | local_add(entries, &cpu_buffer->overrun); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 2358 | local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2359 | |
| 2360 | /* |
| 2361 | * The entries will be zeroed out when we move the |
| 2362 | * tail page. |
| 2363 | */ |
| 2364 | |
| 2365 | /* still more to do */ |
| 2366 | break; |
| 2367 | |
| 2368 | case RB_PAGE_UPDATE: |
| 2369 | /* |
| 2370 | * This is an interrupt that interrupt the |
| 2371 | * previous update. Still more to do. |
| 2372 | */ |
| 2373 | break; |
| 2374 | case RB_PAGE_NORMAL: |
| 2375 | /* |
| 2376 | * An interrupt came in before the update |
| 2377 | * and processed this for us. |
| 2378 | * Nothing left to do. |
| 2379 | */ |
| 2380 | return 1; |
| 2381 | case RB_PAGE_MOVED: |
| 2382 | /* |
| 2383 | * The reader is on another CPU and just did |
| 2384 | * a swap with our next_page. |
| 2385 | * Try again. |
| 2386 | */ |
| 2387 | return 1; |
| 2388 | default: |
| 2389 | RB_WARN_ON(cpu_buffer, 1); /* WTF??? */ |
| 2390 | return -1; |
| 2391 | } |
| 2392 | |
| 2393 | /* |
| 2394 | * Now that we are here, the old head pointer is |
| 2395 | * set to UPDATE. This will keep the reader from |
| 2396 | * swapping the head page with the reader page. |
| 2397 | * The reader (on another CPU) will spin till |
| 2398 | * we are finished. |
| 2399 | * |
| 2400 | * We just need to protect against interrupts |
| 2401 | * doing the job. We will set the next pointer |
| 2402 | * to HEAD. After that, we set the old pointer |
| 2403 | * to NORMAL, but only if it was HEAD before. |
| 2404 | * otherwise we are an interrupt, and only |
| 2405 | * want the outer most commit to reset it. |
| 2406 | */ |
| 2407 | new_head = next_page; |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 2408 | rb_inc_page(&new_head); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2409 | |
| 2410 | ret = rb_head_page_set_head(cpu_buffer, new_head, next_page, |
| 2411 | RB_PAGE_NORMAL); |
| 2412 | |
| 2413 | /* |
| 2414 | * Valid returns are: |
| 2415 | * HEAD - an interrupt came in and already set it. |
| 2416 | * NORMAL - One of two things: |
| 2417 | * 1) We really set it. |
| 2418 | * 2) A bunch of interrupts came in and moved |
| 2419 | * the page forward again. |
| 2420 | */ |
| 2421 | switch (ret) { |
| 2422 | case RB_PAGE_HEAD: |
| 2423 | case RB_PAGE_NORMAL: |
| 2424 | /* OK */ |
| 2425 | break; |
| 2426 | default: |
| 2427 | RB_WARN_ON(cpu_buffer, 1); |
| 2428 | return -1; |
| 2429 | } |
| 2430 | |
| 2431 | /* |
| 2432 | * It is possible that an interrupt came in, |
| 2433 | * set the head up, then more interrupts came in |
| 2434 | * and moved it again. When we get back here, |
| 2435 | * the page would have been set to NORMAL but we |
| 2436 | * just set it back to HEAD. |
| 2437 | * |
| 2438 | * How do you detect this? Well, if that happened |
| 2439 | * the tail page would have moved. |
| 2440 | */ |
| 2441 | if (ret == RB_PAGE_NORMAL) { |
Steven Rostedt (Red Hat) | 8573636 | 2015-11-17 14:03:11 -0500 | [diff] [blame] | 2442 | struct buffer_page *buffer_tail_page; |
| 2443 | |
| 2444 | buffer_tail_page = READ_ONCE(cpu_buffer->tail_page); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2445 | /* |
| 2446 | * If the tail had moved passed next, then we need |
| 2447 | * to reset the pointer. |
| 2448 | */ |
Steven Rostedt (Red Hat) | 8573636 | 2015-11-17 14:03:11 -0500 | [diff] [blame] | 2449 | if (buffer_tail_page != tail_page && |
| 2450 | buffer_tail_page != next_page) |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2451 | rb_head_page_set_normal(cpu_buffer, new_head, |
| 2452 | next_page, |
| 2453 | RB_PAGE_HEAD); |
| 2454 | } |
| 2455 | |
| 2456 | /* |
| 2457 | * If this was the outer most commit (the one that |
| 2458 | * changed the original pointer from HEAD to UPDATE), |
| 2459 | * then it is up to us to reset it to NORMAL. |
| 2460 | */ |
| 2461 | if (type == RB_PAGE_HEAD) { |
| 2462 | ret = rb_head_page_set_normal(cpu_buffer, next_page, |
| 2463 | tail_page, |
| 2464 | RB_PAGE_UPDATE); |
| 2465 | if (RB_WARN_ON(cpu_buffer, |
| 2466 | ret != RB_PAGE_UPDATE)) |
| 2467 | return -1; |
| 2468 | } |
| 2469 | |
| 2470 | return 0; |
| 2471 | } |
| 2472 | |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2473 | static inline void |
| 2474 | rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2475 | unsigned long tail, struct rb_event_info *info) |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2476 | { |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2477 | struct buffer_page *tail_page = info->tail_page; |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2478 | struct ring_buffer_event *event; |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2479 | unsigned long length = info->length; |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2480 | |
| 2481 | /* |
| 2482 | * Only the event that crossed the page boundary |
| 2483 | * must fill the old tail_page with padding. |
| 2484 | */ |
| 2485 | if (tail >= BUF_PAGE_SIZE) { |
Steven Rostedt | b3230c8 | 2010-05-21 11:55:21 -0400 | [diff] [blame] | 2486 | /* |
| 2487 | * If the page was filled, then we still need |
| 2488 | * to update the real_end. Reset it to zero |
| 2489 | * and the reader will ignore it. |
| 2490 | */ |
| 2491 | if (tail == BUF_PAGE_SIZE) |
| 2492 | tail_page->real_end = 0; |
| 2493 | |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2494 | local_sub(length, &tail_page->write); |
| 2495 | return; |
| 2496 | } |
| 2497 | |
| 2498 | event = __rb_page_index(tail_page, tail); |
| 2499 | |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 2500 | /* account for padding bytes */ |
| 2501 | local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes); |
| 2502 | |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2503 | /* |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 2504 | * Save the original length to the meta data. |
| 2505 | * This will be used by the reader to add lost event |
| 2506 | * counter. |
| 2507 | */ |
| 2508 | tail_page->real_end = tail; |
| 2509 | |
| 2510 | /* |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2511 | * If this event is bigger than the minimum size, then |
| 2512 | * we need to be careful that we don't subtract the |
| 2513 | * write counter enough to allow another writer to slip |
| 2514 | * in on this page. |
| 2515 | * We put in a discarded commit instead, to make sure |
| 2516 | * that this space is not used again. |
| 2517 | * |
| 2518 | * If we are less than the minimum size, we don't need to |
| 2519 | * worry about it. |
| 2520 | */ |
| 2521 | if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) { |
| 2522 | /* No room for any events */ |
| 2523 | |
| 2524 | /* Mark the rest of the page with padding */ |
| 2525 | rb_event_set_padding(event); |
| 2526 | |
| 2527 | /* Set the write back to the previous setting */ |
| 2528 | local_sub(length, &tail_page->write); |
| 2529 | return; |
| 2530 | } |
| 2531 | |
| 2532 | /* Put in a discarded event */ |
| 2533 | event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE; |
| 2534 | event->type_len = RINGBUF_TYPE_PADDING; |
| 2535 | /* time delta must be non zero */ |
| 2536 | event->time_delta = 1; |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2537 | |
| 2538 | /* Set write to end of buffer */ |
| 2539 | length = (tail + length) - BUF_PAGE_SIZE; |
| 2540 | local_sub(length, &tail_page->write); |
| 2541 | } |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 2542 | |
Steven Rostedt (Red Hat) | 4239c38 | 2015-11-17 16:36:06 -0500 | [diff] [blame] | 2543 | static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer); |
| 2544 | |
Steven Rostedt | 747e94a | 2010-10-08 13:51:48 -0400 | [diff] [blame] | 2545 | /* |
| 2546 | * This is the slow path, force gcc not to inline it. |
| 2547 | */ |
| 2548 | static noinline struct ring_buffer_event * |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 2549 | rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2550 | unsigned long tail, struct rb_event_info *info) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2551 | { |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2552 | struct buffer_page *tail_page = info->tail_page; |
Steven Rostedt | 5a50e33 | 2009-11-17 08:43:01 -0500 | [diff] [blame] | 2553 | struct buffer_page *commit_page = cpu_buffer->commit_page; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2554 | struct trace_buffer *buffer = cpu_buffer->buffer; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2555 | struct buffer_page *next_page; |
| 2556 | int ret; |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2557 | |
| 2558 | next_page = tail_page; |
| 2559 | |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 2560 | rb_inc_page(&next_page); |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2561 | |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2562 | /* |
| 2563 | * If for some reason, we had an interrupt storm that made |
| 2564 | * it all the way around the buffer, bail, and warn |
| 2565 | * about it. |
| 2566 | */ |
| 2567 | if (unlikely(next_page == commit_page)) { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2568 | local_inc(&cpu_buffer->commit_overrun); |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2569 | goto out_reset; |
| 2570 | } |
| 2571 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2572 | /* |
| 2573 | * This is where the fun begins! |
| 2574 | * |
| 2575 | * We are fighting against races between a reader that |
| 2576 | * could be on another CPU trying to swap its reader |
| 2577 | * page with the buffer head. |
| 2578 | * |
| 2579 | * We are also fighting against interrupts coming in and |
| 2580 | * moving the head or tail on us as well. |
| 2581 | * |
| 2582 | * If the next page is the head page then we have filled |
| 2583 | * the buffer, unless the commit page is still on the |
| 2584 | * reader page. |
| 2585 | */ |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 2586 | if (rb_is_head_page(next_page, &tail_page->list)) { |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2587 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2588 | /* |
| 2589 | * If the commit is not on the reader page, then |
| 2590 | * move the header page. |
| 2591 | */ |
| 2592 | if (!rb_is_reader_page(cpu_buffer->commit_page)) { |
| 2593 | /* |
| 2594 | * If we are not in overwrite mode, |
| 2595 | * this is easy, just stop here. |
| 2596 | */ |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 2597 | if (!(buffer->flags & RB_FL_OVERWRITE)) { |
| 2598 | local_inc(&cpu_buffer->dropped_events); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2599 | goto out_reset; |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 2600 | } |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2601 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2602 | ret = rb_handle_head_page(cpu_buffer, |
| 2603 | tail_page, |
| 2604 | next_page); |
| 2605 | if (ret < 0) |
| 2606 | goto out_reset; |
| 2607 | if (ret) |
| 2608 | goto out_again; |
| 2609 | } else { |
| 2610 | /* |
| 2611 | * We need to be careful here too. The |
| 2612 | * commit page could still be on the reader |
| 2613 | * page. We could have a small buffer, and |
| 2614 | * have filled up the buffer with events |
| 2615 | * from interrupts and such, and wrapped. |
| 2616 | * |
Qiujun Huang | c6358ba | 2020-12-24 22:46:34 +0800 | [diff] [blame] | 2617 | * Note, if the tail page is also on the |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2618 | * reader_page, we let it move out. |
| 2619 | */ |
| 2620 | if (unlikely((cpu_buffer->commit_page != |
| 2621 | cpu_buffer->tail_page) && |
| 2622 | (cpu_buffer->commit_page == |
| 2623 | cpu_buffer->reader_page))) { |
| 2624 | local_inc(&cpu_buffer->commit_overrun); |
| 2625 | goto out_reset; |
| 2626 | } |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2627 | } |
| 2628 | } |
| 2629 | |
Steven Rostedt (Red Hat) | 7000498 | 2015-11-17 15:15:19 -0500 | [diff] [blame] | 2630 | rb_tail_page_update(cpu_buffer, tail_page, next_page); |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2631 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2632 | out_again: |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2633 | |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2634 | rb_reset_tail(cpu_buffer, tail, info); |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2635 | |
Steven Rostedt (Red Hat) | 4239c38 | 2015-11-17 16:36:06 -0500 | [diff] [blame] | 2636 | /* Commit what we have for now. */ |
| 2637 | rb_end_commit(cpu_buffer); |
| 2638 | /* rb_end_commit() decs committing */ |
| 2639 | local_inc(&cpu_buffer->committing); |
| 2640 | |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2641 | /* fail and let the caller try again */ |
| 2642 | return ERR_PTR(-EAGAIN); |
| 2643 | |
Steven Rostedt | 45141d4 | 2009-02-12 13:19:48 -0500 | [diff] [blame] | 2644 | out_reset: |
Lai Jiangshan | 6f3b344 | 2009-01-12 11:06:18 +0800 | [diff] [blame] | 2645 | /* reset write */ |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2646 | rb_reset_tail(cpu_buffer, tail, info); |
Lai Jiangshan | 6f3b344 | 2009-01-12 11:06:18 +0800 | [diff] [blame] | 2647 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2648 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2649 | } |
| 2650 | |
Steven Rostedt (VMware) | 74e8793 | 2020-06-30 12:47:56 -0400 | [diff] [blame] | 2651 | /* Slow path */ |
| 2652 | static struct ring_buffer_event * |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 2653 | rb_add_time_stamp(struct ring_buffer_event *event, u64 delta, bool abs) |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2654 | { |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 2655 | if (abs) |
| 2656 | event->type_len = RINGBUF_TYPE_TIME_STAMP; |
| 2657 | else |
| 2658 | event->type_len = RINGBUF_TYPE_TIME_EXTEND; |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2659 | |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 2660 | /* Not the first event on the page, or not delta? */ |
| 2661 | if (abs || rb_event_index(event)) { |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2662 | event->time_delta = delta & TS_MASK; |
| 2663 | event->array[0] = delta >> TS_SHIFT; |
| 2664 | } else { |
| 2665 | /* nope, just zero it */ |
| 2666 | event->time_delta = 0; |
| 2667 | event->array[0] = 0; |
| 2668 | } |
| 2669 | |
| 2670 | return skip_time_extend(event); |
| 2671 | } |
| 2672 | |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 2673 | #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK |
| 2674 | static inline bool sched_clock_stable(void) |
| 2675 | { |
| 2676 | return true; |
| 2677 | } |
| 2678 | #endif |
| 2679 | |
Steven Rostedt (VMware) | 74e8793 | 2020-06-30 12:47:56 -0400 | [diff] [blame] | 2680 | static void |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 2681 | rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer, |
| 2682 | struct rb_event_info *info) |
| 2683 | { |
| 2684 | u64 write_stamp; |
| 2685 | |
Steven Rostedt (VMware) | 29ce245 | 2020-07-01 13:10:19 -0400 | [diff] [blame] | 2686 | WARN_ONCE(1, "Delta way too big! %llu ts=%llu before=%llu after=%llu write stamp=%llu\n%s", |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 2687 | (unsigned long long)info->delta, |
| 2688 | (unsigned long long)info->ts, |
| 2689 | (unsigned long long)info->before, |
| 2690 | (unsigned long long)info->after, |
| 2691 | (unsigned long long)(rb_time_read(&cpu_buffer->write_stamp, &write_stamp) ? write_stamp : 0), |
| 2692 | sched_clock_stable() ? "" : |
| 2693 | "If you just came from a suspend/resume,\n" |
| 2694 | "please switch to the trace global clock:\n" |
| 2695 | " echo global > /sys/kernel/debug/tracing/trace_clock\n" |
| 2696 | "or add trace_clock=global to the kernel command line\n"); |
| 2697 | } |
| 2698 | |
Steven Rostedt (VMware) | 74e8793 | 2020-06-30 12:47:56 -0400 | [diff] [blame] | 2699 | static void rb_add_timestamp(struct ring_buffer_per_cpu *cpu_buffer, |
| 2700 | struct ring_buffer_event **event, |
| 2701 | struct rb_event_info *info, |
| 2702 | u64 *delta, |
| 2703 | unsigned int *length) |
| 2704 | { |
| 2705 | bool abs = info->add_timestamp & |
| 2706 | (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE); |
| 2707 | |
Steven Rostedt (VMware) | 29ce245 | 2020-07-01 13:10:19 -0400 | [diff] [blame] | 2708 | if (unlikely(info->delta > (1ULL << 59))) { |
| 2709 | /* did the clock go backwards */ |
| 2710 | if (info->before == info->after && info->before > info->ts) { |
| 2711 | /* not interrupted */ |
| 2712 | static int once; |
| 2713 | |
| 2714 | /* |
| 2715 | * This is possible with a recalibrating of the TSC. |
| 2716 | * Do not produce a call stack, but just report it. |
| 2717 | */ |
| 2718 | if (!once) { |
| 2719 | once++; |
| 2720 | pr_warn("Ring buffer clock went backwards: %llu -> %llu\n", |
| 2721 | info->before, info->ts); |
| 2722 | } |
| 2723 | } else |
| 2724 | rb_check_timestamp(cpu_buffer, info); |
| 2725 | if (!abs) |
| 2726 | info->delta = 0; |
| 2727 | } |
Steven Rostedt (VMware) | 74e8793 | 2020-06-30 12:47:56 -0400 | [diff] [blame] | 2728 | *event = rb_add_time_stamp(*event, info->delta, abs); |
| 2729 | *length -= RB_LEN_TIME_EXTEND; |
| 2730 | *delta = 0; |
| 2731 | } |
| 2732 | |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2733 | /** |
| 2734 | * rb_update_event - update event type and data |
Steven Rostedt (VMware) | cfc585a | 2020-01-14 16:27:51 -0500 | [diff] [blame] | 2735 | * @cpu_buffer: The per cpu buffer of the @event |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2736 | * @event: the event to update |
Steven Rostedt (VMware) | cfc585a | 2020-01-14 16:27:51 -0500 | [diff] [blame] | 2737 | * @info: The info to update the @event with (contains length and delta) |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2738 | * |
Steven Rostedt (VMware) | cfc585a | 2020-01-14 16:27:51 -0500 | [diff] [blame] | 2739 | * Update the type and data fields of the @event. The length |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2740 | * is the actual size that is written to the ring buffer, |
| 2741 | * and with this, we can determine what to place into the |
| 2742 | * data field. |
| 2743 | */ |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 2744 | static void |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2745 | rb_update_event(struct ring_buffer_per_cpu *cpu_buffer, |
| 2746 | struct ring_buffer_event *event, |
| 2747 | struct rb_event_info *info) |
| 2748 | { |
| 2749 | unsigned length = info->length; |
| 2750 | u64 delta = info->delta; |
Steven Rostedt (VMware) | 8672e494 | 2021-03-16 12:41:02 -0400 | [diff] [blame] | 2751 | unsigned int nest = local_read(&cpu_buffer->committing) - 1; |
| 2752 | |
| 2753 | if (nest < MAX_NEST) |
| 2754 | cpu_buffer->event_stamp[nest] = info->ts; |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2755 | |
| 2756 | /* |
| 2757 | * If we need to add a timestamp, then we |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 2758 | * add it to the start of the reserved space. |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2759 | */ |
Steven Rostedt (VMware) | 74e8793 | 2020-06-30 12:47:56 -0400 | [diff] [blame] | 2760 | if (unlikely(info->add_timestamp)) |
| 2761 | rb_add_timestamp(cpu_buffer, &event, info, &delta, &length); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2762 | |
| 2763 | event->time_delta = delta; |
| 2764 | length -= RB_EVNT_HDR_SIZE; |
Steven Rostedt (VMware) | adab66b | 2020-12-14 12:33:51 -0500 | [diff] [blame] | 2765 | if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) { |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2766 | event->type_len = 0; |
| 2767 | event->array[0] = length; |
| 2768 | } else |
| 2769 | event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT); |
| 2770 | } |
| 2771 | |
| 2772 | static unsigned rb_calculate_event_length(unsigned length) |
| 2773 | { |
| 2774 | struct ring_buffer_event event; /* Used only for sizeof array */ |
| 2775 | |
| 2776 | /* zero length can cause confusions */ |
| 2777 | if (!length) |
| 2778 | length++; |
| 2779 | |
Steven Rostedt (VMware) | adab66b | 2020-12-14 12:33:51 -0500 | [diff] [blame] | 2780 | if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2781 | length += sizeof(event.array[0]); |
| 2782 | |
| 2783 | length += RB_EVNT_HDR_SIZE; |
Steven Rostedt (VMware) | adab66b | 2020-12-14 12:33:51 -0500 | [diff] [blame] | 2784 | length = ALIGN(length, RB_ARCH_ALIGNMENT); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2785 | |
| 2786 | /* |
| 2787 | * In case the time delta is larger than the 27 bits for it |
| 2788 | * in the header, we need to add a timestamp. If another |
| 2789 | * event comes in when trying to discard this one to increase |
| 2790 | * the length, then the timestamp will be added in the allocated |
| 2791 | * space of this event. If length is bigger than the size needed |
| 2792 | * for the TIME_EXTEND, then padding has to be used. The events |
| 2793 | * length must be either RB_LEN_TIME_EXTEND, or greater than or equal |
| 2794 | * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding. |
| 2795 | * As length is a multiple of 4, we only need to worry if it |
| 2796 | * is 12 (RB_LEN_TIME_EXTEND + 4). |
| 2797 | */ |
| 2798 | if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT) |
| 2799 | length += RB_ALIGNMENT; |
| 2800 | |
| 2801 | return length; |
| 2802 | } |
| 2803 | |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2804 | static u64 rb_time_delta(struct ring_buffer_event *event) |
| 2805 | { |
| 2806 | switch (event->type_len) { |
| 2807 | case RINGBUF_TYPE_PADDING: |
| 2808 | return 0; |
| 2809 | |
| 2810 | case RINGBUF_TYPE_TIME_EXTEND: |
Steven Rostedt (VMware) | e20044f | 2021-03-16 12:41:01 -0400 | [diff] [blame] | 2811 | return rb_event_time_stamp(event); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2812 | |
| 2813 | case RINGBUF_TYPE_TIME_STAMP: |
| 2814 | return 0; |
| 2815 | |
| 2816 | case RINGBUF_TYPE_DATA: |
| 2817 | return event->time_delta; |
| 2818 | default: |
| 2819 | return 0; |
| 2820 | } |
| 2821 | } |
Steven Rostedt (Red Hat) | 9826b27 | 2015-05-28 17:36:45 -0400 | [diff] [blame] | 2822 | |
Steven Rostedt (Red Hat) | a4543a2 | 2015-05-29 09:40:18 -0400 | [diff] [blame] | 2823 | static inline int |
| 2824 | rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2825 | struct ring_buffer_event *event) |
| 2826 | { |
| 2827 | unsigned long new_index, old_index; |
| 2828 | struct buffer_page *bpage; |
| 2829 | unsigned long index; |
| 2830 | unsigned long addr; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2831 | u64 write_stamp; |
| 2832 | u64 delta; |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2833 | |
| 2834 | new_index = rb_event_index(event); |
| 2835 | old_index = new_index + rb_event_ts_length(event); |
| 2836 | addr = (unsigned long)event; |
| 2837 | addr &= PAGE_MASK; |
| 2838 | |
Steven Rostedt (Red Hat) | 8573636 | 2015-11-17 14:03:11 -0500 | [diff] [blame] | 2839 | bpage = READ_ONCE(cpu_buffer->tail_page); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2840 | |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2841 | delta = rb_time_delta(event); |
| 2842 | |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 2843 | if (!rb_time_read(&cpu_buffer->write_stamp, &write_stamp)) |
| 2844 | return 0; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2845 | |
| 2846 | /* Make sure the write stamp is read before testing the location */ |
| 2847 | barrier(); |
| 2848 | |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2849 | if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) { |
| 2850 | unsigned long write_mask = |
| 2851 | local_read(&bpage->write) & ~RB_WRITE_MASK; |
| 2852 | unsigned long event_length = rb_event_length(event); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2853 | |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2854 | /* Something came in, can't discard */ |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 2855 | if (!rb_time_cmpxchg(&cpu_buffer->write_stamp, |
| 2856 | write_stamp, write_stamp - delta)) |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2857 | return 0; |
| 2858 | |
| 2859 | /* |
Steven Rostedt (VMware) | 6f6be60 | 2021-03-03 18:03:52 -0500 | [diff] [blame] | 2860 | * It's possible that the event time delta is zero |
| 2861 | * (has the same time stamp as the previous event) |
| 2862 | * in which case write_stamp and before_stamp could |
| 2863 | * be the same. In such a case, force before_stamp |
| 2864 | * to be different than write_stamp. It doesn't |
| 2865 | * matter what it is, as long as its different. |
| 2866 | */ |
| 2867 | if (!delta) |
| 2868 | rb_time_set(&cpu_buffer->before_stamp, 0); |
| 2869 | |
| 2870 | /* |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2871 | * If an event were to come in now, it would see that the |
| 2872 | * write_stamp and the before_stamp are different, and assume |
| 2873 | * that this event just added itself before updating |
| 2874 | * the write stamp. The interrupting event will fix the |
| 2875 | * write stamp for us, and use the before stamp as its delta. |
| 2876 | */ |
| 2877 | |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2878 | /* |
| 2879 | * This is on the tail page. It is possible that |
| 2880 | * a write could come in and move the tail page |
| 2881 | * and write to the next page. That is fine |
| 2882 | * because we just shorten what is on this page. |
| 2883 | */ |
| 2884 | old_index += write_mask; |
| 2885 | new_index += write_mask; |
| 2886 | index = local_cmpxchg(&bpage->write, old_index, new_index); |
| 2887 | if (index == old_index) { |
| 2888 | /* update counters */ |
| 2889 | local_sub(event_length, &cpu_buffer->entries_bytes); |
| 2890 | return 1; |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | /* could not discard */ |
| 2895 | return 0; |
| 2896 | } |
| 2897 | |
| 2898 | static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer) |
| 2899 | { |
| 2900 | local_inc(&cpu_buffer->committing); |
| 2901 | local_inc(&cpu_buffer->commits); |
| 2902 | } |
| 2903 | |
Steven Rostedt (Red Hat) | 38e11df | 2016-11-23 20:42:31 -0500 | [diff] [blame] | 2904 | static __always_inline void |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2905 | rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer) |
| 2906 | { |
| 2907 | unsigned long max_count; |
| 2908 | |
| 2909 | /* |
| 2910 | * We only race with interrupts and NMIs on this CPU. |
| 2911 | * If we own the commit event, then we can commit |
| 2912 | * all others that interrupted us, since the interruptions |
| 2913 | * are in stack format (they finish before they come |
| 2914 | * back to us). This allows us to do a simple loop to |
| 2915 | * assign the commit to the tail. |
| 2916 | */ |
| 2917 | again: |
| 2918 | max_count = cpu_buffer->nr_pages * 100; |
| 2919 | |
Steven Rostedt (Red Hat) | 8573636 | 2015-11-17 14:03:11 -0500 | [diff] [blame] | 2920 | while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) { |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2921 | if (RB_WARN_ON(cpu_buffer, !(--max_count))) |
| 2922 | return; |
| 2923 | if (RB_WARN_ON(cpu_buffer, |
| 2924 | rb_is_reader_page(cpu_buffer->tail_page))) |
| 2925 | return; |
| 2926 | local_set(&cpu_buffer->commit_page->page->commit, |
| 2927 | rb_page_write(cpu_buffer->commit_page)); |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 2928 | rb_inc_page(&cpu_buffer->commit_page); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2929 | /* add barrier to keep gcc from optimizing too much */ |
| 2930 | barrier(); |
| 2931 | } |
| 2932 | while (rb_commit_index(cpu_buffer) != |
| 2933 | rb_page_write(cpu_buffer->commit_page)) { |
| 2934 | |
| 2935 | local_set(&cpu_buffer->commit_page->page->commit, |
| 2936 | rb_page_write(cpu_buffer->commit_page)); |
| 2937 | RB_WARN_ON(cpu_buffer, |
| 2938 | local_read(&cpu_buffer->commit_page->page->commit) & |
| 2939 | ~RB_WRITE_MASK); |
| 2940 | barrier(); |
| 2941 | } |
| 2942 | |
| 2943 | /* again, keep gcc from optimizing */ |
| 2944 | barrier(); |
| 2945 | |
| 2946 | /* |
| 2947 | * If an interrupt came in just after the first while loop |
| 2948 | * and pushed the tail page forward, we will be left with |
| 2949 | * a dangling commit that will never go forward. |
| 2950 | */ |
Steven Rostedt (Red Hat) | 8573636 | 2015-11-17 14:03:11 -0500 | [diff] [blame] | 2951 | if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page))) |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2952 | goto again; |
| 2953 | } |
| 2954 | |
Steven Rostedt (Red Hat) | 38e11df | 2016-11-23 20:42:31 -0500 | [diff] [blame] | 2955 | static __always_inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2956 | { |
| 2957 | unsigned long commits; |
| 2958 | |
| 2959 | if (RB_WARN_ON(cpu_buffer, |
| 2960 | !local_read(&cpu_buffer->committing))) |
| 2961 | return; |
| 2962 | |
| 2963 | again: |
| 2964 | commits = local_read(&cpu_buffer->commits); |
| 2965 | /* synchronize with interrupts */ |
| 2966 | barrier(); |
| 2967 | if (local_read(&cpu_buffer->committing) == 1) |
| 2968 | rb_set_commit_to_write(cpu_buffer); |
| 2969 | |
| 2970 | local_dec(&cpu_buffer->committing); |
| 2971 | |
| 2972 | /* synchronize with interrupts */ |
| 2973 | barrier(); |
| 2974 | |
| 2975 | /* |
| 2976 | * Need to account for interrupts coming in between the |
| 2977 | * updating of the commit page and the clearing of the |
| 2978 | * committing counter. |
| 2979 | */ |
| 2980 | if (unlikely(local_read(&cpu_buffer->commits) != commits) && |
| 2981 | !local_read(&cpu_buffer->committing)) { |
| 2982 | local_inc(&cpu_buffer->committing); |
| 2983 | goto again; |
| 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | static inline void rb_event_discard(struct ring_buffer_event *event) |
| 2988 | { |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 2989 | if (extended_time(event)) |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2990 | event = skip_time_extend(event); |
| 2991 | |
| 2992 | /* array[0] holds the actual length for the discarded event */ |
| 2993 | event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE; |
| 2994 | event->type_len = RINGBUF_TYPE_PADDING; |
| 2995 | /* time delta must be non zero */ |
| 2996 | if (!event->time_delta) |
| 2997 | event->time_delta = 1; |
| 2998 | } |
| 2999 | |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3000 | static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer, |
| 3001 | struct ring_buffer_event *event) |
| 3002 | { |
| 3003 | local_inc(&cpu_buffer->entries); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3004 | rb_end_commit(cpu_buffer); |
| 3005 | } |
| 3006 | |
| 3007 | static __always_inline void |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3008 | rb_wakeups(struct trace_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3009 | { |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 3010 | size_t nr_pages; |
| 3011 | size_t dirty; |
| 3012 | size_t full; |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3013 | |
| 3014 | if (buffer->irq_work.waiters_pending) { |
| 3015 | buffer->irq_work.waiters_pending = false; |
| 3016 | /* irq_work_queue() supplies it's own memory barriers */ |
| 3017 | irq_work_queue(&buffer->irq_work.work); |
| 3018 | } |
| 3019 | |
| 3020 | if (cpu_buffer->irq_work.waiters_pending) { |
| 3021 | cpu_buffer->irq_work.waiters_pending = false; |
| 3022 | /* irq_work_queue() supplies it's own memory barriers */ |
| 3023 | irq_work_queue(&cpu_buffer->irq_work.work); |
| 3024 | } |
| 3025 | |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 3026 | if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched)) |
| 3027 | return; |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3028 | |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 3029 | if (cpu_buffer->reader_page == cpu_buffer->commit_page) |
| 3030 | return; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 3031 | |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 3032 | if (!cpu_buffer->irq_work.full_waiters_pending) |
| 3033 | return; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 3034 | |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 3035 | cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched); |
| 3036 | |
| 3037 | full = cpu_buffer->shortest_full; |
| 3038 | nr_pages = cpu_buffer->nr_pages; |
| 3039 | dirty = ring_buffer_nr_dirty_pages(buffer, cpu_buffer->cpu); |
| 3040 | if (full && nr_pages && (dirty * 100) <= full * nr_pages) |
| 3041 | return; |
| 3042 | |
| 3043 | cpu_buffer->irq_work.wakeup_full = true; |
| 3044 | cpu_buffer->irq_work.full_waiters_pending = false; |
| 3045 | /* irq_work_queue() supplies it's own memory barriers */ |
| 3046 | irq_work_queue(&cpu_buffer->irq_work.work); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3047 | } |
| 3048 | |
Steven Rostedt (VMware) | 28575c6 | 2020-11-02 14:43:10 -0500 | [diff] [blame] | 3049 | #ifdef CONFIG_RING_BUFFER_RECORD_RECURSION |
| 3050 | # define do_ring_buffer_record_recursion() \ |
| 3051 | do_ftrace_record_recursion(_THIS_IP_, _RET_IP_) |
| 3052 | #else |
| 3053 | # define do_ring_buffer_record_recursion() do { } while (0) |
| 3054 | #endif |
| 3055 | |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3056 | /* |
| 3057 | * The lock and unlock are done within a preempt disable section. |
| 3058 | * The current_context per_cpu variable can only be modified |
| 3059 | * by the current task between lock and unlock. But it can |
Steven Rostedt (VMware) | a0e3a18 | 2018-01-15 10:47:09 -0500 | [diff] [blame] | 3060 | * be modified more than once via an interrupt. To pass this |
| 3061 | * information from the lock to the unlock without having to |
| 3062 | * access the 'in_interrupt()' functions again (which do show |
| 3063 | * a bit of overhead in something as critical as function tracing, |
| 3064 | * we use a bitmask trick. |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3065 | * |
Steven Rostedt (VMware) | b02414c | 2020-11-02 15:31:27 -0500 | [diff] [blame] | 3066 | * bit 1 = NMI context |
| 3067 | * bit 2 = IRQ context |
| 3068 | * bit 3 = SoftIRQ context |
| 3069 | * bit 4 = normal context. |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3070 | * |
Steven Rostedt (VMware) | a0e3a18 | 2018-01-15 10:47:09 -0500 | [diff] [blame] | 3071 | * This works because this is the order of contexts that can |
| 3072 | * preempt other contexts. A SoftIRQ never preempts an IRQ |
| 3073 | * context. |
| 3074 | * |
| 3075 | * When the context is determined, the corresponding bit is |
| 3076 | * checked and set (if it was set, then a recursion of that context |
| 3077 | * happened). |
| 3078 | * |
| 3079 | * On unlock, we need to clear this bit. To do so, just subtract |
| 3080 | * 1 from the current_context and AND it to itself. |
| 3081 | * |
| 3082 | * (binary) |
| 3083 | * 101 - 1 = 100 |
| 3084 | * 101 & 100 = 100 (clearing bit zero) |
| 3085 | * |
| 3086 | * 1010 - 1 = 1001 |
| 3087 | * 1010 & 1001 = 1000 (clearing bit 1) |
| 3088 | * |
| 3089 | * The least significant bit can be cleared this way, and it |
| 3090 | * just so happens that it is the same bit corresponding to |
| 3091 | * the current context. |
Steven Rostedt (VMware) | b02414c | 2020-11-02 15:31:27 -0500 | [diff] [blame] | 3092 | * |
| 3093 | * Now the TRANSITION bit breaks the above slightly. The TRANSITION bit |
| 3094 | * is set when a recursion is detected at the current context, and if |
| 3095 | * the TRANSITION bit is already set, it will fail the recursion. |
| 3096 | * This is needed because there's a lag between the changing of |
| 3097 | * interrupt context and updating the preempt count. In this case, |
| 3098 | * a false positive will be found. To handle this, one extra recursion |
| 3099 | * is allowed, and this is done by the TRANSITION bit. If the TRANSITION |
| 3100 | * bit is already set, then it is considered a recursion and the function |
| 3101 | * ends. Otherwise, the TRANSITION bit is set, and that bit is returned. |
| 3102 | * |
| 3103 | * On the trace_recursive_unlock(), the TRANSITION bit will be the first |
| 3104 | * to be cleared. Even if it wasn't the context that set it. That is, |
| 3105 | * if an interrupt comes in while NORMAL bit is set and the ring buffer |
| 3106 | * is called before preempt_count() is updated, since the check will |
| 3107 | * be on the NORMAL bit, the TRANSITION bit will then be set. If an |
| 3108 | * NMI then comes in, it will set the NMI bit, but when the NMI code |
| 3109 | * does the trace_recursive_unlock() it will clear the TRANSTION bit |
| 3110 | * and leave the NMI bit set. But this is fine, because the interrupt |
| 3111 | * code that set the TRANSITION bit will then clear the NMI bit when it |
| 3112 | * calls trace_recursive_unlock(). If another NMI comes in, it will |
| 3113 | * set the TRANSITION bit and continue. |
| 3114 | * |
| 3115 | * Note: The TRANSITION bit only handles a single transition between context. |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3116 | */ |
| 3117 | |
| 3118 | static __always_inline int |
| 3119 | trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer) |
| 3120 | { |
Steven Rostedt (VMware) | a0e3a18 | 2018-01-15 10:47:09 -0500 | [diff] [blame] | 3121 | unsigned int val = cpu_buffer->current_context; |
| 3122 | unsigned long pc = preempt_count(); |
| 3123 | int bit; |
| 3124 | |
| 3125 | if (!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET))) |
| 3126 | bit = RB_CTX_NORMAL; |
| 3127 | else |
| 3128 | bit = pc & NMI_MASK ? RB_CTX_NMI : |
Steven Rostedt (VMware) | 0164e0d | 2018-01-18 15:42:09 -0500 | [diff] [blame] | 3129 | pc & HARDIRQ_MASK ? RB_CTX_IRQ : RB_CTX_SOFTIRQ; |
Steven Rostedt (VMware) | a0e3a18 | 2018-01-15 10:47:09 -0500 | [diff] [blame] | 3130 | |
Steven Rostedt (VMware) | b02414c | 2020-11-02 15:31:27 -0500 | [diff] [blame] | 3131 | if (unlikely(val & (1 << (bit + cpu_buffer->nest)))) { |
| 3132 | /* |
| 3133 | * It is possible that this was called by transitioning |
| 3134 | * between interrupt context, and preempt_count() has not |
| 3135 | * been updated yet. In this case, use the TRANSITION bit. |
| 3136 | */ |
| 3137 | bit = RB_CTX_TRANSITION; |
Steven Rostedt (VMware) | 28575c6 | 2020-11-02 14:43:10 -0500 | [diff] [blame] | 3138 | if (val & (1 << (bit + cpu_buffer->nest))) { |
| 3139 | do_ring_buffer_record_recursion(); |
Steven Rostedt (VMware) | b02414c | 2020-11-02 15:31:27 -0500 | [diff] [blame] | 3140 | return 1; |
Steven Rostedt (VMware) | 28575c6 | 2020-11-02 14:43:10 -0500 | [diff] [blame] | 3141 | } |
Steven Rostedt (VMware) | b02414c | 2020-11-02 15:31:27 -0500 | [diff] [blame] | 3142 | } |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3143 | |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3144 | val |= (1 << (bit + cpu_buffer->nest)); |
Steven Rostedt (VMware) | a0e3a18 | 2018-01-15 10:47:09 -0500 | [diff] [blame] | 3145 | cpu_buffer->current_context = val; |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3146 | |
| 3147 | return 0; |
| 3148 | } |
| 3149 | |
| 3150 | static __always_inline void |
| 3151 | trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer) |
| 3152 | { |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3153 | cpu_buffer->current_context &= |
| 3154 | cpu_buffer->current_context - (1 << cpu_buffer->nest); |
| 3155 | } |
| 3156 | |
Steven Rostedt (VMware) | b02414c | 2020-11-02 15:31:27 -0500 | [diff] [blame] | 3157 | /* The recursive locking above uses 5 bits */ |
| 3158 | #define NESTED_BITS 5 |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3159 | |
| 3160 | /** |
| 3161 | * ring_buffer_nest_start - Allow to trace while nested |
| 3162 | * @buffer: The ring buffer to modify |
| 3163 | * |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 3164 | * The ring buffer has a safety mechanism to prevent recursion. |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3165 | * But there may be a case where a trace needs to be done while |
| 3166 | * tracing something else. In this case, calling this function |
| 3167 | * will allow this function to nest within a currently active |
| 3168 | * ring_buffer_lock_reserve(). |
| 3169 | * |
| 3170 | * Call this function before calling another ring_buffer_lock_reserve() and |
| 3171 | * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit(). |
| 3172 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3173 | void ring_buffer_nest_start(struct trace_buffer *buffer) |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3174 | { |
| 3175 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3176 | int cpu; |
| 3177 | |
| 3178 | /* Enabled by ring_buffer_nest_end() */ |
| 3179 | preempt_disable_notrace(); |
| 3180 | cpu = raw_smp_processor_id(); |
| 3181 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 3182 | /* This is the shift value for the above recursive locking */ |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3183 | cpu_buffer->nest += NESTED_BITS; |
| 3184 | } |
| 3185 | |
| 3186 | /** |
| 3187 | * ring_buffer_nest_end - Allow to trace while nested |
| 3188 | * @buffer: The ring buffer to modify |
| 3189 | * |
| 3190 | * Must be called after ring_buffer_nest_start() and after the |
| 3191 | * ring_buffer_unlock_commit(). |
| 3192 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3193 | void ring_buffer_nest_end(struct trace_buffer *buffer) |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3194 | { |
| 3195 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3196 | int cpu; |
| 3197 | |
| 3198 | /* disabled by ring_buffer_nest_start() */ |
| 3199 | cpu = raw_smp_processor_id(); |
| 3200 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 3201 | /* This is the shift value for the above recursive locking */ |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3202 | cpu_buffer->nest -= NESTED_BITS; |
| 3203 | preempt_enable_notrace(); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | /** |
| 3207 | * ring_buffer_unlock_commit - commit a reserved |
| 3208 | * @buffer: The buffer to commit to |
| 3209 | * @event: The event pointer to commit. |
| 3210 | * |
| 3211 | * This commits the data to the ring buffer, and releases any locks held. |
| 3212 | * |
| 3213 | * Must be paired with ring_buffer_lock_reserve. |
| 3214 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3215 | int ring_buffer_unlock_commit(struct trace_buffer *buffer, |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3216 | struct ring_buffer_event *event) |
| 3217 | { |
| 3218 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3219 | int cpu = raw_smp_processor_id(); |
| 3220 | |
| 3221 | cpu_buffer = buffer->buffers[cpu]; |
| 3222 | |
| 3223 | rb_commit(cpu_buffer, event); |
| 3224 | |
| 3225 | rb_wakeups(buffer, cpu_buffer); |
| 3226 | |
| 3227 | trace_recursive_unlock(cpu_buffer); |
| 3228 | |
| 3229 | preempt_enable_notrace(); |
| 3230 | |
| 3231 | return 0; |
| 3232 | } |
| 3233 | EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit); |
Steven Rostedt (Red Hat) | a4543a2 | 2015-05-29 09:40:18 -0400 | [diff] [blame] | 3234 | |
Steven Rostedt (VMware) | 5b7be9c | 2020-11-30 23:37:33 -0500 | [diff] [blame] | 3235 | /* Special value to validate all deltas on a page. */ |
| 3236 | #define CHECK_FULL_PAGE 1L |
| 3237 | |
| 3238 | #ifdef CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS |
| 3239 | static void dump_buffer_page(struct buffer_data_page *bpage, |
| 3240 | struct rb_event_info *info, |
| 3241 | unsigned long tail) |
| 3242 | { |
| 3243 | struct ring_buffer_event *event; |
| 3244 | u64 ts, delta; |
| 3245 | int e; |
| 3246 | |
| 3247 | ts = bpage->time_stamp; |
| 3248 | pr_warn(" [%lld] PAGE TIME STAMP\n", ts); |
| 3249 | |
| 3250 | for (e = 0; e < tail; e += rb_event_length(event)) { |
| 3251 | |
| 3252 | event = (struct ring_buffer_event *)(bpage->data + e); |
| 3253 | |
| 3254 | switch (event->type_len) { |
| 3255 | |
| 3256 | case RINGBUF_TYPE_TIME_EXTEND: |
Steven Rostedt (VMware) | e20044f | 2021-03-16 12:41:01 -0400 | [diff] [blame] | 3257 | delta = rb_event_time_stamp(event); |
Steven Rostedt (VMware) | 5b7be9c | 2020-11-30 23:37:33 -0500 | [diff] [blame] | 3258 | ts += delta; |
| 3259 | pr_warn(" [%lld] delta:%lld TIME EXTEND\n", ts, delta); |
| 3260 | break; |
| 3261 | |
| 3262 | case RINGBUF_TYPE_TIME_STAMP: |
Steven Rostedt (VMware) | e20044f | 2021-03-16 12:41:01 -0400 | [diff] [blame] | 3263 | delta = rb_event_time_stamp(event); |
Steven Rostedt (VMware) | 5b7be9c | 2020-11-30 23:37:33 -0500 | [diff] [blame] | 3264 | ts = delta; |
| 3265 | pr_warn(" [%lld] absolute:%lld TIME STAMP\n", ts, delta); |
| 3266 | break; |
| 3267 | |
| 3268 | case RINGBUF_TYPE_PADDING: |
| 3269 | ts += event->time_delta; |
| 3270 | pr_warn(" [%lld] delta:%d PADDING\n", ts, event->time_delta); |
| 3271 | break; |
| 3272 | |
| 3273 | case RINGBUF_TYPE_DATA: |
| 3274 | ts += event->time_delta; |
| 3275 | pr_warn(" [%lld] delta:%d\n", ts, event->time_delta); |
| 3276 | break; |
| 3277 | |
| 3278 | default: |
| 3279 | break; |
| 3280 | } |
| 3281 | } |
| 3282 | } |
| 3283 | |
| 3284 | static DEFINE_PER_CPU(atomic_t, checking); |
| 3285 | static atomic_t ts_dump; |
| 3286 | |
| 3287 | /* |
| 3288 | * Check if the current event time stamp matches the deltas on |
| 3289 | * the buffer page. |
| 3290 | */ |
| 3291 | static void check_buffer(struct ring_buffer_per_cpu *cpu_buffer, |
| 3292 | struct rb_event_info *info, |
| 3293 | unsigned long tail) |
| 3294 | { |
| 3295 | struct ring_buffer_event *event; |
| 3296 | struct buffer_data_page *bpage; |
| 3297 | u64 ts, delta; |
| 3298 | bool full = false; |
| 3299 | int e; |
| 3300 | |
| 3301 | bpage = info->tail_page->page; |
| 3302 | |
| 3303 | if (tail == CHECK_FULL_PAGE) { |
| 3304 | full = true; |
| 3305 | tail = local_read(&bpage->commit); |
| 3306 | } else if (info->add_timestamp & |
| 3307 | (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)) { |
| 3308 | /* Ignore events with absolute time stamps */ |
| 3309 | return; |
| 3310 | } |
| 3311 | |
| 3312 | /* |
| 3313 | * Do not check the first event (skip possible extends too). |
| 3314 | * Also do not check if previous events have not been committed. |
| 3315 | */ |
| 3316 | if (tail <= 8 || tail > local_read(&bpage->commit)) |
| 3317 | return; |
| 3318 | |
| 3319 | /* |
| 3320 | * If this interrupted another event, |
| 3321 | */ |
| 3322 | if (atomic_inc_return(this_cpu_ptr(&checking)) != 1) |
| 3323 | goto out; |
| 3324 | |
| 3325 | ts = bpage->time_stamp; |
| 3326 | |
| 3327 | for (e = 0; e < tail; e += rb_event_length(event)) { |
| 3328 | |
| 3329 | event = (struct ring_buffer_event *)(bpage->data + e); |
| 3330 | |
| 3331 | switch (event->type_len) { |
| 3332 | |
| 3333 | case RINGBUF_TYPE_TIME_EXTEND: |
Steven Rostedt (VMware) | e20044f | 2021-03-16 12:41:01 -0400 | [diff] [blame] | 3334 | delta = rb_event_time_stamp(event); |
Steven Rostedt (VMware) | 5b7be9c | 2020-11-30 23:37:33 -0500 | [diff] [blame] | 3335 | ts += delta; |
| 3336 | break; |
| 3337 | |
| 3338 | case RINGBUF_TYPE_TIME_STAMP: |
Steven Rostedt (VMware) | e20044f | 2021-03-16 12:41:01 -0400 | [diff] [blame] | 3339 | delta = rb_event_time_stamp(event); |
Steven Rostedt (VMware) | 5b7be9c | 2020-11-30 23:37:33 -0500 | [diff] [blame] | 3340 | ts = delta; |
| 3341 | break; |
| 3342 | |
| 3343 | case RINGBUF_TYPE_PADDING: |
| 3344 | if (event->time_delta == 1) |
| 3345 | break; |
| 3346 | /* fall through */ |
| 3347 | case RINGBUF_TYPE_DATA: |
| 3348 | ts += event->time_delta; |
| 3349 | break; |
| 3350 | |
| 3351 | default: |
| 3352 | RB_WARN_ON(cpu_buffer, 1); |
| 3353 | } |
| 3354 | } |
| 3355 | if ((full && ts > info->ts) || |
| 3356 | (!full && ts + info->delta != info->ts)) { |
| 3357 | /* If another report is happening, ignore this one */ |
| 3358 | if (atomic_inc_return(&ts_dump) != 1) { |
| 3359 | atomic_dec(&ts_dump); |
| 3360 | goto out; |
| 3361 | } |
| 3362 | atomic_inc(&cpu_buffer->record_disabled); |
Steven Rostedt (VMware) | 6549de1 | 2021-03-03 18:23:40 -0500 | [diff] [blame] | 3363 | /* There's some cases in boot up that this can happen */ |
| 3364 | WARN_ON_ONCE(system_state != SYSTEM_BOOTING); |
| 3365 | pr_warn("[CPU: %d]TIME DOES NOT MATCH expected:%lld actual:%lld delta:%lld before:%lld after:%lld%s\n", |
| 3366 | cpu_buffer->cpu, |
| 3367 | ts + info->delta, info->ts, info->delta, |
| 3368 | info->before, info->after, |
| 3369 | full ? " (full)" : ""); |
Steven Rostedt (VMware) | 5b7be9c | 2020-11-30 23:37:33 -0500 | [diff] [blame] | 3370 | dump_buffer_page(bpage, info, tail); |
| 3371 | atomic_dec(&ts_dump); |
| 3372 | /* Do not re-enable checking */ |
| 3373 | return; |
| 3374 | } |
| 3375 | out: |
| 3376 | atomic_dec(this_cpu_ptr(&checking)); |
| 3377 | } |
| 3378 | #else |
| 3379 | static inline void check_buffer(struct ring_buffer_per_cpu *cpu_buffer, |
| 3380 | struct rb_event_info *info, |
| 3381 | unsigned long tail) |
| 3382 | { |
| 3383 | } |
| 3384 | #endif /* CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS */ |
| 3385 | |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3386 | static struct ring_buffer_event * |
| 3387 | __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3388 | struct rb_event_info *info) |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3389 | { |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3390 | struct ring_buffer_event *event; |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3391 | struct buffer_page *tail_page; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3392 | unsigned long tail, write, w; |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3393 | bool a_ok; |
| 3394 | bool b_ok; |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 3395 | |
Steven Rostedt (Red Hat) | 8573636 | 2015-11-17 14:03:11 -0500 | [diff] [blame] | 3396 | /* Don't let the compiler play games with cpu_buffer->tail_page */ |
| 3397 | tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3398 | |
| 3399 | /*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK; |
| 3400 | barrier(); |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 3401 | b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before); |
| 3402 | a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3403 | barrier(); |
| 3404 | info->ts = rb_time_stamp(cpu_buffer->buffer); |
| 3405 | |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 3406 | if ((info->add_timestamp & RB_ADD_STAMP_ABSOLUTE)) { |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3407 | info->delta = info->ts; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3408 | } else { |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 3409 | /* |
| 3410 | * If interrupting an event time update, we may need an |
| 3411 | * absolute timestamp. |
| 3412 | * Don't bother if this is the start of a new page (w == 0). |
| 3413 | */ |
| 3414 | if (unlikely(!a_ok || !b_ok || (info->before != info->after && w))) { |
| 3415 | info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND; |
| 3416 | info->length += RB_LEN_TIME_EXTEND; |
| 3417 | } else { |
| 3418 | info->delta = info->ts - info->after; |
| 3419 | if (unlikely(test_time_stamp(info->delta))) { |
| 3420 | info->add_timestamp |= RB_ADD_STAMP_EXTEND; |
| 3421 | info->length += RB_LEN_TIME_EXTEND; |
| 3422 | } |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3423 | } |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 3424 | } |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3425 | |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3426 | /*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3427 | |
| 3428 | /*C*/ write = local_add_return(info->length, &tail_page->write); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3429 | |
| 3430 | /* set write to only the index of the write */ |
| 3431 | write &= RB_WRITE_MASK; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3432 | |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3433 | tail = write - info->length; |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3434 | |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3435 | /* See if we shot pass the end of this buffer page */ |
| 3436 | if (unlikely(write > BUF_PAGE_SIZE)) { |
Steven Rostedt (VMware) | 68e10d5 | 2020-11-30 23:16:03 -0500 | [diff] [blame] | 3437 | /* before and after may now different, fix it up*/ |
| 3438 | b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before); |
| 3439 | a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after); |
| 3440 | if (a_ok && b_ok && info->before != info->after) |
| 3441 | (void)rb_time_cmpxchg(&cpu_buffer->before_stamp, |
| 3442 | info->before, info->after); |
Steven Rostedt (VMware) | 5b7be9c | 2020-11-30 23:37:33 -0500 | [diff] [blame] | 3443 | if (a_ok && b_ok) |
| 3444 | check_buffer(cpu_buffer, info, CHECK_FULL_PAGE); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3445 | return rb_move_tail(cpu_buffer, tail, info); |
| 3446 | } |
| 3447 | |
| 3448 | if (likely(tail == w)) { |
| 3449 | u64 save_before; |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3450 | bool s_ok; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3451 | |
| 3452 | /* Nothing interrupted us between A and C */ |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3453 | /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3454 | barrier(); |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3455 | /*E*/ s_ok = rb_time_read(&cpu_buffer->before_stamp, &save_before); |
| 3456 | RB_WARN_ON(cpu_buffer, !s_ok); |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 3457 | if (likely(!(info->add_timestamp & |
| 3458 | (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)))) |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3459 | /* This did not interrupt any time update */ |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 3460 | info->delta = info->ts - info->after; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3461 | else |
Qiujun Huang | 82db909 | 2020-10-14 23:27:49 +0800 | [diff] [blame] | 3462 | /* Just use full timestamp for interrupting event */ |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3463 | info->delta = info->ts; |
| 3464 | barrier(); |
Steven Rostedt (VMware) | 5b7be9c | 2020-11-30 23:37:33 -0500 | [diff] [blame] | 3465 | check_buffer(cpu_buffer, info, tail); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3466 | if (unlikely(info->ts != save_before)) { |
| 3467 | /* SLOW PATH - Interrupted between C and E */ |
| 3468 | |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 3469 | a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after); |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3470 | RB_WARN_ON(cpu_buffer, !a_ok); |
| 3471 | |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3472 | /* Write stamp must only go forward */ |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 3473 | if (save_before > info->after) { |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3474 | /* |
| 3475 | * We do not care about the result, only that |
| 3476 | * it gets updated atomically. |
| 3477 | */ |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 3478 | (void)rb_time_cmpxchg(&cpu_buffer->write_stamp, |
| 3479 | info->after, save_before); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3480 | } |
| 3481 | } |
| 3482 | } else { |
| 3483 | u64 ts; |
| 3484 | /* SLOW PATH - Interrupted between A and C */ |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 3485 | a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after); |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3486 | /* Was interrupted before here, write_stamp must be valid */ |
| 3487 | RB_WARN_ON(cpu_buffer, !a_ok); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3488 | ts = rb_time_stamp(cpu_buffer->buffer); |
| 3489 | barrier(); |
| 3490 | /*E*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) && |
Andrea Righi | 8785f51 | 2020-11-28 10:15:17 +0100 | [diff] [blame] | 3491 | info->after < ts && |
| 3492 | rb_time_cmpxchg(&cpu_buffer->write_stamp, |
| 3493 | info->after, ts)) { |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3494 | /* Nothing came after this event between C and E */ |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 3495 | info->delta = ts - info->after; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3496 | } else { |
| 3497 | /* |
Qiujun Huang | 82db909 | 2020-10-14 23:27:49 +0800 | [diff] [blame] | 3498 | * Interrupted between C and E: |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3499 | * Lost the previous events time stamp. Just set the |
| 3500 | * delta to zero, and this will be the same time as |
| 3501 | * the event this event interrupted. And the events that |
| 3502 | * came after this will still be correct (as they would |
| 3503 | * have built their delta on the previous event. |
| 3504 | */ |
| 3505 | info->delta = 0; |
| 3506 | } |
Steven Rostedt (VMware) | 8672e494 | 2021-03-16 12:41:02 -0400 | [diff] [blame] | 3507 | info->ts = ts; |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 3508 | info->add_timestamp &= ~RB_ADD_STAMP_FORCE; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3509 | } |
| 3510 | |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3511 | /* |
| 3512 | * If this is the first commit on the page, then it has the same |
| 3513 | * timestamp as the page itself. |
| 3514 | */ |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 3515 | if (unlikely(!tail && !(info->add_timestamp & |
| 3516 | (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)))) |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3517 | info->delta = 0; |
| 3518 | |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3519 | /* We reserved something on the buffer */ |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3520 | |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3521 | event = __rb_page_index(tail_page, tail); |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3522 | rb_update_event(cpu_buffer, event, info); |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3523 | |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 3524 | local_inc(&tail_page->entries); |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3525 | |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3526 | /* |
| 3527 | * If this is the first commit on the page, then update |
| 3528 | * its timestamp. |
| 3529 | */ |
Steven Rostedt (VMware) | 75b21c6 | 2020-06-30 09:04:35 -0400 | [diff] [blame] | 3530 | if (unlikely(!tail)) |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3531 | tail_page->page->time_stamp = info->ts; |
| 3532 | |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 3533 | /* account for these added bytes */ |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3534 | local_add(info->length, &cpu_buffer->entries_bytes); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 3535 | |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3536 | return event; |
| 3537 | } |
| 3538 | |
Steven Rostedt (Red Hat) | fa7ffb3 | 2016-11-23 11:36:30 -0500 | [diff] [blame] | 3539 | static __always_inline struct ring_buffer_event * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3540 | rb_reserve_next_event(struct trace_buffer *buffer, |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 3541 | struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt | 1cd8d73 | 2009-05-11 14:08:09 -0400 | [diff] [blame] | 3542 | unsigned long length) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3543 | { |
| 3544 | struct ring_buffer_event *event; |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3545 | struct rb_event_info info; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3546 | int nr_loops = 0; |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 3547 | int add_ts_default; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3548 | |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3549 | rb_start_commit(cpu_buffer); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3550 | /* The commit page can not change after this */ |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3551 | |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 3552 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 3553 | /* |
| 3554 | * Due to the ability to swap a cpu buffer from a buffer |
| 3555 | * it is possible it was swapped before we committed. |
| 3556 | * (committing stops a swap). We check for it here and |
| 3557 | * if it happened, we have to fail the write. |
| 3558 | */ |
| 3559 | barrier(); |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 3560 | if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) { |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 3561 | local_dec(&cpu_buffer->committing); |
| 3562 | local_dec(&cpu_buffer->commits); |
| 3563 | return NULL; |
| 3564 | } |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 3565 | #endif |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3566 | |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3567 | info.length = rb_calculate_event_length(length); |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 3568 | |
| 3569 | if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) { |
| 3570 | add_ts_default = RB_ADD_STAMP_ABSOLUTE; |
| 3571 | info.length += RB_LEN_TIME_EXTEND; |
| 3572 | } else { |
| 3573 | add_ts_default = RB_ADD_STAMP_NONE; |
| 3574 | } |
| 3575 | |
Steven Rostedt (Red Hat) | a4543a2 | 2015-05-29 09:40:18 -0400 | [diff] [blame] | 3576 | again: |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 3577 | info.add_timestamp = add_ts_default; |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3578 | info.delta = 0; |
| 3579 | |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3580 | /* |
| 3581 | * We allow for interrupts to reenter here and do a trace. |
| 3582 | * If one does, it will cause this original code to loop |
| 3583 | * back here. Even with heavy interrupts happening, this |
| 3584 | * should only happen a few times in a row. If this happens |
| 3585 | * 1000 times in a row, there must be either an interrupt |
| 3586 | * storm or we have something buggy. |
| 3587 | * Bail! |
| 3588 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 3589 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000)) |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3590 | goto out_fail; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3591 | |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3592 | event = __rb_reserve_next(cpu_buffer, &info); |
| 3593 | |
Steven Rostedt (Red Hat) | bd1b7cd | 2015-11-23 17:35:24 -0500 | [diff] [blame] | 3594 | if (unlikely(PTR_ERR(event) == -EAGAIN)) { |
Steven Rostedt (VMware) | 58fbc3c | 2020-06-30 08:59:26 -0400 | [diff] [blame] | 3595 | if (info.add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND)) |
Steven Rostedt (Red Hat) | bd1b7cd | 2015-11-23 17:35:24 -0500 | [diff] [blame] | 3596 | info.length -= RB_LEN_TIME_EXTEND; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3597 | goto again; |
Steven Rostedt (Red Hat) | bd1b7cd | 2015-11-23 17:35:24 -0500 | [diff] [blame] | 3598 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3599 | |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3600 | if (likely(event)) |
| 3601 | return event; |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3602 | out_fail: |
| 3603 | rb_end_commit(cpu_buffer); |
| 3604 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | /** |
| 3608 | * ring_buffer_lock_reserve - reserve a part of the buffer |
| 3609 | * @buffer: the ring buffer to reserve from |
| 3610 | * @length: the length of the data to reserve (excluding event header) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3611 | * |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 3612 | * Returns a reserved event on the ring buffer to copy directly to. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3613 | * The user of this interface will need to get the body to write into |
| 3614 | * and can use the ring_buffer_event_data() interface. |
| 3615 | * |
| 3616 | * The length is the length of the data needed, not the event length |
| 3617 | * which also includes the event header. |
| 3618 | * |
| 3619 | * Must be paired with ring_buffer_unlock_commit, unless NULL is returned. |
| 3620 | * If NULL is returned, then nothing has been allocated or locked. |
| 3621 | */ |
| 3622 | struct ring_buffer_event * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3623 | ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3624 | { |
| 3625 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3626 | struct ring_buffer_event *event; |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3627 | int cpu; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3628 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3629 | /* If we are tracing schedule, we don't want to recurse */ |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3630 | preempt_disable_notrace(); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3631 | |
Steven Rostedt (Red Hat) | 3205f80 | 2015-05-21 17:39:29 -0400 | [diff] [blame] | 3632 | if (unlikely(atomic_read(&buffer->record_disabled))) |
Steven Rostedt (Red Hat) | 58a09ec | 2015-05-27 10:27:47 -0400 | [diff] [blame] | 3633 | goto out; |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame] | 3634 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3635 | cpu = raw_smp_processor_id(); |
| 3636 | |
Steven Rostedt (Red Hat) | 3205f80 | 2015-05-21 17:39:29 -0400 | [diff] [blame] | 3637 | if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask))) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3638 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3639 | |
| 3640 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3641 | |
Steven Rostedt (Red Hat) | 3205f80 | 2015-05-21 17:39:29 -0400 | [diff] [blame] | 3642 | if (unlikely(atomic_read(&cpu_buffer->record_disabled))) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3643 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3644 | |
Steven Rostedt (Red Hat) | 3205f80 | 2015-05-21 17:39:29 -0400 | [diff] [blame] | 3645 | if (unlikely(length > BUF_MAX_DATA_SIZE)) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3646 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3647 | |
Steven Rostedt (Red Hat) | 58a09ec | 2015-05-27 10:27:47 -0400 | [diff] [blame] | 3648 | if (unlikely(trace_recursive_lock(cpu_buffer))) |
| 3649 | goto out; |
| 3650 | |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 3651 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3652 | if (!event) |
Steven Rostedt (Red Hat) | 58a09ec | 2015-05-27 10:27:47 -0400 | [diff] [blame] | 3653 | goto out_unlock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3654 | |
| 3655 | return event; |
| 3656 | |
Steven Rostedt (Red Hat) | 58a09ec | 2015-05-27 10:27:47 -0400 | [diff] [blame] | 3657 | out_unlock: |
| 3658 | trace_recursive_unlock(cpu_buffer); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3659 | out: |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3660 | preempt_enable_notrace(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3661 | return NULL; |
| 3662 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3663 | EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3664 | |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 3665 | /* |
| 3666 | * Decrement the entries to the page that an event is on. |
| 3667 | * The event does not even need to exist, only the pointer |
| 3668 | * to the page it is on. This may only be called before the commit |
| 3669 | * takes place. |
| 3670 | */ |
| 3671 | static inline void |
| 3672 | rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer, |
| 3673 | struct ring_buffer_event *event) |
| 3674 | { |
| 3675 | unsigned long addr = (unsigned long)event; |
| 3676 | struct buffer_page *bpage = cpu_buffer->commit_page; |
| 3677 | struct buffer_page *start; |
| 3678 | |
| 3679 | addr &= PAGE_MASK; |
| 3680 | |
| 3681 | /* Do the likely case first */ |
| 3682 | if (likely(bpage->page == (void *)addr)) { |
| 3683 | local_dec(&bpage->entries); |
| 3684 | return; |
| 3685 | } |
| 3686 | |
| 3687 | /* |
| 3688 | * Because the commit page may be on the reader page we |
| 3689 | * start with the next page and check the end loop there. |
| 3690 | */ |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 3691 | rb_inc_page(&bpage); |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 3692 | start = bpage; |
| 3693 | do { |
| 3694 | if (bpage->page == (void *)addr) { |
| 3695 | local_dec(&bpage->entries); |
| 3696 | return; |
| 3697 | } |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 3698 | rb_inc_page(&bpage); |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 3699 | } while (bpage != start); |
| 3700 | |
| 3701 | /* commit not part of this buffer?? */ |
| 3702 | RB_WARN_ON(cpu_buffer, 1); |
| 3703 | } |
| 3704 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3705 | /** |
Qiujun Huang | 8888349 | 2020-11-12 23:18:00 +0800 | [diff] [blame] | 3706 | * ring_buffer_discard_commit - discard an event that has not been committed |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3707 | * @buffer: the ring buffer |
| 3708 | * @event: non committed event to discard |
| 3709 | * |
Steven Rostedt | dc892f7 | 2009-09-03 15:33:41 -0400 | [diff] [blame] | 3710 | * Sometimes an event that is in the ring buffer needs to be ignored. |
| 3711 | * This function lets the user discard an event in the ring buffer |
| 3712 | * and then that event will not be read later. |
| 3713 | * |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 3714 | * This function only works if it is called before the item has been |
Steven Rostedt | dc892f7 | 2009-09-03 15:33:41 -0400 | [diff] [blame] | 3715 | * committed. It will try to free the event from the ring buffer |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3716 | * if another event has not been added behind it. |
| 3717 | * |
| 3718 | * If another event has been added behind it, it will set the event |
| 3719 | * up as discarded, and perform the commit. |
| 3720 | * |
| 3721 | * If this function is called, do not call ring_buffer_unlock_commit on |
| 3722 | * the event. |
| 3723 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3724 | void ring_buffer_discard_commit(struct trace_buffer *buffer, |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3725 | struct ring_buffer_event *event) |
| 3726 | { |
| 3727 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3728 | int cpu; |
| 3729 | |
| 3730 | /* The event is discarded regardless */ |
Frederic Weisbecker | f3b9aae | 2009-04-19 23:39:33 +0200 | [diff] [blame] | 3731 | rb_event_discard(event); |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3732 | |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3733 | cpu = smp_processor_id(); |
| 3734 | cpu_buffer = buffer->buffers[cpu]; |
| 3735 | |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3736 | /* |
| 3737 | * This must only be called if the event has not been |
| 3738 | * committed yet. Thus we can assume that preemption |
| 3739 | * is still disabled. |
| 3740 | */ |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3741 | RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing)); |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3742 | |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 3743 | rb_decrement_entry(cpu_buffer, event); |
Steven Rostedt | 0f2541d | 2009-08-05 12:02:48 -0400 | [diff] [blame] | 3744 | if (rb_try_to_discard(cpu_buffer, event)) |
Steven Rostedt | edd813bf | 2009-06-02 23:00:53 -0400 | [diff] [blame] | 3745 | goto out; |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3746 | |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3747 | out: |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3748 | rb_end_commit(cpu_buffer); |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3749 | |
Steven Rostedt (Red Hat) | 58a09ec | 2015-05-27 10:27:47 -0400 | [diff] [blame] | 3750 | trace_recursive_unlock(cpu_buffer); |
Frederic Weisbecker | f3b9aae | 2009-04-19 23:39:33 +0200 | [diff] [blame] | 3751 | |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3752 | preempt_enable_notrace(); |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3753 | |
| 3754 | } |
| 3755 | EXPORT_SYMBOL_GPL(ring_buffer_discard_commit); |
| 3756 | |
| 3757 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3758 | * ring_buffer_write - write data to the buffer without reserving |
| 3759 | * @buffer: The ring buffer to write to. |
| 3760 | * @length: The length of the data being written (excluding the event header) |
| 3761 | * @data: The data to write to the buffer. |
| 3762 | * |
| 3763 | * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as |
| 3764 | * one function. If you already have the data to write to the buffer, it |
| 3765 | * may be easier to simply call this function. |
| 3766 | * |
| 3767 | * Note, like ring_buffer_lock_reserve, the length is the length of the data |
| 3768 | * and not the length of the event which would hold the header. |
| 3769 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3770 | int ring_buffer_write(struct trace_buffer *buffer, |
David Sharp | 01e3e71 | 2012-06-07 16:46:24 -0700 | [diff] [blame] | 3771 | unsigned long length, |
| 3772 | void *data) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3773 | { |
| 3774 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3775 | struct ring_buffer_event *event; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3776 | void *body; |
| 3777 | int ret = -EBUSY; |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3778 | int cpu; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3779 | |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3780 | preempt_disable_notrace(); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3781 | |
Lai Jiangshan | 52fbe9c | 2010-03-08 14:50:43 +0800 | [diff] [blame] | 3782 | if (atomic_read(&buffer->record_disabled)) |
| 3783 | goto out; |
| 3784 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3785 | cpu = raw_smp_processor_id(); |
| 3786 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3787 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3788 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3789 | |
| 3790 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3791 | |
| 3792 | if (atomic_read(&cpu_buffer->record_disabled)) |
| 3793 | goto out; |
| 3794 | |
Steven Rostedt | be957c4 | 2009-05-11 14:42:53 -0400 | [diff] [blame] | 3795 | if (length > BUF_MAX_DATA_SIZE) |
| 3796 | goto out; |
| 3797 | |
Steven Rostedt (Red Hat) | 985e871 | 2015-05-27 10:48:56 -0400 | [diff] [blame] | 3798 | if (unlikely(trace_recursive_lock(cpu_buffer))) |
| 3799 | goto out; |
| 3800 | |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 3801 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3802 | if (!event) |
Steven Rostedt (Red Hat) | 985e871 | 2015-05-27 10:48:56 -0400 | [diff] [blame] | 3803 | goto out_unlock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3804 | |
| 3805 | body = rb_event_data(event); |
| 3806 | |
| 3807 | memcpy(body, data, length); |
| 3808 | |
| 3809 | rb_commit(cpu_buffer, event); |
| 3810 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 3811 | rb_wakeups(buffer, cpu_buffer); |
| 3812 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3813 | ret = 0; |
Steven Rostedt (Red Hat) | 985e871 | 2015-05-27 10:48:56 -0400 | [diff] [blame] | 3814 | |
| 3815 | out_unlock: |
| 3816 | trace_recursive_unlock(cpu_buffer); |
| 3817 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3818 | out: |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3819 | preempt_enable_notrace(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3820 | |
| 3821 | return ret; |
| 3822 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3823 | EXPORT_SYMBOL_GPL(ring_buffer_write); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3824 | |
Yaowei Bai | da58834 | 2015-09-29 22:43:33 +0800 | [diff] [blame] | 3825 | static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3826 | { |
| 3827 | struct buffer_page *reader = cpu_buffer->reader_page; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3828 | struct buffer_page *head = rb_set_head_page(cpu_buffer); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3829 | struct buffer_page *commit = cpu_buffer->commit_page; |
| 3830 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3831 | /* In case of error, head will be NULL */ |
| 3832 | if (unlikely(!head)) |
Yaowei Bai | da58834 | 2015-09-29 22:43:33 +0800 | [diff] [blame] | 3833 | return true; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3834 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3835 | return reader->read == rb_page_commit(reader) && |
| 3836 | (commit == reader || |
| 3837 | (commit == head && |
| 3838 | head->read == rb_page_commit(commit))); |
| 3839 | } |
| 3840 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3841 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3842 | * ring_buffer_record_disable - stop all writes into the buffer |
| 3843 | * @buffer: The ring buffer to stop writes to. |
| 3844 | * |
| 3845 | * This prevents all writes to the buffer. Any attempt to write |
| 3846 | * to the buffer after this will fail and return NULL. |
| 3847 | * |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 3848 | * The caller should call synchronize_rcu() after this. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3849 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3850 | void ring_buffer_record_disable(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3851 | { |
| 3852 | atomic_inc(&buffer->record_disabled); |
| 3853 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3854 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3855 | |
| 3856 | /** |
| 3857 | * ring_buffer_record_enable - enable writes to the buffer |
| 3858 | * @buffer: The ring buffer to enable writes |
| 3859 | * |
| 3860 | * Note, multiple disables will need the same number of enables |
Adam Buchbinder | c41b20e | 2009-12-11 16:35:39 -0500 | [diff] [blame] | 3861 | * to truly enable the writing (much like preempt_disable). |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3862 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3863 | void ring_buffer_record_enable(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3864 | { |
| 3865 | atomic_dec(&buffer->record_disabled); |
| 3866 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3867 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3868 | |
| 3869 | /** |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 3870 | * ring_buffer_record_off - stop all writes into the buffer |
| 3871 | * @buffer: The ring buffer to stop writes to. |
| 3872 | * |
| 3873 | * This prevents all writes to the buffer. Any attempt to write |
| 3874 | * to the buffer after this will fail and return NULL. |
| 3875 | * |
| 3876 | * This is different than ring_buffer_record_disable() as |
Wang Tianhong | 87abb3b | 2012-08-02 14:02:00 +0800 | [diff] [blame] | 3877 | * it works like an on/off switch, where as the disable() version |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 3878 | * must be paired with a enable(). |
| 3879 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3880 | void ring_buffer_record_off(struct trace_buffer *buffer) |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 3881 | { |
| 3882 | unsigned int rd; |
| 3883 | unsigned int new_rd; |
| 3884 | |
| 3885 | do { |
| 3886 | rd = atomic_read(&buffer->record_disabled); |
| 3887 | new_rd = rd | RB_BUFFER_OFF; |
| 3888 | } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd); |
| 3889 | } |
| 3890 | EXPORT_SYMBOL_GPL(ring_buffer_record_off); |
| 3891 | |
| 3892 | /** |
| 3893 | * ring_buffer_record_on - restart writes into the buffer |
| 3894 | * @buffer: The ring buffer to start writes to. |
| 3895 | * |
| 3896 | * This enables all writes to the buffer that was disabled by |
| 3897 | * ring_buffer_record_off(). |
| 3898 | * |
| 3899 | * This is different than ring_buffer_record_enable() as |
Wang Tianhong | 87abb3b | 2012-08-02 14:02:00 +0800 | [diff] [blame] | 3900 | * it works like an on/off switch, where as the enable() version |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 3901 | * must be paired with a disable(). |
| 3902 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3903 | void ring_buffer_record_on(struct trace_buffer *buffer) |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 3904 | { |
| 3905 | unsigned int rd; |
| 3906 | unsigned int new_rd; |
| 3907 | |
| 3908 | do { |
| 3909 | rd = atomic_read(&buffer->record_disabled); |
| 3910 | new_rd = rd & ~RB_BUFFER_OFF; |
| 3911 | } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd); |
| 3912 | } |
| 3913 | EXPORT_SYMBOL_GPL(ring_buffer_record_on); |
| 3914 | |
| 3915 | /** |
| 3916 | * ring_buffer_record_is_on - return true if the ring buffer can write |
| 3917 | * @buffer: The ring buffer to see if write is enabled |
| 3918 | * |
| 3919 | * Returns true if the ring buffer is in a state that it accepts writes. |
| 3920 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3921 | bool ring_buffer_record_is_on(struct trace_buffer *buffer) |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 3922 | { |
| 3923 | return !atomic_read(&buffer->record_disabled); |
| 3924 | } |
| 3925 | |
| 3926 | /** |
Masami Hiramatsu | 73c8d89 | 2018-07-14 01:28:15 +0900 | [diff] [blame] | 3927 | * ring_buffer_record_is_set_on - return true if the ring buffer is set writable |
| 3928 | * @buffer: The ring buffer to see if write is set enabled |
| 3929 | * |
| 3930 | * Returns true if the ring buffer is set writable by ring_buffer_record_on(). |
| 3931 | * Note that this does NOT mean it is in a writable state. |
| 3932 | * |
| 3933 | * It may return true when the ring buffer has been disabled by |
| 3934 | * ring_buffer_record_disable(), as that is a temporary disabling of |
| 3935 | * the ring buffer. |
| 3936 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3937 | bool ring_buffer_record_is_set_on(struct trace_buffer *buffer) |
Masami Hiramatsu | 73c8d89 | 2018-07-14 01:28:15 +0900 | [diff] [blame] | 3938 | { |
| 3939 | return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF); |
| 3940 | } |
| 3941 | |
| 3942 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3943 | * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer |
| 3944 | * @buffer: The ring buffer to stop writes to. |
| 3945 | * @cpu: The CPU buffer to stop |
| 3946 | * |
| 3947 | * This prevents all writes to the buffer. Any attempt to write |
| 3948 | * to the buffer after this will fail and return NULL. |
| 3949 | * |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 3950 | * The caller should call synchronize_rcu() after this. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3951 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3952 | void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3953 | { |
| 3954 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3955 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3956 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3957 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3958 | |
| 3959 | cpu_buffer = buffer->buffers[cpu]; |
| 3960 | atomic_inc(&cpu_buffer->record_disabled); |
| 3961 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3962 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3963 | |
| 3964 | /** |
| 3965 | * ring_buffer_record_enable_cpu - enable writes to the buffer |
| 3966 | * @buffer: The ring buffer to enable writes |
| 3967 | * @cpu: The CPU to enable. |
| 3968 | * |
| 3969 | * Note, multiple disables will need the same number of enables |
Adam Buchbinder | c41b20e | 2009-12-11 16:35:39 -0500 | [diff] [blame] | 3970 | * to truly enable the writing (much like preempt_disable). |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3971 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3972 | void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3973 | { |
| 3974 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3975 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3976 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3977 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3978 | |
| 3979 | cpu_buffer = buffer->buffers[cpu]; |
| 3980 | atomic_dec(&cpu_buffer->record_disabled); |
| 3981 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3982 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3983 | |
Steven Rostedt | f6195aa | 2010-09-01 12:23:12 -0400 | [diff] [blame] | 3984 | /* |
| 3985 | * The total entries in the ring buffer is the running counter |
| 3986 | * of entries entered into the ring buffer, minus the sum of |
| 3987 | * the entries read from the ring buffer and the number of |
| 3988 | * entries that were overwritten. |
| 3989 | */ |
| 3990 | static inline unsigned long |
| 3991 | rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer) |
| 3992 | { |
| 3993 | return local_read(&cpu_buffer->entries) - |
| 3994 | (local_read(&cpu_buffer->overrun) + cpu_buffer->read); |
| 3995 | } |
| 3996 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3997 | /** |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 3998 | * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer |
| 3999 | * @buffer: The ring buffer |
| 4000 | * @cpu: The per CPU buffer to read from. |
| 4001 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4002 | u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu) |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 4003 | { |
| 4004 | unsigned long flags; |
| 4005 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4006 | struct buffer_page *bpage; |
Linus Torvalds | da830e5 | 2012-12-11 18:18:58 -0800 | [diff] [blame] | 4007 | u64 ret = 0; |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 4008 | |
| 4009 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 4010 | return 0; |
| 4011 | |
| 4012 | cpu_buffer = buffer->buffers[cpu]; |
Linus Torvalds | 7115e3f | 2011-10-26 17:03:38 +0200 | [diff] [blame] | 4013 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 4014 | /* |
| 4015 | * if the tail is on reader_page, oldest time stamp is on the reader |
| 4016 | * page |
| 4017 | */ |
| 4018 | if (cpu_buffer->tail_page == cpu_buffer->reader_page) |
| 4019 | bpage = cpu_buffer->reader_page; |
| 4020 | else |
| 4021 | bpage = rb_set_head_page(cpu_buffer); |
Steven Rostedt | 54f7be5 | 2012-11-29 22:27:22 -0500 | [diff] [blame] | 4022 | if (bpage) |
| 4023 | ret = bpage->page->time_stamp; |
Linus Torvalds | 7115e3f | 2011-10-26 17:03:38 +0200 | [diff] [blame] | 4024 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 4025 | |
| 4026 | return ret; |
| 4027 | } |
| 4028 | EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts); |
| 4029 | |
| 4030 | /** |
| 4031 | * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer |
| 4032 | * @buffer: The ring buffer |
| 4033 | * @cpu: The per CPU buffer to read from. |
| 4034 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4035 | unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu) |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 4036 | { |
| 4037 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4038 | unsigned long ret; |
| 4039 | |
| 4040 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 4041 | return 0; |
| 4042 | |
| 4043 | cpu_buffer = buffer->buffers[cpu]; |
| 4044 | ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes; |
| 4045 | |
| 4046 | return ret; |
| 4047 | } |
| 4048 | EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu); |
| 4049 | |
| 4050 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4051 | * ring_buffer_entries_cpu - get the number of entries in a cpu buffer |
| 4052 | * @buffer: The ring buffer |
| 4053 | * @cpu: The per CPU buffer to get the entries from. |
| 4054 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4055 | unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4056 | { |
| 4057 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4058 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 4059 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4060 | return 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4061 | |
| 4062 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4063 | |
Steven Rostedt | f6195aa | 2010-09-01 12:23:12 -0400 | [diff] [blame] | 4064 | return rb_num_of_entries(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4065 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4066 | EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4067 | |
| 4068 | /** |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 4069 | * ring_buffer_overrun_cpu - get the number of overruns caused by the ring |
| 4070 | * buffer wrapping around (only if RB_FL_OVERWRITE is on). |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4071 | * @buffer: The ring buffer |
| 4072 | * @cpu: The per CPU buffer to get the number of overruns from |
| 4073 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4074 | unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4075 | { |
| 4076 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4077 | unsigned long ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4078 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 4079 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4080 | return 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4081 | |
| 4082 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4083 | ret = local_read(&cpu_buffer->overrun); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4084 | |
| 4085 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4086 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4087 | EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4088 | |
| 4089 | /** |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 4090 | * ring_buffer_commit_overrun_cpu - get the number of overruns caused by |
| 4091 | * commits failing due to the buffer wrapping around while there are uncommitted |
| 4092 | * events, such as during an interrupt storm. |
Steven Rostedt | f0d2c68 | 2009-04-29 13:43:37 -0400 | [diff] [blame] | 4093 | * @buffer: The ring buffer |
| 4094 | * @cpu: The per CPU buffer to get the number of overruns from |
| 4095 | */ |
| 4096 | unsigned long |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4097 | ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | f0d2c68 | 2009-04-29 13:43:37 -0400 | [diff] [blame] | 4098 | { |
| 4099 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4100 | unsigned long ret; |
| 4101 | |
| 4102 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 4103 | return 0; |
| 4104 | |
| 4105 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4106 | ret = local_read(&cpu_buffer->commit_overrun); |
Steven Rostedt | f0d2c68 | 2009-04-29 13:43:37 -0400 | [diff] [blame] | 4107 | |
| 4108 | return ret; |
| 4109 | } |
| 4110 | EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu); |
| 4111 | |
| 4112 | /** |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 4113 | * ring_buffer_dropped_events_cpu - get the number of dropped events caused by |
| 4114 | * the ring buffer filling up (only if RB_FL_OVERWRITE is off). |
| 4115 | * @buffer: The ring buffer |
| 4116 | * @cpu: The per CPU buffer to get the number of overruns from |
| 4117 | */ |
| 4118 | unsigned long |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4119 | ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu) |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 4120 | { |
| 4121 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4122 | unsigned long ret; |
| 4123 | |
| 4124 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 4125 | return 0; |
| 4126 | |
| 4127 | cpu_buffer = buffer->buffers[cpu]; |
| 4128 | ret = local_read(&cpu_buffer->dropped_events); |
| 4129 | |
| 4130 | return ret; |
| 4131 | } |
| 4132 | EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu); |
| 4133 | |
| 4134 | /** |
Steven Rostedt (Red Hat) | ad96470 | 2013-01-29 17:45:49 -0500 | [diff] [blame] | 4135 | * ring_buffer_read_events_cpu - get the number of events successfully read |
| 4136 | * @buffer: The ring buffer |
| 4137 | * @cpu: The per CPU buffer to get the number of events read |
| 4138 | */ |
| 4139 | unsigned long |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4140 | ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu) |
Steven Rostedt (Red Hat) | ad96470 | 2013-01-29 17:45:49 -0500 | [diff] [blame] | 4141 | { |
| 4142 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4143 | |
| 4144 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 4145 | return 0; |
| 4146 | |
| 4147 | cpu_buffer = buffer->buffers[cpu]; |
| 4148 | return cpu_buffer->read; |
| 4149 | } |
| 4150 | EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu); |
| 4151 | |
| 4152 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4153 | * ring_buffer_entries - get the number of entries in a buffer |
| 4154 | * @buffer: The ring buffer |
| 4155 | * |
| 4156 | * Returns the total number of entries in the ring buffer |
| 4157 | * (all CPU entries) |
| 4158 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4159 | unsigned long ring_buffer_entries(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4160 | { |
| 4161 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4162 | unsigned long entries = 0; |
| 4163 | int cpu; |
| 4164 | |
| 4165 | /* if you care about this being correct, lock the buffer */ |
| 4166 | for_each_buffer_cpu(buffer, cpu) { |
| 4167 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | f6195aa | 2010-09-01 12:23:12 -0400 | [diff] [blame] | 4168 | entries += rb_num_of_entries(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4169 | } |
| 4170 | |
| 4171 | return entries; |
| 4172 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4173 | EXPORT_SYMBOL_GPL(ring_buffer_entries); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4174 | |
| 4175 | /** |
Jiri Olsa | 67b394f | 2009-10-23 19:36:18 -0400 | [diff] [blame] | 4176 | * ring_buffer_overruns - get the number of overruns in buffer |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4177 | * @buffer: The ring buffer |
| 4178 | * |
| 4179 | * Returns the total number of overruns in the ring buffer |
| 4180 | * (all CPU entries) |
| 4181 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4182 | unsigned long ring_buffer_overruns(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4183 | { |
| 4184 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4185 | unsigned long overruns = 0; |
| 4186 | int cpu; |
| 4187 | |
| 4188 | /* if you care about this being correct, lock the buffer */ |
| 4189 | for_each_buffer_cpu(buffer, cpu) { |
| 4190 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4191 | overruns += local_read(&cpu_buffer->overrun); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | return overruns; |
| 4195 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4196 | EXPORT_SYMBOL_GPL(ring_buffer_overruns); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4197 | |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 4198 | static void rb_iter_reset(struct ring_buffer_iter *iter) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4199 | { |
| 4200 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 4201 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4202 | /* Iterator usage is expected to have record disabled */ |
Steven Rostedt (Red Hat) | 651e22f | 2014-08-06 14:11:33 -0400 | [diff] [blame] | 4203 | iter->head_page = cpu_buffer->reader_page; |
| 4204 | iter->head = cpu_buffer->reader_page->read; |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4205 | iter->next_event = iter->head; |
Steven Rostedt (Red Hat) | 651e22f | 2014-08-06 14:11:33 -0400 | [diff] [blame] | 4206 | |
| 4207 | iter->cache_reader_page = iter->head_page; |
Steven Rostedt (Red Hat) | 24607f1 | 2014-10-02 16:51:18 -0400 | [diff] [blame] | 4208 | iter->cache_read = cpu_buffer->read; |
Steven Rostedt (Red Hat) | 651e22f | 2014-08-06 14:11:33 -0400 | [diff] [blame] | 4209 | |
Steven Rostedt (VMware) | 28e3fc5 | 2020-03-17 17:32:26 -0400 | [diff] [blame] | 4210 | if (iter->head) { |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4211 | iter->read_stamp = cpu_buffer->read_stamp; |
Steven Rostedt (VMware) | 28e3fc5 | 2020-03-17 17:32:26 -0400 | [diff] [blame] | 4212 | iter->page_stamp = cpu_buffer->reader_page->page->time_stamp; |
| 4213 | } else { |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 4214 | iter->read_stamp = iter->head_page->page->time_stamp; |
Steven Rostedt (VMware) | 28e3fc5 | 2020-03-17 17:32:26 -0400 | [diff] [blame] | 4215 | iter->page_stamp = iter->read_stamp; |
| 4216 | } |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 4217 | } |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4218 | |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 4219 | /** |
| 4220 | * ring_buffer_iter_reset - reset an iterator |
| 4221 | * @iter: The iterator to reset |
| 4222 | * |
| 4223 | * Resets the iterator, so that it will start from the beginning |
| 4224 | * again. |
| 4225 | */ |
| 4226 | void ring_buffer_iter_reset(struct ring_buffer_iter *iter) |
| 4227 | { |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4228 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 4229 | unsigned long flags; |
| 4230 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4231 | if (!iter) |
| 4232 | return; |
| 4233 | |
| 4234 | cpu_buffer = iter->cpu_buffer; |
| 4235 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4236 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 4237 | rb_iter_reset(iter); |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4238 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4239 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4240 | EXPORT_SYMBOL_GPL(ring_buffer_iter_reset); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4241 | |
| 4242 | /** |
| 4243 | * ring_buffer_iter_empty - check if an iterator has no more to read |
| 4244 | * @iter: The iterator to check |
| 4245 | */ |
| 4246 | int ring_buffer_iter_empty(struct ring_buffer_iter *iter) |
| 4247 | { |
| 4248 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt (VMware) | 78f7a45 | 2017-04-19 14:29:46 -0400 | [diff] [blame] | 4249 | struct buffer_page *reader; |
| 4250 | struct buffer_page *head_page; |
| 4251 | struct buffer_page *commit_page; |
Steven Rostedt (VMware) | ead6ecf | 2020-03-17 17:32:24 -0400 | [diff] [blame] | 4252 | struct buffer_page *curr_commit_page; |
Steven Rostedt (VMware) | 78f7a45 | 2017-04-19 14:29:46 -0400 | [diff] [blame] | 4253 | unsigned commit; |
Steven Rostedt (VMware) | ead6ecf | 2020-03-17 17:32:24 -0400 | [diff] [blame] | 4254 | u64 curr_commit_ts; |
| 4255 | u64 commit_ts; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4256 | |
| 4257 | cpu_buffer = iter->cpu_buffer; |
Steven Rostedt (VMware) | 78f7a45 | 2017-04-19 14:29:46 -0400 | [diff] [blame] | 4258 | reader = cpu_buffer->reader_page; |
| 4259 | head_page = cpu_buffer->head_page; |
| 4260 | commit_page = cpu_buffer->commit_page; |
Steven Rostedt (VMware) | ead6ecf | 2020-03-17 17:32:24 -0400 | [diff] [blame] | 4261 | commit_ts = commit_page->page->time_stamp; |
Steven Rostedt (VMware) | 78f7a45 | 2017-04-19 14:29:46 -0400 | [diff] [blame] | 4262 | |
Steven Rostedt (VMware) | ead6ecf | 2020-03-17 17:32:24 -0400 | [diff] [blame] | 4263 | /* |
| 4264 | * When the writer goes across pages, it issues a cmpxchg which |
| 4265 | * is a mb(), which will synchronize with the rmb here. |
| 4266 | * (see rb_tail_page_update()) |
| 4267 | */ |
| 4268 | smp_rmb(); |
| 4269 | commit = rb_page_commit(commit_page); |
| 4270 | /* We want to make sure that the commit page doesn't change */ |
| 4271 | smp_rmb(); |
| 4272 | |
| 4273 | /* Make sure commit page didn't change */ |
| 4274 | curr_commit_page = READ_ONCE(cpu_buffer->commit_page); |
| 4275 | curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp); |
| 4276 | |
| 4277 | /* If the commit page changed, then there's more data */ |
| 4278 | if (curr_commit_page != commit_page || |
| 4279 | curr_commit_ts != commit_ts) |
| 4280 | return 0; |
| 4281 | |
| 4282 | /* Still racy, as it may return a false positive, but that's OK */ |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4283 | return ((iter->head_page == commit_page && iter->head >= commit) || |
Steven Rostedt (VMware) | 78f7a45 | 2017-04-19 14:29:46 -0400 | [diff] [blame] | 4284 | (iter->head_page == reader && commit_page == head_page && |
| 4285 | head_page->read == commit && |
| 4286 | iter->head == rb_page_commit(cpu_buffer->reader_page))); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4287 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4288 | EXPORT_SYMBOL_GPL(ring_buffer_iter_empty); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4289 | |
| 4290 | static void |
| 4291 | rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer, |
| 4292 | struct ring_buffer_event *event) |
| 4293 | { |
| 4294 | u64 delta; |
| 4295 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 4296 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4297 | case RINGBUF_TYPE_PADDING: |
| 4298 | return; |
| 4299 | |
| 4300 | case RINGBUF_TYPE_TIME_EXTEND: |
Steven Rostedt (VMware) | e20044f | 2021-03-16 12:41:01 -0400 | [diff] [blame] | 4301 | delta = rb_event_time_stamp(event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4302 | cpu_buffer->read_stamp += delta; |
| 4303 | return; |
| 4304 | |
| 4305 | case RINGBUF_TYPE_TIME_STAMP: |
Steven Rostedt (VMware) | e20044f | 2021-03-16 12:41:01 -0400 | [diff] [blame] | 4306 | delta = rb_event_time_stamp(event); |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4307 | cpu_buffer->read_stamp = delta; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4308 | return; |
| 4309 | |
| 4310 | case RINGBUF_TYPE_DATA: |
| 4311 | cpu_buffer->read_stamp += event->time_delta; |
| 4312 | return; |
| 4313 | |
| 4314 | default: |
Steven Rostedt (VMware) | da4d401 | 2020-05-13 15:36:22 -0400 | [diff] [blame] | 4315 | RB_WARN_ON(cpu_buffer, 1); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4316 | } |
| 4317 | return; |
| 4318 | } |
| 4319 | |
| 4320 | static void |
| 4321 | rb_update_iter_read_stamp(struct ring_buffer_iter *iter, |
| 4322 | struct ring_buffer_event *event) |
| 4323 | { |
| 4324 | u64 delta; |
| 4325 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 4326 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4327 | case RINGBUF_TYPE_PADDING: |
| 4328 | return; |
| 4329 | |
| 4330 | case RINGBUF_TYPE_TIME_EXTEND: |
Steven Rostedt (VMware) | e20044f | 2021-03-16 12:41:01 -0400 | [diff] [blame] | 4331 | delta = rb_event_time_stamp(event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4332 | iter->read_stamp += delta; |
| 4333 | return; |
| 4334 | |
| 4335 | case RINGBUF_TYPE_TIME_STAMP: |
Steven Rostedt (VMware) | e20044f | 2021-03-16 12:41:01 -0400 | [diff] [blame] | 4336 | delta = rb_event_time_stamp(event); |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4337 | iter->read_stamp = delta; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4338 | return; |
| 4339 | |
| 4340 | case RINGBUF_TYPE_DATA: |
| 4341 | iter->read_stamp += event->time_delta; |
| 4342 | return; |
| 4343 | |
| 4344 | default: |
Steven Rostedt (VMware) | da4d401 | 2020-05-13 15:36:22 -0400 | [diff] [blame] | 4345 | RB_WARN_ON(iter->cpu_buffer, 1); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4346 | } |
| 4347 | return; |
| 4348 | } |
| 4349 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4350 | static struct buffer_page * |
| 4351 | rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4352 | { |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4353 | struct buffer_page *reader = NULL; |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4354 | unsigned long overwrite; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4355 | unsigned long flags; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4356 | int nr_loops = 0; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4357 | int ret; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4358 | |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 4359 | local_irq_save(flags); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 4360 | arch_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4361 | |
| 4362 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4363 | /* |
| 4364 | * This should normally only loop twice. But because the |
| 4365 | * start of the reader inserts an empty page, it causes |
| 4366 | * a case where we will loop three times. There should be no |
| 4367 | * reason to loop four times (that I know of). |
| 4368 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 4369 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) { |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4370 | reader = NULL; |
| 4371 | goto out; |
| 4372 | } |
| 4373 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4374 | reader = cpu_buffer->reader_page; |
| 4375 | |
| 4376 | /* If there's more to read, return this page */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 4377 | if (cpu_buffer->reader_page->read < rb_page_size(reader)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4378 | goto out; |
| 4379 | |
| 4380 | /* Never should we have an index greater than the size */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 4381 | if (RB_WARN_ON(cpu_buffer, |
| 4382 | cpu_buffer->reader_page->read > rb_page_size(reader))) |
| 4383 | goto out; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4384 | |
| 4385 | /* check if we caught up to the tail */ |
| 4386 | reader = NULL; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 4387 | if (cpu_buffer->commit_page == cpu_buffer->reader_page) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4388 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4389 | |
Steven Rostedt | a5fb833 | 2012-06-28 13:35:04 -0400 | [diff] [blame] | 4390 | /* Don't bother swapping if the ring buffer is empty */ |
| 4391 | if (rb_num_of_entries(cpu_buffer) == 0) |
| 4392 | goto out; |
| 4393 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4394 | /* |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4395 | * Reset the reader page to size zero. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4396 | */ |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4397 | local_set(&cpu_buffer->reader_page->write, 0); |
| 4398 | local_set(&cpu_buffer->reader_page->entries, 0); |
| 4399 | local_set(&cpu_buffer->reader_page->page->commit, 0); |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 4400 | cpu_buffer->reader_page->real_end = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4401 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4402 | spin: |
| 4403 | /* |
| 4404 | * Splice the empty reader page into the list around the head. |
| 4405 | */ |
| 4406 | reader = rb_set_head_page(cpu_buffer); |
Steven Rostedt | 54f7be5 | 2012-11-29 22:27:22 -0500 | [diff] [blame] | 4407 | if (!reader) |
| 4408 | goto out; |
Steven Rostedt | 0e1ff5d | 2010-01-06 20:40:44 -0500 | [diff] [blame] | 4409 | cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4410 | cpu_buffer->reader_page->list.prev = reader->list.prev; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 4411 | |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 4412 | /* |
| 4413 | * cpu_buffer->pages just needs to point to the buffer, it |
| 4414 | * has no specific buffer page to point to. Lets move it out |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4415 | * of our way so we don't accidentally swap it. |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 4416 | */ |
| 4417 | cpu_buffer->pages = reader->list.prev; |
| 4418 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4419 | /* The reader page will be pointing to the new head */ |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 4420 | rb_set_list_to_head(&cpu_buffer->reader_page->list); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4421 | |
| 4422 | /* |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4423 | * We want to make sure we read the overruns after we set up our |
| 4424 | * pointers to the next object. The writer side does a |
| 4425 | * cmpxchg to cross pages which acts as the mb on the writer |
| 4426 | * side. Note, the reader will constantly fail the swap |
| 4427 | * while the writer is updating the pointers, so this |
| 4428 | * guarantees that the overwrite recorded here is the one we |
| 4429 | * want to compare with the last_overrun. |
| 4430 | */ |
| 4431 | smp_mb(); |
| 4432 | overwrite = local_read(&(cpu_buffer->overrun)); |
| 4433 | |
| 4434 | /* |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4435 | * Here's the tricky part. |
| 4436 | * |
| 4437 | * We need to move the pointer past the header page. |
| 4438 | * But we can only do that if a writer is not currently |
| 4439 | * moving it. The page before the header page has the |
| 4440 | * flag bit '1' set if it is pointing to the page we want. |
| 4441 | * but if the writer is in the process of moving it |
| 4442 | * than it will be '2' or already moved '0'. |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4443 | */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4444 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4445 | ret = rb_head_page_replace(reader, cpu_buffer->reader_page); |
| 4446 | |
| 4447 | /* |
| 4448 | * If we did not convert it, then we must try again. |
| 4449 | */ |
| 4450 | if (!ret) |
| 4451 | goto spin; |
| 4452 | |
| 4453 | /* |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 4454 | * Yay! We succeeded in replacing the page. |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4455 | * |
| 4456 | * Now make the new head point back to the reader page. |
| 4457 | */ |
David Sharp | 5ded3dc6 | 2010-01-06 17:12:07 -0800 | [diff] [blame] | 4458 | rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list; |
Qiujun Huang | 6689bed | 2020-12-25 22:03:56 +0800 | [diff] [blame] | 4459 | rb_inc_page(&cpu_buffer->head_page); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4460 | |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 4461 | local_inc(&cpu_buffer->pages_read); |
| 4462 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4463 | /* Finally update the reader page to the new head */ |
| 4464 | cpu_buffer->reader_page = reader; |
Steven Rostedt (Red Hat) | b81f472 | 2015-11-23 10:35:36 -0500 | [diff] [blame] | 4465 | cpu_buffer->reader_page->read = 0; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4466 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4467 | if (overwrite != cpu_buffer->last_overrun) { |
| 4468 | cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun; |
| 4469 | cpu_buffer->last_overrun = overwrite; |
| 4470 | } |
| 4471 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4472 | goto again; |
| 4473 | |
| 4474 | out: |
Steven Rostedt (Red Hat) | b81f472 | 2015-11-23 10:35:36 -0500 | [diff] [blame] | 4475 | /* Update the read_stamp on the first event */ |
| 4476 | if (reader && reader->read == 0) |
| 4477 | cpu_buffer->read_stamp = reader->page->time_stamp; |
| 4478 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 4479 | arch_spin_unlock(&cpu_buffer->lock); |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 4480 | local_irq_restore(flags); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4481 | |
| 4482 | return reader; |
| 4483 | } |
| 4484 | |
| 4485 | static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer) |
| 4486 | { |
| 4487 | struct ring_buffer_event *event; |
| 4488 | struct buffer_page *reader; |
| 4489 | unsigned length; |
| 4490 | |
| 4491 | reader = rb_get_reader_page(cpu_buffer); |
| 4492 | |
| 4493 | /* This function should not be called when buffer is empty */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 4494 | if (RB_WARN_ON(cpu_buffer, !reader)) |
| 4495 | return; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4496 | |
| 4497 | event = rb_reader_event(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4498 | |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 4499 | if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
Steven Rostedt | e4906ef | 2009-04-30 20:49:44 -0400 | [diff] [blame] | 4500 | cpu_buffer->read++; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4501 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4502 | rb_update_read_stamp(cpu_buffer, event); |
| 4503 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4504 | length = rb_event_length(event); |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 4505 | cpu_buffer->reader_page->read += length; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4506 | } |
| 4507 | |
| 4508 | static void rb_advance_iter(struct ring_buffer_iter *iter) |
| 4509 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4510 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4511 | |
| 4512 | cpu_buffer = iter->cpu_buffer; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4513 | |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4514 | /* If head == next_event then we need to jump to the next event */ |
| 4515 | if (iter->head == iter->next_event) { |
| 4516 | /* If the event gets overwritten again, there's nothing to do */ |
| 4517 | if (rb_iter_head_event(iter) == NULL) |
| 4518 | return; |
| 4519 | } |
| 4520 | |
| 4521 | iter->head = iter->next_event; |
| 4522 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4523 | /* |
| 4524 | * Check if we are at the end of the buffer. |
| 4525 | */ |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4526 | if (iter->next_event >= rb_page_size(iter->head_page)) { |
Steven Rostedt | ea05b57 | 2009-06-03 09:30:10 -0400 | [diff] [blame] | 4527 | /* discarded commits can make the page empty */ |
| 4528 | if (iter->head_page == cpu_buffer->commit_page) |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 4529 | return; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4530 | rb_inc_iter(iter); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4531 | return; |
| 4532 | } |
| 4533 | |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4534 | rb_update_iter_read_stamp(iter, iter->event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4535 | } |
| 4536 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4537 | static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer) |
| 4538 | { |
| 4539 | return cpu_buffer->lost_events; |
| 4540 | } |
| 4541 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4542 | static struct ring_buffer_event * |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4543 | rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts, |
| 4544 | unsigned long *lost_events) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4545 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4546 | struct ring_buffer_event *event; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4547 | struct buffer_page *reader; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4548 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4549 | |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4550 | if (ts) |
| 4551 | *ts = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4552 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4553 | /* |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 4554 | * We repeat when a time extend is encountered. |
| 4555 | * Since the time extend is always attached to a data event, |
| 4556 | * we should never loop more than once. |
| 4557 | * (We never hit the following condition more than twice). |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4558 | */ |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 4559 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2)) |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4560 | return NULL; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4561 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4562 | reader = rb_get_reader_page(cpu_buffer); |
| 4563 | if (!reader) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4564 | return NULL; |
| 4565 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4566 | event = rb_reader_event(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4567 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 4568 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4569 | case RINGBUF_TYPE_PADDING: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4570 | if (rb_null_event(event)) |
| 4571 | RB_WARN_ON(cpu_buffer, 1); |
| 4572 | /* |
| 4573 | * Because the writer could be discarding every |
| 4574 | * event it creates (which would probably be bad) |
| 4575 | * if we were to go back to "again" then we may never |
| 4576 | * catch up, and will trigger the warn on, or lock |
| 4577 | * the box. Return the padding, and we will release |
| 4578 | * the current locks, and try again. |
| 4579 | */ |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4580 | return event; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4581 | |
| 4582 | case RINGBUF_TYPE_TIME_EXTEND: |
| 4583 | /* Internal data, OK to advance */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4584 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4585 | goto again; |
| 4586 | |
| 4587 | case RINGBUF_TYPE_TIME_STAMP: |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4588 | if (ts) { |
Steven Rostedt (VMware) | e20044f | 2021-03-16 12:41:01 -0400 | [diff] [blame] | 4589 | *ts = rb_event_time_stamp(event); |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4590 | ring_buffer_normalize_time_stamp(cpu_buffer->buffer, |
| 4591 | cpu_buffer->cpu, ts); |
| 4592 | } |
| 4593 | /* Internal data, OK to advance */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4594 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4595 | goto again; |
| 4596 | |
| 4597 | case RINGBUF_TYPE_DATA: |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4598 | if (ts && !(*ts)) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4599 | *ts = cpu_buffer->read_stamp + event->time_delta; |
Robert Richter | d8eeb2d | 2009-07-31 14:58:04 +0200 | [diff] [blame] | 4600 | ring_buffer_normalize_time_stamp(cpu_buffer->buffer, |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 4601 | cpu_buffer->cpu, ts); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4602 | } |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4603 | if (lost_events) |
| 4604 | *lost_events = rb_lost_events(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4605 | return event; |
| 4606 | |
| 4607 | default: |
Steven Rostedt (VMware) | da4d401 | 2020-05-13 15:36:22 -0400 | [diff] [blame] | 4608 | RB_WARN_ON(cpu_buffer, 1); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4609 | } |
| 4610 | |
| 4611 | return NULL; |
| 4612 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4613 | EXPORT_SYMBOL_GPL(ring_buffer_peek); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4614 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4615 | static struct ring_buffer_event * |
| 4616 | rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4617 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4618 | struct trace_buffer *buffer; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4619 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4620 | struct ring_buffer_event *event; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4621 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4622 | |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4623 | if (ts) |
| 4624 | *ts = 0; |
| 4625 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4626 | cpu_buffer = iter->cpu_buffer; |
| 4627 | buffer = cpu_buffer->buffer; |
| 4628 | |
Steven Rostedt | 492a74f | 2010-01-25 15:17:47 -0500 | [diff] [blame] | 4629 | /* |
| 4630 | * Check if someone performed a consuming read to |
| 4631 | * the buffer. A consuming read invalidates the iterator |
| 4632 | * and we need to reset the iterator in this case. |
| 4633 | */ |
| 4634 | if (unlikely(iter->cache_read != cpu_buffer->read || |
| 4635 | iter->cache_reader_page != cpu_buffer->reader_page)) |
| 4636 | rb_iter_reset(iter); |
| 4637 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4638 | again: |
Steven Rostedt | 3c05d74 | 2010-01-26 16:14:08 -0500 | [diff] [blame] | 4639 | if (ring_buffer_iter_empty(iter)) |
| 4640 | return NULL; |
| 4641 | |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4642 | /* |
Steven Rostedt (VMware) | 3d2353d | 2020-05-13 15:18:01 -0400 | [diff] [blame] | 4643 | * As the writer can mess with what the iterator is trying |
| 4644 | * to read, just give up if we fail to get an event after |
| 4645 | * three tries. The iterator is not as reliable when reading |
| 4646 | * the ring buffer with an active write as the consumer is. |
| 4647 | * Do not warn if the three failures is reached. |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4648 | */ |
Steven Rostedt (VMware) | 3d2353d | 2020-05-13 15:18:01 -0400 | [diff] [blame] | 4649 | if (++nr_loops > 3) |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4650 | return NULL; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4651 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4652 | if (rb_per_cpu_empty(cpu_buffer)) |
| 4653 | return NULL; |
| 4654 | |
Steven Rostedt (Red Hat) | 10e83fd | 2014-07-23 19:45:12 -0400 | [diff] [blame] | 4655 | if (iter->head >= rb_page_size(iter->head_page)) { |
Steven Rostedt | 3c05d74 | 2010-01-26 16:14:08 -0500 | [diff] [blame] | 4656 | rb_inc_iter(iter); |
| 4657 | goto again; |
| 4658 | } |
| 4659 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4660 | event = rb_iter_head_event(iter); |
Steven Rostedt (VMware) | 3d2353d | 2020-05-13 15:18:01 -0400 | [diff] [blame] | 4661 | if (!event) |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4662 | goto again; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4663 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 4664 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4665 | case RINGBUF_TYPE_PADDING: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4666 | if (rb_null_event(event)) { |
| 4667 | rb_inc_iter(iter); |
| 4668 | goto again; |
| 4669 | } |
| 4670 | rb_advance_iter(iter); |
| 4671 | return event; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4672 | |
| 4673 | case RINGBUF_TYPE_TIME_EXTEND: |
| 4674 | /* Internal data, OK to advance */ |
| 4675 | rb_advance_iter(iter); |
| 4676 | goto again; |
| 4677 | |
| 4678 | case RINGBUF_TYPE_TIME_STAMP: |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4679 | if (ts) { |
Steven Rostedt (VMware) | e20044f | 2021-03-16 12:41:01 -0400 | [diff] [blame] | 4680 | *ts = rb_event_time_stamp(event); |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4681 | ring_buffer_normalize_time_stamp(cpu_buffer->buffer, |
| 4682 | cpu_buffer->cpu, ts); |
| 4683 | } |
| 4684 | /* Internal data, OK to advance */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4685 | rb_advance_iter(iter); |
| 4686 | goto again; |
| 4687 | |
| 4688 | case RINGBUF_TYPE_DATA: |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4689 | if (ts && !(*ts)) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4690 | *ts = iter->read_stamp + event->time_delta; |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 4691 | ring_buffer_normalize_time_stamp(buffer, |
| 4692 | cpu_buffer->cpu, ts); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4693 | } |
| 4694 | return event; |
| 4695 | |
| 4696 | default: |
Steven Rostedt (VMware) | da4d401 | 2020-05-13 15:36:22 -0400 | [diff] [blame] | 4697 | RB_WARN_ON(cpu_buffer, 1); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | return NULL; |
| 4701 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4702 | EXPORT_SYMBOL_GPL(ring_buffer_iter_peek); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4703 | |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4704 | static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4705 | { |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4706 | if (likely(!in_nmi())) { |
| 4707 | raw_spin_lock(&cpu_buffer->reader_lock); |
| 4708 | return true; |
| 4709 | } |
| 4710 | |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4711 | /* |
| 4712 | * If an NMI die dumps out the content of the ring buffer |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4713 | * trylock must be used to prevent a deadlock if the NMI |
| 4714 | * preempted a task that holds the ring buffer locks. If |
| 4715 | * we get the lock then all is fine, if not, then continue |
| 4716 | * to do the read, but this can corrupt the ring buffer, |
| 4717 | * so it must be permanently disabled from future writes. |
| 4718 | * Reading from NMI is a oneshot deal. |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4719 | */ |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4720 | if (raw_spin_trylock(&cpu_buffer->reader_lock)) |
| 4721 | return true; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4722 | |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4723 | /* Continue without locking, but disable the ring buffer */ |
| 4724 | atomic_inc(&cpu_buffer->record_disabled); |
| 4725 | return false; |
| 4726 | } |
| 4727 | |
| 4728 | static inline void |
| 4729 | rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked) |
| 4730 | { |
| 4731 | if (likely(locked)) |
| 4732 | raw_spin_unlock(&cpu_buffer->reader_lock); |
| 4733 | return; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4734 | } |
| 4735 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4736 | /** |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4737 | * ring_buffer_peek - peek at the next event to be read |
| 4738 | * @buffer: The ring buffer to read |
| 4739 | * @cpu: The cpu to peak at |
| 4740 | * @ts: The timestamp counter of this event. |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4741 | * @lost_events: a variable to store if events were lost (may be NULL) |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4742 | * |
| 4743 | * This will return the event that will be read next, but does |
| 4744 | * not consume the data. |
| 4745 | */ |
| 4746 | struct ring_buffer_event * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4747 | ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts, |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4748 | unsigned long *lost_events) |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4749 | { |
| 4750 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4751 | struct ring_buffer_event *event; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4752 | unsigned long flags; |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4753 | bool dolock; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4754 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4755 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4756 | return NULL; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4757 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4758 | again: |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4759 | local_irq_save(flags); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4760 | dolock = rb_reader_lock(cpu_buffer); |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4761 | event = rb_buffer_peek(cpu_buffer, ts, lost_events); |
Robert Richter | 469535a | 2009-07-30 19:19:18 +0200 | [diff] [blame] | 4762 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
| 4763 | rb_advance_reader(cpu_buffer); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4764 | rb_reader_unlock(cpu_buffer, dolock); |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4765 | local_irq_restore(flags); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4766 | |
Steven Rostedt | 1b959e1 | 2009-09-03 10:12:13 -0400 | [diff] [blame] | 4767 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4768 | goto again; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4769 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4770 | return event; |
| 4771 | } |
| 4772 | |
Steven Rostedt (VMware) | c9b7a4a | 2020-03-17 17:32:32 -0400 | [diff] [blame] | 4773 | /** ring_buffer_iter_dropped - report if there are dropped events |
| 4774 | * @iter: The ring buffer iterator |
| 4775 | * |
| 4776 | * Returns true if there was dropped events since the last peek. |
| 4777 | */ |
| 4778 | bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter) |
| 4779 | { |
| 4780 | bool ret = iter->missed_events != 0; |
| 4781 | |
| 4782 | iter->missed_events = 0; |
| 4783 | return ret; |
| 4784 | } |
| 4785 | EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped); |
| 4786 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4787 | /** |
| 4788 | * ring_buffer_iter_peek - peek at the next event to be read |
| 4789 | * @iter: The ring buffer iterator |
| 4790 | * @ts: The timestamp counter of this event. |
| 4791 | * |
| 4792 | * This will return the event that will be read next, but does |
| 4793 | * not increment the iterator. |
| 4794 | */ |
| 4795 | struct ring_buffer_event * |
| 4796 | ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts) |
| 4797 | { |
| 4798 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 4799 | struct ring_buffer_event *event; |
| 4800 | unsigned long flags; |
| 4801 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4802 | again: |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4803 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4804 | event = rb_iter_peek(iter, ts); |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4805 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4806 | |
Steven Rostedt | 1b959e1 | 2009-09-03 10:12:13 -0400 | [diff] [blame] | 4807 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4808 | goto again; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4809 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4810 | return event; |
| 4811 | } |
| 4812 | |
| 4813 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4814 | * ring_buffer_consume - return an event and consume it |
| 4815 | * @buffer: The ring buffer to get the next event from |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4816 | * @cpu: the cpu to read the buffer from |
| 4817 | * @ts: a variable to store the timestamp (may be NULL) |
| 4818 | * @lost_events: a variable to store if events were lost (may be NULL) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4819 | * |
| 4820 | * Returns the next event in the ring buffer, and that event is consumed. |
| 4821 | * Meaning, that sequential reads will keep returning a different event, |
| 4822 | * and eventually empty the ring buffer if the producer is slower. |
| 4823 | */ |
| 4824 | struct ring_buffer_event * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4825 | ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts, |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4826 | unsigned long *lost_events) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4827 | { |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4828 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4829 | struct ring_buffer_event *event = NULL; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4830 | unsigned long flags; |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4831 | bool dolock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4832 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4833 | again: |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4834 | /* might be called in atomic */ |
| 4835 | preempt_disable(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4836 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4837 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 4838 | goto out; |
| 4839 | |
| 4840 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4841 | local_irq_save(flags); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4842 | dolock = rb_reader_lock(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4843 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4844 | event = rb_buffer_peek(cpu_buffer, ts, lost_events); |
| 4845 | if (event) { |
| 4846 | cpu_buffer->lost_events = 0; |
Robert Richter | 469535a | 2009-07-30 19:19:18 +0200 | [diff] [blame] | 4847 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4848 | } |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4849 | |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4850 | rb_reader_unlock(cpu_buffer, dolock); |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4851 | local_irq_restore(flags); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4852 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4853 | out: |
| 4854 | preempt_enable(); |
| 4855 | |
Steven Rostedt | 1b959e1 | 2009-09-03 10:12:13 -0400 | [diff] [blame] | 4856 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4857 | goto again; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4858 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4859 | return event; |
| 4860 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4861 | EXPORT_SYMBOL_GPL(ring_buffer_consume); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4862 | |
| 4863 | /** |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4864 | * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4865 | * @buffer: The ring buffer to read from |
| 4866 | * @cpu: The cpu buffer to iterate over |
Douglas Anderson | 31b265b | 2019-03-08 11:32:04 -0800 | [diff] [blame] | 4867 | * @flags: gfp flags to use for memory allocation |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4868 | * |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4869 | * This performs the initial preparations necessary to iterate |
| 4870 | * through the buffer. Memory is allocated, buffer recording |
| 4871 | * is disabled, and the iterator pointer is returned to the caller. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4872 | * |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 4873 | * Disabling buffer recording prevents the reading from being |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4874 | * corrupted. This is not a consuming read, so a producer is not |
| 4875 | * expected. |
| 4876 | * |
| 4877 | * After a sequence of ring_buffer_read_prepare calls, the user is |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 4878 | * expected to make at least one call to ring_buffer_read_prepare_sync. |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4879 | * Afterwards, ring_buffer_read_start is invoked to get things going |
| 4880 | * for real. |
| 4881 | * |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 4882 | * This overall must be paired with ring_buffer_read_finish. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4883 | */ |
| 4884 | struct ring_buffer_iter * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4885 | ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4886 | { |
| 4887 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4888 | struct ring_buffer_iter *iter; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4889 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 4890 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4891 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4892 | |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4893 | iter = kzalloc(sizeof(*iter), flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4894 | if (!iter) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4895 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4896 | |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4897 | iter->event = kmalloc(BUF_MAX_DATA_SIZE, flags); |
| 4898 | if (!iter->event) { |
| 4899 | kfree(iter); |
| 4900 | return NULL; |
| 4901 | } |
| 4902 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4903 | cpu_buffer = buffer->buffers[cpu]; |
| 4904 | |
| 4905 | iter->cpu_buffer = cpu_buffer; |
| 4906 | |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 4907 | atomic_inc(&cpu_buffer->resize_disabled); |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4908 | |
| 4909 | return iter; |
| 4910 | } |
| 4911 | EXPORT_SYMBOL_GPL(ring_buffer_read_prepare); |
| 4912 | |
| 4913 | /** |
| 4914 | * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls |
| 4915 | * |
| 4916 | * All previously invoked ring_buffer_read_prepare calls to prepare |
| 4917 | * iterators will be synchronized. Afterwards, read_buffer_read_start |
| 4918 | * calls on those iterators are allowed. |
| 4919 | */ |
| 4920 | void |
| 4921 | ring_buffer_read_prepare_sync(void) |
| 4922 | { |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 4923 | synchronize_rcu(); |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4924 | } |
| 4925 | EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync); |
| 4926 | |
| 4927 | /** |
| 4928 | * ring_buffer_read_start - start a non consuming read of the buffer |
| 4929 | * @iter: The iterator returned by ring_buffer_read_prepare |
| 4930 | * |
| 4931 | * This finalizes the startup of an iteration through the buffer. |
| 4932 | * The iterator comes from a call to ring_buffer_read_prepare and |
| 4933 | * an intervening ring_buffer_read_prepare_sync must have been |
| 4934 | * performed. |
| 4935 | * |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 4936 | * Must be paired with ring_buffer_read_finish. |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4937 | */ |
| 4938 | void |
| 4939 | ring_buffer_read_start(struct ring_buffer_iter *iter) |
| 4940 | { |
| 4941 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4942 | unsigned long flags; |
| 4943 | |
| 4944 | if (!iter) |
| 4945 | return; |
| 4946 | |
| 4947 | cpu_buffer = iter->cpu_buffer; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4948 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4949 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 4950 | arch_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 4951 | rb_iter_reset(iter); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 4952 | arch_spin_unlock(&cpu_buffer->lock); |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4953 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4954 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4955 | EXPORT_SYMBOL_GPL(ring_buffer_read_start); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4956 | |
| 4957 | /** |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 4958 | * ring_buffer_read_finish - finish reading the iterator of the buffer |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4959 | * @iter: The iterator retrieved by ring_buffer_start |
| 4960 | * |
| 4961 | * This re-enables the recording to the buffer, and frees the |
| 4962 | * iterator. |
| 4963 | */ |
| 4964 | void |
| 4965 | ring_buffer_read_finish(struct ring_buffer_iter *iter) |
| 4966 | { |
| 4967 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
Steven Rostedt | 9366c1b | 2012-11-29 22:31:16 -0500 | [diff] [blame] | 4968 | unsigned long flags; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4969 | |
Steven Rostedt | 659f451 | 2012-05-14 17:02:33 -0400 | [diff] [blame] | 4970 | /* |
| 4971 | * Ring buffer is disabled from recording, here's a good place |
Steven Rostedt | 9366c1b | 2012-11-29 22:31:16 -0500 | [diff] [blame] | 4972 | * to check the integrity of the ring buffer. |
| 4973 | * Must prevent readers from trying to read, as the check |
| 4974 | * clears the HEAD page and readers require it. |
Steven Rostedt | 659f451 | 2012-05-14 17:02:33 -0400 | [diff] [blame] | 4975 | */ |
Steven Rostedt | 9366c1b | 2012-11-29 22:31:16 -0500 | [diff] [blame] | 4976 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 659f451 | 2012-05-14 17:02:33 -0400 | [diff] [blame] | 4977 | rb_check_pages(cpu_buffer); |
Steven Rostedt | 9366c1b | 2012-11-29 22:31:16 -0500 | [diff] [blame] | 4978 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 659f451 | 2012-05-14 17:02:33 -0400 | [diff] [blame] | 4979 | |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 4980 | atomic_dec(&cpu_buffer->resize_disabled); |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4981 | kfree(iter->event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4982 | kfree(iter); |
| 4983 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4984 | EXPORT_SYMBOL_GPL(ring_buffer_read_finish); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4985 | |
| 4986 | /** |
Steven Rostedt (VMware) | bc1a72a | 2020-03-17 17:32:25 -0400 | [diff] [blame] | 4987 | * ring_buffer_iter_advance - advance the iterator to the next location |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4988 | * @iter: The ring buffer iterator |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4989 | * |
Steven Rostedt (VMware) | bc1a72a | 2020-03-17 17:32:25 -0400 | [diff] [blame] | 4990 | * Move the location of the iterator such that the next read will |
| 4991 | * be the next location of the iterator. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4992 | */ |
Steven Rostedt (VMware) | bc1a72a | 2020-03-17 17:32:25 -0400 | [diff] [blame] | 4993 | void ring_buffer_iter_advance(struct ring_buffer_iter *iter) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4994 | { |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4995 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 4996 | unsigned long flags; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4997 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4998 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7e9391c | 2009-09-03 10:02:09 -0400 | [diff] [blame] | 4999 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5000 | rb_advance_iter(iter); |
| 5001 | |
Steven Rostedt (VMware) | bc1a72a | 2020-03-17 17:32:25 -0400 | [diff] [blame] | 5002 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5003 | } |
Steven Rostedt (VMware) | bc1a72a | 2020-03-17 17:32:25 -0400 | [diff] [blame] | 5004 | EXPORT_SYMBOL_GPL(ring_buffer_iter_advance); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5005 | |
| 5006 | /** |
| 5007 | * ring_buffer_size - return the size of the ring buffer (in bytes) |
| 5008 | * @buffer: The ring buffer. |
Fabian Frederick | 59e7cff | 2014-06-05 20:22:05 +0200 | [diff] [blame] | 5009 | * @cpu: The CPU to get ring buffer size from. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5010 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5011 | unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5012 | { |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5013 | /* |
| 5014 | * Earlier, this method returned |
| 5015 | * BUF_PAGE_SIZE * buffer->nr_pages |
| 5016 | * Since the nr_pages field is now removed, we have converted this to |
| 5017 | * return the per cpu buffer value. |
| 5018 | */ |
| 5019 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 5020 | return 0; |
| 5021 | |
| 5022 | return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5023 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 5024 | EXPORT_SYMBOL_GPL(ring_buffer_size); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5025 | |
| 5026 | static void |
| 5027 | rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) |
| 5028 | { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 5029 | rb_head_page_deactivate(cpu_buffer); |
| 5030 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5031 | cpu_buffer->head_page |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 5032 | = list_entry(cpu_buffer->pages, struct buffer_page, list); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 5033 | local_set(&cpu_buffer->head_page->write, 0); |
Steven Rostedt | 778c55d | 2009-05-01 18:44:45 -0400 | [diff] [blame] | 5034 | local_set(&cpu_buffer->head_page->entries, 0); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 5035 | local_set(&cpu_buffer->head_page->page->commit, 0); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5036 | |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 5037 | cpu_buffer->head_page->read = 0; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 5038 | |
| 5039 | cpu_buffer->tail_page = cpu_buffer->head_page; |
| 5040 | cpu_buffer->commit_page = cpu_buffer->head_page; |
| 5041 | |
| 5042 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 5043 | INIT_LIST_HEAD(&cpu_buffer->new_pages); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 5044 | local_set(&cpu_buffer->reader_page->write, 0); |
Steven Rostedt | 778c55d | 2009-05-01 18:44:45 -0400 | [diff] [blame] | 5045 | local_set(&cpu_buffer->reader_page->entries, 0); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 5046 | local_set(&cpu_buffer->reader_page->page->commit, 0); |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 5047 | cpu_buffer->reader_page->read = 0; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 5048 | |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 5049 | local_set(&cpu_buffer->entries_bytes, 0); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 5050 | local_set(&cpu_buffer->overrun, 0); |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 5051 | local_set(&cpu_buffer->commit_overrun, 0); |
| 5052 | local_set(&cpu_buffer->dropped_events, 0); |
Steven Rostedt | e4906ef | 2009-04-30 20:49:44 -0400 | [diff] [blame] | 5053 | local_set(&cpu_buffer->entries, 0); |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 5054 | local_set(&cpu_buffer->committing, 0); |
| 5055 | local_set(&cpu_buffer->commits, 0); |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 5056 | local_set(&cpu_buffer->pages_touched, 0); |
| 5057 | local_set(&cpu_buffer->pages_read, 0); |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 5058 | cpu_buffer->last_pages_touch = 0; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 5059 | cpu_buffer->shortest_full = 0; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 5060 | cpu_buffer->read = 0; |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 5061 | cpu_buffer->read_bytes = 0; |
Steven Rostedt | 69507c0 | 2009-01-21 18:45:57 -0500 | [diff] [blame] | 5062 | |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 5063 | rb_time_set(&cpu_buffer->write_stamp, 0); |
| 5064 | rb_time_set(&cpu_buffer->before_stamp, 0); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 5065 | |
Steven Rostedt (VMware) | 8672e494 | 2021-03-16 12:41:02 -0400 | [diff] [blame] | 5066 | memset(cpu_buffer->event_stamp, 0, sizeof(cpu_buffer->event_stamp)); |
| 5067 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 5068 | cpu_buffer->lost_events = 0; |
| 5069 | cpu_buffer->last_overrun = 0; |
| 5070 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 5071 | rb_head_page_activate(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5072 | } |
| 5073 | |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame] | 5074 | /* Must have disabled the cpu buffer then done a synchronize_rcu */ |
| 5075 | static void reset_disabled_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5076 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5077 | unsigned long flags; |
| 5078 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 5079 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 5080 | |
Steven Rostedt | 41b6a95 | 2009-09-02 09:59:48 -0400 | [diff] [blame] | 5081 | if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing))) |
| 5082 | goto out; |
| 5083 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 5084 | arch_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5085 | |
| 5086 | rb_reset_cpu(cpu_buffer); |
| 5087 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 5088 | arch_spin_unlock(&cpu_buffer->lock); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 5089 | |
Steven Rostedt | 41b6a95 | 2009-09-02 09:59:48 -0400 | [diff] [blame] | 5090 | out: |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 5091 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame] | 5092 | } |
| 5093 | |
| 5094 | /** |
| 5095 | * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer |
| 5096 | * @buffer: The ring buffer to reset a per cpu buffer of |
| 5097 | * @cpu: The CPU buffer to be reset |
| 5098 | */ |
| 5099 | void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu) |
| 5100 | { |
| 5101 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
| 5102 | |
| 5103 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 5104 | return; |
| 5105 | |
Gaurav Kohli | bbeb974 | 2020-10-06 15:03:53 +0530 | [diff] [blame] | 5106 | /* prevent another thread from changing buffer sizes */ |
| 5107 | mutex_lock(&buffer->mutex); |
| 5108 | |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame] | 5109 | atomic_inc(&cpu_buffer->resize_disabled); |
| 5110 | atomic_inc(&cpu_buffer->record_disabled); |
| 5111 | |
| 5112 | /* Make sure all commits have finished */ |
| 5113 | synchronize_rcu(); |
| 5114 | |
| 5115 | reset_disabled_cpu_buffer(cpu_buffer); |
Steven Rostedt | 41ede23 | 2009-05-01 20:26:54 -0400 | [diff] [blame] | 5116 | |
| 5117 | atomic_dec(&cpu_buffer->record_disabled); |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 5118 | atomic_dec(&cpu_buffer->resize_disabled); |
Gaurav Kohli | bbeb974 | 2020-10-06 15:03:53 +0530 | [diff] [blame] | 5119 | |
| 5120 | mutex_unlock(&buffer->mutex); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5121 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 5122 | EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5123 | |
| 5124 | /** |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame] | 5125 | * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer |
| 5126 | * @buffer: The ring buffer to reset a per cpu buffer of |
| 5127 | * @cpu: The CPU buffer to be reset |
| 5128 | */ |
| 5129 | void ring_buffer_reset_online_cpus(struct trace_buffer *buffer) |
| 5130 | { |
| 5131 | struct ring_buffer_per_cpu *cpu_buffer; |
| 5132 | int cpu; |
| 5133 | |
Gaurav Kohli | bbeb974 | 2020-10-06 15:03:53 +0530 | [diff] [blame] | 5134 | /* prevent another thread from changing buffer sizes */ |
| 5135 | mutex_lock(&buffer->mutex); |
| 5136 | |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame] | 5137 | for_each_online_buffer_cpu(buffer, cpu) { |
| 5138 | cpu_buffer = buffer->buffers[cpu]; |
| 5139 | |
| 5140 | atomic_inc(&cpu_buffer->resize_disabled); |
| 5141 | atomic_inc(&cpu_buffer->record_disabled); |
| 5142 | } |
| 5143 | |
| 5144 | /* Make sure all commits have finished */ |
| 5145 | synchronize_rcu(); |
| 5146 | |
| 5147 | for_each_online_buffer_cpu(buffer, cpu) { |
| 5148 | cpu_buffer = buffer->buffers[cpu]; |
| 5149 | |
| 5150 | reset_disabled_cpu_buffer(cpu_buffer); |
| 5151 | |
| 5152 | atomic_dec(&cpu_buffer->record_disabled); |
| 5153 | atomic_dec(&cpu_buffer->resize_disabled); |
| 5154 | } |
Gaurav Kohli | bbeb974 | 2020-10-06 15:03:53 +0530 | [diff] [blame] | 5155 | |
| 5156 | mutex_unlock(&buffer->mutex); |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5160 | * ring_buffer_reset - reset a ring buffer |
| 5161 | * @buffer: The ring buffer to reset all cpu buffers |
| 5162 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5163 | void ring_buffer_reset(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5164 | { |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame] | 5165 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5166 | int cpu; |
| 5167 | |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame] | 5168 | for_each_buffer_cpu(buffer, cpu) { |
| 5169 | cpu_buffer = buffer->buffers[cpu]; |
| 5170 | |
| 5171 | atomic_inc(&cpu_buffer->resize_disabled); |
| 5172 | atomic_inc(&cpu_buffer->record_disabled); |
| 5173 | } |
| 5174 | |
| 5175 | /* Make sure all commits have finished */ |
| 5176 | synchronize_rcu(); |
| 5177 | |
| 5178 | for_each_buffer_cpu(buffer, cpu) { |
| 5179 | cpu_buffer = buffer->buffers[cpu]; |
| 5180 | |
| 5181 | reset_disabled_cpu_buffer(cpu_buffer); |
| 5182 | |
| 5183 | atomic_dec(&cpu_buffer->record_disabled); |
| 5184 | atomic_dec(&cpu_buffer->resize_disabled); |
| 5185 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5186 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 5187 | EXPORT_SYMBOL_GPL(ring_buffer_reset); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5188 | |
| 5189 | /** |
| 5190 | * rind_buffer_empty - is the ring buffer empty? |
| 5191 | * @buffer: The ring buffer to test |
| 5192 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5193 | bool ring_buffer_empty(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5194 | { |
| 5195 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 5196 | unsigned long flags; |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 5197 | bool dolock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5198 | int cpu; |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 5199 | int ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5200 | |
| 5201 | /* yes this is racy, but if you don't like the race, lock the buffer */ |
| 5202 | for_each_buffer_cpu(buffer, cpu) { |
| 5203 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 5204 | local_irq_save(flags); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 5205 | dolock = rb_reader_lock(cpu_buffer); |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 5206 | ret = rb_per_cpu_empty(cpu_buffer); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 5207 | rb_reader_unlock(cpu_buffer, dolock); |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 5208 | local_irq_restore(flags); |
| 5209 | |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 5210 | if (!ret) |
Yaowei Bai | 3d4e204 | 2015-09-29 22:43:32 +0800 | [diff] [blame] | 5211 | return false; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5212 | } |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5213 | |
Yaowei Bai | 3d4e204 | 2015-09-29 22:43:32 +0800 | [diff] [blame] | 5214 | return true; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5215 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 5216 | EXPORT_SYMBOL_GPL(ring_buffer_empty); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5217 | |
| 5218 | /** |
| 5219 | * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty? |
| 5220 | * @buffer: The ring buffer |
| 5221 | * @cpu: The CPU buffer to test |
| 5222 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5223 | bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5224 | { |
| 5225 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 5226 | unsigned long flags; |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 5227 | bool dolock; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 5228 | int ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5229 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 5230 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Yaowei Bai | 3d4e204 | 2015-09-29 22:43:32 +0800 | [diff] [blame] | 5231 | return true; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5232 | |
| 5233 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 5234 | local_irq_save(flags); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 5235 | dolock = rb_reader_lock(cpu_buffer); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5236 | ret = rb_per_cpu_empty(cpu_buffer); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 5237 | rb_reader_unlock(cpu_buffer, dolock); |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 5238 | local_irq_restore(flags); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5239 | |
| 5240 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5241 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 5242 | EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5243 | |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 5244 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5245 | /** |
| 5246 | * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers |
| 5247 | * @buffer_a: One buffer to swap with |
| 5248 | * @buffer_b: The other buffer to swap with |
Fabian Frederick | 59e7cff | 2014-06-05 20:22:05 +0200 | [diff] [blame] | 5249 | * @cpu: the CPU of the buffers to swap |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5250 | * |
| 5251 | * This function is useful for tracers that want to take a "snapshot" |
| 5252 | * of a CPU buffer and has another back up buffer lying around. |
| 5253 | * it is expected that the tracer handles the cpu buffer not being |
| 5254 | * used at the moment. |
| 5255 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5256 | int ring_buffer_swap_cpu(struct trace_buffer *buffer_a, |
| 5257 | struct trace_buffer *buffer_b, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5258 | { |
| 5259 | struct ring_buffer_per_cpu *cpu_buffer_a; |
| 5260 | struct ring_buffer_per_cpu *cpu_buffer_b; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5261 | int ret = -EINVAL; |
| 5262 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 5263 | if (!cpumask_test_cpu(cpu, buffer_a->cpumask) || |
| 5264 | !cpumask_test_cpu(cpu, buffer_b->cpumask)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5265 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5266 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5267 | cpu_buffer_a = buffer_a->buffers[cpu]; |
| 5268 | cpu_buffer_b = buffer_b->buffers[cpu]; |
| 5269 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5270 | /* At least make sure the two buffers are somewhat the same */ |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5271 | if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5272 | goto out; |
| 5273 | |
| 5274 | ret = -EAGAIN; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5275 | |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 5276 | if (atomic_read(&buffer_a->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5277 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 5278 | |
| 5279 | if (atomic_read(&buffer_b->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5280 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 5281 | |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 5282 | if (atomic_read(&cpu_buffer_a->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5283 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 5284 | |
| 5285 | if (atomic_read(&cpu_buffer_b->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5286 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 5287 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5288 | /* |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 5289 | * We can't do a synchronize_rcu here because this |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5290 | * function can be called in atomic context. |
| 5291 | * Normally this will be called from the same CPU as cpu. |
| 5292 | * If not it's up to the caller to protect this. |
| 5293 | */ |
| 5294 | atomic_inc(&cpu_buffer_a->record_disabled); |
| 5295 | atomic_inc(&cpu_buffer_b->record_disabled); |
| 5296 | |
Steven Rostedt | 9827799 | 2009-09-02 10:56:15 -0400 | [diff] [blame] | 5297 | ret = -EBUSY; |
| 5298 | if (local_read(&cpu_buffer_a->committing)) |
| 5299 | goto out_dec; |
| 5300 | if (local_read(&cpu_buffer_b->committing)) |
| 5301 | goto out_dec; |
| 5302 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5303 | buffer_a->buffers[cpu] = cpu_buffer_b; |
| 5304 | buffer_b->buffers[cpu] = cpu_buffer_a; |
| 5305 | |
| 5306 | cpu_buffer_b->buffer = buffer_a; |
| 5307 | cpu_buffer_a->buffer = buffer_b; |
| 5308 | |
Steven Rostedt | 9827799 | 2009-09-02 10:56:15 -0400 | [diff] [blame] | 5309 | ret = 0; |
| 5310 | |
| 5311 | out_dec: |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5312 | atomic_dec(&cpu_buffer_a->record_disabled); |
| 5313 | atomic_dec(&cpu_buffer_b->record_disabled); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5314 | out: |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5315 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5316 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 5317 | EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu); |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 5318 | #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5319 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5320 | /** |
| 5321 | * ring_buffer_alloc_read_page - allocate a page to read from buffer |
| 5322 | * @buffer: the buffer to allocate for. |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 5323 | * @cpu: the cpu buffer to allocate. |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5324 | * |
| 5325 | * This function is used in conjunction with ring_buffer_read_page. |
| 5326 | * When reading a full page from the ring buffer, these functions |
| 5327 | * can be used to speed up the process. The calling function should |
| 5328 | * allocate a few pages first with this function. Then when it |
| 5329 | * needs to get pages from the ring buffer, it passes the result |
| 5330 | * of this function into ring_buffer_read_page, which will swap |
| 5331 | * the page that was allocated, with the read page of the buffer. |
| 5332 | * |
| 5333 | * Returns: |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 5334 | * The page allocated, or ERR_PTR |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5335 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5336 | void *ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5337 | { |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 5338 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5339 | struct buffer_data_page *bpage = NULL; |
| 5340 | unsigned long flags; |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 5341 | struct page *page; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5342 | |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 5343 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 5344 | return ERR_PTR(-ENODEV); |
| 5345 | |
| 5346 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5347 | local_irq_save(flags); |
| 5348 | arch_spin_lock(&cpu_buffer->lock); |
| 5349 | |
| 5350 | if (cpu_buffer->free_page) { |
| 5351 | bpage = cpu_buffer->free_page; |
| 5352 | cpu_buffer->free_page = NULL; |
| 5353 | } |
| 5354 | |
| 5355 | arch_spin_unlock(&cpu_buffer->lock); |
| 5356 | local_irq_restore(flags); |
| 5357 | |
| 5358 | if (bpage) |
| 5359 | goto out; |
| 5360 | |
Vaibhav Nagarnaik | d7ec4bf | 2011-06-07 17:01:42 -0700 | [diff] [blame] | 5361 | page = alloc_pages_node(cpu_to_node(cpu), |
| 5362 | GFP_KERNEL | __GFP_NORETRY, 0); |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 5363 | if (!page) |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 5364 | return ERR_PTR(-ENOMEM); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5365 | |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 5366 | bpage = page_address(page); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5367 | |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5368 | out: |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5369 | rb_init_page(bpage); |
| 5370 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 5371 | return bpage; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5372 | } |
Steven Rostedt | d6ce96d | 2009-05-05 01:15:24 -0400 | [diff] [blame] | 5373 | EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5374 | |
| 5375 | /** |
| 5376 | * ring_buffer_free_read_page - free an allocated read page |
| 5377 | * @buffer: the buffer the page was allocate for |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5378 | * @cpu: the cpu buffer the page came from |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5379 | * @data: the page to free |
| 5380 | * |
| 5381 | * Free a page allocated from ring_buffer_alloc_read_page. |
| 5382 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5383 | void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu, void *data) |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5384 | { |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5385 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
| 5386 | struct buffer_data_page *bpage = data; |
Steven Rostedt (VMware) | ae415fa | 2017-12-22 21:19:29 -0500 | [diff] [blame] | 5387 | struct page *page = virt_to_page(bpage); |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5388 | unsigned long flags; |
| 5389 | |
Steven Rostedt (VMware) | ae415fa | 2017-12-22 21:19:29 -0500 | [diff] [blame] | 5390 | /* If the page is still in use someplace else, we can't reuse it */ |
| 5391 | if (page_ref_count(page) > 1) |
| 5392 | goto out; |
| 5393 | |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5394 | local_irq_save(flags); |
| 5395 | arch_spin_lock(&cpu_buffer->lock); |
| 5396 | |
| 5397 | if (!cpu_buffer->free_page) { |
| 5398 | cpu_buffer->free_page = bpage; |
| 5399 | bpage = NULL; |
| 5400 | } |
| 5401 | |
| 5402 | arch_spin_unlock(&cpu_buffer->lock); |
| 5403 | local_irq_restore(flags); |
| 5404 | |
Steven Rostedt (VMware) | ae415fa | 2017-12-22 21:19:29 -0500 | [diff] [blame] | 5405 | out: |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5406 | free_page((unsigned long)bpage); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5407 | } |
Steven Rostedt | d6ce96d | 2009-05-05 01:15:24 -0400 | [diff] [blame] | 5408 | EXPORT_SYMBOL_GPL(ring_buffer_free_read_page); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5409 | |
| 5410 | /** |
| 5411 | * ring_buffer_read_page - extract a page from the ring buffer |
| 5412 | * @buffer: buffer to extract from |
| 5413 | * @data_page: the page to use allocated from ring_buffer_alloc_read_page |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5414 | * @len: amount to extract |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5415 | * @cpu: the cpu of the buffer to extract |
| 5416 | * @full: should the extraction only happen when the page is full. |
| 5417 | * |
| 5418 | * This function will pull out a page from the ring buffer and consume it. |
| 5419 | * @data_page must be the address of the variable that was returned |
| 5420 | * from ring_buffer_alloc_read_page. This is because the page might be used |
| 5421 | * to swap with a page in the ring buffer. |
| 5422 | * |
| 5423 | * for example: |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 5424 | * rpage = ring_buffer_alloc_read_page(buffer, cpu); |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 5425 | * if (IS_ERR(rpage)) |
| 5426 | * return PTR_ERR(rpage); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5427 | * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0); |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5428 | * if (ret >= 0) |
| 5429 | * process_page(rpage, ret); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5430 | * |
| 5431 | * When @full is set, the function will not return true unless |
| 5432 | * the writer is off the reader page. |
| 5433 | * |
| 5434 | * Note: it is up to the calling functions to handle sleeps and wakeups. |
| 5435 | * The ring buffer can be used anywhere in the kernel and can not |
| 5436 | * blindly call wake_up. The layer that uses the ring buffer must be |
| 5437 | * responsible for that. |
| 5438 | * |
| 5439 | * Returns: |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5440 | * >=0 if data has been transferred, returns the offset of consumed data. |
| 5441 | * <0 if no data has been transferred. |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5442 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5443 | int ring_buffer_read_page(struct trace_buffer *buffer, |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5444 | void **data_page, size_t len, int cpu, int full) |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5445 | { |
| 5446 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
| 5447 | struct ring_buffer_event *event; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 5448 | struct buffer_data_page *bpage; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5449 | struct buffer_page *reader; |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5450 | unsigned long missed_events; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5451 | unsigned long flags; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5452 | unsigned int commit; |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5453 | unsigned int read; |
Steven Rostedt | 4f3640f | 2009-03-03 23:52:42 -0500 | [diff] [blame] | 5454 | u64 save_timestamp; |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5455 | int ret = -1; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5456 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5457 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 5458 | goto out; |
| 5459 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5460 | /* |
| 5461 | * If len is not big enough to hold the page header, then |
| 5462 | * we can not copy anything. |
| 5463 | */ |
| 5464 | if (len <= BUF_PAGE_HDR_SIZE) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5465 | goto out; |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5466 | |
| 5467 | len -= BUF_PAGE_HDR_SIZE; |
| 5468 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5469 | if (!data_page) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5470 | goto out; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5471 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 5472 | bpage = *data_page; |
| 5473 | if (!bpage) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5474 | goto out; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5475 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 5476 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5477 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5478 | reader = rb_get_reader_page(cpu_buffer); |
| 5479 | if (!reader) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5480 | goto out_unlock; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5481 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5482 | event = rb_reader_event(cpu_buffer); |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5483 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5484 | read = reader->read; |
| 5485 | commit = rb_page_commit(reader); |
| 5486 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 5487 | /* Check if any events were dropped */ |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5488 | missed_events = cpu_buffer->lost_events; |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 5489 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5490 | /* |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5491 | * If this page has been partially read or |
| 5492 | * if len is not big enough to read the rest of the page or |
| 5493 | * a writer is still on the page, then |
| 5494 | * we must copy the data from the page to the buffer. |
| 5495 | * Otherwise, we can simply swap the page with the one passed in. |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5496 | */ |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5497 | if (read || (len < (commit - read)) || |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5498 | cpu_buffer->reader_page == cpu_buffer->commit_page) { |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5499 | struct buffer_data_page *rpage = cpu_buffer->reader_page->page; |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5500 | unsigned int rpos = read; |
| 5501 | unsigned int pos = 0; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5502 | unsigned int size; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5503 | |
| 5504 | if (full) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5505 | goto out_unlock; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5506 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5507 | if (len > (commit - read)) |
| 5508 | len = (commit - read); |
| 5509 | |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 5510 | /* Always keep the time extend and data together */ |
| 5511 | size = rb_event_ts_length(event); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5512 | |
| 5513 | if (len < size) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5514 | goto out_unlock; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5515 | |
Steven Rostedt | 4f3640f | 2009-03-03 23:52:42 -0500 | [diff] [blame] | 5516 | /* save the current timestamp, since the user will need it */ |
| 5517 | save_timestamp = cpu_buffer->read_stamp; |
| 5518 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5519 | /* Need to copy one event at a time */ |
| 5520 | do { |
David Sharp | e1e3592 | 2010-12-22 16:38:24 -0800 | [diff] [blame] | 5521 | /* We need the size of one event, because |
| 5522 | * rb_advance_reader only advances by one event, |
| 5523 | * whereas rb_event_ts_length may include the size of |
| 5524 | * one or two events. |
| 5525 | * We have already ensured there's enough space if this |
| 5526 | * is a time extend. */ |
| 5527 | size = rb_event_length(event); |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5528 | memcpy(bpage->data + pos, rpage->data + rpos, size); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5529 | |
| 5530 | len -= size; |
| 5531 | |
| 5532 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5533 | rpos = reader->read; |
| 5534 | pos += size; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5535 | |
Huang Ying | 18fab91 | 2010-07-28 14:14:01 +0800 | [diff] [blame] | 5536 | if (rpos >= commit) |
| 5537 | break; |
| 5538 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5539 | event = rb_reader_event(cpu_buffer); |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 5540 | /* Always keep the time extend and data together */ |
| 5541 | size = rb_event_ts_length(event); |
David Sharp | e1e3592 | 2010-12-22 16:38:24 -0800 | [diff] [blame] | 5542 | } while (len >= size); |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5543 | |
| 5544 | /* update bpage */ |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5545 | local_set(&bpage->commit, pos); |
Steven Rostedt | 4f3640f | 2009-03-03 23:52:42 -0500 | [diff] [blame] | 5546 | bpage->time_stamp = save_timestamp; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5547 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5548 | /* we copied everything to the beginning */ |
| 5549 | read = 0; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5550 | } else { |
Steven Rostedt | afbab76 | 2009-05-01 19:40:05 -0400 | [diff] [blame] | 5551 | /* update the entry counter */ |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 5552 | cpu_buffer->read += rb_page_entries(reader); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 5553 | cpu_buffer->read_bytes += BUF_PAGE_SIZE; |
Steven Rostedt | afbab76 | 2009-05-01 19:40:05 -0400 | [diff] [blame] | 5554 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5555 | /* swap the pages */ |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 5556 | rb_init_page(bpage); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5557 | bpage = reader->page; |
| 5558 | reader->page = *data_page; |
| 5559 | local_set(&reader->write, 0); |
Steven Rostedt | 778c55d | 2009-05-01 18:44:45 -0400 | [diff] [blame] | 5560 | local_set(&reader->entries, 0); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5561 | reader->read = 0; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 5562 | *data_page = bpage; |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5563 | |
| 5564 | /* |
| 5565 | * Use the real_end for the data size, |
| 5566 | * This gives us a chance to store the lost events |
| 5567 | * on the page. |
| 5568 | */ |
| 5569 | if (reader->real_end) |
| 5570 | local_set(&bpage->commit, reader->real_end); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5571 | } |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5572 | ret = read; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5573 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 5574 | cpu_buffer->lost_events = 0; |
Steven Rostedt | 2711ca2 | 2010-05-21 13:32:26 -0400 | [diff] [blame] | 5575 | |
| 5576 | commit = local_read(&bpage->commit); |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 5577 | /* |
| 5578 | * Set a flag in the commit field if we lost events |
| 5579 | */ |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5580 | if (missed_events) { |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5581 | /* If there is room at the end of the page to save the |
| 5582 | * missed events, then record it there. |
| 5583 | */ |
| 5584 | if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) { |
| 5585 | memcpy(&bpage->data[commit], &missed_events, |
| 5586 | sizeof(missed_events)); |
| 5587 | local_add(RB_MISSED_STORED, &bpage->commit); |
Steven Rostedt | 2711ca2 | 2010-05-21 13:32:26 -0400 | [diff] [blame] | 5588 | commit += sizeof(missed_events); |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5589 | } |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 5590 | local_add(RB_MISSED_EVENTS, &bpage->commit); |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5591 | } |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 5592 | |
Steven Rostedt | 2711ca2 | 2010-05-21 13:32:26 -0400 | [diff] [blame] | 5593 | /* |
| 5594 | * This page may be off to user land. Zero it out here. |
| 5595 | */ |
| 5596 | if (commit < BUF_PAGE_SIZE) |
| 5597 | memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit); |
| 5598 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5599 | out_unlock: |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 5600 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5601 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5602 | out: |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5603 | return ret; |
| 5604 | } |
Steven Rostedt | d6ce96d | 2009-05-05 01:15:24 -0400 | [diff] [blame] | 5605 | EXPORT_SYMBOL_GPL(ring_buffer_read_page); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5606 | |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 5607 | /* |
| 5608 | * We only allocate new buffers, never free them if the CPU goes down. |
| 5609 | * If we were to free the buffer, then the user would lose any trace that was in |
| 5610 | * the buffer. |
| 5611 | */ |
| 5612 | int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5613 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5614 | struct trace_buffer *buffer; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 5615 | long nr_pages_same; |
| 5616 | int cpu_i; |
| 5617 | unsigned long nr_pages; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5618 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5619 | buffer = container_of(node, struct trace_buffer, node); |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 5620 | if (cpumask_test_cpu(cpu, buffer->cpumask)) |
| 5621 | return 0; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5622 | |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 5623 | nr_pages = 0; |
| 5624 | nr_pages_same = 1; |
| 5625 | /* check if all cpu sizes are same */ |
| 5626 | for_each_buffer_cpu(buffer, cpu_i) { |
| 5627 | /* fill in the size from first enabled cpu */ |
| 5628 | if (nr_pages == 0) |
| 5629 | nr_pages = buffer->buffers[cpu_i]->nr_pages; |
| 5630 | if (nr_pages != buffer->buffers[cpu_i]->nr_pages) { |
| 5631 | nr_pages_same = 0; |
| 5632 | break; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5633 | } |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5634 | } |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 5635 | /* allocate minimum pages, user can later expand it */ |
| 5636 | if (!nr_pages_same) |
| 5637 | nr_pages = 2; |
| 5638 | buffer->buffers[cpu] = |
| 5639 | rb_allocate_cpu_buffer(buffer, nr_pages, cpu); |
| 5640 | if (!buffer->buffers[cpu]) { |
| 5641 | WARN(1, "failed to allocate ring buffer on CPU %u\n", |
| 5642 | cpu); |
| 5643 | return -ENOMEM; |
| 5644 | } |
| 5645 | smp_wmb(); |
| 5646 | cpumask_set_cpu(cpu, buffer->cpumask); |
| 5647 | return 0; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5648 | } |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5649 | |
| 5650 | #ifdef CONFIG_RING_BUFFER_STARTUP_TEST |
| 5651 | /* |
| 5652 | * This is a basic integrity check of the ring buffer. |
| 5653 | * Late in the boot cycle this test will run when configured in. |
| 5654 | * It will kick off a thread per CPU that will go into a loop |
| 5655 | * writing to the per cpu ring buffer various sizes of data. |
| 5656 | * Some of the data will be large items, some small. |
| 5657 | * |
| 5658 | * Another thread is created that goes into a spin, sending out |
| 5659 | * IPIs to the other CPUs to also write into the ring buffer. |
| 5660 | * this is to test the nesting ability of the buffer. |
| 5661 | * |
| 5662 | * Basic stats are recorded and reported. If something in the |
| 5663 | * ring buffer should happen that's not expected, a big warning |
| 5664 | * is displayed and all ring buffers are disabled. |
| 5665 | */ |
| 5666 | static struct task_struct *rb_threads[NR_CPUS] __initdata; |
| 5667 | |
| 5668 | struct rb_test_data { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5669 | struct trace_buffer *buffer; |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5670 | unsigned long events; |
| 5671 | unsigned long bytes_written; |
| 5672 | unsigned long bytes_alloc; |
| 5673 | unsigned long bytes_dropped; |
| 5674 | unsigned long events_nested; |
| 5675 | unsigned long bytes_written_nested; |
| 5676 | unsigned long bytes_alloc_nested; |
| 5677 | unsigned long bytes_dropped_nested; |
| 5678 | int min_size_nested; |
| 5679 | int max_size_nested; |
| 5680 | int max_size; |
| 5681 | int min_size; |
| 5682 | int cpu; |
| 5683 | int cnt; |
| 5684 | }; |
| 5685 | |
| 5686 | static struct rb_test_data rb_data[NR_CPUS] __initdata; |
| 5687 | |
| 5688 | /* 1 meg per cpu */ |
| 5689 | #define RB_TEST_BUFFER_SIZE 1048576 |
| 5690 | |
| 5691 | static char rb_string[] __initdata = |
| 5692 | "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\" |
| 5693 | "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890" |
| 5694 | "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv"; |
| 5695 | |
| 5696 | static bool rb_test_started __initdata; |
| 5697 | |
| 5698 | struct rb_item { |
| 5699 | int size; |
| 5700 | char str[]; |
| 5701 | }; |
| 5702 | |
| 5703 | static __init int rb_write_something(struct rb_test_data *data, bool nested) |
| 5704 | { |
| 5705 | struct ring_buffer_event *event; |
| 5706 | struct rb_item *item; |
| 5707 | bool started; |
| 5708 | int event_len; |
| 5709 | int size; |
| 5710 | int len; |
| 5711 | int cnt; |
| 5712 | |
| 5713 | /* Have nested writes different that what is written */ |
| 5714 | cnt = data->cnt + (nested ? 27 : 0); |
| 5715 | |
| 5716 | /* Multiply cnt by ~e, to make some unique increment */ |
YueHaibing | 40ed29b | 2018-09-23 12:11:33 +0000 | [diff] [blame] | 5717 | size = (cnt * 68 / 25) % (sizeof(rb_string) - 1); |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5718 | |
| 5719 | len = size + sizeof(struct rb_item); |
| 5720 | |
| 5721 | started = rb_test_started; |
| 5722 | /* read rb_test_started before checking buffer enabled */ |
| 5723 | smp_rmb(); |
| 5724 | |
| 5725 | event = ring_buffer_lock_reserve(data->buffer, len); |
| 5726 | if (!event) { |
| 5727 | /* Ignore dropped events before test starts. */ |
| 5728 | if (started) { |
| 5729 | if (nested) |
| 5730 | data->bytes_dropped += len; |
| 5731 | else |
| 5732 | data->bytes_dropped_nested += len; |
| 5733 | } |
| 5734 | return len; |
| 5735 | } |
| 5736 | |
| 5737 | event_len = ring_buffer_event_length(event); |
| 5738 | |
| 5739 | if (RB_WARN_ON(data->buffer, event_len < len)) |
| 5740 | goto out; |
| 5741 | |
| 5742 | item = ring_buffer_event_data(event); |
| 5743 | item->size = size; |
| 5744 | memcpy(item->str, rb_string, size); |
| 5745 | |
| 5746 | if (nested) { |
| 5747 | data->bytes_alloc_nested += event_len; |
| 5748 | data->bytes_written_nested += len; |
| 5749 | data->events_nested++; |
| 5750 | if (!data->min_size_nested || len < data->min_size_nested) |
| 5751 | data->min_size_nested = len; |
| 5752 | if (len > data->max_size_nested) |
| 5753 | data->max_size_nested = len; |
| 5754 | } else { |
| 5755 | data->bytes_alloc += event_len; |
| 5756 | data->bytes_written += len; |
| 5757 | data->events++; |
| 5758 | if (!data->min_size || len < data->min_size) |
| 5759 | data->max_size = len; |
| 5760 | if (len > data->max_size) |
| 5761 | data->max_size = len; |
| 5762 | } |
| 5763 | |
| 5764 | out: |
| 5765 | ring_buffer_unlock_commit(data->buffer, event); |
| 5766 | |
| 5767 | return 0; |
| 5768 | } |
| 5769 | |
| 5770 | static __init int rb_test(void *arg) |
| 5771 | { |
| 5772 | struct rb_test_data *data = arg; |
| 5773 | |
| 5774 | while (!kthread_should_stop()) { |
| 5775 | rb_write_something(data, false); |
| 5776 | data->cnt++; |
| 5777 | |
| 5778 | set_current_state(TASK_INTERRUPTIBLE); |
| 5779 | /* Now sleep between a min of 100-300us and a max of 1ms */ |
| 5780 | usleep_range(((data->cnt % 3) + 1) * 100, 1000); |
| 5781 | } |
| 5782 | |
| 5783 | return 0; |
| 5784 | } |
| 5785 | |
| 5786 | static __init void rb_ipi(void *ignore) |
| 5787 | { |
| 5788 | struct rb_test_data *data; |
| 5789 | int cpu = smp_processor_id(); |
| 5790 | |
| 5791 | data = &rb_data[cpu]; |
| 5792 | rb_write_something(data, true); |
| 5793 | } |
| 5794 | |
| 5795 | static __init int rb_hammer_test(void *arg) |
| 5796 | { |
| 5797 | while (!kthread_should_stop()) { |
| 5798 | |
| 5799 | /* Send an IPI to all cpus to write data! */ |
| 5800 | smp_call_function(rb_ipi, NULL, 1); |
| 5801 | /* No sleep, but for non preempt, let others run */ |
| 5802 | schedule(); |
| 5803 | } |
| 5804 | |
| 5805 | return 0; |
| 5806 | } |
| 5807 | |
| 5808 | static __init int test_ringbuffer(void) |
| 5809 | { |
| 5810 | struct task_struct *rb_hammer; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5811 | struct trace_buffer *buffer; |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5812 | int cpu; |
| 5813 | int ret = 0; |
| 5814 | |
Steven Rostedt (VMware) | a356646 | 2019-12-02 16:25:27 -0500 | [diff] [blame] | 5815 | if (security_locked_down(LOCKDOWN_TRACEFS)) { |
Stephen Rothwell | ee19545 | 2019-12-06 09:25:03 +1100 | [diff] [blame] | 5816 | pr_warn("Lockdown is enabled, skipping ring buffer tests\n"); |
Steven Rostedt (VMware) | a356646 | 2019-12-02 16:25:27 -0500 | [diff] [blame] | 5817 | return 0; |
| 5818 | } |
| 5819 | |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5820 | pr_info("Running ring buffer tests...\n"); |
| 5821 | |
| 5822 | buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE); |
| 5823 | if (WARN_ON(!buffer)) |
| 5824 | return 0; |
| 5825 | |
| 5826 | /* Disable buffer so that threads can't write to it yet */ |
| 5827 | ring_buffer_record_off(buffer); |
| 5828 | |
| 5829 | for_each_online_cpu(cpu) { |
| 5830 | rb_data[cpu].buffer = buffer; |
| 5831 | rb_data[cpu].cpu = cpu; |
| 5832 | rb_data[cpu].cnt = cpu; |
| 5833 | rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu], |
| 5834 | "rbtester/%d", cpu); |
Wei Yongjun | 62277de | 2016-06-17 17:33:59 +0000 | [diff] [blame] | 5835 | if (WARN_ON(IS_ERR(rb_threads[cpu]))) { |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5836 | pr_cont("FAILED\n"); |
Wei Yongjun | 62277de | 2016-06-17 17:33:59 +0000 | [diff] [blame] | 5837 | ret = PTR_ERR(rb_threads[cpu]); |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5838 | goto out_free; |
| 5839 | } |
| 5840 | |
| 5841 | kthread_bind(rb_threads[cpu], cpu); |
| 5842 | wake_up_process(rb_threads[cpu]); |
| 5843 | } |
| 5844 | |
| 5845 | /* Now create the rb hammer! */ |
| 5846 | rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer"); |
Wei Yongjun | 62277de | 2016-06-17 17:33:59 +0000 | [diff] [blame] | 5847 | if (WARN_ON(IS_ERR(rb_hammer))) { |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5848 | pr_cont("FAILED\n"); |
Wei Yongjun | 62277de | 2016-06-17 17:33:59 +0000 | [diff] [blame] | 5849 | ret = PTR_ERR(rb_hammer); |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5850 | goto out_free; |
| 5851 | } |
| 5852 | |
| 5853 | ring_buffer_record_on(buffer); |
| 5854 | /* |
| 5855 | * Show buffer is enabled before setting rb_test_started. |
| 5856 | * Yes there's a small race window where events could be |
| 5857 | * dropped and the thread wont catch it. But when a ring |
| 5858 | * buffer gets enabled, there will always be some kind of |
| 5859 | * delay before other CPUs see it. Thus, we don't care about |
| 5860 | * those dropped events. We care about events dropped after |
| 5861 | * the threads see that the buffer is active. |
| 5862 | */ |
| 5863 | smp_wmb(); |
| 5864 | rb_test_started = true; |
| 5865 | |
| 5866 | set_current_state(TASK_INTERRUPTIBLE); |
| 5867 | /* Just run for 10 seconds */; |
| 5868 | schedule_timeout(10 * HZ); |
| 5869 | |
| 5870 | kthread_stop(rb_hammer); |
| 5871 | |
| 5872 | out_free: |
| 5873 | for_each_online_cpu(cpu) { |
| 5874 | if (!rb_threads[cpu]) |
| 5875 | break; |
| 5876 | kthread_stop(rb_threads[cpu]); |
| 5877 | } |
| 5878 | if (ret) { |
| 5879 | ring_buffer_free(buffer); |
| 5880 | return ret; |
| 5881 | } |
| 5882 | |
| 5883 | /* Report! */ |
| 5884 | pr_info("finished\n"); |
| 5885 | for_each_online_cpu(cpu) { |
| 5886 | struct ring_buffer_event *event; |
| 5887 | struct rb_test_data *data = &rb_data[cpu]; |
| 5888 | struct rb_item *item; |
| 5889 | unsigned long total_events; |
| 5890 | unsigned long total_dropped; |
| 5891 | unsigned long total_written; |
| 5892 | unsigned long total_alloc; |
| 5893 | unsigned long total_read = 0; |
| 5894 | unsigned long total_size = 0; |
| 5895 | unsigned long total_len = 0; |
| 5896 | unsigned long total_lost = 0; |
| 5897 | unsigned long lost; |
| 5898 | int big_event_size; |
| 5899 | int small_event_size; |
| 5900 | |
| 5901 | ret = -1; |
| 5902 | |
| 5903 | total_events = data->events + data->events_nested; |
| 5904 | total_written = data->bytes_written + data->bytes_written_nested; |
| 5905 | total_alloc = data->bytes_alloc + data->bytes_alloc_nested; |
| 5906 | total_dropped = data->bytes_dropped + data->bytes_dropped_nested; |
| 5907 | |
| 5908 | big_event_size = data->max_size + data->max_size_nested; |
| 5909 | small_event_size = data->min_size + data->min_size_nested; |
| 5910 | |
| 5911 | pr_info("CPU %d:\n", cpu); |
| 5912 | pr_info(" events: %ld\n", total_events); |
| 5913 | pr_info(" dropped bytes: %ld\n", total_dropped); |
| 5914 | pr_info(" alloced bytes: %ld\n", total_alloc); |
| 5915 | pr_info(" written bytes: %ld\n", total_written); |
| 5916 | pr_info(" biggest event: %d\n", big_event_size); |
| 5917 | pr_info(" smallest event: %d\n", small_event_size); |
| 5918 | |
| 5919 | if (RB_WARN_ON(buffer, total_dropped)) |
| 5920 | break; |
| 5921 | |
| 5922 | ret = 0; |
| 5923 | |
| 5924 | while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) { |
| 5925 | total_lost += lost; |
| 5926 | item = ring_buffer_event_data(event); |
| 5927 | total_len += ring_buffer_event_length(event); |
| 5928 | total_size += item->size + sizeof(struct rb_item); |
| 5929 | if (memcmp(&item->str[0], rb_string, item->size) != 0) { |
| 5930 | pr_info("FAILED!\n"); |
| 5931 | pr_info("buffer had: %.*s\n", item->size, item->str); |
| 5932 | pr_info("expected: %.*s\n", item->size, rb_string); |
| 5933 | RB_WARN_ON(buffer, 1); |
| 5934 | ret = -1; |
| 5935 | break; |
| 5936 | } |
| 5937 | total_read++; |
| 5938 | } |
| 5939 | if (ret) |
| 5940 | break; |
| 5941 | |
| 5942 | ret = -1; |
| 5943 | |
| 5944 | pr_info(" read events: %ld\n", total_read); |
| 5945 | pr_info(" lost events: %ld\n", total_lost); |
| 5946 | pr_info(" total events: %ld\n", total_lost + total_read); |
| 5947 | pr_info(" recorded len bytes: %ld\n", total_len); |
| 5948 | pr_info(" recorded size bytes: %ld\n", total_size); |
| 5949 | if (total_lost) |
| 5950 | pr_info(" With dropped events, record len and size may not match\n" |
| 5951 | " alloced and written from above\n"); |
| 5952 | if (!total_lost) { |
| 5953 | if (RB_WARN_ON(buffer, total_len != total_alloc || |
| 5954 | total_size != total_written)) |
| 5955 | break; |
| 5956 | } |
| 5957 | if (RB_WARN_ON(buffer, total_lost + total_read != total_events)) |
| 5958 | break; |
| 5959 | |
| 5960 | ret = 0; |
| 5961 | } |
| 5962 | if (!ret) |
| 5963 | pr_info("Ring buffer PASSED!\n"); |
| 5964 | |
| 5965 | ring_buffer_free(buffer); |
| 5966 | return 0; |
| 5967 | } |
| 5968 | |
| 5969 | late_initcall(test_ringbuffer); |
| 5970 | #endif /* CONFIG_RING_BUFFER_STARTUP_TEST */ |