Skip to content

sync: rvck #282: [Backport] Added SBI v3.0 PMU enhancements in KVM and perf driver#241

Merged
xmzzz merged 15 commits into
RVCK-Project:OLK-6.6from
ieiao:PR282@rvck
Jul 7, 2026
Merged

sync: rvck #282: [Backport] Added SBI v3.0 PMU enhancements in KVM and perf driver#241
xmzzz merged 15 commits into
RVCK-Project:OLK-6.6from
ieiao:PR282@rvck

Conversation

@ieiao

@ieiao ieiao commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Sync commits from rvck PR 282.
Source PR: RVCK-Project/rvck#282

mdchitale and others added 15 commits June 24, 2026 11:19
mainline inclusion
from mainline-6.12-rc1
commit f0c9363db2ddfb07723c00cb153c84c8179e92d4
category: feature
bugzilla: RVCK-Project#281

--------------------------------

The SBI v2.0 specification pointed to by the link below reserves the
event code 0xffff for platform specific firmware events. Update the driver
to be able to parse and program such events. The platform specific
firmware events must now be specified in the perf command as below:
perf stat -e rCxxx ...
where bits[63:62] = 0x3 of the event config indicate a platform specific
firmware event and xxx indicate the actual event code which is passed
as the event data.

Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
Link: https://github.com/riscv-non-isa/riscv-sbi-doc/releases/download/v2.0/riscv-sbi.pdf
Link: https://lore.kernel.org/r/20240812051109.6496-1-mchitale@ventanamicro.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-6.10-rc7
commit 16d3b1af0944cd0e4eae291ab0097c54ecbc1048
category: feature
bugzilla: RVCK-Project#281

--------------------------------

The RISC-V SBI PMU specification defines several standard hardware and
cache events. Currently, all of these events are exposed to userspace,
even when not actually implemented. They appear in the `perf list`
output, and commands like `perf stat` try to use them.

This is more than just a cosmetic issue, because the PMU driver's .add
function fails for these events, which causes pmu_groups_sched_in() to
prematurely stop scheduling in other (possibly valid) hardware events.

Add logic to check which events are supported by the hardware (i.e. can
be mapped to some counter), so only usable events are reported to
userspace. Since the kernel does not know the mapping between events and
possible counters, this check must happen during boot, when no counters
are in use. Make the check asynchronous to minimize impact on boot time.

Fixes: e999143 ("RISC-V: Add perf platform driver based on SBI PMU extension")
Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Tested-by: Atish Patra <atishp@rivosinc.com>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Link: https://lore.kernel.org/r/20240628-misc_perf_fixes-v4-3-e01cfddcf035@rivosinc.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-6.12-rv2
commit c625154993d0d24a962b1830cd5ed92adda2cf86
category: feature
bugzilla: RVCK-Project#281

--------------------------------

RISC-V perf driver does not yet support PERF_TYPE_BREAKPOINT. It would
be more appropriate to return -EOPNOTSUPP or -ENOENT for this type in
pmu_sbi_event_map. Considering that other implementations return -ENOENT
for unsupported perf types, let's synchronize this behavior. Due to this
reason, a riscv bpf testcases perf_skip fail. Meanwhile, align that
behavior to the rest of proper place.

Signed-off-by: Pu Lehui <pulehui@huawei.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
Fixes: 9b3e150 ("RISC-V: Add a simple platform driver for RISC-V legacy perf")
Fixes: 16d3b1af0944 ("perf: RISC-V: Check standard event availability")
Fixes: e999143 ("RISC-V: Add perf platform driver based on SBI PMU extension")
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20240831071520.1630360-1-pulehui@huaweicloud.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-6.13-rc7
commit fc58db9aeb15e89b69ff5e9abc69ecf9e5f888ed
category: feature
bugzilla: RVCK-Project#281

--------------------------------

Platform firmware event data field is allowed to be 62 bits for
Linux as uppper most two bits are reserved to indicate SBI fw or
platform specific firmware events.
However, the event data field is masked as per the hardware raw
event mask which is not correct.

Fix the platform firmware event data field with proper mask.

Fixes: f0c9363db2dd ("perf/riscv-sbi: Add platform specific firmware event handling")
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Link: https://lore.kernel.org/r/20241212-pmu_event_fixes_v2-v2-1-813e8a4f5962@rivosinc.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-6.13-rc7
commit 2c206cdede567f53035c622e846678a996f39d69
category: feature
bugzilla: RVCK-Project#281

--------------------------------

If the upper two bits has an invalid valid (0x1), the event mapping
is not reliable as it returns an uninitialized variable.

Return appropriate value for the default case.

Fixes: f0c9363db2dd ("perf/riscv-sbi: Add platform specific firmware event handling")
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Link: https://lore.kernel.org/r/20241212-pmu_event_fixes_v2-v2-2-813e8a4f5962@rivosinc.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-6.13-rc7
commit 3aff4cdbe506652da77570baccad623511628250
category: feature
bugzilla: RVCK-Project#281

--------------------------------

The SBI specification allows only lower 48bits of hpmeventX to be
configured via SBI PMU. Currently, the driver masks of the higher
bits but doesn't return an error. This will lead to an additional
SBI call for config matching which should return for an invalid
event error in most of the cases.

However, if a platform(i.e Rocket and sifive cores) implements a
bitmap of all bits in the event encoding this will lead to an
incorrect event being programmed leading to user confusion.

Report the error to the user if higher bits are set during the
event mapping itself to avoid the confusion and save an additional
SBI call.

Suggested-by: Samuel Holland <samuel.holland@sifive.com>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Link: https://lore.kernel.org/r/20241212-pmu_event_fixes_v2-v2-3-813e8a4f5962@rivosinc.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-6.18-rc1
commit 8c8d0f002b769dbef26c93d68e49d1d0ba54d094
category: feature
bugzilla: RVCK-Project#281

--------------------------------

There are new PMU related features introduced in SBI v3.0.
1. Raw Event v2 which allows mhpmeventX value to be 56 bit wide.
2. Get Event info function to do a bulk query at one shot.

Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Acked-by: Paul Walmsley <pjw@kernel.org>
Link: https://lore.kernel.org/r/20250909-pmu_event_info-v6-1-d8f80cacb884@rivosinc.com
Signed-off-by: Anup Patel <anup@brainfault.org>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-6.18-rc1
commit 656ef2ea30a90f910e1d2691674c98e5dcd164b5
category: feature
bugzilla: RVCK-Project#281

--------------------------------

SBI v3.0 introduced a new raw event type that allows wider
mhpmeventX width to be programmed via CFG_MATCH.

Use the raw event v2 if SBI v3.0 is available.

Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Acked-by: Paul Walmsley <pjw@kernel.org>
Link: https://lore.kernel.org/r/20250909-pmu_event_info-v6-2-d8f80cacb884@rivosinc.com
Signed-off-by: Anup Patel <anup@brainfault.org>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-6.18-rc1
commit 190b74154299d210de7937d7325571fc3fa37383
category: feature
bugzilla: RVCK-Project#281

--------------------------------

SBI v3.0 introduced a new raw event type v2 for wider mhpmeventX
programming. Add the support in kvm for that.

Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Acked-by: Paul Walmsley <pjw@kernel.org>
Link: https://lore.kernel.org/r/20250909-pmu_event_info-v6-3-d8f80cacb884@rivosinc.com
Signed-off-by: Anup Patel <anup@brainfault.org>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-6.18-rc1
commit adffbd06d0036a1f2d8424ecc22d37ca38709012
category: feature
bugzilla: RVCK-Project#281

--------------------------------

With the new SBI PMU event info function, we can query the availability
of the all standard SBI PMU events at boot time with a single ecall.
This improves the bootime by avoiding making an SBI call for each
standard PMU event. Since this function is defined only in SBI v3.0,
invoke this only if the underlying SBI implementation is v3.0 or higher.

Signed-off-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Acked-by: Paul Walmsley <pjw@kernel.org>
Link: https://lore.kernel.org/r/20250909-pmu_event_info-v6-4-d8f80cacb884@rivosinc.com
Signed-off-by: Anup Patel <anup@brainfault.org>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-6.18-rc1
commit 880fcc329e2473ba02ffbc446fcd403972ab1fca
category: feature
bugzilla: RVCK-Project#281

--------------------------------

The event mapping function can be used in event info function to find out
the corresponding SBI PMU event encoding during the get_event_info function
as well. Refactor and export it so that it can be invoked from kvm and
internal driver.

Signed-off-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Acked-by: Paul Walmsley <pjw@kernel.org>
Link: https://lore.kernel.org/r/20250909-pmu_event_info-v6-5-d8f80cacb884@rivosinc.com
Signed-off-by: Anup Patel <anup@brainfault.org>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-6.18-rc1
commit 41f4d0cc331a1e9dc247ce22ae45f983f0ed9054
category: feature
bugzilla: RVCK-Project#281

