Commit 813847a3 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Daniel Borkmann

libbpf: Streamline bpf_attr and perf_event_attr initialization

Make sure that entire libbpf code base is initializing bpf_attr and
perf_event_attr with memset(0). Also for bpf_attr make sure we
clear and pass to kernel only relevant parts of bpf_attr. bpf_attr is
a huge union of independent sub-command attributes, so there is no need
to clear and pass entire union bpf_attr, which over time grows quite
a lot and for most commands this growth is completely irrelevant.

Few cases where we were relying on compiler initialization of BPF UAPI
structs (like bpf_prog_info, bpf_map_info, etc) with `= {};` were
switched to memset(0) pattern for future-proofing.
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarHao Luo <haoluo@google.com>
Link: https://lore.kernel.org/bpf/20220816001929.369487-3-andrii@kernel.org
parent d4e6d684
...@@ -105,7 +105,7 @@ int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts) ...@@ -105,7 +105,7 @@ int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts)
*/ */
int probe_memcg_account(void) int probe_memcg_account(void)
{ {
const size_t prog_load_attr_sz = offsetofend(union bpf_attr, attach_btf_obj_fd); const size_t attr_sz = offsetofend(union bpf_attr, attach_btf_obj_fd);
struct bpf_insn insns[] = { struct bpf_insn insns[] = {
BPF_EMIT_CALL(BPF_FUNC_ktime_get_coarse_ns), BPF_EMIT_CALL(BPF_FUNC_ktime_get_coarse_ns),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
...@@ -115,13 +115,13 @@ int probe_memcg_account(void) ...@@ -115,13 +115,13 @@ int probe_memcg_account(void)
int prog_fd; int prog_fd;
/* attempt loading freplace trying to use custom BTF */ /* attempt loading freplace trying to use custom BTF */
memset(&attr, 0, prog_load_attr_sz); memset(&attr, 0, attr_sz);
attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER; attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
attr.insns = ptr_to_u64(insns); attr.insns = ptr_to_u64(insns);
attr.insn_cnt = insn_cnt; attr.insn_cnt = insn_cnt;
attr.license = ptr_to_u64("GPL"); attr.license = ptr_to_u64("GPL");
prog_fd = sys_bpf_fd(BPF_PROG_LOAD, &attr, prog_load_attr_sz); prog_fd = sys_bpf_fd(BPF_PROG_LOAD, &attr, attr_sz);
if (prog_fd >= 0) { if (prog_fd >= 0) {
close(prog_fd); close(prog_fd);
return 1; return 1;
...@@ -232,6 +232,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, ...@@ -232,6 +232,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
const struct bpf_insn *insns, size_t insn_cnt, const struct bpf_insn *insns, size_t insn_cnt,
const struct bpf_prog_load_opts *opts) const struct bpf_prog_load_opts *opts)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, fd_array);
void *finfo = NULL, *linfo = NULL; void *finfo = NULL, *linfo = NULL;
const char *func_info, *line_info; const char *func_info, *line_info;
__u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd; __u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;
...@@ -251,7 +252,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, ...@@ -251,7 +252,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
if (attempts == 0) if (attempts == 0)
attempts = PROG_LOAD_ATTEMPTS; attempts = PROG_LOAD_ATTEMPTS;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.prog_type = prog_type; attr.prog_type = prog_type;
attr.expected_attach_type = OPTS_GET(opts, expected_attach_type, 0); attr.expected_attach_type = OPTS_GET(opts, expected_attach_type, 0);
...@@ -314,7 +315,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, ...@@ -314,7 +315,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
attr.log_level = log_level; attr.log_level = log_level;
} }
fd = sys_bpf_prog_load(&attr, sizeof(attr), attempts); fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
if (fd >= 0) if (fd >= 0)
return fd; return fd;
...@@ -354,7 +355,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, ...@@ -354,7 +355,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
break; break;
} }
fd = sys_bpf_prog_load(&attr, sizeof(attr), attempts); fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
if (fd >= 0) if (fd >= 0)
goto done; goto done;
} }
...@@ -368,7 +369,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, ...@@ -368,7 +369,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
attr.log_size = log_size; attr.log_size = log_size;
attr.log_level = 1; attr.log_level = 1;
fd = sys_bpf_prog_load(&attr, sizeof(attr), attempts); fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
} }
done: done:
/* free() doesn't affect errno, so we don't need to restore it */ /* free() doesn't affect errno, so we don't need to restore it */
...@@ -380,127 +381,136 @@ int bpf_prog_load(enum bpf_prog_type prog_type, ...@@ -380,127 +381,136 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
int bpf_map_update_elem(int fd, const void *key, const void *value, int bpf_map_update_elem(int fd, const void *key, const void *value,
__u64 flags) __u64 flags)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, flags);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.map_fd = fd; attr.map_fd = fd;
attr.key = ptr_to_u64(key); attr.key = ptr_to_u64(key);
attr.value = ptr_to_u64(value); attr.value = ptr_to_u64(value);
attr.flags = flags; attr.flags = flags;
ret = sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)); ret = sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
int bpf_map_lookup_elem(int fd, const void *key, void *value) int bpf_map_lookup_elem(int fd, const void *key, void *value)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, flags);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.map_fd = fd; attr.map_fd = fd;
attr.key = ptr_to_u64(key); attr.key = ptr_to_u64(key);
attr.value = ptr_to_u64(value); attr.value = ptr_to_u64(value);
ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)); ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags) int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, flags);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.map_fd = fd; attr.map_fd = fd;
attr.key = ptr_to_u64(key); attr.key = ptr_to_u64(key);
attr.value = ptr_to_u64(value); attr.value = ptr_to_u64(value);
attr.flags = flags; attr.flags = flags;
ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)); ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value) int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, flags);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.map_fd = fd; attr.map_fd = fd;
attr.key = ptr_to_u64(key); attr.key = ptr_to_u64(key);
attr.value = ptr_to_u64(value); attr.value = ptr_to_u64(value);
ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, sizeof(attr)); ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key, void *value, __u64 flags) int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key, void *value, __u64 flags)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, flags);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.map_fd = fd; attr.map_fd = fd;
attr.key = ptr_to_u64(key); attr.key = ptr_to_u64(key);
attr.value = ptr_to_u64(value); attr.value = ptr_to_u64(value);
attr.flags = flags; attr.flags = flags;
ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, sizeof(attr)); ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
int bpf_map_delete_elem(int fd, const void *key) int bpf_map_delete_elem(int fd, const void *key)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, flags);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.map_fd = fd; attr.map_fd = fd;
attr.key = ptr_to_u64(key); attr.key = ptr_to_u64(key);
ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr)); ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags) int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, flags);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.map_fd = fd; attr.map_fd = fd;
attr.key = ptr_to_u64(key); attr.key = ptr_to_u64(key);
attr.flags = flags; attr.flags = flags;
ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr)); ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
int bpf_map_get_next_key(int fd, const void *key, void *next_key) int bpf_map_get_next_key(int fd, const void *key, void *next_key)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, next_key);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.map_fd = fd; attr.map_fd = fd;
attr.key = ptr_to_u64(key); attr.key = ptr_to_u64(key);
attr.next_key = ptr_to_u64(next_key); attr.next_key = ptr_to_u64(next_key);
ret = sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr)); ret = sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
int bpf_map_freeze(int fd) int bpf_map_freeze(int fd)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, map_fd);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.map_fd = fd; attr.map_fd = fd;
ret = sys_bpf(BPF_MAP_FREEZE, &attr, sizeof(attr)); ret = sys_bpf(BPF_MAP_FREEZE, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
...@@ -509,13 +519,14 @@ static int bpf_map_batch_common(int cmd, int fd, void *in_batch, ...@@ -509,13 +519,14 @@ static int bpf_map_batch_common(int cmd, int fd, void *in_batch,
__u32 *count, __u32 *count,
const struct bpf_map_batch_opts *opts) const struct bpf_map_batch_opts *opts)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, batch);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
if (!OPTS_VALID(opts, bpf_map_batch_opts)) if (!OPTS_VALID(opts, bpf_map_batch_opts))
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.batch.map_fd = fd; attr.batch.map_fd = fd;
attr.batch.in_batch = ptr_to_u64(in_batch); attr.batch.in_batch = ptr_to_u64(in_batch);
attr.batch.out_batch = ptr_to_u64(out_batch); attr.batch.out_batch = ptr_to_u64(out_batch);
...@@ -525,7 +536,7 @@ static int bpf_map_batch_common(int cmd, int fd, void *in_batch, ...@@ -525,7 +536,7 @@ static int bpf_map_batch_common(int cmd, int fd, void *in_batch,
attr.batch.elem_flags = OPTS_GET(opts, elem_flags, 0); attr.batch.elem_flags = OPTS_GET(opts, elem_flags, 0);
attr.batch.flags = OPTS_GET(opts, flags, 0); attr.batch.flags = OPTS_GET(opts, flags, 0);
ret = sys_bpf(cmd, &attr, sizeof(attr)); ret = sys_bpf(cmd, &attr, attr_sz);
*count = attr.batch.count; *count = attr.batch.count;
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
...@@ -564,14 +575,15 @@ int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *co ...@@ -564,14 +575,15 @@ int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *co
int bpf_obj_pin(int fd, const char *pathname) int bpf_obj_pin(int fd, const char *pathname)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, file_flags);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.pathname = ptr_to_u64((void *)pathname); attr.pathname = ptr_to_u64((void *)pathname);
attr.bpf_fd = fd; attr.bpf_fd = fd;
ret = sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr)); ret = sys_bpf(BPF_OBJ_PIN, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
...@@ -582,17 +594,18 @@ int bpf_obj_get(const char *pathname) ...@@ -582,17 +594,18 @@ int bpf_obj_get(const char *pathname)
int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts) int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, file_flags);
union bpf_attr attr; union bpf_attr attr;
int fd; int fd;
if (!OPTS_VALID(opts, bpf_obj_get_opts)) if (!OPTS_VALID(opts, bpf_obj_get_opts))
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.pathname = ptr_to_u64((void *)pathname); attr.pathname = ptr_to_u64((void *)pathname);
attr.file_flags = OPTS_GET(opts, file_flags, 0); attr.file_flags = OPTS_GET(opts, file_flags, 0);
fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr)); fd = sys_bpf_fd(BPF_OBJ_GET, &attr, attr_sz);
return libbpf_err_errno(fd); return libbpf_err_errno(fd);
} }
...@@ -610,20 +623,21 @@ int bpf_prog_attach_opts(int prog_fd, int target_fd, ...@@ -610,20 +623,21 @@ int bpf_prog_attach_opts(int prog_fd, int target_fd,
enum bpf_attach_type type, enum bpf_attach_type type,
const struct bpf_prog_attach_opts *opts) const struct bpf_prog_attach_opts *opts)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, replace_bpf_fd);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
if (!OPTS_VALID(opts, bpf_prog_attach_opts)) if (!OPTS_VALID(opts, bpf_prog_attach_opts))
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.target_fd = target_fd; attr.target_fd = target_fd;
attr.attach_bpf_fd = prog_fd; attr.attach_bpf_fd = prog_fd;
attr.attach_type = type; attr.attach_type = type;
attr.attach_flags = OPTS_GET(opts, flags, 0); attr.attach_flags = OPTS_GET(opts, flags, 0);
attr.replace_bpf_fd = OPTS_GET(opts, replace_prog_fd, 0); attr.replace_bpf_fd = OPTS_GET(opts, replace_prog_fd, 0);
ret = sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr)); ret = sys_bpf(BPF_PROG_ATTACH, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
...@@ -634,28 +648,30 @@ int bpf_prog_attach_xattr(int prog_fd, int target_fd, ...@@ -634,28 +648,30 @@ int bpf_prog_attach_xattr(int prog_fd, int target_fd,
int bpf_prog_detach(int target_fd, enum bpf_attach_type type) int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, replace_bpf_fd);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.target_fd = target_fd; attr.target_fd = target_fd;
attr.attach_type = type; attr.attach_type = type;
ret = sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr)); ret = sys_bpf(BPF_PROG_DETACH, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type) int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, replace_bpf_fd);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.target_fd = target_fd; attr.target_fd = target_fd;
attr.attach_bpf_fd = prog_fd; attr.attach_bpf_fd = prog_fd;
attr.attach_type = type; attr.attach_type = type;
ret = sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr)); ret = sys_bpf(BPF_PROG_DETACH, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
...@@ -663,6 +679,7 @@ int bpf_link_create(int prog_fd, int target_fd, ...@@ -663,6 +679,7 @@ int bpf_link_create(int prog_fd, int target_fd,
enum bpf_attach_type attach_type, enum bpf_attach_type attach_type,
const struct bpf_link_create_opts *opts) const struct bpf_link_create_opts *opts)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, link_create);
__u32 target_btf_id, iter_info_len; __u32 target_btf_id, iter_info_len;
union bpf_attr attr; union bpf_attr attr;
int fd, err; int fd, err;
...@@ -681,7 +698,7 @@ int bpf_link_create(int prog_fd, int target_fd, ...@@ -681,7 +698,7 @@ int bpf_link_create(int prog_fd, int target_fd,
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
} }
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.link_create.prog_fd = prog_fd; attr.link_create.prog_fd = prog_fd;
attr.link_create.target_fd = target_fd; attr.link_create.target_fd = target_fd;
attr.link_create.attach_type = attach_type; attr.link_create.attach_type = attach_type;
...@@ -725,7 +742,7 @@ int bpf_link_create(int prog_fd, int target_fd, ...@@ -725,7 +742,7 @@ int bpf_link_create(int prog_fd, int target_fd,
break; break;
} }
proceed: proceed:
fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, sizeof(attr)); fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
if (fd >= 0) if (fd >= 0)
return fd; return fd;
/* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry /* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry
...@@ -761,44 +778,47 @@ int bpf_link_create(int prog_fd, int target_fd, ...@@ -761,44 +778,47 @@ int bpf_link_create(int prog_fd, int target_fd,
int bpf_link_detach(int link_fd) int bpf_link_detach(int link_fd)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, link_detach);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.link_detach.link_fd = link_fd; attr.link_detach.link_fd = link_fd;
ret = sys_bpf(BPF_LINK_DETACH, &attr, sizeof(attr)); ret = sys_bpf(BPF_LINK_DETACH, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
int bpf_link_update(int link_fd, int new_prog_fd, int bpf_link_update(int link_fd, int new_prog_fd,
const struct bpf_link_update_opts *opts) const struct bpf_link_update_opts *opts)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, link_update);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
if (!OPTS_VALID(opts, bpf_link_update_opts)) if (!OPTS_VALID(opts, bpf_link_update_opts))
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.link_update.link_fd = link_fd; attr.link_update.link_fd = link_fd;
attr.link_update.new_prog_fd = new_prog_fd; attr.link_update.new_prog_fd = new_prog_fd;
attr.link_update.flags = OPTS_GET(opts, flags, 0); attr.link_update.flags = OPTS_GET(opts, flags, 0);
attr.link_update.old_prog_fd = OPTS_GET(opts, old_prog_fd, 0); attr.link_update.old_prog_fd = OPTS_GET(opts, old_prog_fd, 0);
ret = sys_bpf(BPF_LINK_UPDATE, &attr, sizeof(attr)); ret = sys_bpf(BPF_LINK_UPDATE, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
int bpf_iter_create(int link_fd) int bpf_iter_create(int link_fd)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, iter_create);
union bpf_attr attr; union bpf_attr attr;
int fd; int fd;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.iter_create.link_fd = link_fd; attr.iter_create.link_fd = link_fd;
fd = sys_bpf_fd(BPF_ITER_CREATE, &attr, sizeof(attr)); fd = sys_bpf_fd(BPF_ITER_CREATE, &attr, attr_sz);
return libbpf_err_errno(fd); return libbpf_err_errno(fd);
} }
...@@ -806,13 +826,14 @@ int bpf_prog_query_opts(int target_fd, ...@@ -806,13 +826,14 @@ int bpf_prog_query_opts(int target_fd,
enum bpf_attach_type type, enum bpf_attach_type type,
struct bpf_prog_query_opts *opts) struct bpf_prog_query_opts *opts)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, query);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
if (!OPTS_VALID(opts, bpf_prog_query_opts)) if (!OPTS_VALID(opts, bpf_prog_query_opts))
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.query.target_fd = target_fd; attr.query.target_fd = target_fd;
attr.query.attach_type = type; attr.query.attach_type = type;
...@@ -821,7 +842,7 @@ int bpf_prog_query_opts(int target_fd, ...@@ -821,7 +842,7 @@ int bpf_prog_query_opts(int target_fd,
attr.query.prog_ids = ptr_to_u64(OPTS_GET(opts, prog_ids, NULL)); attr.query.prog_ids = ptr_to_u64(OPTS_GET(opts, prog_ids, NULL));
attr.query.prog_attach_flags = ptr_to_u64(OPTS_GET(opts, prog_attach_flags, NULL)); attr.query.prog_attach_flags = ptr_to_u64(OPTS_GET(opts, prog_attach_flags, NULL));
ret = sys_bpf(BPF_PROG_QUERY, &attr, sizeof(attr)); ret = sys_bpf(BPF_PROG_QUERY, &attr, attr_sz);
OPTS_SET(opts, attach_flags, attr.query.attach_flags); OPTS_SET(opts, attach_flags, attr.query.attach_flags);
OPTS_SET(opts, prog_cnt, attr.query.prog_cnt); OPTS_SET(opts, prog_cnt, attr.query.prog_cnt);
...@@ -850,13 +871,14 @@ int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags, ...@@ -850,13 +871,14 @@ int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts) int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, test);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
if (!OPTS_VALID(opts, bpf_test_run_opts)) if (!OPTS_VALID(opts, bpf_test_run_opts))
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.test.prog_fd = prog_fd; attr.test.prog_fd = prog_fd;
attr.test.batch_size = OPTS_GET(opts, batch_size, 0); attr.test.batch_size = OPTS_GET(opts, batch_size, 0);
attr.test.cpu = OPTS_GET(opts, cpu, 0); attr.test.cpu = OPTS_GET(opts, cpu, 0);
...@@ -872,7 +894,7 @@ int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts) ...@@ -872,7 +894,7 @@ int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts)
attr.test.data_in = ptr_to_u64(OPTS_GET(opts, data_in, NULL)); attr.test.data_in = ptr_to_u64(OPTS_GET(opts, data_in, NULL));
attr.test.data_out = ptr_to_u64(OPTS_GET(opts, data_out, NULL)); attr.test.data_out = ptr_to_u64(OPTS_GET(opts, data_out, NULL));
ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr)); ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, attr_sz);
OPTS_SET(opts, data_size_out, attr.test.data_size_out); OPTS_SET(opts, data_size_out, attr.test.data_size_out);
OPTS_SET(opts, ctx_size_out, attr.test.ctx_size_out); OPTS_SET(opts, ctx_size_out, attr.test.ctx_size_out);
...@@ -884,13 +906,14 @@ int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts) ...@@ -884,13 +906,14 @@ int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts)
static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd) static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
union bpf_attr attr; union bpf_attr attr;
int err; int err;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.start_id = start_id; attr.start_id = start_id;
err = sys_bpf(cmd, &attr, sizeof(attr)); err = sys_bpf(cmd, &attr, attr_sz);
if (!err) if (!err)
*next_id = attr.next_id; *next_id = attr.next_id;
...@@ -919,80 +942,84 @@ int bpf_link_get_next_id(__u32 start_id, __u32 *next_id) ...@@ -919,80 +942,84 @@ int bpf_link_get_next_id(__u32 start_id, __u32 *next_id)
int bpf_prog_get_fd_by_id(__u32 id) int bpf_prog_get_fd_by_id(__u32 id)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
union bpf_attr attr; union bpf_attr attr;
int fd; int fd;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.prog_id = id; attr.prog_id = id;
fd = sys_bpf_fd(BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr)); fd = sys_bpf_fd(BPF_PROG_GET_FD_BY_ID, &attr, attr_sz);
return libbpf_err_errno(fd); return libbpf_err_errno(fd);
} }
int bpf_map_get_fd_by_id(__u32 id) int bpf_map_get_fd_by_id(__u32 id)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
union bpf_attr attr; union bpf_attr attr;
int fd; int fd;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.map_id = id; attr.map_id = id;
fd = sys_bpf_fd(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr)); fd = sys_bpf_fd(BPF_MAP_GET_FD_BY_ID, &attr, attr_sz);
return libbpf_err_errno(fd); return libbpf_err_errno(fd);
} }
int bpf_btf_get_fd_by_id(__u32 id) int bpf_btf_get_fd_by_id(__u32 id)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
union bpf_attr attr; union bpf_attr attr;
int fd; int fd;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.btf_id = id; attr.btf_id = id;
fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, sizeof(attr)); fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, attr_sz);
return libbpf_err_errno(fd); return libbpf_err_errno(fd);
} }
int bpf_link_get_fd_by_id(__u32 id) int bpf_link_get_fd_by_id(__u32 id)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
union bpf_attr attr; union bpf_attr attr;
int fd; int fd;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.link_id = id; attr.link_id = id;
fd = sys_bpf_fd(BPF_LINK_GET_FD_BY_ID, &attr, sizeof(attr)); fd = sys_bpf_fd(BPF_LINK_GET_FD_BY_ID, &attr, attr_sz);
return libbpf_err_errno(fd); return libbpf_err_errno(fd);
} }
int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len) int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, info);
union bpf_attr attr; union bpf_attr attr;
int err; int err;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.info.bpf_fd = bpf_fd; attr.info.bpf_fd = bpf_fd;
attr.info.info_len = *info_len; attr.info.info_len = *info_len;
attr.info.info = ptr_to_u64(info); attr.info.info = ptr_to_u64(info);
err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr)); err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, attr_sz);
if (!err) if (!err)
*info_len = attr.info.info_len; *info_len = attr.info.info_len;
return libbpf_err_errno(err); return libbpf_err_errno(err);
} }
int bpf_raw_tracepoint_open(const char *name, int prog_fd) int bpf_raw_tracepoint_open(const char *name, int prog_fd)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, raw_tracepoint);
union bpf_attr attr; union bpf_attr attr;
int fd; int fd;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.raw_tracepoint.name = ptr_to_u64(name); attr.raw_tracepoint.name = ptr_to_u64(name);
attr.raw_tracepoint.prog_fd = prog_fd; attr.raw_tracepoint.prog_fd = prog_fd;
fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr)); fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, attr_sz);
return libbpf_err_errno(fd); return libbpf_err_errno(fd);
} }
...@@ -1048,16 +1075,18 @@ int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len, ...@@ -1048,16 +1075,18 @@ int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
__u32 *prog_id, __u32 *fd_type, __u64 *probe_offset, __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
__u64 *probe_addr) __u64 *probe_addr)
{ {
union bpf_attr attr = {}; const size_t attr_sz = offsetofend(union bpf_attr, task_fd_query);
union bpf_attr attr;
int err; int err;
memset(&attr, 0, attr_sz);
attr.task_fd_query.pid = pid; attr.task_fd_query.pid = pid;
attr.task_fd_query.fd = fd; attr.task_fd_query.fd = fd;
attr.task_fd_query.flags = flags; attr.task_fd_query.flags = flags;
attr.task_fd_query.buf = ptr_to_u64(buf); attr.task_fd_query.buf = ptr_to_u64(buf);
attr.task_fd_query.buf_len = *buf_len; attr.task_fd_query.buf_len = *buf_len;
err = sys_bpf(BPF_TASK_FD_QUERY, &attr, sizeof(attr)); err = sys_bpf(BPF_TASK_FD_QUERY, &attr, attr_sz);
*buf_len = attr.task_fd_query.buf_len; *buf_len = attr.task_fd_query.buf_len;
*prog_id = attr.task_fd_query.prog_id; *prog_id = attr.task_fd_query.prog_id;
...@@ -1070,30 +1099,32 @@ int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len, ...@@ -1070,30 +1099,32 @@ int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
int bpf_enable_stats(enum bpf_stats_type type) int bpf_enable_stats(enum bpf_stats_type type)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, enable_stats);
union bpf_attr attr; union bpf_attr attr;
int fd; int fd;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.enable_stats.type = type; attr.enable_stats.type = type;
fd = sys_bpf_fd(BPF_ENABLE_STATS, &attr, sizeof(attr)); fd = sys_bpf_fd(BPF_ENABLE_STATS, &attr, attr_sz);
return libbpf_err_errno(fd); return libbpf_err_errno(fd);
} }
int bpf_prog_bind_map(int prog_fd, int map_fd, int bpf_prog_bind_map(int prog_fd, int map_fd,
const struct bpf_prog_bind_opts *opts) const struct bpf_prog_bind_opts *opts)
{ {
const size_t attr_sz = offsetofend(union bpf_attr, prog_bind_map);
union bpf_attr attr; union bpf_attr attr;
int ret; int ret;
if (!OPTS_VALID(opts, bpf_prog_bind_opts)) if (!OPTS_VALID(opts, bpf_prog_bind_opts))
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.prog_bind_map.prog_fd = prog_fd; attr.prog_bind_map.prog_fd = prog_fd;
attr.prog_bind_map.map_fd = map_fd; attr.prog_bind_map.map_fd = map_fd;
attr.prog_bind_map.flags = OPTS_GET(opts, flags, 0); attr.prog_bind_map.flags = OPTS_GET(opts, flags, 0);
ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, sizeof(attr)); ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, attr_sz);
return libbpf_err_errno(ret); return libbpf_err_errno(ret);
} }
...@@ -4287,11 +4287,12 @@ int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate) ...@@ -4287,11 +4287,12 @@ int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate)
int bpf_map__reuse_fd(struct bpf_map *map, int fd) int bpf_map__reuse_fd(struct bpf_map *map, int fd)
{ {
struct bpf_map_info info = {}; struct bpf_map_info info;
__u32 len = sizeof(info), name_len; __u32 len = sizeof(info), name_len;
int new_fd, err; int new_fd, err;
char *new_name; char *new_name;
memset(&info, 0, len);
err = bpf_obj_get_info_by_fd(fd, &info, &len); err = bpf_obj_get_info_by_fd(fd, &info, &len);
if (err && errno == EINVAL) if (err && errno == EINVAL)
err = bpf_get_map_info_from_fdinfo(fd, &info); err = bpf_get_map_info_from_fdinfo(fd, &info);
...@@ -4833,13 +4834,12 @@ bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id) ...@@ -4833,13 +4834,12 @@ bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)
static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd) static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
{ {
struct bpf_map_info map_info = {}; struct bpf_map_info map_info;
char msg[STRERR_BUFSIZE]; char msg[STRERR_BUFSIZE];
__u32 map_info_len; __u32 map_info_len = sizeof(map_info);
int err; int err;
map_info_len = sizeof(map_info); memset(&map_info, 0, map_info_len);
err = bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len); err = bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len);
if (err && errno == EINVAL) if (err && errno == EINVAL)
err = bpf_get_map_info_from_fdinfo(map_fd, &map_info); err = bpf_get_map_info_from_fdinfo(map_fd, &map_info);
...@@ -9007,11 +9007,12 @@ int libbpf_find_vmlinux_btf_id(const char *name, ...@@ -9007,11 +9007,12 @@ int libbpf_find_vmlinux_btf_id(const char *name,
static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd) static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
{ {
struct bpf_prog_info info = {}; struct bpf_prog_info info;
__u32 info_len = sizeof(info); __u32 info_len = sizeof(info);
struct btf *btf; struct btf *btf;
int err; int err;
memset(&info, 0, info_len);
err = bpf_obj_get_info_by_fd(attach_prog_fd, &info, &info_len); err = bpf_obj_get_info_by_fd(attach_prog_fd, &info, &info_len);
if (err) { if (err) {
pr_warn("failed bpf_obj_get_info_by_fd for FD %d: %d\n", pr_warn("failed bpf_obj_get_info_by_fd for FD %d: %d\n",
...@@ -9839,13 +9840,16 @@ static int determine_uprobe_retprobe_bit(void) ...@@ -9839,13 +9840,16 @@ static int determine_uprobe_retprobe_bit(void)
static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
uint64_t offset, int pid, size_t ref_ctr_off) uint64_t offset, int pid, size_t ref_ctr_off)
{ {
struct perf_event_attr attr = {}; const size_t attr_sz = sizeof(struct perf_event_attr);
struct perf_event_attr attr;
char errmsg[STRERR_BUFSIZE]; char errmsg[STRERR_BUFSIZE];
int type, pfd; int type, pfd;
if (ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS)) if (ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
return -EINVAL; return -EINVAL;
memset(&attr, 0, attr_sz);
type = uprobe ? determine_uprobe_perf_type() type = uprobe ? determine_uprobe_perf_type()
: determine_kprobe_perf_type(); : determine_kprobe_perf_type();
if (type < 0) { if (type < 0) {
...@@ -9866,7 +9870,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, ...@@ -9866,7 +9870,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
} }
attr.config |= 1 << bit; attr.config |= 1 << bit;
} }
attr.size = sizeof(attr); attr.size = attr_sz;
attr.type = type; attr.type = type;
attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT; attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */ attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
...@@ -9965,7 +9969,8 @@ static int determine_kprobe_perf_type_legacy(const char *probe_name, bool retpro ...@@ -9965,7 +9969,8 @@ static int determine_kprobe_perf_type_legacy(const char *probe_name, bool retpro
static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe,
const char *kfunc_name, size_t offset, int pid) const char *kfunc_name, size_t offset, int pid)
{ {
struct perf_event_attr attr = {}; const size_t attr_sz = sizeof(struct perf_event_attr);
struct perf_event_attr attr;
char errmsg[STRERR_BUFSIZE]; char errmsg[STRERR_BUFSIZE];
int type, pfd, err; int type, pfd, err;
...@@ -9984,7 +9989,9 @@ static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, ...@@ -9984,7 +9989,9 @@ static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe,
libbpf_strerror_r(err, errmsg, sizeof(errmsg))); libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
goto err_clean_legacy; goto err_clean_legacy;
} }
attr.size = sizeof(attr);
memset(&attr, 0, attr_sz);
attr.size = attr_sz;
attr.config = type; attr.config = type;
attr.type = PERF_TYPE_TRACEPOINT; attr.type = PERF_TYPE_TRACEPOINT;
...@@ -10441,6 +10448,7 @@ static int determine_uprobe_perf_type_legacy(const char *probe_name, bool retpro ...@@ -10441,6 +10448,7 @@ static int determine_uprobe_perf_type_legacy(const char *probe_name, bool retpro
static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe,
const char *binary_path, size_t offset, int pid) const char *binary_path, size_t offset, int pid)
{ {
const size_t attr_sz = sizeof(struct perf_event_attr);
struct perf_event_attr attr; struct perf_event_attr attr;
int type, pfd, err; int type, pfd, err;
...@@ -10458,8 +10466,8 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, ...@@ -10458,8 +10466,8 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe,
goto err_clean_legacy; goto err_clean_legacy;
} }
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, attr_sz);
attr.size = sizeof(attr); attr.size = attr_sz;
attr.config = type; attr.config = type;
attr.type = PERF_TYPE_TRACEPOINT; attr.type = PERF_TYPE_TRACEPOINT;
...@@ -10998,7 +11006,8 @@ static int determine_tracepoint_id(const char *tp_category, ...@@ -10998,7 +11006,8 @@ static int determine_tracepoint_id(const char *tp_category,
static int perf_event_open_tracepoint(const char *tp_category, static int perf_event_open_tracepoint(const char *tp_category,
const char *tp_name) const char *tp_name)
{ {
struct perf_event_attr attr = {}; const size_t attr_sz = sizeof(struct perf_event_attr);
struct perf_event_attr attr;
char errmsg[STRERR_BUFSIZE]; char errmsg[STRERR_BUFSIZE];
int tp_id, pfd, err; int tp_id, pfd, err;
...@@ -11010,8 +11019,9 @@ static int perf_event_open_tracepoint(const char *tp_category, ...@@ -11010,8 +11019,9 @@ static int perf_event_open_tracepoint(const char *tp_category,
return tp_id; return tp_id;
} }
memset(&attr, 0, attr_sz);
attr.type = PERF_TYPE_TRACEPOINT; attr.type = PERF_TYPE_TRACEPOINT;
attr.size = sizeof(attr); attr.size = attr_sz;
attr.config = tp_id; attr.config = tp_id;
pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */, pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */,
...@@ -11631,12 +11641,15 @@ struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt, ...@@ -11631,12 +11641,15 @@ struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
void *ctx, void *ctx,
const struct perf_buffer_opts *opts) const struct perf_buffer_opts *opts)
{ {
const size_t attr_sz = sizeof(struct perf_event_attr);
struct perf_buffer_params p = {}; struct perf_buffer_params p = {};
struct perf_event_attr attr = {}; struct perf_event_attr attr;
if (!OPTS_VALID(opts, perf_buffer_opts)) if (!OPTS_VALID(opts, perf_buffer_opts))
return libbpf_err_ptr(-EINVAL); return libbpf_err_ptr(-EINVAL);
memset(&attr, 0, attr_sz);
attr.size = attr_sz;
attr.config = PERF_COUNT_SW_BPF_OUTPUT; attr.config = PERF_COUNT_SW_BPF_OUTPUT;
attr.type = PERF_TYPE_SOFTWARE; attr.type = PERF_TYPE_SOFTWARE;
attr.sample_type = PERF_SAMPLE_RAW; attr.sample_type = PERF_SAMPLE_RAW;
......
...@@ -587,11 +587,12 @@ static int get_tc_info(struct nlmsghdr *nh, libbpf_dump_nlmsg_t fn, ...@@ -587,11 +587,12 @@ static int get_tc_info(struct nlmsghdr *nh, libbpf_dump_nlmsg_t fn,
static int tc_add_fd_and_name(struct libbpf_nla_req *req, int fd) static int tc_add_fd_and_name(struct libbpf_nla_req *req, int fd)
{ {
struct bpf_prog_info info = {}; struct bpf_prog_info info;
__u32 info_len = sizeof(info); __u32 info_len = sizeof(info);
char name[256]; char name[256];
int len, ret; int len, ret;
memset(&info, 0, info_len);
ret = bpf_obj_get_info_by_fd(fd, &info, &info_len); ret = bpf_obj_get_info_by_fd(fd, &info, &info_len);
if (ret < 0) if (ret < 0)
return ret; return ret;
......
...@@ -285,6 +285,8 @@ static inline int skel_link_create(int prog_fd, int target_fd, ...@@ -285,6 +285,8 @@ static inline int skel_link_create(int prog_fd, int target_fd,
static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts) static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)
{ {
const size_t prog_load_attr_sz = offsetofend(union bpf_attr, fd_array);
const size_t test_run_attr_sz = offsetofend(union bpf_attr, test);
int map_fd = -1, prog_fd = -1, key = 0, err; int map_fd = -1, prog_fd = -1, key = 0, err;
union bpf_attr attr; union bpf_attr attr;
...@@ -302,7 +304,7 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts) ...@@ -302,7 +304,7 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)
goto out; goto out;
} }
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, prog_load_attr_sz);
attr.prog_type = BPF_PROG_TYPE_SYSCALL; attr.prog_type = BPF_PROG_TYPE_SYSCALL;
attr.insns = (long) opts->insns; attr.insns = (long) opts->insns;
attr.insn_cnt = opts->insns_sz / sizeof(struct bpf_insn); attr.insn_cnt = opts->insns_sz / sizeof(struct bpf_insn);
...@@ -313,18 +315,18 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts) ...@@ -313,18 +315,18 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)
attr.log_size = opts->ctx->log_size; attr.log_size = opts->ctx->log_size;
attr.log_buf = opts->ctx->log_buf; attr.log_buf = opts->ctx->log_buf;
attr.prog_flags = BPF_F_SLEEPABLE; attr.prog_flags = BPF_F_SLEEPABLE;
err = prog_fd = skel_sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); err = prog_fd = skel_sys_bpf(BPF_PROG_LOAD, &attr, prog_load_attr_sz);
if (prog_fd < 0) { if (prog_fd < 0) {
opts->errstr = "failed to load loader prog"; opts->errstr = "failed to load loader prog";
set_err; set_err;
goto out; goto out;
} }
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, test_run_attr_sz);
attr.test.prog_fd = prog_fd; attr.test.prog_fd = prog_fd;
attr.test.ctx_in = (long) opts->ctx; attr.test.ctx_in = (long) opts->ctx;
attr.test.ctx_size_in = opts->ctx->sz; attr.test.ctx_size_in = opts->ctx->sz;
err = skel_sys_bpf(BPF_PROG_RUN, &attr, sizeof(attr)); err = skel_sys_bpf(BPF_PROG_RUN, &attr, test_run_attr_sz);
if (err < 0 || (int)attr.test.retval < 0) { if (err < 0 || (int)attr.test.retval < 0) {
opts->errstr = "failed to execute loader prog"; opts->errstr = "failed to execute loader prog";
if (err < 0) { if (err < 0) {
......
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