Skip to contentSkip to conversation

mm: replace vma_start_write() with vma_start_write_killable()

PATCH In review Suren Baghdasaryan 4 participants 6 messages Feb 9 – Feb 11

Subsystems (10)
lore
Share & tools
lore

Suren Baghdasaryan

author reviewer
#0 lore raw

PATCH 1/1 mm: replace vma_start_write() with vma_start_write_killable()

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. userfaultd code, where some paths calling vma_start_write() can handle EINTR and some can't without a deeper code refactoring.

3. 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.

---
 arch/powerpc/kvm/book3s_hv_uvmem.c |  5 +-
 include/linux/mempolicy.h          |  5 +-
 mm/khugepaged.c                    |  5 +-
 mm/madvise.c                       |  4 +-
 mm/memory.c                        |  2 +
 mm/mempolicy.c                     | 23 ++++++--
 mm/mlock.c                         | 20 +++++--
 mm/mprotect.c                      |  4 +-
 mm/mremap.c                        |  4 +-
 mm/pagewalk.c                      | 20 +++++--
 mm/vma.c                           | 94 +++++++++++++++++++++---------
 mm/vma_exec.c                      |  6 +-
 12 files changed, 139 insertions(+), 53 deletions(-)
arch/powerpc/kvm/book3s_hv_uvmem.c +4 -1
index 7cf9310de0ec..69750edcf8d5 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,
include/linux/mempolicy.h +3 -2
index 0fe96f3ab3ef..762930edde5a 100644
@@ -137,7 +137,7 @@ bool vma_policy_mof(struct vm_area_struct *vma);
 extern void numa_default_policy(void);
 extern void numa_policy_init(void);
 extern void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new);
-extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
+extern int mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
 
 extern int huge_node(struct vm_area_struct *vma,
 				unsigned long addr, gfp_t gfp_flags,
@@ -251,8 +251,9 @@ static inline void mpol_rebind_task(struct task_struct *tsk,
 {
 }
 
-static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
+static inline int mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
 {
+	return 0;
 }
 
 static inline int huge_node(struct vm_area_struct *vma,
mm/khugepaged.c +4 -1
index fa1e57fd2c46..392dde66fa86 100644
@@ -1150,7 +1150,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 8debb2d434aa..b41e64231c31 100644
@@ -173,7 +173,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;
+
 	vm_flags_reset(vma, new_flags);
 	if (set_new_anon_name)
 		return replace_anon_vma_name(vma, anon_name);
mm/memory.c +2 -0
index d6d273eb2189..3831e3026615 100644
@@ -379,6 +379,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.
+ * Note: 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 +18 -5
index dbd48502ac24..3de7ab4f4cee 100644
@@ -556,17 +556,25 @@ void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new)
  *
  * Call holding a reference to mm.  Takes mm->mmap_lock during call.
  */
-void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
+int mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
 {
 	struct vm_area_struct *vma;
 	VMA_ITERATOR(vmi, mm, 0);
+	int ret = 0;
+
+	if (mmap_write_lock_killable(mm))
+		return -EINTR;
 
-	mmap_write_lock(mm);
 	for_each_vma(vmi, vma) {
-		vma_start_write(vma);
+		if (vma_start_write_killable(vma)) {
+			ret = -EINTR;
+			break;
+		}
 		mpol_rebind_policy(vma->vm_policy, new);
 	}
 	mmap_write_unlock(mm);
+
+	return ret;
 }
 
 static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
@@ -1785,7 +1793,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) {
 		/*
@@ -1808,7 +1817,11 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le
 			break;
 		}
 
-		vma_start_write(vma);
+		if (vma_start_write_killable(vma)) {
+			err = -EINTR;
+			break;
+		}
+
 		new->home_node = home_node;
 		err = mbind_range(&vmi, vma, &prev, start, end, new);
 		mpol_put(new);
mm/mlock.c +14 -6
index 2f699c3497a5..2885b858aa0f 100644
@@ -420,7 +420,7 @@ 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.
  */
-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, vm_flags_t newflags)
 {
 	static const struct mm_walk_ops mlock_walk_ops = {
@@ -441,7 +441,9 @@ static void mlock_vma_pages_range(struct vm_area_struct *vma,
 	 */
 	if (newflags & VM_LOCKED)
 		newflags |= VM_IO;
-	vma_start_write(vma);
+	if (vma_start_write_killable(vma))
+		return -EINTR;
+
 	vm_flags_reset_once(vma, newflags);
 
 	lru_add_drain();
@@ -452,6 +454,7 @@ static void mlock_vma_pages_range(struct vm_area_struct *vma,
 		newflags &= ~VM_IO;
 		vm_flags_reset_once(vma, newflags);
 	}
+	return 0;
 }
 
 /*
@@ -501,10 +504,12 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
 	 */
 	if ((newflags & VM_LOCKED) && (oldflags & VM_LOCKED)) {
 		/* No work to do, and mlocking twice would be wrong */
-		vma_start_write(vma);
+		ret = vma_start_write_killable(vma);
+		if (ret)
+			goto out;
 		vm_flags_reset(vma, newflags);
 	} else {
-		mlock_vma_pages_range(vma, start, end, newflags);
+		ret = mlock_vma_pages_range(vma, start, end, newflags);
 	}
 out:
 	*prev = vma;
@@ -733,9 +738,12 @@ 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)
+		/* Ignore errors except EINTR, but prev needs fixing up. */
+		if (error) {
+			if (error == -EINTR)
+				break;
 			prev = vma;
+		}
 		cond_resched();
 	}
 out:
mm/mprotect.c +3 -1
index c0571445bef7..49dbb7156936 100644
@@ -765,7 +765,9 @@ 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 < 0)
+		goto fail;
 	vm_flags_reset_once(vma, newflags);
 	if (vma_wants_manual_pte_write_upgrade(vma))
 		mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
