Skip to content

[PW_SID:1123513] RISC-V: KVM: Serialize virtual interrupt pending state updates#2244

Closed
linux-riscv-bot wants to merge 3 commits into
workflow__riscv__fixesfrom
pw1123513
Closed

[PW_SID:1123513] RISC-V: KVM: Serialize virtual interrupt pending state updates#2244
linux-riscv-bot wants to merge 3 commits into
workflow__riscv__fixesfrom
pw1123513

Conversation

@linux-riscv-bot

Copy link
Copy Markdown

PR for series 1123513 applied to workflow__riscv__fixes

Name: RISC-V: KVM: Serialize virtual interrupt pending state updates
URL: https://patchwork.kernel.org/project/linux-riscv/list/?series=1123513
Version: 2

liutgnu and others added 3 commits July 6, 2026 18:24
A NULL pointer dereference issue is noticed in riscv's machine_kexec_prepare(),
where image->segment[i].buf might be NULL and copied unchecked.

The NULL buf comes from ima_add_kexec_buffer(), where kbuf is added by
kexec_add_buffer(), but kbuf.buffer is NULL, then it is copied without
a check in machine_kexec_prepare():

  kexec_file_load
    -> kimage_file_alloc_init()
       -> kimage_file_prepare_segments()
          -> ima_add_kexec_buffer()
             -> kexec_add_buffer()
    -> machine_kexec_prepare()
       -> memcpy()

Address this by adding a check before the data copy attempt.

Fixes: b7fb4d7 ("RISC-V: use memcpy for kexec_file mode")
Cc: stable@vger.kernel.org
Closes: https://lore.kernel.org/kexec/CAO7dBbVftLUhd2qrh7hmijTB3PEPfZAhykCGqEfrPoOcSrrj-w@mail.gmail.com/
Acked-by: Baoquan He <bhe@redhat.com>
Acked-by: Pratyush Yadav <pratyush@kernel.org>
Reviewed-by: Nutty Liu <nutty.liu@hotmail.com>
Signed-off-by: Tao Liu <ltao@redhat.com>
Link: https://patch.msgid.link/20260705232706.30265-2-ltao@redhat.com
Signed-off-by: Paul Walmsley <pjw@kernel.org>
RISC-V KVM tracks guest interrupt state with two bitmaps:

  - irqs_pending: interrupts that should be visible to the guest
  - irqs_pending_mask: interrupts whose pending state changed

The current code updates those bitmaps with independent atomic bitops
and assumes a multiple-producer, single-consumer protocol.  That model
does not actually hold.

kvm_riscv_vcpu_sync_interrupts() is not a pure consumer.  When the guest
changes guest-visible HVIP state, sync_interrupts() writes both
irqs_pending and irqs_pending_mask to reflect the new guest state back
into KVM state.  As a result, irqs_pending and irqs_pending_mask form a
single logical state transition, but they are not updated atomically as
a pair.

This allows a race where a newly injected interrupt is lost.  For
example:

  CPU0                                          CPU1
  ----                                          ----
  kvm_riscv_vcpu_set_interrupt(VS_SOFT)
    set_bit(VS_SOFT, irqs_pending)
                                                kvm_riscv_vcpu_sync_interrupts()
                                                  sees guest-cleared HVIP.VSSIP
                                                  sets irqs_pending_mask
                                                  clears irqs_pending
    set_bit(VS_SOFT, irqs_pending_mask)
    kvm_vcpu_kick()

After that interleaving, a later flush can update HVIP without VSSIP
even though a new virtual interrupt was injected.  In practice, the
guest can remain blocked in WFI with work pending.

The same pending/mask protocol is shared by VS soft interrupts, PMU
overflow delivery, and AIA high interrupt synchronization, so the race
is not limited to one interrupt source.

Fix this by serializing all updates to irqs_pending and
irqs_pending_mask with a per-vCPU raw spinlock.  This keeps the pending
bit and the dirty mask as one state transition across:

  - set/unset interrupt
  - guest HVIP sync
  - interrupt flush to guest CSR state
  - vCPU reset
  - AIA CSR writes that clear dirty state

This intentionally replaces the existing lockless protocol instead of
trying to repair it with additional barriers.  The problem is not memory
ordering on a single field; it is that two separate bitmaps encode one
shared state machine while both producers and sync paths can modify
them.  A per-vCPU raw spinlock keeps the fix small, local, and suitable
for backporting.

Observed symptom:

  - guest occasionally remains blocked in WFI after host-side virtual
    interrupt injection

