1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (c) 2024 Meta Platforms, Inc. and affiliates. 4 * Copyright (c) 2024 David Vernet <dvernet@meta.com> 5 */ 6 #include <bpf/bpf.h> 7 #include <sched.h> 8 #include <scx/common.h> 9 #include <sys/wait.h> 10 #include <unistd.h> 11 #include "exit.bpf.skel.h" 12 #include "scx_test.h" 13 14 #include "exit_test.h" 15 16 static enum scx_test_status run(void *ctx) 17 { 18 enum exit_test_case tc; 19 20 for (tc = 0; tc < NUM_EXITS; tc++) { 21 struct exit *skel; 22 struct bpf_link *link; 23 char buf[16]; 24 25 /* 26 * On single-CPU systems, ops.select_cpu() is never 27 * invoked, so skip this test to avoid getting stuck 28 * indefinitely. 29 */ 30 if (tc == EXIT_SELECT_CPU && libbpf_num_possible_cpus() == 1) 31 continue; 32 33 skel = exit__open(); 34 SCX_FAIL_IF(!skel, "Failed to open"); 35 SCX_ENUM_INIT(skel); 36 skel->rodata->exit_point = tc; 37 SCX_FAIL_IF(exit__load(skel), "Failed to load skel"); 38 link = bpf_map__attach_struct_ops(skel->maps.exit_ops); 39 if (!link) { 40 SCX_ERR("Failed to attach scheduler"); 41 exit__destroy(skel); 42 return SCX_TEST_FAIL; 43 } 44 45 /* Assumes uei.kind is written last */ 46 while (skel->data->uei.kind == EXIT_KIND(SCX_EXIT_NONE)) 47 sched_yield(); 48 49 SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG_BPF)); 50 SCX_EQ(skel->data->uei.exit_code, tc); 51 sprintf(buf, "%d", tc); 52 SCX_ASSERT(!strcmp(skel->data->uei.msg, buf)); 53 bpf_link__destroy(link); 54 exit__destroy(skel); 55 } 56 57 return SCX_TEST_PASS; 58 } 59 60 struct scx_test exit_test = { 61 .name = "exit", 62 .description = "Verify we can cleanly exit a scheduler in multiple places", 63 .run = run, 64 }; 65 REGISTER_SCX_TEST(&exit_test) 66