Scheduler preemption hooks, #2
cover Scheduler preemption hooks, #2
I redid the the scheduler preemption patches based on Rusty's feedback. Things do look much cleaner this time around.
Changes:
- split into core and kvm parts
- incorporated Shaohua Li's fix
- each task now has a list of independently managed preemption hooks
- move preempt hook pointer to a hot cache line within task_struct
I haven't made kernel_fpu_begin()/kernel_fpu_end() preemptible as promised, because (a) we need to find a place to put the fpu state, and (b) we should get better performance by reusing cr0.ts anyway.
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
PATCH 1/2 SCHED: Generic hooks for trapping task preemption
This adds a general mechanism whereby a task can request the scheduler to notify it whenever it is preempted or scheduled back in. This allows the task to swap any special-purpose registers like the fpu or Intel's VT registers.
--- include/linux/preempt.h | 27 +++++++++++++++++++++++ include/linux/sched.h | 4 +++ kernel/Kconfig.preempt | 4 +++ kernel/sched.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 0 deletions(-)
include/linux/preempt.h +27 -0
@@ -8,6 +8,7 @@#include <linux/thread_info.h> #include <linux/linkage.h> +#include <linux/list.h> #ifdef CONFIG_DEBUG_PREEMPT extern void fastcall add_preempt_count(int val);
@@ -60,4 +61,30 @@ do { \#endif +#ifdef CONFIG_PREEMPT_HOOKS + +struct preempt_hook ; + +struct preempt_ops { +void (*sched_in)(struct preempt_hook *hook,int cpu); +void (*sched_out)(struct preempt_hook *hook); +}; + +struct preempt_hook { +struct hlist_node link; +struct preempt_ops *ops; +}; + +void preempt_hook_register (struct preempt_hook *hook); +void preempt_hook_unregister (struct preempt_hook *hook); + +static inline void preempt_hook_init (struct preempt_hook *hook, +struct preempt_ops *ops) +{ +INIT_HLIST_NODE (&hook->link); + hook->ops = ops; +} + +#endif +#endif /* __LINUX_PREEMPT_H */
include/linux/sched.h +4 -0
@@ -908,6 +908,10 @@ struct task_struct {struct sched_class *sched_class;struct sched_entity se; +#ifdef CONFIG_PREEMPT_HOOKS +struct hlist_head preempt_hooks;/* list of struct preempt_hook */ +#endif +unsigned short ioprio;#ifdef CONFIG_BLK_DEV_IO_TRACE unsigned int btrace_seq;
kernel/Kconfig.preempt +4 -0
@@ -63,3 +63,7 @@ config PREEMPT_BKL Say Y here if you are building a kernel for a desktop system. Say N if you are unsure. +config PREEMPT_HOOKS + bool + depends on X86 + default y
kernel/sched.c +54 -0
84 lines not on this page · raw message
Signature · 8 lines
1.5.2.3 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
Re: [PATCH 1/2] SCHED: Generic hooks for trapping task preemption
kernel/Kconfig.preempt config PREEMPT_BKL · line 69
Quoted 4 lines · Avi Kivity
kernel/Kconfig.preempt +4
-0+config PREEMPT_HOOKS + bool + depends on X86 + default y
Hmm, I would have thought that having CONFIG_KVM "select PREEMPT_HOOKS" would be a little clearer.
kernel/sched.c void fastcall wake_up_new_task(struct task_struct *p, unsigned long clone_flags) · line 1675
Quoted 8 lines
kernel/sched.c +8
-0+static void fire_sched_in_preempt_hooks (struct task_struct *tsk) +{ +struct preempt_hook *hook; +struct hlist_node *node; + +hlist_for_each_entry (hook, node, &tsk->preempt_hooks, link) + hook->ops->sched_in (hook,raw_smp_processor_id ()); +}
The old current/tsk conflation. You hand in "tsk" here, but since that's not handed through to the sched_in, it must be "current". It's just unfortunate that this generates better code...
But patch looks good.
Cheers,
Rusty.
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
Re: [PATCH 1/2] SCHED: Generic hooks for trapping task preemption
In reply to Avi Kivitykernel/sched.c void fastcall wake_up_new_task(struct task_struct *p, unsigned long clone_flags) · line 1714
Quoted 3 lines · Avi Kivity
kernel/sched.c +1
-0+fire_sched_out_preempt_hooks (current);prepare_lock_switch (rq, next);prepare_arch_switch (next);
Damn, I just found a use for this in lguest.
Any chance of handing "next" to the sched_out hook so we can optimize the lguest <-> lguest switch?
FYI, lguest turns off PGE globally when you load the module, because turning it off on each guest entry v. slow. With these hooks I can be far smarter...
Thanks!
Rusty.
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
Re: [PATCH 1/2] SCHED: Generic hooks for trapping task preemption
Quoted 9 lines · Rusty Russell
kernel/sched.c void fastcall wake_up_new_task(struct task_struct *p, unsigned long clone_flags) · line 1714
Quoted 4 lines · Avi Kivity
kernel/sched.c +1
-0+fire_sched_out_preempt_hooks (current);prepare_lock_switch (rq, next);prepare_arch_switch (next);Damn, I just found a use for this in lguest.
Quoted 7 lines
Any chance of handing "next" to the
sched_outhook so we can optimize the lguest <-> lguest switch?FYI, lguest turns off PGE globally when you load the module, because turning it off on each guest entry v. slow. With these hooks I can be far smarter...
Sure thang. Will be there in the next version.
Signature · 8 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic. ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 2/2 KVM: Use the scheduler preemption hooks to make kvm preemptible
In reply to Avi KivityCurrent kvm disables preemption while the new virtualization registers are in use. This of course is not very good for latency sensitive workloads (one use of virtualization is to offload user interface and other latency insensitive stuff to a container, so that it is easier to analyze the remaining workload). This patch re-enables preemption for kvm; preemption is now only disabled when switching the registers in and out, and during the switch to guest mode and back.
Contains fixes from Shaohua Li <[email protected]>.
--- drivers/kvm/Kconfig | 2 +- drivers/kvm/kvm.h | 4 ++- drivers/kvm/kvm_main.c | 52 ++++++++++++++++++++++++++++++++++++++++------- drivers/kvm/mmu.c | 2 - drivers/kvm/svm.c | 6 +--- drivers/kvm/vmx.c | 17 ++++++++------- 6 files changed, 59 insertions(+), 24 deletions(-)
drivers/kvm/Kconfig +1 -1
@@ -10,7 +10,7 @@ if VIRTUALIZATION config KVM tristate "Kernel-based Virtual Machine (KVM) support" - depends on X86 && EXPERIMENTAL + depends on X86 && EXPERIMENTAL && PREEMPT_HOOKS depends on X86_CMPXCHG64 || 64BIT ---help--- Support hosting fully virtualized guest machines using hardware
drivers/kvm/kvm.h +3 -1
@@ -13,6 +13,7 @@#include <linux/signal.h> #include <linux/sched.h> #include <linux/mm.h> +#include <linux/preempt.h> #include <asm/signal.h> #include "vmx.h"
@@ -328,6 +329,7 @@ void kvm_io_bus_register_dev(struct kvm_io_bus *bus,struct kvm_vcpu {struct kvm *kvm; +struct preempt_hook preempt_hook;union {struct vmcs *vmcs;struct vcpu_svm *svm;
@@ -474,7 +476,7 @@ struct kvm_arch_ops {int (*vcpu_create)(struct kvm_vcpu *vcpu);void (*vcpu_free)(struct kvm_vcpu *vcpu); -void (*vcpu_load)(struct kvm_vcpu *vcpu); +void (*vcpu_load)(struct kvm_vcpu *vcpu,int cpu);void (*vcpu_put)(struct kvm_vcpu *vcpu);void (*vcpu_decache)(struct kvm_vcpu *vcpu);
drivers/kvm/kvm_main.c +44 -8
129 lines not on this page · raw message
drivers/kvm/mmu.c +0 -2
@@ -254,9 +254,7 @@ static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu) r =__mmu_topup_memory_caches (vcpu,GFP_NOWAIT );if (r <0 ) {spin_unlock (&vcpu->kvm->lock); - kvm_arch_ops->vcpu_put (vcpu); r =__mmu_topup_memory_caches (vcpu,GFP_KERNEL ); - kvm_arch_ops->vcpu_load (vcpu);spin_lock (&vcpu->kvm->lock); }return r;
drivers/kvm/svm.c +2 -4
@@ -611,11 +611,10 @@ static void svm_free_vcpu(struct kvm_vcpu *vcpu)kfree (vcpu->svm); } -static void svm_vcpu_load (struct kvm_vcpu *vcpu) +static void svm_vcpu_load (struct kvm_vcpu *vcpu,int cpu) { -int cpu, i; +int i; - cpu =get_cpu ();if (unlikely (cpu != vcpu->cpu)) {u64 tsc_this, delta;
@@ -641,7 +640,6 @@ static void svm_vcpu_put(struct kvm_vcpu *vcpu)wrmsrl (host_save_user_msrs[i], vcpu->svm->host_user_msrs[i]);rdtscll (vcpu->host_tsc); -put_cpu (); }static void svm_vcpu_decache (struct kvm_vcpu *vcpu)
drivers/kvm/vmx.c +9 -8
@@ -345,6 +345,7 @@ static void vmx_save_host_state(struct kvm_vcpu *vcpu)static void vmx_load_host_state (struct kvm_vcpu *vcpu) {struct vmx_host_state *hs = &vcpu->vmx_host_state; +unsigned long flags;if (!hs->loaded)return ;
@@ -357,12 +358,12 @@ static void vmx_load_host_state(struct kvm_vcpu *vcpu)* If we have to reload gs, we must take care to * preserve our gs base. */ -local_irq_disable (); +local_irq_save (flags);load_gs (hs->gs_sel);#ifdef CONFIG_X86_64 wrmsrl (MSR_GS_BASE ,vmcs_readl (HOST_GS_BASE ));#endif -local_irq_enable (); +local_irq_restore (flags);reload_tss (); }
@@ -376,14 +377,11 @@ static void vmx_load_host_state(struct kvm_vcpu *vcpu)* Switches to specified vcpu, until a matching vcpu_put(), but assumes * vcpu mutex is already taken. */ -static void vmx_vcpu_load (struct kvm_vcpu *vcpu) +static void vmx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {u64 phys_addr =__pa (vcpu->vmcs); -int cpu;u64 tsc_this, delta; - cpu =get_cpu (); -if (vcpu->cpu != cpu)vcpu_clear (vcpu);
@@ -428,7 +426,6 @@ static void vmx_vcpu_put(struct kvm_vcpu *vcpu) {vmx_load_host_state (vcpu);kvm_put_guest_fpu (vcpu); -put_cpu (); }static void vmx_fpu_activate (struct kvm_vcpu *vcpu)
@@ -2000,6 +1997,8 @@ preempted:kvm_guest_debug_pre (vcpu); again: +preempt_disable (); +if (!vcpu->mmio_read_completed)do_interrupt_requests (vcpu, kvm_run);
@@ -2146,6 +2145,9 @@ again: vcpu->interrupt_window_open = (vmcs_read32 (GUEST_INTERRUPTIBILITY_INFO ) &3 ) ==0 ; asm ("mov %0, %%ds; mov %0, %%es" : :"r" (__USER_DS)); + vcpu->launched =1 ; + +preempt_enable ();if (unlikely (fail)) { kvm_run->exit_reason =KVM_EXIT_FAIL_ENTRY ;
@@ -2160,7 +2162,6 @@ again:if (unlikely (prof_on ==KVM_PROFILING ))profile_hit (KVM_PROFILING , (void *)vmcs_readl (GUEST_RIP )); - vcpu->launched =1 ; r =kvm_handle_exit (kvm_run, vcpu);if (r >0 ) {/* Give scheduler a change to reschedule. */
Signature · 8 lines
1.5.2.3 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
Re: [kvm-devel] [PATCH 2/2] KVM: Use the scheduler preemption hooks to make kvm preemptible
Quoted 7 lines · Avi Kivity
Current kvm disables preemption while the new virtualization registers are in use. This of course is not very good for latency sensitive workloads (one use of virtualization is to offload user interface and other latency insensitive stuff to a container, so that it is easier to analyze the remaining workload). This patch re-enables preemption for kvm; preemption is now only disabled when switching the registers in and out, and during the switch to guest mode and back.
[...]
drivers/kvm/Kconfig if VIRTUALIZATION · line 14
Quoted 11 lines
drivers/kvm/Kconfig +1
-1@@ -10,7 +10,7 @@ if VIRTUALIZATION config KVM tristate "Kernel-based Virtual Machine (KVM) support" - depends on X86 && EXPERIMENTAL + depends on X86 && EXPERIMENTAL && PREEMPT_HOOKS depends on X86_CMPXCHG64 || 64BIT
select PREEMPT_HOOKS maybe?
Luca
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
Re: [kvm-devel] [PATCH 2/2] KVM: Use the scheduler preemption hooks to make kvm preemptible
Quoted 18 lines · Luca
Quoted 10 lines · Avi Kivity
Current kvm disables preemption while the new virtualization registers are
in use. This of course is not very good for latency sensitive workloads (one
use of virtualization is to offload user interface and other latency insensitive stuff to a container, so that it is easier to analyze the remaining workload). This patch re-enables preemption for kvm; preemption
is now only disabled when switching the registers in and out, and during the switch to guest mode and back.[...]
Quoted 6 lines
drivers/kvm/Kconfig +0
-0@@ -10,7 +10,7 @@ if VIRTUALIZATION
Quoted 8 lines
drivers/kvm/Kconfig if VIRTUALIZATION · line 14
Quoted 5 lines
drivers/kvm/Kconfig +1
-1config KVM tristate "Kernel-based Virtual Machine (KVM) support" - depends on X86 && EXPERIMENTAL + depends on X86 && EXPERIMENTAL && PREEMPT_HOOKS depends on X86_CMPXCHG64 || 64BITselect
PREEMPT_HOOKSmaybe?
Yes. And default PREEMPT_HOOKS to n in the first patch. I'll make both these changes.
Signature · 8 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic. ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/