All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf 1/2] bpf: Clarify a bpf_bprintf_prepare macro
@ 2021-05-17  9:28 Florent Revest
  2021-05-17  9:28 ` [PATCH bpf 2/2] bpf: Avoid using ARRAY_SIZE on an uninitialized pointer Florent Revest
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Florent Revest @ 2021-05-17  9:28 UTC (permalink / raw)
  To: bpf; +Cc: ast, daniel, andrii, kpsingh, jackmanb, linux-kernel, Florent Revest

The per-cpu buffers contain bprintf data rather than printf arguments.
The macro name and comment were a bit confusing, this rewords them in a
clearer way.

Signed-off-by: Florent Revest <revest@chromium.org>
---
 kernel/bpf/helpers.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index ef658a9ea5c9..3a5ab614cbb0 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -692,13 +692,14 @@ static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
 	return -EINVAL;
 }
 
-/* Per-cpu temp buffers which can be used by printf-like helpers for %s or %p
+/* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary
+ * arguments representation.
  */
-#define MAX_PRINTF_BUF_LEN	512
+#define MAX_BPRINTF_BUF_LEN	512
 
 /* Support executing three nested bprintf helper calls on a given CPU */
 struct bpf_bprintf_buffers {
-	char tmp_bufs[3][MAX_PRINTF_BUF_LEN];
+	char tmp_bufs[3][MAX_BPRINTF_BUF_LEN];
 };
 static DEFINE_PER_CPU(struct bpf_bprintf_buffers, bpf_bprintf_bufs);
 static DEFINE_PER_CPU(int, bpf_bprintf_nest_level);
@@ -761,7 +762,7 @@ int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
 		if (num_args && try_get_fmt_tmp_buf(&tmp_buf))
 			return -EBUSY;
 
-		tmp_buf_end = tmp_buf + MAX_PRINTF_BUF_LEN;
+		tmp_buf_end = tmp_buf + MAX_BPRINTF_BUF_LEN;
 		*bin_args = (u32 *)tmp_buf;
 	}
 
-- 
2.31.1.751.gd2f1c929bd-goog


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

* [PATCH bpf 2/2] bpf: Avoid using ARRAY_SIZE on an uninitialized pointer
  2021-05-17  9:28 [PATCH bpf 1/2] bpf: Clarify a bpf_bprintf_prepare macro Florent Revest
@ 2021-05-17  9:28 ` Florent Revest
  2021-05-17 17:54   ` Song Liu
  2021-05-17 17:54 ` [PATCH bpf 1/2] bpf: Clarify a bpf_bprintf_prepare macro Song Liu
  2021-05-17 18:10 ` patchwork-bot+netdevbpf
  2 siblings, 1 reply; 5+ messages in thread
From: Florent Revest @ 2021-05-17  9:28 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, kpsingh, jackmanb, linux-kernel,
	Florent Revest, kernel test robot

The cppcheck static code analysis reported the following error:
>> helpers.c:713:43: warning: Uninitialized variable: bufs [uninitvar]
    if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bufs->tmp_bufs))) {
                                             ^

ARRAY_SIZE is a macro that expands to sizeofs, so bufs is not actually
dereferenced at runtime, and the code is actually safe. But to keep
things tidy, this patch removes the need for a call to ARRAY_SIZE by
extracting the size of the array into a macro. Cppcheck should no longer
be confused and the code ends up being a bit cleaner.

Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Florent Revest <revest@chromium.org>
---
 kernel/bpf/helpers.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 3a5ab614cbb0..73443498d88f 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -698,8 +698,9 @@ static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
 #define MAX_BPRINTF_BUF_LEN	512
 
 /* Support executing three nested bprintf helper calls on a given CPU */
+#define MAX_BPRINTF_NEST_LEVEL	3
 struct bpf_bprintf_buffers {
-	char tmp_bufs[3][MAX_BPRINTF_BUF_LEN];
+	char tmp_bufs[MAX_BPRINTF_NEST_LEVEL][MAX_BPRINTF_BUF_LEN];
 };
 static DEFINE_PER_CPU(struct bpf_bprintf_buffers, bpf_bprintf_bufs);
 static DEFINE_PER_CPU(int, bpf_bprintf_nest_level);
@@ -711,7 +712,7 @@ static int try_get_fmt_tmp_buf(char **tmp_buf)
 
 	preempt_disable();
 	nest_level = this_cpu_inc_return(bpf_bprintf_nest_level);
-	if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bufs->tmp_bufs))) {
+	if (WARN_ON_ONCE(nest_level > MAX_BPRINTF_NEST_LEVEL)) {
 		this_cpu_dec(bpf_bprintf_nest_level);
 		preempt_enable();
 		return -EBUSY;
-- 
2.31.1.751.gd2f1c929bd-goog


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

* Re: [PATCH bpf 1/2] bpf: Clarify a bpf_bprintf_prepare macro
  2021-05-17  9:28 [PATCH bpf 1/2] bpf: Clarify a bpf_bprintf_prepare macro Florent Revest
  2021-05-17  9:28 ` [PATCH bpf 2/2] bpf: Avoid using ARRAY_SIZE on an uninitialized pointer Florent Revest
