1 /* SPDX-License-Identifier: (GPL-2.0 OR LGPL-2.1)
3 * lib/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/ringbuffer/config.h>
15 #include <wrapper/ringbuffer/backend_types.h>
16 #include <wrapper/ringbuffer/frontend_types.h>
17 #include <linux/string.h>
18 #include <linux/uaccess.h>
20 /* Ring buffer backend API presented to the frontend */
22 /* Ring buffer and channel backend create/free */
24 int lib_ring_buffer_backend_create(struct lib_ring_buffer_backend
*bufb
,
25 struct channel_backend
*chan
, int cpu
);
26 void channel_backend_unregister_notifiers(struct channel_backend
*chanb
);
27 void lib_ring_buffer_backend_free(struct lib_ring_buffer_backend
*bufb
);
28 int channel_backend_init(struct channel_backend
*chanb
,
30 const struct lib_ring_buffer_config
*config
,
31 void *priv
, size_t subbuf_size
,
33 void channel_backend_free(struct channel_backend
*chanb
);
35 void lib_ring_buffer_backend_reset(struct lib_ring_buffer_backend
*bufb
);
36 void channel_backend_reset(struct channel_backend
*chanb
);
38 int lib_ring_buffer_backend_init(void);
39 void lib_ring_buffer_backend_exit(void);
41 extern void _lib_ring_buffer_write(struct lib_ring_buffer_backend
*bufb
,
42 size_t offset
, const void *src
, size_t len
,
44 extern void _lib_ring_buffer_memset(struct lib_ring_buffer_backend
*bufb
,
45 size_t offset
, int c
, size_t len
,
47 extern void _lib_ring_buffer_strcpy(struct lib_ring_buffer_backend
*bufb
,
48 size_t offset
, const char *src
, size_t len
,
49 size_t pagecpy
, int pad
);
50 extern void _lib_ring_buffer_copy_from_user_inatomic(struct lib_ring_buffer_backend
*bufb
,
51 size_t offset
, const void *src
,
52 size_t len
, size_t pagecpy
);
53 extern void _lib_ring_buffer_strcpy_from_user_inatomic(struct lib_ring_buffer_backend
*bufb
,
54 size_t offset
, const char __user
*src
, size_t len
,
55 size_t pagecpy
, int pad
);
58 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
59 * exchanged atomically.
61 * Top half word, except lowest bit, belongs to "offset", which is used to keep
62 * to count the produced buffers. For overwrite mode, this provides the
63 * consumer with the capacity to read subbuffers in order, handling the
64 * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit
65 * systems) concurrently with a single execution of get_subbuf (between offset
66 * sampling and subbuffer ID exchange).
69 #define HALF_ULONG_BITS (BITS_PER_LONG >> 1)
71 #define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1)
72 #define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT)
73 #define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1))
75 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
77 #define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1)
78 #define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT)
79 #define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT
81 * In overwrite mode: lowest half of word is used for index.
82 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
83 * In producer-consumer mode: whole word used for index.
85 #define SB_ID_INDEX_SHIFT 0
86 #define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT)
87 #define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1)
90 * Construct the subbuffer id from offset, index and noref. Use only the index
91 * for producer-consumer mode (offset and noref are only used in overwrite
95 unsigned long subbuffer_id(const struct lib_ring_buffer_config
*config
,
96 unsigned long offset
, unsigned long noref
,
99 if (config
->mode
== RING_BUFFER_OVERWRITE
)
100 return (offset
<< SB_ID_OFFSET_SHIFT
)
101 | (noref
<< SB_ID_NOREF_SHIFT
)
108 * Compare offset with the offset contained within id. Return 1 if the offset
109 * bits are identical, else 0.
112 int subbuffer_id_compare_offset(const struct lib_ring_buffer_config
*config
,
113 unsigned long id
, unsigned long offset
)
115 return (id
& SB_ID_OFFSET_MASK
) == (offset
<< SB_ID_OFFSET_SHIFT
);
119 unsigned long subbuffer_id_get_index(const struct lib_ring_buffer_config
*config
,
122 if (config
->mode
== RING_BUFFER_OVERWRITE
)
123 return id
& SB_ID_INDEX_MASK
;
129 unsigned long subbuffer_id_is_noref(const struct lib_ring_buffer_config
*config
,
132 if (config
->mode
== RING_BUFFER_OVERWRITE
)
133 return !!(id
& SB_ID_NOREF_MASK
);
139 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
143 void subbuffer_id_set_noref(const struct lib_ring_buffer_config
*config
,
146 if (config
->mode
== RING_BUFFER_OVERWRITE
)
147 *id
|= SB_ID_NOREF_MASK
;
151 void subbuffer_id_set_noref_offset(const struct lib_ring_buffer_config
*config
,
152 unsigned long *id
, unsigned long offset
)
156 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
158 tmp
&= ~SB_ID_OFFSET_MASK
;
159 tmp
|= offset
<< SB_ID_OFFSET_SHIFT
;
160 tmp
|= SB_ID_NOREF_MASK
;
161 /* Volatile store, read concurrently by readers. */
162 WRITE_ONCE(*id
, tmp
);
166 /* No volatile access, since already used locally */
168 void subbuffer_id_clear_noref(const struct lib_ring_buffer_config
*config
,
171 if (config
->mode
== RING_BUFFER_OVERWRITE
)
172 *id
&= ~SB_ID_NOREF_MASK
;
176 * For overwrite mode, cap the number of subbuffers per buffer to:
177 * 2^16 on 32-bit architectures
178 * 2^32 on 64-bit architectures
179 * This is required to fit in the index part of the ID. Return 0 on success,
183 int subbuffer_id_check_index(const struct lib_ring_buffer_config
*config
,
184 unsigned long num_subbuf
)
186 if (config
->mode
== RING_BUFFER_OVERWRITE
)
187 return (num_subbuf
> (1UL << HALF_ULONG_BITS
)) ? -EPERM
: 0;
193 void lib_ring_buffer_backend_get_pages(const struct lib_ring_buffer_config
*config
,
194 struct lib_ring_buffer_ctx
*ctx
,
195 struct lib_ring_buffer_backend_pages
**backend_pages
)
197 struct lib_ring_buffer_backend
*bufb
= &ctx
->buf
->backend
;
198 struct channel_backend
*chanb
= &ctx
->chan
->backend
;
199 size_t sbidx
, offset
= ctx
->buf_offset
;
200 unsigned long sb_bindex
, id
;
201 struct lib_ring_buffer_backend_pages
*rpages
;
203 offset
&= chanb
->buf_size
- 1;
204 sbidx
= offset
>> chanb
->subbuf_size_order
;
205 id
= bufb
->buf_wsb
[sbidx
].id
;
206 sb_bindex
= subbuffer_id_get_index(config
, id
);
207 rpages
= bufb
->array
[sb_bindex
];
208 CHAN_WARN_ON(ctx
->chan
,
209 config
->mode
== RING_BUFFER_OVERWRITE
210 && subbuffer_id_is_noref(config
, id
));
211 *backend_pages
= rpages
;
214 /* Get backend pages from cache. */
216 struct lib_ring_buffer_backend_pages
*
217 lib_ring_buffer_get_backend_pages_from_ctx(const struct lib_ring_buffer_config
*config
,
218 struct lib_ring_buffer_ctx
*ctx
)
220 return ctx
->backend_pages
;
224 * The ring buffer can count events recorded and overwritten per buffer,
225 * but it is disabled by default due to its performance overhead.
227 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
229 void subbuffer_count_record(const struct lib_ring_buffer_config
*config
,
230 struct lib_ring_buffer_backend
*bufb
,
233 unsigned long sb_bindex
;
235 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
236 v_inc(config
, &bufb
->array
[sb_bindex
]->records_commit
);
238 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
240 void subbuffer_count_record(const struct lib_ring_buffer_config
*config
,
241 struct lib_ring_buffer_backend
*bufb
,
245 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
248 * Reader has exclusive subbuffer access for record consumption. No need to
249 * perform the decrement atomically.
252 void subbuffer_consume_record(const struct lib_ring_buffer_config
*config
,
253 struct lib_ring_buffer_backend
*bufb
)
255 unsigned long sb_bindex
;
257 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
258 CHAN_WARN_ON(bufb
->chan
,
259 !v_read(config
, &bufb
->array
[sb_bindex
]->records_unread
));
260 /* Non-atomic decrement protected by exclusive subbuffer access */
261 _v_dec(config
, &bufb
->array
[sb_bindex
]->records_unread
);
262 v_inc(config
, &bufb
->records_read
);
266 unsigned long subbuffer_get_records_count(
267 const struct lib_ring_buffer_config
*config
,
268 struct lib_ring_buffer_backend
*bufb
,
271 unsigned long sb_bindex
;
273 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
274 return v_read(config
, &bufb
->array
[sb_bindex
]->records_commit
);
278 * Must be executed at subbuffer delivery when the writer has _exclusive_
279 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
280 * lib_ring_buffer_get_records_count() must be called to get the records
281 * count before this function, because it resets the records_commit
285 unsigned long subbuffer_count_records_overrun(
286 const struct lib_ring_buffer_config
*config
,
287 struct lib_ring_buffer_backend
*bufb
,
290 struct lib_ring_buffer_backend_pages
*pages
;
291 unsigned long overruns
, sb_bindex
;
293 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
294 pages
= bufb
->array
[sb_bindex
];
295 overruns
= v_read(config
, &pages
->records_unread
);
296 v_set(config
, &pages
->records_unread
,
297 v_read(config
, &pages
->records_commit
));
298 v_set(config
, &pages
->records_commit
, 0);
304 void subbuffer_set_data_size(const struct lib_ring_buffer_config
*config
,
305 struct lib_ring_buffer_backend
*bufb
,
307 unsigned long data_size
)
309 struct lib_ring_buffer_backend_pages
*pages
;
310 unsigned long sb_bindex
;
312 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
313 pages
= bufb
->array
[sb_bindex
];
314 pages
->data_size
= data_size
;
318 unsigned long subbuffer_get_read_data_size(
319 const struct lib_ring_buffer_config
*config
,
320 struct lib_ring_buffer_backend
*bufb
)
322 struct lib_ring_buffer_backend_pages
*pages
;
323 unsigned long sb_bindex
;
325 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
326 pages
= bufb
->array
[sb_bindex
];
327 return pages
->data_size
;
331 unsigned long subbuffer_get_data_size(
332 const struct lib_ring_buffer_config
*config
,
333 struct lib_ring_buffer_backend
*bufb
,
336 struct lib_ring_buffer_backend_pages
*pages
;
337 unsigned long sb_bindex
;
339 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
340 pages
= bufb
->array
[sb_bindex
];
341 return pages
->data_size
;
345 void subbuffer_inc_packet_count(const struct lib_ring_buffer_config
*config
,
346 struct lib_ring_buffer_backend
*bufb
,
349 bufb
->buf_cnt
[idx
].seq_cnt
++;
353 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
357 void lib_ring_buffer_clear_noref(const struct lib_ring_buffer_config
*config
,
358 struct lib_ring_buffer_backend
*bufb
,
361 unsigned long id
, new_id
;
363 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
367 * Performing a volatile access to read the sb_pages, because we want to
368 * read a coherent version of the pointer and the associated noref flag.
370 id
= READ_ONCE(bufb
->buf_wsb
[idx
].id
);
372 /* This check is called on the fast path for each record. */
373 if (likely(!subbuffer_id_is_noref(config
, id
))) {
375 * Store after load dependency ordering the writes to
376 * the subbuffer after load and test of the noref flag
377 * matches the memory barrier implied by the cmpxchg()
378 * in update_read_sb_index().
380 return; /* Already writing to this buffer */
383 subbuffer_id_clear_noref(config
, &new_id
);
384 new_id
= cmpxchg(&bufb
->buf_wsb
[idx
].id
, id
, new_id
);
385 if (likely(new_id
== id
))
392 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
396 void lib_ring_buffer_set_noref_offset(const struct lib_ring_buffer_config
*config
,
397 struct lib_ring_buffer_backend
*bufb
,
398 unsigned long idx
, unsigned long offset
)
400 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
404 * Because ring_buffer_set_noref() is only called by a single thread
405 * (the one which updated the cc_sb value), there are no concurrent
406 * updates to take care of: other writers have not updated cc_sb, so
407 * they cannot set the noref flag, and concurrent readers cannot modify
408 * the pointer because the noref flag is not set yet.
409 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
410 * to the subbuffer before this set noref operation.
411 * subbuffer_set_noref() uses a volatile store to deal with concurrent
412 * readers of the noref flag.
414 CHAN_WARN_ON(bufb
->chan
,
415 subbuffer_id_is_noref(config
, bufb
->buf_wsb
[idx
].id
));
417 * Memory barrier that ensures counter stores are ordered before set
421 subbuffer_id_set_noref_offset(config
, &bufb
->buf_wsb
[idx
].id
, offset
);
425 * update_read_sb_index - Read-side subbuffer index update.
428 int update_read_sb_index(const struct lib_ring_buffer_config
*config
,
429 struct lib_ring_buffer_backend
*bufb
,
430 struct channel_backend
*chanb
,
431 unsigned long consumed_idx
,
432 unsigned long consumed_count
)
434 unsigned long old_id
, new_id
;
436 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
438 * Exchange the target writer subbuffer with our own unused
439 * subbuffer. No need to use READ_ONCE() here to read the
440 * old_wpage, because the value read will be confirmed by the
441 * following cmpxchg().
443 old_id
= bufb
->buf_wsb
[consumed_idx
].id
;
444 if (unlikely(!subbuffer_id_is_noref(config
, old_id
)))
447 * Make sure the offset count we are expecting matches the one
448 * indicated by the writer.
450 if (unlikely(!subbuffer_id_compare_offset(config
, old_id
,
453 CHAN_WARN_ON(bufb
->chan
,
454 !subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
455 subbuffer_id_set_noref_offset(config
, &bufb
->buf_rsb
.id
,
457 new_id
= cmpxchg(&bufb
->buf_wsb
[consumed_idx
].id
, old_id
,
459 if (unlikely(old_id
!= new_id
))
461 bufb
->buf_rsb
.id
= new_id
;
463 /* No page exchange, use the writer page directly */
464 bufb
->buf_rsb
.id
= bufb
->buf_wsb
[consumed_idx
].id
;
469 static inline __attribute__((always_inline
))
470 void lttng_inline_memcpy(void *dest
, const void *src
,
475 *(uint8_t *) dest
= *(const uint8_t *) src
;
478 *(uint16_t *) dest
= *(const uint16_t *) src
;
481 *(uint32_t *) dest
= *(const uint32_t *) src
;
484 *(uint64_t *) dest
= *(const uint64_t *) src
;
487 inline_memcpy(dest
, src
, len
);
492 * Use the architecture-specific memcpy implementation for constant-sized
493 * inputs, but rely on an inline memcpy for length statically unknown.
494 * The function call to memcpy is just way too expensive for a fast path.
496 #define lib_ring_buffer_do_copy(config, dest, src, len) \
498 size_t __len = (len); \
499 if (__builtin_constant_p(len)) \
500 memcpy(dest, src, __len); \
502 lttng_inline_memcpy(dest, src, __len); \
506 * We use __copy_from_user_inatomic to copy userspace data since we already
507 * did the access_ok for the whole range.
509 * Return 0 if OK, nonzero on error.
512 unsigned long lib_ring_buffer_do_copy_from_user_inatomic(void *dest
,
513 const void __user
*src
,
516 return __copy_from_user_inatomic(dest
, src
, len
);
520 * write len bytes to dest with c
523 void lib_ring_buffer_do_memset(char *dest
, int c
,
528 for (i
= 0; i
< len
; i
++)
532 #endif /* _LIB_RING_BUFFER_BACKEND_INTERNAL_H */