Skip to contentSkip to conversation

bpf: Use struct_size()

PATCH Applied Andy Shevchenko 3 participants 3 messages Apr 29, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Use struct_size()

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

---
 kernel/bpf/core.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
kernel/bpf/core.c +8 -6
index 778775bdbb2e..6047979d5be6 100644
@@ -25,6 +25,7 @@
 #include <linux/bpf.h>
 #include <linux/btf.h>
 #include <linux/objtool.h>
+#include <linux/overflow.h>
 #include <linux/rbtree_latch.h>
 #include <linux/kallsyms.h>
 #include <linux/rcupdate.h>
@@ -2455,13 +2456,14 @@ EXPORT_SYMBOL(bpf_empty_prog_array);
 
 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)
 {
-	if (prog_cnt)
-		return kzalloc(sizeof(struct bpf_prog_array) +
-			       sizeof(struct bpf_prog_array_item) *
-			       (prog_cnt + 1),
-			       flags);
+	struct bpf_prog_array *p;
 
-	return &bpf_empty_prog_array.hdr;
+	if (prog_cnt)
+		p = kzalloc(struct_size(p, items, prog_cnt + 1), flags);
+	else
+		p = &bpf_empty_prog_array.hdr;
+
+	return p;
 }
 
 void bpf_prog_array_free(struct bpf_prog_array *progs)
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#2 lore raw
In reply to Andy Shevchenko
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 7 lines · On Mon, 29 Apr 2024 15:13:22 +0300 you wrote:

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

---
 kernel/bpf/core.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

Here is the summary with links:
- [v1,1/1] bpf: Use struct_size()

    https://git.kernel.org/bpf/bpf-next/c/cb01621b6d91

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html