fix BTF verification size resolution
Subsystems (5)
v2 cover fix BTF verification size resolution
BTF size resolution logic isn't always resolving type size correctly, leading to erroneous map creation failures due to value size mismatch.
This patch set:
1. fixes the issue (patch #1);
2. adds tests for trickier cases (patch #2);
3. and converts few test cases utilizing BTF-defined maps, that previously couldn't use typedef'ed arrays due to kernel bug (patch #3).
Patch #1 can be applied against bpf tree, but selftest ones (#2 and #3) have to go against bpf-next for now.
Andrii Nakryiko (3):
bpf: fix BTF verifier size resolution logic selftests/bpf: add trickier size resolution tests selftests/bpf: use typedef'ed arrays as map values
kernel/bpf/btf.c | 14 ++- .../bpf/progs/test_get_stack_rawtp.c | 3 +- .../bpf/progs/test_stacktrace_build_id.c | 3 +- .../selftests/bpf/progs/test_stacktrace_map.c | 2 +- tools/testing/selftests/bpf/test_btf.c | 88 +++++++++++++++++++ 5 files changed, 102 insertions(+), 8 deletions(-)
Signature · 1 line
2.17.1
PATCH v2 1/3 bpf: fix BTF verifier size resolution logic
BTF verifier has a size resolution bug which in some circumstances leads to invalid size resolution for, e.g., TYPEDEF modifier. This happens if we have
[1] PTR -> [2] TYPEDEF -> [3] ARRAY, in which case due to being in pointer context ARRAY size won't be resolved (because for pointer it doesn't matter, so it's a sink in pointer context), but it will be permanently remembered as zero for TYPEDEF and TYPEDEF will be marked as RESOLVED. Eventually ARRAY size will be resolved correctly, but TYPEDEF resolved_size won't be updated anymore. This, subsequently, will lead to erroneous map creation failure, if that TYPEDEF is specified as either key or value, as key_size/value_size won't correspond to resolved size of TYPEDEF (kernel will believe it's zero).
Note, that if BTF was ordered as [1] ARRAY <- [2] TYPEDEF <- [3] PTR, this won't be a problem, as by the time we get to TYPEDEF, ARRAY's size is already calculated and stored.
This bug manifests itself in rejecting BTF-defined maps that use array typedef as a value type:
typedef int array_t[16];
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(value, array_t); /* i.e., array_t *value; */
} test_map SEC(".maps");The fix consists on not relying on modifier's resolved_size and instead using modifier's resolved_id (type ID for "concrete" type to which modifier eventually resolves) and doing size determination for that resolved type. This allow to preserve existing "early DFS termination" logic for PTR or STRUCT_OR_ARRAY contexts, but still do correct size determination for modifier types.
--- kernel/bpf/btf.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
kernel/bpf/btf.c +10 -4
@@ -1073,11 +1073,18 @@ const struct btf_type *btf_type_id_size(const struct btf *btf, !btf_type_is_var(size_type)))return NULL ; - size = btf->resolved_sizes[size_type_id]; size_type_id = btf->resolved_ids[size_type_id]; size_type =btf_type_by_id (btf, size_type_id);if (btf_type_nosize_or_null (size_type))return NULL ; +else if (btf_type_has_size (size_type)) + size = size_type->size; +else if (btf_type_is_array (size_type)) + size = btf->resolved_sizes[size_type_id]; +else if (btf_type_is_ptr (size_type)) + size =sizeof (void *); +else +return NULL ; } *type_id = size_type_id;
@@ -1602,7 +1609,6 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,const struct btf_type *next_type;u32 next_type_id = t->type;struct btf *btf = env->btf; -u32 next_type_size =0 ; next_type =btf_type_by_id (btf, next_type_id);if (!next_type ||btf_type_is_resolve_source_only (next_type)) {
@@ -1620,7 +1626,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,* save us a few type-following when we use it later (e.g. in * pretty print). */ -if (!btf_type_id_size (btf, &next_type_id, &next_type_size)) { +if (!btf_type_id_size (btf, &next_type_id,NULL )) {if (env_type_is_resolved (env, next_type_id)) next_type =btf_type_id_resolve (btf, &next_type_id);
@@ -1633,7 +1639,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env, } } -env_stack_pop_resolved (env, next_type_id, next_type_size); +env_stack_pop_resolved (env, next_type_id,0 );return 0 ; }
Signature · 1 line
2.17.1
Re: [PATCH v2 bpf-next 1/3] bpf: fix BTF verifier size resolution logic
Quoted 50 lines · Andrii Nakryiko
BTF verifier has a size resolution bug which in some circumstances leads to invalid size resolution for, e.g., TYPEDEF modifier. This happens if we have
[1] PTR -> [2] TYPEDEF -> [3] ARRAY, in which case due to being in pointer context ARRAY size won't be resolved (because for pointer it doesn't matter, so it's a sink in pointer context), but it will be permanently remembered as zero for TYPEDEF and TYPEDEF will be marked as RESOLVED. Eventually ARRAY size will be resolved correctly, but TYPEDEFresolved_sizewon't be updated anymore. This, subsequently, will lead to erroneous map creation failure, if that TYPEDEF is specified as either key or value, as key_size/value_size won't correspond to resolved size of TYPEDEF (kernel will believe it's zero).Note, that if BTF was ordered as [1] ARRAY <- [2] TYPEDEF <- [3] PTR, this won't be a problem, as by the time we get to TYPEDEF, ARRAY's size is already calculated and stored.
This bug manifests itself in rejecting BTF-defined maps that use array typedef as a value type:
typedef int array_t[16];
struct { __uint(type, BPF_MAP_TYPE_ARRAY); __type(value, array_t); /* i.e., array_t *value; */ } test_map SEC(".maps");The fix consists on not relying on modifier's
resolved_sizeand instead using modifier'sresolved_id(type ID for "concrete" type to which modifier eventually resolves) and doing size determination for that resolved type. This allow to preserve existing "early DFS termination" logic for PTR orSTRUCT_OR_ARRAYcontexts, but still do correct size determination for modifier types.Fixes eb3f595dab40 ("bpf: btf: Validate type reference")Cc Martin KaFai Lau <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>--- kernel/bpf/btf.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)kernel/bpf/btf.c +0
-1@@ -1073,11 +1073,18 @@ const struct btf_type *btf_type_id_size(const struct btf *btf, !btf_type_is_var(size_type)))return NULL ; - size = btf->resolved_sizes[size_type_id]; size_type_id = btf->resolved_ids[size_type_id]; size_type =btf_type_by_id (btf, size_type_id);
kernel/bpf/btf.c const struct btf_type *btf_type_id_size(const struct btf *btf, · line 1087
Quoted 10 lines
kernel/bpf/btf.c +8
-0if (btf_type_nosize_or_null (size_type))return NULL ; +else if (btf_type_has_size (size_type)) + size = size_type->size; +else if (btf_type_is_array (size_type)) + size = btf->resolved_sizes[size_type_id]; +else if (btf_type_is_ptr (size_type)) + size =sizeof (void *); +else +return NULL ;
Looks good to me. Not sure whether we need to do any adjustment for var kind or not. Maybe we can do similar change in btf_var_resolve() to btf_modifier_resolve()? But I do not think it impacts correctness similar to btf_modifier_resolve() below as you changed btf_type_id_size() implementation in the above.
Quoted 30 lines
kernel/bpf/btf.c +2
-3} *type_id = size_type_id;@@ -1602,7 +1609,6 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,const struct btf_type *next_type;u32 next_type_id = t->type;struct btf *btf = env->btf; -u32 next_type_size =0 ; next_type =btf_type_by_id (btf, next_type_id);if (!next_type ||btf_type_is_resolve_source_only (next_type)) {@@ -1620,7 +1626,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,* save us a few type-following when we use it later (e.g. in * pretty print). */ -if (!btf_type_id_size (btf, &next_type_id, &next_type_size)) { +if (!btf_type_id_size (btf, &next_type_id,NULL )) {if (env_type_is_resolved (env, next_type_id)) next_type =btf_type_id_resolve (btf, &next_type_id);@@ -1633,7 +1639,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env, } } -env_stack_pop_resolved (env, next_type_id, next_type_size); +env_stack_pop_resolved (env, next_type_id,0 );return 0 ; }
Re: [PATCH v2 bpf-next 1/3] bpf: fix BTF verifier size resolution logic
Quoted 51 lines · Yonghong Song
Quoted 47 lines · Andrii Nakryiko
BTF verifier has a size resolution bug which in some circumstances leads to invalid size resolution for, e.g., TYPEDEF modifier. This happens if we have
[1] PTR -> [2] TYPEDEF -> [3] ARRAY, in which case due to being in pointer context ARRAY size won't be resolved (because for pointer it doesn't matter, so it's a sink in pointer context), but it will be permanently remembered as zero for TYPEDEF and TYPEDEF will be marked as RESOLVED. Eventually ARRAY size will be resolved correctly, but TYPEDEFresolved_sizewon't be updated anymore. This, subsequently, will lead to erroneous map creation failure, if that TYPEDEF is specified as either key or value, as key_size/value_size won't correspond to resolved size of TYPEDEF (kernel will believe it's zero).Note, that if BTF was ordered as [1] ARRAY <- [2] TYPEDEF <- [3] PTR, this won't be a problem, as by the time we get to TYPEDEF, ARRAY's size is already calculated and stored.
This bug manifests itself in rejecting BTF-defined maps that use array typedef as a value type:
typedef int array_t[16];
struct { __uint(type, BPF_MAP_TYPE_ARRAY); __type(value, array_t); /* i.e., array_t *value; */ } test_map SEC(".maps");The fix consists on not relying on modifier's
resolved_sizeand instead using modifier'sresolved_id(type ID for "concrete" type to which modifier eventually resolves) and doing size determination for that resolved type. This allow to preserve existing "early DFS termination" logic for PTR orSTRUCT_OR_ARRAYcontexts, but still do correct size determination for modifier types.Fixes eb3f595dab40 ("bpf: btf: Validate type reference")Cc Martin KaFai Lau <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>--- kernel/bpf/btf.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)kernel/bpf/btf.c +0
-0@@ -1073,11 +1073,18 @@ const struct btf_type *btf_type_id_size(const struct btf *btf, !btf_type_is_var(size_type)))return NULL ;
Quoted 16 lines
kernel/bpf/btf.c const struct btf_type *btf_type_id_size(const struct btf *btf, · line 1087
Quoted 13 lines
kernel/bpf/btf.c +8
-1- size = btf->resolved_sizes[size_type_id]; size_type_id = btf->resolved_ids[size_type_id]; size_type =btf_type_by_id (btf, size_type_id);if (btf_type_nosize_or_null (size_type))return NULL ; +else if (btf_type_has_size (size_type)) + size = size_type->size; +else if (btf_type_is_array (size_type)) + size = btf->resolved_sizes[size_type_id]; +else if (btf_type_is_ptr (size_type)) + size =sizeof (void *); +else +return NULL ;Looks good to me. Not sure whether we need to do any adjustment for var kind or not. Maybe we can do similar change in
btf_var_resolve()
I don't think btf_var_resolve() needs any change. btf_var_resolve can't be referenced by modifier, so it doesn't have any problem. It's similar to array in that it's size will be determined correctly.
But I think btf_type_id_size() doesn't handle var case correctly, I'll do
+ else if (btf_type_is_array(size_type) ||btf_type_is_var(size_type))
+ size = btf->resolved_sizes[size_type_id];
to fix that.
Quoted 34 lines
to
btf_modifier_resolve()? But I do not think it impacts correctness similar tobtf_modifier_resolve()below as you changedbtf_type_id_size()implementation in the above.Quoted 30 lines
kernel/bpf/btf.c +2
-3} *type_id = size_type_id;@@ -1602,7 +1609,6 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,const struct btf_type *next_type;u32 next_type_id = t->type;struct btf *btf = env->btf; -u32 next_type_size =0 ; next_type =btf_type_by_id (btf, next_type_id);if (!next_type ||btf_type_is_resolve_source_only (next_type)) {@@ -1620,7 +1626,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,* save us a few type-following when we use it later (e.g. in * pretty print). */ -if (!btf_type_id_size (btf, &next_type_id, &next_type_size)) { +if (!btf_type_id_size (btf, &next_type_id,NULL )) {if (env_type_is_resolved (env, next_type_id)) next_type =btf_type_id_resolve (btf, &next_type_id);@@ -1633,7 +1639,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env, } } -env_stack_pop_resolved (env, next_type_id, next_type_size); +env_stack_pop_resolved (env, next_type_id,0 );return 0 ; }
Re: [PATCH v2 bpf-next 1/3] bpf: fix BTF verifier size resolution logic
Quoted 69 lines · Andrii Nakryiko
Quoted 51 lines · Yonghong Song
Quoted 47 lines · Andrii Nakryiko
BTF verifier has a size resolution bug which in some circumstances leads to invalid size resolution for, e.g., TYPEDEF modifier. This happens if we have
[1] PTR -> [2] TYPEDEF -> [3] ARRAY, in which case due to being in pointer context ARRAY size won't be resolved (because for pointer it doesn't matter, so it's a sink in pointer context), but it will be permanently remembered as zero for TYPEDEF and TYPEDEF will be marked as RESOLVED. Eventually ARRAY size will be resolved correctly, but TYPEDEFresolved_sizewon't be updated anymore. This, subsequently, will lead to erroneous map creation failure, if that TYPEDEF is specified as either key or value, as key_size/value_size won't correspond to resolved size of TYPEDEF (kernel will believe it's zero).Note, that if BTF was ordered as [1] ARRAY <- [2] TYPEDEF <- [3] PTR, this won't be a problem, as by the time we get to TYPEDEF, ARRAY's size is already calculated and stored.
This bug manifests itself in rejecting BTF-defined maps that use array typedef as a value type:
typedef int array_t[16];
struct { __uint(type, BPF_MAP_TYPE_ARRAY); __type(value, array_t); /* i.e., array_t *value; */ } test_map SEC(".maps");The fix consists on not relying on modifier's
resolved_sizeand instead using modifier'sresolved_id(type ID for "concrete" type to which modifier eventually resolves) and doing size determination for that resolved type. This allow to preserve existing "early DFS termination" logic for PTR orSTRUCT_OR_ARRAYcontexts, but still do correct size determination for modifier types.Fixes eb3f595dab40 ("bpf: btf: Validate type reference")Cc Martin KaFai Lau <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>--- kernel/bpf/btf.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)kernel/bpf/btf.c +0
-0@@ -1073,11 +1073,18 @@ const struct btf_type *btf_type_id_size(const struct btf *btf, !btf_type_is_var(size_type)))return NULL ;Quoted 16 lines
kernel/bpf/btf.c const struct btf_type *btf_type_id_size(const struct btf *btf, · line 1087
Quoted 13 lines
kernel/bpf/btf.c +8
-1- size = btf->resolved_sizes[size_type_id]; size_type_id = btf->resolved_ids[size_type_id]; size_type =btf_type_by_id (btf, size_type_id);if (btf_type_nosize_or_null (size_type))return NULL ; +else if (btf_type_has_size (size_type)) + size = size_type->size; +else if (btf_type_is_array (size_type)) + size = btf->resolved_sizes[size_type_id]; +else if (btf_type_is_ptr (size_type)) + size =sizeof (void *); +else +return NULL ;Looks good to me. Not sure whether we need to do any adjustment for var kind or not. Maybe we can do similar change in
btf_var_resolve()
Quoted 3 lines
I don't think
btf_var_resolve()needs any change.btf_var_resolvecan't be referenced by modifier, so it doesn't have any problem. It's similar to array in that it's size will be determined correctly.
Correct. With your previous patch, the resolved_sizes[..] for var type is not used, so that is why I suggest to just set it to 0.
Quoted 6 lines
But I think
btf_type_id_size()doesn't handle var case correctly, I'll do+ else if (
btf_type_is_array(size_type)||btf_type_is_var(size_type))
+ size = btf->resolved_sizes[size_type_id];
This change should work too (to use btf->resolved_sizes[...]).
Quoted 37 lines
to fix that.
Quoted 34 lines
to
btf_modifier_resolve()? But I do not think it impacts correctness similar tobtf_modifier_resolve()below as you changedbtf_type_id_size()implementation in the above.Quoted 30 lines
kernel/bpf/btf.c +2
-3} *type_id = size_type_id;@@ -1602,7 +1609,6 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,const struct btf_type *next_type;u32 next_type_id = t->type;struct btf *btf = env->btf; -u32 next_type_size =0 ; next_type =btf_type_by_id (btf, next_type_id);if (!next_type ||btf_type_is_resolve_source_only (next_type)) {@@ -1620,7 +1626,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,* save us a few type-following when we use it later (e.g. in * pretty print). */ -if (!btf_type_id_size (btf, &next_type_id, &next_type_size)) { +if (!btf_type_id_size (btf, &next_type_id,NULL )) {if (env_type_is_resolved (env, next_type_id)) next_type =btf_type_id_resolve (btf, &next_type_id);@@ -1633,7 +1639,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env, } } -env_stack_pop_resolved (env, next_type_id, next_type_size); +env_stack_pop_resolved (env, next_type_id,0 );return 0 ; }
Re: [PATCH v2 bpf-next 1/3] bpf: fix BTF verifier size resolution logic
Quoted 73 lines · Yonghong Song
Quoted 69 lines · Andrii Nakryiko
Quoted 67 lines · Yonghong Song
Quoted 50 lines · Andrii Nakryiko
BTF verifier has a size resolution bug which in some circumstances leads to invalid size resolution for, e.g., TYPEDEF modifier. This happens if we have
[1] PTR -> [2] TYPEDEF -> [3] ARRAY, in which case due to being in pointer context ARRAY size won't be resolved (because for pointer it doesn't matter, so it's a sink in pointer context), but it will be permanently remembered as zero for TYPEDEF and TYPEDEF will be marked as RESOLVED. Eventually ARRAY size will be resolved correctly, but TYPEDEFresolved_sizewon't be updated anymore. This, subsequently, will lead to erroneous map creation failure, if that TYPEDEF is specified as either key or value, as key_size/value_size won't correspond to resolved size of TYPEDEF (kernel will believe it's zero).Note, that if BTF was ordered as [1] ARRAY <- [2] TYPEDEF <- [3] PTR, this won't be a problem, as by the time we get to TYPEDEF, ARRAY's size is already calculated and stored.
This bug manifests itself in rejecting BTF-defined maps that use array typedef as a value type:
typedef int array_t[16];
struct { __uint(type, BPF_MAP_TYPE_ARRAY); __type(value, array_t); /* i.e., array_t *value; */ } test_map SEC(".maps");The fix consists on not relying on modifier's
resolved_sizeand instead using modifier'sresolved_id(type ID for "concrete" type to which modifier eventually resolves) and doing size determination for that resolved type. This allow to preserve existing "early DFS termination" logic for PTR orSTRUCT_OR_ARRAYcontexts, but still do correct size determination for modifier types.Fixes eb3f595dab40 ("bpf: btf: Validate type reference")Cc Martin KaFai Lau <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>--- kernel/bpf/btf.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)kernel/bpf/btf.c +0
-1@@ -1073,11 +1073,18 @@ const struct btf_type *btf_type_id_size(const struct btf *btf, !btf_type_is_var(size_type)))return NULL ; - size = btf->resolved_sizes[size_type_id]; size_type_id = btf->resolved_ids[size_type_id]; size_type =btf_type_by_id (btf, size_type_id);kernel/bpf/btf.c const struct btf_type *btf_type_id_size(const struct btf *btf, · line 1087
Quoted 10 lines
kernel/bpf/btf.c +8
-0if (btf_type_nosize_or_null (size_type))return NULL ; +else if (btf_type_has_size (size_type)) + size = size_type->size; +else if (btf_type_is_array (size_type)) + size = btf->resolved_sizes[size_type_id]; +else if (btf_type_is_ptr (size_type)) + size =sizeof (void *); +else +return NULL ;Looks good to me. Not sure whether we need to do any adjustment for var kind or not. Maybe we can do similar change in
btf_var_resolve()
Quoted 6 lines
Quoted 3 lines
I don't think
btf_var_resolve()needs any change.btf_var_resolvecan't be referenced by modifier, so it doesn't have any problem. It's similar to array in that it's size will be determined correctly.Correct. With your previous patch, the resolved_sizes[..] for var type is not used, so that is why I suggest to just set it to 0.
Ah, sorry, I misunderstood your initial suggestion. Yes, resolved_sizes for VAR is not used anymore, so yeah, I'll set it to zero.
Quoted 9 lines
Quoted 7 lines
But I think
btf_type_id_size()doesn't handle var case correctly, I'll do+ else if (
btf_type_is_array(size_type)||btf_type_is_var(size_type))
+ size = btf->resolved_sizes[size_type_id];This change should work too (to use btf->resolved_sizes[...]).
Reading code again, VAR's size is handled similar to modifier's size, so I won't change btf_type_id_size().
Posting v3 soon.
Quoted 38 lines
Quoted 38 lines
to fix that.
Quoted 34 lines
to
btf_modifier_resolve()? But I do not think it impacts correctness similar tobtf_modifier_resolve()below as you changedbtf_type_id_size()implementation in the above.Quoted 30 lines
kernel/bpf/btf.c +2
-3} *type_id = size_type_id;@@ -1602,7 +1609,6 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,const struct btf_type *next_type;u32 next_type_id = t->type;struct btf *btf = env->btf; -u32 next_type_size =0 ; next_type =btf_type_by_id (btf, next_type_id);if (!next_type ||btf_type_is_resolve_source_only (next_type)) {@@ -1620,7 +1626,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,* save us a few type-following when we use it later (e.g. in * pretty print). */ -if (!btf_type_id_size (btf, &next_type_id, &next_type_size)) { +if (!btf_type_id_size (btf, &next_type_id,NULL )) {if (env_type_is_resolved (env, next_type_id)) next_type =btf_type_id_resolve (btf, &next_type_id);@@ -1633,7 +1639,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env, } } -env_stack_pop_resolved (env, next_type_id, next_type_size); +env_stack_pop_resolved (env, next_type_id,0 );return 0 ; }
PATCH v2 2/3 selftests/bpf: add trickier size resolution tests
In reply to Andrii NakryikoAdd more BTF tests, validating that size resolution logic is correct in few trickier cases.
--- tools/testing/selftests/bpf/test_btf.c | 88 ++++++++++++++++++++++++++ 1 file changed, 88 insertions(+)
tools/testing/selftests/bpf/test_btf.c +88 -0
@@ -3417,6 +3417,94 @@ static struct btf_raw_test raw_tests[] = { .value_type_id = 1, .max_entries = 4, }, +/* +* typedef int arr_t[16]; +* struct s { +* arr_t *a; +* }; +*/ +{ + .descr = "struct->ptr->typedef->array->int size resolution", + .raw_types = { + BTF_STRUCT_ENC(NAME_TBD, 1, 8), /* [1] */ + BTF_MEMBER_ENC(NAME_TBD, 2, 0), + BTF_PTR_ENC(3), /* [2] */ + BTF_TYPEDEF_ENC(NAME_TBD, 4), /* [3] */ + BTF_TYPE_ARRAY_ENC(5, 5, 16), /* [4] */ + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [5] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0s\0a\0arr_t"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "ptr_mod_chain_size_resolve_map", + .key_size = sizeof(int), + .value_size = sizeof(int) * 16, + .key_type_id = 5 /* int */, + .value_type_id = 3 /* arr_t */, + .max_entries = 4, +}, +/* + * typedef int arr_t[16][8][4]; + * struct s { + * arr_t *a; + * }; + */ +{ + .descr = "struct->ptr->typedef->multi-array->int size resolution", + .raw_types = { + BTF_STRUCT_ENC(NAME_TBD, 1, 8), /* [1] */ + BTF_MEMBER_ENC(NAME_TBD, 2, 0), + BTF_PTR_ENC(3), /* [2] */ + BTF_TYPEDEF_ENC(NAME_TBD, 4), /* [3] */ + BTF_TYPE_ARRAY_ENC(5, 7, 16), /* [4] */ + BTF_TYPE_ARRAY_ENC(6, 7, 8), /* [5] */ + BTF_TYPE_ARRAY_ENC(7, 7, 4), /* [6] */ + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [7] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0s\0a\0arr_t"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "multi_arr_size_resolve_map", + .key_size = sizeof(int), + .value_size = sizeof(int) * 16 * 8 * 4, + .key_type_id = 7 /* int */, + .value_type_id = 3 /* arr_t */, + .max_entries = 4, +}, +/* + * typedef int int_t; + * typedef int_t arr3_t[4]; + * typedef arr3_t arr2_t[8]; + * typedef arr2_t arr1_t[16]; + * struct s { + * arr1_t *a; + * }; + */ +{ + .descr = "typedef/multi-arr mix size resolution", + .raw_types = { + BTF_STRUCT_ENC(NAME_TBD, 1, 8), /* [1] */ + BTF_MEMBER_ENC(NAME_TBD, 2, 0), + BTF_PTR_ENC(3), /* [2] */ + BTF_TYPEDEF_ENC(NAME_TBD, 4), /* [3] */ + BTF_TYPE_ARRAY_ENC(5, 10, 16), /* [4] */ + BTF_TYPEDEF_ENC(NAME_TBD, 6), /* [5] */ + BTF_TYPE_ARRAY_ENC(7, 10, 8), /* [6] */ + BTF_TYPEDEF_ENC(NAME_TBD, 8), /* [7] */ + BTF_TYPE_ARRAY_ENC(9, 10, 4), /* [8] */ + BTF_TYPEDEF_ENC(NAME_TBD, 10), /* [9] */ + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [10] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0s\0a\0arr1_t\0arr2_t\0arr3_t\0int_t"), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "typedef_arra_mix_size_resolve_map", + .key_size = sizeof(int), + .value_size = sizeof(int) * 16 * 8 * 4, + .key_type_id = 10 /* int */, + .value_type_id = 3 /* arr_t */, + .max_entries = 4, +}, }; /* struct btf_raw_test raw_tests[] */
Signature · 1 line
2.17.1
PATCH v2 3/3 selftests/bpf: use typedef'ed arrays as map values
In reply to Andrii NakryikoConvert few tests that couldn't use typedef'ed arrays due to kernel bug.
--- tools/testing/selftests/bpf/progs/test_get_stack_rawtp.c | 3 ++- tools/testing/selftests/bpf/progs/test_stacktrace_build_id.c | 3 +-- tools/testing/selftests/bpf/progs/test_stacktrace_map.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/progs/test_get_stack_rawtp.c +2 -1
@@ -47,11 +47,12 @@ struct {* issue and avoid complicated C programming massaging. * This is an acceptable workaround since there is one entry here. */ +typedef __u64 raw_stack_trace_t [2 *MAX_STACK_RAWTP ];struct { __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); __uint(max_entries, 1); __type(key, __u32); -__u64 (*value)[2 *MAX_STACK_RAWTP ]; + __type(value, raw_stack_trace_t); } rawdata_map SEC(".maps");SEC ("tracepoint/raw_syscalls/sys_enter" )
tools/testing/selftests/bpf/progs/test_stacktrace_build_id.c +1 -2
@@ -36,8 +36,7 @@ struct {__uint (type,BPF_MAP_TYPE_ARRAY );__uint (max_entries,128 );__type (key, __u32); -/* there seems to be a bug in kernel not handling typedef properly */ -struct bpf_stack_build_id (*value)[PERF_MAX_STACK_DEPTH ]; +__type (value, stack_trace_t); } stack_amap SEC(".maps");/* taken from /sys/kernel/debug/tracing/events/random/urandom_read/format */
tools/testing/selftests/bpf/progs/test_stacktrace_map.c +1 -1
@@ -35,7 +35,7 @@ struct {__uint (type,BPF_MAP_TYPE_ARRAY );__uint (max_entries,16384 );__type (key, __u32); -__u64 (*value)[PERF_MAX_STACK_DEPTH ]; +__type (value, stack_trace_t); } stack_amap SEC(".maps");/* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */
Signature · 1 line
2.17.1
Quoted 11 lines · Andrii Nakryiko
BTF size resolution logic isn't always resolving type size correctly, leading to erroneous map creation failures due to value size mismatch.
This patch set:
1. fixes the issue (patch #1);
2. adds tests for trickier cases (patch #2);
3. and converts few test cases utilizing BTF-defined maps, that previously couldn't use typedef'ed arrays due to kernel bug (patch #3).Patch #1 can be applied against bpf tree, but selftest ones (#2 and #3) have to go against bpf-next for now.
Why #2 and #3 have to go to bpf-next? bpf tree also accepts tests, AFAIK. Maybe leave for Daniel and Alexei to decide in this particular case.
Quoted 5 lines
Andrii Nakryiko (3):
bpf: fix BTF verifier size resolution logic selftests/bpf: add trickier size resolution tests selftests/bpf: use typedef'ed arrays as map values
Looks good to me. Except minor comments in patch 1/3, Ack the series.
Quoted 8 lines
kernel/bpf/btf.c | 14 ++- .../bpf/progs/test_get_stack_rawtp.c | 3 +- .../bpf/progs/test_stacktrace_build_id.c | 3 +- .../selftests/bpf/progs/test_stacktrace_map.c | 2 +- tools/testing/selftests/bpf/test_btf.c | 88 +++++++++++++++++++ 5 files changed, 102 insertions(+), 8 deletions(-)
Daniel Borkmann
maintainerQuoted 10 lines · Yonghong Song
Quoted 9 lines · Andrii Nakryiko
BTF size resolution logic isn't always resolving type size correctly, leading to erroneous map creation failures due to value size mismatch.
This patch set:
1. fixes the issue (patch #1);
2. adds tests for trickier cases (patch #2);
3. and converts few test cases utilizing BTF-defined maps, that previously couldn't use typedef'ed arrays due to kernel bug (patch #3).
Quoted 5 lines
Quoted 2 lines
Patch #1 can be applied against bpf tree, but selftest ones (#2 and #3) have to go against bpf-next for now.
Why #2 and #3 have to go to bpf-next? bpf tree also accepts tests, AFAIK. Maybe leave for Daniel and Alexei to decide in this particular case.
Yes, corresponding test cases for fixes are also accepted for bpf tree, thanks.
Quoted 16 lines
Quoted 4 lines
Andrii Nakryiko (3):
bpf: fix BTF verifier size resolution logic selftests/bpf: add trickier size resolution tests selftests/bpf: use typedef'ed arrays as map valuesLooks good to me. Except minor comments in patch 1/3, Ack the series.
Acked-by Yonghong Song <[email protected]>Quoted 8 lines
kernel/bpf/btf.c | 14 ++- .../bpf/progs/test_get_stack_rawtp.c | 3 +- .../bpf/progs/test_stacktrace_build_id.c | 3 +- .../selftests/bpf/progs/test_stacktrace_map.c | 2 +- tools/testing/selftests/bpf/test_btf.c | 88 +++++++++++++++++++ 5 files changed, 102 insertions(+), 8 deletions(-)
Quoted 12 lines · Daniel Borkmann
Quoted 10 lines · Yonghong Song
Quoted 9 lines · Andrii Nakryiko
BTF size resolution logic isn't always resolving type size correctly, leading to erroneous map creation failures due to value size mismatch.
This patch set:
1. fixes the issue (patch #1);
2. adds tests for trickier cases (patch #2);
3. and converts few test cases utilizing BTF-defined maps, that previously couldn't use typedef'ed arrays due to kernel bug (patch #3).
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Patch #1 can be applied against bpf tree, but selftest ones (#2 and #3) have to go against bpf-next for now.
Why #2 and #3 have to go to bpf-next? bpf tree also accepts tests, AFAIK. Maybe leave for Daniel and Alexei to decide in this particular case.
Yes, corresponding test cases for fixes are also accepted for bpf tree, thanks.
Thanks for merging, Daniel! My thinking was that at the time I posted patch set, BTF-defined map tests weren't yet merged into bpf, so I assumed it has to go against bpf-next.
Quoted 18 lines
Quoted 17 lines
Quoted 5 lines
Andrii Nakryiko (3):
bpf: fix BTF verifier size resolution logic selftests/bpf: add trickier size resolution tests selftests/bpf: use typedef'ed arrays as map valuesLooks good to me. Except minor comments in patch 1/3, Ack the series.
Acked-by Yonghong Song <[email protected]>Quoted 8 lines
kernel/bpf/btf.c | 14 ++- .../bpf/progs/test_get_stack_rawtp.c | 3 +- .../bpf/progs/test_stacktrace_build_id.c | 3 +- .../selftests/bpf/progs/test_stacktrace_map.c | 2 +- tools/testing/selftests/bpf/test_btf.c | 88 +++++++++++++++++++ 5 files changed, 102 insertions(+), 8 deletions(-)
Daniel Borkmann
maintainerQuoted 15 lines · Andrii Nakryiko
Quoted 14 lines · Daniel Borkmann
Quoted 13 lines · Yonghong Song
Quoted 11 lines · Andrii Nakryiko
BTF size resolution logic isn't always resolving type size correctly, leading to erroneous map creation failures due to value size mismatch.
This patch set:
1. fixes the issue (patch #1);
2. adds tests for trickier cases (patch #2);
3. and converts few test cases utilizing BTF-defined maps, that previously couldn't use typedef'ed arrays due to kernel bug (patch #3).Patch #1 can be applied against bpf tree, but selftest ones (#2 and #3) have to go against bpf-next for now.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Why #2 and #3 have to go to bpf-next? bpf tree also accepts tests, AFAIK. Maybe leave for Daniel and Alexei to decide in this particular case.
Yes, corresponding test cases for fixes are also accepted for bpf tree, thanks.
Thanks for merging, Daniel! My thinking was that at the time I posted patch set, BTF-defined map tests weren't yet merged into bpf, so I assumed it has to go against bpf-next.
Not yet merged given the minor change needed resulting from Yonghong's feedback.
Thanks,
Daniel