mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-28 12:32:05 -06:00
cpus: take seqlock across qemu_icount updates
Even though writes of qemu_icount can safely race with reads in qemu_icount_raw, qemu_icount is also read by icount_adjust, which runs in the I/O thread. Therefore, writes do needs protection of the vm_clock_lock; for simplicity the patch protects it with both seqlock+spinlock, which we already do for hosts that lack 64-bit atomics. The bug actually predated the introduction of vm_clock_lock; cpu_update_icount would have needed the BQL before the spinlock was introduced. Reported-by: Emilio G. Cota <cota@braap.org> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
39fe576c82
commit
9b4e6f4966
1 changed files with 14 additions and 8 deletions
22
cpus.c
22
cpus.c
|
@ -245,21 +245,27 @@ static int64_t cpu_get_icount_executed(CPUState *cpu)
|
||||||
* account executed instructions. This is done by the TCG vCPU
|
* account executed instructions. This is done by the TCG vCPU
|
||||||
* thread so the main-loop can see time has moved forward.
|
* thread so the main-loop can see time has moved forward.
|
||||||
*/
|
*/
|
||||||
void cpu_update_icount(CPUState *cpu)
|
static void cpu_update_icount_locked(CPUState *cpu)
|
||||||
{
|
{
|
||||||
int64_t executed = cpu_get_icount_executed(cpu);
|
int64_t executed = cpu_get_icount_executed(cpu);
|
||||||
cpu->icount_budget -= executed;
|
cpu->icount_budget -= executed;
|
||||||
|
|
||||||
#ifndef CONFIG_ATOMIC64
|
|
||||||
seqlock_write_lock(&timers_state.vm_clock_seqlock,
|
|
||||||
&timers_state.vm_clock_lock);
|
|
||||||
#endif
|
|
||||||
atomic_set__nocheck(&timers_state.qemu_icount,
|
atomic_set__nocheck(&timers_state.qemu_icount,
|
||||||
timers_state.qemu_icount + executed);
|
timers_state.qemu_icount + executed);
|
||||||
#ifndef CONFIG_ATOMIC64
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Update the global shared timer_state.qemu_icount to take into
|
||||||
|
* account executed instructions. This is done by the TCG vCPU
|
||||||
|
* thread so the main-loop can see time has moved forward.
|
||||||
|
*/
|
||||||
|
void cpu_update_icount(CPUState *cpu)
|
||||||
|
{
|
||||||
|
seqlock_write_lock(&timers_state.vm_clock_seqlock,
|
||||||
|
&timers_state.vm_clock_lock);
|
||||||
|
cpu_update_icount_locked(cpu);
|
||||||
seqlock_write_unlock(&timers_state.vm_clock_seqlock,
|
seqlock_write_unlock(&timers_state.vm_clock_seqlock,
|
||||||
&timers_state.vm_clock_lock);
|
&timers_state.vm_clock_lock);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int64_t cpu_get_icount_raw_locked(void)
|
static int64_t cpu_get_icount_raw_locked(void)
|
||||||
|
@ -272,7 +278,7 @@ static int64_t cpu_get_icount_raw_locked(void)
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
/* Take into account what has run */
|
/* Take into account what has run */
|
||||||
cpu_update_icount(cpu);
|
cpu_update_icount_locked(cpu);
|
||||||
}
|
}
|
||||||
/* The read is protected by the seqlock, so __nocheck is okay. */
|
/* The read is protected by the seqlock, so __nocheck is okay. */
|
||||||
return atomic_read__nocheck(&timers_state.qemu_icount);
|
return atomic_read__nocheck(&timers_state.qemu_icount);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue