1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2021 Facebook */ 3 #include <test_progs.h> 4 #include "timer.skel.h" 5 #include "timer_failure.skel.h" 6 #include "timer_interrupt.skel.h" 7 8 #define NUM_THR 8 9 10 static void *spin_lock_thread(void *arg) 11 { 12 int i, err, prog_fd = *(int *)arg; 13 LIBBPF_OPTS(bpf_test_run_opts, topts); 14 15 for (i = 0; i < 10000; i++) { 16 err = bpf_prog_test_run_opts(prog_fd, &topts); 17 if (!ASSERT_OK(err, "test_run_opts err") || 18 !ASSERT_OK(topts.retval, "test_run_opts retval")) 19 break; 20 } 21 22 pthread_exit(arg); 23 } 24 25 static int timer(struct timer *timer_skel) 26 { 27 int i, err, prog_fd; 28 LIBBPF_OPTS(bpf_test_run_opts, topts); 29 pthread_t thread_id[NUM_THR]; 30 void *ret; 31 32 err = timer__attach(timer_skel); 33 if (!ASSERT_OK(err, "timer_attach")) 34 return err; 35 36 ASSERT_EQ(timer_skel->data->callback_check, 52, "callback_check1"); 37 ASSERT_EQ(timer_skel->data->callback2_check, 52, "callback2_check1"); 38 ASSERT_EQ(timer_skel->bss->pinned_callback_check, 0, "pinned_callback_check1"); 39 40 prog_fd = bpf_program__fd(timer_skel->progs.test1); 41 err = bpf_prog_test_run_opts(prog_fd, &topts); 42 ASSERT_OK(err, "test_run"); 43 ASSERT_EQ(topts.retval, 0, "test_run"); 44 timer__detach(timer_skel); 45 46 usleep(50); /* 10 usecs should be enough, but give it extra */ 47 /* check that timer_cb1() was executed 10+10 times */ 48 ASSERT_EQ(timer_skel->data->callback_check, 42, "callback_check2"); 49 ASSERT_EQ(timer_skel->data->callback2_check, 42, "callback2_check2"); 50 51 /* check that timer_cb2() was executed twice */ 52 ASSERT_EQ(timer_skel->bss->bss_data, 10, "bss_data"); 53 54 /* check that timer_cb3() was executed twice */ 55 ASSERT_EQ(timer_skel->bss->abs_data, 12, "abs_data"); 56 57 /* check that timer_cb_pinned() was executed twice */ 58 ASSERT_EQ(timer_skel->bss->pinned_callback_check, 2, "pinned_callback_check"); 59 60 /* check that there were no errors in timer execution */ 61 ASSERT_EQ(timer_skel->bss->err, 0, "err"); 62 63 /* check that code paths completed */ 64 ASSERT_EQ(timer_skel->bss->ok, 1 | 2 | 4, "ok"); 65 66 prog_fd = bpf_program__fd(timer_skel->progs.race); 67 for (i = 0; i < NUM_THR; i++) { 68 err = pthread_create(&thread_id[i], NULL, 69 &spin_lock_thread, &prog_fd); 70 if (!ASSERT_OK(err, "pthread_create")) 71 break; 72 } 73 74 while (i) { 75 err = pthread_join(thread_id[--i], &ret); 76 if (ASSERT_OK(err, "pthread_join")) 77 ASSERT_EQ(ret, (void *)&prog_fd, "pthread_join"); 78 } 79 80 return 0; 81 } 82 83 /* TODO: use pid filtering */ 84 void serial_test_timer(void) 85 { 86 struct timer *timer_skel = NULL; 87 int err; 88 89 timer_skel = timer__open_and_load(); 90 if (!timer_skel && errno == EOPNOTSUPP) { 91 test__skip(); 92 return; 93 } 94 if (!ASSERT_OK_PTR(timer_skel, "timer_skel_load")) 95 return; 96 97 err = timer(timer_skel); 98 ASSERT_OK(err, "timer"); 99 timer__destroy(timer_skel); 100 101 RUN_TESTS(timer_failure); 102 } 103 104 void test_timer_interrupt(void) 105 { 106 struct timer_interrupt *skel = NULL; 107 int err, prog_fd; 108 LIBBPF_OPTS(bpf_test_run_opts, opts); 109 110 skel = timer_interrupt__open_and_load(); 111 if (!skel && errno == EOPNOTSUPP) { 112 test__skip(); 113 return; 114 } 115 if (!ASSERT_OK_PTR(skel, "timer_interrupt__open_and_load")) 116 return; 117 118 err = timer_interrupt__attach(skel); 119 if (!ASSERT_OK(err, "timer_interrupt__attach")) 120 goto out; 121 122 prog_fd = bpf_program__fd(skel->progs.test_timer_interrupt); 123 err = bpf_prog_test_run_opts(prog_fd, &opts); 124 if (!ASSERT_OK(err, "bpf_prog_test_run_opts")) 125 goto out; 126 127 usleep(50); 128 129 ASSERT_EQ(skel->bss->in_interrupt, 0, "in_interrupt"); 130 if (skel->bss->preempt_count) 131 ASSERT_NEQ(skel->bss->in_interrupt_cb, 0, "in_interrupt_cb"); 132 133 out: 134 timer_interrupt__destroy(skel); 135 } 136