scx_cpu0.bpf.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * A CPU0 scheduler.
  4. *
  5. * This scheduler queues all tasks to a shared DSQ and only dispatches them on
  6. * CPU0 in FIFO order. This is useful for testing bypass behavior when many
  7. * tasks are concentrated on a single CPU. If the load balancer doesn't work,
  8. * bypass mode can trigger task hangs or RCU stalls as the queue is long and
  9. * there's only one CPU working on it.
  10. *
  11. * - Statistics tracking how many tasks are queued to local and CPU0 DSQs.
  12. * - Termination notification for userspace.
  13. *
  14. * Copyright (c) 2025 Meta Platforms, Inc. and affiliates.
  15. * Copyright (c) 2025 Tejun Heo <tj@kernel.org>
  16. */
  17. #include <scx/common.bpf.h>
  18. char _license[] SEC("license") = "GPL";
  19. const volatile u32 nr_cpus = 32; /* !0 for veristat, set during init */
  20. UEI_DEFINE(uei);
  21. /*
  22. * We create a custom DSQ with ID 0 that we dispatch to and consume from on
  23. * CPU0.
  24. */
  25. #define DSQ_CPU0 0
  26. struct {
  27. __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
  28. __uint(key_size, sizeof(u32));
  29. __uint(value_size, sizeof(u64));
  30. __uint(max_entries, 2); /* [local, cpu0] */
  31. } stats SEC(".maps");
  32. static void stat_inc(u32 idx)
  33. {
  34. u64 *cnt_p = bpf_map_lookup_elem(&stats, &idx);
  35. if (cnt_p)
  36. (*cnt_p)++;
  37. }
  38. s32 BPF_STRUCT_OPS(cpu0_select_cpu, struct task_struct *p, s32 prev_cpu, u64 wake_flags)
  39. {
  40. return 0;
  41. }
  42. void BPF_STRUCT_OPS(cpu0_enqueue, struct task_struct *p, u64 enq_flags)
  43. {
  44. /*
  45. * select_cpu() always picks CPU0. If @p is not on CPU0, it can't run on
  46. * CPU 0. Queue on whichever CPU it's currently only.
  47. */
  48. if (scx_bpf_task_cpu(p) != 0) {
  49. stat_inc(0); /* count local queueing */
  50. scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, SCX_SLICE_DFL, 0);
  51. return;
  52. }
  53. stat_inc(1); /* count cpu0 queueing */
  54. scx_bpf_dsq_insert(p, DSQ_CPU0, SCX_SLICE_DFL, enq_flags);
  55. }
  56. void BPF_STRUCT_OPS(cpu0_dispatch, s32 cpu, struct task_struct *prev)
  57. {
  58. if (cpu == 0)
  59. scx_bpf_dsq_move_to_local(DSQ_CPU0);
  60. }
  61. s32 BPF_STRUCT_OPS_SLEEPABLE(cpu0_init)
  62. {
  63. int ret;
  64. ret = scx_bpf_create_dsq(DSQ_CPU0, -1);
  65. if (ret) {
  66. scx_bpf_error("failed to create DSQ %d (%d)", DSQ_CPU0, ret);
  67. return ret;
  68. }
  69. return 0;
  70. }
  71. void BPF_STRUCT_OPS(cpu0_exit, struct scx_exit_info *ei)
  72. {
  73. UEI_RECORD(uei, ei);
  74. }
  75. SCX_OPS_DEFINE(cpu0_ops,
  76. .select_cpu = (void *)cpu0_select_cpu,
  77. .enqueue = (void *)cpu0_enqueue,
  78. .dispatch = (void *)cpu0_dispatch,
  79. .init = (void *)cpu0_init,
  80. .exit = (void *)cpu0_exit,
  81. .name = "cpu0");