-
Notifications
You must be signed in to change notification settings - Fork 69
/
ring_buffer_frontend.c
2436 lines (2180 loc) · 77.1 KB
/
ring_buffer_frontend.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
*
* ring_buffer_frontend.c
*
* Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
*
* Ring buffer wait-free buffer synchronization. Producer-consumer and flight
* recorder (overwrite) modes. See thesis:
*
* Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
* dissertation, Ecole Polytechnique de Montreal.
* http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
*
* - Algorithm presentation in Chapter 5:
* "Lockless Multi-Core High-Throughput Buffering".
* - Algorithm formal verification in Section 8.6:
* "Formal verification of LTTng"
*
* Author:
* Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
*
* Inspired from LTT and RelayFS:
* Karim Yaghmour <karim@opersys.com>
* Tom Zanussi <zanussi@us.ibm.com>
* Bob Wisniewski <bob@watson.ibm.com>
* And from K42 :
* Bob Wisniewski <bob@watson.ibm.com>
*
* Buffer reader semantic :
*
* - get_subbuf_size
* while buffer is not finalized and empty
* - get_subbuf
* - if return value != 0, continue
* - splice one subbuffer worth of data to a pipe
* - splice the data from pipe to disk/network
* - put_subbuf
*/
#include <linux/atomic.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/percpu.h>
#include <linux/percpu-defs.h>
#include <asm/cacheflush.h>
#include <ringbuffer/config.h>
#include <ringbuffer/backend.h>
#include <ringbuffer/frontend.h>
#include <ringbuffer/iterator.h>
#include <ringbuffer/nohz.h>
#include <wrapper/cpu.h>
#include <wrapper/kref.h>
#include <wrapper/timer.h>
#include <wrapper/vmalloc.h>
/*
* Internal structure representing offsets to use at a sub-buffer switch.
*/
struct switch_offsets {
unsigned long begin, end, old;
size_t pre_header_padding, size;
unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1,
switch_old_end:1;
};
#ifdef CONFIG_NO_HZ
enum tick_nohz_val {
TICK_NOHZ_STOP,
TICK_NOHZ_FLUSH,
TICK_NOHZ_RESTART,
};
static ATOMIC_NOTIFIER_HEAD(tick_nohz_notifier);
#endif /* CONFIG_NO_HZ */
static DEFINE_PER_CPU(spinlock_t, ring_buffer_nohz_lock);
DEFINE_PER_CPU(unsigned int, lib_ring_buffer_nesting);
EXPORT_PER_CPU_SYMBOL(lib_ring_buffer_nesting);
static
void lib_ring_buffer_print_errors(struct lttng_kernel_ring_buffer_channel *chan,
struct lttng_kernel_ring_buffer *buf, int cpu);
static
void _lib_ring_buffer_switch_remote(struct lttng_kernel_ring_buffer *buf,
enum switch_mode mode);
static
int lib_ring_buffer_poll_deliver(const struct lttng_kernel_ring_buffer_config *config,
struct lttng_kernel_ring_buffer *buf,
struct lttng_kernel_ring_buffer_channel *chan)
{
unsigned long consumed_old, consumed_idx, commit_count, write_offset;
consumed_old = atomic_long_read(&buf->consumed);
consumed_idx = subbuf_index(consumed_old, chan);
commit_count = v_read(config, &buf->commit_cold[consumed_idx].cc_sb);
/*
* No memory barrier here, since we are only interested
* in a statistically correct polling result. The next poll will
* get the data is we are racing. The mb() that ensures correct
* memory order is in get_subbuf.
*/
write_offset = v_read(config, &buf->offset);
/*
* Check that the subbuffer we are trying to consume has been
* already fully committed.
*/
if (((commit_count - chan->backend.subbuf_size)
& chan->commit_count_mask)
- (buf_trunc(consumed_old, chan)
>> chan->backend.num_subbuf_order)
!= 0)
return 0;
/*
* Check that we are not about to read the same subbuffer in
* which the writer head is.
*/
if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_old, chan)
== 0)
return 0;
return 1;
}
/*
* Must be called under cpu hotplug protection.
*/
void lib_ring_buffer_free(struct lttng_kernel_ring_buffer *buf)
{
struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
irq_work_sync(&buf->wakeup_pending);
lib_ring_buffer_print_errors(chan, buf, buf->backend.cpu);
lttng_kvfree(buf->commit_hot);
lttng_kvfree(buf->commit_cold);
lttng_kvfree(buf->ts_end);
lib_ring_buffer_backend_free(&buf->backend);
}
/**
* lib_ring_buffer_reset - Reset ring buffer to initial values.
* @buf: Ring buffer.
*
* Effectively empty the ring buffer. Should be called when the buffer is not
* used for writing. The ring buffer can be opened for reading, but the reader
* should not be using the iterator concurrently with reset. The previous
* current iterator record is reset.
*/
void lib_ring_buffer_reset(struct lttng_kernel_ring_buffer *buf)
{
struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
unsigned int i;
/*
* Reset iterator first. It will put the subbuffer if it currently holds
* it.
*/
lib_ring_buffer_iterator_reset(buf);
v_set(config, &buf->offset, 0);
for (i = 0; i < chan->backend.num_subbuf; i++) {
v_set(config, &buf->commit_hot[i].cc, 0);
v_set(config, &buf->commit_hot[i].seq, 0);
v_set(config, &buf->commit_cold[i].cc_sb, 0);
buf->ts_end[i] = 0;
}
atomic_long_set(&buf->consumed, 0);
atomic_set(&buf->record_disabled, 0);
v_set(config, &buf->last_timestamp, 0);
lib_ring_buffer_backend_reset(&buf->backend);
/* Don't reset number of active readers */
v_set(config, &buf->records_lost_full, 0);
v_set(config, &buf->records_lost_wrap, 0);
v_set(config, &buf->records_lost_big, 0);
v_set(config, &buf->records_count, 0);
v_set(config, &buf->records_overrun, 0);
buf->finalized = 0;
}
EXPORT_SYMBOL_GPL(lib_ring_buffer_reset);
/**
* channel_reset - Reset channel to initial values.
* @chan: Channel.
*
* Effectively empty the channel. Should be called when the channel is not used
* for writing. The channel can be opened for reading, but the reader should not
* be using the iterator concurrently with reset. The previous current iterator
* record is reset.
*/
void channel_reset(struct lttng_kernel_ring_buffer_channel *chan)
{
/*
* Reset iterators first. Will put the subbuffer if held for reading.
*/
channel_iterator_reset(chan);
atomic_set(&chan->record_disabled, 0);
/* Don't reset commit_count_mask, still valid */
channel_backend_reset(&chan->backend);
/* Don't reset switch/read timer interval */
/* Don't reset notifiers and notifier enable bits */
/* Don't reset reader reference count */
}
EXPORT_SYMBOL_GPL(channel_reset);
static void lib_ring_buffer_pending_wakeup_buf(struct irq_work *entry)
{
struct lttng_kernel_ring_buffer *buf = container_of(entry, struct lttng_kernel_ring_buffer,
wakeup_pending);
wake_up_interruptible(&buf->read_wait);
}
static void lib_ring_buffer_pending_wakeup_chan(struct irq_work *entry)
{
struct lttng_kernel_ring_buffer_channel *chan = container_of(entry, struct lttng_kernel_ring_buffer_channel, wakeup_pending);
wake_up_interruptible(&chan->read_wait);
}
/*
* Must be called under cpu hotplug protection.
*/
int lib_ring_buffer_create(struct lttng_kernel_ring_buffer *buf,
struct channel_backend *chanb, int cpu)
{
const struct lttng_kernel_ring_buffer_config *config = &chanb->config;
struct lttng_kernel_ring_buffer_channel *chan = container_of(chanb, struct lttng_kernel_ring_buffer_channel, backend);
void *priv = chanb->priv;
size_t subbuf_header_size;
u64 timestamp;
int ret;
/* Test for cpu hotplug */
if (buf->backend.allocated)
return 0;
/*
* Paranoia: per cpu dynamic allocation is not officially documented as
* zeroing the memory, so let's do it here too, just in case.
*/
memset(buf, 0, sizeof(*buf));
ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend, cpu);
if (ret)
return ret;
buf->commit_hot =
lttng_kvzalloc_node(ALIGN(sizeof(*buf->commit_hot)
* chan->backend.num_subbuf,
1 << INTERNODE_CACHE_SHIFT),
GFP_KERNEL | __GFP_NOWARN,
cpu_to_node(max(cpu, 0)));
if (!buf->commit_hot) {
ret = -ENOMEM;
goto free_chanbuf;
}
buf->commit_cold =
lttng_kvzalloc_node(ALIGN(sizeof(*buf->commit_cold)
* chan->backend.num_subbuf,
1 << INTERNODE_CACHE_SHIFT),
GFP_KERNEL | __GFP_NOWARN,
cpu_to_node(max(cpu, 0)));
if (!buf->commit_cold) {
ret = -ENOMEM;
goto free_commit;
}
buf->ts_end =
lttng_kvzalloc_node(ALIGN(sizeof(*buf->ts_end)
* chan->backend.num_subbuf,
1 << INTERNODE_CACHE_SHIFT),
GFP_KERNEL | __GFP_NOWARN,
cpu_to_node(max(cpu, 0)));
if (!buf->ts_end) {
ret = -ENOMEM;
goto free_commit_cold;
}
init_waitqueue_head(&buf->read_wait);
init_waitqueue_head(&buf->write_wait);
init_irq_work(&buf->wakeup_pending, lib_ring_buffer_pending_wakeup_buf);
raw_spin_lock_init(&buf->raw_tick_nohz_spinlock);
/*
* Write the subbuffer header for first subbuffer so we know the total
* duration of data gathering.
*/
subbuf_header_size = config->cb.subbuffer_header_size();
v_set(config, &buf->offset, subbuf_header_size);
subbuffer_id_clear_noref(config, &buf->backend.buf_wsb[0].id);
timestamp = config->cb.ring_buffer_clock_read(buf->backend.chan);
config->cb.buffer_begin(buf, timestamp, 0);
v_add(config, subbuf_header_size, &buf->commit_hot[0].cc);
if (config->cb.buffer_create) {
ret = config->cb.buffer_create(buf, priv, cpu, chanb->name);
if (ret)
goto free_init;
}
/*
* Ensure the buffer is ready before setting it to allocated and setting
* the cpumask.
* Used for cpu hotplug vs cpumask iteration.
*/
smp_wmb();
buf->backend.allocated = 1;
if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
CHAN_WARN_ON(chan, cpumask_test_cpu(cpu,
chan->backend.cpumask));
cpumask_set_cpu(cpu, chan->backend.cpumask);
}
return 0;
/* Error handling */
free_init:
lttng_kvfree(buf->ts_end);
free_commit_cold:
lttng_kvfree(buf->commit_cold);
free_commit:
lttng_kvfree(buf->commit_hot);
free_chanbuf:
lib_ring_buffer_backend_free(&buf->backend);
return ret;
}
static void switch_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t)
{
struct lttng_kernel_ring_buffer *buf = lttng_from_timer(buf, t, switch_timer);
struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
/*
* Only flush buffers periodically if readers are active.
*/
if (atomic_long_read(&buf->active_readers))
lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
lttng_mod_timer_pinned(&buf->switch_timer,
jiffies + chan->switch_timer_interval);
else
mod_timer(&buf->switch_timer,
jiffies + chan->switch_timer_interval);
}
/*
* Called with ring_buffer_nohz_lock held for per-cpu buffers.
*/
static void lib_ring_buffer_start_switch_timer(struct lttng_kernel_ring_buffer *buf)
{
struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
unsigned int flags = 0;
if (!chan->switch_timer_interval || buf->switch_timer_enabled)
return;
if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
flags = LTTNG_TIMER_PINNED;
lttng_timer_setup(&buf->switch_timer, switch_buffer_timer, flags, buf);
buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
add_timer_on(&buf->switch_timer, buf->backend.cpu);
else
add_timer(&buf->switch_timer);
buf->switch_timer_enabled = 1;
}
/*
* Called with ring_buffer_nohz_lock held for per-cpu buffers.
*/
static void lib_ring_buffer_stop_switch_timer(struct lttng_kernel_ring_buffer *buf)
{
struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
if (!chan->switch_timer_interval || !buf->switch_timer_enabled)
return;
del_timer_sync(&buf->switch_timer);
buf->switch_timer_enabled = 0;
}
/*
* Polling timer to check the channels for data.
*/
static void read_buffer_timer(LTTNG_TIMER_FUNC_ARG_TYPE t)
{
struct lttng_kernel_ring_buffer *buf = lttng_from_timer(buf, t, read_timer);
struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
CHAN_WARN_ON(chan, !buf->backend.allocated);
if (atomic_long_read(&buf->active_readers)
&& lib_ring_buffer_poll_deliver(config, buf, chan)) {
wake_up_interruptible(&buf->read_wait);
wake_up_interruptible(&chan->read_wait);
}
if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
lttng_mod_timer_pinned(&buf->read_timer,
jiffies + chan->read_timer_interval);
else
mod_timer(&buf->read_timer,
jiffies + chan->read_timer_interval);
}
/*
* Called with ring_buffer_nohz_lock held for per-cpu buffers.
*/
static void lib_ring_buffer_start_read_timer(struct lttng_kernel_ring_buffer *buf)
{
struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
unsigned int flags = 0;
if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
|| !chan->read_timer_interval
|| buf->read_timer_enabled)
return;
if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
flags = LTTNG_TIMER_PINNED;
lttng_timer_setup(&buf->read_timer, read_buffer_timer, flags, buf);
buf->read_timer.expires = jiffies + chan->read_timer_interval;
if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
add_timer_on(&buf->read_timer, buf->backend.cpu);
else
add_timer(&buf->read_timer);
buf->read_timer_enabled = 1;
}
/*
* Called with ring_buffer_nohz_lock held for per-cpu buffers.
*/
static void lib_ring_buffer_stop_read_timer(struct lttng_kernel_ring_buffer *buf)
{
struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan;
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
|| !chan->read_timer_interval
|| !buf->read_timer_enabled)
return;
del_timer_sync(&buf->read_timer);
/*
* do one more check to catch data that has been written in the last
* timer period.
*/
if (lib_ring_buffer_poll_deliver(config, buf, chan)) {
wake_up_interruptible(&buf->read_wait);
wake_up_interruptible(&chan->read_wait);
}
buf->read_timer_enabled = 0;
}
#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
enum cpuhp_state lttng_rb_hp_prepare;
enum cpuhp_state lttng_rb_hp_online;
void lttng_rb_set_hp_prepare(enum cpuhp_state val)
{
lttng_rb_hp_prepare = val;
}
EXPORT_SYMBOL_GPL(lttng_rb_set_hp_prepare);
void lttng_rb_set_hp_online(enum cpuhp_state val)
{
lttng_rb_hp_online = val;
}
EXPORT_SYMBOL_GPL(lttng_rb_set_hp_online);
int lttng_cpuhp_rb_frontend_dead(unsigned int cpu,
struct lttng_cpuhp_node *node)
{
struct lttng_kernel_ring_buffer_channel *chan = container_of(node, struct lttng_kernel_ring_buffer_channel,
cpuhp_prepare);
struct lttng_kernel_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
/*
* Performing a buffer switch on a remote CPU. Performed by
* the CPU responsible for doing the hotunplug after the target
* CPU stopped running completely. Ensures that all data
* from that remote CPU is flushed.
*/
lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
return 0;
}
EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_dead);
int lttng_cpuhp_rb_frontend_online(unsigned int cpu,
struct lttng_cpuhp_node *node)
{
struct lttng_kernel_ring_buffer_channel *chan = container_of(node, struct lttng_kernel_ring_buffer_channel,
cpuhp_online);
struct lttng_kernel_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
wake_up_interruptible(&chan->hp_wait);
lib_ring_buffer_start_switch_timer(buf);
lib_ring_buffer_start_read_timer(buf);
return 0;
}
EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_online);
int lttng_cpuhp_rb_frontend_offline(unsigned int cpu,
struct lttng_cpuhp_node *node)
{
struct lttng_kernel_ring_buffer_channel *chan = container_of(node, struct lttng_kernel_ring_buffer_channel,
cpuhp_online);
struct lttng_kernel_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
lib_ring_buffer_stop_switch_timer(buf);
lib_ring_buffer_stop_read_timer(buf);
return 0;
}
EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_frontend_offline);
#else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
#ifdef CONFIG_HOTPLUG_CPU
/**
* lib_ring_buffer_cpu_hp_callback - CPU hotplug callback
* @nb: notifier block
* @action: hotplug action to take
* @hcpu: CPU number
*
* Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
*/
static
int lib_ring_buffer_cpu_hp_callback(struct notifier_block *nb,
unsigned long action,
void *hcpu)
{
unsigned int cpu = (unsigned long)hcpu;
struct lttng_kernel_ring_buffer_channel *chan = container_of(nb, struct lttng_kernel_ring_buffer_channel,
cpu_hp_notifier);
struct lttng_kernel_ring_buffer *buf = per_cpu_ptr(chan->backend.buf, cpu);
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
if (!chan->cpu_hp_enable)
return NOTIFY_DONE;
CHAN_WARN_ON(chan, config->alloc == RING_BUFFER_ALLOC_GLOBAL);
switch (action) {
case CPU_DOWN_FAILED:
case CPU_DOWN_FAILED_FROZEN:
case CPU_ONLINE:
case CPU_ONLINE_FROZEN:
wake_up_interruptible(&chan->hp_wait);
lib_ring_buffer_start_switch_timer(buf);
lib_ring_buffer_start_read_timer(buf);
return NOTIFY_OK;
case CPU_DOWN_PREPARE:
case CPU_DOWN_PREPARE_FROZEN:
lib_ring_buffer_stop_switch_timer(buf);
lib_ring_buffer_stop_read_timer(buf);
return NOTIFY_OK;
case CPU_DEAD:
case CPU_DEAD_FROZEN:
/*
* Performing a buffer switch on a remote CPU. Performed by
* the CPU responsible for doing the hotunplug after the target
* CPU stopped running completely. Ensures that all data
* from that remote CPU is flushed.
*/
lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
return NOTIFY_OK;
default:
return NOTIFY_DONE;
}
}
#endif
#endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
#if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
/*
* For per-cpu buffers, call the reader wakeups before switching the buffer, so
* that wake-up-tracing generated events are flushed before going idle (in
* tick_nohz). We test if the spinlock is locked to deal with the race where
* readers try to sample the ring buffer before we perform the switch. We let
* the readers retry in that case. If there is data in the buffer, the wake up
* is going to forbid the CPU running the reader thread from going idle.
*/
static int notrace ring_buffer_tick_nohz_callback(struct notifier_block *nb,
unsigned long val,
void *data)
{
struct lttng_kernel_ring_buffer_channel *chan = container_of(nb, struct lttng_kernel_ring_buffer_channel,
tick_nohz_notifier);
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
struct lttng_kernel_ring_buffer *buf;
int cpu = smp_processor_id();
if (config->alloc != RING_BUFFER_ALLOC_PER_CPU) {
/*
* We don't support keeping the system idle with global buffers
* and streaming active. In order to do so, we would need to
* sample a non-nohz-cpumask racelessly with the nohz updates
* without adding synchronization overhead to nohz. Leave this
* use-case out for now.
*/
return 0;
}
buf = channel_get_ring_buffer(config, chan, cpu);
switch (val) {
case TICK_NOHZ_FLUSH:
raw_spin_lock(&buf->raw_tick_nohz_spinlock);
if (config->wakeup == RING_BUFFER_WAKEUP_BY_TIMER
&& chan->read_timer_interval
&& atomic_long_read(&buf->active_readers)
&& (lib_ring_buffer_poll_deliver(config, buf, chan)
|| lib_ring_buffer_pending_data(config, buf, chan))) {
wake_up_interruptible(&buf->read_wait);
wake_up_interruptible(&chan->read_wait);
}
if (chan->switch_timer_interval)
lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
raw_spin_unlock(&buf->raw_tick_nohz_spinlock);
break;
case TICK_NOHZ_STOP:
spin_lock(this_cpu_ptr(&ring_buffer_nohz_lock));
lib_ring_buffer_stop_switch_timer(buf);
lib_ring_buffer_stop_read_timer(buf);
spin_unlock(this_cpu_ptr(&ring_buffer_nohz_lock));
break;
case TICK_NOHZ_RESTART:
spin_lock(this_cpu_ptr(&ring_buffer_nohz_lock));
lib_ring_buffer_start_read_timer(buf);
lib_ring_buffer_start_switch_timer(buf);
spin_unlock(this_cpu_ptr(&ring_buffer_nohz_lock));
break;
}
return 0;
}
void notrace lib_ring_buffer_tick_nohz_flush(void)
{
atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_FLUSH,
NULL);
}
void notrace lib_ring_buffer_tick_nohz_stop(void)
{
atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_STOP,
NULL);
}
void notrace lib_ring_buffer_tick_nohz_restart(void)
{
atomic_notifier_call_chain(&tick_nohz_notifier, TICK_NOHZ_RESTART,
NULL);
}
#endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
/*
* Holds CPU hotplug.
*/
static void channel_unregister_notifiers(struct lttng_kernel_ring_buffer_channel *chan)
{
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
channel_iterator_unregister_notifiers(chan);
if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
#ifdef CONFIG_NO_HZ
/*
* Remove the nohz notifier first, so we are certain we stop
* the timers.
*/
atomic_notifier_chain_unregister(&tick_nohz_notifier,
&chan->tick_nohz_notifier);
/*
* ring_buffer_nohz_lock will not be needed below, because
* we just removed the notifiers, which were the only source of
* concurrency.
*/
#endif /* CONFIG_NO_HZ */
#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
{
int ret;
ret = cpuhp_state_remove_instance(lttng_rb_hp_online,
&chan->cpuhp_online.node);
WARN_ON(ret);
ret = cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare,
&chan->cpuhp_prepare.node);
WARN_ON(ret);
}
#else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
{
int cpu;
#ifdef CONFIG_HOTPLUG_CPU
lttng_cpus_read_lock();
chan->cpu_hp_enable = 0;
for_each_online_cpu(cpu) {
struct lttng_kernel_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
cpu);
lib_ring_buffer_stop_switch_timer(buf);
lib_ring_buffer_stop_read_timer(buf);
}
lttng_cpus_read_unlock();
unregister_cpu_notifier(&chan->cpu_hp_notifier);
#else
for_each_possible_cpu(cpu) {
struct lttng_kernel_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
cpu);
lib_ring_buffer_stop_switch_timer(buf);
lib_ring_buffer_stop_read_timer(buf);
}
#endif
}
#endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
} else {
struct lttng_kernel_ring_buffer *buf = chan->backend.buf;
lib_ring_buffer_stop_switch_timer(buf);
lib_ring_buffer_stop_read_timer(buf);
}
channel_backend_unregister_notifiers(&chan->backend);
}
static void lib_ring_buffer_set_quiescent(struct lttng_kernel_ring_buffer *buf)
{
if (!buf->quiescent) {
buf->quiescent = true;
_lib_ring_buffer_switch_remote(buf, SWITCH_FLUSH);
}
}
static void lib_ring_buffer_clear_quiescent(struct lttng_kernel_ring_buffer *buf)
{
buf->quiescent = false;
}
void lib_ring_buffer_set_quiescent_channel(struct lttng_kernel_ring_buffer_channel *chan)
{
int cpu;
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
lttng_cpus_read_lock();
for_each_channel_cpu(cpu, chan) {
struct lttng_kernel_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
cpu);
lib_ring_buffer_set_quiescent(buf);
}
lttng_cpus_read_unlock();
} else {
struct lttng_kernel_ring_buffer *buf = chan->backend.buf;
lib_ring_buffer_set_quiescent(buf);
}
}
EXPORT_SYMBOL_GPL(lib_ring_buffer_set_quiescent_channel);
void lib_ring_buffer_clear_quiescent_channel(struct lttng_kernel_ring_buffer_channel *chan)
{
int cpu;
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
lttng_cpus_read_lock();
for_each_channel_cpu(cpu, chan) {
struct lttng_kernel_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
cpu);
lib_ring_buffer_clear_quiescent(buf);
}
lttng_cpus_read_unlock();
} else {
struct lttng_kernel_ring_buffer *buf = chan->backend.buf;
lib_ring_buffer_clear_quiescent(buf);
}
}
EXPORT_SYMBOL_GPL(lib_ring_buffer_clear_quiescent_channel);
static void channel_free(struct lttng_kernel_ring_buffer_channel *chan)
{
if (chan->backend.release_priv_ops) {
chan->backend.release_priv_ops(chan->backend.priv_ops);
}
channel_iterator_free(chan);
channel_backend_free(&chan->backend);
kfree(chan);
}
/**
* channel_create - Create channel.
* @config: ring buffer instance configuration
* @name: name of the channel
* @priv: ring buffer client private data
* @buf_addr: pointer the the beginning of the preallocated buffer contiguous
* address mapping. It is used only by RING_BUFFER_STATIC
* configuration. It can be set to NULL for other backends.
* @subbuf_size: subbuffer size
* @num_subbuf: number of subbuffers
* @switch_timer_interval: Time interval (in us) to fill sub-buffers with
* padding to let readers get those sub-buffers.
* Used for live streaming.
* @read_timer_interval: Time interval (in us) to wake up pending readers.
*
* Holds cpu hotplug.
* Returns NULL on failure.
*/
struct lttng_kernel_ring_buffer_channel *channel_create(const struct lttng_kernel_ring_buffer_config *config,
const char *name, void *priv, void *buf_addr,
size_t subbuf_size,
size_t num_subbuf, unsigned int switch_timer_interval,
unsigned int read_timer_interval)
{
int ret;
struct lttng_kernel_ring_buffer_channel *chan;
if (lib_ring_buffer_check_config(config, switch_timer_interval,
read_timer_interval))
return NULL;
chan = kzalloc(sizeof(struct lttng_kernel_ring_buffer_channel), GFP_KERNEL);
if (!chan)
return NULL;
ret = channel_backend_init(&chan->backend, name, config, priv,
subbuf_size, num_subbuf);
if (ret)
goto error;
ret = channel_iterator_init(chan);
if (ret)
goto error_free_backend;
chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order);
chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
kref_init(&chan->ref);
init_waitqueue_head(&chan->read_wait);
init_waitqueue_head(&chan->hp_wait);
init_irq_work(&chan->wakeup_pending, lib_ring_buffer_pending_wakeup_chan);
if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
chan->cpuhp_prepare.component = LTTNG_RING_BUFFER_FRONTEND;
ret = cpuhp_state_add_instance_nocalls(lttng_rb_hp_prepare,
&chan->cpuhp_prepare.node);
if (ret)
goto cpuhp_prepare_error;
chan->cpuhp_online.component = LTTNG_RING_BUFFER_FRONTEND;
ret = cpuhp_state_add_instance(lttng_rb_hp_online,
&chan->cpuhp_online.node);
if (ret)
goto cpuhp_online_error;
#else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
{
int cpu;
/*
* In case of non-hotplug cpu, if the ring-buffer is allocated
* in early initcall, it will not be notified of secondary cpus.
* In that off case, we need to allocate for all possible cpus.
*/
#ifdef CONFIG_HOTPLUG_CPU
chan->cpu_hp_notifier.notifier_call =
lib_ring_buffer_cpu_hp_callback;
chan->cpu_hp_notifier.priority = 6;
register_cpu_notifier(&chan->cpu_hp_notifier);
lttng_cpus_read_lock();
for_each_online_cpu(cpu) {
struct lttng_kernel_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
cpu);
spin_lock(&per_cpu(ring_buffer_nohz_lock, cpu));
lib_ring_buffer_start_switch_timer(buf);
lib_ring_buffer_start_read_timer(buf);
spin_unlock(&per_cpu(ring_buffer_nohz_lock, cpu));
}
chan->cpu_hp_enable = 1;
lttng_cpus_read_unlock();
#else
for_each_possible_cpu(cpu) {
struct lttng_kernel_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
cpu);
spin_lock(&per_cpu(ring_buffer_nohz_lock, cpu));
lib_ring_buffer_start_switch_timer(buf);
lib_ring_buffer_start_read_timer(buf);
spin_unlock(&per_cpu(ring_buffer_nohz_lock, cpu));
}
#endif
}
#endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
#if defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER)
/* Only benefit from NO_HZ idle with per-cpu buffers for now. */
chan->tick_nohz_notifier.notifier_call =
ring_buffer_tick_nohz_callback;
chan->tick_nohz_notifier.priority = ~0U;
atomic_notifier_chain_register(&tick_nohz_notifier,
&chan->tick_nohz_notifier);
#endif /* defined(CONFIG_NO_HZ) && defined(CONFIG_LIB_RING_BUFFER) */
} else {
struct lttng_kernel_ring_buffer *buf = chan->backend.buf;
lib_ring_buffer_start_switch_timer(buf);
lib_ring_buffer_start_read_timer(buf);
}
return chan;
#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
cpuhp_online_error:
ret = cpuhp_state_remove_instance_nocalls(lttng_rb_hp_prepare,
&chan->cpuhp_prepare.node);
WARN_ON(ret);
cpuhp_prepare_error:
#endif /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
error_free_backend:
channel_backend_free(&chan->backend);
error:
kfree(chan);
return NULL;
}
EXPORT_SYMBOL_GPL(channel_create);
static
void channel_release(struct kref *kref)
{
struct lttng_kernel_ring_buffer_channel *chan = container_of(kref, struct lttng_kernel_ring_buffer_channel, ref);
channel_free(chan);
}
/**
* channel_destroy - Finalize, wait for q.s. and destroy channel.
* @chan: channel to destroy
*
* Holds cpu hotplug.
* Call "destroy" callback, finalize channels, and then decrement the
* channel reference count. Note that when readers have completed data
* consumption of finalized channels, get_subbuf() will return -ENODATA.
* They should release their handle at that point. Returns the private
* data pointer.
*/
void *channel_destroy(struct lttng_kernel_ring_buffer_channel *chan)
{
int cpu;
const struct lttng_kernel_ring_buffer_config *config = &chan->backend.config;
void *priv;
irq_work_sync(&chan->wakeup_pending);
channel_unregister_notifiers(chan);
if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
/*
* No need to hold cpu hotplug, because all notifiers have been
* unregistered.
*/
for_each_channel_cpu(cpu, chan) {
struct lttng_kernel_ring_buffer *buf = per_cpu_ptr(chan->backend.buf,
cpu);
if (config->cb.buffer_finalize)
config->cb.buffer_finalize(buf,
chan->backend.priv,
cpu);