Rename struct lib_ring_buffer_ctx to struct lttng_kernel_ring_buffer_ctx
[lttng-modules.git] / include / ringbuffer / backend_internal.h
CommitLineData
b7cdc182 1/* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
9f36eaed 2 *
24591303 3 * ringbuffer/backend_internal.h
f3bc08c5
MD
4 *
5 * Ring buffer backend (internal helpers).
6 *
886d51a3 7 * Copyright (C) 2008-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
f3bc08c5
MD
8 */
9
9f36eaed
MJ
10#ifndef _LIB_RING_BUFFER_BACKEND_INTERNAL_H
11#define _LIB_RING_BUFFER_BACKEND_INTERNAL_H
12
a8f2d0c7 13#include <wrapper/compiler.h>
24591303
MD
14#include <wrapper/inline_memcpy.h>
15#include <ringbuffer/config.h>
16#include <ringbuffer/backend_types.h>
17#include <ringbuffer/frontend_types.h>
f3bc08c5 18#include <linux/string.h>
4ea00e4f 19#include <linux/uaccess.h>
f3bc08c5
MD
20
21/* Ring buffer backend API presented to the frontend */
22
23/* Ring buffer and channel backend create/free */
24
25int lib_ring_buffer_backend_create(struct lib_ring_buffer_backend *bufb,
26 struct channel_backend *chan, int cpu);
27void channel_backend_unregister_notifiers(struct channel_backend *chanb);
28void lib_ring_buffer_backend_free(struct lib_ring_buffer_backend *bufb);
29int channel_backend_init(struct channel_backend *chanb,
30 const char *name,
31 const struct lib_ring_buffer_config *config,
32 void *priv, size_t subbuf_size,
33 size_t num_subbuf);
34void channel_backend_free(struct channel_backend *chanb);
35
36void lib_ring_buffer_backend_reset(struct lib_ring_buffer_backend *bufb);
37void channel_backend_reset(struct channel_backend *chanb);
38
39int lib_ring_buffer_backend_init(void);
40void lib_ring_buffer_backend_exit(void);
41
42extern void _lib_ring_buffer_write(struct lib_ring_buffer_backend *bufb,
43 size_t offset, const void *src, size_t len,
bfe529f9 44 size_t pagecpy);
4ea00e4f
JD
45extern void _lib_ring_buffer_memset(struct lib_ring_buffer_backend *bufb,
46 size_t offset, int c, size_t len,
bfe529f9 47 size_t pagecpy);
16f78f3a
MD
48extern void _lib_ring_buffer_strcpy(struct lib_ring_buffer_backend *bufb,
49 size_t offset, const char *src, size_t len,
50 size_t pagecpy, int pad);
7b8ea3a5 51extern void _lib_ring_buffer_copy_from_user_inatomic(struct lib_ring_buffer_backend *bufb,
4ea00e4f 52 size_t offset, const void *src,
bfe529f9 53 size_t len, size_t pagecpy);
16f78f3a
MD
54extern void _lib_ring_buffer_strcpy_from_user_inatomic(struct lib_ring_buffer_backend *bufb,
55 size_t offset, const char __user *src, size_t len,
56 size_t pagecpy, int pad);
f3bc08c5
MD
57
58/*
59 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
60 * exchanged atomically.
61 *
62 * Top half word, except lowest bit, belongs to "offset", which is used to keep
63 * to count the produced buffers. For overwrite mode, this provides the
64 * consumer with the capacity to read subbuffers in order, handling the
65 * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit
66 * systems) concurrently with a single execution of get_subbuf (between offset
67 * sampling and subbuffer ID exchange).
68 */
69
70#define HALF_ULONG_BITS (BITS_PER_LONG >> 1)
71
72#define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1)
73#define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT)
74#define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1))
75/*
76 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
77 */
78#define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1)
79#define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT)
80#define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT
81/*
82 * In overwrite mode: lowest half of word is used for index.
83 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
84 * In producer-consumer mode: whole word used for index.
85 */
86#define SB_ID_INDEX_SHIFT 0
87#define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT)
88#define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1)
89
90/*
91 * Construct the subbuffer id from offset, index and noref. Use only the index
92 * for producer-consumer mode (offset and noref are only used in overwrite
93 * mode).
94 */
95static inline
96unsigned long subbuffer_id(const struct lib_ring_buffer_config *config,
97 unsigned long offset, unsigned long noref,
98 unsigned long index)
99{
100 if (config->mode == RING_BUFFER_OVERWRITE)
101 return (offset << SB_ID_OFFSET_SHIFT)
102 | (noref << SB_ID_NOREF_SHIFT)
103 | index;
104 else
105 return index;
106}
107
108/*
109 * Compare offset with the offset contained within id. Return 1 if the offset
110 * bits are identical, else 0.
111 */
112static inline
113int subbuffer_id_compare_offset(const struct lib_ring_buffer_config *config,
114 unsigned long id, unsigned long offset)
115{
116 return (id & SB_ID_OFFSET_MASK) == (offset << SB_ID_OFFSET_SHIFT);
117}
118
119static inline
120unsigned long subbuffer_id_get_index(const struct lib_ring_buffer_config *config,
121 unsigned long id)
122{
123 if (config->mode == RING_BUFFER_OVERWRITE)
124 return id & SB_ID_INDEX_MASK;
125 else
126 return id;
127}
128
129static inline
130unsigned long subbuffer_id_is_noref(const struct lib_ring_buffer_config *config,
131 unsigned long id)
132{
133 if (config->mode == RING_BUFFER_OVERWRITE)
134 return !!(id & SB_ID_NOREF_MASK);
135 else
136 return 1;
137}
138
139/*
140 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
141 * needed.
142 */
143static inline
144void subbuffer_id_set_noref(const struct lib_ring_buffer_config *config,
145 unsigned long *id)
146{
147 if (config->mode == RING_BUFFER_OVERWRITE)
148 *id |= SB_ID_NOREF_MASK;
149}
150
151static inline
152void subbuffer_id_set_noref_offset(const struct lib_ring_buffer_config *config,
153 unsigned long *id, unsigned long offset)
154{
155 unsigned long tmp;
156
157 if (config->mode == RING_BUFFER_OVERWRITE) {
158 tmp = *id;
159 tmp &= ~SB_ID_OFFSET_MASK;
160 tmp |= offset << SB_ID_OFFSET_SHIFT;
161 tmp |= SB_ID_NOREF_MASK;
162 /* Volatile store, read concurrently by readers. */
a8f2d0c7 163 WRITE_ONCE(*id, tmp);
f3bc08c5
MD
164 }
165}
166
167/* No volatile access, since already used locally */
168static inline
169void subbuffer_id_clear_noref(const struct lib_ring_buffer_config *config,
170 unsigned long *id)
171{
172 if (config->mode == RING_BUFFER_OVERWRITE)
173 *id &= ~SB_ID_NOREF_MASK;
174}
175
176/*
177 * For overwrite mode, cap the number of subbuffers per buffer to:
178 * 2^16 on 32-bit architectures
179 * 2^32 on 64-bit architectures
180 * This is required to fit in the index part of the ID. Return 0 on success,
181 * -EPERM on failure.
182 */
183static inline
184int subbuffer_id_check_index(const struct lib_ring_buffer_config *config,
185 unsigned long num_subbuf)
186{
187 if (config->mode == RING_BUFFER_OVERWRITE)
188 return (num_subbuf > (1UL << HALF_ULONG_BITS)) ? -EPERM : 0;
189 else
190 return 0;
191}
192
85a07c33
MD
193static inline
194void lib_ring_buffer_backend_get_pages(const struct lib_ring_buffer_config *config,
8a57ec02 195 struct lttng_kernel_ring_buffer_ctx *ctx,
85a07c33
MD
196 struct lib_ring_buffer_backend_pages **backend_pages)
197{
b1199bd3
MD
198 struct lib_ring_buffer_backend *bufb = &ctx->priv.buf->backend;
199 struct channel_backend *chanb = &ctx->priv.chan->backend;
200 size_t sbidx, offset = ctx->priv.buf_offset;
85a07c33
MD
201 unsigned long sb_bindex, id;
202 struct lib_ring_buffer_backend_pages *rpages;
203
204 offset &= chanb->buf_size - 1;
205 sbidx = offset >> chanb->subbuf_size_order;
206 id = bufb->buf_wsb[sbidx].id;
207 sb_bindex = subbuffer_id_get_index(config, id);
208 rpages = bufb->array[sb_bindex];
b1199bd3 209 CHAN_WARN_ON(ctx->priv.chan,
85a07c33
MD
210 config->mode == RING_BUFFER_OVERWRITE
211 && subbuffer_id_is_noref(config, id));
212 *backend_pages = rpages;
213}
214
215/* Get backend pages from cache. */
216static inline
217struct lib_ring_buffer_backend_pages *
218 lib_ring_buffer_get_backend_pages_from_ctx(const struct lib_ring_buffer_config *config,
8a57ec02 219 struct lttng_kernel_ring_buffer_ctx *ctx)
85a07c33 220{
b1199bd3 221 return ctx->priv.backend_pages;
85a07c33
MD
222}
223
25337cb5
MD
224/*
225 * The ring buffer can count events recorded and overwritten per buffer,
226 * but it is disabled by default due to its performance overhead.
227 */
228#ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
f3bc08c5
MD
229static inline
230void subbuffer_count_record(const struct lib_ring_buffer_config *config,
231 struct lib_ring_buffer_backend *bufb,
232 unsigned long idx)
233{
234 unsigned long sb_bindex;
235
236 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
237 v_inc(config, &bufb->array[sb_bindex]->records_commit);
238}
239
240/*
241 * Reader has exclusive subbuffer access for record consumption. No need to
242 * perform the decrement atomically.
243 */
244static inline
245void subbuffer_consume_record(const struct lib_ring_buffer_config *config,
246 struct lib_ring_buffer_backend *bufb)
247{
248 unsigned long sb_bindex;
249
250 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
251 CHAN_WARN_ON(bufb->chan,
252 !v_read(config, &bufb->array[sb_bindex]->records_unread));
253 /* Non-atomic decrement protected by exclusive subbuffer access */
254 _v_dec(config, &bufb->array[sb_bindex]->records_unread);
255 v_inc(config, &bufb->records_read);
256}
935150de
MD
257#else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
258static inline
259void subbuffer_count_record(const struct lib_ring_buffer_config *config,
260 struct lib_ring_buffer_backend *bufb,
261 unsigned long idx)
262{
263}
264static inline
265void subbuffer_consume_record(const struct lib_ring_buffer_config *config,
266 struct lib_ring_buffer_backend *bufb)
267{
268}
269#endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
f3bc08c5
MD
270
271static inline
272unsigned long subbuffer_get_records_count(
273 const struct lib_ring_buffer_config *config,
274 struct lib_ring_buffer_backend *bufb,
275 unsigned long idx)
276{
277 unsigned long sb_bindex;
278
279 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
280 return v_read(config, &bufb->array[sb_bindex]->records_commit);
281}
282
283/*
284 * Must be executed at subbuffer delivery when the writer has _exclusive_
c68be968
MD
285 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
286 * lib_ring_buffer_get_records_count() must be called to get the records
287 * count before this function, because it resets the records_commit
288 * count.
f3bc08c5
MD
289 */
290static inline
291unsigned long subbuffer_count_records_overrun(
292 const struct lib_ring_buffer_config *config,
293 struct lib_ring_buffer_backend *bufb,
294 unsigned long idx)
295{
296 struct lib_ring_buffer_backend_pages *pages;
297 unsigned long overruns, sb_bindex;
298
299 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
300 pages = bufb->array[sb_bindex];
301 overruns = v_read(config, &pages->records_unread);
302 v_set(config, &pages->records_unread,
303 v_read(config, &pages->records_commit));
304 v_set(config, &pages->records_commit, 0);
305
306 return overruns;
307}
308
309static inline
310void subbuffer_set_data_size(const struct lib_ring_buffer_config *config,
311 struct lib_ring_buffer_backend *bufb,
312 unsigned long idx,
313 unsigned long data_size)
314{
315 struct lib_ring_buffer_backend_pages *pages;
316 unsigned long sb_bindex;
317
318 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
319 pages = bufb->array[sb_bindex];
320 pages->data_size = data_size;
321}
322
323static inline
324unsigned long subbuffer_get_read_data_size(
325 const struct lib_ring_buffer_config *config,
326 struct lib_ring_buffer_backend *bufb)
327{
328 struct lib_ring_buffer_backend_pages *pages;
329 unsigned long sb_bindex;
330
331 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
332 pages = bufb->array[sb_bindex];
333 return pages->data_size;
334}
335
336static inline
337unsigned long subbuffer_get_data_size(
338 const struct lib_ring_buffer_config *config,
339 struct lib_ring_buffer_backend *bufb,
340 unsigned long idx)
341{
342 struct lib_ring_buffer_backend_pages *pages;
343 unsigned long sb_bindex;
344
345 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
346 pages = bufb->array[sb_bindex];
347 return pages->data_size;
348}
349
5b3cf4f9
JD
350static inline
351void subbuffer_inc_packet_count(const struct lib_ring_buffer_config *config,
352 struct lib_ring_buffer_backend *bufb,
353 unsigned long idx)
354{
355 bufb->buf_cnt[idx].seq_cnt++;
356}
357
f3bc08c5
MD
358/**
359 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
360 * writer.
361 */
362static inline
363void lib_ring_buffer_clear_noref(const struct lib_ring_buffer_config *config,
364 struct lib_ring_buffer_backend *bufb,
365 unsigned long idx)
366{
367 unsigned long id, new_id;
368
369 if (config->mode != RING_BUFFER_OVERWRITE)
370 return;
371
372 /*
373 * Performing a volatile access to read the sb_pages, because we want to
374 * read a coherent version of the pointer and the associated noref flag.
375 */
585e5dcc 376 id = LTTNG_READ_ONCE(bufb->buf_wsb[idx].id);
f3bc08c5
MD
377 for (;;) {
378 /* This check is called on the fast path for each record. */
379 if (likely(!subbuffer_id_is_noref(config, id))) {
380 /*
381 * Store after load dependency ordering the writes to
382 * the subbuffer after load and test of the noref flag
383 * matches the memory barrier implied by the cmpxchg()
384 * in update_read_sb_index().
385 */
386 return; /* Already writing to this buffer */
387 }
388 new_id = id;
389 subbuffer_id_clear_noref(config, &new_id);
390 new_id = cmpxchg(&bufb->buf_wsb[idx].id, id, new_id);
391 if (likely(new_id == id))
392 break;
393 id = new_id;
394 }
395}
396
397/**
398 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
399 * called by writer.
400 */
401static inline
402void lib_ring_buffer_set_noref_offset(const struct lib_ring_buffer_config *config,
403 struct lib_ring_buffer_backend *bufb,
404 unsigned long idx, unsigned long offset)
405{
406 if (config->mode != RING_BUFFER_OVERWRITE)
407 return;
408
409 /*
410 * Because ring_buffer_set_noref() is only called by a single thread
411 * (the one which updated the cc_sb value), there are no concurrent
412 * updates to take care of: other writers have not updated cc_sb, so
413 * they cannot set the noref flag, and concurrent readers cannot modify
414 * the pointer because the noref flag is not set yet.
415 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
416 * to the subbuffer before this set noref operation.
417 * subbuffer_set_noref() uses a volatile store to deal with concurrent
418 * readers of the noref flag.
419 */
420 CHAN_WARN_ON(bufb->chan,
421 subbuffer_id_is_noref(config, bufb->buf_wsb[idx].id));
422 /*
423 * Memory barrier that ensures counter stores are ordered before set
424 * noref and offset.
425 */
426 smp_mb();
427 subbuffer_id_set_noref_offset(config, &bufb->buf_wsb[idx].id, offset);
428}
429
430/**
431 * update_read_sb_index - Read-side subbuffer index update.
432 */
433static inline
434int update_read_sb_index(const struct lib_ring_buffer_config *config,
435 struct lib_ring_buffer_backend *bufb,
436 struct channel_backend *chanb,
437 unsigned long consumed_idx,
438 unsigned long consumed_count)
439{
440 unsigned long old_id, new_id;
441
442 if (config->mode == RING_BUFFER_OVERWRITE) {
443 /*
444 * Exchange the target writer subbuffer with our own unused
a8f2d0c7 445 * subbuffer. No need to use READ_ONCE() here to read the
f3bc08c5
MD
446 * old_wpage, because the value read will be confirmed by the
447 * following cmpxchg().
448 */
449 old_id = bufb->buf_wsb[consumed_idx].id;
450 if (unlikely(!subbuffer_id_is_noref(config, old_id)))
451 return -EAGAIN;
452 /*
453 * Make sure the offset count we are expecting matches the one
454 * indicated by the writer.
455 */
456 if (unlikely(!subbuffer_id_compare_offset(config, old_id,
457 consumed_count)))
458 return -EAGAIN;
459 CHAN_WARN_ON(bufb->chan,
460 !subbuffer_id_is_noref(config, bufb->buf_rsb.id));
461 subbuffer_id_set_noref_offset(config, &bufb->buf_rsb.id,
462 consumed_count);
463 new_id = cmpxchg(&bufb->buf_wsb[consumed_idx].id, old_id,
464 bufb->buf_rsb.id);
465 if (unlikely(old_id != new_id))
466 return -EAGAIN;
467 bufb->buf_rsb.id = new_id;
468 } else {
469 /* No page exchange, use the writer page directly */
470 bufb->buf_rsb.id = bufb->buf_wsb[consumed_idx].id;
471 }
472 return 0;
473}
474
327177e6 475static inline __attribute__((always_inline))
04707312 476void lttng_inline_memcpy(void *dest, const void *src,
327177e6
MD
477 unsigned long len)
478{
479 switch (len) {
480 case 1:
481 *(uint8_t *) dest = *(const uint8_t *) src;
482 break;
483 case 2:
484 *(uint16_t *) dest = *(const uint16_t *) src;
485 break;
486 case 4:
487 *(uint32_t *) dest = *(const uint32_t *) src;
488 break;
489 case 8:
490 *(uint64_t *) dest = *(const uint64_t *) src;
491 break;
492 default:
493 inline_memcpy(dest, src, len);
494 }
495}
496
f3bc08c5
MD
497/*
498 * Use the architecture-specific memcpy implementation for constant-sized
499 * inputs, but rely on an inline memcpy for length statically unknown.
500 * The function call to memcpy is just way too expensive for a fast path.
501 */
502#define lib_ring_buffer_do_copy(config, dest, src, len) \
503do { \
504 size_t __len = (len); \
505 if (__builtin_constant_p(len)) \
506 memcpy(dest, src, __len); \
507 else \
327177e6 508 lttng_inline_memcpy(dest, src, __len); \
f3bc08c5
MD
509} while (0)
510
4ea00e4f 511/*
7b8ea3a5 512 * We use __copy_from_user_inatomic to copy userspace data since we already
4ea00e4f 513 * did the access_ok for the whole range.
d87a9f03
MD
514 *
515 * Return 0 if OK, nonzero on error.
4ea00e4f
JD
516 */
517static inline
7b8ea3a5 518unsigned long lib_ring_buffer_do_copy_from_user_inatomic(void *dest,
4ea00e4f
JD
519 const void __user *src,
520 unsigned long len)
521{
7b8ea3a5 522 return __copy_from_user_inatomic(dest, src, len);
4ea00e4f
JD
523}
524
525/*
526 * write len bytes to dest with c
527 */
528static inline
529void lib_ring_buffer_do_memset(char *dest, int c,
530 unsigned long len)
531{
532 unsigned long i;
533
534 for (i = 0; i < len; i++)
535 dest[i] = c;
536}
537
886d51a3 538#endif /* _LIB_RING_BUFFER_BACKEND_INTERNAL_H */
This page took 0.06453 seconds and 4 git commands to generate.