Skip to content

Commit e785522

Browse files
committed
samples/bpf: order of prog_fd[] must correspond with ELF order
It is highly problematic that the order of array prog_fd[] depend on whether or not a bpf program section if referencing any maps or not. I was implementing an XDP prog using tail calls, and it cause strange issues that I could not count on the prog_fd[] order of the program, when I was adjusting the programs... This caused several days of wasted debugging! :-( Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
1 parent 7e70565 commit e785522

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

kernel/samples/bpf/bpf_load.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,16 @@ int load_bpf_relocate_maps_and_attach(int fd)
366366
}
367367
}
368368

369-
/* load programs that need map fixup (relocations) */
369+
/* process all relo sections, and rewrite bpf insns for maps */
370370
for (i = 1; i < ehdr.e_shnum; i++) {
371371

372372
if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
373373
continue;
374+
374375
if (shdr.sh_type == SHT_REL) {
375376
struct bpf_insn *insns;
376377

378+
/* locate prog sec that need map fixup (relocations) */
377379
if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
378380
&shdr_prog, &data_prog))
379381
continue;
@@ -383,25 +385,14 @@ int load_bpf_relocate_maps_and_attach(int fd)
383385
continue;
384386

385387
insns = (struct bpf_insn *) data_prog->d_buf;
386-
387-
processed_sec[shdr.sh_info] = true;
388-
processed_sec[i] = true;
388+
processed_sec[i] = true; /* relo section */
389389

390390
if (parse_relo_and_apply(data, symbols, &shdr, insns))
391391
continue;
392-
393-
if (memcmp(shname_prog, "kprobe/", 7) == 0 ||
394-
memcmp(shname_prog, "kretprobe/", 10) == 0 ||
395-
memcmp(shname_prog, "tracepoint/", 11) == 0 ||
396-
memcmp(shname_prog, "xdp", 3) == 0 ||
397-
memcmp(shname_prog, "perf_event", 10) == 0 ||
398-
memcmp(shname_prog, "socket", 6) == 0 ||
399-
memcmp(shname_prog, "cgroup/", 7) == 0)
400-
load_and_attach(shname_prog, insns, data_prog->d_size);
401392
}
402393
}
403394

404-
/* load programs that don't use maps */
395+
/* load programs */
405396
for (i = 1; i < ehdr.e_shnum; i++) {
406397

407398
if (processed_sec[i])

0 commit comments

Comments
 (0)