bpf: Implement mprog API on top of existing cgroup progs
Subsystems (6)
Yonghong Song
reviewercover bpf: Implement mprog API on top of existing cgroup progs
Current cgroup prog ordering is appending at attachment time. This is not ideal. In some cases, users want specific ordering at a particular cgroup level. For example, in Meta, we have a case where three different applications all have cgroup/setsockopt progs and they require specific ordering. Current approach is to use a bpfchainer where one bpf prog contains multiple global functions and each global function can be freplaced by a prog for a specific application. The ordering of global functions decides the ordering of those application specific bpf progs. Using bpftrainer is a centralized approach and is not desirable as one of applications acts as a deamon. The decentralized attachment approach is more favorable for those applications.
To address this, the existing mprog API ([2]) seems an ideal solution with supporting BPF_F_BEFORE and BPF_F_AFTER flags on top of existing cgroup bpf implementation. More specifically, the support is added for prog/link attachment with BPF_F_BEFORE and BPF_F_AFTER. The kernel mprog interface ([2]) is not used and the implementation is directly done in cgroup bpf code base. The mprog 'revision' is also implemented in attach/detach/replace, so users can query revision number to check the change of cgroup prog list.
The patch set contains 4 patches. Patch 1 adds revision support for cgroup bpf progs. Patch 2 implements mprog API implementation for prog/link attach and revision update. Patch 3 adds a new libbpf API to do cgroup link attach with flags like BPF_F_BEFORE/BPF_F_AFTER. Patch 4 adds two tests to validate the implementation.
[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]
Yonghong Song (4):
cgroup: Add bpf prog revisions to struct cgroup_bpf
bpf: Implement mprog API on top of existing cgroup progs
libbpf: Support link-based cgroup attach with options selftests/bpf: Add two selftests for mprog API based cgroup progs
include/linux/bpf-cgroup-defs.h | 1 + include/uapi/linux/bpf.h | 7 + kernel/bpf/cgroup.c | 151 +++- kernel/bpf/syscall.c | 58 +- kernel/cgroup/cgroup.c | 5 +- tools/include/uapi/linux/bpf.h | 7 + tools/lib/bpf/bpf.c | 44 + tools/lib/bpf/bpf.h | 5 + tools/lib/bpf/libbpf.c | 28 + tools/lib/bpf/libbpf.h | 15 + tools/lib/bpf/libbpf.map | 1 + .../bpf/prog_tests/cgroup_mprog_opts.c | 752 ++++++++++++++++++ .../bpf/prog_tests/cgroup_mprog_ordering.c | 77 ++ .../selftests/bpf/progs/cgroup_mprog.c | 30 + 14 files changed, 1138 insertions(+), 43 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_mprog_opts.c create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_mprog_ordering.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_mprog.c
Signature · 1 line
2.47.1
Yonghong Song
reviewerRFC PATCH 1/4 cgroup: Add bpf prog revisions to struct cgroup_bpf
One of key items in mprog API is revision for prog list. The revision number will be increased if the prog list changed, e.g., attach, detach or replace.
Add 'revisions' field to struct cgroup_bpf, representing revisions for all cgroup related attachment types. The initial revision value is set to 1, the same as kernel mprog implementations.
--- include/linux/bpf-cgroup-defs.h | 1 + kernel/cgroup/cgroup.c | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-)
include/linux/bpf-cgroup-defs.h +1 -0
@@ -62,6 +62,7 @@ struct cgroup_bpf {* when BPF_F_ALLOW_MULTI the list can have up to BPF_CGROUP_MAX_PROGS */ struct hlist_head progs[MAX_CGROUP_BPF_ATTACH_TYPE ]; +atomic64_t revisions[MAX_CGROUP_BPF_ATTACH_TYPE ];u8 flags[MAX_CGROUP_BPF_ATTACH_TYPE ];/* list of cgroup shared storages */
kernel/cgroup/cgroup.c +4 -1
@@ -2053,7 +2053,7 @@ static int cgroup_reconfigure(struct fs_context *fc)static void init_cgroup_housekeeping (struct cgroup *cgrp) {struct cgroup_subsys *ss; -int ssid; +int i, ssid;INIT_LIST_HEAD (&cgrp->self.sibling);INIT_LIST_HEAD (&cgrp->self.children);
@@ -2071,6 +2071,9 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)for_each_subsys (ss, ssid)INIT_LIST_HEAD (&cgrp->e_csets[ssid]); +for (i =0 ; i <ARRAY_SIZE (cgrp->bpf.revisions); i++) +atomic64_set (&cgrp->bpf.revisions[i],1 ); +init_waitqueue_head (&cgrp->offline_waitq);INIT_WORK (&cgrp->release_agent_work, cgroup1_release_agent); }
Signature · 1 line
2.47.1
kernel test robot
build botRe: [RFC PATCH bpf-next 1/4] cgroup: Add bpf prog revisions to struct cgroup_bpf
kernel test robot noticed the following build errors:
Hi Yonghong,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Yonghong-Song/cgroup-Add-bpf-prog-revisions-to-struct-cgroup_bpf/20250411-091743 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20250411011528.1839359-1-yonghong.song%40linux.dev patch subject: [RFC PATCH bpf-next 1/4] cgroup: Add bpf prog revisions to struct cgroup_bpf config: x86_64-buildonly-randconfig-001-20250412 (https://download.01.org/0day-ci/archive/20250412/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250412/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from include/linux/kernel.h:16,
from include/linux/cpumask.h:11,
from arch/x86/include/asm/tlbbatch.h:5,
from include/linux/mm_types_task.h:17,
from include/linux/sched.h:38,
from include/linux/cgroup.h:12,
from kernel/cgroup/cgroup-internal.h:5,
from kernel/cgroup/cgroup.c:31:
kernel/cgroup/cgroup.c: In function 'init_cgroup_housekeeping':
>> kernel/cgroup/cgroup.c:2074:45: error: 'struct cgroup_bpf' has no member named 'revisions'
2074 | for (i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++)
| ^
include/linux/array_size.h:11:33: note: in definition of macro 'ARRAY_SIZE'
11 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
| ^~~
>> kernel/cgroup/cgroup.c:2074:45: error: 'struct cgroup_bpf' has no member named 'revisions'
2074 | for (i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++)
| ^
include/linux/array_size.h:11:48: note: in definition of macro 'ARRAY_SIZE'
11 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
| ^~~
In file included from include/linux/build_bug.h:5,
from arch/x86/include/asm/current.h:5,
from include/linux/sched.h:12:
>> kernel/cgroup/cgroup.c:2074:45: error: 'struct cgroup_bpf' has no member named 'revisions'
2074 | for (i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++)
| ^
include/linux/compiler.h:197:79: note: in definition of macro '__BUILD_BUG_ON_ZERO_MSG'
197 | #define __BUILD_BUG_ON_ZERO_MSG(e, msg) ((int)sizeof(struct {_Static_assert(!(e), msg);}))
| ^
include/linux/compiler.h:201:35: note: in expansion of macro '__same_type'
201 | #define __is_array(a) (!__same_type((a), &(a)[0]))
| ^~~~~~~~~~~
include/linux/compiler.h:202:58: note: in expansion of macro '__is_array'
202 | #define __must_be_array(a) __BUILD_BUG_ON_ZERO_MSG(!__is_array(a), \
| ^~~~~~~~~~
include/linux/array_size.h:11:59: note: in expansion of macro '__must_be_array'
11 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
| ^~~~~~~~~~~~~~~
kernel/cgroup/cgroup.c:2074:25: note: in expansion of macro 'ARRAY_SIZE'
2074 | for (i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++)
| ^~~~~~~~~~
>> kernel/cgroup/cgroup.c:2074:45: error: 'struct cgroup_bpf' has no member named 'revisions'
2074 | for (i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++)
| ^
include/linux/compiler.h:197:79: note: in definition of macro '__BUILD_BUG_ON_ZERO_MSG'
197 | #define __BUILD_BUG_ON_ZERO_MSG(e, msg) ((int)sizeof(struct {_Static_assert(!(e), msg);}))
| ^
include/linux/compiler.h:201:35: note: in expansion of macro '__same_type'
201 | #define __is_array(a) (!__same_type((a), &(a)[0]))
| ^~~~~~~~~~~
include/linux/compiler.h:202:58: note: in expansion of macro '__is_array'
202 | #define __must_be_array(a) __BUILD_BUG_ON_ZERO_MSG(!__is_array(a), \
| ^~~~~~~~~~
include/linux/array_size.h:11:59: note: in expansion of macro '__must_be_array'
11 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
| ^~~~~~~~~~~~~~~
kernel/cgroup/cgroup.c:2074:25: note: in expansion of macro 'ARRAY_SIZE'
2074 | for (i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++)
| ^~~~~~~~~~
include/linux/compiler.h:197:77: error: expression in static assertion is not an integer
197 | #define __BUILD_BUG_ON_ZERO_MSG(e, msg) ((int)sizeof(struct {_Static_assert(!(e), msg);}))
| ^
include/linux/compiler.h:202:33: note: in expansion of macro '__BUILD_BUG_ON_ZERO_MSG'
202 | #define __must_be_array(a) __BUILD_BUG_ON_ZERO_MSG(!__is_array(a), \
| ^~~~~~~~~~~~~~~~~~~~~~~
include/linux/array_size.h:11:59: note: in expansion of macro '__must_be_array'
11 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
| ^~~~~~~~~~~~~~~
kernel/cgroup/cgroup.c:2074:25: note: in expansion of macro 'ARRAY_SIZE'
2074 | for (i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++)
| ^~~~~~~~~~
kernel/cgroup/cgroup.c:2075:40: error: 'struct cgroup_bpf' has no member named 'revisions'
2075 | atomic64_set(&cgrp->bpf.revisions[i], 1);
| ^vim +2074 kernel/cgroup/cgroup.c
2052
2053 static void init_cgroup_housekeeping(struct cgroup *cgrp)
2054 {
2055 struct cgroup_subsys *ss;
2056 int i, ssid;
2057
2058 INIT_LIST_HEAD(&cgrp->self.sibling);
2059 INIT_LIST_HEAD(&cgrp->self.children);
2060 INIT_LIST_HEAD(&cgrp->cset_links);
2061 INIT_LIST_HEAD(&cgrp->pidlists);
2062 mutex_init(&cgrp->pidlist_mutex);
2063 cgrp->self.cgroup = cgrp;
2064 cgrp->self.flags |= CSS_ONLINE;
2065 cgrp->dom_cgrp = cgrp;
2066 cgrp->max_descendants = INT_MAX;
2067 cgrp->max_depth = INT_MAX;
2068 INIT_LIST_HEAD(&cgrp->rstat_css_list);
2069 prev_cputime_init(&cgrp->prev_cputime);
2070
2071 for_each_subsys(ss, ssid)
2072 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
2073
> 2074 for (i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++)
2075 atomic64_set(&cgrp->bpf.revisions[i], 1);
2076
2077 init_waitqueue_head(&cgrp->offline_waitq);
2078 INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent);
2079 }
2080
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Yonghong Song
reviewerRe: [RFC PATCH bpf-next 1/4] cgroup: Add bpf prog revisions to struct cgroup_bpf
Quoted 31 lines · kernel test robot
Hi Yonghong,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Yonghong-Song/cgroup-Add-bpf-prog-revisions-to-struct-cgroup_bpf/20250411-091743 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20250411011528.1839359-1-yonghong.song%40linux.dev patch subject: [RFC PATCH bpf-next 1/4] cgroup: Add bpf prog revisions to struct cgroup_bpf config: x86_64-buildonly-randconfig-001-20250412 (https://download.01.org/0day-ci/archive/20250412/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250412/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/All errors (new ones prefixed by >>):
In file included from
include/linux/kernel.h:16,
frominclude/linux/cpumask.h:11, fromarch/x86/include/asm/tlbbatch.h:5, frominclude/linux/mm_types_task.h:17, frominclude/linux/sched.h:38,
frominclude/linux/cgroup.h:12, fromkernel/cgroup/cgroup-internal.h:5, fromkernel/cgroup/cgroup.c:31:
kernel/cgroup/cgroup.c: In function 'init_cgroup_housekeeping':
Quoted 2 lines
Quoted 1 line
Quoted 1 line
kernel/cgroup/cgroup.c:2074:45: error: 'structcgroup_bpf' has no member named 'revisions'2074 | for (i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++)
This needs the guard CONFIG_CGROUP_BPF. Will fix in the next revision.
Quoted 5 lines
| ^
include/linux/array_size.h:11:33: note: in definition of macro 'ARRAY_SIZE'
11 | #defineARRAY_SIZE(arr)(sizeof(arr) / sizeof((arr)[0]) +__must_be_array(arr))
| ^~~
[...]
kernel test robot
build botRe: [RFC PATCH bpf-next 1/4] cgroup: Add bpf prog revisions to struct cgroup_bpf
In reply to Yonghong Songkernel test robot noticed the following build errors:
Hi Yonghong,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Yonghong-Song/cgroup-Add-bpf-prog-revisions-to-struct-cgroup_bpf/20250411-091743 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20250411011528.1839359-1-yonghong.song%40linux.dev patch subject: [RFC PATCH bpf-next 1/4] cgroup: Add bpf prog revisions to struct cgroup_bpf config: x86_64-buildonly-randconfig-004-20250412 (https://download.01.org/0day-ci/archive/20250412/[email protected]/config) compiler: clang version 20.1.2 (https://github.com/llvm/llvm-project 58df0ef89dd64126512e4ee27b4ac3fd8ddf6247) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250412/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> kernel/cgroup/cgroup.c:2074:39: error: no member named 'revisions' in 'struct cgroup_bpf'
2074 | for (i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++)
| ~~~~~~~~~ ^
include/linux/array_size.h:11:33: note: expanded from macro 'ARRAY_SIZE'
11 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
| ^~~
>> kernel/cgroup/cgroup.c:2074:39: error: no member named 'revisions' in 'struct cgroup_bpf'
2074 | for (i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++)
| ~~~~~~~~~ ^
include/linux/array_size.h:11:48: note: expanded from macro 'ARRAY_SIZE'
11 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
| ^~~
>> kernel/cgroup/cgroup.c:2074:39: error: no member named 'revisions' in 'struct cgroup_bpf'
2074 | for (i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++)
| ~~~~~~~~~ ^
include/linux/array_size.h:11:75: note: expanded from macro 'ARRAY_SIZE'
11 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
| ^~~
include/linux/compiler.h:202:64: note: expanded from macro '__must_be_array'
202 | #define __must_be_array(a) __BUILD_BUG_ON_ZERO_MSG(!__is_array(a), \
| ^
include/linux/compiler.h:201:39: note: expanded from macro '__is_array'
201 | #define __is_array(a) (!__same_type((a), &(a)[0]))
| ^
include/linux/compiler_types.h:498:63: note: expanded from macro '__same_type'
498 | #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
| ^
include/linux/compiler.h:197:79: note: expanded from macro '__BUILD_BUG_ON_ZERO_MSG'
197 | #define __BUILD_BUG_ON_ZERO_MSG(e, msg) ((int)sizeof(struct {_Static_assert(!(e), msg);}))
| ^
kernel/cgroup/cgroup.c:2075:27: error: no member named 'revisions' in 'struct cgroup_bpf'
2075 | atomic64_set(&cgrp->bpf.revisions[i], 1);
| ~~~~~~~~~ ^4 errors generated.
vim +2074 kernel/cgroup/cgroup.c
2052
2053 static void init_cgroup_housekeeping(struct cgroup *cgrp)
2054 {
2055 struct cgroup_subsys *ss;
2056 int i, ssid;
2057
2058 INIT_LIST_HEAD(&cgrp->self.sibling);
2059 INIT_LIST_HEAD(&cgrp->self.children);
2060 INIT_LIST_HEAD(&cgrp->cset_links);
2061 INIT_LIST_HEAD(&cgrp->pidlists);
2062 mutex_init(&cgrp->pidlist_mutex);
2063 cgrp->self.cgroup = cgrp;
2064 cgrp->self.flags |= CSS_ONLINE;
2065 cgrp->dom_cgrp = cgrp;
2066 cgrp->max_descendants = INT_MAX;
2067 cgrp->max_depth = INT_MAX;
2068 INIT_LIST_HEAD(&cgrp->rstat_css_list);
2069 prev_cputime_init(&cgrp->prev_cputime);
2070
2071 for_each_subsys(ss, ssid)
2072 INIT_LIST_HEAD(&cgrp->e_csets[ssid]);
2073
> 2074 for (i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++)
2075 atomic64_set(&cgrp->bpf.revisions[i], 1);
2076
2077 init_waitqueue_head(&cgrp->offline_waitq);
2078 INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent);
2079 }
2080
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [RFC PATCH bpf-next 1/4] cgroup: Add bpf prog revisions to struct cgroup_bpf
In reply to Yonghong SongQuoted 20 lines · Yonghong Song
One of key items in mprog API is revision for prog list. The revision number will be increased if the prog list changed, e.g., attach, detach or replace.
Add 'revisions' field to struct
cgroup_bpf, representing revisions for all cgroup related attachment types. The initial revision value is set to 1, the same as kernel mprog implementations.Signed-off-by Yonghong Song <[email protected]>--- include/linux/bpf-cgroup-defs.h | 1 + kernel/cgroup/cgroup.c | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-)include/linux/bpf-cgroup-defs.h +0
-0
include/linux/bpf-cgroup-defs.h struct cgroup_bpf { · line 65
Quoted 4 lines
include/linux/bpf-cgroup-defs.h +1
-0@@ -62,6 +62,7 @@ struct cgroup_bpf {* when BPF_F_ALLOW_MULTI the list can have up to BPF_CGROUP_MAX_PROGS */ struct hlist_head progs[MAX_CGROUP_BPF_ATTACH_TYPE ]; +atomic64_t revisions[MAX_CGROUP_BPF_ATTACH_TYPE ];
for cgroups all the attachment and detachment happens under cgroup_mutex, so I don't think we need atomic64_t, just plain u64 will work
Quoted 29 lines
include/linux/bpf-cgroup-defs.h +0
-0u8 flags[MAX_CGROUP_BPF_ATTACH_TYPE ];/* list of cgroup shared storages */ kernel/cgroup/cgroup.c +4
-2@@ -2053,7 +2053,7 @@ static int cgroup_reconfigure(struct fs_context *fc)static void init_cgroup_housekeeping (struct cgroup *cgrp) {struct cgroup_subsys *ss; -int ssid; +int i, ssid;INIT_LIST_HEAD (&cgrp->self.sibling);INIT_LIST_HEAD (&cgrp->self.children);@@ -2071,6 +2071,9 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)for_each_subsys (ss, ssid)INIT_LIST_HEAD (&cgrp->e_csets[ssid]); +for (i =0 ; i <ARRAY_SIZE (cgrp->bpf.revisions); i++) +atomic64_set (&cgrp->bpf.revisions[i],1 ); +init_waitqueue_head (&cgrp->offline_waitq);INIT_WORK (&cgrp->release_agent_work, cgroup1_release_agent); } --2.47.1
Yonghong Song
reviewerRe: [RFC PATCH bpf-next 1/4] cgroup: Add bpf prog revisions to struct cgroup_bpf
Quoted 15 lines · Andrii Nakryiko
Quoted 14 lines · Yonghong Song
One of key items in mprog API is revision for prog list. The revision number will be increased if the prog list changed, e.g., attach, detach or replace.
Add 'revisions' field to struct
cgroup_bpf, representing revisions for all cgroup related attachment types. The initial revision value is set to 1, the same as kernel mprog implementations.Signed-off-by Yonghong Song <[email protected]>--- include/linux/bpf-cgroup-defs.h | 1 + kernel/cgroup/cgroup.c | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-)
Quoted 12 lines
include/linux/bpf-cgroup-defs.h struct cgroup_bpf { · line 65
Quoted 9 lines
include/linux/bpf-cgroup-defs.h +1
-0@@ -62,6 +62,7 @@ struct cgroup_bpf {* when BPF_F_ALLOW_MULTI the list can have up to BPF_CGROUP_MAX_PROGS */ struct hlist_head progs[MAX_CGROUP_BPF_ATTACH_TYPE ]; +atomic64_t revisions[MAX_CGROUP_BPF_ATTACH_TYPE ];for cgroups all the attachment and detachment happens under
cgroup_mutex, so I don't think we needatomic64_t, just plain u64 will work
Indeed Make sense.
Quoted 30 lines
Quoted 30 lines
include/linux/bpf-cgroup-defs.h +0
-0u8 flags[MAX_CGROUP_BPF_ATTACH_TYPE ];/* list of cgroup shared storages */ kernel/cgroup/cgroup.c +4
-2@@ -2053,7 +2053,7 @@ static int cgroup_reconfigure(struct fs_context *fc)static void init_cgroup_housekeeping (struct cgroup *cgrp) {struct cgroup_subsys *ss; -int ssid; +int i, ssid;INIT_LIST_HEAD (&cgrp->self.sibling);INIT_LIST_HEAD (&cgrp->self.children);@@ -2071,6 +2071,9 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)for_each_subsys (ss, ssid)INIT_LIST_HEAD (&cgrp->e_csets[ssid]); +for (i =0 ; i <ARRAY_SIZE (cgrp->bpf.revisions); i++) +atomic64_set (&cgrp->bpf.revisions[i],1 ); +init_waitqueue_head (&cgrp->offline_waitq);INIT_WORK (&cgrp->release_agent_work, cgroup1_release_agent); } --2.47.1
Yonghong Song
reviewerRFC PATCH 2/4 bpf: Implement mprog API on top of existing cgroup progs
In reply to Yonghong SongCurrent cgroup prog ordering is appending at attachment time. This is not ideal. In some cases, users want specific ordering at a particular cgroup level. To address this, the existing mprog API seems an ideal solution with supporting BPF_F_BEFORE and BPF_F_AFTER flags.
But there are a few obstacles to directly use kernel mprog interface. Currently cgroup bpf progs already support prog attach/detach/replace and link-based attach/detach/replace. For example, in struct bpf_prog_array_item, the cgroup_storage field needs to be together with bpf prog. But the mprog API struct bpf_mprog_fp only has bpf_prog as the member, which makes it difficult to use kernel mprog interface.
In another case, the current cgroup prog detach tries to use the same flag as in attach. This is different from mprog kernel interface which uses flags passed from user space.
So to avoid modifying existing behavior, I made the following changes to support mprog API for cgroup progs:
- The support is for prog list at cgroup level. Cross-level prog list (a.k.a. effective prog list) is not supported.
- Previously, BPF_F_PREORDER is supported only for prog attach, now BPF_F_PREORDER is also supported by link-based attach.
- For attach, BPF_F_BEFORE/BPF_F_AFTER/BPF_F_ID is supported similar to kernel mprog but with different implementation.
- For detach and replace, use the existing implementation.
- For attach, detach and replace, the revision for a particular prog list, associated with a particular attach type, will be updated by increasing count by 1.
--- include/uapi/linux/bpf.h | 7 ++ kernel/bpf/cgroup.c | 151 ++++++++++++++++++++++++++++----- kernel/bpf/syscall.c | 58 ++++++++----- tools/include/uapi/linux/bpf.h | 7 ++ 4 files changed, 181 insertions(+), 42 deletions(-)
include/uapi/linux/bpf.h +7 -0
@@ -1794,6 +1794,13 @@ union bpf_attr { };__u64 expected_revision; } netkit; +struct { +union { +__u32 relative_fd; +__u32 relative_id; + }; +__u64 expected_revision; + } cgroup; }; } link_create;
kernel/bpf/cgroup.c +129 -22
292 lines not on this page · raw message
kernel/bpf/syscall.c +38 -20
88 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +7 -0
@@ -1794,6 +1794,13 @@ union bpf_attr { };__u64 expected_revision; } netkit; +struct { +union { +__u32 relative_fd; +__u32 relative_id; + }; +__u64 expected_revision; + } cgroup; }; } link_create;
Signature · 1 line
2.47.1
Quoted 61 lines · Yonghong Song
Current cgroup prog ordering is appending at attachment time. This is not ideal. In some cases, users want specific ordering at a particular cgroup level. To address this, the existing mprog API seems an ideal solution with supporting
BPF_F_BEFOREandBPF_F_AFTERflags.But there are a few obstacles to directly use kernel mprog interface. Currently cgroup bpf progs already support prog attach/detach/replace and link-based attach/detach/replace. For example, in struct
bpf_prog_array_item, thecgroup_storagefield needs to be together with bpf prog. But the mprog API structbpf_mprog_fponly hasbpf_progas the member, which makes it difficult to use kernel mprog interface.In another case, the current cgroup prog detach tries to use the same flag as in attach. This is different from mprog kernel interface which uses flags passed from user space.
So to avoid modifying existing behavior, I made the following changes to support mprog API for cgroup progs:
- The support is for prog list at cgroup level. Cross-level prog list (a.k.a. effective prog list) is not supported.
- Previously,BPF_F_PREORDERis supported only for prog attach, nowBPF_F_PREORDERis also supported by link-based attach.
- For attach, BPF_F_BEFORE/BPF_F_AFTER/BPF_F_ID is supported similar to kernel mprog but with different implementation.
- For detach and replace, use the existing implementation.
- For attach, detach and replace, the revision for a particular prog list, associated with a particular attach type, will be updated by increasing count by 1.Signed-off-by Yonghong Song <[email protected]>--- include/uapi/linux/bpf.h | 7 ++ kernel/bpf/cgroup.c | 151 ++++++++++++++++++++++++++++----- kernel/bpf/syscall.c | 58 ++++++++----- tools/include/uapi/linux/bpf.h | 7 ++ 4 files changed, 181 insertions(+), 42 deletions(-)include/uapi/linux/bpf.h +7
-0@@ -1794,6 +1794,13 @@ union bpf_attr { };__u64 expected_revision; } netkit; +struct { +union { +__u32 relative_fd; +__u32 relative_id; + }; +__u64 expected_revision; + } cgroup; }; } link_create;kernel/bpf/cgroup.c +0
-0
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 631
Quoted 8 lines
kernel/bpf/cgroup.c +5
-0@@ -624,6 +624,90 @@ static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs,return NULL ; } +static struct bpf_prog *get_cmp_prog (struct hlist_head *progs,struct bpf_prog *prog, +u32 flags,u32 id_or_fd,struct bpf_prog_list **ppltmp) +{ +struct bpf_prog *cmp_prog =NULL , *pltmp_prog; +bool preorder = !!(flags &BPF_F_PREORDER );
nit: !!() pattern is not necessary when assigning to bool and is just a visual and cognitive noise
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 636
Quoted 5 lines
kernel/bpf/cgroup.c +5
-0+struct bpf_prog_list *pltmp; +bool id = flags &BPF_F_ID ; +bool found; + +if (id || id_or_fd) {
let's invert the condition and exit early? also I find "id" as a bool very confusing, I think it's fine to just open-coded it in two places you actually check for BPF_F_ID flag.
But also, isn't this if (id) part redundant? Would just if (id_or_fd) be enough?
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 644
Quoted 8 lines
kernel/bpf/cgroup.c +8
-0+/* flags must have BPF_F_BEFORE or BPF_F_AFTER */ +if (!(flags & (BPF_F_BEFORE |BPF_F_AFTER ))) +return ERR_PTR (-EINVAL ); + +if (id) + cmp_prog =bpf_prog_by_id (id_or_fd); +else + cmp_prog =bpf_prog_get (id_or_fd);
how about we use "anchor" terminology here? So this would be "anchor program" or anchor_prog?
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 647
Quoted 3 lines
kernel/bpf/cgroup.c +3
-0+if (IS_ERR (cmp_prog)) +return cmp_prog; +if (cmp_prog->type != prog->type)
bpf_prog_put?
pw-bot: cr
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 653
Quoted 6 lines
kernel/bpf/cgroup.c +6
-0+return ERR_PTR (-EINVAL ); + + found = false; +hlist_for_each_entry (pltmp, progs, node) { + pltmp_prog = pltmp->link ? pltmp->link->link.prog : pltmp->prog; +if (pltmp_prog == cmp_prog) {
try keeping nesting minimal:
if (pltmp_prog != cmp_prog)
continue;
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 657
Quoted 4 lines
kernel/bpf/cgroup.c +4
-0+if (!!(pltmp->flags &BPF_F_PREORDER ) != preorder) +return ERR_PTR (-EINVAL ); + found = true; + *ppltmp = pltmp;
we don't need found flag if we set ppltmp to NULL before loop, and to non-NULL if we find the match
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 661
Quoted 4 lines
kernel/bpf/cgroup.c +4
-0+break ; + } + } +if (!found)
bpf_prog_put(cmp_prog)
Quoted 8 lines
kernel/bpf/cgroup.c +8
-0+return ERR_PTR (-ENOENT ); + } + +return cmp_prog; +} + +static int insert_pl_to_hlist (struct bpf_prog_list *pl,struct hlist_head *progs, +struct bpf_prog *prog,u32 flags,u32 id_or_fd)
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 679
Quoted 10 lines
kernel/bpf/cgroup.c +10
-0+{ +struct hlist_node *last, *last_node =NULL ; +struct bpf_prog_list *pltmp =NULL ; +struct bpf_prog *cmp_prog; + +/* flags cannot have both BPF_F_BEFORE and BPF_F_AFTER */ +if ((flags &BPF_F_BEFORE ) && (flags &BPF_F_AFTER )) +return -EINVAL ; + + cmp_prog =get_cmp_prog (progs, prog, flags, id_or_fd, &pltmp);
why get_cmp_prog can't return this last_node if we have BPF_F_AFTER with no id/fd specified? Then you wouldn't have to special-case appending (same for prepending, actually)?
Quoted 64 lines
kernel/bpf/cgroup.c +38
-1+if (IS_ERR (cmp_prog)) +return PTR_ERR (cmp_prog); + +if (hlist_empty (progs)) { +hlist_add_head (&pl->node, progs); + }else { +hlist_for_each (last, progs) { +if (last->next) +continue ; + last_node = last; +break ; + } + +if (!cmp_prog) { +if (flags &BPF_F_BEFORE ) +hlist_add_head (&pl->node, progs); +else +hlist_add_behind (&pl->node, last_node); + }else { +if (flags &BPF_F_BEFORE ) +hlist_add_before (&pl->node, &pltmp->node); +else if (flags &BPF_F_AFTER ) +hlist_add_behind (&pl->node, &pltmp->node); +else +hlist_add_behind (&pl->node, last_node); + } + } + +return 0 ; +} +/** * __cgroup_bpf_attach() - Attach the program or the link to a cgroup, and * propagate the change to descendants @@ -633,6 +717,8 @@ static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs,* @replace_prog: Previously attached program to replace if BPF_F_REPLACE is set * @type: Type of attach operation * @flags: Option flags +* @id_or_fd: Relative prog id or fd +* @revision: bpf_prog_list revision * * Exactly one of @prog or @link can be non-null. * Must be called with cgroup_mutex held. @@ -640,7 +726,8 @@ static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs,static int __cgroup_bpf_attach (struct cgroup *cgrp,struct bpf_prog *prog,struct bpf_prog *replace_prog,struct bpf_cgroup_link *link, -enum bpf_attach_type type,u32 flags) +enum bpf_attach_type type,u32 flags,u32 id_or_fd, +u64 revision) {u32 saved_flags = (flags & (BPF_F_ALLOW_OVERRIDE |BPF_F_ALLOW_MULTI ));struct bpf_prog *old_prog =NULL ;@@ -656,6 +743,9 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp, ((flags & BPF_F_REPLACE) && !(flags & BPF_F_ALLOW_MULTI)))/* invalid combination */ return -EINVAL ; +if ((flags &BPF_F_REPLACE ) && (flags & (BPF_F_BEFORE |BPF_F_AFTER ))) +/* only either replace or insertion with before/after */ +return -EINVAL ;if (link && (prog || replace_prog))/* only either link or prog/replace_prog can be specified */ return -EINVAL ;
kernel/bpf/cgroup.c static int __cgroup_bpf_attach(struct cgroup *cgrp, · line 760
Quoted 8 lines
kernel/bpf/cgroup.c +2
-0@@ -663,9 +753,12 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,/* replace_prog implies BPF_F_REPLACE, and vice versa */ return -EINVAL ; + atype =bpf_cgroup_atype_find (type, new_prog->aux->attach_btf_id);if (atype <0 )return -EINVAL ; +if (revision && revision !=atomic64_read (&cgrp->bpf.revisions[atype]))
this is happening under lock, no need for atomic operations
Quoted 4 lines
kernel/bpf/cgroup.c +1
-0+return -ESTALE ; progs = &cgrp->bpf.progs[atype];
[...]
Quoted 9 lines
kernel/bpf/cgroup.c +1
-0@@ -1063,6 +1161,7 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,struct bpf_prog_array *effective;int cnt, ret =0 , i;int total_cnt =0 ; +u64 revision =0 ;u32 flags;if (effective_query && prog_attach_flags)
kernel/bpf/cgroup.c static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr, · line 1203
Quoted 5 lines
kernel/bpf/cgroup.c +2
-0@@ -1100,6 +1199,10 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,return -EFAULT ;if (copy_to_user (&uattr->query.prog_cnt, &total_cnt,sizeof (total_cnt)))return -EFAULT ; +if (!effective_query && from_atype == to_atype) + revision =atomic64_read (&cgrp->bpf.revisions[from_atype]);
even here we hold cgroup_mutex
Quoted 5 lines
kernel/bpf/cgroup.c +2
-0+if (copy_to_user (&uattr->query.revision, &revision,sizeof (revision))) +return -EFAULT ;if (attr->query.prog_cnt ==0 || !prog_ids || !total_cnt)/* return early if user requested only program count + flags */ return 0 ;
[...]
Quoted 16 lines
kernel/bpf/cgroup.c +3
-1@@ -1336,7 +1441,9 @@ int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) } err =cgroup_bpf_attach (cgrp,NULL ,NULL , link, - link->type,BPF_F_ALLOW_MULTI ); + link->type,BPF_F_ALLOW_MULTI | attr->link_create.flags, + attr->link_create.cgroup.relative_fd, + attr->link_create.cgroup.expected_revision);if (err) {bpf_link_cleanup (&link_primer); goto out_put_cgroup;kernel/bpf/syscall.c +0
-0
kernel/bpf/syscall.c static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, · line 4186
Quoted 4 lines
kernel/bpf/syscall.c +1
-0@@ -4183,6 +4183,23 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, } } +static bool bpf_cgroup_prog_attached (enum bpf_prog_type ptype)
I find this "attached" naming misleading. I think the name should call out that we are just checking if program type is cgroup-attaching, so maybe something like "is_cgroup_prog_type", or something along those lines?
But for LSM we need to look at expected_attach_type to be BPF_LSM_CGROUP, so maybe pass both prog type and expected attach type?
Quoted 16 lines
kernel/bpf/syscall.c +16
-0+{ +switch (ptype) { +case BPF_PROG_TYPE_CGROUP_DEVICE : +case BPF_PROG_TYPE_CGROUP_SKB : +case BPF_PROG_TYPE_CGROUP_SOCK : +case BPF_PROG_TYPE_CGROUP_SOCK_ADDR : +case BPF_PROG_TYPE_CGROUP_SOCKOPT : +case BPF_PROG_TYPE_CGROUP_SYSCTL : +case BPF_PROG_TYPE_SOCK_OPS : +case BPF_PROG_TYPE_LSM : +return true; +default : +return false; + } +} +
[...]
Yonghong Song
reviewerQuoted 64 lines · Andrii Nakryiko
Quoted 63 lines · Yonghong Song
Current cgroup prog ordering is appending at attachment time. This is not ideal. In some cases, users want specific ordering at a particular cgroup level. To address this, the existing mprog API seems an ideal solution with supporting
BPF_F_BEFOREandBPF_F_AFTERflags.But there are a few obstacles to directly use kernel mprog interface. Currently cgroup bpf progs already support prog attach/detach/replace and link-based attach/detach/replace. For example, in struct
bpf_prog_array_item, thecgroup_storagefield needs to be together with bpf prog. But the mprog API structbpf_mprog_fponly hasbpf_progas the member, which makes it difficult to use kernel mprog interface.In another case, the current cgroup prog detach tries to use the same flag as in attach. This is different from mprog kernel interface which uses flags passed from user space.
So to avoid modifying existing behavior, I made the following changes to support mprog API for cgroup progs:
- The support is for prog list at cgroup level. Cross-level prog list (a.k.a. effective prog list) is not supported.
- Previously,BPF_F_PREORDERis supported only for prog attach, nowBPF_F_PREORDERis also supported by link-based attach.
- For attach, BPF_F_BEFORE/BPF_F_AFTER/BPF_F_ID is supported similar to kernel mprog but with different implementation.
- For detach and replace, use the existing implementation.
- For attach, detach and replace, the revision for a particular prog list, associated with a particular attach type, will be updated by increasing count by 1.Signed-off-by Yonghong Song <[email protected]>--- include/uapi/linux/bpf.h | 7 ++ kernel/bpf/cgroup.c | 151 ++++++++++++++++++++++++++++----- kernel/bpf/syscall.c | 58 ++++++++----- tools/include/uapi/linux/bpf.h | 7 ++ 4 files changed, 181 insertions(+), 42 deletions(-)include/uapi/linux/bpf.h +7
-0@@ -1794,6 +1794,13 @@ union bpf_attr { };__u64 expected_revision; } netkit; +struct { +union { +__u32 relative_fd; +__u32 relative_id; + }; +__u64 expected_revision; + } cgroup; }; } link_create;kernel/bpf/cgroup.c +0
-0@@ -624,6 +624,90 @@ static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs,return NULL ; }
Quoted 7 lines
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 631
Quoted 5 lines
kernel/bpf/cgroup.c +5
-0+static struct bpf_prog *get_cmp_prog (struct hlist_head *progs,struct bpf_prog *prog, +u32 flags,u32 id_or_fd,struct bpf_prog_list **ppltmp) +{ +struct bpf_prog *cmp_prog =NULL , *pltmp_prog; +bool preorder = !!(flags &BPF_F_PREORDER );nit: !!() pattern is not necessary when assigning to bool and is just a visual and cognitive noise
Okay, will remove !! operators.
Quoted 9 lines
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 636
Quoted 6 lines
kernel/bpf/cgroup.c +5
-0+struct bpf_prog_list *pltmp; +bool id = flags &BPF_F_ID ; +bool found; + +if (id || id_or_fd) {let's invert the condition and exit early? also I find "id" as a bool very confusing, I think it's fine to just open-coded it in two places you actually check for
BPF_F_IDflag.
Open-codedflags & BPF_F_ID is okay.
Quoted 2 lines
But also, isn't this
if (id)part redundant? Would justif (id_or_fd)be enough?
Yes, id_or_fd should be enough.
Quoted 10 lines
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 644
Quoted 10 lines
kernel/bpf/cgroup.c +8
-0+/* flags must have BPF_F_BEFORE or BPF_F_AFTER */ +if (!(flags & (BPF_F_BEFORE |BPF_F_AFTER ))) +return ERR_PTR (-EINVAL ); + +if (id) + cmp_prog =bpf_prog_by_id (id_or_fd); +else + cmp_prog =bpf_prog_get (id_or_fd);
Quoted 2 lines
how about we use "anchor" terminology here? So this would be "anchor program" or
anchor_prog?
anchor_prog sounds good.
Quoted 5 lines
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 647
Quoted 4 lines
kernel/bpf/cgroup.c +3
-0+if (IS_ERR (cmp_prog)) +return cmp_prog; +if (cmp_prog->type != prog->type)
bpf_prog_put?
Yes. Missed bpf_prog_put. Will fix.
Quoted 9 lines
pw-bot: cr
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 653
Quoted 6 lines
kernel/bpf/cgroup.c +6
-0+return ERR_PTR (-EINVAL ); + + found = false; +hlist_for_each_entry (pltmp, progs, node) { + pltmp_prog = pltmp->link ? pltmp->link->link.prog : pltmp->prog; +if (pltmp_prog == cmp_prog) {
Quoted 4 lines
try keeping nesting minimal:
if (
pltmp_prog!=cmp_prog)continue;
Good point. Will do.
Quoted 7 lines
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 657
Quoted 5 lines
kernel/bpf/cgroup.c +4
-0+if (!!(pltmp->flags &BPF_F_PREORDER ) != preorder) +return ERR_PTR (-EINVAL ); + found = true; + *ppltmp = pltmp;we don't need found flag if we set ppltmp to NULL before loop, and to non-NULL if we find the match
Ack.
Quoted 6 lines
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 661
Quoted 5 lines
kernel/bpf/cgroup.c +4
-0+break ; + } + } +if (!found)
bpf_prog_put(cmp_prog)
Again, will do bpf_prog_put before turn error.
Quoted 14 lines
Quoted 14 lines
kernel/bpf/cgroup.c +13
-0+return ERR_PTR (-ENOENT ); + } + +return cmp_prog; +} + +static int insert_pl_to_hlist (struct bpf_prog_list *pl,struct hlist_head *progs, +struct bpf_prog *prog,u32 flags,u32 id_or_fd) +{ +struct hlist_node *last, *last_node =NULL ; +struct bpf_prog_list *pltmp =NULL ; +struct bpf_prog *cmp_prog; +
Quoted 8 lines
kernel/bpf/cgroup.c static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs, · line 679
Quoted 5 lines
kernel/bpf/cgroup.c +5
-0+/* flags cannot have both BPF_F_BEFORE and BPF_F_AFTER */ +if ((flags &BPF_F_BEFORE ) && (flags &BPF_F_AFTER )) +return -EINVAL ; + + cmp_prog =get_cmp_prog (progs, prog, flags, id_or_fd, &pltmp);why
get_cmp_progcan't return thislast_nodeif we haveBPF_F_AFTERwith no id/fd specified? Then you wouldn't have to special-case appending (same for prepending, actually)?
We could do this. Let me try this.
Quoted 68 lines
Quoted 68 lines
kernel/bpf/cgroup.c +38
-1+if (IS_ERR (cmp_prog)) +return PTR_ERR (cmp_prog); + +if (hlist_empty (progs)) { +hlist_add_head (&pl->node, progs); + }else { +hlist_for_each (last, progs) { +if (last->next) +continue ; + last_node = last; +break ; + } + +if (!cmp_prog) { +if (flags &BPF_F_BEFORE ) +hlist_add_head (&pl->node, progs); +else +hlist_add_behind (&pl->node, last_node); + }else { +if (flags &BPF_F_BEFORE ) +hlist_add_before (&pl->node, &pltmp->node); +else if (flags &BPF_F_AFTER ) +hlist_add_behind (&pl->node, &pltmp->node); +else +hlist_add_behind (&pl->node, last_node); + } + } + +return 0 ; +} +/** * __cgroup_bpf_attach() - Attach the program or the link to a cgroup, and * propagate the change to descendants @@ -633,6 +717,8 @@ static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs,* @replace_prog: Previously attached program to replace if BPF_F_REPLACE is set * @type: Type of attach operation * @flags: Option flags +* @id_or_fd: Relative prog id or fd +* @revision: bpf_prog_list revision * * Exactly one of @prog or @link can be non-null. * Must be called with cgroup_mutex held. @@ -640,7 +726,8 @@ static struct bpf_prog_list *find_attach_entry(struct hlist_head *progs,static int __cgroup_bpf_attach (struct cgroup *cgrp,struct bpf_prog *prog,struct bpf_prog *replace_prog,struct bpf_cgroup_link *link, -enum bpf_attach_type type,u32 flags) +enum bpf_attach_type type,u32 flags,u32 id_or_fd, +u64 revision) {u32 saved_flags = (flags & (BPF_F_ALLOW_OVERRIDE |BPF_F_ALLOW_MULTI ));struct bpf_prog *old_prog =NULL ;@@ -656,6 +743,9 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp, ((flags & BPF_F_REPLACE) && !(flags & BPF_F_ALLOW_MULTI)))/* invalid combination */ return -EINVAL ; +if ((flags &BPF_F_REPLACE ) && (flags & (BPF_F_BEFORE |BPF_F_AFTER ))) +/* only either replace or insertion with before/after */ +return -EINVAL ;if (link && (prog || replace_prog))/* only either link or prog/replace_prog can be specified */ return -EINVAL ;@@ -663,9 +753,12 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp,/* replace_prog implies BPF_F_REPLACE, and vice versa */ return -EINVAL ;
Quoted 6 lines
kernel/bpf/cgroup.c static int __cgroup_bpf_attach(struct cgroup *cgrp, · line 760
Quoted 5 lines
kernel/bpf/cgroup.c +2
-0+ atype =bpf_cgroup_atype_find (type, new_prog->aux->attach_btf_id);if (atype <0 )return -EINVAL ; +if (revision && revision !=atomic64_read (&cgrp->bpf.revisions[atype]))this is happening under lock, no need for atomic operations
Ack
Quoted 14 lines
Quoted 5 lines
kernel/bpf/cgroup.c +1
-0+return -ESTALE ; progs = &cgrp->bpf.progs[atype];[...]
Quoted 7 lines
kernel/bpf/cgroup.c +1
-0@@ -1063,6 +1161,7 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,struct bpf_prog_array *effective;int cnt, ret =0 , i;int total_cnt =0 ; +u64 revision =0 ;u32 flags;
Quoted 8 lines
kernel/bpf/cgroup.c static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr, · line 1203
Quoted 7 lines
kernel/bpf/cgroup.c +2
-0if (effective_query && prog_attach_flags)@@ -1100,6 +1199,10 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,return -EFAULT ;if (copy_to_user (&uattr->query.prog_cnt, &total_cnt,sizeof (total_cnt)))return -EFAULT ; +if (!effective_query && from_atype == to_atype) + revision =atomic64_read (&cgrp->bpf.revisions[from_atype]);even here we hold
cgroup_mutex
Ack
Quoted 34 lines
Quoted 6 lines
kernel/bpf/cgroup.c +2
-0+if (copy_to_user (&uattr->query.revision, &revision,sizeof (revision))) +return -EFAULT ;if (attr->query.prog_cnt ==0 || !prog_ids || !total_cnt)/* return early if user requested only program count + flags */ return 0 ;[...]
Quoted 16 lines
kernel/bpf/cgroup.c +3
-1@@ -1336,7 +1441,9 @@ int cgroup_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) } err =cgroup_bpf_attach (cgrp,NULL ,NULL , link, - link->type,BPF_F_ALLOW_MULTI ); + link->type,BPF_F_ALLOW_MULTI | attr->link_create.flags, + attr->link_create.cgroup.relative_fd, + attr->link_create.cgroup.expected_revision);if (err) {bpf_link_cleanup (&link_primer); goto out_put_cgroup;kernel/bpf/syscall.c +0
-0kernel/bpf/syscall.c static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, · line 4186
Quoted 4 lines
kernel/bpf/syscall.c +1
-0@@ -4183,6 +4183,23 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, } } +static bool bpf_cgroup_prog_attached (enum bpf_prog_type ptype)I find this "attached" naming misleading. I think the name should call out that we are just checking if program type is cgroup-attaching, so maybe something like "
is_cgroup_prog_type", or something along those lines?
Quoted 2 lines
But for LSM we need to look at
expected_attach_typeto beBPF_LSM_CGROUP, so maybe pass both prog type and expected attach type?
Yes, let us pass both prog type and expected attach type with func name is_cgroup_prog_type().
Quoted 18 lines
Quoted 17 lines
kernel/bpf/syscall.c +16
-0+{ +switch (ptype) { +case BPF_PROG_TYPE_CGROUP_DEVICE : +case BPF_PROG_TYPE_CGROUP_SKB : +case BPF_PROG_TYPE_CGROUP_SOCK : +case BPF_PROG_TYPE_CGROUP_SOCK_ADDR : +case BPF_PROG_TYPE_CGROUP_SOCKOPT : +case BPF_PROG_TYPE_CGROUP_SYSCTL : +case BPF_PROG_TYPE_SOCK_OPS : +case BPF_PROG_TYPE_LSM : +return true; +default : +return false; + } +} +[...]
Yonghong Song
reviewerRFC PATCH 3/4 libbpf: Support link-based cgroup attach with options
In reply to Yonghong SongCurrently libbpf supports bpf_program__attach_cgroup() with signature:
LIBBPF_API struct bpf_link *
bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
To support mprog style attachment, additionsl fields like flags, relative_{fd,id} and expected_revision are needed.
Add a new API:
LIBBPF_API struct bpf_link * bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,
const struct bpf_cgroup_opts *opts);
where bpf_cgroup_opts contains all above needed fields.
--- tools/lib/bpf/bpf.c | 44 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 5 +++++ tools/lib/bpf/libbpf.c | 28 +++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 15 ++++++++++++++ tools/lib/bpf/libbpf.map | 1 + 5 files changed, 93 insertions(+)
tools/lib/bpf/bpf.c +44 -0
@@ -837,6 +837,50 @@ int bpf_link_create(int prog_fd, int target_fd,if (!OPTS_ZEROED (opts, netkit))return libbpf_err (-EINVAL );break ; +case BPF_CGROUP_INET_INGRESS : +case BPF_CGROUP_INET_EGRESS : +case BPF_CGROUP_INET_SOCK_CREATE : +case BPF_CGROUP_INET_SOCK_RELEASE : +case BPF_CGROUP_INET4_BIND : +case BPF_CGROUP_INET6_BIND : +case BPF_CGROUP_INET4_POST_BIND : +case BPF_CGROUP_INET6_POST_BIND : +case BPF_CGROUP_INET4_CONNECT : +case BPF_CGROUP_INET6_CONNECT : +case BPF_CGROUP_UNIX_CONNECT : +case BPF_CGROUP_INET4_GETPEERNAME : +case BPF_CGROUP_INET6_GETPEERNAME : +case BPF_CGROUP_UNIX_GETPEERNAME : +case BPF_CGROUP_INET4_GETSOCKNAME : +case BPF_CGROUP_INET6_GETSOCKNAME : +case BPF_CGROUP_UNIX_GETSOCKNAME : +case BPF_CGROUP_UDP4_SENDMSG : +case BPF_CGROUP_UDP6_SENDMSG : +case BPF_CGROUP_UNIX_SENDMSG : +case BPF_CGROUP_UDP4_RECVMSG : +case BPF_CGROUP_UDP6_RECVMSG : +case BPF_CGROUP_UNIX_RECVMSG : +case BPF_CGROUP_SOCK_OPS : +case BPF_CGROUP_DEVICE : +case BPF_CGROUP_SYSCTL : +case BPF_CGROUP_GETSOCKOPT : +case BPF_CGROUP_SETSOCKOPT : +case BPF_LSM_CGROUP : + relative_fd =OPTS_GET (opts, cgroup.relative_fd,0 ); + relative_id =OPTS_GET (opts, cgroup.relative_id,0 ); +if (relative_fd && relative_id) +return libbpf_err (-EINVAL ); +if (relative_id) { + attr.link_create.cgroup.relative_id = relative_id; + attr.link_create.flags |=BPF_F_ID ; + }else { + attr.link_create.cgroup.relative_fd = relative_fd; + } + attr.link_create.cgroup.expected_revision = +OPTS_GET (opts, cgroup.expected_revision,0 ); +if (!OPTS_ZEROED (opts, cgroup)) +return libbpf_err (-EINVAL ); +break ;default :if (!OPTS_ZEROED (opts, flags))return libbpf_err (-EINVAL );
tools/lib/bpf/bpf.h +5 -0
@@ -438,6 +438,11 @@ struct bpf_link_create_opts {__u32 relative_id;__u64 expected_revision; } netkit; +struct { +__u32 relative_fd; +__u32 relative_id; +__u64 expected_revision; + } cgroup; }; size_t :0; };
tools/lib/bpf/libbpf.c +28 -0
@@ -12848,6 +12848,34 @@ struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifireturn bpf_program_attach_fd (prog, ifindex,"xdp" ,NULL ); } +struct bpf_link * +bpf_program__attach_cgroup_opts (const struct bpf_program *prog,int cgroup_fd, +const struct bpf_cgroup_opts *opts) +{ +LIBBPF_OPTS (bpf_link_create_opts, link_create_opts); +__u32 relative_id; +int relative_fd; + +if (!OPTS_VALID (opts, bpf_cgroup_opts)) +return libbpf_err_ptr (-EINVAL ); + + relative_id =OPTS_GET (opts, relative_id,0 ); + relative_fd =OPTS_GET (opts, relative_fd,0 ); + +if (relative_fd && relative_id) { +pr_warn ("prog '%s': relative_fd and relative_id cannot be set at the same time\n" , + prog->name); +return libbpf_err_ptr (-EINVAL ); + } + + link_create_opts.cgroup.expected_revision =OPTS_GET (opts, expected_revision,0 ); + link_create_opts.cgroup.relative_fd = relative_fd; + link_create_opts.cgroup.relative_id = relative_id; + link_create_opts.flags =OPTS_GET (opts, flags,0 ); + +return bpf_program_attach_fd (prog, cgroup_fd,"cgroup" , &link_create_opts); +} +struct bpf_link *bpf_program__attach_tcx (const struct bpf_program *prog,int ifindex,const struct bpf_tcx_opts *opts)
tools/lib/bpf/libbpf.h +15 -0
@@ -877,6 +877,21 @@ LIBBPF_API struct bpf_link * bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex, const struct bpf_netkit_opts *opts); +struct bpf_cgroup_opts { +/* size of this struct, for forward/backward compatibility */ +size_t sz; +__u32 flags; +__u32 relative_fd; +__u32 relative_id; +__u64 expected_revision; + size_t :0; +}; +#define bpf_cgroup_opts__last_field expected_revision + +LIBBPF_API struct bpf_link * +bpf_program__attach_cgroup_opts (const struct bpf_program *prog,int cgroup_fd, +const struct bpf_cgroup_opts *opts); +struct bpf_map ; LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
tools/lib/bpf/libbpf.map +1 -0
@@ -437,6 +437,7 @@ LIBBPF_1.6.0 { bpf_linker__add_fd; bpf_linker__new_fd; bpf_object__prepare; + bpf_program__attach_cgroup_opts; bpf_program__func_info; bpf_program__func_info_cnt; bpf_program__line_info;
Signature · 1 line
2.47.1
Yonghong Song
reviewerRFC PATCH 4/4 selftests/bpf: Add two selftests for mprog API based cgroup progs
In reply to Yonghong SongTwo tests are added:
- cgroup_mprog_opts, which mimics tc_opts.c ([1]). Both prog and link attach are tested. Some negative tests are also included.
- cgroup_mprog_ordering, which actually runs the program with some mprog API flags.
[1] https://github.com/torvalds/linux/blob/master/tools/testing/selftests/bpf/prog_tests/tc_opts.c
--- .../bpf/prog_tests/cgroup_mprog_opts.c | 752 ++++++++++++++++++ .../bpf/prog_tests/cgroup_mprog_ordering.c | 77 ++ .../selftests/bpf/progs/cgroup_mprog.c | 30 + 3 files changed, 859 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_mprog_opts.c create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_mprog_ordering.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_mprog.c
tools/testing/selftests/bpf/prog_tests/cgroup_mprog_opts.c new file +752 -0
@@ -0,0 +1,752 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ +#include <test_progs.h> +#include "cgroup_helpers.h" +#include "cgroup_mprog.skel.h" + +static __u32 id_from_prog_fd (int fd) +{ +struct bpf_prog_info prog_info = {}; +__u32 prog_info_len =sizeof (prog_info); +int err; + + err =bpf_obj_get_info_by_fd (fd, &prog_info, &prog_info_len); +if (!ASSERT_OK (err,"id_from_prog_fd" )) +return 0 ; + +ASSERT_NEQ (prog_info.id,0 ,"prog_info.id" ); +return prog_info.id; +} + +static void assert_mprog_count (int cg,int atype,int expected) +{ +__u32 count =0 , attach_flags =0 ; +int err; + + err =bpf_prog_query (cg, atype,0 , &attach_flags, +NULL , &count); +ASSERT_EQ (count, expected,"count" ); +ASSERT_EQ (err,0 ,"prog_query" ); +} + +static void test_prog_attach_detach (int atype) +{ +LIBBPF_OPTS (bpf_prog_attach_opts, opta); +LIBBPF_OPTS (bpf_prog_detach_opts, optd); +LIBBPF_OPTS (bpf_prog_query_opts, optq); +__u32 fd1, fd2, fd3, fd4, id1, id2, id3, id4; +struct cgroup_mprog *skel; +__u32 prog_ids[10 ]; +int cg, err; + + cg =test__join_cgroup ("/prog_attach_detach" ); +if (!ASSERT_GE (cg,0 ,"join_cgroup /prog_attach_detach" )) +return ; + + skel =cgroup_mprog__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_load" )) + goto cleanup; + + fd1 =bpf_program__fd (skel->progs.getsockopt_1); + fd2 =bpf_program__fd (skel->progs.getsockopt_2); + fd3 =bpf_program__fd (skel->progs.getsockopt_3); + fd4 =bpf_program__fd (skel->progs.getsockopt_4); + + id1 =id_from_prog_fd (fd1); + id2 =id_from_prog_fd (fd2); + id3 =id_from_prog_fd (fd3); + id4 =id_from_prog_fd (fd4); + +assert_mprog_count (cg, atype,0 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI, + .expected_revision = 1, + ); + +/* ordering: [fd1] */ + err =bpf_prog_attach_opts (fd1, cg, atype, &opta); +if (!ASSERT_EQ (err,0 ,"prog_attach" )) + goto cleanup; + +assert_mprog_count (cg, atype,1 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_BEFORE, + .expected_revision = 2, + ); + +/* ordering: [fd2, fd1] */ + err =bpf_prog_attach_opts (fd2, cg, atype, &opta); +if (!ASSERT_EQ (err,0 ,"prog_attach" )) + goto cleanup1; + +assert_mprog_count (cg, atype,2 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_AFTER, + .relative_fd = fd2, + .expected_revision = 3, + ); + +/* ordering: [fd2, fd3, fd1] */ + err =bpf_prog_attach_opts (fd3, cg, atype, &opta); +if (!ASSERT_EQ (err,0 ,"prog_attach" )) + goto cleanup2; + +assert_mprog_count (cg, atype,3 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI, + .expected_revision = 4, + ); + +/* ordering: [fd2, fd3, fd1, fd4] */ + err =bpf_prog_attach_opts (fd4, cg, atype, &opta); +if (!ASSERT_EQ (err,0 ,"prog_attach" )) + goto cleanup3; + +assert_mprog_count (cg, atype,4 ); + +/* retrieve optq.prog_cnt */ + err =bpf_prog_query_opts (cg, atype, &optq); +if (!ASSERT_OK (err,"prog_query" )) + goto cleanup4; + +/* optq.prog_cnt will be used in below query */ +memset (prog_ids,0 ,sizeof (prog_ids)); + optq.prog_ids = prog_ids; + err =bpf_prog_query_opts (cg, atype, &optq); +if (!ASSERT_OK (err,"prog_query" )) + goto cleanup4; + +ASSERT_EQ (optq.count,4 ,"count" ); +ASSERT_EQ (optq.revision,5 ,"revision" ); +ASSERT_EQ (optq.prog_ids[0 ], id2,"prog_ids[0]" ); +ASSERT_EQ (optq.prog_ids[1 ], id3,"prog_ids[1]" ); +ASSERT_EQ (optq.prog_ids[2 ], id1,"prog_ids[2]" ); +ASSERT_EQ (optq.prog_ids[3 ], id4,"prog_ids[3]" ); +ASSERT_EQ (optq.prog_ids[4 ],0 ,"prog_ids[4]" ); +ASSERT_EQ (optq.link_ids,NULL ,"link_ids" ); + +cleanup4: + optd.expected_revision =5 ; + err =bpf_prog_detach_opts (fd4, cg, atype, &optd); +ASSERT_OK (err,"prog_detach" ); +assert_mprog_count (cg, atype,3 ); + +cleanup3: +LIBBPF_OPTS_RESET (optd); + err =bpf_prog_detach_opts (fd3, cg, atype, &optd); +ASSERT_OK (err,"prog_detach" ); +assert_mprog_count (cg, atype,2 ); + +/* Check revision after two detach operations */ + err =bpf_prog_query_opts (cg, atype, &optq); +ASSERT_OK (err,"prog_query" ); +ASSERT_EQ (optq.revision,7 ,"revision" ); + +cleanup2: + err =bpf_prog_detach_opts (fd2, cg, atype, &optd); +ASSERT_OK (err,"prog_detach" ); +assert_mprog_count (cg, atype,1 ); + +cleanup1: + err =bpf_prog_detach_opts (fd1, cg, atype, &optd); +ASSERT_OK (err,"prog_detach" ); +assert_mprog_count (cg, atype,0 ); + +cleanup: +cgroup_mprog__destroy (skel); +close (cg); +} + +static void test_link_attach_detach (int atype) +{ +LIBBPF_OPTS (bpf_cgroup_opts, opta); +LIBBPF_OPTS (bpf_cgroup_opts, optd); +LIBBPF_OPTS (bpf_prog_query_opts, optq); +struct bpf_link *link1, *link2, *link3, *link4; +__u32 fd1, fd2, fd3, fd4, id1, id2, id3, id4; +struct cgroup_mprog *skel; +__u32 prog_ids[10 ]; +int cg, err; + + cg =test__join_cgroup ("/link_attach_detach" ); +if (!ASSERT_GE (cg,0 ,"join_cgroup /link_attach_detach" )) +return ; + + skel =cgroup_mprog__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_load" )) + goto cleanup; + + fd1 =bpf_program__fd (skel->progs.getsockopt_1); + fd2 =bpf_program__fd (skel->progs.getsockopt_2); + fd3 =bpf_program__fd (skel->progs.getsockopt_3); + fd4 =bpf_program__fd (skel->progs.getsockopt_4); + + id1 =id_from_prog_fd (fd1); + id2 =id_from_prog_fd (fd2); + id3 =id_from_prog_fd (fd3); + id4 =id_from_prog_fd (fd4); + +assert_mprog_count (cg, atype,0 ); + + LIBBPF_OPTS_RESET(opta, + .expected_revision = 1, + ); + +/* ordering: [fd1] */ + link1 =bpf_program__attach_cgroup_opts (skel->progs.getsockopt_1, cg, &opta); +if (!ASSERT_OK_PTR (link1,"link_attach" )) + goto cleanup; + +assert_mprog_count (cg, atype,1 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_BEFORE, + .expected_revision = 2, + ); + +/* ordering: [fd2, fd1] */ + link2 =bpf_program__attach_cgroup_opts (skel->progs.getsockopt_2, cg, &opta); +if (!ASSERT_OK_PTR (link2,"link_attach" )) + goto cleanup1; + +assert_mprog_count (cg, atype,2 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_AFTER, + .relative_fd = fd2, + .expected_revision = 3, + ); + +/* ordering: [fd2, fd3, fd1] */ + link3 =bpf_program__attach_cgroup_opts (skel->progs.getsockopt_3, cg, &opta); +if (!ASSERT_OK_PTR (link3,"link_attach" )) + goto cleanup2; + +assert_mprog_count (cg, atype,3 ); + + LIBBPF_OPTS_RESET(opta, + .expected_revision = 4, + ); + +/* ordering: [fd2, fd3, fd1, fd4] */ + link4 =bpf_program__attach_cgroup_opts (skel->progs.getsockopt_4, cg, &opta); +if (!ASSERT_OK_PTR (link4,"link_attach" )) + goto cleanup3; + +assert_mprog_count (cg, atype,4 ); + +/* retrieve optq.prog_cnt */ + err =bpf_prog_query_opts (cg, atype, &optq); +if (!ASSERT_OK (err,"prog_query" )) + goto cleanup4; + +/* optq.prog_cnt will be used in below query */ +memset (prog_ids,0 ,sizeof (prog_ids)); + optq.prog_ids = prog_ids; + err =bpf_prog_query_opts (cg, atype, &optq); +if (!ASSERT_OK (err,"prog_query" )) + goto cleanup4; + +ASSERT_EQ (optq.count,4 ,"count" ); +ASSERT_EQ (optq.revision,5 ,"revision" ); +ASSERT_EQ (optq.prog_ids[0 ], id2,"prog_ids[0]" ); +ASSERT_EQ (optq.prog_ids[1 ], id3,"prog_ids[1]" ); +ASSERT_EQ (optq.prog_ids[2 ], id1,"prog_ids[2]" ); +ASSERT_EQ (optq.prog_ids[3 ], id4,"prog_ids[3]" ); +ASSERT_EQ (optq.prog_ids[4 ],0 ,"prog_ids[4]" ); +ASSERT_EQ (optq.link_ids,NULL ,"link_ids" ); + +cleanup4: +bpf_link__destroy (link4); +assert_mprog_count (cg, atype,3 ); + +cleanup3: +bpf_link__destroy (link3); +assert_mprog_count (cg, atype,2 ); + +/* Check revision after two detach operations */ + err =bpf_prog_query_opts (cg, atype, &optq); +ASSERT_OK (err,"prog_query" ); +ASSERT_EQ (optq.revision,7 ,"revision" ); + +cleanup2: +bpf_link__destroy (link2); +assert_mprog_count (cg, atype,1 ); + +cleanup1: +bpf_link__destroy (link1); +assert_mprog_count (cg, atype,0 ); + +cleanup: +cgroup_mprog__destroy (skel); +close (cg); +} + +static void test_mix_attach_detach (int atype) +{ +LIBBPF_OPTS (bpf_cgroup_opts, lopta); +LIBBPF_OPTS (bpf_cgroup_opts, loptd); +LIBBPF_OPTS (bpf_prog_attach_opts, opta); +LIBBPF_OPTS (bpf_prog_detach_opts, optd); +LIBBPF_OPTS (bpf_prog_query_opts, optq); +__u32 fd1, fd2, fd3, fd4, id1, id2, id3, id4; +struct bpf_link *link2, *link4; +struct cgroup_mprog *skel; +__u32 prog_ids[10 ]; +int cg, err; + + cg =test__join_cgroup ("/mix_attach_detach" ); +if (!ASSERT_GE (cg,0 ,"join_cgroup /mix_attach_detach" )) +return ; + + skel =cgroup_mprog__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_load" )) + goto cleanup; + + fd1 =bpf_program__fd (skel->progs.getsockopt_1); + fd2 =bpf_program__fd (skel->progs.getsockopt_2); + fd3 =bpf_program__fd (skel->progs.getsockopt_3); + fd4 =bpf_program__fd (skel->progs.getsockopt_4); + + id1 =id_from_prog_fd (fd1); + id2 =id_from_prog_fd (fd2); + id3 =id_from_prog_fd (fd3); + id4 =id_from_prog_fd (fd4); + +assert_mprog_count (cg, atype,0 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI, + .expected_revision = 1, + ); + +/* ordering: [fd1] */ + err =bpf_prog_attach_opts (fd1, cg, atype, &opta); +if (!ASSERT_EQ (err,0 ,"prog_attach" )) + goto cleanup; + +assert_mprog_count (cg, atype,1 ); + + LIBBPF_OPTS_RESET(lopta, + .flags = BPF_F_BEFORE, + .expected_revision = 2, + ); + +/* ordering: [fd2, fd1] */ + link2 =bpf_program__attach_cgroup_opts (skel->progs.getsockopt_2, cg, &lopta); +if (!ASSERT_OK_PTR (link2,"link_attach" )) + goto cleanup1; + +assert_mprog_count (cg, atype,2 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_AFTER, + .relative_fd = fd2, + .expected_revision = 3, + ); + +/* ordering: [fd2, fd3, fd1] */ + err =bpf_prog_attach_opts (fd3, cg, atype, &opta); +if (!ASSERT_EQ (err,0 ,"prog_attach" )) + goto cleanup2; + +assert_mprog_count (cg, atype,3 ); + + LIBBPF_OPTS_RESET(lopta, + .expected_revision = 4, + ); + +/* ordering: [fd2, fd3, fd1, fd4] */ + link4 =bpf_program__attach_cgroup_opts (skel->progs.getsockopt_4, cg, &lopta); +if (!ASSERT_OK_PTR (link4,"link_attach" )) + goto cleanup3; + +assert_mprog_count (cg, atype,4 ); + +/* retrieve optq.prog_cnt */ + err =bpf_prog_query_opts (cg, atype, &optq); +if (!ASSERT_OK (err,"prog_query" )) + goto cleanup4; + +/* optq.prog_cnt will be used in below query */ +memset (prog_ids,0 ,sizeof (prog_ids)); + optq.prog_ids = prog_ids; + err =bpf_prog_query_opts (cg, atype, &optq); +if (!ASSERT_OK (err,"prog_query" )) + goto cleanup4; + +ASSERT_EQ (optq.count,4 ,"count" ); +ASSERT_EQ (optq.revision,5 ,"revision" ); +ASSERT_EQ (optq.prog_ids[0 ], id2,"prog_ids[0]" ); +ASSERT_EQ (optq.prog_ids[1 ], id3,"prog_ids[1]" ); +ASSERT_EQ (optq.prog_ids[2 ], id1,"prog_ids[2]" ); +ASSERT_EQ (optq.prog_ids[3 ], id4,"prog_ids[3]" ); +ASSERT_EQ (optq.prog_ids[4 ],0 ,"prog_ids[4]" ); +ASSERT_EQ (optq.link_ids,NULL ,"link_ids" ); + +cleanup4: +bpf_link__destroy (link4); +assert_mprog_count (cg, atype,3 ); + +cleanup3: + err =bpf_prog_detach_opts (fd3, cg, atype, &optd); +ASSERT_OK (err,"prog_detach" ); +assert_mprog_count (cg, atype,2 ); + +/* Check revision after two detach operations */ + err =bpf_prog_query_opts (cg, atype, &optq); +ASSERT_OK (err,"prog_query" ); +ASSERT_EQ (optq.revision,7 ,"revision" ); + +cleanup2: +bpf_link__destroy (link2); +assert_mprog_count (cg, atype,1 ); + +cleanup1: + err =bpf_prog_detach_opts (fd1, cg, atype, &optd); +ASSERT_OK (err,"prog_detach" ); +assert_mprog_count (cg, atype,0 ); + +cleanup: +cgroup_mprog__destroy (skel); +close (cg); +} + +static void test_preorder_prog_attach_detach (int atype) +{ +LIBBPF_OPTS (bpf_prog_attach_opts, opta); +LIBBPF_OPTS (bpf_prog_detach_opts, optd); +__u32 fd1, fd2, fd3, fd4; +struct cgroup_mprog *skel; +int cg, err; + + cg =test__join_cgroup ("/preorder_prog_attach_detach" ); +if (!ASSERT_GE (cg,0 ,"join_cgroup /preorder_prog_attach_detach" )) +return ; + + skel =cgroup_mprog__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_load" )) + goto cleanup; + + fd1 =bpf_program__fd (skel->progs.getsockopt_1); + fd2 =bpf_program__fd (skel->progs.getsockopt_2); + fd3 =bpf_program__fd (skel->progs.getsockopt_3); + fd4 =bpf_program__fd (skel->progs.getsockopt_4); + +assert_mprog_count (cg, atype,0 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI, + .expected_revision = 1, + ); + +/* ordering: [fd1] */ + err =bpf_prog_attach_opts (fd1, cg, atype, &opta); +if (!ASSERT_EQ (err,0 ,"prog_attach" )) + goto cleanup; + +assert_mprog_count (cg, atype,1 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_PREORDER, + .expected_revision = 2, + ); + +/* ordering: [fd1, fd2] */ + err =bpf_prog_attach_opts (fd2, cg, atype, &opta); +if (!ASSERT_EQ (err,0 ,"prog_attach" )) + goto cleanup1; + +assert_mprog_count (cg, atype,2 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_AFTER, + .relative_fd = fd2, + .expected_revision = 3, + ); + + err =bpf_prog_attach_opts (fd3, cg, atype, &opta); +if (!ASSERT_EQ (err, -EINVAL ,"prog_attach" )) + goto cleanup2; + +assert_mprog_count (cg, atype,2 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_AFTER | BPF_F_PREORDER, + .relative_fd = fd2, + .expected_revision = 3, + ); + +/* ordering: [fd1, fd2, fd3] */ + err =bpf_prog_attach_opts (fd3, cg, atype, &opta); +if (!ASSERT_EQ (err,0 ,"prog_attach" )) + goto cleanup2; + +assert_mprog_count (cg, atype,3 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI, + .expected_revision = 4, + ); + +/* ordering: [fd2, fd3, fd1, fd4] */ + err =bpf_prog_attach_opts (fd4, cg, atype, &opta); +if (!ASSERT_EQ (err,0 ,"prog_attach" )) + goto cleanup3; + +assert_mprog_count (cg, atype,4 ); + + err =bpf_prog_detach_opts (fd4, cg, atype, &optd); +ASSERT_OK (err,"prog_detach" ); +assert_mprog_count (cg, atype,3 ); + +cleanup3: + err =bpf_prog_detach_opts (fd3, cg, atype, &optd); +ASSERT_OK (err,"prog_detach" ); +assert_mprog_count (cg, atype,2 ); + +cleanup2: + err =bpf_prog_detach_opts (fd2, cg, atype, &optd); +ASSERT_OK (err,"prog_detach" ); +assert_mprog_count (cg, atype,1 ); + +cleanup1: + err =bpf_prog_detach_opts (fd1, cg, atype, &optd); +ASSERT_OK (err,"prog_detach" ); +assert_mprog_count (cg, atype,0 ); + +cleanup: +cgroup_mprog__destroy (skel); +close (cg); +} + +static void test_preorder_link_attach_detach (int atype) +{ +LIBBPF_OPTS (bpf_cgroup_opts, opta); +struct bpf_link *link1, *link2, *link3, *link4; +struct cgroup_mprog *skel; +__u32 fd2; +int cg; + + cg =test__join_cgroup ("/preorder_link_attach_detach" ); +if (!ASSERT_GE (cg,0 ,"join_cgroup /preorder_link_attach_detach" )) +return ; + + skel =cgroup_mprog__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_load" )) + goto cleanup; + + fd2 =bpf_program__fd (skel->progs.getsockopt_2); + +assert_mprog_count (cg, atype,0 ); + + LIBBPF_OPTS_RESET(opta, + .expected_revision = 1, + ); + +/* ordering: [fd1] */ + link1 =bpf_program__attach_cgroup_opts (skel->progs.getsockopt_1, cg, &opta); +if (!ASSERT_OK_PTR (link1,"link_attach" )) + goto cleanup; + +assert_mprog_count (cg, atype,1 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_PREORDER, + .expected_revision = 2, + ); + +/* ordering: [fd1, fd2] */ + link2 =bpf_program__attach_cgroup_opts (skel->progs.getsockopt_2, cg, &opta); +if (!ASSERT_OK_PTR (link2,"link_attach" )) + goto cleanup1; + +assert_mprog_count (cg, atype,2 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_AFTER, + .relative_fd = fd2, + .expected_revision = 3, + ); + + link3 =bpf_program__attach_cgroup_opts (skel->progs.getsockopt_3, cg, &opta); +if (!ASSERT_ERR_PTR (link3,"link_attach" )) + goto cleanup2; + +assert_mprog_count (cg, atype,2 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_AFTER | BPF_F_PREORDER, + .relative_fd = fd2, + .expected_revision = 3, + ); + +/* ordering: [fd1, fd2, fd3] */ + link3 =bpf_program__attach_cgroup_opts (skel->progs.getsockopt_3, cg, &opta); +if (!ASSERT_OK_PTR (link3,"link_attach" )) + goto cleanup2; + +assert_mprog_count (cg, atype,3 ); + + LIBBPF_OPTS_RESET(opta, + .expected_revision = 4, + ); + +/* ordering: [fd2, fd3, fd1, fd4] */ + link4 =bpf_program__attach_cgroup_opts (skel->progs.getsockopt_4, cg, &opta); +if (!ASSERT_OK_PTR (link4,"prog_attach" )) + goto cleanup3; + +assert_mprog_count (cg, atype,4 ); + +bpf_link__destroy (link4); +assert_mprog_count (cg, atype,3 ); + +cleanup3: +bpf_link__destroy (link3); +assert_mprog_count (cg, atype,2 ); + +cleanup2: +bpf_link__destroy (link2); +assert_mprog_count (cg, atype,1 ); + +cleanup1: +bpf_link__destroy (link1); +assert_mprog_count (cg, atype,0 ); + +cleanup: +cgroup_mprog__destroy (skel); +close (cg); +} + +static void test_invalid_attach_detach (int atype) +{ +LIBBPF_OPTS (bpf_prog_attach_opts, opta); +__u32 fd1, fd2, id2; +struct cgroup_mprog *skel; +int cg, err; + + cg =test__join_cgroup ("/invalid_attach_detach" ); +if (!ASSERT_GE (cg,0 ,"join_cgroup /invalid_attach_detach" )) +return ; + + skel =cgroup_mprog__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_load" )) + goto cleanup; + + fd1 =bpf_program__fd (skel->progs.getsockopt_1); + fd2 =bpf_program__fd (skel->progs.getsockopt_2); + + id2 =id_from_prog_fd (fd2); + +assert_mprog_count (cg, atype,0 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_BEFORE | BPF_F_AFTER, + ); + + err =bpf_prog_attach_opts (fd1, cg, atype, &opta); +ASSERT_EQ (err, -EINVAL ,"prog_attach" ); +assert_mprog_count (cg, atype,0 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_BEFORE | BPF_F_ID, + ); + + err =bpf_prog_attach_opts (fd1, cg, atype, &opta); +ASSERT_EQ (err, -ENOENT ,"prog_attach" ); +assert_mprog_count (cg, atype,0 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_AFTER | BPF_F_ID, + ); + + err =bpf_prog_attach_opts (fd1, cg, atype, &opta); +ASSERT_EQ (err, -ENOENT ,"prog_attach" ); +assert_mprog_count (cg, atype,0 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_BEFORE | BPF_F_AFTER, + .relative_id = id2, + ); + + err =bpf_prog_attach_opts (fd1, cg, atype, &opta); +ASSERT_EQ (err, -EINVAL ,"prog_attach" ); +assert_mprog_count (cg, atype,0 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_ID, + .relative_id = id2, + ); + + err =bpf_prog_attach_opts (fd1, cg, atype, &opta); +ASSERT_EQ (err, -EINVAL ,"prog_attach" ); +assert_mprog_count (cg, atype,0 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_BEFORE, + .relative_fd = fd1, + ); + + err =bpf_prog_attach_opts (fd1, cg, atype, &opta); +ASSERT_EQ (err, -ENOENT ,"prog_attach" ); +assert_mprog_count (cg, atype,0 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_AFTER, + .relative_fd = fd1, + ); + + err =bpf_prog_attach_opts (fd1, cg, atype, &opta); +ASSERT_EQ (err, -ENOENT ,"prog_attach" ); +assert_mprog_count (cg, atype,0 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI, + ); + + err =bpf_prog_attach_opts (fd1, cg, atype, &opta); +if (!ASSERT_EQ (err,0 ,"prog_attach" )) + goto cleanup; +assert_mprog_count (cg, atype,1 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_AFTER, + ); + + err =bpf_prog_attach_opts (fd1, cg, atype, &opta); +ASSERT_EQ (err, -EINVAL ,"prog_attach" ); +assert_mprog_count (cg, atype,1 ); + + LIBBPF_OPTS_RESET(opta, + .flags = BPF_F_ALLOW_MULTI | BPF_F_REPLACE | BPF_F_AFTER, + .replace_prog_fd = fd1, + ); + + err =bpf_prog_attach_opts (fd1, cg, atype, &opta); +ASSERT_EQ (err, -EINVAL ,"prog_attach" ); +assert_mprog_count (cg, atype,1 ); +cleanup: +cgroup_mprog__destroy (skel); +close (cg); +} + +void test_cgroup_mprog_opts (void ) +{ +if (test__start_subtest ("prog_attach_detach" )) +test_prog_attach_detach (BPF_CGROUP_GETSOCKOPT ); +if (test__start_subtest ("link_attach_detach" )) +test_link_attach_detach (BPF_CGROUP_GETSOCKOPT ); +if (test__start_subtest ("mix_attach_detach" )) +test_mix_attach_detach (BPF_CGROUP_GETSOCKOPT ); +if (test__start_subtest ("preorder_prog_attach_detach" )) +test_preorder_prog_attach_detach (BPF_CGROUP_GETSOCKOPT ); +if (test__start_subtest ("preorder_link_attach_detach" )) +test_preorder_link_attach_detach (BPF_CGROUP_GETSOCKOPT ); +if (test__start_subtest ("invalid_attach_detach" )) +test_invalid_attach_detach (BPF_CGROUP_GETSOCKOPT ); +}
tools/testing/selftests/bpf/prog_tests/cgroup_mprog_ordering.c new file +77 -0
77 lines not on this page · raw message
tools/testing/selftests/bpf/progs/cgroup_mprog.c new file +30 -0
@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +SEC("cgroup/getsockopt") +int getsockopt_1 (struct bpf_sockopt *ctx) +{ +return 1 ; +} + +SEC("cgroup/getsockopt") +int getsockopt_2 (struct bpf_sockopt *ctx) +{ +return 1 ; +} + +SEC("cgroup/getsockopt") +int getsockopt_3 (struct bpf_sockopt *ctx) +{ +return 1 ; +} + +SEC("cgroup/getsockopt") +int getsockopt_4 (struct bpf_sockopt *ctx) +{ +return 1 ; +}
Signature · 1 line
2.47.1
Quoted 10 lines · Yonghong Song
Current cgroup prog ordering is appending at attachment time. This is not ideal. In some cases, users want specific ordering at a particular cgroup level. For example, in Meta, we have a case where three different applications all have cgroup/setsockopt progs and they require specific ordering. Current approach is to use a bpfchainer where one bpf prog contains multiple global functions and each global function can be freplaced by a prog for a specific application. The ordering of global functions decides the ordering of those application specific bpf progs. Using bpftrainer is a centralized approach and is not desirable as
typo: bpfchainer
Quoted 1 line
one of applications acts as a deamon. The decentralized attachment
typo: daemon
Quoted 48 lines
approach is more favorable for those applications.
To address this, the existing mprog API ([2]) seems an ideal solution with supporting
BPF_F_BEFOREandBPF_F_AFTERflags on top of existing cgroup bpf implementation. More specifically, the support is added for prog/link attachment withBPF_F_BEFOREandBPF_F_AFTER. The kernel mprog interface ([2]) is not used and the implementation is directly done in cgroup bpf code base. The mprog 'revision' is also implemented in attach/detach/replace, so users can query revision number to check the change of cgroup prog list.The patch set contains 4 patches. Patch 1 adds revision support for cgroup bpf progs. Patch 2 implements mprog API implementation for prog/link attach and revision update. Patch 3 adds a new libbpf API to do cgroup link attach with flags like BPF_F_BEFORE/BPF_F_AFTER. Patch 4 adds two tests to validate the implementation.
[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]Yonghong Song (4):
cgroup: Add bpf prog revisions to structcgroup_bpf
bpf: Implement mprog API on top of existing cgroup progs
libbpf: Support link-based cgroup attach with options selftests/bpf: Add two selftests for mprog API based cgroup progsinclude/linux/bpf-cgroup-defs.h | 1 + include/uapi/linux/bpf.h | 7 + kernel/bpf/cgroup.c | 151 +++- kernel/bpf/syscall.c | 58 +- kernel/cgroup/cgroup.c | 5 +- tools/include/uapi/linux/bpf.h | 7 + tools/lib/bpf/bpf.c | 44 + tools/lib/bpf/bpf.h | 5 + tools/lib/bpf/libbpf.c | 28 + tools/lib/bpf/libbpf.h | 15 + tools/lib/bpf/libbpf.map | 1 + .../bpf/prog_tests/cgroup_mprog_opts.c | 752 ++++++++++++++++++ .../bpf/prog_tests/cgroup_mprog_ordering.c | 77 ++ .../selftests/bpf/progs/cgroup_mprog.c | 30 + 14 files changed, 1138 insertions(+), 43 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_mprog_opts.c create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_mprog_ordering.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_mprog.c--
2.47.1