From: Hui Zhu <[email protected]> To support features like allowing overrides in cgroup hierarchies, we need a way to pass flags from userspace to the kernel when attaching a struct_ops.
Extend `bpf_struct_ops_link` to include a `flags` field. This field is populated from `attr->link_create.flags` during link creation. This will allow struct_ops implementations, such as the upcoming memory controller ops, to interpret these flags and modify their attachment behavior accordingly. The flags validation in bpf_struct_ops_link_create() is updated to explicitly permit BPF_F_ALLOW_OVERRIDE in addition to the already-allowed BPF_F_CGROUP_FD. Any other flag combination will still be rejected with -EINVAL. UAPI Change: This patch updates the comment in include/uapi/linux/bpf.h to reflect that the cgroup-bpf attach flags (such as BPF_F_ALLOW_OVERRIDE) are now applicable to both BPF_PROG_ATTACH and BPF_LINK_CREATE commands. Previously, these flags were only documented for BPF_PROG_ATTACH. The actual flag definitions remain unchanged, so this is a compatible extension of the existing API. Older userspace will continue to work (by not passing flags), and newer userspace can opt-in to the new functionality by setting appropriate flags. Signed-off-by: Barry Song <[email protected]> Signed-off-by: Geliang Tang <[email protected]> Signed-off-by: Hui Zhu <[email protected]> --- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 2 +- kernel/bpf/bpf_struct_ops.c | 4 +++- tools/include/uapi/linux/bpf.h | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 743b4f0546b5..aae7f9837944 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1915,6 +1915,7 @@ struct bpf_struct_ops_link { bool cgroup_removed; struct list_head list; wait_queue_head_t wait_hup; + u32 flags; }; struct bpf_link_primer { diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index f547613986cc..85ab5bdf81ac 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1194,7 +1194,7 @@ enum bpf_perf_event_type { BPF_PERF_EVENT_EVENT = 6, }; -/* cgroup-bpf attach flags used in BPF_PROG_ATTACH command +/* cgroup-bpf attach flags used in BPF_PROG_ATTACH and BPF_LINK_CREATE command * * NONE(default): No further bpf programs allowed in the subtree. * diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index 5333290957cb..1d15c667a300 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -1389,7 +1389,8 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) struct cgroup *cgrp; int err; - if (attr->link_create.flags & ~BPF_F_CGROUP_FD) + if (attr->link_create.flags & ~(BPF_F_CGROUP_FD | + BPF_F_ALLOW_OVERRIDE)) return -EINVAL; map = bpf_map_get(attr->link_create.map_fd); @@ -1427,6 +1428,7 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) goto err_out; } } + link->flags = attr->link_create.flags; err = bpf_link_prime(&link->link, &link_primer); if (err) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index fa075dc3b7eb..8a2b1f865d2b 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1194,7 +1194,7 @@ enum bpf_perf_event_type { BPF_PERF_EVENT_EVENT = 6, }; -/* cgroup-bpf attach flags used in BPF_PROG_ATTACH command +/* cgroup-bpf attach flags used in BPF_PROG_ATTACH and BPF_LINK_CREATE command * * NONE(default): No further bpf programs allowed in the subtree. * -- 2.43.0

