Skip to content

Commit 4586ea6

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
bpf: Introduce internal bpf_map_check_op_flags helper function
It is to unify map flags checking for lookup_elem, update_elem, lookup_batch and update_batch APIs. Therefore, it will be convenient to check BPF_F_CPU and BPF_F_ALL_CPUS flags in it for these APIs in next patch. Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Leon Hwang <[email protected]>
1 parent 7abc661 commit 4586ea6

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

include/linux/bpf.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3768,4 +3768,15 @@ int bpf_prog_get_file_line(struct bpf_prog *prog, unsigned long ip, const char *
37683768
const char **linep, int *nump);
37693769
struct bpf_prog *bpf_prog_find_from_stack(void);
37703770

3771+
static inline int bpf_map_check_op_flags(struct bpf_map *map, u64 flags, u64 allowed_flags)
3772+
{
3773+
if (flags & ~allowed_flags)
3774+
return -EINVAL;
3775+
3776+
if ((flags & BPF_F_LOCK) && !btf_record_has_field(map->record, BPF_SPIN_LOCK))
3777+
return -EINVAL;
3778+
3779+
return 0;
3780+
}
3781+
37713782
#endif /* _LINUX_BPF_H */

kernel/bpf/syscall.c

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,19 +1709,16 @@ static int map_lookup_elem(union bpf_attr *attr)
17091709
if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
17101710
return -EINVAL;
17111711

1712-
if (attr->flags & ~BPF_F_LOCK)
1713-
return -EINVAL;
1714-
17151712
CLASS(fd, f)(attr->map_fd);
17161713
map = __bpf_map_get(f);
17171714
if (IS_ERR(map))
17181715
return PTR_ERR(map);
17191716
if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ))
17201717
return -EPERM;
17211718

1722-
if ((attr->flags & BPF_F_LOCK) &&
1723-
!btf_record_has_field(map->record, BPF_SPIN_LOCK))
1724-
return -EINVAL;
1719+
err = bpf_map_check_op_flags(map, attr->flags, BPF_F_LOCK);
1720+
if (err)
1721+
return err;
17251722

17261723
key = __bpf_copy_key(ukey, map->key_size);
17271724
if (IS_ERR(key))
@@ -1784,11 +1781,9 @@ static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
17841781
goto err_put;
17851782
}
17861783

1787-
if ((attr->flags & BPF_F_LOCK) &&
1788-
!btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1789-
err = -EINVAL;
1784+
err = bpf_map_check_op_flags(map, attr->flags, ~0);
1785+
if (err)
17901786
goto err_put;
1791-
}
17921787

17931788
key = ___bpf_copy_key(ukey, map->key_size);
17941789
if (IS_ERR(key)) {
@@ -1992,13 +1987,9 @@ int generic_map_update_batch(struct bpf_map *map, struct file *map_file,
19921987
void *key, *value;
19931988
int err = 0;
19941989

1995-
if (attr->batch.elem_flags & ~BPF_F_LOCK)
1996-
return -EINVAL;
1997-
1998-
if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1999-
!btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
2000-
return -EINVAL;
2001-
}
1990+
err = bpf_map_check_op_flags(map, attr->batch.elem_flags, BPF_F_LOCK);
1991+
if (err)
1992+
return err;
20021993

20031994
value_size = bpf_map_value_size(map);
20041995

@@ -2055,12 +2046,9 @@ int generic_map_lookup_batch(struct bpf_map *map,
20552046
u32 value_size, cp, max_count;
20562047
int err;
20572048

2058-
if (attr->batch.elem_flags & ~BPF_F_LOCK)
2059-
return -EINVAL;
2060-
2061-
if ((attr->batch.elem_flags & BPF_F_LOCK) &&
2062-
!btf_record_has_field(map->record, BPF_SPIN_LOCK))
2063-
return -EINVAL;
2049+
err = bpf_map_check_op_flags(map, attr->batch.elem_flags, BPF_F_LOCK);
2050+
if (err)
2051+
return err;
20642052

20652053
value_size = bpf_map_value_size(map);
20662054

0 commit comments

Comments
 (0)