Skip to contentSkip to conversation

mm: lock newly mapped VMA with corrected ordering

PATCH In review Hugh Dickins 2 participants 3 messages Jul 8, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: lock newly mapped VMA with corrected ordering

Lockdep is certainly right to complain about (&vma->vm_lock->lock){++++}-{3:3}, at: vma_start_write+0x2d/0x3f
but task is already holding lock:
(&mapping->i_mmap_rwsem){+.+.}-{3:3}, at: mmap_region+0x4dc/0x6db Invert those to the usual ordering.

Fixes 33313a747e81 ("mm: lock newly mapped VMA which can be modified after it becomes visible")
---
 mm/mmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/mmap.c +2 -2
index 84c71431a527..3eda23c9ebe7 100644
@@ -2809,11 +2809,11 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 	if (vma_iter_prealloc(&vmi))
 		goto close_and_free_vma;
 
+	/* Lock the VMA since it is modified after insertion into VMA tree */
+	vma_start_write(vma);
 	if (vma->vm_file)
 		i_mmap_lock_write(vma->vm_file->f_mapping);
 
-	/* Lock the VMA since it is modified after insertion into VMA tree */
-	vma_start_write(vma);
 	vma_iter_store(&vmi, vma);
 	mm->map_count++;
 	if (vma->vm_file) {
Signature · 1 line
2.35.3
#1 lore raw
Quoted 6 lines · Hugh Dickins

Lockdep is certainly right to complain about (&vma->vm_lock->lock){++++}-{3:3}, at: vma_start_write+0x2d/0x3f
but task is already holding lock:
(&mapping->i_mmap_rwsem){+.+.}-{3:3}, at: mmap_region+0x4dc/0x6db Invert those to the usual ordering.

Doh! Thanks Hugh!
I totally forgot to run this with lockdep enabled :(

Quoted 28 lines
Fixes 33313a747e81 ("mm: lock newly mapped VMA which can be modified after it becomes visible")
---
 mm/mmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/mmap.c +2 -3
index 84c71431a527..3eda23c9ebe7 100644
@@ -2809,11 +2809,11 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
        if (vma_iter_prealloc(&vmi))
                goto close_and_free_vma;
 
+       /* Lock the VMA since it is modified after insertion into VMA tree */
+       vma_start_write(vma);
        if (vma->vm_file)
                i_mmap_lock_write(vma->vm_file->f_mapping);
 
-       /* Lock the VMA since it is modified after insertion into VMA tree */
-       vma_start_write(vma);
        vma_iter_store(&vmi, vma);
        mm->map_count++;
        if (vma->vm_file) {
--

2.35.3

#2 lore raw
Quoted 11 lines · Suren Baghdasaryan
Quoted 6 lines · Hugh Dickins

Lockdep is certainly right to complain about (&vma->vm_lock->lock){++++}-{3:3}, at: vma_start_write+0x2d/0x3f
but task is already holding lock:
(&mapping->i_mmap_rwsem){+.+.}-{3:3}, at: mmap_region+0x4dc/0x6db Invert those to the usual ordering.

Doh! Thanks Hugh!
I totally forgot to run this with lockdep enabled :(

I verified both the lockdep warning and the fix. Thanks again, Hugh!

Quoted 29 lines
Quoted 29 lines
Fixes 33313a747e81 ("mm: lock newly mapped VMA which can be modified after it becomes visible")
---
 mm/mmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/mmap.c +2 -3
index 84c71431a527..3eda23c9ebe7 100644
@@ -2809,11 +2809,11 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
        if (vma_iter_prealloc(&vmi))
                goto close_and_free_vma;
 
+       /* Lock the VMA since it is modified after insertion into VMA tree */
+       vma_start_write(vma);
        if (vma->vm_file)
                i_mmap_lock_write(vma->vm_file->f_mapping);
 
-       /* Lock the VMA since it is modified after insertion into VMA tree */
-       vma_start_write(vma);
        vma_iter_store(&vmi, vma);
        mm->map_count++;
        if (vma->vm_file) {
--

2.35.3