Skip to contentSkip to conversation

cpus: ignore ESRCH in qemu_cpu_kick_thread()

PATCH Applied Laurent Vivier 4 participants 7 messages Jan 2, 2019 – Jan 15, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpus: ignore ESRCH in qemu_cpu_kick_thread()

We can have a race condition between qemu_cpu_kick_thread() and qemu_kvm_cpu_thread_fn() when we hotunplug a CPU. In this case, qemu_cpu_kick_thread() can try to kick a thread that is exiting. pthread_kill() returns an error and qemu is stopped by an exit(1).

qemu:qemu_cpu_kick_thread: No such process

We can ignore safely this error.

---
 cpus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +1 -1
index 0ddeeefc14..4717490bd0 100644
@@ -1778,7 +1778,7 @@ static void qemu_cpu_kick_thread(CPUState *cpu)
     }
     cpu->thread_kicked = true;
     err = pthread_kill(cpu->thread->thread, SIG_IPI);
-    if (err) {
+    if (err && err != ESRCH) {
         fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
         exit(1);
     }
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Laurent Vivier

We can have a race condition between qemu_cpu_kick_thread() and qemu_kvm_cpu_thread_fn() when we hotunplug a CPU. In this case, qemu_cpu_kick_thread() can try to kick a thread that is exiting. pthread_kill() returns an error and qemu is stopped by an exit(1).

qemu:qemu_cpu_kick_thread: No such process

We can ignore safely this error.

Quoted 18 lines
---
 cpus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +1 -1
index 0ddeeefc14..4717490bd0 100644
@@ -1778,7 +1778,7 @@ static void qemu_cpu_kick_thread(CPUState *cpu)
     }
     cpu->thread_kicked = true;
     err = pthread_kill(cpu->thread->thread, SIG_IPI);
-    if (err) {
+    if (err && err != ESRCH) {
         fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
         exit(1);
     }
#2 lore raw
In reply to Laurent Vivier
Quoted 20 lines · Laurent Vivier

We can have a race condition between qemu_cpu_kick_thread() and qemu_kvm_cpu_thread_fn() when we hotunplug a CPU. In this case, qemu_cpu_kick_thread() can try to kick a thread that is exiting. pthread_kill() returns an error and qemu is stopped by an exit(1).

qemu:qemu_cpu_kick_thread: No such process

We can ignore safely this error.

---
 cpus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +0 -0
index 0ddeeefc14..4717490bd0 100644

cpus.c static void qemu_cpu_kick_thread(CPUState *cpu) · line 1784

Quoted 8 lines
cpus.c +1 -1
@@ -1778,7 +1778,7 @@ static void qemu_cpu_kick_thread(CPUState *cpu)
     }
     cpu->thread_kicked = true;
     err = pthread_kill(cpu->thread->thread, SIG_IPI);
-    if (err) {
+    if (err && err != ESRCH) {
         fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
         exit(1);
     }

You could in principle be sending the signal to another thread, so the fix is a bit hackish. However, I don't have a better idea that is not racy. :(

The problem is that qemu_cpu_kick does not use any spinlock or mutex to synchronize against cpu_remove_sync's qemu_thread_join. I think once the you reach qemu_cpu_kick in cpu_remove_sync (so if cpu->unplug) you do not need to reset cpu->thread_kicked anymore, but I don't think that's enough to fix it.

Paolo

#3 lore raw
Quoted 34 lines · Paolo Bonzini
Quoted 20 lines · Laurent Vivier

We can have a race condition between qemu_cpu_kick_thread() and qemu_kvm_cpu_thread_fn() when we hotunplug a CPU. In this case, qemu_cpu_kick_thread() can try to kick a thread that is exiting. pthread_kill() returns an error and qemu is stopped by an exit(1).

qemu:qemu_cpu_kick_thread: No such process

We can ignore safely this error.

---
 cpus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +0 -0
index 0ddeeefc14..4717490bd0 100644

cpus.c static void qemu_cpu_kick_thread(CPUState *cpu) · line 1784

Quoted 8 lines
cpus.c +1 -1
@@ -1778,7 +1778,7 @@ static void qemu_cpu_kick_thread(CPUState *cpu)
     }
     cpu->thread_kicked = true;
     err = pthread_kill(cpu->thread->thread, SIG_IPI);
-    if (err) {
+    if (err && err != ESRCH) {
         fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
         exit(1);
     }

You could in principle be sending the signal to another thread, so the fix is a bit hackish. However, I don't have a better idea that is not racy. :(

Quoted 5 lines

The problem is that qemu_cpu_kick does not use any spinlock or mutex to synchronize against cpu_remove_sync's qemu_thread_join. I think once the you reach qemu_cpu_kick in cpu_remove_sync (so if cpu->unplug) you do not need to reset cpu->thread_kicked anymore, but I don't think that's enough to fix it.

Will you take the patch through one of your pull requests or should I add it to the trivial-patches branch?

Thanks,
Laurent

#4 lore raw
Quoted 35 lines · Laurent Vivier
Quoted 34 lines · Paolo Bonzini
Quoted 20 lines · Laurent Vivier

We can have a race condition between qemu_cpu_kick_thread() and qemu_kvm_cpu_thread_fn() when we hotunplug a CPU. In this case, qemu_cpu_kick_thread() can try to kick a thread that is exiting. pthread_kill() returns an error and qemu is stopped by an exit(1).

qemu:qemu_cpu_kick_thread: No such process

We can ignore safely this error.

---
  cpus.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +0 -0
index 0ddeeefc14..4717490bd0 100644

cpus.c static void qemu_cpu_kick_thread(CPUState *cpu) · line 1784

Quoted 8 lines
cpus.c +1 -1
@@ -1778,7 +1778,7 @@ static void qemu_cpu_kick_thread(CPUState *cpu)
      }
      cpu->thread_kicked = true;
      err = pthread_kill(cpu->thread->thread, SIG_IPI);