Testing:

  - reproduced on a RISC-V KVM setup running lkvm stress workloads
  - observed guest stalls with WFI diagnostics while host-side kick and
    interrupt injection had already happened
  - stress run no longer reproduced the lost-VSSIP hang after applying
    this change

Fixes: cce69af ("RISC-V: KVM: Implement VCPU interrupts and requests handling")
Cc: stable@vger.kernel.org
Signed-off-by: Xie Bo <xb@ultrarisc.com>
Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
@linux-riscv-bot

Copy link
Copy Markdown
Author

Patch 1: "[v2,1/1] RISC-V: KVM: Serialize virtual interrupt pending state updates"
build-rv32-defconfig
Desc: Builds riscv32 defconfig
Duration: 141.91 seconds
Result: PASS

@linux-riscv-bot

Copy link
Copy Markdown
Author

Patch 1: "[v2,1/1] RISC-V: KVM: Serialize virtual interrupt pending state updates"
build-rv64-clang-allmodconfig
Desc: Builds riscv64 allmodconfig with Clang, and checks for errors and added warnings
Duration: 1178.33 seconds
Result: PASS

@linux-riscv-bot

Copy link
Copy Markdown
Author

Patch 1: "[v2,1/1] RISC-V: KVM: Serialize virtual interrupt pending state updates"
build-rv64-gcc-allmodconfig
Desc: Builds riscv64 allmodconfig with GCC, and checks for errors and added warnings
Duration: 1470.70 seconds
Result: PASS

@linux-riscv-bot

Copy link
Copy Markdown
Author

Patch 1: "[v2,1/1] RISC-V: KVM: Serialize virtual interrupt pending state updates"
build-rv64-nommu-k210-defconfig
Desc: Builds riscv64 defconfig with NOMMU for K210
Duration: 26.60 seconds
Result: PASS

@linux-riscv-bot

Copy link
Copy Markdown
Author

Patch 1: "[v2,1/1] RISC-V: KVM: Serialize virtual interrupt pending state updates"
build-rv64-nommu-k210-virt
Desc: Builds riscv64 defconfig with NOMMU for the virt platform
Duration: 28.04 seconds
Result: PASS

@linux-riscv-bot

Copy link
Copy Markdown
Author

Patch 1: "[v2,1/1] RISC-V: KVM: Serialize virtual interrupt pending state updates"
checkpatch
Desc: Runs checkpatch.pl on the patch
Duration: 1.58 seconds
Result: PASS

@linux-riscv-bot

Copy link
Copy Markdown
Author

Patch 1: "[v2,1/1] RISC-V: KVM: Serialize virtual interrupt pending state updates"
dtb-warn-rv64
Desc: Checks for Device Tree warnings/errors
Duration: 87.06 seconds
Result: PASS

@linux-riscv-bot

Copy link
Copy Markdown
Author

Patch 1: "[v2,1/1] RISC-V: KVM: Serialize virtual interrupt pending state updates"
header-inline
Desc: Detects static functions without inline keyword in header files
Duration: 0.24 seconds
Result: PASS

@linux-riscv-bot

Copy link
Copy Markdown
Author

Patch 1: "[v2,1/1] RISC-V: KVM: Serialize virtual interrupt pending state updates"
kdoc
Desc: Detects for kdoc errors
Duration: 0.88 seconds
Result: PASS

@linux-riscv-bot

Copy link
Copy Markdown
Author

Patch 1: "[v2,1/1] RISC-V: KVM: Serialize virtual interrupt pending state updates"
module-param
Desc: Detect module_param changes
Duration: 0.27 seconds
Result: PASS

@linux-riscv-bot

Copy link
Copy Markdown
Author

Patch 1: "[v2,1/1] RISC-V: KVM: Serialize virtual interrupt pending state updates"
verify-fixes
Desc: Verifies that the Fixes: tags exist
Duration: 0.28 seconds
Result: PASS

@linux-riscv-bot

Copy link
Copy Markdown
Author

Patch 1: "[v2,1/1] RISC-V: KVM: Serialize virtual interrupt pending state updates"
verify-signedoff
Desc: Verifies that Signed-off-by: tags are correct
Duration: 0.31 seconds
Result: PASS

@linux-riscv-bot
linux-riscv-bot force-pushed the workflow__riscv__fixes branch 4 times, most recently from 2b6a725 to da477a6 Compare July 11, 2026 01:54
@linux-riscv-bot
linux-riscv-bot deleted the pw1123513 branch July 13, 2026 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants