enq_select_cpu.bpf.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 <scx/common.bpf.h>
  8. char _license[] SEC("license") = "GPL";
  9. UEI_DEFINE(uei);
  10. s32 BPF_STRUCT_OPS(enq_select_cpu_select_cpu, struct task_struct *p,
  11. s32 prev_cpu, u64 wake_flags)
  12. {
  13. /* Bounce all tasks to ops.enqueue() */
  14. return prev_cpu;
  15. }
  16. void BPF_STRUCT_OPS(enq_select_cpu_enqueue, struct task_struct *p,
  17. u64 enq_flags)
  18. {
  19. s32 cpu, prev_cpu = scx_bpf_task_cpu(p);
  20. bool found = false;
  21. cpu = scx_bpf_select_cpu_dfl(p, prev_cpu, 0, &found);
  22. if (found) {
  23. scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL_ON | cpu, SCX_SLICE_DFL, enq_flags);
  24. return;
  25. }
  26. scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, enq_flags);
  27. }
  28. void BPF_STRUCT_OPS(enq_select_cpu_exit, struct scx_exit_info *ei)
  29. {
  30. UEI_RECORD(uei, ei);
  31. }
  32. struct task_cpu_arg {
  33. pid_t pid;
  34. };
  35. SEC("syscall")
  36. int select_cpu_from_user(struct task_cpu_arg *input)
  37. {
  38. struct task_struct *p;
  39. bool found = false;
  40. s32 cpu;
  41. p = bpf_task_from_pid(input->pid);
  42. if (!p)
  43. return -EINVAL;
  44. bpf_rcu_read_lock();
  45. cpu = scx_bpf_select_cpu_dfl(p, bpf_get_smp_processor_id(), 0, &found);
  46. if (!found)
  47. cpu = -EBUSY;
  48. bpf_rcu_read_unlock();
  49. bpf_task_release(p);
  50. return cpu;
  51. }
  52. SEC(".struct_ops.link")
  53. struct sched_ext_ops enq_select_cpu_ops = {
  54. .select_cpu = (void *)enq_select_cpu_select_cpu,
  55. .enqueue = (void *)enq_select_cpu_enqueue,
  56. .exit = (void *)enq_select_cpu_exit,
  57. .name = "enq_select_cpu",
  58. .timeout_ms = 1000U,
  59. };