Sorry for the late reply. On Sat, Nov 21, 2020 at 11:34 AM Martin KaFai Lau <kafai@xxxxxx> wrote: > > On Thu, Nov 19, 2020 at 03:06:11PM +0000, Daniel T. Lee wrote: > [ ... ] > > > static int run_bpf_prog(char *prog, int cg_id) > > [ ... ] > > if (!outFlag) > > - type = BPF_CGROUP_INET_INGRESS; > > - if (bpf_prog_attach(bpfprog_fd, cg1, type, 0)) { > > - printf("ERROR: bpf_prog_attach fails!\n"); > > - log_err("Attaching prog"); > > + bpf_program__set_expected_attach_type(bpf_prog, BPF_CGROUP_INET_INGRESS); > > + > > + link = bpf_program__attach_cgroup(bpf_prog, cg1); > > + if (libbpf_get_error(link)) { > > + fprintf(stderr, "ERROR: bpf_program__attach_cgroup failed\n"); > > + link = NULL; > Again, this is not needed. bpf_link__destroy() can > handle both NULL and error pointer. Please take a look > at the bpf_link__destroy() in libbpf.c > > > + goto err; > > + } > > [ ... ] > > @@ -398,10 +400,10 @@ static int run_bpf_prog(char *prog, int cg_id) > > err: > > rc = 1; > > > > - if (cg1) > > - close(cg1); > > + bpf_link__destroy(link); > > + close(cg1); > > cleanup_cgroup_environment(); > > - > > + bpf_object__close(obj); > The bpf_* cleanup condition still looks wrong. > > I can understand why it does not want to cleanup_cgroup_environment() > on the success case because the sh script may want to run test under this > cgroup. > > However, the bpf_link__destroy(), bpf_object__close(), and > even close(cg1) should be done in both success and error > cases. > > The cg1 test still looks wrong also. The cg1 should > be init to -1 and then test for "if (cg1 == -1)". Thanks for pointing this out. I'll remove NULL initialize and fix this on the next patch. -- Best, Daniel T. Lee