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