Skip to contentSkip to conversation

Use killable vma write locking in most places

PATCH Superseded Suren Baghdasaryan 4 participants 16 messages Mar 22 – Mar 24

Subsystems (13)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Suren Baghdasaryan

author reviewer
#0 lore raw

v4 cover Use killable vma write locking in most places

Now that we have vma_start_write_killable() we can replace most of the vma_start_write() calls with it, improving reaction time to the kill signal.

There are several places which are left untouched by this patchset:

1. free_pgtables() because function should free page tables even if a fatal signal is pending.

2. userfaultd code, where some paths calling vma_start_write() can handle EINTR and some can't without a deeper code refactoring.

3. mpol_rebind_mm() which is used by cpusset controller for migrations and operates on a remote mm. Incomplete operations here would result in an inconsistent cgroup state.

4. vm_flags_{set|mod|clear} require refactoring that involves moving vma_start_write() out of these functions and replacing it with vma_assert_write_locked(), then callers of these functions should lock the vma themselves using vma_start_write_killable() whenever possible.

A cleanup patch is added in the beginning to make later changes more readable. The second patch contains most of the changes. The third patch is a small error handling fixup. The last patch contains the changes associated with process_vma_walk_lock() error handling.

Changes since v3 [1]:
- rebased over mm-unstable
- added Reviewed-by, per Liam R. Howlett and Lorenzo Stoakes
- moved locking before vma_iter_prealloc in vma_shrink and in vma_link, per Liam R. Howlett
- added a separate jump label for vma lock failure case in do_brk_flags(), per Liam R. Howlett
- fixed cpusset -> cpuset, per Lorenzo Stoakes
- added comments explaining vma_start_write moves, per Lorenzo Stoakes
- amended patch description with explanation why vma_start_write moves are safe, per Lorenzo Stoakes
- added comments listing EINTR as a new possible error code, per Lorenzo Stoakes
- moved comments in mlock_fixup() and apply_mlockall_flags() to more appropriate places, per Lorenzo Stoakes
- replaced check for EINTR with fatal_signal_pending() with a comment why it is safe, per Lorenzo Stoakes
- fixed error check in mprotect_fixup(), per Lorenzo Stoakes
- moved vma_start_write_killable() before allocations inside __split_vma() with a clarifying comment
- changed mmap_region() to set err for each failing case,
per Lorenzo Stoakes
- changed label names in expand_upwards() and expand_downwards(), per Lorenzo Stoakes
- changed "if (err < 0)" to "if (err)" for consistency,
per Lorenzo Stoakes
- separated error checking fix for s390 into its own patch, per Lorenzo Stoakes
- removed special handling for EINTR, per Lorenzo Stoakes
- dropped changes trying to propagate EINTR during vma merge, per Lorenzo Stoakes

[1] https://lore.kernel.org/all/[email protected]/

Suren Baghdasaryan (4):
mm/vma: cleanup error handling path in vma_expand()
mm: replace vma_start_write() with vma_start_write_killable()
KVM: s390: avoid kvm_s390_handle_pv() error overwrite
mm: use vma_start_write_killable() in process_vma_walk_lock()

 arch/powerpc/kvm/book3s_hv_uvmem.c |   5 +-
 arch/s390/kvm/kvm-s390.c           |   2 +
 fs/proc/task_mmu.c                 |   5 +-
 mm/khugepaged.c                    |   5 +-
 mm/madvise.c                       |   4 +-
 mm/memory.c                        |   2 +
 mm/mempolicy.c                     |  13 ++-
 mm/mlock.c                         |  28 ++++--
 mm/mprotect.c                      |   5 +-
 mm/mremap.c                        |   4 +-
 mm/pagewalk.c                      |  20 +++--
 mm/vma.c                           | 133 +++++++++++++++++++++--------
 mm/vma_exec.c                      |   6 +-
 13 files changed, 173 insertions(+), 59 deletions(-)

base-commit: 8c65073d94c8b7cc3170de31af38edc9f5d96f0e

Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
author reviewer
#1 lore raw

PATCH v4 1/4 mm/vma: cleanup error handling path in vma_expand()

vma_expand() error handling is a bit confusing with "if (ret) return ret;" mixed with "if (!ret && ...) ret = ...;". Simplify the code to check for errors and return immediately after an operation that might fail. This also makes later changes to this function more readable. Change variable name for storing the error code from "ret" to "err".

No functional change intended.

---
 mm/vma.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
mm/vma.c +11 -7
index a43f3c5d4b3d..ba78ab1f397a 100644
@@ -1170,7 +1170,7 @@ int vma_expand(struct vma_merge_struct *vmg)
 	vma_flags_t sticky_flags =
 		vma_flags_and_mask(&vmg->vma_flags, VMA_STICKY_FLAGS);
 	vma_flags_t target_sticky;
-	int ret = 0;
+	int err = 0;
 
 	mmap_assert_write_locked(vmg->mm);
 	vma_start_write(target);
@@ -1200,12 +1200,16 @@ int vma_expand(struct vma_merge_struct *vmg)
 	 * Note that, by convention, callers ignore OOM for this case, so
 	 * we don't need to account for vmg->give_up_on_mm here.
 	 */
