1 #ifndef _LINUX_RING_BUFFER_BACKEND_INTERNAL_H
2 #define _LINUX_RING_BUFFER_BACKEND_INTERNAL_H
5 * linux/ringbuffer/backend_internal.h
7 * Copyright (C) 2008-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Ring buffer backend (internal helpers).
11 * Dual LGPL v2.1/GPL v2 license.
15 #include <urcu/compiler.h>
18 #include "backend_types.h"
19 #include "frontend_types.h"
21 /* Ring buffer backend API presented to the frontend */
23 /* Ring buffer and channel backend create/free */
25 int lib_ring_buffer_backend_create(struct lib_ring_buffer_backend
*bufb
,
26 struct channel_backend
*chan
, int cpu
);
27 void channel_backend_unregister_notifiers(struct channel_backend
*chanb
);
28 void lib_ring_buffer_backend_free(struct lib_ring_buffer_backend
*bufb
);
29 int channel_backend_init(struct channel_backend
*chanb
,
31 const struct lib_ring_buffer_config
*config
,
32 void *priv
, size_t subbuf_size
,
34 void channel_backend_free(struct channel_backend
*chanb
);
36 void lib_ring_buffer_backend_reset(struct lib_ring_buffer_backend
*bufb
);
37 void channel_backend_reset(struct channel_backend
*chanb
);
39 int lib_ring_buffer_backend_init(void);
40 void lib_ring_buffer_backend_exit(void);
42 extern void _lib_ring_buffer_write(struct lib_ring_buffer_backend
*bufb
,
43 size_t offset
, const void *src
, size_t len
,
47 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
48 * exchanged atomically.
50 * Top half word, except lowest bit, belongs to "offset", which is used to keep
51 * to count the produced buffers. For overwrite mode, this provides the
52 * consumer with the capacity to read subbuffers in order, handling the
53 * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit
54 * systems) concurrently with a single execution of get_subbuf (between offset
55 * sampling and subbuffer ID exchange).
58 #define HALF_ULONG_BITS (CAA_BITS_PER_LONG >> 1)
60 #define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1)
61 #define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT)
62 #define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1))
64 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
66 #define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1)
67 #define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT)
68 #define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT
70 * In overwrite mode: lowest half of word is used for index.
71 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
72 * In producer-consumer mode: whole word used for index.
74 #define SB_ID_INDEX_SHIFT 0
75 #define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT)
76 #define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1)
79 * Construct the subbuffer id from offset, index and noref. Use only the index
80 * for producer-consumer mode (offset and noref are only used in overwrite
84 unsigned long subbuffer_id(const struct lib_ring_buffer_config
*config
,
85 unsigned long offset
, unsigned long noref
,
88 if (config
->mode
== RING_BUFFER_OVERWRITE
)
89 return (offset
<< SB_ID_OFFSET_SHIFT
)
90 | (noref
<< SB_ID_NOREF_SHIFT
)
97 * Compare offset with the offset contained within id. Return 1 if the offset
98 * bits are identical, else 0.
101 int subbuffer_id_compare_offset(const struct lib_ring_buffer_config
*config
,
102 unsigned long id
, unsigned long offset
)
104 return (id
& SB_ID_OFFSET_MASK
) == (offset
<< SB_ID_OFFSET_SHIFT
);
108 unsigned long subbuffer_id_get_index(const struct lib_ring_buffer_config
*config
,
111 if (config
->mode
== RING_BUFFER_OVERWRITE
)
112 return id
& SB_ID_INDEX_MASK
;
118 unsigned long subbuffer_id_is_noref(const struct lib_ring_buffer_config
*config
,
121 if (config
->mode
== RING_BUFFER_OVERWRITE
)
122 return !!(id
& SB_ID_NOREF_MASK
);
128 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
132 void subbuffer_id_set_noref(const struct lib_ring_buffer_config
*config
,
135 if (config
->mode
== RING_BUFFER_OVERWRITE
)
136 *id
|= SB_ID_NOREF_MASK
;
140 void subbuffer_id_set_noref_offset(const struct lib_ring_buffer_config
*config
,
141 unsigned long *id
, unsigned long offset
)
145 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
147 tmp
&= ~SB_ID_OFFSET_MASK
;
148 tmp
|= offset
<< SB_ID_OFFSET_SHIFT
;
149 tmp
|= SB_ID_NOREF_MASK
;
150 /* Volatile store, read concurrently by readers. */
151 CMM_ACCESS_ONCE(*id
) = tmp
;
155 /* No volatile access, since already used locally */
157 void subbuffer_id_clear_noref(const struct lib_ring_buffer_config
*config
,
160 if (config
->mode
== RING_BUFFER_OVERWRITE
)
161 *id
&= ~SB_ID_NOREF_MASK
;
165 * For overwrite mode, cap the number of subbuffers per buffer to:
166 * 2^16 on 32-bit architectures
167 * 2^32 on 64-bit architectures
168 * This is required to fit in the index part of the ID. Return 0 on success,
172 int subbuffer_id_check_index(const struct lib_ring_buffer_config
*config
,
173 unsigned long num_subbuf
)
175 if (config
->mode
== RING_BUFFER_OVERWRITE
)
176 return (num_subbuf
> (1UL << HALF_ULONG_BITS
)) ? -EPERM
: 0;
182 void subbuffer_count_record(const struct lib_ring_buffer_config
*config
,
183 struct lib_ring_buffer_backend
*bufb
,
186 unsigned long sb_bindex
;
188 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
189 v_inc(config
, &bufb
->array
[sb_bindex
]->records_commit
);
193 * Reader has exclusive subbuffer access for record consumption. No need to
194 * perform the decrement atomically.
197 void subbuffer_consume_record(const struct lib_ring_buffer_config
*config
,
198 struct lib_ring_buffer_backend
*bufb
)
200 unsigned long sb_bindex
;
202 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
203 CHAN_WARN_ON(bufb
->chan
,
204 !v_read(config
, &bufb
->array
[sb_bindex
]->records_unread
));
205 /* Non-atomic decrement protected by exclusive subbuffer access */
206 _v_dec(config
, &bufb
->array
[sb_bindex
]->records_unread
);
207 v_inc(config
, &bufb
->records_read
);
211 unsigned long subbuffer_get_records_count(
212 const struct lib_ring_buffer_config
*config
,
213 struct lib_ring_buffer_backend
*bufb
,
216 unsigned long sb_bindex
;
218 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
219 return v_read(config
, &bufb
->array
[sb_bindex
]->records_commit
);
223 * Must be executed at subbuffer delivery when the writer has _exclusive_
224 * subbuffer access. See ring_buffer_check_deliver() for details.
225 * ring_buffer_get_records_count() must be called to get the records count
226 * before this function, because it resets the records_commit count.
229 unsigned long subbuffer_count_records_overrun(
230 const struct lib_ring_buffer_config
*config
,
231 struct lib_ring_buffer_backend
*bufb
,
234 struct lib_ring_buffer_backend_pages
*pages
;
235 unsigned long overruns
, sb_bindex
;
237 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
238 pages
= bufb
->array
[sb_bindex
];
239 overruns
= v_read(config
, &pages
->records_unread
);
240 v_set(config
, &pages
->records_unread
,
241 v_read(config
, &pages
->records_commit
));
242 v_set(config
, &pages
->records_commit
, 0);
248 void subbuffer_set_data_size(const struct lib_ring_buffer_config
*config
,
249 struct lib_ring_buffer_backend
*bufb
,
251 unsigned long data_size
)
253 struct lib_ring_buffer_backend_pages
*pages
;
254 unsigned long sb_bindex
;
256 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
257 pages
= bufb
->array
[sb_bindex
];
258 pages
->data_size
= data_size
;
262 unsigned long subbuffer_get_read_data_size(
263 const struct lib_ring_buffer_config
*config
,
264 struct lib_ring_buffer_backend
*bufb
)
266 struct lib_ring_buffer_backend_pages
*pages
;
267 unsigned long sb_bindex
;
269 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
270 pages
= bufb
->array
[sb_bindex
];
271 return pages
->data_size
;
275 unsigned long subbuffer_get_data_size(
276 const struct lib_ring_buffer_config
*config
,
277 struct lib_ring_buffer_backend
*bufb
,
280 struct lib_ring_buffer_backend_pages
*pages
;
281 unsigned long sb_bindex
;
283 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_wsb
[idx
].id
);
284 pages
= bufb
->array
[sb_bindex
];
285 return pages
->data_size
;
289 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
293 void lib_ring_buffer_clear_noref(const struct lib_ring_buffer_config
*config
,
294 struct lib_ring_buffer_backend
*bufb
,
297 unsigned long id
, new_id
;
299 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
303 * Performing a volatile access to read the sb_pages, because we want to
304 * read a coherent version of the pointer and the associated noref flag.
306 id
= CMM_ACCESS_ONCE(bufb
->buf_wsb
[idx
].id
);
308 /* This check is called on the fast path for each record. */
309 if (likely(!subbuffer_id_is_noref(config
, id
))) {
311 * Store after load dependency ordering the writes to
312 * the subbuffer after load and test of the noref flag
313 * matches the memory barrier implied by the cmpxchg()
314 * in update_read_sb_index().
316 return; /* Already writing to this buffer */
319 subbuffer_id_clear_noref(config
, &new_id
);
320 new_id
= uatomic_cmpxchg(&bufb
->buf_wsb
[idx
].id
, id
, new_id
);
321 if (likely(new_id
== id
))
328 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
332 void lib_ring_buffer_set_noref_offset(const struct lib_ring_buffer_config
*config
,
333 struct lib_ring_buffer_backend
*bufb
,
334 unsigned long idx
, unsigned long offset
)
336 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
340 * Because ring_buffer_set_noref() is only called by a single thread
341 * (the one which updated the cc_sb value), there are no concurrent
342 * updates to take care of: other writers have not updated cc_sb, so
343 * they cannot set the noref flag, and concurrent readers cannot modify
344 * the pointer because the noref flag is not set yet.
345 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
346 * to the subbuffer before this set noref operation.
347 * subbuffer_set_noref() uses a volatile store to deal with concurrent
348 * readers of the noref flag.
350 CHAN_WARN_ON(bufb
->chan
,
351 subbuffer_id_is_noref(config
, bufb
->buf_wsb
[idx
].id
));
353 * Memory barrier that ensures counter stores are ordered before set
357 subbuffer_id_set_noref_offset(config
, &bufb
->buf_wsb
[idx
].id
, offset
);
361 * update_read_sb_index - Read-side subbuffer index update.
364 int update_read_sb_index(const struct lib_ring_buffer_config
*config
,
365 struct lib_ring_buffer_backend
*bufb
,
366 struct channel_backend
*chanb
,
367 unsigned long consumed_idx
,
368 unsigned long consumed_count
)
370 unsigned long old_id
, new_id
;
372 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
374 * Exchange the target writer subbuffer with our own unused
375 * subbuffer. No need to use CMM_ACCESS_ONCE() here to read the
376 * old_wpage, because the value read will be confirmed by the
377 * following cmpxchg().
379 old_id
= bufb
->buf_wsb
[consumed_idx
].id
;
380 if (unlikely(!subbuffer_id_is_noref(config
, old_id
)))
383 * Make sure the offset count we are expecting matches the one
384 * indicated by the writer.
386 if (unlikely(!subbuffer_id_compare_offset(config
, old_id
,
389 CHAN_WARN_ON(bufb
->chan
,
390 !subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
391 subbuffer_id_set_noref_offset(config
, &bufb
->buf_rsb
.id
,
393 new_id
= uatomic_cmpxchg(&bufb
->buf_wsb
[consumed_idx
].id
, old_id
,
395 if (unlikely(old_id
!= new_id
))
397 bufb
->buf_rsb
.id
= new_id
;
399 /* No page exchange, use the writer page directly */
400 bufb
->buf_rsb
.id
= bufb
->buf_wsb
[consumed_idx
].id
;
406 * Use the architecture-specific memcpy implementation for constant-sized
407 * inputs, but rely on an inline memcpy for length statically unknown.
408 * The function call to memcpy is just way too expensive for a fast path.
410 #define lib_ring_buffer_do_copy(config, dest, src, len) \
412 size_t __len = (len); \
413 if (__builtin_constant_p(len)) \
414 memcpy(dest, src, __len); \
416 inline_memcpy(dest, src, __len); \
419 #endif /* _LINUX_RING_BUFFER_BACKEND_INTERNAL_H */