2023-06-21 20:12:44

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH] Updates to Xen hypercall preemption

On Wed, Jun 21, 2023 at 07:19:21PM +0000, Per Bilse wrote:
> On 6/21/2023 5:40 PM, Peter Zijlstra wrote:
> > I don't understand it -- fundamentally, how can linux schedule when the
> > guest isn't even running? Hypercall transfers control to the
> > host/hypervisor and leaves the guest suspended.
>
> Hi Peter, as noted in earlier note to Andy, this is essentially existing
> code that other commits have rendered ineffective over time. Hence,
> the finer details of how or why it works haven't changed since it was
> first introduced.

That doesn't mean you don't have to explain how stuff works.

> > This makes no sense; the race that warning warns about is:
> >
> > CPU0 CPU1
> > per-cpu write
> > <preempt-out>
> > <preempt-in>
> > do-hypercall
> >
> > So you wrote the value on CPU0, got migrated to CPU1 because you had
> > preemptioned enabled, and then continue with the percpu value of CPU1
> > because that's where you're at now.
>
> This issue was raised internally, and it was noted that the only way
> for the preemptible code to switch task is via an interrupt that goes
> through xen_pv_evtchn_do_upcall(), which handles this. I'm happy to
> check with my sources, but it's holiday season right now.

Then it should have all sorts of comments on and a comprehensive
changelog.

> >> 4) Update irqentry_exit_cond_resched() to raw_irqentry_exit_cond_resched().
> >> The code will call irqentry_exit_cond_resched() if the flag (as noted
> >> above) is set, but the dynamic preemption feature will livepatch that
> >> function to a no-op unless full preemption is selected. The code is
> >> therefore updated to call raw_irqentry_exit_cond_resched().
> >
> > That, again meeds more explanation. Why do you want this if not
> > preemptible?
>
> I'm not quite sure what you mean here. Dynamic preemption
> will livepatch irqentry_exit_cond_resched() to be a no-op, while
> raw_irqentry_exit_cond_resched() remains functional. This was
> introduced in commit 4624a14f4daa last year which was said to fix
> the problem, but doesn't. You may remember, it was signed off by
> yourself and Mark Rutland.

I don't see the relation; what you're doing is making dynamic preempt
that's not configured for full preempt do preemption. That's weird, and
again no comments.

I'm with Andy in that simply forcing full preemption would make far more
sense -- but I'm still missing something fundamental, see below.

> > You're doing 4 things, that should be 4 patches. Also, please give more
> > clues for how this is supposed to work at all.
>
> I respectfully have to disagree with that. The fixes here are very
> closely related, and we're not introducing anything new, we're merely
> re-enabling code which has been rendered ineffective due to oversights
> in commits made after the code was first introduced. How the code is
> supposed to work hasn't changed, and is beyond the scope of these fixes;
> I'm sure it must have been discussed at great length at the time (commit
> fdfd811ddde3).

You didn't even so much as reference that commit, nor provide any other
explanation. And having now read that commit, I'm not much enlightend.

*HOW* can a hypercall, something that exits the Guest and has the
Host/Hypervisor run get preempted in the Guest -- that isn't running.

Or are you calling apples pears?


2023-06-22 05:50:49

by Juergen Gross

[permalink] [raw]
Subject: Re: [PATCH] Updates to Xen hypercall preemption