-	if (remove_next)
-		ret = dup_anon_vma(target, next, &anon_dup);
-	if (!ret && vmg->copied_from)
-		ret = dup_anon_vma(target, vmg->copied_from, &anon_dup);
-	if (ret)
-		return ret;
+	if (remove_next) {
+		err = dup_anon_vma(target, next, &anon_dup);
+		if (err)
+			return err;
+	}
+	if (vmg->copied_from) {
+		err = dup_anon_vma(target, vmg->copied_from, &anon_dup);
+		if (err)
+			return err;
+	}
 
 	if (remove_next) {
 		vma_flags_t next_sticky;
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Reviewed-by on v4 1/4
#2 lore raw

Re: [PATCH v4 1/4] mm/vma: cleanup error handling path in vma_expand()

Quoted 7 lines · Suren Baghdasaryan

vma_expand() error handling is a bit confusing with "if (ret) return ret;" mixed with "if (!ret && ...) ret = ...;". Simplify the code to check for errors and return immediately after an operation that might fail. This also makes later changes to this function more readable. Change variable name for storing the error code from "ret" to "err".

Quoted 7 lines

No functional change intended.

---

LGTM,

author reviewer
#3 lore raw

PATCH v4 2/4 mm: replace vma_start_write() with vma_start_write_killable()

In reply to Suren Baghdasaryan

Now that we have vma_start_write_killable() we can replace most of the vma_start_write() calls with it, improving reaction time to the kill signal.

There are several places which are left untouched by this patch:

1. free_pgtables() because function should free page tables even if a fatal signal is pending.

2. process_vma_walk_lock(), which requires changes in its callers and will be handled in the next patch.

3. userfaultd code, where some paths calling vma_start_write() can handle EINTR and some can't without a deeper code refactoring.

4. mpol_rebind_mm() which is used by cpuset controller for migrations and operates on a remote mm. Incomplete operations here would result in an inconsistent cgroup state.

5. vm_flags_{set|mod|clear} require refactoring that involves moving vma_start_write() out of these functions and replacing it with vma_assert_write_locked(), then callers of these functions should lock the vma themselves using vma_start_write_killable() whenever possible.

In a number of places we now lock VMA earlier than before to avoid doing work and undoing it later if a fatal signal is pending. This is safe because the moves are happening within sections where we already hold the mmap_write_lock, so the moves do not change the locking order relative to other kernel locks.

---
 arch/powerpc/kvm/book3s_hv_uvmem.c |   5 +-
 mm/khugepaged.c                    |   5 +-
 mm/madvise.c                       |   4 +-
 mm/memory.c                        |   2 +
 mm/mempolicy.c                     |  12 ++-
 mm/mlock.c                         |  28 +++++--
 mm/mprotect.c                      |   5 +-
 mm/mremap.c                        |   4 +-
 mm/vma.c                           | 117 +++++++++++++++++++++--------
 mm/vma_exec.c                      |   6 +-
 10 files changed, 142 insertions(+), 46 deletions(-)
arch/powerpc/kvm/book3s_hv_uvmem.c +4 -1
index 5fbb95d90e99..0a28b48a46b8 100644
@@ -410,7 +410,10 @@ static int kvmppc_memslot_page_merge(struct kvm *kvm,
 			ret = H_STATE;
 			break;
 		}
-		vma_start_write(vma);
+		if (vma_start_write_killable(vma)) {
+			ret = H_STATE;
+			break;
+		}
 		/* Copy vm_flags to avoid partial modifications in ksm_madvise */
 		vm_flags = vma->vm_flags;
 		ret = ksm_madvise(vma, vma->vm_start, vma->vm_end,
mm/khugepaged.c +4 -1
index 4b0e59c7c0e6..e2f263076084 100644
@@ -1159,7 +1159,10 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
 	if (result != SCAN_SUCCEED)
 		goto out_up_write;
 	/* check if the pmd is still valid */
-	vma_start_write(vma);
+	if (vma_start_write_killable(vma)) {
+		result = SCAN_FAIL;
+		goto out_up_write;
+	}
 	result = check_pmd_still_valid(mm, address, pmd);
 	if (result != SCAN_SUCCEED)
 		goto out_up_write;
mm/madvise.c +3 -1
index 69708e953cf5..feaa16b0e1dc 100644
@@ -175,7 +175,9 @@ static int madvise_update_vma(vm_flags_t new_flags,
 	madv_behavior->vma = vma;
 
 	/* vm_flags is protected by the mmap_lock held in write mode. */
-	vma_start_write(vma);
+	if (vma_start_write_killable(vma))
+		return -EINTR;
+
 	vma->flags = new_vma_flags;
 	if (set_new_anon_name)
 		return replace_anon_vma_name(vma, anon_name);
mm/memory.c +2 -0
index 68cc592ff0ba..b930459e32ec 100644
@@ -366,6 +366,8 @@ void free_pgd_range(struct mmu_gather *tlb,
  * page tables that should be removed.  This can differ from the vma mappings on
  * some archs that may have mappings that need to be removed outside the vmas.
  * Note that the prev->vm_end and next->vm_start are often used.
+ * We don't use vma_start_write_killable() because page tables should be freed
+ * even if the task is being killed.
  *
  * The vma_end differs from the pg_end when a dup_mmap() failed and the tree has
  * unrelated data to the mm_struct being torn down.
mm/mempolicy.c +10 -2
index e5528c35bbb8..929e843543cf 100644
@@ -1784,7 +1784,8 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le
 		return -EINVAL;
 	if (end == start)
 		return 0;
-	mmap_write_lock(mm);
+	if (mmap_write_lock_killable(mm))
+		return -EINTR;
 	prev = vma_prev(&vmi);
 	for_each_vma_range(vmi, vma, end) {
 		/*
@@ -1801,13 +1802,20 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le
 			err = -EOPNOTSUPP;
 			break;
 		}
+		/*
+		 * Lock the VMA early to avoid extra work if fatal signal
+		 * is pending.
+		 */
+		if (vma_start_write_killable(vma)) {
+			err = -EINTR;
+			break;
+		}
 		new = mpol_dup(old);
 		if (IS_ERR(new)) {
 			err = PTR_ERR(new);
 			break;
 		}
 
-		vma_start_write(vma);
 		new->home_node = home_node;
 		err = mbind_range(&vmi, vma, &prev, start, end, new);
 		mpol_put(new);
mm/mlock.c +22 -6
index 8c227fefa2df..efbb9c783f25 100644
@@ -419,8 +419,10 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long addr,
  *
  * Called for mlock(), mlock2() and mlockall(), to set @vma VM_LOCKED;
  * called for munlock() and munlockall(), to clear VM_LOCKED from @vma.
+ *
+ * Return: 0 on success, -EINTR if fatal signal is pending.
  */
-static void mlock_vma_pages_range(struct vm_area_struct *vma,
+static int mlock_vma_pages_range(struct vm_area_struct *vma,
 	unsigned long start, unsigned long end,
 	vma_flags_t *new_vma_flags)
 {
@@ -442,7 +444,9 @@ static void mlock_vma_pages_range(struct vm_area_struct *vma,
 	 */
 	if (vma_flags_test(new_vma_flags, VMA_LOCKED_BIT))
 		vma_flags_set(new_vma_flags, VMA_IO_BIT);
-	vma_start_write(vma);
+	if (vma_start_write_killable(vma))
+		return -EINTR;
+
 	vma_flags_reset_once(vma, new_vma_flags);
 
 	lru_add_drain();
@@ -453,6 +457,7 @@ static void mlock_vma_pages_range(struct vm_area_struct *vma,
 		vma_flags_clear(new_vma_flags, VMA_IO_BIT);
 		vma_flags_reset_once(vma, new_vma_flags);
 	}
+	return 0;
 }
 
 /*
@@ -506,11 +511,13 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	 */
 	if (vma_flags_test(&new_vma_flags, VMA_LOCKED_BIT) &&
 	    vma_flags_test(&old_vma_flags, VMA_LOCKED_BIT)) {
+		ret = vma_start_write_killable(vma);
+		if (ret)
+			goto out;
 		/* No work to do, and mlocking twice would be wrong */
-		vma_start_write(vma);
 		vma->flags = new_vma_flags;
 	} else {
-		mlock_vma_pages_range(vma, start, end, &new_vma_flags);
+		ret = mlock_vma_pages_range(vma, start, end, &new_vma_flags);
 	}
 out:
 	*prev = vma;
@@ -739,9 +746,18 @@ static int apply_mlockall_flags(int flags)
 
 		error = mlock_fixup(&vmi, vma, &prev, vma->vm_start, vma->vm_end,
 				    newflags);
-		/* Ignore errors, but prev needs fixing up. */
-		if (error)
+		if (error) {
+			/*
+			 * If we failed due to a pending fatal signal, return
+			 * now. If we locked the vma before signal arrived, it
+			 * will be unlocked when we drop mmap_write_lock.
+			 */
+			if (fatal_signal_pending(current))
+				return -EINTR;
+
+			/* Ignore errors, but prev needs fixing up. */
 			prev = vma;
+		}
 		cond_resched();
 	}
 out:
mm/mprotect.c +4 -1
index 110d47a36d4b..ae6ed882b600 100644
@@ -768,7 +768,10 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
 	 * vm_flags and vm_page_prot are protected by the mmap_lock
 	 * held in write mode.
 	 */
-	vma_start_write(vma);
+	error = vma_start_write_killable(vma);
+	if (error)
+		goto fail;
+
 	vma_flags_reset_once(vma, &new_vma_flags);
 	if (vma_wants_manual_pte_write_upgrade(vma))
 		mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
mm/mremap.c +3 -1
index e9c8b1d05832..dec39ec314f9 100644
@@ -1356,7 +1356,9 @@ static unsigned long move_vma(struct vma_remap_struct *vrm)
 		return -ENOMEM;
 
 	/* We don't want racing faults. */
-	vma_start_write(vrm->vma);
+	err = vma_start_write_killable(vrm->vma);
+	if (err)
+		return err;
 
 	/* Perform copy step. */
 	err = copy_vma_and_data(vrm, &new_vma);
mm/vma.c +86 -31

302 lines not on this page · raw message

mm/vma_exec.c +4 -2
index 5cee8b7efa0f..8ddcc791d828 100644
@@ -41,6 +41,7 @@ int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)
 	struct vm_area_struct *next;
 	struct mmu_gather tlb;
 	PAGETABLE_MOVE(pmc, vma, vma, old_start, new_start, length);
+	int err;
 
 	BUG_ON(new_start > new_end);
 
@@ -56,8 +57,9 @@ int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)
 	 * cover the whole range: [new_start, old_end)
 	 */
 	vmg.target = vma;
-	if (vma_expand(&vmg))
-		return -ENOMEM;
+	err = vma_expand(&vmg);
+	if (err)
+		return err;
 
 	/*
 	 * move the page tables downwards, on failure we rely on
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
#4 lore raw

Re: [PATCH v4 2/4] mm: replace vma_start_write() with vma_start_write_killable()

Quoted 31 lines · Suren Baghdasaryan

Now that we have vma_start_write_killable() we can replace most of the vma_start_write() calls with it, improving reaction time to the kill signal.

There are several places which are left untouched by this patch:

1. free_pgtables() because function should free page tables even if a fatal signal is pending.

2. process_vma_walk_lock(), which requires changes in its callers and will be handled in the next patch.

3. userfaultd code, where some paths calling vma_start_write() can handle EINTR and some can't without a deeper code refactoring.

4. mpol_rebind_mm() which is used by cpuset controller for migrations and operates on a remote mm. Incomplete operations here would result in an inconsistent cgroup state.

5. vm_flags_{set|mod|clear} require refactoring that involves moving vma_start_write() out of these functions and replacing it with vma_assert_write_locked(), then callers of these functions should lock the vma themselves using vma_start_write_killable() whenever possible.

In a number of places we now lock VMA earlier than before to avoid doing work and undoing it later if a fatal signal is pending. This is safe because the moves are happening within sections where we already hold the mmap_write_lock, so the moves do not change the locking order relative to other kernel locks.

Quoted 3 lines

This really really needs splitting up into separate patches for the various bits you change, I know it might seem pedantic, but it's much harder to review this as one big patch, and hurts bisectability/specificity of fixes etc.

Come, embrace the stats :)

You are welcome to apply a 'Reviewed-by: Lorenzo Stoakes (Oracle) <[email protected]>' tag to all of the split-out patches where I have said LGTM throughout!

Quoted 18 lines
---
 arch/powerpc/kvm/book3s_hv_uvmem.c |   5 +-
 mm/khugepaged.c                    |   5 +-
 mm/madvise.c                       |   4 +-
 mm/memory.c                        |   2 +
 mm/mempolicy.c                     |  12 ++-
 mm/mlock.c                         |  28 +++++--
 mm/mprotect.c                      |   5 +-
 mm/mremap.c                        |   4 +-
 mm/vma.c                           | 117 +++++++++++++++++++++--------
 mm/vma_exec.c                      |   6 +-
 10 files changed, 142 insertions(+), 46 deletions(-)
arch/powerpc/kvm/book3s_hv_uvmem.c +0 -0
index 5fbb95d90e99..0a28b48a46b8 100644

arch/powerpc/kvm/book3s_hv_uvmem.c static int kvmppc_memslot_page_merge(struct kvm *kvm, · line 416

Quoted 8 lines
arch/powerpc/kvm/book3s_hv_uvmem.c +4 -1
@@ -410,7 +410,10 @@ static int kvmppc_memslot_page_merge(struct kvm *kvm,
 			ret = H_STATE;
 			break;
 		}
-		vma_start_write(vma);
+		if (vma_start_write_killable(vma)) {
+			ret = H_STATE;
+			break;
+		}

LGTM

Quoted 8 lines
arch/powerpc/kvm/book3s_hv_uvmem.c +0 -0
 		/* Copy vm_flags to avoid partial modifications in ksm_madvise */
 		vm_flags = vma->vm_flags;
 		ret = ksm_madvise(vma, vma->vm_start, vma->vm_end,
mm/khugepaged.c +0 -0
index 4b0e59c7c0e6..e2f263076084 100644

mm/khugepaged.c static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a · line 1165

Quoted 8 lines
mm/khugepaged.c +4 -1
@@ -1159,7 +1159,10 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
 	if (result != SCAN_SUCCEED)
 		goto out_up_write;
 	/* check if the pmd is still valid */
-	vma_start_write(vma);
+	if (vma_start_write_killable(vma)) {
+		result = SCAN_FAIL;
+		goto out_up_write;
+	}

LGTM

Quoted 8 lines
mm/khugepaged.c +0 -0
 	result = check_pmd_still_valid(mm, address, pmd);
 	if (result != SCAN_SUCCEED)
 		goto out_up_write;
mm/madvise.c +0 -0
index 69708e953cf5..feaa16b0e1dc 100644

mm/madvise.c static int madvise_update_vma(vm_flags_t new_flags, · line 180

Quoted 7 lines
mm/madvise.c +3 -1
@@ -175,7 +175,9 @@ static int madvise_update_vma(vm_flags_t new_flags,
 	madv_behavior->vma = vma;
 
 	/* vm_flags is protected by the mmap_lock held in write mode. */
-	vma_start_write(vma);
+	if (vma_start_write_killable(vma))
+		return -EINTR;
+

LGTM

mm/memory.c void free_pgd_range(struct mmu_gather *tlb, · line 370

Quoted 13 lines
mm/madvise.c +0 -0
 	vma->flags = new_vma_flags;
 	if (set_new_anon_name)
 		return replace_anon_vma_name(vma, anon_name);
mm/memory.c +2 -0
index 68cc592ff0ba..b930459e32ec 100644
@@ -366,6 +366,8 @@ void free_pgd_range(struct mmu_gather *tlb,
  * page tables that should be removed.  This can differ from the vma mappings on
  * some archs that may have mappings that need to be removed outside the vmas.
  * Note that the prev->vm_end and next->vm_start are often used.
+ * We don't use vma_start_write_killable() because page tables should be freed
+ * even if the task is being killed.

Nice.

Quoted 19 lines
mm/memory.c +0 -0
  *
  * The vma_end differs from the pg_end when a dup_mmap() failed and the tree has
  * unrelated data to the mm_struct being torn down.
mm/mempolicy.c +2 -1
index e5528c35bbb8..929e843543cf 100644
@@ -1784,7 +1784,8 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le
 		return -EINVAL;
 	if (end == start)
 		return 0;
-	mmap_write_lock(mm);
+	if (mmap_write_lock_killable(mm))
+		return -EINTR;
 	prev = vma_prev(&vmi);
 	for_each_vma_range(vmi, vma, end) {
 		/*
@@ -1801,13 +1802,20 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le
 			err = -EOPNOTSUPP;

mm/mempolicy.c SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le · line 1812

Quoted 10 lines
mm/mempolicy.c +8 -0
 			break;
 		}
+		/*
+		 * Lock the VMA early to avoid extra work if fatal signal
+		 * is pending.
+		 */
+		if (vma_start_write_killable(vma)) {
+			err = -EINTR;
+			break;
+		}

LGTM, one nitty thing - wonder if we shouldn't pass through the error from vma_start_write_killable()? OTOH, it's not really a big deal. I don't foresee us ever returning anything but -EINTR or 0 :)

Quoted 27 lines
mm/mempolicy.c +0 -1
 		new = mpol_dup(old);
 		if (IS_ERR(new)) {
 			err = PTR_ERR(new);
 			break;
 		}
 
-		vma_start_write(vma);
 		new->home_node = home_node;
 		err = mbind_range(&vmi, vma, &prev, start, end, new);
 		mpol_put(new);
mm/mlock.c +3 -1
index 8c227fefa2df..efbb9c783f25 100644
@@ -419,8 +419,10 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long addr,
  *
  * Called for mlock(), mlock2() and mlockall(), to set @vma VM_LOCKED;
  * called for munlock() and munlockall(), to clear VM_LOCKED from @vma.
+ *
+ * Return: 0 on success, -EINTR if fatal signal is pending.
  */
-static void mlock_vma_pages_range(struct vm_area_struct *vma,
+static int mlock_vma_pages_range(struct vm_area_struct *vma,
 	unsigned long start, unsigned long end,
 	vma_flags_t *new_vma_flags)
 {

mm/mlock.c static void mlock_vma_pages_range(struct vm_area_struct *vma, · line 449

Quoted 7 lines
mm/mlock.c +3 -1
@@ -442,7 +444,9 @@ static void mlock_vma_pages_range(struct vm_area_struct *vma,
 	 */
 	if (vma_flags_test(new_vma_flags, VMA_LOCKED_BIT))
 		vma_flags_set(new_vma_flags, VMA_IO_BIT);
-	vma_start_write(vma);
+	if (vma_start_write_killable(vma))
+		return -EINTR;
+

LGTM

Quoted 12 lines
mm/mlock.c +1 -0
 	vma_flags_reset_once(vma, new_vma_flags);
 
 	lru_add_drain();
@@ -453,6 +457,7 @@ static void mlock_vma_pages_range(struct vm_area_struct *vma,
 		vma_flags_clear(new_vma_flags, VMA_IO_BIT);
 		vma_flags_reset_once(vma, new_vma_flags);
 	}
+	return 0;
 }
 
 /*

mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 516

Quoted 6 lines
mm/mlock.c +3 -0
@@ -506,11 +511,13 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	 */
 	if (vma_flags_test(&new_vma_flags, VMA_LOCKED_BIT) &&
 	    vma_flags_test(&old_vma_flags, VMA_LOCKED_BIT)) {
+		ret = vma_start_write_killable(vma);
+		if (ret)
+			goto out;

LGTM

mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 520

Quoted 6 lines
mm/mlock.c +1 -2
 		/* No work to do, and mlocking twice would be wrong */
-		vma_start_write(vma);
 		vma->flags = new_vma_flags;
 	} else {
-		mlock_vma_pages_range(vma, start, end, &new_vma_flags);
+		ret = mlock_vma_pages_range(vma, start, end, &new_vma_flags);

LGTM

Quoted 7 lines
mm/mlock.c +0 -0
 	}
 out:
 	*prev = vma;
@@ -739,9 +746,18 @@ static int apply_mlockall_flags(int flags)
 
 		error = mlock_fixup(&vmi, vma, &prev, vma->vm_start, vma->vm_end,
 				    newflags);

mm/mlock.c static int apply_mlockall_flags(int flags) · line 756

Quoted 10 lines
mm/mlock.c +8 -2
-		/* Ignore errors, but prev needs fixing up. */
-		if (error)
+		if (error) {
+			/*
+			 * If we failed due to a pending fatal signal, return
+			 * now. If we locked the vma before signal arrived, it
+			 * will be unlocked when we drop mmap_write_lock.
+			 */
+			if (fatal_signal_pending(current))
+				return -EINTR;

LGTM, and thanks for careful explanation, hopefully addresses Matthew's concerns ([0]).

[0]:https://lore.kernel.org/all/[email protected]/

Quoted 12 lines
mm/mlock.c +3 -0
+
+			/* Ignore errors, but prev needs fixing up. */
 			prev = vma;
+		}
 		cond_resched();
 	}
 out:
mm/mprotect.c +0 -0
index 110d47a36d4b..ae6ed882b600 100644

mm/mprotect.c mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, · line 774

Quoted 8 lines
mm/mprotect.c +4 -1
@@ -768,7 +768,10 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
 	 * vm_flags and vm_page_prot are protected by the mmap_lock
 	 * held in write mode.
 	 */
-	vma_start_write(vma);
+	error = vma_start_write_killable(vma);
+	if (error)
+		goto fail;
+

LGTM

Quoted 8 lines
mm/mprotect.c +0 -0
 	vma_flags_reset_once(vma, &new_vma_flags);
 	if (vma_wants_manual_pte_write_upgrade(vma))
 		mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
mm/mremap.c +0 -0
index e9c8b1d05832..dec39ec314f9 100644

mm/mremap.c static unsigned long move_vma(struct vma_remap_struct *vrm) · line 1361

Quoted 7 lines
mm/mremap.c +3 -1
@@ -1356,7 +1356,9 @@ static unsigned long move_vma(struct vma_remap_struct *vrm)
 		return -ENOMEM;
 
 	/* We don't want racing faults. */
-	vma_start_write(vrm->vma);
+	err = vma_start_write_killable(vrm->vma);
+	if (err)
+		return err;

Yeah this is subtle, I saw sashiko flagged it, so I checked and this isn't being accounted properly.

vrm_calc_charge() charges via security_vm_enough_memory_mm()->__vm_enough_memory()->vm_acct_memory()

And we only uncharge via vrm_uncharge()->vm_unacct_memory()

The other error function there is:

	err = copy_vma_and_data(vrm, &new_vma);
	...
	if (err && !new_vma)
		return err;

And in copy_vma_and_data():

	if (!new_vma) {
		vrm_uncharge(vrm);
		*new_vma_ptr = NULL;
		return -ENOMEM;
	}

So that's already taken care of for us.

It's stuff like this that is why it's important to separate this patch into separate patches :)

We also do some weird and wonderful stuff with error handling where we try to back out the remapped page tables, having swapped the new and old VMAs around so the unmapping of the source VMA is done on the destination (yeah it's horrible).

So this is all very subtle really.

Honestly you might just be better off moving the vma_start_write_killable() above vrm_calc_charge()?

If not, you'd just need to replace this with:

	err = vma_start_write_killable(vrm->vma);
	if (err) {
		vrm_uncharge(vrm);
		return err;
	}
Quoted 12 lines
mm/mremap.c +0 -0
 	/* Perform copy step. */
 	err = copy_vma_and_data(vrm, &new_vma);
mm/vma.c +1 -0
index ba78ab1f397a..7930a4270eb9 100644
@@ -524,6 +524,17 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
 		new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
 	}
 
+	/*

mm/vma.c __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 537

Quoted 10 lines
mm/vma.c +10 -0
+	 * Lock VMAs before cloning to avoid extra work if fatal signal
+	 * is pending.
+	 */
+	err = vma_start_write_killable(vma);
+	if (err)
+		goto out_free_vma;
+	err = vma_start_write_killable(new);
+	if (err)
+		goto out_free_vma;
+

LGTM

mm/vma.c __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 548

Quoted 10 lines
mm/vma.c +0 -3
 	err = -ENOMEM;
 	vma_iter_config(vmi, new->vm_start, new->vm_end);
 	if (vma_iter_prealloc(vmi, new))
@@ -543,9 +554,6 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	if (new->vm_ops && new->vm_ops->open)
 		new->vm_ops->open(new);
 
-	vma_start_write(vma);
-	vma_start_write(new);
-

LGTM

mm/vma.c static __must_check struct vm_area_struct *vma_merge_existing_range( · line 913

Quoted 11 lines
mm/vma.c +3 -1
 	init_vma_prep(&vp, vma);
 	vp.insert = new;
 	vma_prepare(&vp);
@@ -900,12 +908,16 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(
 	}
 
 	/* No matter what happens, we will be adjusting middle. */
-	vma_start_write(middle);
+	err = vma_start_write_killable(middle);
+	if (err)
+		goto abort;

I mean looking at this again the vmg_oom() etc. stuff is horrible and I definitely need to rework it. Sorry about that!

I think we need to update vma_modify():

	/* First, try to merge. */
	merged = vma_merge_existing_range(vmg);
	if (merged)
		return merged;
	if (vmg_nomem(vmg))
		return ERR_PTR(-ENOMEM);

+ if (fatal_signal_pending(current))
+ return -EINTR;

So we can just get out early in this case. I _think_ that should be safe? :)

If not, we could hack things here by doing:

	if (err) {
		/* Ensure error propagated. */
		vmg->give_up_on_oom = false;
		goto abort;
	}

And I can just go fix this all up afterwards.

I still don't want a vmg_intr() or anything though, I will rework this into something saner!

Anyway apologies, it's entirely my fault that this is a bit of a mess :)

mm/vma.c static __must_check struct vm_area_struct *vma_merge_existing_range( · line 920

Quoted 8 lines
mm/vma.c +3 -1
 	if (merge_right) {
 		vma_flags_t next_sticky;
 
-		vma_start_write(next);
+		err = vma_start_write_killable(next);
+		if (err)
+			goto abort;

Obv not withstanding above LGTM

mm/vma.c static __must_check struct vm_area_struct *vma_merge_existing_range( · line 931

Quoted 11 lines
mm/vma.c +3 -1
 		vmg->target = next;
 		next_sticky = vma_flags_and_mask(&next->flags, VMA_STICKY_FLAGS);
 		vma_flags_set_mask(&sticky_flags, next_sticky);
@@ -914,7 +926,9 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(
 	if (merge_left) {
 		vma_flags_t prev_sticky;
 
-		vma_start_write(prev);
+		err = vma_start_write_killable(prev);
+		if (err)
+			goto abort;

Ditto

Quoted 5 lines
mm/vma.c +0 -0
 		vmg->target = prev;
 
 		prev_sticky = vma_flags_and_mask(&prev->flags, VMA_STICKY_FLAGS);
@@ -1170,10 +1184,12 @@ int vma_expand(struct vma_merge_struct *vmg)
 	vma_flags_t sticky_flags =

mm/vma.c int vma_expand(struct vma_merge_struct *vmg) · line 1192

Quoted 10 lines
mm/vma.c +4 -2
 		vma_flags_and_mask(&vmg->vma_flags, VMA_STICKY_FLAGS);
 	vma_flags_t target_sticky;
-	int err = 0;
+	int err;
 
 	mmap_assert_write_locked(vmg->mm);
-	vma_start_write(target);
+	err = vma_start_write_killable(target);
+	if (err)
+		return err;

Yeah dup_anon stuff will not be done yet so this LGTM

Quoted 4 lines
mm/vma.c +0 -0
 	target_sticky = vma_flags_and_mask(&target->flags, VMA_STICKY_FLAGS);
 

mm/vma.c int vma_expand(struct vma_merge_struct *vmg) · line 1226

Quoted 10 lines
mm/vma.c +7 -0
@@ -1201,6 +1217,13 @@ int vma_expand(struct vma_merge_struct *vmg)
 	 * we don't need to account for vmg->give_up_on_mm here.
 	 */
 	if (remove_next) {
+		/*
+		 * Lock the VMA early to avoid extra work if fatal signal
+		 * is pending.
+		 */
+		err = vma_start_write_killable(next);
+		if (err)
+			return err;

Same here, so LGTM

mm/vma.c int vma_expand(struct vma_merge_struct *vmg) · line 1217

Quoted 8 lines
mm/vma.c +0 -1
 		err = dup_anon_vma(target, next, &anon_dup);
 		if (err)
 			return err;
@@ -1214,7 +1237,6 @@ int vma_expand(struct vma_merge_struct *vmg)
 	if (remove_next) {
 		vma_flags_t next_sticky;
 
-		vma_start_write(next);

LGTM

Quoted 19 lines
mm/vma.c +5 -0
 		vmg->__remove_next = true;
 
 		next_sticky = vma_flags_and_mask(&next->flags, VMA_STICKY_FLAGS);
@@ -1252,9 +1274,14 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	       unsigned long start, unsigned long end, pgoff_t pgoff)
 {
 	struct vma_prepare vp;
+	int err;
 
 	WARN_ON((vma->vm_start != start) && (vma->vm_end != end));
 
+	err = vma_start_write_killable(vma);
+	if (err)
+		return err;
+
 	if (vma->vm_start < start)
 		vma_iter_config(vmi, vma->vm_start, start);
 	else

mm/vma.c int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 1267

Quoted 5 lines
mm/vma.c +0 -2
@@ -1263,8 +1290,6 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	if (vma_iter_prealloc(vmi, NULL))
 		return -ENOMEM;
 
-	vma_start_write(vma);
-

We will hold the VMA write lock even though we return -ENOMEM, but I guess not a big deal as we will drop the mmap write lock on error at the earliest opportunity anyway.

So LGTM

mm/vma.c static int vms_gather_munmap_vmas(struct vma_munmap_struct *vms, · line 1483

Quoted 11 lines
mm/vma.c +3 -1
 	init_vma_prep(&vp, vma);
 	vma_prepare(&vp);
 	vma_adjust_trans_huge(vma, start, end, NULL);
@@ -1453,7 +1478,9 @@ static int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
 			if (error)
 				goto end_split_failed;
 		}
-		vma_start_write(next);
+		error = vma_start_write_killable(next);
+		if (error)
+			goto munmap_gather_failed;

LGTM

mm/vma.c static void vma_link_file(struct vm_area_struct *vma, bool hold_rmap_lock) · line 1882

Quoted 12 lines
mm/vma.c +5 -0
 		mas_set(mas_detach, vms->vma_count++);
 		error = mas_store_gfp(mas_detach, next, GFP_KERNEL);
 		if (error)
@@ -1848,12 +1875,16 @@ static void vma_link_file(struct vm_area_struct *vma, bool hold_rmap_lock)
 static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma)
 {
 	VMA_ITERATOR(vmi, mm, 0);
+	int err;
+
+	err = vma_start_write_killable(vma);
+	if (err)
+		return err;

LGTM

mm/vma.c static void vma_link_file(struct vm_area_struct *vma, bool hold_rmap_lock) · line 1856

Quoted 6 lines
mm/vma.c +0 -1
 	vma_iter_config(&vmi, vma->vm_start, vma->vm_end);
 	if (vma_iter_prealloc(&vmi, vma))
 		return -ENOMEM;
 
-	vma_start_write(vma);

LGTM

mm/vma.c int mm_take_all_locks(struct mm_struct *mm) · line 2273

Quoted 9 lines
mm/vma.c +1 -1
 	vma_iter_store_new(&vmi, vma);
 	vma_link_file(vma, /* hold_rmap_lock= */false);
 	mm->map_count++;
@@ -2239,9 +2270,8 @@ int mm_take_all_locks(struct mm_struct *mm)
 	 * is reached.
 	 */
 	for_each_vma(vmi, vma) {
-		if (signal_pending(current))
+		if (signal_pending(current) || vma_start_write_killable(vma))

Hmm, surely signal_pending() should catch it :) but I suppose there could be a (very very tight) timing issue here, but case to be made for not converting this?

OTOH it doesn't really make much difference I guess.

Quoted 17 lines
mm/vma.c +1 -2
 			goto out_unlock;
-		vma_start_write(vma);
 	}
 
 	vma_iter_init(&vmi, mm, 0);
@@ -2540,8 +2570,8 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,
 	struct mmap_action *action)
 {
 	struct vma_iterator *vmi = map->vmi;
-	int error = 0;
 	struct vm_area_struct *vma;
+	int error;
 
 	/*
 	 * Determine the object being mapped and call the appropriate
@@ -2552,6 +2582,14 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,
 	if (!vma)

mm/vma.c static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap, · line 2592

Quoted 10 lines
mm/vma.c +8 -0
 		return -ENOMEM;
 
+	/*
+	 * Lock the VMA early to avoid extra work if fatal signal
+	 * is pending.
+	 */
+	error = vma_start_write_killable(vma);
+	if (error)
+		goto free_vma;
+

LGTM

mm/vma.c static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap, · line 2586

Quoted 9 lines
mm/vma.c +0 -2
 	vma_iter_config(vmi, map->addr, map->end);
 	vma_set_range(vma, map->addr, map->end, map->pgoff);
 	vma->flags = map->vma_flags;
@@ -2582,8 +2620,6 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,
 	WARN_ON_ONCE(!arch_validate_flags(map->vm_flags));
 #endif
 
-	/* Lock the VMA since it is modified after insertion into VMA tree */
-	vma_start_write(vma);

LGTM

mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2917

Quoted 8 lines
mm/vma.c +1 -0
 	vma_iter_store_new(vmi, vma);
 	map->mm->map_count++;
 	vma_link_file(vma, action->hide_from_rmap_until_complete);
@@ -2878,6 +2914,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,
 		 unsigned long addr, unsigned long len, vma_flags_t vma_flags)
 {
 	struct mm_struct *mm = current->mm;
+	int err;

LGTM

mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2953

Quoted 12 lines
mm/vma.c +3 -1
 	/*
 	 * Check against address space limits by the changed size
@@ -2910,24 +2947,33 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,
 
 		if (vma_merge_new_range(&vmg))
 			goto out;
-		else if (vmg_nomem(&vmg))
+		if (vmg_nomem(&vmg)) {
+			err = -ENOMEM;
 			goto unacct_fail;
+		}

LGTM

mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2963

Quoted 11 lines
mm/vma.c +3 -1
 	}
 
 	if (vma)
 		vma_iter_next_range(vmi);
 	/* create a vma struct for an anonymous mapping */
 	vma = vm_area_alloc(mm);
-	if (!vma)
+	if (!vma) {
+		err = -ENOMEM;
 		goto unacct_fail;
+	}

LGTM

mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2972

Quoted 11 lines
mm/vma.c +4 -2
 	vma_set_anonymous(vma);
 	vma_set_range(vma, addr, addr + len, addr >> PAGE_SHIFT);
 	vma->flags = vma_flags;
 	vma->vm_page_prot = vm_get_page_prot(vma_flags_to_legacy(vma_flags));
-	vma_start_write(vma);
-	if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL))
+	if (vma_start_write_killable(vma)) {
+		err = -EINTR;
+		goto vma_lock_fail;
+	}

LGTM

mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2976

Quoted 4 lines
mm/vma.c +3 -0
+	if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL)) {
+		err = -ENOMEM;
 		goto mas_store_fail;
+	}

LGTM

mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2995

Quoted 13 lines
mm/vma.c +2 -1
 	mm->map_count++;
 	validate_mm(mm);
@@ -2942,10 +2988,11 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	return 0;
 
 mas_store_fail:
+vma_lock_fail:
 	vm_area_free(vma);
 unacct_fail:
 	vm_unacct_memory(len >> PAGE_SHIFT);
-	return -ENOMEM;
+	return err;

LGTM

mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3163

Quoted 10 lines
mm/vma.c +1 -1
 }
 
 /**
@@ -3112,8 +3159,8 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)
 	struct mm_struct *mm = vma->vm_mm;
 	struct vm_area_struct *next;
 	unsigned long gap_addr;
-	int error = 0;
 	VMA_ITERATOR(vmi, mm, vma->vm_start);
+	int error;

LGTM

mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3200

Quoted 11 lines
mm/vma.c +2 -2
 	if (!vma_test(vma, VMA_GROWSUP_BIT))
 		return -EFAULT;
@@ -3149,12 +3196,14 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)
 
 	/* We must make sure the anon_vma is allocated. */
 	if (unlikely(anon_vma_prepare(vma))) {
-		vma_iter_free(&vmi);
-		return -ENOMEM;
+		error = -ENOMEM;
+		goto vma_prep_fail;

Hm, but before we weren't calling validate_mm() here, and now we will be, I suspect we probably don't want to do that on error. Will comment on it below in that bit of the code.

Otherwise, LGTM.

mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3206

Quoted 7 lines
mm/vma.c +3 -1
 	}
 
 	/* Lock the VMA before expanding to prevent concurrent page faults */
-	vma_start_write(vma);
+	error = vma_start_write_killable(vma);
+	if (error)
+		goto vma_lock_fail;

LGTM

mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3236

Quoted 9 lines
mm/vma.c +2 -0
 	/* We update the anon VMA tree. */
 	anon_vma_lock_write(vma->anon_vma);
 
@@ -3183,6 +3232,8 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)
 		}
 	}
 	anon_vma_unlock_write(vma->anon_vma);
+vma_lock_fail:
+vma_prep_fail:

LGTM

mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3238

Quoted 2 lines
mm/vma.c +0 -0
 	vma_iter_free(&vmi);
 	validate_mm(mm);

Maybe this should be put before the labels?

Then again, I guess there's no harm in it. But I'm not sure taking a long time to check the entire maple tree when CONFIG_DEBUG_VM_MAPLE_TREE is enabled is a good idea with a fatal signal pending?

mm/vma.c int expand_downwards(struct vm_area_struct *vma, unsigned long address) · line 3252

Quoted 8 lines
mm/vma.c +1 -1
 	return error;
@@ -3197,8 +3248,8 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address)
 {
 	struct mm_struct *mm = vma->vm_mm;
 	struct vm_area_struct *prev;
-	int error = 0;
 	VMA_ITERATOR(vmi, mm, vma->vm_start);
+	int error;

LGTM

Quoted 22 lines
mm/vma.c +5 -3
 	if (!vma_test(vma, VMA_GROWSDOWN_BIT))
 		return -EFAULT;
@@ -3228,12 +3279,14 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address)
 
 	/* We must make sure the anon_vma is allocated. */
 	if (unlikely(anon_vma_prepare(vma))) {
-		vma_iter_free(&vmi);
-		return -ENOMEM;
+		error = -ENOMEM;
+		goto vma_prep_fail;
 	}
 
 	/* Lock the VMA before expanding to prevent concurrent page faults */
-	vma_start_write(vma);
+	error = vma_start_write_killable(vma);
+	if (error)
+		goto vma_lock_fail;
 	/* We update the anon VMA tree. */
 	anon_vma_lock_write(vma->anon_vma);
 

mm/vma.c int expand_downwards(struct vm_area_struct *vma, unsigned long address) · line 3320

Quoted 5 lines
mm/vma.c +2 -0
@@ -3263,6 +3316,8 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address)
 		}
 	}
 	anon_vma_unlock_write(vma->anon_vma);
+vma_lock_fail:
+vma_prep_fail:

Obv same comments for expand_upwards() apply here also.

Quoted 16 lines
mm/vma.c +0 -0
 	vma_iter_free(&vmi);
 	validate_mm(mm);
 	return error;
mm/vma_exec.c +1 -0
index 5cee8b7efa0f..8ddcc791d828 100644
@@ -41,6 +41,7 @@ int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)
 	struct vm_area_struct *next;
 	struct mmu_gather tlb;
 	PAGETABLE_MOVE(pmc, vma, vma, old_start, new_start, length);
+	int err;
 
 	BUG_ON(new_start > new_end);
 

mm/vma_exec.c int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift) · line 62

Quoted 8 lines
mm/vma_exec.c +3 -2
@@ -56,8 +57,9 @@ int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)
 	 * cover the whole range: [new_start, old_end)
 	 */
 	vmg.target = vma;
-	if (vma_expand(&vmg))
-		return -ENOMEM;
+	err = vma_expand(&vmg);
+	if (err)
+		return err;

LGTM

Quoted 6 lines
mm/vma_exec.c +0 -1
 	/*
 	 * move the page tables downwards, on failure we rely on
--

2.53.0.1018.g2bb0e51243-goog

Thanks, Lorenzo

author reviewer
#5 lore raw

Re: [PATCH v4 2/4] mm: replace vma_start_write() with vma_start_write_killable()

Quoted 42 lines · Lorenzo Stoakes (Oracle)
Quoted 31 lines · Suren Baghdasaryan

Now that we have vma_start_write_killable() we can replace most of the vma_start_write() calls with it, improving reaction time to the kill signal.

There are several places which are left untouched by this patch:

1. free_pgtables() because function should free page tables even if a fatal signal is pending.

2. process_vma_walk_lock(), which requires changes in its callers and will be handled in the next patch.

3. userfaultd code, where some paths calling vma_start_write() can handle EINTR and some can't without a deeper code refactoring.

4. mpol_rebind_mm() which is used by cpuset controller for migrations and operates on a remote mm. Incomplete operations here would result in an inconsistent cgroup state.

5. vm_flags_{set|mod|clear} require refactoring that involves moving vma_start_write() out of these functions and replacing it with vma_assert_write_locked(), then callers of these functions should lock the vma themselves using vma_start_write_killable() whenever possible.

In a number of places we now lock VMA earlier than before to avoid doing work and undoing it later if a fatal signal is pending. This is safe because the moves are happening within sections where we already hold the mmap_write_lock, so the moves do not change the locking order relative to other kernel locks.

Quoted 3 lines

This really really needs splitting up into separate patches for the various bits you change, I know it might seem pedantic, but it's much harder to review this as one big patch, and hurts bisectability/specificity of fixes etc.

Quoted 5 lines

Come, embrace the stats :)

You are welcome to apply a 'Reviewed-by: Lorenzo Stoakes (Oracle) <[email protected]>' tag to all of the split-out patches where I have said LGTM throughout!

Thanks!
How would you suggest splitting the patch? I'm outlining one possible division below. Let me know if you want to slice it differently:

Quoted 3 lines
Quoted 3 lines
---
 arch/powerpc/kvm/book3s_hv_uvmem.c |   5 +-

The above one can obviously be split as it's powerpc specific.

Quoted 3 lines
Quoted 3 lines
 mm/khugepaged.c                    |   5 +-
 mm/madvise.c                       |   4 +-
 mm/memory.c                        |   2 +

The changes in the above files are almost one-liners with the last one being just a comment change. Should each one go into a separate patch?

Quoted 1 line
Quoted 1 line
 mm/mempolicy.c                     |  12 ++-

I guess mempolicy one can be a separate patch.

Quoted 3 lines
Quoted 3 lines
 mm/mlock.c                         |  28 +++++--
 mm/mprotect.c                      |   5 +-
 mm/mremap.c                        |   4 +-

Maybe the above 3 can go together?

Quoted 2 lines
Quoted 2 lines
 mm/vma.c                           | 117 +++++++++++++++++++++--------
 mm/vma_exec.c                      |   6 +-

The above two files I think can go together and this is the most sizable one.

Quoted 96 lines
Quoted 7 lines
 10 files changed, 142 insertions(+), 46 deletions(-)
arch/powerpc/kvm/book3s_hv_uvmem.c +0 -0
index 5fbb95d90e99..0a28b48a46b8 100644

arch/powerpc/kvm/book3s_hv_uvmem.c static int kvmppc_memslot_page_merge(struct kvm *kvm, · line 416

Quoted 8 lines
arch/powerpc/kvm/book3s_hv_uvmem.c +4 -1
@@ -410,7 +410,10 @@ static int kvmppc_memslot_page_merge(struct kvm *kvm,
                      ret = H_STATE;
                      break;
              }
-             vma_start_write(vma);
+             if (vma_start_write_killable(vma)) {
+                     ret = H_STATE;
+                     break;
+             }

LGTM

Quoted 8 lines
arch/powerpc/kvm/book3s_hv_uvmem.c +0 -0
              /* Copy vm_flags to avoid partial modifications in ksm_madvise */
              vm_flags = vma->vm_flags;
              ret = ksm_madvise(vma, vma->vm_start, vma->vm_end,
mm/khugepaged.c +0 -0
index 4b0e59c7c0e6..e2f263076084 100644

mm/khugepaged.c static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a · line 1165

Quoted 8 lines
mm/khugepaged.c +4 -1
@@ -1159,7 +1159,10 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long a
      if (result != SCAN_SUCCEED)
              goto out_up_write;
      /* check if the pmd is still valid */
-     vma_start_write(vma);
+     if (vma_start_write_killable(vma)) {
+             result = SCAN_FAIL;
+             goto out_up_write;
+     }

LGTM

Quoted 8 lines
mm/khugepaged.c +0 -0
      result = check_pmd_still_valid(mm, address, pmd);
      if (result != SCAN_SUCCEED)
              goto out_up_write;
mm/madvise.c +0 -0
index 69708e953cf5..feaa16b0e1dc 100644

mm/madvise.c static int madvise_update_vma(vm_flags_t new_flags, · line 180

Quoted 7 lines
mm/madvise.c +3 -1
@@ -175,7 +175,9 @@ static int madvise_update_vma(vm_flags_t new_flags,
      madv_behavior->vma = vma;
 
      /* vm_flags is protected by the mmap_lock held in write mode. */
-     vma_start_write(vma);
+     if (vma_start_write_killable(vma))
+             return -EINTR;
+

LGTM

mm/memory.c void free_pgd_range(struct mmu_gather *tlb, · line 370

Quoted 13 lines
mm/madvise.c +0 -0
      vma->flags = new_vma_flags;
      if (set_new_anon_name)
              return replace_anon_vma_name(vma, anon_name);
mm/memory.c +2 -0
index 68cc592ff0ba..b930459e32ec 100644
@@ -366,6 +366,8 @@ void free_pgd_range(struct mmu_gather *tlb,
  * page tables that should be removed.  This can differ from the vma mappings on
  * some archs that may have mappings that need to be removed outside the vmas.
  * Note that the prev->vm_end and next->vm_start are often used.
+ * We don't use vma_start_write_killable() because page tables should be freed
+ * even if the task is being killed.

Nice.

Quoted 25 lines
mm/memory.c +0 -0
  *
  * The vma_end differs from the pg_end when a dup_mmap() failed and the tree has
  * unrelated data to the mm_struct being torn down.
mm/mempolicy.c +6 -1
index e5528c35bbb8..929e843543cf 100644
@@ -1784,7 +1784,8 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le
              return -EINVAL;
      if (end == start)
              return 0;
-     mmap_write_lock(mm);
+     if (mmap_write_lock_killable(mm))
+             return -EINTR;
      prev = vma_prev(&vmi);
      for_each_vma_range(vmi, vma, end) {
              /*
@@ -1801,13 +1802,20 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le
                      err = -EOPNOTSUPP;
                      break;
              }
+             /*
+              * Lock the VMA early to avoid extra work if fatal signal
+              * is pending.
+              */
Quoted 8 lines

mm/mempolicy.c SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le · line 1812

Quoted 4 lines
mm/mempolicy.c +4 -0
+             if (vma_start_write_killable(vma)) {
+                     err = -EINTR;
+                     break;
+             }

LGTM, one nitty thing - wonder if we shouldn't pass through the error from vma_start_write_killable()? OTOH, it's not really a big deal. I don't foresee us ever returning anything but -EINTR or 0 :)

Ack. I'll change err to record vma_start_write_killable() return value instead.

Quoted 73 lines
Quoted 28 lines
mm/mempolicy.c +0 -1
              new = mpol_dup(old);
              if (IS_ERR(new)) {
                      err = PTR_ERR(new);
                      break;
              }
 
-             vma_start_write(vma);
              new->home_node = home_node;
              err = mbind_range(&vmi, vma, &prev, start, end, new);
              mpol_put(new);
mm/mlock.c +3 -1
index 8c227fefa2df..efbb9c783f25 100644
@@ -419,8 +419,10 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long addr,
  *
  * Called for mlock(), mlock2() and mlockall(), to set @vma VM_LOCKED;
  * called for munlock() and munlockall(), to clear VM_LOCKED from @vma.
+ *
+ * Return: 0 on success, -EINTR if fatal signal is pending.
  */
-static void mlock_vma_pages_range(struct vm_area_struct *vma,
+static int mlock_vma_pages_range(struct vm_area_struct *vma,
      unsigned long start, unsigned long end,
      vma_flags_t *new_vma_flags)
 {

mm/mlock.c static void mlock_vma_pages_range(struct vm_area_struct *vma, · line 449

Quoted 7 lines
mm/mlock.c +3 -1
@@ -442,7 +444,9 @@ static void mlock_vma_pages_range(struct vm_area_struct *vma,
       */
      if (vma_flags_test(new_vma_flags, VMA_LOCKED_BIT))
              vma_flags_set(new_vma_flags, VMA_IO_BIT);
-     vma_start_write(vma);
+     if (vma_start_write_killable(vma))
+             return -EINTR;
+

LGTM

Quoted 12 lines
mm/mlock.c +1 -0
      vma_flags_reset_once(vma, new_vma_flags);
 
      lru_add_drain();
@@ -453,6 +457,7 @@ static void mlock_vma_pages_range(struct vm_area_struct *vma,
              vma_flags_clear(new_vma_flags, VMA_IO_BIT);
              vma_flags_reset_once(vma, new_vma_flags);
      }
+     return 0;
 }
 
 /*

mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 516

Quoted 6 lines
mm/mlock.c +3 -0
@@ -506,11 +511,13 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
       */
      if (vma_flags_test(&new_vma_flags, VMA_LOCKED_BIT) &&
          vma_flags_test(&old_vma_flags, VMA_LOCKED_BIT)) {
+             ret = vma_start_write_killable(vma);
+             if (ret)
+                     goto out;

LGTM

mm/mlock.c static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 520

Quoted 6 lines
mm/mlock.c +1 -2
              /* No work to do, and mlocking twice would be wrong */
-             vma_start_write(vma);
              vma->flags = new_vma_flags;
      } else {
-             mlock_vma_pages_range(vma, start, end, &new_vma_flags);
+             ret = mlock_vma_pages_range(vma, start, end, &new_vma_flags);

LGTM

Quoted 5 lines
mm/mlock.c +0 -0
      }
 out:
      *prev = vma;
@@ -739,9 +746,18 @@ static int apply_mlockall_flags(int flags)
 
Quoted 15 lines

mm/mlock.c static int apply_mlockall_flags(int flags) · line 756

Quoted 12 lines
mm/mlock.c +8 -2
              error = mlock_fixup(&vmi, vma, &prev, vma->vm_start, vma->vm_end,
                                  newflags);
-             /* Ignore errors, but prev needs fixing up. */
-             if (error)
+             if (error) {
+                     /*
+                      * If we failed due to a pending fatal signal, return
+                      * now. If we locked the vma before signal arrived, it
+                      * will be unlocked when we drop mmap_write_lock.
+                      */
+                     if (fatal_signal_pending(current))
+                             return -EINTR;

LGTM, and thanks for careful explanation, hopefully addresses Matthew's concerns ([0]).

That was my goal :)

Quoted 75 lines

[0]:https://lore.kernel.org/all/[email protected]/

Quoted 12 lines
mm/mlock.c +3 -0
+
+                     /* Ignore errors, but prev needs fixing up. */
                      prev = vma;
+             }
              cond_resched();
      }
 out:
mm/mprotect.c +0 -0
index 110d47a36d4b..ae6ed882b600 100644

mm/mprotect.c mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, · line 774

Quoted 8 lines
mm/mprotect.c +4 -1
@@ -768,7 +768,10 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
       * vm_flags and vm_page_prot are protected by the mmap_lock
       * held in write mode.
       */
-     vma_start_write(vma);
+     error = vma_start_write_killable(vma);
+     if (error)
+             goto fail;
+

LGTM

Quoted 8 lines
mm/mprotect.c +0 -0
      vma_flags_reset_once(vma, &new_vma_flags);
      if (vma_wants_manual_pte_write_upgrade(vma))
              mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
mm/mremap.c +0 -0
index e9c8b1d05832..dec39ec314f9 100644

mm/mremap.c static unsigned long move_vma(struct vma_remap_struct *vrm) · line 1361

Quoted 7 lines
mm/mremap.c +3 -1
@@ -1356,7 +1356,9 @@ static unsigned long move_vma(struct vma_remap_struct *vrm)
              return -ENOMEM;
 
      /* We don't want racing faults. */
-     vma_start_write(vrm->vma);
+     err = vma_start_write_killable(vrm->vma);
+     if (err)
+             return err;

Yeah this is subtle, I saw sashiko flagged it, so I checked and this isn't being accounted properly.

vrm_calc_charge() charges via security_vm_enough_memory_mm()->__vm_enough_memory()->vm_acct_memory()

And we only uncharge via vrm_uncharge()->vm_unacct_memory()

The other error function there is:

        err = copy_vma_and_data(vrm, &new_vma);
        ...
        if (err && !new_vma)
                return err;

And in copy_vma_and_data():

        if (!new_vma) {
                vrm_uncharge(vrm);
                *new_vma_ptr = NULL;
                return -ENOMEM;
        }

So that's already taken care of for us.

It's stuff like this that is why it's important to separate this patch into separate patches :)

We also do some weird and wonderful stuff with error handling where we try to back out the remapped page tables, having swapped the new and old VMAs around so the unmapping of the source VMA is done on the destination (yeah it's horrible).

Quoted 4 lines

So this is all very subtle really.

Honestly you might just be better off moving the vma_start_write_killable() above vrm_calc_charge()?

Yes I'll try that...

Quoted 8 lines

If not, you'd just need to replace this with:

        err = vma_start_write_killable(vrm->vma);
        if (err) {
                vrm_uncharge(vrm);
                return err;
        }

and if it doesn't work will do the above.

Quoted 56 lines
Quoted 13 lines
mm/mremap.c +0 -0
      /* Perform copy step. */
      err = copy_vma_and_data(vrm, &new_vma);
mm/vma.c +1 -0
index ba78ab1f397a..7930a4270eb9 100644
@@ -524,6 +524,17 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
              new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
      }
 
+     /*

mm/vma.c __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 537

Quoted 10 lines
mm/vma.c +10 -0
+      * Lock VMAs before cloning to avoid extra work if fatal signal
+      * is pending.
+      */
+     err = vma_start_write_killable(vma);
+     if (err)
+             goto out_free_vma;
+     err = vma_start_write_killable(new);
+     if (err)
+             goto out_free_vma;
+

LGTM

mm/vma.c __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 548

Quoted 10 lines
mm/vma.c +0 -3
      err = -ENOMEM;
      vma_iter_config(vmi, new->vm_start, new->vm_end);
      if (vma_iter_prealloc(vmi, new))
@@ -543,9 +554,6 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
      if (new->vm_ops && new->vm_ops->open)
              new->vm_ops->open(new);
 
-     vma_start_write(vma);
-     vma_start_write(new);
-

LGTM

mm/vma.c static __must_check struct vm_area_struct *vma_merge_existing_range( · line 913

Quoted 11 lines
mm/vma.c +3 -1
      init_vma_prep(&vp, vma);
      vp.insert = new;
      vma_prepare(&vp);
@@ -900,12 +908,16 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(
      }
 
      /* No matter what happens, we will be adjusting middle. */
-     vma_start_write(middle);
+     err = vma_start_write_killable(middle);
+     if (err)
+             goto abort;

I mean looking at this again the vmg_oom() etc. stuff is horrible and I definitely need to rework it. Sorry about that!

I think we need to update vma_modify():

Quoted 7 lines
        /* First, try to merge. */
        merged = vma_merge_existing_range(vmg);
        if (merged)
                return merged;
        if (vmg_nomem(vmg))
                return ERR_PTR(-ENOMEM);

+ if (fatal_signal_pending(current))

We need to be careful here. I think there are cases when vma is modified from a context of a different process, for example in process_madvise(). fatal_signal_pending(current) would yield incorrect result because vma->vm_mm is not the same as current->mm.

Quoted 5 lines

+ return -EINTR;

So we can just get out early in this case. I _think_ that should be safe? :)

Quoted 7 lines

If not, we could hack things here by doing:

        if (err) {
                /* Ensure error propagated. */
                vmg->give_up_on_oom = false;
                goto abort;
        }

I think that would be safer.

Quoted 7 lines

And I can just go fix this all up afterwards.

I still don't want a vmg_intr() or anything though, I will rework this into something saner!

Anyway apologies, it's entirely my fault that this is a bit of a mess :)

No worries. We all make mistakes that become obvious only later. Hindsight is always 20/20.

Quoted 141 lines

mm/vma.c static __must_check struct vm_area_struct *vma_merge_existing_range( · line 920

Quoted 9 lines
mm/vma.c +3 -1
      if (merge_right) {
              vma_flags_t next_sticky;
 
-             vma_start_write(next);
+             err = vma_start_write_killable(next);
+             if (err)
+                     goto abort;

Obv not withstanding above LGTM

mm/vma.c static __must_check struct vm_area_struct *vma_merge_existing_range( · line 931

Quoted 11 lines
mm/vma.c +3 -1
              vmg->target = next;
              next_sticky = vma_flags_and_mask(&next->flags, VMA_STICKY_FLAGS);
              vma_flags_set_mask(&sticky_flags, next_sticky);
@@ -914,7 +926,9 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(
      if (merge_left) {
              vma_flags_t prev_sticky;
 
-             vma_start_write(prev);
+             err = vma_start_write_killable(prev);
+             if (err)
+                     goto abort;

Ditto

Quoted 5 lines
mm/vma.c +0 -0
              vmg->target = prev;
 
              prev_sticky = vma_flags_and_mask(&prev->flags, VMA_STICKY_FLAGS);
@@ -1170,10 +1184,12 @@ int vma_expand(struct vma_merge_struct *vmg)
      vma_flags_t sticky_flags =

mm/vma.c int vma_expand(struct vma_merge_struct *vmg) · line 1192

Quoted 10 lines
mm/vma.c +4 -2
              vma_flags_and_mask(&vmg->vma_flags, VMA_STICKY_FLAGS);
      vma_flags_t target_sticky;
-     int err = 0;
+     int err;
 
      mmap_assert_write_locked(vmg->mm);
-     vma_start_write(target);
+     err = vma_start_write_killable(target);
+     if (err)
+             return err;

Yeah dup_anon stuff will not be done yet so this LGTM

Quoted 4 lines
mm/vma.c +0 -0
      target_sticky = vma_flags_and_mask(&target->flags, VMA_STICKY_FLAGS);
 

mm/vma.c int vma_expand(struct vma_merge_struct *vmg) · line 1226

Quoted 10 lines
mm/vma.c +7 -0
@@ -1201,6 +1217,13 @@ int vma_expand(struct vma_merge_struct *vmg)
       * we don't need to account for vmg->give_up_on_mm here.
       */
      if (remove_next) {
+             /*
+              * Lock the VMA early to avoid extra work if fatal signal
+              * is pending.
+              */
+             err = vma_start_write_killable(next);
+             if (err)
+                     return err;

Same here, so LGTM

mm/vma.c int vma_expand(struct vma_merge_struct *vmg) · line 1217

Quoted 8 lines
mm/vma.c +0 -1
              err = dup_anon_vma(target, next, &anon_dup);
              if (err)
                      return err;
@@ -1214,7 +1237,6 @@ int vma_expand(struct vma_merge_struct *vmg)
      if (remove_next) {
              vma_flags_t next_sticky;
 
-             vma_start_write(next);

LGTM

Quoted 19 lines
mm/vma.c +5 -0
              vmg->__remove_next = true;
 
              next_sticky = vma_flags_and_mask(&next->flags, VMA_STICKY_FLAGS);
@@ -1252,9 +1274,14 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,
             unsigned long start, unsigned long end, pgoff_t pgoff)
 {
      struct vma_prepare vp;
+     int err;
 
      WARN_ON((vma->vm_start != start) && (vma->vm_end != end));
 
+     err = vma_start_write_killable(vma);
+     if (err)
+             return err;
+
      if (vma->vm_start < start)
              vma_iter_config(vmi, vma->vm_start, start);
      else

mm/vma.c int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 1267

Quoted 5 lines
mm/vma.c +0 -2
@@ -1263,8 +1290,6 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,
      if (vma_iter_prealloc(vmi, NULL))
              return -ENOMEM;
 
-     vma_start_write(vma);
-

We will hold the VMA write lock even though we return -ENOMEM, but I guess not a big deal as we will drop the mmap write lock on error at the earliest opportunity anyway.

So LGTM

mm/vma.c static int vms_gather_munmap_vmas(struct vma_munmap_struct *vms, · line 1483

Quoted 11 lines
mm/vma.c +3 -1
      init_vma_prep(&vp, vma);
      vma_prepare(&vp);
      vma_adjust_trans_huge(vma, start, end, NULL);
@@ -1453,7 +1478,9 @@ static int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
                      if (error)
                              goto end_split_failed;
              }
-             vma_start_write(next);
+             error = vma_start_write_killable(next);
+             if (error)
+                     goto munmap_gather_failed;

LGTM

mm/vma.c static void vma_link_file(struct vm_area_struct *vma, bool hold_rmap_lock) · line 1882

Quoted 12 lines
mm/vma.c +5 -0
              mas_set(mas_detach, vms->vma_count++);
              error = mas_store_gfp(mas_detach, next, GFP_KERNEL);
              if (error)
@@ -1848,12 +1875,16 @@ static void vma_link_file(struct vm_area_struct *vma, bool hold_rmap_lock)
 static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma)
 {
      VMA_ITERATOR(vmi, mm, 0);
+     int err;
+
+     err = vma_start_write_killable(vma);
+     if (err)
+             return err;

LGTM

mm/vma.c static void vma_link_file(struct vm_area_struct *vma, bool hold_rmap_lock) · line 1856

Quoted 6 lines
mm/vma.c +0 -1
      vma_iter_config(&vmi, vma->vm_start, vma->vm_end);
      if (vma_iter_prealloc(&vmi, vma))
              return -ENOMEM;
 
-     vma_start_write(vma);

LGTM

Quoted 13 lines

mm/vma.c int mm_take_all_locks(struct mm_struct *mm) · line 2273

Quoted 9 lines
mm/vma.c +1 -1
      vma_iter_store_new(&vmi, vma);
      vma_link_file(vma, /* hold_rmap_lock= */false);
      mm->map_count++;
@@ -2239,9 +2270,8 @@ int mm_take_all_locks(struct mm_struct *mm)
       * is reached.
       */
      for_each_vma(vmi, vma) {
-             if (signal_pending(current))
+             if (signal_pending(current) || vma_start_write_killable(vma))

Hmm, surely signal_pending() should catch it :) but I suppose there could be a (very very tight) timing issue here, but case to be made for not converting this?

I want to convert all possible vma_start_write() cases in the hopes that over time I might be able to deprecate vma_start_write() completely. It's not possible right now (see the changelog) but with some effort we might get there eventually.

Quoted 175 lines

OTOH it doesn't really make much difference I guess.

Quoted 17 lines
mm/vma.c +1 -2
                      goto out_unlock;
-             vma_start_write(vma);
      }
 
      vma_iter_init(&vmi, mm, 0);
@@ -2540,8 +2570,8 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,
      struct mmap_action *action)
 {
      struct vma_iterator *vmi = map->vmi;
-     int error = 0;
      struct vm_area_struct *vma;
+     int error;
 
      /*
       * Determine the object being mapped and call the appropriate
@@ -2552,6 +2582,14 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,
      if (!vma)

mm/vma.c static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap, · line 2592

Quoted 10 lines
mm/vma.c +8 -0
              return -ENOMEM;
 
+     /*
+      * Lock the VMA early to avoid extra work if fatal signal
+      * is pending.
+      */
+     error = vma_start_write_killable(vma);
+     if (error)
+             goto free_vma;
+

LGTM

mm/vma.c static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap, · line 2586

Quoted 9 lines
mm/vma.c +0 -2
      vma_iter_config(vmi, map->addr, map->end);
      vma_set_range(vma, map->addr, map->end, map->pgoff);
      vma->flags = map->vma_flags;
@@ -2582,8 +2620,6 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,
      WARN_ON_ONCE(!arch_validate_flags(map->vm_flags));
 #endif
 
-     /* Lock the VMA since it is modified after insertion into VMA tree */
-     vma_start_write(vma);

LGTM

mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2917

Quoted 8 lines
mm/vma.c +1 -0
      vma_iter_store_new(vmi, vma);
      map->mm->map_count++;
      vma_link_file(vma, action->hide_from_rmap_until_complete);
@@ -2878,6 +2914,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,
               unsigned long addr, unsigned long len, vma_flags_t vma_flags)
 {
      struct mm_struct *mm = current->mm;
+     int err;

LGTM

mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2953

Quoted 12 lines
mm/vma.c +3 -1
      /*
       * Check against address space limits by the changed size
@@ -2910,24 +2947,33 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,
 
              if (vma_merge_new_range(&vmg))
                      goto out;
-             else if (vmg_nomem(&vmg))
+             if (vmg_nomem(&vmg)) {
+                     err = -ENOMEM;
                      goto unacct_fail;
+             }

LGTM

mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2963

Quoted 11 lines
mm/vma.c +3 -1
      }
 
      if (vma)
              vma_iter_next_range(vmi);
      /* create a vma struct for an anonymous mapping */
      vma = vm_area_alloc(mm);
-     if (!vma)
+     if (!vma) {
+             err = -ENOMEM;
              goto unacct_fail;
+     }

LGTM

mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2972

Quoted 11 lines
mm/vma.c +4 -2
      vma_set_anonymous(vma);
      vma_set_range(vma, addr, addr + len, addr >> PAGE_SHIFT);
      vma->flags = vma_flags;
      vma->vm_page_prot = vm_get_page_prot(vma_flags_to_legacy(vma_flags));
-     vma_start_write(vma);
-     if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL))
+     if (vma_start_write_killable(vma)) {
+             err = -EINTR;
+             goto vma_lock_fail;
+     }

LGTM

mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2976

Quoted 4 lines
mm/vma.c +3 -0
+     if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL)) {
+             err = -ENOMEM;
              goto mas_store_fail;
+     }

LGTM

mm/vma.c int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, · line 2995

Quoted 13 lines
mm/vma.c +2 -1
      mm->map_count++;
      validate_mm(mm);
@@ -2942,10 +2988,11 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,
      return 0;
 
 mas_store_fail:
+vma_lock_fail:
      vm_area_free(vma);
 unacct_fail:
      vm_unacct_memory(len >> PAGE_SHIFT);
-     return -ENOMEM;
+     return err;

LGTM

mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3163

Quoted 10 lines
mm/vma.c +1 -1
 }
 
 /**
@@ -3112,8 +3159,8 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)
      struct mm_struct *mm = vma->vm_mm;
      struct vm_area_struct *next;
      unsigned long gap_addr;
-     int error = 0;
      VMA_ITERATOR(vmi, mm, vma->vm_start);
+     int error;

LGTM

mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3200

Quoted 11 lines
mm/vma.c +2 -2
      if (!vma_test(vma, VMA_GROWSUP_BIT))
              return -EFAULT;
@@ -3149,12 +3196,14 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)
 
      /* We must make sure the anon_vma is allocated. */
      if (unlikely(anon_vma_prepare(vma))) {
-             vma_iter_free(&vmi);
-             return -ENOMEM;
+             error = -ENOMEM;
+             goto vma_prep_fail;

Hm, but before we weren't calling validate_mm() here, and now we will be, I suspect we probably don't want to do that on error. Will comment on it below in that bit of the code.

Otherwise, LGTM.

mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3206

Quoted 7 lines
mm/vma.c +3 -1
      }
 
      /* Lock the VMA before expanding to prevent concurrent page faults */
-     vma_start_write(vma);
+     error = vma_start_write_killable(vma);
+     if (error)
+             goto vma_lock_fail;

LGTM

mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3236

Quoted 9 lines
mm/vma.c +2 -0
      /* We update the anon VMA tree. */
      anon_vma_lock_write(vma->anon_vma);
 
@@ -3183,6 +3232,8 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)
              }
      }
      anon_vma_unlock_write(vma->anon_vma);
+vma_lock_fail:
+vma_prep_fail:

LGTM

Quoted 8 lines

mm/vma.c int expand_upwards(struct vm_area_struct *vma, unsigned long address) · line 3238

Quoted 2 lines
mm/vma.c +0 -0
      vma_iter_free(&vmi);
      validate_mm(mm);

Maybe this should be put before the labels?

Then again, I guess there's no harm in it. But I'm not sure taking a long time to check the entire maple tree when CONFIG_DEBUG_VM_MAPLE_TREE is enabled is a good idea with a fatal signal pending?

That makes sense. Validating a tree when we know something went wrong is probably meaningless. I'll move it before the jump labels.

Quoted 57 lines

mm/vma.c int expand_downwards(struct vm_area_struct *vma, unsigned long address) · line 3252

Quoted 9 lines
mm/vma.c +1 -1
      return error;
@@ -3197,8 +3248,8 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address)
 {
      struct mm_struct *mm = vma->vm_mm;
      struct vm_area_struct *prev;
-     int error = 0;
      VMA_ITERATOR(vmi, mm, vma->vm_start);
+     int error;

LGTM

Quoted 22 lines
mm/vma.c +5 -3
      if (!vma_test(vma, VMA_GROWSDOWN_BIT))
              return -EFAULT;
@@ -3228,12 +3279,14 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address)
 
      /* We must make sure the anon_vma is allocated. */
      if (unlikely(anon_vma_prepare(vma))) {
-             vma_iter_free(&vmi);
-             return -ENOMEM;
+             error = -ENOMEM;
+             goto vma_prep_fail;
      }
 
      /* Lock the VMA before expanding to prevent concurrent page faults */
-     vma_start_write(vma);
+     error = vma_start_write_killable(vma);
+     if (error)
+             goto vma_lock_fail;
      /* We update the anon VMA tree. */
      anon_vma_lock_write(vma->anon_vma);
 

mm/vma.c int expand_downwards(struct vm_area_struct *vma, unsigned long address) · line 3320

Quoted 5 lines
mm/vma.c +2 -0
@@ -3263,6 +3316,8 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address)
              }
      }
      anon_vma_unlock_write(vma->anon_vma);
+vma_lock_fail:
+vma_prep_fail:

Obv same comments for expand_upwards() apply here also.

Quoted 15 lines
mm/vma.c +0 -0
      vma_iter_free(&vmi);
      validate_mm(mm);
      return error;
mm/vma_exec.c +1 -0
index 5cee8b7efa0f..8ddcc791d828 100644
@@ -41,6 +41,7 @@ int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)
      struct vm_area_struct *next;
      struct mmu_gather tlb;
      PAGETABLE_MOVE(pmc, vma, vma, old_start, new_start, length);
+     int err;
 
      BUG_ON(new_start > new_end);
 
Quoted 11 lines

mm/vma_exec.c int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift) · line 62

