| 1 | #ifndef _LINUX_RING_BUFFER_FRONTEND_API_H |
| 2 | #define _LINUX_RING_BUFFER_FRONTEND_API_H |
| 3 | |
| 4 | /* |
| 5 | * linux/ringbuffer/frontend_api.h |
| 6 | * |
| 7 | * (C) Copyright 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | * |
| 9 | * Ring Buffer Library Synchronization Header (buffer write API). |
| 10 | * |
| 11 | * Author: |
| 12 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 13 | * |
| 14 | * See ring_buffer_frontend.c for more information on wait-free algorithms. |
| 15 | * See linux/ringbuffer/frontend.h for channel allocation and read-side API. |
| 16 | * |
| 17 | * Dual LGPL v2.1/GPL v2 license. |
| 18 | */ |
| 19 | |
| 20 | #include "../../wrapper/ringbuffer/frontend.h" |
| 21 | #include <linux/errno.h> |
| 22 | |
| 23 | /** |
| 24 | * lib_ring_buffer_get_cpu - Precedes ring buffer reserve/commit. |
| 25 | * |
| 26 | * Disables preemption (acts as a RCU read-side critical section) and keeps a |
| 27 | * ring buffer nesting count as supplementary safety net to ensure tracer client |
| 28 | * code will never trigger an endless recursion. Returns the processor ID on |
| 29 | * success, -EPERM on failure (nesting count too high). |
| 30 | * |
| 31 | * asm volatile and "memory" clobber prevent the compiler from moving |
| 32 | * instructions out of the ring buffer nesting count. This is required to ensure |
| 33 | * that probe side-effects which can cause recursion (e.g. unforeseen traps, |
| 34 | * divisions by 0, ...) are triggered within the incremented nesting count |
| 35 | * section. |
| 36 | */ |
| 37 | static inline |
| 38 | int lib_ring_buffer_get_cpu(const struct lib_ring_buffer_config *config) |
| 39 | { |
| 40 | int cpu, nesting; |
| 41 | |
| 42 | rcu_read_lock_sched_notrace(); |
| 43 | cpu = smp_processor_id(); |
| 44 | nesting = ++per_cpu(lib_ring_buffer_nesting, cpu); |
| 45 | barrier(); |
| 46 | |
| 47 | if (unlikely(nesting > 4)) { |
| 48 | WARN_ON_ONCE(1); |
| 49 | per_cpu(lib_ring_buffer_nesting, cpu)--; |
| 50 | rcu_read_unlock_sched_notrace(); |
| 51 | return -EPERM; |
| 52 | } else |
| 53 | return cpu; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * lib_ring_buffer_put_cpu - Follows ring buffer reserve/commit. |
| 58 | */ |
| 59 | static inline |
| 60 | void lib_ring_buffer_put_cpu(const struct lib_ring_buffer_config *config) |
| 61 | { |
| 62 | barrier(); |
| 63 | __get_cpu_var(lib_ring_buffer_nesting)--; |
| 64 | rcu_read_unlock_sched_notrace(); |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * lib_ring_buffer_try_reserve is called by lib_ring_buffer_reserve(). It is not |
| 69 | * part of the API per se. |
| 70 | * |
| 71 | * returns 0 if reserve ok, or 1 if the slow path must be taken. |
| 72 | */ |
| 73 | static inline |
| 74 | int lib_ring_buffer_try_reserve(const struct lib_ring_buffer_config *config, |
| 75 | struct lib_ring_buffer_ctx *ctx, |
| 76 | unsigned long *o_begin, unsigned long *o_end, |
| 77 | unsigned long *o_old, size_t *before_hdr_pad) |
| 78 | { |
| 79 | struct channel *chan = ctx->chan; |
| 80 | struct lib_ring_buffer *buf = ctx->buf; |
| 81 | *o_begin = v_read(config, &buf->offset); |
| 82 | *o_old = *o_begin; |
| 83 | |
| 84 | ctx->tsc = lib_ring_buffer_clock_read(chan); |
| 85 | if ((int64_t) ctx->tsc == -EIO) |
| 86 | return 1; |
| 87 | |
| 88 | /* |
| 89 | * Prefetch cacheline for read because we have to read the previous |
| 90 | * commit counter to increment it and commit seq value to compare it to |
| 91 | * the commit counter. |
| 92 | */ |
| 93 | prefetch(&buf->commit_hot[subbuf_index(*o_begin, chan)]); |
| 94 | |
| 95 | if (last_tsc_overflow(config, buf, ctx->tsc)) |
| 96 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; |
| 97 | |
| 98 | if (unlikely(subbuf_offset(*o_begin, chan) == 0)) |
| 99 | return 1; |
| 100 | |
| 101 | ctx->slot_size = record_header_size(config, chan, *o_begin, |
| 102 | before_hdr_pad, ctx); |
| 103 | ctx->slot_size += |
| 104 | lib_ring_buffer_align(*o_begin + ctx->slot_size, |
| 105 | ctx->largest_align) + ctx->data_size; |
| 106 | if (unlikely((subbuf_offset(*o_begin, chan) + ctx->slot_size) |
| 107 | > chan->backend.subbuf_size)) |
| 108 | return 1; |
| 109 | |
| 110 | /* |
| 111 | * Record fits in the current buffer and we are not on a switch |
| 112 | * boundary. It's safe to write. |
| 113 | */ |
| 114 | *o_end = *o_begin + ctx->slot_size; |
| 115 | |
| 116 | if (unlikely((subbuf_offset(*o_end, chan)) == 0)) |
| 117 | /* |
| 118 | * The offset_end will fall at the very beginning of the next |
| 119 | * subbuffer. |
| 120 | */ |
| 121 | return 1; |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * lib_ring_buffer_reserve - Reserve space in a ring buffer. |
| 128 | * @config: ring buffer instance configuration. |
| 129 | * @ctx: ring buffer context. (input and output) Must be already initialized. |
| 130 | * |
| 131 | * Atomic wait-free slot reservation. The reserved space starts at the context |
| 132 | * "pre_offset". Its length is "slot_size". The associated time-stamp is "tsc". |
| 133 | * |
| 134 | * Return : |
| 135 | * 0 on success. |
| 136 | * -EAGAIN if channel is disabled. |
| 137 | * -ENOSPC if event size is too large for packet. |
| 138 | * -ENOBUFS if there is currently not enough space in buffer for the event. |
| 139 | * -EIO if data cannot be written into the buffer for any other reason. |
| 140 | */ |
| 141 | |
| 142 | static inline |
| 143 | int lib_ring_buffer_reserve(const struct lib_ring_buffer_config *config, |
| 144 | struct lib_ring_buffer_ctx *ctx) |
| 145 | { |
| 146 | struct channel *chan = ctx->chan; |
| 147 | struct lib_ring_buffer *buf; |
| 148 | unsigned long o_begin, o_end, o_old; |
| 149 | size_t before_hdr_pad = 0; |
| 150 | |
| 151 | if (atomic_read(&chan->record_disabled)) |
| 152 | return -EAGAIN; |
| 153 | |
| 154 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
| 155 | buf = per_cpu_ptr(chan->backend.buf, ctx->cpu); |
| 156 | else |
| 157 | buf = chan->backend.buf; |
| 158 | if (atomic_read(&buf->record_disabled)) |
| 159 | return -EAGAIN; |
| 160 | ctx->buf = buf; |
| 161 | |
| 162 | /* |
| 163 | * Perform retryable operations. |
| 164 | */ |
| 165 | if (unlikely(lib_ring_buffer_try_reserve(config, ctx, &o_begin, |
| 166 | &o_end, &o_old, &before_hdr_pad))) |
| 167 | goto slow_path; |
| 168 | |
| 169 | if (unlikely(v_cmpxchg(config, &ctx->buf->offset, o_old, o_end) |
| 170 | != o_old)) |
| 171 | goto slow_path; |
| 172 | |
| 173 | /* |
| 174 | * Atomically update last_tsc. This update races against concurrent |
| 175 | * atomic updates, but the race will always cause supplementary full TSC |
| 176 | * record headers, never the opposite (missing a full TSC record header |
| 177 | * when it would be needed). |
| 178 | */ |
| 179 | save_last_tsc(config, ctx->buf, ctx->tsc); |
| 180 | |
| 181 | /* |
| 182 | * Push the reader if necessary |
| 183 | */ |
| 184 | lib_ring_buffer_reserve_push_reader(ctx->buf, chan, o_end - 1); |
| 185 | |
| 186 | /* |
| 187 | * Clear noref flag for this subbuffer. |
| 188 | */ |
| 189 | lib_ring_buffer_clear_noref(config, &ctx->buf->backend, |
| 190 | subbuf_index(o_end - 1, chan)); |
| 191 | |
| 192 | ctx->pre_offset = o_begin; |
| 193 | ctx->buf_offset = o_begin + before_hdr_pad; |
| 194 | return 0; |
| 195 | slow_path: |
| 196 | return lib_ring_buffer_reserve_slow(ctx); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * lib_ring_buffer_switch - Perform a sub-buffer switch for a per-cpu buffer. |
| 201 | * @config: ring buffer instance configuration. |
| 202 | * @buf: buffer |
| 203 | * @mode: buffer switch mode (SWITCH_ACTIVE or SWITCH_FLUSH) |
| 204 | * |
| 205 | * This operation is completely reentrant : can be called while tracing is |
| 206 | * active with absolutely no lock held. |
| 207 | * |
| 208 | * Note, however, that as a v_cmpxchg is used for some atomic operations and |
| 209 | * requires to be executed locally for per-CPU buffers, this function must be |
| 210 | * called from the CPU which owns the buffer for a ACTIVE flush, with preemption |
| 211 | * disabled, for RING_BUFFER_SYNC_PER_CPU configuration. |
| 212 | */ |
| 213 | static inline |
| 214 | void lib_ring_buffer_switch(const struct lib_ring_buffer_config *config, |
| 215 | struct lib_ring_buffer *buf, enum switch_mode mode) |
| 216 | { |
| 217 | lib_ring_buffer_switch_slow(buf, mode); |
| 218 | } |
| 219 | |
| 220 | /* See ring_buffer_frontend_api.h for lib_ring_buffer_reserve(). */ |
| 221 | |
| 222 | /** |
| 223 | * lib_ring_buffer_commit - Commit an record. |
| 224 | * @config: ring buffer instance configuration. |
| 225 | * @ctx: ring buffer context. (input arguments only) |
| 226 | * |
| 227 | * Atomic unordered slot commit. Increments the commit count in the |
| 228 | * specified sub-buffer, and delivers it if necessary. |
| 229 | */ |
| 230 | static inline |
| 231 | void lib_ring_buffer_commit(const struct lib_ring_buffer_config *config, |
| 232 | const struct lib_ring_buffer_ctx *ctx) |
| 233 | { |
| 234 | struct channel *chan = ctx->chan; |
| 235 | struct lib_ring_buffer *buf = ctx->buf; |
| 236 | unsigned long offset_end = ctx->buf_offset; |
| 237 | unsigned long endidx = subbuf_index(offset_end - 1, chan); |
| 238 | unsigned long commit_count; |
| 239 | |
| 240 | /* |
| 241 | * Must count record before incrementing the commit count. |
| 242 | */ |
| 243 | subbuffer_count_record(config, &buf->backend, endidx); |
| 244 | |
| 245 | /* |
| 246 | * Order all writes to buffer before the commit count update that will |
| 247 | * determine that the subbuffer is full. |
| 248 | */ |
| 249 | if (config->ipi == RING_BUFFER_IPI_BARRIER) { |
| 250 | /* |
| 251 | * Must write slot data before incrementing commit count. This |
| 252 | * compiler barrier is upgraded into a smp_mb() by the IPI sent |
| 253 | * by get_subbuf(). |
| 254 | */ |
| 255 | barrier(); |
| 256 | } else |
| 257 | smp_wmb(); |
| 258 | |
| 259 | v_add(config, ctx->slot_size, &buf->commit_hot[endidx].cc); |
| 260 | |
| 261 | /* |
| 262 | * commit count read can race with concurrent OOO commit count updates. |
| 263 | * This is only needed for lib_ring_buffer_check_deliver (for |
| 264 | * non-polling delivery only) and for |
| 265 | * lib_ring_buffer_write_commit_counter. The race can only cause the |
| 266 | * counter to be read with the same value more than once, which could |
| 267 | * cause : |
| 268 | * - Multiple delivery for the same sub-buffer (which is handled |
| 269 | * gracefully by the reader code) if the value is for a full |
| 270 | * sub-buffer. It's important that we can never miss a sub-buffer |
| 271 | * delivery. Re-reading the value after the v_add ensures this. |
| 272 | * - Reading a commit_count with a higher value that what was actually |
| 273 | * added to it for the lib_ring_buffer_write_commit_counter call |
| 274 | * (again caused by a concurrent committer). It does not matter, |
| 275 | * because this function is interested in the fact that the commit |
| 276 | * count reaches back the reserve offset for a specific sub-buffer, |
| 277 | * which is completely independent of the order. |
| 278 | */ |
| 279 | commit_count = v_read(config, &buf->commit_hot[endidx].cc); |
| 280 | |
| 281 | lib_ring_buffer_check_deliver(config, buf, chan, offset_end - 1, |
| 282 | commit_count, endidx); |
| 283 | /* |
| 284 | * Update used size at each commit. It's needed only for extracting |
| 285 | * ring_buffer buffers from vmcore, after crash. |
| 286 | */ |
| 287 | lib_ring_buffer_write_commit_counter(config, buf, chan, endidx, |
| 288 | ctx->buf_offset, commit_count, |
| 289 | ctx->slot_size); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * lib_ring_buffer_try_discard_reserve - Try discarding a record. |
| 294 | * @config: ring buffer instance configuration. |
| 295 | * @ctx: ring buffer context. (input arguments only) |
| 296 | * |
| 297 | * Only succeeds if no other record has been written after the record to |
| 298 | * discard. If discard fails, the record must be committed to the buffer. |
| 299 | * |
| 300 | * Returns 0 upon success, -EPERM if the record cannot be discarded. |
| 301 | */ |
| 302 | static inline |
| 303 | int lib_ring_buffer_try_discard_reserve(const struct lib_ring_buffer_config *config, |
| 304 | const struct lib_ring_buffer_ctx *ctx) |
| 305 | { |
| 306 | struct lib_ring_buffer *buf = ctx->buf; |
| 307 | unsigned long end_offset = ctx->pre_offset + ctx->slot_size; |
| 308 | |
| 309 | /* |
| 310 | * We need to ensure that if the cmpxchg succeeds and discards the |
| 311 | * record, the next record will record a full TSC, because it cannot |
| 312 | * rely on the last_tsc associated with the discarded record to detect |
| 313 | * overflows. The only way to ensure this is to set the last_tsc to 0 |
| 314 | * (assuming no 64-bit TSC overflow), which forces to write a 64-bit |
| 315 | * timestamp in the next record. |
| 316 | * |
| 317 | * Note: if discard fails, we must leave the TSC in the record header. |
| 318 | * It is needed to keep track of TSC overflows for the following |
| 319 | * records. |
| 320 | */ |
| 321 | save_last_tsc(config, buf, 0ULL); |
| 322 | |
| 323 | if (likely(v_cmpxchg(config, &buf->offset, end_offset, ctx->pre_offset) |
| 324 | != end_offset)) |
| 325 | return -EPERM; |
| 326 | else |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static inline |
| 331 | void channel_record_disable(const struct lib_ring_buffer_config *config, |
| 332 | struct channel *chan) |
| 333 | { |
| 334 | atomic_inc(&chan->record_disabled); |
| 335 | } |
| 336 | |
| 337 | static inline |
| 338 | void channel_record_enable(const struct lib_ring_buffer_config *config, |
| 339 | struct channel *chan) |
| 340 | { |
| 341 | atomic_dec(&chan->record_disabled); |
| 342 | } |
| 343 | |
| 344 | static inline |
| 345 | void lib_ring_buffer_record_disable(const struct lib_ring_buffer_config *config, |
| 346 | struct lib_ring_buffer *buf) |
| 347 | { |
| 348 | atomic_inc(&buf->record_disabled); |
| 349 | } |
| 350 | |
| 351 | static inline |
| 352 | void lib_ring_buffer_record_enable(const struct lib_ring_buffer_config *config, |
| 353 | struct lib_ring_buffer *buf) |
| 354 | { |
| 355 | atomic_dec(&buf->record_disabled); |
| 356 | } |
| 357 | |
| 358 | #endif /* _LINUX_RING_BUFFER_FRONTEND_API_H */ |