xen/sched: rework and rename vcpu_force_reschedule()
PATCH v2 1/1 xen/sched: rework and rename vcpu_force_reschedule()
vcpu_force_reschedule() is only used for modifying the periodic timer of a vcpu. Forcing a vcpu to give up the physical cpu for that purpose is kind of brutal.
So instead of doing the reschedule dance just operate on the timer directly. By protecting periodic timer modifications against concurrent timer activation via a per-vcpu lock it is even no longer required to bother the target vcpu at all for updating its timer.
Rename the function to vcpu_set_periodic_timer() as this now reflects the functionality.
---
- Carved out from my core scheduling series
- Reworked to avoid deadlock when 2 vcpus are trying to modify each others periodic timers, leading to address all comments by Jan Beulich.
V2:
- test periodic_period again in vcpu_periodic_timer_work() when lock obtained (Jan Beulich)
--- xen/arch/x86/pv/shim.c | 4 +--- xen/common/domain.c | 6 ++---- xen/common/schedule.c | 53 ++++++++++++++++++++++++++++--------------------- xen/include/xen/sched.h | 3 ++- 4 files changed, 35 insertions(+), 31 deletions(-)
xen/arch/x86/pv/shim.c +1 -3
@@ -410,7 +410,7 @@ int pv_shim_shutdown(uint8_t reason)unmap_vcpu_info (v);/* Reset the periodic timer to the default value. */ - v->periodic_period =MILLISECS (10 ); +vcpu_set_periodic_timer (v,MILLISECS (10 ));/* Stop the singleshot timer. */ stop_timer (&v->singleshot_timer);
@@ -419,8 +419,6 @@ int pv_shim_shutdown(uint8_t reason)if ( v != current )vcpu_unpause_by_systemcontroller (v); -else -vcpu_force_reschedule (v); }return 0 ;
xen/common/domain.c +2 -4
@@ -1494,15 +1494,13 @@ long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg)if ( set.period_ns >STIME_DELTA_MAX )return -EINVAL ; - v->periodic_period = set.period_ns; -vcpu_force_reschedule (v); +vcpu_set_periodic_timer (v, set.period_ns);break ; }case VCPUOP_stop_periodic_timer: - v->periodic_period =0 ; -vcpu_force_reschedule (v); +vcpu_set_periodic_timer (v,0 );break ;case VCPUOP_set_singleshot_timer:
xen/common/schedule.c +30 -23
84 lines not on this page · raw message
xen/include/xen/sched.h +2 -1
@@ -153,6 +153,7 @@ struct vcpustruct vcpu *next_in_list; +spinlock_t periodic_timer_lock;s_time_t periodic_period;s_time_t periodic_last_event;struct timer periodic_timer;
@@ -864,7 +865,7 @@ struct scheduler *scheduler_get_default(void);struct scheduler *scheduler_alloc (unsigned int sched_id,int *perr);void scheduler_free (struct scheduler *sched);int schedule_cpu_switch (unsigned int cpu,struct cpupool *c); -void vcpu_force_reschedule (struct vcpu *v); +void vcpu_set_periodic_timer (struct vcpu *v,s_time_t value);int cpu_disable_scheduler (unsigned int cpu);/* We need it in dom0_setup_vcpu */ void sched_set_affinity(struct vcpu *v, const cpumask_t *hard,
Signature · 7 lines
2.16.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 9 lines · Juergen Gross
vcpu_force_reschedule()is only used for modifying the periodic timer of a vcpu. Forcing a vcpu to give up the physical cpu for that purpose is kind of brutal.So instead of doing the reschedule dance just operate on the timer directly. By protecting periodic timer modifications against concurrent timer activation via a per-vcpu lock it is even no longer required to bother the target vcpu at all for updating its timer.
Quoted 4 lines
Rename the function to
vcpu_set_periodic_timer()as this now reflects the functionality.Signed-off-by Juergen Gross <[email protected]>
I continue to be unhappy about there being no word at all about ...
xen/common/schedule.c static void vcpu_migrate_finish(struct vcpu *v) · line 733
Quoted 11 lines
xen/common/schedule.c +0
-7@@ -724,24 +725,6 @@ static void vcpu_migrate_finish(struct vcpu *v)vcpu_wake (v); } -/* -* Force a VCPU through a deschedule/reschedule path. -* For example, using this when setting the periodic timer period means that -* most periodic-timer state need only be touched from within the scheduler -* which can thus be done without need for synchronisation. -*/ -void vcpu_force_reschedule (struct vcpu *v)
... the originally intended synchronization-free handling. Forcing the vCPU through the scheduler may seem harsh (and quite some overhead), yes, but I don't think the above was written (and decided) without consideration. One effect of this can be seen by you ...
xen/common/schedule.c static void vcpu_periodic_timer_work(struct vcpu *v) · line 1481
Quoted 5 lines
xen/common/schedule.c +5
-0+void vcpu_set_periodic_timer (struct vcpu *v,s_time_t value) +{ +spin_lock (&v->periodic_timer_lock); + +stop_timer (&v->periodic_timer);
... introducing a new stop_timer() here, i.e. which doesn't replace any existing one. The implication is that other than before the periodic timer may now not run (for a brief moment) despite it being supposed to run - after all it has been active so far whenever a vCPU was running.
Then again, looking at the involved code paths yet again, I wonder whether this has been working right at all: There's an early exit from schedule() when prev == next, which bypasses vcpu_periodic_timer_work(). And I can't seem to be able to spot anything on the vcpu_force_reschedule() path which would guarantee this shortcut to not be taken.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 47 lines · Jan Beulich
Quoted 9 lines · Juergen Gross
vcpu_force_reschedule()is only used for modifying the periodic timer of a vcpu. Forcing a vcpu to give up the physical cpu for that purpose is kind of brutal.So instead of doing the reschedule dance just operate on the timer directly. By protecting periodic timer modifications against concurrent timer activation via a per-vcpu lock it is even no longer required to bother the target vcpu at all for updating its timer.
Quoted 4 lines
Rename the function to
vcpu_set_periodic_timer()as this now reflects the functionality.Signed-off-by Juergen Gross <[email protected]>I continue to be unhappy about there being no word at all about ...
xen/common/schedule.c static void vcpu_migrate_finish(struct vcpu *v) · line 733
Quoted 11 lines
xen/common/schedule.c +0
-7@@ -724,24 +725,6 @@ static void vcpu_migrate_finish(struct vcpu *v)vcpu_wake (v); } -/* -* Force a VCPU through a deschedule/reschedule path. -* For example, using this when setting the periodic timer period means that -* most periodic-timer state need only be touched from within the scheduler -* which can thus be done without need for synchronisation. -*/ -void vcpu_force_reschedule (struct vcpu *v)... the originally intended synchronization-free handling. Forcing the vCPU through the scheduler may seem harsh (and quite some overhead), yes, but I don't think the above was written (and decided) without consideration. One effect of this can be seen by you ...
xen/common/schedule.c static void vcpu_periodic_timer_work(struct vcpu *v) · line 1481
Quoted 5 lines
xen/common/schedule.c +5
-0+void vcpu_set_periodic_timer (struct vcpu *v,s_time_t value) +{ +spin_lock (&v->periodic_timer_lock); + +stop_timer (&v->periodic_timer);... introducing a new
stop_timer()here, i.e. which doesn't replace any existing one. The implication is that other than before the periodic timer may now not run (for a brief moment) despite it being supposed to run - after all it has been active so far whenever a vCPU was running.
Quoted 6 lines
Then again, looking at the involved code paths yet again, I wonder whether this has been working right at all: There's an early exit from
schedule()when prev == next, which bypassesvcpu_periodic_timer_work(). And I can't seem to be able to spot anything on thevcpu_force_reschedule()path which would guarantee this shortcut to not be taken.
First, the current "synchronization-free" handling is not existing. The synchronization is just hidden in the calls of vcpu_migrate_*() and it is done via the scheduler lock.
Yes, I'm adding a stop_timer(), but the related stop_timer() call in the old code was in schedule(). So statically you are right, but dynamically there is no new stop_timer() call involved.
And last: the case prev == next would not occur today, as the migrate flag being set in vcpu->pause_flags would cause the vcpu to be taken away from the cpu. So it is working today, but setting the periodic timer requires two scheduling events in case the target vcpu is currently running.
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 55 lines · Juergen Gross
Quoted 47 lines · Jan Beulich
Quoted 9 lines · Juergen Gross
vcpu_force_reschedule()is only used for modifying the periodic timer of a vcpu. Forcing a vcpu to give up the physical cpu for that purpose is kind of brutal.So instead of doing the reschedule dance just operate on the timer directly. By protecting periodic timer modifications against concurrent timer activation via a per-vcpu lock it is even no longer required to bother the target vcpu at all for updating its timer.
Quoted 4 lines
Rename the function to
vcpu_set_periodic_timer()as this now reflects the functionality.Signed-off-by Juergen Gross <[email protected]>I continue to be unhappy about there being no word at all about ...
xen/common/schedule.c static void vcpu_migrate_finish(struct vcpu *v) · line 733
Quoted 11 lines
xen/common/schedule.c +0
-7@@ -724,24 +725,6 @@ static void vcpu_migrate_finish(struct vcpu *v)vcpu_wake (v); } -/* -* Force a VCPU through a deschedule/reschedule path. -* For example, using this when setting the periodic timer period means that -* most periodic-timer state need only be touched from within the scheduler -* which can thus be done without need for synchronisation. -*/ -void vcpu_force_reschedule (struct vcpu *v)... the originally intended synchronization-free handling. Forcing the vCPU through the scheduler may seem harsh (and quite some overhead), yes, but I don't think the above was written (and decided) without consideration. One effect of this can be seen by you ...
xen/common/schedule.c static void vcpu_periodic_timer_work(struct vcpu *v) · line 1481
Quoted 5 lines
xen/common/schedule.c +5
-0+void vcpu_set_periodic_timer (struct vcpu *v,s_time_t value) +{ +spin_lock (&v->periodic_timer_lock); + +stop_timer (&v->periodic_timer);... introducing a new
stop_timer()here, i.e. which doesn't replace any existing one. The implication is that other than before the periodic timer may now not run (for a brief moment) despite it being supposed to run - after all it has been active so far whenever a vCPU was running.Quoted 6 lines
Then again, looking at the involved code paths yet again, I wonder whether this has been working right at all: There's an early exit from
schedule()when prev == next, which bypassesvcpu_periodic_timer_work(). And I can't seem to be able to spot anything on thevcpu_force_reschedule()path which would guarantee this shortcut to not be taken.
Quoted 3 lines
First, the current "synchronization-free" handling is not existing. The synchronization is just hidden in the calls of vcpu_migrate_*() and it is done via the scheduler lock.
Sure, but the scheduler lock needs to be taken during scheduling of the vCPU anyway. There was no "extra" synchronization involved.
Quoted 3 lines
Yes, I'm adding a
stop_timer(), but the relatedstop_timer()call in the old code was inschedule(). So statically you are right, but dynamically there is no newstop_timer()call involved.
I did specifically check that my comment is not just about the "static" part (as you call it). As said - there was no stop_timer() before behind a running vCPU's back. This is what worries me.
Quoted 5 lines
And last: the case prev == next would not occur today, as the migrate flag being set in
vcpu->pause_flagswould cause the vcpu to be taken away from the cpu. So it is working today, but setting the periodic timer requires two scheduling events in case the target vcpu is currently running.
I'm not going to claim I fully understood the code when looking at it in the morning, but I couldn't find the place(s) guaranteeing that by the time the migration of the vCPU is over it wouldn't be runnable again right away, and hence potentially re-chosen as the vCPU to run on the pCPU is was running on before.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 56 lines · Jan Beulich
Quoted 55 lines · Juergen Gross
Quoted 53 lines · Jan Beulich
Quoted 9 lines · Juergen Gross
vcpu_force_reschedule()is only used for modifying the periodic timer of a vcpu. Forcing a vcpu to give up the physical cpu for that purpose is kind of brutal.So instead of doing the reschedule dance just operate on the timer directly. By protecting periodic timer modifications against concurrent timer activation via a per-vcpu lock it is even no longer required to bother the target vcpu at all for updating its timer.
Quoted 4 lines
Rename the function to
vcpu_set_periodic_timer()as this now reflects the functionality.Signed-off-by Juergen Gross <[email protected]>I continue to be unhappy about there being no word at all about ...
xen/common/schedule.c static void vcpu_migrate_finish(struct vcpu *v) · line 733
Quoted 11 lines
xen/common/schedule.c +0
-7@@ -724,24 +725,6 @@ static void vcpu_migrate_finish(struct vcpu *v)vcpu_wake (v); } -/* -* Force a VCPU through a deschedule/reschedule path. -* For example, using this when setting the periodic timer period means that -* most periodic-timer state need only be touched from within the scheduler -* which can thus be done without need for synchronisation. -*/ -void vcpu_force_reschedule (struct vcpu *v)... the originally intended synchronization-free handling. Forcing the vCPU through the scheduler may seem harsh (and quite some overhead), yes, but I don't think the above was written (and decided) without consideration. One effect of this can be seen by you ...
xen/common/schedule.c static void vcpu_periodic_timer_work(struct vcpu *v) · line 1481
Quoted 5 lines
xen/common/schedule.c +5
-0+void vcpu_set_periodic_timer (struct vcpu *v,s_time_t value) +{ +spin_lock (&v->periodic_timer_lock); + +stop_timer (&v->periodic_timer);... introducing a new
stop_timer()here, i.e. which doesn't replace any existing one. The implication is that other than before the periodic timer may now not run (for a brief moment) despite it being supposed to run - after all it has been active so far whenever a vCPU was running.Then again, looking at the involved code paths yet again, I wonder whether this has been working right at all: There's an early exit from
schedule()when prev == next, which bypassesvcpu_periodic_timer_work(). And I can't seem to be able to spot anything on thevcpu_force_reschedule()path which would guarantee this shortcut to not be taken.
Quoted 6 lines
Quoted 3 lines
First, the current "synchronization-free" handling is not existing. The synchronization is just hidden in the calls of vcpu_migrate_*() and it is done via the scheduler lock.
Sure, but the scheduler lock needs to be taken during scheduling of the vCPU anyway. There was no "extra" synchronization involved.
Of course there was. The scheduling path was forced to happen, this resulted in the extra synchronization (twice in fact, once for de-scheduling and once for scheduling in again).
Quoted 8 lines
Quoted 4 lines
Yes, I'm adding a
stop_timer(), but the relatedstop_timer()call in the old code was inschedule(). So statically you are right, but dynamically there is no newstop_timer()call involved.I did specifically check that my comment is not just about the "static" part (as you call it). As said - there was no
stop_timer()before behind a running vCPU's back. This is what worries me.
There is a stop_timer() for the periodic timer happening each time the vcpu is de-scheduled (look into schedule()). And as setting the periodic timer today results in a de-scheduling of the vcpu the stop_timer() is happening. I assume here that the de-scheduling would not have happened without setting the timer, of course. In case the vcpu is not running when the timer is being set, my patch will call stop_timer() for a timer already being stopped.
Quoted 7 lines
Quoted 6 lines
And last: the case prev == next would not occur today, as the migrate flag being set in
vcpu->pause_flagswould cause the vcpu to be taken away from the cpu. So it is working today, but setting the periodic timer requires two scheduling events in case the target vcpu is currently running.
Quoted 5 lines
I'm not going to claim I fully understood the code when looking at it in the morning, but I couldn't find the place(s) guaranteeing that by the time the migration of the vCPU is over it wouldn't be runnable again right away, and hence potentially re-chosen as the vCPU to run on the pCPU is was running on before.
vcpu_migrate_start() is setting _VPF_migrating in the vcpu's pause_flags and then initiates a scheduling event via vcpu_sleep_nosync(). vcpu_migrate_finish() will only reset the flag if the vcpu is not running (so either it wasn't running before, or the scheduling event already has happened). If it is still running it will not reset the flag, but this will only be done via context_saved(), which is called when the vcpus has been de-scheduled. The vcpu can only be selected to be running again when the migrated flag is not set.
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 5 lines · Juergen Gross
vcpu_force_reschedule()is only used for modifying the periodic timer of a vcpu. Forcing a vcpu to give up the physical cpu for that purpose
is kind of brutal.
Quoted 9 lines
So instead of doing the reschedule dance just operate on the timer directly. By protecting periodic timer modifications against concurrent
timer activation via a per-vcpu lock it is even no longer required to bother the target vcpu at all for updating its timer.Rename the function to
vcpu_set_periodic_timer()as this now reflects the functionality.
Personally, I'm rather happy to see the code which was doing that very weird "let's go through rescheduling" dance going away. I, FWIW, never understood why periodic timer handling was implemented that way (and looking back at relevant changelogs does not help).
The code, as it results after applying this patch, is a lot better, and easier to understand.
Performance and scalability wise, I don't have benchmarks for this specific patch (but the ones I did included it, as it back then was part of the core-scheduling series), but I agree with Juergen. I.e., I think the patch is either neutral or, if it does something, it improves things.
Furthermore, periodic timer is *not* used any longer (and since quite some time/kernel versions). Basically, all we do with the periodic timer is to disable it during boot. At least for Linux, but I think this is the case for FreeBSD too. So, even if the patch would have a negative impact (which again I don't think it's the case), we probably won't see them.
On this grounds (and, of course, on the one that I've looked at the code, and think it's correct), for the scheduling part:
Quoted 2 lines
Signed-off-by Juergen Gross <[email protected]>
Then, if some words about the outcome of the discussion in this thread, e.g., a mention to the fact that the old code wasn't really lockless, and that the new code is a lot more straightforward, it'd be even better.
But my Rev-by stands, with or without this.
Regards
Signature · 6 lines
Dario Faggioli, Ph.D http://about.me/dario.faggioli Virtualization Software Engineer SUSE Labs, SUSE https://www.suse.com/ ------------------------------------------------------------------- <<This happens because _I_ choose it to happen!>> (Raistlin Majere)
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel