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