1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2023 Meta Platforms, Inc. and affiliates. 4 * Copyright (c) 2023 David Vernet <dvernet@meta.com> 5 * Copyright (c) 2023 Tejun Heo <tj@kernel.org> 6 */ 7 #include <bpf/bpf.h> 8 #include <scx/common.h> 9 #include <sys/wait.h> 10 #include <unistd.h> 11 #include "enq_select_cpu.bpf.skel.h" 12 #include "scx_test.h" 13 14 static enum scx_test_status setup(void **ctx) 15 { 16 struct enq_select_cpu *skel; 17 18 skel = enq_select_cpu__open(); 19 SCX_FAIL_IF(!skel, "Failed to open"); 20 SCX_ENUM_INIT(skel); 21 SCX_FAIL_IF(enq_select_cpu__load(skel), "Failed to load skel"); 22 23 *ctx = skel; 24 25 return SCX_TEST_PASS; 26 } 27 28 static int test_select_cpu_from_user(const struct enq_select_cpu *skel) 29 { 30 int fd, ret; 31 __u64 args[1]; 32 33 LIBBPF_OPTS(bpf_test_run_opts, attr, 34 .ctx_in = args, 35 .ctx_size_in = sizeof(args), 36 ); 37 38 args[0] = getpid(); 39 fd = bpf_program__fd(skel->progs.select_cpu_from_user); 40 if (fd < 0) 41 return fd; 42 43 ret = bpf_prog_test_run_opts(fd, &attr); 44 if (ret < 0) 45 return ret; 46 47 fprintf(stderr, "%s: CPU %d\n", __func__, attr.retval); 48 49 return 0; 50 } 51 52 static enum scx_test_status run(void *ctx) 53 { 54 struct enq_select_cpu *skel = ctx; 55 struct bpf_link *link; 56 57 link = bpf_map__attach_struct_ops(skel->maps.enq_select_cpu_ops); 58 if (!link) { 59 SCX_ERR("Failed to attach scheduler"); 60 return SCX_TEST_FAIL; 61 } 62 63 /* Pick an idle CPU from user-space */ 64 SCX_FAIL_IF(test_select_cpu_from_user(skel), "Failed to pick idle CPU"); 65 66 sleep(1); 67 68 SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_NONE)); 69 bpf_link__destroy(link); 70 71 return SCX_TEST_PASS; 72 } 73 74 static void cleanup(void *ctx) 75 { 76 struct enq_select_cpu *skel = ctx; 77 78 enq_select_cpu__destroy(skel); 79 } 80 81 struct scx_test enq_select_cpu = { 82 .name = "enq_select_cpu", 83 .description = "Verify scx_bpf_select_cpu_dfl() from multiple contexts", 84 .setup = setup, 85 .run = run, 86 .cleanup = cleanup, 87 }; 88 REGISTER_SCX_TEST(&enq_select_cpu) 89