fix build error in bpf selftests
cover fix build error in bpf selftests
We found a compile error when building tools/testing/selftests/bpf/ on 5.10.y. tools/testing/selftests/bpf/prog_tests/sk_lookup.c:1092:15: error: 'struct bpf_sk_lookup' has no member named 'cookie'
1092 | if (CHECK(ctx.cookie == 0, "ctx.cookie", "no socket selected\n"))
| ^
To fix this bug, this patchset backports three patches from upstream:
https://lore.kernel.org/bpf/[email protected]/
Patch 1 and 2 are necessary for bpf selftests build pass on 5.10.y. Patch 3 does not impact building stage, but can avoid a test case failure (by skipping it).
Lorenz Bauer (3):
bpf: Consolidate shared test timing code
bpf: Add PROG_TEST_RUN support for sk_lookup programs
selftests: bpf: Don't run sk_lookup in verifier tests
include/linux/bpf.h | 10 + include/uapi/linux/bpf.h | 5 +- net/bpf/test_run.c | 243 +++++++++++++----- net/core/filter.c | 1 + tools/include/uapi/linux/bpf.h | 5 +- tools/testing/selftests/bpf/test_verifier.c | 4 +- .../selftests/bpf/verifier/ctx_sk_lookup.c | 1 + 7 files changed, 204 insertions(+), 65 deletions(-)
Signature · 1 line
2.27.0
PATCH 1/3 bpf: Consolidate shared test timing code
From: Lorenz Bauer <[email protected]>
commit 607b9cc92bd7208338d714a22b8082fe83bcb177 upstream.
Share the timing / signal interruption logic between different implementations of PROG_TEST_RUN. There is a change in behaviour as well. We check the loop exit condition before checking for pending signals. This resolves an edge case where a signal arrives during the last iteration. Instead of aborting with EINTR we return the successful result to user space.
--- net/bpf/test_run.c | 140 +++++++++++++++++++++++++-------------------- 1 file changed, 78 insertions(+), 62 deletions(-)
net/bpf/test_run.c +78 -62
@@ -16,14 +16,78 @@#define CREATE_TRACE_POINTS #include <trace/events/bpf_test_run.h> +struct bpf_test_timer { +enum {NO_PREEMPT ,NO_MIGRATE } mode; +u32 i; +u64 time_start, time_spent; +}; + +static void bpf_test_timer_enter (struct bpf_test_timer *t) +__acquires (rcu) +{ +rcu_read_lock (); +if (t->mode ==NO_PREEMPT ) +preempt_disable (); +else +migrate_disable (); + + t->time_start =ktime_get_ns (); +} + +static void bpf_test_timer_leave (struct bpf_test_timer *t) +__releases (rcu) +{ + t->time_start =0 ; + +if (t->mode ==NO_PREEMPT ) +preempt_enable (); +else +migrate_enable (); +rcu_read_unlock (); +} + +static bool bpf_test_timer_continue (struct bpf_test_timer *t,u32 repeat,int *err,u32 *duration) +__must_hold (rcu) +{ + t->i++; +if (t->i >= repeat) { +/* We're done. */ + t->time_spent +=ktime_get_ns () - t->time_start; +do_div (t->time_spent, t->i); + *duration = t->time_spent >U32_MAX ?U32_MAX : (u32 )t->time_spent; + *err =0 ; + goto reset; + } + +if (signal_pending (current)) { +/* During iteration: we've been cancelled, abort. */ + *err = -EINTR ; + goto reset; + } + +if (need_resched ()) { +/* During iteration: we need to reschedule between runs. */ + t->time_spent +=ktime_get_ns () - t->time_start; +bpf_test_timer_leave (t); +cond_resched (); +bpf_test_timer_enter (t); + } + +/* Do another round. */ +return true; + +reset: + t->i =0 ; +return false; +} +static int bpf_test_run (struct bpf_prog *prog,void *ctx,u32 repeat,u32 *retval,u32 *time,bool xdp) {struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE ] = {NULL }; +struct bpf_test_timer t = {NO_MIGRATE };enum bpf_cgroup_storage_type stype; -u64 time_start, time_spent =0 ; -int ret =0 ; -u32 i; +int ret;for_each_cgroup_storage_type (stype) { storage[stype] =bpf_cgroup_storage_alloc (prog, stype);
@@ -38,10 +102,8 @@ static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,if (!repeat) repeat =1 ; -rcu_read_lock (); -migrate_disable (); - time_start =ktime_get_ns (); -for (i =0 ; i < repeat; i++) { +bpf_test_timer_enter (&t); + do { ret = bpf_cgroup_storage_set(storage); if (ret) break;
@@ -53,29 +115,8 @@ static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,bpf_cgroup_storage_unset (); -if (signal_pending (current)) { - ret = -EINTR ; -break ; - } - -if (need_resched ()) { - time_spent +=ktime_get_ns () - time_start; -migrate_enable (); -rcu_read_unlock (); - -cond_resched (); - -rcu_read_lock (); -migrate_disable (); - time_start =ktime_get_ns (); - } - } - time_spent +=ktime_get_ns () - time_start; -migrate_enable (); -rcu_read_unlock (); - -do_div (time_spent, repeat); - *time = time_spent >U32_MAX ?U32_MAX : (u32 )time_spent; + }while (bpf_test_timer_continue (&t, repeat, &ret, time)); +bpf_test_timer_leave (&t);for_each_cgroup_storage_type (stype)bpf_cgroup_storage_free (storage[stype]);
@@ -688,18 +729,17 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,const union bpf_attr *kattr, union bpf_attr __user *uattr) { + struct bpf_test_timer t = { NO_PREEMPT };u32 size = kattr->test.data_size_in;struct bpf_flow_dissector ctx = {};u32 repeat = kattr->test.repeat;struct bpf_flow_keys *user_ctx;struct bpf_flow_keys flow_keys; -u64 time_start, time_spent =0 ;const struct ethhdr *eth;unsigned int flags =0 ;u32 retval, duration;void *data;int ret; -u32 i;if (prog->type !=BPF_PROG_TYPE_FLOW_DISSECTOR )return -EINVAL ;
@@ -735,39 +775,15 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, ctx.data = data; ctx.data_end = (__u8 *)data + size; -rcu_read_lock (); -preempt_disable (); - time_start =ktime_get_ns (); -for (i =0 ; i < repeat; i++) { +bpf_test_timer_enter (&t); +do { retval =bpf_flow_dissect (prog, &ctx, eth->h_proto,ETH_HLEN , size, flags); + }while (bpf_test_timer_continue (&t, repeat, &ret, &duration)); +bpf_test_timer_leave (&t); -if (signal_pending (current)) { -preempt_enable (); -rcu_read_unlock (); - - ret = -EINTR ; - goto out; - } - -if (need_resched ()) { - time_spent +=ktime_get_ns () - time_start; -preempt_enable (); -rcu_read_unlock (); - -cond_resched (); - -rcu_read_lock (); -preempt_disable (); - time_start =ktime_get_ns (); - } - } - time_spent +=ktime_get_ns () - time_start; -preempt_enable (); -rcu_read_unlock (); - -do_div (time_spent, repeat); - duration = time_spent >U32_MAX ?U32_MAX : (u32 )time_spent; +if (ret <0 ) + goto out; ret =bpf_test_finish (kattr, uattr, &flow_keys,sizeof (flow_keys), retval, duration);
Signature · 1 line
2.27.0
PATCH 2/3 bpf: Add PROG_TEST_RUN support for sk_lookup programs
In reply to Tianchen DingFrom: Lorenz Bauer <[email protected]>
commit 7c32e8f8bc33a5f4b113a630857e46634e3e143b upstream.
Allow to pass sk_lookup programs to PROG_TEST_RUN. User space provides the full bpf_sk_lookup struct as context. Since the context includes a socket pointer that can't be exposed to user space we define that PROG_TEST_RUN returns the cookie of the selected socket or zero in place of the socket pointer.
We don't support testing programs that select a reuseport socket, since this would mean running another (unrelated) BPF program from the sk_lookup test handler.
--- include/linux/bpf.h | 10 ++++ include/uapi/linux/bpf.h | 5 +- net/bpf/test_run.c | 105 +++++++++++++++++++++++++++++++++ net/core/filter.c | 1 + tools/include/uapi/linux/bpf.h | 5 +- 5 files changed, 124 insertions(+), 2 deletions(-)
include/linux/bpf.h +10 -0
@@ -1457,6 +1457,9 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,int bpf_prog_test_run_raw_tp (struct bpf_prog *prog,const union bpf_attr *kattr,union bpf_attr __user *uattr); +int bpf_prog_test_run_sk_lookup (struct bpf_prog *prog, +const union bpf_attr *kattr, +union bpf_attr __user *uattr);bool btf_ctx_access (int off,int size,enum bpf_access_type type,const struct bpf_prog *prog,struct bpf_insn_access_aux *info);
@@ -1671,6 +1674,13 @@ static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,return -ENOTSUPP ; } +static inline int bpf_prog_test_run_sk_lookup (struct bpf_prog *prog, +const union bpf_attr *kattr, +union bpf_attr __user *uattr) +{ +return -ENOTSUPP ; +} +static inline void bpf_map_put (struct bpf_map *map) { }
include/uapi/linux/bpf.h +4 -1
@@ -5007,7 +5007,10 @@ struct bpf_pidns_info {/* User accessible data for SK_LOOKUP programs. Add new fields at the end. */ struct bpf_sk_lookup { - __bpf_md_ptr(struct bpf_sock *, sk); /* Selected socket */ +union { + __bpf_md_ptr(struct bpf_sock *, sk); /* Selected socket */ +__u64 cookie;/* Non-zero if socket was selected in PROG_TEST_RUN */ + };__u32 family;/* Protocol family (AF_INET, AF_INET6) */ __u32 protocol;/* IP protocol (IPPROTO_TCP, IPPROTO_UDP) */
net/bpf/test_run.c +105 -0
116 lines not on this page · raw message
net/core/filter.c +1 -0
@@ -10334,6 +10334,7 @@ static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type, }const struct bpf_prog_ops sk_lookup_prog_ops = { + .test_run = bpf_prog_test_run_sk_lookup, };const struct bpf_verifier_ops sk_lookup_verifier_ops = {
tools/include/uapi/linux/bpf.h +4 -1
@@ -5006,7 +5006,10 @@ struct bpf_pidns_info {/* User accessible data for SK_LOOKUP programs. Add new fields at the end. */ struct bpf_sk_lookup { - __bpf_md_ptr(struct bpf_sock *, sk); /* Selected socket */ +union { + __bpf_md_ptr(struct bpf_sock *, sk); /* Selected socket */ +__u64 cookie;/* Non-zero if socket was selected in PROG_TEST_RUN */ + };__u32 family;/* Protocol family (AF_INET, AF_INET6) */ __u32 protocol;/* IP protocol (IPPROTO_TCP, IPPROTO_UDP) */
Signature · 1 line
2.27.0
PATCH 3/3 selftests: bpf: Don't run sk_lookup in verifier tests
In reply to Tianchen DingFrom: Lorenz Bauer <[email protected]>
commit b4f894633fa14d7d46ba7676f950b90a401504bb upstream.
sk_lookup doesn't allow setting data_in for bpf_prog_run. This doesn't play well with the verifier tests, since they always set a 64 byte input buffer. Allow not running verifier tests by setting bpf_test.runs to a negative value and don't run the ctx access case for sk_lookup. We have dedicated ctx access tests so skipping here doesn't reduce coverage.
--- tools/testing/selftests/bpf/test_verifier.c | 4 ++-- tools/testing/selftests/bpf/verifier/ctx_sk_lookup.c | 1 + 2 files changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/test_verifier.c +2 -2
@@ -100,7 +100,7 @@ struct bpf_test {enum bpf_prog_type prog_type;uint8_t flags;void (*fill_helper)(struct bpf_test *self); -uint8_t runs; +int runs;#define bpf_testdata_struct_t \ struct { \ uint32_t retval, retval_unpriv; \
@@ -1054,7 +1054,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv, run_errs =0 ; run_successes =0 ; -if (!alignment_prevented_execution && fd_prog >=0 ) { +if (!alignment_prevented_execution && fd_prog >=0 && test->runs >=0 ) {uint32_t expected_val;int i;
tools/testing/selftests/bpf/verifier/ctx_sk_lookup.c +1 -0
@@ -239,6 +239,7 @@ .result = ACCEPT, .prog_type = BPF_PROG_TYPE_SK_LOOKUP, .expected_attach_type = BPF_SK_LOOKUP, + .runs = -1, },/* invalid 8-byte reads from a 4-byte fields in bpf_sk_lookup */ {
Signature · 1 line
2.27.0
Greg Kroah-Hartman
maintainerQuoted 11 lines · Tianchen Ding
We found a compile error when building
tools/testing/selftests/bpf/on 5.10.y.tools/testing/selftests/bpf/prog_tests/sk_lookup.c:1092:15: error: 'structbpf_sk_lookup' has no member named 'cookie'
1092 | if (CHECK(ctx.cookie == 0, "ctx.cookie", "no socket selected\n"))
| ^To fix this bug, this patchset backports three patches from upstream:
https://lore.kernel.org/bpf/[email protected]/Patch 1 and 2 are necessary for bpf selftests build pass on 5.10.y. Patch 3 does not impact building stage, but can avoid a test case failure (by skipping it).
Now queued up, thanks.
greg k-h