Skip to content

Conversation

@opsiff
Copy link
Member

@opsiff opsiff commented Jan 14, 2026

chenhuacai and others added 3 commits January 14, 2026 13:34
We need to switch SFB (Store Fill Buffer) and TSO (Total Store Order)
state at runtime to debug memory management and KVM virtualization, so
add two debugfs entries "sfb_state" and "tso_state" under the directory
/sys/kernel/debug/loongarch.

Query SFB:
cat /sys/kernel/debug/loongarch/sfb_state

Enable SFB:
echo 1 > /sys/kernel/debug/loongarch/sfb_state

Disable SFB:
echo 0 > /sys/kernel/debug/loongarch/sfb_state

Query TSO:
cat /sys/kernel/debug/loongarch/tso_state

Switch TSO:
echo [TSO] > /sys/kernel/debug/loongarch/tso_state

Available [TSO] states:
0 (No Load No Store)    1 (All Load No Store)   3 (Same Load No Store)
4 (No Load All Store)   5 (All Load All Store)  7 (Same Load All Store)

Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
(cherry picked from commit 04816c1)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
Some VMMs provides special hypercall service in usermode, KVM should not
handle the usermode hypercall service, thus pass it to usermode, let the
usermode VMM handle it.

Here a new code KVM_HCALL_CODE_USER_SERVICE is added for the user-mode
hypercall service, KVM lets all six registers visible to usermode VMM.

Signed-off-by: Bibo Mao <maobibo@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
(cherry picked from commit 2737dee)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
LLBCTL is a separated guest CSR register from host, host exception ERET
instruction will clear the host LLBCTL CSR register, and guest exception
will clear the guest LLBCTL CSR register.

VCPU0 atomic64_fetch_add_unless     VCPU1 atomic64_fetch_add_unless
     ll.d    %[p],  %[c]
     beq     %[p],  %[u], 1f

Here secondary mmu mapping is changed, host hpa page is replaced with a
new page. And VCPU1 will execute atomic instruction on the new page.

                                       ll.d    %[p],  %[c]
                                       beq     %[p],  %[u], 1f
                                       add.d   %[rc], %[p], %[a]
                                       sc.d    %[rc], %[c]
     add.d   %[rc], %[p], %[a]
     sc.d    %[rc], %[c]

LLBCTL is set on VCPU0 and it represents the memory is not modified by
other VCPUs, sc.d will modify the memory directly.

So clear WCLLB of the guest LLBCTL register when mapping is the changed.

Signed-off-by: Bibo Mao <maobibo@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
(cherry picked from commit 4d38d04)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @opsiff, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from opsiff. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码主要是为 LoongArch 架构的 KVM 添加了用户态超调用(user hypercall)支持、内存排序(memory ordering)相关的 debugfs 接口,以及修复了 LL/SC 指令在 vCPU 迁移时的潜在问题。以下是对代码的详细审查和改进建议:

1. 代码逻辑与功能审查

1.1 用户态超调用支持 (KVM_FEATURE_USER_HCALL)

位置: arch/loongarch/kvm/exit.c 和相关头文件

  • 功能: 允许 Guest OS 通过 KVM_HCALL_USER_SERVICE 发起超调用,该调用会退出到用户态 VMM (如 QEMU) 处理。
  • 问题:
    • kvm_handle_hypercall 中对 KVM_HCALL_USER_SERVICE 的处理逻辑清晰,但缺少对参数有效性的基本校验。
    • kvm_complete_user_service 直接将 run->hypercall.ret 写入 A0 寄存器,如果用户态 VMM 未正确设置返回值,可能导致 Guest 行为异常。

改进建议:

  • kvm_handle_hypercall 中增加对 args 数组的边界检查(虽然目前代码只使用了 args[0]args[5],但显式检查更安全)。
  • kvm_complete_user_service 中添加对 run->hypercall.ret 的有效性校验,例如检查是否为 KVM_HCALL_INVALID_CODE

