Repository navigation
fix(ptyfs): release the pts file on unregister - #12029
BernardXiong wants to merge 2 commits into
Conversation
ptyfs_unregister_pts() removed the pts file from the root list and recycled its unit number but never freed the allocation. Repeated open/close of /dev/pts/ptmx therefore leaked one struct ptyfs_file per cycle and eventually exhausted the kernel heap. Free the file after unlinking it. Also initialize pts_unit to -1 so a failed registration does not unregister unit 0, detach the condvars before freeing the softc on the lwp_tty_create() failure path, and skip rt_device_unregister() for a tty that was never registered.
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: componentsReviewers: @Maihuanyi Changed Files (Click to expand)
🏷️ Tag: components_lwpReviewers: @xu18838022837 Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-11-14 00:16 CST)
📝 Review Instructions
|
|
Rbb666
left a comment
There was a problem hiding this comment.
释放遗漏的 pts 文件和失败路径修正方向正确,但新增释放与现有目录读取/属性操作之间缺少生命周期同步,会引入并发访问已释放对象的问题。请先处理下方行内意见,并补充目录遍历与 PTY 创建/关闭并发的验证。
另一个非阻塞建议:标题及提交标题建议统一为 [dfs][ptyfs] Release the pts file on unregister。
本次验证为 diff 与调用链静态核对,未进行板级或 QEMU 并发复现。
| rid_bitmap_put(ptsno_pool, ptsno); | ||
| /* the file is no longer reachable from the root list */ | ||
| rt_free(pts_file); | ||
| rc = 0; |
There was a problem hiding this comment.
[P1] 释放 pts_file 前需要同步目录读者的生命周期
这里新增 rt_free(pts_file) 后,仅在 ptyfile_remove_from_root() 中短暂持有 sb->lock 不足以保证没有读者持有这个对象。devpty_deffops_getdents() 在不持有该锁的情况下遍历 root_file.subdirs 并读取 n_file->type/basename;另一个线程关闭 PTY 后可经 ptsdrv_free() 删除并释放同一个节点,目录遍历随后就会访问已释放内存。ptyfile_lookup() 也在返回裸指针前释放锁,ptyfs_ops_setattr() 等调用方仍会在返回后访问它。DFS 外层的 dfs_file_getdents() 和 dfs_file_setattr() 没有与关闭路径共享的全局锁来消除这个窗口。
请让查找/遍历后的使用与摘链释放由同一把锁覆盖,或为 ptyfs_file 引入明确的引用生命周期;仅把 free 移进 remove 的锁区仍不能保护已经取得指针的读者。建议增加一个线程反复打开/关闭 ptmx、另一个线程遍历 /dev/pts(并执行属性查询/修改)的并发验证。
ptyfile_lookup() returned a raw pointer after releasing sb->lock, while ptyfs_unregister_pts() removed and freed the same node. A concurrent getdents(), setattr() or vnode lookup could then read freed memory. Add a reference count protected by sb->lock: lookup takes a reference and the last ptyfile_put() frees the node. The root list reference is dropped when the file is removed, so unregistering a pts no longer frees a file that a reader still uses. getdents() also holds the lock while walking the subdir list, keeping the entries alive during the copy.
|
已按 [P1] 意见补上生命周期同步,推送到
验证: English: lifecycle synchronization added in |
Summary
ptyfs_unregister_pts()removes the pts file from the root list and recycles its unit number, but never frees thestruct ptyfs_fileallocated byptyfs_register_pts(). Because the unit number is recycled, repeatedopen()/close()of/dev/pts/ptmxleaks one file per cycle without hitting the live-pair limit.The failure path also had two problems:
pts_unitwas zero afterrt_calloc(), so a failed registration made the free path unregister unit 0.lwp_tty_delete()unconditionally calledrt_device_unregister()on a tty that had never been registered whenptyfs_register_pts()failed, corrupting the object list in the OOM path.lwp_tty_create()failure, the softc was freed before its condvars were detached.Fix
pts_unitto-1so the free path only unregisters a real unit.lwp_tty_delete(), only unregister the device if the object was actually registered as a device.Verification
Built
bsp/qemu-virt64-aarch64withRT_USING_SMARTandRT_USING_DFS_PTYFS(aarch64 musl GCC 12.2.0), booted the ext4 rootfs under QEMU, mounted ptyfs and ran 20,000: < /dev/pts/ptmxopen/close cycles:available245164 KB -> 237932 KB (-7232 KB, about 369 bytes/cycle).available245156 KB -> 245116 KB (-40 KB).The test ran from the boot shell (root), not from an unprivileged process, and the OOM crash path itself was not forced.
Fixes #12023.