Skip to content

Commit f4d31a3

Browse files
liuhangbinkernel-patches-bot
authored andcommitted
bpf: add a new bpf argument type ARG_CONST_MAP_PTR_OR_NULL
Add a new bpf argument type ARG_CONST_MAP_PTR_OR_NULL which could be used when we want to allow NULL pointer for map parameter. The bpf helper need to take care and check if the map is NULL when use this type. Acked-by: Toke Høiland-Jørgensen <toke@redhat.com> Acked-by: John Fastabend <john.fastabend@gmail.com> Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
1 parent fd448bb commit f4d31a3

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ enum bpf_arg_type {
295295
ARG_CONST_ALLOC_SIZE_OR_ZERO, /* number of allocated bytes requested */
296296
ARG_PTR_TO_BTF_ID_SOCK_COMMON, /* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */
297297
ARG_PTR_TO_PERCPU_BTF_ID, /* pointer to in-kernel percpu type */
298+
ARG_CONST_MAP_PTR_OR_NULL, /* const argument used as pointer to bpf_map or NULL */
298299
__BPF_ARG_TYPE_MAX,
299300
};
300301

kernel/bpf/verifier.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ static bool arg_type_may_be_null(enum bpf_arg_type type)
451451
type == ARG_PTR_TO_MEM_OR_NULL ||
452452
type == ARG_PTR_TO_CTX_OR_NULL ||
453453
type == ARG_PTR_TO_SOCKET_OR_NULL ||
454-
type == ARG_PTR_TO_ALLOC_MEM_OR_NULL;
454+
type == ARG_PTR_TO_ALLOC_MEM_OR_NULL ||
455+
type == ARG_CONST_MAP_PTR_OR_NULL;
455456
}
456457

457458
/* Determine whether the function releases some resources allocated by another
@@ -4444,6 +4445,7 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = {
44444445
[ARG_CONST_SIZE_OR_ZERO] = &scalar_types,
44454446
[ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types,
44464447
[ARG_CONST_MAP_PTR] = &const_map_ptr_types,
4448+
[ARG_CONST_MAP_PTR_OR_NULL] = &const_map_ptr_types,
44474449
[ARG_PTR_TO_CTX] = &context_types,
44484450
[ARG_PTR_TO_CTX_OR_NULL] = &context_types,
44494451
[ARG_PTR_TO_SOCK_COMMON] = &sock_types,
@@ -4589,9 +4591,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
45894591
meta->ref_obj_id = reg->ref_obj_id;
45904592
}
45914593

4592-
if (arg_type == ARG_CONST_MAP_PTR) {
4593-
/* bpf_map_xxx(map_ptr) call: remember that map_ptr */
4594-
meta->map_ptr = reg->map_ptr;
4594+
if (arg_type == ARG_CONST_MAP_PTR ||
4595+
arg_type == ARG_CONST_MAP_PTR_OR_NULL) {
4596+
meta->map_ptr = register_is_null(reg) ? NULL : reg->map_ptr;
45954597
} else if (arg_type == ARG_PTR_TO_MAP_KEY) {
45964598
/* bpf_map_xxx(..., map_ptr, ..., key) call:
45974599
* check that [key, key + map->key_size) are within

0 commit comments

Comments
 (0)