loongarch.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Bibo Mao <maobibo@loongson.cn>
To: cuitao <cuitao@kylinos.cn>,
	zhaotianrui@loongson.cn, chenhuacai@kernel.org,
	loongarch@lists.linux.dev
Cc: kernel@xen0n.name
Subject: Re: [PATCH] LoongArch: KVM: Fix data race in kvm_ipi_regs_access
Date: Tue, 14 Apr 2026 09:50:33 +0800	[thread overview]
Message-ID: <ce3d170a-56a4-9343-49fa-2d8b3cbc9ebe@loongson.cn> (raw)
In-Reply-To: <20260414013316.306075-1-cuitao@kylinos.cn>



On 2026/4/14 上午9:33, cuitao wrote:
> Add spin_lock protection around ipi_state field accesses in
> kvm_ipi_regs_access(). get_user/put_user are performed
> outside the lock since they may sleep on page faults.
> 
> Signed-off-by: cuitao <cuitao@kylinos.cn>
> ---
>   arch/loongarch/kvm/intc/ipi.c | 8 ++++++++
>   1 file changed, 8 insertions(+)
> 
> diff --git a/arch/loongarch/kvm/intc/ipi.c b/arch/loongarch/kvm/intc/ipi.c
> index 1f6ebbd0af5c..e6a4a461a0c8 100644
> --- a/arch/loongarch/kvm/intc/ipi.c
> +++ b/arch/loongarch/kvm/intc/ipi.c
> @@ -348,18 +348,26 @@ static int kvm_ipi_regs_access(struct kvm_device *dev,
>   		if (len == 4) {
>   			if (get_user(val, (uint32_t __user *)attr->addr))
>   				return -EFAULT;
> +			spin_lock(&vcpu->arch.ipi_state.lock);
>   			*(uint32_t *)p = (uint32_t)val;
> +			spin_unlock(&vcpu->arch.ipi_state.lock);
>   		} else if (len == 8) {
>   			if (get_user(val, (uint64_t __user *)attr->addr))
>   				return -EFAULT;
> +			spin_lock(&vcpu->arch.ipi_state.lock);
>   			*(uint64_t *)p = val;
> +			spin_unlock(&vcpu->arch.ipi_state.lock);
>   		}
>   	} else {
>   		if (len == 4) {
> +			spin_lock(&vcpu->arch.ipi_state.lock);
>   			val = *(uint32_t *)p;
> +			spin_unlock(&vcpu->arch.ipi_state.lock);
>   			return put_user(val, (uint32_t __user *)attr->addr);
>   		} else if (len == 8) {
> +			spin_lock(&vcpu->arch.ipi_state.lock);
>   			val = *(uint64_t *)p;
> +			spin_unlock(&vcpu->arch.ipi_state.lock);
>   			return put_user(val, (uint64_t __user *)attr->addr);
>   		}
>   	}
> 
I am not sure whether spinlock need be added here. When get/set ipi 
register of vCPU from VMM side, the vCPU should be kicked out at safe 
state by VMM; Otherwise even if lock is added, VMM will get one register 
at a time and vCPU may write these registers at the same time, part of 
registers acquired by VMM still are stale.

Regards
Bibo Mao


  reply	other threads:[~2026-04-14  1:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-14  1:33 [PATCH] LoongArch: KVM: Fix data race in kvm_ipi_regs_access cuitao
2026-04-14  1:50 ` Bibo Mao [this message]
2026-04-14  2:34   ` cuitao
2026-04-14  2:46   ` cuitao

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=ce3d170a-56a4-9343-49fa-2d8b3cbc9ebe@loongson.cn \
    --to=maobibo@loongson.cn \
    --cc=chenhuacai@kernel.org \
    --cc=cuitao@kylinos.cn \
    --cc=kernel@xen0n.name \
    --cc=loongarch@lists.linux.dev \
    --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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).