On 21.06.23 22:04, Peter Zijlstra wrote:
> On Wed, Jun 21, 2023 at 07:19:21PM +0000, Per Bilse wrote:
>> On 6/21/2023 5:40 PM, Peter Zijlstra wrote:
>>> I don't understand it -- fundamentally, how can linux schedule when the
>>> guest isn't even running? Hypercall transfers control to the
>>> host/hypervisor and leaves the guest suspended.
>>
>> Hi Peter, as noted in earlier note to Andy, this is essentially existing
>> code that other commits have rendered ineffective over time. Hence,
>> the finer details of how or why it works haven't changed since it was
>> first introduced.
>
> That doesn't mean you don't have to explain how stuff works.
>
>>> This makes no sense; the race that warning warns about is:
>>>
>>> CPU0 CPU1
>>> per-cpu write
>>> <preempt-out>
>>> <preempt-in>
>>> do-hypercall
>>>
>>> So you wrote the value on CPU0, got migrated to CPU1 because you had
>>> preemptioned enabled, and then continue with the percpu value of CPU1
>>> because that's where you're at now.
>>
>> This issue was raised internally, and it was noted that the only way
>> for the preemptible code to switch task is via an interrupt that goes
>> through xen_pv_evtchn_do_upcall(), which handles this. I'm happy to
>> check with my sources, but it's holiday season right now.
>
> Then it should have all sorts of comments on and a comprehensive
> changelog.
>
>>>> 4) Update irqentry_exit_cond_resched() to raw_irqentry_exit_cond_resched().
>>>> The code will call irqentry_exit_cond_resched() if the flag (as noted
>>>> above) is set, but the dynamic preemption feature will livepatch that
>>>> function to a no-op unless full preemption is selected. The code is
>>>> therefore updated to call raw_irqentry_exit_cond_resched().
>>>
>>> That, again meeds more explanation. Why do you want this if not
>>> preemptible?
>>
>> I'm not quite sure what you mean here. Dynamic preemption
>> will livepatch irqentry_exit_cond_resched() to be a no-op, while
>> raw_irqentry_exit_cond_resched() remains functional. This was
>> introduced in commit 4624a14f4daa last year which was said to fix
>> the problem, but doesn't. You may remember, it was signed off by
>> yourself and Mark Rutland.
>
> I don't see the relation; what you're doing is making dynamic preempt
> that's not configured for full preempt do preemption. That's weird, and
> again no comments.
>
> I'm with Andy in that simply forcing full preemption would make far more
> sense -- but I'm still missing something fundamental, see below.
>
>>> You're doing 4 things, that should be 4 patches. Also, please give more
>>> clues for how this is supposed to work at all.
>>
>> I respectfully have to disagree with that. The fixes here are very
>> closely related, and we're not introducing anything new, we're merely
>> re-enabling code which has been rendered ineffective due to oversights
>> in commits made after the code was first introduced. How the code is
>> supposed to work hasn't changed, and is beyond the scope of these fixes;
>> I'm sure it must have been discussed at great length at the time (commit
>> fdfd811ddde3).
>
> You didn't even so much as reference that commit, nor provide any other
> explanation. And having now read that commit, I'm not much enlightend.
>
> *HOW* can a hypercall, something that exits the Guest and has the
> Host/Hypervisor run get preempted in the Guest -- that isn't running.
>
> Or are you calling apples pears?

The hypercalls we are talking of are synchronous ones. They are running
in the context of the vcpu doing the call (like a syscall from userland is
running in the process context).

The hypervisor will return to guest context from time to time by modifying
the registers such that the guest will do the hypercall again with different
input values for the hypervisor, resulting in a proper continuation of the
hypercall processing.

It is an awful interface and I agree that switching to full preemption in
dom0 seems to be the route which we should try to take. The downside would
be that some workloads might see worse performance due to backend I/O
handling might get preempted.

Just thinking - can full preemption be enabled per process?


Juergen


Attachments:
OpenPGP_0xB0DE9DD628BF132F.asc (3.08 kB)
OpenPGP public key
OpenPGP_signature (505.00 B)
OpenPGP digital signature
Download all attachments

2023-06-22 08:30:14

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH] Updates to Xen hypercall preemption

On Thu, Jun 22, 2023 at 07:22:53AM +0200, Juergen Gross wrote:

> The hypercalls we are talking of are synchronous ones. They are running
> in the context of the vcpu doing the call (like a syscall from userland is
> running in the process context).

(so time actually passes from the guest's pov?)

> The hypervisor will return to guest context from time to time by modifying
> the registers such that the guest will do the hypercall again with different
> input values for the hypervisor, resulting in a proper continuation of the
> hypercall processing.

Eeeuw.. that's pretty terrible. And changing this isn't in the cards,
like at all?

That is, why isn't this whole thing written like:

for (;;) {
ret = hypercall(foo);
if (ret == -EAGAIN) {
cond_resched();
continue;
}
break;
}

> It is an awful interface and I agree that switching to full preemption in
> dom0 seems to be the route which we should try to take.

Well, I would very strongly suggest the route to take is to scrap the
whole thing and invest in doing something saner so we don't have to jump
through hoops like this.

This is quite possibly the worst possible interface for this Xen could
have come up with -- awards material for sure.

> The downside would be that some workloads might see worse performance
> due to backend I/O handling might get preempted.

Is that an actual concern? Mark this a legaxy inteface and anybody who
wants to get away from it updates.

> Just thinking - can full preemption be enabled per process?

Nope, that's a system wide thing. Preemption is something that's driven
by the requirements of the tasks that preempt, not something by the
tasks that get preempted.

Andy's idea of having that thing intercepted as an exception (EXTABLE
like) and relocating the IP to a place that does cond_resched() before
going back is an option.. gross, but possibly better, dunno.

