Notes
main
main
  • Introduction
  • linuxKernel
    • tips
    • make_help
    • old linux
      • compile_linux0.11
      • TestEnvironment
      • load_setup
      • get_hard_data
    • list
    • plist
    • fifo
    • idr
    • xarray
    • rbtree
    • maple_tree
    • syscall
    • bitmap
    • page
    • page_flags
    • page_size
    • page mapcount
    • page refcount
    • folio
    • slub
      • proc_slabinfo
      • slub_theory
      • kmalloc_kfree
      • kmem_cache
      • slab_alloc
      • slab_free
      • proc_meminfo_SReclaimable_SReclaimable
    • vmalloc
    • brk
    • mmap
    • mremap
    • mprotect
    • madvise
    • read
    • write
    • shmem
    • huge_page
    • page_fault
    • rmap
    • lru
    • multi-gen-LRU
    • page_reclaim
    • page_cache
    • page_table
    • rcu
    • kvm
    • aarch64_boot
    • tracing_system
    • cache_coherence_and_memory_consistency
    • cpu_speculates
    • mmap_lock
    • per-vma_lock
    • cgroup
    • symbol
    • campact
    • page_ext
    • mempool
    • kernelstack
    • filesystem
    • io_stack
    • workingset
    • ioremap
    • sched_period
  • linuxDebug
    • openocd_openjtag
    • i2c_tools
    • objdump
    • addr2line
    • gdb_useage
    • debug_linux_kernel_via_gdb
    • debug_linux_module_via_gdb
    • early_boot
    • sequentially_execute
    • dynamic_debug
    • research_linuxKernel_by_patch
    • tracefs
    • ebpf
    • bpftrace
    • perf
    • flame_graph
    • crash
    • ASAN_HWASAN_MTE_check_mem_bug
    • page_owner
    • vmtouch
    • fio
    • benchmark
  • linuxSystem
    • common
      • system_version
      • procfs
      • proc_sys_vm
      • cmd_ps
      • makefile
      • file_descriptor
      • psi
      • ulimit
      • top
      • delay_accounting
    • ubuntu
      • custom_kernel
      • get_cmd_src
      • record_ssh_info
      • log
      • run_custom_script
      • repo
      • cockpit
      • nfs
      • tftp
      • misc
    • fedora
      • system_upgrade
      • custom_kernel
      • lvextend
      • yt-dlp
      • jellyfin
  • linuxDriver
    • i2c_peripherals_driver
    • spi_peripherals_driver
    • gpio_subsystem
    • IRQ_driver
    • blockIO_unblockIO_async
    • linux_own_driver
    • misc_device
    • input_device
    • timer
    • atomic_spinlock_semaphore_mutex
    • lcd
    • touch_screen
    • debugfs
    • v4l2
    • mmap
  • hardware
    • paging_mmu_pt
    • iommu
  • process_thread_scheduler
    • scheduler01
    • scheduler02
    • scheduler03
    • scheduler04
    • scheduler05
    • scheduler06
  • memory_management
    • mm1
    • mm2
    • mm3
    • mm4
    • mm5
  • input_output_filesystem
    • io_fs_01
    • io_fs_02
    • io_fs_03
    • io_fs_04
  • lock_and_lockup_detector
    • general_lock
    • hung_task
    • softLockup_hardLockup
    • crash_experiment
  • MIT_6.S081
    • 6.S081_Operating_System_Engineering
    • Schedule.md
    • Class
      • Overview
      • Administrivia
    • Labs
      • Tools
      • Guidance
      • startup
      • syscall
      • page_table
      • Calling_Convention
      • traps
    • xv6
      • xv6
    • References.md
  • qemu
    • qemu_buildroot
    • qemu_busybox.md
    • Serial.md
    • demo_mini2440
      • 0_compilation_error_summary
      • 1_compilation_steps
      • 2_operation_mode
      • 3_transplant_tools_libraries
      • 4_tools_use
      • reference_website
  • tools
    • getKernelSourceCodeList
    • nat
    • shell
    • translating
    • YouCompleteMe
    • cscope
    • global
    • vscode
    • vim
    • binary
    • markdown
    • draw
    • git
    • tig
    • tmux
    • mail_client
    • download_patchset_from_LKML
    • minicom
    • clash
  • other
    • interview
    • interview_c_base
    • know_dontknow
    • Stop-Ask-Questions-The-Stupid-Ways
    • How-To-Ask-Questions-The-Smart-Way
    • docker
    • buildroot
    • rv32_to_rv64
Powered by GitBook
On this page

Was this helpful?

  1. linuxKernel

per-vma_lock

mmap_lock 控制了对整个进程的地址空间的并发访问顺序。如果一个进程拥有大量线程, 并且不同线程同时触发page fault,因为都是使用 mmap_lock 保护,所以这意味着 许多线程同时生成 page fault 的话就会导致进程执行速度变慢。

由于 page fault 发生在特定VMA 内,因此只需要确保对该 VMA 的访问是依次处理就好, 而不需要对整个进程的地址空间加锁来保证依次处理。因此,PER-VMA lock 是 mmap_lock 的一个更细粒度的版本,可以在执行 page fault 时提供更高的并行性。

struct mm_struct {
    ...
    struct rw_semaphore mmap_lock;
    ...
};

这是没有 PER-VMA lock 功能时,相关结构体的变量。

struct vm_area_struct {
    ...
    int vm_lock_seq;
    struct vma_lock *vm_lock;
    ...
};

struct vma_lock {
    struct rw_semaphore lock;
};

struct mm_struct {
    ...
    struct rw_semaphore mmap_lock;
    ...
    int mm_lock_seq;
    ...
};

这是添加 PER-VMA lock 后,相关结构体的变量。

vma_start_read(vma)
handle_mm_fault()
vma_end_read(vma)

if (fail) {
    down_read(&mm->mmap_lock)
    handle_mm_fault()
    up_read(&mm->mmap_lock)
}

在 pagefault 流程中,当申请读锁时,先调用 vma_start_read() 尝试申请 vma 读锁。如果失败,再调用 down_read() 申请 mm 读锁。

down_write(&mm->mmap_lock)
vma_start_write(vma)
## modify vma
vma_end_write_all() ## mm->mm_lock_seq++
up_write(&mm->mmap_lock)

当申请写锁时,先调用 down_write() 申请 mm 写锁,并且调用 vma_start_write() 将 mm->mm_lock_seq 赋值给 vma->vm_lock_seq,如果有人再调用 vma_start_read() 尝试申请 vma 读锁时,就会直接返回,代表 VMA 写锁还被持有。 当释放写锁时,将 mm->mm_lock_seq 加一,并且释放 mm 写锁。

如果需要使用 PER-VMA lock,需要通过 vm_area_[alloc/free]() 进行动态分配/释放 VMA, 因为 PER-VMA lock 是在 vm_area_[alloc/free]() 里面动态分配/释放的。

https://lore.kernel.org/linux-mm/20230227173632.3292573-13-surenb@google.com/

Previousmmap_lockNextcgroup

Last updated 1 year ago

Was this helpful?