Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf tools: Link all bpf objects onto a list

To allow enumeration of all bpf_objects, keep them in a list (hidden to
caller). bpf_object__for_each_safe() is introduced to do this iteration.
It is safe even user close the object during iteration.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Acked-by: Alexei Starovoitov <ast@plumgrid.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Ahern <dsahern@gmail.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kaixu Xia <xiakaixu@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1435716878-189507-23-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Wang Nan and committed by
Arnaldo Carvalho de Melo
9a208eff aa9b1ac3

+39
+32
tools/lib/bpf/libbpf.c
··· 17 17 #include <asm/unistd.h> 18 18 #include <linux/kernel.h> 19 19 #include <linux/bpf.h> 20 + #include <linux/list.h> 20 21 #include <libelf.h> 21 22 #include <gelf.h> 22 23 ··· 105 104 bpf_program_clear_priv_t clear_priv; 106 105 }; 107 106 107 + static LIST_HEAD(bpf_objects_list); 108 + 108 109 struct bpf_object { 109 110 char license[64]; 110 111 u32 kern_version; ··· 140 137 } *reloc; 141 138 int nr_reloc; 142 139 } efile; 140 + /* 141 + * All loaded bpf_object is linked in a list, which is 142 + * hidden to caller. bpf_objects__<func> handlers deal with 143 + * all objects. 144 + */ 145 + struct list_head list; 143 146 char path[]; 144 147 }; 145 148 #define obj_elf_valid(o) ((o)->efile.elf) ··· 274 265 obj->efile.obj_buf_sz = obj_buf_sz; 275 266 276 267 obj->loaded = false; 268 + 269 + INIT_LIST_HEAD(&obj->list); 270 + list_add(&obj->list, &bpf_objects_list); 277 271 return obj; 278 272 } 279 273 ··· 952 940 } 953 941 zfree(&obj->programs); 954 942 943 + list_del(&obj->list); 955 944 free(obj); 945 + } 946 + 947 + struct bpf_object * 948 + bpf_object__next(struct bpf_object *prev) 949 + { 950 + struct bpf_object *next; 951 + 952 + if (!prev) 953 + next = list_first_entry(&bpf_objects_list, 954 + struct bpf_object, 955 + list); 956 + else 957 + next = list_next_entry(prev, list); 958 + 959 + /* Empty list is noticed here so don't need checking on entry. */ 960 + if (&next->list == &bpf_objects_list) 961 + return NULL; 962 + 963 + return next; 956 964 } 957 965 958 966 struct bpf_program *
+7
tools/lib/bpf/libbpf.h
··· 35 35 int bpf_object__load(struct bpf_object *obj); 36 36 int bpf_object__unload(struct bpf_object *obj); 37 37 38 + struct bpf_object *bpf_object__next(struct bpf_object *prev); 39 + #define bpf_object__for_each_safe(pos, tmp) \ 40 + for ((pos) = bpf_object__next(NULL), \ 41 + (tmp) = bpf_object__next(pos); \ 42 + (pos) != NULL; \ 43 + (pos) = (tmp), (tmp) = bpf_object__next(tmp)) 44 + 38 45 /* Accessors of bpf_program. */ 39 46 struct bpf_program; 40 47 struct bpf_program *bpf_program__next(struct bpf_program *prog,