1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
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 <include/ringbuffer/config.h>
14 #include <include/ringbuffer/backend_types.h>
15 #include <include/ringbuffer/frontend_types.h>
16 #include <linux/string.h>
17 #include <linux/uaccess.h>
19 /* Ring buffer backend API presented to the frontend */
21 /* Ring buffer and channel backend create/free */
23 int lib_ring_buffer_backend_create(struct lib_ring_buffer_backend
*bufb
,
24 struct channel_backend
*chan
, int cpu
);
25 void channel_backend_unregister_notifiers(struct channel_backend
*chanb
);
26 void lib_ring_buffer_backend_free(struct lib_ring_buffer_backend
*bufb
);
27 int channel_backend_init(struct channel_backend
*chanb
,
29 const struct lib_ring_buffer_config
*config
,
30 void *priv
, size_t subbuf_size
,
32 void channel_backend_free(struct channel_backend
*chanb
);
34 void lib_ring_buffer_backend_reset(struct lib_ring_buffer_backend
*bufb
);
35 void channel_backend_reset(struct channel_backend
*chanb
);
37 int lib_ring_buffer_backend_init(void);
38 void lib_ring_buffer_backend_exit(void);
40 extern void _lib_ring_buffer_write(struct lib_ring_buffer_backend
*bufb
,
41 size_t offset
, const void *src
, size_t len
,
43 extern void _lib_ring_buffer_memset(struct lib_ring_buffer_backend
*bufb
,
44 size_t offset
, int c
, size_t len
,
46 extern void _lib_ring_buffer_strcpy(struct lib_ring_buffer_backend
*bufb
,
47 size_t offset
, const char *src
, size_t len
,
48 size_t pagecpy
, int pad
);
49 extern void _lib_ring_buffer_copy_from_user_inatomic(struct lib_ring_buffer_backend
*bufb
,
50 size_t offset
, const void *src
,
51 size_t len
, size_t pagecpy
);
52 extern void _lib_ring_buffer_strcpy_from_user_inatomic(struct lib_ring_buffer_backend
*bufb
,
53 size_t offset
, const char __user
*src
, size_t len
,
54 size_t pagecpy
, int pad
);
57 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
58 * exchanged atomically.
60 * Top half word, except lowest bit, belongs to "offset", which is used to keep
61 * to count the produced buffers. For overwrite mode, this provides the
62 * consumer with the capacity to read subbuffers in order, handling the
63 * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit
64 * systems) concurrently with a single execution of get_subbuf (between offset
65 * sampling and subbuffer ID exchange).
68 #define HALF_ULONG_BITS (BITS_PER_LONG >> 1)
70 #define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1)
71 #define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT)
72 #define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1))
74 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
76 #define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1)
77 #define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT)
78 #define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT
80 * In overwrite mode: lowest half of word is used for index.
81 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
82 * In producer-consumer mode: whole word used for index.
84 #define SB_ID_INDEX_SHIFT 0
85 #define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT)
86 #define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1)
89 * Construct the subbuffer id from offset, index and noref. Use only the index
90 * for producer-consumer mode (offset and noref are only used in overwrite
94 unsigned long subbuffer_id(const struct lib_ring_buffer_config
*config
,
95 unsigned long offset
, unsigned long noref
,
98 if (config
->mode
== RING_BUFFER_OVERWRITE
)
99 return (offset
<< SB_ID_OFFSET_SHIFT
)
100 | (noref
<< SB_ID_NOREF_SHIFT
)
107 * Compare offset with the offset contained within id. Return 1 if the offset
108 * bits are identical, else 0.
111 int subbuffer_id_compare_offset(const struct lib_ring_buffer_config
*config
,
112 unsigned long id
, unsigned long offset
)
114 return (id
& SB_ID_OFFSET_MASK
) == (offset
<< SB_ID_OFFSET_SHIFT
);
118 unsigned long subbuffer_id_get_index(const struct lib_ring_buffer_config
*config
,
121 if (config
->mode
== RING_BUFFER_OVERWRITE
)
122 return id
& SB_ID_INDEX_MASK
;
128 unsigned long subbuffer_id_is_noref(const struct lib_ring_buffer_config
*config
,
131 if (config
->mode
== RING_BUFFER_OVERWRITE
)
132 return !!(id
& SB_ID_NOREF_MASK
);
138 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
142 void subbuffer_id_set_noref(const struct lib_ring_buffer_config
*config
,
145 if (config
->mode
== RING_BUFFER_OVERWRITE
)
146 *id
|= SB_ID_NOREF_MASK
;
150 void subbuffer_id_set_noref_offset(const struct lib_ring_buffer_config
*config
,
151 unsigned long *id
, unsigned long offset
)
155 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
157 tmp
&= ~SB_ID_OFFSET_MASK
;
158 tmp
|= offset
<< SB_ID_OFFSET_SHIFT
;
159 tmp
|= SB_ID_NOREF_MASK
;
160 /* Volatile store, read concurrently by readers. */
161 WRITE_ONCE(*id
, tmp
);
165 /* No volatile access, since already used locally */
167 void subbuffer_id_clear_noref(const struct lib_ring_buffer_config
*config
,
170 if (config
->mode
== RING_BUFFER_OVERWRITE
)
171 *id
&= ~SB_ID_NOREF_MASK
;
175 * For overwrite mode, cap the number of subbuffers per buffer to:
176 * 2^16 on 32-bit architectures
177 * 2^32 on 64-bit architectures
178 * This is required to fit in the index part of the ID. Return 0 on success,
182 int subbuffer_id_check_index(const struct lib_ring_buffer_config
*config
,
183 unsigned long num_subbuf
)
185 if (config
->mode
== RING_BUFFER_OVERWRITE
)
186 return (num_subbuf
> (1UL << HALF_ULONG_BITS
)) ? -EPERM
: 0;
192 void lib_ring_buffer_backend_get_pages(const struct lib_ring_buffer_config
*config
,
193 struct lib_ring_buffer_ctx
*ctx
,
194 struct lib_ring_buffer_backend_pages
**backend_pages
)
196 struct lib_ring_buffer_backend
*bufb
= &ctx
->buf
->backend
;
197 struct channel_backend
*chanb
= &ctx
->chan
->backend
;
198 size_t sbidx
, offset
= ctx
->buf_offset
;
199 unsigned long sb_bindex
, id
;
200 struct lib_ring_buffer_backend_pages
*rpages
;
202 offset
&= chanb
->buf_size
- 1;
203 sbidx
= offset
>> chanb
->subbuf_size_order
;
204 id
= bufb
->buf_wsb
[sbidx
].id
;
205 sb_bindex
= subbuffer_id_get_index(config
, id
);
206 rpages
= bufb
->array
[sb_bindex
];
207 CHAN_WARN_ON(ctx
->chan
,
208 config
->mode
== RING_BUFFER_OVERWRITE
209 && subbuffer_id_is_noref(config
, id
));
210 *backend_pages
= rpages
;
213 /* Get backend pages from cache. */
215 struct lib_ring_buffer_backend_pages
*
216 lib_ring_buffer_get_backend_pages_from_ctx(const struct lib_ring_buffer_config
*config
,
217 struct lib_ring_buffer_ctx
*ctx
)
219 return ctx
->backend_pages
;
223 * The ring buffer can count events recorded and overwritten per buffer,
224 * but it is disabled by default due to its performance overhead.
226 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
228 void subbuffer_count_record(const struct lib_ring_buffer_config
*config
,
229 struct lib_ring_buffer_backend
*bufb
,
232 unsigned long sb_bindex
;
234 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
235 v_inc(config
, &bufb
->array
[sb_bindex
]->records_commit
);
237 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
239 void subbuffer_count_record(const struct lib_ring_buffer_config
*config
,
240 struct lib_ring_buffer_backend
*bufb
,
244 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
247 * Reader has exclusive subbuffer access for record consumption. No need to
248 * perform the decrement atomically.
251 void subbuffer_consume_record(const struct lib_ring_buffer_config
*config
,
252 struct lib_ring_buffer_backend
*bufb
)
254 unsigned long sb_bindex
;
256 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
257 CHAN_WARN_ON(bufb
->chan
,
258 !v_read(config
, &bufb
->array
[sb_bindex
]->records_unread
));
259 /* Non-atomic decrement protected by exclusive subbuffer access */
260 _v_dec(config
, &bufb
->array
[sb_bindex
]->records_unread
);
261 v_inc(config
, &bufb
->records_read
);
265 unsigned long subbuffer_get_records_count(
266 const struct lib_ring_buffer_config
*config
,
267 struct lib_ring_buffer_backend
*bufb
,
270 unsigned long sb_bindex
;
272 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
273 return v_read(config
, &bufb
->array
[sb_bindex
]->records_commit
);
277 * Must be executed at subbuffer delivery when the writer has _exclusive_
278 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
279 * lib_ring_buffer_get_records_count() must be called to get the records
280 * count before this function, because it resets the records_commit
284 unsigned long subbuffer_count_records_overrun(
285 const struct lib_ring_buffer_config
*config
,
286 struct lib_ring_buffer_backend
*bufb
,
289 struct lib_ring_buffer_backend_pages
*pages
;
290 unsigned long overruns
, sb_bindex
;
292 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
293 pages
= bufb
->array
[sb_bindex
];
294 overruns
= v_read(config
, &pages
->records_unread
);
295 v_set(config
, &pages
->records_unread
,
296 v_read(config
, &pages
->records_commit
));
297 v_set(config
, &pages
->records_commit
, 0);
303 void subbuffer_set_data_size(const struct lib_ring_buffer_config
*config
,
304 struct lib_ring_buffer_backend
*bufb
,
306 unsigned long data_size
)
308 struct lib_ring_buffer_backend_pages
*pages
;
309 unsigned long sb_bindex
;
311 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
312 pages
= bufb
->array
[sb_bindex
];
313 pages
->data_size
= data_size
;
317 unsigned long subbuffer_get_read_data_size(
318 const struct lib_ring_buffer_config
*config
,
319 struct lib_ring_buffer_backend
*bufb
)
321 struct lib_ring_buffer_backend_pages
*pages
;
322 unsigned long sb_bindex
;
324 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
325 pages
= bufb
->array
[sb_bindex
];
326 return pages
->data_size
;
330 unsigned long subbuffer_get_data_size(
331 const struct lib_ring_buffer_config
*config
,
332 struct lib_ring_buffer_backend
*bufb
,
335 struct lib_ring_buffer_backend_pages
*pages
;
336 unsigned long sb_bindex
;
338 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
339 pages
= bufb
->array
[sb_bindex
];
340 return pages
->data_size
;
344 void subbuffer_inc_packet_count(const struct lib_ring_buffer_config
*config
,
345 struct lib_ring_buffer_backend
*bufb
,
348 bufb
->buf_cnt
[idx
].seq_cnt
++;
352 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
356 void lib_ring_buffer_clear_noref(const struct lib_ring_buffer_config
*config
,
357 struct lib_ring_buffer_backend
*bufb
,
360 unsigned long id
, new_id
;
362 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
366 * Performing a volatile access to read the sb_pages, because we want to
367 * read a coherent version of the pointer and the associated noref flag.
369 id
= READ_ONCE(bufb
->buf_wsb
[idx
].id
);
371 /* This check is called on the fast path for each record. */
372 if (likely(!subbuffer_id_is_noref(config
, id
))) {
374 * Store after load dependency ordering the writes to
375 * the subbuffer after load and test of the noref flag
376 * matches the memory barrier implied by the cmpxchg()
377 * in update_read_sb_index().
379 return; /* Already writing to this buffer */
382 subbuffer_id_clear_noref(config
, &new_id
);
383 new_id
= cmpxchg(&bufb
->buf_wsb
[idx
].id
, id
, new_id
);
384 if (likely(new_id
== id
))
391 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
395 void lib_ring_buffer_set_noref_offset(const struct lib_ring_buffer_config
*config
,
396 struct lib_ring_buffer_backend
*bufb
,
397 unsigned long idx
, unsigned long offset
)
399 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
403 * Because ring_buffer_set_noref() is only called by a single thread
404 * (the one which updated the cc_sb value), there are no concurrent
405 * updates to take care of: other writers have not updated cc_sb, so
406 * they cannot set the noref flag, and concurrent readers cannot modify
407 * the pointer because the noref flag is not set yet.
408 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
409 * to the subbuffer before this set noref operation.
410 * subbuffer_set_noref() uses a volatile store to deal with concurrent
411 * readers of the noref flag.
413 CHAN_WARN_ON(bufb
->chan
,
414 subbuffer_id_is_noref(config
, bufb
->buf_wsb
[idx
].id
));
416 * Memory barrier that ensures counter stores are ordered before set
420 subbuffer_id_set_noref_offset(config
, &bufb
->buf_wsb
[idx
].id
, offset
);
424 * update_read_sb_index - Read-side subbuffer index update.
427 int update_read_sb_index(const struct lib_ring_buffer_config
*config
,
428 struct lib_ring_buffer_backend
*bufb
,
429 struct channel_backend
*chanb
,
430 unsigned long consumed_idx
,
431 unsigned long consumed_count
)
433 unsigned long old_id
, new_id
;
435 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
437 * Exchange the target writer subbuffer with our own unused
438 * subbuffer. No need to use READ_ONCE() here to read the
439 * old_wpage, because the value read will be confirmed by the
440 * following cmpxchg().
442 old_id
= bufb
->buf_wsb
[consumed_idx
].id
;
443 if (unlikely(!subbuffer_id_is_noref(config
, old_id
)))
446 * Make sure the offset count we are expecting matches the one
447 * indicated by the writer.
449 if (unlikely(!subbuffer_id_compare_offset(config
, old_id
,
452 CHAN_WARN_ON(bufb
->chan
,
453 !subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
454 subbuffer_id_set_noref_offset(config
, &bufb
->buf_rsb
.id
,
456 new_id
= cmpxchg(&bufb
->buf_wsb
[consumed_idx
].id
, old_id
,
458 if (unlikely(old_id
!= new_id
))
460 bufb
->buf_rsb
.id
= new_id
;
462 /* No page exchange, use the writer page directly */
463 bufb
->buf_rsb
.id
= bufb
->buf_wsb
[consumed_idx
].id
;
468 static inline __attribute__((always_inline
))
469 void lttng_inline_memcpy(void *dest
, const void *src
,
474 *(uint8_t *) dest
= *(const uint8_t *) src
;
477 *(uint16_t *) dest
= *(const uint16_t *) src
;
480 *(uint32_t *) dest
= *(const uint32_t *) src
;
483 *(uint64_t *) dest
= *(const uint64_t *) src
;
486 memcpy(dest
, src
, len
);
491 * Use the architecture-specific memcpy implementation for constant-sized
492 * inputs, but rely on an inline memcpy for length statically unknown.
493 * The function call to memcpy is just way too expensive for a fast path.
495 #define lib_ring_buffer_do_copy(config, dest, src, len) \
497 size_t __len = (len); \
498 if (__builtin_constant_p(len)) \
499 memcpy(dest, src, __len); \
501 lttng_inline_memcpy(dest, src, __len); \
505 * We use __copy_from_user_inatomic to copy userspace data since we already
506 * did the access_ok for the whole range.
508 * Return 0 if OK, nonzero on error.
511 unsigned long lib_ring_buffer_do_copy_from_user_inatomic(void *dest
,
512 const void __user
*src
,
515 return __copy_from_user_inatomic(dest
, src
, len
);
519 * write len bytes to dest with c
522 void lib_ring_buffer_do_memset(char *dest
, int c
,
527 for (i
= 0; i
< len
; i
++)
531 #endif /* _LIB_RING_BUFFER_BACKEND_INTERNAL_H */