mm/mremap.c +3 -1
index 2be876a70cc0..aef1e5f373c7 100644
@@ -1286,7 +1286,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/pagewalk.c +14 -6
index a94c401ab2cf..dc9f7a7709c6 100644
@@ -425,14 +425,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;
@@ -444,6 +443,7 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma,
 		break;
 	}
 #endif
+	return 0;
 }
 
 /*
@@ -487,7 +487,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);
@@ -704,6 +706,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;
@@ -711,7 +714,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);
 }
 
@@ -734,6 +739,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;
@@ -741,7 +747,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);
 }
 
mm/vma.c +67 -27

242 lines not on this page · raw message

mm/vma_exec.c +4 -2
index 8134e1afca68..a4addc2a8480 100644
@@ -40,6 +40,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);
 
@@ -55,8 +56,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

base-commit: a1a876489abcc1e75b03bd3b2f6739ceeaaec8c5

Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Suren,

kernel test robot noticed the following build warnings:

[auto build test WARNING on a1a876489abcc1e75b03bd3b2f6739ceeaaec8c5]

url:    https://github.com/intel-lab-lkp/linux/commits/Suren-Baghdasaryan/mm-replace-vma_start_write-with-vma_start_write_killable/20260210-061104
base:   a1a876489abcc1e75b03bd3b2f6739ceeaaec8c5
patch link:    https://lore.kernel.org/r/20260209220849.2126486-1-surenb%40google.com
patch subject: [PATCH 1/1] mm: replace vma_start_write() with vma_start_write_killable()
config: nios2-allnoconfig (https://download.01.org/0day-ci/archive/20260210/[email protected]/config)
compiler: nios2-linux-gcc (GCC) 11.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260210/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> Warning: mm/memory.c:381 duplicate section name 'Note'
>> Warning: mm/memory.c:381 duplicate section name 'Note'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Suren Baghdasaryan
kernel test robot noticed the following build warnings:

Hi Suren,

kernel test robot noticed the following build warnings:

[auto build test WARNING on a1a876489abcc1e75b03bd3b2f6739ceeaaec8c5]

url:    https://github.com/intel-lab-lkp/linux/commits/Suren-Baghdasaryan/mm-replace-vma_start_write-with-vma_start_write_killable/20260210-061104
base:   a1a876489abcc1e75b03bd3b2f6739ceeaaec8c5
patch link:    https://lore.kernel.org/r/20260209220849.2126486-1-surenb%40google.com
patch subject: [PATCH 1/1] mm: replace vma_start_write() with vma_start_write_killable()
config: x86_64-kexec (https://download.01.org/0day-ci/archive/20260210/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260210/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> Warning: mm/memory.c:381 duplicate section name 'Note'
>> Warning: mm/memory.c:381 duplicate section name 'Note'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
reviewer
#3 lore raw
In reply to Suren Baghdasaryan
Quoted 3 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.

[...]

Quoted 4 lines
mm/mempolicy.c +0 -0
index dbd48502ac24..3de7ab4f4cee 100644

[...]

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

Quoted 7 lines
mm/mempolicy.c +2 -1
@@ -1808,7 +1817,11 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le
                        break;
                }
 
-               vma_start_write(vma);
+               if (vma_start_write_killable(vma)) {
+                       err = -EINTR;

Doesn't this need mpol_put(new)? Or less complicated, move the vma_start_write_killable() up to somewhere above the mpol_dup() call.

Quoted 6 lines
mm/mempolicy.c +3 -0
+                       break;
+               }
+
                new->home_node = home_node;
                err = mbind_range(&vmi, vma, &prev, start, end, new);
                mpol_put(new);

[...]

Quoted 8 lines
mm/pagewalk.c +0 -0
index a94c401ab2cf..dc9f7a7709c6 100644
@@ -425,14 +425,13 @@ static inline void process_mm_walk_lock(struct mm_struct *mm,
                mmap_assert_write_locked(mm);
 }
 

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

Quoted 10 lines
mm/pagewalk.c +2 -3
-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);

There are two users of PGWALK_WRLOCK in arch/s390/mm/gmap.c code that don't check pagewalk return values, have you checked that they are not negatively affected by this new possible error return?

Quoted 3 lines
mm/pagewalk.c +0 -0
        case PGWALK_WRLOCK_VERIFY:
                vma_assert_write_locked(vma);
                break;

[...]

Quoted 6 lines
mm/vma.c +0 -0
index be64f781a3aa..3cfb81b3b7cf 100644
@@ -540,8 +540,12 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
        if (new->vm_ops && new->vm_ops->open)

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

Quoted 10 lines
mm/vma.c +6 -2
                new->vm_ops->open(new);
 
-       vma_start_write(vma);
-       vma_start_write(new);
+       err = vma_start_write_killable(vma);
+       if (err)
+               goto out_fput;
+       err = vma_start_write_killable(new);
+       if (err)
+               goto out_fput;

What about the new->vm_ops->open() call and the anon_vma_clone() above? I don't think the error path properly undoes either. These calls should probably be moved further up, so that the point of no return in this function stays where it was.

Quoted 2 lines
mm/vma.c +0 -0
        init_vma_prep(&vp, vma);
        vp.insert = new;

[...]

Quoted 20 lines
mm/vma.c +4 -3
@@ -1155,10 +1168,12 @@ int vma_expand(struct vma_merge_struct *vmg)
        struct vm_area_struct *next = vmg->next;
        bool remove_next = false;
        vm_flags_t sticky_flags;
-       int ret = 0;
+       int ret;
 
        mmap_assert_write_locked(vmg->mm);
-       vma_start_write(target);
+       ret = vma_start_write_killable(target);
+       if (ret)
+               return ret;
 
        if (next && target != next && vmg->end == next->vm_end)
                remove_next = true;
@@ -1186,17 +1201,19 @@ 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)

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

Quoted 10 lines
mm/vma.c +5 -0
+       if (remove_next) {
+               ret = vma_start_write_killable(next);
+               if (ret)
+                       return ret;
                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;

nit: the control flow here is kinda chaotic, with some "if (ret) return ret;" mixed with "if (!ret && ...) ret = ...;".

Quoted 9 lines
mm/vma.c +1 -3
-       if (remove_next) {
-               vma_start_write(next);
+       if (remove_next)
                vmg->__remove_next = true;
-       }
        if (commit_merge(vmg))
                goto nomem;
 

[...]

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

Quoted 8 lines
mm/vma.c +1 -2
@@ -2211,9 +2240,8 @@ int mm_take_all_locks(struct mm_struct *mm)
         * is reached.
         */
        for_each_vma(vmi, vma) {
-               if (signal_pending(current))
+               if (vma_start_write_killable(vma))
                        goto out_unlock;
-               vma_start_write(vma);

nit: might want to keep the signal_pending() so that this can sort of be interrupted by non-fatal signals, which seems to be the intention

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

Quoted 11 lines
mm/vma.c +3 -1
        }
 
        vma_iter_init(&vmi, mm, 0);
