fix(starry-perf): replace magic numbers with named constants#1402
Closed
CN-TangLin wants to merge 4 commits into
Closed
fix(starry-perf): replace magic numbers with named constants#1402CN-TangLin wants to merge 4 commits into
CN-TangLin wants to merge 4 commits into
Conversation
Replace raw config values 0/1 in kprobe/uprobe dispatch with named constants PROBE_CONFIG_ENTRY / PROBE_CONFIG_RETURN, matching the Linux PERF_TYPE_PROBE ABI. Also refactor the kprobe config match from a nested if/else chain into flat const-pattern match arms.
为 BpfPerfEventWrapper 实现 try_read_record 方法,使 perf event fd 支持 read(2) 系统调用读取 ringbuf 中的 eBPF 输出记录。 主要变更: - bpf.rs: 添加 mmap_kvirt 字段保存 ringbuf 内核虚拟地址,实现 try_read_record 从 perf_event_mmap_page 读取 data_head/data_tail, 处理环缓冲区 wrapping,读取 perf_event_header 确定记录尺寸, 推进 data_tail;添加 wrapping_add/copy_ring 辅助函数 - mod.rs: 实现 FileLike::read,通过 downcast 获取 BpfPerfEventWrapper 并调用 try_read_record,使用 PAGE_SIZE_4K 作为栈缓冲区尺寸常量
将 PerfEvent::read 从轮询式非阻塞读重构为基于 block_on(poll_io(...)) 的阻塞读模式,匹配 Linux read(perf_fd) 语义。当 ringbuf 无数据时 返回 WouldBlock 让 poll_io 等待,数据到达后由已有的 BpfPerfEventWrapper poll 基础设施(poll_ready/poll_notify/IrqNotify) 唤醒任务继续读取。 重构要点: - 使用 block_on(poll_io(...)) 替代简单轮询,复用已有的 Pollable 实现 - read 闭包在返回 WouldBlock 前释放 SpinNoPreempt 锁 - 通过 PerfEvent::poll → BpfPerfEventWrapper::poll 获取可读性状态 - 通过 PerfEvent::register → BpfPerfEventWrapper::register 注册 waker
消除 perf 模块中的两处硬编码: - BPF_JIT_MEM_PAGES = 4:x86_64 JIT 可执行内存页数 - KRETPROBE_MAX_ACTIVE = 10:kretprobe 最大并发实例数 使用命名常量替代字面量,提高代码可读性和可维护性。
Contributor
Author
CN-TangLin
added a commit
to CN-TangLin/tgoskits
that referenced
this pull request
Jul 1, 2026
…pf/tracepoint Replace magic numbers with named constants and improve security in perf, ebpf, and tracepoint subsystems: - perf/kprobe: add PROBE_CONFIG_ENTRY/RETURN constants, refactor config match from if/else chain into const-pattern arms - perf/bpf: add BPF_JIT_MEM_PAGES constant, replace magic 4 with it - perf/bpf: remove vm.register_allowed_memory(0..u64::MAX) to restrict BPF direct memory access, preventing buggy/hostile programs from reading arbitrary kernel memory - perf/kprobe: add KRETPROBE_MAX_ACTIVE constant (10, per Linux max(10, 2*NR_CPUS) for single-CPU), replace magic 10 in builder - tracepoint/mod: add MAX_ACTIVE_TRACEPOINTS (16) and MAX_SLOTS_PER_TRACEPOINT (64) constants - ebpf/mod: add MAX_EXEC_MEM_PAGES (256) constant, replace magic 256 in JIT executable memory allocation Changes combined from PRs rcore-os#1399, rcore-os#1402, rcore-os#1404, rcore-os#1405.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
perf 模块中存在两处硬编码的数字字面量,降低了代码可读性和可维护性:
BPFJitMemory::new(4)—— JIT 可执行内存页数KretprobeBuilder::new(10)—— kretprobe 最大并发实例数变更
定义为命名常量并替换:
BPF_JIT_MEM_PAGES: usize = 4—— x86_64 BPF JIT 可执行内存页数,附带注释说明KRETPROBE_MAX_ACTIVE: u32 = 10—— 匹配 Linuxmax(10, 2*NR_CPUS)默认值逻辑
遵循"严禁硬编码"原则,使用命名常量替代字面量,提高代码可读性和可维护性。