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 (Red Hat) | af658dc | 2015-04-29 14:36:05 -0400 | [diff] [blame] | 7 | #include <linux/trace_events.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 8 | #include <linux/ring_buffer.h> |
Ingo Molnar | 14131f2 | 2009-02-26 18:47:11 +0100 | [diff] [blame] | 9 | #include <linux/trace_clock.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 10 | #include <linux/sched/clock.h> |
Steven Rostedt | 0b07436 | 2013-01-22 16:58:30 -0500 | [diff] [blame] | 11 | #include <linux/trace_seq.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 12 | #include <linux/spinlock.h> |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 13 | #include <linux/irq_work.h> |
Steven Rostedt (VMware) | a356646 | 2019-12-02 16:25:27 -0500 | [diff] [blame] | 14 | #include <linux/security.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 15 | #include <linux/uaccess.h> |
Steven Rostedt | a81bd80 | 2009-02-06 01:45:16 -0500 | [diff] [blame] | 16 | #include <linux/hardirq.h> |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 17 | #include <linux/kthread.h> /* for self test */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/percpu.h> |
| 20 | #include <linux/mutex.h> |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 21 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/hash.h> |
| 25 | #include <linux/list.h> |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 26 | #include <linux/cpu.h> |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 27 | #include <linux/oom.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 28 | |
Christoph Lameter | 7961576 | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 29 | #include <asm/local.h> |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 30 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 31 | static void update_pages_handler(struct work_struct *work); |
| 32 | |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 33 | /* |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 34 | * The ring buffer header is special. We must manually up keep it. |
| 35 | */ |
| 36 | int ring_buffer_print_entry_header(struct trace_seq *s) |
| 37 | { |
Steven Rostedt (Red Hat) | c0cd93a | 2014-11-12 11:49:00 -0500 | [diff] [blame] | 38 | trace_seq_puts(s, "# compressed entry header\n"); |
| 39 | trace_seq_puts(s, "\ttype_len : 5 bits\n"); |
| 40 | trace_seq_puts(s, "\ttime_delta : 27 bits\n"); |
| 41 | trace_seq_puts(s, "\tarray : 32 bits\n"); |
| 42 | trace_seq_putc(s, '\n'); |
| 43 | trace_seq_printf(s, "\tpadding : type == %d\n", |
| 44 | RINGBUF_TYPE_PADDING); |
| 45 | trace_seq_printf(s, "\ttime_extend : type == %d\n", |
| 46 | RINGBUF_TYPE_TIME_EXTEND); |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 47 | trace_seq_printf(s, "\ttime_stamp : type == %d\n", |
| 48 | RINGBUF_TYPE_TIME_STAMP); |
Steven Rostedt (Red Hat) | c0cd93a | 2014-11-12 11:49:00 -0500 | [diff] [blame] | 49 | trace_seq_printf(s, "\tdata max type_len == %d\n", |
| 50 | RINGBUF_TYPE_DATA_TYPE_LEN_MAX); |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 51 | |
Steven Rostedt (Red Hat) | c0cd93a | 2014-11-12 11:49:00 -0500 | [diff] [blame] | 52 | return !trace_seq_has_overflowed(s); |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
Steven Rostedt | 5cc9854 | 2009-03-12 22:24:17 -0400 | [diff] [blame] | 56 | * The ring buffer is made up of a list of pages. A separate list of pages is |
| 57 | * allocated for each CPU. A writer may only write to a buffer that is |
| 58 | * associated with the CPU it is currently executing on. A reader may read |
| 59 | * from any per cpu buffer. |
| 60 | * |
| 61 | * The reader is special. For each per cpu buffer, the reader has its own |
| 62 | * reader page. When a reader has read the entire reader page, this reader |
| 63 | * page is swapped with another page in the ring buffer. |
| 64 | * |
| 65 | * Now, as long as the writer is off the reader page, the reader can do what |
| 66 | * ever it wants with that page. The writer will never write to that page |
| 67 | * again (as long as it is out of the ring buffer). |
| 68 | * |
| 69 | * Here's some silly ASCII art. |
| 70 | * |
| 71 | * +------+ |
| 72 | * |reader| RING BUFFER |
| 73 | * |page | |
| 74 | * +------+ +---+ +---+ +---+ |
| 75 | * | |-->| |-->| | |
| 76 | * +---+ +---+ +---+ |
| 77 | * ^ | |
| 78 | * | | |
| 79 | * +---------------+ |
| 80 | * |
| 81 | * |
| 82 | * +------+ |
| 83 | * |reader| RING BUFFER |
| 84 | * |page |------------------v |
| 85 | * +------+ +---+ +---+ +---+ |
| 86 | * | |-->| |-->| | |
| 87 | * +---+ +---+ +---+ |
| 88 | * ^ | |
| 89 | * | | |
| 90 | * +---------------+ |
| 91 | * |
| 92 | * |
| 93 | * +------+ |
| 94 | * |reader| RING BUFFER |
| 95 | * |page |------------------v |
| 96 | * +------+ +---+ +---+ +---+ |
| 97 | * ^ | |-->| |-->| | |
| 98 | * | +---+ +---+ +---+ |
| 99 | * | | |
| 100 | * | | |
| 101 | * +------------------------------+ |
| 102 | * |
| 103 | * |
| 104 | * +------+ |
| 105 | * |buffer| RING BUFFER |
| 106 | * |page |------------------v |
| 107 | * +------+ +---+ +---+ +---+ |
| 108 | * ^ | | | |-->| | |
| 109 | * | New +---+ +---+ +---+ |
| 110 | * | Reader------^ | |
| 111 | * | page | |
| 112 | * +------------------------------+ |
| 113 | * |
| 114 | * |
| 115 | * After we make this swap, the reader can hand this page off to the splice |
| 116 | * code and be done with it. It can even allocate a new page if it needs to |
| 117 | * and swap that into the ring buffer. |
| 118 | * |
| 119 | * We will be using cmpxchg soon to make all this lockless. |
| 120 | * |
| 121 | */ |
| 122 | |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 123 | /* Used for individual buffers (after the counter) */ |
| 124 | #define RB_BUFFER_OFF (1 << 20) |
| 125 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 126 | #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data) |
| 127 | |
Steven Rostedt | e3d6bf0 | 2009-03-03 13:53:07 -0500 | [diff] [blame] | 128 | #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array)) |
Andrew Morton | 67d3472 | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 129 | #define RB_ALIGNMENT 4U |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 130 | #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] | 131 | #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */ |
Steven Rostedt (VMware) | 86b3de6 | 2019-05-28 09:36:19 -0400 | [diff] [blame] | 132 | #define RB_ALIGN_DATA __aligned(RB_ALIGNMENT) |
James Hogan | 649508f | 2012-05-30 12:11:19 +0100 | [diff] [blame] | 133 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 134 | /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */ |
| 135 | #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 136 | |
| 137 | enum { |
| 138 | RB_LEN_TIME_EXTEND = 8, |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 139 | RB_LEN_TIME_STAMP = 8, |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 140 | }; |
| 141 | |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 142 | #define skip_time_extend(event) \ |
| 143 | ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND)) |
| 144 | |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 145 | #define extended_time(event) \ |
| 146 | (event->type_len >= RINGBUF_TYPE_TIME_EXTEND) |
| 147 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 148 | static inline int rb_null_event(struct ring_buffer_event *event) |
| 149 | { |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 150 | return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void rb_event_set_padding(struct ring_buffer_event *event) |
| 154 | { |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 155 | /* padding has a NULL time_delta */ |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 156 | event->type_len = RINGBUF_TYPE_PADDING; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 157 | event->time_delta = 0; |
| 158 | } |
| 159 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 160 | static unsigned |
| 161 | rb_event_data_length(struct ring_buffer_event *event) |
| 162 | { |
| 163 | unsigned length; |
| 164 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 165 | if (event->type_len) |
| 166 | length = event->type_len * RB_ALIGNMENT; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 167 | else |
| 168 | length = event->array[0]; |
| 169 | return length + RB_EVNT_HDR_SIZE; |
| 170 | } |
| 171 | |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 172 | /* |
| 173 | * Return the length of the given event. Will return |
| 174 | * the length of the time extend if the event is a |
| 175 | * time extend. |
| 176 | */ |
| 177 | static inline unsigned |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 178 | rb_event_length(struct ring_buffer_event *event) |
| 179 | { |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 180 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 181 | case RINGBUF_TYPE_PADDING: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 182 | if (rb_null_event(event)) |
| 183 | /* undefined */ |
| 184 | return -1; |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 185 | return event->array[0] + RB_EVNT_HDR_SIZE; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 186 | |
| 187 | case RINGBUF_TYPE_TIME_EXTEND: |
| 188 | return RB_LEN_TIME_EXTEND; |
| 189 | |
| 190 | case RINGBUF_TYPE_TIME_STAMP: |
| 191 | return RB_LEN_TIME_STAMP; |
| 192 | |
| 193 | case RINGBUF_TYPE_DATA: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 194 | return rb_event_data_length(event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 195 | default: |
Steven Rostedt (VMware) | da4d401 | 2020-05-13 15:36:22 -0400 | [diff] [blame] | 196 | WARN_ON_ONCE(1); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 197 | } |
| 198 | /* not hit */ |
| 199 | return 0; |
| 200 | } |
| 201 | |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 202 | /* |
| 203 | * Return total length of time extend and data, |
| 204 | * or just the event length for all other events. |
| 205 | */ |
| 206 | static inline unsigned |
| 207 | rb_event_ts_length(struct ring_buffer_event *event) |
| 208 | { |
| 209 | unsigned len = 0; |
| 210 | |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 211 | if (extended_time(event)) { |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 212 | /* time extends include the data event after it */ |
| 213 | len = RB_LEN_TIME_EXTEND; |
| 214 | event = skip_time_extend(event); |
| 215 | } |
| 216 | return len + rb_event_length(event); |
| 217 | } |
| 218 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 219 | /** |
| 220 | * ring_buffer_event_length - return the length of the event |
| 221 | * @event: the event to get the length of |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 222 | * |
| 223 | * Returns the size of the data load of a data event. |
| 224 | * If the event is something other than a data event, it |
| 225 | * returns the size of the event itself. With the exception |
| 226 | * of a TIME EXTEND, where it still returns the size of the |
| 227 | * data load of the data event after it. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 228 | */ |
| 229 | unsigned ring_buffer_event_length(struct ring_buffer_event *event) |
| 230 | { |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 231 | unsigned length; |
| 232 | |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 233 | if (extended_time(event)) |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 234 | event = skip_time_extend(event); |
| 235 | |
| 236 | length = rb_event_length(event); |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 237 | if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
Robert Richter | 465634a | 2009-01-07 15:32:11 +0100 | [diff] [blame] | 238 | return length; |
| 239 | length -= RB_EVNT_HDR_SIZE; |
| 240 | if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0])) |
| 241 | length -= sizeof(event->array[0]); |
| 242 | return length; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 243 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 244 | EXPORT_SYMBOL_GPL(ring_buffer_event_length); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 245 | |
| 246 | /* inline for ring buffer fast paths */ |
Steven Rostedt (Red Hat) | 929ddbf | 2016-11-23 11:40:34 -0500 | [diff] [blame] | 247 | static __always_inline void * |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 248 | rb_event_data(struct ring_buffer_event *event) |
| 249 | { |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 250 | if (extended_time(event)) |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 251 | event = skip_time_extend(event); |
Steven Rostedt (VMware) | da4d401 | 2020-05-13 15:36:22 -0400 | [diff] [blame] | 252 | WARN_ON_ONCE(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 253 | /* If length is in len field, then array[0] has the data */ |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 254 | if (event->type_len) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 255 | return (void *)&event->array[0]; |
| 256 | /* Otherwise length is in array[0] and array[1] has the data */ |
| 257 | return (void *)&event->array[1]; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * ring_buffer_event_data - return the data of the event |
| 262 | * @event: the event to get the data from |
| 263 | */ |
| 264 | void *ring_buffer_event_data(struct ring_buffer_event *event) |
| 265 | { |
| 266 | return rb_event_data(event); |
| 267 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 268 | EXPORT_SYMBOL_GPL(ring_buffer_event_data); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 269 | |
| 270 | #define for_each_buffer_cpu(buffer, cpu) \ |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 271 | for_each_cpu(cpu, buffer->cpumask) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 272 | |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame^] | 273 | #define for_each_online_buffer_cpu(buffer, cpu) \ |
| 274 | for_each_cpu_and(cpu, buffer->cpumask, cpu_online_mask) |
| 275 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 276 | #define TS_SHIFT 27 |
| 277 | #define TS_MASK ((1ULL << TS_SHIFT) - 1) |
| 278 | #define TS_DELTA_TEST (~TS_MASK) |
| 279 | |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 280 | /** |
| 281 | * ring_buffer_event_time_stamp - return the event's extended timestamp |
| 282 | * @event: the event to get the timestamp of |
| 283 | * |
| 284 | * Returns the extended timestamp associated with a data event. |
| 285 | * An extended time_stamp is a 64-bit timestamp represented |
| 286 | * internally in a special way that makes the best use of space |
| 287 | * contained within a ring buffer event. This function decodes |
| 288 | * it and maps it to a straight u64 value. |
| 289 | */ |
| 290 | u64 ring_buffer_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; |
| 419 | unsigned long length; |
| 420 | struct buffer_page *tail_page; |
| 421 | int add_timestamp; |
| 422 | }; |
| 423 | |
| 424 | /* |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 425 | * Used for the add_timestamp |
| 426 | * NONE |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 427 | * EXTEND - wants a time extend |
| 428 | * ABSOLUTE - the buffer requests all events to have absolute time stamps |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 429 | * FORCE - force a full time stamp. |
| 430 | */ |
| 431 | enum { |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 432 | RB_ADD_STAMP_NONE = 0, |
| 433 | RB_ADD_STAMP_EXTEND = BIT(1), |
| 434 | RB_ADD_STAMP_ABSOLUTE = BIT(2), |
| 435 | RB_ADD_STAMP_FORCE = BIT(3) |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 436 | }; |
| 437 | /* |
Steven Rostedt (Red Hat) | a497adb | 2015-05-29 10:32:28 -0400 | [diff] [blame] | 438 | * Used for which event context the event is in. |
| 439 | * NMI = 0 |
| 440 | * IRQ = 1 |
| 441 | * SOFTIRQ = 2 |
| 442 | * NORMAL = 3 |
| 443 | * |
| 444 | * See trace_recursive_lock() comment below for more details. |
| 445 | */ |
| 446 | enum { |
| 447 | RB_CTX_NMI, |
| 448 | RB_CTX_IRQ, |
| 449 | RB_CTX_SOFTIRQ, |
| 450 | RB_CTX_NORMAL, |
| 451 | RB_CTX_MAX |
| 452 | }; |
| 453 | |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 454 | #if BITS_PER_LONG == 32 |
| 455 | #define RB_TIME_32 |
| 456 | #endif |
| 457 | |
| 458 | /* To test on 64 bit machines */ |
| 459 | //#define RB_TIME_32 |
| 460 | |
| 461 | #ifdef RB_TIME_32 |
| 462 | |
| 463 | struct rb_time_struct { |
| 464 | local_t cnt; |
| 465 | local_t top; |
| 466 | local_t bottom; |
| 467 | }; |
| 468 | #else |
| 469 | #include <asm/local64.h> |
| 470 | struct rb_time_struct { |
| 471 | local64_t time; |
| 472 | }; |
| 473 | #endif |
| 474 | typedef struct rb_time_struct rb_time_t; |
| 475 | |
Steven Rostedt (Red Hat) | a497adb | 2015-05-29 10:32:28 -0400 | [diff] [blame] | 476 | /* |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 477 | * head_page == tail_page && head == tail then buffer is empty. |
| 478 | */ |
| 479 | struct ring_buffer_per_cpu { |
| 480 | int cpu; |
Richard Kennedy | 985023d | 2010-03-25 11:27:36 +0000 | [diff] [blame] | 481 | atomic_t record_disabled; |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 482 | atomic_t resize_disabled; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 483 | struct trace_buffer *buffer; |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 484 | raw_spinlock_t reader_lock; /* serialize readers */ |
Thomas Gleixner | 445c895 | 2009-12-02 19:49:50 +0100 | [diff] [blame] | 485 | arch_spinlock_t lock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 486 | struct lock_class_key lock_key; |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 487 | struct buffer_data_page *free_page; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 488 | unsigned long nr_pages; |
Steven Rostedt (Red Hat) | 58a09ec | 2015-05-27 10:27:47 -0400 | [diff] [blame] | 489 | unsigned int current_context; |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 490 | struct list_head *pages; |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 491 | struct buffer_page *head_page; /* read from head */ |
| 492 | struct buffer_page *tail_page; /* write to tail */ |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 493 | struct buffer_page *commit_page; /* committed pages */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 494 | struct buffer_page *reader_page; |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 495 | unsigned long lost_events; |
| 496 | unsigned long last_overrun; |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 497 | unsigned long nest; |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 498 | local_t entries_bytes; |
Steven Rostedt | e4906ef | 2009-04-30 20:49:44 -0400 | [diff] [blame] | 499 | local_t entries; |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 500 | local_t overrun; |
| 501 | local_t commit_overrun; |
| 502 | local_t dropped_events; |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 503 | local_t committing; |
| 504 | local_t commits; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 505 | local_t pages_touched; |
| 506 | local_t pages_read; |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 507 | long last_pages_touch; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 508 | size_t shortest_full; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 509 | unsigned long read; |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 510 | unsigned long read_bytes; |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 511 | rb_time_t write_stamp; |
| 512 | rb_time_t before_stamp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 513 | u64 read_stamp; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 514 | /* 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] | 515 | long nr_pages_to_update; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 516 | struct list_head new_pages; /* new pages to add */ |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 517 | struct work_struct update_pages_work; |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 518 | struct completion update_done; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 519 | |
| 520 | struct rb_irq_work irq_work; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 521 | }; |
| 522 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 523 | struct trace_buffer { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 524 | unsigned flags; |
| 525 | int cpus; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 526 | atomic_t record_disabled; |
Arnaldo Carvalho de Melo | 00f62f6 | 2009-02-09 17:04:06 -0200 | [diff] [blame] | 527 | cpumask_var_t cpumask; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 528 | |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 529 | struct lock_class_key *reader_lock_key; |
| 530 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 531 | struct mutex mutex; |
| 532 | |
| 533 | struct ring_buffer_per_cpu **buffers; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 534 | |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 535 | struct hlist_node node; |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 536 | u64 (*clock)(void); |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 537 | |
| 538 | struct rb_irq_work irq_work; |
Tom Zanussi | 00b4145 | 2018-01-15 20:51:39 -0600 | [diff] [blame] | 539 | bool time_stamp_abs; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 540 | }; |
| 541 | |
| 542 | struct ring_buffer_iter { |
| 543 | struct ring_buffer_per_cpu *cpu_buffer; |
| 544 | unsigned long head; |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 545 | unsigned long next_event; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 546 | struct buffer_page *head_page; |
Steven Rostedt | 492a74f | 2010-01-25 15:17:47 -0500 | [diff] [blame] | 547 | struct buffer_page *cache_reader_page; |
| 548 | unsigned long cache_read; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 549 | u64 read_stamp; |
Steven Rostedt (VMware) | 28e3fc5 | 2020-03-17 17:32:26 -0400 | [diff] [blame] | 550 | u64 page_stamp; |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 551 | struct ring_buffer_event *event; |
Steven Rostedt (VMware) | c9b7a4a | 2020-03-17 17:32:32 -0400 | [diff] [blame] | 552 | int missed_events; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 553 | }; |
| 554 | |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 555 | #ifdef RB_TIME_32 |
| 556 | |
| 557 | /* |
| 558 | * On 32 bit machines, local64_t is very expensive. As the ring |
| 559 | * buffer doesn't need all the features of a true 64 bit atomic, |
| 560 | * on 32 bit, it uses these functions (64 still uses local64_t). |
| 561 | * |
| 562 | * For the ring buffer, 64 bit required operations for the time is |
| 563 | * the following: |
| 564 | * |
| 565 | * - Only need 59 bits (uses 60 to make it even). |
| 566 | * - Reads may fail if it interrupted a modification of the time stamp. |
| 567 | * It will succeed if it did not interrupt another write even if |
| 568 | * the read itself is interrupted by a write. |
| 569 | * It returns whether it was successful or not. |
| 570 | * |
| 571 | * - Writes always succeed and will overwrite other writes and writes |
| 572 | * that were done by events interrupting the current write. |
| 573 | * |
| 574 | * - A write followed by a read of the same time stamp will always succeed, |
| 575 | * but may not contain the same value. |
| 576 | * |
| 577 | * - A cmpxchg will fail if it interrupted another write or cmpxchg. |
| 578 | * Other than that, it acts like a normal cmpxchg. |
| 579 | * |
| 580 | * The 60 bit time stamp is broken up by 30 bits in a top and bottom half |
| 581 | * (bottom being the least significant 30 bits of the 60 bit time stamp). |
| 582 | * |
| 583 | * The two most significant bits of each half holds a 2 bit counter (0-3). |
| 584 | * Each update will increment this counter by one. |
| 585 | * When reading the top and bottom, if the two counter bits match then the |
| 586 | * top and bottom together make a valid 60 bit number. |
| 587 | */ |
| 588 | #define RB_TIME_SHIFT 30 |
| 589 | #define RB_TIME_VAL_MASK ((1 << RB_TIME_SHIFT) - 1) |
| 590 | |
| 591 | static inline int rb_time_cnt(unsigned long val) |
| 592 | { |
| 593 | return (val >> RB_TIME_SHIFT) & 3; |
| 594 | } |
| 595 | |
| 596 | static inline u64 rb_time_val(unsigned long top, unsigned long bottom) |
| 597 | { |
| 598 | u64 val; |
| 599 | |
| 600 | val = top & RB_TIME_VAL_MASK; |
| 601 | val <<= RB_TIME_SHIFT; |
| 602 | val |= bottom & RB_TIME_VAL_MASK; |
| 603 | |
| 604 | return val; |
| 605 | } |
| 606 | |
| 607 | static inline bool __rb_time_read(rb_time_t *t, u64 *ret, unsigned long *cnt) |
| 608 | { |
| 609 | unsigned long top, bottom; |
| 610 | unsigned long c; |
| 611 | |
| 612 | /* |
| 613 | * If the read is interrupted by a write, then the cnt will |
| 614 | * be different. Loop until both top and bottom have been read |
| 615 | * without interruption. |
| 616 | */ |
| 617 | do { |
| 618 | c = local_read(&t->cnt); |
| 619 | top = local_read(&t->top); |
| 620 | bottom = local_read(&t->bottom); |
| 621 | } while (c != local_read(&t->cnt)); |
| 622 | |
| 623 | *cnt = rb_time_cnt(top); |
| 624 | |
| 625 | /* If top and bottom counts don't match, this interrupted a write */ |
| 626 | if (*cnt != rb_time_cnt(bottom)) |
| 627 | return false; |
| 628 | |
| 629 | *ret = rb_time_val(top, bottom); |
| 630 | return true; |
| 631 | } |
| 632 | |
| 633 | static bool rb_time_read(rb_time_t *t, u64 *ret) |
| 634 | { |
| 635 | unsigned long cnt; |
| 636 | |
| 637 | return __rb_time_read(t, ret, &cnt); |
| 638 | } |
| 639 | |
| 640 | static inline unsigned long rb_time_val_cnt(unsigned long val, unsigned long cnt) |
| 641 | { |
| 642 | return (val & RB_TIME_VAL_MASK) | ((cnt & 3) << RB_TIME_SHIFT); |
| 643 | } |
| 644 | |
| 645 | static inline void rb_time_split(u64 val, unsigned long *top, unsigned long *bottom) |
| 646 | { |
| 647 | *top = (unsigned long)((val >> RB_TIME_SHIFT) & RB_TIME_VAL_MASK); |
| 648 | *bottom = (unsigned long)(val & RB_TIME_VAL_MASK); |
| 649 | } |
| 650 | |
| 651 | static inline void rb_time_val_set(local_t *t, unsigned long val, unsigned long cnt) |
| 652 | { |
| 653 | val = rb_time_val_cnt(val, cnt); |
| 654 | local_set(t, val); |
| 655 | } |
| 656 | |
| 657 | static void rb_time_set(rb_time_t *t, u64 val) |
| 658 | { |
| 659 | unsigned long cnt, top, bottom; |
| 660 | |
| 661 | rb_time_split(val, &top, &bottom); |
| 662 | |
| 663 | /* Writes always succeed with a valid number even if it gets interrupted. */ |
| 664 | do { |
| 665 | cnt = local_inc_return(&t->cnt); |
| 666 | rb_time_val_set(&t->top, top, cnt); |
| 667 | rb_time_val_set(&t->bottom, bottom, cnt); |
| 668 | } while (cnt != local_read(&t->cnt)); |
| 669 | } |
| 670 | |
| 671 | static inline bool |
| 672 | rb_time_read_cmpxchg(local_t *l, unsigned long expect, unsigned long set) |
| 673 | { |
| 674 | unsigned long ret; |
| 675 | |
| 676 | ret = local_cmpxchg(l, expect, set); |
| 677 | return ret == expect; |
| 678 | } |
| 679 | |
| 680 | static int rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set) |
| 681 | { |
| 682 | unsigned long cnt, top, bottom; |
| 683 | unsigned long cnt2, top2, bottom2; |
| 684 | u64 val; |
| 685 | |
| 686 | /* The cmpxchg always fails if it interrupted an update */ |
| 687 | if (!__rb_time_read(t, &val, &cnt2)) |
| 688 | return false; |
| 689 | |
| 690 | if (val != expect) |
| 691 | return false; |
| 692 | |
| 693 | cnt = local_read(&t->cnt); |
| 694 | if ((cnt & 3) != cnt2) |
| 695 | return false; |
| 696 | |
| 697 | cnt2 = cnt + 1; |
| 698 | |
| 699 | rb_time_split(val, &top, &bottom); |
| 700 | top = rb_time_val_cnt(top, cnt); |
| 701 | bottom = rb_time_val_cnt(bottom, cnt); |
| 702 | |
| 703 | rb_time_split(set, &top2, &bottom2); |
| 704 | top2 = rb_time_val_cnt(top2, cnt2); |
| 705 | bottom2 = rb_time_val_cnt(bottom2, cnt2); |
| 706 | |
| 707 | if (!rb_time_read_cmpxchg(&t->cnt, cnt, cnt2)) |
| 708 | return false; |
| 709 | if (!rb_time_read_cmpxchg(&t->top, top, top2)) |
| 710 | return false; |
| 711 | if (!rb_time_read_cmpxchg(&t->bottom, bottom, bottom2)) |
| 712 | return false; |
| 713 | return true; |
| 714 | } |
| 715 | |
| 716 | #else /* 64 bits */ |
| 717 | |
| 718 | /* local64_t always succeeds */ |
| 719 | |
| 720 | static inline bool rb_time_read(rb_time_t *t, u64 *ret) |
| 721 | { |
| 722 | *ret = local64_read(&t->time); |
| 723 | return true; |
| 724 | } |
| 725 | static void rb_time_set(rb_time_t *t, u64 val) |
| 726 | { |
| 727 | local64_set(&t->time, val); |
| 728 | } |
| 729 | |
| 730 | static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set) |
| 731 | { |
| 732 | u64 val; |
| 733 | val = local64_cmpxchg(&t->time, expect, set); |
| 734 | return val == expect; |
| 735 | } |
| 736 | #endif |
| 737 | |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 738 | /** |
| 739 | * ring_buffer_nr_pages - get the number of buffer pages in the ring buffer |
| 740 | * @buffer: The ring_buffer to get the number of pages from |
| 741 | * @cpu: The cpu of the ring_buffer to get the number of pages from |
| 742 | * |
| 743 | * Returns the number of pages used by a per_cpu buffer of the ring buffer. |
| 744 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 745 | 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] | 746 | { |
| 747 | return buffer->buffers[cpu]->nr_pages; |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * ring_buffer_nr_pages_dirty - get the number of used pages in the ring buffer |
| 752 | * @buffer: The ring_buffer to get the number of pages from |
| 753 | * @cpu: The cpu of the ring_buffer to get the number of pages from |
| 754 | * |
| 755 | * Returns the number of pages that have content in the ring buffer. |
| 756 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 757 | 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] | 758 | { |
| 759 | size_t read; |
| 760 | size_t cnt; |
| 761 | |
| 762 | read = local_read(&buffer->buffers[cpu]->pages_read); |
| 763 | cnt = local_read(&buffer->buffers[cpu]->pages_touched); |
| 764 | /* The reader can read an empty page, but not more than that */ |
| 765 | if (cnt < read) { |
| 766 | WARN_ON_ONCE(read > cnt + 1); |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | return cnt - read; |
| 771 | } |
| 772 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 773 | /* |
| 774 | * rb_wake_up_waiters - wake up tasks waiting for ring buffer input |
| 775 | * |
| 776 | * Schedules a delayed work to wake up any task that is blocked on the |
| 777 | * ring buffer waiters queue. |
| 778 | */ |
| 779 | static void rb_wake_up_waiters(struct irq_work *work) |
| 780 | { |
| 781 | struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work); |
| 782 | |
| 783 | wake_up_all(&rbwork->waiters); |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 784 | if (rbwork->wakeup_full) { |
| 785 | rbwork->wakeup_full = false; |
| 786 | wake_up_all(&rbwork->full_waiters); |
| 787 | } |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /** |
| 791 | * ring_buffer_wait - wait for input to the ring buffer |
| 792 | * @buffer: buffer to wait on |
| 793 | * @cpu: the cpu buffer to wait on |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 794 | * @full: wait until a full page is available, if @cpu != RING_BUFFER_ALL_CPUS |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 795 | * |
| 796 | * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon |
| 797 | * as data is added to any of the @buffer's cpu buffers. Otherwise |
| 798 | * it will wait for data to be added to a specific cpu buffer. |
| 799 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 800 | 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] | 801 | { |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 802 | struct ring_buffer_per_cpu *uninitialized_var(cpu_buffer); |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 803 | DEFINE_WAIT(wait); |
| 804 | struct rb_irq_work *work; |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 805 | int ret = 0; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 806 | |
| 807 | /* |
| 808 | * Depending on what the caller is waiting for, either any |
| 809 | * data in any cpu buffer, or a specific buffer, put the |
| 810 | * caller on the appropriate wait queue. |
| 811 | */ |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 812 | if (cpu == RING_BUFFER_ALL_CPUS) { |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 813 | work = &buffer->irq_work; |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 814 | /* Full only makes sense on per cpu reads */ |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 815 | full = 0; |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 816 | } else { |
Steven Rostedt (Red Hat) | 8b8b368 | 2014-06-10 09:46:00 -0400 | [diff] [blame] | 817 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 818 | return -ENODEV; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 819 | cpu_buffer = buffer->buffers[cpu]; |
| 820 | work = &cpu_buffer->irq_work; |
| 821 | } |
| 822 | |
| 823 | |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 824 | while (true) { |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 825 | if (full) |
| 826 | prepare_to_wait(&work->full_waiters, &wait, TASK_INTERRUPTIBLE); |
| 827 | else |
| 828 | prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE); |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 829 | |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 830 | /* |
| 831 | * The events can happen in critical sections where |
| 832 | * checking a work queue can cause deadlocks. |
| 833 | * After adding a task to the queue, this flag is set |
| 834 | * only to notify events to try to wake up the queue |
| 835 | * using irq_work. |
| 836 | * |
| 837 | * We don't clear it even if the buffer is no longer |
| 838 | * empty. The flag only causes the next event to run |
| 839 | * irq_work to do the work queue wake up. The worse |
| 840 | * that can happen if we race with !trace_empty() is that |
| 841 | * an event will cause an irq_work to try to wake up |
| 842 | * an empty queue. |
| 843 | * |
| 844 | * There's no reason to protect this flag either, as |
| 845 | * the work queue and irq_work logic will do the necessary |
| 846 | * synchronization for the wake ups. The only thing |
| 847 | * that is necessary is that the wake up happens after |
| 848 | * a task has been queued. It's OK for spurious wake ups. |
| 849 | */ |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 850 | if (full) |
| 851 | work->full_waiters_pending = true; |
| 852 | else |
| 853 | work->waiters_pending = true; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 854 | |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 855 | if (signal_pending(current)) { |
| 856 | ret = -EINTR; |
| 857 | break; |
| 858 | } |
| 859 | |
| 860 | if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) |
| 861 | break; |
| 862 | |
| 863 | if (cpu != RING_BUFFER_ALL_CPUS && |
| 864 | !ring_buffer_empty_cpu(buffer, cpu)) { |
| 865 | unsigned long flags; |
| 866 | bool pagebusy; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 867 | size_t nr_pages; |
| 868 | size_t dirty; |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 869 | |
| 870 | if (!full) |
| 871 | break; |
| 872 | |
| 873 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 874 | pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 875 | nr_pages = cpu_buffer->nr_pages; |
| 876 | dirty = ring_buffer_nr_dirty_pages(buffer, cpu); |
| 877 | if (!cpu_buffer->shortest_full || |
| 878 | cpu_buffer->shortest_full < full) |
| 879 | cpu_buffer->shortest_full = full; |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 880 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 881 | if (!pagebusy && |
| 882 | (!nr_pages || (dirty * 100) > full * nr_pages)) |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 883 | break; |
| 884 | } |
| 885 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 886 | schedule(); |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 887 | } |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 888 | |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 889 | if (full) |
| 890 | finish_wait(&work->full_waiters, &wait); |
| 891 | else |
| 892 | finish_wait(&work->waiters, &wait); |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 893 | |
| 894 | return ret; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | /** |
| 898 | * ring_buffer_poll_wait - poll on buffer input |
| 899 | * @buffer: buffer to wait on |
| 900 | * @cpu: the cpu buffer to wait on |
| 901 | * @filp: the file descriptor |
| 902 | * @poll_table: The poll descriptor |
| 903 | * |
| 904 | * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon |
| 905 | * as data is added to any of the @buffer's cpu buffers. Otherwise |
| 906 | * it will wait for data to be added to a specific cpu buffer. |
| 907 | * |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 908 | * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers, |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 909 | * zero otherwise. |
| 910 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 911 | __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] | 912 | struct file *filp, poll_table *poll_table) |
| 913 | { |
| 914 | struct ring_buffer_per_cpu *cpu_buffer; |
| 915 | struct rb_irq_work *work; |
| 916 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 917 | if (cpu == RING_BUFFER_ALL_CPUS) |
| 918 | work = &buffer->irq_work; |
| 919 | else { |
Steven Rostedt (Red Hat) | 6721cb6 | 2013-05-23 14:21:36 -0400 | [diff] [blame] | 920 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 921 | return -EINVAL; |
| 922 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 923 | cpu_buffer = buffer->buffers[cpu]; |
| 924 | work = &cpu_buffer->irq_work; |
| 925 | } |
| 926 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 927 | poll_wait(filp, &work->waiters, poll_table); |
Josef Bacik | 4ce97db | 2014-08-25 13:59:41 -0400 | [diff] [blame] | 928 | work->waiters_pending = true; |
| 929 | /* |
| 930 | * There's a tight race between setting the waiters_pending and |
| 931 | * checking if the ring buffer is empty. Once the waiters_pending bit |
| 932 | * is set, the next event will wake the task up, but we can get stuck |
| 933 | * if there's only a single event in. |
| 934 | * |
| 935 | * FIXME: Ideally, we need a memory barrier on the writer side as well, |
| 936 | * but adding a memory barrier to all events will cause too much of a |
| 937 | * performance hit in the fast path. We only need a memory barrier when |
| 938 | * the buffer goes from empty to having content. But as this race is |
| 939 | * extremely small, and it's not a problem if another event comes in, we |
| 940 | * will fix it later. |
| 941 | */ |
| 942 | smp_mb(); |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 943 | |
| 944 | if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) || |
| 945 | (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu))) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 946 | return EPOLLIN | EPOLLRDNORM; |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 947 | return 0; |
| 948 | } |
| 949 | |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 950 | /* buffer may be either ring_buffer or ring_buffer_per_cpu */ |
Steven Rostedt | 077c540 | 2009-09-03 19:53:46 -0400 | [diff] [blame] | 951 | #define RB_WARN_ON(b, cond) \ |
| 952 | ({ \ |
| 953 | int _____ret = unlikely(cond); \ |
| 954 | if (_____ret) { \ |
| 955 | if (__same_type(*(b), struct ring_buffer_per_cpu)) { \ |
| 956 | struct ring_buffer_per_cpu *__b = \ |
| 957 | (void *)b; \ |
| 958 | atomic_inc(&__b->buffer->record_disabled); \ |
| 959 | } else \ |
| 960 | atomic_inc(&b->record_disabled); \ |
| 961 | WARN_ON(1); \ |
| 962 | } \ |
| 963 | _____ret; \ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 964 | }) |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 965 | |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 966 | /* Up this if you want to test the TIME_EXTENTS and normalization */ |
| 967 | #define DEBUG_SHIFT 0 |
| 968 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 969 | static inline u64 rb_time_stamp(struct trace_buffer *buffer) |
Steven Rostedt | 88eb012 | 2009-05-11 16:28:23 -0400 | [diff] [blame] | 970 | { |
| 971 | /* shift to debug/test normalization and TIME_EXTENTS */ |
| 972 | return buffer->clock() << DEBUG_SHIFT; |
| 973 | } |
| 974 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 975 | u64 ring_buffer_time_stamp(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 976 | { |
| 977 | u64 time; |
| 978 | |
| 979 | preempt_disable_notrace(); |
Jiri Olsa | 6d3f1e1 | 2009-10-23 19:36:19 -0400 | [diff] [blame] | 980 | time = rb_time_stamp(buffer); |
Peter Zijlstra | d6097c9 | 2019-04-23 22:03:18 +0200 | [diff] [blame] | 981 | preempt_enable_notrace(); |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 982 | |
| 983 | return time; |
| 984 | } |
| 985 | EXPORT_SYMBOL_GPL(ring_buffer_time_stamp); |
| 986 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 987 | void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer, |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 988 | int cpu, u64 *ts) |
| 989 | { |
| 990 | /* Just stupid testing the normalize function and deltas */ |
| 991 | *ts >>= DEBUG_SHIFT; |
| 992 | } |
| 993 | EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp); |
| 994 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 995 | /* |
| 996 | * Making the ring buffer lockless makes things tricky. |
| 997 | * Although writes only happen on the CPU that they are on, |
| 998 | * and they only need to worry about interrupts. Reads can |
| 999 | * happen on any CPU. |
| 1000 | * |
| 1001 | * The reader page is always off the ring buffer, but when the |
| 1002 | * reader finishes with a page, it needs to swap its page with |
| 1003 | * a new one from the buffer. The reader needs to take from |
| 1004 | * the head (writes go to the tail). But if a writer is in overwrite |
| 1005 | * mode and wraps, it must push the head page forward. |
| 1006 | * |
| 1007 | * Here lies the problem. |
| 1008 | * |
| 1009 | * The reader must be careful to replace only the head page, and |
| 1010 | * not another one. As described at the top of the file in the |
| 1011 | * ASCII art, the reader sets its old page to point to the next |
| 1012 | * page after head. It then sets the page after head to point to |
| 1013 | * the old reader page. But if the writer moves the head page |
| 1014 | * during this operation, the reader could end up with the tail. |
| 1015 | * |
| 1016 | * We use cmpxchg to help prevent this race. We also do something |
| 1017 | * special with the page before head. We set the LSB to 1. |
| 1018 | * |
| 1019 | * When the writer must push the page forward, it will clear the |
| 1020 | * bit that points to the head page, move the head, and then set |
| 1021 | * the bit that points to the new head page. |
| 1022 | * |
| 1023 | * We also don't want an interrupt coming in and moving the head |
| 1024 | * page on another writer. Thus we use the second LSB to catch |
| 1025 | * that too. Thus: |
| 1026 | * |
| 1027 | * head->list->prev->next bit 1 bit 0 |
| 1028 | * ------- ------- |
| 1029 | * Normal page 0 0 |
| 1030 | * Points to head page 0 1 |
| 1031 | * New head page 1 0 |
| 1032 | * |
| 1033 | * Note we can not trust the prev pointer of the head page, because: |
| 1034 | * |
| 1035 | * +----+ +-----+ +-----+ |
| 1036 | * | |------>| T |---X--->| N | |
| 1037 | * | |<------| | | | |
| 1038 | * +----+ +-----+ +-----+ |
| 1039 | * ^ ^ | |
| 1040 | * | +-----+ | | |
| 1041 | * +----------| R |----------+ | |
| 1042 | * | |<-----------+ |
| 1043 | * +-----+ |
| 1044 | * |
| 1045 | * Key: ---X--> HEAD flag set in pointer |
| 1046 | * T Tail page |
| 1047 | * R Reader page |
| 1048 | * N Next page |
| 1049 | * |
| 1050 | * (see __rb_reserve_next() to see where this happens) |
| 1051 | * |
| 1052 | * What the above shows is that the reader just swapped out |
| 1053 | * the reader page with a page in the buffer, but before it |
| 1054 | * could make the new header point back to the new page added |
| 1055 | * it was preempted by a writer. The writer moved forward onto |
| 1056 | * the new page added by the reader and is about to move forward |
| 1057 | * again. |
| 1058 | * |
| 1059 | * You can see, it is legitimate for the previous pointer of |
| 1060 | * 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] | 1061 | * temporarily. |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1062 | */ |
| 1063 | |
| 1064 | #define RB_PAGE_NORMAL 0UL |
| 1065 | #define RB_PAGE_HEAD 1UL |
| 1066 | #define RB_PAGE_UPDATE 2UL |
| 1067 | |
| 1068 | |
| 1069 | #define RB_FLAG_MASK 3UL |
| 1070 | |
| 1071 | /* PAGE_MOVED is not part of the mask */ |
| 1072 | #define RB_PAGE_MOVED 4UL |
| 1073 | |
| 1074 | /* |
| 1075 | * rb_list_head - remove any bit |
| 1076 | */ |
| 1077 | static struct list_head *rb_list_head(struct list_head *list) |
| 1078 | { |
| 1079 | unsigned long val = (unsigned long)list; |
| 1080 | |
| 1081 | return (struct list_head *)(val & ~RB_FLAG_MASK); |
| 1082 | } |
| 1083 | |
| 1084 | /* |
Jiri Olsa | 6d3f1e1 | 2009-10-23 19:36:19 -0400 | [diff] [blame] | 1085 | * 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] | 1086 | * |
| 1087 | * Because the reader may move the head_page pointer, we can |
| 1088 | * not trust what the head page is (it may be pointing to |
| 1089 | * the reader page). But if the next page is a header page, |
| 1090 | * its flags will be non zero. |
| 1091 | */ |
Jesper Juhl | 42b16b3 | 2011-01-17 00:09:38 +0100 | [diff] [blame] | 1092 | static inline int |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1093 | rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer, |
| 1094 | struct buffer_page *page, struct list_head *list) |
| 1095 | { |
| 1096 | unsigned long val; |
| 1097 | |
| 1098 | val = (unsigned long)list->next; |
| 1099 | |
| 1100 | if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list) |
| 1101 | return RB_PAGE_MOVED; |
| 1102 | |
| 1103 | return val & RB_FLAG_MASK; |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | * rb_is_reader_page |
| 1108 | * |
| 1109 | * The unique thing about the reader page, is that, if the |
| 1110 | * writer is ever on it, the previous pointer never points |
| 1111 | * back to the reader page. |
| 1112 | */ |
Yaowei Bai | 06ca320 | 2015-09-29 22:43:31 +0800 | [diff] [blame] | 1113 | static bool rb_is_reader_page(struct buffer_page *page) |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1114 | { |
| 1115 | struct list_head *list = page->list.prev; |
| 1116 | |
| 1117 | return rb_list_head(list->next) != &page->list; |
| 1118 | } |
| 1119 | |
| 1120 | /* |
| 1121 | * rb_set_list_to_head - set a list_head to be pointing to head. |
| 1122 | */ |
| 1123 | static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer, |
| 1124 | struct list_head *list) |
| 1125 | { |
| 1126 | unsigned long *ptr; |
| 1127 | |
| 1128 | ptr = (unsigned long *)&list->next; |
| 1129 | *ptr |= RB_PAGE_HEAD; |
| 1130 | *ptr &= ~RB_PAGE_UPDATE; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * rb_head_page_activate - sets up head page |
| 1135 | */ |
| 1136 | static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer) |
| 1137 | { |
| 1138 | struct buffer_page *head; |
| 1139 | |
| 1140 | head = cpu_buffer->head_page; |
| 1141 | if (!head) |
| 1142 | return; |
| 1143 | |
| 1144 | /* |
| 1145 | * Set the previous list pointer to have the HEAD flag. |
| 1146 | */ |
| 1147 | rb_set_list_to_head(cpu_buffer, head->list.prev); |
| 1148 | } |
| 1149 | |
| 1150 | static void rb_list_head_clear(struct list_head *list) |
| 1151 | { |
| 1152 | unsigned long *ptr = (unsigned long *)&list->next; |
| 1153 | |
| 1154 | *ptr &= ~RB_FLAG_MASK; |
| 1155 | } |
| 1156 | |
| 1157 | /* |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 1158 | * rb_head_page_deactivate - clears head page ptr (for free list) |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1159 | */ |
| 1160 | static void |
| 1161 | rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer) |
| 1162 | { |
| 1163 | struct list_head *hd; |
| 1164 | |
| 1165 | /* Go through the whole list and clear any pointers found. */ |
| 1166 | rb_list_head_clear(cpu_buffer->pages); |
| 1167 | |
| 1168 | list_for_each(hd, cpu_buffer->pages) |
| 1169 | rb_list_head_clear(hd); |
| 1170 | } |
| 1171 | |
| 1172 | static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer, |
| 1173 | struct buffer_page *head, |
| 1174 | struct buffer_page *prev, |
| 1175 | int old_flag, int new_flag) |
| 1176 | { |
| 1177 | struct list_head *list; |
| 1178 | unsigned long val = (unsigned long)&head->list; |
| 1179 | unsigned long ret; |
| 1180 | |
| 1181 | list = &prev->list; |
| 1182 | |
| 1183 | val &= ~RB_FLAG_MASK; |
| 1184 | |
Steven Rostedt | 08a4081 | 2009-09-14 09:31:35 -0400 | [diff] [blame] | 1185 | ret = cmpxchg((unsigned long *)&list->next, |
| 1186 | val | old_flag, val | new_flag); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1187 | |
| 1188 | /* check if the reader took the page */ |
| 1189 | if ((ret & ~RB_FLAG_MASK) != val) |
| 1190 | return RB_PAGE_MOVED; |
| 1191 | |
| 1192 | return ret & RB_FLAG_MASK; |
| 1193 | } |
| 1194 | |
| 1195 | static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer, |
| 1196 | struct buffer_page *head, |
| 1197 | struct buffer_page *prev, |
| 1198 | int old_flag) |
| 1199 | { |
| 1200 | return rb_head_page_set(cpu_buffer, head, prev, |
| 1201 | old_flag, RB_PAGE_UPDATE); |
| 1202 | } |
| 1203 | |
| 1204 | static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer, |
| 1205 | struct buffer_page *head, |
| 1206 | struct buffer_page *prev, |
| 1207 | int old_flag) |
| 1208 | { |
| 1209 | return rb_head_page_set(cpu_buffer, head, prev, |
| 1210 | old_flag, RB_PAGE_HEAD); |
| 1211 | } |
| 1212 | |
| 1213 | static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer, |
| 1214 | struct buffer_page *head, |
| 1215 | struct buffer_page *prev, |
| 1216 | int old_flag) |
| 1217 | { |
| 1218 | return rb_head_page_set(cpu_buffer, head, prev, |
| 1219 | old_flag, RB_PAGE_NORMAL); |
| 1220 | } |
| 1221 | |
| 1222 | static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer, |
| 1223 | struct buffer_page **bpage) |
| 1224 | { |
| 1225 | struct list_head *p = rb_list_head((*bpage)->list.next); |
| 1226 | |
| 1227 | *bpage = list_entry(p, struct buffer_page, list); |
| 1228 | } |
| 1229 | |
| 1230 | static struct buffer_page * |
| 1231 | rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer) |
| 1232 | { |
| 1233 | struct buffer_page *head; |
| 1234 | struct buffer_page *page; |
| 1235 | struct list_head *list; |
| 1236 | int i; |
| 1237 | |
| 1238 | if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page)) |
| 1239 | return NULL; |
| 1240 | |
| 1241 | /* sanity check */ |
| 1242 | list = cpu_buffer->pages; |
| 1243 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list)) |
| 1244 | return NULL; |
| 1245 | |
| 1246 | page = head = cpu_buffer->head_page; |
| 1247 | /* |
| 1248 | * It is possible that the writer moves the header behind |
| 1249 | * where we started, and we miss in one loop. |
| 1250 | * A second loop should grab the header, but we'll do |
| 1251 | * three loops just because I'm paranoid. |
| 1252 | */ |
| 1253 | for (i = 0; i < 3; i++) { |
| 1254 | do { |
| 1255 | if (rb_is_head_page(cpu_buffer, page, page->list.prev)) { |
| 1256 | cpu_buffer->head_page = page; |
| 1257 | return page; |
| 1258 | } |
| 1259 | rb_inc_page(cpu_buffer, &page); |
| 1260 | } while (page != head); |
| 1261 | } |
| 1262 | |
| 1263 | RB_WARN_ON(cpu_buffer, 1); |
| 1264 | |
| 1265 | return NULL; |
| 1266 | } |
| 1267 | |
| 1268 | static int rb_head_page_replace(struct buffer_page *old, |
| 1269 | struct buffer_page *new) |
| 1270 | { |
| 1271 | unsigned long *ptr = (unsigned long *)&old->list.prev->next; |
| 1272 | unsigned long val; |
| 1273 | unsigned long ret; |
| 1274 | |
| 1275 | val = *ptr & ~RB_FLAG_MASK; |
| 1276 | val |= RB_PAGE_HEAD; |
| 1277 | |
Steven Rostedt | 08a4081 | 2009-09-14 09:31:35 -0400 | [diff] [blame] | 1278 | ret = cmpxchg(ptr, val, (unsigned long)&new->list); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1279 | |
| 1280 | return ret == val; |
| 1281 | } |
| 1282 | |
| 1283 | /* |
| 1284 | * rb_tail_page_update - move the tail page forward |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1285 | */ |
Steven Rostedt (Red Hat) | 7000498 | 2015-11-17 15:15:19 -0500 | [diff] [blame] | 1286 | 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] | 1287 | struct buffer_page *tail_page, |
| 1288 | struct buffer_page *next_page) |
| 1289 | { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1290 | unsigned long old_entries; |
| 1291 | unsigned long old_write; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1292 | |
| 1293 | /* |
| 1294 | * The tail page now needs to be moved forward. |
| 1295 | * |
| 1296 | * We need to reset the tail page, but without messing |
| 1297 | * with possible erasing of data brought in by interrupts |
| 1298 | * that have moved the tail page and are currently on it. |
| 1299 | * |
| 1300 | * We add a counter to the write field to denote this. |
| 1301 | */ |
| 1302 | old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write); |
| 1303 | old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries); |
| 1304 | |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 1305 | local_inc(&cpu_buffer->pages_touched); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1306 | /* |
| 1307 | * Just make sure we have seen our old_write and synchronize |
| 1308 | * with any interrupts that come in. |
| 1309 | */ |
| 1310 | barrier(); |
| 1311 | |
| 1312 | /* |
| 1313 | * If the tail page is still the same as what we think |
| 1314 | * it is, then it is up to us to update the tail |
| 1315 | * pointer. |
| 1316 | */ |
Steven Rostedt (Red Hat) | 8573636 | 2015-11-17 14:03:11 -0500 | [diff] [blame] | 1317 | if (tail_page == READ_ONCE(cpu_buffer->tail_page)) { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1318 | /* Zero the write counter */ |
| 1319 | unsigned long val = old_write & ~RB_WRITE_MASK; |
| 1320 | unsigned long eval = old_entries & ~RB_WRITE_MASK; |
| 1321 | |
| 1322 | /* |
| 1323 | * This will only succeed if an interrupt did |
| 1324 | * not come in and change it. In which case, we |
| 1325 | * do not want to modify it. |
Lai Jiangshan | da706d8 | 2009-07-15 16:27:30 +0800 | [diff] [blame] | 1326 | * |
| 1327 | * We add (void) to let the compiler know that we do not care |
| 1328 | * about the return value of these functions. We use the |
| 1329 | * cmpxchg to only update if an interrupt did not already |
| 1330 | * do it for us. If the cmpxchg fails, we don't care. |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1331 | */ |
Lai Jiangshan | da706d8 | 2009-07-15 16:27:30 +0800 | [diff] [blame] | 1332 | (void)local_cmpxchg(&next_page->write, old_write, val); |
| 1333 | (void)local_cmpxchg(&next_page->entries, old_entries, eval); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1334 | |
| 1335 | /* |
| 1336 | * No need to worry about races with clearing out the commit. |
| 1337 | * it only can increment when a commit takes place. But that |
| 1338 | * only happens in the outer most nested commit. |
| 1339 | */ |
| 1340 | local_set(&next_page->page->commit, 0); |
| 1341 | |
Steven Rostedt (Red Hat) | 7000498 | 2015-11-17 15:15:19 -0500 | [diff] [blame] | 1342 | /* Again, either we update tail_page or an interrupt does */ |
| 1343 | (void)cmpxchg(&cpu_buffer->tail_page, tail_page, next_page); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1344 | } |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer, |
| 1348 | struct buffer_page *bpage) |
| 1349 | { |
| 1350 | unsigned long val = (unsigned long)bpage; |
| 1351 | |
| 1352 | if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK)) |
| 1353 | return 1; |
| 1354 | |
| 1355 | return 0; |
| 1356 | } |
| 1357 | |
| 1358 | /** |
| 1359 | * rb_check_list - make sure a pointer to a list has the last bits zero |
| 1360 | */ |
| 1361 | static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer, |
| 1362 | struct list_head *list) |
| 1363 | { |
| 1364 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev)) |
| 1365 | return 1; |
| 1366 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next)) |
| 1367 | return 1; |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1371 | /** |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 1372 | * rb_check_pages - integrity check of buffer pages |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1373 | * @cpu_buffer: CPU buffer with pages to test |
| 1374 | * |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 1375 | * 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] | 1376 | * been corrupted. |
| 1377 | */ |
| 1378 | static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer) |
| 1379 | { |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1380 | struct list_head *head = cpu_buffer->pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1381 | struct buffer_page *bpage, *tmp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1382 | |
Steven Rostedt | 308f7ee | 2012-05-16 19:46:32 -0400 | [diff] [blame] | 1383 | /* Reset the head page if it exists */ |
| 1384 | if (cpu_buffer->head_page) |
| 1385 | rb_set_head_page(cpu_buffer); |
| 1386 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1387 | rb_head_page_deactivate(cpu_buffer); |
| 1388 | |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1389 | if (RB_WARN_ON(cpu_buffer, head->next->prev != head)) |
| 1390 | return -1; |
| 1391 | if (RB_WARN_ON(cpu_buffer, head->prev->next != head)) |
| 1392 | return -1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1393 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1394 | if (rb_check_list(cpu_buffer, head)) |
| 1395 | return -1; |
| 1396 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1397 | list_for_each_entry_safe(bpage, tmp, head, list) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1398 | if (RB_WARN_ON(cpu_buffer, |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1399 | bpage->list.next->prev != &bpage->list)) |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1400 | return -1; |
| 1401 | if (RB_WARN_ON(cpu_buffer, |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1402 | bpage->list.prev->next != &bpage->list)) |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1403 | return -1; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1404 | if (rb_check_list(cpu_buffer, &bpage->list)) |
| 1405 | return -1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1406 | } |
| 1407 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1408 | rb_head_page_activate(cpu_buffer); |
| 1409 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1410 | return 0; |
| 1411 | } |
| 1412 | |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 1413 | static int __rb_allocate_pages(long nr_pages, struct list_head *pages, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1414 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1415 | struct buffer_page *bpage, *tmp; |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1416 | bool user_thread = current->mm != NULL; |
| 1417 | gfp_t mflags; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 1418 | long i; |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1419 | |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1420 | /* |
| 1421 | * Check if the available memory is there first. |
| 1422 | * Note, si_mem_available() only gives us a rough estimate of available |
| 1423 | * memory. It may not be accurate. But we don't care, we just want |
| 1424 | * to prevent doing any allocation when it is obvious that it is |
| 1425 | * not going to succeed. |
| 1426 | */ |
Steven Rostedt (VMware) | 2a872fa | 2018-04-02 10:33:56 -0400 | [diff] [blame] | 1427 | i = si_mem_available(); |
| 1428 | if (i < nr_pages) |
| 1429 | return -ENOMEM; |
| 1430 | |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1431 | /* |
| 1432 | * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails |
| 1433 | * gracefully without invoking oom-killer and the system is not |
| 1434 | * destabilized. |
| 1435 | */ |
| 1436 | mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL; |
| 1437 | |
| 1438 | /* |
| 1439 | * If a user thread allocates too much, and si_mem_available() |
| 1440 | * reports there's enough memory, even though there is not. |
| 1441 | * Make sure the OOM killer kills this thread. This can happen |
| 1442 | * even with RETRY_MAYFAIL because another task may be doing |
| 1443 | * an allocation after this task has taken all memory. |
| 1444 | * This is the task the OOM killer needs to take out during this |
| 1445 | * loop, even if it was triggered by an allocation somewhere else. |
| 1446 | */ |
| 1447 | if (user_thread) |
| 1448 | set_current_oom_origin(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1449 | for (i = 0; i < nr_pages; i++) { |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 1450 | struct page *page; |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1451 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1452 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1453 | mflags, cpu_to_node(cpu)); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1454 | if (!bpage) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1455 | goto free_pages; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1456 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1457 | list_add(&bpage->list, pages); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1458 | |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1459 | page = alloc_pages_node(cpu_to_node(cpu), mflags, 0); |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 1460 | if (!page) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1461 | goto free_pages; |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 1462 | bpage->page = page_address(page); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1463 | rb_init_page(bpage->page); |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1464 | |
| 1465 | if (user_thread && fatal_signal_pending(current)) |
| 1466 | goto free_pages; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1467 | } |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1468 | if (user_thread) |
| 1469 | clear_current_oom_origin(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1470 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1471 | return 0; |
| 1472 | |
| 1473 | free_pages: |
| 1474 | list_for_each_entry_safe(bpage, tmp, pages, list) { |
| 1475 | list_del_init(&bpage->list); |
| 1476 | free_buffer_page(bpage); |
| 1477 | } |
Steven Rostedt (VMware) | 927e56d | 2018-04-04 11:29:57 -0400 | [diff] [blame] | 1478 | if (user_thread) |
| 1479 | clear_current_oom_origin(); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1480 | |
| 1481 | return -ENOMEM; |
| 1482 | } |
| 1483 | |
| 1484 | 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] | 1485 | unsigned long nr_pages) |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1486 | { |
| 1487 | LIST_HEAD(pages); |
| 1488 | |
| 1489 | WARN_ON(!nr_pages); |
| 1490 | |
| 1491 | if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu)) |
| 1492 | return -ENOMEM; |
| 1493 | |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1494 | /* |
| 1495 | * The ring buffer page list is a circular list that does not |
| 1496 | * start and end with a list head. All page list items point to |
| 1497 | * other pages. |
| 1498 | */ |
| 1499 | cpu_buffer->pages = pages.next; |
| 1500 | list_del(&pages); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1501 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1502 | cpu_buffer->nr_pages = nr_pages; |
| 1503 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1504 | rb_check_pages(cpu_buffer); |
| 1505 | |
| 1506 | return 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | static struct ring_buffer_per_cpu * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1510 | 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] | 1511 | { |
| 1512 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1513 | struct buffer_page *bpage; |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 1514 | struct page *page; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1515 | int ret; |
| 1516 | |
| 1517 | cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()), |
| 1518 | GFP_KERNEL, cpu_to_node(cpu)); |
| 1519 | if (!cpu_buffer) |
| 1520 | return NULL; |
| 1521 | |
| 1522 | cpu_buffer->cpu = cpu; |
| 1523 | cpu_buffer->buffer = buffer; |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 1524 | raw_spin_lock_init(&cpu_buffer->reader_lock); |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 1525 | lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key); |
Thomas Gleixner | edc35bd | 2009-12-03 12:38:57 +0100 | [diff] [blame] | 1526 | cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1527 | INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler); |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 1528 | init_completion(&cpu_buffer->update_done); |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 1529 | 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] | 1530 | init_waitqueue_head(&cpu_buffer->irq_work.waiters); |
Steven Rostedt (Red Hat) | 1e0d671 | 2015-02-10 22:14:53 -0500 | [diff] [blame] | 1531 | init_waitqueue_head(&cpu_buffer->irq_work.full_waiters); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1532 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1533 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1534 | GFP_KERNEL, cpu_to_node(cpu)); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1535 | if (!bpage) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1536 | goto fail_free_buffer; |
| 1537 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1538 | rb_check_bpage(cpu_buffer, bpage); |
| 1539 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1540 | cpu_buffer->reader_page = bpage; |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 1541 | page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0); |
| 1542 | if (!page) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1543 | goto fail_free_reader; |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 1544 | bpage->page = page_address(page); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1545 | rb_init_page(bpage->page); |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1546 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1547 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); |
Vaibhav Nagarnaik | 44b9946 | 2012-06-22 11:50:05 -0700 | [diff] [blame] | 1548 | INIT_LIST_HEAD(&cpu_buffer->new_pages); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1549 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1550 | ret = rb_allocate_pages(cpu_buffer, nr_pages); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1551 | if (ret < 0) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1552 | goto fail_free_reader; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1553 | |
| 1554 | cpu_buffer->head_page |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1555 | = list_entry(cpu_buffer->pages, struct buffer_page, list); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1556 | cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1557 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1558 | rb_head_page_activate(cpu_buffer); |
| 1559 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1560 | return cpu_buffer; |
| 1561 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1562 | fail_free_reader: |
| 1563 | free_buffer_page(cpu_buffer->reader_page); |
| 1564 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1565 | fail_free_buffer: |
| 1566 | kfree(cpu_buffer); |
| 1567 | return NULL; |
| 1568 | } |
| 1569 | |
| 1570 | static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer) |
| 1571 | { |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1572 | struct list_head *head = cpu_buffer->pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1573 | struct buffer_page *bpage, *tmp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1574 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1575 | free_buffer_page(cpu_buffer->reader_page); |
| 1576 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1577 | rb_head_page_deactivate(cpu_buffer); |
| 1578 | |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1579 | if (head) { |
| 1580 | list_for_each_entry_safe(bpage, tmp, head, list) { |
| 1581 | list_del_init(&bpage->list); |
| 1582 | free_buffer_page(bpage); |
| 1583 | } |
| 1584 | bpage = list_entry(head, struct buffer_page, list); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1585 | free_buffer_page(bpage); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1586 | } |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1587 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1588 | kfree(cpu_buffer); |
| 1589 | } |
| 1590 | |
| 1591 | /** |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 1592 | * __ring_buffer_alloc - allocate a new ring_buffer |
Robert Richter | 68814b5 | 2008-11-24 12:24:12 +0100 | [diff] [blame] | 1593 | * @size: the size in bytes per cpu that is needed. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1594 | * @flags: attributes to set for the ring buffer. |
Fabian Frederick | 59e7cff | 2014-06-05 20:22:05 +0200 | [diff] [blame] | 1595 | * @key: ring buffer reader_lock_key. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1596 | * |
| 1597 | * Currently the only flag that is available is the RB_FL_OVERWRITE |
| 1598 | * flag. This flag means that the buffer will overwrite old data |
| 1599 | * when the buffer wraps. If this flag is not set, the buffer will |
| 1600 | * drop data when the tail hits the head. |
| 1601 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1602 | struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags, |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 1603 | struct lock_class_key *key) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1604 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1605 | struct trace_buffer *buffer; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 1606 | long nr_pages; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1607 | int bsize; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 1608 | int cpu; |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 1609 | int ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1610 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1611 | /* keep it in its own cache line */ |
| 1612 | buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()), |
| 1613 | GFP_KERNEL); |
| 1614 | if (!buffer) |
| 1615 | return NULL; |
| 1616 | |
Sebastian Andrzej Siewior | b18cc3d | 2016-12-07 14:31:33 +0100 | [diff] [blame] | 1617 | if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL)) |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1618 | goto fail_free_buffer; |
| 1619 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1620 | nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1621 | buffer->flags = flags; |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 1622 | buffer->clock = trace_clock_local; |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 1623 | buffer->reader_lock_key = key; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1624 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 1625 | 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] | 1626 | init_waitqueue_head(&buffer->irq_work.waiters); |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 1627 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1628 | /* need at least two pages */ |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1629 | if (nr_pages < 2) |
| 1630 | nr_pages = 2; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1631 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1632 | buffer->cpus = nr_cpu_ids; |
| 1633 | |
| 1634 | bsize = sizeof(void *) * nr_cpu_ids; |
| 1635 | buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()), |
| 1636 | GFP_KERNEL); |
| 1637 | if (!buffer->buffers) |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1638 | goto fail_free_cpumask; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1639 | |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 1640 | cpu = raw_smp_processor_id(); |
| 1641 | cpumask_set_cpu(cpu, buffer->cpumask); |
| 1642 | buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu); |
| 1643 | if (!buffer->buffers[cpu]) |
| 1644 | goto fail_free_buffers; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1645 | |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 1646 | ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node); |
| 1647 | if (ret < 0) |
| 1648 | goto fail_free_buffers; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1649 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1650 | mutex_init(&buffer->mutex); |
| 1651 | |
| 1652 | return buffer; |
| 1653 | |
| 1654 | fail_free_buffers: |
| 1655 | for_each_buffer_cpu(buffer, cpu) { |
| 1656 | if (buffer->buffers[cpu]) |
| 1657 | rb_free_cpu_buffer(buffer->buffers[cpu]); |
| 1658 | } |
| 1659 | kfree(buffer->buffers); |
| 1660 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1661 | fail_free_cpumask: |
| 1662 | free_cpumask_var(buffer->cpumask); |
| 1663 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1664 | fail_free_buffer: |
| 1665 | kfree(buffer); |
| 1666 | return NULL; |
| 1667 | } |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 1668 | EXPORT_SYMBOL_GPL(__ring_buffer_alloc); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1669 | |
| 1670 | /** |
| 1671 | * ring_buffer_free - free a ring buffer. |
| 1672 | * @buffer: the buffer to free. |
| 1673 | */ |
| 1674 | void |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1675 | ring_buffer_free(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1676 | { |
| 1677 | int cpu; |
| 1678 | |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 1679 | cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1680 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1681 | for_each_buffer_cpu(buffer, cpu) |
| 1682 | rb_free_cpu_buffer(buffer->buffers[cpu]); |
| 1683 | |
Eric Dumazet | bd3f022 | 2009-08-07 12:49:29 +0200 | [diff] [blame] | 1684 | kfree(buffer->buffers); |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1685 | free_cpumask_var(buffer->cpumask); |
| 1686 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1687 | kfree(buffer); |
| 1688 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1689 | EXPORT_SYMBOL_GPL(ring_buffer_free); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1690 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1691 | void ring_buffer_set_clock(struct trace_buffer *buffer, |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 1692 | u64 (*clock)(void)) |
| 1693 | { |
| 1694 | buffer->clock = clock; |
| 1695 | } |
| 1696 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1697 | 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] | 1698 | { |
| 1699 | buffer->time_stamp_abs = abs; |
| 1700 | } |
| 1701 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1702 | bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer) |
Tom Zanussi | 00b4145 | 2018-01-15 20:51:39 -0600 | [diff] [blame] | 1703 | { |
| 1704 | return buffer->time_stamp_abs; |
| 1705 | } |
| 1706 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1707 | static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer); |
| 1708 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1709 | static inline unsigned long rb_page_entries(struct buffer_page *bpage) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1710 | { |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1711 | return local_read(&bpage->entries) & RB_WRITE_MASK; |
| 1712 | } |
| 1713 | |
| 1714 | static inline unsigned long rb_page_write(struct buffer_page *bpage) |
| 1715 | { |
| 1716 | return local_read(&bpage->write) & RB_WRITE_MASK; |
| 1717 | } |
| 1718 | |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1719 | static int |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 1720 | 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] | 1721 | { |
| 1722 | struct list_head *tail_page, *to_remove, *next_page; |
| 1723 | struct buffer_page *to_remove_page, *tmp_iter_page; |
| 1724 | struct buffer_page *last_page, *first_page; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 1725 | unsigned long nr_removed; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1726 | unsigned long head_bit; |
| 1727 | int page_entries; |
| 1728 | |
| 1729 | head_bit = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1730 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 1731 | raw_spin_lock_irq(&cpu_buffer->reader_lock); |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1732 | atomic_inc(&cpu_buffer->record_disabled); |
| 1733 | /* |
| 1734 | * We don't race with the readers since we have acquired the reader |
| 1735 | * lock. We also don't race with writers after disabling recording. |
| 1736 | * This makes it easy to figure out the first and the last page to be |
| 1737 | * removed from the list. We unlink all the pages in between including |
| 1738 | * the first and last pages. This is done in a busy loop so that we |
| 1739 | * lose the least number of traces. |
| 1740 | * The pages are freed after we restart recording and unlock readers. |
| 1741 | */ |
| 1742 | tail_page = &cpu_buffer->tail_page->list; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1743 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1744 | /* |
| 1745 | * tail page might be on reader page, we remove the next page |
| 1746 | * from the ring buffer |
| 1747 | */ |
| 1748 | if (cpu_buffer->tail_page == cpu_buffer->reader_page) |
| 1749 | tail_page = rb_list_head(tail_page->next); |
| 1750 | to_remove = tail_page; |
| 1751 | |
| 1752 | /* start of pages to remove */ |
| 1753 | first_page = list_entry(rb_list_head(to_remove->next), |
| 1754 | struct buffer_page, list); |
| 1755 | |
| 1756 | for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) { |
| 1757 | to_remove = rb_list_head(to_remove)->next; |
| 1758 | head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1759 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1760 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1761 | next_page = rb_list_head(to_remove)->next; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1762 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1763 | /* |
| 1764 | * Now we remove all pages between tail_page and next_page. |
| 1765 | * Make sure that we have head_bit value preserved for the |
| 1766 | * next page |
| 1767 | */ |
| 1768 | tail_page->next = (struct list_head *)((unsigned long)next_page | |
| 1769 | head_bit); |
| 1770 | next_page = rb_list_head(next_page); |
| 1771 | next_page->prev = tail_page; |
| 1772 | |
| 1773 | /* make sure pages points to a valid page in the ring buffer */ |
| 1774 | cpu_buffer->pages = next_page; |
| 1775 | |
| 1776 | /* update head page */ |
| 1777 | if (head_bit) |
| 1778 | cpu_buffer->head_page = list_entry(next_page, |
| 1779 | struct buffer_page, list); |
| 1780 | |
| 1781 | /* |
| 1782 | * change read pointer to make sure any read iterators reset |
| 1783 | * themselves |
| 1784 | */ |
| 1785 | cpu_buffer->read = 0; |
| 1786 | |
| 1787 | /* pages are removed, resume tracing and then free the pages */ |
| 1788 | atomic_dec(&cpu_buffer->record_disabled); |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 1789 | raw_spin_unlock_irq(&cpu_buffer->reader_lock); |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1790 | |
| 1791 | RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)); |
| 1792 | |
| 1793 | /* last buffer page to remove */ |
| 1794 | last_page = list_entry(rb_list_head(to_remove), struct buffer_page, |
| 1795 | list); |
| 1796 | tmp_iter_page = first_page; |
| 1797 | |
| 1798 | do { |
Vaibhav Nagarnaik | 83f3655 | 2018-09-07 15:31:29 -0700 | [diff] [blame] | 1799 | cond_resched(); |
| 1800 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1801 | to_remove_page = tmp_iter_page; |
| 1802 | rb_inc_page(cpu_buffer, &tmp_iter_page); |
| 1803 | |
| 1804 | /* update the counters */ |
| 1805 | page_entries = rb_page_entries(to_remove_page); |
| 1806 | if (page_entries) { |
| 1807 | /* |
| 1808 | * If something was added to this page, it was full |
| 1809 | * since it is not the tail page. So we deduct the |
| 1810 | * bytes consumed in ring buffer from here. |
Vaibhav Nagarnaik | 48fdc72 | 2012-06-29 12:31:41 -0700 | [diff] [blame] | 1811 | * Increment overrun to account for the lost events. |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1812 | */ |
Vaibhav Nagarnaik | 48fdc72 | 2012-06-29 12:31:41 -0700 | [diff] [blame] | 1813 | local_add(page_entries, &cpu_buffer->overrun); |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1814 | local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes); |
| 1815 | } |
| 1816 | |
| 1817 | /* |
| 1818 | * We have already removed references to this list item, just |
| 1819 | * free up the buffer_page and its page |
| 1820 | */ |
| 1821 | free_buffer_page(to_remove_page); |
| 1822 | nr_removed--; |
| 1823 | |
| 1824 | } while (to_remove_page != last_page); |
| 1825 | |
| 1826 | RB_WARN_ON(cpu_buffer, nr_removed); |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1827 | |
| 1828 | return nr_removed == 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1829 | } |
| 1830 | |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1831 | static int |
| 1832 | rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1833 | { |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1834 | struct list_head *pages = &cpu_buffer->new_pages; |
| 1835 | int retries, success; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1836 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 1837 | raw_spin_lock_irq(&cpu_buffer->reader_lock); |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1838 | /* |
| 1839 | * We are holding the reader lock, so the reader page won't be swapped |
| 1840 | * in the ring buffer. Now we are racing with the writer trying to |
| 1841 | * move head page and the tail page. |
| 1842 | * We are going to adapt the reader page update process where: |
| 1843 | * 1. We first splice the start and end of list of new pages between |
| 1844 | * the head page and its previous page. |
| 1845 | * 2. We cmpxchg the prev_page->next to point from head page to the |
| 1846 | * start of new pages list. |
| 1847 | * 3. Finally, we update the head->prev to the end of new list. |
| 1848 | * |
| 1849 | * We will try this process 10 times, to make sure that we don't keep |
| 1850 | * spinning. |
| 1851 | */ |
| 1852 | retries = 10; |
| 1853 | success = 0; |
| 1854 | while (retries--) { |
| 1855 | struct list_head *head_page, *prev_page, *r; |
| 1856 | struct list_head *last_page, *first_page; |
| 1857 | struct list_head *head_page_with_bit; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1858 | |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1859 | head_page = &rb_set_head_page(cpu_buffer)->list; |
Steven Rostedt | 54f7be5 | 2012-11-29 22:27:22 -0500 | [diff] [blame] | 1860 | if (!head_page) |
| 1861 | break; |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1862 | prev_page = head_page->prev; |
| 1863 | |
| 1864 | first_page = pages->next; |
| 1865 | last_page = pages->prev; |
| 1866 | |
| 1867 | head_page_with_bit = (struct list_head *) |
| 1868 | ((unsigned long)head_page | RB_PAGE_HEAD); |
| 1869 | |
| 1870 | last_page->next = head_page_with_bit; |
| 1871 | first_page->prev = prev_page; |
| 1872 | |
| 1873 | r = cmpxchg(&prev_page->next, head_page_with_bit, first_page); |
| 1874 | |
| 1875 | if (r == head_page_with_bit) { |
| 1876 | /* |
| 1877 | * yay, we replaced the page pointer to our new list, |
| 1878 | * now, we just have to update to head page's prev |
| 1879 | * pointer to point to end of list |
| 1880 | */ |
| 1881 | head_page->prev = last_page; |
| 1882 | success = 1; |
| 1883 | break; |
| 1884 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1885 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1886 | |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1887 | if (success) |
| 1888 | INIT_LIST_HEAD(pages); |
| 1889 | /* |
| 1890 | * If we weren't successful in adding in new pages, warn and stop |
| 1891 | * tracing |
| 1892 | */ |
| 1893 | RB_WARN_ON(cpu_buffer, !success); |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 1894 | raw_spin_unlock_irq(&cpu_buffer->reader_lock); |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1895 | |
| 1896 | /* free pages if they weren't inserted */ |
| 1897 | if (!success) { |
| 1898 | struct buffer_page *bpage, *tmp; |
| 1899 | list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages, |
| 1900 | list) { |
| 1901 | list_del_init(&bpage->list); |
| 1902 | free_buffer_page(bpage); |
| 1903 | } |
| 1904 | } |
| 1905 | return success; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1906 | } |
| 1907 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1908 | static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer) |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1909 | { |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1910 | int success; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1911 | |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 1912 | if (cpu_buffer->nr_pages_to_update > 0) |
| 1913 | success = rb_insert_pages(cpu_buffer); |
| 1914 | else |
| 1915 | success = rb_remove_pages(cpu_buffer, |
| 1916 | -cpu_buffer->nr_pages_to_update); |
| 1917 | |
| 1918 | if (success) |
| 1919 | cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | static void update_pages_handler(struct work_struct *work) |
| 1923 | { |
| 1924 | struct ring_buffer_per_cpu *cpu_buffer = container_of(work, |
| 1925 | struct ring_buffer_per_cpu, update_pages_work); |
| 1926 | rb_update_pages(cpu_buffer); |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 1927 | complete(&cpu_buffer->update_done); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1928 | } |
| 1929 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1930 | /** |
| 1931 | * ring_buffer_resize - resize the ring buffer |
| 1932 | * @buffer: the buffer to resize. |
| 1933 | * @size: the new size. |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 1934 | * @cpu_id: the cpu buffer to resize |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1935 | * |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1936 | * Minimum size is 2 * BUF_PAGE_SIZE. |
| 1937 | * |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1938 | * Returns 0 on success and < 0 on failure. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1939 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 1940 | int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size, |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1941 | int cpu_id) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1942 | { |
| 1943 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 1944 | unsigned long nr_pages; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1945 | int cpu, err = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1946 | |
Ingo Molnar | ee51a1d | 2008-11-13 14:58:31 +0100 | [diff] [blame] | 1947 | /* |
| 1948 | * Always succeed at resizing a non-existent buffer: |
| 1949 | */ |
| 1950 | if (!buffer) |
| 1951 | return size; |
| 1952 | |
Steven Rostedt | 6a31e1f | 2012-05-23 15:35:17 -0400 | [diff] [blame] | 1953 | /* Make sure the requested buffer exists */ |
| 1954 | if (cpu_id != RING_BUFFER_ALL_CPUS && |
| 1955 | !cpumask_test_cpu(cpu_id, buffer->cpumask)) |
| 1956 | return size; |
| 1957 | |
Steven Rostedt (Red Hat) | 59643d1 | 2016-05-13 09:34:12 -0400 | [diff] [blame] | 1958 | nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1959 | |
| 1960 | /* we need a minimum of two pages */ |
Steven Rostedt (Red Hat) | 59643d1 | 2016-05-13 09:34:12 -0400 | [diff] [blame] | 1961 | if (nr_pages < 2) |
| 1962 | nr_pages = 2; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1963 | |
Steven Rostedt (Red Hat) | 59643d1 | 2016-05-13 09:34:12 -0400 | [diff] [blame] | 1964 | size = nr_pages * BUF_PAGE_SIZE; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1965 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 1966 | /* prevent another thread from changing buffer sizes */ |
| 1967 | mutex_lock(&buffer->mutex); |
| 1968 | |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 1969 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1970 | if (cpu_id == RING_BUFFER_ALL_CPUS) { |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 1971 | /* |
| 1972 | * Don't succeed if resizing is disabled, as a reader might be |
| 1973 | * manipulating the ring buffer and is expecting a sane state while |
| 1974 | * this is true. |
| 1975 | */ |
| 1976 | for_each_buffer_cpu(buffer, cpu) { |
| 1977 | cpu_buffer = buffer->buffers[cpu]; |
| 1978 | if (atomic_read(&cpu_buffer->resize_disabled)) { |
| 1979 | err = -EBUSY; |
| 1980 | goto out_err_unlock; |
| 1981 | } |
| 1982 | } |
| 1983 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1984 | /* calculate the pages to update */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1985 | for_each_buffer_cpu(buffer, cpu) { |
| 1986 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1987 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1988 | cpu_buffer->nr_pages_to_update = nr_pages - |
| 1989 | cpu_buffer->nr_pages; |
Vaibhav Nagarnaik | d7ec4bf | 2011-06-07 17:01:42 -0700 | [diff] [blame] | 1990 | /* |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1991 | * nothing more to do for removing pages or no update |
Vaibhav Nagarnaik | d7ec4bf | 2011-06-07 17:01:42 -0700 | [diff] [blame] | 1992 | */ |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1993 | if (cpu_buffer->nr_pages_to_update <= 0) |
| 1994 | continue; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 1995 | /* |
| 1996 | * to add pages, make sure all new pages can be |
| 1997 | * allocated without receiving ENOMEM |
| 1998 | */ |
| 1999 | INIT_LIST_HEAD(&cpu_buffer->new_pages); |
| 2000 | if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update, |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2001 | &cpu_buffer->new_pages, cpu)) { |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2002 | /* not enough memory for new pages */ |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2003 | err = -ENOMEM; |
| 2004 | goto out_err; |
| 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | get_online_cpus(); |
| 2009 | /* |
| 2010 | * Fire off all the required work handlers |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2011 | * We can't schedule on offline CPUs, but it's not necessary |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2012 | * since we can change their buffer sizes without any race. |
| 2013 | */ |
| 2014 | for_each_buffer_cpu(buffer, cpu) { |
| 2015 | cpu_buffer = buffer->buffers[cpu]; |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2016 | if (!cpu_buffer->nr_pages_to_update) |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2017 | continue; |
| 2018 | |
Corey Minyard | 021c5b3 | 2014-07-16 14:07:13 -0500 | [diff] [blame] | 2019 | /* Can't run something on an offline CPU. */ |
| 2020 | if (!cpu_online(cpu)) { |
Steven Rostedt (Red Hat) | f5eb558 | 2013-03-07 09:27:42 -0500 | [diff] [blame] | 2021 | rb_update_pages(cpu_buffer); |
| 2022 | cpu_buffer->nr_pages_to_update = 0; |
| 2023 | } else { |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2024 | schedule_work_on(cpu, |
| 2025 | &cpu_buffer->update_pages_work); |
Steven Rostedt (Red Hat) | f5eb558 | 2013-03-07 09:27:42 -0500 | [diff] [blame] | 2026 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2027 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2028 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2029 | /* wait for all the updates to complete */ |
| 2030 | for_each_buffer_cpu(buffer, cpu) { |
| 2031 | cpu_buffer = buffer->buffers[cpu]; |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2032 | if (!cpu_buffer->nr_pages_to_update) |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2033 | continue; |
| 2034 | |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2035 | if (cpu_online(cpu)) |
| 2036 | wait_for_completion(&cpu_buffer->update_done); |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2037 | cpu_buffer->nr_pages_to_update = 0; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2038 | } |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2039 | |
| 2040 | put_online_cpus(); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2041 | } else { |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 2042 | /* Make sure this CPU has been initialized */ |
Vaibhav Nagarnaik | 8e49f41 | 2012-10-10 16:40:27 -0700 | [diff] [blame] | 2043 | if (!cpumask_test_cpu(cpu_id, buffer->cpumask)) |
| 2044 | goto out; |
| 2045 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2046 | cpu_buffer = buffer->buffers[cpu_id]; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2047 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2048 | if (nr_pages == cpu_buffer->nr_pages) |
| 2049 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2050 | |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 2051 | /* |
| 2052 | * Don't succeed if resizing is disabled, as a reader might be |
| 2053 | * manipulating the ring buffer and is expecting a sane state while |
| 2054 | * this is true. |
| 2055 | */ |
| 2056 | if (atomic_read(&cpu_buffer->resize_disabled)) { |
| 2057 | err = -EBUSY; |
| 2058 | goto out_err_unlock; |
| 2059 | } |
| 2060 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2061 | cpu_buffer->nr_pages_to_update = nr_pages - |
| 2062 | cpu_buffer->nr_pages; |
| 2063 | |
| 2064 | INIT_LIST_HEAD(&cpu_buffer->new_pages); |
| 2065 | if (cpu_buffer->nr_pages_to_update > 0 && |
| 2066 | __rb_allocate_pages(cpu_buffer->nr_pages_to_update, |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2067 | &cpu_buffer->new_pages, cpu_id)) { |
| 2068 | err = -ENOMEM; |
| 2069 | goto out_err; |
| 2070 | } |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2071 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2072 | get_online_cpus(); |
| 2073 | |
Corey Minyard | 021c5b3 | 2014-07-16 14:07:13 -0500 | [diff] [blame] | 2074 | /* Can't run something on an offline CPU. */ |
| 2075 | if (!cpu_online(cpu_id)) |
Steven Rostedt (Red Hat) | f5eb558 | 2013-03-07 09:27:42 -0500 | [diff] [blame] | 2076 | rb_update_pages(cpu_buffer); |
| 2077 | else { |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2078 | schedule_work_on(cpu_id, |
| 2079 | &cpu_buffer->update_pages_work); |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2080 | wait_for_completion(&cpu_buffer->update_done); |
Steven Rostedt (Red Hat) | f5eb558 | 2013-03-07 09:27:42 -0500 | [diff] [blame] | 2081 | } |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2082 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2083 | cpu_buffer->nr_pages_to_update = 0; |
Vaibhav Nagarnaik | 05fdd70 | 2012-05-18 13:29:51 -0700 | [diff] [blame] | 2084 | put_online_cpus(); |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2085 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2086 | |
| 2087 | out: |
Steven Rostedt | 659f451 | 2012-05-14 17:02:33 -0400 | [diff] [blame] | 2088 | /* |
| 2089 | * The ring buffer resize can happen with the ring buffer |
| 2090 | * enabled, so that the update disturbs the tracing as little |
| 2091 | * as possible. But if the buffer is disabled, we do not need |
| 2092 | * to worry about that, and we can take the time to verify |
| 2093 | * that the buffer is not corrupt. |
| 2094 | */ |
| 2095 | if (atomic_read(&buffer->record_disabled)) { |
| 2096 | atomic_inc(&buffer->record_disabled); |
| 2097 | /* |
| 2098 | * Even though the buffer was disabled, we must make sure |
| 2099 | * that it is truly disabled before calling rb_check_pages. |
| 2100 | * There could have been a race between checking |
| 2101 | * record_disable and incrementing it. |
| 2102 | */ |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 2103 | synchronize_rcu(); |
Steven Rostedt | 659f451 | 2012-05-14 17:02:33 -0400 | [diff] [blame] | 2104 | for_each_buffer_cpu(buffer, cpu) { |
| 2105 | cpu_buffer = buffer->buffers[cpu]; |
| 2106 | rb_check_pages(cpu_buffer); |
| 2107 | } |
| 2108 | atomic_dec(&buffer->record_disabled); |
| 2109 | } |
| 2110 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2111 | mutex_unlock(&buffer->mutex); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2112 | return size; |
| 2113 | |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2114 | out_err: |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2115 | for_each_buffer_cpu(buffer, cpu) { |
| 2116 | struct buffer_page *bpage, *tmp; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2117 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2118 | cpu_buffer = buffer->buffers[cpu]; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2119 | cpu_buffer->nr_pages_to_update = 0; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2120 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2121 | if (list_empty(&cpu_buffer->new_pages)) |
| 2122 | continue; |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2123 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 2124 | list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages, |
| 2125 | list) { |
| 2126 | list_del_init(&bpage->list); |
| 2127 | free_buffer_page(bpage); |
| 2128 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2129 | } |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 2130 | out_err_unlock: |
Vegard Nossum | 641d2f6 | 2008-11-18 19:22:13 +0100 | [diff] [blame] | 2131 | mutex_unlock(&buffer->mutex); |
Vaibhav Nagarnaik | 83f4031 | 2012-05-03 18:59:50 -0700 | [diff] [blame] | 2132 | return err; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2133 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2134 | EXPORT_SYMBOL_GPL(ring_buffer_resize); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2135 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2136 | void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val) |
David Sharp | 750912f | 2010-12-08 13:46:47 -0800 | [diff] [blame] | 2137 | { |
| 2138 | mutex_lock(&buffer->mutex); |
| 2139 | if (val) |
| 2140 | buffer->flags |= RB_FL_OVERWRITE; |
| 2141 | else |
| 2142 | buffer->flags &= ~RB_FL_OVERWRITE; |
| 2143 | mutex_unlock(&buffer->mutex); |
| 2144 | } |
| 2145 | EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite); |
| 2146 | |
Steven Rostedt (Red Hat) | 2289d56 | 2016-11-23 20:35:32 -0500 | [diff] [blame] | 2147 | 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] | 2148 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 2149 | return bpage->page->data + index; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2150 | } |
| 2151 | |
Steven Rostedt (Red Hat) | 2289d56 | 2016-11-23 20:35:32 -0500 | [diff] [blame] | 2152 | static __always_inline struct ring_buffer_event * |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2153 | rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2154 | { |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 2155 | return __rb_page_index(cpu_buffer->reader_page, |
| 2156 | cpu_buffer->reader_page->read); |
| 2157 | } |
| 2158 | |
Steven Rostedt (Red Hat) | 2289d56 | 2016-11-23 20:35:32 -0500 | [diff] [blame] | 2159 | static __always_inline unsigned rb_page_commit(struct buffer_page *bpage) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2160 | { |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 2161 | return local_read(&bpage->page->commit); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2162 | } |
| 2163 | |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 2164 | static struct ring_buffer_event * |
| 2165 | rb_iter_head_event(struct ring_buffer_iter *iter) |
| 2166 | { |
| 2167 | struct ring_buffer_event *event; |
| 2168 | struct buffer_page *iter_head_page = iter->head_page; |
| 2169 | unsigned long commit; |
| 2170 | unsigned length; |
| 2171 | |
Steven Rostedt (VMware) | 153368c | 2020-03-17 17:32:29 -0400 | [diff] [blame] | 2172 | if (iter->head != iter->next_event) |
| 2173 | return iter->event; |
| 2174 | |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 2175 | /* |
| 2176 | * When the writer goes across pages, it issues a cmpxchg which |
| 2177 | * is a mb(), which will synchronize with the rmb here. |
| 2178 | * (see rb_tail_page_update() and __rb_reserve_next()) |
| 2179 | */ |
| 2180 | commit = rb_page_commit(iter_head_page); |
| 2181 | smp_rmb(); |
| 2182 | event = __rb_page_index(iter_head_page, iter->head); |
| 2183 | length = rb_event_length(event); |
| 2184 | |
| 2185 | /* |
| 2186 | * READ_ONCE() doesn't work on functions and we don't want the |
| 2187 | * compiler doing any crazy optimizations with length. |
| 2188 | */ |
| 2189 | barrier(); |
| 2190 | |
| 2191 | if ((iter->head + length) > commit || length > BUF_MAX_DATA_SIZE) |
| 2192 | /* Writer corrupted the read? */ |
| 2193 | goto reset; |
| 2194 | |
| 2195 | memcpy(iter->event, event, length); |
| 2196 | /* |
| 2197 | * If the page stamp is still the same after this rmb() then the |
| 2198 | * event was safely copied without the writer entering the page. |
| 2199 | */ |
| 2200 | smp_rmb(); |
| 2201 | |
| 2202 | /* Make sure the page didn't change since we read this */ |
| 2203 | if (iter->page_stamp != iter_head_page->page->time_stamp || |
| 2204 | commit > rb_page_commit(iter_head_page)) |
| 2205 | goto reset; |
| 2206 | |
| 2207 | iter->next_event = iter->head + length; |
| 2208 | return iter->event; |
| 2209 | reset: |
| 2210 | /* Reset to the beginning */ |
| 2211 | iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp; |
| 2212 | iter->head = 0; |
| 2213 | iter->next_event = 0; |
Steven Rostedt (VMware) | c9b7a4a | 2020-03-17 17:32:32 -0400 | [diff] [blame] | 2214 | iter->missed_events = 1; |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 2215 | return NULL; |
| 2216 | } |
| 2217 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2218 | /* Size is determined by what has been committed */ |
Steven Rostedt (Red Hat) | 2289d56 | 2016-11-23 20:35:32 -0500 | [diff] [blame] | 2219 | static __always_inline unsigned rb_page_size(struct buffer_page *bpage) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2220 | { |
| 2221 | return rb_page_commit(bpage); |
| 2222 | } |
| 2223 | |
Steven Rostedt (Red Hat) | 2289d56 | 2016-11-23 20:35:32 -0500 | [diff] [blame] | 2224 | static __always_inline unsigned |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2225 | rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer) |
| 2226 | { |
| 2227 | return rb_page_commit(cpu_buffer->commit_page); |
| 2228 | } |
| 2229 | |
Steven Rostedt (Red Hat) | 2289d56 | 2016-11-23 20:35:32 -0500 | [diff] [blame] | 2230 | static __always_inline unsigned |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2231 | rb_event_index(struct ring_buffer_event *event) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2232 | { |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2233 | unsigned long addr = (unsigned long)event; |
| 2234 | |
Steven Rostedt | 22f470f | 2009-06-11 09:29:58 -0400 | [diff] [blame] | 2235 | return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2236 | } |
| 2237 | |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 2238 | static void rb_inc_iter(struct ring_buffer_iter *iter) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2239 | { |
| 2240 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 2241 | |
| 2242 | /* |
| 2243 | * The iterator could be on the reader page (it starts there). |
| 2244 | * But the head could have moved, since the reader was |
| 2245 | * found. Check for this case and assign the iterator |
| 2246 | * to the head page instead of next. |
| 2247 | */ |
| 2248 | if (iter->head_page == cpu_buffer->reader_page) |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2249 | iter->head_page = rb_set_head_page(cpu_buffer); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2250 | else |
| 2251 | rb_inc_page(cpu_buffer, &iter->head_page); |
| 2252 | |
Steven Rostedt (VMware) | 28e3fc5 | 2020-03-17 17:32:26 -0400 | [diff] [blame] | 2253 | iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2254 | iter->head = 0; |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 2255 | iter->next_event = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2256 | } |
| 2257 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2258 | /* |
| 2259 | * rb_handle_head_page - writer hit the head page |
| 2260 | * |
| 2261 | * Returns: +1 to retry page |
| 2262 | * 0 to continue |
| 2263 | * -1 on error |
| 2264 | */ |
| 2265 | static int |
| 2266 | rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer, |
| 2267 | struct buffer_page *tail_page, |
| 2268 | struct buffer_page *next_page) |
| 2269 | { |
| 2270 | struct buffer_page *new_head; |
| 2271 | int entries; |
| 2272 | int type; |
| 2273 | int ret; |
| 2274 | |
| 2275 | entries = rb_page_entries(next_page); |
| 2276 | |
| 2277 | /* |
| 2278 | * The hard part is here. We need to move the head |
| 2279 | * forward, and protect against both readers on |
| 2280 | * other CPUs and writers coming in via interrupts. |
| 2281 | */ |
| 2282 | type = rb_head_page_set_update(cpu_buffer, next_page, tail_page, |
| 2283 | RB_PAGE_HEAD); |
| 2284 | |
| 2285 | /* |
| 2286 | * type can be one of four: |
| 2287 | * NORMAL - an interrupt already moved it for us |
| 2288 | * HEAD - we are the first to get here. |
| 2289 | * UPDATE - we are the interrupt interrupting |
| 2290 | * a current move. |
| 2291 | * MOVED - a reader on another CPU moved the next |
| 2292 | * pointer to its reader page. Give up |
| 2293 | * and try again. |
| 2294 | */ |
| 2295 | |
| 2296 | switch (type) { |
| 2297 | case RB_PAGE_HEAD: |
| 2298 | /* |
| 2299 | * We changed the head to UPDATE, thus |
| 2300 | * it is our responsibility to update |
| 2301 | * the counters. |
| 2302 | */ |
| 2303 | local_add(entries, &cpu_buffer->overrun); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 2304 | local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2305 | |
| 2306 | /* |
| 2307 | * The entries will be zeroed out when we move the |
| 2308 | * tail page. |
| 2309 | */ |
| 2310 | |
| 2311 | /* still more to do */ |
| 2312 | break; |
| 2313 | |
| 2314 | case RB_PAGE_UPDATE: |
| 2315 | /* |
| 2316 | * This is an interrupt that interrupt the |
| 2317 | * previous update. Still more to do. |
| 2318 | */ |
| 2319 | break; |
| 2320 | case RB_PAGE_NORMAL: |
| 2321 | /* |
| 2322 | * An interrupt came in before the update |
| 2323 | * and processed this for us. |
| 2324 | * Nothing left to do. |
| 2325 | */ |
| 2326 | return 1; |
| 2327 | case RB_PAGE_MOVED: |
| 2328 | /* |
| 2329 | * The reader is on another CPU and just did |
| 2330 | * a swap with our next_page. |
| 2331 | * Try again. |
| 2332 | */ |
| 2333 | return 1; |
| 2334 | default: |
| 2335 | RB_WARN_ON(cpu_buffer, 1); /* WTF??? */ |
| 2336 | return -1; |
| 2337 | } |
| 2338 | |
| 2339 | /* |
| 2340 | * Now that we are here, the old head pointer is |
| 2341 | * set to UPDATE. This will keep the reader from |
| 2342 | * swapping the head page with the reader page. |
| 2343 | * The reader (on another CPU) will spin till |
| 2344 | * we are finished. |
| 2345 | * |
| 2346 | * We just need to protect against interrupts |
| 2347 | * doing the job. We will set the next pointer |
| 2348 | * to HEAD. After that, we set the old pointer |
| 2349 | * to NORMAL, but only if it was HEAD before. |
| 2350 | * otherwise we are an interrupt, and only |
| 2351 | * want the outer most commit to reset it. |
| 2352 | */ |
| 2353 | new_head = next_page; |
| 2354 | rb_inc_page(cpu_buffer, &new_head); |
| 2355 | |
| 2356 | ret = rb_head_page_set_head(cpu_buffer, new_head, next_page, |
| 2357 | RB_PAGE_NORMAL); |
| 2358 | |
| 2359 | /* |
| 2360 | * Valid returns are: |
| 2361 | * HEAD - an interrupt came in and already set it. |
| 2362 | * NORMAL - One of two things: |
| 2363 | * 1) We really set it. |
| 2364 | * 2) A bunch of interrupts came in and moved |
| 2365 | * the page forward again. |
| 2366 | */ |
| 2367 | switch (ret) { |
| 2368 | case RB_PAGE_HEAD: |
| 2369 | case RB_PAGE_NORMAL: |
| 2370 | /* OK */ |
| 2371 | break; |
| 2372 | default: |
| 2373 | RB_WARN_ON(cpu_buffer, 1); |
| 2374 | return -1; |
| 2375 | } |
| 2376 | |
| 2377 | /* |
| 2378 | * It is possible that an interrupt came in, |
| 2379 | * set the head up, then more interrupts came in |
| 2380 | * and moved it again. When we get back here, |
| 2381 | * the page would have been set to NORMAL but we |
| 2382 | * just set it back to HEAD. |
| 2383 | * |
| 2384 | * How do you detect this? Well, if that happened |
| 2385 | * the tail page would have moved. |
| 2386 | */ |
| 2387 | if (ret == RB_PAGE_NORMAL) { |
Steven Rostedt (Red Hat) | 8573636 | 2015-11-17 14:03:11 -0500 | [diff] [blame] | 2388 | struct buffer_page *buffer_tail_page; |
| 2389 | |
| 2390 | buffer_tail_page = READ_ONCE(cpu_buffer->tail_page); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2391 | /* |
| 2392 | * If the tail had moved passed next, then we need |
| 2393 | * to reset the pointer. |
| 2394 | */ |
Steven Rostedt (Red Hat) | 8573636 | 2015-11-17 14:03:11 -0500 | [diff] [blame] | 2395 | if (buffer_tail_page != tail_page && |
| 2396 | buffer_tail_page != next_page) |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2397 | rb_head_page_set_normal(cpu_buffer, new_head, |
| 2398 | next_page, |
| 2399 | RB_PAGE_HEAD); |
| 2400 | } |
| 2401 | |
| 2402 | /* |
| 2403 | * If this was the outer most commit (the one that |
| 2404 | * changed the original pointer from HEAD to UPDATE), |
| 2405 | * then it is up to us to reset it to NORMAL. |
| 2406 | */ |
| 2407 | if (type == RB_PAGE_HEAD) { |
| 2408 | ret = rb_head_page_set_normal(cpu_buffer, next_page, |
| 2409 | tail_page, |
| 2410 | RB_PAGE_UPDATE); |
| 2411 | if (RB_WARN_ON(cpu_buffer, |
| 2412 | ret != RB_PAGE_UPDATE)) |
| 2413 | return -1; |
| 2414 | } |
| 2415 | |
| 2416 | return 0; |
| 2417 | } |
| 2418 | |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2419 | static inline void |
| 2420 | rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2421 | unsigned long tail, struct rb_event_info *info) |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2422 | { |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2423 | struct buffer_page *tail_page = info->tail_page; |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2424 | struct ring_buffer_event *event; |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2425 | unsigned long length = info->length; |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2426 | |
| 2427 | /* |
| 2428 | * Only the event that crossed the page boundary |
| 2429 | * must fill the old tail_page with padding. |
| 2430 | */ |
| 2431 | if (tail >= BUF_PAGE_SIZE) { |
Steven Rostedt | b3230c8 | 2010-05-21 11:55:21 -0400 | [diff] [blame] | 2432 | /* |
| 2433 | * If the page was filled, then we still need |
| 2434 | * to update the real_end. Reset it to zero |
| 2435 | * and the reader will ignore it. |
| 2436 | */ |
| 2437 | if (tail == BUF_PAGE_SIZE) |
| 2438 | tail_page->real_end = 0; |
| 2439 | |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2440 | local_sub(length, &tail_page->write); |
| 2441 | return; |
| 2442 | } |
| 2443 | |
| 2444 | event = __rb_page_index(tail_page, tail); |
| 2445 | |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 2446 | /* account for padding bytes */ |
| 2447 | local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes); |
| 2448 | |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2449 | /* |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 2450 | * Save the original length to the meta data. |
| 2451 | * This will be used by the reader to add lost event |
| 2452 | * counter. |
| 2453 | */ |
| 2454 | tail_page->real_end = tail; |
| 2455 | |
| 2456 | /* |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2457 | * If this event is bigger than the minimum size, then |
| 2458 | * we need to be careful that we don't subtract the |
| 2459 | * write counter enough to allow another writer to slip |
| 2460 | * in on this page. |
| 2461 | * We put in a discarded commit instead, to make sure |
| 2462 | * that this space is not used again. |
| 2463 | * |
| 2464 | * If we are less than the minimum size, we don't need to |
| 2465 | * worry about it. |
| 2466 | */ |
| 2467 | if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) { |
| 2468 | /* No room for any events */ |
| 2469 | |
| 2470 | /* Mark the rest of the page with padding */ |
| 2471 | rb_event_set_padding(event); |
| 2472 | |
| 2473 | /* Set the write back to the previous setting */ |
| 2474 | local_sub(length, &tail_page->write); |
| 2475 | return; |
| 2476 | } |
| 2477 | |
| 2478 | /* Put in a discarded event */ |
| 2479 | event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE; |
| 2480 | event->type_len = RINGBUF_TYPE_PADDING; |
| 2481 | /* time delta must be non zero */ |
| 2482 | event->time_delta = 1; |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 2483 | |
| 2484 | /* Set write to end of buffer */ |
| 2485 | length = (tail + length) - BUF_PAGE_SIZE; |
| 2486 | local_sub(length, &tail_page->write); |
| 2487 | } |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 2488 | |
Steven Rostedt (Red Hat) | 4239c38 | 2015-11-17 16:36:06 -0500 | [diff] [blame] | 2489 | static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer); |
| 2490 | |
Steven Rostedt | 747e94a | 2010-10-08 13:51:48 -0400 | [diff] [blame] | 2491 | /* |
| 2492 | * This is the slow path, force gcc not to inline it. |
| 2493 | */ |
| 2494 | static noinline struct ring_buffer_event * |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 2495 | rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2496 | unsigned long tail, struct rb_event_info *info) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2497 | { |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2498 | struct buffer_page *tail_page = info->tail_page; |
Steven Rostedt | 5a50e33 | 2009-11-17 08:43:01 -0500 | [diff] [blame] | 2499 | struct buffer_page *commit_page = cpu_buffer->commit_page; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2500 | struct trace_buffer *buffer = cpu_buffer->buffer; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2501 | struct buffer_page *next_page; |
| 2502 | int ret; |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2503 | |
| 2504 | next_page = tail_page; |
| 2505 | |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2506 | rb_inc_page(cpu_buffer, &next_page); |
| 2507 | |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2508 | /* |
| 2509 | * If for some reason, we had an interrupt storm that made |
| 2510 | * it all the way around the buffer, bail, and warn |
| 2511 | * about it. |
| 2512 | */ |
| 2513 | if (unlikely(next_page == commit_page)) { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2514 | local_inc(&cpu_buffer->commit_overrun); |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2515 | goto out_reset; |
| 2516 | } |
| 2517 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2518 | /* |
| 2519 | * This is where the fun begins! |
| 2520 | * |
| 2521 | * We are fighting against races between a reader that |
| 2522 | * could be on another CPU trying to swap its reader |
| 2523 | * page with the buffer head. |
| 2524 | * |
| 2525 | * We are also fighting against interrupts coming in and |
| 2526 | * moving the head or tail on us as well. |
| 2527 | * |
| 2528 | * If the next page is the head page then we have filled |
| 2529 | * the buffer, unless the commit page is still on the |
| 2530 | * reader page. |
| 2531 | */ |
| 2532 | if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) { |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2533 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2534 | /* |
| 2535 | * If the commit is not on the reader page, then |
| 2536 | * move the header page. |
| 2537 | */ |
| 2538 | if (!rb_is_reader_page(cpu_buffer->commit_page)) { |
| 2539 | /* |
| 2540 | * If we are not in overwrite mode, |
| 2541 | * this is easy, just stop here. |
| 2542 | */ |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 2543 | if (!(buffer->flags & RB_FL_OVERWRITE)) { |
| 2544 | local_inc(&cpu_buffer->dropped_events); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2545 | goto out_reset; |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 2546 | } |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2547 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2548 | ret = rb_handle_head_page(cpu_buffer, |
| 2549 | tail_page, |
| 2550 | next_page); |
| 2551 | if (ret < 0) |
| 2552 | goto out_reset; |
| 2553 | if (ret) |
| 2554 | goto out_again; |
| 2555 | } else { |
| 2556 | /* |
| 2557 | * We need to be careful here too. The |
| 2558 | * commit page could still be on the reader |
| 2559 | * page. We could have a small buffer, and |
| 2560 | * have filled up the buffer with events |
| 2561 | * from interrupts and such, and wrapped. |
| 2562 | * |
| 2563 | * Note, if the tail page is also the on the |
| 2564 | * reader_page, we let it move out. |
| 2565 | */ |
| 2566 | if (unlikely((cpu_buffer->commit_page != |
| 2567 | cpu_buffer->tail_page) && |
| 2568 | (cpu_buffer->commit_page == |
| 2569 | cpu_buffer->reader_page))) { |
| 2570 | local_inc(&cpu_buffer->commit_overrun); |
| 2571 | goto out_reset; |
| 2572 | } |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2573 | } |
| 2574 | } |
| 2575 | |
Steven Rostedt (Red Hat) | 7000498 | 2015-11-17 15:15:19 -0500 | [diff] [blame] | 2576 | rb_tail_page_update(cpu_buffer, tail_page, next_page); |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2577 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2578 | out_again: |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2579 | |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2580 | rb_reset_tail(cpu_buffer, tail, info); |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2581 | |
Steven Rostedt (Red Hat) | 4239c38 | 2015-11-17 16:36:06 -0500 | [diff] [blame] | 2582 | /* Commit what we have for now. */ |
| 2583 | rb_end_commit(cpu_buffer); |
| 2584 | /* rb_end_commit() decs committing */ |
| 2585 | local_inc(&cpu_buffer->committing); |
| 2586 | |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 2587 | /* fail and let the caller try again */ |
| 2588 | return ERR_PTR(-EAGAIN); |
| 2589 | |
Steven Rostedt | 45141d4 | 2009-02-12 13:19:48 -0500 | [diff] [blame] | 2590 | out_reset: |
Lai Jiangshan | 6f3b344 | 2009-01-12 11:06:18 +0800 | [diff] [blame] | 2591 | /* reset write */ |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 2592 | rb_reset_tail(cpu_buffer, tail, info); |
Lai Jiangshan | 6f3b344 | 2009-01-12 11:06:18 +0800 | [diff] [blame] | 2593 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2594 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2595 | } |
| 2596 | |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2597 | /* Slow path, do not inline */ |
| 2598 | static noinline struct ring_buffer_event * |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 2599 | 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] | 2600 | { |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 2601 | if (abs) |
| 2602 | event->type_len = RINGBUF_TYPE_TIME_STAMP; |
| 2603 | else |
| 2604 | event->type_len = RINGBUF_TYPE_TIME_EXTEND; |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2605 | |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 2606 | /* Not the first event on the page, or not delta? */ |
| 2607 | if (abs || rb_event_index(event)) { |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2608 | event->time_delta = delta & TS_MASK; |
| 2609 | event->array[0] = delta >> TS_SHIFT; |
| 2610 | } else { |
| 2611 | /* nope, just zero it */ |
| 2612 | event->time_delta = 0; |
| 2613 | event->array[0] = 0; |
| 2614 | } |
| 2615 | |
| 2616 | return skip_time_extend(event); |
| 2617 | } |
| 2618 | |
Yaowei Bai | cdb2a0a9 | 2015-09-29 22:43:34 +0800 | [diff] [blame] | 2619 | static inline bool rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 2620 | struct ring_buffer_event *event); |
| 2621 | |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2622 | /** |
| 2623 | * rb_update_event - update event type and data |
Steven Rostedt (VMware) | cfc585a | 2020-01-14 16:27:51 -0500 | [diff] [blame] | 2624 | * @cpu_buffer: The per cpu buffer of the @event |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2625 | * @event: the event to update |
Steven Rostedt (VMware) | cfc585a | 2020-01-14 16:27:51 -0500 | [diff] [blame] | 2626 | * @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] | 2627 | * |
Steven Rostedt (VMware) | cfc585a | 2020-01-14 16:27:51 -0500 | [diff] [blame] | 2628 | * 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] | 2629 | * is the actual size that is written to the ring buffer, |
| 2630 | * and with this, we can determine what to place into the |
| 2631 | * data field. |
| 2632 | */ |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 2633 | static void |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2634 | rb_update_event(struct ring_buffer_per_cpu *cpu_buffer, |
| 2635 | struct ring_buffer_event *event, |
| 2636 | struct rb_event_info *info) |
| 2637 | { |
| 2638 | unsigned length = info->length; |
| 2639 | u64 delta = info->delta; |
| 2640 | |
| 2641 | /* |
| 2642 | * If we need to add a timestamp, then we |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 2643 | * add it to the start of the reserved space. |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2644 | */ |
| 2645 | if (unlikely(info->add_timestamp)) { |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 2646 | bool abs = info->add_timestamp & |
| 2647 | (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE); |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 2648 | |
Steven Rostedt (VMware) | 097350d | 2020-06-22 15:18:15 -0400 | [diff] [blame] | 2649 | event = rb_add_time_stamp(event, abs ? info->delta : delta, abs); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2650 | length -= RB_LEN_TIME_EXTEND; |
| 2651 | delta = 0; |
| 2652 | } |
| 2653 | |
| 2654 | event->time_delta = delta; |
| 2655 | length -= RB_EVNT_HDR_SIZE; |
Steven Rostedt (VMware) | 86b3de6 | 2019-05-28 09:36:19 -0400 | [diff] [blame] | 2656 | if (length > RB_MAX_SMALL_DATA) { |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2657 | event->type_len = 0; |
| 2658 | event->array[0] = length; |
| 2659 | } else |
| 2660 | event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT); |
| 2661 | } |
| 2662 | |
| 2663 | static unsigned rb_calculate_event_length(unsigned length) |
| 2664 | { |
| 2665 | struct ring_buffer_event event; /* Used only for sizeof array */ |
| 2666 | |
| 2667 | /* zero length can cause confusions */ |
| 2668 | if (!length) |
| 2669 | length++; |
| 2670 | |
Steven Rostedt (VMware) | 86b3de6 | 2019-05-28 09:36:19 -0400 | [diff] [blame] | 2671 | if (length > RB_MAX_SMALL_DATA) |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2672 | length += sizeof(event.array[0]); |
| 2673 | |
| 2674 | length += RB_EVNT_HDR_SIZE; |
Steven Rostedt (VMware) | 86b3de6 | 2019-05-28 09:36:19 -0400 | [diff] [blame] | 2675 | length = ALIGN(length, RB_ALIGNMENT); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2676 | |
| 2677 | /* |
| 2678 | * In case the time delta is larger than the 27 bits for it |
| 2679 | * in the header, we need to add a timestamp. If another |
| 2680 | * event comes in when trying to discard this one to increase |
| 2681 | * the length, then the timestamp will be added in the allocated |
| 2682 | * space of this event. If length is bigger than the size needed |
| 2683 | * for the TIME_EXTEND, then padding has to be used. The events |
| 2684 | * length must be either RB_LEN_TIME_EXTEND, or greater than or equal |
| 2685 | * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding. |
| 2686 | * As length is a multiple of 4, we only need to worry if it |
| 2687 | * is 12 (RB_LEN_TIME_EXTEND + 4). |
| 2688 | */ |
| 2689 | if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT) |
| 2690 | length += RB_ALIGNMENT; |
| 2691 | |
| 2692 | return length; |
| 2693 | } |
| 2694 | |
Steven Rostedt (Red Hat) | 9826b27 | 2015-05-28 17:36:45 -0400 | [diff] [blame] | 2695 | #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK |
| 2696 | static inline bool sched_clock_stable(void) |
| 2697 | { |
| 2698 | return true; |
| 2699 | } |
| 2700 | #endif |
| 2701 | |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2702 | static __always_inline bool |
| 2703 | rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer, |
| 2704 | struct ring_buffer_event *event) |
| 2705 | { |
| 2706 | unsigned long addr = (unsigned long)event; |
| 2707 | unsigned long index; |
| 2708 | |
| 2709 | index = rb_event_index(event); |
| 2710 | addr &= PAGE_MASK; |
| 2711 | |
| 2712 | return cpu_buffer->commit_page->page == (void *)addr && |
| 2713 | rb_commit_index(cpu_buffer) == index; |
| 2714 | } |
| 2715 | |
| 2716 | static u64 rb_time_delta(struct ring_buffer_event *event) |
| 2717 | { |
| 2718 | switch (event->type_len) { |
| 2719 | case RINGBUF_TYPE_PADDING: |
| 2720 | return 0; |
| 2721 | |
| 2722 | case RINGBUF_TYPE_TIME_EXTEND: |
| 2723 | return ring_buffer_event_time_stamp(event); |
| 2724 | |
| 2725 | case RINGBUF_TYPE_TIME_STAMP: |
| 2726 | return 0; |
| 2727 | |
| 2728 | case RINGBUF_TYPE_DATA: |
| 2729 | return event->time_delta; |
| 2730 | default: |
| 2731 | return 0; |
| 2732 | } |
| 2733 | } |
| 2734 | |
Steven Rostedt (Red Hat) | a4543a2 | 2015-05-29 09:40:18 -0400 | [diff] [blame] | 2735 | static inline int |
| 2736 | 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] | 2737 | struct ring_buffer_event *event) |
| 2738 | { |
| 2739 | unsigned long new_index, old_index; |
| 2740 | struct buffer_page *bpage; |
| 2741 | unsigned long index; |
| 2742 | unsigned long addr; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2743 | u64 write_stamp; |
| 2744 | u64 delta; |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2745 | |
| 2746 | new_index = rb_event_index(event); |
| 2747 | old_index = new_index + rb_event_ts_length(event); |
| 2748 | addr = (unsigned long)event; |
| 2749 | addr &= PAGE_MASK; |
| 2750 | |
Steven Rostedt (Red Hat) | 8573636 | 2015-11-17 14:03:11 -0500 | [diff] [blame] | 2751 | bpage = READ_ONCE(cpu_buffer->tail_page); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2752 | |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2753 | delta = rb_time_delta(event); |
| 2754 | |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 2755 | if (!rb_time_read(&cpu_buffer->write_stamp, &write_stamp)) |
| 2756 | return 0; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2757 | |
| 2758 | /* Make sure the write stamp is read before testing the location */ |
| 2759 | barrier(); |
| 2760 | |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2761 | if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) { |
| 2762 | unsigned long write_mask = |
| 2763 | local_read(&bpage->write) & ~RB_WRITE_MASK; |
| 2764 | unsigned long event_length = rb_event_length(event); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2765 | |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2766 | /* Something came in, can't discard */ |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 2767 | if (!rb_time_cmpxchg(&cpu_buffer->write_stamp, |
| 2768 | write_stamp, write_stamp - delta)) |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 2769 | return 0; |
| 2770 | |
| 2771 | /* |
| 2772 | * If an event were to come in now, it would see that the |
| 2773 | * write_stamp and the before_stamp are different, and assume |
| 2774 | * that this event just added itself before updating |
| 2775 | * the write stamp. The interrupting event will fix the |
| 2776 | * write stamp for us, and use the before stamp as its delta. |
| 2777 | */ |
| 2778 | |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2779 | /* |
| 2780 | * This is on the tail page. It is possible that |
| 2781 | * a write could come in and move the tail page |
| 2782 | * and write to the next page. That is fine |
| 2783 | * because we just shorten what is on this page. |
| 2784 | */ |
| 2785 | old_index += write_mask; |
| 2786 | new_index += write_mask; |
| 2787 | index = local_cmpxchg(&bpage->write, old_index, new_index); |
| 2788 | if (index == old_index) { |
| 2789 | /* update counters */ |
| 2790 | local_sub(event_length, &cpu_buffer->entries_bytes); |
| 2791 | return 1; |
| 2792 | } |
| 2793 | } |
| 2794 | |
| 2795 | /* could not discard */ |
| 2796 | return 0; |
| 2797 | } |
| 2798 | |
| 2799 | static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer) |
| 2800 | { |
| 2801 | local_inc(&cpu_buffer->committing); |
| 2802 | local_inc(&cpu_buffer->commits); |
| 2803 | } |
| 2804 | |
Steven Rostedt (Red Hat) | 38e11df | 2016-11-23 20:42:31 -0500 | [diff] [blame] | 2805 | static __always_inline void |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2806 | rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer) |
| 2807 | { |
| 2808 | unsigned long max_count; |
| 2809 | |
| 2810 | /* |
| 2811 | * We only race with interrupts and NMIs on this CPU. |
| 2812 | * If we own the commit event, then we can commit |
| 2813 | * all others that interrupted us, since the interruptions |
| 2814 | * are in stack format (they finish before they come |
| 2815 | * back to us). This allows us to do a simple loop to |
| 2816 | * assign the commit to the tail. |
| 2817 | */ |
| 2818 | again: |
| 2819 | max_count = cpu_buffer->nr_pages * 100; |
| 2820 | |
Steven Rostedt (Red Hat) | 8573636 | 2015-11-17 14:03:11 -0500 | [diff] [blame] | 2821 | 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] | 2822 | if (RB_WARN_ON(cpu_buffer, !(--max_count))) |
| 2823 | return; |
| 2824 | if (RB_WARN_ON(cpu_buffer, |
| 2825 | rb_is_reader_page(cpu_buffer->tail_page))) |
| 2826 | return; |
| 2827 | local_set(&cpu_buffer->commit_page->page->commit, |
| 2828 | rb_page_write(cpu_buffer->commit_page)); |
| 2829 | rb_inc_page(cpu_buffer, &cpu_buffer->commit_page); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2830 | /* add barrier to keep gcc from optimizing too much */ |
| 2831 | barrier(); |
| 2832 | } |
| 2833 | while (rb_commit_index(cpu_buffer) != |
| 2834 | rb_page_write(cpu_buffer->commit_page)) { |
| 2835 | |
| 2836 | local_set(&cpu_buffer->commit_page->page->commit, |
| 2837 | rb_page_write(cpu_buffer->commit_page)); |
| 2838 | RB_WARN_ON(cpu_buffer, |
| 2839 | local_read(&cpu_buffer->commit_page->page->commit) & |
| 2840 | ~RB_WRITE_MASK); |
| 2841 | barrier(); |
| 2842 | } |
| 2843 | |
| 2844 | /* again, keep gcc from optimizing */ |
| 2845 | barrier(); |
| 2846 | |
| 2847 | /* |
| 2848 | * If an interrupt came in just after the first while loop |
| 2849 | * and pushed the tail page forward, we will be left with |
| 2850 | * a dangling commit that will never go forward. |
| 2851 | */ |
Steven Rostedt (Red Hat) | 8573636 | 2015-11-17 14:03:11 -0500 | [diff] [blame] | 2852 | 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] | 2853 | goto again; |
| 2854 | } |
| 2855 | |
Steven Rostedt (Red Hat) | 38e11df | 2016-11-23 20:42:31 -0500 | [diff] [blame] | 2856 | 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] | 2857 | { |
| 2858 | unsigned long commits; |
| 2859 | |
| 2860 | if (RB_WARN_ON(cpu_buffer, |
| 2861 | !local_read(&cpu_buffer->committing))) |
| 2862 | return; |
| 2863 | |
| 2864 | again: |
| 2865 | commits = local_read(&cpu_buffer->commits); |
| 2866 | /* synchronize with interrupts */ |
| 2867 | barrier(); |
| 2868 | if (local_read(&cpu_buffer->committing) == 1) |
| 2869 | rb_set_commit_to_write(cpu_buffer); |
| 2870 | |
| 2871 | local_dec(&cpu_buffer->committing); |
| 2872 | |
| 2873 | /* synchronize with interrupts */ |
| 2874 | barrier(); |
| 2875 | |
| 2876 | /* |
| 2877 | * Need to account for interrupts coming in between the |
| 2878 | * updating of the commit page and the clearing of the |
| 2879 | * committing counter. |
| 2880 | */ |
| 2881 | if (unlikely(local_read(&cpu_buffer->commits) != commits) && |
| 2882 | !local_read(&cpu_buffer->committing)) { |
| 2883 | local_inc(&cpu_buffer->committing); |
| 2884 | goto again; |
| 2885 | } |
| 2886 | } |
| 2887 | |
| 2888 | static inline void rb_event_discard(struct ring_buffer_event *event) |
| 2889 | { |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 2890 | if (extended_time(event)) |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2891 | event = skip_time_extend(event); |
| 2892 | |
| 2893 | /* array[0] holds the actual length for the discarded event */ |
| 2894 | event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE; |
| 2895 | event->type_len = RINGBUF_TYPE_PADDING; |
| 2896 | /* time delta must be non zero */ |
| 2897 | if (!event->time_delta) |
| 2898 | event->time_delta = 1; |
| 2899 | } |
| 2900 | |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2901 | static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer, |
| 2902 | struct ring_buffer_event *event) |
| 2903 | { |
| 2904 | local_inc(&cpu_buffer->entries); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2905 | rb_end_commit(cpu_buffer); |
| 2906 | } |
| 2907 | |
| 2908 | static __always_inline void |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 2909 | 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] | 2910 | { |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 2911 | size_t nr_pages; |
| 2912 | size_t dirty; |
| 2913 | size_t full; |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2914 | |
| 2915 | if (buffer->irq_work.waiters_pending) { |
| 2916 | buffer->irq_work.waiters_pending = false; |
| 2917 | /* irq_work_queue() supplies it's own memory barriers */ |
| 2918 | irq_work_queue(&buffer->irq_work.work); |
| 2919 | } |
| 2920 | |
| 2921 | if (cpu_buffer->irq_work.waiters_pending) { |
| 2922 | cpu_buffer->irq_work.waiters_pending = false; |
| 2923 | /* irq_work_queue() supplies it's own memory barriers */ |
| 2924 | irq_work_queue(&cpu_buffer->irq_work.work); |
| 2925 | } |
| 2926 | |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 2927 | if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched)) |
| 2928 | return; |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2929 | |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 2930 | if (cpu_buffer->reader_page == cpu_buffer->commit_page) |
| 2931 | return; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 2932 | |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 2933 | if (!cpu_buffer->irq_work.full_waiters_pending) |
| 2934 | return; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 2935 | |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 2936 | cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched); |
| 2937 | |
| 2938 | full = cpu_buffer->shortest_full; |
| 2939 | nr_pages = cpu_buffer->nr_pages; |
| 2940 | dirty = ring_buffer_nr_dirty_pages(buffer, cpu_buffer->cpu); |
| 2941 | if (full && nr_pages && (dirty * 100) <= full * nr_pages) |
| 2942 | return; |
| 2943 | |
| 2944 | cpu_buffer->irq_work.wakeup_full = true; |
| 2945 | cpu_buffer->irq_work.full_waiters_pending = false; |
| 2946 | /* irq_work_queue() supplies it's own memory barriers */ |
| 2947 | irq_work_queue(&cpu_buffer->irq_work.work); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2948 | } |
| 2949 | |
| 2950 | /* |
| 2951 | * The lock and unlock are done within a preempt disable section. |
| 2952 | * The current_context per_cpu variable can only be modified |
| 2953 | * by the current task between lock and unlock. But it can |
Steven Rostedt (VMware) | a0e3a18 | 2018-01-15 10:47:09 -0500 | [diff] [blame] | 2954 | * be modified more than once via an interrupt. To pass this |
| 2955 | * information from the lock to the unlock without having to |
| 2956 | * access the 'in_interrupt()' functions again (which do show |
| 2957 | * a bit of overhead in something as critical as function tracing, |
| 2958 | * we use a bitmask trick. |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2959 | * |
Steven Rostedt (VMware) | a0e3a18 | 2018-01-15 10:47:09 -0500 | [diff] [blame] | 2960 | * bit 0 = NMI context |
| 2961 | * bit 1 = IRQ context |
| 2962 | * bit 2 = SoftIRQ context |
| 2963 | * bit 3 = normal context. |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2964 | * |
Steven Rostedt (VMware) | a0e3a18 | 2018-01-15 10:47:09 -0500 | [diff] [blame] | 2965 | * This works because this is the order of contexts that can |
| 2966 | * preempt other contexts. A SoftIRQ never preempts an IRQ |
| 2967 | * context. |
| 2968 | * |
| 2969 | * When the context is determined, the corresponding bit is |
| 2970 | * checked and set (if it was set, then a recursion of that context |
| 2971 | * happened). |
| 2972 | * |
| 2973 | * On unlock, we need to clear this bit. To do so, just subtract |
| 2974 | * 1 from the current_context and AND it to itself. |
| 2975 | * |
| 2976 | * (binary) |
| 2977 | * 101 - 1 = 100 |
| 2978 | * 101 & 100 = 100 (clearing bit zero) |
| 2979 | * |
| 2980 | * 1010 - 1 = 1001 |
| 2981 | * 1010 & 1001 = 1000 (clearing bit 1) |
| 2982 | * |
| 2983 | * The least significant bit can be cleared this way, and it |
| 2984 | * just so happens that it is the same bit corresponding to |
| 2985 | * the current context. |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 2986 | */ |
| 2987 | |
| 2988 | static __always_inline int |
| 2989 | trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer) |
| 2990 | { |
Steven Rostedt (VMware) | a0e3a18 | 2018-01-15 10:47:09 -0500 | [diff] [blame] | 2991 | unsigned int val = cpu_buffer->current_context; |
| 2992 | unsigned long pc = preempt_count(); |
| 2993 | int bit; |
| 2994 | |
| 2995 | if (!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET))) |
| 2996 | bit = RB_CTX_NORMAL; |
| 2997 | else |
| 2998 | bit = pc & NMI_MASK ? RB_CTX_NMI : |
Steven Rostedt (VMware) | 0164e0d | 2018-01-18 15:42:09 -0500 | [diff] [blame] | 2999 | pc & HARDIRQ_MASK ? RB_CTX_IRQ : RB_CTX_SOFTIRQ; |
Steven Rostedt (VMware) | a0e3a18 | 2018-01-15 10:47:09 -0500 | [diff] [blame] | 3000 | |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3001 | if (unlikely(val & (1 << (bit + cpu_buffer->nest)))) |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3002 | return 1; |
| 3003 | |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3004 | val |= (1 << (bit + cpu_buffer->nest)); |
Steven Rostedt (VMware) | a0e3a18 | 2018-01-15 10:47:09 -0500 | [diff] [blame] | 3005 | cpu_buffer->current_context = val; |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3006 | |
| 3007 | return 0; |
| 3008 | } |
| 3009 | |
| 3010 | static __always_inline void |
| 3011 | trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer) |
| 3012 | { |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3013 | cpu_buffer->current_context &= |
| 3014 | cpu_buffer->current_context - (1 << cpu_buffer->nest); |
| 3015 | } |
| 3016 | |
| 3017 | /* The recursive locking above uses 4 bits */ |
| 3018 | #define NESTED_BITS 4 |
| 3019 | |
| 3020 | /** |
| 3021 | * ring_buffer_nest_start - Allow to trace while nested |
| 3022 | * @buffer: The ring buffer to modify |
| 3023 | * |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 3024 | * The ring buffer has a safety mechanism to prevent recursion. |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3025 | * But there may be a case where a trace needs to be done while |
| 3026 | * tracing something else. In this case, calling this function |
| 3027 | * will allow this function to nest within a currently active |
| 3028 | * ring_buffer_lock_reserve(). |
| 3029 | * |
| 3030 | * Call this function before calling another ring_buffer_lock_reserve() and |
| 3031 | * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit(). |
| 3032 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3033 | void ring_buffer_nest_start(struct trace_buffer *buffer) |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3034 | { |
| 3035 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3036 | int cpu; |
| 3037 | |
| 3038 | /* Enabled by ring_buffer_nest_end() */ |
| 3039 | preempt_disable_notrace(); |
| 3040 | cpu = raw_smp_processor_id(); |
| 3041 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 3042 | /* This is the shift value for the above recursive locking */ |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3043 | cpu_buffer->nest += NESTED_BITS; |
| 3044 | } |
| 3045 | |
| 3046 | /** |
| 3047 | * ring_buffer_nest_end - Allow to trace while nested |
| 3048 | * @buffer: The ring buffer to modify |
| 3049 | * |
| 3050 | * Must be called after ring_buffer_nest_start() and after the |
| 3051 | * ring_buffer_unlock_commit(). |
| 3052 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3053 | void ring_buffer_nest_end(struct trace_buffer *buffer) |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3054 | { |
| 3055 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3056 | int cpu; |
| 3057 | |
| 3058 | /* disabled by ring_buffer_nest_start() */ |
| 3059 | cpu = raw_smp_processor_id(); |
| 3060 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 3061 | /* This is the shift value for the above recursive locking */ |
Steven Rostedt (VMware) | 8e01206 | 2018-02-07 17:26:32 -0500 | [diff] [blame] | 3062 | cpu_buffer->nest -= NESTED_BITS; |
| 3063 | preempt_enable_notrace(); |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | /** |
| 3067 | * ring_buffer_unlock_commit - commit a reserved |
| 3068 | * @buffer: The buffer to commit to |
| 3069 | * @event: The event pointer to commit. |
| 3070 | * |
| 3071 | * This commits the data to the ring buffer, and releases any locks held. |
| 3072 | * |
| 3073 | * Must be paired with ring_buffer_lock_reserve. |
| 3074 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3075 | int ring_buffer_unlock_commit(struct trace_buffer *buffer, |
Steven Rostedt (Red Hat) | d90fd77 | 2015-05-29 12:12:27 -0400 | [diff] [blame] | 3076 | struct ring_buffer_event *event) |
| 3077 | { |
| 3078 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3079 | int cpu = raw_smp_processor_id(); |
| 3080 | |
| 3081 | cpu_buffer = buffer->buffers[cpu]; |
| 3082 | |
| 3083 | rb_commit(cpu_buffer, event); |
| 3084 | |
| 3085 | rb_wakeups(buffer, cpu_buffer); |
| 3086 | |
| 3087 | trace_recursive_unlock(cpu_buffer); |
| 3088 | |
| 3089 | preempt_enable_notrace(); |
| 3090 | |
| 3091 | return 0; |
| 3092 | } |
| 3093 | EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit); |
Steven Rostedt (Red Hat) | a4543a2 | 2015-05-29 09:40:18 -0400 | [diff] [blame] | 3094 | |
Steven Rostedt (Red Hat) | 9826b27 | 2015-05-28 17:36:45 -0400 | [diff] [blame] | 3095 | static noinline void |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 3096 | rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer, |
| 3097 | struct rb_event_info *info) |
Steven Rostedt (Red Hat) | 9826b27 | 2015-05-28 17:36:45 -0400 | [diff] [blame] | 3098 | { |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3099 | u64 write_stamp; |
| 3100 | |
Steven Rostedt (Red Hat) | 9826b27 | 2015-05-28 17:36:45 -0400 | [diff] [blame] | 3101 | WARN_ONCE(info->delta > (1ULL << 59), |
| 3102 | KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s", |
| 3103 | (unsigned long long)info->delta, |
| 3104 | (unsigned long long)info->ts, |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3105 | (unsigned long long)(rb_time_read(&cpu_buffer->write_stamp, &write_stamp) ? write_stamp : 0), |
Steven Rostedt (Red Hat) | 9826b27 | 2015-05-28 17:36:45 -0400 | [diff] [blame] | 3106 | sched_clock_stable() ? "" : |
| 3107 | "If you just came from a suspend/resume,\n" |
| 3108 | "please switch to the trace global clock:\n" |
Chris Wilson | 913ea4d | 2018-03-30 16:01:32 +0100 | [diff] [blame] | 3109 | " echo global > /sys/kernel/debug/tracing/trace_clock\n" |
| 3110 | "or add trace_clock=global to the kernel command line\n"); |
Steven Rostedt (Red Hat) | 9826b27 | 2015-05-28 17:36:45 -0400 | [diff] [blame] | 3111 | } |
| 3112 | |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3113 | static struct ring_buffer_event * |
| 3114 | __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3115 | struct rb_event_info *info) |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3116 | { |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3117 | struct ring_buffer_event *event; |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3118 | struct buffer_page *tail_page; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3119 | unsigned long tail, write, w; |
| 3120 | u64 before, after; |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3121 | bool a_ok; |
| 3122 | bool b_ok; |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3123 | |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3124 | /* Don't let the compiler play games with cpu_buffer->tail_page */ |
| 3125 | tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page); |
| 3126 | |
| 3127 | /*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK; |
| 3128 | barrier(); |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3129 | b_ok = rb_time_read(&cpu_buffer->before_stamp, &before); |
| 3130 | a_ok = rb_time_read(&cpu_buffer->write_stamp, &after); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3131 | barrier(); |
| 3132 | info->ts = rb_time_stamp(cpu_buffer->buffer); |
| 3133 | |
| 3134 | if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) { |
| 3135 | info->delta = info->ts; |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 3136 | info->add_timestamp = RB_ADD_STAMP_ABSOLUTE; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3137 | } else { |
| 3138 | info->delta = info->ts - after; |
| 3139 | } |
| 3140 | |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3141 | if (likely(a_ok && b_ok)) { |
| 3142 | if (unlikely(test_time_stamp(info->delta))) { |
| 3143 | rb_check_timestamp(cpu_buffer, info); |
| 3144 | info->add_timestamp |= RB_ADD_STAMP_EXTEND; |
| 3145 | } |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 3146 | } |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3147 | |
| 3148 | /* |
| 3149 | * If interrupting an event time update, we may need an absolute timestamp. |
| 3150 | * Don't bother if this is the start of a new page (w == 0). |
| 3151 | */ |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3152 | if (unlikely(!a_ok || !b_ok || (before != after && w))) |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 3153 | info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND; |
| 3154 | |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3155 | /* |
| 3156 | * If the time delta since the last event is too big to |
| 3157 | * hold in the time field of the event, then we append a |
| 3158 | * TIME EXTEND event ahead of the data event. |
| 3159 | */ |
| 3160 | if (unlikely(info->add_timestamp)) |
| 3161 | info->length += RB_LEN_TIME_EXTEND; |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 3162 | |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3163 | /*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3164 | |
| 3165 | /*C*/ write = local_add_return(info->length, &tail_page->write); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3166 | |
| 3167 | /* set write to only the index of the write */ |
| 3168 | write &= RB_WRITE_MASK; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3169 | |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3170 | tail = write - info->length; |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3171 | |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3172 | /* See if we shot pass the end of this buffer page */ |
| 3173 | if (unlikely(write > BUF_PAGE_SIZE)) { |
| 3174 | if (tail != w) { |
| 3175 | /* before and after may now different, fix it up*/ |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3176 | b_ok = rb_time_read(&cpu_buffer->before_stamp, &before); |
| 3177 | a_ok = rb_time_read(&cpu_buffer->write_stamp, &after); |
| 3178 | if (a_ok && b_ok && before != after) |
| 3179 | (void)rb_time_cmpxchg(&cpu_buffer->before_stamp, before, after); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3180 | } |
| 3181 | return rb_move_tail(cpu_buffer, tail, info); |
| 3182 | } |
| 3183 | |
| 3184 | if (likely(tail == w)) { |
| 3185 | u64 save_before; |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3186 | bool s_ok; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3187 | |
| 3188 | /* Nothing interrupted us between A and C */ |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3189 | /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3190 | barrier(); |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3191 | /*E*/ s_ok = rb_time_read(&cpu_buffer->before_stamp, &save_before); |
| 3192 | RB_WARN_ON(cpu_buffer, !s_ok); |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 3193 | if (likely(!(info->add_timestamp & |
| 3194 | (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)))) |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3195 | /* This did not interrupt any time update */ |
| 3196 | info->delta = info->ts - after; |
| 3197 | else |
| 3198 | /* Just use full timestamp for inerrupting event */ |
| 3199 | info->delta = info->ts; |
| 3200 | barrier(); |
| 3201 | if (unlikely(info->ts != save_before)) { |
| 3202 | /* SLOW PATH - Interrupted between C and E */ |
| 3203 | |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3204 | a_ok = rb_time_read(&cpu_buffer->write_stamp, &after); |
| 3205 | RB_WARN_ON(cpu_buffer, !a_ok); |
| 3206 | |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3207 | /* Write stamp must only go forward */ |
| 3208 | if (save_before > after) { |
| 3209 | /* |
| 3210 | * We do not care about the result, only that |
| 3211 | * it gets updated atomically. |
| 3212 | */ |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3213 | (void)rb_time_cmpxchg(&cpu_buffer->write_stamp, after, save_before); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3214 | } |
| 3215 | } |
| 3216 | } else { |
| 3217 | u64 ts; |
| 3218 | /* SLOW PATH - Interrupted between A and C */ |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3219 | a_ok = rb_time_read(&cpu_buffer->write_stamp, &after); |
| 3220 | /* Was interrupted before here, write_stamp must be valid */ |
| 3221 | RB_WARN_ON(cpu_buffer, !a_ok); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3222 | ts = rb_time_stamp(cpu_buffer->buffer); |
| 3223 | barrier(); |
| 3224 | /*E*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) && |
| 3225 | after < ts) { |
| 3226 | /* Nothing came after this event between C and E */ |
| 3227 | info->delta = ts - after; |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 3228 | (void)rb_time_cmpxchg(&cpu_buffer->write_stamp, after, info->ts); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3229 | info->ts = ts; |
| 3230 | } else { |
| 3231 | /* |
| 3232 | * Interrupted beween C and E: |
| 3233 | * Lost the previous events time stamp. Just set the |
| 3234 | * delta to zero, and this will be the same time as |
| 3235 | * the event this event interrupted. And the events that |
| 3236 | * came after this will still be correct (as they would |
| 3237 | * have built their delta on the previous event. |
| 3238 | */ |
| 3239 | info->delta = 0; |
| 3240 | } |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 3241 | info->add_timestamp &= ~RB_ADD_STAMP_FORCE; |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3242 | } |
| 3243 | |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3244 | /* |
| 3245 | * If this is the first commit on the page, then it has the same |
| 3246 | * timestamp as the page itself. |
| 3247 | */ |
Steven Rostedt (VMware) | 7c4b4a5 | 2020-06-28 22:52:26 -0400 | [diff] [blame] | 3248 | if (unlikely(!tail && !(info->add_timestamp & |
| 3249 | (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)))) |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3250 | info->delta = 0; |
| 3251 | |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3252 | /* We reserved something on the buffer */ |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3253 | |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3254 | event = __rb_page_index(tail_page, tail); |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3255 | rb_update_event(cpu_buffer, event, info); |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3256 | |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 3257 | local_inc(&tail_page->entries); |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3258 | |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3259 | /* |
| 3260 | * If this is the first commit on the page, then update |
| 3261 | * its timestamp. |
| 3262 | */ |
| 3263 | if (!tail) |
| 3264 | tail_page->page->time_stamp = info->ts; |
| 3265 | |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 3266 | /* account for these added bytes */ |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3267 | local_add(info->length, &cpu_buffer->entries_bytes); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 3268 | |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 3269 | return event; |
| 3270 | } |
| 3271 | |
Steven Rostedt (Red Hat) | fa7ffb3 | 2016-11-23 11:36:30 -0500 | [diff] [blame] | 3272 | static __always_inline struct ring_buffer_event * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3273 | rb_reserve_next_event(struct trace_buffer *buffer, |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 3274 | struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt | 1cd8d73 | 2009-05-11 14:08:09 -0400 | [diff] [blame] | 3275 | unsigned long length) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3276 | { |
| 3277 | struct ring_buffer_event *event; |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3278 | struct rb_event_info info; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3279 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3280 | |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3281 | rb_start_commit(cpu_buffer); |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3282 | /* The commit page can not change after this */ |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3283 | |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 3284 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 3285 | /* |
| 3286 | * Due to the ability to swap a cpu buffer from a buffer |
| 3287 | * it is possible it was swapped before we committed. |
| 3288 | * (committing stops a swap). We check for it here and |
| 3289 | * if it happened, we have to fail the write. |
| 3290 | */ |
| 3291 | barrier(); |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 3292 | if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) { |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 3293 | local_dec(&cpu_buffer->committing); |
| 3294 | local_dec(&cpu_buffer->commits); |
| 3295 | return NULL; |
| 3296 | } |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 3297 | #endif |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3298 | |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3299 | info.length = rb_calculate_event_length(length); |
Steven Rostedt (Red Hat) | a4543a2 | 2015-05-29 09:40:18 -0400 | [diff] [blame] | 3300 | again: |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3301 | info.add_timestamp = RB_ADD_STAMP_NONE; |
Steven Rostedt (Red Hat) | b7dc42f | 2015-09-03 08:57:12 -0400 | [diff] [blame] | 3302 | info.delta = 0; |
| 3303 | |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3304 | /* |
| 3305 | * We allow for interrupts to reenter here and do a trace. |
| 3306 | * If one does, it will cause this original code to loop |
| 3307 | * back here. Even with heavy interrupts happening, this |
| 3308 | * should only happen a few times in a row. If this happens |
| 3309 | * 1000 times in a row, there must be either an interrupt |
| 3310 | * storm or we have something buggy. |
| 3311 | * Bail! |
| 3312 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 3313 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000)) |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3314 | goto out_fail; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3315 | |
Steven Rostedt (Red Hat) | fcc742e | 2015-05-28 17:13:14 -0400 | [diff] [blame] | 3316 | event = __rb_reserve_next(cpu_buffer, &info); |
| 3317 | |
Steven Rostedt (Red Hat) | bd1b7cd | 2015-11-23 17:35:24 -0500 | [diff] [blame] | 3318 | if (unlikely(PTR_ERR(event) == -EAGAIN)) { |
| 3319 | if (info.add_timestamp) |
| 3320 | info.length -= RB_LEN_TIME_EXTEND; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3321 | goto again; |
Steven Rostedt (Red Hat) | bd1b7cd | 2015-11-23 17:35:24 -0500 | [diff] [blame] | 3322 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3323 | |
Steven Rostedt (VMware) | a389d86 | 2020-06-28 22:52:25 -0400 | [diff] [blame] | 3324 | if (likely(event)) |
| 3325 | return event; |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3326 | out_fail: |
| 3327 | rb_end_commit(cpu_buffer); |
| 3328 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3329 | } |
| 3330 | |
| 3331 | /** |
| 3332 | * ring_buffer_lock_reserve - reserve a part of the buffer |
| 3333 | * @buffer: the ring buffer to reserve from |
| 3334 | * @length: the length of the data to reserve (excluding event header) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3335 | * |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 3336 | * Returns a reserved event on the ring buffer to copy directly to. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3337 | * The user of this interface will need to get the body to write into |
| 3338 | * and can use the ring_buffer_event_data() interface. |
| 3339 | * |
| 3340 | * The length is the length of the data needed, not the event length |
| 3341 | * which also includes the event header. |
| 3342 | * |
| 3343 | * Must be paired with ring_buffer_unlock_commit, unless NULL is returned. |
| 3344 | * If NULL is returned, then nothing has been allocated or locked. |
| 3345 | */ |
| 3346 | struct ring_buffer_event * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3347 | ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3348 | { |
| 3349 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3350 | struct ring_buffer_event *event; |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3351 | int cpu; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3352 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3353 | /* If we are tracing schedule, we don't want to recurse */ |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3354 | preempt_disable_notrace(); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3355 | |
Steven Rostedt (Red Hat) | 3205f80 | 2015-05-21 17:39:29 -0400 | [diff] [blame] | 3356 | if (unlikely(atomic_read(&buffer->record_disabled))) |
Steven Rostedt (Red Hat) | 58a09ec | 2015-05-27 10:27:47 -0400 | [diff] [blame] | 3357 | goto out; |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame] | 3358 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3359 | cpu = raw_smp_processor_id(); |
| 3360 | |
Steven Rostedt (Red Hat) | 3205f80 | 2015-05-21 17:39:29 -0400 | [diff] [blame] | 3361 | if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask))) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3362 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3363 | |
| 3364 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3365 | |
Steven Rostedt (Red Hat) | 3205f80 | 2015-05-21 17:39:29 -0400 | [diff] [blame] | 3366 | if (unlikely(atomic_read(&cpu_buffer->record_disabled))) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3367 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3368 | |
Steven Rostedt (Red Hat) | 3205f80 | 2015-05-21 17:39:29 -0400 | [diff] [blame] | 3369 | if (unlikely(length > BUF_MAX_DATA_SIZE)) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3370 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3371 | |
Steven Rostedt (Red Hat) | 58a09ec | 2015-05-27 10:27:47 -0400 | [diff] [blame] | 3372 | if (unlikely(trace_recursive_lock(cpu_buffer))) |
| 3373 | goto out; |
| 3374 | |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 3375 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3376 | if (!event) |
Steven Rostedt (Red Hat) | 58a09ec | 2015-05-27 10:27:47 -0400 | [diff] [blame] | 3377 | goto out_unlock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3378 | |
| 3379 | return event; |
| 3380 | |
Steven Rostedt (Red Hat) | 58a09ec | 2015-05-27 10:27:47 -0400 | [diff] [blame] | 3381 | out_unlock: |
| 3382 | trace_recursive_unlock(cpu_buffer); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3383 | out: |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3384 | preempt_enable_notrace(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3385 | return NULL; |
| 3386 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3387 | EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3388 | |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 3389 | /* |
| 3390 | * Decrement the entries to the page that an event is on. |
| 3391 | * The event does not even need to exist, only the pointer |
| 3392 | * to the page it is on. This may only be called before the commit |
| 3393 | * takes place. |
| 3394 | */ |
| 3395 | static inline void |
| 3396 | rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer, |
| 3397 | struct ring_buffer_event *event) |
| 3398 | { |
| 3399 | unsigned long addr = (unsigned long)event; |
| 3400 | struct buffer_page *bpage = cpu_buffer->commit_page; |
| 3401 | struct buffer_page *start; |
| 3402 | |
| 3403 | addr &= PAGE_MASK; |
| 3404 | |
| 3405 | /* Do the likely case first */ |
| 3406 | if (likely(bpage->page == (void *)addr)) { |
| 3407 | local_dec(&bpage->entries); |
| 3408 | return; |
| 3409 | } |
| 3410 | |
| 3411 | /* |
| 3412 | * Because the commit page may be on the reader page we |
| 3413 | * start with the next page and check the end loop there. |
| 3414 | */ |
| 3415 | rb_inc_page(cpu_buffer, &bpage); |
| 3416 | start = bpage; |
| 3417 | do { |
| 3418 | if (bpage->page == (void *)addr) { |
| 3419 | local_dec(&bpage->entries); |
| 3420 | return; |
| 3421 | } |
| 3422 | rb_inc_page(cpu_buffer, &bpage); |
| 3423 | } while (bpage != start); |
| 3424 | |
| 3425 | /* commit not part of this buffer?? */ |
| 3426 | RB_WARN_ON(cpu_buffer, 1); |
| 3427 | } |
| 3428 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3429 | /** |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3430 | * ring_buffer_commit_discard - discard an event that has not been committed |
| 3431 | * @buffer: the ring buffer |
| 3432 | * @event: non committed event to discard |
| 3433 | * |
Steven Rostedt | dc892f7 | 2009-09-03 15:33:41 -0400 | [diff] [blame] | 3434 | * Sometimes an event that is in the ring buffer needs to be ignored. |
| 3435 | * This function lets the user discard an event in the ring buffer |
| 3436 | * and then that event will not be read later. |
| 3437 | * |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 3438 | * 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] | 3439 | * committed. It will try to free the event from the ring buffer |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3440 | * if another event has not been added behind it. |
| 3441 | * |
| 3442 | * If another event has been added behind it, it will set the event |
| 3443 | * up as discarded, and perform the commit. |
| 3444 | * |
| 3445 | * If this function is called, do not call ring_buffer_unlock_commit on |
| 3446 | * the event. |
| 3447 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3448 | void ring_buffer_discard_commit(struct trace_buffer *buffer, |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3449 | struct ring_buffer_event *event) |
| 3450 | { |
| 3451 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3452 | int cpu; |
| 3453 | |
| 3454 | /* The event is discarded regardless */ |
Frederic Weisbecker | f3b9aae | 2009-04-19 23:39:33 +0200 | [diff] [blame] | 3455 | rb_event_discard(event); |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3456 | |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3457 | cpu = smp_processor_id(); |
| 3458 | cpu_buffer = buffer->buffers[cpu]; |
| 3459 | |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3460 | /* |
| 3461 | * This must only be called if the event has not been |
| 3462 | * committed yet. Thus we can assume that preemption |
| 3463 | * is still disabled. |
| 3464 | */ |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3465 | RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing)); |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3466 | |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 3467 | rb_decrement_entry(cpu_buffer, event); |
Steven Rostedt | 0f2541d | 2009-08-05 12:02:48 -0400 | [diff] [blame] | 3468 | if (rb_try_to_discard(cpu_buffer, event)) |
Steven Rostedt | edd813bf | 2009-06-02 23:00:53 -0400 | [diff] [blame] | 3469 | goto out; |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3470 | |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3471 | out: |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3472 | rb_end_commit(cpu_buffer); |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3473 | |
Steven Rostedt (Red Hat) | 58a09ec | 2015-05-27 10:27:47 -0400 | [diff] [blame] | 3474 | trace_recursive_unlock(cpu_buffer); |
Frederic Weisbecker | f3b9aae | 2009-04-19 23:39:33 +0200 | [diff] [blame] | 3475 | |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3476 | preempt_enable_notrace(); |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 3477 | |
| 3478 | } |
| 3479 | EXPORT_SYMBOL_GPL(ring_buffer_discard_commit); |
| 3480 | |
| 3481 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3482 | * ring_buffer_write - write data to the buffer without reserving |
| 3483 | * @buffer: The ring buffer to write to. |
| 3484 | * @length: The length of the data being written (excluding the event header) |
| 3485 | * @data: The data to write to the buffer. |
| 3486 | * |
| 3487 | * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as |
| 3488 | * one function. If you already have the data to write to the buffer, it |
| 3489 | * may be easier to simply call this function. |
| 3490 | * |
| 3491 | * Note, like ring_buffer_lock_reserve, the length is the length of the data |
| 3492 | * and not the length of the event which would hold the header. |
| 3493 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3494 | int ring_buffer_write(struct trace_buffer *buffer, |
David Sharp | 01e3e71 | 2012-06-07 16:46:24 -0700 | [diff] [blame] | 3495 | unsigned long length, |
| 3496 | void *data) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3497 | { |
| 3498 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3499 | struct ring_buffer_event *event; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3500 | void *body; |
| 3501 | int ret = -EBUSY; |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3502 | int cpu; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3503 | |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3504 | preempt_disable_notrace(); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3505 | |
Lai Jiangshan | 52fbe9c | 2010-03-08 14:50:43 +0800 | [diff] [blame] | 3506 | if (atomic_read(&buffer->record_disabled)) |
| 3507 | goto out; |
| 3508 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3509 | cpu = raw_smp_processor_id(); |
| 3510 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3511 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3512 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3513 | |
| 3514 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3515 | |
| 3516 | if (atomic_read(&cpu_buffer->record_disabled)) |
| 3517 | goto out; |
| 3518 | |
Steven Rostedt | be957c4 | 2009-05-11 14:42:53 -0400 | [diff] [blame] | 3519 | if (length > BUF_MAX_DATA_SIZE) |
| 3520 | goto out; |
| 3521 | |
Steven Rostedt (Red Hat) | 985e871 | 2015-05-27 10:48:56 -0400 | [diff] [blame] | 3522 | if (unlikely(trace_recursive_lock(cpu_buffer))) |
| 3523 | goto out; |
| 3524 | |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 3525 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3526 | if (!event) |
Steven Rostedt (Red Hat) | 985e871 | 2015-05-27 10:48:56 -0400 | [diff] [blame] | 3527 | goto out_unlock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3528 | |
| 3529 | body = rb_event_data(event); |
| 3530 | |
| 3531 | memcpy(body, data, length); |
| 3532 | |
| 3533 | rb_commit(cpu_buffer, event); |
| 3534 | |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 3535 | rb_wakeups(buffer, cpu_buffer); |
| 3536 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3537 | ret = 0; |
Steven Rostedt (Red Hat) | 985e871 | 2015-05-27 10:48:56 -0400 | [diff] [blame] | 3538 | |
| 3539 | out_unlock: |
| 3540 | trace_recursive_unlock(cpu_buffer); |
| 3541 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3542 | out: |
Steven Rostedt | 5168ae5 | 2010-06-03 09:36:50 -0400 | [diff] [blame] | 3543 | preempt_enable_notrace(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3544 | |
| 3545 | return ret; |
| 3546 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3547 | EXPORT_SYMBOL_GPL(ring_buffer_write); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3548 | |
Yaowei Bai | da58834 | 2015-09-29 22:43:33 +0800 | [diff] [blame] | 3549 | 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] | 3550 | { |
| 3551 | struct buffer_page *reader = cpu_buffer->reader_page; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3552 | struct buffer_page *head = rb_set_head_page(cpu_buffer); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3553 | struct buffer_page *commit = cpu_buffer->commit_page; |
| 3554 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3555 | /* In case of error, head will be NULL */ |
| 3556 | if (unlikely(!head)) |
Yaowei Bai | da58834 | 2015-09-29 22:43:33 +0800 | [diff] [blame] | 3557 | return true; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3558 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3559 | return reader->read == rb_page_commit(reader) && |
| 3560 | (commit == reader || |
| 3561 | (commit == head && |
| 3562 | head->read == rb_page_commit(commit))); |
| 3563 | } |
| 3564 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3565 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3566 | * ring_buffer_record_disable - stop all writes into the buffer |
| 3567 | * @buffer: The ring buffer to stop writes to. |
| 3568 | * |
| 3569 | * This prevents all writes to the buffer. Any attempt to write |
| 3570 | * to the buffer after this will fail and return NULL. |
| 3571 | * |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 3572 | * The caller should call synchronize_rcu() after this. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3573 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3574 | void ring_buffer_record_disable(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3575 | { |
| 3576 | atomic_inc(&buffer->record_disabled); |
| 3577 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3578 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3579 | |
| 3580 | /** |
| 3581 | * ring_buffer_record_enable - enable writes to the buffer |
| 3582 | * @buffer: The ring buffer to enable writes |
| 3583 | * |
| 3584 | * Note, multiple disables will need the same number of enables |
Adam Buchbinder | c41b20e | 2009-12-11 16:35:39 -0500 | [diff] [blame] | 3585 | * to truly enable the writing (much like preempt_disable). |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3586 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3587 | void ring_buffer_record_enable(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3588 | { |
| 3589 | atomic_dec(&buffer->record_disabled); |
| 3590 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3591 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3592 | |
| 3593 | /** |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 3594 | * ring_buffer_record_off - stop all writes into the buffer |
| 3595 | * @buffer: The ring buffer to stop writes to. |
| 3596 | * |
| 3597 | * This prevents all writes to the buffer. Any attempt to write |
| 3598 | * to the buffer after this will fail and return NULL. |
| 3599 | * |
| 3600 | * This is different than ring_buffer_record_disable() as |
Wang Tianhong | 87abb3b | 2012-08-02 14:02:00 +0800 | [diff] [blame] | 3601 | * it works like an on/off switch, where as the disable() version |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 3602 | * must be paired with a enable(). |
| 3603 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3604 | void ring_buffer_record_off(struct trace_buffer *buffer) |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 3605 | { |
| 3606 | unsigned int rd; |
| 3607 | unsigned int new_rd; |
| 3608 | |
| 3609 | do { |
| 3610 | rd = atomic_read(&buffer->record_disabled); |
| 3611 | new_rd = rd | RB_BUFFER_OFF; |
| 3612 | } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd); |
| 3613 | } |
| 3614 | EXPORT_SYMBOL_GPL(ring_buffer_record_off); |
| 3615 | |
| 3616 | /** |
| 3617 | * ring_buffer_record_on - restart writes into the buffer |
| 3618 | * @buffer: The ring buffer to start writes to. |
| 3619 | * |
| 3620 | * This enables all writes to the buffer that was disabled by |
| 3621 | * ring_buffer_record_off(). |
| 3622 | * |
| 3623 | * This is different than ring_buffer_record_enable() as |
Wang Tianhong | 87abb3b | 2012-08-02 14:02:00 +0800 | [diff] [blame] | 3624 | * it works like an on/off switch, where as the enable() version |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 3625 | * must be paired with a disable(). |
| 3626 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3627 | void ring_buffer_record_on(struct trace_buffer *buffer) |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 3628 | { |
| 3629 | unsigned int rd; |
| 3630 | unsigned int new_rd; |
| 3631 | |
| 3632 | do { |
| 3633 | rd = atomic_read(&buffer->record_disabled); |
| 3634 | new_rd = rd & ~RB_BUFFER_OFF; |
| 3635 | } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd); |
| 3636 | } |
| 3637 | EXPORT_SYMBOL_GPL(ring_buffer_record_on); |
| 3638 | |
| 3639 | /** |
| 3640 | * ring_buffer_record_is_on - return true if the ring buffer can write |
| 3641 | * @buffer: The ring buffer to see if write is enabled |
| 3642 | * |
| 3643 | * Returns true if the ring buffer is in a state that it accepts writes. |
| 3644 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3645 | bool ring_buffer_record_is_on(struct trace_buffer *buffer) |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 3646 | { |
| 3647 | return !atomic_read(&buffer->record_disabled); |
| 3648 | } |
| 3649 | |
| 3650 | /** |
Masami Hiramatsu | 73c8d89 | 2018-07-14 01:28:15 +0900 | [diff] [blame] | 3651 | * ring_buffer_record_is_set_on - return true if the ring buffer is set writable |
| 3652 | * @buffer: The ring buffer to see if write is set enabled |
| 3653 | * |
| 3654 | * Returns true if the ring buffer is set writable by ring_buffer_record_on(). |
| 3655 | * Note that this does NOT mean it is in a writable state. |
| 3656 | * |
| 3657 | * It may return true when the ring buffer has been disabled by |
| 3658 | * ring_buffer_record_disable(), as that is a temporary disabling of |
| 3659 | * the ring buffer. |
| 3660 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3661 | bool ring_buffer_record_is_set_on(struct trace_buffer *buffer) |
Masami Hiramatsu | 73c8d89 | 2018-07-14 01:28:15 +0900 | [diff] [blame] | 3662 | { |
| 3663 | return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF); |
| 3664 | } |
| 3665 | |
| 3666 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3667 | * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer |
| 3668 | * @buffer: The ring buffer to stop writes to. |
| 3669 | * @cpu: The CPU buffer to stop |
| 3670 | * |
| 3671 | * This prevents all writes to the buffer. Any attempt to write |
| 3672 | * to the buffer after this will fail and return NULL. |
| 3673 | * |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 3674 | * The caller should call synchronize_rcu() after this. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3675 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3676 | void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3677 | { |
| 3678 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3679 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3680 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3681 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3682 | |
| 3683 | cpu_buffer = buffer->buffers[cpu]; |
| 3684 | atomic_inc(&cpu_buffer->record_disabled); |
| 3685 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3686 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3687 | |
| 3688 | /** |
| 3689 | * ring_buffer_record_enable_cpu - enable writes to the buffer |
| 3690 | * @buffer: The ring buffer to enable writes |
| 3691 | * @cpu: The CPU to enable. |
| 3692 | * |
| 3693 | * Note, multiple disables will need the same number of enables |
Adam Buchbinder | c41b20e | 2009-12-11 16:35:39 -0500 | [diff] [blame] | 3694 | * to truly enable the writing (much like preempt_disable). |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3695 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3696 | void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3697 | { |
| 3698 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3699 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3700 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3701 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3702 | |
| 3703 | cpu_buffer = buffer->buffers[cpu]; |
| 3704 | atomic_dec(&cpu_buffer->record_disabled); |
| 3705 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3706 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3707 | |
Steven Rostedt | f6195aa | 2010-09-01 12:23:12 -0400 | [diff] [blame] | 3708 | /* |
| 3709 | * The total entries in the ring buffer is the running counter |
| 3710 | * of entries entered into the ring buffer, minus the sum of |
| 3711 | * the entries read from the ring buffer and the number of |
| 3712 | * entries that were overwritten. |
| 3713 | */ |
| 3714 | static inline unsigned long |
| 3715 | rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer) |
| 3716 | { |
| 3717 | return local_read(&cpu_buffer->entries) - |
| 3718 | (local_read(&cpu_buffer->overrun) + cpu_buffer->read); |
| 3719 | } |
| 3720 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3721 | /** |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 3722 | * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer |
| 3723 | * @buffer: The ring buffer |
| 3724 | * @cpu: The per CPU buffer to read from. |
| 3725 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3726 | u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu) |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 3727 | { |
| 3728 | unsigned long flags; |
| 3729 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3730 | struct buffer_page *bpage; |
Linus Torvalds | da830e5 | 2012-12-11 18:18:58 -0800 | [diff] [blame] | 3731 | u64 ret = 0; |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 3732 | |
| 3733 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 3734 | return 0; |
| 3735 | |
| 3736 | cpu_buffer = buffer->buffers[cpu]; |
Linus Torvalds | 7115e3f | 2011-10-26 17:03:38 +0200 | [diff] [blame] | 3737 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 3738 | /* |
| 3739 | * if the tail is on reader_page, oldest time stamp is on the reader |
| 3740 | * page |
| 3741 | */ |
| 3742 | if (cpu_buffer->tail_page == cpu_buffer->reader_page) |
| 3743 | bpage = cpu_buffer->reader_page; |
| 3744 | else |
| 3745 | bpage = rb_set_head_page(cpu_buffer); |
Steven Rostedt | 54f7be5 | 2012-11-29 22:27:22 -0500 | [diff] [blame] | 3746 | if (bpage) |
| 3747 | ret = bpage->page->time_stamp; |
Linus Torvalds | 7115e3f | 2011-10-26 17:03:38 +0200 | [diff] [blame] | 3748 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 3749 | |
| 3750 | return ret; |
| 3751 | } |
| 3752 | EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts); |
| 3753 | |
| 3754 | /** |
| 3755 | * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer |
| 3756 | * @buffer: The ring buffer |
| 3757 | * @cpu: The per CPU buffer to read from. |
| 3758 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3759 | unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu) |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 3760 | { |
| 3761 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3762 | unsigned long ret; |
| 3763 | |
| 3764 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 3765 | return 0; |
| 3766 | |
| 3767 | cpu_buffer = buffer->buffers[cpu]; |
| 3768 | ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes; |
| 3769 | |
| 3770 | return ret; |
| 3771 | } |
| 3772 | EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu); |
| 3773 | |
| 3774 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3775 | * ring_buffer_entries_cpu - get the number of entries in a cpu buffer |
| 3776 | * @buffer: The ring buffer |
| 3777 | * @cpu: The per CPU buffer to get the entries from. |
| 3778 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3779 | unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3780 | { |
| 3781 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3782 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3783 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3784 | return 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3785 | |
| 3786 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3787 | |
Steven Rostedt | f6195aa | 2010-09-01 12:23:12 -0400 | [diff] [blame] | 3788 | return rb_num_of_entries(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3789 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3790 | EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3791 | |
| 3792 | /** |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 3793 | * ring_buffer_overrun_cpu - get the number of overruns caused by the ring |
| 3794 | * buffer wrapping around (only if RB_FL_OVERWRITE is on). |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3795 | * @buffer: The ring buffer |
| 3796 | * @cpu: The per CPU buffer to get the number of overruns from |
| 3797 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3798 | unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3799 | { |
| 3800 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3801 | unsigned long ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3802 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3803 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3804 | return 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3805 | |
| 3806 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3807 | ret = local_read(&cpu_buffer->overrun); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3808 | |
| 3809 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3810 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3811 | EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3812 | |
| 3813 | /** |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 3814 | * ring_buffer_commit_overrun_cpu - get the number of overruns caused by |
| 3815 | * commits failing due to the buffer wrapping around while there are uncommitted |
| 3816 | * events, such as during an interrupt storm. |
Steven Rostedt | f0d2c68 | 2009-04-29 13:43:37 -0400 | [diff] [blame] | 3817 | * @buffer: The ring buffer |
| 3818 | * @cpu: The per CPU buffer to get the number of overruns from |
| 3819 | */ |
| 3820 | unsigned long |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3821 | ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | f0d2c68 | 2009-04-29 13:43:37 -0400 | [diff] [blame] | 3822 | { |
| 3823 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3824 | unsigned long ret; |
| 3825 | |
| 3826 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 3827 | return 0; |
| 3828 | |
| 3829 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3830 | ret = local_read(&cpu_buffer->commit_overrun); |
Steven Rostedt | f0d2c68 | 2009-04-29 13:43:37 -0400 | [diff] [blame] | 3831 | |
| 3832 | return ret; |
| 3833 | } |
| 3834 | EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu); |
| 3835 | |
| 3836 | /** |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 3837 | * ring_buffer_dropped_events_cpu - get the number of dropped events caused by |
| 3838 | * the ring buffer filling up (only if RB_FL_OVERWRITE is off). |
| 3839 | * @buffer: The ring buffer |
| 3840 | * @cpu: The per CPU buffer to get the number of overruns from |
| 3841 | */ |
| 3842 | unsigned long |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3843 | ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu) |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 3844 | { |
| 3845 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3846 | unsigned long ret; |
| 3847 | |
| 3848 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 3849 | return 0; |
| 3850 | |
| 3851 | cpu_buffer = buffer->buffers[cpu]; |
| 3852 | ret = local_read(&cpu_buffer->dropped_events); |
| 3853 | |
| 3854 | return ret; |
| 3855 | } |
| 3856 | EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu); |
| 3857 | |
| 3858 | /** |
Steven Rostedt (Red Hat) | ad96470 | 2013-01-29 17:45:49 -0500 | [diff] [blame] | 3859 | * ring_buffer_read_events_cpu - get the number of events successfully read |
| 3860 | * @buffer: The ring buffer |
| 3861 | * @cpu: The per CPU buffer to get the number of events read |
| 3862 | */ |
| 3863 | unsigned long |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3864 | 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] | 3865 | { |
| 3866 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3867 | |
| 3868 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 3869 | return 0; |
| 3870 | |
| 3871 | cpu_buffer = buffer->buffers[cpu]; |
| 3872 | return cpu_buffer->read; |
| 3873 | } |
| 3874 | EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu); |
| 3875 | |
| 3876 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3877 | * ring_buffer_entries - get the number of entries in a buffer |
| 3878 | * @buffer: The ring buffer |
| 3879 | * |
| 3880 | * Returns the total number of entries in the ring buffer |
| 3881 | * (all CPU entries) |
| 3882 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3883 | unsigned long ring_buffer_entries(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3884 | { |
| 3885 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3886 | unsigned long entries = 0; |
| 3887 | int cpu; |
| 3888 | |
| 3889 | /* if you care about this being correct, lock the buffer */ |
| 3890 | for_each_buffer_cpu(buffer, cpu) { |
| 3891 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | f6195aa | 2010-09-01 12:23:12 -0400 | [diff] [blame] | 3892 | entries += rb_num_of_entries(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3893 | } |
| 3894 | |
| 3895 | return entries; |
| 3896 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3897 | EXPORT_SYMBOL_GPL(ring_buffer_entries); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3898 | |
| 3899 | /** |
Jiri Olsa | 67b394f | 2009-10-23 19:36:18 -0400 | [diff] [blame] | 3900 | * ring_buffer_overruns - get the number of overruns in buffer |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3901 | * @buffer: The ring buffer |
| 3902 | * |
| 3903 | * Returns the total number of overruns in the ring buffer |
| 3904 | * (all CPU entries) |
| 3905 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 3906 | unsigned long ring_buffer_overruns(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3907 | { |
| 3908 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3909 | unsigned long overruns = 0; |
| 3910 | int cpu; |
| 3911 | |
| 3912 | /* if you care about this being correct, lock the buffer */ |
| 3913 | for_each_buffer_cpu(buffer, cpu) { |
| 3914 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3915 | overruns += local_read(&cpu_buffer->overrun); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | return overruns; |
| 3919 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3920 | EXPORT_SYMBOL_GPL(ring_buffer_overruns); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3921 | |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 3922 | static void rb_iter_reset(struct ring_buffer_iter *iter) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3923 | { |
| 3924 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 3925 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3926 | /* Iterator usage is expected to have record disabled */ |
Steven Rostedt (Red Hat) | 651e22f | 2014-08-06 14:11:33 -0400 | [diff] [blame] | 3927 | iter->head_page = cpu_buffer->reader_page; |
| 3928 | iter->head = cpu_buffer->reader_page->read; |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 3929 | iter->next_event = iter->head; |
Steven Rostedt (Red Hat) | 651e22f | 2014-08-06 14:11:33 -0400 | [diff] [blame] | 3930 | |
| 3931 | iter->cache_reader_page = iter->head_page; |
Steven Rostedt (Red Hat) | 24607f1 | 2014-10-02 16:51:18 -0400 | [diff] [blame] | 3932 | iter->cache_read = cpu_buffer->read; |
Steven Rostedt (Red Hat) | 651e22f | 2014-08-06 14:11:33 -0400 | [diff] [blame] | 3933 | |
Steven Rostedt (VMware) | 28e3fc5 | 2020-03-17 17:32:26 -0400 | [diff] [blame] | 3934 | if (iter->head) { |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3935 | iter->read_stamp = cpu_buffer->read_stamp; |
Steven Rostedt (VMware) | 28e3fc5 | 2020-03-17 17:32:26 -0400 | [diff] [blame] | 3936 | iter->page_stamp = cpu_buffer->reader_page->page->time_stamp; |
| 3937 | } else { |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 3938 | iter->read_stamp = iter->head_page->page->time_stamp; |
Steven Rostedt (VMware) | 28e3fc5 | 2020-03-17 17:32:26 -0400 | [diff] [blame] | 3939 | iter->page_stamp = iter->read_stamp; |
| 3940 | } |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 3941 | } |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3942 | |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 3943 | /** |
| 3944 | * ring_buffer_iter_reset - reset an iterator |
| 3945 | * @iter: The iterator to reset |
| 3946 | * |
| 3947 | * Resets the iterator, so that it will start from the beginning |
| 3948 | * again. |
| 3949 | */ |
| 3950 | void ring_buffer_iter_reset(struct ring_buffer_iter *iter) |
| 3951 | { |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3952 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 3953 | unsigned long flags; |
| 3954 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3955 | if (!iter) |
| 3956 | return; |
| 3957 | |
| 3958 | cpu_buffer = iter->cpu_buffer; |
| 3959 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 3960 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 3961 | rb_iter_reset(iter); |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 3962 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3963 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3964 | EXPORT_SYMBOL_GPL(ring_buffer_iter_reset); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3965 | |
| 3966 | /** |
| 3967 | * ring_buffer_iter_empty - check if an iterator has no more to read |
| 3968 | * @iter: The iterator to check |
| 3969 | */ |
| 3970 | int ring_buffer_iter_empty(struct ring_buffer_iter *iter) |
| 3971 | { |
| 3972 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt (VMware) | 78f7a45 | 2017-04-19 14:29:46 -0400 | [diff] [blame] | 3973 | struct buffer_page *reader; |
| 3974 | struct buffer_page *head_page; |
| 3975 | struct buffer_page *commit_page; |
Steven Rostedt (VMware) | ead6ecf | 2020-03-17 17:32:24 -0400 | [diff] [blame] | 3976 | struct buffer_page *curr_commit_page; |
Steven Rostedt (VMware) | 78f7a45 | 2017-04-19 14:29:46 -0400 | [diff] [blame] | 3977 | unsigned commit; |
Steven Rostedt (VMware) | ead6ecf | 2020-03-17 17:32:24 -0400 | [diff] [blame] | 3978 | u64 curr_commit_ts; |
| 3979 | u64 commit_ts; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3980 | |
| 3981 | cpu_buffer = iter->cpu_buffer; |
Steven Rostedt (VMware) | 78f7a45 | 2017-04-19 14:29:46 -0400 | [diff] [blame] | 3982 | reader = cpu_buffer->reader_page; |
| 3983 | head_page = cpu_buffer->head_page; |
| 3984 | commit_page = cpu_buffer->commit_page; |
Steven Rostedt (VMware) | ead6ecf | 2020-03-17 17:32:24 -0400 | [diff] [blame] | 3985 | commit_ts = commit_page->page->time_stamp; |
Steven Rostedt (VMware) | 78f7a45 | 2017-04-19 14:29:46 -0400 | [diff] [blame] | 3986 | |
Steven Rostedt (VMware) | ead6ecf | 2020-03-17 17:32:24 -0400 | [diff] [blame] | 3987 | /* |
| 3988 | * When the writer goes across pages, it issues a cmpxchg which |
| 3989 | * is a mb(), which will synchronize with the rmb here. |
| 3990 | * (see rb_tail_page_update()) |
| 3991 | */ |
| 3992 | smp_rmb(); |
| 3993 | commit = rb_page_commit(commit_page); |
| 3994 | /* We want to make sure that the commit page doesn't change */ |
| 3995 | smp_rmb(); |
| 3996 | |
| 3997 | /* Make sure commit page didn't change */ |
| 3998 | curr_commit_page = READ_ONCE(cpu_buffer->commit_page); |
| 3999 | curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp); |
| 4000 | |
| 4001 | /* If the commit page changed, then there's more data */ |
| 4002 | if (curr_commit_page != commit_page || |
| 4003 | curr_commit_ts != commit_ts) |
| 4004 | return 0; |
| 4005 | |
| 4006 | /* 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] | 4007 | return ((iter->head_page == commit_page && iter->head >= commit) || |
Steven Rostedt (VMware) | 78f7a45 | 2017-04-19 14:29:46 -0400 | [diff] [blame] | 4008 | (iter->head_page == reader && commit_page == head_page && |
| 4009 | head_page->read == commit && |
| 4010 | iter->head == rb_page_commit(cpu_buffer->reader_page))); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4011 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4012 | EXPORT_SYMBOL_GPL(ring_buffer_iter_empty); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4013 | |
| 4014 | static void |
| 4015 | rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer, |
| 4016 | struct ring_buffer_event *event) |
| 4017 | { |
| 4018 | u64 delta; |
| 4019 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 4020 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4021 | case RINGBUF_TYPE_PADDING: |
| 4022 | return; |
| 4023 | |
| 4024 | case RINGBUF_TYPE_TIME_EXTEND: |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4025 | delta = ring_buffer_event_time_stamp(event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4026 | cpu_buffer->read_stamp += delta; |
| 4027 | return; |
| 4028 | |
| 4029 | case RINGBUF_TYPE_TIME_STAMP: |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4030 | delta = ring_buffer_event_time_stamp(event); |
| 4031 | cpu_buffer->read_stamp = delta; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4032 | return; |
| 4033 | |
| 4034 | case RINGBUF_TYPE_DATA: |
| 4035 | cpu_buffer->read_stamp += event->time_delta; |
| 4036 | return; |
| 4037 | |
| 4038 | default: |
Steven Rostedt (VMware) | da4d401 | 2020-05-13 15:36:22 -0400 | [diff] [blame] | 4039 | RB_WARN_ON(cpu_buffer, 1); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4040 | } |
| 4041 | return; |
| 4042 | } |
| 4043 | |
| 4044 | static void |
| 4045 | rb_update_iter_read_stamp(struct ring_buffer_iter *iter, |
| 4046 | struct ring_buffer_event *event) |
| 4047 | { |
| 4048 | u64 delta; |
| 4049 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 4050 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4051 | case RINGBUF_TYPE_PADDING: |
| 4052 | return; |
| 4053 | |
| 4054 | case RINGBUF_TYPE_TIME_EXTEND: |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4055 | delta = ring_buffer_event_time_stamp(event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4056 | iter->read_stamp += delta; |
| 4057 | return; |
| 4058 | |
| 4059 | case RINGBUF_TYPE_TIME_STAMP: |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4060 | delta = ring_buffer_event_time_stamp(event); |
| 4061 | iter->read_stamp = delta; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4062 | return; |
| 4063 | |
| 4064 | case RINGBUF_TYPE_DATA: |
| 4065 | iter->read_stamp += event->time_delta; |
| 4066 | return; |
| 4067 | |
| 4068 | default: |
Steven Rostedt (VMware) | da4d401 | 2020-05-13 15:36:22 -0400 | [diff] [blame] | 4069 | RB_WARN_ON(iter->cpu_buffer, 1); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4070 | } |
| 4071 | return; |
| 4072 | } |
| 4073 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4074 | static struct buffer_page * |
| 4075 | rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4076 | { |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4077 | struct buffer_page *reader = NULL; |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4078 | unsigned long overwrite; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4079 | unsigned long flags; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4080 | int nr_loops = 0; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4081 | int ret; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4082 | |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 4083 | local_irq_save(flags); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 4084 | arch_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4085 | |
| 4086 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4087 | /* |
| 4088 | * This should normally only loop twice. But because the |
| 4089 | * start of the reader inserts an empty page, it causes |
| 4090 | * a case where we will loop three times. There should be no |
| 4091 | * reason to loop four times (that I know of). |
| 4092 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 4093 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) { |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4094 | reader = NULL; |
| 4095 | goto out; |
| 4096 | } |
| 4097 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4098 | reader = cpu_buffer->reader_page; |
| 4099 | |
| 4100 | /* If there's more to read, return this page */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 4101 | if (cpu_buffer->reader_page->read < rb_page_size(reader)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4102 | goto out; |
| 4103 | |
| 4104 | /* Never should we have an index greater than the size */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 4105 | if (RB_WARN_ON(cpu_buffer, |
| 4106 | cpu_buffer->reader_page->read > rb_page_size(reader))) |
| 4107 | goto out; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4108 | |
| 4109 | /* check if we caught up to the tail */ |
| 4110 | reader = NULL; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 4111 | if (cpu_buffer->commit_page == cpu_buffer->reader_page) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4112 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4113 | |
Steven Rostedt | a5fb833 | 2012-06-28 13:35:04 -0400 | [diff] [blame] | 4114 | /* Don't bother swapping if the ring buffer is empty */ |
| 4115 | if (rb_num_of_entries(cpu_buffer) == 0) |
| 4116 | goto out; |
| 4117 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4118 | /* |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4119 | * Reset the reader page to size zero. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4120 | */ |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4121 | local_set(&cpu_buffer->reader_page->write, 0); |
| 4122 | local_set(&cpu_buffer->reader_page->entries, 0); |
| 4123 | local_set(&cpu_buffer->reader_page->page->commit, 0); |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 4124 | cpu_buffer->reader_page->real_end = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4125 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4126 | spin: |
| 4127 | /* |
| 4128 | * Splice the empty reader page into the list around the head. |
| 4129 | */ |
| 4130 | reader = rb_set_head_page(cpu_buffer); |
Steven Rostedt | 54f7be5 | 2012-11-29 22:27:22 -0500 | [diff] [blame] | 4131 | if (!reader) |
| 4132 | goto out; |
Steven Rostedt | 0e1ff5d | 2010-01-06 20:40:44 -0500 | [diff] [blame] | 4133 | cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4134 | cpu_buffer->reader_page->list.prev = reader->list.prev; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 4135 | |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 4136 | /* |
| 4137 | * cpu_buffer->pages just needs to point to the buffer, it |
| 4138 | * 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] | 4139 | * of our way so we don't accidentally swap it. |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 4140 | */ |
| 4141 | cpu_buffer->pages = reader->list.prev; |
| 4142 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4143 | /* The reader page will be pointing to the new head */ |
| 4144 | rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4145 | |
| 4146 | /* |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4147 | * We want to make sure we read the overruns after we set up our |
| 4148 | * pointers to the next object. The writer side does a |
| 4149 | * cmpxchg to cross pages which acts as the mb on the writer |
| 4150 | * side. Note, the reader will constantly fail the swap |
| 4151 | * while the writer is updating the pointers, so this |
| 4152 | * guarantees that the overwrite recorded here is the one we |
| 4153 | * want to compare with the last_overrun. |
| 4154 | */ |
| 4155 | smp_mb(); |
| 4156 | overwrite = local_read(&(cpu_buffer->overrun)); |
| 4157 | |
| 4158 | /* |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4159 | * Here's the tricky part. |
| 4160 | * |
| 4161 | * We need to move the pointer past the header page. |
| 4162 | * But we can only do that if a writer is not currently |
| 4163 | * moving it. The page before the header page has the |
| 4164 | * flag bit '1' set if it is pointing to the page we want. |
| 4165 | * but if the writer is in the process of moving it |
| 4166 | * than it will be '2' or already moved '0'. |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4167 | */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4168 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4169 | ret = rb_head_page_replace(reader, cpu_buffer->reader_page); |
| 4170 | |
| 4171 | /* |
| 4172 | * If we did not convert it, then we must try again. |
| 4173 | */ |
| 4174 | if (!ret) |
| 4175 | goto spin; |
| 4176 | |
| 4177 | /* |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 4178 | * Yay! We succeeded in replacing the page. |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4179 | * |
| 4180 | * Now make the new head point back to the reader page. |
| 4181 | */ |
David Sharp | 5ded3dc6 | 2010-01-06 17:12:07 -0800 | [diff] [blame] | 4182 | rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4183 | rb_inc_page(cpu_buffer, &cpu_buffer->head_page); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4184 | |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 4185 | local_inc(&cpu_buffer->pages_read); |
| 4186 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4187 | /* Finally update the reader page to the new head */ |
| 4188 | cpu_buffer->reader_page = reader; |
Steven Rostedt (Red Hat) | b81f472 | 2015-11-23 10:35:36 -0500 | [diff] [blame] | 4189 | cpu_buffer->reader_page->read = 0; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4190 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4191 | if (overwrite != cpu_buffer->last_overrun) { |
| 4192 | cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun; |
| 4193 | cpu_buffer->last_overrun = overwrite; |
| 4194 | } |
| 4195 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4196 | goto again; |
| 4197 | |
| 4198 | out: |
Steven Rostedt (Red Hat) | b81f472 | 2015-11-23 10:35:36 -0500 | [diff] [blame] | 4199 | /* Update the read_stamp on the first event */ |
| 4200 | if (reader && reader->read == 0) |
| 4201 | cpu_buffer->read_stamp = reader->page->time_stamp; |
| 4202 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 4203 | arch_spin_unlock(&cpu_buffer->lock); |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 4204 | local_irq_restore(flags); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4205 | |
| 4206 | return reader; |
| 4207 | } |
| 4208 | |
| 4209 | static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer) |
| 4210 | { |
| 4211 | struct ring_buffer_event *event; |
| 4212 | struct buffer_page *reader; |
| 4213 | unsigned length; |
| 4214 | |
| 4215 | reader = rb_get_reader_page(cpu_buffer); |
| 4216 | |
| 4217 | /* This function should not be called when buffer is empty */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 4218 | if (RB_WARN_ON(cpu_buffer, !reader)) |
| 4219 | return; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4220 | |
| 4221 | event = rb_reader_event(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4222 | |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 4223 | if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
Steven Rostedt | e4906ef | 2009-04-30 20:49:44 -0400 | [diff] [blame] | 4224 | cpu_buffer->read++; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4225 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4226 | rb_update_read_stamp(cpu_buffer, event); |
| 4227 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4228 | length = rb_event_length(event); |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 4229 | cpu_buffer->reader_page->read += length; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | static void rb_advance_iter(struct ring_buffer_iter *iter) |
| 4233 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4234 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4235 | |
| 4236 | cpu_buffer = iter->cpu_buffer; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4237 | |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4238 | /* If head == next_event then we need to jump to the next event */ |
| 4239 | if (iter->head == iter->next_event) { |
| 4240 | /* If the event gets overwritten again, there's nothing to do */ |
| 4241 | if (rb_iter_head_event(iter) == NULL) |
| 4242 | return; |
| 4243 | } |
| 4244 | |
| 4245 | iter->head = iter->next_event; |
| 4246 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4247 | /* |
| 4248 | * Check if we are at the end of the buffer. |
| 4249 | */ |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4250 | if (iter->next_event >= rb_page_size(iter->head_page)) { |
Steven Rostedt | ea05b57 | 2009-06-03 09:30:10 -0400 | [diff] [blame] | 4251 | /* discarded commits can make the page empty */ |
| 4252 | if (iter->head_page == cpu_buffer->commit_page) |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 4253 | return; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4254 | rb_inc_iter(iter); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4255 | return; |
| 4256 | } |
| 4257 | |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4258 | rb_update_iter_read_stamp(iter, iter->event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4259 | } |
| 4260 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4261 | static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer) |
| 4262 | { |
| 4263 | return cpu_buffer->lost_events; |
| 4264 | } |
| 4265 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4266 | static struct ring_buffer_event * |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4267 | rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts, |
| 4268 | unsigned long *lost_events) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4269 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4270 | struct ring_buffer_event *event; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4271 | struct buffer_page *reader; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4272 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4273 | |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4274 | if (ts) |
| 4275 | *ts = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4276 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4277 | /* |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 4278 | * We repeat when a time extend is encountered. |
| 4279 | * Since the time extend is always attached to a data event, |
| 4280 | * we should never loop more than once. |
| 4281 | * (We never hit the following condition more than twice). |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4282 | */ |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 4283 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2)) |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4284 | return NULL; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4285 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4286 | reader = rb_get_reader_page(cpu_buffer); |
| 4287 | if (!reader) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4288 | return NULL; |
| 4289 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4290 | event = rb_reader_event(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4291 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 4292 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4293 | case RINGBUF_TYPE_PADDING: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4294 | if (rb_null_event(event)) |
| 4295 | RB_WARN_ON(cpu_buffer, 1); |
| 4296 | /* |
| 4297 | * Because the writer could be discarding every |
| 4298 | * event it creates (which would probably be bad) |
| 4299 | * if we were to go back to "again" then we may never |
| 4300 | * catch up, and will trigger the warn on, or lock |
| 4301 | * the box. Return the padding, and we will release |
| 4302 | * the current locks, and try again. |
| 4303 | */ |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4304 | return event; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4305 | |
| 4306 | case RINGBUF_TYPE_TIME_EXTEND: |
| 4307 | /* Internal data, OK to advance */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4308 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4309 | goto again; |
| 4310 | |
| 4311 | case RINGBUF_TYPE_TIME_STAMP: |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4312 | if (ts) { |
| 4313 | *ts = ring_buffer_event_time_stamp(event); |
| 4314 | ring_buffer_normalize_time_stamp(cpu_buffer->buffer, |
| 4315 | cpu_buffer->cpu, ts); |
| 4316 | } |
| 4317 | /* Internal data, OK to advance */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4318 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4319 | goto again; |
| 4320 | |
| 4321 | case RINGBUF_TYPE_DATA: |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4322 | if (ts && !(*ts)) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4323 | *ts = cpu_buffer->read_stamp + event->time_delta; |
Robert Richter | d8eeb2d | 2009-07-31 14:58:04 +0200 | [diff] [blame] | 4324 | ring_buffer_normalize_time_stamp(cpu_buffer->buffer, |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 4325 | cpu_buffer->cpu, ts); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4326 | } |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4327 | if (lost_events) |
| 4328 | *lost_events = rb_lost_events(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4329 | return event; |
| 4330 | |
| 4331 | default: |
Steven Rostedt (VMware) | da4d401 | 2020-05-13 15:36:22 -0400 | [diff] [blame] | 4332 | RB_WARN_ON(cpu_buffer, 1); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | return NULL; |
| 4336 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4337 | EXPORT_SYMBOL_GPL(ring_buffer_peek); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4338 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4339 | static struct ring_buffer_event * |
| 4340 | rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4341 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4342 | struct trace_buffer *buffer; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4343 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4344 | struct ring_buffer_event *event; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4345 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4346 | |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4347 | if (ts) |
| 4348 | *ts = 0; |
| 4349 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4350 | cpu_buffer = iter->cpu_buffer; |
| 4351 | buffer = cpu_buffer->buffer; |
| 4352 | |
Steven Rostedt | 492a74f | 2010-01-25 15:17:47 -0500 | [diff] [blame] | 4353 | /* |
| 4354 | * Check if someone performed a consuming read to |
| 4355 | * the buffer. A consuming read invalidates the iterator |
| 4356 | * and we need to reset the iterator in this case. |
| 4357 | */ |
| 4358 | if (unlikely(iter->cache_read != cpu_buffer->read || |
| 4359 | iter->cache_reader_page != cpu_buffer->reader_page)) |
| 4360 | rb_iter_reset(iter); |
| 4361 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4362 | again: |
Steven Rostedt | 3c05d74 | 2010-01-26 16:14:08 -0500 | [diff] [blame] | 4363 | if (ring_buffer_iter_empty(iter)) |
| 4364 | return NULL; |
| 4365 | |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4366 | /* |
Steven Rostedt (VMware) | 3d2353d | 2020-05-13 15:18:01 -0400 | [diff] [blame] | 4367 | * As the writer can mess with what the iterator is trying |
| 4368 | * to read, just give up if we fail to get an event after |
| 4369 | * three tries. The iterator is not as reliable when reading |
| 4370 | * the ring buffer with an active write as the consumer is. |
| 4371 | * Do not warn if the three failures is reached. |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4372 | */ |
Steven Rostedt (VMware) | 3d2353d | 2020-05-13 15:18:01 -0400 | [diff] [blame] | 4373 | if (++nr_loops > 3) |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4374 | return NULL; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 4375 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4376 | if (rb_per_cpu_empty(cpu_buffer)) |
| 4377 | return NULL; |
| 4378 | |
Steven Rostedt (Red Hat) | 10e83fd | 2014-07-23 19:45:12 -0400 | [diff] [blame] | 4379 | if (iter->head >= rb_page_size(iter->head_page)) { |
Steven Rostedt | 3c05d74 | 2010-01-26 16:14:08 -0500 | [diff] [blame] | 4380 | rb_inc_iter(iter); |
| 4381 | goto again; |
| 4382 | } |
| 4383 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4384 | event = rb_iter_head_event(iter); |
Steven Rostedt (VMware) | 3d2353d | 2020-05-13 15:18:01 -0400 | [diff] [blame] | 4385 | if (!event) |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4386 | goto again; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4387 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 4388 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4389 | case RINGBUF_TYPE_PADDING: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4390 | if (rb_null_event(event)) { |
| 4391 | rb_inc_iter(iter); |
| 4392 | goto again; |
| 4393 | } |
| 4394 | rb_advance_iter(iter); |
| 4395 | return event; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4396 | |
| 4397 | case RINGBUF_TYPE_TIME_EXTEND: |
| 4398 | /* Internal data, OK to advance */ |
| 4399 | rb_advance_iter(iter); |
| 4400 | goto again; |
| 4401 | |
| 4402 | case RINGBUF_TYPE_TIME_STAMP: |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4403 | if (ts) { |
| 4404 | *ts = ring_buffer_event_time_stamp(event); |
| 4405 | ring_buffer_normalize_time_stamp(cpu_buffer->buffer, |
| 4406 | cpu_buffer->cpu, ts); |
| 4407 | } |
| 4408 | /* Internal data, OK to advance */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4409 | rb_advance_iter(iter); |
| 4410 | goto again; |
| 4411 | |
| 4412 | case RINGBUF_TYPE_DATA: |
Tom Zanussi | dc4e280 | 2018-01-15 20:51:40 -0600 | [diff] [blame] | 4413 | if (ts && !(*ts)) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4414 | *ts = iter->read_stamp + event->time_delta; |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 4415 | ring_buffer_normalize_time_stamp(buffer, |
| 4416 | cpu_buffer->cpu, ts); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4417 | } |
| 4418 | return event; |
| 4419 | |
| 4420 | default: |
Steven Rostedt (VMware) | da4d401 | 2020-05-13 15:36:22 -0400 | [diff] [blame] | 4421 | RB_WARN_ON(cpu_buffer, 1); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | return NULL; |
| 4425 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4426 | EXPORT_SYMBOL_GPL(ring_buffer_iter_peek); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4427 | |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4428 | 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] | 4429 | { |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4430 | if (likely(!in_nmi())) { |
| 4431 | raw_spin_lock(&cpu_buffer->reader_lock); |
| 4432 | return true; |
| 4433 | } |
| 4434 | |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4435 | /* |
| 4436 | * 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] | 4437 | * trylock must be used to prevent a deadlock if the NMI |
| 4438 | * preempted a task that holds the ring buffer locks. If |
| 4439 | * we get the lock then all is fine, if not, then continue |
| 4440 | * to do the read, but this can corrupt the ring buffer, |
| 4441 | * so it must be permanently disabled from future writes. |
| 4442 | * Reading from NMI is a oneshot deal. |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4443 | */ |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4444 | if (raw_spin_trylock(&cpu_buffer->reader_lock)) |
| 4445 | return true; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4446 | |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4447 | /* Continue without locking, but disable the ring buffer */ |
| 4448 | atomic_inc(&cpu_buffer->record_disabled); |
| 4449 | return false; |
| 4450 | } |
| 4451 | |
| 4452 | static inline void |
| 4453 | rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked) |
| 4454 | { |
| 4455 | if (likely(locked)) |
| 4456 | raw_spin_unlock(&cpu_buffer->reader_lock); |
| 4457 | return; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4458 | } |
| 4459 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4460 | /** |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4461 | * ring_buffer_peek - peek at the next event to be read |
| 4462 | * @buffer: The ring buffer to read |
| 4463 | * @cpu: The cpu to peak at |
| 4464 | * @ts: The timestamp counter of this event. |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4465 | * @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] | 4466 | * |
| 4467 | * This will return the event that will be read next, but does |
| 4468 | * not consume the data. |
| 4469 | */ |
| 4470 | struct ring_buffer_event * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4471 | ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts, |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4472 | unsigned long *lost_events) |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4473 | { |
| 4474 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4475 | struct ring_buffer_event *event; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4476 | unsigned long flags; |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4477 | bool dolock; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4478 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4479 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4480 | return NULL; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4481 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4482 | again: |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4483 | local_irq_save(flags); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4484 | dolock = rb_reader_lock(cpu_buffer); |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4485 | event = rb_buffer_peek(cpu_buffer, ts, lost_events); |
Robert Richter | 469535a | 2009-07-30 19:19:18 +0200 | [diff] [blame] | 4486 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
| 4487 | rb_advance_reader(cpu_buffer); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4488 | rb_reader_unlock(cpu_buffer, dolock); |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4489 | local_irq_restore(flags); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4490 | |
Steven Rostedt | 1b959e1 | 2009-09-03 10:12:13 -0400 | [diff] [blame] | 4491 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4492 | goto again; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4493 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4494 | return event; |
| 4495 | } |
| 4496 | |
Steven Rostedt (VMware) | c9b7a4a | 2020-03-17 17:32:32 -0400 | [diff] [blame] | 4497 | /** ring_buffer_iter_dropped - report if there are dropped events |
| 4498 | * @iter: The ring buffer iterator |
| 4499 | * |
| 4500 | * Returns true if there was dropped events since the last peek. |
| 4501 | */ |
| 4502 | bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter) |
| 4503 | { |
| 4504 | bool ret = iter->missed_events != 0; |
| 4505 | |
| 4506 | iter->missed_events = 0; |
| 4507 | return ret; |
| 4508 | } |
| 4509 | EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped); |
| 4510 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4511 | /** |
| 4512 | * ring_buffer_iter_peek - peek at the next event to be read |
| 4513 | * @iter: The ring buffer iterator |
| 4514 | * @ts: The timestamp counter of this event. |
| 4515 | * |
| 4516 | * This will return the event that will be read next, but does |
| 4517 | * not increment the iterator. |
| 4518 | */ |
| 4519 | struct ring_buffer_event * |
| 4520 | ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts) |
| 4521 | { |
| 4522 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 4523 | struct ring_buffer_event *event; |
| 4524 | unsigned long flags; |
| 4525 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4526 | again: |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4527 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4528 | event = rb_iter_peek(iter, ts); |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4529 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4530 | |
Steven Rostedt | 1b959e1 | 2009-09-03 10:12:13 -0400 | [diff] [blame] | 4531 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4532 | goto again; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4533 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4534 | return event; |
| 4535 | } |
| 4536 | |
| 4537 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4538 | * ring_buffer_consume - return an event and consume it |
| 4539 | * @buffer: The ring buffer to get the next event from |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4540 | * @cpu: the cpu to read the buffer from |
| 4541 | * @ts: a variable to store the timestamp (may be NULL) |
| 4542 | * @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] | 4543 | * |
| 4544 | * Returns the next event in the ring buffer, and that event is consumed. |
| 4545 | * Meaning, that sequential reads will keep returning a different event, |
| 4546 | * and eventually empty the ring buffer if the producer is slower. |
| 4547 | */ |
| 4548 | struct ring_buffer_event * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4549 | ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts, |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4550 | unsigned long *lost_events) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4551 | { |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4552 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4553 | struct ring_buffer_event *event = NULL; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4554 | unsigned long flags; |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4555 | bool dolock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4556 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4557 | again: |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4558 | /* might be called in atomic */ |
| 4559 | preempt_disable(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4560 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4561 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 4562 | goto out; |
| 4563 | |
| 4564 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4565 | local_irq_save(flags); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4566 | dolock = rb_reader_lock(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4567 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4568 | event = rb_buffer_peek(cpu_buffer, ts, lost_events); |
| 4569 | if (event) { |
| 4570 | cpu_buffer->lost_events = 0; |
Robert Richter | 469535a | 2009-07-30 19:19:18 +0200 | [diff] [blame] | 4571 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4572 | } |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4573 | |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4574 | rb_reader_unlock(cpu_buffer, dolock); |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4575 | local_irq_restore(flags); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4576 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4577 | out: |
| 4578 | preempt_enable(); |
| 4579 | |
Steven Rostedt | 1b959e1 | 2009-09-03 10:12:13 -0400 | [diff] [blame] | 4580 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4581 | goto again; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 4582 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4583 | return event; |
| 4584 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4585 | EXPORT_SYMBOL_GPL(ring_buffer_consume); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4586 | |
| 4587 | /** |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4588 | * 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] | 4589 | * @buffer: The ring buffer to read from |
| 4590 | * @cpu: The cpu buffer to iterate over |
Douglas Anderson | 31b265b | 2019-03-08 11:32:04 -0800 | [diff] [blame] | 4591 | * @flags: gfp flags to use for memory allocation |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4592 | * |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4593 | * This performs the initial preparations necessary to iterate |
| 4594 | * through the buffer. Memory is allocated, buffer recording |
| 4595 | * is disabled, and the iterator pointer is returned to the caller. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4596 | * |
Steven Rostedt (VMware) | 6167c20 | 2018-05-16 11:17:06 -0400 | [diff] [blame] | 4597 | * Disabling buffer recording prevents the reading from being |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4598 | * corrupted. This is not a consuming read, so a producer is not |
| 4599 | * expected. |
| 4600 | * |
| 4601 | * After a sequence of ring_buffer_read_prepare calls, the user is |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 4602 | * 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] | 4603 | * Afterwards, ring_buffer_read_start is invoked to get things going |
| 4604 | * for real. |
| 4605 | * |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 4606 | * This overall must be paired with ring_buffer_read_finish. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4607 | */ |
| 4608 | struct ring_buffer_iter * |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4609 | 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] | 4610 | { |
| 4611 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4612 | struct ring_buffer_iter *iter; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4613 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 4614 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4615 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4616 | |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4617 | iter = kzalloc(sizeof(*iter), flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4618 | if (!iter) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4619 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4620 | |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4621 | iter->event = kmalloc(BUF_MAX_DATA_SIZE, flags); |
| 4622 | if (!iter->event) { |
| 4623 | kfree(iter); |
| 4624 | return NULL; |
| 4625 | } |
| 4626 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4627 | cpu_buffer = buffer->buffers[cpu]; |
| 4628 | |
| 4629 | iter->cpu_buffer = cpu_buffer; |
| 4630 | |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 4631 | atomic_inc(&cpu_buffer->resize_disabled); |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4632 | |
| 4633 | return iter; |
| 4634 | } |
| 4635 | EXPORT_SYMBOL_GPL(ring_buffer_read_prepare); |
| 4636 | |
| 4637 | /** |
| 4638 | * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls |
| 4639 | * |
| 4640 | * All previously invoked ring_buffer_read_prepare calls to prepare |
| 4641 | * iterators will be synchronized. Afterwards, read_buffer_read_start |
| 4642 | * calls on those iterators are allowed. |
| 4643 | */ |
| 4644 | void |
| 4645 | ring_buffer_read_prepare_sync(void) |
| 4646 | { |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 4647 | synchronize_rcu(); |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4648 | } |
| 4649 | EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync); |
| 4650 | |
| 4651 | /** |
| 4652 | * ring_buffer_read_start - start a non consuming read of the buffer |
| 4653 | * @iter: The iterator returned by ring_buffer_read_prepare |
| 4654 | * |
| 4655 | * This finalizes the startup of an iteration through the buffer. |
| 4656 | * The iterator comes from a call to ring_buffer_read_prepare and |
| 4657 | * an intervening ring_buffer_read_prepare_sync must have been |
| 4658 | * performed. |
| 4659 | * |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 4660 | * Must be paired with ring_buffer_read_finish. |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 4661 | */ |
| 4662 | void |
| 4663 | ring_buffer_read_start(struct ring_buffer_iter *iter) |
| 4664 | { |
| 4665 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4666 | unsigned long flags; |
| 4667 | |
| 4668 | if (!iter) |
| 4669 | return; |
| 4670 | |
| 4671 | cpu_buffer = iter->cpu_buffer; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4672 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4673 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 4674 | arch_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 4675 | rb_iter_reset(iter); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 4676 | arch_spin_unlock(&cpu_buffer->lock); |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4677 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4678 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4679 | EXPORT_SYMBOL_GPL(ring_buffer_read_start); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4680 | |
| 4681 | /** |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 4682 | * ring_buffer_read_finish - finish reading the iterator of the buffer |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4683 | * @iter: The iterator retrieved by ring_buffer_start |
| 4684 | * |
| 4685 | * This re-enables the recording to the buffer, and frees the |
| 4686 | * iterator. |
| 4687 | */ |
| 4688 | void |
| 4689 | ring_buffer_read_finish(struct ring_buffer_iter *iter) |
| 4690 | { |
| 4691 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
Steven Rostedt | 9366c1b | 2012-11-29 22:31:16 -0500 | [diff] [blame] | 4692 | unsigned long flags; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4693 | |
Steven Rostedt | 659f451 | 2012-05-14 17:02:33 -0400 | [diff] [blame] | 4694 | /* |
| 4695 | * Ring buffer is disabled from recording, here's a good place |
Steven Rostedt | 9366c1b | 2012-11-29 22:31:16 -0500 | [diff] [blame] | 4696 | * to check the integrity of the ring buffer. |
| 4697 | * Must prevent readers from trying to read, as the check |
| 4698 | * clears the HEAD page and readers require it. |
Steven Rostedt | 659f451 | 2012-05-14 17:02:33 -0400 | [diff] [blame] | 4699 | */ |
Steven Rostedt | 9366c1b | 2012-11-29 22:31:16 -0500 | [diff] [blame] | 4700 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 659f451 | 2012-05-14 17:02:33 -0400 | [diff] [blame] | 4701 | rb_check_pages(cpu_buffer); |
Steven Rostedt | 9366c1b | 2012-11-29 22:31:16 -0500 | [diff] [blame] | 4702 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 659f451 | 2012-05-14 17:02:33 -0400 | [diff] [blame] | 4703 | |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 4704 | atomic_dec(&cpu_buffer->resize_disabled); |
Steven Rostedt (VMware) | 785888c | 2020-03-17 17:32:27 -0400 | [diff] [blame] | 4705 | kfree(iter->event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4706 | kfree(iter); |
| 4707 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4708 | EXPORT_SYMBOL_GPL(ring_buffer_read_finish); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4709 | |
| 4710 | /** |
Steven Rostedt (VMware) | bc1a72a | 2020-03-17 17:32:25 -0400 | [diff] [blame] | 4711 | * ring_buffer_iter_advance - advance the iterator to the next location |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4712 | * @iter: The ring buffer iterator |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4713 | * |
Steven Rostedt (VMware) | bc1a72a | 2020-03-17 17:32:25 -0400 | [diff] [blame] | 4714 | * Move the location of the iterator such that the next read will |
| 4715 | * be the next location of the iterator. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4716 | */ |
Steven Rostedt (VMware) | bc1a72a | 2020-03-17 17:32:25 -0400 | [diff] [blame] | 4717 | void ring_buffer_iter_advance(struct ring_buffer_iter *iter) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4718 | { |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4719 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 4720 | unsigned long flags; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4721 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4722 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7e9391c | 2009-09-03 10:02:09 -0400 | [diff] [blame] | 4723 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4724 | rb_advance_iter(iter); |
| 4725 | |
Steven Rostedt (VMware) | bc1a72a | 2020-03-17 17:32:25 -0400 | [diff] [blame] | 4726 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4727 | } |
Steven Rostedt (VMware) | bc1a72a | 2020-03-17 17:32:25 -0400 | [diff] [blame] | 4728 | EXPORT_SYMBOL_GPL(ring_buffer_iter_advance); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4729 | |
| 4730 | /** |
| 4731 | * ring_buffer_size - return the size of the ring buffer (in bytes) |
| 4732 | * @buffer: The ring buffer. |
Fabian Frederick | 59e7cff | 2014-06-05 20:22:05 +0200 | [diff] [blame] | 4733 | * @cpu: The CPU to get ring buffer size from. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4734 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4735 | unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4736 | { |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4737 | /* |
| 4738 | * Earlier, this method returned |
| 4739 | * BUF_PAGE_SIZE * buffer->nr_pages |
| 4740 | * Since the nr_pages field is now removed, we have converted this to |
| 4741 | * return the per cpu buffer value. |
| 4742 | */ |
| 4743 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 4744 | return 0; |
| 4745 | |
| 4746 | return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4747 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4748 | EXPORT_SYMBOL_GPL(ring_buffer_size); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4749 | |
| 4750 | static void |
| 4751 | rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) |
| 4752 | { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4753 | rb_head_page_deactivate(cpu_buffer); |
| 4754 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4755 | cpu_buffer->head_page |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 4756 | = list_entry(cpu_buffer->pages, struct buffer_page, list); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 4757 | local_set(&cpu_buffer->head_page->write, 0); |
Steven Rostedt | 778c55d | 2009-05-01 18:44:45 -0400 | [diff] [blame] | 4758 | local_set(&cpu_buffer->head_page->entries, 0); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 4759 | local_set(&cpu_buffer->head_page->page->commit, 0); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4760 | |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 4761 | cpu_buffer->head_page->read = 0; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 4762 | |
| 4763 | cpu_buffer->tail_page = cpu_buffer->head_page; |
| 4764 | cpu_buffer->commit_page = cpu_buffer->head_page; |
| 4765 | |
| 4766 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); |
Vaibhav Nagarnaik | 5040b4b | 2012-05-03 18:59:51 -0700 | [diff] [blame] | 4767 | INIT_LIST_HEAD(&cpu_buffer->new_pages); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 4768 | local_set(&cpu_buffer->reader_page->write, 0); |
Steven Rostedt | 778c55d | 2009-05-01 18:44:45 -0400 | [diff] [blame] | 4769 | local_set(&cpu_buffer->reader_page->entries, 0); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 4770 | local_set(&cpu_buffer->reader_page->page->commit, 0); |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 4771 | cpu_buffer->reader_page->read = 0; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 4772 | |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 4773 | local_set(&cpu_buffer->entries_bytes, 0); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4774 | local_set(&cpu_buffer->overrun, 0); |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 4775 | local_set(&cpu_buffer->commit_overrun, 0); |
| 4776 | local_set(&cpu_buffer->dropped_events, 0); |
Steven Rostedt | e4906ef | 2009-04-30 20:49:44 -0400 | [diff] [blame] | 4777 | local_set(&cpu_buffer->entries, 0); |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 4778 | local_set(&cpu_buffer->committing, 0); |
| 4779 | local_set(&cpu_buffer->commits, 0); |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 4780 | local_set(&cpu_buffer->pages_touched, 0); |
| 4781 | local_set(&cpu_buffer->pages_read, 0); |
Steven Rostedt (VMware) | 03329f9 | 2018-11-29 21:38:42 -0500 | [diff] [blame] | 4782 | cpu_buffer->last_pages_touch = 0; |
Steven Rostedt (VMware) | 2c2b0a7 | 2018-11-29 20:32:26 -0500 | [diff] [blame] | 4783 | cpu_buffer->shortest_full = 0; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4784 | cpu_buffer->read = 0; |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 4785 | cpu_buffer->read_bytes = 0; |
Steven Rostedt | 69507c0 | 2009-01-21 18:45:57 -0500 | [diff] [blame] | 4786 | |
Steven Rostedt (VMware) | 10464b4 | 2020-06-28 22:52:27 -0400 | [diff] [blame] | 4787 | rb_time_set(&cpu_buffer->write_stamp, 0); |
| 4788 | rb_time_set(&cpu_buffer->before_stamp, 0); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4789 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 4790 | cpu_buffer->lost_events = 0; |
| 4791 | cpu_buffer->last_overrun = 0; |
| 4792 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 4793 | rb_head_page_activate(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4794 | } |
| 4795 | |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame^] | 4796 | /* Must have disabled the cpu buffer then done a synchronize_rcu */ |
| 4797 | 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] | 4798 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4799 | unsigned long flags; |
| 4800 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4801 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4802 | |
Steven Rostedt | 41b6a95 | 2009-09-02 09:59:48 -0400 | [diff] [blame] | 4803 | if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing))) |
| 4804 | goto out; |
| 4805 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 4806 | arch_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4807 | |
| 4808 | rb_reset_cpu(cpu_buffer); |
| 4809 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 4810 | arch_spin_unlock(&cpu_buffer->lock); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 4811 | |
Steven Rostedt | 41b6a95 | 2009-09-02 09:59:48 -0400 | [diff] [blame] | 4812 | out: |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 4813 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame^] | 4814 | } |
| 4815 | |
| 4816 | /** |
| 4817 | * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer |
| 4818 | * @buffer: The ring buffer to reset a per cpu buffer of |
| 4819 | * @cpu: The CPU buffer to be reset |
| 4820 | */ |
| 4821 | void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu) |
| 4822 | { |
| 4823 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
| 4824 | |
| 4825 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 4826 | return; |
| 4827 | |
| 4828 | atomic_inc(&cpu_buffer->resize_disabled); |
| 4829 | atomic_inc(&cpu_buffer->record_disabled); |
| 4830 | |
| 4831 | /* Make sure all commits have finished */ |
| 4832 | synchronize_rcu(); |
| 4833 | |
| 4834 | reset_disabled_cpu_buffer(cpu_buffer); |
Steven Rostedt | 41ede23 | 2009-05-01 20:26:54 -0400 | [diff] [blame] | 4835 | |
| 4836 | atomic_dec(&cpu_buffer->record_disabled); |
Steven Rostedt (VMware) | 07b8b10 | 2020-03-27 16:21:22 -0400 | [diff] [blame] | 4837 | atomic_dec(&cpu_buffer->resize_disabled); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4838 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4839 | EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4840 | |
| 4841 | /** |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame^] | 4842 | * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer |
| 4843 | * @buffer: The ring buffer to reset a per cpu buffer of |
| 4844 | * @cpu: The CPU buffer to be reset |
| 4845 | */ |
| 4846 | void ring_buffer_reset_online_cpus(struct trace_buffer *buffer) |
| 4847 | { |
| 4848 | struct ring_buffer_per_cpu *cpu_buffer; |
| 4849 | int cpu; |
| 4850 | |
| 4851 | for_each_online_buffer_cpu(buffer, cpu) { |
| 4852 | cpu_buffer = buffer->buffers[cpu]; |
| 4853 | |
| 4854 | atomic_inc(&cpu_buffer->resize_disabled); |
| 4855 | atomic_inc(&cpu_buffer->record_disabled); |
| 4856 | } |
| 4857 | |
| 4858 | /* Make sure all commits have finished */ |
| 4859 | synchronize_rcu(); |
| 4860 | |
| 4861 | for_each_online_buffer_cpu(buffer, cpu) { |
| 4862 | cpu_buffer = buffer->buffers[cpu]; |
| 4863 | |
| 4864 | reset_disabled_cpu_buffer(cpu_buffer); |
| 4865 | |
| 4866 | atomic_dec(&cpu_buffer->record_disabled); |
| 4867 | atomic_dec(&cpu_buffer->resize_disabled); |
| 4868 | } |
| 4869 | } |
| 4870 | |
| 4871 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4872 | * ring_buffer_reset - reset a ring buffer |
| 4873 | * @buffer: The ring buffer to reset all cpu buffers |
| 4874 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4875 | void ring_buffer_reset(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4876 | { |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame^] | 4877 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4878 | int cpu; |
| 4879 | |
Nicholas Piggin | b23d7a5f | 2020-06-25 15:34:03 +1000 | [diff] [blame^] | 4880 | for_each_buffer_cpu(buffer, cpu) { |
| 4881 | cpu_buffer = buffer->buffers[cpu]; |
| 4882 | |
| 4883 | atomic_inc(&cpu_buffer->resize_disabled); |
| 4884 | atomic_inc(&cpu_buffer->record_disabled); |
| 4885 | } |
| 4886 | |
| 4887 | /* Make sure all commits have finished */ |
| 4888 | synchronize_rcu(); |
| 4889 | |
| 4890 | for_each_buffer_cpu(buffer, cpu) { |
| 4891 | cpu_buffer = buffer->buffers[cpu]; |
| 4892 | |
| 4893 | reset_disabled_cpu_buffer(cpu_buffer); |
| 4894 | |
| 4895 | atomic_dec(&cpu_buffer->record_disabled); |
| 4896 | atomic_dec(&cpu_buffer->resize_disabled); |
| 4897 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4898 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4899 | EXPORT_SYMBOL_GPL(ring_buffer_reset); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4900 | |
| 4901 | /** |
| 4902 | * rind_buffer_empty - is the ring buffer empty? |
| 4903 | * @buffer: The ring buffer to test |
| 4904 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4905 | bool ring_buffer_empty(struct trace_buffer *buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4906 | { |
| 4907 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 4908 | unsigned long flags; |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4909 | bool dolock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4910 | int cpu; |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 4911 | int ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4912 | |
| 4913 | /* yes this is racy, but if you don't like the race, lock the buffer */ |
| 4914 | for_each_buffer_cpu(buffer, cpu) { |
| 4915 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4916 | local_irq_save(flags); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4917 | dolock = rb_reader_lock(cpu_buffer); |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 4918 | ret = rb_per_cpu_empty(cpu_buffer); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4919 | rb_reader_unlock(cpu_buffer, dolock); |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4920 | local_irq_restore(flags); |
| 4921 | |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 4922 | if (!ret) |
Yaowei Bai | 3d4e204 | 2015-09-29 22:43:32 +0800 | [diff] [blame] | 4923 | return false; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4924 | } |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4925 | |
Yaowei Bai | 3d4e204 | 2015-09-29 22:43:32 +0800 | [diff] [blame] | 4926 | return true; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4927 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4928 | EXPORT_SYMBOL_GPL(ring_buffer_empty); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4929 | |
| 4930 | /** |
| 4931 | * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty? |
| 4932 | * @buffer: The ring buffer |
| 4933 | * @cpu: The CPU buffer to test |
| 4934 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4935 | bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4936 | { |
| 4937 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 4938 | unsigned long flags; |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4939 | bool dolock; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 4940 | int ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4941 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 4942 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Yaowei Bai | 3d4e204 | 2015-09-29 22:43:32 +0800 | [diff] [blame] | 4943 | return true; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4944 | |
| 4945 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4946 | local_irq_save(flags); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4947 | dolock = rb_reader_lock(cpu_buffer); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4948 | ret = rb_per_cpu_empty(cpu_buffer); |
Steven Rostedt (Red Hat) | 289a5a2 | 2015-05-28 13:14:51 -0400 | [diff] [blame] | 4949 | rb_reader_unlock(cpu_buffer, dolock); |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 4950 | local_irq_restore(flags); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4951 | |
| 4952 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4953 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 4954 | EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4955 | |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 4956 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4957 | /** |
| 4958 | * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers |
| 4959 | * @buffer_a: One buffer to swap with |
| 4960 | * @buffer_b: The other buffer to swap with |
Fabian Frederick | 59e7cff | 2014-06-05 20:22:05 +0200 | [diff] [blame] | 4961 | * @cpu: the CPU of the buffers to swap |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4962 | * |
| 4963 | * This function is useful for tracers that want to take a "snapshot" |
| 4964 | * of a CPU buffer and has another back up buffer lying around. |
| 4965 | * it is expected that the tracer handles the cpu buffer not being |
| 4966 | * used at the moment. |
| 4967 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 4968 | int ring_buffer_swap_cpu(struct trace_buffer *buffer_a, |
| 4969 | struct trace_buffer *buffer_b, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4970 | { |
| 4971 | struct ring_buffer_per_cpu *cpu_buffer_a; |
| 4972 | struct ring_buffer_per_cpu *cpu_buffer_b; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4973 | int ret = -EINVAL; |
| 4974 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 4975 | if (!cpumask_test_cpu(cpu, buffer_a->cpumask) || |
| 4976 | !cpumask_test_cpu(cpu, buffer_b->cpumask)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4977 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4978 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4979 | cpu_buffer_a = buffer_a->buffers[cpu]; |
| 4980 | cpu_buffer_b = buffer_b->buffers[cpu]; |
| 4981 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4982 | /* At least make sure the two buffers are somewhat the same */ |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 4983 | if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4984 | goto out; |
| 4985 | |
| 4986 | ret = -EAGAIN; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 4987 | |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 4988 | if (atomic_read(&buffer_a->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4989 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 4990 | |
| 4991 | if (atomic_read(&buffer_b->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4992 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 4993 | |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 4994 | if (atomic_read(&cpu_buffer_a->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4995 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 4996 | |
| 4997 | if (atomic_read(&cpu_buffer_b->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4998 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 4999 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5000 | /* |
Paul E. McKenney | 7440172 | 2018-11-06 18:44:52 -0800 | [diff] [blame] | 5001 | * We can't do a synchronize_rcu here because this |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5002 | * function can be called in atomic context. |
| 5003 | * Normally this will be called from the same CPU as cpu. |
| 5004 | * If not it's up to the caller to protect this. |
| 5005 | */ |
| 5006 | atomic_inc(&cpu_buffer_a->record_disabled); |
| 5007 | atomic_inc(&cpu_buffer_b->record_disabled); |
| 5008 | |
Steven Rostedt | 9827799 | 2009-09-02 10:56:15 -0400 | [diff] [blame] | 5009 | ret = -EBUSY; |
| 5010 | if (local_read(&cpu_buffer_a->committing)) |
| 5011 | goto out_dec; |
| 5012 | if (local_read(&cpu_buffer_b->committing)) |
| 5013 | goto out_dec; |
| 5014 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5015 | buffer_a->buffers[cpu] = cpu_buffer_b; |
| 5016 | buffer_b->buffers[cpu] = cpu_buffer_a; |
| 5017 | |
| 5018 | cpu_buffer_b->buffer = buffer_a; |
| 5019 | cpu_buffer_a->buffer = buffer_b; |
| 5020 | |
Steven Rostedt | 9827799 | 2009-09-02 10:56:15 -0400 | [diff] [blame] | 5021 | ret = 0; |
| 5022 | |
| 5023 | out_dec: |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5024 | atomic_dec(&cpu_buffer_a->record_disabled); |
| 5025 | atomic_dec(&cpu_buffer_b->record_disabled); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5026 | out: |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5027 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5028 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 5029 | EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu); |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 5030 | #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5031 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5032 | /** |
| 5033 | * ring_buffer_alloc_read_page - allocate a page to read from buffer |
| 5034 | * @buffer: the buffer to allocate for. |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 5035 | * @cpu: the cpu buffer to allocate. |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5036 | * |
| 5037 | * This function is used in conjunction with ring_buffer_read_page. |
| 5038 | * When reading a full page from the ring buffer, these functions |
| 5039 | * can be used to speed up the process. The calling function should |
| 5040 | * allocate a few pages first with this function. Then when it |
| 5041 | * needs to get pages from the ring buffer, it passes the result |
| 5042 | * of this function into ring_buffer_read_page, which will swap |
| 5043 | * the page that was allocated, with the read page of the buffer. |
| 5044 | * |
| 5045 | * Returns: |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 5046 | * The page allocated, or ERR_PTR |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5047 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5048 | void *ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu) |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5049 | { |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 5050 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5051 | struct buffer_data_page *bpage = NULL; |
| 5052 | unsigned long flags; |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 5053 | struct page *page; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5054 | |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 5055 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 5056 | return ERR_PTR(-ENODEV); |
| 5057 | |
| 5058 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5059 | local_irq_save(flags); |
| 5060 | arch_spin_lock(&cpu_buffer->lock); |
| 5061 | |
| 5062 | if (cpu_buffer->free_page) { |
| 5063 | bpage = cpu_buffer->free_page; |
| 5064 | cpu_buffer->free_page = NULL; |
| 5065 | } |
| 5066 | |
| 5067 | arch_spin_unlock(&cpu_buffer->lock); |
| 5068 | local_irq_restore(flags); |
| 5069 | |
| 5070 | if (bpage) |
| 5071 | goto out; |
| 5072 | |
Vaibhav Nagarnaik | d7ec4bf | 2011-06-07 17:01:42 -0700 | [diff] [blame] | 5073 | page = alloc_pages_node(cpu_to_node(cpu), |
| 5074 | GFP_KERNEL | __GFP_NORETRY, 0); |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 5075 | if (!page) |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 5076 | return ERR_PTR(-ENOMEM); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5077 | |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 5078 | bpage = page_address(page); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5079 | |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5080 | out: |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5081 | rb_init_page(bpage); |
| 5082 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 5083 | return bpage; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5084 | } |
Steven Rostedt | d6ce96d | 2009-05-05 01:15:24 -0400 | [diff] [blame] | 5085 | EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5086 | |
| 5087 | /** |
| 5088 | * ring_buffer_free_read_page - free an allocated read page |
| 5089 | * @buffer: the buffer the page was allocate for |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5090 | * @cpu: the cpu buffer the page came from |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5091 | * @data: the page to free |
| 5092 | * |
| 5093 | * Free a page allocated from ring_buffer_alloc_read_page. |
| 5094 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5095 | 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] | 5096 | { |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5097 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
| 5098 | struct buffer_data_page *bpage = data; |
Steven Rostedt (VMware) | ae415fa | 2017-12-22 21:19:29 -0500 | [diff] [blame] | 5099 | struct page *page = virt_to_page(bpage); |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5100 | unsigned long flags; |
| 5101 | |
Steven Rostedt (VMware) | ae415fa | 2017-12-22 21:19:29 -0500 | [diff] [blame] | 5102 | /* If the page is still in use someplace else, we can't reuse it */ |
| 5103 | if (page_ref_count(page) > 1) |
| 5104 | goto out; |
| 5105 | |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5106 | local_irq_save(flags); |
| 5107 | arch_spin_lock(&cpu_buffer->lock); |
| 5108 | |
| 5109 | if (!cpu_buffer->free_page) { |
| 5110 | cpu_buffer->free_page = bpage; |
| 5111 | bpage = NULL; |
| 5112 | } |
| 5113 | |
| 5114 | arch_spin_unlock(&cpu_buffer->lock); |
| 5115 | local_irq_restore(flags); |
| 5116 | |
Steven Rostedt (VMware) | ae415fa | 2017-12-22 21:19:29 -0500 | [diff] [blame] | 5117 | out: |
Steven Rostedt (VMware) | 73a757e | 2017-05-01 09:35:09 -0400 | [diff] [blame] | 5118 | free_page((unsigned long)bpage); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5119 | } |
Steven Rostedt | d6ce96d | 2009-05-05 01:15:24 -0400 | [diff] [blame] | 5120 | EXPORT_SYMBOL_GPL(ring_buffer_free_read_page); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5121 | |
| 5122 | /** |
| 5123 | * ring_buffer_read_page - extract a page from the ring buffer |
| 5124 | * @buffer: buffer to extract from |
| 5125 | * @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] | 5126 | * @len: amount to extract |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5127 | * @cpu: the cpu of the buffer to extract |
| 5128 | * @full: should the extraction only happen when the page is full. |
| 5129 | * |
| 5130 | * This function will pull out a page from the ring buffer and consume it. |
| 5131 | * @data_page must be the address of the variable that was returned |
| 5132 | * from ring_buffer_alloc_read_page. This is because the page might be used |
| 5133 | * to swap with a page in the ring buffer. |
| 5134 | * |
| 5135 | * for example: |
zhangwei(Jovi) | d611851 | 2013-07-15 16:32:50 +0800 | [diff] [blame] | 5136 | * rpage = ring_buffer_alloc_read_page(buffer, cpu); |
Steven Rostedt (VMware) | a7e52ad | 2017-08-02 14:20:54 -0400 | [diff] [blame] | 5137 | * if (IS_ERR(rpage)) |
| 5138 | * return PTR_ERR(rpage); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5139 | * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0); |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5140 | * if (ret >= 0) |
| 5141 | * process_page(rpage, ret); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5142 | * |
| 5143 | * When @full is set, the function will not return true unless |
| 5144 | * the writer is off the reader page. |
| 5145 | * |
| 5146 | * Note: it is up to the calling functions to handle sleeps and wakeups. |
| 5147 | * The ring buffer can be used anywhere in the kernel and can not |
| 5148 | * blindly call wake_up. The layer that uses the ring buffer must be |
| 5149 | * responsible for that. |
| 5150 | * |
| 5151 | * Returns: |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5152 | * >=0 if data has been transferred, returns the offset of consumed data. |
| 5153 | * <0 if no data has been transferred. |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5154 | */ |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5155 | int ring_buffer_read_page(struct trace_buffer *buffer, |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5156 | void **data_page, size_t len, int cpu, int full) |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5157 | { |
| 5158 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
| 5159 | struct ring_buffer_event *event; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 5160 | struct buffer_data_page *bpage; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5161 | struct buffer_page *reader; |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5162 | unsigned long missed_events; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5163 | unsigned long flags; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5164 | unsigned int commit; |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5165 | unsigned int read; |
Steven Rostedt | 4f3640f | 2009-03-03 23:52:42 -0500 | [diff] [blame] | 5166 | u64 save_timestamp; |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5167 | int ret = -1; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5168 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5169 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 5170 | goto out; |
| 5171 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5172 | /* |
| 5173 | * If len is not big enough to hold the page header, then |
| 5174 | * we can not copy anything. |
| 5175 | */ |
| 5176 | if (len <= BUF_PAGE_HDR_SIZE) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5177 | goto out; |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5178 | |
| 5179 | len -= BUF_PAGE_HDR_SIZE; |
| 5180 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5181 | if (!data_page) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5182 | goto out; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5183 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 5184 | bpage = *data_page; |
| 5185 | if (!bpage) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5186 | goto out; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5187 | |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 5188 | raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5189 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5190 | reader = rb_get_reader_page(cpu_buffer); |
| 5191 | if (!reader) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5192 | goto out_unlock; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5193 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5194 | event = rb_reader_event(cpu_buffer); |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5195 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5196 | read = reader->read; |
| 5197 | commit = rb_page_commit(reader); |
| 5198 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 5199 | /* Check if any events were dropped */ |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5200 | missed_events = cpu_buffer->lost_events; |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 5201 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5202 | /* |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5203 | * If this page has been partially read or |
| 5204 | * if len is not big enough to read the rest of the page or |
| 5205 | * a writer is still on the page, then |
| 5206 | * we must copy the data from the page to the buffer. |
| 5207 | * Otherwise, we can simply swap the page with the one passed in. |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5208 | */ |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5209 | if (read || (len < (commit - read)) || |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5210 | cpu_buffer->reader_page == cpu_buffer->commit_page) { |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5211 | struct buffer_data_page *rpage = cpu_buffer->reader_page->page; |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5212 | unsigned int rpos = read; |
| 5213 | unsigned int pos = 0; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5214 | unsigned int size; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5215 | |
| 5216 | if (full) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5217 | goto out_unlock; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5218 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5219 | if (len > (commit - read)) |
| 5220 | len = (commit - read); |
| 5221 | |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 5222 | /* Always keep the time extend and data together */ |
| 5223 | size = rb_event_ts_length(event); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5224 | |
| 5225 | if (len < size) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5226 | goto out_unlock; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5227 | |
Steven Rostedt | 4f3640f | 2009-03-03 23:52:42 -0500 | [diff] [blame] | 5228 | /* save the current timestamp, since the user will need it */ |
| 5229 | save_timestamp = cpu_buffer->read_stamp; |
| 5230 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5231 | /* Need to copy one event at a time */ |
| 5232 | do { |
David Sharp | e1e3592 | 2010-12-22 16:38:24 -0800 | [diff] [blame] | 5233 | /* We need the size of one event, because |
| 5234 | * rb_advance_reader only advances by one event, |
| 5235 | * whereas rb_event_ts_length may include the size of |
| 5236 | * one or two events. |
| 5237 | * We have already ensured there's enough space if this |
| 5238 | * is a time extend. */ |
| 5239 | size = rb_event_length(event); |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5240 | memcpy(bpage->data + pos, rpage->data + rpos, size); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5241 | |
| 5242 | len -= size; |
| 5243 | |
| 5244 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5245 | rpos = reader->read; |
| 5246 | pos += size; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5247 | |
Huang Ying | 18fab91 | 2010-07-28 14:14:01 +0800 | [diff] [blame] | 5248 | if (rpos >= commit) |
| 5249 | break; |
| 5250 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5251 | event = rb_reader_event(cpu_buffer); |
Steven Rostedt | 69d1b83 | 2010-10-07 18:18:05 -0400 | [diff] [blame] | 5252 | /* Always keep the time extend and data together */ |
| 5253 | size = rb_event_ts_length(event); |
David Sharp | e1e3592 | 2010-12-22 16:38:24 -0800 | [diff] [blame] | 5254 | } while (len >= size); |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5255 | |
| 5256 | /* update bpage */ |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5257 | local_set(&bpage->commit, pos); |
Steven Rostedt | 4f3640f | 2009-03-03 23:52:42 -0500 | [diff] [blame] | 5258 | bpage->time_stamp = save_timestamp; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5259 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 5260 | /* we copied everything to the beginning */ |
| 5261 | read = 0; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5262 | } else { |
Steven Rostedt | afbab76 | 2009-05-01 19:40:05 -0400 | [diff] [blame] | 5263 | /* update the entry counter */ |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 5264 | cpu_buffer->read += rb_page_entries(reader); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 5265 | cpu_buffer->read_bytes += BUF_PAGE_SIZE; |
Steven Rostedt | afbab76 | 2009-05-01 19:40:05 -0400 | [diff] [blame] | 5266 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5267 | /* swap the pages */ |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 5268 | rb_init_page(bpage); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5269 | bpage = reader->page; |
| 5270 | reader->page = *data_page; |
| 5271 | local_set(&reader->write, 0); |
Steven Rostedt | 778c55d | 2009-05-01 18:44:45 -0400 | [diff] [blame] | 5272 | local_set(&reader->entries, 0); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 5273 | reader->read = 0; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 5274 | *data_page = bpage; |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5275 | |
| 5276 | /* |
| 5277 | * Use the real_end for the data size, |
| 5278 | * This gives us a chance to store the lost events |
| 5279 | * on the page. |
| 5280 | */ |
| 5281 | if (reader->real_end) |
| 5282 | local_set(&bpage->commit, reader->real_end); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5283 | } |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 5284 | ret = read; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5285 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 5286 | cpu_buffer->lost_events = 0; |
Steven Rostedt | 2711ca2 | 2010-05-21 13:32:26 -0400 | [diff] [blame] | 5287 | |
| 5288 | commit = local_read(&bpage->commit); |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 5289 | /* |
| 5290 | * Set a flag in the commit field if we lost events |
| 5291 | */ |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5292 | if (missed_events) { |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5293 | /* If there is room at the end of the page to save the |
| 5294 | * missed events, then record it there. |
| 5295 | */ |
| 5296 | if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) { |
| 5297 | memcpy(&bpage->data[commit], &missed_events, |
| 5298 | sizeof(missed_events)); |
| 5299 | local_add(RB_MISSED_STORED, &bpage->commit); |
Steven Rostedt | 2711ca2 | 2010-05-21 13:32:26 -0400 | [diff] [blame] | 5300 | commit += sizeof(missed_events); |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5301 | } |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 5302 | local_add(RB_MISSED_EVENTS, &bpage->commit); |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 5303 | } |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 5304 | |
Steven Rostedt | 2711ca2 | 2010-05-21 13:32:26 -0400 | [diff] [blame] | 5305 | /* |
| 5306 | * This page may be off to user land. Zero it out here. |
| 5307 | */ |
| 5308 | if (commit < BUF_PAGE_SIZE) |
| 5309 | memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit); |
| 5310 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5311 | out_unlock: |
Thomas Gleixner | 5389f6f | 2009-07-25 17:13:33 +0200 | [diff] [blame] | 5312 | raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5313 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5314 | out: |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5315 | return ret; |
| 5316 | } |
Steven Rostedt | d6ce96d | 2009-05-05 01:15:24 -0400 | [diff] [blame] | 5317 | EXPORT_SYMBOL_GPL(ring_buffer_read_page); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 5318 | |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 5319 | /* |
| 5320 | * We only allocate new buffers, never free them if the CPU goes down. |
| 5321 | * If we were to free the buffer, then the user would lose any trace that was in |
| 5322 | * the buffer. |
| 5323 | */ |
| 5324 | int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5325 | { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5326 | struct trace_buffer *buffer; |
Steven Rostedt (Red Hat) | 9b94a8f | 2016-05-12 11:01:24 -0400 | [diff] [blame] | 5327 | long nr_pages_same; |
| 5328 | int cpu_i; |
| 5329 | unsigned long nr_pages; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5330 | |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5331 | buffer = container_of(node, struct trace_buffer, node); |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 5332 | if (cpumask_test_cpu(cpu, buffer->cpumask)) |
| 5333 | return 0; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5334 | |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 5335 | nr_pages = 0; |
| 5336 | nr_pages_same = 1; |
| 5337 | /* check if all cpu sizes are same */ |
| 5338 | for_each_buffer_cpu(buffer, cpu_i) { |
| 5339 | /* fill in the size from first enabled cpu */ |
| 5340 | if (nr_pages == 0) |
| 5341 | nr_pages = buffer->buffers[cpu_i]->nr_pages; |
| 5342 | if (nr_pages != buffer->buffers[cpu_i]->nr_pages) { |
| 5343 | nr_pages_same = 0; |
| 5344 | break; |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 5345 | } |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5346 | } |
Sebastian Andrzej Siewior | b32614c | 2016-11-27 00:13:34 +0100 | [diff] [blame] | 5347 | /* allocate minimum pages, user can later expand it */ |
| 5348 | if (!nr_pages_same) |
| 5349 | nr_pages = 2; |
| 5350 | buffer->buffers[cpu] = |
| 5351 | rb_allocate_cpu_buffer(buffer, nr_pages, cpu); |
| 5352 | if (!buffer->buffers[cpu]) { |
| 5353 | WARN(1, "failed to allocate ring buffer on CPU %u\n", |
| 5354 | cpu); |
| 5355 | return -ENOMEM; |
| 5356 | } |
| 5357 | smp_wmb(); |
| 5358 | cpumask_set_cpu(cpu, buffer->cpumask); |
| 5359 | return 0; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 5360 | } |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5361 | |
| 5362 | #ifdef CONFIG_RING_BUFFER_STARTUP_TEST |
| 5363 | /* |
| 5364 | * This is a basic integrity check of the ring buffer. |
| 5365 | * Late in the boot cycle this test will run when configured in. |
| 5366 | * It will kick off a thread per CPU that will go into a loop |
| 5367 | * writing to the per cpu ring buffer various sizes of data. |
| 5368 | * Some of the data will be large items, some small. |
| 5369 | * |
| 5370 | * Another thread is created that goes into a spin, sending out |
| 5371 | * IPIs to the other CPUs to also write into the ring buffer. |
| 5372 | * this is to test the nesting ability of the buffer. |
| 5373 | * |
| 5374 | * Basic stats are recorded and reported. If something in the |
| 5375 | * ring buffer should happen that's not expected, a big warning |
| 5376 | * is displayed and all ring buffers are disabled. |
| 5377 | */ |
| 5378 | static struct task_struct *rb_threads[NR_CPUS] __initdata; |
| 5379 | |
| 5380 | struct rb_test_data { |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5381 | struct trace_buffer *buffer; |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5382 | unsigned long events; |
| 5383 | unsigned long bytes_written; |
| 5384 | unsigned long bytes_alloc; |
| 5385 | unsigned long bytes_dropped; |
| 5386 | unsigned long events_nested; |
| 5387 | unsigned long bytes_written_nested; |
| 5388 | unsigned long bytes_alloc_nested; |
| 5389 | unsigned long bytes_dropped_nested; |
| 5390 | int min_size_nested; |
| 5391 | int max_size_nested; |
| 5392 | int max_size; |
| 5393 | int min_size; |
| 5394 | int cpu; |
| 5395 | int cnt; |
| 5396 | }; |
| 5397 | |
| 5398 | static struct rb_test_data rb_data[NR_CPUS] __initdata; |
| 5399 | |
| 5400 | /* 1 meg per cpu */ |
| 5401 | #define RB_TEST_BUFFER_SIZE 1048576 |
| 5402 | |
| 5403 | static char rb_string[] __initdata = |
| 5404 | "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\" |
| 5405 | "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890" |
| 5406 | "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv"; |
| 5407 | |
| 5408 | static bool rb_test_started __initdata; |
| 5409 | |
| 5410 | struct rb_item { |
| 5411 | int size; |
| 5412 | char str[]; |
| 5413 | }; |
| 5414 | |
| 5415 | static __init int rb_write_something(struct rb_test_data *data, bool nested) |
| 5416 | { |
| 5417 | struct ring_buffer_event *event; |
| 5418 | struct rb_item *item; |
| 5419 | bool started; |
| 5420 | int event_len; |
| 5421 | int size; |
| 5422 | int len; |
| 5423 | int cnt; |
| 5424 | |
| 5425 | /* Have nested writes different that what is written */ |
| 5426 | cnt = data->cnt + (nested ? 27 : 0); |
| 5427 | |
| 5428 | /* Multiply cnt by ~e, to make some unique increment */ |
YueHaibing | 40ed29b | 2018-09-23 12:11:33 +0000 | [diff] [blame] | 5429 | size = (cnt * 68 / 25) % (sizeof(rb_string) - 1); |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5430 | |
| 5431 | len = size + sizeof(struct rb_item); |
| 5432 | |
| 5433 | started = rb_test_started; |
| 5434 | /* read rb_test_started before checking buffer enabled */ |
| 5435 | smp_rmb(); |
| 5436 | |
| 5437 | event = ring_buffer_lock_reserve(data->buffer, len); |
| 5438 | if (!event) { |
| 5439 | /* Ignore dropped events before test starts. */ |
| 5440 | if (started) { |
| 5441 | if (nested) |
| 5442 | data->bytes_dropped += len; |
| 5443 | else |
| 5444 | data->bytes_dropped_nested += len; |
| 5445 | } |
| 5446 | return len; |
| 5447 | } |
| 5448 | |
| 5449 | event_len = ring_buffer_event_length(event); |
| 5450 | |
| 5451 | if (RB_WARN_ON(data->buffer, event_len < len)) |
| 5452 | goto out; |
| 5453 | |
| 5454 | item = ring_buffer_event_data(event); |
| 5455 | item->size = size; |
| 5456 | memcpy(item->str, rb_string, size); |
| 5457 | |
| 5458 | if (nested) { |
| 5459 | data->bytes_alloc_nested += event_len; |
| 5460 | data->bytes_written_nested += len; |
| 5461 | data->events_nested++; |
| 5462 | if (!data->min_size_nested || len < data->min_size_nested) |
| 5463 | data->min_size_nested = len; |
| 5464 | if (len > data->max_size_nested) |
| 5465 | data->max_size_nested = len; |
| 5466 | } else { |
| 5467 | data->bytes_alloc += event_len; |
| 5468 | data->bytes_written += len; |
| 5469 | data->events++; |
| 5470 | if (!data->min_size || len < data->min_size) |
| 5471 | data->max_size = len; |
| 5472 | if (len > data->max_size) |
| 5473 | data->max_size = len; |
| 5474 | } |
| 5475 | |
| 5476 | out: |
| 5477 | ring_buffer_unlock_commit(data->buffer, event); |
| 5478 | |
| 5479 | return 0; |
| 5480 | } |
| 5481 | |
| 5482 | static __init int rb_test(void *arg) |
| 5483 | { |
| 5484 | struct rb_test_data *data = arg; |
| 5485 | |
| 5486 | while (!kthread_should_stop()) { |
| 5487 | rb_write_something(data, false); |
| 5488 | data->cnt++; |
| 5489 | |
| 5490 | set_current_state(TASK_INTERRUPTIBLE); |
| 5491 | /* Now sleep between a min of 100-300us and a max of 1ms */ |
| 5492 | usleep_range(((data->cnt % 3) + 1) * 100, 1000); |
| 5493 | } |
| 5494 | |
| 5495 | return 0; |
| 5496 | } |
| 5497 | |
| 5498 | static __init void rb_ipi(void *ignore) |
| 5499 | { |
| 5500 | struct rb_test_data *data; |
| 5501 | int cpu = smp_processor_id(); |
| 5502 | |
| 5503 | data = &rb_data[cpu]; |
| 5504 | rb_write_something(data, true); |
| 5505 | } |
| 5506 | |
| 5507 | static __init int rb_hammer_test(void *arg) |
| 5508 | { |
| 5509 | while (!kthread_should_stop()) { |
| 5510 | |
| 5511 | /* Send an IPI to all cpus to write data! */ |
| 5512 | smp_call_function(rb_ipi, NULL, 1); |
| 5513 | /* No sleep, but for non preempt, let others run */ |
| 5514 | schedule(); |
| 5515 | } |
| 5516 | |
| 5517 | return 0; |
| 5518 | } |
| 5519 | |
| 5520 | static __init int test_ringbuffer(void) |
| 5521 | { |
| 5522 | struct task_struct *rb_hammer; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 5523 | struct trace_buffer *buffer; |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5524 | int cpu; |
| 5525 | int ret = 0; |
| 5526 | |
Steven Rostedt (VMware) | a356646 | 2019-12-02 16:25:27 -0500 | [diff] [blame] | 5527 | if (security_locked_down(LOCKDOWN_TRACEFS)) { |
Stephen Rothwell | ee19545 | 2019-12-06 09:25:03 +1100 | [diff] [blame] | 5528 | pr_warn("Lockdown is enabled, skipping ring buffer tests\n"); |
Steven Rostedt (VMware) | a356646 | 2019-12-02 16:25:27 -0500 | [diff] [blame] | 5529 | return 0; |
| 5530 | } |
| 5531 | |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5532 | pr_info("Running ring buffer tests...\n"); |
| 5533 | |
| 5534 | buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE); |
| 5535 | if (WARN_ON(!buffer)) |
| 5536 | return 0; |
| 5537 | |
| 5538 | /* Disable buffer so that threads can't write to it yet */ |
| 5539 | ring_buffer_record_off(buffer); |
| 5540 | |
| 5541 | for_each_online_cpu(cpu) { |
| 5542 | rb_data[cpu].buffer = buffer; |
| 5543 | rb_data[cpu].cpu = cpu; |
| 5544 | rb_data[cpu].cnt = cpu; |
| 5545 | rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu], |
| 5546 | "rbtester/%d", cpu); |
Wei Yongjun | 62277de | 2016-06-17 17:33:59 +0000 | [diff] [blame] | 5547 | if (WARN_ON(IS_ERR(rb_threads[cpu]))) { |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5548 | pr_cont("FAILED\n"); |
Wei Yongjun | 62277de | 2016-06-17 17:33:59 +0000 | [diff] [blame] | 5549 | ret = PTR_ERR(rb_threads[cpu]); |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5550 | goto out_free; |
| 5551 | } |
| 5552 | |
| 5553 | kthread_bind(rb_threads[cpu], cpu); |
| 5554 | wake_up_process(rb_threads[cpu]); |
| 5555 | } |
| 5556 | |
| 5557 | /* Now create the rb hammer! */ |
| 5558 | rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer"); |
Wei Yongjun | 62277de | 2016-06-17 17:33:59 +0000 | [diff] [blame] | 5559 | if (WARN_ON(IS_ERR(rb_hammer))) { |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5560 | pr_cont("FAILED\n"); |
Wei Yongjun | 62277de | 2016-06-17 17:33:59 +0000 | [diff] [blame] | 5561 | ret = PTR_ERR(rb_hammer); |
Steven Rostedt (Red Hat) | 6c43e55 | 2013-03-15 11:32:53 -0400 | [diff] [blame] | 5562 | goto out_free; |
| 5563 | } |
| 5564 | |
| 5565 | ring_buffer_record_on(buffer); |
| 5566 | /* |
| 5567 | * Show buffer is enabled before setting rb_test_started. |
| 5568 | * Yes there's a small race window where events could be |
| 5569 | * dropped and the thread wont catch it. But when a ring |
| 5570 | * buffer gets enabled, there will always be some kind of |
| 5571 | * delay before other CPUs see it. Thus, we don't care about |
| 5572 | * those dropped events. We care about events dropped after |
| 5573 | * the threads see that the buffer is active. |
| 5574 | */ |
| 5575 | smp_wmb(); |
| 5576 | rb_test_started = true; |
| 5577 | |
| 5578 | set_current_state(TASK_INTERRUPTIBLE); |
| 5579 | /* Just run for 10 seconds */; |
| 5580 | schedule_timeout(10 * HZ); |
| 5581 | |
| 5582 | kthread_stop(rb_hammer); |
| 5583 | |
| 5584 | out_free: |
| 5585 | for_each_online_cpu(cpu) { |
| 5586 | if (!rb_threads[cpu]) |
| 5587 | break; |
| 5588 | kthread_stop(rb_threads[cpu]); |
| 5589 | } |
| 5590 | if (ret) { |
| 5591 | ring_buffer_free(buffer); |
| 5592 | return ret; |
| 5593 | } |
| 5594 | |
| 5595 | /* Report! */ |
| 5596 | pr_info("finished\n"); |
| 5597 | for_each_online_cpu(cpu) { |
| 5598 | struct ring_buffer_event *event; |
| 5599 | struct rb_test_data *data = &rb_data[cpu]; |
| 5600 | struct rb_item *item; |
| 5601 | unsigned long total_events; |
| 5602 | unsigned long total_dropped; |
| 5603 | unsigned long total_written; |
| 5604 | unsigned long total_alloc; |
| 5605 | unsigned long total_read = 0; |
| 5606 | unsigned long total_size = 0; |
| 5607 | unsigned long total_len = 0; |
| 5608 | unsigned long total_lost = 0; |
| 5609 | unsigned long lost; |
| 5610 | int big_event_size; |
| 5611 | int small_event_size; |
| 5612 | |
| 5613 | ret = -1; |
| 5614 | |
| 5615 | total_events = data->events + data->events_nested; |
| 5616 | total_written = data->bytes_written + data->bytes_written_nested; |
| 5617 | total_alloc = data->bytes_alloc + data->bytes_alloc_nested; |
| 5618 | total_dropped = data->bytes_dropped + data->bytes_dropped_nested; |
| 5619 | |
| 5620 | big_event_size = data->max_size + data->max_size_nested; |
| 5621 | small_event_size = data->min_size + data->min_size_nested; |
| 5622 | |
| 5623 | pr_info("CPU %d:\n", cpu); |
| 5624 | pr_info(" events: %ld\n", total_events); |
| 5625 | pr_info(" dropped bytes: %ld\n", total_dropped); |
| 5626 | pr_info(" alloced bytes: %ld\n", total_alloc); |
| 5627 | pr_info(" written bytes: %ld\n", total_written); |
| 5628 | pr_info(" biggest event: %d\n", big_event_size); |
| 5629 | pr_info(" smallest event: %d\n", small_event_size); |
| 5630 | |
| 5631 | if (RB_WARN_ON(buffer, total_dropped)) |
| 5632 | break; |
| 5633 | |
| 5634 | ret = 0; |
| 5635 | |
| 5636 | while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) { |
| 5637 | total_lost += lost; |
| 5638 | item = ring_buffer_event_data(event); |
| 5639 | total_len += ring_buffer_event_length(event); |
| 5640 | total_size += item->size + sizeof(struct rb_item); |
| 5641 | if (memcmp(&item->str[0], rb_string, item->size) != 0) { |
| 5642 | pr_info("FAILED!\n"); |
| 5643 | pr_info("buffer had: %.*s\n", item->size, item->str); |
| 5644 | pr_info("expected: %.*s\n", item->size, rb_string); |
| 5645 | RB_WARN_ON(buffer, 1); |
| 5646 | ret = -1; |
| 5647 | break; |
| 5648 | } |
| 5649 | total_read++; |
| 5650 | } |
| 5651 | if (ret) |
| 5652 | break; |
| 5653 | |
| 5654 | ret = -1; |
| 5655 | |
| 5656 | pr_info(" read events: %ld\n", total_read); |
| 5657 | pr_info(" lost events: %ld\n", total_lost); |
| 5658 | pr_info(" total events: %ld\n", total_lost + total_read); |
| 5659 | pr_info(" recorded len bytes: %ld\n", total_len); |
| 5660 | pr_info(" recorded size bytes: %ld\n", total_size); |
| 5661 | if (total_lost) |
| 5662 | pr_info(" With dropped events, record len and size may not match\n" |
| 5663 | " alloced and written from above\n"); |
| 5664 | if (!total_lost) { |
| 5665 | if (RB_WARN_ON(buffer, total_len != total_alloc || |
| 5666 | total_size != total_written)) |
| 5667 | break; |
| 5668 | } |
| 5669 | if (RB_WARN_ON(buffer, total_lost + total_read != total_events)) |
| 5670 | break; |
| 5671 | |
| 5672 | ret = 0; |
| 5673 | } |
| 5674 | if (!ret) |
| 5675 | pr_info("Ring buffer PASSED!\n"); |
| 5676 | |
| 5677 | ring_buffer_free(buffer); |
| 5678 | return 0; |
| 5679 | } |
| 5680 | |
| 5681 | late_initcall(test_ringbuffer); |
| 5682 | #endif /* CONFIG_RING_BUFFER_STARTUP_TEST */ |