Enabling vm_event for a guest with more VCPUs than available ring buffer slots freezes the virtual machine
Hello,
Setting, e.g. 16 VCPUs for a HVM guest, ends up blocking the guest completely when subscribing to vm_events, apparently because of this code in xen/common/vm_event.c:
315 /* Give this vCPU a black eye if necessary, on the way out.
316 * See the comments above wake_blocked() for more information
317 * on how this mechanism works to avoid waiting. */
318 avail_req = vm_event_ring_available(ved);
319 if( current->domain == d && avail_req < d->max_vcpus )
320 vm_event_mark_and_pause(current, ved);
It would appear that even if the guest only has 2 online VCPUs, the "avail_req < d->max_vcpus" condition will pause current, and we eventually end up with all the VCPUs paused.
An ugly hack ("avail_req < 2") has allowed booting a guest with many VCPUs (max_vcpus, the guest only brings 2 VCPUs online), however that's just to prove that that was the culprit - a real solution to this needs more in-depth understading of the issue and potential solution. That's basically very old code (pre-2012 at least) that got moved around into the current shape of Xen today - please CC anyone relevant to the discussion that you're aware of.
Thoughts?
Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 25 lines · Razvan Cojocaru
Hello,
Setting, e.g. 16 VCPUs for a HVM guest, ends up blocking the guest completely when subscribing to
vm_events, apparently because of this code inxen/common/vm_event.c:315 /* Give this vCPU a black eye if necessary, on the way out.
316 * See the comments abovewake_blocked()for more information
317 * on how this mechanism works to avoid waiting. */
318avail_req=vm_event_ring_available(ved);
319if( current->domain == d && avail_req < d->max_vcpus )
320vm_event_mark_and_pause(current, ved);It would appear that even if the guest only has 2 online VCPUs, the "
avail_req<d->max_vcpus" condition will pause current, and we eventually end up with all the VCPUs paused.An ugly hack ("
avail_req< 2") has allowed booting a guest with many VCPUs (max_vcpus, the guest only brings 2 VCPUs online), however that's just to prove that that was the culprit - a real solution to this needs more in-depth understading of the issue and potential solution. That's basically very old code (pre-2012 at least) that got moved around into the current shape of Xen today - please CC anyone relevant to the discussion that you're aware of.
Quoted 2 lines
Thoughts?
I think is a side-effect of the growth of the vm_event structure and the fact that we have a single page ring. The check effectively sets a threshold of having enough space for each vCPU to place at least one more event on the ring, and if that's not the case it gets paused. OTOH I think this would only have an effect on asynchronous events, for all other events the vCPU is already paused. Is that the case you have?
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 33 lines · Tamas K Lengyel
On Tue, Feb 7, 2017 at 9:53 AM, Razvan Cojocaru <[email protected] <mailto:[email protected]>> wrote:
Hello,
Setting, e.g. 16 VCPUs for a HVM guest, ends up blocking the guest completely when subscribing to
vm_events, apparently because of this code inxen/common/vm_event.c:315 /* Give this vCPU a black eye if necessary, on the way out.
316 * See the comments abovewake_blocked()for more information
317 * on how this mechanism works to avoid waiting. */
318avail_req=vm_event_ring_available(ved);
319if( current->domain == d && avail_req < d->max_vcpus )
320vm_event_mark_and_pause(current, ved);It would appear that even if the guest only has 2 online VCPUs, the "
avail_req<d->max_vcpus" condition will pause current, and we eventually end up with all the VCPUs paused.An ugly hack ("
avail_req< 2") has allowed booting a guest with many VCPUs (max_vcpus, the guest only brings 2 VCPUs online), however that's just to prove that that was the culprit - a real solution to this needs more in-depth understading of the issue and potential solution. That's basically very old code (pre-2012 at least) that got moved around into the current shape of Xen today - please CC anyone relevant to the discussion that you're aware of.Thoughts?
Quoted 6 lines
I think is a side-effect of the growth of the
vm_eventstructure and the fact that we have a single page ring. The check effectively sets a threshold of having enough space for each vCPU to place at least one more event on the ring, and if that's not the case it gets paused. OTOH I think this would only have an effect on asynchronous events, for all other events the vCPU is already paused. Is that the case you have?
No, on the contrary, all my events are synchronous (the VCPU is paused waiting for the vm_event reply).
I've debugged this a bit, and the problem seems to be that vm_event_wake_blocked() breaks here:
150 /* We remember which vcpu last woke up to avoid scanning always
linearly
151 * from zero and starving higher-numbered vcpus under high load */
152 if ( d->vcpu )
153 {
154 int i, j, k;
155
156 for (i = ved->last_vcpu_wake_up + 1, j = 0; j <d->max_vcpus; i++, j++)
157 {
158 k = i % d->max_vcpus; 159 v = d->vcpu[k]; 160 if ( !v )
161 continue;
162
163 if ( !(ved->blocked) || online >= avail_req )
164 break;
165
166 if ( test_and_clear_bit(ved->pause_flag, &v->pause_flags) )
167 {
168 vcpu_unpause(v); 169 online++; 170 ved->blocked--; 171 ved->last_vcpu_wake_up = k;
172 }
173 }
174 }
at "if ( !(ved->blocked) || online >= avail_req )". At this point, nothing ever gets unblocked. It's hard to believe that this is desired behaviour, as I don't know what could possibly happen for that condition to become false once all the online VCPUs are stuck (especially when the guest has just started booting).
Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 73 lines · Razvan Cojocaru
Quoted 32 lines · Tamas K Lengyel
On Tue, Feb 7, 2017 at 9:53 AM, Razvan Cojocaru <[email protected] <mailto:[email protected]>> wrote:
Hello,
Setting, e.g. 16 VCPUs for a HVM guest, ends up blocking the guest completely when subscribing to
vm_events, apparently because of this code inxen/common/vm_event.c:315 /* Give this vCPU a black eye if necessary, on the way out.
316 * See the comments abovewake_blocked()for more information
317 * on how this mechanism works to avoid waiting. */
318avail_req=vm_event_ring_available(ved);
319if( current->domain == d && avail_req < d->max_vcpus )
320vm_event_mark_and_pause(current, ved);It would appear that even if the guest only has 2 online VCPUs, the "
avail_req<d->max_vcpus" condition will pause current, and we eventually end up with all the VCPUs paused.An ugly hack ("
avail_req< 2") has allowed booting a guest with many VCPUs (max_vcpus, the guest only brings 2 VCPUs online), however that's just to prove that that was the culprit - a real solution to this needs more in-depth understading of the issue and potential solution. That's basically very old code (pre-2012 at least) that got moved around into the current shape of Xen today - please CC anyone relevant to the discussion that you're aware of.Thoughts?
Quoted 6 lines
I think is a side-effect of the growth of the
vm_eventstructure and the fact that we have a single page ring. The check effectively sets a threshold of having enough space for each vCPU to place at least one more event on the ring, and if that's not the case it gets paused. OTOH I think this would only have an effect on asynchronous events, for all other events the vCPU is already paused. Is that the case you have?No, on the contrary, all my events are synchronous (the VCPU is paused waiting for the
vm_eventreply).I've debugged this a bit, and the problem seems to be that
vm_event_wake_blocked()breaks here:150 /* We remember which vcpu last woke up to avoid scanning always
linearly
151 * from zero and starving higher-numbered vcpus under high load */
152 if (d->vcpu)
153 {
154 int i, j, k;
155
156 for (i =ved->last_vcpu_wake_up+ 1, j = 0; j <d->max_vcpus; i++, j++)
157 {158 k = i % d->max_vcpus; 159 v = d->vcpu[k]; 160 if ( !v )161 continue;
162
163 if ( !(ved->blocked) || online >=avail_req)
164 break;
165
166 if (test_and_clear_bit(ved->pause_flag, &v->pause_flags))
167 {168 vcpu_unpause(v); 169 online++; 170 ved->blocked--; 171 ved->last_vcpu_wake_up = k;172 }
173 }
174 }
Quoted 5 lines
at "if ( !(ved->blocked) || online >=
avail_req)". At this point, nothing ever gets unblocked. It's hard to believe that this is desired behaviour, as I don't know what could possibly happen for that condition to become false once all the online VCPUs are stuck (especially when the guest has just started booting).
I wouldn't bet that this logic has ever been tested. If you recall, the addition of register state into the vm_event ring made each entry far larger, which in turns makes it more likely to hit this condition.
However, simply fixing the logic to re-online the cpus isn't a good solution either, as having $N vcpus paused at any one time because of ring contention is not conducive good system performance.
Realistically, the ring size needs to be max_cpus * sizeof(largest vm_event) at an absolute minimum, and I guess this is now beyond 1 page?
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 84 lines · Andrew Cooper
Quoted 73 lines · Razvan Cojocaru
Quoted 32 lines · Tamas K Lengyel
On Tue, Feb 7, 2017 at 9:53 AM, Razvan Cojocaru <[email protected] <mailto:[email protected]>> wrote:
Hello,
Setting, e.g. 16 VCPUs for a HVM guest, ends up blocking the guest completely when subscribing to
vm_events, apparently because of this code inxen/common/vm_event.c:315 /* Give this vCPU a black eye if necessary, on the way out.
316 * See the comments abovewake_blocked()for more information
317 * on how this mechanism works to avoid waiting. */
318avail_req=vm_event_ring_available(ved);
319if( current->domain == d && avail_req < d->max_vcpus )
320vm_event_mark_and_pause(current, ved);It would appear that even if the guest only has 2 online VCPUs, the "
avail_req<d->max_vcpus" condition will pause current, and we eventually end up with all the VCPUs paused.An ugly hack ("
avail_req< 2") has allowed booting a guest with many VCPUs (max_vcpus, the guest only brings 2 VCPUs online), however that's just to prove that that was the culprit - a real solution to this needs more in-depth understading of the issue and potential solution. That's basically very old code (pre-2012 at least) that got moved around into the current shape of Xen today - please CC anyone relevant to the discussion that you're aware of.Thoughts?
Quoted 6 lines
I think is a side-effect of the growth of the
vm_eventstructure and the fact that we have a single page ring. The check effectively sets a threshold of having enough space for each vCPU to place at least one more event on the ring, and if that's not the case it gets paused. OTOH I think this would only have an effect on asynchronous events, for all other events the vCPU is already paused. Is that the case you have?No, on the contrary, all my events are synchronous (the VCPU is paused waiting for the
vm_eventreply).I've debugged this a bit, and the problem seems to be that
vm_event_wake_blocked()breaks here:150 /* We remember which vcpu last woke up to avoid scanning always
linearly
151 * from zero and starving higher-numbered vcpus under high load */
152 if (d->vcpu)
153 {
154 int i, j, k;
155
156 for (i =ved->last_vcpu_wake_up+ 1, j = 0; j <d->max_vcpus; i++, j++)
157 {158 k = i % d->max_vcpus; 159 v = d->vcpu[k]; 160 if ( !v )161 continue;
162
163 if ( !(ved->blocked) || online >=avail_req)
164 break;
165
166 if (test_and_clear_bit(ved->pause_flag, &v->pause_flags))
167 {168 vcpu_unpause(v); 169 online++; 170 ved->blocked--; 171 ved->last_vcpu_wake_up = k;172 }
173 }
174 }Quoted 5 lines
at "if ( !(ved->blocked) || online >=
avail_req)". At this point, nothing ever gets unblocked. It's hard to believe that this is desired behaviour, as I don't know what could possibly happen for that condition to become false once all the online VCPUs are stuck (especially when the guest has just started booting).I wouldn't bet that this logic has ever been tested. If you recall, the addition of register state into the
vm_eventring made each entry far larger, which in turns makes it more likely to hit this condition.
Quoted 6 lines
However, simply fixing the logic to re-online the cpus isn't a good solution either, as having $N vcpus paused at any one time because of ring contention is not conducive good system performance.
Realistically, the ring size needs to be
max_cpus*sizeof(largest vm_event)at an absolute minimum, and I guess this is now beyond 1 page?
Yes, of course the reason this triggers earlier now is the growth of the request's size. Yes, using e.g. 20 VCPUs in the guest's setup will exceed a page's number of slots.
And yes, ideally we should have multi-page ring buffers - however that is a long-term project that requires design changes in other parts of Xen as well (Andrew, CCd here, was recently talking about one).
However, even with a one-page ring buffer, surely it's not good to end up in this situation, especially for guests such as mine, which never actually bring more than 2 VCPUs online. But even if they were to use more, blocking the guest on vm_event init is completely pointless - we might as well return some kind of error if max_vcpus > available slots.
I don't follow the system performance argument. Surely completely blocking the guest is worse.
Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 3 lines · Razvan Cojocaru
And yes, ideally we should have multi-page ring buffers - however that is a long-term project that requires design changes in other parts of Xen as well (Andrew, CCd here, was recently talking about one).
Hah, sorry for using the third person, it's been a long day and I thought I was replying to Tamas. :)
Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 75 lines · Razvan Cojocaru
Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Razvan Cojocaru
Quoted 8 lines · Tamas K Lengyel
On Tue, Feb 7, 2017 at 9:53 AM, Razvan Cojocaru <[email protected] <mailto:[email protected]>> wrote:
Hello,
Setting, e.g. 16 VCPUs for a HVM guest, ends up blocking the guest completely when subscribing to
vm_events, apparently because ofthis
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
code in
xen/common/vm_event.c:315 /* Give this vCPU a black eye if necessary, on the way out.
316 * See the comments abovewake_blocked()for moreinformation
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
317 * on how this mechanism works to avoid waiting. */
318avail_req=vm_event_ring_available(ved);
319if( current->domain == d && avail_req < d->max_vcpus )
320vm_event_mark_and_pause(current, ved);It would appear that even if the guest only has 2 online VCPUs, the "
avail_req<d->max_vcpus" condition will pause current, and we eventually end up with all the VCPUs paused.An ugly hack ("
avail_req< 2") has allowed booting a guest withmany
Quoted 1 line
Quoted 1 line
Quoted 1 line
VCPUs (
max_vcpus, the guest only brings 2 VCPUs online), howeverthat's
Quoted 1 line
Quoted 1 line
Quoted 1 line
just to prove that that was the culprit - a real solution to this
needs
Quoted 1 line
Quoted 1 line
Quoted 1 line
more in-depth understading of the issue and potential solution.
That's
Quoted 1 line
Quoted 1 line
Quoted 1 line
basically very old code (pre-2012 at least) that got moved around
into
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
the current shape of Xen today - please CC anyone relevant to the discussion that you're aware of.
Thoughts?
I think is a side-effect of the growth of the
vm_eventstructure andthe
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
fact that we have a single page ring. The check effectively sets a threshold of having enough space for each vCPU to place at least one more event on the ring, and if that's not the case it gets paused. OTOH I think this would only have an effect on asynchronous events, for all other events the vCPU is already paused. Is that the case you have?
No, on the contrary, all my events are synchronous (the VCPU is paused waiting for the
vm_eventreply).Quoted 6 lines
Quoted 6 lines
I've debugged this a bit, and the problem seems to be that
vm_event_wake_blocked()breaks here:150 /* We remember which vcpu last woke up to avoid scanning always
linearly
151 * from zero and starving higher-numbered vcpus under high load*/
Quoted 8 lines
Quoted 8 lines
152 if (
d->vcpu)
153 {
154 int i, j, k;
155
156 for (i =ved->last_vcpu_wake_up+ 1, j = 0; j <d->max_vcpus; i++, j++)
157 {
158 k = i %d->max_vcpus;Quoted 8 lines
Quoted 8 lines
159 v = d->vcpu[k];
160 if ( !v )
161 continue;
162
163 if ( !(ved->blocked) || online >=avail_req)
164 break;
165
166 if ( test_and_clear_bit(ved->pause_flag,
Quoted 16 lines
&v->pause_flags) )Quoted 14 lines
Quoted 14 lines
167 {
168 vcpu_unpause(v); 169 online++; 170 ved->blocked--; 171 ved->last_vcpu_wake_up = k;172 }
173 }
174 }at "if ( !(ved->blocked) || online >=
avail_req)". At this point, nothing ever gets unblocked. It's hard to believe that this is desired behaviour, as I don't know what could possibly happen for that condition to become false once all the online VCPUs are stuck (especially when the guest has just started booting).
Ah I see what happens. During boot vCPU 0 generates an event and gets marked blocked because the number of vCPUs is so high. The other vCPUs are still unblocked since they are idle, but this test here will still be true (online >= avail_req) and thus we can never unblock vCPU0. And then the boot process is hanging because vCPU0 never resumes. I would argue that this test should be changed to check that there is at least 1 spot on the ring and only break if that is not the case anymore (ie. instead of incrementing online we should be decrementing avail_req).
Quoted 20 lines
Quoted 11 lines
I wouldn't bet that this logic has ever been tested. If you recall, the addition of register state into the
vm_eventring made each entry far larger, which in turns makes it more likely to hit this condition.However, simply fixing the logic to re-online the cpus isn't a good solution either, as having $N vcpus paused at any one time because of ring contention is not conducive good system performance.
Realistically, the ring size needs to be
max_cpus*sizeof(largest vm_event)at an absolute minimum, and I guess this is now beyond 1 page?Yes, of course the reason this triggers earlier now is the growth of the request's size. Yes, using e.g. 20 VCPUs in the guest's setup will exceed a page's number of slots.
And yes, ideally we should have multi-page ring buffers - however that is a long-term project that requires design changes in other parts of Xen as well (Andrew, CCd here, was recently talking about one).
Quoted 9 lines
However, even with a one-page ring buffer, surely it's not good to end up in this situation, especially for guests such as mine, which never actually bring more than 2 VCPUs online. But even if they were to use more, blocking the guest on
vm_eventinit is completely pointless - we might as well return some kind of error ifmax_vcpus> available slots.I don't follow the system performance argument. Surely completely blocking the guest is worse.
I also don't see the point in marking a vCPU blocked if it is already paused. I think this behavior of blocking vCPUs makes only sense for asynchronous events. Razvan, could you test what happens if vm_event_mark_and_pause is only called if the vCPU is unpaused?
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 110 lines · Tamas K Lengyel
On Tue, Feb 7, 2017 at 11:57 AM, Razvan Cojocaru <[email protected] <mailto:[email protected]>> wrote:
On 02/07/2017 08:39 PM, Andrew Cooper wrote: > On 07/02/17 18:31, Razvan Cojocaru wrote: >> On 02/07/2017 08:15 PM, Tamas K Lengyel wrote: >>> >>> On Tue, Feb 7, 2017 at 9:53 AM, Razvan Cojocaru >>> <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>> wrote: >>> >>> Hello, >>> >>> Setting, e.g. 16 VCPUs for a HVM guest, ends up blocking the guest >>> completely when subscribing to vm_events, apparently because of this >>> code in xen/common/vm_event.c: >>> >>> 315 /* Give this vCPU a black eye if necessary, on the way out. >>> 316 * See the comments above wake_blocked() for more information >>> 317 * on how this mechanism works to avoid waiting. */ >>> 318 avail_req = vm_event_ring_available(ved); >>> 319 if( current->domain == d && avail_req < d->max_vcpus ) >>> 320 vm_event_mark_and_pause(current, ved); >>> >>> It would appear that even if the guest only has 2 online VCPUs, the >>> "avail_req < d->max_vcpus" condition will pause current, and we >>> eventually end up with all the VCPUs paused. >>> >>> An ugly hack ("avail_req < 2") has allowed booting a guest with many >>> VCPUs (max_vcpus, the guest only brings 2 VCPUs online), however that's >>> just to prove that that was the culprit - a real solution to this needs >>> more in-depth understading of the issue and potential solution. That's >>> basically very old code (pre-2012 at least) that got moved around into >>> the current shape of Xen today - please CC anyone relevant to the >>> discussion that you're aware of. >>> >>> Thoughts? >>> >>> >>> I think is a side-effect of the growth of the vm_event structure and the >>> fact that we have a single page ring. The check effectively sets a >>> threshold of having enough space for each vCPU to place at least one >>> more event on the ring, and if that's not the case it gets paused. OTOH >>> I think this would only have an effect on asynchronous events, for all >>> other events the vCPU is already paused. Is that the case you have? >> No, on the contrary, all my events are synchronous (the VCPU is paused >> waiting for the vm_event reply). >> >> I've debugged this a bit, and the problem seems to be that >> vm_event_wake_blocked() breaks here: >> >> 150 /* We remember which vcpu last woke up to avoid scanning always >> linearly >> 151 * from zero and starving higher-numbered vcpus under high load */ >> 152 if ( d->vcpu ) >> 153 { >> 154 int i, j, k; >> 155 >> 156 for (i = ved->last_vcpu_wake_up + 1, j = 0; j < >> d->max_vcpus; i++, j++) >> 157 { >> 158 k = i % d->max_vcpus; >> 159 v = d->vcpu[k]; >> 160 if ( !v ) >> 161 continue; >> 162 >> 163 if ( !(ved->blocked) || online >= avail_req ) >> 164 break; >> 165 >> 166 if ( test_and_clear_bit(ved->pause_flag, &v->pause_flags) ) >> 167 { >> 168 vcpu_unpause(v); >> 169 online++; >> 170 ved->blocked--; >> 171 ved->last_vcpu_wake_up = k; >> 172 } >> 173 } >> 174 } >> >> at "if ( !(ved->blocked) || online >= avail_req )". At this point, >> nothing ever gets unblocked. It's hard to believe that this is desired >> behaviour, as I don't know what could possibly happen for that condition >> to become false once all the online VCPUs are stuck (especially when the >> guest has just started booting).
Quoted 8 lines
Ah I see what happens. During boot vCPU 0 generates an event and gets marked blocked because the number of vCPUs is so high. The other vCPUs are still unblocked since they are idle, but this test here will still be true (online >=
avail_req) and thus we can never unblock vCPU0. And then the boot process is hanging because vCPU0 never resumes. I would argue that this test should be changed to check that there is at least 1 spot on the ring and only break if that is not the case anymore (ie. instead of incrementing online we should be decrementingavail_req).
That is exactly what happens. And it can't really be fixed just by increasing the ring buffer (although that definitely helps a lot and would be a smart move): no matter how large it is, we can always ask the domain to use more VCPUs than there are slots in the buffer.
Quoted 28 lines
Quoted 2 lines
I wouldn't bet that this logic has ever been tested. If you
recall, the
> addition of register state into thevm_eventring made each entry far > larger, which in turns makes it more likely to hit this condition. >
> However, simply fixing the logic to re-online the cpus isn't a good > solution either, as having $N vcpus paused at any one time because of > ring contention is not conducive good system performance. >
> Realistically, the ring size needs to bemax_cpus*sizeof(largest > vm_event)at an absolute minimum, and I guess this is now beyond 1 page?Yes, of course the reason this triggers earlier now is the growth of the request's size. Yes, using e.g. 20 VCPUs in the guest's setup will exceed a page's number of slots.
And yes, ideally we should have multi-page ring buffers - however that is a long-term project that requires design changes in other parts of Xen as well (Andrew, CCd here, was recently talking about one).
However, even with a one-page ring buffer, surely it's not good to end up in this situation, especially for guests such as mine, which never actually bring more than 2 VCPUs online. But even if they were to use more, blocking the guest on
vm_eventinit is completely pointless - we might as well return some kind of error ifmax_vcpus> available slots.
Quoted 8 lines
I don't follow the system performance argument. Surely completely blocking the guest is worse.
I also don't see the point in marking a vCPU blocked if it is already paused. I think this behavior of blocking vCPUs makes only sense for asynchronous events. Razvan, could you test what happens if
vm_event_mark_and_pauseis only called if the vCPU is unpaused?
It works for me with this change (using Xen 4.7 sources here):
@@ -318,7 +329,11 @@ void vm_event_put_request(struct domain *d,
* on how this mechanism works to avoid waiting. */
avail_req = vm_event_ring_available(ved);
if( current->domain == d && avail_req < d->max_vcpus )- vm_event_mark_and_pause(current, ved);
+ {
+ if ( !atomic_read( ¤t->vm_event_pause_count ) )
+ vm_event_mark_and_pause(current, ved);
+ }
Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 156 lines · On Tue, 7 Feb 2017 22:41:57 +0200 Razvan Cojocaru wrote:
Quoted 101 lines · Tamas K Lengyel
On Tue, Feb 7, 2017 at 11:57 AM, Razvan Cojocaru wrote:
On 02/07/2017 08:39 PM, Andrew Cooper wrote: > On 07/02/17 18:31, Razvan Cojocaru wrote: >> On 02/07/2017 08:15 PM, Tamas K Lengyel wrote: >>> On Tue, Feb 7, 2017 at 9:53 AM, Razvan Cojocaru wrote: >>> Hello, >>> >>> Setting, e.g. 16 VCPUs for a HVM guest, ends up blocking the guest >>> completely when subscribing to vm_events, apparently because of this >>> code in xen/common/vm_event.c: >>> >>> 315 /* Give this vCPU a black eye if necessary, on the way out. >>> 316 * See the comments above wake_blocked() for more information >>> 317 * on how this mechanism works to avoid waiting. */ >>> 318 avail_req = vm_event_ring_available(ved); >>> 319 if( current->domain == d && avail_req < d->max_vcpus ) >>> 320 vm_event_mark_and_pause(current, ved); >>> >>> It would appear that even if the guest only has 2 online VCPUs, the >>> "avail_req < d->max_vcpus" condition will pause current, and we >>> eventually end up with all the VCPUs paused. >>> >>> An ugly hack ("avail_req < 2") has allowed booting a guest with many >>> VCPUs (max_vcpus, the guest only brings 2 VCPUs online), however that's >>> just to prove that that was the culprit - a real solution to this needs >>> more in-depth understading of the issue and potential solution. That's >>> basically very old code (pre-2012 at least) that got moved around into >>> the current shape of Xen today - please CC anyone relevant to the >>> discussion that you're aware of. >>> >>> Thoughts? >>> >>> >>> I think is a side-effect of the growth of the vm_event structure and the >>> fact that we have a single page ring. The check effectively sets a >>> threshold of having enough space for each vCPU to place at least one >>> more event on the ring, and if that's not the case it gets paused. OTOH >>> I think this would only have an effect on asynchronous events, for all >>> other events the vCPU is already paused. Is that the case you have? >> No, on the contrary, all my events are synchronous (the VCPU is paused >> waiting for the vm_event reply). >> >> I've debugged this a bit, and the problem seems to be that >> vm_event_wake_blocked() breaks here: >> >> 150 /* We remember which vcpu last woke up to avoid scanning always >> linearly >> 151 * from zero and starving higher-numbered vcpus under high load */ >> 152 if ( d->vcpu ) >> 153 { >> 154 int i, j, k; >> 155 >> 156 for (i = ved->last_vcpu_wake_up + 1, j = 0; j < >> d->max_vcpus; i++, j++) >> 157 { >> 158 k = i % d->max_vcpus; >> 159 v = d->vcpu[k]; >> 160 if ( !v ) >> 161 continue; >> 162 >> 163 if ( !(ved->blocked) || online >= avail_req ) >> 164 break; >> 165 >> 166 if ( test_and_clear_bit(ved->pause_flag, &v->pause_flags) ) >> 167 { >> 168 vcpu_unpause(v); >> 169 online++; >> 170 ved->blocked--; >> 171 ved->last_vcpu_wake_up = k; >> 172 } >> 173 } >> 174 } >> >> at "if ( !(ved->blocked) || online >= avail_req )". At this point, >> nothing ever gets unblocked. It's hard to believe that this is desired >> behaviour, as I don't know what could possibly happen for that condition >> to become false once all the online VCPUs are stuck (especially when the >> guest has just started booting).Quoted 8 lines
Ah I see what happens. During boot vCPU 0 generates an event and gets marked blocked because the number of vCPUs is so high. The other vCPUs are still unblocked since they are idle, but this test here will still be true (online >=
avail_req) and thus we can never unblock vCPU0. And then the boot process is hanging because vCPU0 never resumes. I would argue that this test should be changed to check that there is at least 1 spot on the ring and only break if that is not the case anymore (ie. instead of incrementing online we should be decrementingavail_req).That is exactly what happens. And it can't really be fixed just by increasing the ring buffer (although that definitely helps a lot and would be a smart move): no matter how large it is, we can always ask the domain to use more VCPUs than there are slots in the buffer.
Quoted 28 lines
Quoted 2 lines
I wouldn't bet that this logic has ever been tested. If you
recall, the
> addition of register state into thevm_eventring made each entry far > larger, which in turns makes it more likely to hit this condition. >
> However, simply fixing the logic to re-online the cpus isn't a good > solution either, as having $N vcpus paused at any one time because of > ring contention is not conducive good system performance. >
> Realistically, the ring size needs to bemax_cpus*sizeof(largest > vm_event)at an absolute minimum, and I guess this is now beyond 1 page?Yes, of course the reason this triggers earlier now is the growth of the request's size. Yes, using e.g. 20 VCPUs in the guest's setup will exceed a page's number of slots.
And yes, ideally we should have multi-page ring buffers - however that is a long-term project that requires design changes in other parts of Xen as well (Andrew, CCd here, was recently talking about one).
However, even with a one-page ring buffer, surely it's not good to end up in this situation, especially for guests such as mine, which never actually bring more than 2 VCPUs online. But even if they were to use more, blocking the guest on
vm_eventinit is completely pointless - we might as well return some kind of error ifmax_vcpus> available slots.Quoted 8 lines
I don't follow the system performance argument. Surely completely blocking the guest is worse.
I also don't see the point in marking a vCPU blocked if it is already paused. I think this behavior of blocking vCPUs makes only sense for asynchronous events. Razvan, could you test what happens if
vm_event_mark_and_pauseis only called if the vCPU is unpaused?It works for me with this change (using Xen 4.7 sources here):
Review void vm_event_put_request(struct domain *d, · line 335
Quoted 8 lines
(quoted diff) +4
-1@@ -318,7 +329,11 @@ void vm_event_put_request(struct domain *d, * on how this mechanism works to avoid waiting. */ avail_req = vm_event_ring_available(ved); if( current->domain == d && avail_req < d->max_vcpus ) - vm_event_mark_and_pause(current, ved); + { + if ( !atomic_read( ¤t->vm_event_pause_count ) ) + vm_event_mark_and_pause(current, ved); + }
If I'm reading the code correctly, when max_vcpus is greater than the number of slots available in the ring, a race appears that can lead to a ring corruption (in debug mode ASSERT(free_req > 0) will trigger).
For example, when a single slot is available, two vCPUs can race to vm_event_put_request() after both being given a green light in __vm_event_claim_slot(), whose return depends only on vm_event_ring_available() returning non-zero (which it can do, for both vCPUs at the same time).
As it turns out, the bug being talked about prevented this from showing up.
PS: https://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=3643a961195f76ba849a213628c1979240e6fbdd
Signature · 6 lines
Mihai Donțu _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 176 lines · On Wed, 8 Feb 2017 00:09:52 +0200 Mihai Donțu wrote:
Quoted 156 lines · On Tue, 7 Feb 2017 22:41:57 +0200 Razvan Cojocaru wrote:
Quoted 101 lines · Tamas K Lengyel
On Tue, Feb 7, 2017 at 11:57 AM, Razvan Cojocaru wrote:
On 02/07/2017 08:39 PM, Andrew Cooper wrote: > On 07/02/17 18:31, Razvan Cojocaru wrote: >> On 02/07/2017 08:15 PM, Tamas K Lengyel wrote: >>> On Tue, Feb 7, 2017 at 9:53 AM, Razvan Cojocaru wrote: >>> Hello, >>> >>> Setting, e.g. 16 VCPUs for a HVM guest, ends up blocking the guest >>> completely when subscribing to vm_events, apparently because of this >>> code in xen/common/vm_event.c: >>> >>> 315 /* Give this vCPU a black eye if necessary, on the way out. >>> 316 * See the comments above wake_blocked() for more information >>> 317 * on how this mechanism works to avoid waiting. */ >>> 318 avail_req = vm_event_ring_available(ved); >>> 319 if( current->domain == d && avail_req < d->max_vcpus ) >>> 320 vm_event_mark_and_pause(current, ved); >>> >>> It would appear that even if the guest only has 2 online VCPUs, the >>> "avail_req < d->max_vcpus" condition will pause current, and we >>> eventually end up with all the VCPUs paused. >>> >>> An ugly hack ("avail_req < 2") has allowed booting a guest with many >>> VCPUs (max_vcpus, the guest only brings 2 VCPUs online), however that's >>> just to prove that that was the culprit - a real solution to this needs >>> more in-depth understading of the issue and potential solution. That's >>> basically very old code (pre-2012 at least) that got moved around into >>> the current shape of Xen today - please CC anyone relevant to the >>> discussion that you're aware of. >>> >>> Thoughts? >>> >>> >>> I think is a side-effect of the growth of the vm_event structure and the >>> fact that we have a single page ring. The check effectively sets a >>> threshold of having enough space for each vCPU to place at least one >>> more event on the ring, and if that's not the case it gets paused. OTOH >>> I think this would only have an effect on asynchronous events, for all >>> other events the vCPU is already paused. Is that the case you have? >> No, on the contrary, all my events are synchronous (the VCPU is paused >> waiting for the vm_event reply). >> >> I've debugged this a bit, and the problem seems to be that >> vm_event_wake_blocked() breaks here: >> >> 150 /* We remember which vcpu last woke up to avoid scanning always >> linearly >> 151 * from zero and starving higher-numbered vcpus under high load */ >> 152 if ( d->vcpu ) >> 153 { >> 154 int i, j, k; >> 155 >> 156 for (i = ved->last_vcpu_wake_up + 1, j = 0; j < >> d->max_vcpus; i++, j++) >> 157 { >> 158 k = i % d->max_vcpus; >> 159 v = d->vcpu[k]; >> 160 if ( !v ) >> 161 continue; >> 162 >> 163 if ( !(ved->blocked) || online >= avail_req ) >> 164 break; >> 165 >> 166 if ( test_and_clear_bit(ved->pause_flag, &v->pause_flags) ) >> 167 { >> 168 vcpu_unpause(v); >> 169 online++; >> 170 ved->blocked--; >> 171 ved->last_vcpu_wake_up = k; >> 172 } >> 173 } >> 174 } >> >> at "if ( !(ved->blocked) || online >= avail_req )". At this point, >> nothing ever gets unblocked. It's hard to believe that this is desired >> behaviour, as I don't know what could possibly happen for that condition >> to become false once all the online VCPUs are stuck (especially when the >> guest has just started booting).Quoted 8 lines
Ah I see what happens. During boot vCPU 0 generates an event and gets marked blocked because the number of vCPUs is so high. The other vCPUs are still unblocked since they are idle, but this test here will still be true (online >=
avail_req) and thus we can never unblock vCPU0. And then the boot process is hanging because vCPU0 never resumes. I would argue that this test should be changed to check that there is at least 1 spot on the ring and only break if that is not the case anymore (ie. instead of incrementing online we should be decrementingavail_req).That is exactly what happens. And it can't really be fixed just by increasing the ring buffer (although that definitely helps a lot and would be a smart move): no matter how large it is, we can always ask the domain to use more VCPUs than there are slots in the buffer.
Quoted 28 lines
Quoted 2 lines
I wouldn't bet that this logic has ever been tested. If you
recall, the
> addition of register state into thevm_eventring made each entry far > larger, which in turns makes it more likely to hit this condition. >
> However, simply fixing the logic to re-online the cpus isn't a good > solution either, as having $N vcpus paused at any one time because of > ring contention is not conducive good system performance. >
> Realistically, the ring size needs to bemax_cpus*sizeof(largest > vm_event)at an absolute minimum, and I guess this is now beyond 1 page?Yes, of course the reason this triggers earlier now is the growth of the request's size. Yes, using e.g. 20 VCPUs in the guest's setup will exceed a page's number of slots.
And yes, ideally we should have multi-page ring buffers - however that is a long-term project that requires design changes in other parts of Xen as well (Andrew, CCd here, was recently talking about one).
However, even with a one-page ring buffer, surely it's not good to end up in this situation, especially for guests such as mine, which never actually bring more than 2 VCPUs online. But even if they were to use more, blocking the guest on
vm_eventinit is completely pointless - we might as well return some kind of error ifmax_vcpus> available slots.Quoted 8 lines
I don't follow the system performance argument. Surely completely blocking the guest is worse.
I also don't see the point in marking a vCPU blocked if it is already paused. I think this behavior of blocking vCPUs makes only sense for asynchronous events. Razvan, could you test what happens if
vm_event_mark_and_pauseis only called if the vCPU is unpaused?It works for me with this change (using Xen 4.7 sources here):
Review void vm_event_put_request(struct domain *d, · line 335
Quoted 8 lines
(quoted diff) +4
-1@@ -318,7 +329,11 @@ void vm_event_put_request(struct domain *d, * on how this mechanism works to avoid waiting. */ avail_req = vm_event_ring_available(ved); if( current->domain == d && avail_req < d->max_vcpus ) - vm_event_mark_and_pause(current, ved); + { + if ( !atomic_read( ¤t->vm_event_pause_count ) ) + vm_event_mark_and_pause(current, ved); + }If I'm reading the code correctly, when
max_vcpusis greater than the number of slots available in the ring, a race appears that can lead to a ring corruption (in debug modeASSERT(free_req > 0)will trigger).For example, when a single slot is available, two vCPUs can race to
vm_event_put_request()after both being given a green light in__vm_event_claim_slot(), whose return depends only onvm_event_ring_available()returning non-zero (which it can do, for both vCPUs at the same time).
Quoted 5 lines
As it turns out, the bug being talked about prevented this from showing up.
PS: https://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=3643a961195f76ba849a213628c1979240e6fbdd
Nevermind. I missed the math in vm_event_ring_available().
Signature · 6 lines
Mihai Donțu _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 201 lines · Mihai Donțu
Quoted 9 lines · On Tue, 7 Feb 2017 22:41:57 +0200 Razvan Cojocaru wrote:
Quoted 8 lines · Tamas K Lengyel
On Tue, Feb 7, 2017 at 11:57 AM, Razvan Cojocaru wrote:
On 02/07/2017 08:39 PM, Andrew Cooper wrote: > On 07/02/17 18:31, Razvan Cojocaru wrote: >> On 02/07/2017 08:15 PM, Tamas K Lengyel wrote: >>> On Tue, Feb 7, 2017 at 9:53 AM, Razvan Cojocaru wrote: >>> Hello, >>> >>> Setting, e.g. 16 VCPUs for a HVM guest, ends up blockingthe
Quoted 2 lines
Quoted 2 lines
guest
>>> completely when subscribing tovm_events, apparentlybecause
Quoted 10 lines
Quoted 10 lines
of this
>>> code inxen/common/vm_event.c:
>>>
>>> 315 /* Give this vCPU a black eye if necessary, on the
way out.
>>> 316 * See the comments abovewake_blocked()for more
information
>>> 317 * on how this mechanism works to avoid waiting. */
>>> 318avail_req=vm_event_ring_available(ved);
>>> 319 if(current->domain== d &&avail_req<
d->max_vcpus)Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
320
vm_event_mark_and_pause(current, ved);It would appear that even if the guest only has 2 online
VCPUs, the >>> "avail_req < d->max_vcpus" condition will pause current,and we
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
eventually end up with all the VCPUs paused.
An ugly hack ("
avail_req< 2") has allowed booting a guestwith many
>>> VCPUs (max_vcpus, the guest only brings 2 VCPUs online),
however that's
>>> just to prove that that was the culprit - a real solutionto
Quoted 9 lines
Quoted 9 lines
this needs
>>> more in-depth understading of the issue and potential
solution. That's
>>> basically very old code (pre-2012 at least) that got moved
around into
>>> the current shape of Xen today - please CC anyone relevant
to the
>>> discussion that you're aware of.
>>>Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Thoughts?
I think is a side-effect of the growth of the
vm_eventstructure
Quoted 2 lines
Quoted 2 lines
and the
>>> fact that we have a single page ring. The check effectivelysets a
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
threshold of having enough space for each vCPU to place at
least one
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
more event on the ring, and if that's not the case it gets
paused. OTOH
>>> I think this would only have an effect on asynchronous events, for all
>>> other events the vCPU is already paused. Is that the case youhave?
Quoted 38 lines
Quoted 38 lines
Quoted 1 line
Quoted 1 line
No, on the contrary, all my events are synchronous (the VCPU is
paused >> waiting for the vm_event reply). >> >> I've debugged this a bit, and the problem seems to be that >> vm_event_wake_blocked() breaks here: >> >> 150 /* We remember which vcpu last woke up to avoid scanning always >> linearly >> 151 * from zero and starving higher-numbered vcpus under high load */ >> 152 if ( d->vcpu ) >> 153 { >> 154 int i, j, k; >> 155 >> 156 for (i = ved->last_vcpu_wake_up + 1, j = 0; j < >> d->max_vcpus; i++, j++) >> 157 { >> 158 k = i % d->max_vcpus; >> 159 v = d->vcpu[k]; >> 160 if ( !v ) >> 161 continue; >> 162 >> 163 if ( !(ved->blocked) || online >= avail_req ) >> 164 break; >> 165 >> 166 if ( test_and_clear_bit(ved->pause_flag, &v->pause_flags) ) >> 167 { >> 168 vcpu_unpause(v); >> 169 online++; >> 170 ved->blocked--; >> 171 ved->last_vcpu_wake_up = k; >> 172 } >> 173 } >> 174 } >>Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
at "if ( !(ved->blocked) || online >=
avail_req)". At thispoint,
Quoted 9 lines
Quoted 9 lines
Quoted 1 line
Quoted 1 line
nothing ever gets unblocked. It's hard to believe that this is
desired
>> behaviour, as I don't know what could possibly happen for that condition
>> to become false once all the online VCPUs are stuck (especially when the
>> guest has just started booting).Quoted 6 lines
Quoted 6 lines
Ah I see what happens. During boot vCPU 0 generates an event and gets marked blocked because the number of vCPUs is so high. The other vCPUs are still unblocked since they are idle, but this test here will still be true (online >=
avail_req) and thus we can never unblock vCPU0. And then the boot process is hanging because vCPU0 never resumes. I would argue that this test should be changed to check that there is at least1
Quoted 12 lines
Quoted 2 lines
spot on the ring and only break if that is not the case anymore (ie. instead of incrementing online we should be decrementing
avail_req).That is exactly what happens. And it can't really be fixed just by increasing the ring buffer (although that definitely helps a lot and would be a smart move): no matter how large it is, we can always ask the domain to use more VCPUs than there are slots in the buffer.
Quoted 4 lines
Quoted 2 lines
I wouldn't bet that this logic has ever been tested. If you
recall, the
> addition of register state into thevm_eventring made eachentry far
Quoted 1 line
Quoted 1 line
Quoted 1 line
larger, which in turns makes it more likely to hit this
condition.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
However, simply fixing the logic to re-online the cpus isn't a
good
Quoted 1 line
Quoted 1 line
Quoted 1 line
solution either, as having $N vcpus paused at any one time
because of
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
ring contention is not conducive good system performance.
Realistically, the ring size needs to be
max_cpus*sizeof(largest
Quoted 1 line
Quoted 1 line
Quoted 1 line
vm_event) at an absolute minimum, and I guess this is now beyond1
Quoted 3 lines
Quoted 3 lines
page?
Yes, of course the reason this triggers earlier now is the growth
of the
Quoted 4 lines
Quoted 4 lines
request's size. Yes, using e.g. 20 VCPUs in the guest's setup will exceed a page's number of slots.
And yes, ideally we should have multi-page ring buffers - however
that
Quoted 1 line
Quoted 1 line
is a long-term project that requires design changes in other parts
of
Quoted 3 lines
Quoted 3 lines
Xen as well (Andrew, CCd here, was recently talking about one).
However, even with a one-page ring buffer, surely it's not good to
end
Quoted 1 line
Quoted 1 line
up in this situation, especially for guests such as mine, which
never
Quoted 1 line
Quoted 1 line
actually bring more than 2 VCPUs online. But even if they were to
use
Quoted 1 line
Quoted 1 line
more, blocking the guest on
vm_eventinit is completely pointless- we
Quoted 1 line
Quoted 1 line
might as well return some kind of error if
max_vcpus> availableslots.
Quoted 13 lines
Quoted 9 lines
I don't follow the system performance argument. Surely completely blocking the guest is worse.
I also don't see the point in marking a vCPU blocked if it is already paused. I think this behavior of blocking vCPUs makes only sense for asynchronous events. Razvan, could you test what happens if
vm_event_mark_and_pauseis only called if the vCPU is unpaused?It works for me with this change (using Xen 4.7 sources here):
Review void vm_event_put_request(struct domain *d, · line 335
Quoted 8 lines
(quoted diff) +4
-1@@ -318,7 +329,11 @@ void vm_event_put_request(struct domain *d, * on how this mechanism works to avoid waiting. */ avail_req = vm_event_ring_available(ved); if( current->domain == d && avail_req < d->max_vcpus ) - vm_event_mark_and_pause(current, ved); + { + if ( !atomic_read( ¤t->vm_event_pause_count ) ) + vm_event_mark_and_pause(current, ved); + }If I'm reading the code correctly, when
max_vcpusis greater than the number of slots available in the ring, a race appears that can lead to a ring corruption (in debug modeASSERT(free_req > 0)will trigger).For example, when a single slot is available, two vCPUs can race to
vm_event_put_request()after both being given a green light in__vm_event_claim_slot(), whose return depends only onvm_event_ring_available()returning non-zero (which it can do, for both vCPUs at the same time).
Quoted 5 lines
As it turns out, the bug being talked about prevented this from showing up.
PS: https://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=
3643a961195f76ba849a213628c1979240e6fbdd
I was also looking at this but that's not the case. The function __vm_event_claim_slot will eventually trickle down to vm_event_grab_slot, which does have a lock. While holding the lock it calls vm_event_ring_available which returns how many free spots are on the ring (number of items the ring can hold - ved->target_producers - ved->foreign_producers). So if it returns 1 it will be grabbed, and either ved->target_producers or ved->foreign_producers gets incremented, and only then we unlock. The next caller to vm_event_grab_slot will not find a free spot. If that happens the caller to __vm_event_claim_slot will either gets put on the waitqueue (if sleeping_allowed is set) or returns -EBUSY (if losing the event is permissible).
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 125 lines · Razvan Cojocaru
Quoted 21 lines · Tamas K Lengyel
On Tue, Feb 7, 2017 at 11:57 AM, Razvan Cojocaru <[email protected] <mailto:[email protected]>> wrote:
On 02/07/2017 08:39 PM, Andrew Cooper wrote: > On 07/02/17 18:31, Razvan Cojocaru wrote: >> On 02/07/2017 08:15 PM, Tamas K Lengyel wrote: >>> >>> On Tue, Feb 7, 2017 at 9:53 AM, Razvan Cojocaru >>> <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>> wrote: >>> >>> Hello, >>> >>> Setting, e.g. 16 VCPUs for a HVM guest, ends up blocking the guest >>> completely when subscribing to vm_events, apparently because of this >>> code in xen/common/vm_event.c:Quoted 8 lines
>>> >>> 315 /* Give this vCPU a black eye if necessary, on the way out. >>> 316 * See the comments above wake_blocked() for more information >>> 317 * on how this mechanism works to avoid waiting. */ >>> 318 avail_req = vm_event_ring_available(ved); >>> 319 if( current->domain == d && avail_req < d->max_vcpus)
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
320
vm_event_mark_and_pause(current, ved);It would appear that even if the guest only has 2 online
VCPUs, the >>> "avail_req < d->max_vcpus" condition will pause current, andwe
Quoted 16 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
eventually end up with all the VCPUs paused.
An ugly hack ("
avail_req< 2") has allowed booting a guestwith many
>>> VCPUs (max_vcpus, the guest only brings 2 VCPUs online),
however that's
>>> just to prove that that was the culprit - a real solution to
this needs
>>> more in-depth understading of the issue and potential
solution. That's
>>> basically very old code (pre-2012 at least) that got moved
around into
>>> the current shape of Xen today - please CC anyone relevant
to the
>>> discussion that you're aware of.
>>>Quoted 6 lines
>>> Thoughts?
>>>
>>>
>>> I think is a side-effect of the growth of thevm_eventstructure and the
>>> fact that we have a single page ring. The check effectively setsa
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
threshold of having enough space for each vCPU to place at least
one
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
more event on the ring, and if that's not the case it gets
paused. OTOH
>>> I think this would only have an effect on asynchronous events, for all
>>> other events the vCPU is already paused. Is that the case youhave?
Quoted 48 lines
Quoted 1 line
Quoted 1 line
No, on the contrary, all my events are synchronous (the VCPU is
paused >> waiting for the vm_event reply). >> >> I've debugged this a bit, and the problem seems to be that >> vm_event_wake_blocked() breaks here: >> >> 150 /* We remember which vcpu last woke up to avoid scanning always >> linearly >> 151 * from zero and starving higher-numbered vcpus under high load */ >> 152 if ( d->vcpu ) >> 153 { >> 154 int i, j, k; >> 155 >> 156 for (i = ved->last_vcpu_wake_up + 1, j = 0; j < >> d->max_vcpus; i++, j++) >> 157 { >> 158 k = i % d->max_vcpus; >> 159 v = d->vcpu[k]; >> 160 if ( !v ) >> 161 continue; >> 162 >> 163 if ( !(ved->blocked) || online >= avail_req ) >> 164 break; >> 165 >> 166 if ( test_and_clear_bit(ved->pause_flag, &v->pause_flags) ) >> 167 { >> 168 vcpu_unpause(v); >> 169 online++; >> 170 ved->blocked--; >> 171 ved->last_vcpu_wake_up = k; >> 172 } >> 173 } >> 174 } >> >> at "if ( !(ved->blocked) || online >= avail_req )". At this point, >> nothing ever gets unblocked. It's hard to believe that this is desired >> behaviour, as I don't know what could possibly happen for that condition >> to become false once all the online VCPUs are stuck (especially when the >> guest has just started booting).Quoted 8 lines
Ah I see what happens. During boot vCPU 0 generates an event and gets marked blocked because the number of vCPUs is so high. The other vCPUs are still unblocked since they are idle, but this test here will still be true (online >=
avail_req) and thus we can never unblock vCPU0. And then the boot process is hanging because vCPU0 never resumes. I would argue that this test should be changed to check that there is at least 1 spot on the ring and only break if that is not the case anymore (ie. instead of incrementing online we should be decrementingavail_req).
Quoted 5 lines
That is exactly what happens. And it can't really be fixed just by increasing the ring buffer (although that definitely helps a lot and would be a smart move): no matter how large it is, we can always ask the domain to use more VCPUs than there are slots in the buffer.
No, not increasing the ring buffer but fixing the check to unblock when there is at least 1 spot on the ring. So look at this comment...
/*
* vm_event_wake_blocked() will wakeup vcpus waiting for room in the
* ring. These vCPUs were paused on their way out after placing an event,
* but need to be resumed where the ring is capable of processing at least
* one event from them.
*/
.. it seems to me that the check "online >= avail_req" was just wrong from the start.
Quoted 48 lines
Quoted 5 lines
Quoted 3 lines
I wouldn't bet that this logic has ever been tested. If you
recall, the
> addition of register state into thevm_eventring made each entryfar
Quoted 4 lines
Quoted 4 lines
larger, which in turns makes it more likely to hit this condition.
However, simply fixing the logic to re-online the cpus isn't a good solution either, as having $N vcpus paused at any one time because
of
Quoted 7 lines
Quoted 4 lines
ring contention is not conducive good system performance.
Realistically, the ring size needs to be
max_cpus*sizeof(largest vm_event)at an absolute minimum, and I guess this is now beyond 1page?
Yes, of course the reason this triggers earlier now is the growth of
the
Quoted 4 lines
request's size. Yes, using e.g. 20 VCPUs in the guest's setup will exceed a page's number of slots.
And yes, ideally we should have multi-page ring buffers - however
that
Quoted 4 lines
is a long-term project that requires design changes in other parts of Xen as well (Andrew, CCd here, was recently talking about one).
However, even with a one-page ring buffer, surely it's not good to
end
Quoted 3 lines
up in this situation, especially for guests such as mine, which never actually bring more than 2 VCPUs online. But even if they were to use more, blocking the guest on
vm_eventinit is completely pointless -we
Quoted 1 line
might as well return some kind of error if
max_vcpus> availableslots.
Quoted 9 lines
I don't follow the system performance argument. Surely completely blocking the guest is worse.
I also don't see the point in marking a vCPU blocked if it is already paused. I think this behavior of blocking vCPUs makes only sense for asynchronous events. Razvan, could you test what happens if
vm_event_mark_and_pauseis only called if the vCPU is unpaused?It works for me with this change (using Xen 4.7 sources here):
Review void vm_event_put_request(struct domain *d, · line 337
Quoted 10 lines
(quoted diff) +4
-1@@ -318,7 +329,11 @@ void vm_event_put_request(struct domain *d, * on how this mechanism works to avoid waiting. */ avail_req = vm_event_ring_available(ved); if( current->domain == d && avail_req < d->max_vcpus ) - vm_event_mark_and_pause(current, ved); + { + if ( !atomic_read( ¤t->vm_event_pause_count ) ) + vm_event_mark_and_pause(current, ved); + }
Good, that's what I expected. There really is no point in blocking a vCPU that is already paused. But I would just add that check to the if above as another && clause.
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 159 lines · Tamas K Lengyel
On Tue, Feb 7, 2017 at 1:41 PM, Razvan Cojocaru <[email protected] <mailto:[email protected]>> wrote:
On 02/07/2017 10:20 PM, Tamas K Lengyel wrote:
>
>
> On Tue, Feb 7, 2017 at 11:57 AM, Razvan Cojocaru
> <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>> wrote:
>
> On 02/07/2017 08:39 PM, Andrew Cooper wrote:
> > On 07/02/17 18:31, Razvan Cojocaru wrote:
> >> On 02/07/2017 08:15 PM, Tamas K Lengyel wrote:
> >>>
> >>> On Tue, Feb 7, 2017 at 9:53 AM, Razvan Cojocaru
> >>> <[email protected] <mailto:[email protected]>
<mailto:[email protected] <mailto:[email protected]>>
> <mailto:[email protected]
<mailto:[email protected]>
> <mailto:[email protected]
<mailto:[email protected]>>>> wrote:
> >>>
> >>> Hello,
> >>>
> >>> Setting, e.g. 16 VCPUs for a HVM guest, ends up
blocking the
> guest
> >>> completely when subscribing tovm_events, apparently
because
> of this
> >>> code inxen/common/vm_event.c:
> >>>
> >>> 315 /* Give this vCPU a black eye if necessary, on the
> way out.
> >>> 316 * See the comments abovewake_blocked()for more
> information
> >>> 317 * on how this mechanism works to avoid
waiting. */
> >>> 318avail_req=vm_event_ring_available(ved);
> >>> 319 if(current->domain== d &&avail_req<
d->max_vcpus)
> >>> 320vm_event_mark_and_pause(current, ved);
> >>>
> >>> It would appear that even if the guest only has 2 online
> VCPUs, the
> >>> "avail_req<d->max_vcpus" condition will pause
current, and we
> >>> eventually end up with all the VCPUs paused.
> >>>
> >>> An ugly hack ("avail_req< 2") has allowed booting a guest
> with many
> >>> VCPUs (max_vcpus, the guest only brings 2 VCPUs online),
> however that's
> >>> just to prove that that was the culprit - a real
solution to
> this needs
> >>> more in-depth understading of the issue and potential
> solution. That's
> >>> basically very old code (pre-2012 at least) that got moved
> around into
> >>> the current shape of Xen today - please CC anyone relevant
> to the
> >>> discussion that you're aware of.
> >>>
> >>> Thoughts?
> >>>
> >>>
> >>> I think is a side-effect of the growth of thevm_event
structure
> and the
> >>> fact that we have a single page ring. The check
effectively sets a
> >>> threshold of having enough space for each vCPU to place at
least one
> >>> more event on the ring, and if that's not the case it gets
> paused. OTOH
> >>> I think this would only have an effect on asynchronous events,
> for all
> >>> other events the vCPU is already paused. Is that the case
you have?
> >> No, on the contrary, all my events are synchronous (the VCPU is
> paused
> >> waiting for thevm_eventreply).
> >>
> >> I've debugged this a bit, and the problem seems to be that
> >>vm_event_wake_blocked()breaks here:
> >>
> >> 150 /* We remember which vcpu last woke up to avoid
scanning
> always
> >> linearly
> >> 151 * from zero and starving higher-numbered vcpus under
> high load */
> >> 152 if (d->vcpu)
> >> 153 {
> >> 154 int i, j, k;
> >> 155
> >> 156 for (i =ved->last_vcpu_wake_up+ 1, j = 0; j <
> >>d->max_vcpus; i++, j++)
> >> 157 {
> >> 158 k = i %d->max_vcpus;
> >> 159 v = d->vcpu[k];
> >> 160 if ( !v )
> >> 161 continue;
> >> 162
> >> 163 if ( !(ved->blocked) || online >=avail_req)
> >> 164 break;
> >> 165
> >> 166 if ( test_and_clear_bit(ved->pause_flag,
>&v->pause_flags) )
> >> 167 {
> >> 168vcpu_unpause(v);
> >> 169 online++;
> >> 170 ved->blocked--;
> >> 171ved->last_vcpu_wake_up= k;
> >> 172 }
> >> 173 }
> >> 174 }
> >>
> >> at "if ( !(ved->blocked) || online >=avail_req)". At this
point,
> >> nothing ever gets unblocked. It's hard to believe that this is
> desired
> >> behaviour, as I don't know what could possibly happen for that
> condition
> >> to become false once all the online VCPUs are stuck (especially
> when the
> >> guest has just started booting).
>
>
> Ah I see what happens. During boot vCPU 0 generates an event and gets > marked blocked because the number of vCPUs is so high. The other vCPUs > are still unblocked since they are idle, but this test here will still > be true (online >=avail_req) and thus we can never unblock vCPU0. And > then the boot process is hanging because vCPU0 never resumes. I would > argue that this test should be changed to check that there is at least 1
> spot on the ring and only break if that is not the case anymore (ie. > instead of incrementing online we should be decrementingavail_req).That is exactly what happens. And it can't really be fixed just by increasing the ring buffer (although that definitely helps a lot and would be a smart move): no matter how large it is, we can always ask the domain to use more VCPUs than there are slots in the buffer.
No, not increasing the ring buffer but fixing the check to unblock when there is at least 1 spot on the ring. So look at this comment...
/*
*vm_event_wake_blocked()will wakeup vcpus waiting for room in the
* ring. These vCPUs were paused on their way out after placing an event,
* but need to be resumed where the ring is capable of processing at least
* one event from them.
*/
Quoted 2 lines
.. it seems to me that the check "online >=
avail_req" was just wrong from the start.
I've read that to read that there need to be more than one available slot in the ring: "wake vcpus [plural] [...] at leas one event from them".
Quoted 64 lines
Quoted 4 lines
Quoted 2 lines
I wouldn't bet that this logic has ever been tested. If you
recall, the
> addition of register state into thevm_eventring made eachentry far
> > larger, which in turns makes it more likely to hit this
condition.
> >
> > However, simply fixing the logic to re-online the cpus isn't
a good
> > solution either, as having $N vcpus paused at any one time
because of
> > ring contention is not conducive good system performance.
> >
> > Realistically, the ring size needs to bemax_cpus*
sizeof(largest
> >vm_event) at an absolute minimum, and I guess this is now
beyond 1
> page?
>
> Yes, of course the reason this triggers earlier now is the
growth of the
> request's size. Yes, using e.g. 20 VCPUs in the guest's setup will
> exceed a page's number of slots.
>
> And yes, ideally we should have multi-page ring buffers -
however that
> is a long-term project that requires design changes in other
parts of
> Xen as well (Andrew, CCd here, was recently talking about one).
>
> However, even with a one-page ring buffer, surely it's not
good to end
> up in this situation, especially for guests such as mine,
which never
> actually bring more than 2 VCPUs online. But even if they were
to use
> more, blocking the guest onvm_eventinit is completely
pointless - we
> might as well return some kind of error ifmax_vcpus>
available slots.
>
> I don't follow the system performance argument. Surely completely
> blocking the guest is worse.
>
>
> I also don't see the point in marking a vCPU blocked if it is already > paused. I think this behavior of blocking vCPUs makes only sense for > asynchronous events. Razvan, could you test what happens if >vm_event_mark_and_pauseis only called if the vCPU is unpaused?It works for me with this change (using Xen 4.7 sources here):
@@ -318,7 +329,11 @@ void vm_event_put_request(struct domain *d,
* on how this mechanism works to avoid waiting. */
avail_req=vm_event_ring_available(ved);
if( current->domain == d && avail_req < d->max_vcpus )
-vm_event_mark_and_pause(current, ved);
+ {
+ if ( !atomic_read(¤t->vm_event_pause_count) )
+vm_event_mark_and_pause(current, ved);
+ }
Quoted 3 lines
Good, that's what I expected. There really is no point in blocking a vCPU that is already paused. But I would just add that check to the if above as another && clause.
Quite right, this was late-night debugging and I also had a printk() in that scope, which I quickly removed on pasting the change. The patch will simply have the additional && condition.
Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 182 lines · Razvan Cojocaru
Quoted 5 lines · Tamas K Lengyel
On Tue, Feb 7, 2017 at 1:41 PM, Razvan Cojocaru <[email protected] <mailto:[email protected]>> wrote:
Quoted 14 lines
On 02/07/2017 10:20 PM, Tamas K Lengyel wrote: > > > On Tue, Feb 7, 2017 at 11:57 AM, Razvan Cojocaru > <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>> wrote: > > On 02/07/2017 08:39 PM, Andrew Cooper wrote: > > On 07/02/17 18:31, Razvan Cojocaru wrote: > >> On 02/07/2017 08:15 PM, Tamas K Lengyel wrote: > >>> > >>> On Tue, Feb 7, 2017 at 9:53 AM, Razvan Cojocaru > >>> <[email protected] <mailto:rcojocaru@bitdefender.com>
Quoted 17 lines
<mailto:[email protected] <mailto:[email protected]
Quoted 2 lines
<mailto:[email protected]
<mailto:[email protected]>
> <mailto:[email protected]
<mailto:[email protected]>>>> wrote:
> >>>
> >>> Hello,
> >>>
> >>> Setting, e.g. 16 VCPUs for a HVM guest, ends up
blocking the
> guest
> >>> completely when subscribing tovm_events, apparently
because
> of this
> >>> code inxen/common/vm_event.c:
> >>>Quoted 1 line
> >>> 315 /* Give this vCPU a black eye if necessary, on
the
Quoted 2 lines
Quoted 2 lines
way out.
>>> 316 * See the comments abovewake_blocked()formore
Quoted 9 lines
Quoted 2 lines
information
>>> 317 * on how this mechanism works to avoidwaiting. */ > >>> 318 avail_req = vm_event_ring_available(ved); > >>> 319 if( current->domain == d && avail_req < d->max_vcpus ) > >>> 320 vm_event_mark_and_pause(current, ved); > >>> > >>> It would appear that even if the guest only has 2online
Quoted 6 lines
Quoted 2 lines
VCPUs, the >>> "avail_req < d->max_vcpus" condition will pausecurrent, and we
> >>> eventually end up with all the VCPUs paused.
> >>>
> >>> An ugly hack ("avail_req< 2") has allowed booting aguest
Quoted 2 lines
Quoted 2 lines
with many
>>> VCPUs (max_vcpus, the guest only brings 2 VCPUsonline),
Quoted 7 lines
Quoted 2 lines
however that's
>>> just to prove that that was the culprit - a realsolution to
> this needs
> >>> more in-depth understading of the issue and potential
> solution. That's
> >>> basically very old code (pre-2012 at least) that gotmoved
Quoted 2 lines
Quoted 2 lines
around into
>>> the current shape of Xen today - please CC anyonerelevant
Quoted 7 lines
Quoted 7 lines
to the >>> discussion that you're aware of. >>> >>> Thoughts? >>> >>> >>> I think is a side-effect of the growth of the vm_eventQuoted 9 lines
structure
> and the
> >>> fact that we have a single page ring. The check
effectively sets a
> >>> threshold of having enough space for each vCPU to place at
least one
> >>> more event on the ring, and if that's not the case it gets
> paused. OTOH
> >>> I think this would only have an effect on asynchronousevents,
Quoted 4 lines
Quoted 2 lines
for all
>>> other events the vCPU is already paused. Is that the caseyou have?
> >> No, on the contrary, all my events are synchronous (theVCPU is
Quoted 11 lines
Quoted 7 lines
paused
>> waiting for thevm_eventreply).
>>
>> I've debugged this a bit, and the problem seems to be that >>vm_event_wake_blocked()breaks here:
>>
>> 150 /* We remember which vcpu last woke up to avoidscanning
> always
> >> linearly
> >> 151 * from zero and starving higher-numbered vcpusunder
Quoted 6 lines
Quoted 6 lines
high load */ >> 152 if ( d->vcpu ) >> 153 { >> 154 int i, j, k; >> 155 >> 156 for (i = ved->last_vcpu_wake_up + 1, j = 0; j <Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
d->max_vcpus; i++, j++)
157 {158 k = i % d->max_vcpus; 159 v = d->vcpu[k]; 160 if ( !v )161 continue;
162
163 if ( !(ved->blocked) || online >=avail_req)
Quoted 13 lines
Quoted 13 lines
Quoted 3 lines
Quoted 3 lines
164 break;
165
166 if ( test_and_clear_bit(ved->pause_flag,&v->pause_flags) ) >> 167 { >> 168 vcpu_unpause(v); >> 169 online++; >> 170 ved->blocked--; >> 171 ved->last_vcpu_wake_up = k; >> 172 } >> 173 } >> 174 } >>Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
at "if ( !(ved->blocked) || online >=
avail_req)". At thispoint,
> >> nothing ever gets unblocked. It's hard to believe that thisis
Quoted 2 lines
Quoted 2 lines
desired
>> behaviour, as I don't know what could possibly happen forthat
Quoted 2 lines
Quoted 2 lines
condition
>> to become false once all the online VCPUs are stuck(especially
Quoted 5 lines
Quoted 5 lines
when the
>> guest has just started booting).Ah I see what happens. During boot vCPU 0 generates an event and
gets
Quoted 1 line
Quoted 1 line
marked blocked because the number of vCPUs is so high. The other
vCPUs
Quoted 1 line
Quoted 1 line
are still unblocked since they are idle, but this test here will
still
Quoted 1 line
Quoted 1 line
be true (online >=
avail_req) and thus we can never unblock vCPU0.And
Quoted 1 line
Quoted 1 line
then the boot process is hanging because vCPU0 never resumes. I
would
Quoted 3 lines
Quoted 1 line
argue that this test should be changed to check that there is at
least 1
> spot on the ring and only break if that is not the case anymore(ie.
Quoted 1 line
Quoted 1 line
instead of incrementing online we should be decrementing
avail_req).Quoted 4 lines
That is exactly what happens. And it can't really be fixed just by increasing the ring buffer (although that definitely helps a lot and would be a smart move): no matter how large it is, we can always ask
the
Quoted 10 lines
domain to use more VCPUs than there are slots in the buffer.
No, not increasing the ring buffer but fixing the check to unblock when there is at least 1 spot on the ring. So look at this comment...
/*
*vm_event_wake_blocked()will wakeup vcpus waiting for room in the
* ring. These vCPUs were paused on their way out after placing an event,
* but need to be resumed where the ring is capable of processing atleast
Quoted 9 lines
Quoted 5 lines
* one event from them.
*/.. it seems to me that the check "online >=
avail_req" was just wrong from the start.I've read that to read that there need to be more than one available slot in the ring: "wake vcpus [plural] [...] at leas one event from them".
Right, I mean we can unblock all vCPUs as soon as there is 1 spot on the ring. If they trap again, they will just be placed in the waitqueue instead of being blocked. I think that would work fine.
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel