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/ringbuffer/config.h>
27 #include <wrapper/ringbuffer/backend_types.h>
28 #include <wrapper/ringbuffer/frontend_types.h>
29 #include <linux/string.h>
30 #include <linux/uaccess.h>
32 /* Ring buffer backend API presented to the frontend */
34 /* Ring buffer and channel backend create/free */
36 int lib_ring_buffer_backend_create(struct lib_ring_buffer_backend
*bufb
,
37 struct channel_backend
*chan
, int cpu
);
38 void channel_backend_unregister_notifiers(struct channel_backend
*chanb
);
39 void lib_ring_buffer_backend_free(struct lib_ring_buffer_backend
*bufb
);
40 int channel_backend_init(struct channel_backend
*chanb
,
42 const struct lib_ring_buffer_config
*config
,
43 void *priv
, size_t subbuf_size
,
45 void channel_backend_free(struct channel_backend
*chanb
);
47 void lib_ring_buffer_backend_reset(struct lib_ring_buffer_backend
*bufb
);
48 void channel_backend_reset(struct channel_backend
*chanb
);
50 int lib_ring_buffer_backend_init(void);
51 void lib_ring_buffer_backend_exit(void);
53 extern void _lib_ring_buffer_write(struct lib_ring_buffer_backend
*bufb
,
54 size_t offset
, const void *src
, size_t len
,
56 extern void _lib_ring_buffer_memset(struct lib_ring_buffer_backend
*bufb
,
57 size_t offset
, int c
, size_t len
,
59 extern void _lib_ring_buffer_strcpy(struct lib_ring_buffer_backend
*bufb
,
60 size_t offset
, const char *src
, size_t len
,
61 size_t pagecpy
, int pad
);
62 extern void _lib_ring_buffer_copy_from_user_inatomic(struct lib_ring_buffer_backend
*bufb
,
63 size_t offset
, const void *src
,
64 size_t len
, size_t pagecpy
);
65 extern void _lib_ring_buffer_strcpy_from_user_inatomic(struct lib_ring_buffer_backend
*bufb
,
66 size_t offset
, const char __user
*src
, size_t len
,
67 size_t pagecpy
, int pad
);
70 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
71 * exchanged atomically.
73 * Top half word, except lowest bit, belongs to "offset", which is used to keep
74 * to count the produced buffers. For overwrite mode, this provides the
75 * consumer with the capacity to read subbuffers in order, handling the
76 * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit
77 * systems) concurrently with a single execution of get_subbuf (between offset
78 * sampling and subbuffer ID exchange).
81 #define HALF_ULONG_BITS (BITS_PER_LONG >> 1)
83 #define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1)
84 #define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT)
85 #define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1))
87 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
89 #define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1)
90 #define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT)
91 #define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT
93 * In overwrite mode: lowest half of word is used for index.
94 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
95 * In producer-consumer mode: whole word used for index.
97 #define SB_ID_INDEX_SHIFT 0
98 #define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT)
99 #define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1)
102 * Construct the subbuffer id from offset, index and noref. Use only the index
103 * for producer-consumer mode (offset and noref are only used in overwrite
107 unsigned long subbuffer_id(const struct lib_ring_buffer_config
*config
,
108 unsigned long offset
, unsigned long noref
,
111 if (config
->mode
== RING_BUFFER_OVERWRITE
)
112 return (offset
<< SB_ID_OFFSET_SHIFT
)
113 | (noref
<< SB_ID_NOREF_SHIFT
)
120 * Compare offset with the offset contained within id. Return 1 if the offset
121 * bits are identical, else 0.
124 int subbuffer_id_compare_offset(const struct lib_ring_buffer_config
*config
,
125 unsigned long id
, unsigned long offset
)
127 return (id
& SB_ID_OFFSET_MASK
) == (offset
<< SB_ID_OFFSET_SHIFT
);
131 unsigned long subbuffer_id_get_index(const struct lib_ring_buffer_config
*config
,
134 if (config
->mode
== RING_BUFFER_OVERWRITE
)
135 return id
& SB_ID_INDEX_MASK
;
141 unsigned long subbuffer_id_is_noref(const struct lib_ring_buffer_config
*config
,
144 if (config
->mode
== RING_BUFFER_OVERWRITE
)
145 return !!(id
& SB_ID_NOREF_MASK
);
151 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
155 void subbuffer_id_set_noref(const struct lib_ring_buffer_config
*config
,
158 if (config
->mode
== RING_BUFFER_OVERWRITE
)
159 *id
|= SB_ID_NOREF_MASK
;
163 void subbuffer_id_set_noref_offset(const struct lib_ring_buffer_config
*config
,
164 unsigned long *id
, unsigned long offset
)
168 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
170 tmp
&= ~SB_ID_OFFSET_MASK
;
171 tmp
|= offset
<< SB_ID_OFFSET_SHIFT
;
172 tmp
|= SB_ID_NOREF_MASK
;
173 /* Volatile store, read concurrently by readers. */
174 ACCESS_ONCE(*id
) = tmp
;
178 /* No volatile access, since already used locally */
180 void subbuffer_id_clear_noref(const struct lib_ring_buffer_config
*config
,
183 if (config
->mode
== RING_BUFFER_OVERWRITE
)
184 *id
&= ~SB_ID_NOREF_MASK
;
188 * For overwrite mode, cap the number of subbuffers per buffer to:
189 * 2^16 on 32-bit architectures
190 * 2^32 on 64-bit architectures
191 * This is required to fit in the index part of the ID. Return 0 on success,
195 int subbuffer_id_check_index(const struct lib_ring_buffer_config
*config
,
196 unsigned long num_subbuf
)
198 if (config
->mode
== RING_BUFFER_OVERWRITE
)
199 return (num_subbuf
> (1UL << HALF_ULONG_BITS
)) ? -EPERM
: 0;
205 * The ring buffer can count events recorded and overwritten per buffer,
206 * but it is disabled by default due to its performance overhead.
208 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
210 void subbuffer_count_record(const struct lib_ring_buffer_config
*config
,
211 struct lib_ring_buffer_backend
*bufb
,
214 unsigned long sb_bindex
;
216 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
217 v_inc(config
, &bufb
->array
[sb_bindex
]->records_commit
);
219 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
221 void subbuffer_count_record(const struct lib_ring_buffer_config
*config
,
222 struct lib_ring_buffer_backend
*bufb
,
226 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
229 * Reader has exclusive subbuffer access for record consumption. No need to
230 * perform the decrement atomically.
233 void subbuffer_consume_record(const struct lib_ring_buffer_config
*config
,
234 struct lib_ring_buffer_backend
*bufb
)
236 unsigned long sb_bindex
;
238 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
239 CHAN_WARN_ON(bufb
->chan
,
240 !v_read(config
, &bufb
->array
[sb_bindex
]->records_unread
));
241 /* Non-atomic decrement protected by exclusive subbuffer access */
242 _v_dec(config
, &bufb
->array
[sb_bindex
]->records_unread
);
243 v_inc(config
, &bufb
->records_read
);
247 unsigned long subbuffer_get_records_count(
248 const struct lib_ring_buffer_config
*config
,
249 struct lib_ring_buffer_backend
*bufb
,
252 unsigned long sb_bindex
;
254 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
255 return v_read(config
, &bufb
->array
[sb_bindex
]->records_commit
);
259 * Must be executed at subbuffer delivery when the writer has _exclusive_
260 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
261 * lib_ring_buffer_get_records_count() must be called to get the records
262 * count before this function, because it resets the records_commit
266 unsigned long subbuffer_count_records_overrun(
267 const struct lib_ring_buffer_config
*config
,
268 struct lib_ring_buffer_backend
*bufb
,
271 struct lib_ring_buffer_backend_pages
*pages
;
272 unsigned long overruns
, sb_bindex
;
274 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
275 pages
= bufb
->array
[sb_bindex
];
276 overruns
= v_read(config
, &pages
->records_unread
);
277 v_set(config
, &pages
->records_unread
,
278 v_read(config
, &pages
->records_commit
));
279 v_set(config
, &pages
->records_commit
, 0);
285 void subbuffer_set_data_size(const struct lib_ring_buffer_config
*config
,
286 struct lib_ring_buffer_backend
*bufb
,
288 unsigned long data_size
)
290 struct lib_ring_buffer_backend_pages
*pages
;
291 unsigned long sb_bindex
;
293 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
294 pages
= bufb
->array
[sb_bindex
];
295 pages
->data_size
= data_size
;
299 unsigned long subbuffer_get_read_data_size(
300 const struct lib_ring_buffer_config
*config
,
301 struct lib_ring_buffer_backend
*bufb
)
303 struct lib_ring_buffer_backend_pages
*pages
;
304 unsigned long sb_bindex
;
306 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
307 pages
= bufb
->array
[sb_bindex
];
308 return pages
->data_size
;
312 unsigned long subbuffer_get_data_size(
313 const struct lib_ring_buffer_config
*config
,
314 struct lib_ring_buffer_backend
*bufb
,
317 struct lib_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 return pages
->data_size
;
326 void subbuffer_inc_packet_count(const struct lib_ring_buffer_config
*config
,
327 struct lib_ring_buffer_backend
*bufb
,
330 bufb
->buf_cnt
[idx
].seq_cnt
++;
334 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
338 void lib_ring_buffer_clear_noref(const struct lib_ring_buffer_config
*config
,
339 struct lib_ring_buffer_backend
*bufb
,
342 unsigned long id
, new_id
;
344 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
348 * Performing a volatile access to read the sb_pages, because we want to
349 * read a coherent version of the pointer and the associated noref flag.
351 id
= ACCESS_ONCE(bufb
->buf_wsb
[idx
].id
);
353 /* This check is called on the fast path for each record. */
354 if (likely(!subbuffer_id_is_noref(config
, id
))) {
356 * Store after load dependency ordering the writes to
357 * the subbuffer after load and test of the noref flag
358 * matches the memory barrier implied by the cmpxchg()
359 * in update_read_sb_index().
361 return; /* Already writing to this buffer */
364 subbuffer_id_clear_noref(config
, &new_id
);
365 new_id
= cmpxchg(&bufb
->buf_wsb
[idx
].id
, id
, new_id
);
366 if (likely(new_id
== id
))
373 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
377 void lib_ring_buffer_set_noref_offset(const struct lib_ring_buffer_config
*config
,
378 struct lib_ring_buffer_backend
*bufb
,
379 unsigned long idx
, unsigned long offset
)
381 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
385 * Because ring_buffer_set_noref() is only called by a single thread
386 * (the one which updated the cc_sb value), there are no concurrent
387 * updates to take care of: other writers have not updated cc_sb, so
388 * they cannot set the noref flag, and concurrent readers cannot modify
389 * the pointer because the noref flag is not set yet.
390 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
391 * to the subbuffer before this set noref operation.
392 * subbuffer_set_noref() uses a volatile store to deal with concurrent
393 * readers of the noref flag.
395 CHAN_WARN_ON(bufb
->chan
,
396 subbuffer_id_is_noref(config
, bufb
->buf_wsb
[idx
].id
));
398 * Memory barrier that ensures counter stores are ordered before set
402 subbuffer_id_set_noref_offset(config
, &bufb
->buf_wsb
[idx
].id
, offset
);
406 * update_read_sb_index - Read-side subbuffer index update.
409 int update_read_sb_index(const struct lib_ring_buffer_config
*config
,
410 struct lib_ring_buffer_backend
*bufb
,
411 struct channel_backend
*chanb
,
412 unsigned long consumed_idx
,
413 unsigned long consumed_count
)
415 unsigned long old_id
, new_id
;
417 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
419 * Exchange the target writer subbuffer with our own unused
420 * subbuffer. No need to use ACCESS_ONCE() here to read the
421 * old_wpage, because the value read will be confirmed by the
422 * following cmpxchg().
424 old_id
= bufb
->buf_wsb
[consumed_idx
].id
;
425 if (unlikely(!subbuffer_id_is_noref(config
, old_id
)))
428 * Make sure the offset count we are expecting matches the one
429 * indicated by the writer.
431 if (unlikely(!subbuffer_id_compare_offset(config
, old_id
,
434 CHAN_WARN_ON(bufb
->chan
,
435 !subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
436 subbuffer_id_set_noref_offset(config
, &bufb
->buf_rsb
.id
,
438 new_id
= cmpxchg(&bufb
->buf_wsb
[consumed_idx
].id
, old_id
,
440 if (unlikely(old_id
!= new_id
))
442 bufb
->buf_rsb
.id
= new_id
;
444 /* No page exchange, use the writer page directly */
445 bufb
->buf_rsb
.id
= bufb
->buf_wsb
[consumed_idx
].id
;
451 * Use the architecture-specific memcpy implementation for constant-sized
452 * inputs, but rely on an inline memcpy for length statically unknown.
453 * The function call to memcpy is just way too expensive for a fast path.
455 #define lib_ring_buffer_do_copy(config, dest, src, len) \
457 size_t __len = (len); \
458 if (__builtin_constant_p(len)) \
459 memcpy(dest, src, __len); \
461 inline_memcpy(dest, src, __len); \
465 * We use __copy_from_user_inatomic to copy userspace data since we already
466 * did the access_ok for the whole range.
468 * Return 0 if OK, nonzero on error.
471 unsigned long lib_ring_buffer_do_copy_from_user_inatomic(void *dest
,
472 const void __user
*src
,
475 return __copy_from_user_inatomic(dest
, src
, len
);
479 * write len bytes to dest with c
482 void lib_ring_buffer_do_memset(char *dest
, int c
,
487 for (i
= 0; i
< len
; i
++)
491 #endif /* _LIB_RING_BUFFER_BACKEND_INTERNAL_H */