Skip to content

Commit eb41137

Browse files
alan-maguireAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Add bpf_seq_printf_btf helper
A helper is added to allow seq file writing of kernel data structures using vmlinux BTF. Its signature is long bpf_seq_printf_btf(struct seq_file *m, struct btf_ptr *ptr, u32 btf_ptr_size, u64 flags); Flags and struct btf_ptr definitions/use are identical to the bpf_snprintf_btf helper, and the helper returns 0 on success or a negative error value. Suggested-by: Alexei Starovoitov <[email protected]> Signed-off-by: Alan Maguire <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent eb58bbf commit eb41137

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

include/linux/btf.h

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ const struct btf_type *btf_type_id_size(const struct btf *btf,
6868

6969
void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
7070
struct seq_file *m);
71+
int btf_type_seq_show_flags(const struct btf *btf, u32 type_id, void *obj,
72+
struct seq_file *m, u64 flags);
7173

7274
/*
7375
* Copy len bytes of string representation of obj of BTF type_id into buf.

include/uapi/linux/bpf.h

+9
Original file line numberDiff line numberDiff line change
@@ -3630,6 +3630,14 @@ union bpf_attr {
36303630
* The number of bytes that were written (or would have been
36313631
* written if output had to be truncated due to string size),
36323632
* or a negative error in cases of failure.
3633+
*
3634+
* long bpf_seq_printf_btf(struct seq_file *m, struct btf_ptr *ptr, u32 ptr_size, u64 flags)
3635+
* Description
3636+
* Use BTF to write to seq_write a string representation of
3637+
* *ptr*->ptr, using *ptr*->type_id as per bpf_snprintf_btf().
3638+
* *flags* are identical to those used for bpf_snprintf_btf.
3639+
* Return
3640+
* 0 on success or a negative error in case of failure.
36333641
*/
36343642
#define __BPF_FUNC_MAPPER(FN) \
36353643
FN(unspec), \
@@ -3782,6 +3790,7 @@ union bpf_attr {
37823790
FN(d_path), \
37833791
FN(copy_from_user), \
37843792
FN(snprintf_btf), \
3793+
FN(seq_printf_btf), \
37853794
/* */
37863795

37873796
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

kernel/bpf/btf.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -5346,8 +5346,8 @@ static void btf_seq_show(struct btf_show *show, const char *fmt,
53465346
seq_vprintf((struct seq_file *)show->target, fmt, args);
53475347
}
53485348

5349-
static int btf_type_seq_show_flags(const struct btf *btf, u32 type_id,
5350-
void *obj, struct seq_file *m, u64 flags)
5349+
int btf_type_seq_show_flags(const struct btf *btf, u32 type_id,
5350+
void *obj, struct seq_file *m, u64 flags)
53515351
{
53525352
struct btf_show sseq;
53535353

kernel/bpf/core.c

+1
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,7 @@ const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto __weak;
22172217
const struct bpf_func_proto bpf_get_local_storage_proto __weak;
22182218
const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto __weak;
22192219
const struct bpf_func_proto bpf_snprintf_btf_proto __weak;
2220+
const struct bpf_func_proto bpf_seq_printf_btf_proto __weak;
22202221

22212222
const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
22222223
{

kernel/trace/bpf_trace.c

+33
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
7171
u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
7272
u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
7373

74+
static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
75+
u64 flags, const struct btf **btf,
76+
s32 *btf_id);
77+
7478
/**
7579
* trace_call_bpf - invoke BPF program
7680
* @call: tracepoint event
@@ -776,6 +780,31 @@ static const struct bpf_func_proto bpf_seq_write_proto = {
776780
.arg3_type = ARG_CONST_SIZE_OR_ZERO,
777781
};
778782

783+
BPF_CALL_4(bpf_seq_printf_btf, struct seq_file *, m, struct btf_ptr *, ptr,
784+
u32, btf_ptr_size, u64, flags)
785+
{
786+
const struct btf *btf;
787+
s32 btf_id;
788+
int ret;
789+
790+
ret = bpf_btf_printf_prepare(ptr, btf_ptr_size, flags, &btf, &btf_id);
791+
if (ret)
792+
return ret;
793+
794+
return btf_type_seq_show_flags(btf, btf_id, ptr->ptr, m, flags);
795+
}
796+
797+
static const struct bpf_func_proto bpf_seq_printf_btf_proto = {
798+
.func = bpf_seq_printf_btf,
799+
.gpl_only = true,
800+
.ret_type = RET_INTEGER,
801+
.arg1_type = ARG_PTR_TO_BTF_ID,
802+
.arg1_btf_id = &btf_seq_file_ids[0],
803+
.arg2_type = ARG_PTR_TO_MEM,
804+
.arg3_type = ARG_CONST_SIZE_OR_ZERO,
805+
.arg4_type = ARG_ANYTHING,
806+
};
807+
779808
static __always_inline int
780809
get_map_perf_counter(struct bpf_map *map, u64 flags,
781810
u64 *value, u64 *enabled, u64 *running)
@@ -1695,6 +1724,10 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
16951724
return prog->expected_attach_type == BPF_TRACE_ITER ?
16961725
&bpf_seq_write_proto :
16971726
NULL;
1727+
case BPF_FUNC_seq_printf_btf:
1728+
return prog->expected_attach_type == BPF_TRACE_ITER ?
1729+
&bpf_seq_printf_btf_proto :
1730+
NULL;
16981731
case BPF_FUNC_d_path:
16991732
return &bpf_d_path_proto;
17001733
default:

tools/include/uapi/linux/bpf.h

+9
Original file line numberDiff line numberDiff line change
@@ -3630,6 +3630,14 @@ union bpf_attr {
36303630
* The number of bytes that were written (or would have been
36313631
* written if output had to be truncated due to string size),
36323632
* or a negative error in cases of failure.
3633+
*
3634+
* long bpf_seq_printf_btf(struct seq_file *m, struct btf_ptr *ptr, u32 ptr_size, u64 flags)
3635+
* Description
3636+
* Use BTF to write to seq_write a string representation of
3637+
* *ptr*->ptr, using *ptr*->type_id as per bpf_snprintf_btf().
3638+
* *flags* are identical to those used for bpf_snprintf_btf.
3639+
* Return
3640+
* 0 on success or a negative error in case of failure.
36333641
*/
36343642
#define __BPF_FUNC_MAPPER(FN) \
36353643
FN(unspec), \
@@ -3782,6 +3790,7 @@ union bpf_attr {
37823790
FN(d_path), \
37833791
FN(copy_from_user), \
37843792
FN(snprintf_btf), \
3793+
FN(seq_printf_btf), \
37853794
/* */
37863795

37873796
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)