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

bpf: Use struct_size()

Use struct_size() instead of hand writing it.
This is less verbose and more robust.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/bpf/20240429121323.3818497-1-andriy.shevchenko@linux.intel.com

authored by

Andy Shevchenko and committed by
Andrii Nakryiko
cb01621b 397658dd

+8 -6
+8 -6
kernel/bpf/core.c
··· 26 26 #include <linux/bpf.h> 27 27 #include <linux/btf.h> 28 28 #include <linux/objtool.h> 29 + #include <linux/overflow.h> 29 30 #include <linux/rbtree_latch.h> 30 31 #include <linux/kallsyms.h> 31 32 #include <linux/rcupdate.h> ··· 2456 2455 2457 2456 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags) 2458 2457 { 2459 - if (prog_cnt) 2460 - return kzalloc(sizeof(struct bpf_prog_array) + 2461 - sizeof(struct bpf_prog_array_item) * 2462 - (prog_cnt + 1), 2463 - flags); 2458 + struct bpf_prog_array *p; 2464 2459 2465 - return &bpf_empty_prog_array.hdr; 2460 + if (prog_cnt) 2461 + p = kzalloc(struct_size(p, items, prog_cnt + 1), flags); 2462 + else 2463 + p = &bpf_empty_prog_array.hdr; 2464 + 2465 + return p; 2466 2466 } 2467 2467 2468 2468 void bpf_prog_array_free(struct bpf_prog_array *progs)