1a5db7817SDavid Vernet /* SPDX-License-Identifier: GPL-2.0 */
2a5db7817SDavid Vernet /*
3a5db7817SDavid Vernet * Copyright (c) 2024 Meta Platforms, Inc. and affiliates.
4a5db7817SDavid Vernet * Copyright (c) 2024 David Vernet <dvernet@meta.com>
5a5db7817SDavid Vernet */
6a5db7817SDavid Vernet #include <scx/common.bpf.h>
7a5db7817SDavid Vernet
8a5db7817SDavid Vernet char _license[] SEC("license") = "GPL";
9a5db7817SDavid Vernet const volatile s32 nr_cpus;
10a5db7817SDavid Vernet
11a5db7817SDavid Vernet UEI_DEFINE(uei);
12a5db7817SDavid Vernet
13a5db7817SDavid Vernet struct {
14a5db7817SDavid Vernet __uint(type, BPF_MAP_TYPE_QUEUE);
15a5db7817SDavid Vernet __uint(max_entries, 8192);
16a5db7817SDavid Vernet __type(value, s32);
17a5db7817SDavid Vernet } queue SEC(".maps");
18a5db7817SDavid Vernet
BPF_STRUCT_OPS(dsp_local_on_select_cpu,struct task_struct * p,s32 prev_cpu,u64 wake_flags)19a5db7817SDavid Vernet s32 BPF_STRUCT_OPS(dsp_local_on_select_cpu, struct task_struct *p,
20a5db7817SDavid Vernet s32 prev_cpu, u64 wake_flags)
21a5db7817SDavid Vernet {
22a5db7817SDavid Vernet return prev_cpu;
23a5db7817SDavid Vernet }
24a5db7817SDavid Vernet
BPF_STRUCT_OPS(dsp_local_on_enqueue,struct task_struct * p,u64 enq_flags)25a5db7817SDavid Vernet void BPF_STRUCT_OPS(dsp_local_on_enqueue, struct task_struct *p,
26a5db7817SDavid Vernet u64 enq_flags)
27a5db7817SDavid Vernet {
28a5db7817SDavid Vernet s32 pid = p->pid;
29a5db7817SDavid Vernet
30a5db7817SDavid Vernet if (bpf_map_push_elem(&queue, &pid, 0))
31a5db7817SDavid Vernet scx_bpf_error("Failed to enqueue %s[%d]", p->comm, p->pid);
32a5db7817SDavid Vernet }
33a5db7817SDavid Vernet
BPF_STRUCT_OPS(dsp_local_on_dispatch,s32 cpu,struct task_struct * prev)34a5db7817SDavid Vernet void BPF_STRUCT_OPS(dsp_local_on_dispatch, s32 cpu, struct task_struct *prev)
35a5db7817SDavid Vernet {
36a5db7817SDavid Vernet s32 pid, target;
37a5db7817SDavid Vernet struct task_struct *p;
38a5db7817SDavid Vernet
39a5db7817SDavid Vernet if (bpf_map_pop_elem(&queue, &pid))
40a5db7817SDavid Vernet return;
41a5db7817SDavid Vernet
42a5db7817SDavid Vernet p = bpf_task_from_pid(pid);
43a5db7817SDavid Vernet if (!p)
44a5db7817SDavid Vernet return;
45a5db7817SDavid Vernet
46a5db7817SDavid Vernet target = bpf_get_prandom_u32() % nr_cpus;
47a5db7817SDavid Vernet
48a5db7817SDavid Vernet scx_bpf_dispatch(p, SCX_DSQ_LOCAL_ON | target, SCX_SLICE_DFL, 0);
49a5db7817SDavid Vernet bpf_task_release(p);
50a5db7817SDavid Vernet }
51a5db7817SDavid Vernet
BPF_STRUCT_OPS(dsp_local_on_exit,struct scx_exit_info * ei)52a5db7817SDavid Vernet void BPF_STRUCT_OPS(dsp_local_on_exit, struct scx_exit_info *ei)
53a5db7817SDavid Vernet {
54a5db7817SDavid Vernet UEI_RECORD(uei, ei);
55a5db7817SDavid Vernet }
56a5db7817SDavid Vernet
57a5db7817SDavid Vernet SEC(".struct_ops.link")
58a5db7817SDavid Vernet struct sched_ext_ops dsp_local_on_ops = {
59*4f7f4170SVishal Chourasia .select_cpu = (void *) dsp_local_on_select_cpu,
60*4f7f4170SVishal Chourasia .enqueue = (void *) dsp_local_on_enqueue,
61*4f7f4170SVishal Chourasia .dispatch = (void *) dsp_local_on_dispatch,
62*4f7f4170SVishal Chourasia .exit = (void *) dsp_local_on_exit,
63a5db7817SDavid Vernet .name = "dsp_local_on",
64a5db7817SDavid Vernet .timeout_ms = 1000U,
65a5db7817SDavid Vernet };
66