Quite the mess indeed :/

2023-06-22 11:17:31

by Juergen Gross

[permalink] [raw]
Subject: Re: [PATCH] Updates to Xen hypercall preemption

On 22.06.23 10:26, Peter Zijlstra wrote:
> On Thu, Jun 22, 2023 at 07:22:53AM +0200, Juergen Gross wrote:
>
>> The hypercalls we are talking of are synchronous ones. They are running
>> in the context of the vcpu doing the call (like a syscall from userland is
>> running in the process context).
>
> (so time actually passes from the guest's pov?)

Correct.

>
>> The hypervisor will return to guest context from time to time by modifying
>> the registers such that the guest will do the hypercall again with different
>> input values for the hypervisor, resulting in a proper continuation of the
>> hypercall processing.
>
> Eeeuw.. that's pretty terrible. And changing this isn't in the cards,
> like at all?

In the long run this should be possible, but not for already existing Xen
versions.

>
> That is, why isn't this whole thing written like:
>
> for (;;) {
> ret = hypercall(foo);
> if (ret == -EAGAIN) {
> cond_resched();
> continue;
> }
> break;
> }

The hypervisor doesn't return -EAGAIN for hysterical reasons.

This would be one of the options to change the interface. OTOH there are cases
where already existing hypercalls need to be modified in the hypervisor to do
preemption in the middle due to e.g. security reasons (avoiding cpu hogging in
special cases).

Additionally some of the hypercalls being subject to preemption are allowed in
unprivileged guests, too. Those are mostly hypercalls allowed for PV guests
only, but some are usable by all guests.

>
>> It is an awful interface and I agree that switching to full preemption in
>> dom0 seems to be the route which we should try to take.
>
> Well, I would very strongly suggest the route to take is to scrap the
> whole thing and invest in doing something saner so we don't have to jump
> through hoops like this.
>
> This is quite possibly the worst possible interface for this Xen could
> have come up with -- awards material for sure.

Yes.

>
>> The downside would be that some workloads might see worse performance
>> due to backend I/O handling might get preempted.
>
> Is that an actual concern? Mark this a legaxy inteface and anybody who
> wants to get away from it updates.

It isn't that easy. See above.

>
>> Just thinking - can full preemption be enabled per process?
>
> Nope, that's a system wide thing. Preemption is something that's driven
> by the requirements of the tasks that preempt, not something by the
> tasks that get preempted.

Depends. If a task in a non-preempt system could switch itself to be
preemptable, we could do so around hypercalls without compromising the
general preemption setting. Disabling preemption in a preemptable system
should continue to be possible for short code paths only, of course.

> Andy's idea of having that thing intercepted as an exception (EXTABLE
> like) and relocating the IP to a place that does cond_resched() before
> going back is an option.. gross, but possibly better, dunno.
>
> Quite the mess indeed :/

Yeah.


Juergen


Attachments:
OpenPGP_0xB0DE9DD628BF132F.asc (3.08 kB)
OpenPGP public key
OpenPGP_signature (505.00 B)
OpenPGP digital signature
Download all attachments

2023-06-22 11:33:00

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH] Updates to Xen hypercall preemption

On Thu, Jun 22, 2023 at 12:33:31PM +0200, Juergen Gross wrote:
> On 22.06.23 10:26, Peter Zijlstra wrote:

> > > The downside would be that some workloads might see worse performance
> > > due to backend I/O handling might get preempted.
> >
> > Is that an actual concern? Mark this a legaxy inteface and anybody who
> > wants to get away from it updates.
>
> It isn't that easy. See above.

Well, the old stuff gets to use full preemption on Dom0, then the new
stuff gets more shiny options.

> > > Just thinking - can full preemption be enabled per process?
> >
> > Nope, that's a system wide thing. Preemption is something that's driven
> > by the requirements of the tasks that preempt, not something by the
> > tasks that get preempted.
>
> Depends. If a task in a non-preempt system could switch itself to be
> preemptable, we could do so around hypercalls without compromising the
> general preemption setting. Disabling preemption in a preemptable system
> should continue to be possible for short code paths only, of course.

