Commit 2ad97d47 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

bpftool: Generate externs datasec in BPF skeleton

Add support for generation of mmap()-ed read-only view of libbpf-provided
extern variables. As externs are not supposed to be provided by user code
(that's what .data, .bss, and .rodata is for), don't mmap() it initially. Only
after skeleton load is performed, map .extern contents as read-only memory.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20191214014710.3449601-4-andriin@fb.com
parent 166750bc
...@@ -82,6 +82,8 @@ static const char *get_map_ident(const struct bpf_map *map) ...@@ -82,6 +82,8 @@ static const char *get_map_ident(const struct bpf_map *map)
return "rodata"; return "rodata";
else if (str_has_suffix(name, ".bss")) else if (str_has_suffix(name, ".bss"))
return "bss"; return "bss";
else if (str_has_suffix(name, ".extern"))
return "externs"; /* extern is a C keyword */
else else
return NULL; return NULL;
} }
...@@ -109,6 +111,8 @@ static int codegen_datasec_def(struct bpf_object *obj, ...@@ -109,6 +111,8 @@ static int codegen_datasec_def(struct bpf_object *obj,
sec_ident = "bss"; sec_ident = "bss";
else if (strcmp(sec_name, ".rodata") == 0) else if (strcmp(sec_name, ".rodata") == 0)
sec_ident = "rodata"; sec_ident = "rodata";
else if (strcmp(sec_name, ".extern") == 0)
sec_ident = "externs"; /* extern is a C keyword */
else else
return 0; return 0;
......
...@@ -7472,7 +7472,8 @@ int bpf_object__open_skeleton(struct bpf_object_skeleton *s, ...@@ -7472,7 +7472,8 @@ int bpf_object__open_skeleton(struct bpf_object_skeleton *s,
return -ESRCH; return -ESRCH;
} }
if (mmaped) /* externs shouldn't be pre-setup from user code */
if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_EXTERN)
*mmaped = (*map)->mmaped; *mmaped = (*map)->mmaped;
} }
...@@ -7505,7 +7506,6 @@ int bpf_object__load_skeleton(struct bpf_object_skeleton *s) ...@@ -7505,7 +7506,6 @@ int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
size_t mmap_sz = bpf_map_mmap_sz(map); size_t mmap_sz = bpf_map_mmap_sz(map);
int prot, map_fd = bpf_map__fd(map); int prot, map_fd = bpf_map__fd(map);
void **mmaped = s->maps[i].mmaped; void **mmaped = s->maps[i].mmaped;
void *remapped;
if (!mmaped) if (!mmaped)
continue; continue;
...@@ -7530,9 +7530,9 @@ int bpf_object__load_skeleton(struct bpf_object_skeleton *s) ...@@ -7530,9 +7530,9 @@ int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
* as per normal clean up procedure, so we don't need to worry * as per normal clean up procedure, so we don't need to worry
* about it from skeleton's clean up perspective. * about it from skeleton's clean up perspective.
*/ */
remapped = mmap(*mmaped, mmap_sz, prot, MAP_SHARED | MAP_FIXED, *mmaped = mmap(map->mmaped, mmap_sz, prot,
map_fd, 0); MAP_SHARED | MAP_FIXED, map_fd, 0);
if (remapped == MAP_FAILED) { if (*mmaped == MAP_FAILED) {
err = -errno; err = -errno;
*mmaped = NULL; *mmaped = NULL;
pr_warn("failed to re-mmap() map '%s': %d\n", pr_warn("failed to re-mmap() map '%s': %d\n",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment