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