Re: [PATCH bpf-next v5 2/7] bpf: Support associating BPF program with struct_ops

Amery Hung
2025-11-04T23:26:21Z

On Tue, Nov 4, 2025 at 2:00 PM Song Liu <songliubraving@meta.com> wrote:
>
>
>
> > On Nov 4, 2025, at 9:26 AM, Amery Hung <ameryhung@gmail.com> wrote:
> >
> > Add a new BPF command BPF_PROG_ASSOC_STRUCT_OPS to allow associating
> > a BPF program with a struct_ops map. This command takes a file
> > descriptor of a struct_ops map and a BPF program and set
> > prog->aux->st_ops_assoc to the kdata of the struct_ops map.
> >
> > The command does not accept a struct_ops program nor a non-struct_ops
> > map. Programs of a struct_ops map is automatically associated with the
> > map during map update. If a program is shared between two struct_ops
> > maps, prog->aux->st_ops_assoc will be poisoned to indicate that the
> > associated struct_ops is ambiguous. The pointer, once poisoned, cannot
> > be reset since we have lost track of associated struct_ops. For other
> > program types, the associated struct_ops map, once set, cannot be
> > changed later. This restriction may be lifted in the future if there is
> > a use case.
> >
> > A kernel helper bpf_prog_get_assoc_struct_ops() can be used to retrieve
> > the associated struct_ops pointer. The returned pointer, if not NULL, is
> > guaranteed to be valid and point to a fully updated struct_ops struct.
> > For struct_ops program reused in multiple struct_ops map, the return
> > will be NULL.
> >
> > To make sure the returned pointer to be valid, the command increases the
> > refcount of the map for every associated non-struct_ops programs. For
> > struct_ops programs, the destruction of a struct_ops map already waits for
> > its BPF programs to finish running. A later patch will further make sure
> > the map will not be freed when an async callback schedule from struct_ops
> > is running.
> >
> > struct_ops implementers should note that the struct_ops returned may or
> > may not be attached. The struct_ops implementer will be responsible for
> > tracking and checking the state of the associated struct_ops map if the
> > use case requires an attached struct_ops.
> >
> > Signed-off-by: Amery Hung <ameryhung@gmail.com>
> > ---
> > include/linux/bpf.h | 16 ++++++
> > include/uapi/linux/bpf.h | 17 +++++++
> > kernel/bpf/bpf_struct_ops.c | 90 ++++++++++++++++++++++++++++++++++
> > kernel/bpf/core.c | 3 ++
> > kernel/bpf/syscall.c | 46 +++++++++++++++++
> > tools/include/uapi/linux/bpf.h | 17 +++++++
> > 6 files changed, 189 insertions(+)
> >
> > diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> > index a47d67db3be5..0f71030c03e1 100644
> > --- a/include/linux/bpf.h
> > +++ b/include/linux/bpf.h
> > @@ -1726,6 +1726,8 @@ struct bpf_prog_aux {
> > struct rcu_head rcu;
> > };
> > struct bpf_stream stream[2];
> > + struct mutex st_ops_assoc_mutex;
> > + struct bpf_map *st_ops_assoc;
> > };
>
> In the bpf-oom thread, we agreed (mostly agreed?) that we will allow
> attaching a struct_ops map multiple times.
>
> To match this design, shall we associate a BPF program with a
> bpf_struct_ops_link instead of bpf_map? This requires one more
> pointer deref to get the pointer to the struct_ops map. But the
> solution will be more future proof.
>
> Does this make sense?

I think it makes sense and can be a future work to have the ability to
associate attachments. The command can be extended to take a link to
struct_ops map and a link to bpf program.

For this patchset, I think firstly we should still aim for providing a
way to associate the implementation (think of it as C++ class).

>
> Thanks,
> Song
>
>
>