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