--------------------------------

There is not much value in checking if a memslot is writable explicitly
before a write as it may change underneath after the check. Rather, return
invalid address error when write_guest fails as it checks if the slot
is writable anyways.

Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Acked-by: Paul Walmsley <pjw@kernel.org>
Link: https://lore.kernel.org/r/20250909-pmu_event_info-v6-6-d8f80cacb884@rivosinc.com
Signed-off-by: Anup Patel <anup@brainfault.org>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-6.18-rc1
commit e309fd113b9f6c34672a487be08fecb7e12e7e29
category: feature
bugzilla: RVCK-Project#281

--------------------------------

The new get_event_info funciton allows the guest to query the presence
of multiple events with single SBI call. Currently, the perf driver
in linux guest invokes it for all the standard SBI PMU events. Support
the SBI function implementation in KVM as well.

Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Acked-by: Paul Walmsley <pjw@kernel.org>
Link: https://lore.kernel.org/r/20250909-pmu_event_info-v6-7-d8f80cacb884@rivosinc.com
Signed-off-by: Anup Patel <anup@brainfault.org>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-6.18-rc1
commit dbdadd943a278fb8a24ae4199a668131108034b4
category: feature
bugzilla: RVCK-Project#281

--------------------------------

Upgrade the SBI version to v3.0 so that corresponding features
can be enabled in the guest.

Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
Acked-by: Paul Walmsley <pjw@kernel.org>
Link: https://lore.kernel.org/r/20250909-pmu_event_info-v6-8-d8f80cacb884@rivosinc.com
Signed-off-by: Anup Patel <anup@brainfault.org>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
mainline inclusion
from mainline-7.0-rc1
commit 1c0180cd42839fe7a60a61412b7c28cd43553c6b
category: feature
bugzilla: RVCK-Project#281

--------------------------------

If execution reaches "ret = 0" assignment in
kvm_riscv_vcpu_pmu_event_info() then it means
kvm_vcpu_write_guest() returned 0 hence ret is
already zero and does not need to be assigned 0.

Fixes: e309fd113b9f ("RISC-V: KVM: Implement get event info function")
Signed-off-by: Qiang Ma <maqianga@uniontech.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20251229072530.3075496-1-maqianga@uniontech.com
Signed-off-by: Anup Patel <anup@brainfault.org>
[sync from rvck.]
Signed-off-by: WeiHao Li <cn.liweihao@gmail.com>
@github-actions

github-actions Bot commented Jun 24, 2026

Copy link
Copy Markdown

开始测试 log: https://github.com/RVCK-Project/rvck-olk/actions/runs/28075008448

参数解析结果
args value
repository RVCK-Project/rvck-olk
head ref pull/241/head
base ref OLK-6.6
LAVA repo RVCK-Project/lavaci
LAVA hardware ['qemu']
LAVA Testcase path lava-testcases/common-test/ltp/ltp.yaml
need run job kunit-test,kernel-build,check-patch,lava-trigger

测试完成

详细结果:
check result
kunit-test failure
kernel-build success
check-patch success
lava-trigger-qemu success
lava-trigger-sg2042 skipped
lava-trigger-k1 skipped
lava-trigger-lpi4a skipped

Kunit Test Result

kunit test failed

Kernel Build Result

Check Patch Result

Total Errors 0
Total Warnings 15

LAVA Check (qemu)

args value
testcase_repo RVCK-Project/lavaci
lava_template lava-job-template/qemu/qemu-ltp.yaml
testcase_path lava-testcases/common-test/ltp/ltp.yaml
kernel_download_url http://10.30.190.110/openEuler-RISC-V/RVCK/OERV-RVCI/RVCK-Project/rvck-olk/241_28075008448_1/Image
initramfs_download_url http://10.30.190.110/openEuler-RISC-V/RVCK/OERV-RVCI/RVCK-Project/rvck-olk/241_28075008448_1/initramfs.img
rootfs_download_url https://fast-mirror.isrc.ac.cn/openeuler-sig-riscv/openEuler-RISC-V/RVCK/openEuler24.03-LTS-SP1/openeuler-rootfs.img.zst
testcase_ref main
testitem_name RVCK-Project_rvck-olk_pull_request_target_241__common-test_qemu

result: Lava check done!

@xmzzz
xmzzz merged commit 5f8da43 into RVCK-Project:OLK-6.6 Jul 7, 2026
51 of 53 checks passed
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.

6 participants