Skip to content

Commit bdcee1b

Browse files
Pu LehuiMartin KaFai Lau
authored andcommitted
bpftool: Fix wrong cgroup attach flags being assigned to effective progs
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. For querying with EFFECTIVE flags, exporting attach flags does not make sense. So let's remove the AttachFlags field and the associated logic. After this patch, the above effective cgroup tree will show as bellow: $ bpftool cgroup tree /sys/fs/cgroup effective CgroupPath ID AttachType Name /sys/fs/cgroup 6 cgroup_sysctl sysctl_tcp_mem 13 cgroup_sysctl sysctl_tcp_mem /sys/fs/cgroup/cg1 20 cgroup_sysctl 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 Fixes: b79c9fc ("bpf: implement BPF_PROG_QUERY for BPF_LSM_CGROUP") Fixes: a98bf57 ("tools: bpftool: add support for reporting the effective cgroup progs") Signed-off-by: Pu Lehui <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent 0e426a3 commit bdcee1b

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

tools/bpf/bpftool/cgroup.c

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ static int show_bpf_prog(int id, enum bpf_attach_type attach_type,
136136
jsonw_string_field(json_wtr, "attach_type", attach_type_str);
137137
else
138138
jsonw_uint_field(json_wtr, "attach_type", attach_type);
139-
jsonw_string_field(json_wtr, "attach_flags",
140-
attach_flags_str);
139+
if (!(query_flags & BPF_F_QUERY_EFFECTIVE))
140+
jsonw_string_field(json_wtr, "attach_flags", attach_flags_str);
141141
jsonw_string_field(json_wtr, "name", prog_name);
142142
if (attach_btf_name)
143143
jsonw_string_field(json_wtr, "attach_btf_name", attach_btf_name);
@@ -150,7 +150,10 @@ static int show_bpf_prog(int id, enum bpf_attach_type attach_type,
150150
printf("%-15s", attach_type_str);
151151
else
152152
printf("type %-10u", attach_type);
153-
printf(" %-15s %-15s", attach_flags_str, prog_name);
153+
if (query_flags & BPF_F_QUERY_EFFECTIVE)
154+
printf(" %-15s", prog_name);
155+
else
156+
printf(" %-15s %-15s", attach_flags_str, prog_name);
154157
if (attach_btf_name)
155158
printf(" %-15s", attach_btf_name);
156159
else if (info.attach_btf_id)
@@ -195,6 +198,32 @@ static int cgroup_has_attached_progs(int cgroup_fd)
195198

196199
return no_prog ? 0 : 1;
197200
}
201+
202+
static int show_effective_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
203+
int level)
204+
{
205+
LIBBPF_OPTS(bpf_prog_query_opts, p);
206+
__u32 prog_ids[1024] = {0};
207+
__u32 iter;
208+
int ret;
209+
210+
p.query_flags = query_flags;
211+
p.prog_cnt = ARRAY_SIZE(prog_ids);
212+
p.prog_ids = prog_ids;
213+
214+
ret = bpf_prog_query_opts(cgroup_fd, type, &p);
215+
if (ret)
216+
return ret;
217+
218+
if (p.prog_cnt == 0)
219+
return 0;
220+
221+
for (iter = 0; iter < p.prog_cnt; iter++)
222+
show_bpf_prog(prog_ids[iter], type, NULL, level);
223+
224+
return 0;
225+
}
226+
198227
static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
199228
int level)
200229
{
@@ -245,6 +274,14 @@ static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
245274
return 0;
246275
}
247276

277+
static int show_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
278+
int level)
279+
{
280+
return query_flags & BPF_F_QUERY_EFFECTIVE ?
281+
show_effective_bpf_progs(cgroup_fd, type, level) :
282+
show_attached_bpf_progs(cgroup_fd, type, level);
283+
}
284+
248285
static int do_show(int argc, char **argv)
249286
{
250287
enum bpf_attach_type type;
@@ -292,6 +329,8 @@ static int do_show(int argc, char **argv)
292329

293330
if (json_output)
294331
jsonw_start_array(json_wtr);
332+
else if (query_flags & BPF_F_QUERY_EFFECTIVE)
333+
printf("%-8s %-15s %-15s\n", "ID", "AttachType", "Name");
295334
else
296335
printf("%-8s %-15s %-15s %-15s\n", "ID", "AttachType",
297336
"AttachFlags", "Name");
@@ -304,7 +343,7 @@ static int do_show(int argc, char **argv)
304343
* If we were able to get the show for at least one
305344
* attach type, let's return 0.
306345
*/
307-
if (show_attached_bpf_progs(cgroup_fd, type, 0) == 0)
346+
if (show_bpf_progs(cgroup_fd, type, 0) == 0)
308347
ret = 0;
309348
}
310349

@@ -362,7 +401,7 @@ static int do_show_tree_fn(const char *fpath, const struct stat *sb,
362401

363402
btf_vmlinux = libbpf_find_kernel_btf();
364403
for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++)
365-
show_attached_bpf_progs(cgroup_fd, type, ftw->level);
404+
show_bpf_progs(cgroup_fd, type, ftw->level);
366405

367406
if (errno == EINVAL)
368407
/* Last attach type does not support query.
@@ -436,6 +475,11 @@ static int do_show_tree(int argc, char **argv)
436475

437476
if (json_output)
438477
jsonw_start_array(json_wtr);
478+
else if (query_flags & BPF_F_QUERY_EFFECTIVE)
479+
printf("%s\n"
480+
"%-8s %-15s %-15s\n",
481+
"CgroupPath",
482+
"ID", "AttachType", "Name");
439483
else
440484
printf("%s\n"
441485
"%-8s %-15s %-15s %-15s\n",

0 commit comments

Comments
 (0)