summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/sched_ext/enq_select_cpu.bpf.c
blob: ee2c9b89716e03020062f3ad2ec0a71b4ac21d76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
 * Copyright (c) 2023 David Vernet <dvernet@meta.com>
 * Copyright (c) 2023 Tejun Heo <tj@kernel.org>
 */

#include <scx/common.bpf.h>

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

UEI_DEFINE(uei);

s32 BPF_STRUCT_OPS(enq_select_cpu_select_cpu, struct task_struct *p,
		   s32 prev_cpu, u64 wake_flags)
{
	/* Bounce all tasks to ops.enqueue() */
	return prev_cpu;
}

void BPF_STRUCT_OPS(enq_select_cpu_enqueue, struct task_struct *p,
		    u64 enq_flags)
{
	s32 cpu, prev_cpu = scx_bpf_task_cpu(p);
	bool found = false;

	cpu = scx_bpf_select_cpu_dfl(p, prev_cpu, 0, &found);
	if (found) {
		scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL_ON | cpu, SCX_SLICE_DFL, enq_flags);
		return;
	}

	scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, enq_flags);
}

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

struct task_cpu_arg {
	pid_t pid;
};

SEC("syscall")
int select_cpu_from_user(struct task_cpu_arg *input)
{
	struct task_struct *p;
	bool found = false;
	s32 cpu;

	p = bpf_task_from_pid(input->pid);
	if (!p)
		return -EINVAL;

	bpf_rcu_read_lock();
	cpu = scx_bpf_select_cpu_dfl(p, bpf_get_smp_processor_id(), 0, &found);
	if (!found)
		cpu = -EBUSY;
	bpf_rcu_read_unlock();

	bpf_task_release(p);

	return cpu;
}

SEC(".struct_ops.link")
struct sched_ext_ops enq_select_cpu_ops = {
	.select_cpu		= (void *)enq_select_cpu_select_cpu,
	.enqueue		= (void *)enq_select_cpu_enqueue,
	.exit			= (void *)enq_select_cpu_exit,
	.name			= "enq_select_cpu",
	.timeout_ms		= 1000U,
};