Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <andrii@kernel.org>
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://lore.kernel.org/r/20251125145857.98134-2-leon.hwang@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Leon Hwang and committed by
Alexei Starovoitov
8f6ddc05 8c868a34

+22 -23
+11
include/linux/bpf.h
··· 3829 3829 } 3830 3830 #endif 3831 3831 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 + 3832 3843 #endif /* _LINUX_BPF_H */
+11 -23
kernel/bpf/syscall.c
··· 1725 1725 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) 1726 1726 return -EINVAL; 1727 1727 1728 - if (attr->flags & ~BPF_F_LOCK) 1729 - return -EINVAL; 1730 - 1731 1728 CLASS(fd, f)(attr->map_fd); 1732 1729 map = __bpf_map_get(f); 1733 1730 if (IS_ERR(map)) ··· 1732 1735 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) 1733 1736 return -EPERM; 1734 1737 1735 - if ((attr->flags & BPF_F_LOCK) && 1736 - !btf_record_has_field(map->record, BPF_SPIN_LOCK)) 1737 - return -EINVAL; 1738 + err = bpf_map_check_op_flags(map, attr->flags, BPF_F_LOCK); 1739 + if (err) 1740 + return err; 1738 1741 1739 1742 key = __bpf_copy_key(ukey, map->key_size); 1740 1743 if (IS_ERR(key)) ··· 1797 1800 goto err_put; 1798 1801 } 1799 1802 1800 - if ((attr->flags & BPF_F_LOCK) && 1801 - !btf_record_has_field(map->record, BPF_SPIN_LOCK)) { 1802 - err = -EINVAL; 1803 + err = bpf_map_check_op_flags(map, attr->flags, ~0); 1804 + if (err) 1803 1805 goto err_put; 1804 - } 1805 1806 1806 1807 key = ___bpf_copy_key(ukey, map->key_size); 1807 1808 if (IS_ERR(key)) { ··· 2003 2008 void *key, *value; 2004 2009 int err = 0; 2005 2010 2006 - if (attr->batch.elem_flags & ~BPF_F_LOCK) 2007 - return -EINVAL; 2008 - 2009 - if ((attr->batch.elem_flags & BPF_F_LOCK) && 2010 - !btf_record_has_field(map->record, BPF_SPIN_LOCK)) { 2011 - return -EINVAL; 2012 - } 2011 + err = bpf_map_check_op_flags(map, attr->batch.elem_flags, BPF_F_LOCK); 2012 + if (err) 2013 + return err; 2013 2014 2014 2015 value_size = bpf_map_value_size(map); 2015 2016 ··· 2062 2071 u32 value_size, cp, max_count; 2063 2072 int err; 2064 2073 2065 - if (attr->batch.elem_flags & ~BPF_F_LOCK) 2066 - return -EINVAL; 2067 - 2068 - if ((attr->batch.elem_flags & BPF_F_LOCK) && 2069 - !btf_record_has_field(map->record, BPF_SPIN_LOCK)) 2070 - return -EINVAL; 2074 + err = bpf_map_check_op_flags(map, attr->batch.elem_flags, BPF_F_LOCK); 2075 + if (err) 2076 + return err; 2071 2077 2072 2078 value_size = bpf_map_value_size(map); 2073 2079