| 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 | |
| 64 | /* |
| 65 | * Internal structure representing offsets to use at a sub-buffer switch. |
| 66 | */ |
| 67 | struct switch_offsets { |
| 68 | unsigned long begin, end, old; |
| 69 | size_t pre_header_padding, size; |
| 70 | unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1, |
| 71 | switch_old_end:1; |
| 72 | }; |
| 73 | |
| 74 | #ifdef CONFIG_NO_HZ |
| 75 | enum tick_nohz_val { |
| 76 | TICK_NOHZ_STOP, |
| 77 | TICK_NOHZ_FLUSH, |
| 78 | TICK_NOHZ_RESTART, |
| 79 | }; |
| 80 | |
| 81 | static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier); |
| 82 | #endif /* CONFIG_NO_HZ */ |
| 83 | |
| 84 | static DEFINE_PER_CPU(spinlock_t, ring_buffer_nohz_lock); |
| 85 | |
| 86 | DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting); |
| 87 | EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting); |
| 88 | |
| 89 | static |
| 90 | void lib_ring_buffer_print_errors(struct channel *chan, |
| 91 | struct lib_ring_buffer *buf, int cpu); |
| 92 | |
| 93 | /* |
| 94 | * Must be called under cpu hotplug protection. |
| 95 | */ |
| 96 | void lib_ring_buffer_free(struct lib_ring_buffer *buf) |
| 97 | { |
| 98 | struct channel *chan = buf->backend.chan; |
| 99 | |
| 100 | lib_ring_buffer_print_errors(chan, buf, buf->backend.cpu); |
| 101 | kfree(buf->commit_hot); |
| 102 | kfree(buf->commit_cold); |
| 103 | |
| 104 | lib_ring_buffer_backend_free(&buf->backend); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * lib_ring_buffer_reset - Reset ring buffer to initial values. |
| 109 | * @buf: Ring buffer. |
| 110 | * |
| 111 | * Effectively empty the ring buffer. Should be called when the buffer is not |
| 112 | * used for writing. The ring buffer can be opened for reading, but the reader |
| 113 | * should not be using the iterator concurrently with reset. The previous |
| 114 | * current iterator record is reset. |
| 115 | */ |
| 116 | void lib_ring_buffer_reset(struct lib_ring_buffer *buf) |
| 117 | { |
| 118 | struct channel *chan = buf->backend.chan; |
| 119 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 120 | unsigned int i; |
| 121 | |
| 122 | /* |
| 123 | * Reset iterator first. It will put the subbuffer if it currently holds |
| 124 | * it. |
| 125 | */ |
| 126 | lib_ring_buffer_iterator_reset(buf); |
| 127 | v_set(config, &buf->offset, 0); |
| 128 | for (i = 0; i < chan->backend.num_subbuf; i++) { |
| 129 | v_set(config, &buf->commit_hot[i].cc, 0); |
| 130 | v_set(config, &buf->commit_hot[i].seq, 0); |
| 131 | v_set(config, &buf->commit_cold[i].cc_sb, 0); |
| 132 | } |
| 133 | atomic_long_set(&buf->consumed, 0); |
| 134 | atomic_set(&buf->record_disabled, 0); |
| 135 | v_set(config, &buf->last_tsc, 0); |
| 136 | lib_ring_buffer_backend_reset(&buf->backend); |
| 137 | /* Don't reset number of active readers */ |
| 138 | v_set(config, &buf->records_lost_full, 0); |
| 139 | v_set(config, &buf->records_lost_wrap, 0); |
| 140 | v_set(config, &buf->records_lost_big, 0); |
| 141 | v_set(config, &buf->records_count, 0); |
| 142 | v_set(config, &buf->records_overrun, 0); |
| 143 | buf->finalized = 0; |
| 144 | } |
| 145 | EXPORT_SYMBOL_GPL(lib_ring_buffer_reset); |
| 146 | |
| 147 | /** |
| 148 | * channel_reset - Reset channel to initial values. |
| 149 | * @chan: Channel. |
| 150 | * |
| 151 | * Effectively empty the channel. Should be called when the channel is not used |
| 152 | * for writing. The channel can be opened for reading, but the reader should not |
| 153 | * be using the iterator concurrently with reset. The previous current iterator |
| 154 | * record is reset. |
| 155 | */ |
| 156 | void channel_reset(struct channel *chan) |
| 157 | { |
| 158 | /* |
| 159 | * Reset iterators first. Will put the subbuffer if held for reading. |
| 160 | */ |
| 161 | channel_iterator_reset(chan); |
| 162 | atomic_set(&chan->record_disabled, 0); |
| 163 | /* Don't reset commit_count_mask, still valid */ |
| 164 | channel_backend_reset(&chan->backend); |
| 165 | /* Don't reset switch/read timer interval */ |
| 166 | /* Don't reset notifiers and notifier enable bits */ |
| 167 | /* Don't reset reader reference count */ |
| 168 | } |
| 169 | EXPORT_SYMBOL_GPL(channel_reset); |
| 170 | |
| 171 | /* |
| 172 | * Must be called under cpu hotplug protection. |
| 173 | */ |
| 174 | int lib_ring_buffer_create(struct lib_ring_buffer *buf, |
| 175 | struct channel_backend *chanb, int cpu) |
| 176 | { |
| 177 | const struct lib_ring_buffer_config *config = &chanb->config; |
| 178 | struct channel *chan = container_of(chanb, struct channel, backend); |
| 179 | void *priv = chanb->priv; |
| 180 | size_t subbuf_header_size; |
| 181 | u64 tsc; |
| 182 | int ret; |
| 183 | |
| 184 | /* Test for cpu hotplug */ |
| 185 | if (buf->backend.allocated) |
| 186 | return 0; |
| 187 | |
| 188 | /* |
| 189 | * Paranoia: per cpu dynamic allocation is not officially documented as |
| 190 | * zeroing the memory, so let's do it here too, just in case. |
| 191 | */ |
| 192 | memset(buf, 0, sizeof(*buf)); |
| 193 | |
| 194 | ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend, cpu); |
| 195 | if (ret) |
| 196 | return ret; |
| 197 | |
| 198 | buf->commit_hot = |
| 199 | kzalloc_node(ALIGN(sizeof(*buf->commit_hot) |
| 200 | * chan->backend.num_subbuf, |
| 201 | 1 << INTERNODE_CACHE_SHIFT), |
| 202 | GFP_KERNEL, cpu_to_node(max(cpu, 0))); |
| 203 | if (!buf->commit_hot) { |
| 204 | ret = -ENOMEM; |
| 205 | goto free_chanbuf; |
| 206 | } |
| 207 | |
| 208 | buf->commit_cold = |
| 209 | kzalloc_node(ALIGN(sizeof(*buf->commit_cold) |
| 210 | * chan->backend.num_subbuf, |
| 211 | 1 << INTERNODE_CACHE_SHIFT), |
| 212 | GFP_KERNEL, cpu_to_node(max(cpu, 0))); |
| 213 | if (!buf->commit_cold) { |
| 214 | ret = -ENOMEM; |
| 215 | goto free_commit; |
| 216 | } |
| 217 | |
| 218 | init_waitqueue_head(&buf->read_wait); |
| 219 | init_waitqueue_head(&buf->write_wait); |
| 220 | raw_spin_lock_init(&buf->raw_tick_nohz_spinlock); |
| 221 | |
| 222 | /* |
| 223 | * Write the subbuffer header for first subbuffer so we know the total |
| 224 | * duration of data gathering. |
| 225 | */ |
| 226 | subbuf_header_size = config->cb.subbuffer_header_size(); |
| 227 | v_set(config, &buf->offset, subbuf_header_size); |
| 228 | subbuffer_id_clear_noref(config, &buf->backend.buf_wsb[0].id); |
| 229 | tsc = config->cb.ring_buffer_clock_read(buf->backend.chan); |
| 230 | config->cb.buffer_begin(buf, tsc, 0); |
| 231 | v_add(config, subbuf_header_size, &buf->commit_hot[0].cc); |
| 232 | |
| 233 | if (config->cb.buffer_create) { |
| 234 | ret = config->cb.buffer_create(buf, priv, cpu, chanb->name); |
| 235 | if (ret) |
| 236 | goto free_init; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Ensure the buffer is ready before setting it to allocated and setting |
| 241 | * the cpumask. |
| 242 | * Used for cpu hotplug vs cpumask iteration. |
| 243 | */ |
| 244 | smp_wmb(); |
| 245 | buf->backend.allocated = 1; |
| 246 | |
| 247 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 248 | CHAN_WARN_ON(chan, cpumask_test_cpu(cpu, |
| 249 | chan->backend.cpumask)); |
| 250 | cpumask_set_cpu(cpu, chan->backend.cpumask); |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | |
| 255 | /* Error handling */ |
| 256 | free_init: |
| 257 | kfree(buf->commit_cold); |
| 258 | free_commit: |
| 259 | kfree(buf->commit_hot); |
| 260 | free_chanbuf: |
| 261 | lib_ring_buffer_backend_free(&buf->backend); |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | static void switch_buffer_timer(unsigned long data) |
| 266 | { |
| 267 | struct lib_ring_buffer *buf = (struct lib_ring_buffer *)data; |
| 268 | struct channel *chan = buf->backend.chan; |
| 269 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 270 | |
| 271 | /* |
| 272 | * Only flush buffers periodically if readers are active. |
| 273 | */ |
| 274 | if (atomic_long_read(&buf->active_readers)) |
| 275 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 276 | |
| 277 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 278 | mod_timer_pinned(&buf->switch_timer, |
| 279 | jiffies + chan->switch_timer_interval); |
| 280 | else |
| 281 | mod_timer(&buf->switch_timer, |
| 282 | jiffies + chan->switch_timer_interval); |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Called with ring_buffer_nohz_lock held for per-cpu buffers. |
| 287 | */ |
| 288 | static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer *buf) |
| 289 | { |
| 290 | struct channel *chan = buf->backend.chan; |
| 291 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 292 | |
| 293 | if (!chan->switch_timer_interval || buf->switch_timer_enabled) |
| 294 | return; |
| 295 | init_timer(&buf->switch_timer); |
| 296 | buf->switch_timer.function = switch_buffer_timer; |
| 297 | buf->switch_timer.expires = jiffies + chan->switch_timer_interval; |
| 298 | buf->switch_timer.data = (unsigned long)buf; |
| 299 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 300 | add_timer_on(&buf->switch_timer, buf->backend.cpu); |
| 301 | else |
| 302 | add_timer(&buf->switch_timer); |
| 303 | buf->switch_timer_enabled = 1; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Called with ring_buffer_nohz_lock held for per-cpu buffers. |
| 308 | */ |
| 309 | static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer *buf) |
| 310 | { |
| 311 | struct channel *chan = buf->backend.chan; |
| 312 | |
| 313 | if (!chan->switch_timer_interval || !buf->switch_timer_enabled) |
| 314 | return; |
| 315 | |
| 316 | del_timer_sync(&buf->switch_timer); |
| 317 | buf->switch_timer_enabled = 0; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Polling timer to check the channels for data. |
| 322 | */ |
| 323 | static void read_buffer_timer(unsigned long data) |
| 324 | { |
| 325 | struct lib_ring_buffer *buf = (struct lib_ring_buffer *)data; |
| 326 | struct channel *chan = buf->backend.chan; |
| 327 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 328 | |
| 329 | CHAN_WARN_ON(chan, !buf->backend.allocated); |
| 330 | |
| 331 | if (atomic_long_read(&buf->active_readers) |
| 332 | && lib_ring_buffer_poll_deliver(config, buf, chan)) { |
| 333 | wake_up_interruptible(&buf->read_wait); |
| 334 | wake_up_interruptible(&chan->read_wait); |
| 335 | } |
| 336 | |
| 337 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 338 | mod_timer_pinned(&buf->read_timer, |
| 339 | jiffies + chan->read_timer_interval); |
| 340 | else |
| 341 | mod_timer(&buf->read_timer, |
| 342 | jiffies + chan->read_timer_interval); |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Called with ring_buffer_nohz_lock held for per-cpu buffers. |
| 347 | */ |
| 348 | static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer *buf) |
| 349 | { |
| 350 | struct channel *chan = buf->backend.chan; |
| 351 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 352 | |
| 353 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER |
| 354 | || !chan->read_timer_interval |
| 355 | || buf->read_timer_enabled) |
| 356 | return; |
| 357 | |
| 358 | init_timer(&buf->read_timer); |
| 359 | buf->read_timer.function = read_buffer_timer; |
| 360 | buf->read_timer.expires = jiffies + chan->read_timer_interval; |
| 361 | buf->read_timer.data = (unsigned long)buf; |
| 362 | |
| 363 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 364 | add_timer_on(&buf->read_timer, buf->backend.cpu); |
| 365 | else |
| 366 | add_timer(&buf->read_timer); |
| 367 | buf->read_timer_enabled = 1; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Called with ring_buffer_nohz_lock held for per-cpu buffers. |
| 372 | */ |
| 373 | static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer *buf) |
| 374 | { |
| 375 | struct channel *chan = buf->backend.chan; |
| 376 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 377 | |
| 378 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER |
| 379 | || !chan->read_timer_interval |
| 380 | || !buf->read_timer_enabled) |
| 381 | return; |
| 382 | |
| 383 | del_timer_sync(&buf->read_timer); |
| 384 | /* |
| 385 | * do one more check to catch data that has been written in the last |
| 386 | * timer period. |
| 387 | */ |
| 388 | if (lib_ring_buffer_poll_deliver(config, buf, chan)) { |
| 389 | wake_up_interruptible(&buf->read_wait); |
| 390 | wake_up_interruptible(&chan->read_wait); |
| 391 | } |
| 392 | buf->read_timer_enabled = 0; |
| 393 | } |
| 394 | |
| 395 | #ifdef CONFIG_HOTPLUG_CPU |
| 396 | /** |
| 397 | * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback |
| 398 | * @nb: notifier block |
| 399 | * @action: hotplug action to take |
| 400 | * @hcpu: CPU number |
| 401 | * |
| 402 | * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD) |
| 403 | */ |
| 404 | static |
| 405 | int lib_ring_buffer_cpu_hp_callback(struct notifier_block *nb, |
| 406 | unsigned long action, |
| 407 | void *hcpu) |
| 408 | { |
| 409 | unsigned int cpu = (unsigned long)hcpu; |
| 410 | struct channel *chan = container_of(nb, struct channel, |
| 411 | cpu_hp_notifier); |
| 412 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu); |
| 413 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 414 | |
| 415 | if (!chan->cpu_hp_enable) |
| 416 | return NOTIFY_DONE; |
| 417 | |
| 418 | CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL); |
| 419 | |
| 420 | switch (action) { |
| 421 | case CPU_DOWN_FAILED: |
| 422 | case CPU_DOWN_FAILED_FROZEN: |
| 423 | case CPU_ONLINE: |
| 424 | case CPU_ONLINE_FROZEN: |
| 425 | wake_up_interruptible(&chan->hp_wait); |
| 426 | lib_ring_buffer_start_switch_timer(buf); |
| 427 | lib_ring_buffer_start_read_timer(buf); |
| 428 | return NOTIFY_OK; |
| 429 | |
| 430 | case CPU_DOWN_PREPARE: |
| 431 | case CPU_DOWN_PREPARE_FROZEN: |
| 432 | lib_ring_buffer_stop_switch_timer(buf); |
| 433 | lib_ring_buffer_stop_read_timer(buf); |
| 434 | return NOTIFY_OK; |
| 435 | |
| 436 | case CPU_DEAD: |
| 437 | case CPU_DEAD_FROZEN: |
| 438 | /* |
| 439 | * Performing a buffer switch on a remote CPU. Performed by |
| 440 | * the CPU responsible for doing the hotunplug after the target |
| 441 | * CPU stopped running completely. Ensures that all data |
| 442 | * from that remote CPU is flushed. |
| 443 | */ |
| 444 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 445 | return NOTIFY_OK; |
| 446 | |
| 447 | default: |
| 448 | return NOTIFY_DONE; |
| 449 | } |
| 450 | } |
| 451 | #endif |
| 452 | |
| 453 | #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) |
| 454 | /* |
| 455 | * For per-cpu buffers, call the reader wakeups before switching the buffer, so |
| 456 | * that wake-up-tracing generated events are flushed before going idle (in |
| 457 | * tick_nohz). We test if the spinlock is locked to deal with the race where |
| 458 | * readers try to sample the ring buffer before we perform the switch. We let |
| 459 | * the readers retry in that case. If there is data in the buffer, the wake up |
| 460 | * is going to forbid the CPU running the reader thread from going idle. |
| 461 | */ |
| 462 | static int notrace ring_buffer_tick_nohz_callback(struct notifier_block *nb, |
| 463 | unsigned long val, |
| 464 | void *data) |
| 465 | { |
| 466 | struct channel *chan = container_of(nb, struct channel, |
| 467 | tick_nohz_notifier); |
| 468 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 469 | struct lib_ring_buffer *buf; |
| 470 | int cpu = smp_processor_id(); |
| 471 | |
| 472 | if (config->alloc != RING_BUFFER_ALLOC_PER_CPU) { |
| 473 | /* |
| 474 | * We don't support keeping the system idle with global buffers |
| 475 | * and streaming active. In order to do so, we would need to |
| 476 | * sample a non-nohz-cpumask racelessly with the nohz updates |
| 477 | * without adding synchronization overhead to nohz. Leave this |
| 478 | * use-case out for now. |
| 479 | */ |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | buf = channel_get_ring_buffer(config, chan, cpu); |
| 484 | switch (val) { |
| 485 | case TICK_NOHZ_FLUSH: |
| 486 | raw_spin_lock(&buf->raw_tick_nohz_spinlock); |
| 487 | if (config->wakeup == RING_BUFFER_WAKEUP_BY_TIMER |
| 488 | && chan->read_timer_interval |
| 489 | && atomic_long_read(&buf->active_readers) |
| 490 | && (lib_ring_buffer_poll_deliver(config, buf, chan) |
| 491 | || lib_ring_buffer_pending_data(config, buf, chan))) { |
| 492 | wake_up_interruptible(&buf->read_wait); |
| 493 | wake_up_interruptible(&chan->read_wait); |
| 494 | } |
| 495 | if (chan->switch_timer_interval) |
| 496 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 497 | raw_spin_unlock(&buf->raw_tick_nohz_spinlock); |
| 498 | break; |
| 499 | case TICK_NOHZ_STOP: |
| 500 | spin_lock(&__get_cpu_var(ring_buffer_nohz_lock)); |
| 501 | lib_ring_buffer_stop_switch_timer(buf); |
| 502 | lib_ring_buffer_stop_read_timer(buf); |
| 503 | spin_unlock(&__get_cpu_var(ring_buffer_nohz_lock)); |
| 504 | break; |
| 505 | case TICK_NOHZ_RESTART: |
| 506 | spin_lock(&__get_cpu_var(ring_buffer_nohz_lock)); |
| 507 | lib_ring_buffer_start_read_timer(buf); |
| 508 | lib_ring_buffer_start_switch_timer(buf); |
| 509 | spin_unlock(&__get_cpu_var(ring_buffer_nohz_lock)); |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | void notrace lib_ring_buffer_tick_nohz_flush(void) |
| 517 | { |
| 518 | atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_FLUSH, |
| 519 | NULL); |
| 520 | } |
| 521 | |
| 522 | void notrace lib_ring_buffer_tick_nohz_stop(void) |
| 523 | { |
| 524 | atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_STOP, |
| 525 | NULL); |
| 526 | } |
| 527 | |
| 528 | void notrace lib_ring_buffer_tick_nohz_restart(void) |
| 529 | { |
| 530 | atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_RESTART, |
| 531 | NULL); |
| 532 | } |
| 533 | #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */ |
| 534 | |
| 535 | /* |
| 536 | * Holds CPU hotplug. |
| 537 | */ |
| 538 | static void channel_unregister_notifiers(struct channel *chan) |
| 539 | { |
| 540 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 541 | int cpu; |
| 542 | |
| 543 | channel_iterator_unregister_notifiers(chan); |
| 544 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 545 | #ifdef CONFIG_NO_HZ |
| 546 | /* |
| 547 | * Remove the nohz notifier first, so we are certain we stop |
| 548 | * the timers. |
| 549 | */ |
| 550 | atomic_notifier_chain_unregister(&tick_nohz_notifier, |
| 551 | &chan->tick_nohz_notifier); |
| 552 | /* |
| 553 | * ring_buffer_nohz_lock will not be needed below, because |
| 554 | * we just removed the notifiers, which were the only source of |
| 555 | * concurrency. |
| 556 | */ |
| 557 | #endif /* CONFIG_NO_HZ */ |
| 558 | #ifdef CONFIG_HOTPLUG_CPU |
| 559 | get_online_cpus(); |
| 560 | chan->cpu_hp_enable = 0; |
| 561 | for_each_online_cpu(cpu) { |
| 562 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, |
| 563 | cpu); |
| 564 | lib_ring_buffer_stop_switch_timer(buf); |
| 565 | lib_ring_buffer_stop_read_timer(buf); |
| 566 | } |
| 567 | put_online_cpus(); |
| 568 | unregister_cpu_notifier(&chan->cpu_hp_notifier); |
| 569 | #else |
| 570 | for_each_possible_cpu(cpu) { |
| 571 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, |
| 572 | cpu); |
| 573 | lib_ring_buffer_stop_switch_timer(buf); |
| 574 | lib_ring_buffer_stop_read_timer(buf); |
| 575 | } |
| 576 | #endif |
| 577 | } else { |
| 578 | struct lib_ring_buffer *buf = chan->backend.buf; |
| 579 | |
| 580 | lib_ring_buffer_stop_switch_timer(buf); |
| 581 | lib_ring_buffer_stop_read_timer(buf); |
| 582 | } |
| 583 | channel_backend_unregister_notifiers(&chan->backend); |
| 584 | } |
| 585 | |
| 586 | static void channel_free(struct channel *chan) |
| 587 | { |
| 588 | if (chan->backend.release_priv_ops) { |
| 589 | chan->backend.release_priv_ops(chan->backend.priv_ops); |
| 590 | } |
| 591 | channel_iterator_free(chan); |
| 592 | channel_backend_free(&chan->backend); |
| 593 | kfree(chan); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * channel_create - Create channel. |
| 598 | * @config: ring buffer instance configuration |
| 599 | * @name: name of the channel |
| 600 | * @priv: ring buffer client private data |
| 601 | * @buf_addr: pointer the the beginning of the preallocated buffer contiguous |
| 602 | * address mapping. It is used only by RING_BUFFER_STATIC |
| 603 | * configuration. It can be set to NULL for other backends. |
| 604 | * @subbuf_size: subbuffer size |
| 605 | * @num_subbuf: number of subbuffers |
| 606 | * @switch_timer_interval: Time interval (in us) to fill sub-buffers with |
| 607 | * padding to let readers get those sub-buffers. |
| 608 | * Used for live streaming. |
| 609 | * @read_timer_interval: Time interval (in us) to wake up pending readers. |
| 610 | * |
| 611 | * Holds cpu hotplug. |
| 612 | * Returns NULL on failure. |
| 613 | */ |
| 614 | struct channel *channel_create(const struct lib_ring_buffer_config *config, |
| 615 | const char *name, void *priv, void *buf_addr, |
| 616 | size_t subbuf_size, |
| 617 | size_t num_subbuf, unsigned int switch_timer_interval, |
| 618 | unsigned int read_timer_interval) |
| 619 | { |
| 620 | int ret, cpu; |
| 621 | struct channel *chan; |
| 622 | |
| 623 | if (lib_ring_buffer_check_config(config, switch_timer_interval, |
| 624 | read_timer_interval)) |
| 625 | return NULL; |
| 626 | |
| 627 | chan = kzalloc(sizeof(struct channel), GFP_KERNEL); |
| 628 | if (!chan) |
| 629 | return NULL; |
| 630 | |
| 631 | ret = channel_backend_init(&chan->backend, name, config, priv, |
| 632 | subbuf_size, num_subbuf); |
| 633 | if (ret) |
| 634 | goto error; |
| 635 | |
| 636 | ret = channel_iterator_init(chan); |
| 637 | if (ret) |
| 638 | goto error_free_backend; |
| 639 | |
| 640 | chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order); |
| 641 | chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval); |
| 642 | chan->read_timer_interval = usecs_to_jiffies(read_timer_interval); |
| 643 | kref_init(&chan->ref); |
| 644 | init_waitqueue_head(&chan->read_wait); |
| 645 | init_waitqueue_head(&chan->hp_wait); |
| 646 | |
| 647 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 648 | #if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) |
| 649 | /* Only benefit from NO_HZ idle with per-cpu buffers for now. */ |
| 650 | chan->tick_nohz_notifier.notifier_call = |
| 651 | ring_buffer_tick_nohz_callback; |
| 652 | chan->tick_nohz_notifier.priority = ~0U; |
| 653 | atomic_notifier_chain_register(&tick_nohz_notifier, |
| 654 | &chan->tick_nohz_notifier); |
| 655 | #endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */ |
| 656 | |
| 657 | /* |
| 658 | * In case of non-hotplug cpu, if the ring-buffer is allocated |
| 659 | * in early initcall, it will not be notified of secondary cpus. |
| 660 | * In that off case, we need to allocate for all possible cpus. |
| 661 | */ |
| 662 | #ifdef CONFIG_HOTPLUG_CPU |
| 663 | chan->cpu_hp_notifier.notifier_call = |
| 664 | lib_ring_buffer_cpu_hp_callback; |
| 665 | chan->cpu_hp_notifier.priority = 6; |
| 666 | register_cpu_notifier(&chan->cpu_hp_notifier); |
| 667 | |
| 668 | get_online_cpus(); |
| 669 | for_each_online_cpu(cpu) { |
| 670 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, |
| 671 | cpu); |
| 672 | spin_lock(&per_cpu(ring_buffer_nohz_lock, cpu)); |
| 673 | lib_ring_buffer_start_switch_timer(buf); |
| 674 | lib_ring_buffer_start_read_timer(buf); |
| 675 | spin_unlock(&per_cpu(ring_buffer_nohz_lock, cpu)); |
| 676 | } |
| 677 | chan->cpu_hp_enable = 1; |
| 678 | put_online_cpus(); |
| 679 | #else |
| 680 | for_each_possible_cpu(cpu) { |
| 681 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, |
| 682 | cpu); |
| 683 | spin_lock(&per_cpu(ring_buffer_nohz_lock, cpu)); |
| 684 | lib_ring_buffer_start_switch_timer(buf); |
| 685 | lib_ring_buffer_start_read_timer(buf); |
| 686 | spin_unlock(&per_cpu(ring_buffer_nohz_lock, cpu)); |
| 687 | } |
| 688 | #endif |
| 689 | } else { |
| 690 | struct lib_ring_buffer *buf = chan->backend.buf; |
| 691 | |
| 692 | lib_ring_buffer_start_switch_timer(buf); |
| 693 | lib_ring_buffer_start_read_timer(buf); |
| 694 | } |
| 695 | |
| 696 | return chan; |
| 697 | |
| 698 | error_free_backend: |
| 699 | channel_backend_free(&chan->backend); |
| 700 | error: |
| 701 | kfree(chan); |
| 702 | return NULL; |
| 703 | } |
| 704 | EXPORT_SYMBOL_GPL(channel_create); |
| 705 | |
| 706 | static |
| 707 | void channel_release(struct kref *kref) |
| 708 | { |
| 709 | struct channel *chan = container_of(kref, struct channel, ref); |
| 710 | channel_free(chan); |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * channel_destroy - Finalize, wait for q.s. and destroy channel. |
| 715 | * @chan: channel to destroy |
| 716 | * |
| 717 | * Holds cpu hotplug. |
| 718 | * Call "destroy" callback, finalize channels, and then decrement the |
| 719 | * channel reference count. Note that when readers have completed data |
| 720 | * consumption of finalized channels, get_subbuf() will return -ENODATA. |
| 721 | * They should release their handle at that point. Returns the private |
| 722 | * data pointer. |
| 723 | */ |
| 724 | void *channel_destroy(struct channel *chan) |
| 725 | { |
| 726 | int cpu; |
| 727 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 728 | void *priv; |
| 729 | |
| 730 | channel_unregister_notifiers(chan); |
| 731 | |
| 732 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 733 | /* |
| 734 | * No need to hold cpu hotplug, because all notifiers have been |
| 735 | * unregistered. |
| 736 | */ |
| 737 | for_each_channel_cpu(cpu, chan) { |
| 738 | struct lib_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, |
| 739 | cpu); |
| 740 | |
| 741 | if (config->cb.buffer_finalize) |
| 742 | config->cb.buffer_finalize(buf, |
| 743 | chan->backend.priv, |
| 744 | cpu); |
| 745 | if (buf->backend.allocated) |
| 746 | lib_ring_buffer_switch_slow(buf, SWITCH_FLUSH); |
| 747 | /* |
| 748 | * Perform flush before writing to finalized. |
| 749 | */ |
| 750 | smp_wmb(); |
| 751 | ACCESS_ONCE(buf->finalized) = 1; |
| 752 | wake_up_interruptible(&buf->read_wait); |
| 753 | } |
| 754 | } else { |
| 755 | struct lib_ring_buffer *buf = chan->backend.buf; |
| 756 | |
| 757 | if (config->cb.buffer_finalize) |
| 758 | config->cb.buffer_finalize(buf, chan->backend.priv, -1); |
| 759 | if (buf->backend.allocated) |
| 760 | lib_ring_buffer_switch_slow(buf, SWITCH_FLUSH); |
| 761 | /* |
| 762 | * Perform flush before writing to finalized. |
| 763 | */ |
| 764 | smp_wmb(); |
| 765 | ACCESS_ONCE(buf->finalized) = 1; |
| 766 | wake_up_interruptible(&buf->read_wait); |
| 767 | } |
| 768 | ACCESS_ONCE(chan->finalized) = 1; |
| 769 | wake_up_interruptible(&chan->hp_wait); |
| 770 | wake_up_interruptible(&chan->read_wait); |
| 771 | priv = chan->backend.priv; |
| 772 | kref_put(&chan->ref, channel_release); |
| 773 | return priv; |
| 774 | } |
| 775 | EXPORT_SYMBOL_GPL(channel_destroy); |
| 776 | |
| 777 | struct lib_ring_buffer *channel_get_ring_buffer( |
| 778 | const struct lib_ring_buffer_config *config, |
| 779 | struct channel *chan, int cpu) |
| 780 | { |
| 781 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) |
| 782 | return chan->backend.buf; |
| 783 | else |
| 784 | return per_cpu_ptr(chan->backend.buf, cpu); |
| 785 | } |
| 786 | EXPORT_SYMBOL_GPL(channel_get_ring_buffer); |
| 787 | |
| 788 | int lib_ring_buffer_open_read(struct lib_ring_buffer *buf) |
| 789 | { |
| 790 | struct channel *chan = buf->backend.chan; |
| 791 | |
| 792 | if (!atomic_long_add_unless(&buf->active_readers, 1, 1)) |
| 793 | return -EBUSY; |
| 794 | kref_get(&chan->ref); |
| 795 | smp_mb__after_atomic_inc(); |
| 796 | return 0; |
| 797 | } |
| 798 | EXPORT_SYMBOL_GPL(lib_ring_buffer_open_read); |
| 799 | |
| 800 | void lib_ring_buffer_release_read(struct lib_ring_buffer *buf) |
| 801 | { |
| 802 | struct channel *chan = buf->backend.chan; |
| 803 | |
| 804 | CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1); |
| 805 | smp_mb__before_atomic_dec(); |
| 806 | atomic_long_dec(&buf->active_readers); |
| 807 | kref_put(&chan->ref, channel_release); |
| 808 | } |
| 809 | EXPORT_SYMBOL_GPL(lib_ring_buffer_release_read); |
| 810 | |
| 811 | /* |
| 812 | * Promote compiler barrier to a smp_mb(). |
| 813 | * For the specific ring buffer case, this IPI call should be removed if the |
| 814 | * architecture does not reorder writes. This should eventually be provided by |
| 815 | * a separate architecture-specific infrastructure. |
| 816 | */ |
| 817 | static void remote_mb(void *info) |
| 818 | { |
| 819 | smp_mb(); |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read) |
| 824 | * @buf: ring buffer |
| 825 | * @consumed: consumed count indicating the position where to read |
| 826 | * @produced: produced count, indicates position when to stop reading |
| 827 | * |
| 828 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no |
| 829 | * data to read at consumed position, or 0 if the get operation succeeds. |
| 830 | * Busy-loop trying to get data if the tick_nohz sequence lock is held. |
| 831 | */ |
| 832 | |
| 833 | int lib_ring_buffer_snapshot(struct lib_ring_buffer *buf, |
| 834 | unsigned long *consumed, unsigned long *produced) |
| 835 | { |
| 836 | struct channel *chan = buf->backend.chan; |
| 837 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 838 | unsigned long consumed_cur, write_offset; |
| 839 | int finalized; |
| 840 | |
| 841 | retry: |
| 842 | finalized = ACCESS_ONCE(buf->finalized); |
| 843 | /* |
| 844 | * Read finalized before counters. |
| 845 | */ |
| 846 | smp_rmb(); |
| 847 | consumed_cur = atomic_long_read(&buf->consumed); |
| 848 | /* |
| 849 | * No need to issue a memory barrier between consumed count read and |
| 850 | * write offset read, because consumed count can only change |
| 851 | * concurrently in overwrite mode, and we keep a sequence counter |
| 852 | * identifier derived from the write offset to check we are getting |
| 853 | * the same sub-buffer we are expecting (the sub-buffers are atomically |
| 854 | * "tagged" upon writes, tags are checked upon read). |
| 855 | */ |
| 856 | write_offset = v_read(config, &buf->offset); |
| 857 | |
| 858 | /* |
| 859 | * Check that we are not about to read the same subbuffer in |
| 860 | * which the writer head is. |
| 861 | */ |
| 862 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) |
| 863 | == 0) |
| 864 | goto nodata; |
| 865 | |
| 866 | *consumed = consumed_cur; |
| 867 | *produced = subbuf_trunc(write_offset, chan); |
| 868 | |
| 869 | return 0; |
| 870 | |
| 871 | nodata: |
| 872 | /* |
| 873 | * The memory barriers __wait_event()/wake_up_interruptible() take care |
| 874 | * of "raw_spin_is_locked" memory ordering. |
| 875 | */ |
| 876 | if (finalized) |
| 877 | return -ENODATA; |
| 878 | else if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock)) |
| 879 | goto retry; |
| 880 | else |
| 881 | return -EAGAIN; |
| 882 | } |
| 883 | EXPORT_SYMBOL_GPL(lib_ring_buffer_snapshot); |
| 884 | |
| 885 | /** |
| 886 | * lib_ring_buffer_put_snapshot - move consumed counter forward |
| 887 | * |
| 888 | * Should only be called from consumer context. |
| 889 | * @buf: ring buffer |
| 890 | * @consumed_new: new consumed count value |
| 891 | */ |
| 892 | void lib_ring_buffer_move_consumer(struct lib_ring_buffer *buf, |
| 893 | unsigned long consumed_new) |
| 894 | { |
| 895 | struct lib_ring_buffer_backend *bufb = &buf->backend; |
| 896 | struct channel *chan = bufb->chan; |
| 897 | unsigned long consumed; |
| 898 | |
| 899 | CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1); |
| 900 | |
| 901 | /* |
| 902 | * Only push the consumed value forward. |
| 903 | * If the consumed cmpxchg fails, this is because we have been pushed by |
| 904 | * the writer in flight recorder mode. |
| 905 | */ |
| 906 | consumed = atomic_long_read(&buf->consumed); |
| 907 | while ((long) consumed - (long) consumed_new < 0) |
| 908 | consumed = atomic_long_cmpxchg(&buf->consumed, consumed, |
| 909 | consumed_new); |
| 910 | /* Wake-up the metadata producer */ |
| 911 | wake_up_interruptible(&buf->write_wait); |
| 912 | } |
| 913 | EXPORT_SYMBOL_GPL(lib_ring_buffer_move_consumer); |
| 914 | |
| 915 | /** |
| 916 | * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading |
| 917 | * @buf: ring buffer |
| 918 | * @consumed: consumed count indicating the position where to read |
| 919 | * |
| 920 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no |
| 921 | * data to read at consumed position, or 0 if the get operation succeeds. |
| 922 | * Busy-loop trying to get data if the tick_nohz sequence lock is held. |
| 923 | */ |
| 924 | int lib_ring_buffer_get_subbuf(struct lib_ring_buffer *buf, |
| 925 | unsigned long consumed) |
| 926 | { |
| 927 | struct channel *chan = buf->backend.chan; |
| 928 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 929 | unsigned long consumed_cur, consumed_idx, commit_count, write_offset; |
| 930 | int ret; |
| 931 | int finalized; |
| 932 | |
| 933 | if (buf->get_subbuf) { |
| 934 | /* |
| 935 | * Reader is trying to get a subbuffer twice. |
| 936 | */ |
| 937 | CHAN_WARN_ON(chan, 1); |
| 938 | return -EBUSY; |
| 939 | } |
| 940 | retry: |
| 941 | finalized = ACCESS_ONCE(buf->finalized); |
| 942 | /* |
| 943 | * Read finalized before counters. |
| 944 | */ |
| 945 | smp_rmb(); |
| 946 | consumed_cur = atomic_long_read(&buf->consumed); |
| 947 | consumed_idx = subbuf_index(consumed, chan); |
| 948 | commit_count = v_read(config, &buf->commit_cold[consumed_idx].cc_sb); |
| 949 | /* |
| 950 | * Make sure we read the commit count before reading the buffer |
| 951 | * data and the write offset. Correct consumed offset ordering |
| 952 | * wrt commit count is insured by the use of cmpxchg to update |
| 953 | * the consumed offset. |
| 954 | * smp_call_function_single can fail if the remote CPU is offline, |
| 955 | * this is OK because then there is no wmb to execute there. |
| 956 | * If our thread is executing on the same CPU as the on the buffers |
| 957 | * belongs to, we don't have to synchronize it at all. If we are |
| 958 | * migrated, the scheduler will take care of the memory barriers. |
| 959 | * Normally, smp_call_function_single() should ensure program order when |
| 960 | * executing the remote function, which implies that it surrounds the |
| 961 | * function execution with : |
| 962 | * smp_mb() |
| 963 | * send IPI |
| 964 | * csd_lock_wait |
| 965 | * recv IPI |
| 966 | * smp_mb() |
| 967 | * exec. function |
| 968 | * smp_mb() |
| 969 | * csd unlock |
| 970 | * smp_mb() |
| 971 | * |
| 972 | * However, smp_call_function_single() does not seem to clearly execute |
| 973 | * such barriers. It depends on spinlock semantic to provide the barrier |
| 974 | * before executing the IPI and, when busy-looping, csd_lock_wait only |
| 975 | * executes smp_mb() when it has to wait for the other CPU. |
| 976 | * |
| 977 | * I don't trust this code. Therefore, let's add the smp_mb() sequence |
| 978 | * required ourself, even if duplicated. It has no performance impact |
| 979 | * anyway. |
| 980 | * |
| 981 | * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs |
| 982 | * read and write vs write. They do not ensure core synchronization. We |
| 983 | * really have to ensure total order between the 3 barriers running on |
| 984 | * the 2 CPUs. |
| 985 | */ |
| 986 | if (config->ipi == RING_BUFFER_IPI_BARRIER) { |
| 987 | if (config->sync == RING_BUFFER_SYNC_PER_CPU |
| 988 | && config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
| 989 | if (raw_smp_processor_id() != buf->backend.cpu) { |
| 990 | /* Total order with IPI handler smp_mb() */ |
| 991 | smp_mb(); |
| 992 | smp_call_function_single(buf->backend.cpu, |
| 993 | remote_mb, NULL, 1); |
| 994 | /* Total order with IPI handler smp_mb() */ |
| 995 | smp_mb(); |
| 996 | } |
| 997 | } else { |
| 998 | /* Total order with IPI handler smp_mb() */ |
| 999 | smp_mb(); |
| 1000 | smp_call_function(remote_mb, NULL, 1); |
| 1001 | /* Total order with IPI handler smp_mb() */ |
| 1002 | smp_mb(); |
| 1003 | } |
| 1004 | } else { |
| 1005 | /* |
| 1006 | * Local rmb to match the remote wmb to read the commit count |
| 1007 | * before the buffer data and the write offset. |
| 1008 | */ |
| 1009 | smp_rmb(); |
| 1010 | } |
| 1011 | |
| 1012 | write_offset = v_read(config, &buf->offset); |
| 1013 | |
| 1014 | /* |
| 1015 | * Check that the buffer we are getting is after or at consumed_cur |
| 1016 | * position. |
| 1017 | */ |
| 1018 | if ((long) subbuf_trunc(consumed, chan) |
| 1019 | - (long) subbuf_trunc(consumed_cur, chan) < 0) |
| 1020 | goto nodata; |
| 1021 | |
| 1022 | /* |
| 1023 | * Check that the subbuffer we are trying to consume has been |
| 1024 | * already fully committed. |
| 1025 | */ |
| 1026 | if (((commit_count - chan->backend.subbuf_size) |
| 1027 | & chan->commit_count_mask) |
| 1028 | - (buf_trunc(consumed, chan) |
| 1029 | >> chan->backend.num_subbuf_order) |
| 1030 | != 0) |
| 1031 | goto nodata; |
| 1032 | |
| 1033 | /* |
| 1034 | * Check that we are not about to read the same subbuffer in |
| 1035 | * which the writer head is. |
| 1036 | */ |
| 1037 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed, chan) |
| 1038 | == 0) |
| 1039 | goto nodata; |
| 1040 | |
| 1041 | /* |
| 1042 | * Failure to get the subbuffer causes a busy-loop retry without going |
| 1043 | * to a wait queue. These are caused by short-lived race windows where |
| 1044 | * the writer is getting access to a subbuffer we were trying to get |
| 1045 | * access to. Also checks that the "consumed" buffer count we are |
| 1046 | * looking for matches the one contained in the subbuffer id. |
| 1047 | */ |
| 1048 | ret = update_read_sb_index(config, &buf->backend, &chan->backend, |
| 1049 | consumed_idx, buf_trunc_val(consumed, chan)); |
| 1050 | if (ret) |
| 1051 | goto retry; |
| 1052 | subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id); |
| 1053 | |
| 1054 | buf->get_subbuf_consumed = consumed; |
| 1055 | buf->get_subbuf = 1; |
| 1056 | |
| 1057 | return 0; |
| 1058 | |
| 1059 | nodata: |
| 1060 | /* |
| 1061 | * The memory barriers __wait_event()/wake_up_interruptible() take care |
| 1062 | * of "raw_spin_is_locked" memory ordering. |
| 1063 | */ |
| 1064 | if (finalized) |
| 1065 | return -ENODATA; |
| 1066 | else if (raw_spin_is_locked(&buf->raw_tick_nohz_spinlock)) |
| 1067 | goto retry; |
| 1068 | else |
| 1069 | return -EAGAIN; |
| 1070 | } |
| 1071 | EXPORT_SYMBOL_GPL(lib_ring_buffer_get_subbuf); |
| 1072 | |
| 1073 | /** |
| 1074 | * lib_ring_buffer_put_subbuf - release exclusive subbuffer access |
| 1075 | * @buf: ring buffer |
| 1076 | */ |
| 1077 | void lib_ring_buffer_put_subbuf(struct lib_ring_buffer *buf) |
| 1078 | { |
| 1079 | struct lib_ring_buffer_backend *bufb = &buf->backend; |
| 1080 | struct channel *chan = bufb->chan; |
| 1081 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1082 | unsigned long read_sb_bindex, consumed_idx, consumed; |
| 1083 | |
| 1084 | CHAN_WARN_ON(chan, atomic_long_read(&buf->active_readers) != 1); |
| 1085 | |
| 1086 | if (!buf->get_subbuf) { |
| 1087 | /* |
| 1088 | * Reader puts a subbuffer it did not get. |
| 1089 | */ |
| 1090 | CHAN_WARN_ON(chan, 1); |
| 1091 | return; |
| 1092 | } |
| 1093 | consumed = buf->get_subbuf_consumed; |
| 1094 | buf->get_subbuf = 0; |
| 1095 | |
| 1096 | /* |
| 1097 | * Clear the records_unread counter. (overruns counter) |
| 1098 | * Can still be non-zero if a file reader simply grabbed the data |
| 1099 | * without using iterators. |
| 1100 | * Can be below zero if an iterator is used on a snapshot more than |
| 1101 | * once. |
| 1102 | */ |
| 1103 | read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); |
| 1104 | v_add(config, v_read(config, |
| 1105 | &bufb->array[read_sb_bindex]->records_unread), |
| 1106 | &bufb->records_read); |
| 1107 | v_set(config, &bufb->array[read_sb_bindex]->records_unread, 0); |
| 1108 | CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE |
| 1109 | && subbuffer_id_is_noref(config, bufb->buf_rsb.id)); |
| 1110 | subbuffer_id_set_noref(config, &bufb->buf_rsb.id); |
| 1111 | |
| 1112 | /* |
| 1113 | * Exchange the reader subbuffer with the one we put in its place in the |
| 1114 | * writer subbuffer table. Expect the original consumed count. If |
| 1115 | * update_read_sb_index fails, this is because the writer updated the |
| 1116 | * subbuffer concurrently. We should therefore keep the subbuffer we |
| 1117 | * currently have: it has become invalid to try reading this sub-buffer |
| 1118 | * consumed count value anyway. |
| 1119 | */ |
| 1120 | consumed_idx = subbuf_index(consumed, chan); |
| 1121 | update_read_sb_index(config, &buf->backend, &chan->backend, |
| 1122 | consumed_idx, buf_trunc_val(consumed, chan)); |
| 1123 | /* |
| 1124 | * update_read_sb_index return value ignored. Don't exchange sub-buffer |
| 1125 | * if the writer concurrently updated it. |
| 1126 | */ |
| 1127 | } |
| 1128 | EXPORT_SYMBOL_GPL(lib_ring_buffer_put_subbuf); |
| 1129 | |
| 1130 | /* |
| 1131 | * cons_offset is an iterator on all subbuffer offsets between the reader |
| 1132 | * position and the writer position. (inclusive) |
| 1133 | */ |
| 1134 | static |
| 1135 | void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer *buf, |
| 1136 | struct channel *chan, |
| 1137 | unsigned long cons_offset, |
| 1138 | int cpu) |
| 1139 | { |
| 1140 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1141 | unsigned long cons_idx, commit_count, commit_count_sb; |
| 1142 | |
| 1143 | cons_idx = subbuf_index(cons_offset, chan); |
| 1144 | commit_count = v_read(config, &buf->commit_hot[cons_idx].cc); |
| 1145 | commit_count_sb = v_read(config, &buf->commit_cold[cons_idx].cc_sb); |
| 1146 | |
| 1147 | if (subbuf_offset(commit_count, chan) != 0) |
| 1148 | printk(KERN_WARNING |
| 1149 | "ring buffer %s, cpu %d: " |
| 1150 | "commit count in subbuffer %lu,\n" |
| 1151 | "expecting multiples of %lu bytes\n" |
| 1152 | " [ %lu bytes committed, %lu bytes reader-visible ]\n", |
| 1153 | chan->backend.name, cpu, cons_idx, |
| 1154 | chan->backend.subbuf_size, |
| 1155 | commit_count, commit_count_sb); |
| 1156 | |
| 1157 | printk(KERN_DEBUG "ring buffer: %s, cpu %d: %lu bytes committed\n", |
| 1158 | chan->backend.name, cpu, commit_count); |
| 1159 | } |
| 1160 | |
| 1161 | static |
| 1162 | void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer *buf, |
| 1163 | struct channel *chan, |
| 1164 | void *priv, int cpu) |
| 1165 | { |
| 1166 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1167 | unsigned long write_offset, cons_offset; |
| 1168 | |
| 1169 | /* |
| 1170 | * No need to order commit_count, write_offset and cons_offset reads |
| 1171 | * because we execute at teardown when no more writer nor reader |
| 1172 | * references are left. |
| 1173 | */ |
| 1174 | write_offset = v_read(config, &buf->offset); |
| 1175 | cons_offset = atomic_long_read(&buf->consumed); |
| 1176 | if (write_offset != cons_offset) |
| 1177 | printk(KERN_DEBUG |
| 1178 | "ring buffer %s, cpu %d: " |
| 1179 | "non-consumed data\n" |
| 1180 | " [ %lu bytes written, %lu bytes read ]\n", |
| 1181 | chan->backend.name, cpu, write_offset, cons_offset); |
| 1182 | |
| 1183 | for (cons_offset = atomic_long_read(&buf->consumed); |
| 1184 | (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset), |
| 1185 | chan) |
| 1186 | - cons_offset) > 0; |
| 1187 | cons_offset = subbuf_align(cons_offset, chan)) |
| 1188 | lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset, |
| 1189 | cpu); |
| 1190 | } |
| 1191 | |
| 1192 | static |
| 1193 | void lib_ring_buffer_print_errors(struct channel *chan, |
| 1194 | struct lib_ring_buffer *buf, int cpu) |
| 1195 | { |
| 1196 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1197 | void *priv = chan->backend.priv; |
| 1198 | |
| 1199 | if (!strcmp(chan->backend.name, "relay-metadata")) { |
| 1200 | printk(KERN_DEBUG "ring buffer %s: %lu records written, " |
| 1201 | "%lu records overrun\n", |
| 1202 | chan->backend.name, |
| 1203 | v_read(config, &buf->records_count), |
| 1204 | v_read(config, &buf->records_overrun)); |
| 1205 | } else { |
| 1206 | printk(KERN_DEBUG "ring buffer %s, cpu %d: %lu records written, " |
| 1207 | "%lu records overrun\n", |
| 1208 | chan->backend.name, cpu, |
| 1209 | v_read(config, &buf->records_count), |
| 1210 | v_read(config, &buf->records_overrun)); |
| 1211 | |
| 1212 | if (v_read(config, &buf->records_lost_full) |
| 1213 | || v_read(config, &buf->records_lost_wrap) |
| 1214 | || v_read(config, &buf->records_lost_big)) |
| 1215 | printk(KERN_WARNING |
| 1216 | "ring buffer %s, cpu %d: records were lost. Caused by:\n" |
| 1217 | " [ %lu buffer full, %lu nest buffer wrap-around, " |
| 1218 | "%lu event too big ]\n", |
| 1219 | chan->backend.name, cpu, |
| 1220 | v_read(config, &buf->records_lost_full), |
| 1221 | v_read(config, &buf->records_lost_wrap), |
| 1222 | v_read(config, &buf->records_lost_big)); |
| 1223 | } |
| 1224 | lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu); |
| 1225 | } |
| 1226 | |
| 1227 | /* |
| 1228 | * lib_ring_buffer_switch_old_start: Populate old subbuffer header. |
| 1229 | * |
| 1230 | * Only executed when the buffer is finalized, in SWITCH_FLUSH. |
| 1231 | */ |
| 1232 | static |
| 1233 | void lib_ring_buffer_switch_old_start(struct lib_ring_buffer *buf, |
| 1234 | struct channel *chan, |
| 1235 | struct switch_offsets *offsets, |
| 1236 | u64 tsc) |
| 1237 | { |
| 1238 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1239 | unsigned long oldidx = subbuf_index(offsets->old, chan); |
| 1240 | unsigned long commit_count; |
| 1241 | |
| 1242 | config->cb.buffer_begin(buf, tsc, oldidx); |
| 1243 | |
| 1244 | /* |
| 1245 | * Order all writes to buffer before the commit count update that will |
| 1246 | * determine that the subbuffer is full. |
| 1247 | */ |
| 1248 | if (config->ipi == RING_BUFFER_IPI_BARRIER) { |
| 1249 | /* |
| 1250 | * Must write slot data before incrementing commit count. This |
| 1251 | * compiler barrier is upgraded into a smp_mb() by the IPI sent |
| 1252 | * by get_subbuf(). |
| 1253 | */ |
| 1254 | barrier(); |
| 1255 | } else |
| 1256 | smp_wmb(); |
| 1257 | v_add(config, config->cb.subbuffer_header_size(), |
| 1258 | &buf->commit_hot[oldidx].cc); |
| 1259 | commit_count = v_read(config, &buf->commit_hot[oldidx].cc); |
| 1260 | /* Check if the written buffer has to be delivered */ |
| 1261 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old, |
| 1262 | commit_count, oldidx, tsc); |
| 1263 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
| 1264 | offsets->old + config->cb.subbuffer_header_size(), |
| 1265 | commit_count); |
| 1266 | } |
| 1267 | |
| 1268 | /* |
| 1269 | * lib_ring_buffer_switch_old_end: switch old subbuffer |
| 1270 | * |
| 1271 | * Note : offset_old should never be 0 here. It is ok, because we never perform |
| 1272 | * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller |
| 1273 | * increments the offset_old value when doing a SWITCH_FLUSH on an empty |
| 1274 | * subbuffer. |
| 1275 | */ |
| 1276 | static |
| 1277 | void lib_ring_buffer_switch_old_end(struct lib_ring_buffer *buf, |
| 1278 | struct channel *chan, |
| 1279 | struct switch_offsets *offsets, |
| 1280 | u64 tsc) |
| 1281 | { |
| 1282 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1283 | unsigned long oldidx = subbuf_index(offsets->old - 1, chan); |
| 1284 | unsigned long commit_count, padding_size, data_size; |
| 1285 | |
| 1286 | data_size = subbuf_offset(offsets->old - 1, chan) + 1; |
| 1287 | padding_size = chan->backend.subbuf_size - data_size; |
| 1288 | subbuffer_set_data_size(config, &buf->backend, oldidx, data_size); |
| 1289 | |
| 1290 | /* |
| 1291 | * Order all writes to buffer before the commit count update that will |
| 1292 | * determine that the subbuffer is full. |
| 1293 | */ |
| 1294 | if (config->ipi == RING_BUFFER_IPI_BARRIER) { |
| 1295 | /* |
| 1296 | * Must write slot data before incrementing commit count. This |
| 1297 | * compiler barrier is upgraded into a smp_mb() by the IPI sent |
| 1298 | * by get_subbuf(). |
| 1299 | */ |
| 1300 | barrier(); |
| 1301 | } else |
| 1302 | smp_wmb(); |
| 1303 | v_add(config, padding_size, &buf->commit_hot[oldidx].cc); |
| 1304 | commit_count = v_read(config, &buf->commit_hot[oldidx].cc); |
| 1305 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1, |
| 1306 | commit_count, oldidx, tsc); |
| 1307 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
| 1308 | offsets->old + padding_size, commit_count); |
| 1309 | } |
| 1310 | |
| 1311 | /* |
| 1312 | * lib_ring_buffer_switch_new_start: Populate new subbuffer. |
| 1313 | * |
| 1314 | * This code can be executed unordered : writers may already have written to the |
| 1315 | * sub-buffer before this code gets executed, caution. The commit makes sure |
| 1316 | * that this code is executed before the deliver of this sub-buffer. |
| 1317 | */ |
| 1318 | static |
| 1319 | void lib_ring_buffer_switch_new_start(struct lib_ring_buffer *buf, |
| 1320 | struct channel *chan, |
| 1321 | struct switch_offsets *offsets, |
| 1322 | u64 tsc) |
| 1323 | { |
| 1324 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1325 | unsigned long beginidx = subbuf_index(offsets->begin, chan); |
| 1326 | unsigned long commit_count; |
| 1327 | |
| 1328 | config->cb.buffer_begin(buf, tsc, beginidx); |
| 1329 | |
| 1330 | /* |
| 1331 | * Order all writes to buffer before the commit count update that will |
| 1332 | * determine that the subbuffer is full. |
| 1333 | */ |
| 1334 | if (config->ipi == RING_BUFFER_IPI_BARRIER) { |
| 1335 | /* |
| 1336 | * Must write slot data before incrementing commit count. This |
| 1337 | * compiler barrier is upgraded into a smp_mb() by the IPI sent |
| 1338 | * by get_subbuf(). |
| 1339 | */ |
| 1340 | barrier(); |
| 1341 | } else |
| 1342 | smp_wmb(); |
| 1343 | v_add(config, config->cb.subbuffer_header_size(), |
| 1344 | &buf->commit_hot[beginidx].cc); |
| 1345 | commit_count = v_read(config, &buf->commit_hot[beginidx].cc); |
| 1346 | /* Check if the written buffer has to be delivered */ |
| 1347 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin, |
| 1348 | commit_count, beginidx, tsc); |
| 1349 | lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx, |
| 1350 | offsets->begin + config->cb.subbuffer_header_size(), |
| 1351 | commit_count); |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * lib_ring_buffer_switch_new_end: finish switching current subbuffer |
| 1356 | * |
| 1357 | * Calls subbuffer_set_data_size() to set the data size of the current |
| 1358 | * sub-buffer. We do not need to perform check_deliver nor commit here, |
| 1359 | * since this task will be done by the "commit" of the event for which |
| 1360 | * we are currently doing the space reservation. |
| 1361 | */ |
| 1362 | static |
| 1363 | void lib_ring_buffer_switch_new_end(struct lib_ring_buffer *buf, |
| 1364 | struct channel *chan, |
| 1365 | struct switch_offsets *offsets, |
| 1366 | u64 tsc) |
| 1367 | { |
| 1368 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1369 | unsigned long endidx, data_size; |
| 1370 | |
| 1371 | endidx = subbuf_index(offsets->end - 1, chan); |
| 1372 | data_size = subbuf_offset(offsets->end - 1, chan) + 1; |
| 1373 | subbuffer_set_data_size(config, &buf->backend, endidx, data_size); |
| 1374 | } |
| 1375 | |
| 1376 | /* |
| 1377 | * Returns : |
| 1378 | * 0 if ok |
| 1379 | * !0 if execution must be aborted. |
| 1380 | */ |
| 1381 | static |
| 1382 | int lib_ring_buffer_try_switch_slow(enum switch_mode mode, |
| 1383 | struct lib_ring_buffer *buf, |
| 1384 | struct channel *chan, |
| 1385 | struct switch_offsets *offsets, |
| 1386 | u64 *tsc) |
| 1387 | { |
| 1388 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1389 | unsigned long off, reserve_commit_diff; |
| 1390 | |
| 1391 | offsets->begin = v_read(config, &buf->offset); |
| 1392 | offsets->old = offsets->begin; |
| 1393 | offsets->switch_old_start = 0; |
| 1394 | off = subbuf_offset(offsets->begin, chan); |
| 1395 | |
| 1396 | *tsc = config->cb.ring_buffer_clock_read(chan); |
| 1397 | |
| 1398 | /* |
| 1399 | * Ensure we flush the header of an empty subbuffer when doing the |
| 1400 | * finalize (SWITCH_FLUSH). This ensures that we end up knowing the |
| 1401 | * total data gathering duration even if there were no records saved |
| 1402 | * after the last buffer switch. |
| 1403 | * In SWITCH_ACTIVE mode, switch the buffer when it contains events. |
| 1404 | * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of |
| 1405 | * subbuffer header as appropriate. |
| 1406 | * The next record that reserves space will be responsible for |
| 1407 | * populating the following subbuffer header. We choose not to populate |
| 1408 | * the next subbuffer header here because we want to be able to use |
| 1409 | * SWITCH_ACTIVE for periodical buffer flush and CPU tick_nohz stop |
| 1410 | * buffer flush, which must guarantee that all the buffer content |
| 1411 | * (records and header timestamps) are visible to the reader. This is |
| 1412 | * required for quiescence guarantees for the fusion merge. |
| 1413 | */ |
| 1414 | if (mode != SWITCH_FLUSH && !off) |
| 1415 | return -1; /* we do not have to switch : buffer is empty */ |
| 1416 | |
| 1417 | if (unlikely(off == 0)) { |
| 1418 | unsigned long sb_index, commit_count; |
| 1419 | |
| 1420 | /* |
| 1421 | * We are performing a SWITCH_FLUSH. At this stage, there are no |
| 1422 | * concurrent writes into the buffer. |
| 1423 | * |
| 1424 | * The client does not save any header information. Don't |
| 1425 | * switch empty subbuffer on finalize, because it is invalid to |
| 1426 | * deliver a completely empty subbuffer. |
| 1427 | */ |
| 1428 | if (!config->cb.subbuffer_header_size()) |
| 1429 | return -1; |
| 1430 | |
| 1431 | /* Test new buffer integrity */ |
| 1432 | sb_index = subbuf_index(offsets->begin, chan); |
| 1433 | commit_count = v_read(config, |
| 1434 | &buf->commit_cold[sb_index].cc_sb); |
| 1435 | reserve_commit_diff = |
| 1436 | (buf_trunc(offsets->begin, chan) |
| 1437 | >> chan->backend.num_subbuf_order) |
| 1438 | - (commit_count & chan->commit_count_mask); |
| 1439 | if (likely(reserve_commit_diff == 0)) { |
| 1440 | /* Next subbuffer not being written to. */ |
| 1441 | if (unlikely(config->mode != RING_BUFFER_OVERWRITE && |
| 1442 | subbuf_trunc(offsets->begin, chan) |
| 1443 | - subbuf_trunc((unsigned long) |
| 1444 | atomic_long_read(&buf->consumed), chan) |
| 1445 | >= chan->backend.buf_size)) { |
| 1446 | /* |
| 1447 | * We do not overwrite non consumed buffers |
| 1448 | * and we are full : don't switch. |
| 1449 | */ |
| 1450 | return -1; |
| 1451 | } else { |
| 1452 | /* |
| 1453 | * Next subbuffer not being written to, and we |
| 1454 | * are either in overwrite mode or the buffer is |
| 1455 | * not full. It's safe to write in this new |
| 1456 | * subbuffer. |
| 1457 | */ |
| 1458 | } |
| 1459 | } else { |
| 1460 | /* |
| 1461 | * Next subbuffer reserve offset does not match the |
| 1462 | * commit offset. Don't perform switch in |
| 1463 | * producer-consumer and overwrite mode. Caused by |
| 1464 | * either a writer OOPS or too many nested writes over a |
| 1465 | * reserve/commit pair. |
| 1466 | */ |
| 1467 | return -1; |
| 1468 | } |
| 1469 | |
| 1470 | /* |
| 1471 | * Need to write the subbuffer start header on finalize. |
| 1472 | */ |
| 1473 | offsets->switch_old_start = 1; |
| 1474 | } |
| 1475 | offsets->begin = subbuf_align(offsets->begin, chan); |
| 1476 | /* Note: old points to the next subbuf at offset 0 */ |
| 1477 | offsets->end = offsets->begin; |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | * Force a sub-buffer switch. This operation is completely reentrant : can be |
| 1483 | * called while tracing is active with absolutely no lock held. |
| 1484 | * |
| 1485 | * Note, however, that as a v_cmpxchg is used for some atomic |
| 1486 | * operations, this function must be called from the CPU which owns the buffer |
| 1487 | * for a ACTIVE flush. |
| 1488 | */ |
| 1489 | void lib_ring_buffer_switch_slow(struct lib_ring_buffer *buf, enum switch_mode mode) |
| 1490 | { |
| 1491 | struct channel *chan = buf->backend.chan; |
| 1492 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1493 | struct switch_offsets offsets; |
| 1494 | unsigned long oldidx; |
| 1495 | u64 tsc; |
| 1496 | |
| 1497 | offsets.size = 0; |
| 1498 | |
| 1499 | /* |
| 1500 | * Perform retryable operations. |
| 1501 | */ |
| 1502 | do { |
| 1503 | if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets, |
| 1504 | &tsc)) |
| 1505 | return; /* Switch not needed */ |
| 1506 | } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end) |
| 1507 | != offsets.old); |
| 1508 | |
| 1509 | /* |
| 1510 | * Atomically update last_tsc. This update races against concurrent |
| 1511 | * atomic updates, but the race will always cause supplementary full TSC |
| 1512 | * records, never the opposite (missing a full TSC record when it would |
| 1513 | * be needed). |
| 1514 | */ |
| 1515 | save_last_tsc(config, buf, tsc); |
| 1516 | |
| 1517 | /* |
| 1518 | * Push the reader if necessary |
| 1519 | */ |
| 1520 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old); |
| 1521 | |
| 1522 | oldidx = subbuf_index(offsets.old, chan); |
| 1523 | lib_ring_buffer_clear_noref(config, &buf->backend, oldidx); |
| 1524 | |
| 1525 | /* |
| 1526 | * May need to populate header start on SWITCH_FLUSH. |
| 1527 | */ |
| 1528 | if (offsets.switch_old_start) { |
| 1529 | lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc); |
| 1530 | offsets.old += config->cb.subbuffer_header_size(); |
| 1531 | } |
| 1532 | |
| 1533 | /* |
| 1534 | * Switch old subbuffer. |
| 1535 | */ |
| 1536 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc); |
| 1537 | } |
| 1538 | EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_slow); |
| 1539 | |
| 1540 | static void remote_switch(void *info) |
| 1541 | { |
| 1542 | struct lib_ring_buffer *buf = info; |
| 1543 | |
| 1544 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 1545 | } |
| 1546 | |
| 1547 | void lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf) |
| 1548 | { |
| 1549 | struct channel *chan = buf->backend.chan; |
| 1550 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1551 | int ret; |
| 1552 | |
| 1553 | /* |
| 1554 | * With global synchronization we don't need to use the IPI scheme. |
| 1555 | */ |
| 1556 | if (config->sync == RING_BUFFER_SYNC_GLOBAL) { |
| 1557 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 1558 | return; |
| 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | * Taking lock on CPU hotplug to ensure two things: first, that the |
| 1563 | * target cpu is not taken concurrently offline while we are within |
| 1564 | * smp_call_function_single() (I don't trust that get_cpu() on the |
| 1565 | * _local_ CPU actually inhibit CPU hotplug for the _remote_ CPU (to be |
| 1566 | * confirmed)). Secondly, if it happens that the CPU is not online, our |
| 1567 | * own call to lib_ring_buffer_switch_slow() needs to be protected from |
| 1568 | * CPU hotplug handlers, which can also perform a remote subbuffer |
| 1569 | * switch. |
| 1570 | */ |
| 1571 | get_online_cpus(); |
| 1572 | ret = smp_call_function_single(buf->backend.cpu, |
| 1573 | remote_switch, buf, 1); |
| 1574 | if (ret) { |
| 1575 | /* Remote CPU is offline, do it ourself. */ |
| 1576 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); |
| 1577 | } |
| 1578 | put_online_cpus(); |
| 1579 | } |
| 1580 | EXPORT_SYMBOL_GPL(lib_ring_buffer_switch_remote); |
| 1581 | |
| 1582 | /* |
| 1583 | * Returns : |
| 1584 | * 0 if ok |
| 1585 | * -ENOSPC if event size is too large for packet. |
| 1586 | * -ENOBUFS if there is currently not enough space in buffer for the event. |
| 1587 | * -EIO if data cannot be written into the buffer for any other reason. |
| 1588 | */ |
| 1589 | static |
| 1590 | int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer *buf, |
| 1591 | struct channel *chan, |
| 1592 | struct switch_offsets *offsets, |
| 1593 | struct lib_ring_buffer_ctx *ctx) |
| 1594 | { |
| 1595 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1596 | unsigned long reserve_commit_diff, offset_cmp; |
| 1597 | |
| 1598 | retry: |
| 1599 | offsets->begin = offset_cmp = v_read(config, &buf->offset); |
| 1600 | offsets->old = offsets->begin; |
| 1601 | offsets->switch_new_start = 0; |
| 1602 | offsets->switch_new_end = 0; |
| 1603 | offsets->switch_old_end = 0; |
| 1604 | offsets->pre_header_padding = 0; |
| 1605 | |
| 1606 | ctx->tsc = config->cb.ring_buffer_clock_read(chan); |
| 1607 | if ((int64_t) ctx->tsc == -EIO) |
| 1608 | return -EIO; |
| 1609 | |
| 1610 | if (last_tsc_overflow(config, buf, ctx->tsc)) |
| 1611 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; |
| 1612 | |
| 1613 | if (unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) { |
| 1614 | offsets->switch_new_start = 1; /* For offsets->begin */ |
| 1615 | } else { |
| 1616 | offsets->size = config->cb.record_header_size(config, chan, |
| 1617 | offsets->begin, |
| 1618 | &offsets->pre_header_padding, |
| 1619 | ctx); |
| 1620 | offsets->size += |
| 1621 | lib_ring_buffer_align(offsets->begin + offsets->size, |
| 1622 | ctx->largest_align) |
| 1623 | + ctx->data_size; |
| 1624 | if (unlikely(subbuf_offset(offsets->begin, chan) + |
| 1625 | offsets->size > chan->backend.subbuf_size)) { |
| 1626 | offsets->switch_old_end = 1; /* For offsets->old */ |
| 1627 | offsets->switch_new_start = 1; /* For offsets->begin */ |
| 1628 | } |
| 1629 | } |
| 1630 | if (unlikely(offsets->switch_new_start)) { |
| 1631 | unsigned long sb_index, commit_count; |
| 1632 | |
| 1633 | /* |
| 1634 | * We are typically not filling the previous buffer completely. |
| 1635 | */ |
| 1636 | if (likely(offsets->switch_old_end)) |
| 1637 | offsets->begin = subbuf_align(offsets->begin, chan); |
| 1638 | offsets->begin = offsets->begin |
| 1639 | + config->cb.subbuffer_header_size(); |
| 1640 | /* Test new buffer integrity */ |
| 1641 | sb_index = subbuf_index(offsets->begin, chan); |
| 1642 | /* |
| 1643 | * Read buf->offset before buf->commit_cold[sb_index].cc_sb. |
| 1644 | * lib_ring_buffer_check_deliver() has the matching |
| 1645 | * memory barriers required around commit_cold cc_sb |
| 1646 | * updates to ensure reserve and commit counter updates |
| 1647 | * are not seen reordered when updated by another CPU. |
| 1648 | */ |
| 1649 | smp_rmb(); |
| 1650 | commit_count = v_read(config, |
| 1651 | &buf->commit_cold[sb_index].cc_sb); |
| 1652 | /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */ |
| 1653 | smp_rmb(); |
| 1654 | if (unlikely(offset_cmp != v_read(config, &buf->offset))) { |
| 1655 | /* |
| 1656 | * The reserve counter have been concurrently updated |
| 1657 | * while we read the commit counter. This means the |
| 1658 | * commit counter we read might not match buf->offset |
| 1659 | * due to concurrent update. We therefore need to retry. |
| 1660 | */ |
| 1661 | goto retry; |
| 1662 | } |
| 1663 | reserve_commit_diff = |
| 1664 | (buf_trunc(offsets->begin, chan) |
| 1665 | >> chan->backend.num_subbuf_order) |
| 1666 | - (commit_count & chan->commit_count_mask); |
| 1667 | if (likely(reserve_commit_diff == 0)) { |
| 1668 | /* Next subbuffer not being written to. */ |
| 1669 | if (unlikely(config->mode != RING_BUFFER_OVERWRITE && |
| 1670 | subbuf_trunc(offsets->begin, chan) |
| 1671 | - subbuf_trunc((unsigned long) |
| 1672 | atomic_long_read(&buf->consumed), chan) |
| 1673 | >= chan->backend.buf_size)) { |
| 1674 | /* |
| 1675 | * We do not overwrite non consumed buffers |
| 1676 | * and we are full : record is lost. |
| 1677 | */ |
| 1678 | v_inc(config, &buf->records_lost_full); |
| 1679 | return -ENOBUFS; |
| 1680 | } else { |
| 1681 | /* |
| 1682 | * Next subbuffer not being written to, and we |
| 1683 | * are either in overwrite mode or the buffer is |
| 1684 | * not full. It's safe to write in this new |
| 1685 | * subbuffer. |
| 1686 | */ |
| 1687 | } |
| 1688 | } else { |
| 1689 | /* |
| 1690 | * Next subbuffer reserve offset does not match the |
| 1691 | * commit offset, and this did not involve update to the |
| 1692 | * reserve counter. Drop record in producer-consumer and |
| 1693 | * overwrite mode. Caused by either a writer OOPS or |
| 1694 | * too many nested writes over a reserve/commit pair. |
| 1695 | */ |
| 1696 | v_inc(config, &buf->records_lost_wrap); |
| 1697 | return -EIO; |
| 1698 | } |
| 1699 | offsets->size = |
| 1700 | config->cb.record_header_size(config, chan, |
| 1701 | offsets->begin, |
| 1702 | &offsets->pre_header_padding, |
| 1703 | ctx); |
| 1704 | offsets->size += |
| 1705 | lib_ring_buffer_align(offsets->begin + offsets->size, |
| 1706 | ctx->largest_align) |
| 1707 | + ctx->data_size; |
| 1708 | if (unlikely(subbuf_offset(offsets->begin, chan) |
| 1709 | + offsets->size > chan->backend.subbuf_size)) { |
| 1710 | /* |
| 1711 | * Record too big for subbuffers, report error, don't |
| 1712 | * complete the sub-buffer switch. |
| 1713 | */ |
| 1714 | v_inc(config, &buf->records_lost_big); |
| 1715 | return -ENOSPC; |
| 1716 | } else { |
| 1717 | /* |
| 1718 | * We just made a successful buffer switch and the |
| 1719 | * record fits in the new subbuffer. Let's write. |
| 1720 | */ |
| 1721 | } |
| 1722 | } else { |
| 1723 | /* |
| 1724 | * Record fits in the current buffer and we are not on a switch |
| 1725 | * boundary. It's safe to write. |
| 1726 | */ |
| 1727 | } |
| 1728 | offsets->end = offsets->begin + offsets->size; |
| 1729 | |
| 1730 | if (unlikely(subbuf_offset(offsets->end, chan) == 0)) { |
| 1731 | /* |
| 1732 | * The offset_end will fall at the very beginning of the next |
| 1733 | * subbuffer. |
| 1734 | */ |
| 1735 | offsets->switch_new_end = 1; /* For offsets->begin */ |
| 1736 | } |
| 1737 | return 0; |
| 1738 | } |
| 1739 | |
| 1740 | /** |
| 1741 | * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer. |
| 1742 | * @ctx: ring buffer context. |
| 1743 | * |
| 1744 | * Return : -NOBUFS if not enough space, -ENOSPC if event size too large, |
| 1745 | * -EIO for other errors, else returns 0. |
| 1746 | * It will take care of sub-buffer switching. |
| 1747 | */ |
| 1748 | int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx *ctx) |
| 1749 | { |
| 1750 | struct channel *chan = ctx->chan; |
| 1751 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
| 1752 | struct lib_ring_buffer *buf; |
| 1753 | struct switch_offsets offsets; |
| 1754 | int ret; |
| 1755 | |
| 1756 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 1757 | buf = per_cpu_ptr(chan->backend.buf, ctx->cpu); |
| 1758 | else |
| 1759 | buf = chan->backend.buf; |
| 1760 | ctx->buf = buf; |
| 1761 | |
| 1762 | offsets.size = 0; |
| 1763 | |
| 1764 | do { |
| 1765 | ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets, |
| 1766 | ctx); |
| 1767 | if (unlikely(ret)) |
| 1768 | return ret; |
| 1769 | } while (unlikely(v_cmpxchg(config, &buf->offset, offsets.old, |
| 1770 | offsets.end) |
| 1771 | != offsets.old)); |
| 1772 | |
| 1773 | /* |
| 1774 | * Atomically update last_tsc. This update races against concurrent |
| 1775 | * atomic updates, but the race will always cause supplementary full TSC |
| 1776 | * records, never the opposite (missing a full TSC record when it would |
| 1777 | * be needed). |
| 1778 | */ |
| 1779 | save_last_tsc(config, buf, ctx->tsc); |
| 1780 | |
| 1781 | /* |
| 1782 | * Push the reader if necessary |
| 1783 | */ |
| 1784 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1); |
| 1785 | |
| 1786 | /* |
| 1787 | * Clear noref flag for this subbuffer. |
| 1788 | */ |
| 1789 | lib_ring_buffer_clear_noref(config, &buf->backend, |
| 1790 | subbuf_index(offsets.end - 1, chan)); |
| 1791 | |
| 1792 | /* |
| 1793 | * Switch old subbuffer if needed. |
| 1794 | */ |
| 1795 | if (unlikely(offsets.switch_old_end)) { |
| 1796 | lib_ring_buffer_clear_noref(config, &buf->backend, |
| 1797 | subbuf_index(offsets.old - 1, chan)); |
| 1798 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc); |
| 1799 | } |
| 1800 | |
| 1801 | /* |
| 1802 | * Populate new subbuffer. |
| 1803 | */ |
| 1804 | if (unlikely(offsets.switch_new_start)) |
| 1805 | lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc); |
| 1806 | |
| 1807 | if (unlikely(offsets.switch_new_end)) |
| 1808 | lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc); |
| 1809 | |
| 1810 | ctx->slot_size = offsets.size; |
| 1811 | ctx->pre_offset = offsets.begin; |
| 1812 | ctx->buf_offset = offsets.begin + offsets.pre_header_padding; |
| 1813 | return 0; |
| 1814 | } |
| 1815 | EXPORT_SYMBOL_GPL(lib_ring_buffer_reserve_slow); |
| 1816 | |
| 1817 | int __init init_lib_ring_buffer_frontend(void) |
| 1818 | { |
| 1819 | int cpu; |
| 1820 | |
| 1821 | for_each_possible_cpu(cpu) |
| 1822 | spin_lock_init(&per_cpu(ring_buffer_nohz_lock, cpu)); |
| 1823 | return 0; |
| 1824 | } |
| 1825 | |
| 1826 | module_init(init_lib_ring_buffer_frontend); |
| 1827 | |
| 1828 | void __exit exit_lib_ring_buffer_frontend(void) |
| 1829 | { |
| 1830 | } |
| 1831 | |
| 1832 | module_exit(exit_lib_ring_buffer_frontend); |