1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2022. Huawei Technologies Co., Ltd */
3 #define _GNU_SOURCE
4 #include <sched.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <stdbool.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <pthread.h>
11 
12 #include <bpf/bpf.h>
13 #include <bpf/libbpf.h>
14 
15 #include "test_maps.h"
16 #include "task_local_storage_helpers.h"
17 #include "read_bpf_task_storage_busy.skel.h"
18 
19 struct lookup_ctx {
20 	bool start;
21 	bool stop;
22 	int pid_fd;
23 	int map_fd;
24 	int loop;
25 };
26 
27 static void *lookup_fn(void *arg)
28 {
29 	struct lookup_ctx *ctx = arg;
30 	long value;
31 	int i = 0;
32 
33 	while (!ctx->start)
34 		usleep(1);
35 
36 	while (!ctx->stop && i++ < ctx->loop)
37 		bpf_map_lookup_elem(ctx->map_fd, &ctx->pid_fd, &value);
38 	return NULL;
39 }
40 
41 static void abort_lookup(struct lookup_ctx *ctx, pthread_t *tids, unsigned int nr)
42 {
43 	unsigned int i;
44 
45 	ctx->stop = true;
46 	ctx->start = true;
47 	for (i = 0; i < nr; i++)
48 		pthread_join(tids[i], NULL);
49 }
50 
51 void test_task_storage_map_stress_lookup(void)
52 {
53 #define MAX_NR_THREAD 4096
54 	unsigned int i, nr = 256, loop = 8192, cpu = 0;
55 	struct read_bpf_task_storage_busy *skel;
56 	pthread_t tids[MAX_NR_THREAD];
57 	struct lookup_ctx ctx;
58 	cpu_set_t old, new;
59 	const char *cfg;
60 	int err;
61 
62 	cfg = getenv("TASK_STORAGE_MAP_NR_THREAD");
63 	if (cfg) {
64 		nr = atoi(cfg);
65 		if (nr > MAX_NR_THREAD)
66 			nr = MAX_NR_THREAD;
67 	}
68 	cfg = getenv("TASK_STORAGE_MAP_NR_LOOP");
69 	if (cfg)
70 		loop = atoi(cfg);
71 	cfg = getenv("TASK_STORAGE_MAP_PIN_CPU");
72 	if (cfg)
73 		cpu = atoi(cfg);
74 
75 	skel = read_bpf_task_storage_busy__open_and_load();
76 	err = libbpf_get_error(skel);
77 	CHECK(err, "open_and_load", "error %d\n", err);
78 
79 	/* Only for a fully preemptible kernel */
80 	if (!skel->kconfig->CONFIG_PREEMPT)
81 		return;
82 
83 	/* Save the old affinity setting */
84 	sched_getaffinity(getpid(), sizeof(old), &old);
85 
86 	/* Pinned on a specific CPU */
87 	CPU_ZERO(&new);
88 	CPU_SET(cpu, &new);
89 	sched_setaffinity(getpid(), sizeof(new), &new);
90 
91 	ctx.start = false;
92 	ctx.stop = false;
93 	ctx.pid_fd = sys_pidfd_open(getpid(), 0);
94 	ctx.map_fd = bpf_map__fd(skel->maps.task);
95 	ctx.loop = loop;
96 	for (i = 0; i < nr; i++) {
97 		err = pthread_create(&tids[i], NULL, lookup_fn, &ctx);
98 		if (err) {
99 			abort_lookup(&ctx, tids, i);
100 			CHECK(err, "pthread_create", "error %d\n", err);
101 			goto out;
102 		}
103 	}
104 
105 	ctx.start = true;
106 	for (i = 0; i < nr; i++)
107 		pthread_join(tids[i], NULL);
108 
109 	skel->bss->pid = getpid();
110 	err = read_bpf_task_storage_busy__attach(skel);
111 	CHECK(err, "attach", "error %d\n", err);
112 
113 	/* Trigger program */
114 	syscall(SYS_gettid);
115 	skel->bss->pid = 0;
116 
117 	CHECK(skel->bss->busy != 0, "bad bpf_task_storage_busy", "got %d\n", skel->bss->busy);
118 out:
119 	read_bpf_task_storage_busy__destroy(skel);
120 	/* Restore affinity setting */
121 	sched_setaffinity(getpid(), sizeof(old), &old);
122 }
123