@ 2021-05-17 17:54 ` Song Liu
  2021-05-17 18:10 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: Song Liu @ 2021-05-17 17:54 UTC (permalink / raw)
  To: Florent Revest
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	KP Singh, jackmanb, open list

On Mon, May 17, 2021 at 2:29 AM Florent Revest <revest@chromium.org> wrote:
>
> The per-cpu buffers contain bprintf data rather than printf arguments.
> The macro name and comment were a bit confusing, this rewords them in a
> clearer way.
>
> Signed-off-by: Florent Revest <revest@chromium.org>

Acked-by: Song Liu <song@kernel.org>

> ---
>  kernel/bpf/helpers.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index ef658a9ea5c9..3a5ab614cbb0 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -692,13 +692,14 @@ static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
>         return -EINVAL;
>  }
>
> -/* Per-cpu temp buffers which can be used by printf-like helpers for %s or %p
> +/* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary
> + * arguments representation.
>   */
> -#define MAX_PRINTF_BUF_LEN     512
> +#define MAX_BPRINTF_BUF_LEN    512
>
>  /* Support executing three nested bprintf helper calls on a given CPU */
>  struct bpf_bprintf_buffers {
> -       char tmp_bufs[3][MAX_PRINTF_BUF_LEN];
> +       char tmp_bufs[3][MAX_BPRINTF_BUF_LEN];
>  };
>  static DEFINE_PER_CPU(struct bpf_bprintf_buffers, bpf_bprintf_bufs);
>  static DEFINE_PER_CPU(int, bpf_bprintf_nest_level);
> @@ -761,7 +762,7 @@ int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
>                 if (num_args && try_get_fmt_tmp_buf(&tmp_buf))
>                         return -EBUSY;
>
> -               tmp_buf_end = tmp_buf + MAX_PRINTF_BUF_LEN;
> +               tmp_buf_end = tmp_buf + MAX_BPRINTF_BUF_LEN;
>                 *bin_args = (u32 *)tmp_buf;
>         }
>
> --
> 2.31.1.751.gd2f1c929bd-goog
>

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

* Re: [PATCH bpf 2/2] bpf: Avoid using ARRAY_SIZE on an uninitialized pointer
  2021-05-17  9:28 ` [PATCH bpf 2/2] bpf: Avoid using ARRAY_SIZE on an uninitialized pointer Florent Revest
