Skip to content

Commit 1840e9d

Browse files
alan-maguirekernel-patches-bot
authored andcommitted
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]>
1 parent 1b82c13 commit 1840e9d

File tree

6 files changed

+58
-2
lines changed

6 files changed

+58
-2
lines changed

include/linux/btf.h

Lines changed: 2 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3621,6 +3621,15 @@ union bpf_attr {
36213621
* The number of bytes that were written (or would have been
36223622
* written if output had to be truncated due to string size),
36233623
* or a negative error in cases of failure.
3624+
*
3625+
* long bpf_seq_printf_btf(struct seq_file *m, struct btf_ptr *ptr, u32 ptr_size, u64 flags)
3626+
* Description
3627+
* Use BTF to write to seq_write a string representation of
3628+
* *ptr*->ptr, using *ptr*->type name or *ptr*->type_id as per
3629+
* bpf_snprintf_btf() above. *flags* are identical to those
3630+
* used for bpf_snprintf_btf.
3631+
* Return
3632+
* 0 on success or a negative error in case of failure.
36243633
*/
36253634
#define __BPF_FUNC_MAPPER(FN) \
36263635
FN(unspec), \
@@ -3773,6 +3782,7 @@ union bpf_attr {
37733782
FN(d_path), \
37743783
FN(copy_from_user), \
37753784
FN(snprintf_btf), \
3785+
FN(seq_printf_btf), \
37763786
/* */
37773787

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

kernel/bpf/btf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5316,8 +5316,8 @@ static __printf(2, 3) void btf_seq_show(struct btf_show *show, const char *fmt,
53165316
va_end(args);
53175317
}
53185318

5319-
static int btf_type_seq_show_flags(const struct btf *btf, u32 type_id,
5320-
void *obj, struct seq_file *m, u64 flags)
5319+
int btf_type_seq_show_flags(const struct btf *btf, u32 type_id,
5320+
void *obj, struct seq_file *m, u64 flags)
53215321
{
53225322
struct btf_show sseq;
53235323

kernel/bpf/core.c

Lines changed: 1 addition & 0 deletions
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

Lines changed: 33 additions & 0 deletions
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)
@@ -1731,6 +1760,10 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
17311760
return prog->expected_attach_type == BPF_TRACE_ITER ?
17321761
&bpf_seq_write_proto :
17331762
NULL;
1763+
case BPF_FUNC_seq_printf_btf:
1764+
return prog->expected_attach_type == BPF_TRACE_ITER ?
1765+
&bpf_seq_printf_btf_proto :
1766+
NULL;
17341767
case BPF_FUNC_d_path:
17351768
return &bpf_d_path_proto;
17361769
default:

tools/include/uapi/linux/bpf.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3621,6 +3621,15 @@ union bpf_attr {
36213621
* The number of bytes that were written (or would have been
36223622
* written if output had to be truncated due to string size),
36233623
* or a negative error in cases of failure.
3624+
*
3625+
* long bpf_seq_printf_btf(struct seq_file *m, struct btf_ptr *ptr, u32 ptr_size, u64 flags)
3626+
* Description
3627+
* Use BTF to write to seq_write a string representation of
3628+
* *ptr*->ptr, using *ptr*->type name or *ptr*->type_id as per
3629+
* bpf_snprintf_btf() above. *flags* are identical to those
3630+
* used for bpf_snprintf_btf.
3631+
* Return
3632+
* 0 on success or a negative error in case of failure.
36243633
*/
36253634
#define __BPF_FUNC_MAPPER(FN) \
36263635
FN(unspec), \
@@ -3773,6 +3782,7 @@ union bpf_attr {
37733782
FN(d_path), \
37743783
FN(copy_from_user), \
37753784
FN(snprintf_btf), \
3785+
FN(seq_printf_btf), \
37763786
/* */
37773787

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

0 commit comments

Comments
 (0)