Skip to content

Commit 750c30d

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
bpf: Introduce internal check_map_flags helper function
It is to unify map flags checking for lookup, update, lookup_batch and update_batch. Therefore, it will be convenient to check BPF_F_CPU flag in this helper function for them in next patch. Signed-off-by: Leon Hwang <[email protected]>
1 parent 07f2c0d commit 750c30d

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

kernel/bpf/syscall.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,17 @@ static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
16541654
return NULL;
16551655
}
16561656

1657+
static int check_map_flags(struct bpf_map *map, u64 flags, bool check_flag)
1658+
{
1659+
if (check_flag && (flags & ~BPF_F_LOCK))
1660+
return -EINVAL;
1661+
1662+
if ((flags & BPF_F_LOCK) && !btf_record_has_field(map->record, BPF_SPIN_LOCK))
1663+
return -EINVAL;
1664+
1665+
return 0;
1666+
}
1667+
16571668
/* last field in 'union bpf_attr' used by this command */
16581669
#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
16591670

@@ -1669,19 +1680,16 @@ static int map_lookup_elem(union bpf_attr *attr)
16691680
if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
16701681
return -EINVAL;
16711682

1672-
if (attr->flags & ~BPF_F_LOCK)
1673-
return -EINVAL;
1674-
16751683
CLASS(fd, f)(attr->map_fd);
16761684
map = __bpf_map_get(f);
16771685
if (IS_ERR(map))
16781686
return PTR_ERR(map);
16791687
if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ))
16801688
return -EPERM;
16811689

1682-
if ((attr->flags & BPF_F_LOCK) &&
1683-
!btf_record_has_field(map->record, BPF_SPIN_LOCK))
1684-
return -EINVAL;
1690+
err = check_map_flags(map, attr->flags, true);
1691+
if (err)
1692+
return err;
16851693

16861694
key = __bpf_copy_key(ukey, map->key_size);
16871695
if (IS_ERR(key))
@@ -1744,11 +1752,9 @@ static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
17441752
goto err_put;
17451753
}
17461754

1747-
if ((attr->flags & BPF_F_LOCK) &&
1748-
!btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1749-
err = -EINVAL;
1755+
err = check_map_flags(map, attr->flags, false);
1756+
if (err)
17501757
goto err_put;
1751-
}
17521758

17531759
key = ___bpf_copy_key(ukey, map->key_size);
17541760
if (IS_ERR(key)) {
@@ -1952,13 +1958,9 @@ int generic_map_update_batch(struct bpf_map *map, struct file *map_file,
19521958
void *key, *value;
19531959
int err = 0;
19541960

1955-
if (attr->batch.elem_flags & ~BPF_F_LOCK)
1956-
return -EINVAL;
1957-
1958-
if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1959-
!btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1960-
return -EINVAL;
1961-
}
1961+
err = check_map_flags(map, attr->batch.elem_flags, true);
1962+
if (err)
1963+
return err;
19621964

19631965
value_size = bpf_map_value_size(map);
19641966

@@ -2015,12 +2017,9 @@ int generic_map_lookup_batch(struct bpf_map *map,
20152017
u32 value_size, cp, max_count;
20162018
int err;
20172019

2018-
if (attr->batch.elem_flags & ~BPF_F_LOCK)
2019-
return -EINVAL;
2020-
2021-
if ((attr->batch.elem_flags & BPF_F_LOCK) &&
2022-
!btf_record_has_field(map->record, BPF_SPIN_LOCK))
2023-
return -EINVAL;
2020+
err = check_map_flags(map, attr->batch.elem_flags, true);
2021+
if (err)
2022+
return err;
20242023

20252024
value_size = bpf_map_value_size(map);
20262025

0 commit comments

Comments
 (0)