Merge branch 'perf-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[muen/linux.git] / tools / perf / util / bpf-loader.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * bpf-loader.c
4  *
5  * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
6  * Copyright (C) 2015 Huawei Inc.
7  */
8
9 #include <linux/bpf.h>
10 #include <bpf/libbpf.h>
11 #include <bpf/bpf.h>
12 #include <linux/err.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <errno.h>
16 #include "perf.h"
17 #include "debug.h"
18 #include "evlist.h"
19 #include "bpf-loader.h"
20 #include "bpf-prologue.h"
21 #include "probe-event.h"
22 #include "probe-finder.h" // for MAX_PROBES
23 #include "parse-events.h"
24 #include "strfilter.h"
25 #include "llvm-utils.h"
26 #include "c++/clang-c.h"
27
28 static int libbpf_perf_print(enum libbpf_print_level level __attribute__((unused)),
29                               const char *fmt, va_list args)
30 {
31         return veprintf(1, verbose, pr_fmt(fmt), args);
32 }
33
34 struct bpf_prog_priv {
35         bool is_tp;
36         char *sys_name;
37         char *evt_name;
38         struct perf_probe_event pev;
39         bool need_prologue;
40         struct bpf_insn *insns_buf;
41         int nr_types;
42         int *type_mapping;
43 };
44
45 static bool libbpf_initialized;
46
47 struct bpf_object *
48 bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name)
49 {
50         struct bpf_object *obj;
51
52         if (!libbpf_initialized) {
53                 libbpf_set_print(libbpf_perf_print);
54                 libbpf_initialized = true;
55         }
56
57         obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, name);
58         if (IS_ERR_OR_NULL(obj)) {
59                 pr_debug("bpf: failed to load buffer\n");
60                 return ERR_PTR(-EINVAL);
61         }
62
63         return obj;
64 }
65
66 struct bpf_object *bpf__prepare_load(const char *filename, bool source)
67 {
68         struct bpf_object *obj;
69
70         if (!libbpf_initialized) {
71                 libbpf_set_print(libbpf_perf_print);
72                 libbpf_initialized = true;
73         }
74
75         if (source) {
76                 int err;
77                 void *obj_buf;
78                 size_t obj_buf_sz;
79
80                 perf_clang__init();
81                 err = perf_clang__compile_bpf(filename, &obj_buf, &obj_buf_sz);
82                 perf_clang__cleanup();
83                 if (err) {
84                         pr_debug("bpf: builtin compilation failed: %d, try external compiler\n", err);
85                         err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz);
86                         if (err)
87                                 return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
88                 } else
89                         pr_debug("bpf: successful builtin compilation\n");
90                 obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);
91
92                 if (!IS_ERR_OR_NULL(obj) && llvm_param.dump_obj)
93                         llvm__dump_obj(filename, obj_buf, obj_buf_sz);
94
95                 free(obj_buf);
96         } else
97                 obj = bpf_object__open(filename);
98
99         if (IS_ERR_OR_NULL(obj)) {
100                 pr_debug("bpf: failed to load %s\n", filename);
101                 return obj;
102         }
103
104         return obj;
105 }
106
107 void bpf__clear(void)
108 {
109         struct bpf_object *obj, *tmp;
110
111         bpf_object__for_each_safe(obj, tmp) {
112                 bpf__unprobe(obj);
113                 bpf_object__close(obj);
114         }
115 }
116
117 static void
118 clear_prog_priv(struct bpf_program *prog __maybe_unused,
119                 void *_priv)
120 {
121         struct bpf_prog_priv *priv = _priv;
122
123         cleanup_perf_probe_events(&priv->pev, 1);
124         zfree(&priv->insns_buf);
125         zfree(&priv->type_mapping);
126         zfree(&priv->sys_name);
127         zfree(&priv->evt_name);
128         free(priv);
129 }
130
131 static int
132 prog_config__exec(const char *value, struct perf_probe_event *pev)
133 {
134         pev->uprobes = true;
135         pev->target = strdup(value);
136         if (!pev->target)
137                 return -ENOMEM;
138         return 0;
139 }
140
141 static int
142 prog_config__module(const char *value, struct perf_probe_event *pev)
143 {
144         pev->uprobes = false;
145         pev->target = strdup(value);
146         if (!pev->target)
147                 return -ENOMEM;
148         return 0;
149 }
150
151 static int
152 prog_config__bool(const char *value, bool *pbool, bool invert)
153 {
154         int err;
155         bool bool_value;
156
157         if (!pbool)
158                 return -EINVAL;
159
160         err = strtobool(value, &bool_value);
161         if (err)
162                 return err;
163
164         *pbool = invert ? !bool_value : bool_value;
165         return 0;
166 }
167
168 static int
169 prog_config__inlines(const char *value,
170                      struct perf_probe_event *pev __maybe_unused)
171 {
172         return prog_config__bool(value, &probe_conf.no_inlines, true);
173 }
174
175 static int
176 prog_config__force(const char *value,
177                    struct perf_probe_event *pev __maybe_unused)
178 {
179         return prog_config__bool(value, &probe_conf.force_add, false);
180 }
181
182 static struct {
183         const char *key;
184         const char *usage;
185         const char *desc;
186         int (*func)(const char *, struct perf_probe_event *);
187 } bpf_prog_config_terms[] = {
188         {
189                 .key    = "exec",
190                 .usage  = "exec=<full path of file>",
191                 .desc   = "Set uprobe target",
192                 .func   = prog_config__exec,
193         },
194         {
195                 .key    = "module",
196                 .usage  = "module=<module name>    ",
197                 .desc   = "Set kprobe module",
198                 .func   = prog_config__module,
199         },
200         {
201                 .key    = "inlines",
202                 .usage  = "inlines=[yes|no]        ",
203                 .desc   = "Probe at inline symbol",
204                 .func   = prog_config__inlines,
205         },
206         {
207                 .key    = "force",
208                 .usage  = "force=[yes|no]          ",
209                 .desc   = "Forcibly add events with existing name",
210                 .func   = prog_config__force,
211         },
212 };
213
214 static int
215 do_prog_config(const char *key, const char *value,
216                struct perf_probe_event *pev)
217 {
218         unsigned int i;
219
220         pr_debug("config bpf program: %s=%s\n", key, value);
221         for (i = 0; i < ARRAY_SIZE(bpf_prog_config_terms); i++)
222                 if (strcmp(key, bpf_prog_config_terms[i].key) == 0)
223                         return bpf_prog_config_terms[i].func(value, pev);
224
225         pr_debug("BPF: ERROR: invalid program config option: %s=%s\n",
226                  key, value);
227
228         pr_debug("\nHint: Valid options are:\n");
229         for (i = 0; i < ARRAY_SIZE(bpf_prog_config_terms); i++)
230                 pr_debug("\t%s:\t%s\n", bpf_prog_config_terms[i].usage,
231                          bpf_prog_config_terms[i].desc);
232         pr_debug("\n");
233
234         return -BPF_LOADER_ERRNO__PROGCONF_TERM;
235 }
236
237 static const char *
238 parse_prog_config_kvpair(const char *config_str, struct perf_probe_event *pev)
239 {
240         char *text = strdup(config_str);
241         char *sep, *line;
242         const char *main_str = NULL;
243         int err = 0;
244
245         if (!text) {
246                 pr_debug("Not enough memory: dup config_str failed\n");
247                 return ERR_PTR(-ENOMEM);
248         }
249
250         line = text;
251         while ((sep = strchr(line, ';'))) {
252                 char *equ;
253
254                 *sep = '\0';
255                 equ = strchr(line, '=');
256                 if (!equ) {
257                         pr_warning("WARNING: invalid config in BPF object: %s\n",
258                                    line);
259                         pr_warning("\tShould be 'key=value'.\n");
260                         goto nextline;
261                 }
262                 *equ = '\0';
263
264                 err = do_prog_config(line, equ + 1, pev);
265                 if (err)
266                         break;
267 nextline:
268                 line = sep + 1;
269         }
270
271         if (!err)
272                 main_str = config_str + (line - text);
273         free(text);
274
275         return err ? ERR_PTR(err) : main_str;
276 }
277
278 static int
279 parse_prog_config(const char *config_str, const char **p_main_str,
280                   bool *is_tp, struct perf_probe_event *pev)
281 {
282         int err;
283         const char *main_str = parse_prog_config_kvpair(config_str, pev);
284
285         if (IS_ERR(main_str))
286                 return PTR_ERR(main_str);
287
288         *p_main_str = main_str;
289         if (!strchr(main_str, '=')) {
290                 /* Is a tracepoint event? */
291                 const char *s = strchr(main_str, ':');
292
293                 if (!s) {
294                         pr_debug("bpf: '%s' is not a valid tracepoint\n",
295                                  config_str);
296                         return -BPF_LOADER_ERRNO__CONFIG;
297                 }
298
299                 *is_tp = true;
300                 return 0;
301         }
302
303         *is_tp = false;
304         err = parse_perf_probe_command(main_str, pev);
305         if (err < 0) {
306                 pr_debug("bpf: '%s' is not a valid config string\n",
307                          config_str);
308                 /* parse failed, don't need clear pev. */
309                 return -BPF_LOADER_ERRNO__CONFIG;
310         }
311         return 0;
312 }
313
314 static int
315 config_bpf_program(struct bpf_program *prog)
316 {
317         struct perf_probe_event *pev = NULL;
318         struct bpf_prog_priv *priv = NULL;
319         const char *config_str, *main_str;
320         bool is_tp = false;
321         int err;
322
323         /* Initialize per-program probing setting */
324         probe_conf.no_inlines = false;
325         probe_conf.force_add = false;
326
327         config_str = bpf_program__title(prog, false);
328         if (IS_ERR(config_str)) {
329                 pr_debug("bpf: unable to get title for program\n");
330                 return PTR_ERR(config_str);
331         }
332
333         priv = calloc(sizeof(*priv), 1);
334         if (!priv) {
335                 pr_debug("bpf: failed to alloc priv\n");
336                 return -ENOMEM;
337         }
338         pev = &priv->pev;
339
340         pr_debug("bpf: config program '%s'\n", config_str);
341         err = parse_prog_config(config_str, &main_str, &is_tp, pev);
342         if (err)
343                 goto errout;
344
345         if (is_tp) {
346                 char *s = strchr(main_str, ':');
347
348                 priv->is_tp = true;
349                 priv->sys_name = strndup(main_str, s - main_str);
350                 priv->evt_name = strdup(s + 1);
351                 goto set_priv;
352         }
353
354         if (pev->group && strcmp(pev->group, PERF_BPF_PROBE_GROUP)) {
355                 pr_debug("bpf: '%s': group for event is set and not '%s'.\n",
356                          config_str, PERF_BPF_PROBE_GROUP);
357                 err = -BPF_LOADER_ERRNO__GROUP;
358                 goto errout;
359         } else if (!pev->group)
360                 pev->group = strdup(PERF_BPF_PROBE_GROUP);
361
362         if (!pev->group) {
363                 pr_debug("bpf: strdup failed\n");
364                 err = -ENOMEM;
365                 goto errout;
366         }
367
368         if (!pev->event) {
369                 pr_debug("bpf: '%s': event name is missing. Section name should be 'key=value'\n",
370                          config_str);
371                 err = -BPF_LOADER_ERRNO__EVENTNAME;
372                 goto errout;
373         }
374         pr_debug("bpf: config '%s' is ok\n", config_str);
375
376 set_priv:
377         err = bpf_program__set_priv(prog, priv, clear_prog_priv);
378         if (err) {
379                 pr_debug("Failed to set priv for program '%s'\n", config_str);
380                 goto errout;
381         }
382
383         return 0;
384
385 errout:
386         if (pev)
387                 clear_perf_probe_event(pev);
388         free(priv);
389         return err;
390 }
391
392 static int bpf__prepare_probe(void)
393 {
394         static int err = 0;
395         static bool initialized = false;
396
397         /*
398          * Make err static, so if init failed the first, bpf__prepare_probe()
399          * fails each time without calling init_probe_symbol_maps multiple
400          * times.
401          */
402         if (initialized)
403                 return err;
404
405         initialized = true;
406         err = init_probe_symbol_maps(false);
407         if (err < 0)
408                 pr_debug("Failed to init_probe_symbol_maps\n");
409         probe_conf.max_probes = MAX_PROBES;
410         return err;
411 }
412
413 static int
414 preproc_gen_prologue(struct bpf_program *prog, int n,
415                      struct bpf_insn *orig_insns, int orig_insns_cnt,
416                      struct bpf_prog_prep_result *res)
417 {
418         struct bpf_prog_priv *priv = bpf_program__priv(prog);
419         struct probe_trace_event *tev;
420         struct perf_probe_event *pev;
421         struct bpf_insn *buf;
422         size_t prologue_cnt = 0;
423         int i, err;
424
425         if (IS_ERR(priv) || !priv || priv->is_tp)
426                 goto errout;
427
428         pev = &priv->pev;
429
430         if (n < 0 || n >= priv->nr_types)
431                 goto errout;
432
433         /* Find a tev belongs to that type */
434         for (i = 0; i < pev->ntevs; i++) {
435                 if (priv->type_mapping[i] == n)
436                         break;
437         }
438
439         if (i >= pev->ntevs) {
440                 pr_debug("Internal error: prologue type %d not found\n", n);
441                 return -BPF_LOADER_ERRNO__PROLOGUE;
442         }
443
444         tev = &pev->tevs[i];
445
446         buf = priv->insns_buf;
447         err = bpf__gen_prologue(tev->args, tev->nargs,
448                                 buf, &prologue_cnt,
449                                 BPF_MAXINSNS - orig_insns_cnt);
450         if (err) {
451                 const char *title;
452
453                 title = bpf_program__title(prog, false);
454                 if (!title)
455                         title = "[unknown]";
456
457                 pr_debug("Failed to generate prologue for program %s\n",
458                          title);
459                 return err;
460         }
461
462         memcpy(&buf[prologue_cnt], orig_insns,
463                sizeof(struct bpf_insn) * orig_insns_cnt);
464
465         res->new_insn_ptr = buf;
466         res->new_insn_cnt = prologue_cnt + orig_insns_cnt;
467         res->pfd = NULL;
468         return 0;
469
470 errout:
471         pr_debug("Internal error in preproc_gen_prologue\n");
472         return -BPF_LOADER_ERRNO__PROLOGUE;
473 }
474
475 /*
476  * compare_tev_args is reflexive, transitive and antisymmetric.
477  * I can proof it but this margin is too narrow to contain.
478  */
479 static int compare_tev_args(const void *ptev1, const void *ptev2)
480 {
481         int i, ret;
482         const struct probe_trace_event *tev1 =
483                 *(const struct probe_trace_event **)ptev1;
484         const struct probe_trace_event *tev2 =
485                 *(const struct probe_trace_event **)ptev2;
486
487         ret = tev2->nargs - tev1->nargs;
488         if (ret)
489                 return ret;
490
491         for (i = 0; i < tev1->nargs; i++) {
492                 struct probe_trace_arg *arg1, *arg2;
493                 struct probe_trace_arg_ref *ref1, *ref2;
494
495                 arg1 = &tev1->args[i];
496                 arg2 = &tev2->args[i];
497
498                 ret = strcmp(arg1->value, arg2->value);
499                 if (ret)
500                         return ret;
501
502                 ref1 = arg1->ref;
503                 ref2 = arg2->ref;
504
505                 while (ref1 && ref2) {
506                         ret = ref2->offset - ref1->offset;
507                         if (ret)
508                                 return ret;
509
510                         ref1 = ref1->next;
511                         ref2 = ref2->next;
512                 }
513
514                 if (ref1 || ref2)
515                         return ref2 ? 1 : -1;
516         }
517
518         return 0;
519 }
520
521 /*
522  * Assign a type number to each tevs in a pev.
523  * mapping is an array with same slots as tevs in that pev.
524  * nr_types will be set to number of types.
525  */
526 static int map_prologue(struct perf_probe_event *pev, int *mapping,
527                         int *nr_types)
528 {
529         int i, type = 0;
530         struct probe_trace_event **ptevs;
531
532         size_t array_sz = sizeof(*ptevs) * pev->ntevs;
533
534         ptevs = malloc(array_sz);
535         if (!ptevs) {
536                 pr_debug("Not enough memory: alloc ptevs failed\n");
537                 return -ENOMEM;
538         }
539
540         pr_debug("In map_prologue, ntevs=%d\n", pev->ntevs);
541         for (i = 0; i < pev->ntevs; i++)
542                 ptevs[i] = &pev->tevs[i];
543
544         qsort(ptevs, pev->ntevs, sizeof(*ptevs),
545               compare_tev_args);
546
547         for (i = 0; i < pev->ntevs; i++) {
548                 int n;
549
550                 n = ptevs[i] - pev->tevs;
551                 if (i == 0) {
552                         mapping[n] = type;
553                         pr_debug("mapping[%d]=%d\n", n, type);
554                         continue;
555                 }
556
557                 if (compare_tev_args(ptevs + i, ptevs + i - 1) == 0)
558                         mapping[n] = type;
559                 else
560                         mapping[n] = ++type;
561
562                 pr_debug("mapping[%d]=%d\n", n, mapping[n]);
563         }
564         free(ptevs);
565         *nr_types = type + 1;
566
567         return 0;
568 }
569
570 static int hook_load_preprocessor(struct bpf_program *prog)
571 {
572         struct bpf_prog_priv *priv = bpf_program__priv(prog);
573         struct perf_probe_event *pev;
574         bool need_prologue = false;
575         int err, i;
576
577         if (IS_ERR(priv) || !priv) {
578                 pr_debug("Internal error when hook preprocessor\n");
579                 return -BPF_LOADER_ERRNO__INTERNAL;
580         }
581
582         if (priv->is_tp) {
583                 priv->need_prologue = false;
584                 return 0;
585         }
586
587         pev = &priv->pev;
588         for (i = 0; i < pev->ntevs; i++) {
589                 struct probe_trace_event *tev = &pev->tevs[i];
590
591                 if (tev->nargs > 0) {
592                         need_prologue = true;
593                         break;
594                 }
595         }
596
597         /*
598          * Since all tevs don't have argument, we don't need generate
599          * prologue.
600          */
601         if (!need_prologue) {
602                 priv->need_prologue = false;
603                 return 0;
604         }
605
606         priv->need_prologue = true;
607         priv->insns_buf = malloc(sizeof(struct bpf_insn) * BPF_MAXINSNS);
608         if (!priv->insns_buf) {
609                 pr_debug("Not enough memory: alloc insns_buf failed\n");
610                 return -ENOMEM;
611         }
612
613         priv->type_mapping = malloc(sizeof(int) * pev->ntevs);
614         if (!priv->type_mapping) {
615                 pr_debug("Not enough memory: alloc type_mapping failed\n");
616                 return -ENOMEM;
617         }
618         memset(priv->type_mapping, -1,
619                sizeof(int) * pev->ntevs);
620
621         err = map_prologue(pev, priv->type_mapping, &priv->nr_types);
622         if (err)
623                 return err;
624
625         err = bpf_program__set_prep(prog, priv->nr_types,
626                                     preproc_gen_prologue);
627         return err;
628 }
629
630 int bpf__probe(struct bpf_object *obj)
631 {
632         int err = 0;
633         struct bpf_program *prog;
634         struct bpf_prog_priv *priv;
635         struct perf_probe_event *pev;
636
637         err = bpf__prepare_probe();
638         if (err) {
639                 pr_debug("bpf__prepare_probe failed\n");
640                 return err;
641         }
642
643         bpf_object__for_each_program(prog, obj) {
644                 err = config_bpf_program(prog);
645                 if (err)
646                         goto out;
647
648                 priv = bpf_program__priv(prog);
649                 if (IS_ERR(priv) || !priv) {
650                         err = PTR_ERR(priv);
651                         goto out;
652                 }
653
654                 if (priv->is_tp) {
655                         bpf_program__set_tracepoint(prog);
656                         continue;
657                 }
658
659                 bpf_program__set_kprobe(prog);
660                 pev = &priv->pev;
661
662                 err = convert_perf_probe_events(pev, 1);
663                 if (err < 0) {
664                         pr_debug("bpf_probe: failed to convert perf probe events\n");
665                         goto out;
666                 }
667
668                 err = apply_perf_probe_events(pev, 1);
669                 if (err < 0) {
670                         pr_debug("bpf_probe: failed to apply perf probe events\n");
671                         goto out;
672                 }
673
674                 /*
675                  * After probing, let's consider prologue, which
676                  * adds program fetcher to BPF programs.
677                  *
678                  * hook_load_preprocessorr() hooks pre-processor
679                  * to bpf_program, let it generate prologue
680                  * dynamically during loading.
681                  */
682                 err = hook_load_preprocessor(prog);
683                 if (err)
684                         goto out;
685         }
686 out:
687         return err < 0 ? err : 0;
688 }
689
690 #define EVENTS_WRITE_BUFSIZE  4096
691 int bpf__unprobe(struct bpf_object *obj)
692 {
693         int err, ret = 0;
694         struct bpf_program *prog;
695
696         bpf_object__for_each_program(prog, obj) {
697                 struct bpf_prog_priv *priv = bpf_program__priv(prog);
698                 int i;
699
700                 if (IS_ERR(priv) || !priv || priv->is_tp)
701                         continue;
702
703                 for (i = 0; i < priv->pev.ntevs; i++) {
704                         struct probe_trace_event *tev = &priv->pev.tevs[i];
705                         char name_buf[EVENTS_WRITE_BUFSIZE];
706                         struct strfilter *delfilter;
707
708                         snprintf(name_buf, EVENTS_WRITE_BUFSIZE,
709                                  "%s:%s", tev->group, tev->event);
710                         name_buf[EVENTS_WRITE_BUFSIZE - 1] = '\0';
711
712                         delfilter = strfilter__new(name_buf, NULL);
713                         if (!delfilter) {
714                                 pr_debug("Failed to create filter for unprobing\n");
715                                 ret = -ENOMEM;
716                                 continue;
717                         }
718
719                         err = del_perf_probe_events(delfilter);
720                         strfilter__delete(delfilter);
721                         if (err) {
722                                 pr_debug("Failed to delete %s\n", name_buf);
723                                 ret = err;
724                                 continue;
725                         }
726                 }
727         }
728         return ret;
729 }
730
731 int bpf__load(struct bpf_object *obj)
732 {
733         int err;
734
735         err = bpf_object__load(obj);
736         if (err) {
737                 char bf[128];
738                 libbpf_strerror(err, bf, sizeof(bf));
739                 pr_debug("bpf: load objects failed: err=%d: (%s)\n", err, bf);
740                 return err;
741         }
742         return 0;
743 }
744
745 int bpf__foreach_event(struct bpf_object *obj,
746                        bpf_prog_iter_callback_t func,
747                        void *arg)
748 {
749         struct bpf_program *prog;
750         int err;
751
752         bpf_object__for_each_program(prog, obj) {
753                 struct bpf_prog_priv *priv = bpf_program__priv(prog);
754                 struct probe_trace_event *tev;
755                 struct perf_probe_event *pev;
756                 int i, fd;
757
758                 if (IS_ERR(priv) || !priv) {
759                         pr_debug("bpf: failed to get private field\n");
760                         return -BPF_LOADER_ERRNO__INTERNAL;
761                 }
762
763                 if (priv->is_tp) {
764                         fd = bpf_program__fd(prog);
765                         err = (*func)(priv->sys_name, priv->evt_name, fd, arg);
766                         if (err) {
767                                 pr_debug("bpf: tracepoint call back failed, stop iterate\n");
768                                 return err;
769                         }
770                         continue;
771                 }
772
773                 pev = &priv->pev;
774                 for (i = 0; i < pev->ntevs; i++) {
775                         tev = &pev->tevs[i];
776
777                         if (priv->need_prologue) {
778                                 int type = priv->type_mapping[i];
779
780                                 fd = bpf_program__nth_fd(prog, type);
781                         } else {
782                                 fd = bpf_program__fd(prog);
783                         }
784
785                         if (fd < 0) {
786                                 pr_debug("bpf: failed to get file descriptor\n");
787                                 return fd;
788                         }
789
790                         err = (*func)(tev->group, tev->event, fd, arg);
791                         if (err) {
792                                 pr_debug("bpf: call back failed, stop iterate\n");
793                                 return err;
794                         }
795                 }
796         }
797         return 0;
798 }
799
800 enum bpf_map_op_type {
801         BPF_MAP_OP_SET_VALUE,
802         BPF_MAP_OP_SET_EVSEL,
803 };
804
805 enum bpf_map_key_type {
806         BPF_MAP_KEY_ALL,
807         BPF_MAP_KEY_RANGES,
808 };
809
810 struct bpf_map_op {
811         struct list_head list;
812         enum bpf_map_op_type op_type;
813         enum bpf_map_key_type key_type;
814         union {
815                 struct parse_events_array array;
816         } k;
817         union {
818                 u64 value;
819                 struct perf_evsel *evsel;
820         } v;
821 };
822
823 struct bpf_map_priv {
824         struct list_head ops_list;
825 };
826
827 static void
828 bpf_map_op__delete(struct bpf_map_op *op)
829 {
830         if (!list_empty(&op->list))
831                 list_del(&op->list);
832         if (op->key_type == BPF_MAP_KEY_RANGES)
833                 parse_events__clear_array(&op->k.array);
834         free(op);
835 }
836
837 static void
838 bpf_map_priv__purge(struct bpf_map_priv *priv)
839 {
840         struct bpf_map_op *pos, *n;
841
842         list_for_each_entry_safe(pos, n, &priv->ops_list, list) {
843                 list_del_init(&pos->list);
844                 bpf_map_op__delete(pos);
845         }
846 }
847
848 static void
849 bpf_map_priv__clear(struct bpf_map *map __maybe_unused,
850                     void *_priv)
851 {
852         struct bpf_map_priv *priv = _priv;
853
854         bpf_map_priv__purge(priv);
855         free(priv);
856 }
857
858 static int
859 bpf_map_op_setkey(struct bpf_map_op *op, struct parse_events_term *term)
860 {
861         op->key_type = BPF_MAP_KEY_ALL;
862         if (!term)
863                 return 0;
864
865         if (term->array.nr_ranges) {
866                 size_t memsz = term->array.nr_ranges *
867                                 sizeof(op->k.array.ranges[0]);
868
869                 op->k.array.ranges = memdup(term->array.ranges, memsz);
870                 if (!op->k.array.ranges) {
871                         pr_debug("Not enough memory to alloc indices for map\n");
872                         return -ENOMEM;
873                 }
874                 op->key_type = BPF_MAP_KEY_RANGES;
875                 op->k.array.nr_ranges = term->array.nr_ranges;
876         }
877         return 0;
878 }
879
880 static struct bpf_map_op *
881 bpf_map_op__new(struct parse_events_term *term)
882 {
883         struct bpf_map_op *op;
884         int err;
885
886         op = zalloc(sizeof(*op));
887         if (!op) {
888                 pr_debug("Failed to alloc bpf_map_op\n");
889                 return ERR_PTR(-ENOMEM);
890         }
891         INIT_LIST_HEAD(&op->list);
892
893         err = bpf_map_op_setkey(op, term);
894         if (err) {
895                 free(op);
896                 return ERR_PTR(err);
897         }
898         return op;
899 }
900
901 static struct bpf_map_op *
902 bpf_map_op__clone(struct bpf_map_op *op)
903 {
904         struct bpf_map_op *newop;
905
906         newop = memdup(op, sizeof(*op));
907         if (!newop) {
908                 pr_debug("Failed to alloc bpf_map_op\n");
909                 return NULL;
910         }
911
912         INIT_LIST_HEAD(&newop->list);
913         if (op->key_type == BPF_MAP_KEY_RANGES) {
914                 size_t memsz = op->k.array.nr_ranges *
915                                sizeof(op->k.array.ranges[0]);
916
917                 newop->k.array.ranges = memdup(op->k.array.ranges, memsz);
918                 if (!newop->k.array.ranges) {
919                         pr_debug("Failed to alloc indices for map\n");
920                         free(newop);
921                         return NULL;
922                 }
923         }
924
925         return newop;
926 }
927
928 static struct bpf_map_priv *
929 bpf_map_priv__clone(struct bpf_map_priv *priv)
930 {
931         struct bpf_map_priv *newpriv;
932         struct bpf_map_op *pos, *newop;
933
934         newpriv = zalloc(sizeof(*newpriv));
935         if (!newpriv) {
936                 pr_debug("Not enough memory to alloc map private\n");
937                 return NULL;
938         }
939         INIT_LIST_HEAD(&newpriv->ops_list);
940
941         list_for_each_entry(pos, &priv->ops_list, list) {
942                 newop = bpf_map_op__clone(pos);
943                 if (!newop) {
944                         bpf_map_priv__purge(newpriv);
945                         return NULL;
946                 }
947                 list_add_tail(&newop->list, &newpriv->ops_list);
948         }
949
950         return newpriv;
951 }
952
953 static int
954 bpf_map__add_op(struct bpf_map *map, struct bpf_map_op *op)
955 {
956         const char *map_name = bpf_map__name(map);
957         struct bpf_map_priv *priv = bpf_map__priv(map);
958
959         if (IS_ERR(priv)) {
960                 pr_debug("Failed to get private from map %s\n", map_name);
961                 return PTR_ERR(priv);
962         }
963
964         if (!priv) {
965                 priv = zalloc(sizeof(*priv));
966                 if (!priv) {
967                         pr_debug("Not enough memory to alloc map private\n");
968                         return -ENOMEM;
969                 }
970                 INIT_LIST_HEAD(&priv->ops_list);
971
972                 if (bpf_map__set_priv(map, priv, bpf_map_priv__clear)) {
973                         free(priv);
974                         return -BPF_LOADER_ERRNO__INTERNAL;
975                 }
976         }
977
978         list_add_tail(&op->list, &priv->ops_list);
979         return 0;
980 }
981
982 static struct bpf_map_op *
983 bpf_map__add_newop(struct bpf_map *map, struct parse_events_term *term)
984 {
985         struct bpf_map_op *op;
986         int err;
987
988         op = bpf_map_op__new(term);
989         if (IS_ERR(op))
990                 return op;
991
992         err = bpf_map__add_op(map, op);
993         if (err) {
994                 bpf_map_op__delete(op);
995                 return ERR_PTR(err);
996         }
997         return op;
998 }
999
1000 static int
1001 __bpf_map__config_value(struct bpf_map *map,
1002                         struct parse_events_term *term)
1003 {
1004         struct bpf_map_op *op;
1005         const char *map_name = bpf_map__name(map);
1006         const struct bpf_map_def *def = bpf_map__def(map);
1007
1008         if (IS_ERR(def)) {
1009                 pr_debug("Unable to get map definition from '%s'\n",
1010                          map_name);
1011                 return -BPF_LOADER_ERRNO__INTERNAL;
1012         }
1013
1014         if (def->type != BPF_MAP_TYPE_ARRAY) {
1015                 pr_debug("Map %s type is not BPF_MAP_TYPE_ARRAY\n",
1016                          map_name);
1017                 return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
1018         }
1019         if (def->key_size < sizeof(unsigned int)) {
1020                 pr_debug("Map %s has incorrect key size\n", map_name);
1021                 return -BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE;
1022         }
1023         switch (def->value_size) {
1024         case 1:
1025         case 2:
1026         case 4:
1027         case 8:
1028                 break;
1029         default:
1030                 pr_debug("Map %s has incorrect value size\n", map_name);
1031                 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE;
1032         }
1033
1034         op = bpf_map__add_newop(map, term);
1035         if (IS_ERR(op))
1036                 return PTR_ERR(op);
1037         op->op_type = BPF_MAP_OP_SET_VALUE;
1038         op->v.value = term->val.num;
1039         return 0;
1040 }
1041
1042 static int
1043 bpf_map__config_value(struct bpf_map *map,
1044                       struct parse_events_term *term,
1045                       struct perf_evlist *evlist __maybe_unused)
1046 {
1047         if (!term->err_val) {
1048                 pr_debug("Config value not set\n");
1049                 return -BPF_LOADER_ERRNO__OBJCONF_CONF;
1050         }
1051
1052         if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) {
1053                 pr_debug("ERROR: wrong value type for 'value'\n");
1054                 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE;
1055         }
1056
1057         return __bpf_map__config_value(map, term);
1058 }
1059
1060 static int
1061 __bpf_map__config_event(struct bpf_map *map,
1062                         struct parse_events_term *term,
1063                         struct perf_evlist *evlist)
1064 {
1065         struct perf_evsel *evsel;
1066         const struct bpf_map_def *def;
1067         struct bpf_map_op *op;
1068         const char *map_name = bpf_map__name(map);
1069
1070         evsel = perf_evlist__find_evsel_by_str(evlist, term->val.str);
1071         if (!evsel) {
1072                 pr_debug("Event (for '%s') '%s' doesn't exist\n",
1073                          map_name, term->val.str);
1074                 return -BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT;
1075         }
1076
1077         def = bpf_map__def(map);
1078         if (IS_ERR(def)) {
1079                 pr_debug("Unable to get map definition from '%s'\n",
1080                          map_name);
1081                 return PTR_ERR(def);
1082         }
1083
1084         /*
1085          * No need to check key_size and value_size:
1086          * kernel has already checked them.
1087          */
1088         if (def->type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
1089                 pr_debug("Map %s type is not BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
1090                          map_name);
1091                 return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
1092         }
1093
1094         op = bpf_map__add_newop(map, term);
1095         if (IS_ERR(op))
1096                 return PTR_ERR(op);
1097         op->op_type = BPF_MAP_OP_SET_EVSEL;
1098         op->v.evsel = evsel;
1099         return 0;
1100 }
1101
1102 static int
1103 bpf_map__config_event(struct bpf_map *map,
1104                       struct parse_events_term *term,
1105                       struct perf_evlist *evlist)
1106 {
1107         if (!term->err_val) {
1108                 pr_debug("Config value not set\n");
1109                 return -BPF_LOADER_ERRNO__OBJCONF_CONF;
1110         }
1111
1112         if (term->type_val != PARSE_EVENTS__TERM_TYPE_STR) {
1113                 pr_debug("ERROR: wrong value type for 'event'\n");
1114                 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE;
1115         }
1116
1117         return __bpf_map__config_event(map, term, evlist);
1118 }
1119
1120 struct bpf_obj_config__map_func {
1121         const char *config_opt;
1122         int (*config_func)(struct bpf_map *, struct parse_events_term *,
1123                            struct perf_evlist *);
1124 };
1125
1126 struct bpf_obj_config__map_func bpf_obj_config__map_funcs[] = {
1127         {"value", bpf_map__config_value},
1128         {"event", bpf_map__config_event},
1129 };
1130
1131 static int
1132 config_map_indices_range_check(struct parse_events_term *term,
1133                                struct bpf_map *map,
1134                                const char *map_name)
1135 {
1136         struct parse_events_array *array = &term->array;
1137         const struct bpf_map_def *def;
1138         unsigned int i;
1139
1140         if (!array->nr_ranges)
1141                 return 0;
1142         if (!array->ranges) {
1143                 pr_debug("ERROR: map %s: array->nr_ranges is %d but range array is NULL\n",
1144                          map_name, (int)array->nr_ranges);
1145                 return -BPF_LOADER_ERRNO__INTERNAL;
1146         }
1147
1148         def = bpf_map__def(map);
1149         if (IS_ERR(def)) {
1150                 pr_debug("ERROR: Unable to get map definition from '%s'\n",
1151                          map_name);
1152                 return -BPF_LOADER_ERRNO__INTERNAL;
1153         }
1154
1155         for (i = 0; i < array->nr_ranges; i++) {
1156                 unsigned int start = array->ranges[i].start;
1157                 size_t length = array->ranges[i].length;
1158                 unsigned int idx = start + length - 1;
1159
1160                 if (idx >= def->max_entries) {
1161                         pr_debug("ERROR: index %d too large\n", idx);
1162                         return -BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG;
1163                 }
1164         }
1165         return 0;
1166 }
1167
1168 static int
1169 bpf__obj_config_map(struct bpf_object *obj,
1170                     struct parse_events_term *term,
1171                     struct perf_evlist *evlist,
1172                     int *key_scan_pos)
1173 {
1174         /* key is "map:<mapname>.<config opt>" */
1175         char *map_name = strdup(term->config + sizeof("map:") - 1);
1176         struct bpf_map *map;
1177         int err = -BPF_LOADER_ERRNO__OBJCONF_OPT;
1178         char *map_opt;
1179         size_t i;
1180
1181         if (!map_name)
1182                 return -ENOMEM;
1183
1184         map_opt = strchr(map_name, '.');
1185         if (!map_opt) {
1186                 pr_debug("ERROR: Invalid map config: %s\n", map_name);
1187                 goto out;
1188         }
1189
1190         *map_opt++ = '\0';
1191         if (*map_opt == '\0') {
1192                 pr_debug("ERROR: Invalid map option: %s\n", term->config);
1193                 goto out;
1194         }
1195
1196         map = bpf_object__find_map_by_name(obj, map_name);
1197         if (!map) {
1198                 pr_debug("ERROR: Map %s doesn't exist\n", map_name);
1199                 err = -BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST;
1200                 goto out;
1201         }
1202
1203         *key_scan_pos += strlen(map_opt);
1204         err = config_map_indices_range_check(term, map, map_name);
1205         if (err)
1206                 goto out;
1207         *key_scan_pos -= strlen(map_opt);
1208
1209         for (i = 0; i < ARRAY_SIZE(bpf_obj_config__map_funcs); i++) {
1210                 struct bpf_obj_config__map_func *func =
1211                                 &bpf_obj_config__map_funcs[i];
1212
1213                 if (strcmp(map_opt, func->config_opt) == 0) {
1214                         err = func->config_func(map, term, evlist);
1215                         goto out;
1216                 }
1217         }
1218
1219         pr_debug("ERROR: Invalid map config option '%s'\n", map_opt);
1220         err = -BPF_LOADER_ERRNO__OBJCONF_MAP_OPT;
1221 out:
1222         free(map_name);
1223         if (!err)
1224                 key_scan_pos += strlen(map_opt);
1225         return err;
1226 }
1227
1228 int bpf__config_obj(struct bpf_object *obj,
1229                     struct parse_events_term *term,
1230                     struct perf_evlist *evlist,
1231                     int *error_pos)
1232 {
1233         int key_scan_pos = 0;
1234         int err;
1235
1236         if (!obj || !term || !term->config)
1237                 return -EINVAL;
1238
1239         if (strstarts(term->config, "map:")) {
1240                 key_scan_pos = sizeof("map:") - 1;
1241                 err = bpf__obj_config_map(obj, term, evlist, &key_scan_pos);
1242                 goto out;
1243         }
1244         err = -BPF_LOADER_ERRNO__OBJCONF_OPT;
1245 out:
1246         if (error_pos)
1247                 *error_pos = key_scan_pos;
1248         return err;
1249
1250 }
1251
1252 typedef int (*map_config_func_t)(const char *name, int map_fd,
1253                                  const struct bpf_map_def *pdef,
1254                                  struct bpf_map_op *op,
1255                                  void *pkey, void *arg);
1256
1257 static int
1258 foreach_key_array_all(map_config_func_t func,
1259                       void *arg, const char *name,
1260                       int map_fd, const struct bpf_map_def *pdef,
1261                       struct bpf_map_op *op)
1262 {
1263         unsigned int i;
1264         int err;
1265
1266         for (i = 0; i < pdef->max_entries; i++) {
1267                 err = func(name, map_fd, pdef, op, &i, arg);
1268                 if (err) {
1269                         pr_debug("ERROR: failed to insert value to %s[%u]\n",
1270                                  name, i);
1271                         return err;
1272                 }
1273         }
1274         return 0;
1275 }
1276
1277 static int
1278 foreach_key_array_ranges(map_config_func_t func, void *arg,
1279                          const char *name, int map_fd,
1280                          const struct bpf_map_def *pdef,
1281                          struct bpf_map_op *op)
1282 {
1283         unsigned int i, j;
1284         int err;
1285
1286         for (i = 0; i < op->k.array.nr_ranges; i++) {
1287                 unsigned int start = op->k.array.ranges[i].start;
1288                 size_t length = op->k.array.ranges[i].length;
1289
1290                 for (j = 0; j < length; j++) {
1291                         unsigned int idx = start + j;
1292
1293                         err = func(name, map_fd, pdef, op, &idx, arg);
1294                         if (err) {
1295                                 pr_debug("ERROR: failed to insert value to %s[%u]\n",
1296                                          name, idx);
1297                                 return err;
1298                         }
1299                 }
1300         }
1301         return 0;
1302 }
1303
1304 static int
1305 bpf_map_config_foreach_key(struct bpf_map *map,
1306                            map_config_func_t func,
1307                            void *arg)
1308 {
1309         int err, map_fd;
1310         struct bpf_map_op *op;
1311         const struct bpf_map_def *def;
1312         const char *name = bpf_map__name(map);
1313         struct bpf_map_priv *priv = bpf_map__priv(map);
1314
1315         if (IS_ERR(priv)) {
1316                 pr_debug("ERROR: failed to get private from map %s\n", name);
1317                 return -BPF_LOADER_ERRNO__INTERNAL;
1318         }
1319         if (!priv || list_empty(&priv->ops_list)) {
1320                 pr_debug("INFO: nothing to config for map %s\n", name);
1321                 return 0;
1322         }
1323
1324         def = bpf_map__def(map);
1325         if (IS_ERR(def)) {
1326                 pr_debug("ERROR: failed to get definition from map %s\n", name);
1327                 return -BPF_LOADER_ERRNO__INTERNAL;
1328         }
1329         map_fd = bpf_map__fd(map);
1330         if (map_fd < 0) {
1331                 pr_debug("ERROR: failed to get fd from map %s\n", name);
1332                 return map_fd;
1333         }
1334
1335         list_for_each_entry(op, &priv->ops_list, list) {
1336                 switch (def->type) {
1337                 case BPF_MAP_TYPE_ARRAY:
1338                 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1339                         switch (op->key_type) {
1340                         case BPF_MAP_KEY_ALL:
1341                                 err = foreach_key_array_all(func, arg, name,
1342                                                             map_fd, def, op);
1343                                 break;
1344                         case BPF_MAP_KEY_RANGES:
1345                                 err = foreach_key_array_ranges(func, arg, name,
1346                                                                map_fd, def,
1347                                                                op);
1348                                 break;
1349                         default:
1350                                 pr_debug("ERROR: keytype for map '%s' invalid\n",
1351                                          name);
1352                                 return -BPF_LOADER_ERRNO__INTERNAL;
1353                         }
1354                         if (err)
1355                                 return err;
1356                         break;
1357                 default:
1358                         pr_debug("ERROR: type of '%s' incorrect\n", name);
1359                         return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE;
1360                 }
1361         }
1362
1363         return 0;
1364 }
1365
1366 static int
1367 apply_config_value_for_key(int map_fd, void *pkey,
1368                            size_t val_size, u64 val)
1369 {
1370         int err = 0;
1371
1372         switch (val_size) {
1373         case 1: {
1374                 u8 _val = (u8)(val);
1375                 err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
1376                 break;
1377         }
1378         case 2: {
1379                 u16 _val = (u16)(val);
1380                 err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
1381                 break;
1382         }
1383         case 4: {
1384                 u32 _val = (u32)(val);
1385                 err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY);
1386                 break;
1387         }
1388         case 8: {
1389                 err = bpf_map_update_elem(map_fd, pkey, &val, BPF_ANY);
1390                 break;
1391         }
1392         default:
1393                 pr_debug("ERROR: invalid value size\n");
1394                 return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE;
1395         }
1396         if (err && errno)
1397                 err = -errno;
1398         return err;
1399 }
1400
1401 static int
1402 apply_config_evsel_for_key(const char *name, int map_fd, void *pkey,
1403                            struct perf_evsel *evsel)
1404 {
1405         struct xyarray *xy = evsel->fd;
1406         struct perf_event_attr *attr;
1407         unsigned int key, events;
1408         bool check_pass = false;
1409         int *evt_fd;
1410         int err;
1411
1412         if (!xy) {
1413                 pr_debug("ERROR: evsel not ready for map %s\n", name);
1414                 return -BPF_LOADER_ERRNO__INTERNAL;
1415         }
1416
1417         if (xy->row_size / xy->entry_size != 1) {
1418                 pr_debug("ERROR: Dimension of target event is incorrect for map %s\n",
1419                          name);
1420                 return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM;
1421         }
1422
1423         attr = &evsel->attr;
1424         if (attr->inherit) {
1425                 pr_debug("ERROR: Can't put inherit event into map %s\n", name);
1426                 return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH;
1427         }
1428
1429         if (perf_evsel__is_bpf_output(evsel))
1430                 check_pass = true;
1431         if (attr->type == PERF_TYPE_RAW)
1432                 check_pass = true;
1433         if (attr->type == PERF_TYPE_HARDWARE)
1434                 check_pass = true;
1435         if (!check_pass) {
1436                 pr_debug("ERROR: Event type is wrong for map %s\n", name);
1437                 return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE;
1438         }
1439
1440         events = xy->entries / (xy->row_size / xy->entry_size);
1441         key = *((unsigned int *)pkey);
1442         if (key >= events) {
1443                 pr_debug("ERROR: there is no event %d for map %s\n",
1444                          key, name);
1445                 return -BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE;
1446         }
1447         evt_fd = xyarray__entry(xy, key, 0);
1448         err = bpf_map_update_elem(map_fd, pkey, evt_fd, BPF_ANY);
1449         if (err && errno)
1450                 err = -errno;
1451         return err;
1452 }
1453
1454 static int
1455 apply_obj_config_map_for_key(const char *name, int map_fd,
1456                              const struct bpf_map_def *pdef,
1457                              struct bpf_map_op *op,
1458                              void *pkey, void *arg __maybe_unused)
1459 {
1460         int err;
1461
1462         switch (op->op_type) {
1463         case BPF_MAP_OP_SET_VALUE:
1464                 err = apply_config_value_for_key(map_fd, pkey,
1465                                                  pdef->value_size,
1466                                                  op->v.value);
1467                 break;
1468         case BPF_MAP_OP_SET_EVSEL:
1469                 err = apply_config_evsel_for_key(name, map_fd, pkey,
1470                                                  op->v.evsel);
1471                 break;
1472         default:
1473                 pr_debug("ERROR: unknown value type for '%s'\n", name);
1474                 err = -BPF_LOADER_ERRNO__INTERNAL;
1475         }
1476         return err;
1477 }
1478
1479 static int
1480 apply_obj_config_map(struct bpf_map *map)
1481 {
1482         return bpf_map_config_foreach_key(map,
1483                                           apply_obj_config_map_for_key,
1484                                           NULL);
1485 }
1486
1487 static int
1488 apply_obj_config_object(struct bpf_object *obj)
1489 {
1490         struct bpf_map *map;
1491         int err;
1492
1493         bpf_object__for_each_map(map, obj) {
1494                 err = apply_obj_config_map(map);
1495                 if (err)
1496                         return err;
1497         }
1498         return 0;
1499 }
1500
1501 int bpf__apply_obj_config(void)
1502 {
1503         struct bpf_object *obj, *tmp;
1504         int err;
1505
1506         bpf_object__for_each_safe(obj, tmp) {
1507                 err = apply_obj_config_object(obj);
1508                 if (err)
1509                         return err;
1510         }
1511
1512         return 0;
1513 }
1514
1515 #define bpf__for_each_map(pos, obj, objtmp)     \
1516         bpf_object__for_each_safe(obj, objtmp)  \
1517                 bpf_object__for_each_map(pos, obj)
1518
1519 #define bpf__for_each_map_named(pos, obj, objtmp, name) \
1520         bpf__for_each_map(pos, obj, objtmp)             \
1521                 if (bpf_map__name(pos) &&               \
1522                         (strcmp(name,                   \
1523                                 bpf_map__name(pos)) == 0))
1524
1525 struct perf_evsel *bpf__setup_output_event(struct perf_evlist *evlist, const char *name)
1526 {
1527         struct bpf_map_priv *tmpl_priv = NULL;
1528         struct bpf_object *obj, *tmp;
1529         struct perf_evsel *evsel = NULL;
1530         struct bpf_map *map;
1531         int err;
1532         bool need_init = false;
1533
1534         bpf__for_each_map_named(map, obj, tmp, name) {
1535                 struct bpf_map_priv *priv = bpf_map__priv(map);
1536
1537                 if (IS_ERR(priv))
1538                         return ERR_PTR(-BPF_LOADER_ERRNO__INTERNAL);
1539
1540                 /*
1541                  * No need to check map type: type should have been
1542                  * verified by kernel.
1543                  */
1544                 if (!need_init && !priv)
1545                         need_init = !priv;
1546                 if (!tmpl_priv && priv)
1547                         tmpl_priv = priv;
1548         }
1549
1550         if (!need_init)
1551                 return NULL;
1552
1553         if (!tmpl_priv) {
1554                 char *event_definition = NULL;
1555
1556                 if (asprintf(&event_definition, "bpf-output/no-inherit=1,name=%s/", name) < 0)
1557                         return ERR_PTR(-ENOMEM);
1558
1559                 err = parse_events(evlist, event_definition, NULL);
1560                 free(event_definition);
1561
1562                 if (err) {
1563                         pr_debug("ERROR: failed to create the \"%s\" bpf-output event\n", name);
1564                         return ERR_PTR(-err);
1565                 }
1566
1567                 evsel = perf_evlist__last(evlist);
1568         }
1569
1570         bpf__for_each_map_named(map, obj, tmp, name) {
1571                 struct bpf_map_priv *priv = bpf_map__priv(map);
1572
1573                 if (IS_ERR(priv))
1574                         return ERR_PTR(-BPF_LOADER_ERRNO__INTERNAL);
1575                 if (priv)
1576                         continue;
1577
1578                 if (tmpl_priv) {
1579                         priv = bpf_map_priv__clone(tmpl_priv);
1580                         if (!priv)
1581                                 return ERR_PTR(-ENOMEM);
1582
1583                         err = bpf_map__set_priv(map, priv, bpf_map_priv__clear);
1584                         if (err) {
1585                                 bpf_map_priv__clear(map, priv);
1586                                 return ERR_PTR(err);
1587                         }
1588                 } else if (evsel) {
1589                         struct bpf_map_op *op;
1590
1591                         op = bpf_map__add_newop(map, NULL);
1592                         if (IS_ERR(op))
1593                                 return ERR_CAST(op);
1594                         op->op_type = BPF_MAP_OP_SET_EVSEL;
1595                         op->v.evsel = evsel;
1596                 }
1597         }
1598
1599         return evsel;
1600 }
1601
1602 int bpf__setup_stdout(struct perf_evlist *evlist)
1603 {
1604         struct perf_evsel *evsel = bpf__setup_output_event(evlist, "__bpf_stdout__");
1605         return PTR_ERR_OR_ZERO(evsel);
1606 }
1607
1608 #define ERRNO_OFFSET(e)         ((e) - __BPF_LOADER_ERRNO__START)
1609 #define ERRCODE_OFFSET(c)       ERRNO_OFFSET(BPF_LOADER_ERRNO__##c)
1610 #define NR_ERRNO        (__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START)
1611
1612 static const char *bpf_loader_strerror_table[NR_ERRNO] = {
1613         [ERRCODE_OFFSET(CONFIG)]        = "Invalid config string",
1614         [ERRCODE_OFFSET(GROUP)]         = "Invalid group name",
1615         [ERRCODE_OFFSET(EVENTNAME)]     = "No event name found in config string",
1616         [ERRCODE_OFFSET(INTERNAL)]      = "BPF loader internal error",
1617         [ERRCODE_OFFSET(COMPILE)]       = "Error when compiling BPF scriptlet",
1618         [ERRCODE_OFFSET(PROGCONF_TERM)] = "Invalid program config term in config string",
1619         [ERRCODE_OFFSET(PROLOGUE)]      = "Failed to generate prologue",
1620         [ERRCODE_OFFSET(PROLOGUE2BIG)]  = "Prologue too big for program",
1621         [ERRCODE_OFFSET(PROLOGUEOOB)]   = "Offset out of bound for prologue",
1622         [ERRCODE_OFFSET(OBJCONF_OPT)]   = "Invalid object config option",
1623         [ERRCODE_OFFSET(OBJCONF_CONF)]  = "Config value not set (missing '=')",
1624         [ERRCODE_OFFSET(OBJCONF_MAP_OPT)]       = "Invalid object map config option",
1625         [ERRCODE_OFFSET(OBJCONF_MAP_NOTEXIST)]  = "Target map doesn't exist",
1626         [ERRCODE_OFFSET(OBJCONF_MAP_VALUE)]     = "Incorrect value type for map",
1627         [ERRCODE_OFFSET(OBJCONF_MAP_TYPE)]      = "Incorrect map type",
1628         [ERRCODE_OFFSET(OBJCONF_MAP_KEYSIZE)]   = "Incorrect map key size",
1629         [ERRCODE_OFFSET(OBJCONF_MAP_VALUESIZE)] = "Incorrect map value size",
1630         [ERRCODE_OFFSET(OBJCONF_MAP_NOEVT)]     = "Event not found for map setting",
1631         [ERRCODE_OFFSET(OBJCONF_MAP_MAPSIZE)]   = "Invalid map size for event setting",
1632         [ERRCODE_OFFSET(OBJCONF_MAP_EVTDIM)]    = "Event dimension too large",
1633         [ERRCODE_OFFSET(OBJCONF_MAP_EVTINH)]    = "Doesn't support inherit event",
1634         [ERRCODE_OFFSET(OBJCONF_MAP_EVTTYPE)]   = "Wrong event type for map",
1635         [ERRCODE_OFFSET(OBJCONF_MAP_IDX2BIG)]   = "Index too large",
1636 };
1637
1638 static int
1639 bpf_loader_strerror(int err, char *buf, size_t size)
1640 {
1641         char sbuf[STRERR_BUFSIZE];
1642         const char *msg;
1643
1644         if (!buf || !size)
1645                 return -1;
1646
1647         err = err > 0 ? err : -err;
1648
1649         if (err >= __LIBBPF_ERRNO__START)
1650                 return libbpf_strerror(err, buf, size);
1651
1652         if (err >= __BPF_LOADER_ERRNO__START && err < __BPF_LOADER_ERRNO__END) {
1653                 msg = bpf_loader_strerror_table[ERRNO_OFFSET(err)];
1654                 snprintf(buf, size, "%s", msg);
1655                 buf[size - 1] = '\0';
1656                 return 0;
1657         }
1658
1659         if (err >= __BPF_LOADER_ERRNO__END)
1660                 snprintf(buf, size, "Unknown bpf loader error %d", err);
1661         else
1662                 snprintf(buf, size, "%s",
1663                          str_error_r(err, sbuf, sizeof(sbuf)));
1664
1665         buf[size - 1] = '\0';
1666         return -1;
1667 }
1668
1669 #define bpf__strerror_head(err, buf, size) \
1670         char sbuf[STRERR_BUFSIZE], *emsg;\
1671         if (!size)\
1672                 return 0;\
1673         if (err < 0)\
1674                 err = -err;\
1675         bpf_loader_strerror(err, sbuf, sizeof(sbuf));\
1676         emsg = sbuf;\
1677         switch (err) {\
1678         default:\
1679                 scnprintf(buf, size, "%s", emsg);\
1680                 break;
1681
1682 #define bpf__strerror_entry(val, fmt...)\
1683         case val: {\
1684                 scnprintf(buf, size, fmt);\
1685                 break;\
1686         }
1687
1688 #define bpf__strerror_end(buf, size)\
1689         }\
1690         buf[size - 1] = '\0';
1691
1692 int bpf__strerror_prepare_load(const char *filename, bool source,
1693                                int err, char *buf, size_t size)
1694 {
1695         size_t n;
1696         int ret;
1697
1698         n = snprintf(buf, size, "Failed to load %s%s: ",
1699                          filename, source ? " from source" : "");
1700         if (n >= size) {
1701                 buf[size - 1] = '\0';
1702                 return 0;
1703         }
1704         buf += n;
1705         size -= n;
1706
1707         ret = bpf_loader_strerror(err, buf, size);
1708         buf[size - 1] = '\0';
1709         return ret;
1710 }
1711
1712 int bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
1713                         int err, char *buf, size_t size)
1714 {
1715         bpf__strerror_head(err, buf, size);
1716         case BPF_LOADER_ERRNO__PROGCONF_TERM: {
1717                 scnprintf(buf, size, "%s (add -v to see detail)", emsg);
1718                 break;
1719         }
1720         bpf__strerror_entry(EEXIST, "Probe point exist. Try 'perf probe -d \"*\"' and set 'force=yes'");
1721         bpf__strerror_entry(EACCES, "You need to be root");
1722         bpf__strerror_entry(EPERM, "You need to be root, and /proc/sys/kernel/kptr_restrict should be 0");
1723         bpf__strerror_entry(ENOENT, "You need to check probing points in BPF file");
1724         bpf__strerror_end(buf, size);
1725         return 0;
1726 }
1727
1728 int bpf__strerror_load(struct bpf_object *obj,
1729                        int err, char *buf, size_t size)
1730 {
1731         bpf__strerror_head(err, buf, size);
1732         case LIBBPF_ERRNO__KVER: {
1733                 unsigned int obj_kver = bpf_object__kversion(obj);
1734                 unsigned int real_kver;
1735
1736                 if (fetch_kernel_version(&real_kver, NULL, 0)) {
1737                         scnprintf(buf, size, "Unable to fetch kernel version");
1738                         break;
1739                 }
1740
1741                 if (obj_kver != real_kver) {
1742                         scnprintf(buf, size,
1743                                   "'version' ("KVER_FMT") doesn't match running kernel ("KVER_FMT")",
1744                                   KVER_PARAM(obj_kver),
1745                                   KVER_PARAM(real_kver));
1746                         break;
1747                 }
1748
1749                 scnprintf(buf, size, "Failed to load program for unknown reason");
1750                 break;
1751         }
1752         bpf__strerror_end(buf, size);
1753         return 0;
1754 }
1755
1756 int bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
1757                              struct parse_events_term *term __maybe_unused,
1758                              struct perf_evlist *evlist __maybe_unused,
1759                              int *error_pos __maybe_unused, int err,
1760                              char *buf, size_t size)
1761 {
1762         bpf__strerror_head(err, buf, size);
1763         bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE,
1764                             "Can't use this config term with this map type");
1765         bpf__strerror_end(buf, size);
1766         return 0;
1767 }
1768
1769 int bpf__strerror_apply_obj_config(int err, char *buf, size_t size)
1770 {
1771         bpf__strerror_head(err, buf, size);
1772         bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM,
1773                             "Cannot set event to BPF map in multi-thread tracing");
1774         bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH,
1775                             "%s (Hint: use -i to turn off inherit)", emsg);
1776         bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE,
1777                             "Can only put raw, hardware and BPF output event into a BPF map");
1778         bpf__strerror_end(buf, size);
1779         return 0;
1780 }
1781
1782 int bpf__strerror_setup_output_event(struct perf_evlist *evlist __maybe_unused,
1783                                      int err, char *buf, size_t size)
1784 {
1785         bpf__strerror_head(err, buf, size);
1786         bpf__strerror_end(buf, size);
1787         return 0;
1788 }