1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (C) 2023. Huawei Technologies Co., Ltd */
3#define _GNU_SOURCE
4#include <sched.h>
5#include <pthread.h>
6#include <stdbool.h>
7#include <test_progs.h>
8
9#include "preempted_bpf_ma_op.skel.h"
10
11#define ALLOC_THREAD_NR 4
12#define ALLOC_LOOP_NR 512
13
14struct alloc_ctx {
15	/* output */
16	int run_err;
17	/* input */
18	int fd;
19	bool *nomem_err;
20};
21
22static void *run_alloc_prog(void *data)
23{
24	struct alloc_ctx *ctx = data;
25	cpu_set_t cpu_set;
26	int i;
27
28	CPU_ZERO(&cpu_set);
29	CPU_SET(0, &cpu_set);
30	pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set);
31
32	for (i = 0; i < ALLOC_LOOP_NR && !*ctx->nomem_err; i++) {
33		LIBBPF_OPTS(bpf_test_run_opts, topts);
34		int err;
35
36		err = bpf_prog_test_run_opts(ctx->fd, &topts);
37		ctx->run_err |= err | topts.retval;
38	}
39
40	return NULL;
41}
42
43void test_preempted_bpf_ma_op(void)
44{
45	struct alloc_ctx ctx[ALLOC_THREAD_NR];
46	struct preempted_bpf_ma_op *skel;
47	pthread_t tid[ALLOC_THREAD_NR];
48	int i, err;
49
50	skel = preempted_bpf_ma_op__open_and_load();
51	if (!ASSERT_OK_PTR(skel, "open_and_load"))
52		return;
53
54	err = preempted_bpf_ma_op__attach(skel);
55	if (!ASSERT_OK(err, "attach"))
56		goto out;
57
58	for (i = 0; i < ARRAY_SIZE(ctx); i++) {
59		struct bpf_program *prog;
60		char name[8];
61
62		snprintf(name, sizeof(name), "test%d", i);
63		prog = bpf_object__find_program_by_name(skel->obj, name);
64		if (!ASSERT_OK_PTR(prog, "no test prog"))
65			goto out;
66
67		ctx[i].run_err = 0;
68		ctx[i].fd = bpf_program__fd(prog);
69		ctx[i].nomem_err = &skel->bss->nomem_err;
70	}
71
72	memset(tid, 0, sizeof(tid));
73	for (i = 0; i < ARRAY_SIZE(tid); i++) {
74		err = pthread_create(&tid[i], NULL, run_alloc_prog, &ctx[i]);
75		if (!ASSERT_OK(err, "pthread_create"))
76			break;
77	}
78
79	for (i = 0; i < ARRAY_SIZE(tid); i++) {
80		if (!tid[i])
81			break;
82		pthread_join(tid[i], NULL);
83		ASSERT_EQ(ctx[i].run_err, 0, "run prog err");
84	}
85
86	ASSERT_FALSE(skel->bss->nomem_err, "ENOMEM");
87out:
88	preempted_bpf_ma_op__destroy(skel);
89}
90