当我们到达 Linux 内核模块中struct file_operations
的mmap
回调时,我们是否可以假设在调用回调之前已经持有vma->vm_mm->mm_sem
? 还是我们必须在执行remap_pfn_range
之前显式调用down_write(&vma->vm_mm->mmap_sem)
?
mmap
文件操作处理程序应假定 mmap 锁在调用时已写锁定。mmap
文件处理程序通过call_mmap()
通过mmap_region()
do_mmap()
调用,以下注释出现在"mm/mmap.c"中的do_mmap()
函数之前:
/*
* The caller must write-lock current->mm->mmap_lock.
*/
注:注:在 Linux 内核 5.8 中,mmap 锁从mmap_sem
重命名为mmap_lock
。5.7 内核中对应的注释是:
/*
* The caller must hold down_write(¤t->mm->mmap_sem).
*/
do_mmap()
通过do_mmap_pgoff()
(在"include/linux/mm.h"中)通过vm_mmap_pgoff()
(在"mm/util.c"中)通过ksys_mmap_pgoff()
(在"mm/mmap.c"中)通过mmap_pgoff()
syscall处理程序(在"mm/mmap.c"中)调用。(注从内核版本 5.9 开始,do_mmap_pgoff()
被消除,do_mmap()
直接从vm_mmap_pgoff()
调用。mmap 锁在vm_mmap_pgoff()
中被写锁定。