@@ -2549,7 +2577,9 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap)
 #endif
 
        /* Lock the VMA since it is modified after insertion into VMA tree */
-       vma_start_write(vma);
+       error = vma_start_write_killable(vma);
+       if (error)
+               goto free_iter_vma;

This seems way past the point of no return, we've already called the ->mmap() handler which I think means removing the VMA again would require a ->close() call. The VMA should be locked further up if we want to do it killably.

Quoted 4 lines
mm/vma.c +0 -0
        vma_iter_store_new(vmi, vma);
        map->mm->map_count++;
        vma_link_file(vma, map->hold_file_rmap_lock);
author reviewer
#4 lore raw
Quoted 15 lines · Jann Horn
Quoted 3 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.

[...]

Quoted 4 lines
mm/mempolicy.c +0 -0
index dbd48502ac24..3de7ab4f4cee 100644

[...]

Quoted 4 lines
mm/mempolicy.c +0 -0
@@ -1808,7 +1817,11 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le
                        break;
                }
 
Quoted 6 lines

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

Quoted 3 lines
mm/mempolicy.c +2 -1
-               vma_start_write(vma);
+               if (vma_start_write_killable(vma)) {
+                       err = -EINTR;

Doesn't this need mpol_put(new)? Or less complicated, move the vma_start_write_killable() up to somewhere above the mpol_dup() call.

Thanks for the review, Jann!

Yes you are right. I'll move it before mpol_dup().

Quoted 16 lines
Quoted 7 lines
mm/mempolicy.c +3 -0
+                       break;
+               }
+
                new->home_node = home_node;
                err = mbind_range(&vmi, vma, &prev, start, end, new);
                mpol_put(new);

