Skip to content

Commit 2d3ee75

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 d3f58a7 commit 2d3ee75

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
@@ -1708,19 +1708,16 @@ static int map_lookup_elem(union bpf_attr *attr)
17081708
if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
17091709
return -EINVAL;
17101710

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

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

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

1786-
if ((attr->flags & BPF_F_LOCK) &&
1787-
!btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1788-
err = -EINVAL;
1783+
err = bpf_map_check_op_flags(map, attr->flags, ~0);
1784+
if (err)
17891785
goto err_put;
1790-
}
17911786

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

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

20021993
value_size = bpf_map_value_size(map);
20031994

@@ -2054,12 +2045,9 @@ int generic_map_lookup_batch(struct bpf_map *map,
20542045
u32 value_size, cp, max_count;
20552046
int err;
20562047

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

20642052
value_size = bpf_map_value_size(map);
20652053

0 commit comments

Comments
 (0)