allowed_cpus.bpf.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * A scheduler that validates the behavior of scx_bpf_select_cpu_and() by
  4. * selecting idle CPUs strictly within a subset of allowed CPUs.
  5. *
  6. * Copyright (c) 2025 Andrea Righi <arighi@nvidia.com>
  7. */
  8. #include <scx/common.bpf.h>
  9. char _license[] SEC("license") = "GPL";
  10. UEI_DEFINE(uei);
  11. private(PREF_CPUS) struct bpf_cpumask __kptr * allowed_cpumask;
  12. static void
  13. validate_idle_cpu(const struct task_struct *p, const struct cpumask *allowed, s32 cpu)
  14. {
  15. if (scx_bpf_test_and_clear_cpu_idle(cpu))
  16. scx_bpf_error("CPU %d should be marked as busy", cpu);
  17. if (bpf_cpumask_subset(allowed, p->cpus_ptr) &&
  18. !bpf_cpumask_test_cpu(cpu, allowed))
  19. scx_bpf_error("CPU %d not in the allowed domain for %d (%s)",
  20. cpu, p->pid, p->comm);
  21. }
  22. s32 BPF_STRUCT_OPS(allowed_cpus_select_cpu,
  23. struct task_struct *p, s32 prev_cpu, u64 wake_flags)
  24. {
  25. const struct cpumask *allowed;
  26. s32 cpu;
  27. allowed = cast_mask(allowed_cpumask);
  28. if (!allowed) {
  29. scx_bpf_error("allowed domain not initialized");
  30. return -EINVAL;
  31. }
  32. /*
  33. * Select an idle CPU strictly within the allowed domain.
  34. */
  35. cpu = scx_bpf_select_cpu_and(p, prev_cpu, wake_flags, allowed, 0);
  36. if (cpu >= 0) {
  37. validate_idle_cpu(p, allowed, cpu);
  38. scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, SCX_SLICE_DFL, 0);
  39. return cpu;
  40. }
  41. return prev_cpu;
  42. }
  43. void BPF_STRUCT_OPS(allowed_cpus_enqueue, struct task_struct *p, u64 enq_flags)
  44. {
  45. const struct cpumask *allowed;
  46. s32 prev_cpu = scx_bpf_task_cpu(p), cpu;
  47. scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, 0);
  48. allowed = cast_mask(allowed_cpumask);
  49. if (!allowed) {
  50. scx_bpf_error("allowed domain not initialized");
  51. return;
  52. }
  53. /*
  54. * Use scx_bpf_select_cpu_and() to proactively kick an idle CPU
  55. * within @allowed_cpumask, usable by @p.
  56. */
  57. cpu = scx_bpf_select_cpu_and(p, prev_cpu, 0, allowed, 0);
  58. if (cpu >= 0) {
  59. validate_idle_cpu(p, allowed, cpu);
  60. scx_bpf_kick_cpu(cpu, SCX_KICK_IDLE);
  61. }
  62. }
  63. s32 BPF_STRUCT_OPS_SLEEPABLE(allowed_cpus_init)
  64. {
  65. struct bpf_cpumask *mask;
  66. mask = bpf_cpumask_create();
  67. if (!mask)
  68. return -ENOMEM;
  69. mask = bpf_kptr_xchg(&allowed_cpumask, mask);
  70. if (mask)
  71. bpf_cpumask_release(mask);
  72. bpf_rcu_read_lock();
  73. /*
  74. * Assign the first online CPU to the allowed domain.
  75. */
  76. mask = allowed_cpumask;
  77. if (mask) {
  78. const struct cpumask *online = scx_bpf_get_online_cpumask();
  79. bpf_cpumask_set_cpu(bpf_cpumask_first(online), mask);
  80. scx_bpf_put_cpumask(online);
  81. }
  82. bpf_rcu_read_unlock();
  83. return 0;
  84. }
  85. void BPF_STRUCT_OPS(allowed_cpus_exit, struct scx_exit_info *ei)
  86. {
  87. UEI_RECORD(uei, ei);
  88. }
  89. struct task_cpu_arg {
  90. pid_t pid;
  91. };
  92. SEC("syscall")
  93. int select_cpu_from_user(struct task_cpu_arg *input)
  94. {
  95. struct task_struct *p;
  96. int cpu;
  97. p = bpf_task_from_pid(input->pid);
  98. if (!p)
  99. return -EINVAL;
  100. bpf_rcu_read_lock();
  101. cpu = scx_bpf_select_cpu_and(p, bpf_get_smp_processor_id(), 0, p->cpus_ptr, 0);
  102. bpf_rcu_read_unlock();
  103. bpf_task_release(p);
  104. return cpu;
  105. }
  106. SEC(".struct_ops.link")
  107. struct sched_ext_ops allowed_cpus_ops = {
  108. .select_cpu = (void *)allowed_cpus_select_cpu,
  109. .enqueue = (void *)allowed_cpus_enqueue,
  110. .init = (void *)allowed_cpus_init,
  111. .exit = (void *)allowed_cpus_exit,
  112. .name = "allowed_cpus",
  113. };