Quoted 9 lines
mm/vma_exec.c +3 -2
@@ -56,8 +57,9 @@ int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)
       * cover the whole range: [new_start, old_end)
       */
      vmg.target = vma;
-     if (vma_expand(&vmg))
-             return -ENOMEM;
+     err = vma_expand(&vmg);
+     if (err)
+             return err;

LGTM

Thanks for the detailed review!

Quoted 9 lines
Quoted 7 lines
mm/vma_exec.c +0 -1
      /*
       * move the page tables downwards, on failure we rely on
--

2.53.0.1018.g2bb0e51243-goog

Thanks, Lorenzo

author reviewer
#6 lore raw

PATCH v4 3/4 KVM: s390: avoid kvm_s390_handle_pv() error overwrite

In reply to Suren Baghdasaryan

If kvm_s390_handle_pv() call fails its error code gets recorded but execution proceeds as if the call was successful. If the next call to copy_to_user() fails then the original error is overwritten. The follow-up patch adds fatal signal checks during VMA walk, which makes it possible for kvm_s390_handle_pv() to return EINTR error. Without this fix any error including EINTR can be overwritten and original error will be lost.

Change error handling for kvm_s390_handle_pv() to alter normal flow once failure happens. This is consistent with how kvm_arch_vm_ioctl handles errors for other ioctl commands.

---
 arch/s390/kvm/kvm-s390.c | 2 ++
 1 file changed, 2 insertions(+)
arch/s390/kvm/kvm-s390.c +2 -0
index 3eb60aa932ec..ddad08c0926f 100644
@@ -2947,6 +2947,8 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
 		}
 		/* must be called without kvm->lock */
 		r = kvm_s390_handle_pv(kvm, &args);
