xen: do live patching only from main idle loop
PATCH 1/1 xen: do live patching only from main idle loop
One of the main design goals of core scheduling is to avoid actions which are not directly related to the domain currently running on a given cpu or core. Live patching is one of those actions which are allowed taking place on a cpu only when the idle scheduling unit is active on that cpu.
Unfortunately live patching tries to force the cpus into the idle loop just by raising the schedule softirq, which will no longer be guaranteed to work with core scheduling active. Additionally there are still some places in the hypervisor calling check_for_livepatch_work() without being in the idle loop.
It is easy to force a cpu into the main idle loop by scheduling a tasklet on it. So switch live patching to use tasklets for switching to idle and raising scheduling events. Additionally the calls of check_for_livepatch_work() outside the main idle loop can be dropped.
As tasklets are only running on idle vcpus and stop_machine_run() is activating tasklets on all cpus but the one it has been called on to rendezvous, it is mandatory for stop_machine_run() to be called on an idle vcpu, too, as otherwise there is no way for scheduling to activate the idle vcpu for the tasklet on the sibling of the cpu stop_machine_run() has been called on.
--- xen/arch/arm/domain.c | 9 ++++----- xen/arch/arm/traps.c | 6 ------ xen/arch/x86/domain.c | 9 ++++----- xen/arch/x86/hvm/svm/svm.c | 2 +- xen/arch/x86/hvm/vmx/vmcs.c | 2 +- xen/arch/x86/pv/domain.c | 2 +- xen/arch/x86/setup.c | 2 +- xen/common/livepatch.c | 39 ++++++++++++++++++++++++++++++++++----- 8 files changed, 46 insertions(+), 25 deletions(-)
xen/arch/arm/domain.c +4 -5
@@ -72,7 +72,11 @@ void idle_loop(void)/* Are we here for running vcpu context tasklets, or for idling? */ if (unlikely (tasklet_work_to_do (cpu)) ) + {do_tasklet (); +/* Livepatch work is always kicked off via a tasklet. */ +check_for_livepatch_work (); + }/* * Test softirqs twice --- first to see if should even try scrubbing * and then, after it is done, whether softirqs became pending
@@ -83,11 +87,6 @@ void idle_loop(void)do_idle ();do_softirq (); -/* -* We MUST be last (or before dsb, wfi). Otherwise after we get the -* softirq we would execute dsb,wfi (and sleep) and not patch. -*/ -check_for_livepatch_work (); } }
xen/arch/arm/traps.c +0 -6
@@ -23,7 +23,6 @@#include <xen/iocap.h> #include <xen/irq.h> #include <xen/lib.h> -#include <xen/livepatch.h> #include <xen/mem_access.h> #include <xen/mm.h> #include <xen/param.h>
@@ -2239,11 +2238,6 @@ static void check_for_pcpu_work(void) {local_irq_enable ();do_softirq (); -/* -* Must be the last one - as the IPI will trigger us to come here -* and we want to patch the hypervisor with almost no stack. -*/ -check_for_livepatch_work ();local_irq_disable (); } }
xen/arch/x86/domain.c +4 -5
@@ -141,7 +141,11 @@ static void idle_loop(void)/* Are we here for running vcpu context tasklets, or for idling? */ if (unlikely (tasklet_work_to_do (cpu)) ) + {do_tasklet (); +/* Livepatch work is always kicked off via a tasklet. */ +check_for_livepatch_work (); + }/* * Test softirqs twice --- first to see if should even try scrubbing * and then, after it is done, whether softirqs became pending
@@ -151,11 +155,6 @@ static void idle_loop(void) !softirq_pending(cpu) )pm_idle ();do_softirq (); -/* -* We MUST be last (or before pm_idle). Otherwise after we get the -* softirq we would execute pm_idle (and sleep) and not patch. -*/ -check_for_livepatch_work (); } }
xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -1032,7 +1032,7 @@ static void noreturn svm_do_resume(struct vcpu *v)hvm_do_resume (v); -reset_stack_and_jump (svm_asm_do_resume); +reset_stack_and_jump_nolp (svm_asm_do_resume); }void svm_vmenter_helper (const struct cpu_user_regs *regs)
xen/arch/x86/hvm/vmx/vmcs.c +1 -1
@@ -1890,7 +1890,7 @@ void vmx_do_resume(struct vcpu *v)if ( host_cr4 !=read_cr4 () )__vmwrite (HOST_CR4 ,read_cr4 ()); -reset_stack_and_jump (vmx_asm_do_vmentry); +reset_stack_and_jump_nolp (vmx_asm_do_vmentry); }static inline unsigned long vmr (unsigned long field)
xen/arch/x86/pv/domain.c +1 -1
@@ -62,7 +62,7 @@ custom_runtime_param("pcid", parse_pcid);static void noreturncontinue_nonidle_domain (struct vcpu *v) {check_wakeup_from_wait (); -reset_stack_and_jump (ret_from_intr); +reset_stack_and_jump_nolp (ret_from_intr); }static int setup_compat_l4 (struct vcpu *v)
xen/arch/x86/setup.c +1 -1
@@ -632,7 +632,7 @@ static void __init noreturn reinit_bsp_stack(void) stack_base[0 ] = stack;memguard_guard_stack (stack); -reset_stack_and_jump (init_done); +reset_stack_and_jump_nolp (init_done); }/*
xen/common/livepatch.c +34 -5
@@ -17,6 +17,7 @@#include <xen/spinlock.h> #include <xen/string.h> #include <xen/symbols.h> +#include <xen/tasklet.h> #include <xen/version.h> #include <xen/virtual_region.h> #include <xen/vmap.h>
@@ -69,6 +70,7 @@ static struct livepatch_work livepatch_work;* Having an per-cpu lessens the load. */ static DEFINE_PER_CPU(bool_t, work_to_do); +static DEFINE_PER_CPU(struct tasklet, livepatch_tasklet);static int get_name (const struct xen_livepatch_name *name,char *n) {
@@ -1582,17 +1584,16 @@ static int schedule_work(struct payload *data, uint32_t cmd, uint32_t timeout)smp_wmb (); livepatch_work.do_work =1 ; -this_cpu (work_to_do) =1 ; +tasklet_schedule_on_cpu (&this_cpu (livepatch_tasklet),smp_processor_id ());put_cpu_maps ();return 0 ; } -static void reschedule_fn (void *unused) +static void tasklet_fn (void *unused) {this_cpu (work_to_do) =1 ; -raise_softirq (SCHEDULE_SOFTIRQ ); } static int livepatch_spin(atomic_t *counter, s_time_t timeout,
@@ -1652,7 +1653,7 @@ void check_for_livepatch_work(void)if (atomic_inc_and_test (&livepatch_work.semaphore) ) {struct payload *p; -unsigned int cpus; +unsigned int cpus, i;bool action_done = false; p = livepatch_work.data;
@@ -1682,7 +1683,9 @@ void check_for_livepatch_work(void) {dprintk (XENLOG_DEBUG ,LIVEPATCH "%s: CPU%u - IPIing the other %u CPUs\n" , p->name, cpu, cpus); -smp_call_function (reschedule_fn,NULL ,0 ); + for_each_online_cpu ( i ) + if ( i != cpu ) + tasklet_schedule_on_cpu(&per_cpu(livepatch_tasklet, i), i); } timeout = livepatch_work.timeout +NOW ();
@@ -2116,8 +2119,34 @@ static void livepatch_printall(unsigned char key)spin_unlock (&payload_lock); } +static int cpu_callback ( +struct notifier_block *nfb,unsigned long action,void *hcpu) +{ +unsigned int cpu = (unsigned long )hcpu; + +if ( action ==CPU_UP_PREPARE ) +tasklet_init (&per_cpu (livepatch_tasklet, cpu), tasklet_fn,NULL ); + +return NOTIFY_DONE ; +} + +static struct notifier_block cpu_nfb = { + .notifier_call = cpu_callback +}; +static int __init livepatch_init (void ) { +unsigned int cpu; + +for_each_online_cpu ( cpu ) + { +void *hcpu = (void *)(long )cpu; + +cpu_callback (&cpu_nfb,CPU_UP_PREPARE , hcpu); + } + +register_cpu_notifier (&cpu_nfb); +register_keyhandler ('x' , livepatch_printall,"print livepatch info" ,1 );arch_livepatch_init ();
Signature · 7 lines
2.16.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 17 lines · Juergen Gross
One of the main design goals of core scheduling is to avoid actions which are not directly related to the domain currently running on a given cpu or core. Live patching is one of those actions which are allowed taking place on a cpu only when the idle scheduling unit is active on that cpu.
Unfortunately live patching tries to force the cpus into the idle loop just by raising the schedule softirq, which will no longer be guaranteed to work with core scheduling active. Additionally there are still some places in the hypervisor calling
check_for_livepatch_work()without being in the idle loop.It is easy to force a cpu into the main idle loop by scheduling a tasklet on it. So switch live patching to use tasklets for switching to idle and raising scheduling events. Additionally the calls of
check_for_livepatch_work()outside the main idle loop can be dropped.
Quoted 8 lines
As tasklets are only running on idle vcpus and
stop_machine_run()is activating tasklets on all cpus but the one it has been called on to rendezvous, it is mandatory forstop_machine_run()to be called on an idle vcpu, too, as otherwise there is no way for scheduling to activate the idle vcpu for the tasklet on the sibling of the cpustop_machine_run()has been called on.Signed-off-by Juergen Gross <[email protected]>
Applicable x86 bits
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 20 lines
From: Juergen Gross <[email protected]>
Sent: Tuesday, February 11, 2020 5:31 PMOne of the main design goals of core scheduling is to avoid actions which are not directly related to the domain currently running on a given cpu or core. Live patching is one of those actions which are allowed taking place on a cpu only when the idle scheduling unit is active on that cpu.
Unfortunately live patching tries to force the cpus into the idle loop just by raising the schedule softirq, which will no longer be guaranteed to work with core scheduling active. Additionally there are still some places in the hypervisor calling
check_for_livepatch_work()without being in the idle loop.It is easy to force a cpu into the main idle loop by scheduling a tasklet on it. So switch live patching to use tasklets for switching to idle and raising scheduling events. Additionally the calls of
check_for_livepatch_work()outside the main idle loop can be dropped.
Quoted 8 lines
As tasklets are only running on idle vcpus and
stop_machine_run()is activating tasklets on all cpus but the one it has been called on to rendezvous, it is mandatory forstop_machine_run()to be called on an idle vcpu, too, as otherwise there is no way for scheduling to activate the idle vcpu for the tasklet on the sibling of the cpustop_machine_run()has been called on.Signed-off-by Juergen Gross <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Hi Juergen,
Quoted 13 lines · Juergen Gross
xen/arch/arm/traps.c +0
-1@@ -23,7 +23,6 @@#include <xen/iocap.h> #include <xen/irq.h> #include <xen/lib.h> -#include <xen/livepatch.h> #include <xen/mem_access.h> #include <xen/mm.h> #include <xen/param.h>
xen/arch/arm/traps.c static void check_for_pcpu_work(void) · line 2246
Quoted 8 lines
xen/arch/arm/traps.c +0
-5@@ -2239,11 +2238,6 @@ static void check_for_pcpu_work(void) {local_irq_enable ();do_softirq (); -/* -* Must be the last one - as the IPI will trigger us to come here -* and we want to patch the hypervisor with almost no stack. -*/ -check_for_livepatch_work ();
The check here was meant to match the x86 counterpart in reset_stack_and_jump(). I suspect you also need to remove it.
xen/arch/arm/traps.c static void check_for_pcpu_work(void) · line 2243
Quoted 3 lines
xen/arch/arm/traps.c +0
-0local_irq_disable (); } }
Cheers,
Signature · 6 lines
Julien Grall _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 19 lines · Julien Grall
Hi Juergen,
Quoted 16 lines · Juergen Gross
xen/arch/arm/traps.c +0
-1@@ -23,7 +23,6 @@ #include <xen/iocap.h> #include <xen/irq.h> #include <xen/lib.h> -#include <xen/livepatch.h> #include <xen/mem_access.h> #include <xen/mm.h> #include <xen/param.h>@@ -2239,11 +2238,6 @@ static void check_for_pcpu_work(void) { local_irq_enable(); do_softirq();
Quoted 8 lines
Quoted 6 lines
xen/arch/arm/traps.c +0
-2- /* -* Must be the last one - as the IPI will trigger us to come here
- * and we want to patch the hypervisor with almost no stack. - */
-check_for_livepatch_work();The check here was meant to match the x86 counterpart in
reset_stack_and_jump(). I suspect you also need to remove it.
Not really, as on x86 all relevant non-idle cases are being switched to use reset_stack_and_jump_nolp().
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Julien Grall
Acked-byHi Juergen,
Quoted 29 lines · Jürgen Groß
Quoted 19 lines · Julien Grall
Hi Juergen,
Quoted 16 lines · Juergen Gross
xen/arch/arm/traps.c +0
-1@@ -23,7 +23,6 @@ #include <xen/iocap.h> #include <xen/irq.h> #include <xen/lib.h> -#include <xen/livepatch.h> #include <xen/mem_access.h> #include <xen/mm.h> #include <xen/param.h>@@ -2239,11 +2238,6 @@ static void check_for_pcpu_work(void) { local_irq_enable(); do_softirq();Quoted 8 lines
Quoted 6 lines
xen/arch/arm/traps.c +0
-2- /* -* Must be the last one - as the IPI will trigger us to come here
- * and we want to patch the hypervisor with almost no stack. - */
-check_for_livepatch_work();The check here was meant to match the x86 counterpart in
reset_stack_and_jump(). I suspect you also need to remove it.
Quoted 2 lines
Not really, as on x86 all relevant non-idle cases are being switched to use
reset_stack_and_jump_nolp().
Ah yes, I missed that bit. Thank you for the explanation!
Cheers,
Signature · 6 lines
Julien Grall _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 25 lines · Juergen Gross
One of the main design goals of core scheduling is to avoid actions which are not directly related to the domain currently running on a given cpu or core. Live patching is one of those actions which are allowed taking place on a cpu only when the idle scheduling unit is active on that cpu.
Unfortunately live patching tries to force the cpus into the idle loop just by raising the schedule softirq, which will no longer be guaranteed to work with core scheduling active. Additionally there are still some places in the hypervisor calling
check_for_livepatch_work()without being in the idle loop.It is easy to force a cpu into the main idle loop by scheduling a tasklet on it. So switch live patching to use tasklets for switching to idle and raising scheduling events. Additionally the calls of
check_for_livepatch_work()outside the main idle loop can be dropped.As tasklets are only running on idle vcpus and
stop_machine_run()is activating tasklets on all cpus but the one it has been called on to rendezvous, it is mandatory forstop_machine_run()to be called on an idle vcpu, too, as otherwise there is no way for scheduling to activate the idle vcpu for the tasklet on the sibling of the cpustop_machine_run()has been called on.Signed-off-by Juergen Gross <[email protected]>
Quoted 10 lines
--- xen/arch/arm/domain.c | 9 ++++----- xen/arch/arm/traps.c | 6 ------ xen/arch/x86/domain.c | 9 ++++----- xen/arch/x86/hvm/svm/svm.c | 2 +- xen/arch/x86/hvm/vmx/vmcs.c | 2 +- xen/arch/x86/pv/domain.c | 2 +- xen/arch/x86/setup.c | 2 +- xen/common/livepatch.c | 39 ++++++++++++++++++++++++++++++++++----- 8 files changed, 46 insertions(+), 25 deletions(-)
Konrad, Ross - I was about to apply this when I noticed an ack by one of the two of you is still needed. Care to provide one (or comment if there are issues)?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Ross Lagerwall
Reviewed-byQuoted 37 lines · Jan Beulich
Quoted 25 lines · Juergen Gross
One of the main design goals of core scheduling is to avoid actions which are not directly related to the domain currently running on a given cpu or core. Live patching is one of those actions which are allowed taking place on a cpu only when the idle scheduling unit is active on that cpu.
Unfortunately live patching tries to force the cpus into the idle loop just by raising the schedule softirq, which will no longer be guaranteed to work with core scheduling active. Additionally there are still some places in the hypervisor calling
check_for_livepatch_work()without being in the idle loop.It is easy to force a cpu into the main idle loop by scheduling a tasklet on it. So switch live patching to use tasklets for switching to idle and raising scheduling events. Additionally the calls of
check_for_livepatch_work()outside the main idle loop can be dropped.As tasklets are only running on idle vcpus and
stop_machine_run()is activating tasklets on all cpus but the one it has been called on to rendezvous, it is mandatory forstop_machine_run()to be called on an idle vcpu, too, as otherwise there is no way for scheduling to activate the idle vcpu for the tasklet on the sibling of the cpustop_machine_run()has been called on.Signed-off-by Juergen Gross <[email protected]>Quoted 10 lines
--- xen/arch/arm/domain.c | 9 ++++----- xen/arch/arm/traps.c | 6 ------ xen/arch/x86/domain.c | 9 ++++----- xen/arch/x86/hvm/svm/svm.c | 2 +- xen/arch/x86/hvm/vmx/vmcs.c | 2 +- xen/arch/x86/pv/domain.c | 2 +- xen/arch/x86/setup.c | 2 +- xen/common/livepatch.c | 39 ++++++++++++++++++++++++++++++++++----- 8 files changed, 46 insertions(+), 25 deletions(-)
Quoted 4 lines
Konrad, Ross - I was about to apply this when I noticed an ack by one of the two of you is still needed. Care to provide one (or comment if there are issues)?
Sorry.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 37 lines · Jan Beulich
Quoted 25 lines · Juergen Gross
One of the main design goals of core scheduling is to avoid actions which are not directly related to the domain currently running on a given cpu or core. Live patching is one of those actions which are allowed taking place on a cpu only when the idle scheduling unit is active on that cpu.
Unfortunately live patching tries to force the cpus into the idle loop just by raising the schedule softirq, which will no longer be guaranteed to work with core scheduling active. Additionally there are still some places in the hypervisor calling
check_for_livepatch_work()without being in the idle loop.It is easy to force a cpu into the main idle loop by scheduling a tasklet on it. So switch live patching to use tasklets for switching to idle and raising scheduling events. Additionally the calls of
check_for_livepatch_work()outside the main idle loop can be dropped.As tasklets are only running on idle vcpus and
stop_machine_run()is activating tasklets on all cpus but the one it has been called on to rendezvous, it is mandatory forstop_machine_run()to be called on an idle vcpu, too, as otherwise there is no way for scheduling to activate the idle vcpu for the tasklet on the sibling of the cpustop_machine_run()has been called on.Signed-off-by Juergen Gross <[email protected]>Quoted 10 lines
--- xen/arch/arm/domain.c | 9 ++++----- xen/arch/arm/traps.c | 6 ------ xen/arch/x86/domain.c | 9 ++++----- xen/arch/x86/hvm/svm/svm.c | 2 +- xen/arch/x86/hvm/vmx/vmcs.c | 2 +- xen/arch/x86/pv/domain.c | 2 +- xen/arch/x86/setup.c | 2 +- xen/common/livepatch.c | 39 ++++++++++++++++++++++++++++++++++----- 8 files changed, 46 insertions(+), 25 deletions(-)
Quoted 3 lines
Konrad, Ross - I was about to apply this when I noticed an ack by one of the two of you is still needed. Care to provide one (or comment if there are issues)?
I tested this so feel free to add also Tested-by/Acked-by from me.
Thank you!
Quoted 2 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel