accel-blocker.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/lockcnt.h"
  28. #include "qemu/thread.h"
  29. #include "qemu/main-loop.h"
  30. #include "hw/core/cpu.h"
  31. #include "sysemu/accel-blocker.h"
  32. static QemuLockCnt accel_in_ioctl_lock;
  33. static QemuEvent accel_in_ioctl_event;
  34. void accel_blocker_init(void)
  35. {
  36. qemu_lockcnt_init(&accel_in_ioctl_lock);
  37. qemu_event_init(&accel_in_ioctl_event, false);
  38. }
  39. void accel_ioctl_begin(void)
  40. {
  41. if (likely(bql_locked())) {
  42. return;
  43. }
  44. /* block if lock is taken in kvm_ioctl_inhibit_begin() */
  45. qemu_lockcnt_inc(&accel_in_ioctl_lock);
  46. }
  47. void accel_ioctl_end(void)
  48. {
  49. if (likely(bql_locked())) {
  50. return;
  51. }
  52. qemu_lockcnt_dec(&accel_in_ioctl_lock);
  53. /* change event to SET. If event was BUSY, wake up all waiters */
  54. qemu_event_set(&accel_in_ioctl_event);
  55. }
  56. void accel_cpu_ioctl_begin(CPUState *cpu)
  57. {
  58. if (unlikely(bql_locked())) {
  59. return;
  60. }
  61. /* block if lock is taken in kvm_ioctl_inhibit_begin() */
  62. qemu_lockcnt_inc(&cpu->in_ioctl_lock);
  63. }
  64. void accel_cpu_ioctl_end(CPUState *cpu)
  65. {
  66. if (unlikely(bql_locked())) {
  67. return;
  68. }
  69. qemu_lockcnt_dec(&cpu->in_ioctl_lock);
  70. /* change event to SET. If event was BUSY, wake up all waiters */
  71. qemu_event_set(&accel_in_ioctl_event);
  72. }
  73. static bool accel_has_to_wait(void)
  74. {
  75. CPUState *cpu;
  76. bool needs_to_wait = false;
  77. CPU_FOREACH(cpu) {
  78. if (qemu_lockcnt_count(&cpu->in_ioctl_lock)) {
  79. /* exit the ioctl, if vcpu is running it */
  80. qemu_cpu_kick(cpu);
  81. needs_to_wait = true;
  82. }
  83. }
  84. return needs_to_wait || qemu_lockcnt_count(&accel_in_ioctl_lock);
  85. }
  86. void accel_ioctl_inhibit_begin(void)
  87. {
  88. CPUState *cpu;
  89. /*
  90. * We allow to inhibit only when holding the BQL, so we can identify
  91. * when an inhibitor wants to issue an ioctl easily.
  92. */
  93. g_assert(bql_locked());
  94. /* Block further invocations of the ioctls outside the BQL. */
  95. CPU_FOREACH(cpu) {
  96. qemu_lockcnt_lock(&cpu->in_ioctl_lock);
  97. }
  98. qemu_lockcnt_lock(&accel_in_ioctl_lock);
  99. /* Keep waiting until there are running ioctls */
  100. while (true) {
  101. /* Reset event to FREE. */
  102. qemu_event_reset(&accel_in_ioctl_event);
  103. if (accel_has_to_wait()) {
  104. /*
  105. * If event is still FREE, and there are ioctls still in progress,
  106. * wait.
  107. *
  108. * If an ioctl finishes before qemu_event_wait(), it will change
  109. * the event state to SET. This will prevent qemu_event_wait() from
  110. * blocking, but it's not a problem because if other ioctls are
  111. * still running the loop will iterate once more and reset the event
  112. * status to FREE so that it can wait properly.
  113. *
  114. * If an ioctls finishes while qemu_event_wait() is blocking, then
  115. * it will be waken up, but also here the while loop makes sure
  116. * to re-enter the wait if there are other running ioctls.
  117. */
  118. qemu_event_wait(&accel_in_ioctl_event);
  119. } else {
  120. /* No ioctl is running */
  121. return;
  122. }
  123. }
  124. }
  125. void accel_ioctl_inhibit_end(void)
  126. {
  127. CPUState *cpu;
  128. qemu_lockcnt_unlock(&accel_in_ioctl_lock);
  129. CPU_FOREACH(cpu) {
  130. qemu_lockcnt_unlock(&cpu->in_ioctl_lock);
  131. }
  132. }