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