Skip to contentSkip to conversation

Multiple instances of Qemu on Windows multicore

Qemu-devel Fabien Chouteau 5 participants 16 messages Nov 2, 2011 – Nov 4, 2011

lore
Share & tools
lore
#0 lore raw

Hello fellow Qemu aficionados,

On Windows, Qemu sets the affinity mask in order to run all thread on CPU0, with this comment in the code (os-win32.c:182):

/* Note: cpu_interrupt() is currently not SMP safe, so we force
QEMU to run on a single CPU */

This was added by Fabrice Bellard in 2006 (git show a8e5ac33d).

I can't find/understand any reason for this CPU affinity restriction. So I'm asking to the experts:

1 - Is this comment still applicable?
2 - If yes, what is the problem with cpu_interrupt on SMP? 3 - Why is this different on Linux?

This is a real drawback, especially to run automated tests.

Thanks in advance,

Signature · 1 line
Fabien Chouteau
#1 lore raw
Quoted 11 lines · Fabien Chouteau

Hello fellow Qemu aficionados,

On Windows, Qemu sets the affinity mask in order to run all thread on CPU0, with this comment in the code (os-win32.c:182):

/* Note: cpu_interrupt() is currently not SMP safe, so we force
QEMU to run on a single CPU */

This was added by Fabrice Bellard in 2006 (git show a8e5ac33d).

I can't find/understand any reason for this CPU affinity restriction.

Have you tried looking for a justification in the mailing lists? Also, I suppose you have tested without the affinity mask and it works?

Offhand I cannot think of why that would be needed.

Paolo

#2 lore raw
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Fabien Chouteau

Hello fellow Qemu aficionados,

On Windows, Qemu sets the affinity mask in order to run all thread on CPU0, with this comment in the code (os-win32.c:182):

Quoted 8 lines
Quoted 6 lines

/* Note: cpu_interrupt() is currently not SMP safe, so we force
QEMU to run on a single CPU */

This was added by Fabrice Bellard in 2006 (git show a8e5ac33d).

I can't find/understand any reason for this CPU affinity restriction.

Have you tried looking for a justification in the mailing lists?

Yes, and I found few mails from Fabrice Bellard and Konrad Schwarz in the archives:

http://thread.gmane.org/gmane.comp.emulators.qemu/13804

and

http://thread.gmane.org/gmane.comp.emulators.qemu/13831/focus=13805

But it didn't provide more information about the problem.

Quoted 3 lines

Also, I suppose you have tested without the affinity mask and it works?

Yes I did, it works pretty well. I had 1 unexpected failure among ~6000 tests. But I would like to have a substantial explanation.

Quoted 3 lines

Offhand I cannot think of why that would be needed.

OK, thanks for your help.

Signature · 1 line
Fabien Chouteau
#3 lore raw
Quoted 31 lines · Fabien Chouteau
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Fabien Chouteau

Hello fellow Qemu aficionados,

On Windows, Qemu sets the affinity mask in order to run all thread on CPU0, with this comment in the code (os-win32.c:182):

Quoted 8 lines
Quoted 6 lines

/* Note: cpu_interrupt() is currently not SMP safe, so we force
QEMU to run on a single CPU */

This was added by Fabrice Bellard in 2006 (git show a8e5ac33d).

I can't find/understand any reason for this CPU affinity restriction.

Have you tried looking for a justification in the mailing lists?

Yes, and I found few mails from Fabrice Bellard and Konrad Schwarz in the archives:

http://thread.gmane.org/gmane.comp.emulators.qemu/13804

and

http://thread.gmane.org/gmane.comp.emulators.qemu/13831/focus=13805

But it didn't provide more information about the problem.

Quoted 3 lines

Also, I suppose you have tested without the affinity mask and it works?

Quoted 8 lines

Yes I did, it works pretty well. I had 1 unexpected failure among ~6000 tests. But I would like to have a substantial explanation.

Quoted 3 lines

Offhand I cannot think of why that would be needed.

OK, thanks for your help.

(mm)Timers have a possibility of running on a thread of their own which might be schedulled on the CPU different from the thread that runs emulated code, unchaining TBs and can (and will) fail in this case.

Signature · 1 line
mailto:[email protected]
#4 lore raw
Quoted 3 lines · malc

(mm)Timers have a possibility of running on a thread of their own which might be schedulled on the CPU different from the thread that runs emulated code, unchaining TBs and can (and will) fail in this case.

This should not be a problem with dynticks+iothread (i.e. it should work or not work equally). We now run just this basically when an alarm fires:

        t->expired = t->pending = 1;
        qemu_notify_event();

The rest is always done in the iothread. The iothread will then suspend/resume the VCPU thread around the unchaining, so what matters is (in Unix parlance) signal-safety of the unchaining, not thread-safety.

Paolo

#5 lore raw
Quoted 8 lines · Paolo Bonzini
Quoted 3 lines · malc

(mm)Timers have a possibility of running on a thread of their own which might be schedulled on the CPU different from the thread that runs emulated code, unchaining TBs and can (and will) fail in this case.

This should not be a problem with dynticks+iothread (i.e. it should work or not work equally). We now run just this basically when an alarm fires:

I was explaining rationale behind pinning stuff at the time it was done.

Quoted 9 lines
        t->expired = t->pending = 1;
        qemu_notify_event();

The rest is always done in the iothread. The iothread will then suspend/resume the VCPU thread around the unchaining, so what matters is (in Unix parlance) signal-safety of the unchaining, not thread-safety.

Paolo

