-
Notifications
You must be signed in to change notification settings - Fork 81
/
ras-events.c
1243 lines (1059 loc) · 29.5 KB
/
ras-events.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-or-later
/*
* Copyright (C) 2013 Mauro Carvalho Chehab <[email protected]>
*/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/poll.h>
#include <sys/signalfd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <traceevent/event-parse.h>
#include <traceevent/kbuffer.h>
#include <unistd.h>
#include "ras-aer-handler.h"
#include "ras-arm-handler.h"
#include "ras-cpu-isolation.h"
#include "ras-cxl-handler.h"
#include "ras-devlink-handler.h"
#include "ras-diskerror-handler.h"
#include "ras-events.h"
#include "ras-extlog-handler.h"
#include "ras-logger.h"
#include "ras-mce-handler.h"
#include "ras-mc-handler.h"
#include "ras-memory-failure-handler.h"
#include "ras-non-standard-handler.h"
#include "ras-page-isolation.h"
#include "ras-record.h"
#include "trigger.h"
/*
* Polling time, if read() doesn't block. Currently, trace_pipe_raw never
* blocks on read(). So, we need to sleep for a while, to avoid spending
* too much CPU cycles. A fix for it is expected for 3.10.
*/
#define POLLING_TIME 3
/* Test for a little-endian machine */
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define ENDIAN KBUFFER_ENDIAN_LITTLE
#else
#define ENDIAN KBUFFER_ENDIAN_BIG
#endif
char *choices_disable;
static const struct event_trigger event_triggers[] = {
{ "mc_event", &mc_event_trigger_setup },
#ifdef HAVE_MEMORY_FAILURE
{ "memory_failure_event", &mem_fail_event_trigger_setup },
#endif
};
static int get_debugfs_dir(char *tracing_dir, size_t len)
{
FILE *fp;
char line[MAX_PATH + 1 + 256];
char *p, *type, *dir;
fp = fopen("/proc/mounts", "r");
if (!fp) {
log(ALL, LOG_INFO, "Can't open /proc/mounts");
return -errno;
}
do {
if (!fgets(line, sizeof(line), fp))
break;
p = strtok(line, " \t");
if (!p)
break;
dir = strtok(NULL, " \t");
if (!dir)
break;
type = strtok(NULL, " \t");
if (!type)
break;
if (!strcmp(type, "debugfs")) {
fclose(fp);
strscpy(tracing_dir, dir, len - 1);
return 0;
}
} while (1);
fclose(fp);
log(ALL, LOG_INFO, "Can't find debugfs\n");
return -ENOENT;
}
static int wait_access(char *path, int ms)
{
int i;
for (i = 0; i < ms; i++) {
if (access(path, F_OK) == 0)
return 0;
usleep(1000);
}
log(ALL, LOG_WARNING, "%s failed, %s not created in %d ms\n",
__func__, path, ms);
return -1;
}
static int open_trace(struct ras_events *ras, char *name, int flags)
{
char fname[MAX_PATH + 1];
int rc;
rc = strscpy(fname, ras->tracing, sizeof(fname));
if (rc < 0)
return rc;
rc = strscat(fname, "/", sizeof(fname));
if (rc < 0)
return rc;
rc = strscat(fname, name, sizeof(fname));
if (rc < 0)
return rc;
rc = wait_access(fname, 30000);
if (rc != 0) {
/* use -1 to keep same error value with open() */
return -1;
}
rc = open(fname, flags);
if (rc < 0) {
rc = -errno;
return -errno;
}
return rc;
}
static int get_tracing_dir(struct ras_events *ras)
{
char fname[MAX_PATH + 1];
int rc, has_instances = 0;
DIR *dir;
struct dirent *entry;
get_debugfs_dir(ras->debugfs, sizeof(ras->debugfs));
rc = strscpy(fname, ras->debugfs, sizeof(fname));
if (rc < 0)
return rc;
rc = strscat(fname, "/tracing", sizeof(fname));
if (rc < 0)
return rc;
dir = opendir(fname);
if (!dir)
return -EINVAL;
for (entry = readdir(dir); entry; entry = readdir(dir)) {
if (strstr(entry->d_name, "instances")) {
has_instances = 1;
break;
}
}
closedir(dir);
strscpy(ras->tracing, ras->debugfs, sizeof(ras->tracing));
strscat(ras->tracing, "/tracing", sizeof(ras->tracing));
if (has_instances) {
rc = strscat(ras->tracing, "/instances/" TOOL_NAME,
sizeof(ras->tracing));
if (rc < 0)
return rc;
rc = mkdir(ras->tracing, 0700);
if (rc < 0 && errno != EEXIST) {
log(ALL, LOG_INFO,
"Unable to create " TOOL_NAME " instance at %s\n",
ras->tracing);
return -EINVAL;
}
}
return 0;
}
static bool is_disabled_event(char *group, char *event)
{
char ras_event_name[MAX_PATH + 1];
snprintf(ras_event_name, sizeof(ras_event_name), "%s:%s",
group, event);
if (choices_disable && strlen(choices_disable) != 0 &&
strstr(choices_disable, ras_event_name)) {
return true;
}
return false;
}
/*
* Tracing enable/disable code
*/
static int __toggle_ras_mc_event(struct ras_events *ras,
char *group, char *event, int enable)
{
int fd, rc;
char fname[MAX_PATH + 1];
if (enable)
enable = is_disabled_event(group, event) ? 0 : 1;
snprintf(fname, sizeof(fname), "%s%s:%s\n",
enable ? "" : "!",
group, event);
/* Enable RAS events */
fd = open_trace(ras, "set_event", O_RDWR | O_APPEND);
if (fd < 0) {
log(ALL, LOG_WARNING, "Can't open set_event\n");
return -errno;
}
rc = write(fd, fname, strlen(fname));
if (rc < 0) {
log(ALL, LOG_WARNING, "Can't write to set_event\n");
close(fd);
return rc;
}
close(fd);
if (!rc) {
log(ALL, LOG_WARNING, "Nothing was written on set_event\n");
return -EIO;
}
log(TERM, LOG_DEBUG, "%s:%s event %s\n",
group, event,
enable ? "enabled" : "disabled");
return 0;
}
int toggle_ras_mc_event(int enable)
{
struct ras_events *ras;
int rc = 0;
ras = calloc(1, sizeof(*ras));
if (!ras) {
log(TERM, LOG_ERR, "Can't allocate memory for ras struct\n");
return -errno;
}
rc = get_tracing_dir(ras);
if (rc < 0) {
log(TERM, LOG_ERR, "Can't locate a mounted debugfs\n");
goto free_ras;
}
rc = __toggle_ras_mc_event(ras, "ras", "mc_event", enable);
#ifdef HAVE_AER
rc |= __toggle_ras_mc_event(ras, "ras", "aer_event", enable);
#endif
#ifdef HAVE_MCE
rc |= __toggle_ras_mc_event(ras, "mce", "mce_record", enable);
#endif
#ifdef HAVE_EXTLOG
rc |= __toggle_ras_mc_event(ras, "ras", "extlog_mem_event", enable);
#endif
#ifdef HAVE_NON_STANDARD
rc |= __toggle_ras_mc_event(ras, "ras", "non_standard_event", enable);
#endif
#ifdef HAVE_ARM
rc |= __toggle_ras_mc_event(ras, "ras", "arm_event", enable);
#endif
#ifdef HAVE_DEVLINK
rc |= __toggle_ras_mc_event(ras, "devlink", "devlink_health_report", enable);
#endif
#ifdef HAVE_DISKERROR
#ifdef HAVE_BLK_RQ_ERROR
rc |= __toggle_ras_mc_event(ras, "block", "block_rq_error", enable);
#else
rc |= __toggle_ras_mc_event(ras, "block", "block_rq_complete", enable);
#endif
#endif
#ifdef HAVE_MEMORY_FAILURE
rc |= __toggle_ras_mc_event(ras, "ras", "memory_failure_event", enable);
#endif
#ifdef HAVE_CXL
rc |= __toggle_ras_mc_event(ras, "cxl", "cxl_poison", enable);
rc |= __toggle_ras_mc_event(ras, "cxl", "cxl_aer_uncorrectable_error", enable);
rc |= __toggle_ras_mc_event(ras, "cxl", "cxl_aer_correctable_error", enable);
rc |= __toggle_ras_mc_event(ras, "cxl", "cxl_overflow", enable);
rc |= __toggle_ras_mc_event(ras, "cxl", "cxl_generic_event", enable);
rc |= __toggle_ras_mc_event(ras, "cxl", "cxl_general_media", enable);
rc |= __toggle_ras_mc_event(ras, "cxl", "cxl_dram", enable);
rc |= __toggle_ras_mc_event(ras, "cxl", "cxl_memory_module", enable);
#endif
free_ras:
free(ras);
if (rc)
return -EINVAL;
return 0;
}
static void setup_event_trigger(char *event)
{
struct event_trigger trigger;
for (int i = 0; i < ARRAY_SIZE(event_triggers); i++) {
trigger = event_triggers[i];
if (!strcmp(event, trigger.name))
trigger.setup();
}
}
#ifdef HAVE_DISKERROR
#ifndef HAVE_BLK_RQ_ERROR
/*
* Set kernel filter. libtrace doesn't provide an API for setting filters
* in kernel, we have to implement it here.
*/
static int filter_ras_mc_event(struct ras_events *ras, char *group, char *event,
const char *filter_str)
{
int fd, rc;
char fname[MAX_PATH + 1];
snprintf(fname, sizeof(fname), "events/%s/%s/filter", group, event);
fd = open_trace(ras, fname, O_RDWR | O_APPEND);
if (fd < 0) {
log(ALL, LOG_WARNING, "Can't open filter file\n");
return -errno;
}
rc = write(fd, filter_str, strlen(filter_str));
if (rc < 0) {
log(ALL, LOG_WARNING, "Can't write to filter file\n");
close(fd);
return rc;
}
close(fd);
if (!rc) {
log(ALL, LOG_WARNING, "Nothing was written on filter file\n");
return -EIO;
}
return 0;
}
#endif
#endif
/*
* Tracing read code
*/
static int get_pagesize(struct ras_events *ras, struct tep_handle *pevent)
{
int fd, len, page_size = 4096;
char buf[page_size];
fd = open_trace(ras, "events/header_page", O_RDONLY);
if (fd < 0)
return page_size;
len = read(fd, buf, page_size);
if (len <= 0)
goto error;
if (tep_parse_header_page(pevent, buf, len, sizeof(long)))
goto error;
error:
close(fd);
return page_size;
}
static void parse_ras_data(struct pthread_data *pdata, struct kbuffer *kbuf,
void *data, unsigned long long time_stamp)
{
struct tep_record record;
struct trace_seq s;
record.ts = time_stamp;
record.size = kbuffer_event_size(kbuf);
record.data = data;
record.offset = kbuffer_curr_offset(kbuf);
record.cpu = pdata->cpu;
/* note offset is just offset in subbuffer */
record.missed_events = kbuffer_missed_events(kbuf);
record.record_size = kbuffer_curr_size(kbuf);
/* TODO - logging */
trace_seq_init(&s);
tep_print_event(pdata->ras->pevent, &s, &record,
"%16s-%-5d [%03d] %s %6.1000d %s %s",
TEP_PRINT_COMM, TEP_PRINT_PID, TEP_PRINT_CPU,
TEP_PRINT_LATENCY, TEP_PRINT_TIME, TEP_PRINT_NAME,
TEP_PRINT_INFO);
trace_seq_do_printf(&s);
printf("\n");
fflush(stdout);
trace_seq_destroy(&s);
}
static int get_num_cpus(struct ras_events *ras)
{
int cpus;
cpus = sysconf(_SC_NPROCESSORS_ONLN);
assert(cpus > 0);
return cpus;
}
static int set_buffer_percent(struct ras_events *ras, int percent)
{
char buf[16];
ssize_t size;
int res = 0;
int fd;
fd = open_trace(ras, "buffer_percent", O_WRONLY);
if (fd >= 0) {
/* For the backward compatibility to the old kernels, do not return
* if fail to set the buffer_percent.
*/
snprintf(buf, sizeof(buf), "%d", percent);
size = write(fd, buf, strlen(buf));
if (size <= 0) {
log(TERM, LOG_WARNING, "can't write to buffer_percent\n");
res = -EINVAL;
}
close(fd);
} else {
log(TERM, LOG_WARNING, "Can't open buffer_percent\n");
res = -EINVAL;
}
return res;
}
/*
* Kernel tracepoint had an incompatible change in 2019, causing polling
* tracepoints to fail. Rasdaemon can support both legacy and newer versions,
* with the help of a backup-compatibility legacy kernel mode.
*
* The LEGACY_KERNEL flag indicates the need to enable such code.
*/
#define LEGACY_KERNEL 255
static int read_ras_event_all_cpus(struct pthread_data *pdata,
unsigned int n_cpus)
{
ssize_t size;
unsigned long long time_stamp;
void *data;
int ready, i, count_nready;
struct kbuffer *kbuf;
void *page;
struct pollfd fds[n_cpus + 1];
struct signalfd_siginfo fdsiginfo;
sigset_t mask;
int warnonce[n_cpus];
char pipe_raw[PATH_MAX];
int legacy_kernel = 0;
memset(&warnonce, 0, sizeof(warnonce));
page = malloc(pdata[0].ras->page_size);
if (!page) {
log(TERM, LOG_ERR, "Can't allocate page\n");
return -ENOMEM;
}
kbuf = kbuffer_alloc(KBUFFER_LSIZE_8, ENDIAN);
if (!kbuf) {
log(TERM, LOG_ERR, "Can't allocate kbuf\n");
free(page);
return -ENOMEM;
}
/* Fix for poll() on the per_cpu trace_pipe and trace_pipe_raw blocks
* indefinitely with the default buffer_percent in the kernel trace system,
* which is introduced by the following change in the kernel.
* https://lore.kernel.org/all/[email protected]/T/#u.
* Set buffer_percent to 0 so that poll() will return immediately
* when the trace data is available in the ras per_cpu trace pipe_raw
*/
if (set_buffer_percent(pdata[0].ras, 0))
log(TERM, LOG_WARNING, "Set buffer_percent failed\n");
for (i = 0; i < (n_cpus + 1); i++)
fds[i].fd = -1;
for (i = 0; i < n_cpus; i++) {
fds[i].events = POLLIN;
/* FIXME: use select to open for all CPUs */
snprintf(pipe_raw, sizeof(pipe_raw),
"per_cpu/cpu%d/trace_pipe_raw", i);
fds[i].fd = open_trace(pdata[0].ras, pipe_raw, O_RDONLY);
if (fds[i].fd < 0) {
log(TERM, LOG_ERR, "Can't open trace_pipe_raw\n");
goto error;
}
}
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGHUP);
sigaddset(&mask, SIGQUIT);
if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
log(TERM, LOG_WARNING, "sigprocmask\n");
fds[n_cpus].events = POLLIN;
fds[n_cpus].fd = signalfd(-1, &mask, 0);
if (fds[n_cpus].fd < 0) {
log(TERM, LOG_WARNING, "signalfd\n");
goto error;
}
log(TERM, LOG_INFO, "Listening to events for cpus 0 to %d\n", n_cpus - 1);
if (pdata[0].ras->record_events) {
if (ras_mc_event_opendb(pdata[0].cpu, pdata[0].ras))
goto error;
#ifdef HAVE_NON_STANDARD
if (ras_ns_add_vendor_tables(pdata[0].ras))
log(TERM, LOG_ERR, "Can't add vendor table\n");
#endif
}
do {
ready = poll(fds, (n_cpus + 1), -1);
if (ready < 0)
log(TERM, LOG_WARNING, "poll\n");
/* check for the signal */
if (fds[n_cpus].revents & POLLIN) {
size = read(fds[n_cpus].fd, &fdsiginfo,
sizeof(struct signalfd_siginfo));
if (size != sizeof(struct signalfd_siginfo))
log(TERM, LOG_WARNING, "signalfd read\n");
if (fdsiginfo.ssi_signo == SIGINT ||
fdsiginfo.ssi_signo == SIGTERM ||
fdsiginfo.ssi_signo == SIGHUP ||
fdsiginfo.ssi_signo == SIGQUIT) {
log(TERM, LOG_INFO, "Received signal=%d\n",
fdsiginfo.ssi_signo);
goto cleanup;
} else {
log(TERM, LOG_INFO,
"Received unexpected signal=%d\n",
fdsiginfo.ssi_signo);
}
}
count_nready = 0;
for (i = 0; i < n_cpus; i++) {
if (fds[i].revents & POLLERR) {
if (!warnonce[i]) {
log(TERM, LOG_INFO,
"Error on CPU %i\n", i);
warnonce[i]++;
}
}
if (!(fds[i].revents & POLLIN)) {
count_nready++;
continue;
}
size = read(fds[i].fd, page, pdata[i].ras->page_size);
if (size < 0) {
log(TERM, LOG_WARNING, "read\n");
goto cleanup;
} else if (size > 0) {
kbuffer_load_subbuffer(kbuf, page);
while ((data = kbuffer_read_event(kbuf, &time_stamp))) {
if (kbuffer_curr_size(kbuf) < 0) {
log(TERM, LOG_ERR, "invalid kbuf data, discard\n");
break;
}
parse_ras_data(&pdata[i],
kbuf, data, time_stamp);
/* increment to read next event */
kbuffer_next_event(kbuf, NULL);
}
} else {
count_nready++;
}
}
/*
* If we enable fallback mode, it will always be used, as
* poll is still not working fine, IMHO
*/
if (count_nready == n_cpus) {
/* Should only happen with legacy kernels */
legacy_kernel = 1;
break;
}
} while (1);
/* poll() is not supported. We need to fallback to the old way */
log(TERM, LOG_INFO,
"Old kernel detected. Stop listening and fall back to pthread way.\n");
cleanup:
if (pdata[0].ras->record_events) {
#ifdef HAVE_NON_STANDARD
ras_ns_finalize_vendor_tables();
#endif
ras_mc_event_closedb(pdata[0].cpu, pdata[0].ras);
}
error:
kbuffer_free(kbuf);
free(page);
sigprocmask(SIG_UNBLOCK, &mask, NULL);
for (i = 0; i < (n_cpus + 1); i++) {
if (fds[i].fd > 0)
close(fds[i].fd);
}
if (legacy_kernel)
return LEGACY_KERNEL;
return -EINVAL;
}
static int read_ras_event(int fd,
struct pthread_data *pdata,
struct kbuffer *kbuf,
void *page)
{
int size;
unsigned long long time_stamp;
void *data;
/*
* read() never blocks. We can't call poll() here, as it is
* not supported on kernels below 3.10. So, the better is to just
* sleep for a while, to avoid eating too much CPU here.
*/
do {
size = read(fd, page, pdata->ras->page_size);
if (size < 0) {
log(TERM, LOG_WARNING, "read\n");
return -EINVAL;
} else if (size > 0) {
kbuffer_load_subbuffer(kbuf, page);
while ((data = kbuffer_read_event(kbuf, &time_stamp))) {
parse_ras_data(pdata, kbuf, data, time_stamp);
/* increment to read next event */
kbuffer_next_event(kbuf, NULL);
}
} else {
sleep(POLLING_TIME);
}
} while (1);
}
static void *handle_ras_events_cpu(void *priv)
{
int fd;
struct kbuffer *kbuf;
void *page;
char pipe_raw[PATH_MAX];
struct pthread_data *pdata = priv;
page = malloc(pdata->ras->page_size);
if (!page) {
log(TERM, LOG_ERR, "Can't allocate page\n");
return NULL;
}
kbuf = kbuffer_alloc(KBUFFER_LSIZE_8, ENDIAN);
if (!kbuf) {
log(TERM, LOG_ERR, "Can't allocate kbuf");
free(page);
return NULL;
}
/* FIXME: use select to open for all CPUs */
snprintf(pipe_raw, sizeof(pipe_raw),
"per_cpu/cpu%d/trace_pipe_raw",
pdata->cpu);
fd = open_trace(pdata->ras, pipe_raw, O_RDONLY);
if (fd < 0) {
log(TERM, LOG_ERR, "Can't open trace_pipe_raw\n");
kbuffer_free(kbuf);
free(page);
return NULL;
}
log(TERM, LOG_INFO, "Listening to events on cpu %d\n", pdata->cpu);
if (pdata->ras->record_events) {
pthread_mutex_lock(&pdata->ras->db_lock);
if (ras_mc_event_opendb(pdata->cpu, pdata->ras)) {
pthread_mutex_unlock(&pdata->ras->db_lock);
log(TERM, LOG_ERR, "Can't open database\n");
close(fd);
kbuffer_free(kbuf);
free(page);
return 0;
}
#ifdef HAVE_NON_STANDARD
if (ras_ns_add_vendor_tables(pdata->ras))
log(TERM, LOG_ERR, "Can't add vendor table\n");
#endif
pthread_mutex_unlock(&pdata->ras->db_lock);
}
read_ras_event(fd, pdata, kbuf, page);
if (pdata->ras->record_events) {
pthread_mutex_lock(&pdata->ras->db_lock);
#ifdef HAVE_NON_STANDARD
ras_ns_finalize_vendor_tables();
#endif
ras_mc_event_closedb(pdata->cpu, pdata->ras);
pthread_mutex_unlock(&pdata->ras->db_lock);
}
close(fd);
kbuffer_free(kbuf);
free(page);
return NULL;
}
#define UPTIME "uptime"
static int select_tracing_timestamp(struct ras_events *ras)
{
FILE *fp;
int fd, rc;
time_t uptime, now;
size_t size;
unsigned int j1;
char buf[4096];
/* Check if uptime is supported (kernel 3.10-rc1 or upper) */
fd = open_trace(ras, "trace_clock", O_RDONLY);
if (fd < 0) {
log(TERM, LOG_ERR, "Can't open trace_clock\n");
return -EINVAL;
}
size = read(fd, buf, sizeof(buf));
close(fd);
if (!size) {
log(TERM, LOG_ERR, "trace_clock is empty!\n");
return -EINVAL;
}
if (!strstr(buf, UPTIME)) {
log(TERM, LOG_INFO, "Kernel doesn't support uptime clock\n");
return 0;
}
/* Select uptime tracing */
fd = open_trace(ras, "trace_clock", O_WRONLY);
if (!fd) {
log(TERM, LOG_ERR,
"Kernel didn't allow writing to trace_clock\n");
return 0;
}
rc = write(fd, UPTIME, sizeof(UPTIME));
close(fd);
if (rc < 0) {
log(TERM, LOG_ERR,
"Kernel didn't allow selecting uptime on trace_clock\n");
return 0;
}
/* Reference uptime with localtime */
fp = fopen("/proc/uptime", "r");
if (!fp) {
log(TERM, LOG_ERR,
"Couldn't read from /proc/uptime\n");
return 0;
}
rc = fscanf(fp, "%zu.%u ", &uptime, &j1);
fclose(fp);
if (rc <= 0) {
log(TERM, LOG_ERR, "Can't parse /proc/uptime!\n");
return -EINVAL;
}
now = time(NULL);
ras->use_uptime = 1;
ras->uptime_diff = now - uptime;
return 0;
}
#define EVENT_DISABLED 1
static int add_event_handler(struct ras_events *ras, struct tep_handle *pevent,
unsigned int page_size, char *group, char *event,
tep_event_handler_func func, char *filter_str, int id)
{
int fd, size, rc;
char *page, fname[MAX_PATH + 1];
struct tep_event_filter *filter = NULL;
snprintf(fname, sizeof(fname), "events/%s/%s/format", group, event);
fd = open_trace(ras, fname, O_RDONLY);
if (fd < 0) {
if (fd == -ENOENT) {
log(TERM, LOG_ERR,
"Feature %s:%s not supported on your system.\n",
group, event);
return EVENT_DISABLED;
}
log(TERM, LOG_ERR, "Can't get %s:%s traces: %s\n",
group, event, strerror(-fd));
return fd;
}
page = malloc(page_size);
if (!page) {
rc = -errno;
log(TERM, LOG_ERR, "Can't allocate page to read %s:%s format\n",
group, event);
close(fd);
return rc;
}
size = read(fd, page, page_size);
close(fd);
if (size < 0) {
log(TERM, LOG_ERR, "Can't get arch page size\n");
free(page);
return size;
}
/* Registers the special event handlers */
rc = tep_register_event_handler(pevent, -1, group, event, func, ras);
if (rc == TEP_ERRNO__MEM_ALLOC_FAILED) {
log(TERM, LOG_ERR, "Can't register event handler for %s:%s\n",
group, event);
free(page);
return -EINVAL;
}
rc = tep_parse_event(pevent, page, size, group);
if (rc) {
log(TERM, LOG_ERR, "Can't parse event %s:%s\n", group, event);
free(page);
return -EINVAL;
}
if (filter_str) {
char error[255];
filter = tep_filter_alloc(pevent);
if (!filter) {
log(TERM, LOG_ERR,
"Failed to allocate filter for %s/%s.\n", group, event);
free(page);
return -EINVAL;
}
rc = tep_filter_add_filter_str(filter, filter_str);
if (rc < 0) {
tep_filter_strerror(filter, rc, error, sizeof(error));
log(TERM, LOG_ERR,
"Failed to install filter for %s/%s: %s\n", group, event, error);
tep_filter_free(filter);
free(page);
return rc;
}
}
ras->filters[id] = filter;
if (is_disabled_event(group, event)) {
log(ALL, LOG_INFO, "Disabled %s:%s tracing from config\n",
group, event);
return EVENT_DISABLED;
}
/* Enable RAS events */
rc = __toggle_ras_mc_event(ras, group, event, 1);
free(page);
if (rc < 0) {
log(TERM, LOG_ERR, "Can't enable %s:%s tracing\n",
group, event);
return -EINVAL;
}
setup_event_trigger(event);
log(ALL, LOG_INFO, "Enabled event %s:%s\n", group, event);
return 0;
}
int handle_ras_events(int record_events, int enable_ipmitool)
{
int rc, page_size, i;
int num_events = 0;
unsigned int cpus;
struct tep_handle *pevent = NULL;
struct pthread_data *data = NULL;
struct ras_events *ras = NULL;
#ifdef HAVE_DEVLINK
char *filter_str = NULL;
#endif
ras = calloc(1, sizeof(*ras));
if (!ras) {
log(TERM, LOG_ERR, "Can't allocate memory for ras struct\n");
return -errno;
}
rc = get_tracing_dir(ras);
if (rc < 0) {
log(TERM, LOG_ERR, "Can't locate a mounted debugfs\n");
goto err;
}
rc = select_tracing_timestamp(ras);
if (rc < 0)
log(TERM, LOG_ERR, "Can't select a timestamp for tracing. Using default\n");
pevent = tep_alloc();
if (!pevent) {
log(TERM, LOG_ERR, "Can't allocate pevent\n");
rc = -errno;
goto err;
}
page_size = get_pagesize(ras, pevent);
ras->pevent = pevent;
ras->page_size = page_size;
ras->record_events = record_events;
#ifdef HAVE_MEMORY_ROW_CE_PFA
ras_row_account_init();
#endif
#ifdef HAVE_MEMORY_CE_PFA
/* FIXME: enable memory isolation unconditionally */
ras_page_account_init();
#endif
rc = add_event_handler(ras, pevent, page_size, "ras", "mc_event",
ras_mc_event_handler, NULL, MC_EVENT);
if (!rc)
num_events++;
else if (rc != EVENT_DISABLED)
log(ALL, LOG_ERR, "Can't get traces from %s:%s\n",
"ras", "mc_event");
#ifdef HAVE_AER
ras_aer_handler_init(enable_ipmitool);
rc = add_event_handler(ras, pevent, page_size, "ras", "aer_event",
ras_aer_event_handler, NULL, AER_EVENT);
if (!rc)
num_events++;
else if (rc != EVENT_DISABLED && rc != ENOENT)
log(ALL, LOG_ERR, "Can't get traces from %s:%s\n",
"ras", "aer_event");
#endif
#ifdef HAVE_NON_STANDARD
rc = add_event_handler(ras, pevent, page_size, "ras", "non_standard_event",