+		if (r)
+			break;
 		if (copy_to_user(argp, &args, sizeof(args))) {
 			r = -EFAULT;
 			break;
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
author reviewer
#7 lore raw

Re: [PATCH v4 3/4] KVM: s390: avoid kvm_s390_handle_pv() error overwrite

Quoted 23 lines · Suren Baghdasaryan

If kvm_s390_handle_pv() call fails its error code gets recorded but execution proceeds as if the call was successful. If the next call to copy_to_user() fails then the original error is overwritten. The follow-up patch adds fatal signal checks during VMA walk, which makes it possible for kvm_s390_handle_pv() to return EINTR error. Without this fix any error including EINTR can be overwritten and original error will be lost.

Change error handling for kvm_s390_handle_pv() to alter normal flow once failure happens. This is consistent with how kvm_arch_vm_ioctl handles errors for other ioctl commands.

---
 arch/s390/kvm/kvm-s390.c | 2 ++
 1 file changed, 2 insertions(+)
arch/s390/kvm/kvm-s390.c +0 -0
index 3eb60aa932ec..ddad08c0926f 100644

arch/s390/kvm/kvm-s390.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2951

Quoted 5 lines
arch/s390/kvm/kvm-s390.c +2 -0
@@ -2947,6 +2947,8 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
                }
                /* must be called without kvm->lock */
                r = kvm_s390_handle_pv(kvm, &args);
