All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] samples: bpf: Replace sizeof(arr)/sizeof(arr[0]) with ARRAY_SIZE
@ 2022-07-12  7:23 xiaolinkui
  2022-07-12 14:15 ` Jiri Olsa
  2022-07-14  4:50 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 3+ messages in thread
From: xiaolinkui @ 2022-07-12  7:23 UTC (permalink / raw)
  To: xiaolinkui, ast, daniel, andrii, martin.lau, song, yhs,
	john.fastabend, kpsingh, sdf, haoluo, jolsa
  Cc: bpf, linux-kernel, Linkui Xiao

From: Linkui Xiao<xiaolinkui@kylinos.cn>

The ARRAY_SIZE macro is more compact and more formal in linux source.

Signed-off-by: Linkui Xiao<xiaolinkui@kylinos.cn>
---
 samples/bpf/fds_example.c          | 3 ++-
 samples/bpf/sock_example.c         | 3 ++-
 samples/bpf/test_cgrp2_attach.c    | 3 ++-
 samples/bpf/test_lru_dist.c        | 2 +-
 samples/bpf/test_map_in_map_user.c | 4 +++-
 samples/bpf/tracex5_user.c         | 3 ++-
 6 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/samples/bpf/fds_example.c b/samples/bpf/fds_example.c
index 16dbf49e0f19..88a26f3ce201 100644
--- a/samples/bpf/fds_example.c
+++ b/samples/bpf/fds_example.c
@@ -17,6 +17,7 @@
 #include <bpf/libbpf.h>
 #include "bpf_insn.h"
 #include "sock_example.h"
+#include "bpf_util.h"
 
 #define BPF_F_PIN	(1 << 0)
 #define BPF_F_GET	(1 << 1)
@@ -52,7 +53,7 @@ static int bpf_prog_create(const char *object)
 		BPF_MOV64_IMM(BPF_REG_0, 1),
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(insns);
 	struct bpf_object *obj;
 	int err;
 
diff --git a/samples/bpf/sock_example.c b/samples/bpf/sock_example.c
index a88f69504c08..5b66f2401b96 100644
--- a/samples/bpf/sock_example.c
+++ b/samples/bpf/sock_example.c
@@ -29,6 +29,7 @@
 #include <bpf/bpf.h>
 #include "bpf_insn.h"
 #include "sock_example.h"
+#include "bpf_util.h"
 
 char bpf_log_buf[BPF_LOG_BUF_SIZE];
 
@@ -58,7 +59,7 @@ static int test_sock(void)
 		BPF_MOV64_IMM(BPF_REG_0, 0), /* r0 = 0 */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 	LIBBPF_OPTS(bpf_prog_load_opts, opts,
 		.log_buf = bpf_log_buf,
 		.log_size = BPF_LOG_BUF_SIZE,
diff --git a/samples/bpf/test_cgrp2_attach.c b/samples/bpf/test_cgrp2_attach.c
index 6d90874b09c3..68ce69457afe 100644
--- a/samples/bpf/test_cgrp2_attach.c
+++ b/samples/bpf/test_cgrp2_attach.c
@@ -31,6 +31,7 @@
 #include <bpf/bpf.h>
 
 #include "bpf_insn.h"
+#include "bpf_util.h"
 
 enum {
 	MAP_KEY_PACKETS,
@@ -70,7 +71,7 @@ static int prog_load(int map_fd, int verdict)
 		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
 		BPF_EXIT_INSN(),
 	};
-	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
+	size_t insns_cnt = ARRAY_SIZE(prog);
 	LIBBPF_OPTS(bpf_prog_load_opts, opts,
 		.log_buf = bpf_log_buf,
 		.log_size = BPF_LOG_BUF_SIZE,
diff --git a/samples/bpf/test_lru_dist.c b/samples/bpf/test_lru_dist.c
index be98ccb4952f..5efb91763d65 100644
--- a/samples/bpf/test_lru_dist.c
+++ b/samples/bpf/test_lru_dist.c
@@ -523,7 +523,7 @@ int main(int argc, char **argv)
 		return -1;
 	}
 
-	for (f = 0; f < sizeof(map_flags) / sizeof(*map_flags); f++) {
+	for (f = 0; f < ARRAY_SIZE(map_flags); f++) {
 		test_lru_loss0(BPF_MAP_TYPE_LRU_HASH, map_flags[f]);
 		test_lru_loss1(BPF_MAP_TYPE_LRU_HASH, map_flags[f]);
 		test_parallel_lru_loss(BPF_MAP_TYPE_LRU_HASH, map_flags[f],
diff --git a/samples/bpf/test_map_in_map_user.c b/samples/bpf/test_map_in_map_user.c
index e8b4cc184ac9..652ec720533d 100644
--- a/samples/bpf/test_map_in_map_user.c
+++ b/samples/bpf/test_map_in_map_user.c
@@ -12,6 +12,8 @@
 #include <bpf/bpf.h>
 #include <bpf/libbpf.h>
 
+#include "bpf_util.h"
+
 static int map_fd[7];
 
 #define PORT_A		(map_fd[0])
@@ -28,7 +30,7 @@ static const char * const test_names[] = {
 	"Hash of Hash",
 };
 
-#define NR_TESTS (sizeof(test_names) / sizeof(*test_names))
+#define NR_TESTS ARRAY_SIZE(test_names)
 
 static void check_map_id(int inner_map_fd, int map_in_map_fd, uint32_t key)
 {
diff --git a/samples/bpf/tracex5_user.c b/samples/bpf/tracex5_user.c
index e910dc265c31..9d7d79f0d47d 100644
--- a/samples/bpf/tracex5_user.c
+++ b/samples/bpf/tracex5_user.c
@@ -8,6 +8,7 @@
 #include <bpf/bpf.h>
 #include <bpf/libbpf.h>
 #include "trace_helpers.h"
+#include "bpf_util.h"
 
 #ifdef __mips__
 #define	MAX_ENTRIES  6000 /* MIPS n64 syscalls start at 5000 */
@@ -24,7 +25,7 @@ static void install_accept_all_seccomp(void)
 		BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
 	};
 	struct sock_fprog prog = {
-		.len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
+		.len = (unsigned short)ARRAY_SIZE(filter),
 		.filter = filter,
 	};
 	if (prctl(PR_SET_SECCOMP, 2, &prog))
-- 
2.17.1


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

* Re: [PATCH] samples: bpf: Replace sizeof(arr)/sizeof(arr[0]) with ARRAY_SIZE
  2022-07-12  7:23 [PATCH] samples: bpf: Replace sizeof(arr)/sizeof(arr[0]) with ARRAY_SIZE xiaolinkui
@ 2022-07-12 14:15 ` Jiri Olsa
  2022-07-14  4:50 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: Jiri Olsa @ 2022-07-12 14:15 UTC (permalink / raw)
  To: xiaolinkui
  Cc: ast, daniel, andrii, martin.lau, song, yhs, john.fastabend,
	kpsingh, sdf, haoluo, bpf, linux-kernel, Linkui Xiao