[...]

Quoted 8 lines
mm/pagewalk.c +0 -0
index a94c401ab2cf..dc9f7a7709c6 100644
@@ -425,14 +425,13 @@ static inline void process_mm_walk_lock(struct mm_struct *mm,
                mmap_assert_write_locked(mm);
 }
 
Quoted 14 lines

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

Quoted 10 lines
mm/pagewalk.c +2 -3
-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);

There are two users of PGWALK_WRLOCK in arch/s390/mm/gmap.c code that don't check pagewalk return values, have you checked that they are not negatively affected by this new possible error return?

Uh, even the ones which check for the error like queue_pages_range() don't seem to handle it well. I'll split this part into a separate patch as I think it will be sizable and will go over all users to ensure they handle the new error.

Quoted 13 lines
Quoted 4 lines
mm/pagewalk.c +0 -0
        case PGWALK_WRLOCK_VERIFY:
                vma_assert_write_locked(vma);
                break;

[...]

Quoted 8 lines
mm/vma.c +0 -0
index be64f781a3aa..3cfb81b3b7cf 100644
@@ -540,8 +540,12 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
        if (new->vm_ops && new->vm_ops->open)
                new->vm_ops->open(new);
 
Quoted 13 lines

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

Quoted 8 lines
mm/vma.c +6 -2
-       vma_start_write(vma);
-       vma_start_write(new);
+       err = vma_start_write_killable(vma);
+       if (err)
+               goto out_fput;
+       err = vma_start_write_killable(new);
+       if (err)
+               goto out_fput;

What about the new->vm_ops->open() call and the anon_vma_clone() above? I don't think the error path properly undoes either. These calls should probably be moved further up, so that the point of no return in this function stays where it was.

Ack.

Quoted 29 lines
Quoted 3 lines
mm/vma.c +0 -0
        init_vma_prep(&vp, vma);
        vp.insert = new;

[...]

Quoted 25 lines
mm/vma.c +8 -3
@@ -1155,10 +1168,12 @@ int vma_expand(struct vma_merge_struct *vmg)
        struct vm_area_struct *next = vmg->next;
        bool remove_next = false;
        vm_flags_t sticky_flags;
-       int ret = 0;
+       int ret;
 
        mmap_assert_write_locked(vmg->mm);
