xref: /openbmc/linux/net/bpf/test_run.c (revision df561f6688fef775baa341a0f5d960becd248b11)
125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21cf1cae9SAlexei Starovoitov /* Copyright (c) 2017 Facebook
31cf1cae9SAlexei Starovoitov  */
41cf1cae9SAlexei Starovoitov #include <linux/bpf.h>
51cf1cae9SAlexei Starovoitov #include <linux/slab.h>
61cf1cae9SAlexei Starovoitov #include <linux/vmalloc.h>
71cf1cae9SAlexei Starovoitov #include <linux/etherdevice.h>
81cf1cae9SAlexei Starovoitov #include <linux/filter.h>
91cf1cae9SAlexei Starovoitov #include <linux/sched/signal.h>
106ac99e8fSMartin KaFai Lau #include <net/bpf_sk_storage.h>
112cb494a3SSong Liu #include <net/sock.h>
122cb494a3SSong Liu #include <net/tcp.h>
133d08b6f2SKP Singh #include <linux/error-injection.h>
141cf1cae9SAlexei Starovoitov 
15e950e843SMatt Mullins #define CREATE_TRACE_POINTS
16e950e843SMatt Mullins #include <trace/events/bpf_test_run.h>
17e950e843SMatt Mullins 
18df1a2cb7SStanislav Fomichev static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,
19f23c4b39SBjörn Töpel 			u32 *retval, u32 *time, bool xdp)
201cf1cae9SAlexei Starovoitov {
2171b91a50SBo YU 	struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { NULL };
228bad74f9SRoman Gushchin 	enum bpf_cgroup_storage_type stype;
231cf1cae9SAlexei Starovoitov 	u64 time_start, time_spent = 0;
24df1a2cb7SStanislav Fomichev 	int ret = 0;
25dcb40590SRoman Gushchin 	u32 i;
261cf1cae9SAlexei Starovoitov 
278bad74f9SRoman Gushchin 	for_each_cgroup_storage_type(stype) {
288bad74f9SRoman Gushchin 		storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
298bad74f9SRoman Gushchin 		if (IS_ERR(storage[stype])) {
308bad74f9SRoman Gushchin 			storage[stype] = NULL;
318bad74f9SRoman Gushchin 			for_each_cgroup_storage_type(stype)
328bad74f9SRoman Gushchin 				bpf_cgroup_storage_free(storage[stype]);
338bad74f9SRoman Gushchin 			return -ENOMEM;
348bad74f9SRoman Gushchin 		}
358bad74f9SRoman Gushchin 	}
36f42ee093SRoman Gushchin 
371cf1cae9SAlexei Starovoitov 	if (!repeat)
381cf1cae9SAlexei Starovoitov 		repeat = 1;
39df1a2cb7SStanislav Fomichev 
40df1a2cb7SStanislav Fomichev 	rcu_read_lock();
416eac7795SDavid Miller 	migrate_disable();
421cf1cae9SAlexei Starovoitov 	time_start = ktime_get_ns();
431cf1cae9SAlexei Starovoitov 	for (i = 0; i < repeat; i++) {
44df1a2cb7SStanislav Fomichev 		bpf_cgroup_storage_set(storage);
45f23c4b39SBjörn Töpel 
46f23c4b39SBjörn Töpel 		if (xdp)
47f23c4b39SBjörn Töpel 			*retval = bpf_prog_run_xdp(prog, ctx);
48f23c4b39SBjörn Töpel 		else
49df1a2cb7SStanislav Fomichev 			*retval = BPF_PROG_RUN(prog, ctx);
50df1a2cb7SStanislav Fomichev 
51df1a2cb7SStanislav Fomichev 		if (signal_pending(current)) {
52df1a2cb7SStanislav Fomichev 			ret = -EINTR;
531cf1cae9SAlexei Starovoitov 			break;
54df1a2cb7SStanislav Fomichev 		}
55df1a2cb7SStanislav Fomichev 
56df1a2cb7SStanislav Fomichev 		if (need_resched()) {
571cf1cae9SAlexei Starovoitov 			time_spent += ktime_get_ns() - time_start;
586eac7795SDavid Miller 			migrate_enable();
59df1a2cb7SStanislav Fomichev 			rcu_read_unlock();
60df1a2cb7SStanislav Fomichev 
611cf1cae9SAlexei Starovoitov 			cond_resched();
62df1a2cb7SStanislav Fomichev 
63df1a2cb7SStanislav Fomichev 			rcu_read_lock();
646eac7795SDavid Miller 			migrate_disable();
651cf1cae9SAlexei Starovoitov 			time_start = ktime_get_ns();
661cf1cae9SAlexei Starovoitov 		}
671cf1cae9SAlexei Starovoitov 	}
681cf1cae9SAlexei Starovoitov 	time_spent += ktime_get_ns() - time_start;
696eac7795SDavid Miller 	migrate_enable();
70df1a2cb7SStanislav Fomichev 	rcu_read_unlock();
71df1a2cb7SStanislav Fomichev 
721cf1cae9SAlexei Starovoitov 	do_div(time_spent, repeat);
731cf1cae9SAlexei Starovoitov 	*time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent;
741cf1cae9SAlexei Starovoitov 
758bad74f9SRoman Gushchin 	for_each_cgroup_storage_type(stype)
768bad74f9SRoman Gushchin 		bpf_cgroup_storage_free(storage[stype]);
77f42ee093SRoman Gushchin 
78df1a2cb7SStanislav Fomichev 	return ret;
791cf1cae9SAlexei Starovoitov }
801cf1cae9SAlexei Starovoitov 
8178e52272SDavid Miller static int bpf_test_finish(const union bpf_attr *kattr,
8278e52272SDavid Miller 			   union bpf_attr __user *uattr, const void *data,
831cf1cae9SAlexei Starovoitov 			   u32 size, u32 retval, u32 duration)
841cf1cae9SAlexei Starovoitov {
8578e52272SDavid Miller 	void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
861cf1cae9SAlexei Starovoitov 	int err = -EFAULT;
87b5a36b1eSLorenz Bauer 	u32 copy_size = size;
881cf1cae9SAlexei Starovoitov 
89b5a36b1eSLorenz Bauer 	/* Clamp copy if the user has provided a size hint, but copy the full
90b5a36b1eSLorenz Bauer 	 * buffer if not to retain old behaviour.
91b5a36b1eSLorenz Bauer 	 */
92b5a36b1eSLorenz Bauer 	if (kattr->test.data_size_out &&
93b5a36b1eSLorenz Bauer 	    copy_size > kattr->test.data_size_out) {
94b5a36b1eSLorenz Bauer 		copy_size = kattr->test.data_size_out;
95b5a36b1eSLorenz Bauer 		err = -ENOSPC;
96b5a36b1eSLorenz Bauer 	}
97b5a36b1eSLorenz Bauer 
98b5a36b1eSLorenz Bauer 	if (data_out && copy_to_user(data_out, data, copy_size))
991cf1cae9SAlexei Starovoitov 		goto out;
1001cf1cae9SAlexei Starovoitov 	if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
1011cf1cae9SAlexei Starovoitov 		goto out;
1021cf1cae9SAlexei Starovoitov 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
1031cf1cae9SAlexei Starovoitov 		goto out;
1041cf1cae9SAlexei Starovoitov 	if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
1051cf1cae9SAlexei Starovoitov 		goto out;
106b5a36b1eSLorenz Bauer 	if (err != -ENOSPC)
1071cf1cae9SAlexei Starovoitov 		err = 0;
1081cf1cae9SAlexei Starovoitov out:
109e950e843SMatt Mullins 	trace_bpf_test_finish(&err);
1101cf1cae9SAlexei Starovoitov 	return err;
1111cf1cae9SAlexei Starovoitov }
1121cf1cae9SAlexei Starovoitov 
113faeb2dceSAlexei Starovoitov /* Integer types of various sizes and pointer combinations cover variety of
114faeb2dceSAlexei Starovoitov  * architecture dependent calling conventions. 7+ can be supported in the
115faeb2dceSAlexei Starovoitov  * future.
116faeb2dceSAlexei Starovoitov  */
117e9ff9d52SJean-Philippe Menil __diag_push();
118e9ff9d52SJean-Philippe Menil __diag_ignore(GCC, 8, "-Wmissing-prototypes",
119e9ff9d52SJean-Philippe Menil 	      "Global functions as their definitions will be in vmlinux BTF");
120faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test1(int a)
121faeb2dceSAlexei Starovoitov {
122faeb2dceSAlexei Starovoitov 	return a + 1;
123faeb2dceSAlexei Starovoitov }
124faeb2dceSAlexei Starovoitov 
125faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test2(int a, u64 b)
126faeb2dceSAlexei Starovoitov {
127faeb2dceSAlexei Starovoitov 	return a + b;
128faeb2dceSAlexei Starovoitov }
129faeb2dceSAlexei Starovoitov 
130faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test3(char a, int b, u64 c)
131faeb2dceSAlexei Starovoitov {
132faeb2dceSAlexei Starovoitov 	return a + b + c;
133faeb2dceSAlexei Starovoitov }
134faeb2dceSAlexei Starovoitov 
135faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test4(void *a, char b, int c, u64 d)
136faeb2dceSAlexei Starovoitov {
137faeb2dceSAlexei Starovoitov 	return (long)a + b + c + d;
138faeb2dceSAlexei Starovoitov }
139faeb2dceSAlexei Starovoitov 
140faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e)
141faeb2dceSAlexei Starovoitov {
142faeb2dceSAlexei Starovoitov 	return a + (long)b + c + d + e;
143faeb2dceSAlexei Starovoitov }
144faeb2dceSAlexei Starovoitov 
145faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f)
146faeb2dceSAlexei Starovoitov {
147faeb2dceSAlexei Starovoitov 	return a + (long)b + c + d + (long)e + f;
148faeb2dceSAlexei Starovoitov }
149faeb2dceSAlexei Starovoitov 
150d923021cSYonghong Song struct bpf_fentry_test_t {
151d923021cSYonghong Song 	struct bpf_fentry_test_t *a;
152d923021cSYonghong Song };
153d923021cSYonghong Song 
154d923021cSYonghong Song int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg)
155d923021cSYonghong Song {
156d923021cSYonghong Song 	return (long)arg;
157d923021cSYonghong Song }
158d923021cSYonghong Song 
159d923021cSYonghong Song int noinline bpf_fentry_test8(struct bpf_fentry_test_t *arg)
160d923021cSYonghong Song {
161d923021cSYonghong Song 	return (long)arg->a;
162d923021cSYonghong Song }
163d923021cSYonghong Song 
1643d08b6f2SKP Singh int noinline bpf_modify_return_test(int a, int *b)
1653d08b6f2SKP Singh {
1663d08b6f2SKP Singh 	*b += 1;
1673d08b6f2SKP Singh 	return a + *b;
1683d08b6f2SKP Singh }
169e9ff9d52SJean-Philippe Menil __diag_pop();
1703d08b6f2SKP Singh 
1713d08b6f2SKP Singh ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO);
1723d08b6f2SKP Singh 
1731cf1cae9SAlexei Starovoitov static void *bpf_test_init(const union bpf_attr *kattr, u32 size,
1741cf1cae9SAlexei Starovoitov 			   u32 headroom, u32 tailroom)
1751cf1cae9SAlexei Starovoitov {
1761cf1cae9SAlexei Starovoitov 	void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
177d800bad6SJesper Dangaard Brouer 	u32 user_size = kattr->test.data_size_in;
1781cf1cae9SAlexei Starovoitov 	void *data;
1791cf1cae9SAlexei Starovoitov 
1801cf1cae9SAlexei Starovoitov 	if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom)
1811cf1cae9SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
1821cf1cae9SAlexei Starovoitov 
183d800bad6SJesper Dangaard Brouer 	if (user_size > size)
184d800bad6SJesper Dangaard Brouer 		return ERR_PTR(-EMSGSIZE);
185d800bad6SJesper Dangaard Brouer 
1861cf1cae9SAlexei Starovoitov 	data = kzalloc(size + headroom + tailroom, GFP_USER);
1871cf1cae9SAlexei Starovoitov 	if (!data)
1881cf1cae9SAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
1891cf1cae9SAlexei Starovoitov 
190d800bad6SJesper Dangaard Brouer 	if (copy_from_user(data + headroom, data_in, user_size)) {
1911cf1cae9SAlexei Starovoitov 		kfree(data);
1921cf1cae9SAlexei Starovoitov 		return ERR_PTR(-EFAULT);
1931cf1cae9SAlexei Starovoitov 	}
194da00d2f1SKP Singh 
195da00d2f1SKP Singh 	return data;
196da00d2f1SKP Singh }
197da00d2f1SKP Singh 
198da00d2f1SKP Singh int bpf_prog_test_run_tracing(struct bpf_prog *prog,
199da00d2f1SKP Singh 			      const union bpf_attr *kattr,
200da00d2f1SKP Singh 			      union bpf_attr __user *uattr)
201da00d2f1SKP Singh {
202d923021cSYonghong Song 	struct bpf_fentry_test_t arg = {};
2033d08b6f2SKP Singh 	u16 side_effect = 0, ret = 0;
2043d08b6f2SKP Singh 	int b = 2, err = -EFAULT;
2053d08b6f2SKP Singh 	u32 retval = 0;
206da00d2f1SKP Singh 
207da00d2f1SKP Singh 	switch (prog->expected_attach_type) {
208da00d2f1SKP Singh 	case BPF_TRACE_FENTRY:
209da00d2f1SKP Singh 	case BPF_TRACE_FEXIT:
210faeb2dceSAlexei Starovoitov 		if (bpf_fentry_test1(1) != 2 ||
211faeb2dceSAlexei Starovoitov 		    bpf_fentry_test2(2, 3) != 5 ||
212faeb2dceSAlexei Starovoitov 		    bpf_fentry_test3(4, 5, 6) != 15 ||
213faeb2dceSAlexei Starovoitov 		    bpf_fentry_test4((void *)7, 8, 9, 10) != 34 ||
214faeb2dceSAlexei Starovoitov 		    bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 ||
215d923021cSYonghong Song 		    bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 ||
216d923021cSYonghong Song 		    bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 ||
217d923021cSYonghong Song 		    bpf_fentry_test8(&arg) != 0)
218da00d2f1SKP Singh 			goto out;
219da00d2f1SKP Singh 		break;
2203d08b6f2SKP Singh 	case BPF_MODIFY_RETURN:
2213d08b6f2SKP Singh 		ret = bpf_modify_return_test(1, &b);
2223d08b6f2SKP Singh 		if (b != 2)
2233d08b6f2SKP Singh 			side_effect = 1;
2243d08b6f2SKP Singh 		break;
225da00d2f1SKP Singh 	default:
226da00d2f1SKP Singh 		goto out;
227a25ecd9dSColin Ian King 	}
228da00d2f1SKP Singh 
2293d08b6f2SKP Singh 	retval = ((u32)side_effect << 16) | ret;
2303d08b6f2SKP Singh 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
2313d08b6f2SKP Singh 		goto out;
2323d08b6f2SKP Singh 
233da00d2f1SKP Singh 	err = 0;
234da00d2f1SKP Singh out:
235da00d2f1SKP Singh 	trace_bpf_test_finish(&err);
236da00d2f1SKP Singh 	return err;
2371cf1cae9SAlexei Starovoitov }
2381cf1cae9SAlexei Starovoitov 
239b0b9395dSStanislav Fomichev static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size)
240b0b9395dSStanislav Fomichev {
241b0b9395dSStanislav Fomichev 	void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in);
242b0b9395dSStanislav Fomichev 	void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
243b0b9395dSStanislav Fomichev 	u32 size = kattr->test.ctx_size_in;
244b0b9395dSStanislav Fomichev 	void *data;
245b0b9395dSStanislav Fomichev 	int err;
246b0b9395dSStanislav Fomichev 
247b0b9395dSStanislav Fomichev 	if (!data_in && !data_out)
248b0b9395dSStanislav Fomichev 		return NULL;
249b0b9395dSStanislav Fomichev 
250b0b9395dSStanislav Fomichev 	data = kzalloc(max_size, GFP_USER);
251b0b9395dSStanislav Fomichev 	if (!data)
252b0b9395dSStanislav Fomichev 		return ERR_PTR(-ENOMEM);
253b0b9395dSStanislav Fomichev 
254b0b9395dSStanislav Fomichev 	if (data_in) {
255b0b9395dSStanislav Fomichev 		err = bpf_check_uarg_tail_zero(data_in, max_size, size);
256b0b9395dSStanislav Fomichev 		if (err) {
257b0b9395dSStanislav Fomichev 			kfree(data);
258b0b9395dSStanislav Fomichev 			return ERR_PTR(err);
259b0b9395dSStanislav Fomichev 		}
260b0b9395dSStanislav Fomichev 
261b0b9395dSStanislav Fomichev 		size = min_t(u32, max_size, size);
262b0b9395dSStanislav Fomichev 		if (copy_from_user(data, data_in, size)) {
263b0b9395dSStanislav Fomichev 			kfree(data);
264b0b9395dSStanislav Fomichev 			return ERR_PTR(-EFAULT);
265b0b9395dSStanislav Fomichev 		}
266b0b9395dSStanislav Fomichev 	}
267b0b9395dSStanislav Fomichev 	return data;
268b0b9395dSStanislav Fomichev }
269b0b9395dSStanislav Fomichev 
270b0b9395dSStanislav Fomichev static int bpf_ctx_finish(const union bpf_attr *kattr,
271b0b9395dSStanislav Fomichev 			  union bpf_attr __user *uattr, const void *data,
272b0b9395dSStanislav Fomichev 			  u32 size)
273b0b9395dSStanislav Fomichev {
274b0b9395dSStanislav Fomichev 	void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
275b0b9395dSStanislav Fomichev 	int err = -EFAULT;
276b0b9395dSStanislav Fomichev 	u32 copy_size = size;
277b0b9395dSStanislav Fomichev 
278b0b9395dSStanislav Fomichev 	if (!data || !data_out)
279b0b9395dSStanislav Fomichev 		return 0;
280b0b9395dSStanislav Fomichev 
281b0b9395dSStanislav Fomichev 	if (copy_size > kattr->test.ctx_size_out) {
282b0b9395dSStanislav Fomichev 		copy_size = kattr->test.ctx_size_out;
283b0b9395dSStanislav Fomichev 		err = -ENOSPC;
284b0b9395dSStanislav Fomichev 	}
285b0b9395dSStanislav Fomichev 
286b0b9395dSStanislav Fomichev 	if (copy_to_user(data_out, data, copy_size))
287b0b9395dSStanislav Fomichev 		goto out;
288b0b9395dSStanislav Fomichev 	if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size)))
289b0b9395dSStanislav Fomichev 		goto out;
290b0b9395dSStanislav Fomichev 	if (err != -ENOSPC)
291b0b9395dSStanislav Fomichev 		err = 0;
292b0b9395dSStanislav Fomichev out:
293b0b9395dSStanislav Fomichev 	return err;
294b0b9395dSStanislav Fomichev }
295b0b9395dSStanislav Fomichev 
296b0b9395dSStanislav Fomichev /**
297b0b9395dSStanislav Fomichev  * range_is_zero - test whether buffer is initialized
298b0b9395dSStanislav Fomichev  * @buf: buffer to check
299b0b9395dSStanislav Fomichev  * @from: check from this position
300b0b9395dSStanislav Fomichev  * @to: check up until (excluding) this position
301b0b9395dSStanislav Fomichev  *
302b0b9395dSStanislav Fomichev  * This function returns true if the there is a non-zero byte
303b0b9395dSStanislav Fomichev  * in the buf in the range [from,to).
304b0b9395dSStanislav Fomichev  */
305b0b9395dSStanislav Fomichev static inline bool range_is_zero(void *buf, size_t from, size_t to)
306b0b9395dSStanislav Fomichev {
307b0b9395dSStanislav Fomichev 	return !memchr_inv((u8 *)buf + from, 0, to - from);
308b0b9395dSStanislav Fomichev }
309b0b9395dSStanislav Fomichev 
310b0b9395dSStanislav Fomichev static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
311b0b9395dSStanislav Fomichev {
312b0b9395dSStanislav Fomichev 	struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
313b0b9395dSStanislav Fomichev 
314b0b9395dSStanislav Fomichev 	if (!__skb)
315b0b9395dSStanislav Fomichev 		return 0;
316b0b9395dSStanislav Fomichev 
317b0b9395dSStanislav Fomichev 	/* make sure the fields we don't use are zeroed */
3186de6c1f8SNikita V. Shirokov 	if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark)))
3196de6c1f8SNikita V. Shirokov 		return -EINVAL;
3206de6c1f8SNikita V. Shirokov 
3216de6c1f8SNikita V. Shirokov 	/* mark is allowed */
3226de6c1f8SNikita V. Shirokov 
3236de6c1f8SNikita V. Shirokov 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark),
3246de6c1f8SNikita V. Shirokov 			   offsetof(struct __sk_buff, priority)))
325b0b9395dSStanislav Fomichev 		return -EINVAL;
326b0b9395dSStanislav Fomichev 
327b0b9395dSStanislav Fomichev 	/* priority is allowed */
328b0b9395dSStanislav Fomichev 
329b590cb5fSStanislav Fomichev 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, priority),
33021594c44SDmitry Yakunin 			   offsetof(struct __sk_buff, ifindex)))
33121594c44SDmitry Yakunin 		return -EINVAL;
33221594c44SDmitry Yakunin 
33321594c44SDmitry Yakunin 	/* ifindex is allowed */
33421594c44SDmitry Yakunin 
33521594c44SDmitry Yakunin 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex),
336b0b9395dSStanislav Fomichev 			   offsetof(struct __sk_buff, cb)))
337b0b9395dSStanislav Fomichev 		return -EINVAL;
338b0b9395dSStanislav Fomichev 
339b0b9395dSStanislav Fomichev 	/* cb is allowed */
340b0b9395dSStanislav Fomichev 
341b590cb5fSStanislav Fomichev 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb),
342ba940948SStanislav Fomichev 			   offsetof(struct __sk_buff, tstamp)))
343ba940948SStanislav Fomichev 		return -EINVAL;
344ba940948SStanislav Fomichev 
345ba940948SStanislav Fomichev 	/* tstamp is allowed */
346850a88ccSStanislav Fomichev 	/* wire_len is allowed */
347850a88ccSStanislav Fomichev 	/* gso_segs is allowed */
348ba940948SStanislav Fomichev 
349850a88ccSStanislav Fomichev 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs),
350cf62089bSWillem de Bruijn 			   offsetof(struct __sk_buff, gso_size)))
351cf62089bSWillem de Bruijn 		return -EINVAL;
352cf62089bSWillem de Bruijn 
353cf62089bSWillem de Bruijn 	/* gso_size is allowed */
354cf62089bSWillem de Bruijn 
355cf62089bSWillem de Bruijn 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size),
356b0b9395dSStanislav Fomichev 			   sizeof(struct __sk_buff)))
357b0b9395dSStanislav Fomichev 		return -EINVAL;
358b0b9395dSStanislav Fomichev 
3596de6c1f8SNikita V. Shirokov 	skb->mark = __skb->mark;
360b0b9395dSStanislav Fomichev 	skb->priority = __skb->priority;
361ba940948SStanislav Fomichev 	skb->tstamp = __skb->tstamp;
362b0b9395dSStanislav Fomichev 	memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN);
363b0b9395dSStanislav Fomichev 
364850a88ccSStanislav Fomichev 	if (__skb->wire_len == 0) {
365850a88ccSStanislav Fomichev 		cb->pkt_len = skb->len;
366850a88ccSStanislav Fomichev 	} else {
367850a88ccSStanislav Fomichev 		if (__skb->wire_len < skb->len ||
368850a88ccSStanislav Fomichev 		    __skb->wire_len > GSO_MAX_SIZE)
369850a88ccSStanislav Fomichev 			return -EINVAL;
370850a88ccSStanislav Fomichev 		cb->pkt_len = __skb->wire_len;
371850a88ccSStanislav Fomichev 	}
372850a88ccSStanislav Fomichev 
373850a88ccSStanislav Fomichev 	if (__skb->gso_segs > GSO_MAX_SEGS)
374850a88ccSStanislav Fomichev 		return -EINVAL;
375850a88ccSStanislav Fomichev 	skb_shinfo(skb)->gso_segs = __skb->gso_segs;
376cf62089bSWillem de Bruijn 	skb_shinfo(skb)->gso_size = __skb->gso_size;
377850a88ccSStanislav Fomichev 
378b0b9395dSStanislav Fomichev 	return 0;
379b0b9395dSStanislav Fomichev }
380b0b9395dSStanislav Fomichev 
381b0b9395dSStanislav Fomichev static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb)
382b0b9395dSStanislav Fomichev {
383b0b9395dSStanislav Fomichev 	struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
384b0b9395dSStanislav Fomichev 
385b0b9395dSStanislav Fomichev 	if (!__skb)
386b0b9395dSStanislav Fomichev 		return;
387b0b9395dSStanislav Fomichev 
3886de6c1f8SNikita V. Shirokov 	__skb->mark = skb->mark;
389b0b9395dSStanislav Fomichev 	__skb->priority = skb->priority;
39021594c44SDmitry Yakunin 	__skb->ifindex = skb->dev->ifindex;
391ba940948SStanislav Fomichev 	__skb->tstamp = skb->tstamp;
392b0b9395dSStanislav Fomichev 	memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN);
393850a88ccSStanislav Fomichev 	__skb->wire_len = cb->pkt_len;
394850a88ccSStanislav Fomichev 	__skb->gso_segs = skb_shinfo(skb)->gso_segs;
395b0b9395dSStanislav Fomichev }
396b0b9395dSStanislav Fomichev 
3971cf1cae9SAlexei Starovoitov int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
3981cf1cae9SAlexei Starovoitov 			  union bpf_attr __user *uattr)
3991cf1cae9SAlexei Starovoitov {
4001cf1cae9SAlexei Starovoitov 	bool is_l2 = false, is_direct_pkt_access = false;
40121594c44SDmitry Yakunin 	struct net *net = current->nsproxy->net_ns;
40221594c44SDmitry Yakunin 	struct net_device *dev = net->loopback_dev;
4031cf1cae9SAlexei Starovoitov 	u32 size = kattr->test.data_size_in;
4041cf1cae9SAlexei Starovoitov 	u32 repeat = kattr->test.repeat;
405b0b9395dSStanislav Fomichev 	struct __sk_buff *ctx = NULL;
4061cf1cae9SAlexei Starovoitov 	u32 retval, duration;
4076e6fddc7SDaniel Borkmann 	int hh_len = ETH_HLEN;
4081cf1cae9SAlexei Starovoitov 	struct sk_buff *skb;
4092cb494a3SSong Liu 	struct sock *sk;
4101cf1cae9SAlexei Starovoitov 	void *data;
4111cf1cae9SAlexei Starovoitov 	int ret;
4121cf1cae9SAlexei Starovoitov 
413586f8525SDavid Miller 	data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
4141cf1cae9SAlexei Starovoitov 			     SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
4151cf1cae9SAlexei Starovoitov 	if (IS_ERR(data))
4161cf1cae9SAlexei Starovoitov 		return PTR_ERR(data);
4171cf1cae9SAlexei Starovoitov 
418b0b9395dSStanislav Fomichev 	ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
419b0b9395dSStanislav Fomichev 	if (IS_ERR(ctx)) {
420b0b9395dSStanislav Fomichev 		kfree(data);
421b0b9395dSStanislav Fomichev 		return PTR_ERR(ctx);
422b0b9395dSStanislav Fomichev 	}
423b0b9395dSStanislav Fomichev 
4241cf1cae9SAlexei Starovoitov 	switch (prog->type) {
4251cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_SCHED_CLS:
4261cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_SCHED_ACT:
4271cf1cae9SAlexei Starovoitov 		is_l2 = true;
428*df561f66SGustavo A. R. Silva 		fallthrough;
4291cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_IN:
4301cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_OUT:
4311cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_XMIT:
4321cf1cae9SAlexei Starovoitov 		is_direct_pkt_access = true;
4331cf1cae9SAlexei Starovoitov 		break;
4341cf1cae9SAlexei Starovoitov 	default:
4351cf1cae9SAlexei Starovoitov 		break;
4361cf1cae9SAlexei Starovoitov 	}
4371cf1cae9SAlexei Starovoitov 
4382cb494a3SSong Liu 	sk = kzalloc(sizeof(struct sock), GFP_USER);
4392cb494a3SSong Liu 	if (!sk) {
4401cf1cae9SAlexei Starovoitov 		kfree(data);
441b0b9395dSStanislav Fomichev 		kfree(ctx);
4421cf1cae9SAlexei Starovoitov 		return -ENOMEM;
4431cf1cae9SAlexei Starovoitov 	}
44421594c44SDmitry Yakunin 	sock_net_set(sk, net);
4452cb494a3SSong Liu 	sock_init_data(NULL, sk);
4462cb494a3SSong Liu 
4472cb494a3SSong Liu 	skb = build_skb(data, 0);
4482cb494a3SSong Liu 	if (!skb) {
4492cb494a3SSong Liu 		kfree(data);
450b0b9395dSStanislav Fomichev 		kfree(ctx);
4512cb494a3SSong Liu 		kfree(sk);
4522cb494a3SSong Liu 		return -ENOMEM;
4532cb494a3SSong Liu 	}
4542cb494a3SSong Liu 	skb->sk = sk;
4551cf1cae9SAlexei Starovoitov 
456586f8525SDavid Miller 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
4571cf1cae9SAlexei Starovoitov 	__skb_put(skb, size);
45821594c44SDmitry Yakunin 	if (ctx && ctx->ifindex > 1) {
45921594c44SDmitry Yakunin 		dev = dev_get_by_index(net, ctx->ifindex);
46021594c44SDmitry Yakunin 		if (!dev) {
46121594c44SDmitry Yakunin 			ret = -ENODEV;
46221594c44SDmitry Yakunin 			goto out;
46321594c44SDmitry Yakunin 		}
46421594c44SDmitry Yakunin 	}
46521594c44SDmitry Yakunin 	skb->protocol = eth_type_trans(skb, dev);
4661cf1cae9SAlexei Starovoitov 	skb_reset_network_header(skb);
4671cf1cae9SAlexei Starovoitov 
468fa5cb548SDmitry Yakunin 	switch (skb->protocol) {
469fa5cb548SDmitry Yakunin 	case htons(ETH_P_IP):
470fa5cb548SDmitry Yakunin 		sk->sk_family = AF_INET;
471fa5cb548SDmitry Yakunin 		if (sizeof(struct iphdr) <= skb_headlen(skb)) {
472fa5cb548SDmitry Yakunin 			sk->sk_rcv_saddr = ip_hdr(skb)->saddr;
473fa5cb548SDmitry Yakunin 			sk->sk_daddr = ip_hdr(skb)->daddr;
474fa5cb548SDmitry Yakunin 		}
475fa5cb548SDmitry Yakunin 		break;
476fa5cb548SDmitry Yakunin #if IS_ENABLED(CONFIG_IPV6)
477fa5cb548SDmitry Yakunin 	case htons(ETH_P_IPV6):
478fa5cb548SDmitry Yakunin 		sk->sk_family = AF_INET6;
479fa5cb548SDmitry Yakunin 		if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) {
480fa5cb548SDmitry Yakunin 			sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr;
481fa5cb548SDmitry Yakunin 			sk->sk_v6_daddr = ipv6_hdr(skb)->daddr;
482fa5cb548SDmitry Yakunin 		}
483fa5cb548SDmitry Yakunin 		break;
484fa5cb548SDmitry Yakunin #endif
485fa5cb548SDmitry Yakunin 	default:
486fa5cb548SDmitry Yakunin 		break;
487fa5cb548SDmitry Yakunin 	}
488fa5cb548SDmitry Yakunin 
4891cf1cae9SAlexei Starovoitov 	if (is_l2)
4906e6fddc7SDaniel Borkmann 		__skb_push(skb, hh_len);
4911cf1cae9SAlexei Starovoitov 	if (is_direct_pkt_access)
4926aaae2b6SDaniel Borkmann 		bpf_compute_data_pointers(skb);
493b0b9395dSStanislav Fomichev 	ret = convert___skb_to_skb(skb, ctx);
494b0b9395dSStanislav Fomichev 	if (ret)
495b0b9395dSStanislav Fomichev 		goto out;
496f23c4b39SBjörn Töpel 	ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false);
497b0b9395dSStanislav Fomichev 	if (ret)
498b0b9395dSStanislav Fomichev 		goto out;
4996e6fddc7SDaniel Borkmann 	if (!is_l2) {
5006e6fddc7SDaniel Borkmann 		if (skb_headroom(skb) < hh_len) {
5016e6fddc7SDaniel Borkmann 			int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
5026e6fddc7SDaniel Borkmann 
5036e6fddc7SDaniel Borkmann 			if (pskb_expand_head(skb, nhead, 0, GFP_USER)) {
504b0b9395dSStanislav Fomichev 				ret = -ENOMEM;
505b0b9395dSStanislav Fomichev 				goto out;
5066e6fddc7SDaniel Borkmann 			}
5076e6fddc7SDaniel Borkmann 		}
5086e6fddc7SDaniel Borkmann 		memset(__skb_push(skb, hh_len), 0, hh_len);
5096e6fddc7SDaniel Borkmann 	}
510b0b9395dSStanislav Fomichev 	convert_skb_to___skb(skb, ctx);
5116e6fddc7SDaniel Borkmann 
5121cf1cae9SAlexei Starovoitov 	size = skb->len;
5131cf1cae9SAlexei Starovoitov 	/* bpf program can never convert linear skb to non-linear */
5141cf1cae9SAlexei Starovoitov 	if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
5151cf1cae9SAlexei Starovoitov 		size = skb_headlen(skb);
51678e52272SDavid Miller 	ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration);
517b0b9395dSStanislav Fomichev 	if (!ret)
518b0b9395dSStanislav Fomichev 		ret = bpf_ctx_finish(kattr, uattr, ctx,
519b0b9395dSStanislav Fomichev 				     sizeof(struct __sk_buff));
520b0b9395dSStanislav Fomichev out:
52121594c44SDmitry Yakunin 	if (dev && dev != net->loopback_dev)
52221594c44SDmitry Yakunin 		dev_put(dev);
5231cf1cae9SAlexei Starovoitov 	kfree_skb(skb);
5246ac99e8fSMartin KaFai Lau 	bpf_sk_storage_free(sk);
5252cb494a3SSong Liu 	kfree(sk);
526b0b9395dSStanislav Fomichev 	kfree(ctx);
5271cf1cae9SAlexei Starovoitov 	return ret;
5281cf1cae9SAlexei Starovoitov }
5291cf1cae9SAlexei Starovoitov 
5301cf1cae9SAlexei Starovoitov int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
5311cf1cae9SAlexei Starovoitov 			  union bpf_attr __user *uattr)
5321cf1cae9SAlexei Starovoitov {
533bc56c919SJesper Dangaard Brouer 	u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
534bc56c919SJesper Dangaard Brouer 	u32 headroom = XDP_PACKET_HEADROOM;
5351cf1cae9SAlexei Starovoitov 	u32 size = kattr->test.data_size_in;
5361cf1cae9SAlexei Starovoitov 	u32 repeat = kattr->test.repeat;
53765073a67SDaniel Borkmann 	struct netdev_rx_queue *rxqueue;
5381cf1cae9SAlexei Starovoitov 	struct xdp_buff xdp = {};
5391cf1cae9SAlexei Starovoitov 	u32 retval, duration;
540bc56c919SJesper Dangaard Brouer 	u32 max_data_sz;
5411cf1cae9SAlexei Starovoitov 	void *data;
5421cf1cae9SAlexei Starovoitov 	int ret;
5431cf1cae9SAlexei Starovoitov 
544947e8b59SStanislav Fomichev 	if (kattr->test.ctx_in || kattr->test.ctx_out)
545947e8b59SStanislav Fomichev 		return -EINVAL;
546947e8b59SStanislav Fomichev 
547bc56c919SJesper Dangaard Brouer 	/* XDP have extra tailroom as (most) drivers use full page */
548bc56c919SJesper Dangaard Brouer 	max_data_sz = 4096 - headroom - tailroom;
549bc56c919SJesper Dangaard Brouer 
550bc56c919SJesper Dangaard Brouer 	data = bpf_test_init(kattr, max_data_sz, headroom, tailroom);
5511cf1cae9SAlexei Starovoitov 	if (IS_ERR(data))
5521cf1cae9SAlexei Starovoitov 		return PTR_ERR(data);
5531cf1cae9SAlexei Starovoitov 
5541cf1cae9SAlexei Starovoitov 	xdp.data_hard_start = data;
555bc56c919SJesper Dangaard Brouer 	xdp.data = data + headroom;
556de8f3a83SDaniel Borkmann 	xdp.data_meta = xdp.data;
5571cf1cae9SAlexei Starovoitov 	xdp.data_end = xdp.data + size;
558bc56c919SJesper Dangaard Brouer 	xdp.frame_sz = headroom + max_data_sz + tailroom;
5591cf1cae9SAlexei Starovoitov 
56065073a67SDaniel Borkmann 	rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
56165073a67SDaniel Borkmann 	xdp.rxq = &rxqueue->xdp_rxq;
562f23c4b39SBjörn Töpel 	bpf_prog_change_xdp(NULL, prog);
563f23c4b39SBjörn Töpel 	ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true);
564dcb40590SRoman Gushchin 	if (ret)
565dcb40590SRoman Gushchin 		goto out;
566bc56c919SJesper Dangaard Brouer 	if (xdp.data != data + headroom || xdp.data_end != xdp.data + size)
5671cf1cae9SAlexei Starovoitov 		size = xdp.data_end - xdp.data;
56878e52272SDavid Miller 	ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration);
569dcb40590SRoman Gushchin out:
570f23c4b39SBjörn Töpel 	bpf_prog_change_xdp(prog, NULL);
5711cf1cae9SAlexei Starovoitov 	kfree(data);
5721cf1cae9SAlexei Starovoitov 	return ret;
5731cf1cae9SAlexei Starovoitov }
574b7a1848eSStanislav Fomichev 
575b2ca4e1cSStanislav Fomichev static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx)
576b2ca4e1cSStanislav Fomichev {
577b2ca4e1cSStanislav Fomichev 	/* make sure the fields we don't use are zeroed */
578b2ca4e1cSStanislav Fomichev 	if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags)))
579b2ca4e1cSStanislav Fomichev 		return -EINVAL;
580b2ca4e1cSStanislav Fomichev 
581b2ca4e1cSStanislav Fomichev 	/* flags is allowed */
582b2ca4e1cSStanislav Fomichev 
583b590cb5fSStanislav Fomichev 	if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags),
584b2ca4e1cSStanislav Fomichev 			   sizeof(struct bpf_flow_keys)))
585b2ca4e1cSStanislav Fomichev 		return -EINVAL;
586b2ca4e1cSStanislav Fomichev 
587b2ca4e1cSStanislav Fomichev 	return 0;
588b2ca4e1cSStanislav Fomichev }
589b2ca4e1cSStanislav Fomichev 
590b7a1848eSStanislav Fomichev int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
591b7a1848eSStanislav Fomichev 				     const union bpf_attr *kattr,
592b7a1848eSStanislav Fomichev 				     union bpf_attr __user *uattr)
593b7a1848eSStanislav Fomichev {
594b7a1848eSStanislav Fomichev 	u32 size = kattr->test.data_size_in;
5957b8a1304SStanislav Fomichev 	struct bpf_flow_dissector ctx = {};
596b7a1848eSStanislav Fomichev 	u32 repeat = kattr->test.repeat;
597b2ca4e1cSStanislav Fomichev 	struct bpf_flow_keys *user_ctx;
598b7a1848eSStanislav Fomichev 	struct bpf_flow_keys flow_keys;
599b7a1848eSStanislav Fomichev 	u64 time_start, time_spent = 0;
6007b8a1304SStanislav Fomichev 	const struct ethhdr *eth;
601b2ca4e1cSStanislav Fomichev 	unsigned int flags = 0;
602b7a1848eSStanislav Fomichev 	u32 retval, duration;
603b7a1848eSStanislav Fomichev 	void *data;
604b7a1848eSStanislav Fomichev 	int ret;
605b7a1848eSStanislav Fomichev 	u32 i;
606b7a1848eSStanislav Fomichev 
607b7a1848eSStanislav Fomichev 	if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR)
608b7a1848eSStanislav Fomichev 		return -EINVAL;
609b7a1848eSStanislav Fomichev 
6107b8a1304SStanislav Fomichev 	if (size < ETH_HLEN)
6117b8a1304SStanislav Fomichev 		return -EINVAL;
6127b8a1304SStanislav Fomichev 
6137b8a1304SStanislav Fomichev 	data = bpf_test_init(kattr, size, 0, 0);
614b7a1848eSStanislav Fomichev 	if (IS_ERR(data))
615b7a1848eSStanislav Fomichev 		return PTR_ERR(data);
616b7a1848eSStanislav Fomichev 
6177b8a1304SStanislav Fomichev 	eth = (struct ethhdr *)data;
618b7a1848eSStanislav Fomichev 
619b7a1848eSStanislav Fomichev 	if (!repeat)
620b7a1848eSStanislav Fomichev 		repeat = 1;
621b7a1848eSStanislav Fomichev 
622b2ca4e1cSStanislav Fomichev 	user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys));
623b2ca4e1cSStanislav Fomichev 	if (IS_ERR(user_ctx)) {
624b2ca4e1cSStanislav Fomichev 		kfree(data);
625b2ca4e1cSStanislav Fomichev 		return PTR_ERR(user_ctx);
626b2ca4e1cSStanislav Fomichev 	}
627b2ca4e1cSStanislav Fomichev 	if (user_ctx) {
628b2ca4e1cSStanislav Fomichev 		ret = verify_user_bpf_flow_keys(user_ctx);
629b2ca4e1cSStanislav Fomichev 		if (ret)
630b2ca4e1cSStanislav Fomichev 			goto out;
631b2ca4e1cSStanislav Fomichev 		flags = user_ctx->flags;
632b2ca4e1cSStanislav Fomichev 	}
633b2ca4e1cSStanislav Fomichev 
6347b8a1304SStanislav Fomichev 	ctx.flow_keys = &flow_keys;
6357b8a1304SStanislav Fomichev 	ctx.data = data;
6367b8a1304SStanislav Fomichev 	ctx.data_end = (__u8 *)data + size;
6377b8a1304SStanislav Fomichev 
638a439184dSStanislav Fomichev 	rcu_read_lock();
639a439184dSStanislav Fomichev 	preempt_disable();
640b7a1848eSStanislav Fomichev 	time_start = ktime_get_ns();
641b7a1848eSStanislav Fomichev 	for (i = 0; i < repeat; i++) {
6427b8a1304SStanislav Fomichev 		retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN,
643b2ca4e1cSStanislav Fomichev 					  size, flags);
6447b8a1304SStanislav Fomichev 
645a439184dSStanislav Fomichev 		if (signal_pending(current)) {
646b7a1848eSStanislav Fomichev 			preempt_enable();
647a439184dSStanislav Fomichev 			rcu_read_unlock();
648a439184dSStanislav Fomichev 
649a439184dSStanislav Fomichev 			ret = -EINTR;
650a439184dSStanislav Fomichev 			goto out;
651a439184dSStanislav Fomichev 		}
652b7a1848eSStanislav Fomichev 
653b7a1848eSStanislav Fomichev 		if (need_resched()) {
654b7a1848eSStanislav Fomichev 			time_spent += ktime_get_ns() - time_start;
655a439184dSStanislav Fomichev 			preempt_enable();
656a439184dSStanislav Fomichev 			rcu_read_unlock();
657a439184dSStanislav Fomichev 
658b7a1848eSStanislav Fomichev 			cond_resched();
659a439184dSStanislav Fomichev 
660a439184dSStanislav Fomichev 			rcu_read_lock();
661a439184dSStanislav Fomichev 			preempt_disable();
662b7a1848eSStanislav Fomichev 			time_start = ktime_get_ns();
663b7a1848eSStanislav Fomichev 		}
664b7a1848eSStanislav Fomichev 	}
665b7a1848eSStanislav Fomichev 	time_spent += ktime_get_ns() - time_start;
666a439184dSStanislav Fomichev 	preempt_enable();
667a439184dSStanislav Fomichev 	rcu_read_unlock();
668a439184dSStanislav Fomichev 
669b7a1848eSStanislav Fomichev 	do_div(time_spent, repeat);
670b7a1848eSStanislav Fomichev 	duration = time_spent > U32_MAX ? U32_MAX : (u32)time_spent;
671b7a1848eSStanislav Fomichev 
672b7a1848eSStanislav Fomichev 	ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),
673b7a1848eSStanislav Fomichev 			      retval, duration);
674b2ca4e1cSStanislav Fomichev 	if (!ret)
675b2ca4e1cSStanislav Fomichev 		ret = bpf_ctx_finish(kattr, uattr, user_ctx,
676b2ca4e1cSStanislav Fomichev 				     sizeof(struct bpf_flow_keys));
677b7a1848eSStanislav Fomichev 
678a439184dSStanislav Fomichev out:
679b2ca4e1cSStanislav Fomichev 	kfree(user_ctx);
6807b8a1304SStanislav Fomichev 	kfree(data);
681b7a1848eSStanislav Fomichev 	return ret;
682b7a1848eSStanislav Fomichev }
683