Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

scx: Allow calling scx kfuncs from BPF_PROG_TYPE_SYSCALL #172

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions kernel/sched/ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -5282,6 +5282,8 @@ static int __init scx_init(void)
(ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
&scx_kfunc_set_any)) ||
(ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
&scx_kfunc_set_any)) ||
(ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
&scx_kfunc_set_any))) {
pr_err("sched_ext: Failed to register kfunc sets (%d)\n", ret);
return ret;
Expand Down
1 change: 1 addition & 0 deletions tools/testing/selftests/sched_ext/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ auto-test-targets := \
maximal \
maybe_null \
minimal \
prog_run \
reload_loop \
select_cpu_dfl \
select_cpu_dfl_nodispatch \
Expand Down
32 changes: 32 additions & 0 deletions tools/testing/selftests/sched_ext/prog_run.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* A scheduler that validates that we can invoke sched_ext kfuncs in
* BPF_PROG_TYPE_SYSCALL programs.
*
* Copyright (c) 2024 Meta Platforms, Inc. and affiliates.
* Copyright (c) 2024 David Vernet <[email protected]>
*/

#include <scx/common.bpf.h>

UEI_DEFINE(uei);

char _license[] SEC("license") = "GPL";

SEC("syscall")
int BPF_PROG(prog_run_syscall)
{
scx_bpf_exit(0xdeadbeef, "Exited from PROG_RUN");
return 0;
}

void BPF_STRUCT_OPS(prog_run_exit, struct scx_exit_info *ei)
{
UEI_RECORD(uei, ei);
}

SEC(".struct_ops.link")
struct sched_ext_ops prog_run_ops = {
.exit = prog_run_exit,
.name = "prog_run",
};
78 changes: 78 additions & 0 deletions tools/testing/selftests/sched_ext/prog_run.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2024 Meta Platforms, Inc. and affiliates.
* Copyright (c) 2024 David Vernet <[email protected]>
*/
#include <bpf/bpf.h>
#include <sched.h>
#include <scx/common.h>
#include <sys/wait.h>
#include <unistd.h>
#include "prog_run.bpf.skel.h"
#include "scx_test.h"

static enum scx_test_status setup(void **ctx)
{
struct prog_run *skel;

skel = prog_run__open_and_load();
if (!skel) {
SCX_ERR("Failed to open and load skel");
return SCX_TEST_FAIL;
}
*ctx = skel;

return SCX_TEST_PASS;
}

static enum scx_test_status run(void *ctx)
{
struct prog_run *skel = ctx;
struct bpf_link *link;
int prog_fd, err = 0;

prog_fd = bpf_program__fd(skel->progs.prog_run_syscall);
if (prog_fd < 0) {
SCX_ERR("Failed to get BPF_PROG_RUN prog");
return SCX_TEST_FAIL;
}

LIBBPF_OPTS(bpf_test_run_opts, topts);

link = bpf_map__attach_struct_ops(skel->maps.prog_run_ops);
if (!link) {
SCX_ERR("Failed to attach scheduler");
close(prog_fd);
return SCX_TEST_FAIL;
}

//err = bpf_prog_test_run_opts(prog_fd, &topts);
SCX_EQ(err, 0);

/* Assumes uei.kind is written last */
while (skel->data->uei.kind == SCX_EXIT_NONE)
sched_yield();

SCX_EQ(skel->data->uei.kind, SCX_EXIT_UNREG_BPF);
SCX_EQ(skel->data->uei.exit_code, 0xdeadbeef);
close(prog_fd);
bpf_link__destroy(link);

return SCX_TEST_PASS;
}

static void cleanup(void *ctx)
{
struct prog_run *skel = ctx;

prog_run__destroy(skel);
}

struct scx_test prog_run = {
.name = "prog_run",
.description = "Verify we can call into a scheduler with BPF_PROG_RUN, and invoke kfuncs",
.setup = setup,
.run = run,
.cleanup = cleanup,
};
REGISTER_SCX_TEST(&prog_run)