Signature · 1 line
mailto:[email protected]
#6 lore raw
In reply to Paolo Bonzini
Quoted 3 lines · Paolo Bonzini

The rest is always done in the iothread.  The iothread will then suspend/resume the VCPU thread around the unchaining, so what matters is (in Unix parlance) signal-safety of the unchaining, not thread-safety.

The unchaining is neither signal-safe nor thread-safe...

-- PMM

#7 lore raw
Quoted 6 lines · Peter Maydell
Quoted 3 lines · Paolo Bonzini

The rest is always done in the iothread. The iothread will then suspend/resume the VCPU thread around the unchaining, so what matters is (in Unix parlance) signal-safety of the unchaining, not thread-safety.

The unchaining is neither signal-safe nor thread-safe...

Yeah, but there's nothing Windows-specific in that.

(Also, the unchaining is safer, or even completely safe in system mode than it is with pthreads).

Paolo

#8 lore raw
Quoted 2 lines · Paolo Bonzini

(Also, the unchaining is safer, or even completely safe in system mode than it is with pthreads).

I don't think it's completely safe, you're just a bit less likely to get bitten than if you're trying to run a linux-user-mode multithreaded guest binary.

-- PMM

#10 lore raw
In reply to Paolo Bonzini
Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Peter Maydell
Quoted 3 lines · Paolo Bonzini

The rest is always done in the iothread. The iothread will then suspend/resume the VCPU thread around the unchaining, so what matters is (in Unix parlance) signal-safety of the unchaining, not thread-safety.

The unchaining is neither signal-safe nor thread-safe...

Yeah, but there's nothing Windows-specific in that.

That's very important, I don't see why it is different between Linux and Windows here. Also, why running all the threads on the same CPU would make the code thread-safe?

Signature · 1 line
Fabien Chouteau
#9 lore raw
In reply to Peter Maydell
Quoted 7 lines · Peter Maydell
Quoted 2 lines · Paolo Bonzini

(Also, the unchaining is safer, or even completely safe in system mode than it is with pthreads).

I don't think it's completely safe, you're just a bit less likely to get bitten than if you're trying to run a linux-user-mode multithreaded guest binary.

Do you think it's possible to make the code safe?

Signature · 1 line
Fabien Chouteau
#11 lore raw
In reply to Fabien Chouteau
Quoted 5 lines · Fabien Chouteau
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

The unchaining is neither signal-safe nor thread-safe...

Yeah, but there's nothing Windows-specific in that.

That's very important, I don't see why it is different between Linux and Windows here.

Yep, perhaps for timers it was the case a while ago, but with iothread+dynticks it should not be a problem anymore. For unchaining, Linux and Windows should have never been different.

Quoted 2 lines

Also, why running all the threads on the same CPU would make the code thread-safe?

It would ensure that two mutators wouldn't run concurrently. In some sense, signal-safe code could then be considered thread-safe too.

Paolo

#12 lore raw
Quoted 7 lines · Paolo Bonzini
Quoted 3 lines

Also, why running all the threads on the same CPU would make the code thread-safe?

It would ensure that two mutators wouldn't run concurrently. In some sense, signal-safe code could then be considered thread-safe too.

How so? The scheduler can switch between the two threads on every instruction.

Signature · 1 line
error compiling committee.c: too many arguments to function
#13 lore raw
Quoted 5 lines · Avi Kivity
Quoted 2 lines

It would ensure that two mutators wouldn't run concurrently. In some sense, signal-safe code could then be considered thread-safe too.

How so? The scheduler can switch between the two threads on every instruction.

In general signal-safe is more stringent than thread-safe, but with two exceptions: memory barriers and locked memory access. On x86 (implied by Windows...) you might also assume that the compiler will generate arithmetic operations with a memory destination, which makes code like

    void cpu_interrupt(CPUState *env, int mask)
    {
        env->interrupt_request |= mask;          /* <--- this */
        cpu_unlink_tb(env);
    }

signal-safe in practice---and even "thread-safe" on non-SMP systems. It's a huge assumption though, and I don't think it should be assumed anymore. With iothread the architecture of the QEMU main loop is anyway completely different.

Paolo

#14 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 5 lines · Avi Kivity
Quoted 2 lines

It would ensure that two mutators wouldn't run concurrently. In some sense, signal-safe code could then be considered thread-safe too.

How so? The scheduler can switch between the two threads on every instruction.

In general signal-safe is more stringent than thread-safe, but with two exceptions: memory barriers and locked memory access. On x86 (implied by Windows...) you might also assume that the compiler will generate arithmetic operations with a memory destination, which makes code like

Quoted 7 lines
    void cpu_interrupt(CPUState *env, int mask)
    {
        env->interrupt_request |= mask;          /* <--- this */
        cpu_unlink_tb(env);
    }

signal-safe in practice---and even "thread-safe" on non-SMP systems. It's a huge assumption though, and I don't think it should be assumed anymore.

What can we do to improve that?

Quoted 3 lines

With iothread the architecture of the QEMU main loop is anyway completely different.

Are you saying that things are better or worst with iothread?

Signature · 1 line
Fabien Chouteau
#15 lore raw
Quoted 3 lines · Fabien Chouteau
Quoted 1 line

It's a huge assumption though, and I don't think it should be assumed anymore.

What can we do to improve that?

Remove the affinity change and see what breaks. :)

Quoted 3 lines
Quoted 1 line

With iothread the architecture of the QEMU main loop is anyway completely different.

Are you saying that things are better or worst with iothread?

I think better. Without iothread, cpu_interrupt was called by qemu_notify_event. With iothread it is not. Perhaps that was the original problem with timers?...

Paolo