Fix: Argument with 'nonnull' attribute passed null
[lttng-ust.git] / libringbuffer / ring_buffer_backend.c
1 /*
2 * ring_buffer_backend.c
3 *
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #define _GNU_SOURCE
22 #include <unistd.h>
23 #include <urcu/arch.h>
24 #include <limits.h>
25
26 #include <lttng/ringbuffer-config.h>
27 #include "vatomic.h"
28 #include "backend.h"
29 #include "frontend.h"
30 #include "smp.h"
31 #include "shm.h"
32
33 /**
34 * lib_ring_buffer_backend_allocate - allocate a channel buffer
35 * @config: ring buffer instance configuration
36 * @buf: the buffer struct
37 * @size: total size of the buffer
38 * @num_subbuf: number of subbuffers
39 * @extra_reader_sb: need extra subbuffer for reader
40 */
41 static
42 int lib_ring_buffer_backend_allocate(const struct lttng_ust_lib_ring_buffer_config *config,
43 struct lttng_ust_lib_ring_buffer_backend *bufb,
44 size_t size, size_t num_subbuf,
45 int extra_reader_sb,
46 struct lttng_ust_shm_handle *handle,
47 struct shm_object *shmobj)
48 {
49 struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend;
50 unsigned long subbuf_size, mmap_offset = 0;
51 unsigned long num_subbuf_alloc;
52 unsigned long i;
53 long page_size;
54
55 subbuf_size = chanb->subbuf_size;
56 num_subbuf_alloc = num_subbuf;
57
58 if (extra_reader_sb)
59 num_subbuf_alloc++;
60
61 page_size = sysconf(_SC_PAGE_SIZE);
62 if (page_size <= 0) {
63 goto page_size_error;
64 }
65
66 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp));
67 set_shmp(bufb->array, zalloc_shm(shmobj,
68 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp) * num_subbuf_alloc));
69 if (caa_unlikely(!shmp(handle, bufb->array)))
70 goto array_error;
71
72 /*
73 * This is the largest element (the buffer pages) which needs to
74 * be aligned on page size.
75 */
76 align_shm(shmobj, page_size);
77 set_shmp(bufb->memory_map, zalloc_shm(shmobj,
78 subbuf_size * num_subbuf_alloc));
79 if (caa_unlikely(!shmp(handle, bufb->memory_map)))
80 goto memory_map_error;
81
82 /* Allocate backend pages array elements */
83 for (i = 0; i < num_subbuf_alloc; i++) {
84 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages));
85 set_shmp(shmp_index(handle, bufb->array, i)->shmp,
86 zalloc_shm(shmobj,
87 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages)));
88 if (!shmp(handle, shmp_index(handle, bufb->array, i)->shmp))
89 goto free_array;
90 }
91
92 /* Allocate write-side subbuffer table */
93 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer));
94 set_shmp(bufb->buf_wsb, zalloc_shm(shmobj,
95 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer)
96 * num_subbuf));
97 if (caa_unlikely(!shmp(handle, bufb->buf_wsb)))
98 goto free_array;
99
100 for (i = 0; i < num_subbuf; i++)
101 shmp_index(handle, bufb->buf_wsb, i)->id = subbuffer_id(config, 0, 1, i);
102
103 /* Assign read-side subbuffer table */
104 if (extra_reader_sb)
105 bufb->buf_rsb.id = subbuffer_id(config, 0, 1,
106 num_subbuf_alloc - 1);
107 else
108 bufb->buf_rsb.id = subbuffer_id(config, 0, 1, 0);
109
110 /* Assign pages to page index */
111 for (i = 0; i < num_subbuf_alloc; i++) {
112 struct shm_ref ref;
113
114 ref.index = bufb->memory_map._ref.index;
115 ref.offset = bufb->memory_map._ref.offset;
116 ref.offset += i * subbuf_size;
117
118 set_shmp(shmp(handle, shmp_index(handle, bufb->array, i)->shmp)->p,
119 ref);
120 if (config->output == RING_BUFFER_MMAP) {
121 shmp(handle, shmp_index(handle, bufb->array, i)->shmp)->mmap_offset = mmap_offset;
122 mmap_offset += subbuf_size;
123 }
124 }
125
126 return 0;
127
128 free_array:
129 /* bufb->array[i] will be freed by shm teardown */
130 memory_map_error:
131 /* bufb->array will be freed by shm teardown */
132 array_error:
133 page_size_error:
134 return -ENOMEM;
135 }
136
137 int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend *bufb,
138 struct channel_backend *chanb, int cpu,
139 struct lttng_ust_shm_handle *handle,
140 struct shm_object *shmobj)
141 {
142 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
143
144 set_shmp(bufb->chan, handle->chan._ref);
145 bufb->cpu = cpu;
146
147 return lib_ring_buffer_backend_allocate(config, bufb, chanb->buf_size,
148 chanb->num_subbuf,
149 chanb->extra_reader_sb,
150 handle, shmobj);
151 }
152
153 void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend *bufb,
154 struct lttng_ust_shm_handle *handle)
155 {
156 struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend;
157 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
158 unsigned long num_subbuf_alloc;
159 unsigned int i;
160
161 num_subbuf_alloc = chanb->num_subbuf;
162 if (chanb->extra_reader_sb)
163 num_subbuf_alloc++;
164
165 for (i = 0; i < chanb->num_subbuf; i++)
166 shmp_index(handle, bufb->buf_wsb, i)->id = subbuffer_id(config, 0, 1, i);
167 if (chanb->extra_reader_sb)
168 bufb->buf_rsb.id = subbuffer_id(config, 0, 1,
169 num_subbuf_alloc - 1);
170 else
171 bufb->buf_rsb.id = subbuffer_id(config, 0, 1, 0);
172
173 for (i = 0; i < num_subbuf_alloc; i++) {
174 /* Don't reset mmap_offset */
175 v_set(config, &shmp(handle, shmp_index(handle, bufb->array, i)->shmp)->records_commit, 0);
176 v_set(config, &shmp(handle, shmp_index(handle, bufb->array, i)->shmp)->records_unread, 0);
177 shmp(handle, shmp_index(handle, bufb->array, i)->shmp)->data_size = 0;
178 /* Don't reset backend page and virt addresses */
179 }
180 /* Don't reset num_pages_per_subbuf, cpu, allocated */
181 v_set(config, &bufb->records_read, 0);
182 }
183
184 /*
185 * The frontend is responsible for also calling ring_buffer_backend_reset for
186 * each buffer when calling channel_backend_reset.
187 */
188 void channel_backend_reset(struct channel_backend *chanb)
189 {
190 struct channel *chan = caa_container_of(chanb, struct channel, backend);
191 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
192
193 /*
194 * Don't reset buf_size, subbuf_size, subbuf_size_order,
195 * num_subbuf_order, buf_size_order, extra_reader_sb, num_subbuf,
196 * priv, notifiers, config, cpumask and name.
197 */
198 chanb->start_tsc = config->cb.ring_buffer_clock_read(chan);
199 }
200
201 /**
202 * channel_backend_init - initialize a channel backend
203 * @chanb: channel backend
204 * @name: channel name
205 * @config: client ring buffer configuration
206 * @parent: dentry of parent directory, %NULL for root directory
207 * @subbuf_size: size of sub-buffers (> page size, power of 2)
208 * @num_subbuf: number of sub-buffers (power of 2)
209 * @lttng_ust_shm_handle: shared memory handle
210 *
211 * Returns channel pointer if successful, %NULL otherwise.
212 *
213 * Creates per-cpu channel buffers using the sizes and attributes
214 * specified. The created channel buffer files will be named
215 * name_0...name_N-1. File permissions will be %S_IRUSR.
216 *
217 * Called with CPU hotplug disabled.
218 */
219 int channel_backend_init(struct channel_backend *chanb,
220 const char *name,
221 const struct lttng_ust_lib_ring_buffer_config *config,
222 size_t subbuf_size, size_t num_subbuf,
223 struct lttng_ust_shm_handle *handle)
224 {
225 struct channel *chan = caa_container_of(chanb, struct channel, backend);
226 unsigned int i;
227 int ret;
228 size_t shmsize = 0, num_subbuf_alloc;
229 long page_size;
230
231 if (!name)
232 return -EPERM;
233
234 page_size = sysconf(_SC_PAGE_SIZE);
235 if (page_size <= 0) {
236 return -ENOMEM;
237 }
238 /* Check that the subbuffer size is larger than a page. */
239 if (subbuf_size < page_size)
240 return -EINVAL;
241
242 /*
243 * Make sure the number of subbuffers and subbuffer size are
244 * power of 2, and nonzero.
245 */
246 if (!subbuf_size || (subbuf_size & (subbuf_size - 1)))
247 return -EINVAL;
248 if (!num_subbuf || (num_subbuf & (num_subbuf - 1)))
249 return -EINVAL;
250 /*
251 * Overwrite mode buffers require at least 2 subbuffers per
252 * buffer.
253 */
254 if (config->mode == RING_BUFFER_OVERWRITE && num_subbuf < 2)
255 return -EINVAL;
256
257 ret = subbuffer_id_check_index(config, num_subbuf);
258 if (ret)
259 return ret;
260
261 chanb->buf_size = num_subbuf * subbuf_size;
262 chanb->subbuf_size = subbuf_size;
263 chanb->buf_size_order = get_count_order(chanb->buf_size);
264 chanb->subbuf_size_order = get_count_order(subbuf_size);
265 chanb->num_subbuf_order = get_count_order(num_subbuf);
266 chanb->extra_reader_sb =
267 (config->mode == RING_BUFFER_OVERWRITE) ? 1 : 0;
268 chanb->num_subbuf = num_subbuf;
269 strncpy(chanb->name, name, NAME_MAX);
270 chanb->name[NAME_MAX - 1] = '\0';
271 memcpy(&chanb->config, config, sizeof(*config));
272
273 /* Per-cpu buffer size: control (prior to backend) */
274 shmsize = offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer));
275 shmsize += sizeof(struct lttng_ust_lib_ring_buffer);
276
277 /* Per-cpu buffer size: backend */
278 /* num_subbuf + 1 is the worse case */
279 num_subbuf_alloc = num_subbuf + 1;
280 shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp));
281 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp) * num_subbuf_alloc;
282 shmsize += offset_align(shmsize, page_size);
283 shmsize += subbuf_size * num_subbuf_alloc;
284 shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages));
285 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_backend_pages) * num_subbuf_alloc;
286 shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer));
287 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer) * num_subbuf;
288 /* Per-cpu buffer size: control (after backend) */
289 shmsize += offset_align(shmsize, __alignof__(struct commit_counters_hot));
290 shmsize += sizeof(struct commit_counters_hot) * num_subbuf;
291 shmsize += offset_align(shmsize, __alignof__(struct commit_counters_cold));
292 shmsize += sizeof(struct commit_counters_cold) * num_subbuf;
293
294 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
295 struct lttng_ust_lib_ring_buffer *buf;
296 /*
297 * We need to allocate for all possible cpus.
298 */
299 for_each_possible_cpu(i) {
300 struct shm_object *shmobj;
301
302 shmobj = shm_object_table_alloc(handle->table, shmsize,
303 SHM_OBJECT_SHM);
304 if (!shmobj)
305 goto end;
306 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer));
307 set_shmp(chanb->buf[i].shmp, zalloc_shm(shmobj, sizeof(struct lttng_ust_lib_ring_buffer)));
308 buf = shmp(handle, chanb->buf[i].shmp);
309 if (!buf)
310 goto end;
311 set_shmp(buf->self, chanb->buf[i].shmp._ref);
312 ret = lib_ring_buffer_create(buf, chanb, i,
313 handle, shmobj);
314 if (ret)
315 goto free_bufs; /* cpu hotplug locked */
316 }
317 } else {
318 struct shm_object *shmobj;
319 struct lttng_ust_lib_ring_buffer *buf;
320
321 shmobj = shm_object_table_alloc(handle->table, shmsize,
322 SHM_OBJECT_SHM);
323 if (!shmobj)
324 goto end;
325 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer));
326 set_shmp(chanb->buf[0].shmp, zalloc_shm(shmobj, sizeof(struct lttng_ust_lib_ring_buffer)));
327 buf = shmp(handle, chanb->buf[0].shmp);
328 if (!buf)
329 goto end;
330 set_shmp(buf->self, chanb->buf[0].shmp._ref);
331 ret = lib_ring_buffer_create(buf, chanb, -1,
332 handle, shmobj);
333 if (ret)
334 goto free_bufs;
335 }
336 chanb->start_tsc = config->cb.ring_buffer_clock_read(chan);
337
338 return 0;
339
340 free_bufs:
341 /* We only free the buffer data upon shm teardown */
342 end:
343 return -ENOMEM;
344 }
345
346 /**
347 * channel_backend_free - destroy the channel
348 * @chan: the channel
349 *
350 * Destroy all channel buffers and frees the channel.
351 */
352 void channel_backend_free(struct channel_backend *chanb,
353 struct lttng_ust_shm_handle *handle)
354 {
355 /* SHM teardown takes care of everything */
356 }
357
358 /**
359 * lib_ring_buffer_read - read data from ring_buffer_buffer.
360 * @bufb : buffer backend
361 * @offset : offset within the buffer
362 * @dest : destination address
363 * @len : length to copy to destination
364 *
365 * Should be protected by get_subbuf/put_subbuf.
366 * Returns the length copied.
367 */
368 size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend *bufb, size_t offset,
369 void *dest, size_t len, struct lttng_ust_shm_handle *handle)
370 {
371 struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend;
372 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
373 ssize_t orig_len;
374 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
375 unsigned long sb_bindex, id;
376 void *src;
377
378 orig_len = len;
379 offset &= chanb->buf_size - 1;
380
381 if (caa_unlikely(!len))
382 return 0;
383 id = bufb->buf_rsb.id;
384 sb_bindex = subbuffer_id_get_index(config, id);
385 rpages = shmp_index(handle, bufb->array, sb_bindex);
386 /*
387 * Underlying layer should never ask for reads across
388 * subbuffers.
389 */
390 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
391 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
392 && subbuffer_id_is_noref(config, id));
393 src = shmp_index(handle, shmp(handle, rpages->shmp)->p,
394 offset & (chanb->subbuf_size - 1));
395 if (caa_unlikely(!src))
396 return 0;
397 memcpy(dest, src, len);
398 return orig_len;
399 }
400
401 /**
402 * lib_ring_buffer_read_cstr - read a C-style string from ring_buffer.
403 * @bufb : buffer backend
404 * @offset : offset within the buffer
405 * @dest : destination address
406 * @len : destination's length
407 *
408 * Return string's length, or -EINVAL on error.
409 * Should be protected by get_subbuf/put_subbuf.
410 * Destination length should be at least 1 to hold '\0'.
411 */
412 int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend *bufb, size_t offset,
413 void *dest, size_t len, struct lttng_ust_shm_handle *handle)
414 {
415 struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend;
416 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
417 ssize_t string_len, orig_offset;
418 char *str;
419 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
420 unsigned long sb_bindex, id;
421
422 if (caa_unlikely(!len))
423 return -EINVAL;
424 offset &= chanb->buf_size - 1;
425 orig_offset = offset;
426 id = bufb->buf_rsb.id;
427 sb_bindex = subbuffer_id_get_index(config, id);
428 rpages = shmp_index(handle, bufb->array, sb_bindex);
429 /*
430 * Underlying layer should never ask for reads across
431 * subbuffers.
432 */
433 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
434 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
435 && subbuffer_id_is_noref(config, id));
436 str = shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1));
437 if (caa_unlikely(!str))
438 return -EINVAL;
439 string_len = strnlen(str, len);
440 if (dest && len) {
441 memcpy(dest, str, string_len);
442 ((char *)dest)[0] = 0;
443 }
444 return offset - orig_offset;
445 }
446
447 /**
448 * lib_ring_buffer_read_offset_address - get address of a buffer location
449 * @bufb : buffer backend
450 * @offset : offset within the buffer.
451 *
452 * Return the address where a given offset is located (for read).
453 * Should be used to get the current subbuffer header pointer. Given we know
454 * it's never on a page boundary, it's safe to read/write directly
455 * from/to this address, as long as the read/write is never bigger than
456 * a page size.
457 */
458 void *lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
459 size_t offset,
460 struct lttng_ust_shm_handle *handle)
461 {
462 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
463 struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend;
464 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
465 unsigned long sb_bindex, id;
466
467 offset &= chanb->buf_size - 1;
468 id = bufb->buf_rsb.id;
469 sb_bindex = subbuffer_id_get_index(config, id);
470 rpages = shmp_index(handle, bufb->array, sb_bindex);
471 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
472 && subbuffer_id_is_noref(config, id));
473 return shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1));
474 }
475
476 /**
477 * lib_ring_buffer_offset_address - get address of a location within the buffer
478 * @bufb : buffer backend
479 * @offset : offset within the buffer.
480 *
481 * Return the address where a given offset is located.
482 * Should be used to get the current subbuffer header pointer. Given we know
483 * it's always at the beginning of a page, it's safe to write directly to this
484 * address, as long as the write is never bigger than a page size.
485 */
486 void *lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
487 size_t offset,
488 struct lttng_ust_shm_handle *handle)
489 {
490 size_t sbidx;
491 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
492 struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend;
493 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
494 unsigned long sb_bindex, id;
495
496 offset &= chanb->buf_size - 1;
497 sbidx = offset >> chanb->subbuf_size_order;
498 id = shmp_index(handle, bufb->buf_wsb, sbidx)->id;
499 sb_bindex = subbuffer_id_get_index(config, id);
500 rpages = shmp_index(handle, bufb->array, sb_bindex);
501 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
502 && subbuffer_id_is_noref(config, id));
503 return shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1));
504 }
This page took 0.047793 seconds and 4 git commands to generate.