bpf: implement variadic printk helper
Subsystems (11)
v6 cover bpf: implement variadic printk helper
This series introduces a new helper, bpf_trace_vprintk, which functions like bpf_trace_printk but supports > 3 arguments via a pseudo-vararg u64 array. The bpf_printk libbpf convenience macro is modified to use bpf_trace_vprintk when > 3 varargs are passed, otherwise the previous behavior - using bpf_trace_printk - is retained.
Helper functions and macros added during the implementation of bpf_seq_printf and bpf_snprintf do most of the heavy lifting for bpf_trace_vprintk. There's no novel format string wrangling here.
Usecase here is straightforward: Giving BPF program writers a more powerful printk will ease development of BPF programs, particularly during debugging and testing, where printk tends to be used.
This feature was proposed by Andrii in libbpf mirror's issue tracker [1].
[1] https://github.com/libbpf/libbpf/issues/315
v5 -> v6: Rebase to pick up newly-added helper
v4 -> v5:
* patch 8: added test for "%pS" format string w/ NULL fmt arg [Daniel]
* patch 8: dmesg -> /sys/kernel/debug/tracing/trace_pipe in commit message [Andrii]
* patch 9: squash into patch 8, remove the added test in favor of just bpf_printk'ing in patch 8's test [Andrii]
* migrate comment to /* */
* header comments improved$
* uapi/linux/bpf.h: u64 -> long return type [Daniel]
* uapi/linux/bpf.h: function description explains benefit of bpf_trace_vprintk over bpf_trace_printk [Daniel]
* uapi/linux/bpf.h: added patch explaining that data_len should be a multiple of 8 in bpf_seq_printf, bpf_snprintf descriptions [Daniel]
* tools/lib/bpf/bpf_helpers.h: move comment to new bpf_printk [Andrii]
* rebase
v3 -> v4:
* Add patch 2, which migrates reference_tracking prog_test away from bpf_program__load. Could be placed a bit later in the series, but wanted to keep the actual vprintk-related patches contiguous
* Add patch 9, which adds a program w/ 0 fmt arg bpf_printk to vprintk test
* bpf_printk convenience macro isn't multiline anymore, so simplify [Andrii]
* Add some comments to ___bpf_pick_printk to make it more obvious when implementation switches from printk to vprintk [Andrii]
* BPF_PRINTK_FMT_TYPE -> BPF_PRINTK_FMT_MOD for 'static const' fmt string in printk wrapper macro [Andrii]
* checkpatch.pl doesn't like this, says "Macros with complex values should be enclosed in parentheses". Strange that it didn't have similar complaints about v3's BPF_PRINTK_FMT_TYPE. Regardless, IMO the complaint is not highlighting a real issue in the case of this macro.
* Fix alignment of __bpf_vprintk and __bpf_pick_printk [Andrii]
* rebase
v2 -> v3:
* Clean up patch 3's commit message [Alexei]
* Add patch 4, which modifies __bpf_printk to use 'static const char' to store fmt string with fallback for older kernels [Andrii]
* rebase
v1 -> v2:
* Naming conversation seems to have gone in favor of keeping bpf_trace_vprintk, names are unchanged
* Patch 3 now modifies bpf_printk convenience macro to choose between __bpf_printk and __bpf_vprintk 'implementation' macros based on arg count. __bpf_vprintk is a renaming of bpf_vprintk convenience macro from v1, __bpf_printk is the existing bpf_printk implementation.
This patch could use some scrutiny as I think current implementation may regress developer experience in a specific case, turning a compile-time error into a load-time error. Unclear to me how common the case is, or whether the macro magic I chose is ideal.
* char ___fmt[] to static const char ___fmt[] change was not done, wanted to leave __bpf_printk 'implementation' macro unchanged for v2 to ease discussion of above point
* Removed __always_inline from __set_printk_clr_event [Andrii]
* Simplified bpf_trace_printk docstring to refer to other functions instead of copy/pasting and avoid specifying 12 vararg limit [Andrii]
* Migrated trace_printk selftest to use ASSERT_ instead of CHECK
* Adds new patch 5, previous patch 5 is now 6
* Migrated trace_vprintk selftest to use ASSERT_ instead of CHECK, open_and_load instead of separate open, load [Andrii]
* Patch 2's commit message now correctly mentions trace_pipe instead of dmesg [Andrii]
Dave Marchevsky (9):
bpf: merge printk and seq_printf VARARG max macros selftests/bpf: stop using bpf_program__load
bpf: add bpf_trace_vprintk helper
libbpf: Modify bpf_printk to choose helper based on arg count
libbpf: use static const fmt string in __bpf_printk
bpftool: only probe trace_vprintk feature in 'full' mode selftests/bpf: Migrate prog_tests/trace_printk CHECKs to ASSERTs selftests/bpf: add trace_vprintk test prog
bpf: Clarify data_len param in bpf_snprintf and bpf_seq_printf
comments
include/linux/bpf.h | 3 + include/uapi/linux/bpf.h | 16 ++++- kernel/bpf/core.c | 5 ++ kernel/bpf/helpers.c | 6 +- kernel/trace/bpf_trace.c | 54 ++++++++++++++- tools/bpf/bpftool/feature.c | 1 + tools/include/uapi/linux/bpf.h | 16 ++++- tools/lib/bpf/bpf_helpers.h | 51 +++++++++++--- tools/testing/selftests/bpf/Makefile | 3 +- .../bpf/prog_tests/reference_tracking.c | 39 ++++++++--- .../selftests/bpf/prog_tests/trace_printk.c | 24 +++---- .../selftests/bpf/prog_tests/trace_vprintk.c | 68 +++++++++++++++++++ .../selftests/bpf/progs/trace_vprintk.c | 33 +++++++++ tools/testing/selftests/bpf/test_bpftool.py | 22 +++--- 14 files changed, 286 insertions(+), 55 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/trace_vprintk.c create mode 100644 tools/testing/selftests/bpf/progs/trace_vprintk.c
Signature · 1 line
2.30.2
PATCH v6 1/9 bpf: merge printk and seq_printf VARARG max macros
MAX_SNPRINTF_VARARGS and MAX_SEQ_PRINTF_VARARGS are used by bpf helpers bpf_snprintf and bpf_seq_printf to limit their varargs. Both call into bpf_bprintf_prepare for print formatting logic and have convenience macros in libbpf (BPF_SNPRINTF, BPF_SEQ_PRINTF) which use the same helper macros to convert varargs to a byte array.
Changing shared functionality to support more varargs for either bpf helper would affect the other as well, so let's combine the _VARARGS macros to make this more obvious.
--- include/linux/bpf.h | 2 ++ kernel/bpf/helpers.c | 4 +--- kernel/trace/bpf_trace.c | 4 +--- 3 files changed, 4 insertions(+), 6 deletions(-)
include/linux/bpf.h +2 -0
@@ -2216,6 +2216,8 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,struct btf_id_set ;bool btf_id_set_contains (const struct btf_id_set *set,u32 id); +#define MAX_BPRINTF_VARARGS 12 +int bpf_bprintf_prepare (char *fmt,u32 fmt_size,const u64 *raw_args,u32 **bin_buf,u32 num_args);void bpf_bprintf_cleanup (void );
kernel/bpf/helpers.c +1 -3
@@ -979,15 +979,13 @@ int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,return err; } -#define MAX_SNPRINTF_VARARGS 12 - BPF_CALL_5(bpf_snprintf, char *, str, u32, str_size, char *, fmt, const void *, data, u32, data_len) {int err, num_args;u32 *bin_args; -if (data_len %8 || data_len >MAX_SNPRINTF_VARARGS *8 || +if (data_len %8 || data_len >MAX_BPRINTF_VARARGS *8 || (data_len && !data))return -EINVAL ; num_args = data_len /8 ;
kernel/trace/bpf_trace.c +1 -3
@@ -414,15 +414,13 @@ const struct bpf_func_proto *bpf_get_trace_printk_proto(void)return &bpf_trace_printk_proto; } -#define MAX_SEQ_PRINTF_VARARGS 12 - BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size, const void *, data, u32, data_len) {int err, num_args;u32 *bin_args; -if (data_len &7 || data_len >MAX_SEQ_PRINTF_VARARGS *8 || +if (data_len &7 || data_len >MAX_BPRINTF_VARARGS *8 || (data_len && !data))return -EINVAL ; num_args = data_len /8 ;
Signature · 1 line
2.30.2
PATCH v6 2/9 selftests/bpf: stop using bpf_program__load
In reply to Dave Marchevskybpf_program__load is not supposed to be used directly. Replace it with bpf_object__ APIs for the reference_tracking prog_test, which is the last offender in bpf selftests.
Some additional complexity is added for this test, namely the use of one bpf_object to iterate through progs, while a second bpf_object is created and opened/closed to test actual loading of progs. This is because the test was doing bpf_program__load then __unload to test loading of individual progs and same semantics with bpf_object__load/__unload result in failure to load an __unload-ed obj.
--- .../bpf/prog_tests/reference_tracking.c | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-)
tools/testing/selftests/bpf/prog_tests/reference_tracking.c +31 -8
@@ -1,6 +1,21 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +static void toggle_object_autoload_progs (const struct bpf_object *obj, +const char *title_load) +{ +struct bpf_program *prog; + +bpf_object__for_each_program (prog, obj) { +const char *title =bpf_program__section_name (prog); + +if (!strcmp (title_load, title)) +bpf_program__set_autoload (prog, true); +else +bpf_program__set_autoload (prog, false); + } +} +void test_reference_tracking (void ) {const char *file ="test_sk_lookup_kern.o" ;
@@ -9,21 +24,21 @@ void test_reference_tracking(void) .object_name = obj_name, .relaxed_maps = true, ); -struct bpf_object *obj; +struct bpf_object *obj_iter, *obj =NULL ;struct bpf_program *prog;__u32 duration =0 ;int err =0 ; - obj =bpf_object__open_file (file, &open_opts); -if (!ASSERT_OK_PTR (obj,"obj_open_file" )) + obj_iter =bpf_object__open_file (file, &open_opts); +if (!ASSERT_OK_PTR (obj_iter,"obj_iter_open_file" ))return ; -if (CHECK (strcmp (bpf_object__name (obj), obj_name),"obj_name" , +if (CHECK (strcmp (bpf_object__name (obj_iter), obj_name),"obj_name" ,"wrong obj name '%s', expected '%s'\n" , -bpf_object__name (obj), obj_name)) +bpf_object__name (obj_iter), obj_name)) goto cleanup; -bpf_object__for_each_program (prog, obj) { +bpf_object__for_each_program (prog, obj_iter) {const char *title;/* Ignore .text sections */
@@ -34,19 +49,27 @@ void test_reference_tracking(void)if (!test__start_subtest (title))continue ; + obj =bpf_object__open_file (file, &open_opts); +if (!ASSERT_OK_PTR (obj,"obj_open_file" )) + goto cleanup; + +toggle_object_autoload_progs (obj, title);/* Expect verifier failure if test name has 'err' */ if (strstr (title,"err_" ) !=NULL ) {libbpf_print_fn_t old_print_fn; old_print_fn =libbpf_set_print (NULL ); - err = !bpf_program__load (prog,"GPL" ,0 ); + err = !bpf_object__load (obj);libbpf_set_print (old_print_fn); }else { - err =bpf_program__load (prog,"GPL" ,0 ); + err =bpf_object__load (obj); }CHECK (err, title,"\n" ); +bpf_object__close (obj); + obj =NULL ; } cleanup:bpf_object__close (obj); +bpf_object__close (obj_iter); }
Signature · 1 line
2.30.2
PATCH v6 3/9 bpf: add bpf_trace_vprintk helper
In reply to Dave MarchevskyThis helper is meant to be "bpf_trace_printk, but with proper vararg support". Follow bpf_snprintf's example and take a u64 pseudo-vararg array. Write to /sys/kernel/debug/tracing/trace_pipe using the same mechanism as bpf_trace_printk. The functionality of this helper was requested in the libbpf issue tracker [0].
[0] Closes: https://github.com/libbpf/libbpf/issues/315
--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 11 +++++++ kernel/bpf/core.c | 5 ++++ kernel/bpf/helpers.c | 2 ++ kernel/trace/bpf_trace.c | 52 +++++++++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 11 +++++++ 6 files changed, 81 insertions(+), 1 deletion(-)
include/linux/bpf.h +1 -0
@@ -1088,6 +1088,7 @@ bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fint bpf_prog_calc_tag (struct bpf_prog *fp);const struct bpf_func_proto *bpf_get_trace_printk_proto (void ); +const struct bpf_func_proto *bpf_get_trace_vprintk_proto (void );typedef unsigned long (*bpf_ctx_copy_t )(void *dst,const void *src,unsigned long off,unsigned long len);
include/uapi/linux/bpf.h +11 -0
@@ -4898,6 +4898,16 @@ union bpf_attr {* **-EINVAL** if *flags* is not zero. * * **-ENOENT** if architecture does not support branch records. +* +* long bpf_trace_vprintk(const char *fmt, u32 fmt_size, const void *data, u32 data_len) +* Description +* Behaves like **bpf_trace_printk**\ () helper, but takes an array of u64 +* to format and can handle more format args as a result. +* +* Arguments are to be used as in **bpf_seq_printf**\ () helper. +* Return +* The number of bytes written to the buffer, or a negative error +* in case of failure. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5077,6 +5087,7 @@ union bpf_attr {FN (get_attach_cookie), \FN (task_pt_regs), \FN (get_branch_snapshot), \ +FN (trace_vprintk), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/core.c +5 -0
@@ -2357,6 +2357,11 @@ const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)return NULL ; } +const struct bpf_func_proto *__weak bpf_get_trace_vprintk_proto (void ) +{ +return NULL ; +} +u64 __weak bpf_event_output (struct bpf_map *map,u64 flags,void *meta,u64 meta_size,void *ctx,u64 ctx_size,bpf_ctx_copy_t ctx_copy)
kernel/bpf/helpers.c +2 -0
@@ -1435,6 +1435,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_snprintf_proto;case BPF_FUNC_task_pt_regs:return &bpf_task_pt_regs_proto; +case BPF_FUNC_trace_vprintk: +return bpf_get_trace_vprintk_proto ();default :return NULL ; }
kernel/trace/bpf_trace.c +51 -1
74 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +11 -0
@@ -4898,6 +4898,16 @@ union bpf_attr {* **-EINVAL** if *flags* is not zero. * * **-ENOENT** if architecture does not support branch records. +* +* long bpf_trace_vprintk(const char *fmt, u32 fmt_size, const void *data, u32 data_len) +* Description +* Behaves like **bpf_trace_printk**\ () helper, but takes an array of u64 +* to format and can handle more format args as a result. +* +* Arguments are to be used as in **bpf_seq_printf**\ () helper. +* Return +* The number of bytes written to the buffer, or a negative error +* in case of failure. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5077,6 +5087,7 @@ union bpf_attr {FN (get_attach_cookie), \FN (task_pt_regs), \FN (get_branch_snapshot), \ +FN (trace_vprintk), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.30.2
PATCH v6 4/9 libbpf: Modify bpf_printk to choose helper based on arg count
In reply to Dave MarchevskyInstead of being a thin wrapper which calls into bpf_trace_printk, libbpf's bpf_printk convenience macro now chooses between bpf_trace_printk and bpf_trace_vprintk. If the arg count (excluding format string) is >3, use bpf_trace_vprintk, otherwise use the older helper.
The motivation behind this added complexity - instead of migrating entirely to bpf_trace_vprintk - is to maintain good developer experience for users compiling against new libbpf but running on older kernels. Users who are passing <=3 args to bpf_printk will see no change in their bytecode.
__bpf_vprintk functions similarly to BPF_SEQ_PRINTF and BPF_SNPRINTF macros elsewhere in the file - it allows use of bpf_trace_vprintk without manual conversion of varargs to u64 array. Previous implementation of bpf_printk macro is moved to __bpf_printk for use by the new implementation.
This does change behavior of bpf_printk calls with >3 args in the "new libbpf, old kernels" scenario. Before this patch, attempting to use 4 args to bpf_printk results in a compile-time error. After this patch, using bpf_printk with 4 args results in a trace_vprintk helper call being emitted and a load-time failure on older kernels.
--- tools/lib/bpf/bpf_helpers.h | 45 ++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 8 deletions(-)
tools/lib/bpf/bpf_helpers.h +37 -8
@@ -14,14 +14,6 @@#define __type (name, val) typeof(val) *name#define __array (name, val) typeof(val) *name[] -/* Helper macro to print out debug messages */ -#define bpf_printk (fmt, ...) \ -({ \ - char ____fmt[] = fmt; \ - bpf_trace_printk(____fmt, sizeof(____fmt), \ - ##__VA_ARGS__); \ -}) -/* * Helper macro to place programs, maps, license in * different sections in elf_bpf file. Section names
@@ -224,4 +216,41 @@ enum libbpf_tristate { ___param, sizeof(___param)); \ }) +#define __bpf_printk (fmt, ...) \ +({ \ + char ____fmt[] = fmt; \ + bpf_trace_printk(____fmt, sizeof(____fmt), \ + ##__VA_ARGS__); \ +}) + +/* +* __bpf_vprintk wraps the bpf_trace_vprintk helper with variadic arguments +* instead of an array of u64. +*/ +#define __bpf_vprintk(fmt, args...) \ +({ \ + static const char ___fmt[] = fmt; \ + unsigned long long ___param[___bpf_narg(args)]; \ + \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ + ___bpf_fill(___param, args); \ + _Pragma("GCC diagnostic pop") \ + \ + bpf_trace_vprintk(___fmt, sizeof(___fmt), \ + ___param, sizeof(___param)); \ +}) + +/* Use __bpf_printk when bpf_printk call has 3 or fewer fmt args +* Otherwise use __bpf_vprintk +*/ +#define ___bpf_pick_printk(...) \ + ___bpf_nth(_, ##__VA_ARGS__, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \ + __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \ + __bpf_vprintk, __bpf_vprintk, __bpf_printk /*3*/, __bpf_printk /*2*/,\ + __bpf_printk /*1*/, __bpf_printk /*0*/) + +/* Helper macro to print out debug messages */ +#define bpf_printk(fmt, args...) ___bpf_pick_printk(args)(fmt, ##args) +#endif
Signature · 1 line
2.30.2
PATCH v6 5/9 libbpf: use static const fmt string in __bpf_printk
In reply to Dave MarchevskyThe __bpf_printk convenience macro was using a 'char' fmt string holder as it predates support for globals in libbpf. Move to more efficient 'static const char', but provide a fallback to the old way via BPF_NO_GLOBAL_DATA so users on old kernels can still use the macro.
--- tools/lib/bpf/bpf_helpers.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/lib/bpf/bpf_helpers.h +7 -1
@@ -216,9 +216,15 @@ enum libbpf_tristate { ___param, sizeof(___param)); \ }) +#ifdef BPF_NO_GLOBAL_DATA +#define BPF_PRINTK_FMT_MOD +#else +#define BPF_PRINTK_FMT_MOD static const +#endif +#define __bpf_printk (fmt, ...) \ ({ \ - char ____fmt[] = fmt; \ + BPF_PRINTK_FMT_MOD char ____fmt[] = fmt; \ bpf_trace_printk(____fmt, sizeof(____fmt), \ ##__VA_ARGS__); \ })
Signature · 1 line
2.30.2
PATCH v6 6/9 bpftool: only probe trace_vprintk feature in 'full' mode
In reply to Dave MarchevskySince commit 368cb0e7cdb5e ("bpftool: Make probes which emit dmesg warnings optional"), some helpers aren't probed by bpftool unless full arg is added to bpftool feature probe.
bpf_trace_vprintk can emit dmesg warnings when probed, so include it.
--- tools/bpf/bpftool/feature.c | 1 + tools/testing/selftests/bpf/test_bpftool.py | 22 +++++++++------------ 2 files changed, 10 insertions(+), 13 deletions(-)
tools/bpf/bpftool/feature.c +1 -0
@@ -624,6 +624,7 @@ probe_helpers_for_progtype(enum bpf_prog_type prog_type, bool supported_type,*/ switch (id) {case BPF_FUNC_trace_printk: +case BPF_FUNC_trace_vprintk:case BPF_FUNC_probe_write_user:if (!full_mode)continue ;
tools/testing/selftests/bpf/test_bpftool.py +9 -13
@@ -57,6 +57,11 @@ def default_iface(f): return f(*args, iface, **kwargs) return wrapper +DMESG_EMITTING_HELPERS = [ + "bpf_probe_write_user", + "bpf_trace_printk", + "bpf_trace_vprintk", + ] class TestBpftool(unittest.TestCase): @classmethod
@@ -67,10 +72,7 @@ class TestBpftool(unittest.TestCase): @default_iface def test_feature_dev_json(self, iface): - unexpected_helpers = [ - "bpf_probe_write_user", - "bpf_trace_printk", - ] + unexpected_helpers = DMESG_EMITTING_HELPERS expected_keys = [ "syscall_config", "program_types",
@@ -94,10 +96,7 @@ class TestBpftool(unittest.TestCase): bpftool_json(["feature", "probe"]), bpftool_json(["feature"]), ] - unexpected_helpers = [ - "bpf_probe_write_user", - "bpf_trace_printk", - ] + unexpected_helpers = DMESG_EMITTING_HELPERS expected_keys = [ "syscall_config", "system_config",
@@ -121,10 +120,7 @@ class TestBpftool(unittest.TestCase): bpftool_json(["feature", "probe", "kernel", "full"]), bpftool_json(["feature", "probe", "full"]), ] - expected_helpers = [ - "bpf_probe_write_user", - "bpf_trace_printk", - ] + expected_helpers = DMESG_EMITTING_HELPERS for tc in test_cases: # Check if expected helpers are included at least once in any
@@ -157,7 +153,7 @@ class TestBpftool(unittest.TestCase): not_full_set.add(helper) self.assertCountEqual(full_set - not_full_set, - {"bpf_probe_write_user", "bpf_trace_printk"}) + set(DMESG_EMITTING_HELPERS)) self.assertCountEqual(not_full_set - full_set, set()) def test_feature_macros(self):
Signature · 1 line
2.30.2
PATCH v6 7/9 selftests/bpf: Migrate prog_tests/trace_printk CHECKs to ASSERTs
In reply to Dave MarchevskyGuidance for new tests is to use ASSERT macros instead of CHECK. Since trace_vprintk test will borrow heavily from trace_printk's, migrate its CHECKs so it remains obvious that the two are closely related.
--- .../selftests/bpf/prog_tests/trace_printk.c | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-)
tools/testing/selftests/bpf/prog_tests/trace_printk.c +9 -15
@@ -10,7 +10,7 @@void test_trace_printk (void ) { -int err, iter =0 , duration =0 , found =0 ; +int err =0 , iter =0 , found =0 ;struct trace_printk__bss *bss;struct trace_printk *skel;char *buf =NULL ;
@@ -18,25 +18,24 @@ void test_trace_printk(void)size_t buflen; skel =trace_printk__open (); -if (CHECK (!skel,"skel_open" ,"failed to open skeleton\n" )) +if (!ASSERT_OK_PTR (skel,"trace_printk__open" ))return ; -ASSERT_EQ (skel->rodata->fmt[0 ],'T' ,"invalid printk fmt string" ); +ASSERT_EQ (skel->rodata->fmt[0 ],'T' ,"skel->rodata->fmt[0]" ); skel->rodata->fmt[0 ] ='t' ; err =trace_printk__load (skel); -if (CHECK (err,"skel_load" ,"failed to load skeleton: %d\n" , err)) +if (!ASSERT_OK (err,"trace_printk__load" )) goto cleanup; bss = skel->bss; err =trace_printk__attach (skel); -if (CHECK (err,"skel_attach" ,"skeleton attach failed: %d\n" , err)) +if (!ASSERT_OK (err,"trace_printk__attach" )) goto cleanup; fp =fopen (TRACEBUF ,"r" ); -if (CHECK (fp ==NULL ,"could not open trace buffer" , -"error %d opening %s" , errno,TRACEBUF )) +if (!ASSERT_OK_PTR (fp,"fopen(TRACEBUF)" )) goto cleanup;/* We do not want to wait forever if this test fails... */
@@ -46,14 +45,10 @@ void test_trace_printk(void)usleep (1 );trace_printk__detach (skel); -if (CHECK (bss->trace_printk_ran ==0 , -"bpf_trace_printk never ran" , -"ran == %d" , bss->trace_printk_ran)) +if (!ASSERT_GT (bss->trace_printk_ran,0 ,"bss->trace_printk_ran" )) goto cleanup; -if (CHECK (bss->trace_printk_ret <=0 , -"bpf_trace_printk returned <= 0 value" , -"got %d" , bss->trace_printk_ret)) +if (!ASSERT_GT (bss->trace_printk_ret,0 ,"bss->trace_printk_ret" )) goto cleanup;/* verify our search string is in the trace buffer */
@@ -66,8 +61,7 @@ void test_trace_printk(void)break ; } -if (CHECK (!found,"message from bpf_trace_printk not found" , -"no instance of %s in %s" ,SEARCHMSG ,TRACEBUF )) +if (!ASSERT_EQ (found, bss->trace_printk_ran,"found" )) goto cleanup; cleanup:
Signature · 1 line
2.30.2
PATCH v6 8/9 selftests/bpf: add trace_vprintk test prog
In reply to Dave MarchevskyThis commit adds a test prog for vprintk which confirms that:
* bpf_trace_vprintk is writing to /sys/kernel/debug/tracing/trace_pipe
* __bpf_vprintk macro works as expected
* >3 args are printed
* bpf_printk w/ 0 format args compiles
* bpf_trace_vprintk call w/ a fmt specifier but NULL fmt data fails
Approach and code are borrowed from trace_printk test.
--- tools/testing/selftests/bpf/Makefile | 3 +- .../selftests/bpf/prog_tests/trace_vprintk.c | 68 +++++++++++++++++++ .../selftests/bpf/progs/trace_vprintk.c | 33 +++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/trace_vprintk.c create mode 100644 tools/testing/selftests/bpf/progs/trace_vprintk.c
tools/testing/selftests/bpf/Makefile +2 -1
@@ -315,7 +315,8 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ linked_vars.skel.h linked_maps.skel.h LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ - test_ksyms_module.c test_ringbuf.c atomics.c trace_printk.c + test_ksyms_module.c test_ringbuf.c atomics.c trace_printk.c \ + trace_vprintk.c SKEL_BLACKLIST += $$(LSKELS) test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o
tools/testing/selftests/bpf/prog_tests/trace_vprintk.c new file +68 -0
68 lines not on this page · raw message
tools/testing/selftests/bpf/progs/trace_vprintk.c new file +33 -0
@@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +int null_data_vprintk_ret =0 ; +int trace_vprintk_ret =0 ; +int trace_vprintk_ran =0 ; + +SEC("fentry/__x64_sys_nanosleep") +int sys_enter (void *ctx) +{ +static const char one[] ="1" ; +static const char three[] ="3" ; +static const char five[] ="5" ; +static const char seven[] ="7" ; +static const char nine[] ="9" ; +static const char f[] ="%pS\n" ; + +/* runner doesn't search for \t, just ensure it compiles */ +bpf_printk ("\t" ); + + trace_vprintk_ret =__bpf_vprintk ("%s,%d,%s,%d,%s,%d,%s,%d,%s,%d %d\n" , + one,2 , three,4 , five,6 , seven,8 , nine,10 , ++trace_vprintk_ran); + +/* non-NULL fmt w/ NULL data should result in error */ + null_data_vprintk_ret =bpf_trace_vprintk (f,sizeof (f),NULL ,0 ); +return 0 ; +}
Signature · 1 line
2.30.2
PATCH v6 9/9 bpf: Clarify data_len param in bpf_snprintf and bpf_seq_printf comments
In reply to Dave MarchevskySince the data_len in these two functions is a byte len of the preceding u64 *data array, it must always be a multiple of 8. If this isn't the case both helpers error out, so let's make the requirement explicit so users don't need to infer it.
--- include/uapi/linux/bpf.h | 5 +++-- tools/include/uapi/linux/bpf.h | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-)
include/uapi/linux/bpf.h +3 -2
@@ -4046,7 +4046,7 @@ union bpf_attr {* arguments. The *data* are a **u64** array and corresponding format string * values are stored in the array. For strings and pointers where pointees * are accessed, only the pointer values are stored in the *data* array. -* The *data_len* is the size of *data* in bytes. +* The *data_len* is the size of *data* in bytes - must be a multiple of 8. * * Formats **%s**, **%p{i,I}{4,6}** requires to read kernel memory. * Reading kernel memory may fail due to either invalid address or
@@ -4751,7 +4751,8 @@ union bpf_attr {* Each format specifier in **fmt** corresponds to one u64 element * in the **data** array. For strings and pointers where pointees * are accessed, only the pointer values are stored in the *data* -* array. The *data_len* is the size of *data* in bytes. +* array. The *data_len* is the size of *data* in bytes - must be +* a multiple of 8. * * Formats **%s** and **%p{i,I}{4,6}** require to read kernel * memory. Reading kernel memory may fail due to either invalid
tools/include/uapi/linux/bpf.h +3 -2
@@ -4046,7 +4046,7 @@ union bpf_attr {* arguments. The *data* are a **u64** array and corresponding format string * values are stored in the array. For strings and pointers where pointees * are accessed, only the pointer values are stored in the *data* array. -* The *data_len* is the size of *data* in bytes. +* The *data_len* is the size of *data* in bytes - must be a multiple of 8. * * Formats **%s**, **%p{i,I}{4,6}** requires to read kernel memory. * Reading kernel memory may fail due to either invalid address or
@@ -4751,7 +4751,8 @@ union bpf_attr {* Each format specifier in **fmt** corresponds to one u64 element * in the **data** array. For strings and pointers where pointees * are accessed, only the pointer values are stored in the *data* -* array. The *data_len* is the size of *data* in bytes. +* array. The *data_len* is the size of *data* in bytes - must be +* a multiple of 8. * * Formats **%s** and **%p{i,I}{4,6}** require to read kernel * memory. Reading kernel memory may fail due to either invalid
Signature · 1 line
2.30.2
Alexei Starovoitov
AppliedQuoted 15 lines · Dave Marchevsky
This series introduces a new helper,
bpf_trace_vprintk, which functions likebpf_trace_printkbut supports > 3 arguments via a pseudo-vararg u64 array. Thebpf_printklibbpf convenience macro is modified to usebpf_trace_vprintkwhen > 3 varargs are passed, otherwise the previous behavior - usingbpf_trace_printk- is retained.Helper functions and macros added during the implementation of
bpf_seq_printfandbpf_snprintfdo most of the heavy lifting forbpf_trace_vprintk. There's no novel format string wrangling here.Usecase here is straightforward: Giving BPF program writers a more powerful printk will ease development of BPF programs, particularly during debugging and testing, where printk tends to be used.
Quoted 6 lines
This feature was proposed by Andrii in libbpf mirror's issue tracker [1].
[1] https://github.com/libbpf/libbpf/issues/315
v5 -> v6: Rebase to pick up newly-added helper
Applied. Thanks!