+               if (r)
+                       break;

At [1] Sashiko says:

"Does skipping the copy_to_user() call here prevent returning Ultravisor error codes (rc and rrc) to userspace when kvm_s390_handle_pv() fails? When an Ultravisor command fails inside kvm_s390_handle_pv(), it populates args.rc and args.rrc with hardware failure codes and returns a negative error code (e.g. -EINVAL).
Before this patch, execution unconditionally continued to copy_to_user(), allowing these diagnostic codes to reach userspace even if the ioctl ultimately returned an error.
By breaking early on error, this skips copy_to_user() entirely and silently drops the updated rc and rrc values, which might break userspace's ability to handle and diagnose hardware Secure Execution failures.
If the goal is to prevent overwriting the original error with -EFAULT, could we perform the copy unconditionally and only update 'r' if it was previously 0?"

[1] https://sashiko.dev/#/patchset/[email protected]

This might be the reason why copy_to_user() in the original code is called even when kvm_s390_handle_pv() fails. Then I guess it would not be a problem if copy_to_user() later fails and overrides EINTR with EFAULT. We could avoid that override by checking if r is already holding an error code but that would change current behavior and possibly the userspace expectations. I'm more inclined to simply drop this patch and let errors including EINTR to be handled as they are today. If anyone has objections please let me know.

