mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	bpf: add helper bpf_perf_prog_read_value
This patch adds helper bpf_perf_prog_read_cvalue for perf event based bpf programs, to read event counter and enabled/running time. The enabled/running time is accumulated since the perf event open. The typical use case for perf event based bpf program is to attach itself to a single event. In such cases, if it is desirable to get scaling factor between two bpf invocations, users can can save the time values in a map, and use the value from the map and the current value to calculate the scaling factor. Signed-off-by: Yonghong Song <yhs@fb.com> Acked-by: Alexei Starovoitov <ast@fb.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
		
							parent
							
								
									020a32d958
								
							
						
					
					
						commit
						4bebdc7a85
					
				
					 2 changed files with 37 additions and 1 deletions
				
			
		| 
						 | 
					@ -649,6 +649,13 @@ union bpf_attr {
 | 
				
			||||||
 *     @buf: buf to fill
 | 
					 *     @buf: buf to fill
 | 
				
			||||||
 *     @buf_size: size of the buf
 | 
					 *     @buf_size: size of the buf
 | 
				
			||||||
 *     Return: 0 on success or negative error code
 | 
					 *     Return: 0 on success or negative error code
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * int bpf_perf_prog_read_value(ctx, buf, buf_size)
 | 
				
			||||||
 | 
					 *     read perf prog attached perf event counter and enabled/running time
 | 
				
			||||||
 | 
					 *     @ctx: pointer to ctx
 | 
				
			||||||
 | 
					 *     @buf: buf to fill
 | 
				
			||||||
 | 
					 *     @buf_size: size of the buf
 | 
				
			||||||
 | 
					 *     Return : 0 on success or negative error code
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
#define __BPF_FUNC_MAPPER(FN)		\
 | 
					#define __BPF_FUNC_MAPPER(FN)		\
 | 
				
			||||||
	FN(unspec),			\
 | 
						FN(unspec),			\
 | 
				
			||||||
| 
						 | 
					@ -706,7 +713,8 @@ union bpf_attr {
 | 
				
			||||||
	FN(sk_redirect_map),		\
 | 
						FN(sk_redirect_map),		\
 | 
				
			||||||
	FN(sock_map_update),		\
 | 
						FN(sock_map_update),		\
 | 
				
			||||||
	FN(xdp_adjust_meta),		\
 | 
						FN(xdp_adjust_meta),		\
 | 
				
			||||||
	FN(perf_event_read_value),
 | 
						FN(perf_event_read_value),	\
 | 
				
			||||||
 | 
						FN(perf_prog_read_value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
 | 
					/* integer value in 'imm' field of BPF_CALL instruction selects which helper
 | 
				
			||||||
 * function eBPF program intends to call
 | 
					 * function eBPF program intends to call
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -613,6 +613,32 @@ static const struct bpf_func_proto bpf_get_stackid_proto_tp = {
 | 
				
			||||||
	.arg3_type	= ARG_ANYTHING,
 | 
						.arg3_type	= ARG_ANYTHING,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					BPF_CALL_3(bpf_perf_prog_read_value_tp, struct bpf_perf_event_data_kern *, ctx,
 | 
				
			||||||
 | 
						   struct bpf_perf_event_value *, buf, u32, size)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int err = -EINVAL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (unlikely(size != sizeof(struct bpf_perf_event_value)))
 | 
				
			||||||
 | 
							goto clear;
 | 
				
			||||||
 | 
						err = perf_event_read_local(ctx->event, &buf->counter, &buf->enabled,
 | 
				
			||||||
 | 
									    &buf->running);
 | 
				
			||||||
 | 
						if (unlikely(err))
 | 
				
			||||||
 | 
							goto clear;
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					clear:
 | 
				
			||||||
 | 
						memset(buf, 0, size);
 | 
				
			||||||
 | 
						return err;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const struct bpf_func_proto bpf_perf_prog_read_value_proto_tp = {
 | 
				
			||||||
 | 
					         .func           = bpf_perf_prog_read_value_tp,
 | 
				
			||||||
 | 
					         .gpl_only       = true,
 | 
				
			||||||
 | 
					         .ret_type       = RET_INTEGER,
 | 
				
			||||||
 | 
					         .arg1_type      = ARG_PTR_TO_CTX,
 | 
				
			||||||
 | 
					         .arg2_type      = ARG_PTR_TO_UNINIT_MEM,
 | 
				
			||||||
 | 
					         .arg3_type      = ARG_CONST_SIZE,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
 | 
					static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	switch (func_id) {
 | 
						switch (func_id) {
 | 
				
			||||||
| 
						 | 
					@ -620,6 +646,8 @@ static const struct bpf_func_proto *tp_prog_func_proto(enum bpf_func_id func_id)
 | 
				
			||||||
		return &bpf_perf_event_output_proto_tp;
 | 
							return &bpf_perf_event_output_proto_tp;
 | 
				
			||||||
	case BPF_FUNC_get_stackid:
 | 
						case BPF_FUNC_get_stackid:
 | 
				
			||||||
		return &bpf_get_stackid_proto_tp;
 | 
							return &bpf_get_stackid_proto_tp;
 | 
				
			||||||
 | 
						case BPF_FUNC_perf_prog_read_value:
 | 
				
			||||||
 | 
							return &bpf_perf_prog_read_value_proto_tp;
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		return tracing_func_proto(func_id);
 | 
							return tracing_func_proto(func_id);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue