cyclic_kick_wait.bpf.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Stress concurrent SCX_KICK_WAIT calls to reproduce wait-cycle deadlock.
  4. *
  5. * Three CPUs are designated from userspace. Every enqueue from one of the
  6. * three CPUs kicks the next CPU in the ring with SCX_KICK_WAIT, creating a
  7. * persistent A -> B -> C -> A wait cycle pressure.
  8. */
  9. #include <scx/common.bpf.h>
  10. char _license[] SEC("license") = "GPL";
  11. const volatile s32 test_cpu_a;
  12. const volatile s32 test_cpu_b;
  13. const volatile s32 test_cpu_c;
  14. u64 nr_enqueues;
  15. u64 nr_wait_kicks;
  16. UEI_DEFINE(uei);
  17. static s32 target_cpu(s32 cpu)
  18. {
  19. if (cpu == test_cpu_a)
  20. return test_cpu_b;
  21. if (cpu == test_cpu_b)
  22. return test_cpu_c;
  23. if (cpu == test_cpu_c)
  24. return test_cpu_a;
  25. return -1;
  26. }
  27. void BPF_STRUCT_OPS(cyclic_kick_wait_enqueue, struct task_struct *p,
  28. u64 enq_flags)
  29. {
  30. s32 this_cpu = bpf_get_smp_processor_id();
  31. s32 tgt;
  32. __sync_fetch_and_add(&nr_enqueues, 1);
  33. if (p->flags & PF_KTHREAD) {
  34. scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, SCX_SLICE_INF,
  35. enq_flags | SCX_ENQ_PREEMPT);
  36. return;
  37. }
  38. scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, enq_flags);
  39. tgt = target_cpu(this_cpu);
  40. if (tgt < 0 || tgt == this_cpu)
  41. return;
  42. __sync_fetch_and_add(&nr_wait_kicks, 1);
  43. scx_bpf_kick_cpu(tgt, SCX_KICK_WAIT);
  44. }
  45. void BPF_STRUCT_OPS(cyclic_kick_wait_exit, struct scx_exit_info *ei)
  46. {
  47. UEI_RECORD(uei, ei);
  48. }
  49. SEC(".struct_ops.link")
  50. struct sched_ext_ops cyclic_kick_wait_ops = {
  51. .enqueue = cyclic_kick_wait_enqueue,
  52. .exit = cyclic_kick_wait_exit,
  53. .name = "cyclic_kick_wait",
  54. .timeout_ms = 1000U,
  55. };