Skip to content

Commit c25b2ae

Browse files
haoluo1022Alexei Starovoitov
authored andcommitted
bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL
We have introduced a new type to make bpf_reg composable, by allocating bits in the type to represent flags. One of the flags is PTR_MAYBE_NULL which indicates a pointer may be NULL. This patch switches the qualified reg_types to use this flag. The reg_types changed in this patch include: 1. PTR_TO_MAP_VALUE_OR_NULL 2. PTR_TO_SOCKET_OR_NULL 3. PTR_TO_SOCK_COMMON_OR_NULL 4. PTR_TO_TCP_SOCK_OR_NULL 5. PTR_TO_BTF_ID_OR_NULL 6. PTR_TO_MEM_OR_NULL 7. PTR_TO_RDONLY_BUF_OR_NULL 8. PTR_TO_RDWR_BUF_OR_NULL Signed-off-by: Hao Luo <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 3c48073 commit c25b2ae

File tree

7 files changed

+147
-188
lines changed

7 files changed

+147
-188
lines changed

include/linux/bpf.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -465,18 +465,15 @@ enum bpf_reg_type {
465465
PTR_TO_CTX, /* reg points to bpf_context */
466466
CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
467467
PTR_TO_MAP_VALUE, /* reg points to map element value */
468-
PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
468+
PTR_TO_MAP_KEY, /* reg points to a map element key */
469469
PTR_TO_STACK, /* reg == frame_pointer + offset */
470470
PTR_TO_PACKET_META, /* skb->data - meta_len */
471471
PTR_TO_PACKET, /* reg points to skb->data */
472472
PTR_TO_PACKET_END, /* skb->data + headlen */
473473
PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
474474
PTR_TO_SOCKET, /* reg points to struct bpf_sock */
475-
PTR_TO_SOCKET_OR_NULL, /* reg points to struct bpf_sock or NULL */
476475
PTR_TO_SOCK_COMMON, /* reg points to sock_common */
477-
PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */
478476
PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */
479-
PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */
480477
PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
481478
PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
482479
/* PTR_TO_BTF_ID points to a kernel struct that does not need
@@ -494,18 +491,21 @@ enum bpf_reg_type {
494491
* been checked for null. Used primarily to inform the verifier
495492
* an explicit null check is required for this struct.
496493
*/
497-
PTR_TO_BTF_ID_OR_NULL,
498494
PTR_TO_MEM, /* reg points to valid memory region */
499-
PTR_TO_MEM_OR_NULL, /* reg points to valid memory region or NULL */
500495
PTR_TO_RDONLY_BUF, /* reg points to a readonly buffer */
501-
PTR_TO_RDONLY_BUF_OR_NULL, /* reg points to a readonly buffer or NULL */
502496
PTR_TO_RDWR_BUF, /* reg points to a read/write buffer */
503-
PTR_TO_RDWR_BUF_OR_NULL, /* reg points to a read/write buffer or NULL */
504497
PTR_TO_PERCPU_BTF_ID, /* reg points to a percpu kernel variable */
505498
PTR_TO_FUNC, /* reg points to a bpf program function */
506-
PTR_TO_MAP_KEY, /* reg points to a map element key */
507499
__BPF_REG_TYPE_MAX,
508500

501+
/* Extended reg_types. */
502+
PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | PTR_TO_MAP_VALUE,
503+
PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | PTR_TO_SOCKET,
504+
PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | PTR_TO_SOCK_COMMON,
505+
PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | PTR_TO_TCP_SOCK,
506+
PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | PTR_TO_BTF_ID,
507+
PTR_TO_MEM_OR_NULL = PTR_MAYBE_NULL | PTR_TO_MEM,
508+
509509
/* This must be the last entry. Its purpose is to ensure the enum is
510510
* wide enough to hold the higher bits reserved for bpf_type_flag.
511511
*/

include/linux/bpf_verifier.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* that converting umax_value to int cannot overflow.
1919
*/
2020
#define BPF_MAX_VAR_SIZ (1 << 29)
21+
/* size of type_str_buf in bpf_verifier. */
22+
#define TYPE_STR_BUF_LEN 64
2123

2224
/* Liveness marks, used for registers and spilled-regs (in stack slots).
2325
* Read marks propagate upwards until they find a write mark; they record that
@@ -484,6 +486,8 @@ struct bpf_verifier_env {
484486
/* Same as scratched_regs but for stack slots */
485487
u64 scratched_stack_slots;
486488
u32 prev_log_len, prev_insn_print_len;
489+
/* buffer used in reg_type_str() to generate reg_type string */
490+
char type_str_buf[TYPE_STR_BUF_LEN];
487491
};
488492

489493
__printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log,

kernel/bpf/btf.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4940,10 +4940,13 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
49404940
/* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */
49414941
for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
49424942
const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
4943+
u32 type, flag;
49434944

4945+
type = base_type(ctx_arg_info->reg_type);
4946+
flag = type_flag(ctx_arg_info->reg_type);
49444947
if (ctx_arg_info->offset == off &&
4945-
(ctx_arg_info->reg_type == PTR_TO_RDONLY_BUF_OR_NULL ||
4946-
ctx_arg_info->reg_type == PTR_TO_RDWR_BUF_OR_NULL)) {
4948+
(type == PTR_TO_RDWR_BUF || type == PTR_TO_RDONLY_BUF) &&
4949+
(flag & PTR_MAYBE_NULL)) {
49474950
info->reg_type = ctx_arg_info->reg_type;
49484951
return true;
49494952
}

kernel/bpf/map_iter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ static const struct bpf_iter_reg bpf_map_elem_reg_info = {
174174
.ctx_arg_info_size = 2,
175175
.ctx_arg_info = {
176176
{ offsetof(struct bpf_iter__bpf_map_elem, key),
177-
PTR_TO_RDONLY_BUF_OR_NULL },
177+
PTR_TO_RDONLY_BUF | PTR_MAYBE_NULL },
178178
{ offsetof(struct bpf_iter__bpf_map_elem, value),
179-
PTR_TO_RDWR_BUF_OR_NULL },
179+
PTR_TO_RDWR_BUF | PTR_MAYBE_NULL },
180180
},
181181
};
182182

0 commit comments

Comments
 (0)