On Tue, Jul 12, 2022 at 03:23:02PM +0800, xiaolinkui wrote:
> From: Linkui Xiao<xiaolinkui@kylinos.cn>
> 
> The ARRAY_SIZE macro is more compact and more formal in linux source.
> 
> Signed-off-by: Linkui Xiao<xiaolinkui@kylinos.cn>

LGTM

Acked-by: Jiri Olsa <jolsa@kernel.org>

jirka

> ---
>  samples/bpf/fds_example.c          | 3 ++-
>  samples/bpf/sock_example.c         | 3 ++-
>  samples/bpf/test_cgrp2_attach.c    | 3 ++-
>  samples/bpf/test_lru_dist.c        | 2 +-
>  samples/bpf/test_map_in_map_user.c | 4 +++-
>  samples/bpf/tracex5_user.c         | 3 ++-
>  6 files changed, 12 insertions(+), 6 deletions(-)
> 
> diff --git a/samples/bpf/fds_example.c b/samples/bpf/fds_example.c
> index 16dbf49e0f19..88a26f3ce201 100644
> --- a/samples/bpf/fds_example.c
> +++ b/samples/bpf/fds_example.c
> @@ -17,6 +17,7 @@
>  #include <bpf/libbpf.h>
>  #include "bpf_insn.h"
>  #include "sock_example.h"
> +#include "bpf_util.h"
>  
>  #define BPF_F_PIN	(1 << 0)
>  #define BPF_F_GET	(1 << 1)
> @@ -52,7 +53,7 @@ static int bpf_prog_create(const char *object)
>  		BPF_MOV64_IMM(BPF_REG_0, 1),
>  		BPF_EXIT_INSN(),
>  	};
> -	size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn);
> +	size_t insns_cnt = ARRAY_SIZE(insns);
>  	struct bpf_object *obj;
>  	int err;
>  
> diff --git a/samples/bpf/sock_example.c b/samples/bpf/sock_example.c
> index a88f69504c08..5b66f2401b96 100644
> --- a/samples/bpf/sock_example.c
> +++ b/samples/bpf/sock_example.c
> @@ -29,6 +29,7 @@
>  #include <bpf/bpf.h>
>  #include "bpf_insn.h"
>  #include "sock_example.h"
> +#include "bpf_util.h"
>  
>  char bpf_log_buf[BPF_LOG_BUF_SIZE];
>  
> @@ -58,7 +59,7 @@ static int test_sock(void)
>  		BPF_MOV64_IMM(BPF_REG_0, 0), /* r0 = 0 */
>  		BPF_EXIT_INSN(),
>  	};
> -	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
> +	size_t insns_cnt = ARRAY_SIZE(prog);
>  	LIBBPF_OPTS(bpf_prog_load_opts, opts,
>  		.log_buf = bpf_log_buf,
>  		.log_size = BPF_LOG_BUF_SIZE,
> diff --git a/samples/bpf/test_cgrp2_attach.c b/samples/bpf/test_cgrp2_attach.c
> index 6d90874b09c3..68ce69457afe 100644
> --- a/samples/bpf/test_cgrp2_attach.c
> +++ b/samples/bpf/test_cgrp2_attach.c
> @@ -31,6 +31,7 @@
>  #include <bpf/bpf.h>
>  
>  #include "bpf_insn.h"
> +#include "bpf_util.h"
>  
>  enum {
>  	MAP_KEY_PACKETS,
> @@ -70,7 +71,7 @@ static int prog_load(int map_fd, int verdict)
>  		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
>  		BPF_EXIT_INSN(),
>  	};
> -	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
> +	size_t insns_cnt = ARRAY_SIZE(prog);
>  	LIBBPF_OPTS(bpf_prog_load_opts, opts,
>  		.log_buf = bpf_log_buf,
>  		.log_size = BPF_LOG_BUF_SIZE,
> diff --git a/samples/bpf/test_lru_dist.c b/samples/bpf/test_lru_dist.c
> index be98ccb4952f..5efb91763d65 100644
> --- a/samples/bpf/test_lru_dist.c
> +++ b/samples/bpf/test_lru_dist.c
> @@ -523,7 +523,7 @@ int main(int argc, char **argv)
>  		return -1;
>  	}
>  
> -	for (f = 0; f < sizeof(map_flags) / sizeof(*map_flags); f++) {
> +	for (f = 0; f < ARRAY_SIZE(map_flags); f++) {
>  		test_lru_loss0(BPF_MAP_TYPE_LRU_HASH, map_flags[f]);
>  		test_lru_loss1(BPF_MAP_TYPE_LRU_HASH, map_flags[f]);
>  		test_parallel_lru_loss(BPF_MAP_TYPE_LRU_HASH, map_flags[f],
> diff --git a/samples/bpf/test_map_in_map_user.c b/samples/bpf/test_map_in_map_user.c
> index e8b4cc184ac9..652ec720533d 100644
> --- a/samples/bpf/test_map_in_map_user.c
> +++ b/samples/bpf/test_map_in_map_user.c
> @@ -12,6 +12,8 @@
>  #include <bpf/bpf.h>
>  #include <bpf/libbpf.h>
>  
> +#include "bpf_util.h"
> +
>  static int map_fd[7];
>  
>  #define PORT_A		(map_fd[0])
> @@ -28,7 +30,7 @@ static const char * const test_names[] = {
>  	"Hash of Hash",
>  };
>  
> -#define NR_TESTS (sizeof(test_names) / sizeof(*test_names))
> +#define NR_TESTS ARRAY_SIZE(test_names)
>  
>  static void check_map_id(int inner_map_fd, int map_in_map_fd, uint32_t key)
>  {
> diff --git a/samples/bpf/tracex5_user.c b/samples/bpf/tracex5_user.c
> index e910dc265c31..9d7d79f0d47d 100644
> --- a/samples/bpf/tracex5_user.c
> +++ b/samples/bpf/tracex5_user.c
> @@ -8,6 +8,7 @@
>  #include <bpf/bpf.h>
>  #include <bpf/libbpf.h>
>  #include "trace_helpers.h"
> +#include "bpf_util.h"
>  
>  #ifdef __mips__
>  #define	MAX_ENTRIES  6000 /* MIPS n64 syscalls start at 5000 */
> @@ -24,7 +25,7 @@ static void install_accept_all_seccomp(void)
>  		BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
>  	};
>  	struct sock_fprog prog = {
> -		.len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
> +		.len = (unsigned short)ARRAY_SIZE(filter),
>  		.filter = filter,
>  	};
>  	if (prctl(PR_SET_SECCOMP, 2, &prog))
> -- 
> 2.17.1
> 

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

* Re: [PATCH] samples: bpf: Replace sizeof(arr)/sizeof(arr[0]) with ARRAY_SIZE
  2022-07-12  7:23 [PATCH] samples: bpf: Replace sizeof(arr)/sizeof(arr[0]) with ARRAY_SIZE xiaolinkui
  2022-07-12 14:15 ` Jiri Olsa
@ 2022-07-14  4:50 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-07-14  4:50 UTC (permalink / raw)
  To: xiaolinkui
  Cc: ast, daniel, andrii, martin.lau, song, yhs, john.fastabend,
	kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel, xiaolinkui

Hello:

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

On Tue, 12 Jul 2022 15:23:02 +0800 you wrote:
> From: Linkui Xiao<xiaolinkui@kylinos.cn>
> 
> The ARRAY_SIZE macro is more compact and more formal in linux source.
> 
> Signed-off-by: Linkui Xiao<xiaolinkui@kylinos.cn>
> ---
>  samples/bpf/fds_example.c          | 3 ++-
>  samples/bpf/sock_example.c         | 3 ++-
>  samples/bpf/test_cgrp2_attach.c    | 3 ++-
>  samples/bpf/test_lru_dist.c        | 2 +-
>  samples/bpf/test_map_in_map_user.c | 4 +++-
>  samples/bpf/tracex5_user.c         | 3 ++-
>  6 files changed, 12 insertions(+), 6 deletions(-)

Here is the summary with links:
  - samples: bpf: Replace sizeof(arr)/sizeof(arr[0]) with ARRAY_SIZE
    https://git.kernel.org/bpf/bpf-next/c/b1fc28b33886

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:[~2022-07-14  4:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-12  7:23 [PATCH] samples: bpf: Replace sizeof(arr)/sizeof(arr[0]) with ARRAY_SIZE xiaolinkui
2022-07-12 14:15 ` Jiri Olsa
2022-07-14  4:50 ` patchwork-bot+netdevbpf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.