Fix: allow racy tracepoint string input from kernel and userspace
[lttng-modules.git] / lib / ringbuffer / backend_internal.h
CommitLineData
886d51a3
MD
1#ifndef _LIB_RING_BUFFER_BACKEND_INTERNAL_H
2#define _LIB_RING_BUFFER_BACKEND_INTERNAL_H
f3bc08c5
MD
3
4/*
886d51a3 5 * lib/ringbuffer/backend_internal.h
f3bc08c5
MD
6 *
7 * Ring buffer backend (internal helpers).
8 *
886d51a3
MD
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
f3bc08c5
MD
24 */
25
26#include "../../wrapper/ringbuffer/config.h"
494a81f5 27#include "../../wrapper/ringbuffer/backend_types.h"
f3bc08c5
MD
28#include "../../wrapper/ringbuffer/frontend_types.h"
29#include <linux/string.h>
4ea00e4f 30#include <linux/uaccess.h>
f3bc08c5
MD
31
32/* Ring buffer backend API presented to the frontend */
33
34/* Ring buffer and channel backend create/free */
35
36int lib_ring_buffer_backend_create(struct lib_ring_buffer_backend *bufb,
37 struct channel_backend *chan, int cpu);
38void channel_backend_unregister_notifiers(struct channel_backend *chanb);
39void lib_ring_buffer_backend_free(struct lib_ring_buffer_backend *bufb);
40int 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);
45void channel_backend_free(struct channel_backend *chanb);
46
47void lib_ring_buffer_backend_reset(struct lib_ring_buffer_backend *bufb);
48void channel_backend_reset(struct channel_backend *chanb);
49
50int lib_ring_buffer_backend_init(void);
51void lib_ring_buffer_backend_exit(void);
52
53extern void _lib_ring_buffer_write(struct lib_ring_buffer_backend *bufb,
54 size_t offset, const void *src, size_t len,
55 ssize_t pagecpy);
4ea00e4f
JD
56extern void _lib_ring_buffer_memset(struct lib_ring_buffer_backend *bufb,
57 size_t offset, int c, size_t len,
58 ssize_t pagecpy);
16f78f3a
MD
59extern 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);
7b8ea3a5 62extern void _lib_ring_buffer_copy_from_user_inatomic(struct lib_ring_buffer_backend *bufb,
4ea00e4f
JD
63 size_t offset, const void *src,
64 size_t len, ssize_t pagecpy);
16f78f3a
MD
65extern 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);
f3bc08c5
MD
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 */
106static inline
107unsigned 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 */
123static inline
124int 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
130static inline
131unsigned 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
140static inline
141unsigned 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 */
154static inline
155void 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
162static inline
163void 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 */
179static inline
180void 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 */
194static inline
195int 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
204static inline
205void subbuffer_count_record(const struct lib_ring_buffer_config *config,
206 struct lib_ring_buffer_backend *bufb,
207 unsigned long idx)
208{
209 unsigned long sb_bindex;
210
211 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
212 v_inc(config, &bufb->array[sb_bindex]->records_commit);
213}
214
215/*
216 * Reader has exclusive subbuffer access for record consumption. No need to
217 * perform the decrement atomically.
218 */
219static inline
220void subbuffer_consume_record(const struct lib_ring_buffer_config *config,
221 struct lib_ring_buffer_backend *bufb)
222{
223 unsigned long sb_bindex;
224
225 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
226 CHAN_WARN_ON(bufb->chan,
227 !v_read(config, &bufb->array[sb_bindex]->records_unread));
228 /* Non-atomic decrement protected by exclusive subbuffer access */
229 _v_dec(config, &bufb->array[sb_bindex]->records_unread);
230 v_inc(config, &bufb->records_read);
231}
232
233static inline
234unsigned long subbuffer_get_records_count(
235 const struct lib_ring_buffer_config *config,
236 struct lib_ring_buffer_backend *bufb,
237 unsigned long idx)
238{
239 unsigned long sb_bindex;
240
241 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
242 return v_read(config, &bufb->array[sb_bindex]->records_commit);
243}
244
245/*
246 * Must be executed at subbuffer delivery when the writer has _exclusive_
c68be968
MD
247 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
248 * lib_ring_buffer_get_records_count() must be called to get the records
249 * count before this function, because it resets the records_commit
250 * count.
f3bc08c5
MD
251 */
252static inline
253unsigned long subbuffer_count_records_overrun(
254 const struct lib_ring_buffer_config *config,
255 struct lib_ring_buffer_backend *bufb,
256 unsigned long idx)
257{
258 struct lib_ring_buffer_backend_pages *pages;
259 unsigned long overruns, sb_bindex;
260
261 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
262 pages = bufb->array[sb_bindex];
263 overruns = v_read(config, &pages->records_unread);
264 v_set(config, &pages->records_unread,
265 v_read(config, &pages->records_commit));
266 v_set(config, &pages->records_commit, 0);
267
268 return overruns;
269}
270
271static inline
272void subbuffer_set_data_size(const struct lib_ring_buffer_config *config,
273 struct lib_ring_buffer_backend *bufb,
274 unsigned long idx,
275 unsigned long data_size)
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 pages->data_size = data_size;
283}
284
285static inline
286unsigned long subbuffer_get_read_data_size(
287 const struct lib_ring_buffer_config *config,
288 struct lib_ring_buffer_backend *bufb)
289{
290 struct lib_ring_buffer_backend_pages *pages;
291 unsigned long sb_bindex;
292
293 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
294 pages = bufb->array[sb_bindex];
295 return pages->data_size;
296}
297
298static inline
299unsigned long subbuffer_get_data_size(
300 const struct lib_ring_buffer_config *config,
301 struct lib_ring_buffer_backend *bufb,
302 unsigned long idx)
303{
304 struct lib_ring_buffer_backend_pages *pages;
305 unsigned long sb_bindex;
306
307 sb_bindex = subbuffer_id_get_index(config, bufb->buf_wsb[idx].id);
308 pages = bufb->array[sb_bindex];
309 return pages->data_size;
310}
311
312/**
313 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
314 * writer.
315 */
316static inline
317void lib_ring_buffer_clear_noref(const struct lib_ring_buffer_config *config,
318 struct lib_ring_buffer_backend *bufb,
319 unsigned long idx)
320{
321 unsigned long id, new_id;
322
323 if (config->mode != RING_BUFFER_OVERWRITE)
324 return;
325
326 /*
327 * Performing a volatile access to read the sb_pages, because we want to
328 * read a coherent version of the pointer and the associated noref flag.
329 */
330 id = ACCESS_ONCE(bufb->buf_wsb[idx].id);
331 for (;;) {
332 /* This check is called on the fast path for each record. */
333 if (likely(!subbuffer_id_is_noref(config, id))) {
334 /*
335 * Store after load dependency ordering the writes to
336 * the subbuffer after load and test of the noref flag
337 * matches the memory barrier implied by the cmpxchg()
338 * in update_read_sb_index().
339 */
340 return; /* Already writing to this buffer */
341 }
342 new_id = id;
343 subbuffer_id_clear_noref(config, &new_id);
344 new_id = cmpxchg(&bufb->buf_wsb[idx].id, id, new_id);
345 if (likely(new_id == id))
346 break;
347 id = new_id;
348 }
349}
350
351/**
352 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
353 * called by writer.
354 */
355static inline
356void lib_ring_buffer_set_noref_offset(const struct lib_ring_buffer_config *config,
357 struct lib_ring_buffer_backend *bufb,
358 unsigned long idx, unsigned long offset)
359{
360 if (config->mode != RING_BUFFER_OVERWRITE)
361 return;
362
363 /*
364 * Because ring_buffer_set_noref() is only called by a single thread
365 * (the one which updated the cc_sb value), there are no concurrent
366 * updates to take care of: other writers have not updated cc_sb, so
367 * they cannot set the noref flag, and concurrent readers cannot modify
368 * the pointer because the noref flag is not set yet.
369 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
370 * to the subbuffer before this set noref operation.
371 * subbuffer_set_noref() uses a volatile store to deal with concurrent
372 * readers of the noref flag.
373 */
374 CHAN_WARN_ON(bufb->chan,
375 subbuffer_id_is_noref(config, bufb->buf_wsb[idx].id));
376 /*
377 * Memory barrier that ensures counter stores are ordered before set
378 * noref and offset.
379 */
380 smp_mb();
381 subbuffer_id_set_noref_offset(config, &bufb->buf_wsb[idx].id, offset);
382}
383
384/**
385 * update_read_sb_index - Read-side subbuffer index update.
386 */
387static inline
388int update_read_sb_index(const struct lib_ring_buffer_config *config,
389 struct lib_ring_buffer_backend *bufb,
390 struct channel_backend *chanb,
391 unsigned long consumed_idx,
392 unsigned long consumed_count)
393{
394 unsigned long old_id, new_id;
395
396 if (config->mode == RING_BUFFER_OVERWRITE) {
397 /*
398 * Exchange the target writer subbuffer with our own unused
399 * subbuffer. No need to use ACCESS_ONCE() here to read the
400 * old_wpage, because the value read will be confirmed by the
401 * following cmpxchg().
402 */
403 old_id = bufb->buf_wsb[consumed_idx].id;
404 if (unlikely(!subbuffer_id_is_noref(config, old_id)))
405 return -EAGAIN;
406 /*
407 * Make sure the offset count we are expecting matches the one
408 * indicated by the writer.
409 */
410 if (unlikely(!subbuffer_id_compare_offset(config, old_id,
411 consumed_count)))
412 return -EAGAIN;
413 CHAN_WARN_ON(bufb->chan,
414 !subbuffer_id_is_noref(config, bufb->buf_rsb.id));
415 subbuffer_id_set_noref_offset(config, &bufb->buf_rsb.id,
416 consumed_count);
417 new_id = cmpxchg(&bufb->buf_wsb[consumed_idx].id, old_id,
418 bufb->buf_rsb.id);
419 if (unlikely(old_id != new_id))
420 return -EAGAIN;
421 bufb->buf_rsb.id = new_id;
422 } else {
423 /* No page exchange, use the writer page directly */
424 bufb->buf_rsb.id = bufb->buf_wsb[consumed_idx].id;
425 }
426 return 0;
427}
428
429/*
430 * Use the architecture-specific memcpy implementation for constant-sized
431 * inputs, but rely on an inline memcpy for length statically unknown.
432 * The function call to memcpy is just way too expensive for a fast path.
433 */
434#define lib_ring_buffer_do_copy(config, dest, src, len) \
435do { \
436 size_t __len = (len); \
437 if (__builtin_constant_p(len)) \
438 memcpy(dest, src, __len); \
439 else \
440 inline_memcpy(dest, src, __len); \
441} while (0)
442
4ea00e4f 443/*
7b8ea3a5 444 * We use __copy_from_user_inatomic to copy userspace data since we already
4ea00e4f
JD
445 * did the access_ok for the whole range.
446 */
447static inline
7b8ea3a5 448unsigned long lib_ring_buffer_do_copy_from_user_inatomic(void *dest,
4ea00e4f
JD
449 const void __user *src,
450 unsigned long len)
451{
7b8ea3a5 452 return __copy_from_user_inatomic(dest, src, len);
4ea00e4f
JD
453}
454
455/*
456 * write len bytes to dest with c
457 */
458static inline
459void lib_ring_buffer_do_memset(char *dest, int c,
460 unsigned long len)
461{
462 unsigned long i;
463
464 for (i = 0; i < len; i++)
465 dest[i] = c;
466}
467
886d51a3 468#endif /* _LIB_RING_BUFFER_BACKEND_INTERNAL_H */
This page took 0.045665 seconds and 4 git commands to generate.