2e59b68b482a6cc3520d87c09278663aaa237553
[lttng-modules.git] / lib / ringbuffer / backend_internal.h
1 #ifndef _LIB_RING_BUFFER_BACKEND_INTERNAL_H
2 #define _LIB_RING_BUFFER_BACKEND_INTERNAL_H
3
4 /*
5 * lib/ringbuffer/backend_internal.h
6 *
7 * Ring buffer backend (internal helpers).
8 *
9 * Copyright (C) 2008-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
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.
15 *
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.
20 *
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
24 */
25
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>
31
32 /* Ring buffer backend API presented to the frontend */
33
34 /* Ring buffer and channel backend create/free */
35
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,
41 const char *name,
42 const struct lib_ring_buffer_config *config,
43 void *priv, size_t subbuf_size,
44 size_t num_subbuf);
45 void channel_backend_free(struct channel_backend *chanb);
46
47 void lib_ring_buffer_backend_reset(struct lib_ring_buffer_backend *bufb);
48 void channel_backend_reset(struct channel_backend *chanb);
49
50 int lib_ring_buffer_backend_init(void);
51 void lib_ring_buffer_backend_exit(void);
52
53 extern void _lib_ring_buffer_write(struct lib_ring_buffer_backend *bufb,
54 size_t offset, const void *src, size_t len,
55 size_t pagecpy);
56 extern void _lib_ring_buffer_memset(struct lib_ring_buffer_backend *bufb,
57 size_t offset, int c, size_t len,
58 size_t pagecpy);
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);
68
69 /*
70 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
71 * exchanged atomically.
72 *
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).
79 */
80
81 #define HALF_ULONG_BITS (BITS_PER_LONG >> 1)
82
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))
86 /*
87 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
88 */
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
92 /*
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.
96 */
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)
100
101 /*
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
104 * mode).
105 */
106 static inline
107 unsigned long subbuffer_id(const struct lib_ring_buffer_config *config,
108 unsigned long offset, unsigned long noref,
109 unsigned long index)
110 {
111 if (config->mode == RING_BUFFER_OVERWRITE)
112 return (offset << SB_ID_OFFSET_SHIFT)
113 | (noref << SB_ID_NOREF_SHIFT)
114 | index;
115 else
116 return index;
117 }
118
119 /*
120 * Compare offset with the offset contained within id. Return 1 if the offset
121 * bits are identical, else 0.
122 */
123 static inline
124 int subbuffer_id_compare_offset(const struct lib_ring_buffer_config *config,
125 unsigned long id, unsigned long offset)
126 {
127 return (id & SB_ID_OFFSET_MASK) == (offset << SB_ID_OFFSET_SHIFT);
128 }
129
130 static inline
131 unsigned long subbuffer_id_get_index(const struct lib_ring_buffer_config *config,
132 unsigned long id)
133 {
134 if (config->mode == RING_BUFFER_OVERWRITE)
135 return id & SB_ID_INDEX_MASK;
136 else
137 return id;
138 }
139
140 static inline
141 unsigned long subbuffer_id_is_noref(const struct lib_ring_buffer_config *config,
142 unsigned long id)
143 {
144 if (config->mode == RING_BUFFER_OVERWRITE)
145 return !!(id & SB_ID_NOREF_MASK);
146 else
147 return 1;
148 }
149
150 /*
151 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
152 * needed.
153 */
154 static inline
155 void subbuffer_id_set_noref(const struct lib_ring_buffer_config *config,
156 unsigned long *id)
157 {
158 if (config->mode == RING_BUFFER_OVERWRITE)
159 *id |= SB_ID_NOREF_MASK;
160 }
161
162 static inline
163 void subbuffer_id_set_noref_offset(const struct lib_ring_buffer_config *config,
164 unsigned long *id, unsigned long offset)
165 {
166 unsigned long tmp;
167
168 if (config->mode == RING_BUFFER_OVERWRITE) {
169 tmp = *id;
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;
175 }
176 }
177
178 /* No volatile access, since already used locally */
179 static inline
180 void subbuffer_id_clear_noref(const struct lib_ring_buffer_config *config,
181 unsigned long *id)
182 {
183 if (config->mode == RING_BUFFER_OVERWRITE)
184 *id &= ~SB_ID_NOREF_MASK;
185 }
186
187 /*
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,
192 * -EPERM on failure.
193 */
194 static inline
195 int subbuffer_id_check_index(const struct lib_ring_buffer_config *config,
196 unsigned long num_subbuf)
197 {
198 if (config->mode == RING_BUFFER_OVERWRITE)
199 return (num_subbuf > (1UL << HALF_ULONG_BITS)) ? -EPERM : 0;
200 else
201 return 0;
202 }
203
204 static inline
205 void lib_ring_buffer_backend_get_pages(const struct lib_ring_buffer_config *config,
206 struct lib_ring_buffer_ctx *ctx,
207 struct lib_ring_buffer_backend_pages **backend_pages)
208 {
209 struct lib_ring_buffer_backend *bufb = &ctx->buf->backend;
210 struct channel_backend *chanb = &ctx->chan->backend;
211 size_t sbidx, offset = ctx->buf_offset;
212 unsigned long sb_bindex, id;
213 struct lib_ring_buffer_backend_pages *rpages;
214
215 offset &= chanb->buf_size - 1;
216 sbidx = offset >> chanb->subbuf_size_order;
217 id = bufb->buf_wsb[sbidx].id;
218 sb_bindex = subbuffer_id_get_index(config, id);
219 rpages = bufb->array[sb_bindex];
220 CHAN_WARN_ON(ctx->chan,
221 config->mode == RING_BUFFER_OVERWRITE
222 && subbuffer_id_is_noref(config, id));
223 *backend_pages = rpages;
224 }
225
226 /* Get backend pages from cache. */
227 static inline
228 struct lib_ring_buffer_backend_pages *
229 lib_ring_buffer_get_backend_pages_from_ctx(const struct lib_ring_buffer_config *config,
230 struct lib_ring_buffer_ctx *ctx)
231 {
232 return ctx->backend_pages;
233 }
234
235 /*
236 * The ring buffer can count events recorded and overwritten per buffer,
237 * but it is disabled by default due to its performance overhead.
238 */
239 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
240 static inline
241 void subbuffer_count_record(const struct lib_ring_buffer_config *config,
242 struct lib_ring_buffer_backend *bufb,
243 unsigned long idx)
244 {
245 unsigned long sb_bindex;
246
247 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
248 v_inc(config, &bufb->array[sb_bindex]->records_commit);
249 }
250 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
251 static inline
252 void subbuffer_count_record(const struct lib_ring_buffer_config *config,
253 struct lib_ring_buffer_backend *bufb,
254 unsigned long idx)
255 {
256 }
257 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
258
259 /*
260 * Reader has exclusive subbuffer access for record consumption. No need to
261 * perform the decrement atomically.
262 */
263 static inline
264 void subbuffer_consume_record(const struct lib_ring_buffer_config *config,
265 struct lib_ring_buffer_backend *bufb)
266 {
267 unsigned long sb_bindex;
268
269 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
270 CHAN_WARN_ON(bufb->chan,
271 !v_read(config, &bufb->array[sb_bindex]->records_unread));
272 /* Non-atomic decrement protected by exclusive subbuffer access */
273 _v_dec(config, &bufb->array[sb_bindex]->records_unread);
274 v_inc(config, &bufb->records_read);
275 }
276
277 static inline
278 unsigned long subbuffer_get_records_count(
279 const struct lib_ring_buffer_config *config,
280 struct lib_ring_buffer_backend *bufb,
281 unsigned long idx)
282 {
283 unsigned long sb_bindex;
284
285 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
286 return v_read(config, &bufb->array[sb_bindex]->records_commit);
287 }
288
289 /*
290 * Must be executed at subbuffer delivery when the writer has _exclusive_
291 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
292 * lib_ring_buffer_get_records_count() must be called to get the records
293 * count before this function, because it resets the records_commit
294 * count.
295 */
296 static inline
297 unsigned long subbuffer_count_records_overrun(
298 const struct lib_ring_buffer_config *config,
299 struct lib_ring_buffer_backend *bufb,
300 unsigned long idx)
301 {
302 struct lib_ring_buffer_backend_pages *pages;
303 unsigned long overruns, sb_bindex;
304
305 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
306 pages = bufb->array[sb_bindex];
307 overruns = v_read(config, &pages->records_unread);
308 v_set(config, &pages->records_unread,
309 v_read(config, &pages->records_commit));
310 v_set(config, &pages->records_commit, 0);
311
312 return overruns;
313 }
314
315 static inline
316 void subbuffer_set_data_size(const struct lib_ring_buffer_config *config,
317 struct lib_ring_buffer_backend *bufb,
318 unsigned long idx,
319 unsigned long data_size)
320 {
321 struct lib_ring_buffer_backend_pages *pages;
322 unsigned long sb_bindex;
323
324 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
325 pages = bufb->array[sb_bindex];
326 pages->data_size = data_size;
327 }
328
329 static inline
330 unsigned long subbuffer_get_read_data_size(
331 const struct lib_ring_buffer_config *config,
332 struct lib_ring_buffer_backend *bufb)
333 {
334 struct lib_ring_buffer_backend_pages *pages;
335 unsigned long sb_bindex;
336
337 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
338 pages = bufb->array[sb_bindex];
339 return pages->data_size;
340 }
341
342 static inline
343 unsigned long subbuffer_get_data_size(
344 const struct lib_ring_buffer_config *config,
345 struct lib_ring_buffer_backend *bufb,
346 unsigned long idx)
347 {
348 struct lib_ring_buffer_backend_pages *pages;
349 unsigned long sb_bindex;
350
351 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
352 pages = bufb->array[sb_bindex];
353 return pages->data_size;
354 }
355
356 static inline
357 void subbuffer_inc_packet_count(const struct lib_ring_buffer_config *config,
358 struct lib_ring_buffer_backend *bufb,
359 unsigned long idx)
360 {
361 bufb->buf_cnt[idx].seq_cnt++;
362 }
363
364 /**
365 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
366 * writer.
367 */
368 static inline
369 void lib_ring_buffer_clear_noref(const struct lib_ring_buffer_config *config,
370 struct lib_ring_buffer_backend *bufb,
371 unsigned long idx)
372 {
373 unsigned long id, new_id;
374
375 if (config->mode != RING_BUFFER_OVERWRITE)
376 return;
377
378 /*
379 * Performing a volatile access to read the sb_pages, because we want to
380 * read a coherent version of the pointer and the associated noref flag.
381 */
382 id = ACCESS_ONCE(bufb->buf_wsb[idx].id);
383 for (;;) {
384 /* This check is called on the fast path for each record. */
385 if (likely(!subbuffer_id_is_noref(config, id))) {
386 /*
387 * Store after load dependency ordering the writes to
388 * the subbuffer after load and test of the noref flag
389 * matches the memory barrier implied by the cmpxchg()
390 * in update_read_sb_index().
391 */
392 return; /* Already writing to this buffer */
393 }
394 new_id = id;
395 subbuffer_id_clear_noref(config, &new_id);
396 new_id = cmpxchg(&bufb->buf_wsb[idx].id, id, new_id);
397 if (likely(new_id == id))
398 break;
399 id = new_id;
400 }
401 }
402
403 /**
404 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
405 * called by writer.
406 */
407 static inline
408 void lib_ring_buffer_set_noref_offset(const struct lib_ring_buffer_config *config,
409 struct lib_ring_buffer_backend *bufb,
410 unsigned long idx, unsigned long offset)
411 {
412 if (config->mode != RING_BUFFER_OVERWRITE)
413 return;
414
415 /*
416 * Because ring_buffer_set_noref() is only called by a single thread
417 * (the one which updated the cc_sb value), there are no concurrent
418 * updates to take care of: other writers have not updated cc_sb, so
419 * they cannot set the noref flag, and concurrent readers cannot modify
420 * the pointer because the noref flag is not set yet.
421 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
422 * to the subbuffer before this set noref operation.
423 * subbuffer_set_noref() uses a volatile store to deal with concurrent
424 * readers of the noref flag.
425 */
426 CHAN_WARN_ON(bufb->chan,
427 subbuffer_id_is_noref(config, bufb->buf_wsb[idx].id));
428 /*
429 * Memory barrier that ensures counter stores are ordered before set
430 * noref and offset.
431 */
432 smp_mb();
433 subbuffer_id_set_noref_offset(config, &bufb->buf_wsb[idx].id, offset);
434 }
435
436 /**
437 * update_read_sb_index - Read-side subbuffer index update.
438 */
439 static inline
440 int update_read_sb_index(const struct lib_ring_buffer_config *config,
441 struct lib_ring_buffer_backend *bufb,
442 struct channel_backend *chanb,
443 unsigned long consumed_idx,
444 unsigned long consumed_count)
445 {
446 unsigned long old_id, new_id;
447
448 if (config->mode == RING_BUFFER_OVERWRITE) {
449 /*
450 * Exchange the target writer subbuffer with our own unused
451 * subbuffer. No need to use ACCESS_ONCE() here to read the
452 * old_wpage, because the value read will be confirmed by the
453 * following cmpxchg().
454 */
455 old_id = bufb->buf_wsb[consumed_idx].id;
456 if (unlikely(!subbuffer_id_is_noref(config, old_id)))
457 return -EAGAIN;
458 /*
459 * Make sure the offset count we are expecting matches the one
460 * indicated by the writer.
461 */
462 if (unlikely(!subbuffer_id_compare_offset(config, old_id,
463 consumed_count)))
464 return -EAGAIN;
465 CHAN_WARN_ON(bufb->chan,
466 !subbuffer_id_is_noref(config, bufb->buf_rsb.id));
467 subbuffer_id_set_noref_offset(config, &bufb->buf_rsb.id,
468 consumed_count);
469 new_id = cmpxchg(&bufb->buf_wsb[consumed_idx].id, old_id,
470 bufb->buf_rsb.id);
471 if (unlikely(old_id != new_id))
472 return -EAGAIN;
473 bufb->buf_rsb.id = new_id;
474 } else {
475 /* No page exchange, use the writer page directly */
476 bufb->buf_rsb.id = bufb->buf_wsb[consumed_idx].id;
477 }
478 return 0;
479 }
480
481 static inline __attribute__((always_inline))
482 void lttng_inline_memcpy(void *dest, const void *src,
483 unsigned long len)
484 {
485 switch (len) {
486 case 1:
487 *(uint8_t *) dest = *(const uint8_t *) src;
488 break;
489 case 2:
490 *(uint16_t *) dest = *(const uint16_t *) src;
491 break;
492 case 4:
493 *(uint32_t *) dest = *(const uint32_t *) src;
494 break;
495 case 8:
496 *(uint64_t *) dest = *(const uint64_t *) src;
497 break;
498 default:
499 inline_memcpy(dest, src, len);
500 }
501 }
502
503 /*
504 * Use the architecture-specific memcpy implementation for constant-sized
505 * inputs, but rely on an inline memcpy for length statically unknown.
506 * The function call to memcpy is just way too expensive for a fast path.
507 */
508 #define lib_ring_buffer_do_copy(config, dest, src, len) \
509 do { \
510 size_t __len = (len); \
511 if (__builtin_constant_p(len)) \
512 memcpy(dest, src, __len); \
513 else \
514 lttng_inline_memcpy(dest, src, __len); \
515 } while (0)
516
517 /*
518 * We use __copy_from_user_inatomic to copy userspace data since we already
519 * did the access_ok for the whole range.
520 *
521 * Return 0 if OK, nonzero on error.
522 */
523 static inline
524 unsigned long lib_ring_buffer_do_copy_from_user_inatomic(void *dest,
525 const void __user *src,
526 unsigned long len)
527 {
528 return __copy_from_user_inatomic(dest, src, len);
529 }
530
531 /*
532 * write len bytes to dest with c
533 */
534 static inline
535 void lib_ring_buffer_do_memset(char *dest, int c,
536 unsigned long len)
537 {
538 unsigned long i;
539
540 for (i = 0; i < len; i++)
541 dest[i] = c;
542 }
543
544 #endif /* _LIB_RING_BUFFER_BACKEND_INTERNAL_H */
This page took 0.039238 seconds and 3 git commands to generate.