Rename "tsc" to "timestamp"
[lttng-modules.git] / src / lib / ringbuffer / ring_buffer_backend.c
CommitLineData
b7cdc182 1/* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
9f36eaed 2 *
f3bc08c5
MD
3 * ring_buffer_backend.c
4 *
886d51a3 5 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
f3bc08c5
MD
6 */
7
f3bc08c5
MD
8#include <linux/stddef.h>
9#include <linux/module.h>
10#include <linux/string.h>
11#include <linux/bitops.h>
12#include <linux/delay.h>
13#include <linux/errno.h>
14#include <linux/slab.h>
f3bc08c5 15#include <linux/mm.h>
df388b78 16#include <linux/vmalloc.h>
f3bc08c5 17
ffcc8734 18#include <wrapper/cpu.h>
7502f47a 19#include <wrapper/mm.h>
263b6c88 20#include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
24591303
MD
21#include <ringbuffer/config.h>
22#include <ringbuffer/backend.h>
23#include <ringbuffer/frontend.h>
f3bc08c5
MD
24
25/**
26 * lib_ring_buffer_backend_allocate - allocate a channel buffer
27 * @config: ring buffer instance configuration
28 * @buf: the buffer struct
29 * @size: total size of the buffer
30 * @num_subbuf: number of subbuffers
31 * @extra_reader_sb: need extra subbuffer for reader
32 */
33static
e20c0fec
MD
34int lib_ring_buffer_backend_allocate(const struct lttng_kernel_ring_buffer_config *config,
35 struct lttng_kernel_ring_buffer_backend *bufb,
f3bc08c5
MD
36 size_t size, size_t num_subbuf,
37 int extra_reader_sb)
38{
39 struct channel_backend *chanb = &bufb->chan->backend;
40 unsigned long j, num_pages, num_pages_per_subbuf, page_idx = 0;
41 unsigned long subbuf_size, mmap_offset = 0;
42 unsigned long num_subbuf_alloc;
43 struct page **pages;
f3bc08c5
MD
44 unsigned long i;
45
46 num_pages = size >> PAGE_SHIFT;
1f0ab1eb
FD
47
48 /*
7502f47a
FD
49 * Verify that there is enough free pages available on the system for
50 * the current allocation request.
51 * wrapper_check_enough_free_pages uses si_mem_available() if available
52 * and returns if there should be enough free pages based on the
53 * current estimate.
1f0ab1eb 54 */
7502f47a 55 if (!wrapper_check_enough_free_pages(num_pages))
1f0ab1eb
FD
56 goto not_enough_pages;
57
58 /*
59 * Set the current user thread as the first target of the OOM killer.
60 * If the estimate received by si_mem_available() was off, and we do
61 * end up running out of memory because of this buffer allocation, we
62 * want to kill the offending app first.
63 */
686eb005 64 set_current_oom_origin();
1f0ab1eb 65
f3bc08c5
MD
66 num_pages_per_subbuf = num_pages >> get_count_order(num_subbuf);
67 subbuf_size = chanb->subbuf_size;
68 num_subbuf_alloc = num_subbuf;
69
70 if (extra_reader_sb) {
71 num_pages += num_pages_per_subbuf; /* Add pages for reader */
72 num_subbuf_alloc++;
73 }
74
df388b78 75 pages = vmalloc_node(ALIGN(sizeof(*pages) * num_pages,
f3bc08c5 76 1 << INTERNODE_CACHE_SHIFT),
df388b78 77 cpu_to_node(max(bufb->cpu, 0)));
f3bc08c5
MD
78 if (unlikely(!pages))
79 goto pages_error;
80
48f5e0b5 81 bufb->array = lttng_kvmalloc_node(ALIGN(sizeof(*bufb->array)
f3bc08c5
MD
82 * num_subbuf_alloc,
83 1 << INTERNODE_CACHE_SHIFT),
df388b78
MD
84 GFP_KERNEL | __GFP_NOWARN,
85 cpu_to_node(max(bufb->cpu, 0)));
f3bc08c5
MD
86 if (unlikely(!bufb->array))
87 goto array_error;
88
89 for (i = 0; i < num_pages; i++) {
90 pages[i] = alloc_pages_node(cpu_to_node(max(bufb->cpu, 0)),
df388b78 91 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, 0);
f3bc08c5
MD
92 if (unlikely(!pages[i]))
93 goto depopulate;
f3bc08c5
MD
94 }
95 bufb->num_pages_per_subbuf = num_pages_per_subbuf;
96
97 /* Allocate backend pages array elements */
98 for (i = 0; i < num_subbuf_alloc; i++) {
99 bufb->array[i] =
48f5e0b5 100 lttng_kvzalloc_node(ALIGN(
e20c0fec
MD
101 sizeof(struct lttng_kernel_ring_buffer_backend_pages) +
102 sizeof(struct lttng_kernel_ring_buffer_backend_page)
f3bc08c5
MD
103 * num_pages_per_subbuf,
104 1 << INTERNODE_CACHE_SHIFT),
df388b78
MD
105 GFP_KERNEL | __GFP_NOWARN,
106 cpu_to_node(max(bufb->cpu, 0)));
f3bc08c5
MD
107 if (!bufb->array[i])
108 goto free_array;
109 }
110
111 /* Allocate write-side subbuffer table */
48f5e0b5 112 bufb->buf_wsb = lttng_kvzalloc_node(ALIGN(
e20c0fec 113 sizeof(struct lttng_kernel_ring_buffer_backend_subbuffer)
f3bc08c5
MD
114 * num_subbuf,
115 1 << INTERNODE_CACHE_SHIFT),
df388b78
MD
116 GFP_KERNEL | __GFP_NOWARN,
117 cpu_to_node(max(bufb->cpu, 0)));
f3bc08c5
MD
118 if (unlikely(!bufb->buf_wsb))
119 goto free_array;
120
121 for (i = 0; i < num_subbuf; i++)
122 bufb->buf_wsb[i].id = subbuffer_id(config, 0, 1, i);
123
124 /* Assign read-side subbuffer table */
125 if (extra_reader_sb)
126 bufb->buf_rsb.id = subbuffer_id(config, 0, 1,
127 num_subbuf_alloc - 1);
128 else
129 bufb->buf_rsb.id = subbuffer_id(config, 0, 1, 0);
130
5b3cf4f9 131 /* Allocate subbuffer packet counter table */
48f5e0b5 132 bufb->buf_cnt = lttng_kvzalloc_node(ALIGN(
e20c0fec 133 sizeof(struct lttng_kernel_ring_buffer_backend_counts)
5b3cf4f9
JD
134 * num_subbuf,
135 1 << INTERNODE_CACHE_SHIFT),
df388b78
MD
136 GFP_KERNEL | __GFP_NOWARN,
137 cpu_to_node(max(bufb->cpu, 0)));
5b3cf4f9
JD
138 if (unlikely(!bufb->buf_cnt))
139 goto free_wsb;
140
f3bc08c5
MD
141 /* Assign pages to page index */
142 for (i = 0; i < num_subbuf_alloc; i++) {
143 for (j = 0; j < num_pages_per_subbuf; j++) {
144 CHAN_WARN_ON(chanb, page_idx > num_pages);
0112cb7b
MD
145 bufb->array[i]->p[j].virt = page_address(pages[page_idx]);
146 bufb->array[i]->p[j].pfn = page_to_pfn(pages[page_idx]);
f3bc08c5
MD
147 page_idx++;
148 }
149 if (config->output == RING_BUFFER_MMAP) {
150 bufb->array[i]->mmap_offset = mmap_offset;
151 mmap_offset += subbuf_size;
152 }
153 }
154
155 /*
156 * If kmalloc ever uses vmalloc underneath, make sure the buffer pages
157 * will not fault.
158 */
263b6c88 159 wrapper_vmalloc_sync_mappings();
686eb005 160 clear_current_oom_origin();
df388b78 161 vfree(pages);
f3bc08c5
MD
162 return 0;
163
5b3cf4f9 164free_wsb:
48f5e0b5 165 lttng_kvfree(bufb->buf_wsb);
f3bc08c5
MD
166free_array:
167 for (i = 0; (i < num_subbuf_alloc && bufb->array[i]); i++)
48f5e0b5 168 lttng_kvfree(bufb->array[i]);
f3bc08c5
MD
169depopulate:
170 /* Free all allocated pages */
171 for (i = 0; (i < num_pages && pages[i]); i++)
172 __free_page(pages[i]);
48f5e0b5 173 lttng_kvfree(bufb->array);
f3bc08c5 174array_error:
df388b78 175 vfree(pages);
f3bc08c5 176pages_error:
686eb005 177 clear_current_oom_origin();
1f0ab1eb 178not_enough_pages:
f3bc08c5
MD
179 return -ENOMEM;
180}
181
e20c0fec 182int lib_ring_buffer_backend_create(struct lttng_kernel_ring_buffer_backend *bufb,
f3bc08c5
MD
183 struct channel_backend *chanb, int cpu)
184{
e20c0fec 185 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
f3bc08c5 186
860c213b 187 bufb->chan = container_of(chanb, struct lttng_kernel_ring_buffer_channel, backend);
f3bc08c5
MD
188 bufb->cpu = cpu;
189
190 return lib_ring_buffer_backend_allocate(config, bufb, chanb->buf_size,
191 chanb->num_subbuf,
192 chanb->extra_reader_sb);
193}
194
e20c0fec 195void lib_ring_buffer_backend_free(struct lttng_kernel_ring_buffer_backend *bufb)
f3bc08c5
MD
196{
197 struct channel_backend *chanb = &bufb->chan->backend;
198 unsigned long i, j, num_subbuf_alloc;
199
200 num_subbuf_alloc = chanb->num_subbuf;
201 if (chanb->extra_reader_sb)
202 num_subbuf_alloc++;
203
48f5e0b5
MJ
204 lttng_kvfree(bufb->buf_wsb);
205 lttng_kvfree(bufb->buf_cnt);
f3bc08c5
MD
206 for (i = 0; i < num_subbuf_alloc; i++) {
207 for (j = 0; j < bufb->num_pages_per_subbuf; j++)
0112cb7b 208 __free_page(pfn_to_page(bufb->array[i]->p[j].pfn));
48f5e0b5 209 lttng_kvfree(bufb->array[i]);
f3bc08c5 210 }
48f5e0b5 211 lttng_kvfree(bufb->array);
f3bc08c5
MD
212 bufb->allocated = 0;
213}
214
e20c0fec 215void lib_ring_buffer_backend_reset(struct lttng_kernel_ring_buffer_backend *bufb)
f3bc08c5
MD
216{
217 struct channel_backend *chanb = &bufb->chan->backend;
e20c0fec 218 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
f3bc08c5
MD
219 unsigned long num_subbuf_alloc;
220 unsigned int i;
221
222 num_subbuf_alloc = chanb->num_subbuf;
223 if (chanb->extra_reader_sb)
224 num_subbuf_alloc++;
225
226 for (i = 0; i < chanb->num_subbuf; i++)
227 bufb->buf_wsb[i].id = subbuffer_id(config, 0, 1, i);
228 if (chanb->extra_reader_sb)
229 bufb->buf_rsb.id = subbuffer_id(config, 0, 1,
230 num_subbuf_alloc - 1);
231 else
232 bufb->buf_rsb.id = subbuffer_id(config, 0, 1, 0);
233
234 for (i = 0; i < num_subbuf_alloc; i++) {
235 /* Don't reset mmap_offset */
236 v_set(config, &bufb->array[i]->records_commit, 0);
237 v_set(config, &bufb->array[i]->records_unread, 0);
238 bufb->array[i]->data_size = 0;
239 /* Don't reset backend page and virt addresses */
240 }
241 /* Don't reset num_pages_per_subbuf, cpu, allocated */
242 v_set(config, &bufb->records_read, 0);
243}
244
245/*
246 * The frontend is responsible for also calling ring_buffer_backend_reset for
247 * each buffer when calling channel_backend_reset.
248 */
249void channel_backend_reset(struct channel_backend *chanb)
250{
860c213b 251 struct lttng_kernel_ring_buffer_channel *chan = container_of(chanb, struct lttng_kernel_ring_buffer_channel, backend);
e20c0fec 252 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
f3bc08c5
MD
253
254 /*
255 * Don't reset buf_size, subbuf_size, subbuf_size_order,
256 * num_subbuf_order, buf_size_order, extra_reader_sb, num_subbuf,
257 * priv, notifiers, config, cpumask and name.
258 */
3a6d0934 259 chanb->start_timestamp = config->cb.ring_buffer_clock_read(chan);
f3bc08c5
MD
260}
261
5f4c791e 262#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
1e367326
MD
263
264/*
265 * No need to implement a "dead" callback to do a buffer switch here,
266 * because it will happen when tracing is stopped, or will be done by
267 * switch timer CPU DEAD callback.
268 * We don't free buffers when CPU go away, because it would make trace
269 * data vanish, which is unwanted.
270 */
271int lttng_cpuhp_rb_backend_prepare(unsigned int cpu,
272 struct lttng_cpuhp_node *node)
273{
274 struct channel_backend *chanb = container_of(node,
275 struct channel_backend, cpuhp_prepare);
e20c0fec
MD
276 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
277 struct lttng_kernel_ring_buffer *buf;
1e367326
MD
278 int ret;
279
280 CHAN_WARN_ON(chanb, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
281
282 buf = per_cpu_ptr(chanb->buf, cpu);
283 ret = lib_ring_buffer_create(buf, chanb, cpu);
284 if (ret) {
285 printk(KERN_ERR
5a15f70c 286 "LTTng: ring_buffer_cpu_hp_callback: cpu %d "
1e367326
MD
287 "buffer creation failed\n", cpu);
288 return ret;
289 }
290 return 0;
291}
292EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_backend_prepare);
293
5f4c791e 294#else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
1e367326 295
f3bc08c5 296#ifdef CONFIG_HOTPLUG_CPU
1e367326 297
f3bc08c5
MD
298/**
299 * lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
300 * @nb: notifier block
301 * @action: hotplug action to take
302 * @hcpu: CPU number
303 *
304 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
305 */
306static
e8f071d5 307int lib_ring_buffer_cpu_hp_callback(struct notifier_block *nb,
f3bc08c5
MD
308 unsigned long action,
309 void *hcpu)
310{
311 unsigned int cpu = (unsigned long)hcpu;
312 struct channel_backend *chanb = container_of(nb, struct channel_backend,
313 cpu_hp_notifier);
e20c0fec
MD
314 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
315 struct lttng_kernel_ring_buffer *buf;
f3bc08c5
MD
316 int ret;
317
318 CHAN_WARN_ON(chanb, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
319
320 switch (action) {
321 case CPU_UP_PREPARE:
322 case CPU_UP_PREPARE_FROZEN:
323 buf = per_cpu_ptr(chanb->buf, cpu);
324 ret = lib_ring_buffer_create(buf, chanb, cpu);
325 if (ret) {
326 printk(KERN_ERR
5a15f70c 327 "LTTng: ring_buffer_cpu_hp_callback: cpu %d "
f3bc08c5
MD
328 "buffer creation failed\n", cpu);
329 return NOTIFY_BAD;
330 }
331 break;
332 case CPU_DEAD:
333 case CPU_DEAD_FROZEN:
334 /* No need to do a buffer switch here, because it will happen
335 * when tracing is stopped, or will be done by switch timer CPU
336 * DEAD callback. */
337 break;
338 }
339 return NOTIFY_OK;
340}
1e367326 341
f3bc08c5
MD
342#endif
343
5f4c791e 344#endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
1e367326 345
f3bc08c5
MD
346/**
347 * channel_backend_init - initialize a channel backend
348 * @chanb: channel backend
349 * @name: channel name
350 * @config: client ring buffer configuration
351 * @priv: client private data
352 * @parent: dentry of parent directory, %NULL for root directory
353 * @subbuf_size: size of sub-buffers (> PAGE_SIZE, power of 2)
354 * @num_subbuf: number of sub-buffers (power of 2)
355 *
356 * Returns channel pointer if successful, %NULL otherwise.
357 *
358 * Creates per-cpu channel buffers using the sizes and attributes
359 * specified. The created channel buffer files will be named
360 * name_0...name_N-1. File permissions will be %S_IRUSR.
361 *
362 * Called with CPU hotplug disabled.
363 */
364int channel_backend_init(struct channel_backend *chanb,
365 const char *name,
e20c0fec 366 const struct lttng_kernel_ring_buffer_config *config,
f3bc08c5
MD
367 void *priv, size_t subbuf_size, size_t num_subbuf)
368{
860c213b 369 struct lttng_kernel_ring_buffer_channel *chan = container_of(chanb, struct lttng_kernel_ring_buffer_channel, backend);
f3bc08c5
MD
370 unsigned int i;
371 int ret;
372
373 if (!name)
374 return -EPERM;
375
f3bc08c5 376 /* Check that the subbuffer size is larger than a page. */
2fb46300
MD
377 if (subbuf_size < PAGE_SIZE)
378 return -EINVAL;
f3bc08c5
MD
379
380 /*
bbda3a00
MD
381 * Make sure the number of subbuffers and subbuffer size are
382 * power of 2 and nonzero.
f3bc08c5 383 */
bbda3a00 384 if (!subbuf_size || (subbuf_size & (subbuf_size - 1)))
863497fa 385 return -EINVAL;
bbda3a00 386 if (!num_subbuf || (num_subbuf & (num_subbuf - 1)))
863497fa 387 return -EINVAL;
5140d2b3
MD
388 /*
389 * Overwrite mode buffers require at least 2 subbuffers per
390 * buffer.
391 */
392 if (config->mode == RING_BUFFER_OVERWRITE && num_subbuf < 2)
393 return -EINVAL;
f3bc08c5
MD
394
395 ret = subbuffer_id_check_index(config, num_subbuf);
396 if (ret)
397 return ret;
398
399 chanb->priv = priv;
400 chanb->buf_size = num_subbuf * subbuf_size;
401 chanb->subbuf_size = subbuf_size;
402 chanb->buf_size_order = get_count_order(chanb->buf_size);
403 chanb->subbuf_size_order = get_count_order(subbuf_size);
404 chanb->num_subbuf_order = get_count_order(num_subbuf);
405 chanb->extra_reader_sb =
406 (config->mode == RING_BUFFER_OVERWRITE) ? 1 : 0;
407 chanb->num_subbuf = num_subbuf;
a3d0aa68 408 strscpy(chanb->name, name, NAME_MAX);
5a8fd222 409 memcpy(&chanb->config, config, sizeof(chanb->config));
f3bc08c5
MD
410
411 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
412 if (!zalloc_cpumask_var(&chanb->cpumask, GFP_KERNEL))
413 return -ENOMEM;
414 }
415
416 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
417 /* Allocating the buffer per-cpu structures */
e20c0fec 418 chanb->buf = alloc_percpu(struct lttng_kernel_ring_buffer);
f3bc08c5
MD
419 if (!chanb->buf)
420 goto free_cpumask;
421
5f4c791e 422#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
1e367326
MD
423 chanb->cpuhp_prepare.component = LTTNG_RING_BUFFER_BACKEND;
424 ret = cpuhp_state_add_instance(lttng_rb_hp_prepare,
425 &chanb->cpuhp_prepare.node);
426 if (ret)
427 goto free_bufs;
5f4c791e 428#else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
1e367326
MD
429
430 {
431 /*
432 * In case of non-hotplug cpu, if the ring-buffer is allocated
433 * in early initcall, it will not be notified of secondary cpus.
434 * In that off case, we need to allocate for all possible cpus.
435 */
f3bc08c5 436#ifdef CONFIG_HOTPLUG_CPU
1e367326
MD
437 /*
438 * buf->backend.allocated test takes care of concurrent CPU
439 * hotplug.
440 * Priority higher than frontend, so we create the ring buffer
441 * before we start the timer.
442 */
443 chanb->cpu_hp_notifier.notifier_call =
444 lib_ring_buffer_cpu_hp_callback;
445 chanb->cpu_hp_notifier.priority = 5;
446 register_hotcpu_notifier(&chanb->cpu_hp_notifier);
447
ffcc8734 448 lttng_cpus_read_lock();
1e367326
MD
449 for_each_online_cpu(i) {
450 ret = lib_ring_buffer_create(per_cpu_ptr(chanb->buf, i),
451 chanb, i);
452 if (ret)
453 goto free_bufs; /* cpu hotplug locked */
454 }
ffcc8734 455 lttng_cpus_read_unlock();
f3bc08c5 456#else
1e367326
MD
457 for_each_possible_cpu(i) {
458 ret = lib_ring_buffer_create(per_cpu_ptr(chanb->buf, i),
459 chanb, i);
460 if (ret)
461 goto free_bufs;
462 }
f3bc08c5 463#endif
1e367326 464 }
5f4c791e 465#endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
f3bc08c5 466 } else {
e20c0fec 467 chanb->buf = kzalloc(sizeof(struct lttng_kernel_ring_buffer), GFP_KERNEL);
f3bc08c5
MD
468 if (!chanb->buf)
469 goto free_cpumask;
470 ret = lib_ring_buffer_create(chanb->buf, chanb, -1);
471 if (ret)
472 goto free_bufs;
473 }
3a6d0934 474 chanb->start_timestamp = config->cb.ring_buffer_clock_read(chan);
f3bc08c5
MD
475
476 return 0;
477
478free_bufs:
479 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
5f4c791e 480#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
5f14d8ae
MD
481 /*
482 * Teardown of lttng_rb_hp_prepare instance
483 * on "add" error is handled within cpu hotplug,
484 * no teardown to do from the caller.
485 */
5f4c791e 486#else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
1e367326 487#ifdef CONFIG_HOTPLUG_CPU
ffcc8734 488 lttng_cpus_read_unlock();
1e367326
MD
489 unregister_hotcpu_notifier(&chanb->cpu_hp_notifier);
490#endif
5f4c791e 491#endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
f3bc08c5 492 for_each_possible_cpu(i) {
e20c0fec 493 struct lttng_kernel_ring_buffer *buf =
1e367326 494 per_cpu_ptr(chanb->buf, i);
f3bc08c5
MD
495
496 if (!buf->backend.allocated)
497 continue;
498 lib_ring_buffer_free(buf);
499 }
f3bc08c5
MD
500 free_percpu(chanb->buf);
501 } else
502 kfree(chanb->buf);
503free_cpumask:
504 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
505 free_cpumask_var(chanb->cpumask);
506 return -ENOMEM;
507}
508
509/**
510 * channel_backend_unregister_notifiers - unregister notifiers
511 * @chan: the channel
512 *
513 * Holds CPU hotplug.
514 */
515void channel_backend_unregister_notifiers(struct channel_backend *chanb)
516{
e20c0fec 517 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
f3bc08c5 518
1e367326 519 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
5f4c791e 520#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
1e367326
MD
521 int ret;
522
523 ret = cpuhp_state_remove_instance(lttng_rb_hp_prepare,
524 &chanb->cpuhp_prepare.node);
525 WARN_ON(ret);
5f4c791e 526#else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
f3bc08c5 527 unregister_hotcpu_notifier(&chanb->cpu_hp_notifier);
5f4c791e 528#endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
1e367326 529 }
f3bc08c5
MD
530}
531
532/**
533 * channel_backend_free - destroy the channel
534 * @chan: the channel
535 *
536 * Destroy all channel buffers and frees the channel.
537 */
538void channel_backend_free(struct channel_backend *chanb)
539{
e20c0fec 540 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
f3bc08c5
MD
541 unsigned int i;
542
543 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
544 for_each_possible_cpu(i) {
e20c0fec 545 struct lttng_kernel_ring_buffer *buf = per_cpu_ptr(chanb->buf, i);
f3bc08c5
MD
546
547 if (!buf->backend.allocated)
548 continue;
549 lib_ring_buffer_free(buf);
550 }
551 free_cpumask_var(chanb->cpumask);
552 free_percpu(chanb->buf);
553 } else {
e20c0fec 554 struct lttng_kernel_ring_buffer *buf = chanb->buf;
f3bc08c5
MD
555
556 CHAN_WARN_ON(chanb, !buf->backend.allocated);
557 lib_ring_buffer_free(buf);
558 kfree(buf);
559 }
560}
561
562/**
563 * lib_ring_buffer_write - write data to a ring_buffer buffer.
564 * @bufb : buffer backend
565 * @offset : offset within the buffer
566 * @src : source address
567 * @len : length to write
f3bc08c5 568 */
e20c0fec 569void _lib_ring_buffer_write(struct lttng_kernel_ring_buffer_backend *bufb, size_t offset,
4774817f 570 const void *src, size_t len)
f3bc08c5
MD
571{
572 struct channel_backend *chanb = &bufb->chan->backend;
e20c0fec 573 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
4774817f 574 size_t sbidx, index, bytes_left_in_page;
e20c0fec 575 struct lttng_kernel_ring_buffer_backend_pages *rpages;
f3bc08c5
MD
576 unsigned long sb_bindex, id;
577
578 do {
f3bc08c5
MD
579 sbidx = offset >> chanb->subbuf_size_order;
580 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
581
582 /*
583 * Underlying layer should never ask for writes across
584 * subbuffers.
585 */
586 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
587
4774817f 588 bytes_left_in_page = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
f3bc08c5
MD
589 id = bufb->buf_wsb[sbidx].id;
590 sb_bindex = subbuffer_id_get_index(config, id);
591 rpages = bufb->array[sb_bindex];
592 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
593 && subbuffer_id_is_noref(config, id));
594 lib_ring_buffer_do_copy(config,
595 rpages->p[index].virt
596 + (offset & ~PAGE_MASK),
4774817f
MD
597 src, bytes_left_in_page);
598 len -= bytes_left_in_page;
599 src += bytes_left_in_page;
600 offset += bytes_left_in_page;
601 } while (unlikely(len));
f3bc08c5
MD
602}
603EXPORT_SYMBOL_GPL(_lib_ring_buffer_write);
604
4ea00e4f
JD
605/**
606 * lib_ring_buffer_memset - write len bytes of c to a ring_buffer buffer.
607 * @bufb : buffer backend
608 * @offset : offset within the buffer
609 * @c : the byte to write
610 * @len : length to write
4ea00e4f 611 */
e20c0fec 612void _lib_ring_buffer_memset(struct lttng_kernel_ring_buffer_backend *bufb,
4774817f 613 size_t offset, int c, size_t len)
4ea00e4f
JD
614{
615 struct channel_backend *chanb = &bufb->chan->backend;
e20c0fec 616 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
4774817f 617 size_t sbidx, index, bytes_left_in_page;
e20c0fec 618 struct lttng_kernel_ring_buffer_backend_pages *rpages;
4ea00e4f
JD
619 unsigned long sb_bindex, id;
620
621 do {
4ea00e4f
JD
622 sbidx = offset >> chanb->subbuf_size_order;
623 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
624
625 /*
626 * Underlying layer should never ask for writes across
627 * subbuffers.
628 */
629 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
630
4774817f 631 bytes_left_in_page = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
4ea00e4f
JD
632 id = bufb->buf_wsb[sbidx].id;
633 sb_bindex = subbuffer_id_get_index(config, id);
634 rpages = bufb->array[sb_bindex];
635 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
636 && subbuffer_id_is_noref(config, id));
637 lib_ring_buffer_do_memset(rpages->p[index].virt
638 + (offset & ~PAGE_MASK),
4774817f
MD
639 c, bytes_left_in_page);
640 len -= bytes_left_in_page;
641 offset += bytes_left_in_page;
642 } while (unlikely(len));
4ea00e4f
JD
643}
644EXPORT_SYMBOL_GPL(_lib_ring_buffer_memset);
645
16f78f3a
MD
646/**
647 * lib_ring_buffer_strcpy - write string data to a ring_buffer buffer.
648 * @bufb : buffer backend
649 * @offset : offset within the buffer
650 * @src : source address
651 * @len : length to write
16f78f3a
MD
652 * @pad : character to use for padding
653 */
e20c0fec 654void _lib_ring_buffer_strcpy(struct lttng_kernel_ring_buffer_backend *bufb,
e5f1eb9a 655 size_t offset, const char *src, size_t len, int pad)
16f78f3a
MD
656{
657 struct channel_backend *chanb = &bufb->chan->backend;
e20c0fec 658 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
e5f1eb9a 659 size_t sbidx, index, bytes_left_in_page;
e20c0fec 660 struct lttng_kernel_ring_buffer_backend_pages *rpages;
16f78f3a 661 unsigned long sb_bindex, id;
e5f1eb9a 662 bool src_terminated = false;
16f78f3a
MD
663
664 CHAN_WARN_ON(chanb, !len);
16f78f3a 665 do {
16f78f3a
MD
666 sbidx = offset >> chanb->subbuf_size_order;
667 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
668
669 /*
670 * Underlying layer should never ask for writes across
671 * subbuffers.
672 */
673 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
674
e5f1eb9a 675 bytes_left_in_page = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
16f78f3a
MD
676 id = bufb->buf_wsb[sbidx].id;
677 sb_bindex = subbuffer_id_get_index(config, id);
678 rpages = bufb->array[sb_bindex];
679 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
680 && subbuffer_id_is_noref(config, id));
681
682 if (likely(!src_terminated)) {
683 size_t count, to_copy;
684
e5f1eb9a
MD
685 to_copy = bytes_left_in_page;
686 if (bytes_left_in_page == len)
16f78f3a
MD
687 to_copy--; /* Final '\0' */
688 count = lib_ring_buffer_do_strcpy(config,
689 rpages->p[index].virt
690 + (offset & ~PAGE_MASK),
691 src, to_copy);
692 offset += count;
693 /* Padding */
694 if (unlikely(count < to_copy)) {
695 size_t pad_len = to_copy - count;
696
697 /* Next pages will have padding */
e5f1eb9a 698 src_terminated = true;
16f78f3a
MD
699 lib_ring_buffer_do_memset(rpages->p[index].virt
700 + (offset & ~PAGE_MASK),
701 pad, pad_len);
702 offset += pad_len;
703 }
704 } else {
705 size_t pad_len;
706
e5f1eb9a
MD
707 pad_len = bytes_left_in_page;
708 if (bytes_left_in_page == len)
16f78f3a
MD
709 pad_len--; /* Final '\0' */
710 lib_ring_buffer_do_memset(rpages->p[index].virt
711 + (offset & ~PAGE_MASK),
712 pad, pad_len);
713 offset += pad_len;
714 }
e5f1eb9a
MD
715 len -= bytes_left_in_page;
716 if (!src_terminated)
717 src += bytes_left_in_page;
718 } while (unlikely(len));
719
16f78f3a
MD
720 /* Ending '\0' */
721 lib_ring_buffer_do_memset(rpages->p[index].virt + (offset & ~PAGE_MASK),
722 '\0', 1);
723}
724EXPORT_SYMBOL_GPL(_lib_ring_buffer_strcpy);
4ea00e4f 725
0d1080f4
MD
726/**
727 * _lib_ring_buffer_pstrcpy - write to a buffer backend P-string
728 * @bufb : buffer backend
729 * @src : source pointer to copy from
730 * @len : length of data to copy
731 * @pad : character to use for padding
732 *
733 * This function copies up to @len bytes of data from a source pointer
734 * to a Pascal String into the buffer backend. If a terminating '\0'
735 * character is found in @src before @len characters are copied, pad the
736 * buffer with @pad characters (e.g. '\0').
737 *
738 * The length of the pascal strings in the ring buffer is explicit: it
739 * is either the array or sequence length.
740 */
741void _lib_ring_buffer_pstrcpy(struct lttng_kernel_ring_buffer_backend *bufb,
742 size_t offset, const char *src, size_t len, int pad)
743{
744 struct channel_backend *chanb = &bufb->chan->backend;
745 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
746 size_t sbidx, index, bytes_left_in_page;
747 struct lttng_kernel_ring_buffer_backend_pages *rpages;
748 unsigned long sb_bindex, id;
749 bool src_terminated = false;
750
751 CHAN_WARN_ON(chanb, !len);
752 do {
753 sbidx = offset >> chanb->subbuf_size_order;
754 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
755
756 /*
757 * Underlying layer should never ask for writes across
758 * subbuffers.
759 */
760 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
761
762 bytes_left_in_page = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
763 id = bufb->buf_wsb[sbidx].id;
764 sb_bindex = subbuffer_id_get_index(config, id);
765 rpages = bufb->array[sb_bindex];
766 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
767 && subbuffer_id_is_noref(config, id));
768
769 if (likely(!src_terminated)) {
770 size_t count, to_copy;
771
772 to_copy = bytes_left_in_page;
773 count = lib_ring_buffer_do_strcpy(config,
774 rpages->p[index].virt
775 + (offset & ~PAGE_MASK),
776 src, to_copy);
777 offset += count;
778 /* Padding */
779 if (unlikely(count < to_copy)) {
780 size_t pad_len = to_copy - count;
781
782 /* Next pages will have padding */
783 src_terminated = true;
784 lib_ring_buffer_do_memset(rpages->p[index].virt
785 + (offset & ~PAGE_MASK),
786 pad, pad_len);
787 offset += pad_len;
788 }
789 } else {
790 size_t pad_len;
791
792 pad_len = bytes_left_in_page;
793 lib_ring_buffer_do_memset(rpages->p[index].virt
794 + (offset & ~PAGE_MASK),
795 pad, pad_len);
796 offset += pad_len;
797 }
798 len -= bytes_left_in_page;
799 if (!src_terminated)
800 src += bytes_left_in_page;
801 } while (unlikely(len));
802}
803EXPORT_SYMBOL_GPL(_lib_ring_buffer_pstrcpy);
804
4ea00e4f 805/**
7b8ea3a5 806 * lib_ring_buffer_copy_from_user_inatomic - write user data to a ring_buffer buffer.
4ea00e4f
JD
807 * @bufb : buffer backend
808 * @offset : offset within the buffer
809 * @src : source address
810 * @len : length to write
4ea00e4f
JD
811 *
812 * This function deals with userspace pointers, it should never be called
813 * directly without having the src pointer checked with access_ok()
814 * previously.
815 */
e20c0fec 816void _lib_ring_buffer_copy_from_user_inatomic(struct lttng_kernel_ring_buffer_backend *bufb,
4774817f 817 size_t offset, const void __user *src, size_t len)
4ea00e4f
JD
818{
819 struct channel_backend *chanb = &bufb->chan->backend;
e20c0fec 820 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
4774817f 821 size_t sbidx, index, bytes_left_in_page;
e20c0fec 822 struct lttng_kernel_ring_buffer_backend_pages *rpages;
4ea00e4f
JD
823 unsigned long sb_bindex, id;
824 int ret;
825
826 do {
4ea00e4f
JD
827 sbidx = offset >> chanb->subbuf_size_order;
828 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
829
830 /*
831 * Underlying layer should never ask for writes across
832 * subbuffers.
833 */
834 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
835
4774817f 836 bytes_left_in_page = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
4ea00e4f
JD
837 id = bufb->buf_wsb[sbidx].id;
838 sb_bindex = subbuffer_id_get_index(config, id);
839 rpages = bufb->array[sb_bindex];
840 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
841 && subbuffer_id_is_noref(config, id));
7b8ea3a5 842 ret = lib_ring_buffer_do_copy_from_user_inatomic(rpages->p[index].virt
4ea00e4f 843 + (offset & ~PAGE_MASK),
4774817f 844 src, bytes_left_in_page) != 0;
4ea00e4f 845 if (ret > 0) {
d87a9f03 846 /* Copy failed. */
4774817f 847 _lib_ring_buffer_memset(bufb, offset, 0, len);
4ea00e4f
JD
848 break; /* stop copy */
849 }
4774817f
MD
850 len -= bytes_left_in_page;
851 src += bytes_left_in_page;
852 offset += bytes_left_in_page;
853 } while (unlikely(len));
4ea00e4f 854}
7b8ea3a5 855EXPORT_SYMBOL_GPL(_lib_ring_buffer_copy_from_user_inatomic);
4ea00e4f 856
16f78f3a
MD
857/**
858 * lib_ring_buffer_strcpy_from_user_inatomic - write userspace string data to a ring_buffer buffer.
859 * @bufb : buffer backend
860 * @offset : offset within the buffer
861 * @src : source address
862 * @len : length to write
16f78f3a
MD
863 * @pad : character to use for padding
864 *
865 * This function deals with userspace pointers, it should never be called
866 * directly without having the src pointer checked with access_ok()
867 * previously.
868 */
e20c0fec 869void _lib_ring_buffer_strcpy_from_user_inatomic(struct lttng_kernel_ring_buffer_backend *bufb,
e5f1eb9a 870 size_t offset, const char __user *src, size_t len, int pad)
16f78f3a
MD
871{
872 struct channel_backend *chanb = &bufb->chan->backend;
e20c0fec 873 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
e5f1eb9a 874 size_t sbidx, index, bytes_left_in_page;
e20c0fec 875 struct lttng_kernel_ring_buffer_backend_pages *rpages;
16f78f3a 876 unsigned long sb_bindex, id;
e5f1eb9a 877 bool src_terminated = false;
16f78f3a 878
16f78f3a 879 do {
16f78f3a
MD
880 sbidx = offset >> chanb->subbuf_size_order;
881 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
882
883 /*
884 * Underlying layer should never ask for writes across
885 * subbuffers.
886 */
887 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
888
e5f1eb9a 889 bytes_left_in_page = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
16f78f3a
MD
890 id = bufb->buf_wsb[sbidx].id;
891 sb_bindex = subbuffer_id_get_index(config, id);
892 rpages = bufb->array[sb_bindex];
893 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
894 && subbuffer_id_is_noref(config, id));
895
896 if (likely(!src_terminated)) {
897 size_t count, to_copy;
898
e5f1eb9a
MD
899 to_copy = bytes_left_in_page;
900 if (bytes_left_in_page == len)
16f78f3a
MD
901 to_copy--; /* Final '\0' */
902 count = lib_ring_buffer_do_strcpy_from_user_inatomic(config,
903 rpages->p[index].virt
904 + (offset & ~PAGE_MASK),
905 src, to_copy);
906 offset += count;
907 /* Padding */
908 if (unlikely(count < to_copy)) {
909 size_t pad_len = to_copy - count;
910
911 /* Next pages will have padding */
e5f1eb9a 912 src_terminated = true;
16f78f3a
MD
913 lib_ring_buffer_do_memset(rpages->p[index].virt
914 + (offset & ~PAGE_MASK),
915 pad, pad_len);
916 offset += pad_len;
917 }
918 } else {
919 size_t pad_len;
920
e5f1eb9a
MD
921 pad_len = bytes_left_in_page;
922 if (bytes_left_in_page == len)
16f78f3a
MD
923 pad_len--; /* Final '\0' */
924 lib_ring_buffer_do_memset(rpages->p[index].virt
925 + (offset & ~PAGE_MASK),
926 pad, pad_len);
927 offset += pad_len;
928 }
e5f1eb9a
MD
929 len -= bytes_left_in_page;
930 if (!src_terminated)
931 src += bytes_left_in_page;
932 } while (unlikely(len));
933
16f78f3a
MD
934 /* Ending '\0' */
935 lib_ring_buffer_do_memset(rpages->p[index].virt + (offset & ~PAGE_MASK),
936 '\0', 1);
937}
938EXPORT_SYMBOL_GPL(_lib_ring_buffer_strcpy_from_user_inatomic);
939
0d1080f4
MD
940/**
941 * _lib_ring_buffer_pstrcpy_from_user_inatomic - write userspace string to a buffer backend P-string
942 * @bufb : buffer backend
943 * @src : source pointer to copy from
944 * @len : length of data to copy
945 * @pad : character to use for padding
946 *
947 * This function copies up to @len bytes of data from a source pointer
948 * to a Pascal String into the buffer backend. If a terminating '\0'
949 * character is found in @src before @len characters are copied, pad the
950 * buffer with @pad characters (e.g. '\0').
951 *
952 * The length of the pascal strings in the ring buffer is explicit: it
953 * is either the array or sequence length.
954 *
955 * This function deals with userspace pointers, it should never be called
956 * directly without having the src pointer checked with access_ok()
957 * previously.
958 */
959void _lib_ring_buffer_pstrcpy_from_user_inatomic(struct lttng_kernel_ring_buffer_backend *bufb,
960 size_t offset, const char __user *src, size_t len, int pad)
961{
962 struct channel_backend *chanb = &bufb->chan->backend;
963 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
964 size_t sbidx, index, bytes_left_in_page;
965 struct lttng_kernel_ring_buffer_backend_pages *rpages;
966 unsigned long sb_bindex, id;
967 bool src_terminated = false;
968
969 CHAN_WARN_ON(chanb, !len);
970 do {
971 sbidx = offset >> chanb->subbuf_size_order;
972 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
973
974 /*
975 * Underlying layer should never ask for writes across
976 * subbuffers.
977 */
978 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
979
980 bytes_left_in_page = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
981 id = bufb->buf_wsb[sbidx].id;
982 sb_bindex = subbuffer_id_get_index(config, id);
983 rpages = bufb->array[sb_bindex];
984 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
985 && subbuffer_id_is_noref(config, id));
986
987 if (likely(!src_terminated)) {
988 size_t count, to_copy;
989
990 to_copy = bytes_left_in_page;
991 count = lib_ring_buffer_do_strcpy_from_user_inatomic(config,
992 rpages->p[index].virt
993 + (offset & ~PAGE_MASK),
994 src, to_copy);
995 offset += count;
996 /* Padding */
997 if (unlikely(count < to_copy)) {
998 size_t pad_len = to_copy - count;
999
1000 /* Next pages will have padding */
1001 src_terminated = true;
1002 lib_ring_buffer_do_memset(rpages->p[index].virt
1003 + (offset & ~PAGE_MASK),
1004 pad, pad_len);
1005 offset += pad_len;
1006 }
1007 } else {
1008 size_t pad_len;
1009
1010 pad_len = bytes_left_in_page;
1011 lib_ring_buffer_do_memset(rpages->p[index].virt
1012 + (offset & ~PAGE_MASK),
1013 pad, pad_len);
1014 offset += pad_len;
1015 }
1016 len -= bytes_left_in_page;
1017 if (!src_terminated)
1018 src += bytes_left_in_page;
1019 } while (unlikely(len));
1020}
1021EXPORT_SYMBOL_GPL(_lib_ring_buffer_pstrcpy_from_user_inatomic);
1022
f3bc08c5
MD
1023/**
1024 * lib_ring_buffer_read - read data from ring_buffer_buffer.
1025 * @bufb : buffer backend
1026 * @offset : offset within the buffer
1027 * @dest : destination address
1028 * @len : length to copy to destination
1029 *
1030 * Should be protected by get_subbuf/put_subbuf.
1031 * Returns the length copied.
1032 */
e20c0fec 1033size_t lib_ring_buffer_read(struct lttng_kernel_ring_buffer_backend *bufb, size_t offset,
f3bc08c5
MD
1034 void *dest, size_t len)
1035{
1036 struct channel_backend *chanb = &bufb->chan->backend;
e20c0fec 1037 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
4774817f 1038 size_t index, bytes_left_in_page, orig_len;
e20c0fec 1039 struct lttng_kernel_ring_buffer_backend_pages *rpages;
f3bc08c5
MD
1040 unsigned long sb_bindex, id;
1041
1042 orig_len = len;
1043 offset &= chanb->buf_size - 1;
1044 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
1045 if (unlikely(!len))
1046 return 0;
1047 for (;;) {
4774817f 1048 bytes_left_in_page = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
f3bc08c5
MD
1049 id = bufb->buf_rsb.id;
1050 sb_bindex = subbuffer_id_get_index(config, id);
1051 rpages = bufb->array[sb_bindex];
1052 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
1053 && subbuffer_id_is_noref(config, id));
1054 memcpy(dest, rpages->p[index].virt + (offset & ~PAGE_MASK),
4774817f
MD
1055 bytes_left_in_page);
1056 len -= bytes_left_in_page;
f3bc08c5
MD
1057 if (likely(!len))
1058 break;
4774817f
MD
1059 dest += bytes_left_in_page;
1060 offset += bytes_left_in_page;
f3bc08c5
MD
1061 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
1062 /*
1063 * Underlying layer should never ask for reads across
1064 * subbuffers.
1065 */
1066 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
1067 }
1068 return orig_len;
1069}
1070EXPORT_SYMBOL_GPL(lib_ring_buffer_read);
1071
1072/**
1073 * __lib_ring_buffer_copy_to_user - read data from ring_buffer to userspace
1074 * @bufb : buffer backend
1075 * @offset : offset within the buffer
1076 * @dest : destination userspace address
1077 * @len : length to copy to destination
1078 *
1079 * Should be protected by get_subbuf/put_subbuf.
1080 * access_ok() must have been performed on dest addresses prior to call this
1081 * function.
1082 * Returns -EFAULT on error, 0 if ok.
1083 */
e20c0fec 1084int __lib_ring_buffer_copy_to_user(struct lttng_kernel_ring_buffer_backend *bufb,
f3bc08c5
MD
1085 size_t offset, void __user *dest, size_t len)
1086{
1087 struct channel_backend *chanb = &bufb->chan->backend;
e20c0fec 1088 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
f3bc08c5 1089 size_t index;
4774817f 1090 ssize_t bytes_left_in_page;
e20c0fec 1091 struct lttng_kernel_ring_buffer_backend_pages *rpages;
f3bc08c5
MD
1092 unsigned long sb_bindex, id;
1093
f3bc08c5
MD
1094 offset &= chanb->buf_size - 1;
1095 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
1096 if (unlikely(!len))
1097 return 0;
1098 for (;;) {
4774817f 1099 bytes_left_in_page = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
f3bc08c5
MD
1100 id = bufb->buf_rsb.id;
1101 sb_bindex = subbuffer_id_get_index(config, id);
1102 rpages = bufb->array[sb_bindex];
1103 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
1104 && subbuffer_id_is_noref(config, id));
1105 if (__copy_to_user(dest,
1106 rpages->p[index].virt + (offset & ~PAGE_MASK),
4774817f 1107 bytes_left_in_page))
f3bc08c5 1108 return -EFAULT;
4774817f 1109 len -= bytes_left_in_page;
f3bc08c5
MD
1110 if (likely(!len))
1111 break;
4774817f
MD
1112 dest += bytes_left_in_page;
1113 offset += bytes_left_in_page;
f3bc08c5
MD
1114 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
1115 /*
1116 * Underlying layer should never ask for reads across
1117 * subbuffers.
1118 */
1119 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
1120 }
1121 return 0;
1122}
1123EXPORT_SYMBOL_GPL(__lib_ring_buffer_copy_to_user);
1124
1125/**
1126 * lib_ring_buffer_read_cstr - read a C-style string from ring_buffer.
1127 * @bufb : buffer backend
1128 * @offset : offset within the buffer
1129 * @dest : destination address
1130 * @len : destination's length
1131 *
61eb4c39 1132 * Return string's length, or -EINVAL on error.
f3bc08c5 1133 * Should be protected by get_subbuf/put_subbuf.
61eb4c39 1134 * Destination length should be at least 1 to hold '\0'.
f3bc08c5 1135 */
e20c0fec 1136int lib_ring_buffer_read_cstr(struct lttng_kernel_ring_buffer_backend *bufb, size_t offset,
f3bc08c5
MD
1137 void *dest, size_t len)
1138{
1139 struct channel_backend *chanb = &bufb->chan->backend;
e20c0fec 1140 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
f3bc08c5 1141 size_t index;
4774817f 1142 ssize_t bytes_left_in_page, pagelen, strpagelen, orig_offset;
f3bc08c5 1143 char *str;
e20c0fec 1144 struct lttng_kernel_ring_buffer_backend_pages *rpages;
f3bc08c5
MD
1145 unsigned long sb_bindex, id;
1146
1147 offset &= chanb->buf_size - 1;
1148 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
1149 orig_offset = offset;
61eb4c39
MD
1150 if (unlikely(!len))
1151 return -EINVAL;
f3bc08c5
MD
1152 for (;;) {
1153 id = bufb->buf_rsb.id;
1154 sb_bindex = subbuffer_id_get_index(config, id);
1155 rpages = bufb->array[sb_bindex];
1156 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
1157 && subbuffer_id_is_noref(config, id));
1158 str = (char *)rpages->p[index].virt + (offset & ~PAGE_MASK);
1159 pagelen = PAGE_SIZE - (offset & ~PAGE_MASK);
1160 strpagelen = strnlen(str, pagelen);
1161 if (len) {
4774817f 1162 bytes_left_in_page = min_t(size_t, len, strpagelen);
f3bc08c5 1163 if (dest) {
4774817f
MD
1164 memcpy(dest, str, bytes_left_in_page);
1165 dest += bytes_left_in_page;
f3bc08c5 1166 }
4774817f 1167 len -= bytes_left_in_page;
f3bc08c5
MD
1168 }
1169 offset += strpagelen;
1170 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
1171 if (strpagelen < pagelen)
1172 break;
1173 /*
1174 * Underlying layer should never ask for reads across
1175 * subbuffers.
1176 */
1177 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
1178 }
1179 if (dest && len)
1180 ((char *)dest)[0] = 0;
1181 return offset - orig_offset;
1182}
1183EXPORT_SYMBOL_GPL(lib_ring_buffer_read_cstr);
1184
1185/**
0112cb7b 1186 * lib_ring_buffer_read_get_pfn - Get a page frame number to read from
f3bc08c5
MD
1187 * @bufb : buffer backend
1188 * @offset : offset within the buffer
1189 * @virt : pointer to page address (output)
1190 *
1191 * Should be protected by get_subbuf/put_subbuf.
0112cb7b 1192 * Returns the pointer to the page frame number unsigned long.
f3bc08c5 1193 */
e20c0fec 1194unsigned long *lib_ring_buffer_read_get_pfn(struct lttng_kernel_ring_buffer_backend *bufb,
f3bc08c5
MD
1195 size_t offset, void ***virt)
1196{
1197 size_t index;
e20c0fec 1198 struct lttng_kernel_ring_buffer_backend_pages *rpages;
f3bc08c5 1199 struct channel_backend *chanb = &bufb->chan->backend;
e20c0fec 1200 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
f3bc08c5
MD
1201 unsigned long sb_bindex, id;
1202
1203 offset &= chanb->buf_size - 1;
1204 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
1205 id = bufb->buf_rsb.id;
1206 sb_bindex = subbuffer_id_get_index(config, id);
1207 rpages = bufb->array[sb_bindex];
1208 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
1209 && subbuffer_id_is_noref(config, id));
1210 *virt = &rpages->p[index].virt;
0112cb7b 1211 return &rpages->p[index].pfn;
f3bc08c5 1212}
0112cb7b 1213EXPORT_SYMBOL_GPL(lib_ring_buffer_read_get_pfn);
f3bc08c5
MD
1214
1215/**
1216 * lib_ring_buffer_read_offset_address - get address of a buffer location
1217 * @bufb : buffer backend
1218 * @offset : offset within the buffer.
1219 *
1220 * Return the address where a given offset is located (for read).
1221 * Should be used to get the current subbuffer header pointer. Given we know
759d02c1
MD
1222 * it's never on a page boundary, it's safe to read/write directly
1223 * from/to this address, as long as the read/write is never bigger than a
1224 * page size.
f3bc08c5 1225 */
e20c0fec 1226void *lib_ring_buffer_read_offset_address(struct lttng_kernel_ring_buffer_backend *bufb,
f3bc08c5
MD
1227 size_t offset)
1228{
1229 size_t index;
e20c0fec 1230 struct lttng_kernel_ring_buffer_backend_pages *rpages;
f3bc08c5 1231 struct channel_backend *chanb = &bufb->chan->backend;
e20c0fec 1232 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
f3bc08c5
MD
1233 unsigned long sb_bindex, id;
1234
1235 offset &= chanb->buf_size - 1;
1236 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
1237 id = bufb->buf_rsb.id;
1238 sb_bindex = subbuffer_id_get_index(config, id);
1239 rpages = bufb->array[sb_bindex];
1240 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
1241 && subbuffer_id_is_noref(config, id));
1242 return rpages->p[index].virt + (offset & ~PAGE_MASK);
1243}
1244EXPORT_SYMBOL_GPL(lib_ring_buffer_read_offset_address);
1245
1246/**
1247 * lib_ring_buffer_offset_address - get address of a location within the buffer
1248 * @bufb : buffer backend
1249 * @offset : offset within the buffer.
1250 *
1251 * Return the address where a given offset is located.
1252 * Should be used to get the current subbuffer header pointer. Given we know
1253 * it's always at the beginning of a page, it's safe to write directly to this
1254 * address, as long as the write is never bigger than a page size.
1255 */
e20c0fec 1256void *lib_ring_buffer_offset_address(struct lttng_kernel_ring_buffer_backend *bufb,
f3bc08c5
MD
1257 size_t offset)
1258{
1259 size_t sbidx, index;
e20c0fec 1260 struct lttng_kernel_ring_buffer_backend_pages *rpages;
f3bc08c5 1261 struct channel_backend *chanb = &bufb->chan->backend;
e20c0fec 1262 const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
f3bc08c5
MD
1263 unsigned long sb_bindex, id;
1264
1265 offset &= chanb->buf_size - 1;
1266 sbidx = offset >> chanb->subbuf_size_order;
1267 index = (offset & (chanb->subbuf_size - 1)) >> PAGE_SHIFT;
1268 id = bufb->buf_wsb[sbidx].id;
1269 sb_bindex = subbuffer_id_get_index(config, id);
1270 rpages = bufb->array[sb_bindex];
1271 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
1272 && subbuffer_id_is_noref(config, id));
1273 return rpages->p[index].virt + (offset & ~PAGE_MASK);
1274}
1275EXPORT_SYMBOL_GPL(lib_ring_buffer_offset_address);
This page took 0.181942 seconds and 4 git commands to generate.