2019-05-23 14:47:47 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <test_progs.h>
|
2021-08-17 12:09:23 -07:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2020-01-14 19:50:03 -08:00
|
|
|
#include "test_send_signal_kern.skel.h"
|
2019-05-23 14:47:47 -07:00
|
|
|
|
2022-03-08 12:04:48 -08:00
|
|
|
static int sigusr1_received;
|
2019-05-23 14:47:47 -07:00
|
|
|
|
|
|
|
static void sigusr1_handler(int signum)
|
|
|
|
{
|
2022-03-08 12:04:48 -08:00
|
|
|
sigusr1_received = 1;
|
2019-05-23 14:47:47 -07:00
|
|
|
}
|
|
|
|
|
2019-08-21 16:44:27 -07:00
|
|
|
static void test_send_signal_common(struct perf_event_attr *attr,
|
2021-08-17 12:09:18 -07:00
|
|
|
bool signal_thread)
|
2019-05-23 14:47:47 -07:00
|
|
|
{
|
2020-01-14 19:50:03 -08:00
|
|
|
struct test_send_signal_kern *skel;
|
2019-05-23 14:47:47 -07:00
|
|
|
int pipe_c2p[2], pipe_p2c[2];
|
2020-01-14 19:50:03 -08:00
|
|
|
int err = -1, pmu_fd = -1;
|
2019-05-23 14:47:47 -07:00
|
|
|
char buf[256];
|
|
|
|
pid_t pid;
|
|
|
|
|
2021-08-17 12:09:18 -07:00
|
|
|
if (!ASSERT_OK(pipe(pipe_c2p), "pipe_c2p"))
|
2019-08-21 16:44:27 -07:00
|
|
|
return;
|
2019-05-23 14:47:47 -07:00
|
|
|
|
2021-08-17 12:09:18 -07:00
|
|
|
if (!ASSERT_OK(pipe(pipe_p2c), "pipe_p2c")) {
|
2019-05-23 14:47:47 -07:00
|
|
|
close(pipe_c2p[0]);
|
|
|
|
close(pipe_c2p[1]);
|
2019-08-21 16:44:27 -07:00
|
|
|
return;
|
2019-05-23 14:47:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pid = fork();
|
2021-08-17 12:09:18 -07:00
|
|
|
if (!ASSERT_GE(pid, 0, "fork")) {
|
2019-05-23 14:47:47 -07:00
|
|
|
close(pipe_c2p[0]);
|
|
|
|
close(pipe_c2p[1]);
|
|
|
|
close(pipe_p2c[0]);
|
|
|
|
close(pipe_p2c[1]);
|
2019-08-21 16:44:27 -07:00
|
|
|
return;
|
2019-05-23 14:47:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0) {
|
2021-08-17 12:09:23 -07:00
|
|
|
int old_prio;
|
2022-03-08 12:04:48 -08:00
|
|
|
volatile int j = 0;
|
2021-08-17 12:09:23 -07:00
|
|
|
|
2019-05-23 14:47:47 -07:00
|
|
|
/* install signal handler and notify parent */
|
2022-03-08 12:04:48 -08:00
|
|
|
ASSERT_NEQ(signal(SIGUSR1, sigusr1_handler), SIG_ERR, "signal");
|
2019-05-23 14:47:47 -07:00
|
|
|
|
|
|
|
close(pipe_c2p[0]); /* close read */
|
|
|
|
close(pipe_p2c[1]); /* close write */
|
|
|
|
|
2021-08-17 12:09:23 -07:00
|
|
|
/* boost with a high priority so we got a higher chance
|
|
|
|
* that if an interrupt happens, the underlying task
|
|
|
|
* is this process.
|
|
|
|
*/
|
|
|
|
errno = 0;
|
|
|
|
old_prio = getpriority(PRIO_PROCESS, 0);
|
|
|
|
ASSERT_OK(errno, "getpriority");
|
|
|
|
ASSERT_OK(setpriority(PRIO_PROCESS, 0, -20), "setpriority");
|
|
|
|
|
2019-05-23 14:47:47 -07:00
|
|
|
/* notify parent signal handler is installed */
|
2021-08-17 12:09:18 -07:00
|
|
|
ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");
|
2019-05-23 14:47:47 -07:00
|
|
|
|
|
|
|
/* make sure parent enabled bpf program to send_signal */
|
2021-08-17 12:09:18 -07:00
|
|
|
ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
|
2019-05-23 14:47:47 -07:00
|
|
|
|
|
|
|
/* wait a little for signal handler */
|
2022-03-08 12:04:48 -08:00
|
|
|
for (int i = 0; i < 100000000 && !sigusr1_received; i++)
|
|
|
|
j /= i + 1;
|
2019-05-23 14:47:47 -07:00
|
|
|
|
bpf: Fix compilation warning of selftests
Clang compiler version: 12.0.0
The following warning appears during the selftests/bpf compilation:
prog_tests/send_signal.c:51:3: warning: ignoring return value of ‘write’,
declared with attribute warn_unused_result [-Wunused-result]
51 | write(pipe_c2p[1], buf, 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
prog_tests/send_signal.c:54:3: warning: ignoring return value of ‘read’,
declared with attribute warn_unused_result [-Wunused-result]
54 | read(pipe_p2c[0], buf, 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~
......
prog_tests/stacktrace_build_id_nmi.c:13:2: warning: ignoring return value
of ‘fscanf’,declared with attribute warn_unused_result [-Wunused-resul]
13 | fscanf(f, "%llu", &sample_freq);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test_tcpnotify_user.c:133:2: warning:ignoring return value of ‘system’,
declared with attribute warn_unused_result [-Wunused-result]
133 | system(test_script);
| ^~~~~~~~~~~~~~~~~~~
test_tcpnotify_user.c:138:2: warning:ignoring return value of ‘system’,
declared with attribute warn_unused_result [-Wunused-result]
138 | system(test_script);
| ^~~~~~~~~~~~~~~~~~~
test_tcpnotify_user.c:143:2: warning:ignoring return value of ‘system’,
declared with attribute warn_unused_result [-Wunused-result]
143 | system(test_script);
| ^~~~~~~~~~~~~~~~~~~
Add code that fix compilation warning about ignoring return value and
handles any errors; Check return value of library`s API make the code
more secure.
Signed-off-by: Jianlin Lv <Jianlin.Lv@arm.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200806104224.95306-1-Jianlin.Lv@arm.com
2020-08-06 18:42:24 +08:00
|
|
|
buf[0] = sigusr1_received ? '2' : '0';
|
2022-03-08 12:04:48 -08:00
|
|
|
ASSERT_EQ(sigusr1_received, 1, "sigusr1_received");
|
2021-08-17 12:09:18 -07:00
|
|
|
ASSERT_EQ(write(pipe_c2p[1], buf, 1), 1, "pipe_write");
|
2019-05-23 14:47:47 -07:00
|
|
|
|
|
|
|
/* wait for parent notification and exit */
|
2021-08-17 12:09:18 -07:00
|
|
|
ASSERT_EQ(read(pipe_p2c[0], buf, 1), 1, "pipe_read");
|
2019-05-23 14:47:47 -07:00
|
|
|
|
2021-08-17 12:09:23 -07:00
|
|
|
/* restore the old priority */
|
|
|
|
ASSERT_OK(setpriority(PRIO_PROCESS, 0, old_prio), "setpriority");
|
|
|
|
|
2019-05-23 14:47:47 -07:00
|
|
|
close(pipe_c2p[1]);
|
|
|
|
close(pipe_p2c[0]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(pipe_c2p[1]); /* close write */
|
|
|
|
close(pipe_p2c[0]); /* close read */
|
|
|
|
|
2020-01-14 19:50:03 -08:00
|
|
|
skel = test_send_signal_kern__open_and_load();
|
2021-08-17 12:09:18 -07:00
|
|
|
if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
|
2020-01-14 19:50:03 -08:00
|
|
|
goto skel_open_load_failure;
|
2019-05-23 14:47:47 -07:00
|
|
|
|
2020-01-14 19:50:03 -08:00
|
|
|
if (!attr) {
|
|
|
|
err = test_send_signal_kern__attach(skel);
|
2021-08-17 12:09:18 -07:00
|
|
|
if (!ASSERT_OK(err, "skel_attach")) {
|
2020-01-14 19:50:03 -08:00
|
|
|
err = -1;
|
|
|
|
goto destroy_skel;
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-08 12:04:48 -08:00
|
|
|
pmu_fd = syscall(__NR_perf_event_open, attr, pid, -1 /* cpu */,
|
2020-01-14 19:50:03 -08:00
|
|
|
-1 /* group id */, 0 /* flags */);
|
2021-08-17 12:09:18 -07:00
|
|
|
if (!ASSERT_GE(pmu_fd, 0, "perf_event_open")) {
|
2020-01-14 19:50:03 -08:00
|
|
|
err = -1;
|
|
|
|
goto destroy_skel;
|
|
|
|
}
|
2019-05-23 14:47:47 -07:00
|
|
|
|
2020-01-14 19:50:03 -08:00
|
|
|
skel->links.send_signal_perf =
|
|
|
|
bpf_program__attach_perf_event(skel->progs.send_signal_perf, pmu_fd);
|
2021-05-24 20:59:32 -07:00
|
|
|
if (!ASSERT_OK_PTR(skel->links.send_signal_perf, "attach_perf_event"))
|
2020-01-14 19:50:03 -08:00
|
|
|
goto disable_pmu;
|
|
|
|
}
|
2019-05-23 14:47:47 -07:00
|
|
|
|
|
|
|
/* wait until child signal handler installed */
|
2021-08-17 12:09:18 -07:00
|
|
|
ASSERT_EQ(read(pipe_c2p[0], buf, 1), 1, "pipe_read");
|
2019-05-23 14:47:47 -07:00
|
|
|
|
|
|
|
/* trigger the bpf send_signal */
|
2020-01-14 19:50:03 -08:00
|
|
|
skel->bss->signal_thread = signal_thread;
|
2022-03-08 12:04:48 -08:00
|
|
|
skel->bss->sig = SIGUSR1;
|
|
|
|
skel->bss->pid = pid;
|
2019-05-23 14:47:47 -07:00
|
|
|
|
|
|
|
/* notify child that bpf program can send_signal now */
|
2021-08-17 12:09:18 -07:00
|
|
|
ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
|
2019-05-23 14:47:47 -07:00
|
|
|
|
|
|
|
/* wait for result */
|
|
|
|
err = read(pipe_c2p[0], buf, 1);
|
2021-08-17 12:09:18 -07:00
|
|
|
if (!ASSERT_GE(err, 0, "reading pipe"))
|
2019-05-23 14:47:47 -07:00
|
|
|
goto disable_pmu;
|
2021-08-17 12:09:18 -07:00
|
|
|
if (!ASSERT_GT(err, 0, "reading pipe error: size 0")) {
|
2019-05-23 14:47:47 -07:00
|
|
|
err = -1;
|
|
|
|
goto disable_pmu;
|
|
|
|
}
|
|
|
|
|
2021-08-17 12:09:18 -07:00
|
|
|
ASSERT_EQ(buf[0], '2', "incorrect result");
|
2019-05-23 14:47:47 -07:00
|
|
|
|
|
|
|
/* notify child safe to exit */
|
2021-08-17 12:09:18 -07:00
|
|
|
ASSERT_EQ(write(pipe_p2c[1], buf, 1), 1, "pipe_write");
|
2019-05-23 14:47:47 -07:00
|
|
|
|
|
|
|
disable_pmu:
|
|
|
|
close(pmu_fd);
|
2020-01-14 19:50:03 -08:00
|
|
|
destroy_skel:
|
|
|
|
test_send_signal_kern__destroy(skel);
|
|
|
|
skel_open_load_failure:
|
2019-05-23 14:47:47 -07:00
|
|
|
close(pipe_c2p[0]);
|
|
|
|
close(pipe_p2c[1]);
|
|
|
|
wait(NULL);
|
|
|
|
}
|
|
|
|
|
2020-01-14 19:50:03 -08:00
|
|
|
static void test_send_signal_tracepoint(bool signal_thread)
|
2019-05-23 14:47:47 -07:00
|
|
|
{
|
2021-08-17 12:09:18 -07:00
|
|
|
test_send_signal_common(NULL, signal_thread);
|
2019-05-23 14:47:47 -07:00
|
|
|
}
|
|
|
|
|
2020-01-14 19:50:03 -08:00
|
|
|
static void test_send_signal_perf(bool signal_thread)
|
2019-07-16 12:56:34 +02:00
|
|
|
{
|
|
|
|
struct perf_event_attr attr = {
|
|
|
|
.sample_period = 1,
|
|
|
|
.type = PERF_TYPE_SOFTWARE,
|
|
|
|
.config = PERF_COUNT_SW_CPU_CLOCK,
|
|
|
|
};
|
|
|
|
|
2021-08-17 12:09:18 -07:00
|
|
|
test_send_signal_common(&attr, signal_thread);
|
2019-07-16 12:56:34 +02:00
|
|
|
}
|
|
|
|
|
2020-01-14 19:50:03 -08:00
|
|
|
static void test_send_signal_nmi(bool signal_thread)
|
2019-05-23 14:47:47 -07:00
|
|
|
{
|
|
|
|
struct perf_event_attr attr = {
|
2020-01-16 09:40:04 -08:00
|
|
|
.sample_period = 1,
|
2019-05-23 14:47:47 -07:00
|
|
|
.type = PERF_TYPE_HARDWARE,
|
|
|
|
.config = PERF_COUNT_HW_CPU_CYCLES,
|
|
|
|
};
|
2019-07-16 12:56:34 +02:00
|
|
|
int pmu_fd;
|
|
|
|
|
|
|
|
/* Some setups (e.g. virtual machines) might run with hardware
|
|
|
|
* perf events disabled. If this is the case, skip this test.
|
|
|
|
*/
|
|
|
|
pmu_fd = syscall(__NR_perf_event_open, &attr, 0 /* pid */,
|
|
|
|
-1 /* cpu */, -1 /* group_fd */, 0 /* flags */);
|
|
|
|
if (pmu_fd == -1) {
|
|
|
|
if (errno == ENOENT) {
|
2019-08-06 10:45:28 -07:00
|
|
|
printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n",
|
|
|
|
__func__);
|
2019-08-21 16:44:24 -07:00
|
|
|
test__skip();
|
2019-08-21 16:44:27 -07:00
|
|
|
return;
|
2019-07-16 12:56:34 +02:00
|
|
|
}
|
|
|
|
/* Let the test fail with a more informative message */
|
|
|
|
} else {
|
|
|
|
close(pmu_fd);
|
|
|
|
}
|
2019-05-23 14:47:47 -07:00
|
|
|
|
2021-08-17 12:09:18 -07:00
|
|
|
test_send_signal_common(&attr, signal_thread);
|
2019-05-23 14:47:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_send_signal(void)
|
|
|
|
{
|
2019-07-27 20:25:31 -07:00
|
|
|
if (test__start_subtest("send_signal_tracepoint"))
|
2020-01-14 19:50:03 -08:00
|
|
|
test_send_signal_tracepoint(false);
|
2019-07-27 20:25:31 -07:00
|
|
|
if (test__start_subtest("send_signal_perf"))
|
2020-01-14 19:50:03 -08:00
|
|
|
test_send_signal_perf(false);
|
2019-07-27 20:25:31 -07:00
|
|
|
if (test__start_subtest("send_signal_nmi"))
|
2020-01-14 19:50:03 -08:00
|
|
|
test_send_signal_nmi(false);
|
|
|
|
if (test__start_subtest("send_signal_tracepoint_thread"))
|
|
|
|
test_send_signal_tracepoint(true);
|
|
|
|
if (test__start_subtest("send_signal_perf_thread"))
|
|
|
|
test_send_signal_perf(true);
|
|
|
|
if (test__start_subtest("send_signal_nmi_thread"))
|
|
|
|
test_send_signal_nmi(true);
|
2019-05-23 14:47:47 -07:00
|
|
|
}
|