linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: Florian Lehner <dev@der-flo.net>, bpf@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org,
	john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com,
	haoluo@google.com, jolsa@kernel.org, rostedt@goodmis.org,
	mhiramat@kernel.org, mathieu.desnoyers@efficios.com,
	linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH bpf-next] bpf: add support to read cpu_entry in bpf program
Date: Mon, 29 Apr 2024 15:26:25 -0700	[thread overview]
Message-ID: <a8e502fd-db39-4129-96ff-18e65f0f753b@linux.dev> (raw)
In-Reply-To: <20240427151825.174486-1-dev@der-flo.net>


On 4/27/24 8:18 AM, Florian Lehner wrote:
> Add new field "cpu_entry" to bpf_perf_event_data which could be read by
> bpf programs attached to perf events. The value contains the CPU value
> recorded by specifying sample_type with PERF_SAMPLE_CPU when calling
> perf_event_open().

You can use bpf_cast_to_kern_ctx kfunc which can cast 'struct bpf_perf_event_data'
ctx to 'struct bpf_perf_event_data_kern'.

struct bpf_perf_event_data_kern {
         bpf_user_pt_regs_t *regs;
         struct perf_sample_data *data;
         struct perf_event *event;
};

You can access bpf_perf_event_data_kern->data and then to access 'cpu_entry' field.

>
> Signed-off-by: Florian Lehner <dev@der-flo.net>
> ---
>   include/uapi/linux/bpf_perf_event.h       |  4 ++++
>   kernel/trace/bpf_trace.c                  | 13 +++++++++++++
>   tools/include/uapi/linux/bpf_perf_event.h |  4 ++++
>   3 files changed, 21 insertions(+)
>
> diff --git a/include/uapi/linux/bpf_perf_event.h b/include/uapi/linux/bpf_perf_event.h
> index eb1b9d21250c..4856b4396ece 100644
> --- a/include/uapi/linux/bpf_perf_event.h
> +++ b/include/uapi/linux/bpf_perf_event.h
> @@ -14,6 +14,10 @@ struct bpf_perf_event_data {
>   	bpf_user_pt_regs_t regs;
>   	__u64 sample_period;
>   	__u64 addr;
> +	struct {
> +		u32	cpu;
> +		u32	reserved;
> +	}			cpu_entry;
>   };
>   
>   #endif /* _UAPI__LINUX_BPF_PERF_EVENT_H__ */
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index afb232b1d7c2..2b303221af5c 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -2176,6 +2176,11 @@ static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type
>   		if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
>   			return false;
>   		break;
> +	case bpf_ctx_range(struct bpf_perf_event_data, cpu_entry):
> +		bpf_ctx_record_field_size(info, size_u64);
> +		if (!bpf_ctx_narrow_access_ok(off, size, size_u64))
> +			return false;
> +		break;
>   	default:
>   		if (size != sizeof(long))
>   			return false;
> @@ -2208,6 +2213,14 @@ static u32 pe_prog_convert_ctx_access(enum bpf_access_type type,
>   				      bpf_target_off(struct perf_sample_data, addr, 8,
>   						     target_size));
>   		break;
> +	case offsetof(struct bpf_perf_event_data, cpu_entry):
> +		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
> +						       data), si->dst_reg, si->src_reg,
> +				      offsetof(struct bpf_perf_event_data_kern, data));
> +		*insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
> +				      bpf_target_off(struct perf_sample_data, cpu_entry, 8,
> +						     target_size));
> +		break;
>   	default:
>   		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_perf_event_data_kern,
>   						       regs), si->dst_reg, si->src_reg,
> diff --git a/tools/include/uapi/linux/bpf_perf_event.h b/tools/include/uapi/linux/bpf_perf_event.h
> index eb1b9d21250c..4856b4396ece 100644
> --- a/tools/include/uapi/linux/bpf_perf_event.h
> +++ b/tools/include/uapi/linux/bpf_perf_event.h
> @@ -14,6 +14,10 @@ struct bpf_perf_event_data {
>   	bpf_user_pt_regs_t regs;
>   	__u64 sample_period;
>   	__u64 addr;
> +	struct {
> +		u32	cpu;
> +		u32	reserved;
> +	}			cpu_entry;
>   };
>   
>   #endif /* _UAPI__LINUX_BPF_PERF_EVENT_H__ */

      reply	other threads:[~2024-04-29 22:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-27 15:18 [PATCH bpf-next] bpf: add support to read cpu_entry in bpf program Florian Lehner
2024-04-29 22:26 ` Yonghong Song [this message]

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=a8e502fd-db39-4129-96ff-18e65f0f753b@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=dev@der-flo.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    /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).