-       vma_start_write(target);
+       ret = vma_start_write_killable(target);
+       if (ret)
+               return ret;
 
        if (next && target != next && vmg->end == next->vm_end)
                remove_next = true;
@@ -1186,17 +1201,19 @@ 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)
+       if (remove_next) {
+               ret = vma_start_write_killable(next);
+               if (ret)
+                       return ret;
                ret = dup_anon_vma(target, next, &anon_dup);
Quoted 8 lines

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

Quoted 5 lines
mm/vma.c +1 -0
+       }
        if (!ret && vmg->copied_from)
                ret = dup_anon_vma(target, vmg->copied_from, &anon_dup);
        if (ret)
                return ret;

nit: the control flow here is kinda chaotic, with some "if (ret) return ret;" mixed with "if (!ret && ...) ret = ...;".

I'll see what I can do about it but probably as a separate patch.

Quoted 11 lines
Quoted 10 lines
mm/vma.c +1 -3
-       if (remove_next) {
-               vma_start_write(next);
+       if (remove_next)
                vmg->__remove_next = true;
-       }
        if (commit_merge(vmg))
                goto nomem;
 

[...]

Quoted 11 lines

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

Quoted 8 lines
mm/vma.c +1 -2
@@ -2211,9 +2240,8 @@ int mm_take_all_locks(struct mm_struct *mm)
         * is reached.
         */
        for_each_vma(vmi, vma) {
-               if (signal_pending(current))
+               if (vma_start_write_killable(vma))
                        goto out_unlock;
-               vma_start_write(vma);

nit: might want to keep the signal_pending() so that this can sort of be interrupted by non-fatal signals, which seems to be the intention

Yes, I will bring back that check.

Quoted 7 lines
Quoted 7 lines
mm/vma.c +0 -0
        }
 
        vma_iter_init(&vmi, mm, 0);
@@ -2549,7 +2577,9 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap)
 #endif
 
Quoted 10 lines

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

Quoted 5 lines
mm/vma.c +3 -1
        /* Lock the VMA since it is modified after insertion into VMA tree */
-       vma_start_write(vma);
+       error = vma_start_write_killable(vma);
+       if (error)
+               goto free_iter_vma;

This seems way past the point of no return, we've already called the ->mmap() handler which I think means removing the VMA again would require a ->close() call. The VMA should be locked further up if we want to do it killably.

Yeah, I realized this big issue after posting the patch. Moving it up seems possible, so I'll try that.

Thanks,
Suren.

Quoted 5 lines
Quoted 5 lines
mm/vma.c +0 -0
        vma_iter_store_new(vmi, vma);
        map->mm->map_count++;
        vma_link_file(vma, map->hold_file_rmap_lock);
reviewer Reviewed-by on v1 1/1
#5 lore raw
In reply to Suren Baghdasaryan
Quoted 41 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. userfaultd code, where some paths calling vma_start_write() can handle EINTR and some can't without a deeper code refactoring.

3. 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.

---
 arch/powerpc/kvm/book3s_hv_uvmem.c |  5 +-
 include/linux/mempolicy.h          |  5 +-
 mm/khugepaged.c                    |  5 +-
 mm/madvise.c                       |  4 +-
 mm/memory.c                        |  2 +
 mm/mempolicy.c                     | 23 ++++++--
 mm/mlock.c                         | 20 +++++--
 mm/mprotect.c                      |  4 +-
 mm/mremap.c                        |  4 +-
 mm/pagewalk.c                      | 20 +++++--
 mm/vma.c                           | 94 +++++++++++++++++++++---------
 mm/vma_exec.c                      |  6 +-
 12 files changed, 139 insertions(+), 53 deletions(-)
arch/powerpc/kvm/book3s_hv_uvmem.c +0 -0
index 7cf9310de0ec..69750edcf8d5 100644
@@ -410,7 +410,10 @@ static int kvmppc_memslot_page_merge(struct kvm *kvm,
 			ret = H_STATE;

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

Quoted 10 lines
arch/powerpc/kvm/book3s_hv_uvmem.c +4 -1
 			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,

The above change w.r.t. powerpc error handling in function kvmppc_memslot_page_merge() looks good to me.

Please feel free to add:

-ritesh