Skip to content

fix(mm): 修复匿名共享页跨进程共享问题#1334

Merged
fslongjin merged 1 commit intoDragonOS-Community:masterfrom
fslongjin:fix-shared-anon-page-fault
Nov 3, 2025
Merged

fix(mm): 修复匿名共享页跨进程共享问题#1334
fslongjin merged 1 commit intoDragonOS-Community:masterfrom
fslongjin:fix-shared-anon-page-fault

Conversation

@fslongjin
Copy link
Member

@fslongjin fslongjin commented Nov 2, 2025

  • 修复 MAP_SHARED|ANON 语义,确保匿名共享页在跨进程间真正共享, 避免了由于匿名共享页面在尚未fault的时候,fork, 接着父子进程分别为这一区域分配了不同的物理页面.
  • 为 AnonSharedMapping 添加页面缓存,避免重复分配同一页面
  • 实现原子性的页面获取或创建机制,防止竞态条件

现象: ForkTest.SharedMapping 中子进程读取到 0(预期为 1)
根因: 匿名共享映射首次缺页为各进程各自分配零页,缺少共享的后备页对象,导致 fork 后父子进程不共享同一物理页
方案:

  • 在 VMA 的 AnonSharedMapping 中引入按页偏移的共享页表(Weak),提供稳定身份并可跨进程共享
  • 新增 get_or_create_page(pgoff),在持有自旋锁的情况下“检查-分配-登记”同一页,避免并发下双重分配竞态
  • do_anonymous_page:对 MAP_SHARED|ANONYMOUS 缺页优先从共享表获取并 map_phys 同一物理页;非共享匿名保持原逻辑

并发安全: 在 map 锁内分配与登记,确保原子;使用 Weak 避免强引用导致的泄漏,过期项会被清理

语义: 与 Linux 对 MAP_SHARED|ANON 的共享行为一致;futex 使用的稳定身份保持不变

影响范围: 仅影响匿名共享首次缺页路径;对 MAP_PRIVATE 和文件映射无破坏性影响


Note

Use a shared anon backing with per-page cache and atomic get-or-create to map the same physical page for VM_SHARED|ANON faults, falling back to private anon mapping otherwise.

  • MM Fault Handling
    • In do_anonymous_page, detect VM_SHARED anonymous VMAs, compute per-VMA pgoff, atomically get_or_create_page from AnonSharedMapping, and map_phys the shared page; otherwise fallback to map for private anon.
  • VMA/Mapping Infrastructure
    • Add AnonSharedMapping in ucontext.rs with per-page HashMap<usize, Weak<Page>> cache and get_or_create_page(pgoff) allocating via LockedFrameAllocator to avoid double allocation races.
    • On map_anonymous, initialize VMA.shared_anon when VmFlags::VM_SHARED.
  • Imports/Types
    • Wire up required types (Page, PageFlags, PageType, HashMap, LockedFrameAllocator) for the shared-page mechanism.

Written by Cursor Bugbot for commit 2c60d25. This will update automatically on new commits. Configure here.

@fslongjin fslongjin requested review from Copilot and sparkzky November 2, 2025 14:51
@github-actions github-actions bot added Bug fix A bug is fixed in this pull request test Unitest/User space test labels Nov 2, 2025
Copy link
Contributor

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 PR implements shared page caching for anonymous shared mappings (MAP_SHARED | MAP_ANONYMOUS) to prevent duplicate allocations and ensure proper page sharing across forked processes. The implementation uses a per-mapping HashMap with weak references to track allocated pages.

  • Adds a page cache to AnonSharedMapping using weak references to prevent memory leaks
  • Implements atomic get_or_create_page method to prevent race conditions during concurrent page faults
  • Updates page fault handler to use shared backing for anonymous shared mappings

Reviewed Changes

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

File Description
kernel/src/mm/ucontext.rs Added HashMap-based page cache to AnonSharedMapping with weak references and atomic get_or_create_page method
kernel/src/mm/fault.rs Modified anonymous page fault handler to check for shared anonymous mappings and use cached pages via map_phys

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

};

// Map the shared page into current process
let flags = vma.lock_irqsave().flags();
Copy link

Copilot AI Nov 2, 2025

Choose a reason for hiding this comment

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

The VMA lock is acquired twice: once on line 243 for checking and once on line 258 for getting flags. Consider storing the flags during the first lock acquisition on line 243 before dropping the guard, to avoid unnecessary lock contention.

Copilot uses AI. Check for mistakes.
- 修复 MAP_SHARED|ANON 语义,确保匿名共享页在跨进程间真正共享,
	避免了由于匿名共享页面在尚未fault的时候,fork,
	接着父子进程分别为这一区域分配了不同的物理页面.
- 为 AnonSharedMapping 添加页面缓存,避免重复分配同一页面
- 实现原子性的页面获取或创建机制,防止竞态条件

Signed-off-by: longjin <longjin@DragonOS.org>
@fslongjin fslongjin force-pushed the fix-shared-anon-page-fault branch from 6790dc7 to 2c60d25 Compare November 2, 2025 14:54
cursor[bot]

This comment was marked as outdated.

@fslongjin fslongjin merged commit d94badf into DragonOS-Community:master Nov 3, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug fix A bug is fixed in this pull request test Unitest/User space test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants