libbpf / bpftool

Automated upstream mirror for bpftool stand-alone build.
Other
377 stars 69 forks source link

Missing bpffs mount when pinning maps for prog load (`pinmaps`) #146

Closed qmonnet closed 2 months ago

qmonnet commented 3 months ago

Looking at the code around mounting the bpffs in bpftool's prog.c, I see that we don't call mount_bpffs_for_pin() for the directory where the user wants to pin maps, if pinmaps is passed:

    err = mount_bpffs_for_pin(pinfile, !first_prog_only);
    if (err)
        goto err_close_obj;

    if (first_prog_only) {
        prog = bpf_object__next_program(obj, NULL);
        if (!prog) {
            p_err("object file doesn't contain any bpf program");
            goto err_close_obj;
        }

        if (auto_attach)
            err = auto_attach_program(prog, pinfile);
        else
            err = bpf_obj_pin(bpf_program__fd(prog), pinfile);
        if (err) {
            p_err("failed to pin program %s",
                  bpf_program__section_name(prog));
            goto err_close_obj;
        }
    } else {
        if (auto_attach)
            err = auto_attach_programs(obj, pinfile);
        else
            err = bpf_object__pin_programs(obj, pinfile);
        if (err) {
            p_err("failed to pin all programs");
            goto err_close_obj;
        }
    }

    if (pinmaps) {

        // XXX No guarantee that the bpffs is mounted for directory pinmaps, here

        err = bpf_object__pin_maps(obj, pinmaps);
        if (err) {
            p_err("failed to pin all maps");
            goto err_unpin;
        }
    }

I haven't tested it though, but I expect the map pinning to fail if we pass a pinmaps directory that is not under the bpffs.

Originally posted by @qmonnet in https://github.com/libbpf/bpftool/issues/100#issuecomment-1978460616

chentao-kernel commented 2 months ago

Hi, qmonnet The problem seems still exist in 6.10-rc4, when i run a test case:

libbpf: specified path /home/ubuntu/test/sock_ops_map is not on BPF FS                                                                       │
Error: failed to pin all maps

No one fixes it right?Maybe i can submit a patch

qmonnet commented 2 months ago

Correct, nobody has fixed the issue, and a patch is welcome, thanks! :)

chentao-kernel commented 2 months ago

Correct, nobody has fixed the issue, and a patch is welcome, thanks! :)

Thank you for your reply, i will send it later

qmonnet commented 2 months ago

https://lore.kernel.org/bpf/20240702131150.15622-1-chen.dylane@gmail.com/t/#u

qmonnet commented 2 months ago

Daniel just merged the patch to bpf-next, thanks a lot! It will land in this repo at the next sync.

chentao-kernel commented 2 months ago

Daniel just merged the patch to bpf-next, thanks a lot! It will land in this repo at the next sync.

Daniel just merged the patch to bpf-next, thanks a lot! It will land in this repo at the next sync.

Hi qmonnet, Thank you for merging my patch!