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 | */ |
| 6 | #include <linux/ring_buffer.h> |
Ingo Molnar | 14131f2 | 2009-02-26 18:47:11 +0100 | [diff] [blame] | 7 | #include <linux/trace_clock.h> |
Steven Rostedt | 78d904b | 2009-02-05 18:43:07 -0500 | [diff] [blame] | 8 | #include <linux/ftrace_irq.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 9 | #include <linux/spinlock.h> |
| 10 | #include <linux/debugfs.h> |
| 11 | #include <linux/uaccess.h> |
Steven Rostedt | a81bd80 | 2009-02-06 01:45:16 -0500 | [diff] [blame] | 12 | #include <linux/hardirq.h> |
Vegard Nossum | 1744a21 | 2009-02-28 08:29:44 +0100 | [diff] [blame] | 13 | #include <linux/kmemcheck.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/percpu.h> |
| 16 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/hash.h> |
| 20 | #include <linux/list.h> |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 21 | #include <linux/cpu.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 22 | #include <linux/fs.h> |
| 23 | |
Christoph Lameter | 7961576 | 2010-01-05 15:34:50 +0900 | [diff] [blame] | 24 | #include <asm/local.h> |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 25 | #include "trace.h" |
| 26 | |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 27 | /* |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 28 | * The ring buffer header is special. We must manually up keep it. |
| 29 | */ |
| 30 | int ring_buffer_print_entry_header(struct trace_seq *s) |
| 31 | { |
| 32 | int ret; |
| 33 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 34 | ret = trace_seq_printf(s, "# compressed entry header\n"); |
| 35 | ret = trace_seq_printf(s, "\ttype_len : 5 bits\n"); |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 36 | ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n"); |
| 37 | ret = trace_seq_printf(s, "\tarray : 32 bits\n"); |
| 38 | ret = trace_seq_printf(s, "\n"); |
| 39 | ret = trace_seq_printf(s, "\tpadding : type == %d\n", |
| 40 | RINGBUF_TYPE_PADDING); |
| 41 | ret = trace_seq_printf(s, "\ttime_extend : type == %d\n", |
| 42 | RINGBUF_TYPE_TIME_EXTEND); |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 43 | ret = trace_seq_printf(s, "\tdata max type_len == %d\n", |
| 44 | RINGBUF_TYPE_DATA_TYPE_LEN_MAX); |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 45 | |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | /* |
Steven Rostedt | 5cc9854 | 2009-03-12 22:24:17 -0400 | [diff] [blame] | 50 | * The ring buffer is made up of a list of pages. A separate list of pages is |
| 51 | * allocated for each CPU. A writer may only write to a buffer that is |
| 52 | * associated with the CPU it is currently executing on. A reader may read |
| 53 | * from any per cpu buffer. |
| 54 | * |
| 55 | * The reader is special. For each per cpu buffer, the reader has its own |
| 56 | * reader page. When a reader has read the entire reader page, this reader |
| 57 | * page is swapped with another page in the ring buffer. |
| 58 | * |
| 59 | * Now, as long as the writer is off the reader page, the reader can do what |
| 60 | * ever it wants with that page. The writer will never write to that page |
| 61 | * again (as long as it is out of the ring buffer). |
| 62 | * |
| 63 | * Here's some silly ASCII art. |
| 64 | * |
| 65 | * +------+ |
| 66 | * |reader| RING BUFFER |
| 67 | * |page | |
| 68 | * +------+ +---+ +---+ +---+ |
| 69 | * | |-->| |-->| | |
| 70 | * +---+ +---+ +---+ |
| 71 | * ^ | |
| 72 | * | | |
| 73 | * +---------------+ |
| 74 | * |
| 75 | * |
| 76 | * +------+ |
| 77 | * |reader| RING BUFFER |
| 78 | * |page |------------------v |
| 79 | * +------+ +---+ +---+ +---+ |
| 80 | * | |-->| |-->| | |
| 81 | * +---+ +---+ +---+ |
| 82 | * ^ | |
| 83 | * | | |
| 84 | * +---------------+ |
| 85 | * |
| 86 | * |
| 87 | * +------+ |
| 88 | * |reader| RING BUFFER |
| 89 | * |page |------------------v |
| 90 | * +------+ +---+ +---+ +---+ |
| 91 | * ^ | |-->| |-->| | |
| 92 | * | +---+ +---+ +---+ |
| 93 | * | | |
| 94 | * | | |
| 95 | * +------------------------------+ |
| 96 | * |
| 97 | * |
| 98 | * +------+ |
| 99 | * |buffer| RING BUFFER |
| 100 | * |page |------------------v |
| 101 | * +------+ +---+ +---+ +---+ |
| 102 | * ^ | | | |-->| | |
| 103 | * | New +---+ +---+ +---+ |
| 104 | * | Reader------^ | |
| 105 | * | page | |
| 106 | * +------------------------------+ |
| 107 | * |
| 108 | * |
| 109 | * After we make this swap, the reader can hand this page off to the splice |
| 110 | * code and be done with it. It can even allocate a new page if it needs to |
| 111 | * and swap that into the ring buffer. |
| 112 | * |
| 113 | * We will be using cmpxchg soon to make all this lockless. |
| 114 | * |
| 115 | */ |
| 116 | |
| 117 | /* |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 118 | * A fast way to enable or disable all ring buffers is to |
| 119 | * call tracing_on or tracing_off. Turning off the ring buffers |
| 120 | * prevents all ring buffers from being recorded to. |
| 121 | * Turning this switch on, makes it OK to write to the |
| 122 | * ring buffer, if the ring buffer is enabled itself. |
| 123 | * |
| 124 | * There's three layers that must be on in order to write |
| 125 | * to the ring buffer. |
| 126 | * |
| 127 | * 1) This global flag must be set. |
| 128 | * 2) The ring buffer must be enabled for recording. |
| 129 | * 3) The per cpu buffer must be enabled for recording. |
| 130 | * |
| 131 | * In case of an anomaly, this global flag has a bit set that |
| 132 | * will permantly disable all ring buffers. |
| 133 | */ |
| 134 | |
| 135 | /* |
| 136 | * Global flag to disable all recording to ring buffers |
| 137 | * This has two bits: ON, DISABLED |
| 138 | * |
| 139 | * ON DISABLED |
| 140 | * ---- ---------- |
| 141 | * 0 0 : ring buffers are off |
| 142 | * 1 0 : ring buffers are on |
| 143 | * X 1 : ring buffers are permanently disabled |
| 144 | */ |
| 145 | |
| 146 | enum { |
| 147 | RB_BUFFERS_ON_BIT = 0, |
| 148 | RB_BUFFERS_DISABLED_BIT = 1, |
| 149 | }; |
| 150 | |
| 151 | enum { |
| 152 | RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT, |
| 153 | RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT, |
| 154 | }; |
| 155 | |
Hannes Eder | 5e39841 | 2009-02-10 19:44:34 +0100 | [diff] [blame] | 156 | static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON; |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 157 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 158 | #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data) |
| 159 | |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 160 | /** |
| 161 | * tracing_on - enable all tracing buffers |
| 162 | * |
| 163 | * This function enables all tracing buffers that may have been |
| 164 | * disabled with tracing_off. |
| 165 | */ |
| 166 | void tracing_on(void) |
| 167 | { |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 168 | set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 169 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 170 | EXPORT_SYMBOL_GPL(tracing_on); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * tracing_off - turn off all tracing buffers |
| 174 | * |
| 175 | * This function stops all tracing buffers from recording data. |
| 176 | * It does not disable any overhead the tracers themselves may |
| 177 | * be causing. This function simply causes all recording to |
| 178 | * the ring buffers to fail. |
| 179 | */ |
| 180 | void tracing_off(void) |
| 181 | { |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 182 | clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags); |
| 183 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 184 | EXPORT_SYMBOL_GPL(tracing_off); |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * tracing_off_permanent - permanently disable ring buffers |
| 188 | * |
| 189 | * This function, once called, will disable all ring buffers |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 190 | * permanently. |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 191 | */ |
| 192 | void tracing_off_permanent(void) |
| 193 | { |
| 194 | set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 195 | } |
| 196 | |
Steven Rostedt | 988ae9d | 2009-02-14 19:17:02 -0500 | [diff] [blame] | 197 | /** |
| 198 | * tracing_is_on - show state of ring buffers enabled |
| 199 | */ |
| 200 | int tracing_is_on(void) |
| 201 | { |
| 202 | return ring_buffer_flags == RB_BUFFERS_ON; |
| 203 | } |
| 204 | EXPORT_SYMBOL_GPL(tracing_is_on); |
| 205 | |
Steven Rostedt | e3d6bf0 | 2009-03-03 13:53:07 -0500 | [diff] [blame] | 206 | #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array)) |
Andrew Morton | 67d3472 | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 207 | #define RB_ALIGNMENT 4U |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 208 | #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] | 209 | #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */ |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 210 | |
Steven Rostedt | 2271048 | 2010-03-18 17:54:19 -0400 | [diff] [blame] | 211 | #if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) |
| 212 | # define RB_FORCE_8BYTE_ALIGNMENT 0 |
| 213 | # define RB_ARCH_ALIGNMENT RB_ALIGNMENT |
| 214 | #else |
| 215 | # define RB_FORCE_8BYTE_ALIGNMENT 1 |
| 216 | # define RB_ARCH_ALIGNMENT 8U |
| 217 | #endif |
| 218 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 219 | /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */ |
| 220 | #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 221 | |
| 222 | enum { |
| 223 | RB_LEN_TIME_EXTEND = 8, |
| 224 | RB_LEN_TIME_STAMP = 16, |
| 225 | }; |
| 226 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 227 | static inline int rb_null_event(struct ring_buffer_event *event) |
| 228 | { |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 229 | return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static void rb_event_set_padding(struct ring_buffer_event *event) |
| 233 | { |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 234 | /* padding has a NULL time_delta */ |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 235 | event->type_len = RINGBUF_TYPE_PADDING; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 236 | event->time_delta = 0; |
| 237 | } |
| 238 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 239 | static unsigned |
| 240 | rb_event_data_length(struct ring_buffer_event *event) |
| 241 | { |
| 242 | unsigned length; |
| 243 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 244 | if (event->type_len) |
| 245 | length = event->type_len * RB_ALIGNMENT; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 246 | else |
| 247 | length = event->array[0]; |
| 248 | return length + RB_EVNT_HDR_SIZE; |
| 249 | } |
| 250 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 251 | /* inline for ring buffer fast paths */ |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 252 | static unsigned |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 253 | rb_event_length(struct ring_buffer_event *event) |
| 254 | { |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 255 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 256 | case RINGBUF_TYPE_PADDING: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 257 | if (rb_null_event(event)) |
| 258 | /* undefined */ |
| 259 | return -1; |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 260 | return event->array[0] + RB_EVNT_HDR_SIZE; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 261 | |
| 262 | case RINGBUF_TYPE_TIME_EXTEND: |
| 263 | return RB_LEN_TIME_EXTEND; |
| 264 | |
| 265 | case RINGBUF_TYPE_TIME_STAMP: |
| 266 | return RB_LEN_TIME_STAMP; |
| 267 | |
| 268 | case RINGBUF_TYPE_DATA: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 269 | return rb_event_data_length(event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 270 | default: |
| 271 | BUG(); |
| 272 | } |
| 273 | /* not hit */ |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * ring_buffer_event_length - return the length of the event |
| 279 | * @event: the event to get the length of |
| 280 | */ |
| 281 | unsigned ring_buffer_event_length(struct ring_buffer_event *event) |
| 282 | { |
Robert Richter | 465634a | 2009-01-07 15:32:11 +0100 | [diff] [blame] | 283 | unsigned length = rb_event_length(event); |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 284 | if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
Robert Richter | 465634a | 2009-01-07 15:32:11 +0100 | [diff] [blame] | 285 | return length; |
| 286 | length -= RB_EVNT_HDR_SIZE; |
| 287 | if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0])) |
| 288 | length -= sizeof(event->array[0]); |
| 289 | return length; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 290 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 291 | EXPORT_SYMBOL_GPL(ring_buffer_event_length); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 292 | |
| 293 | /* inline for ring buffer fast paths */ |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 294 | static void * |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 295 | rb_event_data(struct ring_buffer_event *event) |
| 296 | { |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 297 | BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 298 | /* If length is in len field, then array[0] has the data */ |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 299 | if (event->type_len) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 300 | return (void *)&event->array[0]; |
| 301 | /* Otherwise length is in array[0] and array[1] has the data */ |
| 302 | return (void *)&event->array[1]; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * ring_buffer_event_data - return the data of the event |
| 307 | * @event: the event to get the data from |
| 308 | */ |
| 309 | void *ring_buffer_event_data(struct ring_buffer_event *event) |
| 310 | { |
| 311 | return rb_event_data(event); |
| 312 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 313 | EXPORT_SYMBOL_GPL(ring_buffer_event_data); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 314 | |
| 315 | #define for_each_buffer_cpu(buffer, cpu) \ |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 316 | for_each_cpu(cpu, buffer->cpumask) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 317 | |
| 318 | #define TS_SHIFT 27 |
| 319 | #define TS_MASK ((1ULL << TS_SHIFT) - 1) |
| 320 | #define TS_DELTA_TEST (~TS_MASK) |
| 321 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 322 | /* Flag when events were overwritten */ |
| 323 | #define RB_MISSED_EVENTS (1 << 31) |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 324 | /* Missed count stored at end */ |
| 325 | #define RB_MISSED_STORED (1 << 30) |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 326 | |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 327 | struct buffer_data_page { |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 328 | u64 time_stamp; /* page time stamp */ |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 329 | local_t commit; /* write committed index */ |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 330 | unsigned char data[]; /* data of buffer page */ |
| 331 | }; |
| 332 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 333 | /* |
| 334 | * Note, the buffer_page list must be first. The buffer pages |
| 335 | * are allocated in cache lines, which means that each buffer |
| 336 | * page will be at the beginning of a cache line, and thus |
| 337 | * the least significant bits will be zero. We use this to |
| 338 | * add flags in the list struct pointers, to make the ring buffer |
| 339 | * lockless. |
| 340 | */ |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 341 | struct buffer_page { |
Steven Rostedt | 778c55d | 2009-05-01 18:44:45 -0400 | [diff] [blame] | 342 | struct list_head list; /* list of buffer pages */ |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 343 | local_t write; /* index for next write */ |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 344 | unsigned read; /* index for next read */ |
Steven Rostedt | 778c55d | 2009-05-01 18:44:45 -0400 | [diff] [blame] | 345 | local_t entries; /* entries on this page */ |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 346 | unsigned long real_end; /* real end of data */ |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 347 | struct buffer_data_page *page; /* Actual data page */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 348 | }; |
| 349 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 350 | /* |
| 351 | * The buffer page counters, write and entries, must be reset |
| 352 | * atomically when crossing page boundaries. To synchronize this |
| 353 | * update, two counters are inserted into the number. One is |
| 354 | * the actual counter for the write position or count on the page. |
| 355 | * |
| 356 | * The other is a counter of updaters. Before an update happens |
| 357 | * the update partition of the counter is incremented. This will |
| 358 | * allow the updater to update the counter atomically. |
| 359 | * |
| 360 | * The counter is 20 bits, and the state data is 12. |
| 361 | */ |
| 362 | #define RB_WRITE_MASK 0xfffff |
| 363 | #define RB_WRITE_INTCNT (1 << 20) |
| 364 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 365 | static void rb_init_page(struct buffer_data_page *bpage) |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 366 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 367 | local_set(&bpage->commit, 0); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 368 | } |
| 369 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 370 | /** |
| 371 | * ring_buffer_page_len - the size of data on the page. |
| 372 | * @page: The page to read |
| 373 | * |
| 374 | * Returns the amount of data on the page, including buffer page header. |
| 375 | */ |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 376 | size_t ring_buffer_page_len(void *page) |
| 377 | { |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 378 | return local_read(&((struct buffer_data_page *)page)->commit) |
| 379 | + BUF_PAGE_HDR_SIZE; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 380 | } |
| 381 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 382 | /* |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 383 | * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing |
| 384 | * this issue out. |
| 385 | */ |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 386 | static void free_buffer_page(struct buffer_page *bpage) |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 387 | { |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 388 | free_page((unsigned long)bpage->page); |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 389 | kfree(bpage); |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /* |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 393 | * We need to fit the time_stamp delta into 27 bits. |
| 394 | */ |
| 395 | static inline int test_time_stamp(u64 delta) |
| 396 | { |
| 397 | if (delta & TS_DELTA_TEST) |
| 398 | return 1; |
| 399 | return 0; |
| 400 | } |
| 401 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 402 | #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 403 | |
Steven Rostedt | be957c4 | 2009-05-11 14:42:53 -0400 | [diff] [blame] | 404 | /* Max payload is BUF_PAGE_SIZE - header (8bytes) */ |
| 405 | #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2)) |
| 406 | |
Steven Rostedt | ea05b57 | 2009-06-03 09:30:10 -0400 | [diff] [blame] | 407 | /* Max number of timestamps that can fit on a page */ |
| 408 | #define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP) |
| 409 | |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 410 | int ring_buffer_print_page_header(struct trace_seq *s) |
| 411 | { |
| 412 | struct buffer_data_page field; |
| 413 | int ret; |
| 414 | |
| 415 | ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t" |
Tom Zanussi | 26a5074 | 2009-10-06 01:09:50 -0500 | [diff] [blame] | 416 | "offset:0;\tsize:%u;\tsigned:%u;\n", |
| 417 | (unsigned int)sizeof(field.time_stamp), |
| 418 | (unsigned int)is_signed_type(u64)); |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 419 | |
| 420 | ret = trace_seq_printf(s, "\tfield: local_t commit;\t" |
Tom Zanussi | 26a5074 | 2009-10-06 01:09:50 -0500 | [diff] [blame] | 421 | "offset:%u;\tsize:%u;\tsigned:%u;\n", |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 422 | (unsigned int)offsetof(typeof(field), commit), |
Tom Zanussi | 26a5074 | 2009-10-06 01:09:50 -0500 | [diff] [blame] | 423 | (unsigned int)sizeof(field.commit), |
| 424 | (unsigned int)is_signed_type(long)); |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 425 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 426 | ret = trace_seq_printf(s, "\tfield: int overwrite;\t" |
| 427 | "offset:%u;\tsize:%u;\tsigned:%u;\n", |
| 428 | (unsigned int)offsetof(typeof(field), commit), |
| 429 | 1, |
| 430 | (unsigned int)is_signed_type(long)); |
| 431 | |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 432 | ret = trace_seq_printf(s, "\tfield: char data;\t" |
Tom Zanussi | 26a5074 | 2009-10-06 01:09:50 -0500 | [diff] [blame] | 433 | "offset:%u;\tsize:%u;\tsigned:%u;\n", |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 434 | (unsigned int)offsetof(typeof(field), data), |
Tom Zanussi | 26a5074 | 2009-10-06 01:09:50 -0500 | [diff] [blame] | 435 | (unsigned int)BUF_PAGE_SIZE, |
| 436 | (unsigned int)is_signed_type(char)); |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 437 | |
| 438 | return ret; |
| 439 | } |
| 440 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 441 | /* |
| 442 | * head_page == tail_page && head == tail then buffer is empty. |
| 443 | */ |
| 444 | struct ring_buffer_per_cpu { |
| 445 | int cpu; |
| 446 | struct ring_buffer *buffer; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 447 | spinlock_t reader_lock; /* serialize readers */ |
Thomas Gleixner | 445c895 | 2009-12-02 19:49:50 +0100 | [diff] [blame] | 448 | arch_spinlock_t lock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 449 | struct lock_class_key lock_key; |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 450 | struct list_head *pages; |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 451 | struct buffer_page *head_page; /* read from head */ |
| 452 | struct buffer_page *tail_page; /* write to tail */ |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 453 | struct buffer_page *commit_page; /* committed pages */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 454 | struct buffer_page *reader_page; |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 455 | unsigned long lost_events; |
| 456 | unsigned long last_overrun; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 457 | local_t commit_overrun; |
| 458 | local_t overrun; |
Steven Rostedt | e4906ef | 2009-04-30 20:49:44 -0400 | [diff] [blame] | 459 | local_t entries; |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 460 | local_t committing; |
| 461 | local_t commits; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 462 | unsigned long read; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 463 | u64 write_stamp; |
| 464 | u64 read_stamp; |
| 465 | atomic_t record_disabled; |
| 466 | }; |
| 467 | |
| 468 | struct ring_buffer { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 469 | unsigned pages; |
| 470 | unsigned flags; |
| 471 | int cpus; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 472 | atomic_t record_disabled; |
Arnaldo Carvalho de Melo | 00f62f6 | 2009-02-09 17:04:06 -0200 | [diff] [blame] | 473 | cpumask_var_t cpumask; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 474 | |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 475 | struct lock_class_key *reader_lock_key; |
| 476 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 477 | struct mutex mutex; |
| 478 | |
| 479 | struct ring_buffer_per_cpu **buffers; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 480 | |
Steven Rostedt | 59222ef | 2009-03-12 11:46:03 -0400 | [diff] [blame] | 481 | #ifdef CONFIG_HOTPLUG_CPU |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 482 | struct notifier_block cpu_notify; |
| 483 | #endif |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 484 | u64 (*clock)(void); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 485 | }; |
| 486 | |
| 487 | struct ring_buffer_iter { |
| 488 | struct ring_buffer_per_cpu *cpu_buffer; |
| 489 | unsigned long head; |
| 490 | struct buffer_page *head_page; |
Steven Rostedt | 492a74f | 2010-01-25 15:17:47 -0500 | [diff] [blame] | 491 | struct buffer_page *cache_reader_page; |
| 492 | unsigned long cache_read; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 493 | u64 read_stamp; |
| 494 | }; |
| 495 | |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 496 | /* buffer may be either ring_buffer or ring_buffer_per_cpu */ |
Steven Rostedt | 077c540 | 2009-09-03 19:53:46 -0400 | [diff] [blame] | 497 | #define RB_WARN_ON(b, cond) \ |
| 498 | ({ \ |
| 499 | int _____ret = unlikely(cond); \ |
| 500 | if (_____ret) { \ |
| 501 | if (__same_type(*(b), struct ring_buffer_per_cpu)) { \ |
| 502 | struct ring_buffer_per_cpu *__b = \ |
| 503 | (void *)b; \ |
| 504 | atomic_inc(&__b->buffer->record_disabled); \ |
| 505 | } else \ |
| 506 | atomic_inc(&b->record_disabled); \ |
| 507 | WARN_ON(1); \ |
| 508 | } \ |
| 509 | _____ret; \ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 510 | }) |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 511 | |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 512 | /* Up this if you want to test the TIME_EXTENTS and normalization */ |
| 513 | #define DEBUG_SHIFT 0 |
| 514 | |
Jiri Olsa | 6d3f1e1 | 2009-10-23 19:36:19 -0400 | [diff] [blame] | 515 | static inline u64 rb_time_stamp(struct ring_buffer *buffer) |
Steven Rostedt | 88eb012 | 2009-05-11 16:28:23 -0400 | [diff] [blame] | 516 | { |
| 517 | /* shift to debug/test normalization and TIME_EXTENTS */ |
| 518 | return buffer->clock() << DEBUG_SHIFT; |
| 519 | } |
| 520 | |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 521 | u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu) |
| 522 | { |
| 523 | u64 time; |
| 524 | |
| 525 | preempt_disable_notrace(); |
Jiri Olsa | 6d3f1e1 | 2009-10-23 19:36:19 -0400 | [diff] [blame] | 526 | time = rb_time_stamp(buffer); |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 527 | preempt_enable_no_resched_notrace(); |
| 528 | |
| 529 | return time; |
| 530 | } |
| 531 | EXPORT_SYMBOL_GPL(ring_buffer_time_stamp); |
| 532 | |
| 533 | void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer, |
| 534 | int cpu, u64 *ts) |
| 535 | { |
| 536 | /* Just stupid testing the normalize function and deltas */ |
| 537 | *ts >>= DEBUG_SHIFT; |
| 538 | } |
| 539 | EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp); |
| 540 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 541 | /* |
| 542 | * Making the ring buffer lockless makes things tricky. |
| 543 | * Although writes only happen on the CPU that they are on, |
| 544 | * and they only need to worry about interrupts. Reads can |
| 545 | * happen on any CPU. |
| 546 | * |
| 547 | * The reader page is always off the ring buffer, but when the |
| 548 | * reader finishes with a page, it needs to swap its page with |
| 549 | * a new one from the buffer. The reader needs to take from |
| 550 | * the head (writes go to the tail). But if a writer is in overwrite |
| 551 | * mode and wraps, it must push the head page forward. |
| 552 | * |
| 553 | * Here lies the problem. |
| 554 | * |
| 555 | * The reader must be careful to replace only the head page, and |
| 556 | * not another one. As described at the top of the file in the |
| 557 | * ASCII art, the reader sets its old page to point to the next |
| 558 | * page after head. It then sets the page after head to point to |
| 559 | * the old reader page. But if the writer moves the head page |
| 560 | * during this operation, the reader could end up with the tail. |
| 561 | * |
| 562 | * We use cmpxchg to help prevent this race. We also do something |
| 563 | * special with the page before head. We set the LSB to 1. |
| 564 | * |
| 565 | * When the writer must push the page forward, it will clear the |
| 566 | * bit that points to the head page, move the head, and then set |
| 567 | * the bit that points to the new head page. |
| 568 | * |
| 569 | * We also don't want an interrupt coming in and moving the head |
| 570 | * page on another writer. Thus we use the second LSB to catch |
| 571 | * that too. Thus: |
| 572 | * |
| 573 | * head->list->prev->next bit 1 bit 0 |
| 574 | * ------- ------- |
| 575 | * Normal page 0 0 |
| 576 | * Points to head page 0 1 |
| 577 | * New head page 1 0 |
| 578 | * |
| 579 | * Note we can not trust the prev pointer of the head page, because: |
| 580 | * |
| 581 | * +----+ +-----+ +-----+ |
| 582 | * | |------>| T |---X--->| N | |
| 583 | * | |<------| | | | |
| 584 | * +----+ +-----+ +-----+ |
| 585 | * ^ ^ | |
| 586 | * | +-----+ | | |
| 587 | * +----------| R |----------+ | |
| 588 | * | |<-----------+ |
| 589 | * +-----+ |
| 590 | * |
| 591 | * Key: ---X--> HEAD flag set in pointer |
| 592 | * T Tail page |
| 593 | * R Reader page |
| 594 | * N Next page |
| 595 | * |
| 596 | * (see __rb_reserve_next() to see where this happens) |
| 597 | * |
| 598 | * What the above shows is that the reader just swapped out |
| 599 | * the reader page with a page in the buffer, but before it |
| 600 | * could make the new header point back to the new page added |
| 601 | * it was preempted by a writer. The writer moved forward onto |
| 602 | * the new page added by the reader and is about to move forward |
| 603 | * again. |
| 604 | * |
| 605 | * You can see, it is legitimate for the previous pointer of |
| 606 | * the head (or any page) not to point back to itself. But only |
| 607 | * temporarially. |
| 608 | */ |
| 609 | |
| 610 | #define RB_PAGE_NORMAL 0UL |
| 611 | #define RB_PAGE_HEAD 1UL |
| 612 | #define RB_PAGE_UPDATE 2UL |
| 613 | |
| 614 | |
| 615 | #define RB_FLAG_MASK 3UL |
| 616 | |
| 617 | /* PAGE_MOVED is not part of the mask */ |
| 618 | #define RB_PAGE_MOVED 4UL |
| 619 | |
| 620 | /* |
| 621 | * rb_list_head - remove any bit |
| 622 | */ |
| 623 | static struct list_head *rb_list_head(struct list_head *list) |
| 624 | { |
| 625 | unsigned long val = (unsigned long)list; |
| 626 | |
| 627 | return (struct list_head *)(val & ~RB_FLAG_MASK); |
| 628 | } |
| 629 | |
| 630 | /* |
Jiri Olsa | 6d3f1e1 | 2009-10-23 19:36:19 -0400 | [diff] [blame] | 631 | * 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] | 632 | * |
| 633 | * Because the reader may move the head_page pointer, we can |
| 634 | * not trust what the head page is (it may be pointing to |
| 635 | * the reader page). But if the next page is a header page, |
| 636 | * its flags will be non zero. |
| 637 | */ |
| 638 | static int inline |
| 639 | rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer, |
| 640 | struct buffer_page *page, struct list_head *list) |
| 641 | { |
| 642 | unsigned long val; |
| 643 | |
| 644 | val = (unsigned long)list->next; |
| 645 | |
| 646 | if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list) |
| 647 | return RB_PAGE_MOVED; |
| 648 | |
| 649 | return val & RB_FLAG_MASK; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * rb_is_reader_page |
| 654 | * |
| 655 | * The unique thing about the reader page, is that, if the |
| 656 | * writer is ever on it, the previous pointer never points |
| 657 | * back to the reader page. |
| 658 | */ |
| 659 | static int rb_is_reader_page(struct buffer_page *page) |
| 660 | { |
| 661 | struct list_head *list = page->list.prev; |
| 662 | |
| 663 | return rb_list_head(list->next) != &page->list; |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * rb_set_list_to_head - set a list_head to be pointing to head. |
| 668 | */ |
| 669 | static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer, |
| 670 | struct list_head *list) |
| 671 | { |
| 672 | unsigned long *ptr; |
| 673 | |
| 674 | ptr = (unsigned long *)&list->next; |
| 675 | *ptr |= RB_PAGE_HEAD; |
| 676 | *ptr &= ~RB_PAGE_UPDATE; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * rb_head_page_activate - sets up head page |
| 681 | */ |
| 682 | static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer) |
| 683 | { |
| 684 | struct buffer_page *head; |
| 685 | |
| 686 | head = cpu_buffer->head_page; |
| 687 | if (!head) |
| 688 | return; |
| 689 | |
| 690 | /* |
| 691 | * Set the previous list pointer to have the HEAD flag. |
| 692 | */ |
| 693 | rb_set_list_to_head(cpu_buffer, head->list.prev); |
| 694 | } |
| 695 | |
| 696 | static void rb_list_head_clear(struct list_head *list) |
| 697 | { |
| 698 | unsigned long *ptr = (unsigned long *)&list->next; |
| 699 | |
| 700 | *ptr &= ~RB_FLAG_MASK; |
| 701 | } |
| 702 | |
| 703 | /* |
| 704 | * rb_head_page_dactivate - clears head page ptr (for free list) |
| 705 | */ |
| 706 | static void |
| 707 | rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer) |
| 708 | { |
| 709 | struct list_head *hd; |
| 710 | |
| 711 | /* Go through the whole list and clear any pointers found. */ |
| 712 | rb_list_head_clear(cpu_buffer->pages); |
| 713 | |
| 714 | list_for_each(hd, cpu_buffer->pages) |
| 715 | rb_list_head_clear(hd); |
| 716 | } |
| 717 | |
| 718 | static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer, |
| 719 | struct buffer_page *head, |
| 720 | struct buffer_page *prev, |
| 721 | int old_flag, int new_flag) |
| 722 | { |
| 723 | struct list_head *list; |
| 724 | unsigned long val = (unsigned long)&head->list; |
| 725 | unsigned long ret; |
| 726 | |
| 727 | list = &prev->list; |
| 728 | |
| 729 | val &= ~RB_FLAG_MASK; |
| 730 | |
Steven Rostedt | 08a4081 | 2009-09-14 09:31:35 -0400 | [diff] [blame] | 731 | ret = cmpxchg((unsigned long *)&list->next, |
| 732 | val | old_flag, val | new_flag); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 733 | |
| 734 | /* check if the reader took the page */ |
| 735 | if ((ret & ~RB_FLAG_MASK) != val) |
| 736 | return RB_PAGE_MOVED; |
| 737 | |
| 738 | return ret & RB_FLAG_MASK; |
| 739 | } |
| 740 | |
| 741 | static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer, |
| 742 | struct buffer_page *head, |
| 743 | struct buffer_page *prev, |
| 744 | int old_flag) |
| 745 | { |
| 746 | return rb_head_page_set(cpu_buffer, head, prev, |
| 747 | old_flag, RB_PAGE_UPDATE); |
| 748 | } |
| 749 | |
| 750 | static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer, |
| 751 | struct buffer_page *head, |
| 752 | struct buffer_page *prev, |
| 753 | int old_flag) |
| 754 | { |
| 755 | return rb_head_page_set(cpu_buffer, head, prev, |
| 756 | old_flag, RB_PAGE_HEAD); |
| 757 | } |
| 758 | |
| 759 | static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer, |
| 760 | struct buffer_page *head, |
| 761 | struct buffer_page *prev, |
| 762 | int old_flag) |
| 763 | { |
| 764 | return rb_head_page_set(cpu_buffer, head, prev, |
| 765 | old_flag, RB_PAGE_NORMAL); |
| 766 | } |
| 767 | |
| 768 | static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer, |
| 769 | struct buffer_page **bpage) |
| 770 | { |
| 771 | struct list_head *p = rb_list_head((*bpage)->list.next); |
| 772 | |
| 773 | *bpage = list_entry(p, struct buffer_page, list); |
| 774 | } |
| 775 | |
| 776 | static struct buffer_page * |
| 777 | rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer) |
| 778 | { |
| 779 | struct buffer_page *head; |
| 780 | struct buffer_page *page; |
| 781 | struct list_head *list; |
| 782 | int i; |
| 783 | |
| 784 | if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page)) |
| 785 | return NULL; |
| 786 | |
| 787 | /* sanity check */ |
| 788 | list = cpu_buffer->pages; |
| 789 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list)) |
| 790 | return NULL; |
| 791 | |
| 792 | page = head = cpu_buffer->head_page; |
| 793 | /* |
| 794 | * It is possible that the writer moves the header behind |
| 795 | * where we started, and we miss in one loop. |
| 796 | * A second loop should grab the header, but we'll do |
| 797 | * three loops just because I'm paranoid. |
| 798 | */ |
| 799 | for (i = 0; i < 3; i++) { |
| 800 | do { |
| 801 | if (rb_is_head_page(cpu_buffer, page, page->list.prev)) { |
| 802 | cpu_buffer->head_page = page; |
| 803 | return page; |
| 804 | } |
| 805 | rb_inc_page(cpu_buffer, &page); |
| 806 | } while (page != head); |
| 807 | } |
| 808 | |
| 809 | RB_WARN_ON(cpu_buffer, 1); |
| 810 | |
| 811 | return NULL; |
| 812 | } |
| 813 | |
| 814 | static int rb_head_page_replace(struct buffer_page *old, |
| 815 | struct buffer_page *new) |
| 816 | { |
| 817 | unsigned long *ptr = (unsigned long *)&old->list.prev->next; |
| 818 | unsigned long val; |
| 819 | unsigned long ret; |
| 820 | |
| 821 | val = *ptr & ~RB_FLAG_MASK; |
| 822 | val |= RB_PAGE_HEAD; |
| 823 | |
Steven Rostedt | 08a4081 | 2009-09-14 09:31:35 -0400 | [diff] [blame] | 824 | ret = cmpxchg(ptr, val, (unsigned long)&new->list); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 825 | |
| 826 | return ret == val; |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * rb_tail_page_update - move the tail page forward |
| 831 | * |
| 832 | * Returns 1 if moved tail page, 0 if someone else did. |
| 833 | */ |
| 834 | static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer, |
| 835 | struct buffer_page *tail_page, |
| 836 | struct buffer_page *next_page) |
| 837 | { |
| 838 | struct buffer_page *old_tail; |
| 839 | unsigned long old_entries; |
| 840 | unsigned long old_write; |
| 841 | int ret = 0; |
| 842 | |
| 843 | /* |
| 844 | * The tail page now needs to be moved forward. |
| 845 | * |
| 846 | * We need to reset the tail page, but without messing |
| 847 | * with possible erasing of data brought in by interrupts |
| 848 | * that have moved the tail page and are currently on it. |
| 849 | * |
| 850 | * We add a counter to the write field to denote this. |
| 851 | */ |
| 852 | old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write); |
| 853 | old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries); |
| 854 | |
| 855 | /* |
| 856 | * Just make sure we have seen our old_write and synchronize |
| 857 | * with any interrupts that come in. |
| 858 | */ |
| 859 | barrier(); |
| 860 | |
| 861 | /* |
| 862 | * If the tail page is still the same as what we think |
| 863 | * it is, then it is up to us to update the tail |
| 864 | * pointer. |
| 865 | */ |
| 866 | if (tail_page == cpu_buffer->tail_page) { |
| 867 | /* Zero the write counter */ |
| 868 | unsigned long val = old_write & ~RB_WRITE_MASK; |
| 869 | unsigned long eval = old_entries & ~RB_WRITE_MASK; |
| 870 | |
| 871 | /* |
| 872 | * This will only succeed if an interrupt did |
| 873 | * not come in and change it. In which case, we |
| 874 | * do not want to modify it. |
Lai Jiangshan | da706d8 | 2009-07-15 16:27:30 +0800 | [diff] [blame] | 875 | * |
| 876 | * We add (void) to let the compiler know that we do not care |
| 877 | * about the return value of these functions. We use the |
| 878 | * cmpxchg to only update if an interrupt did not already |
| 879 | * do it for us. If the cmpxchg fails, we don't care. |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 880 | */ |
Lai Jiangshan | da706d8 | 2009-07-15 16:27:30 +0800 | [diff] [blame] | 881 | (void)local_cmpxchg(&next_page->write, old_write, val); |
| 882 | (void)local_cmpxchg(&next_page->entries, old_entries, eval); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 883 | |
| 884 | /* |
| 885 | * No need to worry about races with clearing out the commit. |
| 886 | * it only can increment when a commit takes place. But that |
| 887 | * only happens in the outer most nested commit. |
| 888 | */ |
| 889 | local_set(&next_page->page->commit, 0); |
| 890 | |
| 891 | old_tail = cmpxchg(&cpu_buffer->tail_page, |
| 892 | tail_page, next_page); |
| 893 | |
| 894 | if (old_tail == tail_page) |
| 895 | ret = 1; |
| 896 | } |
| 897 | |
| 898 | return ret; |
| 899 | } |
| 900 | |
| 901 | static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer, |
| 902 | struct buffer_page *bpage) |
| 903 | { |
| 904 | unsigned long val = (unsigned long)bpage; |
| 905 | |
| 906 | if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK)) |
| 907 | return 1; |
| 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | * rb_check_list - make sure a pointer to a list has the last bits zero |
| 914 | */ |
| 915 | static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer, |
| 916 | struct list_head *list) |
| 917 | { |
| 918 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev)) |
| 919 | return 1; |
| 920 | if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next)) |
| 921 | return 1; |
| 922 | return 0; |
| 923 | } |
| 924 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 925 | /** |
| 926 | * check_pages - integrity check of buffer pages |
| 927 | * @cpu_buffer: CPU buffer with pages to test |
| 928 | * |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 929 | * 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] | 930 | * been corrupted. |
| 931 | */ |
| 932 | static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer) |
| 933 | { |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 934 | struct list_head *head = cpu_buffer->pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 935 | struct buffer_page *bpage, *tmp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 936 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 937 | rb_head_page_deactivate(cpu_buffer); |
| 938 | |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 939 | if (RB_WARN_ON(cpu_buffer, head->next->prev != head)) |
| 940 | return -1; |
| 941 | if (RB_WARN_ON(cpu_buffer, head->prev->next != head)) |
| 942 | return -1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 943 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 944 | if (rb_check_list(cpu_buffer, head)) |
| 945 | return -1; |
| 946 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 947 | list_for_each_entry_safe(bpage, tmp, head, list) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 948 | if (RB_WARN_ON(cpu_buffer, |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 949 | bpage->list.next->prev != &bpage->list)) |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 950 | return -1; |
| 951 | if (RB_WARN_ON(cpu_buffer, |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 952 | bpage->list.prev->next != &bpage->list)) |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 953 | return -1; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 954 | if (rb_check_list(cpu_buffer, &bpage->list)) |
| 955 | return -1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 956 | } |
| 957 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 958 | rb_head_page_activate(cpu_buffer); |
| 959 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 960 | return 0; |
| 961 | } |
| 962 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 963 | static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, |
| 964 | unsigned nr_pages) |
| 965 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 966 | struct buffer_page *bpage, *tmp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 967 | unsigned long addr; |
| 968 | LIST_HEAD(pages); |
| 969 | unsigned i; |
| 970 | |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 971 | WARN_ON(!nr_pages); |
| 972 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 973 | for (i = 0; i < nr_pages; i++) { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 974 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
Steven Rostedt | aa1e0e3b | 2008-10-02 19:18:09 -0400 | [diff] [blame] | 975 | GFP_KERNEL, cpu_to_node(cpu_buffer->cpu)); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 976 | if (!bpage) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 977 | goto free_pages; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 978 | |
| 979 | rb_check_bpage(cpu_buffer, bpage); |
| 980 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 981 | list_add(&bpage->list, &pages); |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 982 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 983 | addr = __get_free_page(GFP_KERNEL); |
| 984 | if (!addr) |
| 985 | goto free_pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 986 | bpage->page = (void *)addr; |
| 987 | rb_init_page(bpage->page); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 988 | } |
| 989 | |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 990 | /* |
| 991 | * The ring buffer page list is a circular list that does not |
| 992 | * start and end with a list head. All page list items point to |
| 993 | * other pages. |
| 994 | */ |
| 995 | cpu_buffer->pages = pages.next; |
| 996 | list_del(&pages); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 997 | |
| 998 | rb_check_pages(cpu_buffer); |
| 999 | |
| 1000 | return 0; |
| 1001 | |
| 1002 | free_pages: |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1003 | list_for_each_entry_safe(bpage, tmp, &pages, list) { |
| 1004 | list_del_init(&bpage->list); |
| 1005 | free_buffer_page(bpage); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1006 | } |
| 1007 | return -ENOMEM; |
| 1008 | } |
| 1009 | |
| 1010 | static struct ring_buffer_per_cpu * |
| 1011 | rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu) |
| 1012 | { |
| 1013 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1014 | struct buffer_page *bpage; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1015 | unsigned long addr; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1016 | int ret; |
| 1017 | |
| 1018 | cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()), |
| 1019 | GFP_KERNEL, cpu_to_node(cpu)); |
| 1020 | if (!cpu_buffer) |
| 1021 | return NULL; |
| 1022 | |
| 1023 | cpu_buffer->cpu = cpu; |
| 1024 | cpu_buffer->buffer = buffer; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1025 | spin_lock_init(&cpu_buffer->reader_lock); |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 1026 | lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key); |
Thomas Gleixner | edc35bd | 2009-12-03 12:38:57 +0100 | [diff] [blame] | 1027 | cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1028 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1029 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1030 | GFP_KERNEL, cpu_to_node(cpu)); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1031 | if (!bpage) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1032 | goto fail_free_buffer; |
| 1033 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1034 | rb_check_bpage(cpu_buffer, bpage); |
| 1035 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1036 | cpu_buffer->reader_page = bpage; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1037 | addr = __get_free_page(GFP_KERNEL); |
| 1038 | if (!addr) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1039 | goto fail_free_reader; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1040 | bpage->page = (void *)addr; |
| 1041 | rb_init_page(bpage->page); |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1042 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1043 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1044 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1045 | ret = rb_allocate_pages(cpu_buffer, buffer->pages); |
| 1046 | if (ret < 0) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1047 | goto fail_free_reader; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1048 | |
| 1049 | cpu_buffer->head_page |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1050 | = list_entry(cpu_buffer->pages, struct buffer_page, list); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1051 | cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1052 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1053 | rb_head_page_activate(cpu_buffer); |
| 1054 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1055 | return cpu_buffer; |
| 1056 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1057 | fail_free_reader: |
| 1058 | free_buffer_page(cpu_buffer->reader_page); |
| 1059 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1060 | fail_free_buffer: |
| 1061 | kfree(cpu_buffer); |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | |
| 1065 | static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer) |
| 1066 | { |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1067 | struct list_head *head = cpu_buffer->pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1068 | struct buffer_page *bpage, *tmp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1069 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1070 | free_buffer_page(cpu_buffer->reader_page); |
| 1071 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1072 | rb_head_page_deactivate(cpu_buffer); |
| 1073 | |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1074 | if (head) { |
| 1075 | list_for_each_entry_safe(bpage, tmp, head, list) { |
| 1076 | list_del_init(&bpage->list); |
| 1077 | free_buffer_page(bpage); |
| 1078 | } |
| 1079 | bpage = list_entry(head, struct buffer_page, list); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1080 | free_buffer_page(bpage); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1081 | } |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1082 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1083 | kfree(cpu_buffer); |
| 1084 | } |
| 1085 | |
Steven Rostedt | 59222ef | 2009-03-12 11:46:03 -0400 | [diff] [blame] | 1086 | #ifdef CONFIG_HOTPLUG_CPU |
Frederic Weisbecker | 09c9e84 | 2009-03-21 04:33:36 +0100 | [diff] [blame] | 1087 | static int rb_cpu_notify(struct notifier_block *self, |
| 1088 | unsigned long action, void *hcpu); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1089 | #endif |
| 1090 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1091 | /** |
| 1092 | * ring_buffer_alloc - allocate a new ring_buffer |
Robert Richter | 68814b5 | 2008-11-24 12:24:12 +0100 | [diff] [blame] | 1093 | * @size: the size in bytes per cpu that is needed. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1094 | * @flags: attributes to set for the ring buffer. |
| 1095 | * |
| 1096 | * Currently the only flag that is available is the RB_FL_OVERWRITE |
| 1097 | * flag. This flag means that the buffer will overwrite old data |
| 1098 | * when the buffer wraps. If this flag is not set, the buffer will |
| 1099 | * drop data when the tail hits the head. |
| 1100 | */ |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 1101 | struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags, |
| 1102 | struct lock_class_key *key) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1103 | { |
| 1104 | struct ring_buffer *buffer; |
| 1105 | int bsize; |
| 1106 | int cpu; |
| 1107 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1108 | /* keep it in its own cache line */ |
| 1109 | buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()), |
| 1110 | GFP_KERNEL); |
| 1111 | if (!buffer) |
| 1112 | return NULL; |
| 1113 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1114 | if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL)) |
| 1115 | goto fail_free_buffer; |
| 1116 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1117 | buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
| 1118 | buffer->flags = flags; |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 1119 | buffer->clock = trace_clock_local; |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 1120 | buffer->reader_lock_key = key; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1121 | |
| 1122 | /* need at least two pages */ |
Steven Rostedt | 5f78abe | 2009-06-17 14:11:10 -0400 | [diff] [blame] | 1123 | if (buffer->pages < 2) |
| 1124 | buffer->pages = 2; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1125 | |
Frederic Weisbecker | 3bf832c | 2009-03-19 14:47:33 +0100 | [diff] [blame] | 1126 | /* |
| 1127 | * In case of non-hotplug cpu, if the ring-buffer is allocated |
| 1128 | * in early initcall, it will not be notified of secondary cpus. |
| 1129 | * In that off case, we need to allocate for all possible cpus. |
| 1130 | */ |
| 1131 | #ifdef CONFIG_HOTPLUG_CPU |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1132 | get_online_cpus(); |
| 1133 | cpumask_copy(buffer->cpumask, cpu_online_mask); |
Frederic Weisbecker | 3bf832c | 2009-03-19 14:47:33 +0100 | [diff] [blame] | 1134 | #else |
| 1135 | cpumask_copy(buffer->cpumask, cpu_possible_mask); |
| 1136 | #endif |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1137 | buffer->cpus = nr_cpu_ids; |
| 1138 | |
| 1139 | bsize = sizeof(void *) * nr_cpu_ids; |
| 1140 | buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()), |
| 1141 | GFP_KERNEL); |
| 1142 | if (!buffer->buffers) |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1143 | goto fail_free_cpumask; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1144 | |
| 1145 | for_each_buffer_cpu(buffer, cpu) { |
| 1146 | buffer->buffers[cpu] = |
| 1147 | rb_allocate_cpu_buffer(buffer, cpu); |
| 1148 | if (!buffer->buffers[cpu]) |
| 1149 | goto fail_free_buffers; |
| 1150 | } |
| 1151 | |
Steven Rostedt | 59222ef | 2009-03-12 11:46:03 -0400 | [diff] [blame] | 1152 | #ifdef CONFIG_HOTPLUG_CPU |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1153 | buffer->cpu_notify.notifier_call = rb_cpu_notify; |
| 1154 | buffer->cpu_notify.priority = 0; |
| 1155 | register_cpu_notifier(&buffer->cpu_notify); |
| 1156 | #endif |
| 1157 | |
| 1158 | put_online_cpus(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1159 | mutex_init(&buffer->mutex); |
| 1160 | |
| 1161 | return buffer; |
| 1162 | |
| 1163 | fail_free_buffers: |
| 1164 | for_each_buffer_cpu(buffer, cpu) { |
| 1165 | if (buffer->buffers[cpu]) |
| 1166 | rb_free_cpu_buffer(buffer->buffers[cpu]); |
| 1167 | } |
| 1168 | kfree(buffer->buffers); |
| 1169 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1170 | fail_free_cpumask: |
| 1171 | free_cpumask_var(buffer->cpumask); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1172 | put_online_cpus(); |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1173 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1174 | fail_free_buffer: |
| 1175 | kfree(buffer); |
| 1176 | return NULL; |
| 1177 | } |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 1178 | EXPORT_SYMBOL_GPL(__ring_buffer_alloc); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1179 | |
| 1180 | /** |
| 1181 | * ring_buffer_free - free a ring buffer. |
| 1182 | * @buffer: the buffer to free. |
| 1183 | */ |
| 1184 | void |
| 1185 | ring_buffer_free(struct ring_buffer *buffer) |
| 1186 | { |
| 1187 | int cpu; |
| 1188 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1189 | get_online_cpus(); |
| 1190 | |
Steven Rostedt | 59222ef | 2009-03-12 11:46:03 -0400 | [diff] [blame] | 1191 | #ifdef CONFIG_HOTPLUG_CPU |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1192 | unregister_cpu_notifier(&buffer->cpu_notify); |
| 1193 | #endif |
| 1194 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1195 | for_each_buffer_cpu(buffer, cpu) |
| 1196 | rb_free_cpu_buffer(buffer->buffers[cpu]); |
| 1197 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1198 | put_online_cpus(); |
| 1199 | |
Eric Dumazet | bd3f022 | 2009-08-07 12:49:29 +0200 | [diff] [blame] | 1200 | kfree(buffer->buffers); |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1201 | free_cpumask_var(buffer->cpumask); |
| 1202 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1203 | kfree(buffer); |
| 1204 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1205 | EXPORT_SYMBOL_GPL(ring_buffer_free); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1206 | |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 1207 | void ring_buffer_set_clock(struct ring_buffer *buffer, |
| 1208 | u64 (*clock)(void)) |
| 1209 | { |
| 1210 | buffer->clock = clock; |
| 1211 | } |
| 1212 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1213 | static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer); |
| 1214 | |
| 1215 | static void |
| 1216 | rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages) |
| 1217 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1218 | struct buffer_page *bpage; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1219 | struct list_head *p; |
| 1220 | unsigned i; |
| 1221 | |
Lai Jiangshan | f711294 | 2009-11-03 19:42:45 +0800 | [diff] [blame] | 1222 | spin_lock_irq(&cpu_buffer->reader_lock); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1223 | rb_head_page_deactivate(cpu_buffer); |
| 1224 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1225 | for (i = 0; i < nr_pages; i++) { |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1226 | if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages))) |
Julia Lawall | 292f60c | 2010-03-29 17:37:02 +0200 | [diff] [blame] | 1227 | goto out; |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1228 | p = cpu_buffer->pages->next; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1229 | bpage = list_entry(p, struct buffer_page, list); |
| 1230 | list_del_init(&bpage->list); |
| 1231 | free_buffer_page(bpage); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1232 | } |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1233 | if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages))) |
Julia Lawall | 292f60c | 2010-03-29 17:37:02 +0200 | [diff] [blame] | 1234 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1235 | |
| 1236 | rb_reset_cpu(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1237 | rb_check_pages(cpu_buffer); |
| 1238 | |
Julia Lawall | 292f60c | 2010-03-29 17:37:02 +0200 | [diff] [blame] | 1239 | out: |
Steven Rostedt | dd7f594 | 2009-12-10 23:20:52 -0500 | [diff] [blame] | 1240 | spin_unlock_irq(&cpu_buffer->reader_lock); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | static void |
| 1244 | rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer, |
| 1245 | struct list_head *pages, unsigned nr_pages) |
| 1246 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1247 | struct buffer_page *bpage; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1248 | struct list_head *p; |
| 1249 | unsigned i; |
| 1250 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1251 | spin_lock_irq(&cpu_buffer->reader_lock); |
| 1252 | rb_head_page_deactivate(cpu_buffer); |
| 1253 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1254 | for (i = 0; i < nr_pages; i++) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1255 | if (RB_WARN_ON(cpu_buffer, list_empty(pages))) |
Julia Lawall | 292f60c | 2010-03-29 17:37:02 +0200 | [diff] [blame] | 1256 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1257 | p = pages->next; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1258 | bpage = list_entry(p, struct buffer_page, list); |
| 1259 | list_del_init(&bpage->list); |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 1260 | list_add_tail(&bpage->list, cpu_buffer->pages); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1261 | } |
| 1262 | rb_reset_cpu(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1263 | rb_check_pages(cpu_buffer); |
| 1264 | |
Julia Lawall | 292f60c | 2010-03-29 17:37:02 +0200 | [diff] [blame] | 1265 | out: |
Steven Rostedt | dd7f594 | 2009-12-10 23:20:52 -0500 | [diff] [blame] | 1266 | spin_unlock_irq(&cpu_buffer->reader_lock); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | /** |
| 1270 | * ring_buffer_resize - resize the ring buffer |
| 1271 | * @buffer: the buffer to resize. |
| 1272 | * @size: the new size. |
| 1273 | * |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1274 | * Minimum size is 2 * BUF_PAGE_SIZE. |
| 1275 | * |
| 1276 | * Returns -1 on failure. |
| 1277 | */ |
| 1278 | int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size) |
| 1279 | { |
| 1280 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1281 | unsigned nr_pages, rm_pages, new_pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1282 | struct buffer_page *bpage, *tmp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1283 | unsigned long buffer_size; |
| 1284 | unsigned long addr; |
| 1285 | LIST_HEAD(pages); |
| 1286 | int i, cpu; |
| 1287 | |
Ingo Molnar | ee51a1d | 2008-11-13 14:58:31 +0100 | [diff] [blame] | 1288 | /* |
| 1289 | * Always succeed at resizing a non-existent buffer: |
| 1290 | */ |
| 1291 | if (!buffer) |
| 1292 | return size; |
| 1293 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1294 | size = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
| 1295 | size *= BUF_PAGE_SIZE; |
| 1296 | buffer_size = buffer->pages * BUF_PAGE_SIZE; |
| 1297 | |
| 1298 | /* we need a minimum of two pages */ |
| 1299 | if (size < BUF_PAGE_SIZE * 2) |
| 1300 | size = BUF_PAGE_SIZE * 2; |
| 1301 | |
| 1302 | if (size == buffer_size) |
| 1303 | return size; |
| 1304 | |
Steven Rostedt | 1842101 | 2009-12-10 22:54:27 -0500 | [diff] [blame] | 1305 | atomic_inc(&buffer->record_disabled); |
| 1306 | |
| 1307 | /* Make sure all writers are done with this buffer. */ |
| 1308 | synchronize_sched(); |
| 1309 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1310 | mutex_lock(&buffer->mutex); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1311 | get_online_cpus(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1312 | |
| 1313 | nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
| 1314 | |
| 1315 | if (size < buffer_size) { |
| 1316 | |
| 1317 | /* easy case, just free pages */ |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1318 | if (RB_WARN_ON(buffer, nr_pages >= buffer->pages)) |
| 1319 | goto out_fail; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1320 | |
| 1321 | rm_pages = buffer->pages - nr_pages; |
| 1322 | |
| 1323 | for_each_buffer_cpu(buffer, cpu) { |
| 1324 | cpu_buffer = buffer->buffers[cpu]; |
| 1325 | rb_remove_pages(cpu_buffer, rm_pages); |
| 1326 | } |
| 1327 | goto out; |
| 1328 | } |
| 1329 | |
| 1330 | /* |
| 1331 | * This is a bit more difficult. We only want to add pages |
| 1332 | * when we can allocate enough for all CPUs. We do this |
| 1333 | * by allocating all the pages and storing them on a local |
| 1334 | * link list. If we succeed in our allocation, then we |
| 1335 | * add these pages to the cpu_buffers. Otherwise we just free |
| 1336 | * them all and return -ENOMEM; |
| 1337 | */ |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1338 | if (RB_WARN_ON(buffer, nr_pages <= buffer->pages)) |
| 1339 | goto out_fail; |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 1340 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1341 | new_pages = nr_pages - buffer->pages; |
| 1342 | |
| 1343 | for_each_buffer_cpu(buffer, cpu) { |
| 1344 | for (i = 0; i < new_pages; i++) { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1345 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1346 | cache_line_size()), |
| 1347 | GFP_KERNEL, cpu_to_node(cpu)); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1348 | if (!bpage) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 1349 | goto free_pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1350 | list_add(&bpage->list, &pages); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1351 | addr = __get_free_page(GFP_KERNEL); |
| 1352 | if (!addr) |
| 1353 | goto free_pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1354 | bpage->page = (void *)addr; |
| 1355 | rb_init_page(bpage->page); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | for_each_buffer_cpu(buffer, cpu) { |
| 1360 | cpu_buffer = buffer->buffers[cpu]; |
| 1361 | rb_insert_pages(cpu_buffer, &pages, new_pages); |
| 1362 | } |
| 1363 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1364 | if (RB_WARN_ON(buffer, !list_empty(&pages))) |
| 1365 | goto out_fail; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1366 | |
| 1367 | out: |
| 1368 | buffer->pages = nr_pages; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1369 | put_online_cpus(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1370 | mutex_unlock(&buffer->mutex); |
| 1371 | |
Steven Rostedt | 1842101 | 2009-12-10 22:54:27 -0500 | [diff] [blame] | 1372 | atomic_dec(&buffer->record_disabled); |
| 1373 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1374 | return size; |
| 1375 | |
| 1376 | free_pages: |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1377 | list_for_each_entry_safe(bpage, tmp, &pages, list) { |
| 1378 | list_del_init(&bpage->list); |
| 1379 | free_buffer_page(bpage); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1380 | } |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1381 | put_online_cpus(); |
Vegard Nossum | 641d2f6 | 2008-11-18 19:22:13 +0100 | [diff] [blame] | 1382 | mutex_unlock(&buffer->mutex); |
Steven Rostedt | 1842101 | 2009-12-10 22:54:27 -0500 | [diff] [blame] | 1383 | atomic_dec(&buffer->record_disabled); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1384 | return -ENOMEM; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1385 | |
| 1386 | /* |
| 1387 | * Something went totally wrong, and we are too paranoid |
| 1388 | * to even clean up the mess. |
| 1389 | */ |
| 1390 | out_fail: |
| 1391 | put_online_cpus(); |
| 1392 | mutex_unlock(&buffer->mutex); |
Steven Rostedt | 1842101 | 2009-12-10 22:54:27 -0500 | [diff] [blame] | 1393 | atomic_dec(&buffer->record_disabled); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1394 | return -1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1395 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1396 | EXPORT_SYMBOL_GPL(ring_buffer_resize); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1397 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 1398 | static inline void * |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1399 | __rb_data_page_index(struct buffer_data_page *bpage, unsigned index) |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 1400 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1401 | return bpage->data + index; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 1402 | } |
| 1403 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1404 | static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1405 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1406 | return bpage->page->data + index; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | static inline struct ring_buffer_event * |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1410 | rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1411 | { |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 1412 | return __rb_page_index(cpu_buffer->reader_page, |
| 1413 | cpu_buffer->reader_page->read); |
| 1414 | } |
| 1415 | |
| 1416 | static inline struct ring_buffer_event * |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1417 | rb_iter_head_event(struct ring_buffer_iter *iter) |
| 1418 | { |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 1419 | return __rb_page_index(iter->head_page, iter->head); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1420 | } |
| 1421 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1422 | static inline unsigned long rb_page_write(struct buffer_page *bpage) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1423 | { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1424 | return local_read(&bpage->write) & RB_WRITE_MASK; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | static inline unsigned rb_page_commit(struct buffer_page *bpage) |
| 1428 | { |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1429 | return local_read(&bpage->page->commit); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1430 | } |
| 1431 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1432 | static inline unsigned long rb_page_entries(struct buffer_page *bpage) |
| 1433 | { |
| 1434 | return local_read(&bpage->entries) & RB_WRITE_MASK; |
| 1435 | } |
| 1436 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1437 | /* Size is determined by what has been commited */ |
| 1438 | static inline unsigned rb_page_size(struct buffer_page *bpage) |
| 1439 | { |
| 1440 | return rb_page_commit(bpage); |
| 1441 | } |
| 1442 | |
| 1443 | static inline unsigned |
| 1444 | rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer) |
| 1445 | { |
| 1446 | return rb_page_commit(cpu_buffer->commit_page); |
| 1447 | } |
| 1448 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1449 | static inline unsigned |
| 1450 | rb_event_index(struct ring_buffer_event *event) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1451 | { |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1452 | unsigned long addr = (unsigned long)event; |
| 1453 | |
Steven Rostedt | 22f470f | 2009-06-11 09:29:58 -0400 | [diff] [blame] | 1454 | return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1455 | } |
| 1456 | |
Steven Rostedt | 0f0c85f | 2009-05-11 16:08:00 -0400 | [diff] [blame] | 1457 | static inline int |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 1458 | rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer, |
| 1459 | struct ring_buffer_event *event) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1460 | { |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1461 | unsigned long addr = (unsigned long)event; |
| 1462 | unsigned long index; |
| 1463 | |
| 1464 | index = rb_event_index(event); |
| 1465 | addr &= PAGE_MASK; |
| 1466 | |
| 1467 | return cpu_buffer->commit_page->page == (void *)addr && |
| 1468 | rb_commit_index(cpu_buffer) == index; |
| 1469 | } |
| 1470 | |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1471 | static void |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1472 | rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer) |
| 1473 | { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1474 | unsigned long max_count; |
| 1475 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1476 | /* |
| 1477 | * We only race with interrupts and NMIs on this CPU. |
| 1478 | * If we own the commit event, then we can commit |
| 1479 | * all others that interrupted us, since the interruptions |
| 1480 | * are in stack format (they finish before they come |
| 1481 | * back to us). This allows us to do a simple loop to |
| 1482 | * assign the commit to the tail. |
| 1483 | */ |
Steven Rostedt | a8ccf1d | 2008-12-23 11:32:24 -0500 | [diff] [blame] | 1484 | again: |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1485 | max_count = cpu_buffer->buffer->pages * 100; |
| 1486 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1487 | while (cpu_buffer->commit_page != cpu_buffer->tail_page) { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1488 | if (RB_WARN_ON(cpu_buffer, !(--max_count))) |
| 1489 | return; |
| 1490 | if (RB_WARN_ON(cpu_buffer, |
| 1491 | rb_is_reader_page(cpu_buffer->tail_page))) |
| 1492 | return; |
| 1493 | local_set(&cpu_buffer->commit_page->page->commit, |
| 1494 | rb_page_write(cpu_buffer->commit_page)); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1495 | rb_inc_page(cpu_buffer, &cpu_buffer->commit_page); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1496 | cpu_buffer->write_stamp = |
| 1497 | cpu_buffer->commit_page->page->time_stamp; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1498 | /* add barrier to keep gcc from optimizing too much */ |
| 1499 | barrier(); |
| 1500 | } |
| 1501 | while (rb_commit_index(cpu_buffer) != |
| 1502 | rb_page_write(cpu_buffer->commit_page)) { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1503 | |
| 1504 | local_set(&cpu_buffer->commit_page->page->commit, |
| 1505 | rb_page_write(cpu_buffer->commit_page)); |
| 1506 | RB_WARN_ON(cpu_buffer, |
| 1507 | local_read(&cpu_buffer->commit_page->page->commit) & |
| 1508 | ~RB_WRITE_MASK); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1509 | barrier(); |
| 1510 | } |
Steven Rostedt | a8ccf1d | 2008-12-23 11:32:24 -0500 | [diff] [blame] | 1511 | |
| 1512 | /* again, keep gcc from optimizing */ |
| 1513 | barrier(); |
| 1514 | |
| 1515 | /* |
| 1516 | * If an interrupt came in just after the first while loop |
| 1517 | * and pushed the tail page forward, we will be left with |
| 1518 | * a dangling commit that will never go forward. |
| 1519 | */ |
| 1520 | if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page)) |
| 1521 | goto again; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1522 | } |
| 1523 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1524 | 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] | 1525 | { |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1526 | cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp; |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 1527 | cpu_buffer->reader_page->read = 0; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1528 | } |
| 1529 | |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1530 | static void rb_inc_iter(struct ring_buffer_iter *iter) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1531 | { |
| 1532 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 1533 | |
| 1534 | /* |
| 1535 | * The iterator could be on the reader page (it starts there). |
| 1536 | * But the head could have moved, since the reader was |
| 1537 | * found. Check for this case and assign the iterator |
| 1538 | * to the head page instead of next. |
| 1539 | */ |
| 1540 | if (iter->head_page == cpu_buffer->reader_page) |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1541 | iter->head_page = rb_set_head_page(cpu_buffer); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1542 | else |
| 1543 | rb_inc_page(cpu_buffer, &iter->head_page); |
| 1544 | |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1545 | iter->read_stamp = iter->head_page->page->time_stamp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1546 | iter->head = 0; |
| 1547 | } |
| 1548 | |
| 1549 | /** |
| 1550 | * ring_buffer_update_event - update event type and data |
| 1551 | * @event: the even to update |
| 1552 | * @type: the type of event |
| 1553 | * @length: the size of the event field in the ring buffer |
| 1554 | * |
| 1555 | * Update the type and data fields of the event. The length |
| 1556 | * is the actual size that is written to the ring buffer, |
| 1557 | * and with this, we can determine what to place into the |
| 1558 | * data field. |
| 1559 | */ |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1560 | static void |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1561 | rb_update_event(struct ring_buffer_event *event, |
| 1562 | unsigned type, unsigned length) |
| 1563 | { |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 1564 | event->type_len = type; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1565 | |
| 1566 | switch (type) { |
| 1567 | |
| 1568 | case RINGBUF_TYPE_PADDING: |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1569 | case RINGBUF_TYPE_TIME_EXTEND: |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1570 | case RINGBUF_TYPE_TIME_STAMP: |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1571 | break; |
| 1572 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 1573 | case 0: |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1574 | length -= RB_EVNT_HDR_SIZE; |
Steven Rostedt | 2271048 | 2010-03-18 17:54:19 -0400 | [diff] [blame] | 1575 | if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1576 | event->array[0] = length; |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 1577 | else |
| 1578 | event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1579 | break; |
| 1580 | default: |
| 1581 | BUG(); |
| 1582 | } |
| 1583 | } |
| 1584 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1585 | /* |
| 1586 | * rb_handle_head_page - writer hit the head page |
| 1587 | * |
| 1588 | * Returns: +1 to retry page |
| 1589 | * 0 to continue |
| 1590 | * -1 on error |
| 1591 | */ |
| 1592 | static int |
| 1593 | rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer, |
| 1594 | struct buffer_page *tail_page, |
| 1595 | struct buffer_page *next_page) |
| 1596 | { |
| 1597 | struct buffer_page *new_head; |
| 1598 | int entries; |
| 1599 | int type; |
| 1600 | int ret; |
| 1601 | |
| 1602 | entries = rb_page_entries(next_page); |
| 1603 | |
| 1604 | /* |
| 1605 | * The hard part is here. We need to move the head |
| 1606 | * forward, and protect against both readers on |
| 1607 | * other CPUs and writers coming in via interrupts. |
| 1608 | */ |
| 1609 | type = rb_head_page_set_update(cpu_buffer, next_page, tail_page, |
| 1610 | RB_PAGE_HEAD); |
| 1611 | |
| 1612 | /* |
| 1613 | * type can be one of four: |
| 1614 | * NORMAL - an interrupt already moved it for us |
| 1615 | * HEAD - we are the first to get here. |
| 1616 | * UPDATE - we are the interrupt interrupting |
| 1617 | * a current move. |
| 1618 | * MOVED - a reader on another CPU moved the next |
| 1619 | * pointer to its reader page. Give up |
| 1620 | * and try again. |
| 1621 | */ |
| 1622 | |
| 1623 | switch (type) { |
| 1624 | case RB_PAGE_HEAD: |
| 1625 | /* |
| 1626 | * We changed the head to UPDATE, thus |
| 1627 | * it is our responsibility to update |
| 1628 | * the counters. |
| 1629 | */ |
| 1630 | local_add(entries, &cpu_buffer->overrun); |
| 1631 | |
| 1632 | /* |
| 1633 | * The entries will be zeroed out when we move the |
| 1634 | * tail page. |
| 1635 | */ |
| 1636 | |
| 1637 | /* still more to do */ |
| 1638 | break; |
| 1639 | |
| 1640 | case RB_PAGE_UPDATE: |
| 1641 | /* |
| 1642 | * This is an interrupt that interrupt the |
| 1643 | * previous update. Still more to do. |
| 1644 | */ |
| 1645 | break; |
| 1646 | case RB_PAGE_NORMAL: |
| 1647 | /* |
| 1648 | * An interrupt came in before the update |
| 1649 | * and processed this for us. |
| 1650 | * Nothing left to do. |
| 1651 | */ |
| 1652 | return 1; |
| 1653 | case RB_PAGE_MOVED: |
| 1654 | /* |
| 1655 | * The reader is on another CPU and just did |
| 1656 | * a swap with our next_page. |
| 1657 | * Try again. |
| 1658 | */ |
| 1659 | return 1; |
| 1660 | default: |
| 1661 | RB_WARN_ON(cpu_buffer, 1); /* WTF??? */ |
| 1662 | return -1; |
| 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | * Now that we are here, the old head pointer is |
| 1667 | * set to UPDATE. This will keep the reader from |
| 1668 | * swapping the head page with the reader page. |
| 1669 | * The reader (on another CPU) will spin till |
| 1670 | * we are finished. |
| 1671 | * |
| 1672 | * We just need to protect against interrupts |
| 1673 | * doing the job. We will set the next pointer |
| 1674 | * to HEAD. After that, we set the old pointer |
| 1675 | * to NORMAL, but only if it was HEAD before. |
| 1676 | * otherwise we are an interrupt, and only |
| 1677 | * want the outer most commit to reset it. |
| 1678 | */ |
| 1679 | new_head = next_page; |
| 1680 | rb_inc_page(cpu_buffer, &new_head); |
| 1681 | |
| 1682 | ret = rb_head_page_set_head(cpu_buffer, new_head, next_page, |
| 1683 | RB_PAGE_NORMAL); |
| 1684 | |
| 1685 | /* |
| 1686 | * Valid returns are: |
| 1687 | * HEAD - an interrupt came in and already set it. |
| 1688 | * NORMAL - One of two things: |
| 1689 | * 1) We really set it. |
| 1690 | * 2) A bunch of interrupts came in and moved |
| 1691 | * the page forward again. |
| 1692 | */ |
| 1693 | switch (ret) { |
| 1694 | case RB_PAGE_HEAD: |
| 1695 | case RB_PAGE_NORMAL: |
| 1696 | /* OK */ |
| 1697 | break; |
| 1698 | default: |
| 1699 | RB_WARN_ON(cpu_buffer, 1); |
| 1700 | return -1; |
| 1701 | } |
| 1702 | |
| 1703 | /* |
| 1704 | * It is possible that an interrupt came in, |
| 1705 | * set the head up, then more interrupts came in |
| 1706 | * and moved it again. When we get back here, |
| 1707 | * the page would have been set to NORMAL but we |
| 1708 | * just set it back to HEAD. |
| 1709 | * |
| 1710 | * How do you detect this? Well, if that happened |
| 1711 | * the tail page would have moved. |
| 1712 | */ |
| 1713 | if (ret == RB_PAGE_NORMAL) { |
| 1714 | /* |
| 1715 | * If the tail had moved passed next, then we need |
| 1716 | * to reset the pointer. |
| 1717 | */ |
| 1718 | if (cpu_buffer->tail_page != tail_page && |
| 1719 | cpu_buffer->tail_page != next_page) |
| 1720 | rb_head_page_set_normal(cpu_buffer, new_head, |
| 1721 | next_page, |
| 1722 | RB_PAGE_HEAD); |
| 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | * If this was the outer most commit (the one that |
| 1727 | * changed the original pointer from HEAD to UPDATE), |
| 1728 | * then it is up to us to reset it to NORMAL. |
| 1729 | */ |
| 1730 | if (type == RB_PAGE_HEAD) { |
| 1731 | ret = rb_head_page_set_normal(cpu_buffer, next_page, |
| 1732 | tail_page, |
| 1733 | RB_PAGE_UPDATE); |
| 1734 | if (RB_WARN_ON(cpu_buffer, |
| 1735 | ret != RB_PAGE_UPDATE)) |
| 1736 | return -1; |
| 1737 | } |
| 1738 | |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1742 | static unsigned rb_calculate_event_length(unsigned length) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1743 | { |
| 1744 | struct ring_buffer_event event; /* Used only for sizeof array */ |
| 1745 | |
| 1746 | /* zero length can cause confusions */ |
| 1747 | if (!length) |
| 1748 | length = 1; |
| 1749 | |
Steven Rostedt | 2271048 | 2010-03-18 17:54:19 -0400 | [diff] [blame] | 1750 | if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1751 | length += sizeof(event.array[0]); |
| 1752 | |
| 1753 | length += RB_EVNT_HDR_SIZE; |
Steven Rostedt | 2271048 | 2010-03-18 17:54:19 -0400 | [diff] [blame] | 1754 | length = ALIGN(length, RB_ARCH_ALIGNMENT); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1755 | |
| 1756 | return length; |
| 1757 | } |
| 1758 | |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 1759 | static inline void |
| 1760 | rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer, |
| 1761 | struct buffer_page *tail_page, |
| 1762 | unsigned long tail, unsigned long length) |
| 1763 | { |
| 1764 | struct ring_buffer_event *event; |
| 1765 | |
| 1766 | /* |
| 1767 | * Only the event that crossed the page boundary |
| 1768 | * must fill the old tail_page with padding. |
| 1769 | */ |
| 1770 | if (tail >= BUF_PAGE_SIZE) { |
| 1771 | local_sub(length, &tail_page->write); |
| 1772 | return; |
| 1773 | } |
| 1774 | |
| 1775 | event = __rb_page_index(tail_page, tail); |
Linus Torvalds | b0b7065 | 2009-06-20 10:56:46 -0700 | [diff] [blame] | 1776 | kmemcheck_annotate_bitfield(event, bitfield); |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 1777 | |
| 1778 | /* |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 1779 | * Save the original length to the meta data. |
| 1780 | * This will be used by the reader to add lost event |
| 1781 | * counter. |
| 1782 | */ |
| 1783 | tail_page->real_end = tail; |
| 1784 | |
| 1785 | /* |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 1786 | * If this event is bigger than the minimum size, then |
| 1787 | * we need to be careful that we don't subtract the |
| 1788 | * write counter enough to allow another writer to slip |
| 1789 | * in on this page. |
| 1790 | * We put in a discarded commit instead, to make sure |
| 1791 | * that this space is not used again. |
| 1792 | * |
| 1793 | * If we are less than the minimum size, we don't need to |
| 1794 | * worry about it. |
| 1795 | */ |
| 1796 | if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) { |
| 1797 | /* No room for any events */ |
| 1798 | |
| 1799 | /* Mark the rest of the page with padding */ |
| 1800 | rb_event_set_padding(event); |
| 1801 | |
| 1802 | /* Set the write back to the previous setting */ |
| 1803 | local_sub(length, &tail_page->write); |
| 1804 | return; |
| 1805 | } |
| 1806 | |
| 1807 | /* Put in a discarded event */ |
| 1808 | event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE; |
| 1809 | event->type_len = RINGBUF_TYPE_PADDING; |
| 1810 | /* time delta must be non zero */ |
| 1811 | event->time_delta = 1; |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 1812 | |
| 1813 | /* Set write to end of buffer */ |
| 1814 | length = (tail + length) - BUF_PAGE_SIZE; |
| 1815 | local_sub(length, &tail_page->write); |
| 1816 | } |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 1817 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1818 | static struct ring_buffer_event * |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 1819 | rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer, |
| 1820 | unsigned long length, unsigned long tail, |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 1821 | struct buffer_page *tail_page, u64 *ts) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1822 | { |
Steven Rostedt | 5a50e33 | 2009-11-17 08:43:01 -0500 | [diff] [blame] | 1823 | struct buffer_page *commit_page = cpu_buffer->commit_page; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1824 | struct ring_buffer *buffer = cpu_buffer->buffer; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1825 | struct buffer_page *next_page; |
| 1826 | int ret; |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 1827 | |
| 1828 | next_page = tail_page; |
| 1829 | |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 1830 | rb_inc_page(cpu_buffer, &next_page); |
| 1831 | |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 1832 | /* |
| 1833 | * If for some reason, we had an interrupt storm that made |
| 1834 | * it all the way around the buffer, bail, and warn |
| 1835 | * about it. |
| 1836 | */ |
| 1837 | if (unlikely(next_page == commit_page)) { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1838 | local_inc(&cpu_buffer->commit_overrun); |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 1839 | goto out_reset; |
| 1840 | } |
| 1841 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1842 | /* |
| 1843 | * This is where the fun begins! |
| 1844 | * |
| 1845 | * We are fighting against races between a reader that |
| 1846 | * could be on another CPU trying to swap its reader |
| 1847 | * page with the buffer head. |
| 1848 | * |
| 1849 | * We are also fighting against interrupts coming in and |
| 1850 | * moving the head or tail on us as well. |
| 1851 | * |
| 1852 | * If the next page is the head page then we have filled |
| 1853 | * the buffer, unless the commit page is still on the |
| 1854 | * reader page. |
| 1855 | */ |
| 1856 | if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) { |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 1857 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1858 | /* |
| 1859 | * If the commit is not on the reader page, then |
| 1860 | * move the header page. |
| 1861 | */ |
| 1862 | if (!rb_is_reader_page(cpu_buffer->commit_page)) { |
| 1863 | /* |
| 1864 | * If we are not in overwrite mode, |
| 1865 | * this is easy, just stop here. |
| 1866 | */ |
| 1867 | if (!(buffer->flags & RB_FL_OVERWRITE)) |
| 1868 | goto out_reset; |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 1869 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1870 | ret = rb_handle_head_page(cpu_buffer, |
| 1871 | tail_page, |
| 1872 | next_page); |
| 1873 | if (ret < 0) |
| 1874 | goto out_reset; |
| 1875 | if (ret) |
| 1876 | goto out_again; |
| 1877 | } else { |
| 1878 | /* |
| 1879 | * We need to be careful here too. The |
| 1880 | * commit page could still be on the reader |
| 1881 | * page. We could have a small buffer, and |
| 1882 | * have filled up the buffer with events |
| 1883 | * from interrupts and such, and wrapped. |
| 1884 | * |
| 1885 | * Note, if the tail page is also the on the |
| 1886 | * reader_page, we let it move out. |
| 1887 | */ |
| 1888 | if (unlikely((cpu_buffer->commit_page != |
| 1889 | cpu_buffer->tail_page) && |
| 1890 | (cpu_buffer->commit_page == |
| 1891 | cpu_buffer->reader_page))) { |
| 1892 | local_inc(&cpu_buffer->commit_overrun); |
| 1893 | goto out_reset; |
| 1894 | } |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1898 | ret = rb_tail_page_update(cpu_buffer, tail_page, next_page); |
| 1899 | if (ret) { |
| 1900 | /* |
| 1901 | * Nested commits always have zero deltas, so |
| 1902 | * just reread the time stamp |
| 1903 | */ |
Jiri Olsa | 6d3f1e1 | 2009-10-23 19:36:19 -0400 | [diff] [blame] | 1904 | *ts = rb_time_stamp(buffer); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1905 | next_page->page->time_stamp = *ts; |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 1906 | } |
| 1907 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1908 | out_again: |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 1909 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1910 | rb_reset_tail(cpu_buffer, tail_page, tail, length); |
Steven Rostedt | aa20ae8 | 2009-05-05 21:16:11 -0400 | [diff] [blame] | 1911 | |
| 1912 | /* fail and let the caller try again */ |
| 1913 | return ERR_PTR(-EAGAIN); |
| 1914 | |
Steven Rostedt | 45141d4 | 2009-02-12 13:19:48 -0500 | [diff] [blame] | 1915 | out_reset: |
Lai Jiangshan | 6f3b344 | 2009-01-12 11:06:18 +0800 | [diff] [blame] | 1916 | /* reset write */ |
Steven Rostedt | c7b0930 | 2009-06-11 11:12:00 -0400 | [diff] [blame] | 1917 | rb_reset_tail(cpu_buffer, tail_page, tail, length); |
Lai Jiangshan | 6f3b344 | 2009-01-12 11:06:18 +0800 | [diff] [blame] | 1918 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1919 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1920 | } |
| 1921 | |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 1922 | static struct ring_buffer_event * |
| 1923 | __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, |
| 1924 | unsigned type, unsigned long length, u64 *ts) |
| 1925 | { |
Steven Rostedt | 5a50e33 | 2009-11-17 08:43:01 -0500 | [diff] [blame] | 1926 | struct buffer_page *tail_page; |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 1927 | struct ring_buffer_event *event; |
| 1928 | unsigned long tail, write; |
| 1929 | |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 1930 | tail_page = cpu_buffer->tail_page; |
| 1931 | write = local_add_return(length, &tail_page->write); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1932 | |
| 1933 | /* set write to only the index of the write */ |
| 1934 | write &= RB_WRITE_MASK; |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 1935 | tail = write - length; |
| 1936 | |
| 1937 | /* See if we shot pass the end of this buffer page */ |
| 1938 | if (write > BUF_PAGE_SIZE) |
| 1939 | return rb_move_tail(cpu_buffer, length, tail, |
Steven Rostedt | 5a50e33 | 2009-11-17 08:43:01 -0500 | [diff] [blame] | 1940 | tail_page, ts); |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 1941 | |
| 1942 | /* We reserved something on the buffer */ |
| 1943 | |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 1944 | event = __rb_page_index(tail_page, tail); |
Vegard Nossum | 1744a21 | 2009-02-28 08:29:44 +0100 | [diff] [blame] | 1945 | kmemcheck_annotate_bitfield(event, bitfield); |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 1946 | rb_update_event(event, type, length); |
| 1947 | |
| 1948 | /* The passed in type is zero for DATA */ |
| 1949 | if (likely(!type)) |
| 1950 | local_inc(&tail_page->entries); |
| 1951 | |
| 1952 | /* |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 1953 | * If this is the first commit on the page, then update |
| 1954 | * its timestamp. |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 1955 | */ |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 1956 | if (!tail) |
| 1957 | tail_page->page->time_stamp = *ts; |
Steven Rostedt | 6634ff2 | 2009-05-06 15:30:07 -0400 | [diff] [blame] | 1958 | |
| 1959 | return event; |
| 1960 | } |
| 1961 | |
Steven Rostedt | edd813bf | 2009-06-02 23:00:53 -0400 | [diff] [blame] | 1962 | static inline int |
| 1963 | rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer, |
| 1964 | struct ring_buffer_event *event) |
| 1965 | { |
| 1966 | unsigned long new_index, old_index; |
| 1967 | struct buffer_page *bpage; |
| 1968 | unsigned long index; |
| 1969 | unsigned long addr; |
| 1970 | |
| 1971 | new_index = rb_event_index(event); |
| 1972 | old_index = new_index + rb_event_length(event); |
| 1973 | addr = (unsigned long)event; |
| 1974 | addr &= PAGE_MASK; |
| 1975 | |
| 1976 | bpage = cpu_buffer->tail_page; |
| 1977 | |
| 1978 | if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1979 | unsigned long write_mask = |
| 1980 | local_read(&bpage->write) & ~RB_WRITE_MASK; |
Steven Rostedt | edd813bf | 2009-06-02 23:00:53 -0400 | [diff] [blame] | 1981 | /* |
| 1982 | * This is on the tail page. It is possible that |
| 1983 | * a write could come in and move the tail page |
| 1984 | * and write to the next page. That is fine |
| 1985 | * because we just shorten what is on this page. |
| 1986 | */ |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 1987 | old_index += write_mask; |
| 1988 | new_index += write_mask; |
Steven Rostedt | edd813bf | 2009-06-02 23:00:53 -0400 | [diff] [blame] | 1989 | index = local_cmpxchg(&bpage->write, old_index, new_index); |
| 1990 | if (index == old_index) |
| 1991 | return 1; |
| 1992 | } |
| 1993 | |
| 1994 | /* could not discard */ |
| 1995 | return 0; |
| 1996 | } |
| 1997 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1998 | static int |
| 1999 | rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer, |
| 2000 | u64 *ts, u64 *delta) |
| 2001 | { |
| 2002 | struct ring_buffer_event *event; |
| 2003 | static int once; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2004 | int ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2005 | |
| 2006 | if (unlikely(*delta > (1ULL << 59) && !once++)) { |
| 2007 | printk(KERN_WARNING "Delta way too big! %llu" |
| 2008 | " ts=%llu write stamp = %llu\n", |
Stephen Rothwell | e2862c9 | 2008-10-27 17:43:28 +1100 | [diff] [blame] | 2009 | (unsigned long long)*delta, |
| 2010 | (unsigned long long)*ts, |
| 2011 | (unsigned long long)cpu_buffer->write_stamp); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2012 | WARN_ON(1); |
| 2013 | } |
| 2014 | |
| 2015 | /* |
| 2016 | * The delta is too big, we to add a |
| 2017 | * new timestamp. |
| 2018 | */ |
| 2019 | event = __rb_reserve_next(cpu_buffer, |
| 2020 | RINGBUF_TYPE_TIME_EXTEND, |
| 2021 | RB_LEN_TIME_EXTEND, |
| 2022 | ts); |
| 2023 | if (!event) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2024 | return -EBUSY; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2025 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2026 | if (PTR_ERR(event) == -EAGAIN) |
| 2027 | return -EAGAIN; |
| 2028 | |
| 2029 | /* Only a commited time event can update the write stamp */ |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2030 | if (rb_event_is_commit(cpu_buffer, event)) { |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2031 | /* |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2032 | * If this is the first on the page, then it was |
| 2033 | * updated with the page itself. Try to discard it |
| 2034 | * and if we can't just make it zero. |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2035 | */ |
| 2036 | if (rb_event_index(event)) { |
| 2037 | event->time_delta = *delta & TS_MASK; |
| 2038 | event->array[0] = *delta >> TS_SHIFT; |
| 2039 | } else { |
Steven Rostedt | ea05b57 | 2009-06-03 09:30:10 -0400 | [diff] [blame] | 2040 | /* try to discard, since we do not need this */ |
| 2041 | if (!rb_try_to_discard(cpu_buffer, event)) { |
| 2042 | /* nope, just zero it */ |
| 2043 | event->time_delta = 0; |
| 2044 | event->array[0] = 0; |
| 2045 | } |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2046 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2047 | cpu_buffer->write_stamp = *ts; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2048 | /* let the caller know this was the commit */ |
| 2049 | ret = 1; |
| 2050 | } else { |
Steven Rostedt | edd813bf | 2009-06-02 23:00:53 -0400 | [diff] [blame] | 2051 | /* Try to discard the event */ |
| 2052 | if (!rb_try_to_discard(cpu_buffer, event)) { |
| 2053 | /* Darn, this is just wasted space */ |
| 2054 | event->time_delta = 0; |
| 2055 | event->array[0] = 0; |
Steven Rostedt | edd813bf | 2009-06-02 23:00:53 -0400 | [diff] [blame] | 2056 | } |
Steven Rostedt | f57a8a1 | 2009-06-05 14:11:30 -0400 | [diff] [blame] | 2057 | ret = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2058 | } |
| 2059 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2060 | *delta = 0; |
| 2061 | |
| 2062 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2063 | } |
| 2064 | |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2065 | static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer) |
| 2066 | { |
| 2067 | local_inc(&cpu_buffer->committing); |
| 2068 | local_inc(&cpu_buffer->commits); |
| 2069 | } |
| 2070 | |
| 2071 | static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer) |
| 2072 | { |
| 2073 | unsigned long commits; |
| 2074 | |
| 2075 | if (RB_WARN_ON(cpu_buffer, |
| 2076 | !local_read(&cpu_buffer->committing))) |
| 2077 | return; |
| 2078 | |
| 2079 | again: |
| 2080 | commits = local_read(&cpu_buffer->commits); |
| 2081 | /* synchronize with interrupts */ |
| 2082 | barrier(); |
| 2083 | if (local_read(&cpu_buffer->committing) == 1) |
| 2084 | rb_set_commit_to_write(cpu_buffer); |
| 2085 | |
| 2086 | local_dec(&cpu_buffer->committing); |
| 2087 | |
| 2088 | /* synchronize with interrupts */ |
| 2089 | barrier(); |
| 2090 | |
| 2091 | /* |
| 2092 | * Need to account for interrupts coming in between the |
| 2093 | * updating of the commit page and the clearing of the |
| 2094 | * committing counter. |
| 2095 | */ |
| 2096 | if (unlikely(local_read(&cpu_buffer->commits) != commits) && |
| 2097 | !local_read(&cpu_buffer->committing)) { |
| 2098 | local_inc(&cpu_buffer->committing); |
| 2099 | goto again; |
| 2100 | } |
| 2101 | } |
| 2102 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2103 | static struct ring_buffer_event * |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 2104 | rb_reserve_next_event(struct ring_buffer *buffer, |
| 2105 | struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt | 1cd8d73 | 2009-05-11 14:08:09 -0400 | [diff] [blame] | 2106 | unsigned long length) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2107 | { |
| 2108 | struct ring_buffer_event *event; |
Steven Rostedt | 168b6b1 | 2009-05-11 22:11:05 -0400 | [diff] [blame] | 2109 | u64 ts, delta = 0; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2110 | int commit = 0; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2111 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2112 | |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2113 | rb_start_commit(cpu_buffer); |
| 2114 | |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 2115 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 2116 | /* |
| 2117 | * Due to the ability to swap a cpu buffer from a buffer |
| 2118 | * it is possible it was swapped before we committed. |
| 2119 | * (committing stops a swap). We check for it here and |
| 2120 | * if it happened, we have to fail the write. |
| 2121 | */ |
| 2122 | barrier(); |
| 2123 | if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) { |
| 2124 | local_dec(&cpu_buffer->committing); |
| 2125 | local_dec(&cpu_buffer->commits); |
| 2126 | return NULL; |
| 2127 | } |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 2128 | #endif |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 2129 | |
Steven Rostedt | be957c4 | 2009-05-11 14:42:53 -0400 | [diff] [blame] | 2130 | length = rb_calculate_event_length(length); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2131 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2132 | /* |
| 2133 | * We allow for interrupts to reenter here and do a trace. |
| 2134 | * If one does, it will cause this original code to loop |
| 2135 | * back here. Even with heavy interrupts happening, this |
| 2136 | * should only happen a few times in a row. If this happens |
| 2137 | * 1000 times in a row, there must be either an interrupt |
| 2138 | * storm or we have something buggy. |
| 2139 | * Bail! |
| 2140 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 2141 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000)) |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2142 | goto out_fail; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2143 | |
Jiri Olsa | 6d3f1e1 | 2009-10-23 19:36:19 -0400 | [diff] [blame] | 2144 | ts = rb_time_stamp(cpu_buffer->buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2145 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2146 | /* |
| 2147 | * Only the first commit can update the timestamp. |
| 2148 | * Yes there is a race here. If an interrupt comes in |
| 2149 | * just after the conditional and it traces too, then it |
| 2150 | * will also check the deltas. More than one timestamp may |
| 2151 | * also be made. But only the entry that did the actual |
| 2152 | * commit will be something other than zero. |
| 2153 | */ |
Steven Rostedt | 0f0c85f | 2009-05-11 16:08:00 -0400 | [diff] [blame] | 2154 | if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page && |
| 2155 | rb_page_write(cpu_buffer->tail_page) == |
| 2156 | rb_commit_index(cpu_buffer))) { |
Steven Rostedt | 168b6b1 | 2009-05-11 22:11:05 -0400 | [diff] [blame] | 2157 | u64 diff; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2158 | |
Steven Rostedt | 168b6b1 | 2009-05-11 22:11:05 -0400 | [diff] [blame] | 2159 | diff = ts - cpu_buffer->write_stamp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2160 | |
Steven Rostedt | 168b6b1 | 2009-05-11 22:11:05 -0400 | [diff] [blame] | 2161 | /* make sure this diff is calculated here */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2162 | barrier(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2163 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2164 | /* Did the write stamp get updated already? */ |
| 2165 | if (unlikely(ts < cpu_buffer->write_stamp)) |
Steven Rostedt | 168b6b1 | 2009-05-11 22:11:05 -0400 | [diff] [blame] | 2166 | goto get_event; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2167 | |
Steven Rostedt | 168b6b1 | 2009-05-11 22:11:05 -0400 | [diff] [blame] | 2168 | delta = diff; |
| 2169 | if (unlikely(test_time_stamp(delta))) { |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2170 | |
| 2171 | commit = rb_add_time_stamp(cpu_buffer, &ts, &delta); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2172 | if (commit == -EBUSY) |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2173 | goto out_fail; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2174 | |
| 2175 | if (commit == -EAGAIN) |
| 2176 | goto again; |
| 2177 | |
| 2178 | RB_WARN_ON(cpu_buffer, commit < 0); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2179 | } |
Steven Rostedt | 168b6b1 | 2009-05-11 22:11:05 -0400 | [diff] [blame] | 2180 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2181 | |
Steven Rostedt | 168b6b1 | 2009-05-11 22:11:05 -0400 | [diff] [blame] | 2182 | get_event: |
Steven Rostedt | 1cd8d73 | 2009-05-11 14:08:09 -0400 | [diff] [blame] | 2183 | event = __rb_reserve_next(cpu_buffer, 0, length, &ts); |
Steven Rostedt | 168b6b1 | 2009-05-11 22:11:05 -0400 | [diff] [blame] | 2184 | if (unlikely(PTR_ERR(event) == -EAGAIN)) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2185 | goto again; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2186 | |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2187 | if (!event) |
| 2188 | goto out_fail; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2189 | |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2190 | if (!rb_event_is_commit(cpu_buffer, event)) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2191 | delta = 0; |
| 2192 | |
| 2193 | event->time_delta = delta; |
| 2194 | |
| 2195 | return event; |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2196 | |
| 2197 | out_fail: |
| 2198 | rb_end_commit(cpu_buffer); |
| 2199 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2200 | } |
| 2201 | |
Paul Mundt | 1155de4 | 2009-06-25 14:30:12 +0900 | [diff] [blame] | 2202 | #ifdef CONFIG_TRACING |
| 2203 | |
Steven Rostedt | aa18efb | 2009-04-20 16:16:11 -0400 | [diff] [blame] | 2204 | #define TRACE_RECURSIVE_DEPTH 16 |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame] | 2205 | |
| 2206 | static int trace_recursive_lock(void) |
| 2207 | { |
Steven Rostedt | aa18efb | 2009-04-20 16:16:11 -0400 | [diff] [blame] | 2208 | current->trace_recursion++; |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame] | 2209 | |
Steven Rostedt | aa18efb | 2009-04-20 16:16:11 -0400 | [diff] [blame] | 2210 | if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH)) |
| 2211 | return 0; |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame] | 2212 | |
Steven Rostedt | aa18efb | 2009-04-20 16:16:11 -0400 | [diff] [blame] | 2213 | /* Disable all tracing before we do anything else */ |
| 2214 | tracing_off_permanent(); |
Frederic Weisbecker | e057a5e | 2009-04-19 23:38:12 +0200 | [diff] [blame] | 2215 | |
Steven Rostedt | 7d7d2b8 | 2009-04-27 12:37:49 -0400 | [diff] [blame] | 2216 | printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:" |
Steven Rostedt | aa18efb | 2009-04-20 16:16:11 -0400 | [diff] [blame] | 2217 | "HC[%lu]:SC[%lu]:NMI[%lu]\n", |
| 2218 | current->trace_recursion, |
| 2219 | hardirq_count() >> HARDIRQ_SHIFT, |
| 2220 | softirq_count() >> SOFTIRQ_SHIFT, |
| 2221 | in_nmi()); |
Frederic Weisbecker | e057a5e | 2009-04-19 23:38:12 +0200 | [diff] [blame] | 2222 | |
Steven Rostedt | aa18efb | 2009-04-20 16:16:11 -0400 | [diff] [blame] | 2223 | WARN_ON_ONCE(1); |
| 2224 | return -1; |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | static void trace_recursive_unlock(void) |
| 2228 | { |
Steven Rostedt | aa18efb | 2009-04-20 16:16:11 -0400 | [diff] [blame] | 2229 | WARN_ON_ONCE(!current->trace_recursion); |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame] | 2230 | |
Steven Rostedt | aa18efb | 2009-04-20 16:16:11 -0400 | [diff] [blame] | 2231 | current->trace_recursion--; |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame] | 2232 | } |
| 2233 | |
Paul Mundt | 1155de4 | 2009-06-25 14:30:12 +0900 | [diff] [blame] | 2234 | #else |
| 2235 | |
| 2236 | #define trace_recursive_lock() (0) |
| 2237 | #define trace_recursive_unlock() do { } while (0) |
| 2238 | |
| 2239 | #endif |
| 2240 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2241 | static DEFINE_PER_CPU(int, rb_need_resched); |
| 2242 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2243 | /** |
| 2244 | * ring_buffer_lock_reserve - reserve a part of the buffer |
| 2245 | * @buffer: the ring buffer to reserve from |
| 2246 | * @length: the length of the data to reserve (excluding event header) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2247 | * |
| 2248 | * Returns a reseverd event on the ring buffer to copy directly to. |
| 2249 | * The user of this interface will need to get the body to write into |
| 2250 | * and can use the ring_buffer_event_data() interface. |
| 2251 | * |
| 2252 | * The length is the length of the data needed, not the event length |
| 2253 | * which also includes the event header. |
| 2254 | * |
| 2255 | * Must be paired with ring_buffer_unlock_commit, unless NULL is returned. |
| 2256 | * If NULL is returned, then nothing has been allocated or locked. |
| 2257 | */ |
| 2258 | struct ring_buffer_event * |
Arnaldo Carvalho de Melo | 0a98775 | 2009-02-05 16:12:56 -0200 | [diff] [blame] | 2259 | ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2260 | { |
| 2261 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2262 | struct ring_buffer_event *event; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2263 | int cpu, resched; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2264 | |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 2265 | if (ring_buffer_flags != RB_BUFFERS_ON) |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 2266 | return NULL; |
| 2267 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2268 | /* If we are tracing schedule, we don't want to recurse */ |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 2269 | resched = ftrace_preempt_disable(); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2270 | |
Lai Jiangshan | 52fbe9c | 2010-03-08 14:50:43 +0800 | [diff] [blame] | 2271 | if (atomic_read(&buffer->record_disabled)) |
| 2272 | goto out_nocheck; |
| 2273 | |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame] | 2274 | if (trace_recursive_lock()) |
| 2275 | goto out_nocheck; |
| 2276 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2277 | cpu = raw_smp_processor_id(); |
| 2278 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 2279 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2280 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2281 | |
| 2282 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2283 | |
| 2284 | if (atomic_read(&cpu_buffer->record_disabled)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2285 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2286 | |
Steven Rostedt | be957c4 | 2009-05-11 14:42:53 -0400 | [diff] [blame] | 2287 | if (length > BUF_MAX_DATA_SIZE) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2288 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2289 | |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 2290 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2291 | if (!event) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2292 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2293 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2294 | /* |
| 2295 | * Need to store resched state on this cpu. |
| 2296 | * Only the first needs to. |
| 2297 | */ |
| 2298 | |
| 2299 | if (preempt_count() == 1) |
| 2300 | per_cpu(rb_need_resched, cpu) = resched; |
| 2301 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2302 | return event; |
| 2303 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2304 | out: |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame] | 2305 | trace_recursive_unlock(); |
| 2306 | |
| 2307 | out_nocheck: |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 2308 | ftrace_preempt_enable(resched); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2309 | return NULL; |
| 2310 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2311 | EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2312 | |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 2313 | static void |
| 2314 | rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2315 | struct ring_buffer_event *event) |
| 2316 | { |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2317 | /* |
| 2318 | * The event first in the commit queue updates the |
| 2319 | * time stamp. |
| 2320 | */ |
| 2321 | if (rb_event_is_commit(cpu_buffer, event)) |
| 2322 | cpu_buffer->write_stamp += event->time_delta; |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 2323 | } |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2324 | |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 2325 | static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer, |
| 2326 | struct ring_buffer_event *event) |
| 2327 | { |
| 2328 | local_inc(&cpu_buffer->entries); |
| 2329 | rb_update_write_stamp(cpu_buffer, event); |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2330 | rb_end_commit(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | /** |
| 2334 | * ring_buffer_unlock_commit - commit a reserved |
| 2335 | * @buffer: The buffer to commit to |
| 2336 | * @event: The event pointer to commit. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2337 | * |
| 2338 | * This commits the data to the ring buffer, and releases any locks held. |
| 2339 | * |
| 2340 | * Must be paired with ring_buffer_lock_reserve. |
| 2341 | */ |
| 2342 | int ring_buffer_unlock_commit(struct ring_buffer *buffer, |
Arnaldo Carvalho de Melo | 0a98775 | 2009-02-05 16:12:56 -0200 | [diff] [blame] | 2343 | struct ring_buffer_event *event) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2344 | { |
| 2345 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2346 | int cpu = raw_smp_processor_id(); |
| 2347 | |
| 2348 | cpu_buffer = buffer->buffers[cpu]; |
| 2349 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2350 | rb_commit(cpu_buffer, event); |
| 2351 | |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame] | 2352 | trace_recursive_unlock(); |
| 2353 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2354 | /* |
| 2355 | * Only the last preempt count needs to restore preemption. |
| 2356 | */ |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 2357 | if (preempt_count() == 1) |
| 2358 | ftrace_preempt_enable(per_cpu(rb_need_resched, cpu)); |
| 2359 | else |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2360 | preempt_enable_no_resched_notrace(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2361 | |
| 2362 | return 0; |
| 2363 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2364 | EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2365 | |
Frederic Weisbecker | f3b9aae | 2009-04-19 23:39:33 +0200 | [diff] [blame] | 2366 | static inline void rb_event_discard(struct ring_buffer_event *event) |
| 2367 | { |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 2368 | /* array[0] holds the actual length for the discarded event */ |
| 2369 | event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE; |
| 2370 | event->type_len = RINGBUF_TYPE_PADDING; |
Frederic Weisbecker | f3b9aae | 2009-04-19 23:39:33 +0200 | [diff] [blame] | 2371 | /* time delta must be non zero */ |
| 2372 | if (!event->time_delta) |
| 2373 | event->time_delta = 1; |
| 2374 | } |
| 2375 | |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 2376 | /* |
| 2377 | * Decrement the entries to the page that an event is on. |
| 2378 | * The event does not even need to exist, only the pointer |
| 2379 | * to the page it is on. This may only be called before the commit |
| 2380 | * takes place. |
| 2381 | */ |
| 2382 | static inline void |
| 2383 | rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer, |
| 2384 | struct ring_buffer_event *event) |
| 2385 | { |
| 2386 | unsigned long addr = (unsigned long)event; |
| 2387 | struct buffer_page *bpage = cpu_buffer->commit_page; |
| 2388 | struct buffer_page *start; |
| 2389 | |
| 2390 | addr &= PAGE_MASK; |
| 2391 | |
| 2392 | /* Do the likely case first */ |
| 2393 | if (likely(bpage->page == (void *)addr)) { |
| 2394 | local_dec(&bpage->entries); |
| 2395 | return; |
| 2396 | } |
| 2397 | |
| 2398 | /* |
| 2399 | * Because the commit page may be on the reader page we |
| 2400 | * start with the next page and check the end loop there. |
| 2401 | */ |
| 2402 | rb_inc_page(cpu_buffer, &bpage); |
| 2403 | start = bpage; |
| 2404 | do { |
| 2405 | if (bpage->page == (void *)addr) { |
| 2406 | local_dec(&bpage->entries); |
| 2407 | return; |
| 2408 | } |
| 2409 | rb_inc_page(cpu_buffer, &bpage); |
| 2410 | } while (bpage != start); |
| 2411 | |
| 2412 | /* commit not part of this buffer?? */ |
| 2413 | RB_WARN_ON(cpu_buffer, 1); |
| 2414 | } |
| 2415 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2416 | /** |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 2417 | * ring_buffer_commit_discard - discard an event that has not been committed |
| 2418 | * @buffer: the ring buffer |
| 2419 | * @event: non committed event to discard |
| 2420 | * |
Steven Rostedt | dc892f7 | 2009-09-03 15:33:41 -0400 | [diff] [blame] | 2421 | * Sometimes an event that is in the ring buffer needs to be ignored. |
| 2422 | * This function lets the user discard an event in the ring buffer |
| 2423 | * and then that event will not be read later. |
| 2424 | * |
| 2425 | * This function only works if it is called before the the item has been |
| 2426 | * committed. It will try to free the event from the ring buffer |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 2427 | * if another event has not been added behind it. |
| 2428 | * |
| 2429 | * If another event has been added behind it, it will set the event |
| 2430 | * up as discarded, and perform the commit. |
| 2431 | * |
| 2432 | * If this function is called, do not call ring_buffer_unlock_commit on |
| 2433 | * the event. |
| 2434 | */ |
| 2435 | void ring_buffer_discard_commit(struct ring_buffer *buffer, |
| 2436 | struct ring_buffer_event *event) |
| 2437 | { |
| 2438 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 2439 | int cpu; |
| 2440 | |
| 2441 | /* The event is discarded regardless */ |
Frederic Weisbecker | f3b9aae | 2009-04-19 23:39:33 +0200 | [diff] [blame] | 2442 | rb_event_discard(event); |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 2443 | |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2444 | cpu = smp_processor_id(); |
| 2445 | cpu_buffer = buffer->buffers[cpu]; |
| 2446 | |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 2447 | /* |
| 2448 | * This must only be called if the event has not been |
| 2449 | * committed yet. Thus we can assume that preemption |
| 2450 | * is still disabled. |
| 2451 | */ |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2452 | RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing)); |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 2453 | |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 2454 | rb_decrement_entry(cpu_buffer, event); |
Steven Rostedt | 0f2541d | 2009-08-05 12:02:48 -0400 | [diff] [blame] | 2455 | if (rb_try_to_discard(cpu_buffer, event)) |
Steven Rostedt | edd813bf | 2009-06-02 23:00:53 -0400 | [diff] [blame] | 2456 | goto out; |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 2457 | |
| 2458 | /* |
| 2459 | * The commit is still visible by the reader, so we |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 2460 | * must still update the timestamp. |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 2461 | */ |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 2462 | rb_update_write_stamp(cpu_buffer, event); |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 2463 | out: |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 2464 | rb_end_commit(cpu_buffer); |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 2465 | |
Frederic Weisbecker | f3b9aae | 2009-04-19 23:39:33 +0200 | [diff] [blame] | 2466 | trace_recursive_unlock(); |
| 2467 | |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 2468 | /* |
| 2469 | * Only the last preempt count needs to restore preemption. |
| 2470 | */ |
| 2471 | if (preempt_count() == 1) |
| 2472 | ftrace_preempt_enable(per_cpu(rb_need_resched, cpu)); |
| 2473 | else |
| 2474 | preempt_enable_no_resched_notrace(); |
| 2475 | |
| 2476 | } |
| 2477 | EXPORT_SYMBOL_GPL(ring_buffer_discard_commit); |
| 2478 | |
| 2479 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2480 | * ring_buffer_write - write data to the buffer without reserving |
| 2481 | * @buffer: The ring buffer to write to. |
| 2482 | * @length: The length of the data being written (excluding the event header) |
| 2483 | * @data: The data to write to the buffer. |
| 2484 | * |
| 2485 | * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as |
| 2486 | * one function. If you already have the data to write to the buffer, it |
| 2487 | * may be easier to simply call this function. |
| 2488 | * |
| 2489 | * Note, like ring_buffer_lock_reserve, the length is the length of the data |
| 2490 | * and not the length of the event which would hold the header. |
| 2491 | */ |
| 2492 | int ring_buffer_write(struct ring_buffer *buffer, |
| 2493 | unsigned long length, |
| 2494 | void *data) |
| 2495 | { |
| 2496 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2497 | struct ring_buffer_event *event; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2498 | void *body; |
| 2499 | int ret = -EBUSY; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2500 | int cpu, resched; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2501 | |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 2502 | if (ring_buffer_flags != RB_BUFFERS_ON) |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 2503 | return -EBUSY; |
| 2504 | |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 2505 | resched = ftrace_preempt_disable(); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2506 | |
Lai Jiangshan | 52fbe9c | 2010-03-08 14:50:43 +0800 | [diff] [blame] | 2507 | if (atomic_read(&buffer->record_disabled)) |
| 2508 | goto out; |
| 2509 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2510 | cpu = raw_smp_processor_id(); |
| 2511 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 2512 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2513 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2514 | |
| 2515 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2516 | |
| 2517 | if (atomic_read(&cpu_buffer->record_disabled)) |
| 2518 | goto out; |
| 2519 | |
Steven Rostedt | be957c4 | 2009-05-11 14:42:53 -0400 | [diff] [blame] | 2520 | if (length > BUF_MAX_DATA_SIZE) |
| 2521 | goto out; |
| 2522 | |
Steven Rostedt | 62f0b3e | 2009-09-04 14:11:34 -0400 | [diff] [blame] | 2523 | event = rb_reserve_next_event(buffer, cpu_buffer, length); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2524 | if (!event) |
| 2525 | goto out; |
| 2526 | |
| 2527 | body = rb_event_data(event); |
| 2528 | |
| 2529 | memcpy(body, data, length); |
| 2530 | |
| 2531 | rb_commit(cpu_buffer, event); |
| 2532 | |
| 2533 | ret = 0; |
| 2534 | out: |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 2535 | ftrace_preempt_enable(resched); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2536 | |
| 2537 | return ret; |
| 2538 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2539 | EXPORT_SYMBOL_GPL(ring_buffer_write); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2540 | |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 2541 | 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] | 2542 | { |
| 2543 | struct buffer_page *reader = cpu_buffer->reader_page; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2544 | struct buffer_page *head = rb_set_head_page(cpu_buffer); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2545 | struct buffer_page *commit = cpu_buffer->commit_page; |
| 2546 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2547 | /* In case of error, head will be NULL */ |
| 2548 | if (unlikely(!head)) |
| 2549 | return 1; |
| 2550 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2551 | return reader->read == rb_page_commit(reader) && |
| 2552 | (commit == reader || |
| 2553 | (commit == head && |
| 2554 | head->read == rb_page_commit(commit))); |
| 2555 | } |
| 2556 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2557 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2558 | * ring_buffer_record_disable - stop all writes into the buffer |
| 2559 | * @buffer: The ring buffer to stop writes to. |
| 2560 | * |
| 2561 | * This prevents all writes to the buffer. Any attempt to write |
| 2562 | * to the buffer after this will fail and return NULL. |
| 2563 | * |
| 2564 | * The caller should call synchronize_sched() after this. |
| 2565 | */ |
| 2566 | void ring_buffer_record_disable(struct ring_buffer *buffer) |
| 2567 | { |
| 2568 | atomic_inc(&buffer->record_disabled); |
| 2569 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2570 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2571 | |
| 2572 | /** |
| 2573 | * ring_buffer_record_enable - enable writes to the buffer |
| 2574 | * @buffer: The ring buffer to enable writes |
| 2575 | * |
| 2576 | * Note, multiple disables will need the same number of enables |
Adam Buchbinder | c41b20e | 2009-12-11 16:35:39 -0500 | [diff] [blame] | 2577 | * to truly enable the writing (much like preempt_disable). |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2578 | */ |
| 2579 | void ring_buffer_record_enable(struct ring_buffer *buffer) |
| 2580 | { |
| 2581 | atomic_dec(&buffer->record_disabled); |
| 2582 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2583 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2584 | |
| 2585 | /** |
| 2586 | * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer |
| 2587 | * @buffer: The ring buffer to stop writes to. |
| 2588 | * @cpu: The CPU buffer to stop |
| 2589 | * |
| 2590 | * This prevents all writes to the buffer. Any attempt to write |
| 2591 | * to the buffer after this will fail and return NULL. |
| 2592 | * |
| 2593 | * The caller should call synchronize_sched() after this. |
| 2594 | */ |
| 2595 | void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu) |
| 2596 | { |
| 2597 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2598 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 2599 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2600 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2601 | |
| 2602 | cpu_buffer = buffer->buffers[cpu]; |
| 2603 | atomic_inc(&cpu_buffer->record_disabled); |
| 2604 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2605 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2606 | |
| 2607 | /** |
| 2608 | * ring_buffer_record_enable_cpu - enable writes to the buffer |
| 2609 | * @buffer: The ring buffer to enable writes |
| 2610 | * @cpu: The CPU to enable. |
| 2611 | * |
| 2612 | * Note, multiple disables will need the same number of enables |
Adam Buchbinder | c41b20e | 2009-12-11 16:35:39 -0500 | [diff] [blame] | 2613 | * to truly enable the writing (much like preempt_disable). |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2614 | */ |
| 2615 | void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu) |
| 2616 | { |
| 2617 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2618 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 2619 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2620 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2621 | |
| 2622 | cpu_buffer = buffer->buffers[cpu]; |
| 2623 | atomic_dec(&cpu_buffer->record_disabled); |
| 2624 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2625 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2626 | |
| 2627 | /** |
| 2628 | * ring_buffer_entries_cpu - get the number of entries in a cpu buffer |
| 2629 | * @buffer: The ring buffer |
| 2630 | * @cpu: The per CPU buffer to get the entries from. |
| 2631 | */ |
| 2632 | unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu) |
| 2633 | { |
| 2634 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2635 | unsigned long ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2636 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 2637 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2638 | return 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2639 | |
| 2640 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2641 | ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun)) |
Steven Rostedt | e4906ef | 2009-04-30 20:49:44 -0400 | [diff] [blame] | 2642 | - cpu_buffer->read; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2643 | |
| 2644 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2645 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2646 | EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2647 | |
| 2648 | /** |
| 2649 | * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer |
| 2650 | * @buffer: The ring buffer |
| 2651 | * @cpu: The per CPU buffer to get the number of overruns from |
| 2652 | */ |
| 2653 | unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu) |
| 2654 | { |
| 2655 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2656 | unsigned long ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2657 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 2658 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2659 | return 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2660 | |
| 2661 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2662 | ret = local_read(&cpu_buffer->overrun); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2663 | |
| 2664 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2665 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2666 | EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2667 | |
| 2668 | /** |
Steven Rostedt | f0d2c68 | 2009-04-29 13:43:37 -0400 | [diff] [blame] | 2669 | * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits |
| 2670 | * @buffer: The ring buffer |
| 2671 | * @cpu: The per CPU buffer to get the number of overruns from |
| 2672 | */ |
| 2673 | unsigned long |
| 2674 | ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu) |
| 2675 | { |
| 2676 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2677 | unsigned long ret; |
| 2678 | |
| 2679 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 2680 | return 0; |
| 2681 | |
| 2682 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2683 | ret = local_read(&cpu_buffer->commit_overrun); |
Steven Rostedt | f0d2c68 | 2009-04-29 13:43:37 -0400 | [diff] [blame] | 2684 | |
| 2685 | return ret; |
| 2686 | } |
| 2687 | EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu); |
| 2688 | |
| 2689 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2690 | * ring_buffer_entries - get the number of entries in a buffer |
| 2691 | * @buffer: The ring buffer |
| 2692 | * |
| 2693 | * Returns the total number of entries in the ring buffer |
| 2694 | * (all CPU entries) |
| 2695 | */ |
| 2696 | unsigned long ring_buffer_entries(struct ring_buffer *buffer) |
| 2697 | { |
| 2698 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2699 | unsigned long entries = 0; |
| 2700 | int cpu; |
| 2701 | |
| 2702 | /* if you care about this being correct, lock the buffer */ |
| 2703 | for_each_buffer_cpu(buffer, cpu) { |
| 2704 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | e4906ef | 2009-04-30 20:49:44 -0400 | [diff] [blame] | 2705 | entries += (local_read(&cpu_buffer->entries) - |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2706 | local_read(&cpu_buffer->overrun)) - cpu_buffer->read; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | return entries; |
| 2710 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2711 | EXPORT_SYMBOL_GPL(ring_buffer_entries); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2712 | |
| 2713 | /** |
Jiri Olsa | 67b394f | 2009-10-23 19:36:18 -0400 | [diff] [blame] | 2714 | * ring_buffer_overruns - get the number of overruns in buffer |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2715 | * @buffer: The ring buffer |
| 2716 | * |
| 2717 | * Returns the total number of overruns in the ring buffer |
| 2718 | * (all CPU entries) |
| 2719 | */ |
| 2720 | unsigned long ring_buffer_overruns(struct ring_buffer *buffer) |
| 2721 | { |
| 2722 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2723 | unsigned long overruns = 0; |
| 2724 | int cpu; |
| 2725 | |
| 2726 | /* if you care about this being correct, lock the buffer */ |
| 2727 | for_each_buffer_cpu(buffer, cpu) { |
| 2728 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2729 | overruns += local_read(&cpu_buffer->overrun); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | return overruns; |
| 2733 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2734 | EXPORT_SYMBOL_GPL(ring_buffer_overruns); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2735 | |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 2736 | static void rb_iter_reset(struct ring_buffer_iter *iter) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2737 | { |
| 2738 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 2739 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2740 | /* Iterator usage is expected to have record disabled */ |
| 2741 | if (list_empty(&cpu_buffer->reader_page->list)) { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2742 | iter->head_page = rb_set_head_page(cpu_buffer); |
| 2743 | if (unlikely(!iter->head_page)) |
| 2744 | return; |
| 2745 | iter->head = iter->head_page->read; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2746 | } else { |
| 2747 | iter->head_page = cpu_buffer->reader_page; |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 2748 | iter->head = cpu_buffer->reader_page->read; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2749 | } |
| 2750 | if (iter->head) |
| 2751 | iter->read_stamp = cpu_buffer->read_stamp; |
| 2752 | else |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 2753 | iter->read_stamp = iter->head_page->page->time_stamp; |
Steven Rostedt | 492a74f | 2010-01-25 15:17:47 -0500 | [diff] [blame] | 2754 | iter->cache_reader_page = cpu_buffer->reader_page; |
| 2755 | iter->cache_read = cpu_buffer->read; |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 2756 | } |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2757 | |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 2758 | /** |
| 2759 | * ring_buffer_iter_reset - reset an iterator |
| 2760 | * @iter: The iterator to reset |
| 2761 | * |
| 2762 | * Resets the iterator, so that it will start from the beginning |
| 2763 | * again. |
| 2764 | */ |
| 2765 | void ring_buffer_iter_reset(struct ring_buffer_iter *iter) |
| 2766 | { |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2767 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 2768 | unsigned long flags; |
| 2769 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2770 | if (!iter) |
| 2771 | return; |
| 2772 | |
| 2773 | cpu_buffer = iter->cpu_buffer; |
| 2774 | |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 2775 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 2776 | rb_iter_reset(iter); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2777 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2778 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2779 | EXPORT_SYMBOL_GPL(ring_buffer_iter_reset); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2780 | |
| 2781 | /** |
| 2782 | * ring_buffer_iter_empty - check if an iterator has no more to read |
| 2783 | * @iter: The iterator to check |
| 2784 | */ |
| 2785 | int ring_buffer_iter_empty(struct ring_buffer_iter *iter) |
| 2786 | { |
| 2787 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2788 | |
| 2789 | cpu_buffer = iter->cpu_buffer; |
| 2790 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2791 | return iter->head_page == cpu_buffer->commit_page && |
| 2792 | iter->head == rb_commit_index(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2793 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2794 | EXPORT_SYMBOL_GPL(ring_buffer_iter_empty); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2795 | |
| 2796 | static void |
| 2797 | rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer, |
| 2798 | struct ring_buffer_event *event) |
| 2799 | { |
| 2800 | u64 delta; |
| 2801 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 2802 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2803 | case RINGBUF_TYPE_PADDING: |
| 2804 | return; |
| 2805 | |
| 2806 | case RINGBUF_TYPE_TIME_EXTEND: |
| 2807 | delta = event->array[0]; |
| 2808 | delta <<= TS_SHIFT; |
| 2809 | delta += event->time_delta; |
| 2810 | cpu_buffer->read_stamp += delta; |
| 2811 | return; |
| 2812 | |
| 2813 | case RINGBUF_TYPE_TIME_STAMP: |
| 2814 | /* FIXME: not implemented */ |
| 2815 | return; |
| 2816 | |
| 2817 | case RINGBUF_TYPE_DATA: |
| 2818 | cpu_buffer->read_stamp += event->time_delta; |
| 2819 | return; |
| 2820 | |
| 2821 | default: |
| 2822 | BUG(); |
| 2823 | } |
| 2824 | return; |
| 2825 | } |
| 2826 | |
| 2827 | static void |
| 2828 | rb_update_iter_read_stamp(struct ring_buffer_iter *iter, |
| 2829 | struct ring_buffer_event *event) |
| 2830 | { |
| 2831 | u64 delta; |
| 2832 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 2833 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2834 | case RINGBUF_TYPE_PADDING: |
| 2835 | return; |
| 2836 | |
| 2837 | case RINGBUF_TYPE_TIME_EXTEND: |
| 2838 | delta = event->array[0]; |
| 2839 | delta <<= TS_SHIFT; |
| 2840 | delta += event->time_delta; |
| 2841 | iter->read_stamp += delta; |
| 2842 | return; |
| 2843 | |
| 2844 | case RINGBUF_TYPE_TIME_STAMP: |
| 2845 | /* FIXME: not implemented */ |
| 2846 | return; |
| 2847 | |
| 2848 | case RINGBUF_TYPE_DATA: |
| 2849 | iter->read_stamp += event->time_delta; |
| 2850 | return; |
| 2851 | |
| 2852 | default: |
| 2853 | BUG(); |
| 2854 | } |
| 2855 | return; |
| 2856 | } |
| 2857 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2858 | static struct buffer_page * |
| 2859 | rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2860 | { |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2861 | struct buffer_page *reader = NULL; |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 2862 | unsigned long overwrite; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2863 | unsigned long flags; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2864 | int nr_loops = 0; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2865 | int ret; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2866 | |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 2867 | local_irq_save(flags); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 2868 | arch_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2869 | |
| 2870 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2871 | /* |
| 2872 | * This should normally only loop twice. But because the |
| 2873 | * start of the reader inserts an empty page, it causes |
| 2874 | * a case where we will loop three times. There should be no |
| 2875 | * reason to loop four times (that I know of). |
| 2876 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 2877 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) { |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2878 | reader = NULL; |
| 2879 | goto out; |
| 2880 | } |
| 2881 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2882 | reader = cpu_buffer->reader_page; |
| 2883 | |
| 2884 | /* If there's more to read, return this page */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2885 | if (cpu_buffer->reader_page->read < rb_page_size(reader)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2886 | goto out; |
| 2887 | |
| 2888 | /* Never should we have an index greater than the size */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 2889 | if (RB_WARN_ON(cpu_buffer, |
| 2890 | cpu_buffer->reader_page->read > rb_page_size(reader))) |
| 2891 | goto out; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2892 | |
| 2893 | /* check if we caught up to the tail */ |
| 2894 | reader = NULL; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2895 | if (cpu_buffer->commit_page == cpu_buffer->reader_page) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2896 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2897 | |
| 2898 | /* |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2899 | * Reset the reader page to size zero. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2900 | */ |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2901 | local_set(&cpu_buffer->reader_page->write, 0); |
| 2902 | local_set(&cpu_buffer->reader_page->entries, 0); |
| 2903 | local_set(&cpu_buffer->reader_page->page->commit, 0); |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 2904 | cpu_buffer->reader_page->real_end = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2905 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2906 | spin: |
| 2907 | /* |
| 2908 | * Splice the empty reader page into the list around the head. |
| 2909 | */ |
| 2910 | reader = rb_set_head_page(cpu_buffer); |
Steven Rostedt | 0e1ff5d | 2010-01-06 20:40:44 -0500 | [diff] [blame] | 2911 | cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2912 | cpu_buffer->reader_page->list.prev = reader->list.prev; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2913 | |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 2914 | /* |
| 2915 | * cpu_buffer->pages just needs to point to the buffer, it |
| 2916 | * has no specific buffer page to point to. Lets move it out |
| 2917 | * of our way so we don't accidently swap it. |
| 2918 | */ |
| 2919 | cpu_buffer->pages = reader->list.prev; |
| 2920 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2921 | /* The reader page will be pointing to the new head */ |
| 2922 | rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2923 | |
| 2924 | /* |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 2925 | * We want to make sure we read the overruns after we set up our |
| 2926 | * pointers to the next object. The writer side does a |
| 2927 | * cmpxchg to cross pages which acts as the mb on the writer |
| 2928 | * side. Note, the reader will constantly fail the swap |
| 2929 | * while the writer is updating the pointers, so this |
| 2930 | * guarantees that the overwrite recorded here is the one we |
| 2931 | * want to compare with the last_overrun. |
| 2932 | */ |
| 2933 | smp_mb(); |
| 2934 | overwrite = local_read(&(cpu_buffer->overrun)); |
| 2935 | |
| 2936 | /* |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2937 | * Here's the tricky part. |
| 2938 | * |
| 2939 | * We need to move the pointer past the header page. |
| 2940 | * But we can only do that if a writer is not currently |
| 2941 | * moving it. The page before the header page has the |
| 2942 | * flag bit '1' set if it is pointing to the page we want. |
| 2943 | * but if the writer is in the process of moving it |
| 2944 | * than it will be '2' or already moved '0'. |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2945 | */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2946 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2947 | ret = rb_head_page_replace(reader, cpu_buffer->reader_page); |
| 2948 | |
| 2949 | /* |
| 2950 | * If we did not convert it, then we must try again. |
| 2951 | */ |
| 2952 | if (!ret) |
| 2953 | goto spin; |
| 2954 | |
| 2955 | /* |
| 2956 | * Yeah! We succeeded in replacing the page. |
| 2957 | * |
| 2958 | * Now make the new head point back to the reader page. |
| 2959 | */ |
David Sharp | 5ded3dc6 | 2010-01-06 17:12:07 -0800 | [diff] [blame] | 2960 | rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 2961 | rb_inc_page(cpu_buffer, &cpu_buffer->head_page); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2962 | |
| 2963 | /* Finally update the reader page to the new head */ |
| 2964 | cpu_buffer->reader_page = reader; |
| 2965 | rb_reset_reader_page(cpu_buffer); |
| 2966 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 2967 | if (overwrite != cpu_buffer->last_overrun) { |
| 2968 | cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun; |
| 2969 | cpu_buffer->last_overrun = overwrite; |
| 2970 | } |
| 2971 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2972 | goto again; |
| 2973 | |
| 2974 | out: |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 2975 | arch_spin_unlock(&cpu_buffer->lock); |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 2976 | local_irq_restore(flags); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2977 | |
| 2978 | return reader; |
| 2979 | } |
| 2980 | |
| 2981 | static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer) |
| 2982 | { |
| 2983 | struct ring_buffer_event *event; |
| 2984 | struct buffer_page *reader; |
| 2985 | unsigned length; |
| 2986 | |
| 2987 | reader = rb_get_reader_page(cpu_buffer); |
| 2988 | |
| 2989 | /* This function should not be called when buffer is empty */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 2990 | if (RB_WARN_ON(cpu_buffer, !reader)) |
| 2991 | return; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2992 | |
| 2993 | event = rb_reader_event(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2994 | |
Steven Rostedt | a1863c2 | 2009-09-03 10:23:58 -0400 | [diff] [blame] | 2995 | if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX) |
Steven Rostedt | e4906ef | 2009-04-30 20:49:44 -0400 | [diff] [blame] | 2996 | cpu_buffer->read++; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2997 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2998 | rb_update_read_stamp(cpu_buffer, event); |
| 2999 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3000 | length = rb_event_length(event); |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 3001 | cpu_buffer->reader_page->read += length; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | static void rb_advance_iter(struct ring_buffer_iter *iter) |
| 3005 | { |
| 3006 | struct ring_buffer *buffer; |
| 3007 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3008 | struct ring_buffer_event *event; |
| 3009 | unsigned length; |
| 3010 | |
| 3011 | cpu_buffer = iter->cpu_buffer; |
| 3012 | buffer = cpu_buffer->buffer; |
| 3013 | |
| 3014 | /* |
| 3015 | * Check if we are at the end of the buffer. |
| 3016 | */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3017 | if (iter->head >= rb_page_size(iter->head_page)) { |
Steven Rostedt | ea05b57 | 2009-06-03 09:30:10 -0400 | [diff] [blame] | 3018 | /* discarded commits can make the page empty */ |
| 3019 | if (iter->head_page == cpu_buffer->commit_page) |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 3020 | return; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3021 | rb_inc_iter(iter); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3022 | return; |
| 3023 | } |
| 3024 | |
| 3025 | event = rb_iter_head_event(iter); |
| 3026 | |
| 3027 | length = rb_event_length(event); |
| 3028 | |
| 3029 | /* |
| 3030 | * This should not be called to advance the header if we are |
| 3031 | * at the tail of the buffer. |
| 3032 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 3033 | if (RB_WARN_ON(cpu_buffer, |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 3034 | (iter->head_page == cpu_buffer->commit_page) && |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 3035 | (iter->head + length > rb_commit_index(cpu_buffer)))) |
| 3036 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3037 | |
| 3038 | rb_update_iter_read_stamp(iter, event); |
| 3039 | |
| 3040 | iter->head += length; |
| 3041 | |
| 3042 | /* check for end of page padding */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3043 | if ((iter->head >= rb_page_size(iter->head_page)) && |
| 3044 | (iter->head_page != cpu_buffer->commit_page)) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3045 | rb_advance_iter(iter); |
| 3046 | } |
| 3047 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3048 | static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer) |
| 3049 | { |
| 3050 | return cpu_buffer->lost_events; |
| 3051 | } |
| 3052 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3053 | static struct ring_buffer_event * |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3054 | rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts, |
| 3055 | unsigned long *lost_events) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3056 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3057 | struct ring_buffer_event *event; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3058 | struct buffer_page *reader; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3059 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3060 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3061 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3062 | /* |
| 3063 | * We repeat when a timestamp is encountered. It is possible |
| 3064 | * to get multiple timestamps from an interrupt entering just |
Steven Rostedt | ea05b57 | 2009-06-03 09:30:10 -0400 | [diff] [blame] | 3065 | * as one timestamp is about to be written, or from discarded |
| 3066 | * commits. The most that we can have is the number on a single page. |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3067 | */ |
Steven Rostedt | ea05b57 | 2009-06-03 09:30:10 -0400 | [diff] [blame] | 3068 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE)) |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3069 | return NULL; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3070 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3071 | reader = rb_get_reader_page(cpu_buffer); |
| 3072 | if (!reader) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3073 | return NULL; |
| 3074 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3075 | event = rb_reader_event(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3076 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 3077 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3078 | case RINGBUF_TYPE_PADDING: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 3079 | if (rb_null_event(event)) |
| 3080 | RB_WARN_ON(cpu_buffer, 1); |
| 3081 | /* |
| 3082 | * Because the writer could be discarding every |
| 3083 | * event it creates (which would probably be bad) |
| 3084 | * if we were to go back to "again" then we may never |
| 3085 | * catch up, and will trigger the warn on, or lock |
| 3086 | * the box. Return the padding, and we will release |
| 3087 | * the current locks, and try again. |
| 3088 | */ |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 3089 | return event; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3090 | |
| 3091 | case RINGBUF_TYPE_TIME_EXTEND: |
| 3092 | /* Internal data, OK to advance */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3093 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3094 | goto again; |
| 3095 | |
| 3096 | case RINGBUF_TYPE_TIME_STAMP: |
| 3097 | /* FIXME: not implemented */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3098 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3099 | goto again; |
| 3100 | |
| 3101 | case RINGBUF_TYPE_DATA: |
| 3102 | if (ts) { |
| 3103 | *ts = cpu_buffer->read_stamp + event->time_delta; |
Robert Richter | d8eeb2d | 2009-07-31 14:58:04 +0200 | [diff] [blame] | 3104 | ring_buffer_normalize_time_stamp(cpu_buffer->buffer, |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 3105 | cpu_buffer->cpu, ts); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3106 | } |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3107 | if (lost_events) |
| 3108 | *lost_events = rb_lost_events(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3109 | return event; |
| 3110 | |
| 3111 | default: |
| 3112 | BUG(); |
| 3113 | } |
| 3114 | |
| 3115 | return NULL; |
| 3116 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3117 | EXPORT_SYMBOL_GPL(ring_buffer_peek); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3118 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3119 | static struct ring_buffer_event * |
| 3120 | rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3121 | { |
| 3122 | struct ring_buffer *buffer; |
| 3123 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3124 | struct ring_buffer_event *event; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3125 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3126 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3127 | cpu_buffer = iter->cpu_buffer; |
| 3128 | buffer = cpu_buffer->buffer; |
| 3129 | |
Steven Rostedt | 492a74f | 2010-01-25 15:17:47 -0500 | [diff] [blame] | 3130 | /* |
| 3131 | * Check if someone performed a consuming read to |
| 3132 | * the buffer. A consuming read invalidates the iterator |
| 3133 | * and we need to reset the iterator in this case. |
| 3134 | */ |
| 3135 | if (unlikely(iter->cache_read != cpu_buffer->read || |
| 3136 | iter->cache_reader_page != cpu_buffer->reader_page)) |
| 3137 | rb_iter_reset(iter); |
| 3138 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3139 | again: |
Steven Rostedt | 3c05d74 | 2010-01-26 16:14:08 -0500 | [diff] [blame] | 3140 | if (ring_buffer_iter_empty(iter)) |
| 3141 | return NULL; |
| 3142 | |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3143 | /* |
Steven Rostedt | ea05b57 | 2009-06-03 09:30:10 -0400 | [diff] [blame] | 3144 | * We repeat when a timestamp is encountered. |
| 3145 | * We can get multiple timestamps by nested interrupts or also |
| 3146 | * if filtering is on (discarding commits). Since discarding |
| 3147 | * commits can be frequent we can get a lot of timestamps. |
| 3148 | * But we limit them by not adding timestamps if they begin |
| 3149 | * at the start of a page. |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3150 | */ |
Steven Rostedt | ea05b57 | 2009-06-03 09:30:10 -0400 | [diff] [blame] | 3151 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE)) |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3152 | return NULL; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 3153 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3154 | if (rb_per_cpu_empty(cpu_buffer)) |
| 3155 | return NULL; |
| 3156 | |
Steven Rostedt | 3c05d74 | 2010-01-26 16:14:08 -0500 | [diff] [blame] | 3157 | if (iter->head >= local_read(&iter->head_page->page->commit)) { |
| 3158 | rb_inc_iter(iter); |
| 3159 | goto again; |
| 3160 | } |
| 3161 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3162 | event = rb_iter_head_event(iter); |
| 3163 | |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 3164 | switch (event->type_len) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3165 | case RINGBUF_TYPE_PADDING: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 3166 | if (rb_null_event(event)) { |
| 3167 | rb_inc_iter(iter); |
| 3168 | goto again; |
| 3169 | } |
| 3170 | rb_advance_iter(iter); |
| 3171 | return event; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3172 | |
| 3173 | case RINGBUF_TYPE_TIME_EXTEND: |
| 3174 | /* Internal data, OK to advance */ |
| 3175 | rb_advance_iter(iter); |
| 3176 | goto again; |
| 3177 | |
| 3178 | case RINGBUF_TYPE_TIME_STAMP: |
| 3179 | /* FIXME: not implemented */ |
| 3180 | rb_advance_iter(iter); |
| 3181 | goto again; |
| 3182 | |
| 3183 | case RINGBUF_TYPE_DATA: |
| 3184 | if (ts) { |
| 3185 | *ts = iter->read_stamp + event->time_delta; |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 3186 | ring_buffer_normalize_time_stamp(buffer, |
| 3187 | cpu_buffer->cpu, ts); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3188 | } |
| 3189 | return event; |
| 3190 | |
| 3191 | default: |
| 3192 | BUG(); |
| 3193 | } |
| 3194 | |
| 3195 | return NULL; |
| 3196 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3197 | EXPORT_SYMBOL_GPL(ring_buffer_iter_peek); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3198 | |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3199 | static inline int rb_ok_to_lock(void) |
| 3200 | { |
| 3201 | /* |
| 3202 | * If an NMI die dumps out the content of the ring buffer |
| 3203 | * do not grab locks. We also permanently disable the ring |
| 3204 | * buffer too. A one time deal is all you get from reading |
| 3205 | * the ring buffer from an NMI. |
| 3206 | */ |
Steven Rostedt | 464e85e | 2009-08-05 15:26:37 -0400 | [diff] [blame] | 3207 | if (likely(!in_nmi())) |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3208 | return 1; |
| 3209 | |
| 3210 | tracing_off_permanent(); |
| 3211 | return 0; |
| 3212 | } |
| 3213 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3214 | /** |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3215 | * ring_buffer_peek - peek at the next event to be read |
| 3216 | * @buffer: The ring buffer to read |
| 3217 | * @cpu: The cpu to peak at |
| 3218 | * @ts: The timestamp counter of this event. |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3219 | * @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] | 3220 | * |
| 3221 | * This will return the event that will be read next, but does |
| 3222 | * not consume the data. |
| 3223 | */ |
| 3224 | struct ring_buffer_event * |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3225 | ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts, |
| 3226 | unsigned long *lost_events) |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3227 | { |
| 3228 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3229 | struct ring_buffer_event *event; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3230 | unsigned long flags; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3231 | int dolock; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3232 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3233 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3234 | return NULL; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3235 | |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3236 | dolock = rb_ok_to_lock(); |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 3237 | again: |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3238 | local_irq_save(flags); |
| 3239 | if (dolock) |
| 3240 | spin_lock(&cpu_buffer->reader_lock); |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3241 | event = rb_buffer_peek(cpu_buffer, ts, lost_events); |
Robert Richter | 469535a | 2009-07-30 19:19:18 +0200 | [diff] [blame] | 3242 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
| 3243 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3244 | if (dolock) |
| 3245 | spin_unlock(&cpu_buffer->reader_lock); |
| 3246 | local_irq_restore(flags); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3247 | |
Steven Rostedt | 1b959e1 | 2009-09-03 10:12:13 -0400 | [diff] [blame] | 3248 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 3249 | goto again; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 3250 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3251 | return event; |
| 3252 | } |
| 3253 | |
| 3254 | /** |
| 3255 | * ring_buffer_iter_peek - peek at the next event to be read |
| 3256 | * @iter: The ring buffer iterator |
| 3257 | * @ts: The timestamp counter of this event. |
| 3258 | * |
| 3259 | * This will return the event that will be read next, but does |
| 3260 | * not increment the iterator. |
| 3261 | */ |
| 3262 | struct ring_buffer_event * |
| 3263 | ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts) |
| 3264 | { |
| 3265 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 3266 | struct ring_buffer_event *event; |
| 3267 | unsigned long flags; |
| 3268 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 3269 | again: |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3270 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 3271 | event = rb_iter_peek(iter, ts); |
| 3272 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
| 3273 | |
Steven Rostedt | 1b959e1 | 2009-09-03 10:12:13 -0400 | [diff] [blame] | 3274 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 3275 | goto again; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 3276 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3277 | return event; |
| 3278 | } |
| 3279 | |
| 3280 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3281 | * ring_buffer_consume - return an event and consume it |
| 3282 | * @buffer: The ring buffer to get the next event from |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3283 | * @cpu: the cpu to read the buffer from |
| 3284 | * @ts: a variable to store the timestamp (may be NULL) |
| 3285 | * @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] | 3286 | * |
| 3287 | * Returns the next event in the ring buffer, and that event is consumed. |
| 3288 | * Meaning, that sequential reads will keep returning a different event, |
| 3289 | * and eventually empty the ring buffer if the producer is slower. |
| 3290 | */ |
| 3291 | struct ring_buffer_event * |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3292 | ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts, |
| 3293 | unsigned long *lost_events) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3294 | { |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3295 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3296 | struct ring_buffer_event *event = NULL; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3297 | unsigned long flags; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3298 | int dolock; |
| 3299 | |
| 3300 | dolock = rb_ok_to_lock(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3301 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 3302 | again: |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3303 | /* might be called in atomic */ |
| 3304 | preempt_disable(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3305 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3306 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 3307 | goto out; |
| 3308 | |
| 3309 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3310 | local_irq_save(flags); |
| 3311 | if (dolock) |
| 3312 | spin_lock(&cpu_buffer->reader_lock); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3313 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3314 | event = rb_buffer_peek(cpu_buffer, ts, lost_events); |
| 3315 | if (event) { |
| 3316 | cpu_buffer->lost_events = 0; |
Robert Richter | 469535a | 2009-07-30 19:19:18 +0200 | [diff] [blame] | 3317 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3318 | } |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3319 | |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3320 | if (dolock) |
| 3321 | spin_unlock(&cpu_buffer->reader_lock); |
| 3322 | local_irq_restore(flags); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3323 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3324 | out: |
| 3325 | preempt_enable(); |
| 3326 | |
Steven Rostedt | 1b959e1 | 2009-09-03 10:12:13 -0400 | [diff] [blame] | 3327 | if (event && event->type_len == RINGBUF_TYPE_PADDING) |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 3328 | goto again; |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 3329 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3330 | return event; |
| 3331 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3332 | EXPORT_SYMBOL_GPL(ring_buffer_consume); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3333 | |
| 3334 | /** |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame^] | 3335 | * 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] | 3336 | * @buffer: The ring buffer to read from |
| 3337 | * @cpu: The cpu buffer to iterate over |
| 3338 | * |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame^] | 3339 | * This performs the initial preparations necessary to iterate |
| 3340 | * through the buffer. Memory is allocated, buffer recording |
| 3341 | * is disabled, and the iterator pointer is returned to the caller. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3342 | * |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame^] | 3343 | * Disabling buffer recordng prevents the reading from being |
| 3344 | * corrupted. This is not a consuming read, so a producer is not |
| 3345 | * expected. |
| 3346 | * |
| 3347 | * After a sequence of ring_buffer_read_prepare calls, the user is |
| 3348 | * expected to make at least one call to ring_buffer_prepare_sync. |
| 3349 | * Afterwards, ring_buffer_read_start is invoked to get things going |
| 3350 | * for real. |
| 3351 | * |
| 3352 | * This overall must be paired with ring_buffer_finish. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3353 | */ |
| 3354 | struct ring_buffer_iter * |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame^] | 3355 | ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3356 | { |
| 3357 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3358 | struct ring_buffer_iter *iter; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3359 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3360 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3361 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3362 | |
| 3363 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); |
| 3364 | if (!iter) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3365 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3366 | |
| 3367 | cpu_buffer = buffer->buffers[cpu]; |
| 3368 | |
| 3369 | iter->cpu_buffer = cpu_buffer; |
| 3370 | |
| 3371 | atomic_inc(&cpu_buffer->record_disabled); |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame^] | 3372 | |
| 3373 | return iter; |
| 3374 | } |
| 3375 | EXPORT_SYMBOL_GPL(ring_buffer_read_prepare); |
| 3376 | |
| 3377 | /** |
| 3378 | * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls |
| 3379 | * |
| 3380 | * All previously invoked ring_buffer_read_prepare calls to prepare |
| 3381 | * iterators will be synchronized. Afterwards, read_buffer_read_start |
| 3382 | * calls on those iterators are allowed. |
| 3383 | */ |
| 3384 | void |
| 3385 | ring_buffer_read_prepare_sync(void) |
| 3386 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3387 | synchronize_sched(); |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame^] | 3388 | } |
| 3389 | EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync); |
| 3390 | |
| 3391 | /** |
| 3392 | * ring_buffer_read_start - start a non consuming read of the buffer |
| 3393 | * @iter: The iterator returned by ring_buffer_read_prepare |
| 3394 | * |
| 3395 | * This finalizes the startup of an iteration through the buffer. |
| 3396 | * The iterator comes from a call to ring_buffer_read_prepare and |
| 3397 | * an intervening ring_buffer_read_prepare_sync must have been |
| 3398 | * performed. |
| 3399 | * |
| 3400 | * Must be paired with ring_buffer_finish. |
| 3401 | */ |
| 3402 | void |
| 3403 | ring_buffer_read_start(struct ring_buffer_iter *iter) |
| 3404 | { |
| 3405 | struct ring_buffer_per_cpu *cpu_buffer; |
| 3406 | unsigned long flags; |
| 3407 | |
| 3408 | if (!iter) |
| 3409 | return; |
| 3410 | |
| 3411 | cpu_buffer = iter->cpu_buffer; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3412 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3413 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 3414 | arch_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 3415 | rb_iter_reset(iter); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 3416 | arch_spin_unlock(&cpu_buffer->lock); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3417 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3418 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3419 | EXPORT_SYMBOL_GPL(ring_buffer_read_start); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3420 | |
| 3421 | /** |
| 3422 | * ring_buffer_finish - finish reading the iterator of the buffer |
| 3423 | * @iter: The iterator retrieved by ring_buffer_start |
| 3424 | * |
| 3425 | * This re-enables the recording to the buffer, and frees the |
| 3426 | * iterator. |
| 3427 | */ |
| 3428 | void |
| 3429 | ring_buffer_read_finish(struct ring_buffer_iter *iter) |
| 3430 | { |
| 3431 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 3432 | |
| 3433 | atomic_dec(&cpu_buffer->record_disabled); |
| 3434 | kfree(iter); |
| 3435 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3436 | EXPORT_SYMBOL_GPL(ring_buffer_read_finish); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3437 | |
| 3438 | /** |
| 3439 | * ring_buffer_read - read the next item in the ring buffer by the iterator |
| 3440 | * @iter: The ring buffer iterator |
| 3441 | * @ts: The time stamp of the event read. |
| 3442 | * |
| 3443 | * This reads the next event in the ring buffer and increments the iterator. |
| 3444 | */ |
| 3445 | struct ring_buffer_event * |
| 3446 | ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts) |
| 3447 | { |
| 3448 | struct ring_buffer_event *event; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3449 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 3450 | unsigned long flags; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3451 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3452 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7e9391c | 2009-09-03 10:02:09 -0400 | [diff] [blame] | 3453 | again: |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3454 | event = rb_iter_peek(iter, ts); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3455 | if (!event) |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3456 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3457 | |
Steven Rostedt | 7e9391c | 2009-09-03 10:02:09 -0400 | [diff] [blame] | 3458 | if (event->type_len == RINGBUF_TYPE_PADDING) |
| 3459 | goto again; |
| 3460 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3461 | rb_advance_iter(iter); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3462 | out: |
| 3463 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3464 | |
| 3465 | return event; |
| 3466 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3467 | EXPORT_SYMBOL_GPL(ring_buffer_read); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3468 | |
| 3469 | /** |
| 3470 | * ring_buffer_size - return the size of the ring buffer (in bytes) |
| 3471 | * @buffer: The ring buffer. |
| 3472 | */ |
| 3473 | unsigned long ring_buffer_size(struct ring_buffer *buffer) |
| 3474 | { |
| 3475 | return BUF_PAGE_SIZE * buffer->pages; |
| 3476 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3477 | EXPORT_SYMBOL_GPL(ring_buffer_size); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3478 | |
| 3479 | static void |
| 3480 | rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) |
| 3481 | { |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3482 | rb_head_page_deactivate(cpu_buffer); |
| 3483 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3484 | cpu_buffer->head_page |
Steven Rostedt | 3adc54f | 2009-03-30 15:32:01 -0400 | [diff] [blame] | 3485 | = list_entry(cpu_buffer->pages, struct buffer_page, list); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3486 | local_set(&cpu_buffer->head_page->write, 0); |
Steven Rostedt | 778c55d | 2009-05-01 18:44:45 -0400 | [diff] [blame] | 3487 | local_set(&cpu_buffer->head_page->entries, 0); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 3488 | local_set(&cpu_buffer->head_page->page->commit, 0); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3489 | |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 3490 | cpu_buffer->head_page->read = 0; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 3491 | |
| 3492 | cpu_buffer->tail_page = cpu_buffer->head_page; |
| 3493 | cpu_buffer->commit_page = cpu_buffer->head_page; |
| 3494 | |
| 3495 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); |
| 3496 | local_set(&cpu_buffer->reader_page->write, 0); |
Steven Rostedt | 778c55d | 2009-05-01 18:44:45 -0400 | [diff] [blame] | 3497 | local_set(&cpu_buffer->reader_page->entries, 0); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 3498 | local_set(&cpu_buffer->reader_page->page->commit, 0); |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 3499 | cpu_buffer->reader_page->read = 0; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3500 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3501 | local_set(&cpu_buffer->commit_overrun, 0); |
| 3502 | local_set(&cpu_buffer->overrun, 0); |
Steven Rostedt | e4906ef | 2009-04-30 20:49:44 -0400 | [diff] [blame] | 3503 | local_set(&cpu_buffer->entries, 0); |
Steven Rostedt | fa74395 | 2009-06-16 12:37:57 -0400 | [diff] [blame] | 3504 | local_set(&cpu_buffer->committing, 0); |
| 3505 | local_set(&cpu_buffer->commits, 0); |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3506 | cpu_buffer->read = 0; |
Steven Rostedt | 69507c0 | 2009-01-21 18:45:57 -0500 | [diff] [blame] | 3507 | |
| 3508 | cpu_buffer->write_stamp = 0; |
| 3509 | cpu_buffer->read_stamp = 0; |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3510 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3511 | cpu_buffer->lost_events = 0; |
| 3512 | cpu_buffer->last_overrun = 0; |
| 3513 | |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3514 | rb_head_page_activate(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | /** |
| 3518 | * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer |
| 3519 | * @buffer: The ring buffer to reset a per cpu buffer of |
| 3520 | * @cpu: The CPU buffer to be reset |
| 3521 | */ |
| 3522 | void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu) |
| 3523 | { |
| 3524 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
| 3525 | unsigned long flags; |
| 3526 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3527 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3528 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3529 | |
Steven Rostedt | 41ede23 | 2009-05-01 20:26:54 -0400 | [diff] [blame] | 3530 | atomic_inc(&cpu_buffer->record_disabled); |
| 3531 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3532 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 3533 | |
Steven Rostedt | 41b6a95 | 2009-09-02 09:59:48 -0400 | [diff] [blame] | 3534 | if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing))) |
| 3535 | goto out; |
| 3536 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 3537 | arch_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3538 | |
| 3539 | rb_reset_cpu(cpu_buffer); |
| 3540 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 3541 | arch_spin_unlock(&cpu_buffer->lock); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3542 | |
Steven Rostedt | 41b6a95 | 2009-09-02 09:59:48 -0400 | [diff] [blame] | 3543 | out: |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 3544 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 41ede23 | 2009-05-01 20:26:54 -0400 | [diff] [blame] | 3545 | |
| 3546 | atomic_dec(&cpu_buffer->record_disabled); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3547 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3548 | EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3549 | |
| 3550 | /** |
| 3551 | * ring_buffer_reset - reset a ring buffer |
| 3552 | * @buffer: The ring buffer to reset all cpu buffers |
| 3553 | */ |
| 3554 | void ring_buffer_reset(struct ring_buffer *buffer) |
| 3555 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3556 | int cpu; |
| 3557 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3558 | for_each_buffer_cpu(buffer, cpu) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 3559 | ring_buffer_reset_cpu(buffer, cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3560 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3561 | EXPORT_SYMBOL_GPL(ring_buffer_reset); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3562 | |
| 3563 | /** |
| 3564 | * rind_buffer_empty - is the ring buffer empty? |
| 3565 | * @buffer: The ring buffer to test |
| 3566 | */ |
| 3567 | int ring_buffer_empty(struct ring_buffer *buffer) |
| 3568 | { |
| 3569 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 3570 | unsigned long flags; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3571 | int dolock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3572 | int cpu; |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 3573 | int ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3574 | |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3575 | dolock = rb_ok_to_lock(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3576 | |
| 3577 | /* yes this is racy, but if you don't like the race, lock the buffer */ |
| 3578 | for_each_buffer_cpu(buffer, cpu) { |
| 3579 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3580 | local_irq_save(flags); |
| 3581 | if (dolock) |
| 3582 | spin_lock(&cpu_buffer->reader_lock); |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 3583 | ret = rb_per_cpu_empty(cpu_buffer); |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3584 | if (dolock) |
| 3585 | spin_unlock(&cpu_buffer->reader_lock); |
| 3586 | local_irq_restore(flags); |
| 3587 | |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 3588 | if (!ret) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3589 | return 0; |
| 3590 | } |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3591 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3592 | return 1; |
| 3593 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3594 | EXPORT_SYMBOL_GPL(ring_buffer_empty); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3595 | |
| 3596 | /** |
| 3597 | * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty? |
| 3598 | * @buffer: The ring buffer |
| 3599 | * @cpu: The CPU buffer to test |
| 3600 | */ |
| 3601 | int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu) |
| 3602 | { |
| 3603 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | d478820 | 2009-06-17 00:39:43 -0400 | [diff] [blame] | 3604 | unsigned long flags; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3605 | int dolock; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3606 | int ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3607 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3608 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 3609 | return 1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3610 | |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3611 | dolock = rb_ok_to_lock(); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3612 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3613 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3614 | local_irq_save(flags); |
| 3615 | if (dolock) |
| 3616 | spin_lock(&cpu_buffer->reader_lock); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3617 | ret = rb_per_cpu_empty(cpu_buffer); |
Steven Rostedt | 8d707e8 | 2009-06-16 21:22:48 -0400 | [diff] [blame] | 3618 | if (dolock) |
| 3619 | spin_unlock(&cpu_buffer->reader_lock); |
| 3620 | local_irq_restore(flags); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3621 | |
| 3622 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3623 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3624 | EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3625 | |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 3626 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3627 | /** |
| 3628 | * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers |
| 3629 | * @buffer_a: One buffer to swap with |
| 3630 | * @buffer_b: The other buffer to swap with |
| 3631 | * |
| 3632 | * This function is useful for tracers that want to take a "snapshot" |
| 3633 | * of a CPU buffer and has another back up buffer lying around. |
| 3634 | * it is expected that the tracer handles the cpu buffer not being |
| 3635 | * used at the moment. |
| 3636 | */ |
| 3637 | int ring_buffer_swap_cpu(struct ring_buffer *buffer_a, |
| 3638 | struct ring_buffer *buffer_b, int cpu) |
| 3639 | { |
| 3640 | struct ring_buffer_per_cpu *cpu_buffer_a; |
| 3641 | struct ring_buffer_per_cpu *cpu_buffer_b; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3642 | int ret = -EINVAL; |
| 3643 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 3644 | if (!cpumask_test_cpu(cpu, buffer_a->cpumask) || |
| 3645 | !cpumask_test_cpu(cpu, buffer_b->cpumask)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3646 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3647 | |
| 3648 | /* At least make sure the two buffers are somewhat the same */ |
Lai Jiangshan | 6d102bc | 2008-12-17 17:48:23 +0800 | [diff] [blame] | 3649 | if (buffer_a->pages != buffer_b->pages) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3650 | goto out; |
| 3651 | |
| 3652 | ret = -EAGAIN; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3653 | |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 3654 | if (ring_buffer_flags != RB_BUFFERS_ON) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3655 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 3656 | |
| 3657 | if (atomic_read(&buffer_a->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3658 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 3659 | |
| 3660 | if (atomic_read(&buffer_b->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3661 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 3662 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3663 | cpu_buffer_a = buffer_a->buffers[cpu]; |
| 3664 | cpu_buffer_b = buffer_b->buffers[cpu]; |
| 3665 | |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 3666 | if (atomic_read(&cpu_buffer_a->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3667 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 3668 | |
| 3669 | if (atomic_read(&cpu_buffer_b->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3670 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 3671 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3672 | /* |
| 3673 | * We can't do a synchronize_sched here because this |
| 3674 | * function can be called in atomic context. |
| 3675 | * Normally this will be called from the same CPU as cpu. |
| 3676 | * If not it's up to the caller to protect this. |
| 3677 | */ |
| 3678 | atomic_inc(&cpu_buffer_a->record_disabled); |
| 3679 | atomic_inc(&cpu_buffer_b->record_disabled); |
| 3680 | |
Steven Rostedt | 9827799 | 2009-09-02 10:56:15 -0400 | [diff] [blame] | 3681 | ret = -EBUSY; |
| 3682 | if (local_read(&cpu_buffer_a->committing)) |
| 3683 | goto out_dec; |
| 3684 | if (local_read(&cpu_buffer_b->committing)) |
| 3685 | goto out_dec; |
| 3686 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3687 | buffer_a->buffers[cpu] = cpu_buffer_b; |
| 3688 | buffer_b->buffers[cpu] = cpu_buffer_a; |
| 3689 | |
| 3690 | cpu_buffer_b->buffer = buffer_a; |
| 3691 | cpu_buffer_a->buffer = buffer_b; |
| 3692 | |
Steven Rostedt | 9827799 | 2009-09-02 10:56:15 -0400 | [diff] [blame] | 3693 | ret = 0; |
| 3694 | |
| 3695 | out_dec: |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3696 | atomic_dec(&cpu_buffer_a->record_disabled); |
| 3697 | atomic_dec(&cpu_buffer_b->record_disabled); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3698 | out: |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3699 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3700 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 3701 | EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu); |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 3702 | #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 3703 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3704 | /** |
| 3705 | * ring_buffer_alloc_read_page - allocate a page to read from buffer |
| 3706 | * @buffer: the buffer to allocate for. |
| 3707 | * |
| 3708 | * This function is used in conjunction with ring_buffer_read_page. |
| 3709 | * When reading a full page from the ring buffer, these functions |
| 3710 | * can be used to speed up the process. The calling function should |
| 3711 | * allocate a few pages first with this function. Then when it |
| 3712 | * needs to get pages from the ring buffer, it passes the result |
| 3713 | * of this function into ring_buffer_read_page, which will swap |
| 3714 | * the page that was allocated, with the read page of the buffer. |
| 3715 | * |
| 3716 | * Returns: |
| 3717 | * The page allocated, or NULL on error. |
| 3718 | */ |
| 3719 | void *ring_buffer_alloc_read_page(struct ring_buffer *buffer) |
| 3720 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 3721 | struct buffer_data_page *bpage; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3722 | unsigned long addr; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3723 | |
| 3724 | addr = __get_free_page(GFP_KERNEL); |
| 3725 | if (!addr) |
| 3726 | return NULL; |
| 3727 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 3728 | bpage = (void *)addr; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3729 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3730 | rb_init_page(bpage); |
| 3731 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 3732 | return bpage; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3733 | } |
Steven Rostedt | d6ce96d | 2009-05-05 01:15:24 -0400 | [diff] [blame] | 3734 | EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3735 | |
| 3736 | /** |
| 3737 | * ring_buffer_free_read_page - free an allocated read page |
| 3738 | * @buffer: the buffer the page was allocate for |
| 3739 | * @data: the page to free |
| 3740 | * |
| 3741 | * Free a page allocated from ring_buffer_alloc_read_page. |
| 3742 | */ |
| 3743 | void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data) |
| 3744 | { |
| 3745 | free_page((unsigned long)data); |
| 3746 | } |
Steven Rostedt | d6ce96d | 2009-05-05 01:15:24 -0400 | [diff] [blame] | 3747 | EXPORT_SYMBOL_GPL(ring_buffer_free_read_page); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3748 | |
| 3749 | /** |
| 3750 | * ring_buffer_read_page - extract a page from the ring buffer |
| 3751 | * @buffer: buffer to extract from |
| 3752 | * @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] | 3753 | * @len: amount to extract |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3754 | * @cpu: the cpu of the buffer to extract |
| 3755 | * @full: should the extraction only happen when the page is full. |
| 3756 | * |
| 3757 | * This function will pull out a page from the ring buffer and consume it. |
| 3758 | * @data_page must be the address of the variable that was returned |
| 3759 | * from ring_buffer_alloc_read_page. This is because the page might be used |
| 3760 | * to swap with a page in the ring buffer. |
| 3761 | * |
| 3762 | * for example: |
Lai Jiangshan | b85fa01 | 2009-02-09 14:21:14 +0800 | [diff] [blame] | 3763 | * rpage = ring_buffer_alloc_read_page(buffer); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3764 | * if (!rpage) |
| 3765 | * return error; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3766 | * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0); |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 3767 | * if (ret >= 0) |
| 3768 | * process_page(rpage, ret); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3769 | * |
| 3770 | * When @full is set, the function will not return true unless |
| 3771 | * the writer is off the reader page. |
| 3772 | * |
| 3773 | * Note: it is up to the calling functions to handle sleeps and wakeups. |
| 3774 | * The ring buffer can be used anywhere in the kernel and can not |
| 3775 | * blindly call wake_up. The layer that uses the ring buffer must be |
| 3776 | * responsible for that. |
| 3777 | * |
| 3778 | * Returns: |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 3779 | * >=0 if data has been transferred, returns the offset of consumed data. |
| 3780 | * <0 if no data has been transferred. |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3781 | */ |
| 3782 | int ring_buffer_read_page(struct ring_buffer *buffer, |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3783 | void **data_page, size_t len, int cpu, int full) |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3784 | { |
| 3785 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
| 3786 | struct ring_buffer_event *event; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 3787 | struct buffer_data_page *bpage; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3788 | struct buffer_page *reader; |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 3789 | unsigned long missed_events; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3790 | unsigned long flags; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3791 | unsigned int commit; |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 3792 | unsigned int read; |
Steven Rostedt | 4f3640f | 2009-03-03 23:52:42 -0500 | [diff] [blame] | 3793 | u64 save_timestamp; |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 3794 | int ret = -1; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3795 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3796 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 3797 | goto out; |
| 3798 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 3799 | /* |
| 3800 | * If len is not big enough to hold the page header, then |
| 3801 | * we can not copy anything. |
| 3802 | */ |
| 3803 | if (len <= BUF_PAGE_HDR_SIZE) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3804 | goto out; |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 3805 | |
| 3806 | len -= BUF_PAGE_HDR_SIZE; |
| 3807 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3808 | if (!data_page) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3809 | goto out; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3810 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 3811 | bpage = *data_page; |
| 3812 | if (!bpage) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3813 | goto out; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3814 | |
| 3815 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 3816 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3817 | reader = rb_get_reader_page(cpu_buffer); |
| 3818 | if (!reader) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3819 | goto out_unlock; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3820 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3821 | event = rb_reader_event(cpu_buffer); |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 3822 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3823 | read = reader->read; |
| 3824 | commit = rb_page_commit(reader); |
| 3825 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3826 | /* Check if any events were dropped */ |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 3827 | missed_events = cpu_buffer->lost_events; |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3828 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3829 | /* |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 3830 | * If this page has been partially read or |
| 3831 | * if len is not big enough to read the rest of the page or |
| 3832 | * a writer is still on the page, then |
| 3833 | * we must copy the data from the page to the buffer. |
| 3834 | * Otherwise, we can simply swap the page with the one passed in. |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3835 | */ |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 3836 | if (read || (len < (commit - read)) || |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3837 | cpu_buffer->reader_page == cpu_buffer->commit_page) { |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 3838 | struct buffer_data_page *rpage = cpu_buffer->reader_page->page; |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 3839 | unsigned int rpos = read; |
| 3840 | unsigned int pos = 0; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3841 | unsigned int size; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3842 | |
| 3843 | if (full) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3844 | goto out_unlock; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3845 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3846 | if (len > (commit - read)) |
| 3847 | len = (commit - read); |
| 3848 | |
| 3849 | size = rb_event_length(event); |
| 3850 | |
| 3851 | if (len < size) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3852 | goto out_unlock; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3853 | |
Steven Rostedt | 4f3640f | 2009-03-03 23:52:42 -0500 | [diff] [blame] | 3854 | /* save the current timestamp, since the user will need it */ |
| 3855 | save_timestamp = cpu_buffer->read_stamp; |
| 3856 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3857 | /* Need to copy one event at a time */ |
| 3858 | do { |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 3859 | memcpy(bpage->data + pos, rpage->data + rpos, size); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3860 | |
| 3861 | len -= size; |
| 3862 | |
| 3863 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 3864 | rpos = reader->read; |
| 3865 | pos += size; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3866 | |
| 3867 | event = rb_reader_event(cpu_buffer); |
| 3868 | size = rb_event_length(event); |
| 3869 | } while (len > size); |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 3870 | |
| 3871 | /* update bpage */ |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3872 | local_set(&bpage->commit, pos); |
Steven Rostedt | 4f3640f | 2009-03-03 23:52:42 -0500 | [diff] [blame] | 3873 | bpage->time_stamp = save_timestamp; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3874 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 3875 | /* we copied everything to the beginning */ |
| 3876 | read = 0; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3877 | } else { |
Steven Rostedt | afbab76 | 2009-05-01 19:40:05 -0400 | [diff] [blame] | 3878 | /* update the entry counter */ |
Steven Rostedt | 77ae365 | 2009-03-27 11:00:29 -0400 | [diff] [blame] | 3879 | cpu_buffer->read += rb_page_entries(reader); |
Steven Rostedt | afbab76 | 2009-05-01 19:40:05 -0400 | [diff] [blame] | 3880 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3881 | /* swap the pages */ |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 3882 | rb_init_page(bpage); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3883 | bpage = reader->page; |
| 3884 | reader->page = *data_page; |
| 3885 | local_set(&reader->write, 0); |
Steven Rostedt | 778c55d | 2009-05-01 18:44:45 -0400 | [diff] [blame] | 3886 | local_set(&reader->entries, 0); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 3887 | reader->read = 0; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 3888 | *data_page = bpage; |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 3889 | |
| 3890 | /* |
| 3891 | * Use the real_end for the data size, |
| 3892 | * This gives us a chance to store the lost events |
| 3893 | * on the page. |
| 3894 | */ |
| 3895 | if (reader->real_end) |
| 3896 | local_set(&bpage->commit, reader->real_end); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3897 | } |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 3898 | ret = read; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3899 | |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3900 | cpu_buffer->lost_events = 0; |
| 3901 | /* |
| 3902 | * Set a flag in the commit field if we lost events |
| 3903 | */ |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 3904 | if (missed_events) { |
| 3905 | commit = local_read(&bpage->commit); |
| 3906 | |
| 3907 | /* If there is room at the end of the page to save the |
| 3908 | * missed events, then record it there. |
| 3909 | */ |
| 3910 | if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) { |
| 3911 | memcpy(&bpage->data[commit], &missed_events, |
| 3912 | sizeof(missed_events)); |
| 3913 | local_add(RB_MISSED_STORED, &bpage->commit); |
| 3914 | } |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3915 | local_add(RB_MISSED_EVENTS, &bpage->commit); |
Steven Rostedt | ff0ff84 | 2010-03-31 22:11:42 -0400 | [diff] [blame] | 3916 | } |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 3917 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3918 | out_unlock: |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3919 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
| 3920 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3921 | out: |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3922 | return ret; |
| 3923 | } |
Steven Rostedt | d6ce96d | 2009-05-05 01:15:24 -0400 | [diff] [blame] | 3924 | EXPORT_SYMBOL_GPL(ring_buffer_read_page); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 3925 | |
Paul Mundt | 1155de4 | 2009-06-25 14:30:12 +0900 | [diff] [blame] | 3926 | #ifdef CONFIG_TRACING |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 3927 | static ssize_t |
| 3928 | rb_simple_read(struct file *filp, char __user *ubuf, |
| 3929 | size_t cnt, loff_t *ppos) |
| 3930 | { |
Hannes Eder | 5e39841 | 2009-02-10 19:44:34 +0100 | [diff] [blame] | 3931 | unsigned long *p = filp->private_data; |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 3932 | char buf[64]; |
| 3933 | int r; |
| 3934 | |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 3935 | if (test_bit(RB_BUFFERS_DISABLED_BIT, p)) |
| 3936 | r = sprintf(buf, "permanently disabled\n"); |
| 3937 | else |
| 3938 | r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p)); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 3939 | |
| 3940 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 3941 | } |
| 3942 | |
| 3943 | static ssize_t |
| 3944 | rb_simple_write(struct file *filp, const char __user *ubuf, |
| 3945 | size_t cnt, loff_t *ppos) |
| 3946 | { |
Hannes Eder | 5e39841 | 2009-02-10 19:44:34 +0100 | [diff] [blame] | 3947 | unsigned long *p = filp->private_data; |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 3948 | char buf[64]; |
Hannes Eder | 5e39841 | 2009-02-10 19:44:34 +0100 | [diff] [blame] | 3949 | unsigned long val; |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 3950 | int ret; |
| 3951 | |
| 3952 | if (cnt >= sizeof(buf)) |
| 3953 | return -EINVAL; |
| 3954 | |
| 3955 | if (copy_from_user(&buf, ubuf, cnt)) |
| 3956 | return -EFAULT; |
| 3957 | |
| 3958 | buf[cnt] = 0; |
| 3959 | |
| 3960 | ret = strict_strtoul(buf, 10, &val); |
| 3961 | if (ret < 0) |
| 3962 | return ret; |
| 3963 | |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 3964 | if (val) |
| 3965 | set_bit(RB_BUFFERS_ON_BIT, p); |
| 3966 | else |
| 3967 | clear_bit(RB_BUFFERS_ON_BIT, p); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 3968 | |
| 3969 | (*ppos)++; |
| 3970 | |
| 3971 | return cnt; |
| 3972 | } |
| 3973 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 3974 | static const struct file_operations rb_simple_fops = { |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 3975 | .open = tracing_open_generic, |
| 3976 | .read = rb_simple_read, |
| 3977 | .write = rb_simple_write, |
| 3978 | }; |
| 3979 | |
| 3980 | |
| 3981 | static __init int rb_init_debugfs(void) |
| 3982 | { |
| 3983 | struct dentry *d_tracer; |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 3984 | |
| 3985 | d_tracer = tracing_init_dentry(); |
| 3986 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 3987 | trace_create_file("tracing_on", 0644, d_tracer, |
| 3988 | &ring_buffer_flags, &rb_simple_fops); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 3989 | |
| 3990 | return 0; |
| 3991 | } |
| 3992 | |
| 3993 | fs_initcall(rb_init_debugfs); |
Paul Mundt | 1155de4 | 2009-06-25 14:30:12 +0900 | [diff] [blame] | 3994 | #endif |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3995 | |
Steven Rostedt | 59222ef | 2009-03-12 11:46:03 -0400 | [diff] [blame] | 3996 | #ifdef CONFIG_HOTPLUG_CPU |
Frederic Weisbecker | 09c9e84 | 2009-03-21 04:33:36 +0100 | [diff] [blame] | 3997 | static int rb_cpu_notify(struct notifier_block *self, |
| 3998 | unsigned long action, void *hcpu) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3999 | { |
| 4000 | struct ring_buffer *buffer = |
| 4001 | container_of(self, struct ring_buffer, cpu_notify); |
| 4002 | long cpu = (long)hcpu; |
| 4003 | |
| 4004 | switch (action) { |
| 4005 | case CPU_UP_PREPARE: |
| 4006 | case CPU_UP_PREPARE_FROZEN: |
Rusty Russell | 3f237a7 | 2009-06-12 21:15:30 +0930 | [diff] [blame] | 4007 | if (cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4008 | return NOTIFY_OK; |
| 4009 | |
| 4010 | buffer->buffers[cpu] = |
| 4011 | rb_allocate_cpu_buffer(buffer, cpu); |
| 4012 | if (!buffer->buffers[cpu]) { |
| 4013 | WARN(1, "failed to allocate ring buffer on CPU %ld\n", |
| 4014 | cpu); |
| 4015 | return NOTIFY_OK; |
| 4016 | } |
| 4017 | smp_wmb(); |
Rusty Russell | 3f237a7 | 2009-06-12 21:15:30 +0930 | [diff] [blame] | 4018 | cpumask_set_cpu(cpu, buffer->cpumask); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 4019 | break; |
| 4020 | case CPU_DOWN_PREPARE: |
| 4021 | case CPU_DOWN_PREPARE_FROZEN: |
| 4022 | /* |
| 4023 | * Do nothing. |
| 4024 | * If we were to free the buffer, then the user would |
| 4025 | * lose any trace that was in the buffer. |
| 4026 | */ |
| 4027 | break; |
| 4028 | default: |
| 4029 | break; |
| 4030 | } |
| 4031 | return NOTIFY_OK; |
| 4032 | } |
| 4033 | #endif |