Signal-free qemu_cpu_kick for TCG
cover Signal-free qemu_cpu_kick for TCG
The first two patches remove most uses of cpu_exit outside the CPU loop. The third patch converts qemu_cpu_kick to do memory accesses from the iothread instead of using a signal.
Paolo
Paolo Bonzini (3):
i8257: rewrite DMA_schedule to avoid hooking into the CPU loop
i8257: remove cpu_request_exit irq
tcg: signal-free qemu_cpu_kick
cpu-exec.c | 18 ++++------ cpus.c | 91 ++++++++++++++----------------------------------- gdbstub.c | 2 +- hw/block/fdc.c | 2 +- hw/dma/i82374.c | 5 +-- hw/dma/i8257.c | 31 +++++++++-------- hw/i386/pc.c | 13 +------ hw/isa/i82378.c | 3 +- hw/mips/mips_fulong2e.c | 13 +------ hw/mips/mips_jazz.c | 13 +------ hw/mips/mips_malta.c | 13 +------ hw/ppc/prep.c | 11 ------ hw/ppc/spapr_rtas.c | 2 +- hw/sparc/sun4m.c | 4 +-- hw/sparc64/sun4u.c | 4 +-- include/hw/isa/isa.h | 4 +-- qom/cpu.c | 2 ++ 17 files changed, 64 insertions(+), 167 deletions(-)
Signature · 1 line
2.4.3
PATCH 2/3 i8257: remove cpu_request_exit irq
This is unused. cpu_exit now is almost exclusively an internal function to the CPU execution loop. The next patch will change the remaining occurrences to qemu_cpu_kick, making it truly internal.
--- hw/dma/i82374.c | 5 +---- hw/dma/i8257.c | 13 ++++--------- hw/i386/pc.c | 13 +------------ hw/isa/i82378.c | 3 +-- hw/mips/mips_fulong2e.c | 13 +------------ hw/mips/mips_jazz.c | 13 +------------ hw/mips/mips_malta.c | 13 +------------ hw/ppc/prep.c | 11 ----------- hw/sparc/sun4m.c | 2 +- hw/sparc64/sun4u.c | 2 +- include/hw/isa/isa.h | 2 +- 11 files changed, 13 insertions(+), 77 deletions(-)
hw/dma/i82374.c +1 -4
@@ -38,7 +38,6 @@ do { fprintf(stderr, "i82374 ERROR: " fmt , ## __VA_ARGS__); } while (0)typedef struct I82374State {uint8_t commands[8 ]; -qemu_irq out;PortioList port_list; }I82374State ;
@@ -101,7 +100,7 @@ static uint32_t i82374_read_descriptor(void *opaque, uint32_t nport)static void i82374_realize (I82374State *s,Error **errp) { -DMA_init (1 , &s->out); +DMA_init (1 );memset (s->commands,0 ,sizeof (s->commands)); }
@@ -145,8 +144,6 @@ static void i82374_isa_realize(DeviceState *dev, Error **errp) isa->iobase);i82374_realize (s, errp); - -qdev_init_gpio_out (dev, &s->out,1 ); }static Property i82374_properties[] = {
hw/dma/i8257.c +4 -9
@@ -59,7 +59,6 @@ static struct dma_cont {uint8_t flip_flop;int dshift;struct dma_regs regs[4 ]; -qemu_irq *cpu_request_exit;MemoryRegion channel_io;MemoryRegion cont_io; } dma_controllers[2 ];
@@ -521,13 +520,11 @@ static const MemoryRegionOps cont_io_ops = {/* dshift = 0: 8 bit DMA, 1 = 16 bit DMA */ static void dma_init2 (struct dma_cont *d,int base,int dshift, -int page_base,int pageh_base, -qemu_irq *cpu_request_exit) +int page_base,int pageh_base) {int i; d->dshift = dshift; - d->cpu_request_exit = cpu_request_exit;memory_region_init_io (&d->channel_io,NULL , &channel_io_ops, d,"dma-chan" ,8 << d->dshift);
@@ -591,12 +588,10 @@ static const VMStateDescription vmstate_dma = { } }; -void DMA_init (int high_page_enable,qemu_irq *cpu_request_exit) +void DMA_init (int high_page_enable) { -dma_init2 (&dma_controllers[0 ],0x00 ,0 ,0x80 , - high_page_enable ?0x480 :-1 , cpu_request_exit); -dma_init2 (&dma_controllers[1 ],0xc0 ,1 ,0x88 , - high_page_enable ?0x488 :-1 , cpu_request_exit); +dma_init2 (&dma_controllers[0 ],0x00 ,0 ,0x80 , high_page_enable ?0x480 :-1 ); +dma_init2 (&dma_controllers[1 ],0xc0 ,1 ,0x88 , high_page_enable ?0x488 :-1 );vmstate_register (NULL ,0 , &vmstate_dma, &dma_controllers[0 ]);vmstate_register (NULL ,1 , &vmstate_dma, &dma_controllers[1 ]);
hw/i386/pc.c +1 -12
@@ -1431,15 +1431,6 @@ DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)return dev; } -static void cpu_request_exit (void *opaque,int irq,int level) -{ -CPUState *cpu = current_cpu; - -if (cpu && level) { -cpu_exit (cpu); - } -} -static const MemoryRegionOps ioport80_io_ops = { .write = ioport80_write, .read = ioport80_read,
@@ -1474,7 +1465,6 @@ void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,qemu_irq rtc_irq =NULL ;qemu_irq *a20_line;ISADevice *i8042, *port92, *vmmouse, *pit =NULL ; -qemu_irq *cpu_exit_irq;MemoryRegion *ioport80_io =g_new (MemoryRegion,1 );MemoryRegion *ioportF0_io =g_new (MemoryRegion,1 );
@@ -1551,8 +1541,7 @@ void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi, port92 =isa_create_simple (isa_bus,"port92" );port92_init (port92, &a20_line[1 ]); - cpu_exit_irq =qemu_allocate_irqs (cpu_request_exit,NULL ,1 ); -DMA_init (0 , cpu_exit_irq); +DMA_init (0 );for (i =0 ; i <MAX_FD ; i++) { fd[i] =drive_get (IF_FLOPPY ,0 , i);
hw/isa/i82378.c +1 -2
@@ -100,7 +100,6 @@ static void i82378_realize(PCIDevice *pci, Error **errp)/* 2 82C37 (dma) */ isa =isa_create_simple (isabus,"i82374" ); -qdev_connect_gpio_out (DEVICE (isa),0 , s->out[1 ]);/* timer */ isa_create_simple (isabus,"mc146818rtc" );
@@ -111,7 +110,7 @@ static void i82378_init(Object *obj)DeviceState *dev =DEVICE (obj);I82378State *s =I82378 (obj); -qdev_init_gpio_out (dev, s->out,2 ); +qdev_init_gpio_out (dev, s->out,1 );qdev_init_gpio_in (dev, i82378_request_pic_irq,16 ); }
hw/mips/mips_fulong2e.c +1 -12
@@ -251,15 +251,6 @@ static void network_init (PCIBus *pci_bus) } } -static void cpu_request_exit (void *opaque,int irq,int level) -{ -CPUState *cpu = current_cpu; - -if (cpu && level) { -cpu_exit (cpu); - } -} -static void mips_fulong2e_init (MachineState *machine) {ram_addr_t ram_size = machine->ram_size;
@@ -274,7 +265,6 @@ static void mips_fulong2e_init(MachineState *machine)long bios_size;int64_t kernel_entry;qemu_irq *i8259; -qemu_irq *cpu_exit_irq;PCIBus *pci_bus;ISABus *isa_bus;I2CBus *smbus;
@@ -375,8 +365,7 @@ static void mips_fulong2e_init(MachineState *machine)/* init other devices */ pit =pit_init (isa_bus,0x40 ,0 ,NULL ); - cpu_exit_irq =qemu_allocate_irqs (cpu_request_exit,NULL ,1 ); -DMA_init (0 , cpu_exit_irq); +DMA_init (0 );/* Super I/O */ isa_create_simple (isa_bus,"i8042" );
hw/mips/mips_jazz.c +1 -12
@@ -104,15 +104,6 @@ static const MemoryRegionOps dma_dummy_ops = {#define MAGNUM_BIOS_SIZE_MAX 0x7e000#define MAGNUM_BIOS_SIZE (BIOS_SIZE < MAGNUM_BIOS_SIZE_MAX ? BIOS_SIZE : MAGNUM_BIOS_SIZE_MAX) -static void cpu_request_exit (void *opaque,int irq,int level) -{ -CPUState *cpu = current_cpu; - -if (cpu && level) { -cpu_exit (cpu); - } -} -static CPUUnassignedAccess real_do_unassigned_access; static void mips_jazz_do_unassigned_access(CPUState *cpu, hwaddr addr, bool is_write, bool is_exec,
@@ -150,7 +141,6 @@ static void mips_jazz_init(MachineState *machine,ISADevice *pit;DriveInfo *fds[MAX_FD ];qemu_irq esp_reset, dma_enable; -qemu_irq *cpu_exit_irq;MemoryRegion *ram =g_new (MemoryRegion,1 );MemoryRegion *bios =g_new (MemoryRegion,1 );MemoryRegion *bios2 =g_new (MemoryRegion,1 );
@@ -234,8 +224,7 @@ static void mips_jazz_init(MachineState *machine,/* ISA devices */ i8259 =i8259_init (isa_bus, env->irq[4 ]);isa_bus_irqs (isa_bus, i8259); - cpu_exit_irq =qemu_allocate_irqs (cpu_request_exit,NULL ,1 ); -DMA_init (0 , cpu_exit_irq); +DMA_init (0 ); pit =pit_init (isa_bus,0x40 ,0 ,NULL );pcspk_init (isa_bus, pit);
hw/mips/mips_malta.c +1 -12
@@ -905,15 +905,6 @@ static void main_cpu_reset(void *opaque) } } -static void cpu_request_exit (void *opaque,int irq,int level) -{ -CPUState *cpu = current_cpu; - -if (cpu && level) { -cpu_exit (cpu); - } -} -static void mips_malta_init (MachineState *machine) {
@@ -939,7 +930,6 @@ void mips_malta_init(MachineState *machine)MIPSCPU *cpu;CPUMIPSState *env;qemu_irq *isa_irq; -qemu_irq *cpu_exit_irq;int piix4_devfn;I2CBus *smbus;int i;
@@ -1175,8 +1165,7 @@ void mips_malta_init(MachineState *machine)smbus_eeprom_init (smbus,8 , smbus_eeprom_buf, smbus_eeprom_size);g_free (smbus_eeprom_buf); pit =pit_init (isa_bus,0x40 ,0 ,NULL ); - cpu_exit_irq =qemu_allocate_irqs (cpu_request_exit,NULL ,1 ); -DMA_init (0 , cpu_exit_irq); +DMA_init (0 );/* Super I/O */ isa_create_simple (isa_bus,"i8042" );
hw/ppc/prep.c +0 -11
@@ -336,15 +336,6 @@ static uint32_t PREP_io_800_readb (void *opaque, uint32_t addr)#define NVRAM_SIZE 0x2000 -static void cpu_request_exit (void *opaque,int irq,int level) -{ -CPUState *cpu = current_cpu; - -if (cpu && level) { -cpu_exit (cpu); - } -} -static void ppc_prep_reset (void *opaque) {PowerPCCPU *cpu = opaque;
@@ -626,8 +617,6 @@ static void ppc_prep_init(MachineState *machine) cpu =POWERPC_CPU (first_cpu);qdev_connect_gpio_out (&pci->qdev,0 , cpu->env.irq_inputs[PPC6xx_INPUT_INT]); -qdev_connect_gpio_out (&pci->qdev,1 , -qemu_allocate_irq (cpu_request_exit,NULL ,0 ));sysbus_connect_irq (&pcihost->busdev,0 ,qdev_get_gpio_in (&pci->qdev,9 ));sysbus_connect_irq (&pcihost->busdev,1 ,qdev_get_gpio_in (&pci->qdev,11 ));sysbus_connect_irq (&pcihost->busdev,2 ,qdev_get_gpio_in (&pci->qdev,9 ));
hw/sparc/sun4m.c +1 -1
@@ -111,7 +111,7 @@ void DMA_hold_DREQ (int nchan) {}void DMA_release_DREQ (int nchan) {}void DMA_schedule (void ) {} -void DMA_init (int high_page_enable,qemu_irq *cpu_request_exit) +void DMA_init (int high_page_enable) { }
hw/sparc64/sun4u.c +1 -1
@@ -114,7 +114,7 @@ void DMA_hold_DREQ (int nchan) {}void DMA_release_DREQ (int nchan) {}void DMA_schedule (void ) {} -void DMA_init (int high_page_enable,qemu_irq *cpu_request_exit) +void DMA_init (int high_page_enable) { }
include/hw/isa/isa.h +1 -1
@@ -113,7 +113,7 @@ int DMA_write_memory (int nchan, void *buf, int pos, int size);void DMA_hold_DREQ (int nchan);void DMA_release_DREQ (int nchan);void DMA_schedule (void ); -void DMA_init (int high_page_enable,qemu_irq *cpu_request_exit); +void DMA_init (int high_page_enable);void DMA_register_channel (int nchan,DMA_transfer_handler transfer_handler,void *opaque);
Signature · 1 line
2.4.3
PATCH 1/3 i8257: rewrite DMA_schedule to avoid hooking into the CPU loop
In reply to Paolo BonziniThe i8257 DMA controller uses an idle bottom half, which by default does not cause the main loop to exit. Therefore, the DMA_schedule function is there to ensure that the CPU relinquishes the iothread mutex to the iothread.
However, this is not enough since the iothread will call aio_compute_timeout() and go to sleep again. In the iothread world, forcing execution of the idle bottom half is much simpler, and only requires a call to qemu_notify_event(). Do it, removing the need for the "cpu_request_exit" pseudo-irq. The next patch will remove it.
--- hw/block/fdc.c | 2 +- hw/dma/i8257.c | 18 ++++++++++++------ hw/sparc/sun4m.c | 2 +- hw/sparc64/sun4u.c | 2 +- include/hw/isa/isa.h | 2 +- 5 files changed, 16 insertions(+), 10 deletions(-)
hw/block/fdc.c +1 -1
@@ -1417,7 +1417,7 @@ static void fdctrl_start_transfer(FDCtrl *fdctrl, int direction)* recall us... */ DMA_hold_DREQ (fdctrl->dma_chann); -DMA_schedule (fdctrl->dma_chann); +DMA_schedule (); }else {/* Start transfer */ fdctrl_transfer_handler(fdctrl, fdctrl->dma_chann, 0,
hw/dma/i8257.c +12 -6
@@ -358,6 +358,7 @@ static void channel_run (int ncont, int ichan) }static QEMUBH *dma_bh; +static bool dma_bh_scheduled;static void DMA_run (void ) {
@@ -390,12 +391,15 @@ static void DMA_run (void) running =0 ; out: -if (rearm) +if (rearm) {qemu_bh_schedule_idle (dma_bh); + dma_bh_scheduled = true; + } }static void DMA_run_bh (void *unused) { + dma_bh_scheduled = false;DMA_run (); }
@@ -458,12 +462,14 @@ int DMA_write_memory (int nchan, void *buf, int pos, int len)return len; } -/* request the emulator to transfer a new DMA memory block ASAP */ -void DMA_schedule (int nchan) +/* request the emulator to transfer a new DMA memory block ASAP (even +* if the idle bottom half would not have exited the iothread yet). +*/ +void DMA_schedule (void ) { -struct dma_cont *d = &dma_controllers[nchan >3 ]; - -qemu_irq_pulse (*d->cpu_request_exit); +if (dma_bh_scheduled) { +qemu_notify_event (); + } }static void dma_reset (void *opaque)
hw/sparc/sun4m.c +1 -1
@@ -109,7 +109,7 @@ int DMA_write_memory (int nchan, void *buf, int pos, int size) }void DMA_hold_DREQ (int nchan) {}void DMA_release_DREQ (int nchan) {} -void DMA_schedule (int nchan) {} +void DMA_schedule (void ) {}void DMA_init (int high_page_enable,qemu_irq *cpu_request_exit) {
hw/sparc64/sun4u.c +1 -1
@@ -112,7 +112,7 @@ int DMA_write_memory (int nchan, void *buf, int pos, int size) }void DMA_hold_DREQ (int nchan) {}void DMA_release_DREQ (int nchan) {} -void DMA_schedule (int nchan) {} +void DMA_schedule (void ) {}void DMA_init (int high_page_enable,qemu_irq *cpu_request_exit) {
include/hw/isa/isa.h +1 -1
@@ -112,7 +112,7 @@ int DMA_read_memory (int nchan, void *buf, int pos, int size);int DMA_write_memory (int nchan,void *buf,int pos,int size);void DMA_hold_DREQ (int nchan);void DMA_release_DREQ (int nchan); -void DMA_schedule (int nchan); +void DMA_schedule (void );void DMA_init (int high_page_enable,qemu_irq *cpu_request_exit); void DMA_register_channel (int nchan, DMA_transfer_handler transfer_handler,
Signature · 1 line
2.4.3
PATCH 3/3 tcg: signal-free qemu_cpu_kick
In reply to Paolo BonziniSignals are slow and do not exist on Win32. It is not much more complicated to use memory barriers (which we already need anyway on Windows!) and set the existing flags in the iothread.
qemu_cpu_kick_thread is not used anymore on TCG, since the TCG thread is never outside usermode while the CPU is running (not halted).
--- cpu-exec.c | 18 ++++------- cpus.c | 91 +++++++++++++++-------------------------------------- gdbstub.c | 2 +- hw/ppc/spapr_rtas.c | 2 +- qom/cpu.c | 2 ++ 5 files changed, 35 insertions(+), 80 deletions(-)
cpu-exec.c +6 -12
@@ -367,19 +367,10 @@ int cpu_exec(CPUState *cpu) cpu->halted =0 ; } - current_cpu = cpu; - -/* As long as current_cpu is null, up to the assignment just above, -* requests by other threads to exit the execution loop are expected to -* be issued using the exit_request global. We must make sure that our -* evaluation of the global value is performed past the current_cpu -* value transition point, which requires a memory barrier as well as -* an instruction scheduling constraint on modern architectures. */ -smp_mb (); - +atomic_mb_set (¤t_cpu, cpu);rcu_read_lock (); -if (unlikely (exit_request)) { +if (unlikely (atomic_mb_read (&exit_request))) { cpu->exit_request =1 ; }
@@ -519,8 +510,11 @@ int cpu_exec(CPUState *cpu)* loop. Whatever requested the exit will also * have set something else (eg exit_request or * interrupt_request) which we will handle -* next time around the loop. +* next time around the loop. But we need to +* ensure tcg_exit_req is read before exit_request +* or interrupt_request. */ +smp_rmb (); next_tb =0 ;break ;case TB_EXIT_ICOUNT_EXPIRED :
cpus.c +24 -65
149 lines not on this page · raw message
gdbstub.c +1 -1
@@ -1362,7 +1362,7 @@ void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)is still in the running state, which can cause packets to be dropped and state transition 'T' packets to be sent while the syscall is still being processed. */ -cpu_exit (s->c_cpu); +qemu_cpu_kick (s->c_cpu);#endif }
hw/ppc/spapr_rtas.c +1 -1
@@ -214,7 +214,7 @@ static void rtas_stop_self(PowerPCCPU *cpu, sPAPRMachineState *spapr,CPUPPCState *env = &cpu->env; cs->halted =1 ; -cpu_exit (cs); +qemu_cpu_kick (cs);/* * While stopping a CPU, the guest calls H_CPPR which * effectively disables interrupts on XICS level.
qom/cpu.c +2 -0
@@ -114,6 +114,8 @@ void cpu_reset_interrupt(CPUState *cpu, int mask)void cpu_exit (CPUState *cpu) { cpu->exit_request =1 ; +/* Ensure cpu_exec will see the exit request after TCG has exited. */ +smp_wmb (); cpu->tcg_exit_req =1 ; }
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH 3/3] tcg: signal-free qemu_cpu_kick
Quoted 1 line · Paolo Bonzini
+
atomic_mb_set(¤t_cpu, cpu);
...
Quoted 1 line
+ cpu_exit(atomic_rcu_read(¤t_cpu));
Mixing java and rcu style sync to the same data structure?
cpu-exec.c int cpu_exec(CPUState *cpu) · line 518
Quoted 5 lines
cpu-exec.c +3
-0+* ensure tcg_exit_req is read before exit_request +* or interrupt_request. */ +smp_rmb (); next_tb =0 ;
This I don't understand, since we've just read exit_request above, and you're putting the barrier here?
Quoted 12 lines
cpus.c +12
-0+/* Ensure whatever caused the exit has reached the CPU threads before +* writing exit_request. +*/ +smp_wmb (); + exit_request =1 ; +/* Ignore the CPU argument since all CPUs run in the same thread; +* preempt the currently running one. The memory barriers ensures +* that other CPUs will see the request if the current CPU is +* preempted. +*/ +smp_wmb (); +cpu_exit (atomic_rcu_read (¤t_cpu));
...
cpus.c static void tcg_exec_all(void) · line 1403
Quoted 3 lines
cpus.c +2
-0+/* Pairs with smp_wmb in qemu_cpu_kick. */ +atomic_mb_set (&exit_request,0 ); }
Bare barriers and java style sync to the same data structure?
qom/cpu.c void cpu_reset_interrupt(CPUState *cpu, int mask) · line 120
Quoted 5 lines
cpu->exit_request = 1;qom/cpu.c +2
-0+/* Ensure cpu_exec will see the exit request after TCG has exited. */ +smp_wmb (); cpu->tcg_exit_req =1 ; }
Likewise.
I find this mixing highly confusing. I see no way to prove that it's going to be right for non-x86.
r~
Re: [Qemu-devel] [PATCH 3/3] tcg: signal-free qemu_cpu_kick
Quoted 6 lines · Richard Henderson
Quoted 1 line · Paolo Bonzini
+
atomic_mb_set(¤t_cpu, cpu);...
Quoted 1 line
+ cpu_exit(atomic_rcu_read(¤t_cpu));
Mixing java and rcu style sync to the same data structure?
Well, I usually read rcu_read as CONSUME, rcu_set as RELEASE, mb_read as either ACQUIRE or "SEQ_CST without IRIW" and mb_set as "SEQ_CST without IRIW". But you're right that the patch is unreadable.
Quoted 8 lines
cpu-exec.c int cpu_exec(CPUState *cpu) · line 518
Quoted 5 lines
cpu-exec.c +3
-0+* ensure tcg_exit_req is read before exit_request +* or interrupt_request. */ +smp_rmb (); next_tb =0 ;This I don't understand, since we've just read
exit_requestabove, and you're putting the barrier here?
If we see cpu->exit_request == 1, we exit. In that case, cpu->tcg_exit_req doesn't matter.
Here we saw cpu->exit_request == 0 and then got TB_EXIT_REQUESTED. Because of TB_EXIT_REQUESTED we know cpu->tcg_exit_req is 1; the smp_rmb() ensures that cpu->exit_request will be read as 1 on the next iteration.
Paolo
Quoted 36 lines
Quoted 12 lines
cpus.c +12
-0+/* Ensure whatever caused the exit has reached the CPU threads before +* writing exit_request. +*/ +smp_wmb (); + exit_request =1 ; +/* Ignore the CPU argument since all CPUs run in the same thread; +* preempt the currently running one. The memory barriers ensures +* that other CPUs will see the request if the current CPU is +* preempted. +*/ +smp_wmb (); +cpu_exit (atomic_rcu_read (¤t_cpu));...
cpus.c static void tcg_exec_all(void) · line 1403
Quoted 3 lines
cpus.c +2
-0+/* Pairs with smp_wmb in qemu_cpu_kick. */ +atomic_mb_set (&exit_request,0 ); }Bare barriers and java style sync to the same data structure?
qom/cpu.c void cpu_reset_interrupt(CPUState *cpu, int mask) · line 120
Quoted 5 lines
cpu->exit_request = 1;qom/cpu.c +2
-0+/* Ensure cpu_exec will see the exit request after TCG has exited. */ +smp_wmb (); cpu->tcg_exit_req =1 ; }Likewise.
I find this mixing highly confusing. I see no way to prove that it's going to be right for non-x86.
r~