diff mbox series

[bpf-next,3/3] bpf: introduce bpf_relay_output helper

Message ID 20231222122146.65519-4-lulie@linux.alibaba.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: introduce BPF_MAP_TYPE_RELAY | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-7 fail Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 fail Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 fail Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 fail Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-32 fail Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 fail Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 fail Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 fail Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 fail Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-14 fail Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 fail Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/ynl success SINGLE THREAD; Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 2918 this patch: 2918
netdev/cc_maintainers success CCed 16 of 16 maintainers
netdev/build_clang success Errors and warnings before: 1280 this patch: 1280
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 2999 this patch: 2999
netdev/checkpatch warning WARNING: line length of 84 exceeds 80 columns WARNING: please, no space before tabs
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 1 this patch: 1
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-next-PR fail PR summary

Commit Message

Philo Lu Dec. 22, 2023, 12:21 p.m. UTC
Like perfbuf/ringbuf, a helper is needed to write into the buffer, named
bpf_relay_output, whose usage is same as ringbuf. Note that it works only
after relay files are set, i.e., after calling map_update_elem for the
created relay map.

Signed-off-by: Philo Lu <lulie@linux.alibaba.com>
---
 include/linux/bpf.h      |  1 +
 include/uapi/linux/bpf.h | 10 ++++++++++
 kernel/bpf/helpers.c     |  4 ++++
 kernel/bpf/relaymap.c    | 26 ++++++++++++++++++++++++++
 kernel/bpf/verifier.c    |  8 ++++++++
 kernel/trace/bpf_trace.c |  4 ++++
 6 files changed, 53 insertions(+)

Comments

Jiri Olsa Dec. 22, 2023, 2:46 p.m. UTC | #1
On Fri, Dec 22, 2023 at 08:21:46PM +0800, Philo Lu wrote:
> Like perfbuf/ringbuf, a helper is needed to write into the buffer, named
> bpf_relay_output, whose usage is same as ringbuf. Note that it works only
> after relay files are set, i.e., after calling map_update_elem for the
> created relay map.

we can't add helpers anymore, this will need to be kfunc
check functions marked with __bpf_kfunc as examples

jirka

> 
> Signed-off-by: Philo Lu <lulie@linux.alibaba.com>
> ---
>  include/linux/bpf.h      |  1 +
>  include/uapi/linux/bpf.h | 10 ++++++++++
>  kernel/bpf/helpers.c     |  4 ++++
>  kernel/bpf/relaymap.c    | 26 ++++++++++++++++++++++++++
>  kernel/bpf/verifier.c    |  8 ++++++++
>  kernel/trace/bpf_trace.c |  4 ++++
>  6 files changed, 53 insertions(+)
> 
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index 7671530d6e4e..b177122369e6 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -3095,6 +3095,7 @@ extern const struct bpf_func_proto bpf_get_retval_proto;
>  extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto;
>  extern const struct bpf_func_proto bpf_cgrp_storage_get_proto;
>  extern const struct bpf_func_proto bpf_cgrp_storage_delete_proto;
> +extern const struct bpf_func_proto bpf_relay_output_proto;
>  
>  const struct bpf_func_proto *tracing_prog_func_proto(
>    enum bpf_func_id func_id, const struct bpf_prog *prog);
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 143b75676bd3..03c0c1953ba1 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -5686,6 +5686,15 @@ union bpf_attr {
>   *		0 on success.
>   *
>   *		**-ENOENT** if the bpf_local_storage cannot be found.
> + *
> + * long bpf_relay_output(void *map, void *data, u64 size, u64 flags)
> + * 	Description
> + * 		Copy *size* bytes from *data* into *map* of type BPF_MAP_TYPE_RELAY.
> + * 		Currently, the *flags* must be 0.
> + * 	Return
> + * 		0 on success.
> + *
> + *		**-ENOENT** if the relay base_file in debugfs cannot be found.
>   */
>  #define ___BPF_FUNC_MAPPER(FN, ctx...)			\
>  	FN(unspec, 0, ##ctx)				\
> @@ -5900,6 +5909,7 @@ union bpf_attr {
>  	FN(user_ringbuf_drain, 209, ##ctx)		\
>  	FN(cgrp_storage_get, 210, ##ctx)		\
>  	FN(cgrp_storage_delete, 211, ##ctx)		\
> +	FN(relay_output, 212, ##ctx)		\
>  	/* */
>  
>  /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index be72824f32b2..0c26e87ce729 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -1720,6 +1720,10 @@ bpf_base_func_proto(enum bpf_func_id func_id)
>  		return &bpf_ringbuf_discard_proto;
>  	case BPF_FUNC_ringbuf_query:
>  		return &bpf_ringbuf_query_proto;
> +#ifdef CONFIG_RELAY
> +	case BPF_FUNC_relay_output:
> +		return &bpf_relay_output_proto;
> +#endif
>  	case BPF_FUNC_strncmp:
>  		return &bpf_strncmp_proto;
>  	case BPF_FUNC_strtol:
> diff --git a/kernel/bpf/relaymap.c b/kernel/bpf/relaymap.c
> index 588c8de0a4bd..f9e2e4a780df 100644
> --- a/kernel/bpf/relaymap.c
> +++ b/kernel/bpf/relaymap.c
> @@ -173,6 +173,32 @@ static u64 relay_map_mem_usage(const struct bpf_map *map)
>  	return usage;
>  }
>  
> +BPF_CALL_4(bpf_relay_output, struct bpf_map *, map, void *, data, u64, size,
> +	   u64, flags)
> +{
> +	struct bpf_relay_map *rmap;
> +
> +	/* not support any flag now */
> +	if (unlikely(flags))
> +		return -EINVAL;
> +
> +	rmap = container_of(map, struct bpf_relay_map, map);
> +	if (!rmap->relay_chan->has_base_filename)
> +		return -ENOENT;
> +
> +	relay_write(rmap->relay_chan, data, size);
> +	return 0;
> +}
> +
> +const struct bpf_func_proto bpf_relay_output_proto = {
> +	.func		= bpf_relay_output,
> +	.ret_type	= RET_INTEGER,
> +	.arg1_type	= ARG_CONST_MAP_PTR,
> +	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
> +	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
> +	.arg4_type	= ARG_ANYTHING,
> +};
> +
>  BTF_ID_LIST_SINGLE(relay_map_btf_ids, struct, bpf_relay_map)
>  const struct bpf_map_ops relay_map_ops = {
>  	.map_meta_equal = bpf_map_meta_equal,
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index f13008d27f35..8c8287d6ae18 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -8800,6 +8800,10 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
>  		if (func_id != BPF_FUNC_user_ringbuf_drain)
>  			goto error;
>  		break;
> +	case BPF_MAP_TYPE_RELAY:
> +		if (func_id != BPF_FUNC_relay_output)
> +			goto error;
> +		break;
>  	case BPF_MAP_TYPE_STACK_TRACE:
>  		if (func_id != BPF_FUNC_get_stackid)
>  			goto error;
> @@ -8932,6 +8936,10 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
>  		if (map->map_type != BPF_MAP_TYPE_USER_RINGBUF)
>  			goto error;
>  		break;
> +	case BPF_FUNC_relay_output:
> +		if (map->map_type != BPF_MAP_TYPE_RELAY)
> +			goto error;
> +		break;
>  	case BPF_FUNC_get_stackid:
>  		if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
>  			goto error;
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 7ac6c52b25eb..5b13553c6232 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -1594,6 +1594,10 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
>  		return &bpf_ringbuf_discard_proto;
>  	case BPF_FUNC_ringbuf_query:
>  		return &bpf_ringbuf_query_proto;
> +#ifdef CONFIG_RELAY
> +	case BPF_FUNC_relay_output:
> +		return &bpf_relay_output_proto;
> +#endif
>  	case BPF_FUNC_jiffies64:
>  		return &bpf_jiffies64_proto;
>  	case BPF_FUNC_get_task_stack:
> -- 
> 2.32.0.3.g01195cf9f
>
Philo Lu Dec. 23, 2023, 2:56 a.m. UTC | #2
On 2023/12/22 22:46, Jiri Olsa wrote:
> On Fri, Dec 22, 2023 at 08:21:46PM +0800, Philo Lu wrote:
>> Like perfbuf/ringbuf, a helper is needed to write into the buffer, named
>> bpf_relay_output, whose usage is same as ringbuf. Note that it works only
>> after relay files are set, i.e., after calling map_update_elem for the
>> created relay map.
> 
> we can't add helpers anymore, this will need to be kfunc
> check functions marked with __bpf_kfunc as examples
> 

Got it, I will change it to kfunc in the next version.

Thanks.

> jirka
> 
>>
>> Signed-off-by: Philo Lu <lulie@linux.alibaba.com>
>> ---
>>   include/linux/bpf.h      |  1 +
>>   include/uapi/linux/bpf.h | 10 ++++++++++
>>   kernel/bpf/helpers.c     |  4 ++++
>>   kernel/bpf/relaymap.c    | 26 ++++++++++++++++++++++++++
>>   kernel/bpf/verifier.c    |  8 ++++++++
>>   kernel/trace/bpf_trace.c |  4 ++++
>>   6 files changed, 53 insertions(+)
>>
>> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
>> index 7671530d6e4e..b177122369e6 100644
>> --- a/include/linux/bpf.h
>> +++ b/include/linux/bpf.h
>> @@ -3095,6 +3095,7 @@ extern const struct bpf_func_proto bpf_get_retval_proto;
>>   extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto;
>>   extern const struct bpf_func_proto bpf_cgrp_storage_get_proto;
>>   extern const struct bpf_func_proto bpf_cgrp_storage_delete_proto;
>> +extern const struct bpf_func_proto bpf_relay_output_proto;
>>   
>>   const struct bpf_func_proto *tracing_prog_func_proto(
>>     enum bpf_func_id func_id, const struct bpf_prog *prog);
>> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
>> index 143b75676bd3..03c0c1953ba1 100644
>> --- a/include/uapi/linux/bpf.h
>> +++ b/include/uapi/linux/bpf.h
>> @@ -5686,6 +5686,15 @@ union bpf_attr {
>>    *		0 on success.
>>    *
>>    *		**-ENOENT** if the bpf_local_storage cannot be found.
>> + *
>> + * long bpf_relay_output(void *map, void *data, u64 size, u64 flags)
>> + * 	Description
>> + * 		Copy *size* bytes from *data* into *map* of type BPF_MAP_TYPE_RELAY.
>> + * 		Currently, the *flags* must be 0.
>> + * 	Return
>> + * 		0 on success.
>> + *
>> + *		**-ENOENT** if the relay base_file in debugfs cannot be found.
>>    */
>>   #define ___BPF_FUNC_MAPPER(FN, ctx...)			\
>>   	FN(unspec, 0, ##ctx)				\
>> @@ -5900,6 +5909,7 @@ union bpf_attr {
>>   	FN(user_ringbuf_drain, 209, ##ctx)		\
>>   	FN(cgrp_storage_get, 210, ##ctx)		\
>>   	FN(cgrp_storage_delete, 211, ##ctx)		\
>> +	FN(relay_output, 212, ##ctx)		\
>>   	/* */
>>   
>>   /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
>> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
>> index be72824f32b2..0c26e87ce729 100644
>> --- a/kernel/bpf/helpers.c
>> +++ b/kernel/bpf/helpers.c
>> @@ -1720,6 +1720,10 @@ bpf_base_func_proto(enum bpf_func_id func_id)
>>   		return &bpf_ringbuf_discard_proto;
>>   	case BPF_FUNC_ringbuf_query:
>>   		return &bpf_ringbuf_query_proto;
>> +#ifdef CONFIG_RELAY
>> +	case BPF_FUNC_relay_output:
>> +		return &bpf_relay_output_proto;
>> +#endif
>>   	case BPF_FUNC_strncmp:
>>   		return &bpf_strncmp_proto;
>>   	case BPF_FUNC_strtol:
>> diff --git a/kernel/bpf/relaymap.c b/kernel/bpf/relaymap.c
>> index 588c8de0a4bd..f9e2e4a780df 100644
>> --- a/kernel/bpf/relaymap.c
>> +++ b/kernel/bpf/relaymap.c
>> @@ -173,6 +173,32 @@ static u64 relay_map_mem_usage(const struct bpf_map *map)
>>   	return usage;
>>   }
>>   
>> +BPF_CALL_4(bpf_relay_output, struct bpf_map *, map, void *, data, u64, size,
>> +	   u64, flags)
>> +{
>> +	struct bpf_relay_map *rmap;
>> +
>> +	/* not support any flag now */
>> +	if (unlikely(flags))
>> +		return -EINVAL;
>> +
>> +	rmap = container_of(map, struct bpf_relay_map, map);
>> +	if (!rmap->relay_chan->has_base_filename)
>> +		return -ENOENT;
>> +
>> +	relay_write(rmap->relay_chan, data, size);
>> +	return 0;
>> +}
>> +
>> +const struct bpf_func_proto bpf_relay_output_proto = {
>> +	.func		= bpf_relay_output,
>> +	.ret_type	= RET_INTEGER,
>> +	.arg1_type	= ARG_CONST_MAP_PTR,
>> +	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
>> +	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
>> +	.arg4_type	= ARG_ANYTHING,
>> +};
>> +
>>   BTF_ID_LIST_SINGLE(relay_map_btf_ids, struct, bpf_relay_map)
>>   const struct bpf_map_ops relay_map_ops = {
>>   	.map_meta_equal = bpf_map_meta_equal,
>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>> index f13008d27f35..8c8287d6ae18 100644
>> --- a/kernel/bpf/verifier.c
>> +++ b/kernel/bpf/verifier.c
>> @@ -8800,6 +8800,10 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
>>   		if (func_id != BPF_FUNC_user_ringbuf_drain)
>>   			goto error;
>>   		break;
>> +	case BPF_MAP_TYPE_RELAY:
>> +		if (func_id != BPF_FUNC_relay_output)
>> +			goto error;
>> +		break;
>>   	case BPF_MAP_TYPE_STACK_TRACE:
>>   		if (func_id != BPF_FUNC_get_stackid)
>>   			goto error;
>> @@ -8932,6 +8936,10 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
>>   		if (map->map_type != BPF_MAP_TYPE_USER_RINGBUF)
>>   			goto error;
>>   		break;
>> +	case BPF_FUNC_relay_output:
>> +		if (map->map_type != BPF_MAP_TYPE_RELAY)
>> +			goto error;
>> +		break;
>>   	case BPF_FUNC_get_stackid:
>>   		if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
>>   			goto error;
>> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
>> index 7ac6c52b25eb..5b13553c6232 100644
>> --- a/kernel/trace/bpf_trace.c
>> +++ b/kernel/trace/bpf_trace.c
>> @@ -1594,6 +1594,10 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
>>   		return &bpf_ringbuf_discard_proto;
>>   	case BPF_FUNC_ringbuf_query:
>>   		return &bpf_ringbuf_query_proto;
>> +#ifdef CONFIG_RELAY
>> +	case BPF_FUNC_relay_output:
>> +		return &bpf_relay_output_proto;
>> +#endif
>>   	case BPF_FUNC_jiffies64:
>>   		return &bpf_jiffies64_proto;
>>   	case BPF_FUNC_get_task_stack:
>> -- 
>> 2.32.0.3.g01195cf9f
>>
diff mbox series

Patch

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 7671530d6e4e..b177122369e6 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -3095,6 +3095,7 @@  extern const struct bpf_func_proto bpf_get_retval_proto;
 extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto;
 extern const struct bpf_func_proto bpf_cgrp_storage_get_proto;
 extern const struct bpf_func_proto bpf_cgrp_storage_delete_proto;
+extern const struct bpf_func_proto bpf_relay_output_proto;
 
 const struct bpf_func_proto *tracing_prog_func_proto(
   enum bpf_func_id func_id, const struct bpf_prog *prog);
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 143b75676bd3..03c0c1953ba1 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -5686,6 +5686,15 @@  union bpf_attr {
  *		0 on success.
  *
  *		**-ENOENT** if the bpf_local_storage cannot be found.
+ *
+ * long bpf_relay_output(void *map, void *data, u64 size, u64 flags)
+ * 	Description
+ * 		Copy *size* bytes from *data* into *map* of type BPF_MAP_TYPE_RELAY.
+ * 		Currently, the *flags* must be 0.
+ * 	Return
+ * 		0 on success.
+ *
+ *		**-ENOENT** if the relay base_file in debugfs cannot be found.
  */
 #define ___BPF_FUNC_MAPPER(FN, ctx...)			\
 	FN(unspec, 0, ##ctx)				\
@@ -5900,6 +5909,7 @@  union bpf_attr {
 	FN(user_ringbuf_drain, 209, ##ctx)		\
 	FN(cgrp_storage_get, 210, ##ctx)		\
 	FN(cgrp_storage_delete, 211, ##ctx)		\
+	FN(relay_output, 212, ##ctx)		\
 	/* */
 
 /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index be72824f32b2..0c26e87ce729 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1720,6 +1720,10 @@  bpf_base_func_proto(enum bpf_func_id func_id)
 		return &bpf_ringbuf_discard_proto;
 	case BPF_FUNC_ringbuf_query:
 		return &bpf_ringbuf_query_proto;
+#ifdef CONFIG_RELAY
+	case BPF_FUNC_relay_output:
+		return &bpf_relay_output_proto;
+#endif
 	case BPF_FUNC_strncmp:
 		return &bpf_strncmp_proto;
 	case BPF_FUNC_strtol:
diff --git a/kernel/bpf/relaymap.c b/kernel/bpf/relaymap.c
index 588c8de0a4bd..f9e2e4a780df 100644
--- a/kernel/bpf/relaymap.c
+++ b/kernel/bpf/relaymap.c
@@ -173,6 +173,32 @@  static u64 relay_map_mem_usage(const struct bpf_map *map)
 	return usage;
 }
 
+BPF_CALL_4(bpf_relay_output, struct bpf_map *, map, void *, data, u64, size,
+	   u64, flags)
+{
+	struct bpf_relay_map *rmap;
+
+	/* not support any flag now */
+	if (unlikely(flags))
+		return -EINVAL;
+
+	rmap = container_of(map, struct bpf_relay_map, map);
+	if (!rmap->relay_chan->has_base_filename)
+		return -ENOENT;
+
+	relay_write(rmap->relay_chan, data, size);
+	return 0;
+}
+
+const struct bpf_func_proto bpf_relay_output_proto = {
+	.func		= bpf_relay_output,
+	.ret_type	= RET_INTEGER,
+	.arg1_type	= ARG_CONST_MAP_PTR,
+	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
+	.arg3_type	= ARG_CONST_SIZE_OR_ZERO,
+	.arg4_type	= ARG_ANYTHING,
+};
+
 BTF_ID_LIST_SINGLE(relay_map_btf_ids, struct, bpf_relay_map)
 const struct bpf_map_ops relay_map_ops = {
 	.map_meta_equal = bpf_map_meta_equal,
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index f13008d27f35..8c8287d6ae18 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -8800,6 +8800,10 @@  static int check_map_func_compatibility(struct bpf_verifier_env *env,
 		if (func_id != BPF_FUNC_user_ringbuf_drain)
 			goto error;
 		break;
+	case BPF_MAP_TYPE_RELAY:
+		if (func_id != BPF_FUNC_relay_output)
+			goto error;
+		break;
 	case BPF_MAP_TYPE_STACK_TRACE:
 		if (func_id != BPF_FUNC_get_stackid)
 			goto error;
@@ -8932,6 +8936,10 @@  static int check_map_func_compatibility(struct bpf_verifier_env *env,
 		if (map->map_type != BPF_MAP_TYPE_USER_RINGBUF)
 			goto error;
 		break;
+	case BPF_FUNC_relay_output:
+		if (map->map_type != BPF_MAP_TYPE_RELAY)
+			goto error;
+		break;
 	case BPF_FUNC_get_stackid:
 		if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
 			goto error;
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 7ac6c52b25eb..5b13553c6232 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -1594,6 +1594,10 @@  bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_ringbuf_discard_proto;
 	case BPF_FUNC_ringbuf_query:
 		return &bpf_ringbuf_query_proto;
+#ifdef CONFIG_RELAY
+	case BPF_FUNC_relay_output:
+		return &bpf_relay_output_proto;
+#endif
 	case BPF_FUNC_jiffies64:
 		return &bpf_jiffies64_proto;
 	case BPF_FUNC_get_task_stack: