bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 1/1] bpf: Use struct_size()
@ 2024-04-29 12:13 Andy Shevchenko
  2024-04-29 17:08 ` Yonghong Song
  2024-04-29 23:20 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 3+ messages in thread
From: Andy Shevchenko @ 2024-04-29 12:13 UTC (permalink / raw)
  To: Alexei Starovoitov, bpf, linux-kernel
  Cc: Daniel Borkmann, John Fastabend, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Andy Shevchenko

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>
---
 kernel/bpf/core.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 778775bdbb2e..6047979d5be6 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -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)
-- 
2.43.0.rc1.1336.g36b5255a03ac


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v1 1/1] bpf: Use struct_size()
  2024-04-29 12:13 [PATCH v1 1/1] bpf: Use struct_size() Andy Shevchenko
@ 2024-04-29 17:08 ` Yonghong Song
  2024-04-29 23:20 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: Yonghong Song @ 2024-04-29 17:08 UTC (permalink / raw)
  To: Andy Shevchenko, Alexei Starovoitov, bpf, linux-kernel
  Cc: Daniel Borkmann, John Fastabend, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, KP Singh,
	Stanislav Fomichev, Hao Luo, Jiri Olsa


On 4/29/24 5:13 AM, Andy Shevchenko wrote:
> 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>

Acked-by: Yonghong Song <yonghong.song@linux.dev>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v1 1/1] bpf: Use struct_size()
  2024-04-29 12:13 [PATCH v1 1/1] bpf: Use struct_size() Andy Shevchenko
  2024-04-29 17:08 ` Yonghong Song
@ 2024-04-29 23:20 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-04-29 23:20 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: ast, bpf, linux-kernel, daniel, john.fastabend, andrii,
	martin.lau, eddyz87, song, yonghong.song, kpsingh, sdf, haoluo,
	jolsa

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

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.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  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!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-04-29 23:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-29 12:13 [PATCH v1 1/1] bpf: Use struct_size() Andy Shevchenko
2024-04-29 17:08 ` Yonghong Song
2024-04-29 23:20 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).