Commit | Line | Data |
---|---|---|
852c2936 MD |
1 | #ifndef _LINUX_RING_BUFFER_BACKEND_INTERNAL_H |
2 | #define _LINUX_RING_BUFFER_BACKEND_INTERNAL_H | |
3 | ||
4 | /* | |
5 | * linux/ringbuffer/backend_internal.h | |
6 | * | |
7 | * Copyright (C) 2008-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
8 | * | |
9 | * Ring buffer backend (internal helpers). | |
10 | * | |
11 | * Dual LGPL v2.1/GPL v2 license. | |
12 | */ | |
13 | ||
14641deb MD |
14 | #include <unistd.h> |
15 | #include <urcu/compiler.h> | |
16 | ||
4931a13e MD |
17 | #include "config.h" |
18 | #include "backend_types.h" | |
19 | #include "frontend_types.h" | |
a6352fd4 | 20 | #include "shm.h" |
852c2936 MD |
21 | |
22 | /* Ring buffer backend API presented to the frontend */ | |
23 | ||
24 | /* Ring buffer and channel backend create/free */ | |
25 | ||
26 | int lib_ring_buffer_backend_create(struct lib_ring_buffer_backend *bufb, | |
a6352fd4 MD |
27 | struct channel_backend *chan, int cpu, |
28 | struct shm_header *shm_header); | |
852c2936 MD |
29 | void channel_backend_unregister_notifiers(struct channel_backend *chanb); |
30 | void lib_ring_buffer_backend_free(struct lib_ring_buffer_backend *bufb); | |
31 | int channel_backend_init(struct channel_backend *chanb, | |
32 | const char *name, | |
33 | const struct lib_ring_buffer_config *config, | |
34 | void *priv, size_t subbuf_size, | |
a6352fd4 | 35 | size_t num_subbuf, struct shm_header *shm_header); |
852c2936 MD |
36 | void channel_backend_free(struct channel_backend *chanb); |
37 | ||
38 | void lib_ring_buffer_backend_reset(struct lib_ring_buffer_backend *bufb); | |
39 | void channel_backend_reset(struct channel_backend *chanb); | |
40 | ||
41 | int lib_ring_buffer_backend_init(void); | |
42 | void lib_ring_buffer_backend_exit(void); | |
43 | ||
44 | extern void _lib_ring_buffer_write(struct lib_ring_buffer_backend *bufb, | |
45 | size_t offset, const void *src, size_t len, | |
46 | ssize_t pagecpy); | |
47 | ||
48 | /* | |
49 | * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be | |
50 | * exchanged atomically. | |
51 | * | |
52 | * Top half word, except lowest bit, belongs to "offset", which is used to keep | |
53 | * to count the produced buffers. For overwrite mode, this provides the | |
54 | * consumer with the capacity to read subbuffers in order, handling the | |
55 | * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit | |
56 | * systems) concurrently with a single execution of get_subbuf (between offset | |
57 | * sampling and subbuffer ID exchange). | |
58 | */ | |
59 | ||
14641deb | 60 | #define HALF_ULONG_BITS (CAA_BITS_PER_LONG >> 1) |
852c2936 MD |
61 | |
62 | #define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1) | |
63 | #define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT) | |
64 | #define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1)) | |
65 | /* | |
66 | * Lowest bit of top word half belongs to noref. Used only for overwrite mode. | |
67 | */ | |
68 | #define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1) | |
69 | #define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT) | |
70 | #define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT | |
71 | /* | |
72 | * In overwrite mode: lowest half of word is used for index. | |
73 | * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit. | |
74 | * In producer-consumer mode: whole word used for index. | |
75 | */ | |
76 | #define SB_ID_INDEX_SHIFT 0 | |
77 | #define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT) | |
78 | #define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1) | |
79 | ||
80 | /* | |
81 | * Construct the subbuffer id from offset, index and noref. Use only the index | |
82 | * for producer-consumer mode (offset and noref are only used in overwrite | |
83 | * mode). | |
84 | */ | |
85 | static inline | |
86 | unsigned long subbuffer_id(const struct lib_ring_buffer_config *config, | |
87 | unsigned long offset, unsigned long noref, | |
88 | unsigned long index) | |
89 | { | |
90 | if (config->mode == RING_BUFFER_OVERWRITE) | |
91 | return (offset << SB_ID_OFFSET_SHIFT) | |
92 | | (noref << SB_ID_NOREF_SHIFT) | |
93 | | index; | |
94 | else | |
95 | return index; | |
96 | } | |
97 | ||
98 | /* | |
99 | * Compare offset with the offset contained within id. Return 1 if the offset | |
100 | * bits are identical, else 0. | |
101 | */ | |
102 | static inline | |
103 | int subbuffer_id_compare_offset(const struct lib_ring_buffer_config *config, | |
104 | unsigned long id, unsigned long offset) | |
105 | { | |
106 | return (id & SB_ID_OFFSET_MASK) == (offset << SB_ID_OFFSET_SHIFT); | |
107 | } | |
108 | ||
109 | static inline | |
110 | unsigned long subbuffer_id_get_index(const struct lib_ring_buffer_config *config, | |
111 | unsigned long id) | |
112 | { | |
113 | if (config->mode == RING_BUFFER_OVERWRITE) | |
114 | return id & SB_ID_INDEX_MASK; | |
115 | else | |
116 | return id; | |
117 | } | |
118 | ||
119 | static inline | |
120 | unsigned long subbuffer_id_is_noref(const struct lib_ring_buffer_config *config, | |
121 | unsigned long id) | |
122 | { | |
123 | if (config->mode == RING_BUFFER_OVERWRITE) | |
124 | return !!(id & SB_ID_NOREF_MASK); | |
125 | else | |
126 | return 1; | |
127 | } | |
128 | ||
129 | /* | |
130 | * Only used by reader on subbuffer ID it has exclusive access to. No volatile | |
131 | * needed. | |
132 | */ | |
133 | static inline | |
134 | void subbuffer_id_set_noref(const struct lib_ring_buffer_config *config, | |
135 | unsigned long *id) | |
136 | { | |
137 | if (config->mode == RING_BUFFER_OVERWRITE) | |
138 | *id |= SB_ID_NOREF_MASK; | |
139 | } | |
140 | ||
141 | static inline | |
142 | void subbuffer_id_set_noref_offset(const struct lib_ring_buffer_config *config, | |
143 | unsigned long *id, unsigned long offset) | |
144 | { | |
145 | unsigned long tmp; | |
146 | ||
147 | if (config->mode == RING_BUFFER_OVERWRITE) { | |
148 | tmp = *id; | |
149 | tmp &= ~SB_ID_OFFSET_MASK; | |
150 | tmp |= offset << SB_ID_OFFSET_SHIFT; | |
151 | tmp |= SB_ID_NOREF_MASK; | |
152 | /* Volatile store, read concurrently by readers. */ | |
14641deb | 153 | CMM_ACCESS_ONCE(*id) = tmp; |
852c2936 MD |
154 | } |
155 | } | |
156 | ||
157 | /* No volatile access, since already used locally */ | |
158 | static inline | |
159 | void subbuffer_id_clear_noref(const struct lib_ring_buffer_config *config, | |
160 | unsigned long *id) | |
161 | { | |
162 | if (config->mode == RING_BUFFER_OVERWRITE) | |
163 | *id &= ~SB_ID_NOREF_MASK; | |
164 | } | |
165 | ||
166 | /* | |
167 | * For overwrite mode, cap the number of subbuffers per buffer to: | |
168 | * 2^16 on 32-bit architectures | |
169 | * 2^32 on 64-bit architectures | |
170 | * This is required to fit in the index part of the ID. Return 0 on success, | |
171 | * -EPERM on failure. | |
172 | */ | |
173 | static inline | |
174 | int subbuffer_id_check_index(const struct lib_ring_buffer_config *config, | |
175 | unsigned long num_subbuf) | |
176 | { | |
177 | if (config->mode == RING_BUFFER_OVERWRITE) | |
178 | return (num_subbuf > (1UL << HALF_ULONG_BITS)) ? -EPERM : 0; | |
179 | else | |
180 | return 0; | |
181 | } | |
182 | ||
183 | static inline | |
184 | void subbuffer_count_record(const struct lib_ring_buffer_config *config, | |
185 | struct lib_ring_buffer_backend *bufb, | |
186 | unsigned long idx) | |
187 | { | |
188 | unsigned long sb_bindex; | |
189 | ||
a6352fd4 MD |
190 | sb_bindex = subbuffer_id_get_index(config, shmp(bufb->buf_wsb)[idx].id); |
191 | v_inc(config, &shmp(bufb->array)[sb_bindex]->records_commit); | |
852c2936 MD |
192 | } |
193 | ||
194 | /* | |
195 | * Reader has exclusive subbuffer access for record consumption. No need to | |
196 | * perform the decrement atomically. | |
197 | */ | |
198 | static inline | |
199 | void subbuffer_consume_record(const struct lib_ring_buffer_config *config, | |
200 | struct lib_ring_buffer_backend *bufb) | |
201 | { | |
202 | unsigned long sb_bindex; | |
203 | ||
204 | sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); | |
205 | CHAN_WARN_ON(bufb->chan, | |
a6352fd4 | 206 | !v_read(config, &shmp(bufb->array)[sb_bindex]->records_unread)); |
852c2936 | 207 | /* Non-atomic decrement protected by exclusive subbuffer access */ |
a6352fd4 | 208 | _v_dec(config, &shmp(bufb->array)[sb_bindex]->records_unread); |
852c2936 MD |
209 | v_inc(config, &bufb->records_read); |
210 | } | |
211 | ||
212 | static inline | |
213 | unsigned long subbuffer_get_records_count( | |
214 | const struct lib_ring_buffer_config *config, | |
215 | struct lib_ring_buffer_backend *bufb, | |
216 | unsigned long idx) | |
217 | { | |
218 | unsigned long sb_bindex; | |
219 | ||
a6352fd4 MD |
220 | sb_bindex = subbuffer_id_get_index(config, shmp(bufb->buf_wsb)[idx].id); |
221 | return v_read(config, &shmp(bufb->array)[sb_bindex]->records_commit); | |
852c2936 MD |
222 | } |
223 | ||
224 | /* | |
225 | * Must be executed at subbuffer delivery when the writer has _exclusive_ | |
226 | * subbuffer access. See ring_buffer_check_deliver() for details. | |
227 | * ring_buffer_get_records_count() must be called to get the records count | |
228 | * before this function, because it resets the records_commit count. | |
229 | */ | |
230 | static inline | |
231 | unsigned long subbuffer_count_records_overrun( | |
232 | const struct lib_ring_buffer_config *config, | |
233 | struct lib_ring_buffer_backend *bufb, | |
234 | unsigned long idx) | |
235 | { | |
236 | struct lib_ring_buffer_backend_pages *pages; | |
237 | unsigned long overruns, sb_bindex; | |
238 | ||
a6352fd4 MD |
239 | sb_bindex = subbuffer_id_get_index(config, shmp(bufb->buf_wsb)[idx].id); |
240 | pages = shmp(bufb->array)[sb_bindex]; | |
852c2936 MD |
241 | overruns = v_read(config, &pages->records_unread); |
242 | v_set(config, &pages->records_unread, | |
243 | v_read(config, &pages->records_commit)); | |
244 | v_set(config, &pages->records_commit, 0); | |
245 | ||
246 | return overruns; | |
247 | } | |
248 | ||
249 | static inline | |
250 | void subbuffer_set_data_size(const struct lib_ring_buffer_config *config, | |
251 | struct lib_ring_buffer_backend *bufb, | |
252 | unsigned long idx, | |
253 | unsigned long data_size) | |
254 | { | |
255 | struct lib_ring_buffer_backend_pages *pages; | |
256 | unsigned long sb_bindex; | |
257 | ||
a6352fd4 MD |
258 | sb_bindex = subbuffer_id_get_index(config, shmp(bufb->buf_wsb)[idx].id); |
259 | pages = shmp(bufb->array)[sb_bindex]; | |
852c2936 MD |
260 | pages->data_size = data_size; |
261 | } | |
262 | ||
263 | static inline | |
264 | unsigned long subbuffer_get_read_data_size( | |
265 | const struct lib_ring_buffer_config *config, | |
266 | struct lib_ring_buffer_backend *bufb) | |
267 | { | |
268 | struct lib_ring_buffer_backend_pages *pages; | |
269 | unsigned long sb_bindex; | |
270 | ||
271 | sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); | |
a6352fd4 | 272 | pages = shmp(bufb->array)[sb_bindex]; |
852c2936 MD |
273 | return pages->data_size; |
274 | } | |
275 | ||
276 | static inline | |
277 | unsigned long subbuffer_get_data_size( | |
278 | const struct lib_ring_buffer_config *config, | |
279 | struct lib_ring_buffer_backend *bufb, | |
280 | unsigned long idx) | |
281 | { | |
282 | struct lib_ring_buffer_backend_pages *pages; | |
283 | unsigned long sb_bindex; | |
284 | ||
a6352fd4 MD |
285 | sb_bindex = subbuffer_id_get_index(config, shmp(bufb->buf_wsb)[idx].id); |
286 | pages = shmp(bufb->array)[sb_bindex]; | |
852c2936 MD |
287 | return pages->data_size; |
288 | } | |
289 | ||
290 | /** | |
291 | * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by | |
292 | * writer. | |
293 | */ | |
294 | static inline | |
295 | void lib_ring_buffer_clear_noref(const struct lib_ring_buffer_config *config, | |
296 | struct lib_ring_buffer_backend *bufb, | |
297 | unsigned long idx) | |
298 | { | |
299 | unsigned long id, new_id; | |
300 | ||
301 | if (config->mode != RING_BUFFER_OVERWRITE) | |
302 | return; | |
303 | ||
304 | /* | |
305 | * Performing a volatile access to read the sb_pages, because we want to | |
306 | * read a coherent version of the pointer and the associated noref flag. | |
307 | */ | |
a6352fd4 | 308 | id = CMM_ACCESS_ONCE(shmp(bufb->buf_wsb)[idx].id); |
852c2936 MD |
309 | for (;;) { |
310 | /* This check is called on the fast path for each record. */ | |
311 | if (likely(!subbuffer_id_is_noref(config, id))) { | |
312 | /* | |
313 | * Store after load dependency ordering the writes to | |
314 | * the subbuffer after load and test of the noref flag | |
315 | * matches the memory barrier implied by the cmpxchg() | |
316 | * in update_read_sb_index(). | |
317 | */ | |
318 | return; /* Already writing to this buffer */ | |
319 | } | |
320 | new_id = id; | |
321 | subbuffer_id_clear_noref(config, &new_id); | |
a6352fd4 | 322 | new_id = uatomic_cmpxchg(&shmp(bufb->buf_wsb)[idx].id, id, new_id); |
852c2936 MD |
323 | if (likely(new_id == id)) |
324 | break; | |
325 | id = new_id; | |
326 | } | |
327 | } | |
328 | ||
329 | /** | |
330 | * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset, | |
331 | * called by writer. | |
332 | */ | |
333 | static inline | |
334 | void lib_ring_buffer_set_noref_offset(const struct lib_ring_buffer_config *config, | |
335 | struct lib_ring_buffer_backend *bufb, | |
336 | unsigned long idx, unsigned long offset) | |
337 | { | |
338 | if (config->mode != RING_BUFFER_OVERWRITE) | |
339 | return; | |
340 | ||
341 | /* | |
342 | * Because ring_buffer_set_noref() is only called by a single thread | |
343 | * (the one which updated the cc_sb value), there are no concurrent | |
344 | * updates to take care of: other writers have not updated cc_sb, so | |
345 | * they cannot set the noref flag, and concurrent readers cannot modify | |
346 | * the pointer because the noref flag is not set yet. | |
347 | * The smp_wmb() in ring_buffer_commit() takes care of ordering writes | |
348 | * to the subbuffer before this set noref operation. | |
349 | * subbuffer_set_noref() uses a volatile store to deal with concurrent | |
350 | * readers of the noref flag. | |
351 | */ | |
352 | CHAN_WARN_ON(bufb->chan, | |
a6352fd4 | 353 | subbuffer_id_is_noref(config, shmp(bufb->buf_wsb)[idx].id)); |
852c2936 MD |
354 | /* |
355 | * Memory barrier that ensures counter stores are ordered before set | |
356 | * noref and offset. | |
357 | */ | |
14641deb | 358 | cmm_smp_mb(); |
a6352fd4 | 359 | subbuffer_id_set_noref_offset(config, &shmp(bufb->buf_wsb)[idx].id, offset); |
852c2936 MD |
360 | } |
361 | ||
362 | /** | |
363 | * update_read_sb_index - Read-side subbuffer index update. | |
364 | */ | |
365 | static inline | |
366 | int update_read_sb_index(const struct lib_ring_buffer_config *config, | |
367 | struct lib_ring_buffer_backend *bufb, | |
368 | struct channel_backend *chanb, | |
369 | unsigned long consumed_idx, | |
370 | unsigned long consumed_count) | |
371 | { | |
372 | unsigned long old_id, new_id; | |
373 | ||
374 | if (config->mode == RING_BUFFER_OVERWRITE) { | |
375 | /* | |
376 | * Exchange the target writer subbuffer with our own unused | |
14641deb | 377 | * subbuffer. No need to use CMM_ACCESS_ONCE() here to read the |
852c2936 MD |
378 | * old_wpage, because the value read will be confirmed by the |
379 | * following cmpxchg(). | |
380 | */ | |
a6352fd4 | 381 | old_id = shmp(bufb->buf_wsb)[consumed_idx].id; |
852c2936 MD |
382 | if (unlikely(!subbuffer_id_is_noref(config, old_id))) |
383 | return -EAGAIN; | |
384 | /* | |
385 | * Make sure the offset count we are expecting matches the one | |
386 | * indicated by the writer. | |
387 | */ | |
388 | if (unlikely(!subbuffer_id_compare_offset(config, old_id, | |
389 | consumed_count))) | |
390 | return -EAGAIN; | |
391 | CHAN_WARN_ON(bufb->chan, | |
392 | !subbuffer_id_is_noref(config, bufb->buf_rsb.id)); | |
393 | subbuffer_id_set_noref_offset(config, &bufb->buf_rsb.id, | |
394 | consumed_count); | |
a6352fd4 | 395 | new_id = uatomic_cmpxchg(&shmp(bufb->buf_wsb)[consumed_idx].id, old_id, |
852c2936 MD |
396 | bufb->buf_rsb.id); |
397 | if (unlikely(old_id != new_id)) | |
398 | return -EAGAIN; | |
399 | bufb->buf_rsb.id = new_id; | |
400 | } else { | |
401 | /* No page exchange, use the writer page directly */ | |
a6352fd4 | 402 | bufb->buf_rsb.id = shmp(bufb->buf_wsb)[consumed_idx].id; |
852c2936 MD |
403 | } |
404 | return 0; | |
405 | } | |
406 | ||
407 | /* | |
408 | * Use the architecture-specific memcpy implementation for constant-sized | |
409 | * inputs, but rely on an inline memcpy for length statically unknown. | |
410 | * The function call to memcpy is just way too expensive for a fast path. | |
411 | */ | |
412 | #define lib_ring_buffer_do_copy(config, dest, src, len) \ | |
413 | do { \ | |
414 | size_t __len = (len); \ | |
415 | if (__builtin_constant_p(len)) \ | |
416 | memcpy(dest, src, __len); \ | |
417 | else \ | |
418 | inline_memcpy(dest, src, __len); \ | |
419 | } while (0) | |
420 | ||
421 | #endif /* _LINUX_RING_BUFFER_BACKEND_INTERNAL_H */ |