So something along those lines was suggested elsewhere, and I'm still
not entirely sure how I feel about it, but look here:

https://lkml.kernel.org/r/[email protected]

Specifically patches 7 and 8. It is very close so that you currently
do/want. Those patches are many moons old and i've not seen an update on
them, so I've no idea where they are.

It solves a similar problem except it is 'rep string' instructions
that's being interrupted.

2023-06-22 12:22:42

by Juergen Gross

[permalink] [raw]
Subject: Re: [PATCH] Updates to Xen hypercall preemption

On 22.06.23 13:15, Peter Zijlstra wrote:
> On Thu, Jun 22, 2023 at 12:33:31PM +0200, Juergen Gross wrote:
>> On 22.06.23 10:26, Peter Zijlstra wrote:
>
>>>> The downside would be that some workloads might see worse performance
>>>> due to backend I/O handling might get preempted.
>>>
>>> Is that an actual concern? Mark this a legaxy inteface and anybody who
>>> wants to get away from it updates.
>>
>> It isn't that easy. See above.
>
> Well, the old stuff gets to use full preemption on Dom0, then the new
> stuff gets more shiny options.

Yeah, but what about the hypercalls from non-dom0 systems needing the same
handling? This would require to run all guests which are using hypercalls
fully preemptive.

>
>>>> Just thinking - can full preemption be enabled per process?
>>>
>>> Nope, that's a system wide thing. Preemption is something that's driven
>>> by the requirements of the tasks that preempt, not something by the
>>> tasks that get preempted.
>>
>> Depends. If a task in a non-preempt system could switch itself to be
>> preemptable, we could do so around hypercalls without compromising the
>> general preemption setting. Disabling preemption in a preemptable system
>> should continue to be possible for short code paths only, of course.
>
> So something along those lines was suggested elsewhere, and I'm still
> not entirely sure how I feel about it, but look here:
>
> https://lkml.kernel.org/r/[email protected]
>
> Specifically patches 7 and 8. It is very close so that you currently
> do/want. Those patches are many moons old and i've not seen an update on
> them, so I've no idea where they are.
>
> It solves a similar problem except it is 'rep string' instructions
> that's being interrupted.

Right. I'll ping Ankur.


Juergen


Attachments:
OpenPGP_0xB0DE9DD628BF132F.asc (3.08 kB)
OpenPGP public key
OpenPGP_signature (505.00 B)
OpenPGP digital signature
Download all attachments

2023-06-22 16:47:39

by Andy Lutomirski

[permalink] [raw]
Subject: Re: [PATCH] Updates to Xen hypercall preemption

On Thu, Jun 22, 2023, at 3:33 AM, Juergen Gross wrote:
> On 22.06.23 10:26, Peter Zijlstra wrote:
>> On Thu, Jun 22, 2023 at 07:22:53AM +0200, Juergen Gross wrote:
>>
>>> The hypercalls we are talking of are synchronous ones. They are running
>>> in the context of the vcpu doing the call (like a syscall from userland is
>>> running in the process context).
>>
>> (so time actually passes from the guest's pov?)
>
> Correct.
>
>>
>>> The hypervisor will return to guest context from time to time by modifying
>>> the registers such that the guest will do the hypercall again with different
>>> input values for the hypervisor, resulting in a proper continuation of the
>>> hypercall processing.
>>
>> Eeeuw.. that's pretty terrible. And changing this isn't in the cards,
>> like at all?
>
> In the long run this should be possible, but not for already existing Xen
> versions.
>
>>
>> That is, why isn't this whole thing written like:
>>
>> for (;;) {
>> ret = hypercall(foo);
>> if (ret == -EAGAIN) {
>> cond_resched();
>> continue;
>> }
>> break;
>> }
>
> The hypervisor doesn't return -EAGAIN for hysterical reasons.
>
> This would be one of the options to change the interface. OTOH there are cases
> where already existing hypercalls need to be modified in the hypervisor to do
> preemption in the middle due to e.g. security reasons (avoiding cpu hogging in
> special cases).
>
> Additionally some of the hypercalls being subject to preemption are allowed in
> unprivileged guests, too. Those are mostly hypercalls allowed for PV guests
> only, but some are usable by all guests.
>
>>
>>> It is an awful interface and I agree that switching to full preemption in
>>> dom0 seems to be the route which we should try to take.
>>
>> Well, I would very strongly suggest the route to take is to scrap the
>> whole thing and invest in doing something saner so we don't have to jump
>> through hoops like this.
>>
>> This is quite possibly the worst possible interface for this Xen could
>> have come up with -- awards material for sure.
>
> Yes.
>
>>
>>> The downside would be that some workloads might see worse performance
>>> due to backend I/O handling might get preempted.
>>
>> Is that an actual concern? Mark this a legaxy inteface and anybody who
>> wants to get away from it updates.
>
> It isn't that easy. See above.
>
>>
>>> Just thinking - can full preemption be enabled per process?
>>
>> Nope, that's a system wide thing. Preemption is something that's driven
>> by the requirements of the tasks that preempt, not something by the
>> tasks that get preempted.
>
> Depends. If a task in a non-preempt system could switch itself to be
> preemptable, we could do so around hypercalls without compromising the
> general preemption setting. Disabling preemption in a preemptable system
> should continue to be possible for short code paths only, of course.
>
>> Andy's idea of having that thing intercepted as an exception (EXTABLE
>> like) and relocating the IP to a place that does cond_resched() before
>> going back is an option.. gross, but possibly better, dunno.
>>
>> Quite the mess indeed :/
>
> Yeah.