1.2 内存排序的 debugfs 接口 (kdebugfs.c)

位置: arch/loongarch/kernel/kdebugfs.c

  • 功能: 通过 debugfs 提供 sfb_statetso_state 接口,用于在运行时动态配置内存填充和排序策略。
  • 问题:
    • sfb_readtso_read 中使用固定大小的缓冲区 (char str[32]char str[240]),如果未来格式化字符串变长,可能导致缓冲区溢出。
    • tso_hints 数组的大小为 8,但 tso_write 中只允许写入 0, 1, 3, 4, 5, 7,未处理 2 和 6 的情况(虽然标记为 "Invalid Config",但代码中未明确拒绝)。

改进建议:

  • 使用 snprintf 的返回值检查缓冲区是否足够,或动态分配内存。
  • tso_write 中显式拒绝 2 和 6 的输入,即使它们在 tso_hints 中标记为无效。

1.3 LL/SC 指令的 vCPU 迁移问题修复

位置: arch/loongarch/kvm/main.c

  • 功能: 在 vCPU 迁移到新物理 CPU 时,清除 Guest 的 LLBCTL_WCLLB 标志,避免 LL/SC 指令因内存映射变化而错误成功。
  • 问题:
    • 注释解释清晰,但代码逻辑依赖于 set_gcsr_llbctl 的实现,需确保该函数正确设置 CSR_LLBCTL_WCLLB

改进建议:

  • 确认 set_gcsr_llbctl 的实现是否原子操作,避免并发问题。
  • 考虑在 kvm_check_vpid 中添加日志或 tracepoint,便于调试 vCPU 迁移问题。

2. 代码质量与可维护性

2.1 魔法数字和宏定义

位置: arch/loongarch/include/asm/loongarch.h

  • 问题: CSR_LDSTORDER_* 宏定义的值(如 0x0, 0x1, 0x3 等)是硬编码的,缺乏语义化命名。
  • 改进建议:
    • 为这些值定义更具描述性的宏,例如:
      #define CSR_LDSTORDER_MODE_NONE        0x0
      #define CSR_LDSTORDER_MODE_ALL_LOAD    0x1
      #define CSR_LDSTORDER_MODE_SAME_LOAD   0x3

2.2 错误处理

位置: arch/loongarch/kernel/kdebugfs.c

  • 问题: sfb_writetso_write 中对 kstrtoint_from_user 的错误处理返回 -EFAULT,但 kstrtoint_from_user 失败时可能返回其他错误码(如 -EINVAL)。
  • 改进建议:
    • 直接返回 kstrtoint_from_user 的错误码,而不是统一返回 -EFAULT

3. 性能优化

3.1 debugfs 的读写性能

位置: arch/loongarch/kernel/kdebugfs.c

  • 问题: sfb_readtso_read 每次调用都会重新格式化字符串,即使内容未变化。
  • 改进建议:
    • 如果状态不常变化,可以缓存格式化后的字符串,仅在状态变化时更新。

3.2 vCPU 迁移的开销

位置: arch/loongarch/kvm/main.c

  • 问题: kvm_check_vpid 中新增的 set_gcsr_llbctl 调用会增加 vCPU 迁移的开销。
  • 改进建议:
    • 评估 set_gcsr_llbctl 的性能影响,必要时优化其实现。

4. 安全性

4.1 debugfs 的权限控制

位置: arch/loongarch/kernel/kdebugfs.c

  • 问题: sfb_statetso_state 的权限为 S_IRUGO | S_IWUSR,允许所有用户读取,但仅 root 可写。
  • 改进建议:
    • 如果这些接口可能影响系统稳定性,建议进一步限制权限(如仅 root 可读写)。

4.2 超调用的参数校验

位置: arch/loongarch/kvm/exit.c

  • 问题: KVM_HCALL_USER_SERVICE 的参数直接从 Guest 寄存器读取,未校验其合法性。
  • 改进建议:
    • 增加对 args 数组的边界检查,避免越界访问。

