Skip to contentSkip to conversation

vcpu_block() and do_poll() question

DISCUSSION Juergen Gross 4 participants 11 messages Apr 30, 2019 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

In xen/common/schedule.c there is a weird "#ifndef CONFIG_X86" in do_poll().

It was introduced way before anyone would think about ARM by commit ef4c6b079cc55e (I couldn't find any xen-devel mail related to that commit), so I guess it is related to IA64?

Question is: can we just drop it, or does ARM depend on it? And if ARM really needs it, is it the memory barrier only? And why wouldn't a similar barrier be needed in vcpu_block() then?

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#1 lore raw
Quoted 7 lines · Juergen Gross

In xen/common/schedule.c there is a weird "#ifndef CONFIG_X86" in do_poll().

It was introduced way before anyone would think about ARM by commit ef4c6b079cc55e (I couldn't find any xen-devel mail related to that commit), so I guess it is related to IA64?

I guess it must be that one.

I rolled back the tree at that commit, and went checking how set_bit() was implemented for IA64.

Interestingly, there's a comment above clear_bit(), which says:

* clear_bit() is atomic and may not be reordered. However, it does
* not contain a memory barrier, so if it is used for locking purposes,
* you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
* in order to ensure changes are visible on other processors.

which seems to confirm what we're thinking. Memory barriers are, however, not mentioned at all for set_bit().

It's also explicitly stated that test_and_set_bit() and test_and_clear_bit() do act as memory barriers.

The code appears to having been taken from Linux (it's even in xen/include/asm-ia64/linux/asm/bitops.h), so maybe some more investigation is possible, if deemed interesting.

Quoted 5 lines

Question is: can we just drop it, or does ARM depend on it? And if ARM
really needs it, is it the memory barrier only? And why wouldn't a similar barrier be needed in vcpu_block() then?

Well, the difference between vcpu_block() and do_poll() is that the former does just one set_bit(), while the latter does two, with an assignment in between.

So I guess what we're trying to deal with is the case of some CPU having seen any random combination of poll_evtchn and the bits of pause_flags and poll_mask, on an arch that permits enough reordering.

But, yes, if what you're saying is that, on an architectures that need a barrier after set_bit(), that's then necessary in both functions, that makes sense to me too.

Regards

Signature · 6 lines
Dario Faggioli, Ph.D
http://about.me/dario.faggioli
Virtualization Software Engineer
SUSE Labs, SUSE https://www.suse.com/
-------------------------------------------------------------------
<<This happens because _I_ choose it to happen!>> (Raistlin Majere)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
In reply to Juergen Gross
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 11:13, <[email protected]> wrote:

In xen/common/schedule.c there is a weird "#ifndef CONFIG_X86" in do_poll().

It was introduced way before anyone would think about ARM by commit ef4c6b079cc55e (I couldn't find any xen-devel mail related to that commit), so I guess it is related to IA64?

Quite certainly, yes (or PPC as an alternative, but I know nothing about their memory ordering model).

Quoted 3 lines

Question is: can we just drop it, or does ARM depend on it? And if ARM really needs it, is it the memory barrier only? And why wouldn't a similar barrier be needed in vcpu_block() then?

It's not the memory barrier, but the subsequent checks. The barrier is there only to make sure the checks don't happen before the earlier writes. And as the comment says - it's the combination of the 3 writes that may get mis-ordered and hence mis-interpreted by other (reading) parties. vcpu_block(), otoh, sets the VPF_blocked bit only, so there's no potential ordering issue there.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 15 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 11:13, <[email protected]> wrote:

In xen/common/schedule.c there is a weird "#ifndef CONFIG_X86" in do_poll().

It was introduced way before anyone would think about ARM by commit ef4c6b079cc55e (I couldn't find any xen-devel mail related to that commit), so I guess it is related to IA64?

Quite certainly, yes (or PPC as an alternative, but I know nothing about their memory ordering model).

Quoted 3 lines

Question is: can we just drop it, or does ARM depend on it? And if ARM really needs it, is it the memory barrier only? And why wouldn't a similar barrier be needed in vcpu_block() then?

Quoted 6 lines

It's not the memory barrier, but the subsequent checks. The barrier is there only to make sure the checks don't happen before the earlier writes. And as the comment says - it's the combination of the 3 writes that may get mis-ordered and hence mis-interpreted by other (reading) parties. vcpu_block(), otoh, sets the VPF_blocked bit only, so there's no potential ordering issue there.

Are you suggesting the comment in vcpu_block is not correct?

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 12:31, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 11:13, <[email protected]> wrote:

In xen/common/schedule.c there is a weird "#ifndef CONFIG_X86" in do_poll().

It was introduced way before anyone would think about ARM by commit ef4c6b079cc55e (I couldn't find any xen-devel mail related to that commit), so I guess it is related to IA64?

Quite certainly, yes (or PPC as an alternative, but I know nothing about their memory ordering model).

Quoted 3 lines

Question is: can we just drop it, or does ARM depend on it? And if ARM really needs it, is it the memory barrier only? And why wouldn't a similar barrier be needed in vcpu_block() then?

Quoted 8 lines
Quoted 6 lines

It's not the memory barrier, but the subsequent checks. The barrier is there only to make sure the checks don't happen before the earlier writes. And as the comment says - it's the combination of the 3 writes that may get mis-ordered and hence mis-interpreted by other (reading) parties. vcpu_block(), otoh, sets the VPF_blocked bit only, so there's no potential ordering issue there.

Are you suggesting the comment in vcpu_block is not correct?

No. But I'm not talking about the local_events_need_delivery() checks (which exist in both functions), but the ones inside the #ifdef in do_poll() (which have no equivalent in vcpu_block()). That's not to say that there may not need to be a barrier ahead of local_events_need_delivery() as well - I've simply not thought through that further case (yet).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 26 lines · Jan Beulich
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 12:31, <[email protected]> wrote:
Quoted 21 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 11:13, <[email protected]> wrote:

In xen/common/schedule.c there is a weird "#ifndef CONFIG_X86" in do_poll().

It was introduced way before anyone would think about ARM by commit ef4c6b079cc55e (I couldn't find any xen-devel mail related to that commit), so I guess it is related to IA64?

Quite certainly, yes (or PPC as an alternative, but I know nothing about their memory ordering model).

Quoted 3 lines

Question is: can we just drop it, or does ARM depend on it? And if ARM really needs it, is it the memory barrier only? And why wouldn't a similar barrier be needed in vcpu_block() then?

It's not the memory barrier, but the subsequent checks. The barrier is there only to make sure the checks don't happen before the earlier writes. And as the comment says - it's the combination of the 3 writes that may get mis-ordered and hence mis-interpreted by other (reading) parties. vcpu_block(), otoh, sets the VPF_blocked bit only, so there's no potential ordering issue there.

Quoted 8 lines
Quoted 1 line

Are you suggesting the comment in vcpu_block is not correct?

No. But I'm not talking about the local_events_need_delivery() checks (which exist in both functions), but the ones inside the #ifdef in do_poll() (which have no equivalent in vcpu_block()). That's not to say that there may not need to be a barrier ahead of local_events_need_delivery() as well - I've simply not thought through that further case (yet).

An alternative would be memory barriers between the writes on ARM, right? Or a strong ordered set_bit() variant (we had that discussion recently related to a barrier in ARM-specific __cpu_disable()).

Then we could drop this #ifndef section.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 12:44, <[email protected]> wrote:

An alternative would be memory barriers between the writes on ARM, right? Or a strong ordered set_bit() variant (we had that discussion recently related to a barrier in ARM-specific __cpu_disable()).

Yes.

Quoted 1 line

Then we could drop this #ifndef section.

Not sure about this - I'm actually unconvinced the latter part of what's inside the #ifdef isn't actually needed on x86 as well. Just consider the case of vcpu_unblock() making it to the vcpu_wake() invocation on another CPU while we're between any two of the three writes here. (I know I've been feeling uneasy about this before, but I guess I must have come to the conclusion that it's _probably_ okay.)

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw

Hi,

Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 12:44, <[email protected]> wrote:

An alternative would be memory barriers between the writes on ARM, right? Or a strong ordered set_bit() variant (we had that discussion recently related to a barrier in ARM-specific __cpu_disable()).

I am not entirely a big fan of a strong-order variant. It will potentially add more memory barriers than necessary in this context.

Quoted 2 lines

Yes.

What would be the advantage of 2-3 memory barriers over a memory barrier + check?

Quoted 10 lines
Quoted 2 lines

Then we could drop this #ifndef section.

Not sure about this - I'm actually unconvinced the latter part of what's inside the #ifdef isn't actually needed on x86 as well. Just consider the case of vcpu_unblock() making it to the vcpu_wake() invocation on another CPU while we're between any two of the three writes here. (I know I've been feeling uneasy about this before, but I guess I must have come to the conclusion that it's _probably_ okay.)

That's not going to be covered by the check on non-x86 platform. Indeed, vcpu_wake() is not updating any of the fields. So, from my understanding, the wake-up request will just be ignored.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#8 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 15:16, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 12:44, <[email protected]> wrote:

An alternative would be memory barriers between the writes on ARM, right? Or a strong ordered set_bit() variant (we had that discussion recently related to a barrier in ARM-specific __cpu_disable()).

Quoted 8 lines

I am not entirely a big fan of a strong-order variant. It will potentially add more memory barriers than necessary in this context.

Quoted 2 lines

Yes.

What would be the advantage of 2-3 memory barriers over a memory barrier + check?

Dunno - I've merely confirmed that this looks to be an alternative correct solution.

Quoted 9 lines
Quoted 9 lines
Quoted 1 line

Then we could drop this #ifndef section.

Not sure about this - I'm actually unconvinced the latter part of what's inside the #ifdef isn't actually needed on x86 as well. Just consider the case of vcpu_unblock() making it to the vcpu_wake() invocation on another CPU while we're between any two of the three writes here. (I know I've been feeling uneasy about this before, but I guess I must have come to the conclusion that it's _probably_ okay.)

Quoted 3 lines

That's not going to be covered by the check on non-x86 platform. Indeed, vcpu_wake() is not updating any of the fields. So, from my understanding, the wake-up request will just be ignored.

Perhaps a misunderstanding (or I'm confused now): I mentioned vcpu_wake() only to delimit how much of vcpu_unblock() needs completing for the possible problem to surface that I'm suspecting.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw
Quoted 28 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 15:16, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 12:44, <[email protected]> wrote:

An alternative would be memory barriers between the writes on ARM, right? Or a strong ordered set_bit() variant (we had that discussion recently related to a barrier in ARM-specific __cpu_disable()).

Quoted 8 lines

I am not entirely a big fan of a strong-order variant. It will potentially add more memory barriers than necessary in this context.

Quoted 2 lines

Yes.

What would be the advantage of 2-3 memory barriers over a memory barrier + check?

Dunno - I've merely confirmed that this looks to be an alternative correct solution.

Quoted 9 lines
Quoted 9 lines
Quoted 1 line

Then we could drop this #ifndef section.

Not sure about this - I'm actually unconvinced the latter part of what's inside the #ifdef isn't actually needed on x86 as well. Just consider the case of vcpu_unblock() making it to the vcpu_wake() invocation on another CPU while we're between any two of the three writes here. (I know I've been feeling uneasy about this before, but I guess I must have come to the conclusion that it's _probably_ okay.)

Quoted 7 lines
Quoted 3 lines

That's not going to be covered by the check on non-x86 platform. Indeed, vcpu_wake() is not updating any of the fields. So, from my understanding, the wake-up request will just be ignored.

Perhaps a misunderstanding (or I'm confused now): I mentioned vcpu_wake() only to delimit how much of vcpu_unblock() needs completing for the possible problem to surface that I'm suspecting.

I am not sure what problem you refer. Is it the wake-up request will get ignored?

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#10 lore raw
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 15:35, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 15:16, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 30.04.19 at 12:44, <[email protected]> wrote:

Then we could drop this #ifndef section.

Not sure about this - I'm actually unconvinced the latter part of what's inside the #ifdef isn't actually needed on x86 as well. Just consider the case of vcpu_unblock() making it to the vcpu_wake() invocation on another CPU while we're between any two of the three writes here. (I know I've been feeling uneasy about this before, but I guess I must have come to the conclusion that it's _probably_ okay.)

That's not going to be covered by the check on non-x86 platform. Indeed, vcpu_wake() is not updating any of the fields. So, from my understanding, the wake-up request will just be ignored.

Quoted 6 lines
Quoted 3 lines

Perhaps a misunderstanding (or I'm confused now): I mentioned vcpu_wake() only to delimit how much of vcpu_unblock() needs completing for the possible problem to surface that I'm suspecting.

I am not sure what problem you refer. Is it the wake-up request will get ignored?

I've not noticed any risk of an ignored wakeup so far. What I did notice is that if the initial part of vcpu_unblock() runs between any two of the three stores in do_poll(), inconsistent state may result:
We may end up with VPF_blocked clear but ->poll_evtchn set to -1 and/or the respective poll_mask bit set. I've not done any analysis to decide whether such inconsistent state could lead to actual issues.

With the (currently Arm-only) extra if() nothing like this can result upon completion of do_poll().

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel