1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
3 * ringbuffer/backend_internal.h
5 * Ring buffer backend (internal helpers).
7 * Copyright (C) 2008-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #ifndef _LIB_RING_BUFFER_BACKEND_INTERNAL_H
11 #define _LIB_RING_BUFFER_BACKEND_INTERNAL_H
13 #include <wrapper/compiler.h>
14 #include <wrapper/inline_memcpy.h>
15 #include <ringbuffer/config.h>
16 #include <ringbuffer/backend_types.h>
17 #include <ringbuffer/frontend_types.h>
18 #include <linux/string.h>
19 #include <linux/uaccess.h>
21 /* Ring buffer backend API presented to the frontend */
23 /* Ring buffer and channel backend create/free */
25 int lib_ring_buffer_backend_create(struct lttng_kernel_ring_buffer_backend
*bufb
,
26 struct channel_backend
*chan
, int cpu
);
27 void channel_backend_unregister_notifiers(struct channel_backend
*chanb
);
28 void lib_ring_buffer_backend_free(struct lttng_kernel_ring_buffer_backend
*bufb
);
29 int channel_backend_init(struct channel_backend
*chanb
,
31 const struct lttng_kernel_ring_buffer_config
*config
,
32 void *priv
, size_t subbuf_size
,
34 void channel_backend_free(struct channel_backend
*chanb
);
36 void lib_ring_buffer_backend_reset(struct lttng_kernel_ring_buffer_backend
*bufb
);
37 void channel_backend_reset(struct channel_backend
*chanb
);
39 int lib_ring_buffer_backend_init(void);
40 void lib_ring_buffer_backend_exit(void);
42 extern void _lib_ring_buffer_write(struct lttng_kernel_ring_buffer_backend
*bufb
,
43 size_t offset
, const void *src
, size_t len
,
45 extern void _lib_ring_buffer_memset(struct lttng_kernel_ring_buffer_backend
*bufb
,
46 size_t offset
, int c
, size_t len
,
48 extern void _lib_ring_buffer_strcpy(struct lttng_kernel_ring_buffer_backend
*bufb
,
49 size_t offset
, const char *src
, size_t len
, int pad
);
50 extern void _lib_ring_buffer_pstrcpy(struct lttng_kernel_ring_buffer_backend
*bufb
,
51 size_t offset
, const char *src
, size_t len
, int pad
);
52 extern void _lib_ring_buffer_copy_from_user_inatomic(struct lttng_kernel_ring_buffer_backend
*bufb
,
53 size_t offset
, const void *src
,
54 size_t len
, size_t pagecpy
);
55 extern void _lib_ring_buffer_strcpy_from_user_inatomic(struct lttng_kernel_ring_buffer_backend
*bufb
,
56 size_t offset
, const char __user
*src
, size_t len
, int pad
);
57 extern void _lib_ring_buffer_pstrcpy_from_user_inatomic(struct lttng_kernel_ring_buffer_backend
*bufb
,
58 size_t offset
, const char __user
*src
, size_t len
, int pad
);
61 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
62 * exchanged atomically.
64 * Top half word, except lowest bit, belongs to "offset", which is used to keep
65 * to count the produced buffers. For overwrite mode, this provides the
66 * consumer with the capacity to read subbuffers in order, handling the
67 * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit
68 * systems) concurrently with a single execution of get_subbuf (between offset
69 * sampling and subbuffer ID exchange).
72 #define HALF_ULONG_BITS (BITS_PER_LONG >> 1)
74 #define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1)
75 #define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT)
76 #define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1))
78 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
80 #define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1)
81 #define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT)
82 #define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT
84 * In overwrite mode: lowest half of word is used for index.
85 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
86 * In producer-consumer mode: whole word used for index.
88 #define SB_ID_INDEX_SHIFT 0
89 #define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT)
90 #define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1)
93 * Construct the subbuffer id from offset, index and noref. Use only the index
94 * for producer-consumer mode (offset and noref are only used in overwrite
98 unsigned long subbuffer_id(const struct lttng_kernel_ring_buffer_config
*config
,
99 unsigned long offset
, unsigned long noref
,
102 if (config
->mode
== RING_BUFFER_OVERWRITE
)
103 return (offset
<< SB_ID_OFFSET_SHIFT
)
104 | (noref
<< SB_ID_NOREF_SHIFT
)
111 * Compare offset with the offset contained within id. Return 1 if the offset
112 * bits are identical, else 0.
115 int subbuffer_id_compare_offset(const struct lttng_kernel_ring_buffer_config
*config
,
116 unsigned long id
, unsigned long offset
)
118 return (id
& SB_ID_OFFSET_MASK
) == (offset
<< SB_ID_OFFSET_SHIFT
);
122 unsigned long subbuffer_id_get_index(const struct lttng_kernel_ring_buffer_config
*config
,
125 if (config
->mode
== RING_BUFFER_OVERWRITE
)
126 return id
& SB_ID_INDEX_MASK
;
132 unsigned long subbuffer_id_is_noref(const struct lttng_kernel_ring_buffer_config
*config
,
135 if (config
->mode
== RING_BUFFER_OVERWRITE
)
136 return !!(id
& SB_ID_NOREF_MASK
);
142 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
146 void subbuffer_id_set_noref(const struct lttng_kernel_ring_buffer_config
*config
,
149 if (config
->mode
== RING_BUFFER_OVERWRITE
)
150 *id
|= SB_ID_NOREF_MASK
;
154 void subbuffer_id_set_noref_offset(const struct lttng_kernel_ring_buffer_config
*config
,
155 unsigned long *id
, unsigned long offset
)
159 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
161 tmp
&= ~SB_ID_OFFSET_MASK
;
162 tmp
|= offset
<< SB_ID_OFFSET_SHIFT
;
163 tmp
|= SB_ID_NOREF_MASK
;
164 /* Volatile store, read concurrently by readers. */
165 WRITE_ONCE(*id
, tmp
);
169 /* No volatile access, since already used locally */
171 void subbuffer_id_clear_noref(const struct lttng_kernel_ring_buffer_config
*config
,
174 if (config
->mode
== RING_BUFFER_OVERWRITE
)
175 *id
&= ~SB_ID_NOREF_MASK
;
179 * For overwrite mode, cap the number of subbuffers per buffer to:
180 * 2^16 on 32-bit architectures
181 * 2^32 on 64-bit architectures
182 * This is required to fit in the index part of the ID. Return 0 on success,
186 int subbuffer_id_check_index(const struct lttng_kernel_ring_buffer_config
*config
,
187 unsigned long num_subbuf
)
189 if (config
->mode
== RING_BUFFER_OVERWRITE
)
190 return (num_subbuf
> (1UL << HALF_ULONG_BITS
)) ? -EPERM
: 0;
196 void lib_ring_buffer_backend_get_pages(const struct lttng_kernel_ring_buffer_config
*config
,
197 struct lttng_kernel_ring_buffer_ctx
*ctx
,
198 struct lttng_kernel_ring_buffer_backend_pages
**backend_pages
)
200 struct lttng_kernel_ring_buffer_backend
*bufb
= &ctx
->priv
.buf
->backend
;
201 struct channel_backend
*chanb
= &ctx
->priv
.chan
->backend
;
202 size_t sbidx
, offset
= ctx
->priv
.buf_offset
;
203 unsigned long sb_bindex
, id
;
204 struct lttng_kernel_ring_buffer_backend_pages
*rpages
;
206 offset
&= chanb
->buf_size
- 1;
207 sbidx
= offset
>> chanb
->subbuf_size_order
;
208 id
= bufb
->buf_wsb
[sbidx
].id
;
209 sb_bindex
= subbuffer_id_get_index(config
, id
);
210 rpages
= bufb
->array
[sb_bindex
];
211 CHAN_WARN_ON(ctx
->priv
.chan
,
212 config
->mode
== RING_BUFFER_OVERWRITE
213 && subbuffer_id_is_noref(config
, id
));
214 *backend_pages
= rpages
;
217 /* Get backend pages from cache. */
219 struct lttng_kernel_ring_buffer_backend_pages
*
220 lib_ring_buffer_get_backend_pages_from_ctx(const struct lttng_kernel_ring_buffer_config
*config
,
221 struct lttng_kernel_ring_buffer_ctx
*ctx
)
223 return ctx
->priv
.backend_pages
;
227 * The ring buffer can count events recorded and overwritten per buffer,
228 * but it is disabled by default due to its performance overhead.
230 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
232 void subbuffer_count_record(const struct lttng_kernel_ring_buffer_config
*config
,
233 struct lttng_kernel_ring_buffer_backend
*bufb
,
236 unsigned long sb_bindex
;
238 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
239 v_inc(config
, &bufb
->array
[sb_bindex
]->records_commit
);
243 * Reader has exclusive subbuffer access for record consumption. No need to
244 * perform the decrement atomically.
247 void subbuffer_consume_record(const struct lttng_kernel_ring_buffer_config
*config
,
248 struct lttng_kernel_ring_buffer_backend
*bufb
)
250 unsigned long sb_bindex
;
252 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
253 CHAN_WARN_ON(bufb
->chan
,
254 !v_read(config
, &bufb
->array
[sb_bindex
]->records_unread
));
255 /* Non-atomic decrement protected by exclusive subbuffer access */
256 _v_dec(config
, &bufb
->array
[sb_bindex
]->records_unread
);
257 v_inc(config
, &bufb
->records_read
);
259 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
261 void subbuffer_count_record(const struct lttng_kernel_ring_buffer_config
*config
,
262 struct lttng_kernel_ring_buffer_backend
*bufb
,
267 void subbuffer_consume_record(const struct lttng_kernel_ring_buffer_config
*config
,
268 struct lttng_kernel_ring_buffer_backend
*bufb
)
271 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
274 unsigned long subbuffer_get_records_count(
275 const struct lttng_kernel_ring_buffer_config
*config
,
276 struct lttng_kernel_ring_buffer_backend
*bufb
,
279 unsigned long sb_bindex
;
281 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
282 return v_read(config
, &bufb
->array
[sb_bindex
]->records_commit
);
286 * Must be executed at subbuffer delivery when the writer has _exclusive_
287 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
288 * lib_ring_buffer_get_records_count() must be called to get the records
289 * count before this function, because it resets the records_commit
293 unsigned long subbuffer_count_records_overrun(
294 const struct lttng_kernel_ring_buffer_config
*config
,
295 struct lttng_kernel_ring_buffer_backend
*bufb
,
298 struct lttng_kernel_ring_buffer_backend_pages
*pages
;
299 unsigned long overruns
, sb_bindex
;
301 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
302 pages
= bufb
->array
[sb_bindex
];
303 overruns
= v_read(config
, &pages
->records_unread
);
304 v_set(config
, &pages
->records_unread
,
305 v_read(config
, &pages
->records_commit
));
306 v_set(config
, &pages
->records_commit
, 0);
312 void subbuffer_set_data_size(const struct lttng_kernel_ring_buffer_config
*config
,
313 struct lttng_kernel_ring_buffer_backend
*bufb
,
315 unsigned long data_size
)
317 struct lttng_kernel_ring_buffer_backend_pages
*pages
;
318 unsigned long sb_bindex
;
320 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
321 pages
= bufb
->array
[sb_bindex
];
322 pages
->data_size
= data_size
;
326 unsigned long subbuffer_get_read_data_size(
327 const struct lttng_kernel_ring_buffer_config
*config
,
328 struct lttng_kernel_ring_buffer_backend
*bufb
)
330 struct lttng_kernel_ring_buffer_backend_pages
*pages
;
331 unsigned long sb_bindex
;
333 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
334 pages
= bufb
->array
[sb_bindex
];
335 return pages
->data_size
;
339 unsigned long subbuffer_get_data_size(
340 const struct lttng_kernel_ring_buffer_config
*config
,
341 struct lttng_kernel_ring_buffer_backend
*bufb
,
344 struct lttng_kernel_ring_buffer_backend_pages
*pages
;
345 unsigned long sb_bindex
;
347 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
348 pages
= bufb
->array
[sb_bindex
];
349 return pages
->data_size
;
353 void subbuffer_inc_packet_count(const struct lttng_kernel_ring_buffer_config
*config
,
354 struct lttng_kernel_ring_buffer_backend
*bufb
,
357 bufb
->buf_cnt
[idx
].seq_cnt
++;
361 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
365 void lib_ring_buffer_clear_noref(const struct lttng_kernel_ring_buffer_config
*config
,
366 struct lttng_kernel_ring_buffer_backend
*bufb
,
369 unsigned long id
, new_id
;
371 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
375 * Performing a volatile access to read the sb_pages, because we want to
376 * read a coherent version of the pointer and the associated noref flag.
378 id
= LTTNG_READ_ONCE(bufb
->buf_wsb
[idx
].id
);
380 /* This check is called on the fast path for each record. */
381 if (likely(!subbuffer_id_is_noref(config
, id
))) {
383 * Store after load dependency ordering the writes to
384 * the subbuffer after load and test of the noref flag
385 * matches the memory barrier implied by the cmpxchg()
386 * in update_read_sb_index().
388 return; /* Already writing to this buffer */
391 subbuffer_id_clear_noref(config
, &new_id
);
392 new_id
= cmpxchg(&bufb
->buf_wsb
[idx
].id
, id
, new_id
);
393 if (likely(new_id
== id
))
400 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
404 void lib_ring_buffer_set_noref_offset(const struct lttng_kernel_ring_buffer_config
*config
,
405 struct lttng_kernel_ring_buffer_backend
*bufb
,
406 unsigned long idx
, unsigned long offset
)
408 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
412 * Because ring_buffer_set_noref() is only called by a single thread
413 * (the one which updated the cc_sb value), there are no concurrent
414 * updates to take care of: other writers have not updated cc_sb, so
415 * they cannot set the noref flag, and concurrent readers cannot modify
416 * the pointer because the noref flag is not set yet.
417 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
418 * to the subbuffer before this set noref operation.
419 * subbuffer_set_noref() uses a volatile store to deal with concurrent
420 * readers of the noref flag.
422 CHAN_WARN_ON(bufb
->chan
,
423 subbuffer_id_is_noref(config
, bufb
->buf_wsb
[idx
].id
));
425 * Memory barrier that ensures counter stores are ordered before set
429 subbuffer_id_set_noref_offset(config
, &bufb
->buf_wsb
[idx
].id
, offset
);
433 * update_read_sb_index - Read-side subbuffer index update.
436 int update_read_sb_index(const struct lttng_kernel_ring_buffer_config
*config
,
437 struct lttng_kernel_ring_buffer_backend
*bufb
,
438 struct channel_backend
*chanb
,
439 unsigned long consumed_idx
,
440 unsigned long consumed_count
)
442 unsigned long old_id
, new_id
;
444 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
446 * Exchange the target writer subbuffer with our own unused
447 * subbuffer. No need to use READ_ONCE() here to read the
448 * old_wpage, because the value read will be confirmed by the
449 * following cmpxchg().
451 old_id
= bufb
->buf_wsb
[consumed_idx
].id
;
452 if (unlikely(!subbuffer_id_is_noref(config
, old_id
)))
455 * Make sure the offset count we are expecting matches the one
456 * indicated by the writer.
458 if (unlikely(!subbuffer_id_compare_offset(config
, old_id
,
461 CHAN_WARN_ON(bufb
->chan
,
462 !subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
463 subbuffer_id_set_noref_offset(config
, &bufb
->buf_rsb
.id
,
465 new_id
= cmpxchg(&bufb
->buf_wsb
[consumed_idx
].id
, old_id
,
467 if (unlikely(old_id
!= new_id
))
469 bufb
->buf_rsb
.id
= new_id
;
471 /* No page exchange, use the writer page directly */
472 bufb
->buf_rsb
.id
= bufb
->buf_wsb
[consumed_idx
].id
;
477 static inline __attribute__((always_inline
))
478 void lttng_inline_memcpy(void *dest
, const void *src
,
483 *(uint8_t *) dest
= *(const uint8_t *) src
;
486 *(uint16_t *) dest
= *(const uint16_t *) src
;
489 *(uint32_t *) dest
= *(const uint32_t *) src
;
492 *(uint64_t *) dest
= *(const uint64_t *) src
;
495 inline_memcpy(dest
, src
, len
);
500 * Use the architecture-specific memcpy implementation for constant-sized
501 * inputs, but rely on an inline memcpy for length statically unknown.
502 * The function call to memcpy is just way too expensive for a fast path.
504 #define lib_ring_buffer_do_copy(config, dest, src, len) \
506 size_t __len = (len); \
507 if (__builtin_constant_p(len)) \
508 memcpy(dest, src, __len); \
510 lttng_inline_memcpy(dest, src, __len); \
514 * We use __copy_from_user_inatomic to copy userspace data since we already
515 * did the access_ok for the whole range.
517 * Return 0 if OK, nonzero on error.
520 unsigned long lib_ring_buffer_do_copy_from_user_inatomic(void *dest
,
521 const void __user
*src
,
524 return __copy_from_user_inatomic(dest
, src
, len
);
528 * write len bytes to dest with c
531 void lib_ring_buffer_do_memset(char *dest
, int c
,
536 for (i
= 0; i
< len
; i
++)
540 #endif /* _LIB_RING_BUFFER_BACKEND_INTERNAL_H */