Having one implementation of interrupt handlers that schedule when they interrupt kernel code (the normal full preempt path) is one thing. Having two of them (full preempt and super-special-Xen) is IMO quite a bit worse. Especially since no one tests the latter very well.

Having a horrible Xen-specific extable-like thingy seems honestly rather less bad. It could even have a little self-contained test that runs at boot, I bet.

But I'll bite on the performance impact issue. What, exactly, is wrong with full preemption? Full preemption has two sources of overhead, I think. One is a bit of bookkeeping. The other is the overhead inherent in actually rescheduling -- context switch cost, losing things from cache, etc.

The bookkeeping part should have quite low overhead. The scheduling part sounds like it might just need some scheduler tuning if it's really a problem.

In any case, for backend IO, full preemption sounds like it should be a win, not a loss. If I'm asking dom0 to do backend IO for me, I don't want it delayed because dom0 was busy doing something else boring. IO is faster when the latency between requesting it and actually submitting it to hardware is lower.

Can anyone actually demonstrate full preemption being a loss on a real Xen PV workload?

--Andy

2023-06-22 17:30:59

by Juergen Gross

[permalink] [raw]
Subject: Re: [PATCH] Updates to Xen hypercall preemption

On 22.06.23 18:39, Andy Lutomirski wrote:
> On Thu, Jun 22, 2023, at 3:33 AM, Juergen Gross wrote:
>> On 22.06.23 10:26, Peter Zijlstra wrote:
>>> On Thu, Jun 22, 2023 at 07:22:53AM +0200, Juergen Gross wrote:
>>>
>>>> The hypercalls we are talking of are synchronous ones. They are running
>>>> in the context of the vcpu doing the call (like a syscall from userland is
>>>> running in the process context).
>>>
>>> (so time actually passes from the guest's pov?)
>>
>> Correct.
>>
>>>
>>>> The hypervisor will return to guest context from time to time by modifying
>>>> the registers such that the guest will do the hypercall again with different
>>>> input values for the hypervisor, resulting in a proper continuation of the
>>>> hypercall processing.
>>>
>>> Eeeuw.. that's pretty terrible. And changing this isn't in the cards,
>>> like at all?
>>
>> In the long run this should be possible, but not for already existing Xen
>> versions.
>>
>>>
>>> That is, why isn't this whole thing written like:
>>>
>>> for (;;) {
>>> ret = hypercall(foo);
>>> if (ret == -EAGAIN) {
>>> cond_resched();
>>> continue;
>>> }
>>> break;
>>> }
>>
>> The hypervisor doesn't return -EAGAIN for hysterical reasons.
>>
>> This would be one of the options to change the interface. OTOH there are cases
>> where already existing hypercalls need to be modified in the hypervisor to do
>> preemption in the middle due to e.g. security reasons (avoiding cpu hogging in
>> special cases).
>>
>> Additionally some of the hypercalls being subject to preemption are allowed in
>> unprivileged guests, too. Those are mostly hypercalls allowed for PV guests
>> only, but some are usable by all guests.
>>
>>>
>>>> It is an awful interface and I agree that switching to full preemption in
>>>> dom0 seems to be the route which we should try to take.
>>>
>>> Well, I would very strongly suggest the route to take is to scrap the
>>> whole thing and invest in doing something saner so we don't have to jump
>>> through hoops like this.
>>>
>>> This is quite possibly the worst possible interface for this Xen could
>>> have come up with -- awards material for sure.
>>
>> Yes.
>>
>>>
>>>> The downside would be that some workloads might see worse performance
>>>> due to backend I/O handling might get preempted.
>>>
>>> Is that an actual concern? Mark this a legaxy inteface and anybody who
>>> wants to get away from it updates.
>>
>> It isn't that easy. See above.
>>
>>>
>>>> Just thinking - can full preemption be enabled per process?
>>>
>>> Nope, that's a system wide thing. Preemption is something that's driven
>>> by the requirements of the tasks that preempt, not something by the
>>> tasks that get preempted.
>>
>> Depends. If a task in a non-preempt system could switch itself to be
>> preemptable, we could do so around hypercalls without compromising the
>> general preemption setting. Disabling preemption in a preemptable system
>> should continue to be possible for short code paths only, of course.
>>
>>> Andy's idea of having that thing intercepted as an exception (EXTABLE
>>> like) and relocating the IP to a place that does cond_resched() before
>>> going back is an option.. gross, but possibly better, dunno.
>>>
>>> Quite the mess indeed :/
>>
>> Yeah.
>
> Having one implementation of interrupt handlers that schedule when they interrupt kernel code (the normal full preempt path) is one thing. Having two of them (full preempt and super-special-Xen) is IMO quite a bit worse. Especially since no one tests the latter very well.
>
> Having a horrible Xen-specific extable-like thingy seems honestly rather less bad. It could even have a little self-contained test that runs at boot, I bet.
>
> But I'll bite on the performance impact issue. What, exactly, is wrong with full preemption? Full preemption has two sources of overhead, I think. One is a bit of bookkeeping. The other is the overhead inherent in actually rescheduling -- context switch cost, losing things from cache, etc.
>
> The bookkeeping part should have quite low overhead. The scheduling part sounds like it might just need some scheduler tuning if it's really a problem.
>
> In any case, for backend IO, full preemption sounds like it should be a win, not a loss. If I'm asking dom0 to do backend IO for me, I don't want it delayed because dom0 was busy doing something else boring. IO is faster when the latency between requesting it and actually submitting it to hardware is lower.

