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