Quoted 6 lines
arch/s390/kvm/kvm-s390.c +0 -1
                if (copy_to_user(argp, &args, sizeof(args))) {
                        r = -EFAULT;
                        break;
--

2.53.0.1018.g2bb0e51243-goog

#8 lore raw

Re: [PATCH v4 3/4] KVM: s390: avoid kvm_s390_handle_pv() error overwrite

In reply to Suren Baghdasaryan
Quoted 3 lines · Suren Baghdasaryan

If kvm_s390_handle_pv() call fails its error code gets recorded but execution proceeds as if the call was successful. If the next call to copy_to_user() fails then the original error is overwritten.

Is that really a big deal though, as you're returning an error in either case?

Quoted 19 lines

The follow-up patch adds fatal signal checks during VMA walk, which makes it possible for kvm_s390_handle_pv() to return EINTR error. Without this fix any error including EINTR can be overwritten and original error will be lost.

Change error handling for kvm_s390_handle_pv() to alter normal flow once failure happens. This is consistent with how kvm_arch_vm_ioctl handles errors for other ioctl commands.

---
 arch/s390/kvm/kvm-s390.c | 2 ++
 1 file changed, 2 insertions(+)
arch/s390/kvm/kvm-s390.c +0 -0
index 3eb60aa932ec..ddad08c0926f 100644

arch/s390/kvm/kvm-s390.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2952

Quoted 6 lines
arch/s390/kvm/kvm-s390.c +2 -0
@@ -2947,6 +2947,8 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
 		}
 		/* must be called without kvm->lock */
 		r = kvm_s390_handle_pv(kvm, &args);
