accel-blocker.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Lock to inhibit accelerator ioctls
  3. *
  4. * Copyright (c) 2022 Red Hat Inc.
  5. *
  6. * Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "qemu/thread.h"
  28. #include "qemu/main-loop.h"
  29. #include "hw/core/cpu.h"
  30. #include "sysemu/accel-blocker.h"
  31. static QemuLockCnt accel_in_ioctl_lock;
  32. static QemuEvent accel_in_ioctl_event;
  33. void accel_blocker_init(void)
  34. {
  35. qemu_lockcnt_init(&accel_in_ioctl_lock);
  36. qemu_event_init(&accel_in_ioctl_event, false);
  37. }
  38. void accel_ioctl_begin(void)
  39. {
  40. if (likely(qemu_mutex_iothread_locked())) {
  41. return;
  42. }
  43. /* block if lock is taken in kvm_ioctl_inhibit_begin() */
  44. qemu_lockcnt_inc(&accel_in_ioctl_lock);
  45. }
  46. void accel_ioctl_end(void)
  47. {
  48. if (likely(qemu_mutex_iothread_locked())) {
  49. return;
  50. }
  51. qemu_lockcnt_dec(&accel_in_ioctl_lock);
  52. /* change event to SET. If event was BUSY, wake up all waiters */
  53. qemu_event_set(&accel_in_ioctl_event);
  54. }
  55. void accel_cpu_ioctl_begin(CPUState *cpu)
  56. {
  57. if (unlikely(qemu_mutex_iothread_locked())) {
  58. return;
  59. }
  60. /* block if lock is taken in kvm_ioctl_inhibit_begin() */
  61. qemu_lockcnt_inc(&cpu->in_ioctl_lock);
  62. }
  63. void accel_cpu_ioctl_end(CPUState *cpu)
  64. {
  65. if (unlikely(qemu_mutex_iothread_locked())) {
  66. return;
  67. }
  68. qemu_lockcnt_dec(&cpu->in_ioctl_lock);
  69. /* change event to SET. If event was BUSY, wake up all waiters */
  70. qemu_event_set(&accel_in_ioctl_event);
  71. }
  72. static bool accel_has_to_wait(void)
  73. {
  74. CPUState *cpu;
  75. bool needs_to_wait = false;
  76. CPU_FOREACH(cpu) {
  77. if (qemu_lockcnt_count(&cpu->in_ioctl_lock)) {
  78. /* exit the ioctl, if vcpu is running it */
  79. qemu_cpu_kick(cpu);
  80. needs_to_wait = true;
  81. }
  82. }
  83. return needs_to_wait || qemu_lockcnt_count(&accel_in_ioctl_lock);
  84. }
  85. void accel_ioctl_inhibit_begin(void)
  86. {
  87. CPUState *cpu;
  88. /*
  89. * We allow to inhibit only when holding the BQL, so we can identify
  90. * when an inhibitor wants to issue an ioctl easily.
  91. */
  92. g_assert(qemu_mutex_iothread_locked());
  93. /* Block further invocations of the ioctls outside the BQL. */
  94. CPU_FOREACH(cpu) {
  95. qemu_lockcnt_lock(&cpu->in_ioctl_lock);
  96. }
  97. qemu_lockcnt_lock(&accel_in_ioctl_lock);
  98. /* Keep waiting until there are running ioctls */
  99. while (true) {
  100. /* Reset event to FREE. */
  101. qemu_event_reset(&accel_in_ioctl_event);
  102. if (accel_has_to_wait()) {
  103. /*
  104. * If event is still FREE, and there are ioctls still in progress,
  105. * wait.
  106. *
  107. * If an ioctl finishes before qemu_event_wait(), it will change
  108. * the event state to SET. This will prevent qemu_event_wait() from
  109. * blocking, but it's not a problem because if other ioctls are
  110. * still running the loop will iterate once more and reset the event
  111. * status to FREE so that it can wait properly.
  112. *
  113. * If an ioctls finishes while qemu_event_wait() is blocking, then
  114. * it will be waken up, but also here the while loop makes sure
  115. * to re-enter the wait if there are other running ioctls.
  116. */
  117. qemu_event_wait(&accel_in_ioctl_event);
  118. } else {
  119. /* No ioctl is running */
  120. return;
  121. }
  122. }
  123. }
  124. void accel_ioctl_inhibit_end(void)
  125. {
  126. CPUState *cpu;
  127. qemu_lockcnt_unlock(&accel_in_ioctl_lock);
  128. CPU_FOREACH(cpu) {
  129. qemu_lockcnt_unlock(&cpu->in_ioctl_lock);
  130. }
  131. }