1 #ifndef _LIB_RING_BUFFER_BACKEND_INTERNAL_H
2 #define _LIB_RING_BUFFER_BACKEND_INTERNAL_H
5 * lib/ringbuffer/backend_internal.h
7 * Ring buffer backend (internal helpers).
9 * Copyright (C) 2008-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; only
14 * version 2.1 of the License.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <wrapper/compiler.h>
27 #include <wrapper/ringbuffer/config.h>
28 #include <wrapper/ringbuffer/backend_types.h>
29 #include <wrapper/ringbuffer/frontend_types.h>
30 #include <linux/string.h>
31 #include <linux/uaccess.h>
33 /* Ring buffer backend API presented to the frontend */
35 /* Ring buffer and channel backend create/free */
37 int lib_ring_buffer_backend_create(struct lib_ring_buffer_backend
*bufb
,
38 struct channel_backend
*chan
, int cpu
);
39 void channel_backend_unregister_notifiers(struct channel_backend
*chanb
);
40 void lib_ring_buffer_backend_free(struct lib_ring_buffer_backend
*bufb
);
41 int channel_backend_init(struct channel_backend
*chanb
,
43 const struct lib_ring_buffer_config
*config
,
44 void *priv
, size_t subbuf_size
,
46 void channel_backend_free(struct channel_backend
*chanb
);
48 void lib_ring_buffer_backend_reset(struct lib_ring_buffer_backend
*bufb
);
49 void channel_backend_reset(struct channel_backend
*chanb
);
51 int lib_ring_buffer_backend_init(void);
52 void lib_ring_buffer_backend_exit(void);
54 extern void _lib_ring_buffer_write(struct lib_ring_buffer_backend
*bufb
,
55 size_t offset
, const void *src
, size_t len
,
57 extern void _lib_ring_buffer_memset(struct lib_ring_buffer_backend
*bufb
,
58 size_t offset
, int c
, size_t len
,
60 extern void _lib_ring_buffer_strcpy(struct lib_ring_buffer_backend
*bufb
,
61 size_t offset
, const char *src
, size_t len
,
62 size_t pagecpy
, int pad
);
63 extern void _lib_ring_buffer_copy_from_user_inatomic(struct lib_ring_buffer_backend
*bufb
,
64 size_t offset
, const void *src
,
65 size_t len
, size_t pagecpy
);
66 extern void _lib_ring_buffer_strcpy_from_user_inatomic(struct lib_ring_buffer_backend
*bufb
,
67 size_t offset
, const char __user
*src
, size_t len
,
68 size_t pagecpy
, int pad
);
71 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
72 * exchanged atomically.
74 * Top half word, except lowest bit, belongs to "offset", which is used to keep
75 * to count the produced buffers. For overwrite mode, this provides the
76 * consumer with the capacity to read subbuffers in order, handling the
77 * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit
78 * systems) concurrently with a single execution of get_subbuf (between offset
79 * sampling and subbuffer ID exchange).
82 #define HALF_ULONG_BITS (BITS_PER_LONG >> 1)
84 #define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1)
85 #define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT)
86 #define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1))
88 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
90 #define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1)
91 #define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT)
92 #define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT
94 * In overwrite mode: lowest half of word is used for index.
95 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
96 * In producer-consumer mode: whole word used for index.
98 #define SB_ID_INDEX_SHIFT 0
99 #define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT)
100 #define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1)
103 * Construct the subbuffer id from offset, index and noref. Use only the index
104 * for producer-consumer mode (offset and noref are only used in overwrite
108 unsigned long subbuffer_id(const struct lib_ring_buffer_config
*config
,
109 unsigned long offset
, unsigned long noref
,
112 if (config
->mode
== RING_BUFFER_OVERWRITE
)
113 return (offset
<< SB_ID_OFFSET_SHIFT
)
114 | (noref
<< SB_ID_NOREF_SHIFT
)
121 * Compare offset with the offset contained within id. Return 1 if the offset
122 * bits are identical, else 0.
125 int subbuffer_id_compare_offset(const struct lib_ring_buffer_config
*config
,
126 unsigned long id
, unsigned long offset
)
128 return (id
& SB_ID_OFFSET_MASK
) == (offset
<< SB_ID_OFFSET_SHIFT
);
132 unsigned long subbuffer_id_get_index(const struct lib_ring_buffer_config
*config
,
135 if (config
->mode
== RING_BUFFER_OVERWRITE
)
136 return id
& SB_ID_INDEX_MASK
;
142 unsigned long subbuffer_id_is_noref(const struct lib_ring_buffer_config
*config
,
145 if (config
->mode
== RING_BUFFER_OVERWRITE
)
146 return !!(id
& SB_ID_NOREF_MASK
);
152 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
156 void subbuffer_id_set_noref(const struct lib_ring_buffer_config
*config
,
159 if (config
->mode
== RING_BUFFER_OVERWRITE
)
160 *id
|= SB_ID_NOREF_MASK
;
164 void subbuffer_id_set_noref_offset(const struct lib_ring_buffer_config
*config
,
165 unsigned long *id
, unsigned long offset
)
169 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
171 tmp
&= ~SB_ID_OFFSET_MASK
;
172 tmp
|= offset
<< SB_ID_OFFSET_SHIFT
;
173 tmp
|= SB_ID_NOREF_MASK
;
174 /* Volatile store, read concurrently by readers. */
175 WRITE_ONCE(*id
, tmp
);
179 /* No volatile access, since already used locally */
181 void subbuffer_id_clear_noref(const struct lib_ring_buffer_config
*config
,
184 if (config
->mode
== RING_BUFFER_OVERWRITE
)
185 *id
&= ~SB_ID_NOREF_MASK
;
189 * For overwrite mode, cap the number of subbuffers per buffer to:
190 * 2^16 on 32-bit architectures
191 * 2^32 on 64-bit architectures
192 * This is required to fit in the index part of the ID. Return 0 on success,
196 int subbuffer_id_check_index(const struct lib_ring_buffer_config
*config
,
197 unsigned long num_subbuf
)
199 if (config
->mode
== RING_BUFFER_OVERWRITE
)
200 return (num_subbuf
> (1UL << HALF_ULONG_BITS
)) ? -EPERM
: 0;
206 void lib_ring_buffer_backend_get_pages(const struct lib_ring_buffer_config
*config
,
207 struct lib_ring_buffer_ctx
*ctx
,
208 struct lib_ring_buffer_backend_pages
**backend_pages
)
210 struct lib_ring_buffer_backend
*bufb
= &ctx
->buf
->backend
;
211 struct channel_backend
*chanb
= &ctx
->chan
->backend
;
212 size_t sbidx
, offset
= ctx
->buf_offset
;
213 unsigned long sb_bindex
, id
;
214 struct lib_ring_buffer_backend_pages
*rpages
;
216 offset
&= chanb
->buf_size
- 1;
217 sbidx
= offset
>> chanb
->subbuf_size_order
;
218 id
= bufb
->buf_wsb
[sbidx
].id
;
219 sb_bindex
= subbuffer_id_get_index(config
, id
);
220 rpages
= bufb
->array
[sb_bindex
];
221 CHAN_WARN_ON(ctx
->chan
,
222 config
->mode
== RING_BUFFER_OVERWRITE
223 && subbuffer_id_is_noref(config
, id
));
224 *backend_pages
= rpages
;
227 /* Get backend pages from cache. */
229 struct lib_ring_buffer_backend_pages
*
230 lib_ring_buffer_get_backend_pages_from_ctx(const struct lib_ring_buffer_config
*config
,
231 struct lib_ring_buffer_ctx
*ctx
)
233 return ctx
->backend_pages
;
237 * The ring buffer can count events recorded and overwritten per buffer,
238 * but it is disabled by default due to its performance overhead.
240 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
242 void subbuffer_count_record(const struct lib_ring_buffer_config
*config
,
243 struct lib_ring_buffer_backend
*bufb
,
246 unsigned long sb_bindex
;
248 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
249 v_inc(config
, &bufb
->array
[sb_bindex
]->records_commit
);
251 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
253 void subbuffer_count_record(const struct lib_ring_buffer_config
*config
,
254 struct lib_ring_buffer_backend
*bufb
,
258 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
261 * Reader has exclusive subbuffer access for record consumption. No need to
262 * perform the decrement atomically.
265 void subbuffer_consume_record(const struct lib_ring_buffer_config
*config
,
266 struct lib_ring_buffer_backend
*bufb
)
268 unsigned long sb_bindex
;
270 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
271 CHAN_WARN_ON(bufb
->chan
,
272 !v_read(config
, &bufb
->array
[sb_bindex
]->records_unread
));
273 /* Non-atomic decrement protected by exclusive subbuffer access */
274 _v_dec(config
, &bufb
->array
[sb_bindex
]->records_unread
);
275 v_inc(config
, &bufb
->records_read
);
279 unsigned long subbuffer_get_records_count(
280 const struct lib_ring_buffer_config
*config
,
281 struct lib_ring_buffer_backend
*bufb
,
284 unsigned long sb_bindex
;
286 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
287 return v_read(config
, &bufb
->array
[sb_bindex
]->records_commit
);
291 * Must be executed at subbuffer delivery when the writer has _exclusive_
292 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
293 * lib_ring_buffer_get_records_count() must be called to get the records
294 * count before this function, because it resets the records_commit
298 unsigned long subbuffer_count_records_overrun(
299 const struct lib_ring_buffer_config
*config
,
300 struct lib_ring_buffer_backend
*bufb
,
303 struct lib_ring_buffer_backend_pages
*pages
;
304 unsigned long overruns
, sb_bindex
;
306 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
307 pages
= bufb
->array
[sb_bindex
];
308 overruns
= v_read(config
, &pages
->records_unread
);
309 v_set(config
, &pages
->records_unread
,
310 v_read(config
, &pages
->records_commit
));
311 v_set(config
, &pages
->records_commit
, 0);
317 void subbuffer_set_data_size(const struct lib_ring_buffer_config
*config
,
318 struct lib_ring_buffer_backend
*bufb
,
320 unsigned long data_size
)
322 struct lib_ring_buffer_backend_pages
*pages
;
323 unsigned long sb_bindex
;
325 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
326 pages
= bufb
->array
[sb_bindex
];
327 pages
->data_size
= data_size
;
331 unsigned long subbuffer_get_read_data_size(
332 const struct lib_ring_buffer_config
*config
,
333 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_rsb
.id
);
339 pages
= bufb
->array
[sb_bindex
];
340 return pages
->data_size
;
344 unsigned long subbuffer_get_data_size(
345 const struct lib_ring_buffer_config
*config
,
346 struct lib_ring_buffer_backend
*bufb
,
349 struct lib_ring_buffer_backend_pages
*pages
;
350 unsigned long sb_bindex
;
352 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
353 pages
= bufb
->array
[sb_bindex
];
354 return pages
->data_size
;
358 void subbuffer_inc_packet_count(const struct lib_ring_buffer_config
*config
,
359 struct lib_ring_buffer_backend
*bufb
,
362 bufb
->buf_cnt
[idx
].seq_cnt
++;
366 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
370 void lib_ring_buffer_clear_noref(const struct lib_ring_buffer_config
*config
,
371 struct lib_ring_buffer_backend
*bufb
,
374 unsigned long id
, new_id
;
376 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
380 * Performing a volatile access to read the sb_pages, because we want to
381 * read a coherent version of the pointer and the associated noref flag.
383 id
= READ_ONCE(bufb
->buf_wsb
[idx
].id
);
385 /* This check is called on the fast path for each record. */
386 if (likely(!subbuffer_id_is_noref(config
, id
))) {
388 * Store after load dependency ordering the writes to
389 * the subbuffer after load and test of the noref flag
390 * matches the memory barrier implied by the cmpxchg()
391 * in update_read_sb_index().
393 return; /* Already writing to this buffer */
396 subbuffer_id_clear_noref(config
, &new_id
);
397 new_id
= cmpxchg(&bufb
->buf_wsb
[idx
].id
, id
, new_id
);
398 if (likely(new_id
== id
))
405 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
409 void lib_ring_buffer_set_noref_offset(const struct lib_ring_buffer_config
*config
,
410 struct lib_ring_buffer_backend
*bufb
,
411 unsigned long idx
, unsigned long offset
)
413 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
417 * Because ring_buffer_set_noref() is only called by a single thread
418 * (the one which updated the cc_sb value), there are no concurrent
419 * updates to take care of: other writers have not updated cc_sb, so
420 * they cannot set the noref flag, and concurrent readers cannot modify
421 * the pointer because the noref flag is not set yet.
422 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
423 * to the subbuffer before this set noref operation.
424 * subbuffer_set_noref() uses a volatile store to deal with concurrent
425 * readers of the noref flag.
427 CHAN_WARN_ON(bufb
->chan
,
428 subbuffer_id_is_noref(config
, bufb
->buf_wsb
[idx
].id
));
430 * Memory barrier that ensures counter stores are ordered before set
434 subbuffer_id_set_noref_offset(config
, &bufb
->buf_wsb
[idx
].id
, offset
);
438 * update_read_sb_index - Read-side subbuffer index update.
441 int update_read_sb_index(const struct lib_ring_buffer_config
*config
,
442 struct lib_ring_buffer_backend
*bufb
,
443 struct channel_backend
*chanb
,
444 unsigned long consumed_idx
,
445 unsigned long consumed_count
)
447 unsigned long old_id
, new_id
;
449 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
451 * Exchange the target writer subbuffer with our own unused
452 * subbuffer. No need to use READ_ONCE() here to read the
453 * old_wpage, because the value read will be confirmed by the
454 * following cmpxchg().
456 old_id
= bufb
->buf_wsb
[consumed_idx
].id
;
457 if (unlikely(!subbuffer_id_is_noref(config
, old_id
)))
460 * Make sure the offset count we are expecting matches the one
461 * indicated by the writer.
463 if (unlikely(!subbuffer_id_compare_offset(config
, old_id
,
466 CHAN_WARN_ON(bufb
->chan
,
467 !subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
468 subbuffer_id_set_noref_offset(config
, &bufb
->buf_rsb
.id
,
470 new_id
= cmpxchg(&bufb
->buf_wsb
[consumed_idx
].id
, old_id
,
472 if (unlikely(old_id
!= new_id
))
474 bufb
->buf_rsb
.id
= new_id
;
476 /* No page exchange, use the writer page directly */
477 bufb
->buf_rsb
.id
= bufb
->buf_wsb
[consumed_idx
].id
;
482 static inline __attribute__((always_inline
))
483 void lttng_inline_memcpy(void *dest
, const void *src
,
488 *(uint8_t *) dest
= *(const uint8_t *) src
;
491 *(uint16_t *) dest
= *(const uint16_t *) src
;
494 *(uint32_t *) dest
= *(const uint32_t *) src
;
497 *(uint64_t *) dest
= *(const uint64_t *) src
;
500 inline_memcpy(dest
, src
, len
);
505 * Use the architecture-specific memcpy implementation for constant-sized
506 * inputs, but rely on an inline memcpy for length statically unknown.
507 * The function call to memcpy is just way too expensive for a fast path.
509 #define lib_ring_buffer_do_copy(config, dest, src, len) \
511 size_t __len = (len); \
512 if (__builtin_constant_p(len)) \
513 memcpy(dest, src, __len); \
515 lttng_inline_memcpy(dest, src, __len); \
519 * We use __copy_from_user_inatomic to copy userspace data since we already
520 * did the access_ok for the whole range.
522 * Return 0 if OK, nonzero on error.
525 unsigned long lib_ring_buffer_do_copy_from_user_inatomic(void *dest
,
526 const void __user
*src
,
529 return __copy_from_user_inatomic(dest
, src
, len
);
533 * write len bytes to dest with c
536 void lib_ring_buffer_do_memset(char *dest
, int c
,
541 for (i
= 0; i
< len
; i
++)
545 #endif /* _LIB_RING_BUFFER_BACKEND_INTERNAL_H */