Subject: Re: bpf: ability to attach freplace to multiple parents

From: Toke Høiland-Jørgensen <[email-masked]>
Date: Thu, 02 Apr 2020 23:23:12 +0200

Alexei Starovoitov <[email-masked]> writes:

> On Fri, Mar 27, 2020 at 12:11:15PM +0100, Toke Høiland-Jørgensen wrote:
>> 
>> Current code is in [0], for those following along. There are two bits of
>> kernel support missing before I can get it to where I want it for an
>> initial "release": Atomic replace of the dispatcher (this series), and
>> the ability to attach an freplace program to more than one "parent".
>> I'll try to get an RFC out for the latter during the merge window, but
>> I'll probably need some help in figuring out how to make it safe from
>> the verifier PoV.
>
> I have some thoughts on the second part "ability to attach an freplace
> to more than one 'parent'".
> I think the solution should be more generic than just freplace.
> fentry/fexit need to have the same feature.
> Few folks already said that they want to attach fentry to multiple
> kernel functions. It's similar to what people do with kprobe progs now.
> (attach to multiple and differentiate attach point based on parent IP)
> Similarly "bpftool profile" needs it to avoid creating new pair of fentry/fexit
> progs for every target bpf prog it's collecting stats about.
> I didn't add this ability to fentry/fexit/freplace only to simplify
> initial implementation ;) I think the time had come.

Yup, I agree that it makes sense to do the same for fentry/fexit.

> Currently fentry/fexit/freplace progs have single prog->aux->linked_prog pointer.
> It just needs to become a linked list.
> The api extension could be like this:
> bpf_raw_tp_open(prog_fd, attach_prog_fd, attach_btf_id);
> (currently it's just bpf_raw_tp_open(prog_fd))
> The same pair of (attach_prog_fd, attach_btf_id) is already passed into prog_load
> to hold the linked_prog and its corresponding btf_id.
> I'm proposing to extend raw_tp_open with this pair as well to
> attach existing fentry/fexit/freplace prog to another target.
> Internally the kernel verify that btf of current linked_prog
> exactly matches to btf of another requested linked_prog and
> if they match it will attach the same prog to two target programs (in case of freplace)
> or two kernel functions (in case of fentry/fexit).

API-wise this was exactly what I had in mind as well.

> Toke, Andrey,
> if above kinda makes sense from high level description
> I can prototype it quickly and then we can discuss details
> in the patches ?
> Or we can drill further into details and discuss corner cases.

I have one detail to discuss: What would the bpf_raw_tp_open() call
return on the second attachment? A second reference to the same bpf_link
fd as the initial attachment, or a different link?

For the dispatcher use case, the former would make sense: If the
bpf_link is returned to the application as a canonical reference to its
program's attachment, it should persist even when the dispatcher program
itself is replaced from underneath it. But I'm not sure if the same is
true for all such secondary attachments?

-Toke