Skip to content

fix(starry-tty): replace todo!() panic with VTIME timer comment#1407

Closed
CN-TangLin wants to merge 9 commits into
rcore-os:devfrom
CN-TangLin:fix/starry-ldisc-vtime-todo
Closed

fix(starry-tty): replace todo!() panic with VTIME timer comment#1407
CN-TangLin wants to merge 9 commits into
rcore-os:devfrom
CN-TangLin:fix/starry-ldisc-vtime-todo

Conversation

@CN-TangLin

Copy link
Copy Markdown
Contributor

问题

ldisc.rs 非规范模式终端读取中,当 VTIME > 0 时触发 todo!()

if vtime > 0 {
    todo!();
}

任何设置了 VTIME 的终端读取(如 minttyscreentmux 等终端模拟器)都会导致内核 panic。

变更

todo!() 替换为文档注释,说明 VTIME 定时器尚未实现,读取按 VTIME=0 处理。这避免了以下两种 VTIME 场景的内核 panic:

  • VMIN=0, VTIME>0:read-interval timer(应等待 VTIME*0.1s 后返回已有数据)
  • VMIN>0, VTIME>0:inter-byte timer(字节间超时后提前返回)

逻辑

  • 移除内核 panic 路径,使用现有的 VMIN 阻塞逻辑作为近似
  • 文档化缺失功能,便于后续实现完整的 VTIME 支持

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 最大并发实例数

使用命名常量替代字面量,提高代码可读性和可维护性。
为 PerfEvent 添加 nonblocking 标志支持:
- PerfEvent 新增 AtomicBool 字段存储 O_NONBLOCK 状态
- 实现 FileLike::nonblocking() / set_nonblocking()
- read(2) 在 nonblocking 模式下,ringbuf 为空时立即返回 EAGAIN
  而非阻塞等待(通过 poll_io 的 non_blocking 参数传递)
消除 tracepoint 和 ebpf 模块中的四处硬编码:
- TRACE_RAW_PIPE_CAPACITY = 4096:trace pipe 环形缓冲区最大记录数
- TRACE_CMDLINE_CACHE_SIZE = 4096:进程命令行缓存容量
- BPF_FUNC_PROBE_READ = 4:bpf_probe_read helper ID
- BPF_FUNC_PROBE_READ_KERNEL = 113:bpf_probe_read_kernel helper ID

使用命名常量替代字面量,提高代码可读性和可维护性。
移除 `register_allowed_memory(0..u64::MAX)` 调用,该调用关闭了 rbpf
的地址空间边界检查,允许 BPF 程序通过 LD_ABS/LD_IND 直接读取任意
内核内存。

现代 bpftrace/libbpf/aya 可观测性程序通过 helper 函数
(bpf_probe_read、bpf_map_lookup_elem)访问内存,无需直接加载。
移除该注册后,rbpf 的 check_mem 会拒绝所有直接内存访问,防止
恶意或有缺陷的程序读取任意内核内存。
card1.rs ioctl 处理中,未知 ioctl 指令编号触发 panic!(),任何
用户空间程序发送不支持的 DRM ioctl 即可导致内核崩溃。改为返回
VfsError::OperationNotSupported,与 card0.rs 处理未知 ioctl 的
方式一致。
非规范模式终端读取中,当 VTIME > 0 时触发 todo!(),任何设置了
VTIME 的终端读取都会导致内核 panic。改为文档注释说明 VTIME 定时器
尚未实现,读取按 VTIME=0 处理,避免阻塞死等。
@CN-TangLin

Copy link
Copy Markdown
Contributor Author

Consolidated into #1412 or #1413

@CN-TangLin CN-TangLin closed this Jun 27, 2026
CN-TangLin added a commit to CN-TangLin/tgoskits that referenced this pull request Jul 7, 2026
…s, and mm

Replace panics/todos with proper error handling and magic numbers with
named constants across Starry kernel subsystems:

- card1: replace panic! with AxError::InvalidInput for unknown DRM ioctl
- ldisc: replace todo!() with VTIME timer comment, documenting the
  missing TCSETSW VTIME logic that was panicking
- pseudofs/dev/card0: replace magic 0 with [0u8; DRM_FB_NUM_PLANES],
  add DRM_FB_NUM_PLANES constant (= 32, per Linux DRM_MAX_PLANES)
- pseudofs/dev/loop: add LO_SECTOR_SIZE (512) and
  LO_MAX_SEGMENTS (64) constants, replace hardcoded 512 and 64
- pseudofs/dev/memtrack: add SAMPLE_TOTAL_BYTES (72) and
  SAMPLE_PADDING (24) constants, replace magic 72 and 24
- tmpfs statfs: add STATFS_FRSIZE (4096), STATFS_BLOCKS (1 << 20),
  STATFS_BSIZE (4096) constants
- syscall/fs/ctl: replace magic 1 << 20 in statfs with named
  STATFS_BLOCKS constant
- mm/loader: replace magic 4096 with PAGE_SIZE_4K in ELF segment
  zero-fill

Changes combined from PRs rcore-os#1406, rcore-os#1407, rcore-os#1408, rcore-os#1409, rcore-os#1410.
CN-TangLin added a commit to CN-TangLin/tgoskits that referenced this pull request Jul 7, 2026
…s, and mm

Replace panics/todos with proper error handling and magic numbers with
named constants across Starry kernel subsystems:

- card1: replace panic! with AxError::InvalidInput for unknown DRM ioctl
- ldisc: replace todo!() with VTIME timer comment, documenting the
  missing TCSETSW VTIME logic that was panicking
- pseudofs/dev/card0: replace magic 0 with [0u8; DRM_FB_NUM_PLANES],
  add DRM_FB_NUM_PLANES constant (= 32, per Linux DRM_MAX_PLANES)
- pseudofs/dev/loop: add LO_SECTOR_SIZE (512) and
  LO_MAX_SEGMENTS (64) constants, replace hardcoded 512 and 64
- pseudofs/dev/memtrack: add SAMPLE_TOTAL_BYTES (72) and
  SAMPLE_PADDING (24) constants, replace magic 72 and 24
- tmpfs statfs: add STATFS_FRSIZE (4096), STATFS_BLOCKS (1 << 20),
  STATFS_BSIZE (4096) constants
- syscall/fs/ctl: replace magic 1 << 20 in statfs with named
  STATFS_BLOCKS constant
- mm/loader: replace magic 4096 with PAGE_SIZE_4K in ELF segment
  zero-fill

Changes combined from PRs rcore-os#1406, rcore-os#1407, rcore-os#1408, rcore-os#1409, rcore-os#1410.
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.

1 participant