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