Add fd_array_cnt attribute for BPF_PROG_LOAD
Subsystems (5)
v3 cover Add fd_array_cnt attribute for BPF_PROG_LOAD
Add a new attribute to the bpf(BPF_PROG_LOAD) system call. If this new attribute is non-zero, then the fd_array is considered to be a continuous array of the fd_array_cnt length and to contain only proper map file descriptors or btf file descriptors.
This change allows maps, which aren't referenced directly by a BPF program, to be bound to the program _and_ also to be present during the program verification (so BPF_PROG_BIND_MAP is not enough for this use case).
The primary reason for this change is that it is a prerequisite for adding "instruction set" maps, which are both non-referenced by the program and must be present during the program verification.
The first four commits add the new functionality, the fifth adds corresponding self-tests, and the last two are small additional fixes.
v1 -> v2:
* rewrite the add_fd_from_fd_array() function (Eduard)
* a few cleanups in selftests (Eduard)
v2 -> v3:
* various renamings (Alexey)
* "0 is not special" (Alexey, Andrii)
* do not alloc memory on fd_array init (Alexey)
* fix leaking maps for error path (Hou Tao)
* use libbpf helpers vs. raw syscalls (Andrii)
* add comments on __btf_get_by_fd/__bpf_map_get (Alexey)
* remove extra code (Alexey)
Anton Protopopov (7):
bpf: add a __btf_get_by_fd helper
bpf: move map/prog compatibility checks
bpf: add fd_array_cnt attribute for prog_load
libbpf: prog load: allow to use fd_array_cnt
selftests/bpf: Add tests for fd_array_cnt
bpf: fix potential error return
selftest/bpf: replace magic constants by macros
include/linux/bpf.h | 17 + include/linux/btf.h | 2 + include/uapi/linux/bpf.h | 10 + kernel/bpf/btf.c | 13 +- kernel/bpf/core.c | 6 +- kernel/bpf/syscall.c | 2 +- kernel/bpf/verifier.c | 215 ++++++----- tools/include/uapi/linux/bpf.h | 10 + tools/lib/bpf/bpf.c | 5 +- tools/lib/bpf/bpf.h | 5 +- tools/lib/bpf/features.c | 2 +- .../selftests/bpf/prog_tests/fd_array.c | 340 ++++++++++++++++++ tools/testing/selftests/bpf/progs/syscall.c | 6 +- 13 files changed, 535 insertions(+), 98 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/fd_array.c
Signature · 1 line
2.34.1
PATCH v3 1/7 bpf: add a __btf_get_by_fd helper
Add a new helper to get a pointer to a struct btf from a file descriptor. This helper doesn't increase a refcnt. Add a comment explaining this and pointing to a corresponding function which does take a reference.
--- include/linux/bpf.h | 17 +++++++++++++++++ include/linux/btf.h | 2 ++ kernel/bpf/btf.c | 13 ++++--------- 3 files changed, 23 insertions(+), 9 deletions(-)
include/linux/bpf.h +17 -0
@@ -2294,6 +2294,14 @@ void __bpf_obj_drop_impl(void *p, const struct btf_record *rec, bool percpu);struct bpf_map *bpf_map_get (u32 ufd);struct bpf_map *bpf_map_get_with_uref (u32 ufd); +/* +* The __bpf_map_get() and __btf_get_by_fd() functions parse a file +* descriptor and return a corresponding map or btf object. +* Their names are double underscored to emphasize the fact that they +* do not increase refcnt. To also increase refcnt use corresponding +* bpf_map_get() and btf_get_by_fd() functions. +*/ +static inline struct bpf_map *__bpf_map_get (struct fd f) {if (fd_empty (f))
@@ -2303,6 +2311,15 @@ static inline struct bpf_map *__bpf_map_get(struct fd f)return fd_file (f)->private_data; } +static inline struct btf *__btf_get_by_fd (struct fd f) +{ +if (fd_empty (f)) +return ERR_PTR (-EBADF ); +if (unlikely (fd_file (f)->f_op != &btf_fops)) +return ERR_PTR (-EINVAL ); +return fd_file (f)->private_data; +} +void bpf_map_inc (struct bpf_map *map);void bpf_map_inc_with_uref (struct bpf_map *map);struct bpf_map *__bpf_map_inc_not_zero (struct bpf_map *map,bool uref);
include/linux/btf.h +2 -0
@@ -4,6 +4,7 @@#ifndef _LINUX_BTF_H#define _LINUX_BTF_H 1 +#include <linux/file.h> #include <linux/types.h> #include <linux/bpfptr.h> #include <linux/bsearch.h>
@@ -143,6 +144,7 @@ void btf_get(struct btf *btf);void btf_put (struct btf *btf);const struct btf_header *btf_header (const struct btf *btf);int btf_new_fd (const union bpf_attr *attr,bpfptr_t uattr,u32 uattr_sz); +struct btf *btf_get_by_fd (int fd); int btf_get_info_by_fd(const struct btf *btf, const union bpf_attr *attr,
kernel/bpf/btf.c +4 -9
@@ -7743,17 +7743,12 @@ int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)struct btf *btf_get_by_fd (int fd) { -struct btf *btf;CLASS (fd, f)(fd); +struct btf *btf; -if (fd_empty (f)) -return ERR_PTR (-EBADF ); - -if (fd_file (f)->f_op != &btf_fops) -return ERR_PTR (-EINVAL ); - - btf =fd_file (f)->private_data; -refcount_inc (&btf->refcnt); + btf =__btf_get_by_fd (f); +if (!IS_ERR (btf)) +refcount_inc (&btf->refcnt);return btf; }
Signature · 1 line
2.34.1
PATCH v3 2/7 bpf: move map/prog compatibility checks
In reply to Anton ProtopopovMove some inlined map/prog compatibility checks from the resolve_pseudo_ldimm64() function to the dedicated check_map_prog_compatibility() function. Call the latter function from the add_used_map_from_fd() function directly.
This simplifies code and optimizes logic a bit, as before these changes the check_map_prog_compatibility() function was executed on every map usage, which doesn't make sense, as it doesn't include any per-instruction checks, only map type vs. prog type.
(This patch also simplifies a consequent patch which will call the add_used_map_from_fd() function from another code path.)
--- kernel/bpf/verifier.c | 101 +++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 55 deletions(-)
kernel/bpf/verifier.c +46 -55
@@ -19064,6 +19064,12 @@ static bool is_tracing_prog_type(enum bpf_prog_type type) } } +static bool bpf_map_is_cgroup_storage (struct bpf_map *map) +{ +return (map->map_type ==BPF_MAP_TYPE_CGROUP_STORAGE || + map->map_type ==BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE ); +} +static int check_map_prog_compatibility (struct bpf_verifier_env *env,struct bpf_map *map,struct bpf_prog *prog)
@@ -19142,25 +19148,48 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env,return -EINVAL ; } -return 0 ; -} +if (bpf_map_is_cgroup_storage (map) && +bpf_cgroup_storage_assign (env->prog->aux, map)) { +verbose (env,"only one cgroup storage of each type is allowed\n" ); +return -EBUSY ; + } -static bool bpf_map_is_cgroup_storage (struct bpf_map *map) -{ -return (map->map_type ==BPF_MAP_TYPE_CGROUP_STORAGE || - map->map_type ==BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE ); +if (map->map_type ==BPF_MAP_TYPE_ARENA ) { +if (env->prog->aux->arena) { +verbose (env,"Only one arena per program\n" ); +return -EBUSY ; + } +if (!env->allow_ptr_leaks || !env->bpf_capable) { +verbose (env,"CAP_BPF and CAP_PERFMON are required to use arena\n" ); +return -EPERM ; + } +if (!env->prog->jit_requested) { +verbose (env,"JIT is required to use arena\n" ); +return -EOPNOTSUPP ; + } +if (!bpf_jit_supports_arena ()) { +verbose (env,"JIT doesn't support arena\n" ); +return -EOPNOTSUPP ; + } + env->prog->aux->arena = (void *)map; +if (!bpf_arena_get_user_vm_start (env->prog->aux->arena)) { +verbose (env,"arena's user address must be set via map_extra or mmap()\n" ); +return -EINVAL ; + } + } + +return 0 ; }/* Add map behind fd to used maps list, if it's not already there, and return -* its index. Also set *reused to true if this map was already in the list of -* used maps. +* its index. * Returns <0 on error, or >= 0 index, on success. */ -static int add_used_map_from_fd (struct bpf_verifier_env *env,int fd,bool *reused) +static int add_used_map_from_fd (struct bpf_verifier_env *env,int fd) {CLASS (fd, f)(fd);struct bpf_map *map; -int i; +int i, err; map =__bpf_map_get (f);if (IS_ERR (map)) {
@@ -19169,12 +19198,9 @@ static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd, bool *reus }/* check whether we recorded this map already */ -for (i =0 ; i < env->used_map_cnt; i++) { -if (env->used_maps[i] == map) { - *reused = true; +for (i =0 ; i < env->used_map_cnt; i++) +if (env->used_maps[i] == map)return i; - } - }if (env->used_map_cnt >=MAX_USED_MAPS ) { verbose(env, "The total number of maps per program has reached the limit of %u\n",
@@ -19182,6 +19208,10 @@ static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd, bool *reusreturn -E2BIG ; } + err =check_map_prog_compatibility (env, map, env->prog); +if (err) +return err; +if (env->prog->sleepable)atomic64_inc (&map->sleepable_refcnt);
@@ -19192,7 +19222,6 @@ static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd, bool *reus*/ bpf_map_inc (map); - *reused = false; env->used_maps[env->used_map_cnt++] = map;return env->used_map_cnt -1 ;
@@ -19229,7 +19258,6 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env)int map_idx;u64 addr;u32 fd; -bool reused; if (i == insn_cnt - 1 || insn[1].code != 0 || insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
@@ -19290,7 +19318,7 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env)break ; } - map_idx =add_used_map_from_fd (env, fd, &reused); + map_idx =add_used_map_from_fd (env, fd);if (map_idx <0 )return map_idx; map = env->used_maps[map_idx];
@@ -19298,10 +19326,6 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) aux = &env->insn_aux_data[i]; aux->map_index = map_idx; - err =check_map_prog_compatibility (env, map, env->prog); -if (err) -return err; -if (insn[0 ].src_reg ==BPF_PSEUDO_MAP_FD || insn[0 ].src_reg ==BPF_PSEUDO_MAP_IDX ) { addr = (unsigned long )map;
@@ -19332,39 +19356,6 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) insn[0 ].imm = (u32 )addr; insn[1 ].imm = addr >>32 ; -/* proceed with extra checks only if its newly added used map */ -if (reused) - goto next_insn; - -if (bpf_map_is_cgroup_storage (map) && -bpf_cgroup_storage_assign (env->prog->aux, map)) { -verbose (env,"only one cgroup storage of each type is allowed\n" ); -return -EBUSY ; - } -if (map->map_type ==BPF_MAP_TYPE_ARENA ) { -if (env->prog->aux->arena) { -verbose (env,"Only one arena per program\n" ); -return -EBUSY ; - } -if (!env->allow_ptr_leaks || !env->bpf_capable) { -verbose (env,"CAP_BPF and CAP_PERFMON are required to use arena\n" ); -return -EPERM ; - } -if (!env->prog->jit_requested) { -verbose (env,"JIT is required to use arena\n" ); -return -EOPNOTSUPP ; - } -if (!bpf_jit_supports_arena ()) { -verbose (env,"JIT doesn't support arena\n" ); -return -EOPNOTSUPP ; - } - env->prog->aux->arena = (void *)map; -if (!bpf_arena_get_user_vm_start (env->prog->aux->arena)) { -verbose (env,"arena's user address must be set via map_extra or mmap()\n" ); -return -EINVAL ; - } - } - next_insn: insn++; i++;
Signature · 1 line
2.34.1
PATCH v3 3/7 bpf: add fd_array_cnt attribute for prog_load
In reply to Anton ProtopopovThe fd_array attribute of the BPF_PROG_LOAD syscall may contain a set of file descriptors: maps or btfs. This field was introduced as a sparse array. Introduce a new attribute, fd_array_cnt, which, if present, indicates that the fd_array is a continuous array of the corresponding length.
If fd_array_cnt is non-zero, then every map in the fd_array will be bound to the program, as if it was used by the program. This functionality is similar to the BPF_PROG_BIND_MAP syscall, but such maps can be used by the verifier during the program load.
--- include/uapi/linux/bpf.h | 10 ++++ kernel/bpf/syscall.c | 2 +- kernel/bpf/verifier.c | 94 ++++++++++++++++++++++++++++------ tools/include/uapi/linux/bpf.h | 10 ++++ 4 files changed, 100 insertions(+), 16 deletions(-)
include/uapi/linux/bpf.h +10 -0
@@ -1573,6 +1573,16 @@ union bpf_attr {* If provided, prog_flags should have BPF_F_TOKEN_FD flag set. */ __s32 prog_token_fd; +/* The fd_array_cnt can be used to pass the length of the +* fd_array array. In this case all the [map] file descriptors +* passed in this array will be bound to the program, even if +* the maps are not referenced directly. The functionality is +* similar to the BPF_PROG_BIND_MAP syscall, but maps can be +* used by the verifier during the program load. If provided, +* then the fd_array[0,...,fd_array_cnt-1] is expected to be +* continuous. +*/ +__u32 fd_array_cnt; };struct {/* anonymous struct used by BPF_OBJ_* commands */
kernel/bpf/syscall.c +1 -1
@@ -2729,7 +2729,7 @@ static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) }/* last field in 'union bpf_attr' used by this command */ -#define BPF_PROG_LOAD_LAST_FIELD prog_token_fd +#define BPF_PROG_LOAD_LAST_FIELD fd_array_cntstatic int bpf_prog_load (union bpf_attr *attr,bpfptr_t uattr,u32 uattr_size) {
kernel/bpf/verifier.c +79 -15
133 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +10 -0
@@ -1573,6 +1573,16 @@ union bpf_attr {* If provided, prog_flags should have BPF_F_TOKEN_FD flag set. */ __s32 prog_token_fd; +/* The fd_array_cnt can be used to pass the length of the +* fd_array array. In this case all the [map] file descriptors +* passed in this array will be bound to the program, even if +* the maps are not referenced directly. The functionality is +* similar to the BPF_PROG_BIND_MAP syscall, but maps can be +* used by the verifier during the program load. If provided, +* then the fd_array[0,...,fd_array_cnt-1] is expected to be +* continuous. +*/ +__u32 fd_array_cnt; };struct {/* anonymous struct used by BPF_OBJ_* commands */
Signature · 1 line
2.34.1
Re: [PATCH v3 bpf-next 3/7] bpf: add fd_array_cnt attribute for prog_load
Quoted 117 lines · Anton Protopopov
The
fd_arrayattribute of theBPF_PROG_LOADsyscall may contain a set of file descriptors: maps or btfs. This field was introduced as a sparse array. Introduce a new attribute,fd_array_cnt, which, if present, indicates that thefd_arrayis a continuous array of the corresponding length.If
fd_array_cntis non-zero, then every map in thefd_arraywill be bound to the program, as if it was used by the program. This functionality is similar to theBPF_PROG_BIND_MAPsyscall, but such maps can be used by the verifier during the program load.Signed-off-by Anton Protopopov <[email protected]>--- include/uapi/linux/bpf.h | 10 ++++ kernel/bpf/syscall.c | 2 +- kernel/bpf/verifier.c | 94 ++++++++++++++++++++++++++++------ tools/include/uapi/linux/bpf.h | 10 ++++ 4 files changed, 100 insertions(+), 16 deletions(-)include/uapi/linux/bpf.h +10
-0@@ -1573,6 +1573,16 @@ union bpf_attr {* If provided, prog_flags should have BPF_F_TOKEN_FD flag set. */ __s32 prog_token_fd; +/* The fd_array_cnt can be used to pass the length of the +* fd_array array. In this case all the [map] file descriptors +* passed in this array will be bound to the program, even if +* the maps are not referenced directly. The functionality is +* similar to the BPF_PROG_BIND_MAP syscall, but maps can be +* used by the verifier during the program load. If provided, +* then the fd_array[0,...,fd_array_cnt-1] is expected to be +* continuous. +*/ +__u32 fd_array_cnt; };struct {/* anonymous struct used by BPF_OBJ_* commands */ kernel/bpf/syscall.c +1
-1@@ -2729,7 +2729,7 @@ static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) }/* last field in 'union bpf_attr' used by this command */ -#define BPF_PROG_LOAD_LAST_FIELD prog_token_fd +#define BPF_PROG_LOAD_LAST_FIELD fd_array_cntstatic int bpf_prog_load (union bpf_attr *attr,bpfptr_t uattr,u32 uattr_size) {kernel/bpf/verifier.c +20
-14@@ -19181,22 +19181,10 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env,return 0 ; } -/* Add map behind fd to used maps list, if it's not already there, and return -* its index. -* Returns <0 on error, or >= 0 index, on success. -*/ -static int add_used_map_from_fd (struct bpf_verifier_env *env,int fd) +static int __add_used_map (struct bpf_verifier_env *env,struct bpf_map *map) { -CLASS (fd, f)(fd); -struct bpf_map *map;int i, err; - map =__bpf_map_get (f); -if (IS_ERR (map)) { -verbose (env,"fd %d is not pointing to valid bpf_map\n" , fd); -return PTR_ERR (map); - } -/* check whether we recorded this map already */ for (i =0 ; i < env->used_map_cnt; i++)if (env->used_maps[i] == map)@@ -19227,6 +19215,24 @@ static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd)return env->used_map_cnt -1 ; } +/* Add map behind fd to used maps list, if it's not already there, and return +* its index. +* Returns <0 on error, or >= 0 index, on success. +*/ +static int add_used_map (struct bpf_verifier_env *env,int fd) +{ +struct bpf_map *map; +CLASS (fd, f)(fd); + + map =__bpf_map_get (f); +if (IS_ERR (map)) { +verbose (env,"fd %d is not pointing to valid bpf_map\n" , fd); +return PTR_ERR (map); + } + +return __add_used_map (env, map); +} +/* find and rewrite pseudo imm in ld_imm64 instructions: * * 1. if it accesses map FD, replace it with actual map pointer. @@ -19318,7 +19324,7 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env)break ; } - map_idx =add_used_map_from_fd (env, fd); + map_idx =add_used_map (env, fd);if (map_idx <0 )return map_idx; map = env->used_maps[map_idx];
kernel/bpf/verifier.c struct btf *bpf_get_btf_vmlinux(void) · line 22539
Quoted 8 lines
kernel/bpf/verifier.c +5
-0@@ -22526,6 +22532,61 @@ struct btf *bpf_get_btf_vmlinux(void)return btf_vmlinux; } +/* +* The add_fd_from_fd_array() is executed only if fd_array_cnt is given. In +* this case expect that every file descriptor in the array is either a map or +* a BTF, or a hole (0). Everything else is considered to be trash. +*/
The comment is now incorrect. 0 is not a valid hole. No holes are allowed. And no trash.
Quoted 6 lines
kernel/bpf/verifier.c +6
-0+static int add_fd_from_fd_array (struct bpf_verifier_env *env,int fd) +{ +struct bpf_map *map; +CLASS (fd, f)(fd); +int ret; +
kernel/bpf/verifier.c struct btf *bpf_get_btf_vmlinux(void) · line 22555
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ map =__bpf_map_get (f); +if (!IS_ERR (map)) { + ret =__add_used_map (env, map); +if (ret <0 ) +return ret; +return 0 ; + } + +if (!IS_ERR (__btf_get_by_fd (f))) +return 0 ;
It is quite asymmetrical that maps get refcnted and saved while BTFs just checked for existence and not refcnted. A comment is necessary.
kernel/bpf/verifier.c struct btf *bpf_get_btf_vmlinux(void) · line 22561
Quoted 6 lines
kernel/bpf/verifier.c +6
-0+ +verbose (env,"fd %d is not pointing to valid bpf_map or btf\n" , fd); +return PTR_ERR (map); +} + +static int init_fd_array (struct bpf_verifier_env *env,union bpf_attr *attr,bpfptr_t uattr)
The name still bothers me.
The fd_array in the env and in uattr is not initialized.
Maybe process_fd_array() ?
pw-bot: cr
Re: [PATCH v3 bpf-next 3/7] bpf: add fd_array_cnt attribute for prog_load
Quoted 121 lines · Alexei Starovoitov
Quoted 120 lines · Anton Protopopov
The
fd_arrayattribute of theBPF_PROG_LOADsyscall may contain a set of file descriptors: maps or btfs. This field was introduced as a sparse array. Introduce a new attribute,fd_array_cnt, which, if present, indicates that thefd_arrayis a continuous array of the corresponding length.If
fd_array_cntis non-zero, then every map in thefd_arraywill be bound to the program, as if it was used by the program. This functionality is similar to theBPF_PROG_BIND_MAPsyscall, but such maps can be used by the verifier during the program load.Signed-off-by Anton Protopopov <[email protected]>--- include/uapi/linux/bpf.h | 10 ++++ kernel/bpf/syscall.c | 2 +- kernel/bpf/verifier.c | 94 ++++++++++++++++++++++++++++------ tools/include/uapi/linux/bpf.h | 10 ++++ 4 files changed, 100 insertions(+), 16 deletions(-)include/uapi/linux/bpf.h +10
-0@@ -1573,6 +1573,16 @@ union bpf_attr {* If provided, prog_flags should have BPF_F_TOKEN_FD flag set. */ __s32 prog_token_fd; +/* The fd_array_cnt can be used to pass the length of the +* fd_array array. In this case all the [map] file descriptors +* passed in this array will be bound to the program, even if +* the maps are not referenced directly. The functionality is +* similar to the BPF_PROG_BIND_MAP syscall, but maps can be +* used by the verifier during the program load. If provided, +* then the fd_array[0,...,fd_array_cnt-1] is expected to be +* continuous. +*/ +__u32 fd_array_cnt; };struct {/* anonymous struct used by BPF_OBJ_* commands */ kernel/bpf/syscall.c +1
-1@@ -2729,7 +2729,7 @@ static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) }/* last field in 'union bpf_attr' used by this command */ -#define BPF_PROG_LOAD_LAST_FIELD prog_token_fd +#define BPF_PROG_LOAD_LAST_FIELD fd_array_cntstatic int bpf_prog_load (union bpf_attr *attr,bpfptr_t uattr,u32 uattr_size) {kernel/bpf/verifier.c +20
-14@@ -19181,22 +19181,10 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env,return 0 ; } -/* Add map behind fd to used maps list, if it's not already there, and return -* its index. -* Returns <0 on error, or >= 0 index, on success. -*/ -static int add_used_map_from_fd (struct bpf_verifier_env *env,int fd) +static int __add_used_map (struct bpf_verifier_env *env,struct bpf_map *map) { -CLASS (fd, f)(fd); -struct bpf_map *map;int i, err; - map =__bpf_map_get (f); -if (IS_ERR (map)) { -verbose (env,"fd %d is not pointing to valid bpf_map\n" , fd); -return PTR_ERR (map); - } -/* check whether we recorded this map already */ for (i =0 ; i < env->used_map_cnt; i++)if (env->used_maps[i] == map)@@ -19227,6 +19215,24 @@ static int add_used_map_from_fd(struct bpf_verifier_env *env, int fd)return env->used_map_cnt -1 ; } +/* Add map behind fd to used maps list, if it's not already there, and return +* its index. +* Returns <0 on error, or >= 0 index, on success. +*/ +static int add_used_map (struct bpf_verifier_env *env,int fd) +{ +struct bpf_map *map; +CLASS (fd, f)(fd); + + map =__bpf_map_get (f); +if (IS_ERR (map)) { +verbose (env,"fd %d is not pointing to valid bpf_map\n" , fd); +return PTR_ERR (map); + } + +return __add_used_map (env, map); +} +/* find and rewrite pseudo imm in ld_imm64 instructions: * * 1. if it accesses map FD, replace it with actual map pointer. @@ -19318,7 +19324,7 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env)break ; } - map_idx =add_used_map_from_fd (env, fd); + map_idx =add_used_map (env, fd);if (map_idx <0 )return map_idx; map = env->used_maps[map_idx];@@ -22526,6 +22532,61 @@ struct btf *bpf_get_btf_vmlinux(void)return btf_vmlinux; }
Quoted 8 lines
kernel/bpf/verifier.c struct btf *bpf_get_btf_vmlinux(void) · line 22539
Quoted 5 lines
kernel/bpf/verifier.c +5
-0+/* +* The add_fd_from_fd_array() is executed only if fd_array_cnt is given. In +* this case expect that every file descriptor in the array is either a map or +* a BTF, or a hole (0). Everything else is considered to be trash. +*/ The comment is now incorrect. 0 is not a valid hole. No holes are allowed. And no trash.
Thanks, fixed.
Quoted 12 lines
Quoted 12 lines
kernel/bpf/verifier.c +12
-0+static int add_fd_from_fd_array (struct bpf_verifier_env *env,int fd) +{ +struct bpf_map *map; +CLASS (fd, f)(fd); +int ret; + + map =__bpf_map_get (f); +if (!IS_ERR (map)) { + ret =__add_used_map (env, map); +if (ret <0 ) +return ret; +return 0 ;
Quoted 8 lines
kernel/bpf/verifier.c struct btf *bpf_get_btf_vmlinux(void) · line 22555
Quoted 4 lines
kernel/bpf/verifier.c +4
-0+ } + +if (!IS_ERR (__btf_get_by_fd (f))) +return 0 ;It is quite asymmetrical that maps get refcnted and saved while BTFs just checked for existence and not refcnted. A comment is necessary.
Added.
Quoted 11 lines
kernel/bpf/verifier.c struct btf *bpf_get_btf_vmlinux(void) · line 22561
Quoted 7 lines
kernel/bpf/verifier.c +6
-0+ +verbose (env,"fd %d is not pointing to valid bpf_map or btf\n" , fd); +return PTR_ERR (map); +} + +static int init_fd_array (struct bpf_verifier_env *env,union bpf_attr *attr,bpfptr_t uattr)The name still bothers me.
Thefd_arrayin the env and in uattr is not initialized.
Quoted 1 line
Maybe
process_fd_array()?
Sure, 'process_fd_array' looks fine for me.
Quoted 1 line
pw-bot: cr
PATCH v3 4/7 libbpf: prog load: allow to use fd_array_cnt
In reply to Anton ProtopopovAdd new fd_array_cnt field to bpf_prog_load_opts
and pass it in bpf_attr, if set.
--- tools/lib/bpf/bpf.c | 5 +++-- tools/lib/bpf/bpf.h | 5 ++++- tools/lib/bpf/features.c | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-)
tools/lib/bpf/bpf.c +3 -2
@@ -105,7 +105,7 @@ int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts)*/ int probe_memcg_account (int token_fd) { - const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); + const size_t attr_sz = offsetofend(union bpf_attr, fd_array_cnt);struct bpf_insn insns[] = {BPF_EMIT_CALL (BPF_FUNC_ktime_get_coarse_ns),BPF_EXIT_INSN (),
@@ -238,7 +238,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns, size_t insn_cnt, struct bpf_prog_load_opts *opts) { - const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); + const size_t attr_sz = offsetofend(union bpf_attr, fd_array_cnt);void *finfo =NULL , *linfo =NULL ;const char *func_info, *line_info;__u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;
@@ -311,6 +311,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, attr.line_info_cnt =OPTS_GET (opts, line_info_cnt,0 ); attr.fd_array =ptr_to_u64 (OPTS_GET (opts, fd_array,NULL )); + attr.fd_array_cnt =OPTS_GET (opts, fd_array_cnt,0 );if (log_level) { attr.log_buf =ptr_to_u64 (log_buf);
tools/lib/bpf/bpf.h +4 -1
@@ -107,9 +107,12 @@ struct bpf_prog_load_opts {*/ __u32 log_true_size;__u32 token_fd; + +/* if set, provides the length of fd_array */ +__u32 fd_array_cnt; size_t :0; }; -#define bpf_prog_load_opts__last_field token_fd +#define bpf_prog_load_opts__last_field fd_array_cnt LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type, const char *prog_name, const char *license,
tools/lib/bpf/features.c +1 -1
@@ -22,7 +22,7 @@ int probe_fd(int fd)static int probe_kern_prog_name (int token_fd) { - const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); + const size_t attr_sz = offsetofend(union bpf_attr, fd_array_cnt);struct bpf_insn insns[] = {BPF_MOV64_IMM (BPF_REG_0 ,0 ),BPF_EXIT_INSN (),
Signature · 1 line
2.34.1
Re: [PATCH v3 bpf-next 4/7] libbpf: prog load: allow to use fd_array_cnt
Quoted 16 lines · Anton Protopopov
Add new
fd_array_cntfield tobpf_prog_load_opts
and pass it inbpf_attr, if set.Signed-off-by Anton Protopopov <[email protected]>--- tools/lib/bpf/bpf.c | 5 +++-- tools/lib/bpf/bpf.h | 5 ++++- tools/lib/bpf/features.c | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-)tools/lib/bpf/bpf.c +0
-0
tools/lib/bpf/bpf.c int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts) · line 108
Quoted 5 lines
tools/lib/bpf/bpf.c +1
-1@@ -105,7 +105,7 @@ int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts)*/ int probe_memcg_account (int token_fd) { - const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); + const size_t attr_sz = offsetofend(union bpf_attr, fd_array_cnt);
Thankfully this function doesn't set fd_array_cnt below. Otherwise the detection of memcg would fail on older kernels. Let's avoid people mindlessly adding init of fd_array_cnt here by accident. Simply keep this line as-is.offsetofend(.., prog_token_fd) is fine.
Quoted 43 lines
tools/lib/bpf/bpf.c +2
-1struct bpf_insn insns[] = {BPF_EMIT_CALL (BPF_FUNC_ktime_get_coarse_ns),BPF_EXIT_INSN (),@@ -238,7 +238,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns, size_t insn_cnt, struct bpf_prog_load_opts *opts) { - const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); + const size_t attr_sz = offsetofend(union bpf_attr, fd_array_cnt);void *finfo =NULL , *linfo =NULL ;const char *func_info, *line_info;__u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;@@ -311,6 +311,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, attr.line_info_cnt =OPTS_GET (opts, line_info_cnt,0 ); attr.fd_array =ptr_to_u64 (OPTS_GET (opts, fd_array,NULL )); + attr.fd_array_cnt =OPTS_GET (opts, fd_array_cnt,0 );if (log_level) { attr.log_buf =ptr_to_u64 (log_buf);tools/lib/bpf/bpf.h +4
-1@@ -107,9 +107,12 @@ struct bpf_prog_load_opts {*/ __u32 log_true_size;__u32 token_fd; + +/* if set, provides the length of fd_array */ +__u32 fd_array_cnt; size_t :0; }; -#define bpf_prog_load_opts__last_field token_fd +#define bpf_prog_load_opts__last_field fd_array_cnt LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type, const char *prog_name, const char *license,tools/lib/bpf/features.c +0
-0
tools/lib/bpf/features.c int probe_fd(int fd) · line 25
Quoted 5 lines
tools/lib/bpf/features.c +1
-1@@ -22,7 +22,7 @@ int probe_fd(int fd)static int probe_kern_prog_name (int token_fd) { - const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); + const size_t attr_sz = offsetofend(union bpf_attr, fd_array_cnt);
Same here. Don't change it.
Re: [PATCH v3 bpf-next 4/7] libbpf: prog load: allow to use fd_array_cnt
Quoted 12 lines · Alexei Starovoitov
Quoted 11 lines · Anton Protopopov
Add new
fd_array_cntfield tobpf_prog_load_opts
and pass it inbpf_attr, if set.Signed-off-by Anton Protopopov <[email protected]>--- tools/lib/bpf/bpf.c | 5 +++-- tools/lib/bpf/bpf.h | 5 ++++- tools/lib/bpf/features.c | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-)
Quoted 16 lines
tools/lib/bpf/bpf.c int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts) · line 108
Quoted 10 lines
tools/lib/bpf/bpf.c +1
-1@@ -105,7 +105,7 @@ int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts)*/ int probe_memcg_account (int token_fd) { - const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); + const size_t attr_sz = offsetofend(union bpf_attr, fd_array_cnt);Thankfully this function doesn't set
fd_array_cntbelow. Otherwise the detection of memcg would fail on older kernels. Let's avoid people mindlessly adding init offd_array_cnthere by accident. Simply keep this line as-is.offsetofend(.., prog_token_fd)is fine.
Ok, thanks, reverted (as with the one you've mentioned below)
Quoted 50 lines
Quoted 43 lines
tools/lib/bpf/bpf.c +2
-1struct bpf_insn insns[] = {BPF_EMIT_CALL (BPF_FUNC_ktime_get_coarse_ns),BPF_EXIT_INSN (),@@ -238,7 +238,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns, size_t insn_cnt, struct bpf_prog_load_opts *opts) { - const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); + const size_t attr_sz = offsetofend(union bpf_attr, fd_array_cnt);void *finfo =NULL , *linfo =NULL ;const char *func_info, *line_info;__u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;@@ -311,6 +311,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, attr.line_info_cnt =OPTS_GET (opts, line_info_cnt,0 ); attr.fd_array =ptr_to_u64 (OPTS_GET (opts, fd_array,NULL )); + attr.fd_array_cnt =OPTS_GET (opts, fd_array_cnt,0 );if (log_level) { attr.log_buf =ptr_to_u64 (log_buf);tools/lib/bpf/bpf.h +4
-1@@ -107,9 +107,12 @@ struct bpf_prog_load_opts {*/ __u32 log_true_size;__u32 token_fd; + +/* if set, provides the length of fd_array */ +__u32 fd_array_cnt; size_t :0; }; -#define bpf_prog_load_opts__last_field token_fd +#define bpf_prog_load_opts__last_field fd_array_cnt LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type, const char *prog_name, const char *license,tools/lib/bpf/features.c +0
-0tools/lib/bpf/features.c int probe_fd(int fd) · line 25
Quoted 5 lines
tools/lib/bpf/features.c +1
-1@@ -22,7 +22,7 @@ int probe_fd(int fd)static int probe_kern_prog_name (int token_fd) { - const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); + const size_t attr_sz = offsetofend(union bpf_attr, fd_array_cnt);Same here. Don't change it.
PATCH v3 5/7 selftests/bpf: Add tests for fd_array_cnt
In reply to Anton ProtopopovAdd a new set of tests to test the new field in PROG_LOAD-related part of bpf_attr: fd_array_cnt.
Add the following test cases:
* fd_array_cnt/no-fd-array: program is loaded in a normal way, without any fd_array present
* fd_array_cnt/fd-array-ok: pass two extra non-used maps, check that they're bound to the program
* fd_array_cnt/fd-array-dup-input: pass a few extra maps, only two of which are unique
* fd_array_cnt/fd-array-ref-maps-in-array: pass a map in fd_array which is also referenced from within the program
* fd_array_cnt/fd-array-trash-input: pass array with some trash
* fd_array_cnt/fd-array-with-holes: pass an array with holes (fd=0)
* fd_array_cnt/fd-array-2big: pass too large array
All the tests above are using the bpf(2) syscall directly, no libbpf involved.
--- kernel/bpf/verifier.c | 30 +- .../selftests/bpf/prog_tests/fd_array.c | 340 ++++++++++++++++++ 2 files changed, 355 insertions(+), 15 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/fd_array.c
kernel/bpf/verifier.c +15 -15
@@ -22620,7 +22620,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 env->ops = bpf_verifier_ops[env->prog->type]; ret =init_fd_array (env, attr, uattr);if (ret) - goto err_free_aux_data; + goto err_release_maps; env->allow_ptr_leaks =bpf_allow_ptr_leaks (env->prog->aux->token); env->allow_uninit_stack =bpf_allow_uninit_stack (env->prog->aux->token);
@@ -22773,11 +22773,11 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, log_true_size), &log_true_size, sizeof(log_true_size))) { ret = -EFAULT ; - goto err_release_maps; + goto err_ext; }if (ret) - goto err_release_maps; + goto err_ext;if (env->used_map_cnt) {/* if program passed verifier, update used_maps in bpf_prog_info */
@@ -22787,7 +22787,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3if (!env->prog->aux->used_maps) { ret = -ENOMEM ; - goto err_release_maps; + goto err_ext; } memcpy(env->prog->aux->used_maps, env->used_maps,
@@ -22801,7 +22801,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 GFP_KERNEL);if (!env->prog->aux->used_btfs) { ret = -ENOMEM ; - goto err_release_maps; + goto err_ext; } memcpy(env->prog->aux->used_btfs, env->used_btfs,
@@ -22817,15 +22817,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3adjust_btf_func (env); -err_release_maps: -if (!env->prog->aux->used_maps) -/* if we didn't copy map pointers into bpf_prog_info, release -* them now. Otherwise free_used_maps() will release them. -*/ -release_maps (env); -if (!env->prog->aux->used_btfs) -release_btfs (env); - +err_ext:/* extension progs temporarily inherit the attach_type of their targets for verification purposes, so set it back to zero before returning */
@@ -22838,7 +22830,15 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 err_unlock:if (!is_priv)mutex_unlock (&bpf_verifier_lock); -err_free_aux_data: +err_release_maps: +if (!env->prog->aux->used_maps) +/* if we didn't copy map pointers into bpf_prog_info, release +* them now. Otherwise free_used_maps() will release them. +*/ +release_maps (env); +if (!env->prog->aux->used_btfs) +release_btfs (env); +vfree (env->insn_aux_data);kvfree (env->insn_hist); err_free_env:
tools/testing/selftests/bpf/prog_tests/fd_array.c new file +340 -0
340 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v3 bpf-next 5/7] selftests/bpf: Add tests for fd_array_cnt
Quoted 102 lines · Anton Protopopov
Add a new set of tests to test the new field in PROG_LOAD-related part of
bpf_attr:fd_array_cnt.Add the following test cases:
* fd_array_cnt/no-fd-array: program is loaded in a normal way, without any
fd_arraypresent* fd_array_cnt/fd-array-ok: pass two extra non-used maps, check that they're bound to the program
* fd_array_cnt/fd-array-dup-input: pass a few extra maps, only two of which are unique
* fd_array_cnt/fd-array-ref-maps-in-array: pass a map in
fd_arraywhich is also referenced from within the program* fd_array_cnt/fd-array-trash-input: pass array with some trash
* fd_array_cnt/fd-array-with-holes: pass an array with holes (fd=0)
* fd_array_cnt/fd-array-2big: pass too large array
All the tests above are using the bpf(2) syscall directly, no libbpf involved.
Signed-off-by Anton Protopopov <[email protected]>--- kernel/bpf/verifier.c | 30 +- .../selftests/bpf/prog_tests/fd_array.c | 340 ++++++++++++++++++ 2 files changed, 355 insertions(+), 15 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/fd_array.ckernel/bpf/verifier.c +7
-15@@ -22620,7 +22620,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 env->ops = bpf_verifier_ops[env->prog->type]; ret =init_fd_array (env, attr, uattr);if (ret) - goto err_free_aux_data; + goto err_release_maps; env->allow_ptr_leaks =bpf_allow_ptr_leaks (env->prog->aux->token); env->allow_uninit_stack =bpf_allow_uninit_stack (env->prog->aux->token);@@ -22773,11 +22773,11 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, log_true_size), &log_true_size, sizeof(log_true_size))) { ret = -EFAULT ; - goto err_release_maps; + goto err_ext; }if (ret) - goto err_release_maps; + goto err_ext;if (env->used_map_cnt) {/* if program passed verifier, update used_maps in bpf_prog_info */ @@ -22787,7 +22787,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3if (!env->prog->aux->used_maps) { ret = -ENOMEM ; - goto err_release_maps; + goto err_ext; } memcpy(env->prog->aux->used_maps, env->used_maps,@@ -22801,7 +22801,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 GFP_KERNEL);if (!env->prog->aux->used_btfs) { ret = -ENOMEM ; - goto err_release_maps; + goto err_ext; } memcpy(env->prog->aux->used_btfs, env->used_btfs,@@ -22817,15 +22817,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3adjust_btf_func (env); -err_release_maps: -if (!env->prog->aux->used_maps) -/* if we didn't copy map pointers into bpf_prog_info, release -* them now. Otherwise free_used_maps() will release them. -*/ -release_maps (env); -if (!env->prog->aux->used_btfs) -release_btfs (env); - +err_ext:/* extension progs temporarily inherit the attach_type of their targets for verification purposes, so set it back to zero before returning */ @@ -22838,7 +22830,15 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 err_unlock:if (!is_priv)mutex_unlock (&bpf_verifier_lock); -err_free_aux_data: +err_release_maps:
kernel/bpf/verifier.c int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 · line 22843
Quoted 10 lines
kernel/bpf/verifier.c +8
-0+if (!env->prog->aux->used_maps) +/* if we didn't copy map pointers into bpf_prog_info, release +* them now. Otherwise free_used_maps() will release them. +*/ +release_maps (env); +if (!env->prog->aux->used_btfs) +release_btfs (env); +vfree (env->insn_aux_data);kvfree (env->insn_hist);
verifier.c hunk shouldn't be mixed with the selftests.
Looks like it should be in patch 3?
Not sure what it does though.
Re: [PATCH v3 bpf-next 5/7] selftests/bpf: Add tests for fd_array_cnt
Quoted 116 lines · Alexei Starovoitov
Quoted 102 lines · Anton Protopopov
Add a new set of tests to test the new field in PROG_LOAD-related part of
bpf_attr:fd_array_cnt.Add the following test cases:
* fd_array_cnt/no-fd-array: program is loaded in a normal way, without any
fd_arraypresent* fd_array_cnt/fd-array-ok: pass two extra non-used maps, check that they're bound to the program
* fd_array_cnt/fd-array-dup-input: pass a few extra maps, only two of which are unique
* fd_array_cnt/fd-array-ref-maps-in-array: pass a map in
fd_arraywhich is also referenced from within the program* fd_array_cnt/fd-array-trash-input: pass array with some trash
* fd_array_cnt/fd-array-with-holes: pass an array with holes (fd=0)
* fd_array_cnt/fd-array-2big: pass too large array
All the tests above are using the bpf(2) syscall directly, no libbpf involved.
Signed-off-by Anton Protopopov <[email protected]>--- kernel/bpf/verifier.c | 30 +- .../selftests/bpf/prog_tests/fd_array.c | 340 ++++++++++++++++++ 2 files changed, 355 insertions(+), 15 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/fd_array.ckernel/bpf/verifier.c +7
-15@@ -22620,7 +22620,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 env->ops = bpf_verifier_ops[env->prog->type]; ret =init_fd_array (env, attr, uattr);if (ret) - goto err_free_aux_data; + goto err_release_maps; env->allow_ptr_leaks =bpf_allow_ptr_leaks (env->prog->aux->token); env->allow_uninit_stack =bpf_allow_uninit_stack (env->prog->aux->token);@@ -22773,11 +22773,11 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 copy_to_bpfptr_offset(uattr, offsetof(union bpf_attr, log_true_size), &log_true_size, sizeof(log_true_size))) { ret = -EFAULT ; - goto err_release_maps; + goto err_ext; }if (ret) - goto err_release_maps; + goto err_ext;if (env->used_map_cnt) {/* if program passed verifier, update used_maps in bpf_prog_info */ @@ -22787,7 +22787,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3if (!env->prog->aux->used_maps) { ret = -ENOMEM ; - goto err_release_maps; + goto err_ext; } memcpy(env->prog->aux->used_maps, env->used_maps,@@ -22801,7 +22801,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 GFP_KERNEL);if (!env->prog->aux->used_btfs) { ret = -ENOMEM ; - goto err_release_maps; + goto err_ext; } memcpy(env->prog->aux->used_btfs, env->used_btfs,@@ -22817,15 +22817,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3adjust_btf_func (env); -err_release_maps: -if (!env->prog->aux->used_maps) -/* if we didn't copy map pointers into bpf_prog_info, release -* them now. Otherwise free_used_maps() will release them. -*/ -release_maps (env); -if (!env->prog->aux->used_btfs) -release_btfs (env); - +err_ext:/* extension progs temporarily inherit the attach_type of their targets for verification purposes, so set it back to zero before returning */ @@ -22838,7 +22830,15 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 err_unlock:if (!is_priv)mutex_unlock (&bpf_verifier_lock); -err_free_aux_data: +err_release_maps:kernel/bpf/verifier.c int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 · line 22843
Quoted 10 lines
kernel/bpf/verifier.c +8
-0+if (!env->prog->aux->used_maps) +/* if we didn't copy map pointers into bpf_prog_info, release +* them now. Otherwise free_used_maps() will release them. +*/ +release_maps (env); +if (!env->prog->aux->used_btfs) +release_btfs (env); +vfree (env->insn_aux_data);kvfree (env->insn_hist);
verifier.chunk shouldn't be mixed with the selftests.
Quoted 1 line
Looks like it should be in patch 3?
Sorry, wrong rebase -i
Quoted 1 line
Not sure what it does though.
Yeah, thanks. I've simplified this part of Patch 3, this diff will not appear in v4
PATCH v3 6/7 bpf: fix potential error return
In reply to Anton ProtopopovThe bpf_remove_insns() function returns WARN_ON_ONCE(error), where error is a result of bpf_adj_branches(), and thus should be always 0 However, if for any reason it is not 0, then it will be converted to boolean by WARN_ON_ONCE and returned to user space as 1, not an actual error value. Fix this by returning the original err after the WARN check.
--- kernel/bpf/core.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/bpf/core.c +5 -1
@@ -539,6 +539,8 @@ struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,int bpf_remove_insns (struct bpf_prog *prog,u32 off,u32 cnt) { +int err; +/* Branch offsets can't overflow when program is shrinking, no need * to call bpf_adj_branches(..., true) here */
@@ -546,7 +548,9 @@ int bpf_remove_insns(struct bpf_prog *prog, u32 off, u32 cnt) sizeof(struct bpf_insn) * (prog->len - off - cnt)); prog->len -= cnt; -return WARN_ON_ONCE (bpf_adj_branches (prog, off, off + cnt, off, false)); + err =bpf_adj_branches (prog, off, off + cnt, off, false); +WARN_ON_ONCE (err); +return err; }static void bpf_prog_kallsyms_del_subprogs (struct bpf_prog *fp)
Signature · 1 line
2.34.1
PATCH v3 7/7 selftest/bpf: replace magic constants by macros
In reply to Anton ProtopopovReplace magic constants in a BTF structure initialization code by proper macros, as is done in other similar selftests.
--- tools/testing/selftests/bpf/progs/syscall.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/progs/syscall.c +3 -3
@@ -76,9 +76,9 @@ static int btf_load(void) .magic = BTF_MAGIC, .version = BTF_VERSION, .hdr_len = sizeof(struct btf_header), - .type_len = sizeof(__u32) * 8, - .str_off = sizeof(__u32) * 8, - .str_len = sizeof(__u32), + .type_len = sizeof(raw_btf.types), + .str_off = offsetof(struct btf_blob, str) - offsetof(struct btf_blob, types), + .str_len = sizeof(raw_btf.str), }, .types = {/* long */
Signature · 1 line
2.34.1