Skip to content

Commit 6aefdf4

Browse files
mykyta5Kernel Patches Daemon
authored andcommitted
bpf: refactor special field-type detection
Reduce code duplication in detection of the known special field types in map values. This refactoring helps to avoid copying a chunk of code in the next patch of the series. Signed-off-by: Mykyta Yatsenko <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Acked-by: Andrii Nakryiko <[email protected]>
1 parent e2bfc81 commit 6aefdf4

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

kernel/bpf/btf.c

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3488,44 +3488,28 @@ static int btf_get_field_type(const struct btf *btf, const struct btf_type *var_
34883488
u32 field_mask, u32 *seen_mask,
34893489
int *align, int *sz)
34903490
{
3491-
int type = 0;
3491+
const struct {
3492+
enum btf_field_type type;
3493+
const char *const name;
3494+
} field_types[] = { { BPF_SPIN_LOCK, "bpf_spin_lock" },
3495+
{ BPF_RES_SPIN_LOCK, "bpf_res_spin_lock" },
3496+
{ BPF_TIMER, "bpf_timer" },
3497+
{ BPF_WORKQUEUE, "bpf_wq" }};
3498+
int type = 0, i;
34923499
const char *name = __btf_name_by_offset(btf, var_type->name_off);
3500+
const char *field_type_name;
3501+
enum btf_field_type field_type;
34933502

3494-
if (field_mask & BPF_SPIN_LOCK) {
3495-
if (!strcmp(name, "bpf_spin_lock")) {
3496-
if (*seen_mask & BPF_SPIN_LOCK)
3497-
return -E2BIG;
3498-
*seen_mask |= BPF_SPIN_LOCK;
3499-
type = BPF_SPIN_LOCK;
3500-
goto end;
3501-
}
3502-
}
3503-
if (field_mask & BPF_RES_SPIN_LOCK) {
3504-
if (!strcmp(name, "bpf_res_spin_lock")) {
3505-
if (*seen_mask & BPF_RES_SPIN_LOCK)
3506-
return -E2BIG;
3507-
*seen_mask |= BPF_RES_SPIN_LOCK;
3508-
type = BPF_RES_SPIN_LOCK;
3509-
goto end;
3510-
}
3511-
}
3512-
if (field_mask & BPF_TIMER) {
3513-
if (!strcmp(name, "bpf_timer")) {
3514-
if (*seen_mask & BPF_TIMER)
3515-
return -E2BIG;
3516-
*seen_mask |= BPF_TIMER;
3517-
type = BPF_TIMER;
3518-
goto end;
3519-
}
3520-
}
3521-
if (field_mask & BPF_WORKQUEUE) {
3522-
if (!strcmp(name, "bpf_wq")) {
3523-
if (*seen_mask & BPF_WORKQUEUE)
3524-
return -E2BIG;
3525-
*seen_mask |= BPF_WORKQUEUE;
3526-
type = BPF_WORKQUEUE;
3527-
goto end;
3528-
}
3503+
for (i = 0; i < ARRAY_SIZE(field_types); ++i) {
3504+
field_type = field_types[i].type;
3505+
field_type_name = field_types[i].name;
3506+
if (!(field_mask & field_type) || strcmp(name, field_type_name))
3507+
continue;
3508+
if (*seen_mask & field_type)
3509+
return -E2BIG;
3510+
*seen_mask |= field_type;
3511+
type = field_type;
3512+
goto end;
35293513
}
35303514
field_mask_test_name(BPF_LIST_HEAD, "bpf_list_head");
35313515
field_mask_test_name(BPF_LIST_NODE, "bpf_list_node");

0 commit comments

Comments
 (0)