bpf: Extract nullable reg type conversion into a helper function
authorDmitrii Banshchikov <me@ubique.spb.ru>
Fri, 12 Feb 2021 20:56:40 +0000 (00:56 +0400)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 13 Feb 2021 01:37:23 +0000 (17:37 -0800)
Extract conversion from a register's nullable type to a type with a
value. The helper will be used in mark_ptr_not_null_reg().

Signed-off-by: Dmitrii Banshchikov <me@ubique.spb.ru>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210212205642.620788-3-me@ubique.spb.ru
kernel/bpf/verifier.c

index beae700bb56e2ff6fb41d2ce0e3eb4b2cb834a4e..a9f75bd7f8d38b35b46022eeb1010aa16d0d18a6 100644 (file)
@@ -1079,6 +1079,51 @@ static void mark_reg_known_zero(struct bpf_verifier_env *env,
        __mark_reg_known_zero(regs + regno);
 }
 
+static void mark_ptr_not_null_reg(struct bpf_reg_state *reg)
+{
+       switch (reg->type) {
+       case PTR_TO_MAP_VALUE_OR_NULL: {
+               const struct bpf_map *map = reg->map_ptr;
+
+               if (map->inner_map_meta) {
+                       reg->type = CONST_PTR_TO_MAP;
+                       reg->map_ptr = map->inner_map_meta;
+               } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
+                       reg->type = PTR_TO_XDP_SOCK;
+               } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP ||
+                          map->map_type == BPF_MAP_TYPE_SOCKHASH) {
+                       reg->type = PTR_TO_SOCKET;
+               } else {
+                       reg->type = PTR_TO_MAP_VALUE;
+               }
+               break;
+       }
+       case PTR_TO_SOCKET_OR_NULL:
+               reg->type = PTR_TO_SOCKET;
+               break;
+       case PTR_TO_SOCK_COMMON_OR_NULL:
+               reg->type = PTR_TO_SOCK_COMMON;
+               break;
+       case PTR_TO_TCP_SOCK_OR_NULL:
+               reg->type = PTR_TO_TCP_SOCK;
+               break;
+       case PTR_TO_BTF_ID_OR_NULL:
+               reg->type = PTR_TO_BTF_ID;
+               break;
+       case PTR_TO_MEM_OR_NULL:
+               reg->type = PTR_TO_MEM;
+               break;
+       case PTR_TO_RDONLY_BUF_OR_NULL:
+               reg->type = PTR_TO_RDONLY_BUF;
+               break;
+       case PTR_TO_RDWR_BUF_OR_NULL:
+               reg->type = PTR_TO_RDWR_BUF;
+               break;
+       default:
+               WARN_ON("unknown nullable register type");
+       }
+}
+
 static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
 {
        return type_is_pkt_pointer(reg->type);
@@ -7737,43 +7782,19 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state,
                }
                if (is_null) {
                        reg->type = SCALAR_VALUE;
-               } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
-                       const struct bpf_map *map = reg->map_ptr;
-
-                       if (map->inner_map_meta) {
-                               reg->type = CONST_PTR_TO_MAP;
-                               reg->map_ptr = map->inner_map_meta;
-                       } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
-                               reg->type = PTR_TO_XDP_SOCK;
-                       } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP ||
-                                  map->map_type == BPF_MAP_TYPE_SOCKHASH) {
-                               reg->type = PTR_TO_SOCKET;
-                       } else {
-                               reg->type = PTR_TO_MAP_VALUE;
-                       }
-               } else if (reg->type == PTR_TO_SOCKET_OR_NULL) {
-                       reg->type = PTR_TO_SOCKET;
-               } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) {
-                       reg->type = PTR_TO_SOCK_COMMON;
-               } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) {
-                       reg->type = PTR_TO_TCP_SOCK;
-               } else if (reg->type == PTR_TO_BTF_ID_OR_NULL) {
-                       reg->type = PTR_TO_BTF_ID;
-               } else if (reg->type == PTR_TO_MEM_OR_NULL) {
-                       reg->type = PTR_TO_MEM;
-               } else if (reg->type == PTR_TO_RDONLY_BUF_OR_NULL) {
-                       reg->type = PTR_TO_RDONLY_BUF;
-               } else if (reg->type == PTR_TO_RDWR_BUF_OR_NULL) {
-                       reg->type = PTR_TO_RDWR_BUF;
-               }
-               if (is_null) {
                        /* We don't need id and ref_obj_id from this point
                         * onwards anymore, thus we should better reset it,
                         * so that state pruning has chances to take effect.
                         */
                        reg->id = 0;
                        reg->ref_obj_id = 0;
-               } else if (!reg_may_point_to_spin_lock(reg)) {
+
+                       return;
+               }
+
+               mark_ptr_not_null_reg(reg);
+
+               if (!reg_may_point_to_spin_lock(reg)) {
                        /* For not-NULL ptr, reg->ref_obj_id will be reset
                         * in release_reg_references().
                         *