Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "GUO Ren (XuanTie)" <guoren@kernel.org>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH RFC v4 4/4] RISC-V: paravirt: Support nopvspin to disable PARAVIRT_SPINLOCKS
Date: Mon, 21 Sep 2026 12:41:03 +0000	[thread overview]
Message-ID: <20260921124103.E63431F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260921-pvqspinlock-v4-4-409a22aed6ef@kernel.org>

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] build: unresolved virt_spin_lock_key when PARAVIRT_SPINLOCKS is disabled
- [High] locking/pv: inverted nopvspin semantics disables TAS fallback on unaccelerated VMs
- [High] locking/pv: skipping ZICCRSE extension check exposes guest to LR/SC livelock
- [Low] locking/pv: casting atomic_t to raw pointer in smp_cond_load_relaxed
- [Low] locking/pv: static_branch_likely pessimizes native spinlock fastpath
--

commit 51ef25d6df1335ba98379605c62c0034ca0b9dfe
Author: GUO Ren (XuanTie) <guoren@kernel.org>

RISC-V: paravirt: Support nopvspin to disable PARAVIRT_SPINLOCKS

VM guests should fall back to a Test-and-Set spinlock when
PARAVIRT_SPINLOCKS is disabled, because fair locks suffer from severe
lock-holder preemption issues. The virt_spin_lock_key shortcuts
queued_spin_lock_slowpath(), allowing virt_spin_lock() to hijack it.

> diff --git a/arch/riscv/include/asm/qspinlock.h b/arch/riscv/include/asm/qspinlock.h
> index 330b714edc447..9c7108baa40a1 100644
> --- a/arch/riscv/include/asm/qspinlock.h
> +++ b/arch/riscv/include/asm/qspinlock.h
[ ... ]
> @@ -29,5 +31,27 @@ static inline void queued_spin_unlock(struct qspinlock *lock)
>  #endif /* CONFIG_PARAVIRT_SPINLOCKS */
>  
>  #include <asm-generic/qspinlock.h>
> +#include <asm/jump_label.h>
> +
> +/*
> + * KVM guests fall back to a Test-and-Set spinlock because fair locks suffer
> + * from severe lock-holder-preemption issues. When virt_spin_lock_key is
> + * enabled, virt_spin_lock() shortcuts queued_spin_lock_slowpath() and hijacks
> + * the lock acquisition.
> + */
> +DECLARE_STATIC_KEY_FALSE(virt_spin_lock_key);

[Severity: High]
Is it possible for the kernel to fail to link due to an undefined symbol here?

virt_spin_lock_key is unconditionally declared and used by the inline function
rv_virt_spin_lock(). However, its definition resides in
arch/riscv/kernel/qspinlock_paravirt.c, which is only compiled when
CONFIG_PARAVIRT_SPINLOCKS is enabled. Does this break the build when
CONFIG_QUEUED_SPINLOCKS=y but CONFIG_PARAVIRT_SPINLOCKS=n?

> +
> +#define virt_spin_lock rv_virt_spin_lock
> +static inline bool rv_virt_spin_lock(struct qspinlock *lock)
> +{
> +	if (!static_branch_likely(&virt_spin_lock_key))
> +		return false;

[Severity: Low]
Does using likely() here force the compiler to treat the bare-metal default
path (when the key is disabled) as out-of-line?

Since virt_spin_lock_key is false by default on bare-metal systems, wouldn't
static_branch_likely() require an unconditional jump in the native queued
spinlock fastpath, degrading performance? Should this be
static_branch_unlikely() instead?

> +
> +	do {
> +		smp_cond_load_relaxed((s32 *)&lock->val, VAL == 0);

[Severity: Low]
Is it safe to cast &lock->val (an atomic_t) to a raw pointer like (s32 *)?

This bypasses the atomic_t abstraction. Should atomic_cond_read_relaxed()
be used here instead?

> +	} while (atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL) != 0);
> +
> +	return true;
> +}
>  
>  #endif /* _ASM_RISCV_QSPINLOCK_H */
> diff --git a/arch/riscv/kernel/qspinlock_paravirt.c b/arch/riscv/kernel/qspinlock_paravirt.c
> index 04b13994e971c..28c6c78d7e84c 100644
> --- a/arch/riscv/kernel/qspinlock_paravirt.c
> +++ b/arch/riscv/kernel/qspinlock_paravirt.c
[ ... ]
> @@ -58,6 +60,12 @@ bool __init pv_qspinlock_init(void)
>  	if (!sbi_probe_extension(SBI_EXT_PVLOCK))
>  		return false;
>  
> +	if (nopvspin) {
> +		static_branch_enable(&virt_spin_lock_key);
> +		pr_info("virt_spin_lock enabled by nopvspin\n");
> +		return true;
> +	}

[Severity: High]
Do the nopvspin semantics here invert the expected behavior?

If an unaccelerated VM boots without SBI_EXT_PVLOCK, pv_qspinlock_init()
returns early before enabling virt_spin_lock_key, defeating the TAS fallback.

Furthermore, if nopvspin is passed, it statically enables the key, whereas on
x86, nopvspin explicitly disables PV optimizations and forces native queued
spinlocks.

[Severity: High]
Can this early return expose guests to LR/SC livelocks?

If a VM uses nopvspin, returning true here causes riscv_spinlock_init() in
arch/riscv/kernel/setup.c to completely bypass the hardware extension checks
like ZABHA, ZACAS, or ZICCRSE.

Because rv_virt_spin_lock() relies on an LR/SC atomic_cmpxchg loop, will it
livelock under heavy contention if the underlying hardware lacks the ZICCRSE
extension, which provides forward progress guarantees?

> +
>  	pr_info("PV qspinlocks enabled\n");
>  	__pv_init_lock_hash();

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260921-pvqspinlock-v4-0-409a22aed6ef@kernel.org?part=4

      reply	other threads:[~2026-09-21 12:41 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-21 12:27 [PATCH RFC v4 0/4] RISC-V: Add PARAVIRT_SPINLOCKS support GUO Ren (XuanTie)
2026-09-21 12:27 ` [PATCH RFC v4 1/4] RISC-V: paravirt: Add pvqspinlock KVM backend GUO Ren (XuanTie)
2026-09-21 12:35   ` sashiko-bot
2026-09-21 12:27 ` [PATCH RFC v4 2/4] RISC-V: paravirt: Add pvqspinlock frontend GUO Ren (XuanTie)
2026-09-21 12:41   ` sashiko-bot
2026-09-21 12:27 ` [PATCH RFC v4 3/4] RISC-V: paravirt: pvqspinlock: Add trace point for pv_kick/wait GUO Ren (XuanTie)
2026-09-21 12:39   ` sashiko-bot
2026-09-21 12:27 ` [PATCH RFC v4 4/4] RISC-V: paravirt: Support nopvspin to disable PARAVIRT_SPINLOCKS GUO Ren (XuanTie)
2026-09-21 12:41   ` sashiko-bot [this message]

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=20260921124103.E63431F00893@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=guoren@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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