| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * Ring buffer backend (internal helpers). |
| 7 | */ |
| 8 | |
| 9 | #ifndef _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H |
| 10 | #define _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H |
| 11 | |
| 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
| 14 | #include <unistd.h> |
| 15 | #include <urcu/compiler.h> |
| 16 | |
| 17 | #include <lttng/ringbuffer-config.h> |
| 18 | #include "backend_types.h" |
| 19 | #include "frontend_types.h" |
| 20 | #include "shm.h" |
| 21 | |
| 22 | /* Ring buffer backend API presented to the frontend */ |
| 23 | |
| 24 | /* Ring buffer and channel backend create/free */ |
| 25 | |
| 26 | __attribute__((visibility("hidden"))) |
| 27 | int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 28 | struct channel_backend *chan, int cpu, |
| 29 | struct lttng_ust_shm_handle *handle, |
| 30 | struct shm_object *shmobj); |
| 31 | |
| 32 | __attribute__((visibility("hidden"))) |
| 33 | void channel_backend_unregister_notifiers(struct channel_backend *chanb); |
| 34 | |
| 35 | __attribute__((visibility("hidden"))) |
| 36 | void lib_ring_buffer_backend_free(struct lttng_ust_lib_ring_buffer_backend *bufb); |
| 37 | |
| 38 | __attribute__((visibility("hidden"))) |
| 39 | int channel_backend_init(struct channel_backend *chanb, |
| 40 | const char *name, |
| 41 | const struct lttng_ust_lib_ring_buffer_config *config, |
| 42 | size_t subbuf_size, |
| 43 | size_t num_subbuf, struct lttng_ust_shm_handle *handle, |
| 44 | const int *stream_fds); |
| 45 | |
| 46 | __attribute__((visibility("hidden"))) |
| 47 | void channel_backend_free(struct channel_backend *chanb, |
| 48 | struct lttng_ust_shm_handle *handle); |
| 49 | |
| 50 | __attribute__((visibility("hidden"))) |
| 51 | void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 52 | struct lttng_ust_shm_handle *handle); |
| 53 | |
| 54 | __attribute__((visibility("hidden"))) |
| 55 | void channel_backend_reset(struct channel_backend *chanb); |
| 56 | |
| 57 | __attribute__((visibility("hidden"))) |
| 58 | int lib_ring_buffer_backend_init(void); |
| 59 | |
| 60 | __attribute__((visibility("hidden"))) |
| 61 | void lib_ring_buffer_backend_exit(void); |
| 62 | |
| 63 | __attribute__((visibility("hidden"))) |
| 64 | extern void _lib_ring_buffer_write(struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 65 | size_t offset, const void *src, size_t len, |
| 66 | ssize_t pagecpy); |
| 67 | |
| 68 | /* |
| 69 | * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be |
| 70 | * exchanged atomically. |
| 71 | * |
| 72 | * Top half word, except lowest bit, belongs to "offset", which is used to keep |
| 73 | * to count the produced buffers. For overwrite mode, this provides the |
| 74 | * consumer with the capacity to read subbuffers in order, handling the |
| 75 | * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit |
| 76 | * systems) concurrently with a single execution of get_subbuf (between offset |
| 77 | * sampling and subbuffer ID exchange). |
| 78 | */ |
| 79 | |
| 80 | #define HALF_ULONG_BITS (CAA_BITS_PER_LONG >> 1) |
| 81 | |
| 82 | #define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1) |
| 83 | #define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT) |
| 84 | #define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1)) |
| 85 | /* |
| 86 | * Lowest bit of top word half belongs to noref. Used only for overwrite mode. |
| 87 | */ |
| 88 | #define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1) |
| 89 | #define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT) |
| 90 | #define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT |
| 91 | /* |
| 92 | * In overwrite mode: lowest half of word is used for index. |
| 93 | * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit. |
| 94 | * In producer-consumer mode: whole word used for index. |
| 95 | */ |
| 96 | #define SB_ID_INDEX_SHIFT 0 |
| 97 | #define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT) |
| 98 | #define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1) |
| 99 | |
| 100 | /* |
| 101 | * Construct the subbuffer id from offset, index and noref. Use only the index |
| 102 | * for producer-consumer mode (offset and noref are only used in overwrite |
| 103 | * mode). |
| 104 | */ |
| 105 | static inline |
| 106 | unsigned long subbuffer_id(const struct lttng_ust_lib_ring_buffer_config *config, |
| 107 | unsigned long offset, unsigned long noref, |
| 108 | unsigned long index) |
| 109 | { |
| 110 | if (config->mode == RING_BUFFER_OVERWRITE) |
| 111 | return (offset << SB_ID_OFFSET_SHIFT) |
| 112 | | (noref << SB_ID_NOREF_SHIFT) |
| 113 | | index; |
| 114 | else |
| 115 | return index; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Compare offset with the offset contained within id. Return 1 if the offset |
| 120 | * bits are identical, else 0. |
| 121 | */ |
| 122 | static inline |
| 123 | int subbuffer_id_compare_offset(const struct lttng_ust_lib_ring_buffer_config *config, |
| 124 | unsigned long id, unsigned long offset) |
| 125 | { |
| 126 | return (id & SB_ID_OFFSET_MASK) == (offset << SB_ID_OFFSET_SHIFT); |
| 127 | } |
| 128 | |
| 129 | static inline |
| 130 | unsigned long subbuffer_id_get_index(const struct lttng_ust_lib_ring_buffer_config *config, |
| 131 | unsigned long id) |
| 132 | { |
| 133 | if (config->mode == RING_BUFFER_OVERWRITE) |
| 134 | return id & SB_ID_INDEX_MASK; |
| 135 | else |
| 136 | return id; |
| 137 | } |
| 138 | |
| 139 | static inline |
| 140 | unsigned long subbuffer_id_is_noref(const struct lttng_ust_lib_ring_buffer_config *config, |
| 141 | unsigned long id) |
| 142 | { |
| 143 | if (config->mode == RING_BUFFER_OVERWRITE) |
| 144 | return !!(id & SB_ID_NOREF_MASK); |
| 145 | else |
| 146 | return 1; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Only used by reader on subbuffer ID it has exclusive access to. No volatile |
| 151 | * needed. |
| 152 | */ |
| 153 | static inline |
| 154 | void subbuffer_id_set_noref(const struct lttng_ust_lib_ring_buffer_config *config, |
| 155 | unsigned long *id) |
| 156 | { |
| 157 | if (config->mode == RING_BUFFER_OVERWRITE) |
| 158 | *id |= SB_ID_NOREF_MASK; |
| 159 | } |
| 160 | |
| 161 | static inline |
| 162 | void subbuffer_id_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config *config, |
| 163 | unsigned long *id, unsigned long offset) |
| 164 | { |
| 165 | unsigned long tmp; |
| 166 | |
| 167 | if (config->mode == RING_BUFFER_OVERWRITE) { |
| 168 | tmp = *id; |
| 169 | tmp &= ~SB_ID_OFFSET_MASK; |
| 170 | tmp |= offset << SB_ID_OFFSET_SHIFT; |
| 171 | tmp |= SB_ID_NOREF_MASK; |
| 172 | /* Volatile store, read concurrently by readers. */ |
| 173 | CMM_ACCESS_ONCE(*id) = tmp; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* No volatile access, since already used locally */ |
| 178 | static inline |
| 179 | void subbuffer_id_clear_noref(const struct lttng_ust_lib_ring_buffer_config *config, |
| 180 | unsigned long *id) |
| 181 | { |
| 182 | if (config->mode == RING_BUFFER_OVERWRITE) |
| 183 | *id &= ~SB_ID_NOREF_MASK; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * For overwrite mode, cap the number of subbuffers per buffer to: |
| 188 | * 2^16 on 32-bit architectures |
| 189 | * 2^32 on 64-bit architectures |
| 190 | * This is required to fit in the index part of the ID. Return 0 on success, |
| 191 | * -EPERM on failure. |
| 192 | */ |
| 193 | static inline |
| 194 | int subbuffer_id_check_index(const struct lttng_ust_lib_ring_buffer_config *config, |
| 195 | unsigned long num_subbuf) |
| 196 | { |
| 197 | if (config->mode == RING_BUFFER_OVERWRITE) |
| 198 | return (num_subbuf > (1UL << HALF_ULONG_BITS)) ? -EPERM : 0; |
| 199 | else |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static inline |
| 204 | int lib_ring_buffer_backend_get_pages(const struct lttng_ust_lib_ring_buffer_config *config, |
| 205 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 206 | struct lttng_ust_lib_ring_buffer_backend_pages **backend_pages) |
| 207 | { |
| 208 | struct lttng_ust_lib_ring_buffer_backend *bufb = &ctx->buf->backend; |
| 209 | struct channel_backend *chanb = &ctx->chan->backend; |
| 210 | struct lttng_ust_shm_handle *handle = ctx->handle; |
| 211 | size_t sbidx; |
| 212 | size_t offset = ctx->buf_offset; |
| 213 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
| 214 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
| 215 | unsigned long sb_bindex, id; |
| 216 | struct lttng_ust_lib_ring_buffer_backend_pages *_backend_pages; |
| 217 | |
| 218 | offset &= chanb->buf_size - 1; |
| 219 | sbidx = offset >> chanb->subbuf_size_order; |
| 220 | wsb = shmp_index(handle, bufb->buf_wsb, sbidx); |
| 221 | if (caa_unlikely(!wsb)) |
| 222 | return -1; |
| 223 | id = wsb->id; |
| 224 | sb_bindex = subbuffer_id_get_index(config, id); |
| 225 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
| 226 | if (caa_unlikely(!rpages)) |
| 227 | return -1; |
| 228 | CHAN_WARN_ON(ctx->chan, |
| 229 | config->mode == RING_BUFFER_OVERWRITE |
| 230 | && subbuffer_id_is_noref(config, id)); |
| 231 | _backend_pages = shmp(handle, rpages->shmp); |
| 232 | if (caa_unlikely(!_backend_pages)) |
| 233 | return -1; |
| 234 | *backend_pages = _backend_pages; |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* Get backend pages from cache. */ |
| 239 | static inline |
| 240 | struct lttng_ust_lib_ring_buffer_backend_pages * |
| 241 | lib_ring_buffer_get_backend_pages_from_ctx(const struct lttng_ust_lib_ring_buffer_config *config, |
| 242 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
| 243 | { |
| 244 | return ctx->backend_pages; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * The ring buffer can count events recorded and overwritten per buffer, |
| 249 | * but it is disabled by default due to its performance overhead. |
| 250 | */ |
| 251 | #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS |
| 252 | static inline |
| 253 | void subbuffer_count_record(const struct lttng_ust_lib_ring_buffer_config *config, |
| 254 | const struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 255 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 256 | unsigned long idx, struct lttng_ust_shm_handle *handle) |
| 257 | { |
| 258 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; |
| 259 | |
| 260 | backend_pages = lib_ring_buffer_get_backend_pages_from_ctx(config, ctx); |
| 261 | if (caa_unlikely(!backend_pages)) { |
| 262 | if (lib_ring_buffer_backend_get_pages(config, ctx, &backend_pages)) |
| 263 | return; |
| 264 | } |
| 265 | v_inc(config, &backend_pages->records_commit); |
| 266 | } |
| 267 | #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */ |
| 268 | static inline |
| 269 | void subbuffer_count_record(const struct lttng_ust_lib_ring_buffer_config *config, |
| 270 | const struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 271 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 272 | unsigned long idx, struct lttng_ust_shm_handle *handle) |
| 273 | { |
| 274 | } |
| 275 | #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */ |
| 276 | |
| 277 | /* |
| 278 | * Reader has exclusive subbuffer access for record consumption. No need to |
| 279 | * perform the decrement atomically. |
| 280 | */ |
| 281 | static inline |
| 282 | void subbuffer_consume_record(const struct lttng_ust_lib_ring_buffer_config *config, |
| 283 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 284 | struct lttng_ust_shm_handle *handle) |
| 285 | { |
| 286 | unsigned long sb_bindex; |
| 287 | struct channel *chan; |
| 288 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages_shmp; |
| 289 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; |
| 290 | |
| 291 | sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); |
| 292 | chan = shmp(handle, bufb->chan); |
| 293 | if (!chan) |
| 294 | return; |
| 295 | pages_shmp = shmp_index(handle, bufb->array, sb_bindex); |
| 296 | if (!pages_shmp) |
| 297 | return; |
| 298 | backend_pages = shmp(handle, pages_shmp->shmp); |
| 299 | if (!backend_pages) |
| 300 | return; |
| 301 | CHAN_WARN_ON(chan, !v_read(config, &backend_pages->records_unread)); |
| 302 | /* Non-atomic decrement protected by exclusive subbuffer access */ |
| 303 | _v_dec(config, &backend_pages->records_unread); |
| 304 | v_inc(config, &bufb->records_read); |
| 305 | } |
| 306 | |
| 307 | static inline |
| 308 | unsigned long subbuffer_get_records_count( |
| 309 | const struct lttng_ust_lib_ring_buffer_config *config, |
| 310 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 311 | unsigned long idx, |
| 312 | struct lttng_ust_shm_handle *handle) |
| 313 | { |
| 314 | unsigned long sb_bindex; |
| 315 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
| 316 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
| 317 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; |
| 318 | |
| 319 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
| 320 | if (!wsb) |
| 321 | return 0; |
| 322 | sb_bindex = subbuffer_id_get_index(config, wsb->id); |
| 323 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
| 324 | if (!rpages) |
| 325 | return 0; |
| 326 | backend_pages = shmp(handle, rpages->shmp); |
| 327 | if (!backend_pages) |
| 328 | return 0; |
| 329 | return v_read(config, &backend_pages->records_commit); |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Must be executed at subbuffer delivery when the writer has _exclusive_ |
| 334 | * subbuffer access. See lib_ring_buffer_check_deliver() for details. |
| 335 | * lib_ring_buffer_get_records_count() must be called to get the records |
| 336 | * count before this function, because it resets the records_commit |
| 337 | * count. |
| 338 | */ |
| 339 | static inline |
| 340 | unsigned long subbuffer_count_records_overrun( |
| 341 | const struct lttng_ust_lib_ring_buffer_config *config, |
| 342 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 343 | unsigned long idx, |
| 344 | struct lttng_ust_shm_handle *handle) |
| 345 | { |
| 346 | unsigned long overruns, sb_bindex; |
| 347 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
| 348 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
| 349 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; |
| 350 | |
| 351 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
| 352 | if (!wsb) |
| 353 | return 0; |
| 354 | sb_bindex = subbuffer_id_get_index(config, wsb->id); |
| 355 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
| 356 | if (!rpages) |
| 357 | return 0; |
| 358 | backend_pages = shmp(handle, rpages->shmp); |
| 359 | if (!backend_pages) |
| 360 | return 0; |
| 361 | overruns = v_read(config, &backend_pages->records_unread); |
| 362 | v_set(config, &backend_pages->records_unread, |
| 363 | v_read(config, &backend_pages->records_commit)); |
| 364 | v_set(config, &backend_pages->records_commit, 0); |
| 365 | |
| 366 | return overruns; |
| 367 | } |
| 368 | |
| 369 | static inline |
| 370 | void subbuffer_set_data_size(const struct lttng_ust_lib_ring_buffer_config *config, |
| 371 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 372 | unsigned long idx, |
| 373 | unsigned long data_size, |
| 374 | struct lttng_ust_shm_handle *handle) |
| 375 | { |
| 376 | unsigned long sb_bindex; |
| 377 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
| 378 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
| 379 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; |
| 380 | |
| 381 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
| 382 | if (!wsb) |
| 383 | return; |
| 384 | sb_bindex = subbuffer_id_get_index(config, wsb->id); |
| 385 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
| 386 | if (!rpages) |
| 387 | return; |
| 388 | backend_pages = shmp(handle, rpages->shmp); |
| 389 | if (!backend_pages) |
| 390 | return; |
| 391 | backend_pages->data_size = data_size; |
| 392 | } |
| 393 | |
| 394 | static inline |
| 395 | unsigned long subbuffer_get_read_data_size( |
| 396 | const struct lttng_ust_lib_ring_buffer_config *config, |
| 397 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 398 | struct lttng_ust_shm_handle *handle) |
| 399 | { |
| 400 | unsigned long sb_bindex; |
| 401 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages_shmp; |
| 402 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; |
| 403 | |
| 404 | sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); |
| 405 | pages_shmp = shmp_index(handle, bufb->array, sb_bindex); |
| 406 | if (!pages_shmp) |
| 407 | return 0; |
| 408 | backend_pages = shmp(handle, pages_shmp->shmp); |
| 409 | if (!backend_pages) |
| 410 | return 0; |
| 411 | return backend_pages->data_size; |
| 412 | } |
| 413 | |
| 414 | static inline |
| 415 | unsigned long subbuffer_get_data_size( |
| 416 | const struct lttng_ust_lib_ring_buffer_config *config, |
| 417 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 418 | unsigned long idx, |
| 419 | struct lttng_ust_shm_handle *handle) |
| 420 | { |
| 421 | unsigned long sb_bindex; |
| 422 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
| 423 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
| 424 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; |
| 425 | |
| 426 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
| 427 | if (!wsb) |
| 428 | return 0; |
| 429 | sb_bindex = subbuffer_id_get_index(config, wsb->id); |
| 430 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
| 431 | if (!rpages) |
| 432 | return 0; |
| 433 | backend_pages = shmp(handle, rpages->shmp); |
| 434 | if (!backend_pages) |
| 435 | return 0; |
| 436 | return backend_pages->data_size; |
| 437 | } |
| 438 | |
| 439 | static inline |
| 440 | void subbuffer_inc_packet_count(const struct lttng_ust_lib_ring_buffer_config *config, |
| 441 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 442 | unsigned long idx, struct lttng_ust_shm_handle *handle) |
| 443 | { |
| 444 | struct lttng_ust_lib_ring_buffer_backend_counts *counts; |
| 445 | |
| 446 | counts = shmp_index(handle, bufb->buf_cnt, idx); |
| 447 | if (!counts) |
| 448 | return; |
| 449 | counts->seq_cnt++; |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by |
| 454 | * writer. |
| 455 | */ |
| 456 | static inline |
| 457 | void lib_ring_buffer_clear_noref(const struct lttng_ust_lib_ring_buffer_config *config, |
| 458 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 459 | unsigned long idx, |
| 460 | struct lttng_ust_shm_handle *handle) |
| 461 | { |
| 462 | unsigned long id, new_id; |
| 463 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
| 464 | |
| 465 | if (config->mode != RING_BUFFER_OVERWRITE) |
| 466 | return; |
| 467 | |
| 468 | /* |
| 469 | * Performing a volatile access to read the sb_pages, because we want to |
| 470 | * read a coherent version of the pointer and the associated noref flag. |
| 471 | */ |
| 472 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
| 473 | if (!wsb) |
| 474 | return; |
| 475 | id = CMM_ACCESS_ONCE(wsb->id); |
| 476 | for (;;) { |
| 477 | /* This check is called on the fast path for each record. */ |
| 478 | if (caa_likely(!subbuffer_id_is_noref(config, id))) { |
| 479 | /* |
| 480 | * Store after load dependency ordering the writes to |
| 481 | * the subbuffer after load and test of the noref flag |
| 482 | * matches the memory barrier implied by the cmpxchg() |
| 483 | * in update_read_sb_index(). |
| 484 | */ |
| 485 | return; /* Already writing to this buffer */ |
| 486 | } |
| 487 | new_id = id; |
| 488 | subbuffer_id_clear_noref(config, &new_id); |
| 489 | new_id = uatomic_cmpxchg(&wsb->id, id, new_id); |
| 490 | if (caa_likely(new_id == id)) |
| 491 | break; |
| 492 | id = new_id; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset, |
| 498 | * called by writer. |
| 499 | */ |
| 500 | static inline |
| 501 | void lib_ring_buffer_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config *config, |
| 502 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 503 | unsigned long idx, unsigned long offset, |
| 504 | struct lttng_ust_shm_handle *handle) |
| 505 | { |
| 506 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
| 507 | struct channel *chan; |
| 508 | |
| 509 | if (config->mode != RING_BUFFER_OVERWRITE) |
| 510 | return; |
| 511 | |
| 512 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
| 513 | if (!wsb) |
| 514 | return; |
| 515 | /* |
| 516 | * Because ring_buffer_set_noref() is only called by a single thread |
| 517 | * (the one which updated the cc_sb value), there are no concurrent |
| 518 | * updates to take care of: other writers have not updated cc_sb, so |
| 519 | * they cannot set the noref flag, and concurrent readers cannot modify |
| 520 | * the pointer because the noref flag is not set yet. |
| 521 | * The smp_wmb() in ring_buffer_commit() takes care of ordering writes |
| 522 | * to the subbuffer before this set noref operation. |
| 523 | * subbuffer_set_noref() uses a volatile store to deal with concurrent |
| 524 | * readers of the noref flag. |
| 525 | */ |
| 526 | chan = shmp(handle, bufb->chan); |
| 527 | if (!chan) |
| 528 | return; |
| 529 | CHAN_WARN_ON(chan, subbuffer_id_is_noref(config, wsb->id)); |
| 530 | /* |
| 531 | * Memory barrier that ensures counter stores are ordered before set |
| 532 | * noref and offset. |
| 533 | */ |
| 534 | cmm_smp_mb(); |
| 535 | subbuffer_id_set_noref_offset(config, &wsb->id, offset); |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * update_read_sb_index - Read-side subbuffer index update. |
| 540 | */ |
| 541 | static inline |
| 542 | int update_read_sb_index(const struct lttng_ust_lib_ring_buffer_config *config, |
| 543 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
| 544 | struct channel_backend *chanb, |
| 545 | unsigned long consumed_idx, |
| 546 | unsigned long consumed_count, |
| 547 | struct lttng_ust_shm_handle *handle) |
| 548 | { |
| 549 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
| 550 | unsigned long old_id, new_id; |
| 551 | |
| 552 | wsb = shmp_index(handle, bufb->buf_wsb, consumed_idx); |
| 553 | if (caa_unlikely(!wsb)) |
| 554 | return -EPERM; |
| 555 | |
| 556 | if (config->mode == RING_BUFFER_OVERWRITE) { |
| 557 | struct channel *chan; |
| 558 | |
| 559 | /* |
| 560 | * Exchange the target writer subbuffer with our own unused |
| 561 | * subbuffer. No need to use CMM_ACCESS_ONCE() here to read the |
| 562 | * old_wpage, because the value read will be confirmed by the |
| 563 | * following cmpxchg(). |
| 564 | */ |
| 565 | old_id = wsb->id; |
| 566 | if (caa_unlikely(!subbuffer_id_is_noref(config, old_id))) |
| 567 | return -EAGAIN; |
| 568 | /* |
| 569 | * Make sure the offset count we are expecting matches the one |
| 570 | * indicated by the writer. |
| 571 | */ |
| 572 | if (caa_unlikely(!subbuffer_id_compare_offset(config, old_id, |
| 573 | consumed_count))) |
| 574 | return -EAGAIN; |
| 575 | chan = shmp(handle, bufb->chan); |
| 576 | if (caa_unlikely(!chan)) |
| 577 | return -EPERM; |
| 578 | CHAN_WARN_ON(chan, !subbuffer_id_is_noref(config, bufb->buf_rsb.id)); |
| 579 | subbuffer_id_set_noref_offset(config, &bufb->buf_rsb.id, |
| 580 | consumed_count); |
| 581 | new_id = uatomic_cmpxchg(&wsb->id, old_id, bufb->buf_rsb.id); |
| 582 | if (caa_unlikely(old_id != new_id)) |
| 583 | return -EAGAIN; |
| 584 | bufb->buf_rsb.id = new_id; |
| 585 | } else { |
| 586 | /* No page exchange, use the writer page directly */ |
| 587 | bufb->buf_rsb.id = wsb->id; |
| 588 | } |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | #ifndef inline_memcpy |
| 593 | #define inline_memcpy(dest, src, n) memcpy(dest, src, n) |
| 594 | #endif |
| 595 | |
| 596 | static inline __attribute__((always_inline)) |
| 597 | void lttng_inline_memcpy(void *dest, const void *src, |
| 598 | unsigned long len) |
| 599 | { |
| 600 | switch (len) { |
| 601 | case 1: |
| 602 | *(uint8_t *) dest = *(const uint8_t *) src; |
| 603 | break; |
| 604 | case 2: |
| 605 | *(uint16_t *) dest = *(const uint16_t *) src; |
| 606 | break; |
| 607 | case 4: |
| 608 | *(uint32_t *) dest = *(const uint32_t *) src; |
| 609 | break; |
| 610 | case 8: |
| 611 | *(uint64_t *) dest = *(const uint64_t *) src; |
| 612 | break; |
| 613 | default: |
| 614 | inline_memcpy(dest, src, len); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Use the architecture-specific memcpy implementation for constant-sized |
| 620 | * inputs, but rely on an inline memcpy for length statically unknown. |
| 621 | * The function call to memcpy is just way too expensive for a fast path. |
| 622 | */ |
| 623 | #define lib_ring_buffer_do_copy(config, dest, src, len) \ |
| 624 | do { \ |
| 625 | size_t __len = (len); \ |
| 626 | if (__builtin_constant_p(len)) \ |
| 627 | memcpy(dest, src, __len); \ |
| 628 | else \ |
| 629 | lttng_inline_memcpy(dest, src, __len); \ |
| 630 | } while (0) |
| 631 | |
| 632 | /* |
| 633 | * write len bytes to dest with c |
| 634 | */ |
| 635 | static inline |
| 636 | void lib_ring_buffer_do_memset(char *dest, int c, unsigned long len) |
| 637 | { |
| 638 | unsigned long i; |
| 639 | |
| 640 | for (i = 0; i < len; i++) |
| 641 | dest[i] = c; |
| 642 | } |
| 643 | |
| 644 | /* arch-agnostic implementation */ |
| 645 | |
| 646 | static inline int lttng_ust_fls(unsigned int x) |
| 647 | { |
| 648 | int r = 32; |
| 649 | |
| 650 | if (!x) |
| 651 | return 0; |
| 652 | if (!(x & 0xFFFF0000U)) { |
| 653 | x <<= 16; |
| 654 | r -= 16; |
| 655 | } |
| 656 | if (!(x & 0xFF000000U)) { |
| 657 | x <<= 8; |
| 658 | r -= 8; |
| 659 | } |
| 660 | if (!(x & 0xF0000000U)) { |
| 661 | x <<= 4; |
| 662 | r -= 4; |
| 663 | } |
| 664 | if (!(x & 0xC0000000U)) { |
| 665 | x <<= 2; |
| 666 | r -= 2; |
| 667 | } |
| 668 | if (!(x & 0x80000000U)) { |
| 669 | /* No need to bit shift on last operation */ |
| 670 | r -= 1; |
| 671 | } |
| 672 | return r; |
| 673 | } |
| 674 | |
| 675 | static inline int get_count_order(unsigned int count) |
| 676 | { |
| 677 | int order; |
| 678 | |
| 679 | order = lttng_ust_fls(count) - 1; |
| 680 | if (count & (count - 1)) |
| 681 | order++; |
| 682 | return order; |
| 683 | } |
| 684 | |
| 685 | #endif /* _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H */ |