From nobody Thu Mar 28 15:12:06 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1581413528785380.343185390606; Tue, 11 Feb 2020 01:32:08 -0800 (PST) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j1RtA-0008Fb-M2; Tue, 11 Feb 2020 09:31:28 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1j1Rt9-0008FU-DU for xen-devel@lists.xenproject.org; Tue, 11 Feb 2020 09:31:27 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 464ac2d6-4cb1-11ea-b53a-12813bfff9fa; Tue, 11 Feb 2020 09:31:26 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 257CBAC77; Tue, 11 Feb 2020 09:31:25 +0000 (UTC) X-Inumbo-ID: 464ac2d6-4cb1-11ea-b53a-12813bfff9fa X-Virus-Scanned: by amavisd-new at test-mx.suse.de From: Juergen Gross To: xen-devel@lists.xenproject.org Date: Tue, 11 Feb 2020 10:31:22 +0100 Message-Id: <20200211093122.5644-1-jgross@suse.com> X-Mailer: git-send-email 2.16.4 Subject: [Xen-devel] [PATCH] xen: do live patching only from main idle loop X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Juergen Gross , Kevin Tian , Stefano Stabellini , Julien Grall , Jun Nakajima , Wei Liu , Konrad Rzeszutek Wilk , Andrew Cooper , Ross Lagerwall , Jan Beulich , Volodymyr Babchuk , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" 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. Signed-off-by: Juergen Gross Acked-by: Jan Beulich Acked-by: Julien Grall Reviewed-by: Kevin Tian Reviewed-by: Ross Lagerwall --- 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(-) diff --git a/xen/arch/arm/domain.c b/xen/arch/arm/domain.c index aa3df3b3ba..6627be2922 100644 --- a/xen/arch/arm/domain.c +++ b/xen/arch/arm/domain.c @@ -72,7 +72,11 @@ void idle_loop(void) =20 /* 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 scrubbi= ng * and then, after it is done, whether softirqs became pending @@ -83,11 +87,6 @@ void idle_loop(void) do_idle(); =20 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(); } } =20 diff --git a/xen/arch/arm/traps.c b/xen/arch/arm/traps.c index 6f9bec22d3..30c4c1830b 100644 --- a/xen/arch/arm/traps.c +++ b/xen/arch/arm/traps.c @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -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(); } } diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index f53ae5ff86..2bc7c4fb2d 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -141,7 +141,11 @@ static void idle_loop(void) =20 /* 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 scrubbi= ng * 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(); } } =20 diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index b7f67f9f03..32d8d847f2 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -1032,7 +1032,7 @@ static void noreturn svm_do_resume(struct vcpu *v) =20 hvm_do_resume(v); =20 - reset_stack_and_jump(svm_asm_do_resume); + reset_stack_and_jump_nolp(svm_asm_do_resume); } =20 void svm_vmenter_helper(const struct cpu_user_regs *regs) diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index 65445afeb0..4c23645454 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -1890,7 +1890,7 @@ void vmx_do_resume(struct vcpu *v) if ( host_cr4 !=3D read_cr4() ) __vmwrite(HOST_CR4, read_cr4()); =20 - reset_stack_and_jump(vmx_asm_do_vmentry); + reset_stack_and_jump_nolp(vmx_asm_do_vmentry); } =20 static inline unsigned long vmr(unsigned long field) diff --git a/xen/arch/x86/pv/domain.c b/xen/arch/x86/pv/domain.c index c3473b9a47..7dbd8dbfa2 100644 --- a/xen/arch/x86/pv/domain.c +++ b/xen/arch/x86/pv/domain.c @@ -62,7 +62,7 @@ custom_runtime_param("pcid", parse_pcid); static void noreturn continue_nonidle_domain(struct vcpu *v) { check_wakeup_from_wait(); - reset_stack_and_jump(ret_from_intr); + reset_stack_and_jump_nolp(ret_from_intr); } =20 static int setup_compat_l4(struct vcpu *v) diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c index e50e1f86b3..3bed0a9492 100644 --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -632,7 +632,7 @@ static void __init noreturn reinit_bsp_stack(void) stack_base[0] =3D stack; memguard_guard_stack(stack); =20 - reset_stack_and_jump(init_done); + reset_stack_and_jump_nolp(init_done); } =20 /* diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c index 5e09dc990b..861a227dbd 100644 --- a/xen/common/livepatch.c +++ b/xen/common/livepatch.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -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); =20 static int get_name(const struct xen_livepatch_name *name, char *n) { @@ -1582,17 +1584,16 @@ static int schedule_work(struct payload *data, uint= 32_t cmd, uint32_t timeout) smp_wmb(); =20 livepatch_work.do_work =3D 1; - this_cpu(work_to_do) =3D 1; + tasklet_schedule_on_cpu(&this_cpu(livepatch_tasklet), smp_processor_id= ()); =20 put_cpu_maps(); =20 return 0; } =20 -static void reschedule_fn(void *unused) +static void tasklet_fn(void *unused) { this_cpu(work_to_do) =3D 1; - raise_softirq(SCHEDULE_SOFTIRQ); } =20 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 =3D false; =20 p =3D 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 !=3D cpu ) + tasklet_schedule_on_cpu(&per_cpu(livepatch_tasklet, i)= , i); } =20 timeout =3D livepatch_work.timeout + NOW(); @@ -2116,8 +2119,34 @@ static void livepatch_printall(unsigned char key) spin_unlock(&payload_lock); } =20 +static int cpu_callback( + struct notifier_block *nfb, unsigned long action, void *hcpu) +{ + unsigned int cpu =3D (unsigned long)hcpu; + + if ( action =3D=3D CPU_UP_PREPARE ) + tasklet_init(&per_cpu(livepatch_tasklet, cpu), tasklet_fn, NULL); + + return NOTIFY_DONE; +} + +static struct notifier_block cpu_nfb =3D { + .notifier_call =3D cpu_callback +}; + static int __init livepatch_init(void) { + unsigned int cpu; + + for_each_online_cpu ( cpu ) + { + void *hcpu =3D (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= ); =20 arch_livepatch_init(); --=20 2.16.4 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel