Skip to content

Commit 4d45e75

Browse files
thejhtorvalds
authored andcommitted
mm: remove the now-unnecessary mmget_still_valid() hack
The preceding patches have ensured that core dumping properly takes the mmap_lock. Thanks to that, we can now remove mmget_still_valid() and all its users. Signed-off-by: Jann Horn <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Acked-by: Linus Torvalds <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Alexander Viro <[email protected]> Cc: "Eric W . Biederman" <[email protected]> Cc: Oleg Nesterov <[email protected]> Cc: Hugh Dickins <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 7f3bfab commit 4d45e75

File tree

8 files changed

+29
-107
lines changed

8 files changed

+29
-107
lines changed

drivers/infiniband/core/uverbs_main.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,6 @@ void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)
845845
* will only be one mm, so no big deal.
846846
*/
847847
mmap_read_lock(mm);
848-
if (!mmget_still_valid(mm))
849-
goto skip_mm;
850848
mutex_lock(&ufile->umap_lock);
851849
list_for_each_entry_safe (priv, next_priv, &ufile->umaps,
852850
list) {
@@ -865,7 +863,6 @@ void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)
865863
}
866864
}
867865
mutex_unlock(&ufile->umap_lock);
868-
skip_mm:
869866
mmap_read_unlock(mm);
870867
mmput(mm);
871868
}

drivers/vfio/pci/vfio_pci.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,31 +1480,29 @@ static int vfio_pci_zap_and_vma_lock(struct vfio_pci_device *vdev, bool try)
14801480
} else {
14811481
mmap_read_lock(mm);
14821482
}
1483-
if (mmget_still_valid(mm)) {
1484-
if (try) {
1485-
if (!mutex_trylock(&vdev->vma_lock)) {
1486-
mmap_read_unlock(mm);
1487-
mmput(mm);
1488-
return 0;
1489-
}
1490-
} else {
1491-
mutex_lock(&vdev->vma_lock);
1483+
if (try) {
1484+
if (!mutex_trylock(&vdev->vma_lock)) {
1485+
mmap_read_unlock(mm);
1486+
mmput(mm);
1487+
return 0;
14921488
}
1493-
list_for_each_entry_safe(mmap_vma, tmp,
1494-
&vdev->vma_list, vma_next) {
1495-
struct vm_area_struct *vma = mmap_vma->vma;
1489+
} else {
1490+
mutex_lock(&vdev->vma_lock);
1491+
}
1492+
list_for_each_entry_safe(mmap_vma, tmp,
1493+
&vdev->vma_list, vma_next) {
1494+
struct vm_area_struct *vma = mmap_vma->vma;
14961495

1497-
if (vma->vm_mm != mm)
1498-
continue;
1496+
if (vma->vm_mm != mm)
1497+
continue;
14991498

1500-
list_del(&mmap_vma->vma_next);
1501-
kfree(mmap_vma);
1499+
list_del(&mmap_vma->vma_next);
1500+
kfree(mmap_vma);
15021501

1503-
zap_vma_ptes(vma, vma->vm_start,
1504-
vma->vm_end - vma->vm_start);
1505-
}
1506-
mutex_unlock(&vdev->vma_lock);
1502+
zap_vma_ptes(vma, vma->vm_start,
1503+
vma->vm_end - vma->vm_start);
15071504
}
1505+
mutex_unlock(&vdev->vma_lock);
15081506
mmap_read_unlock(mm);
15091507
mmput(mm);
15101508
}

fs/proc/task_mmu.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,24 +1244,6 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
12441244
count = -EINTR;
12451245
goto out_mm;
12461246
}
1247-
/*
1248-
* Avoid to modify vma->vm_flags
1249-
* without locked ops while the
1250-
* coredump reads the vm_flags.
1251-
*/
1252-
if (!mmget_still_valid(mm)) {
1253-
/*
1254-
* Silently return "count"
1255-
* like if get_task_mm()
1256-
* failed. FIXME: should this
1257-
* function have returned
1258-
* -ESRCH if get_task_mm()
1259-
* failed like if
1260-
* get_proc_task() fails?
1261-
*/
1262-
mmap_write_unlock(mm);
1263-
goto out_mm;
1264-
}
12651247
for (vma = mm->mmap; vma; vma = vma->vm_next) {
12661248
vma->vm_flags &= ~VM_SOFTDIRTY;
12671249
vma_set_page_prot(vma);

fs/userfaultfd.c

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,6 @@ static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx,
601601

602602
/* the various vma->vm_userfaultfd_ctx still points to it */
603603
mmap_write_lock(mm);
604-
/* no task can run (and in turn coredump) yet */
605-
VM_WARN_ON(!mmget_still_valid(mm));
606604
for (vma = mm->mmap; vma; vma = vma->vm_next)
607605
if (vma->vm_userfaultfd_ctx.ctx == release_new_ctx) {
608606
vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
@@ -842,7 +840,6 @@ static int userfaultfd_release(struct inode *inode, struct file *file)
842840
/* len == 0 means wake all */
843841
struct userfaultfd_wake_range range = { .len = 0, };
844842
unsigned long new_flags;
845-
bool still_valid;
846843

847844
WRITE_ONCE(ctx->released, true);
848845

@@ -858,7 +855,6 @@ static int userfaultfd_release(struct inode *inode, struct file *file)
858855
* taking the mmap_lock for writing.
859856
*/
860857
mmap_write_lock(mm);
861-
still_valid = mmget_still_valid(mm);
862858
prev = NULL;
863859
for (vma = mm->mmap; vma; vma = vma->vm_next) {
864860
cond_resched();
@@ -869,17 +865,15 @@ static int userfaultfd_release(struct inode *inode, struct file *file)
869865
continue;
870866
}
871867
new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP);
872-
if (still_valid) {
873-
prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
874-
new_flags, vma->anon_vma,
875-
vma->vm_file, vma->vm_pgoff,
876-
vma_policy(vma),
877-
NULL_VM_UFFD_CTX);
878-
if (prev)
879-
vma = prev;
880-
else
881-
prev = vma;
882-
}
868+
prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end,
869+
new_flags, vma->anon_vma,
870+
vma->vm_file, vma->vm_pgoff,
871+
vma_policy(vma),
872+
NULL_VM_UFFD_CTX);
873+
if (prev)
874+
vma = prev;
875+
else
876+
prev = vma;
883877
vma->vm_flags = new_flags;
884878
vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX;
885879
}
@@ -1309,8 +1303,6 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
13091303
goto out;
13101304

13111305
mmap_write_lock(mm);
1312-
if (!mmget_still_valid(mm))
1313-
goto out_unlock;
13141306
vma = find_vma_prev(mm, start, &prev);
13151307
if (!vma)
13161308
goto out_unlock;
@@ -1511,8 +1503,6 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
15111503
goto out;
15121504

15131505
mmap_write_lock(mm);
1514-
if (!mmget_still_valid(mm))
1515-
goto out_unlock;
15161506
vma = find_vma_prev(mm, start, &prev);
15171507
if (!vma)
15181508
goto out_unlock;

include/linux/sched/mm.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,6 @@ static inline void mmdrop(struct mm_struct *mm)
4949
__mmdrop(mm);
5050
}
5151

52-
/*
53-
* This has to be called after a get_task_mm()/mmget_not_zero()
54-
* followed by taking the mmap_lock for writing before modifying the
55-
* vmas or anything the coredump pretends not to change from under it.
56-
*
57-
* It also has to be called when mmgrab() is used in the context of
58-
* the process, but then the mm_count refcount is transferred outside
59-
* the context of the process to run down_write() on that pinned mm.
60-
*
61-
* NOTE: find_extend_vma() called from GUP context is the only place
62-
* that can modify the "mm" (notably the vm_start/end) under mmap_lock
63-
* for reading and outside the context of the process, so it is also
64-
* the only case that holds the mmap_lock for reading that must call
65-
* this function. Generally if the mmap_lock is hold for reading
66-
* there's no need of this check after get_task_mm()/mmget_not_zero().
67-
*
68-
* This function can be obsoleted and the check can be removed, after
69-
* the coredump code will hold the mmap_lock for writing before
70-
* invoking the ->core_dump methods.
71-
*/
72-
static inline bool mmget_still_valid(struct mm_struct *mm)
73-
{
74-
return likely(!mm->core_state);
75-
}
76-
7752
/**
7853
* mmget() - Pin the address space associated with a &struct mm_struct.
7954
* @mm: The address space to pin.

mm/khugepaged.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ static void insert_to_mm_slots_hash(struct mm_struct *mm,
434434

435435
static inline int khugepaged_test_exit(struct mm_struct *mm)
436436
{
437-
return atomic_read(&mm->mm_users) == 0 || !mmget_still_valid(mm);
437+
return atomic_read(&mm->mm_users) == 0;
438438
}
439439

440440
static bool hugepage_vma_check(struct vm_area_struct *vma,

mm/madvise.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,23 +1085,6 @@ int do_madvise(unsigned long start, size_t len_in, int behavior)
10851085
if (write) {
10861086
if (mmap_write_lock_killable(current->mm))
10871087
return -EINTR;
1088-
1089-
/*
1090-
* We may have stolen the mm from another process
1091-
* that is undergoing core dumping.
1092-
*
1093-
* Right now that's io_ring, in the future it may
1094-
* be remote process management and not "current"
1095-
* at all.
1096-
*
1097-
* We need to fix core dumping to not do this,
1098-
* but for now we have the mmget_still_valid()
1099-
* model.
1100-
*/
1101-
if (!mmget_still_valid(current->mm)) {
1102-
mmap_write_unlock(current->mm);
1103-
return -EINTR;
1104-
}
11051088
} else {
11061089
mmap_read_lock(current->mm);
11071090
}

mm/mmap.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,7 +2562,7 @@ find_extend_vma(struct mm_struct *mm, unsigned long addr)
25622562
if (vma && (vma->vm_start <= addr))
25632563
return vma;
25642564
/* don't alter vm_end if the coredump is running */
2565-
if (!prev || !mmget_still_valid(mm) || expand_stack(prev, addr))
2565+
if (!prev || expand_stack(prev, addr))
25662566
return NULL;
25672567
if (prev->vm_flags & VM_LOCKED)
25682568
populate_vma_page_range(prev, addr, prev->vm_end, NULL);
@@ -2588,9 +2588,6 @@ find_extend_vma(struct mm_struct *mm, unsigned long addr)
25882588
return vma;
25892589
if (!(vma->vm_flags & VM_GROWSDOWN))
25902590
return NULL;
2591-
/* don't alter vm_start if the coredump is running */
2592-
if (!mmget_still_valid(mm))
2593-
return NULL;
25942591
start = vma->vm_start;
25952592
if (expand_stack(vma, addr))
25962593
return NULL;

0 commit comments

Comments
 (0)