Skip to content

Commit 8f6ddc0

Browse files
AsphalttAlexei Starovoitov
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. Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Leon Hwang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 8c868a3 commit 8f6ddc0

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
@@ -3829,4 +3829,15 @@ bpf_prog_update_insn_ptrs(struct bpf_prog *prog, u32 *offsets, void *image)
38293829
}
38303830
#endif
38313831

3832+
static inline int bpf_map_check_op_flags(struct bpf_map *map, u64 flags, u64 allowed_flags)
3833+
{
3834+
if (flags & ~allowed_flags)
3835+
return -EINVAL;
3836+
3837+
if ((flags & BPF_F_LOCK) && !btf_record_has_field(map->record, BPF_SPIN_LOCK))
3838+
return -EINVAL;
3839+
3840+
return 0;
3841+
}
3842+
38323843
#endif /* _LINUX_BPF_H */

kernel/bpf/syscall.c

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

1728-
if (attr->flags & ~BPF_F_LOCK)
1729-
return -EINVAL;
1730-
17311728
CLASS(fd, f)(attr->map_fd);
17321729
map = __bpf_map_get(f);
17331730
if (IS_ERR(map))
17341731
return PTR_ERR(map);
17351732
if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ))
17361733
return -EPERM;
17371734

1738-
if ((attr->flags & BPF_F_LOCK) &&
1739-
!btf_record_has_field(map->record, BPF_SPIN_LOCK))
1740-
return -EINVAL;
1735+
err = bpf_map_check_op_flags(map, attr->flags, BPF_F_LOCK);
1736+
if (err)
1737+
return err;
17411738

17421739
key = __bpf_copy_key(ukey, map->key_size);
17431740
if (IS_ERR(key))
@@ -1800,11 +1797,9 @@ static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
18001797
goto err_put;
18011798
}
18021799

1803-
if ((attr->flags & BPF_F_LOCK) &&
1804-
!btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1805-
err = -EINVAL;
1800+
err = bpf_map_check_op_flags(map, attr->flags, ~0);
1801+
if (err)
18061802
goto err_put;
1807-
}
18081803

18091804
key = ___bpf_copy_key(ukey, map->key_size);
18101805
if (IS_ERR(key)) {
@@ -2008,13 +2003,9 @@ int generic_map_update_batch(struct bpf_map *map, struct file *map_file,
20082003
void *key, *value;
20092004
int err = 0;
20102005

2011-
if (attr->batch.elem_flags & ~BPF_F_LOCK)
2012-
return -EINVAL;
2013-
2014-
if ((attr->batch.elem_flags & BPF_F_LOCK) &&
2015-
!btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
2016-
return -EINVAL;
2017-
}
2006+
err = bpf_map_check_op_flags(map, attr->batch.elem_flags, BPF_F_LOCK);
2007+
if (err)
2008+
return err;
20182009

20192010
value_size = bpf_map_value_size(map);
20202011

@@ -2071,12 +2062,9 @@ int generic_map_lookup_batch(struct bpf_map *map,
20712062
u32 value_size, cp, max_count;
20722063
int err;
20732064

2074-
if (attr->batch.elem_flags & ~BPF_F_LOCK)
2075-
return -EINVAL;
2076-
2077-
if ((attr->batch.elem_flags & BPF_F_LOCK) &&
2078-
!btf_record_has_field(map->record, BPF_SPIN_LOCK))
2079-
return -EINVAL;
2065+
err = bpf_map_check_op_flags(map, attr->batch.elem_flags, BPF_F_LOCK);
2066+
if (err)
2067+
return err;
20802068

20812069
value_size = bpf_map_value_size(map);
20822070

0 commit comments

Comments
 (0)