diff --git a/mm/internal.h b/mm/internal.h index 8cef7593003c..93d8fa5ccabc 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -970,14 +970,7 @@ void mlock_folio(struct folio *folio); static inline void mlock_vma_folio(struct folio *folio, struct vm_area_struct *vma) { - /* - * The VM_SPECIAL check here serves two purposes. - * 1) VM_IO check prevents migration from double-counting during mlock. - * 2) Although mmap_region() and mlock_fixup() take care that VM_LOCKED - * is never left set on a VM_SPECIAL vma, there is an interval while - * file->f_op->mmap() is using vm_insert_page(s), when VM_LOCKED may - * still be set while VM_SPECIAL bits are added: so ignore it then. - */ + /* The VM_IO check prevents migration from double-counting during mlock. */ if (unlikely((vma->vm_flags & (VM_LOCKED|VM_SPECIAL)) == VM_LOCKED)) mlock_folio(folio); } diff --git a/mm/vma.c b/mm/vma.c index f334c87582d7..e422e06c7dfa 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -2625,6 +2625,11 @@ static int __mmap_new_file_vma(struct mmap_state *map, if (!map->vm_file->f_op->mmap) return 0; + /* + * Driver-specified flags may make the lock flags invalid, so clear + * VMA_LOCKED_MASK and reinstate it afterwards if appropriate. + */ + vma_clear_flags_mask(vma, VMA_LOCKED_MASK); error = mmap_file(vma->vm_file, vma); map->vm_file = vma->vm_file; @@ -2647,6 +2652,16 @@ static int __mmap_new_file_vma(struct mmap_state *map, return error; } + /* + * If the VMA is still eligible for mlock(), reinstate any original + * VMA_LOCKED_BIT and/or VMA_LOCKONFAULT_BIT flags. + */ + if (vma_supports_mlock(vma)) { + const vma_flags_t mask = vma_flags_and_mask(&map->vma_flags, VMA_LOCKED_MASK); + + vma_set_flags_mask(vma, mask); + } + map->vma_flags = vma->flags; return 0;