Skip to content

Commit 310794c

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-some-fixes-for-nullness-elision'
Daniel Xu says: ==================== Tow fixes for nullness elision. Changes from v1: * Reword commit message in patch 1 * Add tags ==================== Link: https://patch.msgid.link/cover.1738689872.git.dxu@dxuuu.xyz Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 517e8a7 + 7968c65 commit 310794c

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

kernel/bpf/verifier.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9149,10 +9149,11 @@ static int check_reg_const_str(struct bpf_verifier_env *env,
91499149
return 0;
91509150
}
91519151

9152-
/* Returns constant key value if possible, else negative error */
9153-
static s64 get_constant_map_key(struct bpf_verifier_env *env,
9152+
/* Returns constant key value in `value` if possible, else negative error */
9153+
static int get_constant_map_key(struct bpf_verifier_env *env,
91549154
struct bpf_reg_state *key,
9155-
u32 key_size)
9155+
u32 key_size,
9156+
s64 *value)
91569157
{
91579158
struct bpf_func_state *state = func(env, key);
91589159
struct bpf_reg_state *reg;
@@ -9179,8 +9180,10 @@ static s64 get_constant_map_key(struct bpf_verifier_env *env,
91799180
/* First handle precisely tracked STACK_ZERO */
91809181
for (i = off; i >= 0 && stype[i] == STACK_ZERO; i--)
91819182
zero_size++;
9182-
if (zero_size >= key_size)
9183+
if (zero_size >= key_size) {
9184+
*value = 0;
91839185
return 0;
9186+
}
91849187

91859188
/* Check that stack contains a scalar spill of expected size */
91869189
if (!is_spilled_scalar_reg(&state->stack[spi]))
@@ -9203,9 +9206,12 @@ static s64 get_constant_map_key(struct bpf_verifier_env *env,
92039206
if (err < 0)
92049207
return err;
92059208

9206-
return reg->var_off.value;
9209+
*value = reg->var_off.value;
9210+
return 0;
92079211
}
92089212

9213+
static bool can_elide_value_nullness(enum bpf_map_type type);
9214+
92099215
static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
92109216
struct bpf_call_arg_meta *meta,
92119217
const struct bpf_func_proto *fn,
@@ -9354,9 +9360,16 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
93549360
err = check_helper_mem_access(env, regno, key_size, BPF_READ, false, NULL);
93559361
if (err)
93569362
return err;
9357-
meta->const_map_key = get_constant_map_key(env, reg, key_size);
9358-
if (meta->const_map_key < 0 && meta->const_map_key != -EOPNOTSUPP)
9359-
return meta->const_map_key;
9363+
if (can_elide_value_nullness(meta->map_ptr->map_type)) {
9364+
err = get_constant_map_key(env, reg, key_size, &meta->const_map_key);
9365+
if (err < 0) {
9366+
meta->const_map_key = -1;
9367+
if (err == -EOPNOTSUPP)
9368+
err = 0;
9369+
else
9370+
return err;
9371+
}
9372+
}
93609373
break;
93619374
case ARG_PTR_TO_MAP_VALUE:
93629375
if (type_may_be_null(arg_type) && register_is_null(reg))

tools/testing/selftests/bpf/progs/verifier_array_access.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,4 +713,19 @@ unsigned int non_stack_key_lookup(void)
713713
return val->index;
714714
}
715715

716+
SEC("socket")
717+
__description("doesn't reject UINT64_MAX as s64 for irrelevant maps")
718+
__success __retval(42)
719+
unsigned int doesnt_reject_irrelevant_maps(void)
720+
{
721+
__u64 key = 0xFFFFFFFFFFFFFFFF;
722+
struct test_val *val;
723+
724+
val = bpf_map_lookup_elem(&map_hash_48b, &key);
725+
if (val)
726+
return val->index;
727+
728+
return 42;
729+
}
730+
716731
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)