Remove ARG_CONST_SIZE_OR_ZERO and use flag to mark that the argument may be zero. Signed-off-by: Hao Luo <haoluo@xxxxxxxxxx> --- include/linux/bpf.h | 2 - kernel/bpf/helpers.c | 20 +++++++-- kernel/bpf/ringbuf.c | 5 ++- kernel/bpf/stackmap.c | 15 +++++-- kernel/bpf/verifier.c | 8 ++-- kernel/trace/bpf_trace.c | 90 ++++++++++++++++++++++++++++++++-------- net/core/filter.c | 35 ++++++++++++---- 7 files changed, 135 insertions(+), 40 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index dd92418814b5..27f81989f992 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -323,8 +323,6 @@ struct bpf_arg_type { */ ARG_CONST_SIZE, /* number of bytes accessed from memory */ - ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */ - ARG_PTR_TO_CTX, /* pointer to context */ ARG_PTR_TO_CTX_OR_NULL, /* pointer to context or NULL */ ARG_ANYTHING, /* any (initialized) argument is ok */ diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index cd792777afb2..082a8620f666 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -631,7 +631,10 @@ const struct bpf_func_proto bpf_event_output_data_proto = { .arg2 = { .type = ARG_CONST_MAP_PTR }, .arg3 = { .type = ARG_ANYTHING }, .arg4 = { .type = ARG_PTR_TO_MEM }, - .arg5 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg5 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, }; BPF_CALL_3(bpf_copy_from_user, void *, dst, u32, size, @@ -652,7 +655,10 @@ const struct bpf_func_proto bpf_copy_from_user_proto = { .gpl_only = false, .ret_type = RET_INTEGER, .arg1 = { .type = ARG_PTR_TO_UNINIT_MEM }, - .arg2 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg2 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg3 = { .type = ARG_ANYTHING }, }; @@ -1012,13 +1018,19 @@ const struct bpf_func_proto bpf_snprintf_proto = { .type = ARG_PTR_TO_MEM, .flag = ARG_FLAG_MAYBE_NULL, }, - .arg2 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg2 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg3 = { .type = ARG_PTR_TO_CONST_STR }, .arg4 = { .type = ARG_PTR_TO_MEM, .flag = ARG_FLAG_MAYBE_NULL, }, - .arg5 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg5 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, }; /* BPF map elements can contain 'struct bpf_timer'. diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c index bf29de9c4a2d..a8af9c7c6423 100644 --- a/kernel/bpf/ringbuf.c +++ b/kernel/bpf/ringbuf.c @@ -445,7 +445,10 @@ const struct bpf_func_proto bpf_ringbuf_output_proto = { .ret_type = RET_INTEGER, .arg1 = { .type = ARG_CONST_MAP_PTR }, .arg2 = { .type = ARG_PTR_TO_MEM }, - .arg3 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg3 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg4 = { .type = ARG_ANYTHING }, }; diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index ae7bfc278443..496baa1b8def 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -464,7 +464,10 @@ const struct bpf_func_proto bpf_get_stack_proto = { .ret_type = RET_INTEGER, .arg1 = { .type = ARG_PTR_TO_CTX }, .arg2 = { .type = ARG_PTR_TO_UNINIT_MEM }, - .arg3 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg3 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg4 = { .type = ARG_ANYTHING }, }; @@ -491,7 +494,10 @@ const struct bpf_func_proto bpf_get_task_stack_proto = { .arg1 = { .type = ARG_PTR_TO_BTF_ID }, .arg1_btf_id = &btf_task_struct_ids[0], .arg2 = { .type = ARG_PTR_TO_UNINIT_MEM }, - .arg3 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg3 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg4 = { .type = ARG_ANYTHING }, }; @@ -554,7 +560,10 @@ const struct bpf_func_proto bpf_get_stack_proto_pe = { .ret_type = RET_INTEGER, .arg1 = { .type = ARG_PTR_TO_CTX }, .arg2 = { .type = ARG_PTR_TO_UNINIT_MEM }, - .arg3 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg3 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg4 = { .type = ARG_ANYTHING }, }; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index eb69b8bddee5..1a4830ad2be2 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -2493,7 +2493,7 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, * r5 += 1 * ... * call bpf_perf_event_output#25 - * where .arg5 = ARG_CONST_SIZE_OR_ZERO + * where .arg5 = ARG_CONST_SIZE * * and this case: * r6 = 1 @@ -4955,8 +4955,7 @@ static bool arg_type_is_mem_ptr(struct bpf_arg_type arg) static bool arg_type_is_mem_size(struct bpf_arg_type arg) { - return arg.type == ARG_CONST_SIZE || - arg.type == ARG_CONST_SIZE_OR_ZERO; + return arg.type == ARG_CONST_SIZE; } static bool arg_type_is_alloc_size(struct bpf_arg_type arg) @@ -5088,7 +5087,6 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, [ARG_CONST_SIZE] = &scalar_types, - [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, [ARG_CONST_MAP_PTR] = &const_map_ptr_types, [ARG_PTR_TO_CTX] = &context_types, @@ -5326,7 +5324,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg_num, */ meta->raw_mode = (arg.type == ARG_PTR_TO_UNINIT_MEM); } else if (arg_type_is_mem_size(arg)) { - bool zero_size_allowed = (arg.type == ARG_CONST_SIZE_OR_ZERO); + bool zero_size_allowed = (arg.flag & ARG_FLAG_MAYBE_NULL); /* This is used to refine r0 return value bounds for helpers * that enforce this value as an upper bound on return values. diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 086889a29ca7..7d6b51ed3292 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -171,7 +171,10 @@ const struct bpf_func_proto bpf_probe_read_user_proto = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1 = { .type = ARG_PTR_TO_UNINIT_MEM }, - .arg2 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg2 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg3 = { .type = ARG_ANYTHING }, }; @@ -208,7 +211,10 @@ const struct bpf_func_proto bpf_probe_read_user_str_proto = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1 = { .type = ARG_PTR_TO_UNINIT_MEM }, - .arg2 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg2 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg3 = { .type = ARG_ANYTHING }, }; @@ -234,7 +240,10 @@ const struct bpf_func_proto bpf_probe_read_kernel_proto = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1 = { .type = ARG_PTR_TO_UNINIT_MEM }, - .arg2 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg2 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg3 = { .type = ARG_ANYTHING }, }; @@ -269,7 +278,10 @@ const struct bpf_func_proto bpf_probe_read_kernel_str_proto = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1 = { .type = ARG_PTR_TO_UNINIT_MEM }, - .arg2 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg2 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg3 = { .type = ARG_ANYTHING }, }; @@ -289,7 +301,10 @@ static const struct bpf_func_proto bpf_probe_read_compat_proto = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1 = { .type = ARG_PTR_TO_UNINIT_MEM }, - .arg2 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg2 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg3 = { .type = ARG_ANYTHING }, }; @@ -308,7 +323,10 @@ static const struct bpf_func_proto bpf_probe_read_compat_str_proto = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1 = { .type = ARG_PTR_TO_UNINIT_MEM }, - .arg2 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg2 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg3 = { .type = ARG_ANYTHING }, }; #endif /* CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE */ @@ -456,7 +474,10 @@ static const struct bpf_func_proto bpf_trace_vprintk_proto = { .type = ARG_PTR_TO_MEM, .flag = ARG_FLAG_MAYBE_NULL, }, - .arg4 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg4 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, }; const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void) @@ -501,7 +522,10 @@ static const struct bpf_func_proto bpf_seq_printf_proto = { .type = ARG_PTR_TO_MEM, .flag = ARG_FLAG_MAYBE_NULL, }, - .arg5 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg5 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, }; BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len) @@ -516,7 +540,10 @@ static const struct bpf_func_proto bpf_seq_write_proto = { .arg1 = { .type = ARG_PTR_TO_BTF_ID }, .arg1_btf_id = &btf_seq_file_ids[0], .arg2 = { .type = ARG_PTR_TO_MEM }, - .arg3 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg3 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, }; BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr, @@ -540,7 +567,10 @@ static const struct bpf_func_proto bpf_seq_printf_btf_proto = { .arg1 = { .type = ARG_PTR_TO_BTF_ID }, .arg1_btf_id = &btf_seq_file_ids[0], .arg2 = { .type = ARG_PTR_TO_MEM }, - .arg3 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg3 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg4 = { .type = ARG_ANYTHING }, }; @@ -701,7 +731,10 @@ static const struct bpf_func_proto bpf_perf_event_output_proto = { .arg2 = { .type = ARG_CONST_MAP_PTR }, .arg3 = { .type = ARG_ANYTHING }, .arg4 = { .type = ARG_PTR_TO_MEM }, - .arg5 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg5 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, }; static DEFINE_PER_CPU(int, bpf_event_output_nest_level); @@ -952,7 +985,10 @@ static const struct bpf_func_proto bpf_d_path_proto = { .arg1 = { .type = ARG_PTR_TO_BTF_ID }, .arg1_btf_id = &bpf_d_path_btf_ids[0], .arg2 = { .type = ARG_PTR_TO_MEM }, - .arg3 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg3 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .allowed = bpf_d_path_allowed, }; @@ -1094,7 +1130,10 @@ static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1 = { .type = ARG_PTR_TO_UNINIT_MEM }, - .arg2 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg2 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, }; static const struct bpf_func_proto * @@ -1296,7 +1335,10 @@ static const struct bpf_func_proto bpf_perf_event_output_proto_tp = { .arg2 = { .type = ARG_CONST_MAP_PTR }, .arg3 = { .type = ARG_ANYTHING }, .arg4 = { .type = ARG_PTR_TO_MEM }, - .arg5 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg5 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, }; BPF_CALL_3(bpf_get_stackid_tp, void *, tp_buff, struct bpf_map *, map, @@ -1337,7 +1379,10 @@ static const struct bpf_func_proto bpf_get_stack_proto_tp = { .ret_type = RET_INTEGER, .arg1 = { .type = ARG_PTR_TO_CTX }, .arg2 = { .type = ARG_PTR_TO_UNINIT_MEM }, - .arg3 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg3 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg4 = { .type = ARG_ANYTHING }, }; @@ -1445,7 +1490,10 @@ static const struct bpf_func_proto bpf_read_branch_records_proto = { .type = ARG_PTR_TO_MEM, .flag = ARG_FLAG_MAYBE_NULL, }, - .arg3 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg3 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg4 = { .type = ARG_ANYTHING }, }; @@ -1525,7 +1573,10 @@ static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = { .arg2 = { .type = ARG_CONST_MAP_PTR }, .arg3 = { .type = ARG_ANYTHING }, .arg4 = { .type = ARG_PTR_TO_MEM }, - .arg5 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg5 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, }; extern const struct bpf_func_proto bpf_skb_output_proto; @@ -1579,7 +1630,10 @@ static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { .ret_type = RET_INTEGER, .arg1 = { .type = ARG_PTR_TO_CTX }, .arg2 = { .type = ARG_PTR_TO_MEM }, - .arg3 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg3 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg4 = { .type = ARG_ANYTHING }, }; diff --git a/net/core/filter.c b/net/core/filter.c index 81c6638ffc2a..5e0f726a9bcd 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -2022,12 +2022,18 @@ static const struct bpf_func_proto bpf_csum_diff_proto = { .type = ARG_PTR_TO_MEM, .flag = ARG_FLAG_MAYBE_NULL, }, - .arg2 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg2 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg3 = { .type = ARG_PTR_TO_MEM, .flag = ARG_FLAG_MAYBE_NULL, }, - .arg4 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg4 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg5 = { .type = ARG_ANYTHING }, }; @@ -2551,7 +2557,10 @@ static const struct bpf_func_proto bpf_redirect_neigh_proto = { .type = ARG_PTR_TO_MEM, .flag = ARG_FLAG_MAYBE_NULL, }, - .arg3 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg3 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, .arg4 = { .type = ARG_ANYTHING }, }; @@ -4184,7 +4193,10 @@ static const struct bpf_func_proto bpf_skb_event_output_proto = { .arg2 = { .type = ARG_CONST_MAP_PTR }, .arg3 = { .type = ARG_ANYTHING }, .arg4 = { .type = ARG_PTR_TO_MEM }, - .arg5 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg5 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, }; BTF_ID_LIST_SINGLE(bpf_skb_output_btf_ids, struct, sk_buff) @@ -4198,7 +4210,10 @@ const struct bpf_func_proto bpf_skb_output_proto = { .arg2 = { .type = ARG_CONST_MAP_PTR }, .arg3 = { .type = ARG_ANYTHING }, .arg4 = { .type = ARG_PTR_TO_MEM }, - .arg5 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg5 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, }; static unsigned short bpf_tunnel_key_af(u64 flags) @@ -4577,7 +4592,10 @@ static const struct bpf_func_proto bpf_xdp_event_output_proto = { .arg2 = { .type = ARG_CONST_MAP_PTR }, .arg3 = { .type = ARG_ANYTHING }, .arg4 = { .type = ARG_PTR_TO_MEM }, - .arg5 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg5 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, }; BTF_ID_LIST_SINGLE(bpf_xdp_output_btf_ids, struct, xdp_buff) @@ -4591,7 +4609,10 @@ const struct bpf_func_proto bpf_xdp_output_proto = { .arg2 = { .type = ARG_CONST_MAP_PTR }, .arg3 = { .type = ARG_ANYTHING }, .arg4 = { .type = ARG_PTR_TO_MEM }, - .arg5 = { .type = ARG_CONST_SIZE_OR_ZERO }, + .arg5 = { + .type = ARG_CONST_SIZE, + .flag = ARG_FLAG_MAYBE_NULL, + }, }; BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb) -- 2.34.0.rc0.344.g81b53c2807-goog