-    if (err) {
+    if (err && err != ESRCH) {
          fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
          exit(1);
      }

You could in principle be sending the signal to another thread, so the fix is a bit hackish. However, I don't have a better idea that is not racy. :(

Quoted 8 lines
Quoted 5 lines

The problem is that qemu_cpu_kick does not use any spinlock or mutex to synchronize against cpu_remove_sync's qemu_thread_join. I think once the you reach qemu_cpu_kick in cpu_remove_sync (so if cpu->unplug) you do not need to reset cpu->thread_kicked anymore, but I don't think that's enough to fix it.

Will you take the patch through one of your pull requests or should I add it to the trivial-patches branch?

Paolo?

Thanks,
Laurent

#5 lore raw
Quoted 42 lines · Laurent Vivier
Quoted 41 lines · Laurent Vivier
Quoted 39 lines · Paolo Bonzini
Quoted 20 lines · Laurent Vivier

We can have a race condition between qemu_cpu_kick_thread() and qemu_kvm_cpu_thread_fn() when we hotunplug a CPU. In this case, qemu_cpu_kick_thread() can try to kick a thread that is exiting. pthread_kill() returns an error and qemu is stopped by an exit(1).

    qemu:qemu_cpu_kick_thread: No such process

We can ignore safely this error.

---
  cpus.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +0 -0
index 0ddeeefc14..4717490bd0 100644

cpus.c static void qemu_cpu_kick_thread(CPUState *cpu) · line 1784

Quoted 8 lines
cpus.c +1 -1
@@ -1778,7 +1778,7 @@ static void qemu_cpu_kick_thread(CPUState *cpu)
       }
       cpu->thread_kicked = true;
       err = pthread_kill(cpu->thread->thread, SIG_IPI);
-    if (err) {
+    if (err && err != ESRCH) {
           fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
           exit(1);
       }

You could in principle be sending the signal to another thread, so the fix is a bit hackish.  However, I don't have a better idea that is not racy. :(

The problem is that qemu_cpu_kick does not use any spinlock or mutex to synchronize against cpu_remove_sync's qemu_thread_join.  I think once the you reach qemu_cpu_kick in cpu_remove_sync (so if cpu->unplug) you do not need to reset cpu->thread_kicked anymore, but I don't think that's enough to fix it.

Quoted 4 lines
Quoted 2 lines

Will you take the patch through one of your pull requests or should I add it to the trivial-patches branch?

Paolo?

I queued it now, thanks.

Paolo

#6 lore raw
In reply to Paolo Bonzini
Quoted 34 lines · Paolo Bonzini
Quoted 20 lines · Laurent Vivier

We can have a race condition between qemu_cpu_kick_thread() and qemu_kvm_cpu_thread_fn() when we hotunplug a CPU. In this case, qemu_cpu_kick_thread() can try to kick a thread that is exiting. pthread_kill() returns an error and qemu is stopped by an exit(1).

qemu:qemu_cpu_kick_thread: No such process

We can ignore safely this error.

---
 cpus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +0 -0
index 0ddeeefc14..4717490bd0 100644

cpus.c static void qemu_cpu_kick_thread(CPUState *cpu) · line 1784

Quoted 8 lines
cpus.c +1 -1
@@ -1778,7 +1778,7 @@ static void qemu_cpu_kick_thread(CPUState *cpu)
     }
     cpu->thread_kicked = true;
     err = pthread_kill(cpu->thread->thread, SIG_IPI);
-    if (err) {
+    if (err && err != ESRCH) {
         fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
         exit(1);
     }

You could in principle be sending the signal to another thread, so the fix is a bit hackish. However, I don't have a better idea that is not racy. :(

Quoted 5 lines

The problem is that qemu_cpu_kick does not use any spinlock or mutex to synchronize against cpu_remove_sync's qemu_thread_join. I think once the you reach qemu_cpu_kick in cpu_remove_sync (so if cpu->unplug) you do not need to reset cpu->thread_kicked anymore, but I don't think that's enough to fix it.

I think the per-cpu lock series[1] can help here. For instance, in qemu_cpu_kick_thread we can acquire the CPU lock, then check cpu->unplug. If it's set, then we don't send the signal, because the thread is on its way out. If it isn't set, then we send the signal while still holding the CPU lock. This guarantees that the thread exists, since cpu_remove_sync will acquire the CPU lock to set cpu->unplug.

Thanks,

Emilio

[1] https://lists.gnu.org/archive/html/qemu-devel/2018-12/msg02979.html