bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Borkmann <daniel@iogearbox.net>
To: Dave Marchevsky <davemarchevsky@fb.com>, bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>, Yonghong Song <yhs@fb.com>,
	netdev@vger.kernel.org
Subject: Re: [PATCH v3 bpf-next 2/7] bpf: add bpf_trace_vprintk helper
Date: Fri, 3 Sep 2021 10:00:00 +0200	[thread overview]
Message-ID: <1253feeb-9832-1a86-7eb2-5076698c4ca3@iogearbox.net> (raw)
In-Reply-To: <20210828052006.1313788-3-davemarchevsky@fb.com>

On 8/28/21 7:20 AM, Dave Marchevsky wrote:
> This helper is meant to be "bpf_trace_printk, but with proper vararg
> support". Follow bpf_snprintf's example and take a u64 pseudo-vararg
> array. Write to /sys/kernel/debug/tracing/trace_pipe using the same
> mechanism as bpf_trace_printk.
> 
> Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com>

lgtm, minor comments below:

> ---
>   include/linux/bpf.h            |  1 +
>   include/uapi/linux/bpf.h       |  9 ++++++
>   kernel/bpf/core.c              |  5 ++++
>   kernel/bpf/helpers.c           |  2 ++
>   kernel/trace/bpf_trace.c       | 52 +++++++++++++++++++++++++++++++++-
>   tools/include/uapi/linux/bpf.h |  9 ++++++
>   6 files changed, 77 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index be8d57e6e78a..b6c45a6cbbba 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -1088,6 +1088,7 @@ bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *f
>   int bpf_prog_calc_tag(struct bpf_prog *fp);
>   
>   const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
> +const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void);
>   
>   typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
>   					unsigned long off, unsigned long len);
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 791f31dd0abe..f171d4d33136 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -4877,6 +4877,14 @@ union bpf_attr {
>    *		Get the struct pt_regs associated with **task**.
>    *	Return
>    *		A pointer to struct pt_regs.
> + *
> + * u64 bpf_trace_vprintk(const char *fmt, u32 fmt_size, const void *data, u32 data_len)

s/u64/long/

> + *	Description
> + *		Behaves like **bpf_trace_printk**\ () helper, but takes an array of u64

nit: maybe for users it's more clear from description if you instead mention that data_len
needs to be multiple of 8 bytes? Or somehow mention the relation with data more clearly
resp. which shortcoming it addresses compared to bpf_trace_printk(), so developers can more
easily parse it.

> + *		to format. Arguments are to be used as in **bpf_seq_printf**\ () helper.
> + *	Return
> + *		The number of bytes written to the buffer, or a negative error
> + *		in case of failure.
>    */
[...]
>   	default:
>   		return NULL;
>   	}
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 10672ebc63b7..ea8358b0c748 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -398,7 +398,7 @@ static const struct bpf_func_proto bpf_trace_printk_proto = {
>   	.arg2_type	= ARG_CONST_SIZE,
>   };
>   
> -const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
> +static void __set_printk_clr_event(void)
>   {
>   	/*
>   	 * This program might be calling bpf_trace_printk,
> @@ -410,10 +410,58 @@ const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
>   	 */
>   	if (trace_set_clr_event("bpf_trace", "bpf_trace_printk", 1))
>   		pr_warn_ratelimited("could not enable bpf_trace_printk events");
> +}
>   
> +const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
> +{
> +	__set_printk_clr_event();
>   	return &bpf_trace_printk_proto;
>   }
>   
> +BPF_CALL_4(bpf_trace_vprintk, char *, fmt, u32, fmt_size, const void *, data,
> +	   u32, data_len)
> +{
> +	static char buf[BPF_TRACE_PRINTK_SIZE];
> +	unsigned long flags;
> +	int ret, num_args;
> +	u32 *bin_args;
> +
> +	if (data_len & 7 || data_len > MAX_BPRINTF_VARARGS * 8 ||
> +	    (data_len && !data))
> +		return -EINVAL;
> +	num_args = data_len / 8;
> +
> +	ret = bpf_bprintf_prepare(fmt, fmt_size, data, &bin_args, num_args);
> +	if (ret < 0)
> +		return ret;

Given you have ARG_PTR_TO_MEM_OR_NULL for data, does this gracefully handle the
case where you pass in fmt string containing e.g. %ps but data being NULL? From
reading bpf_bprintf_prepare() looks like it does just fine, but might be nice
to explicitly add a tiny selftest case for it while you're at it.

> +	raw_spin_lock_irqsave(&trace_printk_lock, flags);
> +	ret = bstr_printf(buf, sizeof(buf), fmt, bin_args);
> +
> +	trace_bpf_trace_printk(buf);
> +	raw_spin_unlock_irqrestore(&trace_printk_lock, flags);
> +
> +	bpf_bprintf_cleanup();
> +
> +	return ret;
> +}

Thanks,
Daniel

  parent reply	other threads:[~2021-09-03  8:00 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-28  5:19 [PATCH v3 bpf-next 0/7] bpf: implement variadic printk helper Dave Marchevsky
2021-08-28  5:20 ` [PATCH v3 bpf-next 1/7] bpf: merge printk and seq_printf VARARG max macros Dave Marchevsky
2021-08-28  5:20 ` [PATCH v3 bpf-next 2/7] bpf: add bpf_trace_vprintk helper Dave Marchevsky
2021-08-30 23:35   ` Andrii Nakryiko
2021-09-03  8:00   ` Daniel Borkmann [this message]
2021-09-13 17:30     ` Dave Marchevsky
2021-08-28  5:20 ` [PATCH v3 bpf-next 3/7] libbpf: Modify bpf_printk to choose helper based on arg count Dave Marchevsky
2021-08-28 16:44   ` Dave Marchevsky
2021-08-28  5:20 ` [PATCH v3 bpf-next 4/7] libbpf: use static const fmt string in __bpf_printk Dave Marchevsky
2021-08-28 16:40   ` Dave Marchevsky
2021-08-29 16:57     ` Alexei Starovoitov
2021-08-31  0:36       ` Andrii Nakryiko
2021-08-31  0:37   ` Andrii Nakryiko
2021-08-28  5:20 ` [PATCH v3 bpf-next 5/7] bpftool: only probe trace_vprintk feature in 'full' mode Dave Marchevsky
2021-08-28  5:20 ` [PATCH v3 bpf-next 6/7] selftests/bpf: Migrate prog_tests/trace_printk CHECKs to ASSERTs Dave Marchevsky
2021-08-31  0:03   ` Andrii Nakryiko
2021-08-28  5:20 ` [PATCH v3 bpf-next 7/7] selftests/bpf: add trace_vprintk test prog Dave Marchevsky
2021-08-31  0:04   ` Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1253feeb-9832-1a86-7eb2-5076698c4ca3@iogearbox.net \
    --to=daniel@iogearbox.net \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=davemarchevsky@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).