RV内核增加 runtime const(运行时常量)机制#338
Conversation
|
开始测试 log: https://github.com/RVCK-Project/rvck/actions/runs/29714125637 参数解析结果
测试完成 详细结果:
Kunit Test Resultkunit test failed
Kernel Build Result
Check Patch Result
LAVA Check (qemu)
result: Lava check done!
|
|
开始测试 log: https://github.com/RVCK-Project/rvck/actions/runs/29724583548 参数解析结果
|
|
开始测试 log: https://github.com/RVCK-Project/rvck/actions/runs/29724858642 参数解析结果
测试完成 详细结果:
Kunit Test Result[07:35:46] Testing complete. Ran 482 tests: passed: 465, skipped: 17
Kernel Build Result
Check Patch Result
LAVA Check (qemu)
result: Lava check done!
|
mainline inclusion from mainline-6.12-rc6 commit 0c3beac category: feature bugzilla: RVCK-Project#333 -------------------------------- Several architectures support text patching, but they name the header files that declare patching functions differently. Make all such headers consistently named text-patching.h and add an empty header in asm-generic for architectures that do not support text patching. Link: https://lkml.kernel.org/r/20241023162711.2579610-4-rppt@kernel.org Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> Reviewed-by: Christoph Hellwig <hch@lst.de> Acked-by: Geert Uytterhoeven <geert@linux-m68k.org> # m68k Acked-by: Arnd Bergmann <arnd@arndb.de> Reviewed-by: Luis Chamberlain <mcgrof@kernel.org> Tested-by: kdevops <kdevops@lists.linux.dev> Cc: Andreas Larsson <andreas@gaisler.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Ard Biesheuvel <ardb@kernel.org> Cc: Borislav Petkov (AMD) <bp@alien8.de> Cc: Brian Cain <bcain@quicinc.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Christophe Leroy <christophe.leroy@csgroup.eu> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: Dinh Nguyen <dinguyen@kernel.org> Cc: Guo Ren <guoren@kernel.org> Cc: Helge Deller <deller@gmx.de> Cc: Huacai Chen <chenhuacai@kernel.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Johannes Berg <johannes@sipsolutions.net> Cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de> Cc: Kent Overstreet <kent.overstreet@linux.dev> Cc: Liam R. Howlett <Liam.Howlett@Oracle.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org> Cc: Matt Turner <mattst88@gmail.com> Cc: Max Filippov <jcmvbkbc@gmail.com> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Michal Simek <monstr@monstr.eu> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Palmer Dabbelt <palmer@dabbelt.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Richard Weinberger <richard@nod.at> Cc: Russell King <linux@armlinux.org.uk> Cc: Song Liu <song@kernel.org> Cc: Stafford Horne <shorne@gmail.com> Cc: Steven Rostedt (Google) <rostedt@goodmis.org> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Uladzislau Rezki (Sony) <urezki@gmail.com> Cc: Vineet Gupta <vgupta@kernel.org> Cc: Will Deacon <will@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
mainline inclusion from mainline-6.10-rc5 commit e60cc61 category: feature bugzilla: RVCK-Project#333 -------------------------------- Both __d_lookup_rcu() and __d_lookup_rcu_op_compare() have the full 'name_hash' value of the qstr that they want to look up, and mask it off to just the low 32-bit hash before calling down to d_hash(). Other callers just load the 32-bit hash and pass it as the argument. If we move the masking into d_hash() itself, it simplifies the two callers that currently do the masking, and is a no-op for the other cases. It doesn't actually change the generated code since the compiler will inline d_hash() and see that the end result is the same. [ Technically, since the parse tree changes, the code generation may not be 100% the same, and for me on x86-64, this does result in gcc switching the operands around for one 'cmpl' instruction. So not necessarily the exact same code generation, but equivalent ] However, this does encapsulate the 'd_hash()' operation more, and makes the shift operation in particular be a "shift 32 bits right, return full word". Which matches the instruction semantics on both x86-64 and arm64 better, since a 32-bit shift will clear the upper bits. That makes the next step of introducing a "shift by runtime constant" more obvious and generates the shift with no extraneous type masking. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
mainline inclusion from mainline-6.6-rc7 commit 68279f9 category: feature bugzilla: RVCK-Project#333 -------------------------------- __read_mostly predates __ro_after_init. Many variables which are marked __read_mostly should have been __ro_after_init from day 1. Also, mark some stuff as "const" and "__init" while I'm at it. [akpm@linux-foundation.org: revert sysctl_nr_open_min, sysctl_nr_open_max changes due to arm warning] [akpm@linux-foundation.org: coding-style cleanups] Link: https://lkml.kernel.org/r/4f6bb9c0-abba-4ee4-a7aa-89265e886817@p183 Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
mainline inclusion from mainline-6.10-rc5 commit e782985 category: feature bugzilla: RVCK-Project#333 -------------------------------- This adds the initial dummy support for 'runtime constants' for when an architecture doesn't actually support an implementation of fixing up said runtime constants. This ends up being the fallback to just using the variables as regular __ro_after_init variables, and changes the dcache d_hash() function to use this model. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
mainline inclusion from mainline-6.11-rc2 commit b6547e5 category: feature bugzilla: RVCK-Project#333 -------------------------------- The runtime constants linker script depended on documented linker behavior [1]: "If an output section’s name is the same as the input section’s name and is representable as a C identifier, then the linker will automatically PROVIDE two symbols: __start_SECNAME and __stop_SECNAME, where SECNAME is the name of the section. These indicate the start address and end address of the output section respectively" to just automatically define the symbol names for the bounds of the runtime constant arrays. It turns out that this isn't actually something we can rely on, with old linkers not generating these automatic symbols. It looks to have been introduced in binutils-2.29 back in 2017, and we still support building with versions all the way back to binutils-2.25 (from 2015). And yes, Oleg actually seems to be using such ancient versions of binutils. So instead of depending on the implicit symbols from "section names match and are representable C identifiers", just do this all manually. It's not like it causes us any extra pain, we already have to do that for all the other sections that we use that often have special characters in them. Reported-and-tested-by: Oleg Nesterov <oleg@redhat.com> Link: https://sourceware.org/binutils/docs/ld/Input-Section-Example.html [1] Link: https://lore.kernel.org/all/20240802114518.GA20924@redhat.com/ Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
mainline inclusion from mainline-6.11-rc5 commit 92a10d3 category: feature bugzilla: RVCK-Project#333 -------------------------------- Refactor the list of constant variables into a macro. This should make it easier to add more constants in the future. Signed-off-by: Jann Horn <jannh@google.com> Reviewed-by: Alexander Gordeev <agordeev@linux.ibm.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: Arnd Bergmann <arnd@arndb.de> Acked-by: Will Deacon <will@kernel.org> Signed-off-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
mainline inclusion from mainline-6.18-rc6 commit 21b561d category: feature bugzilla: RVCK-Project#333 -------------------------------- Signed-off-by: Mateusz Guzik <mjguzik@gmail.com> Link: https://patch.msgid.link/20251105153622.758836-1-mjguzik@gmail.com Signed-off-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
mainline inclusion from mainline-7.0-rc7 commit 1f1651d category: feature bugzilla: RVCK-Project#333 -------------------------------- s/cachep/cache/ for consistency with namei and dentry caches. Signed-off-by: Mateusz Guzik <mjguzik@gmail.com> Link: https://patch.msgid.link/20260328173728.3388070-1-mjguzik@gmail.com Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
mainline inclusion from mainline-6.14 commit afa8a93 category: feature bugzilla: RVCK-Project#333 -------------------------------- We have duplicated the definition of the nop instruction in ftrace.h and in jump_label.c. Move this definition into the generic file insn-def.h so that they can share the definition with each other and with future files. Signed-off-by: Charlie Jenkins <charlie@rivosinc.com> Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com> Tested-by: Alexandre Ghiti <alexghiti@rivosinc.com> Reviewed-by: Andrew Jones <ajones@ventanamicro.com> Link: https://lore.kernel.org/r/20250319-runtime_const_riscv-v10-1-745b31a11d65@rivosinc.com Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
|
开始测试 log: https://github.com/RVCK-Project/rvck/actions/runs/29795442215 参数解析结果
测试完成 详细结果:
Kunit Test Result[02:21:37] Testing complete. Ran 482 tests: passed: 465, skipped: 17
Kernel Build Result
Check Patch Result
LAVA Check (qemu)
result: Lava check done!
|
|
@uestc-gr 请问如何测试这个 PR?我手头没有可以支持 Zba/Zbp/Zbkb 的硬件,是否可以使用 QEMU 来验证? |
使用高版本的qemu启动一个tcg虚拟机就可以测试,-cpu max 打开所有支持的硬件特性 |
mainline inclusion from mainline-6.14 commit a44fb57 category: feature bugzilla: RVCK-Project#333 -------------------------------- Implement the runtime constant infrastructure for riscv. Use this infrastructure to generate constants to be used by the d_hash() function. This is the riscv variant of commit 94a2bc0 ("arm64: add 'runtime constant' support") and commit e3c92e8 ("runtime constants: add x86 architecture support"). [ alex: Remove trailing whitespace ] Signed-off-by: Charlie Jenkins <charlie@rivosinc.com> Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com> Tested-by: Alexandre Ghiti <alexghiti@rivosinc.com> Link: https://lore.kernel.org/r/20250319-runtime_const_riscv-v10-2-745b31a11d65@rivosinc.com Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
mainline inclusion from mainline-6.16-rc2 commit 8d90d98 category: feature bugzilla: RVCK-Project#333 -------------------------------- the `__runtime_fixup_32` function does not handle the case where `val` is zero correctly (as might occur when patching a nommu kernel and referring to a physical address below the 4GiB boundary whose upper 32 bits are all zero) because nothing in the existing logic prevents the code from taking the `else` branch of both nop-checks and emitting two `nop` instructions. This leaves random garbage in the register that is supposed to receive the upper 32 bits of the pointer instead of zero that when combined with the value for the lower 32 bits yields an invalid pointer and causes a kernel panic when that pointer is eventually accessed. The author clearly considered the fact that if the `lui` is converted into a `nop` that the second instruction needs to be adjusted to become an `li` instead of an `addi`, hence introducing the `addi_insn_mask` variable, but didn't follow that logic through fully to the case where the `else` branch executes. To fix it just adjust the logic to ensure that the second `else` branch is not taken if the first instruction will be patched to a `nop`. Fixes: a44fb57 ("riscv: Add runtime constant support") Signed-off-by: Charles Mirabile <cmirabil@redhat.com> Reviewed-by: Charlie Jenkins <charlie@rivosinc.com> Tested-by: Charlie Jenkins <charlie@rivosinc.com> Link: https://lore.kernel.org/r/20250530211422.784415-2-cmirabil@redhat.com Signed-off-by: Palmer Dabbelt <palmer@dabbelt.com> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
mainline inclusion from mainline-6.11-rc6 commit 04c8aba category: feature bugzilla: RVCK-Project#333 -------------------------------- The runtime constant feature removes all the users of these variables, allowing the compiler to optimize them away. It's quite difficult to extract their values from the kernel text, and the memory saved by removing them is tiny, and it was never the point of this optimization. Since the dentry_hashtable is a core data structure, it's valuable for debugging tools to be able to read it easily. For instance, scripts built on drgn, like the dentrycache script[1], rely on it to be able to perform diagnostics on the contents of the dcache. Annotate it as used, so the compiler doesn't discard it. Link: https://github.com/oracle-samples/drgn-tools/blob/3afc56146f54d09dfd1f6d3c1b7436eda7e638be/drgn_tools/dentry.py#L325-L355 [1] Fixes: e3c92e8 ("runtime constants: add x86 architecture support") Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
mainline inclusion from mainline-6.14 commit 6ee9281 category: feature bugzilla: RVCK-Project#333 -------------------------------- .option arch clobbers .option norvc. Prevent gas from emitting compressed instructions in the runtime const alternative blocks by setting .option norvc after .option arch. This issue starts appearing on gcc 15, which adds zca to the march. Reported by: Klara Modin <klarasmodin@gmail.com> Signed-off-by: Charlie Jenkins <charlie@rivosinc.com> Fixes: a44fb57 ("riscv: Add runtime constant support") Closes: https://lore.kernel.org/all/cc8f3525-20b7-445b-877b-2add28a160a2@gmail.com/ Tested-by: Klara Modin <klarasmodin@gmail.com> Link: https://lore.kernel.org/r/20250331-fix_runtime_const_norvc-v1-1-89bc62687ab8@rivosinc.com Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
|
开始测试 log: https://github.com/RVCK-Project/rvck/actions/runs/29814991728 参数解析结果
测试完成 详细结果:
Kunit Test Result[08:41:28] Testing complete. Ran 482 tests: passed: 465, skipped: 17
Kernel Build Result
Check Patch Result
LAVA Check (qemu)
result: Lava check done!
|
|
PR已完成,请老师评审 |
unicornx
left a comment
There was a problem hiding this comment.
1st review
问题 1
- runtime constants: add default dummy infrastructure
- runtime constants: deal with old decrepit linkers
我发现这两个 commit 的改动和上游补丁的改动存在一些差别,主要是在新增代码中,比上游补丁多一些空格。譬如
#define runtime_const_init(type, sym) do { } while (0) 中 sym 之前没有空格,但是你这多了一个空格。
可以通过 git show > A.patch 和 git show > B.patch, 然后对比 A 和 B 即可看到差别。虽然你这么写比较好,如无特殊原因请保持和上游补丁改动一致。
请关注增加的部分(+ 开头的部分)
问题 2
- runtime constants: move list of constants to vmlinux.lds.h
为何没有合入原上游补丁中针对 arm64/s390/x86 部分的改动?
如果的确需要修改适配 6.6,请在 commit 的 message 中加以说明, 添加的位置如下所示
runtime constants: move list of constants to vmlinux.lds.h
mainline inclusion
from mainline-6.11-rc5
commit 92a10d3861491d09e73b35db7113dd049659f588
category: feature
bugzilla: https://github.com/RVCK-Project/rvck/issues/333
额外的修改说明加在这里
--------------------------------
Refactor the list of constant variables into a macro.
This should make it easier to add more constants in the future.
Signed-off-by: Jann Horn <jannh@google.com>
Reviewed-by: Alexander Gordeev <agordeev@linux.ibm.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Acked-by: Will Deacon <will@kernel.org>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
问题 3
- fs: hide file and bfile caches behind runtime const machinery
请对 pick 上游补丁时没有摘取的部分添加修改说明(__bfilp_cache 部分,6.6 中缺少什么导致这部分我们不能 pick 云云),方便以后回溯
问题 4
另外 rvck 有需求需要确保合入的 PR 除了 rv64 外至少不要影响到 x86_64 和 arm64(正常启动吧),我看你这个 pr 大量修改了跨 arch 的改动,不知道对 x86_64 和 arm64 的影响有多大,请问您评估过吗?
|
更新时请注意及时 rebase 最新的 rvck-6.6 分支,因为它一直在升级。你这个 pr 改动文件较多,所以请及时同步,谢谢 |
问题 1 |
我觉得对于这种情况还是与上游一致吧,毕竟我们是 backport 而且这里的空格问题也不是为了fix 6.6 和上游主线的兼容性问题
参考问题 4,兼容 x86_64 和 arm64 是最近 rvck 的新需求,所以从现在开始,不是 riscv-only 了。@sterling-teng
这里你的回答里有乱码字符,请修改一下。
比较好奇,因为目前 rvck 的 rvck-6.6 我本地试过 x86_64_defconfig 并没有编译过(有报错),请问你那边是怎么测试的呢? |
问题3 添加变动的commit的位置和之前的要求不一致,现在是以此为准吗 |
|
fixed: #333
在内核中,很多 全局变量或指针(如 d_hash_shift、dentry_hashtable)会被频繁访问。
传统方式:编译器需要通过符号解析,把这些变量的地址加载到寄存器,通常需要多条指令。
RISC-V 新增的 Zba/Zbp/Zbkb 扩展指令可以更高效地组合常量地址,但需要一种机制把这些符号和指令序列绑定起来。
runtime const 就是为此设计的:通过链接脚本生成锚点符号,再在运行时用 fixup 修复,把符号地址和常量区间关联。
测试过程
1、编译内核后,启动新内核发现dentry_cache等变量已转换为runtime const,且系统长时间运行无故障
~ # cat /proc/kallsyms | grep _runtime_ptr
ffffffff80e7f230 R __start_runtime_ptr_dentry_hashtable
ffffffff80e7f244 R __stop_runtime_ptr_dentry_hashtable
ffffffff80e7f248 R __start_runtime_ptr___dentry_cache
ffffffff80e7f25c R __stop_runtime_ptr___dentry_cache
ffffffff80e7f260 R __start_runtime_ptr___filp_cache
ffffffff80e7f274 R __stop_runtime_ptr___filp_cache
2、性能比较,使用测试代码,针对某个文件系统的文件进行遍历
stat_dfs.c
补丁前
~ # /home/stat_dfs /home/
stat 文件数: 220088
总耗时: 14490.120 ms
补丁后
~ # /home/stat_dfs /home/
stat 文件数: 220088
总耗时: 13749.695 ms