| 1 | /* |
| 2 | * ring_buffer_frontend.c |
| 3 | * |
| 4 | * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; only |
| 9 | * version 2.1 of the License. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | * |
| 20 | * |
| 21 | * Ring buffer wait-free buffer synchronization. Producer-consumer and flight |
| 22 | * recorder (overwrite) modes. See thesis: |
| 23 | * |
| 24 | * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D. |
| 25 | * dissertation, Ecole Polytechnique de Montreal. |
| 26 | * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf |
| 27 | * |
| 28 | * - Algorithm presentation in Chapter 5: |
| 29 | * "Lockless Multi-Core High-Throughput Buffering". |
| 30 | * - Algorithm formal verification in Section 8.6: |
| 31 | * "Formal verification of LTTng" |
| 32 | * |
| 33 | * Author: |
| 34 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 35 | * |
| 36 | * Inspired from LTT and RelayFS: |
| 37 | * Karim Yaghmour <karim@opersys.com> |
| 38 | * Tom Zanussi <zanussi@us.ibm.com> |
| 39 | * Bob Wisniewski <bob@watson.ibm.com> |
| 40 | * And from K42 : |
| 41 | * Bob Wisniewski <bob@watson.ibm.com> |
| 42 | * |
| 43 | * Buffer reader semantic : |
| 44 | * |
| 45 | * - get_subbuf_size |
| 46 | * while buffer is not finalized and empty |
| 47 | * - get_subbuf |
| 48 | * - if return value != 0, continue |
| 49 | * - splice one subbuffer worth of data to a pipe |
| 50 | * - splice the data from pipe to disk/network |
| 51 | * - put_subbuf |
| 52 | */ |
| 53 | |
| 54 | #include <linux/delay.h> |
| 55 | #include <linux/module.h> |
| 56 | #include <linux/percpu.h> |
| 57 | |
| 58 | #include <wrapper/ringbuffer/config.h> |
| 59 | #include <wrapper/ringbuffer/backend.h> |
| 60 | #include <wrapper/ringbuffer/frontend.h> |
| 61 | #include <wrapper/ringbuffer/iterator.h> |
| 62 | #include <wrapper/ringbuffer/nohz.h> |
| 63 | #include <wrapper/atomic.h> |
| 64 | #include <wrapper/kref.h> |
| 65 | #include <wrapper/percpu-defs.h> |
| 66 | #include <wrapper/timer.h> |
| 67 | |
| 68 | /* |
| 69 | * Internal structure representing offsets to use at a sub-buffer switch. |
| 70 | */ |
| 71 | struct switch_offsets { |
| 72 | unsigned long begin, end, old; |
| 73 | size_t pre_header_padding, size; |
| 74 | unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1, |
| 75 | switch_old_end:1; |
| 76 | }; |
| 77 | |
| 78 | #ifdef CONFIG_NO_HZ |
| 79 | enum tick_nohz_val { |
| 80 | TICK_NOHZ_STOP, |
| 81 | TICK_NOHZ_FLUSH, |
| 82 | TICK_NOHZ_RESTART, |
| 83 | }; |
| 84 | |
| 85 | static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier); |
| 86 | #endif /* CONFIG_NO_HZ */ |
| 87 | |
| 88 | static DEFINE_PER_CPU(spinlock_t, ring_buffer_nohz_lock); |
| 89 | |
| 90 | DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting); |
| 91 | EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting); |
| 92 | |
| 93 | static |
| 94 | void lib_ring_buffer_print_errors(struct channel *chan, |
| 95 | struct lib_ring_buffer *buf, int cpu); |
| 96 | static |
| 97 | void _lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf, |
| 98 | enum switch_mode mode); |
| 99 | |
| 100 | static |
| 101 | int lib_ring_buffer_poll_deliver(const struct lib_ring_buffer_config *config, |
| 102 | struct lib_ring_buffer *buf, |
| 103 | struct channel *chan) |
| 104 | { |
| 105 | unsigned long consumed_old, consumed_idx, commit_count, write_offset; |
| 106 | |
| 107 | consumed_old = atomic_long_read(&buf->consumed); |
| 108 | consumed_idx = subbuf_index(consumed_old, chan); |
| 109 | commit_count = v_read(config, &buf->commit_cold[consumed_idx].cc_sb); |
| 110 | /* |
| 111 | * No memory barrier here, since we are only interested |
| 112 | * in a statistically correct polling result. The next poll will |
| 113 | * get the data is we are racing. The mb() that ensures correct |
| 114 | * memory order is in get_subbuf. |
| 115 | */ |
| 116 | write_offset = v_read(config, &buf->offset); |
| 117 | |
| 118 | /* |
| 119 | * Check that the subbuffer we are trying to consume has been |
| 120 | * already fully committed. |
| 121 | */ |
| 122 | |
| 123 | if (((commit_count - chan->backend.subbuf_size) |
| 124 | & chan->commit_count_mask) |
| 125 | - (buf_trunc(consumed_old, chan) |
| 126 | >> chan->backend.num_subbuf_order) |
| 127 | != 0) |
| 128 | return 0; |
| 129 | |
| 130 | /* |
| 131 | * Check that we are not about to read the same subbuffer in |
| 132 | * which the writer head is. |
| 133 | */ |
| 134 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_old, chan) |
| 135 | == 0) |
| 136 | return 0; |
| 137 | |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Must be called under cpu hotplug protection. |
| 143 | */ |
| 144 | void lib_ring_buffer_free(struct lib_ring_buffer *buf) |
| 145 | { |
| 146 | struct channel *chan = buf->backend.chan; |
| 147 | |
| 148 | lib_ring_buffer_print_errors(chan, buf, buf->backend.cpu); |
| 149 | kfree(buf->commit_hot); |
| 150 | kfree(buf->commit_cold); |
| 151 | |
| 152 | lib_ring_buffer_backend_free(&buf->backend); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * lib_ring_buffer_reset - Reset ring buffer to initial values. |
| 157 | * @buf: Ring buffer. |
| 158 | * |
| 159 | * Effectively empty the ring buffer. Should be called when the buffer is not |
| 160 | * used for writing. The ring buffer can be opened for reading, but the reader |
| 161 | * should not be using the iterator concurrently with reset. The previous |
| 162 | * current iterator record is reset. |
| 163 | */ |
| 164 | void lib_ring_buffer_reset(struct lib_ring_buffer *buf) |
| 165 | { |
| 166 | struct channel *chan = buf->backend.chan; |
| 167 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 168 | unsigned int i; |
| 169 | |
| 170 | /* |
| 171 | * Reset iterator first. It will put the subbuffer if it currently holds |
| 172 | * it. |
| 173 | */ |
| 174 | lib_ring_buffer_iterator_reset(buf); |
| 175 | v_set(config, &buf->offset, 0); |
| 176 | for (i = 0; i < chan->backend.num_subbuf; i++) { |
| 177 | v_set(config, &buf->commit_hot[i].cc, 0); |
| 178 | v_set(config, &buf->commit_hot[i].seq, 0); |
| 179 | v_set(config, &buf->commit_cold[i].cc_sb, 0); |
| 180 | } |
| 181 | atomic_long_set(&buf->consumed, 0); |
| 182 | atomic_set(&buf->record_disabled, 0); |
| 183 | v_set(config, &buf->last_tsc, 0); |
| 184 | lib_ring_buffer_backend_reset(&buf->backend); |
| 185 | /* Don't reset number of active readers */ |
| 186 | v_set(config, &buf->records_lost_full, 0); |
| 187 | v_set(config, &buf->records_lost_wrap, 0); |
| 188 | v_set(config, &buf->records_lost_big, 0); |
| 189 | v_set(config, &buf->records_count, 0); |
| 190 | v_set(config, &buf->records_overrun, 0); |
| 191 | buf->finalized = 0; |
| 192 | } |
| 193 | EXPORT_SYMBOL_GPL(lib_ring_buffer_reset); |
| 194 | |
| 195 | /** |
| 196 | * channel_reset - Reset channel to initial values. |
| 197 | * @chan: Channel. |
| 198 | * |
| 199 | * Effectively empty the channel. Should be called when the channel is not used |
| 200 | * for writing. The channel can be opened for reading, but the reader should not |
| 201 | * be using the iterator concurrently with reset. The previous current iterator |
| 202 | * record is reset. |
| 203 | */ |
| 204 | void channel_reset(struct channel *chan) |
| 205 | { |
| 206 | /* |
| 207 | * Reset iterators first. Will put the subbuffer if held for reading. |
| 208 | */ |
| 209 | channel_iterator_reset(chan); |
| 210 | atomic_set(&chan->record_disabled, 0); |
| 211 | /* Don't reset commit_count_mask, still valid */ |
| 212 | channel_backend_reset(&chan->backend); |
| 213 | /* Don't reset switch/read timer interval */ |
| 214 | /* Don't reset notifiers and notifier enable bits */ |
| 215 | /* Don't reset reader reference count */ |
| 216 | } |
| 217 | EXPORT_SYMBOL_GPL(channel_reset); |
| 218 | |
| 219 | /* |
| 220 | * Must be called under cpu hotplug protection. |
| 221 | */ |
| 222 | int lib_ring_buffer_create(struct lib_ring_buffer *buf, |
| 223 | struct channel_backend *chanb, int cpu) |
| 224 | { |
| 225 | const struct lib_ring_buffer_config *config = &chanb->config; |
| 226 | struct channel *chan = container_of(chanb, struct channel, backend); |
| 227 | void *priv = chanb->priv; |
| 228 | size_t subbuf_header_size; |
| 229 | u64 tsc; |
| 230 | int ret; |
| 231 | |
| 232 | /* Test for cpu hotplug */ |
| 233 | if (buf->backend.allocated) |
| 234 | return 0; |
| 235 | |
| 236 | /* |
| 237 | * Paranoia: per cpu dynamic allocation is not officially documented as |
| 238 | * zeroing the memory, so let's do it here too, just in case. |
| 239 | */ |
| 240 | memset(buf, 0, sizeof(*buf)); |
| 241 | |
| 242 | ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend, cpu); |
| 243 | if (ret) |
| 244 | return ret; |
| 245 | |
| 246 | buf->commit_hot = |
| 247 | kzalloc_node(ALIGN(sizeof(*buf->commit_hot) |
| 248 | * chan->backend.num_subbuf, |
| 249 | 1 << INTERNODE_CACHE_SHIFT), |
| 250 | GFP_KERNEL | __GFP_NOWARN, |
| 251 | cpu_to_node(max(cpu, 0))); |
| 252 | if (!buf->commit_hot) { |
| 253 | ret = -ENOMEM; |
| 254 | goto free_chanbuf; |
| 255 | } |
| 256 | |
| 257 | buf->commit_cold = |
| 258 | kzalloc_node(ALIGN(sizeof(*buf->commit_cold) |
| 259 | * chan->backend.num_subbuf, |
| 260 | 1 << INTERNODE_CACHE_SHIFT), |
| 261 | GFP_KERNEL | __GFP_NOWARN, |
| 262 | cpu_to_node(max(cpu, 0))); |
| 263 | if (!buf->commit_cold) { |
| 264 | ret = -ENOMEM; |
| 265 | goto free_commit; |
| 266 | } |
| 267 | |
| 268 | init_waitqueue_head(&buf->read_wait); |
| 269 | init_waitqueue_head(&buf->write_wait); |
| 270 | raw_spin_lock_init(&buf->raw_tick_nohz_spinlock); |
| 271 | |
| 272 | /* |
| 273 | * Write the subbuffer header for first subbuffer so we know the total |
| 274 | * duration of data gathering. |
| 275 | */ |
| 276 | subbuf_header_size = config->cb.subbuffer_header_size(); |
| 277 | v_set(config, &buf->offset, subbuf_header_size); |
| 278 | subbuffer_id_clear_noref(config, &buf->backend.buf_wsb[0].id); |
| 279 | tsc = config->cb.ring_buffer_clock_read(buf->backend.chan); |
| 280 | config->cb.buffer_begin(buf, tsc, 0); |
| 281 | v_add(config, subbuf_header_size, &buf->commit_hot[0].cc); |
| 282 | |
| 283 | if (config->cb.buffer_create) { |
| 284 | ret = config->cb.buffer_create(buf, priv, cpu, chanb->name); |
| 285 | if (ret) |
| 286 | goto free_init; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Ensure the buffer is ready before setting it to allocated and setting |
| 291 | * the cpumask. |
| 292 | * Used for cpu hotplug vs cpumask iteration. |
| 293 | */ |
| 294 | smp_wmb(); |
| 295 | buf->backend.allocated = 1; |
| 296 | |
| 297 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 298 | CHAN_WARN_ON(chan, cpumask_test_cpu(cpu, |
| 299 | chan->backend.cpumask)); |
| 300 | cpumask_set_cpu(cpu, chan->backend.cpumask); |
| 301 | } |
| 302 | |
| 303 | return 0; |
| 304 | |
| 305 | /* Error handling */ |
| 306 | free_init: |
| 307 | kfree(buf->commit_cold); |
| 308 | free_commit: |
| 309 | kfree(buf->commit_hot); |
| 310 | free_chanbuf: |
| 311 | lib_ring_buffer_backend_free(&buf->backend); |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | static void switch_buffer_timer(unsigned long data) |
| 316 | { |
| 317 | struct lib_ring_buffer *buf = (struct lib_ring_buffer *)data; |
| 318 | struct channel *chan = buf->backend.chan; |
| 319 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 320 | |
| 321 | /* |
| 322 | * Only flush buffers periodically if readers are active. |
| 323 | */ |
| 324 | if (atomic_long_read(&buf->active_readers)) |
| 325 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 326 | |
| 327 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 328 | lttng_mod_timer_pinned(&buf->switch_timer, |
| 329 | jiffies + chan->switch_timer_interval); |
| 330 | else |
| 331 | mod_timer(&buf->switch_timer, |
| 332 | jiffies + chan->switch_timer_interval); |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Called with ring_buffer_nohz_lock held for per-cpu buffers. |
| 337 | */ |
| 338 | static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer *buf) |
| 339 | { |
| 340 | struct channel *chan = buf->backend.chan; |
| 341 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 342 | |
| 343 | if (!chan->switch_timer_interval || buf->switch_timer_enabled) |
| 344 | return; |
| 345 | |
| 346 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 347 | lttng_init_timer_pinned(&buf->switch_timer); |
| 348 | else |
| 349 | init_timer(&buf->switch_timer); |
| 350 | |
| 351 | buf->switch_timer.function = switch_buffer_timer; |
| 352 | buf->switch_timer.expires = jiffies + chan->switch_timer_interval; |
| 353 | buf->switch_timer.data = (unsigned long)buf; |
| 354 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 355 | add_timer_on(&buf->switch_timer, buf->backend.cpu); |
| 356 | else |
| 357 | add_timer(&buf->switch_timer); |
| 358 | buf->switch_timer_enabled = 1; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Called with ring_buffer_nohz_lock held for per-cpu buffers. |
| 363 | */ |
| 364 | static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer *buf) |
| 365 | { |
| 366 | struct channel *chan = buf->backend.chan; |
| 367 | |
| 368 | if (!chan->switch_timer_interval || !buf->switch_timer_enabled) |
| 369 | return; |
| 370 | |
| 371 | del_timer_sync(&buf->switch_timer); |
| 372 | buf->switch_timer_enabled = 0; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Polling timer to check the channels for data. |
| 377 | */ |
| 378 | static void read_buffer_timer(unsigned long data) |
| 379 | { |
| 380 | struct lib_ring_buffer *buf = (struct lib_ring_buffer *)data; |
| 381 | struct channel *chan = buf->backend.chan; |
| 382 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 383 | |
| 384 | CHAN_WARN_ON(chan, !buf->backend.allocated); |
| 385 | |
| 386 | if (atomic_long_read(&buf->active_readers) |
| 387 | && lib_ring_buffer_poll_deliver(config, buf, chan)) { |
| 388 | wake_up_interruptible(&buf->read_wait); |
| 389 | wake_up_interruptible(&chan->read_wait); |
| 390 | } |
| 391 | |
| 392 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 393 | lttng_mod_timer_pinned(&buf->read_timer, |
| 394 | jiffies + chan->read_timer_interval); |
| 395 | else |
| 396 | mod_timer(&buf->read_timer, |
| 397 | jiffies + chan->read_timer_interval); |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Called with ring_buffer_nohz_lock held for per-cpu buffers. |
| 402 | */ |
| 403 | static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer *buf) |
| 404 | { |
| 405 | struct channel *chan = buf->backend.chan; |
| 406 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 407 | |
| 408 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER |
| 409 | || !chan->read_timer_interval |
| 410 | || buf->read_timer_enabled) |
| 411 | return; |
| 412 | |
| 413 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 414 | lttng_init_timer_pinned(&buf->read_timer); |
| 415 | else |
| 416 | init_timer(&buf->read_timer); |
| 417 | |
| 418 | buf->read_timer.function = read_buffer_timer; |
| 419 | buf->read_timer.expires = jiffies + chan->read_timer_interval; |
| 420 | buf->read_timer.data = (unsigned long)buf; |
| 421 | |
| 422 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 423 | add_timer_on(&buf->read_timer, buf->backend.cpu); |
| 424 | else |
| 425 | add_timer(&buf->read_timer); |
| 426 | buf->read_timer_enabled = 1; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Called with ring_buffer_nohz_lock held for per-cpu buffers. |
| 431 | */ |
| 432 | static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer *buf) |
| 433 | { |
| 434 | struct channel *chan = buf->backend.chan; |
| 435 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 436 | |
| 437 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER |
| 438 | || !chan->read_timer_interval |
| 439 | || !buf->read_timer_enabled) |
| 440 | return; |
| 441 | |
| 442 | del_timer_sync(&buf->read_timer); |
| 443 | /* |
| 444 | * do one more check to catch data that has been written in the last |
| 445 | * timer period. |
| 446 | */ |
| 447 | if (lib_ring_buffer_poll_deliver(config, buf, chan)) { |
| 448 | wake_up_interruptible(&buf->read_wait); |
| 449 | wake_up_interruptible(&chan->read_wait); |
| 450 | } |
| 451 | buf->read_timer_enabled = 0; |
| 452 | } |
| 453 | |
| 454 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) |
| 455 | |
| 456 | enum cpuhp_state lttng_rb_hp_prepare; |
| 457 | enum cpuhp_state lttng_rb_hp_online; |
| 458 | |
| 459 | void lttng_rb_set_hp_prepare(enum cpuhp_state val) |
| 460 | { |
| 461 | lttng_rb_hp_prepare = val; |
| 462 | } |
| 463 | EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare); |
| 464 | |
| 465 | void lttng_rb_set_hp_online(enum cpuhp_state val) |
| 466 | { |
| 467 | lttng_rb_hp_online = val; |
| 468 | } |
| 469 | EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online); |
| 470 | |
| 471 | int lttng_cpuhp_rb_frontend_dead(unsigned int cpu, |
| 472 | struct lttng_cpuhp_node *node) |
| 473 | { |
| 474 | struct channel *chan = container_of(node, struct channel, |
| 475 | cpuhp_prepare); |
| 476 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu); |
| 477 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 478 | |
| 479 | CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL); |
| 480 | |
| 481 | /* |
| 482 | * Performing a buffer switch on a remote CPU. Performed by |
| 483 | * the CPU responsible for doing the hotunplug after the target |
| 484 | * CPU stopped running completely. Ensures that all data |
| 485 | * from that remote CPU is flushed. |
| 486 | */ |
| 487 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 488 | return 0; |
| 489 | } |
| 490 | EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead); |
| 491 | |
| 492 | int lttng_cpuhp_rb_frontend_online(unsigned int cpu, |
| 493 | struct lttng_cpuhp_node *node) |
| 494 | { |
| 495 | struct channel *chan = container_of(node, struct channel, |
| 496 | cpuhp_online); |
| 497 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu); |
| 498 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 499 | |
| 500 | CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL); |
| 501 | |
| 502 | wake_up_interruptible(&chan->hp_wait); |
| 503 | lib_ring_buffer_start_switch_timer(buf); |
| 504 | lib_ring_buffer_start_read_timer(buf); |
| 505 | return 0; |
| 506 | } |
| 507 | EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online); |
| 508 | |
| 509 | int lttng_cpuhp_rb_frontend_offline(unsigned int cpu, |
| 510 | struct lttng_cpuhp_node *node) |
| 511 | { |
| 512 | struct channel *chan = container_of(node, struct channel, |
| 513 | cpuhp_online); |
| 514 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu); |
| 515 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 516 | |
| 517 | CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL); |
| 518 | |
| 519 | lib_ring_buffer_stop_switch_timer(buf); |
| 520 | lib_ring_buffer_stop_read_timer(buf); |
| 521 | return 0; |
| 522 | } |
| 523 | EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline); |
| 524 | |
| 525 | #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
| 526 | |
| 527 | #ifdef CONFIG_HOTPLUG_CPU |
| 528 | |
| 529 | /** |
| 530 | * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback |
| 531 | * @nb: notifier block |
| 532 | * @action: hotplug action to take |
| 533 | * @hcpu: CPU number |
| 534 | * |
| 535 | * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD) |
| 536 | */ |
| 537 | static |
| 538 | int lib_ring_buffer_cpu_hp_callback(struct notifier_block *nb, |
| 539 | unsigned long action, |
| 540 | void *hcpu) |
| 541 | { |
| 542 | unsigned int cpu = (unsigned long)hcpu; |
| 543 | struct channel *chan = container_of(nb, struct channel, |
| 544 | cpu_hp_notifier); |
| 545 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu); |
| 546 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 547 | |
| 548 | if (!chan->cpu_hp_enable) |
| 549 | return NOTIFY_DONE; |
| 550 | |
| 551 | CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL); |
| 552 | |
| 553 | switch (action) { |
| 554 | case CPU_DOWN_FAILED: |
| 555 | case CPU_DOWN_FAILED_FROZEN: |
| 556 | case CPU_ONLINE: |
| 557 | case CPU_ONLINE_FROZEN: |
| 558 | wake_up_interruptible(&chan->hp_wait); |
| 559 | lib_ring_buffer_start_switch_timer(buf); |
| 560 | lib_ring_buffer_start_read_timer(buf); |
| 561 | return NOTIFY_OK; |
| 562 | |
| 563 | case CPU_DOWN_PREPARE: |
| 564 | case CPU_DOWN_PREPARE_FROZEN: |
| 565 | lib_ring_buffer_stop_switch_timer(buf); |
| 566 | lib_ring_buffer_stop_read_timer(buf); |
| 567 | return NOTIFY_OK; |
| 568 | |
| 569 | case CPU_DEAD: |
| 570 | case CPU_DEAD_FROZEN: |
| 571 | /* |
| 572 | * Performing a buffer switch on a remote CPU. Performed by |
| 573 | * the CPU responsible for doing the hotunplug after the target |
| 574 | * CPU stopped running completely. Ensures that all data |
| 575 | * from that remote CPU is flushed. |
| 576 | */ |
| 577 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 578 | return NOTIFY_OK; |
| 579 | |
| 580 | default: |
| 581 | return NOTIFY_DONE; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | #endif |
| 586 | |
| 587 | #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
| 588 | |
| 589 | #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) |
| 590 | /* |
| 591 | * For per-cpu buffers, call the reader wakeups before switching the buffer, so |
| 592 | * that wake-up-tracing generated events are flushed before going idle (in |
| 593 | * tick_nohz). We test if the spinlock is locked to deal with the race where |
| 594 | * readers try to sample the ring buffer before we perform the switch. We let |
| 595 | * the readers retry in that case. If there is data in the buffer, the wake up |
| 596 | * is going to forbid the CPU running the reader thread from going idle. |
| 597 | */ |
| 598 | static int notrace ring_buffer_tick_nohz_callback(struct notifier_block *nb, |
| 599 | unsigned long val, |
| 600 | void *data) |
| 601 | { |
| 602 | struct channel *chan = container_of(nb, struct channel, |
| 603 | tick_nohz_notifier); |
| 604 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 605 | struct lib_ring_buffer *buf; |
| 606 | int cpu = smp_processor_id(); |
| 607 | |
| 608 | if (config->alloc != RING_BUFFER_ALLOC_PER_CPU) { |
| 609 | /* |
| 610 | * We don't support keeping the system idle with global buffers |
| 611 | * and streaming active. In order to do so, we would need to |
| 612 | * sample a non-nohz-cpumask racelessly with the nohz updates |
| 613 | * without adding synchronization overhead to nohz. Leave this |
| 614 | * use-case out for now. |
| 615 | */ |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | buf = channel_get_ring_buffer(config, chan, cpu); |
| 620 | switch (val) { |
| 621 | case TICK_NOHZ_FLUSH: |
| 622 | raw_spin_lock(&buf->raw_tick_nohz_spinlock); |
| 623 | if (config->wakeup == RING_BUFFER_WAKEUP_BY_TIMER |
| 624 | && chan->read_timer_interval |
| 625 | && atomic_long_read(&buf->active_readers) |
| 626 | && (lib_ring_buffer_poll_deliver(config, buf, chan) |
| 627 | || lib_ring_buffer_pending_data(config, buf, chan))) { |
| 628 | wake_up_interruptible(&buf->read_wait); |
| 629 | wake_up_interruptible(&chan->read_wait); |
| 630 | } |
| 631 | if (chan->switch_timer_interval) |
| 632 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 633 | raw_spin_unlock(&buf->raw_tick_nohz_spinlock); |
| 634 | break; |
| 635 | case TICK_NOHZ_STOP: |
| 636 | spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock)); |
| 637 | lib_ring_buffer_stop_switch_timer(buf); |
| 638 | lib_ring_buffer_stop_read_timer(buf); |
| 639 | spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock)); |
| 640 | break; |
| 641 | case TICK_NOHZ_RESTART: |
| 642 | spin_lock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock)); |
| 643 | lib_ring_buffer_start_read_timer(buf); |
| 644 | lib_ring_buffer_start_switch_timer(buf); |
| 645 | spin_unlock(lttng_this_cpu_ptr(&ring_buffer_nohz_lock)); |
| 646 | break; |
| 647 | } |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | void notrace lib_ring_buffer_tick_nohz_flush(void) |
| 653 | { |
| 654 | atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_FLUSH, |
| 655 | NULL); |
| 656 | } |
| 657 | |
| 658 | void notrace lib_ring_buffer_tick_nohz_stop(void) |
| 659 | { |
| 660 | atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_STOP, |
| 661 | NULL); |
| 662 | } |
| 663 | |
| 664 | void notrace lib_ring_buffer_tick_nohz_restart(void) |
| 665 | { |
| 666 | atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_RESTART, |
| 667 | NULL); |
| 668 | } |
| 669 | #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */ |
| 670 | |
| 671 | /* |
| 672 | * Holds CPU hotplug. |
| 673 | */ |
| 674 | static void channel_unregister_notifiers(struct channel *chan) |
| 675 | { |
| 676 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 677 | |
| 678 | channel_iterator_unregister_notifiers(chan); |
| 679 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 680 | #ifdef CONFIG_NO_HZ |
| 681 | /* |
| 682 | * Remove the nohz notifier first, so we are certain we stop |
| 683 | * the timers. |
| 684 | */ |
| 685 | atomic_notifier_chain_unregister(&tick_nohz_notifier, |
| 686 | &chan->tick_nohz_notifier); |
| 687 | /* |
| 688 | * ring_buffer_nohz_lock will not be needed below, because |
| 689 | * we just removed the notifiers, which were the only source of |
| 690 | * concurrency. |
| 691 | */ |
| 692 | #endif /* CONFIG_NO_HZ */ |
| 693 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) |
| 694 | { |
| 695 | int ret; |
| 696 | |
| 697 | ret = cpuhp_state_remove_instance(lttng_rb_hp_online, |
| 698 | &chan->cpuhp_online.node); |
| 699 | WARN_ON(ret); |
| 700 | ret = cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare, |
| 701 | &chan->cpuhp_prepare.node); |
| 702 | WARN_ON(ret); |
| 703 | } |
| 704 | #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
| 705 | { |
| 706 | int cpu; |
| 707 | |
| 708 | #ifdef CONFIG_HOTPLUG_CPU |
| 709 | get_online_cpus(); |
| 710 | chan->cpu_hp_enable = 0; |
| 711 | for_each_online_cpu(cpu) { |
| 712 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, |
| 713 | cpu); |
| 714 | lib_ring_buffer_stop_switch_timer(buf); |
| 715 | lib_ring_buffer_stop_read_timer(buf); |
| 716 | } |
| 717 | put_online_cpus(); |
| 718 | unregister_cpu_notifier(&chan->cpu_hp_notifier); |
| 719 | #else |
| 720 | for_each_possible_cpu(cpu) { |
| 721 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, |
| 722 | cpu); |
| 723 | lib_ring_buffer_stop_switch_timer(buf); |
| 724 | lib_ring_buffer_stop_read_timer(buf); |
| 725 | } |
| 726 | #endif |
| 727 | } |
| 728 | #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
| 729 | } else { |
| 730 | struct lib_ring_buffer *buf = chan->backend.buf; |
| 731 | |
| 732 | lib_ring_buffer_stop_switch_timer(buf); |
| 733 | lib_ring_buffer_stop_read_timer(buf); |
| 734 | } |
| 735 | channel_backend_unregister_notifiers(&chan->backend); |
| 736 | } |
| 737 | |
| 738 | static void lib_ring_buffer_set_quiescent(struct lib_ring_buffer *buf) |
| 739 | { |
| 740 | if (!buf->quiescent) { |
| 741 | buf->quiescent = true; |
| 742 | _lib_ring_buffer_switch_remote(buf, SWITCH_FLUSH); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | static void lib_ring_buffer_clear_quiescent(struct lib_ring_buffer *buf) |
| 747 | { |
| 748 | buf->quiescent = false; |
| 749 | } |
| 750 | |
| 751 | void lib_ring_buffer_set_quiescent_channel(struct channel *chan) |
| 752 | { |
| 753 | int cpu; |
| 754 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 755 | |
| 756 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 757 | get_online_cpus(); |
| 758 | for_each_channel_cpu(cpu, chan) { |
| 759 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, |
| 760 | cpu); |
| 761 | |
| 762 | lib_ring_buffer_set_quiescent(buf); |
| 763 | } |
| 764 | put_online_cpus(); |
| 765 | } else { |
| 766 | struct lib_ring_buffer *buf = chan->backend.buf; |
| 767 | |
| 768 | lib_ring_buffer_set_quiescent(buf); |
| 769 | } |
| 770 | } |
| 771 | EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel); |
| 772 | |
| 773 | void lib_ring_buffer_clear_quiescent_channel(struct channel *chan) |
| 774 | { |
| 775 | int cpu; |
| 776 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 777 | |
| 778 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 779 | get_online_cpus(); |
| 780 | for_each_channel_cpu(cpu, chan) { |
| 781 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, |
| 782 | cpu); |
| 783 | |
| 784 | lib_ring_buffer_clear_quiescent(buf); |
| 785 | } |
| 786 | put_online_cpus(); |
| 787 | } else { |
| 788 | struct lib_ring_buffer *buf = chan->backend.buf; |
| 789 | |
| 790 | lib_ring_buffer_clear_quiescent(buf); |
| 791 | } |
| 792 | } |
| 793 | EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel); |
| 794 | |
| 795 | static void channel_free(struct channel *chan) |
| 796 | { |
| 797 | if (chan->backend.release_priv_ops) { |
| 798 | chan->backend.release_priv_ops(chan->backend.priv_ops); |
| 799 | } |
| 800 | channel_iterator_free(chan); |
| 801 | channel_backend_free(&chan->backend); |
| 802 | kfree(chan); |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * channel_create - Create channel. |
| 807 | * @config: ring buffer instance configuration |
| 808 | * @name: name of the channel |
| 809 | * @priv: ring buffer client private data |
| 810 | * @buf_addr: pointer the the beginning of the preallocated buffer contiguous |
| 811 | * address mapping. It is used only by RING_BUFFER_STATIC |
| 812 | * configuration. It can be set to NULL for other backends. |
| 813 | * @subbuf_size: subbuffer size |
| 814 | * @num_subbuf: number of subbuffers |
| 815 | * @switch_timer_interval: Time interval (in us) to fill sub-buffers with |
| 816 | * padding to let readers get those sub-buffers. |
| 817 | * Used for live streaming. |
| 818 | * @read_timer_interval: Time interval (in us) to wake up pending readers. |
| 819 | * |
| 820 | * Holds cpu hotplug. |
| 821 | * Returns NULL on failure. |
| 822 | */ |
| 823 | struct channel *channel_create(const struct lib_ring_buffer_config *config, |
| 824 | const char *name, void *priv, void *buf_addr, |
| 825 | size_t subbuf_size, |
| 826 | size_t num_subbuf, unsigned int switch_timer_interval, |
| 827 | unsigned int read_timer_interval) |
| 828 | { |
| 829 | int ret; |
| 830 | struct channel *chan; |
| 831 | |
| 832 | if (lib_ring_buffer_check_config(config, switch_timer_interval, |
| 833 | read_timer_interval)) |
| 834 | return NULL; |
| 835 | |
| 836 | chan = kzalloc(sizeof(struct channel), GFP_KERNEL); |
| 837 | if (!chan) |
| 838 | return NULL; |
| 839 | |
| 840 | ret = channel_backend_init(&chan->backend, name, config, priv, |
| 841 | subbuf_size, num_subbuf); |
| 842 | if (ret) |
| 843 | goto error; |
| 844 | |
| 845 | ret = channel_iterator_init(chan); |
| 846 | if (ret) |
| 847 | goto error_free_backend; |
| 848 | |
| 849 | chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order); |
| 850 | chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval); |
| 851 | chan->read_timer_interval = usecs_to_jiffies(read_timer_interval); |
| 852 | kref_init(&chan->ref); |
| 853 | init_waitqueue_head(&chan->read_wait); |
| 854 | init_waitqueue_head(&chan->hp_wait); |
| 855 | |
| 856 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 857 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) |
| 858 | chan->cpuhp_prepare.component = LTTNG_RING_BUFFER_FRONTEND; |
| 859 | ret = cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare, |
| 860 | &chan->cpuhp_prepare.node); |
| 861 | if (ret) |
| 862 | goto cpuhp_prepare_error; |
| 863 | |
| 864 | chan->cpuhp_online.component = LTTNG_RING_BUFFER_FRONTEND; |
| 865 | ret = cpuhp_state_add_instance(lttng_rb_hp_online, |
| 866 | &chan->cpuhp_online.node); |
| 867 | if (ret) |
| 868 | goto cpuhp_online_error; |
| 869 | #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
| 870 | { |
| 871 | int cpu; |
| 872 | /* |
| 873 | * In case of non-hotplug cpu, if the ring-buffer is allocated |
| 874 | * in early initcall, it will not be notified of secondary cpus. |
| 875 | * In that off case, we need to allocate for all possible cpus. |
| 876 | */ |
| 877 | #ifdef CONFIG_HOTPLUG_CPU |
| 878 | chan->cpu_hp_notifier.notifier_call = |
| 879 | lib_ring_buffer_cpu_hp_callback; |
| 880 | chan->cpu_hp_notifier.priority = 6; |
| 881 | register_cpu_notifier(&chan->cpu_hp_notifier); |
| 882 | |
| 883 | get_online_cpus(); |
| 884 | for_each_online_cpu(cpu) { |
| 885 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, |
| 886 | cpu); |
| 887 | spin_lock(&per_cpu(ring_buffer_nohz_lock, cpu)); |
| 888 | lib_ring_buffer_start_switch_timer(buf); |
| 889 | lib_ring_buffer_start_read_timer(buf); |
| 890 | spin_unlock(&per_cpu(ring_buffer_nohz_lock, cpu)); |
| 891 | } |
| 892 | chan->cpu_hp_enable = 1; |
| 893 | put_online_cpus(); |
| 894 | #else |
| 895 | for_each_possible_cpu(cpu) { |
| 896 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, |
| 897 | cpu); |
| 898 | spin_lock(&per_cpu(ring_buffer_nohz_lock, cpu)); |
| 899 | lib_ring_buffer_start_switch_timer(buf); |
| 900 | lib_ring_buffer_start_read_timer(buf); |
| 901 | spin_unlock(&per_cpu(ring_buffer_nohz_lock, cpu)); |
| 902 | } |
| 903 | #endif |
| 904 | } |
| 905 | #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
| 906 | |
| 907 | #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) |
| 908 | /* Only benefit from NO_HZ idle with per-cpu buffers for now. */ |
| 909 | chan->tick_nohz_notifier.notifier_call = |
| 910 | ring_buffer_tick_nohz_callback; |
| 911 | chan->tick_nohz_notifier.priority = ~0U; |
| 912 | atomic_notifier_chain_register(&tick_nohz_notifier, |
| 913 | &chan->tick_nohz_notifier); |
| 914 | #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */ |
| 915 | |
| 916 | } else { |
| 917 | struct lib_ring_buffer *buf = chan->backend.buf; |
| 918 | |
| 919 | lib_ring_buffer_start_switch_timer(buf); |
| 920 | lib_ring_buffer_start_read_timer(buf); |
| 921 | } |
| 922 | |
| 923 | return chan; |
| 924 | |
| 925 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) |
| 926 | cpuhp_online_error: |
| 927 | ret = cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare, |
| 928 | &chan->cpuhp_prepare.node); |
| 929 | WARN_ON(ret); |
| 930 | cpuhp_prepare_error: |
| 931 | #endif /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */ |
| 932 | error_free_backend: |
| 933 | channel_backend_free(&chan->backend); |
| 934 | error: |
| 935 | kfree(chan); |
| 936 | return NULL; |
| 937 | } |
| 938 | EXPORT_SYMBOL_GPL(channel_create); |
| 939 | |
| 940 | static |
| 941 | void channel_release(struct kref *kref) |
| 942 | { |
| 943 | struct channel *chan = container_of(kref, struct channel, ref); |
| 944 | channel_free(chan); |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * channel_destroy - Finalize, wait for q.s. and destroy channel. |
| 949 | * @chan: channel to destroy |
| 950 | * |
| 951 | * Holds cpu hotplug. |
| 952 | * Call "destroy" callback, finalize channels, and then decrement the |
| 953 | * channel reference count. Note that when readers have completed data |
| 954 | * consumption of finalized channels, get_subbuf() will return -ENODATA. |
| 955 | * They should release their handle at that point. Returns the private |
| 956 | * data pointer. |
| 957 | */ |
| 958 | void *channel_destroy(struct channel *chan) |
| 959 | { |
| 960 | int cpu; |
| 961 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 962 | void *priv; |
| 963 | |
| 964 | channel_unregister_notifiers(chan); |
| 965 | |
| 966 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 967 | /* |
| 968 | * No need to hold cpu hotplug, because all notifiers have been |
| 969 | * unregistered. |
| 970 | */ |
| 971 | for_each_channel_cpu(cpu, chan) { |
| 972 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, |
| 973 | cpu); |
| 974 | |
| 975 | if (config->cb.buffer_finalize) |
| 976 | config->cb.buffer_finalize(buf, |
| 977 | chan->backend.priv, |
| 978 | cpu); |
| 979 | if (buf->backend.allocated) |
| 980 | lib_ring_buffer_set_quiescent(buf); |
| 981 | /* |
| 982 | * Perform flush before writing to finalized. |
| 983 | */ |
| 984 | smp_wmb(); |
| 985 | ACCESS_ONCE(buf->finalized) = 1; |
| 986 | wake_up_interruptible(&buf->read_wait); |
| 987 | } |
| 988 | } else { |
| 989 | struct lib_ring_buffer *buf = chan->backend.buf; |
| 990 | |
| 991 | if (config->cb.buffer_finalize) |
| 992 | config->cb.buffer_finalize(buf, chan->backend.priv, -1); |
| 993 | if (buf->backend.allocated) |
| 994 | lib_ring_buffer_set_quiescent(buf); |
| 995 | /* |
| 996 | * Perform flush before writing to finalized. |
| 997 | */ |
| 998 | smp_wmb(); |
| 999 | ACCESS_ONCE(buf->finalized) = 1; |
| 1000 | wake_up_interruptible(&buf->read_wait); |
| 1001 | } |
| 1002 | ACCESS_ONCE(chan->finalized) = 1; |
| 1003 | wake_up_interruptible(&chan->hp_wait); |
| 1004 | wake_up_interruptible(&chan->read_wait); |
| 1005 | priv = chan->backend.priv; |
| 1006 | kref_put(&chan->ref, channel_release); |
| 1007 | return priv; |
| 1008 | } |
| 1009 | EXPORT_SYMBOL_GPL(channel_destroy); |
| 1010 | |
| 1011 | struct lib_ring_buffer *channel_get_ring_buffer( |
| 1012 | const struct lib_ring_buffer_config *config, |
| 1013 | struct channel *chan, int cpu) |
| 1014 | { |
| 1015 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) |
| 1016 | return chan->backend.buf; |
| 1017 | else |
| 1018 | return per_cpu_ptr(chan->backend.buf, cpu); |
| 1019 | } |
| 1020 | EXPORT_SYMBOL_GPL(channel_get_ring_buffer); |
| 1021 | |
| 1022 | int lib_ring_buffer_open_read(struct lib_ring_buffer *buf) |
| 1023 | { |
| 1024 | struct channel *chan = buf->backend.chan; |
| 1025 | |
| 1026 | if (!atomic_long_add_unless(&buf->active_readers, 1, 1)) |
| 1027 | return -EBUSY; |
| 1028 | if (!lttng_kref_get(&chan->ref)) { |
| 1029 | atomic_long_dec(&buf->active_readers); |
| 1030 | return -EOVERFLOW; |
| 1031 | } |
| 1032 | lttng_smp_mb__after_atomic(); |
| 1033 | return 0; |
| 1034 | } |
| 1035 | EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read); |
| 1036 | |
| 1037 | void lib_ring_buffer_release_read(struct lib_ring_buffer *buf) |
| 1038 | { |
| 1039 | struct channel *chan = buf->backend.chan; |
| 1040 | |
| 1041 | CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1); |
| 1042 | lttng_smp_mb__before_atomic(); |
| 1043 | atomic_long_dec(&buf->active_readers); |
| 1044 | kref_put(&chan->ref, channel_release); |
| 1045 | } |
| 1046 | EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read); |
| 1047 | |
| 1048 | /* |
| 1049 | * Promote compiler barrier to a smp_mb(). |
| 1050 | * For the specific ring buffer case, this IPI call should be removed if the |
| 1051 | * architecture does not reorder writes. This should eventually be provided by |
| 1052 | * a separate architecture-specific infrastructure. |
| 1053 | */ |
| 1054 | static void remote_mb(void *info) |
| 1055 | { |
| 1056 | smp_mb(); |
| 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read) |
| 1061 | * @buf: ring buffer |
| 1062 | * @consumed: consumed count indicating the position where to read |
| 1063 | * @produced: produced count, indicates position when to stop reading |
| 1064 | * |
| 1065 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no |
| 1066 | * data to read at consumed position, or 0 if the get operation succeeds. |
| 1067 | * Busy-loop trying to get data if the tick_nohz sequence lock is held. |
| 1068 | */ |
| 1069 | |
| 1070 | int lib_ring_buffer_snapshot(struct lib_ring_buffer *buf, |
| 1071 | unsigned long *consumed, unsigned long *produced) |
| 1072 | { |
| 1073 | struct channel *chan = buf->backend.chan; |
| 1074 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1075 | unsigned long consumed_cur, write_offset; |
| 1076 | int finalized; |
| 1077 | |
| 1078 | retry: |
| 1079 | finalized = ACCESS_ONCE(buf->finalized); |
| 1080 | /* |
| 1081 | * Read finalized before counters. |
| 1082 | */ |
| 1083 | smp_rmb(); |
| 1084 | consumed_cur = atomic_long_read(&buf->consumed); |
| 1085 | /* |
| 1086 | * No need to issue a memory barrier between consumed count read and |
| 1087 | * write offset read, because consumed count can only change |
| 1088 | * concurrently in overwrite mode, and we keep a sequence counter |
| 1089 | * identifier derived from the write offset to check we are getting |
| 1090 | * the same sub-buffer we are expecting (the sub-buffers are atomically |
| 1091 | * "tagged" upon writes, tags are checked upon read). |
| 1092 | */ |
| 1093 | write_offset = v_read(config, &buf->offset); |
| 1094 | |
| 1095 | /* |
| 1096 | * Check that we are not about to read the same subbuffer in |
| 1097 | * which the writer head is. |
| 1098 | */ |
| 1099 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) |
| 1100 | == 0) |
| 1101 | goto nodata; |
| 1102 | |
| 1103 | *consumed = consumed_cur; |
| 1104 | *produced = subbuf_trunc(write_offset, chan); |
| 1105 | |
| 1106 | return 0; |
| 1107 | |
| 1108 | nodata: |
| 1109 | /* |
| 1110 | * The memory barriers __wait_event()/wake_up_interruptible() take care |
| 1111 | * of "raw_spin_is_locked" memory ordering. |
| 1112 | */ |
| 1113 | if (finalized) |
| 1114 | return -ENODATA; |
| 1115 | else if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock)) |
| 1116 | goto retry; |
| 1117 | else |
| 1118 | return -EAGAIN; |
| 1119 | } |
| 1120 | EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot); |
| 1121 | |
| 1122 | /** |
| 1123 | * Performs the same function as lib_ring_buffer_snapshot(), but the positions |
| 1124 | * are saved regardless of whether the consumed and produced positions are |
| 1125 | * in the same subbuffer. |
| 1126 | * @buf: ring buffer |
| 1127 | * @consumed: consumed byte count indicating the last position read |
| 1128 | * @produced: produced byte count indicating the last position written |
| 1129 | * |
| 1130 | * This function is meant to provide information on the exact producer and |
| 1131 | * consumer positions without regard for the "snapshot" feature. |
| 1132 | */ |
| 1133 | int lib_ring_buffer_snapshot_sample_positions(struct lib_ring_buffer *buf, |
| 1134 | unsigned long *consumed, unsigned long *produced) |
| 1135 | { |
| 1136 | struct channel *chan = buf->backend.chan; |
| 1137 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1138 | |
| 1139 | smp_rmb(); |
| 1140 | *consumed = atomic_long_read(&buf->consumed); |
| 1141 | /* |
| 1142 | * No need to issue a memory barrier between consumed count read and |
| 1143 | * write offset read, because consumed count can only change |
| 1144 | * concurrently in overwrite mode, and we keep a sequence counter |
| 1145 | * identifier derived from the write offset to check we are getting |
| 1146 | * the same sub-buffer we are expecting (the sub-buffers are atomically |
| 1147 | * "tagged" upon writes, tags are checked upon read). |
| 1148 | */ |
| 1149 | *produced = v_read(config, &buf->offset); |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * lib_ring_buffer_put_snapshot - move consumed counter forward |
| 1155 | * |
| 1156 | * Should only be called from consumer context. |
| 1157 | * @buf: ring buffer |
| 1158 | * @consumed_new: new consumed count value |
| 1159 | */ |
| 1160 | void lib_ring_buffer_move_consumer(struct lib_ring_buffer *buf, |
| 1161 | unsigned long consumed_new) |
| 1162 | { |
| 1163 | struct lib_ring_buffer_backend *bufb = &buf->backend; |
| 1164 | struct channel *chan = bufb->chan; |
| 1165 | unsigned long consumed; |
| 1166 | |
| 1167 | CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1); |
| 1168 | |
| 1169 | /* |
| 1170 | * Only push the consumed value forward. |
| 1171 | * If the consumed cmpxchg fails, this is because we have been pushed by |
| 1172 | * the writer in flight recorder mode. |
| 1173 | */ |
| 1174 | consumed = atomic_long_read(&buf->consumed); |
| 1175 | while ((long) consumed - (long) consumed_new < 0) |
| 1176 | consumed = atomic_long_cmpxchg(&buf->consumed, consumed, |
| 1177 | consumed_new); |
| 1178 | /* Wake-up the metadata producer */ |
| 1179 | wake_up_interruptible(&buf->write_wait); |
| 1180 | } |
| 1181 | EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer); |
| 1182 | |
| 1183 | /** |
| 1184 | * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading |
| 1185 | * @buf: ring buffer |
| 1186 | * @consumed: consumed count indicating the position where to read |
| 1187 | * |
| 1188 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no |
| 1189 | * data to read at consumed position, or 0 if the get operation succeeds. |
| 1190 | * Busy-loop trying to get data if the tick_nohz sequence lock is held. |
| 1191 | */ |
| 1192 | int lib_ring_buffer_get_subbuf(struct lib_ring_buffer *buf, |
| 1193 | unsigned long consumed) |
| 1194 | { |
| 1195 | struct channel *chan = buf->backend.chan; |
| 1196 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1197 | unsigned long consumed_cur, consumed_idx, commit_count, write_offset; |
| 1198 | int ret; |
| 1199 | int finalized; |
| 1200 | |
| 1201 | if (buf->get_subbuf) { |
| 1202 | /* |
| 1203 | * Reader is trying to get a subbuffer twice. |
| 1204 | */ |
| 1205 | CHAN_WARN_ON(chan, 1); |
| 1206 | return -EBUSY; |
| 1207 | } |
| 1208 | retry: |
| 1209 | finalized = ACCESS_ONCE(buf->finalized); |
| 1210 | /* |
| 1211 | * Read finalized before counters. |
| 1212 | */ |
| 1213 | smp_rmb(); |
| 1214 | consumed_cur = atomic_long_read(&buf->consumed); |
| 1215 | consumed_idx = subbuf_index(consumed, chan); |
| 1216 | commit_count = v_read(config, &buf->commit_cold[consumed_idx].cc_sb); |
| 1217 | /* |
| 1218 | * Make sure we read the commit count before reading the buffer |
| 1219 | * data and the write offset. Correct consumed offset ordering |
| 1220 | * wrt commit count is insured by the use of cmpxchg to update |
| 1221 | * the consumed offset. |
| 1222 | * smp_call_function_single can fail if the remote CPU is offline, |
| 1223 | * this is OK because then there is no wmb to execute there. |
| 1224 | * If our thread is executing on the same CPU as the on the buffers |
| 1225 | * belongs to, we don't have to synchronize it at all. If we are |
| 1226 | * migrated, the scheduler will take care of the memory barriers. |
| 1227 | * Normally, smp_call_function_single() should ensure program order when |
| 1228 | * executing the remote function, which implies that it surrounds the |
| 1229 | * function execution with : |
| 1230 | * smp_mb() |
| 1231 | * send IPI |
| 1232 | * csd_lock_wait |
| 1233 | * recv IPI |
| 1234 | * smp_mb() |
| 1235 | * exec. function |
| 1236 | * smp_mb() |
| 1237 | * csd unlock |
| 1238 | * smp_mb() |
| 1239 | * |
| 1240 | * However, smp_call_function_single() does not seem to clearly execute |
| 1241 | * such barriers. It depends on spinlock semantic to provide the barrier |
| 1242 | * before executing the IPI and, when busy-looping, csd_lock_wait only |
| 1243 | * executes smp_mb() when it has to wait for the other CPU. |
| 1244 | * |
| 1245 | * I don't trust this code. Therefore, let's add the smp_mb() sequence |
| 1246 | * required ourself, even if duplicated. It has no performance impact |
| 1247 | * anyway. |
| 1248 | * |
| 1249 | * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs |
| 1250 | * read and write vs write. They do not ensure core synchronization. We |
| 1251 | * really have to ensure total order between the 3 barriers running on |
| 1252 | * the 2 CPUs. |
| 1253 | */ |
| 1254 | if (config->ipi == RING_BUFFER_IPI_BARRIER) { |
| 1255 | if (config->sync == RING_BUFFER_SYNC_PER_CPU |
| 1256 | && config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 1257 | if (raw_smp_processor_id() != buf->backend.cpu) { |
| 1258 | /* Total order with IPI handler smp_mb() */ |
| 1259 | smp_mb(); |
| 1260 | smp_call_function_single(buf->backend.cpu, |
| 1261 | remote_mb, NULL, 1); |
| 1262 | /* Total order with IPI handler smp_mb() */ |
| 1263 | smp_mb(); |
| 1264 | } |
| 1265 | } else { |
| 1266 | /* Total order with IPI handler smp_mb() */ |
| 1267 | smp_mb(); |
| 1268 | smp_call_function(remote_mb, NULL, 1); |
| 1269 | /* Total order with IPI handler smp_mb() */ |
| 1270 | smp_mb(); |
| 1271 | } |
| 1272 | } else { |
| 1273 | /* |
| 1274 | * Local rmb to match the remote wmb to read the commit count |
| 1275 | * before the buffer data and the write offset. |
| 1276 | */ |
| 1277 | smp_rmb(); |
| 1278 | } |
| 1279 | |
| 1280 | write_offset = v_read(config, &buf->offset); |
| 1281 | |
| 1282 | /* |
| 1283 | * Check that the buffer we are getting is after or at consumed_cur |
| 1284 | * position. |
| 1285 | */ |
| 1286 | if ((long) subbuf_trunc(consumed, chan) |
| 1287 | - (long) subbuf_trunc(consumed_cur, chan) < 0) |
| 1288 | goto nodata; |
| 1289 | |
| 1290 | /* |
| 1291 | * Check that the subbuffer we are trying to consume has been |
| 1292 | * already fully committed. |
| 1293 | */ |
| 1294 | if (((commit_count - chan->backend.subbuf_size) |
| 1295 | & chan->commit_count_mask) |
| 1296 | - (buf_trunc(consumed, chan) |
| 1297 | >> chan->backend.num_subbuf_order) |
| 1298 | != 0) |
| 1299 | goto nodata; |
| 1300 | |
| 1301 | /* |
| 1302 | * Check that we are not about to read the same subbuffer in |
| 1303 | * which the writer head is. |
| 1304 | */ |
| 1305 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed, chan) |
| 1306 | == 0) |
| 1307 | goto nodata; |
| 1308 | |
| 1309 | /* |
| 1310 | * Failure to get the subbuffer causes a busy-loop retry without going |
| 1311 | * to a wait queue. These are caused by short-lived race windows where |
| 1312 | * the writer is getting access to a subbuffer we were trying to get |
| 1313 | * access to. Also checks that the "consumed" buffer count we are |
| 1314 | * looking for matches the one contained in the subbuffer id. |
| 1315 | */ |
| 1316 | ret = update_read_sb_index(config, &buf->backend, &chan->backend, |
| 1317 | consumed_idx, buf_trunc_val(consumed, chan)); |
| 1318 | if (ret) |
| 1319 | goto retry; |
| 1320 | subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id); |
| 1321 | |
| 1322 | buf->get_subbuf_consumed = consumed; |
| 1323 | buf->get_subbuf = 1; |
| 1324 | |
| 1325 | return 0; |
| 1326 | |
| 1327 | nodata: |
| 1328 | /* |
| 1329 | * The memory barriers __wait_event()/wake_up_interruptible() take care |
| 1330 | * of "raw_spin_is_locked" memory ordering. |
| 1331 | */ |
| 1332 | if (finalized) |
| 1333 | return -ENODATA; |
| 1334 | else if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock)) |
| 1335 | goto retry; |
| 1336 | else |
| 1337 | return -EAGAIN; |
| 1338 | } |
| 1339 | EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf); |
| 1340 | |
| 1341 | /** |
| 1342 | * lib_ring_buffer_put_subbuf - release exclusive subbuffer access |
| 1343 | * @buf: ring buffer |
| 1344 | */ |
| 1345 | void lib_ring_buffer_put_subbuf(struct lib_ring_buffer *buf) |
| 1346 | { |
| 1347 | struct lib_ring_buffer_backend *bufb = &buf->backend; |
| 1348 | struct channel *chan = bufb->chan; |
| 1349 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1350 | unsigned long read_sb_bindex, consumed_idx, consumed; |
| 1351 | |
| 1352 | CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1); |
| 1353 | |
| 1354 | if (!buf->get_subbuf) { |
| 1355 | /* |
| 1356 | * Reader puts a subbuffer it did not get. |
| 1357 | */ |
| 1358 | CHAN_WARN_ON(chan, 1); |
| 1359 | return; |
| 1360 | } |
| 1361 | consumed = buf->get_subbuf_consumed; |
| 1362 | buf->get_subbuf = 0; |
| 1363 | |
| 1364 | /* |
| 1365 | * Clear the records_unread counter. (overruns counter) |
| 1366 | * Can still be non-zero if a file reader simply grabbed the data |
| 1367 | * without using iterators. |
| 1368 | * Can be below zero if an iterator is used on a snapshot more than |
| 1369 | * once. |
| 1370 | */ |
| 1371 | read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); |
| 1372 | v_add(config, v_read(config, |
| 1373 | &bufb->array[read_sb_bindex]->records_unread), |
| 1374 | &bufb->records_read); |
| 1375 | v_set(config, &bufb->array[read_sb_bindex]->records_unread, 0); |
| 1376 | CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE |
| 1377 | && subbuffer_id_is_noref(config, bufb->buf_rsb.id)); |
| 1378 | subbuffer_id_set_noref(config, &bufb->buf_rsb.id); |
| 1379 | |
| 1380 | /* |
| 1381 | * Exchange the reader subbuffer with the one we put in its place in the |
| 1382 | * writer subbuffer table. Expect the original consumed count. If |
| 1383 | * update_read_sb_index fails, this is because the writer updated the |
| 1384 | * subbuffer concurrently. We should therefore keep the subbuffer we |
| 1385 | * currently have: it has become invalid to try reading this sub-buffer |
| 1386 | * consumed count value anyway. |
| 1387 | */ |
| 1388 | consumed_idx = subbuf_index(consumed, chan); |
| 1389 | update_read_sb_index(config, &buf->backend, &chan->backend, |
| 1390 | consumed_idx, buf_trunc_val(consumed, chan)); |
| 1391 | /* |
| 1392 | * update_read_sb_index return value ignored. Don't exchange sub-buffer |
| 1393 | * if the writer concurrently updated it. |
| 1394 | */ |
| 1395 | } |
| 1396 | EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf); |
| 1397 | |
| 1398 | /* |
| 1399 | * cons_offset is an iterator on all subbuffer offsets between the reader |
| 1400 | * position and the writer position. (inclusive) |
| 1401 | */ |
| 1402 | static |
| 1403 | void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer *buf, |
| 1404 | struct channel *chan, |
| 1405 | unsigned long cons_offset, |
| 1406 | int cpu) |
| 1407 | { |
| 1408 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1409 | unsigned long cons_idx, commit_count, commit_count_sb; |
| 1410 | |
| 1411 | cons_idx = subbuf_index(cons_offset, chan); |
| 1412 | commit_count = v_read(config, &buf->commit_hot[cons_idx].cc); |
| 1413 | commit_count_sb = v_read(config, &buf->commit_cold[cons_idx].cc_sb); |
| 1414 | |
| 1415 | if (subbuf_offset(commit_count, chan) != 0) |
| 1416 | printk(KERN_WARNING |
| 1417 | "ring buffer %s, cpu %d: " |
| 1418 | "commit count in subbuffer %lu,\n" |
| 1419 | "expecting multiples of %lu bytes\n" |
| 1420 | " [ %lu bytes committed, %lu bytes reader-visible ]\n", |
| 1421 | chan->backend.name, cpu, cons_idx, |
| 1422 | chan->backend.subbuf_size, |
| 1423 | commit_count, commit_count_sb); |
| 1424 | |
| 1425 | printk(KERN_DEBUG "ring buffer: %s, cpu %d: %lu bytes committed\n", |
| 1426 | chan->backend.name, cpu, commit_count); |
| 1427 | } |
| 1428 | |
| 1429 | static |
| 1430 | void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer *buf, |
| 1431 | struct channel *chan, |
| 1432 | void *priv, int cpu) |
| 1433 | { |
| 1434 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1435 | unsigned long write_offset, cons_offset; |
| 1436 | |
| 1437 | /* |
| 1438 | * No need to order commit_count, write_offset and cons_offset reads |
| 1439 | * because we execute at teardown when no more writer nor reader |
| 1440 | * references are left. |
| 1441 | */ |
| 1442 | write_offset = v_read(config, &buf->offset); |
| 1443 | cons_offset = atomic_long_read(&buf->consumed); |
| 1444 | if (write_offset != cons_offset) |
| 1445 | printk(KERN_DEBUG |
| 1446 | "ring buffer %s, cpu %d: " |
| 1447 | "non-consumed data\n" |
| 1448 | " [ %lu bytes written, %lu bytes read ]\n", |
| 1449 | chan->backend.name, cpu, write_offset, cons_offset); |
| 1450 | |
| 1451 | for (cons_offset = atomic_long_read(&buf->consumed); |
| 1452 | (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset), |
| 1453 | chan) |
| 1454 | - cons_offset) > 0; |
| 1455 | cons_offset = subbuf_align(cons_offset, chan)) |
| 1456 | lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset, |
| 1457 | cpu); |
| 1458 | } |
| 1459 | |
| 1460 | static |
| 1461 | void lib_ring_buffer_print_errors(struct channel *chan, |
| 1462 | struct lib_ring_buffer *buf, int cpu) |
| 1463 | { |
| 1464 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1465 | void *priv = chan->backend.priv; |
| 1466 | |
| 1467 | if (!strcmp(chan->backend.name, "relay-metadata")) { |
| 1468 | printk(KERN_DEBUG "ring buffer %s: %lu records written, " |
| 1469 | "%lu records overrun\n", |
| 1470 | chan->backend.name, |
| 1471 | v_read(config, &buf->records_count), |
| 1472 | v_read(config, &buf->records_overrun)); |
| 1473 | } else { |
| 1474 | printk(KERN_DEBUG "ring buffer %s, cpu %d: %lu records written, " |
| 1475 | "%lu records overrun\n", |
| 1476 | chan->backend.name, cpu, |
| 1477 | v_read(config, &buf->records_count), |
| 1478 | v_read(config, &buf->records_overrun)); |
| 1479 | |
| 1480 | if (v_read(config, &buf->records_lost_full) |
| 1481 | || v_read(config, &buf->records_lost_wrap) |
| 1482 | || v_read(config, &buf->records_lost_big)) |
| 1483 | printk(KERN_WARNING |
| 1484 | "ring buffer %s, cpu %d: records were lost. Caused by:\n" |
| 1485 | " [ %lu buffer full, %lu nest buffer wrap-around, " |
| 1486 | "%lu event too big ]\n", |
| 1487 | chan->backend.name, cpu, |
| 1488 | v_read(config, &buf->records_lost_full), |
| 1489 | v_read(config, &buf->records_lost_wrap), |
| 1490 | v_read(config, &buf->records_lost_big)); |
| 1491 | } |
| 1492 | lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu); |
| 1493 | } |
| 1494 | |
| 1495 | /* |
| 1496 | * lib_ring_buffer_switch_old_start: Populate old subbuffer header. |
| 1497 | * |
| 1498 | * Only executed when the buffer is finalized, in SWITCH_FLUSH. |
| 1499 | */ |
| 1500 | static |
| 1501 | void lib_ring_buffer_switch_old_start(struct lib_ring_buffer *buf, |
| 1502 | struct channel *chan, |
| 1503 | struct switch_offsets *offsets, |
| 1504 | u64 tsc) |
| 1505 | { |
| 1506 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1507 | unsigned long oldidx = subbuf_index(offsets->old, chan); |
| 1508 | unsigned long commit_count; |
| 1509 | struct commit_counters_hot *cc_hot; |
| 1510 | |
| 1511 | config->cb.buffer_begin(buf, tsc, oldidx); |
| 1512 | |
| 1513 | /* |
| 1514 | * Order all writes to buffer before the commit count update that will |
| 1515 | * determine that the subbuffer is full. |
| 1516 | */ |
| 1517 | if (config->ipi == RING_BUFFER_IPI_BARRIER) { |
| 1518 | /* |
| 1519 | * Must write slot data before incrementing commit count. This |
| 1520 | * compiler barrier is upgraded into a smp_mb() by the IPI sent |
| 1521 | * by get_subbuf(). |
| 1522 | */ |
| 1523 | barrier(); |
| 1524 | } else |
| 1525 | smp_wmb(); |
| 1526 | cc_hot = &buf->commit_hot[oldidx]; |
| 1527 | v_add(config, config->cb.subbuffer_header_size(), &cc_hot->cc); |
| 1528 | commit_count = v_read(config, &cc_hot->cc); |
| 1529 | /* Check if the written buffer has to be delivered */ |
| 1530 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old, |
| 1531 | commit_count, oldidx, tsc); |
| 1532 | lib_ring_buffer_write_commit_counter(config, buf, chan, |
| 1533 | offsets->old + config->cb.subbuffer_header_size(), |
| 1534 | commit_count, cc_hot); |
| 1535 | } |
| 1536 | |
| 1537 | /* |
| 1538 | * lib_ring_buffer_switch_old_end: switch old subbuffer |
| 1539 | * |
| 1540 | * Note : offset_old should never be 0 here. It is ok, because we never perform |
| 1541 | * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller |
| 1542 | * increments the offset_old value when doing a SWITCH_FLUSH on an empty |
| 1543 | * subbuffer. |
| 1544 | */ |
| 1545 | static |
| 1546 | void lib_ring_buffer_switch_old_end(struct lib_ring_buffer *buf, |
| 1547 | struct channel *chan, |
| 1548 | struct switch_offsets *offsets, |
| 1549 | u64 tsc) |
| 1550 | { |
| 1551 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1552 | unsigned long oldidx = subbuf_index(offsets->old - 1, chan); |
| 1553 | unsigned long commit_count, padding_size, data_size; |
| 1554 | struct commit_counters_hot *cc_hot; |
| 1555 | |
| 1556 | data_size = subbuf_offset(offsets->old - 1, chan) + 1; |
| 1557 | padding_size = chan->backend.subbuf_size - data_size; |
| 1558 | subbuffer_set_data_size(config, &buf->backend, oldidx, data_size); |
| 1559 | |
| 1560 | /* |
| 1561 | * Order all writes to buffer before the commit count update that will |
| 1562 | * determine that the subbuffer is full. |
| 1563 | */ |
| 1564 | if (config->ipi == RING_BUFFER_IPI_BARRIER) { |
| 1565 | /* |
| 1566 | * Must write slot data before incrementing commit count. This |
| 1567 | * compiler barrier is upgraded into a smp_mb() by the IPI sent |
| 1568 | * by get_subbuf(). |
| 1569 | */ |
| 1570 | barrier(); |
| 1571 | } else |
| 1572 | smp_wmb(); |
| 1573 | cc_hot = &buf->commit_hot[oldidx]; |
| 1574 | v_add(config, padding_size, &cc_hot->cc); |
| 1575 | commit_count = v_read(config, &cc_hot->cc); |
| 1576 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1, |
| 1577 | commit_count, oldidx, tsc); |
| 1578 | lib_ring_buffer_write_commit_counter(config, buf, chan, |
| 1579 | offsets->old + padding_size, commit_count, |
| 1580 | cc_hot); |
| 1581 | } |
| 1582 | |
| 1583 | /* |
| 1584 | * lib_ring_buffer_switch_new_start: Populate new subbuffer. |
| 1585 | * |
| 1586 | * This code can be executed unordered : writers may already have written to the |
| 1587 | * sub-buffer before this code gets executed, caution. The commit makes sure |
| 1588 | * that this code is executed before the deliver of this sub-buffer. |
| 1589 | */ |
| 1590 | static |
| 1591 | void lib_ring_buffer_switch_new_start(struct lib_ring_buffer *buf, |
| 1592 | struct channel *chan, |
| 1593 | struct switch_offsets *offsets, |
| 1594 | u64 tsc) |
| 1595 | { |
| 1596 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1597 | unsigned long beginidx = subbuf_index(offsets->begin, chan); |
| 1598 | unsigned long commit_count; |
| 1599 | struct commit_counters_hot *cc_hot; |
| 1600 | |
| 1601 | config->cb.buffer_begin(buf, tsc, beginidx); |
| 1602 | |
| 1603 | /* |
| 1604 | * Order all writes to buffer before the commit count update that will |
| 1605 | * determine that the subbuffer is full. |
| 1606 | */ |
| 1607 | if (config->ipi == RING_BUFFER_IPI_BARRIER) { |
| 1608 | /* |
| 1609 | * Must write slot data before incrementing commit count. This |
| 1610 | * compiler barrier is upgraded into a smp_mb() by the IPI sent |
| 1611 | * by get_subbuf(). |
| 1612 | */ |
| 1613 | barrier(); |
| 1614 | } else |
| 1615 | smp_wmb(); |
| 1616 | cc_hot = &buf->commit_hot[beginidx]; |
| 1617 | v_add(config, config->cb.subbuffer_header_size(), &cc_hot->cc); |
| 1618 | commit_count = v_read(config, &cc_hot->cc); |
| 1619 | /* Check if the written buffer has to be delivered */ |
| 1620 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin, |
| 1621 | commit_count, beginidx, tsc); |
| 1622 | lib_ring_buffer_write_commit_counter(config, buf, chan, |
| 1623 | offsets->begin + config->cb.subbuffer_header_size(), |
| 1624 | commit_count, cc_hot); |
| 1625 | } |
| 1626 | |
| 1627 | /* |
| 1628 | * lib_ring_buffer_switch_new_end: finish switching current subbuffer |
| 1629 | * |
| 1630 | * Calls subbuffer_set_data_size() to set the data size of the current |
| 1631 | * sub-buffer. We do not need to perform check_deliver nor commit here, |
| 1632 | * since this task will be done by the "commit" of the event for which |
| 1633 | * we are currently doing the space reservation. |
| 1634 | */ |
| 1635 | static |
| 1636 | void lib_ring_buffer_switch_new_end(struct lib_ring_buffer *buf, |
| 1637 | struct channel *chan, |
| 1638 | struct switch_offsets *offsets, |
| 1639 | u64 tsc) |
| 1640 | { |
| 1641 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1642 | unsigned long endidx, data_size; |
| 1643 | |
| 1644 | endidx = subbuf_index(offsets->end - 1, chan); |
| 1645 | data_size = subbuf_offset(offsets->end - 1, chan) + 1; |
| 1646 | subbuffer_set_data_size(config, &buf->backend, endidx, data_size); |
| 1647 | } |
| 1648 | |
| 1649 | /* |
| 1650 | * Returns : |
| 1651 | * 0 if ok |
| 1652 | * !0 if execution must be aborted. |
| 1653 | */ |
| 1654 | static |
| 1655 | int lib_ring_buffer_try_switch_slow(enum switch_mode mode, |
| 1656 | struct lib_ring_buffer *buf, |
| 1657 | struct channel *chan, |
| 1658 | struct switch_offsets *offsets, |
| 1659 | u64 *tsc) |
| 1660 | { |
| 1661 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1662 | unsigned long off, reserve_commit_diff; |
| 1663 | |
| 1664 | offsets->begin = v_read(config, &buf->offset); |
| 1665 | offsets->old = offsets->begin; |
| 1666 | offsets->switch_old_start = 0; |
| 1667 | off = subbuf_offset(offsets->begin, chan); |
| 1668 | |
| 1669 | *tsc = config->cb.ring_buffer_clock_read(chan); |
| 1670 | |
| 1671 | /* |
| 1672 | * Ensure we flush the header of an empty subbuffer when doing the |
| 1673 | * finalize (SWITCH_FLUSH). This ensures that we end up knowing the |
| 1674 | * total data gathering duration even if there were no records saved |
| 1675 | * after the last buffer switch. |
| 1676 | * In SWITCH_ACTIVE mode, switch the buffer when it contains events. |
| 1677 | * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of |
| 1678 | * subbuffer header as appropriate. |
| 1679 | * The next record that reserves space will be responsible for |
| 1680 | * populating the following subbuffer header. We choose not to populate |
| 1681 | * the next subbuffer header here because we want to be able to use |
| 1682 | * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop |
| 1683 | * buffer flush, which must guarantee that all the buffer content |
| 1684 | * (records and header timestamps) are visible to the reader. This is |
| 1685 | * required for quiescence guarantees for the fusion merge. |
| 1686 | */ |
| 1687 | if (mode != SWITCH_FLUSH && !off) |
| 1688 | return -1; /* we do not have to switch : buffer is empty */ |
| 1689 | |
| 1690 | if (unlikely(off == 0)) { |
| 1691 | unsigned long sb_index, commit_count; |
| 1692 | |
| 1693 | /* |
| 1694 | * We are performing a SWITCH_FLUSH. At this stage, there are no |
| 1695 | * concurrent writes into the buffer. |
| 1696 | * |
| 1697 | * The client does not save any header information. Don't |
| 1698 | * switch empty subbuffer on finalize, because it is invalid to |
| 1699 | * deliver a completely empty subbuffer. |
| 1700 | */ |
| 1701 | if (!config->cb.subbuffer_header_size()) |
| 1702 | return -1; |
| 1703 | |
| 1704 | /* Test new buffer integrity */ |
| 1705 | sb_index = subbuf_index(offsets->begin, chan); |
| 1706 | commit_count = v_read(config, |
| 1707 | &buf->commit_cold[sb_index].cc_sb); |
| 1708 | reserve_commit_diff = |
| 1709 | (buf_trunc(offsets->begin, chan) |
| 1710 | >> chan->backend.num_subbuf_order) |
| 1711 | - (commit_count & chan->commit_count_mask); |
| 1712 | if (likely(reserve_commit_diff == 0)) { |
| 1713 | /* Next subbuffer not being written to. */ |
| 1714 | if (unlikely(config->mode != RING_BUFFER_OVERWRITE && |
| 1715 | subbuf_trunc(offsets->begin, chan) |
| 1716 | - subbuf_trunc((unsigned long) |
| 1717 | atomic_long_read(&buf->consumed), chan) |
| 1718 | >= chan->backend.buf_size)) { |
| 1719 | /* |
| 1720 | * We do not overwrite non consumed buffers |
| 1721 | * and we are full : don't switch. |
| 1722 | */ |
| 1723 | return -1; |
| 1724 | } else { |
| 1725 | /* |
| 1726 | * Next subbuffer not being written to, and we |
| 1727 | * are either in overwrite mode or the buffer is |
| 1728 | * not full. It's safe to write in this new |
| 1729 | * subbuffer. |
| 1730 | */ |
| 1731 | } |
| 1732 | } else { |
| 1733 | /* |
| 1734 | * Next subbuffer reserve offset does not match the |
| 1735 | * commit offset. Don't perform switch in |
| 1736 | * producer-consumer and overwrite mode. Caused by |
| 1737 | * either a writer OOPS or too many nested writes over a |
| 1738 | * reserve/commit pair. |
| 1739 | */ |
| 1740 | return -1; |
| 1741 | } |
| 1742 | |
| 1743 | /* |
| 1744 | * Need to write the subbuffer start header on finalize. |
| 1745 | */ |
| 1746 | offsets->switch_old_start = 1; |
| 1747 | } |
| 1748 | offsets->begin = subbuf_align(offsets->begin, chan); |
| 1749 | /* Note: old points to the next subbuf at offset 0 */ |
| 1750 | offsets->end = offsets->begin; |
| 1751 | return 0; |
| 1752 | } |
| 1753 | |
| 1754 | /* |
| 1755 | * Force a sub-buffer switch. This operation is completely reentrant : can be |
| 1756 | * called while tracing is active with absolutely no lock held. |
| 1757 | * |
| 1758 | * Note, however, that as a v_cmpxchg is used for some atomic |
| 1759 | * operations, this function must be called from the CPU which owns the buffer |
| 1760 | * for a ACTIVE flush. |
| 1761 | */ |
| 1762 | void lib_ring_buffer_switch_slow(struct lib_ring_buffer *buf, enum switch_mode mode) |
| 1763 | { |
| 1764 | struct channel *chan = buf->backend.chan; |
| 1765 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1766 | struct switch_offsets offsets; |
| 1767 | unsigned long oldidx; |
| 1768 | u64 tsc; |
| 1769 | |
| 1770 | offsets.size = 0; |
| 1771 | |
| 1772 | /* |
| 1773 | * Perform retryable operations. |
| 1774 | */ |
| 1775 | do { |
| 1776 | if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets, |
| 1777 | &tsc)) |
| 1778 | return; /* Switch not needed */ |
| 1779 | } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end) |
| 1780 | != offsets.old); |
| 1781 | |
| 1782 | /* |
| 1783 | * Atomically update last_tsc. This update races against concurrent |
| 1784 | * atomic updates, but the race will always cause supplementary full TSC |
| 1785 | * records, never the opposite (missing a full TSC record when it would |
| 1786 | * be needed). |
| 1787 | */ |
| 1788 | save_last_tsc(config, buf, tsc); |
| 1789 | |
| 1790 | /* |
| 1791 | * Push the reader if necessary |
| 1792 | */ |
| 1793 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old); |
| 1794 | |
| 1795 | oldidx = subbuf_index(offsets.old, chan); |
| 1796 | lib_ring_buffer_clear_noref(config, &buf->backend, oldidx); |
| 1797 | |
| 1798 | /* |
| 1799 | * May need to populate header start on SWITCH_FLUSH. |
| 1800 | */ |
| 1801 | if (offsets.switch_old_start) { |
| 1802 | lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc); |
| 1803 | offsets.old += config->cb.subbuffer_header_size(); |
| 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * Switch old subbuffer. |
| 1808 | */ |
| 1809 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc); |
| 1810 | } |
| 1811 | EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow); |
| 1812 | |
| 1813 | struct switch_param { |
| 1814 | struct lib_ring_buffer *buf; |
| 1815 | enum switch_mode mode; |
| 1816 | }; |
| 1817 | |
| 1818 | static void remote_switch(void *info) |
| 1819 | { |
| 1820 | struct switch_param *param = info; |
| 1821 | struct lib_ring_buffer *buf = param->buf; |
| 1822 | |
| 1823 | lib_ring_buffer_switch_slow(buf, param->mode); |
| 1824 | } |
| 1825 | |
| 1826 | static void _lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf, |
| 1827 | enum switch_mode mode) |
| 1828 | { |
| 1829 | struct channel *chan = buf->backend.chan; |
| 1830 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1831 | int ret; |
| 1832 | struct switch_param param; |
| 1833 | |
| 1834 | /* |
| 1835 | * With global synchronization we don't need to use the IPI scheme. |
| 1836 | */ |
| 1837 | if (config->sync == RING_BUFFER_SYNC_GLOBAL) { |
| 1838 | lib_ring_buffer_switch_slow(buf, mode); |
| 1839 | return; |
| 1840 | } |
| 1841 | |
| 1842 | /* |
| 1843 | * Taking lock on CPU hotplug to ensure two things: first, that the |
| 1844 | * target cpu is not taken concurrently offline while we are within |
| 1845 | * smp_call_function_single() (I don't trust that get_cpu() on the |
| 1846 | * _local_ CPU actually inhibit CPU hotplug for the _remote_ CPU (to be |
| 1847 | * confirmed)). Secondly, if it happens that the CPU is not online, our |
| 1848 | * own call to lib_ring_buffer_switch_slow() needs to be protected from |
| 1849 | * CPU hotplug handlers, which can also perform a remote subbuffer |
| 1850 | * switch. |
| 1851 | */ |
| 1852 | get_online_cpus(); |
| 1853 | param.buf = buf; |
| 1854 | param.mode = mode; |
| 1855 | ret = smp_call_function_single(buf->backend.cpu, |
| 1856 | remote_switch, ¶m, 1); |
| 1857 | if (ret) { |
| 1858 | /* Remote CPU is offline, do it ourself. */ |
| 1859 | lib_ring_buffer_switch_slow(buf, mode); |
| 1860 | } |
| 1861 | put_online_cpus(); |
| 1862 | } |
| 1863 | |
| 1864 | /* Switch sub-buffer if current sub-buffer is non-empty. */ |
| 1865 | void lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf) |
| 1866 | { |
| 1867 | _lib_ring_buffer_switch_remote(buf, SWITCH_ACTIVE); |
| 1868 | } |
| 1869 | EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote); |
| 1870 | |
| 1871 | /* Switch sub-buffer even if current sub-buffer is empty. */ |
| 1872 | void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer *buf) |
| 1873 | { |
| 1874 | _lib_ring_buffer_switch_remote(buf, SWITCH_FLUSH); |
| 1875 | } |
| 1876 | EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote_empty); |
| 1877 | |
| 1878 | /* |
| 1879 | * Returns : |
| 1880 | * 0 if ok |
| 1881 | * -ENOSPC if event size is too large for packet. |
| 1882 | * -ENOBUFS if there is currently not enough space in buffer for the event. |
| 1883 | * -EIO if data cannot be written into the buffer for any other reason. |
| 1884 | */ |
| 1885 | static |
| 1886 | int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer *buf, |
| 1887 | struct channel *chan, |
| 1888 | struct switch_offsets *offsets, |
| 1889 | struct lib_ring_buffer_ctx *ctx) |
| 1890 | { |
| 1891 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1892 | unsigned long reserve_commit_diff, offset_cmp; |
| 1893 | |
| 1894 | retry: |
| 1895 | offsets->begin = offset_cmp = v_read(config, &buf->offset); |
| 1896 | offsets->old = offsets->begin; |
| 1897 | offsets->switch_new_start = 0; |
| 1898 | offsets->switch_new_end = 0; |
| 1899 | offsets->switch_old_end = 0; |
| 1900 | offsets->pre_header_padding = 0; |
| 1901 | |
| 1902 | ctx->tsc = config->cb.ring_buffer_clock_read(chan); |
| 1903 | if ((int64_t) ctx->tsc == -EIO) |
| 1904 | return -EIO; |
| 1905 | |
| 1906 | if (last_tsc_overflow(config, buf, ctx->tsc)) |
| 1907 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; |
| 1908 | |
| 1909 | if (unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) { |
| 1910 | offsets->switch_new_start = 1; /* For offsets->begin */ |
| 1911 | } else { |
| 1912 | offsets->size = config->cb.record_header_size(config, chan, |
| 1913 | offsets->begin, |
| 1914 | &offsets->pre_header_padding, |
| 1915 | ctx); |
| 1916 | offsets->size += |
| 1917 | lib_ring_buffer_align(offsets->begin + offsets->size, |
| 1918 | ctx->largest_align) |
| 1919 | + ctx->data_size; |
| 1920 | if (unlikely(subbuf_offset(offsets->begin, chan) + |
| 1921 | offsets->size > chan->backend.subbuf_size)) { |
| 1922 | offsets->switch_old_end = 1; /* For offsets->old */ |
| 1923 | offsets->switch_new_start = 1; /* For offsets->begin */ |
| 1924 | } |
| 1925 | } |
| 1926 | if (unlikely(offsets->switch_new_start)) { |
| 1927 | unsigned long sb_index, commit_count; |
| 1928 | |
| 1929 | /* |
| 1930 | * We are typically not filling the previous buffer completely. |
| 1931 | */ |
| 1932 | if (likely(offsets->switch_old_end)) |
| 1933 | offsets->begin = subbuf_align(offsets->begin, chan); |
| 1934 | offsets->begin = offsets->begin |
| 1935 | + config->cb.subbuffer_header_size(); |
| 1936 | /* Test new buffer integrity */ |
| 1937 | sb_index = subbuf_index(offsets->begin, chan); |
| 1938 | /* |
| 1939 | * Read buf->offset before buf->commit_cold[sb_index].cc_sb. |
| 1940 | * lib_ring_buffer_check_deliver() has the matching |
| 1941 | * memory barriers required around commit_cold cc_sb |
| 1942 | * updates to ensure reserve and commit counter updates |
| 1943 | * are not seen reordered when updated by another CPU. |
| 1944 | */ |
| 1945 | smp_rmb(); |
| 1946 | commit_count = v_read(config, |
| 1947 | &buf->commit_cold[sb_index].cc_sb); |
| 1948 | /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */ |
| 1949 | smp_rmb(); |
| 1950 | if (unlikely(offset_cmp != v_read(config, &buf->offset))) { |
| 1951 | /* |
| 1952 | * The reserve counter have been concurrently updated |
| 1953 | * while we read the commit counter. This means the |
| 1954 | * commit counter we read might not match buf->offset |
| 1955 | * due to concurrent update. We therefore need to retry. |
| 1956 | */ |
| 1957 | goto retry; |
| 1958 | } |
| 1959 | reserve_commit_diff = |
| 1960 | (buf_trunc(offsets->begin, chan) |
| 1961 | >> chan->backend.num_subbuf_order) |
| 1962 | - (commit_count & chan->commit_count_mask); |
| 1963 | if (likely(reserve_commit_diff == 0)) { |
| 1964 | /* Next subbuffer not being written to. */ |
| 1965 | if (unlikely(config->mode != RING_BUFFER_OVERWRITE && |
| 1966 | subbuf_trunc(offsets->begin, chan) |
| 1967 | - subbuf_trunc((unsigned long) |
| 1968 | atomic_long_read(&buf->consumed), chan) |
| 1969 | >= chan->backend.buf_size)) { |
| 1970 | /* |
| 1971 | * We do not overwrite non consumed buffers |
| 1972 | * and we are full : record is lost. |
| 1973 | */ |
| 1974 | v_inc(config, &buf->records_lost_full); |
| 1975 | return -ENOBUFS; |
| 1976 | } else { |
| 1977 | /* |
| 1978 | * Next subbuffer not being written to, and we |
| 1979 | * are either in overwrite mode or the buffer is |
| 1980 | * not full. It's safe to write in this new |
| 1981 | * subbuffer. |
| 1982 | */ |
| 1983 | } |
| 1984 | } else { |
| 1985 | /* |
| 1986 | * Next subbuffer reserve offset does not match the |
| 1987 | * commit offset, and this did not involve update to the |
| 1988 | * reserve counter. Drop record in producer-consumer and |
| 1989 | * overwrite mode. Caused by either a writer OOPS or |
| 1990 | * too many nested writes over a reserve/commit pair. |
| 1991 | */ |
| 1992 | v_inc(config, &buf->records_lost_wrap); |
| 1993 | return -EIO; |
| 1994 | } |
| 1995 | offsets->size = |
| 1996 | config->cb.record_header_size(config, chan, |
| 1997 | offsets->begin, |
| 1998 | &offsets->pre_header_padding, |
| 1999 | ctx); |
| 2000 | offsets->size += |
| 2001 | lib_ring_buffer_align(offsets->begin + offsets->size, |
| 2002 | ctx->largest_align) |
| 2003 | + ctx->data_size; |
| 2004 | if (unlikely(subbuf_offset(offsets->begin, chan) |
| 2005 | + offsets->size > chan->backend.subbuf_size)) { |
| 2006 | /* |
| 2007 | * Record too big for subbuffers, report error, don't |
| 2008 | * complete the sub-buffer switch. |
| 2009 | */ |
| 2010 | v_inc(config, &buf->records_lost_big); |
| 2011 | return -ENOSPC; |
| 2012 | } else { |
| 2013 | /* |
| 2014 | * We just made a successful buffer switch and the |
| 2015 | * record fits in the new subbuffer. Let's write. |
| 2016 | */ |
| 2017 | } |
| 2018 | } else { |
| 2019 | /* |
| 2020 | * Record fits in the current buffer and we are not on a switch |
| 2021 | * boundary. It's safe to write. |
| 2022 | */ |
| 2023 | } |
| 2024 | offsets->end = offsets->begin + offsets->size; |
| 2025 | |
| 2026 | if (unlikely(subbuf_offset(offsets->end, chan) == 0)) { |
| 2027 | /* |
| 2028 | * The offset_end will fall at the very beginning of the next |
| 2029 | * subbuffer. |
| 2030 | */ |
| 2031 | offsets->switch_new_end = 1; /* For offsets->begin */ |
| 2032 | } |
| 2033 | return 0; |
| 2034 | } |
| 2035 | |
| 2036 | static struct lib_ring_buffer *get_current_buf(struct channel *chan, int cpu) |
| 2037 | { |
| 2038 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 2039 | |
| 2040 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 2041 | return per_cpu_ptr(chan->backend.buf, cpu); |
| 2042 | else |
| 2043 | return chan->backend.buf; |
| 2044 | } |
| 2045 | |
| 2046 | void lib_ring_buffer_lost_event_too_big(struct channel *chan) |
| 2047 | { |
| 2048 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 2049 | struct lib_ring_buffer *buf = get_current_buf(chan, smp_processor_id()); |
| 2050 | |
| 2051 | v_inc(config, &buf->records_lost_big); |
| 2052 | } |
| 2053 | EXPORT_SYMBOL_GPL(lib_ring_buffer_lost_event_too_big); |
| 2054 | |
| 2055 | /** |
| 2056 | * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer. |
| 2057 | * @ctx: ring buffer context. |
| 2058 | * |
| 2059 | * Return : -NOBUFS if not enough space, -ENOSPC if event size too large, |
| 2060 | * -EIO for other errors, else returns 0. |
| 2061 | * It will take care of sub-buffer switching. |
| 2062 | */ |
| 2063 | int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx *ctx) |
| 2064 | { |
| 2065 | struct channel *chan = ctx->chan; |
| 2066 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 2067 | struct lib_ring_buffer *buf; |
| 2068 | struct switch_offsets offsets; |
| 2069 | int ret; |
| 2070 | |
| 2071 | ctx->buf = buf = get_current_buf(chan, ctx->cpu); |
| 2072 | offsets.size = 0; |
| 2073 | |
| 2074 | do { |
| 2075 | ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets, |
| 2076 | ctx); |
| 2077 | if (unlikely(ret)) |
| 2078 | return ret; |
| 2079 | } while (unlikely(v_cmpxchg(config, &buf->offset, offsets.old, |
| 2080 | offsets.end) |
| 2081 | != offsets.old)); |
| 2082 | |
| 2083 | /* |
| 2084 | * Atomically update last_tsc. This update races against concurrent |
| 2085 | * atomic updates, but the race will always cause supplementary full TSC |
| 2086 | * records, never the opposite (missing a full TSC record when it would |
| 2087 | * be needed). |
| 2088 | */ |
| 2089 | save_last_tsc(config, buf, ctx->tsc); |
| 2090 | |
| 2091 | /* |
| 2092 | * Push the reader if necessary |
| 2093 | */ |
| 2094 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1); |
| 2095 | |
| 2096 | /* |
| 2097 | * Clear noref flag for this subbuffer. |
| 2098 | */ |
| 2099 | lib_ring_buffer_clear_noref(config, &buf->backend, |
| 2100 | subbuf_index(offsets.end - 1, chan)); |
| 2101 | |
| 2102 | /* |
| 2103 | * Switch old subbuffer if needed. |
| 2104 | */ |
| 2105 | if (unlikely(offsets.switch_old_end)) { |
| 2106 | lib_ring_buffer_clear_noref(config, &buf->backend, |
| 2107 | subbuf_index(offsets.old - 1, chan)); |
| 2108 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc); |
| 2109 | } |
| 2110 | |
| 2111 | /* |
| 2112 | * Populate new subbuffer. |
| 2113 | */ |
| 2114 | if (unlikely(offsets.switch_new_start)) |
| 2115 | lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc); |
| 2116 | |
| 2117 | if (unlikely(offsets.switch_new_end)) |
| 2118 | lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc); |
| 2119 | |
| 2120 | ctx->slot_size = offsets.size; |
| 2121 | ctx->pre_offset = offsets.begin; |
| 2122 | ctx->buf_offset = offsets.begin + offsets.pre_header_padding; |
| 2123 | return 0; |
| 2124 | } |
| 2125 | EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow); |
| 2126 | |
| 2127 | static |
| 2128 | void lib_ring_buffer_vmcore_check_deliver(const struct lib_ring_buffer_config *config, |
| 2129 | struct lib_ring_buffer *buf, |
| 2130 | unsigned long commit_count, |
| 2131 | unsigned long idx) |
| 2132 | { |
| 2133 | if (config->oops == RING_BUFFER_OOPS_CONSISTENCY) |
| 2134 | v_set(config, &buf->commit_hot[idx].seq, commit_count); |
| 2135 | } |
| 2136 | |
| 2137 | /* |
| 2138 | * The ring buffer can count events recorded and overwritten per buffer, |
| 2139 | * but it is disabled by default due to its performance overhead. |
| 2140 | */ |
| 2141 | #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS |
| 2142 | static |
| 2143 | void deliver_count_events(const struct lib_ring_buffer_config *config, |
| 2144 | struct lib_ring_buffer *buf, |
| 2145 | unsigned long idx) |
| 2146 | { |
| 2147 | v_add(config, subbuffer_get_records_count(config, |
| 2148 | &buf->backend, idx), |
| 2149 | &buf->records_count); |
| 2150 | v_add(config, subbuffer_count_records_overrun(config, |
| 2151 | &buf->backend, idx), |
| 2152 | &buf->records_overrun); |
| 2153 | } |
| 2154 | #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */ |
| 2155 | static |
| 2156 | void deliver_count_events(const struct lib_ring_buffer_config *config, |
| 2157 | struct lib_ring_buffer *buf, |
| 2158 | unsigned long idx) |
| 2159 | { |
| 2160 | } |
| 2161 | #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */ |
| 2162 | |
| 2163 | |
| 2164 | void lib_ring_buffer_check_deliver_slow(const struct lib_ring_buffer_config *config, |
| 2165 | struct lib_ring_buffer *buf, |
| 2166 | struct channel *chan, |
| 2167 | unsigned long offset, |
| 2168 | unsigned long commit_count, |
| 2169 | unsigned long idx, |
| 2170 | u64 tsc) |
| 2171 | { |
| 2172 | unsigned long old_commit_count = commit_count |
| 2173 | - chan->backend.subbuf_size; |
| 2174 | |
| 2175 | /* |
| 2176 | * If we succeeded at updating cc_sb below, we are the subbuffer |
| 2177 | * writer delivering the subbuffer. Deals with concurrent |
| 2178 | * updates of the "cc" value without adding a add_return atomic |
| 2179 | * operation to the fast path. |
| 2180 | * |
| 2181 | * We are doing the delivery in two steps: |
| 2182 | * - First, we cmpxchg() cc_sb to the new value |
| 2183 | * old_commit_count + 1. This ensures that we are the only |
| 2184 | * subbuffer user successfully filling the subbuffer, but we |
| 2185 | * do _not_ set the cc_sb value to "commit_count" yet. |
| 2186 | * Therefore, other writers that would wrap around the ring |
| 2187 | * buffer and try to start writing to our subbuffer would |
| 2188 | * have to drop records, because it would appear as |
| 2189 | * non-filled. |
| 2190 | * We therefore have exclusive access to the subbuffer control |
| 2191 | * structures. This mutual exclusion with other writers is |
| 2192 | * crucially important to perform record overruns count in |
| 2193 | * flight recorder mode locklessly. |
| 2194 | * - When we are ready to release the subbuffer (either for |
| 2195 | * reading or for overrun by other writers), we simply set the |
| 2196 | * cc_sb value to "commit_count" and perform delivery. |
| 2197 | * |
| 2198 | * The subbuffer size is least 2 bytes (minimum size: 1 page). |
| 2199 | * This guarantees that old_commit_count + 1 != commit_count. |
| 2200 | */ |
| 2201 | |
| 2202 | /* |
| 2203 | * Order prior updates to reserve count prior to the |
| 2204 | * commit_cold cc_sb update. |
| 2205 | */ |
| 2206 | smp_wmb(); |
| 2207 | if (likely(v_cmpxchg(config, &buf->commit_cold[idx].cc_sb, |
| 2208 | old_commit_count, old_commit_count + 1) |
| 2209 | == old_commit_count)) { |
| 2210 | /* |
| 2211 | * Start of exclusive subbuffer access. We are |
| 2212 | * guaranteed to be the last writer in this subbuffer |
| 2213 | * and any other writer trying to access this subbuffer |
| 2214 | * in this state is required to drop records. |
| 2215 | */ |
| 2216 | deliver_count_events(config, buf, idx); |
| 2217 | config->cb.buffer_end(buf, tsc, idx, |
| 2218 | lib_ring_buffer_get_data_size(config, |
| 2219 | buf, |
| 2220 | idx)); |
| 2221 | |
| 2222 | /* |
| 2223 | * Increment the packet counter while we have exclusive |
| 2224 | * access. |
| 2225 | */ |
| 2226 | subbuffer_inc_packet_count(config, &buf->backend, idx); |
| 2227 | |
| 2228 | /* |
| 2229 | * Set noref flag and offset for this subbuffer id. |
| 2230 | * Contains a memory barrier that ensures counter stores |
| 2231 | * are ordered before set noref and offset. |
| 2232 | */ |
| 2233 | lib_ring_buffer_set_noref_offset(config, &buf->backend, idx, |
| 2234 | buf_trunc_val(offset, chan)); |
| 2235 | |
| 2236 | /* |
| 2237 | * Order set_noref and record counter updates before the |
| 2238 | * end of subbuffer exclusive access. Orders with |
| 2239 | * respect to writers coming into the subbuffer after |
| 2240 | * wrap around, and also order wrt concurrent readers. |
| 2241 | */ |
| 2242 | smp_mb(); |
| 2243 | /* End of exclusive subbuffer access */ |
| 2244 | v_set(config, &buf->commit_cold[idx].cc_sb, |
| 2245 | commit_count); |
| 2246 | /* |
| 2247 | * Order later updates to reserve count after |
| 2248 | * the commit_cold cc_sb update. |
| 2249 | */ |
| 2250 | smp_wmb(); |
| 2251 | lib_ring_buffer_vmcore_check_deliver(config, buf, |
| 2252 | commit_count, idx); |
| 2253 | |
| 2254 | /* |
| 2255 | * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free. |
| 2256 | */ |
| 2257 | if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER |
| 2258 | && atomic_long_read(&buf->active_readers) |
| 2259 | && lib_ring_buffer_poll_deliver(config, buf, chan)) { |
| 2260 | wake_up_interruptible(&buf->read_wait); |
| 2261 | wake_up_interruptible(&chan->read_wait); |
| 2262 | } |
| 2263 | |
| 2264 | } |
| 2265 | } |
| 2266 | EXPORT_SYMBOL_GPL(lib_ring_buffer_check_deliver_slow); |
| 2267 | |
| 2268 | int __init init_lib_ring_buffer_frontend(void) |
| 2269 | { |
| 2270 | int cpu; |
| 2271 | |
| 2272 | for_each_possible_cpu(cpu) |
| 2273 | spin_lock_init(&per_cpu(ring_buffer_nohz_lock, cpu)); |
| 2274 | return 0; |
| 2275 | } |
| 2276 | |
| 2277 | module_init(init_lib_ring_buffer_frontend); |
| 2278 | |
| 2279 | void __exit exit_lib_ring_buffer_frontend(void) |
| 2280 | { |
| 2281 | } |
| 2282 | |
| 2283 | module_exit(exit_lib_ring_buffer_frontend); |