@ 2021-05-17 17:54   ` Song Liu
  0 siblings, 0 replies; 5+ messages in thread
From: Song Liu @ 2021-05-17 17:54 UTC (permalink / raw)
  To: Florent Revest
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	KP Singh, jackmanb, open list, kernel test robot

On Mon, May 17, 2021 at 2:29 AM Florent Revest <revest@chromium.org> wrote:
>
> The cppcheck static code analysis reported the following error:
> >> helpers.c:713:43: warning: Uninitialized variable: bufs [uninitvar]
>     if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bufs->tmp_bufs))) {
>                                              ^
>
> ARRAY_SIZE is a macro that expands to sizeofs, so bufs is not actually
> dereferenced at runtime, and the code is actually safe. But to keep
> things tidy, this patch removes the need for a call to ARRAY_SIZE by
> extracting the size of the array into a macro. Cppcheck should no longer
> be confused and the code ends up being a bit cleaner.
>
> Reported-by: kernel test robot <lkp@intel.com>
> Signed-off-by: Florent Revest <revest@chromium.org>

Acked-by: Song Liu <song@kernel.org>

> ---
>  kernel/bpf/helpers.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 3a5ab614cbb0..73443498d88f 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -698,8 +698,9 @@ static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
>  #define MAX_BPRINTF_BUF_LEN    512
>
>  /* Support executing three nested bprintf helper calls on a given CPU */
> +#define MAX_BPRINTF_NEST_LEVEL 3
>  struct bpf_bprintf_buffers {
> -       char tmp_bufs[3][MAX_BPRINTF_BUF_LEN];
> +       char tmp_bufs[MAX_BPRINTF_NEST_LEVEL][MAX_BPRINTF_BUF_LEN];
>  };
>  static DEFINE_PER_CPU(struct bpf_bprintf_buffers, bpf_bprintf_bufs);
>  static DEFINE_PER_CPU(int, bpf_bprintf_nest_level);
> @@ -711,7 +712,7 @@ static int try_get_fmt_tmp_buf(char **tmp_buf)
>
>         preempt_disable();
>         nest_level = this_cpu_inc_return(bpf_bprintf_nest_level);
> -       if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bufs->tmp_bufs))) {
> +       if (WARN_ON_ONCE(nest_level > MAX_BPRINTF_NEST_LEVEL)) {
>                 this_cpu_dec(bpf_bprintf_nest_level);
>                 preempt_enable();
>                 return -EBUSY;
> --
> 2.31.1.751.gd2f1c929bd-goog
>

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

* Re: [PATCH bpf 1/2] bpf: Clarify a bpf_bprintf_prepare macro
  2021-05-17  9:28 [PATCH bpf 1/2] bpf: Clarify a bpf_bprintf_prepare macro Florent Revest
  2021-05-17  9:28 ` [PATCH bpf 2/2] bpf: Avoid using ARRAY_SIZE on an uninitialized pointer Florent Revest
  2021-05-17 17:54 ` [PATCH bpf 1/2] bpf: Clarify a bpf_bprintf_prepare macro Song Liu
@ 2021-05-17 18:10 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2021-05-17 18:10 UTC (permalink / raw)
  To: Florent Revest; +Cc: bpf, ast, daniel, andrii, kpsingh, jackmanb, linux-kernel

Hello:

This series was applied to bpf/bpf.git (refs/heads/master):

On Mon, 17 May 2021 11:28:29 +0200 you wrote:
> The per-cpu buffers contain bprintf data rather than printf arguments.
> The macro name and comment were a bit confusing, this rewords them in a
> clearer way.
> 
> Signed-off-by: Florent Revest <revest@chromium.org>
> ---
>  kernel/bpf/helpers.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)

Here is the summary with links:
  - [bpf,1/2] bpf: Clarify a bpf_bprintf_prepare macro
    https://git.kernel.org/bpf/bpf/c/8ba1030385e3
  - [bpf,2/2] bpf: Avoid using ARRAY_SIZE on an uninitialized pointer
    https://git.kernel.org/bpf/bpf/c/d0c0fe10ce6d

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] 5+ messages in thread

end of thread, other threads:[~2021-05-17 18:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-17  9:28 [PATCH bpf 1/2] bpf: Clarify a bpf_bprintf_prepare macro Florent Revest
2021-05-17  9:28 ` [PATCH bpf 2/2] bpf: Avoid using ARRAY_SIZE on an uninitialized pointer Florent Revest
2021-05-17 17:54   ` Song Liu
2021-05-17 17:54 ` [PATCH bpf 1/2] bpf: Clarify a bpf_bprintf_prepare macro Song Liu
2021-05-17 18:10 ` 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.