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: Use struct_size() helper

In an effort to avoid open-coded arithmetic in the kernel, use the
struct_size() helper instead of open-coded calculation.

Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://github.com/KSPP/linux/issues/160
Link: https://lore.kernel.org/bpf/20211220113048.2859-1-xiujianfeng@huawei.com

authored by

Xiu Jianfeng and committed by
Andrii Nakryiko
0dd668d2 426b87b1

+2 -7
+1 -2
kernel/bpf/local_storage.c
··· 163 163 return 0; 164 164 } 165 165 166 - new = bpf_map_kmalloc_node(map, sizeof(struct bpf_storage_buffer) + 167 - map->value_size, 166 + new = bpf_map_kmalloc_node(map, struct_size(new, data, map->value_size), 168 167 __GFP_ZERO | GFP_ATOMIC | __GFP_NOWARN, 169 168 map->numa_node); 170 169 if (!new)
+1 -5
kernel/bpf/reuseport_array.c
··· 152 152 { 153 153 int numa_node = bpf_map_attr_numa_node(attr); 154 154 struct reuseport_array *array; 155 - u64 array_size; 156 155 157 156 if (!bpf_capable()) 158 157 return ERR_PTR(-EPERM); 159 158 160 - array_size = sizeof(*array); 161 - array_size += (u64)attr->max_entries * sizeof(struct sock *); 162 - 163 159 /* allocate all map elements and zero-initialize them */ 164 - array = bpf_map_area_alloc(array_size, numa_node); 160 + array = bpf_map_area_alloc(struct_size(array, ptrs, attr->max_entries), numa_node); 165 161 if (!array) 166 162 return ERR_PTR(-ENOMEM); 167 163