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