Maybe. I was assuming that full preemption would result in more context
switches, especially in case many guests are hammering dom0 with I/Os.
This means that more time is spent with switching instead of doing real
work, resulting in dom0 being at 100% cpu faster with doing less work.

IMHO the reason is similar to the reason why servers tend to be run
without preemption (higher throughput at the expense of higher latency).
Full preemption is preferred for systems being used interactively, like
workstations and laptops, as here latency does matter, as long as the
system isn't limited by cpu power most of the time.

I'm pretty sure Xen installations like in QubesOS will prefer to run the
guests fully preemptive for that very reason.

> Can anyone actually demonstrate full preemption being a loss on a real Xen PV workload?

Should be doable, but I think above reasoning is pointing into the right
direction already.


Juergen


Attachments:
OpenPGP_0xB0DE9DD628BF132F.asc (3.08 kB)
OpenPGP public key
OpenPGP_signature (505.00 B)
OpenPGP digital signature
Download all attachments

2023-06-24 15:50:06

by Andy Lutomirski

[permalink] [raw]
Subject: Re: [PATCH] Updates to Xen hypercall preemption

On Thu, Jun 22, 2023, at 10:20 AM, Juergen Gross wrote:
> On 22.06.23 18:39, Andy Lutomirski wrote:
>> On Thu, Jun 22, 2023, at 3:33 AM, Juergen Gross wrote:
>>> On 22.06.23 10:26, Peter Zijlstra wrote:
>>>> On Thu, Jun 22, 2023 at 07:22:53AM +0200, Juergen Gross wrote:
>>>>
>>>>> The hypercalls we are talking of are synchronous ones. They are running
>>>>> in the context of the vcpu doing the call (like a syscall from userland is
>>>>> running in the process context).
>>>>
>>>> (so time actually passes from the guest's pov?)
>>>
>>> Correct.
>>>
>>>>
>>>>> The hypervisor will return to guest context from time to time by modifying
>>>>> the registers such that the guest will do the hypercall again with different
>>>>> input values for the hypervisor, resulting in a proper continuation of the
>>>>> hypercall processing.
>>>>
>>>> Eeeuw.. that's pretty terrible. And changing this isn't in the cards,
>>>> like at all?
>>>
>>> In the long run this should be possible, but not for already existing Xen
>>> versions.
>>>
>>>>
>>>> That is, why isn't this whole thing written like:
>>>>
>>>> for (;;) {
>>>> ret = hypercall(foo);
>>>> if (ret == -EAGAIN) {
>>>> cond_resched();
>>>> continue;
>>>> }
>>>> break;
>>>> }
>>>
>>> The hypervisor doesn't return -EAGAIN for hysterical reasons.
>>>
>>> This would be one of the options to change the interface. OTOH there are cases
>>> where already existing hypercalls need to be modified in the hypervisor to do
>>> preemption in the middle due to e.g. security reasons (avoiding cpu hogging in
>>> special cases).
>>>
>>> Additionally some of the hypercalls being subject to preemption are allowed in
>>> unprivileged guests, too. Those are mostly hypercalls allowed for PV guests
>>> only, but some are usable by all guests.
>>>
>>>>
>>>>> It is an awful interface and I agree that switching to full preemption in
>>>>> dom0 seems to be the route which we should try to take.
>>>>
>>>> Well, I would very strongly suggest the route to take is to scrap the
>>>> whole thing and invest in doing something saner so we don't have to jump
>>>> through hoops like this.
>>>>
>>>> This is quite possibly the worst possible interface for this Xen could
>>>> have come up with -- awards material for sure.
>>>
>>> Yes.
>>>
>>>>
>>>>> The downside would be that some workloads might see worse performance
>>>>> due to backend I/O handling might get preempted.
>>>>
>>>> Is that an actual concern? Mark this a legaxy inteface and anybody who
>>>> wants to get away from it updates.
>>>
>>> It isn't that easy. See above.
>>>
>>>>
>>>>> Just thinking - can full preemption be enabled per process?
>>>>
>>>> Nope, that's a system wide thing. Preemption is something that's driven
>>>> by the requirements of the tasks that preempt, not something by the
>>>> tasks that get preempted.
>>>
>>> Depends. If a task in a non-preempt system could switch itself to be
>>> preemptable, we could do so around hypercalls without compromising the
>>> general preemption setting. Disabling preemption in a preemptable system
>>> should continue to be possible for short code paths only, of course.
>>>
>>>> Andy's idea of having that thing intercepted as an exception (EXTABLE
>>>> like) and relocating the IP to a place that does cond_resched() before
>>>> going back is an option.. gross, but possibly better, dunno.
>>>>
>>>> Quite the mess indeed :/
>>>
>>> Yeah.
>>
>> Having one implementation of interrupt handlers that schedule when they interrupt kernel code (the normal full preempt path) is one thing. Having two of them (full preempt and super-special-Xen) is IMO quite a bit worse. Especially since no one tests the latter very well.
>>
>> Having a horrible Xen-specific extable-like thingy seems honestly rather less bad. It could even have a little self-contained test that runs at boot, I bet.
>>
>> But I'll bite on the performance impact issue. What, exactly, is wrong with full preemption? Full preemption has two sources of overhead, I think. One is a bit of bookkeeping. The other is the overhead inherent in actually rescheduling -- context switch cost, losing things from cache, etc.
>>
>> The bookkeeping part should have quite low overhead. The scheduling part sounds like it might just need some scheduler tuning if it's really a problem.
>>
>> In any case, for backend IO, full preemption sounds like it should be a win, not a loss. If I'm asking dom0 to do backend IO for me, I don't want it delayed because dom0 was busy doing something else boring. IO is faster when the latency between requesting it and actually submitting it to hardware is lower.
>
> Maybe. I was assuming that full preemption would result in more context
> switches, especially in case many guests are hammering dom0 with I/Os.
> This means that more time is spent with switching instead of doing real
> work, resulting in dom0 being at 100% cpu faster with doing less work.

It ought to just result in context switches happening a bit earlier when the scheduler decides it wants one. When a non-fully-preemptible kernel gets an interrupt and need_resched gets set, it will still schedule as soon as it hits a cond_resched() or a return to usermode or anything else that explicitly allows scheduling.

If you're hammering dom0 with IO and it's getting swamped by context switches, the problem is the code handling the IO (too many threads or something), not the preemption.