mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: bibo mao <maobibo@loongson.cn>
To: Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>
Cc: Huacai Chen <chenhuacai@kernel.org>,
	Tianrui Zhao <zhaotianrui@loongson.cn>,
	loongarch@lists.linux.dev, linux-kernel@vger.kernel.org,
	kvm@vger.kernel.org
Subject: Re: [RFC V2] LoongArch: KVM: Handle interrupt early before enabling irq
Date: Tue, 25 Mar 2025 10:13:41 +0800	[thread overview]
Message-ID: <c220d043-2314-85bb-e99d-dc2c609aa739@loongson.cn> (raw)
In-Reply-To: <20250311074737.3160546-1-maobibo@loongson.cn>

Hi Paolo, Sean

This idea comes from x86, do you have any guidance or suggestion about it?

Also I notice that there is such irq_enable()/irq_disable() pair on x86, 
I do not know why it is so.
     local_irq_enable();
     ++vcpu->stat.exits;
     local_irq_disable();
     guest_timing_exit_irqoff();
     local_irq_enable();

Regards
Bibo Mao

On 2025/3/11 下午3:47, Bibo Mao wrote:
> If interrupt arrive when vCPU is running, vCPU will exit because of
> interrupt exception. Currently interrupt exception is handled after
> local_irq_enable() is called, and it is handled by host kernel rather
> than KVM hypervisor. It will introduce extra another interrupt
> exception and then host will handle irq.
> 
> If KVM hypervisor detect that it is interrupt exception, interrupt
> can be handle early in KVM hypervisor before local_irq_enable() is
> called.
> 
> On 3C5000 dual-way machine, there will be 10% -- 15% performance
> improvement with netperf UDP_RR option with 10G ethernet card.
>                     original     with patch    improvement
>    netperf UDP_RR     7200          8100           +12%
> 
> The total performance is low because irqchip is emulated in qemu VMM,
> however from the same testbed, there is performance improvement
> actually.
> 
> Signed-off-by: Bibo Mao <maobibo@loongson.cn>
> ---
> v1 ... v2:
>    1. Move guest_timing_exit_irqoff() after host interrupt handling like
>       other architectures.
>    2. Construct interrupt context pt_regs from guest entering context
>    3. Add cond_resched() after irq enabling
> ---
>   arch/loongarch/kernel/traps.c |  1 +
>   arch/loongarch/kvm/vcpu.c     | 36 ++++++++++++++++++++++++++++++++++-
>   2 files changed, 36 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/loongarch/kernel/traps.c b/arch/loongarch/kernel/traps.c
> index 2ec3106c0da3..eed0d8b02ee3 100644
> --- a/arch/loongarch/kernel/traps.c
> +++ b/arch/loongarch/kernel/traps.c
> @@ -1114,6 +1114,7 @@ asmlinkage void noinstr do_vint(struct pt_regs *regs, unsigned long sp)
>   
>   	irqentry_exit(regs, state);
>   }
> +EXPORT_SYMBOL(do_vint);
>   
>   unsigned long eentry;
>   unsigned long tlbrentry;
> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
> index 9e1a9b4aa4c6..bab7a71eb965 100644
> --- a/arch/loongarch/kvm/vcpu.c
> +++ b/arch/loongarch/kvm/vcpu.c
> @@ -5,6 +5,7 @@
>   
>   #include <linux/kvm_host.h>
>   #include <linux/entry-kvm.h>
> +#include <asm/exception.h>
>   #include <asm/fpu.h>
>   #include <asm/lbt.h>
>   #include <asm/loongarch.h>
> @@ -304,6 +305,23 @@ static int kvm_pre_enter_guest(struct kvm_vcpu *vcpu)
>   	return ret;
>   }
>   
> +static void kvm_handle_irq(struct kvm_vcpu *vcpu)
> +{
> +	struct pt_regs regs, *old;
> +
> +	/*
> +	 * Construct pseudo pt_regs, only necessary registers is added
> +	 * Interrupt context coming from guest enter context
> +	 */
> +	old = (struct pt_regs *)(vcpu->arch.host_sp - sizeof(struct pt_regs));
> +	/* Disable preemption in irq exit function irqentry_exit() */
> +	regs.csr_prmd = 0;
> +	regs.regs[LOONGARCH_GPR_SP] = vcpu->arch.host_sp;
> +	regs.regs[LOONGARCH_GPR_FP] = old->regs[LOONGARCH_GPR_FP];
> +	regs.csr_era = old->regs[LOONGARCH_GPR_RA];
> +	do_vint(&regs, (unsigned long)&regs);
> +}
> +
>   /*
>    * Return 1 for resume guest and "<= 0" for resume host.
>    */
> @@ -321,8 +339,23 @@ static int kvm_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
>   
>   	kvm_lose_pmu(vcpu);
>   
> -	guest_timing_exit_irqoff();
>   	guest_state_exit_irqoff();
> +
> +	/*
> +	 * VM exit because of host interrupts
> +	 * Handle irq directly before enabling irq
> +	 */
> +	if (!ecode && intr)
> +		kvm_handle_irq(vcpu);
> +
> +	/*
> +	 * Wait until after servicing IRQs to account guest time so that any
> +	 * ticks that occurred while running the guest are properly accounted
> +	 * to the guest. Waiting until IRQs are enabled degrades the accuracy
> +	 * of accounting via context tracking, but the loss of accuracy is
> +	 * acceptable for all known use cases.
> +	 */
> +	guest_timing_exit_irqoff();
>   	local_irq_enable();
>   
>   	trace_kvm_exit(vcpu, ecode);
> @@ -331,6 +364,7 @@ static int kvm_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
>   	} else {
>   		WARN(!intr, "vm exiting with suspicious irq\n");
>   		++vcpu->stat.int_exits;
> +		cond_resched();
>   	}
>   
>   	if (ret == RESUME_GUEST)
> 
> base-commit: 80e54e84911a923c40d7bee33a34c1b4be148d7a
> 


  reply	other threads:[~2025-03-25  2:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-11  7:47 Bibo Mao
2025-03-25  2:13 ` bibo mao [this message]
2025-03-26 15:09   ` Sean Christopherson
2025-03-27  3:16     ` bibo mao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c220d043-2314-85bb-e99d-dc2c609aa739@loongson.cn \
    --to=maobibo@loongson.cn \
    --cc=chenhuacai@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=loongarch@lists.linux.dev \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=zhaotianrui@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®