* [PATCH bpf-next v2 1/2] bpf, cgroup: Fix attach flags being assigned to effective progs
2022-09-08 14:53 [PATCH bpf-next v2 0/2] Fix cgroup attach flags being assigned to effective progs Pu Lehui
@ 2022-09-08 14:53 ` Pu Lehui
2022-09-08 14:53 ` [PATCH bpf-next v2 2/2] bpftool: Fix cgroup " Pu Lehui
2022-09-08 17:28 ` [PATCH bpf-next v2 0/2] " sdf
2 siblings, 0 replies; 6+ messages in thread
From: Pu Lehui @ 2022-09-08 14:53 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Quentin Monnet, Martin KaFai Lau, Song Liu, Yonghong Song,
John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
Pu Lehui
From: Pu Lehui <pulehui@huawei.com>
When root-cgroup attach multi progs and sub-cgroup attach a override
prog, query sub-cgroup with effective flags will incorrectly fill
the prog_attach_flags array. Attach flags is only valid for attached
progs of this layer cgroup, but not for effective progs. We know that
the attached progs is at the beginning of the effective progs array,
so we can just populate the elements in front of the prog_attach_flags
array.
Signed-off-by: Pu Lehui <pulehui@huawei.com>
---
kernel/bpf/cgroup.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index 00c7f864900e..9c439d163a0b 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -1093,11 +1093,14 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
}
if (prog_attach_flags) {
+ int progs_cnt = min_t(int, prog_list_length(&cgrp->bpf.progs[atype]), total_cnt);
flags = cgrp->bpf.flags[atype];
- for (i = 0; i < cnt; i++)
+ /* attach flags only valid for attached progs, but not effective progs */
+ for (i = 0; i < progs_cnt; i++)
if (copy_to_user(prog_attach_flags + i, &flags, sizeof(flags)))
return -EFAULT;
+
prog_attach_flags += cnt;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH bpf-next v2 2/2] bpftool: Fix cgroup attach flags being assigned to effective progs
2022-09-08 14:53 [PATCH bpf-next v2 0/2] Fix cgroup attach flags being assigned to effective progs Pu Lehui
2022-09-08 14:53 ` [PATCH bpf-next v2 1/2] bpf, cgroup: Fix " Pu Lehui
@ 2022-09-08 14:53 ` Pu Lehui
2022-09-08 17:28 ` [PATCH bpf-next v2 0/2] " sdf
2 siblings, 0 replies; 6+ messages in thread
From: Pu Lehui @ 2022-09-08 14:53 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Quentin Monnet, Martin KaFai Lau, Song Liu, Yonghong Song,
John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
Pu Lehui
From: Pu Lehui <pulehui@huawei.com>
When root-cgroup attach multi progs and sub-cgroup attach a
override prog, bpftool will display incorrectly for the attach
flags of the sub-cgroup’s effective progs:
$ bpftool cgroup tree /sys/fs/cgroup effective
CgroupPath
ID AttachType AttachFlags Name
/sys/fs/cgroup
6 cgroup_sysctl multi sysctl_tcp_mem
13 cgroup_sysctl multi sysctl_tcp_mem
/sys/fs/cgroup/cg1
20 cgroup_sysctl override sysctl_tcp_mem
6 cgroup_sysctl override sysctl_tcp_mem <- wrong
13 cgroup_sysctl override sysctl_tcp_mem <- wrong
/sys/fs/cgroup/cg1/cg2
20 cgroup_sysctl sysctl_tcp_mem
6 cgroup_sysctl sysctl_tcp_mem
13 cgroup_sysctl sysctl_tcp_mem
Attach flags is only valid for attached progs of this layer
cgroup, but not for effective progs. Since prog_attach_flags
array is already bypass the effective progs, so we can just
use it.
Signed-off-by: Pu Lehui <pulehui@huawei.com>
---
tools/bpf/bpftool/cgroup.c | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/tools/bpf/bpftool/cgroup.c b/tools/bpf/bpftool/cgroup.c
index cced668fb2a3..fa3eef0ff860 100644
--- a/tools/bpf/bpftool/cgroup.c
+++ b/tools/bpf/bpftool/cgroup.c
@@ -219,11 +219,7 @@ static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
return 0;
for (iter = 0; iter < p.prog_cnt; iter++) {
- __u32 attach_flags;
-
- attach_flags = prog_attach_flags[iter] ?: p.attach_flags;
-
- switch (attach_flags) {
+ switch (prog_attach_flags[iter]) {
case BPF_F_ALLOW_MULTI:
attach_flags_str = "multi";
break;
@@ -234,7 +230,8 @@ static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
attach_flags_str = "";
break;
default:
- snprintf(buf, sizeof(buf), "unknown(%x)", attach_flags);
+ snprintf(buf, sizeof(buf), "unknown(%x)",
+ prog_attach_flags[iter]);
attach_flags_str = buf;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH bpf-next v2 0/2] Fix cgroup attach flags being assigned to effective progs
2022-09-08 14:53 [PATCH bpf-next v2 0/2] Fix cgroup attach flags being assigned to effective progs Pu Lehui
2022-09-08 14:53 ` [PATCH bpf-next v2 1/2] bpf, cgroup: Fix " Pu Lehui
2022-09-08 14:53 ` [PATCH bpf-next v2 2/2] bpftool: Fix cgroup " Pu Lehui
@ 2022-09-08 17:28 ` sdf
2022-09-08 19:13 ` Martin KaFai Lau
2022-09-13 13:35 ` Pu Lehui
2 siblings, 2 replies; 6+ messages in thread
From: sdf @ 2022-09-08 17:28 UTC (permalink / raw)
To: Pu Lehui
Cc: bpf, linux-kernel, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Quentin Monnet, Martin KaFai Lau, Song Liu,
Yonghong Song, John Fastabend, KP Singh, Hao Luo, Jiri Olsa,
Pu Lehui
On 09/08, Pu Lehui wrote:
> From: Pu Lehui <pulehui@huawei.com>
> When root-cgroup attach multi progs and sub-cgroup attach a
> override prog, bpftool will display incorrectly for the attach
> flags of the sub-cgroup’s effective progs:
> $ bpftool cgroup tree /sys/fs/cgroup effective
> CgroupPath
> ID AttachType AttachFlags Name
> /sys/fs/cgroup
> 6 cgroup_sysctl multi sysctl_tcp_mem
> 13 cgroup_sysctl multi sysctl_tcp_mem
> /sys/fs/cgroup/cg1
> 20 cgroup_sysctl override sysctl_tcp_mem
> 6 cgroup_sysctl override sysctl_tcp_mem <- wrong
> 13 cgroup_sysctl override sysctl_tcp_mem <- wrong
> /sys/fs/cgroup/cg1/cg2
> 20 cgroup_sysctl sysctl_tcp_mem
> 6 cgroup_sysctl sysctl_tcp_mem
> 13 cgroup_sysctl sysctl_tcp_mem
> For cg1, obviously, the attach flags of prog6 and prog13 can not be
> OVERRIDE, and the attach flags of prog6 and prog13 is meaningless for
> cg1. We only need to care the attach flags of prog which attached to
> cg1, other progs attach flags should be omit. After these patches,
> the above situation will show as bellow:
> $ bpftool cgroup tree /sys/fs/cgroup effective
> CgroupPath
> ID AttachType AttachFlags Name
> /sys/fs/cgroup
> 6 cgroup_sysctl multi sysctl_tcp_mem
> 13 cgroup_sysctl multi sysctl_tcp_mem
> /sys/fs/cgroup/cg1
> 20 cgroup_sysctl override sysctl_tcp_mem
> 6 cgroup_sysctl sysctl_tcp_mem
> 13 cgroup_sysctl sysctl_tcp_mem
> /sys/fs/cgroup/cg1/cg2
> 20 cgroup_sysctl sysctl_tcp_mem
> 6 cgroup_sysctl sysctl_tcp_mem
> 13 cgroup_sysctl sysctl_tcp_mem
> v2:
> - Limit prog_cnt to avoid overflow. (John)
> - Add more detail message.
John also raised a good question in v1: the flags don't seem to
make sense when requesting effective list. So maybe not export them
at all?
> v1:
> https://lore.kernel.org/bpf/20220820120234.2121044-1-pulehui@huawei.com
> Pu Lehui (2):
> bpf, cgroup: Fix attach flags being assigned to effective progs
> bpftool: Fix cgroup attach flags being assigned to effective progs
> kernel/bpf/cgroup.c | 5 ++++-
> tools/bpf/bpftool/cgroup.c | 9 +++------
> 2 files changed, 7 insertions(+), 7 deletions(-)
> --
> 2.25.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next v2 0/2] Fix cgroup attach flags being assigned to effective progs
2022-09-08 17:28 ` [PATCH bpf-next v2 0/2] " sdf
@ 2022-09-08 19:13 ` Martin KaFai Lau
2022-09-13 13:35 ` Pu Lehui
1 sibling, 0 replies; 6+ messages in thread
From: Martin KaFai Lau @ 2022-09-08 19:13 UTC (permalink / raw)
To: sdf, Pu Lehui
Cc: bpf, linux-kernel, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Quentin Monnet, Song Liu, Yonghong Song,
John Fastabend, KP Singh, Hao Luo, Jiri Olsa, Pu Lehui
On 9/8/22 10:28 AM, sdf@google.com wrote:
> On 09/08, Pu Lehui wrote:
>> From: Pu Lehui <pulehui@huawei.com>
>
>> When root-cgroup attach multi progs and sub-cgroup attach a
>> override prog, bpftool will display incorrectly for the attach
>> flags of the sub-cgroup’s effective progs:
>
>> $ bpftool cgroup tree /sys/fs/cgroup effective
>> CgroupPath
>> ID AttachType AttachFlags Name
>> /sys/fs/cgroup
>> 6 cgroup_sysctl multi sysctl_tcp_mem
>> 13 cgroup_sysctl multi sysctl_tcp_mem
>> /sys/fs/cgroup/cg1
>> 20 cgroup_sysctl override sysctl_tcp_mem
>> 6 cgroup_sysctl override sysctl_tcp_mem <- wrong
>> 13 cgroup_sysctl override sysctl_tcp_mem <- wrong
>> /sys/fs/cgroup/cg1/cg2
>> 20 cgroup_sysctl sysctl_tcp_mem
>> 6 cgroup_sysctl sysctl_tcp_mem
>> 13 cgroup_sysctl sysctl_tcp_mem
>
>> For cg1, obviously, the attach flags of prog6 and prog13 can not be
>> OVERRIDE, and the attach flags of prog6 and prog13 is meaningless for
>> cg1. We only need to care the attach flags of prog which attached to
>> cg1, other progs attach flags should be omit. After these patches,
>> the above situation will show as bellow:
>
>> $ bpftool cgroup tree /sys/fs/cgroup effective
>> CgroupPath
>> ID AttachType AttachFlags Name
>> /sys/fs/cgroup
>> 6 cgroup_sysctl multi sysctl_tcp_mem
>> 13 cgroup_sysctl multi sysctl_tcp_mem
>> /sys/fs/cgroup/cg1
>> 20 cgroup_sysctl override sysctl_tcp_mem
>> 6 cgroup_sysctl sysctl_tcp_mem
>> 13 cgroup_sysctl sysctl_tcp_mem
>> /sys/fs/cgroup/cg1/cg2
>> 20 cgroup_sysctl sysctl_tcp_mem
>> 6 cgroup_sysctl sysctl_tcp_mem
>> 13 cgroup_sysctl sysctl_tcp_mem
>
>> v2:
>> - Limit prog_cnt to avoid overflow. (John)
>> - Add more detail message.
>
> John also raised a good question in v1: the flags don't seem to
> make sense when requesting effective list. So maybe not export them
> at all?
+1. not exporting them for 'effective' listing makes sense.
This seems to be the day one behavior instead of the recent
prog_attach_flags changes? so bpf-next makes sense also.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next v2 0/2] Fix cgroup attach flags being assigned to effective progs
2022-09-08 17:28 ` [PATCH bpf-next v2 0/2] " sdf
2022-09-08 19:13 ` Martin KaFai Lau
@ 2022-09-13 13:35 ` Pu Lehui
1 sibling, 0 replies; 6+ messages in thread
From: Pu Lehui @ 2022-09-13 13:35 UTC (permalink / raw)
To: sdf, John Fastabend, Martin KaFai Lau
Cc: bpf, linux-kernel, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Quentin Monnet, Song Liu, Yonghong Song,
KP Singh, Hao Luo, Jiri Olsa, Pu Lehui
On 2022/9/9 1:28, sdf@google.com wrote:
> On 09/08, Pu Lehui wrote:
>> From: Pu Lehui <pulehui@huawei.com>
>
>> When root-cgroup attach multi progs and sub-cgroup attach a
>> override prog, bpftool will display incorrectly for the attach
>> flags of the sub-cgroup’s effective progs:
>
>> $ bpftool cgroup tree /sys/fs/cgroup effective
>> CgroupPath
>> ID AttachType AttachFlags Name
>> /sys/fs/cgroup
>> 6 cgroup_sysctl multi sysctl_tcp_mem
>> 13 cgroup_sysctl multi sysctl_tcp_mem
>> /sys/fs/cgroup/cg1
>> 20 cgroup_sysctl override sysctl_tcp_mem
>> 6 cgroup_sysctl override sysctl_tcp_mem <- wrong
>> 13 cgroup_sysctl override sysctl_tcp_mem <- wrong
>> /sys/fs/cgroup/cg1/cg2
>> 20 cgroup_sysctl sysctl_tcp_mem
>> 6 cgroup_sysctl sysctl_tcp_mem
>> 13 cgroup_sysctl sysctl_tcp_mem
>
>> For cg1, obviously, the attach flags of prog6 and prog13 can not be
>> OVERRIDE, and the attach flags of prog6 and prog13 is meaningless for
>> cg1. We only need to care the attach flags of prog which attached to
>> cg1, other progs attach flags should be omit. After these patches,
>> the above situation will show as bellow:
>
>> $ bpftool cgroup tree /sys/fs/cgroup effective
>> CgroupPath
>> ID AttachType AttachFlags Name
>> /sys/fs/cgroup
>> 6 cgroup_sysctl multi sysctl_tcp_mem
>> 13 cgroup_sysctl multi sysctl_tcp_mem
>> /sys/fs/cgroup/cg1
>> 20 cgroup_sysctl override sysctl_tcp_mem
>> 6 cgroup_sysctl sysctl_tcp_mem
>> 13 cgroup_sysctl sysctl_tcp_mem
>> /sys/fs/cgroup/cg1/cg2
>> 20 cgroup_sysctl sysctl_tcp_mem
>> 6 cgroup_sysctl sysctl_tcp_mem
>> 13 cgroup_sysctl sysctl_tcp_mem
>
>> v2:
>> - Limit prog_cnt to avoid overflow. (John)
>> - Add more detail message.
>
> John also raised a good question in v1: the flags don't seem to
> make sense when requesting effective list. So maybe not export them
> at all?
>
Misunderstood John's meaning. will remove the attach flags when query
with EFFECTIVE in next version. Thanks all.
>> v1:
>> https://lore.kernel.org/bpf/20220820120234.2121044-1-pulehui@huawei.com
>
>> Pu Lehui (2):
>> bpf, cgroup: Fix attach flags being assigned to effective progs
>> bpftool: Fix cgroup attach flags being assigned to effective progs
>
>> kernel/bpf/cgroup.c | 5 ++++-
>> tools/bpf/bpftool/cgroup.c | 9 +++------
>> 2 files changed, 7 insertions(+), 7 deletions(-)
>
>> --
>> 2.25.1
>
^ permalink raw reply [flat|nested] 6+ messages in thread