Skip to content

Commit

Permalink
mm/mmap: clean up validate_mm() calls
Browse files Browse the repository at this point in the history
Patch series "More strict maple tree lockdep", v2.

Linus asked for more strict maple tree lockdep checking [1] and for them
to resume the normal path through Andrews tree.

This series of patches adds checks to ensure the lock is held in write
mode during the write path of the maple tree instead of checking if it's
held at all.

It also reduces the validate_mm() calls by consolidating into commonly
used functions (patch 0001), and removes the necessity of holding the lock
on the detached tree during munmap() operations.


This patch (of 4):

validate_mm() calls are too spread out and duplicated in numerous
locations.  Also, now that the stack write is done under the write lock,
it is not necessary to validate the mm prior to write operations.

Add a validate_mm() to the stack expansions, and to vma_complete() so
that numerous others may be dropped.

Note that vma_link() (and also insert_vm_struct() by call path) already
call validate_mm().

vma_merge() also had an unnecessary call to vma_iter_free() since the
logic change to abort earlier if no merging is necessary.

Drop extra validate_mm() calls at the start of functions and error paths
which won't write to the tree.

Relocate the validate_mm() call in the do_brk_flags() to avoid
re-running the same test when vma_complete() is used.

The call within the error path of mmap_region() is left intentionally
because of the complexity of the function and the potential of drivers
modifying the tree.

Link: https://lkml.kernel.org/r/20230714195551.894800-1-Liam.Howlett@oracle.com
Link: https://lkml.kernel.org/r/20230714195551.894800-2-Liam.Howlett@oracle.com
Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oliver Sang <oliver.sang@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
  • Loading branch information
Liam R. Howlett authored and Andrew Morton committed Aug 18, 2023
1 parent affd26b commit 2574d5e
Showing 1 changed file with 4 additions and 20 deletions.
24 changes: 4 additions & 20 deletions mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ static inline void vma_complete(struct vma_prepare *vp,
}
if (vp->insert && vp->file)
uprobe_mmap(vp->insert);
validate_mm(mm);
}

/*
Expand Down Expand Up @@ -676,7 +677,6 @@ int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma,
vma_iter_store(vmi, vma);

vma_complete(&vp, vmi, vma->vm_mm);
validate_mm(vma->vm_mm);
return 0;

nomem:
Expand Down Expand Up @@ -716,7 +716,6 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,
vma->vm_end = end;
vma->vm_pgoff = pgoff;
vma_complete(&vp, vmi, vma->vm_mm);
validate_mm(vma->vm_mm);
return 0;
}

Expand Down Expand Up @@ -889,7 +888,6 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm,
pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
long adj_start = 0;

validate_mm(mm);
/*
* We later require that vma->vm_flags == vm_flags,
* so this tests vma->vm_flags & VM_SPECIAL, too.
Expand Down Expand Up @@ -1016,10 +1014,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm,
}

vma_complete(&vp, vmi, mm);
vma_iter_free(vmi);
validate_mm(mm);
khugepaged_enter_vma(res, vm_flags);

return res;
}

Expand Down Expand Up @@ -1194,7 +1189,6 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
vm_flags_t vm_flags;
int pkey = 0;

validate_mm(mm);
*populate = 0;

if (!len)
Expand Down Expand Up @@ -2023,6 +2017,7 @@ static int expand_upwards(struct vm_area_struct *vma, unsigned long address)
anon_vma_unlock_write(vma->anon_vma);
khugepaged_enter_vma(vma, vma->vm_flags);
mas_destroy(&mas);
validate_mm(mm);
return error;
}
#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
Expand Down Expand Up @@ -2113,6 +2108,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address)
anon_vma_unlock_write(vma->anon_vma);
khugepaged_enter_vma(vma, vma->vm_flags);
mas_destroy(&mas);
validate_mm(mm);
return error;
}

Expand Down Expand Up @@ -2290,7 +2286,6 @@ static inline void remove_mt(struct mm_struct *mm, struct ma_state *mas)
remove_vma(vma, false);
}
vm_unacct_memory(nr_accounted);
validate_mm(mm);
}

/*
Expand Down Expand Up @@ -2327,8 +2322,6 @@ int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
struct vm_area_struct *new;
int err;

validate_mm(vma->vm_mm);

WARN_ON(vma->vm_start >= addr);
WARN_ON(vma->vm_end <= addr);

Expand Down Expand Up @@ -2385,7 +2378,6 @@ int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
/* Success. */
if (new_below)
vma_next(vmi);
validate_mm(vma->vm_mm);
return 0;

out_free_mpol:
Expand All @@ -2394,7 +2386,6 @@ int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
vma_iter_free(vmi);
out_free_vma:
vm_area_free(new);
validate_mm(vma->vm_mm);
return err;
}

Expand Down Expand Up @@ -3045,7 +3036,6 @@ static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,
struct mm_struct *mm = current->mm;
struct vma_prepare vp;

validate_mm(mm);
/*
* Check against address space limits by the changed size
* Note: This happens *after* clearing old mappings in some code paths.
Expand Down Expand Up @@ -3097,6 +3087,7 @@ static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,
goto mas_store_fail;

mm->map_count++;
validate_mm(mm);
ksm_add_vma(vma);
out:
perf_event_mmap(vma);
Expand All @@ -3105,7 +3096,6 @@ static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,
if (flags & VM_LOCKED)
mm->locked_vm += (len >> PAGE_SHIFT);
vm_flags_set(vma, VM_SOFTDIRTY);
validate_mm(mm);
return 0;

mas_store_fail:
Expand Down Expand Up @@ -3286,7 +3276,6 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
bool faulted_in_anon_vma = true;
VMA_ITERATOR(vmi, mm, addr);

validate_mm(mm);
/*
* If anonymous vma has not yet been faulted, update new pgoff
* to match new location, to increase its chance of merging.
Expand Down Expand Up @@ -3345,7 +3334,6 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
goto out_vma_link;
*need_rmap_locks = false;
}
validate_mm(mm);
return new_vma;

out_vma_link:
Expand All @@ -3361,7 +3349,6 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
out_free_vma:
vm_area_free(new_vma);
out:
validate_mm(mm);
return NULL;
}

Expand Down Expand Up @@ -3498,7 +3485,6 @@ static struct vm_area_struct *__install_special_mapping(
int ret;
struct vm_area_struct *vma;

validate_mm(mm);
vma = vm_area_alloc(mm);
if (unlikely(vma == NULL))
return ERR_PTR(-ENOMEM);
Expand All @@ -3521,12 +3507,10 @@ static struct vm_area_struct *__install_special_mapping(

perf_event_mmap(vma);

validate_mm(mm);
return vma;

out:
vm_area_free(vma);
validate_mm(mm);
return ERR_PTR(ret);
}

Expand Down

0 comments on commit 2574d5e

Please sign in to comment.