5. 其他建议

5.1 文档和注释

  • 问题: 部分新增功能(如 KVM_FEATURE_USER_HCALL)缺乏详细文档。
  • 改进建议:
    • 在头文件或文档中说明 KVM_FEATURE_USER_HCALL 的用途和参数格式。
    • kdebugfs.c 中的接口添加使用示例。

5.2 测试覆盖

  • 建议:
    • 为新增的 KVM_HCALL_USER_SERVICE 和 debugfs 接口编写单元测试和集成测试。
    • 验证 LL/SC 修复在不同 vCPU 迁移场景下的正确性。

总结

这段代码的功能实现较为清晰,但在错误处理、参数校验和性能优化方面仍有改进空间。建议重点关注安全性问题(如超调用的参数校验)和性能优化(如 debugfs 的缓存机制)。

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request synchronizes upstream KVM patches for the LoongArch architecture in Linux 6.12. The changes add support for user-space hypercall handling, fix LLBCTL register handling for atomic operations across VCPU migrations, and consolidate debugfs infrastructure.

Changes:

  • Add support for user-space hypercall handling (KVM_HCALL_USER_SERVICE) allowing VMMs to handle custom hypercalls
  • Fix LLBCTL register clearing on VCPU migration to prevent LL/SC pair issues with changed MMU mappings
  • Consolidate debugfs directory creation into a centralized kdebugfs.c module

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
arch/loongarch/kvm/vcpu.c Convert if statement to switch for handling multiple exit reasons including new KVM_EXIT_HYPERCALL
arch/loongarch/kvm/main.c Add LLBCTL register clearing on VCPU migration to fix atomic operation issues
arch/loongarch/kvm/exit.c Implement user-space hypercall handler and completion function
arch/loongarch/kernel/unaligned.c Update to use centralized arch_debugfs_dir instead of local directory
arch/loongarch/kernel/kdebugfs.c New centralized debugfs infrastructure with SFB and TSO state controls
arch/loongarch/kernel/Makefile Add kdebugfs.o to build system
arch/loongarch/include/uapi/asm/kvm_para.h Define KVM_FEATURE_USER_HCALL feature flag
arch/loongarch/include/asm/loongarch.h Add CPU config bits and CSR definitions for TSO ordering
arch/loongarch/include/asm/kvm_vcpu.h Declare kvm_complete_user_service function
arch/loongarch/include/asm/kvm_para.h Define KVM_HCALL_USER_SERVICE hypercall code
arch/loongarch/include/asm/kvm_host.h Add KVM_FEATURE_USER_HCALL to feature mask

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 485 to +488
debugfs_create_u32("unaligned_instructions_user",
S_IRUGO, d, &unaligned_instructions_user);
S_IRUGO, arch_debugfs_dir, &unaligned_instructions_user);
debugfs_create_u32("unaligned_instructions_kernel",
S_IRUGO, d, &unaligned_instructions_kernel);
S_IRUGO, arch_debugfs_dir, &unaligned_instructions_kernel);
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable arch_debugfs_dir is used here but is not declared in the current file or included headers. Since this variable is defined and exported in kdebugfs.c (added in this PR), it needs an extern declaration, likely in an appropriate header file (such as asm/loongarch.h or a new debugfs-related header) so that this file can use it. Without the declaration, this will cause a compilation error.

Copilot uses AI. Check for mistakes.
ret = RESUME_HOST;
break;
case KVM_HCALL_SWDBG:
/* KVM_HCALL_SWDBG only in effective when SW_BP is enabled */
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment contains a grammatical error. "in effective" should be "effective" or "in effect".

Suggested change
/* KVM_HCALL_SWDBG only in effective when SW_BP is enabled */
/* KVM_HCALL_SWDBG only effective when SW_BP is enabled */

Copilot uses AI. Check for mistakes.
@opsiff opsiff merged commit 3ee2008 into deepin-community:linux-6.12.y Jan 14, 2026
10 of 13 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.

4 participants