+		if (r)
+			break;
 		if (copy_to_user(argp, &args, sizeof(args))) {

Yeah as per Sashiko we probably need to copy_to_user() still.

But in that case, do we even need a change at all? I'm not sure it really matters which error code terminates things does it?

Quoted 5 lines
arch/s390/kvm/kvm-s390.c +0 -1
 			r = -EFAULT;
 			break;
--

2.53.0.1018.g2bb0e51243-goog

Thanks, Lorenzo

author reviewer
#9 lore raw

Re: [PATCH v4 3/4] KVM: s390: avoid kvm_s390_handle_pv() error overwrite

Quoted 36 lines · Lorenzo Stoakes (Oracle)
Quoted 3 lines · Suren Baghdasaryan

If kvm_s390_handle_pv() call fails its error code gets recorded but execution proceeds as if the call was successful. If the next call to copy_to_user() fails then the original error is overwritten.

Is that really a big deal though, as you're returning an error in either case?

Quoted 19 lines

The follow-up patch adds fatal signal checks during VMA walk, which makes it possible for kvm_s390_handle_pv() to return EINTR error. Without this fix any error including EINTR can be overwritten and original error will be lost.

Change error handling for kvm_s390_handle_pv() to alter normal flow once failure happens. This is consistent with how kvm_arch_vm_ioctl handles errors for other ioctl commands.

---
 arch/s390/kvm/kvm-s390.c | 2 ++
 1 file changed, 2 insertions(+)
arch/s390/kvm/kvm-s390.c +0 -0
index 3eb60aa932ec..ddad08c0926f 100644

arch/s390/kvm/kvm-s390.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2952

Quoted 6 lines
arch/s390/kvm/kvm-s390.c +2 -0
@@ -2947,6 +2947,8 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
              }
              /* must be called without kvm->lock */
              r = kvm_s390_handle_pv(kvm, &args);
+             if (r)
+                     break;
              if (copy_to_user(argp, &args, sizeof(args))) {

Yeah as per Sashiko we probably need to copy_to_user() still.

Quoted 2 lines

But in that case, do we even need a change at all? I'm not sure it really matters which error code terminates things does it?

I agree. I plan to drop this patch from the series (see my former reply) unless someone tells me otherwise.

Quoted 8 lines
Quoted 6 lines
arch/s390/kvm/kvm-s390.c +0 -1
                      r = -EFAULT;
                      break;
--

2.53.0.1018.g2bb0e51243-goog

Thanks, Lorenzo

author reviewer
#10 lore raw

PATCH v4 4/4 mm: use vma_start_write_killable() in process_vma_walk_lock()

In reply to Suren Baghdasaryan

Replace vma_start_write() with vma_start_write_killable() when process_vma_walk_lock() is used with PGWALK_WRLOCK option. Adjust its direct and indirect users to check for a possible error and handle it. Ensure users handle EINTR correctly and do not ignore it.

---
 fs/proc/task_mmu.c |  5 ++++-
 mm/mempolicy.c     |  1 +
 mm/pagewalk.c      | 20 ++++++++++++++------
 3 files changed, 19 insertions(+), 7 deletions(-)
fs/proc/task_mmu.c +4 -1
index e091931d7ca1..2fe3d11aad03 100644
@@ -1797,6 +1797,7 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 		struct clear_refs_private cp = {
 			.type = type,
 		};
+		int err;
 
 		if (mmap_write_lock_killable(mm)) {
 			count = -EINTR;
@@ -1824,7 +1825,9 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 						0, mm, 0, -1UL);
 			mmu_notifier_invalidate_range_start(&range);
 		}
-		walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp);
+		err = walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp);
+		if (err)
+			count = err;
 		if (type == CLEAR_REFS_SOFT_DIRTY) {
 			mmu_notifier_invalidate_range_end(&range);
 			flush_tlb_mm(mm);
mm/mempolicy.c +1 -0
index 929e843543cf..bb5b0e83ce0f 100644
@@ -969,6 +969,7 @@ static const struct mm_walk_ops queue_pages_lock_vma_walk_ops = {
  *      (a hugetlbfs page or a transparent huge page being counted as 1).
  * -EIO - a misplaced page found, when MPOL_MF_STRICT specified without MOVEs.
  * -EFAULT - a hole in the memory range, when MPOL_MF_DISCONTIG_OK unspecified.
+ * -EINTR - walk got terminated due to pending fatal signal.
  */
 static long
 queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end,
mm/pagewalk.c +14 -6
index eda74273c8ec..a42cd6a6d812 100644
@@ -438,14 +438,13 @@ static inline void process_mm_walk_lock(struct mm_struct *mm,
 		mmap_assert_write_locked(mm);
 }
 
-static inline void process_vma_walk_lock(struct vm_area_struct *vma,
+static inline int process_vma_walk_lock(struct vm_area_struct *vma,
 					 enum page_walk_lock walk_lock)
 {
 #ifdef CONFIG_PER_VMA_LOCK
 	switch (walk_lock) {
 	case PGWALK_WRLOCK:
-		vma_start_write(vma);
-		break;
+		return vma_start_write_killable(vma);
 	case PGWALK_WRLOCK_VERIFY:
 		vma_assert_write_locked(vma);
 		break;
@@ -457,6 +456,7 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma,
 		break;
 	}
 #endif
+	return 0;
 }
 
 /*
@@ -500,7 +500,9 @@ int walk_page_range_mm_unsafe(struct mm_struct *mm, unsigned long start,
 			if (ops->pte_hole)
 				err = ops->pte_hole(start, next, -1, &walk);
 		} else { /* inside vma */
-			process_vma_walk_lock(vma, ops->walk_lock);
+			err = process_vma_walk_lock(vma, ops->walk_lock);
+			if (err)
+				break;
 			walk.vma = vma;
 			next = min(end, vma->vm_end);
 			vma = find_vma(mm, vma->vm_end);
@@ -717,6 +719,7 @@ int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start,
 		.vma		= vma,
 		.private	= private,
 	};
+	int err;
 
 	if (start >= end || !walk.mm)
 		return -EINVAL;
@@ -724,7 +727,9 @@ int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start,
 		return -EINVAL;
 
 	process_mm_walk_lock(walk.mm, ops->walk_lock);
-	process_vma_walk_lock(vma, ops->walk_lock);
+	err = process_vma_walk_lock(vma, ops->walk_lock);
+	if (err)
+		return err;
 	return __walk_page_range(start, end, &walk);
 }
 
@@ -747,6 +752,7 @@ int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
 		.vma		= vma,
 		.private	= private,
 	};
+	int err;
 
 	if (!walk.mm)
 		return -EINVAL;
@@ -754,7 +760,9 @@ int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
 		return -EINVAL;
 
 	process_mm_walk_lock(walk.mm, ops->walk_lock);
-	process_vma_walk_lock(vma, ops->walk_lock);
+	err = process_vma_walk_lock(vma, ops->walk_lock);
+	if (err)
+		return err;
 	return __walk_page_range(vma->vm_start, vma->vm_end, &walk);
 }
 
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
#11 lore raw

Re: [PATCH v4 4/4] mm: use vma_start_write_killable() in process_vma_walk_lock()

Quoted 18 lines · Suren Baghdasaryan

Replace vma_start_write() with vma_start_write_killable() when process_vma_walk_lock() is used with PGWALK_WRLOCK option. Adjust its direct and indirect users to check for a possible error and handle it. Ensure users handle EINTR correctly and do not ignore it.

---
 fs/proc/task_mmu.c |  5 ++++-
 mm/mempolicy.c     |  1 +
 mm/pagewalk.c      | 20 ++++++++++++++------
 3 files changed, 19 insertions(+), 7 deletions(-)
fs/proc/task_mmu.c +0 -0
index e091931d7ca1..2fe3d11aad03 100644

fs/proc/task_mmu.c static ssize_t clear_refs_write(struct file *file, const char __user *buf, · line 1800

Quoted 4 lines
fs/proc/task_mmu.c +1 -0
@@ -1797,6 +1797,7 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 		struct clear_refs_private cp = {
 			.type = type,
 		};
+		int err;

Maybe better to make it a ssize_t given return type of function?

fs/proc/task_mmu.c static ssize_t clear_refs_write(struct file *file, const char __user *buf, · line 1830

Quoted 11 lines
fs/proc/task_mmu.c +3 -1
 		if (mmap_write_lock_killable(mm)) {
 			count = -EINTR;
@@ -1824,7 +1825,9 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
 						0, mm, 0, -1UL);
 			mmu_notifier_invalidate_range_start(&range);
 		}
-		walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp);
+		err = walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp);
+		if (err)
+			count = err;

Hmm this is gross, but it's an established pattern here, ugh.

Now we have an err though, could we update:

		if (mmap_write_lock_killable(mm)) {
-			count = -EINTR;
+			err = -EINTR;
			goto out_mm;
		}

Then we can just do:

+ err = walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp);

And at the end do:

	return err ?: count;

Which possibly _necessitates_ err being a ssize_t.

