Skip to contentSkip to conversation

accel/tcg: mttcg remove false-negative halted assertion

PATCH Applied Nicholas Piggin 3 participants 4 messages Aug 29, 2023 – Sep 27, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/tcg: mttcg remove false-negative halted assertion

mttcg asserts that an execution ending with EXCP_HALTED must have cpu->halted. However between the event or instruction that sets cpu->halted and requests exit and the assertion here, an asynchronous event could clear cpu->halted.

This leads to crashes running AIX on ppc/pseries because it uses H_CEDE/H_PROD hcalls, where H_CEDE sets self->halted = 1 and H_PROD sets other cpu->halted = 0 and kicks it.

H_PROD could be turned into an interrupt to wake, but several other places in ppc, sparc, and semihosting follow what looks like a similar pattern setting halted = 0 directly. So remove this assertion.

---
 accel/tcg/tcg-accel-ops-mttcg.c | 11 -----------
 1 file changed, 11 deletions(-)
accel/tcg/tcg-accel-ops-mttcg.c +0 -11
index b276262007..d0b6f288d9 100644
@@ -98,17 +98,6 @@ static void *mttcg_cpu_thread_fn(void *arg)
             case EXCP_DEBUG:
                 cpu_handle_guest_debug(cpu);
                 break;
-            case EXCP_HALTED:
-                /*
-                 * during start-up the vCPU is reset and the thread is
-                 * kicked several times. If we don't ensure we go back
-                 * to sleep in the halted state we won't cleanly
-                 * start-up when the vCPU is enabled.
-                 *
-                 * cpu->halted should ensure we sleep in wait_io_event
-                 */
-                g_assert(cpu->halted);
-                break;
             case EXCP_ATOMIC:
                 qemu_mutex_unlock_iothread();
                 cpu_exec_step_atomic(cpu);
Signature · 1 line
2.40.1
Reviewed-by on v1 1/1 Applied
#1 lore raw
Quoted 13 lines · Nicholas Piggin

mttcg asserts that an execution ending with EXCP_HALTED must have cpu->halted. However between the event or instruction that sets cpu->halted and requests exit and the assertion here, an asynchronous event could clear cpu->halted.

This leads to crashes running AIX on ppc/pseries because it uses H_CEDE/H_PROD hcalls, where H_CEDE sets self->halted = 1 and H_PROD sets other cpu->halted = 0 and kicks it.

H_PROD could be turned into an interrupt to wake, but several other places in ppc, sparc, and semihosting follow what looks like a similar pattern setting halted = 0 directly. So remove this assertion.

Quoted 5 lines
---
  accel/tcg/tcg-accel-ops-mttcg.c | 11 -----------
  1 file changed, 11 deletions(-)

The adjustments of 'halted' and 'prod' are done under the io lock in both cases, so there's no race there.

It is perfectly reasonable that after thread A sets halted and drops the lock, thread B may acquire the lock and clear halted before thread A has a chance to complete longjmp and cycle through its main loop.

Quoted 10 lines
accel/tcg/tcg-accel-ops-mttcg.c +0 -1
index b276262007..d0b6f288d9 100644
@@ -98,17 +98,6 @@ static void *mttcg_cpu_thread_fn(void *arg)
              case EXCP_DEBUG:
                  cpu_handle_guest_debug(cpu);
                  break;
-            case EXCP_HALTED:

accel/tcg/tcg-accel-ops-mttcg.c static void *mttcg_cpu_thread_fn(void *arg) · line 111

Quoted 10 lines
accel/tcg/tcg-accel-ops-mttcg.c +0 -10
-                /*
-                 * during start-up the vCPU is reset and the thread is
-                 * kicked several times. If we don't ensure we go back
-                 * to sleep in the halted state we won't cleanly
-                 * start-up when the vCPU is enabled.
-                 *
-                 * cpu->halted should ensure we sleep in wait_io_event
-                 */
-                g_assert(cpu->halted);
-                break;

I adjusted the patch to keep the case label and update the comment, still dropping the assert.

Queued to tcg-next.

r~

#2 lore raw
In reply to Nicholas Piggin
Quoted 9 lines · Nicholas Piggin

mttcg asserts that an execution ending with EXCP_HALTED must have cpu->halted. However between the event or instruction that sets cpu->halted and requests exit and the assertion here, an asynchronous event could clear cpu->halted.

This leads to crashes running AIX on ppc/pseries because it uses H_CEDE/H_PROD hcalls, where H_CEDE sets self->halted = 1 and H_PROD sets other cpu->halted = 0 and kicks it.

Quoted 6 lines

H_PROD could be turned into an interrupt to wake, but several other places in ppc, sparc, and semihosting follow what looks like a similar pattern setting halted = 0 directly. So remove this assertion.

This one also smells like a stable material, is it not?

Thanks,

/mjt

Quoted 22 lines
accel/tcg/tcg-accel-ops-mttcg.c +0 -11
index b276262007..d0b6f288d9 100644
@@ -98,17 +98,6 @@ static void *mttcg_cpu_thread_fn(void *arg)
              case EXCP_DEBUG:
                  cpu_handle_guest_debug(cpu);
                  break;
-            case EXCP_HALTED:
-                /*
-                 * during start-up the vCPU is reset and the thread is
-                 * kicked several times. If we don't ensure we go back
-                 * to sleep in the halted state we won't cleanly
-                 * start-up when the vCPU is enabled.
-                 *
-                 * cpu->halted should ensure we sleep in wait_io_event
-                 */
-                g_assert(cpu->halted);
-                break;
              case EXCP_ATOMIC:
                  qemu_mutex_unlock_iothread();
                  cpu_exec_step_atomic(cpu);
#3 lore raw
Quoted 10 lines · Michael Tokarev
Quoted 9 lines · Nicholas Piggin

mttcg asserts that an execution ending with EXCP_HALTED must have cpu->halted. However between the event or instruction that sets cpu->halted and requests exit and the assertion here, an asynchronous event could clear cpu->halted.

This leads to crashes running AIX on ppc/pseries because it uses H_CEDE/H_PROD hcalls, where H_CEDE sets self->halted = 1 and H_PROD sets other cpu->halted = 0 and kicks it.

Quoted 8 lines
Quoted 6 lines

H_PROD could be turned into an interrupt to wake, but several other places in ppc, sparc, and semihosting follow what looks like a similar pattern setting halted = 0 directly. So remove this assertion.

This one also smells like a stable material, is it not?

Yeah I would say it is.

Thanks,
Nick

Quoted 27 lines

Thanks,

/mjt

Quoted 22 lines
accel/tcg/tcg-accel-ops-mttcg.c +0 -11
index b276262007..d0b6f288d9 100644
@@ -98,17 +98,6 @@ static void *mttcg_cpu_thread_fn(void *arg)
              case EXCP_DEBUG:
                  cpu_handle_guest_debug(cpu);
                  break;
-            case EXCP_HALTED:
-                /*
-                 * during start-up the vCPU is reset and the thread is
-                 * kicked several times. If we don't ensure we go back
-                 * to sleep in the halted state we won't cleanly
-                 * start-up when the vCPU is enabled.
-                 *
-                 * cpu->halted should ensure we sleep in wait_io_event
-                 */
-                g_assert(cpu->halted);
-                break;
              case EXCP_ATOMIC:
                  qemu_mutex_unlock_iothread();
                  cpu_exec_step_atomic(cpu);