Commit f6d66139 authored by Stanislav Fomichev's avatar Stanislav Fomichev Committed by Greg Kroah-Hartman

bpf: libbpf: retry map creation without the name

[ Upstream commit 23499442 ]

Since commit 88cda1c9 ("bpf: libbpf: Provide basic API support
to specify BPF obj name"), libbpf unconditionally sets bpf_attr->name
for maps. Pre v4.14 kernels don't know about map names and return an
error about unexpected non-zero data. Retry sys_bpf without a map
name to cover older kernels.

v2 changes:
* check for errno == EINVAL as suggested by Daniel Borkmann
Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent 0857b843
...@@ -77,6 +77,7 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr) ...@@ -77,6 +77,7 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
{ {
__u32 name_len = create_attr->name ? strlen(create_attr->name) : 0; __u32 name_len = create_attr->name ? strlen(create_attr->name) : 0;
union bpf_attr attr; union bpf_attr attr;
int ret;
memset(&attr, '\0', sizeof(attr)); memset(&attr, '\0', sizeof(attr));
...@@ -94,7 +95,15 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr) ...@@ -94,7 +95,15 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
attr.map_ifindex = create_attr->map_ifindex; attr.map_ifindex = create_attr->map_ifindex;
attr.inner_map_fd = create_attr->inner_map_fd; attr.inner_map_fd = create_attr->inner_map_fd;
return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); ret = sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
if (ret < 0 && errno == EINVAL && create_attr->name) {
/* Retry the same syscall, but without the name.
* Pre v4.14 kernels don't support map names.
*/
memset(attr.map_name, 0, sizeof(attr.map_name));
return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
}
return ret;
} }
int bpf_create_map_node(enum bpf_map_type map_type, const char *name, int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
......
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