mm/mempolicy.c static const struct mm_walk_ops queue_pages_lock_vma_walk_ops = { · line 972

Quoted 12 lines
fs/proc/task_mmu.c +0 -0
 		if (type == CLEAR_REFS_SOFT_DIRTY) {
 			mmu_notifier_invalidate_range_end(&range);
 			flush_tlb_mm(mm);
mm/mempolicy.c +1 -0
index 929e843543cf..bb5b0e83ce0f 100644
@@ -969,6 +969,7 @@ static const struct mm_walk_ops queue_pages_lock_vma_walk_ops = {
  *      (a hugetlbfs page or a transparent huge page being counted as 1).
  * -EIO - a misplaced page found, when MPOL_MF_STRICT specified without MOVEs.
  * -EFAULT - a hole in the memory range, when MPOL_MF_DISCONTIG_OK unspecified.
+ * -EINTR - walk got terminated due to pending fatal signal.

Thanks!

mm/pagewalk.c static inline void process_mm_walk_lock(struct mm_struct *mm, · line 441

Quoted 13 lines
mm/mempolicy.c +0 -0
  */
 static long
 queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end,
mm/pagewalk.c +1 -1
index eda74273c8ec..a42cd6a6d812 100644
@@ -438,14 +438,13 @@ static inline void process_mm_walk_lock(struct mm_struct *mm,
 		mmap_assert_write_locked(mm);
 }
 
-static inline void process_vma_walk_lock(struct vm_area_struct *vma,
+static inline int process_vma_walk_lock(struct vm_area_struct *vma,

NIT: Don't need this to be an inline any longer. May as well fix up while we're here.

mm/pagewalk.c static inline void process_mm_walk_lock(struct mm_struct *mm, · line 447

Quoted 8 lines
mm/pagewalk.c +1 -2
 					 enum page_walk_lock walk_lock)
 {
 #ifdef CONFIG_PER_VMA_LOCK
 	switch (walk_lock) {
 	case PGWALK_WRLOCK:
-		vma_start_write(vma);
-		break;
+		return vma_start_write_killable(vma);

LGTM

Quoted 12 lines
mm/pagewalk.c +1 -0
 	case PGWALK_WRLOCK_VERIFY:
 		vma_assert_write_locked(vma);
 		break;
@@ -457,6 +456,7 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma,
 		break;
 	}
 #endif
+	return 0;
 }
 
 /*

mm/pagewalk.c int walk_page_range_mm_unsafe(struct mm_struct *mm, unsigned long start, · line 505

Quoted 7 lines
mm/pagewalk.c +3 -1
@@ -500,7 +500,9 @@ int walk_page_range_mm_unsafe(struct mm_struct *mm, unsigned long start,
 			if (ops->pte_hole)
 				err = ops->pte_hole(start, next, -1, &walk);
 		} else { /* inside vma */
-			process_vma_walk_lock(vma, ops->walk_lock);
+			err = process_vma_walk_lock(vma, ops->walk_lock);
+			if (err)
+				break;

In every other case we set walk.vma = vma or NULL. Is it a problem not setting it at all in this code path?

Quoted 12 lines
mm/pagewalk.c +1 -0
 			walk.vma = vma;
 			next = min(end, vma->vm_end);
 			vma = find_vma(mm, vma->vm_end);
@@ -717,6 +719,7 @@ int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start,
 		.vma		= vma,
 		.private	= private,
 	};
+	int err;
 
 	if (start >= end || !walk.mm)
 		return -EINVAL;

mm/pagewalk.c int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start, · line 732

Quoted 7 lines
mm/pagewalk.c +3 -1
@@ -724,7 +727,9 @@ int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start,
 		return -EINVAL;
 
 	process_mm_walk_lock(walk.mm, ops->walk_lock);
-	process_vma_walk_lock(vma, ops->walk_lock);
+	err = process_vma_walk_lock(vma, ops->walk_lock);
+	if (err)
+		return err;

LGTM

Quoted 12 lines
mm/pagewalk.c +1 -0
 	return __walk_page_range(start, end, &walk);
 }
 
@@ -747,6 +752,7 @@ int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
 		.vma		= vma,
 		.private	= private,
 	};
+	int err;
 
 	if (!walk.mm)
 		return -EINVAL;

mm/pagewalk.c int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops, · line 765

Quoted 7 lines
mm/pagewalk.c +3 -1
@@ -754,7 +760,9 @@ int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
 		return -EINVAL;
 
 	process_mm_walk_lock(walk.mm, ops->walk_lock);
-	process_vma_walk_lock(vma, ops->walk_lock);
+	err = process_vma_walk_lock(vma, ops->walk_lock);
+	if (err)
+		return err;

LGTM

Quoted 6 lines
mm/pagewalk.c +0 -1
 	return __walk_page_range(vma->vm_start, vma->vm_end, &walk);
 }
 
--

2.53.0.1018.g2bb0e51243-goog

Thanks, Lorenzo

author reviewer
#12 lore raw

Re: [PATCH v4 4/4] mm: use vma_start_write_killable() in process_vma_walk_lock()

Quoted 15 lines · Lorenzo Stoakes (Oracle)
Quoted 13 lines · Suren Baghdasaryan

Replace vma_start_write() with vma_start_write_killable() when process_vma_walk_lock() is used with PGWALK_WRLOCK option. Adjust its direct and indirect users to check for a possible error and handle it. Ensure users handle EINTR correctly and do not ignore it.

---
 fs/proc/task_mmu.c |  5 ++++-
 mm/mempolicy.c     |  1 +
 mm/pagewalk.c      | 20 ++++++++++++++------
 3 files changed, 19 insertions(+), 7 deletions(-)
Quoted 11 lines

fs/proc/task_mmu.c static ssize_t clear_refs_write(struct file *file, const char __user *buf, · line 1800

Quoted 9 lines
fs/proc/task_mmu.c +1 -0
index e091931d7ca1..2fe3d11aad03 100644
@@ -1797,6 +1797,7 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
              struct clear_refs_private cp = {
                      .type = type,
              };
+             int err;

Maybe better to make it a ssize_t given return type of function?

Ack.

Quoted 25 lines

fs/proc/task_mmu.c static ssize_t clear_refs_write(struct file *file, const char __user *buf, · line 1830

Quoted 12 lines
fs/proc/task_mmu.c +3 -1
              if (mmap_write_lock_killable(mm)) {
                      count = -EINTR;
@@ -1824,7 +1825,9 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
                                              0, mm, 0, -1UL);
                      mmu_notifier_invalidate_range_start(&range);
              }
-             walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp);
+             err = walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp);
+             if (err)
+                     count = err;

Hmm this is gross, but it's an established pattern here, ugh.

Now we have an err though, could we update:

                if (mmap_write_lock_killable(mm)) {
(quoted diff) +1 -1
-                       count = -EINTR;
+                       err = -EINTR;
                        goto out_mm;
                }

Then we can just do:

Quoted 7 lines

+ err = walk_page_range(mm, 0, -1, &clear_refs_walk_ops, &cp);

And at the end do:

        return err ?: count;

Which possibly _necessitates_ err being a ssize_t.

Sounds doable. Let me try that.

Quoted 27 lines

mm/mempolicy.c static const struct mm_walk_ops queue_pages_lock_vma_walk_ops = { · line 972

Quoted 13 lines
fs/proc/task_mmu.c +0 -0
              if (type == CLEAR_REFS_SOFT_DIRTY) {
                      mmu_notifier_invalidate_range_end(&range);
                      flush_tlb_mm(mm);
mm/mempolicy.c +1 -0
index 929e843543cf..bb5b0e83ce0f 100644
@@ -969,6 +969,7 @@ static const struct mm_walk_ops queue_pages_lock_vma_walk_ops = {
  *      (a hugetlbfs page or a transparent huge page being counted as 1).
  * -EIO - a misplaced page found, when MPOL_MF_STRICT specified without MOVEs.
  * -EFAULT - a hole in the memory range, when MPOL_MF_DISCONTIG_OK unspecified.
+ * -EINTR - walk got terminated due to pending fatal signal.

Thanks!

Quoted 11 lines
mm/mempolicy.c +0 -0
  */
 static long
 queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end,
mm/pagewalk.c +0 -0
index eda74273c8ec..a42cd6a6d812 100644
@@ -438,14 +438,13 @@ static inline void process_mm_walk_lock(struct mm_struct *mm,
              mmap_assert_write_locked(mm);
 }
 
Quoted 5 lines

mm/pagewalk.c static inline void process_mm_walk_lock(struct mm_struct *mm, · line 441

Quoted 2 lines
mm/pagewalk.c +1 -1
-static inline void process_vma_walk_lock(struct vm_area_struct *vma,
+static inline int process_vma_walk_lock(struct vm_area_struct *vma,

NIT: Don't need this to be an inline any longer. May as well fix up while we're here.

Ack.

Quoted 22 lines

mm/pagewalk.c static inline void process_mm_walk_lock(struct mm_struct *mm, · line 447

Quoted 9 lines
mm/pagewalk.c +1 -2
                                       enum page_walk_lock walk_lock)
 {
 #ifdef CONFIG_PER_VMA_LOCK
      switch (walk_lock) {
      case PGWALK_WRLOCK:
-             vma_start_write(vma);
-             break;
+             return vma_start_write_killable(vma);

LGTM

Quoted 10 lines
mm/pagewalk.c +1 -0
      case PGWALK_WRLOCK_VERIFY:
              vma_assert_write_locked(vma);
              break;
@@ -457,6 +456,7 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma,
              break;
      }
 #endif
+     return 0;
 }
 
Quoted 12 lines

mm/pagewalk.c int walk_page_range_mm_unsafe(struct mm_struct *mm, unsigned long start, · line 505

Quoted 9 lines
mm/pagewalk.c +3 -1
 /*
@@ -500,7 +500,9 @@ int walk_page_range_mm_unsafe(struct mm_struct *mm, unsigned long start,
                      if (ops->pte_hole)
                              err = ops->pte_hole(start, next, -1, &walk);
              } else { /* inside vma */
-                     process_vma_walk_lock(vma, ops->walk_lock);
+                     err = process_vma_walk_lock(vma, ops->walk_lock);
+                     if (err)
+                             break;

In every other case we set walk.vma = vma or NULL. Is it a problem not setting it at all in this code path?

IIUC the other cases set walk.vma because they later call ops->pte_hole(..., walk). In our case we immediately break out of the loop and exit the function, which pushes "walk" variable out of scope. So, walk.vma won't be used and setting it would achieve nothing.

Quoted 53 lines
Quoted 13 lines
mm/pagewalk.c +1 -0
                      walk.vma = vma;
                      next = min(end, vma->vm_end);
                      vma = find_vma(mm, vma->vm_end);
@@ -717,6 +719,7 @@ int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start,
              .vma            = vma,
              .private        = private,
      };
+     int err;
 
      if (start >= end || !walk.mm)
              return -EINVAL;

mm/pagewalk.c int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start, · line 732

Quoted 7 lines
mm/pagewalk.c +3 -1
@@ -724,7 +727,9 @@ int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start,
              return -EINVAL;
 
      process_mm_walk_lock(walk.mm, ops->walk_lock);
-     process_vma_walk_lock(vma, ops->walk_lock);
+     err = process_vma_walk_lock(vma, ops->walk_lock);
+     if (err)
+             return err;

LGTM

Quoted 12 lines
mm/pagewalk.c +1 -0
      return __walk_page_range(start, end, &walk);
 }
 
@@ -747,6 +752,7 @@ int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
              .vma            = vma,
              .private        = private,
      };
+     int err;
 
      if (!walk.mm)
              return -EINVAL;

mm/pagewalk.c int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops, · line 765

Quoted 7 lines
mm/pagewalk.c +3 -1
@@ -754,7 +760,9 @@ int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,
              return -EINVAL;
 
      process_mm_walk_lock(walk.mm, ops->walk_lock);
-     process_vma_walk_lock(vma, ops->walk_lock);
+     err = process_vma_walk_lock(vma, ops->walk_lock);
+     if (err)
+             return err;

LGTM

Quoted 6 lines
mm/pagewalk.c +0 -1
      return __walk_page_range(vma->vm_start, vma->vm_end, &walk);
 }
 
--

2.53.0.1018.g2bb0e51243-goog

Thanks, Lorenzo

author reviewer
#14 lore raw
Quoted 9 lines · Andrew Morton
Quoted 3 lines · On Sat, 21 Mar 2026 22:43:04 -0700 Suren Baghdasaryan <[email protected]> wrote:

Now that we have vma_start_write_killable() we can replace most of the vma_start_write() calls with it, improving reaction time to the kill signal.

Thanks. Sashiko review raised a few possible issues:

        https://sashiko.dev/#/patchset/[email protected]

Thanks! This Sashiko dude is good :)

author reviewer
#15 lore raw
Quoted 5 lines · Suren Baghdasaryan
Quoted 3 lines · Andrew Morton

On Sat, 21 Mar 2026 22:43:04 -0700 Suren Baghdasaryan <[email protected]> wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Now that we have vma_start_write_killable() we can replace most of the vma_start_write() calls with it, improving reaction time to the kill signal.

Thanks. Sashiko review raised a few possible issues:

        https://sashiko.dev/#/patchset/[email protected]

Thanks! This Sashiko dude is good :)

Interestingly Sashiko had one false flag: "Does this code leave mm->locked_vm permanently corrupted if vma_start_write_killable() fails?"

In mlock_fixup() the path that we call vma_start_write_killable() happens only if both new_vma_flags and old_vma_flags have their VMA_LOCKED_BIT set. In such case nr_pages is 0, so "mm->locked_vm += nr_pages;" does not change the value of mm->locked_vm and we are fine. Perhaps this can be used to improve the model?

CC'ing Roman.